virtio: Implement generic vhost-user-i2c backend
v2 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]/
I2C 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)SMBUS Testing:
--------------
I wasn't required to have such a tedious setup for testing out with SMBUS devices. I was able to emulate a SMBUS device on my x86 machine using i2c-stub driver.
$ modprobe i2c-stub chip_addr=0x20 //Boot the arm64 guest now with i2c-virtio driver and then do: $ echo al3320a 0x20 > /sys/class/i2c-adapter/i2c-0/new_device $ cat /sys/bus/iio/devices/iio:device0/in_illuminance_raw
That's it.
I hope I was able to give a clear picture of my test setup here :)
Thanks.
V1->V2:
- Add a separate patch (not to be merged) to keep stuff temporarily taken from Linux kernel.
- Support SMBUS devices/busses in the backend daemon.
- Fix lots of checkpatch warnings/errors.
- Some other bug fixes, suggestions, etc.
Viresh Kumar (6):
!Merge: Update virtio headers from kernel 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 | 69 ++ hw/virtio/vhost-user-i2c.c | 285 +++++++ include/hw/virtio/vhost-user-i2c.h | 37 + include/standard-headers/linux/virtio_i2c.h | 40 + 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 | 809 ++++++++++++++++++++ tools/vhost-user-i2c/meson.build | 10 + 14 files changed, 1356 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 include/standard-headers/linux/virtio_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 v2 1/6 !Merge: Update virtio headers from kernel
DO NOT MERGE
Update Linux virtio headers with help of
./scripts/update-linux-headers.sh ~/lsrc/linux.git
--- include/standard-headers/linux/virtio_i2c.h | 40 +++++++++++++++++++++ include/standard-headers/linux/virtio_ids.h | 1 + 2 files changed, 41 insertions(+) create mode 100644 include/standard-headers/linux/virtio_i2c.h
include/standard-headers/linux/virtio_i2c.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later WITH Linux-syscall-note */ +/* +* Definitions for virtio I2C Adpter +* +* Copyright (c) 2021 Intel Corporation. All rights reserved. +*/ + +#ifndef _LINUX_VIRTIO_I2C_H +#define _LINUX_VIRTIO_I2C_H + +#include "standard-headers/linux/types.h" + +/* 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 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; +}; + +/** +* 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; +}; + +/* The final status written by the device */ +#define VIRTIO_I2C_MSG_OK 0 +#define VIRTIO_I2C_MSG_ERR 1 + +#endif /* _LINUX_VIRTIO_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_ADAPTER 34/* virtio i2c adapter */ #endif /* _LINUX_VIRTIO_IDS_H */
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
PATCH v2 2/6 hw/virtio: add boilerplate for vhost-user-i2c device
In reply to Viresh KumarThis 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 | 285 +++++++++++++++++++++++++++++ include/hw/virtio/vhost-user-i2c.h | 37 ++++ 4 files changed, 328 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 +285 -0
285 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 */
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
Re: [PATCH V2 2/6] hw/virtio: add boilerplate for vhost-user-i2c device
Quoted 4 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]>
Signature · 1 line
Alex Bennée
PATCH v2 3/6 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 | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 70 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 +69 -0
69 lines not on this page · raw message
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
PATCH v2 4/6 tools/vhost-user-i2c: Add backend driver
In reply to Viresh KumarThis adds the vhost-user backend driver to support virtio based I2C and SMBUS devices.
vhost-user-i2c --help
--- tools/meson.build | 8 + tools/vhost-user-i2c/50-qemu-i2c.json.in | 5 + tools/vhost-user-i2c/main.c | 809 +++++++++++++++++++++++ tools/vhost-user-i2c/meson.build | 10 + 4 files changed, 832 insertions(+) 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
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 +809 -0
809 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 V2 4/6] tools/vhost-user-i2c: Add backend driver
Quoted 6 lines · Viresh Kumar
This adds the vhost-user backend driver to support virtio based I2C and SMBUS devices.
vhost-user-i2c --help
Signed-off-by Viresh Kumar <[email protected]>
<snip>
tools/vhost-user-i2c/main.c line 197
Quoted 6 lines
tools/vhost-user-i2c/main.c new file +6
-0+ +static VI2cAdapter *vi2c_create_adapter (int32_t bus,uint16_t client_addr[], +int32_t n_client) +{ +VI2cAdapter *adapter; +char path[20 ];
If you use:
g_autofree char *path = NULL;
If fact you could also use g_autofree with adapter to make you exit case a little easier.
tools/vhost-user-i2c/main.c line 205
Quoted 8 lines
tools/vhost-user-i2c/main.c new file +8
-0+uint64_t funcs; +int32_t fd, i; + +if (bus <0 ) { +return NULL ; + } + + adapter =g_malloc0 (sizeof (*adapter));
g_malloc will abort() on lack of memory so you can skip the check here.
tools/vhost-user-i2c/main.c line 212
Quoted 7 lines
tools/vhost-user-i2c/main.c new file +7
-0+if (!adapter) { +g_printerr ("failed to alloc adapter" ); +return NULL ; + } + +snprintf (path,sizeof (path),"/dev/i2c-%d" , bus); + path[sizeof (path) -1 ] ='\0' ;
then this becomes:
path = g_strdup_printf("/dev/i2c-%d", bus);Quoted 39 lines
tools/vhost-user-i2c/main.c new file +39
-0+ + fd =open (path,O_RDWR ); +if (fd <0 ) { +g_printerr ("virtio_i2c: failed to open %s\n" , path); + goto fail; + } + + adapter->fd = fd; + adapter->bus = bus; + +if (ioctl (fd,I2C_FUNCS , &funcs) <0 ) { +g_printerr ("virtio_i2c: failed to get functionality %s: %d\n" , path, + errno); + goto close_fd; + } + +if (funcs &I2C_FUNC_I2C ) { + adapter->smbus = false; + }else if (funcs &I2C_FUNC_SMBUS_WORD_DATA ) { + adapter->smbus = true; + }else { +g_printerr ("virtio_i2c: invalid functionality %lx\n" , funcs); + goto close_fd; + } + +for (i =0 ; i < n_client; i++) { +if (client_addr[i]) { +if (!vi2c_set_client_addr (adapter, client_addr[i])) { + goto close_fd; + } + +if (adapter->clients[client_addr[i]]) { +g_printerr ("client addr 0x%x repeat, not allowed.\n" , + client_addr[i]); + goto close_fd; + } + + adapter->clients[client_addr[i]] = true; +if (verbose) {
tools/vhost-user-i2c/main.c line 261
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+g_print ("Added client 0x%x to bus %u\n" , client_addr[i], bus); + } + } + } + +if (verbose) { +g_print ("Added adapter: bus: %d, func %s\n" , bus, + adapter->smbus ?"smbus" :"i2c" ); + } +return adapter;
This would then be:
return g_steal_pointer(adapter);
Quoted 23 lines
tools/vhost-user-i2c/main.c new file +23
-0+ +close_fd: +close (fd); +fail: +g_free (adapter); +return NULL ; +} + +/* +* Virtio I2C device list format. +* +* <bus>:<client_addr>[:<client_addr>], +* [<bus>:<client_addr>[:<client_addr>]] +* +* bus (dec): adatper bus number. +* e.g. 2 for /dev/i2c-2 +* client_addr (hex): address for client device +* e.g. 0x1C or 1C +* +* Example: --device-list="2:0x1c:0x20,3:0x10:0x2c" +* +* Note: client address can not repeat. +*/
tools/vhost-user-i2c/main.c line 294
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+static int32_t 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,"," );
The glib approach would be to use g_strsplit(device_list, ",", 0) which you can then iterate over with something like:
for (i = 0; cp[i] != NULL; i++) {
}
g_strfreev(cp);Quoted 20 lines
tools/vhost-user-i2c/main.c new file +20
-0+ +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
Quoted 1 line
one */
Then this would be:
**dev = g_strsplit(cp[i], ":", 2);
if (dev && dev[0]) {
if (dev[1]) {
}
}
g_freestrv(dev);which would avoid you manually iterating over the string.
Quoted 49 lines
tools/vhost-user-i2c/main.c new file +49
-0+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]) { + goto out; + } + + n_adapter++; + } + +if (!n_adapter) { +g_printerr ("Failed to add any adapters\n" ); +return -1 ; + } + + i2c->adapter_num = n_adapter; + +if (!vi2c_map_adapters (i2c)) { +return 0 ; + } + +out: +vi2c_remove_adapters (i2c); +return -1 ; +} +
<snip>
Quoted 9 lines
tools/vhost-user-i2c/main.c new file +9
-0+ +static void vi2c_handle_ctrl (VuDev *dev,int qidx) +{ +VuVirtq *vq =vu_get_queue (dev, qidx); +struct virtio_i2c_out_hdr *out_hdr; +size_t i, count =0 , len, in_hdr_len; +struct i2c_msg *msgs; +VuVirtqElement *elem; +uint8_t status;
tools/vhost-user-i2c/main.c line 542
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+struct { +struct virtio_i2c_in_hdr *in_hdr; +VuVirtqElement *elem; +size_t size; + } *info; + +/* Count number of messages */ +do { + elem =vu_queue_pop (dev, vq,sizeof (VuVirtqElement)); + }while (elem && ++count);
Can you expand on this comment as to why we can't process each message as we come to it?
Quoted 11 lines
tools/vhost-user-i2c/main.c new file +11
-0+ +if (!count) { +if (verbose) { +g_printerr ("Virtqueue can't have 0 elements\n" ); + } +return ; + } + +/* Rewind everything, now that we have counted the number of messages */ +vu_queue_rewind (dev, vq, count); +
tools/vhost-user-i2c/main.c line 563
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+if (verbose) { +g_print ("Received %ld messages in virtqueue\n" , count); + } + +/* Allocate memory for msgs and info */ + msgs =g_malloc0_n (count,sizeof (*msgs) +sizeof (*info)); +if (!msgs) { +g_printerr ("failed to allocate space for messages\n" ); +return ; + }
g_malloc0_n will abort() or succeed. Could this be a g_new and be typed in some way?
tools/vhost-user-i2c/main.c line 571
Quoted 8 lines
tools/vhost-user-i2c/main.c new file +8
-0+ info = (void *)(msgs + count); + +for (i =0 ; i < count; i++) { + elem =vu_queue_pop (dev, vq,sizeof (VuVirtqElement)); +if (!elem) { +g_printerr ("Failed to pop element: %ld : %ld\n" , i, count); + goto out; + }
not withstanding the above comment this seems like an assert() because it should never fail?
Quoted 74 lines
tools/vhost-user-i2c/main.c new file +74
-0+ info[i].elem = elem; + info[i].size =sizeof (*info[i].in_hdr); + +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)); + goto out; + } + + out_hdr = elem->out_sg[0 ].iov_base; + +/* Bit 0 is reserved in virtio spec */ + msgs[i].addr =le16toh (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__); + goto out; + } + + msgs[i].buf = elem->in_sg[0 ].iov_base; + msgs[i].flags =I2C_M_RD ; + msgs[i].len = len; + + info[i].in_hdr = elem->in_sg[1 ].iov_base; + info[i].size += len; + in_hdr_len = elem->in_sg[1 ].iov_len; + }else if (elem->out_num ==2 && elem->in_num ==1 ) { +/* Write Operation */ + len = elem->out_sg[1 ].iov_len; +if (!len) { +g_warning ("%s: Write buffer length can't be zero\n" , __func__); + goto out; + } + + msgs[i].buf = elem->out_sg[1 ].iov_base; + msgs[i].flags =0 ; + msgs[i].len = len; + + info[i].in_hdr = elem->in_sg[0 ].iov_base; + in_hdr_len = elem->in_sg[0 ].iov_len; + }else { +g_warning ("%s: Transfer type not supported (in %d, out %d)\n" , + __func__, elem->in_num, elem->out_num); + goto out; + } + +/* Validate size of "in" header */ +if (in_hdr_len !=sizeof (*(info[i].in_hdr))) { +g_warning ("%s: Invalid in hdr %zu : %zu\n" , __func__, in_hdr_len, +sizeof (*(info[i].in_hdr))); + goto out; + } + } + + status =vi2c_xfer (dev, msgs, count); + +for (i =0 ; i < count; i++) { + info[i].in_hdr->status = status; +vu_queue_push (dev, vq, info[i].elem, info[i].size); + } + +vu_queue_notify (dev, vq); + +out: +g_free (msgs); +} +
<snip>
Quoted 6 lines
tools/vhost-user-i2c/meson.build new file +0
-0
tools/vhost-user-i2c/meson.build line 9
Quoted 9 lines
tools/vhost-user-i2c/meson.build new file +9
-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,
FYI this now needs to be:
configuration: { 'libexecdir' : get_option('prefix') / get_option('libexecdir') },
to properly pass libexecdir above.
Quoted 1 line
tools/vhost-user-i2c/meson.build new file +1
-0+ install_dir: qemu_datadir / 'vhost-user')
Signature · 1 line
Alex Bennée
Re: [PATCH V2 4/6] tools/vhost-user-i2c: Add backend driver
Quoted 6 lines · Alex Bennée
Quoted 2 lines
+ /* Parse clients <client_addr>[:<client_addr>] entries one by
one */Then this would be:
**dev = g_strsplit(cp[i], ":", 2);
There can be any number of client devices present for a bus, not just 2.
Signature · 1 line
viresh
Re: [PATCH V2 4/6] tools/vhost-user-i2c: Add backend driver
In reply to Viresh Kumartools/vhost-user-i2c/main.c line 43
Quoted 5 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +5
-0+ +/* vhost-user-i2c definitions */ + +#define MAX_I2C_VDEV (1 << 7) +#define MAX_I2C_ADAPTER 16
Generally speaking, 16 is big enough for most cases. But comparing with static configuration, I think it is better if we can check how many adapters in the host when doing initialization and use that number as "MAX_I2C_ADAPTER".
Quoted 30 lines
tools/vhost-user-i2c/main.c new file +30
-0+ +static VI2cAdapter *vi2c_create_adapter (int32_t bus,uint16_t client_addr[], +int32_t n_client) +{ +VI2cAdapter *adapter; +char path[20 ]; +uint64_t funcs; +int32_t fd, i; + +if (bus <0 ) { +return NULL ; + } + + adapter =g_malloc0 (sizeof (*adapter)); +if (!adapter) { +g_printerr ("failed to alloc adapter" ); +return NULL ; + } + +snprintf (path,sizeof (path),"/dev/i2c-%d" , bus); + path[sizeof (path) -1 ] ='\0' ; + + fd =open (path,O_RDWR ); +if (fd <0 ) { +g_printerr ("virtio_i2c: failed to open %s\n" , path); + goto fail; + } + + adapter->fd = fd; + adapter->bus = bus;
tools/vhost-user-i2c/main.c line 231
Quoted 10 lines
tools/vhost-user-i2c/main.c new file +10
-0+ +if (ioctl (fd,I2C_FUNCS , &funcs) <0 ) { +g_printerr ("virtio_i2c: failed to get functionality %s: %d\n" , path, + errno); + goto close_fd; + } + +if (funcs &I2C_FUNC_I2C ) { + adapter->smbus = false; + }else if (funcs &I2C_FUNC_SMBUS_WORD_DATA ) {
Only I2C_FUNC_SMBUS_WORD_DATA is checked here. But in addition to it, the smbus_xfer seems support I2C_FUNC_SMBUS_BYTE, I2C_FUNC_SMBUS_BYTE_DATA. So if an adapter only support the latter two, it will never go to smbus_xfer.
Quoted 8 lines
tools/vhost-user-i2c/main.c new file +6
-0+ adapter->smbus = true; + }else { +g_printerr ("virtio_i2c: invalid functionality %lx\n" , funcs); + goto close_fd; + } +
Re: [PATCH V2 4/6] tools/vhost-user-i2c: Add backend driver
Quoted 13 lines · Jie Deng
tools/vhost-user-i2c/main.c line 43
Quoted 5 lines · Viresh Kumar
tools/vhost-user-i2c/main.c new file +5
-0+ +/* vhost-user-i2c definitions */ + +#define MAX_I2C_VDEV (1 << 7) +#define MAX_I2C_ADAPTER 16Generally speaking, 16 is big enough for most cases. But comparing with static configuration,
I think it is better if we can check how many adapters in the host when doing initialization and
use that number as "MAX_I2C_ADAPTER".
Hmm, so this doesn't limit the adapter number in /dev/i2cX (i.e. we can still process /dev/i2c99), but rather how many adapters we can pass in --device-list="" parameter. And so I feel 16 is big enough and if someone really needs more, then they can update the macro here.
Quoted 38 lines
Quoted 38 lines
tools/vhost-user-i2c/main.c new file +38
-0+static VI2cAdapter *vi2c_create_adapter (int32_t bus,uint16_t client_addr[], +int32_t n_client) +{ +VI2cAdapter *adapter; +char path[20 ]; +uint64_t funcs; +int32_t fd, i; + +if (bus <0 ) { +return NULL ; + } + + adapter =g_malloc0 (sizeof (*adapter)); +if (!adapter) { +g_printerr ("failed to alloc adapter" ); +return NULL ; + } + +snprintf (path,sizeof (path),"/dev/i2c-%d" , bus); + path[sizeof (path) -1 ] ='\0' ; + + fd =open (path,O_RDWR ); +if (fd <0 ) { +g_printerr ("virtio_i2c: failed to open %s\n" , path); + goto fail; + } + + adapter->fd = fd; + adapter->bus = bus; + +if (ioctl (fd,I2C_FUNCS , &funcs) <0 ) { +g_printerr ("virtio_i2c: failed to get functionality %s: %d\n" , path, + errno); + goto close_fd; + } + +if (funcs &I2C_FUNC_I2C ) { + adapter->smbus = false;
Quoted 8 lines
Quoted 1 line
+ } else if (funcs &
I2C_FUNC_SMBUS_WORD_DATA) {Only
I2C_FUNC_SMBUS_WORD_DATAis checked here. But in addition to it, thesmbus_xfer
seems supportI2C_FUNC_SMBUS_BYTE,I2C_FUNC_SMBUS_BYTE_DATA. So if an adapter only
support the latter two, it will never go tosmbus_xfer.
Yeah, I missed it. Thanks. I am updating it as:
- } else if (funcs & I2C_FUNC_SMBUS_WORD_DATA) {
+ } else if (funcs & (I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BYTE |
+ I2C_FUNC_SMBUS_BYTE_DATA)) {
Signature · 1 line
viresh
PATCH v2 4/6 tools/vhost-user-i2c: Add backend driver
In reply to Viresh KumarThis adds the vhost-user backend driver to support virtio based I2C and SMBUS devices.
vhost-user-i2c --help
---
V2->2.1
- Check for more smbus functionalities.
- Use better glibc helpers for string parsing.
- Remove code to check return value of memory allocation.
- Elaborate a comment.
- Minor update to meson.build
tools/meson.build | 8 + tools/vhost-user-i2c/50-qemu-i2c.json.in | 5 + tools/vhost-user-i2c/main.c | 800 +++++++++++++++++++++++ tools/vhost-user-i2c/meson.build | 10 + 4 files changed, 823 insertions(+) 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
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 +800 -0
800 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: { 'libexecdir' : get_option('prefix') / get_option('libexecdir') }, + install_dir: qemu_datadir / 'vhost-user')
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af
PATCH v2 5/6 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 v2 6/6 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
@@ -2136,6 +2136,15 @@ F: docs/interop/vhost-user-gpu.rst F: contrib/vhost-user-gpu F: hw/display/vhost-user-* +vhost-user-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/* + Cirrus VGA M: Gerd Hoffmann <[email protected]> S: Odd Fixes
Signature · 1 line
2.25.0.rc1.19.g042ed3e048af