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