writing_virtio_drivers.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. _writing_virtio_drivers:
  3. ======================
  4. Writing Virtio Drivers
  5. ======================
  6. Introduction
  7. ============
  8. This document serves as a basic guideline for driver programmers that
  9. need to hack a new virtio driver or understand the essentials of the
  10. existing ones. See :ref:`Virtio on Linux <virtio>` for a general
  11. overview of virtio.
  12. Driver boilerplate
  13. ==================
  14. As a bare minimum, a virtio driver needs to register in the virtio bus
  15. and configure the virtqueues for the device according to its spec, the
  16. configuration of the virtqueues in the driver side must match the
  17. virtqueue definitions in the device. A basic driver skeleton could look
  18. like this::
  19. #include <linux/virtio.h>
  20. #include <linux/virtio_ids.h>
  21. #include <linux/virtio_config.h>
  22. #include <linux/module.h>
  23. /* device private data (one per device) */
  24. struct virtio_dummy_dev {
  25. struct virtqueue *vq;
  26. };
  27. static void virtio_dummy_recv_cb(struct virtqueue *vq)
  28. {
  29. struct virtio_dummy_dev *dev = vq->vdev->priv;
  30. char *buf;
  31. unsigned int len;
  32. while ((buf = virtqueue_get_buf(dev->vq, &len)) != NULL) {
  33. /* process the received data */
  34. }
  35. }
  36. static int virtio_dummy_probe(struct virtio_device *vdev)
  37. {
  38. struct virtio_dummy_dev *dev = NULL;
  39. /* initialize device data */
  40. dev = kzalloc(sizeof(struct virtio_dummy_dev), GFP_KERNEL);
  41. if (!dev)
  42. return -ENOMEM;
  43. /* the device has a single virtqueue */
  44. dev->vq = virtio_find_single_vq(vdev, virtio_dummy_recv_cb, "input");
  45. if (IS_ERR(dev->vq)) {
  46. kfree(dev);
  47. return PTR_ERR(dev->vq);
  48. }
  49. vdev->priv = dev;
  50. /* from this point on, the device can notify and get callbacks */
  51. virtio_device_ready(vdev);
  52. return 0;
  53. }
  54. static void virtio_dummy_remove(struct virtio_device *vdev)
  55. {
  56. struct virtio_dummy_dev *dev = vdev->priv;
  57. /*
  58. * disable vq interrupts: equivalent to
  59. * vdev->config->reset(vdev)
  60. */
  61. virtio_reset_device(vdev);
  62. /* detach unused buffers */
  63. while ((buf = virtqueue_detach_unused_buf(dev->vq)) != NULL) {
  64. kfree(buf);
  65. }
  66. /* remove virtqueues */
  67. vdev->config->del_vqs(vdev);
  68. kfree(dev);
  69. }
  70. static const struct virtio_device_id id_table[] = {
  71. { VIRTIO_ID_DUMMY, VIRTIO_DEV_ANY_ID },
  72. { 0 },
  73. };
  74. static struct virtio_driver virtio_dummy_driver = {
  75. .driver.name = KBUILD_MODNAME,
  76. .id_table = id_table,
  77. .probe = virtio_dummy_probe,
  78. .remove = virtio_dummy_remove,
  79. };
  80. module_virtio_driver(virtio_dummy_driver);
  81. MODULE_DEVICE_TABLE(virtio, id_table);
  82. MODULE_DESCRIPTION("Dummy virtio driver");
  83. MODULE_LICENSE("GPL");
  84. The device id ``VIRTIO_ID_DUMMY`` here is a placeholder, virtio drivers
  85. should be added only for devices that are defined in the spec, see
  86. include/uapi/linux/virtio_ids.h. Device ids need to be at least reserved
  87. in the virtio spec before being added to that file.
  88. If your driver doesn't have to do anything special in its ``init`` and
  89. ``exit`` methods, you can use the module_virtio_driver() helper to
  90. reduce the amount of boilerplate code.
  91. The ``probe`` method does the minimum driver setup in this case
  92. (memory allocation for the device data) and initializes the
  93. virtqueue. virtio_device_ready() is used to enable the virtqueue and to
  94. notify the device that the driver is ready to manage the device
  95. ("DRIVER_OK"). The virtqueues are anyway enabled automatically by the
  96. core after ``probe`` returns.
  97. .. kernel-doc:: include/linux/virtio_config.h
  98. :identifiers: virtio_device_ready
  99. In any case, the virtqueues need to be enabled before adding buffers to
  100. them.
  101. Sending and receiving data
  102. ==========================
  103. The virtio_dummy_recv_cb() callback in the code above will be triggered
  104. when the device notifies the driver after it finishes processing a
  105. descriptor or descriptor chain, either for reading or writing. However,
  106. that's only the second half of the virtio device-driver communication
  107. process, as the communication is always started by the driver regardless
  108. of the direction of the data transfer.
  109. To configure a buffer transfer from the driver to the device, first you
  110. have to add the buffers -- packed as `scatterlists` -- to the
  111. appropriate virtqueue using any of the virtqueue_add_inbuf(),
  112. virtqueue_add_outbuf() or virtqueue_add_sgs(), depending on whether you
  113. need to add one input `scatterlist` (for the device to fill in), one
  114. output `scatterlist` (for the device to consume) or multiple
  115. `scatterlists`, respectively. Then, once the virtqueue is set up, a call
  116. to virtqueue_kick() sends a notification that will be serviced by the
  117. hypervisor that implements the device::
  118. struct scatterlist sg[1];
  119. sg_init_one(sg, buffer, BUFLEN);
  120. virtqueue_add_inbuf(dev->vq, sg, 1, buffer, GFP_ATOMIC);
  121. virtqueue_kick(dev->vq);
  122. .. kernel-doc:: drivers/virtio/virtio_ring.c
  123. :identifiers: virtqueue_add_inbuf
  124. .. kernel-doc:: drivers/virtio/virtio_ring.c
  125. :identifiers: virtqueue_add_outbuf
  126. .. kernel-doc:: drivers/virtio/virtio_ring.c
  127. :identifiers: virtqueue_add_sgs
  128. Then, after the device has read or written the buffers prepared by the
  129. driver and notifies it back, the driver can call virtqueue_get_buf() to
  130. read the data produced by the device (if the virtqueue was set up with
  131. input buffers) or simply to reclaim the buffers if they were already
  132. consumed by the device:
  133. .. kernel-doc:: drivers/virtio/virtio_ring.c
  134. :identifiers: virtqueue_get_buf_ctx
  135. The virtqueue callbacks can be disabled and re-enabled using the
  136. virtqueue_disable_cb() and the family of virtqueue_enable_cb() functions
  137. respectively. See drivers/virtio/virtio_ring.c for more details:
  138. .. kernel-doc:: drivers/virtio/virtio_ring.c
  139. :identifiers: virtqueue_disable_cb
  140. .. kernel-doc:: drivers/virtio/virtio_ring.c
  141. :identifiers: virtqueue_enable_cb
  142. But note that some spurious callbacks can still be triggered under
  143. certain scenarios. The way to disable callbacks reliably is to reset the
  144. device or the virtqueue (virtio_reset_device()).
  145. References
  146. ==========
  147. _`[1]` Virtio Spec v1.2:
  148. https://docs.oasis-open.org/virtio/virtio/v1.2/virtio-v1.2.html
  149. Check for later versions of the spec as well.