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