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> 270a8a69ddSRusty Russell 280a8a69ddSRusty Russell #ifdef DEBUG 290a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */ 309499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 319499f5e7SRusty Russell do { \ 329499f5e7SRusty Russell dev_err(&(_vq)->vq.vdev->dev, \ 339499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 349499f5e7SRusty Russell BUG(); \ 359499f5e7SRusty Russell } while (0) 36c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */ 373a35ce7dSRoel Kluin #define START_USE(_vq) \ 38c5f841f1SRusty Russell do { \ 39c5f841f1SRusty Russell if ((_vq)->in_use) \ 409499f5e7SRusty Russell panic("%s:in_use = %i\n", \ 419499f5e7SRusty Russell (_vq)->vq.name, (_vq)->in_use); \ 42c5f841f1SRusty Russell (_vq)->in_use = __LINE__; \ 43c5f841f1SRusty Russell } while (0) 443a35ce7dSRoel Kluin #define END_USE(_vq) \ 4597a545abSRusty Russell do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0) 460a8a69ddSRusty Russell #else 479499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...) \ 489499f5e7SRusty Russell do { \ 499499f5e7SRusty Russell dev_err(&_vq->vq.vdev->dev, \ 509499f5e7SRusty Russell "%s:"fmt, (_vq)->vq.name, ##args); \ 519499f5e7SRusty Russell (_vq)->broken = true; \ 529499f5e7SRusty Russell } while (0) 530a8a69ddSRusty Russell #define START_USE(vq) 540a8a69ddSRusty Russell #define END_USE(vq) 550a8a69ddSRusty Russell #endif 560a8a69ddSRusty Russell 570a8a69ddSRusty Russell struct vring_virtqueue 580a8a69ddSRusty Russell { 590a8a69ddSRusty Russell struct virtqueue vq; 600a8a69ddSRusty Russell 610a8a69ddSRusty Russell /* Actual memory layout for this queue */ 620a8a69ddSRusty Russell struct vring vring; 630a8a69ddSRusty Russell 647b21e34fSRusty Russell /* Can we use weak barriers? */ 657b21e34fSRusty Russell bool weak_barriers; 667b21e34fSRusty Russell 670a8a69ddSRusty Russell /* Other side has made a mess, don't try any more. */ 680a8a69ddSRusty Russell bool broken; 690a8a69ddSRusty Russell 709fa29b9dSMark McLoughlin /* Host supports indirect buffers */ 719fa29b9dSMark McLoughlin bool indirect; 729fa29b9dSMark McLoughlin 73a5c262c5SMichael S. Tsirkin /* Host publishes avail event idx */ 74a5c262c5SMichael S. Tsirkin bool event; 75a5c262c5SMichael S. Tsirkin 760a8a69ddSRusty Russell /* Head of free buffer list. */ 770a8a69ddSRusty Russell unsigned int free_head; 780a8a69ddSRusty Russell /* Number we've added since last sync. */ 790a8a69ddSRusty Russell unsigned int num_added; 800a8a69ddSRusty Russell 810a8a69ddSRusty Russell /* Last used index we've seen. */ 821bc4953eSAnthony Liguori u16 last_used_idx; 830a8a69ddSRusty Russell 840a8a69ddSRusty Russell /* How to notify other side. FIXME: commonalize hcalls! */ 8546f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *vq); 860a8a69ddSRusty Russell 870a8a69ddSRusty Russell #ifdef DEBUG 880a8a69ddSRusty Russell /* They're supposed to lock for us. */ 890a8a69ddSRusty Russell unsigned int in_use; 90e93300b1SRusty Russell 91e93300b1SRusty Russell /* Figure out if their kicks are too delayed. */ 92e93300b1SRusty Russell bool last_add_time_valid; 93e93300b1SRusty Russell ktime_t last_add_time; 940a8a69ddSRusty Russell #endif 950a8a69ddSRusty Russell 960a8a69ddSRusty Russell /* Tokens for callbacks. */ 970a8a69ddSRusty Russell void *data[]; 980a8a69ddSRusty Russell }; 990a8a69ddSRusty Russell 1000a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq) 1010a8a69ddSRusty Russell 10213816c76SRusty Russell static inline struct scatterlist *sg_next_chained(struct scatterlist *sg, 10313816c76SRusty Russell unsigned int *count) 10413816c76SRusty Russell { 10513816c76SRusty Russell return sg_next(sg); 10613816c76SRusty Russell } 10713816c76SRusty Russell 10813816c76SRusty Russell static inline struct scatterlist *sg_next_arr(struct scatterlist *sg, 10913816c76SRusty Russell unsigned int *count) 11013816c76SRusty Russell { 11113816c76SRusty Russell if (--(*count) == 0) 11213816c76SRusty Russell return NULL; 11313816c76SRusty Russell return sg + 1; 11413816c76SRusty Russell } 11513816c76SRusty Russell 1169fa29b9dSMark McLoughlin /* Set up an indirect table of descriptors and add it to the queue. */ 11713816c76SRusty Russell static inline int vring_add_indirect(struct vring_virtqueue *vq, 11813816c76SRusty Russell struct scatterlist *sgs[], 11913816c76SRusty Russell struct scatterlist *(*next) 12013816c76SRusty Russell (struct scatterlist *, unsigned int *), 12113816c76SRusty Russell unsigned int total_sg, 12213816c76SRusty Russell unsigned int total_out, 12313816c76SRusty Russell unsigned int total_in, 12413816c76SRusty Russell unsigned int out_sgs, 12513816c76SRusty Russell unsigned int in_sgs, 126bbd603efSMichael S. Tsirkin gfp_t gfp) 1279fa29b9dSMark McLoughlin { 1289fa29b9dSMark McLoughlin struct vring_desc *desc; 1299fa29b9dSMark McLoughlin unsigned head; 13013816c76SRusty Russell struct scatterlist *sg; 13113816c76SRusty Russell int i, n; 1329fa29b9dSMark McLoughlin 133b92b1b89SWill Deacon /* 134b92b1b89SWill Deacon * We require lowmem mappings for the descriptors because 135b92b1b89SWill Deacon * otherwise virt_to_phys will give us bogus addresses in the 136b92b1b89SWill Deacon * virtqueue. 137b92b1b89SWill Deacon */ 138b92b1b89SWill Deacon gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH); 139b92b1b89SWill Deacon 14013816c76SRusty Russell desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp); 1419fa29b9dSMark McLoughlin if (!desc) 142686d3637SMichael S. Tsirkin return -ENOMEM; 1439fa29b9dSMark McLoughlin 14413816c76SRusty Russell /* Transfer entries from the sg lists into the indirect page */ 14513816c76SRusty Russell i = 0; 14613816c76SRusty Russell for (n = 0; n < out_sgs; n++) { 14713816c76SRusty Russell for (sg = sgs[n]; sg; sg = next(sg, &total_out)) { 1489fa29b9dSMark McLoughlin desc[i].flags = VRING_DESC_F_NEXT; 1499fa29b9dSMark McLoughlin desc[i].addr = sg_phys(sg); 1509fa29b9dSMark McLoughlin desc[i].len = sg->length; 1519fa29b9dSMark McLoughlin desc[i].next = i+1; 15213816c76SRusty Russell i++; 1539fa29b9dSMark McLoughlin } 15413816c76SRusty Russell } 15513816c76SRusty Russell for (; n < (out_sgs + in_sgs); n++) { 15613816c76SRusty Russell for (sg = sgs[n]; sg; sg = next(sg, &total_in)) { 1579fa29b9dSMark McLoughlin desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE; 1589fa29b9dSMark McLoughlin desc[i].addr = sg_phys(sg); 1599fa29b9dSMark McLoughlin desc[i].len = sg->length; 1609fa29b9dSMark McLoughlin desc[i].next = i+1; 16113816c76SRusty Russell i++; 1629fa29b9dSMark McLoughlin } 16313816c76SRusty Russell } 16413816c76SRusty Russell BUG_ON(i != total_sg); 1659fa29b9dSMark McLoughlin 1669fa29b9dSMark McLoughlin /* Last one doesn't continue. */ 1679fa29b9dSMark McLoughlin desc[i-1].flags &= ~VRING_DESC_F_NEXT; 1689fa29b9dSMark McLoughlin desc[i-1].next = 0; 1699fa29b9dSMark McLoughlin 1709fa29b9dSMark McLoughlin /* We're about to use a buffer */ 17106ca287dSRusty Russell vq->vq.num_free--; 1729fa29b9dSMark McLoughlin 1739fa29b9dSMark McLoughlin /* Use a single buffer which doesn't continue */ 1749fa29b9dSMark McLoughlin head = vq->free_head; 1759fa29b9dSMark McLoughlin vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT; 1769fa29b9dSMark McLoughlin vq->vring.desc[head].addr = virt_to_phys(desc); 177bb478d8bSRusty Russell /* kmemleak gives a false positive, as it's hidden by virt_to_phys */ 178bb478d8bSRusty Russell kmemleak_ignore(desc); 1799fa29b9dSMark McLoughlin vq->vring.desc[head].len = i * sizeof(struct vring_desc); 1809fa29b9dSMark McLoughlin 1819fa29b9dSMark McLoughlin /* Update free pointer */ 1829fa29b9dSMark McLoughlin vq->free_head = vq->vring.desc[head].next; 1839fa29b9dSMark McLoughlin 1849fa29b9dSMark McLoughlin return head; 1859fa29b9dSMark McLoughlin } 1869fa29b9dSMark McLoughlin 18713816c76SRusty Russell static inline int virtqueue_add(struct virtqueue *_vq, 18813816c76SRusty Russell struct scatterlist *sgs[], 18913816c76SRusty Russell struct scatterlist *(*next) 19013816c76SRusty Russell (struct scatterlist *, unsigned int *), 19113816c76SRusty Russell unsigned int total_out, 19213816c76SRusty Russell unsigned int total_in, 19313816c76SRusty Russell unsigned int out_sgs, 19413816c76SRusty Russell unsigned int in_sgs, 195bbd603efSMichael S. Tsirkin void *data, 196bbd603efSMichael S. Tsirkin gfp_t gfp) 1970a8a69ddSRusty Russell { 1980a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 19913816c76SRusty Russell struct scatterlist *sg; 20013816c76SRusty Russell unsigned int i, n, avail, uninitialized_var(prev), total_sg; 2011fe9b6feSMichael S. Tsirkin int head; 2020a8a69ddSRusty Russell 2039fa29b9dSMark McLoughlin START_USE(vq); 2049fa29b9dSMark McLoughlin 2050a8a69ddSRusty Russell BUG_ON(data == NULL); 2069fa29b9dSMark McLoughlin 207*70670444SRusty Russell if (unlikely(vq->broken)) { 208*70670444SRusty Russell END_USE(vq); 209*70670444SRusty Russell return -EIO; 210*70670444SRusty Russell } 211*70670444SRusty Russell 212e93300b1SRusty Russell #ifdef DEBUG 213e93300b1SRusty Russell { 214e93300b1SRusty Russell ktime_t now = ktime_get(); 215e93300b1SRusty Russell 216e93300b1SRusty Russell /* No kick or get, with .1 second between? Warn. */ 217e93300b1SRusty Russell if (vq->last_add_time_valid) 218e93300b1SRusty Russell WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time)) 219e93300b1SRusty Russell > 100); 220e93300b1SRusty Russell vq->last_add_time = now; 221e93300b1SRusty Russell vq->last_add_time_valid = true; 222e93300b1SRusty Russell } 223e93300b1SRusty Russell #endif 224e93300b1SRusty Russell 22513816c76SRusty Russell total_sg = total_in + total_out; 22613816c76SRusty Russell 2279fa29b9dSMark McLoughlin /* If the host supports indirect descriptor tables, and we have multiple 2289fa29b9dSMark McLoughlin * buffers, then go indirect. FIXME: tune this threshold */ 22913816c76SRusty Russell if (vq->indirect && total_sg > 1 && vq->vq.num_free) { 23013816c76SRusty Russell head = vring_add_indirect(vq, sgs, next, total_sg, total_out, 23113816c76SRusty Russell total_in, 23213816c76SRusty Russell out_sgs, in_sgs, gfp); 2331fe9b6feSMichael S. Tsirkin if (likely(head >= 0)) 2349fa29b9dSMark McLoughlin goto add_head; 2359fa29b9dSMark McLoughlin } 2369fa29b9dSMark McLoughlin 23713816c76SRusty Russell BUG_ON(total_sg > vq->vring.num); 23813816c76SRusty Russell BUG_ON(total_sg == 0); 2390a8a69ddSRusty Russell 24013816c76SRusty Russell if (vq->vq.num_free < total_sg) { 2410a8a69ddSRusty Russell pr_debug("Can't add buf len %i - avail = %i\n", 24213816c76SRusty Russell total_sg, vq->vq.num_free); 24344653eaeSRusty Russell /* FIXME: for historical reasons, we force a notify here if 24444653eaeSRusty Russell * there are outgoing parts to the buffer. Presumably the 24544653eaeSRusty Russell * host should service the ring ASAP. */ 24613816c76SRusty Russell if (out_sgs) 247426e3e0aSRusty Russell vq->notify(&vq->vq); 2480a8a69ddSRusty Russell END_USE(vq); 2490a8a69ddSRusty Russell return -ENOSPC; 2500a8a69ddSRusty Russell } 2510a8a69ddSRusty Russell 2520a8a69ddSRusty Russell /* We're about to use some buffers from the free list. */ 25313816c76SRusty Russell vq->vq.num_free -= total_sg; 2540a8a69ddSRusty Russell 25513816c76SRusty Russell head = i = vq->free_head; 25613816c76SRusty Russell for (n = 0; n < out_sgs; n++) { 25713816c76SRusty Russell for (sg = sgs[n]; sg; sg = next(sg, &total_out)) { 2580a8a69ddSRusty Russell vq->vring.desc[i].flags = VRING_DESC_F_NEXT; 25915f9c890SRusty Russell vq->vring.desc[i].addr = sg_phys(sg); 2600a8a69ddSRusty Russell vq->vring.desc[i].len = sg->length; 2610a8a69ddSRusty Russell prev = i; 26213816c76SRusty Russell i = vq->vring.desc[i].next; 2630a8a69ddSRusty Russell } 26413816c76SRusty Russell } 26513816c76SRusty Russell for (; n < (out_sgs + in_sgs); n++) { 26613816c76SRusty Russell for (sg = sgs[n]; sg; sg = next(sg, &total_in)) { 2670a8a69ddSRusty Russell vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE; 26815f9c890SRusty Russell vq->vring.desc[i].addr = sg_phys(sg); 2690a8a69ddSRusty Russell vq->vring.desc[i].len = sg->length; 2700a8a69ddSRusty Russell prev = i; 27113816c76SRusty Russell i = vq->vring.desc[i].next; 27213816c76SRusty Russell } 2730a8a69ddSRusty Russell } 2740a8a69ddSRusty Russell /* Last one doesn't continue. */ 2750a8a69ddSRusty Russell vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT; 2760a8a69ddSRusty Russell 2770a8a69ddSRusty Russell /* Update free pointer */ 2780a8a69ddSRusty Russell vq->free_head = i; 2790a8a69ddSRusty Russell 2809fa29b9dSMark McLoughlin add_head: 2810a8a69ddSRusty Russell /* Set token. */ 2820a8a69ddSRusty Russell vq->data[head] = data; 2830a8a69ddSRusty Russell 2840a8a69ddSRusty Russell /* Put entry in available array (but don't update avail->idx until they 2853b720b8cSRusty Russell * do sync). */ 286ee7cd898SRusty Russell avail = (vq->vring.avail->idx & (vq->vring.num-1)); 2870a8a69ddSRusty Russell vq->vring.avail->ring[avail] = head; 2880a8a69ddSRusty Russell 289ee7cd898SRusty Russell /* Descriptors and available array need to be set before we expose the 290ee7cd898SRusty Russell * new available array entries. */ 291a9a0fef7SRusty Russell virtio_wmb(vq->weak_barriers); 292ee7cd898SRusty Russell vq->vring.avail->idx++; 293ee7cd898SRusty Russell vq->num_added++; 294ee7cd898SRusty Russell 295ee7cd898SRusty Russell /* This is very unlikely, but theoretically possible. Kick 296ee7cd898SRusty Russell * just in case. */ 297ee7cd898SRusty Russell if (unlikely(vq->num_added == (1 << 16) - 1)) 298ee7cd898SRusty Russell virtqueue_kick(_vq); 299ee7cd898SRusty Russell 3000a8a69ddSRusty Russell pr_debug("Added buffer head %i to %p\n", head, vq); 3010a8a69ddSRusty Russell END_USE(vq); 3023c1b27d5SRusty Russell 30398e8c6bcSRusty Russell return 0; 3040a8a69ddSRusty Russell } 30513816c76SRusty Russell 30613816c76SRusty Russell /** 30713816c76SRusty Russell * virtqueue_add_sgs - expose buffers to other end 30813816c76SRusty Russell * @vq: the struct virtqueue we're talking about. 30913816c76SRusty Russell * @sgs: array of terminated scatterlists. 31013816c76SRusty Russell * @out_num: the number of scatterlists readable by other side 31113816c76SRusty Russell * @in_num: the number of scatterlists which are writable (after readable ones) 31213816c76SRusty Russell * @data: the token identifying the buffer. 31313816c76SRusty Russell * @gfp: how to do memory allocations (if necessary). 31413816c76SRusty Russell * 31513816c76SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 31613816c76SRusty Russell * at the same time (except where noted). 31713816c76SRusty Russell * 318*70670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 31913816c76SRusty Russell */ 32013816c76SRusty Russell int virtqueue_add_sgs(struct virtqueue *_vq, 32113816c76SRusty Russell struct scatterlist *sgs[], 32213816c76SRusty Russell unsigned int out_sgs, 32313816c76SRusty Russell unsigned int in_sgs, 32413816c76SRusty Russell void *data, 32513816c76SRusty Russell gfp_t gfp) 32613816c76SRusty Russell { 32713816c76SRusty Russell unsigned int i, total_out, total_in; 32813816c76SRusty Russell 32913816c76SRusty Russell /* Count them first. */ 33013816c76SRusty Russell for (i = total_out = total_in = 0; i < out_sgs; i++) { 33113816c76SRusty Russell struct scatterlist *sg; 33213816c76SRusty Russell for (sg = sgs[i]; sg; sg = sg_next(sg)) 33313816c76SRusty Russell total_out++; 33413816c76SRusty Russell } 33513816c76SRusty Russell for (; i < out_sgs + in_sgs; i++) { 33613816c76SRusty Russell struct scatterlist *sg; 33713816c76SRusty Russell for (sg = sgs[i]; sg; sg = sg_next(sg)) 33813816c76SRusty Russell total_in++; 33913816c76SRusty Russell } 34013816c76SRusty Russell return virtqueue_add(_vq, sgs, sg_next_chained, 34113816c76SRusty Russell total_out, total_in, out_sgs, in_sgs, data, gfp); 34213816c76SRusty Russell } 34313816c76SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_sgs); 34413816c76SRusty Russell 34513816c76SRusty Russell /** 346282edb36SRusty Russell * virtqueue_add_outbuf - expose output buffers to other end 347282edb36SRusty Russell * @vq: the struct virtqueue we're talking about. 348282edb36SRusty Russell * @sgs: array of scatterlists (need not be terminated!) 349282edb36SRusty Russell * @num: the number of scatterlists readable by other side 350282edb36SRusty Russell * @data: the token identifying the buffer. 351282edb36SRusty Russell * @gfp: how to do memory allocations (if necessary). 352282edb36SRusty Russell * 353282edb36SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 354282edb36SRusty Russell * at the same time (except where noted). 355282edb36SRusty Russell * 356*70670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 357282edb36SRusty Russell */ 358282edb36SRusty Russell int virtqueue_add_outbuf(struct virtqueue *vq, 359282edb36SRusty Russell struct scatterlist sg[], unsigned int num, 360282edb36SRusty Russell void *data, 361282edb36SRusty Russell gfp_t gfp) 362282edb36SRusty Russell { 363282edb36SRusty Russell return virtqueue_add(vq, &sg, sg_next_arr, num, 0, 1, 0, data, gfp); 364282edb36SRusty Russell } 365282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); 366282edb36SRusty Russell 367282edb36SRusty Russell /** 368282edb36SRusty Russell * virtqueue_add_inbuf - expose input buffers to other end 369282edb36SRusty Russell * @vq: the struct virtqueue we're talking about. 370282edb36SRusty Russell * @sgs: array of scatterlists (need not be terminated!) 371282edb36SRusty Russell * @num: the number of scatterlists writable by other side 372282edb36SRusty Russell * @data: the token identifying the buffer. 373282edb36SRusty Russell * @gfp: how to do memory allocations (if necessary). 374282edb36SRusty Russell * 375282edb36SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 376282edb36SRusty Russell * at the same time (except where noted). 377282edb36SRusty Russell * 378*70670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 379282edb36SRusty Russell */ 380282edb36SRusty Russell int virtqueue_add_inbuf(struct virtqueue *vq, 381282edb36SRusty Russell struct scatterlist sg[], unsigned int num, 382282edb36SRusty Russell void *data, 383282edb36SRusty Russell gfp_t gfp) 384282edb36SRusty Russell { 385282edb36SRusty Russell return virtqueue_add(vq, &sg, sg_next_arr, 0, num, 0, 1, data, gfp); 386282edb36SRusty Russell } 387282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); 388282edb36SRusty Russell 389282edb36SRusty Russell /** 39041f0377fSRusty Russell * virtqueue_kick_prepare - first half of split virtqueue_kick call. 3915dfc1762SRusty Russell * @vq: the struct virtqueue 3925dfc1762SRusty Russell * 39341f0377fSRusty Russell * Instead of virtqueue_kick(), you can do: 39441f0377fSRusty Russell * if (virtqueue_kick_prepare(vq)) 39541f0377fSRusty Russell * virtqueue_notify(vq); 3965dfc1762SRusty Russell * 39741f0377fSRusty Russell * This is sometimes useful because the virtqueue_kick_prepare() needs 39841f0377fSRusty Russell * to be serialized, but the actual virtqueue_notify() call does not. 3995dfc1762SRusty Russell */ 40041f0377fSRusty Russell bool virtqueue_kick_prepare(struct virtqueue *_vq) 4010a8a69ddSRusty Russell { 4020a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 403a5c262c5SMichael S. Tsirkin u16 new, old; 40441f0377fSRusty Russell bool needs_kick; 40541f0377fSRusty Russell 4060a8a69ddSRusty Russell START_USE(vq); 407a72caae2SJason Wang /* We need to expose available array entries before checking avail 408a72caae2SJason Wang * event. */ 409a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 4100a8a69ddSRusty Russell 411ee7cd898SRusty Russell old = vq->vring.avail->idx - vq->num_added; 412ee7cd898SRusty Russell new = vq->vring.avail->idx; 4130a8a69ddSRusty Russell vq->num_added = 0; 4140a8a69ddSRusty Russell 415e93300b1SRusty Russell #ifdef DEBUG 416e93300b1SRusty Russell if (vq->last_add_time_valid) { 417e93300b1SRusty Russell WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), 418e93300b1SRusty Russell vq->last_add_time)) > 100); 419e93300b1SRusty Russell } 420e93300b1SRusty Russell vq->last_add_time_valid = false; 421e93300b1SRusty Russell #endif 422e93300b1SRusty Russell 42341f0377fSRusty Russell if (vq->event) { 42441f0377fSRusty Russell needs_kick = vring_need_event(vring_avail_event(&vq->vring), 42541f0377fSRusty Russell new, old); 42641f0377fSRusty Russell } else { 42741f0377fSRusty Russell needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY); 42841f0377fSRusty Russell } 4290a8a69ddSRusty Russell END_USE(vq); 43041f0377fSRusty Russell return needs_kick; 43141f0377fSRusty Russell } 43241f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); 43341f0377fSRusty Russell 43441f0377fSRusty Russell /** 43541f0377fSRusty Russell * virtqueue_notify - second half of split virtqueue_kick call. 43641f0377fSRusty Russell * @vq: the struct virtqueue 43741f0377fSRusty Russell * 43841f0377fSRusty Russell * This does not need to be serialized. 4395b1bf7cbSHeinz Graalfs * 4405b1bf7cbSHeinz Graalfs * Returns false if host notify failed or queue is broken, otherwise true. 44141f0377fSRusty Russell */ 4425b1bf7cbSHeinz Graalfs bool virtqueue_notify(struct virtqueue *_vq) 44341f0377fSRusty Russell { 44441f0377fSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 44541f0377fSRusty Russell 4465b1bf7cbSHeinz Graalfs if (unlikely(vq->broken)) 4475b1bf7cbSHeinz Graalfs return false; 4485b1bf7cbSHeinz Graalfs 44941f0377fSRusty Russell /* Prod other side to tell it about changes. */ 4502342d6a6SHeinz Graalfs if (!vq->notify(_vq)) { 4515b1bf7cbSHeinz Graalfs vq->broken = true; 4525b1bf7cbSHeinz Graalfs return false; 4535b1bf7cbSHeinz Graalfs } 4545b1bf7cbSHeinz Graalfs return true; 45541f0377fSRusty Russell } 45641f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify); 45741f0377fSRusty Russell 45841f0377fSRusty Russell /** 45941f0377fSRusty Russell * virtqueue_kick - update after add_buf 46041f0377fSRusty Russell * @vq: the struct virtqueue 46141f0377fSRusty Russell * 462b3087e48SRusty Russell * After one or more virtqueue_add_* calls, invoke this to kick 46341f0377fSRusty Russell * the other side. 46441f0377fSRusty Russell * 46541f0377fSRusty Russell * Caller must ensure we don't call this with other virtqueue 46641f0377fSRusty Russell * operations at the same time (except where noted). 4675b1bf7cbSHeinz Graalfs * 4685b1bf7cbSHeinz Graalfs * Returns false if kick failed, otherwise true. 46941f0377fSRusty Russell */ 4705b1bf7cbSHeinz Graalfs bool virtqueue_kick(struct virtqueue *vq) 47141f0377fSRusty Russell { 47241f0377fSRusty Russell if (virtqueue_kick_prepare(vq)) 4735b1bf7cbSHeinz Graalfs return virtqueue_notify(vq); 4745b1bf7cbSHeinz Graalfs return true; 4750a8a69ddSRusty Russell } 4767c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick); 4770a8a69ddSRusty Russell 4780a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head) 4790a8a69ddSRusty Russell { 4800a8a69ddSRusty Russell unsigned int i; 4810a8a69ddSRusty Russell 4820a8a69ddSRusty Russell /* Clear data ptr. */ 4830a8a69ddSRusty Russell vq->data[head] = NULL; 4840a8a69ddSRusty Russell 4850a8a69ddSRusty Russell /* Put back on free list: find end */ 4860a8a69ddSRusty Russell i = head; 4879fa29b9dSMark McLoughlin 4889fa29b9dSMark McLoughlin /* Free the indirect table */ 4899fa29b9dSMark McLoughlin if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT) 4909fa29b9dSMark McLoughlin kfree(phys_to_virt(vq->vring.desc[i].addr)); 4919fa29b9dSMark McLoughlin 4920a8a69ddSRusty Russell while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) { 4930a8a69ddSRusty Russell i = vq->vring.desc[i].next; 49406ca287dSRusty Russell vq->vq.num_free++; 4950a8a69ddSRusty Russell } 4960a8a69ddSRusty Russell 4970a8a69ddSRusty Russell vq->vring.desc[i].next = vq->free_head; 4980a8a69ddSRusty Russell vq->free_head = head; 4990a8a69ddSRusty Russell /* Plus final descriptor */ 50006ca287dSRusty Russell vq->vq.num_free++; 5010a8a69ddSRusty Russell } 5020a8a69ddSRusty Russell 5030a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq) 5040a8a69ddSRusty Russell { 5050a8a69ddSRusty Russell return vq->last_used_idx != vq->vring.used->idx; 5060a8a69ddSRusty Russell } 5070a8a69ddSRusty Russell 5085dfc1762SRusty Russell /** 5095dfc1762SRusty Russell * virtqueue_get_buf - get the next used buffer 5105dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 5115dfc1762SRusty Russell * @len: the length written into the buffer 5125dfc1762SRusty Russell * 5135dfc1762SRusty Russell * If the driver wrote data into the buffer, @len will be set to the 5145dfc1762SRusty Russell * amount written. This means you don't need to clear the buffer 5155dfc1762SRusty Russell * beforehand to ensure there's no data leakage in the case of short 5165dfc1762SRusty Russell * writes. 5175dfc1762SRusty Russell * 5185dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 5195dfc1762SRusty Russell * operations at the same time (except where noted). 5205dfc1762SRusty Russell * 5215dfc1762SRusty Russell * Returns NULL if there are no used buffers, or the "data" token 522b3087e48SRusty Russell * handed to virtqueue_add_*(). 5235dfc1762SRusty Russell */ 5247c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) 5250a8a69ddSRusty Russell { 5260a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 5270a8a69ddSRusty Russell void *ret; 5280a8a69ddSRusty Russell unsigned int i; 5293b720b8cSRusty Russell u16 last_used; 5300a8a69ddSRusty Russell 5310a8a69ddSRusty Russell START_USE(vq); 5320a8a69ddSRusty Russell 5335ef82752SRusty Russell if (unlikely(vq->broken)) { 5345ef82752SRusty Russell END_USE(vq); 5355ef82752SRusty Russell return NULL; 5365ef82752SRusty Russell } 5375ef82752SRusty Russell 5380a8a69ddSRusty Russell if (!more_used(vq)) { 5390a8a69ddSRusty Russell pr_debug("No more buffers in queue\n"); 5400a8a69ddSRusty Russell END_USE(vq); 5410a8a69ddSRusty Russell return NULL; 5420a8a69ddSRusty Russell } 5430a8a69ddSRusty Russell 5442d61ba95SMichael S. Tsirkin /* Only get used array entries after they have been exposed by host. */ 545a9a0fef7SRusty Russell virtio_rmb(vq->weak_barriers); 5462d61ba95SMichael S. Tsirkin 5473b720b8cSRusty Russell last_used = (vq->last_used_idx & (vq->vring.num - 1)); 5483b720b8cSRusty Russell i = vq->vring.used->ring[last_used].id; 5493b720b8cSRusty Russell *len = vq->vring.used->ring[last_used].len; 5500a8a69ddSRusty Russell 5510a8a69ddSRusty Russell if (unlikely(i >= vq->vring.num)) { 5520a8a69ddSRusty Russell BAD_RING(vq, "id %u out of range\n", i); 5530a8a69ddSRusty Russell return NULL; 5540a8a69ddSRusty Russell } 5550a8a69ddSRusty Russell if (unlikely(!vq->data[i])) { 5560a8a69ddSRusty Russell BAD_RING(vq, "id %u is not a head!\n", i); 5570a8a69ddSRusty Russell return NULL; 5580a8a69ddSRusty Russell } 5590a8a69ddSRusty Russell 5600a8a69ddSRusty Russell /* detach_buf clears data, so grab it now. */ 5610a8a69ddSRusty Russell ret = vq->data[i]; 5620a8a69ddSRusty Russell detach_buf(vq, i); 5630a8a69ddSRusty Russell vq->last_used_idx++; 564a5c262c5SMichael S. Tsirkin /* If we expect an interrupt for the next entry, tell host 565a5c262c5SMichael S. Tsirkin * by writing event index and flush out the write before 566a5c262c5SMichael S. Tsirkin * the read in the next get_buf call. */ 567a5c262c5SMichael S. Tsirkin if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) { 568a5c262c5SMichael S. Tsirkin vring_used_event(&vq->vring) = vq->last_used_idx; 569a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 570a5c262c5SMichael S. Tsirkin } 571a5c262c5SMichael S. Tsirkin 572e93300b1SRusty Russell #ifdef DEBUG 573e93300b1SRusty Russell vq->last_add_time_valid = false; 574e93300b1SRusty Russell #endif 575e93300b1SRusty Russell 5760a8a69ddSRusty Russell END_USE(vq); 5770a8a69ddSRusty Russell return ret; 5780a8a69ddSRusty Russell } 5797c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf); 5800a8a69ddSRusty Russell 5815dfc1762SRusty Russell /** 5825dfc1762SRusty Russell * virtqueue_disable_cb - disable callbacks 5835dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 5845dfc1762SRusty Russell * 5855dfc1762SRusty Russell * Note that this is not necessarily synchronous, hence unreliable and only 5865dfc1762SRusty Russell * useful as an optimization. 5875dfc1762SRusty Russell * 5885dfc1762SRusty Russell * Unlike other operations, this need not be serialized. 5895dfc1762SRusty Russell */ 5907c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq) 59118445c4dSRusty Russell { 59218445c4dSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 59318445c4dSRusty Russell 59418445c4dSRusty Russell vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; 59518445c4dSRusty Russell } 5967c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb); 59718445c4dSRusty Russell 5985dfc1762SRusty Russell /** 599cc229884SMichael S. Tsirkin * virtqueue_enable_cb_prepare - restart callbacks after disable_cb 600cc229884SMichael S. Tsirkin * @vq: the struct virtqueue we're talking about. 601cc229884SMichael S. Tsirkin * 602cc229884SMichael S. Tsirkin * This re-enables callbacks; it returns current queue state 603cc229884SMichael S. Tsirkin * in an opaque unsigned value. This value should be later tested by 604cc229884SMichael S. Tsirkin * virtqueue_poll, to detect a possible race between the driver checking for 605cc229884SMichael S. Tsirkin * more work, and enabling callbacks. 606cc229884SMichael S. Tsirkin * 607cc229884SMichael S. Tsirkin * Caller must ensure we don't call this with other virtqueue 608cc229884SMichael S. Tsirkin * operations at the same time (except where noted). 609cc229884SMichael S. Tsirkin */ 610cc229884SMichael S. Tsirkin unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) 611cc229884SMichael S. Tsirkin { 612cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 613cc229884SMichael S. Tsirkin u16 last_used_idx; 614cc229884SMichael S. Tsirkin 615cc229884SMichael S. Tsirkin START_USE(vq); 616cc229884SMichael S. Tsirkin 617cc229884SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 618cc229884SMichael S. Tsirkin * more to do. */ 619cc229884SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to 620cc229884SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 621cc229884SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 622cc229884SMichael S. Tsirkin vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; 623cc229884SMichael S. Tsirkin vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx; 624cc229884SMichael S. Tsirkin END_USE(vq); 625cc229884SMichael S. Tsirkin return last_used_idx; 626cc229884SMichael S. Tsirkin } 627cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); 628cc229884SMichael S. Tsirkin 629cc229884SMichael S. Tsirkin /** 630cc229884SMichael S. Tsirkin * virtqueue_poll - query pending used buffers 631cc229884SMichael S. Tsirkin * @vq: the struct virtqueue we're talking about. 632cc229884SMichael S. Tsirkin * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). 633cc229884SMichael S. Tsirkin * 634cc229884SMichael S. Tsirkin * Returns "true" if there are pending used buffers in the queue. 635cc229884SMichael S. Tsirkin * 636cc229884SMichael S. Tsirkin * This does not need to be serialized. 637cc229884SMichael S. Tsirkin */ 638cc229884SMichael S. Tsirkin bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) 639cc229884SMichael S. Tsirkin { 640cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 641cc229884SMichael S. Tsirkin 642cc229884SMichael S. Tsirkin virtio_mb(vq->weak_barriers); 643cc229884SMichael S. Tsirkin return (u16)last_used_idx != vq->vring.used->idx; 644cc229884SMichael S. Tsirkin } 645cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_poll); 646cc229884SMichael S. Tsirkin 647cc229884SMichael S. Tsirkin /** 6485dfc1762SRusty Russell * virtqueue_enable_cb - restart callbacks after disable_cb. 6495dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 6505dfc1762SRusty Russell * 6515dfc1762SRusty Russell * This re-enables callbacks; it returns "false" if there are pending 6525dfc1762SRusty Russell * buffers in the queue, to detect a possible race between the driver 6535dfc1762SRusty Russell * checking for more work, and enabling callbacks. 6545dfc1762SRusty Russell * 6555dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 6565dfc1762SRusty Russell * operations at the same time (except where noted). 6575dfc1762SRusty Russell */ 6587c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq) 6590a8a69ddSRusty Russell { 660cc229884SMichael S. Tsirkin unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); 661cc229884SMichael S. Tsirkin return !virtqueue_poll(_vq, last_used_idx); 6620a8a69ddSRusty Russell } 6637c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb); 6640a8a69ddSRusty Russell 6655dfc1762SRusty Russell /** 6665dfc1762SRusty Russell * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. 6675dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 6685dfc1762SRusty Russell * 6695dfc1762SRusty Russell * This re-enables callbacks but hints to the other side to delay 6705dfc1762SRusty Russell * interrupts until most of the available buffers have been processed; 6715dfc1762SRusty Russell * it returns "false" if there are many pending buffers in the queue, 6725dfc1762SRusty Russell * to detect a possible race between the driver checking for more work, 6735dfc1762SRusty Russell * and enabling callbacks. 6745dfc1762SRusty Russell * 6755dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 6765dfc1762SRusty Russell * operations at the same time (except where noted). 6775dfc1762SRusty Russell */ 6787ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) 6797ab358c2SMichael S. Tsirkin { 6807ab358c2SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 6817ab358c2SMichael S. Tsirkin u16 bufs; 6827ab358c2SMichael S. Tsirkin 6837ab358c2SMichael S. Tsirkin START_USE(vq); 6847ab358c2SMichael S. Tsirkin 6857ab358c2SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 6867ab358c2SMichael S. Tsirkin * more to do. */ 6877ab358c2SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to 6887ab358c2SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 6897ab358c2SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 6907ab358c2SMichael S. Tsirkin vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT; 6917ab358c2SMichael S. Tsirkin /* TODO: tune this threshold */ 6927ab358c2SMichael S. Tsirkin bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4; 6937ab358c2SMichael S. Tsirkin vring_used_event(&vq->vring) = vq->last_used_idx + bufs; 694a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 6957ab358c2SMichael S. Tsirkin if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) { 6967ab358c2SMichael S. Tsirkin END_USE(vq); 6977ab358c2SMichael S. Tsirkin return false; 6987ab358c2SMichael S. Tsirkin } 6997ab358c2SMichael S. Tsirkin 7007ab358c2SMichael S. Tsirkin END_USE(vq); 7017ab358c2SMichael S. Tsirkin return true; 7027ab358c2SMichael S. Tsirkin } 7037ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); 7047ab358c2SMichael S. Tsirkin 7055dfc1762SRusty Russell /** 7065dfc1762SRusty Russell * virtqueue_detach_unused_buf - detach first unused buffer 7075dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 7085dfc1762SRusty Russell * 709b3087e48SRusty Russell * Returns NULL or the "data" token handed to virtqueue_add_*(). 7105dfc1762SRusty Russell * This is not valid on an active queue; it is useful only for device 7115dfc1762SRusty Russell * shutdown. 7125dfc1762SRusty Russell */ 7137c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq) 714c021eac4SShirley Ma { 715c021eac4SShirley Ma struct vring_virtqueue *vq = to_vvq(_vq); 716c021eac4SShirley Ma unsigned int i; 717c021eac4SShirley Ma void *buf; 718c021eac4SShirley Ma 719c021eac4SShirley Ma START_USE(vq); 720c021eac4SShirley Ma 721c021eac4SShirley Ma for (i = 0; i < vq->vring.num; i++) { 722c021eac4SShirley Ma if (!vq->data[i]) 723c021eac4SShirley Ma continue; 724c021eac4SShirley Ma /* detach_buf clears data, so grab it now. */ 725c021eac4SShirley Ma buf = vq->data[i]; 726c021eac4SShirley Ma detach_buf(vq, i); 727b3258ff1SAmit Shah vq->vring.avail->idx--; 728c021eac4SShirley Ma END_USE(vq); 729c021eac4SShirley Ma return buf; 730c021eac4SShirley Ma } 731c021eac4SShirley Ma /* That should have freed everything. */ 73206ca287dSRusty Russell BUG_ON(vq->vq.num_free != vq->vring.num); 733c021eac4SShirley Ma 734c021eac4SShirley Ma END_USE(vq); 735c021eac4SShirley Ma return NULL; 736c021eac4SShirley Ma } 7377c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); 738c021eac4SShirley Ma 7390a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq) 7400a8a69ddSRusty Russell { 7410a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 7420a8a69ddSRusty Russell 7430a8a69ddSRusty Russell if (!more_used(vq)) { 7440a8a69ddSRusty Russell pr_debug("virtqueue interrupt with no work for %p\n", vq); 7450a8a69ddSRusty Russell return IRQ_NONE; 7460a8a69ddSRusty Russell } 7470a8a69ddSRusty Russell 7480a8a69ddSRusty Russell if (unlikely(vq->broken)) 7490a8a69ddSRusty Russell return IRQ_HANDLED; 7500a8a69ddSRusty Russell 7510a8a69ddSRusty Russell pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); 75218445c4dSRusty Russell if (vq->vq.callback) 75318445c4dSRusty Russell vq->vq.callback(&vq->vq); 7540a8a69ddSRusty Russell 7550a8a69ddSRusty Russell return IRQ_HANDLED; 7560a8a69ddSRusty Russell } 757c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt); 7580a8a69ddSRusty Russell 75917bb6d40SJason Wang struct virtqueue *vring_new_virtqueue(unsigned int index, 76017bb6d40SJason Wang unsigned int num, 76187c7d57cSRusty Russell unsigned int vring_align, 7620a8a69ddSRusty Russell struct virtio_device *vdev, 7637b21e34fSRusty Russell bool weak_barriers, 7640a8a69ddSRusty Russell void *pages, 76546f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *), 7669499f5e7SRusty Russell void (*callback)(struct virtqueue *), 7679499f5e7SRusty Russell const char *name) 7680a8a69ddSRusty Russell { 7690a8a69ddSRusty Russell struct vring_virtqueue *vq; 7700a8a69ddSRusty Russell unsigned int i; 7710a8a69ddSRusty Russell 77242b36cc0SRusty Russell /* We assume num is a power of 2. */ 77342b36cc0SRusty Russell if (num & (num - 1)) { 77442b36cc0SRusty Russell dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); 77542b36cc0SRusty Russell return NULL; 77642b36cc0SRusty Russell } 77742b36cc0SRusty Russell 7780a8a69ddSRusty Russell vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL); 7790a8a69ddSRusty Russell if (!vq) 7800a8a69ddSRusty Russell return NULL; 7810a8a69ddSRusty Russell 78287c7d57cSRusty Russell vring_init(&vq->vring, num, pages, vring_align); 7830a8a69ddSRusty Russell vq->vq.callback = callback; 7840a8a69ddSRusty Russell vq->vq.vdev = vdev; 7859499f5e7SRusty Russell vq->vq.name = name; 78606ca287dSRusty Russell vq->vq.num_free = num; 78706ca287dSRusty Russell vq->vq.index = index; 7880a8a69ddSRusty Russell vq->notify = notify; 7897b21e34fSRusty Russell vq->weak_barriers = weak_barriers; 7900a8a69ddSRusty Russell vq->broken = false; 7910a8a69ddSRusty Russell vq->last_used_idx = 0; 7920a8a69ddSRusty Russell vq->num_added = 0; 7939499f5e7SRusty Russell list_add_tail(&vq->vq.list, &vdev->vqs); 7940a8a69ddSRusty Russell #ifdef DEBUG 7950a8a69ddSRusty Russell vq->in_use = false; 796e93300b1SRusty Russell vq->last_add_time_valid = false; 7970a8a69ddSRusty Russell #endif 7980a8a69ddSRusty Russell 7999fa29b9dSMark McLoughlin vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); 800a5c262c5SMichael S. Tsirkin vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 8019fa29b9dSMark McLoughlin 8020a8a69ddSRusty Russell /* No callback? Tell other side not to bother us. */ 8030a8a69ddSRusty Russell if (!callback) 8040a8a69ddSRusty Russell vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; 8050a8a69ddSRusty Russell 8060a8a69ddSRusty Russell /* Put everything in free lists. */ 8070a8a69ddSRusty Russell vq->free_head = 0; 8083b870624SAmit Shah for (i = 0; i < num-1; i++) { 8090a8a69ddSRusty Russell vq->vring.desc[i].next = i+1; 8103b870624SAmit Shah vq->data[i] = NULL; 8113b870624SAmit Shah } 8123b870624SAmit Shah vq->data[i] = NULL; 8130a8a69ddSRusty Russell 8140a8a69ddSRusty Russell return &vq->vq; 8150a8a69ddSRusty Russell } 816c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue); 8170a8a69ddSRusty Russell 8180a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq) 8190a8a69ddSRusty Russell { 8209499f5e7SRusty Russell list_del(&vq->list); 8210a8a69ddSRusty Russell kfree(to_vvq(vq)); 8220a8a69ddSRusty Russell } 823c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue); 8240a8a69ddSRusty Russell 825e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */ 826e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev) 827e34f8725SRusty Russell { 828e34f8725SRusty Russell unsigned int i; 829e34f8725SRusty Russell 830e34f8725SRusty Russell for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { 831e34f8725SRusty Russell switch (i) { 8329fa29b9dSMark McLoughlin case VIRTIO_RING_F_INDIRECT_DESC: 8339fa29b9dSMark McLoughlin break; 834a5c262c5SMichael S. Tsirkin case VIRTIO_RING_F_EVENT_IDX: 835a5c262c5SMichael S. Tsirkin break; 836e34f8725SRusty Russell default: 837e34f8725SRusty Russell /* We don't understand this bit. */ 838e34f8725SRusty Russell clear_bit(i, vdev->features); 839e34f8725SRusty Russell } 840e34f8725SRusty Russell } 841e34f8725SRusty Russell } 842e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features); 843e34f8725SRusty Russell 8445dfc1762SRusty Russell /** 8455dfc1762SRusty Russell * virtqueue_get_vring_size - return the size of the virtqueue's vring 8465dfc1762SRusty Russell * @vq: the struct virtqueue containing the vring of interest. 8475dfc1762SRusty Russell * 8485dfc1762SRusty Russell * Returns the size of the vring. This is mainly used for boasting to 8495dfc1762SRusty Russell * userspace. Unlike other operations, this need not be serialized. 8505dfc1762SRusty Russell */ 8518f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) 8528f9f4668SRick Jones { 8538f9f4668SRick Jones 8548f9f4668SRick Jones struct vring_virtqueue *vq = to_vvq(_vq); 8558f9f4668SRick Jones 8568f9f4668SRick Jones return vq->vring.num; 8578f9f4668SRick Jones } 8588f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); 8598f9f4668SRick Jones 860b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq) 861b3b32c94SHeinz Graalfs { 862b3b32c94SHeinz Graalfs struct vring_virtqueue *vq = to_vvq(_vq); 863b3b32c94SHeinz Graalfs 864b3b32c94SHeinz Graalfs return vq->broken; 865b3b32c94SHeinz Graalfs } 866b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken); 867b3b32c94SHeinz Graalfs 868c6fd4701SRusty Russell MODULE_LICENSE("GPL"); 869