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