virtio: Implement generic vhost-user-i2c backend
cover virtio: Implement generic vhost-user-i2c backend
Hello,
This is an initial implementation of a generic vhost-user backend for the I2C bus. This is based of the virtio specifications (already merged) for the I2C bus.
The kernel virtio I2C driver is still under review, here is the latest version (v10):
The backend is implemented as a vhost-user device because we want to experiment in making portable backends that can be used with multiple hypervisors. We also want to support backends isolated in their own separate service VMs with limited memory cross-sections with the principle guest. This is part of a wider initiative by Linaro called "project Stratos" for which you can find information here:
https://collaborate.linaro.org/display/STR/Stratos+Home
I mentioned this to explain the decision to write the daemon as a fairly pure glib application that just depends on libvhost-user.
We are not sure where the vhost-user backend should get queued, qemu or a separate repository. Similar questions were raised by an earlier thread by Alex Bennée for his RPMB work:
https://lore.kernel.org/qemu-devel/[email protected]/
Testing:
-------
I didn't have access to a real hardware where I can play with a I2C client device (like RTC, eeprom, etc) to verify the working of the backend daemon, so I decided to test it on my x86 box itself with hierarchy of two ARM64 guests.
The first ARM64 guest was passed "-device ds1338,address=0x20" option, so it could emulate a ds1338 RTC device, which connects to an I2C bus. Once the guest came up, ds1338 device instance was created within the guest kernel by doing:
echo ds1338 0x20 > /sys/bus/i2c/devices/i2c-0/new_device
[
Note that this may end up binding the ds1338 device to its driver, which won't let our i2c daemon talk to the device. For that we need to manually unbind the device from the driver:
echo 0-0020 > /sys/bus/i2c/devices/0-0020/driver/unbind
]
After this is done, you will get /dev/rtc1. This is the device we wanted to emulate, which will be accessed by the vhost-user-i2c backend daemon via the /dev/i2c-0 file present in the guest VM.
At this point we need to start the backend daemon and give it a socket-path to talk to from qemu (you can pass -v to it to get more detailed messages):
vhost-user-i2c --socket-path=vi2c.sock --device-list 0:20
[ Here, 0:20 is the bus/device mapping, 0 for /dev/i2c-0 and 20 is client address of ds1338 that we used while creating the device. ]
Now we need to start the second level ARM64 guest (from within the first guest) to get the i2c-virtio.c Linux driver up. The second level guest is passed the following options to connect to the same socket:
-chardev socket,path=vi2c.sock,id=vi2c \ -device vhost-user-i2c-pci,chardev=vi2c,id=i2c
Once the second level guest boots up, we will see the i2c-virtio bus at /sys/bus/i2c/devices/i2c-X/. From there we can now make it emulate the ds1338 device again by doing:
echo ds1338 0x20 > /sys/bus/i2c/devices/i2c-0/new_device
[ This time we want ds1338's driver to be bound to the device, so it should be enabled in the kernel as well. ]
And we will get /dev/rtc1 device again here in the second level guest. Now we can play with the rtc device with help of hwclock utility and we can see the following sequence of transfers happening if we try to update rtc's time from system time.
hwclock -w -f /dev/rtc1 (in guest2) ->
Reaches i2c-virtio.c (Linux bus driver in guest2) ->
transfer over virtio ->
Reaches the qemu's vhost-i2c device emulation (running over guest1) ->
Reaches the backend daemon vhost-user-i2c started earlier (in guest1) ->
ioctl(/dev/i2c-0, I2C_RDWR, ..); (in guest1) ->
reaches qemu's hw/rtc/ds1338.c (running over host)I hope I was able to give a clear picture of my test setup here :)
Thanks.
Viresh Kumar (5):
hw/virtio: add boilerplate for vhost-user-i2c device hw/virtio: add vhost-user-i2c-pci boilerplate tools/vhost-user-i2c: Add backend driver
docs: add a man page for vhost-user-i2c
MAINTAINERS: Add entry for virtio-i2c
MAINTAINERS | 9 + docs/tools/index.rst | 1 + docs/tools/vhost-user-i2c.rst | 75 +++ hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 2 + hw/virtio/vhost-user-i2c-pci.c | 79 +++ hw/virtio/vhost-user-i2c.c | 286 +++++++++ include/hw/virtio/vhost-user-i2c.h | 37 ++ include/standard-headers/linux/virtio_ids.h | 1 + tools/meson.build | 8 + tools/vhost-user-i2c/50-qemu-i2c.json.in | 5 + tools/vhost-user-i2c/main.c | 652 ++++++++++++++++++++ tools/vhost-user-i2c/meson.build | 10 + 13 files changed, 1170 insertions(+) create mode 100644 docs/tools/vhost-user-i2c.rst create mode 100644 hw/virtio/vhost-user-i2c-pci.c create mode 100644 hw/virtio/vhost-user-i2c.c create mode 100644 include/hw/virtio/vhost-user-i2c.h create mode 100644 tools/vhost-user-i2c/50-qemu-i2c.json.in create mode 100644 tools/vhost-user-i2c/main.c create mode 100644 tools/vhost-user-i2c/meson.build
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
PATCH 1/5 hw/virtio: add boilerplate for vhost-user-i2c device
This creates the QEMU side of the vhost-user-i2c device which connects to the remote daemon. It is based of vhost-user-fs code.
--- hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 1 + hw/virtio/vhost-user-i2c.c | 286 ++++++++++++++++++++ include/hw/virtio/vhost-user-i2c.h | 37 +++ include/standard-headers/linux/virtio_ids.h | 1 + 5 files changed, 330 insertions(+) create mode 100644 hw/virtio/vhost-user-i2c.c create mode 100644 include/hw/virtio/vhost-user-i2c.h
hw/virtio/Kconfig +5 -0
@@ -58,3 +58,8 @@ config VIRTIO_MEM depends on LINUX depends on VIRTIO_MEM_SUPPORTED select MEM_DEVICE + +config VHOST_USER_I2C + bool + default y + depends on VIRTIO && VHOST_USER
hw/virtio/meson.build +1 -0
@@ -25,6 +25,7 @@ virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock. virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) +virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c')) virtio_pci_ss = ss.source_set() virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c'))
hw/virtio/vhost-user-i2c.c new file +286 -0
286 lines not on this page · raw message
include/hw/virtio/vhost-user-i2c.h new file +37 -0
@@ -0,0 +1,37 @@ +/* +* Vhost-user i2c virtio device +* +* Copyright (c) 2021 Viresh Kumar <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef _QEMU_VHOST_USER_I2C_H +#define _QEMU_VHOST_USER_I2C_H + +#include "hw/virtio/virtio.h" +#include "hw/virtio/vhost.h" +#include "hw/virtio/vhost-user.h" +#include "chardev/char-fe.h" + +#define TYPE_VHOST_USER_I2C "vhost-user-i2c-device" +OBJECT_DECLARE_SIMPLE_TYPE(VHostUserI2C, VHOST_USER_I2C) + +typedef struct { +CharBackend chardev; +}VHostUserI2CConf ; + +struct VHostUserI2C { +/*< private >*/ +VirtIODevice parent; +VHostUserI2CConf conf; +struct vhost_virtqueue *vhost_vq; +struct vhost_dev vhost_dev; +VhostUserState vhost_user; +VirtQueue *req_vq; +bool connected; + +/*< public >*/ +}; + +#endif /* _QEMU_VHOST_USER_I2C_H */
include/standard-headers/linux/virtio_ids.h +1 -0
@@ -54,5 +54,6 @@#define VIRTIO_ID_FS 26/* virtio filesystem */ #define VIRTIO_ID_PMEM 27/* virtio pmem */ #define VIRTIO_ID_MAC80211_HWSIM 29/* virtio mac80211-hwsim */ +#define VIRTIO_ID_I2C 34/* virtio i2c */ #endif /* _LINUX_VIRTIO_IDS_H */
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
Re: [PATCH 1/5] hw/virtio: add boilerplate for vhost-user-i2c device
Quoted 178 lines · Viresh Kumar
This creates the QEMU side of the vhost-user-i2c device which connects to the remote daemon. It is based of vhost-user-fs code.
Signed-off-by Viresh Kumar <[email protected]>--- hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 1 + hw/virtio/vhost-user-i2c.c | 286 ++++++++++++++++++++ include/hw/virtio/vhost-user-i2c.h | 37 +++ include/standard-headers/linux/virtio_ids.h | 1 + 5 files changed, 330 insertions(+) create mode 100644 hw/virtio/vhost-user-i2c.c create mode 100644 include/hw/virtio/vhost-user-i2c.hhw/virtio/Kconfig +5
-0@@ -58,3 +58,8 @@ config VIRTIO_MEM depends on LINUX depends on VIRTIO_MEM_SUPPORTED select MEM_DEVICE + +config VHOST_USER_I2C + bool + default y + depends on VIRTIO && VHOST_USERhw/virtio/meson.build +1
-0@@ -25,6 +25,7 @@ virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock. virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) +virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c')) virtio_pci_ss = ss.source_set() virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c'))hw/virtio/vhost-user-i2c.c new file +133
-0@@ -0,0 +1,286 @@ +/* +* Vhost-user i2c virtio device +* +* Copyright (c) 2021 Viresh Kumar <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "hw/qdev-properties.h" +#include "hw/virtio/virtio-bus.h" +#include "hw/virtio/vhost-user-i2c.h" +#include "qemu/error-report.h" +#include "standard-headers/linux/virtio_ids.h" + +static void vu_i2c_start (VirtIODevice *vdev) +{ +VHostUserI2C *i2c =VHOST_USER_I2C (vdev); +BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (vdev))); +VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus); +int ret; +int i; + +if (!k->set_guest_notifiers) { +error_report ("binding does not support guest notifiers" ); +return ; + } + + ret =vhost_dev_enable_notifiers (&i2c->vhost_dev, vdev); +if (ret <0 ) { +error_report ("Error enabling host notifiers: %d" , -ret); +return ; + } + + ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, true); +if (ret <0 ) { +error_report ("Error binding guest notifier: %d" , -ret); + goto err_host_notifiers; + } + + i2c->vhost_dev.acked_features = vdev->guest_features; + ret =vhost_dev_start (&i2c->vhost_dev, vdev); +if (ret <0 ) { +error_report ("Error starting vhost-user-i2c: %d" , -ret); + goto err_guest_notifiers; + } + +/* +* guest_notifier_mask/pending not used yet, so just unmask +* everything here. virtio-pci will do the right thing by +* enabling/disabling irqfd. +*/ +for (i =0 ; i < i2c->vhost_dev.nvqs; i++) { +vhost_virtqueue_mask (&i2c->vhost_dev, vdev, i, false); + } + +return ; + +err_guest_notifiers: + k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false); +err_host_notifiers: +vhost_dev_disable_notifiers (&i2c->vhost_dev, vdev); +} + +static void vu_i2c_stop (VirtIODevice *vdev) +{ +VHostUserI2C *i2c =VHOST_USER_I2C (vdev); +BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (vdev))); +VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus); +int ret; + +if (!k->set_guest_notifiers) { +return ; + } + +vhost_dev_stop (&i2c->vhost_dev, vdev); + + ret = k->set_guest_notifiers (qbus->parent, i2c->vhost_dev.nvqs, false); +if (ret <0 ) { +error_report ("vhost guest notifier cleanup failed: %d" , ret); +return ; + } + +vhost_dev_disable_notifiers (&i2c->vhost_dev, vdev); +} + +static void vu_i2c_set_status (VirtIODevice *vdev,uint8_t status) +{ +VHostUserI2C *i2c =VHOST_USER_I2C (vdev); +bool should_start = status &VIRTIO_CONFIG_S_DRIVER_OK ; + +if (!vdev->vm_running) { + should_start = false; + } + +if (i2c->vhost_dev.started == should_start) { +return ; + } + +if (should_start) { +vu_i2c_start (vdev); + }else { +vu_i2c_stop (vdev); + } +} + +static uint64_t vu_i2c_get_features (VirtIODevice *vdev, +uint64_t requested_features,Error **errp) +{ +/* No feature bits used yet */ +return requested_features; +} + +static void vu_i2c_handle_output (VirtIODevice *vdev,VirtQueue *vq) +{ +/* +* Not normally called; it's the daemon that handles the queue; +* however virtio's cleanup path can call this. +*/ +} + +static void vu_i2c_guest_notifier_mask (VirtIODevice *vdev,int idx,bool mask) +{ +VHostUserI2C *i2c =VHOST_USER_I2C (vdev); + +vhost_virtqueue_mask (&i2c->vhost_dev, vdev, idx, mask); +} + +static bool vu_i2c_guest_notifier_pending (VirtIODevice *vdev,int idx) +{ +VHostUserI2C *i2c =VHOST_USER_I2C (vdev); +
hw/virtio/vhost-user-i2c.c line 143
Quoted 10 lines
hw/virtio/vhost-user-i2c.c new file +10
-0+return vhost_virtqueue_pending (&i2c->vhost_dev, idx); +} + +static void do_vhost_user_cleanup (VirtIODevice *vdev,VHostUserI2C *i2c) +{ +vhost_user_cleanup (&i2c->vhost_user); +virtio_delete_queue (i2c->req_vq); +g_free (i2c->vhost_dev.vqs); +virtio_cleanup (vdev); +g_free (i2c->vhost_dev.vqs);
This is a double free of the same queue.
Quoted 191 lines
hw/virtio/vhost-user-i2c.c new file +143
-0+ i2c->vhost_dev.vqs =NULL ; +} + +static int vu_i2c_connect (DeviceState *dev) +{ +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VHostUserI2C *i2c =VHOST_USER_I2C (vdev); + +if (i2c->connected) { +return 0 ; + } + i2c->connected = true; + +/* restore vhost state */ +if (virtio_device_started (vdev, vdev->status)) { +vu_i2c_start (vdev); + } + +return 0 ; +} + +static void vu_i2c_disconnect (DeviceState *dev) +{ +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VHostUserI2C *i2c =VHOST_USER_I2C (vdev); + +if (!i2c->connected) { +return ; + } + i2c->connected = false; + +if (i2c->vhost_dev.started) { +vu_i2c_stop (vdev); + } +} + +static void vu_i2c_event (void *opaque,QEMUChrEvent event) +{ +DeviceState *dev = opaque; +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VHostUserI2C *i2c =VHOST_USER_I2C (vdev); + +switch (event) { +case CHR_EVENT_OPENED : +if (vu_i2c_connect (dev) <0 ) { +qemu_chr_fe_disconnect (&i2c->conf.chardev); +return ; + } +break ; +case CHR_EVENT_CLOSED : +vu_i2c_disconnect (dev); +break ; +case CHR_EVENT_BREAK : +case CHR_EVENT_MUX_IN : +case CHR_EVENT_MUX_OUT : +/* Ignore */ +break ; + } +} + +static void vu_i2c_device_realize (DeviceState *dev,Error **errp) +{ +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VHostUserI2C *i2c =VHOST_USER_I2C (dev); +int ret; + +if (!i2c->conf.chardev.chr) { +error_setg (errp,"missing chardev" ); +return ; + } + +if (!vhost_user_init (&i2c->vhost_user, &i2c->conf.chardev, errp)) { +return ; + } + +virtio_init (vdev,"vhost-user-i2c" ,VIRTIO_ID_I2C ,0 ); + + i2c->req_vq =virtio_add_queue (vdev,3 , vu_i2c_handle_output); + i2c->vhost_dev.nvqs =1 ; + i2c->vhost_dev.vqs = g_new0(struct vhost_virtqueue, i2c->vhost_dev.nvqs); + ret =vhost_dev_init (&i2c->vhost_dev, &i2c->vhost_user, +VHOST_BACKEND_TYPE_USER ,0 ); +if (ret <0 ) { +error_setg_errno (errp, -ret,"vhost_dev_init() failed" ); +do_vhost_user_cleanup (vdev, i2c); + } + +qemu_chr_fe_set_handlers (&i2c->conf.chardev,NULL ,NULL , vu_i2c_event,NULL , + dev,NULL , true); +} + +static void vu_i2c_device_unrealize (DeviceState *dev) +{ +VirtIODevice *vdev =VIRTIO_DEVICE (dev); +VHostUserI2C *i2c =VHOST_USER_I2C (dev); + +/* This will stop vhost backend if appropriate. */ +vu_i2c_set_status (vdev,0 ); + +vhost_dev_cleanup (&i2c->vhost_dev); + +do_vhost_user_cleanup (vdev, i2c); +} + +static const VMStateDescription vu_i2c_vmstate = { + .name ="vhost-user-i2c" , + .unmigratable =1 , +}; + +static Property vu_i2c_properties[] = { +DEFINE_PROP_CHR ("chardev" , VHostUserI2C, conf.chardev), +DEFINE_PROP_END_OF_LIST (), +}; + +static void vu_i2c_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +VirtioDeviceClass *vdc =VIRTIO_DEVICE_CLASS (klass); + +device_class_set_props (dc, vu_i2c_properties); + dc->vmsd = &vu_i2c_vmstate; +set_bit (DEVICE_CATEGORY_INPUT , dc->categories); + vdc->realize = vu_i2c_device_realize; + vdc->unrealize = vu_i2c_device_unrealize; + vdc->get_features = vu_i2c_get_features; + vdc->set_status = vu_i2c_set_status; + vdc->guest_notifier_mask = vu_i2c_guest_notifier_mask; + vdc->guest_notifier_pending = vu_i2c_guest_notifier_pending; +} + +static const TypeInfo vu_i2c_info = { + .name =TYPE_VHOST_USER_I2C , + .parent =TYPE_VIRTIO_DEVICE , + .instance_size =sizeof (VHostUserI2C), + .class_init = vu_i2c_class_init, +}; + +static void vu_i2c_register_types (void ) +{ +type_register_static (&vu_i2c_info); +} + +type_init (vu_i2c_register_types)include/hw/virtio/vhost-user-i2c.h new file +37
-0@@ -0,0 +1,37 @@ +/* +* Vhost-user i2c virtio device +* +* Copyright (c) 2021 Viresh Kumar <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef _QEMU_VHOST_USER_I2C_H +#define _QEMU_VHOST_USER_I2C_H + +#include "hw/virtio/virtio.h" +#include "hw/virtio/vhost.h" +#include "hw/virtio/vhost-user.h" +#include "chardev/char-fe.h" + +#define TYPE_VHOST_USER_I2C "vhost-user-i2c-device" +OBJECT_DECLARE_SIMPLE_TYPE(VHostUserI2C, VHOST_USER_I2C) + +typedef struct { +CharBackend chardev; +}VHostUserI2CConf ; + +struct VHostUserI2C { +/*< private >*/ +VirtIODevice parent; +VHostUserI2CConf conf; +struct vhost_virtqueue *vhost_vq; +struct vhost_dev vhost_dev; +VhostUserState vhost_user; +VirtQueue *req_vq; +bool connected; + +/*< public >*/ +}; + +#endif /* _QEMU_VHOST_USER_I2C_H */ include/standard-headers/linux/virtio_ids.h +0
-0
include/standard-headers/linux/virtio_ids.h line 57
Quoted 4 lines
include/standard-headers/linux/virtio_ids.h +1
-0@@ -54,5 +54,6 @@#define VIRTIO_ID_FS 26/* virtio filesystem */ #define VIRTIO_ID_PMEM 27/* virtio pmem */ #define VIRTIO_ID_MAC80211_HWSIM 29/* virtio mac80211-hwsim */ +#define VIRTIO_ID_I2C 34/* virtio i2c */
This should come in as a separate patch. Generally I run:
./scripts/update-linux-headers.sh ~/lsrc/linux.git
and tag the patch as (!MERGE) as a place holder until it can get picked up with one of the regular UAPI updates.
Quoted 2 lines
include/standard-headers/linux/virtio_ids.h +0
-0#endif /* _LINUX_VIRTIO_IDS_H */
Signature · 1 line
Alex Bennée
PATCH 2/5 hw/virtio: add vhost-user-i2c-pci boilerplate
In reply to Viresh KumarThis allows is to instantiate a vhost-user-i2c device as part of a PCI bus. It is mostly boilerplate which looks pretty similar to the vhost-user-fs-pci device.
--- hw/virtio/meson.build | 1 + hw/virtio/vhost-user-i2c-pci.c | 79 ++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 hw/virtio/vhost-user-i2c-pci.c
hw/virtio/meson.build +1 -0
@@ -26,6 +26,7 @@ virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c')) +virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_I2C'], if_true: files('vhost-user-i2c-pci.c')) virtio_pci_ss = ss.source_set() virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c'))
hw/virtio/vhost-user-i2c-pci.c new file +79 -0
79 lines not on this page · raw message
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
Re: [PATCH 2/5] hw/virtio: add vhost-user-i2c-pci boilerplate
Quoted 47 lines · Viresh Kumar
This allows is to instantiate a vhost-user-i2c device as part of a PCI bus. It is mostly boilerplate which looks pretty similar to the vhost-user-fs-pci device.
Signed-off-by Viresh Kumar <[email protected]>--- hw/virtio/meson.build | 1 + hw/virtio/vhost-user-i2c-pci.c | 79 ++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 hw/virtio/vhost-user-i2c-pci.chw/virtio/meson.build +1
-0@@ -26,6 +26,7 @@ virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c')) +virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_I2C'], if_true: files('vhost-user-i2c-pci.c')) virtio_pci_ss = ss.source_set() virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c'))hw/virtio/vhost-user-i2c-pci.c new file +18
-0@@ -0,0 +1,79 @@ +/* +* Vhost-user i2c virtio device PCI glue +* +* Copyright (c) 2021 Viresh Kumar <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "hw/qdev-properties.h" +#include "hw/virtio/vhost-user-i2c.h" +#include "virtio-pci.h" + +struct VHostUserI2CPCI { +VirtIOPCIProxy parent_obj; +VHostUserI2C vdev; +}; +
hw/virtio/vhost-user-i2c-pci.c line 28
Quoted 10 lines
hw/virtio/vhost-user-i2c-pci.c new file +10
-0+typedef struct VHostUserI2CPCI VHostUserI2CPCI ; + +#define TYPE_VHOST_USER_I2C_PCI "vhost-user-i2c-pci-base" + +DECLARE_INSTANCE_CHECKER (VHostUserI2CPCI,VHOST_USER_I2C_PCI , + TYPE_VHOST_USER_I2C_PCI) + +static Property vhost_user_i2c_pci_properties[] = { +DEFINE_PROP_UINT32 ("vectors" , VirtIOPCIProxy, nvectors, +DEV_NVECTORS_UNSPECIFIED ),
I suspect we can drop the property if there is nothing useful the user can specify here. We can just default to 1 on device realization.
Otherwise:
Signature · 1 line
Alex Bennée
PATCH 3/5 tools/vhost-user-i2c: Add backend driver
In reply to Viresh KumarThis adds the vhost-user backend driver to support virtio based I2C devices.
vhost-user-i2c --help
--- hw/virtio/vhost-user-i2c.c | 2 +- tools/meson.build | 8 + tools/vhost-user-i2c/50-qemu-i2c.json.in | 5 + tools/vhost-user-i2c/main.c | 652 +++++++++++++++++++++++ tools/vhost-user-i2c/meson.build | 10 + 5 files changed, 676 insertions(+), 1 deletion(-) create mode 100644 tools/vhost-user-i2c/50-qemu-i2c.json.in create mode 100644 tools/vhost-user-i2c/main.c create mode 100644 tools/vhost-user-i2c/meson.build
hw/virtio/vhost-user-i2c.c +1 -1
@@ -218,7 +218,7 @@ static void vu_i2c_device_realize(DeviceState *dev, Error **errp)virtio_init (vdev,"vhost-user-i2c" ,VIRTIO_ID_I2C ,0 ); - i2c->req_vq =virtio_add_queue (vdev,3 , vu_i2c_handle_output); + i2c->req_vq =virtio_add_queue (vdev,4 , vu_i2c_handle_output); i2c->vhost_dev.nvqs =1 ; i2c->vhost_dev.vqs = g_new0(struct vhost_virtqueue, i2c->vhost_dev.nvqs); ret = vhost_dev_init(&i2c->vhost_dev, &i2c->vhost_user,
tools/meson.build +8 -0
@@ -24,3 +24,11 @@ endif if have_virtiofsd subdir('virtiofsd') endif + +have_virtioi2c= (have_system and + have_tools and + 'CONFIG_LINUX' in config_host) + +if have_virtioi2c + subdir('vhost-user-i2c') +endif
tools/vhost-user-i2c/50-qemu-i2c.json.in new file +5 -0
@@ -0,0 +1,5 @@ +{ + "description": "QEMU vhost-user-i2c", + "type": "bridge", + "binary": "@libexecdir@/vhost-user-i2c" +}
tools/vhost-user-i2c/main.c new file +652 -0
652 lines not on this page · raw message
tools/vhost-user-i2c/meson.build new file +10 -0
@@ -0,0 +1,10 @@ +executable('vhost-user-i2c', files( + 'main.c'), + dependencies: [qemuutil, glib, gio], + install: true, + install_dir: get_option('libexecdir')) + +configure_file(input: '50-qemu-i2c.json.in', + output: '50-qemu-i2c.json', + configuration: config_host, + install_dir: qemu_datadir / 'vhost-user')
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
Quoted 12 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +12
-0+ +/* Definitions from virtio-i2c specifications */ +#define VHOST_USER_I2C_MAX_QUEUES 1 + +/* Status */ +#define VIRTIO_I2C_MSG_OK 0 +#define VIRTIO_I2C_MSG_ERR 1 + +/* The bit 0 of the @virtio_i2c_out_hdr.@flags, used to group the requests */ +#define VIRTIO_I2C_FLAGS_FAIL_NEXT 0x00000001 + +/**
tools/vhost-user-i2c/main.c line 56
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+* struct virtio_i2c_out_hdr - the virtio I2C message OUT header +* @addr: the controlled device's address +* @padding: used to pad to full dword +* @flags: used for feature extensibility +*/ +struct virtio_i2c_out_hdr { +uint16_t addr; +uint16_t padding; +uint32_t flags; +} __attribute__((packed));
__le16, __le32 ?
tools/vhost-user-i2c/main.c line 65
Quoted 9 lines
tools/vhost-user-i2c/main.c new file +9
-0+ +/** +* struct virtio_i2c_in_hdr - the virtio I2C message IN header +* @status: the processing result from the backend +*/ +struct virtio_i2c_in_hdr { +uint8_t status; +} __attribute__((packed)); +
I understand these definitions can be removed once the frontend driver is merged by the Linux ?
tools/vhost-user-i2c/main.c line 72
Quoted 7 lines
tools/vhost-user-i2c/main.c new file +7
-0+/* vhost-user-i2c definitions */ + +#ifndef container_of +#define container_of (ptr, type, member) ({ \ + const typeof(((type *) 0)->member) *__mptr = (ptr); \ + (type *) ((char *) __mptr - offsetof(type, member));}) +#endif
This seems to be a general interface. I see there is a definition in qemu/compiler.h.
Can we reuse it ?
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
Quoted 19 lines · Jie Deng
Quoted 17 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +17
-0+ +/* Definitions from virtio-i2c specifications */ +#define VHOST_USER_I2C_MAX_QUEUES 1 + +/* Status */ +#define VIRTIO_I2C_MSG_OK 0 +#define VIRTIO_I2C_MSG_ERR 1 + +/* The bit 0 of the @virtio_i2c_out_hdr.@flags, used to group the requests */ +#define VIRTIO_I2C_FLAGS_FAIL_NEXT 0x00000001 + +/** +* struct virtio_i2c_out_hdr - the virtio I2C message OUT header +* @addr: the controlled device's address +* @padding: used to pad to full dword +* @flags: used for feature extensibility +*/
Quoted 8 lines
tools/vhost-user-i2c/main.c line 56
Quoted 5 lines
tools/vhost-user-i2c/main.c new file +5
-0+struct virtio_i2c_out_hdr { +uint16_t addr; +uint16_t padding; +uint32_t flags; +} __attribute__((packed));__le16,
__le32?
Maybe, but I didn't do them because of this:
docs/devel/style.rst:
"Don't use Linux kernel internal types like u32, __u32 or __le32."
Quoted 4 lines
Quoted 4 lines
tools/vhost-user-i2c/main.c new file +4
-0+ +/** +* struct virtio_i2c_in_hdr - the virtio I2C message IN header +* @status: the processing result from the backend
Quoted 8 lines
tools/vhost-user-i2c/main.c line 65
Quoted 5 lines
+ */
tools/vhost-user-i2c/main.c new file +4
-0+struct virtio_i2c_in_hdr { +uint8_t status; +} __attribute__((packed)); +I understand these definitions can be removed once the frontend driver is merged by the Linux ?
Yes, we would be required to somehow include the uapi header that kernel is adding and then this won't be required.
Quoted 12 lines
tools/vhost-user-i2c/main.c line 72
Quoted 7 lines
tools/vhost-user-i2c/main.c new file +7
-0+/* vhost-user-i2c definitions */ + +#ifndef container_of +#define container_of (ptr, type, member) ({ \ + const typeof(((type *) 0)->member) *__mptr = (ptr); \ + (type *) ((char *) __mptr - offsetof(type, member));}) +#endif This seems to be a general interface. I see there is a definition in
qemu/compiler.h.
Quoted 1 line
Can we reuse it ?
Damn. My bad (maybe not). I picked this part from the RPMB patchset that Alex sent and didn't bother looking for it.
Though on the other hand, we are looking to make this file independent of qemu so it can be used by other hypervisors without any (or much) modifications, and maybe so it was done so.
Alex ?
Signature · 1 line
viresh
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
Quoted 48 lines · Viresh Kumar
Quoted 19 lines · Jie Deng
Quoted 17 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +17
-0+ +/* Definitions from virtio-i2c specifications */ +#define VHOST_USER_I2C_MAX_QUEUES 1 + +/* Status */ +#define VIRTIO_I2C_MSG_OK 0 +#define VIRTIO_I2C_MSG_ERR 1 + +/* The bit 0 of the @virtio_i2c_out_hdr.@flags, used to group the requests */ +#define VIRTIO_I2C_FLAGS_FAIL_NEXT 0x00000001 + +/** +* struct virtio_i2c_out_hdr - the virtio I2C message OUT header +* @addr: the controlled device's address +* @padding: used to pad to full dword +* @flags: used for feature extensibility +*/ Quoted 8 lines
tools/vhost-user-i2c/main.c line 56
Quoted 5 lines
tools/vhost-user-i2c/main.c new file +5
-0+struct virtio_i2c_out_hdr { +uint16_t addr; +uint16_t padding; +uint32_t flags; +} __attribute__((packed));__le16,
__le32?Maybe, but I didn't do them because of this:
docs/devel/style.rst:"Don't use Linux kernel internal types like u32,
__u32or__le32."Quoted 4 lines
Quoted 4 lines
tools/vhost-user-i2c/main.c new file +4
-0+ +/** +* struct virtio_i2c_in_hdr - the virtio I2C message IN header +* @status: the processing result from the backend Quoted 8 lines
tools/vhost-user-i2c/main.c line 65
Quoted 5 lines
+ */
tools/vhost-user-i2c/main.c new file +4
-0+struct virtio_i2c_in_hdr { +uint8_t status; +} __attribute__((packed)); +I understand these definitions can be removed once the frontend driver is merged by the Linux ?
Quoted 2 lines
Yes, we would be required to somehow include the uapi header that kernel is adding and then this won't be required.
What I often do is include a temporary patch in my series that includes the updated uapi headers from my Linux branch and mark it as not for merge until the uapi headers make it into a released tree.
Quoted 23 lines
Quoted 13 lines
tools/vhost-user-i2c/main.c line 72
Quoted 8 lines
tools/vhost-user-i2c/main.c new file +7
-0+/* vhost-user-i2c definitions */ + +#ifndef container_of +#define container_of (ptr, type, member) ({ \ + const typeof(((type *) 0)->member) *__mptr = (ptr); \ + (type *) ((char *) __mptr - offsetof(type, member));}) +#endif This seems to be a general interface. I see there is a definition in
qemu/compiler.h.Quoted 1 line
Can we reuse it ?
Damn. My bad (maybe not). I picked this part from the RPMB patchset that Alex sent and didn't bother looking for it.
Though on the other hand, we are looking to make this file independent of qemu so it can be used by other hypervisors without any (or much) modifications, and maybe so it was done so.
Alex ?
Signature · 1 line
Alex Bennée
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
In reply to Viresh KumarQuoted 3 lines · Viresh Kumar
Quoted 2 lines · Jie Deng
On 2021/3/24 15:33, Viresh Kumar wrote:
<snip>
Quoted 15 lines
Quoted 13 lines
tools/vhost-user-i2c/main.c line 72
Quoted 8 lines
tools/vhost-user-i2c/main.c new file +7
-0+/* vhost-user-i2c definitions */ + +#ifndef container_of +#define container_of (ptr, type, member) ({ \ + const typeof(((type *) 0)->member) *__mptr = (ptr); \ + (type *) ((char *) __mptr - offsetof(type, member));}) +#endif This seems to be a general interface. I see there is a definition in
qemu/compiler.h.Quoted 1 line
Can we reuse it ?
Quoted 8 lines
Damn. My bad (maybe not). I picked this part from the RPMB patchset that Alex sent and didn't bother looking for it.
Though on the other hand, we are looking to make this file independent of qemu so it can be used by other hypervisors without any (or much) modifications, and maybe so it was done so.
Alex ?
Yes when doing virtio-rpmb I ended up copying a bunch of helper functions from the core QEMU to avoid a dependency on the core code. These included the vrpmb_iov_* functions as well as the HMAC-SHA-256 implementation (which QEMU has as well).
That said maybe this is futile because I ended up having to qemuutil to the dependencies:
dependencies: [qemuutil, glib, gio, vhost_user],
because it ends up failing to build due to the trace_ points that have been added to the libvhost-user library. I'm sure this wouldn't be too difficult to overcome if needed.
That said this is all build time dependencies - the final binary does not rely on a QEMU library as everything it needs from qemu is statically linked in. That doesn't stop it being portable to other hypervisors or running independently of QEMU but it does tie it to being built as part of the source tree.
Signature · 1 line
Alex Bennée
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
In reply to Viresh KumarQuoted 54 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +54
-0+static int vi2c_parse (VuI2c *i2c) +{ +uint16_t client_addr[MAX_I2C_VDEV ]; +int32_t n_adapter =0 , n_client; +int64_t addr, bus; +const char *cp, *t; + +while (device_list) { +/* Read <bus>:<client_addr>[:<client_addr>] entries one by one */ + cp =strsep (&device_list,"," ); + +if (!cp || *cp =='\0' ) { +break ; + } + +if (n_adapter ==MAX_I2C_ADAPTER ) { +g_printerr ("too many adapter (%d), only support %d \n" , n_adapter, +MAX_I2C_ADAPTER ); + goto out; + } + +if (qemu_strtol (cp, &t,10 , &bus) || bus <0 ) { +g_printerr ("Invalid bus number %s\n" , cp); + goto out; + } + + cp = t; + n_client =0 ; + +/* Parse clients <client_addr>[:<client_addr>] entries one by one */ +while (cp !=NULL && *cp !='\0' ) { +if (*cp ==':' ) + cp++; + +if (n_client ==MAX_I2C_VDEV ) { +g_printerr ("too many devices (%d), only support %d \n" , + n_client,MAX_I2C_VDEV ); + goto out; + } + +if (qemu_strtol (cp, &t,16 , &addr) || addr <0 || addr >MAX_I2C_VDEV ) { +g_printerr ("Invalid address %s : %lx\n" , cp, addr); + goto out; + } + + client_addr[n_client++] = addr; + cp = t; +if (verbose) { +g_print ("i2c adapter %ld:0x%lx\n" , bus, addr); + } + } + + i2c->adapter[n_adapter] =vi2c_create_adapter (bus, client_addr, n_client); +if (!i2c->adapter[n_adapter])
tools/vhost-user-i2c/main.c line 344
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+ goto out; + n_adapter++; + } + +if (!n_adapter) { +g_printerr ("Failed to add any adapters\n" ); +return -1 ; + } + + i2c->adapter_num = n_adapter;
i2c->adapter_num is set here, but used in vi2c_remove_adapters. when you goto out from while {...}, i2c->adapter_num is always 0, May be a bug ?
Quoted 11 lines
tools/vhost-user-i2c/main.c new file +10
-0+ +if (!vi2c_map_adapters (i2c)) { +return 0 ; + } + +out: +vi2c_remove_adapters (i2c); +return -1 ; +} +
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
Quoted 3 lines · Jie Deng
i2c->adapter_numis set here, but used invi2c_remove_adapters. when you goto out from while {...},i2c->adapter_numis always 0, May be a bug ?
It certainly is, this should fix it:
tools/vhost-user-i2c/main.c +1 -1
@@ -202,7 +202,7 @@ static void vi2c_remove_adapters(VuI2c *i2c)VI2cAdapter *adapter;int32_t i; -for (i =0 ; i < i2c->adapter_num; i++) { +for (i =0 ; i <MAX_I2C_ADAPTER ; i++) { adapter = i2c->adapter[i];if (!adapter) {break ;
Signature · 1 line
viresh
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
In reply to Viresh KumarQuoted 9 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +9
-0+static uint8_t vi2c_xfer (VuDev *dev,struct i2c_msg *msg) +{ +VuI2c *i2c =container_of (dev, VuI2c, dev.parent); +struct i2c_rdwr_ioctl_data data; +VI2cAdapter *adapter; + + adapter =vi2c_find_adapter (i2c, msg->addr); +if (!adapter) { +g_printerr ("Failed to find adapter for address: %x\n" , msg->addr);
tools/vhost-user-i2c/main.c line 373
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+return VIRTIO_I2C_MSG_ERR ; + } + + data.nmsgs =1 ; + data.msgs = msg; + +if (ioctl (adapter->fd,I2C_RDWR , &data) <0 ) { +g_printerr ("Failed to transfer data to address %x : %d\n" , msg->addr, errno); +return VIRTIO_I2C_MSG_ERR ; + }
As you found during testing, this doesn't work for host kernels that only implement the SMBUS protocol. Since most i2c clients only need simple register read/write operations, I think you should at least handle the common ones (and one two byte read/write) here to make it more useful.
Quoted 29 lines
tools/vhost-user-i2c/main.c new file +29
-0+static void vi2c_handle_ctrl (VuDev *dev,int qidx) +{ +VuVirtq *vq =vu_get_queue (dev, qidx); +struct i2c_msg msg; +struct virtio_i2c_out_hdr *out_hdr; +struct virtio_i2c_in_hdr *in_hdr; +bool fail_next = false; +size_t len, in_hdr_len; + +for (;;) { +VuVirtqElement *elem; + + elem =vu_queue_pop (dev, vq,sizeof (VuVirtqElement)); +if (!elem) { +break ; + } + +g_debug ("%s: got queue (in %d, out %d)" , __func__, elem->in_num, + elem->out_num); + +/* Validate size of out header */ +if (elem->out_sg[0 ].iov_len !=sizeof (*out_hdr)) { +g_warning ("%s: Invalid out hdr %zu : %zu\n" , __func__, + elem->out_sg[0 ].iov_len,sizeof (*out_hdr)); +continue ; + } + + out_hdr = elem->out_sg[0 ].iov_base; +
tools/vhost-user-i2c/main.c line 439
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+/* Bit 0 is reserved in virtio spec */ + msg.addr = out_hdr->addr >>1 ; + +/* Read Operation */ +if (elem->out_num ==1 && elem->in_num ==2 ) { + len = elem->in_sg[0 ].iov_len; +if (!len) { +g_warning ("%s: Read buffer length can't be zero\n" , __func__); +continue ; + }
It looks like you are not handling endianness conversion here. As far as I can tell, the protocol requires little-endian data, but the code might run on a big-endian CPU.
Jie Deng also pointed out the type differences, but actually handling them correctly is more important that describing them the right way.
Arnd
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
Quoted 19 lines · Arnd Bergmann
Quoted 17 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +17
-0+static uint8_t vi2c_xfer (VuDev *dev,struct i2c_msg *msg) +{ +VuI2c *i2c =container_of (dev, VuI2c, dev.parent); +struct i2c_rdwr_ioctl_data data; +VI2cAdapter *adapter; + + adapter =vi2c_find_adapter (i2c, msg->addr); +if (!adapter) { +g_printerr ("Failed to find adapter for address: %x\n" , msg->addr); +return VIRTIO_I2C_MSG_ERR ; + } + + data.nmsgs =1 ; + data.msgs = msg; + +if (ioctl (adapter->fd,I2C_RDWR , &data) <0 ) { +g_printerr ("Failed to transfer data to address %x : %d\n" , msg->addr, errno);
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return VIRTIO_I2C_MSG_ERR; + }As you found during testing, this doesn't work for host kernels that only implement the SMBUS protocol. Since most i2c clients only need simple register read/write operations, I think you should at least handle the common ones (and one two byte read/write) here to make it more useful.
I am thinking if that is what we really want to support, then shouldn't the i2c virtio spec be updated first to support SMBUS type transfers as well?
Quoted 45 lines
Quoted 29 lines
tools/vhost-user-i2c/main.c new file +29
-0+static void vi2c_handle_ctrl (VuDev *dev,int qidx) +{ +VuVirtq *vq =vu_get_queue (dev, qidx); +struct i2c_msg msg; +struct virtio_i2c_out_hdr *out_hdr; +struct virtio_i2c_in_hdr *in_hdr; +bool fail_next = false; +size_t len, in_hdr_len; + +for (;;) { +VuVirtqElement *elem; + + elem =vu_queue_pop (dev, vq,sizeof (VuVirtqElement)); +if (!elem) { +break ; + } + +g_debug ("%s: got queue (in %d, out %d)" , __func__, elem->in_num, + elem->out_num); + +/* Validate size of out header */ +if (elem->out_sg[0 ].iov_len !=sizeof (*out_hdr)) { +g_warning ("%s: Invalid out hdr %zu : %zu\n" , __func__, + elem->out_sg[0 ].iov_len,sizeof (*out_hdr)); +continue ; + } + + out_hdr = elem->out_sg[0 ].iov_base; +tools/vhost-user-i2c/main.c line 439
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+/* Bit 0 is reserved in virtio spec */ + msg.addr = out_hdr->addr >>1 ; + +/* Read Operation */ +if (elem->out_num ==1 && elem->in_num ==2 ) { + len = elem->in_sg[0 ].iov_len; +if (!len) { +g_warning ("%s: Read buffer length can't be zero\n" , __func__); +continue ; + }It looks like you are not handling endianness conversion here. As far as I can tell, the protocol requires little-endian data, but the code might run on a big-endian CPU.
Quoted 2 lines
Jie Deng also pointed out the type differences, but actually handling them correctly is more important that describing them the right way.
Right, I missed that.
Signature · 1 line
viresh
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
Quoted 29 lines · Viresh Kumar
Quoted 19 lines · Arnd Bergmann
Quoted 17 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +17
-0+static uint8_t vi2c_xfer (VuDev *dev,struct i2c_msg *msg) +{ +VuI2c *i2c =container_of (dev, VuI2c, dev.parent); +struct i2c_rdwr_ioctl_data data; +VI2cAdapter *adapter; + + adapter =vi2c_find_adapter (i2c, msg->addr); +if (!adapter) { +g_printerr ("Failed to find adapter for address: %x\n" , msg->addr); +return VIRTIO_I2C_MSG_ERR ; + } + + data.nmsgs =1 ; + data.msgs = msg; + +if (ioctl (adapter->fd,I2C_RDWR , &data) <0 ) { +g_printerr ("Failed to transfer data to address %x : %d\n" , msg->addr, errno);Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ return VIRTIO_I2C_MSG_ERR; + }As you found during testing, this doesn't work for host kernels that only implement the SMBUS protocol. Since most i2c clients only need simple register read/write operations, I think you should at least handle the common ones (and one two byte read/write) here to make it more useful.
Quoted 3 lines
I am thinking if that is what we really want to support, then shouldn't the i2c virtio spec be updated first to support SMBUS type transfers as well?
As far as I can tell, all the simple devices should just work, with I2C_FUNC_SMBUS_READ_BLOCK_DATA being the main exception, but it seems that has practically no users.
Arnd
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
In reply to Arnd BergmannQuoted 37 lines · Arnd Bergmann
Quoted 36 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +36
-0+static void vi2c_handle_ctrl (VuDev *dev,int qidx) +{ +VuVirtq *vq =vu_get_queue (dev, qidx); +struct i2c_msg msg; +struct virtio_i2c_out_hdr *out_hdr; +struct virtio_i2c_in_hdr *in_hdr; +bool fail_next = false; +size_t len, in_hdr_len; + +for (;;) { +VuVirtqElement *elem; + + elem =vu_queue_pop (dev, vq,sizeof (VuVirtqElement)); +if (!elem) { +break ; + } + +g_debug ("%s: got queue (in %d, out %d)" , __func__, elem->in_num, + elem->out_num); + +/* Validate size of out header */ +if (elem->out_sg[0 ].iov_len !=sizeof (*out_hdr)) { +g_warning ("%s: Invalid out hdr %zu : %zu\n" , __func__, + elem->out_sg[0 ].iov_len,sizeof (*out_hdr)); +continue ; + } + + out_hdr = elem->out_sg[0 ].iov_base; + +/* Bit 0 is reserved in virtio spec */ + msg.addr = out_hdr->addr >>1 ; + +/* Read Operation */ +if (elem->out_num ==1 && elem->in_num ==2 ) { + len = elem->in_sg[0 ].iov_len; +if (!len) {
Quoted 8 lines
tools/vhost-user-i2c/main.c line 439
Quoted 3 lines
tools/vhost-user-i2c/main.c new file +3
-0+g_warning ("%s: Read buffer length can't be zero\n" , __func__); +continue ; + }It looks like you are not handling endianness conversion here. As far as I can tell, the protocol requires little-endian data, but the code might run on a big-endian CPU.
I hope this is all we are required to do here, right ?
@@ -442,7 +421,7 @@ static void vi2c_handle_ctrl(VuDev *dev, int qidx)
out_hdr = elem->out_sg[0].iov_base;
/* Bit 0 is reserved in virtio spec */- msg.addr = out_hdr->addr >> 1;
+ msg.addr = le16toh(out_hdr->addr) >> 1;
/* Read Operation */
if (elem->out_num == 1 && elem->in_num == 2) {
@@ -489,7 +468,7 @@ static void vi2c_handle_ctrl(VuDev *dev, int qidx)
in_hdr->status = fail_next ? VIRTIO_I2C_MSG_ERR : vi2c_xfer(dev, &msg);
if (in_hdr->status == VIRTIO_I2C_MSG_ERR) {
/* We need to fail remaining transfers as well */
- fail_next = out_hdr->flags & VIRTIO_I2C_FLAGS_FAIL_NEXT;
+ fail_next = le32toh(out_hdr->flags) & VIRTIO_I2C_FLAGS_FAIL_NEXT;
}These are the only fields we are passing apart from buf, which goes directly to the client device.
Signature · 1 line
viresh
Re: [PATCH 3/5] tools/vhost-user-i2c: Add backend driver
Quoted 27 lines · Viresh Kumar
Quoted 6 lines · Arnd Bergmann
On Wed, Mar 24, 2021 at 8:33 AM Viresh Kumar <[email protected]> wrote:
It looks like you are not handling endianness conversion here. As far as I can tell, the protocol requires little-endian data, but the code might run on a big-endian CPU.
I hope this is all we are required to do here, right ?
(quoted diff) +2
-2@@ -442,7 +421,7 @@ static void vi2c_handle_ctrl(VuDev *dev, int qidx) out_hdr = elem->out_sg[0].iov_base; /* Bit 0 is reserved in virtio spec */ - msg.addr = out_hdr->addr >> 1; + msg.addr = le16toh(out_hdr->addr) >> 1; /* Read Operation */ if (elem->out_num == 1 && elem->in_num == 2) {@@ -489,7 +468,7 @@ static void vi2c_handle_ctrl(VuDev *dev, int qidx) in_hdr->status = fail_next ? VIRTIO_I2C_MSG_ERR : vi2c_xfer(dev, &msg); if (in_hdr->status == VIRTIO_I2C_MSG_ERR) { /* We need to fail remaining transfers as well */ - fail_next = out_hdr->flags & VIRTIO_I2C_FLAGS_FAIL_NEXT; + fail_next = le32toh(out_hdr->flags) & VIRTIO_I2C_FLAGS_FAIL_NEXT; }
Quoted 2 lines
These are the only fields we are passing apart from buf, which goes directly to the client device.
I think so, the in_hdr is only one byte long, so it doesn't have an endianness.
Arnd
PATCH 4/5 docs: add a man page for vhost-user-i2c
In reply to Viresh KumarBasic usage and example invocation.
--- docs/tools/index.rst | 1 + docs/tools/vhost-user-i2c.rst | 75 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 docs/tools/vhost-user-i2c.rst
docs/tools/index.rst +1 -0
@@ -17,3 +17,4 @@ QEMU Tools Guide qemu-trace-stap virtfs-proxy-helper virtiofsd + vhost-user-i2c
docs/tools/vhost-user-i2c.rst new file +75 -0
75 lines not on this page · raw message
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
PATCH 5/5 MAINTAINERS: Add entry for virtio-i2c
In reply to Viresh KumarThis patch adds entry for virtio-i2c related files in MAINTAINERS.
--- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
@@ -1967,6 +1967,15 @@ F: hw/virtio/virtio-mem-pci.h F: hw/virtio/virtio-mem-pci.c F: include/hw/virtio/virtio-mem.h +virtio-i2c +M: Viresh Kumar <[email protected]> +S: Supported +F: docs/tools/vhost-user-i2c.rst +F: hw/virtio/vhost-user-i2c.c +F: hw/virtio/vhost-user-i2c-pci.c +F: include/hw/virtio/vhost-user-i2c.h +F: tools/vhost-user-i2c/* + nvme M: Keith Busch <[email protected]> M: Klaus Jensen <[email protected]>
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Type: series
Message-id: [email protected]
Subject: [PATCH 0/5] virtio: Implement generic vhost-user-i2c backend
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
40ccc27 MAINTAINERS: Add entry for virtio-i2c 9b632f4 docs: add a man page for vhost-user-i2c 5da2cca tools/vhost-user-i2c: Add backend driver 6716f9a hw/virtio: add vhost-user-i2c-pci boilerplate d6439eb hw/virtio: add boilerplate for vhost-user-i2c device
=== OUTPUT BEGIN ===
1/5 Checking commit d6439ebcaa1d (hw/virtio: add boilerplate for vhost-user-i2c device) Use of uninitialized value $acpi_testexpected in string eq at ./scripts/checkpatch.pl line 1529.
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#46:
new file mode 100644
total: 0 errors, 1 warnings, 344 lines checked
Patch 1/5 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
2/5 Checking commit 6716f9a6b4c1 (hw/virtio: add vhost-user-i2c-pci boilerplate) Use of uninitialized value $acpi_testexpected in string eq at ./scripts/checkpatch.pl line 1529.
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#31:
new file mode 100644
total: 0 errors, 1 warnings, 86 lines checked
Patch 2/5 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
3/5 Checking commit 5da2ccae459c (tools/vhost-user-i2c: Add backend driver)
Use of uninitialized value $acpi_testexpected in string eq at ./scripts/checkpatch.pl line 1529.
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#52:
new file mode 100644
ERROR: spaces required around that '*' (ctx:WxV)
#137: FILE: tools/vhost-user-i2c/main.c:70:
+ const typeof(((type *) 0)->member) *__mptr = (ptr); \
^ERROR: space required after that ';' (ctx:VxV)
#138: FILE: tools/vhost-user-i2c/main.c:71:
+ (type *) ((char *) __mptr - offsetof(type, member));})
^
ERROR: line over 90 characters
#163: FILE: tools/vhost-user-i2c/main.c:96:
+ { "socket-path", 's', 0, G_OPTION_ARG_FILENAME, &socket_path, "Location of vhost-user Unix domain socket, incompatible with --fd", "PATH" },
ERROR: line over 90 characters
#164: FILE: tools/vhost-user-i2c/main.c:97:
+ { "fd", 'f', 0, G_OPTION_ARG_INT, &socket_fd, "Specify the file-descriptor of the backend, incompatible with --socket-path", "FD" },
ERROR: line over 90 characters
#165: FILE: tools/vhost-user-i2c/main.c:98:
+ { "device-list", 'l', 0, G_OPTION_ARG_STRING, &device_list, "List of i2c-dev bus and attached devices", "I2C Devices" },
ERROR: line over 90 characters
#166: FILE: tools/vhost-user-i2c/main.c:99:
+ { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE, &print_cap, "Output to stdout the backend capabilities in JSON format and exit", NULL},
WARNING: line over 80 characters
#167: FILE: tools/vhost-user-i2c/main.c:100:
+ { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be more verbose in output", NULL},
WARNING: line over 80 characters
#209: FILE: tools/vhost-user-i2c/main.c:142:
+ * Adapter | adapter2 | none | adapter1 | adapter3 | none | none| (val)
WARNING: line over 80 characters
#211: FILE: tools/vhost-user-i2c/main.c:144:
+ * Slave Address | addr 1 | none | addr 2 | addr 3 | none | none| (idx)
ERROR: do not use assignment in if condition
#241: FILE: tools/vhost-user-i2c/main.c:174:
+ if (addr < MAX_I2C_VDEV && ((idx = i2c->adapter_map[addr]) != 0)) {
ERROR: braces {} are necessary for all arms of this statement #288: FILE: tools/vhost-user-i2c/main.c:221:
+ if (bus < 0)
[...]
WARNING: line over 80 characters
#316: FILE: tools/vhost-user-i2c/main.c:249:
+ g_printerr("client addr 0x%x repeat, not allowed.\n", client_addr[i]);
ERROR: code indent should never use tabs
#340: FILE: tools/vhost-user-i2c/main.c:273:
+ * ^Ie.g. 2 for /dev/i2c-2$
ERROR: code indent should never use tabs
#342: FILE: tools/vhost-user-i2c/main.c:275:
+ * ^Ie.g. 0x1C or 1C$
ERROR: spaces required around that '==' (ctx:WxV)
#359: FILE: tools/vhost-user-i2c/main.c:292:
+ if (!cp || *cp =='\0') {
^ERROR: unnecessary whitespace before a quoted newline #364: FILE: tools/vhost-user-i2c/main.c:297:
+ g_printerr("too many adapter (%d), only support %d \n", n_adapter,
ERROR: spaces required around that '!=' (ctx:WxV)
#378: FILE: tools/vhost-user-i2c/main.c:311:
+ while (cp != NULL && *cp !='\0') {
^ERROR: braces {} are necessary for all arms of this statement #379: FILE: tools/vhost-user-i2c/main.c:312:
+ if (*cp == ':')
[...]
ERROR: unnecessary whitespace before a quoted newline #383: FILE: tools/vhost-user-i2c/main.c:316:
+ g_printerr("too many devices (%d), only support %d \n",
WARNING: line over 80 characters
#388: FILE: tools/vhost-user-i2c/main.c:321:
+ if (qemu_strtol(cp, &t, 16, &addr) || addr < 0 || addr > MAX_I2C_VDEV) {
WARNING: line over 80 characters
#400: FILE: tools/vhost-user-i2c/main.c:333:
+ i2c->adapter[n_adapter] = vi2c_create_adapter(bus, client_addr, n_client);
ERROR: braces {} are necessary for all arms of this statement #401: FILE: tools/vhost-user-i2c/main.c:334:
+ if (!i2c->adapter[n_adapter])
[...]
WARNING: line over 80 characters
#438: FILE: tools/vhost-user-i2c/main.c:371:
+ g_printerr("Failed to transfer data to address %x : %d\n", msg->addr, errno);
ERROR: that open brace { should be on the previous line #632: FILE: tools/vhost-user-i2c/main.c:565:
+ if (!g_option_context_parse(context, &argc, &argv, &error))
+ {
WARNING: line over 80 characters
#653: FILE: tools/vhost-user-i2c/main.c:586:
+ G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR,
ERROR: line over 90 characters
#664: FILE: tools/vhost-user-i2c/main.c:597:
+ g_autoptr(GSocket) bind_socket = g_socket_new(G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM,
WARNING: line over 80 characters
#665: FILE: tools/vhost-user-i2c/main.c:598:
+ G_SOCKET_PROTOCOL_DEFAULT, &error);
total: 18 errors, 10 warnings, 686 lines checked
Patch 3/5 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
4/5 Checking commit 9b632f47c605 (docs: add a man page for vhost-user-i2c)
Use of uninitialized value $acpi_testexpected in string eq at ./scripts/checkpatch.pl line 1529.
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
#26:
new file mode 100644
total: 0 errors, 1 warnings, 79 lines checked
Patch 4/5 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
5/5 Checking commit 40ccc271213e (MAINTAINERS: Add entry for virtio-i2c) === OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]
Quoted 9 lines · On 24-03-21, 00:42, [email protected] wrote:
Patchew URL: https://patchew.org/QEMU/[email protected]/
=== TEST SCRIPT BEGIN === #!/bin/bash
git rev-parse base >/dev/null|| exit 0 git config --local diff.renamelimit 0 git config --local diff.renames True git config --local diff.algorithm histogram./scripts/checkpatch.pl--mailback base..
I missed the fact that we have an implementation of checkpatch here as well. I have updated the patches already after running checkpatch at my end now, and will fix them while sending V2.
Signature · 1 line
viresh