Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1 | /* Virtio ring implementation. |
| 2 | * |
| 3 | * Copyright 2007 Rusty Russell IBM Corporation |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | #include <linux/virtio.h> |
| 20 | #include <linux/virtio_ring.h> |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 21 | #include <linux/virtio_config.h> |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 22 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Paul Gortmaker | b5a2c4f | 2011-07-03 16:20:30 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 25 | #include <linux/hrtimer.h> |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 26 | |
Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 27 | /* virtio guest is communicating with a virtual "device" that actually runs on |
| 28 | * a host processor. Memory barriers are used to control SMP effects. */ |
| 29 | #ifdef CONFIG_SMP |
| 30 | /* Where possible, use SMP barriers which are more lightweight than mandatory |
| 31 | * barriers, because mandatory barriers control MMIO effects on accesses |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 32 | * through relaxed memory I/O windows (which virtio-pci does not use). */ |
| 33 | #define virtio_mb(vq) \ |
| 34 | do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0) |
| 35 | #define virtio_rmb(vq) \ |
| 36 | do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0) |
| 37 | #define virtio_wmb(vq) \ |
Jason Wang | 4dbc5d9 | 2012-01-20 16:16:59 +0800 | [diff] [blame] | 38 | do { if ((vq)->weak_barriers) smp_wmb(); else wmb(); } while(0) |
Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 39 | #else |
| 40 | /* We must force memory ordering even if guest is UP since host could be |
| 41 | * running on another CPU, but SMP barriers are defined to barrier() in that |
| 42 | * configuration. So fall back to mandatory barriers instead. */ |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 43 | #define virtio_mb(vq) mb() |
| 44 | #define virtio_rmb(vq) rmb() |
| 45 | #define virtio_wmb(vq) wmb() |
Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 46 | #endif |
| 47 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 48 | #ifdef DEBUG |
| 49 | /* For development, we want to crash whenever the ring is screwed. */ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 50 | #define BAD_RING(_vq, fmt, args...) \ |
| 51 | do { \ |
| 52 | dev_err(&(_vq)->vq.vdev->dev, \ |
| 53 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 54 | BUG(); \ |
| 55 | } while (0) |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 56 | /* Caller is supposed to guarantee no reentry. */ |
| 57 | #define START_USE(_vq) \ |
| 58 | do { \ |
| 59 | if ((_vq)->in_use) \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 60 | panic("%s:in_use = %i\n", \ |
| 61 | (_vq)->vq.name, (_vq)->in_use); \ |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 62 | (_vq)->in_use = __LINE__; \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 63 | } while (0) |
Roel Kluin | 3a35ce7 | 2009-01-22 16:42:57 +0100 | [diff] [blame] | 64 | #define END_USE(_vq) \ |
Rusty Russell | 97a545a | 2010-02-24 14:22:22 -0600 | [diff] [blame] | 65 | do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 66 | #else |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 67 | #define BAD_RING(_vq, fmt, args...) \ |
| 68 | do { \ |
| 69 | dev_err(&_vq->vq.vdev->dev, \ |
| 70 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 71 | (_vq)->broken = true; \ |
| 72 | } while (0) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 73 | #define START_USE(vq) |
| 74 | #define END_USE(vq) |
| 75 | #endif |
| 76 | |
| 77 | struct vring_virtqueue |
| 78 | { |
| 79 | struct virtqueue vq; |
| 80 | |
| 81 | /* Actual memory layout for this queue */ |
| 82 | struct vring vring; |
| 83 | |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 84 | /* Can we use weak barriers? */ |
| 85 | bool weak_barriers; |
| 86 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 87 | /* Other side has made a mess, don't try any more. */ |
| 88 | bool broken; |
| 89 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 90 | /* Host supports indirect buffers */ |
| 91 | bool indirect; |
| 92 | |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 93 | /* Host publishes avail event idx */ |
| 94 | bool event; |
| 95 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 96 | /* Number of free buffers */ |
| 97 | unsigned int num_free; |
| 98 | /* Head of free buffer list. */ |
| 99 | unsigned int free_head; |
| 100 | /* Number we've added since last sync. */ |
| 101 | unsigned int num_added; |
| 102 | |
| 103 | /* Last used index we've seen. */ |
Anthony Liguori | 1bc4953 | 2007-11-07 15:49:24 -0600 | [diff] [blame] | 104 | u16 last_used_idx; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 105 | |
| 106 | /* How to notify other side. FIXME: commonalize hcalls! */ |
| 107 | void (*notify)(struct virtqueue *vq); |
| 108 | |
Jason Wang | 17bb6d4 | 2012-08-28 13:54:13 +0200 | [diff] [blame] | 109 | /* Index of the queue */ |
| 110 | int queue_index; |
| 111 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 112 | #ifdef DEBUG |
| 113 | /* They're supposed to lock for us. */ |
| 114 | unsigned int in_use; |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 115 | |
| 116 | /* Figure out if their kicks are too delayed. */ |
| 117 | bool last_add_time_valid; |
| 118 | ktime_t last_add_time; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 119 | #endif |
| 120 | |
| 121 | /* Tokens for callbacks. */ |
| 122 | void *data[]; |
| 123 | }; |
| 124 | |
| 125 | #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) |
| 126 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 127 | /* Set up an indirect table of descriptors and add it to the queue. */ |
| 128 | static int vring_add_indirect(struct vring_virtqueue *vq, |
| 129 | struct scatterlist sg[], |
| 130 | unsigned int out, |
Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 131 | unsigned int in, |
| 132 | gfp_t gfp) |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 133 | { |
| 134 | struct vring_desc *desc; |
| 135 | unsigned head; |
| 136 | int i; |
| 137 | |
Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 138 | desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 139 | if (!desc) |
Michael S. Tsirkin | 686d363 | 2010-06-10 18:16:11 +0300 | [diff] [blame] | 140 | return -ENOMEM; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 141 | |
| 142 | /* Transfer entries from the sg list into the indirect page */ |
| 143 | for (i = 0; i < out; i++) { |
| 144 | desc[i].flags = VRING_DESC_F_NEXT; |
| 145 | desc[i].addr = sg_phys(sg); |
| 146 | desc[i].len = sg->length; |
| 147 | desc[i].next = i+1; |
| 148 | sg++; |
| 149 | } |
| 150 | for (; i < (out + in); i++) { |
| 151 | desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE; |
| 152 | desc[i].addr = sg_phys(sg); |
| 153 | desc[i].len = sg->length; |
| 154 | desc[i].next = i+1; |
| 155 | sg++; |
| 156 | } |
| 157 | |
| 158 | /* Last one doesn't continue. */ |
| 159 | desc[i-1].flags &= ~VRING_DESC_F_NEXT; |
| 160 | desc[i-1].next = 0; |
| 161 | |
| 162 | /* We're about to use a buffer */ |
| 163 | vq->num_free--; |
| 164 | |
| 165 | /* Use a single buffer which doesn't continue */ |
| 166 | head = vq->free_head; |
| 167 | vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT; |
| 168 | vq->vring.desc[head].addr = virt_to_phys(desc); |
| 169 | vq->vring.desc[head].len = i * sizeof(struct vring_desc); |
| 170 | |
| 171 | /* Update free pointer */ |
| 172 | vq->free_head = vq->vring.desc[head].next; |
| 173 | |
| 174 | return head; |
| 175 | } |
| 176 | |
Jason Wang | 17bb6d4 | 2012-08-28 13:54:13 +0200 | [diff] [blame] | 177 | int virtqueue_get_queue_index(struct virtqueue *_vq) |
| 178 | { |
| 179 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 180 | return vq->queue_index; |
| 181 | } |
| 182 | EXPORT_SYMBOL_GPL(virtqueue_get_queue_index); |
| 183 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 184 | /** |
Rusty Russell | f96fde4 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 185 | * virtqueue_add_buf - expose buffer to other end |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 186 | * @vq: the struct virtqueue we're talking about. |
| 187 | * @sg: the description of the buffer(s). |
| 188 | * @out_num: the number of sg readable by other side |
| 189 | * @in_num: the number of sg which are writable (after readable ones) |
| 190 | * @data: the token identifying the buffer. |
| 191 | * @gfp: how to do memory allocations (if necessary). |
| 192 | * |
| 193 | * Caller must ensure we don't call this with other virtqueue operations |
| 194 | * at the same time (except where noted). |
| 195 | * |
| 196 | * Returns remaining capacity of queue or a negative error |
| 197 | * (ie. ENOSPC). Note that it only really makes sense to treat all |
| 198 | * positive return values as "available": indirect buffers mean that |
| 199 | * we can put an entire sg[] array inside a single queue entry. |
| 200 | */ |
Rusty Russell | f96fde4 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 201 | int virtqueue_add_buf(struct virtqueue *_vq, |
| 202 | struct scatterlist sg[], |
| 203 | unsigned int out, |
| 204 | unsigned int in, |
| 205 | void *data, |
| 206 | gfp_t gfp) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 207 | { |
| 208 | struct vring_virtqueue *vq = to_vvq(_vq); |
Michael S. Tsirkin | 1fe9b6f | 2010-07-26 16:55:30 +0930 | [diff] [blame] | 209 | unsigned int i, avail, uninitialized_var(prev); |
| 210 | int head; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 211 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 212 | START_USE(vq); |
| 213 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 214 | BUG_ON(data == NULL); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 215 | |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 216 | #ifdef DEBUG |
| 217 | { |
| 218 | ktime_t now = ktime_get(); |
| 219 | |
| 220 | /* No kick or get, with .1 second between? Warn. */ |
| 221 | if (vq->last_add_time_valid) |
| 222 | WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time)) |
| 223 | > 100); |
| 224 | vq->last_add_time = now; |
| 225 | vq->last_add_time_valid = true; |
| 226 | } |
| 227 | #endif |
| 228 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 229 | /* If the host supports indirect descriptor tables, and we have multiple |
| 230 | * buffers, then go indirect. FIXME: tune this threshold */ |
| 231 | if (vq->indirect && (out + in) > 1 && vq->num_free) { |
Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 232 | head = vring_add_indirect(vq, sg, out, in, gfp); |
Michael S. Tsirkin | 1fe9b6f | 2010-07-26 16:55:30 +0930 | [diff] [blame] | 233 | if (likely(head >= 0)) |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 234 | goto add_head; |
| 235 | } |
| 236 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 237 | BUG_ON(out + in > vq->vring.num); |
| 238 | BUG_ON(out + in == 0); |
| 239 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 240 | if (vq->num_free < out + in) { |
| 241 | pr_debug("Can't add buf len %i - avail = %i\n", |
| 242 | out + in, vq->num_free); |
Rusty Russell | 44653ea | 2008-07-25 12:06:04 -0500 | [diff] [blame] | 243 | /* FIXME: for historical reasons, we force a notify here if |
| 244 | * there are outgoing parts to the buffer. Presumably the |
| 245 | * host should service the ring ASAP. */ |
| 246 | if (out) |
| 247 | vq->notify(&vq->vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 248 | END_USE(vq); |
| 249 | return -ENOSPC; |
| 250 | } |
| 251 | |
| 252 | /* We're about to use some buffers from the free list. */ |
| 253 | vq->num_free -= out + in; |
| 254 | |
| 255 | head = vq->free_head; |
| 256 | for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) { |
| 257 | vq->vring.desc[i].flags = VRING_DESC_F_NEXT; |
Rusty Russell | 15f9c89 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 258 | vq->vring.desc[i].addr = sg_phys(sg); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 259 | vq->vring.desc[i].len = sg->length; |
| 260 | prev = i; |
| 261 | sg++; |
| 262 | } |
| 263 | for (; in; i = vq->vring.desc[i].next, in--) { |
| 264 | vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE; |
Rusty Russell | 15f9c89 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 265 | vq->vring.desc[i].addr = sg_phys(sg); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 266 | vq->vring.desc[i].len = sg->length; |
| 267 | prev = i; |
| 268 | sg++; |
| 269 | } |
| 270 | /* Last one doesn't continue. */ |
| 271 | vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT; |
| 272 | |
| 273 | /* Update free pointer */ |
| 274 | vq->free_head = i; |
| 275 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 276 | add_head: |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 277 | /* Set token. */ |
| 278 | vq->data[head] = data; |
| 279 | |
| 280 | /* Put entry in available array (but don't update avail->idx until they |
Rusty Russell | 3b720b8 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 281 | * do sync). */ |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 282 | avail = (vq->vring.avail->idx & (vq->vring.num-1)); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 283 | vq->vring.avail->ring[avail] = head; |
| 284 | |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 285 | /* Descriptors and available array need to be set before we expose the |
| 286 | * new available array entries. */ |
| 287 | virtio_wmb(vq); |
| 288 | vq->vring.avail->idx++; |
| 289 | vq->num_added++; |
| 290 | |
| 291 | /* This is very unlikely, but theoretically possible. Kick |
| 292 | * just in case. */ |
| 293 | if (unlikely(vq->num_added == (1 << 16) - 1)) |
| 294 | virtqueue_kick(_vq); |
| 295 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 296 | pr_debug("Added buffer head %i to %p\n", head, vq); |
| 297 | END_USE(vq); |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 298 | |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 299 | return vq->num_free; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 300 | } |
Rusty Russell | f96fde4 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 301 | EXPORT_SYMBOL_GPL(virtqueue_add_buf); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 302 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 303 | /** |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 304 | * virtqueue_kick_prepare - first half of split virtqueue_kick call. |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 305 | * @vq: the struct virtqueue |
| 306 | * |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 307 | * Instead of virtqueue_kick(), you can do: |
| 308 | * if (virtqueue_kick_prepare(vq)) |
| 309 | * virtqueue_notify(vq); |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 310 | * |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 311 | * This is sometimes useful because the virtqueue_kick_prepare() needs |
| 312 | * to be serialized, but the actual virtqueue_notify() call does not. |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 313 | */ |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 314 | bool virtqueue_kick_prepare(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 315 | { |
| 316 | struct vring_virtqueue *vq = to_vvq(_vq); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 317 | u16 new, old; |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 318 | bool needs_kick; |
| 319 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 320 | START_USE(vq); |
Jason Wang | a72caae | 2012-01-20 16:17:08 +0800 | [diff] [blame] | 321 | /* We need to expose available array entries before checking avail |
| 322 | * event. */ |
| 323 | virtio_mb(vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 324 | |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 325 | old = vq->vring.avail->idx - vq->num_added; |
| 326 | new = vq->vring.avail->idx; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 327 | vq->num_added = 0; |
| 328 | |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 329 | #ifdef DEBUG |
| 330 | if (vq->last_add_time_valid) { |
| 331 | WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), |
| 332 | vq->last_add_time)) > 100); |
| 333 | } |
| 334 | vq->last_add_time_valid = false; |
| 335 | #endif |
| 336 | |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 337 | if (vq->event) { |
| 338 | needs_kick = vring_need_event(vring_avail_event(&vq->vring), |
| 339 | new, old); |
| 340 | } else { |
| 341 | needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY); |
| 342 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 343 | END_USE(vq); |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 344 | return needs_kick; |
| 345 | } |
| 346 | EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); |
| 347 | |
| 348 | /** |
| 349 | * virtqueue_notify - second half of split virtqueue_kick call. |
| 350 | * @vq: the struct virtqueue |
| 351 | * |
| 352 | * This does not need to be serialized. |
| 353 | */ |
| 354 | void virtqueue_notify(struct virtqueue *_vq) |
| 355 | { |
| 356 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 357 | |
| 358 | /* Prod other side to tell it about changes. */ |
| 359 | vq->notify(_vq); |
| 360 | } |
| 361 | EXPORT_SYMBOL_GPL(virtqueue_notify); |
| 362 | |
| 363 | /** |
| 364 | * virtqueue_kick - update after add_buf |
| 365 | * @vq: the struct virtqueue |
| 366 | * |
| 367 | * After one or more virtqueue_add_buf calls, invoke this to kick |
| 368 | * the other side. |
| 369 | * |
| 370 | * Caller must ensure we don't call this with other virtqueue |
| 371 | * operations at the same time (except where noted). |
| 372 | */ |
| 373 | void virtqueue_kick(struct virtqueue *vq) |
| 374 | { |
| 375 | if (virtqueue_kick_prepare(vq)) |
| 376 | virtqueue_notify(vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 377 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 378 | EXPORT_SYMBOL_GPL(virtqueue_kick); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 379 | |
| 380 | static void detach_buf(struct vring_virtqueue *vq, unsigned int head) |
| 381 | { |
| 382 | unsigned int i; |
| 383 | |
| 384 | /* Clear data ptr. */ |
| 385 | vq->data[head] = NULL; |
| 386 | |
| 387 | /* Put back on free list: find end */ |
| 388 | i = head; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 389 | |
| 390 | /* Free the indirect table */ |
| 391 | if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT) |
| 392 | kfree(phys_to_virt(vq->vring.desc[i].addr)); |
| 393 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 394 | while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) { |
| 395 | i = vq->vring.desc[i].next; |
| 396 | vq->num_free++; |
| 397 | } |
| 398 | |
| 399 | vq->vring.desc[i].next = vq->free_head; |
| 400 | vq->free_head = head; |
| 401 | /* Plus final descriptor */ |
| 402 | vq->num_free++; |
| 403 | } |
| 404 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 405 | static inline bool more_used(const struct vring_virtqueue *vq) |
| 406 | { |
| 407 | return vq->last_used_idx != vq->vring.used->idx; |
| 408 | } |
| 409 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 410 | /** |
| 411 | * virtqueue_get_buf - get the next used buffer |
| 412 | * @vq: the struct virtqueue we're talking about. |
| 413 | * @len: the length written into the buffer |
| 414 | * |
| 415 | * If the driver wrote data into the buffer, @len will be set to the |
| 416 | * amount written. This means you don't need to clear the buffer |
| 417 | * beforehand to ensure there's no data leakage in the case of short |
| 418 | * writes. |
| 419 | * |
| 420 | * Caller must ensure we don't call this with other virtqueue |
| 421 | * operations at the same time (except where noted). |
| 422 | * |
| 423 | * Returns NULL if there are no used buffers, or the "data" token |
Rusty Russell | f96fde4 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 424 | * handed to virtqueue_add_buf(). |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 425 | */ |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 426 | void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 427 | { |
| 428 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 429 | void *ret; |
| 430 | unsigned int i; |
Rusty Russell | 3b720b8 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 431 | u16 last_used; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 432 | |
| 433 | START_USE(vq); |
| 434 | |
Rusty Russell | 5ef8275 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 435 | if (unlikely(vq->broken)) { |
| 436 | END_USE(vq); |
| 437 | return NULL; |
| 438 | } |
| 439 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 440 | if (!more_used(vq)) { |
| 441 | pr_debug("No more buffers in queue\n"); |
| 442 | END_USE(vq); |
| 443 | return NULL; |
| 444 | } |
| 445 | |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 446 | /* Only get used array entries after they have been exposed by host. */ |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 447 | virtio_rmb(vq); |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 448 | |
Rusty Russell | 3b720b8 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 449 | last_used = (vq->last_used_idx & (vq->vring.num - 1)); |
| 450 | i = vq->vring.used->ring[last_used].id; |
| 451 | *len = vq->vring.used->ring[last_used].len; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 452 | |
| 453 | if (unlikely(i >= vq->vring.num)) { |
| 454 | BAD_RING(vq, "id %u out of range\n", i); |
| 455 | return NULL; |
| 456 | } |
| 457 | if (unlikely(!vq->data[i])) { |
| 458 | BAD_RING(vq, "id %u is not a head!\n", i); |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | /* detach_buf clears data, so grab it now. */ |
| 463 | ret = vq->data[i]; |
| 464 | detach_buf(vq, i); |
| 465 | vq->last_used_idx++; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 466 | /* If we expect an interrupt for the next entry, tell host |
| 467 | * by writing event index and flush out the write before |
| 468 | * the read in the next get_buf call. */ |
| 469 | if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) { |
| 470 | vring_used_event(&vq->vring) = vq->last_used_idx; |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 471 | virtio_mb(vq); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 472 | } |
| 473 | |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 474 | #ifdef DEBUG |
| 475 | vq->last_add_time_valid = false; |
| 476 | #endif |
| 477 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 478 | END_USE(vq); |
| 479 | return ret; |
| 480 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 481 | EXPORT_SYMBOL_GPL(virtqueue_get_buf); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 482 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 483 | /** |
| 484 | * virtqueue_disable_cb - disable callbacks |
| 485 | * @vq: the struct virtqueue we're talking about. |
| 486 | * |
| 487 | * Note that this is not necessarily synchronous, hence unreliable and only |
| 488 | * useful as an optimization. |
| 489 | * |
| 490 | * Unlike other operations, this need not be serialized. |
| 491 | */ |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 492 | void virtqueue_disable_cb(struct virtqueue *_vq) |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 493 | { |
| 494 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 495 | |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 496 | vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 497 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 498 | EXPORT_SYMBOL_GPL(virtqueue_disable_cb); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 499 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 500 | /** |
| 501 | * virtqueue_enable_cb - restart callbacks after disable_cb. |
| 502 | * @vq: the struct virtqueue we're talking about. |
| 503 | * |
| 504 | * This re-enables callbacks; it returns "false" if there are pending |
| 505 | * buffers in the queue, to detect a possible race between the driver |
| 506 | * checking for more work, and enabling callbacks. |
| 507 | * |
| 508 | * Caller must ensure we don't call this with other virtqueue |
| 509 | * operations at the same time (except where noted). |
| 510 | */ |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 511 | bool virtqueue_enable_cb(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 512 | { |
| 513 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 514 | |
| 515 | START_USE(vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 516 | |
| 517 | /* We optimistically turn back on interrupts, then check if there was |
| 518 | * more to do. */ |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 519 | /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to |
| 520 | * either clear the flags bit or point the event index at the next |
| 521 | * entry. Always do both to keep code simple. */ |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 522 | vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 523 | vring_used_event(&vq->vring) = vq->last_used_idx; |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 524 | virtio_mb(vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 525 | if (unlikely(more_used(vq))) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 526 | END_USE(vq); |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | END_USE(vq); |
| 531 | return true; |
| 532 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 533 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 534 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 535 | /** |
| 536 | * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. |
| 537 | * @vq: the struct virtqueue we're talking about. |
| 538 | * |
| 539 | * This re-enables callbacks but hints to the other side to delay |
| 540 | * interrupts until most of the available buffers have been processed; |
| 541 | * it returns "false" if there are many pending buffers in the queue, |
| 542 | * to detect a possible race between the driver checking for more work, |
| 543 | * and enabling callbacks. |
| 544 | * |
| 545 | * Caller must ensure we don't call this with other virtqueue |
| 546 | * operations at the same time (except where noted). |
| 547 | */ |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 548 | bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) |
| 549 | { |
| 550 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 551 | u16 bufs; |
| 552 | |
| 553 | START_USE(vq); |
| 554 | |
| 555 | /* We optimistically turn back on interrupts, then check if there was |
| 556 | * more to do. */ |
| 557 | /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to |
| 558 | * either clear the flags bit or point the event index at the next |
| 559 | * entry. Always do both to keep code simple. */ |
| 560 | vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; |
| 561 | /* TODO: tune this threshold */ |
| 562 | bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4; |
| 563 | vring_used_event(&vq->vring) = vq->last_used_idx + bufs; |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 564 | virtio_mb(vq); |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 565 | if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) { |
| 566 | END_USE(vq); |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | END_USE(vq); |
| 571 | return true; |
| 572 | } |
| 573 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); |
| 574 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 575 | /** |
| 576 | * virtqueue_detach_unused_buf - detach first unused buffer |
| 577 | * @vq: the struct virtqueue we're talking about. |
| 578 | * |
Rusty Russell | f96fde4 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 579 | * Returns NULL or the "data" token handed to virtqueue_add_buf(). |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 580 | * This is not valid on an active queue; it is useful only for device |
| 581 | * shutdown. |
| 582 | */ |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 583 | void *virtqueue_detach_unused_buf(struct virtqueue *_vq) |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 584 | { |
| 585 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 586 | unsigned int i; |
| 587 | void *buf; |
| 588 | |
| 589 | START_USE(vq); |
| 590 | |
| 591 | for (i = 0; i < vq->vring.num; i++) { |
| 592 | if (!vq->data[i]) |
| 593 | continue; |
| 594 | /* detach_buf clears data, so grab it now. */ |
| 595 | buf = vq->data[i]; |
| 596 | detach_buf(vq, i); |
Amit Shah | b3258ff | 2011-03-16 19:12:10 +0530 | [diff] [blame] | 597 | vq->vring.avail->idx--; |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 598 | END_USE(vq); |
| 599 | return buf; |
| 600 | } |
| 601 | /* That should have freed everything. */ |
| 602 | BUG_ON(vq->num_free != vq->vring.num); |
| 603 | |
| 604 | END_USE(vq); |
| 605 | return NULL; |
| 606 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 607 | EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 608 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 609 | irqreturn_t vring_interrupt(int irq, void *_vq) |
| 610 | { |
| 611 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 612 | |
| 613 | if (!more_used(vq)) { |
| 614 | pr_debug("virtqueue interrupt with no work for %p\n", vq); |
| 615 | return IRQ_NONE; |
| 616 | } |
| 617 | |
| 618 | if (unlikely(vq->broken)) |
| 619 | return IRQ_HANDLED; |
| 620 | |
| 621 | pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 622 | if (vq->vq.callback) |
| 623 | vq->vq.callback(&vq->vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 624 | |
| 625 | return IRQ_HANDLED; |
| 626 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 627 | EXPORT_SYMBOL_GPL(vring_interrupt); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 628 | |
Jason Wang | 17bb6d4 | 2012-08-28 13:54:13 +0200 | [diff] [blame] | 629 | struct virtqueue *vring_new_virtqueue(unsigned int index, |
| 630 | unsigned int num, |
Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 631 | unsigned int vring_align, |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 632 | struct virtio_device *vdev, |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 633 | bool weak_barriers, |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 634 | void *pages, |
| 635 | void (*notify)(struct virtqueue *), |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 636 | void (*callback)(struct virtqueue *), |
| 637 | const char *name) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 638 | { |
| 639 | struct vring_virtqueue *vq; |
| 640 | unsigned int i; |
| 641 | |
Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 642 | /* We assume num is a power of 2. */ |
| 643 | if (num & (num - 1)) { |
| 644 | dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); |
| 645 | return NULL; |
| 646 | } |
| 647 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 648 | vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL); |
| 649 | if (!vq) |
| 650 | return NULL; |
| 651 | |
Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 652 | vring_init(&vq->vring, num, pages, vring_align); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 653 | vq->vq.callback = callback; |
| 654 | vq->vq.vdev = vdev; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 655 | vq->vq.name = name; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 656 | vq->notify = notify; |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 657 | vq->weak_barriers = weak_barriers; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 658 | vq->broken = false; |
| 659 | vq->last_used_idx = 0; |
| 660 | vq->num_added = 0; |
Jason Wang | 17bb6d4 | 2012-08-28 13:54:13 +0200 | [diff] [blame] | 661 | vq->queue_index = index; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 662 | list_add_tail(&vq->vq.list, &vdev->vqs); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 663 | #ifdef DEBUG |
| 664 | vq->in_use = false; |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 665 | vq->last_add_time_valid = false; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 666 | #endif |
| 667 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 668 | vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 669 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 670 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 671 | /* No callback? Tell other side not to bother us. */ |
| 672 | if (!callback) |
| 673 | vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; |
| 674 | |
| 675 | /* Put everything in free lists. */ |
| 676 | vq->num_free = num; |
| 677 | vq->free_head = 0; |
Amit Shah | 3b87062 | 2010-02-12 10:32:14 +0530 | [diff] [blame] | 678 | for (i = 0; i < num-1; i++) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 679 | vq->vring.desc[i].next = i+1; |
Amit Shah | 3b87062 | 2010-02-12 10:32:14 +0530 | [diff] [blame] | 680 | vq->data[i] = NULL; |
| 681 | } |
| 682 | vq->data[i] = NULL; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 683 | |
| 684 | return &vq->vq; |
| 685 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 686 | EXPORT_SYMBOL_GPL(vring_new_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 687 | |
| 688 | void vring_del_virtqueue(struct virtqueue *vq) |
| 689 | { |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 690 | list_del(&vq->list); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 691 | kfree(to_vvq(vq)); |
| 692 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 693 | EXPORT_SYMBOL_GPL(vring_del_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 694 | |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 695 | /* Manipulates transport-specific feature bits. */ |
| 696 | void vring_transport_features(struct virtio_device *vdev) |
| 697 | { |
| 698 | unsigned int i; |
| 699 | |
| 700 | for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { |
| 701 | switch (i) { |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 702 | case VIRTIO_RING_F_INDIRECT_DESC: |
| 703 | break; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 704 | case VIRTIO_RING_F_EVENT_IDX: |
| 705 | break; |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 706 | default: |
| 707 | /* We don't understand this bit. */ |
| 708 | clear_bit(i, vdev->features); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | EXPORT_SYMBOL_GPL(vring_transport_features); |
| 713 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 714 | /** |
| 715 | * virtqueue_get_vring_size - return the size of the virtqueue's vring |
| 716 | * @vq: the struct virtqueue containing the vring of interest. |
| 717 | * |
| 718 | * Returns the size of the vring. This is mainly used for boasting to |
| 719 | * userspace. Unlike other operations, this need not be serialized. |
| 720 | */ |
Rick Jones | 8f9f466 | 2011-10-19 08:10:59 +0000 | [diff] [blame] | 721 | unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) |
| 722 | { |
| 723 | |
| 724 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 725 | |
| 726 | return vq->vring.num; |
| 727 | } |
| 728 | EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); |
| 729 | |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 730 | MODULE_LICENSE("GPL"); |