xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision ee7cd8981e15bcb365fc762afe3fc47b8242f630)
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>
250a8a69ddSRusty Russell 
26d57ed95dSMichael S. Tsirkin /* virtio guest is communicating with a virtual "device" that actually runs on
27d57ed95dSMichael S. Tsirkin  * a host processor.  Memory barriers are used to control SMP effects. */
28d57ed95dSMichael S. Tsirkin #ifdef CONFIG_SMP
29d57ed95dSMichael S. Tsirkin /* Where possible, use SMP barriers which are more lightweight than mandatory
30d57ed95dSMichael S. Tsirkin  * barriers, because mandatory barriers control MMIO effects on accesses
317b21e34fSRusty Russell  * through relaxed memory I/O windows (which virtio-pci does not use). */
327b21e34fSRusty Russell #define virtio_mb(vq) \
337b21e34fSRusty Russell 	do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0)
347b21e34fSRusty Russell #define virtio_rmb(vq) \
357b21e34fSRusty Russell 	do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
367b21e34fSRusty Russell #define virtio_wmb(vq) \
377b21e34fSRusty Russell 	do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
38d57ed95dSMichael S. Tsirkin #else
39d57ed95dSMichael S. Tsirkin /* We must force memory ordering even if guest is UP since host could be
40d57ed95dSMichael S. Tsirkin  * running on another CPU, but SMP barriers are defined to barrier() in that
41d57ed95dSMichael S. Tsirkin  * configuration. So fall back to mandatory barriers instead. */
427b21e34fSRusty Russell #define virtio_mb(vq) mb()
437b21e34fSRusty Russell #define virtio_rmb(vq) rmb()
447b21e34fSRusty Russell #define virtio_wmb(vq) wmb()
45d57ed95dSMichael S. Tsirkin #endif
46d57ed95dSMichael S. Tsirkin 
470a8a69ddSRusty Russell #ifdef DEBUG
480a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */
499499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
509499f5e7SRusty Russell 	do {							\
519499f5e7SRusty Russell 		dev_err(&(_vq)->vq.vdev->dev,			\
529499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
539499f5e7SRusty Russell 		BUG();						\
549499f5e7SRusty Russell 	} while (0)
55c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */
563a35ce7dSRoel Kluin #define START_USE(_vq)						\
57c5f841f1SRusty Russell 	do {							\
58c5f841f1SRusty Russell 		if ((_vq)->in_use)				\
599499f5e7SRusty Russell 			panic("%s:in_use = %i\n",		\
609499f5e7SRusty Russell 			      (_vq)->vq.name, (_vq)->in_use);	\
61c5f841f1SRusty Russell 		(_vq)->in_use = __LINE__;			\
62c5f841f1SRusty Russell 	} while (0)
633a35ce7dSRoel Kluin #define END_USE(_vq) \
6497a545abSRusty Russell 	do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
650a8a69ddSRusty Russell #else
669499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
679499f5e7SRusty Russell 	do {							\
689499f5e7SRusty Russell 		dev_err(&_vq->vq.vdev->dev,			\
699499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
709499f5e7SRusty Russell 		(_vq)->broken = true;				\
719499f5e7SRusty Russell 	} while (0)
720a8a69ddSRusty Russell #define START_USE(vq)
730a8a69ddSRusty Russell #define END_USE(vq)
740a8a69ddSRusty Russell #endif
750a8a69ddSRusty Russell 
760a8a69ddSRusty Russell struct vring_virtqueue
770a8a69ddSRusty Russell {
780a8a69ddSRusty Russell 	struct virtqueue vq;
790a8a69ddSRusty Russell 
800a8a69ddSRusty Russell 	/* Actual memory layout for this queue */
810a8a69ddSRusty Russell 	struct vring vring;
820a8a69ddSRusty Russell 
837b21e34fSRusty Russell 	/* Can we use weak barriers? */
847b21e34fSRusty Russell 	bool weak_barriers;
857b21e34fSRusty Russell 
860a8a69ddSRusty Russell 	/* Other side has made a mess, don't try any more. */
870a8a69ddSRusty Russell 	bool broken;
880a8a69ddSRusty Russell 
899fa29b9dSMark McLoughlin 	/* Host supports indirect buffers */
909fa29b9dSMark McLoughlin 	bool indirect;
919fa29b9dSMark McLoughlin 
92a5c262c5SMichael S. Tsirkin 	/* Host publishes avail event idx */
93a5c262c5SMichael S. Tsirkin 	bool event;
94a5c262c5SMichael S. Tsirkin 
950a8a69ddSRusty Russell 	/* Number of free buffers */
960a8a69ddSRusty Russell 	unsigned int num_free;
970a8a69ddSRusty Russell 	/* Head of free buffer list. */
980a8a69ddSRusty Russell 	unsigned int free_head;
990a8a69ddSRusty Russell 	/* Number we've added since last sync. */
1000a8a69ddSRusty Russell 	unsigned int num_added;
1010a8a69ddSRusty Russell 
1020a8a69ddSRusty Russell 	/* Last used index we've seen. */
1031bc4953eSAnthony Liguori 	u16 last_used_idx;
1040a8a69ddSRusty Russell 
1050a8a69ddSRusty Russell 	/* How to notify other side. FIXME: commonalize hcalls! */
1060a8a69ddSRusty Russell 	void (*notify)(struct virtqueue *vq);
1070a8a69ddSRusty Russell 
1080a8a69ddSRusty Russell #ifdef DEBUG
1090a8a69ddSRusty Russell 	/* They're supposed to lock for us. */
1100a8a69ddSRusty Russell 	unsigned int in_use;
1110a8a69ddSRusty Russell #endif
1120a8a69ddSRusty Russell 
1130a8a69ddSRusty Russell 	/* Tokens for callbacks. */
1140a8a69ddSRusty Russell 	void *data[];
1150a8a69ddSRusty Russell };
1160a8a69ddSRusty Russell 
1170a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
1180a8a69ddSRusty Russell 
1199fa29b9dSMark McLoughlin /* Set up an indirect table of descriptors and add it to the queue. */
1209fa29b9dSMark McLoughlin static int vring_add_indirect(struct vring_virtqueue *vq,
1219fa29b9dSMark McLoughlin 			      struct scatterlist sg[],
1229fa29b9dSMark McLoughlin 			      unsigned int out,
123bbd603efSMichael S. Tsirkin 			      unsigned int in,
124bbd603efSMichael S. Tsirkin 			      gfp_t gfp)
1259fa29b9dSMark McLoughlin {
1269fa29b9dSMark McLoughlin 	struct vring_desc *desc;
1279fa29b9dSMark McLoughlin 	unsigned head;
1289fa29b9dSMark McLoughlin 	int i;
1299fa29b9dSMark McLoughlin 
130bbd603efSMichael S. Tsirkin 	desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
1319fa29b9dSMark McLoughlin 	if (!desc)
132686d3637SMichael S. Tsirkin 		return -ENOMEM;
1339fa29b9dSMark McLoughlin 
1349fa29b9dSMark McLoughlin 	/* Transfer entries from the sg list into the indirect page */
1359fa29b9dSMark McLoughlin 	for (i = 0; i < out; i++) {
1369fa29b9dSMark McLoughlin 		desc[i].flags = VRING_DESC_F_NEXT;
1379fa29b9dSMark McLoughlin 		desc[i].addr = sg_phys(sg);
1389fa29b9dSMark McLoughlin 		desc[i].len = sg->length;
1399fa29b9dSMark McLoughlin 		desc[i].next = i+1;
1409fa29b9dSMark McLoughlin 		sg++;
1419fa29b9dSMark McLoughlin 	}
1429fa29b9dSMark McLoughlin 	for (; i < (out + in); i++) {
1439fa29b9dSMark McLoughlin 		desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
1449fa29b9dSMark McLoughlin 		desc[i].addr = sg_phys(sg);
1459fa29b9dSMark McLoughlin 		desc[i].len = sg->length;
1469fa29b9dSMark McLoughlin 		desc[i].next = i+1;
1479fa29b9dSMark McLoughlin 		sg++;
1489fa29b9dSMark McLoughlin 	}
1499fa29b9dSMark McLoughlin 
1509fa29b9dSMark McLoughlin 	/* Last one doesn't continue. */
1519fa29b9dSMark McLoughlin 	desc[i-1].flags &= ~VRING_DESC_F_NEXT;
1529fa29b9dSMark McLoughlin 	desc[i-1].next = 0;
1539fa29b9dSMark McLoughlin 
1549fa29b9dSMark McLoughlin 	/* We're about to use a buffer */
1559fa29b9dSMark McLoughlin 	vq->num_free--;
1569fa29b9dSMark McLoughlin 
1579fa29b9dSMark McLoughlin 	/* Use a single buffer which doesn't continue */
1589fa29b9dSMark McLoughlin 	head = vq->free_head;
1599fa29b9dSMark McLoughlin 	vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
1609fa29b9dSMark McLoughlin 	vq->vring.desc[head].addr = virt_to_phys(desc);
1619fa29b9dSMark McLoughlin 	vq->vring.desc[head].len = i * sizeof(struct vring_desc);
1629fa29b9dSMark McLoughlin 
1639fa29b9dSMark McLoughlin 	/* Update free pointer */
1649fa29b9dSMark McLoughlin 	vq->free_head = vq->vring.desc[head].next;
1659fa29b9dSMark McLoughlin 
1669fa29b9dSMark McLoughlin 	return head;
1679fa29b9dSMark McLoughlin }
1689fa29b9dSMark McLoughlin 
1695dfc1762SRusty Russell /**
170f96fde41SRusty Russell  * virtqueue_add_buf - expose buffer to other end
1715dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
1725dfc1762SRusty Russell  * @sg: the description of the buffer(s).
1735dfc1762SRusty Russell  * @out_num: the number of sg readable by other side
1745dfc1762SRusty Russell  * @in_num: the number of sg which are writable (after readable ones)
1755dfc1762SRusty Russell  * @data: the token identifying the buffer.
1765dfc1762SRusty Russell  * @gfp: how to do memory allocations (if necessary).
1775dfc1762SRusty Russell  *
1785dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
1795dfc1762SRusty Russell  * at the same time (except where noted).
1805dfc1762SRusty Russell  *
1815dfc1762SRusty Russell  * Returns remaining capacity of queue or a negative error
1825dfc1762SRusty Russell  * (ie. ENOSPC).  Note that it only really makes sense to treat all
1835dfc1762SRusty Russell  * positive return values as "available": indirect buffers mean that
1845dfc1762SRusty Russell  * we can put an entire sg[] array inside a single queue entry.
1855dfc1762SRusty Russell  */
186f96fde41SRusty Russell int virtqueue_add_buf(struct virtqueue *_vq,
1870a8a69ddSRusty Russell 		      struct scatterlist sg[],
1880a8a69ddSRusty Russell 		      unsigned int out,
1890a8a69ddSRusty Russell 		      unsigned int in,
190bbd603efSMichael S. Tsirkin 		      void *data,
191bbd603efSMichael S. Tsirkin 		      gfp_t gfp)
1920a8a69ddSRusty Russell {
1930a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
1941fe9b6feSMichael S. Tsirkin 	unsigned int i, avail, uninitialized_var(prev);
1951fe9b6feSMichael S. Tsirkin 	int head;
1960a8a69ddSRusty Russell 
1979fa29b9dSMark McLoughlin 	START_USE(vq);
1989fa29b9dSMark McLoughlin 
1990a8a69ddSRusty Russell 	BUG_ON(data == NULL);
2009fa29b9dSMark McLoughlin 
2019fa29b9dSMark McLoughlin 	/* If the host supports indirect descriptor tables, and we have multiple
2029fa29b9dSMark McLoughlin 	 * buffers, then go indirect. FIXME: tune this threshold */
2039fa29b9dSMark McLoughlin 	if (vq->indirect && (out + in) > 1 && vq->num_free) {
204bbd603efSMichael S. Tsirkin 		head = vring_add_indirect(vq, sg, out, in, gfp);
2051fe9b6feSMichael S. Tsirkin 		if (likely(head >= 0))
2069fa29b9dSMark McLoughlin 			goto add_head;
2079fa29b9dSMark McLoughlin 	}
2089fa29b9dSMark McLoughlin 
2090a8a69ddSRusty Russell 	BUG_ON(out + in > vq->vring.num);
2100a8a69ddSRusty Russell 	BUG_ON(out + in == 0);
2110a8a69ddSRusty Russell 
2120a8a69ddSRusty Russell 	if (vq->num_free < out + in) {
2130a8a69ddSRusty Russell 		pr_debug("Can't add buf len %i - avail = %i\n",
2140a8a69ddSRusty Russell 			 out + in, vq->num_free);
21544653eaeSRusty Russell 		/* FIXME: for historical reasons, we force a notify here if
21644653eaeSRusty Russell 		 * there are outgoing parts to the buffer.  Presumably the
21744653eaeSRusty Russell 		 * host should service the ring ASAP. */
21844653eaeSRusty Russell 		if (out)
219426e3e0aSRusty Russell 			vq->notify(&vq->vq);
2200a8a69ddSRusty Russell 		END_USE(vq);
2210a8a69ddSRusty Russell 		return -ENOSPC;
2220a8a69ddSRusty Russell 	}
2230a8a69ddSRusty Russell 
2240a8a69ddSRusty Russell 	/* We're about to use some buffers from the free list. */
2250a8a69ddSRusty Russell 	vq->num_free -= out + in;
2260a8a69ddSRusty Russell 
2270a8a69ddSRusty Russell 	head = vq->free_head;
2280a8a69ddSRusty Russell 	for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
2290a8a69ddSRusty Russell 		vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
23015f9c890SRusty Russell 		vq->vring.desc[i].addr = sg_phys(sg);
2310a8a69ddSRusty Russell 		vq->vring.desc[i].len = sg->length;
2320a8a69ddSRusty Russell 		prev = i;
2330a8a69ddSRusty Russell 		sg++;
2340a8a69ddSRusty Russell 	}
2350a8a69ddSRusty Russell 	for (; in; i = vq->vring.desc[i].next, in--) {
2360a8a69ddSRusty Russell 		vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
23715f9c890SRusty Russell 		vq->vring.desc[i].addr = sg_phys(sg);
2380a8a69ddSRusty Russell 		vq->vring.desc[i].len = sg->length;
2390a8a69ddSRusty Russell 		prev = i;
2400a8a69ddSRusty Russell 		sg++;
2410a8a69ddSRusty Russell 	}
2420a8a69ddSRusty Russell 	/* Last one doesn't continue. */
2430a8a69ddSRusty Russell 	vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
2440a8a69ddSRusty Russell 
2450a8a69ddSRusty Russell 	/* Update free pointer */
2460a8a69ddSRusty Russell 	vq->free_head = i;
2470a8a69ddSRusty Russell 
2489fa29b9dSMark McLoughlin add_head:
2490a8a69ddSRusty Russell 	/* Set token. */
2500a8a69ddSRusty Russell 	vq->data[head] = data;
2510a8a69ddSRusty Russell 
2520a8a69ddSRusty Russell 	/* Put entry in available array (but don't update avail->idx until they
2533b720b8cSRusty Russell 	 * do sync). */
254*ee7cd898SRusty Russell 	avail = (vq->vring.avail->idx & (vq->vring.num-1));
2550a8a69ddSRusty Russell 	vq->vring.avail->ring[avail] = head;
2560a8a69ddSRusty Russell 
257*ee7cd898SRusty Russell 	/* Descriptors and available array need to be set before we expose the
258*ee7cd898SRusty Russell 	 * new available array entries. */
259*ee7cd898SRusty Russell 	virtio_wmb(vq);
260*ee7cd898SRusty Russell 	vq->vring.avail->idx++;
261*ee7cd898SRusty Russell 	vq->num_added++;
262*ee7cd898SRusty Russell 
263*ee7cd898SRusty Russell 	/* This is very unlikely, but theoretically possible.  Kick
264*ee7cd898SRusty Russell 	 * just in case. */
265*ee7cd898SRusty Russell 	if (unlikely(vq->num_added == (1 << 16) - 1))
266*ee7cd898SRusty Russell 		virtqueue_kick(_vq);
267*ee7cd898SRusty Russell 
2680a8a69ddSRusty Russell 	pr_debug("Added buffer head %i to %p\n", head, vq);
2690a8a69ddSRusty Russell 	END_USE(vq);
2703c1b27d5SRusty Russell 
2713c1b27d5SRusty Russell 	return vq->num_free;
2720a8a69ddSRusty Russell }
273f96fde41SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_buf);
2740a8a69ddSRusty Russell 
2755dfc1762SRusty Russell /**
27641f0377fSRusty Russell  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
2775dfc1762SRusty Russell  * @vq: the struct virtqueue
2785dfc1762SRusty Russell  *
27941f0377fSRusty Russell  * Instead of virtqueue_kick(), you can do:
28041f0377fSRusty Russell  *	if (virtqueue_kick_prepare(vq))
28141f0377fSRusty Russell  *		virtqueue_notify(vq);
2825dfc1762SRusty Russell  *
28341f0377fSRusty Russell  * This is sometimes useful because the virtqueue_kick_prepare() needs
28441f0377fSRusty Russell  * to be serialized, but the actual virtqueue_notify() call does not.
2855dfc1762SRusty Russell  */
28641f0377fSRusty Russell bool virtqueue_kick_prepare(struct virtqueue *_vq)
2870a8a69ddSRusty Russell {
2880a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
289a5c262c5SMichael S. Tsirkin 	u16 new, old;
29041f0377fSRusty Russell 	bool needs_kick;
29141f0377fSRusty Russell 
2920a8a69ddSRusty Russell 	START_USE(vq);
2930a8a69ddSRusty Russell 	/* Descriptors and available array need to be set before we expose the
2940a8a69ddSRusty Russell 	 * new available array entries. */
2957b21e34fSRusty Russell 	virtio_wmb(vq);
2960a8a69ddSRusty Russell 
297*ee7cd898SRusty Russell 	old = vq->vring.avail->idx - vq->num_added;
298*ee7cd898SRusty Russell 	new = vq->vring.avail->idx;
2990a8a69ddSRusty Russell 	vq->num_added = 0;
3000a8a69ddSRusty Russell 
30141f0377fSRusty Russell 	if (vq->event) {
30241f0377fSRusty Russell 		needs_kick = vring_need_event(vring_avail_event(&vq->vring),
30341f0377fSRusty Russell 					      new, old);
30441f0377fSRusty Russell 	} else {
30541f0377fSRusty Russell 		needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
30641f0377fSRusty Russell 	}
3070a8a69ddSRusty Russell 	END_USE(vq);
30841f0377fSRusty Russell 	return needs_kick;
30941f0377fSRusty Russell }
31041f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
31141f0377fSRusty Russell 
31241f0377fSRusty Russell /**
31341f0377fSRusty Russell  * virtqueue_notify - second half of split virtqueue_kick call.
31441f0377fSRusty Russell  * @vq: the struct virtqueue
31541f0377fSRusty Russell  *
31641f0377fSRusty Russell  * This does not need to be serialized.
31741f0377fSRusty Russell  */
31841f0377fSRusty Russell void virtqueue_notify(struct virtqueue *_vq)
31941f0377fSRusty Russell {
32041f0377fSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
32141f0377fSRusty Russell 
32241f0377fSRusty Russell 	/* Prod other side to tell it about changes. */
32341f0377fSRusty Russell 	vq->notify(_vq);
32441f0377fSRusty Russell }
32541f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify);
32641f0377fSRusty Russell 
32741f0377fSRusty Russell /**
32841f0377fSRusty Russell  * virtqueue_kick - update after add_buf
32941f0377fSRusty Russell  * @vq: the struct virtqueue
33041f0377fSRusty Russell  *
33141f0377fSRusty Russell  * After one or more virtqueue_add_buf calls, invoke this to kick
33241f0377fSRusty Russell  * the other side.
33341f0377fSRusty Russell  *
33441f0377fSRusty Russell  * Caller must ensure we don't call this with other virtqueue
33541f0377fSRusty Russell  * operations at the same time (except where noted).
33641f0377fSRusty Russell  */
33741f0377fSRusty Russell void virtqueue_kick(struct virtqueue *vq)
33841f0377fSRusty Russell {
33941f0377fSRusty Russell 	if (virtqueue_kick_prepare(vq))
34041f0377fSRusty Russell 		virtqueue_notify(vq);
3410a8a69ddSRusty Russell }
3427c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick);
3430a8a69ddSRusty Russell 
3440a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
3450a8a69ddSRusty Russell {
3460a8a69ddSRusty Russell 	unsigned int i;
3470a8a69ddSRusty Russell 
3480a8a69ddSRusty Russell 	/* Clear data ptr. */
3490a8a69ddSRusty Russell 	vq->data[head] = NULL;
3500a8a69ddSRusty Russell 
3510a8a69ddSRusty Russell 	/* Put back on free list: find end */
3520a8a69ddSRusty Russell 	i = head;
3539fa29b9dSMark McLoughlin 
3549fa29b9dSMark McLoughlin 	/* Free the indirect table */
3559fa29b9dSMark McLoughlin 	if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
3569fa29b9dSMark McLoughlin 		kfree(phys_to_virt(vq->vring.desc[i].addr));
3579fa29b9dSMark McLoughlin 
3580a8a69ddSRusty Russell 	while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
3590a8a69ddSRusty Russell 		i = vq->vring.desc[i].next;
3600a8a69ddSRusty Russell 		vq->num_free++;
3610a8a69ddSRusty Russell 	}
3620a8a69ddSRusty Russell 
3630a8a69ddSRusty Russell 	vq->vring.desc[i].next = vq->free_head;
3640a8a69ddSRusty Russell 	vq->free_head = head;
3650a8a69ddSRusty Russell 	/* Plus final descriptor */
3660a8a69ddSRusty Russell 	vq->num_free++;
3670a8a69ddSRusty Russell }
3680a8a69ddSRusty Russell 
3690a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq)
3700a8a69ddSRusty Russell {
3710a8a69ddSRusty Russell 	return vq->last_used_idx != vq->vring.used->idx;
3720a8a69ddSRusty Russell }
3730a8a69ddSRusty Russell 
3745dfc1762SRusty Russell /**
3755dfc1762SRusty Russell  * virtqueue_get_buf - get the next used buffer
3765dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
3775dfc1762SRusty Russell  * @len: the length written into the buffer
3785dfc1762SRusty Russell  *
3795dfc1762SRusty Russell  * If the driver wrote data into the buffer, @len will be set to the
3805dfc1762SRusty Russell  * amount written.  This means you don't need to clear the buffer
3815dfc1762SRusty Russell  * beforehand to ensure there's no data leakage in the case of short
3825dfc1762SRusty Russell  * writes.
3835dfc1762SRusty Russell  *
3845dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
3855dfc1762SRusty Russell  * operations at the same time (except where noted).
3865dfc1762SRusty Russell  *
3875dfc1762SRusty Russell  * Returns NULL if there are no used buffers, or the "data" token
388f96fde41SRusty Russell  * handed to virtqueue_add_buf().
3895dfc1762SRusty Russell  */
3907c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
3910a8a69ddSRusty Russell {
3920a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
3930a8a69ddSRusty Russell 	void *ret;
3940a8a69ddSRusty Russell 	unsigned int i;
3953b720b8cSRusty Russell 	u16 last_used;
3960a8a69ddSRusty Russell 
3970a8a69ddSRusty Russell 	START_USE(vq);
3980a8a69ddSRusty Russell 
3995ef82752SRusty Russell 	if (unlikely(vq->broken)) {
4005ef82752SRusty Russell 		END_USE(vq);
4015ef82752SRusty Russell 		return NULL;
4025ef82752SRusty Russell 	}
4035ef82752SRusty Russell 
4040a8a69ddSRusty Russell 	if (!more_used(vq)) {
4050a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
4060a8a69ddSRusty Russell 		END_USE(vq);
4070a8a69ddSRusty Russell 		return NULL;
4080a8a69ddSRusty Russell 	}
4090a8a69ddSRusty Russell 
4102d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
4117b21e34fSRusty Russell 	virtio_rmb(vq);
4122d61ba95SMichael S. Tsirkin 
4133b720b8cSRusty Russell 	last_used = (vq->last_used_idx & (vq->vring.num - 1));
4143b720b8cSRusty Russell 	i = vq->vring.used->ring[last_used].id;
4153b720b8cSRusty Russell 	*len = vq->vring.used->ring[last_used].len;
4160a8a69ddSRusty Russell 
4170a8a69ddSRusty Russell 	if (unlikely(i >= vq->vring.num)) {
4180a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
4190a8a69ddSRusty Russell 		return NULL;
4200a8a69ddSRusty Russell 	}
4210a8a69ddSRusty Russell 	if (unlikely(!vq->data[i])) {
4220a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
4230a8a69ddSRusty Russell 		return NULL;
4240a8a69ddSRusty Russell 	}
4250a8a69ddSRusty Russell 
4260a8a69ddSRusty Russell 	/* detach_buf clears data, so grab it now. */
4270a8a69ddSRusty Russell 	ret = vq->data[i];
4280a8a69ddSRusty Russell 	detach_buf(vq, i);
4290a8a69ddSRusty Russell 	vq->last_used_idx++;
430a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
431a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
432a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
433a5c262c5SMichael S. Tsirkin 	if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
434a5c262c5SMichael S. Tsirkin 		vring_used_event(&vq->vring) = vq->last_used_idx;
4357b21e34fSRusty Russell 		virtio_mb(vq);
436a5c262c5SMichael S. Tsirkin 	}
437a5c262c5SMichael S. Tsirkin 
4380a8a69ddSRusty Russell 	END_USE(vq);
4390a8a69ddSRusty Russell 	return ret;
4400a8a69ddSRusty Russell }
4417c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf);
4420a8a69ddSRusty Russell 
4435dfc1762SRusty Russell /**
4445dfc1762SRusty Russell  * virtqueue_disable_cb - disable callbacks
4455dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
4465dfc1762SRusty Russell  *
4475dfc1762SRusty Russell  * Note that this is not necessarily synchronous, hence unreliable and only
4485dfc1762SRusty Russell  * useful as an optimization.
4495dfc1762SRusty Russell  *
4505dfc1762SRusty Russell  * Unlike other operations, this need not be serialized.
4515dfc1762SRusty Russell  */
4527c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq)
45318445c4dSRusty Russell {
45418445c4dSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
45518445c4dSRusty Russell 
45618445c4dSRusty Russell 	vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
45718445c4dSRusty Russell }
4587c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
45918445c4dSRusty Russell 
4605dfc1762SRusty Russell /**
4615dfc1762SRusty Russell  * virtqueue_enable_cb - restart callbacks after disable_cb.
4625dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
4635dfc1762SRusty Russell  *
4645dfc1762SRusty Russell  * This re-enables callbacks; it returns "false" if there are pending
4655dfc1762SRusty Russell  * buffers in the queue, to detect a possible race between the driver
4665dfc1762SRusty Russell  * checking for more work, and enabling callbacks.
4675dfc1762SRusty Russell  *
4685dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
4695dfc1762SRusty Russell  * operations at the same time (except where noted).
4705dfc1762SRusty Russell  */
4717c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq)
4720a8a69ddSRusty Russell {
4730a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
4740a8a69ddSRusty Russell 
4750a8a69ddSRusty Russell 	START_USE(vq);
4760a8a69ddSRusty Russell 
4770a8a69ddSRusty Russell 	/* We optimistically turn back on interrupts, then check if there was
4780a8a69ddSRusty Russell 	 * more to do. */
479a5c262c5SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
480a5c262c5SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
481a5c262c5SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
4820a8a69ddSRusty Russell 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
483a5c262c5SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx;
4847b21e34fSRusty Russell 	virtio_mb(vq);
4850a8a69ddSRusty Russell 	if (unlikely(more_used(vq))) {
4860a8a69ddSRusty Russell 		END_USE(vq);
4870a8a69ddSRusty Russell 		return false;
4880a8a69ddSRusty Russell 	}
4890a8a69ddSRusty Russell 
4900a8a69ddSRusty Russell 	END_USE(vq);
4910a8a69ddSRusty Russell 	return true;
4920a8a69ddSRusty Russell }
4937c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
4940a8a69ddSRusty Russell 
4955dfc1762SRusty Russell /**
4965dfc1762SRusty Russell  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
4975dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
4985dfc1762SRusty Russell  *
4995dfc1762SRusty Russell  * This re-enables callbacks but hints to the other side to delay
5005dfc1762SRusty Russell  * interrupts until most of the available buffers have been processed;
5015dfc1762SRusty Russell  * it returns "false" if there are many pending buffers in the queue,
5025dfc1762SRusty Russell  * to detect a possible race between the driver checking for more work,
5035dfc1762SRusty Russell  * and enabling callbacks.
5045dfc1762SRusty Russell  *
5055dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
5065dfc1762SRusty Russell  * operations at the same time (except where noted).
5075dfc1762SRusty Russell  */
5087ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
5097ab358c2SMichael S. Tsirkin {
5107ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
5117ab358c2SMichael S. Tsirkin 	u16 bufs;
5127ab358c2SMichael S. Tsirkin 
5137ab358c2SMichael S. Tsirkin 	START_USE(vq);
5147ab358c2SMichael S. Tsirkin 
5157ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
5167ab358c2SMichael S. Tsirkin 	 * more to do. */
5177ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
5187ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
5197ab358c2SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
5207ab358c2SMichael S. Tsirkin 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
5217ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
5227ab358c2SMichael S. Tsirkin 	bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
5237ab358c2SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
5247b21e34fSRusty Russell 	virtio_mb(vq);
5257ab358c2SMichael S. Tsirkin 	if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
5267ab358c2SMichael S. Tsirkin 		END_USE(vq);
5277ab358c2SMichael S. Tsirkin 		return false;
5287ab358c2SMichael S. Tsirkin 	}
5297ab358c2SMichael S. Tsirkin 
5307ab358c2SMichael S. Tsirkin 	END_USE(vq);
5317ab358c2SMichael S. Tsirkin 	return true;
5327ab358c2SMichael S. Tsirkin }
5337ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
5347ab358c2SMichael S. Tsirkin 
5355dfc1762SRusty Russell /**
5365dfc1762SRusty Russell  * virtqueue_detach_unused_buf - detach first unused buffer
5375dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5385dfc1762SRusty Russell  *
539f96fde41SRusty Russell  * Returns NULL or the "data" token handed to virtqueue_add_buf().
5405dfc1762SRusty Russell  * This is not valid on an active queue; it is useful only for device
5415dfc1762SRusty Russell  * shutdown.
5425dfc1762SRusty Russell  */
5437c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
544c021eac4SShirley Ma {
545c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
546c021eac4SShirley Ma 	unsigned int i;
547c021eac4SShirley Ma 	void *buf;
548c021eac4SShirley Ma 
549c021eac4SShirley Ma 	START_USE(vq);
550c021eac4SShirley Ma 
551c021eac4SShirley Ma 	for (i = 0; i < vq->vring.num; i++) {
552c021eac4SShirley Ma 		if (!vq->data[i])
553c021eac4SShirley Ma 			continue;
554c021eac4SShirley Ma 		/* detach_buf clears data, so grab it now. */
555c021eac4SShirley Ma 		buf = vq->data[i];
556c021eac4SShirley Ma 		detach_buf(vq, i);
557b3258ff1SAmit Shah 		vq->vring.avail->idx--;
558c021eac4SShirley Ma 		END_USE(vq);
559c021eac4SShirley Ma 		return buf;
560c021eac4SShirley Ma 	}
561c021eac4SShirley Ma 	/* That should have freed everything. */
562c021eac4SShirley Ma 	BUG_ON(vq->num_free != vq->vring.num);
563c021eac4SShirley Ma 
564c021eac4SShirley Ma 	END_USE(vq);
565c021eac4SShirley Ma 	return NULL;
566c021eac4SShirley Ma }
5677c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
568c021eac4SShirley Ma 
5690a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
5700a8a69ddSRusty Russell {
5710a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
5720a8a69ddSRusty Russell 
5730a8a69ddSRusty Russell 	if (!more_used(vq)) {
5740a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
5750a8a69ddSRusty Russell 		return IRQ_NONE;
5760a8a69ddSRusty Russell 	}
5770a8a69ddSRusty Russell 
5780a8a69ddSRusty Russell 	if (unlikely(vq->broken))
5790a8a69ddSRusty Russell 		return IRQ_HANDLED;
5800a8a69ddSRusty Russell 
5810a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
58218445c4dSRusty Russell 	if (vq->vq.callback)
58318445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
5840a8a69ddSRusty Russell 
5850a8a69ddSRusty Russell 	return IRQ_HANDLED;
5860a8a69ddSRusty Russell }
587c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
5880a8a69ddSRusty Russell 
5890a8a69ddSRusty Russell struct virtqueue *vring_new_virtqueue(unsigned int num,
59087c7d57cSRusty Russell 				      unsigned int vring_align,
5910a8a69ddSRusty Russell 				      struct virtio_device *vdev,
5927b21e34fSRusty Russell 				      bool weak_barriers,
5930a8a69ddSRusty Russell 				      void *pages,
5940a8a69ddSRusty Russell 				      void (*notify)(struct virtqueue *),
5959499f5e7SRusty Russell 				      void (*callback)(struct virtqueue *),
5969499f5e7SRusty Russell 				      const char *name)
5970a8a69ddSRusty Russell {
5980a8a69ddSRusty Russell 	struct vring_virtqueue *vq;
5990a8a69ddSRusty Russell 	unsigned int i;
6000a8a69ddSRusty Russell 
60142b36cc0SRusty Russell 	/* We assume num is a power of 2. */
60242b36cc0SRusty Russell 	if (num & (num - 1)) {
60342b36cc0SRusty Russell 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
60442b36cc0SRusty Russell 		return NULL;
60542b36cc0SRusty Russell 	}
60642b36cc0SRusty Russell 
6070a8a69ddSRusty Russell 	vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
6080a8a69ddSRusty Russell 	if (!vq)
6090a8a69ddSRusty Russell 		return NULL;
6100a8a69ddSRusty Russell 
61187c7d57cSRusty Russell 	vring_init(&vq->vring, num, pages, vring_align);
6120a8a69ddSRusty Russell 	vq->vq.callback = callback;
6130a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
6149499f5e7SRusty Russell 	vq->vq.name = name;
6150a8a69ddSRusty Russell 	vq->notify = notify;
6167b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
6170a8a69ddSRusty Russell 	vq->broken = false;
6180a8a69ddSRusty Russell 	vq->last_used_idx = 0;
6190a8a69ddSRusty Russell 	vq->num_added = 0;
6209499f5e7SRusty Russell 	list_add_tail(&vq->vq.list, &vdev->vqs);
6210a8a69ddSRusty Russell #ifdef DEBUG
6220a8a69ddSRusty Russell 	vq->in_use = false;
6230a8a69ddSRusty Russell #endif
6240a8a69ddSRusty Russell 
6259fa29b9dSMark McLoughlin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
626a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
6279fa29b9dSMark McLoughlin 
6280a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
6290a8a69ddSRusty Russell 	if (!callback)
6300a8a69ddSRusty Russell 		vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
6310a8a69ddSRusty Russell 
6320a8a69ddSRusty Russell 	/* Put everything in free lists. */
6330a8a69ddSRusty Russell 	vq->num_free = num;
6340a8a69ddSRusty Russell 	vq->free_head = 0;
6353b870624SAmit Shah 	for (i = 0; i < num-1; i++) {
6360a8a69ddSRusty Russell 		vq->vring.desc[i].next = i+1;
6373b870624SAmit Shah 		vq->data[i] = NULL;
6383b870624SAmit Shah 	}
6393b870624SAmit Shah 	vq->data[i] = NULL;
6400a8a69ddSRusty Russell 
6410a8a69ddSRusty Russell 	return &vq->vq;
6420a8a69ddSRusty Russell }
643c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
6440a8a69ddSRusty Russell 
6450a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq)
6460a8a69ddSRusty Russell {
6479499f5e7SRusty Russell 	list_del(&vq->list);
6480a8a69ddSRusty Russell 	kfree(to_vvq(vq));
6490a8a69ddSRusty Russell }
650c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
6510a8a69ddSRusty Russell 
652e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
653e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
654e34f8725SRusty Russell {
655e34f8725SRusty Russell 	unsigned int i;
656e34f8725SRusty Russell 
657e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
658e34f8725SRusty Russell 		switch (i) {
6599fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
6609fa29b9dSMark McLoughlin 			break;
661a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
662a5c262c5SMichael S. Tsirkin 			break;
663e34f8725SRusty Russell 		default:
664e34f8725SRusty Russell 			/* We don't understand this bit. */
665e34f8725SRusty Russell 			clear_bit(i, vdev->features);
666e34f8725SRusty Russell 		}
667e34f8725SRusty Russell 	}
668e34f8725SRusty Russell }
669e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
670e34f8725SRusty Russell 
6715dfc1762SRusty Russell /**
6725dfc1762SRusty Russell  * virtqueue_get_vring_size - return the size of the virtqueue's vring
6735dfc1762SRusty Russell  * @vq: the struct virtqueue containing the vring of interest.
6745dfc1762SRusty Russell  *
6755dfc1762SRusty Russell  * Returns the size of the vring.  This is mainly used for boasting to
6765dfc1762SRusty Russell  * userspace.  Unlike other operations, this need not be serialized.
6775dfc1762SRusty Russell  */
6788f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
6798f9f4668SRick Jones {
6808f9f4668SRick Jones 
6818f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
6828f9f4668SRick Jones 
6838f9f4668SRick Jones 	return vq->vring.num;
6848f9f4668SRick Jones }
6858f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
6868f9f4668SRick Jones 
687c6fd4701SRusty Russell MODULE_LICENSE("GPL");
688