virtio.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. *
  63. * Locking rules are straightforward: the driver is responsible for
  64. * locking. No two operations may be invoked simultaneously, with the exception
  65. * of virtqueue_disable_cb.
  66. *
  67. * All operations can be called in any context.
  68. */
  69. int virtqueue_add_buf_gfp(struct virtqueue *vq,
  70. struct scatterlist sg[],
  71. unsigned int out_num,
  72. unsigned int in_num,
  73. void *data,
  74. gfp_t gfp);
  75. static inline int virtqueue_add_buf(struct virtqueue *vq,
  76. struct scatterlist sg[],
  77. unsigned int out_num,
  78. unsigned int in_num,
  79. void *data)
  80. {
  81. return virtqueue_add_buf_gfp(vq, sg, out_num, in_num, data, GFP_ATOMIC);
  82. }
  83. void virtqueue_kick(struct virtqueue *vq);
  84. void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
  85. void virtqueue_disable_cb(struct virtqueue *vq);
  86. bool virtqueue_enable_cb(struct virtqueue *vq);
  87. bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
  88. void *virtqueue_detach_unused_buf(struct virtqueue *vq);
  89. /**
  90. * virtio_device - representation of a device using virtio
  91. * @index: unique position on the virtio bus
  92. * @dev: underlying device.
  93. * @id: the device type identification (used to match it with a driver).
  94. * @config: the configuration ops for this device.
  95. * @vqs: the list of virtqueues for this device.
  96. * @features: the features supported by both driver and device.
  97. * @priv: private pointer for the driver's use.
  98. */
  99. struct virtio_device {
  100. int index;
  101. struct device dev;
  102. struct virtio_device_id id;
  103. struct virtio_config_ops *config;
  104. struct list_head vqs;
  105. /* Note that this is a Linux set_bit-style bitmap. */
  106. unsigned long features[1];
  107. void *priv;
  108. };
  109. #define dev_to_virtio(dev) container_of(dev, struct virtio_device, dev)
  110. int register_virtio_device(struct virtio_device *dev);
  111. void unregister_virtio_device(struct virtio_device *dev);
  112. /**
  113. * virtio_driver - operations for a virtio I/O driver
  114. * @driver: underlying device driver (populate name and owner).
  115. * @id_table: the ids serviced by this driver.
  116. * @feature_table: an array of feature numbers supported by this device.
  117. * @feature_table_size: number of entries in the feature table array.
  118. * @probe: the function to call when a device is found. Returns 0 or -errno.
  119. * @remove: the function when a device is removed.
  120. * @config_changed: optional function to call when the device configuration
  121. * changes; may be called in interrupt context.
  122. */
  123. struct virtio_driver {
  124. struct device_driver driver;
  125. const struct virtio_device_id *id_table;
  126. const unsigned int *feature_table;
  127. unsigned int feature_table_size;
  128. int (*probe)(struct virtio_device *dev);
  129. void (*remove)(struct virtio_device *dev);
  130. void (*config_changed)(struct virtio_device *dev);
  131. };
  132. int register_virtio_driver(struct virtio_driver *drv);
  133. void unregister_virtio_driver(struct virtio_driver *drv);
  134. #endif /* _LINUX_VIRTIO_H */