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 10200e6f3d9SMichael S. Tsirkin static struct vring_desc *alloc_indirect(struct virtqueue *_vq, 10300e6f3d9SMichael S. Tsirkin unsigned int total_sg, gfp_t gfp) 1049fa29b9dSMark McLoughlin { 1059fa29b9dSMark McLoughlin struct vring_desc *desc; 106b25bd251SRusty Russell unsigned int i; 1079fa29b9dSMark McLoughlin 108b92b1b89SWill Deacon /* 109b92b1b89SWill Deacon * We require lowmem mappings for the descriptors because 110b92b1b89SWill Deacon * otherwise virt_to_phys will give us bogus addresses in the 111b92b1b89SWill Deacon * virtqueue. 112b92b1b89SWill Deacon */ 113b92b1b89SWill Deacon gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH); 114b92b1b89SWill Deacon 11513816c76SRusty Russell desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp); 1169fa29b9dSMark McLoughlin if (!desc) 117b25bd251SRusty Russell return NULL; 1189fa29b9dSMark McLoughlin 119b25bd251SRusty Russell for (i = 0; i < total_sg; i++) 12000e6f3d9SMichael S. Tsirkin desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1); 121b25bd251SRusty Russell return desc; 1229fa29b9dSMark McLoughlin } 1239fa29b9dSMark McLoughlin 12413816c76SRusty Russell static inline int virtqueue_add(struct virtqueue *_vq, 12513816c76SRusty Russell struct scatterlist *sgs[], 126eeebf9b1SRusty Russell unsigned int total_sg, 12713816c76SRusty Russell unsigned int out_sgs, 12813816c76SRusty Russell unsigned int in_sgs, 129bbd603efSMichael S. Tsirkin void *data, 130bbd603efSMichael S. Tsirkin gfp_t gfp) 1310a8a69ddSRusty Russell { 1320a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 13313816c76SRusty Russell struct scatterlist *sg; 134b25bd251SRusty Russell struct vring_desc *desc; 135b25bd251SRusty Russell unsigned int i, n, avail, descs_used, uninitialized_var(prev); 1361fe9b6feSMichael S. Tsirkin int head; 137b25bd251SRusty Russell bool indirect; 1380a8a69ddSRusty Russell 1399fa29b9dSMark McLoughlin START_USE(vq); 1409fa29b9dSMark McLoughlin 1410a8a69ddSRusty Russell BUG_ON(data == NULL); 1429fa29b9dSMark McLoughlin 14370670444SRusty Russell if (unlikely(vq->broken)) { 14470670444SRusty Russell END_USE(vq); 14570670444SRusty Russell return -EIO; 14670670444SRusty Russell } 14770670444SRusty Russell 148e93300b1SRusty Russell #ifdef DEBUG 149e93300b1SRusty Russell { 150e93300b1SRusty Russell ktime_t now = ktime_get(); 151e93300b1SRusty Russell 152e93300b1SRusty Russell /* No kick or get, with .1 second between? Warn. */ 153e93300b1SRusty Russell if (vq->last_add_time_valid) 154e93300b1SRusty Russell WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time)) 155e93300b1SRusty Russell > 100); 156e93300b1SRusty Russell vq->last_add_time = now; 157e93300b1SRusty Russell vq->last_add_time_valid = true; 158e93300b1SRusty Russell } 159e93300b1SRusty Russell #endif 160e93300b1SRusty Russell 16113816c76SRusty Russell BUG_ON(total_sg > vq->vring.num); 16213816c76SRusty Russell BUG_ON(total_sg == 0); 1630a8a69ddSRusty Russell 164b25bd251SRusty Russell head = vq->free_head; 165b25bd251SRusty Russell 166b25bd251SRusty Russell /* If the host supports indirect descriptor tables, and we have multiple 167b25bd251SRusty Russell * buffers, then go indirect. FIXME: tune this threshold */ 168b25bd251SRusty Russell if (vq->indirect && total_sg > 1 && vq->vq.num_free) 16900e6f3d9SMichael S. Tsirkin desc = alloc_indirect(_vq, total_sg, gfp); 170b25bd251SRusty Russell else 171b25bd251SRusty Russell desc = NULL; 172b25bd251SRusty Russell 173b25bd251SRusty Russell if (desc) { 174b25bd251SRusty Russell /* Use a single buffer which doesn't continue */ 17500e6f3d9SMichael S. Tsirkin vq->vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_INDIRECT); 17600e6f3d9SMichael S. Tsirkin vq->vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, virt_to_phys(desc)); 177b25bd251SRusty Russell /* avoid kmemleak false positive (hidden by virt_to_phys) */ 178b25bd251SRusty Russell kmemleak_ignore(desc); 17900e6f3d9SMichael S. Tsirkin vq->vring.desc[head].len = cpu_to_virtio32(_vq->vdev, total_sg * sizeof(struct vring_desc)); 180b25bd251SRusty Russell 181b25bd251SRusty Russell /* Set up rest to use this indirect table. */ 182b25bd251SRusty Russell i = 0; 183b25bd251SRusty Russell descs_used = 1; 184b25bd251SRusty Russell indirect = true; 185b25bd251SRusty Russell } else { 186b25bd251SRusty Russell desc = vq->vring.desc; 187b25bd251SRusty Russell i = head; 188b25bd251SRusty Russell descs_used = total_sg; 189b25bd251SRusty Russell indirect = false; 190b25bd251SRusty Russell } 191b25bd251SRusty Russell 192b25bd251SRusty Russell if (vq->vq.num_free < descs_used) { 1930a8a69ddSRusty Russell pr_debug("Can't add buf len %i - avail = %i\n", 194b25bd251SRusty Russell descs_used, vq->vq.num_free); 19544653eaeSRusty Russell /* FIXME: for historical reasons, we force a notify here if 19644653eaeSRusty Russell * there are outgoing parts to the buffer. Presumably the 19744653eaeSRusty Russell * host should service the ring ASAP. */ 19813816c76SRusty Russell if (out_sgs) 199426e3e0aSRusty Russell vq->notify(&vq->vq); 2000a8a69ddSRusty Russell END_USE(vq); 2010a8a69ddSRusty Russell return -ENOSPC; 2020a8a69ddSRusty Russell } 2030a8a69ddSRusty Russell 2040a8a69ddSRusty Russell /* We're about to use some buffers from the free list. */ 205b25bd251SRusty Russell vq->vq.num_free -= descs_used; 2060a8a69ddSRusty Russell 20713816c76SRusty Russell for (n = 0; n < out_sgs; n++) { 208eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 20900e6f3d9SMichael S. Tsirkin desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT); 21000e6f3d9SMichael S. Tsirkin desc[i].addr = cpu_to_virtio64(_vq->vdev, sg_phys(sg)); 21100e6f3d9SMichael S. Tsirkin desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); 2120a8a69ddSRusty Russell prev = i; 21300e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(_vq->vdev, desc[i].next); 2140a8a69ddSRusty Russell } 21513816c76SRusty Russell } 21613816c76SRusty Russell for (; n < (out_sgs + in_sgs); n++) { 217eeebf9b1SRusty Russell for (sg = sgs[n]; sg; sg = sg_next(sg)) { 21800e6f3d9SMichael S. Tsirkin desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE); 21900e6f3d9SMichael S. Tsirkin desc[i].addr = cpu_to_virtio64(_vq->vdev, sg_phys(sg)); 22000e6f3d9SMichael S. Tsirkin desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length); 2210a8a69ddSRusty Russell prev = i; 22200e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(_vq->vdev, desc[i].next); 22313816c76SRusty Russell } 2240a8a69ddSRusty Russell } 2250a8a69ddSRusty Russell /* Last one doesn't continue. */ 22600e6f3d9SMichael S. Tsirkin desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); 2270a8a69ddSRusty Russell 2280a8a69ddSRusty Russell /* Update free pointer */ 229b25bd251SRusty Russell if (indirect) 23000e6f3d9SMichael S. Tsirkin vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next); 231b25bd251SRusty Russell else 2320a8a69ddSRusty Russell vq->free_head = i; 2330a8a69ddSRusty Russell 2340a8a69ddSRusty Russell /* Set token. */ 2350a8a69ddSRusty Russell vq->data[head] = data; 2360a8a69ddSRusty Russell 2370a8a69ddSRusty Russell /* Put entry in available array (but don't update avail->idx until they 2383b720b8cSRusty Russell * do sync). */ 23900e6f3d9SMichael S. Tsirkin avail = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) & (vq->vring.num - 1); 24000e6f3d9SMichael S. Tsirkin vq->vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head); 2410a8a69ddSRusty Russell 242ee7cd898SRusty Russell /* Descriptors and available array need to be set before we expose the 243ee7cd898SRusty Russell * new available array entries. */ 244a9a0fef7SRusty Russell virtio_wmb(vq->weak_barriers); 24500e6f3d9SMichael S. Tsirkin vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) + 1); 246ee7cd898SRusty Russell vq->num_added++; 247ee7cd898SRusty Russell 248ee7cd898SRusty Russell /* This is very unlikely, but theoretically possible. Kick 249ee7cd898SRusty Russell * just in case. */ 250ee7cd898SRusty Russell if (unlikely(vq->num_added == (1 << 16) - 1)) 251ee7cd898SRusty Russell virtqueue_kick(_vq); 252ee7cd898SRusty Russell 2530a8a69ddSRusty Russell pr_debug("Added buffer head %i to %p\n", head, vq); 2540a8a69ddSRusty Russell END_USE(vq); 2553c1b27d5SRusty Russell 25698e8c6bcSRusty Russell return 0; 2570a8a69ddSRusty Russell } 25813816c76SRusty Russell 25913816c76SRusty Russell /** 26013816c76SRusty Russell * virtqueue_add_sgs - expose buffers to other end 26113816c76SRusty Russell * @vq: the struct virtqueue we're talking about. 26213816c76SRusty Russell * @sgs: array of terminated scatterlists. 26313816c76SRusty Russell * @out_num: the number of scatterlists readable by other side 26413816c76SRusty Russell * @in_num: the number of scatterlists which are writable (after readable ones) 26513816c76SRusty Russell * @data: the token identifying the buffer. 26613816c76SRusty Russell * @gfp: how to do memory allocations (if necessary). 26713816c76SRusty Russell * 26813816c76SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 26913816c76SRusty Russell * at the same time (except where noted). 27013816c76SRusty Russell * 27170670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 27213816c76SRusty Russell */ 27313816c76SRusty Russell int virtqueue_add_sgs(struct virtqueue *_vq, 27413816c76SRusty Russell struct scatterlist *sgs[], 27513816c76SRusty Russell unsigned int out_sgs, 27613816c76SRusty Russell unsigned int in_sgs, 27713816c76SRusty Russell void *data, 27813816c76SRusty Russell gfp_t gfp) 27913816c76SRusty Russell { 280eeebf9b1SRusty Russell unsigned int i, total_sg = 0; 28113816c76SRusty Russell 28213816c76SRusty Russell /* Count them first. */ 283eeebf9b1SRusty Russell for (i = 0; i < out_sgs + in_sgs; i++) { 28413816c76SRusty Russell struct scatterlist *sg; 28513816c76SRusty Russell for (sg = sgs[i]; sg; sg = sg_next(sg)) 286eeebf9b1SRusty Russell total_sg++; 28713816c76SRusty Russell } 288eeebf9b1SRusty Russell return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp); 28913816c76SRusty Russell } 29013816c76SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_sgs); 29113816c76SRusty Russell 29213816c76SRusty Russell /** 293282edb36SRusty Russell * virtqueue_add_outbuf - expose output buffers to other end 294282edb36SRusty Russell * @vq: the struct virtqueue we're talking about. 295eeebf9b1SRusty Russell * @sg: scatterlist (must be well-formed and terminated!) 296eeebf9b1SRusty Russell * @num: the number of entries in @sg readable by other side 297282edb36SRusty Russell * @data: the token identifying the buffer. 298282edb36SRusty Russell * @gfp: how to do memory allocations (if necessary). 299282edb36SRusty Russell * 300282edb36SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 301282edb36SRusty Russell * at the same time (except where noted). 302282edb36SRusty Russell * 30370670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 304282edb36SRusty Russell */ 305282edb36SRusty Russell int virtqueue_add_outbuf(struct virtqueue *vq, 306eeebf9b1SRusty Russell struct scatterlist *sg, unsigned int num, 307282edb36SRusty Russell void *data, 308282edb36SRusty Russell gfp_t gfp) 309282edb36SRusty Russell { 310eeebf9b1SRusty Russell return virtqueue_add(vq, &sg, num, 1, 0, data, gfp); 311282edb36SRusty Russell } 312282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_outbuf); 313282edb36SRusty Russell 314282edb36SRusty Russell /** 315282edb36SRusty Russell * virtqueue_add_inbuf - expose input buffers to other end 316282edb36SRusty Russell * @vq: the struct virtqueue we're talking about. 317eeebf9b1SRusty Russell * @sg: scatterlist (must be well-formed and terminated!) 318eeebf9b1SRusty Russell * @num: the number of entries in @sg writable by other side 319282edb36SRusty Russell * @data: the token identifying the buffer. 320282edb36SRusty Russell * @gfp: how to do memory allocations (if necessary). 321282edb36SRusty Russell * 322282edb36SRusty Russell * Caller must ensure we don't call this with other virtqueue operations 323282edb36SRusty Russell * at the same time (except where noted). 324282edb36SRusty Russell * 32570670444SRusty Russell * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). 326282edb36SRusty Russell */ 327282edb36SRusty Russell int virtqueue_add_inbuf(struct virtqueue *vq, 328eeebf9b1SRusty Russell struct scatterlist *sg, unsigned int num, 329282edb36SRusty Russell void *data, 330282edb36SRusty Russell gfp_t gfp) 331282edb36SRusty Russell { 332eeebf9b1SRusty Russell return virtqueue_add(vq, &sg, num, 0, 1, data, gfp); 333282edb36SRusty Russell } 334282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_inbuf); 335282edb36SRusty Russell 336282edb36SRusty Russell /** 33741f0377fSRusty Russell * virtqueue_kick_prepare - first half of split virtqueue_kick call. 3385dfc1762SRusty Russell * @vq: the struct virtqueue 3395dfc1762SRusty Russell * 34041f0377fSRusty Russell * Instead of virtqueue_kick(), you can do: 34141f0377fSRusty Russell * if (virtqueue_kick_prepare(vq)) 34241f0377fSRusty Russell * virtqueue_notify(vq); 3435dfc1762SRusty Russell * 34441f0377fSRusty Russell * This is sometimes useful because the virtqueue_kick_prepare() needs 34541f0377fSRusty Russell * to be serialized, but the actual virtqueue_notify() call does not. 3465dfc1762SRusty Russell */ 34741f0377fSRusty Russell bool virtqueue_kick_prepare(struct virtqueue *_vq) 3480a8a69ddSRusty Russell { 3490a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 350a5c262c5SMichael S. Tsirkin u16 new, old; 35141f0377fSRusty Russell bool needs_kick; 35241f0377fSRusty Russell 3530a8a69ddSRusty Russell START_USE(vq); 354a72caae2SJason Wang /* We need to expose available array entries before checking avail 355a72caae2SJason Wang * event. */ 356a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 3570a8a69ddSRusty Russell 35800e6f3d9SMichael S. Tsirkin old = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - vq->num_added; 35900e6f3d9SMichael S. Tsirkin new = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx); 3600a8a69ddSRusty Russell vq->num_added = 0; 3610a8a69ddSRusty Russell 362e93300b1SRusty Russell #ifdef DEBUG 363e93300b1SRusty Russell if (vq->last_add_time_valid) { 364e93300b1SRusty Russell WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), 365e93300b1SRusty Russell vq->last_add_time)) > 100); 366e93300b1SRusty Russell } 367e93300b1SRusty Russell vq->last_add_time_valid = false; 368e93300b1SRusty Russell #endif 369e93300b1SRusty Russell 37041f0377fSRusty Russell if (vq->event) { 37100e6f3d9SMichael S. Tsirkin needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, vring_avail_event(&vq->vring)), 37241f0377fSRusty Russell new, old); 37341f0377fSRusty Russell } else { 37400e6f3d9SMichael S. Tsirkin needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(_vq->vdev, VRING_USED_F_NO_NOTIFY)); 37541f0377fSRusty Russell } 3760a8a69ddSRusty Russell END_USE(vq); 37741f0377fSRusty Russell return needs_kick; 37841f0377fSRusty Russell } 37941f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); 38041f0377fSRusty Russell 38141f0377fSRusty Russell /** 38241f0377fSRusty Russell * virtqueue_notify - second half of split virtqueue_kick call. 38341f0377fSRusty Russell * @vq: the struct virtqueue 38441f0377fSRusty Russell * 38541f0377fSRusty Russell * This does not need to be serialized. 3865b1bf7cbSHeinz Graalfs * 3875b1bf7cbSHeinz Graalfs * Returns false if host notify failed or queue is broken, otherwise true. 38841f0377fSRusty Russell */ 3895b1bf7cbSHeinz Graalfs bool virtqueue_notify(struct virtqueue *_vq) 39041f0377fSRusty Russell { 39141f0377fSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 39241f0377fSRusty Russell 3935b1bf7cbSHeinz Graalfs if (unlikely(vq->broken)) 3945b1bf7cbSHeinz Graalfs return false; 3955b1bf7cbSHeinz Graalfs 39641f0377fSRusty Russell /* Prod other side to tell it about changes. */ 3972342d6a6SHeinz Graalfs if (!vq->notify(_vq)) { 3985b1bf7cbSHeinz Graalfs vq->broken = true; 3995b1bf7cbSHeinz Graalfs return false; 4005b1bf7cbSHeinz Graalfs } 4015b1bf7cbSHeinz Graalfs return true; 40241f0377fSRusty Russell } 40341f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify); 40441f0377fSRusty Russell 40541f0377fSRusty Russell /** 40641f0377fSRusty Russell * virtqueue_kick - update after add_buf 40741f0377fSRusty Russell * @vq: the struct virtqueue 40841f0377fSRusty Russell * 409b3087e48SRusty Russell * After one or more virtqueue_add_* calls, invoke this to kick 41041f0377fSRusty Russell * the other side. 41141f0377fSRusty Russell * 41241f0377fSRusty Russell * Caller must ensure we don't call this with other virtqueue 41341f0377fSRusty Russell * operations at the same time (except where noted). 4145b1bf7cbSHeinz Graalfs * 4155b1bf7cbSHeinz Graalfs * Returns false if kick failed, otherwise true. 41641f0377fSRusty Russell */ 4175b1bf7cbSHeinz Graalfs bool virtqueue_kick(struct virtqueue *vq) 41841f0377fSRusty Russell { 41941f0377fSRusty Russell if (virtqueue_kick_prepare(vq)) 4205b1bf7cbSHeinz Graalfs return virtqueue_notify(vq); 4215b1bf7cbSHeinz Graalfs return true; 4220a8a69ddSRusty Russell } 4237c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick); 4240a8a69ddSRusty Russell 4250a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head) 4260a8a69ddSRusty Russell { 4270a8a69ddSRusty Russell unsigned int i; 4280a8a69ddSRusty Russell 4290a8a69ddSRusty Russell /* Clear data ptr. */ 4300a8a69ddSRusty Russell vq->data[head] = NULL; 4310a8a69ddSRusty Russell 4320a8a69ddSRusty Russell /* Put back on free list: find end */ 4330a8a69ddSRusty Russell i = head; 4349fa29b9dSMark McLoughlin 4359fa29b9dSMark McLoughlin /* Free the indirect table */ 43600e6f3d9SMichael S. Tsirkin if (vq->vring.desc[i].flags & cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)) 43700e6f3d9SMichael S. Tsirkin kfree(phys_to_virt(virtio64_to_cpu(vq->vq.vdev, vq->vring.desc[i].addr))); 4389fa29b9dSMark McLoughlin 43900e6f3d9SMichael S. Tsirkin while (vq->vring.desc[i].flags & cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT)) { 44000e6f3d9SMichael S. Tsirkin i = virtio16_to_cpu(vq->vq.vdev, vq->vring.desc[i].next); 44106ca287dSRusty Russell vq->vq.num_free++; 4420a8a69ddSRusty Russell } 4430a8a69ddSRusty Russell 44400e6f3d9SMichael S. Tsirkin vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head); 4450a8a69ddSRusty Russell vq->free_head = head; 4460a8a69ddSRusty Russell /* Plus final descriptor */ 44706ca287dSRusty Russell vq->vq.num_free++; 4480a8a69ddSRusty Russell } 4490a8a69ddSRusty Russell 4500a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq) 4510a8a69ddSRusty Russell { 45200e6f3d9SMichael S. Tsirkin return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, vq->vring.used->idx); 4530a8a69ddSRusty Russell } 4540a8a69ddSRusty Russell 4555dfc1762SRusty Russell /** 4565dfc1762SRusty Russell * virtqueue_get_buf - get the next used buffer 4575dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 4585dfc1762SRusty Russell * @len: the length written into the buffer 4595dfc1762SRusty Russell * 4605dfc1762SRusty Russell * If the driver wrote data into the buffer, @len will be set to the 4615dfc1762SRusty Russell * amount written. This means you don't need to clear the buffer 4625dfc1762SRusty Russell * beforehand to ensure there's no data leakage in the case of short 4635dfc1762SRusty Russell * writes. 4645dfc1762SRusty Russell * 4655dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 4665dfc1762SRusty Russell * operations at the same time (except where noted). 4675dfc1762SRusty Russell * 4685dfc1762SRusty Russell * Returns NULL if there are no used buffers, or the "data" token 469b3087e48SRusty Russell * handed to virtqueue_add_*(). 4705dfc1762SRusty Russell */ 4717c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) 4720a8a69ddSRusty Russell { 4730a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 4740a8a69ddSRusty Russell void *ret; 4750a8a69ddSRusty Russell unsigned int i; 4763b720b8cSRusty Russell u16 last_used; 4770a8a69ddSRusty Russell 4780a8a69ddSRusty Russell START_USE(vq); 4790a8a69ddSRusty Russell 4805ef82752SRusty Russell if (unlikely(vq->broken)) { 4815ef82752SRusty Russell END_USE(vq); 4825ef82752SRusty Russell return NULL; 4835ef82752SRusty Russell } 4845ef82752SRusty Russell 4850a8a69ddSRusty Russell if (!more_used(vq)) { 4860a8a69ddSRusty Russell pr_debug("No more buffers in queue\n"); 4870a8a69ddSRusty Russell END_USE(vq); 4880a8a69ddSRusty Russell return NULL; 4890a8a69ddSRusty Russell } 4900a8a69ddSRusty Russell 4912d61ba95SMichael S. Tsirkin /* Only get used array entries after they have been exposed by host. */ 492a9a0fef7SRusty Russell virtio_rmb(vq->weak_barriers); 4932d61ba95SMichael S. Tsirkin 4943b720b8cSRusty Russell last_used = (vq->last_used_idx & (vq->vring.num - 1)); 49500e6f3d9SMichael S. Tsirkin i = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].id); 49600e6f3d9SMichael S. Tsirkin *len = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].len); 4970a8a69ddSRusty Russell 4980a8a69ddSRusty Russell if (unlikely(i >= vq->vring.num)) { 4990a8a69ddSRusty Russell BAD_RING(vq, "id %u out of range\n", i); 5000a8a69ddSRusty Russell return NULL; 5010a8a69ddSRusty Russell } 5020a8a69ddSRusty Russell if (unlikely(!vq->data[i])) { 5030a8a69ddSRusty Russell BAD_RING(vq, "id %u is not a head!\n", i); 5040a8a69ddSRusty Russell return NULL; 5050a8a69ddSRusty Russell } 5060a8a69ddSRusty Russell 5070a8a69ddSRusty Russell /* detach_buf clears data, so grab it now. */ 5080a8a69ddSRusty Russell ret = vq->data[i]; 5090a8a69ddSRusty Russell detach_buf(vq, i); 5100a8a69ddSRusty Russell vq->last_used_idx++; 511a5c262c5SMichael S. Tsirkin /* If we expect an interrupt for the next entry, tell host 512a5c262c5SMichael S. Tsirkin * by writing event index and flush out the write before 513a5c262c5SMichael S. Tsirkin * the read in the next get_buf call. */ 51400e6f3d9SMichael S. Tsirkin if (!(vq->vring.avail->flags & cpu_to_virtio16(_vq->vdev, VRING_AVAIL_F_NO_INTERRUPT))) { 51500e6f3d9SMichael S. Tsirkin vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx); 516a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 517a5c262c5SMichael S. Tsirkin } 518a5c262c5SMichael S. Tsirkin 519e93300b1SRusty Russell #ifdef DEBUG 520e93300b1SRusty Russell vq->last_add_time_valid = false; 521e93300b1SRusty Russell #endif 522e93300b1SRusty Russell 5230a8a69ddSRusty Russell END_USE(vq); 5240a8a69ddSRusty Russell return ret; 5250a8a69ddSRusty Russell } 5267c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf); 5270a8a69ddSRusty Russell 5285dfc1762SRusty Russell /** 5295dfc1762SRusty Russell * virtqueue_disable_cb - disable callbacks 5305dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 5315dfc1762SRusty Russell * 5325dfc1762SRusty Russell * Note that this is not necessarily synchronous, hence unreliable and only 5335dfc1762SRusty Russell * useful as an optimization. 5345dfc1762SRusty Russell * 5355dfc1762SRusty Russell * Unlike other operations, this need not be serialized. 5365dfc1762SRusty Russell */ 5377c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq) 53818445c4dSRusty Russell { 53918445c4dSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 54018445c4dSRusty Russell 54100e6f3d9SMichael S. Tsirkin vq->vring.avail->flags |= cpu_to_virtio16(_vq->vdev, VRING_AVAIL_F_NO_INTERRUPT); 54218445c4dSRusty Russell } 5437c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb); 54418445c4dSRusty Russell 5455dfc1762SRusty Russell /** 546cc229884SMichael S. Tsirkin * virtqueue_enable_cb_prepare - restart callbacks after disable_cb 547cc229884SMichael S. Tsirkin * @vq: the struct virtqueue we're talking about. 548cc229884SMichael S. Tsirkin * 549cc229884SMichael S. Tsirkin * This re-enables callbacks; it returns current queue state 550cc229884SMichael S. Tsirkin * in an opaque unsigned value. This value should be later tested by 551cc229884SMichael S. Tsirkin * virtqueue_poll, to detect a possible race between the driver checking for 552cc229884SMichael S. Tsirkin * more work, and enabling callbacks. 553cc229884SMichael S. Tsirkin * 554cc229884SMichael S. Tsirkin * Caller must ensure we don't call this with other virtqueue 555cc229884SMichael S. Tsirkin * operations at the same time (except where noted). 556cc229884SMichael S. Tsirkin */ 557cc229884SMichael S. Tsirkin unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq) 558cc229884SMichael S. Tsirkin { 559cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 560cc229884SMichael S. Tsirkin u16 last_used_idx; 561cc229884SMichael S. Tsirkin 562cc229884SMichael S. Tsirkin START_USE(vq); 563cc229884SMichael S. Tsirkin 564cc229884SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 565cc229884SMichael S. Tsirkin * more to do. */ 566cc229884SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to 567cc229884SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 568cc229884SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 56900e6f3d9SMichael S. Tsirkin vq->vring.avail->flags &= cpu_to_virtio16(_vq->vdev, ~VRING_AVAIL_F_NO_INTERRUPT); 57000e6f3d9SMichael S. Tsirkin vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, last_used_idx = vq->last_used_idx); 571cc229884SMichael S. Tsirkin END_USE(vq); 572cc229884SMichael S. Tsirkin return last_used_idx; 573cc229884SMichael S. Tsirkin } 574cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); 575cc229884SMichael S. Tsirkin 576cc229884SMichael S. Tsirkin /** 577cc229884SMichael S. Tsirkin * virtqueue_poll - query pending used buffers 578cc229884SMichael S. Tsirkin * @vq: the struct virtqueue we're talking about. 579cc229884SMichael S. Tsirkin * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare). 580cc229884SMichael S. Tsirkin * 581cc229884SMichael S. Tsirkin * Returns "true" if there are pending used buffers in the queue. 582cc229884SMichael S. Tsirkin * 583cc229884SMichael S. Tsirkin * This does not need to be serialized. 584cc229884SMichael S. Tsirkin */ 585cc229884SMichael S. Tsirkin bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx) 586cc229884SMichael S. Tsirkin { 587cc229884SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 588cc229884SMichael S. Tsirkin 589cc229884SMichael S. Tsirkin virtio_mb(vq->weak_barriers); 59000e6f3d9SMichael S. Tsirkin return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, vq->vring.used->idx); 591cc229884SMichael S. Tsirkin } 592cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_poll); 593cc229884SMichael S. Tsirkin 594cc229884SMichael S. Tsirkin /** 5955dfc1762SRusty Russell * virtqueue_enable_cb - restart callbacks after disable_cb. 5965dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 5975dfc1762SRusty Russell * 5985dfc1762SRusty Russell * This re-enables callbacks; it returns "false" if there are pending 5995dfc1762SRusty Russell * buffers in the queue, to detect a possible race between the driver 6005dfc1762SRusty Russell * checking for more work, and enabling callbacks. 6015dfc1762SRusty Russell * 6025dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 6035dfc1762SRusty Russell * operations at the same time (except where noted). 6045dfc1762SRusty Russell */ 6057c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq) 6060a8a69ddSRusty Russell { 607cc229884SMichael S. Tsirkin unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq); 608cc229884SMichael S. Tsirkin return !virtqueue_poll(_vq, last_used_idx); 6090a8a69ddSRusty Russell } 6107c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb); 6110a8a69ddSRusty Russell 6125dfc1762SRusty Russell /** 6135dfc1762SRusty Russell * virtqueue_enable_cb_delayed - restart callbacks after disable_cb. 6145dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 6155dfc1762SRusty Russell * 6165dfc1762SRusty Russell * This re-enables callbacks but hints to the other side to delay 6175dfc1762SRusty Russell * interrupts until most of the available buffers have been processed; 6185dfc1762SRusty Russell * it returns "false" if there are many pending buffers in the queue, 6195dfc1762SRusty Russell * to detect a possible race between the driver checking for more work, 6205dfc1762SRusty Russell * and enabling callbacks. 6215dfc1762SRusty Russell * 6225dfc1762SRusty Russell * Caller must ensure we don't call this with other virtqueue 6235dfc1762SRusty Russell * operations at the same time (except where noted). 6245dfc1762SRusty Russell */ 6257ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq) 6267ab358c2SMichael S. Tsirkin { 6277ab358c2SMichael S. Tsirkin struct vring_virtqueue *vq = to_vvq(_vq); 6287ab358c2SMichael S. Tsirkin u16 bufs; 6297ab358c2SMichael S. Tsirkin 6307ab358c2SMichael S. Tsirkin START_USE(vq); 6317ab358c2SMichael S. Tsirkin 6327ab358c2SMichael S. Tsirkin /* We optimistically turn back on interrupts, then check if there was 6337ab358c2SMichael S. Tsirkin * more to do. */ 6347ab358c2SMichael S. Tsirkin /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to 6357ab358c2SMichael S. Tsirkin * either clear the flags bit or point the event index at the next 6367ab358c2SMichael S. Tsirkin * entry. Always do both to keep code simple. */ 63700e6f3d9SMichael S. Tsirkin vq->vring.avail->flags &= cpu_to_virtio16(_vq->vdev, ~VRING_AVAIL_F_NO_INTERRUPT); 6387ab358c2SMichael S. Tsirkin /* TODO: tune this threshold */ 63900e6f3d9SMichael S. Tsirkin bufs = (u16)(virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - vq->last_used_idx) * 3 / 4; 64000e6f3d9SMichael S. Tsirkin vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs); 641a9a0fef7SRusty Russell virtio_mb(vq->weak_barriers); 64200e6f3d9SMichael S. Tsirkin if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) { 6437ab358c2SMichael S. Tsirkin END_USE(vq); 6447ab358c2SMichael S. Tsirkin return false; 6457ab358c2SMichael S. Tsirkin } 6467ab358c2SMichael S. Tsirkin 6477ab358c2SMichael S. Tsirkin END_USE(vq); 6487ab358c2SMichael S. Tsirkin return true; 6497ab358c2SMichael S. Tsirkin } 6507ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); 6517ab358c2SMichael S. Tsirkin 6525dfc1762SRusty Russell /** 6535dfc1762SRusty Russell * virtqueue_detach_unused_buf - detach first unused buffer 6545dfc1762SRusty Russell * @vq: the struct virtqueue we're talking about. 6555dfc1762SRusty Russell * 656b3087e48SRusty Russell * Returns NULL or the "data" token handed to virtqueue_add_*(). 6575dfc1762SRusty Russell * This is not valid on an active queue; it is useful only for device 6585dfc1762SRusty Russell * shutdown. 6595dfc1762SRusty Russell */ 6607c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq) 661c021eac4SShirley Ma { 662c021eac4SShirley Ma struct vring_virtqueue *vq = to_vvq(_vq); 663c021eac4SShirley Ma unsigned int i; 664c021eac4SShirley Ma void *buf; 665c021eac4SShirley Ma 666c021eac4SShirley Ma START_USE(vq); 667c021eac4SShirley Ma 668c021eac4SShirley Ma for (i = 0; i < vq->vring.num; i++) { 669c021eac4SShirley Ma if (!vq->data[i]) 670c021eac4SShirley Ma continue; 671c021eac4SShirley Ma /* detach_buf clears data, so grab it now. */ 672c021eac4SShirley Ma buf = vq->data[i]; 673c021eac4SShirley Ma detach_buf(vq, i); 67400e6f3d9SMichael S. Tsirkin vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - 1); 675c021eac4SShirley Ma END_USE(vq); 676c021eac4SShirley Ma return buf; 677c021eac4SShirley Ma } 678c021eac4SShirley Ma /* That should have freed everything. */ 67906ca287dSRusty Russell BUG_ON(vq->vq.num_free != vq->vring.num); 680c021eac4SShirley Ma 681c021eac4SShirley Ma END_USE(vq); 682c021eac4SShirley Ma return NULL; 683c021eac4SShirley Ma } 6847c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); 685c021eac4SShirley Ma 6860a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq) 6870a8a69ddSRusty Russell { 6880a8a69ddSRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 6890a8a69ddSRusty Russell 6900a8a69ddSRusty Russell if (!more_used(vq)) { 6910a8a69ddSRusty Russell pr_debug("virtqueue interrupt with no work for %p\n", vq); 6920a8a69ddSRusty Russell return IRQ_NONE; 6930a8a69ddSRusty Russell } 6940a8a69ddSRusty Russell 6950a8a69ddSRusty Russell if (unlikely(vq->broken)) 6960a8a69ddSRusty Russell return IRQ_HANDLED; 6970a8a69ddSRusty Russell 6980a8a69ddSRusty Russell pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback); 69918445c4dSRusty Russell if (vq->vq.callback) 70018445c4dSRusty Russell vq->vq.callback(&vq->vq); 7010a8a69ddSRusty Russell 7020a8a69ddSRusty Russell return IRQ_HANDLED; 7030a8a69ddSRusty Russell } 704c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt); 7050a8a69ddSRusty Russell 70617bb6d40SJason Wang struct virtqueue *vring_new_virtqueue(unsigned int index, 70717bb6d40SJason Wang unsigned int num, 70887c7d57cSRusty Russell unsigned int vring_align, 7090a8a69ddSRusty Russell struct virtio_device *vdev, 7107b21e34fSRusty Russell bool weak_barriers, 7110a8a69ddSRusty Russell void *pages, 71246f9c2b9SHeinz Graalfs bool (*notify)(struct virtqueue *), 7139499f5e7SRusty Russell void (*callback)(struct virtqueue *), 7149499f5e7SRusty Russell const char *name) 7150a8a69ddSRusty Russell { 7160a8a69ddSRusty Russell struct vring_virtqueue *vq; 7170a8a69ddSRusty Russell unsigned int i; 7180a8a69ddSRusty Russell 71942b36cc0SRusty Russell /* We assume num is a power of 2. */ 72042b36cc0SRusty Russell if (num & (num - 1)) { 72142b36cc0SRusty Russell dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num); 72242b36cc0SRusty Russell return NULL; 72342b36cc0SRusty Russell } 72442b36cc0SRusty Russell 7250a8a69ddSRusty Russell vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL); 7260a8a69ddSRusty Russell if (!vq) 7270a8a69ddSRusty Russell return NULL; 7280a8a69ddSRusty Russell 72987c7d57cSRusty Russell vring_init(&vq->vring, num, pages, vring_align); 7300a8a69ddSRusty Russell vq->vq.callback = callback; 7310a8a69ddSRusty Russell vq->vq.vdev = vdev; 7329499f5e7SRusty Russell vq->vq.name = name; 73306ca287dSRusty Russell vq->vq.num_free = num; 73406ca287dSRusty Russell vq->vq.index = index; 7350a8a69ddSRusty Russell vq->notify = notify; 7367b21e34fSRusty Russell vq->weak_barriers = weak_barriers; 7370a8a69ddSRusty Russell vq->broken = false; 7380a8a69ddSRusty Russell vq->last_used_idx = 0; 7390a8a69ddSRusty Russell vq->num_added = 0; 7409499f5e7SRusty Russell list_add_tail(&vq->vq.list, &vdev->vqs); 7410a8a69ddSRusty Russell #ifdef DEBUG 7420a8a69ddSRusty Russell vq->in_use = false; 743e93300b1SRusty Russell vq->last_add_time_valid = false; 7440a8a69ddSRusty Russell #endif 7450a8a69ddSRusty Russell 7469fa29b9dSMark McLoughlin vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC); 747a5c262c5SMichael S. Tsirkin vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); 7489fa29b9dSMark McLoughlin 7490a8a69ddSRusty Russell /* No callback? Tell other side not to bother us. */ 7500a8a69ddSRusty Russell if (!callback) 75100e6f3d9SMichael S. Tsirkin vq->vring.avail->flags |= cpu_to_virtio16(vdev, VRING_AVAIL_F_NO_INTERRUPT); 7520a8a69ddSRusty Russell 7530a8a69ddSRusty Russell /* Put everything in free lists. */ 7540a8a69ddSRusty Russell vq->free_head = 0; 7553b870624SAmit Shah for (i = 0; i < num-1; i++) { 75600e6f3d9SMichael S. Tsirkin vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1); 7573b870624SAmit Shah vq->data[i] = NULL; 7583b870624SAmit Shah } 7593b870624SAmit Shah vq->data[i] = NULL; 7600a8a69ddSRusty Russell 7610a8a69ddSRusty Russell return &vq->vq; 7620a8a69ddSRusty Russell } 763c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue); 7640a8a69ddSRusty Russell 7650a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq) 7660a8a69ddSRusty Russell { 7679499f5e7SRusty Russell list_del(&vq->list); 7680a8a69ddSRusty Russell kfree(to_vvq(vq)); 7690a8a69ddSRusty Russell } 770c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue); 7710a8a69ddSRusty Russell 772e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */ 773e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev) 774e34f8725SRusty Russell { 775e34f8725SRusty Russell unsigned int i; 776e34f8725SRusty Russell 777e34f8725SRusty Russell for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) { 778e34f8725SRusty Russell switch (i) { 7799fa29b9dSMark McLoughlin case VIRTIO_RING_F_INDIRECT_DESC: 7809fa29b9dSMark McLoughlin break; 781a5c262c5SMichael S. Tsirkin case VIRTIO_RING_F_EVENT_IDX: 782a5c262c5SMichael S. Tsirkin break; 783e34f8725SRusty Russell default: 784e34f8725SRusty Russell /* We don't understand this bit. */ 785e16e12beSMichael S. Tsirkin __virtio_clear_bit(vdev, i); 786e34f8725SRusty Russell } 787e34f8725SRusty Russell } 788e34f8725SRusty Russell } 789e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features); 790e34f8725SRusty Russell 7915dfc1762SRusty Russell /** 7925dfc1762SRusty Russell * virtqueue_get_vring_size - return the size of the virtqueue's vring 7935dfc1762SRusty Russell * @vq: the struct virtqueue containing the vring of interest. 7945dfc1762SRusty Russell * 7955dfc1762SRusty Russell * Returns the size of the vring. This is mainly used for boasting to 7965dfc1762SRusty Russell * userspace. Unlike other operations, this need not be serialized. 7975dfc1762SRusty Russell */ 7988f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq) 7998f9f4668SRick Jones { 8008f9f4668SRick Jones 8018f9f4668SRick Jones struct vring_virtqueue *vq = to_vvq(_vq); 8028f9f4668SRick Jones 8038f9f4668SRick Jones return vq->vring.num; 8048f9f4668SRick Jones } 8058f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); 8068f9f4668SRick Jones 807b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq) 808b3b32c94SHeinz Graalfs { 809b3b32c94SHeinz Graalfs struct vring_virtqueue *vq = to_vvq(_vq); 810b3b32c94SHeinz Graalfs 811b3b32c94SHeinz Graalfs return vq->broken; 812b3b32c94SHeinz Graalfs } 813b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken); 814b3b32c94SHeinz Graalfs 815e2dcdfe9SRusty Russell /* 816e2dcdfe9SRusty Russell * This should prevent the device from being used, allowing drivers to 817e2dcdfe9SRusty Russell * recover. You may need to grab appropriate locks to flush. 818e2dcdfe9SRusty Russell */ 819e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev) 820e2dcdfe9SRusty Russell { 821e2dcdfe9SRusty Russell struct virtqueue *_vq; 822e2dcdfe9SRusty Russell 823e2dcdfe9SRusty Russell list_for_each_entry(_vq, &dev->vqs, list) { 824e2dcdfe9SRusty Russell struct vring_virtqueue *vq = to_vvq(_vq); 825e2dcdfe9SRusty Russell vq->broken = true; 826e2dcdfe9SRusty Russell } 827e2dcdfe9SRusty Russell } 828e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device); 829e2dcdfe9SRusty Russell 830*89062652SCornelia Huck void *virtqueue_get_avail(struct virtqueue *_vq) 831*89062652SCornelia Huck { 832*89062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 833*89062652SCornelia Huck 834*89062652SCornelia Huck return vq->vring.avail; 835*89062652SCornelia Huck } 836*89062652SCornelia Huck EXPORT_SYMBOL_GPL(virtqueue_get_avail); 837*89062652SCornelia Huck 838*89062652SCornelia Huck void *virtqueue_get_used(struct virtqueue *_vq) 839*89062652SCornelia Huck { 840*89062652SCornelia Huck struct vring_virtqueue *vq = to_vvq(_vq); 841*89062652SCornelia Huck 842*89062652SCornelia Huck return vq->vring.used; 843*89062652SCornelia Huck } 844*89062652SCornelia Huck EXPORT_SYMBOL_GPL(virtqueue_get_used); 845*89062652SCornelia Huck 846c6fd4701SRusty Russell MODULE_LICENSE("GPL"); 847