10a8a69ddSRusty Russell /* Virtio ring implementation. 20a8a69ddSRusty Russell * 30a8a69ddSRusty Russell * Copyright 2007 Rusty Russell IBM Corporation 40a8a69ddSRusty Russell * 50a8a69ddSRusty Russell * This program is free software; you can redistribute it and/or modify 60a8a69ddSRusty Russell * it under the terms of the GNU General Public License as published by 70a8a69ddSRusty Russell * the Free Software Foundation; either version 2 of the License, or 80a8a69ddSRusty Russell * (at your option) any later version. 90a8a69ddSRusty Russell * 100a8a69ddSRusty Russell * This program is distributed in the hope that it will be useful, 110a8a69ddSRusty Russell * but WITHOUT ANY WARRANTY; without even the implied warranty of 120a8a69ddSRusty Russell * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 130a8a69ddSRusty Russell * GNU General Public License for more details. 140a8a69ddSRusty Russell * 150a8a69ddSRusty Russell * You should have received a copy of the GNU General Public License 160a8a69ddSRusty Russell * along with this program; if not, write to the Free Software 170a8a69ddSRusty Russell * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 180a8a69ddSRusty Russell */ 190a8a69ddSRusty Russell #include <linux/virtio.h> 200a8a69ddSRusty Russell #include <linux/virtio_ring.h> 21e34f8725SRusty Russell #include <linux/virtio_config.h> 220a8a69ddSRusty Russell #include <linux/device.h> 235a0e3ad6STejun Heo #include <linux/slab.h> 24b5a2c4f1SPaul Gortmaker #include <linux/module.h> 25e93300b1SRusty Russell #include <linux/hrtimer.h> 26780bc790SAndy Lutomirski #include <linux/dma-mapping.h> 2778fe3987SAndy Lutomirski #include <xen/xen.h> 280a8a69ddSRusty Russell 290a8a69ddSRusty Russell #ifdef DEBUG 300a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */ 319499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 329499f5e7SRusty Russell do { \ 339499f5e7SRusty Russell dev_err(&(_vq)->vq.vdev->dev, \ 349499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 359499f5e7SRusty Russell BUG(); \ 369499f5e7SRusty Russell } while (0) 37c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */ 383a35ce7dSRoel Kluin #define START_USE(_vq) \ 39c5f841f1SRusty Russell do { \ 40c5f841f1SRusty Russell if ((_vq)->in_use) \ 419499f5e7SRusty Russell panic("%s:in_use = %i\n", \ 429499f5e7SRusty Russell (_vq)->vq.name, (_vq)->in_use); \ 43c5f841f1SRusty Russell (_vq)->in_use = __LINE__; \ 44c5f841f1SRusty Russell } while (0) 453a35ce7dSRoel Kluin #define END_USE(_vq) \ 4697a545abSRusty Russell do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) 474d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(_vq) \ 484d6a105eSTiwei Bie do { \ 494d6a105eSTiwei Bie ktime_t now = ktime_get(); \ 504d6a105eSTiwei Bie \ 514d6a105eSTiwei Bie /* No kick or get, with .1 second between? Warn. */ \ 524d6a105eSTiwei Bie if ((_vq)->last_add_time_valid) \ 534d6a105eSTiwei Bie WARN_ON(ktime_to_ms(ktime_sub(now, \ 544d6a105eSTiwei Bie (_vq)->last_add_time)) > 100); \ 554d6a105eSTiwei Bie (_vq)->last_add_time = now; \ 564d6a105eSTiwei Bie (_vq)->last_add_time_valid = true; \ 574d6a105eSTiwei Bie } while (0) 584d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(_vq) \ 594d6a105eSTiwei Bie do { \ 604d6a105eSTiwei Bie if ((_vq)->last_add_time_valid) { \ 614d6a105eSTiwei Bie WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \ 624d6a105eSTiwei Bie (_vq)->last_add_time)) > 100); \ 634d6a105eSTiwei Bie } \ 644d6a105eSTiwei Bie } while (0) 654d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(_vq) \ 664d6a105eSTiwei Bie ((_vq)->last_add_time_valid = false) 670a8a69ddSRusty Russell #else 689499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 699499f5e7SRusty Russell do { \ 709499f5e7SRusty Russell dev_err(&_vq->vq.vdev->dev, \ 719499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 729499f5e7SRusty Russell (_vq)->broken = true; \ 739499f5e7SRusty Russell } while (0) 740a8a69ddSRusty Russell #define START_USE(vq) 750a8a69ddSRusty Russell #define END_USE(vq) 764d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(vq) 774d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(vq) 784d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(vq) 790a8a69ddSRusty Russell #endif 800a8a69ddSRusty Russell 81cbeedb72STiwei Bie struct vring_desc_state_split { 82780bc790SAndy Lutomirski void *data; /* Data for callback. */ 83780bc790SAndy Lutomirski struct vring_desc *indir_desc; /* Indirect descriptor, if any. */ 84780bc790SAndy Lutomirski }; 85780bc790SAndy Lutomirski 861ce9e605STiwei Bie struct vring_desc_state_packed { 871ce9e605STiwei Bie void *data; /* Data for callback. */ 881ce9e605STiwei Bie struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */ 891ce9e605STiwei Bie u16 num; /* Descriptor list length. */ 901ce9e605STiwei Bie u16 next; /* The next desc state in a list. */ 911ce9e605STiwei Bie u16 last; /* The last desc state in a list. */ 921ce9e605STiwei Bie }; 931ce9e605STiwei Bie 941ce9e605STiwei Bie struct vring_desc_extra_packed { 951ce9e605STiwei Bie dma_addr_t addr; /* Buffer DMA addr. */ 961ce9e605STiwei Bie u32 len; /* Buffer length. */ 971ce9e605STiwei Bie u16 flags; /* Descriptor flags. */ 981ce9e605STiwei Bie }; 991ce9e605STiwei Bie 10043b4f721SMichael S. Tsirkin struct vring_virtqueue { 1010a8a69ddSRusty Russell struct virtqueue vq; 1020a8a69ddSRusty Russell 1031ce9e605STiwei Bie /* Is this a packed ring? */ 1041ce9e605STiwei Bie bool packed_ring; 1051ce9e605STiwei Bie 106fb3fba6bSTiwei Bie /* Is DMA API used? */ 107fb3fba6bSTiwei Bie bool use_dma_api; 108fb3fba6bSTiwei Bie 1097b21e34fSRusty Russell /* Can we use weak barriers? */ 1107b21e34fSRusty Russell bool weak_barriers; 1117b21e34fSRusty Russell 1120a8a69ddSRusty Russell /* Other side has made a mess, don't try any more. */ 1130a8a69ddSRusty Russell bool broken; 1140a8a69ddSRusty Russell 1159fa29b9dSMark McLoughlin /* Host supports indirect buffers */ 1169fa29b9dSMark McLoughlin bool indirect; 1179fa29b9dSMark McLoughlin 118a5c262c5SMichael S. Tsirkin /* Host publishes avail event idx */ 119a5c262c5SMichael S. Tsirkin bool event; 120a5c262c5SMichael S. Tsirkin 1210a8a69ddSRusty Russell /* Head of free buffer list. */ 1220a8a69ddSRusty Russell unsigned int free_head; 1230a8a69ddSRusty Russell /* Number we've added since last sync. */ 1240a8a69ddSRusty Russell unsigned int num_added; 1250a8a69ddSRusty Russell 1260a8a69ddSRusty Russell /* Last used index we've seen. */ 1271bc4953eSAnthony Liguori u16 last_used_idx; 1280a8a69ddSRusty Russell 1291ce9e605STiwei Bie union { 1301ce9e605STiwei Bie /* Available for split ring */ 131e593bf97STiwei Bie struct { 1321ce9e605STiwei Bie /* Actual memory layout for this queue. */ 133e593bf97STiwei Bie struct vring vring; 134e593bf97STiwei Bie 135f277ec42SVenkatesh Srinivas /* Last written value to avail->flags */ 136f277ec42SVenkatesh Srinivas u16 avail_flags_shadow; 137f277ec42SVenkatesh Srinivas 1381ce9e605STiwei Bie /* 1391ce9e605STiwei Bie * Last written value to avail->idx in 1401ce9e605STiwei Bie * guest byte order. 1411ce9e605STiwei Bie */ 142f277ec42SVenkatesh Srinivas u16 avail_idx_shadow; 143cbeedb72STiwei Bie 144cbeedb72STiwei Bie /* Per-descriptor state. */ 145cbeedb72STiwei Bie struct vring_desc_state_split *desc_state; 146d79dca75STiwei Bie 1471ce9e605STiwei Bie /* DMA address and size information */ 148d79dca75STiwei Bie dma_addr_t queue_dma_addr; 1491ce9e605STiwei Bie size_t queue_size_in_bytes; 150e593bf97STiwei Bie } split; 151f277ec42SVenkatesh Srinivas 1521ce9e605STiwei Bie /* Available for packed ring */ 1531ce9e605STiwei Bie struct { 1541ce9e605STiwei Bie /* Actual memory layout for this queue. */ 1551ce9e605STiwei Bie struct vring_packed vring; 1561ce9e605STiwei Bie 1571ce9e605STiwei Bie /* Driver ring wrap counter. */ 1581ce9e605STiwei Bie bool avail_wrap_counter; 1591ce9e605STiwei Bie 1601ce9e605STiwei Bie /* Device ring wrap counter. */ 1611ce9e605STiwei Bie bool used_wrap_counter; 1621ce9e605STiwei Bie 1631ce9e605STiwei Bie /* Avail used flags. */ 1641ce9e605STiwei Bie u16 avail_used_flags; 1651ce9e605STiwei Bie 1661ce9e605STiwei Bie /* Index of the next avail descriptor. */ 1671ce9e605STiwei Bie u16 next_avail_idx; 1681ce9e605STiwei Bie 1691ce9e605STiwei Bie /* 1701ce9e605STiwei Bie * Last written value to driver->flags in 1711ce9e605STiwei Bie * guest byte order. 1721ce9e605STiwei Bie */ 1731ce9e605STiwei Bie u16 event_flags_shadow; 1741ce9e605STiwei Bie 1751ce9e605STiwei Bie /* Per-descriptor state. */ 1761ce9e605STiwei Bie struct vring_desc_state_packed *desc_state; 1771ce9e605STiwei Bie struct vring_desc_extra_packed *desc_extra; 1781ce9e605STiwei Bie 1791ce9e605STiwei Bie /* DMA address and size information */ 1801ce9e605STiwei Bie dma_addr_t ring_dma_addr; 1811ce9e605STiwei Bie dma_addr_t driver_event_dma_addr; 1821ce9e605STiwei Bie dma_addr_t device_event_dma_addr; 1831ce9e605STiwei Bie size_t ring_size_in_bytes; 1841ce9e605STiwei Bie size_t event_size_in_bytes; 1851ce9e605STiwei Bie } packed; 1861ce9e605STiwei Bie }; 1871ce9e605STiwei Bie 1880a8a69ddSRusty Russell /* How to notify other side. FIXME: commonalize hcalls! */ 18946f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *vq); 1900a8a69ddSRusty Russell 1912a2d1382SAndy Lutomirski /* DMA, allocation, and size information */ 1922a2d1382SAndy Lutomirski bool we_own_ring; 1932a2d1382SAndy Lutomirski 1940a8a69ddSRusty Russell #ifdef DEBUG 1950a8a69ddSRusty Russell /* They're supposed to lock for us. */ 1960a8a69ddSRusty Russell unsigned int in_use; 197e93300b1SRusty Russell 198e93300b1SRusty Russell /* Figure out if their kicks are too delayed. */ 199e93300b1SRusty Russell bool last_add_time_valid; 200e93300b1SRusty Russell ktime_t last_add_time; 2010a8a69ddSRusty Russell #endif 2020a8a69ddSRusty Russell }; 2030a8a69ddSRusty Russell 204e6f633e5STiwei Bie 205e6f633e5STiwei Bie /* 206e6f633e5STiwei Bie * Helpers. 207e6f633e5STiwei Bie */ 208e6f633e5STiwei Bie 2090a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) 2100a8a69ddSRusty Russell 2112f18c2d1STiwei Bie static inline bool virtqueue_use_indirect(struct virtqueue *_vq, 2122f18c2d1STiwei Bie unsigned int total_sg) 2132f18c2d1STiwei Bie { 2142f18c2d1STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 2152f18c2d1STiwei Bie 2162f18c2d1STiwei Bie /* 2172f18c2d1STiwei Bie * If the host supports indirect descriptor tables, and we have multiple 2182f18c2d1STiwei Bie * buffers, then go indirect. FIXME: tune this threshold 2192f18c2d1STiwei Bie */ 2202f18c2d1STiwei Bie return (vq->indirect && total_sg > 1 && vq->vq.num_free); 2212f18c2d1STiwei Bie } 2222f18c2d1STiwei Bie 223d26c96c8SAndy Lutomirski /* 2241a937693SMichael S. Tsirkin * Modern virtio devices have feature bits to specify whether they need a 2251a937693SMichael S. Tsirkin * quirk and bypass the IOMMU. If not there, just use the DMA API. 2261a937693SMichael S. Tsirkin * 2271a937693SMichael S. Tsirkin * If there, the interaction between virtio and DMA API is messy. 228d26c96c8SAndy Lutomirski * 229d26c96c8SAndy Lutomirski * On most systems with virtio, physical addresses match bus addresses, 230d26c96c8SAndy Lutomirski * and it doesn't particularly matter whether we use the DMA API. 231d26c96c8SAndy Lutomirski * 232d26c96c8SAndy Lutomirski * On some systems, including Xen and any system with a physical device 233d26c96c8SAndy Lutomirski * that speaks virtio behind a physical IOMMU, we must use the DMA API 234d26c96c8SAndy Lutomirski * for virtio DMA to work at all. 235d26c96c8SAndy Lutomirski * 236d26c96c8SAndy Lutomirski * On other systems, including SPARC and PPC64, virtio-pci devices are 237d26c96c8SAndy Lutomirski * enumerated as though they are behind an IOMMU, but the virtio host 238d26c96c8SAndy Lutomirski * ignores the IOMMU, so we must either pretend that the IOMMU isn't 239d26c96c8SAndy Lutomirski * there or somehow map everything as the identity. 240d26c96c8SAndy Lutomirski * 241d26c96c8SAndy Lutomirski * For the time being, we preserve historic behavior and bypass the DMA 242d26c96c8SAndy Lutomirski * API. 2431a937693SMichael S. Tsirkin * 2441a937693SMichael S. Tsirkin * TODO: install a per-device DMA ops structure that does the right thing 2451a937693SMichael S. Tsirkin * taking into account all the above quirks, and use the DMA API 2461a937693SMichael S. Tsirkin * unconditionally on data path. 247d26c96c8SAndy Lutomirski */ 248d26c96c8SAndy Lutomirski 249d26c96c8SAndy Lutomirski static bool vring_use_dma_api(struct virtio_device *vdev) 250d26c96c8SAndy Lutomirski { 2511a937693SMichael S. Tsirkin if (!virtio_has_iommu_quirk(vdev)) 2521a937693SMichael S. Tsirkin return true; 2531a937693SMichael S. Tsirkin 2541a937693SMichael S. Tsirkin /* Otherwise, we are left to guess. */ 25578fe3987SAndy Lutomirski /* 25678fe3987SAndy Lutomirski * In theory, it's possible to have a buggy QEMU-supposed 25778fe3987SAndy Lutomirski * emulated Q35 IOMMU and Xen enabled at the same time. On 25878fe3987SAndy Lutomirski * such a configuration, virtio has never worked and will 25978fe3987SAndy Lutomirski * not work without an even larger kludge. Instead, enable 26078fe3987SAndy Lutomirski * the DMA API if we're a Xen guest, which at least allows 26178fe3987SAndy Lutomirski * all of the sensible Xen configurations to work correctly. 26278fe3987SAndy Lutomirski */ 26378fe3987SAndy Lutomirski if (xen_domain()) 26478fe3987SAndy Lutomirski return true; 26578fe3987SAndy Lutomirski 266d26c96c8SAndy Lutomirski return false; 267d26c96c8SAndy Lutomirski } 268d26c96c8SAndy Lutomirski 269d79dca75STiwei Bie static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, 270d79dca75STiwei Bie dma_addr_t *dma_handle, gfp_t flag) 271d79dca75STiwei Bie { 272d79dca75STiwei Bie if (vring_use_dma_api(vdev)) { 273d79dca75STiwei Bie return dma_alloc_coherent(vdev->dev.parent, size, 274d79dca75STiwei Bie dma_handle, flag); 275d79dca75STiwei Bie } else { 276d79dca75STiwei Bie void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag); 277d79dca75STiwei Bie 278d79dca75STiwei Bie if (queue) { 279d79dca75STiwei Bie phys_addr_t phys_addr = virt_to_phys(queue); 280d79dca75STiwei Bie *dma_handle = (dma_addr_t)phys_addr; 281d79dca75STiwei Bie 282d79dca75STiwei Bie /* 283d79dca75STiwei Bie * Sanity check: make sure we dind't truncate 284d79dca75STiwei Bie * the address. The only arches I can find that 285d79dca75STiwei Bie * have 64-bit phys_addr_t but 32-bit dma_addr_t 286d79dca75STiwei Bie * are certain non-highmem MIPS and x86 287d79dca75STiwei Bie * configurations, but these configurations 288d79dca75STiwei Bie * should never allocate physical pages above 32 289d79dca75STiwei Bie * bits, so this is fine. Just in case, throw a 290d79dca75STiwei Bie * warning and abort if we end up with an 291d79dca75STiwei Bie * unrepresentable address. 292d79dca75STiwei Bie */ 293d79dca75STiwei Bie if (WARN_ON_ONCE(*dma_handle != phys_addr)) { 294d79dca75STiwei Bie free_pages_exact(queue, PAGE_ALIGN(size)); 295d79dca75STiwei Bie return NULL; 296d79dca75STiwei Bie } 297d79dca75STiwei Bie } 298d79dca75STiwei Bie return queue; 299d79dca75STiwei Bie } 300d79dca75STiwei Bie } 301d79dca75STiwei Bie 302d79dca75STiwei Bie static void vring_free_queue(struct virtio_device *vdev, size_t size, 303d79dca75STiwei Bie void *queue, dma_addr_t dma_handle) 304d79dca75STiwei Bie { 305d79dca75STiwei Bie if (vring_use_dma_api(vdev)) 306d79dca75STiwei Bie dma_free_coherent(vdev->dev.parent, size, queue, dma_handle); 307d79dca75STiwei Bie else 308d79dca75STiwei Bie free_pages_exact(queue, PAGE_ALIGN(size)); 309d79dca75STiwei Bie } 310d79dca75STiwei Bie 311780bc790SAndy Lutomirski /* 312780bc790SAndy Lutomirski * The DMA ops on various arches are rather gnarly right now, and 313780bc790SAndy Lutomirski * making all of the arch DMA ops work on the vring device itself 314780bc790SAndy Lutomirski * is a mess. For now, we use the parent device for DMA ops. 315780bc790SAndy Lutomirski */ 31675bfa81bSMichael S. Tsirkin static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq) 317780bc790SAndy Lutomirski { 318780bc790SAndy Lutomirski return vq->vq.vdev->dev.parent; 319780bc790SAndy Lutomirski } 320780bc790SAndy Lutomirski 321780bc790SAndy Lutomirski /* Map one sg entry. */ 322780bc790SAndy Lutomirski static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq, 323780bc790SAndy Lutomirski struct scatterlist *sg, 324780bc790SAndy Lutomirski enum dma_data_direction direction) 325780bc790SAndy Lutomirski { 326fb3fba6bSTiwei Bie if (!vq->use_dma_api) 327780bc790SAndy Lutomirski return (dma_addr_t)sg_phys(sg); 328780bc790SAndy Lutomirski 329780bc790SAndy Lutomirski /* 330780bc790SAndy Lutomirski * We can't use dma_map_sg, because we don't use scatterlists in 331780bc790SAndy Lutomirski * the way it expects (we don't guarantee that the scatterlist 332780bc790SAndy Lutomirski * will exist for the lifetime of the mapping). 333780bc790SAndy Lutomirski */ 334780bc790SAndy Lutomirski return dma_map_page(vring_dma_dev(vq), 335780bc790SAndy Lutomirski sg_page(sg), sg->offset, sg->length, 336780bc790SAndy Lutomirski direction); 337780bc790SAndy Lutomirski } 338780bc790SAndy Lutomirski 339780bc790SAndy Lutomirski static dma_addr_t vring_map_single(const struct vring_virtqueue *vq, 340780bc790SAndy Lutomirski void *cpu_addr, size_t size, 341780bc790SAndy Lutomirski enum dma_data_direction direction) 342780bc790SAndy Lutomirski { 343fb3fba6bSTiwei Bie if (!vq->use_dma_api) 344780bc790SAndy Lutomirski return (dma_addr_t)virt_to_phys(cpu_addr); 345780bc790SAndy Lutomirski 346780bc790SAndy Lutomirski return dma_map_single(vring_dma_dev(vq), 347780bc790SAndy Lutomirski cpu_addr, size, direction); 348780bc790SAndy Lutomirski } 349780bc790SAndy Lutomirski 350e6f633e5STiwei Bie static int vring_mapping_error(const struct vring_virtqueue *vq, 351e6f633e5STiwei Bie dma_addr_t addr) 352e6f633e5STiwei Bie { 353fb3fba6bSTiwei Bie if (!vq->use_dma_api) 354e6f633e5STiwei Bie return 0; 355e6f633e5STiwei Bie 356e6f633e5STiwei Bie return dma_mapping_error(vring_dma_dev(vq), addr); 357e6f633e5STiwei Bie } 358e6f633e5STiwei Bie 359e6f633e5STiwei Bie 360e6f633e5STiwei Bie /* 361e6f633e5STiwei Bie * Split ring specific functions - *_split(). 362e6f633e5STiwei Bie */ 363e6f633e5STiwei Bie 364138fd251STiwei Bie static void vring_unmap_one_split(const struct vring_virtqueue *vq, 365780bc790SAndy Lutomirski struct vring_desc *desc) 366780bc790SAndy Lutomirski { 367780bc790SAndy Lutomirski u16 flags; 368780bc790SAndy Lutomirski 369fb3fba6bSTiwei Bie if (!vq->use_dma_api) 370780bc790SAndy Lutomirski return; 371780bc790SAndy Lutomirski 372780bc790SAndy Lutomirski flags = virtio16_to_cpu(vq->vq.vdev, desc->flags); 373780bc790SAndy Lutomirski 374780bc790SAndy Lutomirski if (flags & VRING_DESC_F_INDIRECT) { 375780bc790SAndy Lutomirski dma_unmap_single(vring_dma_dev(vq), 376780bc790SAndy Lutomirski virtio64_to_cpu(vq->vq.vdev, desc->addr), 377780bc790SAndy Lutomirski virtio32_to_cpu(vq->vq.vdev, desc->len), 378780bc790SAndy Lutomirski (flags & VRING_DESC_F_WRITE) ? 379780bc790SAndy Lutomirski DMA_FROM_DEVICE : DMA_TO_DEVICE); 380780bc790SAndy Lutomirski } else { 381780bc790SAndy Lutomirski dma_unmap_page(vring_dma_dev(vq), 382780bc790SAndy Lutomirski virtio64_to_cpu(vq->vq.vdev, desc->addr), 383780bc790SAndy Lutomirski virtio32_to_cpu(vq->vq.vdev, desc->len), 384780bc790SAndy Lutomirski (flags & VRING_DESC_F_WRITE) ? 385780bc790SAndy Lutomirski DMA_FROM_DEVICE : DMA_TO_DEVICE); 386780bc790SAndy Lutomirski } 387780bc790SAndy Lutomirski } 388780bc790SAndy Lutomirski 389138fd251STiwei Bie static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq, 390138fd251STiwei Bie unsigned int total_sg, 391138fd251STiwei Bie gfp_t gfp) 3929fa29b9dSMark McLoughlin { 3939fa29b9dSMark McLoughlin struct vring_desc *desc; 394b25bd251SRusty Russell unsigned int i; 3959fa29b9dSMark McLoughlin 396b92b1b89SWill Deacon /* 397b92b1b89SWill Deacon * We require lowmem mappings for the descriptors because 398b92b1b89SWill Deacon * otherwise virt_to_phys will give us bogus addresses in the 399b92b1b89SWill Deacon * virtqueue. 400b92b1b89SWill Deacon */ 40182107539SMichal Hocko gfp &= ~__GFP_HIGHMEM; 402b92b1b89SWill Deacon 4036da2ec56SKees Cook desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp); 4049fa29b9dSMark McLoughlin if (!desc) 405b25bd251SRusty Russell return NULL; 4069fa29b9dSMark McLoughlin 407b25bd251SRusty Russell for (i = 0; i < total_sg; i++) 40800e6f3d9SMichael S. Tsirkin desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); 409b25bd251SRusty Russell return desc; 4109fa29b9dSMark McLoughlin } 4119fa29b9dSMark McLoughlin 412138fd251STiwei Bie static inline int virtqueue_add_split(struct virtqueue *_vq, 41313816c76SRusty Russell struct scatterlist *sgs[], 414eeebf9b1SRusty Russell unsigned int total_sg, 41513816c76SRusty Russell unsigned int out_sgs, 41613816c76SRusty Russell unsigned int in_sgs, 417bbd603efSMichael S. Tsirkin void *data, 4185a08b04fSMichael S. Tsirkin void *ctx, 419bbd603efSMichael S. Tsirkin gfp_t gfp) 4200a8a69ddSRusty Russell { 4210a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 42213816c76SRusty Russell struct scatterlist *sg; 423b25bd251SRusty Russell struct vring_desc *desc; 424780bc790SAndy Lutomirski unsigned int i, n, avail, descs_used, uninitialized_var(prev), err_idx; 4251fe9b6feSMichael S. Tsirkin int head; 426b25bd251SRusty Russell bool indirect; 4270a8a69ddSRusty Russell 4289fa29b9dSMark McLoughlin START_USE(vq); 4299fa29b9dSMark McLoughlin 4300a8a69ddSRusty Russell BUG_ON(data == NULL); 4315a08b04fSMichael S. Tsirkin BUG_ON(ctx && vq->indirect); 4329fa29b9dSMark McLoughlin 43370670444SRusty Russell if (unlikely(vq->broken)) { 43470670444SRusty Russell END_USE(vq); 43570670444SRusty Russell return -EIO; 43670670444SRusty Russell } 43770670444SRusty Russell 4384d6a105eSTiwei Bie LAST_ADD_TIME_UPDATE(vq); 439e93300b1SRusty Russell 44013816c76SRusty Russell BUG_ON(total_sg == 0); 4410a8a69ddSRusty Russell 442b25bd251SRusty Russell head = vq->free_head; 443b25bd251SRusty Russell 4442f18c2d1STiwei Bie if (virtqueue_use_indirect(_vq, total_sg)) 445138fd251STiwei Bie desc = alloc_indirect_split(_vq, total_sg, gfp); 44644ed8089SRichard W.M. Jones else { 447b25bd251SRusty Russell desc = NULL; 448e593bf97STiwei Bie WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect); 44944ed8089SRichard W.M. Jones } 450b25bd251SRusty Russell 451b25bd251SRusty Russell if (desc) { 452b25bd251SRusty Russell /* Use a single buffer which doesn't continue */ 453780bc790SAndy Lutomirski indirect = true; 454b25bd251SRusty Russell /* Set up rest to use this indirect table. */ 455b25bd251SRusty Russell i = 0; 456b25bd251SRusty Russell descs_used = 1; 457b25bd251SRusty Russell } else { 458780bc790SAndy Lutomirski indirect = false; 459e593bf97STiwei Bie desc = vq->split.vring.desc; 460b25bd251SRusty Russell i = head; 461b25bd251SRusty Russell descs_used = total_sg; 462b25bd251SRusty Russell } 463b25bd251SRusty Russell 464b25bd251SRusty Russell if (vq->vq.num_free < descs_used) { 4650a8a69ddSRusty Russell pr_debug("Can't add buf len %i - avail = %i\n", 466b25bd251SRusty Russell descs_used, vq->vq.num_free); 46744653eaeSRusty Russell /* FIXME: for historical reasons, we force a notify here if 46844653eaeSRusty Russell * there are outgoing parts to the buffer. Presumably the 46944653eaeSRusty Russell * host should service the ring ASAP. */ 47013816c76SRusty Russell if (out_sgs) 471426e3e0aSRusty Russell vq->notify(&vq->vq); 47258625edfSWei Yongjun if (indirect) 47358625edfSWei Yongjun kfree(desc); 4740a8a69ddSRusty Russell END_USE(vq); 4750a8a69ddSRusty Russell return -ENOSPC; 4760a8a69ddSRusty Russell } 4770a8a69ddSRusty Russell 47813816c76SRusty Russell for (n = 0; n < out_sgs; n++) { 479eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 480780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE); 481780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 482780bc790SAndy Lutomirski goto unmap_release; 483780bc790SAndy Lutomirski 48400e6f3d9SMichael S. Tsirkin desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT); 485780bc790SAndy Lutomirski desc[i].addr = cpu_to_virtio64(_vq->vdev, addr); 48600e6f3d9SMichael S. Tsirkin desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); 4870a8a69ddSRusty Russell prev = i; 48800e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(_vq->vdev, desc[i].next); 4890a8a69ddSRusty Russell } 49013816c76SRusty Russell } 49113816c76SRusty Russell for (; n < (out_sgs + in_sgs); n++) { 492eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 493780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE); 494780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 495780bc790SAndy Lutomirski goto unmap_release; 496780bc790SAndy Lutomirski 49700e6f3d9SMichael S. Tsirkin desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE); 498780bc790SAndy Lutomirski desc[i].addr = cpu_to_virtio64(_vq->vdev, addr); 49900e6f3d9SMichael S. Tsirkin desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); 5000a8a69ddSRusty Russell prev = i; 50100e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(_vq->vdev, desc[i].next); 50213816c76SRusty Russell } 5030a8a69ddSRusty Russell } 5040a8a69ddSRusty Russell /* Last one doesn't continue. */ 50500e6f3d9SMichael S. Tsirkin desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); 5060a8a69ddSRusty Russell 507780bc790SAndy Lutomirski if (indirect) { 508780bc790SAndy Lutomirski /* Now that the indirect table is filled in, map it. */ 509780bc790SAndy Lutomirski dma_addr_t addr = vring_map_single( 510780bc790SAndy Lutomirski vq, desc, total_sg * sizeof(struct vring_desc), 511780bc790SAndy Lutomirski DMA_TO_DEVICE); 512780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 513780bc790SAndy Lutomirski goto unmap_release; 514780bc790SAndy Lutomirski 515e593bf97STiwei Bie vq->split.vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, 516e593bf97STiwei Bie VRING_DESC_F_INDIRECT); 517e593bf97STiwei Bie vq->split.vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, 518e593bf97STiwei Bie addr); 519780bc790SAndy Lutomirski 520e593bf97STiwei Bie vq->split.vring.desc[head].len = cpu_to_virtio32(_vq->vdev, 521e593bf97STiwei Bie total_sg * sizeof(struct vring_desc)); 522780bc790SAndy Lutomirski } 523780bc790SAndy Lutomirski 524780bc790SAndy Lutomirski /* We're using some buffers from the free list. */ 525780bc790SAndy Lutomirski vq->vq.num_free -= descs_used; 526780bc790SAndy Lutomirski 5270a8a69ddSRusty Russell /* Update free pointer */ 528b25bd251SRusty Russell if (indirect) 529e593bf97STiwei Bie vq->free_head = virtio16_to_cpu(_vq->vdev, 530e593bf97STiwei Bie vq->split.vring.desc[head].next); 531b25bd251SRusty Russell else 5320a8a69ddSRusty Russell vq->free_head = i; 5330a8a69ddSRusty Russell 534780bc790SAndy Lutomirski /* Store token and indirect buffer state. */ 535cbeedb72STiwei Bie vq->split.desc_state[head].data = data; 536780bc790SAndy Lutomirski if (indirect) 537cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc = desc; 53887646a34SJason Wang else 539cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc = ctx; 5400a8a69ddSRusty Russell 5410a8a69ddSRusty Russell /* Put entry in available array (but don't update avail->idx until they 5423b720b8cSRusty Russell * do sync). */ 543e593bf97STiwei Bie avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1); 544e593bf97STiwei Bie vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); 5450a8a69ddSRusty Russell 546ee7cd898SRusty Russell /* Descriptors and available array need to be set before we expose the 547ee7cd898SRusty Russell * new available array entries. */ 548a9a0fef7SRusty Russell virtio_wmb(vq->weak_barriers); 549e593bf97STiwei Bie vq->split.avail_idx_shadow++; 550e593bf97STiwei Bie vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, 551e593bf97STiwei Bie vq->split.avail_idx_shadow); 552ee7cd898SRusty Russell vq->num_added++; 553ee7cd898SRusty Russell 5545e05bf58STetsuo Handa pr_debug("Added buffer head %i to %p\n", head, vq); 5555e05bf58STetsuo Handa END_USE(vq); 5565e05bf58STetsuo Handa 557ee7cd898SRusty Russell /* This is very unlikely, but theoretically possible. Kick 558ee7cd898SRusty Russell * just in case. */ 559ee7cd898SRusty Russell if (unlikely(vq->num_added == (1 << 16) - 1)) 560ee7cd898SRusty Russell virtqueue_kick(_vq); 561ee7cd898SRusty Russell 56298e8c6bcSRusty Russell return 0; 563780bc790SAndy Lutomirski 564780bc790SAndy Lutomirski unmap_release: 565780bc790SAndy Lutomirski err_idx = i; 566780bc790SAndy Lutomirski i = head; 567780bc790SAndy Lutomirski 568780bc790SAndy Lutomirski for (n = 0; n < total_sg; n++) { 569780bc790SAndy Lutomirski if (i == err_idx) 570780bc790SAndy Lutomirski break; 571138fd251STiwei Bie vring_unmap_one_split(vq, &desc[i]); 572e593bf97STiwei Bie i = virtio16_to_cpu(_vq->vdev, vq->split.vring.desc[i].next); 573780bc790SAndy Lutomirski } 574780bc790SAndy Lutomirski 575780bc790SAndy Lutomirski if (indirect) 576780bc790SAndy Lutomirski kfree(desc); 577780bc790SAndy Lutomirski 5783cc36f6eSMichael S. Tsirkin END_USE(vq); 579780bc790SAndy Lutomirski return -EIO; 5800a8a69ddSRusty Russell } 58113816c76SRusty Russell 582138fd251STiwei Bie static bool virtqueue_kick_prepare_split(struct virtqueue *_vq) 5830a8a69ddSRusty Russell { 5840a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 585a5c262c5SMichael S. Tsirkin u16 new, old; 58641f0377fSRusty Russell bool needs_kick; 58741f0377fSRusty Russell 5880a8a69ddSRusty Russell START_USE(vq); 589a72caae2SJason Wang /* We need to expose available array entries before checking avail 590a72caae2SJason Wang * event. */ 591a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 5920a8a69ddSRusty Russell 593e593bf97STiwei Bie old = vq->split.avail_idx_shadow - vq->num_added; 594e593bf97STiwei Bie new = vq->split.avail_idx_shadow; 5950a8a69ddSRusty Russell vq->num_added = 0; 5960a8a69ddSRusty Russell 5974d6a105eSTiwei Bie LAST_ADD_TIME_CHECK(vq); 5984d6a105eSTiwei Bie LAST_ADD_TIME_INVALID(vq); 599e93300b1SRusty Russell 60041f0377fSRusty Russell if (vq->event) { 601e593bf97STiwei Bie needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, 602e593bf97STiwei Bie vring_avail_event(&vq->split.vring)), 60341f0377fSRusty Russell new, old); 60441f0377fSRusty Russell } else { 605e593bf97STiwei Bie needs_kick = !(vq->split.vring.used->flags & 606e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 607e593bf97STiwei Bie VRING_USED_F_NO_NOTIFY)); 60841f0377fSRusty Russell } 6090a8a69ddSRusty Russell END_USE(vq); 61041f0377fSRusty Russell return needs_kick; 61141f0377fSRusty Russell } 612138fd251STiwei Bie 613138fd251STiwei Bie static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, 6145a08b04fSMichael S. Tsirkin void **ctx) 6150a8a69ddSRusty Russell { 616780bc790SAndy Lutomirski unsigned int i, j; 617c60923cbSGonglei __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); 6180a8a69ddSRusty Russell 6190a8a69ddSRusty Russell /* Clear data ptr. */ 620cbeedb72STiwei Bie vq->split.desc_state[head].data = NULL; 6210a8a69ddSRusty Russell 622780bc790SAndy Lutomirski /* Put back on free list: unmap first-level descriptors and find end */ 6230a8a69ddSRusty Russell i = head; 6249fa29b9dSMark McLoughlin 625e593bf97STiwei Bie while (vq->split.vring.desc[i].flags & nextflag) { 626e593bf97STiwei Bie vring_unmap_one_split(vq, &vq->split.vring.desc[i]); 627e593bf97STiwei Bie i = virtio16_to_cpu(vq->vq.vdev, vq->split.vring.desc[i].next); 62806ca287dSRusty Russell vq->vq.num_free++; 6290a8a69ddSRusty Russell } 6300a8a69ddSRusty Russell 631e593bf97STiwei Bie vring_unmap_one_split(vq, &vq->split.vring.desc[i]); 632e593bf97STiwei Bie vq->split.vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, 633e593bf97STiwei Bie vq->free_head); 6340a8a69ddSRusty Russell vq->free_head = head; 635780bc790SAndy Lutomirski 6360a8a69ddSRusty Russell /* Plus final descriptor */ 63706ca287dSRusty Russell vq->vq.num_free++; 638780bc790SAndy Lutomirski 6395a08b04fSMichael S. Tsirkin if (vq->indirect) { 640cbeedb72STiwei Bie struct vring_desc *indir_desc = 641cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc; 6425a08b04fSMichael S. Tsirkin u32 len; 6435a08b04fSMichael S. Tsirkin 6445a08b04fSMichael S. Tsirkin /* Free the indirect table, if any, now that it's unmapped. */ 6455a08b04fSMichael S. Tsirkin if (!indir_desc) 6465a08b04fSMichael S. Tsirkin return; 6475a08b04fSMichael S. Tsirkin 648e593bf97STiwei Bie len = virtio32_to_cpu(vq->vq.vdev, 649e593bf97STiwei Bie vq->split.vring.desc[head].len); 650780bc790SAndy Lutomirski 651e593bf97STiwei Bie BUG_ON(!(vq->split.vring.desc[head].flags & 652780bc790SAndy Lutomirski cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT))); 653780bc790SAndy Lutomirski BUG_ON(len == 0 || len % sizeof(struct vring_desc)); 654780bc790SAndy Lutomirski 655780bc790SAndy Lutomirski for (j = 0; j < len / sizeof(struct vring_desc); j++) 656138fd251STiwei Bie vring_unmap_one_split(vq, &indir_desc[j]); 657780bc790SAndy Lutomirski 6585a08b04fSMichael S. Tsirkin kfree(indir_desc); 659cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc = NULL; 6605a08b04fSMichael S. Tsirkin } else if (ctx) { 661cbeedb72STiwei Bie *ctx = vq->split.desc_state[head].indir_desc; 662780bc790SAndy Lutomirski } 6630a8a69ddSRusty Russell } 6640a8a69ddSRusty Russell 665138fd251STiwei Bie static inline bool more_used_split(const struct vring_virtqueue *vq) 6660a8a69ddSRusty Russell { 667e593bf97STiwei Bie return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, 668e593bf97STiwei Bie vq->split.vring.used->idx); 6690a8a69ddSRusty Russell } 6700a8a69ddSRusty Russell 671138fd251STiwei Bie static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, 672138fd251STiwei Bie unsigned int *len, 6735a08b04fSMichael S. Tsirkin void **ctx) 6740a8a69ddSRusty Russell { 6750a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 6760a8a69ddSRusty Russell void *ret; 6770a8a69ddSRusty Russell unsigned int i; 6783b720b8cSRusty Russell u16 last_used; 6790a8a69ddSRusty Russell 6800a8a69ddSRusty Russell START_USE(vq); 6810a8a69ddSRusty Russell 6825ef82752SRusty Russell if (unlikely(vq->broken)) { 6835ef82752SRusty Russell END_USE(vq); 6845ef82752SRusty Russell return NULL; 6855ef82752SRusty Russell } 6865ef82752SRusty Russell 687138fd251STiwei Bie if (!more_used_split(vq)) { 6880a8a69ddSRusty Russell pr_debug("No more buffers in queue\n"); 6890a8a69ddSRusty Russell END_USE(vq); 6900a8a69ddSRusty Russell return NULL; 6910a8a69ddSRusty Russell } 6920a8a69ddSRusty Russell 6932d61ba95SMichael S. Tsirkin /* Only get used array entries after they have been exposed by host. */ 694a9a0fef7SRusty Russell virtio_rmb(vq->weak_barriers); 6952d61ba95SMichael S. Tsirkin 696e593bf97STiwei Bie last_used = (vq->last_used_idx & (vq->split.vring.num - 1)); 697e593bf97STiwei Bie i = virtio32_to_cpu(_vq->vdev, 698e593bf97STiwei Bie vq->split.vring.used->ring[last_used].id); 699e593bf97STiwei Bie *len = virtio32_to_cpu(_vq->vdev, 700e593bf97STiwei Bie vq->split.vring.used->ring[last_used].len); 7010a8a69ddSRusty Russell 702e593bf97STiwei Bie if (unlikely(i >= vq->split.vring.num)) { 7030a8a69ddSRusty Russell BAD_RING(vq, "id %u out of range\n", i); 7040a8a69ddSRusty Russell return NULL; 7050a8a69ddSRusty Russell } 706cbeedb72STiwei Bie if (unlikely(!vq->split.desc_state[i].data)) { 7070a8a69ddSRusty Russell BAD_RING(vq, "id %u is not a head!\n", i); 7080a8a69ddSRusty Russell return NULL; 7090a8a69ddSRusty Russell } 7100a8a69ddSRusty Russell 711138fd251STiwei Bie /* detach_buf_split clears data, so grab it now. */ 712cbeedb72STiwei Bie ret = vq->split.desc_state[i].data; 713138fd251STiwei Bie detach_buf_split(vq, i, ctx); 7140a8a69ddSRusty Russell vq->last_used_idx++; 715a5c262c5SMichael S. Tsirkin /* If we expect an interrupt for the next entry, tell host 716a5c262c5SMichael S. Tsirkin * by writing event index and flush out the write before 717a5c262c5SMichael S. Tsirkin * the read in the next get_buf call. */ 718e593bf97STiwei Bie if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) 719788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 720e593bf97STiwei Bie &vring_used_event(&vq->split.vring), 721788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); 722a5c262c5SMichael S. Tsirkin 7234d6a105eSTiwei Bie LAST_ADD_TIME_INVALID(vq); 724e93300b1SRusty Russell 7250a8a69ddSRusty Russell END_USE(vq); 7260a8a69ddSRusty Russell return ret; 7270a8a69ddSRusty Russell } 728138fd251STiwei Bie 729138fd251STiwei Bie static void virtqueue_disable_cb_split(struct virtqueue *_vq) 730138fd251STiwei Bie { 731138fd251STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 732138fd251STiwei Bie 733e593bf97STiwei Bie if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { 734e593bf97STiwei Bie vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 735138fd251STiwei Bie if (!vq->event) 736e593bf97STiwei Bie vq->split.vring.avail->flags = 737e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 738e593bf97STiwei Bie vq->split.avail_flags_shadow); 739138fd251STiwei Bie } 740138fd251STiwei Bie } 741138fd251STiwei Bie 742138fd251STiwei Bie static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq) 743cc229884SMichael S. Tsirkin { 744cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 745cc229884SMichael S. Tsirkin u16 last_used_idx; 746cc229884SMichael S. Tsirkin 747cc229884SMichael S. Tsirkin START_USE(vq); 748cc229884SMichael S. Tsirkin 749cc229884SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 750cc229884SMichael S. Tsirkin * more to do. */ 751cc229884SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to 752cc229884SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 753cc229884SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 754e593bf97STiwei Bie if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 755e593bf97STiwei Bie vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 7560ea1e4a6SLadi Prosek if (!vq->event) 757e593bf97STiwei Bie vq->split.vring.avail->flags = 758e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 759e593bf97STiwei Bie vq->split.avail_flags_shadow); 760f277ec42SVenkatesh Srinivas } 761e593bf97STiwei Bie vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev, 762e593bf97STiwei Bie last_used_idx = vq->last_used_idx); 763cc229884SMichael S. Tsirkin END_USE(vq); 764cc229884SMichael S. Tsirkin return last_used_idx; 765cc229884SMichael S. Tsirkin } 766138fd251STiwei Bie 767138fd251STiwei Bie static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx) 768138fd251STiwei Bie { 769138fd251STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 770138fd251STiwei Bie 771138fd251STiwei Bie return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, 772e593bf97STiwei Bie vq->split.vring.used->idx); 773138fd251STiwei Bie } 774138fd251STiwei Bie 775138fd251STiwei Bie static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq) 7767ab358c2SMichael S. Tsirkin { 7777ab358c2SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 7787ab358c2SMichael S. Tsirkin u16 bufs; 7797ab358c2SMichael S. Tsirkin 7807ab358c2SMichael S. Tsirkin START_USE(vq); 7817ab358c2SMichael S. Tsirkin 7827ab358c2SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 7837ab358c2SMichael S. Tsirkin * more to do. */ 7847ab358c2SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to 7857ab358c2SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 7860ea1e4a6SLadi Prosek * entry. Always update the event index to keep code simple. */ 787e593bf97STiwei Bie if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 788e593bf97STiwei Bie vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 7890ea1e4a6SLadi Prosek if (!vq->event) 790e593bf97STiwei Bie vq->split.vring.avail->flags = 791e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 792e593bf97STiwei Bie vq->split.avail_flags_shadow); 793f277ec42SVenkatesh Srinivas } 7947ab358c2SMichael S. Tsirkin /* TODO: tune this threshold */ 795e593bf97STiwei Bie bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4; 796788e5b3aSMichael S. Tsirkin 797788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 798e593bf97STiwei Bie &vring_used_event(&vq->split.vring), 799788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); 800788e5b3aSMichael S. Tsirkin 801e593bf97STiwei Bie if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx) 802e593bf97STiwei Bie - vq->last_used_idx) > bufs)) { 8037ab358c2SMichael S. Tsirkin END_USE(vq); 8047ab358c2SMichael S. Tsirkin return false; 8057ab358c2SMichael S. Tsirkin } 8067ab358c2SMichael S. Tsirkin 8077ab358c2SMichael S. Tsirkin END_USE(vq); 8087ab358c2SMichael S. Tsirkin return true; 8097ab358c2SMichael S. Tsirkin } 8107ab358c2SMichael S. Tsirkin 811138fd251STiwei Bie static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq) 812c021eac4SShirley Ma { 813c021eac4SShirley Ma struct vring_virtqueue *vq = to_vvq(_vq); 814c021eac4SShirley Ma unsigned int i; 815c021eac4SShirley Ma void *buf; 816c021eac4SShirley Ma 817c021eac4SShirley Ma START_USE(vq); 818c021eac4SShirley Ma 819e593bf97STiwei Bie for (i = 0; i < vq->split.vring.num; i++) { 820cbeedb72STiwei Bie if (!vq->split.desc_state[i].data) 821c021eac4SShirley Ma continue; 822138fd251STiwei Bie /* detach_buf_split clears data, so grab it now. */ 823cbeedb72STiwei Bie buf = vq->split.desc_state[i].data; 824138fd251STiwei Bie detach_buf_split(vq, i, NULL); 825e593bf97STiwei Bie vq->split.avail_idx_shadow--; 826e593bf97STiwei Bie vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, 827e593bf97STiwei Bie vq->split.avail_idx_shadow); 828c021eac4SShirley Ma END_USE(vq); 829c021eac4SShirley Ma return buf; 830c021eac4SShirley Ma } 831c021eac4SShirley Ma /* That should have freed everything. */ 832e593bf97STiwei Bie BUG_ON(vq->vq.num_free != vq->split.vring.num); 833c021eac4SShirley Ma 834c021eac4SShirley Ma END_USE(vq); 835c021eac4SShirley Ma return NULL; 836c021eac4SShirley Ma } 837138fd251STiwei Bie 838d79dca75STiwei Bie static struct virtqueue *vring_create_virtqueue_split( 839d79dca75STiwei Bie unsigned int index, 840d79dca75STiwei Bie unsigned int num, 841d79dca75STiwei Bie unsigned int vring_align, 842d79dca75STiwei Bie struct virtio_device *vdev, 843d79dca75STiwei Bie bool weak_barriers, 844d79dca75STiwei Bie bool may_reduce_num, 845d79dca75STiwei Bie bool context, 846d79dca75STiwei Bie bool (*notify)(struct virtqueue *), 847d79dca75STiwei Bie void (*callback)(struct virtqueue *), 848d79dca75STiwei Bie const char *name) 849d79dca75STiwei Bie { 850d79dca75STiwei Bie struct virtqueue *vq; 851d79dca75STiwei Bie void *queue = NULL; 852d79dca75STiwei Bie dma_addr_t dma_addr; 853d79dca75STiwei Bie size_t queue_size_in_bytes; 854d79dca75STiwei Bie struct vring vring; 855d79dca75STiwei Bie 856d79dca75STiwei Bie /* We assume num is a power of 2. */ 857d79dca75STiwei Bie if (num & (num - 1)) { 858d79dca75STiwei Bie dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); 859d79dca75STiwei Bie return NULL; 860d79dca75STiwei Bie } 861d79dca75STiwei Bie 862d79dca75STiwei Bie /* TODO: allocate each queue chunk individually */ 863d79dca75STiwei Bie for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { 864d79dca75STiwei Bie queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 865d79dca75STiwei Bie &dma_addr, 866d79dca75STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 867d79dca75STiwei Bie if (queue) 868d79dca75STiwei Bie break; 869d79dca75STiwei Bie } 870d79dca75STiwei Bie 871d79dca75STiwei Bie if (!num) 872d79dca75STiwei Bie return NULL; 873d79dca75STiwei Bie 874d79dca75STiwei Bie if (!queue) { 875d79dca75STiwei Bie /* Try to get a single page. You are my only hope! */ 876d79dca75STiwei Bie queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 877d79dca75STiwei Bie &dma_addr, GFP_KERNEL|__GFP_ZERO); 878d79dca75STiwei Bie } 879d79dca75STiwei Bie if (!queue) 880d79dca75STiwei Bie return NULL; 881d79dca75STiwei Bie 882d79dca75STiwei Bie queue_size_in_bytes = vring_size(num, vring_align); 883d79dca75STiwei Bie vring_init(&vring, num, queue, vring_align); 884d79dca75STiwei Bie 885d79dca75STiwei Bie vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, 886d79dca75STiwei Bie notify, callback, name); 887d79dca75STiwei Bie if (!vq) { 888d79dca75STiwei Bie vring_free_queue(vdev, queue_size_in_bytes, queue, 889d79dca75STiwei Bie dma_addr); 890d79dca75STiwei Bie return NULL; 891d79dca75STiwei Bie } 892d79dca75STiwei Bie 893d79dca75STiwei Bie to_vvq(vq)->split.queue_dma_addr = dma_addr; 894d79dca75STiwei Bie to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes; 895d79dca75STiwei Bie to_vvq(vq)->we_own_ring = true; 896d79dca75STiwei Bie 897d79dca75STiwei Bie return vq; 898d79dca75STiwei Bie } 899d79dca75STiwei Bie 900e6f633e5STiwei Bie 901e6f633e5STiwei Bie /* 9021ce9e605STiwei Bie * Packed ring specific functions - *_packed(). 9031ce9e605STiwei Bie */ 9041ce9e605STiwei Bie 9051ce9e605STiwei Bie static void vring_unmap_state_packed(const struct vring_virtqueue *vq, 9061ce9e605STiwei Bie struct vring_desc_extra_packed *state) 9071ce9e605STiwei Bie { 9081ce9e605STiwei Bie u16 flags; 9091ce9e605STiwei Bie 9101ce9e605STiwei Bie if (!vq->use_dma_api) 9111ce9e605STiwei Bie return; 9121ce9e605STiwei Bie 9131ce9e605STiwei Bie flags = state->flags; 9141ce9e605STiwei Bie 9151ce9e605STiwei Bie if (flags & VRING_DESC_F_INDIRECT) { 9161ce9e605STiwei Bie dma_unmap_single(vring_dma_dev(vq), 9171ce9e605STiwei Bie state->addr, state->len, 9181ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 9191ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 9201ce9e605STiwei Bie } else { 9211ce9e605STiwei Bie dma_unmap_page(vring_dma_dev(vq), 9221ce9e605STiwei Bie state->addr, state->len, 9231ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 9241ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 9251ce9e605STiwei Bie } 9261ce9e605STiwei Bie } 9271ce9e605STiwei Bie 9281ce9e605STiwei Bie static void vring_unmap_desc_packed(const struct vring_virtqueue *vq, 9291ce9e605STiwei Bie struct vring_packed_desc *desc) 9301ce9e605STiwei Bie { 9311ce9e605STiwei Bie u16 flags; 9321ce9e605STiwei Bie 9331ce9e605STiwei Bie if (!vq->use_dma_api) 9341ce9e605STiwei Bie return; 9351ce9e605STiwei Bie 9361ce9e605STiwei Bie flags = le16_to_cpu(desc->flags); 9371ce9e605STiwei Bie 9381ce9e605STiwei Bie if (flags & VRING_DESC_F_INDIRECT) { 9391ce9e605STiwei Bie dma_unmap_single(vring_dma_dev(vq), 9401ce9e605STiwei Bie le64_to_cpu(desc->addr), 9411ce9e605STiwei Bie le32_to_cpu(desc->len), 9421ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 9431ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 9441ce9e605STiwei Bie } else { 9451ce9e605STiwei Bie dma_unmap_page(vring_dma_dev(vq), 9461ce9e605STiwei Bie le64_to_cpu(desc->addr), 9471ce9e605STiwei Bie le32_to_cpu(desc->len), 9481ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 9491ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 9501ce9e605STiwei Bie } 9511ce9e605STiwei Bie } 9521ce9e605STiwei Bie 9531ce9e605STiwei Bie static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg, 9541ce9e605STiwei Bie gfp_t gfp) 9551ce9e605STiwei Bie { 9561ce9e605STiwei Bie struct vring_packed_desc *desc; 9571ce9e605STiwei Bie 9581ce9e605STiwei Bie /* 9591ce9e605STiwei Bie * We require lowmem mappings for the descriptors because 9601ce9e605STiwei Bie * otherwise virt_to_phys will give us bogus addresses in the 9611ce9e605STiwei Bie * virtqueue. 9621ce9e605STiwei Bie */ 9631ce9e605STiwei Bie gfp &= ~__GFP_HIGHMEM; 9641ce9e605STiwei Bie 9651ce9e605STiwei Bie desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp); 9661ce9e605STiwei Bie 9671ce9e605STiwei Bie return desc; 9681ce9e605STiwei Bie } 9691ce9e605STiwei Bie 9701ce9e605STiwei Bie static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, 9711ce9e605STiwei Bie struct scatterlist *sgs[], 9721ce9e605STiwei Bie unsigned int total_sg, 9731ce9e605STiwei Bie unsigned int out_sgs, 9741ce9e605STiwei Bie unsigned int in_sgs, 9751ce9e605STiwei Bie void *data, 9761ce9e605STiwei Bie gfp_t gfp) 9771ce9e605STiwei Bie { 9781ce9e605STiwei Bie struct vring_packed_desc *desc; 9791ce9e605STiwei Bie struct scatterlist *sg; 9801ce9e605STiwei Bie unsigned int i, n, err_idx; 9811ce9e605STiwei Bie u16 head, id; 9821ce9e605STiwei Bie dma_addr_t addr; 9831ce9e605STiwei Bie 9841ce9e605STiwei Bie head = vq->packed.next_avail_idx; 9851ce9e605STiwei Bie desc = alloc_indirect_packed(total_sg, gfp); 9861ce9e605STiwei Bie 9871ce9e605STiwei Bie if (unlikely(vq->vq.num_free < 1)) { 9881ce9e605STiwei Bie pr_debug("Can't add buf len 1 - avail = 0\n"); 9891ce9e605STiwei Bie END_USE(vq); 9901ce9e605STiwei Bie return -ENOSPC; 9911ce9e605STiwei Bie } 9921ce9e605STiwei Bie 9931ce9e605STiwei Bie i = 0; 9941ce9e605STiwei Bie id = vq->free_head; 9951ce9e605STiwei Bie BUG_ON(id == vq->packed.vring.num); 9961ce9e605STiwei Bie 9971ce9e605STiwei Bie for (n = 0; n < out_sgs + in_sgs; n++) { 9981ce9e605STiwei Bie for (sg = sgs[n]; sg; sg = sg_next(sg)) { 9991ce9e605STiwei Bie addr = vring_map_one_sg(vq, sg, n < out_sgs ? 10001ce9e605STiwei Bie DMA_TO_DEVICE : DMA_FROM_DEVICE); 10011ce9e605STiwei Bie if (vring_mapping_error(vq, addr)) 10021ce9e605STiwei Bie goto unmap_release; 10031ce9e605STiwei Bie 10041ce9e605STiwei Bie desc[i].flags = cpu_to_le16(n < out_sgs ? 10051ce9e605STiwei Bie 0 : VRING_DESC_F_WRITE); 10061ce9e605STiwei Bie desc[i].addr = cpu_to_le64(addr); 10071ce9e605STiwei Bie desc[i].len = cpu_to_le32(sg->length); 10081ce9e605STiwei Bie i++; 10091ce9e605STiwei Bie } 10101ce9e605STiwei Bie } 10111ce9e605STiwei Bie 10121ce9e605STiwei Bie /* Now that the indirect table is filled in, map it. */ 10131ce9e605STiwei Bie addr = vring_map_single(vq, desc, 10141ce9e605STiwei Bie total_sg * sizeof(struct vring_packed_desc), 10151ce9e605STiwei Bie DMA_TO_DEVICE); 10161ce9e605STiwei Bie if (vring_mapping_error(vq, addr)) 10171ce9e605STiwei Bie goto unmap_release; 10181ce9e605STiwei Bie 10191ce9e605STiwei Bie vq->packed.vring.desc[head].addr = cpu_to_le64(addr); 10201ce9e605STiwei Bie vq->packed.vring.desc[head].len = cpu_to_le32(total_sg * 10211ce9e605STiwei Bie sizeof(struct vring_packed_desc)); 10221ce9e605STiwei Bie vq->packed.vring.desc[head].id = cpu_to_le16(id); 10231ce9e605STiwei Bie 10241ce9e605STiwei Bie if (vq->use_dma_api) { 10251ce9e605STiwei Bie vq->packed.desc_extra[id].addr = addr; 10261ce9e605STiwei Bie vq->packed.desc_extra[id].len = total_sg * 10271ce9e605STiwei Bie sizeof(struct vring_packed_desc); 10281ce9e605STiwei Bie vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT | 10291ce9e605STiwei Bie vq->packed.avail_used_flags; 10301ce9e605STiwei Bie } 10311ce9e605STiwei Bie 10321ce9e605STiwei Bie /* 10331ce9e605STiwei Bie * A driver MUST NOT make the first descriptor in the list 10341ce9e605STiwei Bie * available before all subsequent descriptors comprising 10351ce9e605STiwei Bie * the list are made available. 10361ce9e605STiwei Bie */ 10371ce9e605STiwei Bie virtio_wmb(vq->weak_barriers); 10381ce9e605STiwei Bie vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT | 10391ce9e605STiwei Bie vq->packed.avail_used_flags); 10401ce9e605STiwei Bie 10411ce9e605STiwei Bie /* We're using some buffers from the free list. */ 10421ce9e605STiwei Bie vq->vq.num_free -= 1; 10431ce9e605STiwei Bie 10441ce9e605STiwei Bie /* Update free pointer */ 10451ce9e605STiwei Bie n = head + 1; 10461ce9e605STiwei Bie if (n >= vq->packed.vring.num) { 10471ce9e605STiwei Bie n = 0; 10481ce9e605STiwei Bie vq->packed.avail_wrap_counter ^= 1; 10491ce9e605STiwei Bie vq->packed.avail_used_flags ^= 10501ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_AVAIL | 10511ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_USED; 10521ce9e605STiwei Bie } 10531ce9e605STiwei Bie vq->packed.next_avail_idx = n; 10541ce9e605STiwei Bie vq->free_head = vq->packed.desc_state[id].next; 10551ce9e605STiwei Bie 10561ce9e605STiwei Bie /* Store token and indirect buffer state. */ 10571ce9e605STiwei Bie vq->packed.desc_state[id].num = 1; 10581ce9e605STiwei Bie vq->packed.desc_state[id].data = data; 10591ce9e605STiwei Bie vq->packed.desc_state[id].indir_desc = desc; 10601ce9e605STiwei Bie vq->packed.desc_state[id].last = id; 10611ce9e605STiwei Bie 10621ce9e605STiwei Bie vq->num_added += 1; 10631ce9e605STiwei Bie 10641ce9e605STiwei Bie pr_debug("Added buffer head %i to %p\n", head, vq); 10651ce9e605STiwei Bie END_USE(vq); 10661ce9e605STiwei Bie 10671ce9e605STiwei Bie return 0; 10681ce9e605STiwei Bie 10691ce9e605STiwei Bie unmap_release: 10701ce9e605STiwei Bie err_idx = i; 10711ce9e605STiwei Bie 10721ce9e605STiwei Bie for (i = 0; i < err_idx; i++) 10731ce9e605STiwei Bie vring_unmap_desc_packed(vq, &desc[i]); 10741ce9e605STiwei Bie 10751ce9e605STiwei Bie kfree(desc); 10761ce9e605STiwei Bie 10771ce9e605STiwei Bie END_USE(vq); 10781ce9e605STiwei Bie return -EIO; 10791ce9e605STiwei Bie } 10801ce9e605STiwei Bie 10811ce9e605STiwei Bie static inline int virtqueue_add_packed(struct virtqueue *_vq, 10821ce9e605STiwei Bie struct scatterlist *sgs[], 10831ce9e605STiwei Bie unsigned int total_sg, 10841ce9e605STiwei Bie unsigned int out_sgs, 10851ce9e605STiwei Bie unsigned int in_sgs, 10861ce9e605STiwei Bie void *data, 10871ce9e605STiwei Bie void *ctx, 10881ce9e605STiwei Bie gfp_t gfp) 10891ce9e605STiwei Bie { 10901ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 10911ce9e605STiwei Bie struct vring_packed_desc *desc; 10921ce9e605STiwei Bie struct scatterlist *sg; 10931ce9e605STiwei Bie unsigned int i, n, c, descs_used, err_idx; 10941ce9e605STiwei Bie __le16 uninitialized_var(head_flags), flags; 10951ce9e605STiwei Bie u16 head, id, uninitialized_var(prev), curr, avail_used_flags; 10961ce9e605STiwei Bie 10971ce9e605STiwei Bie START_USE(vq); 10981ce9e605STiwei Bie 10991ce9e605STiwei Bie BUG_ON(data == NULL); 11001ce9e605STiwei Bie BUG_ON(ctx && vq->indirect); 11011ce9e605STiwei Bie 11021ce9e605STiwei Bie if (unlikely(vq->broken)) { 11031ce9e605STiwei Bie END_USE(vq); 11041ce9e605STiwei Bie return -EIO; 11051ce9e605STiwei Bie } 11061ce9e605STiwei Bie 11071ce9e605STiwei Bie LAST_ADD_TIME_UPDATE(vq); 11081ce9e605STiwei Bie 11091ce9e605STiwei Bie BUG_ON(total_sg == 0); 11101ce9e605STiwei Bie 11111ce9e605STiwei Bie if (virtqueue_use_indirect(_vq, total_sg)) 11121ce9e605STiwei Bie return virtqueue_add_indirect_packed(vq, sgs, total_sg, 11131ce9e605STiwei Bie out_sgs, in_sgs, data, gfp); 11141ce9e605STiwei Bie 11151ce9e605STiwei Bie head = vq->packed.next_avail_idx; 11161ce9e605STiwei Bie avail_used_flags = vq->packed.avail_used_flags; 11171ce9e605STiwei Bie 11181ce9e605STiwei Bie WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect); 11191ce9e605STiwei Bie 11201ce9e605STiwei Bie desc = vq->packed.vring.desc; 11211ce9e605STiwei Bie i = head; 11221ce9e605STiwei Bie descs_used = total_sg; 11231ce9e605STiwei Bie 11241ce9e605STiwei Bie if (unlikely(vq->vq.num_free < descs_used)) { 11251ce9e605STiwei Bie pr_debug("Can't add buf len %i - avail = %i\n", 11261ce9e605STiwei Bie descs_used, vq->vq.num_free); 11271ce9e605STiwei Bie END_USE(vq); 11281ce9e605STiwei Bie return -ENOSPC; 11291ce9e605STiwei Bie } 11301ce9e605STiwei Bie 11311ce9e605STiwei Bie id = vq->free_head; 11321ce9e605STiwei Bie BUG_ON(id == vq->packed.vring.num); 11331ce9e605STiwei Bie 11341ce9e605STiwei Bie curr = id; 11351ce9e605STiwei Bie c = 0; 11361ce9e605STiwei Bie for (n = 0; n < out_sgs + in_sgs; n++) { 11371ce9e605STiwei Bie for (sg = sgs[n]; sg; sg = sg_next(sg)) { 11381ce9e605STiwei Bie dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ? 11391ce9e605STiwei Bie DMA_TO_DEVICE : DMA_FROM_DEVICE); 11401ce9e605STiwei Bie if (vring_mapping_error(vq, addr)) 11411ce9e605STiwei Bie goto unmap_release; 11421ce9e605STiwei Bie 11431ce9e605STiwei Bie flags = cpu_to_le16(vq->packed.avail_used_flags | 11441ce9e605STiwei Bie (++c == total_sg ? 0 : VRING_DESC_F_NEXT) | 11451ce9e605STiwei Bie (n < out_sgs ? 0 : VRING_DESC_F_WRITE)); 11461ce9e605STiwei Bie if (i == head) 11471ce9e605STiwei Bie head_flags = flags; 11481ce9e605STiwei Bie else 11491ce9e605STiwei Bie desc[i].flags = flags; 11501ce9e605STiwei Bie 11511ce9e605STiwei Bie desc[i].addr = cpu_to_le64(addr); 11521ce9e605STiwei Bie desc[i].len = cpu_to_le32(sg->length); 11531ce9e605STiwei Bie desc[i].id = cpu_to_le16(id); 11541ce9e605STiwei Bie 11551ce9e605STiwei Bie if (unlikely(vq->use_dma_api)) { 11561ce9e605STiwei Bie vq->packed.desc_extra[curr].addr = addr; 11571ce9e605STiwei Bie vq->packed.desc_extra[curr].len = sg->length; 11581ce9e605STiwei Bie vq->packed.desc_extra[curr].flags = 11591ce9e605STiwei Bie le16_to_cpu(flags); 11601ce9e605STiwei Bie } 11611ce9e605STiwei Bie prev = curr; 11621ce9e605STiwei Bie curr = vq->packed.desc_state[curr].next; 11631ce9e605STiwei Bie 11641ce9e605STiwei Bie if ((unlikely(++i >= vq->packed.vring.num))) { 11651ce9e605STiwei Bie i = 0; 11661ce9e605STiwei Bie vq->packed.avail_used_flags ^= 11671ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_AVAIL | 11681ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_USED; 11691ce9e605STiwei Bie } 11701ce9e605STiwei Bie } 11711ce9e605STiwei Bie } 11721ce9e605STiwei Bie 11731ce9e605STiwei Bie if (i < head) 11741ce9e605STiwei Bie vq->packed.avail_wrap_counter ^= 1; 11751ce9e605STiwei Bie 11761ce9e605STiwei Bie /* We're using some buffers from the free list. */ 11771ce9e605STiwei Bie vq->vq.num_free -= descs_used; 11781ce9e605STiwei Bie 11791ce9e605STiwei Bie /* Update free pointer */ 11801ce9e605STiwei Bie vq->packed.next_avail_idx = i; 11811ce9e605STiwei Bie vq->free_head = curr; 11821ce9e605STiwei Bie 11831ce9e605STiwei Bie /* Store token. */ 11841ce9e605STiwei Bie vq->packed.desc_state[id].num = descs_used; 11851ce9e605STiwei Bie vq->packed.desc_state[id].data = data; 11861ce9e605STiwei Bie vq->packed.desc_state[id].indir_desc = ctx; 11871ce9e605STiwei Bie vq->packed.desc_state[id].last = prev; 11881ce9e605STiwei Bie 11891ce9e605STiwei Bie /* 11901ce9e605STiwei Bie * A driver MUST NOT make the first descriptor in the list 11911ce9e605STiwei Bie * available before all subsequent descriptors comprising 11921ce9e605STiwei Bie * the list are made available. 11931ce9e605STiwei Bie */ 11941ce9e605STiwei Bie virtio_wmb(vq->weak_barriers); 11951ce9e605STiwei Bie vq->packed.vring.desc[head].flags = head_flags; 11961ce9e605STiwei Bie vq->num_added += descs_used; 11971ce9e605STiwei Bie 11981ce9e605STiwei Bie pr_debug("Added buffer head %i to %p\n", head, vq); 11991ce9e605STiwei Bie END_USE(vq); 12001ce9e605STiwei Bie 12011ce9e605STiwei Bie return 0; 12021ce9e605STiwei Bie 12031ce9e605STiwei Bie unmap_release: 12041ce9e605STiwei Bie err_idx = i; 12051ce9e605STiwei Bie i = head; 12061ce9e605STiwei Bie 12071ce9e605STiwei Bie vq->packed.avail_used_flags = avail_used_flags; 12081ce9e605STiwei Bie 12091ce9e605STiwei Bie for (n = 0; n < total_sg; n++) { 12101ce9e605STiwei Bie if (i == err_idx) 12111ce9e605STiwei Bie break; 12121ce9e605STiwei Bie vring_unmap_desc_packed(vq, &desc[i]); 12131ce9e605STiwei Bie i++; 12141ce9e605STiwei Bie if (i >= vq->packed.vring.num) 12151ce9e605STiwei Bie i = 0; 12161ce9e605STiwei Bie } 12171ce9e605STiwei Bie 12181ce9e605STiwei Bie END_USE(vq); 12191ce9e605STiwei Bie return -EIO; 12201ce9e605STiwei Bie } 12211ce9e605STiwei Bie 12221ce9e605STiwei Bie static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq) 12231ce9e605STiwei Bie { 12241ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 1225f51f9826STiwei Bie u16 new, old, off_wrap, flags, wrap_counter, event_idx; 12261ce9e605STiwei Bie bool needs_kick; 12271ce9e605STiwei Bie union { 12281ce9e605STiwei Bie struct { 12291ce9e605STiwei Bie __le16 off_wrap; 12301ce9e605STiwei Bie __le16 flags; 12311ce9e605STiwei Bie }; 12321ce9e605STiwei Bie u32 u32; 12331ce9e605STiwei Bie } snapshot; 12341ce9e605STiwei Bie 12351ce9e605STiwei Bie START_USE(vq); 12361ce9e605STiwei Bie 12371ce9e605STiwei Bie /* 12381ce9e605STiwei Bie * We need to expose the new flags value before checking notification 12391ce9e605STiwei Bie * suppressions. 12401ce9e605STiwei Bie */ 12411ce9e605STiwei Bie virtio_mb(vq->weak_barriers); 12421ce9e605STiwei Bie 1243f51f9826STiwei Bie old = vq->packed.next_avail_idx - vq->num_added; 1244f51f9826STiwei Bie new = vq->packed.next_avail_idx; 12451ce9e605STiwei Bie vq->num_added = 0; 12461ce9e605STiwei Bie 12471ce9e605STiwei Bie snapshot.u32 = *(u32 *)vq->packed.vring.device; 12481ce9e605STiwei Bie flags = le16_to_cpu(snapshot.flags); 12491ce9e605STiwei Bie 12501ce9e605STiwei Bie LAST_ADD_TIME_CHECK(vq); 12511ce9e605STiwei Bie LAST_ADD_TIME_INVALID(vq); 12521ce9e605STiwei Bie 1253f51f9826STiwei Bie if (flags != VRING_PACKED_EVENT_FLAG_DESC) { 12541ce9e605STiwei Bie needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE); 1255f51f9826STiwei Bie goto out; 1256f51f9826STiwei Bie } 1257f51f9826STiwei Bie 1258f51f9826STiwei Bie off_wrap = le16_to_cpu(snapshot.off_wrap); 1259f51f9826STiwei Bie 1260f51f9826STiwei Bie wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; 1261f51f9826STiwei Bie event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); 1262f51f9826STiwei Bie if (wrap_counter != vq->packed.avail_wrap_counter) 1263f51f9826STiwei Bie event_idx -= vq->packed.vring.num; 1264f51f9826STiwei Bie 1265f51f9826STiwei Bie needs_kick = vring_need_event(event_idx, new, old); 1266f51f9826STiwei Bie out: 12671ce9e605STiwei Bie END_USE(vq); 12681ce9e605STiwei Bie return needs_kick; 12691ce9e605STiwei Bie } 12701ce9e605STiwei Bie 12711ce9e605STiwei Bie static void detach_buf_packed(struct vring_virtqueue *vq, 12721ce9e605STiwei Bie unsigned int id, void **ctx) 12731ce9e605STiwei Bie { 12741ce9e605STiwei Bie struct vring_desc_state_packed *state = NULL; 12751ce9e605STiwei Bie struct vring_packed_desc *desc; 12761ce9e605STiwei Bie unsigned int i, curr; 12771ce9e605STiwei Bie 12781ce9e605STiwei Bie state = &vq->packed.desc_state[id]; 12791ce9e605STiwei Bie 12801ce9e605STiwei Bie /* Clear data ptr. */ 12811ce9e605STiwei Bie state->data = NULL; 12821ce9e605STiwei Bie 12831ce9e605STiwei Bie vq->packed.desc_state[state->last].next = vq->free_head; 12841ce9e605STiwei Bie vq->free_head = id; 12851ce9e605STiwei Bie vq->vq.num_free += state->num; 12861ce9e605STiwei Bie 12871ce9e605STiwei Bie if (unlikely(vq->use_dma_api)) { 12881ce9e605STiwei Bie curr = id; 12891ce9e605STiwei Bie for (i = 0; i < state->num; i++) { 12901ce9e605STiwei Bie vring_unmap_state_packed(vq, 12911ce9e605STiwei Bie &vq->packed.desc_extra[curr]); 12921ce9e605STiwei Bie curr = vq->packed.desc_state[curr].next; 12931ce9e605STiwei Bie } 12941ce9e605STiwei Bie } 12951ce9e605STiwei Bie 12961ce9e605STiwei Bie if (vq->indirect) { 12971ce9e605STiwei Bie u32 len; 12981ce9e605STiwei Bie 12991ce9e605STiwei Bie /* Free the indirect table, if any, now that it's unmapped. */ 13001ce9e605STiwei Bie desc = state->indir_desc; 13011ce9e605STiwei Bie if (!desc) 13021ce9e605STiwei Bie return; 13031ce9e605STiwei Bie 13041ce9e605STiwei Bie if (vq->use_dma_api) { 13051ce9e605STiwei Bie len = vq->packed.desc_extra[id].len; 13061ce9e605STiwei Bie for (i = 0; i < len / sizeof(struct vring_packed_desc); 13071ce9e605STiwei Bie i++) 13081ce9e605STiwei Bie vring_unmap_desc_packed(vq, &desc[i]); 13091ce9e605STiwei Bie } 13101ce9e605STiwei Bie kfree(desc); 13111ce9e605STiwei Bie state->indir_desc = NULL; 13121ce9e605STiwei Bie } else if (ctx) { 13131ce9e605STiwei Bie *ctx = state->indir_desc; 13141ce9e605STiwei Bie } 13151ce9e605STiwei Bie } 13161ce9e605STiwei Bie 13171ce9e605STiwei Bie static inline bool is_used_desc_packed(const struct vring_virtqueue *vq, 13181ce9e605STiwei Bie u16 idx, bool used_wrap_counter) 13191ce9e605STiwei Bie { 13201ce9e605STiwei Bie bool avail, used; 13211ce9e605STiwei Bie u16 flags; 13221ce9e605STiwei Bie 13231ce9e605STiwei Bie flags = le16_to_cpu(vq->packed.vring.desc[idx].flags); 13241ce9e605STiwei Bie avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL)); 13251ce9e605STiwei Bie used = !!(flags & (1 << VRING_PACKED_DESC_F_USED)); 13261ce9e605STiwei Bie 13271ce9e605STiwei Bie return avail == used && used == used_wrap_counter; 13281ce9e605STiwei Bie } 13291ce9e605STiwei Bie 13301ce9e605STiwei Bie static inline bool more_used_packed(const struct vring_virtqueue *vq) 13311ce9e605STiwei Bie { 13321ce9e605STiwei Bie return is_used_desc_packed(vq, vq->last_used_idx, 13331ce9e605STiwei Bie vq->packed.used_wrap_counter); 13341ce9e605STiwei Bie } 13351ce9e605STiwei Bie 13361ce9e605STiwei Bie static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq, 13371ce9e605STiwei Bie unsigned int *len, 13381ce9e605STiwei Bie void **ctx) 13391ce9e605STiwei Bie { 13401ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 13411ce9e605STiwei Bie u16 last_used, id; 13421ce9e605STiwei Bie void *ret; 13431ce9e605STiwei Bie 13441ce9e605STiwei Bie START_USE(vq); 13451ce9e605STiwei Bie 13461ce9e605STiwei Bie if (unlikely(vq->broken)) { 13471ce9e605STiwei Bie END_USE(vq); 13481ce9e605STiwei Bie return NULL; 13491ce9e605STiwei Bie } 13501ce9e605STiwei Bie 13511ce9e605STiwei Bie if (!more_used_packed(vq)) { 13521ce9e605STiwei Bie pr_debug("No more buffers in queue\n"); 13531ce9e605STiwei Bie END_USE(vq); 13541ce9e605STiwei Bie return NULL; 13551ce9e605STiwei Bie } 13561ce9e605STiwei Bie 13571ce9e605STiwei Bie /* Only get used elements after they have been exposed by host. */ 13581ce9e605STiwei Bie virtio_rmb(vq->weak_barriers); 13591ce9e605STiwei Bie 13601ce9e605STiwei Bie last_used = vq->last_used_idx; 13611ce9e605STiwei Bie id = le16_to_cpu(vq->packed.vring.desc[last_used].id); 13621ce9e605STiwei Bie *len = le32_to_cpu(vq->packed.vring.desc[last_used].len); 13631ce9e605STiwei Bie 13641ce9e605STiwei Bie if (unlikely(id >= vq->packed.vring.num)) { 13651ce9e605STiwei Bie BAD_RING(vq, "id %u out of range\n", id); 13661ce9e605STiwei Bie return NULL; 13671ce9e605STiwei Bie } 13681ce9e605STiwei Bie if (unlikely(!vq->packed.desc_state[id].data)) { 13691ce9e605STiwei Bie BAD_RING(vq, "id %u is not a head!\n", id); 13701ce9e605STiwei Bie return NULL; 13711ce9e605STiwei Bie } 13721ce9e605STiwei Bie 13731ce9e605STiwei Bie /* detach_buf_packed clears data, so grab it now. */ 13741ce9e605STiwei Bie ret = vq->packed.desc_state[id].data; 13751ce9e605STiwei Bie detach_buf_packed(vq, id, ctx); 13761ce9e605STiwei Bie 13771ce9e605STiwei Bie vq->last_used_idx += vq->packed.desc_state[id].num; 13781ce9e605STiwei Bie if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) { 13791ce9e605STiwei Bie vq->last_used_idx -= vq->packed.vring.num; 13801ce9e605STiwei Bie vq->packed.used_wrap_counter ^= 1; 13811ce9e605STiwei Bie } 13821ce9e605STiwei Bie 1383f51f9826STiwei Bie /* 1384f51f9826STiwei Bie * If we expect an interrupt for the next entry, tell host 1385f51f9826STiwei Bie * by writing event index and flush out the write before 1386f51f9826STiwei Bie * the read in the next get_buf call. 1387f51f9826STiwei Bie */ 1388f51f9826STiwei Bie if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC) 1389f51f9826STiwei Bie virtio_store_mb(vq->weak_barriers, 1390f51f9826STiwei Bie &vq->packed.vring.driver->off_wrap, 1391f51f9826STiwei Bie cpu_to_le16(vq->last_used_idx | 1392f51f9826STiwei Bie (vq->packed.used_wrap_counter << 1393f51f9826STiwei Bie VRING_PACKED_EVENT_F_WRAP_CTR))); 1394f51f9826STiwei Bie 13951ce9e605STiwei Bie LAST_ADD_TIME_INVALID(vq); 13961ce9e605STiwei Bie 13971ce9e605STiwei Bie END_USE(vq); 13981ce9e605STiwei Bie return ret; 13991ce9e605STiwei Bie } 14001ce9e605STiwei Bie 14011ce9e605STiwei Bie static void virtqueue_disable_cb_packed(struct virtqueue *_vq) 14021ce9e605STiwei Bie { 14031ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 14041ce9e605STiwei Bie 14051ce9e605STiwei Bie if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) { 14061ce9e605STiwei Bie vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; 14071ce9e605STiwei Bie vq->packed.vring.driver->flags = 14081ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 14091ce9e605STiwei Bie } 14101ce9e605STiwei Bie } 14111ce9e605STiwei Bie 14121ce9e605STiwei Bie static unsigned virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq) 14131ce9e605STiwei Bie { 14141ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 14151ce9e605STiwei Bie 14161ce9e605STiwei Bie START_USE(vq); 14171ce9e605STiwei Bie 14181ce9e605STiwei Bie /* 14191ce9e605STiwei Bie * We optimistically turn back on interrupts, then check if there was 14201ce9e605STiwei Bie * more to do. 14211ce9e605STiwei Bie */ 14221ce9e605STiwei Bie 1423f51f9826STiwei Bie if (vq->event) { 1424f51f9826STiwei Bie vq->packed.vring.driver->off_wrap = 1425f51f9826STiwei Bie cpu_to_le16(vq->last_used_idx | 1426f51f9826STiwei Bie (vq->packed.used_wrap_counter << 1427f51f9826STiwei Bie VRING_PACKED_EVENT_F_WRAP_CTR)); 1428f51f9826STiwei Bie /* 1429f51f9826STiwei Bie * We need to update event offset and event wrap 1430f51f9826STiwei Bie * counter first before updating event flags. 1431f51f9826STiwei Bie */ 1432f51f9826STiwei Bie virtio_wmb(vq->weak_barriers); 1433f51f9826STiwei Bie } 1434f51f9826STiwei Bie 14351ce9e605STiwei Bie if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { 1436f51f9826STiwei Bie vq->packed.event_flags_shadow = vq->event ? 1437f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_DESC : 1438f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_ENABLE; 14391ce9e605STiwei Bie vq->packed.vring.driver->flags = 14401ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 14411ce9e605STiwei Bie } 14421ce9e605STiwei Bie 14431ce9e605STiwei Bie END_USE(vq); 14441ce9e605STiwei Bie return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter << 14451ce9e605STiwei Bie VRING_PACKED_EVENT_F_WRAP_CTR); 14461ce9e605STiwei Bie } 14471ce9e605STiwei Bie 14481ce9e605STiwei Bie static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap) 14491ce9e605STiwei Bie { 14501ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 14511ce9e605STiwei Bie bool wrap_counter; 14521ce9e605STiwei Bie u16 used_idx; 14531ce9e605STiwei Bie 14541ce9e605STiwei Bie wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; 14551ce9e605STiwei Bie used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); 14561ce9e605STiwei Bie 14571ce9e605STiwei Bie return is_used_desc_packed(vq, used_idx, wrap_counter); 14581ce9e605STiwei Bie } 14591ce9e605STiwei Bie 14601ce9e605STiwei Bie static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq) 14611ce9e605STiwei Bie { 14621ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 14631ce9e605STiwei Bie u16 used_idx, wrap_counter; 1464f51f9826STiwei Bie u16 bufs; 14651ce9e605STiwei Bie 14661ce9e605STiwei Bie START_USE(vq); 14671ce9e605STiwei Bie 14681ce9e605STiwei Bie /* 14691ce9e605STiwei Bie * We optimistically turn back on interrupts, then check if there was 14701ce9e605STiwei Bie * more to do. 14711ce9e605STiwei Bie */ 14721ce9e605STiwei Bie 1473f51f9826STiwei Bie if (vq->event) { 1474f51f9826STiwei Bie /* TODO: tune this threshold */ 1475f51f9826STiwei Bie bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4; 14761ce9e605STiwei Bie wrap_counter = vq->packed.used_wrap_counter; 14771ce9e605STiwei Bie 1478f51f9826STiwei Bie used_idx = vq->last_used_idx + bufs; 1479f51f9826STiwei Bie if (used_idx >= vq->packed.vring.num) { 1480f51f9826STiwei Bie used_idx -= vq->packed.vring.num; 1481f51f9826STiwei Bie wrap_counter ^= 1; 1482f51f9826STiwei Bie } 1483f51f9826STiwei Bie 1484f51f9826STiwei Bie vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx | 1485f51f9826STiwei Bie (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR)); 1486f51f9826STiwei Bie 1487f51f9826STiwei Bie /* 1488f51f9826STiwei Bie * We need to update event offset and event wrap 1489f51f9826STiwei Bie * counter first before updating event flags. 1490f51f9826STiwei Bie */ 1491f51f9826STiwei Bie virtio_wmb(vq->weak_barriers); 1492f51f9826STiwei Bie } else { 1493f51f9826STiwei Bie used_idx = vq->last_used_idx; 1494f51f9826STiwei Bie wrap_counter = vq->packed.used_wrap_counter; 1495f51f9826STiwei Bie } 1496f51f9826STiwei Bie 14971ce9e605STiwei Bie if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { 1498f51f9826STiwei Bie vq->packed.event_flags_shadow = vq->event ? 1499f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_DESC : 1500f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_ENABLE; 15011ce9e605STiwei Bie vq->packed.vring.driver->flags = 15021ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 15031ce9e605STiwei Bie } 15041ce9e605STiwei Bie 15051ce9e605STiwei Bie /* 15061ce9e605STiwei Bie * We need to update event suppression structure first 15071ce9e605STiwei Bie * before re-checking for more used buffers. 15081ce9e605STiwei Bie */ 15091ce9e605STiwei Bie virtio_mb(vq->weak_barriers); 15101ce9e605STiwei Bie 15111ce9e605STiwei Bie if (is_used_desc_packed(vq, used_idx, wrap_counter)) { 15121ce9e605STiwei Bie END_USE(vq); 15131ce9e605STiwei Bie return false; 15141ce9e605STiwei Bie } 15151ce9e605STiwei Bie 15161ce9e605STiwei Bie END_USE(vq); 15171ce9e605STiwei Bie return true; 15181ce9e605STiwei Bie } 15191ce9e605STiwei Bie 15201ce9e605STiwei Bie static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq) 15211ce9e605STiwei Bie { 15221ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 15231ce9e605STiwei Bie unsigned int i; 15241ce9e605STiwei Bie void *buf; 15251ce9e605STiwei Bie 15261ce9e605STiwei Bie START_USE(vq); 15271ce9e605STiwei Bie 15281ce9e605STiwei Bie for (i = 0; i < vq->packed.vring.num; i++) { 15291ce9e605STiwei Bie if (!vq->packed.desc_state[i].data) 15301ce9e605STiwei Bie continue; 15311ce9e605STiwei Bie /* detach_buf clears data, so grab it now. */ 15321ce9e605STiwei Bie buf = vq->packed.desc_state[i].data; 15331ce9e605STiwei Bie detach_buf_packed(vq, i, NULL); 15341ce9e605STiwei Bie END_USE(vq); 15351ce9e605STiwei Bie return buf; 15361ce9e605STiwei Bie } 15371ce9e605STiwei Bie /* That should have freed everything. */ 15381ce9e605STiwei Bie BUG_ON(vq->vq.num_free != vq->packed.vring.num); 15391ce9e605STiwei Bie 15401ce9e605STiwei Bie END_USE(vq); 15411ce9e605STiwei Bie return NULL; 15421ce9e605STiwei Bie } 15431ce9e605STiwei Bie 15441ce9e605STiwei Bie static struct virtqueue *vring_create_virtqueue_packed( 15451ce9e605STiwei Bie unsigned int index, 15461ce9e605STiwei Bie unsigned int num, 15471ce9e605STiwei Bie unsigned int vring_align, 15481ce9e605STiwei Bie struct virtio_device *vdev, 15491ce9e605STiwei Bie bool weak_barriers, 15501ce9e605STiwei Bie bool may_reduce_num, 15511ce9e605STiwei Bie bool context, 15521ce9e605STiwei Bie bool (*notify)(struct virtqueue *), 15531ce9e605STiwei Bie void (*callback)(struct virtqueue *), 15541ce9e605STiwei Bie const char *name) 15551ce9e605STiwei Bie { 15561ce9e605STiwei Bie struct vring_virtqueue *vq; 15571ce9e605STiwei Bie struct vring_packed_desc *ring; 15581ce9e605STiwei Bie struct vring_packed_desc_event *driver, *device; 15591ce9e605STiwei Bie dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr; 15601ce9e605STiwei Bie size_t ring_size_in_bytes, event_size_in_bytes; 15611ce9e605STiwei Bie unsigned int i; 15621ce9e605STiwei Bie 15631ce9e605STiwei Bie ring_size_in_bytes = num * sizeof(struct vring_packed_desc); 15641ce9e605STiwei Bie 15651ce9e605STiwei Bie ring = vring_alloc_queue(vdev, ring_size_in_bytes, 15661ce9e605STiwei Bie &ring_dma_addr, 15671ce9e605STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 15681ce9e605STiwei Bie if (!ring) 15691ce9e605STiwei Bie goto err_ring; 15701ce9e605STiwei Bie 15711ce9e605STiwei Bie event_size_in_bytes = sizeof(struct vring_packed_desc_event); 15721ce9e605STiwei Bie 15731ce9e605STiwei Bie driver = vring_alloc_queue(vdev, event_size_in_bytes, 15741ce9e605STiwei Bie &driver_event_dma_addr, 15751ce9e605STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 15761ce9e605STiwei Bie if (!driver) 15771ce9e605STiwei Bie goto err_driver; 15781ce9e605STiwei Bie 15791ce9e605STiwei Bie device = vring_alloc_queue(vdev, event_size_in_bytes, 15801ce9e605STiwei Bie &device_event_dma_addr, 15811ce9e605STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 15821ce9e605STiwei Bie if (!device) 15831ce9e605STiwei Bie goto err_device; 15841ce9e605STiwei Bie 15851ce9e605STiwei Bie vq = kmalloc(sizeof(*vq), GFP_KERNEL); 15861ce9e605STiwei Bie if (!vq) 15871ce9e605STiwei Bie goto err_vq; 15881ce9e605STiwei Bie 15891ce9e605STiwei Bie vq->vq.callback = callback; 15901ce9e605STiwei Bie vq->vq.vdev = vdev; 15911ce9e605STiwei Bie vq->vq.name = name; 15921ce9e605STiwei Bie vq->vq.num_free = num; 15931ce9e605STiwei Bie vq->vq.index = index; 15941ce9e605STiwei Bie vq->we_own_ring = true; 15951ce9e605STiwei Bie vq->notify = notify; 15961ce9e605STiwei Bie vq->weak_barriers = weak_barriers; 15971ce9e605STiwei Bie vq->broken = false; 15981ce9e605STiwei Bie vq->last_used_idx = 0; 15991ce9e605STiwei Bie vq->num_added = 0; 16001ce9e605STiwei Bie vq->packed_ring = true; 16011ce9e605STiwei Bie vq->use_dma_api = vring_use_dma_api(vdev); 16021ce9e605STiwei Bie list_add_tail(&vq->vq.list, &vdev->vqs); 16031ce9e605STiwei Bie #ifdef DEBUG 16041ce9e605STiwei Bie vq->in_use = false; 16051ce9e605STiwei Bie vq->last_add_time_valid = false; 16061ce9e605STiwei Bie #endif 16071ce9e605STiwei Bie 16081ce9e605STiwei Bie vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && 16091ce9e605STiwei Bie !context; 16101ce9e605STiwei Bie vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 16111ce9e605STiwei Bie 1612*45383fb0STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) 1613*45383fb0STiwei Bie vq->weak_barriers = false; 1614*45383fb0STiwei Bie 16151ce9e605STiwei Bie vq->packed.ring_dma_addr = ring_dma_addr; 16161ce9e605STiwei Bie vq->packed.driver_event_dma_addr = driver_event_dma_addr; 16171ce9e605STiwei Bie vq->packed.device_event_dma_addr = device_event_dma_addr; 16181ce9e605STiwei Bie 16191ce9e605STiwei Bie vq->packed.ring_size_in_bytes = ring_size_in_bytes; 16201ce9e605STiwei Bie vq->packed.event_size_in_bytes = event_size_in_bytes; 16211ce9e605STiwei Bie 16221ce9e605STiwei Bie vq->packed.vring.num = num; 16231ce9e605STiwei Bie vq->packed.vring.desc = ring; 16241ce9e605STiwei Bie vq->packed.vring.driver = driver; 16251ce9e605STiwei Bie vq->packed.vring.device = device; 16261ce9e605STiwei Bie 16271ce9e605STiwei Bie vq->packed.next_avail_idx = 0; 16281ce9e605STiwei Bie vq->packed.avail_wrap_counter = 1; 16291ce9e605STiwei Bie vq->packed.used_wrap_counter = 1; 16301ce9e605STiwei Bie vq->packed.event_flags_shadow = 0; 16311ce9e605STiwei Bie vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL; 16321ce9e605STiwei Bie 16331ce9e605STiwei Bie vq->packed.desc_state = kmalloc_array(num, 16341ce9e605STiwei Bie sizeof(struct vring_desc_state_packed), 16351ce9e605STiwei Bie GFP_KERNEL); 16361ce9e605STiwei Bie if (!vq->packed.desc_state) 16371ce9e605STiwei Bie goto err_desc_state; 16381ce9e605STiwei Bie 16391ce9e605STiwei Bie memset(vq->packed.desc_state, 0, 16401ce9e605STiwei Bie num * sizeof(struct vring_desc_state_packed)); 16411ce9e605STiwei Bie 16421ce9e605STiwei Bie /* Put everything in free lists. */ 16431ce9e605STiwei Bie vq->free_head = 0; 16441ce9e605STiwei Bie for (i = 0; i < num-1; i++) 16451ce9e605STiwei Bie vq->packed.desc_state[i].next = i + 1; 16461ce9e605STiwei Bie 16471ce9e605STiwei Bie vq->packed.desc_extra = kmalloc_array(num, 16481ce9e605STiwei Bie sizeof(struct vring_desc_extra_packed), 16491ce9e605STiwei Bie GFP_KERNEL); 16501ce9e605STiwei Bie if (!vq->packed.desc_extra) 16511ce9e605STiwei Bie goto err_desc_extra; 16521ce9e605STiwei Bie 16531ce9e605STiwei Bie memset(vq->packed.desc_extra, 0, 16541ce9e605STiwei Bie num * sizeof(struct vring_desc_extra_packed)); 16551ce9e605STiwei Bie 16561ce9e605STiwei Bie /* No callback? Tell other side not to bother us. */ 16571ce9e605STiwei Bie if (!callback) { 16581ce9e605STiwei Bie vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; 16591ce9e605STiwei Bie vq->packed.vring.driver->flags = 16601ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 16611ce9e605STiwei Bie } 16621ce9e605STiwei Bie 16631ce9e605STiwei Bie return &vq->vq; 16641ce9e605STiwei Bie 16651ce9e605STiwei Bie err_desc_extra: 16661ce9e605STiwei Bie kfree(vq->packed.desc_state); 16671ce9e605STiwei Bie err_desc_state: 16681ce9e605STiwei Bie kfree(vq); 16691ce9e605STiwei Bie err_vq: 16701ce9e605STiwei Bie vring_free_queue(vdev, event_size_in_bytes, device, ring_dma_addr); 16711ce9e605STiwei Bie err_device: 16721ce9e605STiwei Bie vring_free_queue(vdev, event_size_in_bytes, driver, ring_dma_addr); 16731ce9e605STiwei Bie err_driver: 16741ce9e605STiwei Bie vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr); 16751ce9e605STiwei Bie err_ring: 16761ce9e605STiwei Bie return NULL; 16771ce9e605STiwei Bie } 16781ce9e605STiwei Bie 16791ce9e605STiwei Bie 16801ce9e605STiwei Bie /* 1681e6f633e5STiwei Bie * Generic functions and exported symbols. 1682e6f633e5STiwei Bie */ 1683e6f633e5STiwei Bie 1684e6f633e5STiwei Bie static inline int virtqueue_add(struct virtqueue *_vq, 1685e6f633e5STiwei Bie struct scatterlist *sgs[], 1686e6f633e5STiwei Bie unsigned int total_sg, 1687e6f633e5STiwei Bie unsigned int out_sgs, 1688e6f633e5STiwei Bie unsigned int in_sgs, 1689e6f633e5STiwei Bie void *data, 1690e6f633e5STiwei Bie void *ctx, 1691e6f633e5STiwei Bie gfp_t gfp) 1692e6f633e5STiwei Bie { 16931ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 16941ce9e605STiwei Bie 16951ce9e605STiwei Bie return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg, 16961ce9e605STiwei Bie out_sgs, in_sgs, data, ctx, gfp) : 16971ce9e605STiwei Bie virtqueue_add_split(_vq, sgs, total_sg, 1698e6f633e5STiwei Bie out_sgs, in_sgs, data, ctx, gfp); 1699e6f633e5STiwei Bie } 1700e6f633e5STiwei Bie 1701e6f633e5STiwei Bie /** 1702e6f633e5STiwei Bie * virtqueue_add_sgs - expose buffers to other end 1703e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1704e6f633e5STiwei Bie * @sgs: array of terminated scatterlists. 1705e6f633e5STiwei Bie * @out_num: the number of scatterlists readable by other side 1706e6f633e5STiwei Bie * @in_num: the number of scatterlists which are writable (after readable ones) 1707e6f633e5STiwei Bie * @data: the token identifying the buffer. 1708e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 1709e6f633e5STiwei Bie * 1710e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 1711e6f633e5STiwei Bie * at the same time (except where noted). 1712e6f633e5STiwei Bie * 1713e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 1714e6f633e5STiwei Bie */ 1715e6f633e5STiwei Bie int virtqueue_add_sgs(struct virtqueue *_vq, 1716e6f633e5STiwei Bie struct scatterlist *sgs[], 1717e6f633e5STiwei Bie unsigned int out_sgs, 1718e6f633e5STiwei Bie unsigned int in_sgs, 1719e6f633e5STiwei Bie void *data, 1720e6f633e5STiwei Bie gfp_t gfp) 1721e6f633e5STiwei Bie { 1722e6f633e5STiwei Bie unsigned int i, total_sg = 0; 1723e6f633e5STiwei Bie 1724e6f633e5STiwei Bie /* Count them first. */ 1725e6f633e5STiwei Bie for (i = 0; i < out_sgs + in_sgs; i++) { 1726e6f633e5STiwei Bie struct scatterlist *sg; 1727e6f633e5STiwei Bie 1728e6f633e5STiwei Bie for (sg = sgs[i]; sg; sg = sg_next(sg)) 1729e6f633e5STiwei Bie total_sg++; 1730e6f633e5STiwei Bie } 1731e6f633e5STiwei Bie return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, 1732e6f633e5STiwei Bie data, NULL, gfp); 1733e6f633e5STiwei Bie } 1734e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_sgs); 1735e6f633e5STiwei Bie 1736e6f633e5STiwei Bie /** 1737e6f633e5STiwei Bie * virtqueue_add_outbuf - expose output buffers to other end 1738e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1739e6f633e5STiwei Bie * @sg: scatterlist (must be well-formed and terminated!) 1740e6f633e5STiwei Bie * @num: the number of entries in @sg readable by other side 1741e6f633e5STiwei Bie * @data: the token identifying the buffer. 1742e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 1743e6f633e5STiwei Bie * 1744e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 1745e6f633e5STiwei Bie * at the same time (except where noted). 1746e6f633e5STiwei Bie * 1747e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 1748e6f633e5STiwei Bie */ 1749e6f633e5STiwei Bie int virtqueue_add_outbuf(struct virtqueue *vq, 1750e6f633e5STiwei Bie struct scatterlist *sg, unsigned int num, 1751e6f633e5STiwei Bie void *data, 1752e6f633e5STiwei Bie gfp_t gfp) 1753e6f633e5STiwei Bie { 1754e6f633e5STiwei Bie return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp); 1755e6f633e5STiwei Bie } 1756e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); 1757e6f633e5STiwei Bie 1758e6f633e5STiwei Bie /** 1759e6f633e5STiwei Bie * virtqueue_add_inbuf - expose input buffers to other end 1760e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1761e6f633e5STiwei Bie * @sg: scatterlist (must be well-formed and terminated!) 1762e6f633e5STiwei Bie * @num: the number of entries in @sg writable by other side 1763e6f633e5STiwei Bie * @data: the token identifying the buffer. 1764e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 1765e6f633e5STiwei Bie * 1766e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 1767e6f633e5STiwei Bie * at the same time (except where noted). 1768e6f633e5STiwei Bie * 1769e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 1770e6f633e5STiwei Bie */ 1771e6f633e5STiwei Bie int virtqueue_add_inbuf(struct virtqueue *vq, 1772e6f633e5STiwei Bie struct scatterlist *sg, unsigned int num, 1773e6f633e5STiwei Bie void *data, 1774e6f633e5STiwei Bie gfp_t gfp) 1775e6f633e5STiwei Bie { 1776e6f633e5STiwei Bie return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp); 1777e6f633e5STiwei Bie } 1778e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); 1779e6f633e5STiwei Bie 1780e6f633e5STiwei Bie /** 1781e6f633e5STiwei Bie * virtqueue_add_inbuf_ctx - expose input buffers to other end 1782e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1783e6f633e5STiwei Bie * @sg: scatterlist (must be well-formed and terminated!) 1784e6f633e5STiwei Bie * @num: the number of entries in @sg writable by other side 1785e6f633e5STiwei Bie * @data: the token identifying the buffer. 1786e6f633e5STiwei Bie * @ctx: extra context for the token 1787e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 1788e6f633e5STiwei Bie * 1789e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 1790e6f633e5STiwei Bie * at the same time (except where noted). 1791e6f633e5STiwei Bie * 1792e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 1793e6f633e5STiwei Bie */ 1794e6f633e5STiwei Bie int virtqueue_add_inbuf_ctx(struct virtqueue *vq, 1795e6f633e5STiwei Bie struct scatterlist *sg, unsigned int num, 1796e6f633e5STiwei Bie void *data, 1797e6f633e5STiwei Bie void *ctx, 1798e6f633e5STiwei Bie gfp_t gfp) 1799e6f633e5STiwei Bie { 1800e6f633e5STiwei Bie return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp); 1801e6f633e5STiwei Bie } 1802e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx); 1803e6f633e5STiwei Bie 1804e6f633e5STiwei Bie /** 1805e6f633e5STiwei Bie * virtqueue_kick_prepare - first half of split virtqueue_kick call. 1806e6f633e5STiwei Bie * @vq: the struct virtqueue 1807e6f633e5STiwei Bie * 1808e6f633e5STiwei Bie * Instead of virtqueue_kick(), you can do: 1809e6f633e5STiwei Bie * if (virtqueue_kick_prepare(vq)) 1810e6f633e5STiwei Bie * virtqueue_notify(vq); 1811e6f633e5STiwei Bie * 1812e6f633e5STiwei Bie * This is sometimes useful because the virtqueue_kick_prepare() needs 1813e6f633e5STiwei Bie * to be serialized, but the actual virtqueue_notify() call does not. 1814e6f633e5STiwei Bie */ 1815e6f633e5STiwei Bie bool virtqueue_kick_prepare(struct virtqueue *_vq) 1816e6f633e5STiwei Bie { 18171ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 18181ce9e605STiwei Bie 18191ce9e605STiwei Bie return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) : 18201ce9e605STiwei Bie virtqueue_kick_prepare_split(_vq); 1821e6f633e5STiwei Bie } 1822e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); 1823e6f633e5STiwei Bie 1824e6f633e5STiwei Bie /** 1825e6f633e5STiwei Bie * virtqueue_notify - second half of split virtqueue_kick call. 1826e6f633e5STiwei Bie * @vq: the struct virtqueue 1827e6f633e5STiwei Bie * 1828e6f633e5STiwei Bie * This does not need to be serialized. 1829e6f633e5STiwei Bie * 1830e6f633e5STiwei Bie * Returns false if host notify failed or queue is broken, otherwise true. 1831e6f633e5STiwei Bie */ 1832e6f633e5STiwei Bie bool virtqueue_notify(struct virtqueue *_vq) 1833e6f633e5STiwei Bie { 1834e6f633e5STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 1835e6f633e5STiwei Bie 1836e6f633e5STiwei Bie if (unlikely(vq->broken)) 1837e6f633e5STiwei Bie return false; 1838e6f633e5STiwei Bie 1839e6f633e5STiwei Bie /* Prod other side to tell it about changes. */ 1840e6f633e5STiwei Bie if (!vq->notify(_vq)) { 1841e6f633e5STiwei Bie vq->broken = true; 1842e6f633e5STiwei Bie return false; 1843e6f633e5STiwei Bie } 1844e6f633e5STiwei Bie return true; 1845e6f633e5STiwei Bie } 1846e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_notify); 1847e6f633e5STiwei Bie 1848e6f633e5STiwei Bie /** 1849e6f633e5STiwei Bie * virtqueue_kick - update after add_buf 1850e6f633e5STiwei Bie * @vq: the struct virtqueue 1851e6f633e5STiwei Bie * 1852e6f633e5STiwei Bie * After one or more virtqueue_add_* calls, invoke this to kick 1853e6f633e5STiwei Bie * the other side. 1854e6f633e5STiwei Bie * 1855e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 1856e6f633e5STiwei Bie * operations at the same time (except where noted). 1857e6f633e5STiwei Bie * 1858e6f633e5STiwei Bie * Returns false if kick failed, otherwise true. 1859e6f633e5STiwei Bie */ 1860e6f633e5STiwei Bie bool virtqueue_kick(struct virtqueue *vq) 1861e6f633e5STiwei Bie { 1862e6f633e5STiwei Bie if (virtqueue_kick_prepare(vq)) 1863e6f633e5STiwei Bie return virtqueue_notify(vq); 1864e6f633e5STiwei Bie return true; 1865e6f633e5STiwei Bie } 1866e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick); 1867e6f633e5STiwei Bie 1868e6f633e5STiwei Bie /** 1869e6f633e5STiwei Bie * virtqueue_get_buf - get the next used buffer 1870e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1871e6f633e5STiwei Bie * @len: the length written into the buffer 1872e6f633e5STiwei Bie * 1873e6f633e5STiwei Bie * If the device wrote data into the buffer, @len will be set to the 1874e6f633e5STiwei Bie * amount written. This means you don't need to clear the buffer 1875e6f633e5STiwei Bie * beforehand to ensure there's no data leakage in the case of short 1876e6f633e5STiwei Bie * writes. 1877e6f633e5STiwei Bie * 1878e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 1879e6f633e5STiwei Bie * operations at the same time (except where noted). 1880e6f633e5STiwei Bie * 1881e6f633e5STiwei Bie * Returns NULL if there are no used buffers, or the "data" token 1882e6f633e5STiwei Bie * handed to virtqueue_add_*(). 1883e6f633e5STiwei Bie */ 1884e6f633e5STiwei Bie void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len, 1885e6f633e5STiwei Bie void **ctx) 1886e6f633e5STiwei Bie { 18871ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 18881ce9e605STiwei Bie 18891ce9e605STiwei Bie return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) : 18901ce9e605STiwei Bie virtqueue_get_buf_ctx_split(_vq, len, ctx); 1891e6f633e5STiwei Bie } 1892e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx); 1893e6f633e5STiwei Bie 1894e6f633e5STiwei Bie void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) 1895e6f633e5STiwei Bie { 1896e6f633e5STiwei Bie return virtqueue_get_buf_ctx(_vq, len, NULL); 1897e6f633e5STiwei Bie } 1898e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf); 1899e6f633e5STiwei Bie /** 1900e6f633e5STiwei Bie * virtqueue_disable_cb - disable callbacks 1901e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1902e6f633e5STiwei Bie * 1903e6f633e5STiwei Bie * Note that this is not necessarily synchronous, hence unreliable and only 1904e6f633e5STiwei Bie * useful as an optimization. 1905e6f633e5STiwei Bie * 1906e6f633e5STiwei Bie * Unlike other operations, this need not be serialized. 1907e6f633e5STiwei Bie */ 1908e6f633e5STiwei Bie void virtqueue_disable_cb(struct virtqueue *_vq) 1909e6f633e5STiwei Bie { 19101ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 19111ce9e605STiwei Bie 19121ce9e605STiwei Bie if (vq->packed_ring) 19131ce9e605STiwei Bie virtqueue_disable_cb_packed(_vq); 19141ce9e605STiwei Bie else 1915e6f633e5STiwei Bie virtqueue_disable_cb_split(_vq); 1916e6f633e5STiwei Bie } 1917e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_disable_cb); 1918e6f633e5STiwei Bie 1919e6f633e5STiwei Bie /** 1920e6f633e5STiwei Bie * virtqueue_enable_cb_prepare - restart callbacks after disable_cb 1921e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1922e6f633e5STiwei Bie * 1923e6f633e5STiwei Bie * This re-enables callbacks; it returns current queue state 1924e6f633e5STiwei Bie * in an opaque unsigned value. This value should be later tested by 1925e6f633e5STiwei Bie * virtqueue_poll, to detect a possible race between the driver checking for 1926e6f633e5STiwei Bie * more work, and enabling callbacks. 1927e6f633e5STiwei Bie * 1928e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 1929e6f633e5STiwei Bie * operations at the same time (except where noted). 1930e6f633e5STiwei Bie */ 1931e6f633e5STiwei Bie unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) 1932e6f633e5STiwei Bie { 19331ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 19341ce9e605STiwei Bie 19351ce9e605STiwei Bie return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) : 19361ce9e605STiwei Bie virtqueue_enable_cb_prepare_split(_vq); 1937e6f633e5STiwei Bie } 1938e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); 1939e6f633e5STiwei Bie 1940e6f633e5STiwei Bie /** 1941e6f633e5STiwei Bie * virtqueue_poll - query pending used buffers 1942e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1943e6f633e5STiwei Bie * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). 1944e6f633e5STiwei Bie * 1945e6f633e5STiwei Bie * Returns "true" if there are pending used buffers in the queue. 1946e6f633e5STiwei Bie * 1947e6f633e5STiwei Bie * This does not need to be serialized. 1948e6f633e5STiwei Bie */ 1949e6f633e5STiwei Bie bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) 1950e6f633e5STiwei Bie { 1951e6f633e5STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 1952e6f633e5STiwei Bie 1953e6f633e5STiwei Bie virtio_mb(vq->weak_barriers); 19541ce9e605STiwei Bie return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) : 19551ce9e605STiwei Bie virtqueue_poll_split(_vq, last_used_idx); 1956e6f633e5STiwei Bie } 1957e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_poll); 1958e6f633e5STiwei Bie 1959e6f633e5STiwei Bie /** 1960e6f633e5STiwei Bie * virtqueue_enable_cb - restart callbacks after disable_cb. 1961e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1962e6f633e5STiwei Bie * 1963e6f633e5STiwei Bie * This re-enables callbacks; it returns "false" if there are pending 1964e6f633e5STiwei Bie * buffers in the queue, to detect a possible race between the driver 1965e6f633e5STiwei Bie * checking for more work, and enabling callbacks. 1966e6f633e5STiwei Bie * 1967e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 1968e6f633e5STiwei Bie * operations at the same time (except where noted). 1969e6f633e5STiwei Bie */ 1970e6f633e5STiwei Bie bool virtqueue_enable_cb(struct virtqueue *_vq) 1971e6f633e5STiwei Bie { 1972e6f633e5STiwei Bie unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); 1973e6f633e5STiwei Bie 1974e6f633e5STiwei Bie return !virtqueue_poll(_vq, last_used_idx); 1975e6f633e5STiwei Bie } 1976e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb); 1977e6f633e5STiwei Bie 1978e6f633e5STiwei Bie /** 1979e6f633e5STiwei Bie * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. 1980e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1981e6f633e5STiwei Bie * 1982e6f633e5STiwei Bie * This re-enables callbacks but hints to the other side to delay 1983e6f633e5STiwei Bie * interrupts until most of the available buffers have been processed; 1984e6f633e5STiwei Bie * it returns "false" if there are many pending buffers in the queue, 1985e6f633e5STiwei Bie * to detect a possible race between the driver checking for more work, 1986e6f633e5STiwei Bie * and enabling callbacks. 1987e6f633e5STiwei Bie * 1988e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 1989e6f633e5STiwei Bie * operations at the same time (except where noted). 1990e6f633e5STiwei Bie */ 1991e6f633e5STiwei Bie bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) 1992e6f633e5STiwei Bie { 19931ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 19941ce9e605STiwei Bie 19951ce9e605STiwei Bie return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) : 19961ce9e605STiwei Bie virtqueue_enable_cb_delayed_split(_vq); 1997e6f633e5STiwei Bie } 1998e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); 1999e6f633e5STiwei Bie 2000138fd251STiwei Bie /** 2001138fd251STiwei Bie * virtqueue_detach_unused_buf - detach first unused buffer 2002138fd251STiwei Bie * @vq: the struct virtqueue we're talking about. 2003138fd251STiwei Bie * 2004138fd251STiwei Bie * Returns NULL or the "data" token handed to virtqueue_add_*(). 2005138fd251STiwei Bie * This is not valid on an active queue; it is useful only for device 2006138fd251STiwei Bie * shutdown. 2007138fd251STiwei Bie */ 2008138fd251STiwei Bie void *virtqueue_detach_unused_buf(struct virtqueue *_vq) 2009138fd251STiwei Bie { 20101ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 20111ce9e605STiwei Bie 20121ce9e605STiwei Bie return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) : 20131ce9e605STiwei Bie virtqueue_detach_unused_buf_split(_vq); 2014138fd251STiwei Bie } 20157c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); 2016c021eac4SShirley Ma 2017138fd251STiwei Bie static inline bool more_used(const struct vring_virtqueue *vq) 2018138fd251STiwei Bie { 20191ce9e605STiwei Bie return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq); 2020138fd251STiwei Bie } 2021138fd251STiwei Bie 20220a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq) 20230a8a69ddSRusty Russell { 20240a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 20250a8a69ddSRusty Russell 20260a8a69ddSRusty Russell if (!more_used(vq)) { 20270a8a69ddSRusty Russell pr_debug("virtqueue interrupt with no work for %p\n", vq); 20280a8a69ddSRusty Russell return IRQ_NONE; 20290a8a69ddSRusty Russell } 20300a8a69ddSRusty Russell 20310a8a69ddSRusty Russell if (unlikely(vq->broken)) 20320a8a69ddSRusty Russell return IRQ_HANDLED; 20330a8a69ddSRusty Russell 20340a8a69ddSRusty Russell pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); 203518445c4dSRusty Russell if (vq->vq.callback) 203618445c4dSRusty Russell vq->vq.callback(&vq->vq); 20370a8a69ddSRusty Russell 20380a8a69ddSRusty Russell return IRQ_HANDLED; 20390a8a69ddSRusty Russell } 2040c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt); 20410a8a69ddSRusty Russell 20421ce9e605STiwei Bie /* Only available for split ring */ 20432a2d1382SAndy Lutomirski struct virtqueue *__vring_new_virtqueue(unsigned int index, 20442a2d1382SAndy Lutomirski struct vring vring, 20450a8a69ddSRusty Russell struct virtio_device *vdev, 20467b21e34fSRusty Russell bool weak_barriers, 2047f94682ddSMichael S. Tsirkin bool context, 204846f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *), 20499499f5e7SRusty Russell void (*callback)(struct virtqueue *), 20509499f5e7SRusty Russell const char *name) 20510a8a69ddSRusty Russell { 20520a8a69ddSRusty Russell unsigned int i; 20532a2d1382SAndy Lutomirski struct vring_virtqueue *vq; 20540a8a69ddSRusty Russell 20551ce9e605STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) 20561ce9e605STiwei Bie return NULL; 20571ce9e605STiwei Bie 2058cbeedb72STiwei Bie vq = kmalloc(sizeof(*vq), GFP_KERNEL); 20590a8a69ddSRusty Russell if (!vq) 20600a8a69ddSRusty Russell return NULL; 20610a8a69ddSRusty Russell 20621ce9e605STiwei Bie vq->packed_ring = false; 20630a8a69ddSRusty Russell vq->vq.callback = callback; 20640a8a69ddSRusty Russell vq->vq.vdev = vdev; 20659499f5e7SRusty Russell vq->vq.name = name; 20662a2d1382SAndy Lutomirski vq->vq.num_free = vring.num; 206706ca287dSRusty Russell vq->vq.index = index; 20682a2d1382SAndy Lutomirski vq->we_own_ring = false; 20690a8a69ddSRusty Russell vq->notify = notify; 20707b21e34fSRusty Russell vq->weak_barriers = weak_barriers; 20710a8a69ddSRusty Russell vq->broken = false; 20720a8a69ddSRusty Russell vq->last_used_idx = 0; 20730a8a69ddSRusty Russell vq->num_added = 0; 2074fb3fba6bSTiwei Bie vq->use_dma_api = vring_use_dma_api(vdev); 20759499f5e7SRusty Russell list_add_tail(&vq->vq.list, &vdev->vqs); 20760a8a69ddSRusty Russell #ifdef DEBUG 20770a8a69ddSRusty Russell vq->in_use = false; 2078e93300b1SRusty Russell vq->last_add_time_valid = false; 20790a8a69ddSRusty Russell #endif 20800a8a69ddSRusty Russell 20815a08b04fSMichael S. Tsirkin vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && 20825a08b04fSMichael S. Tsirkin !context; 2083a5c262c5SMichael S. Tsirkin vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 20849fa29b9dSMark McLoughlin 2085*45383fb0STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) 2086*45383fb0STiwei Bie vq->weak_barriers = false; 2087*45383fb0STiwei Bie 2088d79dca75STiwei Bie vq->split.queue_dma_addr = 0; 2089d79dca75STiwei Bie vq->split.queue_size_in_bytes = 0; 2090d79dca75STiwei Bie 2091e593bf97STiwei Bie vq->split.vring = vring; 2092e593bf97STiwei Bie vq->split.avail_flags_shadow = 0; 2093e593bf97STiwei Bie vq->split.avail_idx_shadow = 0; 2094e593bf97STiwei Bie 20950a8a69ddSRusty Russell /* No callback? Tell other side not to bother us. */ 2096f277ec42SVenkatesh Srinivas if (!callback) { 2097e593bf97STiwei Bie vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 20980ea1e4a6SLadi Prosek if (!vq->event) 2099e593bf97STiwei Bie vq->split.vring.avail->flags = cpu_to_virtio16(vdev, 2100e593bf97STiwei Bie vq->split.avail_flags_shadow); 2101f277ec42SVenkatesh Srinivas } 21020a8a69ddSRusty Russell 2103cbeedb72STiwei Bie vq->split.desc_state = kmalloc_array(vring.num, 2104cbeedb72STiwei Bie sizeof(struct vring_desc_state_split), GFP_KERNEL); 2105cbeedb72STiwei Bie if (!vq->split.desc_state) { 2106cbeedb72STiwei Bie kfree(vq); 2107cbeedb72STiwei Bie return NULL; 2108cbeedb72STiwei Bie } 2109cbeedb72STiwei Bie 21100a8a69ddSRusty Russell /* Put everything in free lists. */ 21110a8a69ddSRusty Russell vq->free_head = 0; 21122a2d1382SAndy Lutomirski for (i = 0; i < vring.num-1; i++) 2113e593bf97STiwei Bie vq->split.vring.desc[i].next = cpu_to_virtio16(vdev, i + 1); 2114cbeedb72STiwei Bie memset(vq->split.desc_state, 0, vring.num * 2115cbeedb72STiwei Bie sizeof(struct vring_desc_state_split)); 21160a8a69ddSRusty Russell 21170a8a69ddSRusty Russell return &vq->vq; 21180a8a69ddSRusty Russell } 21192a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(__vring_new_virtqueue); 21202a2d1382SAndy Lutomirski 21212a2d1382SAndy Lutomirski struct virtqueue *vring_create_virtqueue( 21222a2d1382SAndy Lutomirski unsigned int index, 21232a2d1382SAndy Lutomirski unsigned int num, 21242a2d1382SAndy Lutomirski unsigned int vring_align, 21252a2d1382SAndy Lutomirski struct virtio_device *vdev, 21262a2d1382SAndy Lutomirski bool weak_barriers, 21272a2d1382SAndy Lutomirski bool may_reduce_num, 2128f94682ddSMichael S. Tsirkin bool context, 21292a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *), 21302a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *), 21312a2d1382SAndy Lutomirski const char *name) 21322a2d1382SAndy Lutomirski { 21331ce9e605STiwei Bie 21341ce9e605STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) 21351ce9e605STiwei Bie return vring_create_virtqueue_packed(index, num, vring_align, 21361ce9e605STiwei Bie vdev, weak_barriers, may_reduce_num, 21371ce9e605STiwei Bie context, notify, callback, name); 21381ce9e605STiwei Bie 2139d79dca75STiwei Bie return vring_create_virtqueue_split(index, num, vring_align, 2140d79dca75STiwei Bie vdev, weak_barriers, may_reduce_num, 2141d79dca75STiwei Bie context, notify, callback, name); 21422a2d1382SAndy Lutomirski } 21432a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(vring_create_virtqueue); 21442a2d1382SAndy Lutomirski 21451ce9e605STiwei Bie /* Only available for split ring */ 21462a2d1382SAndy Lutomirski struct virtqueue *vring_new_virtqueue(unsigned int index, 21472a2d1382SAndy Lutomirski unsigned int num, 21482a2d1382SAndy Lutomirski unsigned int vring_align, 21492a2d1382SAndy Lutomirski struct virtio_device *vdev, 21502a2d1382SAndy Lutomirski bool weak_barriers, 2151f94682ddSMichael S. Tsirkin bool context, 21522a2d1382SAndy Lutomirski void *pages, 21532a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *vq), 21542a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *vq), 21552a2d1382SAndy Lutomirski const char *name) 21562a2d1382SAndy Lutomirski { 21572a2d1382SAndy Lutomirski struct vring vring; 21581ce9e605STiwei Bie 21591ce9e605STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) 21601ce9e605STiwei Bie return NULL; 21611ce9e605STiwei Bie 21622a2d1382SAndy Lutomirski vring_init(&vring, num, pages, vring_align); 2163f94682ddSMichael S. Tsirkin return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, 21642a2d1382SAndy Lutomirski notify, callback, name); 21652a2d1382SAndy Lutomirski } 2166c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue); 21670a8a69ddSRusty Russell 21682a2d1382SAndy Lutomirski void vring_del_virtqueue(struct virtqueue *_vq) 21690a8a69ddSRusty Russell { 21702a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 21712a2d1382SAndy Lutomirski 21722a2d1382SAndy Lutomirski if (vq->we_own_ring) { 21731ce9e605STiwei Bie if (vq->packed_ring) { 21741ce9e605STiwei Bie vring_free_queue(vq->vq.vdev, 21751ce9e605STiwei Bie vq->packed.ring_size_in_bytes, 21761ce9e605STiwei Bie vq->packed.vring.desc, 21771ce9e605STiwei Bie vq->packed.ring_dma_addr); 21781ce9e605STiwei Bie 21791ce9e605STiwei Bie vring_free_queue(vq->vq.vdev, 21801ce9e605STiwei Bie vq->packed.event_size_in_bytes, 21811ce9e605STiwei Bie vq->packed.vring.driver, 21821ce9e605STiwei Bie vq->packed.driver_event_dma_addr); 21831ce9e605STiwei Bie 21841ce9e605STiwei Bie vring_free_queue(vq->vq.vdev, 21851ce9e605STiwei Bie vq->packed.event_size_in_bytes, 21861ce9e605STiwei Bie vq->packed.vring.device, 21871ce9e605STiwei Bie vq->packed.device_event_dma_addr); 21881ce9e605STiwei Bie 21891ce9e605STiwei Bie kfree(vq->packed.desc_state); 21901ce9e605STiwei Bie kfree(vq->packed.desc_extra); 21911ce9e605STiwei Bie } else { 2192d79dca75STiwei Bie vring_free_queue(vq->vq.vdev, 2193d79dca75STiwei Bie vq->split.queue_size_in_bytes, 2194d79dca75STiwei Bie vq->split.vring.desc, 2195d79dca75STiwei Bie vq->split.queue_dma_addr); 21961ce9e605STiwei Bie 2197cbeedb72STiwei Bie kfree(vq->split.desc_state); 21982a2d1382SAndy Lutomirski } 21991ce9e605STiwei Bie } 22002a2d1382SAndy Lutomirski list_del(&_vq->list); 22012a2d1382SAndy Lutomirski kfree(vq); 22020a8a69ddSRusty Russell } 2203c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue); 22040a8a69ddSRusty Russell 2205e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */ 2206e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev) 2207e34f8725SRusty Russell { 2208e34f8725SRusty Russell unsigned int i; 2209e34f8725SRusty Russell 2210e34f8725SRusty Russell for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { 2211e34f8725SRusty Russell switch (i) { 22129fa29b9dSMark McLoughlin case VIRTIO_RING_F_INDIRECT_DESC: 22139fa29b9dSMark McLoughlin break; 2214a5c262c5SMichael S. Tsirkin case VIRTIO_RING_F_EVENT_IDX: 2215a5c262c5SMichael S. Tsirkin break; 2216747ae34aSMichael S. Tsirkin case VIRTIO_F_VERSION_1: 2217747ae34aSMichael S. Tsirkin break; 22181a937693SMichael S. Tsirkin case VIRTIO_F_IOMMU_PLATFORM: 22191a937693SMichael S. Tsirkin break; 2220f959a128STiwei Bie case VIRTIO_F_RING_PACKED: 2221f959a128STiwei Bie break; 2222*45383fb0STiwei Bie case VIRTIO_F_ORDER_PLATFORM: 2223*45383fb0STiwei Bie break; 2224e34f8725SRusty Russell default: 2225e34f8725SRusty Russell /* We don't understand this bit. */ 2226e16e12beSMichael S. Tsirkin __virtio_clear_bit(vdev, i); 2227e34f8725SRusty Russell } 2228e34f8725SRusty Russell } 2229e34f8725SRusty Russell } 2230e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features); 2231e34f8725SRusty Russell 22325dfc1762SRusty Russell /** 22335dfc1762SRusty Russell * virtqueue_get_vring_size - return the size of the virtqueue's vring 22345dfc1762SRusty Russell * @vq: the struct virtqueue containing the vring of interest. 22355dfc1762SRusty Russell * 22365dfc1762SRusty Russell * Returns the size of the vring. This is mainly used for boasting to 22375dfc1762SRusty Russell * userspace. Unlike other operations, this need not be serialized. 22385dfc1762SRusty Russell */ 22398f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) 22408f9f4668SRick Jones { 22418f9f4668SRick Jones 22428f9f4668SRick Jones struct vring_virtqueue *vq = to_vvq(_vq); 22438f9f4668SRick Jones 22441ce9e605STiwei Bie return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num; 22458f9f4668SRick Jones } 22468f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); 22478f9f4668SRick Jones 2248b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq) 2249b3b32c94SHeinz Graalfs { 2250b3b32c94SHeinz Graalfs struct vring_virtqueue *vq = to_vvq(_vq); 2251b3b32c94SHeinz Graalfs 2252b3b32c94SHeinz Graalfs return vq->broken; 2253b3b32c94SHeinz Graalfs } 2254b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken); 2255b3b32c94SHeinz Graalfs 2256e2dcdfe9SRusty Russell /* 2257e2dcdfe9SRusty Russell * This should prevent the device from being used, allowing drivers to 2258e2dcdfe9SRusty Russell * recover. You may need to grab appropriate locks to flush. 2259e2dcdfe9SRusty Russell */ 2260e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev) 2261e2dcdfe9SRusty Russell { 2262e2dcdfe9SRusty Russell struct virtqueue *_vq; 2263e2dcdfe9SRusty Russell 2264e2dcdfe9SRusty Russell list_for_each_entry(_vq, &dev->vqs, list) { 2265e2dcdfe9SRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 2266e2dcdfe9SRusty Russell vq->broken = true; 2267e2dcdfe9SRusty Russell } 2268e2dcdfe9SRusty Russell } 2269e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device); 2270e2dcdfe9SRusty Russell 22712a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq) 227289062652SCornelia Huck { 227389062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 227489062652SCornelia Huck 22752a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 227689062652SCornelia Huck 22771ce9e605STiwei Bie if (vq->packed_ring) 22781ce9e605STiwei Bie return vq->packed.ring_dma_addr; 22791ce9e605STiwei Bie 2280d79dca75STiwei Bie return vq->split.queue_dma_addr; 22812a2d1382SAndy Lutomirski } 22822a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr); 22832a2d1382SAndy Lutomirski 22842a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq) 228589062652SCornelia Huck { 228689062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 228789062652SCornelia Huck 22882a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 22892a2d1382SAndy Lutomirski 22901ce9e605STiwei Bie if (vq->packed_ring) 22911ce9e605STiwei Bie return vq->packed.driver_event_dma_addr; 22921ce9e605STiwei Bie 2293d79dca75STiwei Bie return vq->split.queue_dma_addr + 2294e593bf97STiwei Bie ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc); 229589062652SCornelia Huck } 22962a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr); 22972a2d1382SAndy Lutomirski 22982a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq) 22992a2d1382SAndy Lutomirski { 23002a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 23012a2d1382SAndy Lutomirski 23022a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 23032a2d1382SAndy Lutomirski 23041ce9e605STiwei Bie if (vq->packed_ring) 23051ce9e605STiwei Bie return vq->packed.device_event_dma_addr; 23061ce9e605STiwei Bie 2307d79dca75STiwei Bie return vq->split.queue_dma_addr + 2308e593bf97STiwei Bie ((char *)vq->split.vring.used - (char *)vq->split.vring.desc); 23092a2d1382SAndy Lutomirski } 23102a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_used_addr); 23112a2d1382SAndy Lutomirski 23121ce9e605STiwei Bie /* Only available for split ring */ 23132a2d1382SAndy Lutomirski const struct vring *virtqueue_get_vring(struct virtqueue *vq) 23142a2d1382SAndy Lutomirski { 2315e593bf97STiwei Bie return &to_vvq(vq)->split.vring; 23162a2d1382SAndy Lutomirski } 23172a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_vring); 231889062652SCornelia Huck 2319c6fd4701SRusty Russell MODULE_LICENSE("GPL"); 2320