Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 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> |
Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 10 | #include <linux/gfp.h> |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 11 | |
| 12 | /** |
| 13 | * virtqueue - a queue to register buffers for sending or receiving. |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 14 | * @list: the chain of virtqueues for this device |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 15 | * @callback: the function to call when buffers are consumed (can be NULL). |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 16 | * @name: the name of this virtqueue (mainly for debugging) |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 17 | * @vdev: the virtio device this queue was created for. |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 18 | * @vq_ops: the operations for this virtqueue (see below). |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 19 | * @priv: a pointer for the virtqueue implementation to use. |
| 20 | */ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 21 | struct virtqueue { |
| 22 | struct list_head list; |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 23 | void (*callback)(struct virtqueue *vq); |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 24 | const char *name; |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 25 | struct virtio_device *vdev; |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 26 | struct virtqueue_ops *vq_ops; |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 27 | void *priv; |
| 28 | }; |
| 29 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 30 | /** |
| 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 | */ |
| 79 | struct 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 Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 86 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 87 | 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 Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 97 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 98 | /** |
| 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 | */ |
| 112 | static 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 | */ |
| 131 | static inline void virtqueue_kick(struct virtqueue *vq) |
| 132 | { |
| 133 | vq->vq_ops->kick(vq); |
| 134 | } |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 135 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 136 | /** |
| 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 | */ |
| 147 | static inline bool virtqueue_kick_prepare(struct virtqueue *vq) |
| 148 | { |
| 149 | return vq->vq_ops->kick_prepare(vq); |
| 150 | } |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 151 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 152 | /** |
| 153 | * virtqueue_kick_notify - second half of split virtqueue_kick call. |
| 154 | * @vq: the struct virtqueue |
| 155 | */ |
| 156 | static inline void virtqueue_kick_notify(struct virtqueue *vq) |
| 157 | { |
| 158 | vq->vq_ops->kick_notify(vq); |
| 159 | } |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 160 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 161 | /** |
| 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 | */ |
| 177 | static inline void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len) |
| 178 | { |
| 179 | return vq->vq_ops->get_buf(vq, len); |
| 180 | } |
Michael S. Tsirkin | 316f25f | 2010-04-12 16:18:25 +0300 | [diff] [blame] | 181 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 182 | /** |
| 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 | */ |
| 191 | static inline void virtqueue_disable_cb(struct virtqueue *vq) |
| 192 | { |
| 193 | vq->vq_ops->disable_cb(vq); |
| 194 | } |
Michael S. Tsirkin | 316f25f | 2010-04-12 16:18:25 +0300 | [diff] [blame] | 195 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 196 | /** |
| 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 | */ |
| 207 | static inline bool virtqueue_enable_cb(struct virtqueue *vq) |
| 208 | { |
| 209 | return vq->vq_ops->enable_cb(vq); |
| 210 | } |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 211 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 212 | /** |
| 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 | */ |
| 225 | static inline bool virtqueue_enable_cb_delayed(struct virtqueue *vq) |
| 226 | { |
| 227 | return vq->vq_ops->enable_cb_delayed(vq); |
| 228 | } |
Michael S. Tsirkin | 316f25f | 2010-04-12 16:18:25 +0300 | [diff] [blame] | 229 | |
Yatin Manerkar | c89a2d0 | 2012-06-12 15:27:41 -0700 | [diff] [blame] | 230 | /** |
| 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 | */ |
| 238 | static 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 | */ |
| 251 | static inline unsigned int virtqueue_get_impl_size(struct virtqueue *vq) |
| 252 | { |
| 253 | return vq->vq_ops->get_impl_size(vq); |
| 254 | } |
Rick Jones | 8f9f466 | 2011-10-19 08:10:59 +0000 | [diff] [blame] | 255 | |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 256 | /** |
| 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 Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 262 | * @vqs: the list of virtqueues for this device. |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 263 | * @features: the features supported by both driver and device. |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 264 | * @priv: private pointer for the driver's use. |
| 265 | */ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 266 | struct virtio_device { |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 267 | int index; |
| 268 | struct device dev; |
| 269 | struct virtio_device_id id; |
| 270 | struct virtio_config_ops *config; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 271 | struct list_head vqs; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 272 | /* Note that this is a Linux set_bit-style bitmap. */ |
| 273 | unsigned long features[1]; |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 274 | void *priv; |
| 275 | }; |
| 276 | |
Aneesh Kumar K.V | 86c8437 | 2010-03-06 04:44:15 +0000 | [diff] [blame] | 277 | #define dev_to_virtio(dev) container_of(dev, struct virtio_device, dev) |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 278 | int register_virtio_device(struct virtio_device *dev); |
| 279 | void 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-Hui | 5f41f8b | 2011-08-25 21:04:05 +0800 | [diff] [blame] | 285 | * @feature_table: an array of feature numbers supported by this driver. |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 286 | * @feature_table_size: number of entries in the feature table array. |
Rusty Russell | 20f77f5 | 2009-06-12 22:16:33 -0600 | [diff] [blame] | 287 | * @probe: the function to call when a device is found. Returns 0 or -errno. |
Wang Sheng-Hui | 5f41f8b | 2011-08-25 21:04:05 +0800 | [diff] [blame] | 288 | * @remove: the function to call when a device is removed. |
Rusty Russell | f957d1f | 2008-02-04 23:49:58 -0500 | [diff] [blame] | 289 | * @config_changed: optional function to call when the device configuration |
| 290 | * changes; may be called in interrupt context. |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 291 | */ |
| 292 | struct virtio_driver { |
| 293 | struct device_driver driver; |
| 294 | const struct virtio_device_id *id_table; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 295 | const unsigned int *feature_table; |
| 296 | unsigned int feature_table_size; |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 297 | int (*probe)(struct virtio_device *dev); |
| 298 | void (*remove)(struct virtio_device *dev); |
Rusty Russell | f957d1f | 2008-02-04 23:49:58 -0500 | [diff] [blame] | 299 | void (*config_changed)(struct virtio_device *dev); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 300 | #ifdef CONFIG_PM |
| 301 | int (*freeze)(struct virtio_device *dev); |
Amit Shah | f0fe6f1 | 2011-12-22 16:58:26 +0530 | [diff] [blame] | 302 | int (*restore)(struct virtio_device *dev); |
| 303 | #endif |
Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | int register_virtio_driver(struct virtio_driver *drv); |
| 307 | void unregister_virtio_driver(struct virtio_driver *drv); |
| 308 | #endif /* _LINUX_VIRTIO_H */ |