Skip to contentSkip to conversation

i2c: virtio: add a virtio i2c frontend driver

PATCH Applied Jie Deng 4 participants 8 messages Jul 23, 2021 – Aug 18, 2021

Subsystems (3)
lore
Share & tools
lore

Jie Deng

author
#0 lore raw

PATCH v15 1/1 i2c: virtio: add a virtio i2c frontend driver

Add an I2C bus driver for virtio para-virtualization.

The controller can be emulated by the backend driver in any device model software by following the virtio protocol.

The device specification can be found on
https://lists.oasis-open.org/archives/virtio-comment/202101/msg00008.html.

By following the specification, people may implement different backend drivers to emulate different controllers according to their needs.

---
Changes v14 -> v15
- Minor fixes in Makefile and Kconfig.
- Add an i2c_adapter_quirks to filter out 0 length messages.
- Update the MAINTAINERS.

Changes v13 -> v14
- Put the headers in virtio_i2c.h in alphabetical order.
- Dropped I2C_FUNC_SMBUS_QUICK support.
- Dropped few unnecessary variables and checks.
- Use "num" everywhere instead of num or nr, to be consistent.
- Added few comments which make the design more clear.

Changes v12 -> v13
- Use _BITUL() instead of BIT().
- Rename "virtio_i2c_send_reqs" to "virtio_i2c_prepare_reqs".
- Optimize the return value of "virtio_i2c_complete_reqs".

Changes v11 -> v12
- Do not sent msg_buf for zero-length request.
- Send requests to host only if all the number of transfers requested prepared successfully.
- Remove the line #include <linux/bits.h> in virtio_i2c.h.

Changes v10 -> v11
- Remove vi->adap.class = I2C_CLASS_DEPRECATED.
- Use #ifdef CONFIG_PM_SLEEP to replace the "__maybe_unused".
- Remove "struct mutex lock" in "struct virtio_i2c".
- Support zero-length request.
- Remove unnecessary logs.
- Remove vi->adap.timeout = HZ / 10, just use the default value.
- Use BIT(0) to define VIRTIO_I2C_FLAGS_FAIL_NEXT.
- Add the virtio_device index to adapter's naming mechanism.

 MAINTAINERS                     |   9 ++
 drivers/i2c/busses/Kconfig      |  11 ++
 drivers/i2c/busses/Makefile     |   1 +
 drivers/i2c/busses/i2c-virtio.c | 290 ++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_i2c.h |  41 ++++++
 include/uapi/linux/virtio_ids.h |   1 +
 6 files changed, 353 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-virtio.c
 create mode 100644 include/uapi/linux/virtio_i2c.h
MAINTAINERS +9 -0
index 6c8be73..0183c50 100644
@@ -19707,6 +19707,15 @@ S:	Maintained
 F:	include/uapi/linux/virtio_snd.h
 F:	sound/virtio/*
 
+VIRTIO I2C DRIVER
+M:	Jie Deng <[email protected]>
+M:	Viresh Kumar <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	drivers/i2c/busses/i2c-virtio.c
+F:	include/uapi/linux/virtio_i2c.h
+
 VIRTUAL BOX GUEST DEVICE DRIVER
 M:	Hans de Goede <[email protected]>
 M:	Arnd Bergmann <[email protected]>
drivers/i2c/busses/Kconfig +11 -0
index 10acece..cbaf199 100644
@@ -1402,4 +1402,15 @@ config I2C_FSI
 	  This driver can also be built as a module. If so, the module will be
 	  called as i2c-fsi.
 
+config I2C_VIRTIO
+        tristate "Virtio I2C Adapter"
+        select VIRTIO
+        help
+          If you say yes to this option, support will be included for the virtio
+          I2C adapter driver. The hardware can be emulated by any device model
+          software according to the virtio protocol.
+
+          This driver can also be built as a module. If so, the module
+          will be called i2c-virtio.
+
 endmenu
drivers/i2c/busses/Makefile +1 -0
index 69e9963..1e8c0ba 100644
@@ -146,5 +146,6 @@ obj-$(CONFIG_I2C_SIBYTE)	+= i2c-sibyte.o
 obj-$(CONFIG_I2C_XGENE_SLIMPRO) += i2c-xgene-slimpro.o
 obj-$(CONFIG_SCx200_ACB)	+= scx200_acb.o
 obj-$(CONFIG_I2C_FSI)		+= i2c-fsi.o
+obj-$(CONFIG_I2C_VIRTIO)	+= i2c-virtio.o
 
 ccflags-$(CONFIG_I2C_DEBUG_BUS) := -DDEBUG
drivers/i2c/busses/i2c-virtio.c new file +290 -0

290 lines not on this page · raw message

include/uapi/linux/virtio_i2c.h new file +41 -0

41 lines not on this page · raw message

include/uapi/linux/virtio_ids.h +1 -0
index 70a8057a..99aa27b 100644
@@ -55,6 +55,7 @@
 #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 */
 #define VIRTIO_ID_BT			40 /* virtio bluetooth */
 
 /*
Signature · 6 lines
2.7.4

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#1 lore raw
Quoted 8 lines · Jie Deng
drivers/i2c/busses/i2c-virtio.c new file +8 -0
+
+       ret = virtio_i2c_setup_vqs(vi);
+       if (ret)
+               return ret;
+
+       vi->adap.owner = THIS_MODULE;
+       snprintf(vi->adap.name, sizeof(vi->adap.name),
+                "i2c_virtio at virtio bus %d", vdev->index);

drivers/i2c/busses/i2c-virtio.c line 237

Quoted 10 lines
drivers/i2c/busses/i2c-virtio.c new file +10 -0
+       vi->adap.algo = &virtio_algorithm;
+       vi->adap.quirks = &virtio_i2c_quirks;
+       vi->adap.dev.parent = &vdev->dev;
+       i2c_set_adapdata(&vi->adap, vi);
+
+       /*
+        * Setup ACPI node for controlled devices which will be probed through
+        * ACPI.
+        */
+       ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(pdev));

Since there is now a generic way for virtio drivers to link up with OF device nodes, maybe this should be handled the same way in the virtio core rather than the driver?

include/uapi/linux/virtio_ids.h line 59

Quoted 9 lines

index 70a8057a..99aa27b 100644

include/uapi/linux/virtio_ids.h +1 -0
@@ -55,6 +55,7 @@
 #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 */
 #define VIRTIO_ID_BT                   40 /* virtio bluetooth */

This will now conflict with Viresh's patch that adds all the other IDs. Not sure if there is anything to be done about that.

Arnd

maintainer
#2 lore raw
Quoted 4 lines · Arnd Bergmann
Quoted 4 lines

index 70a8057a..99aa27b 100644

include/uapi/linux/virtio_ids.h +0 -0
@@ -55,6 +55,7 @@
Quoted 8 lines

include/uapi/linux/virtio_ids.h line 59

Quoted 5 lines
include/uapi/linux/virtio_ids.h +1 -0
 #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 */
 #define VIRTIO_ID_BT                   40 /* virtio bluetooth */

This will now conflict with Viresh's patch that adds all the other IDs. Not sure if there is anything to be done about that.

An easier way of avoiding all such conflicts can be:

- Michael applies my first patch (which sync's the device id's from specs) for 5.14-rc4. Rest of the patches can go for 5.15.

- And then Wolfram applies this series over rc4 instead of rc1.

Or we can leave the conflict there for Linus to handle.

Signature · 1 line
viresh
#3 lore raw
Quoted 4 lines
Quoted 3 lines

This will now conflict with Viresh's patch that adds all the other IDs. Not sure if there is anything to be done about that.

Quoted 8 lines

An easier way of avoiding all such conflicts can be:

- Michael applies my first patch (which sync's the device id's from specs) for 5.14-rc4. Rest of the patches can go for 5.15.

- And then Wolfram applies this series over rc4 instead of rc1.

Or we can leave the conflict there for Linus to handle.

Or Michael creates an immutable branch for me to pull in. I am fine with either approach. A bit in favor of the immutable branch.

Other than that, I am happy with the virtio driver itself and ready to apply it once we decide how to handle the dependency.

Thank you everyone for the hard work and the patience!

author
#4 lore raw
In reply to Arnd Bergmann
Quoted 15 lines · Arnd Bergmann
Quoted 13 lines · Jie Deng
drivers/i2c/busses/i2c-virtio.c new file +13 -0
+
+       ret = virtio_i2c_setup_vqs(vi);
+       if (ret)
+               return ret;
+
+       vi->adap.owner = THIS_MODULE;
+       snprintf(vi->adap.name, sizeof(vi->adap.name),
+                "i2c_virtio at virtio bus %d", vdev->index);
+       vi->adap.algo = &virtio_algorithm;
+       vi->adap.quirks = &virtio_i2c_quirks;
+       vi->adap.dev.parent = &vdev->dev;
+       i2c_set_adapdata(&vi->adap, vi);
+
Quoted 8 lines

drivers/i2c/busses/i2c-virtio.c line 237

Quoted 5 lines
drivers/i2c/busses/i2c-virtio.c new file +5 -0
+       /*
+        * Setup ACPI node for controlled devices which will be probed through
+        * ACPI.
+        */
+       ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(pdev));

Since there is now a generic way for virtio drivers to link up with OF device nodes, maybe this should be handled the same way in the virtio core rather than the driver?

I'm currently based on the I2C tree. Has that patch been already merged ?

Anyway, I think we can send an additional patch to remove this line once that

"generic way" patch is merged.

Regards,

Jie

maintainer
#5 lore raw
Quoted 27 lines · Jie Deng
Quoted 15 lines · Arnd Bergmann
Quoted 13 lines · Jie Deng
drivers/i2c/busses/i2c-virtio.c new file +13 -0
+
+       ret = virtio_i2c_setup_vqs(vi);
+       if (ret)
+               return ret;
+
+       vi->adap.owner = THIS_MODULE;
+       snprintf(vi->adap.name, sizeof(vi->adap.name),
+                "i2c_virtio at virtio bus %d", vdev->index);
+       vi->adap.algo = &virtio_algorithm;
+       vi->adap.quirks = &virtio_i2c_quirks;
+       vi->adap.dev.parent = &vdev->dev;
+       i2c_set_adapdata(&vi->adap, vi);
+
Quoted 8 lines

drivers/i2c/busses/i2c-virtio.c line 237

Quoted 5 lines
drivers/i2c/busses/i2c-virtio.c new file +5 -0
+       /*
+        * Setup ACPI node for controlled devices which will be probed through
+        * ACPI.
+        */
+       ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(pdev));

Since there is now a generic way for virtio drivers to link up with OF device nodes, maybe this should be handled the same way in the virtio core rather than the driver?

Quoted 6 lines

I'm currently based on the I2C tree. Has that patch been already merged ?

Anyway, I think we can send an additional patch to remove this line once that

"generic way" patch is merged.

I agree, we need to get this merged and do stuff on top of it. We are already on v15 :)

Signature · 1 line
viresh
Applied
#6 lore raw
In reply to Jie Deng
Quoted 12 lines · Jie Deng

Add an I2C bus driver for virtio para-virtualization.

The controller can be emulated by the backend driver in any device model software by following the virtio protocol.

The device specification can be found on
https://lists.oasis-open.org/archives/virtio-comment/202101/msg00008.html.

By following the specification, people may implement different backend drivers to emulate different controllers according to their needs.

Quoted 6 lines

Okay, with rc6 being released, I won't wait for an immutable branch anymore. I applied this now and we will see if there will be a merge conflict. If so, it will be trivial to handle, I'd think. So:

Applied to for-next, thanks!

author
#7 lore raw
Quoted 7 lines · Wolfram Sang

Michael S. Tsirkin <[email protected]>
Okay, with rc6 being released, I won't wait for an immutable branch anymore. I applied this now and we will see if there will be a merge conflict. If so, it will be trivial to handle, I'd think. So:

Applied to for-next, thanks!

Thanks Wolfram!