1fd534e9bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 20a8a69ddSRusty Russell /* Virtio ring implementation. 30a8a69ddSRusty Russell * 40a8a69ddSRusty Russell * Copyright 2007 Rusty Russell IBM Corporation 50a8a69ddSRusty Russell */ 60a8a69ddSRusty Russell #include <linux/virtio.h> 70a8a69ddSRusty Russell #include <linux/virtio_ring.h> 8e34f8725SRusty Russell #include <linux/virtio_config.h> 90a8a69ddSRusty Russell #include <linux/device.h> 105a0e3ad6STejun Heo #include <linux/slab.h> 11b5a2c4f1SPaul Gortmaker #include <linux/module.h> 12e93300b1SRusty Russell #include <linux/hrtimer.h> 13780bc790SAndy Lutomirski #include <linux/dma-mapping.h> 14f8ce7263SMichael S. Tsirkin #include <linux/spinlock.h> 1578fe3987SAndy Lutomirski #include <xen/xen.h> 160a8a69ddSRusty Russell 170a8a69ddSRusty Russell #ifdef DEBUG 180a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */ 199499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 209499f5e7SRusty Russell do { \ 219499f5e7SRusty Russell dev_err(&(_vq)->vq.vdev->dev, \ 229499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 239499f5e7SRusty Russell BUG(); \ 249499f5e7SRusty Russell } while (0) 25c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */ 263a35ce7dSRoel Kluin #define START_USE(_vq) \ 27c5f841f1SRusty Russell do { \ 28c5f841f1SRusty Russell if ((_vq)->in_use) \ 299499f5e7SRusty Russell panic("%s:in_use = %i\n", \ 309499f5e7SRusty Russell (_vq)->vq.name, (_vq)->in_use); \ 31c5f841f1SRusty Russell (_vq)->in_use = __LINE__; \ 32c5f841f1SRusty Russell } while (0) 333a35ce7dSRoel Kluin #define END_USE(_vq) \ 3497a545abSRusty Russell do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) 354d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(_vq) \ 364d6a105eSTiwei Bie do { \ 374d6a105eSTiwei Bie ktime_t now = ktime_get(); \ 384d6a105eSTiwei Bie \ 394d6a105eSTiwei Bie /* No kick or get, with .1 second between? Warn. */ \ 404d6a105eSTiwei Bie if ((_vq)->last_add_time_valid) \ 414d6a105eSTiwei Bie WARN_ON(ktime_to_ms(ktime_sub(now, \ 424d6a105eSTiwei Bie (_vq)->last_add_time)) > 100); \ 434d6a105eSTiwei Bie (_vq)->last_add_time = now; \ 444d6a105eSTiwei Bie (_vq)->last_add_time_valid = true; \ 454d6a105eSTiwei Bie } while (0) 464d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(_vq) \ 474d6a105eSTiwei Bie do { \ 484d6a105eSTiwei Bie if ((_vq)->last_add_time_valid) { \ 494d6a105eSTiwei Bie WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \ 504d6a105eSTiwei Bie (_vq)->last_add_time)) > 100); \ 514d6a105eSTiwei Bie } \ 524d6a105eSTiwei Bie } while (0) 534d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(_vq) \ 544d6a105eSTiwei Bie ((_vq)->last_add_time_valid = false) 550a8a69ddSRusty Russell #else 569499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 579499f5e7SRusty Russell do { \ 589499f5e7SRusty Russell dev_err(&_vq->vq.vdev->dev, \ 599499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 609499f5e7SRusty Russell (_vq)->broken = true; \ 619499f5e7SRusty Russell } while (0) 620a8a69ddSRusty Russell #define START_USE(vq) 630a8a69ddSRusty Russell #define END_USE(vq) 644d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(vq) 654d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(vq) 664d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(vq) 670a8a69ddSRusty Russell #endif 680a8a69ddSRusty Russell 69cbeedb72STiwei Bie struct vring_desc_state_split { 70780bc790SAndy Lutomirski void *data; /* Data for callback. */ 71780bc790SAndy Lutomirski struct vring_desc *indir_desc; /* Indirect descriptor, if any. */ 72780bc790SAndy Lutomirski }; 73780bc790SAndy Lutomirski 741ce9e605STiwei Bie struct vring_desc_state_packed { 751ce9e605STiwei Bie void *data; /* Data for callback. */ 761ce9e605STiwei Bie struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */ 771ce9e605STiwei Bie u16 num; /* Descriptor list length. */ 781ce9e605STiwei Bie u16 last; /* The last desc state in a list. */ 791ce9e605STiwei Bie }; 801ce9e605STiwei Bie 811f28750fSJason Wang struct vring_desc_extra { 82ef5c366fSJason Wang dma_addr_t addr; /* Descriptor DMA addr. */ 83ef5c366fSJason Wang u32 len; /* Descriptor length. */ 841ce9e605STiwei Bie u16 flags; /* Descriptor flags. */ 85aeef9b47SJason Wang u16 next; /* The next desc state in a list. */ 861ce9e605STiwei Bie }; 871ce9e605STiwei Bie 8843b4f721SMichael S. Tsirkin struct vring_virtqueue { 890a8a69ddSRusty Russell struct virtqueue vq; 900a8a69ddSRusty Russell 911ce9e605STiwei Bie /* Is this a packed ring? */ 921ce9e605STiwei Bie bool packed_ring; 931ce9e605STiwei Bie 94fb3fba6bSTiwei Bie /* Is DMA API used? */ 95fb3fba6bSTiwei Bie bool use_dma_api; 96fb3fba6bSTiwei Bie 977b21e34fSRusty Russell /* Can we use weak barriers? */ 987b21e34fSRusty Russell bool weak_barriers; 997b21e34fSRusty Russell 1000a8a69ddSRusty Russell /* Other side has made a mess, don't try any more. */ 1010a8a69ddSRusty Russell bool broken; 1020a8a69ddSRusty Russell 1039fa29b9dSMark McLoughlin /* Host supports indirect buffers */ 1049fa29b9dSMark McLoughlin bool indirect; 1059fa29b9dSMark McLoughlin 106a5c262c5SMichael S. Tsirkin /* Host publishes avail event idx */ 107a5c262c5SMichael S. Tsirkin bool event; 108a5c262c5SMichael S. Tsirkin 1090a8a69ddSRusty Russell /* Head of free buffer list. */ 1100a8a69ddSRusty Russell unsigned int free_head; 1110a8a69ddSRusty Russell /* Number we've added since last sync. */ 1120a8a69ddSRusty Russell unsigned int num_added; 1130a8a69ddSRusty Russell 1140a8a69ddSRusty Russell /* Last used index we've seen. */ 1151bc4953eSAnthony Liguori u16 last_used_idx; 1160a8a69ddSRusty Russell 1178d622d21SMichael S. Tsirkin /* Hint for event idx: already triggered no need to disable. */ 1188d622d21SMichael S. Tsirkin bool event_triggered; 1198d622d21SMichael S. Tsirkin 1201ce9e605STiwei Bie union { 1211ce9e605STiwei Bie /* Available for split ring */ 122e593bf97STiwei Bie struct { 1231ce9e605STiwei Bie /* Actual memory layout for this queue. */ 124e593bf97STiwei Bie struct vring vring; 125e593bf97STiwei Bie 126f277ec42SVenkatesh Srinivas /* Last written value to avail->flags */ 127f277ec42SVenkatesh Srinivas u16 avail_flags_shadow; 128f277ec42SVenkatesh Srinivas 1291ce9e605STiwei Bie /* 1301ce9e605STiwei Bie * Last written value to avail->idx in 1311ce9e605STiwei Bie * guest byte order. 1321ce9e605STiwei Bie */ 133f277ec42SVenkatesh Srinivas u16 avail_idx_shadow; 134cbeedb72STiwei Bie 135cbeedb72STiwei Bie /* Per-descriptor state. */ 136cbeedb72STiwei Bie struct vring_desc_state_split *desc_state; 13772b5e895SJason Wang struct vring_desc_extra *desc_extra; 138d79dca75STiwei Bie 1391ce9e605STiwei Bie /* DMA address and size information */ 140d79dca75STiwei Bie dma_addr_t queue_dma_addr; 1411ce9e605STiwei Bie size_t queue_size_in_bytes; 142e593bf97STiwei Bie } split; 143f277ec42SVenkatesh Srinivas 1441ce9e605STiwei Bie /* Available for packed ring */ 1451ce9e605STiwei Bie struct { 1461ce9e605STiwei Bie /* Actual memory layout for this queue. */ 1479c0644eeSMichael S. Tsirkin struct { 1489c0644eeSMichael S. Tsirkin unsigned int num; 1499c0644eeSMichael S. Tsirkin struct vring_packed_desc *desc; 1509c0644eeSMichael S. Tsirkin struct vring_packed_desc_event *driver; 1519c0644eeSMichael S. Tsirkin struct vring_packed_desc_event *device; 1529c0644eeSMichael S. Tsirkin } vring; 1531ce9e605STiwei Bie 1541ce9e605STiwei Bie /* Driver ring wrap counter. */ 1551ce9e605STiwei Bie bool avail_wrap_counter; 1561ce9e605STiwei Bie 1571ce9e605STiwei Bie /* Device ring wrap counter. */ 1581ce9e605STiwei Bie bool used_wrap_counter; 1591ce9e605STiwei Bie 1601ce9e605STiwei Bie /* Avail used flags. */ 1611ce9e605STiwei Bie u16 avail_used_flags; 1621ce9e605STiwei Bie 1631ce9e605STiwei Bie /* Index of the next avail descriptor. */ 1641ce9e605STiwei Bie u16 next_avail_idx; 1651ce9e605STiwei Bie 1661ce9e605STiwei Bie /* 1671ce9e605STiwei Bie * Last written value to driver->flags in 1681ce9e605STiwei Bie * guest byte order. 1691ce9e605STiwei Bie */ 1701ce9e605STiwei Bie u16 event_flags_shadow; 1711ce9e605STiwei Bie 1721ce9e605STiwei Bie /* Per-descriptor state. */ 1731ce9e605STiwei Bie struct vring_desc_state_packed *desc_state; 1741f28750fSJason Wang struct vring_desc_extra *desc_extra; 1751ce9e605STiwei Bie 1761ce9e605STiwei Bie /* DMA address and size information */ 1771ce9e605STiwei Bie dma_addr_t ring_dma_addr; 1781ce9e605STiwei Bie dma_addr_t driver_event_dma_addr; 1791ce9e605STiwei Bie dma_addr_t device_event_dma_addr; 1801ce9e605STiwei Bie size_t ring_size_in_bytes; 1811ce9e605STiwei Bie size_t event_size_in_bytes; 1821ce9e605STiwei Bie } packed; 1831ce9e605STiwei Bie }; 1841ce9e605STiwei Bie 1850a8a69ddSRusty Russell /* How to notify other side. FIXME: commonalize hcalls! */ 18646f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *vq); 1870a8a69ddSRusty Russell 1882a2d1382SAndy Lutomirski /* DMA, allocation, and size information */ 1892a2d1382SAndy Lutomirski bool we_own_ring; 1902a2d1382SAndy Lutomirski 1910a8a69ddSRusty Russell #ifdef DEBUG 1920a8a69ddSRusty Russell /* They're supposed to lock for us. */ 1930a8a69ddSRusty Russell unsigned int in_use; 194e93300b1SRusty Russell 195e93300b1SRusty Russell /* Figure out if their kicks are too delayed. */ 196e93300b1SRusty Russell bool last_add_time_valid; 197e93300b1SRusty Russell ktime_t last_add_time; 1980a8a69ddSRusty Russell #endif 1990a8a69ddSRusty Russell }; 2000a8a69ddSRusty Russell 201e6f633e5STiwei Bie 202e6f633e5STiwei Bie /* 203e6f633e5STiwei Bie * Helpers. 204e6f633e5STiwei Bie */ 205e6f633e5STiwei Bie 2060a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) 2070a8a69ddSRusty Russell 208*35c51e09SXianting Tian static inline bool virtqueue_use_indirect(struct vring_virtqueue *vq, 2092f18c2d1STiwei Bie unsigned int total_sg) 2102f18c2d1STiwei Bie { 2112f18c2d1STiwei Bie /* 2122f18c2d1STiwei Bie * If the host supports indirect descriptor tables, and we have multiple 2132f18c2d1STiwei Bie * buffers, then go indirect. FIXME: tune this threshold 2142f18c2d1STiwei Bie */ 2152f18c2d1STiwei Bie return (vq->indirect && total_sg > 1 && vq->vq.num_free); 2162f18c2d1STiwei Bie } 2172f18c2d1STiwei Bie 218d26c96c8SAndy Lutomirski /* 2191a937693SMichael S. Tsirkin * Modern virtio devices have feature bits to specify whether they need a 2201a937693SMichael S. Tsirkin * quirk and bypass the IOMMU. If not there, just use the DMA API. 2211a937693SMichael S. Tsirkin * 2221a937693SMichael S. Tsirkin * If there, the interaction between virtio and DMA API is messy. 223d26c96c8SAndy Lutomirski * 224d26c96c8SAndy Lutomirski * On most systems with virtio, physical addresses match bus addresses, 225d26c96c8SAndy Lutomirski * and it doesn't particularly matter whether we use the DMA API. 226d26c96c8SAndy Lutomirski * 227d26c96c8SAndy Lutomirski * On some systems, including Xen and any system with a physical device 228d26c96c8SAndy Lutomirski * that speaks virtio behind a physical IOMMU, we must use the DMA API 229d26c96c8SAndy Lutomirski * for virtio DMA to work at all. 230d26c96c8SAndy Lutomirski * 231d26c96c8SAndy Lutomirski * On other systems, including SPARC and PPC64, virtio-pci devices are 232d26c96c8SAndy Lutomirski * enumerated as though they are behind an IOMMU, but the virtio host 233d26c96c8SAndy Lutomirski * ignores the IOMMU, so we must either pretend that the IOMMU isn't 234d26c96c8SAndy Lutomirski * there or somehow map everything as the identity. 235d26c96c8SAndy Lutomirski * 236d26c96c8SAndy Lutomirski * For the time being, we preserve historic behavior and bypass the DMA 237d26c96c8SAndy Lutomirski * API. 2381a937693SMichael S. Tsirkin * 2391a937693SMichael S. Tsirkin * TODO: install a per-device DMA ops structure that does the right thing 2401a937693SMichael S. Tsirkin * taking into account all the above quirks, and use the DMA API 2411a937693SMichael S. Tsirkin * unconditionally on data path. 242d26c96c8SAndy Lutomirski */ 243d26c96c8SAndy Lutomirski 244d26c96c8SAndy Lutomirski static bool vring_use_dma_api(struct virtio_device *vdev) 245d26c96c8SAndy Lutomirski { 24624b6842aSMichael S. Tsirkin if (!virtio_has_dma_quirk(vdev)) 2471a937693SMichael S. Tsirkin return true; 2481a937693SMichael S. Tsirkin 2491a937693SMichael S. Tsirkin /* Otherwise, we are left to guess. */ 25078fe3987SAndy Lutomirski /* 25178fe3987SAndy Lutomirski * In theory, it's possible to have a buggy QEMU-supposed 25278fe3987SAndy Lutomirski * emulated Q35 IOMMU and Xen enabled at the same time. On 25378fe3987SAndy Lutomirski * such a configuration, virtio has never worked and will 25478fe3987SAndy Lutomirski * not work without an even larger kludge. Instead, enable 25578fe3987SAndy Lutomirski * the DMA API if we're a Xen guest, which at least allows 25678fe3987SAndy Lutomirski * all of the sensible Xen configurations to work correctly. 25778fe3987SAndy Lutomirski */ 25878fe3987SAndy Lutomirski if (xen_domain()) 25978fe3987SAndy Lutomirski return true; 26078fe3987SAndy Lutomirski 261d26c96c8SAndy Lutomirski return false; 262d26c96c8SAndy Lutomirski } 263d26c96c8SAndy Lutomirski 264e6d6dd6cSJoerg Roedel size_t virtio_max_dma_size(struct virtio_device *vdev) 265e6d6dd6cSJoerg Roedel { 266e6d6dd6cSJoerg Roedel size_t max_segment_size = SIZE_MAX; 267e6d6dd6cSJoerg Roedel 268e6d6dd6cSJoerg Roedel if (vring_use_dma_api(vdev)) 269817fc978SWill Deacon max_segment_size = dma_max_mapping_size(vdev->dev.parent); 270e6d6dd6cSJoerg Roedel 271e6d6dd6cSJoerg Roedel return max_segment_size; 272e6d6dd6cSJoerg Roedel } 273e6d6dd6cSJoerg Roedel EXPORT_SYMBOL_GPL(virtio_max_dma_size); 274e6d6dd6cSJoerg Roedel 275d79dca75STiwei Bie static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, 276d79dca75STiwei Bie dma_addr_t *dma_handle, gfp_t flag) 277d79dca75STiwei Bie { 278d79dca75STiwei Bie if (vring_use_dma_api(vdev)) { 279d79dca75STiwei Bie return dma_alloc_coherent(vdev->dev.parent, size, 280d79dca75STiwei Bie dma_handle, flag); 281d79dca75STiwei Bie } else { 282d79dca75STiwei Bie void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag); 283d79dca75STiwei Bie 284d79dca75STiwei Bie if (queue) { 285d79dca75STiwei Bie phys_addr_t phys_addr = virt_to_phys(queue); 286d79dca75STiwei Bie *dma_handle = (dma_addr_t)phys_addr; 287d79dca75STiwei Bie 288d79dca75STiwei Bie /* 289d79dca75STiwei Bie * Sanity check: make sure we dind't truncate 290d79dca75STiwei Bie * the address. The only arches I can find that 291d79dca75STiwei Bie * have 64-bit phys_addr_t but 32-bit dma_addr_t 292d79dca75STiwei Bie * are certain non-highmem MIPS and x86 293d79dca75STiwei Bie * configurations, but these configurations 294d79dca75STiwei Bie * should never allocate physical pages above 32 295d79dca75STiwei Bie * bits, so this is fine. Just in case, throw a 296d79dca75STiwei Bie * warning and abort if we end up with an 297d79dca75STiwei Bie * unrepresentable address. 298d79dca75STiwei Bie */ 299d79dca75STiwei Bie if (WARN_ON_ONCE(*dma_handle != phys_addr)) { 300d79dca75STiwei Bie free_pages_exact(queue, PAGE_ALIGN(size)); 301d79dca75STiwei Bie return NULL; 302d79dca75STiwei Bie } 303d79dca75STiwei Bie } 304d79dca75STiwei Bie return queue; 305d79dca75STiwei Bie } 306d79dca75STiwei Bie } 307d79dca75STiwei Bie 308d79dca75STiwei Bie static void vring_free_queue(struct virtio_device *vdev, size_t size, 309d79dca75STiwei Bie void *queue, dma_addr_t dma_handle) 310d79dca75STiwei Bie { 311d79dca75STiwei Bie if (vring_use_dma_api(vdev)) 312d79dca75STiwei Bie dma_free_coherent(vdev->dev.parent, size, queue, dma_handle); 313d79dca75STiwei Bie else 314d79dca75STiwei Bie free_pages_exact(queue, PAGE_ALIGN(size)); 315d79dca75STiwei Bie } 316d79dca75STiwei Bie 317780bc790SAndy Lutomirski /* 318780bc790SAndy Lutomirski * The DMA ops on various arches are rather gnarly right now, and 319780bc790SAndy Lutomirski * making all of the arch DMA ops work on the vring device itself 320780bc790SAndy Lutomirski * is a mess. For now, we use the parent device for DMA ops. 321780bc790SAndy Lutomirski */ 32275bfa81bSMichael S. Tsirkin static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq) 323780bc790SAndy Lutomirski { 324780bc790SAndy Lutomirski return vq->vq.vdev->dev.parent; 325780bc790SAndy Lutomirski } 326780bc790SAndy Lutomirski 327780bc790SAndy Lutomirski /* Map one sg entry. */ 328780bc790SAndy Lutomirski static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq, 329780bc790SAndy Lutomirski struct scatterlist *sg, 330780bc790SAndy Lutomirski enum dma_data_direction direction) 331780bc790SAndy Lutomirski { 332fb3fba6bSTiwei Bie if (!vq->use_dma_api) 333780bc790SAndy Lutomirski return (dma_addr_t)sg_phys(sg); 334780bc790SAndy Lutomirski 335780bc790SAndy Lutomirski /* 336780bc790SAndy Lutomirski * We can't use dma_map_sg, because we don't use scatterlists in 337780bc790SAndy Lutomirski * the way it expects (we don't guarantee that the scatterlist 338780bc790SAndy Lutomirski * will exist for the lifetime of the mapping). 339780bc790SAndy Lutomirski */ 340780bc790SAndy Lutomirski return dma_map_page(vring_dma_dev(vq), 341780bc790SAndy Lutomirski sg_page(sg), sg->offset, sg->length, 342780bc790SAndy Lutomirski direction); 343780bc790SAndy Lutomirski } 344780bc790SAndy Lutomirski 345780bc790SAndy Lutomirski static dma_addr_t vring_map_single(const struct vring_virtqueue *vq, 346780bc790SAndy Lutomirski void *cpu_addr, size_t size, 347780bc790SAndy Lutomirski enum dma_data_direction direction) 348780bc790SAndy Lutomirski { 349fb3fba6bSTiwei Bie if (!vq->use_dma_api) 350780bc790SAndy Lutomirski return (dma_addr_t)virt_to_phys(cpu_addr); 351780bc790SAndy Lutomirski 352780bc790SAndy Lutomirski return dma_map_single(vring_dma_dev(vq), 353780bc790SAndy Lutomirski cpu_addr, size, direction); 354780bc790SAndy Lutomirski } 355780bc790SAndy Lutomirski 356e6f633e5STiwei Bie static int vring_mapping_error(const struct vring_virtqueue *vq, 357e6f633e5STiwei Bie dma_addr_t addr) 358e6f633e5STiwei Bie { 359fb3fba6bSTiwei Bie if (!vq->use_dma_api) 360e6f633e5STiwei Bie return 0; 361e6f633e5STiwei Bie 362e6f633e5STiwei Bie return dma_mapping_error(vring_dma_dev(vq), addr); 363e6f633e5STiwei Bie } 364e6f633e5STiwei Bie 365e6f633e5STiwei Bie 366e6f633e5STiwei Bie /* 367e6f633e5STiwei Bie * Split ring specific functions - *_split(). 368e6f633e5STiwei Bie */ 369e6f633e5STiwei Bie 37072b5e895SJason Wang static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq, 371780bc790SAndy Lutomirski struct vring_desc *desc) 372780bc790SAndy Lutomirski { 373780bc790SAndy Lutomirski u16 flags; 374780bc790SAndy Lutomirski 375fb3fba6bSTiwei Bie if (!vq->use_dma_api) 376780bc790SAndy Lutomirski return; 377780bc790SAndy Lutomirski 378780bc790SAndy Lutomirski flags = virtio16_to_cpu(vq->vq.vdev, desc->flags); 379780bc790SAndy Lutomirski 380780bc790SAndy Lutomirski dma_unmap_page(vring_dma_dev(vq), 381780bc790SAndy Lutomirski virtio64_to_cpu(vq->vq.vdev, desc->addr), 382780bc790SAndy Lutomirski virtio32_to_cpu(vq->vq.vdev, desc->len), 383780bc790SAndy Lutomirski (flags & VRING_DESC_F_WRITE) ? 384780bc790SAndy Lutomirski DMA_FROM_DEVICE : DMA_TO_DEVICE); 385780bc790SAndy Lutomirski } 386780bc790SAndy Lutomirski 38772b5e895SJason Wang static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq, 38872b5e895SJason Wang unsigned int i) 38972b5e895SJason Wang { 39072b5e895SJason Wang struct vring_desc_extra *extra = vq->split.desc_extra; 39172b5e895SJason Wang u16 flags; 39272b5e895SJason Wang 39372b5e895SJason Wang if (!vq->use_dma_api) 39472b5e895SJason Wang goto out; 39572b5e895SJason Wang 39672b5e895SJason Wang flags = extra[i].flags; 39772b5e895SJason Wang 39872b5e895SJason Wang if (flags & VRING_DESC_F_INDIRECT) { 39972b5e895SJason Wang dma_unmap_single(vring_dma_dev(vq), 40072b5e895SJason Wang extra[i].addr, 40172b5e895SJason Wang extra[i].len, 40272b5e895SJason Wang (flags & VRING_DESC_F_WRITE) ? 40372b5e895SJason Wang DMA_FROM_DEVICE : DMA_TO_DEVICE); 40472b5e895SJason Wang } else { 40572b5e895SJason Wang dma_unmap_page(vring_dma_dev(vq), 40672b5e895SJason Wang extra[i].addr, 40772b5e895SJason Wang extra[i].len, 40872b5e895SJason Wang (flags & VRING_DESC_F_WRITE) ? 40972b5e895SJason Wang DMA_FROM_DEVICE : DMA_TO_DEVICE); 41072b5e895SJason Wang } 41172b5e895SJason Wang 41272b5e895SJason Wang out: 41372b5e895SJason Wang return extra[i].next; 41472b5e895SJason Wang } 41572b5e895SJason Wang 416138fd251STiwei Bie static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq, 417138fd251STiwei Bie unsigned int total_sg, 418138fd251STiwei Bie gfp_t gfp) 4199fa29b9dSMark McLoughlin { 4209fa29b9dSMark McLoughlin struct vring_desc *desc; 421b25bd251SRusty Russell unsigned int i; 4229fa29b9dSMark McLoughlin 423b92b1b89SWill Deacon /* 424b92b1b89SWill Deacon * We require lowmem mappings for the descriptors because 425b92b1b89SWill Deacon * otherwise virt_to_phys will give us bogus addresses in the 426b92b1b89SWill Deacon * virtqueue. 427b92b1b89SWill Deacon */ 42882107539SMichal Hocko gfp &= ~__GFP_HIGHMEM; 429b92b1b89SWill Deacon 4306da2ec56SKees Cook desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp); 4319fa29b9dSMark McLoughlin if (!desc) 432b25bd251SRusty Russell return NULL; 4339fa29b9dSMark McLoughlin 434b25bd251SRusty Russell for (i = 0; i < total_sg; i++) 43500e6f3d9SMichael S. Tsirkin desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); 436b25bd251SRusty Russell return desc; 4379fa29b9dSMark McLoughlin } 4389fa29b9dSMark McLoughlin 439fe4c3862SJason Wang static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq, 440fe4c3862SJason Wang struct vring_desc *desc, 441fe4c3862SJason Wang unsigned int i, 442fe4c3862SJason Wang dma_addr_t addr, 443fe4c3862SJason Wang unsigned int len, 44472b5e895SJason Wang u16 flags, 44572b5e895SJason Wang bool indirect) 446fe4c3862SJason Wang { 44772b5e895SJason Wang struct vring_virtqueue *vring = to_vvq(vq); 44872b5e895SJason Wang struct vring_desc_extra *extra = vring->split.desc_extra; 44972b5e895SJason Wang u16 next; 45072b5e895SJason Wang 451fe4c3862SJason Wang desc[i].flags = cpu_to_virtio16(vq->vdev, flags); 452fe4c3862SJason Wang desc[i].addr = cpu_to_virtio64(vq->vdev, addr); 453fe4c3862SJason Wang desc[i].len = cpu_to_virtio32(vq->vdev, len); 454fe4c3862SJason Wang 45572b5e895SJason Wang if (!indirect) { 45672b5e895SJason Wang next = extra[i].next; 45772b5e895SJason Wang desc[i].next = cpu_to_virtio16(vq->vdev, next); 45872b5e895SJason Wang 45972b5e895SJason Wang extra[i].addr = addr; 46072b5e895SJason Wang extra[i].len = len; 46172b5e895SJason Wang extra[i].flags = flags; 46272b5e895SJason Wang } else 46372b5e895SJason Wang next = virtio16_to_cpu(vq->vdev, desc[i].next); 46472b5e895SJason Wang 46572b5e895SJason Wang return next; 466fe4c3862SJason Wang } 467fe4c3862SJason Wang 468138fd251STiwei Bie static inline int virtqueue_add_split(struct virtqueue *_vq, 46913816c76SRusty Russell struct scatterlist *sgs[], 470eeebf9b1SRusty Russell unsigned int total_sg, 47113816c76SRusty Russell unsigned int out_sgs, 47213816c76SRusty Russell unsigned int in_sgs, 473bbd603efSMichael S. Tsirkin void *data, 4745a08b04fSMichael S. Tsirkin void *ctx, 475bbd603efSMichael S. Tsirkin gfp_t gfp) 4760a8a69ddSRusty Russell { 4770a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 47813816c76SRusty Russell struct scatterlist *sg; 479b25bd251SRusty Russell struct vring_desc *desc; 4803f649ab7SKees Cook unsigned int i, n, avail, descs_used, prev, err_idx; 4811fe9b6feSMichael S. Tsirkin int head; 482b25bd251SRusty Russell bool indirect; 4830a8a69ddSRusty Russell 4849fa29b9dSMark McLoughlin START_USE(vq); 4859fa29b9dSMark McLoughlin 4860a8a69ddSRusty Russell BUG_ON(data == NULL); 4875a08b04fSMichael S. Tsirkin BUG_ON(ctx && vq->indirect); 4889fa29b9dSMark McLoughlin 48970670444SRusty Russell if (unlikely(vq->broken)) { 49070670444SRusty Russell END_USE(vq); 49170670444SRusty Russell return -EIO; 49270670444SRusty Russell } 49370670444SRusty Russell 4944d6a105eSTiwei Bie LAST_ADD_TIME_UPDATE(vq); 495e93300b1SRusty Russell 49613816c76SRusty Russell BUG_ON(total_sg == 0); 4970a8a69ddSRusty Russell 498b25bd251SRusty Russell head = vq->free_head; 499b25bd251SRusty Russell 500*35c51e09SXianting Tian if (virtqueue_use_indirect(vq, total_sg)) 501138fd251STiwei Bie desc = alloc_indirect_split(_vq, total_sg, gfp); 50244ed8089SRichard W.M. Jones else { 503b25bd251SRusty Russell desc = NULL; 504e593bf97STiwei Bie WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect); 50544ed8089SRichard W.M. Jones } 506b25bd251SRusty Russell 507b25bd251SRusty Russell if (desc) { 508b25bd251SRusty Russell /* Use a single buffer which doesn't continue */ 509780bc790SAndy Lutomirski indirect = true; 510b25bd251SRusty Russell /* Set up rest to use this indirect table. */ 511b25bd251SRusty Russell i = 0; 512b25bd251SRusty Russell descs_used = 1; 513b25bd251SRusty Russell } else { 514780bc790SAndy Lutomirski indirect = false; 515e593bf97STiwei Bie desc = vq->split.vring.desc; 516b25bd251SRusty Russell i = head; 517b25bd251SRusty Russell descs_used = total_sg; 518b25bd251SRusty Russell } 519b25bd251SRusty Russell 520b25bd251SRusty Russell if (vq->vq.num_free < descs_used) { 5210a8a69ddSRusty Russell pr_debug("Can't add buf len %i - avail = %i\n", 522b25bd251SRusty Russell descs_used, vq->vq.num_free); 52344653eaeSRusty Russell /* FIXME: for historical reasons, we force a notify here if 52444653eaeSRusty Russell * there are outgoing parts to the buffer. Presumably the 52544653eaeSRusty Russell * host should service the ring ASAP. */ 52613816c76SRusty Russell if (out_sgs) 527426e3e0aSRusty Russell vq->notify(&vq->vq); 52858625edfSWei Yongjun if (indirect) 52958625edfSWei Yongjun kfree(desc); 5300a8a69ddSRusty Russell END_USE(vq); 5310a8a69ddSRusty Russell return -ENOSPC; 5320a8a69ddSRusty Russell } 5330a8a69ddSRusty Russell 53413816c76SRusty Russell for (n = 0; n < out_sgs; n++) { 535eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 536780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE); 537780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 538780bc790SAndy Lutomirski goto unmap_release; 539780bc790SAndy Lutomirski 5400a8a69ddSRusty Russell prev = i; 54172b5e895SJason Wang /* Note that we trust indirect descriptor 54272b5e895SJason Wang * table since it use stream DMA mapping. 54372b5e895SJason Wang */ 544fe4c3862SJason Wang i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length, 54572b5e895SJason Wang VRING_DESC_F_NEXT, 54672b5e895SJason Wang indirect); 5470a8a69ddSRusty Russell } 54813816c76SRusty Russell } 54913816c76SRusty Russell for (; n < (out_sgs + in_sgs); n++) { 550eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 551780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE); 552780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 553780bc790SAndy Lutomirski goto unmap_release; 554780bc790SAndy Lutomirski 5550a8a69ddSRusty Russell prev = i; 55672b5e895SJason Wang /* Note that we trust indirect descriptor 55772b5e895SJason Wang * table since it use stream DMA mapping. 55872b5e895SJason Wang */ 559fe4c3862SJason Wang i = virtqueue_add_desc_split(_vq, desc, i, addr, 560fe4c3862SJason Wang sg->length, 561fe4c3862SJason Wang VRING_DESC_F_NEXT | 56272b5e895SJason Wang VRING_DESC_F_WRITE, 56372b5e895SJason Wang indirect); 56413816c76SRusty Russell } 5650a8a69ddSRusty Russell } 5660a8a69ddSRusty Russell /* Last one doesn't continue. */ 56700e6f3d9SMichael S. Tsirkin desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); 56872b5e895SJason Wang if (!indirect && vq->use_dma_api) 569890d3356SVincent Whitchurch vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &= 57072b5e895SJason Wang ~VRING_DESC_F_NEXT; 5710a8a69ddSRusty Russell 572780bc790SAndy Lutomirski if (indirect) { 573780bc790SAndy Lutomirski /* Now that the indirect table is filled in, map it. */ 574780bc790SAndy Lutomirski dma_addr_t addr = vring_map_single( 575780bc790SAndy Lutomirski vq, desc, total_sg * sizeof(struct vring_desc), 576780bc790SAndy Lutomirski DMA_TO_DEVICE); 577780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 578780bc790SAndy Lutomirski goto unmap_release; 579780bc790SAndy Lutomirski 580fe4c3862SJason Wang virtqueue_add_desc_split(_vq, vq->split.vring.desc, 581fe4c3862SJason Wang head, addr, 582fe4c3862SJason Wang total_sg * sizeof(struct vring_desc), 58372b5e895SJason Wang VRING_DESC_F_INDIRECT, 58472b5e895SJason Wang false); 585780bc790SAndy Lutomirski } 586780bc790SAndy Lutomirski 587780bc790SAndy Lutomirski /* We're using some buffers from the free list. */ 588780bc790SAndy Lutomirski vq->vq.num_free -= descs_used; 589780bc790SAndy Lutomirski 5900a8a69ddSRusty Russell /* Update free pointer */ 591b25bd251SRusty Russell if (indirect) 59272b5e895SJason Wang vq->free_head = vq->split.desc_extra[head].next; 593b25bd251SRusty Russell else 5940a8a69ddSRusty Russell vq->free_head = i; 5950a8a69ddSRusty Russell 596780bc790SAndy Lutomirski /* Store token and indirect buffer state. */ 597cbeedb72STiwei Bie vq->split.desc_state[head].data = data; 598780bc790SAndy Lutomirski if (indirect) 599cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc = desc; 60087646a34SJason Wang else 601cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc = ctx; 6020a8a69ddSRusty Russell 6030a8a69ddSRusty Russell /* Put entry in available array (but don't update avail->idx until they 6043b720b8cSRusty Russell * do sync). */ 605e593bf97STiwei Bie avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1); 606e593bf97STiwei Bie vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); 6070a8a69ddSRusty Russell 608ee7cd898SRusty Russell /* Descriptors and available array need to be set before we expose the 609ee7cd898SRusty Russell * new available array entries. */ 610a9a0fef7SRusty Russell virtio_wmb(vq->weak_barriers); 611e593bf97STiwei Bie vq->split.avail_idx_shadow++; 612e593bf97STiwei Bie vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, 613e593bf97STiwei Bie vq->split.avail_idx_shadow); 614ee7cd898SRusty Russell vq->num_added++; 615ee7cd898SRusty Russell 6165e05bf58STetsuo Handa pr_debug("Added buffer head %i to %p\n", head, vq); 6175e05bf58STetsuo Handa END_USE(vq); 6185e05bf58STetsuo Handa 619ee7cd898SRusty Russell /* This is very unlikely, but theoretically possible. Kick 620ee7cd898SRusty Russell * just in case. */ 621ee7cd898SRusty Russell if (unlikely(vq->num_added == (1 << 16) - 1)) 622ee7cd898SRusty Russell virtqueue_kick(_vq); 623ee7cd898SRusty Russell 62498e8c6bcSRusty Russell return 0; 625780bc790SAndy Lutomirski 626780bc790SAndy Lutomirski unmap_release: 627780bc790SAndy Lutomirski err_idx = i; 628cf8f1696SMatthias Lange 629cf8f1696SMatthias Lange if (indirect) 630cf8f1696SMatthias Lange i = 0; 631cf8f1696SMatthias Lange else 632780bc790SAndy Lutomirski i = head; 633780bc790SAndy Lutomirski 634780bc790SAndy Lutomirski for (n = 0; n < total_sg; n++) { 635780bc790SAndy Lutomirski if (i == err_idx) 636780bc790SAndy Lutomirski break; 63772b5e895SJason Wang if (indirect) { 63872b5e895SJason Wang vring_unmap_one_split_indirect(vq, &desc[i]); 639cf8f1696SMatthias Lange i = virtio16_to_cpu(_vq->vdev, desc[i].next); 64072b5e895SJason Wang } else 64172b5e895SJason Wang i = vring_unmap_one_split(vq, i); 642780bc790SAndy Lutomirski } 643780bc790SAndy Lutomirski 644780bc790SAndy Lutomirski if (indirect) 645780bc790SAndy Lutomirski kfree(desc); 646780bc790SAndy Lutomirski 6473cc36f6eSMichael S. Tsirkin END_USE(vq); 648f7728002SHalil Pasic return -ENOMEM; 6490a8a69ddSRusty Russell } 65013816c76SRusty Russell 651138fd251STiwei Bie static bool virtqueue_kick_prepare_split(struct virtqueue *_vq) 6520a8a69ddSRusty Russell { 6530a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 654a5c262c5SMichael S. Tsirkin u16 new, old; 65541f0377fSRusty Russell bool needs_kick; 65641f0377fSRusty Russell 6570a8a69ddSRusty Russell START_USE(vq); 658a72caae2SJason Wang /* We need to expose available array entries before checking avail 659a72caae2SJason Wang * event. */ 660a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 6610a8a69ddSRusty Russell 662e593bf97STiwei Bie old = vq->split.avail_idx_shadow - vq->num_added; 663e593bf97STiwei Bie new = vq->split.avail_idx_shadow; 6640a8a69ddSRusty Russell vq->num_added = 0; 6650a8a69ddSRusty Russell 6664d6a105eSTiwei Bie LAST_ADD_TIME_CHECK(vq); 6674d6a105eSTiwei Bie LAST_ADD_TIME_INVALID(vq); 668e93300b1SRusty Russell 66941f0377fSRusty Russell if (vq->event) { 670e593bf97STiwei Bie needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, 671e593bf97STiwei Bie vring_avail_event(&vq->split.vring)), 67241f0377fSRusty Russell new, old); 67341f0377fSRusty Russell } else { 674e593bf97STiwei Bie needs_kick = !(vq->split.vring.used->flags & 675e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 676e593bf97STiwei Bie VRING_USED_F_NO_NOTIFY)); 67741f0377fSRusty Russell } 6780a8a69ddSRusty Russell END_USE(vq); 67941f0377fSRusty Russell return needs_kick; 68041f0377fSRusty Russell } 681138fd251STiwei Bie 682138fd251STiwei Bie static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, 6835a08b04fSMichael S. Tsirkin void **ctx) 6840a8a69ddSRusty Russell { 685780bc790SAndy Lutomirski unsigned int i, j; 686c60923cbSGonglei __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); 6870a8a69ddSRusty Russell 6880a8a69ddSRusty Russell /* Clear data ptr. */ 689cbeedb72STiwei Bie vq->split.desc_state[head].data = NULL; 6900a8a69ddSRusty Russell 691780bc790SAndy Lutomirski /* Put back on free list: unmap first-level descriptors and find end */ 6920a8a69ddSRusty Russell i = head; 6939fa29b9dSMark McLoughlin 694e593bf97STiwei Bie while (vq->split.vring.desc[i].flags & nextflag) { 69572b5e895SJason Wang vring_unmap_one_split(vq, i); 69672b5e895SJason Wang i = vq->split.desc_extra[i].next; 69706ca287dSRusty Russell vq->vq.num_free++; 6980a8a69ddSRusty Russell } 6990a8a69ddSRusty Russell 70072b5e895SJason Wang vring_unmap_one_split(vq, i); 70172b5e895SJason Wang vq->split.desc_extra[i].next = vq->free_head; 7020a8a69ddSRusty Russell vq->free_head = head; 703780bc790SAndy Lutomirski 7040a8a69ddSRusty Russell /* Plus final descriptor */ 70506ca287dSRusty Russell vq->vq.num_free++; 706780bc790SAndy Lutomirski 7075a08b04fSMichael S. Tsirkin if (vq->indirect) { 708cbeedb72STiwei Bie struct vring_desc *indir_desc = 709cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc; 7105a08b04fSMichael S. Tsirkin u32 len; 7115a08b04fSMichael S. Tsirkin 7125a08b04fSMichael S. Tsirkin /* Free the indirect table, if any, now that it's unmapped. */ 7135a08b04fSMichael S. Tsirkin if (!indir_desc) 7145a08b04fSMichael S. Tsirkin return; 7155a08b04fSMichael S. Tsirkin 71672b5e895SJason Wang len = vq->split.desc_extra[head].len; 717780bc790SAndy Lutomirski 71872b5e895SJason Wang BUG_ON(!(vq->split.desc_extra[head].flags & 71972b5e895SJason Wang VRING_DESC_F_INDIRECT)); 720780bc790SAndy Lutomirski BUG_ON(len == 0 || len % sizeof(struct vring_desc)); 721780bc790SAndy Lutomirski 722780bc790SAndy Lutomirski for (j = 0; j < len / sizeof(struct vring_desc); j++) 72372b5e895SJason Wang vring_unmap_one_split_indirect(vq, &indir_desc[j]); 724780bc790SAndy Lutomirski 7255a08b04fSMichael S. Tsirkin kfree(indir_desc); 726cbeedb72STiwei Bie vq->split.desc_state[head].indir_desc = NULL; 7275a08b04fSMichael S. Tsirkin } else if (ctx) { 728cbeedb72STiwei Bie *ctx = vq->split.desc_state[head].indir_desc; 729780bc790SAndy Lutomirski } 7300a8a69ddSRusty Russell } 7310a8a69ddSRusty Russell 732138fd251STiwei Bie static inline bool more_used_split(const struct vring_virtqueue *vq) 7330a8a69ddSRusty Russell { 734e593bf97STiwei Bie return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, 735e593bf97STiwei Bie vq->split.vring.used->idx); 7360a8a69ddSRusty Russell } 7370a8a69ddSRusty Russell 738138fd251STiwei Bie static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, 739138fd251STiwei Bie unsigned int *len, 7405a08b04fSMichael S. Tsirkin void **ctx) 7410a8a69ddSRusty Russell { 7420a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 7430a8a69ddSRusty Russell void *ret; 7440a8a69ddSRusty Russell unsigned int i; 7453b720b8cSRusty Russell u16 last_used; 7460a8a69ddSRusty Russell 7470a8a69ddSRusty Russell START_USE(vq); 7480a8a69ddSRusty Russell 7495ef82752SRusty Russell if (unlikely(vq->broken)) { 7505ef82752SRusty Russell END_USE(vq); 7515ef82752SRusty Russell return NULL; 7525ef82752SRusty Russell } 7535ef82752SRusty Russell 754138fd251STiwei Bie if (!more_used_split(vq)) { 7550a8a69ddSRusty Russell pr_debug("No more buffers in queue\n"); 7560a8a69ddSRusty Russell END_USE(vq); 7570a8a69ddSRusty Russell return NULL; 7580a8a69ddSRusty Russell } 7590a8a69ddSRusty Russell 7602d61ba95SMichael S. Tsirkin /* Only get used array entries after they have been exposed by host. */ 761a9a0fef7SRusty Russell virtio_rmb(vq->weak_barriers); 7622d61ba95SMichael S. Tsirkin 763e593bf97STiwei Bie last_used = (vq->last_used_idx & (vq->split.vring.num - 1)); 764e593bf97STiwei Bie i = virtio32_to_cpu(_vq->vdev, 765e593bf97STiwei Bie vq->split.vring.used->ring[last_used].id); 766e593bf97STiwei Bie *len = virtio32_to_cpu(_vq->vdev, 767e593bf97STiwei Bie vq->split.vring.used->ring[last_used].len); 7680a8a69ddSRusty Russell 769e593bf97STiwei Bie if (unlikely(i >= vq->split.vring.num)) { 7700a8a69ddSRusty Russell BAD_RING(vq, "id %u out of range\n", i); 7710a8a69ddSRusty Russell return NULL; 7720a8a69ddSRusty Russell } 773cbeedb72STiwei Bie if (unlikely(!vq->split.desc_state[i].data)) { 7740a8a69ddSRusty Russell BAD_RING(vq, "id %u is not a head!\n", i); 7750a8a69ddSRusty Russell return NULL; 7760a8a69ddSRusty Russell } 7770a8a69ddSRusty Russell 778138fd251STiwei Bie /* detach_buf_split clears data, so grab it now. */ 779cbeedb72STiwei Bie ret = vq->split.desc_state[i].data; 780138fd251STiwei Bie detach_buf_split(vq, i, ctx); 7810a8a69ddSRusty Russell vq->last_used_idx++; 782a5c262c5SMichael S. Tsirkin /* If we expect an interrupt for the next entry, tell host 783a5c262c5SMichael S. Tsirkin * by writing event index and flush out the write before 784a5c262c5SMichael S. Tsirkin * the read in the next get_buf call. */ 785e593bf97STiwei Bie if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) 786788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 787e593bf97STiwei Bie &vring_used_event(&vq->split.vring), 788788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); 789a5c262c5SMichael S. Tsirkin 7904d6a105eSTiwei Bie LAST_ADD_TIME_INVALID(vq); 791e93300b1SRusty Russell 7920a8a69ddSRusty Russell END_USE(vq); 7930a8a69ddSRusty Russell return ret; 7940a8a69ddSRusty Russell } 795138fd251STiwei Bie 796138fd251STiwei Bie static void virtqueue_disable_cb_split(struct virtqueue *_vq) 797138fd251STiwei Bie { 798138fd251STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 799138fd251STiwei Bie 800e593bf97STiwei Bie if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { 801e593bf97STiwei Bie vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 8028d622d21SMichael S. Tsirkin if (vq->event) 8038d622d21SMichael S. Tsirkin /* TODO: this is a hack. Figure out a cleaner value to write. */ 8048d622d21SMichael S. Tsirkin vring_used_event(&vq->split.vring) = 0x0; 8058d622d21SMichael S. Tsirkin else 806e593bf97STiwei Bie vq->split.vring.avail->flags = 807e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 808e593bf97STiwei Bie vq->split.avail_flags_shadow); 809138fd251STiwei Bie } 810138fd251STiwei Bie } 811138fd251STiwei Bie 812138fd251STiwei Bie static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq) 813cc229884SMichael S. Tsirkin { 814cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 815cc229884SMichael S. Tsirkin u16 last_used_idx; 816cc229884SMichael S. Tsirkin 817cc229884SMichael S. Tsirkin START_USE(vq); 818cc229884SMichael S. Tsirkin 819cc229884SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 820cc229884SMichael S. Tsirkin * more to do. */ 821cc229884SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to 822cc229884SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 823cc229884SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 824e593bf97STiwei Bie if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 825e593bf97STiwei Bie vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 8260ea1e4a6SLadi Prosek if (!vq->event) 827e593bf97STiwei Bie vq->split.vring.avail->flags = 828e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 829e593bf97STiwei Bie vq->split.avail_flags_shadow); 830f277ec42SVenkatesh Srinivas } 831e593bf97STiwei Bie vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev, 832e593bf97STiwei Bie last_used_idx = vq->last_used_idx); 833cc229884SMichael S. Tsirkin END_USE(vq); 834cc229884SMichael S. Tsirkin return last_used_idx; 835cc229884SMichael S. Tsirkin } 836138fd251STiwei Bie 837138fd251STiwei Bie static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx) 838138fd251STiwei Bie { 839138fd251STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 840138fd251STiwei Bie 841138fd251STiwei Bie return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, 842e593bf97STiwei Bie vq->split.vring.used->idx); 843138fd251STiwei Bie } 844138fd251STiwei Bie 845138fd251STiwei Bie static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq) 8467ab358c2SMichael S. Tsirkin { 8477ab358c2SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 8487ab358c2SMichael S. Tsirkin u16 bufs; 8497ab358c2SMichael S. Tsirkin 8507ab358c2SMichael S. Tsirkin START_USE(vq); 8517ab358c2SMichael S. Tsirkin 8527ab358c2SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 8537ab358c2SMichael S. Tsirkin * more to do. */ 8547ab358c2SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to 8557ab358c2SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 8560ea1e4a6SLadi Prosek * entry. Always update the event index to keep code simple. */ 857e593bf97STiwei Bie if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 858e593bf97STiwei Bie vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 8590ea1e4a6SLadi Prosek if (!vq->event) 860e593bf97STiwei Bie vq->split.vring.avail->flags = 861e593bf97STiwei Bie cpu_to_virtio16(_vq->vdev, 862e593bf97STiwei Bie vq->split.avail_flags_shadow); 863f277ec42SVenkatesh Srinivas } 8647ab358c2SMichael S. Tsirkin /* TODO: tune this threshold */ 865e593bf97STiwei Bie bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4; 866788e5b3aSMichael S. Tsirkin 867788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 868e593bf97STiwei Bie &vring_used_event(&vq->split.vring), 869788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); 870788e5b3aSMichael S. Tsirkin 871e593bf97STiwei Bie if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx) 872e593bf97STiwei Bie - vq->last_used_idx) > bufs)) { 8737ab358c2SMichael S. Tsirkin END_USE(vq); 8747ab358c2SMichael S. Tsirkin return false; 8757ab358c2SMichael S. Tsirkin } 8767ab358c2SMichael S. Tsirkin 8777ab358c2SMichael S. Tsirkin END_USE(vq); 8787ab358c2SMichael S. Tsirkin return true; 8797ab358c2SMichael S. Tsirkin } 8807ab358c2SMichael S. Tsirkin 881138fd251STiwei Bie static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq) 882c021eac4SShirley Ma { 883c021eac4SShirley Ma struct vring_virtqueue *vq = to_vvq(_vq); 884c021eac4SShirley Ma unsigned int i; 885c021eac4SShirley Ma void *buf; 886c021eac4SShirley Ma 887c021eac4SShirley Ma START_USE(vq); 888c021eac4SShirley Ma 889e593bf97STiwei Bie for (i = 0; i < vq->split.vring.num; i++) { 890cbeedb72STiwei Bie if (!vq->split.desc_state[i].data) 891c021eac4SShirley Ma continue; 892138fd251STiwei Bie /* detach_buf_split clears data, so grab it now. */ 893cbeedb72STiwei Bie buf = vq->split.desc_state[i].data; 894138fd251STiwei Bie detach_buf_split(vq, i, NULL); 895e593bf97STiwei Bie vq->split.avail_idx_shadow--; 896e593bf97STiwei Bie vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev, 897e593bf97STiwei Bie vq->split.avail_idx_shadow); 898c021eac4SShirley Ma END_USE(vq); 899c021eac4SShirley Ma return buf; 900c021eac4SShirley Ma } 901c021eac4SShirley Ma /* That should have freed everything. */ 902e593bf97STiwei Bie BUG_ON(vq->vq.num_free != vq->split.vring.num); 903c021eac4SShirley Ma 904c021eac4SShirley Ma END_USE(vq); 905c021eac4SShirley Ma return NULL; 906c021eac4SShirley Ma } 907138fd251STiwei Bie 908d79dca75STiwei Bie static struct virtqueue *vring_create_virtqueue_split( 909d79dca75STiwei Bie unsigned int index, 910d79dca75STiwei Bie unsigned int num, 911d79dca75STiwei Bie unsigned int vring_align, 912d79dca75STiwei Bie struct virtio_device *vdev, 913d79dca75STiwei Bie bool weak_barriers, 914d79dca75STiwei Bie bool may_reduce_num, 915d79dca75STiwei Bie bool context, 916d79dca75STiwei Bie bool (*notify)(struct virtqueue *), 917d79dca75STiwei Bie void (*callback)(struct virtqueue *), 918d79dca75STiwei Bie const char *name) 919d79dca75STiwei Bie { 920d79dca75STiwei Bie struct virtqueue *vq; 921d79dca75STiwei Bie void *queue = NULL; 922d79dca75STiwei Bie dma_addr_t dma_addr; 923d79dca75STiwei Bie size_t queue_size_in_bytes; 924d79dca75STiwei Bie struct vring vring; 925d79dca75STiwei Bie 926d79dca75STiwei Bie /* We assume num is a power of 2. */ 927d79dca75STiwei Bie if (num & (num - 1)) { 928d79dca75STiwei Bie dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); 929d79dca75STiwei Bie return NULL; 930d79dca75STiwei Bie } 931d79dca75STiwei Bie 932d79dca75STiwei Bie /* TODO: allocate each queue chunk individually */ 933d79dca75STiwei Bie for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { 934d79dca75STiwei Bie queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 935d79dca75STiwei Bie &dma_addr, 936d79dca75STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 937d79dca75STiwei Bie if (queue) 938d79dca75STiwei Bie break; 939cf94db21SCornelia Huck if (!may_reduce_num) 940cf94db21SCornelia Huck return NULL; 941d79dca75STiwei Bie } 942d79dca75STiwei Bie 943d79dca75STiwei Bie if (!num) 944d79dca75STiwei Bie return NULL; 945d79dca75STiwei Bie 946d79dca75STiwei Bie if (!queue) { 947d79dca75STiwei Bie /* Try to get a single page. You are my only hope! */ 948d79dca75STiwei Bie queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 949d79dca75STiwei Bie &dma_addr, GFP_KERNEL|__GFP_ZERO); 950d79dca75STiwei Bie } 951d79dca75STiwei Bie if (!queue) 952d79dca75STiwei Bie return NULL; 953d79dca75STiwei Bie 954d79dca75STiwei Bie queue_size_in_bytes = vring_size(num, vring_align); 955d79dca75STiwei Bie vring_init(&vring, num, queue, vring_align); 956d79dca75STiwei Bie 957d79dca75STiwei Bie vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, 958d79dca75STiwei Bie notify, callback, name); 959d79dca75STiwei Bie if (!vq) { 960d79dca75STiwei Bie vring_free_queue(vdev, queue_size_in_bytes, queue, 961d79dca75STiwei Bie dma_addr); 962d79dca75STiwei Bie return NULL; 963d79dca75STiwei Bie } 964d79dca75STiwei Bie 965d79dca75STiwei Bie to_vvq(vq)->split.queue_dma_addr = dma_addr; 966d79dca75STiwei Bie to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes; 967d79dca75STiwei Bie to_vvq(vq)->we_own_ring = true; 968d79dca75STiwei Bie 969d79dca75STiwei Bie return vq; 970d79dca75STiwei Bie } 971d79dca75STiwei Bie 972e6f633e5STiwei Bie 973e6f633e5STiwei Bie /* 9741ce9e605STiwei Bie * Packed ring specific functions - *_packed(). 9751ce9e605STiwei Bie */ 9761ce9e605STiwei Bie 977d80dc15bSXuan Zhuo static void vring_unmap_extra_packed(const struct vring_virtqueue *vq, 978d80dc15bSXuan Zhuo struct vring_desc_extra *extra) 9791ce9e605STiwei Bie { 9801ce9e605STiwei Bie u16 flags; 9811ce9e605STiwei Bie 9821ce9e605STiwei Bie if (!vq->use_dma_api) 9831ce9e605STiwei Bie return; 9841ce9e605STiwei Bie 985d80dc15bSXuan Zhuo flags = extra->flags; 9861ce9e605STiwei Bie 9871ce9e605STiwei Bie if (flags & VRING_DESC_F_INDIRECT) { 9881ce9e605STiwei Bie dma_unmap_single(vring_dma_dev(vq), 989d80dc15bSXuan Zhuo extra->addr, extra->len, 9901ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 9911ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 9921ce9e605STiwei Bie } else { 9931ce9e605STiwei Bie dma_unmap_page(vring_dma_dev(vq), 994d80dc15bSXuan Zhuo extra->addr, extra->len, 9951ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 9961ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 9971ce9e605STiwei Bie } 9981ce9e605STiwei Bie } 9991ce9e605STiwei Bie 10001ce9e605STiwei Bie static void vring_unmap_desc_packed(const struct vring_virtqueue *vq, 10011ce9e605STiwei Bie struct vring_packed_desc *desc) 10021ce9e605STiwei Bie { 10031ce9e605STiwei Bie u16 flags; 10041ce9e605STiwei Bie 10051ce9e605STiwei Bie if (!vq->use_dma_api) 10061ce9e605STiwei Bie return; 10071ce9e605STiwei Bie 10081ce9e605STiwei Bie flags = le16_to_cpu(desc->flags); 10091ce9e605STiwei Bie 10101ce9e605STiwei Bie dma_unmap_page(vring_dma_dev(vq), 10111ce9e605STiwei Bie le64_to_cpu(desc->addr), 10121ce9e605STiwei Bie le32_to_cpu(desc->len), 10131ce9e605STiwei Bie (flags & VRING_DESC_F_WRITE) ? 10141ce9e605STiwei Bie DMA_FROM_DEVICE : DMA_TO_DEVICE); 10151ce9e605STiwei Bie } 10161ce9e605STiwei Bie 10171ce9e605STiwei Bie static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg, 10181ce9e605STiwei Bie gfp_t gfp) 10191ce9e605STiwei Bie { 10201ce9e605STiwei Bie struct vring_packed_desc *desc; 10211ce9e605STiwei Bie 10221ce9e605STiwei Bie /* 10231ce9e605STiwei Bie * We require lowmem mappings for the descriptors because 10241ce9e605STiwei Bie * otherwise virt_to_phys will give us bogus addresses in the 10251ce9e605STiwei Bie * virtqueue. 10261ce9e605STiwei Bie */ 10271ce9e605STiwei Bie gfp &= ~__GFP_HIGHMEM; 10281ce9e605STiwei Bie 10291ce9e605STiwei Bie desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp); 10301ce9e605STiwei Bie 10311ce9e605STiwei Bie return desc; 10321ce9e605STiwei Bie } 10331ce9e605STiwei Bie 10341ce9e605STiwei Bie static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, 10351ce9e605STiwei Bie struct scatterlist *sgs[], 10361ce9e605STiwei Bie unsigned int total_sg, 10371ce9e605STiwei Bie unsigned int out_sgs, 10381ce9e605STiwei Bie unsigned int in_sgs, 10391ce9e605STiwei Bie void *data, 10401ce9e605STiwei Bie gfp_t gfp) 10411ce9e605STiwei Bie { 10421ce9e605STiwei Bie struct vring_packed_desc *desc; 10431ce9e605STiwei Bie struct scatterlist *sg; 10441ce9e605STiwei Bie unsigned int i, n, err_idx; 10451ce9e605STiwei Bie u16 head, id; 10461ce9e605STiwei Bie dma_addr_t addr; 10471ce9e605STiwei Bie 10481ce9e605STiwei Bie head = vq->packed.next_avail_idx; 10491ce9e605STiwei Bie desc = alloc_indirect_packed(total_sg, gfp); 1050fc6d70f4SXuan Zhuo if (!desc) 1051fc6d70f4SXuan Zhuo return -ENOMEM; 10521ce9e605STiwei Bie 10531ce9e605STiwei Bie if (unlikely(vq->vq.num_free < 1)) { 10541ce9e605STiwei Bie pr_debug("Can't add buf len 1 - avail = 0\n"); 1055df0bfe75SYueHaibing kfree(desc); 10561ce9e605STiwei Bie END_USE(vq); 10571ce9e605STiwei Bie return -ENOSPC; 10581ce9e605STiwei Bie } 10591ce9e605STiwei Bie 10601ce9e605STiwei Bie i = 0; 10611ce9e605STiwei Bie id = vq->free_head; 10621ce9e605STiwei Bie BUG_ON(id == vq->packed.vring.num); 10631ce9e605STiwei Bie 10641ce9e605STiwei Bie for (n = 0; n < out_sgs + in_sgs; n++) { 10651ce9e605STiwei Bie for (sg = sgs[n]; sg; sg = sg_next(sg)) { 10661ce9e605STiwei Bie addr = vring_map_one_sg(vq, sg, n < out_sgs ? 10671ce9e605STiwei Bie DMA_TO_DEVICE : DMA_FROM_DEVICE); 10681ce9e605STiwei Bie if (vring_mapping_error(vq, addr)) 10691ce9e605STiwei Bie goto unmap_release; 10701ce9e605STiwei Bie 10711ce9e605STiwei Bie desc[i].flags = cpu_to_le16(n < out_sgs ? 10721ce9e605STiwei Bie 0 : VRING_DESC_F_WRITE); 10731ce9e605STiwei Bie desc[i].addr = cpu_to_le64(addr); 10741ce9e605STiwei Bie desc[i].len = cpu_to_le32(sg->length); 10751ce9e605STiwei Bie i++; 10761ce9e605STiwei Bie } 10771ce9e605STiwei Bie } 10781ce9e605STiwei Bie 10791ce9e605STiwei Bie /* Now that the indirect table is filled in, map it. */ 10801ce9e605STiwei Bie addr = vring_map_single(vq, desc, 10811ce9e605STiwei Bie total_sg * sizeof(struct vring_packed_desc), 10821ce9e605STiwei Bie DMA_TO_DEVICE); 10831ce9e605STiwei Bie if (vring_mapping_error(vq, addr)) 10841ce9e605STiwei Bie goto unmap_release; 10851ce9e605STiwei Bie 10861ce9e605STiwei Bie vq->packed.vring.desc[head].addr = cpu_to_le64(addr); 10871ce9e605STiwei Bie vq->packed.vring.desc[head].len = cpu_to_le32(total_sg * 10881ce9e605STiwei Bie sizeof(struct vring_packed_desc)); 10891ce9e605STiwei Bie vq->packed.vring.desc[head].id = cpu_to_le16(id); 10901ce9e605STiwei Bie 10911ce9e605STiwei Bie if (vq->use_dma_api) { 10921ce9e605STiwei Bie vq->packed.desc_extra[id].addr = addr; 10931ce9e605STiwei Bie vq->packed.desc_extra[id].len = total_sg * 10941ce9e605STiwei Bie sizeof(struct vring_packed_desc); 10951ce9e605STiwei Bie vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT | 10961ce9e605STiwei Bie vq->packed.avail_used_flags; 10971ce9e605STiwei Bie } 10981ce9e605STiwei Bie 10991ce9e605STiwei Bie /* 11001ce9e605STiwei Bie * A driver MUST NOT make the first descriptor in the list 11011ce9e605STiwei Bie * available before all subsequent descriptors comprising 11021ce9e605STiwei Bie * the list are made available. 11031ce9e605STiwei Bie */ 11041ce9e605STiwei Bie virtio_wmb(vq->weak_barriers); 11051ce9e605STiwei Bie vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT | 11061ce9e605STiwei Bie vq->packed.avail_used_flags); 11071ce9e605STiwei Bie 11081ce9e605STiwei Bie /* We're using some buffers from the free list. */ 11091ce9e605STiwei Bie vq->vq.num_free -= 1; 11101ce9e605STiwei Bie 11111ce9e605STiwei Bie /* Update free pointer */ 11121ce9e605STiwei Bie n = head + 1; 11131ce9e605STiwei Bie if (n >= vq->packed.vring.num) { 11141ce9e605STiwei Bie n = 0; 11151ce9e605STiwei Bie vq->packed.avail_wrap_counter ^= 1; 11161ce9e605STiwei Bie vq->packed.avail_used_flags ^= 11171ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_AVAIL | 11181ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_USED; 11191ce9e605STiwei Bie } 11201ce9e605STiwei Bie vq->packed.next_avail_idx = n; 1121aeef9b47SJason Wang vq->free_head = vq->packed.desc_extra[id].next; 11221ce9e605STiwei Bie 11231ce9e605STiwei Bie /* Store token and indirect buffer state. */ 11241ce9e605STiwei Bie vq->packed.desc_state[id].num = 1; 11251ce9e605STiwei Bie vq->packed.desc_state[id].data = data; 11261ce9e605STiwei Bie vq->packed.desc_state[id].indir_desc = desc; 11271ce9e605STiwei Bie vq->packed.desc_state[id].last = id; 11281ce9e605STiwei Bie 11291ce9e605STiwei Bie vq->num_added += 1; 11301ce9e605STiwei Bie 11311ce9e605STiwei Bie pr_debug("Added buffer head %i to %p\n", head, vq); 11321ce9e605STiwei Bie END_USE(vq); 11331ce9e605STiwei Bie 11341ce9e605STiwei Bie return 0; 11351ce9e605STiwei Bie 11361ce9e605STiwei Bie unmap_release: 11371ce9e605STiwei Bie err_idx = i; 11381ce9e605STiwei Bie 11391ce9e605STiwei Bie for (i = 0; i < err_idx; i++) 11401ce9e605STiwei Bie vring_unmap_desc_packed(vq, &desc[i]); 11411ce9e605STiwei Bie 11421ce9e605STiwei Bie kfree(desc); 11431ce9e605STiwei Bie 11441ce9e605STiwei Bie END_USE(vq); 1145f7728002SHalil Pasic return -ENOMEM; 11461ce9e605STiwei Bie } 11471ce9e605STiwei Bie 11481ce9e605STiwei Bie static inline int virtqueue_add_packed(struct virtqueue *_vq, 11491ce9e605STiwei Bie struct scatterlist *sgs[], 11501ce9e605STiwei Bie unsigned int total_sg, 11511ce9e605STiwei Bie unsigned int out_sgs, 11521ce9e605STiwei Bie unsigned int in_sgs, 11531ce9e605STiwei Bie void *data, 11541ce9e605STiwei Bie void *ctx, 11551ce9e605STiwei Bie gfp_t gfp) 11561ce9e605STiwei Bie { 11571ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 11581ce9e605STiwei Bie struct vring_packed_desc *desc; 11591ce9e605STiwei Bie struct scatterlist *sg; 11601ce9e605STiwei Bie unsigned int i, n, c, descs_used, err_idx; 11613f649ab7SKees Cook __le16 head_flags, flags; 11623f649ab7SKees Cook u16 head, id, prev, curr, avail_used_flags; 1163fc6d70f4SXuan Zhuo int err; 11641ce9e605STiwei Bie 11651ce9e605STiwei Bie START_USE(vq); 11661ce9e605STiwei Bie 11671ce9e605STiwei Bie BUG_ON(data == NULL); 11681ce9e605STiwei Bie BUG_ON(ctx && vq->indirect); 11691ce9e605STiwei Bie 11701ce9e605STiwei Bie if (unlikely(vq->broken)) { 11711ce9e605STiwei Bie END_USE(vq); 11721ce9e605STiwei Bie return -EIO; 11731ce9e605STiwei Bie } 11741ce9e605STiwei Bie 11751ce9e605STiwei Bie LAST_ADD_TIME_UPDATE(vq); 11761ce9e605STiwei Bie 11771ce9e605STiwei Bie BUG_ON(total_sg == 0); 11781ce9e605STiwei Bie 1179*35c51e09SXianting Tian if (virtqueue_use_indirect(vq, total_sg)) { 1180fc6d70f4SXuan Zhuo err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs, 1181fc6d70f4SXuan Zhuo in_sgs, data, gfp); 11821861ba62SMichael S. Tsirkin if (err != -ENOMEM) { 11831861ba62SMichael S. Tsirkin END_USE(vq); 1184fc6d70f4SXuan Zhuo return err; 11851861ba62SMichael S. Tsirkin } 1186fc6d70f4SXuan Zhuo 1187fc6d70f4SXuan Zhuo /* fall back on direct */ 1188fc6d70f4SXuan Zhuo } 11891ce9e605STiwei Bie 11901ce9e605STiwei Bie head = vq->packed.next_avail_idx; 11911ce9e605STiwei Bie avail_used_flags = vq->packed.avail_used_flags; 11921ce9e605STiwei Bie 11931ce9e605STiwei Bie WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect); 11941ce9e605STiwei Bie 11951ce9e605STiwei Bie desc = vq->packed.vring.desc; 11961ce9e605STiwei Bie i = head; 11971ce9e605STiwei Bie descs_used = total_sg; 11981ce9e605STiwei Bie 11991ce9e605STiwei Bie if (unlikely(vq->vq.num_free < descs_used)) { 12001ce9e605STiwei Bie pr_debug("Can't add buf len %i - avail = %i\n", 12011ce9e605STiwei Bie descs_used, vq->vq.num_free); 12021ce9e605STiwei Bie END_USE(vq); 12031ce9e605STiwei Bie return -ENOSPC; 12041ce9e605STiwei Bie } 12051ce9e605STiwei Bie 12061ce9e605STiwei Bie id = vq->free_head; 12071ce9e605STiwei Bie BUG_ON(id == vq->packed.vring.num); 12081ce9e605STiwei Bie 12091ce9e605STiwei Bie curr = id; 12101ce9e605STiwei Bie c = 0; 12111ce9e605STiwei Bie for (n = 0; n < out_sgs + in_sgs; n++) { 12121ce9e605STiwei Bie for (sg = sgs[n]; sg; sg = sg_next(sg)) { 12131ce9e605STiwei Bie dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ? 12141ce9e605STiwei Bie DMA_TO_DEVICE : DMA_FROM_DEVICE); 12151ce9e605STiwei Bie if (vring_mapping_error(vq, addr)) 12161ce9e605STiwei Bie goto unmap_release; 12171ce9e605STiwei Bie 12181ce9e605STiwei Bie flags = cpu_to_le16(vq->packed.avail_used_flags | 12191ce9e605STiwei Bie (++c == total_sg ? 0 : VRING_DESC_F_NEXT) | 12201ce9e605STiwei Bie (n < out_sgs ? 0 : VRING_DESC_F_WRITE)); 12211ce9e605STiwei Bie if (i == head) 12221ce9e605STiwei Bie head_flags = flags; 12231ce9e605STiwei Bie else 12241ce9e605STiwei Bie desc[i].flags = flags; 12251ce9e605STiwei Bie 12261ce9e605STiwei Bie desc[i].addr = cpu_to_le64(addr); 12271ce9e605STiwei Bie desc[i].len = cpu_to_le32(sg->length); 12281ce9e605STiwei Bie desc[i].id = cpu_to_le16(id); 12291ce9e605STiwei Bie 12301ce9e605STiwei Bie if (unlikely(vq->use_dma_api)) { 12311ce9e605STiwei Bie vq->packed.desc_extra[curr].addr = addr; 12321ce9e605STiwei Bie vq->packed.desc_extra[curr].len = sg->length; 12331ce9e605STiwei Bie vq->packed.desc_extra[curr].flags = 12341ce9e605STiwei Bie le16_to_cpu(flags); 12351ce9e605STiwei Bie } 12361ce9e605STiwei Bie prev = curr; 1237aeef9b47SJason Wang curr = vq->packed.desc_extra[curr].next; 12381ce9e605STiwei Bie 12391ce9e605STiwei Bie if ((unlikely(++i >= vq->packed.vring.num))) { 12401ce9e605STiwei Bie i = 0; 12411ce9e605STiwei Bie vq->packed.avail_used_flags ^= 12421ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_AVAIL | 12431ce9e605STiwei Bie 1 << VRING_PACKED_DESC_F_USED; 12441ce9e605STiwei Bie } 12451ce9e605STiwei Bie } 12461ce9e605STiwei Bie } 12471ce9e605STiwei Bie 12481ce9e605STiwei Bie if (i < head) 12491ce9e605STiwei Bie vq->packed.avail_wrap_counter ^= 1; 12501ce9e605STiwei Bie 12511ce9e605STiwei Bie /* We're using some buffers from the free list. */ 12521ce9e605STiwei Bie vq->vq.num_free -= descs_used; 12531ce9e605STiwei Bie 12541ce9e605STiwei Bie /* Update free pointer */ 12551ce9e605STiwei Bie vq->packed.next_avail_idx = i; 12561ce9e605STiwei Bie vq->free_head = curr; 12571ce9e605STiwei Bie 12581ce9e605STiwei Bie /* Store token. */ 12591ce9e605STiwei Bie vq->packed.desc_state[id].num = descs_used; 12601ce9e605STiwei Bie vq->packed.desc_state[id].data = data; 12611ce9e605STiwei Bie vq->packed.desc_state[id].indir_desc = ctx; 12621ce9e605STiwei Bie vq->packed.desc_state[id].last = prev; 12631ce9e605STiwei Bie 12641ce9e605STiwei Bie /* 12651ce9e605STiwei Bie * A driver MUST NOT make the first descriptor in the list 12661ce9e605STiwei Bie * available before all subsequent descriptors comprising 12671ce9e605STiwei Bie * the list are made available. 12681ce9e605STiwei Bie */ 12691ce9e605STiwei Bie virtio_wmb(vq->weak_barriers); 12701ce9e605STiwei Bie vq->packed.vring.desc[head].flags = head_flags; 12711ce9e605STiwei Bie vq->num_added += descs_used; 12721ce9e605STiwei Bie 12731ce9e605STiwei Bie pr_debug("Added buffer head %i to %p\n", head, vq); 12741ce9e605STiwei Bie END_USE(vq); 12751ce9e605STiwei Bie 12761ce9e605STiwei Bie return 0; 12771ce9e605STiwei Bie 12781ce9e605STiwei Bie unmap_release: 12791ce9e605STiwei Bie err_idx = i; 12801ce9e605STiwei Bie i = head; 128144593865SJason Wang curr = vq->free_head; 12821ce9e605STiwei Bie 12831ce9e605STiwei Bie vq->packed.avail_used_flags = avail_used_flags; 12841ce9e605STiwei Bie 12851ce9e605STiwei Bie for (n = 0; n < total_sg; n++) { 12861ce9e605STiwei Bie if (i == err_idx) 12871ce9e605STiwei Bie break; 1288d80dc15bSXuan Zhuo vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]); 128944593865SJason Wang curr = vq->packed.desc_extra[curr].next; 12901ce9e605STiwei Bie i++; 12911ce9e605STiwei Bie if (i >= vq->packed.vring.num) 12921ce9e605STiwei Bie i = 0; 12931ce9e605STiwei Bie } 12941ce9e605STiwei Bie 12951ce9e605STiwei Bie END_USE(vq); 12961ce9e605STiwei Bie return -EIO; 12971ce9e605STiwei Bie } 12981ce9e605STiwei Bie 12991ce9e605STiwei Bie static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq) 13001ce9e605STiwei Bie { 13011ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 1302f51f9826STiwei Bie u16 new, old, off_wrap, flags, wrap_counter, event_idx; 13031ce9e605STiwei Bie bool needs_kick; 13041ce9e605STiwei Bie union { 13051ce9e605STiwei Bie struct { 13061ce9e605STiwei Bie __le16 off_wrap; 13071ce9e605STiwei Bie __le16 flags; 13081ce9e605STiwei Bie }; 13091ce9e605STiwei Bie u32 u32; 13101ce9e605STiwei Bie } snapshot; 13111ce9e605STiwei Bie 13121ce9e605STiwei Bie START_USE(vq); 13131ce9e605STiwei Bie 13141ce9e605STiwei Bie /* 13151ce9e605STiwei Bie * We need to expose the new flags value before checking notification 13161ce9e605STiwei Bie * suppressions. 13171ce9e605STiwei Bie */ 13181ce9e605STiwei Bie virtio_mb(vq->weak_barriers); 13191ce9e605STiwei Bie 1320f51f9826STiwei Bie old = vq->packed.next_avail_idx - vq->num_added; 1321f51f9826STiwei Bie new = vq->packed.next_avail_idx; 13221ce9e605STiwei Bie vq->num_added = 0; 13231ce9e605STiwei Bie 13241ce9e605STiwei Bie snapshot.u32 = *(u32 *)vq->packed.vring.device; 13251ce9e605STiwei Bie flags = le16_to_cpu(snapshot.flags); 13261ce9e605STiwei Bie 13271ce9e605STiwei Bie LAST_ADD_TIME_CHECK(vq); 13281ce9e605STiwei Bie LAST_ADD_TIME_INVALID(vq); 13291ce9e605STiwei Bie 1330f51f9826STiwei Bie if (flags != VRING_PACKED_EVENT_FLAG_DESC) { 13311ce9e605STiwei Bie needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE); 1332f51f9826STiwei Bie goto out; 1333f51f9826STiwei Bie } 1334f51f9826STiwei Bie 1335f51f9826STiwei Bie off_wrap = le16_to_cpu(snapshot.off_wrap); 1336f51f9826STiwei Bie 1337f51f9826STiwei Bie wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; 1338f51f9826STiwei Bie event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); 1339f51f9826STiwei Bie if (wrap_counter != vq->packed.avail_wrap_counter) 1340f51f9826STiwei Bie event_idx -= vq->packed.vring.num; 1341f51f9826STiwei Bie 1342f51f9826STiwei Bie needs_kick = vring_need_event(event_idx, new, old); 1343f51f9826STiwei Bie out: 13441ce9e605STiwei Bie END_USE(vq); 13451ce9e605STiwei Bie return needs_kick; 13461ce9e605STiwei Bie } 13471ce9e605STiwei Bie 13481ce9e605STiwei Bie static void detach_buf_packed(struct vring_virtqueue *vq, 13491ce9e605STiwei Bie unsigned int id, void **ctx) 13501ce9e605STiwei Bie { 13511ce9e605STiwei Bie struct vring_desc_state_packed *state = NULL; 13521ce9e605STiwei Bie struct vring_packed_desc *desc; 13531ce9e605STiwei Bie unsigned int i, curr; 13541ce9e605STiwei Bie 13551ce9e605STiwei Bie state = &vq->packed.desc_state[id]; 13561ce9e605STiwei Bie 13571ce9e605STiwei Bie /* Clear data ptr. */ 13581ce9e605STiwei Bie state->data = NULL; 13591ce9e605STiwei Bie 1360aeef9b47SJason Wang vq->packed.desc_extra[state->last].next = vq->free_head; 13611ce9e605STiwei Bie vq->free_head = id; 13621ce9e605STiwei Bie vq->vq.num_free += state->num; 13631ce9e605STiwei Bie 13641ce9e605STiwei Bie if (unlikely(vq->use_dma_api)) { 13651ce9e605STiwei Bie curr = id; 13661ce9e605STiwei Bie for (i = 0; i < state->num; i++) { 1367d80dc15bSXuan Zhuo vring_unmap_extra_packed(vq, 13681ce9e605STiwei Bie &vq->packed.desc_extra[curr]); 1369aeef9b47SJason Wang curr = vq->packed.desc_extra[curr].next; 13701ce9e605STiwei Bie } 13711ce9e605STiwei Bie } 13721ce9e605STiwei Bie 13731ce9e605STiwei Bie if (vq->indirect) { 13741ce9e605STiwei Bie u32 len; 13751ce9e605STiwei Bie 13761ce9e605STiwei Bie /* Free the indirect table, if any, now that it's unmapped. */ 13771ce9e605STiwei Bie desc = state->indir_desc; 13781ce9e605STiwei Bie if (!desc) 13791ce9e605STiwei Bie return; 13801ce9e605STiwei Bie 13811ce9e605STiwei Bie if (vq->use_dma_api) { 13821ce9e605STiwei Bie len = vq->packed.desc_extra[id].len; 13831ce9e605STiwei Bie for (i = 0; i < len / sizeof(struct vring_packed_desc); 13841ce9e605STiwei Bie i++) 13851ce9e605STiwei Bie vring_unmap_desc_packed(vq, &desc[i]); 13861ce9e605STiwei Bie } 13871ce9e605STiwei Bie kfree(desc); 13881ce9e605STiwei Bie state->indir_desc = NULL; 13891ce9e605STiwei Bie } else if (ctx) { 13901ce9e605STiwei Bie *ctx = state->indir_desc; 13911ce9e605STiwei Bie } 13921ce9e605STiwei Bie } 13931ce9e605STiwei Bie 13941ce9e605STiwei Bie static inline bool is_used_desc_packed(const struct vring_virtqueue *vq, 13951ce9e605STiwei Bie u16 idx, bool used_wrap_counter) 13961ce9e605STiwei Bie { 13971ce9e605STiwei Bie bool avail, used; 13981ce9e605STiwei Bie u16 flags; 13991ce9e605STiwei Bie 14001ce9e605STiwei Bie flags = le16_to_cpu(vq->packed.vring.desc[idx].flags); 14011ce9e605STiwei Bie avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL)); 14021ce9e605STiwei Bie used = !!(flags & (1 << VRING_PACKED_DESC_F_USED)); 14031ce9e605STiwei Bie 14041ce9e605STiwei Bie return avail == used && used == used_wrap_counter; 14051ce9e605STiwei Bie } 14061ce9e605STiwei Bie 14071ce9e605STiwei Bie static inline bool more_used_packed(const struct vring_virtqueue *vq) 14081ce9e605STiwei Bie { 14091ce9e605STiwei Bie return is_used_desc_packed(vq, vq->last_used_idx, 14101ce9e605STiwei Bie vq->packed.used_wrap_counter); 14111ce9e605STiwei Bie } 14121ce9e605STiwei Bie 14131ce9e605STiwei Bie static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq, 14141ce9e605STiwei Bie unsigned int *len, 14151ce9e605STiwei Bie void **ctx) 14161ce9e605STiwei Bie { 14171ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 14181ce9e605STiwei Bie u16 last_used, id; 14191ce9e605STiwei Bie void *ret; 14201ce9e605STiwei Bie 14211ce9e605STiwei Bie START_USE(vq); 14221ce9e605STiwei Bie 14231ce9e605STiwei Bie if (unlikely(vq->broken)) { 14241ce9e605STiwei Bie END_USE(vq); 14251ce9e605STiwei Bie return NULL; 14261ce9e605STiwei Bie } 14271ce9e605STiwei Bie 14281ce9e605STiwei Bie if (!more_used_packed(vq)) { 14291ce9e605STiwei Bie pr_debug("No more buffers in queue\n"); 14301ce9e605STiwei Bie END_USE(vq); 14311ce9e605STiwei Bie return NULL; 14321ce9e605STiwei Bie } 14331ce9e605STiwei Bie 14341ce9e605STiwei Bie /* Only get used elements after they have been exposed by host. */ 14351ce9e605STiwei Bie virtio_rmb(vq->weak_barriers); 14361ce9e605STiwei Bie 14371ce9e605STiwei Bie last_used = vq->last_used_idx; 14381ce9e605STiwei Bie id = le16_to_cpu(vq->packed.vring.desc[last_used].id); 14391ce9e605STiwei Bie *len = le32_to_cpu(vq->packed.vring.desc[last_used].len); 14401ce9e605STiwei Bie 14411ce9e605STiwei Bie if (unlikely(id >= vq->packed.vring.num)) { 14421ce9e605STiwei Bie BAD_RING(vq, "id %u out of range\n", id); 14431ce9e605STiwei Bie return NULL; 14441ce9e605STiwei Bie } 14451ce9e605STiwei Bie if (unlikely(!vq->packed.desc_state[id].data)) { 14461ce9e605STiwei Bie BAD_RING(vq, "id %u is not a head!\n", id); 14471ce9e605STiwei Bie return NULL; 14481ce9e605STiwei Bie } 14491ce9e605STiwei Bie 14501ce9e605STiwei Bie /* detach_buf_packed clears data, so grab it now. */ 14511ce9e605STiwei Bie ret = vq->packed.desc_state[id].data; 14521ce9e605STiwei Bie detach_buf_packed(vq, id, ctx); 14531ce9e605STiwei Bie 14541ce9e605STiwei Bie vq->last_used_idx += vq->packed.desc_state[id].num; 14551ce9e605STiwei Bie if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) { 14561ce9e605STiwei Bie vq->last_used_idx -= vq->packed.vring.num; 14571ce9e605STiwei Bie vq->packed.used_wrap_counter ^= 1; 14581ce9e605STiwei Bie } 14591ce9e605STiwei Bie 1460f51f9826STiwei Bie /* 1461f51f9826STiwei Bie * If we expect an interrupt for the next entry, tell host 1462f51f9826STiwei Bie * by writing event index and flush out the write before 1463f51f9826STiwei Bie * the read in the next get_buf call. 1464f51f9826STiwei Bie */ 1465f51f9826STiwei Bie if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC) 1466f51f9826STiwei Bie virtio_store_mb(vq->weak_barriers, 1467f51f9826STiwei Bie &vq->packed.vring.driver->off_wrap, 1468f51f9826STiwei Bie cpu_to_le16(vq->last_used_idx | 1469f51f9826STiwei Bie (vq->packed.used_wrap_counter << 1470f51f9826STiwei Bie VRING_PACKED_EVENT_F_WRAP_CTR))); 1471f51f9826STiwei Bie 14721ce9e605STiwei Bie LAST_ADD_TIME_INVALID(vq); 14731ce9e605STiwei Bie 14741ce9e605STiwei Bie END_USE(vq); 14751ce9e605STiwei Bie return ret; 14761ce9e605STiwei Bie } 14771ce9e605STiwei Bie 14781ce9e605STiwei Bie static void virtqueue_disable_cb_packed(struct virtqueue *_vq) 14791ce9e605STiwei Bie { 14801ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 14811ce9e605STiwei Bie 14821ce9e605STiwei Bie if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) { 14831ce9e605STiwei Bie vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; 14841ce9e605STiwei Bie vq->packed.vring.driver->flags = 14851ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 14861ce9e605STiwei Bie } 14871ce9e605STiwei Bie } 14881ce9e605STiwei Bie 14891ce9e605STiwei Bie static unsigned virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq) 14901ce9e605STiwei Bie { 14911ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 14921ce9e605STiwei Bie 14931ce9e605STiwei Bie START_USE(vq); 14941ce9e605STiwei Bie 14951ce9e605STiwei Bie /* 14961ce9e605STiwei Bie * We optimistically turn back on interrupts, then check if there was 14971ce9e605STiwei Bie * more to do. 14981ce9e605STiwei Bie */ 14991ce9e605STiwei Bie 1500f51f9826STiwei Bie if (vq->event) { 1501f51f9826STiwei Bie vq->packed.vring.driver->off_wrap = 1502f51f9826STiwei Bie cpu_to_le16(vq->last_used_idx | 1503f51f9826STiwei Bie (vq->packed.used_wrap_counter << 1504f51f9826STiwei Bie VRING_PACKED_EVENT_F_WRAP_CTR)); 1505f51f9826STiwei Bie /* 1506f51f9826STiwei Bie * We need to update event offset and event wrap 1507f51f9826STiwei Bie * counter first before updating event flags. 1508f51f9826STiwei Bie */ 1509f51f9826STiwei Bie virtio_wmb(vq->weak_barriers); 1510f51f9826STiwei Bie } 1511f51f9826STiwei Bie 15121ce9e605STiwei Bie if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { 1513f51f9826STiwei Bie vq->packed.event_flags_shadow = vq->event ? 1514f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_DESC : 1515f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_ENABLE; 15161ce9e605STiwei Bie vq->packed.vring.driver->flags = 15171ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 15181ce9e605STiwei Bie } 15191ce9e605STiwei Bie 15201ce9e605STiwei Bie END_USE(vq); 15211ce9e605STiwei Bie return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter << 15221ce9e605STiwei Bie VRING_PACKED_EVENT_F_WRAP_CTR); 15231ce9e605STiwei Bie } 15241ce9e605STiwei Bie 15251ce9e605STiwei Bie static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap) 15261ce9e605STiwei Bie { 15271ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 15281ce9e605STiwei Bie bool wrap_counter; 15291ce9e605STiwei Bie u16 used_idx; 15301ce9e605STiwei Bie 15311ce9e605STiwei Bie wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; 15321ce9e605STiwei Bie used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); 15331ce9e605STiwei Bie 15341ce9e605STiwei Bie return is_used_desc_packed(vq, used_idx, wrap_counter); 15351ce9e605STiwei Bie } 15361ce9e605STiwei Bie 15371ce9e605STiwei Bie static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq) 15381ce9e605STiwei Bie { 15391ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 15401ce9e605STiwei Bie u16 used_idx, wrap_counter; 1541f51f9826STiwei Bie u16 bufs; 15421ce9e605STiwei Bie 15431ce9e605STiwei Bie START_USE(vq); 15441ce9e605STiwei Bie 15451ce9e605STiwei Bie /* 15461ce9e605STiwei Bie * We optimistically turn back on interrupts, then check if there was 15471ce9e605STiwei Bie * more to do. 15481ce9e605STiwei Bie */ 15491ce9e605STiwei Bie 1550f51f9826STiwei Bie if (vq->event) { 1551f51f9826STiwei Bie /* TODO: tune this threshold */ 1552f51f9826STiwei Bie bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4; 15531ce9e605STiwei Bie wrap_counter = vq->packed.used_wrap_counter; 15541ce9e605STiwei Bie 1555f51f9826STiwei Bie used_idx = vq->last_used_idx + bufs; 1556f51f9826STiwei Bie if (used_idx >= vq->packed.vring.num) { 1557f51f9826STiwei Bie used_idx -= vq->packed.vring.num; 1558f51f9826STiwei Bie wrap_counter ^= 1; 1559f51f9826STiwei Bie } 1560f51f9826STiwei Bie 1561f51f9826STiwei Bie vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx | 1562f51f9826STiwei Bie (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR)); 1563f51f9826STiwei Bie 1564f51f9826STiwei Bie /* 1565f51f9826STiwei Bie * We need to update event offset and event wrap 1566f51f9826STiwei Bie * counter first before updating event flags. 1567f51f9826STiwei Bie */ 1568f51f9826STiwei Bie virtio_wmb(vq->weak_barriers); 1569f51f9826STiwei Bie } 1570f51f9826STiwei Bie 15711ce9e605STiwei Bie if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) { 1572f51f9826STiwei Bie vq->packed.event_flags_shadow = vq->event ? 1573f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_DESC : 1574f51f9826STiwei Bie VRING_PACKED_EVENT_FLAG_ENABLE; 15751ce9e605STiwei Bie vq->packed.vring.driver->flags = 15761ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 15771ce9e605STiwei Bie } 15781ce9e605STiwei Bie 15791ce9e605STiwei Bie /* 15801ce9e605STiwei Bie * We need to update event suppression structure first 15811ce9e605STiwei Bie * before re-checking for more used buffers. 15821ce9e605STiwei Bie */ 15831ce9e605STiwei Bie virtio_mb(vq->weak_barriers); 15841ce9e605STiwei Bie 158540ce7919SMarvin Liu if (is_used_desc_packed(vq, 158640ce7919SMarvin Liu vq->last_used_idx, 158740ce7919SMarvin Liu vq->packed.used_wrap_counter)) { 15881ce9e605STiwei Bie END_USE(vq); 15891ce9e605STiwei Bie return false; 15901ce9e605STiwei Bie } 15911ce9e605STiwei Bie 15921ce9e605STiwei Bie END_USE(vq); 15931ce9e605STiwei Bie return true; 15941ce9e605STiwei Bie } 15951ce9e605STiwei Bie 15961ce9e605STiwei Bie static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq) 15971ce9e605STiwei Bie { 15981ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 15991ce9e605STiwei Bie unsigned int i; 16001ce9e605STiwei Bie void *buf; 16011ce9e605STiwei Bie 16021ce9e605STiwei Bie START_USE(vq); 16031ce9e605STiwei Bie 16041ce9e605STiwei Bie for (i = 0; i < vq->packed.vring.num; i++) { 16051ce9e605STiwei Bie if (!vq->packed.desc_state[i].data) 16061ce9e605STiwei Bie continue; 16071ce9e605STiwei Bie /* detach_buf clears data, so grab it now. */ 16081ce9e605STiwei Bie buf = vq->packed.desc_state[i].data; 16091ce9e605STiwei Bie detach_buf_packed(vq, i, NULL); 16101ce9e605STiwei Bie END_USE(vq); 16111ce9e605STiwei Bie return buf; 16121ce9e605STiwei Bie } 16131ce9e605STiwei Bie /* That should have freed everything. */ 16141ce9e605STiwei Bie BUG_ON(vq->vq.num_free != vq->packed.vring.num); 16151ce9e605STiwei Bie 16161ce9e605STiwei Bie END_USE(vq); 16171ce9e605STiwei Bie return NULL; 16181ce9e605STiwei Bie } 16191ce9e605STiwei Bie 16205a222421SJason Wang static struct vring_desc_extra *vring_alloc_desc_extra(struct vring_virtqueue *vq, 16215a222421SJason Wang unsigned int num) 16225a222421SJason Wang { 16235a222421SJason Wang struct vring_desc_extra *desc_extra; 16245a222421SJason Wang unsigned int i; 16255a222421SJason Wang 16265a222421SJason Wang desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra), 16275a222421SJason Wang GFP_KERNEL); 16285a222421SJason Wang if (!desc_extra) 16295a222421SJason Wang return NULL; 16305a222421SJason Wang 16315a222421SJason Wang memset(desc_extra, 0, num * sizeof(struct vring_desc_extra)); 16325a222421SJason Wang 16335a222421SJason Wang for (i = 0; i < num - 1; i++) 16345a222421SJason Wang desc_extra[i].next = i + 1; 16355a222421SJason Wang 16365a222421SJason Wang return desc_extra; 16375a222421SJason Wang } 16385a222421SJason Wang 16391ce9e605STiwei Bie static struct virtqueue *vring_create_virtqueue_packed( 16401ce9e605STiwei Bie unsigned int index, 16411ce9e605STiwei Bie unsigned int num, 16421ce9e605STiwei Bie unsigned int vring_align, 16431ce9e605STiwei Bie struct virtio_device *vdev, 16441ce9e605STiwei Bie bool weak_barriers, 16451ce9e605STiwei Bie bool may_reduce_num, 16461ce9e605STiwei Bie bool context, 16471ce9e605STiwei Bie bool (*notify)(struct virtqueue *), 16481ce9e605STiwei Bie void (*callback)(struct virtqueue *), 16491ce9e605STiwei Bie const char *name) 16501ce9e605STiwei Bie { 16511ce9e605STiwei Bie struct vring_virtqueue *vq; 16521ce9e605STiwei Bie struct vring_packed_desc *ring; 16531ce9e605STiwei Bie struct vring_packed_desc_event *driver, *device; 16541ce9e605STiwei Bie dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr; 16551ce9e605STiwei Bie size_t ring_size_in_bytes, event_size_in_bytes; 16561ce9e605STiwei Bie 16571ce9e605STiwei Bie ring_size_in_bytes = num * sizeof(struct vring_packed_desc); 16581ce9e605STiwei Bie 16591ce9e605STiwei Bie ring = vring_alloc_queue(vdev, ring_size_in_bytes, 16601ce9e605STiwei Bie &ring_dma_addr, 16611ce9e605STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 16621ce9e605STiwei Bie if (!ring) 16631ce9e605STiwei Bie goto err_ring; 16641ce9e605STiwei Bie 16651ce9e605STiwei Bie event_size_in_bytes = sizeof(struct vring_packed_desc_event); 16661ce9e605STiwei Bie 16671ce9e605STiwei Bie driver = vring_alloc_queue(vdev, event_size_in_bytes, 16681ce9e605STiwei Bie &driver_event_dma_addr, 16691ce9e605STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 16701ce9e605STiwei Bie if (!driver) 16711ce9e605STiwei Bie goto err_driver; 16721ce9e605STiwei Bie 16731ce9e605STiwei Bie device = vring_alloc_queue(vdev, event_size_in_bytes, 16741ce9e605STiwei Bie &device_event_dma_addr, 16751ce9e605STiwei Bie GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 16761ce9e605STiwei Bie if (!device) 16771ce9e605STiwei Bie goto err_device; 16781ce9e605STiwei Bie 16791ce9e605STiwei Bie vq = kmalloc(sizeof(*vq), GFP_KERNEL); 16801ce9e605STiwei Bie if (!vq) 16811ce9e605STiwei Bie goto err_vq; 16821ce9e605STiwei Bie 16831ce9e605STiwei Bie vq->vq.callback = callback; 16841ce9e605STiwei Bie vq->vq.vdev = vdev; 16851ce9e605STiwei Bie vq->vq.name = name; 16861ce9e605STiwei Bie vq->vq.num_free = num; 16871ce9e605STiwei Bie vq->vq.index = index; 16881ce9e605STiwei Bie vq->we_own_ring = true; 16891ce9e605STiwei Bie vq->notify = notify; 16901ce9e605STiwei Bie vq->weak_barriers = weak_barriers; 16911ce9e605STiwei Bie vq->broken = false; 16921ce9e605STiwei Bie vq->last_used_idx = 0; 16938d622d21SMichael S. Tsirkin vq->event_triggered = false; 16941ce9e605STiwei Bie vq->num_added = 0; 16951ce9e605STiwei Bie vq->packed_ring = true; 16961ce9e605STiwei Bie vq->use_dma_api = vring_use_dma_api(vdev); 16971ce9e605STiwei Bie #ifdef DEBUG 16981ce9e605STiwei Bie vq->in_use = false; 16991ce9e605STiwei Bie vq->last_add_time_valid = false; 17001ce9e605STiwei Bie #endif 17011ce9e605STiwei Bie 17021ce9e605STiwei Bie vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && 17031ce9e605STiwei Bie !context; 17041ce9e605STiwei Bie vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 17051ce9e605STiwei Bie 170645383fb0STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) 170745383fb0STiwei Bie vq->weak_barriers = false; 170845383fb0STiwei Bie 17091ce9e605STiwei Bie vq->packed.ring_dma_addr = ring_dma_addr; 17101ce9e605STiwei Bie vq->packed.driver_event_dma_addr = driver_event_dma_addr; 17111ce9e605STiwei Bie vq->packed.device_event_dma_addr = device_event_dma_addr; 17121ce9e605STiwei Bie 17131ce9e605STiwei Bie vq->packed.ring_size_in_bytes = ring_size_in_bytes; 17141ce9e605STiwei Bie vq->packed.event_size_in_bytes = event_size_in_bytes; 17151ce9e605STiwei Bie 17161ce9e605STiwei Bie vq->packed.vring.num = num; 17171ce9e605STiwei Bie vq->packed.vring.desc = ring; 17181ce9e605STiwei Bie vq->packed.vring.driver = driver; 17191ce9e605STiwei Bie vq->packed.vring.device = device; 17201ce9e605STiwei Bie 17211ce9e605STiwei Bie vq->packed.next_avail_idx = 0; 17221ce9e605STiwei Bie vq->packed.avail_wrap_counter = 1; 17231ce9e605STiwei Bie vq->packed.used_wrap_counter = 1; 17241ce9e605STiwei Bie vq->packed.event_flags_shadow = 0; 17251ce9e605STiwei Bie vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL; 17261ce9e605STiwei Bie 17271ce9e605STiwei Bie vq->packed.desc_state = kmalloc_array(num, 17281ce9e605STiwei Bie sizeof(struct vring_desc_state_packed), 17291ce9e605STiwei Bie GFP_KERNEL); 17301ce9e605STiwei Bie if (!vq->packed.desc_state) 17311ce9e605STiwei Bie goto err_desc_state; 17321ce9e605STiwei Bie 17331ce9e605STiwei Bie memset(vq->packed.desc_state, 0, 17341ce9e605STiwei Bie num * sizeof(struct vring_desc_state_packed)); 17351ce9e605STiwei Bie 17361ce9e605STiwei Bie /* Put everything in free lists. */ 17371ce9e605STiwei Bie vq->free_head = 0; 17381ce9e605STiwei Bie 17395a222421SJason Wang vq->packed.desc_extra = vring_alloc_desc_extra(vq, num); 17401ce9e605STiwei Bie if (!vq->packed.desc_extra) 17411ce9e605STiwei Bie goto err_desc_extra; 17421ce9e605STiwei Bie 17431ce9e605STiwei Bie /* No callback? Tell other side not to bother us. */ 17441ce9e605STiwei Bie if (!callback) { 17451ce9e605STiwei Bie vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE; 17461ce9e605STiwei Bie vq->packed.vring.driver->flags = 17471ce9e605STiwei Bie cpu_to_le16(vq->packed.event_flags_shadow); 17481ce9e605STiwei Bie } 17491ce9e605STiwei Bie 17500e566c8fSParav Pandit spin_lock(&vdev->vqs_list_lock); 1751e152d8afSDan Carpenter list_add_tail(&vq->vq.list, &vdev->vqs); 17520e566c8fSParav Pandit spin_unlock(&vdev->vqs_list_lock); 17531ce9e605STiwei Bie return &vq->vq; 17541ce9e605STiwei Bie 17551ce9e605STiwei Bie err_desc_extra: 17561ce9e605STiwei Bie kfree(vq->packed.desc_state); 17571ce9e605STiwei Bie err_desc_state: 17581ce9e605STiwei Bie kfree(vq); 17591ce9e605STiwei Bie err_vq: 1760ae93d8eaSDan Carpenter vring_free_queue(vdev, event_size_in_bytes, device, device_event_dma_addr); 17611ce9e605STiwei Bie err_device: 1762ae93d8eaSDan Carpenter vring_free_queue(vdev, event_size_in_bytes, driver, driver_event_dma_addr); 17631ce9e605STiwei Bie err_driver: 17641ce9e605STiwei Bie vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr); 17651ce9e605STiwei Bie err_ring: 17661ce9e605STiwei Bie return NULL; 17671ce9e605STiwei Bie } 17681ce9e605STiwei Bie 17691ce9e605STiwei Bie 17701ce9e605STiwei Bie /* 1771e6f633e5STiwei Bie * Generic functions and exported symbols. 1772e6f633e5STiwei Bie */ 1773e6f633e5STiwei Bie 1774e6f633e5STiwei Bie static inline int virtqueue_add(struct virtqueue *_vq, 1775e6f633e5STiwei Bie struct scatterlist *sgs[], 1776e6f633e5STiwei Bie unsigned int total_sg, 1777e6f633e5STiwei Bie unsigned int out_sgs, 1778e6f633e5STiwei Bie unsigned int in_sgs, 1779e6f633e5STiwei Bie void *data, 1780e6f633e5STiwei Bie void *ctx, 1781e6f633e5STiwei Bie gfp_t gfp) 1782e6f633e5STiwei Bie { 17831ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 17841ce9e605STiwei Bie 17851ce9e605STiwei Bie return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg, 17861ce9e605STiwei Bie out_sgs, in_sgs, data, ctx, gfp) : 17871ce9e605STiwei Bie virtqueue_add_split(_vq, sgs, total_sg, 1788e6f633e5STiwei Bie out_sgs, in_sgs, data, ctx, gfp); 1789e6f633e5STiwei Bie } 1790e6f633e5STiwei Bie 1791e6f633e5STiwei Bie /** 1792e6f633e5STiwei Bie * virtqueue_add_sgs - expose buffers to other end 1793a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 1794e6f633e5STiwei Bie * @sgs: array of terminated scatterlists. 1795a5581206SJiang Biao * @out_sgs: the number of scatterlists readable by other side 1796a5581206SJiang Biao * @in_sgs: the number of scatterlists which are writable (after readable ones) 1797e6f633e5STiwei Bie * @data: the token identifying the buffer. 1798e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 1799e6f633e5STiwei Bie * 1800e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 1801e6f633e5STiwei Bie * at the same time (except where noted). 1802e6f633e5STiwei Bie * 1803e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 1804e6f633e5STiwei Bie */ 1805e6f633e5STiwei Bie int virtqueue_add_sgs(struct virtqueue *_vq, 1806e6f633e5STiwei Bie struct scatterlist *sgs[], 1807e6f633e5STiwei Bie unsigned int out_sgs, 1808e6f633e5STiwei Bie unsigned int in_sgs, 1809e6f633e5STiwei Bie void *data, 1810e6f633e5STiwei Bie gfp_t gfp) 1811e6f633e5STiwei Bie { 1812e6f633e5STiwei Bie unsigned int i, total_sg = 0; 1813e6f633e5STiwei Bie 1814e6f633e5STiwei Bie /* Count them first. */ 1815e6f633e5STiwei Bie for (i = 0; i < out_sgs + in_sgs; i++) { 1816e6f633e5STiwei Bie struct scatterlist *sg; 1817e6f633e5STiwei Bie 1818e6f633e5STiwei Bie for (sg = sgs[i]; sg; sg = sg_next(sg)) 1819e6f633e5STiwei Bie total_sg++; 1820e6f633e5STiwei Bie } 1821e6f633e5STiwei Bie return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, 1822e6f633e5STiwei Bie data, NULL, gfp); 1823e6f633e5STiwei Bie } 1824e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_sgs); 1825e6f633e5STiwei Bie 1826e6f633e5STiwei Bie /** 1827e6f633e5STiwei Bie * virtqueue_add_outbuf - expose output buffers to other end 1828e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1829e6f633e5STiwei Bie * @sg: scatterlist (must be well-formed and terminated!) 1830e6f633e5STiwei Bie * @num: the number of entries in @sg readable by other side 1831e6f633e5STiwei Bie * @data: the token identifying the buffer. 1832e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 1833e6f633e5STiwei Bie * 1834e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 1835e6f633e5STiwei Bie * at the same time (except where noted). 1836e6f633e5STiwei Bie * 1837e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 1838e6f633e5STiwei Bie */ 1839e6f633e5STiwei Bie int virtqueue_add_outbuf(struct virtqueue *vq, 1840e6f633e5STiwei Bie struct scatterlist *sg, unsigned int num, 1841e6f633e5STiwei Bie void *data, 1842e6f633e5STiwei Bie gfp_t gfp) 1843e6f633e5STiwei Bie { 1844e6f633e5STiwei Bie return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp); 1845e6f633e5STiwei Bie } 1846e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); 1847e6f633e5STiwei Bie 1848e6f633e5STiwei Bie /** 1849e6f633e5STiwei Bie * virtqueue_add_inbuf - expose input buffers to other end 1850e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1851e6f633e5STiwei Bie * @sg: scatterlist (must be well-formed and terminated!) 1852e6f633e5STiwei Bie * @num: the number of entries in @sg writable by other side 1853e6f633e5STiwei Bie * @data: the token identifying the buffer. 1854e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 1855e6f633e5STiwei Bie * 1856e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 1857e6f633e5STiwei Bie * at the same time (except where noted). 1858e6f633e5STiwei Bie * 1859e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 1860e6f633e5STiwei Bie */ 1861e6f633e5STiwei Bie int virtqueue_add_inbuf(struct virtqueue *vq, 1862e6f633e5STiwei Bie struct scatterlist *sg, unsigned int num, 1863e6f633e5STiwei Bie void *data, 1864e6f633e5STiwei Bie gfp_t gfp) 1865e6f633e5STiwei Bie { 1866e6f633e5STiwei Bie return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp); 1867e6f633e5STiwei Bie } 1868e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); 1869e6f633e5STiwei Bie 1870e6f633e5STiwei Bie /** 1871e6f633e5STiwei Bie * virtqueue_add_inbuf_ctx - expose input buffers to other end 1872e6f633e5STiwei Bie * @vq: the struct virtqueue we're talking about. 1873e6f633e5STiwei Bie * @sg: scatterlist (must be well-formed and terminated!) 1874e6f633e5STiwei Bie * @num: the number of entries in @sg writable by other side 1875e6f633e5STiwei Bie * @data: the token identifying the buffer. 1876e6f633e5STiwei Bie * @ctx: extra context for the token 1877e6f633e5STiwei Bie * @gfp: how to do memory allocations (if necessary). 1878e6f633e5STiwei Bie * 1879e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue operations 1880e6f633e5STiwei Bie * at the same time (except where noted). 1881e6f633e5STiwei Bie * 1882e6f633e5STiwei Bie * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 1883e6f633e5STiwei Bie */ 1884e6f633e5STiwei Bie int virtqueue_add_inbuf_ctx(struct virtqueue *vq, 1885e6f633e5STiwei Bie struct scatterlist *sg, unsigned int num, 1886e6f633e5STiwei Bie void *data, 1887e6f633e5STiwei Bie void *ctx, 1888e6f633e5STiwei Bie gfp_t gfp) 1889e6f633e5STiwei Bie { 1890e6f633e5STiwei Bie return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp); 1891e6f633e5STiwei Bie } 1892e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx); 1893e6f633e5STiwei Bie 1894e6f633e5STiwei Bie /** 1895e6f633e5STiwei Bie * virtqueue_kick_prepare - first half of split virtqueue_kick call. 1896a5581206SJiang Biao * @_vq: the struct virtqueue 1897e6f633e5STiwei Bie * 1898e6f633e5STiwei Bie * Instead of virtqueue_kick(), you can do: 1899e6f633e5STiwei Bie * if (virtqueue_kick_prepare(vq)) 1900e6f633e5STiwei Bie * virtqueue_notify(vq); 1901e6f633e5STiwei Bie * 1902e6f633e5STiwei Bie * This is sometimes useful because the virtqueue_kick_prepare() needs 1903e6f633e5STiwei Bie * to be serialized, but the actual virtqueue_notify() call does not. 1904e6f633e5STiwei Bie */ 1905e6f633e5STiwei Bie bool virtqueue_kick_prepare(struct virtqueue *_vq) 1906e6f633e5STiwei Bie { 19071ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 19081ce9e605STiwei Bie 19091ce9e605STiwei Bie return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) : 19101ce9e605STiwei Bie virtqueue_kick_prepare_split(_vq); 1911e6f633e5STiwei Bie } 1912e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); 1913e6f633e5STiwei Bie 1914e6f633e5STiwei Bie /** 1915e6f633e5STiwei Bie * virtqueue_notify - second half of split virtqueue_kick call. 1916a5581206SJiang Biao * @_vq: the struct virtqueue 1917e6f633e5STiwei Bie * 1918e6f633e5STiwei Bie * This does not need to be serialized. 1919e6f633e5STiwei Bie * 1920e6f633e5STiwei Bie * Returns false if host notify failed or queue is broken, otherwise true. 1921e6f633e5STiwei Bie */ 1922e6f633e5STiwei Bie bool virtqueue_notify(struct virtqueue *_vq) 1923e6f633e5STiwei Bie { 1924e6f633e5STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 1925e6f633e5STiwei Bie 1926e6f633e5STiwei Bie if (unlikely(vq->broken)) 1927e6f633e5STiwei Bie return false; 1928e6f633e5STiwei Bie 1929e6f633e5STiwei Bie /* Prod other side to tell it about changes. */ 1930e6f633e5STiwei Bie if (!vq->notify(_vq)) { 1931e6f633e5STiwei Bie vq->broken = true; 1932e6f633e5STiwei Bie return false; 1933e6f633e5STiwei Bie } 1934e6f633e5STiwei Bie return true; 1935e6f633e5STiwei Bie } 1936e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_notify); 1937e6f633e5STiwei Bie 1938e6f633e5STiwei Bie /** 1939e6f633e5STiwei Bie * virtqueue_kick - update after add_buf 1940e6f633e5STiwei Bie * @vq: the struct virtqueue 1941e6f633e5STiwei Bie * 1942e6f633e5STiwei Bie * After one or more virtqueue_add_* calls, invoke this to kick 1943e6f633e5STiwei Bie * the other side. 1944e6f633e5STiwei Bie * 1945e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 1946e6f633e5STiwei Bie * operations at the same time (except where noted). 1947e6f633e5STiwei Bie * 1948e6f633e5STiwei Bie * Returns false if kick failed, otherwise true. 1949e6f633e5STiwei Bie */ 1950e6f633e5STiwei Bie bool virtqueue_kick(struct virtqueue *vq) 1951e6f633e5STiwei Bie { 1952e6f633e5STiwei Bie if (virtqueue_kick_prepare(vq)) 1953e6f633e5STiwei Bie return virtqueue_notify(vq); 1954e6f633e5STiwei Bie return true; 1955e6f633e5STiwei Bie } 1956e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick); 1957e6f633e5STiwei Bie 1958e6f633e5STiwei Bie /** 195931c11db6SYang Li * virtqueue_get_buf_ctx - get the next used buffer 1960a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 1961e6f633e5STiwei Bie * @len: the length written into the buffer 1962a5581206SJiang Biao * @ctx: extra context for the token 1963e6f633e5STiwei Bie * 1964e6f633e5STiwei Bie * If the device wrote data into the buffer, @len will be set to the 1965e6f633e5STiwei Bie * amount written. This means you don't need to clear the buffer 1966e6f633e5STiwei Bie * beforehand to ensure there's no data leakage in the case of short 1967e6f633e5STiwei Bie * writes. 1968e6f633e5STiwei Bie * 1969e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 1970e6f633e5STiwei Bie * operations at the same time (except where noted). 1971e6f633e5STiwei Bie * 1972e6f633e5STiwei Bie * Returns NULL if there are no used buffers, or the "data" token 1973e6f633e5STiwei Bie * handed to virtqueue_add_*(). 1974e6f633e5STiwei Bie */ 1975e6f633e5STiwei Bie void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len, 1976e6f633e5STiwei Bie void **ctx) 1977e6f633e5STiwei Bie { 19781ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 19791ce9e605STiwei Bie 19801ce9e605STiwei Bie return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) : 19811ce9e605STiwei Bie virtqueue_get_buf_ctx_split(_vq, len, ctx); 1982e6f633e5STiwei Bie } 1983e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx); 1984e6f633e5STiwei Bie 1985e6f633e5STiwei Bie void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) 1986e6f633e5STiwei Bie { 1987e6f633e5STiwei Bie return virtqueue_get_buf_ctx(_vq, len, NULL); 1988e6f633e5STiwei Bie } 1989e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf); 1990e6f633e5STiwei Bie /** 1991e6f633e5STiwei Bie * virtqueue_disable_cb - disable callbacks 1992a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 1993e6f633e5STiwei Bie * 1994e6f633e5STiwei Bie * Note that this is not necessarily synchronous, hence unreliable and only 1995e6f633e5STiwei Bie * useful as an optimization. 1996e6f633e5STiwei Bie * 1997e6f633e5STiwei Bie * Unlike other operations, this need not be serialized. 1998e6f633e5STiwei Bie */ 1999e6f633e5STiwei Bie void virtqueue_disable_cb(struct virtqueue *_vq) 2000e6f633e5STiwei Bie { 20011ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 20021ce9e605STiwei Bie 20038d622d21SMichael S. Tsirkin /* If device triggered an event already it won't trigger one again: 20048d622d21SMichael S. Tsirkin * no need to disable. 20058d622d21SMichael S. Tsirkin */ 20068d622d21SMichael S. Tsirkin if (vq->event_triggered) 20078d622d21SMichael S. Tsirkin return; 20088d622d21SMichael S. Tsirkin 20091ce9e605STiwei Bie if (vq->packed_ring) 20101ce9e605STiwei Bie virtqueue_disable_cb_packed(_vq); 20111ce9e605STiwei Bie else 2012e6f633e5STiwei Bie virtqueue_disable_cb_split(_vq); 2013e6f633e5STiwei Bie } 2014e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_disable_cb); 2015e6f633e5STiwei Bie 2016e6f633e5STiwei Bie /** 2017e6f633e5STiwei Bie * virtqueue_enable_cb_prepare - restart callbacks after disable_cb 2018a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2019e6f633e5STiwei Bie * 2020e6f633e5STiwei Bie * This re-enables callbacks; it returns current queue state 2021e6f633e5STiwei Bie * in an opaque unsigned value. This value should be later tested by 2022e6f633e5STiwei Bie * virtqueue_poll, to detect a possible race between the driver checking for 2023e6f633e5STiwei Bie * more work, and enabling callbacks. 2024e6f633e5STiwei Bie * 2025e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 2026e6f633e5STiwei Bie * operations at the same time (except where noted). 2027e6f633e5STiwei Bie */ 2028e6f633e5STiwei Bie unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) 2029e6f633e5STiwei Bie { 20301ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 20311ce9e605STiwei Bie 20328d622d21SMichael S. Tsirkin if (vq->event_triggered) 20338d622d21SMichael S. Tsirkin vq->event_triggered = false; 20348d622d21SMichael S. Tsirkin 20351ce9e605STiwei Bie return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) : 20361ce9e605STiwei Bie virtqueue_enable_cb_prepare_split(_vq); 2037e6f633e5STiwei Bie } 2038e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); 2039e6f633e5STiwei Bie 2040e6f633e5STiwei Bie /** 2041e6f633e5STiwei Bie * virtqueue_poll - query pending used buffers 2042a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2043e6f633e5STiwei Bie * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). 2044e6f633e5STiwei Bie * 2045e6f633e5STiwei Bie * Returns "true" if there are pending used buffers in the queue. 2046e6f633e5STiwei Bie * 2047e6f633e5STiwei Bie * This does not need to be serialized. 2048e6f633e5STiwei Bie */ 2049e6f633e5STiwei Bie bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) 2050e6f633e5STiwei Bie { 2051e6f633e5STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 2052e6f633e5STiwei Bie 2053481a0d74SMao Wenan if (unlikely(vq->broken)) 2054481a0d74SMao Wenan return false; 2055481a0d74SMao Wenan 2056e6f633e5STiwei Bie virtio_mb(vq->weak_barriers); 20571ce9e605STiwei Bie return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) : 20581ce9e605STiwei Bie virtqueue_poll_split(_vq, last_used_idx); 2059e6f633e5STiwei Bie } 2060e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_poll); 2061e6f633e5STiwei Bie 2062e6f633e5STiwei Bie /** 2063e6f633e5STiwei Bie * virtqueue_enable_cb - restart callbacks after disable_cb. 2064a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2065e6f633e5STiwei Bie * 2066e6f633e5STiwei Bie * This re-enables callbacks; it returns "false" if there are pending 2067e6f633e5STiwei Bie * buffers in the queue, to detect a possible race between the driver 2068e6f633e5STiwei Bie * checking for more work, and enabling callbacks. 2069e6f633e5STiwei Bie * 2070e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 2071e6f633e5STiwei Bie * operations at the same time (except where noted). 2072e6f633e5STiwei Bie */ 2073e6f633e5STiwei Bie bool virtqueue_enable_cb(struct virtqueue *_vq) 2074e6f633e5STiwei Bie { 2075e6f633e5STiwei Bie unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); 2076e6f633e5STiwei Bie 2077e6f633e5STiwei Bie return !virtqueue_poll(_vq, last_used_idx); 2078e6f633e5STiwei Bie } 2079e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb); 2080e6f633e5STiwei Bie 2081e6f633e5STiwei Bie /** 2082e6f633e5STiwei Bie * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. 2083a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2084e6f633e5STiwei Bie * 2085e6f633e5STiwei Bie * This re-enables callbacks but hints to the other side to delay 2086e6f633e5STiwei Bie * interrupts until most of the available buffers have been processed; 2087e6f633e5STiwei Bie * it returns "false" if there are many pending buffers in the queue, 2088e6f633e5STiwei Bie * to detect a possible race between the driver checking for more work, 2089e6f633e5STiwei Bie * and enabling callbacks. 2090e6f633e5STiwei Bie * 2091e6f633e5STiwei Bie * Caller must ensure we don't call this with other virtqueue 2092e6f633e5STiwei Bie * operations at the same time (except where noted). 2093e6f633e5STiwei Bie */ 2094e6f633e5STiwei Bie bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) 2095e6f633e5STiwei Bie { 20961ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 20971ce9e605STiwei Bie 20988d622d21SMichael S. Tsirkin if (vq->event_triggered) 20998d622d21SMichael S. Tsirkin vq->event_triggered = false; 21008d622d21SMichael S. Tsirkin 21011ce9e605STiwei Bie return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) : 21021ce9e605STiwei Bie virtqueue_enable_cb_delayed_split(_vq); 2103e6f633e5STiwei Bie } 2104e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); 2105e6f633e5STiwei Bie 2106138fd251STiwei Bie /** 2107138fd251STiwei Bie * virtqueue_detach_unused_buf - detach first unused buffer 2108a5581206SJiang Biao * @_vq: the struct virtqueue we're talking about. 2109138fd251STiwei Bie * 2110138fd251STiwei Bie * Returns NULL or the "data" token handed to virtqueue_add_*(). 2111138fd251STiwei Bie * This is not valid on an active queue; it is useful only for device 2112138fd251STiwei Bie * shutdown. 2113138fd251STiwei Bie */ 2114138fd251STiwei Bie void *virtqueue_detach_unused_buf(struct virtqueue *_vq) 2115138fd251STiwei Bie { 21161ce9e605STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 21171ce9e605STiwei Bie 21181ce9e605STiwei Bie return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) : 21191ce9e605STiwei Bie virtqueue_detach_unused_buf_split(_vq); 2120138fd251STiwei Bie } 21217c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); 2122c021eac4SShirley Ma 2123138fd251STiwei Bie static inline bool more_used(const struct vring_virtqueue *vq) 2124138fd251STiwei Bie { 21251ce9e605STiwei Bie return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq); 2126138fd251STiwei Bie } 2127138fd251STiwei Bie 21280a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq) 21290a8a69ddSRusty Russell { 21300a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 21310a8a69ddSRusty Russell 21320a8a69ddSRusty Russell if (!more_used(vq)) { 21330a8a69ddSRusty Russell pr_debug("virtqueue interrupt with no work for %p\n", vq); 21340a8a69ddSRusty Russell return IRQ_NONE; 21350a8a69ddSRusty Russell } 21360a8a69ddSRusty Russell 21370a8a69ddSRusty Russell if (unlikely(vq->broken)) 21380a8a69ddSRusty Russell return IRQ_HANDLED; 21390a8a69ddSRusty Russell 21408d622d21SMichael S. Tsirkin /* Just a hint for performance: so it's ok that this can be racy! */ 21418d622d21SMichael S. Tsirkin if (vq->event) 21428d622d21SMichael S. Tsirkin vq->event_triggered = true; 21438d622d21SMichael S. Tsirkin 21440a8a69ddSRusty Russell pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); 214518445c4dSRusty Russell if (vq->vq.callback) 214618445c4dSRusty Russell vq->vq.callback(&vq->vq); 21470a8a69ddSRusty Russell 21480a8a69ddSRusty Russell return IRQ_HANDLED; 21490a8a69ddSRusty Russell } 2150c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt); 21510a8a69ddSRusty Russell 21521ce9e605STiwei Bie /* Only available for split ring */ 21532a2d1382SAndy Lutomirski struct virtqueue *__vring_new_virtqueue(unsigned int index, 21542a2d1382SAndy Lutomirski struct vring vring, 21550a8a69ddSRusty Russell struct virtio_device *vdev, 21567b21e34fSRusty Russell bool weak_barriers, 2157f94682ddSMichael S. Tsirkin bool context, 215846f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *), 21599499f5e7SRusty Russell void (*callback)(struct virtqueue *), 21609499f5e7SRusty Russell const char *name) 21610a8a69ddSRusty Russell { 21622a2d1382SAndy Lutomirski struct vring_virtqueue *vq; 21630a8a69ddSRusty Russell 21641ce9e605STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) 21651ce9e605STiwei Bie return NULL; 21661ce9e605STiwei Bie 2167cbeedb72STiwei Bie vq = kmalloc(sizeof(*vq), GFP_KERNEL); 21680a8a69ddSRusty Russell if (!vq) 21690a8a69ddSRusty Russell return NULL; 21700a8a69ddSRusty Russell 21711ce9e605STiwei Bie vq->packed_ring = false; 21720a8a69ddSRusty Russell vq->vq.callback = callback; 21730a8a69ddSRusty Russell vq->vq.vdev = vdev; 21749499f5e7SRusty Russell vq->vq.name = name; 21752a2d1382SAndy Lutomirski vq->vq.num_free = vring.num; 217606ca287dSRusty Russell vq->vq.index = index; 21772a2d1382SAndy Lutomirski vq->we_own_ring = false; 21780a8a69ddSRusty Russell vq->notify = notify; 21797b21e34fSRusty Russell vq->weak_barriers = weak_barriers; 21800a8a69ddSRusty Russell vq->broken = false; 21810a8a69ddSRusty Russell vq->last_used_idx = 0; 21828d622d21SMichael S. Tsirkin vq->event_triggered = false; 21830a8a69ddSRusty Russell vq->num_added = 0; 2184fb3fba6bSTiwei Bie vq->use_dma_api = vring_use_dma_api(vdev); 21850a8a69ddSRusty Russell #ifdef DEBUG 21860a8a69ddSRusty Russell vq->in_use = false; 2187e93300b1SRusty Russell vq->last_add_time_valid = false; 21880a8a69ddSRusty Russell #endif 21890a8a69ddSRusty Russell 21905a08b04fSMichael S. Tsirkin vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && 21915a08b04fSMichael S. Tsirkin !context; 2192a5c262c5SMichael S. Tsirkin vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 21939fa29b9dSMark McLoughlin 219445383fb0STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) 219545383fb0STiwei Bie vq->weak_barriers = false; 219645383fb0STiwei Bie 2197d79dca75STiwei Bie vq->split.queue_dma_addr = 0; 2198d79dca75STiwei Bie vq->split.queue_size_in_bytes = 0; 2199d79dca75STiwei Bie 2200e593bf97STiwei Bie vq->split.vring = vring; 2201e593bf97STiwei Bie vq->split.avail_flags_shadow = 0; 2202e593bf97STiwei Bie vq->split.avail_idx_shadow = 0; 2203e593bf97STiwei Bie 22040a8a69ddSRusty Russell /* No callback? Tell other side not to bother us. */ 2205f277ec42SVenkatesh Srinivas if (!callback) { 2206e593bf97STiwei Bie vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 22070ea1e4a6SLadi Prosek if (!vq->event) 2208e593bf97STiwei Bie vq->split.vring.avail->flags = cpu_to_virtio16(vdev, 2209e593bf97STiwei Bie vq->split.avail_flags_shadow); 2210f277ec42SVenkatesh Srinivas } 22110a8a69ddSRusty Russell 2212cbeedb72STiwei Bie vq->split.desc_state = kmalloc_array(vring.num, 2213cbeedb72STiwei Bie sizeof(struct vring_desc_state_split), GFP_KERNEL); 22145bc72234SJason Wang if (!vq->split.desc_state) 22155bc72234SJason Wang goto err_state; 2216cbeedb72STiwei Bie 221772b5e895SJason Wang vq->split.desc_extra = vring_alloc_desc_extra(vq, vring.num); 221872b5e895SJason Wang if (!vq->split.desc_extra) 221972b5e895SJason Wang goto err_extra; 222072b5e895SJason Wang 22210a8a69ddSRusty Russell /* Put everything in free lists. */ 22220a8a69ddSRusty Russell vq->free_head = 0; 2223cbeedb72STiwei Bie memset(vq->split.desc_state, 0, vring.num * 2224cbeedb72STiwei Bie sizeof(struct vring_desc_state_split)); 22250a8a69ddSRusty Russell 22260e566c8fSParav Pandit spin_lock(&vdev->vqs_list_lock); 2227e152d8afSDan Carpenter list_add_tail(&vq->vq.list, &vdev->vqs); 22280e566c8fSParav Pandit spin_unlock(&vdev->vqs_list_lock); 22290a8a69ddSRusty Russell return &vq->vq; 22305bc72234SJason Wang 223172b5e895SJason Wang err_extra: 223272b5e895SJason Wang kfree(vq->split.desc_state); 22335bc72234SJason Wang err_state: 22345bc72234SJason Wang kfree(vq); 22355bc72234SJason Wang return NULL; 22360a8a69ddSRusty Russell } 22372a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(__vring_new_virtqueue); 22382a2d1382SAndy Lutomirski 22392a2d1382SAndy Lutomirski struct virtqueue *vring_create_virtqueue( 22402a2d1382SAndy Lutomirski unsigned int index, 22412a2d1382SAndy Lutomirski unsigned int num, 22422a2d1382SAndy Lutomirski unsigned int vring_align, 22432a2d1382SAndy Lutomirski struct virtio_device *vdev, 22442a2d1382SAndy Lutomirski bool weak_barriers, 22452a2d1382SAndy Lutomirski bool may_reduce_num, 2246f94682ddSMichael S. Tsirkin bool context, 22472a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *), 22482a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *), 22492a2d1382SAndy Lutomirski const char *name) 22502a2d1382SAndy Lutomirski { 22511ce9e605STiwei Bie 22521ce9e605STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) 22531ce9e605STiwei Bie return vring_create_virtqueue_packed(index, num, vring_align, 22541ce9e605STiwei Bie vdev, weak_barriers, may_reduce_num, 22551ce9e605STiwei Bie context, notify, callback, name); 22561ce9e605STiwei Bie 2257d79dca75STiwei Bie return vring_create_virtqueue_split(index, num, vring_align, 2258d79dca75STiwei Bie vdev, weak_barriers, may_reduce_num, 2259d79dca75STiwei Bie context, notify, callback, name); 22602a2d1382SAndy Lutomirski } 22612a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(vring_create_virtqueue); 22622a2d1382SAndy Lutomirski 22631ce9e605STiwei Bie /* Only available for split ring */ 22642a2d1382SAndy Lutomirski struct virtqueue *vring_new_virtqueue(unsigned int index, 22652a2d1382SAndy Lutomirski unsigned int num, 22662a2d1382SAndy Lutomirski unsigned int vring_align, 22672a2d1382SAndy Lutomirski struct virtio_device *vdev, 22682a2d1382SAndy Lutomirski bool weak_barriers, 2269f94682ddSMichael S. Tsirkin bool context, 22702a2d1382SAndy Lutomirski void *pages, 22712a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *vq), 22722a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *vq), 22732a2d1382SAndy Lutomirski const char *name) 22742a2d1382SAndy Lutomirski { 22752a2d1382SAndy Lutomirski struct vring vring; 22761ce9e605STiwei Bie 22771ce9e605STiwei Bie if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) 22781ce9e605STiwei Bie return NULL; 22791ce9e605STiwei Bie 22802a2d1382SAndy Lutomirski vring_init(&vring, num, pages, vring_align); 2281f94682ddSMichael S. Tsirkin return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, 22822a2d1382SAndy Lutomirski notify, callback, name); 22832a2d1382SAndy Lutomirski } 2284c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue); 22850a8a69ddSRusty Russell 22862a2d1382SAndy Lutomirski void vring_del_virtqueue(struct virtqueue *_vq) 22870a8a69ddSRusty Russell { 22882a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 22892a2d1382SAndy Lutomirski 22900e566c8fSParav Pandit spin_lock(&vq->vq.vdev->vqs_list_lock); 2291249f2554SParav Pandit list_del(&_vq->list); 22920e566c8fSParav Pandit spin_unlock(&vq->vq.vdev->vqs_list_lock); 2293249f2554SParav Pandit 22942a2d1382SAndy Lutomirski if (vq->we_own_ring) { 22951ce9e605STiwei Bie if (vq->packed_ring) { 22961ce9e605STiwei Bie vring_free_queue(vq->vq.vdev, 22971ce9e605STiwei Bie vq->packed.ring_size_in_bytes, 22981ce9e605STiwei Bie vq->packed.vring.desc, 22991ce9e605STiwei Bie vq->packed.ring_dma_addr); 23001ce9e605STiwei Bie 23011ce9e605STiwei Bie vring_free_queue(vq->vq.vdev, 23021ce9e605STiwei Bie vq->packed.event_size_in_bytes, 23031ce9e605STiwei Bie vq->packed.vring.driver, 23041ce9e605STiwei Bie vq->packed.driver_event_dma_addr); 23051ce9e605STiwei Bie 23061ce9e605STiwei Bie vring_free_queue(vq->vq.vdev, 23071ce9e605STiwei Bie vq->packed.event_size_in_bytes, 23081ce9e605STiwei Bie vq->packed.vring.device, 23091ce9e605STiwei Bie vq->packed.device_event_dma_addr); 23101ce9e605STiwei Bie 23111ce9e605STiwei Bie kfree(vq->packed.desc_state); 23121ce9e605STiwei Bie kfree(vq->packed.desc_extra); 23131ce9e605STiwei Bie } else { 2314d79dca75STiwei Bie vring_free_queue(vq->vq.vdev, 2315d79dca75STiwei Bie vq->split.queue_size_in_bytes, 2316d79dca75STiwei Bie vq->split.vring.desc, 2317d79dca75STiwei Bie vq->split.queue_dma_addr); 2318f13f09a1SSuman Anna } 2319f13f09a1SSuman Anna } 232072b5e895SJason Wang if (!vq->packed_ring) { 2321cbeedb72STiwei Bie kfree(vq->split.desc_state); 232272b5e895SJason Wang kfree(vq->split.desc_extra); 232372b5e895SJason Wang } 23242a2d1382SAndy Lutomirski kfree(vq); 23250a8a69ddSRusty Russell } 2326c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue); 23270a8a69ddSRusty Russell 2328e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */ 2329e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev) 2330e34f8725SRusty Russell { 2331e34f8725SRusty Russell unsigned int i; 2332e34f8725SRusty Russell 2333e34f8725SRusty Russell for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { 2334e34f8725SRusty Russell switch (i) { 23359fa29b9dSMark McLoughlin case VIRTIO_RING_F_INDIRECT_DESC: 23369fa29b9dSMark McLoughlin break; 2337a5c262c5SMichael S. Tsirkin case VIRTIO_RING_F_EVENT_IDX: 2338a5c262c5SMichael S. Tsirkin break; 2339747ae34aSMichael S. Tsirkin case VIRTIO_F_VERSION_1: 2340747ae34aSMichael S. Tsirkin break; 2341321bd212SMichael S. Tsirkin case VIRTIO_F_ACCESS_PLATFORM: 23421a937693SMichael S. Tsirkin break; 2343f959a128STiwei Bie case VIRTIO_F_RING_PACKED: 2344f959a128STiwei Bie break; 234545383fb0STiwei Bie case VIRTIO_F_ORDER_PLATFORM: 234645383fb0STiwei Bie break; 2347e34f8725SRusty Russell default: 2348e34f8725SRusty Russell /* We don't understand this bit. */ 2349e16e12beSMichael S. Tsirkin __virtio_clear_bit(vdev, i); 2350e34f8725SRusty Russell } 2351e34f8725SRusty Russell } 2352e34f8725SRusty Russell } 2353e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features); 2354e34f8725SRusty Russell 23555dfc1762SRusty Russell /** 23565dfc1762SRusty Russell * virtqueue_get_vring_size - return the size of the virtqueue's vring 2357a5581206SJiang Biao * @_vq: the struct virtqueue containing the vring of interest. 23585dfc1762SRusty Russell * 23595dfc1762SRusty Russell * Returns the size of the vring. This is mainly used for boasting to 23605dfc1762SRusty Russell * userspace. Unlike other operations, this need not be serialized. 23615dfc1762SRusty Russell */ 23628f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) 23638f9f4668SRick Jones { 23648f9f4668SRick Jones 23658f9f4668SRick Jones struct vring_virtqueue *vq = to_vvq(_vq); 23668f9f4668SRick Jones 23671ce9e605STiwei Bie return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num; 23688f9f4668SRick Jones } 23698f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); 23708f9f4668SRick Jones 2371b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq) 2372b3b32c94SHeinz Graalfs { 2373b3b32c94SHeinz Graalfs struct vring_virtqueue *vq = to_vvq(_vq); 2374b3b32c94SHeinz Graalfs 237560f07798SParav Pandit return READ_ONCE(vq->broken); 2376b3b32c94SHeinz Graalfs } 2377b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken); 2378b3b32c94SHeinz Graalfs 2379e2dcdfe9SRusty Russell /* 2380e2dcdfe9SRusty Russell * This should prevent the device from being used, allowing drivers to 2381e2dcdfe9SRusty Russell * recover. You may need to grab appropriate locks to flush. 2382e2dcdfe9SRusty Russell */ 2383e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev) 2384e2dcdfe9SRusty Russell { 2385e2dcdfe9SRusty Russell struct virtqueue *_vq; 2386e2dcdfe9SRusty Russell 23870e566c8fSParav Pandit spin_lock(&dev->vqs_list_lock); 2388e2dcdfe9SRusty Russell list_for_each_entry(_vq, &dev->vqs, list) { 2389e2dcdfe9SRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 239060f07798SParav Pandit 239160f07798SParav Pandit /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ 239260f07798SParav Pandit WRITE_ONCE(vq->broken, true); 2393e2dcdfe9SRusty Russell } 23940e566c8fSParav Pandit spin_unlock(&dev->vqs_list_lock); 2395e2dcdfe9SRusty Russell } 2396e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device); 2397e2dcdfe9SRusty Russell 23982a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq) 239989062652SCornelia Huck { 240089062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 240189062652SCornelia Huck 24022a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 240389062652SCornelia Huck 24041ce9e605STiwei Bie if (vq->packed_ring) 24051ce9e605STiwei Bie return vq->packed.ring_dma_addr; 24061ce9e605STiwei Bie 2407d79dca75STiwei Bie return vq->split.queue_dma_addr; 24082a2d1382SAndy Lutomirski } 24092a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr); 24102a2d1382SAndy Lutomirski 24112a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq) 241289062652SCornelia Huck { 241389062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 241489062652SCornelia Huck 24152a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 24162a2d1382SAndy Lutomirski 24171ce9e605STiwei Bie if (vq->packed_ring) 24181ce9e605STiwei Bie return vq->packed.driver_event_dma_addr; 24191ce9e605STiwei Bie 2420d79dca75STiwei Bie return vq->split.queue_dma_addr + 2421e593bf97STiwei Bie ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc); 242289062652SCornelia Huck } 24232a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr); 24242a2d1382SAndy Lutomirski 24252a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq) 24262a2d1382SAndy Lutomirski { 24272a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 24282a2d1382SAndy Lutomirski 24292a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 24302a2d1382SAndy Lutomirski 24311ce9e605STiwei Bie if (vq->packed_ring) 24321ce9e605STiwei Bie return vq->packed.device_event_dma_addr; 24331ce9e605STiwei Bie 2434d79dca75STiwei Bie return vq->split.queue_dma_addr + 2435e593bf97STiwei Bie ((char *)vq->split.vring.used - (char *)vq->split.vring.desc); 24362a2d1382SAndy Lutomirski } 24372a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_used_addr); 24382a2d1382SAndy Lutomirski 24391ce9e605STiwei Bie /* Only available for split ring */ 24402a2d1382SAndy Lutomirski const struct vring *virtqueue_get_vring(struct virtqueue *vq) 24412a2d1382SAndy Lutomirski { 2442e593bf97STiwei Bie return &to_vvq(vq)->split.vring; 24432a2d1382SAndy Lutomirski } 24442a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_vring); 244589062652SCornelia Huck 2446c6fd4701SRusty Russell MODULE_LICENSE("GPL"); 2447