xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision 88938359e2dfe1f5f5840268b98935948db8fbd9)
1fd534e9bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20a8a69ddSRusty Russell /* Virtio ring implementation.
30a8a69ddSRusty Russell  *
40a8a69ddSRusty Russell  *  Copyright 2007 Rusty Russell IBM Corporation
50a8a69ddSRusty Russell  */
60a8a69ddSRusty Russell #include <linux/virtio.h>
70a8a69ddSRusty Russell #include <linux/virtio_ring.h>
8e34f8725SRusty Russell #include <linux/virtio_config.h>
90a8a69ddSRusty Russell #include <linux/device.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
11b5a2c4f1SPaul Gortmaker #include <linux/module.h>
12e93300b1SRusty Russell #include <linux/hrtimer.h>
13780bc790SAndy Lutomirski #include <linux/dma-mapping.h>
14*88938359SAlexander Potapenko #include <linux/kmsan.h>
15f8ce7263SMichael S. Tsirkin #include <linux/spinlock.h>
1678fe3987SAndy Lutomirski #include <xen/xen.h>
170a8a69ddSRusty Russell 
180a8a69ddSRusty Russell #ifdef DEBUG
190a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */
209499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
219499f5e7SRusty Russell 	do {							\
229499f5e7SRusty Russell 		dev_err(&(_vq)->vq.vdev->dev,			\
239499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
249499f5e7SRusty Russell 		BUG();						\
259499f5e7SRusty Russell 	} while (0)
26c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */
273a35ce7dSRoel Kluin #define START_USE(_vq)						\
28c5f841f1SRusty Russell 	do {							\
29c5f841f1SRusty Russell 		if ((_vq)->in_use)				\
309499f5e7SRusty Russell 			panic("%s:in_use = %i\n",		\
319499f5e7SRusty Russell 			      (_vq)->vq.name, (_vq)->in_use);	\
32c5f841f1SRusty Russell 		(_vq)->in_use = __LINE__;			\
33c5f841f1SRusty Russell 	} while (0)
343a35ce7dSRoel Kluin #define END_USE(_vq) \
3597a545abSRusty Russell 	do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
364d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(_vq)				\
374d6a105eSTiwei Bie 	do {							\
384d6a105eSTiwei Bie 		ktime_t now = ktime_get();			\
394d6a105eSTiwei Bie 								\
404d6a105eSTiwei Bie 		/* No kick or get, with .1 second between?  Warn. */ \
414d6a105eSTiwei Bie 		if ((_vq)->last_add_time_valid)			\
424d6a105eSTiwei Bie 			WARN_ON(ktime_to_ms(ktime_sub(now,	\
434d6a105eSTiwei Bie 				(_vq)->last_add_time)) > 100);	\
444d6a105eSTiwei Bie 		(_vq)->last_add_time = now;			\
454d6a105eSTiwei Bie 		(_vq)->last_add_time_valid = true;		\
464d6a105eSTiwei Bie 	} while (0)
474d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(_vq)				\
484d6a105eSTiwei Bie 	do {							\
494d6a105eSTiwei Bie 		if ((_vq)->last_add_time_valid) {		\
504d6a105eSTiwei Bie 			WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \
514d6a105eSTiwei Bie 				      (_vq)->last_add_time)) > 100); \
524d6a105eSTiwei Bie 		}						\
534d6a105eSTiwei Bie 	} while (0)
544d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(_vq)				\
554d6a105eSTiwei Bie 	((_vq)->last_add_time_valid = false)
560a8a69ddSRusty Russell #else
579499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
589499f5e7SRusty Russell 	do {							\
599499f5e7SRusty Russell 		dev_err(&_vq->vq.vdev->dev,			\
609499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
619499f5e7SRusty Russell 		(_vq)->broken = true;				\
629499f5e7SRusty Russell 	} while (0)
630a8a69ddSRusty Russell #define START_USE(vq)
640a8a69ddSRusty Russell #define END_USE(vq)
654d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(vq)
664d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(vq)
674d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(vq)
680a8a69ddSRusty Russell #endif
690a8a69ddSRusty Russell 
70cbeedb72STiwei Bie struct vring_desc_state_split {
71780bc790SAndy Lutomirski 	void *data;			/* Data for callback. */
72780bc790SAndy Lutomirski 	struct vring_desc *indir_desc;	/* Indirect descriptor, if any. */
73780bc790SAndy Lutomirski };
74780bc790SAndy Lutomirski 
751ce9e605STiwei Bie struct vring_desc_state_packed {
761ce9e605STiwei Bie 	void *data;			/* Data for callback. */
771ce9e605STiwei Bie 	struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */
781ce9e605STiwei Bie 	u16 num;			/* Descriptor list length. */
791ce9e605STiwei Bie 	u16 last;			/* The last desc state in a list. */
801ce9e605STiwei Bie };
811ce9e605STiwei Bie 
821f28750fSJason Wang struct vring_desc_extra {
83ef5c366fSJason Wang 	dma_addr_t addr;		/* Descriptor DMA addr. */
84ef5c366fSJason Wang 	u32 len;			/* Descriptor length. */
851ce9e605STiwei Bie 	u16 flags;			/* Descriptor flags. */
86aeef9b47SJason Wang 	u16 next;			/* The next desc state in a list. */
871ce9e605STiwei Bie };
881ce9e605STiwei Bie 
89d76136e4SXuan Zhuo struct vring_virtqueue_split {
90d76136e4SXuan Zhuo 	/* Actual memory layout for this queue. */
91d76136e4SXuan Zhuo 	struct vring vring;
92d76136e4SXuan Zhuo 
93d76136e4SXuan Zhuo 	/* Last written value to avail->flags */
94d76136e4SXuan Zhuo 	u16 avail_flags_shadow;
95d76136e4SXuan Zhuo 
96d76136e4SXuan Zhuo 	/*
97d76136e4SXuan Zhuo 	 * Last written value to avail->idx in
98d76136e4SXuan Zhuo 	 * guest byte order.
99d76136e4SXuan Zhuo 	 */
100d76136e4SXuan Zhuo 	u16 avail_idx_shadow;
101d76136e4SXuan Zhuo 
102d76136e4SXuan Zhuo 	/* Per-descriptor state. */
103d76136e4SXuan Zhuo 	struct vring_desc_state_split *desc_state;
104d76136e4SXuan Zhuo 	struct vring_desc_extra *desc_extra;
105d76136e4SXuan Zhuo 
106d76136e4SXuan Zhuo 	/* DMA address and size information */
107d76136e4SXuan Zhuo 	dma_addr_t queue_dma_addr;
108d76136e4SXuan Zhuo 	size_t queue_size_in_bytes;
109af36b16fSXuan Zhuo 
110af36b16fSXuan Zhuo 	/*
111af36b16fSXuan Zhuo 	 * The parameters for creating vrings are reserved for creating new
112af36b16fSXuan Zhuo 	 * vring.
113af36b16fSXuan Zhuo 	 */
114af36b16fSXuan Zhuo 	u32 vring_align;
115af36b16fSXuan Zhuo 	bool may_reduce_num;
116d76136e4SXuan Zhuo };
117d76136e4SXuan Zhuo 
118d76136e4SXuan Zhuo struct vring_virtqueue_packed {
119d76136e4SXuan Zhuo 	/* Actual memory layout for this queue. */
120d76136e4SXuan Zhuo 	struct {
121d76136e4SXuan Zhuo 		unsigned int num;
122d76136e4SXuan Zhuo 		struct vring_packed_desc *desc;
123d76136e4SXuan Zhuo 		struct vring_packed_desc_event *driver;
124d76136e4SXuan Zhuo 		struct vring_packed_desc_event *device;
125d76136e4SXuan Zhuo 	} vring;
126d76136e4SXuan Zhuo 
127d76136e4SXuan Zhuo 	/* Driver ring wrap counter. */
128d76136e4SXuan Zhuo 	bool avail_wrap_counter;
129d76136e4SXuan Zhuo 
130d76136e4SXuan Zhuo 	/* Avail used flags. */
131d76136e4SXuan Zhuo 	u16 avail_used_flags;
132d76136e4SXuan Zhuo 
133d76136e4SXuan Zhuo 	/* Index of the next avail descriptor. */
134d76136e4SXuan Zhuo 	u16 next_avail_idx;
135d76136e4SXuan Zhuo 
136d76136e4SXuan Zhuo 	/*
137d76136e4SXuan Zhuo 	 * Last written value to driver->flags in
138d76136e4SXuan Zhuo 	 * guest byte order.
139d76136e4SXuan Zhuo 	 */
140d76136e4SXuan Zhuo 	u16 event_flags_shadow;
141d76136e4SXuan Zhuo 
142d76136e4SXuan Zhuo 	/* Per-descriptor state. */
143d76136e4SXuan Zhuo 	struct vring_desc_state_packed *desc_state;
144d76136e4SXuan Zhuo 	struct vring_desc_extra *desc_extra;
145d76136e4SXuan Zhuo 
146d76136e4SXuan Zhuo 	/* DMA address and size information */
147d76136e4SXuan Zhuo 	dma_addr_t ring_dma_addr;
148d76136e4SXuan Zhuo 	dma_addr_t driver_event_dma_addr;
149d76136e4SXuan Zhuo 	dma_addr_t device_event_dma_addr;
150d76136e4SXuan Zhuo 	size_t ring_size_in_bytes;
151d76136e4SXuan Zhuo 	size_t event_size_in_bytes;
152d76136e4SXuan Zhuo };
153d76136e4SXuan Zhuo 
15443b4f721SMichael S. Tsirkin struct vring_virtqueue {
1550a8a69ddSRusty Russell 	struct virtqueue vq;
1560a8a69ddSRusty Russell 
1571ce9e605STiwei Bie 	/* Is this a packed ring? */
1581ce9e605STiwei Bie 	bool packed_ring;
1591ce9e605STiwei Bie 
160fb3fba6bSTiwei Bie 	/* Is DMA API used? */
161fb3fba6bSTiwei Bie 	bool use_dma_api;
162fb3fba6bSTiwei Bie 
1637b21e34fSRusty Russell 	/* Can we use weak barriers? */
1647b21e34fSRusty Russell 	bool weak_barriers;
1657b21e34fSRusty Russell 
1660a8a69ddSRusty Russell 	/* Other side has made a mess, don't try any more. */
1670a8a69ddSRusty Russell 	bool broken;
1680a8a69ddSRusty Russell 
1699fa29b9dSMark McLoughlin 	/* Host supports indirect buffers */
1709fa29b9dSMark McLoughlin 	bool indirect;
1719fa29b9dSMark McLoughlin 
172a5c262c5SMichael S. Tsirkin 	/* Host publishes avail event idx */
173a5c262c5SMichael S. Tsirkin 	bool event;
174a5c262c5SMichael S. Tsirkin 
1750a8a69ddSRusty Russell 	/* Head of free buffer list. */
1760a8a69ddSRusty Russell 	unsigned int free_head;
1770a8a69ddSRusty Russell 	/* Number we've added since last sync. */
1780a8a69ddSRusty Russell 	unsigned int num_added;
1790a8a69ddSRusty Russell 
180a7722890Shuangjie.albert 	/* Last used index  we've seen.
181a7722890Shuangjie.albert 	 * for split ring, it just contains last used index
182a7722890Shuangjie.albert 	 * for packed ring:
183a7722890Shuangjie.albert 	 * bits up to VRING_PACKED_EVENT_F_WRAP_CTR include the last used index.
184a7722890Shuangjie.albert 	 * bits from VRING_PACKED_EVENT_F_WRAP_CTR include the used wrap counter.
185a7722890Shuangjie.albert 	 */
1861bc4953eSAnthony Liguori 	u16 last_used_idx;
1870a8a69ddSRusty Russell 
1888d622d21SMichael S. Tsirkin 	/* Hint for event idx: already triggered no need to disable. */
1898d622d21SMichael S. Tsirkin 	bool event_triggered;
1908d622d21SMichael S. Tsirkin 
1911ce9e605STiwei Bie 	union {
1921ce9e605STiwei Bie 		/* Available for split ring */
193d76136e4SXuan Zhuo 		struct vring_virtqueue_split split;
194f277ec42SVenkatesh Srinivas 
1951ce9e605STiwei Bie 		/* Available for packed ring */
196d76136e4SXuan Zhuo 		struct vring_virtqueue_packed packed;
1971ce9e605STiwei Bie 	};
1981ce9e605STiwei Bie 
1990a8a69ddSRusty Russell 	/* How to notify other side. FIXME: commonalize hcalls! */
20046f9c2b9SHeinz Graalfs 	bool (*notify)(struct virtqueue *vq);
2010a8a69ddSRusty Russell 
2022a2d1382SAndy Lutomirski 	/* DMA, allocation, and size information */
2032a2d1382SAndy Lutomirski 	bool we_own_ring;
2042a2d1382SAndy Lutomirski 
2050a8a69ddSRusty Russell #ifdef DEBUG
2060a8a69ddSRusty Russell 	/* They're supposed to lock for us. */
2070a8a69ddSRusty Russell 	unsigned int in_use;
208e93300b1SRusty Russell 
209e93300b1SRusty Russell 	/* Figure out if their kicks are too delayed. */
210e93300b1SRusty Russell 	bool last_add_time_valid;
211e93300b1SRusty Russell 	ktime_t last_add_time;
2120a8a69ddSRusty Russell #endif
2130a8a69ddSRusty Russell };
2140a8a69ddSRusty Russell 
21507d9629dSXuan Zhuo static struct virtqueue *__vring_new_virtqueue(unsigned int index,
216cd4c812aSXuan Zhuo 					       struct vring_virtqueue_split *vring_split,
21707d9629dSXuan Zhuo 					       struct virtio_device *vdev,
21807d9629dSXuan Zhuo 					       bool weak_barriers,
21907d9629dSXuan Zhuo 					       bool context,
22007d9629dSXuan Zhuo 					       bool (*notify)(struct virtqueue *),
22107d9629dSXuan Zhuo 					       void (*callback)(struct virtqueue *),
22207d9629dSXuan Zhuo 					       const char *name);
223a2b36c8dSXuan Zhuo static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num);
2246fea20e5SXuan Zhuo static void vring_free(struct virtqueue *_vq);
225e6f633e5STiwei Bie 
226e6f633e5STiwei Bie /*
227e6f633e5STiwei Bie  * Helpers.
228e6f633e5STiwei Bie  */
229e6f633e5STiwei Bie 
2300a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
2310a8a69ddSRusty Russell 
23235c51e09SXianting Tian static inline bool virtqueue_use_indirect(struct vring_virtqueue *vq,
2332f18c2d1STiwei Bie 					  unsigned int total_sg)
2342f18c2d1STiwei Bie {
2352f18c2d1STiwei Bie 	/*
2362f18c2d1STiwei Bie 	 * If the host supports indirect descriptor tables, and we have multiple
2372f18c2d1STiwei Bie 	 * buffers, then go indirect. FIXME: tune this threshold
2382f18c2d1STiwei Bie 	 */
2392f18c2d1STiwei Bie 	return (vq->indirect && total_sg > 1 && vq->vq.num_free);
2402f18c2d1STiwei Bie }
2412f18c2d1STiwei Bie 
242d26c96c8SAndy Lutomirski /*
2431a937693SMichael S. Tsirkin  * Modern virtio devices have feature bits to specify whether they need a
2441a937693SMichael S. Tsirkin  * quirk and bypass the IOMMU. If not there, just use the DMA API.
2451a937693SMichael S. Tsirkin  *
2461a937693SMichael S. Tsirkin  * If there, the interaction between virtio and DMA API is messy.
247d26c96c8SAndy Lutomirski  *
248d26c96c8SAndy Lutomirski  * On most systems with virtio, physical addresses match bus addresses,
249d26c96c8SAndy Lutomirski  * and it doesn't particularly matter whether we use the DMA API.
250d26c96c8SAndy Lutomirski  *
251d26c96c8SAndy Lutomirski  * On some systems, including Xen and any system with a physical device
252d26c96c8SAndy Lutomirski  * that speaks virtio behind a physical IOMMU, we must use the DMA API
253d26c96c8SAndy Lutomirski  * for virtio DMA to work at all.
254d26c96c8SAndy Lutomirski  *
255d26c96c8SAndy Lutomirski  * On other systems, including SPARC and PPC64, virtio-pci devices are
256d26c96c8SAndy Lutomirski  * enumerated as though they are behind an IOMMU, but the virtio host
257d26c96c8SAndy Lutomirski  * ignores the IOMMU, so we must either pretend that the IOMMU isn't
258d26c96c8SAndy Lutomirski  * there or somehow map everything as the identity.
259d26c96c8SAndy Lutomirski  *
260d26c96c8SAndy Lutomirski  * For the time being, we preserve historic behavior and bypass the DMA
261d26c96c8SAndy Lutomirski  * API.
2621a937693SMichael S. Tsirkin  *
2631a937693SMichael S. Tsirkin  * TODO: install a per-device DMA ops structure that does the right thing
2641a937693SMichael S. Tsirkin  * taking into account all the above quirks, and use the DMA API
2651a937693SMichael S. Tsirkin  * unconditionally on data path.
266d26c96c8SAndy Lutomirski  */
267d26c96c8SAndy Lutomirski 
268d26c96c8SAndy Lutomirski static bool vring_use_dma_api(struct virtio_device *vdev)
269d26c96c8SAndy Lutomirski {
27024b6842aSMichael S. Tsirkin 	if (!virtio_has_dma_quirk(vdev))
2711a937693SMichael S. Tsirkin 		return true;
2721a937693SMichael S. Tsirkin 
2731a937693SMichael S. Tsirkin 	/* Otherwise, we are left to guess. */
27478fe3987SAndy Lutomirski 	/*
27578fe3987SAndy Lutomirski 	 * In theory, it's possible to have a buggy QEMU-supposed
27678fe3987SAndy Lutomirski 	 * emulated Q35 IOMMU and Xen enabled at the same time.  On
27778fe3987SAndy Lutomirski 	 * such a configuration, virtio has never worked and will
27878fe3987SAndy Lutomirski 	 * not work without an even larger kludge.  Instead, enable
27978fe3987SAndy Lutomirski 	 * the DMA API if we're a Xen guest, which at least allows
28078fe3987SAndy Lutomirski 	 * all of the sensible Xen configurations to work correctly.
28178fe3987SAndy Lutomirski 	 */
28278fe3987SAndy Lutomirski 	if (xen_domain())
28378fe3987SAndy Lutomirski 		return true;
28478fe3987SAndy Lutomirski 
285d26c96c8SAndy Lutomirski 	return false;
286d26c96c8SAndy Lutomirski }
287d26c96c8SAndy Lutomirski 
288e6d6dd6cSJoerg Roedel size_t virtio_max_dma_size(struct virtio_device *vdev)
289e6d6dd6cSJoerg Roedel {
290e6d6dd6cSJoerg Roedel 	size_t max_segment_size = SIZE_MAX;
291e6d6dd6cSJoerg Roedel 
292e6d6dd6cSJoerg Roedel 	if (vring_use_dma_api(vdev))
293817fc978SWill Deacon 		max_segment_size = dma_max_mapping_size(vdev->dev.parent);
294e6d6dd6cSJoerg Roedel 
295e6d6dd6cSJoerg Roedel 	return max_segment_size;
296e6d6dd6cSJoerg Roedel }
297e6d6dd6cSJoerg Roedel EXPORT_SYMBOL_GPL(virtio_max_dma_size);
298e6d6dd6cSJoerg Roedel 
299d79dca75STiwei Bie static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
300d79dca75STiwei Bie 			      dma_addr_t *dma_handle, gfp_t flag)
301d79dca75STiwei Bie {
302d79dca75STiwei Bie 	if (vring_use_dma_api(vdev)) {
303d79dca75STiwei Bie 		return dma_alloc_coherent(vdev->dev.parent, size,
304d79dca75STiwei Bie 					  dma_handle, flag);
305d79dca75STiwei Bie 	} else {
306d79dca75STiwei Bie 		void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
307d79dca75STiwei Bie 
308d79dca75STiwei Bie 		if (queue) {
309d79dca75STiwei Bie 			phys_addr_t phys_addr = virt_to_phys(queue);
310d79dca75STiwei Bie 			*dma_handle = (dma_addr_t)phys_addr;
311d79dca75STiwei Bie 
312d79dca75STiwei Bie 			/*
313d79dca75STiwei Bie 			 * Sanity check: make sure we dind't truncate
314d79dca75STiwei Bie 			 * the address.  The only arches I can find that
315d79dca75STiwei Bie 			 * have 64-bit phys_addr_t but 32-bit dma_addr_t
316d79dca75STiwei Bie 			 * are certain non-highmem MIPS and x86
317d79dca75STiwei Bie 			 * configurations, but these configurations
318d79dca75STiwei Bie 			 * should never allocate physical pages above 32
319d79dca75STiwei Bie 			 * bits, so this is fine.  Just in case, throw a
320d79dca75STiwei Bie 			 * warning and abort if we end up with an
321d79dca75STiwei Bie 			 * unrepresentable address.
322d79dca75STiwei Bie 			 */
323d79dca75STiwei Bie 			if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
324d79dca75STiwei Bie 				free_pages_exact(queue, PAGE_ALIGN(size));
325d79dca75STiwei Bie 				return NULL;
326d79dca75STiwei Bie 			}
327d79dca75STiwei Bie 		}
328d79dca75STiwei Bie 		return queue;
329d79dca75STiwei Bie 	}
330d79dca75STiwei Bie }
331d79dca75STiwei Bie 
332d79dca75STiwei Bie static void vring_free_queue(struct virtio_device *vdev, size_t size,
333d79dca75STiwei Bie 			     void *queue, dma_addr_t dma_handle)
334d79dca75STiwei Bie {
335d79dca75STiwei Bie 	if (vring_use_dma_api(vdev))
336d79dca75STiwei Bie 		dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
337d79dca75STiwei Bie 	else
338d79dca75STiwei Bie 		free_pages_exact(queue, PAGE_ALIGN(size));
339d79dca75STiwei Bie }
340d79dca75STiwei Bie 
341780bc790SAndy Lutomirski /*
342780bc790SAndy Lutomirski  * The DMA ops on various arches are rather gnarly right now, and
343780bc790SAndy Lutomirski  * making all of the arch DMA ops work on the vring device itself
344780bc790SAndy Lutomirski  * is a mess.  For now, we use the parent device for DMA ops.
345780bc790SAndy Lutomirski  */
34675bfa81bSMichael S. Tsirkin static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq)
347780bc790SAndy Lutomirski {
348780bc790SAndy Lutomirski 	return vq->vq.vdev->dev.parent;
349780bc790SAndy Lutomirski }
350780bc790SAndy Lutomirski 
351780bc790SAndy Lutomirski /* Map one sg entry. */
352780bc790SAndy Lutomirski static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
353780bc790SAndy Lutomirski 				   struct scatterlist *sg,
354780bc790SAndy Lutomirski 				   enum dma_data_direction direction)
355780bc790SAndy Lutomirski {
356*88938359SAlexander Potapenko 	if (!vq->use_dma_api) {
357*88938359SAlexander Potapenko 		/*
358*88938359SAlexander Potapenko 		 * If DMA is not used, KMSAN doesn't know that the scatterlist
359*88938359SAlexander Potapenko 		 * is initialized by the hardware. Explicitly check/unpoison it
360*88938359SAlexander Potapenko 		 * depending on the direction.
361*88938359SAlexander Potapenko 		 */
362*88938359SAlexander Potapenko 		kmsan_handle_dma(sg_page(sg), sg->offset, sg->length, direction);
363780bc790SAndy Lutomirski 		return (dma_addr_t)sg_phys(sg);
364*88938359SAlexander Potapenko 	}
365780bc790SAndy Lutomirski 
366780bc790SAndy Lutomirski 	/*
367780bc790SAndy Lutomirski 	 * We can't use dma_map_sg, because we don't use scatterlists in
368780bc790SAndy Lutomirski 	 * the way it expects (we don't guarantee that the scatterlist
369780bc790SAndy Lutomirski 	 * will exist for the lifetime of the mapping).
370780bc790SAndy Lutomirski 	 */
371780bc790SAndy Lutomirski 	return dma_map_page(vring_dma_dev(vq),
372780bc790SAndy Lutomirski 			    sg_page(sg), sg->offset, sg->length,
373780bc790SAndy Lutomirski 			    direction);
374780bc790SAndy Lutomirski }
375780bc790SAndy Lutomirski 
376780bc790SAndy Lutomirski static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
377780bc790SAndy Lutomirski 				   void *cpu_addr, size_t size,
378780bc790SAndy Lutomirski 				   enum dma_data_direction direction)
379780bc790SAndy Lutomirski {
380fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
381780bc790SAndy Lutomirski 		return (dma_addr_t)virt_to_phys(cpu_addr);
382780bc790SAndy Lutomirski 
383780bc790SAndy Lutomirski 	return dma_map_single(vring_dma_dev(vq),
384780bc790SAndy Lutomirski 			      cpu_addr, size, direction);
385780bc790SAndy Lutomirski }
386780bc790SAndy Lutomirski 
387e6f633e5STiwei Bie static int vring_mapping_error(const struct vring_virtqueue *vq,
388e6f633e5STiwei Bie 			       dma_addr_t addr)
389e6f633e5STiwei Bie {
390fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
391e6f633e5STiwei Bie 		return 0;
392e6f633e5STiwei Bie 
393e6f633e5STiwei Bie 	return dma_mapping_error(vring_dma_dev(vq), addr);
394e6f633e5STiwei Bie }
395e6f633e5STiwei Bie 
3963a897128SXuan Zhuo static void virtqueue_init(struct vring_virtqueue *vq, u32 num)
3973a897128SXuan Zhuo {
3983a897128SXuan Zhuo 	vq->vq.num_free = num;
3993a897128SXuan Zhuo 
4003a897128SXuan Zhuo 	if (vq->packed_ring)
4013a897128SXuan Zhuo 		vq->last_used_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR);
4023a897128SXuan Zhuo 	else
4033a897128SXuan Zhuo 		vq->last_used_idx = 0;
4043a897128SXuan Zhuo 
4053a897128SXuan Zhuo 	vq->event_triggered = false;
4063a897128SXuan Zhuo 	vq->num_added = 0;
4073a897128SXuan Zhuo 
4083a897128SXuan Zhuo #ifdef DEBUG
4093a897128SXuan Zhuo 	vq->in_use = false;
4103a897128SXuan Zhuo 	vq->last_add_time_valid = false;
4113a897128SXuan Zhuo #endif
4123a897128SXuan Zhuo }
4133a897128SXuan Zhuo 
414e6f633e5STiwei Bie 
415e6f633e5STiwei Bie /*
416e6f633e5STiwei Bie  * Split ring specific functions - *_split().
417e6f633e5STiwei Bie  */
418e6f633e5STiwei Bie 
41972b5e895SJason Wang static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
420780bc790SAndy Lutomirski 					   struct vring_desc *desc)
421780bc790SAndy Lutomirski {
422780bc790SAndy Lutomirski 	u16 flags;
423780bc790SAndy Lutomirski 
424fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
425780bc790SAndy Lutomirski 		return;
426780bc790SAndy Lutomirski 
427780bc790SAndy Lutomirski 	flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
428780bc790SAndy Lutomirski 
429780bc790SAndy Lutomirski 	dma_unmap_page(vring_dma_dev(vq),
430780bc790SAndy Lutomirski 		       virtio64_to_cpu(vq->vq.vdev, desc->addr),
431780bc790SAndy Lutomirski 		       virtio32_to_cpu(vq->vq.vdev, desc->len),
432780bc790SAndy Lutomirski 		       (flags & VRING_DESC_F_WRITE) ?
433780bc790SAndy Lutomirski 		       DMA_FROM_DEVICE : DMA_TO_DEVICE);
434780bc790SAndy Lutomirski }
435780bc790SAndy Lutomirski 
43672b5e895SJason Wang static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
43772b5e895SJason Wang 					  unsigned int i)
43872b5e895SJason Wang {
43972b5e895SJason Wang 	struct vring_desc_extra *extra = vq->split.desc_extra;
44072b5e895SJason Wang 	u16 flags;
44172b5e895SJason Wang 
44272b5e895SJason Wang 	if (!vq->use_dma_api)
44372b5e895SJason Wang 		goto out;
44472b5e895SJason Wang 
44572b5e895SJason Wang 	flags = extra[i].flags;
44672b5e895SJason Wang 
44772b5e895SJason Wang 	if (flags & VRING_DESC_F_INDIRECT) {
44872b5e895SJason Wang 		dma_unmap_single(vring_dma_dev(vq),
44972b5e895SJason Wang 				 extra[i].addr,
45072b5e895SJason Wang 				 extra[i].len,
45172b5e895SJason Wang 				 (flags & VRING_DESC_F_WRITE) ?
45272b5e895SJason Wang 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
45372b5e895SJason Wang 	} else {
45472b5e895SJason Wang 		dma_unmap_page(vring_dma_dev(vq),
45572b5e895SJason Wang 			       extra[i].addr,
45672b5e895SJason Wang 			       extra[i].len,
45772b5e895SJason Wang 			       (flags & VRING_DESC_F_WRITE) ?
45872b5e895SJason Wang 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
45972b5e895SJason Wang 	}
46072b5e895SJason Wang 
46172b5e895SJason Wang out:
46272b5e895SJason Wang 	return extra[i].next;
46372b5e895SJason Wang }
46472b5e895SJason Wang 
465138fd251STiwei Bie static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
466138fd251STiwei Bie 					       unsigned int total_sg,
467138fd251STiwei Bie 					       gfp_t gfp)
4689fa29b9dSMark McLoughlin {
4699fa29b9dSMark McLoughlin 	struct vring_desc *desc;
470b25bd251SRusty Russell 	unsigned int i;
4719fa29b9dSMark McLoughlin 
472b92b1b89SWill Deacon 	/*
473b92b1b89SWill Deacon 	 * We require lowmem mappings for the descriptors because
474b92b1b89SWill Deacon 	 * otherwise virt_to_phys will give us bogus addresses in the
475b92b1b89SWill Deacon 	 * virtqueue.
476b92b1b89SWill Deacon 	 */
47782107539SMichal Hocko 	gfp &= ~__GFP_HIGHMEM;
478b92b1b89SWill Deacon 
4796da2ec56SKees Cook 	desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
4809fa29b9dSMark McLoughlin 	if (!desc)
481b25bd251SRusty Russell 		return NULL;
4829fa29b9dSMark McLoughlin 
483b25bd251SRusty Russell 	for (i = 0; i < total_sg; i++)
48400e6f3d9SMichael S. Tsirkin 		desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
485b25bd251SRusty Russell 	return desc;
4869fa29b9dSMark McLoughlin }
4879fa29b9dSMark McLoughlin 
488fe4c3862SJason Wang static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
489fe4c3862SJason Wang 						    struct vring_desc *desc,
490fe4c3862SJason Wang 						    unsigned int i,
491fe4c3862SJason Wang 						    dma_addr_t addr,
492fe4c3862SJason Wang 						    unsigned int len,
49372b5e895SJason Wang 						    u16 flags,
49472b5e895SJason Wang 						    bool indirect)
495fe4c3862SJason Wang {
49672b5e895SJason Wang 	struct vring_virtqueue *vring = to_vvq(vq);
49772b5e895SJason Wang 	struct vring_desc_extra *extra = vring->split.desc_extra;
49872b5e895SJason Wang 	u16 next;
49972b5e895SJason Wang 
500fe4c3862SJason Wang 	desc[i].flags = cpu_to_virtio16(vq->vdev, flags);
501fe4c3862SJason Wang 	desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
502fe4c3862SJason Wang 	desc[i].len = cpu_to_virtio32(vq->vdev, len);
503fe4c3862SJason Wang 
50472b5e895SJason Wang 	if (!indirect) {
50572b5e895SJason Wang 		next = extra[i].next;
50672b5e895SJason Wang 		desc[i].next = cpu_to_virtio16(vq->vdev, next);
50772b5e895SJason Wang 
50872b5e895SJason Wang 		extra[i].addr = addr;
50972b5e895SJason Wang 		extra[i].len = len;
51072b5e895SJason Wang 		extra[i].flags = flags;
51172b5e895SJason Wang 	} else
51272b5e895SJason Wang 		next = virtio16_to_cpu(vq->vdev, desc[i].next);
51372b5e895SJason Wang 
51472b5e895SJason Wang 	return next;
515fe4c3862SJason Wang }
516fe4c3862SJason Wang 
517138fd251STiwei Bie static inline int virtqueue_add_split(struct virtqueue *_vq,
51813816c76SRusty Russell 				      struct scatterlist *sgs[],
519eeebf9b1SRusty Russell 				      unsigned int total_sg,
52013816c76SRusty Russell 				      unsigned int out_sgs,
52113816c76SRusty Russell 				      unsigned int in_sgs,
522bbd603efSMichael S. Tsirkin 				      void *data,
5235a08b04fSMichael S. Tsirkin 				      void *ctx,
524bbd603efSMichael S. Tsirkin 				      gfp_t gfp)
5250a8a69ddSRusty Russell {
5260a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
52713816c76SRusty Russell 	struct scatterlist *sg;
528b25bd251SRusty Russell 	struct vring_desc *desc;
5293f649ab7SKees Cook 	unsigned int i, n, avail, descs_used, prev, err_idx;
5301fe9b6feSMichael S. Tsirkin 	int head;
531b25bd251SRusty Russell 	bool indirect;
5320a8a69ddSRusty Russell 
5339fa29b9dSMark McLoughlin 	START_USE(vq);
5349fa29b9dSMark McLoughlin 
5350a8a69ddSRusty Russell 	BUG_ON(data == NULL);
5365a08b04fSMichael S. Tsirkin 	BUG_ON(ctx && vq->indirect);
5379fa29b9dSMark McLoughlin 
53870670444SRusty Russell 	if (unlikely(vq->broken)) {
53970670444SRusty Russell 		END_USE(vq);
54070670444SRusty Russell 		return -EIO;
54170670444SRusty Russell 	}
54270670444SRusty Russell 
5434d6a105eSTiwei Bie 	LAST_ADD_TIME_UPDATE(vq);
544e93300b1SRusty Russell 
54513816c76SRusty Russell 	BUG_ON(total_sg == 0);
5460a8a69ddSRusty Russell 
547b25bd251SRusty Russell 	head = vq->free_head;
548b25bd251SRusty Russell 
54935c51e09SXianting Tian 	if (virtqueue_use_indirect(vq, total_sg))
550138fd251STiwei Bie 		desc = alloc_indirect_split(_vq, total_sg, gfp);
55144ed8089SRichard W.M. Jones 	else {
552b25bd251SRusty Russell 		desc = NULL;
553e593bf97STiwei Bie 		WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
55444ed8089SRichard W.M. Jones 	}
555b25bd251SRusty Russell 
556b25bd251SRusty Russell 	if (desc) {
557b25bd251SRusty Russell 		/* Use a single buffer which doesn't continue */
558780bc790SAndy Lutomirski 		indirect = true;
559b25bd251SRusty Russell 		/* Set up rest to use this indirect table. */
560b25bd251SRusty Russell 		i = 0;
561b25bd251SRusty Russell 		descs_used = 1;
562b25bd251SRusty Russell 	} else {
563780bc790SAndy Lutomirski 		indirect = false;
564e593bf97STiwei Bie 		desc = vq->split.vring.desc;
565b25bd251SRusty Russell 		i = head;
566b25bd251SRusty Russell 		descs_used = total_sg;
567b25bd251SRusty Russell 	}
568b25bd251SRusty Russell 
569b4b4ff73SXianting Tian 	if (unlikely(vq->vq.num_free < descs_used)) {
5700a8a69ddSRusty Russell 		pr_debug("Can't add buf len %i - avail = %i\n",
571b25bd251SRusty Russell 			 descs_used, vq->vq.num_free);
57244653eaeSRusty Russell 		/* FIXME: for historical reasons, we force a notify here if
57344653eaeSRusty Russell 		 * there are outgoing parts to the buffer.  Presumably the
57444653eaeSRusty Russell 		 * host should service the ring ASAP. */
57513816c76SRusty Russell 		if (out_sgs)
576426e3e0aSRusty Russell 			vq->notify(&vq->vq);
57758625edfSWei Yongjun 		if (indirect)
57858625edfSWei Yongjun 			kfree(desc);
5790a8a69ddSRusty Russell 		END_USE(vq);
5800a8a69ddSRusty Russell 		return -ENOSPC;
5810a8a69ddSRusty Russell 	}
5820a8a69ddSRusty Russell 
58313816c76SRusty Russell 	for (n = 0; n < out_sgs; n++) {
584eeebf9b1SRusty Russell 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
585780bc790SAndy Lutomirski 			dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
586780bc790SAndy Lutomirski 			if (vring_mapping_error(vq, addr))
587780bc790SAndy Lutomirski 				goto unmap_release;
588780bc790SAndy Lutomirski 
5890a8a69ddSRusty Russell 			prev = i;
59072b5e895SJason Wang 			/* Note that we trust indirect descriptor
59172b5e895SJason Wang 			 * table since it use stream DMA mapping.
59272b5e895SJason Wang 			 */
593fe4c3862SJason Wang 			i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length,
59472b5e895SJason Wang 						     VRING_DESC_F_NEXT,
59572b5e895SJason Wang 						     indirect);
5960a8a69ddSRusty Russell 		}
59713816c76SRusty Russell 	}
59813816c76SRusty Russell 	for (; n < (out_sgs + in_sgs); n++) {
599eeebf9b1SRusty Russell 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
600780bc790SAndy Lutomirski 			dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
601780bc790SAndy Lutomirski 			if (vring_mapping_error(vq, addr))
602780bc790SAndy Lutomirski 				goto unmap_release;
603780bc790SAndy Lutomirski 
6040a8a69ddSRusty Russell 			prev = i;
60572b5e895SJason Wang 			/* Note that we trust indirect descriptor
60672b5e895SJason Wang 			 * table since it use stream DMA mapping.
60772b5e895SJason Wang 			 */
608fe4c3862SJason Wang 			i = virtqueue_add_desc_split(_vq, desc, i, addr,
609fe4c3862SJason Wang 						     sg->length,
610fe4c3862SJason Wang 						     VRING_DESC_F_NEXT |
61172b5e895SJason Wang 						     VRING_DESC_F_WRITE,
61272b5e895SJason Wang 						     indirect);
61313816c76SRusty Russell 		}
6140a8a69ddSRusty Russell 	}
6150a8a69ddSRusty Russell 	/* Last one doesn't continue. */
61600e6f3d9SMichael S. Tsirkin 	desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
61772b5e895SJason Wang 	if (!indirect && vq->use_dma_api)
618890d3356SVincent Whitchurch 		vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
61972b5e895SJason Wang 			~VRING_DESC_F_NEXT;
6200a8a69ddSRusty Russell 
621780bc790SAndy Lutomirski 	if (indirect) {
622780bc790SAndy Lutomirski 		/* Now that the indirect table is filled in, map it. */
623780bc790SAndy Lutomirski 		dma_addr_t addr = vring_map_single(
624780bc790SAndy Lutomirski 			vq, desc, total_sg * sizeof(struct vring_desc),
625780bc790SAndy Lutomirski 			DMA_TO_DEVICE);
626780bc790SAndy Lutomirski 		if (vring_mapping_error(vq, addr))
627780bc790SAndy Lutomirski 			goto unmap_release;
628780bc790SAndy Lutomirski 
629fe4c3862SJason Wang 		virtqueue_add_desc_split(_vq, vq->split.vring.desc,
630fe4c3862SJason Wang 					 head, addr,
631fe4c3862SJason Wang 					 total_sg * sizeof(struct vring_desc),
63272b5e895SJason Wang 					 VRING_DESC_F_INDIRECT,
63372b5e895SJason Wang 					 false);
634780bc790SAndy Lutomirski 	}
635780bc790SAndy Lutomirski 
636780bc790SAndy Lutomirski 	/* We're using some buffers from the free list. */
637780bc790SAndy Lutomirski 	vq->vq.num_free -= descs_used;
638780bc790SAndy Lutomirski 
6390a8a69ddSRusty Russell 	/* Update free pointer */
640b25bd251SRusty Russell 	if (indirect)
64172b5e895SJason Wang 		vq->free_head = vq->split.desc_extra[head].next;
642b25bd251SRusty Russell 	else
6430a8a69ddSRusty Russell 		vq->free_head = i;
6440a8a69ddSRusty Russell 
645780bc790SAndy Lutomirski 	/* Store token and indirect buffer state. */
646cbeedb72STiwei Bie 	vq->split.desc_state[head].data = data;
647780bc790SAndy Lutomirski 	if (indirect)
648cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = desc;
64987646a34SJason Wang 	else
650cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = ctx;
6510a8a69ddSRusty Russell 
6520a8a69ddSRusty Russell 	/* Put entry in available array (but don't update avail->idx until they
6533b720b8cSRusty Russell 	 * do sync). */
654e593bf97STiwei Bie 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
655e593bf97STiwei Bie 	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
6560a8a69ddSRusty Russell 
657ee7cd898SRusty Russell 	/* Descriptors and available array need to be set before we expose the
658ee7cd898SRusty Russell 	 * new available array entries. */
659a9a0fef7SRusty Russell 	virtio_wmb(vq->weak_barriers);
660e593bf97STiwei Bie 	vq->split.avail_idx_shadow++;
661e593bf97STiwei Bie 	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
662e593bf97STiwei Bie 						vq->split.avail_idx_shadow);
663ee7cd898SRusty Russell 	vq->num_added++;
664ee7cd898SRusty Russell 
6655e05bf58STetsuo Handa 	pr_debug("Added buffer head %i to %p\n", head, vq);
6665e05bf58STetsuo Handa 	END_USE(vq);
6675e05bf58STetsuo Handa 
668ee7cd898SRusty Russell 	/* This is very unlikely, but theoretically possible.  Kick
669ee7cd898SRusty Russell 	 * just in case. */
670ee7cd898SRusty Russell 	if (unlikely(vq->num_added == (1 << 16) - 1))
671ee7cd898SRusty Russell 		virtqueue_kick(_vq);
672ee7cd898SRusty Russell 
67398e8c6bcSRusty Russell 	return 0;
674780bc790SAndy Lutomirski 
675780bc790SAndy Lutomirski unmap_release:
676780bc790SAndy Lutomirski 	err_idx = i;
677cf8f1696SMatthias Lange 
678cf8f1696SMatthias Lange 	if (indirect)
679cf8f1696SMatthias Lange 		i = 0;
680cf8f1696SMatthias Lange 	else
681780bc790SAndy Lutomirski 		i = head;
682780bc790SAndy Lutomirski 
683780bc790SAndy Lutomirski 	for (n = 0; n < total_sg; n++) {
684780bc790SAndy Lutomirski 		if (i == err_idx)
685780bc790SAndy Lutomirski 			break;
68672b5e895SJason Wang 		if (indirect) {
68772b5e895SJason Wang 			vring_unmap_one_split_indirect(vq, &desc[i]);
688cf8f1696SMatthias Lange 			i = virtio16_to_cpu(_vq->vdev, desc[i].next);
68972b5e895SJason Wang 		} else
69072b5e895SJason Wang 			i = vring_unmap_one_split(vq, i);
691780bc790SAndy Lutomirski 	}
692780bc790SAndy Lutomirski 
693780bc790SAndy Lutomirski 	if (indirect)
694780bc790SAndy Lutomirski 		kfree(desc);
695780bc790SAndy Lutomirski 
6963cc36f6eSMichael S. Tsirkin 	END_USE(vq);
697f7728002SHalil Pasic 	return -ENOMEM;
6980a8a69ddSRusty Russell }
69913816c76SRusty Russell 
700138fd251STiwei Bie static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
7010a8a69ddSRusty Russell {
7020a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
703a5c262c5SMichael S. Tsirkin 	u16 new, old;
70441f0377fSRusty Russell 	bool needs_kick;
70541f0377fSRusty Russell 
7060a8a69ddSRusty Russell 	START_USE(vq);
707a72caae2SJason Wang 	/* We need to expose available array entries before checking avail
708a72caae2SJason Wang 	 * event. */
709a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
7100a8a69ddSRusty Russell 
711e593bf97STiwei Bie 	old = vq->split.avail_idx_shadow - vq->num_added;
712e593bf97STiwei Bie 	new = vq->split.avail_idx_shadow;
7130a8a69ddSRusty Russell 	vq->num_added = 0;
7140a8a69ddSRusty Russell 
7154d6a105eSTiwei Bie 	LAST_ADD_TIME_CHECK(vq);
7164d6a105eSTiwei Bie 	LAST_ADD_TIME_INVALID(vq);
717e93300b1SRusty Russell 
71841f0377fSRusty Russell 	if (vq->event) {
719e593bf97STiwei Bie 		needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
720e593bf97STiwei Bie 					vring_avail_event(&vq->split.vring)),
72141f0377fSRusty Russell 					      new, old);
72241f0377fSRusty Russell 	} else {
723e593bf97STiwei Bie 		needs_kick = !(vq->split.vring.used->flags &
724e593bf97STiwei Bie 					cpu_to_virtio16(_vq->vdev,
725e593bf97STiwei Bie 						VRING_USED_F_NO_NOTIFY));
72641f0377fSRusty Russell 	}
7270a8a69ddSRusty Russell 	END_USE(vq);
72841f0377fSRusty Russell 	return needs_kick;
72941f0377fSRusty Russell }
730138fd251STiwei Bie 
731138fd251STiwei Bie static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
7325a08b04fSMichael S. Tsirkin 			     void **ctx)
7330a8a69ddSRusty Russell {
734780bc790SAndy Lutomirski 	unsigned int i, j;
735c60923cbSGonglei 	__virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
7360a8a69ddSRusty Russell 
7370a8a69ddSRusty Russell 	/* Clear data ptr. */
738cbeedb72STiwei Bie 	vq->split.desc_state[head].data = NULL;
7390a8a69ddSRusty Russell 
740780bc790SAndy Lutomirski 	/* Put back on free list: unmap first-level descriptors and find end */
7410a8a69ddSRusty Russell 	i = head;
7429fa29b9dSMark McLoughlin 
743e593bf97STiwei Bie 	while (vq->split.vring.desc[i].flags & nextflag) {
74472b5e895SJason Wang 		vring_unmap_one_split(vq, i);
74572b5e895SJason Wang 		i = vq->split.desc_extra[i].next;
74606ca287dSRusty Russell 		vq->vq.num_free++;
7470a8a69ddSRusty Russell 	}
7480a8a69ddSRusty Russell 
74972b5e895SJason Wang 	vring_unmap_one_split(vq, i);
75072b5e895SJason Wang 	vq->split.desc_extra[i].next = vq->free_head;
7510a8a69ddSRusty Russell 	vq->free_head = head;
752780bc790SAndy Lutomirski 
7530a8a69ddSRusty Russell 	/* Plus final descriptor */
75406ca287dSRusty Russell 	vq->vq.num_free++;
755780bc790SAndy Lutomirski 
7565a08b04fSMichael S. Tsirkin 	if (vq->indirect) {
757cbeedb72STiwei Bie 		struct vring_desc *indir_desc =
758cbeedb72STiwei Bie 				vq->split.desc_state[head].indir_desc;
7595a08b04fSMichael S. Tsirkin 		u32 len;
7605a08b04fSMichael S. Tsirkin 
7615a08b04fSMichael S. Tsirkin 		/* Free the indirect table, if any, now that it's unmapped. */
7625a08b04fSMichael S. Tsirkin 		if (!indir_desc)
7635a08b04fSMichael S. Tsirkin 			return;
7645a08b04fSMichael S. Tsirkin 
76572b5e895SJason Wang 		len = vq->split.desc_extra[head].len;
766780bc790SAndy Lutomirski 
76772b5e895SJason Wang 		BUG_ON(!(vq->split.desc_extra[head].flags &
76872b5e895SJason Wang 				VRING_DESC_F_INDIRECT));
769780bc790SAndy Lutomirski 		BUG_ON(len == 0 || len % sizeof(struct vring_desc));
770780bc790SAndy Lutomirski 
771780bc790SAndy Lutomirski 		for (j = 0; j < len / sizeof(struct vring_desc); j++)
77272b5e895SJason Wang 			vring_unmap_one_split_indirect(vq, &indir_desc[j]);
773780bc790SAndy Lutomirski 
7745a08b04fSMichael S. Tsirkin 		kfree(indir_desc);
775cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = NULL;
7765a08b04fSMichael S. Tsirkin 	} else if (ctx) {
777cbeedb72STiwei Bie 		*ctx = vq->split.desc_state[head].indir_desc;
778780bc790SAndy Lutomirski 	}
7790a8a69ddSRusty Russell }
7800a8a69ddSRusty Russell 
781138fd251STiwei Bie static inline bool more_used_split(const struct vring_virtqueue *vq)
7820a8a69ddSRusty Russell {
783e593bf97STiwei Bie 	return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
784e593bf97STiwei Bie 			vq->split.vring.used->idx);
7850a8a69ddSRusty Russell }
7860a8a69ddSRusty Russell 
787138fd251STiwei Bie static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
788138fd251STiwei Bie 					 unsigned int *len,
7895a08b04fSMichael S. Tsirkin 					 void **ctx)
7900a8a69ddSRusty Russell {
7910a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
7920a8a69ddSRusty Russell 	void *ret;
7930a8a69ddSRusty Russell 	unsigned int i;
7943b720b8cSRusty Russell 	u16 last_used;
7950a8a69ddSRusty Russell 
7960a8a69ddSRusty Russell 	START_USE(vq);
7970a8a69ddSRusty Russell 
7985ef82752SRusty Russell 	if (unlikely(vq->broken)) {
7995ef82752SRusty Russell 		END_USE(vq);
8005ef82752SRusty Russell 		return NULL;
8015ef82752SRusty Russell 	}
8025ef82752SRusty Russell 
803138fd251STiwei Bie 	if (!more_used_split(vq)) {
8040a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
8050a8a69ddSRusty Russell 		END_USE(vq);
8060a8a69ddSRusty Russell 		return NULL;
8070a8a69ddSRusty Russell 	}
8080a8a69ddSRusty Russell 
8092d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
810a9a0fef7SRusty Russell 	virtio_rmb(vq->weak_barriers);
8112d61ba95SMichael S. Tsirkin 
812e593bf97STiwei Bie 	last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
813e593bf97STiwei Bie 	i = virtio32_to_cpu(_vq->vdev,
814e593bf97STiwei Bie 			vq->split.vring.used->ring[last_used].id);
815e593bf97STiwei Bie 	*len = virtio32_to_cpu(_vq->vdev,
816e593bf97STiwei Bie 			vq->split.vring.used->ring[last_used].len);
8170a8a69ddSRusty Russell 
818e593bf97STiwei Bie 	if (unlikely(i >= vq->split.vring.num)) {
8190a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
8200a8a69ddSRusty Russell 		return NULL;
8210a8a69ddSRusty Russell 	}
822cbeedb72STiwei Bie 	if (unlikely(!vq->split.desc_state[i].data)) {
8230a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
8240a8a69ddSRusty Russell 		return NULL;
8250a8a69ddSRusty Russell 	}
8260a8a69ddSRusty Russell 
827138fd251STiwei Bie 	/* detach_buf_split clears data, so grab it now. */
828cbeedb72STiwei Bie 	ret = vq->split.desc_state[i].data;
829138fd251STiwei Bie 	detach_buf_split(vq, i, ctx);
8300a8a69ddSRusty Russell 	vq->last_used_idx++;
831a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
832a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
833a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
834e593bf97STiwei Bie 	if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
835788e5b3aSMichael S. Tsirkin 		virtio_store_mb(vq->weak_barriers,
836e593bf97STiwei Bie 				&vring_used_event(&vq->split.vring),
837788e5b3aSMichael S. Tsirkin 				cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
838a5c262c5SMichael S. Tsirkin 
8394d6a105eSTiwei Bie 	LAST_ADD_TIME_INVALID(vq);
840e93300b1SRusty Russell 
8410a8a69ddSRusty Russell 	END_USE(vq);
8420a8a69ddSRusty Russell 	return ret;
8430a8a69ddSRusty Russell }
844138fd251STiwei Bie 
845138fd251STiwei Bie static void virtqueue_disable_cb_split(struct virtqueue *_vq)
846138fd251STiwei Bie {
847138fd251STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
848138fd251STiwei Bie 
849e593bf97STiwei Bie 	if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
850e593bf97STiwei Bie 		vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
8518d622d21SMichael S. Tsirkin 		if (vq->event)
8528d622d21SMichael S. Tsirkin 			/* TODO: this is a hack. Figure out a cleaner value to write. */
8538d622d21SMichael S. Tsirkin 			vring_used_event(&vq->split.vring) = 0x0;
8548d622d21SMichael S. Tsirkin 		else
855e593bf97STiwei Bie 			vq->split.vring.avail->flags =
856e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
857e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
858138fd251STiwei Bie 	}
859138fd251STiwei Bie }
860138fd251STiwei Bie 
86131532340SSolomon Tan static unsigned int virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
862cc229884SMichael S. Tsirkin {
863cc229884SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
864cc229884SMichael S. Tsirkin 	u16 last_used_idx;
865cc229884SMichael S. Tsirkin 
866cc229884SMichael S. Tsirkin 	START_USE(vq);
867cc229884SMichael S. Tsirkin 
868cc229884SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
869cc229884SMichael S. Tsirkin 	 * more to do. */
870cc229884SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
871cc229884SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
872cc229884SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
873e593bf97STiwei Bie 	if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
874e593bf97STiwei Bie 		vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
8750ea1e4a6SLadi Prosek 		if (!vq->event)
876e593bf97STiwei Bie 			vq->split.vring.avail->flags =
877e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
878e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
879f277ec42SVenkatesh Srinivas 	}
880e593bf97STiwei Bie 	vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
881e593bf97STiwei Bie 			last_used_idx = vq->last_used_idx);
882cc229884SMichael S. Tsirkin 	END_USE(vq);
883cc229884SMichael S. Tsirkin 	return last_used_idx;
884cc229884SMichael S. Tsirkin }
885138fd251STiwei Bie 
88631532340SSolomon Tan static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned int last_used_idx)
887138fd251STiwei Bie {
888138fd251STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
889138fd251STiwei Bie 
890138fd251STiwei Bie 	return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
891e593bf97STiwei Bie 			vq->split.vring.used->idx);
892138fd251STiwei Bie }
893138fd251STiwei Bie 
894138fd251STiwei Bie static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
8957ab358c2SMichael S. Tsirkin {
8967ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
8977ab358c2SMichael S. Tsirkin 	u16 bufs;
8987ab358c2SMichael S. Tsirkin 
8997ab358c2SMichael S. Tsirkin 	START_USE(vq);
9007ab358c2SMichael S. Tsirkin 
9017ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
9027ab358c2SMichael S. Tsirkin 	 * more to do. */
9037ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
9047ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
9050ea1e4a6SLadi Prosek 	 * entry. Always update the event index to keep code simple. */
906e593bf97STiwei Bie 	if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
907e593bf97STiwei Bie 		vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
9080ea1e4a6SLadi Prosek 		if (!vq->event)
909e593bf97STiwei Bie 			vq->split.vring.avail->flags =
910e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
911e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
912f277ec42SVenkatesh Srinivas 	}
9137ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
914e593bf97STiwei Bie 	bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
915788e5b3aSMichael S. Tsirkin 
916788e5b3aSMichael S. Tsirkin 	virtio_store_mb(vq->weak_barriers,
917e593bf97STiwei Bie 			&vring_used_event(&vq->split.vring),
918788e5b3aSMichael S. Tsirkin 			cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
919788e5b3aSMichael S. Tsirkin 
920e593bf97STiwei Bie 	if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
921e593bf97STiwei Bie 					- vq->last_used_idx) > bufs)) {
9227ab358c2SMichael S. Tsirkin 		END_USE(vq);
9237ab358c2SMichael S. Tsirkin 		return false;
9247ab358c2SMichael S. Tsirkin 	}
9257ab358c2SMichael S. Tsirkin 
9267ab358c2SMichael S. Tsirkin 	END_USE(vq);
9277ab358c2SMichael S. Tsirkin 	return true;
9287ab358c2SMichael S. Tsirkin }
9297ab358c2SMichael S. Tsirkin 
930138fd251STiwei Bie static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
931c021eac4SShirley Ma {
932c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
933c021eac4SShirley Ma 	unsigned int i;
934c021eac4SShirley Ma 	void *buf;
935c021eac4SShirley Ma 
936c021eac4SShirley Ma 	START_USE(vq);
937c021eac4SShirley Ma 
938e593bf97STiwei Bie 	for (i = 0; i < vq->split.vring.num; i++) {
939cbeedb72STiwei Bie 		if (!vq->split.desc_state[i].data)
940c021eac4SShirley Ma 			continue;
941138fd251STiwei Bie 		/* detach_buf_split clears data, so grab it now. */
942cbeedb72STiwei Bie 		buf = vq->split.desc_state[i].data;
943138fd251STiwei Bie 		detach_buf_split(vq, i, NULL);
944e593bf97STiwei Bie 		vq->split.avail_idx_shadow--;
945e593bf97STiwei Bie 		vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
946e593bf97STiwei Bie 				vq->split.avail_idx_shadow);
947c021eac4SShirley Ma 		END_USE(vq);
948c021eac4SShirley Ma 		return buf;
949c021eac4SShirley Ma 	}
950c021eac4SShirley Ma 	/* That should have freed everything. */
951e593bf97STiwei Bie 	BUG_ON(vq->vq.num_free != vq->split.vring.num);
952c021eac4SShirley Ma 
953c021eac4SShirley Ma 	END_USE(vq);
954c021eac4SShirley Ma 	return NULL;
955c021eac4SShirley Ma }
956138fd251STiwei Bie 
957198fa7beSXuan Zhuo static void virtqueue_vring_init_split(struct vring_virtqueue_split *vring_split,
958198fa7beSXuan Zhuo 				       struct vring_virtqueue *vq)
959198fa7beSXuan Zhuo {
960198fa7beSXuan Zhuo 	struct virtio_device *vdev;
961198fa7beSXuan Zhuo 
962198fa7beSXuan Zhuo 	vdev = vq->vq.vdev;
963198fa7beSXuan Zhuo 
964198fa7beSXuan Zhuo 	vring_split->avail_flags_shadow = 0;
965198fa7beSXuan Zhuo 	vring_split->avail_idx_shadow = 0;
966198fa7beSXuan Zhuo 
967198fa7beSXuan Zhuo 	/* No callback?  Tell other side not to bother us. */
968198fa7beSXuan Zhuo 	if (!vq->vq.callback) {
969198fa7beSXuan Zhuo 		vring_split->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
970198fa7beSXuan Zhuo 		if (!vq->event)
971198fa7beSXuan Zhuo 			vring_split->vring.avail->flags = cpu_to_virtio16(vdev,
972198fa7beSXuan Zhuo 					vring_split->avail_flags_shadow);
973198fa7beSXuan Zhuo 	}
974198fa7beSXuan Zhuo }
975198fa7beSXuan Zhuo 
976e5175b41SXuan Zhuo static void virtqueue_reinit_split(struct vring_virtqueue *vq)
977e5175b41SXuan Zhuo {
978e5175b41SXuan Zhuo 	int num;
979e5175b41SXuan Zhuo 
980e5175b41SXuan Zhuo 	num = vq->split.vring.num;
981e5175b41SXuan Zhuo 
982e5175b41SXuan Zhuo 	vq->split.vring.avail->flags = 0;
983e5175b41SXuan Zhuo 	vq->split.vring.avail->idx = 0;
984e5175b41SXuan Zhuo 
985e5175b41SXuan Zhuo 	/* reset avail event */
986e5175b41SXuan Zhuo 	vq->split.vring.avail->ring[num] = 0;
987e5175b41SXuan Zhuo 
988e5175b41SXuan Zhuo 	vq->split.vring.used->flags = 0;
989e5175b41SXuan Zhuo 	vq->split.vring.used->idx = 0;
990e5175b41SXuan Zhuo 
991e5175b41SXuan Zhuo 	/* reset used event */
992e5175b41SXuan Zhuo 	*(__virtio16 *)&(vq->split.vring.used->ring[num]) = 0;
993e5175b41SXuan Zhuo 
994e5175b41SXuan Zhuo 	virtqueue_init(vq, num);
995e5175b41SXuan Zhuo 
996e5175b41SXuan Zhuo 	virtqueue_vring_init_split(&vq->split, vq);
997e5175b41SXuan Zhuo }
998e5175b41SXuan Zhuo 
999e1d6a423SXuan Zhuo static void virtqueue_vring_attach_split(struct vring_virtqueue *vq,
1000e1d6a423SXuan Zhuo 					 struct vring_virtqueue_split *vring_split)
1001e1d6a423SXuan Zhuo {
1002e1d6a423SXuan Zhuo 	vq->split = *vring_split;
1003e1d6a423SXuan Zhuo 
1004e1d6a423SXuan Zhuo 	/* Put everything in free lists. */
1005e1d6a423SXuan Zhuo 	vq->free_head = 0;
1006e1d6a423SXuan Zhuo }
1007e1d6a423SXuan Zhuo 
1008a2b36c8dSXuan Zhuo static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vring_split)
1009a2b36c8dSXuan Zhuo {
1010a2b36c8dSXuan Zhuo 	struct vring_desc_state_split *state;
1011a2b36c8dSXuan Zhuo 	struct vring_desc_extra *extra;
1012a2b36c8dSXuan Zhuo 	u32 num = vring_split->vring.num;
1013a2b36c8dSXuan Zhuo 
1014a2b36c8dSXuan Zhuo 	state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL);
1015a2b36c8dSXuan Zhuo 	if (!state)
1016a2b36c8dSXuan Zhuo 		goto err_state;
1017a2b36c8dSXuan Zhuo 
1018a2b36c8dSXuan Zhuo 	extra = vring_alloc_desc_extra(num);
1019a2b36c8dSXuan Zhuo 	if (!extra)
1020a2b36c8dSXuan Zhuo 		goto err_extra;
1021a2b36c8dSXuan Zhuo 
1022a2b36c8dSXuan Zhuo 	memset(state, 0, num * sizeof(struct vring_desc_state_split));
1023a2b36c8dSXuan Zhuo 
1024a2b36c8dSXuan Zhuo 	vring_split->desc_state = state;
1025a2b36c8dSXuan Zhuo 	vring_split->desc_extra = extra;
1026a2b36c8dSXuan Zhuo 	return 0;
1027a2b36c8dSXuan Zhuo 
1028a2b36c8dSXuan Zhuo err_extra:
1029a2b36c8dSXuan Zhuo 	kfree(state);
1030a2b36c8dSXuan Zhuo err_state:
1031a2b36c8dSXuan Zhuo 	return -ENOMEM;
1032a2b36c8dSXuan Zhuo }
1033a2b36c8dSXuan Zhuo 
103489f05d94SXuan Zhuo static void vring_free_split(struct vring_virtqueue_split *vring_split,
103589f05d94SXuan Zhuo 			     struct virtio_device *vdev)
103689f05d94SXuan Zhuo {
103789f05d94SXuan Zhuo 	vring_free_queue(vdev, vring_split->queue_size_in_bytes,
103889f05d94SXuan Zhuo 			 vring_split->vring.desc,
103989f05d94SXuan Zhuo 			 vring_split->queue_dma_addr);
104089f05d94SXuan Zhuo 
104189f05d94SXuan Zhuo 	kfree(vring_split->desc_state);
104289f05d94SXuan Zhuo 	kfree(vring_split->desc_extra);
104389f05d94SXuan Zhuo }
104489f05d94SXuan Zhuo 
1045c2d87fe6SXuan Zhuo static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
1046c2d87fe6SXuan Zhuo 				   struct virtio_device *vdev,
1047c2d87fe6SXuan Zhuo 				   u32 num,
1048c2d87fe6SXuan Zhuo 				   unsigned int vring_align,
1049c2d87fe6SXuan Zhuo 				   bool may_reduce_num)
1050c2d87fe6SXuan Zhuo {
1051c2d87fe6SXuan Zhuo 	void *queue = NULL;
1052c2d87fe6SXuan Zhuo 	dma_addr_t dma_addr;
1053c2d87fe6SXuan Zhuo 
1054c2d87fe6SXuan Zhuo 	/* We assume num is a power of 2. */
1055c2d87fe6SXuan Zhuo 	if (num & (num - 1)) {
1056c2d87fe6SXuan Zhuo 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
1057c2d87fe6SXuan Zhuo 		return -EINVAL;
1058c2d87fe6SXuan Zhuo 	}
1059c2d87fe6SXuan Zhuo 
1060c2d87fe6SXuan Zhuo 	/* TODO: allocate each queue chunk individually */
1061c2d87fe6SXuan Zhuo 	for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
1062c2d87fe6SXuan Zhuo 		queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1063c2d87fe6SXuan Zhuo 					  &dma_addr,
1064c2d87fe6SXuan Zhuo 					  GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
1065c2d87fe6SXuan Zhuo 		if (queue)
1066c2d87fe6SXuan Zhuo 			break;
1067c2d87fe6SXuan Zhuo 		if (!may_reduce_num)
1068c2d87fe6SXuan Zhuo 			return -ENOMEM;
1069c2d87fe6SXuan Zhuo 	}
1070c2d87fe6SXuan Zhuo 
1071c2d87fe6SXuan Zhuo 	if (!num)
1072c2d87fe6SXuan Zhuo 		return -ENOMEM;
1073c2d87fe6SXuan Zhuo 
1074c2d87fe6SXuan Zhuo 	if (!queue) {
1075c2d87fe6SXuan Zhuo 		/* Try to get a single page. You are my only hope! */
1076c2d87fe6SXuan Zhuo 		queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1077c2d87fe6SXuan Zhuo 					  &dma_addr, GFP_KERNEL|__GFP_ZERO);
1078c2d87fe6SXuan Zhuo 	}
1079c2d87fe6SXuan Zhuo 	if (!queue)
1080c2d87fe6SXuan Zhuo 		return -ENOMEM;
1081c2d87fe6SXuan Zhuo 
1082c2d87fe6SXuan Zhuo 	vring_init(&vring_split->vring, num, queue, vring_align);
1083c2d87fe6SXuan Zhuo 
1084c2d87fe6SXuan Zhuo 	vring_split->queue_dma_addr = dma_addr;
1085c2d87fe6SXuan Zhuo 	vring_split->queue_size_in_bytes = vring_size(num, vring_align);
1086c2d87fe6SXuan Zhuo 
1087af36b16fSXuan Zhuo 	vring_split->vring_align = vring_align;
1088af36b16fSXuan Zhuo 	vring_split->may_reduce_num = may_reduce_num;
1089af36b16fSXuan Zhuo 
1090c2d87fe6SXuan Zhuo 	return 0;
1091c2d87fe6SXuan Zhuo }
1092c2d87fe6SXuan Zhuo 
1093d79dca75STiwei Bie static struct virtqueue *vring_create_virtqueue_split(
1094d79dca75STiwei Bie 	unsigned int index,
1095d79dca75STiwei Bie 	unsigned int num,
1096d79dca75STiwei Bie 	unsigned int vring_align,
1097d79dca75STiwei Bie 	struct virtio_device *vdev,
1098d79dca75STiwei Bie 	bool weak_barriers,
1099d79dca75STiwei Bie 	bool may_reduce_num,
1100d79dca75STiwei Bie 	bool context,
1101d79dca75STiwei Bie 	bool (*notify)(struct virtqueue *),
1102d79dca75STiwei Bie 	void (*callback)(struct virtqueue *),
1103d79dca75STiwei Bie 	const char *name)
1104d79dca75STiwei Bie {
1105cd4c812aSXuan Zhuo 	struct vring_virtqueue_split vring_split = {};
1106d79dca75STiwei Bie 	struct virtqueue *vq;
1107c2d87fe6SXuan Zhuo 	int err;
1108d79dca75STiwei Bie 
1109c2d87fe6SXuan Zhuo 	err = vring_alloc_queue_split(&vring_split, vdev, num, vring_align,
1110c2d87fe6SXuan Zhuo 				      may_reduce_num);
1111c2d87fe6SXuan Zhuo 	if (err)
1112d79dca75STiwei Bie 		return NULL;
1113d79dca75STiwei Bie 
1114cd4c812aSXuan Zhuo 	vq = __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
1115cd4c812aSXuan Zhuo 				   context, notify, callback, name);
1116d79dca75STiwei Bie 	if (!vq) {
1117c2d87fe6SXuan Zhuo 		vring_free_split(&vring_split, vdev);
1118d79dca75STiwei Bie 		return NULL;
1119d79dca75STiwei Bie 	}
1120d79dca75STiwei Bie 
1121d79dca75STiwei Bie 	to_vvq(vq)->we_own_ring = true;
1122d79dca75STiwei Bie 
1123d79dca75STiwei Bie 	return vq;
1124d79dca75STiwei Bie }
1125d79dca75STiwei Bie 
11266fea20e5SXuan Zhuo static int virtqueue_resize_split(struct virtqueue *_vq, u32 num)
11276fea20e5SXuan Zhuo {
11286fea20e5SXuan Zhuo 	struct vring_virtqueue_split vring_split = {};
11296fea20e5SXuan Zhuo 	struct vring_virtqueue *vq = to_vvq(_vq);
11306fea20e5SXuan Zhuo 	struct virtio_device *vdev = _vq->vdev;
11316fea20e5SXuan Zhuo 	int err;
11326fea20e5SXuan Zhuo 
11336fea20e5SXuan Zhuo 	err = vring_alloc_queue_split(&vring_split, vdev, num,
11346fea20e5SXuan Zhuo 				      vq->split.vring_align,
11356fea20e5SXuan Zhuo 				      vq->split.may_reduce_num);
11366fea20e5SXuan Zhuo 	if (err)
11376fea20e5SXuan Zhuo 		goto err;
11386fea20e5SXuan Zhuo 
11396fea20e5SXuan Zhuo 	err = vring_alloc_state_extra_split(&vring_split);
11406fea20e5SXuan Zhuo 	if (err)
11416fea20e5SXuan Zhuo 		goto err_state_extra;
11426fea20e5SXuan Zhuo 
11436fea20e5SXuan Zhuo 	vring_free(&vq->vq);
11446fea20e5SXuan Zhuo 
11456fea20e5SXuan Zhuo 	virtqueue_vring_init_split(&vring_split, vq);
11466fea20e5SXuan Zhuo 
11476fea20e5SXuan Zhuo 	virtqueue_init(vq, vring_split.vring.num);
11486fea20e5SXuan Zhuo 	virtqueue_vring_attach_split(vq, &vring_split);
11496fea20e5SXuan Zhuo 
11506fea20e5SXuan Zhuo 	return 0;
11516fea20e5SXuan Zhuo 
11526fea20e5SXuan Zhuo err_state_extra:
11536fea20e5SXuan Zhuo 	vring_free_split(&vring_split, vdev);
11546fea20e5SXuan Zhuo err:
11556fea20e5SXuan Zhuo 	virtqueue_reinit_split(vq);
11566fea20e5SXuan Zhuo 	return -ENOMEM;
11576fea20e5SXuan Zhuo }
11586fea20e5SXuan Zhuo 
1159e6f633e5STiwei Bie 
1160e6f633e5STiwei Bie /*
11611ce9e605STiwei Bie  * Packed ring specific functions - *_packed().
11621ce9e605STiwei Bie  */
1163a7722890Shuangjie.albert static inline bool packed_used_wrap_counter(u16 last_used_idx)
1164a7722890Shuangjie.albert {
1165a7722890Shuangjie.albert 	return !!(last_used_idx & (1 << VRING_PACKED_EVENT_F_WRAP_CTR));
1166a7722890Shuangjie.albert }
1167a7722890Shuangjie.albert 
1168a7722890Shuangjie.albert static inline u16 packed_last_used(u16 last_used_idx)
1169a7722890Shuangjie.albert {
1170a7722890Shuangjie.albert 	return last_used_idx & ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR));
1171a7722890Shuangjie.albert }
11721ce9e605STiwei Bie 
1173d80dc15bSXuan Zhuo static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
1174d80dc15bSXuan Zhuo 				     struct vring_desc_extra *extra)
11751ce9e605STiwei Bie {
11761ce9e605STiwei Bie 	u16 flags;
11771ce9e605STiwei Bie 
11781ce9e605STiwei Bie 	if (!vq->use_dma_api)
11791ce9e605STiwei Bie 		return;
11801ce9e605STiwei Bie 
1181d80dc15bSXuan Zhuo 	flags = extra->flags;
11821ce9e605STiwei Bie 
11831ce9e605STiwei Bie 	if (flags & VRING_DESC_F_INDIRECT) {
11841ce9e605STiwei Bie 		dma_unmap_single(vring_dma_dev(vq),
1185d80dc15bSXuan Zhuo 				 extra->addr, extra->len,
11861ce9e605STiwei Bie 				 (flags & VRING_DESC_F_WRITE) ?
11871ce9e605STiwei Bie 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
11881ce9e605STiwei Bie 	} else {
11891ce9e605STiwei Bie 		dma_unmap_page(vring_dma_dev(vq),
1190d80dc15bSXuan Zhuo 			       extra->addr, extra->len,
11911ce9e605STiwei Bie 			       (flags & VRING_DESC_F_WRITE) ?
11921ce9e605STiwei Bie 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
11931ce9e605STiwei Bie 	}
11941ce9e605STiwei Bie }
11951ce9e605STiwei Bie 
11961ce9e605STiwei Bie static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
11971ce9e605STiwei Bie 				   struct vring_packed_desc *desc)
11981ce9e605STiwei Bie {
11991ce9e605STiwei Bie 	u16 flags;
12001ce9e605STiwei Bie 
12011ce9e605STiwei Bie 	if (!vq->use_dma_api)
12021ce9e605STiwei Bie 		return;
12031ce9e605STiwei Bie 
12041ce9e605STiwei Bie 	flags = le16_to_cpu(desc->flags);
12051ce9e605STiwei Bie 
12061ce9e605STiwei Bie 	dma_unmap_page(vring_dma_dev(vq),
12071ce9e605STiwei Bie 		       le64_to_cpu(desc->addr),
12081ce9e605STiwei Bie 		       le32_to_cpu(desc->len),
12091ce9e605STiwei Bie 		       (flags & VRING_DESC_F_WRITE) ?
12101ce9e605STiwei Bie 		       DMA_FROM_DEVICE : DMA_TO_DEVICE);
12111ce9e605STiwei Bie }
12121ce9e605STiwei Bie 
12131ce9e605STiwei Bie static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
12141ce9e605STiwei Bie 						       gfp_t gfp)
12151ce9e605STiwei Bie {
12161ce9e605STiwei Bie 	struct vring_packed_desc *desc;
12171ce9e605STiwei Bie 
12181ce9e605STiwei Bie 	/*
12191ce9e605STiwei Bie 	 * We require lowmem mappings for the descriptors because
12201ce9e605STiwei Bie 	 * otherwise virt_to_phys will give us bogus addresses in the
12211ce9e605STiwei Bie 	 * virtqueue.
12221ce9e605STiwei Bie 	 */
12231ce9e605STiwei Bie 	gfp &= ~__GFP_HIGHMEM;
12241ce9e605STiwei Bie 
12251ce9e605STiwei Bie 	desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
12261ce9e605STiwei Bie 
12271ce9e605STiwei Bie 	return desc;
12281ce9e605STiwei Bie }
12291ce9e605STiwei Bie 
12301ce9e605STiwei Bie static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
12311ce9e605STiwei Bie 					 struct scatterlist *sgs[],
12321ce9e605STiwei Bie 					 unsigned int total_sg,
12331ce9e605STiwei Bie 					 unsigned int out_sgs,
12341ce9e605STiwei Bie 					 unsigned int in_sgs,
12351ce9e605STiwei Bie 					 void *data,
12361ce9e605STiwei Bie 					 gfp_t gfp)
12371ce9e605STiwei Bie {
12381ce9e605STiwei Bie 	struct vring_packed_desc *desc;
12391ce9e605STiwei Bie 	struct scatterlist *sg;
12401ce9e605STiwei Bie 	unsigned int i, n, err_idx;
12411ce9e605STiwei Bie 	u16 head, id;
12421ce9e605STiwei Bie 	dma_addr_t addr;
12431ce9e605STiwei Bie 
12441ce9e605STiwei Bie 	head = vq->packed.next_avail_idx;
12451ce9e605STiwei Bie 	desc = alloc_indirect_packed(total_sg, gfp);
1246fc6d70f4SXuan Zhuo 	if (!desc)
1247fc6d70f4SXuan Zhuo 		return -ENOMEM;
12481ce9e605STiwei Bie 
12491ce9e605STiwei Bie 	if (unlikely(vq->vq.num_free < 1)) {
12501ce9e605STiwei Bie 		pr_debug("Can't add buf len 1 - avail = 0\n");
1251df0bfe75SYueHaibing 		kfree(desc);
12521ce9e605STiwei Bie 		END_USE(vq);
12531ce9e605STiwei Bie 		return -ENOSPC;
12541ce9e605STiwei Bie 	}
12551ce9e605STiwei Bie 
12561ce9e605STiwei Bie 	i = 0;
12571ce9e605STiwei Bie 	id = vq->free_head;
12581ce9e605STiwei Bie 	BUG_ON(id == vq->packed.vring.num);
12591ce9e605STiwei Bie 
12601ce9e605STiwei Bie 	for (n = 0; n < out_sgs + in_sgs; n++) {
12611ce9e605STiwei Bie 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
12621ce9e605STiwei Bie 			addr = vring_map_one_sg(vq, sg, n < out_sgs ?
12631ce9e605STiwei Bie 					DMA_TO_DEVICE : DMA_FROM_DEVICE);
12641ce9e605STiwei Bie 			if (vring_mapping_error(vq, addr))
12651ce9e605STiwei Bie 				goto unmap_release;
12661ce9e605STiwei Bie 
12671ce9e605STiwei Bie 			desc[i].flags = cpu_to_le16(n < out_sgs ?
12681ce9e605STiwei Bie 						0 : VRING_DESC_F_WRITE);
12691ce9e605STiwei Bie 			desc[i].addr = cpu_to_le64(addr);
12701ce9e605STiwei Bie 			desc[i].len = cpu_to_le32(sg->length);
12711ce9e605STiwei Bie 			i++;
12721ce9e605STiwei Bie 		}
12731ce9e605STiwei Bie 	}
12741ce9e605STiwei Bie 
12751ce9e605STiwei Bie 	/* Now that the indirect table is filled in, map it. */
12761ce9e605STiwei Bie 	addr = vring_map_single(vq, desc,
12771ce9e605STiwei Bie 			total_sg * sizeof(struct vring_packed_desc),
12781ce9e605STiwei Bie 			DMA_TO_DEVICE);
12791ce9e605STiwei Bie 	if (vring_mapping_error(vq, addr))
12801ce9e605STiwei Bie 		goto unmap_release;
12811ce9e605STiwei Bie 
12821ce9e605STiwei Bie 	vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
12831ce9e605STiwei Bie 	vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
12841ce9e605STiwei Bie 				sizeof(struct vring_packed_desc));
12851ce9e605STiwei Bie 	vq->packed.vring.desc[head].id = cpu_to_le16(id);
12861ce9e605STiwei Bie 
12871ce9e605STiwei Bie 	if (vq->use_dma_api) {
12881ce9e605STiwei Bie 		vq->packed.desc_extra[id].addr = addr;
12891ce9e605STiwei Bie 		vq->packed.desc_extra[id].len = total_sg *
12901ce9e605STiwei Bie 				sizeof(struct vring_packed_desc);
12911ce9e605STiwei Bie 		vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
12921ce9e605STiwei Bie 						  vq->packed.avail_used_flags;
12931ce9e605STiwei Bie 	}
12941ce9e605STiwei Bie 
12951ce9e605STiwei Bie 	/*
12961ce9e605STiwei Bie 	 * A driver MUST NOT make the first descriptor in the list
12971ce9e605STiwei Bie 	 * available before all subsequent descriptors comprising
12981ce9e605STiwei Bie 	 * the list are made available.
12991ce9e605STiwei Bie 	 */
13001ce9e605STiwei Bie 	virtio_wmb(vq->weak_barriers);
13011ce9e605STiwei Bie 	vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
13021ce9e605STiwei Bie 						vq->packed.avail_used_flags);
13031ce9e605STiwei Bie 
13041ce9e605STiwei Bie 	/* We're using some buffers from the free list. */
13051ce9e605STiwei Bie 	vq->vq.num_free -= 1;
13061ce9e605STiwei Bie 
13071ce9e605STiwei Bie 	/* Update free pointer */
13081ce9e605STiwei Bie 	n = head + 1;
13091ce9e605STiwei Bie 	if (n >= vq->packed.vring.num) {
13101ce9e605STiwei Bie 		n = 0;
13111ce9e605STiwei Bie 		vq->packed.avail_wrap_counter ^= 1;
13121ce9e605STiwei Bie 		vq->packed.avail_used_flags ^=
13131ce9e605STiwei Bie 				1 << VRING_PACKED_DESC_F_AVAIL |
13141ce9e605STiwei Bie 				1 << VRING_PACKED_DESC_F_USED;
13151ce9e605STiwei Bie 	}
13161ce9e605STiwei Bie 	vq->packed.next_avail_idx = n;
1317aeef9b47SJason Wang 	vq->free_head = vq->packed.desc_extra[id].next;
13181ce9e605STiwei Bie 
13191ce9e605STiwei Bie 	/* Store token and indirect buffer state. */
13201ce9e605STiwei Bie 	vq->packed.desc_state[id].num = 1;
13211ce9e605STiwei Bie 	vq->packed.desc_state[id].data = data;
13221ce9e605STiwei Bie 	vq->packed.desc_state[id].indir_desc = desc;
13231ce9e605STiwei Bie 	vq->packed.desc_state[id].last = id;
13241ce9e605STiwei Bie 
13251ce9e605STiwei Bie 	vq->num_added += 1;
13261ce9e605STiwei Bie 
13271ce9e605STiwei Bie 	pr_debug("Added buffer head %i to %p\n", head, vq);
13281ce9e605STiwei Bie 	END_USE(vq);
13291ce9e605STiwei Bie 
13301ce9e605STiwei Bie 	return 0;
13311ce9e605STiwei Bie 
13321ce9e605STiwei Bie unmap_release:
13331ce9e605STiwei Bie 	err_idx = i;
13341ce9e605STiwei Bie 
13351ce9e605STiwei Bie 	for (i = 0; i < err_idx; i++)
13361ce9e605STiwei Bie 		vring_unmap_desc_packed(vq, &desc[i]);
13371ce9e605STiwei Bie 
13381ce9e605STiwei Bie 	kfree(desc);
13391ce9e605STiwei Bie 
13401ce9e605STiwei Bie 	END_USE(vq);
1341f7728002SHalil Pasic 	return -ENOMEM;
13421ce9e605STiwei Bie }
13431ce9e605STiwei Bie 
13441ce9e605STiwei Bie static inline int virtqueue_add_packed(struct virtqueue *_vq,
13451ce9e605STiwei Bie 				       struct scatterlist *sgs[],
13461ce9e605STiwei Bie 				       unsigned int total_sg,
13471ce9e605STiwei Bie 				       unsigned int out_sgs,
13481ce9e605STiwei Bie 				       unsigned int in_sgs,
13491ce9e605STiwei Bie 				       void *data,
13501ce9e605STiwei Bie 				       void *ctx,
13511ce9e605STiwei Bie 				       gfp_t gfp)
13521ce9e605STiwei Bie {
13531ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
13541ce9e605STiwei Bie 	struct vring_packed_desc *desc;
13551ce9e605STiwei Bie 	struct scatterlist *sg;
13561ce9e605STiwei Bie 	unsigned int i, n, c, descs_used, err_idx;
13573f649ab7SKees Cook 	__le16 head_flags, flags;
13583f649ab7SKees Cook 	u16 head, id, prev, curr, avail_used_flags;
1359fc6d70f4SXuan Zhuo 	int err;
13601ce9e605STiwei Bie 
13611ce9e605STiwei Bie 	START_USE(vq);
13621ce9e605STiwei Bie 
13631ce9e605STiwei Bie 	BUG_ON(data == NULL);
13641ce9e605STiwei Bie 	BUG_ON(ctx && vq->indirect);
13651ce9e605STiwei Bie 
13661ce9e605STiwei Bie 	if (unlikely(vq->broken)) {
13671ce9e605STiwei Bie 		END_USE(vq);
13681ce9e605STiwei Bie 		return -EIO;
13691ce9e605STiwei Bie 	}
13701ce9e605STiwei Bie 
13711ce9e605STiwei Bie 	LAST_ADD_TIME_UPDATE(vq);
13721ce9e605STiwei Bie 
13731ce9e605STiwei Bie 	BUG_ON(total_sg == 0);
13741ce9e605STiwei Bie 
137535c51e09SXianting Tian 	if (virtqueue_use_indirect(vq, total_sg)) {
1376fc6d70f4SXuan Zhuo 		err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
1377fc6d70f4SXuan Zhuo 						    in_sgs, data, gfp);
13781861ba62SMichael S. Tsirkin 		if (err != -ENOMEM) {
13791861ba62SMichael S. Tsirkin 			END_USE(vq);
1380fc6d70f4SXuan Zhuo 			return err;
13811861ba62SMichael S. Tsirkin 		}
1382fc6d70f4SXuan Zhuo 
1383fc6d70f4SXuan Zhuo 		/* fall back on direct */
1384fc6d70f4SXuan Zhuo 	}
13851ce9e605STiwei Bie 
13861ce9e605STiwei Bie 	head = vq->packed.next_avail_idx;
13871ce9e605STiwei Bie 	avail_used_flags = vq->packed.avail_used_flags;
13881ce9e605STiwei Bie 
13891ce9e605STiwei Bie 	WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
13901ce9e605STiwei Bie 
13911ce9e605STiwei Bie 	desc = vq->packed.vring.desc;
13921ce9e605STiwei Bie 	i = head;
13931ce9e605STiwei Bie 	descs_used = total_sg;
13941ce9e605STiwei Bie 
13951ce9e605STiwei Bie 	if (unlikely(vq->vq.num_free < descs_used)) {
13961ce9e605STiwei Bie 		pr_debug("Can't add buf len %i - avail = %i\n",
13971ce9e605STiwei Bie 			 descs_used, vq->vq.num_free);
13981ce9e605STiwei Bie 		END_USE(vq);
13991ce9e605STiwei Bie 		return -ENOSPC;
14001ce9e605STiwei Bie 	}
14011ce9e605STiwei Bie 
14021ce9e605STiwei Bie 	id = vq->free_head;
14031ce9e605STiwei Bie 	BUG_ON(id == vq->packed.vring.num);
14041ce9e605STiwei Bie 
14051ce9e605STiwei Bie 	curr = id;
14061ce9e605STiwei Bie 	c = 0;
14071ce9e605STiwei Bie 	for (n = 0; n < out_sgs + in_sgs; n++) {
14081ce9e605STiwei Bie 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
14091ce9e605STiwei Bie 			dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ?
14101ce9e605STiwei Bie 					DMA_TO_DEVICE : DMA_FROM_DEVICE);
14111ce9e605STiwei Bie 			if (vring_mapping_error(vq, addr))
14121ce9e605STiwei Bie 				goto unmap_release;
14131ce9e605STiwei Bie 
14141ce9e605STiwei Bie 			flags = cpu_to_le16(vq->packed.avail_used_flags |
14151ce9e605STiwei Bie 				    (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
14161ce9e605STiwei Bie 				    (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
14171ce9e605STiwei Bie 			if (i == head)
14181ce9e605STiwei Bie 				head_flags = flags;
14191ce9e605STiwei Bie 			else
14201ce9e605STiwei Bie 				desc[i].flags = flags;
14211ce9e605STiwei Bie 
14221ce9e605STiwei Bie 			desc[i].addr = cpu_to_le64(addr);
14231ce9e605STiwei Bie 			desc[i].len = cpu_to_le32(sg->length);
14241ce9e605STiwei Bie 			desc[i].id = cpu_to_le16(id);
14251ce9e605STiwei Bie 
14261ce9e605STiwei Bie 			if (unlikely(vq->use_dma_api)) {
14271ce9e605STiwei Bie 				vq->packed.desc_extra[curr].addr = addr;
14281ce9e605STiwei Bie 				vq->packed.desc_extra[curr].len = sg->length;
14291ce9e605STiwei Bie 				vq->packed.desc_extra[curr].flags =
14301ce9e605STiwei Bie 					le16_to_cpu(flags);
14311ce9e605STiwei Bie 			}
14321ce9e605STiwei Bie 			prev = curr;
1433aeef9b47SJason Wang 			curr = vq->packed.desc_extra[curr].next;
14341ce9e605STiwei Bie 
14351ce9e605STiwei Bie 			if ((unlikely(++i >= vq->packed.vring.num))) {
14361ce9e605STiwei Bie 				i = 0;
14371ce9e605STiwei Bie 				vq->packed.avail_used_flags ^=
14381ce9e605STiwei Bie 					1 << VRING_PACKED_DESC_F_AVAIL |
14391ce9e605STiwei Bie 					1 << VRING_PACKED_DESC_F_USED;
14401ce9e605STiwei Bie 			}
14411ce9e605STiwei Bie 		}
14421ce9e605STiwei Bie 	}
14431ce9e605STiwei Bie 
14441ce9e605STiwei Bie 	if (i < head)
14451ce9e605STiwei Bie 		vq->packed.avail_wrap_counter ^= 1;
14461ce9e605STiwei Bie 
14471ce9e605STiwei Bie 	/* We're using some buffers from the free list. */
14481ce9e605STiwei Bie 	vq->vq.num_free -= descs_used;
14491ce9e605STiwei Bie 
14501ce9e605STiwei Bie 	/* Update free pointer */
14511ce9e605STiwei Bie 	vq->packed.next_avail_idx = i;
14521ce9e605STiwei Bie 	vq->free_head = curr;
14531ce9e605STiwei Bie 
14541ce9e605STiwei Bie 	/* Store token. */
14551ce9e605STiwei Bie 	vq->packed.desc_state[id].num = descs_used;
14561ce9e605STiwei Bie 	vq->packed.desc_state[id].data = data;
14571ce9e605STiwei Bie 	vq->packed.desc_state[id].indir_desc = ctx;
14581ce9e605STiwei Bie 	vq->packed.desc_state[id].last = prev;
14591ce9e605STiwei Bie 
14601ce9e605STiwei Bie 	/*
14611ce9e605STiwei Bie 	 * A driver MUST NOT make the first descriptor in the list
14621ce9e605STiwei Bie 	 * available before all subsequent descriptors comprising
14631ce9e605STiwei Bie 	 * the list are made available.
14641ce9e605STiwei Bie 	 */
14651ce9e605STiwei Bie 	virtio_wmb(vq->weak_barriers);
14661ce9e605STiwei Bie 	vq->packed.vring.desc[head].flags = head_flags;
14671ce9e605STiwei Bie 	vq->num_added += descs_used;
14681ce9e605STiwei Bie 
14691ce9e605STiwei Bie 	pr_debug("Added buffer head %i to %p\n", head, vq);
14701ce9e605STiwei Bie 	END_USE(vq);
14711ce9e605STiwei Bie 
14721ce9e605STiwei Bie 	return 0;
14731ce9e605STiwei Bie 
14741ce9e605STiwei Bie unmap_release:
14751ce9e605STiwei Bie 	err_idx = i;
14761ce9e605STiwei Bie 	i = head;
147744593865SJason Wang 	curr = vq->free_head;
14781ce9e605STiwei Bie 
14791ce9e605STiwei Bie 	vq->packed.avail_used_flags = avail_used_flags;
14801ce9e605STiwei Bie 
14811ce9e605STiwei Bie 	for (n = 0; n < total_sg; n++) {
14821ce9e605STiwei Bie 		if (i == err_idx)
14831ce9e605STiwei Bie 			break;
1484d80dc15bSXuan Zhuo 		vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]);
148544593865SJason Wang 		curr = vq->packed.desc_extra[curr].next;
14861ce9e605STiwei Bie 		i++;
14871ce9e605STiwei Bie 		if (i >= vq->packed.vring.num)
14881ce9e605STiwei Bie 			i = 0;
14891ce9e605STiwei Bie 	}
14901ce9e605STiwei Bie 
14911ce9e605STiwei Bie 	END_USE(vq);
14921ce9e605STiwei Bie 	return -EIO;
14931ce9e605STiwei Bie }
14941ce9e605STiwei Bie 
14951ce9e605STiwei Bie static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
14961ce9e605STiwei Bie {
14971ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1498f51f9826STiwei Bie 	u16 new, old, off_wrap, flags, wrap_counter, event_idx;
14991ce9e605STiwei Bie 	bool needs_kick;
15001ce9e605STiwei Bie 	union {
15011ce9e605STiwei Bie 		struct {
15021ce9e605STiwei Bie 			__le16 off_wrap;
15031ce9e605STiwei Bie 			__le16 flags;
15041ce9e605STiwei Bie 		};
15051ce9e605STiwei Bie 		u32 u32;
15061ce9e605STiwei Bie 	} snapshot;
15071ce9e605STiwei Bie 
15081ce9e605STiwei Bie 	START_USE(vq);
15091ce9e605STiwei Bie 
15101ce9e605STiwei Bie 	/*
15111ce9e605STiwei Bie 	 * We need to expose the new flags value before checking notification
15121ce9e605STiwei Bie 	 * suppressions.
15131ce9e605STiwei Bie 	 */
15141ce9e605STiwei Bie 	virtio_mb(vq->weak_barriers);
15151ce9e605STiwei Bie 
1516f51f9826STiwei Bie 	old = vq->packed.next_avail_idx - vq->num_added;
1517f51f9826STiwei Bie 	new = vq->packed.next_avail_idx;
15181ce9e605STiwei Bie 	vq->num_added = 0;
15191ce9e605STiwei Bie 
15201ce9e605STiwei Bie 	snapshot.u32 = *(u32 *)vq->packed.vring.device;
15211ce9e605STiwei Bie 	flags = le16_to_cpu(snapshot.flags);
15221ce9e605STiwei Bie 
15231ce9e605STiwei Bie 	LAST_ADD_TIME_CHECK(vq);
15241ce9e605STiwei Bie 	LAST_ADD_TIME_INVALID(vq);
15251ce9e605STiwei Bie 
1526f51f9826STiwei Bie 	if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
15271ce9e605STiwei Bie 		needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1528f51f9826STiwei Bie 		goto out;
1529f51f9826STiwei Bie 	}
1530f51f9826STiwei Bie 
1531f51f9826STiwei Bie 	off_wrap = le16_to_cpu(snapshot.off_wrap);
1532f51f9826STiwei Bie 
1533f51f9826STiwei Bie 	wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1534f51f9826STiwei Bie 	event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1535f51f9826STiwei Bie 	if (wrap_counter != vq->packed.avail_wrap_counter)
1536f51f9826STiwei Bie 		event_idx -= vq->packed.vring.num;
1537f51f9826STiwei Bie 
1538f51f9826STiwei Bie 	needs_kick = vring_need_event(event_idx, new, old);
1539f51f9826STiwei Bie out:
15401ce9e605STiwei Bie 	END_USE(vq);
15411ce9e605STiwei Bie 	return needs_kick;
15421ce9e605STiwei Bie }
15431ce9e605STiwei Bie 
15441ce9e605STiwei Bie static void detach_buf_packed(struct vring_virtqueue *vq,
15451ce9e605STiwei Bie 			      unsigned int id, void **ctx)
15461ce9e605STiwei Bie {
15471ce9e605STiwei Bie 	struct vring_desc_state_packed *state = NULL;
15481ce9e605STiwei Bie 	struct vring_packed_desc *desc;
15491ce9e605STiwei Bie 	unsigned int i, curr;
15501ce9e605STiwei Bie 
15511ce9e605STiwei Bie 	state = &vq->packed.desc_state[id];
15521ce9e605STiwei Bie 
15531ce9e605STiwei Bie 	/* Clear data ptr. */
15541ce9e605STiwei Bie 	state->data = NULL;
15551ce9e605STiwei Bie 
1556aeef9b47SJason Wang 	vq->packed.desc_extra[state->last].next = vq->free_head;
15571ce9e605STiwei Bie 	vq->free_head = id;
15581ce9e605STiwei Bie 	vq->vq.num_free += state->num;
15591ce9e605STiwei Bie 
15601ce9e605STiwei Bie 	if (unlikely(vq->use_dma_api)) {
15611ce9e605STiwei Bie 		curr = id;
15621ce9e605STiwei Bie 		for (i = 0; i < state->num; i++) {
1563d80dc15bSXuan Zhuo 			vring_unmap_extra_packed(vq,
15641ce9e605STiwei Bie 						 &vq->packed.desc_extra[curr]);
1565aeef9b47SJason Wang 			curr = vq->packed.desc_extra[curr].next;
15661ce9e605STiwei Bie 		}
15671ce9e605STiwei Bie 	}
15681ce9e605STiwei Bie 
15691ce9e605STiwei Bie 	if (vq->indirect) {
15701ce9e605STiwei Bie 		u32 len;
15711ce9e605STiwei Bie 
15721ce9e605STiwei Bie 		/* Free the indirect table, if any, now that it's unmapped. */
15731ce9e605STiwei Bie 		desc = state->indir_desc;
15741ce9e605STiwei Bie 		if (!desc)
15751ce9e605STiwei Bie 			return;
15761ce9e605STiwei Bie 
15771ce9e605STiwei Bie 		if (vq->use_dma_api) {
15781ce9e605STiwei Bie 			len = vq->packed.desc_extra[id].len;
15791ce9e605STiwei Bie 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
15801ce9e605STiwei Bie 					i++)
15811ce9e605STiwei Bie 				vring_unmap_desc_packed(vq, &desc[i]);
15821ce9e605STiwei Bie 		}
15831ce9e605STiwei Bie 		kfree(desc);
15841ce9e605STiwei Bie 		state->indir_desc = NULL;
15851ce9e605STiwei Bie 	} else if (ctx) {
15861ce9e605STiwei Bie 		*ctx = state->indir_desc;
15871ce9e605STiwei Bie 	}
15881ce9e605STiwei Bie }
15891ce9e605STiwei Bie 
15901ce9e605STiwei Bie static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
15911ce9e605STiwei Bie 				       u16 idx, bool used_wrap_counter)
15921ce9e605STiwei Bie {
15931ce9e605STiwei Bie 	bool avail, used;
15941ce9e605STiwei Bie 	u16 flags;
15951ce9e605STiwei Bie 
15961ce9e605STiwei Bie 	flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
15971ce9e605STiwei Bie 	avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
15981ce9e605STiwei Bie 	used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
15991ce9e605STiwei Bie 
16001ce9e605STiwei Bie 	return avail == used && used == used_wrap_counter;
16011ce9e605STiwei Bie }
16021ce9e605STiwei Bie 
16031ce9e605STiwei Bie static inline bool more_used_packed(const struct vring_virtqueue *vq)
16041ce9e605STiwei Bie {
1605a7722890Shuangjie.albert 	u16 last_used;
1606a7722890Shuangjie.albert 	u16 last_used_idx;
1607a7722890Shuangjie.albert 	bool used_wrap_counter;
1608a7722890Shuangjie.albert 
1609a7722890Shuangjie.albert 	last_used_idx = READ_ONCE(vq->last_used_idx);
1610a7722890Shuangjie.albert 	last_used = packed_last_used(last_used_idx);
1611a7722890Shuangjie.albert 	used_wrap_counter = packed_used_wrap_counter(last_used_idx);
1612a7722890Shuangjie.albert 	return is_used_desc_packed(vq, last_used, used_wrap_counter);
16131ce9e605STiwei Bie }
16141ce9e605STiwei Bie 
16151ce9e605STiwei Bie static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
16161ce9e605STiwei Bie 					  unsigned int *len,
16171ce9e605STiwei Bie 					  void **ctx)
16181ce9e605STiwei Bie {
16191ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1620a7722890Shuangjie.albert 	u16 last_used, id, last_used_idx;
1621a7722890Shuangjie.albert 	bool used_wrap_counter;
16221ce9e605STiwei Bie 	void *ret;
16231ce9e605STiwei Bie 
16241ce9e605STiwei Bie 	START_USE(vq);
16251ce9e605STiwei Bie 
16261ce9e605STiwei Bie 	if (unlikely(vq->broken)) {
16271ce9e605STiwei Bie 		END_USE(vq);
16281ce9e605STiwei Bie 		return NULL;
16291ce9e605STiwei Bie 	}
16301ce9e605STiwei Bie 
16311ce9e605STiwei Bie 	if (!more_used_packed(vq)) {
16321ce9e605STiwei Bie 		pr_debug("No more buffers in queue\n");
16331ce9e605STiwei Bie 		END_USE(vq);
16341ce9e605STiwei Bie 		return NULL;
16351ce9e605STiwei Bie 	}
16361ce9e605STiwei Bie 
16371ce9e605STiwei Bie 	/* Only get used elements after they have been exposed by host. */
16381ce9e605STiwei Bie 	virtio_rmb(vq->weak_barriers);
16391ce9e605STiwei Bie 
1640a7722890Shuangjie.albert 	last_used_idx = READ_ONCE(vq->last_used_idx);
1641a7722890Shuangjie.albert 	used_wrap_counter = packed_used_wrap_counter(last_used_idx);
1642a7722890Shuangjie.albert 	last_used = packed_last_used(last_used_idx);
16431ce9e605STiwei Bie 	id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
16441ce9e605STiwei Bie 	*len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
16451ce9e605STiwei Bie 
16461ce9e605STiwei Bie 	if (unlikely(id >= vq->packed.vring.num)) {
16471ce9e605STiwei Bie 		BAD_RING(vq, "id %u out of range\n", id);
16481ce9e605STiwei Bie 		return NULL;
16491ce9e605STiwei Bie 	}
16501ce9e605STiwei Bie 	if (unlikely(!vq->packed.desc_state[id].data)) {
16511ce9e605STiwei Bie 		BAD_RING(vq, "id %u is not a head!\n", id);
16521ce9e605STiwei Bie 		return NULL;
16531ce9e605STiwei Bie 	}
16541ce9e605STiwei Bie 
16551ce9e605STiwei Bie 	/* detach_buf_packed clears data, so grab it now. */
16561ce9e605STiwei Bie 	ret = vq->packed.desc_state[id].data;
16571ce9e605STiwei Bie 	detach_buf_packed(vq, id, ctx);
16581ce9e605STiwei Bie 
1659a7722890Shuangjie.albert 	last_used += vq->packed.desc_state[id].num;
1660a7722890Shuangjie.albert 	if (unlikely(last_used >= vq->packed.vring.num)) {
1661a7722890Shuangjie.albert 		last_used -= vq->packed.vring.num;
1662a7722890Shuangjie.albert 		used_wrap_counter ^= 1;
16631ce9e605STiwei Bie 	}
16641ce9e605STiwei Bie 
1665a7722890Shuangjie.albert 	last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1666a7722890Shuangjie.albert 	WRITE_ONCE(vq->last_used_idx, last_used);
1667a7722890Shuangjie.albert 
1668f51f9826STiwei Bie 	/*
1669f51f9826STiwei Bie 	 * If we expect an interrupt for the next entry, tell host
1670f51f9826STiwei Bie 	 * by writing event index and flush out the write before
1671f51f9826STiwei Bie 	 * the read in the next get_buf call.
1672f51f9826STiwei Bie 	 */
1673f51f9826STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1674f51f9826STiwei Bie 		virtio_store_mb(vq->weak_barriers,
1675f51f9826STiwei Bie 				&vq->packed.vring.driver->off_wrap,
1676a7722890Shuangjie.albert 				cpu_to_le16(vq->last_used_idx));
1677f51f9826STiwei Bie 
16781ce9e605STiwei Bie 	LAST_ADD_TIME_INVALID(vq);
16791ce9e605STiwei Bie 
16801ce9e605STiwei Bie 	END_USE(vq);
16811ce9e605STiwei Bie 	return ret;
16821ce9e605STiwei Bie }
16831ce9e605STiwei Bie 
16841ce9e605STiwei Bie static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
16851ce9e605STiwei Bie {
16861ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
16871ce9e605STiwei Bie 
16881ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
16891ce9e605STiwei Bie 		vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
16901ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
16911ce9e605STiwei Bie 			cpu_to_le16(vq->packed.event_flags_shadow);
16921ce9e605STiwei Bie 	}
16931ce9e605STiwei Bie }
16941ce9e605STiwei Bie 
169531532340SSolomon Tan static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
16961ce9e605STiwei Bie {
16971ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
16981ce9e605STiwei Bie 
16991ce9e605STiwei Bie 	START_USE(vq);
17001ce9e605STiwei Bie 
17011ce9e605STiwei Bie 	/*
17021ce9e605STiwei Bie 	 * We optimistically turn back on interrupts, then check if there was
17031ce9e605STiwei Bie 	 * more to do.
17041ce9e605STiwei Bie 	 */
17051ce9e605STiwei Bie 
1706f51f9826STiwei Bie 	if (vq->event) {
1707f51f9826STiwei Bie 		vq->packed.vring.driver->off_wrap =
1708a7722890Shuangjie.albert 			cpu_to_le16(vq->last_used_idx);
1709f51f9826STiwei Bie 		/*
1710f51f9826STiwei Bie 		 * We need to update event offset and event wrap
1711f51f9826STiwei Bie 		 * counter first before updating event flags.
1712f51f9826STiwei Bie 		 */
1713f51f9826STiwei Bie 		virtio_wmb(vq->weak_barriers);
1714f51f9826STiwei Bie 	}
1715f51f9826STiwei Bie 
17161ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1717f51f9826STiwei Bie 		vq->packed.event_flags_shadow = vq->event ?
1718f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_DESC :
1719f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_ENABLE;
17201ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
17211ce9e605STiwei Bie 				cpu_to_le16(vq->packed.event_flags_shadow);
17221ce9e605STiwei Bie 	}
17231ce9e605STiwei Bie 
17241ce9e605STiwei Bie 	END_USE(vq);
1725a7722890Shuangjie.albert 	return vq->last_used_idx;
17261ce9e605STiwei Bie }
17271ce9e605STiwei Bie 
17281ce9e605STiwei Bie static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
17291ce9e605STiwei Bie {
17301ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
17311ce9e605STiwei Bie 	bool wrap_counter;
17321ce9e605STiwei Bie 	u16 used_idx;
17331ce9e605STiwei Bie 
17341ce9e605STiwei Bie 	wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
17351ce9e605STiwei Bie 	used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
17361ce9e605STiwei Bie 
17371ce9e605STiwei Bie 	return is_used_desc_packed(vq, used_idx, wrap_counter);
17381ce9e605STiwei Bie }
17391ce9e605STiwei Bie 
17401ce9e605STiwei Bie static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
17411ce9e605STiwei Bie {
17421ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1743a7722890Shuangjie.albert 	u16 used_idx, wrap_counter, last_used_idx;
1744f51f9826STiwei Bie 	u16 bufs;
17451ce9e605STiwei Bie 
17461ce9e605STiwei Bie 	START_USE(vq);
17471ce9e605STiwei Bie 
17481ce9e605STiwei Bie 	/*
17491ce9e605STiwei Bie 	 * We optimistically turn back on interrupts, then check if there was
17501ce9e605STiwei Bie 	 * more to do.
17511ce9e605STiwei Bie 	 */
17521ce9e605STiwei Bie 
1753f51f9826STiwei Bie 	if (vq->event) {
1754f51f9826STiwei Bie 		/* TODO: tune this threshold */
1755f51f9826STiwei Bie 		bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
1756a7722890Shuangjie.albert 		last_used_idx = READ_ONCE(vq->last_used_idx);
1757a7722890Shuangjie.albert 		wrap_counter = packed_used_wrap_counter(last_used_idx);
17581ce9e605STiwei Bie 
1759a7722890Shuangjie.albert 		used_idx = packed_last_used(last_used_idx) + bufs;
1760f51f9826STiwei Bie 		if (used_idx >= vq->packed.vring.num) {
1761f51f9826STiwei Bie 			used_idx -= vq->packed.vring.num;
1762f51f9826STiwei Bie 			wrap_counter ^= 1;
1763f51f9826STiwei Bie 		}
1764f51f9826STiwei Bie 
1765f51f9826STiwei Bie 		vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1766f51f9826STiwei Bie 			(wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1767f51f9826STiwei Bie 
1768f51f9826STiwei Bie 		/*
1769f51f9826STiwei Bie 		 * We need to update event offset and event wrap
1770f51f9826STiwei Bie 		 * counter first before updating event flags.
1771f51f9826STiwei Bie 		 */
1772f51f9826STiwei Bie 		virtio_wmb(vq->weak_barriers);
1773f51f9826STiwei Bie 	}
1774f51f9826STiwei Bie 
17751ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1776f51f9826STiwei Bie 		vq->packed.event_flags_shadow = vq->event ?
1777f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_DESC :
1778f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_ENABLE;
17791ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
17801ce9e605STiwei Bie 				cpu_to_le16(vq->packed.event_flags_shadow);
17811ce9e605STiwei Bie 	}
17821ce9e605STiwei Bie 
17831ce9e605STiwei Bie 	/*
17841ce9e605STiwei Bie 	 * We need to update event suppression structure first
17851ce9e605STiwei Bie 	 * before re-checking for more used buffers.
17861ce9e605STiwei Bie 	 */
17871ce9e605STiwei Bie 	virtio_mb(vq->weak_barriers);
17881ce9e605STiwei Bie 
1789a7722890Shuangjie.albert 	last_used_idx = READ_ONCE(vq->last_used_idx);
1790a7722890Shuangjie.albert 	wrap_counter = packed_used_wrap_counter(last_used_idx);
1791a7722890Shuangjie.albert 	used_idx = packed_last_used(last_used_idx);
1792a7722890Shuangjie.albert 	if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
17931ce9e605STiwei Bie 		END_USE(vq);
17941ce9e605STiwei Bie 		return false;
17951ce9e605STiwei Bie 	}
17961ce9e605STiwei Bie 
17971ce9e605STiwei Bie 	END_USE(vq);
17981ce9e605STiwei Bie 	return true;
17991ce9e605STiwei Bie }
18001ce9e605STiwei Bie 
18011ce9e605STiwei Bie static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
18021ce9e605STiwei Bie {
18031ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
18041ce9e605STiwei Bie 	unsigned int i;
18051ce9e605STiwei Bie 	void *buf;
18061ce9e605STiwei Bie 
18071ce9e605STiwei Bie 	START_USE(vq);
18081ce9e605STiwei Bie 
18091ce9e605STiwei Bie 	for (i = 0; i < vq->packed.vring.num; i++) {
18101ce9e605STiwei Bie 		if (!vq->packed.desc_state[i].data)
18111ce9e605STiwei Bie 			continue;
18121ce9e605STiwei Bie 		/* detach_buf clears data, so grab it now. */
18131ce9e605STiwei Bie 		buf = vq->packed.desc_state[i].data;
18141ce9e605STiwei Bie 		detach_buf_packed(vq, i, NULL);
18151ce9e605STiwei Bie 		END_USE(vq);
18161ce9e605STiwei Bie 		return buf;
18171ce9e605STiwei Bie 	}
18181ce9e605STiwei Bie 	/* That should have freed everything. */
18191ce9e605STiwei Bie 	BUG_ON(vq->vq.num_free != vq->packed.vring.num);
18201ce9e605STiwei Bie 
18211ce9e605STiwei Bie 	END_USE(vq);
18221ce9e605STiwei Bie 	return NULL;
18231ce9e605STiwei Bie }
18241ce9e605STiwei Bie 
182596ef18a2SXuan Zhuo static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num)
18265a222421SJason Wang {
18275a222421SJason Wang 	struct vring_desc_extra *desc_extra;
18285a222421SJason Wang 	unsigned int i;
18295a222421SJason Wang 
18305a222421SJason Wang 	desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra),
18315a222421SJason Wang 				   GFP_KERNEL);
18325a222421SJason Wang 	if (!desc_extra)
18335a222421SJason Wang 		return NULL;
18345a222421SJason Wang 
18355a222421SJason Wang 	memset(desc_extra, 0, num * sizeof(struct vring_desc_extra));
18365a222421SJason Wang 
18375a222421SJason Wang 	for (i = 0; i < num - 1; i++)
18385a222421SJason Wang 		desc_extra[i].next = i + 1;
18395a222421SJason Wang 
18405a222421SJason Wang 	return desc_extra;
18415a222421SJason Wang }
18425a222421SJason Wang 
18436356f8bbSXuan Zhuo static void vring_free_packed(struct vring_virtqueue_packed *vring_packed,
18446356f8bbSXuan Zhuo 			      struct virtio_device *vdev)
18456356f8bbSXuan Zhuo {
18466356f8bbSXuan Zhuo 	if (vring_packed->vring.desc)
18476356f8bbSXuan Zhuo 		vring_free_queue(vdev, vring_packed->ring_size_in_bytes,
18486356f8bbSXuan Zhuo 				 vring_packed->vring.desc,
18496356f8bbSXuan Zhuo 				 vring_packed->ring_dma_addr);
18506356f8bbSXuan Zhuo 
18516356f8bbSXuan Zhuo 	if (vring_packed->vring.driver)
18526356f8bbSXuan Zhuo 		vring_free_queue(vdev, vring_packed->event_size_in_bytes,
18536356f8bbSXuan Zhuo 				 vring_packed->vring.driver,
18546356f8bbSXuan Zhuo 				 vring_packed->driver_event_dma_addr);
18556356f8bbSXuan Zhuo 
18566356f8bbSXuan Zhuo 	if (vring_packed->vring.device)
18576356f8bbSXuan Zhuo 		vring_free_queue(vdev, vring_packed->event_size_in_bytes,
18586356f8bbSXuan Zhuo 				 vring_packed->vring.device,
18596356f8bbSXuan Zhuo 				 vring_packed->device_event_dma_addr);
18606356f8bbSXuan Zhuo 
18616356f8bbSXuan Zhuo 	kfree(vring_packed->desc_state);
18626356f8bbSXuan Zhuo 	kfree(vring_packed->desc_extra);
18636356f8bbSXuan Zhuo }
18646356f8bbSXuan Zhuo 
18656b60b9c0SXuan Zhuo static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed,
18666b60b9c0SXuan Zhuo 				    struct virtio_device *vdev,
18676b60b9c0SXuan Zhuo 				    u32 num)
18686b60b9c0SXuan Zhuo {
18696b60b9c0SXuan Zhuo 	struct vring_packed_desc *ring;
18706b60b9c0SXuan Zhuo 	struct vring_packed_desc_event *driver, *device;
18716b60b9c0SXuan Zhuo 	dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
18726b60b9c0SXuan Zhuo 	size_t ring_size_in_bytes, event_size_in_bytes;
18736b60b9c0SXuan Zhuo 
18746b60b9c0SXuan Zhuo 	ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
18756b60b9c0SXuan Zhuo 
18766b60b9c0SXuan Zhuo 	ring = vring_alloc_queue(vdev, ring_size_in_bytes,
18776b60b9c0SXuan Zhuo 				 &ring_dma_addr,
18786b60b9c0SXuan Zhuo 				 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
18796b60b9c0SXuan Zhuo 	if (!ring)
18806b60b9c0SXuan Zhuo 		goto err;
18816b60b9c0SXuan Zhuo 
18826b60b9c0SXuan Zhuo 	vring_packed->vring.desc         = ring;
18836b60b9c0SXuan Zhuo 	vring_packed->ring_dma_addr      = ring_dma_addr;
18846b60b9c0SXuan Zhuo 	vring_packed->ring_size_in_bytes = ring_size_in_bytes;
18856b60b9c0SXuan Zhuo 
18866b60b9c0SXuan Zhuo 	event_size_in_bytes = sizeof(struct vring_packed_desc_event);
18876b60b9c0SXuan Zhuo 
18886b60b9c0SXuan Zhuo 	driver = vring_alloc_queue(vdev, event_size_in_bytes,
18896b60b9c0SXuan Zhuo 				   &driver_event_dma_addr,
18906b60b9c0SXuan Zhuo 				   GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
18916b60b9c0SXuan Zhuo 	if (!driver)
18926b60b9c0SXuan Zhuo 		goto err;
18936b60b9c0SXuan Zhuo 
18946b60b9c0SXuan Zhuo 	vring_packed->vring.driver          = driver;
18956b60b9c0SXuan Zhuo 	vring_packed->event_size_in_bytes   = event_size_in_bytes;
18966b60b9c0SXuan Zhuo 	vring_packed->driver_event_dma_addr = driver_event_dma_addr;
18976b60b9c0SXuan Zhuo 
18986b60b9c0SXuan Zhuo 	device = vring_alloc_queue(vdev, event_size_in_bytes,
18996b60b9c0SXuan Zhuo 				   &device_event_dma_addr,
19006b60b9c0SXuan Zhuo 				   GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
19016b60b9c0SXuan Zhuo 	if (!device)
19026b60b9c0SXuan Zhuo 		goto err;
19036b60b9c0SXuan Zhuo 
19046b60b9c0SXuan Zhuo 	vring_packed->vring.device          = device;
19056b60b9c0SXuan Zhuo 	vring_packed->device_event_dma_addr = device_event_dma_addr;
19066b60b9c0SXuan Zhuo 
19076b60b9c0SXuan Zhuo 	vring_packed->vring.num = num;
19086b60b9c0SXuan Zhuo 
19096b60b9c0SXuan Zhuo 	return 0;
19106b60b9c0SXuan Zhuo 
19116b60b9c0SXuan Zhuo err:
19126b60b9c0SXuan Zhuo 	vring_free_packed(vring_packed, vdev);
19136b60b9c0SXuan Zhuo 	return -ENOMEM;
19146b60b9c0SXuan Zhuo }
19156b60b9c0SXuan Zhuo 
1916ef3167cfSXuan Zhuo static int vring_alloc_state_extra_packed(struct vring_virtqueue_packed *vring_packed)
1917ef3167cfSXuan Zhuo {
1918ef3167cfSXuan Zhuo 	struct vring_desc_state_packed *state;
1919ef3167cfSXuan Zhuo 	struct vring_desc_extra *extra;
1920ef3167cfSXuan Zhuo 	u32 num = vring_packed->vring.num;
1921ef3167cfSXuan Zhuo 
1922ef3167cfSXuan Zhuo 	state = kmalloc_array(num, sizeof(struct vring_desc_state_packed), GFP_KERNEL);
1923ef3167cfSXuan Zhuo 	if (!state)
1924ef3167cfSXuan Zhuo 		goto err_desc_state;
1925ef3167cfSXuan Zhuo 
1926ef3167cfSXuan Zhuo 	memset(state, 0, num * sizeof(struct vring_desc_state_packed));
1927ef3167cfSXuan Zhuo 
1928ef3167cfSXuan Zhuo 	extra = vring_alloc_desc_extra(num);
1929ef3167cfSXuan Zhuo 	if (!extra)
1930ef3167cfSXuan Zhuo 		goto err_desc_extra;
1931ef3167cfSXuan Zhuo 
1932ef3167cfSXuan Zhuo 	vring_packed->desc_state = state;
1933ef3167cfSXuan Zhuo 	vring_packed->desc_extra = extra;
1934ef3167cfSXuan Zhuo 
1935ef3167cfSXuan Zhuo 	return 0;
1936ef3167cfSXuan Zhuo 
1937ef3167cfSXuan Zhuo err_desc_extra:
1938ef3167cfSXuan Zhuo 	kfree(state);
1939ef3167cfSXuan Zhuo err_desc_state:
1940ef3167cfSXuan Zhuo 	return -ENOMEM;
1941ef3167cfSXuan Zhuo }
1942ef3167cfSXuan Zhuo 
19431a107c87SXuan Zhuo static void virtqueue_vring_init_packed(struct vring_virtqueue_packed *vring_packed,
19441a107c87SXuan Zhuo 					bool callback)
19451a107c87SXuan Zhuo {
19461a107c87SXuan Zhuo 	vring_packed->next_avail_idx = 0;
19471a107c87SXuan Zhuo 	vring_packed->avail_wrap_counter = 1;
19481a107c87SXuan Zhuo 	vring_packed->event_flags_shadow = 0;
19491a107c87SXuan Zhuo 	vring_packed->avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
19501a107c87SXuan Zhuo 
19511a107c87SXuan Zhuo 	/* No callback?  Tell other side not to bother us. */
19521a107c87SXuan Zhuo 	if (!callback) {
19531a107c87SXuan Zhuo 		vring_packed->event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
19541a107c87SXuan Zhuo 		vring_packed->vring.driver->flags =
19551a107c87SXuan Zhuo 			cpu_to_le16(vring_packed->event_flags_shadow);
19561a107c87SXuan Zhuo 	}
19571a107c87SXuan Zhuo }
19581a107c87SXuan Zhuo 
195951d649f1SXuan Zhuo static void virtqueue_vring_attach_packed(struct vring_virtqueue *vq,
196051d649f1SXuan Zhuo 					  struct vring_virtqueue_packed *vring_packed)
196151d649f1SXuan Zhuo {
196251d649f1SXuan Zhuo 	vq->packed = *vring_packed;
196351d649f1SXuan Zhuo 
196451d649f1SXuan Zhuo 	/* Put everything in free lists. */
196551d649f1SXuan Zhuo 	vq->free_head = 0;
196651d649f1SXuan Zhuo }
196751d649f1SXuan Zhuo 
196856775e14SXuan Zhuo static void virtqueue_reinit_packed(struct vring_virtqueue *vq)
196956775e14SXuan Zhuo {
197056775e14SXuan Zhuo 	memset(vq->packed.vring.device, 0, vq->packed.event_size_in_bytes);
197156775e14SXuan Zhuo 	memset(vq->packed.vring.driver, 0, vq->packed.event_size_in_bytes);
197256775e14SXuan Zhuo 
197356775e14SXuan Zhuo 	/* we need to reset the desc.flags. For more, see is_used_desc_packed() */
197456775e14SXuan Zhuo 	memset(vq->packed.vring.desc, 0, vq->packed.ring_size_in_bytes);
197556775e14SXuan Zhuo 
197656775e14SXuan Zhuo 	virtqueue_init(vq, vq->packed.vring.num);
197756775e14SXuan Zhuo 	virtqueue_vring_init_packed(&vq->packed, !!vq->vq.callback);
197856775e14SXuan Zhuo }
197956775e14SXuan Zhuo 
19801ce9e605STiwei Bie static struct virtqueue *vring_create_virtqueue_packed(
19811ce9e605STiwei Bie 	unsigned int index,
19821ce9e605STiwei Bie 	unsigned int num,
19831ce9e605STiwei Bie 	unsigned int vring_align,
19841ce9e605STiwei Bie 	struct virtio_device *vdev,
19851ce9e605STiwei Bie 	bool weak_barriers,
19861ce9e605STiwei Bie 	bool may_reduce_num,
19871ce9e605STiwei Bie 	bool context,
19881ce9e605STiwei Bie 	bool (*notify)(struct virtqueue *),
19891ce9e605STiwei Bie 	void (*callback)(struct virtqueue *),
19901ce9e605STiwei Bie 	const char *name)
19911ce9e605STiwei Bie {
19926b60b9c0SXuan Zhuo 	struct vring_virtqueue_packed vring_packed = {};
19931ce9e605STiwei Bie 	struct vring_virtqueue *vq;
1994ef3167cfSXuan Zhuo 	int err;
19951ce9e605STiwei Bie 
19966b60b9c0SXuan Zhuo 	if (vring_alloc_queue_packed(&vring_packed, vdev, num))
19971ce9e605STiwei Bie 		goto err_ring;
19981ce9e605STiwei Bie 
19991ce9e605STiwei Bie 	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
20001ce9e605STiwei Bie 	if (!vq)
20011ce9e605STiwei Bie 		goto err_vq;
20021ce9e605STiwei Bie 
20031ce9e605STiwei Bie 	vq->vq.callback = callback;
20041ce9e605STiwei Bie 	vq->vq.vdev = vdev;
20051ce9e605STiwei Bie 	vq->vq.name = name;
20061ce9e605STiwei Bie 	vq->vq.index = index;
20074913e854SXuan Zhuo 	vq->vq.reset = false;
20081ce9e605STiwei Bie 	vq->we_own_ring = true;
20091ce9e605STiwei Bie 	vq->notify = notify;
20101ce9e605STiwei Bie 	vq->weak_barriers = weak_barriers;
2011c346dae4SJason Wang #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
20128b4ec69dSJason Wang 	vq->broken = true;
2013c346dae4SJason Wang #else
2014c346dae4SJason Wang 	vq->broken = false;
2015c346dae4SJason Wang #endif
20161ce9e605STiwei Bie 	vq->packed_ring = true;
20171ce9e605STiwei Bie 	vq->use_dma_api = vring_use_dma_api(vdev);
20181ce9e605STiwei Bie 
20191ce9e605STiwei Bie 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
20201ce9e605STiwei Bie 		!context;
20211ce9e605STiwei Bie 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
20221ce9e605STiwei Bie 
202345383fb0STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
202445383fb0STiwei Bie 		vq->weak_barriers = false;
202545383fb0STiwei Bie 
2026ef3167cfSXuan Zhuo 	err = vring_alloc_state_extra_packed(&vring_packed);
2027ef3167cfSXuan Zhuo 	if (err)
2028ef3167cfSXuan Zhuo 		goto err_state_extra;
20291ce9e605STiwei Bie 
20301a107c87SXuan Zhuo 	virtqueue_vring_init_packed(&vring_packed, !!callback);
20311ce9e605STiwei Bie 
20323a897128SXuan Zhuo 	virtqueue_init(vq, num);
203351d649f1SXuan Zhuo 	virtqueue_vring_attach_packed(vq, &vring_packed);
20343a897128SXuan Zhuo 
20350e566c8fSParav Pandit 	spin_lock(&vdev->vqs_list_lock);
2036e152d8afSDan Carpenter 	list_add_tail(&vq->vq.list, &vdev->vqs);
20370e566c8fSParav Pandit 	spin_unlock(&vdev->vqs_list_lock);
20381ce9e605STiwei Bie 	return &vq->vq;
20391ce9e605STiwei Bie 
2040ef3167cfSXuan Zhuo err_state_extra:
20411ce9e605STiwei Bie 	kfree(vq);
20421ce9e605STiwei Bie err_vq:
20436b60b9c0SXuan Zhuo 	vring_free_packed(&vring_packed, vdev);
20441ce9e605STiwei Bie err_ring:
20451ce9e605STiwei Bie 	return NULL;
20461ce9e605STiwei Bie }
20471ce9e605STiwei Bie 
2048947f9fcfSXuan Zhuo static int virtqueue_resize_packed(struct virtqueue *_vq, u32 num)
2049947f9fcfSXuan Zhuo {
2050947f9fcfSXuan Zhuo 	struct vring_virtqueue_packed vring_packed = {};
2051947f9fcfSXuan Zhuo 	struct vring_virtqueue *vq = to_vvq(_vq);
2052947f9fcfSXuan Zhuo 	struct virtio_device *vdev = _vq->vdev;
2053947f9fcfSXuan Zhuo 	int err;
2054947f9fcfSXuan Zhuo 
2055947f9fcfSXuan Zhuo 	if (vring_alloc_queue_packed(&vring_packed, vdev, num))
2056947f9fcfSXuan Zhuo 		goto err_ring;
2057947f9fcfSXuan Zhuo 
2058947f9fcfSXuan Zhuo 	err = vring_alloc_state_extra_packed(&vring_packed);
2059947f9fcfSXuan Zhuo 	if (err)
2060947f9fcfSXuan Zhuo 		goto err_state_extra;
2061947f9fcfSXuan Zhuo 
2062947f9fcfSXuan Zhuo 	vring_free(&vq->vq);
2063947f9fcfSXuan Zhuo 
2064947f9fcfSXuan Zhuo 	virtqueue_vring_init_packed(&vring_packed, !!vq->vq.callback);
2065947f9fcfSXuan Zhuo 
2066947f9fcfSXuan Zhuo 	virtqueue_init(vq, vring_packed.vring.num);
2067947f9fcfSXuan Zhuo 	virtqueue_vring_attach_packed(vq, &vring_packed);
2068947f9fcfSXuan Zhuo 
2069947f9fcfSXuan Zhuo 	return 0;
2070947f9fcfSXuan Zhuo 
2071947f9fcfSXuan Zhuo err_state_extra:
2072947f9fcfSXuan Zhuo 	vring_free_packed(&vring_packed, vdev);
2073947f9fcfSXuan Zhuo err_ring:
2074947f9fcfSXuan Zhuo 	virtqueue_reinit_packed(vq);
2075947f9fcfSXuan Zhuo 	return -ENOMEM;
2076947f9fcfSXuan Zhuo }
2077947f9fcfSXuan Zhuo 
20781ce9e605STiwei Bie 
20791ce9e605STiwei Bie /*
2080e6f633e5STiwei Bie  * Generic functions and exported symbols.
2081e6f633e5STiwei Bie  */
2082e6f633e5STiwei Bie 
2083e6f633e5STiwei Bie static inline int virtqueue_add(struct virtqueue *_vq,
2084e6f633e5STiwei Bie 				struct scatterlist *sgs[],
2085e6f633e5STiwei Bie 				unsigned int total_sg,
2086e6f633e5STiwei Bie 				unsigned int out_sgs,
2087e6f633e5STiwei Bie 				unsigned int in_sgs,
2088e6f633e5STiwei Bie 				void *data,
2089e6f633e5STiwei Bie 				void *ctx,
2090e6f633e5STiwei Bie 				gfp_t gfp)
2091e6f633e5STiwei Bie {
20921ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
20931ce9e605STiwei Bie 
20941ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
20951ce9e605STiwei Bie 					out_sgs, in_sgs, data, ctx, gfp) :
20961ce9e605STiwei Bie 				 virtqueue_add_split(_vq, sgs, total_sg,
2097e6f633e5STiwei Bie 					out_sgs, in_sgs, data, ctx, gfp);
2098e6f633e5STiwei Bie }
2099e6f633e5STiwei Bie 
2100e6f633e5STiwei Bie /**
2101e6f633e5STiwei Bie  * virtqueue_add_sgs - expose buffers to other end
2102a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2103e6f633e5STiwei Bie  * @sgs: array of terminated scatterlists.
2104a5581206SJiang Biao  * @out_sgs: the number of scatterlists readable by other side
2105a5581206SJiang Biao  * @in_sgs: the number of scatterlists which are writable (after readable ones)
2106e6f633e5STiwei Bie  * @data: the token identifying the buffer.
2107e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
2108e6f633e5STiwei Bie  *
2109e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
2110e6f633e5STiwei Bie  * at the same time (except where noted).
2111e6f633e5STiwei Bie  *
2112e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2113e6f633e5STiwei Bie  */
2114e6f633e5STiwei Bie int virtqueue_add_sgs(struct virtqueue *_vq,
2115e6f633e5STiwei Bie 		      struct scatterlist *sgs[],
2116e6f633e5STiwei Bie 		      unsigned int out_sgs,
2117e6f633e5STiwei Bie 		      unsigned int in_sgs,
2118e6f633e5STiwei Bie 		      void *data,
2119e6f633e5STiwei Bie 		      gfp_t gfp)
2120e6f633e5STiwei Bie {
2121e6f633e5STiwei Bie 	unsigned int i, total_sg = 0;
2122e6f633e5STiwei Bie 
2123e6f633e5STiwei Bie 	/* Count them first. */
2124e6f633e5STiwei Bie 	for (i = 0; i < out_sgs + in_sgs; i++) {
2125e6f633e5STiwei Bie 		struct scatterlist *sg;
2126e6f633e5STiwei Bie 
2127e6f633e5STiwei Bie 		for (sg = sgs[i]; sg; sg = sg_next(sg))
2128e6f633e5STiwei Bie 			total_sg++;
2129e6f633e5STiwei Bie 	}
2130e6f633e5STiwei Bie 	return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
2131e6f633e5STiwei Bie 			     data, NULL, gfp);
2132e6f633e5STiwei Bie }
2133e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
2134e6f633e5STiwei Bie 
2135e6f633e5STiwei Bie /**
2136e6f633e5STiwei Bie  * virtqueue_add_outbuf - expose output buffers to other end
2137e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
2138e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
2139e6f633e5STiwei Bie  * @num: the number of entries in @sg readable by other side
2140e6f633e5STiwei Bie  * @data: the token identifying the buffer.
2141e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
2142e6f633e5STiwei Bie  *
2143e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
2144e6f633e5STiwei Bie  * at the same time (except where noted).
2145e6f633e5STiwei Bie  *
2146e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2147e6f633e5STiwei Bie  */
2148e6f633e5STiwei Bie int virtqueue_add_outbuf(struct virtqueue *vq,
2149e6f633e5STiwei Bie 			 struct scatterlist *sg, unsigned int num,
2150e6f633e5STiwei Bie 			 void *data,
2151e6f633e5STiwei Bie 			 gfp_t gfp)
2152e6f633e5STiwei Bie {
2153e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
2154e6f633e5STiwei Bie }
2155e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
2156e6f633e5STiwei Bie 
2157e6f633e5STiwei Bie /**
2158e6f633e5STiwei Bie  * virtqueue_add_inbuf - expose input buffers to other end
2159e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
2160e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
2161e6f633e5STiwei Bie  * @num: the number of entries in @sg writable by other side
2162e6f633e5STiwei Bie  * @data: the token identifying the buffer.
2163e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
2164e6f633e5STiwei Bie  *
2165e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
2166e6f633e5STiwei Bie  * at the same time (except where noted).
2167e6f633e5STiwei Bie  *
2168e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2169e6f633e5STiwei Bie  */
2170e6f633e5STiwei Bie int virtqueue_add_inbuf(struct virtqueue *vq,
2171e6f633e5STiwei Bie 			struct scatterlist *sg, unsigned int num,
2172e6f633e5STiwei Bie 			void *data,
2173e6f633e5STiwei Bie 			gfp_t gfp)
2174e6f633e5STiwei Bie {
2175e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
2176e6f633e5STiwei Bie }
2177e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
2178e6f633e5STiwei Bie 
2179e6f633e5STiwei Bie /**
2180e6f633e5STiwei Bie  * virtqueue_add_inbuf_ctx - expose input buffers to other end
2181e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
2182e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
2183e6f633e5STiwei Bie  * @num: the number of entries in @sg writable by other side
2184e6f633e5STiwei Bie  * @data: the token identifying the buffer.
2185e6f633e5STiwei Bie  * @ctx: extra context for the token
2186e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
2187e6f633e5STiwei Bie  *
2188e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
2189e6f633e5STiwei Bie  * at the same time (except where noted).
2190e6f633e5STiwei Bie  *
2191e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2192e6f633e5STiwei Bie  */
2193e6f633e5STiwei Bie int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
2194e6f633e5STiwei Bie 			struct scatterlist *sg, unsigned int num,
2195e6f633e5STiwei Bie 			void *data,
2196e6f633e5STiwei Bie 			void *ctx,
2197e6f633e5STiwei Bie 			gfp_t gfp)
2198e6f633e5STiwei Bie {
2199e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
2200e6f633e5STiwei Bie }
2201e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
2202e6f633e5STiwei Bie 
2203e6f633e5STiwei Bie /**
2204e6f633e5STiwei Bie  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
2205a5581206SJiang Biao  * @_vq: the struct virtqueue
2206e6f633e5STiwei Bie  *
2207e6f633e5STiwei Bie  * Instead of virtqueue_kick(), you can do:
2208e6f633e5STiwei Bie  *	if (virtqueue_kick_prepare(vq))
2209e6f633e5STiwei Bie  *		virtqueue_notify(vq);
2210e6f633e5STiwei Bie  *
2211e6f633e5STiwei Bie  * This is sometimes useful because the virtqueue_kick_prepare() needs
2212e6f633e5STiwei Bie  * to be serialized, but the actual virtqueue_notify() call does not.
2213e6f633e5STiwei Bie  */
2214e6f633e5STiwei Bie bool virtqueue_kick_prepare(struct virtqueue *_vq)
2215e6f633e5STiwei Bie {
22161ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
22171ce9e605STiwei Bie 
22181ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
22191ce9e605STiwei Bie 				 virtqueue_kick_prepare_split(_vq);
2220e6f633e5STiwei Bie }
2221e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
2222e6f633e5STiwei Bie 
2223e6f633e5STiwei Bie /**
2224e6f633e5STiwei Bie  * virtqueue_notify - second half of split virtqueue_kick call.
2225a5581206SJiang Biao  * @_vq: the struct virtqueue
2226e6f633e5STiwei Bie  *
2227e6f633e5STiwei Bie  * This does not need to be serialized.
2228e6f633e5STiwei Bie  *
2229e6f633e5STiwei Bie  * Returns false if host notify failed or queue is broken, otherwise true.
2230e6f633e5STiwei Bie  */
2231e6f633e5STiwei Bie bool virtqueue_notify(struct virtqueue *_vq)
2232e6f633e5STiwei Bie {
2233e6f633e5STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
2234e6f633e5STiwei Bie 
2235e6f633e5STiwei Bie 	if (unlikely(vq->broken))
2236e6f633e5STiwei Bie 		return false;
2237e6f633e5STiwei Bie 
2238e6f633e5STiwei Bie 	/* Prod other side to tell it about changes. */
2239e6f633e5STiwei Bie 	if (!vq->notify(_vq)) {
2240e6f633e5STiwei Bie 		vq->broken = true;
2241e6f633e5STiwei Bie 		return false;
2242e6f633e5STiwei Bie 	}
2243e6f633e5STiwei Bie 	return true;
2244e6f633e5STiwei Bie }
2245e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_notify);
2246e6f633e5STiwei Bie 
2247e6f633e5STiwei Bie /**
2248e6f633e5STiwei Bie  * virtqueue_kick - update after add_buf
2249e6f633e5STiwei Bie  * @vq: the struct virtqueue
2250e6f633e5STiwei Bie  *
2251e6f633e5STiwei Bie  * After one or more virtqueue_add_* calls, invoke this to kick
2252e6f633e5STiwei Bie  * the other side.
2253e6f633e5STiwei Bie  *
2254e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
2255e6f633e5STiwei Bie  * operations at the same time (except where noted).
2256e6f633e5STiwei Bie  *
2257e6f633e5STiwei Bie  * Returns false if kick failed, otherwise true.
2258e6f633e5STiwei Bie  */
2259e6f633e5STiwei Bie bool virtqueue_kick(struct virtqueue *vq)
2260e6f633e5STiwei Bie {
2261e6f633e5STiwei Bie 	if (virtqueue_kick_prepare(vq))
2262e6f633e5STiwei Bie 		return virtqueue_notify(vq);
2263e6f633e5STiwei Bie 	return true;
2264e6f633e5STiwei Bie }
2265e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick);
2266e6f633e5STiwei Bie 
2267e6f633e5STiwei Bie /**
226831c11db6SYang Li  * virtqueue_get_buf_ctx - get the next used buffer
2269a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2270e6f633e5STiwei Bie  * @len: the length written into the buffer
2271a5581206SJiang Biao  * @ctx: extra context for the token
2272e6f633e5STiwei Bie  *
2273e6f633e5STiwei Bie  * If the device wrote data into the buffer, @len will be set to the
2274e6f633e5STiwei Bie  * amount written.  This means you don't need to clear the buffer
2275e6f633e5STiwei Bie  * beforehand to ensure there's no data leakage in the case of short
2276e6f633e5STiwei Bie  * writes.
2277e6f633e5STiwei Bie  *
2278e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
2279e6f633e5STiwei Bie  * operations at the same time (except where noted).
2280e6f633e5STiwei Bie  *
2281e6f633e5STiwei Bie  * Returns NULL if there are no used buffers, or the "data" token
2282e6f633e5STiwei Bie  * handed to virtqueue_add_*().
2283e6f633e5STiwei Bie  */
2284e6f633e5STiwei Bie void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
2285e6f633e5STiwei Bie 			    void **ctx)
2286e6f633e5STiwei Bie {
22871ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
22881ce9e605STiwei Bie 
22891ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
22901ce9e605STiwei Bie 				 virtqueue_get_buf_ctx_split(_vq, len, ctx);
2291e6f633e5STiwei Bie }
2292e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
2293e6f633e5STiwei Bie 
2294e6f633e5STiwei Bie void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
2295e6f633e5STiwei Bie {
2296e6f633e5STiwei Bie 	return virtqueue_get_buf_ctx(_vq, len, NULL);
2297e6f633e5STiwei Bie }
2298e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf);
2299e6f633e5STiwei Bie /**
2300e6f633e5STiwei Bie  * virtqueue_disable_cb - disable callbacks
2301a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2302e6f633e5STiwei Bie  *
2303e6f633e5STiwei Bie  * Note that this is not necessarily synchronous, hence unreliable and only
2304e6f633e5STiwei Bie  * useful as an optimization.
2305e6f633e5STiwei Bie  *
2306e6f633e5STiwei Bie  * Unlike other operations, this need not be serialized.
2307e6f633e5STiwei Bie  */
2308e6f633e5STiwei Bie void virtqueue_disable_cb(struct virtqueue *_vq)
2309e6f633e5STiwei Bie {
23101ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
23111ce9e605STiwei Bie 
23128d622d21SMichael S. Tsirkin 	/* If device triggered an event already it won't trigger one again:
23138d622d21SMichael S. Tsirkin 	 * no need to disable.
23148d622d21SMichael S. Tsirkin 	 */
23158d622d21SMichael S. Tsirkin 	if (vq->event_triggered)
23168d622d21SMichael S. Tsirkin 		return;
23178d622d21SMichael S. Tsirkin 
23181ce9e605STiwei Bie 	if (vq->packed_ring)
23191ce9e605STiwei Bie 		virtqueue_disable_cb_packed(_vq);
23201ce9e605STiwei Bie 	else
2321e6f633e5STiwei Bie 		virtqueue_disable_cb_split(_vq);
2322e6f633e5STiwei Bie }
2323e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
2324e6f633e5STiwei Bie 
2325e6f633e5STiwei Bie /**
2326e6f633e5STiwei Bie  * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
2327a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2328e6f633e5STiwei Bie  *
2329e6f633e5STiwei Bie  * This re-enables callbacks; it returns current queue state
2330e6f633e5STiwei Bie  * in an opaque unsigned value. This value should be later tested by
2331e6f633e5STiwei Bie  * virtqueue_poll, to detect a possible race between the driver checking for
2332e6f633e5STiwei Bie  * more work, and enabling callbacks.
2333e6f633e5STiwei Bie  *
2334e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
2335e6f633e5STiwei Bie  * operations at the same time (except where noted).
2336e6f633e5STiwei Bie  */
233731532340SSolomon Tan unsigned int virtqueue_enable_cb_prepare(struct virtqueue *_vq)
2338e6f633e5STiwei Bie {
23391ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
23401ce9e605STiwei Bie 
23418d622d21SMichael S. Tsirkin 	if (vq->event_triggered)
23428d622d21SMichael S. Tsirkin 		vq->event_triggered = false;
23438d622d21SMichael S. Tsirkin 
23441ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
23451ce9e605STiwei Bie 				 virtqueue_enable_cb_prepare_split(_vq);
2346e6f633e5STiwei Bie }
2347e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
2348e6f633e5STiwei Bie 
2349e6f633e5STiwei Bie /**
2350e6f633e5STiwei Bie  * virtqueue_poll - query pending used buffers
2351a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2352e6f633e5STiwei Bie  * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
2353e6f633e5STiwei Bie  *
2354e6f633e5STiwei Bie  * Returns "true" if there are pending used buffers in the queue.
2355e6f633e5STiwei Bie  *
2356e6f633e5STiwei Bie  * This does not need to be serialized.
2357e6f633e5STiwei Bie  */
235831532340SSolomon Tan bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx)
2359e6f633e5STiwei Bie {
2360e6f633e5STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
2361e6f633e5STiwei Bie 
2362481a0d74SMao Wenan 	if (unlikely(vq->broken))
2363481a0d74SMao Wenan 		return false;
2364481a0d74SMao Wenan 
2365e6f633e5STiwei Bie 	virtio_mb(vq->weak_barriers);
23661ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
23671ce9e605STiwei Bie 				 virtqueue_poll_split(_vq, last_used_idx);
2368e6f633e5STiwei Bie }
2369e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_poll);
2370e6f633e5STiwei Bie 
2371e6f633e5STiwei Bie /**
2372e6f633e5STiwei Bie  * virtqueue_enable_cb - restart callbacks after disable_cb.
2373a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2374e6f633e5STiwei Bie  *
2375e6f633e5STiwei Bie  * This re-enables callbacks; it returns "false" if there are pending
2376e6f633e5STiwei Bie  * buffers in the queue, to detect a possible race between the driver
2377e6f633e5STiwei Bie  * checking for more work, and enabling callbacks.
2378e6f633e5STiwei Bie  *
2379e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
2380e6f633e5STiwei Bie  * operations at the same time (except where noted).
2381e6f633e5STiwei Bie  */
2382e6f633e5STiwei Bie bool virtqueue_enable_cb(struct virtqueue *_vq)
2383e6f633e5STiwei Bie {
238431532340SSolomon Tan 	unsigned int last_used_idx = virtqueue_enable_cb_prepare(_vq);
2385e6f633e5STiwei Bie 
2386e6f633e5STiwei Bie 	return !virtqueue_poll(_vq, last_used_idx);
2387e6f633e5STiwei Bie }
2388e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
2389e6f633e5STiwei Bie 
2390e6f633e5STiwei Bie /**
2391e6f633e5STiwei Bie  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
2392a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2393e6f633e5STiwei Bie  *
2394e6f633e5STiwei Bie  * This re-enables callbacks but hints to the other side to delay
2395e6f633e5STiwei Bie  * interrupts until most of the available buffers have been processed;
2396e6f633e5STiwei Bie  * it returns "false" if there are many pending buffers in the queue,
2397e6f633e5STiwei Bie  * to detect a possible race between the driver checking for more work,
2398e6f633e5STiwei Bie  * and enabling callbacks.
2399e6f633e5STiwei Bie  *
2400e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
2401e6f633e5STiwei Bie  * operations at the same time (except where noted).
2402e6f633e5STiwei Bie  */
2403e6f633e5STiwei Bie bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
2404e6f633e5STiwei Bie {
24051ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
24061ce9e605STiwei Bie 
24078d622d21SMichael S. Tsirkin 	if (vq->event_triggered)
24088d622d21SMichael S. Tsirkin 		vq->event_triggered = false;
24098d622d21SMichael S. Tsirkin 
24101ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
24111ce9e605STiwei Bie 				 virtqueue_enable_cb_delayed_split(_vq);
2412e6f633e5STiwei Bie }
2413e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2414e6f633e5STiwei Bie 
2415138fd251STiwei Bie /**
2416138fd251STiwei Bie  * virtqueue_detach_unused_buf - detach first unused buffer
2417a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2418138fd251STiwei Bie  *
2419138fd251STiwei Bie  * Returns NULL or the "data" token handed to virtqueue_add_*().
2420a62eecb3SXuan Zhuo  * This is not valid on an active queue; it is useful for device
2421a62eecb3SXuan Zhuo  * shutdown or the reset queue.
2422138fd251STiwei Bie  */
2423138fd251STiwei Bie void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2424138fd251STiwei Bie {
24251ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
24261ce9e605STiwei Bie 
24271ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
24281ce9e605STiwei Bie 				 virtqueue_detach_unused_buf_split(_vq);
2429138fd251STiwei Bie }
24307c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
2431c021eac4SShirley Ma 
2432138fd251STiwei Bie static inline bool more_used(const struct vring_virtqueue *vq)
2433138fd251STiwei Bie {
24341ce9e605STiwei Bie 	return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
2435138fd251STiwei Bie }
2436138fd251STiwei Bie 
24375c669c4aSRicardo Cañuelo /**
24385c669c4aSRicardo Cañuelo  * vring_interrupt - notify a virtqueue on an interrupt
24395c669c4aSRicardo Cañuelo  * @irq: the IRQ number (ignored)
24405c669c4aSRicardo Cañuelo  * @_vq: the struct virtqueue to notify
24415c669c4aSRicardo Cañuelo  *
24425c669c4aSRicardo Cañuelo  * Calls the callback function of @_vq to process the virtqueue
24435c669c4aSRicardo Cañuelo  * notification.
24445c669c4aSRicardo Cañuelo  */
24450a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
24460a8a69ddSRusty Russell {
24470a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
24480a8a69ddSRusty Russell 
24490a8a69ddSRusty Russell 	if (!more_used(vq)) {
24500a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
24510a8a69ddSRusty Russell 		return IRQ_NONE;
24520a8a69ddSRusty Russell 	}
24530a8a69ddSRusty Russell 
24548b4ec69dSJason Wang 	if (unlikely(vq->broken)) {
2455c346dae4SJason Wang #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
24568b4ec69dSJason Wang 		dev_warn_once(&vq->vq.vdev->dev,
24578b4ec69dSJason Wang 			      "virtio vring IRQ raised before DRIVER_OK");
24588b4ec69dSJason Wang 		return IRQ_NONE;
2459c346dae4SJason Wang #else
2460c346dae4SJason Wang 		return IRQ_HANDLED;
2461c346dae4SJason Wang #endif
24628b4ec69dSJason Wang 	}
24630a8a69ddSRusty Russell 
24648d622d21SMichael S. Tsirkin 	/* Just a hint for performance: so it's ok that this can be racy! */
24658d622d21SMichael S. Tsirkin 	if (vq->event)
24668d622d21SMichael S. Tsirkin 		vq->event_triggered = true;
24678d622d21SMichael S. Tsirkin 
24680a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
246918445c4dSRusty Russell 	if (vq->vq.callback)
247018445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
24710a8a69ddSRusty Russell 
24720a8a69ddSRusty Russell 	return IRQ_HANDLED;
24730a8a69ddSRusty Russell }
2474c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
24750a8a69ddSRusty Russell 
24761ce9e605STiwei Bie /* Only available for split ring */
247707d9629dSXuan Zhuo static struct virtqueue *__vring_new_virtqueue(unsigned int index,
2478cd4c812aSXuan Zhuo 					       struct vring_virtqueue_split *vring_split,
24790a8a69ddSRusty Russell 					       struct virtio_device *vdev,
24807b21e34fSRusty Russell 					       bool weak_barriers,
2481f94682ddSMichael S. Tsirkin 					       bool context,
248246f9c2b9SHeinz Graalfs 					       bool (*notify)(struct virtqueue *),
24839499f5e7SRusty Russell 					       void (*callback)(struct virtqueue *),
24849499f5e7SRusty Russell 					       const char *name)
24850a8a69ddSRusty Russell {
24862a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq;
2487a2b36c8dSXuan Zhuo 	int err;
24880a8a69ddSRusty Russell 
24891ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
24901ce9e605STiwei Bie 		return NULL;
24911ce9e605STiwei Bie 
2492cbeedb72STiwei Bie 	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
24930a8a69ddSRusty Russell 	if (!vq)
24940a8a69ddSRusty Russell 		return NULL;
24950a8a69ddSRusty Russell 
24961ce9e605STiwei Bie 	vq->packed_ring = false;
24970a8a69ddSRusty Russell 	vq->vq.callback = callback;
24980a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
24999499f5e7SRusty Russell 	vq->vq.name = name;
250006ca287dSRusty Russell 	vq->vq.index = index;
25014913e854SXuan Zhuo 	vq->vq.reset = false;
25022a2d1382SAndy Lutomirski 	vq->we_own_ring = false;
25030a8a69ddSRusty Russell 	vq->notify = notify;
25047b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
2505c346dae4SJason Wang #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
25068b4ec69dSJason Wang 	vq->broken = true;
2507c346dae4SJason Wang #else
2508c346dae4SJason Wang 	vq->broken = false;
2509c346dae4SJason Wang #endif
2510fb3fba6bSTiwei Bie 	vq->use_dma_api = vring_use_dma_api(vdev);
25110a8a69ddSRusty Russell 
25125a08b04fSMichael S. Tsirkin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
25135a08b04fSMichael S. Tsirkin 		!context;
2514a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
25159fa29b9dSMark McLoughlin 
251645383fb0STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
251745383fb0STiwei Bie 		vq->weak_barriers = false;
251845383fb0STiwei Bie 
2519a2b36c8dSXuan Zhuo 	err = vring_alloc_state_extra_split(vring_split);
2520a2b36c8dSXuan Zhuo 	if (err) {
2521a2b36c8dSXuan Zhuo 		kfree(vq);
2522a2b36c8dSXuan Zhuo 		return NULL;
2523a2b36c8dSXuan Zhuo 	}
252472b5e895SJason Wang 
2525198fa7beSXuan Zhuo 	virtqueue_vring_init_split(vring_split, vq);
2526198fa7beSXuan Zhuo 
2527cd4c812aSXuan Zhuo 	virtqueue_init(vq, vring_split->vring.num);
2528e1d6a423SXuan Zhuo 	virtqueue_vring_attach_split(vq, vring_split);
25293a897128SXuan Zhuo 
25300e566c8fSParav Pandit 	spin_lock(&vdev->vqs_list_lock);
2531e152d8afSDan Carpenter 	list_add_tail(&vq->vq.list, &vdev->vqs);
25320e566c8fSParav Pandit 	spin_unlock(&vdev->vqs_list_lock);
25330a8a69ddSRusty Russell 	return &vq->vq;
25340a8a69ddSRusty Russell }
25352a2d1382SAndy Lutomirski 
25362a2d1382SAndy Lutomirski struct virtqueue *vring_create_virtqueue(
25372a2d1382SAndy Lutomirski 	unsigned int index,
25382a2d1382SAndy Lutomirski 	unsigned int num,
25392a2d1382SAndy Lutomirski 	unsigned int vring_align,
25402a2d1382SAndy Lutomirski 	struct virtio_device *vdev,
25412a2d1382SAndy Lutomirski 	bool weak_barriers,
25422a2d1382SAndy Lutomirski 	bool may_reduce_num,
2543f94682ddSMichael S. Tsirkin 	bool context,
25442a2d1382SAndy Lutomirski 	bool (*notify)(struct virtqueue *),
25452a2d1382SAndy Lutomirski 	void (*callback)(struct virtqueue *),
25462a2d1382SAndy Lutomirski 	const char *name)
25472a2d1382SAndy Lutomirski {
25481ce9e605STiwei Bie 
25491ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
25501ce9e605STiwei Bie 		return vring_create_virtqueue_packed(index, num, vring_align,
25511ce9e605STiwei Bie 				vdev, weak_barriers, may_reduce_num,
25521ce9e605STiwei Bie 				context, notify, callback, name);
25531ce9e605STiwei Bie 
2554d79dca75STiwei Bie 	return vring_create_virtqueue_split(index, num, vring_align,
2555d79dca75STiwei Bie 			vdev, weak_barriers, may_reduce_num,
2556d79dca75STiwei Bie 			context, notify, callback, name);
25572a2d1382SAndy Lutomirski }
25582a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(vring_create_virtqueue);
25592a2d1382SAndy Lutomirski 
2560c790e8e1SXuan Zhuo /**
2561c790e8e1SXuan Zhuo  * virtqueue_resize - resize the vring of vq
2562c790e8e1SXuan Zhuo  * @_vq: the struct virtqueue we're talking about.
2563c790e8e1SXuan Zhuo  * @num: new ring num
2564c790e8e1SXuan Zhuo  * @recycle: callback for recycle the useless buffer
2565c790e8e1SXuan Zhuo  *
2566c790e8e1SXuan Zhuo  * When it is really necessary to create a new vring, it will set the current vq
2567c790e8e1SXuan Zhuo  * into the reset state. Then call the passed callback to recycle the buffer
2568c790e8e1SXuan Zhuo  * that is no longer used. Only after the new vring is successfully created, the
2569c790e8e1SXuan Zhuo  * old vring will be released.
2570c790e8e1SXuan Zhuo  *
2571c790e8e1SXuan Zhuo  * Caller must ensure we don't call this with other virtqueue operations
2572c790e8e1SXuan Zhuo  * at the same time (except where noted).
2573c790e8e1SXuan Zhuo  *
2574c790e8e1SXuan Zhuo  * Returns zero or a negative error.
2575c790e8e1SXuan Zhuo  * 0: success.
2576c790e8e1SXuan Zhuo  * -ENOMEM: Failed to allocate a new ring, fall back to the original ring size.
2577c790e8e1SXuan Zhuo  *  vq can still work normally
2578c790e8e1SXuan Zhuo  * -EBUSY: Failed to sync with device, vq may not work properly
2579c790e8e1SXuan Zhuo  * -ENOENT: Transport or device not supported
2580c790e8e1SXuan Zhuo  * -E2BIG/-EINVAL: num error
2581c790e8e1SXuan Zhuo  * -EPERM: Operation not permitted
2582c790e8e1SXuan Zhuo  *
2583c790e8e1SXuan Zhuo  */
2584c790e8e1SXuan Zhuo int virtqueue_resize(struct virtqueue *_vq, u32 num,
2585c790e8e1SXuan Zhuo 		     void (*recycle)(struct virtqueue *vq, void *buf))
2586c790e8e1SXuan Zhuo {
2587c790e8e1SXuan Zhuo 	struct vring_virtqueue *vq = to_vvq(_vq);
2588c790e8e1SXuan Zhuo 	struct virtio_device *vdev = vq->vq.vdev;
2589c790e8e1SXuan Zhuo 	void *buf;
2590c790e8e1SXuan Zhuo 	int err;
2591c790e8e1SXuan Zhuo 
2592c790e8e1SXuan Zhuo 	if (!vq->we_own_ring)
2593c790e8e1SXuan Zhuo 		return -EPERM;
2594c790e8e1SXuan Zhuo 
2595c790e8e1SXuan Zhuo 	if (num > vq->vq.num_max)
2596c790e8e1SXuan Zhuo 		return -E2BIG;
2597c790e8e1SXuan Zhuo 
2598c790e8e1SXuan Zhuo 	if (!num)
2599c790e8e1SXuan Zhuo 		return -EINVAL;
2600c790e8e1SXuan Zhuo 
2601c790e8e1SXuan Zhuo 	if ((vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num) == num)
2602c790e8e1SXuan Zhuo 		return 0;
2603c790e8e1SXuan Zhuo 
2604c790e8e1SXuan Zhuo 	if (!vdev->config->disable_vq_and_reset)
2605c790e8e1SXuan Zhuo 		return -ENOENT;
2606c790e8e1SXuan Zhuo 
2607c790e8e1SXuan Zhuo 	if (!vdev->config->enable_vq_after_reset)
2608c790e8e1SXuan Zhuo 		return -ENOENT;
2609c790e8e1SXuan Zhuo 
2610c790e8e1SXuan Zhuo 	err = vdev->config->disable_vq_and_reset(_vq);
2611c790e8e1SXuan Zhuo 	if (err)
2612c790e8e1SXuan Zhuo 		return err;
2613c790e8e1SXuan Zhuo 
2614c790e8e1SXuan Zhuo 	while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL)
2615c790e8e1SXuan Zhuo 		recycle(_vq, buf);
2616c790e8e1SXuan Zhuo 
2617c790e8e1SXuan Zhuo 	if (vq->packed_ring)
2618c790e8e1SXuan Zhuo 		err = virtqueue_resize_packed(_vq, num);
2619c790e8e1SXuan Zhuo 	else
2620c790e8e1SXuan Zhuo 		err = virtqueue_resize_split(_vq, num);
2621c790e8e1SXuan Zhuo 
2622c790e8e1SXuan Zhuo 	if (vdev->config->enable_vq_after_reset(_vq))
2623c790e8e1SXuan Zhuo 		return -EBUSY;
2624c790e8e1SXuan Zhuo 
2625c790e8e1SXuan Zhuo 	return err;
2626c790e8e1SXuan Zhuo }
2627c790e8e1SXuan Zhuo EXPORT_SYMBOL_GPL(virtqueue_resize);
2628c790e8e1SXuan Zhuo 
26291ce9e605STiwei Bie /* Only available for split ring */
26302a2d1382SAndy Lutomirski struct virtqueue *vring_new_virtqueue(unsigned int index,
26312a2d1382SAndy Lutomirski 				      unsigned int num,
26322a2d1382SAndy Lutomirski 				      unsigned int vring_align,
26332a2d1382SAndy Lutomirski 				      struct virtio_device *vdev,
26342a2d1382SAndy Lutomirski 				      bool weak_barriers,
2635f94682ddSMichael S. Tsirkin 				      bool context,
26362a2d1382SAndy Lutomirski 				      void *pages,
26372a2d1382SAndy Lutomirski 				      bool (*notify)(struct virtqueue *vq),
26382a2d1382SAndy Lutomirski 				      void (*callback)(struct virtqueue *vq),
26392a2d1382SAndy Lutomirski 				      const char *name)
26402a2d1382SAndy Lutomirski {
2641cd4c812aSXuan Zhuo 	struct vring_virtqueue_split vring_split = {};
26421ce9e605STiwei Bie 
26431ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
26441ce9e605STiwei Bie 		return NULL;
26451ce9e605STiwei Bie 
2646cd4c812aSXuan Zhuo 	vring_init(&vring_split.vring, num, pages, vring_align);
2647cd4c812aSXuan Zhuo 	return __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
2648cd4c812aSXuan Zhuo 				     context, notify, callback, name);
26492a2d1382SAndy Lutomirski }
2650c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
26510a8a69ddSRusty Russell 
26523ea19e32SXuan Zhuo static void vring_free(struct virtqueue *_vq)
26530a8a69ddSRusty Russell {
26542a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq = to_vvq(_vq);
26552a2d1382SAndy Lutomirski 
26562a2d1382SAndy Lutomirski 	if (vq->we_own_ring) {
26571ce9e605STiwei Bie 		if (vq->packed_ring) {
26581ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
26591ce9e605STiwei Bie 					 vq->packed.ring_size_in_bytes,
26601ce9e605STiwei Bie 					 vq->packed.vring.desc,
26611ce9e605STiwei Bie 					 vq->packed.ring_dma_addr);
26621ce9e605STiwei Bie 
26631ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
26641ce9e605STiwei Bie 					 vq->packed.event_size_in_bytes,
26651ce9e605STiwei Bie 					 vq->packed.vring.driver,
26661ce9e605STiwei Bie 					 vq->packed.driver_event_dma_addr);
26671ce9e605STiwei Bie 
26681ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
26691ce9e605STiwei Bie 					 vq->packed.event_size_in_bytes,
26701ce9e605STiwei Bie 					 vq->packed.vring.device,
26711ce9e605STiwei Bie 					 vq->packed.device_event_dma_addr);
26721ce9e605STiwei Bie 
26731ce9e605STiwei Bie 			kfree(vq->packed.desc_state);
26741ce9e605STiwei Bie 			kfree(vq->packed.desc_extra);
26751ce9e605STiwei Bie 		} else {
2676d79dca75STiwei Bie 			vring_free_queue(vq->vq.vdev,
2677d79dca75STiwei Bie 					 vq->split.queue_size_in_bytes,
2678d79dca75STiwei Bie 					 vq->split.vring.desc,
2679d79dca75STiwei Bie 					 vq->split.queue_dma_addr);
2680f13f09a1SSuman Anna 		}
2681f13f09a1SSuman Anna 	}
268272b5e895SJason Wang 	if (!vq->packed_ring) {
2683cbeedb72STiwei Bie 		kfree(vq->split.desc_state);
268472b5e895SJason Wang 		kfree(vq->split.desc_extra);
268572b5e895SJason Wang 	}
26863ea19e32SXuan Zhuo }
26873ea19e32SXuan Zhuo 
26883ea19e32SXuan Zhuo void vring_del_virtqueue(struct virtqueue *_vq)
26893ea19e32SXuan Zhuo {
26903ea19e32SXuan Zhuo 	struct vring_virtqueue *vq = to_vvq(_vq);
26913ea19e32SXuan Zhuo 
26923ea19e32SXuan Zhuo 	spin_lock(&vq->vq.vdev->vqs_list_lock);
26933ea19e32SXuan Zhuo 	list_del(&_vq->list);
26943ea19e32SXuan Zhuo 	spin_unlock(&vq->vq.vdev->vqs_list_lock);
26953ea19e32SXuan Zhuo 
26963ea19e32SXuan Zhuo 	vring_free(_vq);
26973ea19e32SXuan Zhuo 
26982a2d1382SAndy Lutomirski 	kfree(vq);
26990a8a69ddSRusty Russell }
2700c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
27010a8a69ddSRusty Russell 
2702e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
2703e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
2704e34f8725SRusty Russell {
2705e34f8725SRusty Russell 	unsigned int i;
2706e34f8725SRusty Russell 
2707e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2708e34f8725SRusty Russell 		switch (i) {
27099fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
27109fa29b9dSMark McLoughlin 			break;
2711a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
2712a5c262c5SMichael S. Tsirkin 			break;
2713747ae34aSMichael S. Tsirkin 		case VIRTIO_F_VERSION_1:
2714747ae34aSMichael S. Tsirkin 			break;
2715321bd212SMichael S. Tsirkin 		case VIRTIO_F_ACCESS_PLATFORM:
27161a937693SMichael S. Tsirkin 			break;
2717f959a128STiwei Bie 		case VIRTIO_F_RING_PACKED:
2718f959a128STiwei Bie 			break;
271945383fb0STiwei Bie 		case VIRTIO_F_ORDER_PLATFORM:
272045383fb0STiwei Bie 			break;
2721e34f8725SRusty Russell 		default:
2722e34f8725SRusty Russell 			/* We don't understand this bit. */
2723e16e12beSMichael S. Tsirkin 			__virtio_clear_bit(vdev, i);
2724e34f8725SRusty Russell 		}
2725e34f8725SRusty Russell 	}
2726e34f8725SRusty Russell }
2727e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
2728e34f8725SRusty Russell 
27295dfc1762SRusty Russell /**
27305dfc1762SRusty Russell  * virtqueue_get_vring_size - return the size of the virtqueue's vring
2731a5581206SJiang Biao  * @_vq: the struct virtqueue containing the vring of interest.
27325dfc1762SRusty Russell  *
27335dfc1762SRusty Russell  * Returns the size of the vring.  This is mainly used for boasting to
27345dfc1762SRusty Russell  * userspace.  Unlike other operations, this need not be serialized.
27355dfc1762SRusty Russell  */
27368f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
27378f9f4668SRick Jones {
27388f9f4668SRick Jones 
27398f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
27408f9f4668SRick Jones 
27411ce9e605STiwei Bie 	return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
27428f9f4668SRick Jones }
27438f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
27448f9f4668SRick Jones 
274532510631SXuan Zhuo /*
274632510631SXuan Zhuo  * This function should only be called by the core, not directly by the driver.
274732510631SXuan Zhuo  */
274832510631SXuan Zhuo void __virtqueue_break(struct virtqueue *_vq)
274932510631SXuan Zhuo {
275032510631SXuan Zhuo 	struct vring_virtqueue *vq = to_vvq(_vq);
275132510631SXuan Zhuo 
275232510631SXuan Zhuo 	/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
275332510631SXuan Zhuo 	WRITE_ONCE(vq->broken, true);
275432510631SXuan Zhuo }
275532510631SXuan Zhuo EXPORT_SYMBOL_GPL(__virtqueue_break);
275632510631SXuan Zhuo 
275732510631SXuan Zhuo /*
275832510631SXuan Zhuo  * This function should only be called by the core, not directly by the driver.
275932510631SXuan Zhuo  */
276032510631SXuan Zhuo void __virtqueue_unbreak(struct virtqueue *_vq)
276132510631SXuan Zhuo {
276232510631SXuan Zhuo 	struct vring_virtqueue *vq = to_vvq(_vq);
276332510631SXuan Zhuo 
276432510631SXuan Zhuo 	/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
276532510631SXuan Zhuo 	WRITE_ONCE(vq->broken, false);
276632510631SXuan Zhuo }
276732510631SXuan Zhuo EXPORT_SYMBOL_GPL(__virtqueue_unbreak);
276832510631SXuan Zhuo 
2769b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq)
2770b3b32c94SHeinz Graalfs {
2771b3b32c94SHeinz Graalfs 	struct vring_virtqueue *vq = to_vvq(_vq);
2772b3b32c94SHeinz Graalfs 
277360f07798SParav Pandit 	return READ_ONCE(vq->broken);
2774b3b32c94SHeinz Graalfs }
2775b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken);
2776b3b32c94SHeinz Graalfs 
2777e2dcdfe9SRusty Russell /*
2778e2dcdfe9SRusty Russell  * This should prevent the device from being used, allowing drivers to
2779e2dcdfe9SRusty Russell  * recover.  You may need to grab appropriate locks to flush.
2780e2dcdfe9SRusty Russell  */
2781e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev)
2782e2dcdfe9SRusty Russell {
2783e2dcdfe9SRusty Russell 	struct virtqueue *_vq;
2784e2dcdfe9SRusty Russell 
27850e566c8fSParav Pandit 	spin_lock(&dev->vqs_list_lock);
2786e2dcdfe9SRusty Russell 	list_for_each_entry(_vq, &dev->vqs, list) {
2787e2dcdfe9SRusty Russell 		struct vring_virtqueue *vq = to_vvq(_vq);
278860f07798SParav Pandit 
278960f07798SParav Pandit 		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
279060f07798SParav Pandit 		WRITE_ONCE(vq->broken, true);
2791e2dcdfe9SRusty Russell 	}
27920e566c8fSParav Pandit 	spin_unlock(&dev->vqs_list_lock);
2793e2dcdfe9SRusty Russell }
2794e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device);
2795e2dcdfe9SRusty Russell 
2796be83f04dSJason Wang /*
2797be83f04dSJason Wang  * This should allow the device to be used by the driver. You may
2798be83f04dSJason Wang  * need to grab appropriate locks to flush the write to
2799be83f04dSJason Wang  * vq->broken. This should only be used in some specific case e.g
2800be83f04dSJason Wang  * (probing and restoring). This function should only be called by the
2801be83f04dSJason Wang  * core, not directly by the driver.
2802be83f04dSJason Wang  */
2803be83f04dSJason Wang void __virtio_unbreak_device(struct virtio_device *dev)
2804be83f04dSJason Wang {
2805be83f04dSJason Wang 	struct virtqueue *_vq;
2806be83f04dSJason Wang 
2807be83f04dSJason Wang 	spin_lock(&dev->vqs_list_lock);
2808be83f04dSJason Wang 	list_for_each_entry(_vq, &dev->vqs, list) {
2809be83f04dSJason Wang 		struct vring_virtqueue *vq = to_vvq(_vq);
2810be83f04dSJason Wang 
2811be83f04dSJason Wang 		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2812be83f04dSJason Wang 		WRITE_ONCE(vq->broken, false);
2813be83f04dSJason Wang 	}
2814be83f04dSJason Wang 	spin_unlock(&dev->vqs_list_lock);
2815be83f04dSJason Wang }
2816be83f04dSJason Wang EXPORT_SYMBOL_GPL(__virtio_unbreak_device);
2817be83f04dSJason Wang 
28182a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
281989062652SCornelia Huck {
282089062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
282189062652SCornelia Huck 
28222a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
282389062652SCornelia Huck 
28241ce9e605STiwei Bie 	if (vq->packed_ring)
28251ce9e605STiwei Bie 		return vq->packed.ring_dma_addr;
28261ce9e605STiwei Bie 
2827d79dca75STiwei Bie 	return vq->split.queue_dma_addr;
28282a2d1382SAndy Lutomirski }
28292a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
28302a2d1382SAndy Lutomirski 
28312a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
283289062652SCornelia Huck {
283389062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
283489062652SCornelia Huck 
28352a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
28362a2d1382SAndy Lutomirski 
28371ce9e605STiwei Bie 	if (vq->packed_ring)
28381ce9e605STiwei Bie 		return vq->packed.driver_event_dma_addr;
28391ce9e605STiwei Bie 
2840d79dca75STiwei Bie 	return vq->split.queue_dma_addr +
2841e593bf97STiwei Bie 		((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
284289062652SCornelia Huck }
28432a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
28442a2d1382SAndy Lutomirski 
28452a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
28462a2d1382SAndy Lutomirski {
28472a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq = to_vvq(_vq);
28482a2d1382SAndy Lutomirski 
28492a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
28502a2d1382SAndy Lutomirski 
28511ce9e605STiwei Bie 	if (vq->packed_ring)
28521ce9e605STiwei Bie 		return vq->packed.device_event_dma_addr;
28531ce9e605STiwei Bie 
2854d79dca75STiwei Bie 	return vq->split.queue_dma_addr +
2855e593bf97STiwei Bie 		((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
28562a2d1382SAndy Lutomirski }
28572a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
28582a2d1382SAndy Lutomirski 
28591ce9e605STiwei Bie /* Only available for split ring */
28602a2d1382SAndy Lutomirski const struct vring *virtqueue_get_vring(struct virtqueue *vq)
28612a2d1382SAndy Lutomirski {
2862e593bf97STiwei Bie 	return &to_vvq(vq)->split.vring;
28632a2d1382SAndy Lutomirski }
28642a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_vring);
286589062652SCornelia Huck 
2866c6fd4701SRusty Russell MODULE_LICENSE("GPL");
2867