10a8a69ddSRusty Russell /* Virtio ring implementation. 20a8a69ddSRusty Russell * 30a8a69ddSRusty Russell * Copyright 2007 Rusty Russell IBM Corporation 40a8a69ddSRusty Russell * 50a8a69ddSRusty Russell * This program is free software; you can redistribute it and/or modify 60a8a69ddSRusty Russell * it under the terms of the GNU General Public License as published by 70a8a69ddSRusty Russell * the Free Software Foundation; either version 2 of the License, or 80a8a69ddSRusty Russell * (at your option) any later version. 90a8a69ddSRusty Russell * 100a8a69ddSRusty Russell * This program is distributed in the hope that it will be useful, 110a8a69ddSRusty Russell * but WITHOUT ANY WARRANTY; without even the implied warranty of 120a8a69ddSRusty Russell * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 130a8a69ddSRusty Russell * GNU General Public License for more details. 140a8a69ddSRusty Russell * 150a8a69ddSRusty Russell * You should have received a copy of the GNU General Public License 160a8a69ddSRusty Russell * along with this program; if not, write to the Free Software 170a8a69ddSRusty Russell * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 180a8a69ddSRusty Russell */ 190a8a69ddSRusty Russell #include <linux/virtio.h> 200a8a69ddSRusty Russell #include <linux/virtio_ring.h> 21e34f8725SRusty Russell #include <linux/virtio_config.h> 220a8a69ddSRusty Russell #include <linux/device.h> 235a0e3ad6STejun Heo #include <linux/slab.h> 24b5a2c4f1SPaul Gortmaker #include <linux/module.h> 25e93300b1SRusty Russell #include <linux/hrtimer.h> 26780bc790SAndy Lutomirski #include <linux/dma-mapping.h> 2778fe3987SAndy Lutomirski #include <xen/xen.h> 280a8a69ddSRusty Russell 290a8a69ddSRusty Russell #ifdef DEBUG 300a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */ 319499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 329499f5e7SRusty Russell do { \ 339499f5e7SRusty Russell dev_err(&(_vq)->vq.vdev->dev, \ 349499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 359499f5e7SRusty Russell BUG(); \ 369499f5e7SRusty Russell } while (0) 37c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */ 383a35ce7dSRoel Kluin #define START_USE(_vq) \ 39c5f841f1SRusty Russell do { \ 40c5f841f1SRusty Russell if ((_vq)->in_use) \ 419499f5e7SRusty Russell panic("%s:in_use = %i\n", \ 429499f5e7SRusty Russell (_vq)->vq.name, (_vq)->in_use); \ 43c5f841f1SRusty Russell (_vq)->in_use = __LINE__; \ 44c5f841f1SRusty Russell } while (0) 453a35ce7dSRoel Kluin #define END_USE(_vq) \ 4697a545abSRusty Russell do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) 470a8a69ddSRusty Russell #else 489499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 499499f5e7SRusty Russell do { \ 509499f5e7SRusty Russell dev_err(&_vq->vq.vdev->dev, \ 519499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 529499f5e7SRusty Russell (_vq)->broken = true; \ 539499f5e7SRusty Russell } while (0) 540a8a69ddSRusty Russell #define START_USE(vq) 550a8a69ddSRusty Russell #define END_USE(vq) 560a8a69ddSRusty Russell #endif 570a8a69ddSRusty Russell 58780bc790SAndy Lutomirski struct vring_desc_state { 59780bc790SAndy Lutomirski void *data; /* Data for callback. */ 60780bc790SAndy Lutomirski struct vring_desc *indir_desc; /* Indirect descriptor, if any. */ 61780bc790SAndy Lutomirski }; 62780bc790SAndy Lutomirski 6343b4f721SMichael S. Tsirkin struct vring_virtqueue { 640a8a69ddSRusty Russell struct virtqueue vq; 650a8a69ddSRusty Russell 660a8a69ddSRusty Russell /* Actual memory layout for this queue */ 670a8a69ddSRusty Russell struct vring vring; 680a8a69ddSRusty Russell 697b21e34fSRusty Russell /* Can we use weak barriers? */ 707b21e34fSRusty Russell bool weak_barriers; 717b21e34fSRusty Russell 720a8a69ddSRusty Russell /* Other side has made a mess, don't try any more. */ 730a8a69ddSRusty Russell bool broken; 740a8a69ddSRusty Russell 759fa29b9dSMark McLoughlin /* Host supports indirect buffers */ 769fa29b9dSMark McLoughlin bool indirect; 779fa29b9dSMark McLoughlin 78a5c262c5SMichael S. Tsirkin /* Host publishes avail event idx */ 79a5c262c5SMichael S. Tsirkin bool event; 80a5c262c5SMichael S. Tsirkin 810a8a69ddSRusty Russell /* Head of free buffer list. */ 820a8a69ddSRusty Russell unsigned int free_head; 830a8a69ddSRusty Russell /* Number we've added since last sync. */ 840a8a69ddSRusty Russell unsigned int num_added; 850a8a69ddSRusty Russell 860a8a69ddSRusty Russell /* Last used index we've seen. */ 871bc4953eSAnthony Liguori u16 last_used_idx; 880a8a69ddSRusty Russell 89f277ec42SVenkatesh Srinivas /* Last written value to avail->flags */ 90f277ec42SVenkatesh Srinivas u16 avail_flags_shadow; 91f277ec42SVenkatesh Srinivas 92f277ec42SVenkatesh Srinivas /* Last written value to avail->idx in guest byte order */ 93f277ec42SVenkatesh Srinivas u16 avail_idx_shadow; 94f277ec42SVenkatesh Srinivas 950a8a69ddSRusty Russell /* How to notify other side. FIXME: commonalize hcalls! */ 9646f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *vq); 970a8a69ddSRusty Russell 982a2d1382SAndy Lutomirski /* DMA, allocation, and size information */ 992a2d1382SAndy Lutomirski bool we_own_ring; 1002a2d1382SAndy Lutomirski size_t queue_size_in_bytes; 1012a2d1382SAndy Lutomirski dma_addr_t queue_dma_addr; 1022a2d1382SAndy Lutomirski 1030a8a69ddSRusty Russell #ifdef DEBUG 1040a8a69ddSRusty Russell /* They're supposed to lock for us. */ 1050a8a69ddSRusty Russell unsigned int in_use; 106e93300b1SRusty Russell 107e93300b1SRusty Russell /* Figure out if their kicks are too delayed. */ 108e93300b1SRusty Russell bool last_add_time_valid; 109e93300b1SRusty Russell ktime_t last_add_time; 1100a8a69ddSRusty Russell #endif 1110a8a69ddSRusty Russell 112780bc790SAndy Lutomirski /* Per-descriptor state. */ 113780bc790SAndy Lutomirski struct vring_desc_state desc_state[]; 1140a8a69ddSRusty Russell }; 1150a8a69ddSRusty Russell 1160a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) 1170a8a69ddSRusty Russell 118d26c96c8SAndy Lutomirski /* 1191a937693SMichael S. Tsirkin * Modern virtio devices have feature bits to specify whether they need a 1201a937693SMichael S. Tsirkin * quirk and bypass the IOMMU. If not there, just use the DMA API. 1211a937693SMichael S. Tsirkin * 1221a937693SMichael S. Tsirkin * If there, the interaction between virtio and DMA API is messy. 123d26c96c8SAndy Lutomirski * 124d26c96c8SAndy Lutomirski * On most systems with virtio, physical addresses match bus addresses, 125d26c96c8SAndy Lutomirski * and it doesn't particularly matter whether we use the DMA API. 126d26c96c8SAndy Lutomirski * 127d26c96c8SAndy Lutomirski * On some systems, including Xen and any system with a physical device 128d26c96c8SAndy Lutomirski * that speaks virtio behind a physical IOMMU, we must use the DMA API 129d26c96c8SAndy Lutomirski * for virtio DMA to work at all. 130d26c96c8SAndy Lutomirski * 131d26c96c8SAndy Lutomirski * On other systems, including SPARC and PPC64, virtio-pci devices are 132d26c96c8SAndy Lutomirski * enumerated as though they are behind an IOMMU, but the virtio host 133d26c96c8SAndy Lutomirski * ignores the IOMMU, so we must either pretend that the IOMMU isn't 134d26c96c8SAndy Lutomirski * there or somehow map everything as the identity. 135d26c96c8SAndy Lutomirski * 136d26c96c8SAndy Lutomirski * For the time being, we preserve historic behavior and bypass the DMA 137d26c96c8SAndy Lutomirski * API. 1381a937693SMichael S. Tsirkin * 1391a937693SMichael S. Tsirkin * TODO: install a per-device DMA ops structure that does the right thing 1401a937693SMichael S. Tsirkin * taking into account all the above quirks, and use the DMA API 1411a937693SMichael S. Tsirkin * unconditionally on data path. 142d26c96c8SAndy Lutomirski */ 143d26c96c8SAndy Lutomirski 144d26c96c8SAndy Lutomirski static bool vring_use_dma_api(struct virtio_device *vdev) 145d26c96c8SAndy Lutomirski { 1461a937693SMichael S. Tsirkin if (!virtio_has_iommu_quirk(vdev)) 1471a937693SMichael S. Tsirkin return true; 1481a937693SMichael S. Tsirkin 1491a937693SMichael S. Tsirkin /* Otherwise, we are left to guess. */ 15078fe3987SAndy Lutomirski /* 15178fe3987SAndy Lutomirski * In theory, it's possible to have a buggy QEMU-supposed 15278fe3987SAndy Lutomirski * emulated Q35 IOMMU and Xen enabled at the same time. On 15378fe3987SAndy Lutomirski * such a configuration, virtio has never worked and will 15478fe3987SAndy Lutomirski * not work without an even larger kludge. Instead, enable 15578fe3987SAndy Lutomirski * the DMA API if we're a Xen guest, which at least allows 15678fe3987SAndy Lutomirski * all of the sensible Xen configurations to work correctly. 15778fe3987SAndy Lutomirski */ 15878fe3987SAndy Lutomirski if (xen_domain()) 15978fe3987SAndy Lutomirski return true; 16078fe3987SAndy Lutomirski 161d26c96c8SAndy Lutomirski return false; 162d26c96c8SAndy Lutomirski } 163d26c96c8SAndy Lutomirski 164780bc790SAndy Lutomirski /* 165780bc790SAndy Lutomirski * The DMA ops on various arches are rather gnarly right now, and 166780bc790SAndy Lutomirski * making all of the arch DMA ops work on the vring device itself 167780bc790SAndy Lutomirski * is a mess. For now, we use the parent device for DMA ops. 168780bc790SAndy Lutomirski */ 16975bfa81bSMichael S. Tsirkin static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq) 170780bc790SAndy Lutomirski { 171780bc790SAndy Lutomirski return vq->vq.vdev->dev.parent; 172780bc790SAndy Lutomirski } 173780bc790SAndy Lutomirski 174780bc790SAndy Lutomirski /* Map one sg entry. */ 175780bc790SAndy Lutomirski static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq, 176780bc790SAndy Lutomirski struct scatterlist *sg, 177780bc790SAndy Lutomirski enum dma_data_direction direction) 178780bc790SAndy Lutomirski { 179780bc790SAndy Lutomirski if (!vring_use_dma_api(vq->vq.vdev)) 180780bc790SAndy Lutomirski return (dma_addr_t)sg_phys(sg); 181780bc790SAndy Lutomirski 182780bc790SAndy Lutomirski /* 183780bc790SAndy Lutomirski * We can't use dma_map_sg, because we don't use scatterlists in 184780bc790SAndy Lutomirski * the way it expects (we don't guarantee that the scatterlist 185780bc790SAndy Lutomirski * will exist for the lifetime of the mapping). 186780bc790SAndy Lutomirski */ 187780bc790SAndy Lutomirski return dma_map_page(vring_dma_dev(vq), 188780bc790SAndy Lutomirski sg_page(sg), sg->offset, sg->length, 189780bc790SAndy Lutomirski direction); 190780bc790SAndy Lutomirski } 191780bc790SAndy Lutomirski 192780bc790SAndy Lutomirski static dma_addr_t vring_map_single(const struct vring_virtqueue *vq, 193780bc790SAndy Lutomirski void *cpu_addr, size_t size, 194780bc790SAndy Lutomirski enum dma_data_direction direction) 195780bc790SAndy Lutomirski { 196780bc790SAndy Lutomirski if (!vring_use_dma_api(vq->vq.vdev)) 197780bc790SAndy Lutomirski return (dma_addr_t)virt_to_phys(cpu_addr); 198780bc790SAndy Lutomirski 199780bc790SAndy Lutomirski return dma_map_single(vring_dma_dev(vq), 200780bc790SAndy Lutomirski cpu_addr, size, direction); 201780bc790SAndy Lutomirski } 202780bc790SAndy Lutomirski 203*138fd251STiwei Bie static void vring_unmap_one_split(const struct vring_virtqueue *vq, 204780bc790SAndy Lutomirski struct vring_desc *desc) 205780bc790SAndy Lutomirski { 206780bc790SAndy Lutomirski u16 flags; 207780bc790SAndy Lutomirski 208780bc790SAndy Lutomirski if (!vring_use_dma_api(vq->vq.vdev)) 209780bc790SAndy Lutomirski return; 210780bc790SAndy Lutomirski 211780bc790SAndy Lutomirski flags = virtio16_to_cpu(vq->vq.vdev, desc->flags); 212780bc790SAndy Lutomirski 213780bc790SAndy Lutomirski if (flags & VRING_DESC_F_INDIRECT) { 214780bc790SAndy Lutomirski dma_unmap_single(vring_dma_dev(vq), 215780bc790SAndy Lutomirski virtio64_to_cpu(vq->vq.vdev, desc->addr), 216780bc790SAndy Lutomirski virtio32_to_cpu(vq->vq.vdev, desc->len), 217780bc790SAndy Lutomirski (flags & VRING_DESC_F_WRITE) ? 218780bc790SAndy Lutomirski DMA_FROM_DEVICE : DMA_TO_DEVICE); 219780bc790SAndy Lutomirski } else { 220780bc790SAndy Lutomirski dma_unmap_page(vring_dma_dev(vq), 221780bc790SAndy Lutomirski virtio64_to_cpu(vq->vq.vdev, desc->addr), 222780bc790SAndy Lutomirski virtio32_to_cpu(vq->vq.vdev, desc->len), 223780bc790SAndy Lutomirski (flags & VRING_DESC_F_WRITE) ? 224780bc790SAndy Lutomirski DMA_FROM_DEVICE : DMA_TO_DEVICE); 225780bc790SAndy Lutomirski } 226780bc790SAndy Lutomirski } 227780bc790SAndy Lutomirski 228780bc790SAndy Lutomirski static int vring_mapping_error(const struct vring_virtqueue *vq, 229780bc790SAndy Lutomirski dma_addr_t addr) 230780bc790SAndy Lutomirski { 231780bc790SAndy Lutomirski if (!vring_use_dma_api(vq->vq.vdev)) 232780bc790SAndy Lutomirski return 0; 233780bc790SAndy Lutomirski 234780bc790SAndy Lutomirski return dma_mapping_error(vring_dma_dev(vq), addr); 235780bc790SAndy Lutomirski } 236780bc790SAndy Lutomirski 237*138fd251STiwei Bie static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq, 238*138fd251STiwei Bie unsigned int total_sg, 239*138fd251STiwei Bie gfp_t gfp) 2409fa29b9dSMark McLoughlin { 2419fa29b9dSMark McLoughlin struct vring_desc *desc; 242b25bd251SRusty Russell unsigned int i; 2439fa29b9dSMark McLoughlin 244b92b1b89SWill Deacon /* 245b92b1b89SWill Deacon * We require lowmem mappings for the descriptors because 246b92b1b89SWill Deacon * otherwise virt_to_phys will give us bogus addresses in the 247b92b1b89SWill Deacon * virtqueue. 248b92b1b89SWill Deacon */ 24982107539SMichal Hocko gfp &= ~__GFP_HIGHMEM; 250b92b1b89SWill Deacon 2516da2ec56SKees Cook desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp); 2529fa29b9dSMark McLoughlin if (!desc) 253b25bd251SRusty Russell return NULL; 2549fa29b9dSMark McLoughlin 255b25bd251SRusty Russell for (i = 0; i < total_sg; i++) 25600e6f3d9SMichael S. Tsirkin desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); 257b25bd251SRusty Russell return desc; 2589fa29b9dSMark McLoughlin } 2599fa29b9dSMark McLoughlin 260*138fd251STiwei Bie static inline int virtqueue_add_split(struct virtqueue *_vq, 26113816c76SRusty Russell struct scatterlist *sgs[], 262eeebf9b1SRusty Russell unsigned int total_sg, 26313816c76SRusty Russell unsigned int out_sgs, 26413816c76SRusty Russell unsigned int in_sgs, 265bbd603efSMichael S. Tsirkin void *data, 2665a08b04fSMichael S. Tsirkin void *ctx, 267bbd603efSMichael S. Tsirkin gfp_t gfp) 2680a8a69ddSRusty Russell { 2690a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 27013816c76SRusty Russell struct scatterlist *sg; 271b25bd251SRusty Russell struct vring_desc *desc; 272780bc790SAndy Lutomirski unsigned int i, n, avail, descs_used, uninitialized_var(prev), err_idx; 2731fe9b6feSMichael S. Tsirkin int head; 274b25bd251SRusty Russell bool indirect; 2750a8a69ddSRusty Russell 2769fa29b9dSMark McLoughlin START_USE(vq); 2779fa29b9dSMark McLoughlin 2780a8a69ddSRusty Russell BUG_ON(data == NULL); 2795a08b04fSMichael S. Tsirkin BUG_ON(ctx && vq->indirect); 2809fa29b9dSMark McLoughlin 28170670444SRusty Russell if (unlikely(vq->broken)) { 28270670444SRusty Russell END_USE(vq); 28370670444SRusty Russell return -EIO; 28470670444SRusty Russell } 28570670444SRusty Russell 286e93300b1SRusty Russell #ifdef DEBUG 287e93300b1SRusty Russell { 288e93300b1SRusty Russell ktime_t now = ktime_get(); 289e93300b1SRusty Russell 290e93300b1SRusty Russell /* No kick or get, with .1 second between? Warn. */ 291e93300b1SRusty Russell if (vq->last_add_time_valid) 292e93300b1SRusty Russell WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time)) 293e93300b1SRusty Russell > 100); 294e93300b1SRusty Russell vq->last_add_time = now; 295e93300b1SRusty Russell vq->last_add_time_valid = true; 296e93300b1SRusty Russell } 297e93300b1SRusty Russell #endif 298e93300b1SRusty Russell 29913816c76SRusty Russell BUG_ON(total_sg == 0); 3000a8a69ddSRusty Russell 301b25bd251SRusty Russell head = vq->free_head; 302b25bd251SRusty Russell 303b25bd251SRusty Russell /* If the host supports indirect descriptor tables, and we have multiple 304b25bd251SRusty Russell * buffers, then go indirect. FIXME: tune this threshold */ 305b25bd251SRusty Russell if (vq->indirect && total_sg > 1 && vq->vq.num_free) 306*138fd251STiwei Bie desc = alloc_indirect_split(_vq, total_sg, gfp); 30744ed8089SRichard W.M. Jones else { 308b25bd251SRusty Russell desc = NULL; 30944ed8089SRichard W.M. Jones WARN_ON_ONCE(total_sg > vq->vring.num && !vq->indirect); 31044ed8089SRichard W.M. Jones } 311b25bd251SRusty Russell 312b25bd251SRusty Russell if (desc) { 313b25bd251SRusty Russell /* Use a single buffer which doesn't continue */ 314780bc790SAndy Lutomirski indirect = true; 315b25bd251SRusty Russell /* Set up rest to use this indirect table. */ 316b25bd251SRusty Russell i = 0; 317b25bd251SRusty Russell descs_used = 1; 318b25bd251SRusty Russell } else { 319780bc790SAndy Lutomirski indirect = false; 320b25bd251SRusty Russell desc = vq->vring.desc; 321b25bd251SRusty Russell i = head; 322b25bd251SRusty Russell descs_used = total_sg; 323b25bd251SRusty Russell } 324b25bd251SRusty Russell 325b25bd251SRusty Russell if (vq->vq.num_free < descs_used) { 3260a8a69ddSRusty Russell pr_debug("Can't add buf len %i - avail = %i\n", 327b25bd251SRusty Russell descs_used, vq->vq.num_free); 32844653eaeSRusty Russell /* FIXME: for historical reasons, we force a notify here if 32944653eaeSRusty Russell * there are outgoing parts to the buffer. Presumably the 33044653eaeSRusty Russell * host should service the ring ASAP. */ 33113816c76SRusty Russell if (out_sgs) 332426e3e0aSRusty Russell vq->notify(&vq->vq); 33358625edfSWei Yongjun if (indirect) 33458625edfSWei Yongjun kfree(desc); 3350a8a69ddSRusty Russell END_USE(vq); 3360a8a69ddSRusty Russell return -ENOSPC; 3370a8a69ddSRusty Russell } 3380a8a69ddSRusty Russell 33913816c76SRusty Russell for (n = 0; n < out_sgs; n++) { 340eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 341780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE); 342780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 343780bc790SAndy Lutomirski goto unmap_release; 344780bc790SAndy Lutomirski 34500e6f3d9SMichael S. Tsirkin desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT); 346780bc790SAndy Lutomirski desc[i].addr = cpu_to_virtio64(_vq->vdev, addr); 34700e6f3d9SMichael S. Tsirkin desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); 3480a8a69ddSRusty Russell prev = i; 34900e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(_vq->vdev, desc[i].next); 3500a8a69ddSRusty Russell } 35113816c76SRusty Russell } 35213816c76SRusty Russell for (; n < (out_sgs + in_sgs); n++) { 353eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 354780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE); 355780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 356780bc790SAndy Lutomirski goto unmap_release; 357780bc790SAndy Lutomirski 35800e6f3d9SMichael S. Tsirkin desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE); 359780bc790SAndy Lutomirski desc[i].addr = cpu_to_virtio64(_vq->vdev, addr); 36000e6f3d9SMichael S. Tsirkin desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); 3610a8a69ddSRusty Russell prev = i; 36200e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(_vq->vdev, desc[i].next); 36313816c76SRusty Russell } 3640a8a69ddSRusty Russell } 3650a8a69ddSRusty Russell /* Last one doesn't continue. */ 36600e6f3d9SMichael S. Tsirkin desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); 3670a8a69ddSRusty Russell 368780bc790SAndy Lutomirski if (indirect) { 369780bc790SAndy Lutomirski /* Now that the indirect table is filled in, map it. */ 370780bc790SAndy Lutomirski dma_addr_t addr = vring_map_single( 371780bc790SAndy Lutomirski vq, desc, total_sg * sizeof(struct vring_desc), 372780bc790SAndy Lutomirski DMA_TO_DEVICE); 373780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 374780bc790SAndy Lutomirski goto unmap_release; 375780bc790SAndy Lutomirski 376780bc790SAndy Lutomirski vq->vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_INDIRECT); 377780bc790SAndy Lutomirski vq->vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, addr); 378780bc790SAndy Lutomirski 379780bc790SAndy Lutomirski vq->vring.desc[head].len = cpu_to_virtio32(_vq->vdev, total_sg * sizeof(struct vring_desc)); 380780bc790SAndy Lutomirski } 381780bc790SAndy Lutomirski 382780bc790SAndy Lutomirski /* We're using some buffers from the free list. */ 383780bc790SAndy Lutomirski vq->vq.num_free -= descs_used; 384780bc790SAndy Lutomirski 3850a8a69ddSRusty Russell /* Update free pointer */ 386b25bd251SRusty Russell if (indirect) 38700e6f3d9SMichael S. Tsirkin vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next); 388b25bd251SRusty Russell else 3890a8a69ddSRusty Russell vq->free_head = i; 3900a8a69ddSRusty Russell 391780bc790SAndy Lutomirski /* Store token and indirect buffer state. */ 392780bc790SAndy Lutomirski vq->desc_state[head].data = data; 393780bc790SAndy Lutomirski if (indirect) 394780bc790SAndy Lutomirski vq->desc_state[head].indir_desc = desc; 39587646a34SJason Wang else 3965a08b04fSMichael S. Tsirkin vq->desc_state[head].indir_desc = ctx; 3970a8a69ddSRusty Russell 3980a8a69ddSRusty Russell /* Put entry in available array (but don't update avail->idx until they 3993b720b8cSRusty Russell * do sync). */ 400f277ec42SVenkatesh Srinivas avail = vq->avail_idx_shadow & (vq->vring.num - 1); 40100e6f3d9SMichael S. Tsirkin vq->vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); 4020a8a69ddSRusty Russell 403ee7cd898SRusty Russell /* Descriptors and available array need to be set before we expose the 404ee7cd898SRusty Russell * new available array entries. */ 405a9a0fef7SRusty Russell virtio_wmb(vq->weak_barriers); 406f277ec42SVenkatesh Srinivas vq->avail_idx_shadow++; 407f277ec42SVenkatesh Srinivas vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow); 408ee7cd898SRusty Russell vq->num_added++; 409ee7cd898SRusty Russell 4105e05bf58STetsuo Handa pr_debug("Added buffer head %i to %p\n", head, vq); 4115e05bf58STetsuo Handa END_USE(vq); 4125e05bf58STetsuo Handa 413ee7cd898SRusty Russell /* This is very unlikely, but theoretically possible. Kick 414ee7cd898SRusty Russell * just in case. */ 415ee7cd898SRusty Russell if (unlikely(vq->num_added == (1 << 16) - 1)) 416ee7cd898SRusty Russell virtqueue_kick(_vq); 417ee7cd898SRusty Russell 41898e8c6bcSRusty Russell return 0; 419780bc790SAndy Lutomirski 420780bc790SAndy Lutomirski unmap_release: 421780bc790SAndy Lutomirski err_idx = i; 422780bc790SAndy Lutomirski i = head; 423780bc790SAndy Lutomirski 424780bc790SAndy Lutomirski for (n = 0; n < total_sg; n++) { 425780bc790SAndy Lutomirski if (i == err_idx) 426780bc790SAndy Lutomirski break; 427*138fd251STiwei Bie vring_unmap_one_split(vq, &desc[i]); 428c60923cbSGonglei i = virtio16_to_cpu(_vq->vdev, vq->vring.desc[i].next); 429780bc790SAndy Lutomirski } 430780bc790SAndy Lutomirski 431780bc790SAndy Lutomirski if (indirect) 432780bc790SAndy Lutomirski kfree(desc); 433780bc790SAndy Lutomirski 4343cc36f6eSMichael S. Tsirkin END_USE(vq); 435780bc790SAndy Lutomirski return -EIO; 4360a8a69ddSRusty Russell } 43713816c76SRusty Russell 438*138fd251STiwei Bie static inline int virtqueue_add(struct virtqueue *_vq, 439*138fd251STiwei Bie struct scatterlist *sgs[], 440*138fd251STiwei Bie unsigned int total_sg, 441*138fd251STiwei Bie unsigned int out_sgs, 442*138fd251STiwei Bie unsigned int in_sgs, 443*138fd251STiwei Bie void *data, 444*138fd251STiwei Bie void *ctx, 445*138fd251STiwei Bie gfp_t gfp) 446*138fd251STiwei Bie { 447*138fd251STiwei Bie return virtqueue_add_split(_vq, sgs, total_sg, 448*138fd251STiwei Bie out_sgs, in_sgs, data, ctx, gfp); 449*138fd251STiwei Bie } 450*138fd251STiwei Bie 45113816c76SRusty Russell /** 45213816c76SRusty Russell * virtqueue_add_sgs - expose buffers to other end 45313816c76SRusty Russell * @vq: the struct virtqueue we're talking about. 45413816c76SRusty Russell * @sgs: array of terminated scatterlists. 45513816c76SRusty Russell * @out_num: the number of scatterlists readable by other side 45613816c76SRusty Russell * @in_num: the number of scatterlists which are writable (after readable ones) 45713816c76SRusty Russell * @data: the token identifying the buffer. 45813816c76SRusty Russell * @gfp: how to do memory allocations (if necessary). 45913816c76SRusty Russell * 46013816c76SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 46113816c76SRusty Russell * at the same time (except where noted). 46213816c76SRusty Russell * 46370670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 46413816c76SRusty Russell */ 46513816c76SRusty Russell int virtqueue_add_sgs(struct virtqueue *_vq, 46613816c76SRusty Russell struct scatterlist *sgs[], 46713816c76SRusty Russell unsigned int out_sgs, 46813816c76SRusty Russell unsigned int in_sgs, 46913816c76SRusty Russell void *data, 47013816c76SRusty Russell gfp_t gfp) 47113816c76SRusty Russell { 472eeebf9b1SRusty Russell unsigned int i, total_sg = 0; 47313816c76SRusty Russell 47413816c76SRusty Russell /* Count them first. */ 475eeebf9b1SRusty Russell for (i = 0; i < out_sgs + in_sgs; i++) { 47613816c76SRusty Russell struct scatterlist *sg; 47713816c76SRusty Russell for (sg = sgs[i]; sg; sg = sg_next(sg)) 478eeebf9b1SRusty Russell total_sg++; 47913816c76SRusty Russell } 4805a08b04fSMichael S. Tsirkin return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, 4815a08b04fSMichael S. Tsirkin data, NULL, gfp); 48213816c76SRusty Russell } 48313816c76SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_sgs); 48413816c76SRusty Russell 48513816c76SRusty Russell /** 486282edb36SRusty Russell * virtqueue_add_outbuf - expose output buffers to other end 487282edb36SRusty Russell * @vq: the struct virtqueue we're talking about. 488eeebf9b1SRusty Russell * @sg: scatterlist (must be well-formed and terminated!) 489eeebf9b1SRusty Russell * @num: the number of entries in @sg readable by other side 490282edb36SRusty Russell * @data: the token identifying the buffer. 491282edb36SRusty Russell * @gfp: how to do memory allocations (if necessary). 492282edb36SRusty Russell * 493282edb36SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 494282edb36SRusty Russell * at the same time (except where noted). 495282edb36SRusty Russell * 49670670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 497282edb36SRusty Russell */ 498282edb36SRusty Russell int virtqueue_add_outbuf(struct virtqueue *vq, 499eeebf9b1SRusty Russell struct scatterlist *sg, unsigned int num, 500282edb36SRusty Russell void *data, 501282edb36SRusty Russell gfp_t gfp) 502282edb36SRusty Russell { 5035a08b04fSMichael S. Tsirkin return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp); 504282edb36SRusty Russell } 505282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); 506282edb36SRusty Russell 507282edb36SRusty Russell /** 508282edb36SRusty Russell * virtqueue_add_inbuf - expose input buffers to other end 509282edb36SRusty Russell * @vq: the struct virtqueue we're talking about. 510eeebf9b1SRusty Russell * @sg: scatterlist (must be well-formed and terminated!) 511eeebf9b1SRusty Russell * @num: the number of entries in @sg writable by other side 512282edb36SRusty Russell * @data: the token identifying the buffer. 513282edb36SRusty Russell * @gfp: how to do memory allocations (if necessary). 514282edb36SRusty Russell * 515282edb36SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 516282edb36SRusty Russell * at the same time (except where noted). 517282edb36SRusty Russell * 51870670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 519282edb36SRusty Russell */ 520282edb36SRusty Russell int virtqueue_add_inbuf(struct virtqueue *vq, 521eeebf9b1SRusty Russell struct scatterlist *sg, unsigned int num, 522282edb36SRusty Russell void *data, 523282edb36SRusty Russell gfp_t gfp) 524282edb36SRusty Russell { 5255a08b04fSMichael S. Tsirkin return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp); 526282edb36SRusty Russell } 527282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); 528282edb36SRusty Russell 529282edb36SRusty Russell /** 5305a08b04fSMichael S. Tsirkin * virtqueue_add_inbuf_ctx - expose input buffers to other end 5315a08b04fSMichael S. Tsirkin * @vq: the struct virtqueue we're talking about. 5325a08b04fSMichael S. Tsirkin * @sg: scatterlist (must be well-formed and terminated!) 5335a08b04fSMichael S. Tsirkin * @num: the number of entries in @sg writable by other side 5345a08b04fSMichael S. Tsirkin * @data: the token identifying the buffer. 5355a08b04fSMichael S. Tsirkin * @ctx: extra context for the token 5365a08b04fSMichael S. Tsirkin * @gfp: how to do memory allocations (if necessary). 5375a08b04fSMichael S. Tsirkin * 5385a08b04fSMichael S. Tsirkin * Caller must ensure we don't call this with other virtqueue operations 5395a08b04fSMichael S. Tsirkin * at the same time (except where noted). 5405a08b04fSMichael S. Tsirkin * 5415a08b04fSMichael S. Tsirkin * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 5425a08b04fSMichael S. Tsirkin */ 5435a08b04fSMichael S. Tsirkin int virtqueue_add_inbuf_ctx(struct virtqueue *vq, 5445a08b04fSMichael S. Tsirkin struct scatterlist *sg, unsigned int num, 5455a08b04fSMichael S. Tsirkin void *data, 5465a08b04fSMichael S. Tsirkin void *ctx, 5475a08b04fSMichael S. Tsirkin gfp_t gfp) 5485a08b04fSMichael S. Tsirkin { 5495a08b04fSMichael S. Tsirkin return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp); 5505a08b04fSMichael S. Tsirkin } 5515a08b04fSMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx); 5525a08b04fSMichael S. Tsirkin 553*138fd251STiwei Bie static bool virtqueue_kick_prepare_split(struct virtqueue *_vq) 5540a8a69ddSRusty Russell { 5550a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 556a5c262c5SMichael S. Tsirkin u16 new, old; 55741f0377fSRusty Russell bool needs_kick; 55841f0377fSRusty Russell 5590a8a69ddSRusty Russell START_USE(vq); 560a72caae2SJason Wang /* We need to expose available array entries before checking avail 561a72caae2SJason Wang * event. */ 562a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 5630a8a69ddSRusty Russell 564f277ec42SVenkatesh Srinivas old = vq->avail_idx_shadow - vq->num_added; 565f277ec42SVenkatesh Srinivas new = vq->avail_idx_shadow; 5660a8a69ddSRusty Russell vq->num_added = 0; 5670a8a69ddSRusty Russell 568e93300b1SRusty Russell #ifdef DEBUG 569e93300b1SRusty Russell if (vq->last_add_time_valid) { 570e93300b1SRusty Russell WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), 571e93300b1SRusty Russell vq->last_add_time)) > 100); 572e93300b1SRusty Russell } 573e93300b1SRusty Russell vq->last_add_time_valid = false; 574e93300b1SRusty Russell #endif 575e93300b1SRusty Russell 57641f0377fSRusty Russell if (vq->event) { 57700e6f3d9SMichael S. Tsirkin needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, vring_avail_event(&vq->vring)), 57841f0377fSRusty Russell new, old); 57941f0377fSRusty Russell } else { 58000e6f3d9SMichael S. Tsirkin needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(_vq->vdev, VRING_USED_F_NO_NOTIFY)); 58141f0377fSRusty Russell } 5820a8a69ddSRusty Russell END_USE(vq); 58341f0377fSRusty Russell return needs_kick; 58441f0377fSRusty Russell } 585*138fd251STiwei Bie 586*138fd251STiwei Bie /** 587*138fd251STiwei Bie * virtqueue_kick_prepare - first half of split virtqueue_kick call. 588*138fd251STiwei Bie * @vq: the struct virtqueue 589*138fd251STiwei Bie * 590*138fd251STiwei Bie * Instead of virtqueue_kick(), you can do: 591*138fd251STiwei Bie * if (virtqueue_kick_prepare(vq)) 592*138fd251STiwei Bie * virtqueue_notify(vq); 593*138fd251STiwei Bie * 594*138fd251STiwei Bie * This is sometimes useful because the virtqueue_kick_prepare() needs 595*138fd251STiwei Bie * to be serialized, but the actual virtqueue_notify() call does not. 596*138fd251STiwei Bie */ 597*138fd251STiwei Bie bool virtqueue_kick_prepare(struct virtqueue *_vq) 598*138fd251STiwei Bie { 599*138fd251STiwei Bie return virtqueue_kick_prepare_split(_vq); 600*138fd251STiwei Bie } 60141f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); 60241f0377fSRusty Russell 60341f0377fSRusty Russell /** 60441f0377fSRusty Russell * virtqueue_notify - second half of split virtqueue_kick call. 60541f0377fSRusty Russell * @vq: the struct virtqueue 60641f0377fSRusty Russell * 60741f0377fSRusty Russell * This does not need to be serialized. 6085b1bf7cbSHeinz Graalfs * 6095b1bf7cbSHeinz Graalfs * Returns false if host notify failed or queue is broken, otherwise true. 61041f0377fSRusty Russell */ 6115b1bf7cbSHeinz Graalfs bool virtqueue_notify(struct virtqueue *_vq) 61241f0377fSRusty Russell { 61341f0377fSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 61441f0377fSRusty Russell 6155b1bf7cbSHeinz Graalfs if (unlikely(vq->broken)) 6165b1bf7cbSHeinz Graalfs return false; 6175b1bf7cbSHeinz Graalfs 61841f0377fSRusty Russell /* Prod other side to tell it about changes. */ 6192342d6a6SHeinz Graalfs if (!vq->notify(_vq)) { 6205b1bf7cbSHeinz Graalfs vq->broken = true; 6215b1bf7cbSHeinz Graalfs return false; 6225b1bf7cbSHeinz Graalfs } 6235b1bf7cbSHeinz Graalfs return true; 62441f0377fSRusty Russell } 62541f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify); 62641f0377fSRusty Russell 62741f0377fSRusty Russell /** 62841f0377fSRusty Russell * virtqueue_kick - update after add_buf 62941f0377fSRusty Russell * @vq: the struct virtqueue 63041f0377fSRusty Russell * 631b3087e48SRusty Russell * After one or more virtqueue_add_* calls, invoke this to kick 63241f0377fSRusty Russell * the other side. 63341f0377fSRusty Russell * 63441f0377fSRusty Russell * Caller must ensure we don't call this with other virtqueue 63541f0377fSRusty Russell * operations at the same time (except where noted). 6365b1bf7cbSHeinz Graalfs * 6375b1bf7cbSHeinz Graalfs * Returns false if kick failed, otherwise true. 63841f0377fSRusty Russell */ 6395b1bf7cbSHeinz Graalfs bool virtqueue_kick(struct virtqueue *vq) 64041f0377fSRusty Russell { 64141f0377fSRusty Russell if (virtqueue_kick_prepare(vq)) 6425b1bf7cbSHeinz Graalfs return virtqueue_notify(vq); 6435b1bf7cbSHeinz Graalfs return true; 6440a8a69ddSRusty Russell } 6457c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick); 6460a8a69ddSRusty Russell 647*138fd251STiwei Bie static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, 6485a08b04fSMichael S. Tsirkin void **ctx) 6490a8a69ddSRusty Russell { 650780bc790SAndy Lutomirski unsigned int i, j; 651c60923cbSGonglei __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); 6520a8a69ddSRusty Russell 6530a8a69ddSRusty Russell /* Clear data ptr. */ 654780bc790SAndy Lutomirski vq->desc_state[head].data = NULL; 6550a8a69ddSRusty Russell 656780bc790SAndy Lutomirski /* Put back on free list: unmap first-level descriptors and find end */ 6570a8a69ddSRusty Russell i = head; 6589fa29b9dSMark McLoughlin 659780bc790SAndy Lutomirski while (vq->vring.desc[i].flags & nextflag) { 660*138fd251STiwei Bie vring_unmap_one_split(vq, &vq->vring.desc[i]); 66100e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(vq->vq.vdev, vq->vring.desc[i].next); 66206ca287dSRusty Russell vq->vq.num_free++; 6630a8a69ddSRusty Russell } 6640a8a69ddSRusty Russell 665*138fd251STiwei Bie vring_unmap_one_split(vq, &vq->vring.desc[i]); 66600e6f3d9SMichael S. Tsirkin vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head); 6670a8a69ddSRusty Russell vq->free_head = head; 668780bc790SAndy Lutomirski 6690a8a69ddSRusty Russell /* Plus final descriptor */ 67006ca287dSRusty Russell vq->vq.num_free++; 671780bc790SAndy Lutomirski 6725a08b04fSMichael S. Tsirkin if (vq->indirect) { 673780bc790SAndy Lutomirski struct vring_desc *indir_desc = vq->desc_state[head].indir_desc; 6745a08b04fSMichael S. Tsirkin u32 len; 6755a08b04fSMichael S. Tsirkin 6765a08b04fSMichael S. Tsirkin /* Free the indirect table, if any, now that it's unmapped. */ 6775a08b04fSMichael S. Tsirkin if (!indir_desc) 6785a08b04fSMichael S. Tsirkin return; 6795a08b04fSMichael S. Tsirkin 6805a08b04fSMichael S. Tsirkin len = virtio32_to_cpu(vq->vq.vdev, vq->vring.desc[head].len); 681780bc790SAndy Lutomirski 682780bc790SAndy Lutomirski BUG_ON(!(vq->vring.desc[head].flags & 683780bc790SAndy Lutomirski cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT))); 684780bc790SAndy Lutomirski BUG_ON(len == 0 || len % sizeof(struct vring_desc)); 685780bc790SAndy Lutomirski 686780bc790SAndy Lutomirski for (j = 0; j < len / sizeof(struct vring_desc); j++) 687*138fd251STiwei Bie vring_unmap_one_split(vq, &indir_desc[j]); 688780bc790SAndy Lutomirski 6895a08b04fSMichael S. Tsirkin kfree(indir_desc); 690780bc790SAndy Lutomirski vq->desc_state[head].indir_desc = NULL; 6915a08b04fSMichael S. Tsirkin } else if (ctx) { 6925a08b04fSMichael S. Tsirkin *ctx = vq->desc_state[head].indir_desc; 693780bc790SAndy Lutomirski } 6940a8a69ddSRusty Russell } 6950a8a69ddSRusty Russell 696*138fd251STiwei Bie static inline bool more_used_split(const struct vring_virtqueue *vq) 6970a8a69ddSRusty Russell { 69800e6f3d9SMichael S. Tsirkin return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, vq->vring.used->idx); 6990a8a69ddSRusty Russell } 7000a8a69ddSRusty Russell 701*138fd251STiwei Bie static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, 702*138fd251STiwei Bie unsigned int *len, 7035a08b04fSMichael S. Tsirkin void **ctx) 7040a8a69ddSRusty Russell { 7050a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 7060a8a69ddSRusty Russell void *ret; 7070a8a69ddSRusty Russell unsigned int i; 7083b720b8cSRusty Russell u16 last_used; 7090a8a69ddSRusty Russell 7100a8a69ddSRusty Russell START_USE(vq); 7110a8a69ddSRusty Russell 7125ef82752SRusty Russell if (unlikely(vq->broken)) { 7135ef82752SRusty Russell END_USE(vq); 7145ef82752SRusty Russell return NULL; 7155ef82752SRusty Russell } 7165ef82752SRusty Russell 717*138fd251STiwei Bie if (!more_used_split(vq)) { 7180a8a69ddSRusty Russell pr_debug("No more buffers in queue\n"); 7190a8a69ddSRusty Russell END_USE(vq); 7200a8a69ddSRusty Russell return NULL; 7210a8a69ddSRusty Russell } 7220a8a69ddSRusty Russell 7232d61ba95SMichael S. Tsirkin /* Only get used array entries after they have been exposed by host. */ 724a9a0fef7SRusty Russell virtio_rmb(vq->weak_barriers); 7252d61ba95SMichael S. Tsirkin 7263b720b8cSRusty Russell last_used = (vq->last_used_idx & (vq->vring.num - 1)); 72700e6f3d9SMichael S. Tsirkin i = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].id); 72800e6f3d9SMichael S. Tsirkin *len = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].len); 7290a8a69ddSRusty Russell 7300a8a69ddSRusty Russell if (unlikely(i >= vq->vring.num)) { 7310a8a69ddSRusty Russell BAD_RING(vq, "id %u out of range\n", i); 7320a8a69ddSRusty Russell return NULL; 7330a8a69ddSRusty Russell } 734780bc790SAndy Lutomirski if (unlikely(!vq->desc_state[i].data)) { 7350a8a69ddSRusty Russell BAD_RING(vq, "id %u is not a head!\n", i); 7360a8a69ddSRusty Russell return NULL; 7370a8a69ddSRusty Russell } 7380a8a69ddSRusty Russell 739*138fd251STiwei Bie /* detach_buf_split clears data, so grab it now. */ 740780bc790SAndy Lutomirski ret = vq->desc_state[i].data; 741*138fd251STiwei Bie detach_buf_split(vq, i, ctx); 7420a8a69ddSRusty Russell vq->last_used_idx++; 743a5c262c5SMichael S. Tsirkin /* If we expect an interrupt for the next entry, tell host 744a5c262c5SMichael S. Tsirkin * by writing event index and flush out the write before 745a5c262c5SMichael S. Tsirkin * the read in the next get_buf call. */ 746788e5b3aSMichael S. Tsirkin if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) 747788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 748788e5b3aSMichael S. Tsirkin &vring_used_event(&vq->vring), 749788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); 750a5c262c5SMichael S. Tsirkin 751e93300b1SRusty Russell #ifdef DEBUG 752e93300b1SRusty Russell vq->last_add_time_valid = false; 753e93300b1SRusty Russell #endif 754e93300b1SRusty Russell 7550a8a69ddSRusty Russell END_USE(vq); 7560a8a69ddSRusty Russell return ret; 7570a8a69ddSRusty Russell } 758*138fd251STiwei Bie 759*138fd251STiwei Bie /** 760*138fd251STiwei Bie * virtqueue_get_buf - get the next used buffer 761*138fd251STiwei Bie * @vq: the struct virtqueue we're talking about. 762*138fd251STiwei Bie * @len: the length written into the buffer 763*138fd251STiwei Bie * 764*138fd251STiwei Bie * If the device wrote data into the buffer, @len will be set to the 765*138fd251STiwei Bie * amount written. This means you don't need to clear the buffer 766*138fd251STiwei Bie * beforehand to ensure there's no data leakage in the case of short 767*138fd251STiwei Bie * writes. 768*138fd251STiwei Bie * 769*138fd251STiwei Bie * Caller must ensure we don't call this with other virtqueue 770*138fd251STiwei Bie * operations at the same time (except where noted). 771*138fd251STiwei Bie * 772*138fd251STiwei Bie * Returns NULL if there are no used buffers, or the "data" token 773*138fd251STiwei Bie * handed to virtqueue_add_*(). 774*138fd251STiwei Bie */ 775*138fd251STiwei Bie void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len, 776*138fd251STiwei Bie void **ctx) 777*138fd251STiwei Bie { 778*138fd251STiwei Bie return virtqueue_get_buf_ctx_split(_vq, len, ctx); 779*138fd251STiwei Bie } 7805a08b04fSMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx); 7810a8a69ddSRusty Russell 7825a08b04fSMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) 7835a08b04fSMichael S. Tsirkin { 7845a08b04fSMichael S. Tsirkin return virtqueue_get_buf_ctx(_vq, len, NULL); 7855a08b04fSMichael S. Tsirkin } 7865a08b04fSMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf); 787*138fd251STiwei Bie 788*138fd251STiwei Bie static void virtqueue_disable_cb_split(struct virtqueue *_vq) 789*138fd251STiwei Bie { 790*138fd251STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 791*138fd251STiwei Bie 792*138fd251STiwei Bie if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { 793*138fd251STiwei Bie vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 794*138fd251STiwei Bie if (!vq->event) 795*138fd251STiwei Bie vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow); 796*138fd251STiwei Bie } 797*138fd251STiwei Bie } 798*138fd251STiwei Bie 7995dfc1762SRusty Russell /** 8005dfc1762SRusty Russell * virtqueue_disable_cb - disable callbacks 8015dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 8025dfc1762SRusty Russell * 8035dfc1762SRusty Russell * Note that this is not necessarily synchronous, hence unreliable and only 8045dfc1762SRusty Russell * useful as an optimization. 8055dfc1762SRusty Russell * 8065dfc1762SRusty Russell * Unlike other operations, this need not be serialized. 8075dfc1762SRusty Russell */ 8087c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq) 80918445c4dSRusty Russell { 810*138fd251STiwei Bie virtqueue_disable_cb_split(_vq); 81118445c4dSRusty Russell } 8127c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb); 81318445c4dSRusty Russell 814*138fd251STiwei Bie static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq) 815cc229884SMichael S. Tsirkin { 816cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 817cc229884SMichael S. Tsirkin u16 last_used_idx; 818cc229884SMichael S. Tsirkin 819cc229884SMichael S. Tsirkin START_USE(vq); 820cc229884SMichael S. Tsirkin 821cc229884SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 822cc229884SMichael S. Tsirkin * more to do. */ 823cc229884SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to 824cc229884SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 825cc229884SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 826f277ec42SVenkatesh Srinivas if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 827f277ec42SVenkatesh Srinivas vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 8280ea1e4a6SLadi Prosek if (!vq->event) 829f277ec42SVenkatesh Srinivas vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow); 830f277ec42SVenkatesh Srinivas } 83100e6f3d9SMichael S. Tsirkin vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, last_used_idx = vq->last_used_idx); 832cc229884SMichael S. Tsirkin END_USE(vq); 833cc229884SMichael S. Tsirkin return last_used_idx; 834cc229884SMichael S. Tsirkin } 835*138fd251STiwei Bie 836*138fd251STiwei Bie /** 837*138fd251STiwei Bie * virtqueue_enable_cb_prepare - restart callbacks after disable_cb 838*138fd251STiwei Bie * @vq: the struct virtqueue we're talking about. 839*138fd251STiwei Bie * 840*138fd251STiwei Bie * This re-enables callbacks; it returns current queue state 841*138fd251STiwei Bie * in an opaque unsigned value. This value should be later tested by 842*138fd251STiwei Bie * virtqueue_poll, to detect a possible race between the driver checking for 843*138fd251STiwei Bie * more work, and enabling callbacks. 844*138fd251STiwei Bie * 845*138fd251STiwei Bie * Caller must ensure we don't call this with other virtqueue 846*138fd251STiwei Bie * operations at the same time (except where noted). 847*138fd251STiwei Bie */ 848*138fd251STiwei Bie unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) 849*138fd251STiwei Bie { 850*138fd251STiwei Bie return virtqueue_enable_cb_prepare_split(_vq); 851*138fd251STiwei Bie } 852cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); 853cc229884SMichael S. Tsirkin 854*138fd251STiwei Bie static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx) 855*138fd251STiwei Bie { 856*138fd251STiwei Bie struct vring_virtqueue *vq = to_vvq(_vq); 857*138fd251STiwei Bie 858*138fd251STiwei Bie return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, 859*138fd251STiwei Bie vq->vring.used->idx); 860*138fd251STiwei Bie } 861*138fd251STiwei Bie 862cc229884SMichael S. Tsirkin /** 863cc229884SMichael S. Tsirkin * virtqueue_poll - query pending used buffers 864cc229884SMichael S. Tsirkin * @vq: the struct virtqueue we're talking about. 865cc229884SMichael S. Tsirkin * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). 866cc229884SMichael S. Tsirkin * 867cc229884SMichael S. Tsirkin * Returns "true" if there are pending used buffers in the queue. 868cc229884SMichael S. Tsirkin * 869cc229884SMichael S. Tsirkin * This does not need to be serialized. 870cc229884SMichael S. Tsirkin */ 871cc229884SMichael S. Tsirkin bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) 872cc229884SMichael S. Tsirkin { 873cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 874cc229884SMichael S. Tsirkin 875cc229884SMichael S. Tsirkin virtio_mb(vq->weak_barriers); 876*138fd251STiwei Bie return virtqueue_poll_split(_vq, last_used_idx); 877cc229884SMichael S. Tsirkin } 878cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_poll); 879cc229884SMichael S. Tsirkin 880cc229884SMichael S. Tsirkin /** 8815dfc1762SRusty Russell * virtqueue_enable_cb - restart callbacks after disable_cb. 8825dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 8835dfc1762SRusty Russell * 8845dfc1762SRusty Russell * This re-enables callbacks; it returns "false" if there are pending 8855dfc1762SRusty Russell * buffers in the queue, to detect a possible race between the driver 8865dfc1762SRusty Russell * checking for more work, and enabling callbacks. 8875dfc1762SRusty Russell * 8885dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 8895dfc1762SRusty Russell * operations at the same time (except where noted). 8905dfc1762SRusty Russell */ 8917c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq) 8920a8a69ddSRusty Russell { 893cc229884SMichael S. Tsirkin unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); 894cc229884SMichael S. Tsirkin return !virtqueue_poll(_vq, last_used_idx); 8950a8a69ddSRusty Russell } 8967c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb); 8970a8a69ddSRusty Russell 898*138fd251STiwei Bie static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq) 8997ab358c2SMichael S. Tsirkin { 9007ab358c2SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 9017ab358c2SMichael S. Tsirkin u16 bufs; 9027ab358c2SMichael S. Tsirkin 9037ab358c2SMichael S. Tsirkin START_USE(vq); 9047ab358c2SMichael S. Tsirkin 9057ab358c2SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 9067ab358c2SMichael S. Tsirkin * more to do. */ 9077ab358c2SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to 9087ab358c2SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 9090ea1e4a6SLadi Prosek * entry. Always update the event index to keep code simple. */ 910f277ec42SVenkatesh Srinivas if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 911f277ec42SVenkatesh Srinivas vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 9120ea1e4a6SLadi Prosek if (!vq->event) 913f277ec42SVenkatesh Srinivas vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow); 914f277ec42SVenkatesh Srinivas } 9157ab358c2SMichael S. Tsirkin /* TODO: tune this threshold */ 916f277ec42SVenkatesh Srinivas bufs = (u16)(vq->avail_idx_shadow - vq->last_used_idx) * 3 / 4; 917788e5b3aSMichael S. Tsirkin 918788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 919788e5b3aSMichael S. Tsirkin &vring_used_event(&vq->vring), 920788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); 921788e5b3aSMichael S. Tsirkin 92200e6f3d9SMichael S. Tsirkin if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) { 9237ab358c2SMichael S. Tsirkin END_USE(vq); 9247ab358c2SMichael S. Tsirkin return false; 9257ab358c2SMichael S. Tsirkin } 9267ab358c2SMichael S. Tsirkin 9277ab358c2SMichael S. Tsirkin END_USE(vq); 9287ab358c2SMichael S. Tsirkin return true; 9297ab358c2SMichael S. Tsirkin } 9307ab358c2SMichael S. Tsirkin 9315dfc1762SRusty Russell /** 932*138fd251STiwei Bie * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. 9335dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 9345dfc1762SRusty Russell * 935*138fd251STiwei Bie * This re-enables callbacks but hints to the other side to delay 936*138fd251STiwei Bie * interrupts until most of the available buffers have been processed; 937*138fd251STiwei Bie * it returns "false" if there are many pending buffers in the queue, 938*138fd251STiwei Bie * to detect a possible race between the driver checking for more work, 939*138fd251STiwei Bie * and enabling callbacks. 940*138fd251STiwei Bie * 941*138fd251STiwei Bie * Caller must ensure we don't call this with other virtqueue 942*138fd251STiwei Bie * operations at the same time (except where noted). 9435dfc1762SRusty Russell */ 944*138fd251STiwei Bie bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) 945*138fd251STiwei Bie { 946*138fd251STiwei Bie return virtqueue_enable_cb_delayed_split(_vq); 947*138fd251STiwei Bie } 948*138fd251STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); 949*138fd251STiwei Bie 950*138fd251STiwei Bie static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq) 951c021eac4SShirley Ma { 952c021eac4SShirley Ma struct vring_virtqueue *vq = to_vvq(_vq); 953c021eac4SShirley Ma unsigned int i; 954c021eac4SShirley Ma void *buf; 955c021eac4SShirley Ma 956c021eac4SShirley Ma START_USE(vq); 957c021eac4SShirley Ma 958c021eac4SShirley Ma for (i = 0; i < vq->vring.num; i++) { 959780bc790SAndy Lutomirski if (!vq->desc_state[i].data) 960c021eac4SShirley Ma continue; 961*138fd251STiwei Bie /* detach_buf_split clears data, so grab it now. */ 962780bc790SAndy Lutomirski buf = vq->desc_state[i].data; 963*138fd251STiwei Bie detach_buf_split(vq, i, NULL); 964f277ec42SVenkatesh Srinivas vq->avail_idx_shadow--; 965f277ec42SVenkatesh Srinivas vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow); 966c021eac4SShirley Ma END_USE(vq); 967c021eac4SShirley Ma return buf; 968c021eac4SShirley Ma } 969c021eac4SShirley Ma /* That should have freed everything. */ 97006ca287dSRusty Russell BUG_ON(vq->vq.num_free != vq->vring.num); 971c021eac4SShirley Ma 972c021eac4SShirley Ma END_USE(vq); 973c021eac4SShirley Ma return NULL; 974c021eac4SShirley Ma } 975*138fd251STiwei Bie 976*138fd251STiwei Bie /** 977*138fd251STiwei Bie * virtqueue_detach_unused_buf - detach first unused buffer 978*138fd251STiwei Bie * @vq: the struct virtqueue we're talking about. 979*138fd251STiwei Bie * 980*138fd251STiwei Bie * Returns NULL or the "data" token handed to virtqueue_add_*(). 981*138fd251STiwei Bie * This is not valid on an active queue; it is useful only for device 982*138fd251STiwei Bie * shutdown. 983*138fd251STiwei Bie */ 984*138fd251STiwei Bie void *virtqueue_detach_unused_buf(struct virtqueue *_vq) 985*138fd251STiwei Bie { 986*138fd251STiwei Bie return virtqueue_detach_unused_buf_split(_vq); 987*138fd251STiwei Bie } 9887c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); 989c021eac4SShirley Ma 990*138fd251STiwei Bie static inline bool more_used(const struct vring_virtqueue *vq) 991*138fd251STiwei Bie { 992*138fd251STiwei Bie return more_used_split(vq); 993*138fd251STiwei Bie } 994*138fd251STiwei Bie 9950a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq) 9960a8a69ddSRusty Russell { 9970a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 9980a8a69ddSRusty Russell 9990a8a69ddSRusty Russell if (!more_used(vq)) { 10000a8a69ddSRusty Russell pr_debug("virtqueue interrupt with no work for %p\n", vq); 10010a8a69ddSRusty Russell return IRQ_NONE; 10020a8a69ddSRusty Russell } 10030a8a69ddSRusty Russell 10040a8a69ddSRusty Russell if (unlikely(vq->broken)) 10050a8a69ddSRusty Russell return IRQ_HANDLED; 10060a8a69ddSRusty Russell 10070a8a69ddSRusty Russell pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); 100818445c4dSRusty Russell if (vq->vq.callback) 100918445c4dSRusty Russell vq->vq.callback(&vq->vq); 10100a8a69ddSRusty Russell 10110a8a69ddSRusty Russell return IRQ_HANDLED; 10120a8a69ddSRusty Russell } 1013c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt); 10140a8a69ddSRusty Russell 10152a2d1382SAndy Lutomirski struct virtqueue *__vring_new_virtqueue(unsigned int index, 10162a2d1382SAndy Lutomirski struct vring vring, 10170a8a69ddSRusty Russell struct virtio_device *vdev, 10187b21e34fSRusty Russell bool weak_barriers, 1019f94682ddSMichael S. Tsirkin bool context, 102046f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *), 10219499f5e7SRusty Russell void (*callback)(struct virtqueue *), 10229499f5e7SRusty Russell const char *name) 10230a8a69ddSRusty Russell { 10240a8a69ddSRusty Russell unsigned int i; 10252a2d1382SAndy Lutomirski struct vring_virtqueue *vq; 10260a8a69ddSRusty Russell 10272a2d1382SAndy Lutomirski vq = kmalloc(sizeof(*vq) + vring.num * sizeof(struct vring_desc_state), 1028780bc790SAndy Lutomirski GFP_KERNEL); 10290a8a69ddSRusty Russell if (!vq) 10300a8a69ddSRusty Russell return NULL; 10310a8a69ddSRusty Russell 10322a2d1382SAndy Lutomirski vq->vring = vring; 10330a8a69ddSRusty Russell vq->vq.callback = callback; 10340a8a69ddSRusty Russell vq->vq.vdev = vdev; 10359499f5e7SRusty Russell vq->vq.name = name; 10362a2d1382SAndy Lutomirski vq->vq.num_free = vring.num; 103706ca287dSRusty Russell vq->vq.index = index; 10382a2d1382SAndy Lutomirski vq->we_own_ring = false; 10392a2d1382SAndy Lutomirski vq->queue_dma_addr = 0; 10402a2d1382SAndy Lutomirski vq->queue_size_in_bytes = 0; 10410a8a69ddSRusty Russell vq->notify = notify; 10427b21e34fSRusty Russell vq->weak_barriers = weak_barriers; 10430a8a69ddSRusty Russell vq->broken = false; 10440a8a69ddSRusty Russell vq->last_used_idx = 0; 1045f277ec42SVenkatesh Srinivas vq->avail_flags_shadow = 0; 1046f277ec42SVenkatesh Srinivas vq->avail_idx_shadow = 0; 10470a8a69ddSRusty Russell vq->num_added = 0; 10489499f5e7SRusty Russell list_add_tail(&vq->vq.list, &vdev->vqs); 10490a8a69ddSRusty Russell #ifdef DEBUG 10500a8a69ddSRusty Russell vq->in_use = false; 1051e93300b1SRusty Russell vq->last_add_time_valid = false; 10520a8a69ddSRusty Russell #endif 10530a8a69ddSRusty Russell 10545a08b04fSMichael S. Tsirkin vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && 10555a08b04fSMichael S. Tsirkin !context; 1056a5c262c5SMichael S. Tsirkin vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 10579fa29b9dSMark McLoughlin 10580a8a69ddSRusty Russell /* No callback? Tell other side not to bother us. */ 1059f277ec42SVenkatesh Srinivas if (!callback) { 1060f277ec42SVenkatesh Srinivas vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 10610ea1e4a6SLadi Prosek if (!vq->event) 1062f277ec42SVenkatesh Srinivas vq->vring.avail->flags = cpu_to_virtio16(vdev, vq->avail_flags_shadow); 1063f277ec42SVenkatesh Srinivas } 10640a8a69ddSRusty Russell 10650a8a69ddSRusty Russell /* Put everything in free lists. */ 10660a8a69ddSRusty Russell vq->free_head = 0; 10672a2d1382SAndy Lutomirski for (i = 0; i < vring.num-1; i++) 106800e6f3d9SMichael S. Tsirkin vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1); 10692a2d1382SAndy Lutomirski memset(vq->desc_state, 0, vring.num * sizeof(struct vring_desc_state)); 10700a8a69ddSRusty Russell 10710a8a69ddSRusty Russell return &vq->vq; 10720a8a69ddSRusty Russell } 10732a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(__vring_new_virtqueue); 10742a2d1382SAndy Lutomirski 10752a2d1382SAndy Lutomirski static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, 10762a2d1382SAndy Lutomirski dma_addr_t *dma_handle, gfp_t flag) 10772a2d1382SAndy Lutomirski { 10782a2d1382SAndy Lutomirski if (vring_use_dma_api(vdev)) { 10792a2d1382SAndy Lutomirski return dma_alloc_coherent(vdev->dev.parent, size, 10802a2d1382SAndy Lutomirski dma_handle, flag); 10812a2d1382SAndy Lutomirski } else { 10822a2d1382SAndy Lutomirski void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag); 10832a2d1382SAndy Lutomirski if (queue) { 10842a2d1382SAndy Lutomirski phys_addr_t phys_addr = virt_to_phys(queue); 10852a2d1382SAndy Lutomirski *dma_handle = (dma_addr_t)phys_addr; 10862a2d1382SAndy Lutomirski 10872a2d1382SAndy Lutomirski /* 10882a2d1382SAndy Lutomirski * Sanity check: make sure we dind't truncate 10892a2d1382SAndy Lutomirski * the address. The only arches I can find that 10902a2d1382SAndy Lutomirski * have 64-bit phys_addr_t but 32-bit dma_addr_t 10912a2d1382SAndy Lutomirski * are certain non-highmem MIPS and x86 10922a2d1382SAndy Lutomirski * configurations, but these configurations 10932a2d1382SAndy Lutomirski * should never allocate physical pages above 32 10942a2d1382SAndy Lutomirski * bits, so this is fine. Just in case, throw a 10952a2d1382SAndy Lutomirski * warning and abort if we end up with an 10962a2d1382SAndy Lutomirski * unrepresentable address. 10972a2d1382SAndy Lutomirski */ 10982a2d1382SAndy Lutomirski if (WARN_ON_ONCE(*dma_handle != phys_addr)) { 10992a2d1382SAndy Lutomirski free_pages_exact(queue, PAGE_ALIGN(size)); 11002a2d1382SAndy Lutomirski return NULL; 11012a2d1382SAndy Lutomirski } 11022a2d1382SAndy Lutomirski } 11032a2d1382SAndy Lutomirski return queue; 11042a2d1382SAndy Lutomirski } 11052a2d1382SAndy Lutomirski } 11062a2d1382SAndy Lutomirski 11072a2d1382SAndy Lutomirski static void vring_free_queue(struct virtio_device *vdev, size_t size, 11082a2d1382SAndy Lutomirski void *queue, dma_addr_t dma_handle) 11092a2d1382SAndy Lutomirski { 11102a2d1382SAndy Lutomirski if (vring_use_dma_api(vdev)) { 11112a2d1382SAndy Lutomirski dma_free_coherent(vdev->dev.parent, size, queue, dma_handle); 11122a2d1382SAndy Lutomirski } else { 11132a2d1382SAndy Lutomirski free_pages_exact(queue, PAGE_ALIGN(size)); 11142a2d1382SAndy Lutomirski } 11152a2d1382SAndy Lutomirski } 11162a2d1382SAndy Lutomirski 11172a2d1382SAndy Lutomirski struct virtqueue *vring_create_virtqueue( 11182a2d1382SAndy Lutomirski unsigned int index, 11192a2d1382SAndy Lutomirski unsigned int num, 11202a2d1382SAndy Lutomirski unsigned int vring_align, 11212a2d1382SAndy Lutomirski struct virtio_device *vdev, 11222a2d1382SAndy Lutomirski bool weak_barriers, 11232a2d1382SAndy Lutomirski bool may_reduce_num, 1124f94682ddSMichael S. Tsirkin bool context, 11252a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *), 11262a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *), 11272a2d1382SAndy Lutomirski const char *name) 11282a2d1382SAndy Lutomirski { 11292a2d1382SAndy Lutomirski struct virtqueue *vq; 1130e00f7bd2SDan Carpenter void *queue = NULL; 11312a2d1382SAndy Lutomirski dma_addr_t dma_addr; 11322a2d1382SAndy Lutomirski size_t queue_size_in_bytes; 11332a2d1382SAndy Lutomirski struct vring vring; 11342a2d1382SAndy Lutomirski 11352a2d1382SAndy Lutomirski /* We assume num is a power of 2. */ 11362a2d1382SAndy Lutomirski if (num & (num - 1)) { 11372a2d1382SAndy Lutomirski dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); 11382a2d1382SAndy Lutomirski return NULL; 11392a2d1382SAndy Lutomirski } 11402a2d1382SAndy Lutomirski 11412a2d1382SAndy Lutomirski /* TODO: allocate each queue chunk individually */ 11422a2d1382SAndy Lutomirski for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { 11432a2d1382SAndy Lutomirski queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 11442a2d1382SAndy Lutomirski &dma_addr, 11452a2d1382SAndy Lutomirski GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 11462a2d1382SAndy Lutomirski if (queue) 11472a2d1382SAndy Lutomirski break; 11482a2d1382SAndy Lutomirski } 11492a2d1382SAndy Lutomirski 11502a2d1382SAndy Lutomirski if (!num) 11512a2d1382SAndy Lutomirski return NULL; 11522a2d1382SAndy Lutomirski 11532a2d1382SAndy Lutomirski if (!queue) { 11542a2d1382SAndy Lutomirski /* Try to get a single page. You are my only hope! */ 11552a2d1382SAndy Lutomirski queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 11562a2d1382SAndy Lutomirski &dma_addr, GFP_KERNEL|__GFP_ZERO); 11572a2d1382SAndy Lutomirski } 11582a2d1382SAndy Lutomirski if (!queue) 11592a2d1382SAndy Lutomirski return NULL; 11602a2d1382SAndy Lutomirski 11612a2d1382SAndy Lutomirski queue_size_in_bytes = vring_size(num, vring_align); 11622a2d1382SAndy Lutomirski vring_init(&vring, num, queue, vring_align); 11632a2d1382SAndy Lutomirski 1164f94682ddSMichael S. Tsirkin vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, 11652a2d1382SAndy Lutomirski notify, callback, name); 11662a2d1382SAndy Lutomirski if (!vq) { 11672a2d1382SAndy Lutomirski vring_free_queue(vdev, queue_size_in_bytes, queue, 11682a2d1382SAndy Lutomirski dma_addr); 11692a2d1382SAndy Lutomirski return NULL; 11702a2d1382SAndy Lutomirski } 11712a2d1382SAndy Lutomirski 11722a2d1382SAndy Lutomirski to_vvq(vq)->queue_dma_addr = dma_addr; 11732a2d1382SAndy Lutomirski to_vvq(vq)->queue_size_in_bytes = queue_size_in_bytes; 11742a2d1382SAndy Lutomirski to_vvq(vq)->we_own_ring = true; 11752a2d1382SAndy Lutomirski 11762a2d1382SAndy Lutomirski return vq; 11772a2d1382SAndy Lutomirski } 11782a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(vring_create_virtqueue); 11792a2d1382SAndy Lutomirski 11802a2d1382SAndy Lutomirski struct virtqueue *vring_new_virtqueue(unsigned int index, 11812a2d1382SAndy Lutomirski unsigned int num, 11822a2d1382SAndy Lutomirski unsigned int vring_align, 11832a2d1382SAndy Lutomirski struct virtio_device *vdev, 11842a2d1382SAndy Lutomirski bool weak_barriers, 1185f94682ddSMichael S. Tsirkin bool context, 11862a2d1382SAndy Lutomirski void *pages, 11872a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *vq), 11882a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *vq), 11892a2d1382SAndy Lutomirski const char *name) 11902a2d1382SAndy Lutomirski { 11912a2d1382SAndy Lutomirski struct vring vring; 11922a2d1382SAndy Lutomirski vring_init(&vring, num, pages, vring_align); 1193f94682ddSMichael S. Tsirkin return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context, 11942a2d1382SAndy Lutomirski notify, callback, name); 11952a2d1382SAndy Lutomirski } 1196c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue); 11970a8a69ddSRusty Russell 11982a2d1382SAndy Lutomirski void vring_del_virtqueue(struct virtqueue *_vq) 11990a8a69ddSRusty Russell { 12002a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 12012a2d1382SAndy Lutomirski 12022a2d1382SAndy Lutomirski if (vq->we_own_ring) { 12032a2d1382SAndy Lutomirski vring_free_queue(vq->vq.vdev, vq->queue_size_in_bytes, 12042a2d1382SAndy Lutomirski vq->vring.desc, vq->queue_dma_addr); 12052a2d1382SAndy Lutomirski } 12062a2d1382SAndy Lutomirski list_del(&_vq->list); 12072a2d1382SAndy Lutomirski kfree(vq); 12080a8a69ddSRusty Russell } 1209c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue); 12100a8a69ddSRusty Russell 1211e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */ 1212e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev) 1213e34f8725SRusty Russell { 1214e34f8725SRusty Russell unsigned int i; 1215e34f8725SRusty Russell 1216e34f8725SRusty Russell for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { 1217e34f8725SRusty Russell switch (i) { 12189fa29b9dSMark McLoughlin case VIRTIO_RING_F_INDIRECT_DESC: 12199fa29b9dSMark McLoughlin break; 1220a5c262c5SMichael S. Tsirkin case VIRTIO_RING_F_EVENT_IDX: 1221a5c262c5SMichael S. Tsirkin break; 1222747ae34aSMichael S. Tsirkin case VIRTIO_F_VERSION_1: 1223747ae34aSMichael S. Tsirkin break; 12241a937693SMichael S. Tsirkin case VIRTIO_F_IOMMU_PLATFORM: 12251a937693SMichael S. Tsirkin break; 1226e34f8725SRusty Russell default: 1227e34f8725SRusty Russell /* We don't understand this bit. */ 1228e16e12beSMichael S. Tsirkin __virtio_clear_bit(vdev, i); 1229e34f8725SRusty Russell } 1230e34f8725SRusty Russell } 1231e34f8725SRusty Russell } 1232e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features); 1233e34f8725SRusty Russell 12345dfc1762SRusty Russell /** 12355dfc1762SRusty Russell * virtqueue_get_vring_size - return the size of the virtqueue's vring 12365dfc1762SRusty Russell * @vq: the struct virtqueue containing the vring of interest. 12375dfc1762SRusty Russell * 12385dfc1762SRusty Russell * Returns the size of the vring. This is mainly used for boasting to 12395dfc1762SRusty Russell * userspace. Unlike other operations, this need not be serialized. 12405dfc1762SRusty Russell */ 12418f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) 12428f9f4668SRick Jones { 12438f9f4668SRick Jones 12448f9f4668SRick Jones struct vring_virtqueue *vq = to_vvq(_vq); 12458f9f4668SRick Jones 12468f9f4668SRick Jones return vq->vring.num; 12478f9f4668SRick Jones } 12488f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); 12498f9f4668SRick Jones 1250b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq) 1251b3b32c94SHeinz Graalfs { 1252b3b32c94SHeinz Graalfs struct vring_virtqueue *vq = to_vvq(_vq); 1253b3b32c94SHeinz Graalfs 1254b3b32c94SHeinz Graalfs return vq->broken; 1255b3b32c94SHeinz Graalfs } 1256b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken); 1257b3b32c94SHeinz Graalfs 1258e2dcdfe9SRusty Russell /* 1259e2dcdfe9SRusty Russell * This should prevent the device from being used, allowing drivers to 1260e2dcdfe9SRusty Russell * recover. You may need to grab appropriate locks to flush. 1261e2dcdfe9SRusty Russell */ 1262e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev) 1263e2dcdfe9SRusty Russell { 1264e2dcdfe9SRusty Russell struct virtqueue *_vq; 1265e2dcdfe9SRusty Russell 1266e2dcdfe9SRusty Russell list_for_each_entry(_vq, &dev->vqs, list) { 1267e2dcdfe9SRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 1268e2dcdfe9SRusty Russell vq->broken = true; 1269e2dcdfe9SRusty Russell } 1270e2dcdfe9SRusty Russell } 1271e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device); 1272e2dcdfe9SRusty Russell 12732a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq) 127489062652SCornelia Huck { 127589062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 127689062652SCornelia Huck 12772a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 127889062652SCornelia Huck 12792a2d1382SAndy Lutomirski return vq->queue_dma_addr; 12802a2d1382SAndy Lutomirski } 12812a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr); 12822a2d1382SAndy Lutomirski 12832a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq) 128489062652SCornelia Huck { 128589062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 128689062652SCornelia Huck 12872a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 12882a2d1382SAndy Lutomirski 12892a2d1382SAndy Lutomirski return vq->queue_dma_addr + 12902a2d1382SAndy Lutomirski ((char *)vq->vring.avail - (char *)vq->vring.desc); 129189062652SCornelia Huck } 12922a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr); 12932a2d1382SAndy Lutomirski 12942a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq) 12952a2d1382SAndy Lutomirski { 12962a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 12972a2d1382SAndy Lutomirski 12982a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 12992a2d1382SAndy Lutomirski 13002a2d1382SAndy Lutomirski return vq->queue_dma_addr + 13012a2d1382SAndy Lutomirski ((char *)vq->vring.used - (char *)vq->vring.desc); 13022a2d1382SAndy Lutomirski } 13032a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_used_addr); 13042a2d1382SAndy Lutomirski 13052a2d1382SAndy Lutomirski const struct vring *virtqueue_get_vring(struct virtqueue *vq) 13062a2d1382SAndy Lutomirski { 13072a2d1382SAndy Lutomirski return &to_vvq(vq)->vring; 13082a2d1382SAndy Lutomirski } 13092a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_vring); 131089062652SCornelia Huck 1311c6fd4701SRusty Russell MODULE_LICENSE("GPL"); 1312