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> |
Joel Stanley | 6abb2dd | 2014-02-13 15:03:46 +1030 | [diff] [blame] | 26 | #include <linux/kmemleak.h> |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 27 | |
| 28 | #ifdef DEBUG |
| 29 | /* For development, we want to crash whenever the ring is screwed. */ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 30 | #define BAD_RING(_vq, fmt, args...) \ |
| 31 | do { \ |
| 32 | dev_err(&(_vq)->vq.vdev->dev, \ |
| 33 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 34 | BUG(); \ |
| 35 | } while (0) |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 36 | /* Caller is supposed to guarantee no reentry. */ |
| 37 | #define START_USE(_vq) \ |
| 38 | do { \ |
| 39 | if ((_vq)->in_use) \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 40 | panic("%s:in_use = %i\n", \ |
| 41 | (_vq)->vq.name, (_vq)->in_use); \ |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 42 | (_vq)->in_use = __LINE__; \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 43 | } while (0) |
Roel Kluin | 3a35ce7 | 2009-01-22 16:42:57 +0100 | [diff] [blame] | 44 | #define END_USE(_vq) \ |
Rusty Russell | 97a545a | 2010-02-24 14:22:22 -0600 | [diff] [blame] | 45 | do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 46 | #else |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 47 | #define BAD_RING(_vq, fmt, args...) \ |
| 48 | do { \ |
| 49 | dev_err(&_vq->vq.vdev->dev, \ |
| 50 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 51 | (_vq)->broken = true; \ |
| 52 | } while (0) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 53 | #define START_USE(vq) |
| 54 | #define END_USE(vq) |
| 55 | #endif |
| 56 | |
Michael S. Tsirkin | 43b4f72 | 2015-01-15 13:33:31 +0200 | [diff] [blame] | 57 | struct vring_virtqueue { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 58 | struct virtqueue vq; |
| 59 | |
| 60 | /* Actual memory layout for this queue */ |
| 61 | struct vring vring; |
| 62 | |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 63 | /* Can we use weak barriers? */ |
| 64 | bool weak_barriers; |
| 65 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 66 | /* Other side has made a mess, don't try any more. */ |
| 67 | bool broken; |
| 68 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 69 | /* Host supports indirect buffers */ |
| 70 | bool indirect; |
| 71 | |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 72 | /* Host publishes avail event idx */ |
| 73 | bool event; |
| 74 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 75 | /* Head of free buffer list. */ |
| 76 | unsigned int free_head; |
| 77 | /* Number we've added since last sync. */ |
| 78 | unsigned int num_added; |
| 79 | |
| 80 | /* Last used index we've seen. */ |
Anthony Liguori | 1bc4953 | 2007-11-07 15:49:24 -0600 | [diff] [blame] | 81 | u16 last_used_idx; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 82 | |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 83 | /* Last written value to avail->flags */ |
| 84 | u16 avail_flags_shadow; |
| 85 | |
| 86 | /* Last written value to avail->idx in guest byte order */ |
| 87 | u16 avail_idx_shadow; |
| 88 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 89 | /* How to notify other side. FIXME: commonalize hcalls! */ |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 90 | bool (*notify)(struct virtqueue *vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 91 | |
| 92 | #ifdef DEBUG |
| 93 | /* They're supposed to lock for us. */ |
| 94 | unsigned int in_use; |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 95 | |
| 96 | /* Figure out if their kicks are too delayed. */ |
| 97 | bool last_add_time_valid; |
| 98 | ktime_t last_add_time; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 99 | #endif |
| 100 | |
| 101 | /* Tokens for callbacks. */ |
| 102 | void *data[]; |
| 103 | }; |
| 104 | |
| 105 | #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) |
| 106 | |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 107 | static struct vring_desc *alloc_indirect(struct virtqueue *_vq, |
| 108 | unsigned int total_sg, gfp_t gfp) |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 109 | { |
| 110 | struct vring_desc *desc; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 111 | unsigned int i; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 112 | |
Will Deacon | b92b1b8 | 2012-10-19 14:03:33 +0100 | [diff] [blame] | 113 | /* |
| 114 | * We require lowmem mappings for the descriptors because |
| 115 | * otherwise virt_to_phys will give us bogus addresses in the |
| 116 | * virtqueue. |
| 117 | */ |
Michal Hocko | 8210753 | 2015-12-01 15:32:49 +0100 | [diff] [blame] | 118 | gfp &= ~__GFP_HIGHMEM; |
Will Deacon | b92b1b8 | 2012-10-19 14:03:33 +0100 | [diff] [blame] | 119 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 120 | desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 121 | if (!desc) |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 122 | return NULL; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 123 | |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 124 | for (i = 0; i < total_sg; i++) |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 125 | desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 126 | return desc; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 129 | static inline int virtqueue_add(struct virtqueue *_vq, |
| 130 | struct scatterlist *sgs[], |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 131 | unsigned int total_sg, |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 132 | unsigned int out_sgs, |
| 133 | unsigned int in_sgs, |
| 134 | void *data, |
| 135 | gfp_t gfp) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 136 | { |
| 137 | struct vring_virtqueue *vq = to_vvq(_vq); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 138 | struct scatterlist *sg; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 139 | struct vring_desc *desc; |
| 140 | unsigned int i, n, avail, descs_used, uninitialized_var(prev); |
Michael S. Tsirkin | 1fe9b6f | 2010-07-26 16:55:30 +0930 | [diff] [blame] | 141 | int head; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 142 | bool indirect; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 143 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 144 | START_USE(vq); |
| 145 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 146 | BUG_ON(data == NULL); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 147 | |
Rusty Russell | 7067044 | 2014-03-13 11:23:40 +1030 | [diff] [blame] | 148 | if (unlikely(vq->broken)) { |
| 149 | END_USE(vq); |
| 150 | return -EIO; |
| 151 | } |
| 152 | |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 153 | #ifdef DEBUG |
| 154 | { |
| 155 | ktime_t now = ktime_get(); |
| 156 | |
| 157 | /* No kick or get, with .1 second between? Warn. */ |
| 158 | if (vq->last_add_time_valid) |
| 159 | WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time)) |
| 160 | > 100); |
| 161 | vq->last_add_time = now; |
| 162 | vq->last_add_time_valid = true; |
| 163 | } |
| 164 | #endif |
| 165 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 166 | BUG_ON(total_sg > vq->vring.num); |
| 167 | BUG_ON(total_sg == 0); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 168 | |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 169 | head = vq->free_head; |
| 170 | |
| 171 | /* If the host supports indirect descriptor tables, and we have multiple |
| 172 | * buffers, then go indirect. FIXME: tune this threshold */ |
| 173 | if (vq->indirect && total_sg > 1 && vq->vq.num_free) |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 174 | desc = alloc_indirect(_vq, total_sg, gfp); |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 175 | else |
| 176 | desc = NULL; |
| 177 | |
| 178 | if (desc) { |
| 179 | /* Use a single buffer which doesn't continue */ |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 180 | vq->vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_INDIRECT); |
| 181 | vq->vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, virt_to_phys(desc)); |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 182 | /* avoid kmemleak false positive (hidden by virt_to_phys) */ |
| 183 | kmemleak_ignore(desc); |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 184 | vq->vring.desc[head].len = cpu_to_virtio32(_vq->vdev, total_sg * sizeof(struct vring_desc)); |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 185 | |
| 186 | /* Set up rest to use this indirect table. */ |
| 187 | i = 0; |
| 188 | descs_used = 1; |
| 189 | indirect = true; |
| 190 | } else { |
| 191 | desc = vq->vring.desc; |
| 192 | i = head; |
| 193 | descs_used = total_sg; |
| 194 | indirect = false; |
| 195 | } |
| 196 | |
| 197 | if (vq->vq.num_free < descs_used) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 198 | pr_debug("Can't add buf len %i - avail = %i\n", |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 199 | descs_used, vq->vq.num_free); |
Rusty Russell | 44653ea | 2008-07-25 12:06:04 -0500 | [diff] [blame] | 200 | /* FIXME: for historical reasons, we force a notify here if |
| 201 | * there are outgoing parts to the buffer. Presumably the |
| 202 | * host should service the ring ASAP. */ |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 203 | if (out_sgs) |
Rusty Russell | 44653ea | 2008-07-25 12:06:04 -0500 | [diff] [blame] | 204 | vq->notify(&vq->vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 205 | END_USE(vq); |
| 206 | return -ENOSPC; |
| 207 | } |
| 208 | |
| 209 | /* We're about to use some buffers from the free list. */ |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 210 | vq->vq.num_free -= descs_used; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 211 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 212 | for (n = 0; n < out_sgs; n++) { |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 213 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 214 | desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT); |
| 215 | desc[i].addr = cpu_to_virtio64(_vq->vdev, sg_phys(sg)); |
| 216 | desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 217 | prev = i; |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 218 | i = virtio16_to_cpu(_vq->vdev, desc[i].next); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 219 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 220 | } |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 221 | for (; n < (out_sgs + in_sgs); n++) { |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 222 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 223 | desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE); |
| 224 | desc[i].addr = cpu_to_virtio64(_vq->vdev, sg_phys(sg)); |
| 225 | desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 226 | prev = i; |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 227 | i = virtio16_to_cpu(_vq->vdev, desc[i].next); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 228 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 229 | } |
| 230 | /* Last one doesn't continue. */ |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 231 | desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 232 | |
| 233 | /* Update free pointer */ |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 234 | if (indirect) |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 235 | vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next); |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 236 | else |
| 237 | vq->free_head = i; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 238 | |
| 239 | /* Set token. */ |
| 240 | vq->data[head] = data; |
| 241 | |
| 242 | /* 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] | 243 | * do sync). */ |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 244 | avail = vq->avail_idx_shadow & (vq->vring.num - 1); |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 245 | vq->vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 246 | |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 247 | /* Descriptors and available array need to be set before we expose the |
| 248 | * new available array entries. */ |
Rusty Russell | a9a0fef | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 249 | virtio_wmb(vq->weak_barriers); |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 250 | vq->avail_idx_shadow++; |
| 251 | vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow); |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 252 | vq->num_added++; |
| 253 | |
Tetsuo Handa | 5e05bf5 | 2015-02-11 15:01:13 +1030 | [diff] [blame] | 254 | pr_debug("Added buffer head %i to %p\n", head, vq); |
| 255 | END_USE(vq); |
| 256 | |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 257 | /* This is very unlikely, but theoretically possible. Kick |
| 258 | * just in case. */ |
| 259 | if (unlikely(vq->num_added == (1 << 16) - 1)) |
| 260 | virtqueue_kick(_vq); |
| 261 | |
Rusty Russell | 98e8c6b | 2012-10-16 23:56:15 +1030 | [diff] [blame] | 262 | return 0; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 263 | } |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 264 | |
| 265 | /** |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 266 | * virtqueue_add_sgs - expose buffers to other end |
| 267 | * @vq: the struct virtqueue we're talking about. |
| 268 | * @sgs: array of terminated scatterlists. |
| 269 | * @out_num: the number of scatterlists readable by other side |
| 270 | * @in_num: the number of scatterlists which are writable (after readable ones) |
| 271 | * @data: the token identifying the buffer. |
| 272 | * @gfp: how to do memory allocations (if necessary). |
| 273 | * |
| 274 | * Caller must ensure we don't call this with other virtqueue operations |
| 275 | * at the same time (except where noted). |
| 276 | * |
Rusty Russell | 7067044 | 2014-03-13 11:23:40 +1030 | [diff] [blame] | 277 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 278 | */ |
| 279 | int virtqueue_add_sgs(struct virtqueue *_vq, |
| 280 | struct scatterlist *sgs[], |
| 281 | unsigned int out_sgs, |
| 282 | unsigned int in_sgs, |
| 283 | void *data, |
| 284 | gfp_t gfp) |
| 285 | { |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 286 | unsigned int i, total_sg = 0; |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 287 | |
| 288 | /* Count them first. */ |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 289 | for (i = 0; i < out_sgs + in_sgs; i++) { |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 290 | struct scatterlist *sg; |
| 291 | for (sg = sgs[i]; sg; sg = sg_next(sg)) |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 292 | total_sg++; |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 293 | } |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 294 | return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 295 | } |
| 296 | EXPORT_SYMBOL_GPL(virtqueue_add_sgs); |
| 297 | |
| 298 | /** |
Rusty Russell | 282edb3 | 2013-03-20 15:44:26 +1030 | [diff] [blame] | 299 | * virtqueue_add_outbuf - expose output buffers to other end |
| 300 | * @vq: the struct virtqueue we're talking about. |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 301 | * @sg: scatterlist (must be well-formed and terminated!) |
| 302 | * @num: the number of entries in @sg readable by other side |
Rusty Russell | 282edb3 | 2013-03-20 15:44:26 +1030 | [diff] [blame] | 303 | * @data: the token identifying the buffer. |
| 304 | * @gfp: how to do memory allocations (if necessary). |
| 305 | * |
| 306 | * Caller must ensure we don't call this with other virtqueue operations |
| 307 | * at the same time (except where noted). |
| 308 | * |
Rusty Russell | 7067044 | 2014-03-13 11:23:40 +1030 | [diff] [blame] | 309 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
Rusty Russell | 282edb3 | 2013-03-20 15:44:26 +1030 | [diff] [blame] | 310 | */ |
| 311 | int virtqueue_add_outbuf(struct virtqueue *vq, |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 312 | struct scatterlist *sg, unsigned int num, |
Rusty Russell | 282edb3 | 2013-03-20 15:44:26 +1030 | [diff] [blame] | 313 | void *data, |
| 314 | gfp_t gfp) |
| 315 | { |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 316 | return virtqueue_add(vq, &sg, num, 1, 0, data, gfp); |
Rusty Russell | 282edb3 | 2013-03-20 15:44:26 +1030 | [diff] [blame] | 317 | } |
| 318 | EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); |
| 319 | |
| 320 | /** |
| 321 | * virtqueue_add_inbuf - expose input buffers to other end |
| 322 | * @vq: the struct virtqueue we're talking about. |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 323 | * @sg: scatterlist (must be well-formed and terminated!) |
| 324 | * @num: the number of entries in @sg writable by other side |
Rusty Russell | 282edb3 | 2013-03-20 15:44:26 +1030 | [diff] [blame] | 325 | * @data: the token identifying the buffer. |
| 326 | * @gfp: how to do memory allocations (if necessary). |
| 327 | * |
| 328 | * Caller must ensure we don't call this with other virtqueue operations |
| 329 | * at the same time (except where noted). |
| 330 | * |
Rusty Russell | 7067044 | 2014-03-13 11:23:40 +1030 | [diff] [blame] | 331 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
Rusty Russell | 282edb3 | 2013-03-20 15:44:26 +1030 | [diff] [blame] | 332 | */ |
| 333 | int virtqueue_add_inbuf(struct virtqueue *vq, |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 334 | struct scatterlist *sg, unsigned int num, |
Rusty Russell | 282edb3 | 2013-03-20 15:44:26 +1030 | [diff] [blame] | 335 | void *data, |
| 336 | gfp_t gfp) |
| 337 | { |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 338 | return virtqueue_add(vq, &sg, num, 0, 1, data, gfp); |
Rusty Russell | 282edb3 | 2013-03-20 15:44:26 +1030 | [diff] [blame] | 339 | } |
| 340 | EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); |
| 341 | |
| 342 | /** |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 343 | * virtqueue_kick_prepare - first half of split virtqueue_kick call. |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 344 | * @vq: the struct virtqueue |
| 345 | * |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 346 | * Instead of virtqueue_kick(), you can do: |
| 347 | * if (virtqueue_kick_prepare(vq)) |
| 348 | * virtqueue_notify(vq); |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 349 | * |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 350 | * This is sometimes useful because the virtqueue_kick_prepare() needs |
| 351 | * to be serialized, but the actual virtqueue_notify() call does not. |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 352 | */ |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 353 | bool virtqueue_kick_prepare(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 354 | { |
| 355 | struct vring_virtqueue *vq = to_vvq(_vq); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 356 | u16 new, old; |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 357 | bool needs_kick; |
| 358 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 359 | START_USE(vq); |
Jason Wang | a72caae | 2012-01-20 16:17:08 +0800 | [diff] [blame] | 360 | /* We need to expose available array entries before checking avail |
| 361 | * event. */ |
Rusty Russell | a9a0fef | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 362 | virtio_mb(vq->weak_barriers); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 363 | |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 364 | old = vq->avail_idx_shadow - vq->num_added; |
| 365 | new = vq->avail_idx_shadow; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 366 | vq->num_added = 0; |
| 367 | |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 368 | #ifdef DEBUG |
| 369 | if (vq->last_add_time_valid) { |
| 370 | WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), |
| 371 | vq->last_add_time)) > 100); |
| 372 | } |
| 373 | vq->last_add_time_valid = false; |
| 374 | #endif |
| 375 | |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 376 | if (vq->event) { |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 377 | needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, vring_avail_event(&vq->vring)), |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 378 | new, old); |
| 379 | } else { |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 380 | needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(_vq->vdev, VRING_USED_F_NO_NOTIFY)); |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 381 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 382 | END_USE(vq); |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 383 | return needs_kick; |
| 384 | } |
| 385 | EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); |
| 386 | |
| 387 | /** |
| 388 | * virtqueue_notify - second half of split virtqueue_kick call. |
| 389 | * @vq: the struct virtqueue |
| 390 | * |
| 391 | * This does not need to be serialized. |
Heinz Graalfs | 5b1bf7c | 2013-10-29 09:39:48 +1030 | [diff] [blame] | 392 | * |
| 393 | * Returns false if host notify failed or queue is broken, otherwise true. |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 394 | */ |
Heinz Graalfs | 5b1bf7c | 2013-10-29 09:39:48 +1030 | [diff] [blame] | 395 | bool virtqueue_notify(struct virtqueue *_vq) |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 396 | { |
| 397 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 398 | |
Heinz Graalfs | 5b1bf7c | 2013-10-29 09:39:48 +1030 | [diff] [blame] | 399 | if (unlikely(vq->broken)) |
| 400 | return false; |
| 401 | |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 402 | /* Prod other side to tell it about changes. */ |
Heinz Graalfs | 2342d6a | 2013-11-05 21:20:27 +1030 | [diff] [blame] | 403 | if (!vq->notify(_vq)) { |
Heinz Graalfs | 5b1bf7c | 2013-10-29 09:39:48 +1030 | [diff] [blame] | 404 | vq->broken = true; |
| 405 | return false; |
| 406 | } |
| 407 | return true; |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 408 | } |
| 409 | EXPORT_SYMBOL_GPL(virtqueue_notify); |
| 410 | |
| 411 | /** |
| 412 | * virtqueue_kick - update after add_buf |
| 413 | * @vq: the struct virtqueue |
| 414 | * |
Rusty Russell | b3087e4 | 2013-05-20 12:15:44 +0930 | [diff] [blame] | 415 | * After one or more virtqueue_add_* calls, invoke this to kick |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 416 | * the other side. |
| 417 | * |
| 418 | * Caller must ensure we don't call this with other virtqueue |
| 419 | * operations at the same time (except where noted). |
Heinz Graalfs | 5b1bf7c | 2013-10-29 09:39:48 +1030 | [diff] [blame] | 420 | * |
| 421 | * Returns false if kick failed, otherwise true. |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 422 | */ |
Heinz Graalfs | 5b1bf7c | 2013-10-29 09:39:48 +1030 | [diff] [blame] | 423 | bool virtqueue_kick(struct virtqueue *vq) |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 424 | { |
| 425 | if (virtqueue_kick_prepare(vq)) |
Heinz Graalfs | 5b1bf7c | 2013-10-29 09:39:48 +1030 | [diff] [blame] | 426 | return virtqueue_notify(vq); |
| 427 | return true; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 428 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 429 | EXPORT_SYMBOL_GPL(virtqueue_kick); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 430 | |
| 431 | static void detach_buf(struct vring_virtqueue *vq, unsigned int head) |
| 432 | { |
| 433 | unsigned int i; |
| 434 | |
| 435 | /* Clear data ptr. */ |
| 436 | vq->data[head] = NULL; |
| 437 | |
| 438 | /* Put back on free list: find end */ |
| 439 | i = head; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 440 | |
| 441 | /* Free the indirect table */ |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 442 | if (vq->vring.desc[i].flags & cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)) |
| 443 | kfree(phys_to_virt(virtio64_to_cpu(vq->vq.vdev, vq->vring.desc[i].addr))); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 444 | |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 445 | while (vq->vring.desc[i].flags & cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT)) { |
| 446 | i = virtio16_to_cpu(vq->vq.vdev, vq->vring.desc[i].next); |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 447 | vq->vq.num_free++; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 448 | } |
| 449 | |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 450 | vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 451 | vq->free_head = head; |
| 452 | /* Plus final descriptor */ |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 453 | vq->vq.num_free++; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 454 | } |
| 455 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 456 | static inline bool more_used(const struct vring_virtqueue *vq) |
| 457 | { |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 458 | return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, vq->vring.used->idx); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 459 | } |
| 460 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 461 | /** |
| 462 | * virtqueue_get_buf - get the next used buffer |
| 463 | * @vq: the struct virtqueue we're talking about. |
| 464 | * @len: the length written into the buffer |
| 465 | * |
| 466 | * If the driver wrote data into the buffer, @len will be set to the |
| 467 | * amount written. This means you don't need to clear the buffer |
| 468 | * beforehand to ensure there's no data leakage in the case of short |
| 469 | * writes. |
| 470 | * |
| 471 | * Caller must ensure we don't call this with other virtqueue |
| 472 | * operations at the same time (except where noted). |
| 473 | * |
| 474 | * Returns NULL if there are no used buffers, or the "data" token |
Rusty Russell | b3087e4 | 2013-05-20 12:15:44 +0930 | [diff] [blame] | 475 | * handed to virtqueue_add_*(). |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 476 | */ |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 477 | void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 478 | { |
| 479 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 480 | void *ret; |
| 481 | unsigned int i; |
Rusty Russell | 3b720b8 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 482 | u16 last_used; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 483 | |
| 484 | START_USE(vq); |
| 485 | |
Rusty Russell | 5ef8275 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 486 | if (unlikely(vq->broken)) { |
| 487 | END_USE(vq); |
| 488 | return NULL; |
| 489 | } |
| 490 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 491 | if (!more_used(vq)) { |
| 492 | pr_debug("No more buffers in queue\n"); |
| 493 | END_USE(vq); |
| 494 | return NULL; |
| 495 | } |
| 496 | |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 497 | /* Only get used array entries after they have been exposed by host. */ |
Rusty Russell | a9a0fef | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 498 | virtio_rmb(vq->weak_barriers); |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 499 | |
Rusty Russell | 3b720b8 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 500 | last_used = (vq->last_used_idx & (vq->vring.num - 1)); |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 501 | i = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].id); |
| 502 | *len = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].len); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 503 | |
| 504 | if (unlikely(i >= vq->vring.num)) { |
| 505 | BAD_RING(vq, "id %u out of range\n", i); |
| 506 | return NULL; |
| 507 | } |
| 508 | if (unlikely(!vq->data[i])) { |
| 509 | BAD_RING(vq, "id %u is not a head!\n", i); |
| 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | /* detach_buf clears data, so grab it now. */ |
| 514 | ret = vq->data[i]; |
| 515 | detach_buf(vq, i); |
| 516 | vq->last_used_idx++; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 517 | /* If we expect an interrupt for the next entry, tell host |
| 518 | * by writing event index and flush out the write before |
| 519 | * the read in the next get_buf call. */ |
Michael S. Tsirkin | 788e5b3 | 2015-12-17 12:20:39 +0200 | [diff] [blame] | 520 | if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) |
| 521 | virtio_store_mb(vq->weak_barriers, |
| 522 | &vring_used_event(&vq->vring), |
| 523 | cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 524 | |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 525 | #ifdef DEBUG |
| 526 | vq->last_add_time_valid = false; |
| 527 | #endif |
| 528 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 529 | END_USE(vq); |
| 530 | return ret; |
| 531 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 532 | EXPORT_SYMBOL_GPL(virtqueue_get_buf); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 533 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 534 | /** |
| 535 | * virtqueue_disable_cb - disable callbacks |
| 536 | * @vq: the struct virtqueue we're talking about. |
| 537 | * |
| 538 | * Note that this is not necessarily synchronous, hence unreliable and only |
| 539 | * useful as an optimization. |
| 540 | * |
| 541 | * Unlike other operations, this need not be serialized. |
| 542 | */ |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 543 | void virtqueue_disable_cb(struct virtqueue *_vq) |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 544 | { |
| 545 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 546 | |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 547 | if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { |
| 548 | vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; |
| 549 | vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow); |
| 550 | } |
| 551 | |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 552 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 553 | EXPORT_SYMBOL_GPL(virtqueue_disable_cb); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 554 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 555 | /** |
Michael S. Tsirkin | cc22988 | 2013-07-09 13:19:18 +0300 | [diff] [blame] | 556 | * virtqueue_enable_cb_prepare - restart callbacks after disable_cb |
| 557 | * @vq: the struct virtqueue we're talking about. |
| 558 | * |
| 559 | * This re-enables callbacks; it returns current queue state |
| 560 | * in an opaque unsigned value. This value should be later tested by |
| 561 | * virtqueue_poll, to detect a possible race between the driver checking for |
| 562 | * more work, and enabling callbacks. |
| 563 | * |
| 564 | * Caller must ensure we don't call this with other virtqueue |
| 565 | * operations at the same time (except where noted). |
| 566 | */ |
| 567 | unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) |
| 568 | { |
| 569 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 570 | u16 last_used_idx; |
| 571 | |
| 572 | START_USE(vq); |
| 573 | |
| 574 | /* We optimistically turn back on interrupts, then check if there was |
| 575 | * more to do. */ |
| 576 | /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to |
| 577 | * either clear the flags bit or point the event index at the next |
| 578 | * entry. Always do both to keep code simple. */ |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 579 | if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { |
| 580 | vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; |
| 581 | vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow); |
| 582 | } |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 583 | vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, last_used_idx = vq->last_used_idx); |
Michael S. Tsirkin | cc22988 | 2013-07-09 13:19:18 +0300 | [diff] [blame] | 584 | END_USE(vq); |
| 585 | return last_used_idx; |
| 586 | } |
| 587 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); |
| 588 | |
| 589 | /** |
| 590 | * virtqueue_poll - query pending used buffers |
| 591 | * @vq: the struct virtqueue we're talking about. |
| 592 | * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). |
| 593 | * |
| 594 | * Returns "true" if there are pending used buffers in the queue. |
| 595 | * |
| 596 | * This does not need to be serialized. |
| 597 | */ |
| 598 | bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) |
| 599 | { |
| 600 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 601 | |
| 602 | virtio_mb(vq->weak_barriers); |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 603 | return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, vq->vring.used->idx); |
Michael S. Tsirkin | cc22988 | 2013-07-09 13:19:18 +0300 | [diff] [blame] | 604 | } |
| 605 | EXPORT_SYMBOL_GPL(virtqueue_poll); |
| 606 | |
| 607 | /** |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 608 | * virtqueue_enable_cb - restart callbacks after disable_cb. |
| 609 | * @vq: the struct virtqueue we're talking about. |
| 610 | * |
| 611 | * This re-enables callbacks; it returns "false" if there are pending |
| 612 | * buffers in the queue, to detect a possible race between the driver |
| 613 | * checking for more work, and enabling callbacks. |
| 614 | * |
| 615 | * Caller must ensure we don't call this with other virtqueue |
| 616 | * operations at the same time (except where noted). |
| 617 | */ |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 618 | bool virtqueue_enable_cb(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 619 | { |
Michael S. Tsirkin | cc22988 | 2013-07-09 13:19:18 +0300 | [diff] [blame] | 620 | unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); |
| 621 | return !virtqueue_poll(_vq, last_used_idx); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 622 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 623 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 624 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 625 | /** |
| 626 | * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. |
| 627 | * @vq: the struct virtqueue we're talking about. |
| 628 | * |
| 629 | * This re-enables callbacks but hints to the other side to delay |
| 630 | * interrupts until most of the available buffers have been processed; |
| 631 | * it returns "false" if there are many pending buffers in the queue, |
| 632 | * to detect a possible race between the driver checking for more work, |
| 633 | * and enabling callbacks. |
| 634 | * |
| 635 | * Caller must ensure we don't call this with other virtqueue |
| 636 | * operations at the same time (except where noted). |
| 637 | */ |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 638 | bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) |
| 639 | { |
| 640 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 641 | u16 bufs; |
| 642 | |
| 643 | START_USE(vq); |
| 644 | |
| 645 | /* We optimistically turn back on interrupts, then check if there was |
| 646 | * more to do. */ |
| 647 | /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to |
| 648 | * either clear the flags bit or point the event index at the next |
| 649 | * entry. Always do both to keep code simple. */ |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 650 | if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { |
| 651 | vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; |
| 652 | vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow); |
| 653 | } |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 654 | /* TODO: tune this threshold */ |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 655 | bufs = (u16)(vq->avail_idx_shadow - vq->last_used_idx) * 3 / 4; |
Michael S. Tsirkin | 788e5b3 | 2015-12-17 12:20:39 +0200 | [diff] [blame] | 656 | |
| 657 | virtio_store_mb(vq->weak_barriers, |
| 658 | &vring_used_event(&vq->vring), |
| 659 | cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); |
| 660 | |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 661 | if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) { |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 662 | END_USE(vq); |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | END_USE(vq); |
| 667 | return true; |
| 668 | } |
| 669 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); |
| 670 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 671 | /** |
| 672 | * virtqueue_detach_unused_buf - detach first unused buffer |
| 673 | * @vq: the struct virtqueue we're talking about. |
| 674 | * |
Rusty Russell | b3087e4 | 2013-05-20 12:15:44 +0930 | [diff] [blame] | 675 | * Returns NULL or the "data" token handed to virtqueue_add_*(). |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 676 | * This is not valid on an active queue; it is useful only for device |
| 677 | * shutdown. |
| 678 | */ |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 679 | void *virtqueue_detach_unused_buf(struct virtqueue *_vq) |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 680 | { |
| 681 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 682 | unsigned int i; |
| 683 | void *buf; |
| 684 | |
| 685 | START_USE(vq); |
| 686 | |
| 687 | for (i = 0; i < vq->vring.num; i++) { |
| 688 | if (!vq->data[i]) |
| 689 | continue; |
| 690 | /* detach_buf clears data, so grab it now. */ |
| 691 | buf = vq->data[i]; |
| 692 | detach_buf(vq, i); |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 693 | vq->avail_idx_shadow--; |
| 694 | vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 695 | END_USE(vq); |
| 696 | return buf; |
| 697 | } |
| 698 | /* That should have freed everything. */ |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 699 | BUG_ON(vq->vq.num_free != vq->vring.num); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 700 | |
| 701 | END_USE(vq); |
| 702 | return NULL; |
| 703 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 704 | EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 705 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 706 | irqreturn_t vring_interrupt(int irq, void *_vq) |
| 707 | { |
| 708 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 709 | |
| 710 | if (!more_used(vq)) { |
| 711 | pr_debug("virtqueue interrupt with no work for %p\n", vq); |
| 712 | return IRQ_NONE; |
| 713 | } |
| 714 | |
| 715 | if (unlikely(vq->broken)) |
| 716 | return IRQ_HANDLED; |
| 717 | |
| 718 | pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 719 | if (vq->vq.callback) |
| 720 | vq->vq.callback(&vq->vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 721 | |
| 722 | return IRQ_HANDLED; |
| 723 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 724 | EXPORT_SYMBOL_GPL(vring_interrupt); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 725 | |
Jason Wang | 17bb6d4 | 2012-08-28 13:54:13 +0200 | [diff] [blame] | 726 | struct virtqueue *vring_new_virtqueue(unsigned int index, |
| 727 | unsigned int num, |
Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 728 | unsigned int vring_align, |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 729 | struct virtio_device *vdev, |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 730 | bool weak_barriers, |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 731 | void *pages, |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 732 | bool (*notify)(struct virtqueue *), |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 733 | void (*callback)(struct virtqueue *), |
| 734 | const char *name) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 735 | { |
| 736 | struct vring_virtqueue *vq; |
| 737 | unsigned int i; |
| 738 | |
Rusty Russell | 42b36cc | 2007-11-12 13:39:18 +1100 | [diff] [blame] | 739 | /* We assume num is a power of 2. */ |
| 740 | if (num & (num - 1)) { |
| 741 | dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); |
| 742 | return NULL; |
| 743 | } |
| 744 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 745 | vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL); |
| 746 | if (!vq) |
| 747 | return NULL; |
| 748 | |
Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 749 | vring_init(&vq->vring, num, pages, vring_align); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 750 | vq->vq.callback = callback; |
| 751 | vq->vq.vdev = vdev; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 752 | vq->vq.name = name; |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 753 | vq->vq.num_free = num; |
| 754 | vq->vq.index = index; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 755 | vq->notify = notify; |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 756 | vq->weak_barriers = weak_barriers; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 757 | vq->broken = false; |
| 758 | vq->last_used_idx = 0; |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 759 | vq->avail_flags_shadow = 0; |
| 760 | vq->avail_idx_shadow = 0; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 761 | vq->num_added = 0; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 762 | list_add_tail(&vq->vq.list, &vdev->vqs); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 763 | #ifdef DEBUG |
| 764 | vq->in_use = false; |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 765 | vq->last_add_time_valid = false; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 766 | #endif |
| 767 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 768 | vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 769 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 770 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 771 | /* No callback? Tell other side not to bother us. */ |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 772 | if (!callback) { |
| 773 | vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; |
| 774 | vq->vring.avail->flags = cpu_to_virtio16(vdev, vq->avail_flags_shadow); |
| 775 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 776 | |
| 777 | /* Put everything in free lists. */ |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 778 | vq->free_head = 0; |
Amit Shah | 3b87062 | 2010-02-12 10:32:14 +0530 | [diff] [blame] | 779 | for (i = 0; i < num-1; i++) { |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 780 | vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1); |
Amit Shah | 3b87062 | 2010-02-12 10:32:14 +0530 | [diff] [blame] | 781 | vq->data[i] = NULL; |
| 782 | } |
| 783 | vq->data[i] = NULL; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 784 | |
| 785 | return &vq->vq; |
| 786 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 787 | EXPORT_SYMBOL_GPL(vring_new_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 788 | |
| 789 | void vring_del_virtqueue(struct virtqueue *vq) |
| 790 | { |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 791 | list_del(&vq->list); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 792 | kfree(to_vvq(vq)); |
| 793 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 794 | EXPORT_SYMBOL_GPL(vring_del_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 795 | |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 796 | /* Manipulates transport-specific feature bits. */ |
| 797 | void vring_transport_features(struct virtio_device *vdev) |
| 798 | { |
| 799 | unsigned int i; |
| 800 | |
| 801 | for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { |
| 802 | switch (i) { |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 803 | case VIRTIO_RING_F_INDIRECT_DESC: |
| 804 | break; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 805 | case VIRTIO_RING_F_EVENT_IDX: |
| 806 | break; |
Michael S. Tsirkin | 747ae34 | 2014-12-01 15:52:40 +0200 | [diff] [blame] | 807 | case VIRTIO_F_VERSION_1: |
| 808 | break; |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 809 | default: |
| 810 | /* We don't understand this bit. */ |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 811 | __virtio_clear_bit(vdev, i); |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | } |
| 815 | EXPORT_SYMBOL_GPL(vring_transport_features); |
| 816 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 817 | /** |
| 818 | * virtqueue_get_vring_size - return the size of the virtqueue's vring |
| 819 | * @vq: the struct virtqueue containing the vring of interest. |
| 820 | * |
| 821 | * Returns the size of the vring. This is mainly used for boasting to |
| 822 | * userspace. Unlike other operations, this need not be serialized. |
| 823 | */ |
Rick Jones | 8f9f466 | 2011-10-19 08:10:59 +0000 | [diff] [blame] | 824 | unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) |
| 825 | { |
| 826 | |
| 827 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 828 | |
| 829 | return vq->vring.num; |
| 830 | } |
| 831 | EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); |
| 832 | |
Heinz Graalfs | b3b32c9 | 2013-10-29 09:40:19 +1030 | [diff] [blame] | 833 | bool virtqueue_is_broken(struct virtqueue *_vq) |
| 834 | { |
| 835 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 836 | |
| 837 | return vq->broken; |
| 838 | } |
| 839 | EXPORT_SYMBOL_GPL(virtqueue_is_broken); |
| 840 | |
Rusty Russell | e2dcdfe | 2014-04-28 11:15:08 +0930 | [diff] [blame] | 841 | /* |
| 842 | * This should prevent the device from being used, allowing drivers to |
| 843 | * recover. You may need to grab appropriate locks to flush. |
| 844 | */ |
| 845 | void virtio_break_device(struct virtio_device *dev) |
| 846 | { |
| 847 | struct virtqueue *_vq; |
| 848 | |
| 849 | list_for_each_entry(_vq, &dev->vqs, list) { |
| 850 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 851 | vq->broken = true; |
| 852 | } |
| 853 | } |
| 854 | EXPORT_SYMBOL_GPL(virtio_break_device); |
| 855 | |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 856 | void *virtqueue_get_avail(struct virtqueue *_vq) |
| 857 | { |
| 858 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 859 | |
| 860 | return vq->vring.avail; |
| 861 | } |
| 862 | EXPORT_SYMBOL_GPL(virtqueue_get_avail); |
| 863 | |
| 864 | void *virtqueue_get_used(struct virtqueue *_vq) |
| 865 | { |
| 866 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 867 | |
| 868 | return vq->vring.used; |
| 869 | } |
| 870 | EXPORT_SYMBOL_GPL(virtqueue_get_used); |
| 871 | |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 872 | MODULE_LICENSE("GPL"); |