1 #ifndef _LINUX_VIRTIO_H
2 #define _LINUX_VIRTIO_H
3 /* Everything a virtio driver needs to work with any particular virtio
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>
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.
20 struct list_head list;
21 void (*callback)(struct virtqueue *vq);
23 struct virtio_device *vdev;
28 * operations for virtqueue
29 * virtqueue_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 * 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_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
56 * Locking rules are straightforward: the driver is responsible for
57 * locking. No two operations may be invoked simultaneously, with the exception
58 * of virtqueue_disable_cb.
60 * All operations can be called in any context.
63 int virtqueue_add_buf(struct virtqueue *vq,
64 struct scatterlist sg[],
69 void virtqueue_kick(struct virtqueue *vq);
71 void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
73 void virtqueue_disable_cb(struct virtqueue *vq);
75 bool virtqueue_enable_cb(struct virtqueue *vq);
77 void *virtqueue_detach_unused_buf(struct virtqueue *vq);
80 * virtio_device - representation of a device using virtio
81 * @index: unique position on the virtio bus
82 * @dev: underlying device.
83 * @id: the device type identification (used to match it with a driver).
84 * @config: the configuration ops for this device.
85 * @vqs: the list of virtqueues for this device.
86 * @features: the features supported by both driver and device.
87 * @priv: private pointer for the driver's use.
89 struct virtio_device {
92 struct virtio_device_id id;
93 struct virtio_config_ops *config;
95 /* Note that this is a Linux set_bit-style bitmap. */
96 unsigned long features[1];
100 #define dev_to_virtio(dev) container_of(dev, struct virtio_device, dev)
101 int register_virtio_device(struct virtio_device *dev);
102 void unregister_virtio_device(struct virtio_device *dev);
105 * virtio_driver - operations for a virtio I/O driver
106 * @driver: underlying device driver (populate name and owner).
107 * @id_table: the ids serviced by this driver.
108 * @feature_table: an array of feature numbers supported by this device.
109 * @feature_table_size: number of entries in the feature table array.
110 * @probe: the function to call when a device is found. Returns 0 or -errno.
111 * @remove: the function when a device is removed.
112 * @config_changed: optional function to call when the device configuration
113 * changes; may be called in interrupt context.
115 struct virtio_driver {
116 struct device_driver driver;
117 const struct virtio_device_id *id_table;
118 const unsigned int *feature_table;
119 unsigned int feature_table_size;
120 int (*probe)(struct virtio_device *dev);
121 void (*remove)(struct virtio_device *dev);
122 void (*config_changed)(struct virtio_device *dev);
125 int register_virtio_driver(struct virtio_driver *drv);
126 void unregister_virtio_driver(struct virtio_driver *drv);
127 #endif /* _LINUX_VIRTIO_H */