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 | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 25 | |
Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 26 | /* virtio guest is communicating with a virtual "device" that actually runs on |
| 27 | * a host processor. Memory barriers are used to control SMP effects. */ |
| 28 | #ifdef CONFIG_SMP |
| 29 | /* Where possible, use SMP barriers which are more lightweight than mandatory |
| 30 | * barriers, because mandatory barriers control MMIO effects on accesses |
| 31 | * through relaxed memory I/O windows (which virtio does not use). */ |
| 32 | #define virtio_mb() smp_mb() |
| 33 | #define virtio_rmb() smp_rmb() |
| 34 | #define virtio_wmb() smp_wmb() |
| 35 | #else |
| 36 | /* We must force memory ordering even if guest is UP since host could be |
| 37 | * running on another CPU, but SMP barriers are defined to barrier() in that |
| 38 | * configuration. So fall back to mandatory barriers instead. */ |
| 39 | #define virtio_mb() mb() |
| 40 | #define virtio_rmb() rmb() |
| 41 | #define virtio_wmb() wmb() |
| 42 | #endif |
| 43 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 44 | #ifdef DEBUG |
| 45 | /* For development, we want to crash whenever the ring is screwed. */ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 46 | #define BAD_RING(_vq, fmt, args...) \ |
| 47 | do { \ |
| 48 | dev_err(&(_vq)->vq.vdev->dev, \ |
| 49 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 50 | BUG(); \ |
| 51 | } while (0) |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 52 | /* Caller is supposed to guarantee no reentry. */ |
| 53 | #define START_USE(_vq) \ |
| 54 | do { \ |
| 55 | if ((_vq)->in_use) \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 56 | panic("%s:in_use = %i\n", \ |
| 57 | (_vq)->vq.name, (_vq)->in_use); \ |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 58 | (_vq)->in_use = __LINE__; \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 59 | } while (0) |
Roel Kluin | 3a35ce7 | 2009-01-22 16:42:57 +0100 | [diff] [blame] | 60 | #define END_USE(_vq) \ |
Rusty Russell | 97a545a | 2010-02-24 14:22:22 -0600 | [diff] [blame] | 61 | do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 62 | #else |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 63 | #define BAD_RING(_vq, fmt, args...) \ |
| 64 | do { \ |
| 65 | dev_err(&_vq->vq.vdev->dev, \ |
| 66 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 67 | (_vq)->broken = true; \ |
| 68 | } while (0) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 69 | #define START_USE(vq) |
| 70 | #define END_USE(vq) |
| 71 | #endif |
| 72 | |
| 73 | struct vring_virtqueue |
| 74 | { |
| 75 | struct virtqueue vq; |
| 76 | |
| 77 | /* Actual memory layout for this queue */ |
| 78 | struct vring vring; |
| 79 | |
| 80 | /* Other side has made a mess, don't try any more. */ |
| 81 | bool broken; |
| 82 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 83 | /* Host supports indirect buffers */ |
| 84 | bool indirect; |
| 85 | |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 86 | /* Host publishes avail event idx */ |
| 87 | bool event; |
| 88 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 89 | /* Number of free buffers */ |
| 90 | unsigned int num_free; |
| 91 | /* Head of free buffer list. */ |
| 92 | unsigned int free_head; |
| 93 | /* Number we've added since last sync. */ |
| 94 | unsigned int num_added; |
| 95 | |
| 96 | /* Last used index we've seen. */ |
Anthony Liguori | 1bc4953 | 2007-11-07 15:49:24 -0600 | [diff] [blame] | 97 | u16 last_used_idx; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 98 | |
| 99 | /* How to notify other side. FIXME: commonalize hcalls! */ |
| 100 | void (*notify)(struct virtqueue *vq); |
| 101 | |
| 102 | #ifdef DEBUG |
| 103 | /* They're supposed to lock for us. */ |
| 104 | unsigned int in_use; |
| 105 | #endif |
| 106 | |
| 107 | /* Tokens for callbacks. */ |
| 108 | void *data[]; |
| 109 | }; |
| 110 | |
| 111 | #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) |
| 112 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 113 | /* Set up an indirect table of descriptors and add it to the queue. */ |
| 114 | static int vring_add_indirect(struct vring_virtqueue *vq, |
| 115 | struct scatterlist sg[], |
| 116 | unsigned int out, |
Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 117 | unsigned int in, |
| 118 | gfp_t gfp) |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 119 | { |
| 120 | struct vring_desc *desc; |
| 121 | unsigned head; |
| 122 | int i; |
| 123 | |
Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 124 | desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 125 | if (!desc) |
Michael S. Tsirkin | 686d363 | 2010-06-10 18:16:11 +0300 | [diff] [blame] | 126 | return -ENOMEM; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 127 | |
| 128 | /* Transfer entries from the sg list into the indirect page */ |
| 129 | for (i = 0; i < out; i++) { |
| 130 | desc[i].flags = VRING_DESC_F_NEXT; |
| 131 | desc[i].addr = sg_phys(sg); |
| 132 | desc[i].len = sg->length; |
| 133 | desc[i].next = i+1; |
| 134 | sg++; |
| 135 | } |
| 136 | for (; i < (out + in); i++) { |
| 137 | desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE; |
| 138 | desc[i].addr = sg_phys(sg); |
| 139 | desc[i].len = sg->length; |
| 140 | desc[i].next = i+1; |
| 141 | sg++; |
| 142 | } |
| 143 | |
| 144 | /* Last one doesn't continue. */ |
| 145 | desc[i-1].flags &= ~VRING_DESC_F_NEXT; |
| 146 | desc[i-1].next = 0; |
| 147 | |
| 148 | /* We're about to use a buffer */ |
| 149 | vq->num_free--; |
| 150 | |
| 151 | /* Use a single buffer which doesn't continue */ |
| 152 | head = vq->free_head; |
| 153 | vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT; |
| 154 | vq->vring.desc[head].addr = virt_to_phys(desc); |
| 155 | vq->vring.desc[head].len = i * sizeof(struct vring_desc); |
| 156 | |
| 157 | /* Update free pointer */ |
| 158 | vq->free_head = vq->vring.desc[head].next; |
| 159 | |
| 160 | return head; |
| 161 | } |
| 162 | |
Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 163 | int virtqueue_add_buf_gfp(struct virtqueue *_vq, |
| 164 | struct scatterlist sg[], |
| 165 | unsigned int out, |
| 166 | unsigned int in, |
| 167 | void *data, |
| 168 | gfp_t gfp) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 169 | { |
| 170 | struct vring_virtqueue *vq = to_vvq(_vq); |
Michael S. Tsirkin | 1fe9b6f | 2010-07-26 16:55:30 +0930 | [diff] [blame] | 171 | unsigned int i, avail, uninitialized_var(prev); |
| 172 | int head; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 173 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 174 | START_USE(vq); |
| 175 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 176 | BUG_ON(data == NULL); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 177 | |
| 178 | /* If the host supports indirect descriptor tables, and we have multiple |
| 179 | * buffers, then go indirect. FIXME: tune this threshold */ |
| 180 | if (vq->indirect && (out + in) > 1 && vq->num_free) { |
Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 181 | head = vring_add_indirect(vq, sg, out, in, gfp); |
Michael S. Tsirkin | 1fe9b6f | 2010-07-26 16:55:30 +0930 | [diff] [blame] | 182 | if (likely(head >= 0)) |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 183 | goto add_head; |
| 184 | } |
| 185 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 186 | BUG_ON(out + in > vq->vring.num); |
| 187 | BUG_ON(out + in == 0); |
| 188 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 189 | if (vq->num_free < out + in) { |
| 190 | pr_debug("Can't add buf len %i - avail = %i\n", |
| 191 | out + in, vq->num_free); |
Rusty Russell | 44653ea | 2008-07-25 12:06:04 -0500 | [diff] [blame] | 192 | /* FIXME: for historical reasons, we force a notify here if |
| 193 | * there are outgoing parts to the buffer. Presumably the |
| 194 | * host should service the ring ASAP. */ |
| 195 | if (out) |
| 196 | vq->notify(&vq->vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 197 | END_USE(vq); |
| 198 | return -ENOSPC; |
| 199 | } |
| 200 | |
| 201 | /* We're about to use some buffers from the free list. */ |
| 202 | vq->num_free -= out + in; |
| 203 | |
| 204 | head = vq->free_head; |
| 205 | for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) { |
| 206 | vq->vring.desc[i].flags = VRING_DESC_F_NEXT; |
Rusty Russell | 15f9c89 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 207 | vq->vring.desc[i].addr = sg_phys(sg); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 208 | vq->vring.desc[i].len = sg->length; |
| 209 | prev = i; |
| 210 | sg++; |
| 211 | } |
| 212 | for (; in; i = vq->vring.desc[i].next, in--) { |
| 213 | 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] | 214 | vq->vring.desc[i].addr = sg_phys(sg); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 215 | vq->vring.desc[i].len = sg->length; |
| 216 | prev = i; |
| 217 | sg++; |
| 218 | } |
| 219 | /* Last one doesn't continue. */ |
| 220 | vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT; |
| 221 | |
| 222 | /* Update free pointer */ |
| 223 | vq->free_head = i; |
| 224 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 225 | add_head: |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 226 | /* Set token. */ |
| 227 | vq->data[head] = data; |
| 228 | |
| 229 | /* Put entry in available array (but don't update avail->idx until they |
| 230 | * do sync). FIXME: avoid modulus here? */ |
| 231 | avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num; |
| 232 | vq->vring.avail->ring[avail] = head; |
| 233 | |
| 234 | pr_debug("Added buffer head %i to %p\n", head, vq); |
| 235 | END_USE(vq); |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 236 | |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 237 | return vq->num_free; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 238 | } |
Michael S. Tsirkin | bbd603e | 2010-04-29 17:26:37 +0300 | [diff] [blame] | 239 | EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 240 | |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 241 | void virtqueue_kick(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 242 | { |
| 243 | struct vring_virtqueue *vq = to_vvq(_vq); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 244 | u16 new, old; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 245 | START_USE(vq); |
| 246 | /* Descriptors and available array need to be set before we expose the |
| 247 | * new available array entries. */ |
Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 248 | virtio_wmb(); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 249 | |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 250 | old = vq->vring.avail->idx; |
| 251 | new = vq->vring.avail->idx = old + vq->num_added; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 252 | vq->num_added = 0; |
| 253 | |
| 254 | /* Need to update avail index before checking if we should notify */ |
Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 255 | virtio_mb(); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 256 | |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 257 | if (vq->event ? |
| 258 | vring_need_event(vring_avail_event(&vq->vring), new, old) : |
| 259 | !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY)) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 260 | /* Prod other side to tell it about changes. */ |
| 261 | vq->notify(&vq->vq); |
| 262 | |
| 263 | END_USE(vq); |
| 264 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 265 | EXPORT_SYMBOL_GPL(virtqueue_kick); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 266 | |
| 267 | static void detach_buf(struct vring_virtqueue *vq, unsigned int head) |
| 268 | { |
| 269 | unsigned int i; |
| 270 | |
| 271 | /* Clear data ptr. */ |
| 272 | vq->data[head] = NULL; |
| 273 | |
| 274 | /* Put back on free list: find end */ |
| 275 | i = head; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 276 | |
| 277 | /* Free the indirect table */ |
| 278 | if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT) |
| 279 | kfree(phys_to_virt(vq->vring.desc[i].addr)); |
| 280 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 281 | while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) { |
| 282 | i = vq->vring.desc[i].next; |
| 283 | vq->num_free++; |
| 284 | } |
| 285 | |
| 286 | vq->vring.desc[i].next = vq->free_head; |
| 287 | vq->free_head = head; |
| 288 | /* Plus final descriptor */ |
| 289 | vq->num_free++; |
| 290 | } |
| 291 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 292 | static inline bool more_used(const struct vring_virtqueue *vq) |
| 293 | { |
| 294 | return vq->last_used_idx != vq->vring.used->idx; |
| 295 | } |
| 296 | |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 297 | void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 298 | { |
| 299 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 300 | void *ret; |
| 301 | unsigned int i; |
| 302 | |
| 303 | START_USE(vq); |
| 304 | |
Rusty Russell | 5ef8275 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 305 | if (unlikely(vq->broken)) { |
| 306 | END_USE(vq); |
| 307 | return NULL; |
| 308 | } |
| 309 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 310 | if (!more_used(vq)) { |
| 311 | pr_debug("No more buffers in queue\n"); |
| 312 | END_USE(vq); |
| 313 | return NULL; |
| 314 | } |
| 315 | |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 316 | /* Only get used array entries after they have been exposed by host. */ |
Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 317 | virtio_rmb(); |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 318 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 319 | i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id; |
| 320 | *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len; |
| 321 | |
| 322 | if (unlikely(i >= vq->vring.num)) { |
| 323 | BAD_RING(vq, "id %u out of range\n", i); |
| 324 | return NULL; |
| 325 | } |
| 326 | if (unlikely(!vq->data[i])) { |
| 327 | BAD_RING(vq, "id %u is not a head!\n", i); |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | /* detach_buf clears data, so grab it now. */ |
| 332 | ret = vq->data[i]; |
| 333 | detach_buf(vq, i); |
| 334 | vq->last_used_idx++; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 335 | /* If we expect an interrupt for the next entry, tell host |
| 336 | * by writing event index and flush out the write before |
| 337 | * the read in the next get_buf call. */ |
| 338 | if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) { |
| 339 | vring_used_event(&vq->vring) = vq->last_used_idx; |
| 340 | virtio_mb(); |
| 341 | } |
| 342 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 343 | END_USE(vq); |
| 344 | return ret; |
| 345 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 346 | EXPORT_SYMBOL_GPL(virtqueue_get_buf); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 347 | |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 348 | void virtqueue_disable_cb(struct virtqueue *_vq) |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 349 | { |
| 350 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 351 | |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 352 | vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 353 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 354 | EXPORT_SYMBOL_GPL(virtqueue_disable_cb); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 355 | |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 356 | bool virtqueue_enable_cb(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 357 | { |
| 358 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 359 | |
| 360 | START_USE(vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 361 | |
| 362 | /* We optimistically turn back on interrupts, then check if there was |
| 363 | * more to do. */ |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 364 | /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to |
| 365 | * either clear the flags bit or point the event index at the next |
| 366 | * entry. Always do both to keep code simple. */ |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 367 | vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 368 | vring_used_event(&vq->vring) = vq->last_used_idx; |
Michael S. Tsirkin | d57ed95 | 2010-01-28 00:42:23 +0200 | [diff] [blame] | 369 | virtio_mb(); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 370 | if (unlikely(more_used(vq))) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 371 | END_USE(vq); |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | END_USE(vq); |
| 376 | return true; |
| 377 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 378 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 379 | |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 380 | bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) |
| 381 | { |
| 382 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 383 | u16 bufs; |
| 384 | |
| 385 | START_USE(vq); |
| 386 | |
| 387 | /* We optimistically turn back on interrupts, then check if there was |
| 388 | * more to do. */ |
| 389 | /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to |
| 390 | * either clear the flags bit or point the event index at the next |
| 391 | * entry. Always do both to keep code simple. */ |
| 392 | vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; |
| 393 | /* TODO: tune this threshold */ |
| 394 | bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4; |
| 395 | vring_used_event(&vq->vring) = vq->last_used_idx + bufs; |
| 396 | virtio_mb(); |
| 397 | if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) { |
| 398 | END_USE(vq); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | END_USE(vq); |
| 403 | return true; |
| 404 | } |
| 405 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); |
| 406 | |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 407 | void *virtqueue_detach_unused_buf(struct virtqueue *_vq) |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 408 | { |
| 409 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 410 | unsigned int i; |
| 411 | void *buf; |
| 412 | |
| 413 | START_USE(vq); |
| 414 | |
| 415 | for (i = 0; i < vq->vring.num; i++) { |
| 416 | if (!vq->data[i]) |
| 417 | continue; |
| 418 | /* detach_buf clears data, so grab it now. */ |
| 419 | buf = vq->data[i]; |
| 420 | detach_buf(vq, i); |
Amit Shah | b3258ff | 2011-03-16 19:12:10 +0530 | [diff] [blame] | 421 | vq->vring.avail->idx--; |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 422 | END_USE(vq); |
| 423 | return buf; |
| 424 | } |
| 425 | /* That should have freed everything. */ |
| 426 | BUG_ON(vq->num_free != vq->vring.num); |
| 427 | |
| 428 | END_USE(vq); |
| 429 | return NULL; |
| 430 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 431 | EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 432 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 433 | irqreturn_t vring_interrupt(int irq, void *_vq) |
| 434 | { |
| 435 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 436 | |
| 437 | if (!more_used(vq)) { |
| 438 | pr_debug("virtqueue interrupt with no work for %p\n", vq); |
| 439 | return IRQ_NONE; |
| 440 | } |
| 441 | |
| 442 | if (unlikely(vq->broken)) |
| 443 | return IRQ_HANDLED; |
| 444 | |
| 445 | pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 446 | if (vq->vq.callback) |
| 447 | vq->vq.callback(&vq->vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 448 | |
| 449 | return IRQ_HANDLED; |
| 450 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 451 | EXPORT_SYMBOL_GPL(vring_interrupt); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 452 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 453 | struct virtqueue *vring_new_virtqueue(unsigned int num, |
Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 454 | unsigned int vring_align, |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 455 | struct virtio_device *vdev, |
| 456 | void *pages, |
| 457 | void (*notify)(struct virtqueue *), |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 458 | void (*callback)(struct virtqueue *), |
| 459 | const char *name) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 460 | { |
| 461 | struct vring_virtqueue *vq; |
| 462 | unsigned int i; |
| 463 | |
Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 464 | /* We assume num is a power of 2. */ |
| 465 | if (num & (num - 1)) { |
| 466 | dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); |
| 467 | return NULL; |
| 468 | } |
| 469 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 470 | vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL); |
| 471 | if (!vq) |
| 472 | return NULL; |
| 473 | |
Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 474 | vring_init(&vq->vring, num, pages, vring_align); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 475 | vq->vq.callback = callback; |
| 476 | vq->vq.vdev = vdev; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 477 | vq->vq.name = name; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 478 | vq->notify = notify; |
| 479 | vq->broken = false; |
| 480 | vq->last_used_idx = 0; |
| 481 | vq->num_added = 0; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 482 | list_add_tail(&vq->vq.list, &vdev->vqs); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 483 | #ifdef DEBUG |
| 484 | vq->in_use = false; |
| 485 | #endif |
| 486 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 487 | vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 488 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 489 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 490 | /* No callback? Tell other side not to bother us. */ |
| 491 | if (!callback) |
| 492 | vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; |
| 493 | |
| 494 | /* Put everything in free lists. */ |
| 495 | vq->num_free = num; |
| 496 | vq->free_head = 0; |
Amit Shah | 3b87062 | 2010-02-12 10:32:14 +0530 | [diff] [blame] | 497 | for (i = 0; i < num-1; i++) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 498 | vq->vring.desc[i].next = i+1; |
Amit Shah | 3b87062 | 2010-02-12 10:32:14 +0530 | [diff] [blame] | 499 | vq->data[i] = NULL; |
| 500 | } |
| 501 | vq->data[i] = NULL; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 502 | |
| 503 | return &vq->vq; |
| 504 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 505 | EXPORT_SYMBOL_GPL(vring_new_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 506 | |
| 507 | void vring_del_virtqueue(struct virtqueue *vq) |
| 508 | { |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 509 | list_del(&vq->list); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 510 | kfree(to_vvq(vq)); |
| 511 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 512 | EXPORT_SYMBOL_GPL(vring_del_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 513 | |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 514 | /* Manipulates transport-specific feature bits. */ |
| 515 | void vring_transport_features(struct virtio_device *vdev) |
| 516 | { |
| 517 | unsigned int i; |
| 518 | |
| 519 | for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { |
| 520 | switch (i) { |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 521 | case VIRTIO_RING_F_INDIRECT_DESC: |
| 522 | break; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 523 | case VIRTIO_RING_F_EVENT_IDX: |
| 524 | break; |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 525 | default: |
| 526 | /* We don't understand this bit. */ |
| 527 | clear_bit(i, vdev->features); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | EXPORT_SYMBOL_GPL(vring_transport_features); |
| 532 | |
Rick Jones | 8f9f466 | 2011-10-19 08:10:59 +0000 | [diff] [blame] | 533 | /* return the size of the vring within the virtqueue */ |
| 534 | unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) |
| 535 | { |
| 536 | |
| 537 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 538 | |
| 539 | return vq->vring.num; |
| 540 | } |
| 541 | EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); |
| 542 | |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 543 | MODULE_LICENSE("GPL"); |