xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision 13816c768d46586e925b22736992258d6105ad2c)
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 
101*13816c76SRusty Russell static inline struct scatterlist *sg_next_chained(struct scatterlist *sg,
102*13816c76SRusty Russell 						  unsigned int *count)
103*13816c76SRusty Russell {
104*13816c76SRusty Russell 	return sg_next(sg);
105*13816c76SRusty Russell }
106*13816c76SRusty Russell 
107*13816c76SRusty Russell static inline struct scatterlist *sg_next_arr(struct scatterlist *sg,
108*13816c76SRusty Russell 					      unsigned int *count)
109*13816c76SRusty Russell {
110*13816c76SRusty Russell 	if (--(*count) == 0)
111*13816c76SRusty Russell 		return NULL;
112*13816c76SRusty Russell 	return sg + 1;
113*13816c76SRusty Russell }
114*13816c76SRusty Russell 
1159fa29b9dSMark McLoughlin /* Set up an indirect table of descriptors and add it to the queue. */
116*13816c76SRusty Russell static inline int vring_add_indirect(struct vring_virtqueue *vq,
117*13816c76SRusty Russell 				     struct scatterlist *sgs[],
118*13816c76SRusty Russell 				     struct scatterlist *(*next)
119*13816c76SRusty Russell 				       (struct scatterlist *, unsigned int *),
120*13816c76SRusty Russell 				     unsigned int total_sg,
121*13816c76SRusty Russell 				     unsigned int total_out,
122*13816c76SRusty Russell 				     unsigned int total_in,
123*13816c76SRusty Russell 				     unsigned int out_sgs,
124*13816c76SRusty Russell 				     unsigned int in_sgs,
125bbd603efSMichael S. Tsirkin 				     gfp_t gfp)
1269fa29b9dSMark McLoughlin {
1279fa29b9dSMark McLoughlin 	struct vring_desc *desc;
1289fa29b9dSMark McLoughlin 	unsigned head;
129*13816c76SRusty Russell 	struct scatterlist *sg;
130*13816c76SRusty 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 
139*13816c76SRusty Russell 	desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
1409fa29b9dSMark McLoughlin 	if (!desc)
141686d3637SMichael S. Tsirkin 		return -ENOMEM;
1429fa29b9dSMark McLoughlin 
143*13816c76SRusty Russell 	/* Transfer entries from the sg lists into the indirect page */
144*13816c76SRusty Russell 	i = 0;
145*13816c76SRusty Russell 	for (n = 0; n < out_sgs; n++) {
146*13816c76SRusty 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;
151*13816c76SRusty Russell 			i++;
1529fa29b9dSMark McLoughlin 		}
153*13816c76SRusty Russell 	}
154*13816c76SRusty Russell 	for (; n < (out_sgs + in_sgs); n++) {
155*13816c76SRusty 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;
160*13816c76SRusty Russell 			i++;
1619fa29b9dSMark McLoughlin 		}
162*13816c76SRusty Russell 	}
163*13816c76SRusty 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 
184*13816c76SRusty Russell static inline int virtqueue_add(struct virtqueue *_vq,
185*13816c76SRusty Russell 				struct scatterlist *sgs[],
186*13816c76SRusty Russell 				struct scatterlist *(*next)
187*13816c76SRusty Russell 				  (struct scatterlist *, unsigned int *),
188*13816c76SRusty Russell 				unsigned int total_out,
189*13816c76SRusty Russell 				unsigned int total_in,
190*13816c76SRusty Russell 				unsigned int out_sgs,
191*13816c76SRusty 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);
196*13816c76SRusty Russell 	struct scatterlist *sg;
197*13816c76SRusty 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 
217*13816c76SRusty Russell 	total_sg = total_in + total_out;
218*13816c76SRusty Russell 
2199fa29b9dSMark McLoughlin 	/* If the host supports indirect descriptor tables, and we have multiple
2209fa29b9dSMark McLoughlin 	 * buffers, then go indirect. FIXME: tune this threshold */
221*13816c76SRusty Russell 	if (vq->indirect && total_sg > 1 && vq->vq.num_free) {
222*13816c76SRusty Russell 		head = vring_add_indirect(vq, sgs, next, total_sg, total_out,
223*13816c76SRusty Russell 					  total_in,
224*13816c76SRusty Russell 					  out_sgs, in_sgs, gfp);
2251fe9b6feSMichael S. Tsirkin 		if (likely(head >= 0))
2269fa29b9dSMark McLoughlin 			goto add_head;
2279fa29b9dSMark McLoughlin 	}
2289fa29b9dSMark McLoughlin 
229*13816c76SRusty Russell 	BUG_ON(total_sg > vq->vring.num);
230*13816c76SRusty Russell 	BUG_ON(total_sg == 0);
2310a8a69ddSRusty Russell 
232*13816c76SRusty Russell 	if (vq->vq.num_free < total_sg) {
2330a8a69ddSRusty Russell 		pr_debug("Can't add buf len %i - avail = %i\n",
234*13816c76SRusty 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. */
238*13816c76SRusty 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. */
245*13816c76SRusty Russell 	vq->vq.num_free -= total_sg;
2460a8a69ddSRusty Russell 
247*13816c76SRusty Russell 	head = i = vq->free_head;
248*13816c76SRusty Russell 	for (n = 0; n < out_sgs; n++) {
249*13816c76SRusty 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;
254*13816c76SRusty Russell 			i = vq->vring.desc[i].next;
2550a8a69ddSRusty Russell 		}
256*13816c76SRusty Russell 	}
257*13816c76SRusty Russell 	for (; n < (out_sgs + in_sgs); n++) {
258*13816c76SRusty 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;
263*13816c76SRusty Russell 			i = vq->vring.desc[i].next;
264*13816c76SRusty 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 }
297*13816c76SRusty Russell 
298*13816c76SRusty Russell /**
299*13816c76SRusty Russell  * virtqueue_add_buf - expose buffer to other end
300*13816c76SRusty Russell  * @vq: the struct virtqueue we're talking about.
301*13816c76SRusty Russell  * @sg: the description of the buffer(s).
302*13816c76SRusty Russell  * @out_num: the number of sg readable by other side
303*13816c76SRusty Russell  * @in_num: the number of sg which are writable (after readable ones)
304*13816c76SRusty Russell  * @data: the token identifying the buffer.
305*13816c76SRusty Russell  * @gfp: how to do memory allocations (if necessary).
306*13816c76SRusty Russell  *
307*13816c76SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
308*13816c76SRusty Russell  * at the same time (except where noted).
309*13816c76SRusty Russell  *
310*13816c76SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
311*13816c76SRusty Russell  */
312*13816c76SRusty Russell int virtqueue_add_buf(struct virtqueue *_vq,
313*13816c76SRusty Russell 		      struct scatterlist sg[],
314*13816c76SRusty Russell 		      unsigned int out,
315*13816c76SRusty Russell 		      unsigned int in,
316*13816c76SRusty Russell 		      void *data,
317*13816c76SRusty Russell 		      gfp_t gfp)
318*13816c76SRusty Russell {
319*13816c76SRusty Russell 	struct scatterlist *sgs[2];
320*13816c76SRusty Russell 
321*13816c76SRusty Russell 	sgs[0] = sg;
322*13816c76SRusty Russell 	sgs[1] = sg + out;
323*13816c76SRusty Russell 
324*13816c76SRusty Russell 	return virtqueue_add(_vq, sgs, sg_next_arr,
325*13816c76SRusty Russell 			     out, in, out ? 1 : 0, in ? 1 : 0, data, gfp);
326*13816c76SRusty Russell }
327f96fde41SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_buf);
3280a8a69ddSRusty Russell 
3295dfc1762SRusty Russell /**
330*13816c76SRusty Russell  * virtqueue_add_sgs - expose buffers to other end
331*13816c76SRusty Russell  * @vq: the struct virtqueue we're talking about.
332*13816c76SRusty Russell  * @sgs: array of terminated scatterlists.
333*13816c76SRusty Russell  * @out_num: the number of scatterlists readable by other side
334*13816c76SRusty Russell  * @in_num: the number of scatterlists which are writable (after readable ones)
335*13816c76SRusty Russell  * @data: the token identifying the buffer.
336*13816c76SRusty Russell  * @gfp: how to do memory allocations (if necessary).
337*13816c76SRusty Russell  *
338*13816c76SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
339*13816c76SRusty Russell  * at the same time (except where noted).
340*13816c76SRusty Russell  *
341*13816c76SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
342*13816c76SRusty Russell  */
343*13816c76SRusty Russell int virtqueue_add_sgs(struct virtqueue *_vq,
344*13816c76SRusty Russell 		      struct scatterlist *sgs[],
345*13816c76SRusty Russell 		      unsigned int out_sgs,
346*13816c76SRusty Russell 		      unsigned int in_sgs,
347*13816c76SRusty Russell 		      void *data,
348*13816c76SRusty Russell 		      gfp_t gfp)
349*13816c76SRusty Russell {
350*13816c76SRusty Russell 	unsigned int i, total_out, total_in;
351*13816c76SRusty Russell 
352*13816c76SRusty Russell 	/* Count them first. */
353*13816c76SRusty Russell 	for (i = total_out = total_in = 0; i < out_sgs; i++) {
354*13816c76SRusty Russell 		struct scatterlist *sg;
355*13816c76SRusty Russell 		for (sg = sgs[i]; sg; sg = sg_next(sg))
356*13816c76SRusty Russell 			total_out++;
357*13816c76SRusty Russell 	}
358*13816c76SRusty Russell 	for (; i < out_sgs + in_sgs; i++) {
359*13816c76SRusty Russell 		struct scatterlist *sg;
360*13816c76SRusty Russell 		for (sg = sgs[i]; sg; sg = sg_next(sg))
361*13816c76SRusty Russell 			total_in++;
362*13816c76SRusty Russell 	}
363*13816c76SRusty Russell 	return virtqueue_add(_vq, sgs, sg_next_chained,
364*13816c76SRusty Russell 			     total_out, total_in, out_sgs, in_sgs, data, gfp);
365*13816c76SRusty Russell }
366*13816c76SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
367*13816c76SRusty Russell 
368*13816c76SRusty Russell /**
36941f0377fSRusty Russell  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
3705dfc1762SRusty Russell  * @vq: the struct virtqueue
3715dfc1762SRusty Russell  *
37241f0377fSRusty Russell  * Instead of virtqueue_kick(), you can do:
37341f0377fSRusty Russell  *	if (virtqueue_kick_prepare(vq))
37441f0377fSRusty Russell  *		virtqueue_notify(vq);
3755dfc1762SRusty Russell  *
37641f0377fSRusty Russell  * This is sometimes useful because the virtqueue_kick_prepare() needs
37741f0377fSRusty Russell  * to be serialized, but the actual virtqueue_notify() call does not.
3785dfc1762SRusty Russell  */
37941f0377fSRusty Russell bool virtqueue_kick_prepare(struct virtqueue *_vq)
3800a8a69ddSRusty Russell {
3810a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
382a5c262c5SMichael S. Tsirkin 	u16 new, old;
38341f0377fSRusty Russell 	bool needs_kick;
38441f0377fSRusty Russell 
3850a8a69ddSRusty Russell 	START_USE(vq);
386a72caae2SJason Wang 	/* We need to expose available array entries before checking avail
387a72caae2SJason Wang 	 * event. */
388a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
3890a8a69ddSRusty Russell 
390ee7cd898SRusty Russell 	old = vq->vring.avail->idx - vq->num_added;
391ee7cd898SRusty Russell 	new = vq->vring.avail->idx;
3920a8a69ddSRusty Russell 	vq->num_added = 0;
3930a8a69ddSRusty Russell 
394e93300b1SRusty Russell #ifdef DEBUG
395e93300b1SRusty Russell 	if (vq->last_add_time_valid) {
396e93300b1SRusty Russell 		WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
397e93300b1SRusty Russell 					      vq->last_add_time)) > 100);
398e93300b1SRusty Russell 	}
399e93300b1SRusty Russell 	vq->last_add_time_valid = false;
400e93300b1SRusty Russell #endif
401e93300b1SRusty Russell 
40241f0377fSRusty Russell 	if (vq->event) {
40341f0377fSRusty Russell 		needs_kick = vring_need_event(vring_avail_event(&vq->vring),
40441f0377fSRusty Russell 					      new, old);
40541f0377fSRusty Russell 	} else {
40641f0377fSRusty Russell 		needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
40741f0377fSRusty Russell 	}
4080a8a69ddSRusty Russell 	END_USE(vq);
40941f0377fSRusty Russell 	return needs_kick;
41041f0377fSRusty Russell }
41141f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
41241f0377fSRusty Russell 
41341f0377fSRusty Russell /**
41441f0377fSRusty Russell  * virtqueue_notify - second half of split virtqueue_kick call.
41541f0377fSRusty Russell  * @vq: the struct virtqueue
41641f0377fSRusty Russell  *
41741f0377fSRusty Russell  * This does not need to be serialized.
41841f0377fSRusty Russell  */
41941f0377fSRusty Russell void virtqueue_notify(struct virtqueue *_vq)
42041f0377fSRusty Russell {
42141f0377fSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
42241f0377fSRusty Russell 
42341f0377fSRusty Russell 	/* Prod other side to tell it about changes. */
42441f0377fSRusty Russell 	vq->notify(_vq);
42541f0377fSRusty Russell }
42641f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify);
42741f0377fSRusty Russell 
42841f0377fSRusty Russell /**
42941f0377fSRusty Russell  * virtqueue_kick - update after add_buf
43041f0377fSRusty Russell  * @vq: the struct virtqueue
43141f0377fSRusty Russell  *
43241f0377fSRusty Russell  * After one or more virtqueue_add_buf calls, invoke this to kick
43341f0377fSRusty Russell  * the other side.
43441f0377fSRusty Russell  *
43541f0377fSRusty Russell  * Caller must ensure we don't call this with other virtqueue
43641f0377fSRusty Russell  * operations at the same time (except where noted).
43741f0377fSRusty Russell  */
43841f0377fSRusty Russell void virtqueue_kick(struct virtqueue *vq)
43941f0377fSRusty Russell {
44041f0377fSRusty Russell 	if (virtqueue_kick_prepare(vq))
44141f0377fSRusty Russell 		virtqueue_notify(vq);
4420a8a69ddSRusty Russell }
4437c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick);
4440a8a69ddSRusty Russell 
4450a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
4460a8a69ddSRusty Russell {
4470a8a69ddSRusty Russell 	unsigned int i;
4480a8a69ddSRusty Russell 
4490a8a69ddSRusty Russell 	/* Clear data ptr. */
4500a8a69ddSRusty Russell 	vq->data[head] = NULL;
4510a8a69ddSRusty Russell 
4520a8a69ddSRusty Russell 	/* Put back on free list: find end */
4530a8a69ddSRusty Russell 	i = head;
4549fa29b9dSMark McLoughlin 
4559fa29b9dSMark McLoughlin 	/* Free the indirect table */
4569fa29b9dSMark McLoughlin 	if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
4579fa29b9dSMark McLoughlin 		kfree(phys_to_virt(vq->vring.desc[i].addr));
4589fa29b9dSMark McLoughlin 
4590a8a69ddSRusty Russell 	while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
4600a8a69ddSRusty Russell 		i = vq->vring.desc[i].next;
46106ca287dSRusty Russell 		vq->vq.num_free++;
4620a8a69ddSRusty Russell 	}
4630a8a69ddSRusty Russell 
4640a8a69ddSRusty Russell 	vq->vring.desc[i].next = vq->free_head;
4650a8a69ddSRusty Russell 	vq->free_head = head;
4660a8a69ddSRusty Russell 	/* Plus final descriptor */
46706ca287dSRusty Russell 	vq->vq.num_free++;
4680a8a69ddSRusty Russell }
4690a8a69ddSRusty Russell 
4700a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq)
4710a8a69ddSRusty Russell {
4720a8a69ddSRusty Russell 	return vq->last_used_idx != vq->vring.used->idx;
4730a8a69ddSRusty Russell }
4740a8a69ddSRusty Russell 
4755dfc1762SRusty Russell /**
4765dfc1762SRusty Russell  * virtqueue_get_buf - get the next used buffer
4775dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
4785dfc1762SRusty Russell  * @len: the length written into the buffer
4795dfc1762SRusty Russell  *
4805dfc1762SRusty Russell  * If the driver wrote data into the buffer, @len will be set to the
4815dfc1762SRusty Russell  * amount written.  This means you don't need to clear the buffer
4825dfc1762SRusty Russell  * beforehand to ensure there's no data leakage in the case of short
4835dfc1762SRusty Russell  * writes.
4845dfc1762SRusty Russell  *
4855dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
4865dfc1762SRusty Russell  * operations at the same time (except where noted).
4875dfc1762SRusty Russell  *
4885dfc1762SRusty Russell  * Returns NULL if there are no used buffers, or the "data" token
489f96fde41SRusty Russell  * handed to virtqueue_add_buf().
4905dfc1762SRusty Russell  */
4917c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
4920a8a69ddSRusty Russell {
4930a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
4940a8a69ddSRusty Russell 	void *ret;
4950a8a69ddSRusty Russell 	unsigned int i;
4963b720b8cSRusty Russell 	u16 last_used;
4970a8a69ddSRusty Russell 
4980a8a69ddSRusty Russell 	START_USE(vq);
4990a8a69ddSRusty Russell 
5005ef82752SRusty Russell 	if (unlikely(vq->broken)) {
5015ef82752SRusty Russell 		END_USE(vq);
5025ef82752SRusty Russell 		return NULL;
5035ef82752SRusty Russell 	}
5045ef82752SRusty Russell 
5050a8a69ddSRusty Russell 	if (!more_used(vq)) {
5060a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
5070a8a69ddSRusty Russell 		END_USE(vq);
5080a8a69ddSRusty Russell 		return NULL;
5090a8a69ddSRusty Russell 	}
5100a8a69ddSRusty Russell 
5112d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
512a9a0fef7SRusty Russell 	virtio_rmb(vq->weak_barriers);
5132d61ba95SMichael S. Tsirkin 
5143b720b8cSRusty Russell 	last_used = (vq->last_used_idx & (vq->vring.num - 1));
5153b720b8cSRusty Russell 	i = vq->vring.used->ring[last_used].id;
5163b720b8cSRusty Russell 	*len = vq->vring.used->ring[last_used].len;
5170a8a69ddSRusty Russell 
5180a8a69ddSRusty Russell 	if (unlikely(i >= vq->vring.num)) {
5190a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
5200a8a69ddSRusty Russell 		return NULL;
5210a8a69ddSRusty Russell 	}
5220a8a69ddSRusty Russell 	if (unlikely(!vq->data[i])) {
5230a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
5240a8a69ddSRusty Russell 		return NULL;
5250a8a69ddSRusty Russell 	}
5260a8a69ddSRusty Russell 
5270a8a69ddSRusty Russell 	/* detach_buf clears data, so grab it now. */
5280a8a69ddSRusty Russell 	ret = vq->data[i];
5290a8a69ddSRusty Russell 	detach_buf(vq, i);
5300a8a69ddSRusty Russell 	vq->last_used_idx++;
531a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
532a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
533a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
534a5c262c5SMichael S. Tsirkin 	if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
535a5c262c5SMichael S. Tsirkin 		vring_used_event(&vq->vring) = vq->last_used_idx;
536a9a0fef7SRusty Russell 		virtio_mb(vq->weak_barriers);
537a5c262c5SMichael S. Tsirkin 	}
538a5c262c5SMichael S. Tsirkin 
539e93300b1SRusty Russell #ifdef DEBUG
540e93300b1SRusty Russell 	vq->last_add_time_valid = false;
541e93300b1SRusty Russell #endif
542e93300b1SRusty Russell 
5430a8a69ddSRusty Russell 	END_USE(vq);
5440a8a69ddSRusty Russell 	return ret;
5450a8a69ddSRusty Russell }
5467c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf);
5470a8a69ddSRusty Russell 
5485dfc1762SRusty Russell /**
5495dfc1762SRusty Russell  * virtqueue_disable_cb - disable callbacks
5505dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5515dfc1762SRusty Russell  *
5525dfc1762SRusty Russell  * Note that this is not necessarily synchronous, hence unreliable and only
5535dfc1762SRusty Russell  * useful as an optimization.
5545dfc1762SRusty Russell  *
5555dfc1762SRusty Russell  * Unlike other operations, this need not be serialized.
5565dfc1762SRusty Russell  */
5577c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq)
55818445c4dSRusty Russell {
55918445c4dSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
56018445c4dSRusty Russell 
56118445c4dSRusty Russell 	vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
56218445c4dSRusty Russell }
5637c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
56418445c4dSRusty Russell 
5655dfc1762SRusty Russell /**
5665dfc1762SRusty Russell  * virtqueue_enable_cb - restart callbacks after disable_cb.
5675dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5685dfc1762SRusty Russell  *
5695dfc1762SRusty Russell  * This re-enables callbacks; it returns "false" if there are pending
5705dfc1762SRusty Russell  * buffers in the queue, to detect a possible race between the driver
5715dfc1762SRusty Russell  * checking for more work, and enabling callbacks.
5725dfc1762SRusty Russell  *
5735dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
5745dfc1762SRusty Russell  * operations at the same time (except where noted).
5755dfc1762SRusty Russell  */
5767c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq)
5770a8a69ddSRusty Russell {
5780a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
5790a8a69ddSRusty Russell 
5800a8a69ddSRusty Russell 	START_USE(vq);
5810a8a69ddSRusty Russell 
5820a8a69ddSRusty Russell 	/* We optimistically turn back on interrupts, then check if there was
5830a8a69ddSRusty Russell 	 * more to do. */
584a5c262c5SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
585a5c262c5SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
586a5c262c5SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
5870a8a69ddSRusty Russell 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
588a5c262c5SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx;
589a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
5900a8a69ddSRusty Russell 	if (unlikely(more_used(vq))) {
5910a8a69ddSRusty Russell 		END_USE(vq);
5920a8a69ddSRusty Russell 		return false;
5930a8a69ddSRusty Russell 	}
5940a8a69ddSRusty Russell 
5950a8a69ddSRusty Russell 	END_USE(vq);
5960a8a69ddSRusty Russell 	return true;
5970a8a69ddSRusty Russell }
5987c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
5990a8a69ddSRusty Russell 
6005dfc1762SRusty Russell /**
6015dfc1762SRusty Russell  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
6025dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
6035dfc1762SRusty Russell  *
6045dfc1762SRusty Russell  * This re-enables callbacks but hints to the other side to delay
6055dfc1762SRusty Russell  * interrupts until most of the available buffers have been processed;
6065dfc1762SRusty Russell  * it returns "false" if there are many pending buffers in the queue,
6075dfc1762SRusty Russell  * to detect a possible race between the driver checking for more work,
6085dfc1762SRusty Russell  * and enabling callbacks.
6095dfc1762SRusty Russell  *
6105dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
6115dfc1762SRusty Russell  * operations at the same time (except where noted).
6125dfc1762SRusty Russell  */
6137ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
6147ab358c2SMichael S. Tsirkin {
6157ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
6167ab358c2SMichael S. Tsirkin 	u16 bufs;
6177ab358c2SMichael S. Tsirkin 
6187ab358c2SMichael S. Tsirkin 	START_USE(vq);
6197ab358c2SMichael S. Tsirkin 
6207ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
6217ab358c2SMichael S. Tsirkin 	 * more to do. */
6227ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
6237ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
6247ab358c2SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
6257ab358c2SMichael S. Tsirkin 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
6267ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
6277ab358c2SMichael S. Tsirkin 	bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
6287ab358c2SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
629a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
6307ab358c2SMichael S. Tsirkin 	if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
6317ab358c2SMichael S. Tsirkin 		END_USE(vq);
6327ab358c2SMichael S. Tsirkin 		return false;
6337ab358c2SMichael S. Tsirkin 	}
6347ab358c2SMichael S. Tsirkin 
6357ab358c2SMichael S. Tsirkin 	END_USE(vq);
6367ab358c2SMichael S. Tsirkin 	return true;
6377ab358c2SMichael S. Tsirkin }
6387ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
6397ab358c2SMichael S. Tsirkin 
6405dfc1762SRusty Russell /**
6415dfc1762SRusty Russell  * virtqueue_detach_unused_buf - detach first unused buffer
6425dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
6435dfc1762SRusty Russell  *
644f96fde41SRusty Russell  * Returns NULL or the "data" token handed to virtqueue_add_buf().
6455dfc1762SRusty Russell  * This is not valid on an active queue; it is useful only for device
6465dfc1762SRusty Russell  * shutdown.
6475dfc1762SRusty Russell  */
6487c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
649c021eac4SShirley Ma {
650c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
651c021eac4SShirley Ma 	unsigned int i;
652c021eac4SShirley Ma 	void *buf;
653c021eac4SShirley Ma 
654c021eac4SShirley Ma 	START_USE(vq);
655c021eac4SShirley Ma 
656c021eac4SShirley Ma 	for (i = 0; i < vq->vring.num; i++) {
657c021eac4SShirley Ma 		if (!vq->data[i])
658c021eac4SShirley Ma 			continue;
659c021eac4SShirley Ma 		/* detach_buf clears data, so grab it now. */
660c021eac4SShirley Ma 		buf = vq->data[i];
661c021eac4SShirley Ma 		detach_buf(vq, i);
662b3258ff1SAmit Shah 		vq->vring.avail->idx--;
663c021eac4SShirley Ma 		END_USE(vq);
664c021eac4SShirley Ma 		return buf;
665c021eac4SShirley Ma 	}
666c021eac4SShirley Ma 	/* That should have freed everything. */
66706ca287dSRusty Russell 	BUG_ON(vq->vq.num_free != vq->vring.num);
668c021eac4SShirley Ma 
669c021eac4SShirley Ma 	END_USE(vq);
670c021eac4SShirley Ma 	return NULL;
671c021eac4SShirley Ma }
6727c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
673c021eac4SShirley Ma 
6740a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
6750a8a69ddSRusty Russell {
6760a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
6770a8a69ddSRusty Russell 
6780a8a69ddSRusty Russell 	if (!more_used(vq)) {
6790a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
6800a8a69ddSRusty Russell 		return IRQ_NONE;
6810a8a69ddSRusty Russell 	}
6820a8a69ddSRusty Russell 
6830a8a69ddSRusty Russell 	if (unlikely(vq->broken))
6840a8a69ddSRusty Russell 		return IRQ_HANDLED;
6850a8a69ddSRusty Russell 
6860a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
68718445c4dSRusty Russell 	if (vq->vq.callback)
68818445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
6890a8a69ddSRusty Russell 
6900a8a69ddSRusty Russell 	return IRQ_HANDLED;
6910a8a69ddSRusty Russell }
692c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
6930a8a69ddSRusty Russell 
69417bb6d40SJason Wang struct virtqueue *vring_new_virtqueue(unsigned int index,
69517bb6d40SJason Wang 				      unsigned int num,
69687c7d57cSRusty Russell 				      unsigned int vring_align,
6970a8a69ddSRusty Russell 				      struct virtio_device *vdev,
6987b21e34fSRusty Russell 				      bool weak_barriers,
6990a8a69ddSRusty Russell 				      void *pages,
7000a8a69ddSRusty Russell 				      void (*notify)(struct virtqueue *),
7019499f5e7SRusty Russell 				      void (*callback)(struct virtqueue *),
7029499f5e7SRusty Russell 				      const char *name)
7030a8a69ddSRusty Russell {
7040a8a69ddSRusty Russell 	struct vring_virtqueue *vq;
7050a8a69ddSRusty Russell 	unsigned int i;
7060a8a69ddSRusty Russell 
70742b36cc0SRusty Russell 	/* We assume num is a power of 2. */
70842b36cc0SRusty Russell 	if (num & (num - 1)) {
70942b36cc0SRusty Russell 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
71042b36cc0SRusty Russell 		return NULL;
71142b36cc0SRusty Russell 	}
71242b36cc0SRusty Russell 
7130a8a69ddSRusty Russell 	vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
7140a8a69ddSRusty Russell 	if (!vq)
7150a8a69ddSRusty Russell 		return NULL;
7160a8a69ddSRusty Russell 
71787c7d57cSRusty Russell 	vring_init(&vq->vring, num, pages, vring_align);
7180a8a69ddSRusty Russell 	vq->vq.callback = callback;
7190a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
7209499f5e7SRusty Russell 	vq->vq.name = name;
72106ca287dSRusty Russell 	vq->vq.num_free = num;
72206ca287dSRusty Russell 	vq->vq.index = index;
7230a8a69ddSRusty Russell 	vq->notify = notify;
7247b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
7250a8a69ddSRusty Russell 	vq->broken = false;
7260a8a69ddSRusty Russell 	vq->last_used_idx = 0;
7270a8a69ddSRusty Russell 	vq->num_added = 0;
7289499f5e7SRusty Russell 	list_add_tail(&vq->vq.list, &vdev->vqs);
7290a8a69ddSRusty Russell #ifdef DEBUG
7300a8a69ddSRusty Russell 	vq->in_use = false;
731e93300b1SRusty Russell 	vq->last_add_time_valid = false;
7320a8a69ddSRusty Russell #endif
7330a8a69ddSRusty Russell 
7349fa29b9dSMark McLoughlin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
735a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
7369fa29b9dSMark McLoughlin 
7370a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
7380a8a69ddSRusty Russell 	if (!callback)
7390a8a69ddSRusty Russell 		vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
7400a8a69ddSRusty Russell 
7410a8a69ddSRusty Russell 	/* Put everything in free lists. */
7420a8a69ddSRusty Russell 	vq->free_head = 0;
7433b870624SAmit Shah 	for (i = 0; i < num-1; i++) {
7440a8a69ddSRusty Russell 		vq->vring.desc[i].next = i+1;
7453b870624SAmit Shah 		vq->data[i] = NULL;
7463b870624SAmit Shah 	}
7473b870624SAmit Shah 	vq->data[i] = NULL;
7480a8a69ddSRusty Russell 
7490a8a69ddSRusty Russell 	return &vq->vq;
7500a8a69ddSRusty Russell }
751c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
7520a8a69ddSRusty Russell 
7530a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq)
7540a8a69ddSRusty Russell {
7559499f5e7SRusty Russell 	list_del(&vq->list);
7560a8a69ddSRusty Russell 	kfree(to_vvq(vq));
7570a8a69ddSRusty Russell }
758c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
7590a8a69ddSRusty Russell 
760e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
761e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
762e34f8725SRusty Russell {
763e34f8725SRusty Russell 	unsigned int i;
764e34f8725SRusty Russell 
765e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
766e34f8725SRusty Russell 		switch (i) {
7679fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
7689fa29b9dSMark McLoughlin 			break;
769a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
770a5c262c5SMichael S. Tsirkin 			break;
771e34f8725SRusty Russell 		default:
772e34f8725SRusty Russell 			/* We don't understand this bit. */
773e34f8725SRusty Russell 			clear_bit(i, vdev->features);
774e34f8725SRusty Russell 		}
775e34f8725SRusty Russell 	}
776e34f8725SRusty Russell }
777e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
778e34f8725SRusty Russell 
7795dfc1762SRusty Russell /**
7805dfc1762SRusty Russell  * virtqueue_get_vring_size - return the size of the virtqueue's vring
7815dfc1762SRusty Russell  * @vq: the struct virtqueue containing the vring of interest.
7825dfc1762SRusty Russell  *
7835dfc1762SRusty Russell  * Returns the size of the vring.  This is mainly used for boasting to
7845dfc1762SRusty Russell  * userspace.  Unlike other operations, this need not be serialized.
7855dfc1762SRusty Russell  */
7868f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
7878f9f4668SRick Jones {
7888f9f4668SRick Jones 
7898f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
7908f9f4668SRick Jones 
7918f9f4668SRick Jones 	return vq->vring.num;
7928f9f4668SRick Jones }
7938f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
7948f9f4668SRick Jones 
795c6fd4701SRusty Russell MODULE_LICENSE("GPL");
796