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