xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision 7b21e34fd1c272e3a8c3846168f2f6287a4cd72b)
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
31*7b21e34fSRusty Russell  * through relaxed memory I/O windows (which virtio-pci does not use). */
32*7b21e34fSRusty Russell #define virtio_mb(vq) \
33*7b21e34fSRusty Russell 	do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0)
34*7b21e34fSRusty Russell #define virtio_rmb(vq) \
35*7b21e34fSRusty Russell 	do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
36*7b21e34fSRusty Russell #define virtio_wmb(vq) \
37*7b21e34fSRusty 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. */
42*7b21e34fSRusty Russell #define virtio_mb(vq) mb()
43*7b21e34fSRusty Russell #define virtio_rmb(vq) rmb()
44*7b21e34fSRusty 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 
83*7b21e34fSRusty Russell 	/* Can we use weak barriers? */
84*7b21e34fSRusty Russell 	bool weak_barriers;
85*7b21e34fSRusty 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 
169bbd603efSMichael S. Tsirkin int virtqueue_add_buf_gfp(struct virtqueue *_vq,
1700a8a69ddSRusty Russell 			  struct scatterlist sg[],
1710a8a69ddSRusty Russell 			  unsigned int out,
1720a8a69ddSRusty Russell 			  unsigned int in,
173bbd603efSMichael S. Tsirkin 			  void *data,
174bbd603efSMichael S. Tsirkin 			  gfp_t gfp)
1750a8a69ddSRusty Russell {
1760a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
1771fe9b6feSMichael S. Tsirkin 	unsigned int i, avail, uninitialized_var(prev);
1781fe9b6feSMichael S. Tsirkin 	int head;
1790a8a69ddSRusty Russell 
1809fa29b9dSMark McLoughlin 	START_USE(vq);
1819fa29b9dSMark McLoughlin 
1820a8a69ddSRusty Russell 	BUG_ON(data == NULL);
1839fa29b9dSMark McLoughlin 
1849fa29b9dSMark McLoughlin 	/* If the host supports indirect descriptor tables, and we have multiple
1859fa29b9dSMark McLoughlin 	 * buffers, then go indirect. FIXME: tune this threshold */
1869fa29b9dSMark McLoughlin 	if (vq->indirect && (out + in) > 1 && vq->num_free) {
187bbd603efSMichael S. Tsirkin 		head = vring_add_indirect(vq, sg, out, in, gfp);
1881fe9b6feSMichael S. Tsirkin 		if (likely(head >= 0))
1899fa29b9dSMark McLoughlin 			goto add_head;
1909fa29b9dSMark McLoughlin 	}
1919fa29b9dSMark McLoughlin 
1920a8a69ddSRusty Russell 	BUG_ON(out + in > vq->vring.num);
1930a8a69ddSRusty Russell 	BUG_ON(out + in == 0);
1940a8a69ddSRusty Russell 
1950a8a69ddSRusty Russell 	if (vq->num_free < out + in) {
1960a8a69ddSRusty Russell 		pr_debug("Can't add buf len %i - avail = %i\n",
1970a8a69ddSRusty Russell 			 out + in, vq->num_free);
19844653eaeSRusty Russell 		/* FIXME: for historical reasons, we force a notify here if
19944653eaeSRusty Russell 		 * there are outgoing parts to the buffer.  Presumably the
20044653eaeSRusty Russell 		 * host should service the ring ASAP. */
20144653eaeSRusty Russell 		if (out)
202426e3e0aSRusty Russell 			vq->notify(&vq->vq);
2030a8a69ddSRusty Russell 		END_USE(vq);
2040a8a69ddSRusty Russell 		return -ENOSPC;
2050a8a69ddSRusty Russell 	}
2060a8a69ddSRusty Russell 
2070a8a69ddSRusty Russell 	/* We're about to use some buffers from the free list. */
2080a8a69ddSRusty Russell 	vq->num_free -= out + in;
2090a8a69ddSRusty Russell 
2100a8a69ddSRusty Russell 	head = vq->free_head;
2110a8a69ddSRusty Russell 	for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
2120a8a69ddSRusty Russell 		vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
21315f9c890SRusty Russell 		vq->vring.desc[i].addr = sg_phys(sg);
2140a8a69ddSRusty Russell 		vq->vring.desc[i].len = sg->length;
2150a8a69ddSRusty Russell 		prev = i;
2160a8a69ddSRusty Russell 		sg++;
2170a8a69ddSRusty Russell 	}
2180a8a69ddSRusty Russell 	for (; in; i = vq->vring.desc[i].next, in--) {
2190a8a69ddSRusty Russell 		vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
22015f9c890SRusty Russell 		vq->vring.desc[i].addr = sg_phys(sg);
2210a8a69ddSRusty Russell 		vq->vring.desc[i].len = sg->length;
2220a8a69ddSRusty Russell 		prev = i;
2230a8a69ddSRusty Russell 		sg++;
2240a8a69ddSRusty Russell 	}
2250a8a69ddSRusty Russell 	/* Last one doesn't continue. */
2260a8a69ddSRusty Russell 	vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
2270a8a69ddSRusty Russell 
2280a8a69ddSRusty Russell 	/* Update free pointer */
2290a8a69ddSRusty Russell 	vq->free_head = i;
2300a8a69ddSRusty Russell 
2319fa29b9dSMark McLoughlin add_head:
2320a8a69ddSRusty Russell 	/* Set token. */
2330a8a69ddSRusty Russell 	vq->data[head] = data;
2340a8a69ddSRusty Russell 
2350a8a69ddSRusty Russell 	/* Put entry in available array (but don't update avail->idx until they
2360a8a69ddSRusty Russell 	 * do sync).  FIXME: avoid modulus here? */
2370a8a69ddSRusty Russell 	avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
2380a8a69ddSRusty Russell 	vq->vring.avail->ring[avail] = head;
2390a8a69ddSRusty Russell 
2400a8a69ddSRusty Russell 	pr_debug("Added buffer head %i to %p\n", head, vq);
2410a8a69ddSRusty Russell 	END_USE(vq);
2423c1b27d5SRusty Russell 
2433c1b27d5SRusty Russell 	return vq->num_free;
2440a8a69ddSRusty Russell }
245bbd603efSMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp);
2460a8a69ddSRusty Russell 
2477c5e9ed0SMichael S. Tsirkin void virtqueue_kick(struct virtqueue *_vq)
2480a8a69ddSRusty Russell {
2490a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
250a5c262c5SMichael S. Tsirkin 	u16 new, old;
2510a8a69ddSRusty Russell 	START_USE(vq);
2520a8a69ddSRusty Russell 	/* Descriptors and available array need to be set before we expose the
2530a8a69ddSRusty Russell 	 * new available array entries. */
254*7b21e34fSRusty Russell 	virtio_wmb(vq);
2550a8a69ddSRusty Russell 
256a5c262c5SMichael S. Tsirkin 	old = vq->vring.avail->idx;
257a5c262c5SMichael S. Tsirkin 	new = vq->vring.avail->idx = old + vq->num_added;
2580a8a69ddSRusty Russell 	vq->num_added = 0;
2590a8a69ddSRusty Russell 
2600a8a69ddSRusty Russell 	/* Need to update avail index before checking if we should notify */
261*7b21e34fSRusty Russell 	virtio_mb(vq);
2620a8a69ddSRusty Russell 
263a5c262c5SMichael S. Tsirkin 	if (vq->event ?
264a5c262c5SMichael S. Tsirkin 	    vring_need_event(vring_avail_event(&vq->vring), new, old) :
265a5c262c5SMichael S. Tsirkin 	    !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
2660a8a69ddSRusty Russell 		/* Prod other side to tell it about changes. */
2670a8a69ddSRusty Russell 		vq->notify(&vq->vq);
2680a8a69ddSRusty Russell 
2690a8a69ddSRusty Russell 	END_USE(vq);
2700a8a69ddSRusty Russell }
2717c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick);
2720a8a69ddSRusty Russell 
2730a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
2740a8a69ddSRusty Russell {
2750a8a69ddSRusty Russell 	unsigned int i;
2760a8a69ddSRusty Russell 
2770a8a69ddSRusty Russell 	/* Clear data ptr. */
2780a8a69ddSRusty Russell 	vq->data[head] = NULL;
2790a8a69ddSRusty Russell 
2800a8a69ddSRusty Russell 	/* Put back on free list: find end */
2810a8a69ddSRusty Russell 	i = head;
2829fa29b9dSMark McLoughlin 
2839fa29b9dSMark McLoughlin 	/* Free the indirect table */
2849fa29b9dSMark McLoughlin 	if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
2859fa29b9dSMark McLoughlin 		kfree(phys_to_virt(vq->vring.desc[i].addr));
2869fa29b9dSMark McLoughlin 
2870a8a69ddSRusty Russell 	while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
2880a8a69ddSRusty Russell 		i = vq->vring.desc[i].next;
2890a8a69ddSRusty Russell 		vq->num_free++;
2900a8a69ddSRusty Russell 	}
2910a8a69ddSRusty Russell 
2920a8a69ddSRusty Russell 	vq->vring.desc[i].next = vq->free_head;
2930a8a69ddSRusty Russell 	vq->free_head = head;
2940a8a69ddSRusty Russell 	/* Plus final descriptor */
2950a8a69ddSRusty Russell 	vq->num_free++;
2960a8a69ddSRusty Russell }
2970a8a69ddSRusty Russell 
2980a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq)
2990a8a69ddSRusty Russell {
3000a8a69ddSRusty Russell 	return vq->last_used_idx != vq->vring.used->idx;
3010a8a69ddSRusty Russell }
3020a8a69ddSRusty Russell 
3037c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
3040a8a69ddSRusty Russell {
3050a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
3060a8a69ddSRusty Russell 	void *ret;
3070a8a69ddSRusty Russell 	unsigned int i;
3080a8a69ddSRusty Russell 
3090a8a69ddSRusty Russell 	START_USE(vq);
3100a8a69ddSRusty Russell 
3115ef82752SRusty Russell 	if (unlikely(vq->broken)) {
3125ef82752SRusty Russell 		END_USE(vq);
3135ef82752SRusty Russell 		return NULL;
3145ef82752SRusty Russell 	}
3155ef82752SRusty Russell 
3160a8a69ddSRusty Russell 	if (!more_used(vq)) {
3170a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
3180a8a69ddSRusty Russell 		END_USE(vq);
3190a8a69ddSRusty Russell 		return NULL;
3200a8a69ddSRusty Russell 	}
3210a8a69ddSRusty Russell 
3222d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
323*7b21e34fSRusty Russell 	virtio_rmb(vq);
3242d61ba95SMichael S. Tsirkin 
3250a8a69ddSRusty Russell 	i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
3260a8a69ddSRusty Russell 	*len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
3270a8a69ddSRusty Russell 
3280a8a69ddSRusty Russell 	if (unlikely(i >= vq->vring.num)) {
3290a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
3300a8a69ddSRusty Russell 		return NULL;
3310a8a69ddSRusty Russell 	}
3320a8a69ddSRusty Russell 	if (unlikely(!vq->data[i])) {
3330a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
3340a8a69ddSRusty Russell 		return NULL;
3350a8a69ddSRusty Russell 	}
3360a8a69ddSRusty Russell 
3370a8a69ddSRusty Russell 	/* detach_buf clears data, so grab it now. */
3380a8a69ddSRusty Russell 	ret = vq->data[i];
3390a8a69ddSRusty Russell 	detach_buf(vq, i);
3400a8a69ddSRusty Russell 	vq->last_used_idx++;
341a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
342a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
343a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
344a5c262c5SMichael S. Tsirkin 	if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
345a5c262c5SMichael S. Tsirkin 		vring_used_event(&vq->vring) = vq->last_used_idx;
346*7b21e34fSRusty Russell 		virtio_mb(vq);
347a5c262c5SMichael S. Tsirkin 	}
348a5c262c5SMichael S. Tsirkin 
3490a8a69ddSRusty Russell 	END_USE(vq);
3500a8a69ddSRusty Russell 	return ret;
3510a8a69ddSRusty Russell }
3527c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf);
3530a8a69ddSRusty Russell 
3547c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq)
35518445c4dSRusty Russell {
35618445c4dSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
35718445c4dSRusty Russell 
35818445c4dSRusty Russell 	vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
35918445c4dSRusty Russell }
3607c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
36118445c4dSRusty Russell 
3627c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq)
3630a8a69ddSRusty Russell {
3640a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
3650a8a69ddSRusty Russell 
3660a8a69ddSRusty Russell 	START_USE(vq);
3670a8a69ddSRusty Russell 
3680a8a69ddSRusty Russell 	/* We optimistically turn back on interrupts, then check if there was
3690a8a69ddSRusty Russell 	 * more to do. */
370a5c262c5SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
371a5c262c5SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
372a5c262c5SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
3730a8a69ddSRusty Russell 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
374a5c262c5SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx;
375*7b21e34fSRusty Russell 	virtio_mb(vq);
3760a8a69ddSRusty Russell 	if (unlikely(more_used(vq))) {
3770a8a69ddSRusty Russell 		END_USE(vq);
3780a8a69ddSRusty Russell 		return false;
3790a8a69ddSRusty Russell 	}
3800a8a69ddSRusty Russell 
3810a8a69ddSRusty Russell 	END_USE(vq);
3820a8a69ddSRusty Russell 	return true;
3830a8a69ddSRusty Russell }
3847c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
3850a8a69ddSRusty Russell 
3867ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
3877ab358c2SMichael S. Tsirkin {
3887ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
3897ab358c2SMichael S. Tsirkin 	u16 bufs;
3907ab358c2SMichael S. Tsirkin 
3917ab358c2SMichael S. Tsirkin 	START_USE(vq);
3927ab358c2SMichael S. Tsirkin 
3937ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
3947ab358c2SMichael S. Tsirkin 	 * more to do. */
3957ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
3967ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
3977ab358c2SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
3987ab358c2SMichael S. Tsirkin 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
3997ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
4007ab358c2SMichael S. Tsirkin 	bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
4017ab358c2SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
402*7b21e34fSRusty Russell 	virtio_mb(vq);
4037ab358c2SMichael S. Tsirkin 	if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
4047ab358c2SMichael S. Tsirkin 		END_USE(vq);
4057ab358c2SMichael S. Tsirkin 		return false;
4067ab358c2SMichael S. Tsirkin 	}
4077ab358c2SMichael S. Tsirkin 
4087ab358c2SMichael S. Tsirkin 	END_USE(vq);
4097ab358c2SMichael S. Tsirkin 	return true;
4107ab358c2SMichael S. Tsirkin }
4117ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
4127ab358c2SMichael S. Tsirkin 
4137c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
414c021eac4SShirley Ma {
415c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
416c021eac4SShirley Ma 	unsigned int i;
417c021eac4SShirley Ma 	void *buf;
418c021eac4SShirley Ma 
419c021eac4SShirley Ma 	START_USE(vq);
420c021eac4SShirley Ma 
421c021eac4SShirley Ma 	for (i = 0; i < vq->vring.num; i++) {
422c021eac4SShirley Ma 		if (!vq->data[i])
423c021eac4SShirley Ma 			continue;
424c021eac4SShirley Ma 		/* detach_buf clears data, so grab it now. */
425c021eac4SShirley Ma 		buf = vq->data[i];
426c021eac4SShirley Ma 		detach_buf(vq, i);
427b3258ff1SAmit Shah 		vq->vring.avail->idx--;
428c021eac4SShirley Ma 		END_USE(vq);
429c021eac4SShirley Ma 		return buf;
430c021eac4SShirley Ma 	}
431c021eac4SShirley Ma 	/* That should have freed everything. */
432c021eac4SShirley Ma 	BUG_ON(vq->num_free != vq->vring.num);
433c021eac4SShirley Ma 
434c021eac4SShirley Ma 	END_USE(vq);
435c021eac4SShirley Ma 	return NULL;
436c021eac4SShirley Ma }
4377c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
438c021eac4SShirley Ma 
4390a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
4400a8a69ddSRusty Russell {
4410a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
4420a8a69ddSRusty Russell 
4430a8a69ddSRusty Russell 	if (!more_used(vq)) {
4440a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
4450a8a69ddSRusty Russell 		return IRQ_NONE;
4460a8a69ddSRusty Russell 	}
4470a8a69ddSRusty Russell 
4480a8a69ddSRusty Russell 	if (unlikely(vq->broken))
4490a8a69ddSRusty Russell 		return IRQ_HANDLED;
4500a8a69ddSRusty Russell 
4510a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
45218445c4dSRusty Russell 	if (vq->vq.callback)
45318445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
4540a8a69ddSRusty Russell 
4550a8a69ddSRusty Russell 	return IRQ_HANDLED;
4560a8a69ddSRusty Russell }
457c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
4580a8a69ddSRusty Russell 
4590a8a69ddSRusty Russell struct virtqueue *vring_new_virtqueue(unsigned int num,
46087c7d57cSRusty Russell 				      unsigned int vring_align,
4610a8a69ddSRusty Russell 				      struct virtio_device *vdev,
462*7b21e34fSRusty Russell 				      bool weak_barriers,
4630a8a69ddSRusty Russell 				      void *pages,
4640a8a69ddSRusty Russell 				      void (*notify)(struct virtqueue *),
4659499f5e7SRusty Russell 				      void (*callback)(struct virtqueue *),
4669499f5e7SRusty Russell 				      const char *name)
4670a8a69ddSRusty Russell {
4680a8a69ddSRusty Russell 	struct vring_virtqueue *vq;
4690a8a69ddSRusty Russell 	unsigned int i;
4700a8a69ddSRusty Russell 
47142b36cc0SRusty Russell 	/* We assume num is a power of 2. */
47242b36cc0SRusty Russell 	if (num & (num - 1)) {
47342b36cc0SRusty Russell 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
47442b36cc0SRusty Russell 		return NULL;
47542b36cc0SRusty Russell 	}
47642b36cc0SRusty Russell 
4770a8a69ddSRusty Russell 	vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
4780a8a69ddSRusty Russell 	if (!vq)
4790a8a69ddSRusty Russell 		return NULL;
4800a8a69ddSRusty Russell 
48187c7d57cSRusty Russell 	vring_init(&vq->vring, num, pages, vring_align);
4820a8a69ddSRusty Russell 	vq->vq.callback = callback;
4830a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
4849499f5e7SRusty Russell 	vq->vq.name = name;
4850a8a69ddSRusty Russell 	vq->notify = notify;
486*7b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
4870a8a69ddSRusty Russell 	vq->broken = false;
4880a8a69ddSRusty Russell 	vq->last_used_idx = 0;
4890a8a69ddSRusty Russell 	vq->num_added = 0;
4909499f5e7SRusty Russell 	list_add_tail(&vq->vq.list, &vdev->vqs);
4910a8a69ddSRusty Russell #ifdef DEBUG
4920a8a69ddSRusty Russell 	vq->in_use = false;
4930a8a69ddSRusty Russell #endif
4940a8a69ddSRusty Russell 
4959fa29b9dSMark McLoughlin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
496a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
4979fa29b9dSMark McLoughlin 
4980a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
4990a8a69ddSRusty Russell 	if (!callback)
5000a8a69ddSRusty Russell 		vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
5010a8a69ddSRusty Russell 
5020a8a69ddSRusty Russell 	/* Put everything in free lists. */
5030a8a69ddSRusty Russell 	vq->num_free = num;
5040a8a69ddSRusty Russell 	vq->free_head = 0;
5053b870624SAmit Shah 	for (i = 0; i < num-1; i++) {
5060a8a69ddSRusty Russell 		vq->vring.desc[i].next = i+1;
5073b870624SAmit Shah 		vq->data[i] = NULL;
5083b870624SAmit Shah 	}
5093b870624SAmit Shah 	vq->data[i] = NULL;
5100a8a69ddSRusty Russell 
5110a8a69ddSRusty Russell 	return &vq->vq;
5120a8a69ddSRusty Russell }
513c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
5140a8a69ddSRusty Russell 
5150a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq)
5160a8a69ddSRusty Russell {
5179499f5e7SRusty Russell 	list_del(&vq->list);
5180a8a69ddSRusty Russell 	kfree(to_vvq(vq));
5190a8a69ddSRusty Russell }
520c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
5210a8a69ddSRusty Russell 
522e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
523e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
524e34f8725SRusty Russell {
525e34f8725SRusty Russell 	unsigned int i;
526e34f8725SRusty Russell 
527e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
528e34f8725SRusty Russell 		switch (i) {
5299fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
5309fa29b9dSMark McLoughlin 			break;
531a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
532a5c262c5SMichael S. Tsirkin 			break;
533e34f8725SRusty Russell 		default:
534e34f8725SRusty Russell 			/* We don't understand this bit. */
535e34f8725SRusty Russell 			clear_bit(i, vdev->features);
536e34f8725SRusty Russell 		}
537e34f8725SRusty Russell 	}
538e34f8725SRusty Russell }
539e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
540e34f8725SRusty Russell 
5418f9f4668SRick Jones /* return the size of the vring within the virtqueue */
5428f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
5438f9f4668SRick Jones {
5448f9f4668SRick Jones 
5458f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
5468f9f4668SRick Jones 
5478f9f4668SRick Jones 	return vq->vring.num;
5488f9f4668SRick Jones }
5498f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
5508f9f4668SRick Jones 
551c6fd4701SRusty Russell MODULE_LICENSE("GPL");
552