blob: 0d0f6d3855fbd662ee4e91aeabd2c2bf22327275 [file] [log] [blame]
Rusty Russellec3d41c2007-10-22 11:03:36 +10001#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>
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +030010#include <linux/gfp.h>
Rusty Russellec3d41c2007-10-22 11:03:36 +100011
12/**
13 * virtqueue - a queue to register buffers for sending or receiving.
Rusty Russell9499f5e2009-06-12 22:16:35 -060014 * @list: the chain of virtqueues for this device
Rusty Russellec3d41c2007-10-22 11:03:36 +100015 * @callback: the function to call when buffers are consumed (can be NULL).
Rusty Russell9499f5e2009-06-12 22:16:35 -060016 * @name: the name of this virtqueue (mainly for debugging)
Rusty Russellec3d41c2007-10-22 11:03:36 +100017 * @vdev: the virtio device this queue was created for.
Yatin Manerkarc89a2d02012-06-12 15:27:41 -070018 * @vq_ops: the operations for this virtqueue (see below).
Rusty Russellec3d41c2007-10-22 11:03:36 +100019 * @priv: a pointer for the virtqueue implementation to use.
20 */
Rusty Russell9499f5e2009-06-12 22:16:35 -060021struct virtqueue {
22 struct list_head list;
Rusty Russell18445c42008-02-04 23:49:57 -050023 void (*callback)(struct virtqueue *vq);
Rusty Russell9499f5e2009-06-12 22:16:35 -060024 const char *name;
Rusty Russellec3d41c2007-10-22 11:03:36 +100025 struct virtio_device *vdev;
Yatin Manerkarc89a2d02012-06-12 15:27:41 -070026 struct virtqueue_ops *vq_ops;
Rusty Russellec3d41c2007-10-22 11:03:36 +100027 void *priv;
28};
29
Yatin Manerkarc89a2d02012-06-12 15:27:41 -070030/**
31 * virtqueue_ops - operations for virtqueue abstraction layer
32 * @add_buf: expose buffer to other end
33 * vq: the struct virtqueue we're talking about.
34 * sg: the description of the buffer(s).
35 * out_num: the number of sg readable by other side
36 * in_num: the number of sg which are writable (after readable ones)
37 * data: the token identifying the buffer.
38 * Returns remaining capacity of queue (sg segments) or a negative error.
39 * @kick: update after add_buf
40 * vq: the struct virtqueue
41 * After one or more add_buf calls, invoke this to kick the other side.
42 * @get_buf: get the next used buffer
43 * vq: the struct virtqueue we're talking about.
44 * len: the length written into the buffer
45 * Returns NULL or the "data" token handed to add_buf.
46 * @disable_cb: disable callbacks
47 * vq: the struct virtqueue we're talking about.
48 * Note that this is not necessarily synchronous, hence unreliable and only
49 * useful as an optimization.
50 * @enable_cb: restart callbacks after disable_cb.
51 * vq: the struct virtqueue we're talking about.
52 * This re-enables callbacks; it returns "false" if there are pending
53 * buffers in the queue, to detect a possible race between the driver
54 * checking for more work, and enabling callbacks.
55 * @enable_cb_delayed: restart callbacks after disable_cb.
56 * vq: the struct virtqueue we're talking about.
57 * This re-enables callbacks but hints to the other side to delay
58 * interrupts until most of the available buffers have been processed;
59 * it returns "false" if there are many pending buffers in the queue,
60 * to detect a possible race between the driver checking for more work,
61 * and enabling callbacks.
62 * Caller must ensure we don't call this with other virtqueue
63 * operations at the same time (except where noted).
64 * @detach_unused_buf: detach first unused buffer
65 * vq: the struct virtqueue we're talking about.
66 * Returns NULL or the "data" token handed to add_buf
67 * @get_impl_size: return the size of the virtqueue's implementation
68 * vq: the struct virtqueue containing the implementation of interest.
69 * Returns the size of the implementation. This is mainly used for
70 * boasting to userspace. Unlike other operations, this need not
71 * be serialized.
72 *
73 * Locking rules are straightforward: the driver is responsible for
74 * locking. No two operations may be invoked simultaneously, with the exception
75 * of @disable_cb.
76 *
77 * All operations can be called in any context.
78 */
79struct virtqueue_ops {
80 int (*add_buf)(struct virtqueue *vq,
81 struct scatterlist sg[],
82 unsigned int out_num,
83 unsigned int in_num,
84 void *data,
85 gfp_t gfp);
Rusty Russellec3d41c2007-10-22 11:03:36 +100086
Yatin Manerkarc89a2d02012-06-12 15:27:41 -070087 void (*kick)(struct virtqueue *vq);
88 bool (*kick_prepare)(struct virtqueue *vq);
89 void (*kick_notify)(struct virtqueue *vq);
90 void *(*get_buf)(struct virtqueue *vq, unsigned int *len);
91 void (*disable_cb)(struct virtqueue *vq);
92 bool (*enable_cb)(struct virtqueue *vq);
93 bool (*enable_cb_delayed)(struct virtqueue *vq);
94 void *(*detach_unused_buf)(struct virtqueue *vq);
95 unsigned int (*get_impl_size)(struct virtqueue *vq);
96};
Rusty Russellec3d41c2007-10-22 11:03:36 +100097
Yatin Manerkarc89a2d02012-06-12 15:27:41 -070098/**
99 * virtqueue_add_buf - expose buffer to other end
100 * @vq: the struct virtqueue we're talking about.
101 * @sg: the description of the buffer(s).
102 * @out_num: the number of sg readable by other side
103 * @in_num: the number of sg which are writable (after readable ones)
104 * @data: the token identifying the buffer.
105 * @gfp: how to do memory allocations (if necessary).
106 *
107 * Caller must ensure we don't call this with other virtqueue operations
108 * at the same time (except where noted).
109 *
110 * Returns remaining capacity of queue or a negative error.
111 */
112static inline int virtqueue_add_buf(struct virtqueue *vq,
113 struct scatterlist sg[],
114 unsigned int out_num,
115 unsigned int in_num,
116 void *data,
117 gfp_t gfp)
118{
119 return vq->vq_ops->add_buf(vq, sg, out_num, in_num, data, gfp);
120}
121/**
122 * virtqueue_kick - update after add_buf
123 * @vq: the struct virtqueue
124 *
125 * After one or more virtqueue_add_buf calls, invoke this to kick
126 * the other side.
127 *
128 * Caller must ensure we don't call this with other virtqueue
129 * operations at the same time (except where noted).
130 */
131static inline void virtqueue_kick(struct virtqueue *vq)
132{
133 vq->vq_ops->kick(vq);
134}
Rusty Russell41f03772012-01-12 15:44:43 +1030135
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700136/**
137 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
138 * @vq: the struct virtqueue
139 *
140 * Instead of virtqueue_kick(), you can do:
141 * if (virtqueue_kick_prepare(vq))
142 * virtqueue_kick_notify(vq);
143 *
144 * This is sometimes useful because the virtqueue_kick_prepare() needs
145 * to be serialized, but the actual virtqueue_kick_notify() call does not.
146 */
147static inline bool virtqueue_kick_prepare(struct virtqueue *vq)
148{
149 return vq->vq_ops->kick_prepare(vq);
150}
Rusty Russell41f03772012-01-12 15:44:43 +1030151
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700152/**
153 * virtqueue_kick_notify - second half of split virtqueue_kick call.
154 * @vq: the struct virtqueue
155 */
156static inline void virtqueue_kick_notify(struct virtqueue *vq)
157{
158 vq->vq_ops->kick_notify(vq);
159}
Rusty Russellec3d41c2007-10-22 11:03:36 +1000160
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700161/**
162 * virtqueue_get_buf - get the next used buffer
163 * @vq: the struct virtqueue we're talking about.
164 * @len: the length written into the buffer
165 *
166 * If the driver wrote data into the buffer, @len will be set to the
167 * amount written. This means you don't need to clear the buffer
168 * beforehand to ensure there's no data leakage in the case of short
169 * writes.
170 *
171 * Caller must ensure we don't call this with other virtqueue
172 * operations at the same time (except where noted).
173 *
174 * Returns NULL if there are no used buffers, or the "data" token
175 * handed to virtqueue_add_buf().
176 */
177static inline void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len)
178{
179 return vq->vq_ops->get_buf(vq, len);
180}
Michael S. Tsirkin316f25f2010-04-12 16:18:25 +0300181
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700182/**
183 * virtqueue_disable_cb - disable callbacks
184 * @vq: the struct virtqueue we're talking about.
185 *
186 * Note that this is not necessarily synchronous, hence unreliable and only
187 * useful as an optimization.
188 *
189 * Unlike other operations, this need not be serialized.
190 */
191static inline void virtqueue_disable_cb(struct virtqueue *vq)
192{
193 vq->vq_ops->disable_cb(vq);
194}
Michael S. Tsirkin316f25f2010-04-12 16:18:25 +0300195
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700196/**
197 * virtqueue_enable_cb - restart callbacks after disable_cb.
198 * @vq: the struct virtqueue we're talking about.
199 *
200 * This re-enables callbacks; it returns "false" if there are pending
201 * buffers in the queue, to detect a possible race between the driver
202 * checking for more work, and enabling callbacks.
203 *
204 * Caller must ensure we don't call this with other virtqueue
205 * operations at the same time (except where noted).
206 */
207static inline bool virtqueue_enable_cb(struct virtqueue *vq)
208{
209 return vq->vq_ops->enable_cb(vq);
210}
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300211
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700212/**
213 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
214 * @vq: the struct virtqueue we're talking about.
215 *
216 * This re-enables callbacks but hints to the other side to delay
217 * interrupts until most of the available buffers have been processed;
218 * it returns "false" if there are many pending buffers in the queue,
219 * to detect a possible race between the driver checking for more work,
220 * and enabling callbacks.
221 *
222 * Caller must ensure we don't call this with other virtqueue
223 * operations at the same time (except where noted).
224 */
225static inline bool virtqueue_enable_cb_delayed(struct virtqueue *vq)
226{
227 return vq->vq_ops->enable_cb_delayed(vq);
228}
Michael S. Tsirkin316f25f2010-04-12 16:18:25 +0300229
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700230/**
231 * virtqueue_detach_unused_buf - detach first unused buffer
232 * @vq: the struct virtqueue we're talking about.
233 *
234 * Returns NULL or the "data" token handed to virtqueue_add_buf().
235 * This is not valid on an active queue; it is useful only for device
236 * shutdown.
237 */
238static inline void *virtqueue_detach_unused_buf(struct virtqueue *vq)
239{
240 return vq->vq_ops->detach_unused_buf(vq);
241}
242
243/**
244 * virtqueue_get_impl_size - return the size of the virtqueue's implementation
245 * @vq: the struct virtqueue containing the implementation of interest.
246 *
247 * Returns the size of the virtqueue implementation. This is mainly used
248 * for boasting to userspace. Unlike other operations, this need not
249 * be serialized.
250 */
251static inline unsigned int virtqueue_get_impl_size(struct virtqueue *vq)
252{
253 return vq->vq_ops->get_impl_size(vq);
254}
Rick Jones8f9f4662011-10-19 08:10:59 +0000255
Rusty Russellec3d41c2007-10-22 11:03:36 +1000256/**
257 * virtio_device - representation of a device using virtio
258 * @index: unique position on the virtio bus
259 * @dev: underlying device.
260 * @id: the device type identification (used to match it with a driver).
261 * @config: the configuration ops for this device.
Rusty Russell9499f5e2009-06-12 22:16:35 -0600262 * @vqs: the list of virtqueues for this device.
Rusty Russellc45a6812008-05-02 21:50:50 -0500263 * @features: the features supported by both driver and device.
Rusty Russellec3d41c2007-10-22 11:03:36 +1000264 * @priv: private pointer for the driver's use.
265 */
Rusty Russell9499f5e2009-06-12 22:16:35 -0600266struct virtio_device {
Rusty Russellec3d41c2007-10-22 11:03:36 +1000267 int index;
268 struct device dev;
269 struct virtio_device_id id;
270 struct virtio_config_ops *config;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600271 struct list_head vqs;
Rusty Russellc45a6812008-05-02 21:50:50 -0500272 /* Note that this is a Linux set_bit-style bitmap. */
273 unsigned long features[1];
Rusty Russellec3d41c2007-10-22 11:03:36 +1000274 void *priv;
275};
276
Aneesh Kumar K.V86c84372010-03-06 04:44:15 +0000277#define dev_to_virtio(dev) container_of(dev, struct virtio_device, dev)
Rusty Russellec3d41c2007-10-22 11:03:36 +1000278int register_virtio_device(struct virtio_device *dev);
279void unregister_virtio_device(struct virtio_device *dev);
280
281/**
282 * virtio_driver - operations for a virtio I/O driver
283 * @driver: underlying device driver (populate name and owner).
284 * @id_table: the ids serviced by this driver.
Wang Sheng-Hui5f41f8b2011-08-25 21:04:05 +0800285 * @feature_table: an array of feature numbers supported by this driver.
Rusty Russellc45a6812008-05-02 21:50:50 -0500286 * @feature_table_size: number of entries in the feature table array.
Rusty Russell20f77f52009-06-12 22:16:33 -0600287 * @probe: the function to call when a device is found. Returns 0 or -errno.
Wang Sheng-Hui5f41f8b2011-08-25 21:04:05 +0800288 * @remove: the function to call when a device is removed.
Rusty Russellf957d1f2008-02-04 23:49:58 -0500289 * @config_changed: optional function to call when the device configuration
290 * changes; may be called in interrupt context.
Rusty Russellec3d41c2007-10-22 11:03:36 +1000291 */
292struct virtio_driver {
293 struct device_driver driver;
294 const struct virtio_device_id *id_table;
Rusty Russellc45a6812008-05-02 21:50:50 -0500295 const unsigned int *feature_table;
296 unsigned int feature_table_size;
Rusty Russellec3d41c2007-10-22 11:03:36 +1000297 int (*probe)(struct virtio_device *dev);
298 void (*remove)(struct virtio_device *dev);
Rusty Russellf957d1f2008-02-04 23:49:58 -0500299 void (*config_changed)(struct virtio_device *dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530300#ifdef CONFIG_PM
301 int (*freeze)(struct virtio_device *dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530302 int (*restore)(struct virtio_device *dev);
303#endif
Rusty Russellec3d41c2007-10-22 11:03:36 +1000304};
305
306int register_virtio_driver(struct virtio_driver *drv);
307void unregister_virtio_driver(struct virtio_driver *drv);
308#endif /* _LINUX_VIRTIO_H */