xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision cc229884d3f77ec3b1240e467e0236c3e0647c0c)
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>
260a8a69ddSRusty Russell 
270a8a69ddSRusty Russell #ifdef DEBUG
280a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */
299499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
309499f5e7SRusty Russell 	do {							\
319499f5e7SRusty Russell 		dev_err(&(_vq)->vq.vdev->dev,			\
329499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
339499f5e7SRusty Russell 		BUG();						\
349499f5e7SRusty Russell 	} while (0)
35c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */
363a35ce7dSRoel Kluin #define START_USE(_vq)						\
37c5f841f1SRusty Russell 	do {							\
38c5f841f1SRusty Russell 		if ((_vq)->in_use)				\
399499f5e7SRusty Russell 			panic("%s:in_use = %i\n",		\
409499f5e7SRusty Russell 			      (_vq)->vq.name, (_vq)->in_use);	\
41c5f841f1SRusty Russell 		(_vq)->in_use = __LINE__;			\
42c5f841f1SRusty Russell 	} while (0)
433a35ce7dSRoel Kluin #define END_USE(_vq) \
4497a545abSRusty Russell 	do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
450a8a69ddSRusty Russell #else
469499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
479499f5e7SRusty Russell 	do {							\
489499f5e7SRusty Russell 		dev_err(&_vq->vq.vdev->dev,			\
499499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
509499f5e7SRusty Russell 		(_vq)->broken = true;				\
519499f5e7SRusty Russell 	} while (0)
520a8a69ddSRusty Russell #define START_USE(vq)
530a8a69ddSRusty Russell #define END_USE(vq)
540a8a69ddSRusty Russell #endif
550a8a69ddSRusty Russell 
560a8a69ddSRusty Russell struct vring_virtqueue
570a8a69ddSRusty Russell {
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! */
840a8a69ddSRusty Russell 	void (*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 
10113816c76SRusty Russell static inline struct scatterlist *sg_next_chained(struct scatterlist *sg,
10213816c76SRusty Russell 						  unsigned int *count)
10313816c76SRusty Russell {
10413816c76SRusty Russell 	return sg_next(sg);
10513816c76SRusty Russell }
10613816c76SRusty Russell 
10713816c76SRusty Russell static inline struct scatterlist *sg_next_arr(struct scatterlist *sg,
10813816c76SRusty Russell 					      unsigned int *count)
10913816c76SRusty Russell {
11013816c76SRusty Russell 	if (--(*count) == 0)
11113816c76SRusty Russell 		return NULL;
11213816c76SRusty Russell 	return sg + 1;
11313816c76SRusty Russell }
11413816c76SRusty Russell 
1159fa29b9dSMark McLoughlin /* Set up an indirect table of descriptors and add it to the queue. */
11613816c76SRusty Russell static inline int vring_add_indirect(struct vring_virtqueue *vq,
11713816c76SRusty Russell 				     struct scatterlist *sgs[],
11813816c76SRusty Russell 				     struct scatterlist *(*next)
11913816c76SRusty Russell 				       (struct scatterlist *, unsigned int *),
12013816c76SRusty Russell 				     unsigned int total_sg,
12113816c76SRusty Russell 				     unsigned int total_out,
12213816c76SRusty Russell 				     unsigned int total_in,
12313816c76SRusty Russell 				     unsigned int out_sgs,
12413816c76SRusty Russell 				     unsigned int in_sgs,
125bbd603efSMichael S. Tsirkin 				     gfp_t gfp)
1269fa29b9dSMark McLoughlin {
1279fa29b9dSMark McLoughlin 	struct vring_desc *desc;
1289fa29b9dSMark McLoughlin 	unsigned head;
12913816c76SRusty Russell 	struct scatterlist *sg;
13013816c76SRusty Russell 	int i, n;
1319fa29b9dSMark McLoughlin 
132b92b1b89SWill Deacon 	/*
133b92b1b89SWill Deacon 	 * We require lowmem mappings for the descriptors because
134b92b1b89SWill Deacon 	 * otherwise virt_to_phys will give us bogus addresses in the
135b92b1b89SWill Deacon 	 * virtqueue.
136b92b1b89SWill Deacon 	 */
137b92b1b89SWill Deacon 	gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
138b92b1b89SWill Deacon 
13913816c76SRusty Russell 	desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
1409fa29b9dSMark McLoughlin 	if (!desc)
141686d3637SMichael S. Tsirkin 		return -ENOMEM;
1429fa29b9dSMark McLoughlin 
14313816c76SRusty Russell 	/* Transfer entries from the sg lists into the indirect page */
14413816c76SRusty Russell 	i = 0;
14513816c76SRusty Russell 	for (n = 0; n < out_sgs; n++) {
14613816c76SRusty Russell 		for (sg = sgs[n]; sg; sg = next(sg, &total_out)) {
1479fa29b9dSMark McLoughlin 			desc[i].flags = VRING_DESC_F_NEXT;
1489fa29b9dSMark McLoughlin 			desc[i].addr = sg_phys(sg);
1499fa29b9dSMark McLoughlin 			desc[i].len = sg->length;
1509fa29b9dSMark McLoughlin 			desc[i].next = i+1;
15113816c76SRusty Russell 			i++;
1529fa29b9dSMark McLoughlin 		}
15313816c76SRusty Russell 	}
15413816c76SRusty Russell 	for (; n < (out_sgs + in_sgs); n++) {
15513816c76SRusty Russell 		for (sg = sgs[n]; sg; sg = next(sg, &total_in)) {
1569fa29b9dSMark McLoughlin 			desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
1579fa29b9dSMark McLoughlin 			desc[i].addr = sg_phys(sg);
1589fa29b9dSMark McLoughlin 			desc[i].len = sg->length;
1599fa29b9dSMark McLoughlin 			desc[i].next = i+1;
16013816c76SRusty Russell 			i++;
1619fa29b9dSMark McLoughlin 		}
16213816c76SRusty Russell 	}
16313816c76SRusty Russell 	BUG_ON(i != total_sg);
1649fa29b9dSMark McLoughlin 
1659fa29b9dSMark McLoughlin 	/* Last one doesn't continue. */
1669fa29b9dSMark McLoughlin 	desc[i-1].flags &= ~VRING_DESC_F_NEXT;
1679fa29b9dSMark McLoughlin 	desc[i-1].next = 0;
1689fa29b9dSMark McLoughlin 
1699fa29b9dSMark McLoughlin 	/* We're about to use a buffer */
17006ca287dSRusty Russell 	vq->vq.num_free--;
1719fa29b9dSMark McLoughlin 
1729fa29b9dSMark McLoughlin 	/* Use a single buffer which doesn't continue */
1739fa29b9dSMark McLoughlin 	head = vq->free_head;
1749fa29b9dSMark McLoughlin 	vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
1759fa29b9dSMark McLoughlin 	vq->vring.desc[head].addr = virt_to_phys(desc);
1769fa29b9dSMark McLoughlin 	vq->vring.desc[head].len = i * sizeof(struct vring_desc);
1779fa29b9dSMark McLoughlin 
1789fa29b9dSMark McLoughlin 	/* Update free pointer */
1799fa29b9dSMark McLoughlin 	vq->free_head = vq->vring.desc[head].next;
1809fa29b9dSMark McLoughlin 
1819fa29b9dSMark McLoughlin 	return head;
1829fa29b9dSMark McLoughlin }
1839fa29b9dSMark McLoughlin 
18413816c76SRusty Russell static inline int virtqueue_add(struct virtqueue *_vq,
18513816c76SRusty Russell 				struct scatterlist *sgs[],
18613816c76SRusty Russell 				struct scatterlist *(*next)
18713816c76SRusty Russell 				  (struct scatterlist *, unsigned int *),
18813816c76SRusty Russell 				unsigned int total_out,
18913816c76SRusty Russell 				unsigned int total_in,
19013816c76SRusty Russell 				unsigned int out_sgs,
19113816c76SRusty Russell 				unsigned int in_sgs,
192bbd603efSMichael S. Tsirkin 				void *data,
193bbd603efSMichael S. Tsirkin 				gfp_t gfp)
1940a8a69ddSRusty Russell {
1950a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
19613816c76SRusty Russell 	struct scatterlist *sg;
19713816c76SRusty Russell 	unsigned int i, n, avail, uninitialized_var(prev), total_sg;
1981fe9b6feSMichael S. Tsirkin 	int head;
1990a8a69ddSRusty Russell 
2009fa29b9dSMark McLoughlin 	START_USE(vq);
2019fa29b9dSMark McLoughlin 
2020a8a69ddSRusty Russell 	BUG_ON(data == NULL);
2039fa29b9dSMark McLoughlin 
204e93300b1SRusty Russell #ifdef DEBUG
205e93300b1SRusty Russell 	{
206e93300b1SRusty Russell 		ktime_t now = ktime_get();
207e93300b1SRusty Russell 
208e93300b1SRusty Russell 		/* No kick or get, with .1 second between?  Warn. */
209e93300b1SRusty Russell 		if (vq->last_add_time_valid)
210e93300b1SRusty Russell 			WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
211e93300b1SRusty Russell 					    > 100);
212e93300b1SRusty Russell 		vq->last_add_time = now;
213e93300b1SRusty Russell 		vq->last_add_time_valid = true;
214e93300b1SRusty Russell 	}
215e93300b1SRusty Russell #endif
216e93300b1SRusty Russell 
21713816c76SRusty Russell 	total_sg = total_in + total_out;
21813816c76SRusty Russell 
2199fa29b9dSMark McLoughlin 	/* If the host supports indirect descriptor tables, and we have multiple
2209fa29b9dSMark McLoughlin 	 * buffers, then go indirect. FIXME: tune this threshold */
22113816c76SRusty Russell 	if (vq->indirect && total_sg > 1 && vq->vq.num_free) {
22213816c76SRusty Russell 		head = vring_add_indirect(vq, sgs, next, total_sg, total_out,
22313816c76SRusty Russell 					  total_in,
22413816c76SRusty Russell 					  out_sgs, in_sgs, gfp);
2251fe9b6feSMichael S. Tsirkin 		if (likely(head >= 0))
2269fa29b9dSMark McLoughlin 			goto add_head;
2279fa29b9dSMark McLoughlin 	}
2289fa29b9dSMark McLoughlin 
22913816c76SRusty Russell 	BUG_ON(total_sg > vq->vring.num);
23013816c76SRusty Russell 	BUG_ON(total_sg == 0);
2310a8a69ddSRusty Russell 
23213816c76SRusty Russell 	if (vq->vq.num_free < total_sg) {
2330a8a69ddSRusty Russell 		pr_debug("Can't add buf len %i - avail = %i\n",
23413816c76SRusty Russell 			 total_sg, vq->vq.num_free);
23544653eaeSRusty Russell 		/* FIXME: for historical reasons, we force a notify here if
23644653eaeSRusty Russell 		 * there are outgoing parts to the buffer.  Presumably the
23744653eaeSRusty Russell 		 * host should service the ring ASAP. */
23813816c76SRusty Russell 		if (out_sgs)
239426e3e0aSRusty Russell 			vq->notify(&vq->vq);
2400a8a69ddSRusty Russell 		END_USE(vq);
2410a8a69ddSRusty Russell 		return -ENOSPC;
2420a8a69ddSRusty Russell 	}
2430a8a69ddSRusty Russell 
2440a8a69ddSRusty Russell 	/* We're about to use some buffers from the free list. */
24513816c76SRusty Russell 	vq->vq.num_free -= total_sg;
2460a8a69ddSRusty Russell 
24713816c76SRusty Russell 	head = i = vq->free_head;
24813816c76SRusty Russell 	for (n = 0; n < out_sgs; n++) {
24913816c76SRusty Russell 		for (sg = sgs[n]; sg; sg = next(sg, &total_out)) {
2500a8a69ddSRusty Russell 			vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
25115f9c890SRusty Russell 			vq->vring.desc[i].addr = sg_phys(sg);
2520a8a69ddSRusty Russell 			vq->vring.desc[i].len = sg->length;
2530a8a69ddSRusty Russell 			prev = i;
25413816c76SRusty Russell 			i = vq->vring.desc[i].next;
2550a8a69ddSRusty Russell 		}
25613816c76SRusty Russell 	}
25713816c76SRusty Russell 	for (; n < (out_sgs + in_sgs); n++) {
25813816c76SRusty Russell 		for (sg = sgs[n]; sg; sg = next(sg, &total_in)) {
2590a8a69ddSRusty Russell 			vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
26015f9c890SRusty Russell 			vq->vring.desc[i].addr = sg_phys(sg);
2610a8a69ddSRusty Russell 			vq->vring.desc[i].len = sg->length;
2620a8a69ddSRusty Russell 			prev = i;
26313816c76SRusty Russell 			i = vq->vring.desc[i].next;
26413816c76SRusty Russell 		}
2650a8a69ddSRusty Russell 	}
2660a8a69ddSRusty Russell 	/* Last one doesn't continue. */
2670a8a69ddSRusty Russell 	vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
2680a8a69ddSRusty Russell 
2690a8a69ddSRusty Russell 	/* Update free pointer */
2700a8a69ddSRusty Russell 	vq->free_head = i;
2710a8a69ddSRusty Russell 
2729fa29b9dSMark McLoughlin add_head:
2730a8a69ddSRusty Russell 	/* Set token. */
2740a8a69ddSRusty Russell 	vq->data[head] = data;
2750a8a69ddSRusty Russell 
2760a8a69ddSRusty Russell 	/* Put entry in available array (but don't update avail->idx until they
2773b720b8cSRusty Russell 	 * do sync). */
278ee7cd898SRusty Russell 	avail = (vq->vring.avail->idx & (vq->vring.num-1));
2790a8a69ddSRusty Russell 	vq->vring.avail->ring[avail] = head;
2800a8a69ddSRusty Russell 
281ee7cd898SRusty Russell 	/* Descriptors and available array need to be set before we expose the
282ee7cd898SRusty Russell 	 * new available array entries. */
283a9a0fef7SRusty Russell 	virtio_wmb(vq->weak_barriers);
284ee7cd898SRusty Russell 	vq->vring.avail->idx++;
285ee7cd898SRusty Russell 	vq->num_added++;
286ee7cd898SRusty Russell 
287ee7cd898SRusty Russell 	/* This is very unlikely, but theoretically possible.  Kick
288ee7cd898SRusty Russell 	 * just in case. */
289ee7cd898SRusty Russell 	if (unlikely(vq->num_added == (1 << 16) - 1))
290ee7cd898SRusty Russell 		virtqueue_kick(_vq);
291ee7cd898SRusty Russell 
2920a8a69ddSRusty Russell 	pr_debug("Added buffer head %i to %p\n", head, vq);
2930a8a69ddSRusty Russell 	END_USE(vq);
2943c1b27d5SRusty Russell 
29598e8c6bcSRusty Russell 	return 0;
2960a8a69ddSRusty Russell }
29713816c76SRusty Russell 
29813816c76SRusty Russell /**
29913816c76SRusty Russell  * virtqueue_add_buf - expose buffer to other end
30013816c76SRusty Russell  * @vq: the struct virtqueue we're talking about.
30113816c76SRusty Russell  * @sg: the description of the buffer(s).
30213816c76SRusty Russell  * @out_num: the number of sg readable by other side
30313816c76SRusty Russell  * @in_num: the number of sg which are writable (after readable ones)
30413816c76SRusty Russell  * @data: the token identifying the buffer.
30513816c76SRusty Russell  * @gfp: how to do memory allocations (if necessary).
30613816c76SRusty Russell  *
30713816c76SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
30813816c76SRusty Russell  * at the same time (except where noted).
30913816c76SRusty Russell  *
31013816c76SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
31113816c76SRusty Russell  */
31213816c76SRusty Russell int virtqueue_add_buf(struct virtqueue *_vq,
31313816c76SRusty Russell 		      struct scatterlist sg[],
31413816c76SRusty Russell 		      unsigned int out,
31513816c76SRusty Russell 		      unsigned int in,
31613816c76SRusty Russell 		      void *data,
31713816c76SRusty Russell 		      gfp_t gfp)
31813816c76SRusty Russell {
31913816c76SRusty Russell 	struct scatterlist *sgs[2];
32013816c76SRusty Russell 
32113816c76SRusty Russell 	sgs[0] = sg;
32213816c76SRusty Russell 	sgs[1] = sg + out;
32313816c76SRusty Russell 
32413816c76SRusty Russell 	return virtqueue_add(_vq, sgs, sg_next_arr,
32513816c76SRusty Russell 			     out, in, out ? 1 : 0, in ? 1 : 0, data, gfp);
32613816c76SRusty Russell }
327f96fde41SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_buf);
3280a8a69ddSRusty Russell 
3295dfc1762SRusty Russell /**
33013816c76SRusty Russell  * virtqueue_add_sgs - expose buffers to other end
33113816c76SRusty Russell  * @vq: the struct virtqueue we're talking about.
33213816c76SRusty Russell  * @sgs: array of terminated scatterlists.
33313816c76SRusty Russell  * @out_num: the number of scatterlists readable by other side
33413816c76SRusty Russell  * @in_num: the number of scatterlists which are writable (after readable ones)
33513816c76SRusty Russell  * @data: the token identifying the buffer.
33613816c76SRusty Russell  * @gfp: how to do memory allocations (if necessary).
33713816c76SRusty Russell  *
33813816c76SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
33913816c76SRusty Russell  * at the same time (except where noted).
34013816c76SRusty Russell  *
34113816c76SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
34213816c76SRusty Russell  */
34313816c76SRusty Russell int virtqueue_add_sgs(struct virtqueue *_vq,
34413816c76SRusty Russell 		      struct scatterlist *sgs[],
34513816c76SRusty Russell 		      unsigned int out_sgs,
34613816c76SRusty Russell 		      unsigned int in_sgs,
34713816c76SRusty Russell 		      void *data,
34813816c76SRusty Russell 		      gfp_t gfp)
34913816c76SRusty Russell {
35013816c76SRusty Russell 	unsigned int i, total_out, total_in;
35113816c76SRusty Russell 
35213816c76SRusty Russell 	/* Count them first. */
35313816c76SRusty Russell 	for (i = total_out = total_in = 0; i < out_sgs; i++) {
35413816c76SRusty Russell 		struct scatterlist *sg;
35513816c76SRusty Russell 		for (sg = sgs[i]; sg; sg = sg_next(sg))
35613816c76SRusty Russell 			total_out++;
35713816c76SRusty Russell 	}
35813816c76SRusty Russell 	for (; i < out_sgs + in_sgs; i++) {
35913816c76SRusty Russell 		struct scatterlist *sg;
36013816c76SRusty Russell 		for (sg = sgs[i]; sg; sg = sg_next(sg))
36113816c76SRusty Russell 			total_in++;
36213816c76SRusty Russell 	}
36313816c76SRusty Russell 	return virtqueue_add(_vq, sgs, sg_next_chained,
36413816c76SRusty Russell 			     total_out, total_in, out_sgs, in_sgs, data, gfp);
36513816c76SRusty Russell }
36613816c76SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
36713816c76SRusty Russell 
36813816c76SRusty Russell /**
369282edb36SRusty Russell  * virtqueue_add_outbuf - expose output buffers to other end
370282edb36SRusty Russell  * @vq: the struct virtqueue we're talking about.
371282edb36SRusty Russell  * @sgs: array of scatterlists (need not be terminated!)
372282edb36SRusty Russell  * @num: the number of scatterlists readable by other side
373282edb36SRusty Russell  * @data: the token identifying the buffer.
374282edb36SRusty Russell  * @gfp: how to do memory allocations (if necessary).
375282edb36SRusty Russell  *
376282edb36SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
377282edb36SRusty Russell  * at the same time (except where noted).
378282edb36SRusty Russell  *
379282edb36SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
380282edb36SRusty Russell  */
381282edb36SRusty Russell int virtqueue_add_outbuf(struct virtqueue *vq,
382282edb36SRusty Russell 			 struct scatterlist sg[], unsigned int num,
383282edb36SRusty Russell 			 void *data,
384282edb36SRusty Russell 			 gfp_t gfp)
385282edb36SRusty Russell {
386282edb36SRusty Russell 	return virtqueue_add(vq, &sg, sg_next_arr, num, 0, 1, 0, data, gfp);
387282edb36SRusty Russell }
388282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
389282edb36SRusty Russell 
390282edb36SRusty Russell /**
391282edb36SRusty Russell  * virtqueue_add_inbuf - expose input buffers to other end
392282edb36SRusty Russell  * @vq: the struct virtqueue we're talking about.
393282edb36SRusty Russell  * @sgs: array of scatterlists (need not be terminated!)
394282edb36SRusty Russell  * @num: the number of scatterlists writable by other side
395282edb36SRusty Russell  * @data: the token identifying the buffer.
396282edb36SRusty Russell  * @gfp: how to do memory allocations (if necessary).
397282edb36SRusty Russell  *
398282edb36SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
399282edb36SRusty Russell  * at the same time (except where noted).
400282edb36SRusty Russell  *
401282edb36SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
402282edb36SRusty Russell  */
403282edb36SRusty Russell int virtqueue_add_inbuf(struct virtqueue *vq,
404282edb36SRusty Russell 			struct scatterlist sg[], unsigned int num,
405282edb36SRusty Russell 			void *data,
406282edb36SRusty Russell 			gfp_t gfp)
407282edb36SRusty Russell {
408282edb36SRusty Russell 	return virtqueue_add(vq, &sg, sg_next_arr, 0, num, 0, 1, data, gfp);
409282edb36SRusty Russell }
410282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
411282edb36SRusty Russell 
412282edb36SRusty Russell /**
41341f0377fSRusty Russell  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
4145dfc1762SRusty Russell  * @vq: the struct virtqueue
4155dfc1762SRusty Russell  *
41641f0377fSRusty Russell  * Instead of virtqueue_kick(), you can do:
41741f0377fSRusty Russell  *	if (virtqueue_kick_prepare(vq))
41841f0377fSRusty Russell  *		virtqueue_notify(vq);
4195dfc1762SRusty Russell  *
42041f0377fSRusty Russell  * This is sometimes useful because the virtqueue_kick_prepare() needs
42141f0377fSRusty Russell  * to be serialized, but the actual virtqueue_notify() call does not.
4225dfc1762SRusty Russell  */
42341f0377fSRusty Russell bool virtqueue_kick_prepare(struct virtqueue *_vq)
4240a8a69ddSRusty Russell {
4250a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
426a5c262c5SMichael S. Tsirkin 	u16 new, old;
42741f0377fSRusty Russell 	bool needs_kick;
42841f0377fSRusty Russell 
4290a8a69ddSRusty Russell 	START_USE(vq);
430a72caae2SJason Wang 	/* We need to expose available array entries before checking avail
431a72caae2SJason Wang 	 * event. */
432a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
4330a8a69ddSRusty Russell 
434ee7cd898SRusty Russell 	old = vq->vring.avail->idx - vq->num_added;
435ee7cd898SRusty Russell 	new = vq->vring.avail->idx;
4360a8a69ddSRusty Russell 	vq->num_added = 0;
4370a8a69ddSRusty Russell 
438e93300b1SRusty Russell #ifdef DEBUG
439e93300b1SRusty Russell 	if (vq->last_add_time_valid) {
440e93300b1SRusty Russell 		WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
441e93300b1SRusty Russell 					      vq->last_add_time)) > 100);
442e93300b1SRusty Russell 	}
443e93300b1SRusty Russell 	vq->last_add_time_valid = false;
444e93300b1SRusty Russell #endif
445e93300b1SRusty Russell 
44641f0377fSRusty Russell 	if (vq->event) {
44741f0377fSRusty Russell 		needs_kick = vring_need_event(vring_avail_event(&vq->vring),
44841f0377fSRusty Russell 					      new, old);
44941f0377fSRusty Russell 	} else {
45041f0377fSRusty Russell 		needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
45141f0377fSRusty Russell 	}
4520a8a69ddSRusty Russell 	END_USE(vq);
45341f0377fSRusty Russell 	return needs_kick;
45441f0377fSRusty Russell }
45541f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
45641f0377fSRusty Russell 
45741f0377fSRusty Russell /**
45841f0377fSRusty Russell  * virtqueue_notify - second half of split virtqueue_kick call.
45941f0377fSRusty Russell  * @vq: the struct virtqueue
46041f0377fSRusty Russell  *
46141f0377fSRusty Russell  * This does not need to be serialized.
46241f0377fSRusty Russell  */
46341f0377fSRusty Russell void virtqueue_notify(struct virtqueue *_vq)
46441f0377fSRusty Russell {
46541f0377fSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
46641f0377fSRusty Russell 
46741f0377fSRusty Russell 	/* Prod other side to tell it about changes. */
46841f0377fSRusty Russell 	vq->notify(_vq);
46941f0377fSRusty Russell }
47041f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify);
47141f0377fSRusty Russell 
47241f0377fSRusty Russell /**
47341f0377fSRusty Russell  * virtqueue_kick - update after add_buf
47441f0377fSRusty Russell  * @vq: the struct virtqueue
47541f0377fSRusty Russell  *
47641f0377fSRusty Russell  * After one or more virtqueue_add_buf calls, invoke this to kick
47741f0377fSRusty Russell  * the other side.
47841f0377fSRusty Russell  *
47941f0377fSRusty Russell  * Caller must ensure we don't call this with other virtqueue
48041f0377fSRusty Russell  * operations at the same time (except where noted).
48141f0377fSRusty Russell  */
48241f0377fSRusty Russell void virtqueue_kick(struct virtqueue *vq)
48341f0377fSRusty Russell {
48441f0377fSRusty Russell 	if (virtqueue_kick_prepare(vq))
48541f0377fSRusty Russell 		virtqueue_notify(vq);
4860a8a69ddSRusty Russell }
4877c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick);
4880a8a69ddSRusty Russell 
4890a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
4900a8a69ddSRusty Russell {
4910a8a69ddSRusty Russell 	unsigned int i;
4920a8a69ddSRusty Russell 
4930a8a69ddSRusty Russell 	/* Clear data ptr. */
4940a8a69ddSRusty Russell 	vq->data[head] = NULL;
4950a8a69ddSRusty Russell 
4960a8a69ddSRusty Russell 	/* Put back on free list: find end */
4970a8a69ddSRusty Russell 	i = head;
4989fa29b9dSMark McLoughlin 
4999fa29b9dSMark McLoughlin 	/* Free the indirect table */
5009fa29b9dSMark McLoughlin 	if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
5019fa29b9dSMark McLoughlin 		kfree(phys_to_virt(vq->vring.desc[i].addr));
5029fa29b9dSMark McLoughlin 
5030a8a69ddSRusty Russell 	while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
5040a8a69ddSRusty Russell 		i = vq->vring.desc[i].next;
50506ca287dSRusty Russell 		vq->vq.num_free++;
5060a8a69ddSRusty Russell 	}
5070a8a69ddSRusty Russell 
5080a8a69ddSRusty Russell 	vq->vring.desc[i].next = vq->free_head;
5090a8a69ddSRusty Russell 	vq->free_head = head;
5100a8a69ddSRusty Russell 	/* Plus final descriptor */
51106ca287dSRusty Russell 	vq->vq.num_free++;
5120a8a69ddSRusty Russell }
5130a8a69ddSRusty Russell 
5140a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq)
5150a8a69ddSRusty Russell {
5160a8a69ddSRusty Russell 	return vq->last_used_idx != vq->vring.used->idx;
5170a8a69ddSRusty Russell }
5180a8a69ddSRusty Russell 
5195dfc1762SRusty Russell /**
5205dfc1762SRusty Russell  * virtqueue_get_buf - get the next used buffer
5215dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5225dfc1762SRusty Russell  * @len: the length written into the buffer
5235dfc1762SRusty Russell  *
5245dfc1762SRusty Russell  * If the driver wrote data into the buffer, @len will be set to the
5255dfc1762SRusty Russell  * amount written.  This means you don't need to clear the buffer
5265dfc1762SRusty Russell  * beforehand to ensure there's no data leakage in the case of short
5275dfc1762SRusty Russell  * writes.
5285dfc1762SRusty Russell  *
5295dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
5305dfc1762SRusty Russell  * operations at the same time (except where noted).
5315dfc1762SRusty Russell  *
5325dfc1762SRusty Russell  * Returns NULL if there are no used buffers, or the "data" token
533f96fde41SRusty Russell  * handed to virtqueue_add_buf().
5345dfc1762SRusty Russell  */
5357c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
5360a8a69ddSRusty Russell {
5370a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
5380a8a69ddSRusty Russell 	void *ret;
5390a8a69ddSRusty Russell 	unsigned int i;
5403b720b8cSRusty Russell 	u16 last_used;
5410a8a69ddSRusty Russell 
5420a8a69ddSRusty Russell 	START_USE(vq);
5430a8a69ddSRusty Russell 
5445ef82752SRusty Russell 	if (unlikely(vq->broken)) {
5455ef82752SRusty Russell 		END_USE(vq);
5465ef82752SRusty Russell 		return NULL;
5475ef82752SRusty Russell 	}
5485ef82752SRusty Russell 
5490a8a69ddSRusty Russell 	if (!more_used(vq)) {
5500a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
5510a8a69ddSRusty Russell 		END_USE(vq);
5520a8a69ddSRusty Russell 		return NULL;
5530a8a69ddSRusty Russell 	}
5540a8a69ddSRusty Russell 
5552d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
556a9a0fef7SRusty Russell 	virtio_rmb(vq->weak_barriers);
5572d61ba95SMichael S. Tsirkin 
5583b720b8cSRusty Russell 	last_used = (vq->last_used_idx & (vq->vring.num - 1));
5593b720b8cSRusty Russell 	i = vq->vring.used->ring[last_used].id;
5603b720b8cSRusty Russell 	*len = vq->vring.used->ring[last_used].len;
5610a8a69ddSRusty Russell 
5620a8a69ddSRusty Russell 	if (unlikely(i >= vq->vring.num)) {
5630a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
5640a8a69ddSRusty Russell 		return NULL;
5650a8a69ddSRusty Russell 	}
5660a8a69ddSRusty Russell 	if (unlikely(!vq->data[i])) {
5670a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
5680a8a69ddSRusty Russell 		return NULL;
5690a8a69ddSRusty Russell 	}
5700a8a69ddSRusty Russell 
5710a8a69ddSRusty Russell 	/* detach_buf clears data, so grab it now. */
5720a8a69ddSRusty Russell 	ret = vq->data[i];
5730a8a69ddSRusty Russell 	detach_buf(vq, i);
5740a8a69ddSRusty Russell 	vq->last_used_idx++;
575a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
576a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
577a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
578a5c262c5SMichael S. Tsirkin 	if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
579a5c262c5SMichael S. Tsirkin 		vring_used_event(&vq->vring) = vq->last_used_idx;
580a9a0fef7SRusty Russell 		virtio_mb(vq->weak_barriers);
581a5c262c5SMichael S. Tsirkin 	}
582a5c262c5SMichael S. Tsirkin 
583e93300b1SRusty Russell #ifdef DEBUG
584e93300b1SRusty Russell 	vq->last_add_time_valid = false;
585e93300b1SRusty Russell #endif
586e93300b1SRusty Russell 
5870a8a69ddSRusty Russell 	END_USE(vq);
5880a8a69ddSRusty Russell 	return ret;
5890a8a69ddSRusty Russell }
5907c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf);
5910a8a69ddSRusty Russell 
5925dfc1762SRusty Russell /**
5935dfc1762SRusty Russell  * virtqueue_disable_cb - disable callbacks
5945dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5955dfc1762SRusty Russell  *
5965dfc1762SRusty Russell  * Note that this is not necessarily synchronous, hence unreliable and only
5975dfc1762SRusty Russell  * useful as an optimization.
5985dfc1762SRusty Russell  *
5995dfc1762SRusty Russell  * Unlike other operations, this need not be serialized.
6005dfc1762SRusty Russell  */
6017c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq)
60218445c4dSRusty Russell {
60318445c4dSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
60418445c4dSRusty Russell 
60518445c4dSRusty Russell 	vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
60618445c4dSRusty Russell }
6077c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
60818445c4dSRusty Russell 
6095dfc1762SRusty Russell /**
610*cc229884SMichael S. Tsirkin  * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
611*cc229884SMichael S. Tsirkin  * @vq: the struct virtqueue we're talking about.
612*cc229884SMichael S. Tsirkin  *
613*cc229884SMichael S. Tsirkin  * This re-enables callbacks; it returns current queue state
614*cc229884SMichael S. Tsirkin  * in an opaque unsigned value. This value should be later tested by
615*cc229884SMichael S. Tsirkin  * virtqueue_poll, to detect a possible race between the driver checking for
616*cc229884SMichael S. Tsirkin  * more work, and enabling callbacks.
617*cc229884SMichael S. Tsirkin  *
618*cc229884SMichael S. Tsirkin  * Caller must ensure we don't call this with other virtqueue
619*cc229884SMichael S. Tsirkin  * operations at the same time (except where noted).
620*cc229884SMichael S. Tsirkin  */
621*cc229884SMichael S. Tsirkin unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
622*cc229884SMichael S. Tsirkin {
623*cc229884SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
624*cc229884SMichael S. Tsirkin 	u16 last_used_idx;
625*cc229884SMichael S. Tsirkin 
626*cc229884SMichael S. Tsirkin 	START_USE(vq);
627*cc229884SMichael S. Tsirkin 
628*cc229884SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
629*cc229884SMichael S. Tsirkin 	 * more to do. */
630*cc229884SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
631*cc229884SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
632*cc229884SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
633*cc229884SMichael S. Tsirkin 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
634*cc229884SMichael S. Tsirkin 	vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
635*cc229884SMichael S. Tsirkin 	END_USE(vq);
636*cc229884SMichael S. Tsirkin 	return last_used_idx;
637*cc229884SMichael S. Tsirkin }
638*cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
639*cc229884SMichael S. Tsirkin 
640*cc229884SMichael S. Tsirkin /**
641*cc229884SMichael S. Tsirkin  * virtqueue_poll - query pending used buffers
642*cc229884SMichael S. Tsirkin  * @vq: the struct virtqueue we're talking about.
643*cc229884SMichael S. Tsirkin  * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
644*cc229884SMichael S. Tsirkin  *
645*cc229884SMichael S. Tsirkin  * Returns "true" if there are pending used buffers in the queue.
646*cc229884SMichael S. Tsirkin  *
647*cc229884SMichael S. Tsirkin  * This does not need to be serialized.
648*cc229884SMichael S. Tsirkin  */
649*cc229884SMichael S. Tsirkin bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
650*cc229884SMichael S. Tsirkin {
651*cc229884SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
652*cc229884SMichael S. Tsirkin 
653*cc229884SMichael S. Tsirkin 	virtio_mb(vq->weak_barriers);
654*cc229884SMichael S. Tsirkin 	return (u16)last_used_idx != vq->vring.used->idx;
655*cc229884SMichael S. Tsirkin }
656*cc229884SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_poll);
657*cc229884SMichael S. Tsirkin 
658*cc229884SMichael S. Tsirkin /**
6595dfc1762SRusty Russell  * virtqueue_enable_cb - restart callbacks after disable_cb.
6605dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
6615dfc1762SRusty Russell  *
6625dfc1762SRusty Russell  * This re-enables callbacks; it returns "false" if there are pending
6635dfc1762SRusty Russell  * buffers in the queue, to detect a possible race between the driver
6645dfc1762SRusty Russell  * checking for more work, and enabling callbacks.
6655dfc1762SRusty Russell  *
6665dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
6675dfc1762SRusty Russell  * operations at the same time (except where noted).
6685dfc1762SRusty Russell  */
6697c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq)
6700a8a69ddSRusty Russell {
671*cc229884SMichael S. Tsirkin 	unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
672*cc229884SMichael S. Tsirkin 	return !virtqueue_poll(_vq, last_used_idx);
6730a8a69ddSRusty Russell }
6747c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
6750a8a69ddSRusty Russell 
6765dfc1762SRusty Russell /**
6775dfc1762SRusty Russell  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
6785dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
6795dfc1762SRusty Russell  *
6805dfc1762SRusty Russell  * This re-enables callbacks but hints to the other side to delay
6815dfc1762SRusty Russell  * interrupts until most of the available buffers have been processed;
6825dfc1762SRusty Russell  * it returns "false" if there are many pending buffers in the queue,
6835dfc1762SRusty Russell  * to detect a possible race between the driver checking for more work,
6845dfc1762SRusty Russell  * and enabling callbacks.
6855dfc1762SRusty Russell  *
6865dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
6875dfc1762SRusty Russell  * operations at the same time (except where noted).
6885dfc1762SRusty Russell  */
6897ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
6907ab358c2SMichael S. Tsirkin {
6917ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
6927ab358c2SMichael S. Tsirkin 	u16 bufs;
6937ab358c2SMichael S. Tsirkin 
6947ab358c2SMichael S. Tsirkin 	START_USE(vq);
6957ab358c2SMichael S. Tsirkin 
6967ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
6977ab358c2SMichael S. Tsirkin 	 * more to do. */
6987ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
6997ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
7007ab358c2SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
7017ab358c2SMichael S. Tsirkin 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
7027ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
7037ab358c2SMichael S. Tsirkin 	bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
7047ab358c2SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
705a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
7067ab358c2SMichael S. Tsirkin 	if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
7077ab358c2SMichael S. Tsirkin 		END_USE(vq);
7087ab358c2SMichael S. Tsirkin 		return false;
7097ab358c2SMichael S. Tsirkin 	}
7107ab358c2SMichael S. Tsirkin 
7117ab358c2SMichael S. Tsirkin 	END_USE(vq);
7127ab358c2SMichael S. Tsirkin 	return true;
7137ab358c2SMichael S. Tsirkin }
7147ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
7157ab358c2SMichael S. Tsirkin 
7165dfc1762SRusty Russell /**
7175dfc1762SRusty Russell  * virtqueue_detach_unused_buf - detach first unused buffer
7185dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
7195dfc1762SRusty Russell  *
720f96fde41SRusty Russell  * Returns NULL or the "data" token handed to virtqueue_add_buf().
7215dfc1762SRusty Russell  * This is not valid on an active queue; it is useful only for device
7225dfc1762SRusty Russell  * shutdown.
7235dfc1762SRusty Russell  */
7247c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
725c021eac4SShirley Ma {
726c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
727c021eac4SShirley Ma 	unsigned int i;
728c021eac4SShirley Ma 	void *buf;
729c021eac4SShirley Ma 
730c021eac4SShirley Ma 	START_USE(vq);
731c021eac4SShirley Ma 
732c021eac4SShirley Ma 	for (i = 0; i < vq->vring.num; i++) {
733c021eac4SShirley Ma 		if (!vq->data[i])
734c021eac4SShirley Ma 			continue;
735c021eac4SShirley Ma 		/* detach_buf clears data, so grab it now. */
736c021eac4SShirley Ma 		buf = vq->data[i];
737c021eac4SShirley Ma 		detach_buf(vq, i);
738b3258ff1SAmit Shah 		vq->vring.avail->idx--;
739c021eac4SShirley Ma 		END_USE(vq);
740c021eac4SShirley Ma 		return buf;
741c021eac4SShirley Ma 	}
742c021eac4SShirley Ma 	/* That should have freed everything. */
74306ca287dSRusty Russell 	BUG_ON(vq->vq.num_free != vq->vring.num);
744c021eac4SShirley Ma 
745c021eac4SShirley Ma 	END_USE(vq);
746c021eac4SShirley Ma 	return NULL;
747c021eac4SShirley Ma }
7487c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
749c021eac4SShirley Ma 
7500a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
7510a8a69ddSRusty Russell {
7520a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
7530a8a69ddSRusty Russell 
7540a8a69ddSRusty Russell 	if (!more_used(vq)) {
7550a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
7560a8a69ddSRusty Russell 		return IRQ_NONE;
7570a8a69ddSRusty Russell 	}
7580a8a69ddSRusty Russell 
7590a8a69ddSRusty Russell 	if (unlikely(vq->broken))
7600a8a69ddSRusty Russell 		return IRQ_HANDLED;
7610a8a69ddSRusty Russell 
7620a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
76318445c4dSRusty Russell 	if (vq->vq.callback)
76418445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
7650a8a69ddSRusty Russell 
7660a8a69ddSRusty Russell 	return IRQ_HANDLED;
7670a8a69ddSRusty Russell }
768c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
7690a8a69ddSRusty Russell 
77017bb6d40SJason Wang struct virtqueue *vring_new_virtqueue(unsigned int index,
77117bb6d40SJason Wang 				      unsigned int num,
77287c7d57cSRusty Russell 				      unsigned int vring_align,
7730a8a69ddSRusty Russell 				      struct virtio_device *vdev,
7747b21e34fSRusty Russell 				      bool weak_barriers,
7750a8a69ddSRusty Russell 				      void *pages,
7760a8a69ddSRusty Russell 				      void (*notify)(struct virtqueue *),
7779499f5e7SRusty Russell 				      void (*callback)(struct virtqueue *),
7789499f5e7SRusty Russell 				      const char *name)
7790a8a69ddSRusty Russell {
7800a8a69ddSRusty Russell 	struct vring_virtqueue *vq;
7810a8a69ddSRusty Russell 	unsigned int i;
7820a8a69ddSRusty Russell 
78342b36cc0SRusty Russell 	/* We assume num is a power of 2. */
78442b36cc0SRusty Russell 	if (num & (num - 1)) {
78542b36cc0SRusty Russell 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
78642b36cc0SRusty Russell 		return NULL;
78742b36cc0SRusty Russell 	}
78842b36cc0SRusty Russell 
7890a8a69ddSRusty Russell 	vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
7900a8a69ddSRusty Russell 	if (!vq)
7910a8a69ddSRusty Russell 		return NULL;
7920a8a69ddSRusty Russell 
79387c7d57cSRusty Russell 	vring_init(&vq->vring, num, pages, vring_align);
7940a8a69ddSRusty Russell 	vq->vq.callback = callback;
7950a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
7969499f5e7SRusty Russell 	vq->vq.name = name;
79706ca287dSRusty Russell 	vq->vq.num_free = num;
79806ca287dSRusty Russell 	vq->vq.index = index;
7990a8a69ddSRusty Russell 	vq->notify = notify;
8007b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
8010a8a69ddSRusty Russell 	vq->broken = false;
8020a8a69ddSRusty Russell 	vq->last_used_idx = 0;
8030a8a69ddSRusty Russell 	vq->num_added = 0;
8049499f5e7SRusty Russell 	list_add_tail(&vq->vq.list, &vdev->vqs);
8050a8a69ddSRusty Russell #ifdef DEBUG
8060a8a69ddSRusty Russell 	vq->in_use = false;
807e93300b1SRusty Russell 	vq->last_add_time_valid = false;
8080a8a69ddSRusty Russell #endif
8090a8a69ddSRusty Russell 
8109fa29b9dSMark McLoughlin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
811a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
8129fa29b9dSMark McLoughlin 
8130a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
8140a8a69ddSRusty Russell 	if (!callback)
8150a8a69ddSRusty Russell 		vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
8160a8a69ddSRusty Russell 
8170a8a69ddSRusty Russell 	/* Put everything in free lists. */
8180a8a69ddSRusty Russell 	vq->free_head = 0;
8193b870624SAmit Shah 	for (i = 0; i < num-1; i++) {
8200a8a69ddSRusty Russell 		vq->vring.desc[i].next = i+1;
8213b870624SAmit Shah 		vq->data[i] = NULL;
8223b870624SAmit Shah 	}
8233b870624SAmit Shah 	vq->data[i] = NULL;
8240a8a69ddSRusty Russell 
8250a8a69ddSRusty Russell 	return &vq->vq;
8260a8a69ddSRusty Russell }
827c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
8280a8a69ddSRusty Russell 
8290a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq)
8300a8a69ddSRusty Russell {
8319499f5e7SRusty Russell 	list_del(&vq->list);
8320a8a69ddSRusty Russell 	kfree(to_vvq(vq));
8330a8a69ddSRusty Russell }
834c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
8350a8a69ddSRusty Russell 
836e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
837e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
838e34f8725SRusty Russell {
839e34f8725SRusty Russell 	unsigned int i;
840e34f8725SRusty Russell 
841e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
842e34f8725SRusty Russell 		switch (i) {
8439fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
8449fa29b9dSMark McLoughlin 			break;
845a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
846a5c262c5SMichael S. Tsirkin 			break;
847e34f8725SRusty Russell 		default:
848e34f8725SRusty Russell 			/* We don't understand this bit. */
849e34f8725SRusty Russell 			clear_bit(i, vdev->features);
850e34f8725SRusty Russell 		}
851e34f8725SRusty Russell 	}
852e34f8725SRusty Russell }
853e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
854e34f8725SRusty Russell 
8555dfc1762SRusty Russell /**
8565dfc1762SRusty Russell  * virtqueue_get_vring_size - return the size of the virtqueue's vring
8575dfc1762SRusty Russell  * @vq: the struct virtqueue containing the vring of interest.
8585dfc1762SRusty Russell  *
8595dfc1762SRusty Russell  * Returns the size of the vring.  This is mainly used for boasting to
8605dfc1762SRusty Russell  * userspace.  Unlike other operations, this need not be serialized.
8615dfc1762SRusty Russell  */
8628f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
8638f9f4668SRick Jones {
8648f9f4668SRick Jones 
8658f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
8668f9f4668SRick Jones 
8678f9f4668SRick Jones 	return vq->vring.num;
8688f9f4668SRick Jones }
8698f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
8708f9f4668SRick Jones 
871c6fd4701SRusty Russell MODULE_LICENSE("GPL");
872