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