xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision 82107539bbb9db303fb6676c78c836add5680bb0)
10a8a69ddSRusty Russell /* Virtio ring implementation.
20a8a69ddSRusty Russell  *
30a8a69ddSRusty Russell  *  Copyright 2007 Rusty Russell IBM Corporation
40a8a69ddSRusty Russell  *
50a8a69ddSRusty Russell  *  This program is free software; you can redistribute it and/or modify
60a8a69ddSRusty Russell  *  it under the terms of the GNU General Public License as published by
70a8a69ddSRusty Russell  *  the Free Software Foundation; either version 2 of the License, or
80a8a69ddSRusty Russell  *  (at your option) any later version.
90a8a69ddSRusty Russell  *
100a8a69ddSRusty Russell  *  This program is distributed in the hope that it will be useful,
110a8a69ddSRusty Russell  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
120a8a69ddSRusty Russell  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
130a8a69ddSRusty Russell  *  GNU General Public License for more details.
140a8a69ddSRusty Russell  *
150a8a69ddSRusty Russell  *  You should have received a copy of the GNU General Public License
160a8a69ddSRusty Russell  *  along with this program; if not, write to the Free Software
170a8a69ddSRusty Russell  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
180a8a69ddSRusty Russell  */
190a8a69ddSRusty Russell #include <linux/virtio.h>
200a8a69ddSRusty Russell #include <linux/virtio_ring.h>
21e34f8725SRusty Russell #include <linux/virtio_config.h>
220a8a69ddSRusty Russell #include <linux/device.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
24b5a2c4f1SPaul Gortmaker #include <linux/module.h>
25e93300b1SRusty Russell #include <linux/hrtimer.h>
266abb2dd9SJoel Stanley #include <linux/kmemleak.h>
270a8a69ddSRusty Russell 
280a8a69ddSRusty Russell #ifdef DEBUG
290a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */
309499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
319499f5e7SRusty Russell 	do {							\
329499f5e7SRusty Russell 		dev_err(&(_vq)->vq.vdev->dev,			\
339499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
349499f5e7SRusty Russell 		BUG();						\
359499f5e7SRusty Russell 	} while (0)
36c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */
373a35ce7dSRoel Kluin #define START_USE(_vq)						\
38c5f841f1SRusty Russell 	do {							\
39c5f841f1SRusty Russell 		if ((_vq)->in_use)				\
409499f5e7SRusty Russell 			panic("%s:in_use = %i\n",		\
419499f5e7SRusty Russell 			      (_vq)->vq.name, (_vq)->in_use);	\
42c5f841f1SRusty Russell 		(_vq)->in_use = __LINE__;			\
43c5f841f1SRusty Russell 	} while (0)
443a35ce7dSRoel Kluin #define END_USE(_vq) \
4597a545abSRusty Russell 	do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
460a8a69ddSRusty Russell #else
479499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
489499f5e7SRusty Russell 	do {							\
499499f5e7SRusty Russell 		dev_err(&_vq->vq.vdev->dev,			\
509499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
519499f5e7SRusty Russell 		(_vq)->broken = true;				\
529499f5e7SRusty Russell 	} while (0)
530a8a69ddSRusty Russell #define START_USE(vq)
540a8a69ddSRusty Russell #define END_USE(vq)
550a8a69ddSRusty Russell #endif
560a8a69ddSRusty Russell 
5743b4f721SMichael S. Tsirkin struct vring_virtqueue {
580a8a69ddSRusty Russell 	struct virtqueue vq;
590a8a69ddSRusty Russell 
600a8a69ddSRusty Russell 	/* Actual memory layout for this queue */
610a8a69ddSRusty Russell 	struct vring vring;
620a8a69ddSRusty Russell 
637b21e34fSRusty Russell 	/* Can we use weak barriers? */
647b21e34fSRusty Russell 	bool weak_barriers;
657b21e34fSRusty Russell 
660a8a69ddSRusty Russell 	/* Other side has made a mess, don't try any more. */
670a8a69ddSRusty Russell 	bool broken;
680a8a69ddSRusty Russell 
699fa29b9dSMark McLoughlin 	/* Host supports indirect buffers */
709fa29b9dSMark McLoughlin 	bool indirect;
719fa29b9dSMark McLoughlin 
72a5c262c5SMichael S. Tsirkin 	/* Host publishes avail event idx */
73a5c262c5SMichael S. Tsirkin 	bool event;
74a5c262c5SMichael S. Tsirkin 
750a8a69ddSRusty Russell 	/* Head of free buffer list. */
760a8a69ddSRusty Russell 	unsigned int free_head;
770a8a69ddSRusty Russell 	/* Number we've added since last sync. */
780a8a69ddSRusty Russell 	unsigned int num_added;
790a8a69ddSRusty Russell 
800a8a69ddSRusty Russell 	/* Last used index we've seen. */
811bc4953eSAnthony Liguori 	u16 last_used_idx;
820a8a69ddSRusty Russell 
830a8a69ddSRusty Russell 	/* How to notify other side. FIXME: commonalize hcalls! */
8446f9c2b9SHeinz Graalfs 	bool (*notify)(struct virtqueue *vq);
850a8a69ddSRusty Russell 
860a8a69ddSRusty Russell #ifdef DEBUG
870a8a69ddSRusty Russell 	/* They're supposed to lock for us. */
880a8a69ddSRusty Russell 	unsigned int in_use;
89e93300b1SRusty Russell 
90e93300b1SRusty Russell 	/* Figure out if their kicks are too delayed. */
91e93300b1SRusty Russell 	bool last_add_time_valid;
92e93300b1SRusty Russell 	ktime_t last_add_time;
930a8a69ddSRusty Russell #endif
940a8a69ddSRusty Russell 
950a8a69ddSRusty Russell 	/* Tokens for callbacks. */
960a8a69ddSRusty Russell 	void *data[];
970a8a69ddSRusty Russell };
980a8a69ddSRusty Russell 
990a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
1000a8a69ddSRusty Russell 
10100e6f3d9SMichael S. Tsirkin static struct vring_desc *alloc_indirect(struct virtqueue *_vq,
10200e6f3d9SMichael S. Tsirkin 					 unsigned int total_sg, gfp_t gfp)
1039fa29b9dSMark McLoughlin {
1049fa29b9dSMark McLoughlin 	struct vring_desc *desc;
105b25bd251SRusty Russell 	unsigned int i;
1069fa29b9dSMark McLoughlin 
107b92b1b89SWill Deacon 	/*
108b92b1b89SWill Deacon 	 * We require lowmem mappings for the descriptors because
109b92b1b89SWill Deacon 	 * otherwise virt_to_phys will give us bogus addresses in the
110b92b1b89SWill Deacon 	 * virtqueue.
111b92b1b89SWill Deacon 	 */
112*82107539SMichal Hocko 	gfp &= ~__GFP_HIGHMEM;
113b92b1b89SWill Deacon 
11413816c76SRusty Russell 	desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
1159fa29b9dSMark McLoughlin 	if (!desc)
116b25bd251SRusty Russell 		return NULL;
1179fa29b9dSMark McLoughlin 
118b25bd251SRusty Russell 	for (i = 0; i < total_sg; i++)
11900e6f3d9SMichael S. Tsirkin 		desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
120b25bd251SRusty Russell 	return desc;
1219fa29b9dSMark McLoughlin }
1229fa29b9dSMark McLoughlin 
12313816c76SRusty Russell static inline int virtqueue_add(struct virtqueue *_vq,
12413816c76SRusty Russell 				struct scatterlist *sgs[],
125eeebf9b1SRusty Russell 				unsigned int total_sg,
12613816c76SRusty Russell 				unsigned int out_sgs,
12713816c76SRusty Russell 				unsigned int in_sgs,
128bbd603efSMichael S. Tsirkin 				void *data,
129bbd603efSMichael S. Tsirkin 				gfp_t gfp)
1300a8a69ddSRusty Russell {
1310a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
13213816c76SRusty Russell 	struct scatterlist *sg;
133b25bd251SRusty Russell 	struct vring_desc *desc;
134b25bd251SRusty Russell 	unsigned int i, n, avail, descs_used, uninitialized_var(prev);
1351fe9b6feSMichael S. Tsirkin 	int head;
136b25bd251SRusty Russell 	bool indirect;
1370a8a69ddSRusty Russell 
1389fa29b9dSMark McLoughlin 	START_USE(vq);
1399fa29b9dSMark McLoughlin 
1400a8a69ddSRusty Russell 	BUG_ON(data == NULL);
1419fa29b9dSMark McLoughlin 
14270670444SRusty Russell 	if (unlikely(vq->broken)) {
14370670444SRusty Russell 		END_USE(vq);
14470670444SRusty Russell 		return -EIO;
14570670444SRusty Russell 	}
14670670444SRusty Russell 
147e93300b1SRusty Russell #ifdef DEBUG
148e93300b1SRusty Russell 	{
149e93300b1SRusty Russell 		ktime_t now = ktime_get();
150e93300b1SRusty Russell 
151e93300b1SRusty Russell 		/* No kick or get, with .1 second between?  Warn. */
152e93300b1SRusty Russell 		if (vq->last_add_time_valid)
153e93300b1SRusty Russell 			WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
154e93300b1SRusty Russell 					    > 100);
155e93300b1SRusty Russell 		vq->last_add_time = now;
156e93300b1SRusty Russell 		vq->last_add_time_valid = true;
157e93300b1SRusty Russell 	}
158e93300b1SRusty Russell #endif
159e93300b1SRusty Russell 
16013816c76SRusty Russell 	BUG_ON(total_sg > vq->vring.num);
16113816c76SRusty Russell 	BUG_ON(total_sg == 0);
1620a8a69ddSRusty Russell 
163b25bd251SRusty Russell 	head = vq->free_head;
164b25bd251SRusty Russell 
165b25bd251SRusty Russell 	/* If the host supports indirect descriptor tables, and we have multiple
166b25bd251SRusty Russell 	 * buffers, then go indirect. FIXME: tune this threshold */
167b25bd251SRusty Russell 	if (vq->indirect && total_sg > 1 && vq->vq.num_free)
16800e6f3d9SMichael S. Tsirkin 		desc = alloc_indirect(_vq, total_sg, gfp);
169b25bd251SRusty Russell 	else
170b25bd251SRusty Russell 		desc = NULL;
171b25bd251SRusty Russell 
172b25bd251SRusty Russell 	if (desc) {
173b25bd251SRusty Russell 		/* Use a single buffer which doesn't continue */
17400e6f3d9SMichael S. Tsirkin 		vq->vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_INDIRECT);
17500e6f3d9SMichael S. Tsirkin 		vq->vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, virt_to_phys(desc));
176b25bd251SRusty Russell 		/* avoid kmemleak false positive (hidden by virt_to_phys) */
177b25bd251SRusty Russell 		kmemleak_ignore(desc);
17800e6f3d9SMichael S. Tsirkin 		vq->vring.desc[head].len = cpu_to_virtio32(_vq->vdev, total_sg * sizeof(struct vring_desc));
179b25bd251SRusty Russell 
180b25bd251SRusty Russell 		/* Set up rest to use this indirect table. */
181b25bd251SRusty Russell 		i = 0;
182b25bd251SRusty Russell 		descs_used = 1;
183b25bd251SRusty Russell 		indirect = true;
184b25bd251SRusty Russell 	} else {
185b25bd251SRusty Russell 		desc = vq->vring.desc;
186b25bd251SRusty Russell 		i = head;
187b25bd251SRusty Russell 		descs_used = total_sg;
188b25bd251SRusty Russell 		indirect = false;
189b25bd251SRusty Russell 	}
190b25bd251SRusty Russell 
191b25bd251SRusty Russell 	if (vq->vq.num_free < descs_used) {
1920a8a69ddSRusty Russell 		pr_debug("Can't add buf len %i - avail = %i\n",
193b25bd251SRusty Russell 			 descs_used, vq->vq.num_free);
19444653eaeSRusty Russell 		/* FIXME: for historical reasons, we force a notify here if
19544653eaeSRusty Russell 		 * there are outgoing parts to the buffer.  Presumably the
19644653eaeSRusty Russell 		 * host should service the ring ASAP. */
19713816c76SRusty Russell 		if (out_sgs)
198426e3e0aSRusty Russell 			vq->notify(&vq->vq);
1990a8a69ddSRusty Russell 		END_USE(vq);
2000a8a69ddSRusty Russell 		return -ENOSPC;
2010a8a69ddSRusty Russell 	}
2020a8a69ddSRusty Russell 
2030a8a69ddSRusty Russell 	/* We're about to use some buffers from the free list. */
204b25bd251SRusty Russell 	vq->vq.num_free -= descs_used;
2050a8a69ddSRusty Russell 
20613816c76SRusty Russell 	for (n = 0; n < out_sgs; n++) {
207eeebf9b1SRusty Russell 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
20800e6f3d9SMichael S. Tsirkin 			desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT);
20900e6f3d9SMichael S. Tsirkin 			desc[i].addr = cpu_to_virtio64(_vq->vdev, sg_phys(sg));
21000e6f3d9SMichael S. Tsirkin 			desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
2110a8a69ddSRusty Russell 			prev = i;
21200e6f3d9SMichael S. Tsirkin 			i = virtio16_to_cpu(_vq->vdev, desc[i].next);
2130a8a69ddSRusty Russell 		}
21413816c76SRusty Russell 	}
21513816c76SRusty Russell 	for (; n < (out_sgs + in_sgs); n++) {
216eeebf9b1SRusty Russell 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
21700e6f3d9SMichael S. Tsirkin 			desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE);
21800e6f3d9SMichael S. Tsirkin 			desc[i].addr = cpu_to_virtio64(_vq->vdev, sg_phys(sg));
21900e6f3d9SMichael S. Tsirkin 			desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
2200a8a69ddSRusty Russell 			prev = i;
22100e6f3d9SMichael S. Tsirkin 			i = virtio16_to_cpu(_vq->vdev, desc[i].next);
22213816c76SRusty Russell 		}
2230a8a69ddSRusty Russell 	}
2240a8a69ddSRusty Russell 	/* Last one doesn't continue. */
22500e6f3d9SMichael S. Tsirkin 	desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
2260a8a69ddSRusty Russell 
2270a8a69ddSRusty Russell 	/* Update free pointer */
228b25bd251SRusty Russell 	if (indirect)
22900e6f3d9SMichael S. Tsirkin 		vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next);
230b25bd251SRusty Russell 	else
2310a8a69ddSRusty Russell 		vq->free_head = i;
2320a8a69ddSRusty Russell 
2330a8a69ddSRusty Russell 	/* Set token. */
2340a8a69ddSRusty Russell 	vq->data[head] = data;
2350a8a69ddSRusty Russell 
2360a8a69ddSRusty Russell 	/* Put entry in available array (but don't update avail->idx until they
2373b720b8cSRusty Russell 	 * do sync). */
23800e6f3d9SMichael S. Tsirkin 	avail = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) & (vq->vring.num - 1);
23900e6f3d9SMichael S. Tsirkin 	vq->vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
2400a8a69ddSRusty Russell 
241ee7cd898SRusty Russell 	/* Descriptors and available array need to be set before we expose the
242ee7cd898SRusty Russell 	 * new available array entries. */
243a9a0fef7SRusty Russell 	virtio_wmb(vq->weak_barriers);
24400e6f3d9SMichael S. Tsirkin 	vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) + 1);
245ee7cd898SRusty Russell 	vq->num_added++;
246ee7cd898SRusty Russell 
2475e05bf58STetsuo Handa 	pr_debug("Added buffer head %i to %p\n", head, vq);
2485e05bf58STetsuo Handa 	END_USE(vq);
2495e05bf58STetsuo Handa 
250ee7cd898SRusty Russell 	/* This is very unlikely, but theoretically possible.  Kick
251ee7cd898SRusty Russell 	 * just in case. */
252ee7cd898SRusty Russell 	if (unlikely(vq->num_added == (1 << 16) - 1))
253ee7cd898SRusty Russell 		virtqueue_kick(_vq);
254ee7cd898SRusty Russell 
25598e8c6bcSRusty Russell 	return 0;
2560a8a69ddSRusty Russell }
25713816c76SRusty Russell 
25813816c76SRusty Russell /**
25913816c76SRusty Russell  * virtqueue_add_sgs - expose buffers to other end
26013816c76SRusty Russell  * @vq: the struct virtqueue we're talking about.
26113816c76SRusty Russell  * @sgs: array of terminated scatterlists.
26213816c76SRusty Russell  * @out_num: the number of scatterlists readable by other side
26313816c76SRusty Russell  * @in_num: the number of scatterlists which are writable (after readable ones)
26413816c76SRusty Russell  * @data: the token identifying the buffer.
26513816c76SRusty Russell  * @gfp: how to do memory allocations (if necessary).
26613816c76SRusty Russell  *
26713816c76SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
26813816c76SRusty Russell  * at the same time (except where noted).
26913816c76SRusty Russell  *
27070670444SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
27113816c76SRusty Russell  */
27213816c76SRusty Russell int virtqueue_add_sgs(struct virtqueue *_vq,
27313816c76SRusty Russell 		      struct scatterlist *sgs[],
27413816c76SRusty Russell 		      unsigned int out_sgs,
27513816c76SRusty Russell 		      unsigned int in_sgs,
27613816c76SRusty Russell 		      void *data,
27713816c76SRusty Russell 		      gfp_t gfp)
27813816c76SRusty Russell {
279eeebf9b1SRusty Russell 	unsigned int i, total_sg = 0;
28013816c76SRusty Russell 
28113816c76SRusty Russell 	/* Count them first. */
282eeebf9b1SRusty Russell 	for (i = 0; i < out_sgs + in_sgs; i++) {
28313816c76SRusty Russell 		struct scatterlist *sg;
28413816c76SRusty Russell 		for (sg = sgs[i]; sg; sg = sg_next(sg))
285eeebf9b1SRusty Russell 			total_sg++;
28613816c76SRusty Russell 	}
287eeebf9b1SRusty Russell 	return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp);
28813816c76SRusty Russell }
28913816c76SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
29013816c76SRusty Russell 
29113816c76SRusty Russell /**
292282edb36SRusty Russell  * virtqueue_add_outbuf - expose output buffers to other end
293282edb36SRusty Russell  * @vq: the struct virtqueue we're talking about.
294eeebf9b1SRusty Russell  * @sg: scatterlist (must be well-formed and terminated!)
295eeebf9b1SRusty Russell  * @num: the number of entries in @sg readable by other side
296282edb36SRusty Russell  * @data: the token identifying the buffer.
297282edb36SRusty Russell  * @gfp: how to do memory allocations (if necessary).
298282edb36SRusty Russell  *
299282edb36SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
300282edb36SRusty Russell  * at the same time (except where noted).
301282edb36SRusty Russell  *
30270670444SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
303282edb36SRusty Russell  */
304282edb36SRusty Russell int virtqueue_add_outbuf(struct virtqueue *vq,
305eeebf9b1SRusty Russell 			 struct scatterlist *sg, unsigned int num,
306282edb36SRusty Russell 			 void *data,
307282edb36SRusty Russell 			 gfp_t gfp)
308282edb36SRusty Russell {
309eeebf9b1SRusty Russell 	return virtqueue_add(vq, &sg, num, 1, 0, data, gfp);
310282edb36SRusty Russell }
311282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
312282edb36SRusty Russell 
313282edb36SRusty Russell /**
314282edb36SRusty Russell  * virtqueue_add_inbuf - expose input buffers to other end
315282edb36SRusty Russell  * @vq: the struct virtqueue we're talking about.
316eeebf9b1SRusty Russell  * @sg: scatterlist (must be well-formed and terminated!)
317eeebf9b1SRusty Russell  * @num: the number of entries in @sg writable by other side
318282edb36SRusty Russell  * @data: the token identifying the buffer.
319282edb36SRusty Russell  * @gfp: how to do memory allocations (if necessary).
320282edb36SRusty Russell  *
321282edb36SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
322282edb36SRusty Russell  * at the same time (except where noted).
323282edb36SRusty Russell  *
32470670444SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
325282edb36SRusty Russell  */
326282edb36SRusty Russell int virtqueue_add_inbuf(struct virtqueue *vq,
327eeebf9b1SRusty Russell 			struct scatterlist *sg, unsigned int num,
328282edb36SRusty Russell 			void *data,
329282edb36SRusty Russell 			gfp_t gfp)
330282edb36SRusty Russell {
331eeebf9b1SRusty Russell 	return virtqueue_add(vq, &sg, num, 0, 1, data, gfp);
332282edb36SRusty Russell }
333282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
334282edb36SRusty Russell 
335282edb36SRusty Russell /**
33641f0377fSRusty Russell  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
3375dfc1762SRusty Russell  * @vq: the struct virtqueue
3385dfc1762SRusty Russell  *
33941f0377fSRusty Russell  * Instead of virtqueue_kick(), you can do:
34041f0377fSRusty Russell  *	if (virtqueue_kick_prepare(vq))
34141f0377fSRusty Russell  *		virtqueue_notify(vq);
3425dfc1762SRusty Russell  *
34341f0377fSRusty Russell  * This is sometimes useful because the virtqueue_kick_prepare() needs
34441f0377fSRusty Russell  * to be serialized, but the actual virtqueue_notify() call does not.
3455dfc1762SRusty Russell  */
34641f0377fSRusty Russell bool virtqueue_kick_prepare(struct virtqueue *_vq)
3470a8a69ddSRusty Russell {
3480a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
349a5c262c5SMichael S. Tsirkin 	u16 new, old;
35041f0377fSRusty Russell 	bool needs_kick;
35141f0377fSRusty Russell 
3520a8a69ddSRusty Russell 	START_USE(vq);
353a72caae2SJason Wang 	/* We need to expose available array entries before checking avail
354a72caae2SJason Wang 	 * event. */
355a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
3560a8a69ddSRusty Russell 
35700e6f3d9SMichael S. Tsirkin 	old = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - vq->num_added;
35800e6f3d9SMichael S. Tsirkin 	new = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx);
3590a8a69ddSRusty Russell 	vq->num_added = 0;
3600a8a69ddSRusty Russell 
361e93300b1SRusty Russell #ifdef DEBUG
362e93300b1SRusty Russell 	if (vq->last_add_time_valid) {
363e93300b1SRusty Russell 		WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
364e93300b1SRusty Russell 					      vq->last_add_time)) > 100);
365e93300b1SRusty Russell 	}
366e93300b1SRusty Russell 	vq->last_add_time_valid = false;
367e93300b1SRusty Russell #endif
368e93300b1SRusty Russell 
36941f0377fSRusty Russell 	if (vq->event) {
37000e6f3d9SMichael S. Tsirkin 		needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev, vring_avail_event(&vq->vring)),
37141f0377fSRusty Russell 					      new, old);
37241f0377fSRusty Russell 	} else {
37300e6f3d9SMichael S. Tsirkin 		needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(_vq->vdev, VRING_USED_F_NO_NOTIFY));
37441f0377fSRusty Russell 	}
3750a8a69ddSRusty Russell 	END_USE(vq);
37641f0377fSRusty Russell 	return needs_kick;
37741f0377fSRusty Russell }
37841f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
37941f0377fSRusty Russell 
38041f0377fSRusty Russell /**
38141f0377fSRusty Russell  * virtqueue_notify - second half of split virtqueue_kick call.
38241f0377fSRusty Russell  * @vq: the struct virtqueue
38341f0377fSRusty Russell  *
38441f0377fSRusty Russell  * This does not need to be serialized.
3855b1bf7cbSHeinz Graalfs  *
3865b1bf7cbSHeinz Graalfs  * Returns false if host notify failed or queue is broken, otherwise true.
38741f0377fSRusty Russell  */
3885b1bf7cbSHeinz Graalfs bool virtqueue_notify(struct virtqueue *_vq)
38941f0377fSRusty Russell {
39041f0377fSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
39141f0377fSRusty Russell 
3925b1bf7cbSHeinz Graalfs 	if (unlikely(vq->broken))
3935b1bf7cbSHeinz Graalfs 		return false;
3945b1bf7cbSHeinz Graalfs 
39541f0377fSRusty Russell 	/* Prod other side to tell it about changes. */
3962342d6a6SHeinz Graalfs 	if (!vq->notify(_vq)) {
3975b1bf7cbSHeinz Graalfs 		vq->broken = true;
3985b1bf7cbSHeinz Graalfs 		return false;
3995b1bf7cbSHeinz Graalfs 	}
4005b1bf7cbSHeinz Graalfs 	return true;
40141f0377fSRusty Russell }
40241f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify);
40341f0377fSRusty Russell 
40441f0377fSRusty Russell /**
40541f0377fSRusty Russell  * virtqueue_kick - update after add_buf
40641f0377fSRusty Russell  * @vq: the struct virtqueue
40741f0377fSRusty Russell  *
408b3087e48SRusty Russell  * After one or more virtqueue_add_* calls, invoke this to kick
40941f0377fSRusty Russell  * the other side.
41041f0377fSRusty Russell  *
41141f0377fSRusty Russell  * Caller must ensure we don't call this with other virtqueue
41241f0377fSRusty Russell  * operations at the same time (except where noted).
4135b1bf7cbSHeinz Graalfs  *
4145b1bf7cbSHeinz Graalfs  * Returns false if kick failed, otherwise true.
41541f0377fSRusty Russell  */
4165b1bf7cbSHeinz Graalfs bool virtqueue_kick(struct virtqueue *vq)
41741f0377fSRusty Russell {
41841f0377fSRusty Russell 	if (virtqueue_kick_prepare(vq))
4195b1bf7cbSHeinz Graalfs 		return virtqueue_notify(vq);
4205b1bf7cbSHeinz Graalfs 	return true;
4210a8a69ddSRusty Russell }
4227c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick);
4230a8a69ddSRusty Russell 
4240a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
4250a8a69ddSRusty Russell {
4260a8a69ddSRusty Russell 	unsigned int i;
4270a8a69ddSRusty Russell 
4280a8a69ddSRusty Russell 	/* Clear data ptr. */
4290a8a69ddSRusty Russell 	vq->data[head] = NULL;
4300a8a69ddSRusty Russell 
4310a8a69ddSRusty Russell 	/* Put back on free list: find end */
4320a8a69ddSRusty Russell 	i = head;
4339fa29b9dSMark McLoughlin 
4349fa29b9dSMark McLoughlin 	/* Free the indirect table */
43500e6f3d9SMichael S. Tsirkin 	if (vq->vring.desc[i].flags & cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT))
43600e6f3d9SMichael S. Tsirkin 		kfree(phys_to_virt(virtio64_to_cpu(vq->vq.vdev, vq->vring.desc[i].addr)));
4379fa29b9dSMark McLoughlin 
43800e6f3d9SMichael S. Tsirkin 	while (vq->vring.desc[i].flags & cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT)) {
43900e6f3d9SMichael S. Tsirkin 		i = virtio16_to_cpu(vq->vq.vdev, vq->vring.desc[i].next);
44006ca287dSRusty Russell 		vq->vq.num_free++;
4410a8a69ddSRusty Russell 	}
4420a8a69ddSRusty Russell 
44300e6f3d9SMichael S. Tsirkin 	vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head);
4440a8a69ddSRusty Russell 	vq->free_head = head;
4450a8a69ddSRusty Russell 	/* Plus final descriptor */
44606ca287dSRusty Russell 	vq->vq.num_free++;
4470a8a69ddSRusty Russell }
4480a8a69ddSRusty Russell 
4490a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq)
4500a8a69ddSRusty Russell {
45100e6f3d9SMichael S. Tsirkin 	return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev, vq->vring.used->idx);
4520a8a69ddSRusty Russell }
4530a8a69ddSRusty Russell 
4545dfc1762SRusty Russell /**
4555dfc1762SRusty Russell  * virtqueue_get_buf - get the next used buffer
4565dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
4575dfc1762SRusty Russell  * @len: the length written into the buffer
4585dfc1762SRusty Russell  *
4595dfc1762SRusty Russell  * If the driver wrote data into the buffer, @len will be set to the
4605dfc1762SRusty Russell  * amount written.  This means you don't need to clear the buffer
4615dfc1762SRusty Russell  * beforehand to ensure there's no data leakage in the case of short
4625dfc1762SRusty Russell  * writes.
4635dfc1762SRusty Russell  *
4645dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
4655dfc1762SRusty Russell  * operations at the same time (except where noted).
4665dfc1762SRusty Russell  *
4675dfc1762SRusty Russell  * Returns NULL if there are no used buffers, or the "data" token
468b3087e48SRusty Russell  * handed to virtqueue_add_*().
4695dfc1762SRusty Russell  */
4707c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
4710a8a69ddSRusty Russell {
4720a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
4730a8a69ddSRusty Russell 	void *ret;
4740a8a69ddSRusty Russell 	unsigned int i;
4753b720b8cSRusty Russell 	u16 last_used;
4760a8a69ddSRusty Russell 
4770a8a69ddSRusty Russell 	START_USE(vq);
4780a8a69ddSRusty Russell 
4795ef82752SRusty Russell 	if (unlikely(vq->broken)) {
4805ef82752SRusty Russell 		END_USE(vq);
4815ef82752SRusty Russell 		return NULL;
4825ef82752SRusty Russell 	}
4835ef82752SRusty Russell 
4840a8a69ddSRusty Russell 	if (!more_used(vq)) {
4850a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
4860a8a69ddSRusty Russell 		END_USE(vq);
4870a8a69ddSRusty Russell 		return NULL;
4880a8a69ddSRusty Russell 	}
4890a8a69ddSRusty Russell 
4902d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
491a9a0fef7SRusty Russell 	virtio_rmb(vq->weak_barriers);
4922d61ba95SMichael S. Tsirkin 
4933b720b8cSRusty Russell 	last_used = (vq->last_used_idx & (vq->vring.num - 1));
49400e6f3d9SMichael S. Tsirkin 	i = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].id);
49500e6f3d9SMichael S. Tsirkin 	*len = virtio32_to_cpu(_vq->vdev, vq->vring.used->ring[last_used].len);
4960a8a69ddSRusty Russell 
4970a8a69ddSRusty Russell 	if (unlikely(i >= vq->vring.num)) {
4980a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
4990a8a69ddSRusty Russell 		return NULL;
5000a8a69ddSRusty Russell 	}
5010a8a69ddSRusty Russell 	if (unlikely(!vq->data[i])) {
5020a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
5030a8a69ddSRusty Russell 		return NULL;
5040a8a69ddSRusty Russell 	}
5050a8a69ddSRusty Russell 
5060a8a69ddSRusty Russell 	/* detach_buf clears data, so grab it now. */
5070a8a69ddSRusty Russell 	ret = vq->data[i];
5080a8a69ddSRusty Russell 	detach_buf(vq, i);
5090a8a69ddSRusty Russell 	vq->last_used_idx++;
510a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
511a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
512a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
51300e6f3d9SMichael S. Tsirkin 	if (!(vq->vring.avail->flags & cpu_to_virtio16(_vq->vdev, VRING_AVAIL_F_NO_INTERRUPT))) {
51400e6f3d9SMichael S. Tsirkin 		vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx);
515a9a0fef7SRusty Russell 		virtio_mb(vq->weak_barriers);
516a5c262c5SMichael S. Tsirkin 	}
517a5c262c5SMichael S. Tsirkin 
518e93300b1SRusty Russell #ifdef DEBUG
519e93300b1SRusty Russell 	vq->last_add_time_valid = false;
520e93300b1SRusty Russell #endif
521e93300b1SRusty Russell 
5220a8a69ddSRusty Russell 	END_USE(vq);
5230a8a69ddSRusty Russell 	return ret;
5240a8a69ddSRusty Russell }
5257c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf);
5260a8a69ddSRusty Russell 
5275dfc1762SRusty Russell /**
5285dfc1762SRusty Russell  * virtqueue_disable_cb - disable callbacks
5295dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5305dfc1762SRusty Russell  *
5315dfc1762SRusty Russell  * Note that this is not necessarily synchronous, hence unreliable and only
5325dfc1762SRusty Russell  * useful as an optimization.
5335dfc1762SRusty Russell  *
5345dfc1762SRusty Russell  * Unlike other operations, this need not be serialized.
5355dfc1762SRusty Russell  */
5367c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq)
53718445c4dSRusty Russell {
53818445c4dSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
53918445c4dSRusty Russell 
54000e6f3d9SMichael S. Tsirkin 	vq->vring.avail->flags |= cpu_to_virtio16(_vq->vdev, VRING_AVAIL_F_NO_INTERRUPT);
54118445c4dSRusty Russell }
5427c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
54318445c4dSRusty Russell 
5445dfc1762SRusty Russell /**
545cc229884SMichael S. Tsirkin  * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
546cc229884SMichael S. Tsirkin  * @vq: the struct virtqueue we're talking about.
547cc229884SMichael S. Tsirkin  *
548cc229884SMichael S. Tsirkin  * This re-enables callbacks; it returns current queue state
549cc229884SMichael S. Tsirkin  * in an opaque unsigned value. This value should be later tested by
550cc229884SMichael S. Tsirkin  * virtqueue_poll, to detect a possible race between the driver checking for
551cc229884SMichael S. Tsirkin  * more work, and enabling callbacks.
552cc229884SMichael S. Tsirkin  *
553cc229884SMichael S. Tsirkin  * Caller must ensure we don't call this with other virtqueue
554cc229884SMichael S. Tsirkin  * operations at the same time (except where noted).
555cc229884SMichael S. Tsirkin  */
556cc229884SMichael S. Tsirkin unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
557cc229884SMichael S. Tsirkin {
558cc229884SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
559cc229884SMichael S. Tsirkin 	u16 last_used_idx;
560cc229884SMichael S. Tsirkin 
561cc229884SMichael S. Tsirkin 	START_USE(vq);
562cc229884SMichael S. Tsirkin 
563cc229884SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
564cc229884SMichael S. Tsirkin 	 * more to do. */
565cc229884SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
566cc229884SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
567cc229884SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
56800e6f3d9SMichael S. Tsirkin 	vq->vring.avail->flags &= cpu_to_virtio16(_vq->vdev, ~VRING_AVAIL_F_NO_INTERRUPT);
56900e6f3d9SMichael S. Tsirkin 	vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, last_used_idx = vq->last_used_idx);
570cc229884SMichael S. Tsirkin 	END_USE(vq);
571cc229884SMichael S. Tsirkin 	return last_used_idx;
572cc229884SMichael S. Tsirkin }
573cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
574cc229884SMichael S. Tsirkin 
575cc229884SMichael S. Tsirkin /**
576cc229884SMichael S. Tsirkin  * virtqueue_poll - query pending used buffers
577cc229884SMichael S. Tsirkin  * @vq: the struct virtqueue we're talking about.
578cc229884SMichael S. Tsirkin  * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
579cc229884SMichael S. Tsirkin  *
580cc229884SMichael S. Tsirkin  * Returns "true" if there are pending used buffers in the queue.
581cc229884SMichael S. Tsirkin  *
582cc229884SMichael S. Tsirkin  * This does not need to be serialized.
583cc229884SMichael S. Tsirkin  */
584cc229884SMichael S. Tsirkin bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
585cc229884SMichael S. Tsirkin {
586cc229884SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
587cc229884SMichael S. Tsirkin 
588cc229884SMichael S. Tsirkin 	virtio_mb(vq->weak_barriers);
58900e6f3d9SMichael S. Tsirkin 	return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev, vq->vring.used->idx);
590cc229884SMichael S. Tsirkin }
591cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_poll);
592cc229884SMichael S. Tsirkin 
593cc229884SMichael S. Tsirkin /**
5945dfc1762SRusty Russell  * virtqueue_enable_cb - restart callbacks after disable_cb.
5955dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5965dfc1762SRusty Russell  *
5975dfc1762SRusty Russell  * This re-enables callbacks; it returns "false" if there are pending
5985dfc1762SRusty Russell  * buffers in the queue, to detect a possible race between the driver
5995dfc1762SRusty Russell  * checking for more work, and enabling callbacks.
6005dfc1762SRusty Russell  *
6015dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
6025dfc1762SRusty Russell  * operations at the same time (except where noted).
6035dfc1762SRusty Russell  */
6047c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq)
6050a8a69ddSRusty Russell {
606cc229884SMichael S. Tsirkin 	unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
607cc229884SMichael S. Tsirkin 	return !virtqueue_poll(_vq, last_used_idx);
6080a8a69ddSRusty Russell }
6097c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
6100a8a69ddSRusty Russell 
6115dfc1762SRusty Russell /**
6125dfc1762SRusty Russell  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
6135dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
6145dfc1762SRusty Russell  *
6155dfc1762SRusty Russell  * This re-enables callbacks but hints to the other side to delay
6165dfc1762SRusty Russell  * interrupts until most of the available buffers have been processed;
6175dfc1762SRusty Russell  * it returns "false" if there are many pending buffers in the queue,
6185dfc1762SRusty Russell  * to detect a possible race between the driver checking for more work,
6195dfc1762SRusty Russell  * and enabling callbacks.
6205dfc1762SRusty Russell  *
6215dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
6225dfc1762SRusty Russell  * operations at the same time (except where noted).
6235dfc1762SRusty Russell  */
6247ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
6257ab358c2SMichael S. Tsirkin {
6267ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
6277ab358c2SMichael S. Tsirkin 	u16 bufs;
6287ab358c2SMichael S. Tsirkin 
6297ab358c2SMichael S. Tsirkin 	START_USE(vq);
6307ab358c2SMichael S. Tsirkin 
6317ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
6327ab358c2SMichael S. Tsirkin 	 * more to do. */
6337ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
6347ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
6357ab358c2SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
63600e6f3d9SMichael S. Tsirkin 	vq->vring.avail->flags &= cpu_to_virtio16(_vq->vdev, ~VRING_AVAIL_F_NO_INTERRUPT);
6377ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
63800e6f3d9SMichael S. Tsirkin 	bufs = (u16)(virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - vq->last_used_idx) * 3 / 4;
63900e6f3d9SMichael S. Tsirkin 	vring_used_event(&vq->vring) = cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs);
640a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
64100e6f3d9SMichael S. Tsirkin 	if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->vring.used->idx) - vq->last_used_idx) > bufs)) {
6427ab358c2SMichael S. Tsirkin 		END_USE(vq);
6437ab358c2SMichael S. Tsirkin 		return false;
6447ab358c2SMichael S. Tsirkin 	}
6457ab358c2SMichael S. Tsirkin 
6467ab358c2SMichael S. Tsirkin 	END_USE(vq);
6477ab358c2SMichael S. Tsirkin 	return true;
6487ab358c2SMichael S. Tsirkin }
6497ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
6507ab358c2SMichael S. Tsirkin 
6515dfc1762SRusty Russell /**
6525dfc1762SRusty Russell  * virtqueue_detach_unused_buf - detach first unused buffer
6535dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
6545dfc1762SRusty Russell  *
655b3087e48SRusty Russell  * Returns NULL or the "data" token handed to virtqueue_add_*().
6565dfc1762SRusty Russell  * This is not valid on an active queue; it is useful only for device
6575dfc1762SRusty Russell  * shutdown.
6585dfc1762SRusty Russell  */
6597c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
660c021eac4SShirley Ma {
661c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
662c021eac4SShirley Ma 	unsigned int i;
663c021eac4SShirley Ma 	void *buf;
664c021eac4SShirley Ma 
665c021eac4SShirley Ma 	START_USE(vq);
666c021eac4SShirley Ma 
667c021eac4SShirley Ma 	for (i = 0; i < vq->vring.num; i++) {
668c021eac4SShirley Ma 		if (!vq->data[i])
669c021eac4SShirley Ma 			continue;
670c021eac4SShirley Ma 		/* detach_buf clears data, so grab it now. */
671c021eac4SShirley Ma 		buf = vq->data[i];
672c021eac4SShirley Ma 		detach_buf(vq, i);
67300e6f3d9SMichael S. Tsirkin 		vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - 1);
674c021eac4SShirley Ma 		END_USE(vq);
675c021eac4SShirley Ma 		return buf;
676c021eac4SShirley Ma 	}
677c021eac4SShirley Ma 	/* That should have freed everything. */
67806ca287dSRusty Russell 	BUG_ON(vq->vq.num_free != vq->vring.num);
679c021eac4SShirley Ma 
680c021eac4SShirley Ma 	END_USE(vq);
681c021eac4SShirley Ma 	return NULL;
682c021eac4SShirley Ma }
6837c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
684c021eac4SShirley Ma 
6850a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
6860a8a69ddSRusty Russell {
6870a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
6880a8a69ddSRusty Russell 
6890a8a69ddSRusty Russell 	if (!more_used(vq)) {
6900a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
6910a8a69ddSRusty Russell 		return IRQ_NONE;
6920a8a69ddSRusty Russell 	}
6930a8a69ddSRusty Russell 
6940a8a69ddSRusty Russell 	if (unlikely(vq->broken))
6950a8a69ddSRusty Russell 		return IRQ_HANDLED;
6960a8a69ddSRusty Russell 
6970a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
69818445c4dSRusty Russell 	if (vq->vq.callback)
69918445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
7000a8a69ddSRusty Russell 
7010a8a69ddSRusty Russell 	return IRQ_HANDLED;
7020a8a69ddSRusty Russell }
703c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
7040a8a69ddSRusty Russell 
70517bb6d40SJason Wang struct virtqueue *vring_new_virtqueue(unsigned int index,
70617bb6d40SJason Wang 				      unsigned int num,
70787c7d57cSRusty Russell 				      unsigned int vring_align,
7080a8a69ddSRusty Russell 				      struct virtio_device *vdev,
7097b21e34fSRusty Russell 				      bool weak_barriers,
7100a8a69ddSRusty Russell 				      void *pages,
71146f9c2b9SHeinz Graalfs 				      bool (*notify)(struct virtqueue *),
7129499f5e7SRusty Russell 				      void (*callback)(struct virtqueue *),
7139499f5e7SRusty Russell 				      const char *name)
7140a8a69ddSRusty Russell {
7150a8a69ddSRusty Russell 	struct vring_virtqueue *vq;
7160a8a69ddSRusty Russell 	unsigned int i;
7170a8a69ddSRusty Russell 
71842b36cc0SRusty Russell 	/* We assume num is a power of 2. */
71942b36cc0SRusty Russell 	if (num & (num - 1)) {
72042b36cc0SRusty Russell 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
72142b36cc0SRusty Russell 		return NULL;
72242b36cc0SRusty Russell 	}
72342b36cc0SRusty Russell 
7240a8a69ddSRusty Russell 	vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
7250a8a69ddSRusty Russell 	if (!vq)
7260a8a69ddSRusty Russell 		return NULL;
7270a8a69ddSRusty Russell 
72887c7d57cSRusty Russell 	vring_init(&vq->vring, num, pages, vring_align);
7290a8a69ddSRusty Russell 	vq->vq.callback = callback;
7300a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
7319499f5e7SRusty Russell 	vq->vq.name = name;
73206ca287dSRusty Russell 	vq->vq.num_free = num;
73306ca287dSRusty Russell 	vq->vq.index = index;
7340a8a69ddSRusty Russell 	vq->notify = notify;
7357b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
7360a8a69ddSRusty Russell 	vq->broken = false;
7370a8a69ddSRusty Russell 	vq->last_used_idx = 0;
7380a8a69ddSRusty Russell 	vq->num_added = 0;
7399499f5e7SRusty Russell 	list_add_tail(&vq->vq.list, &vdev->vqs);
7400a8a69ddSRusty Russell #ifdef DEBUG
7410a8a69ddSRusty Russell 	vq->in_use = false;
742e93300b1SRusty Russell 	vq->last_add_time_valid = false;
7430a8a69ddSRusty Russell #endif
7440a8a69ddSRusty Russell 
7459fa29b9dSMark McLoughlin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
746a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
7479fa29b9dSMark McLoughlin 
7480a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
7490a8a69ddSRusty Russell 	if (!callback)
75000e6f3d9SMichael S. Tsirkin 		vq->vring.avail->flags |= cpu_to_virtio16(vdev, VRING_AVAIL_F_NO_INTERRUPT);
7510a8a69ddSRusty Russell 
7520a8a69ddSRusty Russell 	/* Put everything in free lists. */
7530a8a69ddSRusty Russell 	vq->free_head = 0;
7543b870624SAmit Shah 	for (i = 0; i < num-1; i++) {
75500e6f3d9SMichael S. Tsirkin 		vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
7563b870624SAmit Shah 		vq->data[i] = NULL;
7573b870624SAmit Shah 	}
7583b870624SAmit Shah 	vq->data[i] = NULL;
7590a8a69ddSRusty Russell 
7600a8a69ddSRusty Russell 	return &vq->vq;
7610a8a69ddSRusty Russell }
762c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
7630a8a69ddSRusty Russell 
7640a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq)
7650a8a69ddSRusty Russell {
7669499f5e7SRusty Russell 	list_del(&vq->list);
7670a8a69ddSRusty Russell 	kfree(to_vvq(vq));
7680a8a69ddSRusty Russell }
769c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
7700a8a69ddSRusty Russell 
771e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
772e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
773e34f8725SRusty Russell {
774e34f8725SRusty Russell 	unsigned int i;
775e34f8725SRusty Russell 
776e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
777e34f8725SRusty Russell 		switch (i) {
7789fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
7799fa29b9dSMark McLoughlin 			break;
780a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
781a5c262c5SMichael S. Tsirkin 			break;
782747ae34aSMichael S. Tsirkin 		case VIRTIO_F_VERSION_1:
783747ae34aSMichael S. Tsirkin 			break;
784e34f8725SRusty Russell 		default:
785e34f8725SRusty Russell 			/* We don't understand this bit. */
786e16e12beSMichael S. Tsirkin 			__virtio_clear_bit(vdev, i);
787e34f8725SRusty Russell 		}
788e34f8725SRusty Russell 	}
789e34f8725SRusty Russell }
790e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
791e34f8725SRusty Russell 
7925dfc1762SRusty Russell /**
7935dfc1762SRusty Russell  * virtqueue_get_vring_size - return the size of the virtqueue's vring
7945dfc1762SRusty Russell  * @vq: the struct virtqueue containing the vring of interest.
7955dfc1762SRusty Russell  *
7965dfc1762SRusty Russell  * Returns the size of the vring.  This is mainly used for boasting to
7975dfc1762SRusty Russell  * userspace.  Unlike other operations, this need not be serialized.
7985dfc1762SRusty Russell  */
7998f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
8008f9f4668SRick Jones {
8018f9f4668SRick Jones 
8028f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
8038f9f4668SRick Jones 
8048f9f4668SRick Jones 	return vq->vring.num;
8058f9f4668SRick Jones }
8068f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
8078f9f4668SRick Jones 
808b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq)
809b3b32c94SHeinz Graalfs {
810b3b32c94SHeinz Graalfs 	struct vring_virtqueue *vq = to_vvq(_vq);
811b3b32c94SHeinz Graalfs 
812b3b32c94SHeinz Graalfs 	return vq->broken;
813b3b32c94SHeinz Graalfs }
814b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken);
815b3b32c94SHeinz Graalfs 
816e2dcdfe9SRusty Russell /*
817e2dcdfe9SRusty Russell  * This should prevent the device from being used, allowing drivers to
818e2dcdfe9SRusty Russell  * recover.  You may need to grab appropriate locks to flush.
819e2dcdfe9SRusty Russell  */
820e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev)
821e2dcdfe9SRusty Russell {
822e2dcdfe9SRusty Russell 	struct virtqueue *_vq;
823e2dcdfe9SRusty Russell 
824e2dcdfe9SRusty Russell 	list_for_each_entry(_vq, &dev->vqs, list) {
825e2dcdfe9SRusty Russell 		struct vring_virtqueue *vq = to_vvq(_vq);
826e2dcdfe9SRusty Russell 		vq->broken = true;
827e2dcdfe9SRusty Russell 	}
828e2dcdfe9SRusty Russell }
829e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device);
830e2dcdfe9SRusty Russell 
83189062652SCornelia Huck void *virtqueue_get_avail(struct virtqueue *_vq)
83289062652SCornelia Huck {
83389062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
83489062652SCornelia Huck 
83589062652SCornelia Huck 	return vq->vring.avail;
83689062652SCornelia Huck }
83789062652SCornelia Huck EXPORT_SYMBOL_GPL(virtqueue_get_avail);
83889062652SCornelia Huck 
83989062652SCornelia Huck void *virtqueue_get_used(struct virtqueue *_vq)
84089062652SCornelia Huck {
84189062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
84289062652SCornelia Huck 
84389062652SCornelia Huck 	return vq->vring.used;
84489062652SCornelia Huck }
84589062652SCornelia Huck EXPORT_SYMBOL_GPL(virtqueue_get_used);
84689062652SCornelia Huck 
847c6fd4701SRusty Russell MODULE_LICENSE("GPL");
848