virtio.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. int virtqueue_add_buf(struct virtqueue *vq,
  27. struct scatterlist sg[],
  28. unsigned int out_num,
  29. unsigned int in_num,
  30. void *data,
  31. gfp_t gfp);
  32. void virtqueue_kick(struct virtqueue *vq);
  33. void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
  34. void virtqueue_disable_cb(struct virtqueue *vq);
  35. bool virtqueue_enable_cb(struct virtqueue *vq);
  36. bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
  37. void *virtqueue_detach_unused_buf(struct virtqueue *vq);
  38. unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
  39. /**
  40. * virtio_device - representation of a device using virtio
  41. * @index: unique position on the virtio bus
  42. * @dev: underlying device.
  43. * @id: the device type identification (used to match it with a driver).
  44. * @config: the configuration ops for this device.
  45. * @vqs: the list of virtqueues for this device.
  46. * @features: the features supported by both driver and device.
  47. * @priv: private pointer for the driver's use.
  48. */
  49. struct virtio_device {
  50. int index;
  51. struct device dev;
  52. struct virtio_device_id id;
  53. struct virtio_config_ops *config;
  54. struct list_head vqs;
  55. /* Note that this is a Linux set_bit-style bitmap. */
  56. unsigned long features[1];
  57. void *priv;
  58. };
  59. #define dev_to_virtio(dev) container_of(dev, struct virtio_device, dev)
  60. int register_virtio_device(struct virtio_device *dev);
  61. void unregister_virtio_device(struct virtio_device *dev);
  62. /**
  63. * virtio_driver - operations for a virtio I/O driver
  64. * @driver: underlying device driver (populate name and owner).
  65. * @id_table: the ids serviced by this driver.
  66. * @feature_table: an array of feature numbers supported by this driver.
  67. * @feature_table_size: number of entries in the feature table array.
  68. * @probe: the function to call when a device is found. Returns 0 or -errno.
  69. * @remove: the function to call when a device is removed.
  70. * @config_changed: optional function to call when the device configuration
  71. * changes; may be called in interrupt context.
  72. */
  73. struct virtio_driver {
  74. struct device_driver driver;
  75. const struct virtio_device_id *id_table;
  76. const unsigned int *feature_table;
  77. unsigned int feature_table_size;
  78. int (*probe)(struct virtio_device *dev);
  79. void (*remove)(struct virtio_device *dev);
  80. void (*config_changed)(struct virtio_device *dev);
  81. };
  82. int register_virtio_driver(struct virtio_driver *drv);
  83. void unregister_virtio_driver(struct virtio_driver *drv);
  84. #endif /* _LINUX_VIRTIO_H */