Thomas Gleixner | fd534e9 | 2019-05-23 11:14:39 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2 | /* Virtio ring implementation. |
| 3 | * |
| 4 | * Copyright 2007 Rusty Russell IBM Corporation |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/virtio.h> |
| 7 | #include <linux/virtio_ring.h> |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 8 | #include <linux/virtio_config.h> |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 9 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Paul Gortmaker | b5a2c4f | 2011-07-03 16:20:30 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 12 | #include <linux/hrtimer.h> |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 13 | #include <linux/dma-mapping.h> |
Andy Lutomirski | 78fe398 | 2016-02-02 21:46:40 -0800 | [diff] [blame] | 14 | #include <xen/xen.h> |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 15 | |
| 16 | #ifdef DEBUG |
| 17 | /* For development, we want to crash whenever the ring is screwed. */ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 18 | #define BAD_RING(_vq, fmt, args...) \ |
| 19 | do { \ |
| 20 | dev_err(&(_vq)->vq.vdev->dev, \ |
| 21 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 22 | BUG(); \ |
| 23 | } while (0) |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 24 | /* Caller is supposed to guarantee no reentry. */ |
| 25 | #define START_USE(_vq) \ |
| 26 | do { \ |
| 27 | if ((_vq)->in_use) \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 28 | panic("%s:in_use = %i\n", \ |
| 29 | (_vq)->vq.name, (_vq)->in_use); \ |
Rusty Russell | c5f841f | 2009-03-30 21:55:22 -0600 | [diff] [blame] | 30 | (_vq)->in_use = __LINE__; \ |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 31 | } while (0) |
Roel Kluin | 3a35ce7 | 2009-01-22 16:42:57 +0100 | [diff] [blame] | 32 | #define END_USE(_vq) \ |
Rusty Russell | 97a545a | 2010-02-24 14:22:22 -0600 | [diff] [blame] | 33 | do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 34 | #define LAST_ADD_TIME_UPDATE(_vq) \ |
| 35 | do { \ |
| 36 | ktime_t now = ktime_get(); \ |
| 37 | \ |
| 38 | /* No kick or get, with .1 second between? Warn. */ \ |
| 39 | if ((_vq)->last_add_time_valid) \ |
| 40 | WARN_ON(ktime_to_ms(ktime_sub(now, \ |
| 41 | (_vq)->last_add_time)) > 100); \ |
| 42 | (_vq)->last_add_time = now; \ |
| 43 | (_vq)->last_add_time_valid = true; \ |
| 44 | } while (0) |
| 45 | #define LAST_ADD_TIME_CHECK(_vq) \ |
| 46 | do { \ |
| 47 | if ((_vq)->last_add_time_valid) { \ |
| 48 | WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \ |
| 49 | (_vq)->last_add_time)) > 100); \ |
| 50 | } \ |
| 51 | } while (0) |
| 52 | #define LAST_ADD_TIME_INVALID(_vq) \ |
| 53 | ((_vq)->last_add_time_valid = false) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 54 | #else |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 55 | #define BAD_RING(_vq, fmt, args...) \ |
| 56 | do { \ |
| 57 | dev_err(&_vq->vq.vdev->dev, \ |
| 58 | "%s:"fmt, (_vq)->vq.name, ##args); \ |
| 59 | (_vq)->broken = true; \ |
| 60 | } while (0) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 61 | #define START_USE(vq) |
| 62 | #define END_USE(vq) |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 63 | #define LAST_ADD_TIME_UPDATE(vq) |
| 64 | #define LAST_ADD_TIME_CHECK(vq) |
| 65 | #define LAST_ADD_TIME_INVALID(vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 66 | #endif |
| 67 | |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 68 | struct vring_desc_state_split { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 69 | void *data; /* Data for callback. */ |
| 70 | struct vring_desc *indir_desc; /* Indirect descriptor, if any. */ |
| 71 | }; |
| 72 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 73 | struct vring_desc_state_packed { |
| 74 | void *data; /* Data for callback. */ |
| 75 | struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */ |
| 76 | u16 num; /* Descriptor list length. */ |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 77 | u16 last; /* The last desc state in a list. */ |
| 78 | }; |
| 79 | |
Jason Wang | 1f28750 | 2021-06-04 13:53:45 +0800 | [diff] [blame] | 80 | struct vring_desc_extra { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 81 | dma_addr_t addr; /* Buffer DMA addr. */ |
| 82 | u32 len; /* Buffer length. */ |
| 83 | u16 flags; /* Descriptor flags. */ |
Jason Wang | aeef9b4 | 2021-06-04 13:53:44 +0800 | [diff] [blame] | 84 | u16 next; /* The next desc state in a list. */ |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 85 | }; |
| 86 | |
Michael S. Tsirkin | 43b4f72 | 2015-01-15 13:33:31 +0200 | [diff] [blame] | 87 | struct vring_virtqueue { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 88 | struct virtqueue vq; |
| 89 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 90 | /* Is this a packed ring? */ |
| 91 | bool packed_ring; |
| 92 | |
Tiwei Bie | fb3fba6 | 2018-11-21 18:03:26 +0800 | [diff] [blame] | 93 | /* Is DMA API used? */ |
| 94 | bool use_dma_api; |
| 95 | |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 96 | /* Can we use weak barriers? */ |
| 97 | bool weak_barriers; |
| 98 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 99 | /* Other side has made a mess, don't try any more. */ |
| 100 | bool broken; |
| 101 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 102 | /* Host supports indirect buffers */ |
| 103 | bool indirect; |
| 104 | |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 105 | /* Host publishes avail event idx */ |
| 106 | bool event; |
| 107 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 108 | /* Head of free buffer list. */ |
| 109 | unsigned int free_head; |
| 110 | /* Number we've added since last sync. */ |
| 111 | unsigned int num_added; |
| 112 | |
| 113 | /* Last used index we've seen. */ |
Anthony Liguori | 1bc4953 | 2007-11-07 15:49:24 -0600 | [diff] [blame] | 114 | u16 last_used_idx; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 115 | |
Michael S. Tsirkin | 8d622d2 | 2021-04-13 01:19:16 -0400 | [diff] [blame] | 116 | /* Hint for event idx: already triggered no need to disable. */ |
| 117 | bool event_triggered; |
| 118 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 119 | union { |
| 120 | /* Available for split ring */ |
| 121 | struct { |
| 122 | /* Actual memory layout for this queue. */ |
| 123 | struct vring vring; |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 124 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 125 | /* Last written value to avail->flags */ |
| 126 | u16 avail_flags_shadow; |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 127 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 128 | /* |
| 129 | * Last written value to avail->idx in |
| 130 | * guest byte order. |
| 131 | */ |
| 132 | u16 avail_idx_shadow; |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 133 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 134 | /* Per-descriptor state. */ |
| 135 | struct vring_desc_state_split *desc_state; |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 136 | struct vring_desc_extra *desc_extra; |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame] | 137 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 138 | /* DMA address and size information */ |
| 139 | dma_addr_t queue_dma_addr; |
| 140 | size_t queue_size_in_bytes; |
| 141 | } split; |
| 142 | |
| 143 | /* Available for packed ring */ |
| 144 | struct { |
| 145 | /* Actual memory layout for this queue. */ |
Michael S. Tsirkin | 9c0644e | 2019-02-01 17:13:57 -0500 | [diff] [blame] | 146 | struct { |
| 147 | unsigned int num; |
| 148 | struct vring_packed_desc *desc; |
| 149 | struct vring_packed_desc_event *driver; |
| 150 | struct vring_packed_desc_event *device; |
| 151 | } vring; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 152 | |
| 153 | /* Driver ring wrap counter. */ |
| 154 | bool avail_wrap_counter; |
| 155 | |
| 156 | /* Device ring wrap counter. */ |
| 157 | bool used_wrap_counter; |
| 158 | |
| 159 | /* Avail used flags. */ |
| 160 | u16 avail_used_flags; |
| 161 | |
| 162 | /* Index of the next avail descriptor. */ |
| 163 | u16 next_avail_idx; |
| 164 | |
| 165 | /* |
| 166 | * Last written value to driver->flags in |
| 167 | * guest byte order. |
| 168 | */ |
| 169 | u16 event_flags_shadow; |
| 170 | |
| 171 | /* Per-descriptor state. */ |
| 172 | struct vring_desc_state_packed *desc_state; |
Jason Wang | 1f28750 | 2021-06-04 13:53:45 +0800 | [diff] [blame] | 173 | struct vring_desc_extra *desc_extra; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 174 | |
| 175 | /* DMA address and size information */ |
| 176 | dma_addr_t ring_dma_addr; |
| 177 | dma_addr_t driver_event_dma_addr; |
| 178 | dma_addr_t device_event_dma_addr; |
| 179 | size_t ring_size_in_bytes; |
| 180 | size_t event_size_in_bytes; |
| 181 | } packed; |
| 182 | }; |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 183 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 184 | /* How to notify other side. FIXME: commonalize hcalls! */ |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 185 | bool (*notify)(struct virtqueue *vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 186 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 187 | /* DMA, allocation, and size information */ |
| 188 | bool we_own_ring; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 189 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 190 | #ifdef DEBUG |
| 191 | /* They're supposed to lock for us. */ |
| 192 | unsigned int in_use; |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 193 | |
| 194 | /* Figure out if their kicks are too delayed. */ |
| 195 | bool last_add_time_valid; |
| 196 | ktime_t last_add_time; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 197 | #endif |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 198 | }; |
| 199 | |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * Helpers. |
| 203 | */ |
| 204 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 205 | #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) |
| 206 | |
Tiwei Bie | 2f18c2d | 2018-11-21 18:03:23 +0800 | [diff] [blame] | 207 | static inline bool virtqueue_use_indirect(struct virtqueue *_vq, |
| 208 | unsigned int total_sg) |
| 209 | { |
| 210 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 211 | |
| 212 | /* |
| 213 | * If the host supports indirect descriptor tables, and we have multiple |
| 214 | * buffers, then go indirect. FIXME: tune this threshold |
| 215 | */ |
| 216 | return (vq->indirect && total_sg > 1 && vq->vq.num_free); |
| 217 | } |
| 218 | |
Andy Lutomirski | d26c96c | 2016-02-02 21:46:35 -0800 | [diff] [blame] | 219 | /* |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 220 | * Modern virtio devices have feature bits to specify whether they need a |
| 221 | * quirk and bypass the IOMMU. If not there, just use the DMA API. |
| 222 | * |
| 223 | * If there, the interaction between virtio and DMA API is messy. |
Andy Lutomirski | d26c96c | 2016-02-02 21:46:35 -0800 | [diff] [blame] | 224 | * |
| 225 | * On most systems with virtio, physical addresses match bus addresses, |
| 226 | * and it doesn't particularly matter whether we use the DMA API. |
| 227 | * |
| 228 | * On some systems, including Xen and any system with a physical device |
| 229 | * that speaks virtio behind a physical IOMMU, we must use the DMA API |
| 230 | * for virtio DMA to work at all. |
| 231 | * |
| 232 | * On other systems, including SPARC and PPC64, virtio-pci devices are |
| 233 | * enumerated as though they are behind an IOMMU, but the virtio host |
| 234 | * ignores the IOMMU, so we must either pretend that the IOMMU isn't |
| 235 | * there or somehow map everything as the identity. |
| 236 | * |
| 237 | * For the time being, we preserve historic behavior and bypass the DMA |
| 238 | * API. |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 239 | * |
| 240 | * TODO: install a per-device DMA ops structure that does the right thing |
| 241 | * taking into account all the above quirks, and use the DMA API |
| 242 | * unconditionally on data path. |
Andy Lutomirski | d26c96c | 2016-02-02 21:46:35 -0800 | [diff] [blame] | 243 | */ |
| 244 | |
| 245 | static bool vring_use_dma_api(struct virtio_device *vdev) |
| 246 | { |
Michael S. Tsirkin | 24b6842 | 2020-06-24 19:17:04 -0400 | [diff] [blame] | 247 | if (!virtio_has_dma_quirk(vdev)) |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 248 | return true; |
| 249 | |
| 250 | /* Otherwise, we are left to guess. */ |
Andy Lutomirski | 78fe398 | 2016-02-02 21:46:40 -0800 | [diff] [blame] | 251 | /* |
| 252 | * In theory, it's possible to have a buggy QEMU-supposed |
| 253 | * emulated Q35 IOMMU and Xen enabled at the same time. On |
| 254 | * such a configuration, virtio has never worked and will |
| 255 | * not work without an even larger kludge. Instead, enable |
| 256 | * the DMA API if we're a Xen guest, which at least allows |
| 257 | * all of the sensible Xen configurations to work correctly. |
| 258 | */ |
| 259 | if (xen_domain()) |
| 260 | return true; |
| 261 | |
Andy Lutomirski | d26c96c | 2016-02-02 21:46:35 -0800 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | |
Joerg Roedel | e6d6dd6 | 2019-02-07 12:59:16 +0100 | [diff] [blame] | 265 | size_t virtio_max_dma_size(struct virtio_device *vdev) |
| 266 | { |
| 267 | size_t max_segment_size = SIZE_MAX; |
| 268 | |
| 269 | if (vring_use_dma_api(vdev)) |
| 270 | max_segment_size = dma_max_mapping_size(&vdev->dev); |
| 271 | |
| 272 | return max_segment_size; |
| 273 | } |
| 274 | EXPORT_SYMBOL_GPL(virtio_max_dma_size); |
| 275 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame] | 276 | static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, |
| 277 | dma_addr_t *dma_handle, gfp_t flag) |
| 278 | { |
| 279 | if (vring_use_dma_api(vdev)) { |
| 280 | return dma_alloc_coherent(vdev->dev.parent, size, |
| 281 | dma_handle, flag); |
| 282 | } else { |
| 283 | void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag); |
| 284 | |
| 285 | if (queue) { |
| 286 | phys_addr_t phys_addr = virt_to_phys(queue); |
| 287 | *dma_handle = (dma_addr_t)phys_addr; |
| 288 | |
| 289 | /* |
| 290 | * Sanity check: make sure we dind't truncate |
| 291 | * the address. The only arches I can find that |
| 292 | * have 64-bit phys_addr_t but 32-bit dma_addr_t |
| 293 | * are certain non-highmem MIPS and x86 |
| 294 | * configurations, but these configurations |
| 295 | * should never allocate physical pages above 32 |
| 296 | * bits, so this is fine. Just in case, throw a |
| 297 | * warning and abort if we end up with an |
| 298 | * unrepresentable address. |
| 299 | */ |
| 300 | if (WARN_ON_ONCE(*dma_handle != phys_addr)) { |
| 301 | free_pages_exact(queue, PAGE_ALIGN(size)); |
| 302 | return NULL; |
| 303 | } |
| 304 | } |
| 305 | return queue; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static void vring_free_queue(struct virtio_device *vdev, size_t size, |
| 310 | void *queue, dma_addr_t dma_handle) |
| 311 | { |
| 312 | if (vring_use_dma_api(vdev)) |
| 313 | dma_free_coherent(vdev->dev.parent, size, queue, dma_handle); |
| 314 | else |
| 315 | free_pages_exact(queue, PAGE_ALIGN(size)); |
| 316 | } |
| 317 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 318 | /* |
| 319 | * The DMA ops on various arches are rather gnarly right now, and |
| 320 | * making all of the arch DMA ops work on the vring device itself |
| 321 | * is a mess. For now, we use the parent device for DMA ops. |
| 322 | */ |
Michael S. Tsirkin | 75bfa81 | 2016-10-31 00:38:21 +0200 | [diff] [blame] | 323 | static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq) |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 324 | { |
| 325 | return vq->vq.vdev->dev.parent; |
| 326 | } |
| 327 | |
| 328 | /* Map one sg entry. */ |
| 329 | static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq, |
| 330 | struct scatterlist *sg, |
| 331 | enum dma_data_direction direction) |
| 332 | { |
Tiwei Bie | fb3fba6 | 2018-11-21 18:03:26 +0800 | [diff] [blame] | 333 | if (!vq->use_dma_api) |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 334 | return (dma_addr_t)sg_phys(sg); |
| 335 | |
| 336 | /* |
| 337 | * We can't use dma_map_sg, because we don't use scatterlists in |
| 338 | * the way it expects (we don't guarantee that the scatterlist |
| 339 | * will exist for the lifetime of the mapping). |
| 340 | */ |
| 341 | return dma_map_page(vring_dma_dev(vq), |
| 342 | sg_page(sg), sg->offset, sg->length, |
| 343 | direction); |
| 344 | } |
| 345 | |
| 346 | static dma_addr_t vring_map_single(const struct vring_virtqueue *vq, |
| 347 | void *cpu_addr, size_t size, |
| 348 | enum dma_data_direction direction) |
| 349 | { |
Tiwei Bie | fb3fba6 | 2018-11-21 18:03:26 +0800 | [diff] [blame] | 350 | if (!vq->use_dma_api) |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 351 | return (dma_addr_t)virt_to_phys(cpu_addr); |
| 352 | |
| 353 | return dma_map_single(vring_dma_dev(vq), |
| 354 | cpu_addr, size, direction); |
| 355 | } |
| 356 | |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 357 | static int vring_mapping_error(const struct vring_virtqueue *vq, |
| 358 | dma_addr_t addr) |
| 359 | { |
Tiwei Bie | fb3fba6 | 2018-11-21 18:03:26 +0800 | [diff] [blame] | 360 | if (!vq->use_dma_api) |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 361 | return 0; |
| 362 | |
| 363 | return dma_mapping_error(vring_dma_dev(vq), addr); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | /* |
| 368 | * Split ring specific functions - *_split(). |
| 369 | */ |
| 370 | |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 371 | static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq, |
| 372 | struct vring_desc *desc) |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 373 | { |
| 374 | u16 flags; |
| 375 | |
Tiwei Bie | fb3fba6 | 2018-11-21 18:03:26 +0800 | [diff] [blame] | 376 | if (!vq->use_dma_api) |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 377 | return; |
| 378 | |
| 379 | flags = virtio16_to_cpu(vq->vq.vdev, desc->flags); |
| 380 | |
| 381 | if (flags & VRING_DESC_F_INDIRECT) { |
| 382 | dma_unmap_single(vring_dma_dev(vq), |
| 383 | virtio64_to_cpu(vq->vq.vdev, desc->addr), |
| 384 | virtio32_to_cpu(vq->vq.vdev, desc->len), |
| 385 | (flags & VRING_DESC_F_WRITE) ? |
| 386 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 387 | } else { |
| 388 | dma_unmap_page(vring_dma_dev(vq), |
| 389 | virtio64_to_cpu(vq->vq.vdev, desc->addr), |
| 390 | virtio32_to_cpu(vq->vq.vdev, desc->len), |
| 391 | (flags & VRING_DESC_F_WRITE) ? |
| 392 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 393 | } |
| 394 | } |
| 395 | |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 396 | static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq, |
| 397 | unsigned int i) |
| 398 | { |
| 399 | struct vring_desc_extra *extra = vq->split.desc_extra; |
| 400 | u16 flags; |
| 401 | |
| 402 | if (!vq->use_dma_api) |
| 403 | goto out; |
| 404 | |
| 405 | flags = extra[i].flags; |
| 406 | |
| 407 | if (flags & VRING_DESC_F_INDIRECT) { |
| 408 | dma_unmap_single(vring_dma_dev(vq), |
| 409 | extra[i].addr, |
| 410 | extra[i].len, |
| 411 | (flags & VRING_DESC_F_WRITE) ? |
| 412 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 413 | } else { |
| 414 | dma_unmap_page(vring_dma_dev(vq), |
| 415 | extra[i].addr, |
| 416 | extra[i].len, |
| 417 | (flags & VRING_DESC_F_WRITE) ? |
| 418 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 419 | } |
| 420 | |
| 421 | out: |
| 422 | return extra[i].next; |
| 423 | } |
| 424 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 425 | static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq, |
| 426 | unsigned int total_sg, |
| 427 | gfp_t gfp) |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 428 | { |
| 429 | struct vring_desc *desc; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 430 | unsigned int i; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 431 | |
Will Deacon | b92b1b8 | 2012-10-19 14:03:33 +0100 | [diff] [blame] | 432 | /* |
| 433 | * We require lowmem mappings for the descriptors because |
| 434 | * otherwise virt_to_phys will give us bogus addresses in the |
| 435 | * virtqueue. |
| 436 | */ |
Michal Hocko | 8210753 | 2015-12-01 15:32:49 +0100 | [diff] [blame] | 437 | gfp &= ~__GFP_HIGHMEM; |
Will Deacon | b92b1b8 | 2012-10-19 14:03:33 +0100 | [diff] [blame] | 438 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 439 | desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 440 | if (!desc) |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 441 | return NULL; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 442 | |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 443 | for (i = 0; i < total_sg; i++) |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 444 | desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 445 | return desc; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Jason Wang | fe4c386 | 2021-06-04 13:53:48 +0800 | [diff] [blame] | 448 | static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq, |
| 449 | struct vring_desc *desc, |
| 450 | unsigned int i, |
| 451 | dma_addr_t addr, |
| 452 | unsigned int len, |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 453 | u16 flags, |
| 454 | bool indirect) |
Jason Wang | fe4c386 | 2021-06-04 13:53:48 +0800 | [diff] [blame] | 455 | { |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 456 | struct vring_virtqueue *vring = to_vvq(vq); |
| 457 | struct vring_desc_extra *extra = vring->split.desc_extra; |
| 458 | u16 next; |
| 459 | |
Jason Wang | fe4c386 | 2021-06-04 13:53:48 +0800 | [diff] [blame] | 460 | desc[i].flags = cpu_to_virtio16(vq->vdev, flags); |
| 461 | desc[i].addr = cpu_to_virtio64(vq->vdev, addr); |
| 462 | desc[i].len = cpu_to_virtio32(vq->vdev, len); |
| 463 | |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 464 | if (!indirect) { |
| 465 | next = extra[i].next; |
| 466 | desc[i].next = cpu_to_virtio16(vq->vdev, next); |
| 467 | |
| 468 | extra[i].addr = addr; |
| 469 | extra[i].len = len; |
| 470 | extra[i].flags = flags; |
| 471 | } else |
| 472 | next = virtio16_to_cpu(vq->vdev, desc[i].next); |
| 473 | |
| 474 | return next; |
Jason Wang | fe4c386 | 2021-06-04 13:53:48 +0800 | [diff] [blame] | 475 | } |
| 476 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 477 | static inline int virtqueue_add_split(struct virtqueue *_vq, |
| 478 | struct scatterlist *sgs[], |
| 479 | unsigned int total_sg, |
| 480 | unsigned int out_sgs, |
| 481 | unsigned int in_sgs, |
| 482 | void *data, |
| 483 | void *ctx, |
| 484 | gfp_t gfp) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 485 | { |
| 486 | struct vring_virtqueue *vq = to_vvq(_vq); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 487 | struct scatterlist *sg; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 488 | struct vring_desc *desc; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 489 | unsigned int i, n, avail, descs_used, prev, err_idx; |
Michael S. Tsirkin | 1fe9b6f | 2010-07-26 16:55:30 +0930 | [diff] [blame] | 490 | int head; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 491 | bool indirect; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 492 | |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 493 | START_USE(vq); |
| 494 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 495 | BUG_ON(data == NULL); |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 496 | BUG_ON(ctx && vq->indirect); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 497 | |
Rusty Russell | 7067044 | 2014-03-13 11:23:40 +1030 | [diff] [blame] | 498 | if (unlikely(vq->broken)) { |
| 499 | END_USE(vq); |
| 500 | return -EIO; |
| 501 | } |
| 502 | |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 503 | LAST_ADD_TIME_UPDATE(vq); |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 504 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 505 | BUG_ON(total_sg == 0); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 506 | |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 507 | head = vq->free_head; |
| 508 | |
Tiwei Bie | 2f18c2d | 2018-11-21 18:03:23 +0800 | [diff] [blame] | 509 | if (virtqueue_use_indirect(_vq, total_sg)) |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 510 | desc = alloc_indirect_split(_vq, total_sg, gfp); |
Richard W.M. Jones | 44ed808 | 2017-08-10 17:56:51 +0100 | [diff] [blame] | 511 | else { |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 512 | desc = NULL; |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 513 | 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] | 514 | } |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 515 | |
| 516 | if (desc) { |
| 517 | /* Use a single buffer which doesn't continue */ |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 518 | indirect = true; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 519 | /* Set up rest to use this indirect table. */ |
| 520 | i = 0; |
| 521 | descs_used = 1; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 522 | } else { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 523 | indirect = false; |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 524 | desc = vq->split.vring.desc; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 525 | i = head; |
| 526 | descs_used = total_sg; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | if (vq->vq.num_free < descs_used) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 530 | pr_debug("Can't add buf len %i - avail = %i\n", |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 531 | descs_used, vq->vq.num_free); |
Rusty Russell | 44653ea | 2008-07-25 12:06:04 -0500 | [diff] [blame] | 532 | /* FIXME: for historical reasons, we force a notify here if |
| 533 | * there are outgoing parts to the buffer. Presumably the |
| 534 | * host should service the ring ASAP. */ |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 535 | if (out_sgs) |
Rusty Russell | 44653ea | 2008-07-25 12:06:04 -0500 | [diff] [blame] | 536 | vq->notify(&vq->vq); |
Wei Yongjun | 58625ed | 2016-08-02 14:16:31 +0000 | [diff] [blame] | 537 | if (indirect) |
| 538 | kfree(desc); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 539 | END_USE(vq); |
| 540 | return -ENOSPC; |
| 541 | } |
| 542 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 543 | for (n = 0; n < out_sgs; n++) { |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 544 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 545 | dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE); |
| 546 | if (vring_mapping_error(vq, addr)) |
| 547 | goto unmap_release; |
| 548 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 549 | prev = i; |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 550 | /* Note that we trust indirect descriptor |
| 551 | * table since it use stream DMA mapping. |
| 552 | */ |
Jason Wang | fe4c386 | 2021-06-04 13:53:48 +0800 | [diff] [blame] | 553 | i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length, |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 554 | VRING_DESC_F_NEXT, |
| 555 | indirect); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 556 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 557 | } |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 558 | for (; n < (out_sgs + in_sgs); n++) { |
Rusty Russell | eeebf9b | 2014-09-11 10:17:37 +0930 | [diff] [blame] | 559 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 560 | dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE); |
| 561 | if (vring_mapping_error(vq, addr)) |
| 562 | goto unmap_release; |
| 563 | |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 564 | prev = i; |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 565 | /* Note that we trust indirect descriptor |
| 566 | * table since it use stream DMA mapping. |
| 567 | */ |
Jason Wang | fe4c386 | 2021-06-04 13:53:48 +0800 | [diff] [blame] | 568 | i = virtqueue_add_desc_split(_vq, desc, i, addr, |
| 569 | sg->length, |
| 570 | VRING_DESC_F_NEXT | |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 571 | VRING_DESC_F_WRITE, |
| 572 | indirect); |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 573 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 574 | } |
| 575 | /* Last one doesn't continue. */ |
Michael S. Tsirkin | 00e6f3d | 2014-10-22 15:42:09 +0300 | [diff] [blame] | 576 | desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 577 | if (!indirect && vq->use_dma_api) |
| 578 | vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags = |
| 579 | ~VRING_DESC_F_NEXT; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 580 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 581 | if (indirect) { |
| 582 | /* Now that the indirect table is filled in, map it. */ |
| 583 | dma_addr_t addr = vring_map_single( |
| 584 | vq, desc, total_sg * sizeof(struct vring_desc), |
| 585 | DMA_TO_DEVICE); |
| 586 | if (vring_mapping_error(vq, addr)) |
| 587 | goto unmap_release; |
| 588 | |
Jason Wang | fe4c386 | 2021-06-04 13:53:48 +0800 | [diff] [blame] | 589 | virtqueue_add_desc_split(_vq, vq->split.vring.desc, |
| 590 | head, addr, |
| 591 | total_sg * sizeof(struct vring_desc), |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 592 | VRING_DESC_F_INDIRECT, |
| 593 | false); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* We're using some buffers from the free list. */ |
| 597 | vq->vq.num_free -= descs_used; |
| 598 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 599 | /* Update free pointer */ |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 600 | if (indirect) |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 601 | vq->free_head = vq->split.desc_extra[head].next; |
Rusty Russell | b25bd25 | 2014-09-11 10:17:38 +0930 | [diff] [blame] | 602 | else |
| 603 | vq->free_head = i; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 604 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 605 | /* Store token and indirect buffer state. */ |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 606 | vq->split.desc_state[head].data = data; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 607 | if (indirect) |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 608 | vq->split.desc_state[head].indir_desc = desc; |
Jason Wang | 87646a3 | 2017-07-19 16:54:45 +0800 | [diff] [blame] | 609 | else |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 610 | vq->split.desc_state[head].indir_desc = ctx; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 611 | |
| 612 | /* 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] | 613 | * do sync). */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 614 | avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1); |
| 615 | vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 616 | |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 617 | /* Descriptors and available array need to be set before we expose the |
| 618 | * new available array entries. */ |
Rusty Russell | a9a0fef | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 619 | virtio_wmb(vq->weak_barriers); |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 620 | vq->split.avail_idx_shadow++; |
| 621 | vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, |
| 622 | vq->split.avail_idx_shadow); |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 623 | vq->num_added++; |
| 624 | |
Tetsuo Handa | 5e05bf5 | 2015-02-11 15:01:13 +1030 | [diff] [blame] | 625 | pr_debug("Added buffer head %i to %p\n", head, vq); |
| 626 | END_USE(vq); |
| 627 | |
Rusty Russell | ee7cd89 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 628 | /* This is very unlikely, but theoretically possible. Kick |
| 629 | * just in case. */ |
| 630 | if (unlikely(vq->num_added == (1 << 16) - 1)) |
| 631 | virtqueue_kick(_vq); |
| 632 | |
Rusty Russell | 98e8c6b | 2012-10-16 23:56:15 +1030 | [diff] [blame] | 633 | return 0; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 634 | |
| 635 | unmap_release: |
| 636 | err_idx = i; |
Matthias Lange | cf8f169 | 2019-09-06 16:59:01 +0200 | [diff] [blame] | 637 | |
| 638 | if (indirect) |
| 639 | i = 0; |
| 640 | else |
| 641 | i = head; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 642 | |
| 643 | for (n = 0; n < total_sg; n++) { |
| 644 | if (i == err_idx) |
| 645 | break; |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 646 | if (indirect) { |
| 647 | vring_unmap_one_split_indirect(vq, &desc[i]); |
| 648 | i = virtio16_to_cpu(_vq->vdev, desc[i].next); |
| 649 | } else |
| 650 | i = vring_unmap_one_split(vq, i); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 653 | if (indirect) |
| 654 | kfree(desc); |
| 655 | |
Michael S. Tsirkin | 3cc36f6 | 2016-08-03 07:18:51 +0300 | [diff] [blame] | 656 | END_USE(vq); |
Halil Pasic | f772800 | 2019-11-14 13:46:46 +0100 | [diff] [blame] | 657 | return -ENOMEM; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 658 | } |
Rusty Russell | 13816c7 | 2013-03-20 15:37:09 +1030 | [diff] [blame] | 659 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 660 | static bool virtqueue_kick_prepare_split(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 661 | { |
| 662 | struct vring_virtqueue *vq = to_vvq(_vq); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 663 | u16 new, old; |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 664 | bool needs_kick; |
| 665 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 666 | START_USE(vq); |
Jason Wang | a72caae | 2012-01-20 16:17:08 +0800 | [diff] [blame] | 667 | /* We need to expose available array entries before checking avail |
| 668 | * event. */ |
Rusty Russell | a9a0fef | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 669 | virtio_mb(vq->weak_barriers); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 670 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 671 | old = vq->split.avail_idx_shadow - vq->num_added; |
| 672 | new = vq->split.avail_idx_shadow; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 673 | vq->num_added = 0; |
| 674 | |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 675 | LAST_ADD_TIME_CHECK(vq); |
| 676 | LAST_ADD_TIME_INVALID(vq); |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 677 | |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 678 | if (vq->event) { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 679 | needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, |
| 680 | vring_avail_event(&vq->split.vring)), |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 681 | new, old); |
| 682 | } else { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 683 | needs_kick = !(vq->split.vring.used->flags & |
| 684 | cpu_to_virtio16(_vq->vdev, |
| 685 | VRING_USED_F_NO_NOTIFY)); |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 686 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 687 | END_USE(vq); |
Rusty Russell | 41f0377 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 688 | return needs_kick; |
| 689 | } |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 690 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 691 | static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, |
| 692 | void **ctx) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 693 | { |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 694 | unsigned int i, j; |
Gonglei | c60923c | 2016-11-22 13:51:50 +0800 | [diff] [blame] | 695 | __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 696 | |
| 697 | /* Clear data ptr. */ |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 698 | vq->split.desc_state[head].data = NULL; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 699 | |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 700 | /* Put back on free list: unmap first-level descriptors and find end */ |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 701 | i = head; |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 702 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 703 | while (vq->split.vring.desc[i].flags & nextflag) { |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 704 | vring_unmap_one_split(vq, i); |
| 705 | i = vq->split.desc_extra[i].next; |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 706 | vq->vq.num_free++; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 707 | } |
| 708 | |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 709 | vring_unmap_one_split(vq, i); |
| 710 | vq->split.desc_extra[i].next = vq->free_head; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 711 | vq->free_head = head; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 712 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 713 | /* Plus final descriptor */ |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 714 | vq->vq.num_free++; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 715 | |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 716 | if (vq->indirect) { |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 717 | struct vring_desc *indir_desc = |
| 718 | vq->split.desc_state[head].indir_desc; |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 719 | u32 len; |
| 720 | |
| 721 | /* Free the indirect table, if any, now that it's unmapped. */ |
| 722 | if (!indir_desc) |
| 723 | return; |
| 724 | |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 725 | len = vq->split.desc_extra[head].len; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 726 | |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 727 | BUG_ON(!(vq->split.desc_extra[head].flags & |
| 728 | VRING_DESC_F_INDIRECT)); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 729 | BUG_ON(len == 0 || len % sizeof(struct vring_desc)); |
| 730 | |
| 731 | for (j = 0; j < len / sizeof(struct vring_desc); j++) |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 732 | vring_unmap_one_split_indirect(vq, &indir_desc[j]); |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 733 | |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 734 | kfree(indir_desc); |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 735 | vq->split.desc_state[head].indir_desc = NULL; |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 736 | } else if (ctx) { |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 737 | *ctx = vq->split.desc_state[head].indir_desc; |
Andy Lutomirski | 780bc79 | 2016-02-02 21:46:36 -0800 | [diff] [blame] | 738 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 739 | } |
| 740 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 741 | static inline bool more_used_split(const struct vring_virtqueue *vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 742 | { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 743 | return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, |
| 744 | vq->split.vring.used->idx); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 745 | } |
| 746 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 747 | static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, |
| 748 | unsigned int *len, |
| 749 | void **ctx) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 750 | { |
| 751 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 752 | void *ret; |
| 753 | unsigned int i; |
Rusty Russell | 3b720b8 | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 754 | u16 last_used; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 755 | |
| 756 | START_USE(vq); |
| 757 | |
Rusty Russell | 5ef8275 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 758 | if (unlikely(vq->broken)) { |
| 759 | END_USE(vq); |
| 760 | return NULL; |
| 761 | } |
| 762 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 763 | if (!more_used_split(vq)) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 764 | pr_debug("No more buffers in queue\n"); |
| 765 | END_USE(vq); |
| 766 | return NULL; |
| 767 | } |
| 768 | |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 769 | /* Only get used array entries after they have been exposed by host. */ |
Rusty Russell | a9a0fef | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 770 | virtio_rmb(vq->weak_barriers); |
Michael S. Tsirkin | 2d61ba9 | 2009-10-25 15:28:53 +0200 | [diff] [blame] | 771 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 772 | last_used = (vq->last_used_idx & (vq->split.vring.num - 1)); |
| 773 | i = virtio32_to_cpu(_vq->vdev, |
| 774 | vq->split.vring.used->ring[last_used].id); |
| 775 | *len = virtio32_to_cpu(_vq->vdev, |
| 776 | vq->split.vring.used->ring[last_used].len); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 777 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 778 | if (unlikely(i >= vq->split.vring.num)) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 779 | BAD_RING(vq, "id %u out of range\n", i); |
| 780 | return NULL; |
| 781 | } |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 782 | if (unlikely(!vq->split.desc_state[i].data)) { |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 783 | BAD_RING(vq, "id %u is not a head!\n", i); |
| 784 | return NULL; |
| 785 | } |
| 786 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 787 | /* detach_buf_split clears data, so grab it now. */ |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 788 | ret = vq->split.desc_state[i].data; |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 789 | detach_buf_split(vq, i, ctx); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 790 | vq->last_used_idx++; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 791 | /* If we expect an interrupt for the next entry, tell host |
| 792 | * by writing event index and flush out the write before |
| 793 | * the read in the next get_buf call. */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 794 | if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) |
Michael S. Tsirkin | 788e5b3 | 2015-12-17 12:20:39 +0200 | [diff] [blame] | 795 | virtio_store_mb(vq->weak_barriers, |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 796 | &vring_used_event(&vq->split.vring), |
Michael S. Tsirkin | 788e5b3 | 2015-12-17 12:20:39 +0200 | [diff] [blame] | 797 | cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 798 | |
Tiwei Bie | 4d6a105 | 2018-11-21 18:03:22 +0800 | [diff] [blame] | 799 | LAST_ADD_TIME_INVALID(vq); |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 800 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 801 | END_USE(vq); |
| 802 | return ret; |
| 803 | } |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 804 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 805 | static void virtqueue_disable_cb_split(struct virtqueue *_vq) |
| 806 | { |
| 807 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 808 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 809 | if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { |
| 810 | vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; |
Michael S. Tsirkin | 8d622d2 | 2021-04-13 01:19:16 -0400 | [diff] [blame] | 811 | if (vq->event) |
| 812 | /* TODO: this is a hack. Figure out a cleaner value to write. */ |
| 813 | vring_used_event(&vq->split.vring) = 0x0; |
| 814 | else |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 815 | vq->split.vring.avail->flags = |
| 816 | cpu_to_virtio16(_vq->vdev, |
| 817 | vq->split.avail_flags_shadow); |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 821 | static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq) |
Michael S. Tsirkin | cc22988 | 2013-07-09 13:19:18 +0300 | [diff] [blame] | 822 | { |
| 823 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 824 | u16 last_used_idx; |
| 825 | |
| 826 | START_USE(vq); |
| 827 | |
| 828 | /* We optimistically turn back on interrupts, then check if there was |
| 829 | * more to do. */ |
| 830 | /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to |
| 831 | * either clear the flags bit or point the event index at the next |
| 832 | * entry. Always do both to keep code simple. */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 833 | if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { |
| 834 | vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; |
Ladi Prosek | 0ea1e4a | 2016-08-31 14:00:04 +0200 | [diff] [blame] | 835 | if (!vq->event) |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 836 | vq->split.vring.avail->flags = |
| 837 | cpu_to_virtio16(_vq->vdev, |
| 838 | vq->split.avail_flags_shadow); |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 839 | } |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 840 | vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev, |
| 841 | last_used_idx = vq->last_used_idx); |
Michael S. Tsirkin | cc22988 | 2013-07-09 13:19:18 +0300 | [diff] [blame] | 842 | END_USE(vq); |
| 843 | return last_used_idx; |
| 844 | } |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 845 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 846 | static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx) |
| 847 | { |
| 848 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 849 | |
| 850 | return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 851 | vq->split.vring.used->idx); |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 852 | } |
| 853 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 854 | static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq) |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 855 | { |
| 856 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 857 | u16 bufs; |
| 858 | |
| 859 | START_USE(vq); |
| 860 | |
| 861 | /* We optimistically turn back on interrupts, then check if there was |
| 862 | * more to do. */ |
| 863 | /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to |
| 864 | * 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] | 865 | * entry. Always update the event index to keep code simple. */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 866 | if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { |
| 867 | vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; |
Ladi Prosek | 0ea1e4a | 2016-08-31 14:00:04 +0200 | [diff] [blame] | 868 | if (!vq->event) |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 869 | vq->split.vring.avail->flags = |
| 870 | cpu_to_virtio16(_vq->vdev, |
| 871 | vq->split.avail_flags_shadow); |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 872 | } |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 873 | /* TODO: tune this threshold */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 874 | 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] | 875 | |
| 876 | virtio_store_mb(vq->weak_barriers, |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 877 | &vring_used_event(&vq->split.vring), |
Michael S. Tsirkin | 788e5b3 | 2015-12-17 12:20:39 +0200 | [diff] [blame] | 878 | cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); |
| 879 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 880 | if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx) |
| 881 | - vq->last_used_idx) > bufs)) { |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 882 | END_USE(vq); |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | END_USE(vq); |
| 887 | return true; |
| 888 | } |
Michael S. Tsirkin | 7ab358c | 2011-05-20 02:11:14 +0300 | [diff] [blame] | 889 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 890 | static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq) |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 891 | { |
| 892 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 893 | unsigned int i; |
| 894 | void *buf; |
| 895 | |
| 896 | START_USE(vq); |
| 897 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 898 | for (i = 0; i < vq->split.vring.num; i++) { |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 899 | if (!vq->split.desc_state[i].data) |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 900 | continue; |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 901 | /* detach_buf_split clears data, so grab it now. */ |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 902 | buf = vq->split.desc_state[i].data; |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 903 | detach_buf_split(vq, i, NULL); |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 904 | vq->split.avail_idx_shadow--; |
| 905 | vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, |
| 906 | vq->split.avail_idx_shadow); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 907 | END_USE(vq); |
| 908 | return buf; |
| 909 | } |
| 910 | /* That should have freed everything. */ |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 911 | BUG_ON(vq->vq.num_free != vq->split.vring.num); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 912 | |
| 913 | END_USE(vq); |
| 914 | return NULL; |
| 915 | } |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 916 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame] | 917 | static struct virtqueue *vring_create_virtqueue_split( |
| 918 | unsigned int index, |
| 919 | unsigned int num, |
| 920 | unsigned int vring_align, |
| 921 | struct virtio_device *vdev, |
| 922 | bool weak_barriers, |
| 923 | bool may_reduce_num, |
| 924 | bool context, |
| 925 | bool (*notify)(struct virtqueue *), |
| 926 | void (*callback)(struct virtqueue *), |
| 927 | const char *name) |
| 928 | { |
| 929 | struct virtqueue *vq; |
| 930 | void *queue = NULL; |
| 931 | dma_addr_t dma_addr; |
| 932 | size_t queue_size_in_bytes; |
| 933 | struct vring vring; |
| 934 | |
| 935 | /* We assume num is a power of 2. */ |
| 936 | if (num & (num - 1)) { |
| 937 | dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); |
| 938 | return NULL; |
| 939 | } |
| 940 | |
| 941 | /* TODO: allocate each queue chunk individually */ |
| 942 | for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { |
| 943 | queue = vring_alloc_queue(vdev, vring_size(num, vring_align), |
| 944 | &dma_addr, |
| 945 | GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); |
| 946 | if (queue) |
| 947 | break; |
Cornelia Huck | cf94db2 | 2019-04-08 14:33:22 +0200 | [diff] [blame] | 948 | if (!may_reduce_num) |
| 949 | return NULL; |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | if (!num) |
| 953 | return NULL; |
| 954 | |
| 955 | if (!queue) { |
| 956 | /* Try to get a single page. You are my only hope! */ |
| 957 | queue = vring_alloc_queue(vdev, vring_size(num, vring_align), |
| 958 | &dma_addr, GFP_KERNEL|__GFP_ZERO); |
| 959 | } |
| 960 | if (!queue) |
| 961 | return NULL; |
| 962 | |
| 963 | queue_size_in_bytes = vring_size(num, vring_align); |
| 964 | vring_init(&vring, num, queue, vring_align); |
| 965 | |
| 966 | vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, |
| 967 | notify, callback, name); |
| 968 | if (!vq) { |
| 969 | vring_free_queue(vdev, queue_size_in_bytes, queue, |
| 970 | dma_addr); |
| 971 | return NULL; |
| 972 | } |
| 973 | |
| 974 | to_vvq(vq)->split.queue_dma_addr = dma_addr; |
| 975 | to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes; |
| 976 | to_vvq(vq)->we_own_ring = true; |
| 977 | |
| 978 | return vq; |
| 979 | } |
| 980 | |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 981 | |
| 982 | /* |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 983 | * Packed ring specific functions - *_packed(). |
| 984 | */ |
| 985 | |
| 986 | static void vring_unmap_state_packed(const struct vring_virtqueue *vq, |
Jason Wang | 1f28750 | 2021-06-04 13:53:45 +0800 | [diff] [blame] | 987 | struct vring_desc_extra *state) |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 988 | { |
| 989 | u16 flags; |
| 990 | |
| 991 | if (!vq->use_dma_api) |
| 992 | return; |
| 993 | |
| 994 | flags = state->flags; |
| 995 | |
| 996 | if (flags & VRING_DESC_F_INDIRECT) { |
| 997 | dma_unmap_single(vring_dma_dev(vq), |
| 998 | state->addr, state->len, |
| 999 | (flags & VRING_DESC_F_WRITE) ? |
| 1000 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 1001 | } else { |
| 1002 | dma_unmap_page(vring_dma_dev(vq), |
| 1003 | state->addr, state->len, |
| 1004 | (flags & VRING_DESC_F_WRITE) ? |
| 1005 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | static void vring_unmap_desc_packed(const struct vring_virtqueue *vq, |
| 1010 | struct vring_packed_desc *desc) |
| 1011 | { |
| 1012 | u16 flags; |
| 1013 | |
| 1014 | if (!vq->use_dma_api) |
| 1015 | return; |
| 1016 | |
| 1017 | flags = le16_to_cpu(desc->flags); |
| 1018 | |
| 1019 | if (flags & VRING_DESC_F_INDIRECT) { |
| 1020 | dma_unmap_single(vring_dma_dev(vq), |
| 1021 | le64_to_cpu(desc->addr), |
| 1022 | le32_to_cpu(desc->len), |
| 1023 | (flags & VRING_DESC_F_WRITE) ? |
| 1024 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 1025 | } else { |
| 1026 | dma_unmap_page(vring_dma_dev(vq), |
| 1027 | le64_to_cpu(desc->addr), |
| 1028 | le32_to_cpu(desc->len), |
| 1029 | (flags & VRING_DESC_F_WRITE) ? |
| 1030 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg, |
| 1035 | gfp_t gfp) |
| 1036 | { |
| 1037 | struct vring_packed_desc *desc; |
| 1038 | |
| 1039 | /* |
| 1040 | * We require lowmem mappings for the descriptors because |
| 1041 | * otherwise virt_to_phys will give us bogus addresses in the |
| 1042 | * virtqueue. |
| 1043 | */ |
| 1044 | gfp &= ~__GFP_HIGHMEM; |
| 1045 | |
| 1046 | desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp); |
| 1047 | |
| 1048 | return desc; |
| 1049 | } |
| 1050 | |
| 1051 | static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, |
| 1052 | struct scatterlist *sgs[], |
| 1053 | unsigned int total_sg, |
| 1054 | unsigned int out_sgs, |
| 1055 | unsigned int in_sgs, |
| 1056 | void *data, |
| 1057 | gfp_t gfp) |
| 1058 | { |
| 1059 | struct vring_packed_desc *desc; |
| 1060 | struct scatterlist *sg; |
| 1061 | unsigned int i, n, err_idx; |
| 1062 | u16 head, id; |
| 1063 | dma_addr_t addr; |
| 1064 | |
| 1065 | head = vq->packed.next_avail_idx; |
| 1066 | desc = alloc_indirect_packed(total_sg, gfp); |
| 1067 | |
| 1068 | if (unlikely(vq->vq.num_free < 1)) { |
| 1069 | pr_debug("Can't add buf len 1 - avail = 0\n"); |
YueHaibing | df0bfe7 | 2019-03-12 15:06:53 +0800 | [diff] [blame] | 1070 | kfree(desc); |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1071 | END_USE(vq); |
| 1072 | return -ENOSPC; |
| 1073 | } |
| 1074 | |
| 1075 | i = 0; |
| 1076 | id = vq->free_head; |
| 1077 | BUG_ON(id == vq->packed.vring.num); |
| 1078 | |
| 1079 | for (n = 0; n < out_sgs + in_sgs; n++) { |
| 1080 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
| 1081 | addr = vring_map_one_sg(vq, sg, n < out_sgs ? |
| 1082 | DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 1083 | if (vring_mapping_error(vq, addr)) |
| 1084 | goto unmap_release; |
| 1085 | |
| 1086 | desc[i].flags = cpu_to_le16(n < out_sgs ? |
| 1087 | 0 : VRING_DESC_F_WRITE); |
| 1088 | desc[i].addr = cpu_to_le64(addr); |
| 1089 | desc[i].len = cpu_to_le32(sg->length); |
| 1090 | i++; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | /* Now that the indirect table is filled in, map it. */ |
| 1095 | addr = vring_map_single(vq, desc, |
| 1096 | total_sg * sizeof(struct vring_packed_desc), |
| 1097 | DMA_TO_DEVICE); |
| 1098 | if (vring_mapping_error(vq, addr)) |
| 1099 | goto unmap_release; |
| 1100 | |
| 1101 | vq->packed.vring.desc[head].addr = cpu_to_le64(addr); |
| 1102 | vq->packed.vring.desc[head].len = cpu_to_le32(total_sg * |
| 1103 | sizeof(struct vring_packed_desc)); |
| 1104 | vq->packed.vring.desc[head].id = cpu_to_le16(id); |
| 1105 | |
| 1106 | if (vq->use_dma_api) { |
| 1107 | vq->packed.desc_extra[id].addr = addr; |
| 1108 | vq->packed.desc_extra[id].len = total_sg * |
| 1109 | sizeof(struct vring_packed_desc); |
| 1110 | vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT | |
| 1111 | vq->packed.avail_used_flags; |
| 1112 | } |
| 1113 | |
| 1114 | /* |
| 1115 | * A driver MUST NOT make the first descriptor in the list |
| 1116 | * available before all subsequent descriptors comprising |
| 1117 | * the list are made available. |
| 1118 | */ |
| 1119 | virtio_wmb(vq->weak_barriers); |
| 1120 | vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT | |
| 1121 | vq->packed.avail_used_flags); |
| 1122 | |
| 1123 | /* We're using some buffers from the free list. */ |
| 1124 | vq->vq.num_free -= 1; |
| 1125 | |
| 1126 | /* Update free pointer */ |
| 1127 | n = head + 1; |
| 1128 | if (n >= vq->packed.vring.num) { |
| 1129 | n = 0; |
| 1130 | vq->packed.avail_wrap_counter ^= 1; |
| 1131 | vq->packed.avail_used_flags ^= |
| 1132 | 1 << VRING_PACKED_DESC_F_AVAIL | |
| 1133 | 1 << VRING_PACKED_DESC_F_USED; |
| 1134 | } |
| 1135 | vq->packed.next_avail_idx = n; |
Jason Wang | aeef9b4 | 2021-06-04 13:53:44 +0800 | [diff] [blame] | 1136 | vq->free_head = vq->packed.desc_extra[id].next; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1137 | |
| 1138 | /* Store token and indirect buffer state. */ |
| 1139 | vq->packed.desc_state[id].num = 1; |
| 1140 | vq->packed.desc_state[id].data = data; |
| 1141 | vq->packed.desc_state[id].indir_desc = desc; |
| 1142 | vq->packed.desc_state[id].last = id; |
| 1143 | |
| 1144 | vq->num_added += 1; |
| 1145 | |
| 1146 | pr_debug("Added buffer head %i to %p\n", head, vq); |
| 1147 | END_USE(vq); |
| 1148 | |
| 1149 | return 0; |
| 1150 | |
| 1151 | unmap_release: |
| 1152 | err_idx = i; |
| 1153 | |
| 1154 | for (i = 0; i < err_idx; i++) |
| 1155 | vring_unmap_desc_packed(vq, &desc[i]); |
| 1156 | |
| 1157 | kfree(desc); |
| 1158 | |
| 1159 | END_USE(vq); |
Halil Pasic | f772800 | 2019-11-14 13:46:46 +0100 | [diff] [blame] | 1160 | return -ENOMEM; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | static inline int virtqueue_add_packed(struct virtqueue *_vq, |
| 1164 | struct scatterlist *sgs[], |
| 1165 | unsigned int total_sg, |
| 1166 | unsigned int out_sgs, |
| 1167 | unsigned int in_sgs, |
| 1168 | void *data, |
| 1169 | void *ctx, |
| 1170 | gfp_t gfp) |
| 1171 | { |
| 1172 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1173 | struct vring_packed_desc *desc; |
| 1174 | struct scatterlist *sg; |
| 1175 | unsigned int i, n, c, descs_used, err_idx; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 1176 | __le16 head_flags, flags; |
| 1177 | u16 head, id, prev, curr, avail_used_flags; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1178 | |
| 1179 | START_USE(vq); |
| 1180 | |
| 1181 | BUG_ON(data == NULL); |
| 1182 | BUG_ON(ctx && vq->indirect); |
| 1183 | |
| 1184 | if (unlikely(vq->broken)) { |
| 1185 | END_USE(vq); |
| 1186 | return -EIO; |
| 1187 | } |
| 1188 | |
| 1189 | LAST_ADD_TIME_UPDATE(vq); |
| 1190 | |
| 1191 | BUG_ON(total_sg == 0); |
| 1192 | |
| 1193 | if (virtqueue_use_indirect(_vq, total_sg)) |
| 1194 | return virtqueue_add_indirect_packed(vq, sgs, total_sg, |
| 1195 | out_sgs, in_sgs, data, gfp); |
| 1196 | |
| 1197 | head = vq->packed.next_avail_idx; |
| 1198 | avail_used_flags = vq->packed.avail_used_flags; |
| 1199 | |
| 1200 | WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect); |
| 1201 | |
| 1202 | desc = vq->packed.vring.desc; |
| 1203 | i = head; |
| 1204 | descs_used = total_sg; |
| 1205 | |
| 1206 | if (unlikely(vq->vq.num_free < descs_used)) { |
| 1207 | pr_debug("Can't add buf len %i - avail = %i\n", |
| 1208 | descs_used, vq->vq.num_free); |
| 1209 | END_USE(vq); |
| 1210 | return -ENOSPC; |
| 1211 | } |
| 1212 | |
| 1213 | id = vq->free_head; |
| 1214 | BUG_ON(id == vq->packed.vring.num); |
| 1215 | |
| 1216 | curr = id; |
| 1217 | c = 0; |
| 1218 | for (n = 0; n < out_sgs + in_sgs; n++) { |
| 1219 | for (sg = sgs[n]; sg; sg = sg_next(sg)) { |
| 1220 | dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ? |
| 1221 | DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 1222 | if (vring_mapping_error(vq, addr)) |
| 1223 | goto unmap_release; |
| 1224 | |
| 1225 | flags = cpu_to_le16(vq->packed.avail_used_flags | |
| 1226 | (++c == total_sg ? 0 : VRING_DESC_F_NEXT) | |
| 1227 | (n < out_sgs ? 0 : VRING_DESC_F_WRITE)); |
| 1228 | if (i == head) |
| 1229 | head_flags = flags; |
| 1230 | else |
| 1231 | desc[i].flags = flags; |
| 1232 | |
| 1233 | desc[i].addr = cpu_to_le64(addr); |
| 1234 | desc[i].len = cpu_to_le32(sg->length); |
| 1235 | desc[i].id = cpu_to_le16(id); |
| 1236 | |
| 1237 | if (unlikely(vq->use_dma_api)) { |
| 1238 | vq->packed.desc_extra[curr].addr = addr; |
| 1239 | vq->packed.desc_extra[curr].len = sg->length; |
| 1240 | vq->packed.desc_extra[curr].flags = |
| 1241 | le16_to_cpu(flags); |
| 1242 | } |
| 1243 | prev = curr; |
Jason Wang | aeef9b4 | 2021-06-04 13:53:44 +0800 | [diff] [blame] | 1244 | curr = vq->packed.desc_extra[curr].next; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1245 | |
| 1246 | if ((unlikely(++i >= vq->packed.vring.num))) { |
| 1247 | i = 0; |
| 1248 | vq->packed.avail_used_flags ^= |
| 1249 | 1 << VRING_PACKED_DESC_F_AVAIL | |
| 1250 | 1 << VRING_PACKED_DESC_F_USED; |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | if (i < head) |
| 1256 | vq->packed.avail_wrap_counter ^= 1; |
| 1257 | |
| 1258 | /* We're using some buffers from the free list. */ |
| 1259 | vq->vq.num_free -= descs_used; |
| 1260 | |
| 1261 | /* Update free pointer */ |
| 1262 | vq->packed.next_avail_idx = i; |
| 1263 | vq->free_head = curr; |
| 1264 | |
| 1265 | /* Store token. */ |
| 1266 | vq->packed.desc_state[id].num = descs_used; |
| 1267 | vq->packed.desc_state[id].data = data; |
| 1268 | vq->packed.desc_state[id].indir_desc = ctx; |
| 1269 | vq->packed.desc_state[id].last = prev; |
| 1270 | |
| 1271 | /* |
| 1272 | * A driver MUST NOT make the first descriptor in the list |
| 1273 | * available before all subsequent descriptors comprising |
| 1274 | * the list are made available. |
| 1275 | */ |
| 1276 | virtio_wmb(vq->weak_barriers); |
| 1277 | vq->packed.vring.desc[head].flags = head_flags; |
| 1278 | vq->num_added += descs_used; |
| 1279 | |
| 1280 | pr_debug("Added buffer head %i to %p\n", head, vq); |
| 1281 | END_USE(vq); |
| 1282 | |
| 1283 | return 0; |
| 1284 | |
| 1285 | unmap_release: |
| 1286 | err_idx = i; |
| 1287 | i = head; |
Jason Wang | 4459386 | 2021-06-04 13:53:47 +0800 | [diff] [blame] | 1288 | curr = vq->free_head; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1289 | |
| 1290 | vq->packed.avail_used_flags = avail_used_flags; |
| 1291 | |
| 1292 | for (n = 0; n < total_sg; n++) { |
| 1293 | if (i == err_idx) |
| 1294 | break; |
Jason Wang | 4459386 | 2021-06-04 13:53:47 +0800 | [diff] [blame] | 1295 | vring_unmap_state_packed(vq, |
| 1296 | &vq->packed.desc_extra[curr]); |
| 1297 | curr = vq->packed.desc_extra[curr].next; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1298 | i++; |
| 1299 | if (i >= vq->packed.vring.num) |
| 1300 | i = 0; |
| 1301 | } |
| 1302 | |
| 1303 | END_USE(vq); |
| 1304 | return -EIO; |
| 1305 | } |
| 1306 | |
| 1307 | static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq) |
| 1308 | { |
| 1309 | struct vring_virtqueue *vq = to_vvq(_vq); |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1310 | u16 new, old, off_wrap, flags, wrap_counter, event_idx; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1311 | bool needs_kick; |
| 1312 | union { |
| 1313 | struct { |
| 1314 | __le16 off_wrap; |
| 1315 | __le16 flags; |
| 1316 | }; |
| 1317 | u32 u32; |
| 1318 | } snapshot; |
| 1319 | |
| 1320 | START_USE(vq); |
| 1321 | |
| 1322 | /* |
| 1323 | * We need to expose the new flags value before checking notification |
| 1324 | * suppressions. |
| 1325 | */ |
| 1326 | virtio_mb(vq->weak_barriers); |
| 1327 | |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1328 | old = vq->packed.next_avail_idx - vq->num_added; |
| 1329 | new = vq->packed.next_avail_idx; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1330 | vq->num_added = 0; |
| 1331 | |
| 1332 | snapshot.u32 = *(u32 *)vq->packed.vring.device; |
| 1333 | flags = le16_to_cpu(snapshot.flags); |
| 1334 | |
| 1335 | LAST_ADD_TIME_CHECK(vq); |
| 1336 | LAST_ADD_TIME_INVALID(vq); |
| 1337 | |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1338 | if (flags != VRING_PACKED_EVENT_FLAG_DESC) { |
| 1339 | needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE); |
| 1340 | goto out; |
| 1341 | } |
| 1342 | |
| 1343 | off_wrap = le16_to_cpu(snapshot.off_wrap); |
| 1344 | |
| 1345 | wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; |
| 1346 | event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); |
| 1347 | if (wrap_counter != vq->packed.avail_wrap_counter) |
| 1348 | event_idx -= vq->packed.vring.num; |
| 1349 | |
| 1350 | needs_kick = vring_need_event(event_idx, new, old); |
| 1351 | out: |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1352 | END_USE(vq); |
| 1353 | return needs_kick; |
| 1354 | } |
| 1355 | |
| 1356 | static void detach_buf_packed(struct vring_virtqueue *vq, |
| 1357 | unsigned int id, void **ctx) |
| 1358 | { |
| 1359 | struct vring_desc_state_packed *state = NULL; |
| 1360 | struct vring_packed_desc *desc; |
| 1361 | unsigned int i, curr; |
| 1362 | |
| 1363 | state = &vq->packed.desc_state[id]; |
| 1364 | |
| 1365 | /* Clear data ptr. */ |
| 1366 | state->data = NULL; |
| 1367 | |
Jason Wang | aeef9b4 | 2021-06-04 13:53:44 +0800 | [diff] [blame] | 1368 | vq->packed.desc_extra[state->last].next = vq->free_head; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1369 | vq->free_head = id; |
| 1370 | vq->vq.num_free += state->num; |
| 1371 | |
| 1372 | if (unlikely(vq->use_dma_api)) { |
| 1373 | curr = id; |
| 1374 | for (i = 0; i < state->num; i++) { |
| 1375 | vring_unmap_state_packed(vq, |
| 1376 | &vq->packed.desc_extra[curr]); |
Jason Wang | aeef9b4 | 2021-06-04 13:53:44 +0800 | [diff] [blame] | 1377 | curr = vq->packed.desc_extra[curr].next; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | if (vq->indirect) { |
| 1382 | u32 len; |
| 1383 | |
| 1384 | /* Free the indirect table, if any, now that it's unmapped. */ |
| 1385 | desc = state->indir_desc; |
| 1386 | if (!desc) |
| 1387 | return; |
| 1388 | |
| 1389 | if (vq->use_dma_api) { |
| 1390 | len = vq->packed.desc_extra[id].len; |
| 1391 | for (i = 0; i < len / sizeof(struct vring_packed_desc); |
| 1392 | i++) |
| 1393 | vring_unmap_desc_packed(vq, &desc[i]); |
| 1394 | } |
| 1395 | kfree(desc); |
| 1396 | state->indir_desc = NULL; |
| 1397 | } else if (ctx) { |
| 1398 | *ctx = state->indir_desc; |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | static inline bool is_used_desc_packed(const struct vring_virtqueue *vq, |
| 1403 | u16 idx, bool used_wrap_counter) |
| 1404 | { |
| 1405 | bool avail, used; |
| 1406 | u16 flags; |
| 1407 | |
| 1408 | flags = le16_to_cpu(vq->packed.vring.desc[idx].flags); |
| 1409 | avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL)); |
| 1410 | used = !!(flags & (1 << VRING_PACKED_DESC_F_USED)); |
| 1411 | |
| 1412 | return avail == used && used == used_wrap_counter; |
| 1413 | } |
| 1414 | |
| 1415 | static inline bool more_used_packed(const struct vring_virtqueue *vq) |
| 1416 | { |
| 1417 | return is_used_desc_packed(vq, vq->last_used_idx, |
| 1418 | vq->packed.used_wrap_counter); |
| 1419 | } |
| 1420 | |
| 1421 | static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq, |
| 1422 | unsigned int *len, |
| 1423 | void **ctx) |
| 1424 | { |
| 1425 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1426 | u16 last_used, id; |
| 1427 | void *ret; |
| 1428 | |
| 1429 | START_USE(vq); |
| 1430 | |
| 1431 | if (unlikely(vq->broken)) { |
| 1432 | END_USE(vq); |
| 1433 | return NULL; |
| 1434 | } |
| 1435 | |
| 1436 | if (!more_used_packed(vq)) { |
| 1437 | pr_debug("No more buffers in queue\n"); |
| 1438 | END_USE(vq); |
| 1439 | return NULL; |
| 1440 | } |
| 1441 | |
| 1442 | /* Only get used elements after they have been exposed by host. */ |
| 1443 | virtio_rmb(vq->weak_barriers); |
| 1444 | |
| 1445 | last_used = vq->last_used_idx; |
| 1446 | id = le16_to_cpu(vq->packed.vring.desc[last_used].id); |
| 1447 | *len = le32_to_cpu(vq->packed.vring.desc[last_used].len); |
| 1448 | |
| 1449 | if (unlikely(id >= vq->packed.vring.num)) { |
| 1450 | BAD_RING(vq, "id %u out of range\n", id); |
| 1451 | return NULL; |
| 1452 | } |
| 1453 | if (unlikely(!vq->packed.desc_state[id].data)) { |
| 1454 | BAD_RING(vq, "id %u is not a head!\n", id); |
| 1455 | return NULL; |
| 1456 | } |
| 1457 | |
| 1458 | /* detach_buf_packed clears data, so grab it now. */ |
| 1459 | ret = vq->packed.desc_state[id].data; |
| 1460 | detach_buf_packed(vq, id, ctx); |
| 1461 | |
| 1462 | vq->last_used_idx += vq->packed.desc_state[id].num; |
| 1463 | if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) { |
| 1464 | vq->last_used_idx -= vq->packed.vring.num; |
| 1465 | vq->packed.used_wrap_counter ^= 1; |
| 1466 | } |
| 1467 | |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1468 | /* |
| 1469 | * If we expect an interrupt for the next entry, tell host |
| 1470 | * by writing event index and flush out the write before |
| 1471 | * the read in the next get_buf call. |
| 1472 | */ |
| 1473 | if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC) |
| 1474 | virtio_store_mb(vq->weak_barriers, |
| 1475 | &vq->packed.vring.driver->off_wrap, |
| 1476 | cpu_to_le16(vq->last_used_idx | |
| 1477 | (vq->packed.used_wrap_counter << |
| 1478 | VRING_PACKED_EVENT_F_WRAP_CTR))); |
| 1479 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1480 | LAST_ADD_TIME_INVALID(vq); |
| 1481 | |
| 1482 | END_USE(vq); |
| 1483 | return ret; |
| 1484 | } |
| 1485 | |
| 1486 | static void virtqueue_disable_cb_packed(struct virtqueue *_vq) |
| 1487 | { |
| 1488 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1489 | |
| 1490 | if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) { |
| 1491 | vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; |
| 1492 | vq->packed.vring.driver->flags = |
| 1493 | cpu_to_le16(vq->packed.event_flags_shadow); |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | static unsigned virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq) |
| 1498 | { |
| 1499 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1500 | |
| 1501 | START_USE(vq); |
| 1502 | |
| 1503 | /* |
| 1504 | * We optimistically turn back on interrupts, then check if there was |
| 1505 | * more to do. |
| 1506 | */ |
| 1507 | |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1508 | if (vq->event) { |
| 1509 | vq->packed.vring.driver->off_wrap = |
| 1510 | cpu_to_le16(vq->last_used_idx | |
| 1511 | (vq->packed.used_wrap_counter << |
| 1512 | VRING_PACKED_EVENT_F_WRAP_CTR)); |
| 1513 | /* |
| 1514 | * We need to update event offset and event wrap |
| 1515 | * counter first before updating event flags. |
| 1516 | */ |
| 1517 | virtio_wmb(vq->weak_barriers); |
| 1518 | } |
| 1519 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1520 | if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1521 | vq->packed.event_flags_shadow = vq->event ? |
| 1522 | VRING_PACKED_EVENT_FLAG_DESC : |
| 1523 | VRING_PACKED_EVENT_FLAG_ENABLE; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1524 | vq->packed.vring.driver->flags = |
| 1525 | cpu_to_le16(vq->packed.event_flags_shadow); |
| 1526 | } |
| 1527 | |
| 1528 | END_USE(vq); |
| 1529 | return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter << |
| 1530 | VRING_PACKED_EVENT_F_WRAP_CTR); |
| 1531 | } |
| 1532 | |
| 1533 | static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap) |
| 1534 | { |
| 1535 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1536 | bool wrap_counter; |
| 1537 | u16 used_idx; |
| 1538 | |
| 1539 | wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; |
| 1540 | used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); |
| 1541 | |
| 1542 | return is_used_desc_packed(vq, used_idx, wrap_counter); |
| 1543 | } |
| 1544 | |
| 1545 | static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq) |
| 1546 | { |
| 1547 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1548 | u16 used_idx, wrap_counter; |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1549 | u16 bufs; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1550 | |
| 1551 | START_USE(vq); |
| 1552 | |
| 1553 | /* |
| 1554 | * We optimistically turn back on interrupts, then check if there was |
| 1555 | * more to do. |
| 1556 | */ |
| 1557 | |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1558 | if (vq->event) { |
| 1559 | /* TODO: tune this threshold */ |
| 1560 | bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4; |
| 1561 | wrap_counter = vq->packed.used_wrap_counter; |
| 1562 | |
| 1563 | used_idx = vq->last_used_idx + bufs; |
| 1564 | if (used_idx >= vq->packed.vring.num) { |
| 1565 | used_idx -= vq->packed.vring.num; |
| 1566 | wrap_counter ^= 1; |
| 1567 | } |
| 1568 | |
| 1569 | vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx | |
| 1570 | (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR)); |
| 1571 | |
| 1572 | /* |
| 1573 | * We need to update event offset and event wrap |
| 1574 | * counter first before updating event flags. |
| 1575 | */ |
| 1576 | virtio_wmb(vq->weak_barriers); |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1577 | } |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1578 | |
| 1579 | if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { |
Tiwei Bie | f51f982 | 2018-11-21 18:03:28 +0800 | [diff] [blame] | 1580 | vq->packed.event_flags_shadow = vq->event ? |
| 1581 | VRING_PACKED_EVENT_FLAG_DESC : |
| 1582 | VRING_PACKED_EVENT_FLAG_ENABLE; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1583 | vq->packed.vring.driver->flags = |
| 1584 | cpu_to_le16(vq->packed.event_flags_shadow); |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * We need to update event suppression structure first |
| 1589 | * before re-checking for more used buffers. |
| 1590 | */ |
| 1591 | virtio_mb(vq->weak_barriers); |
| 1592 | |
Marvin Liu | 40ce791 | 2019-10-22 01:10:04 +0800 | [diff] [blame] | 1593 | if (is_used_desc_packed(vq, |
| 1594 | vq->last_used_idx, |
| 1595 | vq->packed.used_wrap_counter)) { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1596 | END_USE(vq); |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
| 1600 | END_USE(vq); |
| 1601 | return true; |
| 1602 | } |
| 1603 | |
| 1604 | static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq) |
| 1605 | { |
| 1606 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1607 | unsigned int i; |
| 1608 | void *buf; |
| 1609 | |
| 1610 | START_USE(vq); |
| 1611 | |
| 1612 | for (i = 0; i < vq->packed.vring.num; i++) { |
| 1613 | if (!vq->packed.desc_state[i].data) |
| 1614 | continue; |
| 1615 | /* detach_buf clears data, so grab it now. */ |
| 1616 | buf = vq->packed.desc_state[i].data; |
| 1617 | detach_buf_packed(vq, i, NULL); |
| 1618 | END_USE(vq); |
| 1619 | return buf; |
| 1620 | } |
| 1621 | /* That should have freed everything. */ |
| 1622 | BUG_ON(vq->vq.num_free != vq->packed.vring.num); |
| 1623 | |
| 1624 | END_USE(vq); |
| 1625 | return NULL; |
| 1626 | } |
| 1627 | |
Jason Wang | 5a22242 | 2021-06-04 13:53:46 +0800 | [diff] [blame] | 1628 | static struct vring_desc_extra *vring_alloc_desc_extra(struct vring_virtqueue *vq, |
| 1629 | unsigned int num) |
| 1630 | { |
| 1631 | struct vring_desc_extra *desc_extra; |
| 1632 | unsigned int i; |
| 1633 | |
| 1634 | desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra), |
| 1635 | GFP_KERNEL); |
| 1636 | if (!desc_extra) |
| 1637 | return NULL; |
| 1638 | |
| 1639 | memset(desc_extra, 0, num * sizeof(struct vring_desc_extra)); |
| 1640 | |
| 1641 | for (i = 0; i < num - 1; i++) |
| 1642 | desc_extra[i].next = i + 1; |
| 1643 | |
| 1644 | return desc_extra; |
| 1645 | } |
| 1646 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1647 | static struct virtqueue *vring_create_virtqueue_packed( |
| 1648 | unsigned int index, |
| 1649 | unsigned int num, |
| 1650 | unsigned int vring_align, |
| 1651 | struct virtio_device *vdev, |
| 1652 | bool weak_barriers, |
| 1653 | bool may_reduce_num, |
| 1654 | bool context, |
| 1655 | bool (*notify)(struct virtqueue *), |
| 1656 | void (*callback)(struct virtqueue *), |
| 1657 | const char *name) |
| 1658 | { |
| 1659 | struct vring_virtqueue *vq; |
| 1660 | struct vring_packed_desc *ring; |
| 1661 | struct vring_packed_desc_event *driver, *device; |
| 1662 | dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr; |
| 1663 | size_t ring_size_in_bytes, event_size_in_bytes; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1664 | |
| 1665 | ring_size_in_bytes = num * sizeof(struct vring_packed_desc); |
| 1666 | |
| 1667 | ring = vring_alloc_queue(vdev, ring_size_in_bytes, |
| 1668 | &ring_dma_addr, |
| 1669 | GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); |
| 1670 | if (!ring) |
| 1671 | goto err_ring; |
| 1672 | |
| 1673 | event_size_in_bytes = sizeof(struct vring_packed_desc_event); |
| 1674 | |
| 1675 | driver = vring_alloc_queue(vdev, event_size_in_bytes, |
| 1676 | &driver_event_dma_addr, |
| 1677 | GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); |
| 1678 | if (!driver) |
| 1679 | goto err_driver; |
| 1680 | |
| 1681 | device = vring_alloc_queue(vdev, event_size_in_bytes, |
| 1682 | &device_event_dma_addr, |
| 1683 | GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); |
| 1684 | if (!device) |
| 1685 | goto err_device; |
| 1686 | |
| 1687 | vq = kmalloc(sizeof(*vq), GFP_KERNEL); |
| 1688 | if (!vq) |
| 1689 | goto err_vq; |
| 1690 | |
| 1691 | vq->vq.callback = callback; |
| 1692 | vq->vq.vdev = vdev; |
| 1693 | vq->vq.name = name; |
| 1694 | vq->vq.num_free = num; |
| 1695 | vq->vq.index = index; |
| 1696 | vq->we_own_ring = true; |
| 1697 | vq->notify = notify; |
| 1698 | vq->weak_barriers = weak_barriers; |
| 1699 | vq->broken = false; |
| 1700 | vq->last_used_idx = 0; |
Michael S. Tsirkin | 8d622d2 | 2021-04-13 01:19:16 -0400 | [diff] [blame] | 1701 | vq->event_triggered = false; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1702 | vq->num_added = 0; |
| 1703 | vq->packed_ring = true; |
| 1704 | vq->use_dma_api = vring_use_dma_api(vdev); |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1705 | #ifdef DEBUG |
| 1706 | vq->in_use = false; |
| 1707 | vq->last_add_time_valid = false; |
| 1708 | #endif |
| 1709 | |
| 1710 | vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && |
| 1711 | !context; |
| 1712 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); |
| 1713 | |
Tiwei Bie | 45383fb | 2019-01-23 17:50:26 +0800 | [diff] [blame] | 1714 | if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) |
| 1715 | vq->weak_barriers = false; |
| 1716 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1717 | vq->packed.ring_dma_addr = ring_dma_addr; |
| 1718 | vq->packed.driver_event_dma_addr = driver_event_dma_addr; |
| 1719 | vq->packed.device_event_dma_addr = device_event_dma_addr; |
| 1720 | |
| 1721 | vq->packed.ring_size_in_bytes = ring_size_in_bytes; |
| 1722 | vq->packed.event_size_in_bytes = event_size_in_bytes; |
| 1723 | |
| 1724 | vq->packed.vring.num = num; |
| 1725 | vq->packed.vring.desc = ring; |
| 1726 | vq->packed.vring.driver = driver; |
| 1727 | vq->packed.vring.device = device; |
| 1728 | |
| 1729 | vq->packed.next_avail_idx = 0; |
| 1730 | vq->packed.avail_wrap_counter = 1; |
| 1731 | vq->packed.used_wrap_counter = 1; |
| 1732 | vq->packed.event_flags_shadow = 0; |
| 1733 | vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL; |
| 1734 | |
| 1735 | vq->packed.desc_state = kmalloc_array(num, |
| 1736 | sizeof(struct vring_desc_state_packed), |
| 1737 | GFP_KERNEL); |
| 1738 | if (!vq->packed.desc_state) |
| 1739 | goto err_desc_state; |
| 1740 | |
| 1741 | memset(vq->packed.desc_state, 0, |
| 1742 | num * sizeof(struct vring_desc_state_packed)); |
| 1743 | |
| 1744 | /* Put everything in free lists. */ |
| 1745 | vq->free_head = 0; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1746 | |
Jason Wang | 5a22242 | 2021-06-04 13:53:46 +0800 | [diff] [blame] | 1747 | vq->packed.desc_extra = vring_alloc_desc_extra(vq, num); |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1748 | if (!vq->packed.desc_extra) |
| 1749 | goto err_desc_extra; |
| 1750 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1751 | /* No callback? Tell other side not to bother us. */ |
| 1752 | if (!callback) { |
| 1753 | vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; |
| 1754 | vq->packed.vring.driver->flags = |
| 1755 | cpu_to_le16(vq->packed.event_flags_shadow); |
| 1756 | } |
| 1757 | |
Dan Carpenter | e152d8a | 2020-12-04 17:23:36 +0300 | [diff] [blame] | 1758 | list_add_tail(&vq->vq.list, &vdev->vqs); |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1759 | return &vq->vq; |
| 1760 | |
| 1761 | err_desc_extra: |
| 1762 | kfree(vq->packed.desc_state); |
| 1763 | err_desc_state: |
| 1764 | kfree(vq); |
| 1765 | err_vq: |
Dan Carpenter | ae93d8e | 2020-12-04 17:23:00 +0300 | [diff] [blame] | 1766 | vring_free_queue(vdev, event_size_in_bytes, device, device_event_dma_addr); |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1767 | err_device: |
Dan Carpenter | ae93d8e | 2020-12-04 17:23:00 +0300 | [diff] [blame] | 1768 | vring_free_queue(vdev, event_size_in_bytes, driver, driver_event_dma_addr); |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1769 | err_driver: |
| 1770 | vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr); |
| 1771 | err_ring: |
| 1772 | return NULL; |
| 1773 | } |
| 1774 | |
| 1775 | |
| 1776 | /* |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1777 | * Generic functions and exported symbols. |
| 1778 | */ |
| 1779 | |
| 1780 | static inline int virtqueue_add(struct virtqueue *_vq, |
| 1781 | struct scatterlist *sgs[], |
| 1782 | unsigned int total_sg, |
| 1783 | unsigned int out_sgs, |
| 1784 | unsigned int in_sgs, |
| 1785 | void *data, |
| 1786 | void *ctx, |
| 1787 | gfp_t gfp) |
| 1788 | { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1789 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1790 | |
| 1791 | return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg, |
| 1792 | out_sgs, in_sgs, data, ctx, gfp) : |
| 1793 | virtqueue_add_split(_vq, sgs, total_sg, |
| 1794 | out_sgs, in_sgs, data, ctx, gfp); |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * virtqueue_add_sgs - expose buffers to other end |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 1799 | * @_vq: the struct virtqueue we're talking about. |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1800 | * @sgs: array of terminated scatterlists. |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 1801 | * @out_sgs: the number of scatterlists readable by other side |
| 1802 | * @in_sgs: the number of scatterlists which are writable (after readable ones) |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1803 | * @data: the token identifying the buffer. |
| 1804 | * @gfp: how to do memory allocations (if necessary). |
| 1805 | * |
| 1806 | * Caller must ensure we don't call this with other virtqueue operations |
| 1807 | * at the same time (except where noted). |
| 1808 | * |
| 1809 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
| 1810 | */ |
| 1811 | int virtqueue_add_sgs(struct virtqueue *_vq, |
| 1812 | struct scatterlist *sgs[], |
| 1813 | unsigned int out_sgs, |
| 1814 | unsigned int in_sgs, |
| 1815 | void *data, |
| 1816 | gfp_t gfp) |
| 1817 | { |
| 1818 | unsigned int i, total_sg = 0; |
| 1819 | |
| 1820 | /* Count them first. */ |
| 1821 | for (i = 0; i < out_sgs + in_sgs; i++) { |
| 1822 | struct scatterlist *sg; |
| 1823 | |
| 1824 | for (sg = sgs[i]; sg; sg = sg_next(sg)) |
| 1825 | total_sg++; |
| 1826 | } |
| 1827 | return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, |
| 1828 | data, NULL, gfp); |
| 1829 | } |
| 1830 | EXPORT_SYMBOL_GPL(virtqueue_add_sgs); |
| 1831 | |
| 1832 | /** |
| 1833 | * virtqueue_add_outbuf - expose output buffers to other end |
| 1834 | * @vq: the struct virtqueue we're talking about. |
| 1835 | * @sg: scatterlist (must be well-formed and terminated!) |
| 1836 | * @num: the number of entries in @sg readable by other side |
| 1837 | * @data: the token identifying the buffer. |
| 1838 | * @gfp: how to do memory allocations (if necessary). |
| 1839 | * |
| 1840 | * Caller must ensure we don't call this with other virtqueue operations |
| 1841 | * at the same time (except where noted). |
| 1842 | * |
| 1843 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
| 1844 | */ |
| 1845 | int virtqueue_add_outbuf(struct virtqueue *vq, |
| 1846 | struct scatterlist *sg, unsigned int num, |
| 1847 | void *data, |
| 1848 | gfp_t gfp) |
| 1849 | { |
| 1850 | return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp); |
| 1851 | } |
| 1852 | EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); |
| 1853 | |
| 1854 | /** |
| 1855 | * virtqueue_add_inbuf - expose input buffers to other end |
| 1856 | * @vq: the struct virtqueue we're talking about. |
| 1857 | * @sg: scatterlist (must be well-formed and terminated!) |
| 1858 | * @num: the number of entries in @sg writable by other side |
| 1859 | * @data: the token identifying the buffer. |
| 1860 | * @gfp: how to do memory allocations (if necessary). |
| 1861 | * |
| 1862 | * Caller must ensure we don't call this with other virtqueue operations |
| 1863 | * at the same time (except where noted). |
| 1864 | * |
| 1865 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
| 1866 | */ |
| 1867 | int virtqueue_add_inbuf(struct virtqueue *vq, |
| 1868 | struct scatterlist *sg, unsigned int num, |
| 1869 | void *data, |
| 1870 | gfp_t gfp) |
| 1871 | { |
| 1872 | return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp); |
| 1873 | } |
| 1874 | EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); |
| 1875 | |
| 1876 | /** |
| 1877 | * virtqueue_add_inbuf_ctx - expose input buffers to other end |
| 1878 | * @vq: the struct virtqueue we're talking about. |
| 1879 | * @sg: scatterlist (must be well-formed and terminated!) |
| 1880 | * @num: the number of entries in @sg writable by other side |
| 1881 | * @data: the token identifying the buffer. |
| 1882 | * @ctx: extra context for the token |
| 1883 | * @gfp: how to do memory allocations (if necessary). |
| 1884 | * |
| 1885 | * Caller must ensure we don't call this with other virtqueue operations |
| 1886 | * at the same time (except where noted). |
| 1887 | * |
| 1888 | * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). |
| 1889 | */ |
| 1890 | int virtqueue_add_inbuf_ctx(struct virtqueue *vq, |
| 1891 | struct scatterlist *sg, unsigned int num, |
| 1892 | void *data, |
| 1893 | void *ctx, |
| 1894 | gfp_t gfp) |
| 1895 | { |
| 1896 | return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp); |
| 1897 | } |
| 1898 | EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx); |
| 1899 | |
| 1900 | /** |
| 1901 | * virtqueue_kick_prepare - first half of split virtqueue_kick call. |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 1902 | * @_vq: the struct virtqueue |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1903 | * |
| 1904 | * Instead of virtqueue_kick(), you can do: |
| 1905 | * if (virtqueue_kick_prepare(vq)) |
| 1906 | * virtqueue_notify(vq); |
| 1907 | * |
| 1908 | * This is sometimes useful because the virtqueue_kick_prepare() needs |
| 1909 | * to be serialized, but the actual virtqueue_notify() call does not. |
| 1910 | */ |
| 1911 | bool virtqueue_kick_prepare(struct virtqueue *_vq) |
| 1912 | { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1913 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1914 | |
| 1915 | return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) : |
| 1916 | virtqueue_kick_prepare_split(_vq); |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1917 | } |
| 1918 | EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); |
| 1919 | |
| 1920 | /** |
| 1921 | * virtqueue_notify - second half of split virtqueue_kick call. |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 1922 | * @_vq: the struct virtqueue |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1923 | * |
| 1924 | * This does not need to be serialized. |
| 1925 | * |
| 1926 | * Returns false if host notify failed or queue is broken, otherwise true. |
| 1927 | */ |
| 1928 | bool virtqueue_notify(struct virtqueue *_vq) |
| 1929 | { |
| 1930 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1931 | |
| 1932 | if (unlikely(vq->broken)) |
| 1933 | return false; |
| 1934 | |
| 1935 | /* Prod other side to tell it about changes. */ |
| 1936 | if (!vq->notify(_vq)) { |
| 1937 | vq->broken = true; |
| 1938 | return false; |
| 1939 | } |
| 1940 | return true; |
| 1941 | } |
| 1942 | EXPORT_SYMBOL_GPL(virtqueue_notify); |
| 1943 | |
| 1944 | /** |
| 1945 | * virtqueue_kick - update after add_buf |
| 1946 | * @vq: the struct virtqueue |
| 1947 | * |
| 1948 | * After one or more virtqueue_add_* calls, invoke this to kick |
| 1949 | * the other side. |
| 1950 | * |
| 1951 | * Caller must ensure we don't call this with other virtqueue |
| 1952 | * operations at the same time (except where noted). |
| 1953 | * |
| 1954 | * Returns false if kick failed, otherwise true. |
| 1955 | */ |
| 1956 | bool virtqueue_kick(struct virtqueue *vq) |
| 1957 | { |
| 1958 | if (virtqueue_kick_prepare(vq)) |
| 1959 | return virtqueue_notify(vq); |
| 1960 | return true; |
| 1961 | } |
| 1962 | EXPORT_SYMBOL_GPL(virtqueue_kick); |
| 1963 | |
| 1964 | /** |
Yang Li | 31c11db | 2021-05-26 11:12:11 +0800 | [diff] [blame] | 1965 | * virtqueue_get_buf_ctx - get the next used buffer |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 1966 | * @_vq: the struct virtqueue we're talking about. |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1967 | * @len: the length written into the buffer |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 1968 | * @ctx: extra context for the token |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1969 | * |
| 1970 | * If the device wrote data into the buffer, @len will be set to the |
| 1971 | * amount written. This means you don't need to clear the buffer |
| 1972 | * beforehand to ensure there's no data leakage in the case of short |
| 1973 | * writes. |
| 1974 | * |
| 1975 | * Caller must ensure we don't call this with other virtqueue |
| 1976 | * operations at the same time (except where noted). |
| 1977 | * |
| 1978 | * Returns NULL if there are no used buffers, or the "data" token |
| 1979 | * handed to virtqueue_add_*(). |
| 1980 | */ |
| 1981 | void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len, |
| 1982 | void **ctx) |
| 1983 | { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 1984 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 1985 | |
| 1986 | return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) : |
| 1987 | virtqueue_get_buf_ctx_split(_vq, len, ctx); |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1988 | } |
| 1989 | EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx); |
| 1990 | |
| 1991 | void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) |
| 1992 | { |
| 1993 | return virtqueue_get_buf_ctx(_vq, len, NULL); |
| 1994 | } |
| 1995 | EXPORT_SYMBOL_GPL(virtqueue_get_buf); |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1996 | /** |
| 1997 | * virtqueue_disable_cb - disable callbacks |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 1998 | * @_vq: the struct virtqueue we're talking about. |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 1999 | * |
| 2000 | * Note that this is not necessarily synchronous, hence unreliable and only |
| 2001 | * useful as an optimization. |
| 2002 | * |
| 2003 | * Unlike other operations, this need not be serialized. |
| 2004 | */ |
| 2005 | void virtqueue_disable_cb(struct virtqueue *_vq) |
| 2006 | { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2007 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2008 | |
Michael S. Tsirkin | 8d622d2 | 2021-04-13 01:19:16 -0400 | [diff] [blame] | 2009 | /* If device triggered an event already it won't trigger one again: |
| 2010 | * no need to disable. |
| 2011 | */ |
| 2012 | if (vq->event_triggered) |
| 2013 | return; |
| 2014 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2015 | if (vq->packed_ring) |
| 2016 | virtqueue_disable_cb_packed(_vq); |
| 2017 | else |
| 2018 | virtqueue_disable_cb_split(_vq); |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 2019 | } |
| 2020 | EXPORT_SYMBOL_GPL(virtqueue_disable_cb); |
| 2021 | |
| 2022 | /** |
| 2023 | * virtqueue_enable_cb_prepare - restart callbacks after disable_cb |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 2024 | * @_vq: the struct virtqueue we're talking about. |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 2025 | * |
| 2026 | * This re-enables callbacks; it returns current queue state |
| 2027 | * in an opaque unsigned value. This value should be later tested by |
| 2028 | * virtqueue_poll, to detect a possible race between the driver checking for |
| 2029 | * more work, and enabling callbacks. |
| 2030 | * |
| 2031 | * Caller must ensure we don't call this with other virtqueue |
| 2032 | * operations at the same time (except where noted). |
| 2033 | */ |
| 2034 | unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) |
| 2035 | { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2036 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2037 | |
Michael S. Tsirkin | 8d622d2 | 2021-04-13 01:19:16 -0400 | [diff] [blame] | 2038 | if (vq->event_triggered) |
| 2039 | vq->event_triggered = false; |
| 2040 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2041 | return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) : |
| 2042 | virtqueue_enable_cb_prepare_split(_vq); |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 2043 | } |
| 2044 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); |
| 2045 | |
| 2046 | /** |
| 2047 | * virtqueue_poll - query pending used buffers |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 2048 | * @_vq: the struct virtqueue we're talking about. |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 2049 | * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). |
| 2050 | * |
| 2051 | * Returns "true" if there are pending used buffers in the queue. |
| 2052 | * |
| 2053 | * This does not need to be serialized. |
| 2054 | */ |
| 2055 | bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) |
| 2056 | { |
| 2057 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2058 | |
Mao Wenan | 481a0d7 | 2020-08-02 15:44:09 +0800 | [diff] [blame] | 2059 | if (unlikely(vq->broken)) |
| 2060 | return false; |
| 2061 | |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 2062 | virtio_mb(vq->weak_barriers); |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2063 | return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) : |
| 2064 | virtqueue_poll_split(_vq, last_used_idx); |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 2065 | } |
| 2066 | EXPORT_SYMBOL_GPL(virtqueue_poll); |
| 2067 | |
| 2068 | /** |
| 2069 | * virtqueue_enable_cb - restart callbacks after disable_cb. |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 2070 | * @_vq: the struct virtqueue we're talking about. |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 2071 | * |
| 2072 | * This re-enables callbacks; it returns "false" if there are pending |
| 2073 | * buffers in the queue, to detect a possible race between the driver |
| 2074 | * checking for more work, and enabling callbacks. |
| 2075 | * |
| 2076 | * Caller must ensure we don't call this with other virtqueue |
| 2077 | * operations at the same time (except where noted). |
| 2078 | */ |
| 2079 | bool virtqueue_enable_cb(struct virtqueue *_vq) |
| 2080 | { |
| 2081 | unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); |
| 2082 | |
| 2083 | return !virtqueue_poll(_vq, last_used_idx); |
| 2084 | } |
| 2085 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb); |
| 2086 | |
| 2087 | /** |
| 2088 | * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 2089 | * @_vq: the struct virtqueue we're talking about. |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 2090 | * |
| 2091 | * This re-enables callbacks but hints to the other side to delay |
| 2092 | * interrupts until most of the available buffers have been processed; |
| 2093 | * it returns "false" if there are many pending buffers in the queue, |
| 2094 | * to detect a possible race between the driver checking for more work, |
| 2095 | * and enabling callbacks. |
| 2096 | * |
| 2097 | * Caller must ensure we don't call this with other virtqueue |
| 2098 | * operations at the same time (except where noted). |
| 2099 | */ |
| 2100 | bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) |
| 2101 | { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2102 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2103 | |
Michael S. Tsirkin | 8d622d2 | 2021-04-13 01:19:16 -0400 | [diff] [blame] | 2104 | if (vq->event_triggered) |
| 2105 | vq->event_triggered = false; |
| 2106 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2107 | return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) : |
| 2108 | virtqueue_enable_cb_delayed_split(_vq); |
Tiwei Bie | e6f633e | 2018-11-21 18:03:20 +0800 | [diff] [blame] | 2109 | } |
| 2110 | EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); |
| 2111 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 2112 | /** |
| 2113 | * virtqueue_detach_unused_buf - detach first unused buffer |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 2114 | * @_vq: the struct virtqueue we're talking about. |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 2115 | * |
| 2116 | * Returns NULL or the "data" token handed to virtqueue_add_*(). |
| 2117 | * This is not valid on an active queue; it is useful only for device |
| 2118 | * shutdown. |
| 2119 | */ |
| 2120 | void *virtqueue_detach_unused_buf(struct virtqueue *_vq) |
| 2121 | { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2122 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2123 | |
| 2124 | return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) : |
| 2125 | virtqueue_detach_unused_buf_split(_vq); |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 2126 | } |
Michael S. Tsirkin | 7c5e9ed | 2010-04-12 16:19:07 +0300 | [diff] [blame] | 2127 | EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); |
Shirley Ma | c021eac | 2010-01-18 19:15:23 +0530 | [diff] [blame] | 2128 | |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 2129 | static inline bool more_used(const struct vring_virtqueue *vq) |
| 2130 | { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2131 | return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq); |
Tiwei Bie | 138fd25 | 2018-11-21 18:03:19 +0800 | [diff] [blame] | 2132 | } |
| 2133 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2134 | irqreturn_t vring_interrupt(int irq, void *_vq) |
| 2135 | { |
| 2136 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2137 | |
| 2138 | if (!more_used(vq)) { |
| 2139 | pr_debug("virtqueue interrupt with no work for %p\n", vq); |
| 2140 | return IRQ_NONE; |
| 2141 | } |
| 2142 | |
| 2143 | if (unlikely(vq->broken)) |
| 2144 | return IRQ_HANDLED; |
| 2145 | |
Michael S. Tsirkin | 8d622d2 | 2021-04-13 01:19:16 -0400 | [diff] [blame] | 2146 | /* Just a hint for performance: so it's ok that this can be racy! */ |
| 2147 | if (vq->event) |
| 2148 | vq->event_triggered = true; |
| 2149 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2150 | pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 2151 | if (vq->vq.callback) |
| 2152 | vq->vq.callback(&vq->vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2153 | |
| 2154 | return IRQ_HANDLED; |
| 2155 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 2156 | EXPORT_SYMBOL_GPL(vring_interrupt); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2157 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2158 | /* Only available for split ring */ |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2159 | struct virtqueue *__vring_new_virtqueue(unsigned int index, |
| 2160 | struct vring vring, |
| 2161 | struct virtio_device *vdev, |
| 2162 | bool weak_barriers, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 2163 | bool context, |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2164 | bool (*notify)(struct virtqueue *), |
| 2165 | void (*callback)(struct virtqueue *), |
| 2166 | const char *name) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2167 | { |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2168 | struct vring_virtqueue *vq; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2169 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2170 | if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) |
| 2171 | return NULL; |
| 2172 | |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 2173 | vq = kmalloc(sizeof(*vq), GFP_KERNEL); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2174 | if (!vq) |
| 2175 | return NULL; |
| 2176 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2177 | vq->packed_ring = false; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2178 | vq->vq.callback = callback; |
| 2179 | vq->vq.vdev = vdev; |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 2180 | vq->vq.name = name; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2181 | vq->vq.num_free = vring.num; |
Rusty Russell | 06ca287 | 2012-10-16 23:56:14 +1030 | [diff] [blame] | 2182 | vq->vq.index = index; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2183 | vq->we_own_ring = false; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2184 | vq->notify = notify; |
Rusty Russell | 7b21e34 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 2185 | vq->weak_barriers = weak_barriers; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2186 | vq->broken = false; |
| 2187 | vq->last_used_idx = 0; |
Michael S. Tsirkin | 8d622d2 | 2021-04-13 01:19:16 -0400 | [diff] [blame] | 2188 | vq->event_triggered = false; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2189 | vq->num_added = 0; |
Tiwei Bie | fb3fba6 | 2018-11-21 18:03:26 +0800 | [diff] [blame] | 2190 | vq->use_dma_api = vring_use_dma_api(vdev); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2191 | #ifdef DEBUG |
| 2192 | vq->in_use = false; |
Rusty Russell | e93300b | 2012-01-12 15:44:43 +1030 | [diff] [blame] | 2193 | vq->last_add_time_valid = false; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2194 | #endif |
| 2195 | |
Michael S. Tsirkin | 5a08b04 | 2017-02-07 06:15:13 +0200 | [diff] [blame] | 2196 | vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && |
| 2197 | !context; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 2198 | vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 2199 | |
Tiwei Bie | 45383fb | 2019-01-23 17:50:26 +0800 | [diff] [blame] | 2200 | if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) |
| 2201 | vq->weak_barriers = false; |
| 2202 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame] | 2203 | vq->split.queue_dma_addr = 0; |
| 2204 | vq->split.queue_size_in_bytes = 0; |
| 2205 | |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 2206 | vq->split.vring = vring; |
| 2207 | vq->split.avail_flags_shadow = 0; |
| 2208 | vq->split.avail_idx_shadow = 0; |
| 2209 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2210 | /* No callback? Tell other side not to bother us. */ |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 2211 | if (!callback) { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 2212 | vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; |
Ladi Prosek | 0ea1e4a | 2016-08-31 14:00:04 +0200 | [diff] [blame] | 2213 | if (!vq->event) |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 2214 | vq->split.vring.avail->flags = cpu_to_virtio16(vdev, |
| 2215 | vq->split.avail_flags_shadow); |
Venkatesh Srinivas | f277ec4 | 2015-11-10 16:21:07 -0800 | [diff] [blame] | 2216 | } |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2217 | |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 2218 | vq->split.desc_state = kmalloc_array(vring.num, |
| 2219 | sizeof(struct vring_desc_state_split), GFP_KERNEL); |
Jason Wang | 5bc7223 | 2021-06-04 13:53:49 +0800 | [diff] [blame] | 2220 | if (!vq->split.desc_state) |
| 2221 | goto err_state; |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 2222 | |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 2223 | vq->split.desc_extra = vring_alloc_desc_extra(vq, vring.num); |
| 2224 | if (!vq->split.desc_extra) |
| 2225 | goto err_extra; |
| 2226 | |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2227 | /* Put everything in free lists. */ |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2228 | vq->free_head = 0; |
Tiwei Bie | cbeedb7 | 2018-11-21 18:03:24 +0800 | [diff] [blame] | 2229 | memset(vq->split.desc_state, 0, vring.num * |
| 2230 | sizeof(struct vring_desc_state_split)); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2231 | |
Dan Carpenter | e152d8a | 2020-12-04 17:23:36 +0300 | [diff] [blame] | 2232 | list_add_tail(&vq->vq.list, &vdev->vqs); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2233 | return &vq->vq; |
Jason Wang | 5bc7223 | 2021-06-04 13:53:49 +0800 | [diff] [blame] | 2234 | |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 2235 | err_extra: |
| 2236 | kfree(vq->split.desc_state); |
Jason Wang | 5bc7223 | 2021-06-04 13:53:49 +0800 | [diff] [blame] | 2237 | err_state: |
| 2238 | kfree(vq); |
| 2239 | return NULL; |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2240 | } |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2241 | EXPORT_SYMBOL_GPL(__vring_new_virtqueue); |
| 2242 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2243 | struct virtqueue *vring_create_virtqueue( |
| 2244 | unsigned int index, |
| 2245 | unsigned int num, |
| 2246 | unsigned int vring_align, |
| 2247 | struct virtio_device *vdev, |
| 2248 | bool weak_barriers, |
| 2249 | bool may_reduce_num, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 2250 | bool context, |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2251 | bool (*notify)(struct virtqueue *), |
| 2252 | void (*callback)(struct virtqueue *), |
| 2253 | const char *name) |
| 2254 | { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2255 | |
| 2256 | if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) |
| 2257 | return vring_create_virtqueue_packed(index, num, vring_align, |
| 2258 | vdev, weak_barriers, may_reduce_num, |
| 2259 | context, notify, callback, name); |
| 2260 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame] | 2261 | return vring_create_virtqueue_split(index, num, vring_align, |
| 2262 | vdev, weak_barriers, may_reduce_num, |
| 2263 | context, notify, callback, name); |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2264 | } |
| 2265 | EXPORT_SYMBOL_GPL(vring_create_virtqueue); |
| 2266 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2267 | /* Only available for split ring */ |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2268 | struct virtqueue *vring_new_virtqueue(unsigned int index, |
| 2269 | unsigned int num, |
| 2270 | unsigned int vring_align, |
| 2271 | struct virtio_device *vdev, |
| 2272 | bool weak_barriers, |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 2273 | bool context, |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2274 | void *pages, |
| 2275 | bool (*notify)(struct virtqueue *vq), |
| 2276 | void (*callback)(struct virtqueue *vq), |
| 2277 | const char *name) |
| 2278 | { |
| 2279 | struct vring vring; |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2280 | |
| 2281 | if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) |
| 2282 | return NULL; |
| 2283 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2284 | vring_init(&vring, num, pages, vring_align); |
Michael S. Tsirkin | f94682d | 2017-03-06 18:32:29 +0200 | [diff] [blame] | 2285 | return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2286 | notify, callback, name); |
| 2287 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 2288 | EXPORT_SYMBOL_GPL(vring_new_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2289 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2290 | void vring_del_virtqueue(struct virtqueue *_vq) |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2291 | { |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2292 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2293 | |
| 2294 | if (vq->we_own_ring) { |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2295 | if (vq->packed_ring) { |
| 2296 | vring_free_queue(vq->vq.vdev, |
| 2297 | vq->packed.ring_size_in_bytes, |
| 2298 | vq->packed.vring.desc, |
| 2299 | vq->packed.ring_dma_addr); |
| 2300 | |
| 2301 | vring_free_queue(vq->vq.vdev, |
| 2302 | vq->packed.event_size_in_bytes, |
| 2303 | vq->packed.vring.driver, |
| 2304 | vq->packed.driver_event_dma_addr); |
| 2305 | |
| 2306 | vring_free_queue(vq->vq.vdev, |
| 2307 | vq->packed.event_size_in_bytes, |
| 2308 | vq->packed.vring.device, |
| 2309 | vq->packed.device_event_dma_addr); |
| 2310 | |
| 2311 | kfree(vq->packed.desc_state); |
| 2312 | kfree(vq->packed.desc_extra); |
| 2313 | } else { |
| 2314 | vring_free_queue(vq->vq.vdev, |
| 2315 | vq->split.queue_size_in_bytes, |
| 2316 | vq->split.vring.desc, |
| 2317 | vq->split.queue_dma_addr); |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2318 | } |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2319 | } |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 2320 | if (!vq->packed_ring) { |
Suman Anna | f13f09a | 2020-02-24 15:26:43 -0600 | [diff] [blame] | 2321 | kfree(vq->split.desc_state); |
Jason Wang | 72b5e89 | 2021-06-04 13:53:50 +0800 | [diff] [blame] | 2322 | kfree(vq->split.desc_extra); |
| 2323 | } |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2324 | list_del(&_vq->list); |
| 2325 | kfree(vq); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2326 | } |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 2327 | EXPORT_SYMBOL_GPL(vring_del_virtqueue); |
Rusty Russell | 0a8a69d | 2007-10-22 11:03:40 +1000 | [diff] [blame] | 2328 | |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 2329 | /* Manipulates transport-specific feature bits. */ |
| 2330 | void vring_transport_features(struct virtio_device *vdev) |
| 2331 | { |
| 2332 | unsigned int i; |
| 2333 | |
| 2334 | for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { |
| 2335 | switch (i) { |
Mark McLoughlin | 9fa29b9d | 2009-05-11 18:11:45 +0100 | [diff] [blame] | 2336 | case VIRTIO_RING_F_INDIRECT_DESC: |
| 2337 | break; |
Michael S. Tsirkin | a5c262c | 2011-05-20 02:10:44 +0300 | [diff] [blame] | 2338 | case VIRTIO_RING_F_EVENT_IDX: |
| 2339 | break; |
Michael S. Tsirkin | 747ae34 | 2014-12-01 15:52:40 +0200 | [diff] [blame] | 2340 | case VIRTIO_F_VERSION_1: |
| 2341 | break; |
Michael S. Tsirkin | 321bd21 | 2020-06-24 18:24:33 -0400 | [diff] [blame] | 2342 | case VIRTIO_F_ACCESS_PLATFORM: |
Michael S. Tsirkin | 1a93769 | 2016-04-18 12:58:14 +0300 | [diff] [blame] | 2343 | break; |
Tiwei Bie | f959a12 | 2018-11-21 18:03:30 +0800 | [diff] [blame] | 2344 | case VIRTIO_F_RING_PACKED: |
| 2345 | break; |
Tiwei Bie | 45383fb | 2019-01-23 17:50:26 +0800 | [diff] [blame] | 2346 | case VIRTIO_F_ORDER_PLATFORM: |
| 2347 | break; |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 2348 | default: |
| 2349 | /* We don't understand this bit. */ |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 2350 | __virtio_clear_bit(vdev, i); |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 2351 | } |
| 2352 | } |
| 2353 | } |
| 2354 | EXPORT_SYMBOL_GPL(vring_transport_features); |
| 2355 | |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 2356 | /** |
| 2357 | * virtqueue_get_vring_size - return the size of the virtqueue's vring |
Jiang Biao | a558120 | 2019-04-23 18:25:12 +0800 | [diff] [blame] | 2358 | * @_vq: the struct virtqueue containing the vring of interest. |
Rusty Russell | 5dfc176 | 2012-01-12 15:44:42 +1030 | [diff] [blame] | 2359 | * |
| 2360 | * Returns the size of the vring. This is mainly used for boasting to |
| 2361 | * userspace. Unlike other operations, this need not be serialized. |
| 2362 | */ |
Rick Jones | 8f9f466 | 2011-10-19 08:10:59 +0000 | [diff] [blame] | 2363 | unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) |
| 2364 | { |
| 2365 | |
| 2366 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2367 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2368 | return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num; |
Rick Jones | 8f9f466 | 2011-10-19 08:10:59 +0000 | [diff] [blame] | 2369 | } |
| 2370 | EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); |
| 2371 | |
Heinz Graalfs | b3b32c9 | 2013-10-29 09:40:19 +1030 | [diff] [blame] | 2372 | bool virtqueue_is_broken(struct virtqueue *_vq) |
| 2373 | { |
| 2374 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2375 | |
| 2376 | return vq->broken; |
| 2377 | } |
| 2378 | EXPORT_SYMBOL_GPL(virtqueue_is_broken); |
| 2379 | |
Rusty Russell | e2dcdfe | 2014-04-28 11:15:08 +0930 | [diff] [blame] | 2380 | /* |
| 2381 | * This should prevent the device from being used, allowing drivers to |
| 2382 | * recover. You may need to grab appropriate locks to flush. |
| 2383 | */ |
| 2384 | void virtio_break_device(struct virtio_device *dev) |
| 2385 | { |
| 2386 | struct virtqueue *_vq; |
| 2387 | |
| 2388 | list_for_each_entry(_vq, &dev->vqs, list) { |
| 2389 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2390 | vq->broken = true; |
| 2391 | } |
| 2392 | } |
| 2393 | EXPORT_SYMBOL_GPL(virtio_break_device); |
| 2394 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2395 | dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq) |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 2396 | { |
| 2397 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2398 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2399 | BUG_ON(!vq->we_own_ring); |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 2400 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2401 | if (vq->packed_ring) |
| 2402 | return vq->packed.ring_dma_addr; |
| 2403 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame] | 2404 | return vq->split.queue_dma_addr; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2405 | } |
| 2406 | EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr); |
| 2407 | |
| 2408 | dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq) |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 2409 | { |
| 2410 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2411 | |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2412 | BUG_ON(!vq->we_own_ring); |
| 2413 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2414 | if (vq->packed_ring) |
| 2415 | return vq->packed.driver_event_dma_addr; |
| 2416 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame] | 2417 | return vq->split.queue_dma_addr + |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 2418 | ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc); |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 2419 | } |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2420 | EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr); |
| 2421 | |
| 2422 | dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq) |
| 2423 | { |
| 2424 | struct vring_virtqueue *vq = to_vvq(_vq); |
| 2425 | |
| 2426 | BUG_ON(!vq->we_own_ring); |
| 2427 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2428 | if (vq->packed_ring) |
| 2429 | return vq->packed.device_event_dma_addr; |
| 2430 | |
Tiwei Bie | d79dca7 | 2018-11-21 18:03:25 +0800 | [diff] [blame] | 2431 | return vq->split.queue_dma_addr + |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 2432 | ((char *)vq->split.vring.used - (char *)vq->split.vring.desc); |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2433 | } |
| 2434 | EXPORT_SYMBOL_GPL(virtqueue_get_used_addr); |
| 2435 | |
Tiwei Bie | 1ce9e60 | 2018-11-21 18:03:27 +0800 | [diff] [blame] | 2436 | /* Only available for split ring */ |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2437 | const struct vring *virtqueue_get_vring(struct virtqueue *vq) |
| 2438 | { |
Tiwei Bie | e593bf9 | 2018-11-21 18:03:21 +0800 | [diff] [blame] | 2439 | return &to_vvq(vq)->split.vring; |
Andy Lutomirski | 2a2d138 | 2016-02-02 21:46:37 -0800 | [diff] [blame] | 2440 | } |
| 2441 | EXPORT_SYMBOL_GPL(virtqueue_get_vring); |
Cornelia Huck | 8906265 | 2014-10-07 16:39:47 +0200 | [diff] [blame] | 2442 | |
Rusty Russell | c6fd470 | 2008-02-04 23:50:05 -0500 | [diff] [blame] | 2443 | MODULE_LICENSE("GPL"); |