virtio.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /**
  11. * virtqueue - a queue to register buffers for sending or receiving.
  12. * @list: the chain of virtqueues for this device
  13. * @callback: the function to call when buffers are consumed (can be NULL).
  14. * @name: the name of this virtqueue (mainly for debugging)
  15. * @vdev: the virtio device this queue was created for.
  16. * @vq_ops: the operations for this virtqueue (see below).
  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. struct virtqueue_ops *vq_ops;
  25. void *priv;
  26. };
  27. /**
  28. * virtqueue_ops - operations for virtqueue abstraction layer
  29. * @add_buf: expose buffer to other end
  30. * vq: the struct virtqueue we're talking about.
  31. * sg: the description of the buffer(s).
  32. * out_num: the number of sg readable by other side
  33. * in_num: the number of sg which are writable (after readable ones)
  34. * data: the token identifying the buffer.
  35. * Returns remaining capacity of queue (sg segments) or a negative error.
  36. * @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. * @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. * @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. * @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. * @detach_unused_buf: detach first unused buffer
  53. * vq: the struct virtqueue we're talking about.
  54. * Returns NULL or the "data" token handed to add_buf
  55. *
  56. * Locking rules are straightforward: the driver is responsible for
  57. * locking. No two operations may be invoked simultaneously, with the exception
  58. * of @disable_cb.
  59. *
  60. * All operations can be called in any context.
  61. */
  62. struct virtqueue_ops {
  63. int (*add_buf)(struct virtqueue *vq,
  64. struct scatterlist sg[],
  65. unsigned int out_num,
  66. unsigned int in_num,
  67. void *data);
  68. void (*kick)(struct virtqueue *vq);
  69. void *(*get_buf)(struct virtqueue *vq, unsigned int *len);
  70. void (*disable_cb)(struct virtqueue *vq);
  71. bool (*enable_cb)(struct virtqueue *vq);
  72. void *(*detach_unused_buf)(struct virtqueue *vq);
  73. };
  74. /**
  75. * virtio_device - representation of a device using virtio
  76. * @index: unique position on the virtio bus
  77. * @dev: underlying device.
  78. * @id: the device type identification (used to match it with a driver).
  79. * @config: the configuration ops for this device.
  80. * @vqs: the list of virtqueues for this device.
  81. * @features: the features supported by both driver and device.
  82. * @priv: private pointer for the driver's use.
  83. */
  84. struct virtio_device {
  85. int index;
  86. struct device dev;
  87. struct virtio_device_id id;
  88. struct virtio_config_ops *config;
  89. struct list_head vqs;
  90. /* Note that this is a Linux set_bit-style bitmap. */
  91. unsigned long features[1];
  92. void *priv;
  93. };
  94. int register_virtio_device(struct virtio_device *dev);
  95. void unregister_virtio_device(struct virtio_device *dev);
  96. /**
  97. * virtio_driver - operations for a virtio I/O driver
  98. * @driver: underlying device driver (populate name and owner).
  99. * @id_table: the ids serviced by this driver.
  100. * @feature_table: an array of feature numbers supported by this device.
  101. * @feature_table_size: number of entries in the feature table array.
  102. * @probe: the function to call when a device is found. Returns 0 or -errno.
  103. * @remove: the function when a device is removed.
  104. * @config_changed: optional function to call when the device configuration
  105. * changes; may be called in interrupt context.
  106. */
  107. struct virtio_driver {
  108. struct device_driver driver;
  109. const struct virtio_device_id *id_table;
  110. const unsigned int *feature_table;
  111. unsigned int feature_table_size;
  112. int (*probe)(struct virtio_device *dev);
  113. void (*remove)(struct virtio_device *dev);
  114. void (*config_changed)(struct virtio_device *dev);
  115. };
  116. int register_virtio_driver(struct virtio_driver *drv);
  117. void unregister_virtio_driver(struct virtio_driver *drv);
  118. #endif /* _LINUX_VIRTIO_H */