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