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