xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision ef5c366fea30f64d52bb1c4c1e2959a5e6b66e88)
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>
14f8ce7263SMichael S. Tsirkin #include <linux/spinlock.h>
1578fe3987SAndy Lutomirski #include <xen/xen.h>
160a8a69ddSRusty Russell 
170a8a69ddSRusty Russell #ifdef DEBUG
180a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */
199499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
209499f5e7SRusty Russell 	do {							\
219499f5e7SRusty Russell 		dev_err(&(_vq)->vq.vdev->dev,			\
229499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
239499f5e7SRusty Russell 		BUG();						\
249499f5e7SRusty Russell 	} while (0)
25c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */
263a35ce7dSRoel Kluin #define START_USE(_vq)						\
27c5f841f1SRusty Russell 	do {							\
28c5f841f1SRusty Russell 		if ((_vq)->in_use)				\
299499f5e7SRusty Russell 			panic("%s:in_use = %i\n",		\
309499f5e7SRusty Russell 			      (_vq)->vq.name, (_vq)->in_use);	\
31c5f841f1SRusty Russell 		(_vq)->in_use = __LINE__;			\
32c5f841f1SRusty Russell 	} while (0)
333a35ce7dSRoel Kluin #define END_USE(_vq) \
3497a545abSRusty Russell 	do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
354d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(_vq)				\
364d6a105eSTiwei Bie 	do {							\
374d6a105eSTiwei Bie 		ktime_t now = ktime_get();			\
384d6a105eSTiwei Bie 								\
394d6a105eSTiwei Bie 		/* No kick or get, with .1 second between?  Warn. */ \
404d6a105eSTiwei Bie 		if ((_vq)->last_add_time_valid)			\
414d6a105eSTiwei Bie 			WARN_ON(ktime_to_ms(ktime_sub(now,	\
424d6a105eSTiwei Bie 				(_vq)->last_add_time)) > 100);	\
434d6a105eSTiwei Bie 		(_vq)->last_add_time = now;			\
444d6a105eSTiwei Bie 		(_vq)->last_add_time_valid = true;		\
454d6a105eSTiwei Bie 	} while (0)
464d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(_vq)				\
474d6a105eSTiwei Bie 	do {							\
484d6a105eSTiwei Bie 		if ((_vq)->last_add_time_valid) {		\
494d6a105eSTiwei Bie 			WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \
504d6a105eSTiwei Bie 				      (_vq)->last_add_time)) > 100); \
514d6a105eSTiwei Bie 		}						\
524d6a105eSTiwei Bie 	} while (0)
534d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(_vq)				\
544d6a105eSTiwei Bie 	((_vq)->last_add_time_valid = false)
550a8a69ddSRusty Russell #else
569499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
579499f5e7SRusty Russell 	do {							\
589499f5e7SRusty Russell 		dev_err(&_vq->vq.vdev->dev,			\
599499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
609499f5e7SRusty Russell 		(_vq)->broken = true;				\
619499f5e7SRusty Russell 	} while (0)
620a8a69ddSRusty Russell #define START_USE(vq)
630a8a69ddSRusty Russell #define END_USE(vq)
644d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(vq)
654d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(vq)
664d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(vq)
670a8a69ddSRusty Russell #endif
680a8a69ddSRusty Russell 
69cbeedb72STiwei Bie struct vring_desc_state_split {
70780bc790SAndy Lutomirski 	void *data;			/* Data for callback. */
71780bc790SAndy Lutomirski 	struct vring_desc *indir_desc;	/* Indirect descriptor, if any. */
72780bc790SAndy Lutomirski };
73780bc790SAndy Lutomirski 
741ce9e605STiwei Bie struct vring_desc_state_packed {
751ce9e605STiwei Bie 	void *data;			/* Data for callback. */
761ce9e605STiwei Bie 	struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */
771ce9e605STiwei Bie 	u16 num;			/* Descriptor list length. */
781ce9e605STiwei Bie 	u16 last;			/* The last desc state in a list. */
791ce9e605STiwei Bie };
801ce9e605STiwei Bie 
811f28750fSJason Wang struct vring_desc_extra {
82*ef5c366fSJason Wang 	dma_addr_t addr;		/* Descriptor DMA addr. */
83*ef5c366fSJason Wang 	u32 len;			/* Descriptor length. */
841ce9e605STiwei Bie 	u16 flags;			/* Descriptor flags. */
85aeef9b47SJason Wang 	u16 next;			/* The next desc state in a list. */
861ce9e605STiwei Bie };
871ce9e605STiwei Bie 
8843b4f721SMichael S. Tsirkin struct vring_virtqueue {
890a8a69ddSRusty Russell 	struct virtqueue vq;
900a8a69ddSRusty Russell 
911ce9e605STiwei Bie 	/* Is this a packed ring? */
921ce9e605STiwei Bie 	bool packed_ring;
931ce9e605STiwei Bie 
94fb3fba6bSTiwei Bie 	/* Is DMA API used? */
95fb3fba6bSTiwei Bie 	bool use_dma_api;
96fb3fba6bSTiwei Bie 
977b21e34fSRusty Russell 	/* Can we use weak barriers? */
987b21e34fSRusty Russell 	bool weak_barriers;
997b21e34fSRusty Russell 
1000a8a69ddSRusty Russell 	/* Other side has made a mess, don't try any more. */
1010a8a69ddSRusty Russell 	bool broken;
1020a8a69ddSRusty Russell 
1039fa29b9dSMark McLoughlin 	/* Host supports indirect buffers */
1049fa29b9dSMark McLoughlin 	bool indirect;
1059fa29b9dSMark McLoughlin 
106a5c262c5SMichael S. Tsirkin 	/* Host publishes avail event idx */
107a5c262c5SMichael S. Tsirkin 	bool event;
108a5c262c5SMichael S. Tsirkin 
1090a8a69ddSRusty Russell 	/* Head of free buffer list. */
1100a8a69ddSRusty Russell 	unsigned int free_head;
1110a8a69ddSRusty Russell 	/* Number we've added since last sync. */
1120a8a69ddSRusty Russell 	unsigned int num_added;
1130a8a69ddSRusty Russell 
1140a8a69ddSRusty Russell 	/* Last used index we've seen. */
1151bc4953eSAnthony Liguori 	u16 last_used_idx;
1160a8a69ddSRusty Russell 
1178d622d21SMichael S. Tsirkin 	/* Hint for event idx: already triggered no need to disable. */
1188d622d21SMichael S. Tsirkin 	bool event_triggered;
1198d622d21SMichael S. Tsirkin 
1201ce9e605STiwei Bie 	union {
1211ce9e605STiwei Bie 		/* Available for split ring */
122e593bf97STiwei Bie 		struct {
1231ce9e605STiwei Bie 			/* Actual memory layout for this queue. */
124e593bf97STiwei Bie 			struct vring vring;
125e593bf97STiwei Bie 
126f277ec42SVenkatesh Srinivas 			/* Last written value to avail->flags */
127f277ec42SVenkatesh Srinivas 			u16 avail_flags_shadow;
128f277ec42SVenkatesh Srinivas 
1291ce9e605STiwei Bie 			/*
1301ce9e605STiwei Bie 			 * Last written value to avail->idx in
1311ce9e605STiwei Bie 			 * guest byte order.
1321ce9e605STiwei Bie 			 */
133f277ec42SVenkatesh Srinivas 			u16 avail_idx_shadow;
134cbeedb72STiwei Bie 
135cbeedb72STiwei Bie 			/* Per-descriptor state. */
136cbeedb72STiwei Bie 			struct vring_desc_state_split *desc_state;
13772b5e895SJason Wang 			struct vring_desc_extra *desc_extra;
138d79dca75STiwei Bie 
1391ce9e605STiwei Bie 			/* DMA address and size information */
140d79dca75STiwei Bie 			dma_addr_t queue_dma_addr;
1411ce9e605STiwei Bie 			size_t queue_size_in_bytes;
142e593bf97STiwei Bie 		} split;
143f277ec42SVenkatesh Srinivas 
1441ce9e605STiwei Bie 		/* Available for packed ring */
1451ce9e605STiwei Bie 		struct {
1461ce9e605STiwei Bie 			/* Actual memory layout for this queue. */
1479c0644eeSMichael S. Tsirkin 			struct {
1489c0644eeSMichael S. Tsirkin 				unsigned int num;
1499c0644eeSMichael S. Tsirkin 				struct vring_packed_desc *desc;
1509c0644eeSMichael S. Tsirkin 				struct vring_packed_desc_event *driver;
1519c0644eeSMichael S. Tsirkin 				struct vring_packed_desc_event *device;
1529c0644eeSMichael S. Tsirkin 			} vring;
1531ce9e605STiwei Bie 
1541ce9e605STiwei Bie 			/* Driver ring wrap counter. */
1551ce9e605STiwei Bie 			bool avail_wrap_counter;
1561ce9e605STiwei Bie 
1571ce9e605STiwei Bie 			/* Device ring wrap counter. */
1581ce9e605STiwei Bie 			bool used_wrap_counter;
1591ce9e605STiwei Bie 
1601ce9e605STiwei Bie 			/* Avail used flags. */
1611ce9e605STiwei Bie 			u16 avail_used_flags;
1621ce9e605STiwei Bie 
1631ce9e605STiwei Bie 			/* Index of the next avail descriptor. */
1641ce9e605STiwei Bie 			u16 next_avail_idx;
1651ce9e605STiwei Bie 
1661ce9e605STiwei Bie 			/*
1671ce9e605STiwei Bie 			 * Last written value to driver->flags in
1681ce9e605STiwei Bie 			 * guest byte order.
1691ce9e605STiwei Bie 			 */
1701ce9e605STiwei Bie 			u16 event_flags_shadow;
1711ce9e605STiwei Bie 
1721ce9e605STiwei Bie 			/* Per-descriptor state. */
1731ce9e605STiwei Bie 			struct vring_desc_state_packed *desc_state;
1741f28750fSJason Wang 			struct vring_desc_extra *desc_extra;
1751ce9e605STiwei Bie 
1761ce9e605STiwei Bie 			/* DMA address and size information */
1771ce9e605STiwei Bie 			dma_addr_t ring_dma_addr;
1781ce9e605STiwei Bie 			dma_addr_t driver_event_dma_addr;
1791ce9e605STiwei Bie 			dma_addr_t device_event_dma_addr;
1801ce9e605STiwei Bie 			size_t ring_size_in_bytes;
1811ce9e605STiwei Bie 			size_t event_size_in_bytes;
1821ce9e605STiwei Bie 		} packed;
1831ce9e605STiwei Bie 	};
1841ce9e605STiwei Bie 
1850a8a69ddSRusty Russell 	/* How to notify other side. FIXME: commonalize hcalls! */
18646f9c2b9SHeinz Graalfs 	bool (*notify)(struct virtqueue *vq);
1870a8a69ddSRusty Russell 
1882a2d1382SAndy Lutomirski 	/* DMA, allocation, and size information */
1892a2d1382SAndy Lutomirski 	bool we_own_ring;
1902a2d1382SAndy Lutomirski 
1910a8a69ddSRusty Russell #ifdef DEBUG
1920a8a69ddSRusty Russell 	/* They're supposed to lock for us. */
1930a8a69ddSRusty Russell 	unsigned int in_use;
194e93300b1SRusty Russell 
195e93300b1SRusty Russell 	/* Figure out if their kicks are too delayed. */
196e93300b1SRusty Russell 	bool last_add_time_valid;
197e93300b1SRusty Russell 	ktime_t last_add_time;
1980a8a69ddSRusty Russell #endif
1990a8a69ddSRusty Russell };
2000a8a69ddSRusty Russell 
201e6f633e5STiwei Bie 
202e6f633e5STiwei Bie /*
203e6f633e5STiwei Bie  * Helpers.
204e6f633e5STiwei Bie  */
205e6f633e5STiwei Bie 
2060a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
2070a8a69ddSRusty Russell 
2082f18c2d1STiwei Bie static inline bool virtqueue_use_indirect(struct virtqueue *_vq,
2092f18c2d1STiwei Bie 					  unsigned int total_sg)
2102f18c2d1STiwei Bie {
2112f18c2d1STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
2122f18c2d1STiwei Bie 
2132f18c2d1STiwei Bie 	/*
2142f18c2d1STiwei Bie 	 * If the host supports indirect descriptor tables, and we have multiple
2152f18c2d1STiwei Bie 	 * buffers, then go indirect. FIXME: tune this threshold
2162f18c2d1STiwei Bie 	 */
2172f18c2d1STiwei Bie 	return (vq->indirect && total_sg > 1 && vq->vq.num_free);
2182f18c2d1STiwei Bie }
2192f18c2d1STiwei Bie 
220d26c96c8SAndy Lutomirski /*
2211a937693SMichael S. Tsirkin  * Modern virtio devices have feature bits to specify whether they need a
2221a937693SMichael S. Tsirkin  * quirk and bypass the IOMMU. If not there, just use the DMA API.
2231a937693SMichael S. Tsirkin  *
2241a937693SMichael S. Tsirkin  * If there, the interaction between virtio and DMA API is messy.
225d26c96c8SAndy Lutomirski  *
226d26c96c8SAndy Lutomirski  * On most systems with virtio, physical addresses match bus addresses,
227d26c96c8SAndy Lutomirski  * and it doesn't particularly matter whether we use the DMA API.
228d26c96c8SAndy Lutomirski  *
229d26c96c8SAndy Lutomirski  * On some systems, including Xen and any system with a physical device
230d26c96c8SAndy Lutomirski  * that speaks virtio behind a physical IOMMU, we must use the DMA API
231d26c96c8SAndy Lutomirski  * for virtio DMA to work at all.
232d26c96c8SAndy Lutomirski  *
233d26c96c8SAndy Lutomirski  * On other systems, including SPARC and PPC64, virtio-pci devices are
234d26c96c8SAndy Lutomirski  * enumerated as though they are behind an IOMMU, but the virtio host
235d26c96c8SAndy Lutomirski  * ignores the IOMMU, so we must either pretend that the IOMMU isn't
236d26c96c8SAndy Lutomirski  * there or somehow map everything as the identity.
237d26c96c8SAndy Lutomirski  *
238d26c96c8SAndy Lutomirski  * For the time being, we preserve historic behavior and bypass the DMA
239d26c96c8SAndy Lutomirski  * API.
2401a937693SMichael S. Tsirkin  *
2411a937693SMichael S. Tsirkin  * TODO: install a per-device DMA ops structure that does the right thing
2421a937693SMichael S. Tsirkin  * taking into account all the above quirks, and use the DMA API
2431a937693SMichael S. Tsirkin  * unconditionally on data path.
244d26c96c8SAndy Lutomirski  */
245d26c96c8SAndy Lutomirski 
246d26c96c8SAndy Lutomirski static bool vring_use_dma_api(struct virtio_device *vdev)
247d26c96c8SAndy Lutomirski {
24824b6842aSMichael S. Tsirkin 	if (!virtio_has_dma_quirk(vdev))
2491a937693SMichael S. Tsirkin 		return true;
2501a937693SMichael S. Tsirkin 
2511a937693SMichael S. Tsirkin 	/* Otherwise, we are left to guess. */
25278fe3987SAndy Lutomirski 	/*
25378fe3987SAndy Lutomirski 	 * In theory, it's possible to have a buggy QEMU-supposed
25478fe3987SAndy Lutomirski 	 * emulated Q35 IOMMU and Xen enabled at the same time.  On
25578fe3987SAndy Lutomirski 	 * such a configuration, virtio has never worked and will
25678fe3987SAndy Lutomirski 	 * not work without an even larger kludge.  Instead, enable
25778fe3987SAndy Lutomirski 	 * the DMA API if we're a Xen guest, which at least allows
25878fe3987SAndy Lutomirski 	 * all of the sensible Xen configurations to work correctly.
25978fe3987SAndy Lutomirski 	 */
26078fe3987SAndy Lutomirski 	if (xen_domain())
26178fe3987SAndy Lutomirski 		return true;
26278fe3987SAndy Lutomirski 
263d26c96c8SAndy Lutomirski 	return false;
264d26c96c8SAndy Lutomirski }
265d26c96c8SAndy Lutomirski 
266e6d6dd6cSJoerg Roedel size_t virtio_max_dma_size(struct virtio_device *vdev)
267e6d6dd6cSJoerg Roedel {
268e6d6dd6cSJoerg Roedel 	size_t max_segment_size = SIZE_MAX;
269e6d6dd6cSJoerg Roedel 
270e6d6dd6cSJoerg Roedel 	if (vring_use_dma_api(vdev))
271e6d6dd6cSJoerg Roedel 		max_segment_size = dma_max_mapping_size(&vdev->dev);
272e6d6dd6cSJoerg Roedel 
273e6d6dd6cSJoerg Roedel 	return max_segment_size;
274e6d6dd6cSJoerg Roedel }
275e6d6dd6cSJoerg Roedel EXPORT_SYMBOL_GPL(virtio_max_dma_size);
276e6d6dd6cSJoerg Roedel 
277d79dca75STiwei Bie static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
278d79dca75STiwei Bie 			      dma_addr_t *dma_handle, gfp_t flag)
279d79dca75STiwei Bie {
280d79dca75STiwei Bie 	if (vring_use_dma_api(vdev)) {
281d79dca75STiwei Bie 		return dma_alloc_coherent(vdev->dev.parent, size,
282d79dca75STiwei Bie 					  dma_handle, flag);
283d79dca75STiwei Bie 	} else {
284d79dca75STiwei Bie 		void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
285d79dca75STiwei Bie 
286d79dca75STiwei Bie 		if (queue) {
287d79dca75STiwei Bie 			phys_addr_t phys_addr = virt_to_phys(queue);
288d79dca75STiwei Bie 			*dma_handle = (dma_addr_t)phys_addr;
289d79dca75STiwei Bie 
290d79dca75STiwei Bie 			/*
291d79dca75STiwei Bie 			 * Sanity check: make sure we dind't truncate
292d79dca75STiwei Bie 			 * the address.  The only arches I can find that
293d79dca75STiwei Bie 			 * have 64-bit phys_addr_t but 32-bit dma_addr_t
294d79dca75STiwei Bie 			 * are certain non-highmem MIPS and x86
295d79dca75STiwei Bie 			 * configurations, but these configurations
296d79dca75STiwei Bie 			 * should never allocate physical pages above 32
297d79dca75STiwei Bie 			 * bits, so this is fine.  Just in case, throw a
298d79dca75STiwei Bie 			 * warning and abort if we end up with an
299d79dca75STiwei Bie 			 * unrepresentable address.
300d79dca75STiwei Bie 			 */
301d79dca75STiwei Bie 			if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
302d79dca75STiwei Bie 				free_pages_exact(queue, PAGE_ALIGN(size));
303d79dca75STiwei Bie 				return NULL;
304d79dca75STiwei Bie 			}
305d79dca75STiwei Bie 		}
306d79dca75STiwei Bie 		return queue;
307d79dca75STiwei Bie 	}
308d79dca75STiwei Bie }
309d79dca75STiwei Bie 
310d79dca75STiwei Bie static void vring_free_queue(struct virtio_device *vdev, size_t size,
311d79dca75STiwei Bie 			     void *queue, dma_addr_t dma_handle)
312d79dca75STiwei Bie {
313d79dca75STiwei Bie 	if (vring_use_dma_api(vdev))
314d79dca75STiwei Bie 		dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
315d79dca75STiwei Bie 	else
316d79dca75STiwei Bie 		free_pages_exact(queue, PAGE_ALIGN(size));
317d79dca75STiwei Bie }
318d79dca75STiwei Bie 
319780bc790SAndy Lutomirski /*
320780bc790SAndy Lutomirski  * The DMA ops on various arches are rather gnarly right now, and
321780bc790SAndy Lutomirski  * making all of the arch DMA ops work on the vring device itself
322780bc790SAndy Lutomirski  * is a mess.  For now, we use the parent device for DMA ops.
323780bc790SAndy Lutomirski  */
32475bfa81bSMichael S. Tsirkin static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq)
325780bc790SAndy Lutomirski {
326780bc790SAndy Lutomirski 	return vq->vq.vdev->dev.parent;
327780bc790SAndy Lutomirski }
328780bc790SAndy Lutomirski 
329780bc790SAndy Lutomirski /* Map one sg entry. */
330780bc790SAndy Lutomirski static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
331780bc790SAndy Lutomirski 				   struct scatterlist *sg,
332780bc790SAndy Lutomirski 				   enum dma_data_direction direction)
333780bc790SAndy Lutomirski {
334fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
335780bc790SAndy Lutomirski 		return (dma_addr_t)sg_phys(sg);
336780bc790SAndy Lutomirski 
337780bc790SAndy Lutomirski 	/*
338780bc790SAndy Lutomirski 	 * We can't use dma_map_sg, because we don't use scatterlists in
339780bc790SAndy Lutomirski 	 * the way it expects (we don't guarantee that the scatterlist
340780bc790SAndy Lutomirski 	 * will exist for the lifetime of the mapping).
341780bc790SAndy Lutomirski 	 */
342780bc790SAndy Lutomirski 	return dma_map_page(vring_dma_dev(vq),
343780bc790SAndy Lutomirski 			    sg_page(sg), sg->offset, sg->length,
344780bc790SAndy Lutomirski 			    direction);
345780bc790SAndy Lutomirski }
346780bc790SAndy Lutomirski 
347780bc790SAndy Lutomirski static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
348780bc790SAndy Lutomirski 				   void *cpu_addr, size_t size,
349780bc790SAndy Lutomirski 				   enum dma_data_direction direction)
350780bc790SAndy Lutomirski {
351fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
352780bc790SAndy Lutomirski 		return (dma_addr_t)virt_to_phys(cpu_addr);
353780bc790SAndy Lutomirski 
354780bc790SAndy Lutomirski 	return dma_map_single(vring_dma_dev(vq),
355780bc790SAndy Lutomirski 			      cpu_addr, size, direction);
356780bc790SAndy Lutomirski }
357780bc790SAndy Lutomirski 
358e6f633e5STiwei Bie static int vring_mapping_error(const struct vring_virtqueue *vq,
359e6f633e5STiwei Bie 			       dma_addr_t addr)
360e6f633e5STiwei Bie {
361fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
362e6f633e5STiwei Bie 		return 0;
363e6f633e5STiwei Bie 
364e6f633e5STiwei Bie 	return dma_mapping_error(vring_dma_dev(vq), addr);
365e6f633e5STiwei Bie }
366e6f633e5STiwei Bie 
367e6f633e5STiwei Bie 
368e6f633e5STiwei Bie /*
369e6f633e5STiwei Bie  * Split ring specific functions - *_split().
370e6f633e5STiwei Bie  */
371e6f633e5STiwei Bie 
37272b5e895SJason Wang static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
373780bc790SAndy Lutomirski 					   struct vring_desc *desc)
374780bc790SAndy Lutomirski {
375780bc790SAndy Lutomirski 	u16 flags;
376780bc790SAndy Lutomirski 
377fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
378780bc790SAndy Lutomirski 		return;
379780bc790SAndy Lutomirski 
380780bc790SAndy Lutomirski 	flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
381780bc790SAndy Lutomirski 
382780bc790SAndy Lutomirski 	if (flags & VRING_DESC_F_INDIRECT) {
383780bc790SAndy Lutomirski 		dma_unmap_single(vring_dma_dev(vq),
384780bc790SAndy Lutomirski 				 virtio64_to_cpu(vq->vq.vdev, desc->addr),
385780bc790SAndy Lutomirski 				 virtio32_to_cpu(vq->vq.vdev, desc->len),
386780bc790SAndy Lutomirski 				 (flags & VRING_DESC_F_WRITE) ?
387780bc790SAndy Lutomirski 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
388780bc790SAndy Lutomirski 	} else {
389780bc790SAndy Lutomirski 		dma_unmap_page(vring_dma_dev(vq),
390780bc790SAndy Lutomirski 			       virtio64_to_cpu(vq->vq.vdev, desc->addr),
391780bc790SAndy Lutomirski 			       virtio32_to_cpu(vq->vq.vdev, desc->len),
392780bc790SAndy Lutomirski 			       (flags & VRING_DESC_F_WRITE) ?
393780bc790SAndy Lutomirski 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
394780bc790SAndy Lutomirski 	}
395780bc790SAndy Lutomirski }
396780bc790SAndy Lutomirski 
39772b5e895SJason Wang static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
39872b5e895SJason Wang 					  unsigned int i)
39972b5e895SJason Wang {
40072b5e895SJason Wang 	struct vring_desc_extra *extra = vq->split.desc_extra;
40172b5e895SJason Wang 	u16 flags;
40272b5e895SJason Wang 
40372b5e895SJason Wang 	if (!vq->use_dma_api)
40472b5e895SJason Wang 		goto out;
40572b5e895SJason Wang 
40672b5e895SJason Wang 	flags = extra[i].flags;
40772b5e895SJason Wang 
40872b5e895SJason Wang 	if (flags & VRING_DESC_F_INDIRECT) {
40972b5e895SJason Wang 		dma_unmap_single(vring_dma_dev(vq),
41072b5e895SJason Wang 				 extra[i].addr,
41172b5e895SJason Wang 				 extra[i].len,
41272b5e895SJason Wang 				 (flags & VRING_DESC_F_WRITE) ?
41372b5e895SJason Wang 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
41472b5e895SJason Wang 	} else {
41572b5e895SJason Wang 		dma_unmap_page(vring_dma_dev(vq),
41672b5e895SJason Wang 			       extra[i].addr,
41772b5e895SJason Wang 			       extra[i].len,
41872b5e895SJason Wang 			       (flags & VRING_DESC_F_WRITE) ?
41972b5e895SJason Wang 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
42072b5e895SJason Wang 	}
42172b5e895SJason Wang 
42272b5e895SJason Wang out:
42372b5e895SJason Wang 	return extra[i].next;
42472b5e895SJason Wang }
42572b5e895SJason Wang 
426138fd251STiwei Bie static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
427138fd251STiwei Bie 					       unsigned int total_sg,
428138fd251STiwei Bie 					       gfp_t gfp)
4299fa29b9dSMark McLoughlin {
4309fa29b9dSMark McLoughlin 	struct vring_desc *desc;
431b25bd251SRusty Russell 	unsigned int i;
4329fa29b9dSMark McLoughlin 
433b92b1b89SWill Deacon 	/*
434b92b1b89SWill Deacon 	 * We require lowmem mappings for the descriptors because
435b92b1b89SWill Deacon 	 * otherwise virt_to_phys will give us bogus addresses in the
436b92b1b89SWill Deacon 	 * virtqueue.
437b92b1b89SWill Deacon 	 */
43882107539SMichal Hocko 	gfp &= ~__GFP_HIGHMEM;
439b92b1b89SWill Deacon 
4406da2ec56SKees Cook 	desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
4419fa29b9dSMark McLoughlin 	if (!desc)
442b25bd251SRusty Russell 		return NULL;
4439fa29b9dSMark McLoughlin 
444b25bd251SRusty Russell 	for (i = 0; i < total_sg; i++)
44500e6f3d9SMichael S. Tsirkin 		desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
446b25bd251SRusty Russell 	return desc;
4479fa29b9dSMark McLoughlin }
4489fa29b9dSMark McLoughlin 
449fe4c3862SJason Wang static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
450fe4c3862SJason Wang 						    struct vring_desc *desc,
451fe4c3862SJason Wang 						    unsigned int i,
452fe4c3862SJason Wang 						    dma_addr_t addr,
453fe4c3862SJason Wang 						    unsigned int len,
45472b5e895SJason Wang 						    u16 flags,
45572b5e895SJason Wang 						    bool indirect)
456fe4c3862SJason Wang {
45772b5e895SJason Wang 	struct vring_virtqueue *vring = to_vvq(vq);
45872b5e895SJason Wang 	struct vring_desc_extra *extra = vring->split.desc_extra;
45972b5e895SJason Wang 	u16 next;
46072b5e895SJason Wang 
461fe4c3862SJason Wang 	desc[i].flags = cpu_to_virtio16(vq->vdev, flags);
462fe4c3862SJason Wang 	desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
463fe4c3862SJason Wang 	desc[i].len = cpu_to_virtio32(vq->vdev, len);
464fe4c3862SJason Wang 
46572b5e895SJason Wang 	if (!indirect) {
46672b5e895SJason Wang 		next = extra[i].next;
46772b5e895SJason Wang 		desc[i].next = cpu_to_virtio16(vq->vdev, next);
46872b5e895SJason Wang 
46972b5e895SJason Wang 		extra[i].addr = addr;
47072b5e895SJason Wang 		extra[i].len = len;
47172b5e895SJason Wang 		extra[i].flags = flags;
47272b5e895SJason Wang 	} else
47372b5e895SJason Wang 		next = virtio16_to_cpu(vq->vdev, desc[i].next);
47472b5e895SJason Wang 
47572b5e895SJason Wang 	return next;
476fe4c3862SJason Wang }
477fe4c3862SJason Wang 
478138fd251STiwei Bie static inline int virtqueue_add_split(struct virtqueue *_vq,
47913816c76SRusty Russell 				      struct scatterlist *sgs[],
480eeebf9b1SRusty Russell 				      unsigned int total_sg,
48113816c76SRusty Russell 				      unsigned int out_sgs,
48213816c76SRusty Russell 				      unsigned int in_sgs,
483bbd603efSMichael S. Tsirkin 				      void *data,
4845a08b04fSMichael S. Tsirkin 				      void *ctx,
485bbd603efSMichael S. Tsirkin 				      gfp_t gfp)
4860a8a69ddSRusty Russell {
4870a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
48813816c76SRusty Russell 	struct scatterlist *sg;
489b25bd251SRusty Russell 	struct vring_desc *desc;
4903f649ab7SKees Cook 	unsigned int i, n, avail, descs_used, prev, err_idx;
4911fe9b6feSMichael S. Tsirkin 	int head;
492b25bd251SRusty Russell 	bool indirect;
4930a8a69ddSRusty Russell 
4949fa29b9dSMark McLoughlin 	START_USE(vq);
4959fa29b9dSMark McLoughlin 
4960a8a69ddSRusty Russell 	BUG_ON(data == NULL);
4975a08b04fSMichael S. Tsirkin 	BUG_ON(ctx && vq->indirect);
4989fa29b9dSMark McLoughlin 
49970670444SRusty Russell 	if (unlikely(vq->broken)) {
50070670444SRusty Russell 		END_USE(vq);
50170670444SRusty Russell 		return -EIO;
50270670444SRusty Russell 	}
50370670444SRusty Russell 
5044d6a105eSTiwei Bie 	LAST_ADD_TIME_UPDATE(vq);
505e93300b1SRusty Russell 
50613816c76SRusty Russell 	BUG_ON(total_sg == 0);
5070a8a69ddSRusty Russell 
508b25bd251SRusty Russell 	head = vq->free_head;
509b25bd251SRusty Russell 
5102f18c2d1STiwei Bie 	if (virtqueue_use_indirect(_vq, total_sg))
511138fd251STiwei Bie 		desc = alloc_indirect_split(_vq, total_sg, gfp);
51244ed8089SRichard W.M. Jones 	else {
513b25bd251SRusty Russell 		desc = NULL;
514e593bf97STiwei Bie 		WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
51544ed8089SRichard W.M. Jones 	}
516b25bd251SRusty Russell 
517b25bd251SRusty Russell 	if (desc) {
518b25bd251SRusty Russell 		/* Use a single buffer which doesn't continue */
519780bc790SAndy Lutomirski 		indirect = true;
520b25bd251SRusty Russell 		/* Set up rest to use this indirect table. */
521b25bd251SRusty Russell 		i = 0;
522b25bd251SRusty Russell 		descs_used = 1;
523b25bd251SRusty Russell 	} else {
524780bc790SAndy Lutomirski 		indirect = false;
525e593bf97STiwei Bie 		desc = vq->split.vring.desc;
526b25bd251SRusty Russell 		i = head;
527b25bd251SRusty Russell 		descs_used = total_sg;
528b25bd251SRusty Russell 	}
529b25bd251SRusty Russell 
530b25bd251SRusty Russell 	if (vq->vq.num_free < descs_used) {
5310a8a69ddSRusty Russell 		pr_debug("Can't add buf len %i - avail = %i\n",
532b25bd251SRusty Russell 			 descs_used, vq->vq.num_free);
53344653eaeSRusty Russell 		/* FIXME: for historical reasons, we force a notify here if
53444653eaeSRusty Russell 		 * there are outgoing parts to the buffer.  Presumably the
53544653eaeSRusty Russell 		 * host should service the ring ASAP. */
53613816c76SRusty Russell 		if (out_sgs)
537426e3e0aSRusty Russell 			vq->notify(&vq->vq);
53858625edfSWei Yongjun 		if (indirect)
53958625edfSWei Yongjun 			kfree(desc);
5400a8a69ddSRusty Russell 		END_USE(vq);
5410a8a69ddSRusty Russell 		return -ENOSPC;
5420a8a69ddSRusty Russell 	}
5430a8a69ddSRusty Russell 
54413816c76SRusty Russell 	for (n = 0; n < out_sgs; n++) {
545eeebf9b1SRusty Russell 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
546780bc790SAndy Lutomirski 			dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
547780bc790SAndy Lutomirski 			if (vring_mapping_error(vq, addr))
548780bc790SAndy Lutomirski 				goto unmap_release;
549780bc790SAndy Lutomirski 
5500a8a69ddSRusty Russell 			prev = i;
55172b5e895SJason Wang 			/* Note that we trust indirect descriptor
55272b5e895SJason Wang 			 * table since it use stream DMA mapping.
55372b5e895SJason Wang 			 */
554fe4c3862SJason Wang 			i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length,
55572b5e895SJason Wang 						     VRING_DESC_F_NEXT,
55672b5e895SJason Wang 						     indirect);
5570a8a69ddSRusty Russell 		}
55813816c76SRusty Russell 	}
55913816c76SRusty Russell 	for (; n < (out_sgs + in_sgs); n++) {
560eeebf9b1SRusty Russell 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
561780bc790SAndy Lutomirski 			dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
562780bc790SAndy Lutomirski 			if (vring_mapping_error(vq, addr))
563780bc790SAndy Lutomirski 				goto unmap_release;
564780bc790SAndy Lutomirski 
5650a8a69ddSRusty Russell 			prev = i;
56672b5e895SJason Wang 			/* Note that we trust indirect descriptor
56772b5e895SJason Wang 			 * table since it use stream DMA mapping.
56872b5e895SJason Wang 			 */
569fe4c3862SJason Wang 			i = virtqueue_add_desc_split(_vq, desc, i, addr,
570fe4c3862SJason Wang 						     sg->length,
571fe4c3862SJason Wang 						     VRING_DESC_F_NEXT |
57272b5e895SJason Wang 						     VRING_DESC_F_WRITE,
57372b5e895SJason Wang 						     indirect);
57413816c76SRusty Russell 		}
5750a8a69ddSRusty Russell 	}
5760a8a69ddSRusty Russell 	/* Last one doesn't continue. */
57700e6f3d9SMichael S. Tsirkin 	desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
57872b5e895SJason Wang 	if (!indirect && vq->use_dma_api)
579890d3356SVincent Whitchurch 		vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
58072b5e895SJason Wang 			~VRING_DESC_F_NEXT;
5810a8a69ddSRusty Russell 
582780bc790SAndy Lutomirski 	if (indirect) {
583780bc790SAndy Lutomirski 		/* Now that the indirect table is filled in, map it. */
584780bc790SAndy Lutomirski 		dma_addr_t addr = vring_map_single(
585780bc790SAndy Lutomirski 			vq, desc, total_sg * sizeof(struct vring_desc),
586780bc790SAndy Lutomirski 			DMA_TO_DEVICE);
587780bc790SAndy Lutomirski 		if (vring_mapping_error(vq, addr))
588780bc790SAndy Lutomirski 			goto unmap_release;
589780bc790SAndy Lutomirski 
590fe4c3862SJason Wang 		virtqueue_add_desc_split(_vq, vq->split.vring.desc,
591fe4c3862SJason Wang 					 head, addr,
592fe4c3862SJason Wang 					 total_sg * sizeof(struct vring_desc),
59372b5e895SJason Wang 					 VRING_DESC_F_INDIRECT,
59472b5e895SJason Wang 					 false);
595780bc790SAndy Lutomirski 	}
596780bc790SAndy Lutomirski 
597780bc790SAndy Lutomirski 	/* We're using some buffers from the free list. */
598780bc790SAndy Lutomirski 	vq->vq.num_free -= descs_used;
599780bc790SAndy Lutomirski 
6000a8a69ddSRusty Russell 	/* Update free pointer */
601b25bd251SRusty Russell 	if (indirect)
60272b5e895SJason Wang 		vq->free_head = vq->split.desc_extra[head].next;
603b25bd251SRusty Russell 	else
6040a8a69ddSRusty Russell 		vq->free_head = i;
6050a8a69ddSRusty Russell 
606780bc790SAndy Lutomirski 	/* Store token and indirect buffer state. */
607cbeedb72STiwei Bie 	vq->split.desc_state[head].data = data;
608780bc790SAndy Lutomirski 	if (indirect)
609cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = desc;
61087646a34SJason Wang 	else
611cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = ctx;
6120a8a69ddSRusty Russell 
6130a8a69ddSRusty Russell 	/* Put entry in available array (but don't update avail->idx until they
6143b720b8cSRusty Russell 	 * do sync). */
615e593bf97STiwei Bie 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
616e593bf97STiwei Bie 	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
6170a8a69ddSRusty Russell 
618ee7cd898SRusty Russell 	/* Descriptors and available array need to be set before we expose the
619ee7cd898SRusty Russell 	 * new available array entries. */
620a9a0fef7SRusty Russell 	virtio_wmb(vq->weak_barriers);
621e593bf97STiwei Bie 	vq->split.avail_idx_shadow++;
622e593bf97STiwei Bie 	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
623e593bf97STiwei Bie 						vq->split.avail_idx_shadow);
624ee7cd898SRusty Russell 	vq->num_added++;
625ee7cd898SRusty Russell 
6265e05bf58STetsuo Handa 	pr_debug("Added buffer head %i to %p\n", head, vq);
6275e05bf58STetsuo Handa 	END_USE(vq);
6285e05bf58STetsuo Handa 
629ee7cd898SRusty Russell 	/* This is very unlikely, but theoretically possible.  Kick
630ee7cd898SRusty Russell 	 * just in case. */
631ee7cd898SRusty Russell 	if (unlikely(vq->num_added == (1 << 16) - 1))
632ee7cd898SRusty Russell 		virtqueue_kick(_vq);
633ee7cd898SRusty Russell 
63498e8c6bcSRusty Russell 	return 0;
635780bc790SAndy Lutomirski 
636780bc790SAndy Lutomirski unmap_release:
637780bc790SAndy Lutomirski 	err_idx = i;
638cf8f1696SMatthias Lange 
639cf8f1696SMatthias Lange 	if (indirect)
640cf8f1696SMatthias Lange 		i = 0;
641cf8f1696SMatthias Lange 	else
642780bc790SAndy Lutomirski 		i = head;
643780bc790SAndy Lutomirski 
644780bc790SAndy Lutomirski 	for (n = 0; n < total_sg; n++) {
645780bc790SAndy Lutomirski 		if (i == err_idx)
646780bc790SAndy Lutomirski 			break;
64772b5e895SJason Wang 		if (indirect) {
64872b5e895SJason Wang 			vring_unmap_one_split_indirect(vq, &desc[i]);
649cf8f1696SMatthias Lange 			i = virtio16_to_cpu(_vq->vdev, desc[i].next);
65072b5e895SJason Wang 		} else
65172b5e895SJason Wang 			i = vring_unmap_one_split(vq, i);
652780bc790SAndy Lutomirski 	}
653780bc790SAndy Lutomirski 
654780bc790SAndy Lutomirski 	if (indirect)
655780bc790SAndy Lutomirski 		kfree(desc);
656780bc790SAndy Lutomirski 
6573cc36f6eSMichael S. Tsirkin 	END_USE(vq);
658f7728002SHalil Pasic 	return -ENOMEM;
6590a8a69ddSRusty Russell }
66013816c76SRusty Russell 
661138fd251STiwei Bie static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
6620a8a69ddSRusty Russell {
6630a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
664a5c262c5SMichael S. Tsirkin 	u16 new, old;
66541f0377fSRusty Russell 	bool needs_kick;
66641f0377fSRusty Russell 
6670a8a69ddSRusty Russell 	START_USE(vq);
668a72caae2SJason Wang 	/* We need to expose available array entries before checking avail
669a72caae2SJason Wang 	 * event. */
670a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
6710a8a69ddSRusty Russell 
672e593bf97STiwei Bie 	old = vq->split.avail_idx_shadow - vq->num_added;
673e593bf97STiwei Bie 	new = vq->split.avail_idx_shadow;
6740a8a69ddSRusty Russell 	vq->num_added = 0;
6750a8a69ddSRusty Russell 
6764d6a105eSTiwei Bie 	LAST_ADD_TIME_CHECK(vq);
6774d6a105eSTiwei Bie 	LAST_ADD_TIME_INVALID(vq);
678e93300b1SRusty Russell 
67941f0377fSRusty Russell 	if (vq->event) {
680e593bf97STiwei Bie 		needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
681e593bf97STiwei Bie 					vring_avail_event(&vq->split.vring)),
68241f0377fSRusty Russell 					      new, old);
68341f0377fSRusty Russell 	} else {
684e593bf97STiwei Bie 		needs_kick = !(vq->split.vring.used->flags &
685e593bf97STiwei Bie 					cpu_to_virtio16(_vq->vdev,
686e593bf97STiwei Bie 						VRING_USED_F_NO_NOTIFY));
68741f0377fSRusty Russell 	}
6880a8a69ddSRusty Russell 	END_USE(vq);
68941f0377fSRusty Russell 	return needs_kick;
69041f0377fSRusty Russell }
691138fd251STiwei Bie 
692138fd251STiwei Bie static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
6935a08b04fSMichael S. Tsirkin 			     void **ctx)
6940a8a69ddSRusty Russell {
695780bc790SAndy Lutomirski 	unsigned int i, j;
696c60923cbSGonglei 	__virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
6970a8a69ddSRusty Russell 
6980a8a69ddSRusty Russell 	/* Clear data ptr. */
699cbeedb72STiwei Bie 	vq->split.desc_state[head].data = NULL;
7000a8a69ddSRusty Russell 
701780bc790SAndy Lutomirski 	/* Put back on free list: unmap first-level descriptors and find end */
7020a8a69ddSRusty Russell 	i = head;
7039fa29b9dSMark McLoughlin 
704e593bf97STiwei Bie 	while (vq->split.vring.desc[i].flags & nextflag) {
70572b5e895SJason Wang 		vring_unmap_one_split(vq, i);
70672b5e895SJason Wang 		i = vq->split.desc_extra[i].next;
70706ca287dSRusty Russell 		vq->vq.num_free++;
7080a8a69ddSRusty Russell 	}
7090a8a69ddSRusty Russell 
71072b5e895SJason Wang 	vring_unmap_one_split(vq, i);
71172b5e895SJason Wang 	vq->split.desc_extra[i].next = vq->free_head;
7120a8a69ddSRusty Russell 	vq->free_head = head;
713780bc790SAndy Lutomirski 
7140a8a69ddSRusty Russell 	/* Plus final descriptor */
71506ca287dSRusty Russell 	vq->vq.num_free++;
716780bc790SAndy Lutomirski 
7175a08b04fSMichael S. Tsirkin 	if (vq->indirect) {
718cbeedb72STiwei Bie 		struct vring_desc *indir_desc =
719cbeedb72STiwei Bie 				vq->split.desc_state[head].indir_desc;
7205a08b04fSMichael S. Tsirkin 		u32 len;
7215a08b04fSMichael S. Tsirkin 
7225a08b04fSMichael S. Tsirkin 		/* Free the indirect table, if any, now that it's unmapped. */
7235a08b04fSMichael S. Tsirkin 		if (!indir_desc)
7245a08b04fSMichael S. Tsirkin 			return;
7255a08b04fSMichael S. Tsirkin 
72672b5e895SJason Wang 		len = vq->split.desc_extra[head].len;
727780bc790SAndy Lutomirski 
72872b5e895SJason Wang 		BUG_ON(!(vq->split.desc_extra[head].flags &
72972b5e895SJason Wang 				VRING_DESC_F_INDIRECT));
730780bc790SAndy Lutomirski 		BUG_ON(len == 0 || len % sizeof(struct vring_desc));
731780bc790SAndy Lutomirski 
732780bc790SAndy Lutomirski 		for (j = 0; j < len / sizeof(struct vring_desc); j++)
73372b5e895SJason Wang 			vring_unmap_one_split_indirect(vq, &indir_desc[j]);
734780bc790SAndy Lutomirski 
7355a08b04fSMichael S. Tsirkin 		kfree(indir_desc);
736cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = NULL;
7375a08b04fSMichael S. Tsirkin 	} else if (ctx) {
738cbeedb72STiwei Bie 		*ctx = vq->split.desc_state[head].indir_desc;
739780bc790SAndy Lutomirski 	}
7400a8a69ddSRusty Russell }
7410a8a69ddSRusty Russell 
742138fd251STiwei Bie static inline bool more_used_split(const struct vring_virtqueue *vq)
7430a8a69ddSRusty Russell {
744e593bf97STiwei Bie 	return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
745e593bf97STiwei Bie 			vq->split.vring.used->idx);
7460a8a69ddSRusty Russell }
7470a8a69ddSRusty Russell 
748138fd251STiwei Bie static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
749138fd251STiwei Bie 					 unsigned int *len,
7505a08b04fSMichael S. Tsirkin 					 void **ctx)
7510a8a69ddSRusty Russell {
7520a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
7530a8a69ddSRusty Russell 	void *ret;
7540a8a69ddSRusty Russell 	unsigned int i;
7553b720b8cSRusty Russell 	u16 last_used;
7560a8a69ddSRusty Russell 
7570a8a69ddSRusty Russell 	START_USE(vq);
7580a8a69ddSRusty Russell 
7595ef82752SRusty Russell 	if (unlikely(vq->broken)) {
7605ef82752SRusty Russell 		END_USE(vq);
7615ef82752SRusty Russell 		return NULL;
7625ef82752SRusty Russell 	}
7635ef82752SRusty Russell 
764138fd251STiwei Bie 	if (!more_used_split(vq)) {
7650a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
7660a8a69ddSRusty Russell 		END_USE(vq);
7670a8a69ddSRusty Russell 		return NULL;
7680a8a69ddSRusty Russell 	}
7690a8a69ddSRusty Russell 
7702d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
771a9a0fef7SRusty Russell 	virtio_rmb(vq->weak_barriers);
7722d61ba95SMichael S. Tsirkin 
773e593bf97STiwei Bie 	last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
774e593bf97STiwei Bie 	i = virtio32_to_cpu(_vq->vdev,
775e593bf97STiwei Bie 			vq->split.vring.used->ring[last_used].id);
776e593bf97STiwei Bie 	*len = virtio32_to_cpu(_vq->vdev,
777e593bf97STiwei Bie 			vq->split.vring.used->ring[last_used].len);
7780a8a69ddSRusty Russell 
779e593bf97STiwei Bie 	if (unlikely(i >= vq->split.vring.num)) {
7800a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
7810a8a69ddSRusty Russell 		return NULL;
7820a8a69ddSRusty Russell 	}
783cbeedb72STiwei Bie 	if (unlikely(!vq->split.desc_state[i].data)) {
7840a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
7850a8a69ddSRusty Russell 		return NULL;
7860a8a69ddSRusty Russell 	}
7870a8a69ddSRusty Russell 
788138fd251STiwei Bie 	/* detach_buf_split clears data, so grab it now. */
789cbeedb72STiwei Bie 	ret = vq->split.desc_state[i].data;
790138fd251STiwei Bie 	detach_buf_split(vq, i, ctx);
7910a8a69ddSRusty Russell 	vq->last_used_idx++;
792a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
793a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
794a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
795e593bf97STiwei Bie 	if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
796788e5b3aSMichael S. Tsirkin 		virtio_store_mb(vq->weak_barriers,
797e593bf97STiwei Bie 				&vring_used_event(&vq->split.vring),
798788e5b3aSMichael S. Tsirkin 				cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
799a5c262c5SMichael S. Tsirkin 
8004d6a105eSTiwei Bie 	LAST_ADD_TIME_INVALID(vq);
801e93300b1SRusty Russell 
8020a8a69ddSRusty Russell 	END_USE(vq);
8030a8a69ddSRusty Russell 	return ret;
8040a8a69ddSRusty Russell }
805138fd251STiwei Bie 
806138fd251STiwei Bie static void virtqueue_disable_cb_split(struct virtqueue *_vq)
807138fd251STiwei Bie {
808138fd251STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
809138fd251STiwei Bie 
810e593bf97STiwei Bie 	if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
811e593bf97STiwei Bie 		vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
8128d622d21SMichael S. Tsirkin 		if (vq->event)
8138d622d21SMichael S. Tsirkin 			/* TODO: this is a hack. Figure out a cleaner value to write. */
8148d622d21SMichael S. Tsirkin 			vring_used_event(&vq->split.vring) = 0x0;
8158d622d21SMichael S. Tsirkin 		else
816e593bf97STiwei Bie 			vq->split.vring.avail->flags =
817e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
818e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
819138fd251STiwei Bie 	}
820138fd251STiwei Bie }
821138fd251STiwei Bie 
822138fd251STiwei Bie static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
823cc229884SMichael S. Tsirkin {
824cc229884SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
825cc229884SMichael S. Tsirkin 	u16 last_used_idx;
826cc229884SMichael S. Tsirkin 
827cc229884SMichael S. Tsirkin 	START_USE(vq);
828cc229884SMichael S. Tsirkin 
829cc229884SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
830cc229884SMichael S. Tsirkin 	 * more to do. */
831cc229884SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
832cc229884SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
833cc229884SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
834e593bf97STiwei Bie 	if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
835e593bf97STiwei Bie 		vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
8360ea1e4a6SLadi Prosek 		if (!vq->event)
837e593bf97STiwei Bie 			vq->split.vring.avail->flags =
838e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
839e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
840f277ec42SVenkatesh Srinivas 	}
841e593bf97STiwei Bie 	vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
842e593bf97STiwei Bie 			last_used_idx = vq->last_used_idx);
843cc229884SMichael S. Tsirkin 	END_USE(vq);
844cc229884SMichael S. Tsirkin 	return last_used_idx;
845cc229884SMichael S. Tsirkin }
846138fd251STiwei Bie 
847138fd251STiwei Bie static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx)
848138fd251STiwei Bie {
849138fd251STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
850138fd251STiwei Bie 
851138fd251STiwei Bie 	return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
852e593bf97STiwei Bie 			vq->split.vring.used->idx);
853138fd251STiwei Bie }
854138fd251STiwei Bie 
855138fd251STiwei Bie static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
8567ab358c2SMichael S. Tsirkin {
8577ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
8587ab358c2SMichael S. Tsirkin 	u16 bufs;
8597ab358c2SMichael S. Tsirkin 
8607ab358c2SMichael S. Tsirkin 	START_USE(vq);
8617ab358c2SMichael S. Tsirkin 
8627ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
8637ab358c2SMichael S. Tsirkin 	 * more to do. */
8647ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
8657ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
8660ea1e4a6SLadi Prosek 	 * entry. Always update the event index to keep code simple. */
867e593bf97STiwei Bie 	if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
868e593bf97STiwei Bie 		vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
8690ea1e4a6SLadi Prosek 		if (!vq->event)
870e593bf97STiwei Bie 			vq->split.vring.avail->flags =
871e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
872e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
873f277ec42SVenkatesh Srinivas 	}
8747ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
875e593bf97STiwei Bie 	bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
876788e5b3aSMichael S. Tsirkin 
877788e5b3aSMichael S. Tsirkin 	virtio_store_mb(vq->weak_barriers,
878e593bf97STiwei Bie 			&vring_used_event(&vq->split.vring),
879788e5b3aSMichael S. Tsirkin 			cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
880788e5b3aSMichael S. Tsirkin 
881e593bf97STiwei Bie 	if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
882e593bf97STiwei Bie 					- vq->last_used_idx) > bufs)) {
8837ab358c2SMichael S. Tsirkin 		END_USE(vq);
8847ab358c2SMichael S. Tsirkin 		return false;
8857ab358c2SMichael S. Tsirkin 	}
8867ab358c2SMichael S. Tsirkin 
8877ab358c2SMichael S. Tsirkin 	END_USE(vq);
8887ab358c2SMichael S. Tsirkin 	return true;
8897ab358c2SMichael S. Tsirkin }
8907ab358c2SMichael S. Tsirkin 
891138fd251STiwei Bie static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
892c021eac4SShirley Ma {
893c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
894c021eac4SShirley Ma 	unsigned int i;
895c021eac4SShirley Ma 	void *buf;
896c021eac4SShirley Ma 
897c021eac4SShirley Ma 	START_USE(vq);
898c021eac4SShirley Ma 
899e593bf97STiwei Bie 	for (i = 0; i < vq->split.vring.num; i++) {
900cbeedb72STiwei Bie 		if (!vq->split.desc_state[i].data)
901c021eac4SShirley Ma 			continue;
902138fd251STiwei Bie 		/* detach_buf_split clears data, so grab it now. */
903cbeedb72STiwei Bie 		buf = vq->split.desc_state[i].data;
904138fd251STiwei Bie 		detach_buf_split(vq, i, NULL);
905e593bf97STiwei Bie 		vq->split.avail_idx_shadow--;
906e593bf97STiwei Bie 		vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
907e593bf97STiwei Bie 				vq->split.avail_idx_shadow);
908c021eac4SShirley Ma 		END_USE(vq);
909c021eac4SShirley Ma 		return buf;
910c021eac4SShirley Ma 	}
911c021eac4SShirley Ma 	/* That should have freed everything. */
912e593bf97STiwei Bie 	BUG_ON(vq->vq.num_free != vq->split.vring.num);
913c021eac4SShirley Ma 
914c021eac4SShirley Ma 	END_USE(vq);
915c021eac4SShirley Ma 	return NULL;
916c021eac4SShirley Ma }
917138fd251STiwei Bie 
918d79dca75STiwei Bie static struct virtqueue *vring_create_virtqueue_split(
919d79dca75STiwei Bie 	unsigned int index,
920d79dca75STiwei Bie 	unsigned int num,
921d79dca75STiwei Bie 	unsigned int vring_align,
922d79dca75STiwei Bie 	struct virtio_device *vdev,
923d79dca75STiwei Bie 	bool weak_barriers,
924d79dca75STiwei Bie 	bool may_reduce_num,
925d79dca75STiwei Bie 	bool context,
926d79dca75STiwei Bie 	bool (*notify)(struct virtqueue *),
927d79dca75STiwei Bie 	void (*callback)(struct virtqueue *),
928d79dca75STiwei Bie 	const char *name)
929d79dca75STiwei Bie {
930d79dca75STiwei Bie 	struct virtqueue *vq;
931d79dca75STiwei Bie 	void *queue = NULL;
932d79dca75STiwei Bie 	dma_addr_t dma_addr;
933d79dca75STiwei Bie 	size_t queue_size_in_bytes;
934d79dca75STiwei Bie 	struct vring vring;
935d79dca75STiwei Bie 
936d79dca75STiwei Bie 	/* We assume num is a power of 2. */
937d79dca75STiwei Bie 	if (num & (num - 1)) {
938d79dca75STiwei Bie 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
939d79dca75STiwei Bie 		return NULL;
940d79dca75STiwei Bie 	}
941d79dca75STiwei Bie 
942d79dca75STiwei Bie 	/* TODO: allocate each queue chunk individually */
943d79dca75STiwei Bie 	for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
944d79dca75STiwei Bie 		queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
945d79dca75STiwei Bie 					  &dma_addr,
946d79dca75STiwei Bie 					  GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
947d79dca75STiwei Bie 		if (queue)
948d79dca75STiwei Bie 			break;
949cf94db21SCornelia Huck 		if (!may_reduce_num)
950cf94db21SCornelia Huck 			return NULL;
951d79dca75STiwei Bie 	}
952d79dca75STiwei Bie 
953d79dca75STiwei Bie 	if (!num)
954d79dca75STiwei Bie 		return NULL;
955d79dca75STiwei Bie 
956d79dca75STiwei Bie 	if (!queue) {
957d79dca75STiwei Bie 		/* Try to get a single page. You are my only hope! */
958d79dca75STiwei Bie 		queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
959d79dca75STiwei Bie 					  &dma_addr, GFP_KERNEL|__GFP_ZERO);
960d79dca75STiwei Bie 	}
961d79dca75STiwei Bie 	if (!queue)
962d79dca75STiwei Bie 		return NULL;
963d79dca75STiwei Bie 
964d79dca75STiwei Bie 	queue_size_in_bytes = vring_size(num, vring_align);
965d79dca75STiwei Bie 	vring_init(&vring, num, queue, vring_align);
966d79dca75STiwei Bie 
967d79dca75STiwei Bie 	vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
968d79dca75STiwei Bie 				   notify, callback, name);
969d79dca75STiwei Bie 	if (!vq) {
970d79dca75STiwei Bie 		vring_free_queue(vdev, queue_size_in_bytes, queue,
971d79dca75STiwei Bie 				 dma_addr);
972d79dca75STiwei Bie 		return NULL;
973d79dca75STiwei Bie 	}
974d79dca75STiwei Bie 
975d79dca75STiwei Bie 	to_vvq(vq)->split.queue_dma_addr = dma_addr;
976d79dca75STiwei Bie 	to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes;
977d79dca75STiwei Bie 	to_vvq(vq)->we_own_ring = true;
978d79dca75STiwei Bie 
979d79dca75STiwei Bie 	return vq;
980d79dca75STiwei Bie }
981d79dca75STiwei Bie 
982e6f633e5STiwei Bie 
983e6f633e5STiwei Bie /*
9841ce9e605STiwei Bie  * Packed ring specific functions - *_packed().
9851ce9e605STiwei Bie  */
9861ce9e605STiwei Bie 
9871ce9e605STiwei Bie static void vring_unmap_state_packed(const struct vring_virtqueue *vq,
9881f28750fSJason Wang 				     struct vring_desc_extra *state)
9891ce9e605STiwei Bie {
9901ce9e605STiwei Bie 	u16 flags;
9911ce9e605STiwei Bie 
9921ce9e605STiwei Bie 	if (!vq->use_dma_api)
9931ce9e605STiwei Bie 		return;
9941ce9e605STiwei Bie 
9951ce9e605STiwei Bie 	flags = state->flags;
9961ce9e605STiwei Bie 
9971ce9e605STiwei Bie 	if (flags & VRING_DESC_F_INDIRECT) {
9981ce9e605STiwei Bie 		dma_unmap_single(vring_dma_dev(vq),
9991ce9e605STiwei Bie 				 state->addr, state->len,
10001ce9e605STiwei Bie 				 (flags & VRING_DESC_F_WRITE) ?
10011ce9e605STiwei Bie 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
10021ce9e605STiwei Bie 	} else {
10031ce9e605STiwei Bie 		dma_unmap_page(vring_dma_dev(vq),
10041ce9e605STiwei Bie 			       state->addr, state->len,
10051ce9e605STiwei Bie 			       (flags & VRING_DESC_F_WRITE) ?
10061ce9e605STiwei Bie 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
10071ce9e605STiwei Bie 	}
10081ce9e605STiwei Bie }
10091ce9e605STiwei Bie 
10101ce9e605STiwei Bie static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
10111ce9e605STiwei Bie 				   struct vring_packed_desc *desc)
10121ce9e605STiwei Bie {
10131ce9e605STiwei Bie 	u16 flags;
10141ce9e605STiwei Bie 
10151ce9e605STiwei Bie 	if (!vq->use_dma_api)
10161ce9e605STiwei Bie 		return;
10171ce9e605STiwei Bie 
10181ce9e605STiwei Bie 	flags = le16_to_cpu(desc->flags);
10191ce9e605STiwei Bie 
10201ce9e605STiwei Bie 	if (flags & VRING_DESC_F_INDIRECT) {
10211ce9e605STiwei Bie 		dma_unmap_single(vring_dma_dev(vq),
10221ce9e605STiwei Bie 				 le64_to_cpu(desc->addr),
10231ce9e605STiwei Bie 				 le32_to_cpu(desc->len),
10241ce9e605STiwei Bie 				 (flags & VRING_DESC_F_WRITE) ?
10251ce9e605STiwei Bie 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
10261ce9e605STiwei Bie 	} else {
10271ce9e605STiwei Bie 		dma_unmap_page(vring_dma_dev(vq),
10281ce9e605STiwei Bie 			       le64_to_cpu(desc->addr),
10291ce9e605STiwei Bie 			       le32_to_cpu(desc->len),
10301ce9e605STiwei Bie 			       (flags & VRING_DESC_F_WRITE) ?
10311ce9e605STiwei Bie 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
10321ce9e605STiwei Bie 	}
10331ce9e605STiwei Bie }
10341ce9e605STiwei Bie 
10351ce9e605STiwei Bie static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
10361ce9e605STiwei Bie 						       gfp_t gfp)
10371ce9e605STiwei Bie {
10381ce9e605STiwei Bie 	struct vring_packed_desc *desc;
10391ce9e605STiwei Bie 
10401ce9e605STiwei Bie 	/*
10411ce9e605STiwei Bie 	 * We require lowmem mappings for the descriptors because
10421ce9e605STiwei Bie 	 * otherwise virt_to_phys will give us bogus addresses in the
10431ce9e605STiwei Bie 	 * virtqueue.
10441ce9e605STiwei Bie 	 */
10451ce9e605STiwei Bie 	gfp &= ~__GFP_HIGHMEM;
10461ce9e605STiwei Bie 
10471ce9e605STiwei Bie 	desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
10481ce9e605STiwei Bie 
10491ce9e605STiwei Bie 	return desc;
10501ce9e605STiwei Bie }
10511ce9e605STiwei Bie 
10521ce9e605STiwei Bie static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
10531ce9e605STiwei Bie 					 struct scatterlist *sgs[],
10541ce9e605STiwei Bie 					 unsigned int total_sg,
10551ce9e605STiwei Bie 					 unsigned int out_sgs,
10561ce9e605STiwei Bie 					 unsigned int in_sgs,
10571ce9e605STiwei Bie 					 void *data,
10581ce9e605STiwei Bie 					 gfp_t gfp)
10591ce9e605STiwei Bie {
10601ce9e605STiwei Bie 	struct vring_packed_desc *desc;
10611ce9e605STiwei Bie 	struct scatterlist *sg;
10621ce9e605STiwei Bie 	unsigned int i, n, err_idx;
10631ce9e605STiwei Bie 	u16 head, id;
10641ce9e605STiwei Bie 	dma_addr_t addr;
10651ce9e605STiwei Bie 
10661ce9e605STiwei Bie 	head = vq->packed.next_avail_idx;
10671ce9e605STiwei Bie 	desc = alloc_indirect_packed(total_sg, gfp);
1068fc6d70f4SXuan Zhuo 	if (!desc)
1069fc6d70f4SXuan Zhuo 		return -ENOMEM;
10701ce9e605STiwei Bie 
10711ce9e605STiwei Bie 	if (unlikely(vq->vq.num_free < 1)) {
10721ce9e605STiwei Bie 		pr_debug("Can't add buf len 1 - avail = 0\n");
1073df0bfe75SYueHaibing 		kfree(desc);
10741ce9e605STiwei Bie 		END_USE(vq);
10751ce9e605STiwei Bie 		return -ENOSPC;
10761ce9e605STiwei Bie 	}
10771ce9e605STiwei Bie 
10781ce9e605STiwei Bie 	i = 0;
10791ce9e605STiwei Bie 	id = vq->free_head;
10801ce9e605STiwei Bie 	BUG_ON(id == vq->packed.vring.num);
10811ce9e605STiwei Bie 
10821ce9e605STiwei Bie 	for (n = 0; n < out_sgs + in_sgs; n++) {
10831ce9e605STiwei Bie 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
10841ce9e605STiwei Bie 			addr = vring_map_one_sg(vq, sg, n < out_sgs ?
10851ce9e605STiwei Bie 					DMA_TO_DEVICE : DMA_FROM_DEVICE);
10861ce9e605STiwei Bie 			if (vring_mapping_error(vq, addr))
10871ce9e605STiwei Bie 				goto unmap_release;
10881ce9e605STiwei Bie 
10891ce9e605STiwei Bie 			desc[i].flags = cpu_to_le16(n < out_sgs ?
10901ce9e605STiwei Bie 						0 : VRING_DESC_F_WRITE);
10911ce9e605STiwei Bie 			desc[i].addr = cpu_to_le64(addr);
10921ce9e605STiwei Bie 			desc[i].len = cpu_to_le32(sg->length);
10931ce9e605STiwei Bie 			i++;
10941ce9e605STiwei Bie 		}
10951ce9e605STiwei Bie 	}
10961ce9e605STiwei Bie 
10971ce9e605STiwei Bie 	/* Now that the indirect table is filled in, map it. */
10981ce9e605STiwei Bie 	addr = vring_map_single(vq, desc,
10991ce9e605STiwei Bie 			total_sg * sizeof(struct vring_packed_desc),
11001ce9e605STiwei Bie 			DMA_TO_DEVICE);
11011ce9e605STiwei Bie 	if (vring_mapping_error(vq, addr))
11021ce9e605STiwei Bie 		goto unmap_release;
11031ce9e605STiwei Bie 
11041ce9e605STiwei Bie 	vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
11051ce9e605STiwei Bie 	vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
11061ce9e605STiwei Bie 				sizeof(struct vring_packed_desc));
11071ce9e605STiwei Bie 	vq->packed.vring.desc[head].id = cpu_to_le16(id);
11081ce9e605STiwei Bie 
11091ce9e605STiwei Bie 	if (vq->use_dma_api) {
11101ce9e605STiwei Bie 		vq->packed.desc_extra[id].addr = addr;
11111ce9e605STiwei Bie 		vq->packed.desc_extra[id].len = total_sg *
11121ce9e605STiwei Bie 				sizeof(struct vring_packed_desc);
11131ce9e605STiwei Bie 		vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
11141ce9e605STiwei Bie 						  vq->packed.avail_used_flags;
11151ce9e605STiwei Bie 	}
11161ce9e605STiwei Bie 
11171ce9e605STiwei Bie 	/*
11181ce9e605STiwei Bie 	 * A driver MUST NOT make the first descriptor in the list
11191ce9e605STiwei Bie 	 * available before all subsequent descriptors comprising
11201ce9e605STiwei Bie 	 * the list are made available.
11211ce9e605STiwei Bie 	 */
11221ce9e605STiwei Bie 	virtio_wmb(vq->weak_barriers);
11231ce9e605STiwei Bie 	vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
11241ce9e605STiwei Bie 						vq->packed.avail_used_flags);
11251ce9e605STiwei Bie 
11261ce9e605STiwei Bie 	/* We're using some buffers from the free list. */
11271ce9e605STiwei Bie 	vq->vq.num_free -= 1;
11281ce9e605STiwei Bie 
11291ce9e605STiwei Bie 	/* Update free pointer */
11301ce9e605STiwei Bie 	n = head + 1;
11311ce9e605STiwei Bie 	if (n >= vq->packed.vring.num) {
11321ce9e605STiwei Bie 		n = 0;
11331ce9e605STiwei Bie 		vq->packed.avail_wrap_counter ^= 1;
11341ce9e605STiwei Bie 		vq->packed.avail_used_flags ^=
11351ce9e605STiwei Bie 				1 << VRING_PACKED_DESC_F_AVAIL |
11361ce9e605STiwei Bie 				1 << VRING_PACKED_DESC_F_USED;
11371ce9e605STiwei Bie 	}
11381ce9e605STiwei Bie 	vq->packed.next_avail_idx = n;
1139aeef9b47SJason Wang 	vq->free_head = vq->packed.desc_extra[id].next;
11401ce9e605STiwei Bie 
11411ce9e605STiwei Bie 	/* Store token and indirect buffer state. */
11421ce9e605STiwei Bie 	vq->packed.desc_state[id].num = 1;
11431ce9e605STiwei Bie 	vq->packed.desc_state[id].data = data;
11441ce9e605STiwei Bie 	vq->packed.desc_state[id].indir_desc = desc;
11451ce9e605STiwei Bie 	vq->packed.desc_state[id].last = id;
11461ce9e605STiwei Bie 
11471ce9e605STiwei Bie 	vq->num_added += 1;
11481ce9e605STiwei Bie 
11491ce9e605STiwei Bie 	pr_debug("Added buffer head %i to %p\n", head, vq);
11501ce9e605STiwei Bie 	END_USE(vq);
11511ce9e605STiwei Bie 
11521ce9e605STiwei Bie 	return 0;
11531ce9e605STiwei Bie 
11541ce9e605STiwei Bie unmap_release:
11551ce9e605STiwei Bie 	err_idx = i;
11561ce9e605STiwei Bie 
11571ce9e605STiwei Bie 	for (i = 0; i < err_idx; i++)
11581ce9e605STiwei Bie 		vring_unmap_desc_packed(vq, &desc[i]);
11591ce9e605STiwei Bie 
11601ce9e605STiwei Bie 	kfree(desc);
11611ce9e605STiwei Bie 
11621ce9e605STiwei Bie 	END_USE(vq);
1163f7728002SHalil Pasic 	return -ENOMEM;
11641ce9e605STiwei Bie }
11651ce9e605STiwei Bie 
11661ce9e605STiwei Bie static inline int virtqueue_add_packed(struct virtqueue *_vq,
11671ce9e605STiwei Bie 				       struct scatterlist *sgs[],
11681ce9e605STiwei Bie 				       unsigned int total_sg,
11691ce9e605STiwei Bie 				       unsigned int out_sgs,
11701ce9e605STiwei Bie 				       unsigned int in_sgs,
11711ce9e605STiwei Bie 				       void *data,
11721ce9e605STiwei Bie 				       void *ctx,
11731ce9e605STiwei Bie 				       gfp_t gfp)
11741ce9e605STiwei Bie {
11751ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
11761ce9e605STiwei Bie 	struct vring_packed_desc *desc;
11771ce9e605STiwei Bie 	struct scatterlist *sg;
11781ce9e605STiwei Bie 	unsigned int i, n, c, descs_used, err_idx;
11793f649ab7SKees Cook 	__le16 head_flags, flags;
11803f649ab7SKees Cook 	u16 head, id, prev, curr, avail_used_flags;
1181fc6d70f4SXuan Zhuo 	int err;
11821ce9e605STiwei Bie 
11831ce9e605STiwei Bie 	START_USE(vq);
11841ce9e605STiwei Bie 
11851ce9e605STiwei Bie 	BUG_ON(data == NULL);
11861ce9e605STiwei Bie 	BUG_ON(ctx && vq->indirect);
11871ce9e605STiwei Bie 
11881ce9e605STiwei Bie 	if (unlikely(vq->broken)) {
11891ce9e605STiwei Bie 		END_USE(vq);
11901ce9e605STiwei Bie 		return -EIO;
11911ce9e605STiwei Bie 	}
11921ce9e605STiwei Bie 
11931ce9e605STiwei Bie 	LAST_ADD_TIME_UPDATE(vq);
11941ce9e605STiwei Bie 
11951ce9e605STiwei Bie 	BUG_ON(total_sg == 0);
11961ce9e605STiwei Bie 
1197fc6d70f4SXuan Zhuo 	if (virtqueue_use_indirect(_vq, total_sg)) {
1198fc6d70f4SXuan Zhuo 		err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
1199fc6d70f4SXuan Zhuo 						    in_sgs, data, gfp);
1200fc6d70f4SXuan Zhuo 		if (err != -ENOMEM)
1201fc6d70f4SXuan Zhuo 			return err;
1202fc6d70f4SXuan Zhuo 
1203fc6d70f4SXuan Zhuo 		/* fall back on direct */
1204fc6d70f4SXuan Zhuo 	}
12051ce9e605STiwei Bie 
12061ce9e605STiwei Bie 	head = vq->packed.next_avail_idx;
12071ce9e605STiwei Bie 	avail_used_flags = vq->packed.avail_used_flags;
12081ce9e605STiwei Bie 
12091ce9e605STiwei Bie 	WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
12101ce9e605STiwei Bie 
12111ce9e605STiwei Bie 	desc = vq->packed.vring.desc;
12121ce9e605STiwei Bie 	i = head;
12131ce9e605STiwei Bie 	descs_used = total_sg;
12141ce9e605STiwei Bie 
12151ce9e605STiwei Bie 	if (unlikely(vq->vq.num_free < descs_used)) {
12161ce9e605STiwei Bie 		pr_debug("Can't add buf len %i - avail = %i\n",
12171ce9e605STiwei Bie 			 descs_used, vq->vq.num_free);
12181ce9e605STiwei Bie 		END_USE(vq);
12191ce9e605STiwei Bie 		return -ENOSPC;
12201ce9e605STiwei Bie 	}
12211ce9e605STiwei Bie 
12221ce9e605STiwei Bie 	id = vq->free_head;
12231ce9e605STiwei Bie 	BUG_ON(id == vq->packed.vring.num);
12241ce9e605STiwei Bie 
12251ce9e605STiwei Bie 	curr = id;
12261ce9e605STiwei Bie 	c = 0;
12271ce9e605STiwei Bie 	for (n = 0; n < out_sgs + in_sgs; n++) {
12281ce9e605STiwei Bie 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
12291ce9e605STiwei Bie 			dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ?
12301ce9e605STiwei Bie 					DMA_TO_DEVICE : DMA_FROM_DEVICE);
12311ce9e605STiwei Bie 			if (vring_mapping_error(vq, addr))
12321ce9e605STiwei Bie 				goto unmap_release;
12331ce9e605STiwei Bie 
12341ce9e605STiwei Bie 			flags = cpu_to_le16(vq->packed.avail_used_flags |
12351ce9e605STiwei Bie 				    (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
12361ce9e605STiwei Bie 				    (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
12371ce9e605STiwei Bie 			if (i == head)
12381ce9e605STiwei Bie 				head_flags = flags;
12391ce9e605STiwei Bie 			else
12401ce9e605STiwei Bie 				desc[i].flags = flags;
12411ce9e605STiwei Bie 
12421ce9e605STiwei Bie 			desc[i].addr = cpu_to_le64(addr);
12431ce9e605STiwei Bie 			desc[i].len = cpu_to_le32(sg->length);
12441ce9e605STiwei Bie 			desc[i].id = cpu_to_le16(id);
12451ce9e605STiwei Bie 
12461ce9e605STiwei Bie 			if (unlikely(vq->use_dma_api)) {
12471ce9e605STiwei Bie 				vq->packed.desc_extra[curr].addr = addr;
12481ce9e605STiwei Bie 				vq->packed.desc_extra[curr].len = sg->length;
12491ce9e605STiwei Bie 				vq->packed.desc_extra[curr].flags =
12501ce9e605STiwei Bie 					le16_to_cpu(flags);
12511ce9e605STiwei Bie 			}
12521ce9e605STiwei Bie 			prev = curr;
1253aeef9b47SJason Wang 			curr = vq->packed.desc_extra[curr].next;
12541ce9e605STiwei Bie 
12551ce9e605STiwei Bie 			if ((unlikely(++i >= vq->packed.vring.num))) {
12561ce9e605STiwei Bie 				i = 0;
12571ce9e605STiwei Bie 				vq->packed.avail_used_flags ^=
12581ce9e605STiwei Bie 					1 << VRING_PACKED_DESC_F_AVAIL |
12591ce9e605STiwei Bie 					1 << VRING_PACKED_DESC_F_USED;
12601ce9e605STiwei Bie 			}
12611ce9e605STiwei Bie 		}
12621ce9e605STiwei Bie 	}
12631ce9e605STiwei Bie 
12641ce9e605STiwei Bie 	if (i < head)
12651ce9e605STiwei Bie 		vq->packed.avail_wrap_counter ^= 1;
12661ce9e605STiwei Bie 
12671ce9e605STiwei Bie 	/* We're using some buffers from the free list. */
12681ce9e605STiwei Bie 	vq->vq.num_free -= descs_used;
12691ce9e605STiwei Bie 
12701ce9e605STiwei Bie 	/* Update free pointer */
12711ce9e605STiwei Bie 	vq->packed.next_avail_idx = i;
12721ce9e605STiwei Bie 	vq->free_head = curr;
12731ce9e605STiwei Bie 
12741ce9e605STiwei Bie 	/* Store token. */
12751ce9e605STiwei Bie 	vq->packed.desc_state[id].num = descs_used;
12761ce9e605STiwei Bie 	vq->packed.desc_state[id].data = data;
12771ce9e605STiwei Bie 	vq->packed.desc_state[id].indir_desc = ctx;
12781ce9e605STiwei Bie 	vq->packed.desc_state[id].last = prev;
12791ce9e605STiwei Bie 
12801ce9e605STiwei Bie 	/*
12811ce9e605STiwei Bie 	 * A driver MUST NOT make the first descriptor in the list
12821ce9e605STiwei Bie 	 * available before all subsequent descriptors comprising
12831ce9e605STiwei Bie 	 * the list are made available.
12841ce9e605STiwei Bie 	 */
12851ce9e605STiwei Bie 	virtio_wmb(vq->weak_barriers);
12861ce9e605STiwei Bie 	vq->packed.vring.desc[head].flags = head_flags;
12871ce9e605STiwei Bie 	vq->num_added += descs_used;
12881ce9e605STiwei Bie 
12891ce9e605STiwei Bie 	pr_debug("Added buffer head %i to %p\n", head, vq);
12901ce9e605STiwei Bie 	END_USE(vq);
12911ce9e605STiwei Bie 
12921ce9e605STiwei Bie 	return 0;
12931ce9e605STiwei Bie 
12941ce9e605STiwei Bie unmap_release:
12951ce9e605STiwei Bie 	err_idx = i;
12961ce9e605STiwei Bie 	i = head;
129744593865SJason Wang 	curr = vq->free_head;
12981ce9e605STiwei Bie 
12991ce9e605STiwei Bie 	vq->packed.avail_used_flags = avail_used_flags;
13001ce9e605STiwei Bie 
13011ce9e605STiwei Bie 	for (n = 0; n < total_sg; n++) {
13021ce9e605STiwei Bie 		if (i == err_idx)
13031ce9e605STiwei Bie 			break;
130444593865SJason Wang 		vring_unmap_state_packed(vq,
130544593865SJason Wang 					 &vq->packed.desc_extra[curr]);
130644593865SJason Wang 		curr = vq->packed.desc_extra[curr].next;
13071ce9e605STiwei Bie 		i++;
13081ce9e605STiwei Bie 		if (i >= vq->packed.vring.num)
13091ce9e605STiwei Bie 			i = 0;
13101ce9e605STiwei Bie 	}
13111ce9e605STiwei Bie 
13121ce9e605STiwei Bie 	END_USE(vq);
13131ce9e605STiwei Bie 	return -EIO;
13141ce9e605STiwei Bie }
13151ce9e605STiwei Bie 
13161ce9e605STiwei Bie static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
13171ce9e605STiwei Bie {
13181ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1319f51f9826STiwei Bie 	u16 new, old, off_wrap, flags, wrap_counter, event_idx;
13201ce9e605STiwei Bie 	bool needs_kick;
13211ce9e605STiwei Bie 	union {
13221ce9e605STiwei Bie 		struct {
13231ce9e605STiwei Bie 			__le16 off_wrap;
13241ce9e605STiwei Bie 			__le16 flags;
13251ce9e605STiwei Bie 		};
13261ce9e605STiwei Bie 		u32 u32;
13271ce9e605STiwei Bie 	} snapshot;
13281ce9e605STiwei Bie 
13291ce9e605STiwei Bie 	START_USE(vq);
13301ce9e605STiwei Bie 
13311ce9e605STiwei Bie 	/*
13321ce9e605STiwei Bie 	 * We need to expose the new flags value before checking notification
13331ce9e605STiwei Bie 	 * suppressions.
13341ce9e605STiwei Bie 	 */
13351ce9e605STiwei Bie 	virtio_mb(vq->weak_barriers);
13361ce9e605STiwei Bie 
1337f51f9826STiwei Bie 	old = vq->packed.next_avail_idx - vq->num_added;
1338f51f9826STiwei Bie 	new = vq->packed.next_avail_idx;
13391ce9e605STiwei Bie 	vq->num_added = 0;
13401ce9e605STiwei Bie 
13411ce9e605STiwei Bie 	snapshot.u32 = *(u32 *)vq->packed.vring.device;
13421ce9e605STiwei Bie 	flags = le16_to_cpu(snapshot.flags);
13431ce9e605STiwei Bie 
13441ce9e605STiwei Bie 	LAST_ADD_TIME_CHECK(vq);
13451ce9e605STiwei Bie 	LAST_ADD_TIME_INVALID(vq);
13461ce9e605STiwei Bie 
1347f51f9826STiwei Bie 	if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
13481ce9e605STiwei Bie 		needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1349f51f9826STiwei Bie 		goto out;
1350f51f9826STiwei Bie 	}
1351f51f9826STiwei Bie 
1352f51f9826STiwei Bie 	off_wrap = le16_to_cpu(snapshot.off_wrap);
1353f51f9826STiwei Bie 
1354f51f9826STiwei Bie 	wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1355f51f9826STiwei Bie 	event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1356f51f9826STiwei Bie 	if (wrap_counter != vq->packed.avail_wrap_counter)
1357f51f9826STiwei Bie 		event_idx -= vq->packed.vring.num;
1358f51f9826STiwei Bie 
1359f51f9826STiwei Bie 	needs_kick = vring_need_event(event_idx, new, old);
1360f51f9826STiwei Bie out:
13611ce9e605STiwei Bie 	END_USE(vq);
13621ce9e605STiwei Bie 	return needs_kick;
13631ce9e605STiwei Bie }
13641ce9e605STiwei Bie 
13651ce9e605STiwei Bie static void detach_buf_packed(struct vring_virtqueue *vq,
13661ce9e605STiwei Bie 			      unsigned int id, void **ctx)
13671ce9e605STiwei Bie {
13681ce9e605STiwei Bie 	struct vring_desc_state_packed *state = NULL;
13691ce9e605STiwei Bie 	struct vring_packed_desc *desc;
13701ce9e605STiwei Bie 	unsigned int i, curr;
13711ce9e605STiwei Bie 
13721ce9e605STiwei Bie 	state = &vq->packed.desc_state[id];
13731ce9e605STiwei Bie 
13741ce9e605STiwei Bie 	/* Clear data ptr. */
13751ce9e605STiwei Bie 	state->data = NULL;
13761ce9e605STiwei Bie 
1377aeef9b47SJason Wang 	vq->packed.desc_extra[state->last].next = vq->free_head;
13781ce9e605STiwei Bie 	vq->free_head = id;
13791ce9e605STiwei Bie 	vq->vq.num_free += state->num;
13801ce9e605STiwei Bie 
13811ce9e605STiwei Bie 	if (unlikely(vq->use_dma_api)) {
13821ce9e605STiwei Bie 		curr = id;
13831ce9e605STiwei Bie 		for (i = 0; i < state->num; i++) {
13841ce9e605STiwei Bie 			vring_unmap_state_packed(vq,
13851ce9e605STiwei Bie 				&vq->packed.desc_extra[curr]);
1386aeef9b47SJason Wang 			curr = vq->packed.desc_extra[curr].next;
13871ce9e605STiwei Bie 		}
13881ce9e605STiwei Bie 	}
13891ce9e605STiwei Bie 
13901ce9e605STiwei Bie 	if (vq->indirect) {
13911ce9e605STiwei Bie 		u32 len;
13921ce9e605STiwei Bie 
13931ce9e605STiwei Bie 		/* Free the indirect table, if any, now that it's unmapped. */
13941ce9e605STiwei Bie 		desc = state->indir_desc;
13951ce9e605STiwei Bie 		if (!desc)
13961ce9e605STiwei Bie 			return;
13971ce9e605STiwei Bie 
13981ce9e605STiwei Bie 		if (vq->use_dma_api) {
13991ce9e605STiwei Bie 			len = vq->packed.desc_extra[id].len;
14001ce9e605STiwei Bie 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
14011ce9e605STiwei Bie 					i++)
14021ce9e605STiwei Bie 				vring_unmap_desc_packed(vq, &desc[i]);
14031ce9e605STiwei Bie 		}
14041ce9e605STiwei Bie 		kfree(desc);
14051ce9e605STiwei Bie 		state->indir_desc = NULL;
14061ce9e605STiwei Bie 	} else if (ctx) {
14071ce9e605STiwei Bie 		*ctx = state->indir_desc;
14081ce9e605STiwei Bie 	}
14091ce9e605STiwei Bie }
14101ce9e605STiwei Bie 
14111ce9e605STiwei Bie static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
14121ce9e605STiwei Bie 				       u16 idx, bool used_wrap_counter)
14131ce9e605STiwei Bie {
14141ce9e605STiwei Bie 	bool avail, used;
14151ce9e605STiwei Bie 	u16 flags;
14161ce9e605STiwei Bie 
14171ce9e605STiwei Bie 	flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
14181ce9e605STiwei Bie 	avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
14191ce9e605STiwei Bie 	used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
14201ce9e605STiwei Bie 
14211ce9e605STiwei Bie 	return avail == used && used == used_wrap_counter;
14221ce9e605STiwei Bie }
14231ce9e605STiwei Bie 
14241ce9e605STiwei Bie static inline bool more_used_packed(const struct vring_virtqueue *vq)
14251ce9e605STiwei Bie {
14261ce9e605STiwei Bie 	return is_used_desc_packed(vq, vq->last_used_idx,
14271ce9e605STiwei Bie 			vq->packed.used_wrap_counter);
14281ce9e605STiwei Bie }
14291ce9e605STiwei Bie 
14301ce9e605STiwei Bie static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
14311ce9e605STiwei Bie 					  unsigned int *len,
14321ce9e605STiwei Bie 					  void **ctx)
14331ce9e605STiwei Bie {
14341ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14351ce9e605STiwei Bie 	u16 last_used, id;
14361ce9e605STiwei Bie 	void *ret;
14371ce9e605STiwei Bie 
14381ce9e605STiwei Bie 	START_USE(vq);
14391ce9e605STiwei Bie 
14401ce9e605STiwei Bie 	if (unlikely(vq->broken)) {
14411ce9e605STiwei Bie 		END_USE(vq);
14421ce9e605STiwei Bie 		return NULL;
14431ce9e605STiwei Bie 	}
14441ce9e605STiwei Bie 
14451ce9e605STiwei Bie 	if (!more_used_packed(vq)) {
14461ce9e605STiwei Bie 		pr_debug("No more buffers in queue\n");
14471ce9e605STiwei Bie 		END_USE(vq);
14481ce9e605STiwei Bie 		return NULL;
14491ce9e605STiwei Bie 	}
14501ce9e605STiwei Bie 
14511ce9e605STiwei Bie 	/* Only get used elements after they have been exposed by host. */
14521ce9e605STiwei Bie 	virtio_rmb(vq->weak_barriers);
14531ce9e605STiwei Bie 
14541ce9e605STiwei Bie 	last_used = vq->last_used_idx;
14551ce9e605STiwei Bie 	id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
14561ce9e605STiwei Bie 	*len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
14571ce9e605STiwei Bie 
14581ce9e605STiwei Bie 	if (unlikely(id >= vq->packed.vring.num)) {
14591ce9e605STiwei Bie 		BAD_RING(vq, "id %u out of range\n", id);
14601ce9e605STiwei Bie 		return NULL;
14611ce9e605STiwei Bie 	}
14621ce9e605STiwei Bie 	if (unlikely(!vq->packed.desc_state[id].data)) {
14631ce9e605STiwei Bie 		BAD_RING(vq, "id %u is not a head!\n", id);
14641ce9e605STiwei Bie 		return NULL;
14651ce9e605STiwei Bie 	}
14661ce9e605STiwei Bie 
14671ce9e605STiwei Bie 	/* detach_buf_packed clears data, so grab it now. */
14681ce9e605STiwei Bie 	ret = vq->packed.desc_state[id].data;
14691ce9e605STiwei Bie 	detach_buf_packed(vq, id, ctx);
14701ce9e605STiwei Bie 
14711ce9e605STiwei Bie 	vq->last_used_idx += vq->packed.desc_state[id].num;
14721ce9e605STiwei Bie 	if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) {
14731ce9e605STiwei Bie 		vq->last_used_idx -= vq->packed.vring.num;
14741ce9e605STiwei Bie 		vq->packed.used_wrap_counter ^= 1;
14751ce9e605STiwei Bie 	}
14761ce9e605STiwei Bie 
1477f51f9826STiwei Bie 	/*
1478f51f9826STiwei Bie 	 * If we expect an interrupt for the next entry, tell host
1479f51f9826STiwei Bie 	 * by writing event index and flush out the write before
1480f51f9826STiwei Bie 	 * the read in the next get_buf call.
1481f51f9826STiwei Bie 	 */
1482f51f9826STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1483f51f9826STiwei Bie 		virtio_store_mb(vq->weak_barriers,
1484f51f9826STiwei Bie 				&vq->packed.vring.driver->off_wrap,
1485f51f9826STiwei Bie 				cpu_to_le16(vq->last_used_idx |
1486f51f9826STiwei Bie 					(vq->packed.used_wrap_counter <<
1487f51f9826STiwei Bie 					 VRING_PACKED_EVENT_F_WRAP_CTR)));
1488f51f9826STiwei Bie 
14891ce9e605STiwei Bie 	LAST_ADD_TIME_INVALID(vq);
14901ce9e605STiwei Bie 
14911ce9e605STiwei Bie 	END_USE(vq);
14921ce9e605STiwei Bie 	return ret;
14931ce9e605STiwei Bie }
14941ce9e605STiwei Bie 
14951ce9e605STiwei Bie static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
14961ce9e605STiwei Bie {
14971ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14981ce9e605STiwei Bie 
14991ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
15001ce9e605STiwei Bie 		vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
15011ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
15021ce9e605STiwei Bie 			cpu_to_le16(vq->packed.event_flags_shadow);
15031ce9e605STiwei Bie 	}
15041ce9e605STiwei Bie }
15051ce9e605STiwei Bie 
15061ce9e605STiwei Bie static unsigned virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
15071ce9e605STiwei Bie {
15081ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
15091ce9e605STiwei Bie 
15101ce9e605STiwei Bie 	START_USE(vq);
15111ce9e605STiwei Bie 
15121ce9e605STiwei Bie 	/*
15131ce9e605STiwei Bie 	 * We optimistically turn back on interrupts, then check if there was
15141ce9e605STiwei Bie 	 * more to do.
15151ce9e605STiwei Bie 	 */
15161ce9e605STiwei Bie 
1517f51f9826STiwei Bie 	if (vq->event) {
1518f51f9826STiwei Bie 		vq->packed.vring.driver->off_wrap =
1519f51f9826STiwei Bie 			cpu_to_le16(vq->last_used_idx |
1520f51f9826STiwei Bie 				(vq->packed.used_wrap_counter <<
1521f51f9826STiwei Bie 				 VRING_PACKED_EVENT_F_WRAP_CTR));
1522f51f9826STiwei Bie 		/*
1523f51f9826STiwei Bie 		 * We need to update event offset and event wrap
1524f51f9826STiwei Bie 		 * counter first before updating event flags.
1525f51f9826STiwei Bie 		 */
1526f51f9826STiwei Bie 		virtio_wmb(vq->weak_barriers);
1527f51f9826STiwei Bie 	}
1528f51f9826STiwei Bie 
15291ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1530f51f9826STiwei Bie 		vq->packed.event_flags_shadow = vq->event ?
1531f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_DESC :
1532f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_ENABLE;
15331ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
15341ce9e605STiwei Bie 				cpu_to_le16(vq->packed.event_flags_shadow);
15351ce9e605STiwei Bie 	}
15361ce9e605STiwei Bie 
15371ce9e605STiwei Bie 	END_USE(vq);
15381ce9e605STiwei Bie 	return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter <<
15391ce9e605STiwei Bie 			VRING_PACKED_EVENT_F_WRAP_CTR);
15401ce9e605STiwei Bie }
15411ce9e605STiwei Bie 
15421ce9e605STiwei Bie static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
15431ce9e605STiwei Bie {
15441ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
15451ce9e605STiwei Bie 	bool wrap_counter;
15461ce9e605STiwei Bie 	u16 used_idx;
15471ce9e605STiwei Bie 
15481ce9e605STiwei Bie 	wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
15491ce9e605STiwei Bie 	used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
15501ce9e605STiwei Bie 
15511ce9e605STiwei Bie 	return is_used_desc_packed(vq, used_idx, wrap_counter);
15521ce9e605STiwei Bie }
15531ce9e605STiwei Bie 
15541ce9e605STiwei Bie static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
15551ce9e605STiwei Bie {
15561ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
15571ce9e605STiwei Bie 	u16 used_idx, wrap_counter;
1558f51f9826STiwei Bie 	u16 bufs;
15591ce9e605STiwei Bie 
15601ce9e605STiwei Bie 	START_USE(vq);
15611ce9e605STiwei Bie 
15621ce9e605STiwei Bie 	/*
15631ce9e605STiwei Bie 	 * We optimistically turn back on interrupts, then check if there was
15641ce9e605STiwei Bie 	 * more to do.
15651ce9e605STiwei Bie 	 */
15661ce9e605STiwei Bie 
1567f51f9826STiwei Bie 	if (vq->event) {
1568f51f9826STiwei Bie 		/* TODO: tune this threshold */
1569f51f9826STiwei Bie 		bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
15701ce9e605STiwei Bie 		wrap_counter = vq->packed.used_wrap_counter;
15711ce9e605STiwei Bie 
1572f51f9826STiwei Bie 		used_idx = vq->last_used_idx + bufs;
1573f51f9826STiwei Bie 		if (used_idx >= vq->packed.vring.num) {
1574f51f9826STiwei Bie 			used_idx -= vq->packed.vring.num;
1575f51f9826STiwei Bie 			wrap_counter ^= 1;
1576f51f9826STiwei Bie 		}
1577f51f9826STiwei Bie 
1578f51f9826STiwei Bie 		vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1579f51f9826STiwei Bie 			(wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1580f51f9826STiwei Bie 
1581f51f9826STiwei Bie 		/*
1582f51f9826STiwei Bie 		 * We need to update event offset and event wrap
1583f51f9826STiwei Bie 		 * counter first before updating event flags.
1584f51f9826STiwei Bie 		 */
1585f51f9826STiwei Bie 		virtio_wmb(vq->weak_barriers);
1586f51f9826STiwei Bie 	}
1587f51f9826STiwei Bie 
15881ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1589f51f9826STiwei Bie 		vq->packed.event_flags_shadow = vq->event ?
1590f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_DESC :
1591f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_ENABLE;
15921ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
15931ce9e605STiwei Bie 				cpu_to_le16(vq->packed.event_flags_shadow);
15941ce9e605STiwei Bie 	}
15951ce9e605STiwei Bie 
15961ce9e605STiwei Bie 	/*
15971ce9e605STiwei Bie 	 * We need to update event suppression structure first
15981ce9e605STiwei Bie 	 * before re-checking for more used buffers.
15991ce9e605STiwei Bie 	 */
16001ce9e605STiwei Bie 	virtio_mb(vq->weak_barriers);
16011ce9e605STiwei Bie 
160240ce7919SMarvin Liu 	if (is_used_desc_packed(vq,
160340ce7919SMarvin Liu 				vq->last_used_idx,
160440ce7919SMarvin Liu 				vq->packed.used_wrap_counter)) {
16051ce9e605STiwei Bie 		END_USE(vq);
16061ce9e605STiwei Bie 		return false;
16071ce9e605STiwei Bie 	}
16081ce9e605STiwei Bie 
16091ce9e605STiwei Bie 	END_USE(vq);
16101ce9e605STiwei Bie 	return true;
16111ce9e605STiwei Bie }
16121ce9e605STiwei Bie 
16131ce9e605STiwei Bie static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
16141ce9e605STiwei Bie {
16151ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
16161ce9e605STiwei Bie 	unsigned int i;
16171ce9e605STiwei Bie 	void *buf;
16181ce9e605STiwei Bie 
16191ce9e605STiwei Bie 	START_USE(vq);
16201ce9e605STiwei Bie 
16211ce9e605STiwei Bie 	for (i = 0; i < vq->packed.vring.num; i++) {
16221ce9e605STiwei Bie 		if (!vq->packed.desc_state[i].data)
16231ce9e605STiwei Bie 			continue;
16241ce9e605STiwei Bie 		/* detach_buf clears data, so grab it now. */
16251ce9e605STiwei Bie 		buf = vq->packed.desc_state[i].data;
16261ce9e605STiwei Bie 		detach_buf_packed(vq, i, NULL);
16271ce9e605STiwei Bie 		END_USE(vq);
16281ce9e605STiwei Bie 		return buf;
16291ce9e605STiwei Bie 	}
16301ce9e605STiwei Bie 	/* That should have freed everything. */
16311ce9e605STiwei Bie 	BUG_ON(vq->vq.num_free != vq->packed.vring.num);
16321ce9e605STiwei Bie 
16331ce9e605STiwei Bie 	END_USE(vq);
16341ce9e605STiwei Bie 	return NULL;
16351ce9e605STiwei Bie }
16361ce9e605STiwei Bie 
16375a222421SJason Wang static struct vring_desc_extra *vring_alloc_desc_extra(struct vring_virtqueue *vq,
16385a222421SJason Wang 						       unsigned int num)
16395a222421SJason Wang {
16405a222421SJason Wang 	struct vring_desc_extra *desc_extra;
16415a222421SJason Wang 	unsigned int i;
16425a222421SJason Wang 
16435a222421SJason Wang 	desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra),
16445a222421SJason Wang 				   GFP_KERNEL);
16455a222421SJason Wang 	if (!desc_extra)
16465a222421SJason Wang 		return NULL;
16475a222421SJason Wang 
16485a222421SJason Wang 	memset(desc_extra, 0, num * sizeof(struct vring_desc_extra));
16495a222421SJason Wang 
16505a222421SJason Wang 	for (i = 0; i < num - 1; i++)
16515a222421SJason Wang 		desc_extra[i].next = i + 1;
16525a222421SJason Wang 
16535a222421SJason Wang 	return desc_extra;
16545a222421SJason Wang }
16555a222421SJason Wang 
16561ce9e605STiwei Bie static struct virtqueue *vring_create_virtqueue_packed(
16571ce9e605STiwei Bie 	unsigned int index,
16581ce9e605STiwei Bie 	unsigned int num,
16591ce9e605STiwei Bie 	unsigned int vring_align,
16601ce9e605STiwei Bie 	struct virtio_device *vdev,
16611ce9e605STiwei Bie 	bool weak_barriers,
16621ce9e605STiwei Bie 	bool may_reduce_num,
16631ce9e605STiwei Bie 	bool context,
16641ce9e605STiwei Bie 	bool (*notify)(struct virtqueue *),
16651ce9e605STiwei Bie 	void (*callback)(struct virtqueue *),
16661ce9e605STiwei Bie 	const char *name)
16671ce9e605STiwei Bie {
16681ce9e605STiwei Bie 	struct vring_virtqueue *vq;
16691ce9e605STiwei Bie 	struct vring_packed_desc *ring;
16701ce9e605STiwei Bie 	struct vring_packed_desc_event *driver, *device;
16711ce9e605STiwei Bie 	dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
16721ce9e605STiwei Bie 	size_t ring_size_in_bytes, event_size_in_bytes;
16731ce9e605STiwei Bie 
16741ce9e605STiwei Bie 	ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
16751ce9e605STiwei Bie 
16761ce9e605STiwei Bie 	ring = vring_alloc_queue(vdev, ring_size_in_bytes,
16771ce9e605STiwei Bie 				 &ring_dma_addr,
16781ce9e605STiwei Bie 				 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
16791ce9e605STiwei Bie 	if (!ring)
16801ce9e605STiwei Bie 		goto err_ring;
16811ce9e605STiwei Bie 
16821ce9e605STiwei Bie 	event_size_in_bytes = sizeof(struct vring_packed_desc_event);
16831ce9e605STiwei Bie 
16841ce9e605STiwei Bie 	driver = vring_alloc_queue(vdev, event_size_in_bytes,
16851ce9e605STiwei Bie 				   &driver_event_dma_addr,
16861ce9e605STiwei Bie 				   GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
16871ce9e605STiwei Bie 	if (!driver)
16881ce9e605STiwei Bie 		goto err_driver;
16891ce9e605STiwei Bie 
16901ce9e605STiwei Bie 	device = vring_alloc_queue(vdev, event_size_in_bytes,
16911ce9e605STiwei Bie 				   &device_event_dma_addr,
16921ce9e605STiwei Bie 				   GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
16931ce9e605STiwei Bie 	if (!device)
16941ce9e605STiwei Bie 		goto err_device;
16951ce9e605STiwei Bie 
16961ce9e605STiwei Bie 	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
16971ce9e605STiwei Bie 	if (!vq)
16981ce9e605STiwei Bie 		goto err_vq;
16991ce9e605STiwei Bie 
17001ce9e605STiwei Bie 	vq->vq.callback = callback;
17011ce9e605STiwei Bie 	vq->vq.vdev = vdev;
17021ce9e605STiwei Bie 	vq->vq.name = name;
17031ce9e605STiwei Bie 	vq->vq.num_free = num;
17041ce9e605STiwei Bie 	vq->vq.index = index;
17051ce9e605STiwei Bie 	vq->we_own_ring = true;
17061ce9e605STiwei Bie 	vq->notify = notify;
17071ce9e605STiwei Bie 	vq->weak_barriers = weak_barriers;
17081ce9e605STiwei Bie 	vq->broken = false;
17091ce9e605STiwei Bie 	vq->last_used_idx = 0;
17108d622d21SMichael S. Tsirkin 	vq->event_triggered = false;
17111ce9e605STiwei Bie 	vq->num_added = 0;
17121ce9e605STiwei Bie 	vq->packed_ring = true;
17131ce9e605STiwei Bie 	vq->use_dma_api = vring_use_dma_api(vdev);
17141ce9e605STiwei Bie #ifdef DEBUG
17151ce9e605STiwei Bie 	vq->in_use = false;
17161ce9e605STiwei Bie 	vq->last_add_time_valid = false;
17171ce9e605STiwei Bie #endif
17181ce9e605STiwei Bie 
17191ce9e605STiwei Bie 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
17201ce9e605STiwei Bie 		!context;
17211ce9e605STiwei Bie 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
17221ce9e605STiwei Bie 
172345383fb0STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
172445383fb0STiwei Bie 		vq->weak_barriers = false;
172545383fb0STiwei Bie 
17261ce9e605STiwei Bie 	vq->packed.ring_dma_addr = ring_dma_addr;
17271ce9e605STiwei Bie 	vq->packed.driver_event_dma_addr = driver_event_dma_addr;
17281ce9e605STiwei Bie 	vq->packed.device_event_dma_addr = device_event_dma_addr;
17291ce9e605STiwei Bie 
17301ce9e605STiwei Bie 	vq->packed.ring_size_in_bytes = ring_size_in_bytes;
17311ce9e605STiwei Bie 	vq->packed.event_size_in_bytes = event_size_in_bytes;
17321ce9e605STiwei Bie 
17331ce9e605STiwei Bie 	vq->packed.vring.num = num;
17341ce9e605STiwei Bie 	vq->packed.vring.desc = ring;
17351ce9e605STiwei Bie 	vq->packed.vring.driver = driver;
17361ce9e605STiwei Bie 	vq->packed.vring.device = device;
17371ce9e605STiwei Bie 
17381ce9e605STiwei Bie 	vq->packed.next_avail_idx = 0;
17391ce9e605STiwei Bie 	vq->packed.avail_wrap_counter = 1;
17401ce9e605STiwei Bie 	vq->packed.used_wrap_counter = 1;
17411ce9e605STiwei Bie 	vq->packed.event_flags_shadow = 0;
17421ce9e605STiwei Bie 	vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
17431ce9e605STiwei Bie 
17441ce9e605STiwei Bie 	vq->packed.desc_state = kmalloc_array(num,
17451ce9e605STiwei Bie 			sizeof(struct vring_desc_state_packed),
17461ce9e605STiwei Bie 			GFP_KERNEL);
17471ce9e605STiwei Bie 	if (!vq->packed.desc_state)
17481ce9e605STiwei Bie 		goto err_desc_state;
17491ce9e605STiwei Bie 
17501ce9e605STiwei Bie 	memset(vq->packed.desc_state, 0,
17511ce9e605STiwei Bie 		num * sizeof(struct vring_desc_state_packed));
17521ce9e605STiwei Bie 
17531ce9e605STiwei Bie 	/* Put everything in free lists. */
17541ce9e605STiwei Bie 	vq->free_head = 0;
17551ce9e605STiwei Bie 
17565a222421SJason Wang 	vq->packed.desc_extra = vring_alloc_desc_extra(vq, num);
17571ce9e605STiwei Bie 	if (!vq->packed.desc_extra)
17581ce9e605STiwei Bie 		goto err_desc_extra;
17591ce9e605STiwei Bie 
17601ce9e605STiwei Bie 	/* No callback?  Tell other side not to bother us. */
17611ce9e605STiwei Bie 	if (!callback) {
17621ce9e605STiwei Bie 		vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
17631ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
17641ce9e605STiwei Bie 			cpu_to_le16(vq->packed.event_flags_shadow);
17651ce9e605STiwei Bie 	}
17661ce9e605STiwei Bie 
17670e566c8fSParav Pandit 	spin_lock(&vdev->vqs_list_lock);
1768e152d8afSDan Carpenter 	list_add_tail(&vq->vq.list, &vdev->vqs);
17690e566c8fSParav Pandit 	spin_unlock(&vdev->vqs_list_lock);
17701ce9e605STiwei Bie 	return &vq->vq;
17711ce9e605STiwei Bie 
17721ce9e605STiwei Bie err_desc_extra:
17731ce9e605STiwei Bie 	kfree(vq->packed.desc_state);
17741ce9e605STiwei Bie err_desc_state:
17751ce9e605STiwei Bie 	kfree(vq);
17761ce9e605STiwei Bie err_vq:
1777ae93d8eaSDan Carpenter 	vring_free_queue(vdev, event_size_in_bytes, device, device_event_dma_addr);
17781ce9e605STiwei Bie err_device:
1779ae93d8eaSDan Carpenter 	vring_free_queue(vdev, event_size_in_bytes, driver, driver_event_dma_addr);
17801ce9e605STiwei Bie err_driver:
17811ce9e605STiwei Bie 	vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr);
17821ce9e605STiwei Bie err_ring:
17831ce9e605STiwei Bie 	return NULL;
17841ce9e605STiwei Bie }
17851ce9e605STiwei Bie 
17861ce9e605STiwei Bie 
17871ce9e605STiwei Bie /*
1788e6f633e5STiwei Bie  * Generic functions and exported symbols.
1789e6f633e5STiwei Bie  */
1790e6f633e5STiwei Bie 
1791e6f633e5STiwei Bie static inline int virtqueue_add(struct virtqueue *_vq,
1792e6f633e5STiwei Bie 				struct scatterlist *sgs[],
1793e6f633e5STiwei Bie 				unsigned int total_sg,
1794e6f633e5STiwei Bie 				unsigned int out_sgs,
1795e6f633e5STiwei Bie 				unsigned int in_sgs,
1796e6f633e5STiwei Bie 				void *data,
1797e6f633e5STiwei Bie 				void *ctx,
1798e6f633e5STiwei Bie 				gfp_t gfp)
1799e6f633e5STiwei Bie {
18001ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
18011ce9e605STiwei Bie 
18021ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
18031ce9e605STiwei Bie 					out_sgs, in_sgs, data, ctx, gfp) :
18041ce9e605STiwei Bie 				 virtqueue_add_split(_vq, sgs, total_sg,
1805e6f633e5STiwei Bie 					out_sgs, in_sgs, data, ctx, gfp);
1806e6f633e5STiwei Bie }
1807e6f633e5STiwei Bie 
1808e6f633e5STiwei Bie /**
1809e6f633e5STiwei Bie  * virtqueue_add_sgs - expose buffers to other end
1810a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1811e6f633e5STiwei Bie  * @sgs: array of terminated scatterlists.
1812a5581206SJiang Biao  * @out_sgs: the number of scatterlists readable by other side
1813a5581206SJiang Biao  * @in_sgs: the number of scatterlists which are writable (after readable ones)
1814e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1815e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1816e6f633e5STiwei Bie  *
1817e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1818e6f633e5STiwei Bie  * at the same time (except where noted).
1819e6f633e5STiwei Bie  *
1820e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1821e6f633e5STiwei Bie  */
1822e6f633e5STiwei Bie int virtqueue_add_sgs(struct virtqueue *_vq,
1823e6f633e5STiwei Bie 		      struct scatterlist *sgs[],
1824e6f633e5STiwei Bie 		      unsigned int out_sgs,
1825e6f633e5STiwei Bie 		      unsigned int in_sgs,
1826e6f633e5STiwei Bie 		      void *data,
1827e6f633e5STiwei Bie 		      gfp_t gfp)
1828e6f633e5STiwei Bie {
1829e6f633e5STiwei Bie 	unsigned int i, total_sg = 0;
1830e6f633e5STiwei Bie 
1831e6f633e5STiwei Bie 	/* Count them first. */
1832e6f633e5STiwei Bie 	for (i = 0; i < out_sgs + in_sgs; i++) {
1833e6f633e5STiwei Bie 		struct scatterlist *sg;
1834e6f633e5STiwei Bie 
1835e6f633e5STiwei Bie 		for (sg = sgs[i]; sg; sg = sg_next(sg))
1836e6f633e5STiwei Bie 			total_sg++;
1837e6f633e5STiwei Bie 	}
1838e6f633e5STiwei Bie 	return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
1839e6f633e5STiwei Bie 			     data, NULL, gfp);
1840e6f633e5STiwei Bie }
1841e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
1842e6f633e5STiwei Bie 
1843e6f633e5STiwei Bie /**
1844e6f633e5STiwei Bie  * virtqueue_add_outbuf - expose output buffers to other end
1845e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
1846e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
1847e6f633e5STiwei Bie  * @num: the number of entries in @sg readable by other side
1848e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1849e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1850e6f633e5STiwei Bie  *
1851e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1852e6f633e5STiwei Bie  * at the same time (except where noted).
1853e6f633e5STiwei Bie  *
1854e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1855e6f633e5STiwei Bie  */
1856e6f633e5STiwei Bie int virtqueue_add_outbuf(struct virtqueue *vq,
1857e6f633e5STiwei Bie 			 struct scatterlist *sg, unsigned int num,
1858e6f633e5STiwei Bie 			 void *data,
1859e6f633e5STiwei Bie 			 gfp_t gfp)
1860e6f633e5STiwei Bie {
1861e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
1862e6f633e5STiwei Bie }
1863e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
1864e6f633e5STiwei Bie 
1865e6f633e5STiwei Bie /**
1866e6f633e5STiwei Bie  * virtqueue_add_inbuf - expose input buffers to other end
1867e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
1868e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
1869e6f633e5STiwei Bie  * @num: the number of entries in @sg writable by other side
1870e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1871e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1872e6f633e5STiwei Bie  *
1873e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1874e6f633e5STiwei Bie  * at the same time (except where noted).
1875e6f633e5STiwei Bie  *
1876e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1877e6f633e5STiwei Bie  */
1878e6f633e5STiwei Bie int virtqueue_add_inbuf(struct virtqueue *vq,
1879e6f633e5STiwei Bie 			struct scatterlist *sg, unsigned int num,
1880e6f633e5STiwei Bie 			void *data,
1881e6f633e5STiwei Bie 			gfp_t gfp)
1882e6f633e5STiwei Bie {
1883e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
1884e6f633e5STiwei Bie }
1885e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
1886e6f633e5STiwei Bie 
1887e6f633e5STiwei Bie /**
1888e6f633e5STiwei Bie  * virtqueue_add_inbuf_ctx - expose input buffers to other end
1889e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
1890e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
1891e6f633e5STiwei Bie  * @num: the number of entries in @sg writable by other side
1892e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1893e6f633e5STiwei Bie  * @ctx: extra context for the token
1894e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1895e6f633e5STiwei Bie  *
1896e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1897e6f633e5STiwei Bie  * at the same time (except where noted).
1898e6f633e5STiwei Bie  *
1899e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1900e6f633e5STiwei Bie  */
1901e6f633e5STiwei Bie int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
1902e6f633e5STiwei Bie 			struct scatterlist *sg, unsigned int num,
1903e6f633e5STiwei Bie 			void *data,
1904e6f633e5STiwei Bie 			void *ctx,
1905e6f633e5STiwei Bie 			gfp_t gfp)
1906e6f633e5STiwei Bie {
1907e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
1908e6f633e5STiwei Bie }
1909e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
1910e6f633e5STiwei Bie 
1911e6f633e5STiwei Bie /**
1912e6f633e5STiwei Bie  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
1913a5581206SJiang Biao  * @_vq: the struct virtqueue
1914e6f633e5STiwei Bie  *
1915e6f633e5STiwei Bie  * Instead of virtqueue_kick(), you can do:
1916e6f633e5STiwei Bie  *	if (virtqueue_kick_prepare(vq))
1917e6f633e5STiwei Bie  *		virtqueue_notify(vq);
1918e6f633e5STiwei Bie  *
1919e6f633e5STiwei Bie  * This is sometimes useful because the virtqueue_kick_prepare() needs
1920e6f633e5STiwei Bie  * to be serialized, but the actual virtqueue_notify() call does not.
1921e6f633e5STiwei Bie  */
1922e6f633e5STiwei Bie bool virtqueue_kick_prepare(struct virtqueue *_vq)
1923e6f633e5STiwei Bie {
19241ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
19251ce9e605STiwei Bie 
19261ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
19271ce9e605STiwei Bie 				 virtqueue_kick_prepare_split(_vq);
1928e6f633e5STiwei Bie }
1929e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
1930e6f633e5STiwei Bie 
1931e6f633e5STiwei Bie /**
1932e6f633e5STiwei Bie  * virtqueue_notify - second half of split virtqueue_kick call.
1933a5581206SJiang Biao  * @_vq: the struct virtqueue
1934e6f633e5STiwei Bie  *
1935e6f633e5STiwei Bie  * This does not need to be serialized.
1936e6f633e5STiwei Bie  *
1937e6f633e5STiwei Bie  * Returns false if host notify failed or queue is broken, otherwise true.
1938e6f633e5STiwei Bie  */
1939e6f633e5STiwei Bie bool virtqueue_notify(struct virtqueue *_vq)
1940e6f633e5STiwei Bie {
1941e6f633e5STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1942e6f633e5STiwei Bie 
1943e6f633e5STiwei Bie 	if (unlikely(vq->broken))
1944e6f633e5STiwei Bie 		return false;
1945e6f633e5STiwei Bie 
1946e6f633e5STiwei Bie 	/* Prod other side to tell it about changes. */
1947e6f633e5STiwei Bie 	if (!vq->notify(_vq)) {
1948e6f633e5STiwei Bie 		vq->broken = true;
1949e6f633e5STiwei Bie 		return false;
1950e6f633e5STiwei Bie 	}
1951e6f633e5STiwei Bie 	return true;
1952e6f633e5STiwei Bie }
1953e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_notify);
1954e6f633e5STiwei Bie 
1955e6f633e5STiwei Bie /**
1956e6f633e5STiwei Bie  * virtqueue_kick - update after add_buf
1957e6f633e5STiwei Bie  * @vq: the struct virtqueue
1958e6f633e5STiwei Bie  *
1959e6f633e5STiwei Bie  * After one or more virtqueue_add_* calls, invoke this to kick
1960e6f633e5STiwei Bie  * the other side.
1961e6f633e5STiwei Bie  *
1962e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1963e6f633e5STiwei Bie  * operations at the same time (except where noted).
1964e6f633e5STiwei Bie  *
1965e6f633e5STiwei Bie  * Returns false if kick failed, otherwise true.
1966e6f633e5STiwei Bie  */
1967e6f633e5STiwei Bie bool virtqueue_kick(struct virtqueue *vq)
1968e6f633e5STiwei Bie {
1969e6f633e5STiwei Bie 	if (virtqueue_kick_prepare(vq))
1970e6f633e5STiwei Bie 		return virtqueue_notify(vq);
1971e6f633e5STiwei Bie 	return true;
1972e6f633e5STiwei Bie }
1973e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick);
1974e6f633e5STiwei Bie 
1975e6f633e5STiwei Bie /**
197631c11db6SYang Li  * virtqueue_get_buf_ctx - get the next used buffer
1977a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1978e6f633e5STiwei Bie  * @len: the length written into the buffer
1979a5581206SJiang Biao  * @ctx: extra context for the token
1980e6f633e5STiwei Bie  *
1981e6f633e5STiwei Bie  * If the device wrote data into the buffer, @len will be set to the
1982e6f633e5STiwei Bie  * amount written.  This means you don't need to clear the buffer
1983e6f633e5STiwei Bie  * beforehand to ensure there's no data leakage in the case of short
1984e6f633e5STiwei Bie  * writes.
1985e6f633e5STiwei Bie  *
1986e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1987e6f633e5STiwei Bie  * operations at the same time (except where noted).
1988e6f633e5STiwei Bie  *
1989e6f633e5STiwei Bie  * Returns NULL if there are no used buffers, or the "data" token
1990e6f633e5STiwei Bie  * handed to virtqueue_add_*().
1991e6f633e5STiwei Bie  */
1992e6f633e5STiwei Bie void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
1993e6f633e5STiwei Bie 			    void **ctx)
1994e6f633e5STiwei Bie {
19951ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
19961ce9e605STiwei Bie 
19971ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
19981ce9e605STiwei Bie 				 virtqueue_get_buf_ctx_split(_vq, len, ctx);
1999e6f633e5STiwei Bie }
2000e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
2001e6f633e5STiwei Bie 
2002e6f633e5STiwei Bie void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
2003e6f633e5STiwei Bie {
2004e6f633e5STiwei Bie 	return virtqueue_get_buf_ctx(_vq, len, NULL);
2005e6f633e5STiwei Bie }
2006e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf);
2007e6f633e5STiwei Bie /**
2008e6f633e5STiwei Bie  * virtqueue_disable_cb - disable callbacks
2009a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2010e6f633e5STiwei Bie  *
2011e6f633e5STiwei Bie  * Note that this is not necessarily synchronous, hence unreliable and only
2012e6f633e5STiwei Bie  * useful as an optimization.
2013e6f633e5STiwei Bie  *
2014e6f633e5STiwei Bie  * Unlike other operations, this need not be serialized.
2015e6f633e5STiwei Bie  */
2016e6f633e5STiwei Bie void virtqueue_disable_cb(struct virtqueue *_vq)
2017e6f633e5STiwei Bie {
20181ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
20191ce9e605STiwei Bie 
20208d622d21SMichael S. Tsirkin 	/* If device triggered an event already it won't trigger one again:
20218d622d21SMichael S. Tsirkin 	 * no need to disable.
20228d622d21SMichael S. Tsirkin 	 */
20238d622d21SMichael S. Tsirkin 	if (vq->event_triggered)
20248d622d21SMichael S. Tsirkin 		return;
20258d622d21SMichael S. Tsirkin 
20261ce9e605STiwei Bie 	if (vq->packed_ring)
20271ce9e605STiwei Bie 		virtqueue_disable_cb_packed(_vq);
20281ce9e605STiwei Bie 	else
2029e6f633e5STiwei Bie 		virtqueue_disable_cb_split(_vq);
2030e6f633e5STiwei Bie }
2031e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
2032e6f633e5STiwei Bie 
2033e6f633e5STiwei Bie /**
2034e6f633e5STiwei Bie  * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
2035a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2036e6f633e5STiwei Bie  *
2037e6f633e5STiwei Bie  * This re-enables callbacks; it returns current queue state
2038e6f633e5STiwei Bie  * in an opaque unsigned value. This value should be later tested by
2039e6f633e5STiwei Bie  * virtqueue_poll, to detect a possible race between the driver checking for
2040e6f633e5STiwei Bie  * more work, and enabling callbacks.
2041e6f633e5STiwei Bie  *
2042e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
2043e6f633e5STiwei Bie  * operations at the same time (except where noted).
2044e6f633e5STiwei Bie  */
2045e6f633e5STiwei Bie unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
2046e6f633e5STiwei Bie {
20471ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
20481ce9e605STiwei Bie 
20498d622d21SMichael S. Tsirkin 	if (vq->event_triggered)
20508d622d21SMichael S. Tsirkin 		vq->event_triggered = false;
20518d622d21SMichael S. Tsirkin 
20521ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
20531ce9e605STiwei Bie 				 virtqueue_enable_cb_prepare_split(_vq);
2054e6f633e5STiwei Bie }
2055e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
2056e6f633e5STiwei Bie 
2057e6f633e5STiwei Bie /**
2058e6f633e5STiwei Bie  * virtqueue_poll - query pending used buffers
2059a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2060e6f633e5STiwei Bie  * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
2061e6f633e5STiwei Bie  *
2062e6f633e5STiwei Bie  * Returns "true" if there are pending used buffers in the queue.
2063e6f633e5STiwei Bie  *
2064e6f633e5STiwei Bie  * This does not need to be serialized.
2065e6f633e5STiwei Bie  */
2066e6f633e5STiwei Bie bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
2067e6f633e5STiwei Bie {
2068e6f633e5STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
2069e6f633e5STiwei Bie 
2070481a0d74SMao Wenan 	if (unlikely(vq->broken))
2071481a0d74SMao Wenan 		return false;
2072481a0d74SMao Wenan 
2073e6f633e5STiwei Bie 	virtio_mb(vq->weak_barriers);
20741ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
20751ce9e605STiwei Bie 				 virtqueue_poll_split(_vq, last_used_idx);
2076e6f633e5STiwei Bie }
2077e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_poll);
2078e6f633e5STiwei Bie 
2079e6f633e5STiwei Bie /**
2080e6f633e5STiwei Bie  * virtqueue_enable_cb - restart callbacks after disable_cb.
2081a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2082e6f633e5STiwei Bie  *
2083e6f633e5STiwei Bie  * This re-enables callbacks; it returns "false" if there are pending
2084e6f633e5STiwei Bie  * buffers in the queue, to detect a possible race between the driver
2085e6f633e5STiwei Bie  * checking for more work, and enabling callbacks.
2086e6f633e5STiwei Bie  *
2087e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
2088e6f633e5STiwei Bie  * operations at the same time (except where noted).
2089e6f633e5STiwei Bie  */
2090e6f633e5STiwei Bie bool virtqueue_enable_cb(struct virtqueue *_vq)
2091e6f633e5STiwei Bie {
2092e6f633e5STiwei Bie 	unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
2093e6f633e5STiwei Bie 
2094e6f633e5STiwei Bie 	return !virtqueue_poll(_vq, last_used_idx);
2095e6f633e5STiwei Bie }
2096e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
2097e6f633e5STiwei Bie 
2098e6f633e5STiwei Bie /**
2099e6f633e5STiwei Bie  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
2100a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2101e6f633e5STiwei Bie  *
2102e6f633e5STiwei Bie  * This re-enables callbacks but hints to the other side to delay
2103e6f633e5STiwei Bie  * interrupts until most of the available buffers have been processed;
2104e6f633e5STiwei Bie  * it returns "false" if there are many pending buffers in the queue,
2105e6f633e5STiwei Bie  * to detect a possible race between the driver checking for more work,
2106e6f633e5STiwei Bie  * and enabling callbacks.
2107e6f633e5STiwei Bie  *
2108e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
2109e6f633e5STiwei Bie  * operations at the same time (except where noted).
2110e6f633e5STiwei Bie  */
2111e6f633e5STiwei Bie bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
2112e6f633e5STiwei Bie {
21131ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
21141ce9e605STiwei Bie 
21158d622d21SMichael S. Tsirkin 	if (vq->event_triggered)
21168d622d21SMichael S. Tsirkin 		vq->event_triggered = false;
21178d622d21SMichael S. Tsirkin 
21181ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
21191ce9e605STiwei Bie 				 virtqueue_enable_cb_delayed_split(_vq);
2120e6f633e5STiwei Bie }
2121e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2122e6f633e5STiwei Bie 
2123138fd251STiwei Bie /**
2124138fd251STiwei Bie  * virtqueue_detach_unused_buf - detach first unused buffer
2125a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2126138fd251STiwei Bie  *
2127138fd251STiwei Bie  * Returns NULL or the "data" token handed to virtqueue_add_*().
2128138fd251STiwei Bie  * This is not valid on an active queue; it is useful only for device
2129138fd251STiwei Bie  * shutdown.
2130138fd251STiwei Bie  */
2131138fd251STiwei Bie void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2132138fd251STiwei Bie {
21331ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
21341ce9e605STiwei Bie 
21351ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
21361ce9e605STiwei Bie 				 virtqueue_detach_unused_buf_split(_vq);
2137138fd251STiwei Bie }
21387c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
2139c021eac4SShirley Ma 
2140138fd251STiwei Bie static inline bool more_used(const struct vring_virtqueue *vq)
2141138fd251STiwei Bie {
21421ce9e605STiwei Bie 	return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
2143138fd251STiwei Bie }
2144138fd251STiwei Bie 
21450a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
21460a8a69ddSRusty Russell {
21470a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
21480a8a69ddSRusty Russell 
21490a8a69ddSRusty Russell 	if (!more_used(vq)) {
21500a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
21510a8a69ddSRusty Russell 		return IRQ_NONE;
21520a8a69ddSRusty Russell 	}
21530a8a69ddSRusty Russell 
21540a8a69ddSRusty Russell 	if (unlikely(vq->broken))
21550a8a69ddSRusty Russell 		return IRQ_HANDLED;
21560a8a69ddSRusty Russell 
21578d622d21SMichael S. Tsirkin 	/* Just a hint for performance: so it's ok that this can be racy! */
21588d622d21SMichael S. Tsirkin 	if (vq->event)
21598d622d21SMichael S. Tsirkin 		vq->event_triggered = true;
21608d622d21SMichael S. Tsirkin 
21610a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
216218445c4dSRusty Russell 	if (vq->vq.callback)
216318445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
21640a8a69ddSRusty Russell 
21650a8a69ddSRusty Russell 	return IRQ_HANDLED;
21660a8a69ddSRusty Russell }
2167c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
21680a8a69ddSRusty Russell 
21691ce9e605STiwei Bie /* Only available for split ring */
21702a2d1382SAndy Lutomirski struct virtqueue *__vring_new_virtqueue(unsigned int index,
21712a2d1382SAndy Lutomirski 					struct vring vring,
21720a8a69ddSRusty Russell 					struct virtio_device *vdev,
21737b21e34fSRusty Russell 					bool weak_barriers,
2174f94682ddSMichael S. Tsirkin 					bool context,
217546f9c2b9SHeinz Graalfs 					bool (*notify)(struct virtqueue *),
21769499f5e7SRusty Russell 					void (*callback)(struct virtqueue *),
21779499f5e7SRusty Russell 					const char *name)
21780a8a69ddSRusty Russell {
21792a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq;
21800a8a69ddSRusty Russell 
21811ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
21821ce9e605STiwei Bie 		return NULL;
21831ce9e605STiwei Bie 
2184cbeedb72STiwei Bie 	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
21850a8a69ddSRusty Russell 	if (!vq)
21860a8a69ddSRusty Russell 		return NULL;
21870a8a69ddSRusty Russell 
21881ce9e605STiwei Bie 	vq->packed_ring = false;
21890a8a69ddSRusty Russell 	vq->vq.callback = callback;
21900a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
21919499f5e7SRusty Russell 	vq->vq.name = name;
21922a2d1382SAndy Lutomirski 	vq->vq.num_free = vring.num;
219306ca287dSRusty Russell 	vq->vq.index = index;
21942a2d1382SAndy Lutomirski 	vq->we_own_ring = false;
21950a8a69ddSRusty Russell 	vq->notify = notify;
21967b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
21970a8a69ddSRusty Russell 	vq->broken = false;
21980a8a69ddSRusty Russell 	vq->last_used_idx = 0;
21998d622d21SMichael S. Tsirkin 	vq->event_triggered = false;
22000a8a69ddSRusty Russell 	vq->num_added = 0;
2201fb3fba6bSTiwei Bie 	vq->use_dma_api = vring_use_dma_api(vdev);
22020a8a69ddSRusty Russell #ifdef DEBUG
22030a8a69ddSRusty Russell 	vq->in_use = false;
2204e93300b1SRusty Russell 	vq->last_add_time_valid = false;
22050a8a69ddSRusty Russell #endif
22060a8a69ddSRusty Russell 
22075a08b04fSMichael S. Tsirkin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
22085a08b04fSMichael S. Tsirkin 		!context;
2209a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
22109fa29b9dSMark McLoughlin 
221145383fb0STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
221245383fb0STiwei Bie 		vq->weak_barriers = false;
221345383fb0STiwei Bie 
2214d79dca75STiwei Bie 	vq->split.queue_dma_addr = 0;
2215d79dca75STiwei Bie 	vq->split.queue_size_in_bytes = 0;
2216d79dca75STiwei Bie 
2217e593bf97STiwei Bie 	vq->split.vring = vring;
2218e593bf97STiwei Bie 	vq->split.avail_flags_shadow = 0;
2219e593bf97STiwei Bie 	vq->split.avail_idx_shadow = 0;
2220e593bf97STiwei Bie 
22210a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
2222f277ec42SVenkatesh Srinivas 	if (!callback) {
2223e593bf97STiwei Bie 		vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
22240ea1e4a6SLadi Prosek 		if (!vq->event)
2225e593bf97STiwei Bie 			vq->split.vring.avail->flags = cpu_to_virtio16(vdev,
2226e593bf97STiwei Bie 					vq->split.avail_flags_shadow);
2227f277ec42SVenkatesh Srinivas 	}
22280a8a69ddSRusty Russell 
2229cbeedb72STiwei Bie 	vq->split.desc_state = kmalloc_array(vring.num,
2230cbeedb72STiwei Bie 			sizeof(struct vring_desc_state_split), GFP_KERNEL);
22315bc72234SJason Wang 	if (!vq->split.desc_state)
22325bc72234SJason Wang 		goto err_state;
2233cbeedb72STiwei Bie 
223472b5e895SJason Wang 	vq->split.desc_extra = vring_alloc_desc_extra(vq, vring.num);
223572b5e895SJason Wang 	if (!vq->split.desc_extra)
223672b5e895SJason Wang 		goto err_extra;
223772b5e895SJason Wang 
22380a8a69ddSRusty Russell 	/* Put everything in free lists. */
22390a8a69ddSRusty Russell 	vq->free_head = 0;
2240cbeedb72STiwei Bie 	memset(vq->split.desc_state, 0, vring.num *
2241cbeedb72STiwei Bie 			sizeof(struct vring_desc_state_split));
22420a8a69ddSRusty Russell 
22430e566c8fSParav Pandit 	spin_lock(&vdev->vqs_list_lock);
2244e152d8afSDan Carpenter 	list_add_tail(&vq->vq.list, &vdev->vqs);
22450e566c8fSParav Pandit 	spin_unlock(&vdev->vqs_list_lock);
22460a8a69ddSRusty Russell 	return &vq->vq;
22475bc72234SJason Wang 
224872b5e895SJason Wang err_extra:
224972b5e895SJason Wang 	kfree(vq->split.desc_state);
22505bc72234SJason Wang err_state:
22515bc72234SJason Wang 	kfree(vq);
22525bc72234SJason Wang 	return NULL;
22530a8a69ddSRusty Russell }
22542a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
22552a2d1382SAndy Lutomirski 
22562a2d1382SAndy Lutomirski struct virtqueue *vring_create_virtqueue(
22572a2d1382SAndy Lutomirski 	unsigned int index,
22582a2d1382SAndy Lutomirski 	unsigned int num,
22592a2d1382SAndy Lutomirski 	unsigned int vring_align,
22602a2d1382SAndy Lutomirski 	struct virtio_device *vdev,
22612a2d1382SAndy Lutomirski 	bool weak_barriers,
22622a2d1382SAndy Lutomirski 	bool may_reduce_num,
2263f94682ddSMichael S. Tsirkin 	bool context,
22642a2d1382SAndy Lutomirski 	bool (*notify)(struct virtqueue *),
22652a2d1382SAndy Lutomirski 	void (*callback)(struct virtqueue *),
22662a2d1382SAndy Lutomirski 	const char *name)
22672a2d1382SAndy Lutomirski {
22681ce9e605STiwei Bie 
22691ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
22701ce9e605STiwei Bie 		return vring_create_virtqueue_packed(index, num, vring_align,
22711ce9e605STiwei Bie 				vdev, weak_barriers, may_reduce_num,
22721ce9e605STiwei Bie 				context, notify, callback, name);
22731ce9e605STiwei Bie 
2274d79dca75STiwei Bie 	return vring_create_virtqueue_split(index, num, vring_align,
2275d79dca75STiwei Bie 			vdev, weak_barriers, may_reduce_num,
2276d79dca75STiwei Bie 			context, notify, callback, name);
22772a2d1382SAndy Lutomirski }
22782a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(vring_create_virtqueue);
22792a2d1382SAndy Lutomirski 
22801ce9e605STiwei Bie /* Only available for split ring */
22812a2d1382SAndy Lutomirski struct virtqueue *vring_new_virtqueue(unsigned int index,
22822a2d1382SAndy Lutomirski 				      unsigned int num,
22832a2d1382SAndy Lutomirski 				      unsigned int vring_align,
22842a2d1382SAndy Lutomirski 				      struct virtio_device *vdev,
22852a2d1382SAndy Lutomirski 				      bool weak_barriers,
2286f94682ddSMichael S. Tsirkin 				      bool context,
22872a2d1382SAndy Lutomirski 				      void *pages,
22882a2d1382SAndy Lutomirski 				      bool (*notify)(struct virtqueue *vq),
22892a2d1382SAndy Lutomirski 				      void (*callback)(struct virtqueue *vq),
22902a2d1382SAndy Lutomirski 				      const char *name)
22912a2d1382SAndy Lutomirski {
22922a2d1382SAndy Lutomirski 	struct vring vring;
22931ce9e605STiwei Bie 
22941ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
22951ce9e605STiwei Bie 		return NULL;
22961ce9e605STiwei Bie 
22972a2d1382SAndy Lutomirski 	vring_init(&vring, num, pages, vring_align);
2298f94682ddSMichael S. Tsirkin 	return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
22992a2d1382SAndy Lutomirski 				     notify, callback, name);
23002a2d1382SAndy Lutomirski }
2301c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
23020a8a69ddSRusty Russell 
23032a2d1382SAndy Lutomirski void vring_del_virtqueue(struct virtqueue *_vq)
23040a8a69ddSRusty Russell {
23052a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq = to_vvq(_vq);
23062a2d1382SAndy Lutomirski 
23070e566c8fSParav Pandit 	spin_lock(&vq->vq.vdev->vqs_list_lock);
2308249f2554SParav Pandit 	list_del(&_vq->list);
23090e566c8fSParav Pandit 	spin_unlock(&vq->vq.vdev->vqs_list_lock);
2310249f2554SParav Pandit 
23112a2d1382SAndy Lutomirski 	if (vq->we_own_ring) {
23121ce9e605STiwei Bie 		if (vq->packed_ring) {
23131ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
23141ce9e605STiwei Bie 					 vq->packed.ring_size_in_bytes,
23151ce9e605STiwei Bie 					 vq->packed.vring.desc,
23161ce9e605STiwei Bie 					 vq->packed.ring_dma_addr);
23171ce9e605STiwei Bie 
23181ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
23191ce9e605STiwei Bie 					 vq->packed.event_size_in_bytes,
23201ce9e605STiwei Bie 					 vq->packed.vring.driver,
23211ce9e605STiwei Bie 					 vq->packed.driver_event_dma_addr);
23221ce9e605STiwei Bie 
23231ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
23241ce9e605STiwei Bie 					 vq->packed.event_size_in_bytes,
23251ce9e605STiwei Bie 					 vq->packed.vring.device,
23261ce9e605STiwei Bie 					 vq->packed.device_event_dma_addr);
23271ce9e605STiwei Bie 
23281ce9e605STiwei Bie 			kfree(vq->packed.desc_state);
23291ce9e605STiwei Bie 			kfree(vq->packed.desc_extra);
23301ce9e605STiwei Bie 		} else {
2331d79dca75STiwei Bie 			vring_free_queue(vq->vq.vdev,
2332d79dca75STiwei Bie 					 vq->split.queue_size_in_bytes,
2333d79dca75STiwei Bie 					 vq->split.vring.desc,
2334d79dca75STiwei Bie 					 vq->split.queue_dma_addr);
2335f13f09a1SSuman Anna 		}
2336f13f09a1SSuman Anna 	}
233772b5e895SJason Wang 	if (!vq->packed_ring) {
2338cbeedb72STiwei Bie 		kfree(vq->split.desc_state);
233972b5e895SJason Wang 		kfree(vq->split.desc_extra);
234072b5e895SJason Wang 	}
23412a2d1382SAndy Lutomirski 	kfree(vq);
23420a8a69ddSRusty Russell }
2343c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
23440a8a69ddSRusty Russell 
2345e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
2346e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
2347e34f8725SRusty Russell {
2348e34f8725SRusty Russell 	unsigned int i;
2349e34f8725SRusty Russell 
2350e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2351e34f8725SRusty Russell 		switch (i) {
23529fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
23539fa29b9dSMark McLoughlin 			break;
2354a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
2355a5c262c5SMichael S. Tsirkin 			break;
2356747ae34aSMichael S. Tsirkin 		case VIRTIO_F_VERSION_1:
2357747ae34aSMichael S. Tsirkin 			break;
2358321bd212SMichael S. Tsirkin 		case VIRTIO_F_ACCESS_PLATFORM:
23591a937693SMichael S. Tsirkin 			break;
2360f959a128STiwei Bie 		case VIRTIO_F_RING_PACKED:
2361f959a128STiwei Bie 			break;
236245383fb0STiwei Bie 		case VIRTIO_F_ORDER_PLATFORM:
236345383fb0STiwei Bie 			break;
2364e34f8725SRusty Russell 		default:
2365e34f8725SRusty Russell 			/* We don't understand this bit. */
2366e16e12beSMichael S. Tsirkin 			__virtio_clear_bit(vdev, i);
2367e34f8725SRusty Russell 		}
2368e34f8725SRusty Russell 	}
2369e34f8725SRusty Russell }
2370e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
2371e34f8725SRusty Russell 
23725dfc1762SRusty Russell /**
23735dfc1762SRusty Russell  * virtqueue_get_vring_size - return the size of the virtqueue's vring
2374a5581206SJiang Biao  * @_vq: the struct virtqueue containing the vring of interest.
23755dfc1762SRusty Russell  *
23765dfc1762SRusty Russell  * Returns the size of the vring.  This is mainly used for boasting to
23775dfc1762SRusty Russell  * userspace.  Unlike other operations, this need not be serialized.
23785dfc1762SRusty Russell  */
23798f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
23808f9f4668SRick Jones {
23818f9f4668SRick Jones 
23828f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
23838f9f4668SRick Jones 
23841ce9e605STiwei Bie 	return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
23858f9f4668SRick Jones }
23868f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
23878f9f4668SRick Jones 
2388b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq)
2389b3b32c94SHeinz Graalfs {
2390b3b32c94SHeinz Graalfs 	struct vring_virtqueue *vq = to_vvq(_vq);
2391b3b32c94SHeinz Graalfs 
239260f07798SParav Pandit 	return READ_ONCE(vq->broken);
2393b3b32c94SHeinz Graalfs }
2394b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken);
2395b3b32c94SHeinz Graalfs 
2396e2dcdfe9SRusty Russell /*
2397e2dcdfe9SRusty Russell  * This should prevent the device from being used, allowing drivers to
2398e2dcdfe9SRusty Russell  * recover.  You may need to grab appropriate locks to flush.
2399e2dcdfe9SRusty Russell  */
2400e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev)
2401e2dcdfe9SRusty Russell {
2402e2dcdfe9SRusty Russell 	struct virtqueue *_vq;
2403e2dcdfe9SRusty Russell 
24040e566c8fSParav Pandit 	spin_lock(&dev->vqs_list_lock);
2405e2dcdfe9SRusty Russell 	list_for_each_entry(_vq, &dev->vqs, list) {
2406e2dcdfe9SRusty Russell 		struct vring_virtqueue *vq = to_vvq(_vq);
240760f07798SParav Pandit 
240860f07798SParav Pandit 		/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
240960f07798SParav Pandit 		WRITE_ONCE(vq->broken, true);
2410e2dcdfe9SRusty Russell 	}
24110e566c8fSParav Pandit 	spin_unlock(&dev->vqs_list_lock);
2412e2dcdfe9SRusty Russell }
2413e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device);
2414e2dcdfe9SRusty Russell 
24152a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
241689062652SCornelia Huck {
241789062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
241889062652SCornelia Huck 
24192a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
242089062652SCornelia Huck 
24211ce9e605STiwei Bie 	if (vq->packed_ring)
24221ce9e605STiwei Bie 		return vq->packed.ring_dma_addr;
24231ce9e605STiwei Bie 
2424d79dca75STiwei Bie 	return vq->split.queue_dma_addr;
24252a2d1382SAndy Lutomirski }
24262a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
24272a2d1382SAndy Lutomirski 
24282a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
242989062652SCornelia Huck {
243089062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
243189062652SCornelia Huck 
24322a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
24332a2d1382SAndy Lutomirski 
24341ce9e605STiwei Bie 	if (vq->packed_ring)
24351ce9e605STiwei Bie 		return vq->packed.driver_event_dma_addr;
24361ce9e605STiwei Bie 
2437d79dca75STiwei Bie 	return vq->split.queue_dma_addr +
2438e593bf97STiwei Bie 		((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
243989062652SCornelia Huck }
24402a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
24412a2d1382SAndy Lutomirski 
24422a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
24432a2d1382SAndy Lutomirski {
24442a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq = to_vvq(_vq);
24452a2d1382SAndy Lutomirski 
24462a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
24472a2d1382SAndy Lutomirski 
24481ce9e605STiwei Bie 	if (vq->packed_ring)
24491ce9e605STiwei Bie 		return vq->packed.device_event_dma_addr;
24501ce9e605STiwei Bie 
2451d79dca75STiwei Bie 	return vq->split.queue_dma_addr +
2452e593bf97STiwei Bie 		((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
24532a2d1382SAndy Lutomirski }
24542a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
24552a2d1382SAndy Lutomirski 
24561ce9e605STiwei Bie /* Only available for split ring */
24572a2d1382SAndy Lutomirski const struct vring *virtqueue_get_vring(struct virtqueue *vq)
24582a2d1382SAndy Lutomirski {
2459e593bf97STiwei Bie 	return &to_vvq(vq)->split.vring;
24602a2d1382SAndy Lutomirski }
24612a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_vring);
246289062652SCornelia Huck 
2463c6fd4701SRusty Russell MODULE_LICENSE("GPL");
2464