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> |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 26 | #include <linux/dma-mapping.h> |
Andy Lutomirski | 78fe398 | 2016-02-02 21:46:40 -0800 | [diff] [blame] | 27 | #include <xen/xen.h> |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 28 | |
| 29 | #ifdef DEBUG |
| 30 | /* For development, we want to crash whenever the ring is screwed. */ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 31 | #define BAD_RING(_vq, fmt, args...) \ |
| 32 | do { \ |
| 33 | dev_err(&(_vq)->vq.vdev->dev, \ |
| 34 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 35 | BUG(); \ |
| 36 | } while (0) |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 37 | /* Caller is supposed to guarantee no reentry. */ |
| 38 | #define START_USE(_vq) \ |
| 39 | do { \ |
| 40 | if ((_vq)->in_use) \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 41 | panic("%s:in_use = %i\n", \ |
| 42 | (_vq)->vq.name, (_vq)->in_use); \ |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 43 | (_vq)->in_use = __LINE__; \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 44 | } while (0) |
Roel Kluin | 3a35ce7 | 2009-01-22 16:42:57 +0100 | [diff] [blame] | 45 | #define END_USE(_vq) \ |
Rusty Russell | 97a545a | 2010-02-24 14:22:22 -0600 | [diff] [blame] | 46 | do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 47 | #define LAST_ADD_TIME_UPDATE(_vq) \ |
| 48 | do { \ |
| 49 | ktime_t now = ktime_get(); \ |
| 50 | \ |
| 51 | /* No kick or get, with .1 second between? Warn. */ \ |
| 52 | if ((_vq)->last_add_time_valid) \ |
| 53 | WARN_ON(ktime_to_ms(ktime_sub(now, \ |
| 54 | (_vq)->last_add_time)) > 100); \ |
| 55 | (_vq)->last_add_time = now; \ |
| 56 | (_vq)->last_add_time_valid = true; \ |
| 57 | } while (0) |
| 58 | #define LAST_ADD_TIME_CHECK(_vq) \ |
| 59 | do { \ |
| 60 | if ((_vq)->last_add_time_valid) { \ |
| 61 | WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \ |
| 62 | (_vq)->last_add_time)) > 100); \ |
| 63 | } \ |
| 64 | } while (0) |
| 65 | #define LAST_ADD_TIME_INVALID(_vq) \ |
| 66 | ((_vq)->last_add_time_valid = false) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 67 | #else |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 68 | #define BAD_RING(_vq, fmt, args...) \ |
| 69 | do { \ |
| 70 | dev_err(&_vq->vq.vdev->dev, \ |
| 71 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 72 | (_vq)->broken = true; \ |
| 73 | } while (0) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 74 | #define START_USE(vq) |
| 75 | #define END_USE(vq) |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 76 | #define LAST_ADD_TIME_UPDATE(vq) |
| 77 | #define LAST_ADD_TIME_CHECK(vq) |
| 78 | #define LAST_ADD_TIME_INVALID(vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 79 | #endif |
| 80 | |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 81 | struct vring_desc_state_split { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 82 | void *data; /* Data for callback. */ |
| 83 | struct vring_desc *indir_desc; /* Indirect descriptor, if any. */ |
| 84 | }; |
| 85 | |
Michael S. Tsirkin | 43b4f72 | 2015-01-15 13:33:31 +0200 | [diff] [blame] | 86 | struct vring_virtqueue { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 87 | struct virtqueue vq; |
| 88 | |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 89 | /* Can we use weak barriers? */ |
| 90 | bool weak_barriers; |
| 91 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 92 | /* Other side has made a mess, don't try any more. */ |
| 93 | bool broken; |
| 94 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 95 | /* Host supports indirect buffers */ |
| 96 | bool indirect; |
| 97 | |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 98 | /* Host publishes avail event idx */ |
| 99 | bool event; |
| 100 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 101 | /* Head of free buffer list. */ |
| 102 | unsigned int free_head; |
| 103 | /* Number we've added since last sync. */ |
| 104 | unsigned int num_added; |
| 105 | |
| 106 | /* Last used index we've seen. */ |
Anthony Liguori | 1bc4953 | 2007-11-07 15:49:24 -0600 | [diff] [blame] | 107 | u16 last_used_idx; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 108 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 109 | struct { |
| 110 | /* Actual memory layout for this queue */ |
| 111 | struct vring vring; |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 112 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 113 | /* Last written value to avail->flags */ |
| 114 | u16 avail_flags_shadow; |
| 115 | |
| 116 | /* Last written value to avail->idx in guest byte order */ |
| 117 | u16 avail_idx_shadow; |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 118 | |
| 119 | /* Per-descriptor state. */ |
| 120 | struct vring_desc_state_split *desc_state; |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame^] | 121 | |
| 122 | /* DMA, allocation, and size information */ |
| 123 | size_t queue_size_in_bytes; |
| 124 | dma_addr_t queue_dma_addr; |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 125 | } split; |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 126 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 127 | /* How to notify other side. FIXME: commonalize hcalls! */ |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 128 | bool (*notify)(struct virtqueue *vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 129 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 130 | /* DMA, allocation, and size information */ |
| 131 | bool we_own_ring; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 132 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 133 | #ifdef DEBUG |
| 134 | /* They're supposed to lock for us. */ |
| 135 | unsigned int in_use; |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 136 | |
| 137 | /* Figure out if their kicks are too delayed. */ |
| 138 | bool last_add_time_valid; |
| 139 | ktime_t last_add_time; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 140 | #endif |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 141 | }; |
| 142 | |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Helpers. |
| 146 | */ |
| 147 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 148 | #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) |
| 149 | |
Tiwei Bie | 2f18c2d | 2018-11-21 18:03:23 +0800 | [diff] [blame] | 150 | static inline bool virtqueue_use_indirect(struct virtqueue *_vq, |
| 151 | unsigned int total_sg) |
| 152 | { |
| 153 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 154 | |
| 155 | /* |
| 156 | * If the host supports indirect descriptor tables, and we have multiple |
| 157 | * buffers, then go indirect. FIXME: tune this threshold |
| 158 | */ |
| 159 | return (vq->indirect && total_sg > 1 && vq->vq.num_free); |
| 160 | } |
| 161 | |
Andy Lutomirski | d26c96c | 2016-02-02 21:46:35 -0800 | [diff] [blame] | 162 | /* |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 163 | * Modern virtio devices have feature bits to specify whether they need a |
| 164 | * quirk and bypass the IOMMU. If not there, just use the DMA API. |
| 165 | * |
| 166 | * If there, the interaction between virtio and DMA API is messy. |
Andy Lutomirski | d26c96c | 2016-02-02 21:46:35 -0800 | [diff] [blame] | 167 | * |
| 168 | * On most systems with virtio, physical addresses match bus addresses, |
| 169 | * and it doesn't particularly matter whether we use the DMA API. |
| 170 | * |
| 171 | * On some systems, including Xen and any system with a physical device |
| 172 | * that speaks virtio behind a physical IOMMU, we must use the DMA API |
| 173 | * for virtio DMA to work at all. |
| 174 | * |
| 175 | * On other systems, including SPARC and PPC64, virtio-pci devices are |
| 176 | * enumerated as though they are behind an IOMMU, but the virtio host |
| 177 | * ignores the IOMMU, so we must either pretend that the IOMMU isn't |
| 178 | * there or somehow map everything as the identity. |
| 179 | * |
| 180 | * For the time being, we preserve historic behavior and bypass the DMA |
| 181 | * API. |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 182 | * |
| 183 | * TODO: install a per-device DMA ops structure that does the right thing |
| 184 | * taking into account all the above quirks, and use the DMA API |
| 185 | * unconditionally on data path. |
Andy Lutomirski | d26c96c | 2016-02-02 21:46:35 -0800 | [diff] [blame] | 186 | */ |
| 187 | |
| 188 | static bool vring_use_dma_api(struct virtio_device *vdev) |
| 189 | { |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 190 | if (!virtio_has_iommu_quirk(vdev)) |
| 191 | return true; |
| 192 | |
| 193 | /* Otherwise, we are left to guess. */ |
Andy Lutomirski | 78fe398 | 2016-02-02 21:46:40 -0800 | [diff] [blame] | 194 | /* |
| 195 | * In theory, it's possible to have a buggy QEMU-supposed |
| 196 | * emulated Q35 IOMMU and Xen enabled at the same time. On |
| 197 | * such a configuration, virtio has never worked and will |
| 198 | * not work without an even larger kludge. Instead, enable |
| 199 | * the DMA API if we're a Xen guest, which at least allows |
| 200 | * all of the sensible Xen configurations to work correctly. |
| 201 | */ |
| 202 | if (xen_domain()) |
| 203 | return true; |
| 204 | |
Andy Lutomirski | d26c96c | 2016-02-02 21:46:35 -0800 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame^] | 208 | static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, |
| 209 | dma_addr_t *dma_handle, gfp_t flag) |
| 210 | { |
| 211 | if (vring_use_dma_api(vdev)) { |
| 212 | return dma_alloc_coherent(vdev->dev.parent, size, |
| 213 | dma_handle, flag); |
| 214 | } else { |
| 215 | void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag); |
| 216 | |
| 217 | if (queue) { |
| 218 | phys_addr_t phys_addr = virt_to_phys(queue); |
| 219 | *dma_handle = (dma_addr_t)phys_addr; |
| 220 | |
| 221 | /* |
| 222 | * Sanity check: make sure we dind't truncate |
| 223 | * the address. The only arches I can find that |
| 224 | * have 64-bit phys_addr_t but 32-bit dma_addr_t |
| 225 | * are certain non-highmem MIPS and x86 |
| 226 | * configurations, but these configurations |
| 227 | * should never allocate physical pages above 32 |
| 228 | * bits, so this is fine. Just in case, throw a |
| 229 | * warning and abort if we end up with an |
| 230 | * unrepresentable address. |
| 231 | */ |
| 232 | if (WARN_ON_ONCE(*dma_handle != phys_addr)) { |
| 233 | free_pages_exact(queue, PAGE_ALIGN(size)); |
| 234 | return NULL; |
| 235 | } |
| 236 | } |
| 237 | return queue; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static void vring_free_queue(struct virtio_device *vdev, size_t size, |
| 242 | void *queue, dma_addr_t dma_handle) |
| 243 | { |
| 244 | if (vring_use_dma_api(vdev)) |
| 245 | dma_free_coherent(vdev->dev.parent, size, queue, dma_handle); |
| 246 | else |
| 247 | free_pages_exact(queue, PAGE_ALIGN(size)); |
| 248 | } |
| 249 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 250 | /* |
| 251 | * The DMA ops on various arches are rather gnarly right now, and |
| 252 | * making all of the arch DMA ops work on the vring device itself |
| 253 | * is a mess. For now, we use the parent device for DMA ops. |
| 254 | */ |
Michael S. Tsirkin | 75bfa81 | 2016-10-31 00:38:21 +0200 | [diff] [blame] | 255 | static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq) |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 256 | { |
| 257 | return vq->vq.vdev->dev.parent; |
| 258 | } |
| 259 | |
| 260 | /* Map one sg entry. */ |
| 261 | static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq, |
| 262 | struct scatterlist *sg, |
| 263 | enum dma_data_direction direction) |
| 264 | { |
| 265 | if (!vring_use_dma_api(vq->vq.vdev)) |
| 266 | return (dma_addr_t)sg_phys(sg); |
| 267 | |
| 268 | /* |
| 269 | * We can't use dma_map_sg, because we don't use scatterlists in |
| 270 | * the way it expects (we don't guarantee that the scatterlist |
| 271 | * will exist for the lifetime of the mapping). |
| 272 | */ |
| 273 | return dma_map_page(vring_dma_dev(vq), |
| 274 | sg_page(sg), sg->offset, sg->length, |
| 275 | direction); |
| 276 | } |
| 277 | |
| 278 | static dma_addr_t vring_map_single(const struct vring_virtqueue *vq, |
| 279 | void *cpu_addr, size_t size, |
| 280 | enum dma_data_direction direction) |
| 281 | { |
| 282 | if (!vring_use_dma_api(vq->vq.vdev)) |
| 283 | return (dma_addr_t)virt_to_phys(cpu_addr); |
| 284 | |
| 285 | return dma_map_single(vring_dma_dev(vq), |
| 286 | cpu_addr, size, direction); |
| 287 | } |
| 288 | |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 289 | static int vring_mapping_error(const struct vring_virtqueue *vq, |
| 290 | dma_addr_t addr) |
| 291 | { |
| 292 | if (!vring_use_dma_api(vq->vq.vdev)) |
| 293 | return 0; |
| 294 | |
| 295 | return dma_mapping_error(vring_dma_dev(vq), addr); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | /* |
| 300 | * Split ring specific functions - *_split(). |
| 301 | */ |
| 302 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 303 | static void vring_unmap_one_split(const struct vring_virtqueue *vq, |
| 304 | struct vring_desc *desc) |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 305 | { |
| 306 | u16 flags; |
| 307 | |
| 308 | if (!vring_use_dma_api(vq->vq.vdev)) |
| 309 | return; |
| 310 | |
| 311 | flags = virtio16_to_cpu(vq->vq.vdev, desc->flags); |
| 312 | |
| 313 | if (flags & VRING_DESC_F_INDIRECT) { |
| 314 | dma_unmap_single(vring_dma_dev(vq), |
| 315 | virtio64_to_cpu(vq->vq.vdev, desc->addr), |
| 316 | virtio32_to_cpu(vq->vq.vdev, desc->len), |
| 317 | (flags & VRING_DESC_F_WRITE) ? |
| 318 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 319 | } else { |
| 320 | dma_unmap_page(vring_dma_dev(vq), |
| 321 | virtio64_to_cpu(vq->vq.vdev, desc->addr), |
| 322 | virtio32_to_cpu(vq->vq.vdev, desc->len), |
| 323 | (flags & VRING_DESC_F_WRITE) ? |
| 324 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 325 | } |
| 326 | } |
| 327 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 328 | static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq, |
| 329 | unsigned int total_sg, |
| 330 | gfp_t gfp) |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 331 | { |
| 332 | struct vring_desc *desc; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 333 | unsigned int i; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 334 | |
Will Deacon | b92b1b8 | 2012-10-19 14:03:33 +0100 | [diff] [blame] | 335 | /* |
| 336 | * We require lowmem mappings for the descriptors because |
| 337 | * otherwise virt_to_phys will give us bogus addresses in the |
| 338 | * virtqueue. |
| 339 | */ |
Michal Hocko | 8210753 | 2015-12-01 15:32:49 +0100 | [diff] [blame] | 340 | gfp &= ~__GFP_HIGHMEM; |
Will Deacon | b92b1b8 | 2012-10-19 14:03:33 +0100 | [diff] [blame] | 341 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 342 | desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 343 | if (!desc) |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 344 | return NULL; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 345 | |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 346 | for (i = 0; i < total_sg; i++) |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 347 | desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 348 | return desc; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 351 | static inline int virtqueue_add_split(struct virtqueue *_vq, |
| 352 | struct scatterlist *sgs[], |
| 353 | unsigned int total_sg, |
| 354 | unsigned int out_sgs, |
| 355 | unsigned int in_sgs, |
| 356 | void *data, |
| 357 | void *ctx, |
| 358 | gfp_t gfp) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 359 | { |
| 360 | struct vring_virtqueue *vq = to_vvq(_vq); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 361 | struct scatterlist *sg; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 362 | struct vring_desc *desc; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 363 | unsigned int i, n, avail, descs_used, uninitialized_var(prev), err_idx; |
Michael S. Tsirkin | 1fe9b6f | 2010-07-26 16:55:30 +0930 | [diff] [blame] | 364 | int head; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 365 | bool indirect; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 366 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 367 | START_USE(vq); |
| 368 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 369 | BUG_ON(data == NULL); |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 370 | BUG_ON(ctx && vq->indirect); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 371 | |
Rusty Russell | 7067044 | 2014-03-13 11:23:40 +1030 | [diff] [blame] | 372 | if (unlikely(vq->broken)) { |
| 373 | END_USE(vq); |
| 374 | return -EIO; |
| 375 | } |
| 376 | |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 377 | LAST_ADD_TIME_UPDATE(vq); |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 378 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 379 | BUG_ON(total_sg == 0); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 380 | |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 381 | head = vq->free_head; |
| 382 | |
Tiwei Bie | 2f18c2d | 2018-11-21 18:03:23 +0800 | [diff] [blame] | 383 | if (virtqueue_use_indirect(_vq, total_sg)) |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 384 | desc = alloc_indirect_split(_vq, total_sg, gfp); |
Richard W.M. Jones | 44ed808 | 2017-08-10 17:56:51 +0100 | [diff] [blame] | 385 | else { |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 386 | desc = NULL; |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 387 | WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect); |
Richard W.M. Jones | 44ed808 | 2017-08-10 17:56:51 +0100 | [diff] [blame] | 388 | } |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 389 | |
| 390 | if (desc) { |
| 391 | /* Use a single buffer which doesn't continue */ |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 392 | indirect = true; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 393 | /* Set up rest to use this indirect table. */ |
| 394 | i = 0; |
| 395 | descs_used = 1; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 396 | } else { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 397 | indirect = false; |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 398 | desc = vq->split.vring.desc; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 399 | i = head; |
| 400 | descs_used = total_sg; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | if (vq->vq.num_free < descs_used) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 404 | pr_debug("Can't add buf len %i - avail = %i\n", |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 405 | descs_used, vq->vq.num_free); |
Rusty Russell | 44653ea | 2008-07-25 12:06:04 -0500 | [diff] [blame] | 406 | /* FIXME: for historical reasons, we force a notify here if |
| 407 | * there are outgoing parts to the buffer. Presumably the |
| 408 | * host should service the ring ASAP. */ |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 409 | if (out_sgs) |
Rusty Russell | 44653ea | 2008-07-25 12:06:04 -0500 | [diff] [blame] | 410 | vq->notify(&vq->vq); |
Wei Yongjun | 58625ed | 2016-08-02 14:16:31 +0000 | [diff] [blame] | 411 | if (indirect) |
| 412 | kfree(desc); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 413 | END_USE(vq); |
| 414 | return -ENOSPC; |
| 415 | } |
| 416 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 417 | for (n = 0; n < out_sgs; n++) { |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 418 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 419 | dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE); |
| 420 | if (vring_mapping_error(vq, addr)) |
| 421 | goto unmap_release; |
| 422 | |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 423 | desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 424 | desc[i].addr = cpu_to_virtio64(_vq->vdev, addr); |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 425 | desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 426 | prev = i; |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 427 | i = virtio16_to_cpu(_vq->vdev, desc[i].next); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 428 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 429 | } |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 430 | for (; n < (out_sgs + in_sgs); n++) { |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 431 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 432 | dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE); |
| 433 | if (vring_mapping_error(vq, addr)) |
| 434 | goto unmap_release; |
| 435 | |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 436 | desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 437 | desc[i].addr = cpu_to_virtio64(_vq->vdev, addr); |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 438 | desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 439 | prev = i; |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 440 | i = virtio16_to_cpu(_vq->vdev, desc[i].next); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 441 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 442 | } |
| 443 | /* Last one doesn't continue. */ |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 444 | desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 445 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 446 | if (indirect) { |
| 447 | /* Now that the indirect table is filled in, map it. */ |
| 448 | dma_addr_t addr = vring_map_single( |
| 449 | vq, desc, total_sg * sizeof(struct vring_desc), |
| 450 | DMA_TO_DEVICE); |
| 451 | if (vring_mapping_error(vq, addr)) |
| 452 | goto unmap_release; |
| 453 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 454 | vq->split.vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, |
| 455 | VRING_DESC_F_INDIRECT); |
| 456 | vq->split.vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, |
| 457 | addr); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 458 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 459 | vq->split.vring.desc[head].len = cpu_to_virtio32(_vq->vdev, |
| 460 | total_sg * sizeof(struct vring_desc)); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* We're using some buffers from the free list. */ |
| 464 | vq->vq.num_free -= descs_used; |
| 465 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 466 | /* Update free pointer */ |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 467 | if (indirect) |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 468 | vq->free_head = virtio16_to_cpu(_vq->vdev, |
| 469 | vq->split.vring.desc[head].next); |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 470 | else |
| 471 | vq->free_head = i; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 472 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 473 | /* Store token and indirect buffer state. */ |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 474 | vq->split.desc_state[head].data = data; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 475 | if (indirect) |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 476 | vq->split.desc_state[head].indir_desc = desc; |
Jason Wang | 87646a3 | 2017-07-19 16:54:45 +0800 | [diff] [blame] | 477 | else |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 478 | vq->split.desc_state[head].indir_desc = ctx; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 479 | |
| 480 | /* 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] | 481 | * do sync). */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 482 | avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1); |
| 483 | vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 484 | |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 485 | /* Descriptors and available array need to be set before we expose the |
| 486 | * new available array entries. */ |
Rusty Russell | a9a0fef | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 487 | virtio_wmb(vq->weak_barriers); |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 488 | vq->split.avail_idx_shadow++; |
| 489 | vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, |
| 490 | vq->split.avail_idx_shadow); |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 491 | vq->num_added++; |
| 492 | |
Tetsuo Handa | 5e05bf5 | 2015-02-11 15:01:13 +1030 | [diff] [blame] | 493 | pr_debug("Added buffer head %i to %p\n", head, vq); |
| 494 | END_USE(vq); |
| 495 | |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 496 | /* This is very unlikely, but theoretically possible. Kick |
| 497 | * just in case. */ |
| 498 | if (unlikely(vq->num_added == (1 << 16) - 1)) |
| 499 | virtqueue_kick(_vq); |
| 500 | |
Rusty Russell | 98e8c6b | 2012-10-16 23:56:15 +1030 | [diff] [blame] | 501 | return 0; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 502 | |
| 503 | unmap_release: |
| 504 | err_idx = i; |
| 505 | i = head; |
| 506 | |
| 507 | for (n = 0; n < total_sg; n++) { |
| 508 | if (i == err_idx) |
| 509 | break; |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 510 | vring_unmap_one_split(vq, &desc[i]); |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 511 | i = virtio16_to_cpu(_vq->vdev, vq->split.vring.desc[i].next); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 514 | if (indirect) |
| 515 | kfree(desc); |
| 516 | |
Michael S. Tsirkin | 3cc36f6 | 2016-08-03 07:18:51 +0300 | [diff] [blame] | 517 | END_USE(vq); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 518 | return -EIO; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 519 | } |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 520 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 521 | static bool virtqueue_kick_prepare_split(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 522 | { |
| 523 | struct vring_virtqueue *vq = to_vvq(_vq); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 524 | u16 new, old; |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 525 | bool needs_kick; |
| 526 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 527 | START_USE(vq); |
Jason Wang | a72caae | 2012-01-20 16:17:08 +0800 | [diff] [blame] | 528 | /* We need to expose available array entries before checking avail |
| 529 | * event. */ |
Rusty Russell | a9a0fef | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 530 | virtio_mb(vq->weak_barriers); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 531 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 532 | old = vq->split.avail_idx_shadow - vq->num_added; |
| 533 | new = vq->split.avail_idx_shadow; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 534 | vq->num_added = 0; |
| 535 | |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 536 | LAST_ADD_TIME_CHECK(vq); |
| 537 | LAST_ADD_TIME_INVALID(vq); |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 538 | |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 539 | if (vq->event) { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 540 | needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, |
| 541 | vring_avail_event(&vq->split.vring)), |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 542 | new, old); |
| 543 | } else { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 544 | needs_kick = !(vq->split.vring.used->flags & |
| 545 | cpu_to_virtio16(_vq->vdev, |
| 546 | VRING_USED_F_NO_NOTIFY)); |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 547 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 548 | END_USE(vq); |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 549 | return needs_kick; |
| 550 | } |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 551 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 552 | static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, |
| 553 | void **ctx) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 554 | { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 555 | unsigned int i, j; |
Gonglei | c60923c | 2016-11-22 13:51:50 +0800 | [diff] [blame] | 556 | __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 557 | |
| 558 | /* Clear data ptr. */ |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 559 | vq->split.desc_state[head].data = NULL; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 560 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 561 | /* Put back on free list: unmap first-level descriptors and find end */ |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 562 | i = head; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 563 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 564 | while (vq->split.vring.desc[i].flags & nextflag) { |
| 565 | vring_unmap_one_split(vq, &vq->split.vring.desc[i]); |
| 566 | i = virtio16_to_cpu(vq->vq.vdev, vq->split.vring.desc[i].next); |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 567 | vq->vq.num_free++; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 568 | } |
| 569 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 570 | vring_unmap_one_split(vq, &vq->split.vring.desc[i]); |
| 571 | vq->split.vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, |
| 572 | vq->free_head); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 573 | vq->free_head = head; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 574 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 575 | /* Plus final descriptor */ |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 576 | vq->vq.num_free++; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 577 | |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 578 | if (vq->indirect) { |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 579 | struct vring_desc *indir_desc = |
| 580 | vq->split.desc_state[head].indir_desc; |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 581 | u32 len; |
| 582 | |
| 583 | /* Free the indirect table, if any, now that it's unmapped. */ |
| 584 | if (!indir_desc) |
| 585 | return; |
| 586 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 587 | len = virtio32_to_cpu(vq->vq.vdev, |
| 588 | vq->split.vring.desc[head].len); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 589 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 590 | BUG_ON(!(vq->split.vring.desc[head].flags & |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 591 | cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT))); |
| 592 | BUG_ON(len == 0 || len % sizeof(struct vring_desc)); |
| 593 | |
| 594 | for (j = 0; j < len / sizeof(struct vring_desc); j++) |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 595 | vring_unmap_one_split(vq, &indir_desc[j]); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 596 | |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 597 | kfree(indir_desc); |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 598 | vq->split.desc_state[head].indir_desc = NULL; |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 599 | } else if (ctx) { |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 600 | *ctx = vq->split.desc_state[head].indir_desc; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 601 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 602 | } |
| 603 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 604 | static inline bool more_used_split(const struct vring_virtqueue *vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 605 | { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 606 | return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, |
| 607 | vq->split.vring.used->idx); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 608 | } |
| 609 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 610 | static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, |
| 611 | unsigned int *len, |
| 612 | void **ctx) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 613 | { |
| 614 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 615 | void *ret; |
| 616 | unsigned int i; |
Rusty Russell | 3b720b8 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 617 | u16 last_used; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 618 | |
| 619 | START_USE(vq); |
| 620 | |
Rusty Russell | 5ef8275 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 621 | if (unlikely(vq->broken)) { |
| 622 | END_USE(vq); |
| 623 | return NULL; |
| 624 | } |
| 625 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 626 | if (!more_used_split(vq)) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 627 | pr_debug("No more buffers in queue\n"); |
| 628 | END_USE(vq); |
| 629 | return NULL; |
| 630 | } |
| 631 | |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 632 | /* Only get used array entries after they have been exposed by host. */ |
Rusty Russell | a9a0fef | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 633 | virtio_rmb(vq->weak_barriers); |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 634 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 635 | last_used = (vq->last_used_idx & (vq->split.vring.num - 1)); |
| 636 | i = virtio32_to_cpu(_vq->vdev, |
| 637 | vq->split.vring.used->ring[last_used].id); |
| 638 | *len = virtio32_to_cpu(_vq->vdev, |
| 639 | vq->split.vring.used->ring[last_used].len); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 640 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 641 | if (unlikely(i >= vq->split.vring.num)) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 642 | BAD_RING(vq, "id %u out of range\n", i); |
| 643 | return NULL; |
| 644 | } |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 645 | if (unlikely(!vq->split.desc_state[i].data)) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 646 | BAD_RING(vq, "id %u is not a head!\n", i); |
| 647 | return NULL; |
| 648 | } |
| 649 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 650 | /* detach_buf_split clears data, so grab it now. */ |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 651 | ret = vq->split.desc_state[i].data; |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 652 | detach_buf_split(vq, i, ctx); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 653 | vq->last_used_idx++; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 654 | /* If we expect an interrupt for the next entry, tell host |
| 655 | * by writing event index and flush out the write before |
| 656 | * the read in the next get_buf call. */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 657 | if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) |
Michael S. Tsirkin | 788e5b3 | 2015-12-17 12:20:39 +0200 | [diff] [blame] | 658 | virtio_store_mb(vq->weak_barriers, |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 659 | &vring_used_event(&vq->split.vring), |
Michael S. Tsirkin | 788e5b3 | 2015-12-17 12:20:39 +0200 | [diff] [blame] | 660 | cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 661 | |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 662 | LAST_ADD_TIME_INVALID(vq); |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 663 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 664 | END_USE(vq); |
| 665 | return ret; |
| 666 | } |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 667 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 668 | static void virtqueue_disable_cb_split(struct virtqueue *_vq) |
| 669 | { |
| 670 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 671 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 672 | if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { |
| 673 | vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 674 | if (!vq->event) |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 675 | vq->split.vring.avail->flags = |
| 676 | cpu_to_virtio16(_vq->vdev, |
| 677 | vq->split.avail_flags_shadow); |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 681 | static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq) |
Michael S. Tsirkin | cc22988 | 2013-07-09 13:19:18 +0300 | [diff] [blame] | 682 | { |
| 683 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 684 | u16 last_used_idx; |
| 685 | |
| 686 | START_USE(vq); |
| 687 | |
| 688 | /* We optimistically turn back on interrupts, then check if there was |
| 689 | * more to do. */ |
| 690 | /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to |
| 691 | * either clear the flags bit or point the event index at the next |
| 692 | * entry. Always do both to keep code simple. */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 693 | if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { |
| 694 | vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; |
Ladi Prosek | 0ea1e4a | 2016-08-31 14:00:04 +0200 | [diff] [blame] | 695 | if (!vq->event) |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 696 | vq->split.vring.avail->flags = |
| 697 | cpu_to_virtio16(_vq->vdev, |
| 698 | vq->split.avail_flags_shadow); |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 699 | } |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 700 | vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev, |
| 701 | last_used_idx = vq->last_used_idx); |
Michael S. Tsirkin | cc22988 | 2013-07-09 13:19:18 +0300 | [diff] [blame] | 702 | END_USE(vq); |
| 703 | return last_used_idx; |
| 704 | } |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 705 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 706 | static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx) |
| 707 | { |
| 708 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 709 | |
| 710 | return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 711 | vq->split.vring.used->idx); |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 712 | } |
| 713 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 714 | static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq) |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 715 | { |
| 716 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 717 | u16 bufs; |
| 718 | |
| 719 | START_USE(vq); |
| 720 | |
| 721 | /* We optimistically turn back on interrupts, then check if there was |
| 722 | * more to do. */ |
| 723 | /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to |
| 724 | * either clear the flags bit or point the event index at the next |
Ladi Prosek | 0ea1e4a | 2016-08-31 14:00:04 +0200 | [diff] [blame] | 725 | * entry. Always update the event index to keep code simple. */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 726 | if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { |
| 727 | vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; |
Ladi Prosek | 0ea1e4a | 2016-08-31 14:00:04 +0200 | [diff] [blame] | 728 | if (!vq->event) |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 729 | vq->split.vring.avail->flags = |
| 730 | cpu_to_virtio16(_vq->vdev, |
| 731 | vq->split.avail_flags_shadow); |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 732 | } |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 733 | /* TODO: tune this threshold */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 734 | bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4; |
Michael S. Tsirkin | 788e5b3 | 2015-12-17 12:20:39 +0200 | [diff] [blame] | 735 | |
| 736 | virtio_store_mb(vq->weak_barriers, |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 737 | &vring_used_event(&vq->split.vring), |
Michael S. Tsirkin | 788e5b3 | 2015-12-17 12:20:39 +0200 | [diff] [blame] | 738 | cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); |
| 739 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 740 | if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx) |
| 741 | - vq->last_used_idx) > bufs)) { |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 742 | END_USE(vq); |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | END_USE(vq); |
| 747 | return true; |
| 748 | } |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 749 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 750 | static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq) |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 751 | { |
| 752 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 753 | unsigned int i; |
| 754 | void *buf; |
| 755 | |
| 756 | START_USE(vq); |
| 757 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 758 | for (i = 0; i < vq->split.vring.num; i++) { |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 759 | if (!vq->split.desc_state[i].data) |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 760 | continue; |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 761 | /* detach_buf_split clears data, so grab it now. */ |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 762 | buf = vq->split.desc_state[i].data; |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 763 | detach_buf_split(vq, i, NULL); |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 764 | vq->split.avail_idx_shadow--; |
| 765 | vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, |
| 766 | vq->split.avail_idx_shadow); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 767 | END_USE(vq); |
| 768 | return buf; |
| 769 | } |
| 770 | /* That should have freed everything. */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 771 | BUG_ON(vq->vq.num_free != vq->split.vring.num); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 772 | |
| 773 | END_USE(vq); |
| 774 | return NULL; |
| 775 | } |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 776 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame^] | 777 | static struct virtqueue *vring_create_virtqueue_split( |
| 778 | unsigned int index, |
| 779 | unsigned int num, |
| 780 | unsigned int vring_align, |
| 781 | struct virtio_device *vdev, |
| 782 | bool weak_barriers, |
| 783 | bool may_reduce_num, |
| 784 | bool context, |
| 785 | bool (*notify)(struct virtqueue *), |
| 786 | void (*callback)(struct virtqueue *), |
| 787 | const char *name) |
| 788 | { |
| 789 | struct virtqueue *vq; |
| 790 | void *queue = NULL; |
| 791 | dma_addr_t dma_addr; |
| 792 | size_t queue_size_in_bytes; |
| 793 | struct vring vring; |
| 794 | |
| 795 | /* We assume num is a power of 2. */ |
| 796 | if (num & (num - 1)) { |
| 797 | dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); |
| 798 | return NULL; |
| 799 | } |
| 800 | |
| 801 | /* TODO: allocate each queue chunk individually */ |
| 802 | for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { |
| 803 | queue = vring_alloc_queue(vdev, vring_size(num, vring_align), |
| 804 | &dma_addr, |
| 805 | GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); |
| 806 | if (queue) |
| 807 | break; |
| 808 | } |
| 809 | |
| 810 | if (!num) |
| 811 | return NULL; |
| 812 | |
| 813 | if (!queue) { |
| 814 | /* Try to get a single page. You are my only hope! */ |
| 815 | queue = vring_alloc_queue(vdev, vring_size(num, vring_align), |
| 816 | &dma_addr, GFP_KERNEL|__GFP_ZERO); |
| 817 | } |
| 818 | if (!queue) |
| 819 | return NULL; |
| 820 | |
| 821 | queue_size_in_bytes = vring_size(num, vring_align); |
| 822 | vring_init(&vring, num, queue, vring_align); |
| 823 | |
| 824 | vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, |
| 825 | notify, callback, name); |
| 826 | if (!vq) { |
| 827 | vring_free_queue(vdev, queue_size_in_bytes, queue, |
| 828 | dma_addr); |
| 829 | return NULL; |
| 830 | } |
| 831 | |
| 832 | to_vvq(vq)->split.queue_dma_addr = dma_addr; |
| 833 | to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes; |
| 834 | to_vvq(vq)->we_own_ring = true; |
| 835 | |
| 836 | return vq; |
| 837 | } |
| 838 | |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 839 | |
| 840 | /* |
| 841 | * Generic functions and exported symbols. |
| 842 | */ |
| 843 | |
| 844 | static inline int virtqueue_add(struct virtqueue *_vq, |
| 845 | struct scatterlist *sgs[], |
| 846 | unsigned int total_sg, |
| 847 | unsigned int out_sgs, |
| 848 | unsigned int in_sgs, |
| 849 | void *data, |
| 850 | void *ctx, |
| 851 | gfp_t gfp) |
| 852 | { |
| 853 | return virtqueue_add_split(_vq, sgs, total_sg, |
| 854 | out_sgs, in_sgs, data, ctx, gfp); |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * virtqueue_add_sgs - expose buffers to other end |
| 859 | * @vq: the struct virtqueue we're talking about. |
| 860 | * @sgs: array of terminated scatterlists. |
| 861 | * @out_num: the number of scatterlists readable by other side |
| 862 | * @in_num: the number of scatterlists which are writable (after readable ones) |
| 863 | * @data: the token identifying the buffer. |
| 864 | * @gfp: how to do memory allocations (if necessary). |
| 865 | * |
| 866 | * Caller must ensure we don't call this with other virtqueue operations |
| 867 | * at the same time (except where noted). |
| 868 | * |
| 869 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
| 870 | */ |
| 871 | int virtqueue_add_sgs(struct virtqueue *_vq, |
| 872 | struct scatterlist *sgs[], |
| 873 | unsigned int out_sgs, |
| 874 | unsigned int in_sgs, |
| 875 | void *data, |
| 876 | gfp_t gfp) |
| 877 | { |
| 878 | unsigned int i, total_sg = 0; |
| 879 | |
| 880 | /* Count them first. */ |
| 881 | for (i = 0; i < out_sgs + in_sgs; i++) { |
| 882 | struct scatterlist *sg; |
| 883 | |
| 884 | for (sg = sgs[i]; sg; sg = sg_next(sg)) |
| 885 | total_sg++; |
| 886 | } |
| 887 | return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, |
| 888 | data, NULL, gfp); |
| 889 | } |
| 890 | EXPORT_SYMBOL_GPL(virtqueue_add_sgs); |
| 891 | |
| 892 | /** |
| 893 | * virtqueue_add_outbuf - expose output buffers to other end |
| 894 | * @vq: the struct virtqueue we're talking about. |
| 895 | * @sg: scatterlist (must be well-formed and terminated!) |
| 896 | * @num: the number of entries in @sg readable by other side |
| 897 | * @data: the token identifying the buffer. |
| 898 | * @gfp: how to do memory allocations (if necessary). |
| 899 | * |
| 900 | * Caller must ensure we don't call this with other virtqueue operations |
| 901 | * at the same time (except where noted). |
| 902 | * |
| 903 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
| 904 | */ |
| 905 | int virtqueue_add_outbuf(struct virtqueue *vq, |
| 906 | struct scatterlist *sg, unsigned int num, |
| 907 | void *data, |
| 908 | gfp_t gfp) |
| 909 | { |
| 910 | return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp); |
| 911 | } |
| 912 | EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); |
| 913 | |
| 914 | /** |
| 915 | * virtqueue_add_inbuf - expose input buffers to other end |
| 916 | * @vq: the struct virtqueue we're talking about. |
| 917 | * @sg: scatterlist (must be well-formed and terminated!) |
| 918 | * @num: the number of entries in @sg writable by other side |
| 919 | * @data: the token identifying the buffer. |
| 920 | * @gfp: how to do memory allocations (if necessary). |
| 921 | * |
| 922 | * Caller must ensure we don't call this with other virtqueue operations |
| 923 | * at the same time (except where noted). |
| 924 | * |
| 925 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
| 926 | */ |
| 927 | int virtqueue_add_inbuf(struct virtqueue *vq, |
| 928 | struct scatterlist *sg, unsigned int num, |
| 929 | void *data, |
| 930 | gfp_t gfp) |
| 931 | { |
| 932 | return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp); |
| 933 | } |
| 934 | EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); |
| 935 | |
| 936 | /** |
| 937 | * virtqueue_add_inbuf_ctx - expose input buffers to other end |
| 938 | * @vq: the struct virtqueue we're talking about. |
| 939 | * @sg: scatterlist (must be well-formed and terminated!) |
| 940 | * @num: the number of entries in @sg writable by other side |
| 941 | * @data: the token identifying the buffer. |
| 942 | * @ctx: extra context for the token |
| 943 | * @gfp: how to do memory allocations (if necessary). |
| 944 | * |
| 945 | * Caller must ensure we don't call this with other virtqueue operations |
| 946 | * at the same time (except where noted). |
| 947 | * |
| 948 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
| 949 | */ |
| 950 | int virtqueue_add_inbuf_ctx(struct virtqueue *vq, |
| 951 | struct scatterlist *sg, unsigned int num, |
| 952 | void *data, |
| 953 | void *ctx, |
| 954 | gfp_t gfp) |
| 955 | { |
| 956 | return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp); |
| 957 | } |
| 958 | EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx); |
| 959 | |
| 960 | /** |
| 961 | * virtqueue_kick_prepare - first half of split virtqueue_kick call. |
| 962 | * @vq: the struct virtqueue |
| 963 | * |
| 964 | * Instead of virtqueue_kick(), you can do: |
| 965 | * if (virtqueue_kick_prepare(vq)) |
| 966 | * virtqueue_notify(vq); |
| 967 | * |
| 968 | * This is sometimes useful because the virtqueue_kick_prepare() needs |
| 969 | * to be serialized, but the actual virtqueue_notify() call does not. |
| 970 | */ |
| 971 | bool virtqueue_kick_prepare(struct virtqueue *_vq) |
| 972 | { |
| 973 | return virtqueue_kick_prepare_split(_vq); |
| 974 | } |
| 975 | EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); |
| 976 | |
| 977 | /** |
| 978 | * virtqueue_notify - second half of split virtqueue_kick call. |
| 979 | * @vq: the struct virtqueue |
| 980 | * |
| 981 | * This does not need to be serialized. |
| 982 | * |
| 983 | * Returns false if host notify failed or queue is broken, otherwise true. |
| 984 | */ |
| 985 | bool virtqueue_notify(struct virtqueue *_vq) |
| 986 | { |
| 987 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 988 | |
| 989 | if (unlikely(vq->broken)) |
| 990 | return false; |
| 991 | |
| 992 | /* Prod other side to tell it about changes. */ |
| 993 | if (!vq->notify(_vq)) { |
| 994 | vq->broken = true; |
| 995 | return false; |
| 996 | } |
| 997 | return true; |
| 998 | } |
| 999 | EXPORT_SYMBOL_GPL(virtqueue_notify); |
| 1000 | |
| 1001 | /** |
| 1002 | * virtqueue_kick - update after add_buf |
| 1003 | * @vq: the struct virtqueue |
| 1004 | * |
| 1005 | * After one or more virtqueue_add_* calls, invoke this to kick |
| 1006 | * the other side. |
| 1007 | * |
| 1008 | * Caller must ensure we don't call this with other virtqueue |
| 1009 | * operations at the same time (except where noted). |
| 1010 | * |
| 1011 | * Returns false if kick failed, otherwise true. |
| 1012 | */ |
| 1013 | bool virtqueue_kick(struct virtqueue *vq) |
| 1014 | { |
| 1015 | if (virtqueue_kick_prepare(vq)) |
| 1016 | return virtqueue_notify(vq); |
| 1017 | return true; |
| 1018 | } |
| 1019 | EXPORT_SYMBOL_GPL(virtqueue_kick); |
| 1020 | |
| 1021 | /** |
| 1022 | * virtqueue_get_buf - get the next used buffer |
| 1023 | * @vq: the struct virtqueue we're talking about. |
| 1024 | * @len: the length written into the buffer |
| 1025 | * |
| 1026 | * If the device wrote data into the buffer, @len will be set to the |
| 1027 | * amount written. This means you don't need to clear the buffer |
| 1028 | * beforehand to ensure there's no data leakage in the case of short |
| 1029 | * writes. |
| 1030 | * |
| 1031 | * Caller must ensure we don't call this with other virtqueue |
| 1032 | * operations at the same time (except where noted). |
| 1033 | * |
| 1034 | * Returns NULL if there are no used buffers, or the "data" token |
| 1035 | * handed to virtqueue_add_*(). |
| 1036 | */ |
| 1037 | void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len, |
| 1038 | void **ctx) |
| 1039 | { |
| 1040 | return virtqueue_get_buf_ctx_split(_vq, len, ctx); |
| 1041 | } |
| 1042 | EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx); |
| 1043 | |
| 1044 | void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) |
| 1045 | { |
| 1046 | return virtqueue_get_buf_ctx(_vq, len, NULL); |
| 1047 | } |
| 1048 | EXPORT_SYMBOL_GPL(virtqueue_get_buf); |
| 1049 | |
| 1050 | /** |
| 1051 | * virtqueue_disable_cb - disable callbacks |
| 1052 | * @vq: the struct virtqueue we're talking about. |
| 1053 | * |
| 1054 | * Note that this is not necessarily synchronous, hence unreliable and only |
| 1055 | * useful as an optimization. |
| 1056 | * |
| 1057 | * Unlike other operations, this need not be serialized. |
| 1058 | */ |
| 1059 | void virtqueue_disable_cb(struct virtqueue *_vq) |
| 1060 | { |
| 1061 | virtqueue_disable_cb_split(_vq); |
| 1062 | } |
| 1063 | EXPORT_SYMBOL_GPL(virtqueue_disable_cb); |
| 1064 | |
| 1065 | /** |
| 1066 | * virtqueue_enable_cb_prepare - restart callbacks after disable_cb |
| 1067 | * @vq: the struct virtqueue we're talking about. |
| 1068 | * |
| 1069 | * This re-enables callbacks; it returns current queue state |
| 1070 | * in an opaque unsigned value. This value should be later tested by |
| 1071 | * virtqueue_poll, to detect a possible race between the driver checking for |
| 1072 | * more work, and enabling callbacks. |
| 1073 | * |
| 1074 | * Caller must ensure we don't call this with other virtqueue |
| 1075 | * operations at the same time (except where noted). |
| 1076 | */ |
| 1077 | unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) |
| 1078 | { |
| 1079 | return virtqueue_enable_cb_prepare_split(_vq); |
| 1080 | } |
| 1081 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); |
| 1082 | |
| 1083 | /** |
| 1084 | * virtqueue_poll - query pending used buffers |
| 1085 | * @vq: the struct virtqueue we're talking about. |
| 1086 | * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). |
| 1087 | * |
| 1088 | * Returns "true" if there are pending used buffers in the queue. |
| 1089 | * |
| 1090 | * This does not need to be serialized. |
| 1091 | */ |
| 1092 | bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) |
| 1093 | { |
| 1094 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1095 | |
| 1096 | virtio_mb(vq->weak_barriers); |
| 1097 | return virtqueue_poll_split(_vq, last_used_idx); |
| 1098 | } |
| 1099 | EXPORT_SYMBOL_GPL(virtqueue_poll); |
| 1100 | |
| 1101 | /** |
| 1102 | * virtqueue_enable_cb - restart callbacks after disable_cb. |
| 1103 | * @vq: the struct virtqueue we're talking about. |
| 1104 | * |
| 1105 | * This re-enables callbacks; it returns "false" if there are pending |
| 1106 | * buffers in the queue, to detect a possible race between the driver |
| 1107 | * checking for more work, and enabling callbacks. |
| 1108 | * |
| 1109 | * Caller must ensure we don't call this with other virtqueue |
| 1110 | * operations at the same time (except where noted). |
| 1111 | */ |
| 1112 | bool virtqueue_enable_cb(struct virtqueue *_vq) |
| 1113 | { |
| 1114 | unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); |
| 1115 | |
| 1116 | return !virtqueue_poll(_vq, last_used_idx); |
| 1117 | } |
| 1118 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb); |
| 1119 | |
| 1120 | /** |
| 1121 | * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. |
| 1122 | * @vq: the struct virtqueue we're talking about. |
| 1123 | * |
| 1124 | * This re-enables callbacks but hints to the other side to delay |
| 1125 | * interrupts until most of the available buffers have been processed; |
| 1126 | * it returns "false" if there are many pending buffers in the queue, |
| 1127 | * to detect a possible race between the driver checking for more work, |
| 1128 | * and enabling callbacks. |
| 1129 | * |
| 1130 | * Caller must ensure we don't call this with other virtqueue |
| 1131 | * operations at the same time (except where noted). |
| 1132 | */ |
| 1133 | bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) |
| 1134 | { |
| 1135 | return virtqueue_enable_cb_delayed_split(_vq); |
| 1136 | } |
| 1137 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); |
| 1138 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 1139 | /** |
| 1140 | * virtqueue_detach_unused_buf - detach first unused buffer |
| 1141 | * @vq: the struct virtqueue we're talking about. |
| 1142 | * |
| 1143 | * Returns NULL or the "data" token handed to virtqueue_add_*(). |
| 1144 | * This is not valid on an active queue; it is useful only for device |
| 1145 | * shutdown. |
| 1146 | */ |
| 1147 | void *virtqueue_detach_unused_buf(struct virtqueue *_vq) |
| 1148 | { |
| 1149 | return virtqueue_detach_unused_buf_split(_vq); |
| 1150 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 1151 | EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 1152 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 1153 | static inline bool more_used(const struct vring_virtqueue *vq) |
| 1154 | { |
| 1155 | return more_used_split(vq); |
| 1156 | } |
| 1157 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1158 | irqreturn_t vring_interrupt(int irq, void *_vq) |
| 1159 | { |
| 1160 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1161 | |
| 1162 | if (!more_used(vq)) { |
| 1163 | pr_debug("virtqueue interrupt with no work for %p\n", vq); |
| 1164 | return IRQ_NONE; |
| 1165 | } |
| 1166 | |
| 1167 | if (unlikely(vq->broken)) |
| 1168 | return IRQ_HANDLED; |
| 1169 | |
| 1170 | pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 1171 | if (vq->vq.callback) |
| 1172 | vq->vq.callback(&vq->vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1173 | |
| 1174 | return IRQ_HANDLED; |
| 1175 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 1176 | EXPORT_SYMBOL_GPL(vring_interrupt); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1177 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1178 | struct virtqueue *__vring_new_virtqueue(unsigned int index, |
| 1179 | struct vring vring, |
| 1180 | struct virtio_device *vdev, |
| 1181 | bool weak_barriers, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 1182 | bool context, |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1183 | bool (*notify)(struct virtqueue *), |
| 1184 | void (*callback)(struct virtqueue *), |
| 1185 | const char *name) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1186 | { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1187 | unsigned int i; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1188 | struct vring_virtqueue *vq; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1189 | |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 1190 | vq = kmalloc(sizeof(*vq), GFP_KERNEL); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1191 | if (!vq) |
| 1192 | return NULL; |
| 1193 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1194 | vq->vq.callback = callback; |
| 1195 | vq->vq.vdev = vdev; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 1196 | vq->vq.name = name; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1197 | vq->vq.num_free = vring.num; |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 1198 | vq->vq.index = index; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1199 | vq->we_own_ring = false; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1200 | vq->notify = notify; |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 1201 | vq->weak_barriers = weak_barriers; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1202 | vq->broken = false; |
| 1203 | vq->last_used_idx = 0; |
| 1204 | vq->num_added = 0; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 1205 | list_add_tail(&vq->vq.list, &vdev->vqs); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1206 | #ifdef DEBUG |
| 1207 | vq->in_use = false; |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 1208 | vq->last_add_time_valid = false; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1209 | #endif |
| 1210 | |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 1211 | vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && |
| 1212 | !context; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 1213 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 1214 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame^] | 1215 | vq->split.queue_dma_addr = 0; |
| 1216 | vq->split.queue_size_in_bytes = 0; |
| 1217 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 1218 | vq->split.vring = vring; |
| 1219 | vq->split.avail_flags_shadow = 0; |
| 1220 | vq->split.avail_idx_shadow = 0; |
| 1221 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1222 | /* No callback? Tell other side not to bother us. */ |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 1223 | if (!callback) { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 1224 | vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; |
Ladi Prosek | 0ea1e4a | 2016-08-31 14:00:04 +0200 | [diff] [blame] | 1225 | if (!vq->event) |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 1226 | vq->split.vring.avail->flags = cpu_to_virtio16(vdev, |
| 1227 | vq->split.avail_flags_shadow); |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 1228 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1229 | |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 1230 | vq->split.desc_state = kmalloc_array(vring.num, |
| 1231 | sizeof(struct vring_desc_state_split), GFP_KERNEL); |
| 1232 | if (!vq->split.desc_state) { |
| 1233 | kfree(vq); |
| 1234 | return NULL; |
| 1235 | } |
| 1236 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1237 | /* Put everything in free lists. */ |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1238 | vq->free_head = 0; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1239 | for (i = 0; i < vring.num-1; i++) |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 1240 | vq->split.vring.desc[i].next = cpu_to_virtio16(vdev, i + 1); |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 1241 | memset(vq->split.desc_state, 0, vring.num * |
| 1242 | sizeof(struct vring_desc_state_split)); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1243 | |
| 1244 | return &vq->vq; |
| 1245 | } |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1246 | EXPORT_SYMBOL_GPL(__vring_new_virtqueue); |
| 1247 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1248 | struct virtqueue *vring_create_virtqueue( |
| 1249 | unsigned int index, |
| 1250 | unsigned int num, |
| 1251 | unsigned int vring_align, |
| 1252 | struct virtio_device *vdev, |
| 1253 | bool weak_barriers, |
| 1254 | bool may_reduce_num, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 1255 | bool context, |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1256 | bool (*notify)(struct virtqueue *), |
| 1257 | void (*callback)(struct virtqueue *), |
| 1258 | const char *name) |
| 1259 | { |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame^] | 1260 | return vring_create_virtqueue_split(index, num, vring_align, |
| 1261 | vdev, weak_barriers, may_reduce_num, |
| 1262 | context, notify, callback, name); |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1263 | } |
| 1264 | EXPORT_SYMBOL_GPL(vring_create_virtqueue); |
| 1265 | |
| 1266 | struct virtqueue *vring_new_virtqueue(unsigned int index, |
| 1267 | unsigned int num, |
| 1268 | unsigned int vring_align, |
| 1269 | struct virtio_device *vdev, |
| 1270 | bool weak_barriers, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 1271 | bool context, |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1272 | void *pages, |
| 1273 | bool (*notify)(struct virtqueue *vq), |
| 1274 | void (*callback)(struct virtqueue *vq), |
| 1275 | const char *name) |
| 1276 | { |
| 1277 | struct vring vring; |
| 1278 | vring_init(&vring, num, pages, vring_align); |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 1279 | return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1280 | notify, callback, name); |
| 1281 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 1282 | EXPORT_SYMBOL_GPL(vring_new_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1283 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1284 | void vring_del_virtqueue(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1285 | { |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1286 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1287 | |
| 1288 | if (vq->we_own_ring) { |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame^] | 1289 | vring_free_queue(vq->vq.vdev, |
| 1290 | vq->split.queue_size_in_bytes, |
| 1291 | vq->split.vring.desc, |
| 1292 | vq->split.queue_dma_addr); |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 1293 | kfree(vq->split.desc_state); |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1294 | } |
| 1295 | list_del(&_vq->list); |
| 1296 | kfree(vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1297 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 1298 | EXPORT_SYMBOL_GPL(vring_del_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 1299 | |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 1300 | /* Manipulates transport-specific feature bits. */ |
| 1301 | void vring_transport_features(struct virtio_device *vdev) |
| 1302 | { |
| 1303 | unsigned int i; |
| 1304 | |
| 1305 | for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { |
| 1306 | switch (i) { |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 1307 | case VIRTIO_RING_F_INDIRECT_DESC: |
| 1308 | break; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 1309 | case VIRTIO_RING_F_EVENT_IDX: |
| 1310 | break; |
Michael S. Tsirkin | 747ae34 | 2014-12-01 15:52:40 +0200 | [diff] [blame] | 1311 | case VIRTIO_F_VERSION_1: |
| 1312 | break; |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 1313 | case VIRTIO_F_IOMMU_PLATFORM: |
| 1314 | break; |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 1315 | default: |
| 1316 | /* We don't understand this bit. */ |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 1317 | __virtio_clear_bit(vdev, i); |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | EXPORT_SYMBOL_GPL(vring_transport_features); |
| 1322 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 1323 | /** |
| 1324 | * virtqueue_get_vring_size - return the size of the virtqueue's vring |
| 1325 | * @vq: the struct virtqueue containing the vring of interest. |
| 1326 | * |
| 1327 | * Returns the size of the vring. This is mainly used for boasting to |
| 1328 | * userspace. Unlike other operations, this need not be serialized. |
| 1329 | */ |
Rick Jones | 8f9f466 | 2011-10-19 08:10:59 +0000 | [diff] [blame] | 1330 | unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) |
| 1331 | { |
| 1332 | |
| 1333 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1334 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 1335 | return vq->split.vring.num; |
Rick Jones | 8f9f466 | 2011-10-19 08:10:59 +0000 | [diff] [blame] | 1336 | } |
| 1337 | EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); |
| 1338 | |
Heinz Graalfs | b3b32c9 | 2013-10-29 09:40:19 +1030 | [diff] [blame] | 1339 | bool virtqueue_is_broken(struct virtqueue *_vq) |
| 1340 | { |
| 1341 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1342 | |
| 1343 | return vq->broken; |
| 1344 | } |
| 1345 | EXPORT_SYMBOL_GPL(virtqueue_is_broken); |
| 1346 | |
Rusty Russell | e2dcdfe | 2014-04-28 11:15:08 +0930 | [diff] [blame] | 1347 | /* |
| 1348 | * This should prevent the device from being used, allowing drivers to |
| 1349 | * recover. You may need to grab appropriate locks to flush. |
| 1350 | */ |
| 1351 | void virtio_break_device(struct virtio_device *dev) |
| 1352 | { |
| 1353 | struct virtqueue *_vq; |
| 1354 | |
| 1355 | list_for_each_entry(_vq, &dev->vqs, list) { |
| 1356 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1357 | vq->broken = true; |
| 1358 | } |
| 1359 | } |
| 1360 | EXPORT_SYMBOL_GPL(virtio_break_device); |
| 1361 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1362 | dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq) |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 1363 | { |
| 1364 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1365 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1366 | BUG_ON(!vq->we_own_ring); |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 1367 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame^] | 1368 | return vq->split.queue_dma_addr; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1369 | } |
| 1370 | EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr); |
| 1371 | |
| 1372 | dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq) |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 1373 | { |
| 1374 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1375 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1376 | BUG_ON(!vq->we_own_ring); |
| 1377 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame^] | 1378 | return vq->split.queue_dma_addr + |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 1379 | ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc); |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 1380 | } |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1381 | EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr); |
| 1382 | |
| 1383 | dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq) |
| 1384 | { |
| 1385 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1386 | |
| 1387 | BUG_ON(!vq->we_own_ring); |
| 1388 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame^] | 1389 | return vq->split.queue_dma_addr + |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 1390 | ((char *)vq->split.vring.used - (char *)vq->split.vring.desc); |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1391 | } |
| 1392 | EXPORT_SYMBOL_GPL(virtqueue_get_used_addr); |
| 1393 | |
| 1394 | const struct vring *virtqueue_get_vring(struct virtqueue *vq) |
| 1395 | { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 1396 | return &to_vvq(vq)->split.vring; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 1397 | } |
| 1398 | EXPORT_SYMBOL_GPL(virtqueue_get_vring); |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 1399 | |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 1400 | MODULE_LICENSE("GPL"); |