1fd534e9bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 20a8a69ddSRusty Russell /* Virtio ring implementation. 30a8a69ddSRusty Russell * 40a8a69ddSRusty Russell * Copyright 2007 Rusty Russell IBM Corporation 50a8a69ddSRusty Russell */ 60a8a69ddSRusty Russell #include <linux/virtio.h> 70a8a69ddSRusty Russell #include <linux/virtio_ring.h> 8e34f8725SRusty Russell #include <linux/virtio_config.h> 90a8a69ddSRusty Russell #include <linux/device.h> 105a0e3ad6STejun Heo #include <linux/slab.h> 11b5a2c4f1SPaul Gortmaker #include <linux/module.h> 12e93300b1SRusty Russell #include <linux/hrtimer.h> 13780bc790SAndy Lutomirski #include <linux/dma-mapping.h> 14f8ce7263SMichael S. Tsirkin #include <linux/spinlock.h> 1578fe3987SAndy Lutomirski #include <xen/xen.h> 160a8a69ddSRusty Russell 170a8a69ddSRusty Russell #ifdef DEBUG 180a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */ 199499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 209499f5e7SRusty Russell do { \ 219499f5e7SRusty Russell dev_err(&(_vq)->vq.vdev->dev, \ 229499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 239499f5e7SRusty Russell BUG(); \ 249499f5e7SRusty Russell } while (0) 25c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */ 263a35ce7dSRoel Kluin #define START_USE(_vq) \ 27c5f841f1SRusty Russell do { \ 28c5f841f1SRusty Russell if ((_vq)->in_use) \ 299499f5e7SRusty Russell panic("%s:in_use = %i\n", \ 309499f5e7SRusty Russell (_vq)->vq.name, (_vq)->in_use); \ 31c5f841f1SRusty Russell (_vq)->in_use = __LINE__; \ 32c5f841f1SRusty Russell } while (0) 333a35ce7dSRoel Kluin #define END_USE(_vq) \ 3497a545abSRusty Russell do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) 354d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(_vq) \ 364d6a105eSTiwei Bie do { \ 374d6a105eSTiwei Bie ktime_t now = ktime_get(); \ 384d6a105eSTiwei Bie \ 394d6a105eSTiwei Bie /* No kick or get, with .1 second between? Warn. */ \ 404d6a105eSTiwei Bie if ((_vq)->last_add_time_valid) \ 414d6a105eSTiwei Bie WARN_ON(ktime_to_ms(ktime_sub(now, \ 424d6a105eSTiwei Bie (_vq)->last_add_time)) > 100); \ 434d6a105eSTiwei Bie (_vq)->last_add_time = now; \ 444d6a105eSTiwei Bie (_vq)->last_add_time_valid = true; \ 454d6a105eSTiwei Bie } while (0) 464d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(_vq) \ 474d6a105eSTiwei Bie do { \ 484d6a105eSTiwei Bie if ((_vq)->last_add_time_valid) { \ 494d6a105eSTiwei Bie WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \ 504d6a105eSTiwei Bie (_vq)->last_add_time)) > 100); \ 514d6a105eSTiwei Bie } \ 524d6a105eSTiwei Bie } while (0) 534d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(_vq) \ 544d6a105eSTiwei Bie ((_vq)->last_add_time_valid = false) 550a8a69ddSRusty Russell #else 569499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 579499f5e7SRusty Russell do { \ 589499f5e7SRusty Russell dev_err(&_vq->vq.vdev->dev, \ 599499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 609499f5e7SRusty Russell (_vq)->broken = true; \ 619499f5e7SRusty Russell } while (0) 620a8a69ddSRusty Russell #define START_USE(vq) 630a8a69ddSRusty Russell #define END_USE(vq) 644d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(vq) 654d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(vq) 664d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(vq) 670a8a69ddSRusty Russell #endif 680a8a69ddSRusty Russell 69cbeedb72STiwei Bie struct vring_desc_state_split { 70780bc790SAndy Lutomirski void *data; /* Data for callback. */ 71780bc790SAndy Lutomirski struct vring_desc *indir_desc; /* Indirect descriptor, if any. */ 72780bc790SAndy Lutomirski }; 73780bc790SAndy Lutomirski 741ce9e605STiwei Bie struct vring_desc_state_packed { 751ce9e605STiwei Bie void *data; /* Data for callback. */ 761ce9e605STiwei Bie struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */ 771ce9e605STiwei Bie u16 num; /* Descriptor list length. */ 781ce9e605STiwei Bie u16 last; /* The last desc state in a list. */ 791ce9e605STiwei Bie }; 801ce9e605STiwei Bie 811f28750fSJason Wang struct vring_desc_extra { 82ef5c366fSJason Wang dma_addr_t addr; /* Descriptor DMA addr. */ 83ef5c366fSJason Wang u32 len; /* Descriptor length. */ 841ce9e605STiwei Bie u16 flags; /* Descriptor flags. */ 85aeef9b47SJason Wang u16 next; /* The next desc state in a list. */ 861ce9e605STiwei Bie }; 871ce9e605STiwei Bie 88d76136e4SXuan Zhuo struct vring_virtqueue_split { 89d76136e4SXuan Zhuo /* Actual memory layout for this queue. */ 90d76136e4SXuan Zhuo struct vring vring; 91d76136e4SXuan Zhuo 92d76136e4SXuan Zhuo /* Last written value to avail->flags */ 93d76136e4SXuan Zhuo u16 avail_flags_shadow; 94d76136e4SXuan Zhuo 95d76136e4SXuan Zhuo /* 96d76136e4SXuan Zhuo * Last written value to avail->idx in 97d76136e4SXuan Zhuo * guest byte order. 98d76136e4SXuan Zhuo */ 99d76136e4SXuan Zhuo u16 avail_idx_shadow; 100d76136e4SXuan Zhuo 101d76136e4SXuan Zhuo /* Per-descriptor state. */ 102d76136e4SXuan Zhuo struct vring_desc_state_split *desc_state; 103d76136e4SXuan Zhuo struct vring_desc_extra *desc_extra; 104d76136e4SXuan Zhuo 105d76136e4SXuan Zhuo /* DMA address and size information */ 106d76136e4SXuan Zhuo dma_addr_t queue_dma_addr; 107d76136e4SXuan Zhuo size_t queue_size_in_bytes; 108af36b16fSXuan Zhuo 109af36b16fSXuan Zhuo /* 110af36b16fSXuan Zhuo * The parameters for creating vrings are reserved for creating new 111af36b16fSXuan Zhuo * vring. 112af36b16fSXuan Zhuo */ 113af36b16fSXuan Zhuo u32 vring_align; 114af36b16fSXuan Zhuo bool may_reduce_num; 115d76136e4SXuan Zhuo }; 116d76136e4SXuan Zhuo 117d76136e4SXuan Zhuo struct vring_virtqueue_packed { 118d76136e4SXuan Zhuo /* Actual memory layout for this queue. */ 119d76136e4SXuan Zhuo struct { 120d76136e4SXuan Zhuo unsigned int num; 121d76136e4SXuan Zhuo struct vring_packed_desc *desc; 122d76136e4SXuan Zhuo struct vring_packed_desc_event *driver; 123d76136e4SXuan Zhuo struct vring_packed_desc_event *device; 124d76136e4SXuan Zhuo } vring; 125d76136e4SXuan Zhuo 126d76136e4SXuan Zhuo /* Driver ring wrap counter. */ 127d76136e4SXuan Zhuo bool avail_wrap_counter; 128d76136e4SXuan Zhuo 129d76136e4SXuan Zhuo /* Avail used flags. */ 130d76136e4SXuan Zhuo u16 avail_used_flags; 131d76136e4SXuan Zhuo 132d76136e4SXuan Zhuo /* Index of the next avail descriptor. */ 133d76136e4SXuan Zhuo u16 next_avail_idx; 134d76136e4SXuan Zhuo 135d76136e4SXuan Zhuo /* 136d76136e4SXuan Zhuo * Last written value to driver->flags in 137d76136e4SXuan Zhuo * guest byte order. 138d76136e4SXuan Zhuo */ 139d76136e4SXuan Zhuo u16 event_flags_shadow; 140d76136e4SXuan Zhuo 141d76136e4SXuan Zhuo /* Per-descriptor state. */ 142d76136e4SXuan Zhuo struct vring_desc_state_packed *desc_state; 143d76136e4SXuan Zhuo struct vring_desc_extra *desc_extra; 144d76136e4SXuan Zhuo 145d76136e4SXuan Zhuo /* DMA address and size information */ 146d76136e4SXuan Zhuo dma_addr_t ring_dma_addr; 147d76136e4SXuan Zhuo dma_addr_t driver_event_dma_addr; 148d76136e4SXuan Zhuo dma_addr_t device_event_dma_addr; 149d76136e4SXuan Zhuo size_t ring_size_in_bytes; 150d76136e4SXuan Zhuo size_t event_size_in_bytes; 151d76136e4SXuan Zhuo }; 152d76136e4SXuan Zhuo 15343b4f721SMichael S. Tsirkin struct vring_virtqueue { 1540a8a69ddSRusty Russell struct virtqueue vq; 1550a8a69ddSRusty Russell 1561ce9e605STiwei Bie /* Is this a packed ring? */ 1571ce9e605STiwei Bie bool packed_ring; 1581ce9e605STiwei Bie 159fb3fba6bSTiwei Bie /* Is DMA API used? */ 160fb3fba6bSTiwei Bie bool use_dma_api; 161fb3fba6bSTiwei Bie 1627b21e34fSRusty Russell /* Can we use weak barriers? */ 1637b21e34fSRusty Russell bool weak_barriers; 1647b21e34fSRusty Russell 1650a8a69ddSRusty Russell /* Other side has made a mess, don't try any more. */ 1660a8a69ddSRusty Russell bool broken; 1670a8a69ddSRusty Russell 1689fa29b9dSMark McLoughlin /* Host supports indirect buffers */ 1699fa29b9dSMark McLoughlin bool indirect; 1709fa29b9dSMark McLoughlin 171a5c262c5SMichael S. Tsirkin /* Host publishes avail event idx */ 172a5c262c5SMichael S. Tsirkin bool event; 173a5c262c5SMichael S. Tsirkin 1740a8a69ddSRusty Russell /* Head of free buffer list. */ 1750a8a69ddSRusty Russell unsigned int free_head; 1760a8a69ddSRusty Russell /* Number we've added since last sync. */ 1770a8a69ddSRusty Russell unsigned int num_added; 1780a8a69ddSRusty Russell 179a7722890Shuangjie.albert /* Last used index we've seen. 180a7722890Shuangjie.albert * for split ring, it just contains last used index 181a7722890Shuangjie.albert * for packed ring: 182a7722890Shuangjie.albert * bits up to VRING_PACKED_EVENT_F_WRAP_CTR include the last used index. 183a7722890Shuangjie.albert * bits from VRING_PACKED_EVENT_F_WRAP_CTR include the used wrap counter. 184a7722890Shuangjie.albert */ 1851bc4953eSAnthony Liguori u16 last_used_idx; 1860a8a69ddSRusty Russell 1878d622d21SMichael S. Tsirkin /* Hint for event idx: already triggered no need to disable. */ 1888d622d21SMichael S. Tsirkin bool event_triggered; 1898d622d21SMichael S. Tsirkin 1901ce9e605STiwei Bie union { 1911ce9e605STiwei Bie /* Available for split ring */ 192d76136e4SXuan Zhuo struct vring_virtqueue_split split; 193f277ec42SVenkatesh Srinivas 1941ce9e605STiwei Bie /* Available for packed ring */ 195d76136e4SXuan Zhuo struct vring_virtqueue_packed packed; 1961ce9e605STiwei Bie }; 1971ce9e605STiwei Bie 1980a8a69ddSRusty Russell /* How to notify other side. FIXME: commonalize hcalls! */ 19946f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *vq); 2000a8a69ddSRusty Russell 2012a2d1382SAndy Lutomirski /* DMA, allocation, and size information */ 2022a2d1382SAndy Lutomirski bool we_own_ring; 2032a2d1382SAndy Lutomirski 2040a8a69ddSRusty Russell #ifdef DEBUG 2050a8a69ddSRusty Russell /* They're supposed to lock for us. */ 2060a8a69ddSRusty Russell unsigned int in_use; 207e93300b1SRusty Russell 208e93300b1SRusty Russell /* Figure out if their kicks are too delayed. */ 209e93300b1SRusty Russell bool last_add_time_valid; 210e93300b1SRusty Russell ktime_t last_add_time; 2110a8a69ddSRusty Russell #endif 2120a8a69ddSRusty Russell }; 2130a8a69ddSRusty Russell 21407d9629dSXuan Zhuo static struct virtqueue *__vring_new_virtqueue(unsigned int index, 215cd4c812aSXuan Zhuo struct vring_virtqueue_split *vring_split, 21607d9629dSXuan Zhuo struct virtio_device *vdev, 21707d9629dSXuan Zhuo bool weak_barriers, 21807d9629dSXuan Zhuo bool context, 21907d9629dSXuan Zhuo bool (*notify)(struct virtqueue *), 22007d9629dSXuan Zhuo void (*callback)(struct virtqueue *), 22107d9629dSXuan Zhuo const char *name); 222a2b36c8dSXuan Zhuo static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num); 2236fea20e5SXuan Zhuo static void vring_free(struct virtqueue *_vq); 224e6f633e5STiwei Bie 225e6f633e5STiwei Bie /* 226e6f633e5STiwei Bie * Helpers. 227e6f633e5STiwei Bie */ 228e6f633e5STiwei Bie 2290a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) 2300a8a69ddSRusty Russell 23135c51e09SXianting Tian static inline bool virtqueue_use_indirect(struct vring_virtqueue *vq, 2322f18c2d1STiwei Bie unsigned int total_sg) 2332f18c2d1STiwei Bie { 2342f18c2d1STiwei Bie /* 2352f18c2d1STiwei Bie * If the host supports indirect descriptor tables, and we have multiple 2362f18c2d1STiwei Bie * buffers, then go indirect. FIXME: tune this threshold 2372f18c2d1STiwei Bie */ 2382f18c2d1STiwei Bie return (vq->indirect && total_sg > 1 && vq->vq.num_free); 2392f18c2d1STiwei Bie } 2402f18c2d1STiwei Bie 241d26c96c8SAndy Lutomirski /* 2421a937693SMichael S. Tsirkin * Modern virtio devices have feature bits to specify whether they need a 2431a937693SMichael S. Tsirkin * quirk and bypass the IOMMU. If not there, just use the DMA API. 2441a937693SMichael S. Tsirkin * 2451a937693SMichael S. Tsirkin * If there, the interaction between virtio and DMA API is messy. 246d26c96c8SAndy Lutomirski * 247d26c96c8SAndy Lutomirski * On most systems with virtio, physical addresses match bus addresses, 248d26c96c8SAndy Lutomirski * and it doesn't particularly matter whether we use the DMA API. 249d26c96c8SAndy Lutomirski * 250d26c96c8SAndy Lutomirski * On some systems, including Xen and any system with a physical device 251d26c96c8SAndy Lutomirski * that speaks virtio behind a physical IOMMU, we must use the DMA API 252d26c96c8SAndy Lutomirski * for virtio DMA to work at all. 253d26c96c8SAndy Lutomirski * 254d26c96c8SAndy Lutomirski * On other systems, including SPARC and PPC64, virtio-pci devices are 255d26c96c8SAndy Lutomirski * enumerated as though they are behind an IOMMU, but the virtio host 256d26c96c8SAndy Lutomirski * ignores the IOMMU, so we must either pretend that the IOMMU isn't 257d26c96c8SAndy Lutomirski * there or somehow map everything as the identity. 258d26c96c8SAndy Lutomirski * 259d26c96c8SAndy Lutomirski * For the time being, we preserve historic behavior and bypass the DMA 260d26c96c8SAndy Lutomirski * API. 2611a937693SMichael S. Tsirkin * 2621a937693SMichael S. Tsirkin * TODO: install a per-device DMA ops structure that does the right thing 2631a937693SMichael S. Tsirkin * taking into account all the above quirks, and use the DMA API 2641a937693SMichael S. Tsirkin * unconditionally on data path. 265d26c96c8SAndy Lutomirski */ 266d26c96c8SAndy Lutomirski 267d26c96c8SAndy Lutomirski static bool vring_use_dma_api(struct virtio_device *vdev) 268d26c96c8SAndy Lutomirski { 26924b6842aSMichael S. Tsirkin if (!virtio_has_dma_quirk(vdev)) 2701a937693SMichael S. Tsirkin return true; 2711a937693SMichael S. Tsirkin 2721a937693SMichael S. Tsirkin /* Otherwise, we are left to guess. */ 27378fe3987SAndy Lutomirski /* 27478fe3987SAndy Lutomirski * In theory, it's possible to have a buggy QEMU-supposed 27578fe3987SAndy Lutomirski * emulated Q35 IOMMU and Xen enabled at the same time. On 27678fe3987SAndy Lutomirski * such a configuration, virtio has never worked and will 27778fe3987SAndy Lutomirski * not work without an even larger kludge. Instead, enable 27878fe3987SAndy Lutomirski * the DMA API if we're a Xen guest, which at least allows 27978fe3987SAndy Lutomirski * all of the sensible Xen configurations to work correctly. 28078fe3987SAndy Lutomirski */ 28178fe3987SAndy Lutomirski if (xen_domain()) 28278fe3987SAndy Lutomirski return true; 28378fe3987SAndy Lutomirski 284d26c96c8SAndy Lutomirski return false; 285d26c96c8SAndy Lutomirski } 286d26c96c8SAndy Lutomirski 287e6d6dd6cSJoerg Roedel size_t virtio_max_dma_size(struct virtio_device *vdev) 288e6d6dd6cSJoerg Roedel { 289e6d6dd6cSJoerg Roedel size_t max_segment_size = SIZE_MAX; 290e6d6dd6cSJoerg Roedel 291e6d6dd6cSJoerg Roedel if (vring_use_dma_api(vdev)) 292817fc978SWill Deacon max_segment_size = dma_max_mapping_size(vdev->dev.parent); 293e6d6dd6cSJoerg Roedel 294e6d6dd6cSJoerg Roedel return max_segment_size; 295e6d6dd6cSJoerg Roedel } 296e6d6dd6cSJoerg Roedel EXPORT_SYMBOL_GPL(virtio_max_dma_size); 297e6d6dd6cSJoerg Roedel 298d79dca75STiwei Bie static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, 299d79dca75STiwei Bie dma_addr_t *dma_handle, gfp_t flag) 300d79dca75STiwei Bie { 301d79dca75STiwei Bie if (vring_use_dma_api(vdev)) { 302d79dca75STiwei Bie return dma_alloc_coherent(vdev->dev.parent, size, 303d79dca75STiwei Bie dma_handle, flag); 304d79dca75STiwei Bie } else { 305d79dca75STiwei Bie void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag); 306d79dca75STiwei Bie 307d79dca75STiwei Bie if (queue) { 308d79dca75STiwei Bie phys_addr_t phys_addr = virt_to_phys(queue); 309d79dca75STiwei Bie *dma_handle = (dma_addr_t)phys_addr; 310d79dca75STiwei Bie 311d79dca75STiwei Bie /* 312d79dca75STiwei Bie * Sanity check: make sure we dind't truncate 313d79dca75STiwei Bie * the address. The only arches I can find that 314d79dca75STiwei Bie * have 64-bit phys_addr_t but 32-bit dma_addr_t 315d79dca75STiwei Bie * are certain non-highmem MIPS and x86 316d79dca75STiwei Bie * configurations, but these configurations 317d79dca75STiwei Bie * should never allocate physical pages above 32 318d79dca75STiwei Bie * bits, so this is fine. Just in case, throw a 319d79dca75STiwei Bie * warning and abort if we end up with an 320d79dca75STiwei Bie * unrepresentable address. 321d79dca75STiwei Bie */ 322d79dca75STiwei Bie if (WARN_ON_ONCE(*dma_handle != phys_addr)) { 323d79dca75STiwei Bie free_pages_exact(queue, PAGE_ALIGN(size)); 324d79dca75STiwei Bie return NULL; 325d79dca75STiwei Bie } 326d79dca75STiwei Bie } 327d79dca75STiwei Bie return queue; 328d79dca75STiwei Bie } 329d79dca75STiwei Bie } 330d79dca75STiwei Bie 331d79dca75STiwei Bie static void vring_free_queue(struct virtio_device *vdev, size_t size, 332d79dca75STiwei Bie void *queue, dma_addr_t dma_handle) 333d79dca75STiwei Bie { 334d79dca75STiwei Bie if (vring_use_dma_api(vdev)) 335d79dca75STiwei Bie dma_free_coherent(vdev->dev.parent, size, queue, dma_handle); 336d79dca75STiwei Bie else 337d79dca75STiwei Bie free_pages_exact(queue, PAGE_ALIGN(size)); 338d79dca75STiwei Bie } 339d79dca75STiwei Bie 340780bc790SAndy Lutomirski /* 341780bc790SAndy Lutomirski * The DMA ops on various arches are rather gnarly right now, and 342780bc790SAndy Lutomirski * making all of the arch DMA ops work on the vring device itself 343780bc790SAndy Lutomirski * is a mess. For now, we use the parent device for DMA ops. 344780bc790SAndy Lutomirski */ 34575bfa81bSMichael S. Tsirkin static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq) 346780bc790SAndy Lutomirski { 347780bc790SAndy Lutomirski return vq->vq.vdev->dev.parent; 348780bc790SAndy Lutomirski } 349780bc790SAndy Lutomirski 350780bc790SAndy Lutomirski /* Map one sg entry. */ 351780bc790SAndy Lutomirski static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq, 352780bc790SAndy Lutomirski struct scatterlist *sg, 353780bc790SAndy Lutomirski enum dma_data_direction direction) 354780bc790SAndy Lutomirski { 355fb3fba6bSTiwei Bie if (!vq->use_dma_api) 356780bc790SAndy Lutomirski return (dma_addr_t)sg_phys(sg); 357780bc790SAndy Lutomirski 358780bc790SAndy Lutomirski /* 359780bc790SAndy Lutomirski * We can't use dma_map_sg, because we don't use scatterlists in 360780bc790SAndy Lutomirski * the way it expects (we don't guarantee that the scatterlist 361780bc790SAndy Lutomirski * will exist for the lifetime of the mapping). 362780bc790SAndy Lutomirski */ 363780bc790SAndy Lutomirski return dma_map_page(vring_dma_dev(vq), 364780bc790SAndy Lutomirski sg_page(sg), sg->offset, sg->length, 365780bc790SAndy Lutomirski direction); 366780bc790SAndy Lutomirski } 367780bc790SAndy Lutomirski 368780bc790SAndy Lutomirski static dma_addr_t vring_map_single(const struct vring_virtqueue *vq, 369780bc790SAndy Lutomirski void *cpu_addr, size_t size, 370780bc790SAndy Lutomirski enum dma_data_direction direction) 371780bc790SAndy Lutomirski { 372fb3fba6bSTiwei Bie if (!vq->use_dma_api) 373780bc790SAndy Lutomirski return (dma_addr_t)virt_to_phys(cpu_addr); 374780bc790SAndy Lutomirski 375780bc790SAndy Lutomirski return dma_map_single(vring_dma_dev(vq), 376780bc790SAndy Lutomirski cpu_addr, size, direction); 377780bc790SAndy Lutomirski } 378780bc790SAndy Lutomirski 379e6f633e5STiwei Bie static int vring_mapping_error(const struct vring_virtqueue *vq, 380e6f633e5STiwei Bie dma_addr_t addr) 381e6f633e5STiwei Bie { 382fb3fba6bSTiwei Bie if (!vq->use_dma_api) 383e6f633e5STiwei Bie return 0; 384e6f633e5STiwei Bie 385e6f633e5STiwei Bie return dma_mapping_error(vring_dma_dev(vq), addr); 386e6f633e5STiwei Bie } 387e6f633e5STiwei Bie 3883a897128SXuan Zhuo static void virtqueue_init(struct vring_virtqueue *vq, u32 num) 3893a897128SXuan Zhuo { 3903a897128SXuan Zhuo vq->vq.num_free = num; 3913a897128SXuan Zhuo 3923a897128SXuan Zhuo if (vq->packed_ring) 3933a897128SXuan Zhuo vq->last_used_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR); 3943a897128SXuan Zhuo else 3953a897128SXuan Zhuo vq->last_used_idx = 0; 3963a897128SXuan Zhuo 3973a897128SXuan Zhuo vq->event_triggered = false; 3983a897128SXuan Zhuo vq->num_added = 0; 3993a897128SXuan Zhuo 4003a897128SXuan Zhuo #ifdef DEBUG 4013a897128SXuan Zhuo vq->in_use = false; 4023a897128SXuan Zhuo vq->last_add_time_valid = false; 4033a897128SXuan Zhuo #endif 4043a897128SXuan Zhuo } 4053a897128SXuan Zhuo 406e6f633e5STiwei Bie 407e6f633e5STiwei Bie /* 408e6f633e5STiwei Bie * Split ring specific functions - *_split(). 409e6f633e5STiwei Bie */ 410e6f633e5STiwei Bie 41172b5e895SJason Wang static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq, 412780bc790SAndy Lutomirski struct vring_desc *desc) 413780bc790SAndy Lutomirski { 414780bc790SAndy Lutomirski u16 flags; 415780bc790SAndy Lutomirski 416fb3fba6bSTiwei Bie if (!vq->use_dma_api) 417780bc790SAndy Lutomirski return; 418780bc790SAndy Lutomirski 419780bc790SAndy Lutomirski flags = virtio16_to_cpu(vq->vq.vdev, desc->flags); 420780bc790SAndy Lutomirski 421780bc790SAndy Lutomirski dma_unmap_page(vring_dma_dev(vq), 422780bc790SAndy Lutomirski virtio64_to_cpu(vq->vq.vdev, desc->addr), 423780bc790SAndy Lutomirski virtio32_to_cpu(vq->vq.vdev, desc->len), 424780bc790SAndy Lutomirski (flags & VRING_DESC_F_WRITE) ? 425780bc790SAndy Lutomirski DMA_FROM_DEVICE : DMA_TO_DEVICE); 426780bc790SAndy Lutomirski } 427780bc790SAndy Lutomirski 42872b5e895SJason Wang static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq, 42972b5e895SJason Wang unsigned int i) 43072b5e895SJason Wang { 43172b5e895SJason Wang struct vring_desc_extra *extra = vq->split.desc_extra; 43272b5e895SJason Wang u16 flags; 43372b5e895SJason Wang 43472b5e895SJason Wang if (!vq->use_dma_api) 43572b5e895SJason Wang goto out; 43672b5e895SJason Wang 43772b5e895SJason Wang flags = extra[i].flags; 43872b5e895SJason Wang 43972b5e895SJason Wang if (flags & VRING_DESC_F_INDIRECT) { 44072b5e895SJason Wang dma_unmap_single(vring_dma_dev(vq), 44172b5e895SJason Wang extra[i].addr, 44272b5e895SJason Wang extra[i].len, 44372b5e895SJason Wang (flags & VRING_DESC_F_WRITE) ? 44472b5e895SJason Wang DMA_FROM_DEVICE : DMA_TO_DEVICE); 44572b5e895SJason Wang } else { 44672b5e895SJason Wang dma_unmap_page(vring_dma_dev(vq), 44772b5e895SJason Wang extra[i].addr, 44872b5e895SJason Wang extra[i].len, 44972b5e895SJason Wang (flags & VRING_DESC_F_WRITE) ? 45072b5e895SJason Wang DMA_FROM_DEVICE : DMA_TO_DEVICE); 45172b5e895SJason Wang } 45272b5e895SJason Wang 45372b5e895SJason Wang out: 45472b5e895SJason Wang return extra[i].next; 45572b5e895SJason Wang } 45672b5e895SJason Wang 457138fd251STiwei Bie static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq, 458138fd251STiwei Bie unsigned int total_sg, 459138fd251STiwei Bie gfp_t gfp) 4609fa29b9dSMark McLoughlin { 4619fa29b9dSMark McLoughlin struct vring_desc *desc; 462b25bd251SRusty Russell unsigned int i; 4639fa29b9dSMark McLoughlin 464b92b1b89SWill Deacon /* 465b92b1b89SWill Deacon * We require lowmem mappings for the descriptors because 466b92b1b89SWill Deacon * otherwise virt_to_phys will give us bogus addresses in the 467b92b1b89SWill Deacon * virtqueue. 468b92b1b89SWill Deacon */ 46982107539SMichal Hocko gfp &= ~__GFP_HIGHMEM; 470b92b1b89SWill Deacon 4716da2ec56SKees Cook desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp); 4729fa29b9dSMark McLoughlin if (!desc) 473b25bd251SRusty Russell return NULL; 4749fa29b9dSMark McLoughlin 475b25bd251SRusty Russell for (i = 0; i < total_sg; i++) 47600e6f3d9SMichael S. Tsirkin desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); 477b25bd251SRusty Russell return desc; 4789fa29b9dSMark McLoughlin } 4799fa29b9dSMark McLoughlin 480fe4c3862SJason Wang static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq, 481fe4c3862SJason Wang struct vring_desc *desc, 482fe4c3862SJason Wang unsigned int i, 483fe4c3862SJason Wang dma_addr_t addr, 484fe4c3862SJason Wang unsigned int len, 48572b5e895SJason Wang u16 flags, 48672b5e895SJason Wang bool indirect) 487fe4c3862SJason Wang { 48872b5e895SJason Wang struct vring_virtqueue *vring = to_vvq(vq); 48972b5e895SJason Wang struct vring_desc_extra *extra = vring->split.desc_extra; 49072b5e895SJason Wang u16 next; 49172b5e895SJason Wang 492fe4c3862SJason Wang desc[i].flags = cpu_to_virtio16(vq->vdev, flags); 493fe4c3862SJason Wang desc[i].addr = cpu_to_virtio64(vq->vdev, addr); 494fe4c3862SJason Wang desc[i].len = cpu_to_virtio32(vq->vdev, len); 495fe4c3862SJason Wang 49672b5e895SJason Wang if (!indirect) { 49772b5e895SJason Wang next = extra[i].next; 49872b5e895SJason Wang desc[i].next = cpu_to_virtio16(vq->vdev, next); 49972b5e895SJason Wang 50072b5e895SJason Wang extra[i].addr = addr; 50172b5e895SJason Wang extra[i].len = len; 50272b5e895SJason Wang extra[i].flags = flags; 50372b5e895SJason Wang } else 50472b5e895SJason Wang next = virtio16_to_cpu(vq->vdev, desc[i].next); 50572b5e895SJason Wang 50672b5e895SJason Wang return next; 507fe4c3862SJason Wang } 508fe4c3862SJason Wang 509138fd251STiwei Bie static inline int virtqueue_add_split(struct virtqueue *_vq, 51013816c76SRusty Russell struct scatterlist *sgs[], 511eeebf9b1SRusty Russell unsigned int total_sg, 51213816c76SRusty Russell unsigned int out_sgs, 51313816c76SRusty Russell unsigned int in_sgs, 514bbd603efSMichael S. Tsirkin void *data, 5155a08b04fSMichael S. Tsirkin void *ctx, 516bbd603efSMichael S. Tsirkin gfp_t gfp) 5170a8a69ddSRusty Russell { 5180a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 51913816c76SRusty Russell struct scatterlist *sg; 520b25bd251SRusty Russell struct vring_desc *desc; 5213f649ab7SKees Cook unsigned int i, n, avail, descs_used, prev, err_idx; 5221fe9b6feSMichael S. Tsirkin int head; 523b25bd251SRusty Russell bool indirect; 5240a8a69ddSRusty Russell 5259fa29b9dSMark McLoughlin START_USE(vq); 5269fa29b9dSMark McLoughlin 5270a8a69ddSRusty Russell BUG_ON(data == NULL); 5285a08b04fSMichael S. Tsirkin BUG_ON(ctx && vq->indirect); 5299fa29b9dSMark McLoughlin 53070670444SRusty Russell if (unlikely(vq->broken)) { 53170670444SRusty Russell END_USE(vq); 53270670444SRusty Russell return -EIO; 53370670444SRusty Russell } 53470670444SRusty Russell 5354d6a105eSTiwei Bie LAST_ADD_TIME_UPDATE(vq); 536e93300b1SRusty Russell 53713816c76SRusty Russell BUG_ON(total_sg == 0); 5380a8a69ddSRusty Russell 539b25bd251SRusty Russell head = vq->free_head; 540b25bd251SRusty Russell 54135c51e09SXianting Tian if (virtqueue_use_indirect(vq, total_sg)) 542138fd251STiwei Bie desc = alloc_indirect_split(_vq, total_sg, gfp); 54344ed8089SRichard W.M. Jones else { 544b25bd251SRusty Russell desc = NULL; 545e593bf97STiwei Bie WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect); 54644ed8089SRichard W.M. Jones } 547b25bd251SRusty Russell 548b25bd251SRusty Russell if (desc) { 549b25bd251SRusty Russell /* Use a single buffer which doesn't continue */ 550780bc790SAndy Lutomirski indirect = true; 551b25bd251SRusty Russell /* Set up rest to use this indirect table. */ 552b25bd251SRusty Russell i = 0; 553b25bd251SRusty Russell descs_used = 1; 554b25bd251SRusty Russell } else { 555780bc790SAndy Lutomirski indirect = false; 556e593bf97STiwei Bie desc = vq->split.vring.desc; 557b25bd251SRusty Russell i = head; 558b25bd251SRusty Russell descs_used = total_sg; 559b25bd251SRusty Russell } 560b25bd251SRusty Russell 561b4b4ff73SXianting Tian if (unlikely(vq->vq.num_free < descs_used)) { 5620a8a69ddSRusty Russell pr_debug("Can't add buf len %i - avail = %i\n", 563b25bd251SRusty Russell descs_used, vq->vq.num_free); 56444653eaeSRusty Russell /* FIXME: for historical reasons, we force a notify here if 56544653eaeSRusty Russell * there are outgoing parts to the buffer. Presumably the 56644653eaeSRusty Russell * host should service the ring ASAP. */ 56713816c76SRusty Russell if (out_sgs) 568426e3e0aSRusty Russell vq->notify(&vq->vq); 56958625edfSWei Yongjun if (indirect) 57058625edfSWei Yongjun kfree(desc); 5710a8a69ddSRusty Russell END_USE(vq); 5720a8a69ddSRusty Russell return -ENOSPC; 5730a8a69ddSRusty Russell } 5740a8a69ddSRusty Russell 57513816c76SRusty Russell for (n = 0; n < out_sgs; n++) { 576eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 577780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE); 578780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 579780bc790SAndy Lutomirski goto unmap_release; 580780bc790SAndy Lutomirski 5810a8a69ddSRusty Russell prev = i; 58272b5e895SJason Wang /* Note that we trust indirect descriptor 58372b5e895SJason Wang * table since it use stream DMA mapping. 58472b5e895SJason Wang */ 585fe4c3862SJason Wang i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length, 58672b5e895SJason Wang VRING_DESC_F_NEXT, 58772b5e895SJason Wang indirect); 5880a8a69ddSRusty Russell } 58913816c76SRusty Russell } 59013816c76SRusty Russell for (; n < (out_sgs + in_sgs); n++) { 591eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 592780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE); 593780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 594780bc790SAndy Lutomirski goto unmap_release; 595780bc790SAndy Lutomirski 5960a8a69ddSRusty Russell prev = i; 59772b5e895SJason Wang /* Note that we trust indirect descriptor 59872b5e895SJason Wang * table since it use stream DMA mapping. 59972b5e895SJason Wang */ 600fe4c3862SJason Wang i = virtqueue_add_desc_split(_vq, desc, i, addr, 601fe4c3862SJason Wang sg->length, 602fe4c3862SJason Wang VRING_DESC_F_NEXT | 60372b5e895SJason Wang VRING_DESC_F_WRITE, 60472b5e895SJason Wang indirect); 60513816c76SRusty Russell } 6060a8a69ddSRusty Russell } 6070a8a69ddSRusty Russell /* Last one doesn't continue. */ 60800e6f3d9SMichael S. Tsirkin desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); 60972b5e895SJason Wang if (!indirect && vq->use_dma_api) 610890d3356SVincent Whitchurch vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &= 61172b5e895SJason Wang ~VRING_DESC_F_NEXT; 6120a8a69ddSRusty Russell 613780bc790SAndy Lutomirski if (indirect) { 614780bc790SAndy Lutomirski /* Now that the indirect table is filled in, map it. */ 615780bc790SAndy Lutomirski dma_addr_t addr = vring_map_single( 616780bc790SAndy Lutomirski vq, desc, total_sg * sizeof(struct vring_desc), 617780bc790SAndy Lutomirski DMA_TO_DEVICE); 618780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 619780bc790SAndy Lutomirski goto unmap_release; 620780bc790SAndy Lutomirski 621fe4c3862SJason Wang virtqueue_add_desc_split(_vq, vq->split.vring.desc, 622fe4c3862SJason Wang head, addr, 623fe4c3862SJason Wang total_sg * sizeof(struct vring_desc), 62472b5e895SJason Wang VRING_DESC_F_INDIRECT, 62572b5e895SJason Wang false); 626780bc790SAndy Lutomirski } 627780bc790SAndy Lutomirski 628780bc790SAndy Lutomirski /* We're using some buffers from the free list. */ 629780bc790SAndy Lutomirski vq->vq.num_free -= descs_used; 630780bc790SAndy Lutomirski 6310a8a69ddSRusty Russell /* Update free pointer */ 632b25bd251SRusty Russell if (indirect) 63372b5e895SJason Wang vq->free_head = vq->split.desc_extra[head].next; 634b25bd251SRusty Russell else 6350a8a69ddSRusty Russell vq->free_head = i; 6360a8a69ddSRusty Russell 637780bc790SAndy Lutomirski /* Store token and indirect buffer state. */ 638cbeedb72STiwei Bie vq->split.desc_state[head].data = data; 639780bc790SAndy Lutomirski if (indirect) 640cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc = desc; 64187646a34SJason Wang else 642cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc = ctx; 6430a8a69ddSRusty Russell 6440a8a69ddSRusty Russell /* Put entry in available array (but don't update avail->idx until they 6453b720b8cSRusty Russell * do sync). */ 646e593bf97STiwei Bie avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1); 647e593bf97STiwei Bie vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); 6480a8a69ddSRusty Russell 649ee7cd898SRusty Russell /* Descriptors and available array need to be set before we expose the 650ee7cd898SRusty Russell * new available array entries. */ 651a9a0fef7SRusty Russell virtio_wmb(vq->weak_barriers); 652e593bf97STiwei Bie vq->split.avail_idx_shadow++; 653e593bf97STiwei Bie vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, 654e593bf97STiwei Bie vq->split.avail_idx_shadow); 655ee7cd898SRusty Russell vq->num_added++; 656ee7cd898SRusty Russell 6575e05bf58STetsuo Handa pr_debug("Added buffer head %i to %p\n", head, vq); 6585e05bf58STetsuo Handa END_USE(vq); 6595e05bf58STetsuo Handa 660ee7cd898SRusty Russell /* This is very unlikely, but theoretically possible. Kick 661ee7cd898SRusty Russell * just in case. */ 662ee7cd898SRusty Russell if (unlikely(vq->num_added == (1 << 16) - 1)) 663ee7cd898SRusty Russell virtqueue_kick(_vq); 664ee7cd898SRusty Russell 66598e8c6bcSRusty Russell return 0; 666780bc790SAndy Lutomirski 667780bc790SAndy Lutomirski unmap_release: 668780bc790SAndy Lutomirski err_idx = i; 669cf8f1696SMatthias Lange 670cf8f1696SMatthias Lange if (indirect) 671cf8f1696SMatthias Lange i = 0; 672cf8f1696SMatthias Lange else 673780bc790SAndy Lutomirski i = head; 674780bc790SAndy Lutomirski 675780bc790SAndy Lutomirski for (n = 0; n < total_sg; n++) { 676780bc790SAndy Lutomirski if (i == err_idx) 677780bc790SAndy Lutomirski break; 67872b5e895SJason Wang if (indirect) { 67972b5e895SJason Wang vring_unmap_one_split_indirect(vq, &desc[i]); 680cf8f1696SMatthias Lange i = virtio16_to_cpu(_vq->vdev, desc[i].next); 68172b5e895SJason Wang } else 68272b5e895SJason Wang i = vring_unmap_one_split(vq, i); 683780bc790SAndy Lutomirski } 684780bc790SAndy Lutomirski 685780bc790SAndy Lutomirski if (indirect) 686780bc790SAndy Lutomirski kfree(desc); 687780bc790SAndy Lutomirski 6883cc36f6eSMichael S. Tsirkin END_USE(vq); 689f7728002SHalil Pasic return -ENOMEM; 6900a8a69ddSRusty Russell } 69113816c76SRusty Russell 692138fd251STiwei Bie static bool virtqueue_kick_prepare_split(struct virtqueue *_vq) 6930a8a69ddSRusty Russell { 6940a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 695a5c262c5SMichael S. Tsirkin u16 new, old; 69641f0377fSRusty Russell bool needs_kick; 69741f0377fSRusty Russell 6980a8a69ddSRusty Russell START_USE(vq); 699a72caae2SJason Wang /* We need to expose available array entries before checking avail 700a72caae2SJason Wang * event. */ 701a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 7020a8a69ddSRusty Russell 703e593bf97STiwei Bie old = vq->split.avail_idx_shadow - vq->num_added; 704e593bf97STiwei Bie new = vq->split.avail_idx_shadow; 7050a8a69ddSRusty Russell vq->num_added = 0; 7060a8a69ddSRusty Russell 7074d6a105eSTiwei Bie LAST_ADD_TIME_CHECK(vq); 7084d6a105eSTiwei Bie LAST_ADD_TIME_INVALID(vq); 709e93300b1SRusty Russell 71041f0377fSRusty Russell if (vq->event) { 711e593bf97STiwei Bie needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, 712e593bf97STiwei Bie vring_avail_event(&vq->split.vring)), 71341f0377fSRusty Russell new, old); 71441f0377fSRusty Russell } else { 715e593bf97STiwei Bie needs_kick = !(vq->split.vring.used->flags & 716e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 717e593bf97STiwei Bie VRING_USED_F_NO_NOTIFY)); 71841f0377fSRusty Russell } 7190a8a69ddSRusty Russell END_USE(vq); 72041f0377fSRusty Russell return needs_kick; 72141f0377fSRusty Russell } 722138fd251STiwei Bie 723138fd251STiwei Bie static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, 7245a08b04fSMichael S. Tsirkin void **ctx) 7250a8a69ddSRusty Russell { 726780bc790SAndy Lutomirski unsigned int i, j; 727c60923cbSGonglei __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); 7280a8a69ddSRusty Russell 7290a8a69ddSRusty Russell /* Clear data ptr. */ 730cbeedb72STiwei Bie vq->split.desc_state[head].data = NULL; 7310a8a69ddSRusty Russell 732780bc790SAndy Lutomirski /* Put back on free list: unmap first-level descriptors and find end */ 7330a8a69ddSRusty Russell i = head; 7349fa29b9dSMark McLoughlin 735e593bf97STiwei Bie while (vq->split.vring.desc[i].flags & nextflag) { 73672b5e895SJason Wang vring_unmap_one_split(vq, i); 73772b5e895SJason Wang i = vq->split.desc_extra[i].next; 73806ca287dSRusty Russell vq->vq.num_free++; 7390a8a69ddSRusty Russell } 7400a8a69ddSRusty Russell 74172b5e895SJason Wang vring_unmap_one_split(vq, i); 74272b5e895SJason Wang vq->split.desc_extra[i].next = vq->free_head; 7430a8a69ddSRusty Russell vq->free_head = head; 744780bc790SAndy Lutomirski 7450a8a69ddSRusty Russell /* Plus final descriptor */ 74606ca287dSRusty Russell vq->vq.num_free++; 747780bc790SAndy Lutomirski 7485a08b04fSMichael S. Tsirkin if (vq->indirect) { 749cbeedb72STiwei Bie struct vring_desc *indir_desc = 750cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc; 7515a08b04fSMichael S. Tsirkin u32 len; 7525a08b04fSMichael S. Tsirkin 7535a08b04fSMichael S. Tsirkin /* Free the indirect table, if any, now that it's unmapped. */ 7545a08b04fSMichael S. Tsirkin if (!indir_desc) 7555a08b04fSMichael S. Tsirkin return; 7565a08b04fSMichael S. Tsirkin 75772b5e895SJason Wang len = vq->split.desc_extra[head].len; 758780bc790SAndy Lutomirski 75972b5e895SJason Wang BUG_ON(!(vq->split.desc_extra[head].flags & 76072b5e895SJason Wang VRING_DESC_F_INDIRECT)); 761780bc790SAndy Lutomirski BUG_ON(len == 0 || len % sizeof(struct vring_desc)); 762780bc790SAndy Lutomirski 763780bc790SAndy Lutomirski for (j = 0; j < len / sizeof(struct vring_desc); j++) 76472b5e895SJason Wang vring_unmap_one_split_indirect(vq, &indir_desc[j]); 765780bc790SAndy Lutomirski 7665a08b04fSMichael S. Tsirkin kfree(indir_desc); 767cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc = NULL; 7685a08b04fSMichael S. Tsirkin } else if (ctx) { 769cbeedb72STiwei Bie *ctx = vq->split.desc_state[head].indir_desc; 770780bc790SAndy Lutomirski } 7710a8a69ddSRusty Russell } 7720a8a69ddSRusty Russell 773138fd251STiwei Bie static inline bool more_used_split(const struct vring_virtqueue *vq) 7740a8a69ddSRusty Russell { 775e593bf97STiwei Bie return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, 776e593bf97STiwei Bie vq->split.vring.used->idx); 7770a8a69ddSRusty Russell } 7780a8a69ddSRusty Russell 779138fd251STiwei Bie static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, 780138fd251STiwei Bie unsigned int *len, 7815a08b04fSMichael S. Tsirkin void **ctx) 7820a8a69ddSRusty Russell { 7830a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 7840a8a69ddSRusty Russell void *ret; 7850a8a69ddSRusty Russell unsigned int i; 7863b720b8cSRusty Russell u16 last_used; 7870a8a69ddSRusty Russell 7880a8a69ddSRusty Russell START_USE(vq); 7890a8a69ddSRusty Russell 7905ef82752SRusty Russell if (unlikely(vq->broken)) { 7915ef82752SRusty Russell END_USE(vq); 7925ef82752SRusty Russell return NULL; 7935ef82752SRusty Russell } 7945ef82752SRusty Russell 795138fd251STiwei Bie if (!more_used_split(vq)) { 7960a8a69ddSRusty Russell pr_debug("No more buffers in queue\n"); 7970a8a69ddSRusty Russell END_USE(vq); 7980a8a69ddSRusty Russell return NULL; 7990a8a69ddSRusty Russell } 8000a8a69ddSRusty Russell 8012d61ba95SMichael S. Tsirkin /* Only get used array entries after they have been exposed by host. */ 802a9a0fef7SRusty Russell virtio_rmb(vq->weak_barriers); 8032d61ba95SMichael S. Tsirkin 804e593bf97STiwei Bie last_used = (vq->last_used_idx & (vq->split.vring.num - 1)); 805e593bf97STiwei Bie i = virtio32_to_cpu(_vq->vdev, 806e593bf97STiwei Bie vq->split.vring.used->ring[last_used].id); 807e593bf97STiwei Bie *len = virtio32_to_cpu(_vq->vdev, 808e593bf97STiwei Bie vq->split.vring.used->ring[last_used].len); 8090a8a69ddSRusty Russell 810e593bf97STiwei Bie if (unlikely(i >= vq->split.vring.num)) { 8110a8a69ddSRusty Russell BAD_RING(vq, "id %u out of range\n", i); 8120a8a69ddSRusty Russell return NULL; 8130a8a69ddSRusty Russell } 814cbeedb72STiwei Bie if (unlikely(!vq->split.desc_state[i].data)) { 8150a8a69ddSRusty Russell BAD_RING(vq, "id %u is not a head!\n", i); 8160a8a69ddSRusty Russell return NULL; 8170a8a69ddSRusty Russell } 8180a8a69ddSRusty Russell 819138fd251STiwei Bie /* detach_buf_split clears data, so grab it now. */ 820cbeedb72STiwei Bie ret = vq->split.desc_state[i].data; 821138fd251STiwei Bie detach_buf_split(vq, i, ctx); 8220a8a69ddSRusty Russell vq->last_used_idx++; 823a5c262c5SMichael S. Tsirkin /* If we expect an interrupt for the next entry, tell host 824a5c262c5SMichael S. Tsirkin * by writing event index and flush out the write before 825a5c262c5SMichael S. Tsirkin * the read in the next get_buf call. */ 826e593bf97STiwei Bie if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) 827788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 828e593bf97STiwei Bie &vring_used_event(&vq->split.vring), 829788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); 830a5c262c5SMichael S. Tsirkin 8314d6a105eSTiwei Bie LAST_ADD_TIME_INVALID(vq); 832e93300b1SRusty Russell 8330a8a69ddSRusty Russell END_USE(vq); 8340a8a69ddSRusty Russell return ret; 8350a8a69ddSRusty Russell } 836138fd251STiwei Bie 837138fd251STiwei Bie static void virtqueue_disable_cb_split(struct virtqueue *_vq) 838138fd251STiwei Bie { 839138fd251STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 840138fd251STiwei Bie 841e593bf97STiwei Bie if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { 842e593bf97STiwei Bie vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 8438d622d21SMichael S. Tsirkin if (vq->event) 8448d622d21SMichael S. Tsirkin /* TODO: this is a hack. Figure out a cleaner value to write. */ 8458d622d21SMichael S. Tsirkin vring_used_event(&vq->split.vring) = 0x0; 8468d622d21SMichael S. Tsirkin else 847e593bf97STiwei Bie vq->split.vring.avail->flags = 848e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 849e593bf97STiwei Bie vq->split.avail_flags_shadow); 850138fd251STiwei Bie } 851138fd251STiwei Bie } 852138fd251STiwei Bie 85331532340SSolomon Tan static unsigned int virtqueue_enable_cb_prepare_split(struct virtqueue *_vq) 854cc229884SMichael S. Tsirkin { 855cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 856cc229884SMichael S. Tsirkin u16 last_used_idx; 857cc229884SMichael S. Tsirkin 858cc229884SMichael S. Tsirkin START_USE(vq); 859cc229884SMichael S. Tsirkin 860cc229884SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 861cc229884SMichael S. Tsirkin * more to do. */ 862cc229884SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to 863cc229884SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 864cc229884SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 865e593bf97STiwei Bie if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 866e593bf97STiwei Bie vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 8670ea1e4a6SLadi Prosek if (!vq->event) 868e593bf97STiwei Bie vq->split.vring.avail->flags = 869e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 870e593bf97STiwei Bie vq->split.avail_flags_shadow); 871f277ec42SVenkatesh Srinivas } 872e593bf97STiwei Bie vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev, 873e593bf97STiwei Bie last_used_idx = vq->last_used_idx); 874cc229884SMichael S. Tsirkin END_USE(vq); 875cc229884SMichael S. Tsirkin return last_used_idx; 876cc229884SMichael S. Tsirkin } 877138fd251STiwei Bie 87831532340SSolomon Tan static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned int last_used_idx) 879138fd251STiwei Bie { 880138fd251STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 881138fd251STiwei Bie 882138fd251STiwei Bie return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, 883e593bf97STiwei Bie vq->split.vring.used->idx); 884138fd251STiwei Bie } 885138fd251STiwei Bie 886138fd251STiwei Bie static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq) 8877ab358c2SMichael S. Tsirkin { 8887ab358c2SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 8897ab358c2SMichael S. Tsirkin u16 bufs; 8907ab358c2SMichael S. Tsirkin 8917ab358c2SMichael S. Tsirkin START_USE(vq); 8927ab358c2SMichael S. Tsirkin 8937ab358c2SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 8947ab358c2SMichael S. Tsirkin * more to do. */ 8957ab358c2SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to 8967ab358c2SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 8970ea1e4a6SLadi Prosek * entry. Always update the event index to keep code simple. */ 898e593bf97STiwei Bie if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 899e593bf97STiwei Bie vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 9000ea1e4a6SLadi Prosek if (!vq->event) 901e593bf97STiwei Bie vq->split.vring.avail->flags = 902e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 903e593bf97STiwei Bie vq->split.avail_flags_shadow); 904f277ec42SVenkatesh Srinivas } 9057ab358c2SMichael S. Tsirkin /* TODO: tune this threshold */ 906e593bf97STiwei Bie bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4; 907788e5b3aSMichael S. Tsirkin 908788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 909e593bf97STiwei Bie &vring_used_event(&vq->split.vring), 910788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); 911788e5b3aSMichael S. Tsirkin 912e593bf97STiwei Bie if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx) 913e593bf97STiwei Bie - vq->last_used_idx) > bufs)) { 9147ab358c2SMichael S. Tsirkin END_USE(vq); 9157ab358c2SMichael S. Tsirkin return false; 9167ab358c2SMichael S. Tsirkin } 9177ab358c2SMichael S. Tsirkin 9187ab358c2SMichael S. Tsirkin END_USE(vq); 9197ab358c2SMichael S. Tsirkin return true; 9207ab358c2SMichael S. Tsirkin } 9217ab358c2SMichael S. Tsirkin 922138fd251STiwei Bie static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq) 923c021eac4SShirley Ma { 924c021eac4SShirley Ma struct vring_virtqueue *vq = to_vvq(_vq); 925c021eac4SShirley Ma unsigned int i; 926c021eac4SShirley Ma void *buf; 927c021eac4SShirley Ma 928c021eac4SShirley Ma START_USE(vq); 929c021eac4SShirley Ma 930e593bf97STiwei Bie for (i = 0; i < vq->split.vring.num; i++) { 931cbeedb72STiwei Bie if (!vq->split.desc_state[i].data) 932c021eac4SShirley Ma continue; 933138fd251STiwei Bie /* detach_buf_split clears data, so grab it now. */ 934cbeedb72STiwei Bie buf = vq->split.desc_state[i].data; 935138fd251STiwei Bie detach_buf_split(vq, i, NULL); 936e593bf97STiwei Bie vq->split.avail_idx_shadow--; 937e593bf97STiwei Bie vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, 938e593bf97STiwei Bie vq->split.avail_idx_shadow); 939c021eac4SShirley Ma END_USE(vq); 940c021eac4SShirley Ma return buf; 941c021eac4SShirley Ma } 942c021eac4SShirley Ma /* That should have freed everything. */ 943e593bf97STiwei Bie BUG_ON(vq->vq.num_free != vq->split.vring.num); 944c021eac4SShirley Ma 945c021eac4SShirley Ma END_USE(vq); 946c021eac4SShirley Ma return NULL; 947c021eac4SShirley Ma } 948138fd251STiwei Bie 949198fa7beSXuan Zhuo static void virtqueue_vring_init_split(struct vring_virtqueue_split *vring_split, 950198fa7beSXuan Zhuo struct vring_virtqueue *vq) 951198fa7beSXuan Zhuo { 952198fa7beSXuan Zhuo struct virtio_device *vdev; 953198fa7beSXuan Zhuo 954198fa7beSXuan Zhuo vdev = vq->vq.vdev; 955198fa7beSXuan Zhuo 956198fa7beSXuan Zhuo vring_split->avail_flags_shadow = 0; 957198fa7beSXuan Zhuo vring_split->avail_idx_shadow = 0; 958198fa7beSXuan Zhuo 959198fa7beSXuan Zhuo /* No callback? Tell other side not to bother us. */ 960198fa7beSXuan Zhuo if (!vq->vq.callback) { 961198fa7beSXuan Zhuo vring_split->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 962198fa7beSXuan Zhuo if (!vq->event) 963198fa7beSXuan Zhuo vring_split->vring.avail->flags = cpu_to_virtio16(vdev, 964198fa7beSXuan Zhuo vring_split->avail_flags_shadow); 965198fa7beSXuan Zhuo } 966198fa7beSXuan Zhuo } 967198fa7beSXuan Zhuo 968e5175b41SXuan Zhuo static void virtqueue_reinit_split(struct vring_virtqueue *vq) 969e5175b41SXuan Zhuo { 970e5175b41SXuan Zhuo int num; 971e5175b41SXuan Zhuo 972e5175b41SXuan Zhuo num = vq->split.vring.num; 973e5175b41SXuan Zhuo 974e5175b41SXuan Zhuo vq->split.vring.avail->flags = 0; 975e5175b41SXuan Zhuo vq->split.vring.avail->idx = 0; 976e5175b41SXuan Zhuo 977e5175b41SXuan Zhuo /* reset avail event */ 978e5175b41SXuan Zhuo vq->split.vring.avail->ring[num] = 0; 979e5175b41SXuan Zhuo 980e5175b41SXuan Zhuo vq->split.vring.used->flags = 0; 981e5175b41SXuan Zhuo vq->split.vring.used->idx = 0; 982e5175b41SXuan Zhuo 983e5175b41SXuan Zhuo /* reset used event */ 984e5175b41SXuan Zhuo *(__virtio16 *)&(vq->split.vring.used->ring[num]) = 0; 985e5175b41SXuan Zhuo 986e5175b41SXuan Zhuo virtqueue_init(vq, num); 987e5175b41SXuan Zhuo 988e5175b41SXuan Zhuo virtqueue_vring_init_split(&vq->split, vq); 989e5175b41SXuan Zhuo } 990e5175b41SXuan Zhuo 991e1d6a423SXuan Zhuo static void virtqueue_vring_attach_split(struct vring_virtqueue *vq, 992e1d6a423SXuan Zhuo struct vring_virtqueue_split *vring_split) 993e1d6a423SXuan Zhuo { 994e1d6a423SXuan Zhuo vq->split = *vring_split; 995e1d6a423SXuan Zhuo 996e1d6a423SXuan Zhuo /* Put everything in free lists. */ 997e1d6a423SXuan Zhuo vq->free_head = 0; 998e1d6a423SXuan Zhuo } 999e1d6a423SXuan Zhuo 1000a2b36c8dSXuan Zhuo static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vring_split) 1001a2b36c8dSXuan Zhuo { 1002a2b36c8dSXuan Zhuo struct vring_desc_state_split *state; 1003a2b36c8dSXuan Zhuo struct vring_desc_extra *extra; 1004a2b36c8dSXuan Zhuo u32 num = vring_split->vring.num; 1005a2b36c8dSXuan Zhuo 1006a2b36c8dSXuan Zhuo state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL); 1007a2b36c8dSXuan Zhuo if (!state) 1008a2b36c8dSXuan Zhuo goto err_state; 1009a2b36c8dSXuan Zhuo 1010a2b36c8dSXuan Zhuo extra = vring_alloc_desc_extra(num); 1011a2b36c8dSXuan Zhuo if (!extra) 1012a2b36c8dSXuan Zhuo goto err_extra; 1013a2b36c8dSXuan Zhuo 1014a2b36c8dSXuan Zhuo memset(state, 0, num * sizeof(struct vring_desc_state_split)); 1015a2b36c8dSXuan Zhuo 1016a2b36c8dSXuan Zhuo vring_split->desc_state = state; 1017a2b36c8dSXuan Zhuo vring_split->desc_extra = extra; 1018a2b36c8dSXuan Zhuo return 0; 1019a2b36c8dSXuan Zhuo 1020a2b36c8dSXuan Zhuo err_extra: 1021a2b36c8dSXuan Zhuo kfree(state); 1022a2b36c8dSXuan Zhuo err_state: 1023a2b36c8dSXuan Zhuo return -ENOMEM; 1024a2b36c8dSXuan Zhuo } 1025a2b36c8dSXuan Zhuo 102689f05d94SXuan Zhuo static void vring_free_split(struct vring_virtqueue_split *vring_split, 102789f05d94SXuan Zhuo struct virtio_device *vdev) 102889f05d94SXuan Zhuo { 102989f05d94SXuan Zhuo vring_free_queue(vdev, vring_split->queue_size_in_bytes, 103089f05d94SXuan Zhuo vring_split->vring.desc, 103189f05d94SXuan Zhuo vring_split->queue_dma_addr); 103289f05d94SXuan Zhuo 103389f05d94SXuan Zhuo kfree(vring_split->desc_state); 103489f05d94SXuan Zhuo kfree(vring_split->desc_extra); 103589f05d94SXuan Zhuo } 103689f05d94SXuan Zhuo 1037c2d87fe6SXuan Zhuo static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split, 1038c2d87fe6SXuan Zhuo struct virtio_device *vdev, 1039c2d87fe6SXuan Zhuo u32 num, 1040c2d87fe6SXuan Zhuo unsigned int vring_align, 1041c2d87fe6SXuan Zhuo bool may_reduce_num) 1042c2d87fe6SXuan Zhuo { 1043c2d87fe6SXuan Zhuo void *queue = NULL; 1044c2d87fe6SXuan Zhuo dma_addr_t dma_addr; 1045c2d87fe6SXuan Zhuo 1046c2d87fe6SXuan Zhuo /* We assume num is a power of 2. */ 1047c2d87fe6SXuan Zhuo if (num & (num - 1)) { 1048c2d87fe6SXuan Zhuo dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); 1049c2d87fe6SXuan Zhuo return -EINVAL; 1050c2d87fe6SXuan Zhuo } 1051c2d87fe6SXuan Zhuo 1052c2d87fe6SXuan Zhuo /* TODO: allocate each queue chunk individually */ 1053c2d87fe6SXuan Zhuo for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { 1054c2d87fe6SXuan Zhuo queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 1055c2d87fe6SXuan Zhuo &dma_addr, 1056c2d87fe6SXuan Zhuo GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO); 1057c2d87fe6SXuan Zhuo if (queue) 1058c2d87fe6SXuan Zhuo break; 1059c2d87fe6SXuan Zhuo if (!may_reduce_num) 1060c2d87fe6SXuan Zhuo return -ENOMEM; 1061c2d87fe6SXuan Zhuo } 1062c2d87fe6SXuan Zhuo 1063c2d87fe6SXuan Zhuo if (!num) 1064c2d87fe6SXuan Zhuo return -ENOMEM; 1065c2d87fe6SXuan Zhuo 1066c2d87fe6SXuan Zhuo if (!queue) { 1067c2d87fe6SXuan Zhuo /* Try to get a single page. You are my only hope! */ 1068c2d87fe6SXuan Zhuo queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 1069c2d87fe6SXuan Zhuo &dma_addr, GFP_KERNEL|__GFP_ZERO); 1070c2d87fe6SXuan Zhuo } 1071c2d87fe6SXuan Zhuo if (!queue) 1072c2d87fe6SXuan Zhuo return -ENOMEM; 1073c2d87fe6SXuan Zhuo 1074c2d87fe6SXuan Zhuo vring_init(&vring_split->vring, num, queue, vring_align); 1075c2d87fe6SXuan Zhuo 1076c2d87fe6SXuan Zhuo vring_split->queue_dma_addr = dma_addr; 1077c2d87fe6SXuan Zhuo vring_split->queue_size_in_bytes = vring_size(num, vring_align); 1078c2d87fe6SXuan Zhuo 1079af36b16fSXuan Zhuo vring_split->vring_align = vring_align; 1080af36b16fSXuan Zhuo vring_split->may_reduce_num = may_reduce_num; 1081af36b16fSXuan Zhuo 1082c2d87fe6SXuan Zhuo return 0; 1083c2d87fe6SXuan Zhuo } 1084c2d87fe6SXuan Zhuo 1085d79dca75STiwei Bie static struct virtqueue *vring_create_virtqueue_split( 1086d79dca75STiwei Bie unsigned int index, 1087d79dca75STiwei Bie unsigned int num, 1088d79dca75STiwei Bie unsigned int vring_align, 1089d79dca75STiwei Bie struct virtio_device *vdev, 1090d79dca75STiwei Bie bool weak_barriers, 1091d79dca75STiwei Bie bool may_reduce_num, 1092d79dca75STiwei Bie bool context, 1093d79dca75STiwei Bie bool (*notify)(struct virtqueue *), 1094d79dca75STiwei Bie void (*callback)(struct virtqueue *), 1095d79dca75STiwei Bie const char *name) 1096d79dca75STiwei Bie { 1097cd4c812aSXuan Zhuo struct vring_virtqueue_split vring_split = {}; 1098d79dca75STiwei Bie struct virtqueue *vq; 1099c2d87fe6SXuan Zhuo int err; 1100d79dca75STiwei Bie 1101c2d87fe6SXuan Zhuo err = vring_alloc_queue_split(&vring_split, vdev, num, vring_align, 1102c2d87fe6SXuan Zhuo may_reduce_num); 1103c2d87fe6SXuan Zhuo if (err) 1104d79dca75STiwei Bie return NULL; 1105d79dca75STiwei Bie 1106cd4c812aSXuan Zhuo vq = __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers, 1107cd4c812aSXuan Zhuo context, notify, callback, name); 1108d79dca75STiwei Bie if (!vq) { 1109c2d87fe6SXuan Zhuo vring_free_split(&vring_split, vdev); 1110d79dca75STiwei Bie return NULL; 1111d79dca75STiwei Bie } 1112d79dca75STiwei Bie 1113d79dca75STiwei Bie to_vvq(vq)->we_own_ring = true; 1114d79dca75STiwei Bie 1115d79dca75STiwei Bie return vq; 1116d79dca75STiwei Bie } 1117d79dca75STiwei Bie 11186fea20e5SXuan Zhuo static int virtqueue_resize_split(struct virtqueue *_vq, u32 num) 11196fea20e5SXuan Zhuo { 11206fea20e5SXuan Zhuo struct vring_virtqueue_split vring_split = {}; 11216fea20e5SXuan Zhuo struct vring_virtqueue *vq = to_vvq(_vq); 11226fea20e5SXuan Zhuo struct virtio_device *vdev = _vq->vdev; 11236fea20e5SXuan Zhuo int err; 11246fea20e5SXuan Zhuo 11256fea20e5SXuan Zhuo err = vring_alloc_queue_split(&vring_split, vdev, num, 11266fea20e5SXuan Zhuo vq->split.vring_align, 11276fea20e5SXuan Zhuo vq->split.may_reduce_num); 11286fea20e5SXuan Zhuo if (err) 11296fea20e5SXuan Zhuo goto err; 11306fea20e5SXuan Zhuo 11316fea20e5SXuan Zhuo err = vring_alloc_state_extra_split(&vring_split); 11326fea20e5SXuan Zhuo if (err) 11336fea20e5SXuan Zhuo goto err_state_extra; 11346fea20e5SXuan Zhuo 11356fea20e5SXuan Zhuo vring_free(&vq->vq); 11366fea20e5SXuan Zhuo 11376fea20e5SXuan Zhuo virtqueue_vring_init_split(&vring_split, vq); 11386fea20e5SXuan Zhuo 11396fea20e5SXuan Zhuo virtqueue_init(vq, vring_split.vring.num); 11406fea20e5SXuan Zhuo virtqueue_vring_attach_split(vq, &vring_split); 11416fea20e5SXuan Zhuo 11426fea20e5SXuan Zhuo return 0; 11436fea20e5SXuan Zhuo 11446fea20e5SXuan Zhuo err_state_extra: 11456fea20e5SXuan Zhuo vring_free_split(&vring_split, vdev); 11466fea20e5SXuan Zhuo err: 11476fea20e5SXuan Zhuo virtqueue_reinit_split(vq); 11486fea20e5SXuan Zhuo return -ENOMEM; 11496fea20e5SXuan Zhuo } 11506fea20e5SXuan Zhuo 1151e6f633e5STiwei Bie 1152e6f633e5STiwei Bie /* 11531ce9e605STiwei Bie * Packed ring specific functions - *_packed(). 11541ce9e605STiwei Bie */ 1155a7722890Shuangjie.albert static inline bool packed_used_wrap_counter(u16 last_used_idx) 1156a7722890Shuangjie.albert { 1157a7722890Shuangjie.albert return !!(last_used_idx & (1 << VRING_PACKED_EVENT_F_WRAP_CTR)); 1158a7722890Shuangjie.albert } 1159a7722890Shuangjie.albert 1160a7722890Shuangjie.albert static inline u16 packed_last_used(u16 last_used_idx) 1161a7722890Shuangjie.albert { 1162a7722890Shuangjie.albert return last_used_idx & ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR)); 1163a7722890Shuangjie.albert } 11641ce9e605STiwei Bie 1165d80dc15bSXuan Zhuo static void vring_unmap_extra_packed(const struct vring_virtqueue *vq, 1166d80dc15bSXuan Zhuo struct vring_desc_extra *extra) 11671ce9e605STiwei Bie { 11681ce9e605STiwei Bie u16 flags; 11691ce9e605STiwei Bie 11701ce9e605STiwei Bie if (!vq->use_dma_api) 11711ce9e605STiwei Bie return; 11721ce9e605STiwei Bie 1173d80dc15bSXuan Zhuo flags = extra->flags; 11741ce9e605STiwei Bie 11751ce9e605STiwei Bie if (flags & VRING_DESC_F_INDIRECT) { 11761ce9e605STiwei Bie dma_unmap_single(vring_dma_dev(vq), 1177d80dc15bSXuan Zhuo extra->addr, extra->len, 11781ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 11791ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 11801ce9e605STiwei Bie } else { 11811ce9e605STiwei Bie dma_unmap_page(vring_dma_dev(vq), 1182d80dc15bSXuan Zhuo extra->addr, extra->len, 11831ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 11841ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 11851ce9e605STiwei Bie } 11861ce9e605STiwei Bie } 11871ce9e605STiwei Bie 11881ce9e605STiwei Bie static void vring_unmap_desc_packed(const struct vring_virtqueue *vq, 11891ce9e605STiwei Bie struct vring_packed_desc *desc) 11901ce9e605STiwei Bie { 11911ce9e605STiwei Bie u16 flags; 11921ce9e605STiwei Bie 11931ce9e605STiwei Bie if (!vq->use_dma_api) 11941ce9e605STiwei Bie return; 11951ce9e605STiwei Bie 11961ce9e605STiwei Bie flags = le16_to_cpu(desc->flags); 11971ce9e605STiwei Bie 11981ce9e605STiwei Bie dma_unmap_page(vring_dma_dev(vq), 11991ce9e605STiwei Bie le64_to_cpu(desc->addr), 12001ce9e605STiwei Bie le32_to_cpu(desc->len), 12011ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 12021ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 12031ce9e605STiwei Bie } 12041ce9e605STiwei Bie 12051ce9e605STiwei Bie static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg, 12061ce9e605STiwei Bie gfp_t gfp) 12071ce9e605STiwei Bie { 12081ce9e605STiwei Bie struct vring_packed_desc *desc; 12091ce9e605STiwei Bie 12101ce9e605STiwei Bie /* 12111ce9e605STiwei Bie * We require lowmem mappings for the descriptors because 12121ce9e605STiwei Bie * otherwise virt_to_phys will give us bogus addresses in the 12131ce9e605STiwei Bie * virtqueue. 12141ce9e605STiwei Bie */ 12151ce9e605STiwei Bie gfp &= ~__GFP_HIGHMEM; 12161ce9e605STiwei Bie 12171ce9e605STiwei Bie desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp); 12181ce9e605STiwei Bie 12191ce9e605STiwei Bie return desc; 12201ce9e605STiwei Bie } 12211ce9e605STiwei Bie 12221ce9e605STiwei Bie static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, 12231ce9e605STiwei Bie struct scatterlist *sgs[], 12241ce9e605STiwei Bie unsigned int total_sg, 12251ce9e605STiwei Bie unsigned int out_sgs, 12261ce9e605STiwei Bie unsigned int in_sgs, 12271ce9e605STiwei Bie void *data, 12281ce9e605STiwei Bie gfp_t gfp) 12291ce9e605STiwei Bie { 12301ce9e605STiwei Bie struct vring_packed_desc *desc; 12311ce9e605STiwei Bie struct scatterlist *sg; 12321ce9e605STiwei Bie unsigned int i, n, err_idx; 12331ce9e605STiwei Bie u16 head, id; 12341ce9e605STiwei Bie dma_addr_t addr; 12351ce9e605STiwei Bie 12361ce9e605STiwei Bie head = vq->packed.next_avail_idx; 12371ce9e605STiwei Bie desc = alloc_indirect_packed(total_sg, gfp); 1238fc6d70f4SXuan Zhuo if (!desc) 1239fc6d70f4SXuan Zhuo return -ENOMEM; 12401ce9e605STiwei Bie 12411ce9e605STiwei Bie if (unlikely(vq->vq.num_free < 1)) { 12421ce9e605STiwei Bie pr_debug("Can't add buf len 1 - avail = 0\n"); 1243df0bfe75SYueHaibing kfree(desc); 12441ce9e605STiwei Bie END_USE(vq); 12451ce9e605STiwei Bie return -ENOSPC; 12461ce9e605STiwei Bie } 12471ce9e605STiwei Bie 12481ce9e605STiwei Bie i = 0; 12491ce9e605STiwei Bie id = vq->free_head; 12501ce9e605STiwei Bie BUG_ON(id == vq->packed.vring.num); 12511ce9e605STiwei Bie 12521ce9e605STiwei Bie for (n = 0; n < out_sgs + in_sgs; n++) { 12531ce9e605STiwei Bie for (sg = sgs[n]; sg; sg = sg_next(sg)) { 12541ce9e605STiwei Bie addr = vring_map_one_sg(vq, sg, n < out_sgs ? 12551ce9e605STiwei Bie DMA_TO_DEVICE : DMA_FROM_DEVICE); 12561ce9e605STiwei Bie if (vring_mapping_error(vq, addr)) 12571ce9e605STiwei Bie goto unmap_release; 12581ce9e605STiwei Bie 12591ce9e605STiwei Bie desc[i].flags = cpu_to_le16(n < out_sgs ? 12601ce9e605STiwei Bie 0 : VRING_DESC_F_WRITE); 12611ce9e605STiwei Bie desc[i].addr = cpu_to_le64(addr); 12621ce9e605STiwei Bie desc[i].len = cpu_to_le32(sg->length); 12631ce9e605STiwei Bie i++; 12641ce9e605STiwei Bie } 12651ce9e605STiwei Bie } 12661ce9e605STiwei Bie 12671ce9e605STiwei Bie /* Now that the indirect table is filled in, map it. */ 12681ce9e605STiwei Bie addr = vring_map_single(vq, desc, 12691ce9e605STiwei Bie total_sg * sizeof(struct vring_packed_desc), 12701ce9e605STiwei Bie DMA_TO_DEVICE); 12711ce9e605STiwei Bie if (vring_mapping_error(vq, addr)) 12721ce9e605STiwei Bie goto unmap_release; 12731ce9e605STiwei Bie 12741ce9e605STiwei Bie vq->packed.vring.desc[head].addr = cpu_to_le64(addr); 12751ce9e605STiwei Bie vq->packed.vring.desc[head].len = cpu_to_le32(total_sg * 12761ce9e605STiwei Bie sizeof(struct vring_packed_desc)); 12771ce9e605STiwei Bie vq->packed.vring.desc[head].id = cpu_to_le16(id); 12781ce9e605STiwei Bie 12791ce9e605STiwei Bie if (vq->use_dma_api) { 12801ce9e605STiwei Bie vq->packed.desc_extra[id].addr = addr; 12811ce9e605STiwei Bie vq->packed.desc_extra[id].len = total_sg * 12821ce9e605STiwei Bie sizeof(struct vring_packed_desc); 12831ce9e605STiwei Bie vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT | 12841ce9e605STiwei Bie vq->packed.avail_used_flags; 12851ce9e605STiwei Bie } 12861ce9e605STiwei Bie 12871ce9e605STiwei Bie /* 12881ce9e605STiwei Bie * A driver MUST NOT make the first descriptor in the list 12891ce9e605STiwei Bie * available before all subsequent descriptors comprising 12901ce9e605STiwei Bie * the list are made available. 12911ce9e605STiwei Bie */ 12921ce9e605STiwei Bie virtio_wmb(vq->weak_barriers); 12931ce9e605STiwei Bie vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT | 12941ce9e605STiwei Bie vq->packed.avail_used_flags); 12951ce9e605STiwei Bie 12961ce9e605STiwei Bie /* We're using some buffers from the free list. */ 12971ce9e605STiwei Bie vq->vq.num_free -= 1; 12981ce9e605STiwei Bie 12991ce9e605STiwei Bie /* Update free pointer */ 13001ce9e605STiwei Bie n = head + 1; 13011ce9e605STiwei Bie if (n >= vq->packed.vring.num) { 13021ce9e605STiwei Bie n = 0; 13031ce9e605STiwei Bie vq->packed.avail_wrap_counter ^= 1; 13041ce9e605STiwei Bie vq->packed.avail_used_flags ^= 13051ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_AVAIL | 13061ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_USED; 13071ce9e605STiwei Bie } 13081ce9e605STiwei Bie vq->packed.next_avail_idx = n; 1309aeef9b47SJason Wang vq->free_head = vq->packed.desc_extra[id].next; 13101ce9e605STiwei Bie 13111ce9e605STiwei Bie /* Store token and indirect buffer state. */ 13121ce9e605STiwei Bie vq->packed.desc_state[id].num = 1; 13131ce9e605STiwei Bie vq->packed.desc_state[id].data = data; 13141ce9e605STiwei Bie vq->packed.desc_state[id].indir_desc = desc; 13151ce9e605STiwei Bie vq->packed.desc_state[id].last = id; 13161ce9e605STiwei Bie 13171ce9e605STiwei Bie vq->num_added += 1; 13181ce9e605STiwei Bie 13191ce9e605STiwei Bie pr_debug("Added buffer head %i to %p\n", head, vq); 13201ce9e605STiwei Bie END_USE(vq); 13211ce9e605STiwei Bie 13221ce9e605STiwei Bie return 0; 13231ce9e605STiwei Bie 13241ce9e605STiwei Bie unmap_release: 13251ce9e605STiwei Bie err_idx = i; 13261ce9e605STiwei Bie 13271ce9e605STiwei Bie for (i = 0; i < err_idx; i++) 13281ce9e605STiwei Bie vring_unmap_desc_packed(vq, &desc[i]); 13291ce9e605STiwei Bie 13301ce9e605STiwei Bie kfree(desc); 13311ce9e605STiwei Bie 13321ce9e605STiwei Bie END_USE(vq); 1333f7728002SHalil Pasic return -ENOMEM; 13341ce9e605STiwei Bie } 13351ce9e605STiwei Bie 13361ce9e605STiwei Bie static inline int virtqueue_add_packed(struct virtqueue *_vq, 13371ce9e605STiwei Bie struct scatterlist *sgs[], 13381ce9e605STiwei Bie unsigned int total_sg, 13391ce9e605STiwei Bie unsigned int out_sgs, 13401ce9e605STiwei Bie unsigned int in_sgs, 13411ce9e605STiwei Bie void *data, 13421ce9e605STiwei Bie void *ctx, 13431ce9e605STiwei Bie gfp_t gfp) 13441ce9e605STiwei Bie { 13451ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 13461ce9e605STiwei Bie struct vring_packed_desc *desc; 13471ce9e605STiwei Bie struct scatterlist *sg; 13481ce9e605STiwei Bie unsigned int i, n, c, descs_used, err_idx; 13493f649ab7SKees Cook __le16 head_flags, flags; 13503f649ab7SKees Cook u16 head, id, prev, curr, avail_used_flags; 1351fc6d70f4SXuan Zhuo int err; 13521ce9e605STiwei Bie 13531ce9e605STiwei Bie START_USE(vq); 13541ce9e605STiwei Bie 13551ce9e605STiwei Bie BUG_ON(data == NULL); 13561ce9e605STiwei Bie BUG_ON(ctx && vq->indirect); 13571ce9e605STiwei Bie 13581ce9e605STiwei Bie if (unlikely(vq->broken)) { 13591ce9e605STiwei Bie END_USE(vq); 13601ce9e605STiwei Bie return -EIO; 13611ce9e605STiwei Bie } 13621ce9e605STiwei Bie 13631ce9e605STiwei Bie LAST_ADD_TIME_UPDATE(vq); 13641ce9e605STiwei Bie 13651ce9e605STiwei Bie BUG_ON(total_sg == 0); 13661ce9e605STiwei Bie 136735c51e09SXianting Tian if (virtqueue_use_indirect(vq, total_sg)) { 1368fc6d70f4SXuan Zhuo err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs, 1369fc6d70f4SXuan Zhuo in_sgs, data, gfp); 13701861ba62SMichael S. Tsirkin if (err != -ENOMEM) { 13711861ba62SMichael S. Tsirkin END_USE(vq); 1372fc6d70f4SXuan Zhuo return err; 13731861ba62SMichael S. Tsirkin } 1374fc6d70f4SXuan Zhuo 1375fc6d70f4SXuan Zhuo /* fall back on direct */ 1376fc6d70f4SXuan Zhuo } 13771ce9e605STiwei Bie 13781ce9e605STiwei Bie head = vq->packed.next_avail_idx; 13791ce9e605STiwei Bie avail_used_flags = vq->packed.avail_used_flags; 13801ce9e605STiwei Bie 13811ce9e605STiwei Bie WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect); 13821ce9e605STiwei Bie 13831ce9e605STiwei Bie desc = vq->packed.vring.desc; 13841ce9e605STiwei Bie i = head; 13851ce9e605STiwei Bie descs_used = total_sg; 13861ce9e605STiwei Bie 13871ce9e605STiwei Bie if (unlikely(vq->vq.num_free < descs_used)) { 13881ce9e605STiwei Bie pr_debug("Can't add buf len %i - avail = %i\n", 13891ce9e605STiwei Bie descs_used, vq->vq.num_free); 13901ce9e605STiwei Bie END_USE(vq); 13911ce9e605STiwei Bie return -ENOSPC; 13921ce9e605STiwei Bie } 13931ce9e605STiwei Bie 13941ce9e605STiwei Bie id = vq->free_head; 13951ce9e605STiwei Bie BUG_ON(id == vq->packed.vring.num); 13961ce9e605STiwei Bie 13971ce9e605STiwei Bie curr = id; 13981ce9e605STiwei Bie c = 0; 13991ce9e605STiwei Bie for (n = 0; n < out_sgs + in_sgs; n++) { 14001ce9e605STiwei Bie for (sg = sgs[n]; sg; sg = sg_next(sg)) { 14011ce9e605STiwei Bie dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ? 14021ce9e605STiwei Bie DMA_TO_DEVICE : DMA_FROM_DEVICE); 14031ce9e605STiwei Bie if (vring_mapping_error(vq, addr)) 14041ce9e605STiwei Bie goto unmap_release; 14051ce9e605STiwei Bie 14061ce9e605STiwei Bie flags = cpu_to_le16(vq->packed.avail_used_flags | 14071ce9e605STiwei Bie (++c == total_sg ? 0 : VRING_DESC_F_NEXT) | 14081ce9e605STiwei Bie (n < out_sgs ? 0 : VRING_DESC_F_WRITE)); 14091ce9e605STiwei Bie if (i == head) 14101ce9e605STiwei Bie head_flags = flags; 14111ce9e605STiwei Bie else 14121ce9e605STiwei Bie desc[i].flags = flags; 14131ce9e605STiwei Bie 14141ce9e605STiwei Bie desc[i].addr = cpu_to_le64(addr); 14151ce9e605STiwei Bie desc[i].len = cpu_to_le32(sg->length); 14161ce9e605STiwei Bie desc[i].id = cpu_to_le16(id); 14171ce9e605STiwei Bie 14181ce9e605STiwei Bie if (unlikely(vq->use_dma_api)) { 14191ce9e605STiwei Bie vq->packed.desc_extra[curr].addr = addr; 14201ce9e605STiwei Bie vq->packed.desc_extra[curr].len = sg->length; 14211ce9e605STiwei Bie vq->packed.desc_extra[curr].flags = 14221ce9e605STiwei Bie le16_to_cpu(flags); 14231ce9e605STiwei Bie } 14241ce9e605STiwei Bie prev = curr; 1425aeef9b47SJason Wang curr = vq->packed.desc_extra[curr].next; 14261ce9e605STiwei Bie 14271ce9e605STiwei Bie if ((unlikely(++i >= vq->packed.vring.num))) { 14281ce9e605STiwei Bie i = 0; 14291ce9e605STiwei Bie vq->packed.avail_used_flags ^= 14301ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_AVAIL | 14311ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_USED; 14321ce9e605STiwei Bie } 14331ce9e605STiwei Bie } 14341ce9e605STiwei Bie } 14351ce9e605STiwei Bie 14361ce9e605STiwei Bie if (i < head) 14371ce9e605STiwei Bie vq->packed.avail_wrap_counter ^= 1; 14381ce9e605STiwei Bie 14391ce9e605STiwei Bie /* We're using some buffers from the free list. */ 14401ce9e605STiwei Bie vq->vq.num_free -= descs_used; 14411ce9e605STiwei Bie 14421ce9e605STiwei Bie /* Update free pointer */ 14431ce9e605STiwei Bie vq->packed.next_avail_idx = i; 14441ce9e605STiwei Bie vq->free_head = curr; 14451ce9e605STiwei Bie 14461ce9e605STiwei Bie /* Store token. */ 14471ce9e605STiwei Bie vq->packed.desc_state[id].num = descs_used; 14481ce9e605STiwei Bie vq->packed.desc_state[id].data = data; 14491ce9e605STiwei Bie vq->packed.desc_state[id].indir_desc = ctx; 14501ce9e605STiwei Bie vq->packed.desc_state[id].last = prev; 14511ce9e605STiwei Bie 14521ce9e605STiwei Bie /* 14531ce9e605STiwei Bie * A driver MUST NOT make the first descriptor in the list 14541ce9e605STiwei Bie * available before all subsequent descriptors comprising 14551ce9e605STiwei Bie * the list are made available. 14561ce9e605STiwei Bie */ 14571ce9e605STiwei Bie virtio_wmb(vq->weak_barriers); 14581ce9e605STiwei Bie vq->packed.vring.desc[head].flags = head_flags; 14591ce9e605STiwei Bie vq->num_added += descs_used; 14601ce9e605STiwei Bie 14611ce9e605STiwei Bie pr_debug("Added buffer head %i to %p\n", head, vq); 14621ce9e605STiwei Bie END_USE(vq); 14631ce9e605STiwei Bie 14641ce9e605STiwei Bie return 0; 14651ce9e605STiwei Bie 14661ce9e605STiwei Bie unmap_release: 14671ce9e605STiwei Bie err_idx = i; 14681ce9e605STiwei Bie i = head; 146944593865SJason Wang curr = vq->free_head; 14701ce9e605STiwei Bie 14711ce9e605STiwei Bie vq->packed.avail_used_flags = avail_used_flags; 14721ce9e605STiwei Bie 14731ce9e605STiwei Bie for (n = 0; n < total_sg; n++) { 14741ce9e605STiwei Bie if (i == err_idx) 14751ce9e605STiwei Bie break; 1476d80dc15bSXuan Zhuo vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]); 147744593865SJason Wang curr = vq->packed.desc_extra[curr].next; 14781ce9e605STiwei Bie i++; 14791ce9e605STiwei Bie if (i >= vq->packed.vring.num) 14801ce9e605STiwei Bie i = 0; 14811ce9e605STiwei Bie } 14821ce9e605STiwei Bie 14831ce9e605STiwei Bie END_USE(vq); 14841ce9e605STiwei Bie return -EIO; 14851ce9e605STiwei Bie } 14861ce9e605STiwei Bie 14871ce9e605STiwei Bie static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq) 14881ce9e605STiwei Bie { 14891ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 1490f51f9826STiwei Bie u16 new, old, off_wrap, flags, wrap_counter, event_idx; 14911ce9e605STiwei Bie bool needs_kick; 14921ce9e605STiwei Bie union { 14931ce9e605STiwei Bie struct { 14941ce9e605STiwei Bie __le16 off_wrap; 14951ce9e605STiwei Bie __le16 flags; 14961ce9e605STiwei Bie }; 14971ce9e605STiwei Bie u32 u32; 14981ce9e605STiwei Bie } snapshot; 14991ce9e605STiwei Bie 15001ce9e605STiwei Bie START_USE(vq); 15011ce9e605STiwei Bie 15021ce9e605STiwei Bie /* 15031ce9e605STiwei Bie * We need to expose the new flags value before checking notification 15041ce9e605STiwei Bie * suppressions. 15051ce9e605STiwei Bie */ 15061ce9e605STiwei Bie virtio_mb(vq->weak_barriers); 15071ce9e605STiwei Bie 1508f51f9826STiwei Bie old = vq->packed.next_avail_idx - vq->num_added; 1509f51f9826STiwei Bie new = vq->packed.next_avail_idx; 15101ce9e605STiwei Bie vq->num_added = 0; 15111ce9e605STiwei Bie 15121ce9e605STiwei Bie snapshot.u32 = *(u32 *)vq->packed.vring.device; 15131ce9e605STiwei Bie flags = le16_to_cpu(snapshot.flags); 15141ce9e605STiwei Bie 15151ce9e605STiwei Bie LAST_ADD_TIME_CHECK(vq); 15161ce9e605STiwei Bie LAST_ADD_TIME_INVALID(vq); 15171ce9e605STiwei Bie 1518f51f9826STiwei Bie if (flags != VRING_PACKED_EVENT_FLAG_DESC) { 15191ce9e605STiwei Bie needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE); 1520f51f9826STiwei Bie goto out; 1521f51f9826STiwei Bie } 1522f51f9826STiwei Bie 1523f51f9826STiwei Bie off_wrap = le16_to_cpu(snapshot.off_wrap); 1524f51f9826STiwei Bie 1525f51f9826STiwei Bie wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; 1526f51f9826STiwei Bie event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); 1527f51f9826STiwei Bie if (wrap_counter != vq->packed.avail_wrap_counter) 1528f51f9826STiwei Bie event_idx -= vq->packed.vring.num; 1529f51f9826STiwei Bie 1530f51f9826STiwei Bie needs_kick = vring_need_event(event_idx, new, old); 1531f51f9826STiwei Bie out: 15321ce9e605STiwei Bie END_USE(vq); 15331ce9e605STiwei Bie return needs_kick; 15341ce9e605STiwei Bie } 15351ce9e605STiwei Bie 15361ce9e605STiwei Bie static void detach_buf_packed(struct vring_virtqueue *vq, 15371ce9e605STiwei Bie unsigned int id, void **ctx) 15381ce9e605STiwei Bie { 15391ce9e605STiwei Bie struct vring_desc_state_packed *state = NULL; 15401ce9e605STiwei Bie struct vring_packed_desc *desc; 15411ce9e605STiwei Bie unsigned int i, curr; 15421ce9e605STiwei Bie 15431ce9e605STiwei Bie state = &vq->packed.desc_state[id]; 15441ce9e605STiwei Bie 15451ce9e605STiwei Bie /* Clear data ptr. */ 15461ce9e605STiwei Bie state->data = NULL; 15471ce9e605STiwei Bie 1548aeef9b47SJason Wang vq->packed.desc_extra[state->last].next = vq->free_head; 15491ce9e605STiwei Bie vq->free_head = id; 15501ce9e605STiwei Bie vq->vq.num_free += state->num; 15511ce9e605STiwei Bie 15521ce9e605STiwei Bie if (unlikely(vq->use_dma_api)) { 15531ce9e605STiwei Bie curr = id; 15541ce9e605STiwei Bie for (i = 0; i < state->num; i++) { 1555d80dc15bSXuan Zhuo vring_unmap_extra_packed(vq, 15561ce9e605STiwei Bie &vq->packed.desc_extra[curr]); 1557aeef9b47SJason Wang curr = vq->packed.desc_extra[curr].next; 15581ce9e605STiwei Bie } 15591ce9e605STiwei Bie } 15601ce9e605STiwei Bie 15611ce9e605STiwei Bie if (vq->indirect) { 15621ce9e605STiwei Bie u32 len; 15631ce9e605STiwei Bie 15641ce9e605STiwei Bie /* Free the indirect table, if any, now that it's unmapped. */ 15651ce9e605STiwei Bie desc = state->indir_desc; 15661ce9e605STiwei Bie if (!desc) 15671ce9e605STiwei Bie return; 15681ce9e605STiwei Bie 15691ce9e605STiwei Bie if (vq->use_dma_api) { 15701ce9e605STiwei Bie len = vq->packed.desc_extra[id].len; 15711ce9e605STiwei Bie for (i = 0; i < len / sizeof(struct vring_packed_desc); 15721ce9e605STiwei Bie i++) 15731ce9e605STiwei Bie vring_unmap_desc_packed(vq, &desc[i]); 15741ce9e605STiwei Bie } 15751ce9e605STiwei Bie kfree(desc); 15761ce9e605STiwei Bie state->indir_desc = NULL; 15771ce9e605STiwei Bie } else if (ctx) { 15781ce9e605STiwei Bie *ctx = state->indir_desc; 15791ce9e605STiwei Bie } 15801ce9e605STiwei Bie } 15811ce9e605STiwei Bie 15821ce9e605STiwei Bie static inline bool is_used_desc_packed(const struct vring_virtqueue *vq, 15831ce9e605STiwei Bie u16 idx, bool used_wrap_counter) 15841ce9e605STiwei Bie { 15851ce9e605STiwei Bie bool avail, used; 15861ce9e605STiwei Bie u16 flags; 15871ce9e605STiwei Bie 15881ce9e605STiwei Bie flags = le16_to_cpu(vq->packed.vring.desc[idx].flags); 15891ce9e605STiwei Bie avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL)); 15901ce9e605STiwei Bie used = !!(flags & (1 << VRING_PACKED_DESC_F_USED)); 15911ce9e605STiwei Bie 15921ce9e605STiwei Bie return avail == used && used == used_wrap_counter; 15931ce9e605STiwei Bie } 15941ce9e605STiwei Bie 15951ce9e605STiwei Bie static inline bool more_used_packed(const struct vring_virtqueue *vq) 15961ce9e605STiwei Bie { 1597a7722890Shuangjie.albert u16 last_used; 1598a7722890Shuangjie.albert u16 last_used_idx; 1599a7722890Shuangjie.albert bool used_wrap_counter; 1600a7722890Shuangjie.albert 1601a7722890Shuangjie.albert last_used_idx = READ_ONCE(vq->last_used_idx); 1602a7722890Shuangjie.albert last_used = packed_last_used(last_used_idx); 1603a7722890Shuangjie.albert used_wrap_counter = packed_used_wrap_counter(last_used_idx); 1604a7722890Shuangjie.albert return is_used_desc_packed(vq, last_used, used_wrap_counter); 16051ce9e605STiwei Bie } 16061ce9e605STiwei Bie 16071ce9e605STiwei Bie static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq, 16081ce9e605STiwei Bie unsigned int *len, 16091ce9e605STiwei Bie void **ctx) 16101ce9e605STiwei Bie { 16111ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 1612a7722890Shuangjie.albert u16 last_used, id, last_used_idx; 1613a7722890Shuangjie.albert bool used_wrap_counter; 16141ce9e605STiwei Bie void *ret; 16151ce9e605STiwei Bie 16161ce9e605STiwei Bie START_USE(vq); 16171ce9e605STiwei Bie 16181ce9e605STiwei Bie if (unlikely(vq->broken)) { 16191ce9e605STiwei Bie END_USE(vq); 16201ce9e605STiwei Bie return NULL; 16211ce9e605STiwei Bie } 16221ce9e605STiwei Bie 16231ce9e605STiwei Bie if (!more_used_packed(vq)) { 16241ce9e605STiwei Bie pr_debug("No more buffers in queue\n"); 16251ce9e605STiwei Bie END_USE(vq); 16261ce9e605STiwei Bie return NULL; 16271ce9e605STiwei Bie } 16281ce9e605STiwei Bie 16291ce9e605STiwei Bie /* Only get used elements after they have been exposed by host. */ 16301ce9e605STiwei Bie virtio_rmb(vq->weak_barriers); 16311ce9e605STiwei Bie 1632a7722890Shuangjie.albert last_used_idx = READ_ONCE(vq->last_used_idx); 1633a7722890Shuangjie.albert used_wrap_counter = packed_used_wrap_counter(last_used_idx); 1634a7722890Shuangjie.albert last_used = packed_last_used(last_used_idx); 16351ce9e605STiwei Bie id = le16_to_cpu(vq->packed.vring.desc[last_used].id); 16361ce9e605STiwei Bie *len = le32_to_cpu(vq->packed.vring.desc[last_used].len); 16371ce9e605STiwei Bie 16381ce9e605STiwei Bie if (unlikely(id >= vq->packed.vring.num)) { 16391ce9e605STiwei Bie BAD_RING(vq, "id %u out of range\n", id); 16401ce9e605STiwei Bie return NULL; 16411ce9e605STiwei Bie } 16421ce9e605STiwei Bie if (unlikely(!vq->packed.desc_state[id].data)) { 16431ce9e605STiwei Bie BAD_RING(vq, "id %u is not a head!\n", id); 16441ce9e605STiwei Bie return NULL; 16451ce9e605STiwei Bie } 16461ce9e605STiwei Bie 16471ce9e605STiwei Bie /* detach_buf_packed clears data, so grab it now. */ 16481ce9e605STiwei Bie ret = vq->packed.desc_state[id].data; 16491ce9e605STiwei Bie detach_buf_packed(vq, id, ctx); 16501ce9e605STiwei Bie 1651a7722890Shuangjie.albert last_used += vq->packed.desc_state[id].num; 1652a7722890Shuangjie.albert if (unlikely(last_used >= vq->packed.vring.num)) { 1653a7722890Shuangjie.albert last_used -= vq->packed.vring.num; 1654a7722890Shuangjie.albert used_wrap_counter ^= 1; 16551ce9e605STiwei Bie } 16561ce9e605STiwei Bie 1657a7722890Shuangjie.albert last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR)); 1658a7722890Shuangjie.albert WRITE_ONCE(vq->last_used_idx, last_used); 1659a7722890Shuangjie.albert 1660f51f9826STiwei Bie /* 1661f51f9826STiwei Bie * If we expect an interrupt for the next entry, tell host 1662f51f9826STiwei Bie * by writing event index and flush out the write before 1663f51f9826STiwei Bie * the read in the next get_buf call. 1664f51f9826STiwei Bie */ 1665f51f9826STiwei Bie if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC) 1666f51f9826STiwei Bie virtio_store_mb(vq->weak_barriers, 1667f51f9826STiwei Bie &vq->packed.vring.driver->off_wrap, 1668a7722890Shuangjie.albert cpu_to_le16(vq->last_used_idx)); 1669f51f9826STiwei Bie 16701ce9e605STiwei Bie LAST_ADD_TIME_INVALID(vq); 16711ce9e605STiwei Bie 16721ce9e605STiwei Bie END_USE(vq); 16731ce9e605STiwei Bie return ret; 16741ce9e605STiwei Bie } 16751ce9e605STiwei Bie 16761ce9e605STiwei Bie static void virtqueue_disable_cb_packed(struct virtqueue *_vq) 16771ce9e605STiwei Bie { 16781ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 16791ce9e605STiwei Bie 16801ce9e605STiwei Bie if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) { 16811ce9e605STiwei Bie vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; 16821ce9e605STiwei Bie vq->packed.vring.driver->flags = 16831ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 16841ce9e605STiwei Bie } 16851ce9e605STiwei Bie } 16861ce9e605STiwei Bie 168731532340SSolomon Tan static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq) 16881ce9e605STiwei Bie { 16891ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 16901ce9e605STiwei Bie 16911ce9e605STiwei Bie START_USE(vq); 16921ce9e605STiwei Bie 16931ce9e605STiwei Bie /* 16941ce9e605STiwei Bie * We optimistically turn back on interrupts, then check if there was 16951ce9e605STiwei Bie * more to do. 16961ce9e605STiwei Bie */ 16971ce9e605STiwei Bie 1698f51f9826STiwei Bie if (vq->event) { 1699f51f9826STiwei Bie vq->packed.vring.driver->off_wrap = 1700a7722890Shuangjie.albert cpu_to_le16(vq->last_used_idx); 1701f51f9826STiwei Bie /* 1702f51f9826STiwei Bie * We need to update event offset and event wrap 1703f51f9826STiwei Bie * counter first before updating event flags. 1704f51f9826STiwei Bie */ 1705f51f9826STiwei Bie virtio_wmb(vq->weak_barriers); 1706f51f9826STiwei Bie } 1707f51f9826STiwei Bie 17081ce9e605STiwei Bie if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { 1709f51f9826STiwei Bie vq->packed.event_flags_shadow = vq->event ? 1710f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_DESC : 1711f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_ENABLE; 17121ce9e605STiwei Bie vq->packed.vring.driver->flags = 17131ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 17141ce9e605STiwei Bie } 17151ce9e605STiwei Bie 17161ce9e605STiwei Bie END_USE(vq); 1717a7722890Shuangjie.albert return vq->last_used_idx; 17181ce9e605STiwei Bie } 17191ce9e605STiwei Bie 17201ce9e605STiwei Bie static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap) 17211ce9e605STiwei Bie { 17221ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 17231ce9e605STiwei Bie bool wrap_counter; 17241ce9e605STiwei Bie u16 used_idx; 17251ce9e605STiwei Bie 17261ce9e605STiwei Bie wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; 17271ce9e605STiwei Bie used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); 17281ce9e605STiwei Bie 17291ce9e605STiwei Bie return is_used_desc_packed(vq, used_idx, wrap_counter); 17301ce9e605STiwei Bie } 17311ce9e605STiwei Bie 17321ce9e605STiwei Bie static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq) 17331ce9e605STiwei Bie { 17341ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 1735a7722890Shuangjie.albert u16 used_idx, wrap_counter, last_used_idx; 1736f51f9826STiwei Bie u16 bufs; 17371ce9e605STiwei Bie 17381ce9e605STiwei Bie START_USE(vq); 17391ce9e605STiwei Bie 17401ce9e605STiwei Bie /* 17411ce9e605STiwei Bie * We optimistically turn back on interrupts, then check if there was 17421ce9e605STiwei Bie * more to do. 17431ce9e605STiwei Bie */ 17441ce9e605STiwei Bie 1745f51f9826STiwei Bie if (vq->event) { 1746f51f9826STiwei Bie /* TODO: tune this threshold */ 1747f51f9826STiwei Bie bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4; 1748a7722890Shuangjie.albert last_used_idx = READ_ONCE(vq->last_used_idx); 1749a7722890Shuangjie.albert wrap_counter = packed_used_wrap_counter(last_used_idx); 17501ce9e605STiwei Bie 1751a7722890Shuangjie.albert used_idx = packed_last_used(last_used_idx) + bufs; 1752f51f9826STiwei Bie if (used_idx >= vq->packed.vring.num) { 1753f51f9826STiwei Bie used_idx -= vq->packed.vring.num; 1754f51f9826STiwei Bie wrap_counter ^= 1; 1755f51f9826STiwei Bie } 1756f51f9826STiwei Bie 1757f51f9826STiwei Bie vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx | 1758f51f9826STiwei Bie (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR)); 1759f51f9826STiwei Bie 1760f51f9826STiwei Bie /* 1761f51f9826STiwei Bie * We need to update event offset and event wrap 1762f51f9826STiwei Bie * counter first before updating event flags. 1763f51f9826STiwei Bie */ 1764f51f9826STiwei Bie virtio_wmb(vq->weak_barriers); 1765f51f9826STiwei Bie } 1766f51f9826STiwei Bie 17671ce9e605STiwei Bie if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { 1768f51f9826STiwei Bie vq->packed.event_flags_shadow = vq->event ? 1769f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_DESC : 1770f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_ENABLE; 17711ce9e605STiwei Bie vq->packed.vring.driver->flags = 17721ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 17731ce9e605STiwei Bie } 17741ce9e605STiwei Bie 17751ce9e605STiwei Bie /* 17761ce9e605STiwei Bie * We need to update event suppression structure first 17771ce9e605STiwei Bie * before re-checking for more used buffers. 17781ce9e605STiwei Bie */ 17791ce9e605STiwei Bie virtio_mb(vq->weak_barriers); 17801ce9e605STiwei Bie 1781a7722890Shuangjie.albert last_used_idx = READ_ONCE(vq->last_used_idx); 1782a7722890Shuangjie.albert wrap_counter = packed_used_wrap_counter(last_used_idx); 1783a7722890Shuangjie.albert used_idx = packed_last_used(last_used_idx); 1784a7722890Shuangjie.albert if (is_used_desc_packed(vq, used_idx, wrap_counter)) { 17851ce9e605STiwei Bie END_USE(vq); 17861ce9e605STiwei Bie return false; 17871ce9e605STiwei Bie } 17881ce9e605STiwei Bie 17891ce9e605STiwei Bie END_USE(vq); 17901ce9e605STiwei Bie return true; 17911ce9e605STiwei Bie } 17921ce9e605STiwei Bie 17931ce9e605STiwei Bie static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq) 17941ce9e605STiwei Bie { 17951ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 17961ce9e605STiwei Bie unsigned int i; 17971ce9e605STiwei Bie void *buf; 17981ce9e605STiwei Bie 17991ce9e605STiwei Bie START_USE(vq); 18001ce9e605STiwei Bie 18011ce9e605STiwei Bie for (i = 0; i < vq->packed.vring.num; i++) { 18021ce9e605STiwei Bie if (!vq->packed.desc_state[i].data) 18031ce9e605STiwei Bie continue; 18041ce9e605STiwei Bie /* detach_buf clears data, so grab it now. */ 18051ce9e605STiwei Bie buf = vq->packed.desc_state[i].data; 18061ce9e605STiwei Bie detach_buf_packed(vq, i, NULL); 18071ce9e605STiwei Bie END_USE(vq); 18081ce9e605STiwei Bie return buf; 18091ce9e605STiwei Bie } 18101ce9e605STiwei Bie /* That should have freed everything. */ 18111ce9e605STiwei Bie BUG_ON(vq->vq.num_free != vq->packed.vring.num); 18121ce9e605STiwei Bie 18131ce9e605STiwei Bie END_USE(vq); 18141ce9e605STiwei Bie return NULL; 18151ce9e605STiwei Bie } 18161ce9e605STiwei Bie 181796ef18a2SXuan Zhuo static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num) 18185a222421SJason Wang { 18195a222421SJason Wang struct vring_desc_extra *desc_extra; 18205a222421SJason Wang unsigned int i; 18215a222421SJason Wang 18225a222421SJason Wang desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra), 18235a222421SJason Wang GFP_KERNEL); 18245a222421SJason Wang if (!desc_extra) 18255a222421SJason Wang return NULL; 18265a222421SJason Wang 18275a222421SJason Wang memset(desc_extra, 0, num * sizeof(struct vring_desc_extra)); 18285a222421SJason Wang 18295a222421SJason Wang for (i = 0; i < num - 1; i++) 18305a222421SJason Wang desc_extra[i].next = i + 1; 18315a222421SJason Wang 18325a222421SJason Wang return desc_extra; 18335a222421SJason Wang } 18345a222421SJason Wang 1835*6356f8bbSXuan Zhuo static void vring_free_packed(struct vring_virtqueue_packed *vring_packed, 1836*6356f8bbSXuan Zhuo struct virtio_device *vdev) 1837*6356f8bbSXuan Zhuo { 1838*6356f8bbSXuan Zhuo if (vring_packed->vring.desc) 1839*6356f8bbSXuan Zhuo vring_free_queue(vdev, vring_packed->ring_size_in_bytes, 1840*6356f8bbSXuan Zhuo vring_packed->vring.desc, 1841*6356f8bbSXuan Zhuo vring_packed->ring_dma_addr); 1842*6356f8bbSXuan Zhuo 1843*6356f8bbSXuan Zhuo if (vring_packed->vring.driver) 1844*6356f8bbSXuan Zhuo vring_free_queue(vdev, vring_packed->event_size_in_bytes, 1845*6356f8bbSXuan Zhuo vring_packed->vring.driver, 1846*6356f8bbSXuan Zhuo vring_packed->driver_event_dma_addr); 1847*6356f8bbSXuan Zhuo 1848*6356f8bbSXuan Zhuo if (vring_packed->vring.device) 1849*6356f8bbSXuan Zhuo vring_free_queue(vdev, vring_packed->event_size_in_bytes, 1850*6356f8bbSXuan Zhuo vring_packed->vring.device, 1851*6356f8bbSXuan Zhuo vring_packed->device_event_dma_addr); 1852*6356f8bbSXuan Zhuo 1853*6356f8bbSXuan Zhuo kfree(vring_packed->desc_state); 1854*6356f8bbSXuan Zhuo kfree(vring_packed->desc_extra); 1855*6356f8bbSXuan Zhuo } 1856*6356f8bbSXuan Zhuo 18571ce9e605STiwei Bie static struct virtqueue *vring_create_virtqueue_packed( 18581ce9e605STiwei Bie unsigned int index, 18591ce9e605STiwei Bie unsigned int num, 18601ce9e605STiwei Bie unsigned int vring_align, 18611ce9e605STiwei Bie struct virtio_device *vdev, 18621ce9e605STiwei Bie bool weak_barriers, 18631ce9e605STiwei Bie bool may_reduce_num, 18641ce9e605STiwei Bie bool context, 18651ce9e605STiwei Bie bool (*notify)(struct virtqueue *), 18661ce9e605STiwei Bie void (*callback)(struct virtqueue *), 18671ce9e605STiwei Bie const char *name) 18681ce9e605STiwei Bie { 18691ce9e605STiwei Bie struct vring_virtqueue *vq; 18701ce9e605STiwei Bie struct vring_packed_desc *ring; 18711ce9e605STiwei Bie struct vring_packed_desc_event *driver, *device; 18721ce9e605STiwei Bie dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr; 18731ce9e605STiwei Bie size_t ring_size_in_bytes, event_size_in_bytes; 18741ce9e605STiwei Bie 18751ce9e605STiwei Bie ring_size_in_bytes = num * sizeof(struct vring_packed_desc); 18761ce9e605STiwei Bie 18771ce9e605STiwei Bie ring = vring_alloc_queue(vdev, ring_size_in_bytes, 18781ce9e605STiwei Bie &ring_dma_addr, 18791ce9e605STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 18801ce9e605STiwei Bie if (!ring) 18811ce9e605STiwei Bie goto err_ring; 18821ce9e605STiwei Bie 18831ce9e605STiwei Bie event_size_in_bytes = sizeof(struct vring_packed_desc_event); 18841ce9e605STiwei Bie 18851ce9e605STiwei Bie driver = vring_alloc_queue(vdev, event_size_in_bytes, 18861ce9e605STiwei Bie &driver_event_dma_addr, 18871ce9e605STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 18881ce9e605STiwei Bie if (!driver) 18891ce9e605STiwei Bie goto err_driver; 18901ce9e605STiwei Bie 18911ce9e605STiwei Bie device = vring_alloc_queue(vdev, event_size_in_bytes, 18921ce9e605STiwei Bie &device_event_dma_addr, 18931ce9e605STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 18941ce9e605STiwei Bie if (!device) 18951ce9e605STiwei Bie goto err_device; 18961ce9e605STiwei Bie 18971ce9e605STiwei Bie vq = kmalloc(sizeof(*vq), GFP_KERNEL); 18981ce9e605STiwei Bie if (!vq) 18991ce9e605STiwei Bie goto err_vq; 19001ce9e605STiwei Bie 19011ce9e605STiwei Bie vq->vq.callback = callback; 19021ce9e605STiwei Bie vq->vq.vdev = vdev; 19031ce9e605STiwei Bie vq->vq.name = name; 19041ce9e605STiwei Bie vq->vq.index = index; 19051ce9e605STiwei Bie vq->we_own_ring = true; 19061ce9e605STiwei Bie vq->notify = notify; 19071ce9e605STiwei Bie vq->weak_barriers = weak_barriers; 1908c346dae4SJason Wang #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION 19098b4ec69dSJason Wang vq->broken = true; 1910c346dae4SJason Wang #else 1911c346dae4SJason Wang vq->broken = false; 1912c346dae4SJason Wang #endif 19131ce9e605STiwei Bie vq->packed_ring = true; 19141ce9e605STiwei Bie vq->use_dma_api = vring_use_dma_api(vdev); 19151ce9e605STiwei Bie 19161ce9e605STiwei Bie vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && 19171ce9e605STiwei Bie !context; 19181ce9e605STiwei Bie vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 19191ce9e605STiwei Bie 192045383fb0STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) 192145383fb0STiwei Bie vq->weak_barriers = false; 192245383fb0STiwei Bie 19231ce9e605STiwei Bie vq->packed.ring_dma_addr = ring_dma_addr; 19241ce9e605STiwei Bie vq->packed.driver_event_dma_addr = driver_event_dma_addr; 19251ce9e605STiwei Bie vq->packed.device_event_dma_addr = device_event_dma_addr; 19261ce9e605STiwei Bie 19271ce9e605STiwei Bie vq->packed.ring_size_in_bytes = ring_size_in_bytes; 19281ce9e605STiwei Bie vq->packed.event_size_in_bytes = event_size_in_bytes; 19291ce9e605STiwei Bie 19301ce9e605STiwei Bie vq->packed.vring.num = num; 19311ce9e605STiwei Bie vq->packed.vring.desc = ring; 19321ce9e605STiwei Bie vq->packed.vring.driver = driver; 19331ce9e605STiwei Bie vq->packed.vring.device = device; 19341ce9e605STiwei Bie 19351ce9e605STiwei Bie vq->packed.next_avail_idx = 0; 19361ce9e605STiwei Bie vq->packed.avail_wrap_counter = 1; 19371ce9e605STiwei Bie vq->packed.event_flags_shadow = 0; 19381ce9e605STiwei Bie vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL; 19391ce9e605STiwei Bie 19401ce9e605STiwei Bie vq->packed.desc_state = kmalloc_array(num, 19411ce9e605STiwei Bie sizeof(struct vring_desc_state_packed), 19421ce9e605STiwei Bie GFP_KERNEL); 19431ce9e605STiwei Bie if (!vq->packed.desc_state) 19441ce9e605STiwei Bie goto err_desc_state; 19451ce9e605STiwei Bie 19461ce9e605STiwei Bie memset(vq->packed.desc_state, 0, 19471ce9e605STiwei Bie num * sizeof(struct vring_desc_state_packed)); 19481ce9e605STiwei Bie 19491ce9e605STiwei Bie /* Put everything in free lists. */ 19501ce9e605STiwei Bie vq->free_head = 0; 19511ce9e605STiwei Bie 195296ef18a2SXuan Zhuo vq->packed.desc_extra = vring_alloc_desc_extra(num); 19531ce9e605STiwei Bie if (!vq->packed.desc_extra) 19541ce9e605STiwei Bie goto err_desc_extra; 19551ce9e605STiwei Bie 19561ce9e605STiwei Bie /* No callback? Tell other side not to bother us. */ 19571ce9e605STiwei Bie if (!callback) { 19581ce9e605STiwei Bie vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; 19591ce9e605STiwei Bie vq->packed.vring.driver->flags = 19601ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 19611ce9e605STiwei Bie } 19621ce9e605STiwei Bie 19633a897128SXuan Zhuo virtqueue_init(vq, num); 19643a897128SXuan Zhuo 19650e566c8fSParav Pandit spin_lock(&vdev->vqs_list_lock); 1966e152d8afSDan Carpenter list_add_tail(&vq->vq.list, &vdev->vqs); 19670e566c8fSParav Pandit spin_unlock(&vdev->vqs_list_lock); 19681ce9e605STiwei Bie return &vq->vq; 19691ce9e605STiwei Bie 19701ce9e605STiwei Bie err_desc_extra: 19711ce9e605STiwei Bie kfree(vq->packed.desc_state); 19721ce9e605STiwei Bie err_desc_state: 19731ce9e605STiwei Bie kfree(vq); 19741ce9e605STiwei Bie err_vq: 1975ae93d8eaSDan Carpenter vring_free_queue(vdev, event_size_in_bytes, device, device_event_dma_addr); 19761ce9e605STiwei Bie err_device: 1977ae93d8eaSDan Carpenter vring_free_queue(vdev, event_size_in_bytes, driver, driver_event_dma_addr); 19781ce9e605STiwei Bie err_driver: 19791ce9e605STiwei Bie vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr); 19801ce9e605STiwei Bie err_ring: 19811ce9e605STiwei Bie return NULL; 19821ce9e605STiwei Bie } 19831ce9e605STiwei Bie 19841ce9e605STiwei Bie 19851ce9e605STiwei Bie /* 1986e6f633e5STiwei Bie * Generic functions and exported symbols. 1987e6f633e5STiwei Bie */ 1988e6f633e5STiwei Bie 1989e6f633e5STiwei Bie static inline int virtqueue_add(struct virtqueue *_vq, 1990e6f633e5STiwei Bie struct scatterlist *sgs[], 1991e6f633e5STiwei Bie unsigned int total_sg, 1992e6f633e5STiwei Bie unsigned int out_sgs, 1993e6f633e5STiwei Bie unsigned int in_sgs, 1994e6f633e5STiwei Bie void *data, 1995e6f633e5STiwei Bie void *ctx, 1996e6f633e5STiwei Bie gfp_t gfp) 1997e6f633e5STiwei Bie { 19981ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 19991ce9e605STiwei Bie 20001ce9e605STiwei Bie return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg, 20011ce9e605STiwei Bie out_sgs, in_sgs, data, ctx, gfp) : 20021ce9e605STiwei Bie virtqueue_add_split(_vq, sgs, total_sg, 2003e6f633e5STiwei Bie out_sgs, in_sgs, data, ctx, gfp); 2004e6f633e5STiwei Bie } 2005e6f633e5STiwei Bie 2006e6f633e5STiwei Bie /** 2007e6f633e5STiwei Bie * virtqueue_add_sgs - expose buffers to other end 2008a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2009e6f633e5STiwei Bie * @sgs: array of terminated scatterlists. 2010a5581206SJiang Biao * @out_sgs: the number of scatterlists readable by other side 2011a5581206SJiang Biao * @in_sgs: the number of scatterlists which are writable (after readable ones) 2012e6f633e5STiwei Bie * @data: the token identifying the buffer. 2013e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 2014e6f633e5STiwei Bie * 2015e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 2016e6f633e5STiwei Bie * at the same time (except where noted). 2017e6f633e5STiwei Bie * 2018e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 2019e6f633e5STiwei Bie */ 2020e6f633e5STiwei Bie int virtqueue_add_sgs(struct virtqueue *_vq, 2021e6f633e5STiwei Bie struct scatterlist *sgs[], 2022e6f633e5STiwei Bie unsigned int out_sgs, 2023e6f633e5STiwei Bie unsigned int in_sgs, 2024e6f633e5STiwei Bie void *data, 2025e6f633e5STiwei Bie gfp_t gfp) 2026e6f633e5STiwei Bie { 2027e6f633e5STiwei Bie unsigned int i, total_sg = 0; 2028e6f633e5STiwei Bie 2029e6f633e5STiwei Bie /* Count them first. */ 2030e6f633e5STiwei Bie for (i = 0; i < out_sgs + in_sgs; i++) { 2031e6f633e5STiwei Bie struct scatterlist *sg; 2032e6f633e5STiwei Bie 2033e6f633e5STiwei Bie for (sg = sgs[i]; sg; sg = sg_next(sg)) 2034e6f633e5STiwei Bie total_sg++; 2035e6f633e5STiwei Bie } 2036e6f633e5STiwei Bie return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, 2037e6f633e5STiwei Bie data, NULL, gfp); 2038e6f633e5STiwei Bie } 2039e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_sgs); 2040e6f633e5STiwei Bie 2041e6f633e5STiwei Bie /** 2042e6f633e5STiwei Bie * virtqueue_add_outbuf - expose output buffers to other end 2043e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 2044e6f633e5STiwei Bie * @sg: scatterlist (must be well-formed and terminated!) 2045e6f633e5STiwei Bie * @num: the number of entries in @sg readable by other side 2046e6f633e5STiwei Bie * @data: the token identifying the buffer. 2047e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 2048e6f633e5STiwei Bie * 2049e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 2050e6f633e5STiwei Bie * at the same time (except where noted). 2051e6f633e5STiwei Bie * 2052e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 2053e6f633e5STiwei Bie */ 2054e6f633e5STiwei Bie int virtqueue_add_outbuf(struct virtqueue *vq, 2055e6f633e5STiwei Bie struct scatterlist *sg, unsigned int num, 2056e6f633e5STiwei Bie void *data, 2057e6f633e5STiwei Bie gfp_t gfp) 2058e6f633e5STiwei Bie { 2059e6f633e5STiwei Bie return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp); 2060e6f633e5STiwei Bie } 2061e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); 2062e6f633e5STiwei Bie 2063e6f633e5STiwei Bie /** 2064e6f633e5STiwei Bie * virtqueue_add_inbuf - expose input buffers to other end 2065e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 2066e6f633e5STiwei Bie * @sg: scatterlist (must be well-formed and terminated!) 2067e6f633e5STiwei Bie * @num: the number of entries in @sg writable by other side 2068e6f633e5STiwei Bie * @data: the token identifying the buffer. 2069e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 2070e6f633e5STiwei Bie * 2071e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 2072e6f633e5STiwei Bie * at the same time (except where noted). 2073e6f633e5STiwei Bie * 2074e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 2075e6f633e5STiwei Bie */ 2076e6f633e5STiwei Bie int virtqueue_add_inbuf(struct virtqueue *vq, 2077e6f633e5STiwei Bie struct scatterlist *sg, unsigned int num, 2078e6f633e5STiwei Bie void *data, 2079e6f633e5STiwei Bie gfp_t gfp) 2080e6f633e5STiwei Bie { 2081e6f633e5STiwei Bie return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp); 2082e6f633e5STiwei Bie } 2083e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); 2084e6f633e5STiwei Bie 2085e6f633e5STiwei Bie /** 2086e6f633e5STiwei Bie * virtqueue_add_inbuf_ctx - expose input buffers to other end 2087e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 2088e6f633e5STiwei Bie * @sg: scatterlist (must be well-formed and terminated!) 2089e6f633e5STiwei Bie * @num: the number of entries in @sg writable by other side 2090e6f633e5STiwei Bie * @data: the token identifying the buffer. 2091e6f633e5STiwei Bie * @ctx: extra context for the token 2092e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 2093e6f633e5STiwei Bie * 2094e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 2095e6f633e5STiwei Bie * at the same time (except where noted). 2096e6f633e5STiwei Bie * 2097e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 2098e6f633e5STiwei Bie */ 2099e6f633e5STiwei Bie int virtqueue_add_inbuf_ctx(struct virtqueue *vq, 2100e6f633e5STiwei Bie struct scatterlist *sg, unsigned int num, 2101e6f633e5STiwei Bie void *data, 2102e6f633e5STiwei Bie void *ctx, 2103e6f633e5STiwei Bie gfp_t gfp) 2104e6f633e5STiwei Bie { 2105e6f633e5STiwei Bie return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp); 2106e6f633e5STiwei Bie } 2107e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx); 2108e6f633e5STiwei Bie 2109e6f633e5STiwei Bie /** 2110e6f633e5STiwei Bie * virtqueue_kick_prepare - first half of split virtqueue_kick call. 2111a5581206SJiang Biao * @_vq: the struct virtqueue 2112e6f633e5STiwei Bie * 2113e6f633e5STiwei Bie * Instead of virtqueue_kick(), you can do: 2114e6f633e5STiwei Bie * if (virtqueue_kick_prepare(vq)) 2115e6f633e5STiwei Bie * virtqueue_notify(vq); 2116e6f633e5STiwei Bie * 2117e6f633e5STiwei Bie * This is sometimes useful because the virtqueue_kick_prepare() needs 2118e6f633e5STiwei Bie * to be serialized, but the actual virtqueue_notify() call does not. 2119e6f633e5STiwei Bie */ 2120e6f633e5STiwei Bie bool virtqueue_kick_prepare(struct virtqueue *_vq) 2121e6f633e5STiwei Bie { 21221ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 21231ce9e605STiwei Bie 21241ce9e605STiwei Bie return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) : 21251ce9e605STiwei Bie virtqueue_kick_prepare_split(_vq); 2126e6f633e5STiwei Bie } 2127e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); 2128e6f633e5STiwei Bie 2129e6f633e5STiwei Bie /** 2130e6f633e5STiwei Bie * virtqueue_notify - second half of split virtqueue_kick call. 2131a5581206SJiang Biao * @_vq: the struct virtqueue 2132e6f633e5STiwei Bie * 2133e6f633e5STiwei Bie * This does not need to be serialized. 2134e6f633e5STiwei Bie * 2135e6f633e5STiwei Bie * Returns false if host notify failed or queue is broken, otherwise true. 2136e6f633e5STiwei Bie */ 2137e6f633e5STiwei Bie bool virtqueue_notify(struct virtqueue *_vq) 2138e6f633e5STiwei Bie { 2139e6f633e5STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 2140e6f633e5STiwei Bie 2141e6f633e5STiwei Bie if (unlikely(vq->broken)) 2142e6f633e5STiwei Bie return false; 2143e6f633e5STiwei Bie 2144e6f633e5STiwei Bie /* Prod other side to tell it about changes. */ 2145e6f633e5STiwei Bie if (!vq->notify(_vq)) { 2146e6f633e5STiwei Bie vq->broken = true; 2147e6f633e5STiwei Bie return false; 2148e6f633e5STiwei Bie } 2149e6f633e5STiwei Bie return true; 2150e6f633e5STiwei Bie } 2151e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_notify); 2152e6f633e5STiwei Bie 2153e6f633e5STiwei Bie /** 2154e6f633e5STiwei Bie * virtqueue_kick - update after add_buf 2155e6f633e5STiwei Bie * @vq: the struct virtqueue 2156e6f633e5STiwei Bie * 2157e6f633e5STiwei Bie * After one or more virtqueue_add_* calls, invoke this to kick 2158e6f633e5STiwei Bie * the other side. 2159e6f633e5STiwei Bie * 2160e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 2161e6f633e5STiwei Bie * operations at the same time (except where noted). 2162e6f633e5STiwei Bie * 2163e6f633e5STiwei Bie * Returns false if kick failed, otherwise true. 2164e6f633e5STiwei Bie */ 2165e6f633e5STiwei Bie bool virtqueue_kick(struct virtqueue *vq) 2166e6f633e5STiwei Bie { 2167e6f633e5STiwei Bie if (virtqueue_kick_prepare(vq)) 2168e6f633e5STiwei Bie return virtqueue_notify(vq); 2169e6f633e5STiwei Bie return true; 2170e6f633e5STiwei Bie } 2171e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick); 2172e6f633e5STiwei Bie 2173e6f633e5STiwei Bie /** 217431c11db6SYang Li * virtqueue_get_buf_ctx - get the next used buffer 2175a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2176e6f633e5STiwei Bie * @len: the length written into the buffer 2177a5581206SJiang Biao * @ctx: extra context for the token 2178e6f633e5STiwei Bie * 2179e6f633e5STiwei Bie * If the device wrote data into the buffer, @len will be set to the 2180e6f633e5STiwei Bie * amount written. This means you don't need to clear the buffer 2181e6f633e5STiwei Bie * beforehand to ensure there's no data leakage in the case of short 2182e6f633e5STiwei Bie * writes. 2183e6f633e5STiwei Bie * 2184e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 2185e6f633e5STiwei Bie * operations at the same time (except where noted). 2186e6f633e5STiwei Bie * 2187e6f633e5STiwei Bie * Returns NULL if there are no used buffers, or the "data" token 2188e6f633e5STiwei Bie * handed to virtqueue_add_*(). 2189e6f633e5STiwei Bie */ 2190e6f633e5STiwei Bie void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len, 2191e6f633e5STiwei Bie void **ctx) 2192e6f633e5STiwei Bie { 21931ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 21941ce9e605STiwei Bie 21951ce9e605STiwei Bie return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) : 21961ce9e605STiwei Bie virtqueue_get_buf_ctx_split(_vq, len, ctx); 2197e6f633e5STiwei Bie } 2198e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx); 2199e6f633e5STiwei Bie 2200e6f633e5STiwei Bie void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) 2201e6f633e5STiwei Bie { 2202e6f633e5STiwei Bie return virtqueue_get_buf_ctx(_vq, len, NULL); 2203e6f633e5STiwei Bie } 2204e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf); 2205e6f633e5STiwei Bie /** 2206e6f633e5STiwei Bie * virtqueue_disable_cb - disable callbacks 2207a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2208e6f633e5STiwei Bie * 2209e6f633e5STiwei Bie * Note that this is not necessarily synchronous, hence unreliable and only 2210e6f633e5STiwei Bie * useful as an optimization. 2211e6f633e5STiwei Bie * 2212e6f633e5STiwei Bie * Unlike other operations, this need not be serialized. 2213e6f633e5STiwei Bie */ 2214e6f633e5STiwei Bie void virtqueue_disable_cb(struct virtqueue *_vq) 2215e6f633e5STiwei Bie { 22161ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 22171ce9e605STiwei Bie 22188d622d21SMichael S. Tsirkin /* If device triggered an event already it won't trigger one again: 22198d622d21SMichael S. Tsirkin * no need to disable. 22208d622d21SMichael S. Tsirkin */ 22218d622d21SMichael S. Tsirkin if (vq->event_triggered) 22228d622d21SMichael S. Tsirkin return; 22238d622d21SMichael S. Tsirkin 22241ce9e605STiwei Bie if (vq->packed_ring) 22251ce9e605STiwei Bie virtqueue_disable_cb_packed(_vq); 22261ce9e605STiwei Bie else 2227e6f633e5STiwei Bie virtqueue_disable_cb_split(_vq); 2228e6f633e5STiwei Bie } 2229e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_disable_cb); 2230e6f633e5STiwei Bie 2231e6f633e5STiwei Bie /** 2232e6f633e5STiwei Bie * virtqueue_enable_cb_prepare - restart callbacks after disable_cb 2233a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2234e6f633e5STiwei Bie * 2235e6f633e5STiwei Bie * This re-enables callbacks; it returns current queue state 2236e6f633e5STiwei Bie * in an opaque unsigned value. This value should be later tested by 2237e6f633e5STiwei Bie * virtqueue_poll, to detect a possible race between the driver checking for 2238e6f633e5STiwei Bie * more work, and enabling callbacks. 2239e6f633e5STiwei Bie * 2240e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 2241e6f633e5STiwei Bie * operations at the same time (except where noted). 2242e6f633e5STiwei Bie */ 224331532340SSolomon Tan unsigned int virtqueue_enable_cb_prepare(struct virtqueue *_vq) 2244e6f633e5STiwei Bie { 22451ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 22461ce9e605STiwei Bie 22478d622d21SMichael S. Tsirkin if (vq->event_triggered) 22488d622d21SMichael S. Tsirkin vq->event_triggered = false; 22498d622d21SMichael S. Tsirkin 22501ce9e605STiwei Bie return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) : 22511ce9e605STiwei Bie virtqueue_enable_cb_prepare_split(_vq); 2252e6f633e5STiwei Bie } 2253e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); 2254e6f633e5STiwei Bie 2255e6f633e5STiwei Bie /** 2256e6f633e5STiwei Bie * virtqueue_poll - query pending used buffers 2257a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2258e6f633e5STiwei Bie * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). 2259e6f633e5STiwei Bie * 2260e6f633e5STiwei Bie * Returns "true" if there are pending used buffers in the queue. 2261e6f633e5STiwei Bie * 2262e6f633e5STiwei Bie * This does not need to be serialized. 2263e6f633e5STiwei Bie */ 226431532340SSolomon Tan bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx) 2265e6f633e5STiwei Bie { 2266e6f633e5STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 2267e6f633e5STiwei Bie 2268481a0d74SMao Wenan if (unlikely(vq->broken)) 2269481a0d74SMao Wenan return false; 2270481a0d74SMao Wenan 2271e6f633e5STiwei Bie virtio_mb(vq->weak_barriers); 22721ce9e605STiwei Bie return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) : 22731ce9e605STiwei Bie virtqueue_poll_split(_vq, last_used_idx); 2274e6f633e5STiwei Bie } 2275e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_poll); 2276e6f633e5STiwei Bie 2277e6f633e5STiwei Bie /** 2278e6f633e5STiwei Bie * virtqueue_enable_cb - restart callbacks after disable_cb. 2279a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2280e6f633e5STiwei Bie * 2281e6f633e5STiwei Bie * This re-enables callbacks; it returns "false" if there are pending 2282e6f633e5STiwei Bie * buffers in the queue, to detect a possible race between the driver 2283e6f633e5STiwei Bie * checking for more work, and enabling callbacks. 2284e6f633e5STiwei Bie * 2285e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 2286e6f633e5STiwei Bie * operations at the same time (except where noted). 2287e6f633e5STiwei Bie */ 2288e6f633e5STiwei Bie bool virtqueue_enable_cb(struct virtqueue *_vq) 2289e6f633e5STiwei Bie { 229031532340SSolomon Tan unsigned int last_used_idx = virtqueue_enable_cb_prepare(_vq); 2291e6f633e5STiwei Bie 2292e6f633e5STiwei Bie return !virtqueue_poll(_vq, last_used_idx); 2293e6f633e5STiwei Bie } 2294e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb); 2295e6f633e5STiwei Bie 2296e6f633e5STiwei Bie /** 2297e6f633e5STiwei Bie * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. 2298a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2299e6f633e5STiwei Bie * 2300e6f633e5STiwei Bie * This re-enables callbacks but hints to the other side to delay 2301e6f633e5STiwei Bie * interrupts until most of the available buffers have been processed; 2302e6f633e5STiwei Bie * it returns "false" if there are many pending buffers in the queue, 2303e6f633e5STiwei Bie * to detect a possible race between the driver checking for more work, 2304e6f633e5STiwei Bie * and enabling callbacks. 2305e6f633e5STiwei Bie * 2306e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 2307e6f633e5STiwei Bie * operations at the same time (except where noted). 2308e6f633e5STiwei Bie */ 2309e6f633e5STiwei Bie bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) 2310e6f633e5STiwei Bie { 23111ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 23121ce9e605STiwei Bie 23138d622d21SMichael S. Tsirkin if (vq->event_triggered) 23148d622d21SMichael S. Tsirkin vq->event_triggered = false; 23158d622d21SMichael S. Tsirkin 23161ce9e605STiwei Bie return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) : 23171ce9e605STiwei Bie virtqueue_enable_cb_delayed_split(_vq); 2318e6f633e5STiwei Bie } 2319e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); 2320e6f633e5STiwei Bie 2321138fd251STiwei Bie /** 2322138fd251STiwei Bie * virtqueue_detach_unused_buf - detach first unused buffer 2323a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2324138fd251STiwei Bie * 2325138fd251STiwei Bie * Returns NULL or the "data" token handed to virtqueue_add_*(). 2326a62eecb3SXuan Zhuo * This is not valid on an active queue; it is useful for device 2327a62eecb3SXuan Zhuo * shutdown or the reset queue. 2328138fd251STiwei Bie */ 2329138fd251STiwei Bie void *virtqueue_detach_unused_buf(struct virtqueue *_vq) 2330138fd251STiwei Bie { 23311ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 23321ce9e605STiwei Bie 23331ce9e605STiwei Bie return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) : 23341ce9e605STiwei Bie virtqueue_detach_unused_buf_split(_vq); 2335138fd251STiwei Bie } 23367c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); 2337c021eac4SShirley Ma 2338138fd251STiwei Bie static inline bool more_used(const struct vring_virtqueue *vq) 2339138fd251STiwei Bie { 23401ce9e605STiwei Bie return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq); 2341138fd251STiwei Bie } 2342138fd251STiwei Bie 23430a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq) 23440a8a69ddSRusty Russell { 23450a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 23460a8a69ddSRusty Russell 23470a8a69ddSRusty Russell if (!more_used(vq)) { 23480a8a69ddSRusty Russell pr_debug("virtqueue interrupt with no work for %p\n", vq); 23490a8a69ddSRusty Russell return IRQ_NONE; 23500a8a69ddSRusty Russell } 23510a8a69ddSRusty Russell 23528b4ec69dSJason Wang if (unlikely(vq->broken)) { 2353c346dae4SJason Wang #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION 23548b4ec69dSJason Wang dev_warn_once(&vq->vq.vdev->dev, 23558b4ec69dSJason Wang "virtio vring IRQ raised before DRIVER_OK"); 23568b4ec69dSJason Wang return IRQ_NONE; 2357c346dae4SJason Wang #else 2358c346dae4SJason Wang return IRQ_HANDLED; 2359c346dae4SJason Wang #endif 23608b4ec69dSJason Wang } 23610a8a69ddSRusty Russell 23628d622d21SMichael S. Tsirkin /* Just a hint for performance: so it's ok that this can be racy! */ 23638d622d21SMichael S. Tsirkin if (vq->event) 23648d622d21SMichael S. Tsirkin vq->event_triggered = true; 23658d622d21SMichael S. Tsirkin 23660a8a69ddSRusty Russell pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); 236718445c4dSRusty Russell if (vq->vq.callback) 236818445c4dSRusty Russell vq->vq.callback(&vq->vq); 23690a8a69ddSRusty Russell 23700a8a69ddSRusty Russell return IRQ_HANDLED; 23710a8a69ddSRusty Russell } 2372c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt); 23730a8a69ddSRusty Russell 23741ce9e605STiwei Bie /* Only available for split ring */ 237507d9629dSXuan Zhuo static struct virtqueue *__vring_new_virtqueue(unsigned int index, 2376cd4c812aSXuan Zhuo struct vring_virtqueue_split *vring_split, 23770a8a69ddSRusty Russell struct virtio_device *vdev, 23787b21e34fSRusty Russell bool weak_barriers, 2379f94682ddSMichael S. Tsirkin bool context, 238046f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *), 23819499f5e7SRusty Russell void (*callback)(struct virtqueue *), 23829499f5e7SRusty Russell const char *name) 23830a8a69ddSRusty Russell { 23842a2d1382SAndy Lutomirski struct vring_virtqueue *vq; 2385a2b36c8dSXuan Zhuo int err; 23860a8a69ddSRusty Russell 23871ce9e605STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) 23881ce9e605STiwei Bie return NULL; 23891ce9e605STiwei Bie 2390cbeedb72STiwei Bie vq = kmalloc(sizeof(*vq), GFP_KERNEL); 23910a8a69ddSRusty Russell if (!vq) 23920a8a69ddSRusty Russell return NULL; 23930a8a69ddSRusty Russell 23941ce9e605STiwei Bie vq->packed_ring = false; 23950a8a69ddSRusty Russell vq->vq.callback = callback; 23960a8a69ddSRusty Russell vq->vq.vdev = vdev; 23979499f5e7SRusty Russell vq->vq.name = name; 239806ca287dSRusty Russell vq->vq.index = index; 23992a2d1382SAndy Lutomirski vq->we_own_ring = false; 24000a8a69ddSRusty Russell vq->notify = notify; 24017b21e34fSRusty Russell vq->weak_barriers = weak_barriers; 2402c346dae4SJason Wang #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION 24038b4ec69dSJason Wang vq->broken = true; 2404c346dae4SJason Wang #else 2405c346dae4SJason Wang vq->broken = false; 2406c346dae4SJason Wang #endif 2407fb3fba6bSTiwei Bie vq->use_dma_api = vring_use_dma_api(vdev); 24080a8a69ddSRusty Russell 24095a08b04fSMichael S. Tsirkin vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && 24105a08b04fSMichael S. Tsirkin !context; 2411a5c262c5SMichael S. Tsirkin vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 24129fa29b9dSMark McLoughlin 241345383fb0STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) 241445383fb0STiwei Bie vq->weak_barriers = false; 241545383fb0STiwei Bie 2416a2b36c8dSXuan Zhuo err = vring_alloc_state_extra_split(vring_split); 2417a2b36c8dSXuan Zhuo if (err) { 2418a2b36c8dSXuan Zhuo kfree(vq); 2419a2b36c8dSXuan Zhuo return NULL; 2420a2b36c8dSXuan Zhuo } 242172b5e895SJason Wang 2422198fa7beSXuan Zhuo virtqueue_vring_init_split(vring_split, vq); 2423198fa7beSXuan Zhuo 2424cd4c812aSXuan Zhuo virtqueue_init(vq, vring_split->vring.num); 2425e1d6a423SXuan Zhuo virtqueue_vring_attach_split(vq, vring_split); 24263a897128SXuan Zhuo 24270e566c8fSParav Pandit spin_lock(&vdev->vqs_list_lock); 2428e152d8afSDan Carpenter list_add_tail(&vq->vq.list, &vdev->vqs); 24290e566c8fSParav Pandit spin_unlock(&vdev->vqs_list_lock); 24300a8a69ddSRusty Russell return &vq->vq; 24310a8a69ddSRusty Russell } 24322a2d1382SAndy Lutomirski 24332a2d1382SAndy Lutomirski struct virtqueue *vring_create_virtqueue( 24342a2d1382SAndy Lutomirski unsigned int index, 24352a2d1382SAndy Lutomirski unsigned int num, 24362a2d1382SAndy Lutomirski unsigned int vring_align, 24372a2d1382SAndy Lutomirski struct virtio_device *vdev, 24382a2d1382SAndy Lutomirski bool weak_barriers, 24392a2d1382SAndy Lutomirski bool may_reduce_num, 2440f94682ddSMichael S. Tsirkin bool context, 24412a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *), 24422a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *), 24432a2d1382SAndy Lutomirski const char *name) 24442a2d1382SAndy Lutomirski { 24451ce9e605STiwei Bie 24461ce9e605STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) 24471ce9e605STiwei Bie return vring_create_virtqueue_packed(index, num, vring_align, 24481ce9e605STiwei Bie vdev, weak_barriers, may_reduce_num, 24491ce9e605STiwei Bie context, notify, callback, name); 24501ce9e605STiwei Bie 2451d79dca75STiwei Bie return vring_create_virtqueue_split(index, num, vring_align, 2452d79dca75STiwei Bie vdev, weak_barriers, may_reduce_num, 2453d79dca75STiwei Bie context, notify, callback, name); 24542a2d1382SAndy Lutomirski } 24552a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(vring_create_virtqueue); 24562a2d1382SAndy Lutomirski 24571ce9e605STiwei Bie /* Only available for split ring */ 24582a2d1382SAndy Lutomirski struct virtqueue *vring_new_virtqueue(unsigned int index, 24592a2d1382SAndy Lutomirski unsigned int num, 24602a2d1382SAndy Lutomirski unsigned int vring_align, 24612a2d1382SAndy Lutomirski struct virtio_device *vdev, 24622a2d1382SAndy Lutomirski bool weak_barriers, 2463f94682ddSMichael S. Tsirkin bool context, 24642a2d1382SAndy Lutomirski void *pages, 24652a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *vq), 24662a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *vq), 24672a2d1382SAndy Lutomirski const char *name) 24682a2d1382SAndy Lutomirski { 2469cd4c812aSXuan Zhuo struct vring_virtqueue_split vring_split = {}; 24701ce9e605STiwei Bie 24711ce9e605STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) 24721ce9e605STiwei Bie return NULL; 24731ce9e605STiwei Bie 2474cd4c812aSXuan Zhuo vring_init(&vring_split.vring, num, pages, vring_align); 2475cd4c812aSXuan Zhuo return __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers, 2476cd4c812aSXuan Zhuo context, notify, callback, name); 24772a2d1382SAndy Lutomirski } 2478c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue); 24790a8a69ddSRusty Russell 24803ea19e32SXuan Zhuo static void vring_free(struct virtqueue *_vq) 24810a8a69ddSRusty Russell { 24822a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 24832a2d1382SAndy Lutomirski 24842a2d1382SAndy Lutomirski if (vq->we_own_ring) { 24851ce9e605STiwei Bie if (vq->packed_ring) { 24861ce9e605STiwei Bie vring_free_queue(vq->vq.vdev, 24871ce9e605STiwei Bie vq->packed.ring_size_in_bytes, 24881ce9e605STiwei Bie vq->packed.vring.desc, 24891ce9e605STiwei Bie vq->packed.ring_dma_addr); 24901ce9e605STiwei Bie 24911ce9e605STiwei Bie vring_free_queue(vq->vq.vdev, 24921ce9e605STiwei Bie vq->packed.event_size_in_bytes, 24931ce9e605STiwei Bie vq->packed.vring.driver, 24941ce9e605STiwei Bie vq->packed.driver_event_dma_addr); 24951ce9e605STiwei Bie 24961ce9e605STiwei Bie vring_free_queue(vq->vq.vdev, 24971ce9e605STiwei Bie vq->packed.event_size_in_bytes, 24981ce9e605STiwei Bie vq->packed.vring.device, 24991ce9e605STiwei Bie vq->packed.device_event_dma_addr); 25001ce9e605STiwei Bie 25011ce9e605STiwei Bie kfree(vq->packed.desc_state); 25021ce9e605STiwei Bie kfree(vq->packed.desc_extra); 25031ce9e605STiwei Bie } else { 2504d79dca75STiwei Bie vring_free_queue(vq->vq.vdev, 2505d79dca75STiwei Bie vq->split.queue_size_in_bytes, 2506d79dca75STiwei Bie vq->split.vring.desc, 2507d79dca75STiwei Bie vq->split.queue_dma_addr); 2508f13f09a1SSuman Anna } 2509f13f09a1SSuman Anna } 251072b5e895SJason Wang if (!vq->packed_ring) { 2511cbeedb72STiwei Bie kfree(vq->split.desc_state); 251272b5e895SJason Wang kfree(vq->split.desc_extra); 251372b5e895SJason Wang } 25143ea19e32SXuan Zhuo } 25153ea19e32SXuan Zhuo 25163ea19e32SXuan Zhuo void vring_del_virtqueue(struct virtqueue *_vq) 25173ea19e32SXuan Zhuo { 25183ea19e32SXuan Zhuo struct vring_virtqueue *vq = to_vvq(_vq); 25193ea19e32SXuan Zhuo 25203ea19e32SXuan Zhuo spin_lock(&vq->vq.vdev->vqs_list_lock); 25213ea19e32SXuan Zhuo list_del(&_vq->list); 25223ea19e32SXuan Zhuo spin_unlock(&vq->vq.vdev->vqs_list_lock); 25233ea19e32SXuan Zhuo 25243ea19e32SXuan Zhuo vring_free(_vq); 25253ea19e32SXuan Zhuo 25262a2d1382SAndy Lutomirski kfree(vq); 25270a8a69ddSRusty Russell } 2528c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue); 25290a8a69ddSRusty Russell 2530e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */ 2531e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev) 2532e34f8725SRusty Russell { 2533e34f8725SRusty Russell unsigned int i; 2534e34f8725SRusty Russell 2535e34f8725SRusty Russell for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { 2536e34f8725SRusty Russell switch (i) { 25379fa29b9dSMark McLoughlin case VIRTIO_RING_F_INDIRECT_DESC: 25389fa29b9dSMark McLoughlin break; 2539a5c262c5SMichael S. Tsirkin case VIRTIO_RING_F_EVENT_IDX: 2540a5c262c5SMichael S. Tsirkin break; 2541747ae34aSMichael S. Tsirkin case VIRTIO_F_VERSION_1: 2542747ae34aSMichael S. Tsirkin break; 2543321bd212SMichael S. Tsirkin case VIRTIO_F_ACCESS_PLATFORM: 25441a937693SMichael S. Tsirkin break; 2545f959a128STiwei Bie case VIRTIO_F_RING_PACKED: 2546f959a128STiwei Bie break; 254745383fb0STiwei Bie case VIRTIO_F_ORDER_PLATFORM: 254845383fb0STiwei Bie break; 2549e34f8725SRusty Russell default: 2550e34f8725SRusty Russell /* We don't understand this bit. */ 2551e16e12beSMichael S. Tsirkin __virtio_clear_bit(vdev, i); 2552e34f8725SRusty Russell } 2553e34f8725SRusty Russell } 2554e34f8725SRusty Russell } 2555e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features); 2556e34f8725SRusty Russell 25575dfc1762SRusty Russell /** 25585dfc1762SRusty Russell * virtqueue_get_vring_size - return the size of the virtqueue's vring 2559a5581206SJiang Biao * @_vq: the struct virtqueue containing the vring of interest. 25605dfc1762SRusty Russell * 25615dfc1762SRusty Russell * Returns the size of the vring. This is mainly used for boasting to 25625dfc1762SRusty Russell * userspace. Unlike other operations, this need not be serialized. 25635dfc1762SRusty Russell */ 25648f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) 25658f9f4668SRick Jones { 25668f9f4668SRick Jones 25678f9f4668SRick Jones struct vring_virtqueue *vq = to_vvq(_vq); 25688f9f4668SRick Jones 25691ce9e605STiwei Bie return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num; 25708f9f4668SRick Jones } 25718f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); 25728f9f4668SRick Jones 2573b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq) 2574b3b32c94SHeinz Graalfs { 2575b3b32c94SHeinz Graalfs struct vring_virtqueue *vq = to_vvq(_vq); 2576b3b32c94SHeinz Graalfs 257760f07798SParav Pandit return READ_ONCE(vq->broken); 2578b3b32c94SHeinz Graalfs } 2579b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken); 2580b3b32c94SHeinz Graalfs 2581e2dcdfe9SRusty Russell /* 2582e2dcdfe9SRusty Russell * This should prevent the device from being used, allowing drivers to 2583e2dcdfe9SRusty Russell * recover. You may need to grab appropriate locks to flush. 2584e2dcdfe9SRusty Russell */ 2585e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev) 2586e2dcdfe9SRusty Russell { 2587e2dcdfe9SRusty Russell struct virtqueue *_vq; 2588e2dcdfe9SRusty Russell 25890e566c8fSParav Pandit spin_lock(&dev->vqs_list_lock); 2590e2dcdfe9SRusty Russell list_for_each_entry(_vq, &dev->vqs, list) { 2591e2dcdfe9SRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 259260f07798SParav Pandit 259360f07798SParav Pandit /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ 259460f07798SParav Pandit WRITE_ONCE(vq->broken, true); 2595e2dcdfe9SRusty Russell } 25960e566c8fSParav Pandit spin_unlock(&dev->vqs_list_lock); 2597e2dcdfe9SRusty Russell } 2598e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device); 2599e2dcdfe9SRusty Russell 2600be83f04dSJason Wang /* 2601be83f04dSJason Wang * This should allow the device to be used by the driver. You may 2602be83f04dSJason Wang * need to grab appropriate locks to flush the write to 2603be83f04dSJason Wang * vq->broken. This should only be used in some specific case e.g 2604be83f04dSJason Wang * (probing and restoring). This function should only be called by the 2605be83f04dSJason Wang * core, not directly by the driver. 2606be83f04dSJason Wang */ 2607be83f04dSJason Wang void __virtio_unbreak_device(struct virtio_device *dev) 2608be83f04dSJason Wang { 2609be83f04dSJason Wang struct virtqueue *_vq; 2610be83f04dSJason Wang 2611be83f04dSJason Wang spin_lock(&dev->vqs_list_lock); 2612be83f04dSJason Wang list_for_each_entry(_vq, &dev->vqs, list) { 2613be83f04dSJason Wang struct vring_virtqueue *vq = to_vvq(_vq); 2614be83f04dSJason Wang 2615be83f04dSJason Wang /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ 2616be83f04dSJason Wang WRITE_ONCE(vq->broken, false); 2617be83f04dSJason Wang } 2618be83f04dSJason Wang spin_unlock(&dev->vqs_list_lock); 2619be83f04dSJason Wang } 2620be83f04dSJason Wang EXPORT_SYMBOL_GPL(__virtio_unbreak_device); 2621be83f04dSJason Wang 26222a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq) 262389062652SCornelia Huck { 262489062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 262589062652SCornelia Huck 26262a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 262789062652SCornelia Huck 26281ce9e605STiwei Bie if (vq->packed_ring) 26291ce9e605STiwei Bie return vq->packed.ring_dma_addr; 26301ce9e605STiwei Bie 2631d79dca75STiwei Bie return vq->split.queue_dma_addr; 26322a2d1382SAndy Lutomirski } 26332a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr); 26342a2d1382SAndy Lutomirski 26352a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq) 263689062652SCornelia Huck { 263789062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 263889062652SCornelia Huck 26392a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 26402a2d1382SAndy Lutomirski 26411ce9e605STiwei Bie if (vq->packed_ring) 26421ce9e605STiwei Bie return vq->packed.driver_event_dma_addr; 26431ce9e605STiwei Bie 2644d79dca75STiwei Bie return vq->split.queue_dma_addr + 2645e593bf97STiwei Bie ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc); 264689062652SCornelia Huck } 26472a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr); 26482a2d1382SAndy Lutomirski 26492a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq) 26502a2d1382SAndy Lutomirski { 26512a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 26522a2d1382SAndy Lutomirski 26532a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 26542a2d1382SAndy Lutomirski 26551ce9e605STiwei Bie if (vq->packed_ring) 26561ce9e605STiwei Bie return vq->packed.device_event_dma_addr; 26571ce9e605STiwei Bie 2658d79dca75STiwei Bie return vq->split.queue_dma_addr + 2659e593bf97STiwei Bie ((char *)vq->split.vring.used - (char *)vq->split.vring.desc); 26602a2d1382SAndy Lutomirski } 26612a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_used_addr); 26622a2d1382SAndy Lutomirski 26631ce9e605STiwei Bie /* Only available for split ring */ 26642a2d1382SAndy Lutomirski const struct vring *virtqueue_get_vring(struct virtqueue *vq) 26652a2d1382SAndy Lutomirski { 2666e593bf97STiwei Bie return &to_vvq(vq)->split.vring; 26672a2d1382SAndy Lutomirski } 26682a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_vring); 266989062652SCornelia Huck 2670c6fd4701SRusty Russell MODULE_LICENSE("GPL"); 2671