virtio.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef _LINUX_VIRTIO_H
  2. #define _LINUX_VIRTIO_H
  3. /* Everything a virtio driver needs to work with any particular virtio
  4. * implementation. */
  5. #include <linux/types.h>
  6. #include <linux/scatterlist.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/device.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/gfp.h>
  11. /**
  12. * virtqueue - a queue to register buffers for sending or receiving.
  13. * @list: the chain of virtqueues for this device
  14. * @callback: the function to call when buffers are consumed (can be NULL).
  15. * @name: the name of this virtqueue (mainly for debugging)
  16. * @vdev: the virtio device this queue was created for.
  17. * @priv: a pointer for the virtqueue implementation to use.
  18. */
  19. struct virtqueue {
  20. struct list_head list;
  21. void (*callback)(struct virtqueue *vq);
  22. const char *name;
  23. struct virtio_device *vdev;
  24. void *priv;
  25. };
  26. /**
  27. * operations for virtqueue
  28. * virtqueue_add_buf: expose buffer to other end
  29. * vq: the struct virtqueue we're talking about.
  30. * sg: the description of the buffer(s).
  31. * out_num: the number of sg readable by other side
  32. * in_num: the number of sg which are writable (after readable ones)
  33. * data: the token identifying the buffer.
  34. * gfp: how to do memory allocations (if necessary).
  35. * Returns remaining capacity of queue (sg segments) or a negative error.
  36. * virtqueue_kick: update after add_buf
  37. * vq: the struct virtqueue
  38. * After one or more add_buf calls, invoke this to kick the other side.
  39. * virtqueue_get_buf: get the next used buffer
  40. * vq: the struct virtqueue we're talking about.
  41. * len: the length written into the buffer
  42. * Returns NULL or the "data" token handed to add_buf.
  43. * virtqueue_disable_cb: disable callbacks
  44. * vq: the struct virtqueue we're talking about.
  45. * Note that this is not necessarily synchronous, hence unreliable and only
  46. * useful as an optimization.
  47. * virtqueue_enable_cb: restart callbacks after disable_cb.
  48. * vq: the struct virtqueue we're talking about.
  49. * This re-enables callbacks; it returns "false" if there are pending
  50. * buffers in the queue, to detect a possible race between the driver
  51. * checking for more work, and enabling callbacks.
  52. * virtqueue_enable_cb_delayed: restart callbacks after disable_cb.
  53. * vq: the struct virtqueue we're talking about.
  54. * This re-enables callbacks but hints to the other side to delay
  55. * interrupts until most of the available buffers have been processed;
  56. * it returns "false" if there are many pending buffers in the queue,
  57. * to detect a possible race between the driver checking for more work,
  58. * and enabling callbacks.
  59. * virtqueue_detach_unused_buf: detach first unused buffer
  60. * vq: the struct virtqueue we're talking about.
  61. * Returns NULL or the "data" token handed to add_buf
  62. * virtqueue_get_vring_size: return the size of the virtqueue's vring
  63. * vq: the struct virtqueue containing the vring of interest.
  64. * Returns the size of the vring.
  65. *
  66. * Locking rules are straightforward: the driver is responsible for
  67. * locking. No two operations may be invoked simultaneously, with the exception
  68. * of virtqueue_disable_cb.
  69. *
  70. * All operations can be called in any context.
  71. */
  72. int virtqueue_add_buf_gfp(struct virtqueue *vq,
  73. struct scatterlist sg[],
  74. unsigned int out_num,
  75. unsigned int in_num,
  76. void *data,
  77. gfp_t gfp);
  78. static inline int virtqueue_add_buf(struct virtqueue *vq,
  79. struct scatterlist sg[],
  80. unsigned int out_num,
  81. unsigned int in_num,
  82. void *data)
  83. {
  84. return virtqueue_add_buf_gfp(vq, sg, out_num, in_num, data, GFP_ATOMIC);
  85. }
  86. void virtqueue_kick(struct virtqueue *vq);
  87. void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
  88. void virtqueue_disable_cb(struct virtqueue *vq);
  89. bool virtqueue_enable_cb(struct virtqueue *vq);
  90. bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
  91. void *virtqueue_detach_unused_buf(struct virtqueue *vq);
  92. unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
  93. /**
  94. * virtio_device - representation of a device using virtio
  95. * @index: unique position on the virtio bus
  96. * @dev: underlying device.
  97. * @id: the device type identification (used to match it with a driver).
  98. * @config: the configuration ops for this device.
  99. * @vqs: the list of virtqueues for this device.
  100. * @features: the features supported by both driver and device.
  101. * @priv: private pointer for the driver's use.
  102. */
  103. struct virtio_device {
  104. int index;
  105. struct device dev;
  106. struct virtio_device_id id;
  107. struct virtio_config_ops *config;
  108. struct list_head vqs;
  109. /* Note that this is a Linux set_bit-style bitmap. */
  110. unsigned long features[1];
  111. void *priv;
  112. };
  113. #define dev_to_virtio(dev) container_of(dev, struct virtio_device, dev)
  114. int register_virtio_device(struct virtio_device *dev);
  115. void unregister_virtio_device(struct virtio_device *dev);
  116. /**
  117. * virtio_driver - operations for a virtio I/O driver
  118. * @driver: underlying device driver (populate name and owner).
  119. * @id_table: the ids serviced by this driver.
  120. * @feature_table: an array of feature numbers supported by this device.
  121. * @feature_table_size: number of entries in the feature table array.
  122. * @probe: the function to call when a device is found. Returns 0 or -errno.
  123. * @remove: the function when a device is removed.
  124. * @config_changed: optional function to call when the device configuration
  125. * changes; may be called in interrupt context.
  126. */
  127. struct virtio_driver {
  128. struct device_driver driver;
  129. const struct virtio_device_id *id_table;
  130. const unsigned int *feature_table;
  131. unsigned int feature_table_size;
  132. int (*probe)(struct virtio_device *dev);
  133. void (*remove)(struct virtio_device *dev);
  134. void (*config_changed)(struct virtio_device *dev);
  135. };
  136. int register_virtio_driver(struct virtio_driver *drv);
  137. void unregister_virtio_driver(struct virtio_driver *drv);
  138. #endif /* _LINUX_VIRTIO_H */