Skip to contentSkip to conversation

virtio: Introduce VirtIO driver support

PATCH New Bin Meng 4 participants 82 messages Sep 23, 2018 – Oct 22, 2018

Subsystems (9)
Arm (32-bit) x86 SCSI Block layer +5 more subsystems
lore
Share & tools
lore

Bin Meng

author
#0 lore raw

cover virtio: Introduce VirtIO driver support

This series brings in VirtIO driver support in U-Boot. The work is based on Tuomas's virtio support on QEMU ARM targets.

VirtIO is a virtualization standard for network and disk device drivers where just the guest's device driver "knows" it is running in a virtual environment, and cooperates with the hypervisor. This enables guests to get high performance network and disk operations, and gives most of the performance benefits of paravirtualization. In the U-Boot case, the guest is U-Boot itself, while the virtual environment are normally QEMU targets like ARM, RISC-V and x86.

Both VirtIO MMIO and PCI transport options are supported. Only VirtIO network and block device drivers are supported for now.

The following QEMU targets are supported.

- qemu_arm_defconfig
- qemu_arm64_defconfig
- qemu-riscv32_defconfig
- qemu-riscv64_defconfig
- qemu-x86_defconfig
- qemu-x86_64_defconfig

A new child_post_probe() uclass driver method is introduced to better support virtio device driver. This also changes BLK uclass driver to supply a post_probe() method which calls part_init(), so that we can avoid duplicating such call in every BLK driver.

Not every checkpatch warning reported was fixed, but I tried to fix as many as possible which makes sense.

This series needs to be applied after the risc-v QEMU series is applied.

This series is available at u-boot-x86/virtio-working for testing. Note the branch already contains the risc-v QEMU series plus one network stack fix to make virtio-net driver happy.

Bin Meng (22):
dm: core: Allow uclass to set up a device's child after it is probed
dm: Add a new uclass driver for VirtIO transport devices
virtio: Add virtio over mmio transport driver
test: dm: blk: Correct blk_base test case
sandbox: blk: Switch to use platdata_auto_alloc_size for the driver
data
efi_driver: blk: Switch to use platdata_auto_alloc_size for the driver
data
blk: Call part_init() in the post_probe() method
blk: Drop blk_prepare_device()
blk: Make blk_next_free_devnum() public
arm: qemu: Add a Kconfig in the board directory
arm: qemu: Enumerate virtio bus during early boot
riscv: qemu: Enumerate virtio bus during early boot
riscv: qemu: Include some useful commands
kconfig: Introduce HAVE_ARCH_IOMAP
x86: Implement arch-specific io accessor routines
virtio: Add virtio over pci transport driver
x86: qemu: Imply virtio PCI transport and device drivers
dm: pci: Add APIs to find next capability and extended capability
test: dm: pci: Add cases for finding next PCI capability APIs
virtio: pci: Support non-legacy PCI transport device
virtio: net: Support non-legacy device
doc: Document virtio support

Tuomas Tynkkynen (5):
virtio: Add codes for virtual queue/ring management
virtio: Add net driver support
blk: Introduce IF_TYPE_VIRTIO
virtio: Add block driver support
virtio: cmd: Add virtio command for virtio block devices

 arch/Kconfig                            |   1 +
 arch/arm/Kconfig                        |   1 +
 arch/x86/include/asm/io.h               |  66 +++
 board/emulation/qemu-arm/Kconfig        |  12 +
 board/emulation/qemu-arm/qemu-arm.c     |   7 +
 board/emulation/qemu-riscv/Kconfig      |  11 +
 board/emulation/qemu-riscv/qemu-riscv.c |   7 +
 board/emulation/qemu-x86/Kconfig        |   3 +
 cmd/Kconfig                             |   7 +
 cmd/Makefile                            |   1 +
 cmd/sata.c                              |   9 -
 cmd/virtio.c                            |  37 ++
 common/usb_storage.c                    |   4 +-
 configs/qemu_arm64_defconfig            |   1 -
 configs/qemu_arm_defconfig              |   1 -
 disk/part.c                             |   6 +
 doc/README.virtio                       | 247 ++++++++++++
 drivers/Kconfig                         |   2 +
 drivers/Makefile                        |   1 +
 drivers/block/blk-uclass.c              |  25 +-
 drivers/block/ide.c                     |   2 -
 drivers/block/sandbox.c                 |  17 +-
 drivers/core/uclass.c                   |  13 +-
 drivers/mmc/mmc.c                       |   3 -
 drivers/nvme/nvme.c                     |   1 -
 drivers/pci/pci-uclass.c                |  48 ++-
 drivers/scsi/scsi.c                     |   1 -
 drivers/virtio/Kconfig                  |  43 ++
 drivers/virtio/Makefile                 |  10 +
 drivers/virtio/virtio-uclass.c          | 333 +++++++++++++++
 drivers/virtio/virtio_blk.c             | 127 ++++++
 drivers/virtio/virtio_blk.h             | 129 ++++++
 drivers/virtio/virtio_mmio.c            | 413 +++++++++++++++++++
 drivers/virtio/virtio_mmio.h            | 129 ++++++
 drivers/virtio/virtio_net.c             | 236 +++++++++++
 drivers/virtio/virtio_net.h             | 268 ++++++++++++
 drivers/virtio/virtio_pci.h             | 173 ++++++++
 drivers/virtio/virtio_pci_legacy.c      | 420 +++++++++++++++++++
 drivers/virtio/virtio_pci_modern.c      | 612 ++++++++++++++++++++++++++++
 drivers/virtio/virtio_ring.c            | 356 ++++++++++++++++
 include/blk.h                           |  22 +-
 include/dm/uclass-id.h                  |   1 +
 include/dm/uclass.h                     |   4 +-
 include/linux/io.h                      |   4 +
 include/pci.h                           |  48 +++
 include/virtio.h                        | 694 ++++++++++++++++++++++++++++++++
 include/virtio_ring.h                   | 320 +++++++++++++++
 include/virtio_types.h                  |  24 ++
 lib/Kconfig                             |   6 +
 lib/efi_driver/efi_block_device.c       |  26 +-
 test/dm/blk.c                           |  27 +-
 test/dm/pci.c                           |  20 +
 52 files changed, 4882 insertions(+), 97 deletions(-)
 create mode 100644 board/emulation/qemu-arm/Kconfig
 create mode 100644 cmd/virtio.c
 create mode 100644 doc/README.virtio
 create mode 100644 drivers/virtio/Kconfig
 create mode 100644 drivers/virtio/Makefile
 create mode 100644 drivers/virtio/virtio-uclass.c
 create mode 100644 drivers/virtio/virtio_blk.c
 create mode 100644 drivers/virtio/virtio_blk.h
 create mode 100644 drivers/virtio/virtio_mmio.c
 create mode 100644 drivers/virtio/virtio_mmio.h
 create mode 100644 drivers/virtio/virtio_net.c
 create mode 100644 drivers/virtio/virtio_net.h
 create mode 100644 drivers/virtio/virtio_pci.h
 create mode 100644 drivers/virtio/virtio_pci_legacy.c
 create mode 100644 drivers/virtio/virtio_pci_modern.c
 create mode 100644 drivers/virtio/virtio_ring.c
 create mode 100644 include/virtio.h
 create mode 100644 include/virtio_ring.h
 create mode 100644 include/virtio_types.h
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 1/27 dm: core: Allow uclass to set up a device's child after it is probed

Some buses need to set up their child devices after they are probed. Support a common child_post_probe() method for the uclass.

With this change, the two APIs uclass_pre_probe_device() and uclass_post_probe_device() become symmetric.

---

 drivers/core/uclass.c | 13 ++++++++++++-
 include/dm/uclass.h   |  4 +++-
 2 files changed, 15 insertions(+), 2 deletions(-)
drivers/core/uclass.c +12 -1
index 3113d6a..3c7b9cf 100644
@@ -687,8 +687,19 @@ int uclass_pre_probe_device(struct udevice *dev)
 
 int uclass_post_probe_device(struct udevice *dev)
 {
-	struct uclass_driver *uc_drv = dev->uclass->uc_drv;
+	struct uclass_driver *uc_drv;
+	int ret;
+
+	if (dev->parent) {
+		uc_drv = dev->parent->uclass->uc_drv;
+		if (uc_drv->child_post_probe) {
+			ret = uc_drv->child_post_probe(dev);
+			if (ret)
+				return ret;
+		}
+	}
 
+	uc_drv = dev->uclass->uc_drv;
 	if (uc_drv->post_probe)
 		return uc_drv->post_probe(dev);
 
include/dm/uclass.h +3 -1
index 6e7c1cd..610c68d 100644
@@ -58,7 +58,8 @@ struct udevice;
  * @post_probe: Called after a new device is probed
  * @pre_remove: Called before a device is removed
  * @child_post_bind: Called after a child is bound to a device in this uclass
- * @child_pre_probe: Called before a child is probed in this uclass
+ * @child_pre_probe: Called before a child in this uclass is probed
+ * @child_post_probe: Called after a child in this uclass is probed
  * @init: Called to set up the uclass
  * @destroy: Called to destroy the uclass
  * @priv_auto_alloc_size: If non-zero this is the size of the private data
@@ -91,6 +92,7 @@ struct uclass_driver {
 	int (*pre_remove)(struct udevice *dev);
 	int (*child_post_bind)(struct udevice *dev);
 	int (*child_pre_probe)(struct udevice *dev);
+	int (*child_post_probe)(struct udevice *dev);
 	int (*init)(struct uclass *class);
 	int (*destroy)(struct uclass *class);
 	int priv_auto_alloc_size;
Signature · 1 line
2.7.4
#2 lore raw

PATCH 1/27 dm: core: Allow uclass to set up a device's child after it is probed

Hi Bin,

Quoted 6 lines · Bin Meng

Some buses need to set up their child devices after they are probed. Support a common child_post_probe() method for the uclass.

With this change, the two APIs uclass_pre_probe_device() and uclass_post_probe_device() become symmetric.

Quoted 6 lines

---

 drivers/core/uclass.c | 13 ++++++++++++-
 include/dm/uclass.h   |  4 +++-
 2 files changed, 15 insertions(+), 2 deletions(-)

Another option, perhaps not quite as elegant, is for the driver to call into the uclass in its probe() method. We need to balance elegance with the cost of adding a new field to the uclass which is rarely used. What do you think?

Also, this does need some sort of use in sandbox, so can you update the test driver to use it?

Quoted 49 lines
drivers/core/uclass.c +12 -1
index 3113d6a..3c7b9cf 100644
@@ -687,8 +687,19 @@ int uclass_pre_probe_device(struct udevice *dev)
 
 int uclass_post_probe_device(struct udevice *dev)
 {
-       struct uclass_driver *uc_drv = dev->uclass->uc_drv;
+       struct uclass_driver *uc_drv;
+       int ret;
+
+       if (dev->parent) {
+               uc_drv = dev->parent->uclass->uc_drv;
+               if (uc_drv->child_post_probe) {
+                       ret = uc_drv->child_post_probe(dev);
+                       if (ret)
+                               return ret;
+               }
+       }
 
+       uc_drv = dev->uclass->uc_drv;
        if (uc_drv->post_probe)
                return uc_drv->post_probe(dev);
 
include/dm/uclass.h +3 -2
index 6e7c1cd..610c68d 100644
@@ -58,7 +58,8 @@ struct udevice;
  * @post_probe: Called after a new device is probed
  * @pre_remove: Called before a device is removed
  * @child_post_bind: Called after a child is bound to a device in this uclass
- * @child_pre_probe: Called before a child is probed in this uclass
+ * @child_pre_probe: Called before a child in this uclass is probed
+ * @child_post_probe: Called after a child in this uclass is probed
  * @init: Called to set up the uclass
  * @destroy: Called to destroy the uclass
  * @priv_auto_alloc_size: If non-zero this is the size of the private data
@@ -91,6 +92,7 @@ struct uclass_driver {
        int (*pre_remove)(struct udevice *dev);
        int (*child_post_bind)(struct udevice *dev);
        int (*child_pre_probe)(struct udevice *dev);
+       int (*child_post_probe)(struct udevice *dev);
        int (*init)(struct uclass *class);
        int (*destroy)(struct uclass *class);
        int priv_auto_alloc_size;
--
Quoted 2 lines

2.7.4

Regards,
Simon

author
#3 lore raw

PATCH 1/27 dm: core: Allow uclass to set up a device's child after it is probed

Hi Simon,

Quoted 13 lines · Simon Glass

Hi Bin,

Quoted 9 lines · Bin Meng

Some buses need to set up their child devices after they are probed. Support a common child_post_probe() method for the uclass.

With this change, the two APIs uclass_pre_probe_device() and uclass_post_probe_device() become symmetric.

---

Quoted 9 lines
Quoted 3 lines
 drivers/core/uclass.c | 13 ++++++++++++-
 include/dm/uclass.h   |  4 +++-
 2 files changed, 15 insertions(+), 2 deletions(-)

Another option, perhaps not quite as elegant, is for the driver to call into the uclass in its probe() method. We need to balance elegance with the cost of adding a new field to the uclass which is rarely used. What do you think?

Yes, we can use driver's probe() method to achieve the same goal, but that seems a little bit duplicated effort for every driver. Also I believe we should make uclass_pre_probe_device() and uclass_post_probe_device() symmetric.

Quoted 3 lines

Also, this does need some sort of use in sandbox, so can you update the test driver to use it?

Yes, will add in v2.

Regards,
Bin

author
#4 lore raw

PATCH 2/27 dm: Add a new uclass driver for VirtIO transport devices

In reply to Bin Meng

This adds a new virtio uclass driver for “virtio” [1] family of devices that are are found in virtual environments like QEMU, yet by design they look like physical devices to the guest.

The uclass driver provides child_pre_probe() and child_post_probe() methods to do some common operations for virtio device drivers like device and driver supported feature negotiation, etc.

[1] http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf

---

 drivers/Kconfig                |   2 +
 drivers/Makefile               |   1 +
 drivers/virtio/Kconfig         |  14 +
 drivers/virtio/Makefile        |   6 +
 drivers/virtio/virtio-uclass.c | 333 ++++++++++++++++++++
 include/dm/uclass-id.h         |   1 +
 include/virtio.h               | 694 +++++++++++++++++++++++++++++++++++++++++
 include/virtio_types.h         |  24 ++
 8 files changed, 1075 insertions(+)
 create mode 100644 drivers/virtio/Kconfig
 create mode 100644 drivers/virtio/Makefile
 create mode 100644 drivers/virtio/virtio-uclass.c
 create mode 100644 include/virtio.h
 create mode 100644 include/virtio_types.h
drivers/Kconfig +2 -0
index 56536c4..d40db0d 100644
@@ -106,6 +106,8 @@ source "drivers/usb/Kconfig"
 
 source "drivers/video/Kconfig"
 
+source "drivers/virtio/Kconfig"
+
 source "drivers/watchdog/Kconfig"
 
 config PHYS_TO_BUS
drivers/Makefile +1 -0
index 23ea609..f09daae 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_$(SPL_TPL_)SERIAL_SUPPORT) += serial/
 obj-$(CONFIG_$(SPL_TPL_)SPI_FLASH_SUPPORT) += mtd/spi/
 obj-$(CONFIG_$(SPL_TPL_)SPI_SUPPORT) += spi/
 obj-$(CONFIG_$(SPL_TPL_)TIMER) += timer/
+obj-$(CONFIG_$(SPL_TPL_)VIRTIO) += virtio/
 obj-$(CONFIG_$(SPL_)DM_MAILBOX) += mailbox/
 obj-$(CONFIG_$(SPL_)REMOTEPROC) += remoteproc/
 
drivers/virtio/Kconfig new file +14 -0
new file mode 100644
index 0000000..bdfa96a
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+# Copyright (C) 2018, Bin Meng <[email protected]>
+
+menu "VirtIO Drivers"
+
+config VIRTIO
+	bool
+	help
+	  This option is selected by any driver which implements the virtio
+	  bus, such as CONFIG_VIRTIO_MMIO or CONFIG_VIRTIO_PCI.
+
+endmenu
drivers/virtio/Makefile new file +6 -0
new file mode 100644
index 0000000..23e7be7
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+# Copyright (C) 2018, Bin Meng <[email protected]>
+
+obj-y += virtio-uclass.o
drivers/virtio/virtio-uclass.c new file +333 -0

333 lines not on this page · raw message

include/dm/uclass-id.h +1 -0
index 7027ea0..08b2a8f 100644
@@ -93,6 +93,7 @@ enum uclass_id {
 	UCLASS_VIDEO_BRIDGE,	/* Video bridge, e.g. DisplayPort to LVDS */
 	UCLASS_VIDEO_CONSOLE,	/* Text console driver for video device */
 	UCLASS_WDT,		/* Watchdot Timer driver */
+	UCLASS_VIRTIO,		/* VirtIO transport device */
 
 	UCLASS_COUNT,
 	UCLASS_INVALID = -1,
include/virtio.h new file +694 -0

694 lines not on this page · raw message

include/virtio_types.h new file +24 -0
new file mode 100644
index 0000000..d700d19
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ *
+ * From Linux kernel include/uapi/linux/virtio_types.h
+ */
+
+#ifndef _LINUX_VIRTIO_TYPES_H
+#define _LINUX_VIRTIO_TYPES_H
+
+#include <linux/types.h>
+
+/*
+ * __virtio{16,32,64} have the following meaning:
+ * - __u{16,32,64} for virtio devices in legacy mode, accessed in native endian
+ * - __le{16,32,64} for standard-compliant virtio devices
+ */
+
+typedef __u16 __bitwise __virtio16;
+typedef __u32 __bitwise __virtio32;
+typedef __u64 __bitwise __virtio64;
+
+#endif /* _LINUX_VIRTIO_TYPES_H */
Signature · 1 line
2.7.4
#5 lore raw

PATCH 2/27 dm: Add a new uclass driver for VirtIO transport devices

Hi Bin,

Quoted 9 lines · Bin Meng

This adds a new virtio uclass driver for “virtio” [1] family of devices that are are found in virtual environments like QEMU, yet by design they look like physical devices to the guest.

The uclass driver provides child_pre_probe() and child_post_probe() methods to do some common operations for virtio device drivers like device and driver supported feature negotiation, etc.

[1] http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf

Can you add this link to the header file too? This seems to be lacking docs.in the source code. Also in a header file, a short statement of what virtio is for would be good.

Quoted 53 lines

---

 drivers/Kconfig                |   2 +
 drivers/Makefile               |   1 +
 drivers/virtio/Kconfig         |  14 +
 drivers/virtio/Makefile        |   6 +
 drivers/virtio/virtio-uclass.c | 333 ++++++++++++++++++++
 include/dm/uclass-id.h         |   1 +
 include/virtio.h               | 694 +++++++++++++++++++++++++++++++++++++++++
 include/virtio_types.h         |  24 ++
 8 files changed, 1075 insertions(+)
 create mode 100644 drivers/virtio/Kconfig
 create mode 100644 drivers/virtio/Makefile
 create mode 100644 drivers/virtio/virtio-uclass.c
 create mode 100644 include/virtio.h
 create mode 100644 include/virtio_types.h
drivers/Kconfig +2 -0
index 56536c4..d40db0d 100644
@@ -106,6 +106,8 @@ source "drivers/usb/Kconfig"
 
 source "drivers/video/Kconfig"
 
+source "drivers/virtio/Kconfig"
+
 source "drivers/watchdog/Kconfig"
 
 config PHYS_TO_BUS
drivers/Makefile +1 -0
index 23ea609..f09daae 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_$(SPL_TPL_)SERIAL_SUPPORT) += serial/
 obj-$(CONFIG_$(SPL_TPL_)SPI_FLASH_SUPPORT) += mtd/spi/
 obj-$(CONFIG_$(SPL_TPL_)SPI_SUPPORT) += spi/
 obj-$(CONFIG_$(SPL_TPL_)TIMER) += timer/
+obj-$(CONFIG_$(SPL_TPL_)VIRTIO) += virtio/
 obj-$(CONFIG_$(SPL_)DM_MAILBOX) += mailbox/
 obj-$(CONFIG_$(SPL_)REMOTEPROC) += remoteproc/
 
drivers/virtio/Kconfig new file +2 -0
new file mode 100644
index 0000000..bdfa96a
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0+
+#

drivers/virtio/Kconfig line 12

Quoted 10 lines
drivers/virtio/Kconfig new file +10 -0
+# Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+# Copyright (C) 2018, Bin Meng <[email protected]>
+
+menu "VirtIO Drivers"
+
+config VIRTIO
+       bool
+       help
+         This option is selected by any driver which implements the virtio
+         bus, such as CONFIG_VIRTIO_MMIO or CONFIG_VIRTIO_PCI.

What is a virtio bus?

Quoted 35 lines
drivers/virtio/Kconfig new file +2 -0
+
+endmenu
drivers/virtio/Makefile new file +6 -0
new file mode 100644
index 0000000..23e7be7
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+# Copyright (C) 2018, Bin Meng <[email protected]>
+
+obj-y += virtio-uclass.o
drivers/virtio/virtio-uclass.c new file +15 -0
new file mode 100644
index 0000000..1c85856
@@ -0,0 +1,333 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <virtio.h>
+#include <dm/device.h>
+#include <dm/lists.h>
+
+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,

drivers/virtio/virtio-uclass.c line 25

Quoted 10 lines
drivers/virtio/virtio-uclass.c new file +10 -0
+};
+
+int virtio_get_config(struct udevice *vdev, unsigned int offset,
+                     void *buf, unsigned int len)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->get_config)
+               return -ENOSYS;

blank line before return (fix globally)

Quoted 139 lines
drivers/virtio/virtio-uclass.c new file +139 -0
+       return ops->get_config(vdev->parent, offset, buf, len);
+}
+
+int virtio_set_config(struct udevice *vdev, unsigned int offset,
+                     void *buf, unsigned int len)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->set_config)
+               return -ENOSYS;
+       return ops->set_config(vdev->parent, offset, buf, len);
+}
+
+int virtio_generation(struct udevice *vdev, u32 *counter)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->generation)
+               return -ENOSYS;
+       return ops->generation(vdev->parent, counter);
+}
+
+int virtio_get_status(struct udevice *vdev, u8 *status)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->get_status)
+               return -ENOSYS;
+       return ops->get_status(vdev->parent, status);
+}
+
+int virtio_set_status(struct udevice *vdev, u8 status)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->set_status)
+               return -ENOSYS;
+       return ops->set_status(vdev->parent, status);
+}
+
+int virtio_reset(struct udevice *vdev)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->reset)
+               return -ENOSYS;
+       return ops->reset(vdev->parent);
+}
+
+int virtio_get_features(struct udevice *vdev, u64 *features)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->get_features)
+               return -ENOSYS;
+       return ops->get_features(vdev->parent, features);
+}
+
+int virtio_set_features(struct udevice *vdev)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->set_features)
+               return -ENOSYS;
+       return ops->set_features(vdev->parent);
+}
+
+int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
+                   struct virtqueue *vqs[])
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->find_vqs)
+               return -ENOSYS;
+       return ops->find_vqs(vdev->parent, nvqs, vqs);
+}
+
+int virtio_del_vqs(struct udevice *vdev)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->del_vqs)
+               return -ENOSYS;
+       return ops->del_vqs(vdev->parent);
+}
+
+int virtio_notify(struct udevice *vdev, struct virtqueue *vq)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->notify)
+               return -ENOSYS;
+       return ops->notify(vdev->parent, vq);
+}
+
+void virtio_add_status(struct udevice *vdev, u8 status)
+{
+       u8 old;
+
+       if (!virtio_get_status(vdev, &old))
+               virtio_set_status(vdev, old | status);
+}
+
+int virtio_finalize_features(struct udevice *vdev)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
+       u8 status;
+       int ret;
+
+       ret = virtio_set_features(vdev);
+       if (ret)
+               return ret;
+
+       if (uc_priv->legacy)
+               return 0;
+
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK);
+       ret = virtio_get_status(vdev, &status);
+       if (ret)
+               return ret;
+       if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
+               debug("(%s): device refuses features %x\n", vdev->name, status);
+               return -ENODEV;
+       }
+
+       return 0;
+}
+
+void virtio_driver_features_init(struct virtio_dev_priv *priv,

drivers/virtio/virtio-uclass.c line 174

Quoted 10 lines
drivers/virtio/virtio-uclass.c new file +10 -0
+                                u32 *feature, u32 feature_size,
+                                u32 *feature_legacy, u32 feature_legacy_size)
+{
+       priv->feature_table = feature;
+       priv->feature_table_size = feature_size;
+       priv->feature_table_legacy = feature_legacy;
+       priv->feature_table_size_legacy = feature_legacy_size;
+}
+
+void virtio_init(void)

Shouldn't this return int if there is an error?

Quoted 25 lines
drivers/virtio/virtio-uclass.c new file +25 -0
+{
+       struct udevice *bus;
+
+       /* Enumerate all known virtio devices */
+       for (uclass_first_device(UCLASS_VIRTIO, &bus);
+            bus;
+            uclass_next_device(&bus)) {
+               ;
+       }
+}
+
+static int virtio_uclass_post_probe(struct udevice *udev)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
+       char dev_name[30], *str;
+       struct udevice *vdev;
+       int ret;
+
+       if (uc_priv->device > VIRTIO_ID_MAX_NUM) {
+               debug("(%s): virtio device ID %d exceeds maximum num\n",
+                     udev->name, uc_priv->device);
+               return 0;
+       }
+
+       if (!virtio_drv_name[uc_priv->device]) {

drivers/virtio/virtio-uclass.c line 209

Quoted 10 lines
drivers/virtio/virtio-uclass.c new file +10 -0
+               debug("(%s): underlying virtio device driver unavailable\n",
+                     udev->name);
+               return 0;
+       }
+
+       snprintf(dev_name, sizeof(dev_name), "%s#%d",
+                virtio_drv_name[uc_priv->device], udev->seq);
+       str = strdup(dev_name);
+       if (!str)
+               return -ENOMEM;

You might consider using log_ret() or log_msg_ret() for your error returns

Quoted 10 lines
drivers/virtio/virtio-uclass.c new file +10 -0
+
+       ret = device_bind_driver(udev, virtio_drv_name[uc_priv->device],
+                                str, &vdev);
+       if (ret == -ENOENT) {
+               debug("(%s): no driver configured\n", udev->name);
+               return 0;
+       }
+       if (ret) {
+               free(str);
+               return ret;

drivers/virtio/virtio-uclass.c line 229

Quoted 10 lines
drivers/virtio/virtio-uclass.c new file +10 -0
+       }
+       device_set_name_alloced(vdev);
+
+       return 0;
+}
+
+static int virtio_uclass_child_post_bind(struct udevice *vdev)
+{
+       /* Acknowledge that we've seen the device */
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE);

Not sure this is worth having a new hook?

Quoted 109 lines
drivers/virtio/virtio-uclass.c new file +104 -0
+
+       return 0;
+}
+
+static int virtio_uclass_child_pre_probe(struct udevice *vdev)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
+       u64 device_features;
+       u64 driver_features;
+       u64 driver_features_legacy;
+       int i;
+       int ret;
+
+       /*
+        * Save the real virtio device (eg: virtio-net, virtio-blk) to
+        * the transport (parent) device's uclass priv for future use.
+        */
+       uc_priv->vdev = vdev;
+
+       /*
+        * We always start by resetting the device, in case a previous driver
+        * messed it up. This also tests that code path a little.
+        */
+       ret = virtio_reset(vdev);
+       if (ret)
+               goto err;
+
+       /* We have a driver! */
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER);
+
+       /* Figure out what features the device supports */
+       virtio_get_features(vdev, &device_features);
+       debug("(%s) plain device features supported %016llx\n",
+             vdev->name, device_features);
+       if (!(device_features & (1ULL << VIRTIO_F_VERSION_1)))
+               uc_priv->legacy = true;
+
+       /* Figure out what features the driver supports */
+       driver_features = 0;
+       for (i = 0; i < uc_priv->feature_table_size; i++) {
+               unsigned int f = uc_priv->feature_table[i];
+
+               WARN_ON(f >= 64);
+               driver_features |= (1ULL << f);
+       }
+
+       /* Some drivers have a separate feature table for virtio v1.0 */
+       if (uc_priv->feature_table_legacy) {
+               driver_features_legacy = 0;
+               for (i = 0; i < uc_priv->feature_table_size_legacy; i++) {
+                       unsigned int f = uc_priv->feature_table_legacy[i];
+
+                       WARN_ON(f >= 64);
+                       driver_features_legacy |= (1ULL << f);
+               }
+       } else {
+               driver_features_legacy = driver_features;
+       }
+
+       if (uc_priv->legacy) {
+               debug("(%s): legacy virtio device\n", vdev->name);
+               uc_priv->features = driver_features_legacy & device_features;
+       } else {
+               debug("(%s): v1.0 complaint virtio device\n", vdev->name);
+               uc_priv->features = driver_features & device_features;
+       }
+
+       /* Transport features always preserved to pass to finalize_features */
+       for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
+               if ((device_features & (1ULL << i)) &&
+                   (i == VIRTIO_F_VERSION_1))
+                       __virtio_set_bit(vdev->parent, i);
+
+       debug("(%s) final negotiated features supported %016llx\n",
+             vdev->name, uc_priv->features);
+       ret = virtio_finalize_features(vdev);
+       if (ret)
+               goto err;
+
+       return 0;
+
+err:
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED);
+       return ret;
+}
+
+static int virtio_uclass_child_post_probe(struct udevice *vdev)
+{
+       /* Indicates that the driver is set up and ready to drive the device */
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK);
+
+       return 0;
+}
+
+UCLASS_DRIVER(virtio) = {
+       .name   = "virtio",
+       .id     = UCLASS_VIRTIO,
+       .flags  = DM_UC_FLAG_SEQ_ALIAS,
+       .post_probe = virtio_uclass_post_probe,
+       .child_post_bind = virtio_uclass_child_post_bind,
+       .child_pre_probe = virtio_uclass_child_pre_probe,
+       .child_post_probe = virtio_uclass_child_post_probe,
+       .per_device_auto_alloc_size = sizeof(struct virtio_dev_priv),
+};
include/dm/uclass-id.h +0 -0
index 7027ea0..08b2a8f 100644

include/dm/uclass-id.h enum uclass_id { · line 96

Quoted 4 lines
include/dm/uclass-id.h +1 -0
@@ -93,6 +93,7 @@ enum uclass_id {
        UCLASS_VIDEO_BRIDGE,    /* Video bridge, e.g. DisplayPort to LVDS */
        UCLASS_VIDEO_CONSOLE,   /* Text console driver for video device */
        UCLASS_WDT,             /* Watchdot Timer driver */
+       UCLASS_VIRTIO,          /* VirtIO transport device */

V before W :-)

Quoted 12 lines
include/dm/uclass-id.h +0 -0
        UCLASS_COUNT,
        UCLASS_INVALID = -1,
include/virtio.h new file +3 -0
new file mode 100644
index 0000000..efbedb2
@@ -0,0 +1,694 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>

include/virtio.h line 13

Quoted 10 lines
include/virtio.h new file +10 -0
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ *
+ * This file is largely based on Linux kernel virtio_*.h files
+ */
+
+#ifndef __VIRTIO_H__
+#define __VIRTIO_H__
+
+#include <virtio_types.h>
+#include <dm/device.h>

Should be able to avoid this, just put in C file

Quoted 400 lines
include/virtio.h new file +400 -0
+
+#define VIRTIO_ID_NET          1 /* virtio net */
+#define VIRTIO_ID_BLOCK                2 /* virtio block */
+#define VIRTIO_ID_MAX_NUM      3
+
+#define VIRTIO_NET_DRV_NAME    "virtio-net"
+#define VIRTIO_BLK_DRV_NAME    "virtio-blk"
+
+/* Status byte for guest to report progress, and synchronize features */
+
+/* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
+#define VIRTIO_CONFIG_S_ACKNOWLEDGE    1
+/* We have found a driver for the device */
+#define VIRTIO_CONFIG_S_DRIVER         2
+/* Driver has used its parts of the config, and is happy */
+#define VIRTIO_CONFIG_S_DRIVER_OK      4
+/* Driver has finished configuring features */
+#define VIRTIO_CONFIG_S_FEATURES_OK    8
+/* Device entered invalid state, driver must reset it */
+#define VIRTIO_CONFIG_S_NEEDS_RESET    0x40
+/* We've given up on this device */
+#define VIRTIO_CONFIG_S_FAILED         0x80
+
+/*
+ * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END
+ * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.),
+ * the rest are per-device feature bits.
+ */
+#define VIRTIO_TRANSPORT_F_START       28
+#define VIRTIO_TRANSPORT_F_END         38
+
+#ifndef VIRTIO_CONFIG_NO_LEGACY
+/*
+ * Do we get callbacks when the ring is completely used,
+ * even if we've suppressed them?
+ */
+#define VIRTIO_F_NOTIFY_ON_EMPTY       24
+
+/* Can the device handle any descriptor layout? */
+#define VIRTIO_F_ANY_LAYOUT            27
+#endif /* VIRTIO_CONFIG_NO_LEGACY */
+
+/* v1.0 compliant */
+#define VIRTIO_F_VERSION_1             32
+
+/*
+ * If clear - device has the IOMMU bypass quirk feature.
+ * If set - use platform tools to detect the IOMMU.
+ *
+ * Note the reverse polarity (compared to most other features),
+ * this is for compatibility with legacy systems.
+ */
+#define VIRTIO_F_IOMMU_PLATFORM                33
+
+/* Does the device support Single Root I/O Virtualization? */
+#define VIRTIO_F_SR_IOV                        37
+
+/**
+ * virtio scatter-gather struct
+ *
+ * @addr:              sg buffer address
+ * @lengh:             sg buffer length
+ */
+struct virtio_sg {
+       void *addr;
+       size_t length;
+};
+
+struct virtqueue;
+
+/* virtio bus operations */
+struct dm_virtio_ops {
+       /**
+        * get_config() - read the value of a configuration field
+        *
+        * @vdev:       the real virtio device
+        * @offset:     the offset of the configuration field
+        * @buf:        the buffer to write the field value into
+        * @len:        the length of the buffer
+        * @return 0 if OK, -ve on error
+        */
+       int (*get_config)(struct udevice *vdev, unsigned int offset,
+                         void *buf, unsigned int len);
+       /**
+        * set_config() - write the value of a configuration field
+        *
+        * @vdev:       the real virtio device
+        * @offset:     the offset of the configuration field
+        * @buf:        the buffer to read the field value from
+        * @len:        the length of the buffer
+        * @return 0 if OK, -ve on error
+        */
+       int (*set_config)(struct udevice *vdev, unsigned int offset,
+                         const void *buf, unsigned int len);
+       /**
+        * generation() - config generation counter
+        *
+        * @vdev:       the real virtio device
+        * @counter:    the returned config generation counter
+        * @return 0 if OK, -ve on error
+        */
+       int (*generation)(struct udevice *vdev, u32 *counter);
+       /**
+        * get_status() - read the status byte
+        *
+        * @vdev:       the real virtio device
+        * @status:     the returned status byte
+        * @return 0 if OK, -ve on error
+        */
+       int (*get_status)(struct udevice *vdev, u8 *status);
+       /**
+        * set_status() - write the status byte
+        *
+        * @vdev:       the real virtio device
+        * @status:     the new status byte
+        * @return 0 if OK, -ve on error
+        */
+       int (*set_status)(struct udevice *vdev, u8 status);
+       /**
+        * reset() - reset the device
+        *
+        * @vdev:       the real virtio device
+        * @return 0 if OK, -ve on error
+        */
+       int (*reset)(struct udevice *vdev);
+       /**
+        * get_features() - get the array of feature bits for this device
+        *
+        * @vdev:       the real virtio device
+        * @features:   the first 32 feature bits (all we currently need)
+        * @return 0 if OK, -ve on error
+        */
+       int (*get_features)(struct udevice *vdev, u64 *features);
+       /**
+        * set_features() - confirm what device features we'll be using
+        *
+        * @vdev:       the real virtio device
+        * @return 0 if OK, -ve on error
+        */
+       int (*set_features)(struct udevice *vdev);
+       /**
+        * find_vqs() - find virtqueues and instantiate them
+        *
+        * @vdev:       the real virtio device
+        * @nvqs:       the number of virtqueues to find
+        * @vqs:        on success, includes new virtqueues
+        * @return 0 if OK, -ve on error
+        */
+       int (*find_vqs)(struct udevice *vdev, unsigned int nvqs,
+                       struct virtqueue *vqs[]);
+       /**
+        * del_vqs() - free virtqueues found by find_vqs()
+        *
+        * @vdev:       the real virtio device
+        * @return 0 if OK, -ve on error
+        */
+       int (*del_vqs)(struct udevice *vdev);
+       /**
+        * notify() - notify the device to process the queue
+        *
+        * @vdev:       the real virtio device
+        * @vq:         virtqueue to process
+        * @return 0 if OK, -ve on error
+        */
+       int (*notify)(struct udevice *vdev, struct virtqueue *vq);
+};
+
+/* Get access to a virtio bus' operations */
+#define virtio_get_ops(dev)    ((struct dm_virtio_ops *)(dev)->driver->ops)
+
+/**
+ * virtio uclass per device private data
+ *
+ * @vqs:                       virtualqueue for the virtio device
+ * @vdev:                      the real virtio device underneath
+ * @legacy:                    is it a legacy device?
+ * @device:                    virtio device ID
+ * @vendor:                    virtio vendor ID
+ * @features:                  negotiated supported features
+ * @feature_table:             an array of feature supported by the driver
+ * @feature_table_size:                number of entries in the feature table array
+ * @feature_table_legacy:      same as feature_table but working in legacy mode
+ * @feature_table_size_legacy: number of entries in feature table legacy array
+ */
+struct virtio_dev_priv {
+       struct list_head vqs;
+       struct udevice *vdev;
+       bool legacy;
+       u32 device;
+       u32 vendor;
+       u64 features;
+       u32 *feature_table;
+       u32 feature_table_size;
+       u32 *feature_table_legacy;
+       u32 feature_table_size_legacy;
+};
+
+/**
+ * virtio_get_config() - read the value of a configuration field
+ *
+ * @vdev:      the real virtio device
+ * @offset:    the offset of the configuration field
+ * @buf:       the buffer to write the field value into
+ * @len:       the length of the buffer
+ * @return 0 if OK, -ve on error
+ */
+int virtio_get_config(struct udevice *vdev, unsigned int offset,
+                     void *buf, unsigned int len);
+
+/**
+ * virtio_set_config() - write the value of a configuration field
+ *
+ * @vdev:      the real virtio device
+ * @offset:    the offset of the configuration field
+ * @buf:       the buffer to read the field value from
+ * @len:       the length of the buffer
+ * @return 0 if OK, -ve on error
+ */
+int virtio_set_config(struct udevice *vdev, unsigned int offset,
+                     void *buf, unsigned int len);
+
+/**
+ * virtio_generation() - config generation counter
+ *
+ * @vdev:      the real virtio device
+ * @counter:   the returned config generation counter
+ * @return 0 if OK, -ve on error
+ */
+int virtio_generation(struct udevice *vdev, u32 *counter);
+
+/**
+ * virtio_get_status() - read the status byte
+ *
+ * @vdev:      the real virtio device
+ * @status:    the returned status byte
+ * @return 0 if OK, -ve on error
+ */
+int virtio_get_status(struct udevice *vdev, u8 *status);
+
+/**
+ * virtio_set_status() - write the status byte
+ *
+ * @vdev:      the real virtio device
+ * @status:    the new status byte
+ * @return 0 if OK, -ve on error
+ */
+int virtio_set_status(struct udevice *vdev, u8 status);
+
+/**
+ * virtio_reset() - reset the device
+ *
+ * @vdev:      the real virtio device
+ * @return 0 if OK, -ve on error
+ */
+int virtio_reset(struct udevice *vdev);
+
+/**
+ * virtio_get_features() - get the array of feature bits for this device
+ *
+ * @vdev:      the real virtio device
+ * @features:  the first 32 feature bits (all we currently need)
+ * @return 0 if OK, -ve on error
+ */
+int virtio_get_features(struct udevice *vdev, u64 *features);
+
+/**
+ * virtio_set_features() - confirm what device features we'll be using
+ *
+ * @vdev:      the real virtio device
+ * @return 0 if OK, -ve on error
+ */
+int virtio_set_features(struct udevice *vdev);
+
+/**
+ * virtio_find_vqs() - find virtqueues and instantiate them
+ *
+ * @vdev:      the real virtio device
+ * @nvqs:      the number of virtqueues to find
+ * @vqs:       on success, includes new virtqueues
+ * @return 0 if OK, -ve on error
+ */
+int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
+                   struct virtqueue *vqs[]);
+
+/**
+ * virtio_del_vqs() - free virtqueues found by find_vqs()
+ *
+ * @vdev:      the real virtio device
+ * @return 0 if OK, -ve on error
+ */
+int virtio_del_vqs(struct udevice *vdev);
+
+/**
+ * virtio_notify() - notify the device to process the queue
+ *
+ * @vdev:      the real virtio device
+ * @vq:                virtqueue to process
+ * @return 0 if OK, -ve on error
+ */
+int virtio_notify(struct udevice *vdev, struct virtqueue *vq);
+
+/**
+ * virtio_add_status() - helper to set a new status code to the device
+ *
+ * @vdev:      the real virtio device
+ * @status:    new status code to be added
+ */
+void virtio_add_status(struct udevice *vdev, u8 status);
+
+/**
+ * virtio_finalize_features() - helper to finalize features
+ *
+ * @vdev:      the real virtio device
+ * @return 0 if OK, -ve on error
+ */
+int virtio_finalize_features(struct udevice *vdev);
+
+/**
+ * virtio_driver_features_init() - initialize driver supported features
+ *
+ * This fills in the virtio device parent per child private data with the given
+ * information, which contains driver supported features and legacy features.
+ *
+ * This API should be called in the virtio device driver's bind method, so that
+ * later virtio transport uclass driver can utilize the driver supplied features
+ * to negotiate with the device on the final supported features.
+ *
+ * @priv:              virtio uclass per device private data
+ * @feature:           an array of feature supported by the driver
+ * @feature_size:      number of entries in the feature table array
+ * @feature_legacy:    same as feature_table but working in legacy mode
+ * @feature_legacy_size:number of entries in feature table legacy array
+ */
+void virtio_driver_features_init(struct virtio_dev_priv *priv,
+                                u32 *feature, u32 feature_size,
+                                u32 *feature_legacy, u32 feature_legacy_size);
+
+/**
+ * virtio_init() - helper to enumerate all known virtio devices
+ */
+void virtio_init(void);
+
+static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
+{
+       if (little_endian)
+               return le16_to_cpu((__force __le16)val);
+       else
+               return be16_to_cpu((__force __be16)val);
+}
+
+static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
+{
+       if (little_endian)
+               return (__force __virtio16)cpu_to_le16(val);
+       else
+               return (__force __virtio16)cpu_to_be16(val);
+}
+
+static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
+{
+       if (little_endian)
+               return le32_to_cpu((__force __le32)val);
+       else
+               return be32_to_cpu((__force __be32)val);
+}
+
+static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
+{
+       if (little_endian)
+               return (__force __virtio32)cpu_to_le32(val);
+       else
+               return (__force __virtio32)cpu_to_be32(val);
+}
+
+static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
+{
+       if (little_endian)
+               return le64_to_cpu((__force __le64)val);
+       else
+               return be64_to_cpu((__force __be64)val);
+}
+
+static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
+{
+       if (little_endian)
+               return (__force __virtio64)cpu_to_le64(val);
+       else
+               return (__force __virtio64)cpu_to_be64(val);
+}
+
+/**
+ * __virtio_test_bit - helper to test feature bits
+ *
+ * For use by transports. Devices should normally use virtio_has_feature,
+ * which includes more checks.
+ *
+ * @udev: the transport device
+ * @fbit: the feature bit
+ */
+static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit)

include/virtio.h line 423

Quoted 10 lines
include/virtio.h new file +10 -0
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
+
+       /* Did you forget to fix assumptions on max features? */
+       if (__builtin_constant_p(fbit))
+               BUILD_BUG_ON(fbit >= 64);
+       else
+               WARN_ON(fbit >= 64);
+
+       return uc_priv->features & BIT_ULL(fbit);

Why is this (and the ones below) inline? I worry this might bloat the code for no purpose?

Quoted 107 lines
include/virtio.h new file +107 -0
+}
+
+/**
+ * __virtio_set_bit - helper to set feature bits
+ *
+ * For use by transports.
+ *
+ * @udev: the transport device
+ * @fbit: the feature bit
+ */
+static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
+
+       /* Did you forget to fix assumptions on max features? */
+       if (__builtin_constant_p(fbit))
+               BUILD_BUG_ON(fbit >= 64);
+       else
+               WARN_ON(fbit >= 64);
+
+       uc_priv->features |= BIT_ULL(fbit);
+}
+
+/**
+ * __virtio_clear_bit - helper to clear feature bits
+ *
+ * For use by transports.
+ *
+ * @vdev: the transport device
+ * @fbit: the feature bit
+ */
+static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
+
+       /* Did you forget to fix assumptions on max features? */
+       if (__builtin_constant_p(fbit))
+               BUILD_BUG_ON(fbit >= 64);
+       else
+               WARN_ON(fbit >= 64);
+
+       uc_priv->features &= ~BIT_ULL(fbit);
+}
+
+/**
+ * virtio_has_feature - helper to determine if this device has this feature
+ *
+ * Note this API is only usable after the virtio device driver's bind phase,
+ * as the feature has been negotiated between the device and the driver.
+ *
+ * @vdev: the virtio device
+ * @fbit: the feature bit
+ */
+static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit)
+{
+       if (!(vdev->flags & DM_FLAG_BOUND))
+               WARN_ON(true);
+
+       return __virtio_test_bit(vdev->parent, fbit);
+}
+
+static inline bool virtio_legacy_is_little_endian(void)
+{
+#ifdef __LITTLE_ENDIAN
+       return true;
+#else
+       return false;
+#endif
+}
+
+static inline bool virtio_is_little_endian(struct udevice *vdev)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
+
+       return !uc_priv->legacy || virtio_legacy_is_little_endian();
+}
+
+/* Memory accessors */
+static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val)
+{
+       return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
+}
+
+static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val)
+{
+       return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
+}
+
+static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val)
+{
+       return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
+}
+
+static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val)
+{
+       return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
+}
+
+static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val)
+{
+       return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
+}
+
+static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val)
+{
+       return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
+}

include/virtio.h line 540

Quoted 10 lines
include/virtio.h new file +10 -0
+
+/* Read @count fields, @bytes each */
+static inline void __virtio_cread_many(struct udevice *vdev,
+                                      unsigned int offset,
+                                      void *buf, size_t count, size_t bytes)
+{
+       u32 old, gen;
+       int i;
+
+       virtio_generation(vdev, &gen);

Error check?

Should be in the uclass C file I think

Quoted 185 lines
include/virtio.h new file +154 -0
+       do {
+               old = gen;
+
+               for (i = 0; i < count; i++)
+                       virtio_get_config(vdev, offset + bytes * i,
+                                         buf + i * bytes, bytes);
+
+               virtio_generation(vdev, &gen);
+       } while (gen != old);
+}
+
+static inline void virtio_cread_bytes(struct udevice *vdev,
+                                     unsigned int offset,
+                                     void *buf, size_t len)
+{
+       __virtio_cread_many(vdev, offset, buf, len, 1);
+}
+
+static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset)
+{
+       u8 ret;
+
+       virtio_get_config(vdev, offset, &ret, sizeof(ret));
+       return ret;
+}
+
+static inline void virtio_cwrite8(struct udevice *vdev,
+                                 unsigned int offset, u8 val)
+{
+       virtio_set_config(vdev, offset, &val, sizeof(val));
+}
+
+static inline u16 virtio_cread16(struct udevice *vdev,
+                                unsigned int offset)
+{
+       u16 ret;
+
+       virtio_get_config(vdev, offset, &ret, sizeof(ret));
+       return virtio16_to_cpu(vdev, (__force __virtio16)ret);
+}
+
+static inline void virtio_cwrite16(struct udevice *vdev,
+                                  unsigned int offset, u16 val)
+{
+       val = (__force u16)cpu_to_virtio16(vdev, val);
+       virtio_set_config(vdev, offset, &val, sizeof(val));
+}
+
+static inline u32 virtio_cread32(struct udevice *vdev,
+                                unsigned int offset)
+{
+       u32 ret;
+
+       virtio_get_config(vdev, offset, &ret, sizeof(ret));
+       return virtio32_to_cpu(vdev, (__force __virtio32)ret);
+}
+
+static inline void virtio_cwrite32(struct udevice *vdev,
+                                  unsigned int offset, u32 val)
+{
+       val = (__force u32)cpu_to_virtio32(vdev, val);
+       virtio_set_config(vdev, offset, &val, sizeof(val));
+}
+
+static inline u64 virtio_cread64(struct udevice *vdev,
+                                unsigned int offset)
+{
+       u64 ret;
+
+       __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
+       return virtio64_to_cpu(vdev, (__force __virtio64)ret);
+}
+
+static inline void virtio_cwrite64(struct udevice *vdev,
+                                  unsigned int offset, u64 val)
+{
+       val = (__force u64)cpu_to_virtio64(vdev, val);
+       virtio_set_config(vdev, offset, &val, sizeof(val));
+}
+
+/* Config space read accessor */
+#define virtio_cread(vdev, structname, member, ptr)                    \
+       do {                                                            \
+               /* Must match the member's type, and be integer */      \
+               if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
+                       (*ptr) = 1;                                     \
+                                                                       \
+               switch (sizeof(*ptr)) {                                 \
+               case 1:                                                 \
+                       *(ptr) = virtio_cread8(vdev,                    \
+                                              offsetof(structname, member)); \
+                       break;                                          \
+               case 2:                                                 \
+                       *(ptr) = virtio_cread16(vdev,                   \
+                                               offsetof(structname, member)); \
+                       break;                                          \
+               case 4:                                                 \
+                       *(ptr) = virtio_cread32(vdev,                   \
+                                               offsetof(structname, member)); \
+                       break;                                          \
+               case 8:                                                 \
+                       *(ptr) = virtio_cread64(vdev,                   \
+                                               offsetof(structname, member)); \
+                       break;                                          \
+               default:                                                \
+                       WARN_ON(true);                                  \
+               }                                                       \
+       } while (0)
+
+/* Config space write accessor */
+#define virtio_cwrite(vdev, structname, member, ptr)                   \
+       do {                                                            \
+               /* Must match the member's type, and be integer */      \
+               if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
+                       WARN_ON((*ptr) == 1);                           \
+                                                                       \
+               switch (sizeof(*ptr)) {                                 \
+               case 1:                                                 \
+                       virtio_cwrite8(vdev,                            \
+                                      offsetof(structname, member),    \
+                                      *(ptr));                         \
+                       break;                                          \
+               case 2:                                                 \
+                       virtio_cwrite16(vdev,                           \
+                                       offsetof(structname, member),   \
+                                       *(ptr));                        \
+                       break;                                          \
+               case 4:                                                 \
+                       virtio_cwrite32(vdev,                           \
+                                       offsetof(structname, member),   \
+                                       *(ptr));                        \
+                       break;                                          \
+               case 8:                                                 \
+                       virtio_cwrite64(vdev,                           \
+                                       offsetof(structname, member),   \
+                                       *(ptr));                        \
+                       break;                                          \
+               default:                                                \
+                       WARN_ON(true);                                  \
+               }                                                       \
+       } while (0)
+
+/* Conditional config space accessors */
+#define virtio_cread_feature(vdev, fbit, structname, member, ptr)      \
+       ({                                                              \
+               int _r = 0;                                             \
+               if (!virtio_has_feature(vdev, fbit))                    \
+                       _r = -ENOENT;                                   \
+               else                                                    \
+                       virtio_cread(vdev, structname, member, ptr);    \
+               _r;                                                     \
+       })
+
+#endif /* __VIRTIO_H__ */
include/virtio_types.h new file +24 -1
new file mode 100644
index 0000000..d700d19
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ *
+ * From Linux kernel include/uapi/linux/virtio_types.h
+ */
+
+#ifndef _LINUX_VIRTIO_TYPES_H
+#define _LINUX_VIRTIO_TYPES_H
+
+#include <linux/types.h>
+
+/*
+ * __virtio{16,32,64} have the following meaning:
+ * - __u{16,32,64} for virtio devices in legacy mode, accessed in native endian
+ * - __le{16,32,64} for standard-compliant virtio devices
+ */
+
+typedef __u16 __bitwise __virtio16;
+typedef __u32 __bitwise __virtio32;
+typedef __u64 __bitwise __virtio64;
+
+#endif /* _LINUX_VIRTIO_TYPES_H */
--
Quoted 2 lines

2.7.4

Regards,
Simon

author
#6 lore raw

PATCH 2/27 dm: Add a new uclass driver for VirtIO transport devices

Hi Simon,

Quoted 12 lines · Simon Glass

Hi Bin,

Quoted 8 lines · Bin Meng

This adds a new virtio uclass driver for “virtio” [1] family of devices that are are found in virtual environments like QEMU, yet by design they look like physical devices to the guest.

The uclass driver provides child_pre_probe() and child_post_probe() methods to do some common operations for virtio device drivers like device and driver supported feature negotiation, etc.

Quoted 6 lines
Quoted 1 line

[1] http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf

Can you add this link to the header file too? This seems to be lacking docs.in the source code. Also in a header file, a short statement of what virtio is for would be good.

Will add the link and a short statement for VirtIO in virtio.h, virtio-uclass.c and driver/virtio/Kconfig in v2.

Quoted 57 lines
Quoted 57 lines

---

 drivers/Kconfig                |   2 +
 drivers/Makefile               |   1 +
 drivers/virtio/Kconfig         |  14 +
 drivers/virtio/Makefile        |   6 +
 drivers/virtio/virtio-uclass.c | 333 ++++++++++++++++++++
 include/dm/uclass-id.h         |   1 +
 include/virtio.h               | 694 +++++++++++++++++++++++++++++++++++++++++
 include/virtio_types.h         |  24 ++
 8 files changed, 1075 insertions(+)
 create mode 100644 drivers/virtio/Kconfig
 create mode 100644 drivers/virtio/Makefile
 create mode 100644 drivers/virtio/virtio-uclass.c
 create mode 100644 include/virtio.h
 create mode 100644 include/virtio_types.h
drivers/Kconfig +2 -0
index 56536c4..d40db0d 100644
@@ -106,6 +106,8 @@ source "drivers/usb/Kconfig"
 
 source "drivers/video/Kconfig"
 
+source "drivers/virtio/Kconfig"
+
 source "drivers/watchdog/Kconfig"
 
 config PHYS_TO_BUS
drivers/Makefile +1 -0
index 23ea609..f09daae 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_$(SPL_TPL_)SERIAL_SUPPORT) += serial/
 obj-$(CONFIG_$(SPL_TPL_)SPI_FLASH_SUPPORT) += mtd/spi/
 obj-$(CONFIG_$(SPL_TPL_)SPI_SUPPORT) += spi/
 obj-$(CONFIG_$(SPL_TPL_)TIMER) += timer/
+obj-$(CONFIG_$(SPL_TPL_)VIRTIO) += virtio/
 obj-$(CONFIG_$(SPL_)DM_MAILBOX) += mailbox/
 obj-$(CONFIG_$(SPL_)REMOTEPROC) += remoteproc/
 
drivers/virtio/Kconfig new file +6 -0
new file mode 100644
index 0000000..bdfa96a
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+# Copyright (C) 2018, Bin Meng <[email protected]>
+
+menu "VirtIO Drivers"
Quoted 9 lines

drivers/virtio/Kconfig line 12

Quoted 6 lines
drivers/virtio/Kconfig new file +6 -0
+
+config VIRTIO
+       bool
+       help
+         This option is selected by any driver which implements the virtio
+         bus, such as CONFIG_VIRTIO_MMIO or CONFIG_VIRTIO_PCI.

What is a virtio bus?

Change to transport in v2, as suggested by Tuomas.

Quoted 39 lines
Quoted 39 lines
drivers/virtio/Kconfig new file +2 -0
+
+endmenu
drivers/virtio/Makefile new file +6 -0
new file mode 100644
index 0000000..23e7be7
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+# Copyright (C) 2018, Bin Meng <[email protected]>
+
+obj-y += virtio-uclass.o
drivers/virtio/virtio-uclass.c new file +19 -0
new file mode 100644
index 0000000..1c85856
@@ -0,0 +1,333 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <virtio.h>
+#include <dm/device.h>
+#include <dm/lists.h>
+
+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,
+};
+
+int virtio_get_config(struct udevice *vdev, unsigned int offset,
+                     void *buf, unsigned int len)
Quoted 9 lines

drivers/virtio/virtio-uclass.c line 25

Quoted 6 lines
drivers/virtio/virtio-uclass.c new file +6 -0
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->get_config)
+               return -ENOSYS;

blank line before return (fix globally)

Fixed this in v2. However as Tuomas suggested, I added another check in the uclass' pre_probe method to check these ops once and for all so most of these checks here are unnecessary in v2.

Quoted 143 lines
Quoted 143 lines
drivers/virtio/virtio-uclass.c new file +143 -0
+       return ops->get_config(vdev->parent, offset, buf, len);
+}
+
+int virtio_set_config(struct udevice *vdev, unsigned int offset,
+                     void *buf, unsigned int len)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->set_config)
+               return -ENOSYS;
+       return ops->set_config(vdev->parent, offset, buf, len);
+}
+
+int virtio_generation(struct udevice *vdev, u32 *counter)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->generation)
+               return -ENOSYS;
+       return ops->generation(vdev->parent, counter);
+}
+
+int virtio_get_status(struct udevice *vdev, u8 *status)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->get_status)
+               return -ENOSYS;
+       return ops->get_status(vdev->parent, status);
+}
+
+int virtio_set_status(struct udevice *vdev, u8 status)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->set_status)
+               return -ENOSYS;
+       return ops->set_status(vdev->parent, status);
+}
+
+int virtio_reset(struct udevice *vdev)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->reset)
+               return -ENOSYS;
+       return ops->reset(vdev->parent);
+}
+
+int virtio_get_features(struct udevice *vdev, u64 *features)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->get_features)
+               return -ENOSYS;
+       return ops->get_features(vdev->parent, features);
+}
+
+int virtio_set_features(struct udevice *vdev)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->set_features)
+               return -ENOSYS;
+       return ops->set_features(vdev->parent);
+}
+
+int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
+                   struct virtqueue *vqs[])
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->find_vqs)
+               return -ENOSYS;
+       return ops->find_vqs(vdev->parent, nvqs, vqs);
+}
+
+int virtio_del_vqs(struct udevice *vdev)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->del_vqs)
+               return -ENOSYS;
+       return ops->del_vqs(vdev->parent);
+}
+
+int virtio_notify(struct udevice *vdev, struct virtqueue *vq)
+{
+       struct dm_virtio_ops *ops;
+
+       ops = virtio_get_ops(vdev->parent);
+       if (!ops->notify)
+               return -ENOSYS;
+       return ops->notify(vdev->parent, vq);
+}
+
+void virtio_add_status(struct udevice *vdev, u8 status)
+{
+       u8 old;
+
+       if (!virtio_get_status(vdev, &old))
+               virtio_set_status(vdev, old | status);
+}
+
+int virtio_finalize_features(struct udevice *vdev)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
+       u8 status;
+       int ret;
+
+       ret = virtio_set_features(vdev);
+       if (ret)
+               return ret;
+
+       if (uc_priv->legacy)
+               return 0;
+
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK);
+       ret = virtio_get_status(vdev, &status);
+       if (ret)
+               return ret;
+       if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
+               debug("(%s): device refuses features %x\n", vdev->name, status);
+               return -ENODEV;
+       }
+
+       return 0;
+}
+
+void virtio_driver_features_init(struct virtio_dev_priv *priv,
+                                u32 *feature, u32 feature_size,
+                                u32 *feature_legacy, u32 feature_legacy_size)
+{
+       priv->feature_table = feature;
Quoted 9 lines

drivers/virtio/virtio-uclass.c line 174

Quoted 6 lines
drivers/virtio/virtio-uclass.c new file +6 -0
+       priv->feature_table_size = feature_size;
+       priv->feature_table_legacy = feature_legacy;
+       priv->feature_table_size_legacy = feature_legacy_size;
+}
+
+void virtio_init(void)

Shouldn't this return int if there is an error?

Changed in v2.

Quoted 29 lines
Quoted 29 lines
drivers/virtio/virtio-uclass.c new file +29 -0
+{
+       struct udevice *bus;
+
+       /* Enumerate all known virtio devices */
+       for (uclass_first_device(UCLASS_VIRTIO, &bus);
+            bus;
+            uclass_next_device(&bus)) {
+               ;
+       }
+}
+
+static int virtio_uclass_post_probe(struct udevice *udev)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
+       char dev_name[30], *str;
+       struct udevice *vdev;
+       int ret;
+
+       if (uc_priv->device > VIRTIO_ID_MAX_NUM) {
+               debug("(%s): virtio device ID %d exceeds maximum num\n",
+                     udev->name, uc_priv->device);
+               return 0;
+       }
+
+       if (!virtio_drv_name[uc_priv->device]) {
+               debug("(%s): underlying virtio device driver unavailable\n",
+                     udev->name);
+               return 0;
+       }
Quoted 9 lines

drivers/virtio/virtio-uclass.c line 209

Quoted 6 lines
drivers/virtio/virtio-uclass.c new file +6 -0
+
+       snprintf(dev_name, sizeof(dev_name), "%s#%d",
+                virtio_drv_name[uc_priv->device], udev->seq);
+       str = strdup(dev_name);
+       if (!str)
+               return -ENOMEM;

You might consider using log_ret() or log_msg_ret() for your error returns

Keep this for now as currently log_msg_ret() does not build.

Quoted 130 lines
Quoted 10 lines
drivers/virtio/virtio-uclass.c new file +10 -0
+
+       ret = device_bind_driver(udev, virtio_drv_name[uc_priv->device],
+                                str, &vdev);
+       if (ret == -ENOENT) {
+               debug("(%s): no driver configured\n", udev->name);
+               return 0;
+       }
+       if (ret) {
+               free(str);
+               return ret;

drivers/virtio/virtio-uclass.c line 229

Quoted 10 lines
drivers/virtio/virtio-uclass.c new file +10 -0
+       }
+       device_set_name_alloced(vdev);
+
+       return 0;
+}
+
+static int virtio_uclass_child_post_bind(struct udevice *vdev)
+{
+       /* Acknowledge that we've seen the device */
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE);

Not sure this is worth having a new hook?

Quoted 107 lines
drivers/virtio/virtio-uclass.c new file +104 -0
+
+       return 0;
+}
+
+static int virtio_uclass_child_pre_probe(struct udevice *vdev)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
+       u64 device_features;
+       u64 driver_features;
+       u64 driver_features_legacy;
+       int i;
+       int ret;
+
+       /*
+        * Save the real virtio device (eg: virtio-net, virtio-blk) to
+        * the transport (parent) device's uclass priv for future use.
+        */
+       uc_priv->vdev = vdev;
+
+       /*
+        * We always start by resetting the device, in case a previous driver
+        * messed it up. This also tests that code path a little.
+        */
+       ret = virtio_reset(vdev);
+       if (ret)
+               goto err;
+
+       /* We have a driver! */
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER);
+
+       /* Figure out what features the device supports */
+       virtio_get_features(vdev, &device_features);
+       debug("(%s) plain device features supported %016llx\n",
+             vdev->name, device_features);
+       if (!(device_features & (1ULL << VIRTIO_F_VERSION_1)))
+               uc_priv->legacy = true;
+
+       /* Figure out what features the driver supports */
+       driver_features = 0;
+       for (i = 0; i < uc_priv->feature_table_size; i++) {
+               unsigned int f = uc_priv->feature_table[i];
+
+               WARN_ON(f >= 64);
+               driver_features |= (1ULL << f);
+       }
+
+       /* Some drivers have a separate feature table for virtio v1.0 */
+       if (uc_priv->feature_table_legacy) {
+               driver_features_legacy = 0;
+               for (i = 0; i < uc_priv->feature_table_size_legacy; i++) {
+                       unsigned int f = uc_priv->feature_table_legacy[i];
+
+                       WARN_ON(f >= 64);
+                       driver_features_legacy |= (1ULL << f);
+               }
+       } else {
+               driver_features_legacy = driver_features;
+       }
+
+       if (uc_priv->legacy) {
+               debug("(%s): legacy virtio device\n", vdev->name);
+               uc_priv->features = driver_features_legacy & device_features;
+       } else {
+               debug("(%s): v1.0 complaint virtio device\n", vdev->name);
+               uc_priv->features = driver_features & device_features;
+       }
+
+       /* Transport features always preserved to pass to finalize_features */
+       for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
+               if ((device_features & (1ULL << i)) &&
+                   (i == VIRTIO_F_VERSION_1))
+                       __virtio_set_bit(vdev->parent, i);
+
+       debug("(%s) final negotiated features supported %016llx\n",
+             vdev->name, uc_priv->features);
+       ret = virtio_finalize_features(vdev);
+       if (ret)
+               goto err;
+
+       return 0;
+
+err:
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED);
+       return ret;
+}
+
+static int virtio_uclass_child_post_probe(struct udevice *vdev)
+{
+       /* Indicates that the driver is set up and ready to drive the device */
+       virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK);
+
+       return 0;
+}
+
+UCLASS_DRIVER(virtio) = {
+       .name   = "virtio",
+       .id     = UCLASS_VIRTIO,
+       .flags  = DM_UC_FLAG_SEQ_ALIAS,
+       .post_probe = virtio_uclass_post_probe,
+       .child_post_bind = virtio_uclass_child_post_bind,
+       .child_pre_probe = virtio_uclass_child_pre_probe,
+       .child_post_probe = virtio_uclass_child_post_probe,
+       .per_device_auto_alloc_size = sizeof(struct virtio_dev_priv),
+};
include/dm/uclass-id.h +0 -1
index 7027ea0..08b2a8f 100644
--- a/include/dm/uclass-id.h
Quoted 9 lines

include/dm/uclass-id.h enum uclass_id { · line 96

Quoted 6 lines

+++ b/include/dm/uclass-id.h

include/dm/uclass-id.h +1 -0
@@ -93,6 +93,7 @@ enum uclass_id {
        UCLASS_VIDEO_BRIDGE,    /* Video bridge, e.g. DisplayPort to LVDS */
        UCLASS_VIDEO_CONSOLE,   /* Text console driver for video device */
        UCLASS_WDT,             /* Watchdot Timer driver */
+       UCLASS_VIRTIO,          /* VirtIO transport device */

V before W :-)

Fixed in v2.

Quoted 16 lines
Quoted 16 lines
include/dm/uclass-id.h +0 -0
        UCLASS_COUNT,
        UCLASS_INVALID = -1,
include/virtio.h new file +7 -0
new file mode 100644
index 0000000..efbedb2
@@ -0,0 +1,694 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ *
+ * This file is largely based on Linux kernel virtio_*.h files
+ */
Quoted 9 lines

include/virtio.h line 13

Quoted 6 lines
include/virtio.h new file +6 -0
+
+#ifndef __VIRTIO_H__
+#define __VIRTIO_H__
+
+#include <virtio_types.h>
+#include <dm/device.h>

Should be able to avoid this, just put in C file

Fixed in v2.

Quoted 405 lines
Quoted 405 lines
include/virtio.h new file +405 -0
+
+#define VIRTIO_ID_NET          1 /* virtio net */
+#define VIRTIO_ID_BLOCK                2 /* virtio block */
+#define VIRTIO_ID_MAX_NUM      3
+
+#define VIRTIO_NET_DRV_NAME    "virtio-net"
+#define VIRTIO_BLK_DRV_NAME    "virtio-blk"
+
+/* Status byte for guest to report progress, and synchronize features */
+
+/* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
+#define VIRTIO_CONFIG_S_ACKNOWLEDGE    1
+/* We have found a driver for the device */
+#define VIRTIO_CONFIG_S_DRIVER         2
+/* Driver has used its parts of the config, and is happy */
+#define VIRTIO_CONFIG_S_DRIVER_OK      4
+/* Driver has finished configuring features */
+#define VIRTIO_CONFIG_S_FEATURES_OK    8
+/* Device entered invalid state, driver must reset it */
+#define VIRTIO_CONFIG_S_NEEDS_RESET    0x40
+/* We've given up on this device */
+#define VIRTIO_CONFIG_S_FAILED         0x80
+
+/*
+ * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END
+ * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.),
+ * the rest are per-device feature bits.
+ */
+#define VIRTIO_TRANSPORT_F_START       28
+#define VIRTIO_TRANSPORT_F_END         38
+
+#ifndef VIRTIO_CONFIG_NO_LEGACY
+/*
+ * Do we get callbacks when the ring is completely used,
+ * even if we've suppressed them?
+ */
+#define VIRTIO_F_NOTIFY_ON_EMPTY       24
+
+/* Can the device handle any descriptor layout? */
+#define VIRTIO_F_ANY_LAYOUT            27
+#endif /* VIRTIO_CONFIG_NO_LEGACY */
+
+/* v1.0 compliant */
+#define VIRTIO_F_VERSION_1             32
+
+/*
+ * If clear - device has the IOMMU bypass quirk feature.
+ * If set - use platform tools to detect the IOMMU.
+ *
+ * Note the reverse polarity (compared to most other features),
+ * this is for compatibility with legacy systems.
+ */
+#define VIRTIO_F_IOMMU_PLATFORM                33
+
+/* Does the device support Single Root I/O Virtualization? */
+#define VIRTIO_F_SR_IOV                        37
+
+/**
+ * virtio scatter-gather struct
+ *
+ * @addr:              sg buffer address
+ * @lengh:             sg buffer length
+ */
+struct virtio_sg {
+       void *addr;
+       size_t length;
+};
+
+struct virtqueue;
+
+/* virtio bus operations */
+struct dm_virtio_ops {
+       /**
+        * get_config() - read the value of a configuration field
+        *
+        * @vdev:       the real virtio device
+        * @offset:     the offset of the configuration field
+        * @buf:        the buffer to write the field value into
+        * @len:        the length of the buffer
+        * @return 0 if OK, -ve on error
+        */
+       int (*get_config)(struct udevice *vdev, unsigned int offset,
+                         void *buf, unsigned int len);
+       /**
+        * set_config() - write the value of a configuration field
+        *
+        * @vdev:       the real virtio device
+        * @offset:     the offset of the configuration field
+        * @buf:        the buffer to read the field value from
+        * @len:        the length of the buffer
+        * @return 0 if OK, -ve on error
+        */
+       int (*set_config)(struct udevice *vdev, unsigned int offset,
+                         const void *buf, unsigned int len);
+       /**
+        * generation() - config generation counter
+        *
+        * @vdev:       the real virtio device
+        * @counter:    the returned config generation counter
+        * @return 0 if OK, -ve on error
+        */
+       int (*generation)(struct udevice *vdev, u32 *counter);
+       /**
+        * get_status() - read the status byte
+        *
+        * @vdev:       the real virtio device
+        * @status:     the returned status byte
+        * @return 0 if OK, -ve on error
+        */
+       int (*get_status)(struct udevice *vdev, u8 *status);
+       /**
+        * set_status() - write the status byte
+        *
+        * @vdev:       the real virtio device
+        * @status:     the new status byte
+        * @return 0 if OK, -ve on error
+        */
+       int (*set_status)(struct udevice *vdev, u8 status);
+       /**
+        * reset() - reset the device
+        *
+        * @vdev:       the real virtio device
+        * @return 0 if OK, -ve on error
+        */
+       int (*reset)(struct udevice *vdev);
+       /**
+        * get_features() - get the array of feature bits for this device
+        *
+        * @vdev:       the real virtio device
+        * @features:   the first 32 feature bits (all we currently need)
+        * @return 0 if OK, -ve on error
+        */
+       int (*get_features)(struct udevice *vdev, u64 *features);
+       /**
+        * set_features() - confirm what device features we'll be using
+        *
+        * @vdev:       the real virtio device
+        * @return 0 if OK, -ve on error
+        */
+       int (*set_features)(struct udevice *vdev);
+       /**
+        * find_vqs() - find virtqueues and instantiate them
+        *
+        * @vdev:       the real virtio device
+        * @nvqs:       the number of virtqueues to find
+        * @vqs:        on success, includes new virtqueues
+        * @return 0 if OK, -ve on error
+        */
+       int (*find_vqs)(struct udevice *vdev, unsigned int nvqs,
+                       struct virtqueue *vqs[]);
+       /**
+        * del_vqs() - free virtqueues found by find_vqs()
+        *
+        * @vdev:       the real virtio device
+        * @return 0 if OK, -ve on error
+        */
+       int (*del_vqs)(struct udevice *vdev);
+       /**
+        * notify() - notify the device to process the queue
+        *
+        * @vdev:       the real virtio device
+        * @vq:         virtqueue to process
+        * @return 0 if OK, -ve on error
+        */
+       int (*notify)(struct udevice *vdev, struct virtqueue *vq);
+};
+
+/* Get access to a virtio bus' operations */
+#define virtio_get_ops(dev)    ((struct dm_virtio_ops *)(dev)->driver->ops)
+
+/**
+ * virtio uclass per device private data
+ *
+ * @vqs:                       virtualqueue for the virtio device
+ * @vdev:                      the real virtio device underneath
+ * @legacy:                    is it a legacy device?
+ * @device:                    virtio device ID
+ * @vendor:                    virtio vendor ID
+ * @features:                  negotiated supported features
+ * @feature_table:             an array of feature supported by the driver
+ * @feature_table_size:                number of entries in the feature table array
+ * @feature_table_legacy:      same as feature_table but working in legacy mode
+ * @feature_table_size_legacy: number of entries in feature table legacy array
+ */
+struct virtio_dev_priv {
+       struct list_head vqs;
+       struct udevice *vdev;
+       bool legacy;
+       u32 device;
+       u32 vendor;
+       u64 features;
+       u32 *feature_table;
+       u32 feature_table_size;
+       u32 *feature_table_legacy;
+       u32 feature_table_size_legacy;
+};
+
+/**
+ * virtio_get_config() - read the value of a configuration field
+ *
+ * @vdev:      the real virtio device
+ * @offset:    the offset of the configuration field
+ * @buf:       the buffer to write the field value into
+ * @len:       the length of the buffer
+ * @return 0 if OK, -ve on error
+ */
+int virtio_get_config(struct udevice *vdev, unsigned int offset,
+                     void *buf, unsigned int len);
+
+/**
+ * virtio_set_config() - write the value of a configuration field
+ *
+ * @vdev:      the real virtio device
+ * @offset:    the offset of the configuration field
+ * @buf:       the buffer to read the field value from
+ * @len:       the length of the buffer
+ * @return 0 if OK, -ve on error
+ */
+int virtio_set_config(struct udevice *vdev, unsigned int offset,
+                     void *buf, unsigned int len);
+
+/**
+ * virtio_generation() - config generation counter
+ *
+ * @vdev:      the real virtio device
+ * @counter:   the returned config generation counter
+ * @return 0 if OK, -ve on error
+ */
+int virtio_generation(struct udevice *vdev, u32 *counter);
+
+/**
+ * virtio_get_status() - read the status byte
+ *
+ * @vdev:      the real virtio device
+ * @status:    the returned status byte
+ * @return 0 if OK, -ve on error
+ */
+int virtio_get_status(struct udevice *vdev, u8 *status);
+
+/**
+ * virtio_set_status() - write the status byte
+ *
+ * @vdev:      the real virtio device
+ * @status:    the new status byte
+ * @return 0 if OK, -ve on error
+ */
+int virtio_set_status(struct udevice *vdev, u8 status);
+
+/**
+ * virtio_reset() - reset the device
+ *
+ * @vdev:      the real virtio device
+ * @return 0 if OK, -ve on error
+ */
+int virtio_reset(struct udevice *vdev);
+
+/**
+ * virtio_get_features() - get the array of feature bits for this device
+ *
+ * @vdev:      the real virtio device
+ * @features:  the first 32 feature bits (all we currently need)
+ * @return 0 if OK, -ve on error
+ */
+int virtio_get_features(struct udevice *vdev, u64 *features);
+
+/**
+ * virtio_set_features() - confirm what device features we'll be using
+ *
+ * @vdev:      the real virtio device
+ * @return 0 if OK, -ve on error
+ */
+int virtio_set_features(struct udevice *vdev);
+
+/**
+ * virtio_find_vqs() - find virtqueues and instantiate them
+ *
+ * @vdev:      the real virtio device
+ * @nvqs:      the number of virtqueues to find
+ * @vqs:       on success, includes new virtqueues
+ * @return 0 if OK, -ve on error
+ */
+int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
+                   struct virtqueue *vqs[]);
+
+/**
+ * virtio_del_vqs() - free virtqueues found by find_vqs()
+ *
+ * @vdev:      the real virtio device
+ * @return 0 if OK, -ve on error
+ */
+int virtio_del_vqs(struct udevice *vdev);
+
+/**
+ * virtio_notify() - notify the device to process the queue
+ *
+ * @vdev:      the real virtio device
+ * @vq:                virtqueue to process
+ * @return 0 if OK, -ve on error
+ */
+int virtio_notify(struct udevice *vdev, struct virtqueue *vq);
+
+/**
+ * virtio_add_status() - helper to set a new status code to the device
+ *
+ * @vdev:      the real virtio device
+ * @status:    new status code to be added
+ */
+void virtio_add_status(struct udevice *vdev, u8 status);
+
+/**
+ * virtio_finalize_features() - helper to finalize features
+ *
+ * @vdev:      the real virtio device
+ * @return 0 if OK, -ve on error
+ */
+int virtio_finalize_features(struct udevice *vdev);
+
+/**
+ * virtio_driver_features_init() - initialize driver supported features
+ *
+ * This fills in the virtio device parent per child private data with the given
+ * information, which contains driver supported features and legacy features.
+ *
+ * This API should be called in the virtio device driver's bind method, so that
+ * later virtio transport uclass driver can utilize the driver supplied features
+ * to negotiate with the device on the final supported features.
+ *
+ * @priv:              virtio uclass per device private data
+ * @feature:           an array of feature supported by the driver
+ * @feature_size:      number of entries in the feature table array
+ * @feature_legacy:    same as feature_table but working in legacy mode
+ * @feature_legacy_size:number of entries in feature table legacy array
+ */
+void virtio_driver_features_init(struct virtio_dev_priv *priv,
+                                u32 *feature, u32 feature_size,
+                                u32 *feature_legacy, u32 feature_legacy_size);
+
+/**
+ * virtio_init() - helper to enumerate all known virtio devices
+ */
+void virtio_init(void);
+
+static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
+{
+       if (little_endian)
+               return le16_to_cpu((__force __le16)val);
+       else
+               return be16_to_cpu((__force __be16)val);
+}
+
+static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
+{
+       if (little_endian)
+               return (__force __virtio16)cpu_to_le16(val);
+       else
+               return (__force __virtio16)cpu_to_be16(val);
+}
+
+static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
+{
+       if (little_endian)
+               return le32_to_cpu((__force __le32)val);
+       else
+               return be32_to_cpu((__force __be32)val);
+}
+
+static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
+{
+       if (little_endian)
+               return (__force __virtio32)cpu_to_le32(val);
+       else
+               return (__force __virtio32)cpu_to_be32(val);
+}
+
+static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
+{
+       if (little_endian)
+               return le64_to_cpu((__force __le64)val);
+       else
+               return be64_to_cpu((__force __be64)val);
+}
+
+static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
+{
+       if (little_endian)
+               return (__force __virtio64)cpu_to_le64(val);
+       else
+               return (__force __virtio64)cpu_to_be64(val);
+}
+
+/**
+ * __virtio_test_bit - helper to test feature bits
+ *
+ * For use by transports. Devices should normally use virtio_has_feature,
+ * which includes more checks.
+ *
+ * @udev: the transport device
+ * @fbit: the feature bit
+ */
+static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
+
+       /* Did you forget to fix assumptions on max features? */
+       if (__builtin_constant_p(fbit))
Quoted 9 lines

include/virtio.h line 423

Quoted 5 lines
include/virtio.h new file +5 -0
+               BUILD_BUG_ON(fbit >= 64);
+       else
+               WARN_ON(fbit >= 64);
+
+       return uc_priv->features & BIT_ULL(fbit);

Why is this (and the ones below) inline? I worry this might bloat the code for no purpose?

These are from Linux header files and I plan to leave them as it is for now, to keep sync with Linux version.

Quoted 111 lines
Quoted 111 lines
include/virtio.h new file +111 -0
+}
+
+/**
+ * __virtio_set_bit - helper to set feature bits
+ *
+ * For use by transports.
+ *
+ * @udev: the transport device
+ * @fbit: the feature bit
+ */
+static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
+
+       /* Did you forget to fix assumptions on max features? */
+       if (__builtin_constant_p(fbit))
+               BUILD_BUG_ON(fbit >= 64);
+       else
+               WARN_ON(fbit >= 64);
+
+       uc_priv->features |= BIT_ULL(fbit);
+}
+
+/**
+ * __virtio_clear_bit - helper to clear feature bits
+ *
+ * For use by transports.
+ *
+ * @vdev: the transport device
+ * @fbit: the feature bit
+ */
+static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
+
+       /* Did you forget to fix assumptions on max features? */
+       if (__builtin_constant_p(fbit))
+               BUILD_BUG_ON(fbit >= 64);
+       else
+               WARN_ON(fbit >= 64);
+
+       uc_priv->features &= ~BIT_ULL(fbit);
+}
+
+/**
+ * virtio_has_feature - helper to determine if this device has this feature
+ *
+ * Note this API is only usable after the virtio device driver's bind phase,
+ * as the feature has been negotiated between the device and the driver.
+ *
+ * @vdev: the virtio device
+ * @fbit: the feature bit
+ */
+static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit)
+{
+       if (!(vdev->flags & DM_FLAG_BOUND))
+               WARN_ON(true);
+
+       return __virtio_test_bit(vdev->parent, fbit);
+}
+
+static inline bool virtio_legacy_is_little_endian(void)
+{
+#ifdef __LITTLE_ENDIAN
+       return true;
+#else
+       return false;
+#endif
+}
+
+static inline bool virtio_is_little_endian(struct udevice *vdev)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
+
+       return !uc_priv->legacy || virtio_legacy_is_little_endian();
+}
+
+/* Memory accessors */
+static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val)
+{
+       return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
+}
+
+static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val)
+{
+       return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
+}
+
+static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val)
+{
+       return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
+}
+
+static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val)
+{
+       return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
+}
+
+static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val)
+{
+       return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
+}
+
+static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val)
+{
+       return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
+}
+
+/* Read @count fields, @bytes each */
+static inline void __virtio_cread_many(struct udevice *vdev,
+                                      unsigned int offset,
Quoted 9 lines

include/virtio.h line 540

Quoted 6 lines
include/virtio.h new file +6 -0
+                                      void *buf, size_t count, size_t bytes)
+{
+       u32 old, gen;
+       int i;
+
+       virtio_generation(vdev, &gen);

Error check?

In fact this ops can be optional. Will add a comment in v2.

Quoted 179 lines

Should be in the uclass C file I think

Quoted 177 lines
include/virtio.h new file +154 -0
+       do {
+               old = gen;
+
+               for (i = 0; i < count; i++)
+                       virtio_get_config(vdev, offset + bytes * i,
+                                         buf + i * bytes, bytes);
+
+               virtio_generation(vdev, &gen);
+       } while (gen != old);
+}
+
+static inline void virtio_cread_bytes(struct udevice *vdev,
+                                     unsigned int offset,
+                                     void *buf, size_t len)
+{
+       __virtio_cread_many(vdev, offset, buf, len, 1);
+}
+
+static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset)
+{
+       u8 ret;
+
+       virtio_get_config(vdev, offset, &ret, sizeof(ret));
+       return ret;
+}
+
+static inline void virtio_cwrite8(struct udevice *vdev,
+                                 unsigned int offset, u8 val)
+{
+       virtio_set_config(vdev, offset, &val, sizeof(val));
+}
+
+static inline u16 virtio_cread16(struct udevice *vdev,
+                                unsigned int offset)
+{
+       u16 ret;
+
+       virtio_get_config(vdev, offset, &ret, sizeof(ret));
+       return virtio16_to_cpu(vdev, (__force __virtio16)ret);
+}
+
+static inline void virtio_cwrite16(struct udevice *vdev,
+                                  unsigned int offset, u16 val)
+{
+       val = (__force u16)cpu_to_virtio16(vdev, val);
+       virtio_set_config(vdev, offset, &val, sizeof(val));
+}
+
+static inline u32 virtio_cread32(struct udevice *vdev,
+                                unsigned int offset)
+{
+       u32 ret;
+
+       virtio_get_config(vdev, offset, &ret, sizeof(ret));
+       return virtio32_to_cpu(vdev, (__force __virtio32)ret);
+}
+
+static inline void virtio_cwrite32(struct udevice *vdev,
+                                  unsigned int offset, u32 val)
+{
+       val = (__force u32)cpu_to_virtio32(vdev, val);
+       virtio_set_config(vdev, offset, &val, sizeof(val));
+}
+
+static inline u64 virtio_cread64(struct udevice *vdev,
+                                unsigned int offset)
+{
+       u64 ret;
+
+       __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
+       return virtio64_to_cpu(vdev, (__force __virtio64)ret);
+}
+
+static inline void virtio_cwrite64(struct udevice *vdev,
+                                  unsigned int offset, u64 val)
+{
+       val = (__force u64)cpu_to_virtio64(vdev, val);
+       virtio_set_config(vdev, offset, &val, sizeof(val));
+}
+
+/* Config space read accessor */
+#define virtio_cread(vdev, structname, member, ptr)                    \
+       do {                                                            \
+               /* Must match the member's type, and be integer */      \
+               if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
+                       (*ptr) = 1;                                     \
+                                                                       \
+               switch (sizeof(*ptr)) {                                 \
+               case 1:                                                 \
+                       *(ptr) = virtio_cread8(vdev,                    \
+                                              offsetof(structname, member)); \
+                       break;                                          \
+               case 2:                                                 \
+                       *(ptr) = virtio_cread16(vdev,                   \
+                                               offsetof(structname, member)); \
+                       break;                                          \
+               case 4:                                                 \
+                       *(ptr) = virtio_cread32(vdev,                   \
+                                               offsetof(structname, member)); \
+                       break;                                          \
+               case 8:                                                 \
+                       *(ptr) = virtio_cread64(vdev,                   \
+                                               offsetof(structname, member)); \
+                       break;                                          \
+               default:                                                \
+                       WARN_ON(true);                                  \
+               }                                                       \
+       } while (0)
+
+/* Config space write accessor */
+#define virtio_cwrite(vdev, structname, member, ptr)                   \
+       do {                                                            \
+               /* Must match the member's type, and be integer */      \
+               if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
+                       WARN_ON((*ptr) == 1);                           \
+                                                                       \
+               switch (sizeof(*ptr)) {                                 \
+               case 1:                                                 \
+                       virtio_cwrite8(vdev,                            \
+                                      offsetof(structname, member),    \
+                                      *(ptr));                         \
+                       break;                                          \
+               case 2:                                                 \
+                       virtio_cwrite16(vdev,                           \
+                                       offsetof(structname, member),   \
+                                       *(ptr));                        \
+                       break;                                          \
+               case 4:                                                 \
+                       virtio_cwrite32(vdev,                           \
+                                       offsetof(structname, member),   \
+                                       *(ptr));                        \
+                       break;                                          \
+               case 8:                                                 \
+                       virtio_cwrite64(vdev,                           \
+                                       offsetof(structname, member),   \
+                                       *(ptr));                        \
+                       break;                                          \
+               default:                                                \
+                       WARN_ON(true);                                  \
+               }                                                       \
+       } while (0)
+
+/* Conditional config space accessors */
+#define virtio_cread_feature(vdev, fbit, structname, member, ptr)      \
+       ({                                                              \
+               int _r = 0;                                             \
+               if (!virtio_has_feature(vdev, fbit))                    \
+                       _r = -ENOENT;                                   \
+               else                                                    \
+                       virtio_cread(vdev, structname, member, ptr);    \
+               _r;                                                     \
+       })
+
+#endif /* __VIRTIO_H__ */
include/virtio_types.h new file +17 -0
new file mode 100644
index 0000000..d700d19
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ *
+ * From Linux kernel include/uapi/linux/virtio_types.h
+ */
+
+#ifndef _LINUX_VIRTIO_TYPES_H
+#define _LINUX_VIRTIO_TYPES_H
+
+#include <linux/types.h>
+
+/*
+ * __virtio{16,32,64} have the following meaning:
+ * - __u{16,32,64} for virtio devices in legacy mode, accessed in native endian
+ * - __le{16,32,64} for standard-compliant virtio devices
Quoted 8 lines

include/virtio_types.h

Quoted 8 lines
include/virtio_types.h new file +7 -1
+ */
+
+typedef __u16 __bitwise __virtio16;
+typedef __u32 __bitwise __virtio32;
+typedef __u64 __bitwise __virtio64;
+
+#endif /* _LINUX_VIRTIO_TYPES_H */
--

Regards,
Bin

#7 lore raw

PATCH 2/27 dm: Add a new uclass driver for VirtIO transport devices

In reply to Bin Meng

Hi Bin,

Thanks for the patches, they look great. Some minor comments:

Quoted 13 lines · Bin Meng

This adds a new virtio uclass driver for “virtio” [1] family of devices that are are found in virtual environments like QEMU, yet by design they look like physical devices to the guest.

The uclass driver provides child_pre_probe() and child_post_probe() methods to do some common operations for virtio device drivers like device and driver supported feature negotiation, etc.

[1] http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf

---...

drivers/virtio/Kconfig line 12

Quoted 6 lines
drivers/virtio/Kconfig new file +6 -0
+
+config VIRTIO
+	bool
+	help
+	  This option is selected by any driver which implements the virtio
+	  bus, such as CONFIG_VIRTIO_MMIO or CONFIG_VIRTIO_PCI.

I think most other places use the term 'transport' over 'bus'.

drivers/virtio/virtio-uclass.c line 25

Quoted 9 lines
drivers/virtio/virtio-uclass.c new file +9 -0
+
+int virtio_get_config(struct udevice *vdev, unsigned int offset,
+		      void *buf, unsigned int len)
+{
+	struct dm_virtio_ops *ops;
+
+	ops = virtio_get_ops(vdev->parent);
+	if (!ops->get_config)
+		return -ENOSYS;

I'm not sure how useful the -ENOSYS fallbacks for most of these ops are. E.g. a transport lacking .find_vqs() cannot ever be useful for implementing real-world devices.

author
#8 lore raw

PATCH 2/27 dm: Add a new uclass driver for VirtIO transport devices

Hi Tuomas,

Quoted 16 lines · Tuomas Tynkkynen

Hi Bin,

Thanks for the patches, they look great. Some minor comments:

Quoted 10 lines · Bin Meng

This adds a new virtio uclass driver for “virtio” [1] family of devices that are are found in virtual environments like QEMU, yet by design they look like physical devices to the guest.

The uclass driver provides child_pre_probe() and child_post_probe() methods to do some common operations for virtio device drivers like device and driver supported feature negotiation, etc.

[1] http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf

Quoted 12 lines

drivers/virtio/Kconfig line 12

Quoted 9 lines

---...

drivers/virtio/Kconfig new file +6 -0
+
+config VIRTIO
+      bool
+      help
+        This option is selected by any driver which implements the virtio
+        bus, such as CONFIG_VIRTIO_MMIO or CONFIG_VIRTIO_PCI.

I think most other places use the term 'transport' over 'bus'.

Changed in v2.

Quoted 5 lines
Quoted 5 lines
drivers/virtio/virtio-uclass.c new file +5 -0
+
+int virtio_get_config(struct udevice *vdev, unsigned int offset,
+                   void *buf, unsigned int len)
+{
+     struct dm_virtio_ops *ops;
Quoted 9 lines

drivers/virtio/virtio-uclass.c line 25

Quoted 4 lines
drivers/virtio/virtio-uclass.c new file +4 -0
+
+     ops = virtio_get_ops(vdev->parent);
+     if (!ops->get_config)
+             return -ENOSYS;

I'm not sure how useful the -ENOSYS fallbacks for most of these ops are. E.g. a transport lacking .find_vqs() cannot ever be useful for implementing real-world devices.

Agreed. Address this in v2.

Regards,
Bin

author
#9 lore raw

PATCH 3/27 virtio: Add codes for virtual queue/ring management

In reply to Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds support for managing virtual queue/ring, the channel for high performance I/O between host and guest.

---

 drivers/virtio/Makefile      |   2 +-
 drivers/virtio/virtio_ring.c | 356 +++++++++++++++++++++++++++++++++++++++++++
 include/virtio_ring.h        | 320 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 677 insertions(+), 1 deletion(-)
 create mode 100644 drivers/virtio/virtio_ring.c
 create mode 100644 include/virtio_ring.h
drivers/virtio/Makefile +1 -1
index 23e7be7..17d264a 100644
@@ -3,4 +3,4 @@
 # Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
 # Copyright (C) 2018, Bin Meng <[email protected]>
 
-obj-y += virtio-uclass.o
+obj-y += virtio-uclass.o virtio_ring.o
drivers/virtio/virtio_ring.c new file +356 -0

356 lines not on this page · raw message

include/virtio_ring.h new file +320 -0

320 lines not on this page · raw message

Signature · 1 line
2.7.4
#10 lore raw

PATCH 3/27 virtio: Add codes for virtual queue/ring management

Quoted 7 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds support for managing virtual queue/ring, the channel for high performance I/O between host and guest.

Quoted 8 lines

---

 drivers/virtio/Makefile      |   2 +-
 drivers/virtio/virtio_ring.c | 356 +++++++++++++++++++++++++++++++++++++++++++
 include/virtio_ring.h        | 320 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 677 insertions(+), 1 deletion(-)
 create mode 100644 drivers/virtio/virtio_ring.c
 create mode 100644 include/virtio_ring.h

Seems like vring_create_virtqueue() should return an error code rather than a pointer?

author
#11 lore raw

PATCH 3/27 virtio: Add codes for virtual queue/ring management

Hi Simon,

Quoted 18 lines · Simon Glass
Quoted 7 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds support for managing virtual queue/ring, the channel for high performance I/O between host and guest.

Quoted 8 lines

---

 drivers/virtio/Makefile      |   2 +-
 drivers/virtio/virtio_ring.c | 356 +++++++++++++++++++++++++++++++++++++++++++
 include/virtio_ring.h        | 320 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 677 insertions(+), 1 deletion(-)
 create mode 100644 drivers/virtio/virtio_ring.c
 create mode 100644 include/virtio_ring.h
Quoted 2 lines

Seems like vring_create_virtqueue() should return an error code rather than a pointer?

I think NULL as a pointer can be a sign for the error.

Quoted 2 lines

Regards,
Bin

#12 lore raw

PATCH 3/27 virtio: Add codes for virtual queue/ring management

Hi Bin,

Quoted 21 lines · Bin Meng

Hi Simon,

Quoted 18 lines · Simon Glass
Quoted 15 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds support for managing virtual queue/ring, the channel for high performance I/O between host and guest.

---

 drivers/virtio/Makefile      |   2 +-
 drivers/virtio/virtio_ring.c | 356 +++++++++++++++++++++++++++++++++++++++++++
 include/virtio_ring.h        | 320 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 677 insertions(+), 1 deletion(-)
 create mode 100644 drivers/virtio/virtio_ring.c
 create mode 100644 include/virtio_ring.h
Quoted 4 lines
Quoted 2 lines

Seems like vring_create_virtqueue() should return an error code rather than a pointer?

I think NULL as a pointer can be a sign for the error.

Yes but the issue here is that it returns more than one error, so an in is easier. With NULL you don't know what went wrong. There is ERR_PTR of course.

Regards,
Simon

#13 lore raw

PATCH 3/27 virtio: Add codes for virtual queue/ring management

In reply to Bin Meng

Hi Bin,

Quoted 8 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds support for managing virtual queue/ring, the channel for high performance I/O between host and guest.

---

...

Quoted 15 lines
include/virtio_ring.h new file +14 -0
+
+/*
+ * Barriers in virtio are tricky. Since we are not in a hyperviosr/guest
+ * scenario, having these as nops is enough to work as expected.
+ */
+
+static inline void virtio_mb(void)
+{
+}
+
+static inline void virtio_rmb(void)
+{
+}
+

include/virtio_ring.h line 320

Quoted 10 lines
include/virtio_ring.h new file +10 -0
+static inline void virtio_wmb(void)
+{
+}
+
+static inline void virtio_store_mb(__virtio16 *p, __virtio16 v)
+{
+	WRITE_ONCE(*p, v);
+}
+
+#endif /* _LINUX_VIRTIO_RING_H */

I am not convinced it's safe to leave these barriers empty. For instance if KVM acceleration is used, I believe U-Boot could be running on one core concurrently with QEMU's IO thread running on a different core. Thus we need memory barriers on both sides so that updates to the vring are observed in the correct order.

author
#14 lore raw

PATCH 3/27 virtio: Add codes for virtual queue/ring management

Hi Tuomas,

Quoted 36 lines · Tuomas Tynkkynen

Hi Bin,

Quoted 8 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds support for managing virtual queue/ring, the channel for high performance I/O between host and guest.

---

...

Quoted 22 lines
include/virtio_ring.h new file +22 -0
+
+/*
+ * Barriers in virtio are tricky. Since we are not in a hyperviosr/guest
+ * scenario, having these as nops is enough to work as expected.
+ */
+
+static inline void virtio_mb(void)
+{
+}
+
+static inline void virtio_rmb(void)
+{
+}
+
+static inline void virtio_wmb(void)
+{
+}
+
+static inline void virtio_store_mb(__virtio16 *p, __virtio16 v)
+{
+     WRITE_ONCE(*p, v);
+}
Quoted 8 lines

include/virtio_ring.h line 320

Quoted 3 lines
include/virtio_ring.h new file +2 -0
+
+#endif /* _LINUX_VIRTIO_RING_H */

I am not convinced it's safe to leave these barriers empty. For instance if KVM acceleration is used, I believe U-Boot could be running on one core concurrently with QEMU's IO thread running on a different core. Thus we need memory barriers on both sides so that updates to the vring are observed in the correct order.

I think KVM acceleration only applies if hypervisor/guest are of the same architecture. So far we are not using QEMU this way, even in QEMU x86. Hence I believe barriers are not needed.

Regards,
Bin

Regards,
Bin

author
#15 lore raw

PATCH 4/27 virtio: Add virtio over mmio transport driver

In reply to Bin Meng

VirtIO can use various different buses and virtio devices are commonly implemented as PCI devices. But virtual environments without PCI support (a common situation in embedded devices models) might use simple memory mapped device (“virtio-mmio”) instead of the PCI device.

This adds a transport driver that implements UCLASS_VIRTIO for virtio over mmio.

---

 drivers/virtio/Kconfig       |   7 +
 drivers/virtio/Makefile      |   1 +
 drivers/virtio/virtio_mmio.c | 413 +++++++++++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_mmio.h | 129 ++++++++++++++
 4 files changed, 550 insertions(+)
 create mode 100644 drivers/virtio/virtio_mmio.c
 create mode 100644 drivers/virtio/virtio_mmio.h
drivers/virtio/Kconfig +7 -0
index bdfa96a..60cfaf8 100644
@@ -11,4 +11,11 @@ config VIRTIO
 	  This option is selected by any driver which implements the virtio
 	  bus, such as CONFIG_VIRTIO_MMIO or CONFIG_VIRTIO_PCI.
 
+config VIRTIO_MMIO
+	bool "Platform bus driver for memory mapped virtio devices"
+	select VIRTIO
+	help
+	  This driver provides support for memory mapped virtio
+	  platform device driver.
+
 endmenu
drivers/virtio/Makefile +1 -0
index 17d264a..2e48785 100644
@@ -4,3 +4,4 @@
 # Copyright (C) 2018, Bin Meng <[email protected]>
 
 obj-y += virtio-uclass.o virtio_ring.o
+obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
drivers/virtio/virtio_mmio.c new file +413 -0

413 lines not on this page · raw message

drivers/virtio/virtio_mmio.h new file +129 -0

129 lines not on this page · raw message

Signature · 1 line
2.7.4
#16 lore raw

PATCH 4/27 virtio: Add virtio over mmio transport driver

Quoted 13 lines · Bin Meng

VirtIO can use various different buses and virtio devices are commonly implemented as PCI devices. But virtual environments without PCI support (a common situation in embedded devices models) might use simple memory mapped device (“virtio-mmio”) instead of the PCI device.

This adds a transport driver that implements UCLASS_VIRTIO for virtio over mmio.

---

Quoted 7 lines
 drivers/virtio/Kconfig       |   7 +
 drivers/virtio/Makefile      |   1 +
 drivers/virtio/virtio_mmio.c | 413 +++++++++++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_mmio.h | 129 ++++++++++++++
 4 files changed, 550 insertions(+)
 create mode 100644 drivers/virtio/virtio_mmio.c
 create mode 100644 drivers/virtio/virtio_mmio.h
author
#17 lore raw

PATCH 5/27 virtio: Add net driver support

In reply to Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds virtio net device driver support.

---

 drivers/virtio/Kconfig      |   7 ++
 drivers/virtio/Makefile     |   1 +
 drivers/virtio/virtio_net.c | 215 +++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_net.h | 268 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 491 insertions(+)
 create mode 100644 drivers/virtio/virtio_net.c
 create mode 100644 drivers/virtio/virtio_net.h
drivers/virtio/Kconfig +7 -0
index 60cfaf8..ceea03a 100644
@@ -18,4 +18,11 @@ config VIRTIO_MMIO
 	  This driver provides support for memory mapped virtio
 	  platform device driver.
 
+config VIRTIO_NET
+	bool "virtio net driver"
+	depends on VIRTIO
+	help
+	  This is the virtual net driver for virtio. It can be used with
+	  QEMU based targets.
+
 endmenu
drivers/virtio/Makefile +1 -0
index 2e48785..b7764f1 100644
@@ -5,3 +5,4 @@
 
 obj-y += virtio-uclass.o virtio_ring.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
+obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
drivers/virtio/virtio_net.c new file +215 -0

215 lines not on this page · raw message

drivers/virtio/virtio_net.h new file +268 -0

268 lines not on this page · raw message

Signature · 1 line
2.7.4
#18 lore raw

PATCH 5/27 virtio: Add net driver support

Quoted 8 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds virtio net device driver support.

---

Quoted 7 lines
 drivers/virtio/Kconfig      |   7 ++
 drivers/virtio/Makefile     |   1 +
 drivers/virtio/virtio_net.c | 215 +++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_net.h | 268 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 491 insertions(+)
 create mode 100644 drivers/virtio/virtio_net.c
 create mode 100644 drivers/virtio/virtio_net.h
#19 lore raw

PATCH 5/27 virtio: Add net driver support

In reply to Bin Meng

Hi Bin,

Quoted 7 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds virtio net device driver support.

---

...

drivers/virtio/virtio_net.c line 47

Quoted 7 lines
drivers/virtio/virtio_net.c new file +7 -0
+static u32 feature[] = {
+	VIRTIO_NET_F_MAC
+};
+
+static u32 feature_legacy[] = {
+	VIRTIO_NET_F_MAC
+};

These can be const.

...

drivers/virtio/virtio_net.c line 134

Quoted 9 lines
drivers/virtio/virtio_net.c new file +9 -0
+
+static void virtio_net_stop(struct udevice *dev)
+{
+	/*
+	 * There is no way to stop the queue from running, unless we issue
+	 * a reset to the virtio device, and re-do the queue initialization
+	 * from the beginning.
+	 */
+}

Unless I missed it, we still need to do something when we pass control to Linux so that the memory used for receive buffers doesn't get overwritten by the virtio device during Linux boot.

I think this can be done by calling virtio_reset() on the device in some uclass-level hook, maybe .child_post_remove().

author
#20 lore raw

PATCH 5/27 virtio: Add net driver support

Hi Tuomas,

Quoted 12 lines · Tuomas Tynkkynen

Hi Bin,

Quoted 7 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds virtio net device driver support.

---

...

Quoted 10 lines

drivers/virtio/virtio_net.c line 47

Quoted 7 lines
drivers/virtio/virtio_net.c new file +7 -0
+static u32 feature[] = {
+     VIRTIO_NET_F_MAC
+};
+
+static u32 feature_legacy[] = {
+     VIRTIO_NET_F_MAC
+};

These can be const.

Will do in v2.

Quoted 15 lines

...

drivers/virtio/virtio_net.c line 134

Quoted 9 lines
drivers/virtio/virtio_net.c new file +9 -0
+
+static void virtio_net_stop(struct udevice *dev)
+{
+     /*
+      * There is no way to stop the queue from running, unless we issue
+      * a reset to the virtio device, and re-do the queue initialization
+      * from the beginning.
+      */
+}

Unless I missed it, we still need to do something when we pass control to Linux so that the memory used for receive buffers doesn't get overwritten by the virtio device during Linux boot.

Yes, you are correct.

Quoted 2 lines

I think this can be done by calling virtio_reset() on the device in some uclass-level hook, maybe .child_post_remove().

Will do in v2.

Regards,
Bin

#21 lore raw

PATCH 5/27 virtio: Add net driver support

In reply to Bin Meng
Quoted 100 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds virtio net device driver support.

---

 drivers/virtio/Kconfig      |   7 ++
 drivers/virtio/Makefile     |   1 +
 drivers/virtio/virtio_net.c | 215 +++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_net.h | 268 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 491 insertions(+)
 create mode 100644 drivers/virtio/virtio_net.c
 create mode 100644 drivers/virtio/virtio_net.h
drivers/virtio/Kconfig +7 -0
index 60cfaf8..ceea03a 100644
@@ -18,4 +18,11 @@ config VIRTIO_MMIO
          This driver provides support for memory mapped virtio
          platform device driver.
 
+config VIRTIO_NET
+       bool "virtio net driver"
+       depends on VIRTIO
+       help
+         This is the virtual net driver for virtio. It can be used with
+         QEMU based targets.
+
 endmenu
drivers/virtio/Makefile +1 -0
index 2e48785..b7764f1 100644
@@ -5,3 +5,4 @@
 
 obj-y += virtio-uclass.o virtio_ring.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
+obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
drivers/virtio/virtio_net.c new file +52 -0
new file mode 100644
index 0000000..1ab1513
@@ -0,0 +1,215 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <net.h>
+#include <virtio.h>
+#include <virtio_ring.h>
+#include "virtio_net.h"
+
+/* Amount of buffers to keep in the RX virtqueue */
+#define VIRTIO_NET_NUM_RX_BUFS 32
+
+/*
+ * This value comes from the VirtIO spec: 1500 for maximum packet size,
+ * 14 for the Ethernet header, 12 for virtio_net_hdr. In total 1526 bytes.
+ */
+#define VIRTIO_NET_RX_BUF_SIZE 1526
+
+struct virtio_net_priv {
+       union {
+               struct virtqueue *vqs[2];
+               struct {
+                       struct virtqueue *rx_vq;
+                       struct virtqueue *tx_vq;
+               };
+       };
+
+       char rx_buff[VIRTIO_NET_NUM_RX_BUFS][VIRTIO_NET_RX_BUF_SIZE];
+       bool rx_running;
+};
+
+/*
+ * For simplicity, the driver only negotiates the VIRTIO_NET_F_MAC feature.
+ * For the VIRTIO_NET_F_STATUS feature, we don't negotiate it, hence per spec
+ * we should assume the link is always active.
+ */
+static u32 feature[] = {
+       VIRTIO_NET_F_MAC
+};
+
+static u32 feature_legacy[] = {
+       VIRTIO_NET_F_MAC
+};
+
+static int virtio_net_start(struct udevice *dev)
+{
+       struct virtio_net_priv *priv = dev_get_priv(dev);
+       struct virtio_sg sg;

drivers/virtio/virtio_net.c line 62

Quoted 10 lines
drivers/virtio/virtio_net.c new file +10 -0
+       struct virtio_sg *sgs[] = { &sg };
+       int i;
+
+       if (!priv->rx_running) {
+               /* receive buffer length is always 1526 */
+               sg.length = VIRTIO_NET_RX_BUF_SIZE;
+
+               /* setup the receive buffer address */
+               for (i = 0; i < VIRTIO_NET_NUM_RX_BUFS; i++) {
+                       sg.addr = priv->rx_buff[i];

It's kinda peculiar the way this is used in place multiple times.

drivers/virtio/virtio_net.c line 63

Quoted 1 line
drivers/virtio/virtio_net.c new file +1 -0
+                       virtqueue_add(priv->rx_vq, sgs, 0, 1);

I hope this copies the structure since it is stack memory and modified on each iteration. Probably justifies a comment.

Quoted 21 lines
drivers/virtio/virtio_net.c new file +21 -0
+               }
+
+               virtqueue_kick(priv->rx_vq);
+
+               /* setup the receive queue only once */
+               priv->rx_running = true;
+       }
+
+       return 0;
+}
+
+static int virtio_net_send(struct udevice *dev, void *packet, int length)
+{
+       struct virtio_net_priv *priv = dev_get_priv(dev);
+       struct virtio_net_hdr hdr;
+       struct virtio_sg hdr_sg = { &hdr, sizeof(hdr) };
+       struct virtio_sg data_sg = { packet, length };
+       struct virtio_sg *sgs[] = { &hdr_sg, &data_sg };
+       int ret;
+
+       memset(&hdr, 0, sizeof(struct virtio_net_hdr));

drivers/virtio/virtio_net.c line 94

Quoted 10 lines
drivers/virtio/virtio_net.c new file +10 -0
+
+       ret = virtqueue_add(priv->tx_vq, sgs, 2, 0);
+       if (ret)
+               return ret;
+
+       virtqueue_kick(priv->tx_vq);
+
+       while (1) {
+               if (virtqueue_get_buf(priv->tx_vq, NULL))
+                       break;

Potential infinite loop?

Quoted 18 lines
drivers/virtio/virtio_net.c new file +18 -0
+       }
+
+       return 0;
+}
+
+static int virtio_net_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+       struct virtio_net_priv *priv = dev_get_priv(dev);
+       unsigned int len;
+       void *buf;
+
+       buf = virtqueue_get_buf(priv->rx_vq, &len);
+       if (!buf)
+               return -EAGAIN;
+
+       *packetp = buf + sizeof(struct virtio_net_hdr);
+       return len - sizeof(struct virtio_net_hdr);
+}

drivers/virtio/virtio_net.c line 122

Quoted 10 lines
drivers/virtio/virtio_net.c new file +10 -0
+
+static int virtio_net_free_pkt(struct udevice *dev, uchar *packet, int length)
+{
+       struct virtio_net_priv *priv = dev_get_priv(dev);
+       void *buf = packet - sizeof(struct virtio_net_hdr);
+       struct virtio_sg sg = { buf, VIRTIO_NET_RX_BUF_SIZE };
+       struct virtio_sg *sgs[] = { &sg };
+
+       /* Put the buffer back to the rx ring */
+       virtqueue_add(priv->rx_vq, sgs, 0, 1);

So does that mean that the virtqueue_get_buf() function removed it?

Quoted 57 lines
drivers/virtio/virtio_net.c new file +57 -0
+
+       return 0;
+}
+
+static void virtio_net_stop(struct udevice *dev)
+{
+       /*
+        * There is no way to stop the queue from running, unless we issue
+        * a reset to the virtio device, and re-do the queue initialization
+        * from the beginning.
+        */
+}
+
+static int virtio_net_write_hwaddr(struct udevice *dev)
+{
+       struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       int i;
+
+       /*
+        * v1.0 compliant device's MAC address is set through control channel,
+        * which we don't support for now.
+        */
+       if (!uc_priv->legacy)
+               return -ENOSYS;
+
+       for (i = 0; i < sizeof(pdata->enetaddr); i++) {
+               virtio_cwrite8(dev,
+                              offsetof(struct virtio_net_config, mac) + i,
+                              pdata->enetaddr[i]);
+       }
+
+       return 0;
+}
+
+static int virtio_net_read_rom_hwaddr(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+
+       if (!pdata)
+               return -ENOSYS;
+
+       if (virtio_has_feature(dev, VIRTIO_NET_F_MAC)) {
+               virtio_cread_bytes(dev,
+                                  offsetof(struct virtio_net_config, mac),
+                                  pdata->enetaddr, sizeof(pdata->enetaddr));
+       }
+
+       return 0;
+}
+
+static int virtio_net_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, feature, ARRAY_SIZE(feature),

drivers/virtio/virtio_net.c line 189

Quoted 10 lines
drivers/virtio/virtio_net.c new file +10 -0
+                                   feature_legacy, ARRAY_SIZE(feature_legacy));
+
+       return 0;
+}
+
+static int virtio_net_probe(struct udevice *dev)
+{
+       struct virtio_net_priv *priv = dev_get_priv(dev);
+       int ret;
+

Seems like priv->rx_running should be initialized to false here.

Quoted 307 lines
drivers/virtio/virtio_net.c new file +26 -0
+       ret = virtio_find_vqs(dev, 2, priv->vqs);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static const struct eth_ops virtio_net_ops = {
+       .start = virtio_net_start,
+       .send = virtio_net_send,
+       .recv = virtio_net_recv,
+       .free_pkt = virtio_net_free_pkt,
+       .stop = virtio_net_stop,
+       .write_hwaddr = virtio_net_write_hwaddr,
+       .read_rom_hwaddr = virtio_net_read_rom_hwaddr,
+};
+
+U_BOOT_DRIVER(virtio_net) = {
+       .name   = VIRTIO_NET_DRV_NAME,
+       .id     = UCLASS_ETH,
+       .bind   = virtio_net_bind,
+       .probe  = virtio_net_probe,
+       .ops    = &virtio_net_ops,
+       .priv_auto_alloc_size = sizeof(struct virtio_net_priv),
+       .platdata_auto_alloc_size = sizeof(struct eth_pdata),
+};
drivers/virtio/virtio_net.h new file +268 -1
new file mode 100644
index 0000000..c92bae5
@@ -0,0 +1,268 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ *
+ * From Linux kernel include/uapi/linux/virtio_net.h
+ */
+
+#ifndef _LINUX_VIRTIO_NET_H
+#define _LINUX_VIRTIO_NET_H
+
+/* TODO: needs to be removed! */
+#define ETH_ALEN                               6
+
+/* The feature bitmap for virtio net */
+
+/* Host handles pkts w/ partial csum */
+#define VIRTIO_NET_F_CSUM                      0
+/* Guest handles pkts w/ partial csum */
+#define VIRTIO_NET_F_GUEST_CSUM                        1
+/* Dynamic offload configuration */
+#define VIRTIO_NET_F_CTRL_GUEST_OFFLOADS       2
+/* Initial MTU advice */
+#define VIRTIO_NET_F_MTU                       3
+/* Host has given MAC address */
+#define VIRTIO_NET_F_MAC                       5
+/* Guest can handle TSOv4 in */
+#define VIRTIO_NET_F_GUEST_TSO4                        7
+/* Guest can handle TSOv6 in */
+#define VIRTIO_NET_F_GUEST_TSO6                        8
+/* Guest can handle TSO[6] w/ ECN in */
+#define VIRTIO_NET_F_GUEST_ECN                 9
+/* Guest can handle UFO in */
+#define VIRTIO_NET_F_GUEST_UFO                 10
+/* Host can handle TSOv4 in */
+#define VIRTIO_NET_F_HOST_TSO4                 11
+/* Host can handle TSOv6 in */
+#define VIRTIO_NET_F_HOST_TSO6                 12
+/* Host can handle TSO[6] w/ ECN in */
+#define VIRTIO_NET_F_HOST_ECN                  13
+/* Host can handle UFO in */
+#define VIRTIO_NET_F_HOST_UFO                  14
+/* Host can merge receive buffers */
+#define VIRTIO_NET_F_MRG_RXBUF                 15
+/* virtio_net_config.status available */
+#define VIRTIO_NET_F_STATUS                    16
+/* Control channel available */
+#define VIRTIO_NET_F_CTRL_VQ                   17
+/* Control channel RX mode support */
+#define VIRTIO_NET_F_CTRL_RX                   18
+/* Control channel VLAN filtering */
+#define VIRTIO_NET_F_CTRL_VLAN                 19
+/* Extra RX mode control support */
+#define VIRTIO_NET_F_CTRL_RX_EXTRA             20
+/* Guest can announce device on the network */
+#define VIRTIO_NET_F_GUEST_ANNOUNCE            21
+/* Device supports receive flow steering */
+#define VIRTIO_NET_F_MQ                                22
+/* Set MAC address */
+#define VIRTIO_NET_F_CTRL_MAC_ADDR             23
+/* Device set linkspeed and duplex */
+#define VIRTIO_NET_F_SPEED_DUPLEX              63
+
+#ifndef VIRTIO_NET_NO_LEGACY
+/* Host handles pkts w/ any GSO type */
+#define VIRTIO_NET_F_GSO                       6
+#endif /* VIRTIO_NET_NO_LEGACY */
+
+#define VIRTIO_NET_S_LINK_UP                   1 /* Link is up */
+#define VIRTIO_NET_S_ANNOUNCE                  2 /* Announcement is needed */
+
+struct __packed virtio_net_config {
+       /* The config defining mac address (if VIRTIO_NET_F_MAC) */
+       __u8 mac[ETH_ALEN];
+       /* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
+       __u16 status;
+       /*
+        * Maximum number of each of transmit and receive queues;
+        * see VIRTIO_NET_F_MQ and VIRTIO_NET_CTRL_MQ.
+        * Legal values are between 1 and 0x8000
+        */
+       __u16 max_virtqueue_pairs;
+       /* Default maximum transmit unit advice */
+       __u16 mtu;
+       /*
+        * speed, in units of 1Mb. All values 0 to INT_MAX are legal.
+        * Any other value stands for unknown.
+        */
+       __u32 speed;
+       /*
+        * 0x00 - half duplex
+        * 0x01 - full duplex
+        * Any other value stands for unknown.
+        */
+       __u8 duplex;
+};
+
+/*
+ * This header comes first in the scatter-gather list. If you don't
+ * specify GSO or CSUM features, you can simply ignore the header.
+ *
+ * This is bitwise-equivalent to the legacy struct virtio_net_hdr_mrg_rxbuf,
+ * only flattened.
+ */
+struct virtio_net_hdr_v1 {
+#define VIRTIO_NET_HDR_F_NEEDS_CSUM    0x01 /* Use csum_start, csum_offset */
+#define VIRTIO_NET_HDR_F_DATA_VALID    0x02 /* Csum is valid */
+       __u8 flags;
+#define VIRTIO_NET_HDR_GSO_NONE                0x00 /* Not a GSO frame */
+#define VIRTIO_NET_HDR_GSO_TCPV4       0x01 /* GSO frame, IPv4 TCP (TSO) */
+#define VIRTIO_NET_HDR_GSO_UDP         0x03 /* GSO frame, IPv4 UDP (UFO) */
+#define VIRTIO_NET_HDR_GSO_TCPV6       0x04 /* GSO frame, IPv6 TCP */
+#define VIRTIO_NET_HDR_GSO_ECN         0x80 /* TCP has ECN set */
+       __u8 gso_type;
+       __virtio16 hdr_len;     /* Ethernet + IP + tcp/udp hdrs */
+       __virtio16 gso_size;    /* Bytes to append to hdr_len per frame */
+       __virtio16 csum_start;  /* Position to start checksumming from */
+       __virtio16 csum_offset; /* Offset after that to place checksum */
+       __virtio16 num_buffers; /* Number of merged rx buffers */
+};
+
+#ifndef VIRTIO_NET_NO_LEGACY
+/*
+ * This header comes first in the scatter-gather list.
+ *
+ * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, it must
+ * be the first element of the scatter-gather list. If you don't
+ * specify GSO or CSUM features, you can simply ignore the header.
+ */
+struct virtio_net_hdr {
+       /* See VIRTIO_NET_HDR_F_* */
+       __u8 flags;
+       /* See VIRTIO_NET_HDR_GSO_* */
+       __u8 gso_type;
+       __virtio16 hdr_len;     /* Ethernet + IP + tcp/udp hdrs */
+       __virtio16 gso_size;    /* Bytes to append to hdr_len per frame */
+       __virtio16 csum_start;  /* Position to start checksumming from */
+       __virtio16 csum_offset; /* Offset after that to place checksum */
+};
+
+/*
+ * This is the version of the header to use when the MRG_RXBUF
+ * feature has been negotiated.
+ */
+struct virtio_net_hdr_mrg_rxbuf {
+       struct virtio_net_hdr hdr;
+       __virtio16 num_buffers; /* Number of merged rx buffers */
+};
+#endif /* ...VIRTIO_NET_NO_LEGACY */
+
+/*
+ * Control virtqueue data structures
+ *
+ * The control virtqueue expects a header in the first sg entry
+ * and an ack/status response in the last entry.  Data for the
+ * command goes in between.
+ */
+struct __packed virtio_net_ctrl_hdr {
+       __u8 class;
+       __u8 cmd;
+};
+
+typedef __u8 virtio_net_ctrl_ack;
+
+#define VIRTIO_NET_OK                          0
+#define VIRTIO_NET_ERR                         1
+
+/*
+ * Control the RX mode, ie. promisucous, allmulti, etc...
+ *
+ * All commands require an "out" sg entry containing a 1 byte state value,
+ * zero = disable, non-zero = enable.
+ *
+ * Commands 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
+ * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
+ */
+#define VIRTIO_NET_CTRL_RX                     0
+#define VIRTIO_NET_CTRL_RX_PROMISC             0
+#define VIRTIO_NET_CTRL_RX_ALLMULTI            1
+#define VIRTIO_NET_CTRL_RX_ALLUNI              2
+#define VIRTIO_NET_CTRL_RX_NOMULTI             3
+#define VIRTIO_NET_CTRL_RX_NOUNI               4
+#define VIRTIO_NET_CTRL_RX_NOBCAST             5
+
+/*
+ * Control the MAC
+ *
+ * The MAC filter table is managed by the hypervisor, the guest should assume
+ * the size is infinite. Filtering should be considered non-perfect, ie. based
+ * on hypervisor resources, the guest may received packets from sources not
+ * specified in the filter list.
+ *
+ * In addition to the class/cmd header, the TABLE_SET command requires two
+ * out scatterlists. Each contains a 4 byte count of entries followed by a
+ * concatenated byte stream of the ETH_ALEN MAC addresses.  The first sg list
+ * contains unicast addresses, the second is for multicast. This functionality
+ * is present if the VIRTIO_NET_F_CTRL_RX feature is available.
+ *
+ * The ADDR_SET command requests one out scatterlist, it contains a 6 bytes MAC
+ * address. This functionality is present if the VIRTIO_NET_F_CTRL_MAC_ADDR
+ * feature is available.
+ */
+struct __packed virtio_net_ctrl_mac {
+       __virtio32 entries;
+       __u8 macs[][ETH_ALEN];
+};
+
+#define VIRTIO_NET_CTRL_MAC                    1
+#define VIRTIO_NET_CTRL_MAC_TABLE_SET          0
+#define VIRTIO_NET_CTRL_MAC_ADDR_SET           1
+
+/*
+ * Control VLAN filtering
+ *
+ * The VLAN filter table is controlled via a simple ADD/DEL interface. VLAN IDs
+ * not added may be filterd by the hypervisor. Del is the opposite of add. Both
+ * commands expect an out entry containing a 2 byte VLAN ID. VLAN filterting is
+ * available with the VIRTIO_NET_F_CTRL_VLAN feature bit.
+ */
+#define VIRTIO_NET_CTRL_VLAN                   2
+#define VIRTIO_NET_CTRL_VLAN_ADD               0
+#define VIRTIO_NET_CTRL_VLAN_DEL               1
+
+/*
+ * Control link announce acknowledgment
+ *
+ * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that driver has
+ * recevied the notification; device would clear the VIRTIO_NET_S_ANNOUNCE bit
+ * in the status field after it receives this command.
+ */
+#define VIRTIO_NET_CTRL_ANNOUNCE               3
+#define VIRTIO_NET_CTRL_ANNOUNCE_ACK           0
+
+/*
+ * Control receive flow steering
+ *
+ * The command VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET enables receive flow steering,
+ * specifying the number of the transmit and receive queues that will be used.
+ * After the command is consumed and acked by the device, the device will not
+ * steer new packets on receive virtqueues other than specified nor read from
+ * transmit virtqueues other than specified. Accordingly, driver should not
+ * transmit new packets  on virtqueues other than specified.
+ */
+struct virtio_net_ctrl_mq {
+       __virtio16 virtqueue_pairs;
+};
+
+#define VIRTIO_NET_CTRL_MQ                     4
+#define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET                0
+#define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN                1
+#define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX                0x8000
+
+/*
+ * Control network offloads
+ *
+ * Reconfigures the network offloads that guest can handle.
+ *
+ * Available with the VIRTIO_NET_F_CTRL_GUEST_OFFLOADS feature bit.
+ *
+ * Command data format matches the feature bit mask exactly.
+ *
+ * See VIRTIO_NET_F_GUEST_* for the list of offloads
+ * that can be enabled/disabled.
+ */
+#define VIRTIO_NET_CTRL_GUEST_OFFLOADS         5
+#define VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET     0
+
+#endif /* _LINUX_VIRTIO_NET_H */
--

2.7.4

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot

author
#22 lore raw

PATCH 6/27 test: dm: blk: Correct blk_base test case

In reply to Bin Meng

The blk_base test case creates a USB mass storage block device with the Sandbox host block device as its parent. This does not make any sense and causes potential issue, for example if the test case tries to read/write anything on the USB mass storage block device it will definitely fail as its parent is not on USB bus at all.

Correct the test case by creating another Sandbox host block device instead of the USB mass storage one and adjust the case accordingly.

---

 test/dm/blk.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)
test/dm/blk.c +11 -16
index 4de477b..9c71adc 100644
@@ -15,34 +15,29 @@ DECLARE_GLOBAL_DATA_PTR;
 /* Test that block devices can be created */
 static int dm_test_blk_base(struct unit_test_state *uts)
 {
-	struct udevice *blk, *usb_blk, *dev;
+	struct udevice *blk1, *blk3, *dev;
 
 	/* Make sure there are no block devices */
-	ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &blk));
+	ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &dev));
 
 	/* Create two, one the parent of the other */
 	ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
-				      IF_TYPE_HOST, 1, 512, 2, &blk));
-	ut_assertok(blk_create_device(blk, "usb_storage_blk", "test",
-				      IF_TYPE_USB, 3, 512, 2, &usb_blk));
+				      IF_TYPE_HOST, 1, 512, 2, &blk1));
+	ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test",
+				      IF_TYPE_HOST, 3, 512, 2, &blk3));
 
 	/* Check we can find them */
 	ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev));
 	ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
-	ut_asserteq_ptr(blk, dev);
-
-	ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_USB, 0, &dev));
-	ut_assertok(blk_get_device(IF_TYPE_USB, 3, &dev));
-	ut_asserteq_ptr(usb_blk, dev);
+	ut_asserteq_ptr(blk1, dev);
+	ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev));
+	ut_asserteq_ptr(blk3, dev);
 
 	/* Check we can iterate */
 	ut_assertok(blk_first_device(IF_TYPE_HOST, &dev));
-	ut_asserteq_ptr(blk, dev);
-	ut_asserteq(-ENODEV, blk_next_device(&dev));
-
-	ut_assertok(blk_first_device(IF_TYPE_USB, &dev));
-	ut_asserteq_ptr(usb_blk, dev);
-	ut_asserteq(-ENODEV, blk_next_device(&dev));
+	ut_asserteq_ptr(blk1, dev);
+	ut_assertok(blk_next_device(&dev));
+	ut_asserteq_ptr(blk3, dev);
 
 	return 0;
 }
Signature · 1 line
2.7.4
#23 lore raw

PATCH 6/27 test: dm: blk: Correct blk_base test case

Quoted 6 lines · Bin Meng

The blk_base test case creates a USB mass storage block device with the Sandbox host block device as its parent. This does not make any sense and causes potential issue, for example if the test case tries to read/write anything on the USB mass storage block device it will definitely fail as its parent is not on USB bus at all.

Quoted 9 lines

Correct the test case by creating another Sandbox host block device instead of the USB mass storage one and adjust the case accordingly.

---

 test/dm/blk.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)
author
#24 lore raw

PATCH 7/27 sandbox: blk: Switch to use platdata_auto_alloc_size for the driver data

In reply to Bin Meng

Currently the sandbox block driver uses priv_auto_alloc_size for the driver data, however that's only available after the device probe phase. In order to make it accessible in an earlier phase, switch to use platdata_auto_alloc_size instead.

This patch is the prerequisite for the follow up patch of DM BLK driver changes to work with Sandbox.

---

 drivers/block/sandbox.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/block/sandbox.c +8 -7
index 0392437..576d049 100644
@@ -33,7 +33,7 @@ static unsigned long host_block_read(struct udevice *dev,
 				     unsigned long start, lbaint_t blkcnt,
 				     void *buffer)
 {
-	struct host_block_dev *host_dev = dev_get_priv(dev);
+	struct host_block_dev *host_dev = dev_get_platdata(dev);
 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
 
 #else
@@ -64,7 +64,7 @@ static unsigned long host_block_write(struct udevice *dev,
 				      unsigned long start, lbaint_t blkcnt,
 				      const void *buffer)
 {
-	struct host_block_dev *host_dev = dev_get_priv(dev);
+	struct host_block_dev *host_dev = dev_get_platdata(dev);
 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
 #else
 static unsigned long host_block_write(struct blk_desc *block_dev,
@@ -131,16 +131,17 @@ int host_dev_bind(int devnum, char *filename)
 				os_lseek(fd, 0, OS_SEEK_END) / 512, &dev);
 	if (ret)
 		goto err_file;
+
+	host_dev = dev_get_platdata(dev);
+	host_dev->fd = fd;
+	host_dev->filename = fname;
+
 	ret = device_probe(dev);
 	if (ret) {
 		device_unbind(dev);
 		goto err_file;
 	}
 
-	host_dev = dev_get_priv(dev);
-	host_dev->fd = fd;
-	host_dev->filename = fname;
-
 	return blk_prepare_device(dev);
 err_file:
 	os_close(fd);
@@ -226,7 +227,7 @@ U_BOOT_DRIVER(sandbox_host_blk) = {
 	.name		= "sandbox_host_blk",
 	.id		= UCLASS_BLK,
 	.ops		= &sandbox_host_blk_ops,
-	.priv_auto_alloc_size	= sizeof(struct host_block_dev),
+	.platdata_auto_alloc_size = sizeof(struct host_block_dev),
 };
 #else
 U_BOOT_LEGACY_BLK(sandbox_host) = {
Signature · 1 line
2.7.4
#25 lore raw

PATCH 7/27 sandbox: blk: Switch to use platdata_auto_alloc_size for the driver data

Quoted 5 lines · Bin Meng

Currently the sandbox block driver uses priv_auto_alloc_size for the driver data, however that's only available after the device probe phase. In order to make it accessible in an earlier phase, switch to use platdata_auto_alloc_size instead.

Quoted 8 lines

This patch is the prerequisite for the follow up patch of DM BLK driver changes to work with Sandbox.

---

 drivers/block/sandbox.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
author
#26 lore raw

PATCH 8/27 efi_driver: blk: Switch to use platdata_auto_alloc_size for the driver data

In reply to Bin Meng

Currently the efi block driver uses priv_auto_alloc_size for the driver data, however that's only available after the device probe phase. In order to make it accessible in an earlier phase, switch to use platdata_auto_alloc_size instead.

This patch is the prerequisite for the follow up patch of DM BLK driver changes to work with EFI loader.

---

 lib/efi_driver/efi_block_device.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
lib/efi_driver/efi_block_device.c +12 -12
index 5b9c139..7b71b4d 100644
@@ -38,7 +38,7 @@
  * handle	handle of the controller on which this driver is installed
  * io		block io protocol proxied by this driver
  */
-struct efi_blk_priv {
+struct efi_blk_platdata {
 	efi_handle_t		handle;
 	struct efi_block_io	*io;
 };
@@ -55,8 +55,8 @@ struct efi_blk_priv {
 static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
 			 void *buffer)
 {
-	struct efi_blk_priv *priv = dev->priv;
-	struct efi_block_io *io = priv->io;
+	struct efi_blk_platdata *platdata = dev_get_platdata(dev);
+	struct efi_block_io *io = platdata->io;
 	efi_status_t ret;
 
 	EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n",
@@ -84,8 +84,8 @@ static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
 static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
 			  const void *buffer)
 {
-	struct efi_blk_priv *priv = dev->priv;
-	struct efi_block_io *io = priv->io;
+	struct efi_blk_platdata *platdata = dev_get_platdata(dev);
+	struct efi_block_io *io = platdata->io;
 	efi_status_t ret;
 
 	EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n",
@@ -135,7 +135,7 @@ static int efi_bl_bind(efi_handle_t handle, void *interface)
 	struct efi_object *obj = efi_search_obj(handle);
 	struct efi_block_io *io = interface;
 	int disks;
-	struct efi_blk_priv *priv;
+	struct efi_blk_platdata *platdata;
 
 	EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io);
 
@@ -163,16 +163,16 @@ static int efi_bl_bind(efi_handle_t handle, void *interface)
 		return -ENOENT;
 	/* Set the DM_FLAG_NAME_ALLOCED flag to avoid a memory leak */
 	device_set_name_alloced(bdev);
-	/* Allocate priv */
+
+	platdata = dev_get_platdata(bdev);
+	platdata->handle = handle;
+	platdata->io = interface;
+
 	ret = device_probe(bdev);
 	if (ret)
 		return ret;
 	EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
 
-	priv = bdev->priv;
-	priv->handle = handle;
-	priv->io = interface;
-
 	ret = blk_prepare_device(bdev);
 
 	/* Create handles for the partions of the block device */
@@ -193,7 +193,7 @@ U_BOOT_DRIVER(efi_blk) = {
 	.name			= "efi_blk",
 	.id			= UCLASS_BLK,
 	.ops			= &efi_blk_ops,
-	.priv_auto_alloc_size	= sizeof(struct efi_blk_priv),
+	.platdata_auto_alloc_size = sizeof(struct efi_blk_platdata),
 };
 
 /* EFI driver operators */
Signature · 1 line
2.7.4
#27 lore raw

PATCH 8/27 efi_driver: blk: Switch to use platdata_auto_alloc_size for the driver data

Quoted 5 lines · Bin Meng

Currently the efi block driver uses priv_auto_alloc_size for the driver data, however that's only available after the device probe phase. In order to make it accessible in an earlier phase, switch to use platdata_auto_alloc_size instead.

Quoted 8 lines

This patch is the prerequisite for the follow up patch of DM BLK driver changes to work with EFI loader.

---

 lib/efi_driver/efi_block_device.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
author
#28 lore raw

PATCH 9/27 blk: Call part_init() in the post_probe() method

In reply to Bin Meng

part_init() is currently called in every DM BLK driver, either in its bind() or probe() method. However we can use the BLK uclass driver's post_probe() method to do it automatically.

Update all DM BLK drivers to adopt this change.

---

 cmd/sata.c                        |  9 ---------
 common/usb_storage.c              |  4 +---
 drivers/block/blk-uclass.c        | 12 ++++++++++++
 drivers/block/ide.c               |  2 --
 drivers/block/sandbox.c           |  2 +-
 drivers/mmc/mmc.c                 |  3 ---
 drivers/nvme/nvme.c               |  1 -
 drivers/scsi/scsi.c               |  1 -
 lib/efi_driver/efi_block_device.c |  2 --
 9 files changed, 14 insertions(+), 22 deletions(-)
cmd/sata.c +0 -9
index 4f0c6e0..6d62ba8 100644
@@ -51,7 +51,6 @@ int sata_probe(int devnum)
 {
 #ifdef CONFIG_AHCI
 	struct udevice *dev;
-	struct udevice *blk;
 	int rc;
 
 	rc = uclass_get_device(UCLASS_AHCI, devnum, &dev);
@@ -67,14 +66,6 @@ int sata_probe(int devnum)
 		return CMD_RET_FAILURE;
 	}
 
-	rc = blk_get_from_parent(dev, &blk);
-	if (!rc) {
-		struct blk_desc *desc = dev_get_uclass_platdata(blk);
-
-		if (desc->lba > 0 && desc->blksz > 0)
-			part_init(desc);
-	}
-
 	return 0;
 #else
 	return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
common/usb_storage.c +1 -3
index d92ebb6..560d605 100644
@@ -226,9 +226,7 @@ static int usb_stor_probe_device(struct usb_device *udev)
 		blkdev->lun = lun;
 
 		ret = usb_stor_get_info(udev, data, blkdev);
-		if (ret == 1)
-			ret = blk_prepare_device(dev);
-		if (!ret) {
+		if (ret == 1) {
 			usb_max_devs++;
 			debug("%s: Found device %p\n", __func__, udev);
 		} else {
drivers/block/blk-uclass.c +12 -0
index 9e0c823..c85b392 100644
@@ -621,8 +621,20 @@ int blk_unbind_all(int if_type)
 	return 0;
 }
 
+static int blk_post_probe(struct udevice *dev)
+{
+#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
+	struct blk_desc *desc = dev_get_uclass_platdata(dev);
+
+	part_init(desc);
+#endif
+
+	return 0;
+}
+
 UCLASS_DRIVER(blk) = {
 	.id		= UCLASS_BLK,
 	.name		= "blk",
+	.post_probe	= blk_post_probe,
 	.per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),
 };
drivers/block/ide.c +0 -2
index 38adb6a..4b8a4ea 100644
@@ -1169,8 +1169,6 @@ static int ide_blk_probe(struct udevice *udev)
 		BLK_REV_SIZE);
 	desc->revision[BLK_REV_SIZE] = '\0';
 
-	part_init(desc);
-
 	return 0;
 }
 
drivers/block/sandbox.c +1 -1
index 576d049..d3b1aaa 100644
@@ -142,7 +142,7 @@ int host_dev_bind(int devnum, char *filename)
 		goto err_file;
 	}
 
-	return blk_prepare_device(dev);
+	return 0;
 err_file:
 	os_close(fd);
 err:
drivers/mmc/mmc.c +0 -3
index 585951c..d6b9cdc 100644
@@ -2444,9 +2444,6 @@ static int mmc_startup(struct mmc *mmc)
 	bdesc->product[0] = 0;
 	bdesc->revision[0] = 0;
 #endif
-#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
-	part_init(bdesc);
-#endif
 
 	return 0;
 }
drivers/nvme/nvme.c +0 -1
index eb6fded..1ee0a0a 100644
@@ -664,7 +664,6 @@ static int nvme_blk_probe(struct udevice *udev)
 	sprintf(desc->vendor, "0x%.4x", pplat->vendor);
 	memcpy(desc->product, ndev->serial, sizeof(ndev->serial));
 	memcpy(desc->revision, ndev->firmware_rev, sizeof(ndev->firmware_rev));
-	part_init(desc);
 
 	return 0;
 }
drivers/scsi/scsi.c +0 -1
index bc6ac8c..df47e2f 100644
@@ -592,7 +592,6 @@ static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
 	memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
 	memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
 	memcpy(&bdesc->revision, &bd.revision,	sizeof(bd.revision));
-	part_init(bdesc);
 
 	if (verbose) {
 		printf("  Device %d: ", 0);
lib/efi_driver/efi_block_device.c +0 -2
index 7b71b4d..3f147cf 100644
@@ -173,8 +173,6 @@ static int efi_bl_bind(efi_handle_t handle, void *interface)
 		return ret;
 	EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name);
 
-	ret = blk_prepare_device(bdev);
-
 	/* Create handles for the partions of the block device */
 	disks = efi_bl_bind_partitions(handle, bdev);
 	EFI_PRINT("Found %d partitions\n", disks);
Signature · 1 line
2.7.4
#29 lore raw

PATCH 9/27 blk: Call part_init() in the post_probe() method

Quoted 9 lines · Bin Meng

part_init() is currently called in every DM BLK driver, either in its bind() or probe() method. However we can use the BLK uclass driver's post_probe() method to do it automatically.

Update all DM BLK drivers to adopt this change.

---

Quoted 10 lines
 cmd/sata.c                        |  9 ---------
 common/usb_storage.c              |  4 +---
 drivers/block/blk-uclass.c        | 12 ++++++++++++
 drivers/block/ide.c               |  2 --
 drivers/block/sandbox.c           |  2 +-
 drivers/mmc/mmc.c                 |  3 ---
 drivers/nvme/nvme.c               |  1 -
 drivers/scsi/scsi.c               |  1 -
 lib/efi_driver/efi_block_device.c |  2 --
 9 files changed, 14 insertions(+), 22 deletions(-)
author
#30 lore raw

PATCH 10/27 blk: Drop blk_prepare_device()

In reply to Bin Meng

With the post_probe() changes, this API is no longer needed.

---

 drivers/block/blk-uclass.c |  9 ---------
 include/blk.h              | 10 ----------
 2 files changed, 19 deletions(-)
drivers/block/blk-uclass.c +0 -9
index c85b392..acd2c69 100644
@@ -448,15 +448,6 @@ unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
 	return ops->erase(dev, start, blkcnt);
 }
 
-int blk_prepare_device(struct udevice *dev)
-{
-	struct blk_desc *desc = dev_get_uclass_platdata(dev);
-
-	part_init(desc);
-
-	return 0;
-}
-
 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
 {
 	struct udevice *dev;
include/blk.h +0 -10
index 86f6d50..32104ff 100644
@@ -357,16 +357,6 @@ int blk_create_devicef(struct udevice *parent, const char *drv_name,
 		       lbaint_t lba, struct udevice **devp);
 
 /**
- * blk_prepare_device() - Prepare a block device for use
- *
- * This reads partition information from the device if supported.
- *
- * @dev:	Device to prepare
- * @return 0 if ok, -ve on error
- */
-int blk_prepare_device(struct udevice *dev);
-
-/**
  * blk_unbind_all() - Unbind all device of the given interface type
  *
  * The devices are removed and then unbound.
Signature · 1 line
2.7.4
author
#32 lore raw

PATCH 11/27 blk: Make blk_next_free_devnum() public

In reply to Bin Meng

blk_next_free_devnum() can be helpful in some cases. Make it a public API.

---

 drivers/block/blk-uclass.c |  2 +-
 include/blk.h              | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/block/blk-uclass.c +1 -1
index acd2c69..c14da1f 100644
@@ -494,7 +494,7 @@ int blk_find_max_devnum(enum if_type if_type)
 	return max_devnum;
 }
 
-static int blk_next_free_devnum(enum if_type if_type)
+int blk_next_free_devnum(enum if_type if_type)
 {
 	int ret;
 
include/blk.h +11 -0
index 32104ff..9787a20 100644
@@ -379,6 +379,17 @@ int blk_unbind_all(int if_type);
 int blk_find_max_devnum(enum if_type if_type);
 
 /**
+ * blk_next_free_devnum() - get the next device number for an interface type
+ *
+ * Finds the next number that is safe to use for a newly allocated device for
+ * an interface type @if_type.
+ *
+ * @if_type:	Interface type to scan
+ * @return next device number safe to use, or -ve on error
+ */
+int blk_next_free_devnum(enum if_type if_type);
+
+/**
  * blk_select_hwpart() - select a hardware partition
  *
  * Select a hardware partition if the device supports it (typically MMC does)
Signature · 1 line
2.7.4
author
#34 lore raw

PATCH 12/27 blk: Introduce IF_TYPE_VIRTIO

In reply to Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds a new block interface type for VirtIO block devices.

---

 disk/part.c                | 6 ++++++
 drivers/block/blk-uclass.c | 2 ++
 include/blk.h              | 1 +
 3 files changed, 9 insertions(+)
disk/part.c +6 -0
index 9e457a6..f30f9e9 100644
@@ -150,6 +150,9 @@ void dev_print (struct blk_desc *dev_desc)
 			dev_desc->revision,
 			dev_desc->product);
 		break;
+	case IF_TYPE_VIRTIO:
+		printf("%s VirtIO Block Device\n", dev_desc->vendor);
+		break;
 	case IF_TYPE_DOC:
 		puts("device type DOC\n");
 		return;
@@ -281,6 +284,9 @@ static void print_part_header(const char *type, struct blk_desc *dev_desc)
 	case IF_TYPE_NVME:
 		puts ("NVMe");
 		break;
+	case IF_TYPE_VIRTIO:
+		puts("VirtIO");
+		break;
 	default:
 		puts ("UNKNOWN");
 		break;
drivers/block/blk-uclass.c +2 -0
index c14da1f..dda1f58 100644
@@ -23,6 +23,7 @@ static const char *if_typename_str[IF_TYPE_COUNT] = {
 	[IF_TYPE_HOST]		= "host",
 	[IF_TYPE_NVME]		= "nvme",
 	[IF_TYPE_EFI]		= "efi",
+	[IF_TYPE_VIRTIO]	= "virtio",
 };
 
 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
@@ -37,6 +38,7 @@ static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
 	[IF_TYPE_HOST]		= UCLASS_ROOT,
 	[IF_TYPE_NVME]		= UCLASS_NVME,
 	[IF_TYPE_EFI]		= UCLASS_EFI,
+	[IF_TYPE_VIRTIO]	= UCLASS_VIRTIO,
 };
 
 static enum if_type if_typename_to_iftype(const char *if_typename)
include/blk.h +1 -0
index 9787a20..170ed19 100644
@@ -33,6 +33,7 @@ enum if_type {
 	IF_TYPE_HOST,
 	IF_TYPE_NVME,
 	IF_TYPE_EFI,
+	IF_TYPE_VIRTIO,
 
 	IF_TYPE_COUNT,			/* Number of interface types */
 };
Signature · 1 line
2.7.4
author
#36 lore raw

PATCH 13/27 virtio: Add block driver support

In reply to Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds virtio block device driver support.

---

 drivers/virtio/Kconfig      |   7 +++
 drivers/virtio/Makefile     |   1 +
 drivers/virtio/virtio_blk.c | 127 +++++++++++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_blk.h | 129 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 264 insertions(+)
 create mode 100644 drivers/virtio/virtio_blk.c
 create mode 100644 drivers/virtio/virtio_blk.h
drivers/virtio/Kconfig +7 -0
index ceea03a..01bd116 100644
@@ -25,4 +25,11 @@ config VIRTIO_NET
 	  This is the virtual net driver for virtio. It can be used with
 	  QEMU based targets.
 
+config VIRTIO_BLK
+	bool "virtio block driver"
+	depends on VIRTIO
+	help
+	  This is the virtual block driver for virtio. It can be used with
+	  QEMU based targets.
+
 endmenu
drivers/virtio/Makefile +1 -0
index b7764f1..5fe7428 100644
@@ -6,3 +6,4 @@
 obj-y += virtio-uclass.o virtio_ring.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
 obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
+obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o
drivers/virtio/virtio_blk.c new file +127 -0

127 lines not on this page · raw message

drivers/virtio/virtio_blk.h new file +129 -0

129 lines not on this page · raw message

Signature · 1 line
2.7.4
#37 lore raw

PATCH 13/27 virtio: Add block driver support

Quoted 8 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds virtio block device driver support.

---

Quoted 7 lines
 drivers/virtio/Kconfig      |   7 +++
 drivers/virtio/Makefile     |   1 +
 drivers/virtio/virtio_blk.c | 127 +++++++++++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_blk.h | 129 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 264 insertions(+)
 create mode 100644 drivers/virtio/virtio_blk.c
 create mode 100644 drivers/virtio/virtio_blk.h

Why does this use __u32 instead of u32?

#38 lore raw

PATCH 13/27 virtio: Add block driver support

Hi Simon,

Quoted 8 lines · Simon Glass
Quoted 7 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

This adds virtio block device driver support.

---

...

Quoted 3 lines

Why does this use __u32 instead of u32?

The header comes from include/uapi from Linux where the underscored forms are the convention/rule.

author
#39 lore raw

PATCH 14/27 virtio: cmd: Add virtio command for virtio block devices

In reply to Bin Meng

From: Tuomas Tynkkynen <[email protected]>

Add 'virtio' command in U-Boot command line.

---

 cmd/Kconfig  |  7 +++++++
 cmd/Makefile |  1 +
 cmd/virtio.c | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+)
 create mode 100644 cmd/virtio.c
cmd/Kconfig +7 -0
index 13d4c99..0bde692 100644
@@ -1044,6 +1044,13 @@ config CMD_USB_MASS_STORAGE
 	help
 	  USB mass storage support
 
+config CMD_VIRTIO
+	bool "virtio"
+	depends on VIRTIO_BLK
+	default y if VIRTIO_BLK
+	help
+	  VirtIO block device support
+
 config CMD_AXI
 	bool "axi"
 	depends on AXI
cmd/Makefile +1 -0
index a61fab6..5946e6b 100644
@@ -133,6 +133,7 @@ obj-$(CONFIG_CMD_UBI) += ubi.o
 obj-$(CONFIG_CMD_UBIFS) += ubifs.o
 obj-$(CONFIG_CMD_UNIVERSE) += universe.o
 obj-$(CONFIG_CMD_UNZIP) += unzip.o
+obj-$(CONFIG_CMD_VIRTIO) += virtio.o
 obj-$(CONFIG_CMD_LZMADEC) += lzmadec.o
 
 obj-$(CONFIG_CMD_USB) += usb.o disk.o
cmd/virtio.c new file +37 -0
new file mode 100644
index 0000000..7f83693
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2018, Tuomas Tynkkynen <[email protected]>
+ * Copyright (C) 2018, Bin Meng <[email protected]>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <virtio.h>
+
+static int virtio_curr_dev;
+
+static int do_virtio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	if (argc == 2 && !strcmp(argv[1], "scan")) {
+		/* make sure all virtio devices are enumerated */
+		virtio_init();
+
+		return CMD_RET_SUCCESS;
+	}
+
+	return blk_common_cmd(argc, argv, IF_TYPE_VIRTIO, &virtio_curr_dev);
+}
+
+U_BOOT_CMD(
+	virtio, 8, 1, do_virtio,
+	"virtio block devices sub-system",
+	"scan - initialize virtio bus\n"
+	"virtio info - show all available virtio block devices\n"
+	"virtio device [dev] - show or set current virtio block device\n"
+	"virtio part [dev] - print partition table of one or all virtio block devices\n"
+	"virtio read addr blk# cnt - read `cnt' blocks starting at block\n"
+	"     `blk#' to memory address `addr'\n"
+	"virtio write addr blk# cnt - write `cnt' blocks starting at block\n"
+	"     `blk#' from memory address `addr'"
+);
Signature · 1 line
2.7.4
#40 lore raw

PATCH 14/27 virtio: cmd: Add virtio command for virtio block devices

Quoted 6 lines · Bin Meng

From: Tuomas Tynkkynen <[email protected]>

Add 'virtio' command in U-Boot command line.

Quoted 7 lines

---

 cmd/Kconfig  |  7 +++++++
 cmd/Makefile |  1 +
 cmd/virtio.c | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+)
 create mode 100644 cmd/virtio.c
author
#41 lore raw

PATCH 15/27 arm: qemu: Add a Kconfig in the board directory

In reply to Bin Meng

This adds a Kconfig file in the board directory, so that some board-specific options can be specified there.

---

 arch/arm/Kconfig                 | 1 +
 board/emulation/qemu-arm/Kconfig | 9 +++++++++
 configs/qemu_arm64_defconfig     | 1 -
 configs/qemu_arm_defconfig       | 1 -
 4 files changed, 10 insertions(+), 2 deletions(-)
 create mode 100644 board/emulation/qemu-arm/Kconfig
arch/arm/Kconfig +1 -0
index 0f8dd32..f0917cc 100644
@@ -1472,6 +1472,7 @@ source "board/broadcom/bcmns2/Kconfig"
 source "board/cavium/thunderx/Kconfig"
 source "board/cirrus/edb93xx/Kconfig"
 source "board/eets/pdu001/Kconfig"
+source "board/emulation/qemu-arm/Kconfig"
 source "board/freescale/ls2080a/Kconfig"
 source "board/freescale/ls2080aqds/Kconfig"
 source "board/freescale/ls2080ardb/Kconfig"
board/emulation/qemu-arm/Kconfig new file +9 -0
new file mode 100644
index 0000000..d1c08c2
@@ -0,0 +1,9 @@
+if TARGET_QEMU_ARM_32BIT || TARGET_QEMU_ARM_64BIT
+
+config SYS_TEXT_BASE
+	default 0x00000000
+
+config BOARD_SPECIFIC_OPTIONS # dummy
+	def_bool y
+
+endif
configs/qemu_arm64_defconfig +0 -1
index 2df35a8..131ca19 100644
@@ -1,7 +1,6 @@
 CONFIG_ARM=y
 CONFIG_ARM_SMCCC=y
 CONFIG_ARCH_QEMU=y
-CONFIG_SYS_TEXT_BASE=0x00000000
 CONFIG_TARGET_QEMU_ARM_64BIT=y
 CONFIG_AHCI=y
 CONFIG_DISTRO_DEFAULTS=y
configs/qemu_arm_defconfig +0 -1
index 2865599..3c139fe 100644
@@ -1,7 +1,6 @@
 CONFIG_ARM=y
 CONFIG_ARM_SMCCC=y
 CONFIG_ARCH_QEMU=y
-CONFIG_SYS_TEXT_BASE=0x00000000
 CONFIG_TARGET_QEMU_ARM_32BIT=y
 CONFIG_AHCI=y
 CONFIG_DISTRO_DEFAULTS=y
Signature · 1 line
2.7.4
#42 lore raw

PATCH 15/27 arm: qemu: Add a Kconfig in the board directory

Quoted 4 lines · Bin Meng

This adds a Kconfig file in the board directory, so that some board-specific options can be specified there.

Quoted 8 lines

---

 arch/arm/Kconfig                 | 1 +
 board/emulation/qemu-arm/Kconfig | 9 +++++++++
 configs/qemu_arm64_defconfig     | 1 -
 configs/qemu_arm_defconfig       | 1 -
 4 files changed, 10 insertions(+), 2 deletions(-)
 create mode 100644 board/emulation/qemu-arm/Kconfig
author
#43 lore raw

PATCH 16/27 arm: qemu: Enumerate virtio bus during early boot

In reply to Bin Meng

Currently devices on the virtio bus is not automatically enumerated, which means peripherals on the virtio bus are not discovered by their drivers. This uses board_init() to do the virtio enumeration.

---

 board/emulation/qemu-arm/Kconfig    | 3 +++
 board/emulation/qemu-arm/qemu-arm.c | 7 +++++++
 2 files changed, 10 insertions(+)
board/emulation/qemu-arm/Kconfig +3 -0
index d1c08c2..16b80fe 100644
@@ -5,5 +5,8 @@ config SYS_TEXT_BASE
 
 config BOARD_SPECIFIC_OPTIONS # dummy
 	def_bool y
+	imply VIRTIO_MMIO
+	imply VIRTIO_NET
+	imply VIRTIO_BLK
 
 endif
board/emulation/qemu-arm/qemu-arm.c +7 -0
index 812c906..428498e 100644
@@ -4,6 +4,7 @@
  */
 #include <common.h>
 #include <fdtdec.h>
+#include <virtio.h>
 
 #ifdef CONFIG_ARM64
 #include <asm/armv8/mmu.h>
@@ -58,6 +59,12 @@ struct mm_region *mem_map = qemu_arm64_mem_map;
 
 int board_init(void)
 {
+	/*
+	 * Make sure virtio bus is enumerated so that peripherals
+	 * on the virtio bus can be discovered by their drivers
+	 */
+	virtio_init();
+
 	return 0;
 }
 
Signature · 1 line
2.7.4
#44 lore raw

PATCH 16/27 arm: qemu: Enumerate virtio bus during early boot

Quoted 11 lines · Bin Meng

Currently devices on the virtio bus is not automatically enumerated, which means peripherals on the virtio bus are not discovered by their drivers. This uses board_init() to do the virtio enumeration.

---

 board/emulation/qemu-arm/Kconfig    | 3 +++
 board/emulation/qemu-arm/qemu-arm.c | 7 +++++++
 2 files changed, 10 insertions(+)

But I wonder if we should have a flag in the uclass or perhaps device tree, to indicate that all devices in it should be probed at start-up? Could be useful for PCI too.

Quoted 41 lines
board/emulation/qemu-arm/Kconfig +3 -0
index d1c08c2..16b80fe 100644
@@ -5,5 +5,8 @@ config SYS_TEXT_BASE
 
 config BOARD_SPECIFIC_OPTIONS # dummy
        def_bool y
+       imply VIRTIO_MMIO
+       imply VIRTIO_NET
+       imply VIRTIO_BLK
 
 endif
board/emulation/qemu-arm/qemu-arm.c +7 -1
index 812c906..428498e 100644
@@ -4,6 +4,7 @@
  */
 #include <common.h>
 #include <fdtdec.h>
+#include <virtio.h>
 
 #ifdef CONFIG_ARM64
 #include <asm/armv8/mmu.h>
@@ -58,6 +59,12 @@ struct mm_region *mem_map = qemu_arm64_mem_map;
 
 int board_init(void)
 {
+       /*
+        * Make sure virtio bus is enumerated so that peripherals
+        * on the virtio bus can be discovered by their drivers
+        */
+       virtio_init();
+
        return 0;
 }
 
--

2.7.4

author
#45 lore raw

PATCH 16/27 arm: qemu: Enumerate virtio bus during early boot

Hi Simon,

Quoted 14 lines · Simon Glass
Quoted 11 lines · Bin Meng

Currently devices on the virtio bus is not automatically enumerated, which means peripherals on the virtio bus are not discovered by their drivers. This uses board_init() to do the virtio enumeration.

---

 board/emulation/qemu-arm/Kconfig    | 3 +++
 board/emulation/qemu-arm/qemu-arm.c | 7 +++++++
 2 files changed, 10 insertions(+)
Quoted 6 lines

But I wonder if we should have a flag in the uclass or perhaps device tree, to indicate that all devices in it should be probed at start-up? Could be useful for PCI too.

I think that's a good idea. I will leave this for now and when we have that capability we can revisit.

[snip]

Regards,
Bin

#46 lore raw

PATCH 16/27 arm: qemu: Enumerate virtio bus during early boot

In reply to Bin Meng

Hi Bin,

Quoted 11 lines · Bin Meng

Currently devices on the virtio bus is not automatically enumerated, which means peripherals on the virtio bus are not discovered by their drivers. This uses board_init() to do the virtio enumeration.

---

  board/emulation/qemu-arm/Kconfig    | 3 +++
  board/emulation/qemu-arm/qemu-arm.c | 7 +++++++
  2 files changed, 10 insertions(+)

...

board/emulation/qemu-arm/Kconfig config SYS_TEXT_BASE · line 10

Quoted 11 lines
board/emulation/qemu-arm/Kconfig +0 -0
index d1c08c2..16b80fe 100644
@@ -5,5 +5,8 @@ config SYS_TEXT_BASE
 

config BOARD_SPECIFIC_OPTIONS # dummy
def_bool y

board/emulation/qemu-arm/Kconfig +3 -0
+	imply VIRTIO_MMIO
+	imply VIRTIO_NET
+	imply VIRTIO_BLK

I think we should have VIRTIO_PCI here as well, so that QEMU command lines that work on x86 will work on ARM just as well.

...

Quoted 14 lines
board/emulation/qemu-arm/qemu-arm.c +1 -0
@@ -4,6 +4,7 @@
   */
  #include <common.h>
  #include <fdtdec.h>
+#include <virtio.h>
 
  #ifdef CONFIG_ARM64
  #include <asm/armv8/mmu.h>
@@ -58,6 +59,12 @@ struct mm_region *mem_map = qemu_arm64_mem_map;
 
  int board_init(void)

board/emulation/qemu-arm/qemu-arm.c struct mm_region *mem_map = qemu_arm64_mem_map; · line 70

Quoted 10 lines
board/emulation/qemu-arm/qemu-arm.c +6 -0
  {
+	/*
+	 * Make sure virtio bus is enumerated so that peripherals
+	 * on the virtio bus can be discovered by their drivers
+	 */
+	virtio_init();
+
  	return 0;
  }
 

Note that in include/configs/qemu-arm.h, the boards auto-enumerate PCI devices with a different mechanism, like this:

#define CONFIG_PREBOOT "pci enum"

I am not sure which one is better but at least we should be consistent.

author
#47 lore raw

PATCH 16/27 arm: qemu: Enumerate virtio bus during early boot

Hi Tuomas,

Quoted 22 lines · Tuomas Tynkkynen

Hi Bin,

Quoted 11 lines · Bin Meng

Currently devices on the virtio bus is not automatically enumerated, which means peripherals on the virtio bus are not discovered by their drivers. This uses board_init() to do the virtio enumeration.

---

  board/emulation/qemu-arm/Kconfig    | 3 +++
  board/emulation/qemu-arm/qemu-arm.c | 7 +++++++
  2 files changed, 10 insertions(+)

...

Quoted 6 lines
board/emulation/qemu-arm/Kconfig +0 -0
index d1c08c2..16b80fe 100644
@@ -5,5 +5,8 @@ config SYS_TEXT_BASE
 
Quoted 9 lines

board/emulation/qemu-arm/Kconfig config SYS_TEXT_BASE · line 10

Quoted 5 lines

config BOARD_SPECIFIC_OPTIONS # dummy
def_bool y

board/emulation/qemu-arm/Kconfig +3 -0
+      imply VIRTIO_MMIO
+      imply VIRTIO_NET
+      imply VIRTIO_BLK

I think we should have VIRTIO_PCI here as well, so that QEMU command lines that work on x86 will work on ARM just as well.

Will do in v2.

Quoted 29 lines

...

Quoted 14 lines
board/emulation/qemu-arm/qemu-arm.c +1 -0
@@ -4,6 +4,7 @@
   */
  #include <common.h>
  #include <fdtdec.h>
+#include <virtio.h>
 
  #ifdef CONFIG_ARM64
  #include <asm/armv8/mmu.h>
@@ -58,6 +59,12 @@ struct mm_region *mem_map = qemu_arm64_mem_map;
 
  int board_init(void)

board/emulation/qemu-arm/qemu-arm.c struct mm_region *mem_map = qemu_arm64_mem_map; · line 70

Quoted 10 lines
board/emulation/qemu-arm/qemu-arm.c +6 -0
  {
+     /*
+      * Make sure virtio bus is enumerated so that peripherals
+      * on the virtio bus can be discovered by their drivers
+      */
+     virtio_init();
+
      return 0;
  }
 

Note that in include/configs/qemu-arm.h, the boards auto-enumerate PCI devices with a different mechanism, like this:

Quoted 3 lines

#define CONFIG_PREBOOT "pci enum"

I am not sure which one is better but at least we should be consistent.

Ah yes, but I felt the same as you about which one is better :). I think using PREBOOT is a little bit late, for example if we call
"virtio scan" here, we still see "Net: No ethernet found." which may
confuse people. Probably what Simon suggested (have a flag to indicate that all devices in it should be probed at start-up) is an ideal solution ...

Regards,
Bin

author
#48 lore raw

PATCH 17/27 riscv: qemu: Enumerate virtio bus during early boot

In reply to Bin Meng

Currently devices on the virtio bus is not automatically enumerated, which means peripherals on the virtio bus are not discovered by their drivers. This uses board_init() to do the virtio enumeration.

---

 board/emulation/qemu-riscv/Kconfig      | 3 +++
 board/emulation/qemu-riscv/qemu-riscv.c | 7 +++++++
 2 files changed, 10 insertions(+)
board/emulation/qemu-riscv/Kconfig +3 -0
index af23363..5ae56da 100644
@@ -18,5 +18,8 @@ config SYS_TEXT_BASE
 config BOARD_SPECIFIC_OPTIONS # dummy
 	def_bool y
 	imply SYS_NS16550
+	imply VIRTIO_MMIO
+	imply VIRTIO_NET
+	imply VIRTIO_BLK
 
 endif
board/emulation/qemu-riscv/qemu-riscv.c +7 -0
index 041e716..5ab0a62 100644
@@ -5,11 +5,18 @@
 
 #include <common.h>
 #include <fdtdec.h>
+#include <virtio.h>
 
 #define MROM_FDT_ADDR	0x1020
 
 int board_init(void)
 {
+	/*
+	 * Make sure virtio bus is enumerated so that peripherals
+	 * on the virtio bus can be discovered by their drivers
+	 */
+	virtio_init();
+
 	return 0;
 }
 
Signature · 1 line
2.7.4
#49 lore raw

PATCH 17/27 riscv: qemu: Enumerate virtio bus during early boot

Quoted 10 lines · Bin Meng

Currently devices on the virtio bus is not automatically enumerated, which means peripherals on the virtio bus are not discovered by their drivers. This uses board_init() to do the virtio enumeration.

---

 board/emulation/qemu-riscv/Kconfig      | 3 +++
 board/emulation/qemu-riscv/qemu-riscv.c | 7 +++++++
 2 files changed, 10 insertions(+)
author
#50 lore raw

PATCH 18/27 riscv: qemu: Include some useful commands

In reply to Bin Meng

With the virtio net and blk drivers, we can do more stuff with some useful commands. Imply those in the board Kconfig.

---

 board/emulation/qemu-riscv/Kconfig | 8 ++++++++
 1 file changed, 8 insertions(+)
board/emulation/qemu-riscv/Kconfig +8 -0
index 5ae56da..37a80db 100644
@@ -21,5 +21,13 @@ config BOARD_SPECIFIC_OPTIONS # dummy
 	imply VIRTIO_MMIO
 	imply VIRTIO_NET
 	imply VIRTIO_BLK
+	imply CMD_PING
+	imply CMD_FS_GENERIC
+	imply DOS_PARTITION
+	imply EFI_PARTITION
+	imply ISO_PARTITION
+	imply CMD_EXT2
+	imply CMD_EXT4
+	imply CMD_FAT
 
 endif
Signature · 1 line
2.7.4
#51 lore raw

PATCH 18/27 riscv: qemu: Include some useful commands

Quoted 8 lines · Bin Meng

With the virtio net and blk drivers, we can do more stuff with some useful commands. Imply those in the board Kconfig.

---

 board/emulation/qemu-riscv/Kconfig | 8 ++++++++
 1 file changed, 8 insertions(+)
#52 lore raw

PATCH 18/27 riscv: qemu: Include some useful commands

In reply to Bin Meng

Hi Bin,

Quoted 18 lines · Bin Meng

With the virtio net and blk drivers, we can do more stuff with some useful commands. Imply those in the board Kconfig.

---

  board/emulation/qemu-riscv/Kconfig | 8 ++++++++
  1 file changed, 8 insertions(+)
board/emulation/qemu-riscv/Kconfig +0 -0
index 5ae56da..37a80db 100644
@@ -21,5 +21,13 @@ config BOARD_SPECIFIC_OPTIONS # dummy
  	imply VIRTIO_MMIO
  	imply VIRTIO_NET
  	imply VIRTIO_BLK

board/emulation/qemu-riscv/Kconfig config BOARD_SPECIFIC_OPTIONS # dummy · line 33

Quoted 10 lines
board/emulation/qemu-riscv/Kconfig +8 -0
+	imply CMD_PING
+	imply CMD_FS_GENERIC
+	imply DOS_PARTITION
+	imply EFI_PARTITION
+	imply ISO_PARTITION
+	imply CMD_EXT2
+	imply CMD_EXT4
+	imply CMD_FAT
 
  endif

How about using DISTRO_DEFAULTS?

author
#53 lore raw

PATCH 18/27 riscv: qemu: Include some useful commands

Hi Tuomas,

Quoted 30 lines · Tuomas Tynkkynen

Hi Bin,

Quoted 26 lines · Bin Meng

With the virtio net and blk drivers, we can do more stuff with some useful commands. Imply those in the board Kconfig.

---

  board/emulation/qemu-riscv/Kconfig | 8 ++++++++
  1 file changed, 8 insertions(+)
board/emulation/qemu-riscv/Kconfig +8 -0
index 5ae56da..37a80db 100644
@@ -21,5 +21,13 @@ config BOARD_SPECIFIC_OPTIONS # dummy
      imply VIRTIO_MMIO
      imply VIRTIO_NET
      imply VIRTIO_BLK
+     imply CMD_PING
+     imply CMD_FS_GENERIC
+     imply DOS_PARTITION
+     imply EFI_PARTITION
+     imply ISO_PARTITION
+     imply CMD_EXT2
+     imply CMD_EXT4
+     imply CMD_FAT
 
Quoted 4 lines

board/emulation/qemu-riscv/Kconfig config BOARD_SPECIFIC_OPTIONS # dummy · line 33

Quoted 2 lines
board/emulation/qemu-riscv/Kconfig +0 -0
  endif

How about using DISTRO_DEFAULTS?

At present U-Boot on RISC-V cannot boot Linux directly, so I am a little bit concerned turning on DISTRO_DEFAULTS may confuse people. We may revisit this when directly Linux booting is supported.

Regards,
Bin

author
#54 lore raw

PATCH 19/27 kconfig: Introduce HAVE_ARCH_IOMAP

In reply to Bin Meng

Introduce a new Kconfig option for architecture codes to control whether it provides io{read,write}{8,16,32} I/O accessor functions.

---

 include/linux/io.h | 4 ++++
 lib/Kconfig        | 6 ++++++
 2 files changed, 10 insertions(+)
include/linux/io.h +4 -0
index d1b3efe..9badab4 100644
@@ -7,6 +7,7 @@
 #include <linux/types.h>
 #include <asm/io.h>
 
+#ifndef CONFIG_HAVE_ARCH_IOMAP
 static inline u8 ioread8(const volatile void __iomem *addr)
 {
 	return readb(addr);
@@ -21,6 +22,7 @@ static inline u32 ioread32(const volatile void __iomem *addr)
 {
 	return readl(addr);
 }
+#endif /* !CONFIG_HAVE_ARCH_IOMAP */
 
 #ifdef CONFIG_64BIT
 static inline u64 ioread64(const volatile void __iomem *addr)
@@ -29,6 +31,7 @@ static inline u64 ioread64(const volatile void __iomem *addr)
 }
 #endif /* CONFIG_64BIT */
 
+#ifndef CONFIG_HAVE_ARCH_IOMAP
 static inline void iowrite8(u8 value, volatile void __iomem *addr)
 {
 	writeb(value, addr);
@@ -43,6 +46,7 @@ static inline void iowrite32(u32 value, volatile void __iomem *addr)
 {
 	writel(value, addr);
 }
+#endif /* !CONFIG_HAVE_ARCH_IOMAP */
 
 #ifdef CONFIG_64BIT
 static inline void iowrite64(u64 value, volatile void __iomem *addr)
lib/Kconfig +6 -0
index 622f3c2..64c3825 100644
@@ -21,6 +21,12 @@ config DYNAMIC_CRC_TABLE
 	  Enable this option to calculate entries for CRC tables at runtime.
 	  This can be helpful when reducing the size of the build image
 
+config HAVE_ARCH_IOMAP
+	bool
+	help
+	  Enable this option if architecture provides io{read,write}{8,16,32}
+	  I/O accessor functions.
+
 config HAVE_PRIVATE_LIBGCC
 	bool
 
Signature · 1 line
2.7.4
#55 lore raw

PATCH 19/27 kconfig: Introduce HAVE_ARCH_IOMAP

Quoted 9 lines · Bin Meng

Introduce a new Kconfig option for architecture codes to control whether it provides io{read,write}{8,16,32} I/O accessor functions.

---

 include/linux/io.h | 4 ++++
 lib/Kconfig        | 6 ++++++
 2 files changed, 10 insertions(+)
author
#56 lore raw

PATCH 20/27 x86: Implement arch-specific io accessor routines

In reply to Bin Meng

At present the generic io{read,write}{8,16,32} routines only support MMIO access. With architecture like x86 that has a separate IO space, these routines cannot be used to access I/O ports.

Implement x86-specific version to support both PIO and MMIO access, so that drivers for multiple architectures can use these accessors without the need to know whether it's MMIO or PIO.

These are ported from Linux kernel lib/iomap.c, with slight changes.

---

 arch/Kconfig              |  1 +
 arch/x86/include/asm/io.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
arch/Kconfig +1 -0
index ce183fa..178eb94 100644
@@ -114,6 +114,7 @@ config X86
 	select CREATE_ARCH_SYMLINK
 	select DM
 	select DM_PCI
+	select HAVE_ARCH_IOMAP
 	select HAVE_PRIVATE_LIBGCC
 	select OF_CONTROL
 	select PCI
arch/x86/include/asm/io.h +66 -0

72 lines not on this page · raw message

Signature · 1 line
2.7.4
#57 lore raw

PATCH 20/27 x86: Implement arch-specific io accessor routines

Quoted 8 lines · Bin Meng

At present the generic io{read,write}{8,16,32} routines only support MMIO access. With architecture like x86 that has a separate IO space, these routines cannot be used to access I/O ports.

Implement x86-specific version to support both PIO and MMIO access, so that drivers for multiple architectures can use these accessors without the need to know whether it's MMIO or PIO.

Quoted 8 lines

These are ported from Linux kernel lib/iomap.c, with slight changes.

---

 arch/Kconfig              |  1 +
 arch/x86/include/asm/io.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

Should we use regmap instead?

author
#58 lore raw

PATCH 20/27 x86: Implement arch-specific io accessor routines

Hi Simon,

Quoted 15 lines · Simon Glass
Quoted 13 lines · Bin Meng

At present the generic io{read,write}{8,16,32} routines only support MMIO access. With architecture like x86 that has a separate IO space, these routines cannot be used to access I/O ports.

Implement x86-specific version to support both PIO and MMIO access, so that drivers for multiple architectures can use these accessors without the need to know whether it's MMIO or PIO.

These are ported from Linux kernel lib/iomap.c, with slight changes.

---

Quoted 7 lines
Quoted 3 lines
 arch/Kconfig              |  1 +
 arch/x86/include/asm/io.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

Should we use regmap instead?

No, regmap is for something else, e.g. the same driver to be used on different buses.

Regards,
Bin

author
#59 lore raw

PATCH 21/27 virtio: Add virtio over pci transport driver

In reply to Bin Meng

This adds a transport driver that implements UCLASS_VIRTIO for virtio over pci, which is commonly used on x86.

It only supports the legacy interface of the pci transport, which is the default device that QEMU emulates.

---

 drivers/virtio/Kconfig      |   8 +
 drivers/virtio/Makefile     |   1 +
 drivers/virtio/virtio_pci.c | 420 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_pci.h | 173 ++++++++++++++++++
 4 files changed, 602 insertions(+)
 create mode 100644 drivers/virtio/virtio_pci.c
 create mode 100644 drivers/virtio/virtio_pci.h
drivers/virtio/Kconfig +8 -0
index 01bd116..6db2d73 100644
@@ -18,6 +18,14 @@ config VIRTIO_MMIO
 	  This driver provides support for memory mapped virtio
 	  platform device driver.
 
+config VIRTIO_PCI
+	bool "PCI driver for virtio devices"
+	depends on DM_PCI
+	select VIRTIO
+	help
+	  This driver provides support for virtio based paravirtual device
+	  drivers over PCI.
+
 config VIRTIO_NET
 	bool "virtio net driver"
 	depends on VIRTIO
drivers/virtio/Makefile +1 -0
index 5fe7428..5ee6183 100644
@@ -5,5 +5,6 @@
 
 obj-y += virtio-uclass.o virtio_ring.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
+obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
 obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
 obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o
drivers/virtio/virtio_pci.c new file +420 -0

420 lines not on this page · raw message

drivers/virtio/virtio_pci.h new file +173 -0

173 lines not on this page · raw message

Signature · 1 line
2.7.4
#60 lore raw

PATCH 21/27 virtio: Add virtio over pci transport driver

Hi Bin,

Quoted 9 lines · Bin Meng

This adds a transport driver that implements UCLASS_VIRTIO for virtio over pci, which is commonly used on x86.

It only supports the legacy interface of the pci transport, which is the default device that QEMU emulates.

---

Quoted 7 lines
 drivers/virtio/Kconfig      |   8 +
 drivers/virtio/Makefile     |   1 +
 drivers/virtio/virtio_pci.c | 420 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_pci.h | 173 ++++++++++++++++++
 4 files changed, 602 insertions(+)
 create mode 100644 drivers/virtio/virtio_pci.c
 create mode 100644 drivers/virtio/virtio_pci.h

vring_create_virtqueue() should return an error I think - you assume -ENOMEM

That is a huge table of PCI devices. Can you use a class instead?

Regards,
Simon

author
#61 lore raw

PATCH 21/27 virtio: Add virtio over pci transport driver

Hi Simon,

Quoted 21 lines · Simon Glass

Hi Bin,

Quoted 9 lines · Bin Meng

This adds a transport driver that implements UCLASS_VIRTIO for virtio over pci, which is commonly used on x86.

It only supports the legacy interface of the pci transport, which is the default device that QEMU emulates.

---

Quoted 7 lines
 drivers/virtio/Kconfig      |   8 +
 drivers/virtio/Makefile     |   1 +
 drivers/virtio/virtio_pci.c | 420 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/virtio/virtio_pci.h | 173 ++++++++++++++++++
 4 files changed, 602 insertions(+)
 create mode 100644 drivers/virtio/virtio_pci.c
 create mode 100644 drivers/virtio/virtio_pci.h
Quoted 4 lines

vring_create_virtqueue() should return an error I think - you assume -ENOMEM

It returns a NULL pointer and the error is -ENOMEM, which will be checked by the caller via IS_ERR()

Quoted 1 line

That is a huge table of PCI devices. Can you use a class instead?

Unfortunately we can't. The virtio spec does not define a dedicated class.

Regards,
Bin

author
#62 lore raw

PATCH 22/27 x86: qemu: Imply virtio PCI transport and device drivers

In reply to Bin Meng

Add virtio drivers for QEMU x86 targets.

---

 board/emulation/qemu-x86/Kconfig | 3 +++
 1 file changed, 3 insertions(+)
board/emulation/qemu-x86/Kconfig +3 -0
index 41a27dd..6d19299 100644
@@ -21,5 +21,8 @@ config BOARD_SPECIFIC_OPTIONS # dummy
 	select X86_RESET_VECTOR
 	select QEMU
 	select BOARD_ROMSIZE_KB_1024
+	imply VIRTIO_PCI
+	imply VIRTIO_NET
+	imply VIRTIO_BLK
 
 endif
Signature · 1 line
2.7.4
author
#64 lore raw

PATCH 23/27 dm: pci: Add APIs to find next capability and extended capability

In reply to Bin Meng

This introduces two new APIs dm_pci_find_next_capability() and dm_pci_find_next_ext_capability() to get PCI capability address and PCI express extended capability address for a given PCI device starting from a given offset.

---

 drivers/pci/pci-uclass.c | 48 ++++++++++++++++++++++++++++++++----------------
 include/pci.h            | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 16 deletions(-)
drivers/pci/pci-uclass.c +32 -16
index eb118f3..a100e16 100644
@@ -1318,26 +1318,13 @@ void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
 	return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
 }
 
-int dm_pci_find_capability(struct udevice *dev, int cap)
+int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
 {
-	u16 status;
-	u8 header_type;
 	int ttl = PCI_FIND_CAP_TTL;
+	u8 pos = start;
 	u8 id;
 	u16 ent;
-	u8 pos;
 
-	dm_pci_read_config16(dev, PCI_STATUS, &status);
-	if (!(status & PCI_STATUS_CAP_LIST))
-		return 0;
-
-	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
-	if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
-		pos = PCI_CB_CAPABILITY_LIST;
-	else
-		pos = PCI_CAPABILITY_LIST;
-
-	dm_pci_read_config8(dev, pos, &pos);
 	while (ttl--) {
 		if (pos < PCI_STD_HEADER_SIZEOF)
 			break;
@@ -1355,7 +1342,28 @@ int dm_pci_find_capability(struct udevice *dev, int cap)
 	return 0;
 }
 
-int dm_pci_find_ext_capability(struct udevice *dev, int cap)
+int dm_pci_find_capability(struct udevice *dev, int cap)
+{
+	u16 status;
+	u8 header_type;
+	u8 pos;
+
+	dm_pci_read_config16(dev, PCI_STATUS, &status);
+	if (!(status & PCI_STATUS_CAP_LIST))
+		return 0;
+
+	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
+	if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
+		pos = PCI_CB_CAPABILITY_LIST;
+	else
+		pos = PCI_CAPABILITY_LIST;
+
+	dm_pci_read_config8(dev, pos, &pos);
+
+	return dm_pci_find_next_capability(dev, pos, cap);
+}
+
+int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
 {
 	u32 header;
 	int ttl;
@@ -1364,6 +1372,9 @@ int dm_pci_find_ext_capability(struct udevice *dev, int cap)
 	/* minimum 8 bytes per capability */
 	ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
 
+	if (start)
+		pos = start;
+
 	dm_pci_read_config32(dev, pos, &header);
 	/*
 	 * If we have no capabilities, this is indicated by cap ID,
@@ -1386,6 +1397,11 @@ int dm_pci_find_ext_capability(struct udevice *dev, int cap)
 	return 0;
 }
 
+int dm_pci_find_ext_capability(struct udevice *dev, int cap)
+{
+	return dm_pci_find_next_ext_capability(dev, 0, cap);
+}
+
 UCLASS_DRIVER(pci) = {
 	.id		= UCLASS_PCI,
 	.name		= "pci",
include/pci.h +48 -0

60 lines not on this page · raw message

Signature · 1 line
2.7.4
#65 lore raw

PATCH 23/27 dm: pci: Add APIs to find next capability and extended capability

Quoted 11 lines · Bin Meng

This introduces two new APIs dm_pci_find_next_capability() and dm_pci_find_next_ext_capability() to get PCI capability address and PCI express extended capability address for a given PCI device starting from a given offset.

---

 drivers/pci/pci-uclass.c | 48 ++++++++++++++++++++++++++++++++----------------
 include/pci.h            | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 16 deletions(-)

Are these called somewhere from sandbox?...ah yes, next patch

author
#66 lore raw

PATCH 24/27 test: dm: pci: Add cases for finding next PCI capability APIs

In reply to Bin Meng

Add test cases to cover the two newly added PCI APIs:
dm_pci_find_next_capability() & dm_pci_find_next_ext_capability().

---

 test/dm/pci.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
test/dm/pci.c +20 -0
index 8699700..f645b8b 100644
@@ -206,6 +206,16 @@ static int dm_test_pci_cap(struct unit_test_state *uts)
 	cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX);
 	ut_asserteq(0, cap);
 
+	/* look up PCI_CAP_ID_MSIX starting from PCI_CAP_ID_PM_OFFSET */
+	cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_PM_OFFSET,
+					  PCI_CAP_ID_MSIX);
+	ut_asserteq(PCI_CAP_ID_MSIX_OFFSET, cap);
+
+	/* look up PCI_CAP_ID_VNDR starting from PCI_CAP_ID_EXP_OFFSET */
+	cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_EXP_OFFSET,
+					  PCI_CAP_ID_VNDR);
+	ut_asserteq(0, cap);
+
 	ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus));
 	ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap));
 
@@ -217,6 +227,16 @@ static int dm_test_pci_cap(struct unit_test_state *uts)
 	cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV);
 	ut_asserteq(0, cap);
 
+	/* look up PCI_EXT_CAP_ID_DSN starting from PCI_EXT_CAP_ID_ERR_OFFSET */
+	cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_ERR_OFFSET,
+					      PCI_EXT_CAP_ID_DSN);
+	ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap);
+
+	/* look up PCI_EXT_CAP_ID_RCRB starting from PCI_EXT_CAP_ID_VC_OFFSET */
+	cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_VC_OFFSET,
+					      PCI_EXT_CAP_ID_RCRB);
+	ut_asserteq(0, cap);
+
 	return 0;
 }
 DM_TEST(dm_test_pci_cap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Signature · 1 line
2.7.4
#67 lore raw

PATCH 24/27 test: dm: pci: Add cases for finding next PCI capability APIs

Quoted 8 lines · Bin Meng

Add test cases to cover the two newly added PCI APIs:
dm_pci_find_next_capability() & dm_pci_find_next_ext_capability().

---

 test/dm/pci.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
author
#68 lore raw

PATCH 25/27 virtio: pci: Support non-legacy PCI transport device

In reply to Bin Meng

By default QEMU creates legacy PCI transport devices, but we can ask QEMU to create non-legacy one if we pass additional device property/value pairs in the command line:

-device virtio-blk-pci,disable-legacy=true,disable-modern=false

This adds a new driver driver to support non-legacy (modern) device mode. Previous driver/file name is changed accordingly.

---

 drivers/virtio/Makefile                            |   2 +-
 .../virtio/{virtio_pci.c => virtio_pci_legacy.c}   |   6 +-
 drivers/virtio/virtio_pci_modern.c                 | 612 +++++++++++++++++++++
 3 files changed, 616 insertions(+), 4 deletions(-)
 rename drivers/virtio/{virtio_pci.c => virtio_pci_legacy.c} (98%)
 create mode 100644 drivers/virtio/virtio_pci_modern.c
drivers/virtio/Makefile +1 -1
index 5ee6183..072fb56 100644
@@ -5,6 +5,6 @@
 
 obj-y += virtio-uclass.o virtio_ring.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
-obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
+obj-$(CONFIG_VIRTIO_PCI) += virtio_pci_legacy.o virtio_pci_modern.o
 obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
 obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o
drivers/virtio/virtio_pci_legacy.c renamed from drivers/virtio/virtio_pci.c +3 -3
similarity index 98%
rename from drivers/virtio/virtio_pci.c
rename to drivers/virtio/virtio_pci_legacy.c
index 12ecd84..ab40210 100644
@@ -15,7 +15,7 @@
 #include <linux/io.h>
 #include "virtio_pci.h"
 
-#define VIRTIO_PCI_DRV_NAME	"virtio-pci"
+#define VIRTIO_PCI_DRV_NAME	"virtio-pci.l"
 
 /* PCI device ID in the range 0x1000 to 0x103f */
 #define VIRTIO_PCI_VENDOR_ID	0x1af4
@@ -340,7 +340,7 @@ static const struct dm_virtio_ops virtio_pci_ops = {
 	.notify		= virtio_pci_notify,
 };
 
-U_BOOT_DRIVER(virtio_pci) = {
+U_BOOT_DRIVER(virtio_pci_legacy) = {
 	.name	= VIRTIO_PCI_DRV_NAME,
 	.id	= UCLASS_VIRTIO,
 	.ops	= &virtio_pci_ops,
@@ -417,4 +417,4 @@ static struct pci_device_id virtio_pci_supported[] = {
 	{},
 };
 
-U_BOOT_PCI_DEVICE(virtio_pci, virtio_pci_supported);
+U_BOOT_PCI_DEVICE(virtio_pci_legacy, virtio_pci_supported);
drivers/virtio/virtio_pci_modern.c new file +612 -0

612 lines not on this page · raw message

Signature · 1 line
2.7.4
#69 lore raw

PATCH 25/27 virtio: pci: Support non-legacy PCI transport device

Quoted 10 lines · Bin Meng

By default QEMU creates legacy PCI transport devices, but we can ask QEMU to create non-legacy one if we pass additional device property/value pairs in the command line:

-device virtio-blk-pci,disable-legacy=true,disable-modern=false

This adds a new driver driver to support non-legacy (modern) device mode. Previous driver/file name is changed accordingly.

Quoted 8 lines

---

 drivers/virtio/Makefile                            |   2 +-
 .../virtio/{virtio_pci.c => virtio_pci_legacy.c}   |   6 +-
 drivers/virtio/virtio_pci_modern.c                 | 612 +++++++++++++++++++++
 3 files changed, 616 insertions(+), 4 deletions(-)
 rename drivers/virtio/{virtio_pci.c => virtio_pci_legacy.c} (98%)
 create mode 100644 drivers/virtio/virtio_pci_modern.c

Isn't there some common code between the two?

author
#70 lore raw

PATCH 25/27 virtio: pci: Support non-legacy PCI transport device

Hi Simon,

Quoted 21 lines · Simon Glass
Quoted 10 lines · Bin Meng

By default QEMU creates legacy PCI transport devices, but we can ask QEMU to create non-legacy one if we pass additional device property/value pairs in the command line:

-device virtio-blk-pci,disable-legacy=true,disable-modern=false

This adds a new driver driver to support non-legacy (modern) device mode. Previous driver/file name is changed accordingly.

Quoted 8 lines

---

 drivers/virtio/Makefile                            |   2 +-
 .../virtio/{virtio_pci.c => virtio_pci_legacy.c}   |   6 +-
 drivers/virtio/virtio_pci_modern.c                 | 612 +++++++++++++++++++++
 3 files changed, 616 insertions(+), 4 deletions(-)
 rename drivers/virtio/{virtio_pci.c => virtio_pci_legacy.c} (98%)
 create mode 100644 drivers/virtio/virtio_pci_modern.c
Quoted 3 lines

Isn't there some common code between the two?

Unfortunately the legacy and modern PCI devices are quite different, so we have to use 2 drivers which is how Linux does. For the virtio-mmio driver, the difference are not that big so we can use one driver to support both.

Regards,
Bin

author
#71 lore raw

PATCH 26/27 virtio: net: Support non-legacy device

In reply to Bin Meng

For v1.0 compliant device, it always assumes the member 'num_buffers' exists in the struct virtio_net_hdr while the legacy driver only presented 'num_buffers' when VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature the structure was 2 bytes shorter.

Update the driver to support the non-legacy device.

---

 drivers/virtio/virtio_net.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)
drivers/virtio/virtio_net.c +26 -5
index 1ab1513..080b0be 100644
@@ -31,6 +31,7 @@ struct virtio_net_priv {
 
 	char rx_buff[VIRTIO_NET_NUM_RX_BUFS][VIRTIO_NET_RX_BUF_SIZE];
 	bool rx_running;
+	int net_hdr_len;
 };
 
 /*
@@ -76,12 +77,19 @@ static int virtio_net_send(struct udevice *dev, void *packet, int length)
 {
 	struct virtio_net_priv *priv = dev_get_priv(dev);
 	struct virtio_net_hdr hdr;
-	struct virtio_sg hdr_sg = { &hdr, sizeof(hdr) };
+	struct virtio_net_hdr_v1 hdr_v1;
+	struct virtio_sg hdr_sg;
 	struct virtio_sg data_sg = { packet, length };
 	struct virtio_sg *sgs[] = { &hdr_sg, &data_sg };
 	int ret;
 
-	memset(&hdr, 0, sizeof(struct virtio_net_hdr));
+	if (priv->net_hdr_len == sizeof(struct virtio_net_hdr))
+		hdr_sg.addr = &hdr;
+	else
+		hdr_sg.addr = &hdr_v1;
+	hdr_sg.length = priv->net_hdr_len;
+
+	memset(hdr_sg.addr, 0, priv->net_hdr_len);
 
 	ret = virtqueue_add(priv->tx_vq, sgs, 2, 0);
 	if (ret)
@@ -107,14 +115,14 @@ static int virtio_net_recv(struct udevice *dev, int flags, uchar **packetp)
 	if (!buf)
 		return -EAGAIN;
 
-	*packetp = buf + sizeof(struct virtio_net_hdr);
-	return len - sizeof(struct virtio_net_hdr);
+	*packetp = buf + priv->net_hdr_len;
+	return len - priv->net_hdr_len;
 }
 
 static int virtio_net_free_pkt(struct udevice *dev, uchar *packet, int length)
 {
 	struct virtio_net_priv *priv = dev_get_priv(dev);
-	void *buf = packet - sizeof(struct virtio_net_hdr);
+	void *buf = packet - priv->net_hdr_len;
 	struct virtio_sg sg = { buf, VIRTIO_NET_RX_BUF_SIZE };
 	struct virtio_sg *sgs[] = { &sg };
 
@@ -185,12 +193,25 @@ static int virtio_net_bind(struct udevice *dev)
 static int virtio_net_probe(struct udevice *dev)
 {
 	struct virtio_net_priv *priv = dev_get_priv(dev);
+	struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
 	int ret;
 
 	ret = virtio_find_vqs(dev, 2, priv->vqs);
 	if (ret < 0)
 		return ret;
 
+	/*
+	 * For v1.0 compliant device, it always assumes the member
+	 * 'num_buffers' exists in the struct virtio_net_hdr while
+	 * the legacy driver only presented 'num_buffers' when
+	 * VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature
+	 * the structure was 2 bytes shorter.
+	 */
+	if (uc_priv->legacy)
+		priv->net_hdr_len = sizeof(struct virtio_net_hdr);
+	else
+		priv->net_hdr_len = sizeof(struct virtio_net_hdr_v1);
+
 	return 0;
 }
 
Signature · 1 line
2.7.4
#72 lore raw

PATCH 26/27 virtio: net: Support non-legacy device

Quoted 5 lines · Bin Meng

For v1.0 compliant device, it always assumes the member 'num_buffers' exists in the struct virtio_net_hdr while the legacy driver only presented 'num_buffers' when VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature the structure was 2 bytes shorter.

Quoted 7 lines

Update the driver to support the non-legacy device.

---

 drivers/virtio/virtio_net.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)
#73 lore raw

PATCH 26/27 virtio: net: Support non-legacy device

In reply to Bin Meng
Quoted 9 lines · Bin Meng

For v1.0 compliant device, it always assumes the member 'num_buffers' exists in the struct virtio_net_hdr while the legacy driver only presented 'num_buffers' when VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature the structure was 2 bytes shorter.

Update the driver to support the non-legacy device.

author
#74 lore raw

PATCH 27/27 doc: Document virtio support

In reply to Bin Meng

Add REAME.virtio to describe the information about U-Boot support for VirtIO devices, including supported boards, build instructions, driver details etc.

---

 doc/README.virtio | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 247 insertions(+)
 create mode 100644 doc/README.virtio
doc/README.virtio new file +247 -0
new file mode 100644
index 0000000..ad58fe8
@@ -0,0 +1,247 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2018, Bin Meng <[email protected]>
+
+VirtIO Support
+==============
+
+This document describes the information about U-Boot support for VirtIO [1]
+devices, including supported boards, build instructions, driver details etc.
+
+What's VirtIO?
+--------------
+VirtIO is a virtualization standard for network and disk device drivers where
+just the guest's device driver "knows" it is running in a virtual environment,
+and cooperates with the hypervisor. This enables guests to get high performance
+network and disk operations, and gives most of the performance benefits of
+paravirtualization. In the U-Boot case, the guest is U-Boot itself, while the
+virtual environment are normally QEMU [2] targets like ARM, RISC-V and x86.
+
+Status
+------
+VirtIO can use various different buses, aka transports as described in the
+spec. While VirtIO devices are commonly implemented as PCI devices on x86,
+embedded devices models like ARM/RISC-V, which does not normally come with
+PCI support might use simple memory mapped device (MMIO) instead of the PCI
+device. The memory mapped virtio device behaviour is based on the PCI device
+specification. Therefore most operations including device initialization,
+queues configuration and buffer transfers are nearly identical. Both MMIO
+and PCI transport options are supported in U-Boot.
+
+The VirtIO spec defines a lots of VirtIO device types, however at present only
+network and block device, the most two commonly used devices, are supported.
+
+The following QEMU targets are supported.
+
+  - qemu_arm_defconfig
+  - qemu_arm64_defconfig
+  - qemu-riscv32_defconfig
+  - qemu-riscv64_defconfig
+  - qemu-x86_defconfig
+  - qemu-x86_64_defconfig
+
+Note ARM and RISC-V targets are configured with VirtIO MMIO transport driver,
+and on x86 it's the PCI transport driver.
+
+Build Instructions
+------------------
+Building U-Boot for pre-configured QEMU targets is no different from others.
+For example, we can do the following with the CROSS_COMPILE environment
+variable being properly set to a working toolchain for ARM:
+
+  $ make qemu_arm_defconfig
+  $ make
+
+You can even create a QEMU ARM target with VirtIO devices showing up on both
+MMIO and PCI buses. In this case, you can enable the PCI transport driver
+from 'make menuconfig':
+
+Device Drivers  --->
+	...
+	VirtIO Drivers  --->
+		...
+		[*] PCI driver for virtio devices
+
+Other drivers are at the same location and can be tuned to suit the needs.
+
+Requirements
+------------
+It is required that QEMU v2.5.0+ should be used to test U-Boot VirtIO support
+on QEMU ARM and x86, and v2.12.0+ on QEMU RISC-V.
+
+Testing
+-------
+The following QEMU command line is used to get U-Boot up and running with
+VirtIO net and block devices on ARM.
+
+  $ qemu-system-arm -nographic -machine virt -bios u-boot.bin \
+    -netdev tap,ifname=tap0,id=net0 \
+    -device virtio-net-device,netdev=net0 \
+    -drive if=none,file=test.img,format=raw,id=hd0 \
+    -device virtio-blk-device,drive=hd0
+
+On x86, command is slightly different to create PCI VirtIO devices.
+
+  $ qemu-system-i386 -nographic -bios u-boot.rom \
+    -netdev tap,ifname=tap0,id=net0 \
+    -device virtio-net-pci,netdev=net0 \
+    -drive if=none,file=test.img,format=raw,id=hd0 \
+    -device virtio-blk-pci,drive=hd0
+
+Additional net and block devices can be created by appending more '-device'
+parameters. It is also possible to specify both MMIO and PCI VirtIO devices.
+For example, the following commnad creates 3 VirtIO devices, with 1 on MMIO
+and 2 on PCI bus.
+
+  $ qemu-system-arm -nographic -machine virt -bios u-boot.bin \
+    -netdev tap,ifname=tap0,id=net0 \
+    -device virtio-net-pci,netdev=net0 \
+    -drive if=none,file=test0.img,format=raw,id=hd0 \
+    -device virtio-blk-device,drive=hd0 \
+    -drive if=none,file=test1.img,format=raw,id=hd1 \
+    -device virtio-blk-pci,drive=hd1
+
+By default QEMU creates VirtIO legacy devices by default. To create non-legacy
+(aka modern) devices, pass additional device property/value pairs like below:
+
+  $ qemu-system-i386 -nographic -bios u-boot.rom \
+    -netdev tap,ifname=tap0,id=net0 \
+    -device virtio-net-pci,netdev=net0,disable-legacy=true,disable-modern=false \
+    -drive if=none,file=test.img,format=raw,id=hd0 \
+    -device virtio-blk-pci,drive=hd0,disable-legacy=true,disable-modern=false
+
+A 'virtio' command is provided in U-Boot shell.
+
+  => virtio
+  virtio - virtio block devices sub-system
+
+  Usage:
+  virtio scan - initialize virtio bus
+  virtio info - show all available virtio block devices
+  virtio device [dev] - show or set current virtio block device
+  virtio part [dev] - print partition table of one or all virtio block devices
+  virtio read addr blk# cnt - read `cnt' blocks starting at block
+       `blk#' to memory address `addr'
+  virtio write addr blk# cnt - write `cnt' blocks starting at block
+       `blk#' from memory address `addr'
+
+To probe all the VirtIO devices, type:
+
+  => virtio scan
+
+Then we can show the connected block device details by:
+
+  => virtio info
+  Device 0: QEMU VirtIO Block Device
+              Type: Hard Disk
+              Capacity: 4096.0 MB = 4.0 GB (8388608 x 512)
+
+And list the directories and files on the disk by:
+
+  => ls virtio 0 /
+  <DIR>       4096 .
+  <DIR>       4096 ..
+  <DIR>      16384 lost+found
+  <DIR>       4096 dev
+  <DIR>       4096 proc
+  <DIR>       4096 sys
+  <DIR>       4096 var
+  <DIR>       4096 etc
+  <DIR>       4096 usr
+  <SYM>          7 bin
+  <SYM>          8 sbin
+  <SYM>          7 lib
+  <SYM>          9 lib64
+  <DIR>       4096 run
+  <DIR>       4096 boot
+  <DIR>       4096 home
+  <DIR>       4096 media
+  <DIR>       4096 mnt
+  <DIR>       4096 opt
+  <DIR>       4096 root
+  <DIR>       4096 srv
+  <DIR>       4096 tmp
+                 0 .autorelabel
+
+Driver Internals
+----------------
+There are 3 level of drivers in the VirtIO driver family.
+
+	+---------------------------------------+
+	|	 virtio device drivers		|
+	|    +-------------+ +------------+	|
+	|    | virtio-net  | | virtio-blk |	|
+	|    +-------------+ +------------+	|
+	+---------------------------------------+
+	+---------------------------------------+
+	|	virtio transport drivers	|
+	|    +-------------+ +------------+	|
+	|    | virtio-mmio | | virtio-pci |	|
+	|    +-------------+ +------------+	|
+	+---------------------------------------+
+		+----------------------+
+		| virtio uclass driver |
+		+----------------------+
+
+The root one is the virtio uclass driver (virtio-uclass.c), which does lots of
+common stuff for the transport drivers (virtio_mmio.c, virtio_pci.c). The real
+virtio device is discovered in the transport driver's probe() method, and its
+device ID is saved in the virtio uclass's private data of the transport device.
+Then in the virtio uclass's post_probe() method, the real virtio device driver
+(virtio_net.c, virtio_blk.c) is bound if there is a match on the device ID.
+
+The child_post_bind(), child_pre_probe() and child_post_probe() methods of the
+virtio uclass driver help bring the virtio device driver online. They do things
+like acknowledging device, feature negotiation, etc, which are really common
+for all virtio devices.
+
+The transport drivers provide a set of ops (struct dm_virtio_ops) for the real
+virtio device driver to call. These ops APIs's parameter is designed to remind
+the caller to pass the correct 'struct udevice' id of the virtio device, eg:
+
+int virtio_get_status(struct udevice *vdev, u8 *status)
+
+So the parameter 'vdev' indicates the device should be the real virtio device.
+But we also have an API like:
+
+struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num,
+					 unsigned int vring_align,
+					 struct udevice *udev)
+
+Here the parameter 'udev' indicates the device should be the transport device.
+Similar naming is applied in other functions that are even not APIs, eg:
+
+static int virtio_uclass_post_probe(struct udevice *udev)
+static int virtio_uclass_child_pre_probe(struct udevice *vdev)
+
+So it's easy to tell which device these functions are operating on.
+
+Development Flow
+----------------
+At present only VirtIO network card (device ID 1) and block device (device
+ID 2) are supported. If you want to develop new driver for new devices,
+please follow the guideline below.
+
+1. add new device ID in virtio.h
+#define VIRTIO_ID_XXX		X
+
+2. update VIRTIO_ID_MAX_NUM to be the largest device ID plus 1
+
+3. add new driver name string in virtio.h
+#define VIRTIO_XXX_DRV_NAME	"virtio-xxx"
+
+4. create a new driver with name set to the name string above
+U_BOOT_DRIVER(virtio_xxx) = {
+	.name = VIRTIO_XXX_DRV_NAME,
+	...
+}
+
+5. provide bind() method in the driver, where virtio_driver_features_init()
+   should be called for driver to negotiate feature support with the device.
+
+6. do funny stuff with the driver
+
+References
+----------
+[1] http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf
+[2] https://www.qemu.org
Signature · 1 line
2.7.4
#75 lore raw

PATCH 27/27 doc: Document virtio support

Hi Bin,

Quoted 11 lines · Bin Meng

Add REAME.virtio to describe the information about U-Boot support for VirtIO devices, including supported boards, build instructions, driver details etc.

---

 doc/README.virtio | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 247 insertions(+)
 create mode 100644 doc/README.virtio

Great docs!

- Simon

#76 lore raw

cover virtio: Introduce VirtIO driver support

In reply to Bin Meng

Hi Bin,

Quoted 28 lines · Bin Meng

This series brings in VirtIO driver support in U-Boot. The work is based on Tuomas's virtio support on QEMU ARM targets.

VirtIO is a virtualization standard for network and disk device drivers where just the guest's device driver "knows" it is running in a virtual environment, and cooperates with the hypervisor. This enables guests to get high performance network and disk operations, and gives most of the performance benefits of paravirtualization. In the U-Boot case, the guest is U-Boot itself, while the virtual environment are normally QEMU targets like ARM, RISC-V and x86.

Both VirtIO MMIO and PCI transport options are supported. Only VirtIO network and block device drivers are supported for now.

The following QEMU targets are supported.

- qemu_arm_defconfig
- qemu_arm64_defconfig
- qemu-riscv32_defconfig
- qemu-riscv64_defconfig
- qemu-x86_defconfig
- qemu-x86_64_defconfig

A new child_post_probe() uclass driver method is introduced to better support virtio device driver. This also changes BLK uclass driver to supply a post_probe() method which calls part_init(), so that we can avoid duplicating such call in every BLK driver.

Quoted 9 lines

Not every checkpatch warning reported was fixed, but I tried to fix as many as possible which makes sense.

This series needs to be applied after the risc-v QEMU series is applied.

This series is available at u-boot-x86/virtio-working for testing. Note the branch already contains the risc-v QEMU series plus one network stack fix to make virtio-net driver happy.

Looks like a very nice implementation.

How does this all get tested? Could we have a simple sandbox driver?

Regards,
Simon

#77 lore raw

cover virtio: Introduce VirtIO driver support

Hi Simon,

On 09/27/2018 04:43 PM, Simon Glass wrote:
...

Quoted 3 lines

How does this all get tested? Could we have a simple sandbox driver?

We can switch the Travis-CI jobs for the QEMU boards to use virtio-net instead of the current network cards for the TFTP tests. I don't know if there are pytest equivalents for block devices.

#78 lore raw

cover virtio: Introduce VirtIO driver support

Hi,

Quoted 6 lines · Tuomas Tynkkynen

Hi Simon,

On 09/27/2018 04:43 PM, Simon Glass wrote:
...

Quoted 2 lines
Quoted 6 lines
Quoted 2 lines

How does this all get tested? Could we have a simple sandbox driver?

We can switch the Travis-CI jobs for the QEMU boards to use virtio-net instead of the current network cards for the TFTP tests. I don't know if there are pytest equivalents for block devices.

We do actually have sandbox block devices and can add anything that is needed. While qemu is helpful, I much prefer tests that run with 'make tests'.

Regards,
Simon

author
#79 lore raw

cover virtio: Introduce VirtIO driver support

Hi Simon,

Quoted 13 lines · Simon Glass

Hi,

Quoted 9 lines · Tuomas Tynkkynen

Hi Simon,

On 09/27/2018 04:43 PM, Simon Glass wrote:
...

Quoted 4 lines

How does this all get tested? Could we have a simple sandbox driver?

Quoted 7 lines
Quoted 3 lines

We can switch the Travis-CI jobs for the QEMU boards to use virtio-net instead of the current network cards for the TFTP tests. I don't know if there are pytest equivalents for block devices.

We do actually have sandbox block devices and can add anything that is needed. While qemu is helpful, I much prefer tests that run with 'make tests'.

I had a look at the testing on sandbox. It looks to me that we need introduce a non-existent virtio transport sandbox emulator to support this. I am not sure this is worth to do so. As Tuomas mentioned we can setup travis-ci to test on qemu targets.

Regards,
Bin

#80 lore raw

cover virtio: Introduce VirtIO driver support

Hi Bin,

Quoted 20 lines · Bin Meng

Hi Simon,

Quoted 17 lines · Simon Glass

Hi,

Quoted 12 lines · Tuomas Tynkkynen

Hi Simon,

On 09/27/2018 04:43 PM, Simon Glass wrote:
...

Quoted 4 lines

How does this all get tested? Could we have a simple sandbox driver?

We can switch the Travis-CI jobs for the QEMU boards to use virtio-net instead of the current network cards for the TFTP tests. I don't know if there are pytest equivalents for block devices.

Quoted 8 lines
Quoted 3 lines

We do actually have sandbox block devices and can add anything that is needed. While qemu is helpful, I much prefer tests that run with 'make tests'.

I had a look at the testing on sandbox. It looks to me that we need introduce a non-existent virtio transport sandbox emulator to support this. I am not sure this is worth to do so. As Tuomas mentioned we can setup travis-ci to test on qemu targets.

I would like to have at least some basic testing of each uclass within U-Boot without relying on qemu, which after all is a much more complicated test. We have created simple sandbox drivers other uclasses and I don't see a good reason why virtio should be different? There are only 11 methods in the uclass so it should not be a huge amount of work to call each one. I certainly understand the burden of this, but I think it will pay off.

Regards,
Simon

author
#81 lore raw

cover virtio: Introduce VirtIO driver support

Hi Simon,

Quoted 33 lines · Simon Glass

Hi Bin,

Quoted 20 lines · Bin Meng

Hi Simon,

Quoted 17 lines · Simon Glass

Hi,

Quoted 12 lines · Tuomas Tynkkynen

Hi Simon,

On 09/27/2018 04:43 PM, Simon Glass wrote:
...

Quoted 4 lines

How does this all get tested? Could we have a simple sandbox driver?

We can switch the Travis-CI jobs for the QEMU boards to use virtio-net instead of the current network cards for the TFTP tests. I don't know if there are pytest equivalents for block devices.

Quoted 8 lines
Quoted 3 lines

We do actually have sandbox block devices and can add anything that is needed. While qemu is helpful, I much prefer tests that run with 'make tests'.

I had a look at the testing on sandbox. It looks to me that we need introduce a non-existent virtio transport sandbox emulator to support this. I am not sure this is worth to do so. As Tuomas mentioned we can setup travis-ci to test on qemu targets.

Quoted 8 lines

I would like to have at least some basic testing of each uclass within U-Boot without relying on qemu, which after all is a much more complicated test. We have created simple sandbox drivers other uclasses and I don't see a good reason why virtio should be different? There are only 11 methods in the uclass so it should not be a huge amount of work to call each one. I certainly understand the burden of this, but I think it will pay off.

OK, will try to add a simple virtio sandbox driver to validate the API usage.

Regards,
Bin