xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision 41f0377f73039ca6fe97a469d1941a89cd9757f1)
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
2530a8a69ddSRusty Russell 	 * do sync).  FIXME: avoid modulus here? */
2540a8a69ddSRusty Russell 	avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
2550a8a69ddSRusty Russell 	vq->vring.avail->ring[avail] = head;
2560a8a69ddSRusty Russell 
2570a8a69ddSRusty Russell 	pr_debug("Added buffer head %i to %p\n", head, vq);
2580a8a69ddSRusty Russell 	END_USE(vq);
2593c1b27d5SRusty Russell 
2603c1b27d5SRusty Russell 	return vq->num_free;
2610a8a69ddSRusty Russell }
262f96fde41SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_buf);
2630a8a69ddSRusty Russell 
2645dfc1762SRusty Russell /**
265*41f0377fSRusty Russell  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
2665dfc1762SRusty Russell  * @vq: the struct virtqueue
2675dfc1762SRusty Russell  *
268*41f0377fSRusty Russell  * Instead of virtqueue_kick(), you can do:
269*41f0377fSRusty Russell  *	if (virtqueue_kick_prepare(vq))
270*41f0377fSRusty Russell  *		virtqueue_notify(vq);
2715dfc1762SRusty Russell  *
272*41f0377fSRusty Russell  * This is sometimes useful because the virtqueue_kick_prepare() needs
273*41f0377fSRusty Russell  * to be serialized, but the actual virtqueue_notify() call does not.
2745dfc1762SRusty Russell  */
275*41f0377fSRusty Russell bool virtqueue_kick_prepare(struct virtqueue *_vq)
2760a8a69ddSRusty Russell {
2770a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
278a5c262c5SMichael S. Tsirkin 	u16 new, old;
279*41f0377fSRusty Russell 	bool needs_kick;
280*41f0377fSRusty Russell 
2810a8a69ddSRusty Russell 	START_USE(vq);
2820a8a69ddSRusty Russell 	/* Descriptors and available array need to be set before we expose the
2830a8a69ddSRusty Russell 	 * new available array entries. */
2847b21e34fSRusty Russell 	virtio_wmb(vq);
2850a8a69ddSRusty Russell 
286a5c262c5SMichael S. Tsirkin 	old = vq->vring.avail->idx;
287a5c262c5SMichael S. Tsirkin 	new = vq->vring.avail->idx = old + vq->num_added;
2880a8a69ddSRusty Russell 	vq->num_added = 0;
2890a8a69ddSRusty Russell 
2900a8a69ddSRusty Russell 	/* Need to update avail index before checking if we should notify */
2917b21e34fSRusty Russell 	virtio_mb(vq);
2920a8a69ddSRusty Russell 
293*41f0377fSRusty Russell 	if (vq->event) {
294*41f0377fSRusty Russell 		needs_kick = vring_need_event(vring_avail_event(&vq->vring),
295*41f0377fSRusty Russell 					      new, old);
296*41f0377fSRusty Russell 	} else {
297*41f0377fSRusty Russell 		needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
298*41f0377fSRusty Russell 	}
2990a8a69ddSRusty Russell 	END_USE(vq);
300*41f0377fSRusty Russell 	return needs_kick;
301*41f0377fSRusty Russell }
302*41f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
303*41f0377fSRusty Russell 
304*41f0377fSRusty Russell /**
305*41f0377fSRusty Russell  * virtqueue_notify - second half of split virtqueue_kick call.
306*41f0377fSRusty Russell  * @vq: the struct virtqueue
307*41f0377fSRusty Russell  *
308*41f0377fSRusty Russell  * This does not need to be serialized.
309*41f0377fSRusty Russell  */
310*41f0377fSRusty Russell void virtqueue_notify(struct virtqueue *_vq)
311*41f0377fSRusty Russell {
312*41f0377fSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
313*41f0377fSRusty Russell 
314*41f0377fSRusty Russell 	/* Prod other side to tell it about changes. */
315*41f0377fSRusty Russell 	vq->notify(_vq);
316*41f0377fSRusty Russell }
317*41f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify);
318*41f0377fSRusty Russell 
319*41f0377fSRusty Russell /**
320*41f0377fSRusty Russell  * virtqueue_kick - update after add_buf
321*41f0377fSRusty Russell  * @vq: the struct virtqueue
322*41f0377fSRusty Russell  *
323*41f0377fSRusty Russell  * After one or more virtqueue_add_buf calls, invoke this to kick
324*41f0377fSRusty Russell  * the other side.
325*41f0377fSRusty Russell  *
326*41f0377fSRusty Russell  * Caller must ensure we don't call this with other virtqueue
327*41f0377fSRusty Russell  * operations at the same time (except where noted).
328*41f0377fSRusty Russell  */
329*41f0377fSRusty Russell void virtqueue_kick(struct virtqueue *vq)
330*41f0377fSRusty Russell {
331*41f0377fSRusty Russell 	if (virtqueue_kick_prepare(vq))
332*41f0377fSRusty Russell 		virtqueue_notify(vq);
3330a8a69ddSRusty Russell }
3347c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick);
3350a8a69ddSRusty Russell 
3360a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
3370a8a69ddSRusty Russell {
3380a8a69ddSRusty Russell 	unsigned int i;
3390a8a69ddSRusty Russell 
3400a8a69ddSRusty Russell 	/* Clear data ptr. */
3410a8a69ddSRusty Russell 	vq->data[head] = NULL;
3420a8a69ddSRusty Russell 
3430a8a69ddSRusty Russell 	/* Put back on free list: find end */
3440a8a69ddSRusty Russell 	i = head;
3459fa29b9dSMark McLoughlin 
3469fa29b9dSMark McLoughlin 	/* Free the indirect table */
3479fa29b9dSMark McLoughlin 	if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
3489fa29b9dSMark McLoughlin 		kfree(phys_to_virt(vq->vring.desc[i].addr));
3499fa29b9dSMark McLoughlin 
3500a8a69ddSRusty Russell 	while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
3510a8a69ddSRusty Russell 		i = vq->vring.desc[i].next;
3520a8a69ddSRusty Russell 		vq->num_free++;
3530a8a69ddSRusty Russell 	}
3540a8a69ddSRusty Russell 
3550a8a69ddSRusty Russell 	vq->vring.desc[i].next = vq->free_head;
3560a8a69ddSRusty Russell 	vq->free_head = head;
3570a8a69ddSRusty Russell 	/* Plus final descriptor */
3580a8a69ddSRusty Russell 	vq->num_free++;
3590a8a69ddSRusty Russell }
3600a8a69ddSRusty Russell 
3610a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq)
3620a8a69ddSRusty Russell {
3630a8a69ddSRusty Russell 	return vq->last_used_idx != vq->vring.used->idx;
3640a8a69ddSRusty Russell }
3650a8a69ddSRusty Russell 
3665dfc1762SRusty Russell /**
3675dfc1762SRusty Russell  * virtqueue_get_buf - get the next used buffer
3685dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
3695dfc1762SRusty Russell  * @len: the length written into the buffer
3705dfc1762SRusty Russell  *
3715dfc1762SRusty Russell  * If the driver wrote data into the buffer, @len will be set to the
3725dfc1762SRusty Russell  * amount written.  This means you don't need to clear the buffer
3735dfc1762SRusty Russell  * beforehand to ensure there's no data leakage in the case of short
3745dfc1762SRusty Russell  * writes.
3755dfc1762SRusty Russell  *
3765dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
3775dfc1762SRusty Russell  * operations at the same time (except where noted).
3785dfc1762SRusty Russell  *
3795dfc1762SRusty Russell  * Returns NULL if there are no used buffers, or the "data" token
380f96fde41SRusty Russell  * handed to virtqueue_add_buf().
3815dfc1762SRusty Russell  */
3827c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
3830a8a69ddSRusty Russell {
3840a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
3850a8a69ddSRusty Russell 	void *ret;
3860a8a69ddSRusty Russell 	unsigned int i;
3870a8a69ddSRusty Russell 
3880a8a69ddSRusty Russell 	START_USE(vq);
3890a8a69ddSRusty Russell 
3905ef82752SRusty Russell 	if (unlikely(vq->broken)) {
3915ef82752SRusty Russell 		END_USE(vq);
3925ef82752SRusty Russell 		return NULL;
3935ef82752SRusty Russell 	}
3945ef82752SRusty Russell 
3950a8a69ddSRusty Russell 	if (!more_used(vq)) {
3960a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
3970a8a69ddSRusty Russell 		END_USE(vq);
3980a8a69ddSRusty Russell 		return NULL;
3990a8a69ddSRusty Russell 	}
4000a8a69ddSRusty Russell 
4012d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
4027b21e34fSRusty Russell 	virtio_rmb(vq);
4032d61ba95SMichael S. Tsirkin 
4040a8a69ddSRusty Russell 	i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
4050a8a69ddSRusty Russell 	*len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
4060a8a69ddSRusty Russell 
4070a8a69ddSRusty Russell 	if (unlikely(i >= vq->vring.num)) {
4080a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
4090a8a69ddSRusty Russell 		return NULL;
4100a8a69ddSRusty Russell 	}
4110a8a69ddSRusty Russell 	if (unlikely(!vq->data[i])) {
4120a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
4130a8a69ddSRusty Russell 		return NULL;
4140a8a69ddSRusty Russell 	}
4150a8a69ddSRusty Russell 
4160a8a69ddSRusty Russell 	/* detach_buf clears data, so grab it now. */
4170a8a69ddSRusty Russell 	ret = vq->data[i];
4180a8a69ddSRusty Russell 	detach_buf(vq, i);
4190a8a69ddSRusty Russell 	vq->last_used_idx++;
420a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
421a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
422a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
423a5c262c5SMichael S. Tsirkin 	if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
424a5c262c5SMichael S. Tsirkin 		vring_used_event(&vq->vring) = vq->last_used_idx;
4257b21e34fSRusty Russell 		virtio_mb(vq);
426a5c262c5SMichael S. Tsirkin 	}
427a5c262c5SMichael S. Tsirkin 
4280a8a69ddSRusty Russell 	END_USE(vq);
4290a8a69ddSRusty Russell 	return ret;
4300a8a69ddSRusty Russell }
4317c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf);
4320a8a69ddSRusty Russell 
4335dfc1762SRusty Russell /**
4345dfc1762SRusty Russell  * virtqueue_disable_cb - disable callbacks
4355dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
4365dfc1762SRusty Russell  *
4375dfc1762SRusty Russell  * Note that this is not necessarily synchronous, hence unreliable and only
4385dfc1762SRusty Russell  * useful as an optimization.
4395dfc1762SRusty Russell  *
4405dfc1762SRusty Russell  * Unlike other operations, this need not be serialized.
4415dfc1762SRusty Russell  */
4427c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq)
44318445c4dSRusty Russell {
44418445c4dSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
44518445c4dSRusty Russell 
44618445c4dSRusty Russell 	vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
44718445c4dSRusty Russell }
4487c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
44918445c4dSRusty Russell 
4505dfc1762SRusty Russell /**
4515dfc1762SRusty Russell  * virtqueue_enable_cb - restart callbacks after disable_cb.
4525dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
4535dfc1762SRusty Russell  *
4545dfc1762SRusty Russell  * This re-enables callbacks; it returns "false" if there are pending
4555dfc1762SRusty Russell  * buffers in the queue, to detect a possible race between the driver
4565dfc1762SRusty Russell  * checking for more work, and enabling callbacks.
4575dfc1762SRusty Russell  *
4585dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
4595dfc1762SRusty Russell  * operations at the same time (except where noted).
4605dfc1762SRusty Russell  */
4617c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq)
4620a8a69ddSRusty Russell {
4630a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
4640a8a69ddSRusty Russell 
4650a8a69ddSRusty Russell 	START_USE(vq);
4660a8a69ddSRusty Russell 
4670a8a69ddSRusty Russell 	/* We optimistically turn back on interrupts, then check if there was
4680a8a69ddSRusty Russell 	 * more to do. */
469a5c262c5SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
470a5c262c5SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
471a5c262c5SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
4720a8a69ddSRusty Russell 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
473a5c262c5SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx;
4747b21e34fSRusty Russell 	virtio_mb(vq);
4750a8a69ddSRusty Russell 	if (unlikely(more_used(vq))) {
4760a8a69ddSRusty Russell 		END_USE(vq);
4770a8a69ddSRusty Russell 		return false;
4780a8a69ddSRusty Russell 	}
4790a8a69ddSRusty Russell 
4800a8a69ddSRusty Russell 	END_USE(vq);
4810a8a69ddSRusty Russell 	return true;
4820a8a69ddSRusty Russell }
4837c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
4840a8a69ddSRusty Russell 
4855dfc1762SRusty Russell /**
4865dfc1762SRusty Russell  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
4875dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
4885dfc1762SRusty Russell  *
4895dfc1762SRusty Russell  * This re-enables callbacks but hints to the other side to delay
4905dfc1762SRusty Russell  * interrupts until most of the available buffers have been processed;
4915dfc1762SRusty Russell  * it returns "false" if there are many pending buffers in the queue,
4925dfc1762SRusty Russell  * to detect a possible race between the driver checking for more work,
4935dfc1762SRusty Russell  * and enabling callbacks.
4945dfc1762SRusty Russell  *
4955dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
4965dfc1762SRusty Russell  * operations at the same time (except where noted).
4975dfc1762SRusty Russell  */
4987ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
4997ab358c2SMichael S. Tsirkin {
5007ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
5017ab358c2SMichael S. Tsirkin 	u16 bufs;
5027ab358c2SMichael S. Tsirkin 
5037ab358c2SMichael S. Tsirkin 	START_USE(vq);
5047ab358c2SMichael S. Tsirkin 
5057ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
5067ab358c2SMichael S. Tsirkin 	 * more to do. */
5077ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
5087ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
5097ab358c2SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
5107ab358c2SMichael S. Tsirkin 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
5117ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
5127ab358c2SMichael S. Tsirkin 	bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
5137ab358c2SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
5147b21e34fSRusty Russell 	virtio_mb(vq);
5157ab358c2SMichael S. Tsirkin 	if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
5167ab358c2SMichael S. Tsirkin 		END_USE(vq);
5177ab358c2SMichael S. Tsirkin 		return false;
5187ab358c2SMichael S. Tsirkin 	}
5197ab358c2SMichael S. Tsirkin 
5207ab358c2SMichael S. Tsirkin 	END_USE(vq);
5217ab358c2SMichael S. Tsirkin 	return true;
5227ab358c2SMichael S. Tsirkin }
5237ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
5247ab358c2SMichael S. Tsirkin 
5255dfc1762SRusty Russell /**
5265dfc1762SRusty Russell  * virtqueue_detach_unused_buf - detach first unused buffer
5275dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5285dfc1762SRusty Russell  *
529f96fde41SRusty Russell  * Returns NULL or the "data" token handed to virtqueue_add_buf().
5305dfc1762SRusty Russell  * This is not valid on an active queue; it is useful only for device
5315dfc1762SRusty Russell  * shutdown.
5325dfc1762SRusty Russell  */
5337c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
534c021eac4SShirley Ma {
535c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
536c021eac4SShirley Ma 	unsigned int i;
537c021eac4SShirley Ma 	void *buf;
538c021eac4SShirley Ma 
539c021eac4SShirley Ma 	START_USE(vq);
540c021eac4SShirley Ma 
541c021eac4SShirley Ma 	for (i = 0; i < vq->vring.num; i++) {
542c021eac4SShirley Ma 		if (!vq->data[i])
543c021eac4SShirley Ma 			continue;
544c021eac4SShirley Ma 		/* detach_buf clears data, so grab it now. */
545c021eac4SShirley Ma 		buf = vq->data[i];
546c021eac4SShirley Ma 		detach_buf(vq, i);
547b3258ff1SAmit Shah 		vq->vring.avail->idx--;
548c021eac4SShirley Ma 		END_USE(vq);
549c021eac4SShirley Ma 		return buf;
550c021eac4SShirley Ma 	}
551c021eac4SShirley Ma 	/* That should have freed everything. */
552c021eac4SShirley Ma 	BUG_ON(vq->num_free != vq->vring.num);
553c021eac4SShirley Ma 
554c021eac4SShirley Ma 	END_USE(vq);
555c021eac4SShirley Ma 	return NULL;
556c021eac4SShirley Ma }
5577c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
558c021eac4SShirley Ma 
5590a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
5600a8a69ddSRusty Russell {
5610a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
5620a8a69ddSRusty Russell 
5630a8a69ddSRusty Russell 	if (!more_used(vq)) {
5640a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
5650a8a69ddSRusty Russell 		return IRQ_NONE;
5660a8a69ddSRusty Russell 	}
5670a8a69ddSRusty Russell 
5680a8a69ddSRusty Russell 	if (unlikely(vq->broken))
5690a8a69ddSRusty Russell 		return IRQ_HANDLED;
5700a8a69ddSRusty Russell 
5710a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
57218445c4dSRusty Russell 	if (vq->vq.callback)
57318445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
5740a8a69ddSRusty Russell 
5750a8a69ddSRusty Russell 	return IRQ_HANDLED;
5760a8a69ddSRusty Russell }
577c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
5780a8a69ddSRusty Russell 
5790a8a69ddSRusty Russell struct virtqueue *vring_new_virtqueue(unsigned int num,
58087c7d57cSRusty Russell 				      unsigned int vring_align,
5810a8a69ddSRusty Russell 				      struct virtio_device *vdev,
5827b21e34fSRusty Russell 				      bool weak_barriers,
5830a8a69ddSRusty Russell 				      void *pages,
5840a8a69ddSRusty Russell 				      void (*notify)(struct virtqueue *),
5859499f5e7SRusty Russell 				      void (*callback)(struct virtqueue *),
5869499f5e7SRusty Russell 				      const char *name)
5870a8a69ddSRusty Russell {
5880a8a69ddSRusty Russell 	struct vring_virtqueue *vq;
5890a8a69ddSRusty Russell 	unsigned int i;
5900a8a69ddSRusty Russell 
59142b36cc0SRusty Russell 	/* We assume num is a power of 2. */
59242b36cc0SRusty Russell 	if (num & (num - 1)) {
59342b36cc0SRusty Russell 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
59442b36cc0SRusty Russell 		return NULL;
59542b36cc0SRusty Russell 	}
59642b36cc0SRusty Russell 
5970a8a69ddSRusty Russell 	vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
5980a8a69ddSRusty Russell 	if (!vq)
5990a8a69ddSRusty Russell 		return NULL;
6000a8a69ddSRusty Russell 
60187c7d57cSRusty Russell 	vring_init(&vq->vring, num, pages, vring_align);
6020a8a69ddSRusty Russell 	vq->vq.callback = callback;
6030a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
6049499f5e7SRusty Russell 	vq->vq.name = name;
6050a8a69ddSRusty Russell 	vq->notify = notify;
6067b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
6070a8a69ddSRusty Russell 	vq->broken = false;
6080a8a69ddSRusty Russell 	vq->last_used_idx = 0;
6090a8a69ddSRusty Russell 	vq->num_added = 0;
6109499f5e7SRusty Russell 	list_add_tail(&vq->vq.list, &vdev->vqs);
6110a8a69ddSRusty Russell #ifdef DEBUG
6120a8a69ddSRusty Russell 	vq->in_use = false;
6130a8a69ddSRusty Russell #endif
6140a8a69ddSRusty Russell 
6159fa29b9dSMark McLoughlin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
616a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
6179fa29b9dSMark McLoughlin 
6180a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
6190a8a69ddSRusty Russell 	if (!callback)
6200a8a69ddSRusty Russell 		vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
6210a8a69ddSRusty Russell 
6220a8a69ddSRusty Russell 	/* Put everything in free lists. */
6230a8a69ddSRusty Russell 	vq->num_free = num;
6240a8a69ddSRusty Russell 	vq->free_head = 0;
6253b870624SAmit Shah 	for (i = 0; i < num-1; i++) {
6260a8a69ddSRusty Russell 		vq->vring.desc[i].next = i+1;
6273b870624SAmit Shah 		vq->data[i] = NULL;
6283b870624SAmit Shah 	}
6293b870624SAmit Shah 	vq->data[i] = NULL;
6300a8a69ddSRusty Russell 
6310a8a69ddSRusty Russell 	return &vq->vq;
6320a8a69ddSRusty Russell }
633c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
6340a8a69ddSRusty Russell 
6350a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq)
6360a8a69ddSRusty Russell {
6379499f5e7SRusty Russell 	list_del(&vq->list);
6380a8a69ddSRusty Russell 	kfree(to_vvq(vq));
6390a8a69ddSRusty Russell }
640c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
6410a8a69ddSRusty Russell 
642e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
643e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
644e34f8725SRusty Russell {
645e34f8725SRusty Russell 	unsigned int i;
646e34f8725SRusty Russell 
647e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
648e34f8725SRusty Russell 		switch (i) {
6499fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
6509fa29b9dSMark McLoughlin 			break;
651a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
652a5c262c5SMichael S. Tsirkin 			break;
653e34f8725SRusty Russell 		default:
654e34f8725SRusty Russell 			/* We don't understand this bit. */
655e34f8725SRusty Russell 			clear_bit(i, vdev->features);
656e34f8725SRusty Russell 		}
657e34f8725SRusty Russell 	}
658e34f8725SRusty Russell }
659e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
660e34f8725SRusty Russell 
6615dfc1762SRusty Russell /**
6625dfc1762SRusty Russell  * virtqueue_get_vring_size - return the size of the virtqueue's vring
6635dfc1762SRusty Russell  * @vq: the struct virtqueue containing the vring of interest.
6645dfc1762SRusty Russell  *
6655dfc1762SRusty Russell  * Returns the size of the vring.  This is mainly used for boasting to
6665dfc1762SRusty Russell  * userspace.  Unlike other operations, this need not be serialized.
6675dfc1762SRusty Russell  */
6688f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
6698f9f4668SRick Jones {
6708f9f4668SRick Jones 
6718f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
6728f9f4668SRick Jones 
6738f9f4668SRick Jones 	return vq->vring.num;
6748f9f4668SRick Jones }
6758f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
6768f9f4668SRick Jones 
677c6fd4701SRusty Russell MODULE_LICENSE("GPL");
678