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> 266abb2dd9SJoel Stanley #include <linux/kmemleak.h> 27780bc790SAndy Lutomirski #include <linux/dma-mapping.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 98*2a2d1382SAndy Lutomirski /* DMA, allocation, and size information */ 99*2a2d1382SAndy Lutomirski bool we_own_ring; 100*2a2d1382SAndy Lutomirski size_t queue_size_in_bytes; 101*2a2d1382SAndy Lutomirski dma_addr_t queue_dma_addr; 102*2a2d1382SAndy 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 /* 119d26c96c8SAndy Lutomirski * The interaction between virtio and a possible IOMMU is a mess. 120d26c96c8SAndy Lutomirski * 121d26c96c8SAndy Lutomirski * On most systems with virtio, physical addresses match bus addresses, 122d26c96c8SAndy Lutomirski * and it doesn't particularly matter whether we use the DMA API. 123d26c96c8SAndy Lutomirski * 124d26c96c8SAndy Lutomirski * On some systems, including Xen and any system with a physical device 125d26c96c8SAndy Lutomirski * that speaks virtio behind a physical IOMMU, we must use the DMA API 126d26c96c8SAndy Lutomirski * for virtio DMA to work at all. 127d26c96c8SAndy Lutomirski * 128d26c96c8SAndy Lutomirski * On other systems, including SPARC and PPC64, virtio-pci devices are 129d26c96c8SAndy Lutomirski * enumerated as though they are behind an IOMMU, but the virtio host 130d26c96c8SAndy Lutomirski * ignores the IOMMU, so we must either pretend that the IOMMU isn't 131d26c96c8SAndy Lutomirski * there or somehow map everything as the identity. 132d26c96c8SAndy Lutomirski * 133d26c96c8SAndy Lutomirski * For the time being, we preserve historic behavior and bypass the DMA 134d26c96c8SAndy Lutomirski * API. 135d26c96c8SAndy Lutomirski */ 136d26c96c8SAndy Lutomirski 137d26c96c8SAndy Lutomirski static bool vring_use_dma_api(struct virtio_device *vdev) 138d26c96c8SAndy Lutomirski { 139d26c96c8SAndy Lutomirski return false; 140d26c96c8SAndy Lutomirski } 141d26c96c8SAndy Lutomirski 142780bc790SAndy Lutomirski /* 143780bc790SAndy Lutomirski * The DMA ops on various arches are rather gnarly right now, and 144780bc790SAndy Lutomirski * making all of the arch DMA ops work on the vring device itself 145780bc790SAndy Lutomirski * is a mess. For now, we use the parent device for DMA ops. 146780bc790SAndy Lutomirski */ 147780bc790SAndy Lutomirski struct device *vring_dma_dev(const struct vring_virtqueue *vq) 148780bc790SAndy Lutomirski { 149780bc790SAndy Lutomirski return vq->vq.vdev->dev.parent; 150780bc790SAndy Lutomirski } 151780bc790SAndy Lutomirski 152780bc790SAndy Lutomirski /* Map one sg entry. */ 153780bc790SAndy Lutomirski static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq, 154780bc790SAndy Lutomirski struct scatterlist *sg, 155780bc790SAndy Lutomirski enum dma_data_direction direction) 156780bc790SAndy Lutomirski { 157780bc790SAndy Lutomirski if (!vring_use_dma_api(vq->vq.vdev)) 158780bc790SAndy Lutomirski return (dma_addr_t)sg_phys(sg); 159780bc790SAndy Lutomirski 160780bc790SAndy Lutomirski /* 161780bc790SAndy Lutomirski * We can't use dma_map_sg, because we don't use scatterlists in 162780bc790SAndy Lutomirski * the way it expects (we don't guarantee that the scatterlist 163780bc790SAndy Lutomirski * will exist for the lifetime of the mapping). 164780bc790SAndy Lutomirski */ 165780bc790SAndy Lutomirski return dma_map_page(vring_dma_dev(vq), 166780bc790SAndy Lutomirski sg_page(sg), sg->offset, sg->length, 167780bc790SAndy Lutomirski direction); 168780bc790SAndy Lutomirski } 169780bc790SAndy Lutomirski 170780bc790SAndy Lutomirski static dma_addr_t vring_map_single(const struct vring_virtqueue *vq, 171780bc790SAndy Lutomirski void *cpu_addr, size_t size, 172780bc790SAndy Lutomirski enum dma_data_direction direction) 173780bc790SAndy Lutomirski { 174780bc790SAndy Lutomirski if (!vring_use_dma_api(vq->vq.vdev)) 175780bc790SAndy Lutomirski return (dma_addr_t)virt_to_phys(cpu_addr); 176780bc790SAndy Lutomirski 177780bc790SAndy Lutomirski return dma_map_single(vring_dma_dev(vq), 178780bc790SAndy Lutomirski cpu_addr, size, direction); 179780bc790SAndy Lutomirski } 180780bc790SAndy Lutomirski 181780bc790SAndy Lutomirski static void vring_unmap_one(const struct vring_virtqueue *vq, 182780bc790SAndy Lutomirski struct vring_desc *desc) 183780bc790SAndy Lutomirski { 184780bc790SAndy Lutomirski u16 flags; 185780bc790SAndy Lutomirski 186780bc790SAndy Lutomirski if (!vring_use_dma_api(vq->vq.vdev)) 187780bc790SAndy Lutomirski return; 188780bc790SAndy Lutomirski 189780bc790SAndy Lutomirski flags = virtio16_to_cpu(vq->vq.vdev, desc->flags); 190780bc790SAndy Lutomirski 191780bc790SAndy Lutomirski if (flags & VRING_DESC_F_INDIRECT) { 192780bc790SAndy Lutomirski dma_unmap_single(vring_dma_dev(vq), 193780bc790SAndy Lutomirski virtio64_to_cpu(vq->vq.vdev, desc->addr), 194780bc790SAndy Lutomirski virtio32_to_cpu(vq->vq.vdev, desc->len), 195780bc790SAndy Lutomirski (flags & VRING_DESC_F_WRITE) ? 196780bc790SAndy Lutomirski DMA_FROM_DEVICE : DMA_TO_DEVICE); 197780bc790SAndy Lutomirski } else { 198780bc790SAndy Lutomirski dma_unmap_page(vring_dma_dev(vq), 199780bc790SAndy Lutomirski virtio64_to_cpu(vq->vq.vdev, desc->addr), 200780bc790SAndy Lutomirski virtio32_to_cpu(vq->vq.vdev, desc->len), 201780bc790SAndy Lutomirski (flags & VRING_DESC_F_WRITE) ? 202780bc790SAndy Lutomirski DMA_FROM_DEVICE : DMA_TO_DEVICE); 203780bc790SAndy Lutomirski } 204780bc790SAndy Lutomirski } 205780bc790SAndy Lutomirski 206780bc790SAndy Lutomirski static int vring_mapping_error(const struct vring_virtqueue *vq, 207780bc790SAndy Lutomirski dma_addr_t addr) 208780bc790SAndy Lutomirski { 209780bc790SAndy Lutomirski if (!vring_use_dma_api(vq->vq.vdev)) 210780bc790SAndy Lutomirski return 0; 211780bc790SAndy Lutomirski 212780bc790SAndy Lutomirski return dma_mapping_error(vring_dma_dev(vq), addr); 213780bc790SAndy Lutomirski } 214780bc790SAndy Lutomirski 21500e6f3d9SMichael S. Tsirkin static struct vring_desc *alloc_indirect(struct virtqueue *_vq, 21600e6f3d9SMichael S. Tsirkin unsigned int total_sg, gfp_t gfp) 2179fa29b9dSMark McLoughlin { 2189fa29b9dSMark McLoughlin struct vring_desc *desc; 219b25bd251SRusty Russell unsigned int i; 2209fa29b9dSMark McLoughlin 221b92b1b89SWill Deacon /* 222b92b1b89SWill Deacon * We require lowmem mappings for the descriptors because 223b92b1b89SWill Deacon * otherwise virt_to_phys will give us bogus addresses in the 224b92b1b89SWill Deacon * virtqueue. 225b92b1b89SWill Deacon */ 22682107539SMichal Hocko gfp &= ~__GFP_HIGHMEM; 227b92b1b89SWill Deacon 22813816c76SRusty Russell desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp); 2299fa29b9dSMark McLoughlin if (!desc) 230b25bd251SRusty Russell return NULL; 2319fa29b9dSMark McLoughlin 232b25bd251SRusty Russell for (i = 0; i < total_sg; i++) 23300e6f3d9SMichael S. Tsirkin desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); 234b25bd251SRusty Russell return desc; 2359fa29b9dSMark McLoughlin } 2369fa29b9dSMark McLoughlin 23713816c76SRusty Russell static inline int virtqueue_add(struct virtqueue *_vq, 23813816c76SRusty Russell struct scatterlist *sgs[], 239eeebf9b1SRusty Russell unsigned int total_sg, 24013816c76SRusty Russell unsigned int out_sgs, 24113816c76SRusty Russell unsigned int in_sgs, 242bbd603efSMichael S. Tsirkin void *data, 243bbd603efSMichael S. Tsirkin gfp_t gfp) 2440a8a69ddSRusty Russell { 2450a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 24613816c76SRusty Russell struct scatterlist *sg; 247b25bd251SRusty Russell struct vring_desc *desc; 248780bc790SAndy Lutomirski unsigned int i, n, avail, descs_used, uninitialized_var(prev), err_idx; 2491fe9b6feSMichael S. Tsirkin int head; 250b25bd251SRusty Russell bool indirect; 2510a8a69ddSRusty Russell 2529fa29b9dSMark McLoughlin START_USE(vq); 2539fa29b9dSMark McLoughlin 2540a8a69ddSRusty Russell BUG_ON(data == NULL); 2559fa29b9dSMark McLoughlin 25670670444SRusty Russell if (unlikely(vq->broken)) { 25770670444SRusty Russell END_USE(vq); 25870670444SRusty Russell return -EIO; 25970670444SRusty Russell } 26070670444SRusty Russell 261e93300b1SRusty Russell #ifdef DEBUG 262e93300b1SRusty Russell { 263e93300b1SRusty Russell ktime_t now = ktime_get(); 264e93300b1SRusty Russell 265e93300b1SRusty Russell /* No kick or get, with .1 second between? Warn. */ 266e93300b1SRusty Russell if (vq->last_add_time_valid) 267e93300b1SRusty Russell WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time)) 268e93300b1SRusty Russell > 100); 269e93300b1SRusty Russell vq->last_add_time = now; 270e93300b1SRusty Russell vq->last_add_time_valid = true; 271e93300b1SRusty Russell } 272e93300b1SRusty Russell #endif 273e93300b1SRusty Russell 27413816c76SRusty Russell BUG_ON(total_sg > vq->vring.num); 27513816c76SRusty Russell BUG_ON(total_sg == 0); 2760a8a69ddSRusty Russell 277b25bd251SRusty Russell head = vq->free_head; 278b25bd251SRusty Russell 279b25bd251SRusty Russell /* If the host supports indirect descriptor tables, and we have multiple 280b25bd251SRusty Russell * buffers, then go indirect. FIXME: tune this threshold */ 281b25bd251SRusty Russell if (vq->indirect && total_sg > 1 && vq->vq.num_free) 28200e6f3d9SMichael S. Tsirkin desc = alloc_indirect(_vq, total_sg, gfp); 283b25bd251SRusty Russell else 284b25bd251SRusty Russell desc = NULL; 285b25bd251SRusty Russell 286b25bd251SRusty Russell if (desc) { 287b25bd251SRusty Russell /* Use a single buffer which doesn't continue */ 288780bc790SAndy Lutomirski indirect = true; 289b25bd251SRusty Russell /* Set up rest to use this indirect table. */ 290b25bd251SRusty Russell i = 0; 291b25bd251SRusty Russell descs_used = 1; 292b25bd251SRusty Russell } else { 293780bc790SAndy Lutomirski indirect = false; 294b25bd251SRusty Russell desc = vq->vring.desc; 295b25bd251SRusty Russell i = head; 296b25bd251SRusty Russell descs_used = total_sg; 297b25bd251SRusty Russell } 298b25bd251SRusty Russell 299b25bd251SRusty Russell if (vq->vq.num_free < descs_used) { 3000a8a69ddSRusty Russell pr_debug("Can't add buf len %i - avail = %i\n", 301b25bd251SRusty Russell descs_used, vq->vq.num_free); 30244653eaeSRusty Russell /* FIXME: for historical reasons, we force a notify here if 30344653eaeSRusty Russell * there are outgoing parts to the buffer. Presumably the 30444653eaeSRusty Russell * host should service the ring ASAP. */ 30513816c76SRusty Russell if (out_sgs) 306426e3e0aSRusty Russell vq->notify(&vq->vq); 3070a8a69ddSRusty Russell END_USE(vq); 3080a8a69ddSRusty Russell return -ENOSPC; 3090a8a69ddSRusty Russell } 3100a8a69ddSRusty Russell 31113816c76SRusty Russell for (n = 0; n < out_sgs; n++) { 312eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 313780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE); 314780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 315780bc790SAndy Lutomirski goto unmap_release; 316780bc790SAndy Lutomirski 31700e6f3d9SMichael S. Tsirkin desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT); 318780bc790SAndy Lutomirski desc[i].addr = cpu_to_virtio64(_vq->vdev, addr); 31900e6f3d9SMichael S. Tsirkin desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); 3200a8a69ddSRusty Russell prev = i; 32100e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(_vq->vdev, desc[i].next); 3220a8a69ddSRusty Russell } 32313816c76SRusty Russell } 32413816c76SRusty Russell for (; n < (out_sgs + in_sgs); n++) { 325eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 326780bc790SAndy Lutomirski dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE); 327780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 328780bc790SAndy Lutomirski goto unmap_release; 329780bc790SAndy Lutomirski 33000e6f3d9SMichael S. Tsirkin desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE); 331780bc790SAndy Lutomirski desc[i].addr = cpu_to_virtio64(_vq->vdev, addr); 33200e6f3d9SMichael S. Tsirkin desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); 3330a8a69ddSRusty Russell prev = i; 33400e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(_vq->vdev, desc[i].next); 33513816c76SRusty Russell } 3360a8a69ddSRusty Russell } 3370a8a69ddSRusty Russell /* Last one doesn't continue. */ 33800e6f3d9SMichael S. Tsirkin desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); 3390a8a69ddSRusty Russell 340780bc790SAndy Lutomirski if (indirect) { 341780bc790SAndy Lutomirski /* Now that the indirect table is filled in, map it. */ 342780bc790SAndy Lutomirski dma_addr_t addr = vring_map_single( 343780bc790SAndy Lutomirski vq, desc, total_sg * sizeof(struct vring_desc), 344780bc790SAndy Lutomirski DMA_TO_DEVICE); 345780bc790SAndy Lutomirski if (vring_mapping_error(vq, addr)) 346780bc790SAndy Lutomirski goto unmap_release; 347780bc790SAndy Lutomirski 348780bc790SAndy Lutomirski vq->vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_INDIRECT); 349780bc790SAndy Lutomirski vq->vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, addr); 350780bc790SAndy Lutomirski 351780bc790SAndy Lutomirski vq->vring.desc[head].len = cpu_to_virtio32(_vq->vdev, total_sg * sizeof(struct vring_desc)); 352780bc790SAndy Lutomirski } 353780bc790SAndy Lutomirski 354780bc790SAndy Lutomirski /* We're using some buffers from the free list. */ 355780bc790SAndy Lutomirski vq->vq.num_free -= descs_used; 356780bc790SAndy Lutomirski 3570a8a69ddSRusty Russell /* Update free pointer */ 358b25bd251SRusty Russell if (indirect) 35900e6f3d9SMichael S. Tsirkin vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next); 360b25bd251SRusty Russell else 3610a8a69ddSRusty Russell vq->free_head = i; 3620a8a69ddSRusty Russell 363780bc790SAndy Lutomirski /* Store token and indirect buffer state. */ 364780bc790SAndy Lutomirski vq->desc_state[head].data = data; 365780bc790SAndy Lutomirski if (indirect) 366780bc790SAndy Lutomirski vq->desc_state[head].indir_desc = desc; 3670a8a69ddSRusty Russell 3680a8a69ddSRusty Russell /* Put entry in available array (but don't update avail->idx until they 3693b720b8cSRusty Russell * do sync). */ 370f277ec42SVenkatesh Srinivas avail = vq->avail_idx_shadow & (vq->vring.num - 1); 37100e6f3d9SMichael S. Tsirkin vq->vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); 3720a8a69ddSRusty Russell 373ee7cd898SRusty Russell /* Descriptors and available array need to be set before we expose the 374ee7cd898SRusty Russell * new available array entries. */ 375a9a0fef7SRusty Russell virtio_wmb(vq->weak_barriers); 376f277ec42SVenkatesh Srinivas vq->avail_idx_shadow++; 377f277ec42SVenkatesh Srinivas vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow); 378ee7cd898SRusty Russell vq->num_added++; 379ee7cd898SRusty Russell 3805e05bf58STetsuo Handa pr_debug("Added buffer head %i to %p\n", head, vq); 3815e05bf58STetsuo Handa END_USE(vq); 3825e05bf58STetsuo Handa 383ee7cd898SRusty Russell /* This is very unlikely, but theoretically possible. Kick 384ee7cd898SRusty Russell * just in case. */ 385ee7cd898SRusty Russell if (unlikely(vq->num_added == (1 << 16) - 1)) 386ee7cd898SRusty Russell virtqueue_kick(_vq); 387ee7cd898SRusty Russell 38898e8c6bcSRusty Russell return 0; 389780bc790SAndy Lutomirski 390780bc790SAndy Lutomirski unmap_release: 391780bc790SAndy Lutomirski err_idx = i; 392780bc790SAndy Lutomirski i = head; 393780bc790SAndy Lutomirski 394780bc790SAndy Lutomirski for (n = 0; n < total_sg; n++) { 395780bc790SAndy Lutomirski if (i == err_idx) 396780bc790SAndy Lutomirski break; 397780bc790SAndy Lutomirski vring_unmap_one(vq, &desc[i]); 398780bc790SAndy Lutomirski i = vq->vring.desc[i].next; 399780bc790SAndy Lutomirski } 400780bc790SAndy Lutomirski 401780bc790SAndy Lutomirski vq->vq.num_free += total_sg; 402780bc790SAndy Lutomirski 403780bc790SAndy Lutomirski if (indirect) 404780bc790SAndy Lutomirski kfree(desc); 405780bc790SAndy Lutomirski 406780bc790SAndy Lutomirski return -EIO; 4070a8a69ddSRusty Russell } 40813816c76SRusty Russell 40913816c76SRusty Russell /** 41013816c76SRusty Russell * virtqueue_add_sgs - expose buffers to other end 41113816c76SRusty Russell * @vq: the struct virtqueue we're talking about. 41213816c76SRusty Russell * @sgs: array of terminated scatterlists. 41313816c76SRusty Russell * @out_num: the number of scatterlists readable by other side 41413816c76SRusty Russell * @in_num: the number of scatterlists which are writable (after readable ones) 41513816c76SRusty Russell * @data: the token identifying the buffer. 41613816c76SRusty Russell * @gfp: how to do memory allocations (if necessary). 41713816c76SRusty Russell * 41813816c76SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 41913816c76SRusty Russell * at the same time (except where noted). 42013816c76SRusty Russell * 42170670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 42213816c76SRusty Russell */ 42313816c76SRusty Russell int virtqueue_add_sgs(struct virtqueue *_vq, 42413816c76SRusty Russell struct scatterlist *sgs[], 42513816c76SRusty Russell unsigned int out_sgs, 42613816c76SRusty Russell unsigned int in_sgs, 42713816c76SRusty Russell void *data, 42813816c76SRusty Russell gfp_t gfp) 42913816c76SRusty Russell { 430eeebf9b1SRusty Russell unsigned int i, total_sg = 0; 43113816c76SRusty Russell 43213816c76SRusty Russell /* Count them first. */ 433eeebf9b1SRusty Russell for (i = 0; i < out_sgs + in_sgs; i++) { 43413816c76SRusty Russell struct scatterlist *sg; 43513816c76SRusty Russell for (sg = sgs[i]; sg; sg = sg_next(sg)) 436eeebf9b1SRusty Russell total_sg++; 43713816c76SRusty Russell } 438eeebf9b1SRusty Russell return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp); 43913816c76SRusty Russell } 44013816c76SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_sgs); 44113816c76SRusty Russell 44213816c76SRusty Russell /** 443282edb36SRusty Russell * virtqueue_add_outbuf - expose output buffers to other end 444282edb36SRusty Russell * @vq: the struct virtqueue we're talking about. 445eeebf9b1SRusty Russell * @sg: scatterlist (must be well-formed and terminated!) 446eeebf9b1SRusty Russell * @num: the number of entries in @sg readable by other side 447282edb36SRusty Russell * @data: the token identifying the buffer. 448282edb36SRusty Russell * @gfp: how to do memory allocations (if necessary). 449282edb36SRusty Russell * 450282edb36SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 451282edb36SRusty Russell * at the same time (except where noted). 452282edb36SRusty Russell * 45370670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 454282edb36SRusty Russell */ 455282edb36SRusty Russell int virtqueue_add_outbuf(struct virtqueue *vq, 456eeebf9b1SRusty Russell struct scatterlist *sg, unsigned int num, 457282edb36SRusty Russell void *data, 458282edb36SRusty Russell gfp_t gfp) 459282edb36SRusty Russell { 460eeebf9b1SRusty Russell return virtqueue_add(vq, &sg, num, 1, 0, data, gfp); 461282edb36SRusty Russell } 462282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); 463282edb36SRusty Russell 464282edb36SRusty Russell /** 465282edb36SRusty Russell * virtqueue_add_inbuf - expose input buffers to other end 466282edb36SRusty Russell * @vq: the struct virtqueue we're talking about. 467eeebf9b1SRusty Russell * @sg: scatterlist (must be well-formed and terminated!) 468eeebf9b1SRusty Russell * @num: the number of entries in @sg writable by other side 469282edb36SRusty Russell * @data: the token identifying the buffer. 470282edb36SRusty Russell * @gfp: how to do memory allocations (if necessary). 471282edb36SRusty Russell * 472282edb36SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 473282edb36SRusty Russell * at the same time (except where noted). 474282edb36SRusty Russell * 47570670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 476282edb36SRusty Russell */ 477282edb36SRusty Russell int virtqueue_add_inbuf(struct virtqueue *vq, 478eeebf9b1SRusty Russell struct scatterlist *sg, unsigned int num, 479282edb36SRusty Russell void *data, 480282edb36SRusty Russell gfp_t gfp) 481282edb36SRusty Russell { 482eeebf9b1SRusty Russell return virtqueue_add(vq, &sg, num, 0, 1, data, gfp); 483282edb36SRusty Russell } 484282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); 485282edb36SRusty Russell 486282edb36SRusty Russell /** 48741f0377fSRusty Russell * virtqueue_kick_prepare - first half of split virtqueue_kick call. 4885dfc1762SRusty Russell * @vq: the struct virtqueue 4895dfc1762SRusty Russell * 49041f0377fSRusty Russell * Instead of virtqueue_kick(), you can do: 49141f0377fSRusty Russell * if (virtqueue_kick_prepare(vq)) 49241f0377fSRusty Russell * virtqueue_notify(vq); 4935dfc1762SRusty Russell * 49441f0377fSRusty Russell * This is sometimes useful because the virtqueue_kick_prepare() needs 49541f0377fSRusty Russell * to be serialized, but the actual virtqueue_notify() call does not. 4965dfc1762SRusty Russell */ 49741f0377fSRusty Russell bool virtqueue_kick_prepare(struct virtqueue *_vq) 4980a8a69ddSRusty Russell { 4990a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 500a5c262c5SMichael S. Tsirkin u16 new, old; 50141f0377fSRusty Russell bool needs_kick; 50241f0377fSRusty Russell 5030a8a69ddSRusty Russell START_USE(vq); 504a72caae2SJason Wang /* We need to expose available array entries before checking avail 505a72caae2SJason Wang * event. */ 506a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 5070a8a69ddSRusty Russell 508f277ec42SVenkatesh Srinivas old = vq->avail_idx_shadow - vq->num_added; 509f277ec42SVenkatesh Srinivas new = vq->avail_idx_shadow; 5100a8a69ddSRusty Russell vq->num_added = 0; 5110a8a69ddSRusty Russell 512e93300b1SRusty Russell #ifdef DEBUG 513e93300b1SRusty Russell if (vq->last_add_time_valid) { 514e93300b1SRusty Russell WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), 515e93300b1SRusty Russell vq->last_add_time)) > 100); 516e93300b1SRusty Russell } 517e93300b1SRusty Russell vq->last_add_time_valid = false; 518e93300b1SRusty Russell #endif 519e93300b1SRusty Russell 52041f0377fSRusty Russell if (vq->event) { 52100e6f3d9SMichael S. Tsirkin needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, vring_avail_event(&vq->vring)), 52241f0377fSRusty Russell new, old); 52341f0377fSRusty Russell } else { 52400e6f3d9SMichael S. Tsirkin needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(_vq->vdev, VRING_USED_F_NO_NOTIFY)); 52541f0377fSRusty Russell } 5260a8a69ddSRusty Russell END_USE(vq); 52741f0377fSRusty Russell return needs_kick; 52841f0377fSRusty Russell } 52941f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); 53041f0377fSRusty Russell 53141f0377fSRusty Russell /** 53241f0377fSRusty Russell * virtqueue_notify - second half of split virtqueue_kick call. 53341f0377fSRusty Russell * @vq: the struct virtqueue 53441f0377fSRusty Russell * 53541f0377fSRusty Russell * This does not need to be serialized. 5365b1bf7cbSHeinz Graalfs * 5375b1bf7cbSHeinz Graalfs * Returns false if host notify failed or queue is broken, otherwise true. 53841f0377fSRusty Russell */ 5395b1bf7cbSHeinz Graalfs bool virtqueue_notify(struct virtqueue *_vq) 54041f0377fSRusty Russell { 54141f0377fSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 54241f0377fSRusty Russell 5435b1bf7cbSHeinz Graalfs if (unlikely(vq->broken)) 5445b1bf7cbSHeinz Graalfs return false; 5455b1bf7cbSHeinz Graalfs 54641f0377fSRusty Russell /* Prod other side to tell it about changes. */ 5472342d6a6SHeinz Graalfs if (!vq->notify(_vq)) { 5485b1bf7cbSHeinz Graalfs vq->broken = true; 5495b1bf7cbSHeinz Graalfs return false; 5505b1bf7cbSHeinz Graalfs } 5515b1bf7cbSHeinz Graalfs return true; 55241f0377fSRusty Russell } 55341f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify); 55441f0377fSRusty Russell 55541f0377fSRusty Russell /** 55641f0377fSRusty Russell * virtqueue_kick - update after add_buf 55741f0377fSRusty Russell * @vq: the struct virtqueue 55841f0377fSRusty Russell * 559b3087e48SRusty Russell * After one or more virtqueue_add_* calls, invoke this to kick 56041f0377fSRusty Russell * the other side. 56141f0377fSRusty Russell * 56241f0377fSRusty Russell * Caller must ensure we don't call this with other virtqueue 56341f0377fSRusty Russell * operations at the same time (except where noted). 5645b1bf7cbSHeinz Graalfs * 5655b1bf7cbSHeinz Graalfs * Returns false if kick failed, otherwise true. 56641f0377fSRusty Russell */ 5675b1bf7cbSHeinz Graalfs bool virtqueue_kick(struct virtqueue *vq) 56841f0377fSRusty Russell { 56941f0377fSRusty Russell if (virtqueue_kick_prepare(vq)) 5705b1bf7cbSHeinz Graalfs return virtqueue_notify(vq); 5715b1bf7cbSHeinz Graalfs return true; 5720a8a69ddSRusty Russell } 5737c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick); 5740a8a69ddSRusty Russell 5750a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head) 5760a8a69ddSRusty Russell { 577780bc790SAndy Lutomirski unsigned int i, j; 578780bc790SAndy Lutomirski u16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); 5790a8a69ddSRusty Russell 5800a8a69ddSRusty Russell /* Clear data ptr. */ 581780bc790SAndy Lutomirski vq->desc_state[head].data = NULL; 5820a8a69ddSRusty Russell 583780bc790SAndy Lutomirski /* Put back on free list: unmap first-level descriptors and find end */ 5840a8a69ddSRusty Russell i = head; 5859fa29b9dSMark McLoughlin 586780bc790SAndy Lutomirski while (vq->vring.desc[i].flags & nextflag) { 587780bc790SAndy Lutomirski vring_unmap_one(vq, &vq->vring.desc[i]); 58800e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(vq->vq.vdev, vq->vring.desc[i].next); 58906ca287dSRusty Russell vq->vq.num_free++; 5900a8a69ddSRusty Russell } 5910a8a69ddSRusty Russell 592780bc790SAndy Lutomirski vring_unmap_one(vq, &vq->vring.desc[i]); 59300e6f3d9SMichael S. Tsirkin vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head); 5940a8a69ddSRusty Russell vq->free_head = head; 595780bc790SAndy Lutomirski 5960a8a69ddSRusty Russell /* Plus final descriptor */ 59706ca287dSRusty Russell vq->vq.num_free++; 598780bc790SAndy Lutomirski 599780bc790SAndy Lutomirski /* Free the indirect table, if any, now that it's unmapped. */ 600780bc790SAndy Lutomirski if (vq->desc_state[head].indir_desc) { 601780bc790SAndy Lutomirski struct vring_desc *indir_desc = vq->desc_state[head].indir_desc; 602780bc790SAndy Lutomirski u32 len = virtio32_to_cpu(vq->vq.vdev, vq->vring.desc[head].len); 603780bc790SAndy Lutomirski 604780bc790SAndy Lutomirski BUG_ON(!(vq->vring.desc[head].flags & 605780bc790SAndy Lutomirski cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT))); 606780bc790SAndy Lutomirski BUG_ON(len == 0 || len % sizeof(struct vring_desc)); 607780bc790SAndy Lutomirski 608780bc790SAndy Lutomirski for (j = 0; j < len / sizeof(struct vring_desc); j++) 609780bc790SAndy Lutomirski vring_unmap_one(vq, &indir_desc[j]); 610780bc790SAndy Lutomirski 611780bc790SAndy Lutomirski kfree(vq->desc_state[head].indir_desc); 612780bc790SAndy Lutomirski vq->desc_state[head].indir_desc = NULL; 613780bc790SAndy Lutomirski } 6140a8a69ddSRusty Russell } 6150a8a69ddSRusty Russell 6160a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq) 6170a8a69ddSRusty Russell { 61800e6f3d9SMichael S. Tsirkin return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, vq->vring.used->idx); 6190a8a69ddSRusty Russell } 6200a8a69ddSRusty Russell 6215dfc1762SRusty Russell /** 6225dfc1762SRusty Russell * virtqueue_get_buf - get the next used buffer 6235dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 6245dfc1762SRusty Russell * @len: the length written into the buffer 6255dfc1762SRusty Russell * 6265dfc1762SRusty Russell * If the driver wrote data into the buffer, @len will be set to the 6275dfc1762SRusty Russell * amount written. This means you don't need to clear the buffer 6285dfc1762SRusty Russell * beforehand to ensure there's no data leakage in the case of short 6295dfc1762SRusty Russell * writes. 6305dfc1762SRusty Russell * 6315dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 6325dfc1762SRusty Russell * operations at the same time (except where noted). 6335dfc1762SRusty Russell * 6345dfc1762SRusty Russell * Returns NULL if there are no used buffers, or the "data" token 635b3087e48SRusty Russell * handed to virtqueue_add_*(). 6365dfc1762SRusty Russell */ 6377c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) 6380a8a69ddSRusty Russell { 6390a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 6400a8a69ddSRusty Russell void *ret; 6410a8a69ddSRusty Russell unsigned int i; 6423b720b8cSRusty Russell u16 last_used; 6430a8a69ddSRusty Russell 6440a8a69ddSRusty Russell START_USE(vq); 6450a8a69ddSRusty Russell 6465ef82752SRusty Russell if (unlikely(vq->broken)) { 6475ef82752SRusty Russell END_USE(vq); 6485ef82752SRusty Russell return NULL; 6495ef82752SRusty Russell } 6505ef82752SRusty Russell 6510a8a69ddSRusty Russell if (!more_used(vq)) { 6520a8a69ddSRusty Russell pr_debug("No more buffers in queue\n"); 6530a8a69ddSRusty Russell END_USE(vq); 6540a8a69ddSRusty Russell return NULL; 6550a8a69ddSRusty Russell } 6560a8a69ddSRusty Russell 6572d61ba95SMichael S. Tsirkin /* Only get used array entries after they have been exposed by host. */ 658a9a0fef7SRusty Russell virtio_rmb(vq->weak_barriers); 6592d61ba95SMichael S. Tsirkin 6603b720b8cSRusty Russell last_used = (vq->last_used_idx & (vq->vring.num - 1)); 66100e6f3d9SMichael S. Tsirkin i = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].id); 66200e6f3d9SMichael S. Tsirkin *len = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].len); 6630a8a69ddSRusty Russell 6640a8a69ddSRusty Russell if (unlikely(i >= vq->vring.num)) { 6650a8a69ddSRusty Russell BAD_RING(vq, "id %u out of range\n", i); 6660a8a69ddSRusty Russell return NULL; 6670a8a69ddSRusty Russell } 668780bc790SAndy Lutomirski if (unlikely(!vq->desc_state[i].data)) { 6690a8a69ddSRusty Russell BAD_RING(vq, "id %u is not a head!\n", i); 6700a8a69ddSRusty Russell return NULL; 6710a8a69ddSRusty Russell } 6720a8a69ddSRusty Russell 6730a8a69ddSRusty Russell /* detach_buf clears data, so grab it now. */ 674780bc790SAndy Lutomirski ret = vq->desc_state[i].data; 6750a8a69ddSRusty Russell detach_buf(vq, i); 6760a8a69ddSRusty Russell vq->last_used_idx++; 677a5c262c5SMichael S. Tsirkin /* If we expect an interrupt for the next entry, tell host 678a5c262c5SMichael S. Tsirkin * by writing event index and flush out the write before 679a5c262c5SMichael S. Tsirkin * the read in the next get_buf call. */ 680788e5b3aSMichael S. Tsirkin if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) 681788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 682788e5b3aSMichael S. Tsirkin &vring_used_event(&vq->vring), 683788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); 684a5c262c5SMichael S. Tsirkin 685e93300b1SRusty Russell #ifdef DEBUG 686e93300b1SRusty Russell vq->last_add_time_valid = false; 687e93300b1SRusty Russell #endif 688e93300b1SRusty Russell 6890a8a69ddSRusty Russell END_USE(vq); 6900a8a69ddSRusty Russell return ret; 6910a8a69ddSRusty Russell } 6927c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf); 6930a8a69ddSRusty Russell 6945dfc1762SRusty Russell /** 6955dfc1762SRusty Russell * virtqueue_disable_cb - disable callbacks 6965dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 6975dfc1762SRusty Russell * 6985dfc1762SRusty Russell * Note that this is not necessarily synchronous, hence unreliable and only 6995dfc1762SRusty Russell * useful as an optimization. 7005dfc1762SRusty Russell * 7015dfc1762SRusty Russell * Unlike other operations, this need not be serialized. 7025dfc1762SRusty Russell */ 7037c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq) 70418445c4dSRusty Russell { 70518445c4dSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 70618445c4dSRusty Russell 707f277ec42SVenkatesh Srinivas if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { 708f277ec42SVenkatesh Srinivas vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 709f277ec42SVenkatesh Srinivas vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow); 710f277ec42SVenkatesh Srinivas } 711f277ec42SVenkatesh Srinivas 71218445c4dSRusty Russell } 7137c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb); 71418445c4dSRusty Russell 7155dfc1762SRusty Russell /** 716cc229884SMichael S. Tsirkin * virtqueue_enable_cb_prepare - restart callbacks after disable_cb 717cc229884SMichael S. Tsirkin * @vq: the struct virtqueue we're talking about. 718cc229884SMichael S. Tsirkin * 719cc229884SMichael S. Tsirkin * This re-enables callbacks; it returns current queue state 720cc229884SMichael S. Tsirkin * in an opaque unsigned value. This value should be later tested by 721cc229884SMichael S. Tsirkin * virtqueue_poll, to detect a possible race between the driver checking for 722cc229884SMichael S. Tsirkin * more work, and enabling callbacks. 723cc229884SMichael S. Tsirkin * 724cc229884SMichael S. Tsirkin * Caller must ensure we don't call this with other virtqueue 725cc229884SMichael S. Tsirkin * operations at the same time (except where noted). 726cc229884SMichael S. Tsirkin */ 727cc229884SMichael S. Tsirkin unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) 728cc229884SMichael S. Tsirkin { 729cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 730cc229884SMichael S. Tsirkin u16 last_used_idx; 731cc229884SMichael S. Tsirkin 732cc229884SMichael S. Tsirkin START_USE(vq); 733cc229884SMichael S. Tsirkin 734cc229884SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 735cc229884SMichael S. Tsirkin * more to do. */ 736cc229884SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to 737cc229884SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 738cc229884SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 739f277ec42SVenkatesh Srinivas if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 740f277ec42SVenkatesh Srinivas vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 741f277ec42SVenkatesh Srinivas vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow); 742f277ec42SVenkatesh Srinivas } 74300e6f3d9SMichael S. Tsirkin vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, last_used_idx = vq->last_used_idx); 744cc229884SMichael S. Tsirkin END_USE(vq); 745cc229884SMichael S. Tsirkin return last_used_idx; 746cc229884SMichael S. Tsirkin } 747cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); 748cc229884SMichael S. Tsirkin 749cc229884SMichael S. Tsirkin /** 750cc229884SMichael S. Tsirkin * virtqueue_poll - query pending used buffers 751cc229884SMichael S. Tsirkin * @vq: the struct virtqueue we're talking about. 752cc229884SMichael S. Tsirkin * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). 753cc229884SMichael S. Tsirkin * 754cc229884SMichael S. Tsirkin * Returns "true" if there are pending used buffers in the queue. 755cc229884SMichael S. Tsirkin * 756cc229884SMichael S. Tsirkin * This does not need to be serialized. 757cc229884SMichael S. Tsirkin */ 758cc229884SMichael S. Tsirkin bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) 759cc229884SMichael S. Tsirkin { 760cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 761cc229884SMichael S. Tsirkin 762cc229884SMichael S. Tsirkin virtio_mb(vq->weak_barriers); 76300e6f3d9SMichael S. Tsirkin return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, vq->vring.used->idx); 764cc229884SMichael S. Tsirkin } 765cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_poll); 766cc229884SMichael S. Tsirkin 767cc229884SMichael S. Tsirkin /** 7685dfc1762SRusty Russell * virtqueue_enable_cb - restart callbacks after disable_cb. 7695dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 7705dfc1762SRusty Russell * 7715dfc1762SRusty Russell * This re-enables callbacks; it returns "false" if there are pending 7725dfc1762SRusty Russell * buffers in the queue, to detect a possible race between the driver 7735dfc1762SRusty Russell * checking for more work, and enabling callbacks. 7745dfc1762SRusty Russell * 7755dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 7765dfc1762SRusty Russell * operations at the same time (except where noted). 7775dfc1762SRusty Russell */ 7787c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq) 7790a8a69ddSRusty Russell { 780cc229884SMichael S. Tsirkin unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); 781cc229884SMichael S. Tsirkin return !virtqueue_poll(_vq, last_used_idx); 7820a8a69ddSRusty Russell } 7837c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb); 7840a8a69ddSRusty Russell 7855dfc1762SRusty Russell /** 7865dfc1762SRusty Russell * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. 7875dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 7885dfc1762SRusty Russell * 7895dfc1762SRusty Russell * This re-enables callbacks but hints to the other side to delay 7905dfc1762SRusty Russell * interrupts until most of the available buffers have been processed; 7915dfc1762SRusty Russell * it returns "false" if there are many pending buffers in the queue, 7925dfc1762SRusty Russell * to detect a possible race between the driver checking for more work, 7935dfc1762SRusty Russell * and enabling callbacks. 7945dfc1762SRusty Russell * 7955dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 7965dfc1762SRusty Russell * operations at the same time (except where noted). 7975dfc1762SRusty Russell */ 7987ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) 7997ab358c2SMichael S. Tsirkin { 8007ab358c2SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 8017ab358c2SMichael S. Tsirkin u16 bufs; 8027ab358c2SMichael S. Tsirkin 8037ab358c2SMichael S. Tsirkin START_USE(vq); 8047ab358c2SMichael S. Tsirkin 8057ab358c2SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 8067ab358c2SMichael S. Tsirkin * more to do. */ 8077ab358c2SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to 8087ab358c2SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 8097ab358c2SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 810f277ec42SVenkatesh Srinivas if (vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) { 811f277ec42SVenkatesh Srinivas vq->avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT; 812f277ec42SVenkatesh Srinivas vq->vring.avail->flags = cpu_to_virtio16(_vq->vdev, vq->avail_flags_shadow); 813f277ec42SVenkatesh Srinivas } 8147ab358c2SMichael S. Tsirkin /* TODO: tune this threshold */ 815f277ec42SVenkatesh Srinivas bufs = (u16)(vq->avail_idx_shadow - vq->last_used_idx) * 3 / 4; 816788e5b3aSMichael S. Tsirkin 817788e5b3aSMichael S. Tsirkin virtio_store_mb(vq->weak_barriers, 818788e5b3aSMichael S. Tsirkin &vring_used_event(&vq->vring), 819788e5b3aSMichael S. Tsirkin cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); 820788e5b3aSMichael S. Tsirkin 82100e6f3d9SMichael S. Tsirkin if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) { 8227ab358c2SMichael S. Tsirkin END_USE(vq); 8237ab358c2SMichael S. Tsirkin return false; 8247ab358c2SMichael S. Tsirkin } 8257ab358c2SMichael S. Tsirkin 8267ab358c2SMichael S. Tsirkin END_USE(vq); 8277ab358c2SMichael S. Tsirkin return true; 8287ab358c2SMichael S. Tsirkin } 8297ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); 8307ab358c2SMichael S. Tsirkin 8315dfc1762SRusty Russell /** 8325dfc1762SRusty Russell * virtqueue_detach_unused_buf - detach first unused buffer 8335dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 8345dfc1762SRusty Russell * 835b3087e48SRusty Russell * Returns NULL or the "data" token handed to virtqueue_add_*(). 8365dfc1762SRusty Russell * This is not valid on an active queue; it is useful only for device 8375dfc1762SRusty Russell * shutdown. 8385dfc1762SRusty Russell */ 8397c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq) 840c021eac4SShirley Ma { 841c021eac4SShirley Ma struct vring_virtqueue *vq = to_vvq(_vq); 842c021eac4SShirley Ma unsigned int i; 843c021eac4SShirley Ma void *buf; 844c021eac4SShirley Ma 845c021eac4SShirley Ma START_USE(vq); 846c021eac4SShirley Ma 847c021eac4SShirley Ma for (i = 0; i < vq->vring.num; i++) { 848780bc790SAndy Lutomirski if (!vq->desc_state[i].data) 849c021eac4SShirley Ma continue; 850c021eac4SShirley Ma /* detach_buf clears data, so grab it now. */ 851780bc790SAndy Lutomirski buf = vq->desc_state[i].data; 852c021eac4SShirley Ma detach_buf(vq, i); 853f277ec42SVenkatesh Srinivas vq->avail_idx_shadow--; 854f277ec42SVenkatesh Srinivas vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, vq->avail_idx_shadow); 855c021eac4SShirley Ma END_USE(vq); 856c021eac4SShirley Ma return buf; 857c021eac4SShirley Ma } 858c021eac4SShirley Ma /* That should have freed everything. */ 85906ca287dSRusty Russell BUG_ON(vq->vq.num_free != vq->vring.num); 860c021eac4SShirley Ma 861c021eac4SShirley Ma END_USE(vq); 862c021eac4SShirley Ma return NULL; 863c021eac4SShirley Ma } 8647c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); 865c021eac4SShirley Ma 8660a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq) 8670a8a69ddSRusty Russell { 8680a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 8690a8a69ddSRusty Russell 8700a8a69ddSRusty Russell if (!more_used(vq)) { 8710a8a69ddSRusty Russell pr_debug("virtqueue interrupt with no work for %p\n", vq); 8720a8a69ddSRusty Russell return IRQ_NONE; 8730a8a69ddSRusty Russell } 8740a8a69ddSRusty Russell 8750a8a69ddSRusty Russell if (unlikely(vq->broken)) 8760a8a69ddSRusty Russell return IRQ_HANDLED; 8770a8a69ddSRusty Russell 8780a8a69ddSRusty Russell pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); 87918445c4dSRusty Russell if (vq->vq.callback) 88018445c4dSRusty Russell vq->vq.callback(&vq->vq); 8810a8a69ddSRusty Russell 8820a8a69ddSRusty Russell return IRQ_HANDLED; 8830a8a69ddSRusty Russell } 884c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt); 8850a8a69ddSRusty Russell 886*2a2d1382SAndy Lutomirski struct virtqueue *__vring_new_virtqueue(unsigned int index, 887*2a2d1382SAndy Lutomirski struct vring vring, 8880a8a69ddSRusty Russell struct virtio_device *vdev, 8897b21e34fSRusty Russell bool weak_barriers, 89046f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *), 8919499f5e7SRusty Russell void (*callback)(struct virtqueue *), 8929499f5e7SRusty Russell const char *name) 8930a8a69ddSRusty Russell { 8940a8a69ddSRusty Russell unsigned int i; 895*2a2d1382SAndy Lutomirski struct vring_virtqueue *vq; 8960a8a69ddSRusty Russell 897*2a2d1382SAndy Lutomirski vq = kmalloc(sizeof(*vq) + vring.num * sizeof(struct vring_desc_state), 898780bc790SAndy Lutomirski GFP_KERNEL); 8990a8a69ddSRusty Russell if (!vq) 9000a8a69ddSRusty Russell return NULL; 9010a8a69ddSRusty Russell 902*2a2d1382SAndy Lutomirski vq->vring = vring; 9030a8a69ddSRusty Russell vq->vq.callback = callback; 9040a8a69ddSRusty Russell vq->vq.vdev = vdev; 9059499f5e7SRusty Russell vq->vq.name = name; 906*2a2d1382SAndy Lutomirski vq->vq.num_free = vring.num; 90706ca287dSRusty Russell vq->vq.index = index; 908*2a2d1382SAndy Lutomirski vq->we_own_ring = false; 909*2a2d1382SAndy Lutomirski vq->queue_dma_addr = 0; 910*2a2d1382SAndy Lutomirski vq->queue_size_in_bytes = 0; 9110a8a69ddSRusty Russell vq->notify = notify; 9127b21e34fSRusty Russell vq->weak_barriers = weak_barriers; 9130a8a69ddSRusty Russell vq->broken = false; 9140a8a69ddSRusty Russell vq->last_used_idx = 0; 915f277ec42SVenkatesh Srinivas vq->avail_flags_shadow = 0; 916f277ec42SVenkatesh Srinivas vq->avail_idx_shadow = 0; 9170a8a69ddSRusty Russell vq->num_added = 0; 9189499f5e7SRusty Russell list_add_tail(&vq->vq.list, &vdev->vqs); 9190a8a69ddSRusty Russell #ifdef DEBUG 9200a8a69ddSRusty Russell vq->in_use = false; 921e93300b1SRusty Russell vq->last_add_time_valid = false; 9220a8a69ddSRusty Russell #endif 9230a8a69ddSRusty Russell 9249fa29b9dSMark McLoughlin vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); 925a5c262c5SMichael S. Tsirkin vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 9269fa29b9dSMark McLoughlin 9270a8a69ddSRusty Russell /* No callback? Tell other side not to bother us. */ 928f277ec42SVenkatesh Srinivas if (!callback) { 929f277ec42SVenkatesh Srinivas vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; 930f277ec42SVenkatesh Srinivas vq->vring.avail->flags = cpu_to_virtio16(vdev, vq->avail_flags_shadow); 931f277ec42SVenkatesh Srinivas } 9320a8a69ddSRusty Russell 9330a8a69ddSRusty Russell /* Put everything in free lists. */ 9340a8a69ddSRusty Russell vq->free_head = 0; 935*2a2d1382SAndy Lutomirski for (i = 0; i < vring.num-1; i++) 93600e6f3d9SMichael S. Tsirkin vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1); 937*2a2d1382SAndy Lutomirski memset(vq->desc_state, 0, vring.num * sizeof(struct vring_desc_state)); 9380a8a69ddSRusty Russell 9390a8a69ddSRusty Russell return &vq->vq; 9400a8a69ddSRusty Russell } 941*2a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(__vring_new_virtqueue); 942*2a2d1382SAndy Lutomirski 943*2a2d1382SAndy Lutomirski static void *vring_alloc_queue(struct virtio_device *vdev, size_t size, 944*2a2d1382SAndy Lutomirski dma_addr_t *dma_handle, gfp_t flag) 945*2a2d1382SAndy Lutomirski { 946*2a2d1382SAndy Lutomirski if (vring_use_dma_api(vdev)) { 947*2a2d1382SAndy Lutomirski return dma_alloc_coherent(vdev->dev.parent, size, 948*2a2d1382SAndy Lutomirski dma_handle, flag); 949*2a2d1382SAndy Lutomirski } else { 950*2a2d1382SAndy Lutomirski void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag); 951*2a2d1382SAndy Lutomirski if (queue) { 952*2a2d1382SAndy Lutomirski phys_addr_t phys_addr = virt_to_phys(queue); 953*2a2d1382SAndy Lutomirski *dma_handle = (dma_addr_t)phys_addr; 954*2a2d1382SAndy Lutomirski 955*2a2d1382SAndy Lutomirski /* 956*2a2d1382SAndy Lutomirski * Sanity check: make sure we dind't truncate 957*2a2d1382SAndy Lutomirski * the address. The only arches I can find that 958*2a2d1382SAndy Lutomirski * have 64-bit phys_addr_t but 32-bit dma_addr_t 959*2a2d1382SAndy Lutomirski * are certain non-highmem MIPS and x86 960*2a2d1382SAndy Lutomirski * configurations, but these configurations 961*2a2d1382SAndy Lutomirski * should never allocate physical pages above 32 962*2a2d1382SAndy Lutomirski * bits, so this is fine. Just in case, throw a 963*2a2d1382SAndy Lutomirski * warning and abort if we end up with an 964*2a2d1382SAndy Lutomirski * unrepresentable address. 965*2a2d1382SAndy Lutomirski */ 966*2a2d1382SAndy Lutomirski if (WARN_ON_ONCE(*dma_handle != phys_addr)) { 967*2a2d1382SAndy Lutomirski free_pages_exact(queue, PAGE_ALIGN(size)); 968*2a2d1382SAndy Lutomirski return NULL; 969*2a2d1382SAndy Lutomirski } 970*2a2d1382SAndy Lutomirski } 971*2a2d1382SAndy Lutomirski return queue; 972*2a2d1382SAndy Lutomirski } 973*2a2d1382SAndy Lutomirski } 974*2a2d1382SAndy Lutomirski 975*2a2d1382SAndy Lutomirski static void vring_free_queue(struct virtio_device *vdev, size_t size, 976*2a2d1382SAndy Lutomirski void *queue, dma_addr_t dma_handle) 977*2a2d1382SAndy Lutomirski { 978*2a2d1382SAndy Lutomirski if (vring_use_dma_api(vdev)) { 979*2a2d1382SAndy Lutomirski dma_free_coherent(vdev->dev.parent, size, queue, dma_handle); 980*2a2d1382SAndy Lutomirski } else { 981*2a2d1382SAndy Lutomirski free_pages_exact(queue, PAGE_ALIGN(size)); 982*2a2d1382SAndy Lutomirski } 983*2a2d1382SAndy Lutomirski } 984*2a2d1382SAndy Lutomirski 985*2a2d1382SAndy Lutomirski struct virtqueue *vring_create_virtqueue( 986*2a2d1382SAndy Lutomirski unsigned int index, 987*2a2d1382SAndy Lutomirski unsigned int num, 988*2a2d1382SAndy Lutomirski unsigned int vring_align, 989*2a2d1382SAndy Lutomirski struct virtio_device *vdev, 990*2a2d1382SAndy Lutomirski bool weak_barriers, 991*2a2d1382SAndy Lutomirski bool may_reduce_num, 992*2a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *), 993*2a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *), 994*2a2d1382SAndy Lutomirski const char *name) 995*2a2d1382SAndy Lutomirski { 996*2a2d1382SAndy Lutomirski struct virtqueue *vq; 997*2a2d1382SAndy Lutomirski void *queue; 998*2a2d1382SAndy Lutomirski dma_addr_t dma_addr; 999*2a2d1382SAndy Lutomirski size_t queue_size_in_bytes; 1000*2a2d1382SAndy Lutomirski struct vring vring; 1001*2a2d1382SAndy Lutomirski 1002*2a2d1382SAndy Lutomirski /* We assume num is a power of 2. */ 1003*2a2d1382SAndy Lutomirski if (num & (num - 1)) { 1004*2a2d1382SAndy Lutomirski dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); 1005*2a2d1382SAndy Lutomirski return NULL; 1006*2a2d1382SAndy Lutomirski } 1007*2a2d1382SAndy Lutomirski 1008*2a2d1382SAndy Lutomirski /* TODO: allocate each queue chunk individually */ 1009*2a2d1382SAndy Lutomirski for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { 1010*2a2d1382SAndy Lutomirski queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 1011*2a2d1382SAndy Lutomirski &dma_addr, 1012*2a2d1382SAndy Lutomirski GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO); 1013*2a2d1382SAndy Lutomirski if (queue) 1014*2a2d1382SAndy Lutomirski break; 1015*2a2d1382SAndy Lutomirski } 1016*2a2d1382SAndy Lutomirski 1017*2a2d1382SAndy Lutomirski if (!num) 1018*2a2d1382SAndy Lutomirski return NULL; 1019*2a2d1382SAndy Lutomirski 1020*2a2d1382SAndy Lutomirski if (!queue) { 1021*2a2d1382SAndy Lutomirski /* Try to get a single page. You are my only hope! */ 1022*2a2d1382SAndy Lutomirski queue = vring_alloc_queue(vdev, vring_size(num, vring_align), 1023*2a2d1382SAndy Lutomirski &dma_addr, GFP_KERNEL|__GFP_ZERO); 1024*2a2d1382SAndy Lutomirski } 1025*2a2d1382SAndy Lutomirski if (!queue) 1026*2a2d1382SAndy Lutomirski return NULL; 1027*2a2d1382SAndy Lutomirski 1028*2a2d1382SAndy Lutomirski queue_size_in_bytes = vring_size(num, vring_align); 1029*2a2d1382SAndy Lutomirski vring_init(&vring, num, queue, vring_align); 1030*2a2d1382SAndy Lutomirski 1031*2a2d1382SAndy Lutomirski vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, 1032*2a2d1382SAndy Lutomirski notify, callback, name); 1033*2a2d1382SAndy Lutomirski if (!vq) { 1034*2a2d1382SAndy Lutomirski vring_free_queue(vdev, queue_size_in_bytes, queue, 1035*2a2d1382SAndy Lutomirski dma_addr); 1036*2a2d1382SAndy Lutomirski return NULL; 1037*2a2d1382SAndy Lutomirski } 1038*2a2d1382SAndy Lutomirski 1039*2a2d1382SAndy Lutomirski to_vvq(vq)->queue_dma_addr = dma_addr; 1040*2a2d1382SAndy Lutomirski to_vvq(vq)->queue_size_in_bytes = queue_size_in_bytes; 1041*2a2d1382SAndy Lutomirski to_vvq(vq)->we_own_ring = true; 1042*2a2d1382SAndy Lutomirski 1043*2a2d1382SAndy Lutomirski return vq; 1044*2a2d1382SAndy Lutomirski } 1045*2a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(vring_create_virtqueue); 1046*2a2d1382SAndy Lutomirski 1047*2a2d1382SAndy Lutomirski struct virtqueue *vring_new_virtqueue(unsigned int index, 1048*2a2d1382SAndy Lutomirski unsigned int num, 1049*2a2d1382SAndy Lutomirski unsigned int vring_align, 1050*2a2d1382SAndy Lutomirski struct virtio_device *vdev, 1051*2a2d1382SAndy Lutomirski bool weak_barriers, 1052*2a2d1382SAndy Lutomirski void *pages, 1053*2a2d1382SAndy Lutomirski bool (*notify)(struct virtqueue *vq), 1054*2a2d1382SAndy Lutomirski void (*callback)(struct virtqueue *vq), 1055*2a2d1382SAndy Lutomirski const char *name) 1056*2a2d1382SAndy Lutomirski { 1057*2a2d1382SAndy Lutomirski struct vring vring; 1058*2a2d1382SAndy Lutomirski vring_init(&vring, num, pages, vring_align); 1059*2a2d1382SAndy Lutomirski return __vring_new_virtqueue(index, vring, vdev, weak_barriers, 1060*2a2d1382SAndy Lutomirski notify, callback, name); 1061*2a2d1382SAndy Lutomirski } 1062c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue); 10630a8a69ddSRusty Russell 1064*2a2d1382SAndy Lutomirski void vring_del_virtqueue(struct virtqueue *_vq) 10650a8a69ddSRusty Russell { 1066*2a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 1067*2a2d1382SAndy Lutomirski 1068*2a2d1382SAndy Lutomirski if (vq->we_own_ring) { 1069*2a2d1382SAndy Lutomirski vring_free_queue(vq->vq.vdev, vq->queue_size_in_bytes, 1070*2a2d1382SAndy Lutomirski vq->vring.desc, vq->queue_dma_addr); 1071*2a2d1382SAndy Lutomirski } 1072*2a2d1382SAndy Lutomirski list_del(&_vq->list); 1073*2a2d1382SAndy Lutomirski kfree(vq); 10740a8a69ddSRusty Russell } 1075c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue); 10760a8a69ddSRusty Russell 1077e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */ 1078e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev) 1079e34f8725SRusty Russell { 1080e34f8725SRusty Russell unsigned int i; 1081e34f8725SRusty Russell 1082e34f8725SRusty Russell for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { 1083e34f8725SRusty Russell switch (i) { 10849fa29b9dSMark McLoughlin case VIRTIO_RING_F_INDIRECT_DESC: 10859fa29b9dSMark McLoughlin break; 1086a5c262c5SMichael S. Tsirkin case VIRTIO_RING_F_EVENT_IDX: 1087a5c262c5SMichael S. Tsirkin break; 1088747ae34aSMichael S. Tsirkin case VIRTIO_F_VERSION_1: 1089747ae34aSMichael S. Tsirkin break; 1090e34f8725SRusty Russell default: 1091e34f8725SRusty Russell /* We don't understand this bit. */ 1092e16e12beSMichael S. Tsirkin __virtio_clear_bit(vdev, i); 1093e34f8725SRusty Russell } 1094e34f8725SRusty Russell } 1095e34f8725SRusty Russell } 1096e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features); 1097e34f8725SRusty Russell 10985dfc1762SRusty Russell /** 10995dfc1762SRusty Russell * virtqueue_get_vring_size - return the size of the virtqueue's vring 11005dfc1762SRusty Russell * @vq: the struct virtqueue containing the vring of interest. 11015dfc1762SRusty Russell * 11025dfc1762SRusty Russell * Returns the size of the vring. This is mainly used for boasting to 11035dfc1762SRusty Russell * userspace. Unlike other operations, this need not be serialized. 11045dfc1762SRusty Russell */ 11058f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) 11068f9f4668SRick Jones { 11078f9f4668SRick Jones 11088f9f4668SRick Jones struct vring_virtqueue *vq = to_vvq(_vq); 11098f9f4668SRick Jones 11108f9f4668SRick Jones return vq->vring.num; 11118f9f4668SRick Jones } 11128f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); 11138f9f4668SRick Jones 1114b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq) 1115b3b32c94SHeinz Graalfs { 1116b3b32c94SHeinz Graalfs struct vring_virtqueue *vq = to_vvq(_vq); 1117b3b32c94SHeinz Graalfs 1118b3b32c94SHeinz Graalfs return vq->broken; 1119b3b32c94SHeinz Graalfs } 1120b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken); 1121b3b32c94SHeinz Graalfs 1122e2dcdfe9SRusty Russell /* 1123e2dcdfe9SRusty Russell * This should prevent the device from being used, allowing drivers to 1124e2dcdfe9SRusty Russell * recover. You may need to grab appropriate locks to flush. 1125e2dcdfe9SRusty Russell */ 1126e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev) 1127e2dcdfe9SRusty Russell { 1128e2dcdfe9SRusty Russell struct virtqueue *_vq; 1129e2dcdfe9SRusty Russell 1130e2dcdfe9SRusty Russell list_for_each_entry(_vq, &dev->vqs, list) { 1131e2dcdfe9SRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 1132e2dcdfe9SRusty Russell vq->broken = true; 1133e2dcdfe9SRusty Russell } 1134e2dcdfe9SRusty Russell } 1135e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device); 1136e2dcdfe9SRusty Russell 1137*2a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq) 113889062652SCornelia Huck { 113989062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 114089062652SCornelia Huck 1141*2a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 114289062652SCornelia Huck 1143*2a2d1382SAndy Lutomirski return vq->queue_dma_addr; 1144*2a2d1382SAndy Lutomirski } 1145*2a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr); 1146*2a2d1382SAndy Lutomirski 1147*2a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq) 114889062652SCornelia Huck { 114989062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 115089062652SCornelia Huck 1151*2a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 1152*2a2d1382SAndy Lutomirski 1153*2a2d1382SAndy Lutomirski return vq->queue_dma_addr + 1154*2a2d1382SAndy Lutomirski ((char *)vq->vring.avail - (char *)vq->vring.desc); 115589062652SCornelia Huck } 1156*2a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr); 1157*2a2d1382SAndy Lutomirski 1158*2a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq) 1159*2a2d1382SAndy Lutomirski { 1160*2a2d1382SAndy Lutomirski struct vring_virtqueue *vq = to_vvq(_vq); 1161*2a2d1382SAndy Lutomirski 1162*2a2d1382SAndy Lutomirski BUG_ON(!vq->we_own_ring); 1163*2a2d1382SAndy Lutomirski 1164*2a2d1382SAndy Lutomirski return vq->queue_dma_addr + 1165*2a2d1382SAndy Lutomirski ((char *)vq->vring.used - (char *)vq->vring.desc); 1166*2a2d1382SAndy Lutomirski } 1167*2a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_used_addr); 1168*2a2d1382SAndy Lutomirski 1169*2a2d1382SAndy Lutomirski const struct vring *virtqueue_get_vring(struct virtqueue *vq) 1170*2a2d1382SAndy Lutomirski { 1171*2a2d1382SAndy Lutomirski return &to_vvq(vq)->vring; 1172*2a2d1382SAndy Lutomirski } 1173*2a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_vring); 117489062652SCornelia Huck 1175c6fd4701SRusty Russell MODULE_LICENSE("GPL"); 1176