xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision b3087e48ce20be784fae1dbabc2e42e2ad0f21bc)
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_sgs - expose buffers to other end
30013816c76SRusty Russell  * @vq: the struct virtqueue we're talking about.
30113816c76SRusty Russell  * @sgs: array of terminated scatterlists.
30213816c76SRusty Russell  * @out_num: the number of scatterlists readable by other side
30313816c76SRusty Russell  * @in_num: the number of scatterlists 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_sgs(struct virtqueue *_vq,
31313816c76SRusty Russell 		      struct scatterlist *sgs[],
31413816c76SRusty Russell 		      unsigned int out_sgs,
31513816c76SRusty Russell 		      unsigned int in_sgs,
31613816c76SRusty Russell 		      void *data,
31713816c76SRusty Russell 		      gfp_t gfp)
31813816c76SRusty Russell {
31913816c76SRusty Russell 	unsigned int i, total_out, total_in;
32013816c76SRusty Russell 
32113816c76SRusty Russell 	/* Count them first. */
32213816c76SRusty Russell 	for (i = total_out = total_in = 0; i < out_sgs; i++) {
32313816c76SRusty Russell 		struct scatterlist *sg;
32413816c76SRusty Russell 		for (sg = sgs[i]; sg; sg = sg_next(sg))
32513816c76SRusty Russell 			total_out++;
32613816c76SRusty Russell 	}
32713816c76SRusty Russell 	for (; i < out_sgs + in_sgs; i++) {
32813816c76SRusty Russell 		struct scatterlist *sg;
32913816c76SRusty Russell 		for (sg = sgs[i]; sg; sg = sg_next(sg))
33013816c76SRusty Russell 			total_in++;
33113816c76SRusty Russell 	}
33213816c76SRusty Russell 	return virtqueue_add(_vq, sgs, sg_next_chained,
33313816c76SRusty Russell 			     total_out, total_in, out_sgs, in_sgs, data, gfp);
33413816c76SRusty Russell }
33513816c76SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
33613816c76SRusty Russell 
33713816c76SRusty Russell /**
338282edb36SRusty Russell  * virtqueue_add_outbuf - expose output buffers to other end
339282edb36SRusty Russell  * @vq: the struct virtqueue we're talking about.
340282edb36SRusty Russell  * @sgs: array of scatterlists (need not be terminated!)
341282edb36SRusty Russell  * @num: the number of scatterlists readable by other side
342282edb36SRusty Russell  * @data: the token identifying the buffer.
343282edb36SRusty Russell  * @gfp: how to do memory allocations (if necessary).
344282edb36SRusty Russell  *
345282edb36SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
346282edb36SRusty Russell  * at the same time (except where noted).
347282edb36SRusty Russell  *
348282edb36SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
349282edb36SRusty Russell  */
350282edb36SRusty Russell int virtqueue_add_outbuf(struct virtqueue *vq,
351282edb36SRusty Russell 			 struct scatterlist sg[], unsigned int num,
352282edb36SRusty Russell 			 void *data,
353282edb36SRusty Russell 			 gfp_t gfp)
354282edb36SRusty Russell {
355282edb36SRusty Russell 	return virtqueue_add(vq, &sg, sg_next_arr, num, 0, 1, 0, data, gfp);
356282edb36SRusty Russell }
357282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
358282edb36SRusty Russell 
359282edb36SRusty Russell /**
360282edb36SRusty Russell  * virtqueue_add_inbuf - expose input buffers to other end
361282edb36SRusty Russell  * @vq: the struct virtqueue we're talking about.
362282edb36SRusty Russell  * @sgs: array of scatterlists (need not be terminated!)
363282edb36SRusty Russell  * @num: the number of scatterlists writable by other side
364282edb36SRusty Russell  * @data: the token identifying the buffer.
365282edb36SRusty Russell  * @gfp: how to do memory allocations (if necessary).
366282edb36SRusty Russell  *
367282edb36SRusty Russell  * Caller must ensure we don't call this with other virtqueue operations
368282edb36SRusty Russell  * at the same time (except where noted).
369282edb36SRusty Russell  *
370282edb36SRusty Russell  * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
371282edb36SRusty Russell  */
372282edb36SRusty Russell int virtqueue_add_inbuf(struct virtqueue *vq,
373282edb36SRusty Russell 			struct scatterlist sg[], unsigned int num,
374282edb36SRusty Russell 			void *data,
375282edb36SRusty Russell 			gfp_t gfp)
376282edb36SRusty Russell {
377282edb36SRusty Russell 	return virtqueue_add(vq, &sg, sg_next_arr, 0, num, 0, 1, data, gfp);
378282edb36SRusty Russell }
379282edb36SRusty Russell EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
380282edb36SRusty Russell 
381282edb36SRusty Russell /**
38241f0377fSRusty Russell  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
3835dfc1762SRusty Russell  * @vq: the struct virtqueue
3845dfc1762SRusty Russell  *
38541f0377fSRusty Russell  * Instead of virtqueue_kick(), you can do:
38641f0377fSRusty Russell  *	if (virtqueue_kick_prepare(vq))
38741f0377fSRusty Russell  *		virtqueue_notify(vq);
3885dfc1762SRusty Russell  *
38941f0377fSRusty Russell  * This is sometimes useful because the virtqueue_kick_prepare() needs
39041f0377fSRusty Russell  * to be serialized, but the actual virtqueue_notify() call does not.
3915dfc1762SRusty Russell  */
39241f0377fSRusty Russell bool virtqueue_kick_prepare(struct virtqueue *_vq)
3930a8a69ddSRusty Russell {
3940a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
395a5c262c5SMichael S. Tsirkin 	u16 new, old;
39641f0377fSRusty Russell 	bool needs_kick;
39741f0377fSRusty Russell 
3980a8a69ddSRusty Russell 	START_USE(vq);
399a72caae2SJason Wang 	/* We need to expose available array entries before checking avail
400a72caae2SJason Wang 	 * event. */
401a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
4020a8a69ddSRusty Russell 
403ee7cd898SRusty Russell 	old = vq->vring.avail->idx - vq->num_added;
404ee7cd898SRusty Russell 	new = vq->vring.avail->idx;
4050a8a69ddSRusty Russell 	vq->num_added = 0;
4060a8a69ddSRusty Russell 
407e93300b1SRusty Russell #ifdef DEBUG
408e93300b1SRusty Russell 	if (vq->last_add_time_valid) {
409e93300b1SRusty Russell 		WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
410e93300b1SRusty Russell 					      vq->last_add_time)) > 100);
411e93300b1SRusty Russell 	}
412e93300b1SRusty Russell 	vq->last_add_time_valid = false;
413e93300b1SRusty Russell #endif
414e93300b1SRusty Russell 
41541f0377fSRusty Russell 	if (vq->event) {
41641f0377fSRusty Russell 		needs_kick = vring_need_event(vring_avail_event(&vq->vring),
41741f0377fSRusty Russell 					      new, old);
41841f0377fSRusty Russell 	} else {
41941f0377fSRusty Russell 		needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
42041f0377fSRusty Russell 	}
4210a8a69ddSRusty Russell 	END_USE(vq);
42241f0377fSRusty Russell 	return needs_kick;
42341f0377fSRusty Russell }
42441f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
42541f0377fSRusty Russell 
42641f0377fSRusty Russell /**
42741f0377fSRusty Russell  * virtqueue_notify - second half of split virtqueue_kick call.
42841f0377fSRusty Russell  * @vq: the struct virtqueue
42941f0377fSRusty Russell  *
43041f0377fSRusty Russell  * This does not need to be serialized.
43141f0377fSRusty Russell  */
43241f0377fSRusty Russell void virtqueue_notify(struct virtqueue *_vq)
43341f0377fSRusty Russell {
43441f0377fSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
43541f0377fSRusty Russell 
43641f0377fSRusty Russell 	/* Prod other side to tell it about changes. */
43741f0377fSRusty Russell 	vq->notify(_vq);
43841f0377fSRusty Russell }
43941f0377fSRusty Russell EXPORT_SYMBOL_GPL(virtqueue_notify);
44041f0377fSRusty Russell 
44141f0377fSRusty Russell /**
44241f0377fSRusty Russell  * virtqueue_kick - update after add_buf
44341f0377fSRusty Russell  * @vq: the struct virtqueue
44441f0377fSRusty Russell  *
445*b3087e48SRusty Russell  * After one or more virtqueue_add_* calls, invoke this to kick
44641f0377fSRusty Russell  * the other side.
44741f0377fSRusty Russell  *
44841f0377fSRusty Russell  * Caller must ensure we don't call this with other virtqueue
44941f0377fSRusty Russell  * operations at the same time (except where noted).
45041f0377fSRusty Russell  */
45141f0377fSRusty Russell void virtqueue_kick(struct virtqueue *vq)
45241f0377fSRusty Russell {
45341f0377fSRusty Russell 	if (virtqueue_kick_prepare(vq))
45441f0377fSRusty Russell 		virtqueue_notify(vq);
4550a8a69ddSRusty Russell }
4567c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_kick);
4570a8a69ddSRusty Russell 
4580a8a69ddSRusty Russell static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
4590a8a69ddSRusty Russell {
4600a8a69ddSRusty Russell 	unsigned int i;
4610a8a69ddSRusty Russell 
4620a8a69ddSRusty Russell 	/* Clear data ptr. */
4630a8a69ddSRusty Russell 	vq->data[head] = NULL;
4640a8a69ddSRusty Russell 
4650a8a69ddSRusty Russell 	/* Put back on free list: find end */
4660a8a69ddSRusty Russell 	i = head;
4679fa29b9dSMark McLoughlin 
4689fa29b9dSMark McLoughlin 	/* Free the indirect table */
4699fa29b9dSMark McLoughlin 	if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
4709fa29b9dSMark McLoughlin 		kfree(phys_to_virt(vq->vring.desc[i].addr));
4719fa29b9dSMark McLoughlin 
4720a8a69ddSRusty Russell 	while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
4730a8a69ddSRusty Russell 		i = vq->vring.desc[i].next;
47406ca287dSRusty Russell 		vq->vq.num_free++;
4750a8a69ddSRusty Russell 	}
4760a8a69ddSRusty Russell 
4770a8a69ddSRusty Russell 	vq->vring.desc[i].next = vq->free_head;
4780a8a69ddSRusty Russell 	vq->free_head = head;
4790a8a69ddSRusty Russell 	/* Plus final descriptor */
48006ca287dSRusty Russell 	vq->vq.num_free++;
4810a8a69ddSRusty Russell }
4820a8a69ddSRusty Russell 
4830a8a69ddSRusty Russell static inline bool more_used(const struct vring_virtqueue *vq)
4840a8a69ddSRusty Russell {
4850a8a69ddSRusty Russell 	return vq->last_used_idx != vq->vring.used->idx;
4860a8a69ddSRusty Russell }
4870a8a69ddSRusty Russell 
4885dfc1762SRusty Russell /**
4895dfc1762SRusty Russell  * virtqueue_get_buf - get the next used buffer
4905dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
4915dfc1762SRusty Russell  * @len: the length written into the buffer
4925dfc1762SRusty Russell  *
4935dfc1762SRusty Russell  * If the driver wrote data into the buffer, @len will be set to the
4945dfc1762SRusty Russell  * amount written.  This means you don't need to clear the buffer
4955dfc1762SRusty Russell  * beforehand to ensure there's no data leakage in the case of short
4965dfc1762SRusty Russell  * writes.
4975dfc1762SRusty Russell  *
4985dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
4995dfc1762SRusty Russell  * operations at the same time (except where noted).
5005dfc1762SRusty Russell  *
5015dfc1762SRusty Russell  * Returns NULL if there are no used buffers, or the "data" token
502*b3087e48SRusty Russell  * handed to virtqueue_add_*().
5035dfc1762SRusty Russell  */
5047c5e9ed0SMichael S. Tsirkin void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
5050a8a69ddSRusty Russell {
5060a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
5070a8a69ddSRusty Russell 	void *ret;
5080a8a69ddSRusty Russell 	unsigned int i;
5093b720b8cSRusty Russell 	u16 last_used;
5100a8a69ddSRusty Russell 
5110a8a69ddSRusty Russell 	START_USE(vq);
5120a8a69ddSRusty Russell 
5135ef82752SRusty Russell 	if (unlikely(vq->broken)) {
5145ef82752SRusty Russell 		END_USE(vq);
5155ef82752SRusty Russell 		return NULL;
5165ef82752SRusty Russell 	}
5175ef82752SRusty Russell 
5180a8a69ddSRusty Russell 	if (!more_used(vq)) {
5190a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
5200a8a69ddSRusty Russell 		END_USE(vq);
5210a8a69ddSRusty Russell 		return NULL;
5220a8a69ddSRusty Russell 	}
5230a8a69ddSRusty Russell 
5242d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
525a9a0fef7SRusty Russell 	virtio_rmb(vq->weak_barriers);
5262d61ba95SMichael S. Tsirkin 
5273b720b8cSRusty Russell 	last_used = (vq->last_used_idx & (vq->vring.num - 1));
5283b720b8cSRusty Russell 	i = vq->vring.used->ring[last_used].id;
5293b720b8cSRusty Russell 	*len = vq->vring.used->ring[last_used].len;
5300a8a69ddSRusty Russell 
5310a8a69ddSRusty Russell 	if (unlikely(i >= vq->vring.num)) {
5320a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
5330a8a69ddSRusty Russell 		return NULL;
5340a8a69ddSRusty Russell 	}
5350a8a69ddSRusty Russell 	if (unlikely(!vq->data[i])) {
5360a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
5370a8a69ddSRusty Russell 		return NULL;
5380a8a69ddSRusty Russell 	}
5390a8a69ddSRusty Russell 
5400a8a69ddSRusty Russell 	/* detach_buf clears data, so grab it now. */
5410a8a69ddSRusty Russell 	ret = vq->data[i];
5420a8a69ddSRusty Russell 	detach_buf(vq, i);
5430a8a69ddSRusty Russell 	vq->last_used_idx++;
544a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
545a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
546a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
547a5c262c5SMichael S. Tsirkin 	if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
548a5c262c5SMichael S. Tsirkin 		vring_used_event(&vq->vring) = vq->last_used_idx;
549a9a0fef7SRusty Russell 		virtio_mb(vq->weak_barriers);
550a5c262c5SMichael S. Tsirkin 	}
551a5c262c5SMichael S. Tsirkin 
552e93300b1SRusty Russell #ifdef DEBUG
553e93300b1SRusty Russell 	vq->last_add_time_valid = false;
554e93300b1SRusty Russell #endif
555e93300b1SRusty Russell 
5560a8a69ddSRusty Russell 	END_USE(vq);
5570a8a69ddSRusty Russell 	return ret;
5580a8a69ddSRusty Russell }
5597c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_get_buf);
5600a8a69ddSRusty Russell 
5615dfc1762SRusty Russell /**
5625dfc1762SRusty Russell  * virtqueue_disable_cb - disable callbacks
5635dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5645dfc1762SRusty Russell  *
5655dfc1762SRusty Russell  * Note that this is not necessarily synchronous, hence unreliable and only
5665dfc1762SRusty Russell  * useful as an optimization.
5675dfc1762SRusty Russell  *
5685dfc1762SRusty Russell  * Unlike other operations, this need not be serialized.
5695dfc1762SRusty Russell  */
5707c5e9ed0SMichael S. Tsirkin void virtqueue_disable_cb(struct virtqueue *_vq)
57118445c4dSRusty Russell {
57218445c4dSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
57318445c4dSRusty Russell 
57418445c4dSRusty Russell 	vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
57518445c4dSRusty Russell }
5767c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
57718445c4dSRusty Russell 
5785dfc1762SRusty Russell /**
5795dfc1762SRusty Russell  * virtqueue_enable_cb - restart callbacks after disable_cb.
5805dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
5815dfc1762SRusty Russell  *
5825dfc1762SRusty Russell  * This re-enables callbacks; it returns "false" if there are pending
5835dfc1762SRusty Russell  * buffers in the queue, to detect a possible race between the driver
5845dfc1762SRusty Russell  * checking for more work, and enabling callbacks.
5855dfc1762SRusty Russell  *
5865dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
5875dfc1762SRusty Russell  * operations at the same time (except where noted).
5885dfc1762SRusty Russell  */
5897c5e9ed0SMichael S. Tsirkin bool virtqueue_enable_cb(struct virtqueue *_vq)
5900a8a69ddSRusty Russell {
5910a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
5920a8a69ddSRusty Russell 
5930a8a69ddSRusty Russell 	START_USE(vq);
5940a8a69ddSRusty Russell 
5950a8a69ddSRusty Russell 	/* We optimistically turn back on interrupts, then check if there was
5960a8a69ddSRusty Russell 	 * more to do. */
597a5c262c5SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
598a5c262c5SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
599a5c262c5SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
6000a8a69ddSRusty Russell 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
601a5c262c5SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx;
602a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
6030a8a69ddSRusty Russell 	if (unlikely(more_used(vq))) {
6040a8a69ddSRusty Russell 		END_USE(vq);
6050a8a69ddSRusty Russell 		return false;
6060a8a69ddSRusty Russell 	}
6070a8a69ddSRusty Russell 
6080a8a69ddSRusty Russell 	END_USE(vq);
6090a8a69ddSRusty Russell 	return true;
6100a8a69ddSRusty Russell }
6117c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
6120a8a69ddSRusty Russell 
6135dfc1762SRusty Russell /**
6145dfc1762SRusty Russell  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
6155dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
6165dfc1762SRusty Russell  *
6175dfc1762SRusty Russell  * This re-enables callbacks but hints to the other side to delay
6185dfc1762SRusty Russell  * interrupts until most of the available buffers have been processed;
6195dfc1762SRusty Russell  * it returns "false" if there are many pending buffers in the queue,
6205dfc1762SRusty Russell  * to detect a possible race between the driver checking for more work,
6215dfc1762SRusty Russell  * and enabling callbacks.
6225dfc1762SRusty Russell  *
6235dfc1762SRusty Russell  * Caller must ensure we don't call this with other virtqueue
6245dfc1762SRusty Russell  * operations at the same time (except where noted).
6255dfc1762SRusty Russell  */
6267ab358c2SMichael S. Tsirkin bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
6277ab358c2SMichael S. Tsirkin {
6287ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
6297ab358c2SMichael S. Tsirkin 	u16 bufs;
6307ab358c2SMichael S. Tsirkin 
6317ab358c2SMichael S. Tsirkin 	START_USE(vq);
6327ab358c2SMichael S. Tsirkin 
6337ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
6347ab358c2SMichael S. Tsirkin 	 * more to do. */
6357ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
6367ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
6377ab358c2SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
6387ab358c2SMichael S. Tsirkin 	vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
6397ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
6407ab358c2SMichael S. Tsirkin 	bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
6417ab358c2SMichael S. Tsirkin 	vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
642a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
6437ab358c2SMichael S. Tsirkin 	if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
6447ab358c2SMichael S. Tsirkin 		END_USE(vq);
6457ab358c2SMichael S. Tsirkin 		return false;
6467ab358c2SMichael S. Tsirkin 	}
6477ab358c2SMichael S. Tsirkin 
6487ab358c2SMichael S. Tsirkin 	END_USE(vq);
6497ab358c2SMichael S. Tsirkin 	return true;
6507ab358c2SMichael S. Tsirkin }
6517ab358c2SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
6527ab358c2SMichael S. Tsirkin 
6535dfc1762SRusty Russell /**
6545dfc1762SRusty Russell  * virtqueue_detach_unused_buf - detach first unused buffer
6555dfc1762SRusty Russell  * @vq: the struct virtqueue we're talking about.
6565dfc1762SRusty Russell  *
657*b3087e48SRusty Russell  * Returns NULL or the "data" token handed to virtqueue_add_*().
6585dfc1762SRusty Russell  * This is not valid on an active queue; it is useful only for device
6595dfc1762SRusty Russell  * shutdown.
6605dfc1762SRusty Russell  */
6617c5e9ed0SMichael S. Tsirkin void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
662c021eac4SShirley Ma {
663c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
664c021eac4SShirley Ma 	unsigned int i;
665c021eac4SShirley Ma 	void *buf;
666c021eac4SShirley Ma 
667c021eac4SShirley Ma 	START_USE(vq);
668c021eac4SShirley Ma 
669c021eac4SShirley Ma 	for (i = 0; i < vq->vring.num; i++) {
670c021eac4SShirley Ma 		if (!vq->data[i])
671c021eac4SShirley Ma 			continue;
672c021eac4SShirley Ma 		/* detach_buf clears data, so grab it now. */
673c021eac4SShirley Ma 		buf = vq->data[i];
674c021eac4SShirley Ma 		detach_buf(vq, i);
675b3258ff1SAmit Shah 		vq->vring.avail->idx--;
676c021eac4SShirley Ma 		END_USE(vq);
677c021eac4SShirley Ma 		return buf;
678c021eac4SShirley Ma 	}
679c021eac4SShirley Ma 	/* That should have freed everything. */
68006ca287dSRusty Russell 	BUG_ON(vq->vq.num_free != vq->vring.num);
681c021eac4SShirley Ma 
682c021eac4SShirley Ma 	END_USE(vq);
683c021eac4SShirley Ma 	return NULL;
684c021eac4SShirley Ma }
6857c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
686c021eac4SShirley Ma 
6870a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
6880a8a69ddSRusty Russell {
6890a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
6900a8a69ddSRusty Russell 
6910a8a69ddSRusty Russell 	if (!more_used(vq)) {
6920a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
6930a8a69ddSRusty Russell 		return IRQ_NONE;
6940a8a69ddSRusty Russell 	}
6950a8a69ddSRusty Russell 
6960a8a69ddSRusty Russell 	if (unlikely(vq->broken))
6970a8a69ddSRusty Russell 		return IRQ_HANDLED;
6980a8a69ddSRusty Russell 
6990a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
70018445c4dSRusty Russell 	if (vq->vq.callback)
70118445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
7020a8a69ddSRusty Russell 
7030a8a69ddSRusty Russell 	return IRQ_HANDLED;
7040a8a69ddSRusty Russell }
705c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
7060a8a69ddSRusty Russell 
70717bb6d40SJason Wang struct virtqueue *vring_new_virtqueue(unsigned int index,
70817bb6d40SJason Wang 				      unsigned int num,
70987c7d57cSRusty Russell 				      unsigned int vring_align,
7100a8a69ddSRusty Russell 				      struct virtio_device *vdev,
7117b21e34fSRusty Russell 				      bool weak_barriers,
7120a8a69ddSRusty Russell 				      void *pages,
7130a8a69ddSRusty Russell 				      void (*notify)(struct virtqueue *),
7149499f5e7SRusty Russell 				      void (*callback)(struct virtqueue *),
7159499f5e7SRusty Russell 				      const char *name)
7160a8a69ddSRusty Russell {
7170a8a69ddSRusty Russell 	struct vring_virtqueue *vq;
7180a8a69ddSRusty Russell 	unsigned int i;
7190a8a69ddSRusty Russell 
72042b36cc0SRusty Russell 	/* We assume num is a power of 2. */
72142b36cc0SRusty Russell 	if (num & (num - 1)) {
72242b36cc0SRusty Russell 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
72342b36cc0SRusty Russell 		return NULL;
72442b36cc0SRusty Russell 	}
72542b36cc0SRusty Russell 
7260a8a69ddSRusty Russell 	vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
7270a8a69ddSRusty Russell 	if (!vq)
7280a8a69ddSRusty Russell 		return NULL;
7290a8a69ddSRusty Russell 
73087c7d57cSRusty Russell 	vring_init(&vq->vring, num, pages, vring_align);
7310a8a69ddSRusty Russell 	vq->vq.callback = callback;
7320a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
7339499f5e7SRusty Russell 	vq->vq.name = name;
73406ca287dSRusty Russell 	vq->vq.num_free = num;
73506ca287dSRusty Russell 	vq->vq.index = index;
7360a8a69ddSRusty Russell 	vq->notify = notify;
7377b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
7380a8a69ddSRusty Russell 	vq->broken = false;
7390a8a69ddSRusty Russell 	vq->last_used_idx = 0;
7400a8a69ddSRusty Russell 	vq->num_added = 0;
7419499f5e7SRusty Russell 	list_add_tail(&vq->vq.list, &vdev->vqs);
7420a8a69ddSRusty Russell #ifdef DEBUG
7430a8a69ddSRusty Russell 	vq->in_use = false;
744e93300b1SRusty Russell 	vq->last_add_time_valid = false;
7450a8a69ddSRusty Russell #endif
7460a8a69ddSRusty Russell 
7479fa29b9dSMark McLoughlin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
748a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
7499fa29b9dSMark McLoughlin 
7500a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
7510a8a69ddSRusty Russell 	if (!callback)
7520a8a69ddSRusty Russell 		vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
7530a8a69ddSRusty Russell 
7540a8a69ddSRusty Russell 	/* Put everything in free lists. */
7550a8a69ddSRusty Russell 	vq->free_head = 0;
7563b870624SAmit Shah 	for (i = 0; i < num-1; i++) {
7570a8a69ddSRusty Russell 		vq->vring.desc[i].next = i+1;
7583b870624SAmit Shah 		vq->data[i] = NULL;
7593b870624SAmit Shah 	}
7603b870624SAmit Shah 	vq->data[i] = NULL;
7610a8a69ddSRusty Russell 
7620a8a69ddSRusty Russell 	return &vq->vq;
7630a8a69ddSRusty Russell }
764c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
7650a8a69ddSRusty Russell 
7660a8a69ddSRusty Russell void vring_del_virtqueue(struct virtqueue *vq)
7670a8a69ddSRusty Russell {
7689499f5e7SRusty Russell 	list_del(&vq->list);
7690a8a69ddSRusty Russell 	kfree(to_vvq(vq));
7700a8a69ddSRusty Russell }
771c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
7720a8a69ddSRusty Russell 
773e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
774e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
775e34f8725SRusty Russell {
776e34f8725SRusty Russell 	unsigned int i;
777e34f8725SRusty Russell 
778e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
779e34f8725SRusty Russell 		switch (i) {
7809fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
7819fa29b9dSMark McLoughlin 			break;
782a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
783a5c262c5SMichael S. Tsirkin 			break;
784e34f8725SRusty Russell 		default:
785e34f8725SRusty Russell 			/* We don't understand this bit. */
786e34f8725SRusty Russell 			clear_bit(i, vdev->features);
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 
808c6fd4701SRusty Russell MODULE_LICENSE("GPL");
809