xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision f7728002c1c7bfa787b276a31c3ef458739b8e7c)
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>
1478fe3987SAndy Lutomirski #include <xen/xen.h>
150a8a69ddSRusty Russell 
160a8a69ddSRusty Russell #ifdef DEBUG
170a8a69ddSRusty Russell /* For development, we want to crash whenever the ring is screwed. */
189499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
199499f5e7SRusty Russell 	do {							\
209499f5e7SRusty Russell 		dev_err(&(_vq)->vq.vdev->dev,			\
219499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
229499f5e7SRusty Russell 		BUG();						\
239499f5e7SRusty Russell 	} while (0)
24c5f841f1SRusty Russell /* Caller is supposed to guarantee no reentry. */
253a35ce7dSRoel Kluin #define START_USE(_vq)						\
26c5f841f1SRusty Russell 	do {							\
27c5f841f1SRusty Russell 		if ((_vq)->in_use)				\
289499f5e7SRusty Russell 			panic("%s:in_use = %i\n",		\
299499f5e7SRusty Russell 			      (_vq)->vq.name, (_vq)->in_use);	\
30c5f841f1SRusty Russell 		(_vq)->in_use = __LINE__;			\
31c5f841f1SRusty Russell 	} while (0)
323a35ce7dSRoel Kluin #define END_USE(_vq) \
3397a545abSRusty Russell 	do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
344d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(_vq)				\
354d6a105eSTiwei Bie 	do {							\
364d6a105eSTiwei Bie 		ktime_t now = ktime_get();			\
374d6a105eSTiwei Bie 								\
384d6a105eSTiwei Bie 		/* No kick or get, with .1 second between?  Warn. */ \
394d6a105eSTiwei Bie 		if ((_vq)->last_add_time_valid)			\
404d6a105eSTiwei Bie 			WARN_ON(ktime_to_ms(ktime_sub(now,	\
414d6a105eSTiwei Bie 				(_vq)->last_add_time)) > 100);	\
424d6a105eSTiwei Bie 		(_vq)->last_add_time = now;			\
434d6a105eSTiwei Bie 		(_vq)->last_add_time_valid = true;		\
444d6a105eSTiwei Bie 	} while (0)
454d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(_vq)				\
464d6a105eSTiwei Bie 	do {							\
474d6a105eSTiwei Bie 		if ((_vq)->last_add_time_valid) {		\
484d6a105eSTiwei Bie 			WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \
494d6a105eSTiwei Bie 				      (_vq)->last_add_time)) > 100); \
504d6a105eSTiwei Bie 		}						\
514d6a105eSTiwei Bie 	} while (0)
524d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(_vq)				\
534d6a105eSTiwei Bie 	((_vq)->last_add_time_valid = false)
540a8a69ddSRusty Russell #else
559499f5e7SRusty Russell #define BAD_RING(_vq, fmt, args...)				\
569499f5e7SRusty Russell 	do {							\
579499f5e7SRusty Russell 		dev_err(&_vq->vq.vdev->dev,			\
589499f5e7SRusty Russell 			"%s:"fmt, (_vq)->vq.name, ##args);	\
599499f5e7SRusty Russell 		(_vq)->broken = true;				\
609499f5e7SRusty Russell 	} while (0)
610a8a69ddSRusty Russell #define START_USE(vq)
620a8a69ddSRusty Russell #define END_USE(vq)
634d6a105eSTiwei Bie #define LAST_ADD_TIME_UPDATE(vq)
644d6a105eSTiwei Bie #define LAST_ADD_TIME_CHECK(vq)
654d6a105eSTiwei Bie #define LAST_ADD_TIME_INVALID(vq)
660a8a69ddSRusty Russell #endif
670a8a69ddSRusty Russell 
68cbeedb72STiwei Bie struct vring_desc_state_split {
69780bc790SAndy Lutomirski 	void *data;			/* Data for callback. */
70780bc790SAndy Lutomirski 	struct vring_desc *indir_desc;	/* Indirect descriptor, if any. */
71780bc790SAndy Lutomirski };
72780bc790SAndy Lutomirski 
731ce9e605STiwei Bie struct vring_desc_state_packed {
741ce9e605STiwei Bie 	void *data;			/* Data for callback. */
751ce9e605STiwei Bie 	struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */
761ce9e605STiwei Bie 	u16 num;			/* Descriptor list length. */
771ce9e605STiwei Bie 	u16 next;			/* The next desc state in a list. */
781ce9e605STiwei Bie 	u16 last;			/* The last desc state in a list. */
791ce9e605STiwei Bie };
801ce9e605STiwei Bie 
811ce9e605STiwei Bie struct vring_desc_extra_packed {
821ce9e605STiwei Bie 	dma_addr_t addr;		/* Buffer DMA addr. */
831ce9e605STiwei Bie 	u32 len;			/* Buffer length. */
841ce9e605STiwei Bie 	u16 flags;			/* Descriptor flags. */
851ce9e605STiwei Bie };
861ce9e605STiwei Bie 
8743b4f721SMichael S. Tsirkin struct vring_virtqueue {
880a8a69ddSRusty Russell 	struct virtqueue vq;
890a8a69ddSRusty Russell 
901ce9e605STiwei Bie 	/* Is this a packed ring? */
911ce9e605STiwei Bie 	bool packed_ring;
921ce9e605STiwei Bie 
93fb3fba6bSTiwei Bie 	/* Is DMA API used? */
94fb3fba6bSTiwei Bie 	bool use_dma_api;
95fb3fba6bSTiwei Bie 
967b21e34fSRusty Russell 	/* Can we use weak barriers? */
977b21e34fSRusty Russell 	bool weak_barriers;
987b21e34fSRusty Russell 
990a8a69ddSRusty Russell 	/* Other side has made a mess, don't try any more. */
1000a8a69ddSRusty Russell 	bool broken;
1010a8a69ddSRusty Russell 
1029fa29b9dSMark McLoughlin 	/* Host supports indirect buffers */
1039fa29b9dSMark McLoughlin 	bool indirect;
1049fa29b9dSMark McLoughlin 
105a5c262c5SMichael S. Tsirkin 	/* Host publishes avail event idx */
106a5c262c5SMichael S. Tsirkin 	bool event;
107a5c262c5SMichael S. Tsirkin 
1080a8a69ddSRusty Russell 	/* Head of free buffer list. */
1090a8a69ddSRusty Russell 	unsigned int free_head;
1100a8a69ddSRusty Russell 	/* Number we've added since last sync. */
1110a8a69ddSRusty Russell 	unsigned int num_added;
1120a8a69ddSRusty Russell 
1130a8a69ddSRusty Russell 	/* Last used index we've seen. */
1141bc4953eSAnthony Liguori 	u16 last_used_idx;
1150a8a69ddSRusty Russell 
1161ce9e605STiwei Bie 	union {
1171ce9e605STiwei Bie 		/* Available for split ring */
118e593bf97STiwei Bie 		struct {
1191ce9e605STiwei Bie 			/* Actual memory layout for this queue. */
120e593bf97STiwei Bie 			struct vring vring;
121e593bf97STiwei Bie 
122f277ec42SVenkatesh Srinivas 			/* Last written value to avail->flags */
123f277ec42SVenkatesh Srinivas 			u16 avail_flags_shadow;
124f277ec42SVenkatesh Srinivas 
1251ce9e605STiwei Bie 			/*
1261ce9e605STiwei Bie 			 * Last written value to avail->idx in
1271ce9e605STiwei Bie 			 * guest byte order.
1281ce9e605STiwei Bie 			 */
129f277ec42SVenkatesh Srinivas 			u16 avail_idx_shadow;
130cbeedb72STiwei Bie 
131cbeedb72STiwei Bie 			/* Per-descriptor state. */
132cbeedb72STiwei Bie 			struct vring_desc_state_split *desc_state;
133d79dca75STiwei Bie 
1341ce9e605STiwei Bie 			/* DMA address and size information */
135d79dca75STiwei Bie 			dma_addr_t queue_dma_addr;
1361ce9e605STiwei Bie 			size_t queue_size_in_bytes;
137e593bf97STiwei Bie 		} split;
138f277ec42SVenkatesh Srinivas 
1391ce9e605STiwei Bie 		/* Available for packed ring */
1401ce9e605STiwei Bie 		struct {
1411ce9e605STiwei Bie 			/* Actual memory layout for this queue. */
1429c0644eeSMichael S. Tsirkin 			struct {
1439c0644eeSMichael S. Tsirkin 				unsigned int num;
1449c0644eeSMichael S. Tsirkin 				struct vring_packed_desc *desc;
1459c0644eeSMichael S. Tsirkin 				struct vring_packed_desc_event *driver;
1469c0644eeSMichael S. Tsirkin 				struct vring_packed_desc_event *device;
1479c0644eeSMichael S. Tsirkin 			} vring;
1481ce9e605STiwei Bie 
1491ce9e605STiwei Bie 			/* Driver ring wrap counter. */
1501ce9e605STiwei Bie 			bool avail_wrap_counter;
1511ce9e605STiwei Bie 
1521ce9e605STiwei Bie 			/* Device ring wrap counter. */
1531ce9e605STiwei Bie 			bool used_wrap_counter;
1541ce9e605STiwei Bie 
1551ce9e605STiwei Bie 			/* Avail used flags. */
1561ce9e605STiwei Bie 			u16 avail_used_flags;
1571ce9e605STiwei Bie 
1581ce9e605STiwei Bie 			/* Index of the next avail descriptor. */
1591ce9e605STiwei Bie 			u16 next_avail_idx;
1601ce9e605STiwei Bie 
1611ce9e605STiwei Bie 			/*
1621ce9e605STiwei Bie 			 * Last written value to driver->flags in
1631ce9e605STiwei Bie 			 * guest byte order.
1641ce9e605STiwei Bie 			 */
1651ce9e605STiwei Bie 			u16 event_flags_shadow;
1661ce9e605STiwei Bie 
1671ce9e605STiwei Bie 			/* Per-descriptor state. */
1681ce9e605STiwei Bie 			struct vring_desc_state_packed *desc_state;
1691ce9e605STiwei Bie 			struct vring_desc_extra_packed *desc_extra;
1701ce9e605STiwei Bie 
1711ce9e605STiwei Bie 			/* DMA address and size information */
1721ce9e605STiwei Bie 			dma_addr_t ring_dma_addr;
1731ce9e605STiwei Bie 			dma_addr_t driver_event_dma_addr;
1741ce9e605STiwei Bie 			dma_addr_t device_event_dma_addr;
1751ce9e605STiwei Bie 			size_t ring_size_in_bytes;
1761ce9e605STiwei Bie 			size_t event_size_in_bytes;
1771ce9e605STiwei Bie 		} packed;
1781ce9e605STiwei Bie 	};
1791ce9e605STiwei Bie 
1800a8a69ddSRusty Russell 	/* How to notify other side. FIXME: commonalize hcalls! */
18146f9c2b9SHeinz Graalfs 	bool (*notify)(struct virtqueue *vq);
1820a8a69ddSRusty Russell 
1832a2d1382SAndy Lutomirski 	/* DMA, allocation, and size information */
1842a2d1382SAndy Lutomirski 	bool we_own_ring;
1852a2d1382SAndy Lutomirski 
1860a8a69ddSRusty Russell #ifdef DEBUG
1870a8a69ddSRusty Russell 	/* They're supposed to lock for us. */
1880a8a69ddSRusty Russell 	unsigned int in_use;
189e93300b1SRusty Russell 
190e93300b1SRusty Russell 	/* Figure out if their kicks are too delayed. */
191e93300b1SRusty Russell 	bool last_add_time_valid;
192e93300b1SRusty Russell 	ktime_t last_add_time;
1930a8a69ddSRusty Russell #endif
1940a8a69ddSRusty Russell };
1950a8a69ddSRusty Russell 
196e6f633e5STiwei Bie 
197e6f633e5STiwei Bie /*
198e6f633e5STiwei Bie  * Helpers.
199e6f633e5STiwei Bie  */
200e6f633e5STiwei Bie 
2010a8a69ddSRusty Russell #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
2020a8a69ddSRusty Russell 
2032f18c2d1STiwei Bie static inline bool virtqueue_use_indirect(struct virtqueue *_vq,
2042f18c2d1STiwei Bie 					  unsigned int total_sg)
2052f18c2d1STiwei Bie {
2062f18c2d1STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
2072f18c2d1STiwei Bie 
2082f18c2d1STiwei Bie 	/*
2092f18c2d1STiwei Bie 	 * If the host supports indirect descriptor tables, and we have multiple
2102f18c2d1STiwei Bie 	 * buffers, then go indirect. FIXME: tune this threshold
2112f18c2d1STiwei Bie 	 */
2122f18c2d1STiwei Bie 	return (vq->indirect && total_sg > 1 && vq->vq.num_free);
2132f18c2d1STiwei Bie }
2142f18c2d1STiwei Bie 
215d26c96c8SAndy Lutomirski /*
2161a937693SMichael S. Tsirkin  * Modern virtio devices have feature bits to specify whether they need a
2171a937693SMichael S. Tsirkin  * quirk and bypass the IOMMU. If not there, just use the DMA API.
2181a937693SMichael S. Tsirkin  *
2191a937693SMichael S. Tsirkin  * If there, the interaction between virtio and DMA API is messy.
220d26c96c8SAndy Lutomirski  *
221d26c96c8SAndy Lutomirski  * On most systems with virtio, physical addresses match bus addresses,
222d26c96c8SAndy Lutomirski  * and it doesn't particularly matter whether we use the DMA API.
223d26c96c8SAndy Lutomirski  *
224d26c96c8SAndy Lutomirski  * On some systems, including Xen and any system with a physical device
225d26c96c8SAndy Lutomirski  * that speaks virtio behind a physical IOMMU, we must use the DMA API
226d26c96c8SAndy Lutomirski  * for virtio DMA to work at all.
227d26c96c8SAndy Lutomirski  *
228d26c96c8SAndy Lutomirski  * On other systems, including SPARC and PPC64, virtio-pci devices are
229d26c96c8SAndy Lutomirski  * enumerated as though they are behind an IOMMU, but the virtio host
230d26c96c8SAndy Lutomirski  * ignores the IOMMU, so we must either pretend that the IOMMU isn't
231d26c96c8SAndy Lutomirski  * there or somehow map everything as the identity.
232d26c96c8SAndy Lutomirski  *
233d26c96c8SAndy Lutomirski  * For the time being, we preserve historic behavior and bypass the DMA
234d26c96c8SAndy Lutomirski  * API.
2351a937693SMichael S. Tsirkin  *
2361a937693SMichael S. Tsirkin  * TODO: install a per-device DMA ops structure that does the right thing
2371a937693SMichael S. Tsirkin  * taking into account all the above quirks, and use the DMA API
2381a937693SMichael S. Tsirkin  * unconditionally on data path.
239d26c96c8SAndy Lutomirski  */
240d26c96c8SAndy Lutomirski 
241d26c96c8SAndy Lutomirski static bool vring_use_dma_api(struct virtio_device *vdev)
242d26c96c8SAndy Lutomirski {
2431a937693SMichael S. Tsirkin 	if (!virtio_has_iommu_quirk(vdev))
2441a937693SMichael S. Tsirkin 		return true;
2451a937693SMichael S. Tsirkin 
2461a937693SMichael S. Tsirkin 	/* Otherwise, we are left to guess. */
24778fe3987SAndy Lutomirski 	/*
24878fe3987SAndy Lutomirski 	 * In theory, it's possible to have a buggy QEMU-supposed
24978fe3987SAndy Lutomirski 	 * emulated Q35 IOMMU and Xen enabled at the same time.  On
25078fe3987SAndy Lutomirski 	 * such a configuration, virtio has never worked and will
25178fe3987SAndy Lutomirski 	 * not work without an even larger kludge.  Instead, enable
25278fe3987SAndy Lutomirski 	 * the DMA API if we're a Xen guest, which at least allows
25378fe3987SAndy Lutomirski 	 * all of the sensible Xen configurations to work correctly.
25478fe3987SAndy Lutomirski 	 */
25578fe3987SAndy Lutomirski 	if (xen_domain())
25678fe3987SAndy Lutomirski 		return true;
25778fe3987SAndy Lutomirski 
258d26c96c8SAndy Lutomirski 	return false;
259d26c96c8SAndy Lutomirski }
260d26c96c8SAndy Lutomirski 
261e6d6dd6cSJoerg Roedel size_t virtio_max_dma_size(struct virtio_device *vdev)
262e6d6dd6cSJoerg Roedel {
263e6d6dd6cSJoerg Roedel 	size_t max_segment_size = SIZE_MAX;
264e6d6dd6cSJoerg Roedel 
265e6d6dd6cSJoerg Roedel 	if (vring_use_dma_api(vdev))
266e6d6dd6cSJoerg Roedel 		max_segment_size = dma_max_mapping_size(&vdev->dev);
267e6d6dd6cSJoerg Roedel 
268e6d6dd6cSJoerg Roedel 	return max_segment_size;
269e6d6dd6cSJoerg Roedel }
270e6d6dd6cSJoerg Roedel EXPORT_SYMBOL_GPL(virtio_max_dma_size);
271e6d6dd6cSJoerg Roedel 
272d79dca75STiwei Bie static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
273d79dca75STiwei Bie 			      dma_addr_t *dma_handle, gfp_t flag)
274d79dca75STiwei Bie {
275d79dca75STiwei Bie 	if (vring_use_dma_api(vdev)) {
276d79dca75STiwei Bie 		return dma_alloc_coherent(vdev->dev.parent, size,
277d79dca75STiwei Bie 					  dma_handle, flag);
278d79dca75STiwei Bie 	} else {
279d79dca75STiwei Bie 		void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
280d79dca75STiwei Bie 
281d79dca75STiwei Bie 		if (queue) {
282d79dca75STiwei Bie 			phys_addr_t phys_addr = virt_to_phys(queue);
283d79dca75STiwei Bie 			*dma_handle = (dma_addr_t)phys_addr;
284d79dca75STiwei Bie 
285d79dca75STiwei Bie 			/*
286d79dca75STiwei Bie 			 * Sanity check: make sure we dind't truncate
287d79dca75STiwei Bie 			 * the address.  The only arches I can find that
288d79dca75STiwei Bie 			 * have 64-bit phys_addr_t but 32-bit dma_addr_t
289d79dca75STiwei Bie 			 * are certain non-highmem MIPS and x86
290d79dca75STiwei Bie 			 * configurations, but these configurations
291d79dca75STiwei Bie 			 * should never allocate physical pages above 32
292d79dca75STiwei Bie 			 * bits, so this is fine.  Just in case, throw a
293d79dca75STiwei Bie 			 * warning and abort if we end up with an
294d79dca75STiwei Bie 			 * unrepresentable address.
295d79dca75STiwei Bie 			 */
296d79dca75STiwei Bie 			if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
297d79dca75STiwei Bie 				free_pages_exact(queue, PAGE_ALIGN(size));
298d79dca75STiwei Bie 				return NULL;
299d79dca75STiwei Bie 			}
300d79dca75STiwei Bie 		}
301d79dca75STiwei Bie 		return queue;
302d79dca75STiwei Bie 	}
303d79dca75STiwei Bie }
304d79dca75STiwei Bie 
305d79dca75STiwei Bie static void vring_free_queue(struct virtio_device *vdev, size_t size,
306d79dca75STiwei Bie 			     void *queue, dma_addr_t dma_handle)
307d79dca75STiwei Bie {
308d79dca75STiwei Bie 	if (vring_use_dma_api(vdev))
309d79dca75STiwei Bie 		dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
310d79dca75STiwei Bie 	else
311d79dca75STiwei Bie 		free_pages_exact(queue, PAGE_ALIGN(size));
312d79dca75STiwei Bie }
313d79dca75STiwei Bie 
314780bc790SAndy Lutomirski /*
315780bc790SAndy Lutomirski  * The DMA ops on various arches are rather gnarly right now, and
316780bc790SAndy Lutomirski  * making all of the arch DMA ops work on the vring device itself
317780bc790SAndy Lutomirski  * is a mess.  For now, we use the parent device for DMA ops.
318780bc790SAndy Lutomirski  */
31975bfa81bSMichael S. Tsirkin static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq)
320780bc790SAndy Lutomirski {
321780bc790SAndy Lutomirski 	return vq->vq.vdev->dev.parent;
322780bc790SAndy Lutomirski }
323780bc790SAndy Lutomirski 
324780bc790SAndy Lutomirski /* Map one sg entry. */
325780bc790SAndy Lutomirski static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
326780bc790SAndy Lutomirski 				   struct scatterlist *sg,
327780bc790SAndy Lutomirski 				   enum dma_data_direction direction)
328780bc790SAndy Lutomirski {
329fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
330780bc790SAndy Lutomirski 		return (dma_addr_t)sg_phys(sg);
331780bc790SAndy Lutomirski 
332780bc790SAndy Lutomirski 	/*
333780bc790SAndy Lutomirski 	 * We can't use dma_map_sg, because we don't use scatterlists in
334780bc790SAndy Lutomirski 	 * the way it expects (we don't guarantee that the scatterlist
335780bc790SAndy Lutomirski 	 * will exist for the lifetime of the mapping).
336780bc790SAndy Lutomirski 	 */
337780bc790SAndy Lutomirski 	return dma_map_page(vring_dma_dev(vq),
338780bc790SAndy Lutomirski 			    sg_page(sg), sg->offset, sg->length,
339780bc790SAndy Lutomirski 			    direction);
340780bc790SAndy Lutomirski }
341780bc790SAndy Lutomirski 
342780bc790SAndy Lutomirski static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
343780bc790SAndy Lutomirski 				   void *cpu_addr, size_t size,
344780bc790SAndy Lutomirski 				   enum dma_data_direction direction)
345780bc790SAndy Lutomirski {
346fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
347780bc790SAndy Lutomirski 		return (dma_addr_t)virt_to_phys(cpu_addr);
348780bc790SAndy Lutomirski 
349780bc790SAndy Lutomirski 	return dma_map_single(vring_dma_dev(vq),
350780bc790SAndy Lutomirski 			      cpu_addr, size, direction);
351780bc790SAndy Lutomirski }
352780bc790SAndy Lutomirski 
353e6f633e5STiwei Bie static int vring_mapping_error(const struct vring_virtqueue *vq,
354e6f633e5STiwei Bie 			       dma_addr_t addr)
355e6f633e5STiwei Bie {
356fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
357e6f633e5STiwei Bie 		return 0;
358e6f633e5STiwei Bie 
359e6f633e5STiwei Bie 	return dma_mapping_error(vring_dma_dev(vq), addr);
360e6f633e5STiwei Bie }
361e6f633e5STiwei Bie 
362e6f633e5STiwei Bie 
363e6f633e5STiwei Bie /*
364e6f633e5STiwei Bie  * Split ring specific functions - *_split().
365e6f633e5STiwei Bie  */
366e6f633e5STiwei Bie 
367138fd251STiwei Bie static void vring_unmap_one_split(const struct vring_virtqueue *vq,
368780bc790SAndy Lutomirski 				  struct vring_desc *desc)
369780bc790SAndy Lutomirski {
370780bc790SAndy Lutomirski 	u16 flags;
371780bc790SAndy Lutomirski 
372fb3fba6bSTiwei Bie 	if (!vq->use_dma_api)
373780bc790SAndy Lutomirski 		return;
374780bc790SAndy Lutomirski 
375780bc790SAndy Lutomirski 	flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
376780bc790SAndy Lutomirski 
377780bc790SAndy Lutomirski 	if (flags & VRING_DESC_F_INDIRECT) {
378780bc790SAndy Lutomirski 		dma_unmap_single(vring_dma_dev(vq),
379780bc790SAndy Lutomirski 				 virtio64_to_cpu(vq->vq.vdev, desc->addr),
380780bc790SAndy Lutomirski 				 virtio32_to_cpu(vq->vq.vdev, desc->len),
381780bc790SAndy Lutomirski 				 (flags & VRING_DESC_F_WRITE) ?
382780bc790SAndy Lutomirski 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
383780bc790SAndy Lutomirski 	} else {
384780bc790SAndy Lutomirski 		dma_unmap_page(vring_dma_dev(vq),
385780bc790SAndy Lutomirski 			       virtio64_to_cpu(vq->vq.vdev, desc->addr),
386780bc790SAndy Lutomirski 			       virtio32_to_cpu(vq->vq.vdev, desc->len),
387780bc790SAndy Lutomirski 			       (flags & VRING_DESC_F_WRITE) ?
388780bc790SAndy Lutomirski 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
389780bc790SAndy Lutomirski 	}
390780bc790SAndy Lutomirski }
391780bc790SAndy Lutomirski 
392138fd251STiwei Bie static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
393138fd251STiwei Bie 					       unsigned int total_sg,
394138fd251STiwei Bie 					       gfp_t gfp)
3959fa29b9dSMark McLoughlin {
3969fa29b9dSMark McLoughlin 	struct vring_desc *desc;
397b25bd251SRusty Russell 	unsigned int i;
3989fa29b9dSMark McLoughlin 
399b92b1b89SWill Deacon 	/*
400b92b1b89SWill Deacon 	 * We require lowmem mappings for the descriptors because
401b92b1b89SWill Deacon 	 * otherwise virt_to_phys will give us bogus addresses in the
402b92b1b89SWill Deacon 	 * virtqueue.
403b92b1b89SWill Deacon 	 */
40482107539SMichal Hocko 	gfp &= ~__GFP_HIGHMEM;
405b92b1b89SWill Deacon 
4066da2ec56SKees Cook 	desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
4079fa29b9dSMark McLoughlin 	if (!desc)
408b25bd251SRusty Russell 		return NULL;
4099fa29b9dSMark McLoughlin 
410b25bd251SRusty Russell 	for (i = 0; i < total_sg; i++)
41100e6f3d9SMichael S. Tsirkin 		desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
412b25bd251SRusty Russell 	return desc;
4139fa29b9dSMark McLoughlin }
4149fa29b9dSMark McLoughlin 
415138fd251STiwei Bie static inline int virtqueue_add_split(struct virtqueue *_vq,
41613816c76SRusty Russell 				      struct scatterlist *sgs[],
417eeebf9b1SRusty Russell 				      unsigned int total_sg,
41813816c76SRusty Russell 				      unsigned int out_sgs,
41913816c76SRusty Russell 				      unsigned int in_sgs,
420bbd603efSMichael S. Tsirkin 				      void *data,
4215a08b04fSMichael S. Tsirkin 				      void *ctx,
422bbd603efSMichael S. Tsirkin 				      gfp_t gfp)
4230a8a69ddSRusty Russell {
4240a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
42513816c76SRusty Russell 	struct scatterlist *sg;
426b25bd251SRusty Russell 	struct vring_desc *desc;
427780bc790SAndy Lutomirski 	unsigned int i, n, avail, descs_used, uninitialized_var(prev), err_idx;
4281fe9b6feSMichael S. Tsirkin 	int head;
429b25bd251SRusty Russell 	bool indirect;
4300a8a69ddSRusty Russell 
4319fa29b9dSMark McLoughlin 	START_USE(vq);
4329fa29b9dSMark McLoughlin 
4330a8a69ddSRusty Russell 	BUG_ON(data == NULL);
4345a08b04fSMichael S. Tsirkin 	BUG_ON(ctx && vq->indirect);
4359fa29b9dSMark McLoughlin 
43670670444SRusty Russell 	if (unlikely(vq->broken)) {
43770670444SRusty Russell 		END_USE(vq);
43870670444SRusty Russell 		return -EIO;
43970670444SRusty Russell 	}
44070670444SRusty Russell 
4414d6a105eSTiwei Bie 	LAST_ADD_TIME_UPDATE(vq);
442e93300b1SRusty Russell 
44313816c76SRusty Russell 	BUG_ON(total_sg == 0);
4440a8a69ddSRusty Russell 
445b25bd251SRusty Russell 	head = vq->free_head;
446b25bd251SRusty Russell 
4472f18c2d1STiwei Bie 	if (virtqueue_use_indirect(_vq, total_sg))
448138fd251STiwei Bie 		desc = alloc_indirect_split(_vq, total_sg, gfp);
44944ed8089SRichard W.M. Jones 	else {
450b25bd251SRusty Russell 		desc = NULL;
451e593bf97STiwei Bie 		WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
45244ed8089SRichard W.M. Jones 	}
453b25bd251SRusty Russell 
454b25bd251SRusty Russell 	if (desc) {
455b25bd251SRusty Russell 		/* Use a single buffer which doesn't continue */
456780bc790SAndy Lutomirski 		indirect = true;
457b25bd251SRusty Russell 		/* Set up rest to use this indirect table. */
458b25bd251SRusty Russell 		i = 0;
459b25bd251SRusty Russell 		descs_used = 1;
460b25bd251SRusty Russell 	} else {
461780bc790SAndy Lutomirski 		indirect = false;
462e593bf97STiwei Bie 		desc = vq->split.vring.desc;
463b25bd251SRusty Russell 		i = head;
464b25bd251SRusty Russell 		descs_used = total_sg;
465b25bd251SRusty Russell 	}
466b25bd251SRusty Russell 
467b25bd251SRusty Russell 	if (vq->vq.num_free < descs_used) {
4680a8a69ddSRusty Russell 		pr_debug("Can't add buf len %i - avail = %i\n",
469b25bd251SRusty Russell 			 descs_used, vq->vq.num_free);
47044653eaeSRusty Russell 		/* FIXME: for historical reasons, we force a notify here if
47144653eaeSRusty Russell 		 * there are outgoing parts to the buffer.  Presumably the
47244653eaeSRusty Russell 		 * host should service the ring ASAP. */
47313816c76SRusty Russell 		if (out_sgs)
474426e3e0aSRusty Russell 			vq->notify(&vq->vq);
47558625edfSWei Yongjun 		if (indirect)
47658625edfSWei Yongjun 			kfree(desc);
4770a8a69ddSRusty Russell 		END_USE(vq);
4780a8a69ddSRusty Russell 		return -ENOSPC;
4790a8a69ddSRusty Russell 	}
4800a8a69ddSRusty Russell 
48113816c76SRusty Russell 	for (n = 0; n < out_sgs; n++) {
482eeebf9b1SRusty Russell 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
483780bc790SAndy Lutomirski 			dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
484780bc790SAndy Lutomirski 			if (vring_mapping_error(vq, addr))
485780bc790SAndy Lutomirski 				goto unmap_release;
486780bc790SAndy Lutomirski 
48700e6f3d9SMichael S. Tsirkin 			desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT);
488780bc790SAndy Lutomirski 			desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
48900e6f3d9SMichael S. Tsirkin 			desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
4900a8a69ddSRusty Russell 			prev = i;
49100e6f3d9SMichael S. Tsirkin 			i = virtio16_to_cpu(_vq->vdev, desc[i].next);
4920a8a69ddSRusty Russell 		}
49313816c76SRusty Russell 	}
49413816c76SRusty Russell 	for (; n < (out_sgs + in_sgs); n++) {
495eeebf9b1SRusty Russell 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
496780bc790SAndy Lutomirski 			dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
497780bc790SAndy Lutomirski 			if (vring_mapping_error(vq, addr))
498780bc790SAndy Lutomirski 				goto unmap_release;
499780bc790SAndy Lutomirski 
50000e6f3d9SMichael S. Tsirkin 			desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE);
501780bc790SAndy Lutomirski 			desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
50200e6f3d9SMichael S. Tsirkin 			desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
5030a8a69ddSRusty Russell 			prev = i;
50400e6f3d9SMichael S. Tsirkin 			i = virtio16_to_cpu(_vq->vdev, desc[i].next);
50513816c76SRusty Russell 		}
5060a8a69ddSRusty Russell 	}
5070a8a69ddSRusty Russell 	/* Last one doesn't continue. */
50800e6f3d9SMichael S. Tsirkin 	desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
5090a8a69ddSRusty Russell 
510780bc790SAndy Lutomirski 	if (indirect) {
511780bc790SAndy Lutomirski 		/* Now that the indirect table is filled in, map it. */
512780bc790SAndy Lutomirski 		dma_addr_t addr = vring_map_single(
513780bc790SAndy Lutomirski 			vq, desc, total_sg * sizeof(struct vring_desc),
514780bc790SAndy Lutomirski 			DMA_TO_DEVICE);
515780bc790SAndy Lutomirski 		if (vring_mapping_error(vq, addr))
516780bc790SAndy Lutomirski 			goto unmap_release;
517780bc790SAndy Lutomirski 
518e593bf97STiwei Bie 		vq->split.vring.desc[head].flags = cpu_to_virtio16(_vq->vdev,
519e593bf97STiwei Bie 				VRING_DESC_F_INDIRECT);
520e593bf97STiwei Bie 		vq->split.vring.desc[head].addr = cpu_to_virtio64(_vq->vdev,
521e593bf97STiwei Bie 				addr);
522780bc790SAndy Lutomirski 
523e593bf97STiwei Bie 		vq->split.vring.desc[head].len = cpu_to_virtio32(_vq->vdev,
524e593bf97STiwei Bie 				total_sg * sizeof(struct vring_desc));
525780bc790SAndy Lutomirski 	}
526780bc790SAndy Lutomirski 
527780bc790SAndy Lutomirski 	/* We're using some buffers from the free list. */
528780bc790SAndy Lutomirski 	vq->vq.num_free -= descs_used;
529780bc790SAndy Lutomirski 
5300a8a69ddSRusty Russell 	/* Update free pointer */
531b25bd251SRusty Russell 	if (indirect)
532e593bf97STiwei Bie 		vq->free_head = virtio16_to_cpu(_vq->vdev,
533e593bf97STiwei Bie 					vq->split.vring.desc[head].next);
534b25bd251SRusty Russell 	else
5350a8a69ddSRusty Russell 		vq->free_head = i;
5360a8a69ddSRusty Russell 
537780bc790SAndy Lutomirski 	/* Store token and indirect buffer state. */
538cbeedb72STiwei Bie 	vq->split.desc_state[head].data = data;
539780bc790SAndy Lutomirski 	if (indirect)
540cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = desc;
54187646a34SJason Wang 	else
542cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = ctx;
5430a8a69ddSRusty Russell 
5440a8a69ddSRusty Russell 	/* Put entry in available array (but don't update avail->idx until they
5453b720b8cSRusty Russell 	 * do sync). */
546e593bf97STiwei Bie 	avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
547e593bf97STiwei Bie 	vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
5480a8a69ddSRusty Russell 
549ee7cd898SRusty Russell 	/* Descriptors and available array need to be set before we expose the
550ee7cd898SRusty Russell 	 * new available array entries. */
551a9a0fef7SRusty Russell 	virtio_wmb(vq->weak_barriers);
552e593bf97STiwei Bie 	vq->split.avail_idx_shadow++;
553e593bf97STiwei Bie 	vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
554e593bf97STiwei Bie 						vq->split.avail_idx_shadow);
555ee7cd898SRusty Russell 	vq->num_added++;
556ee7cd898SRusty Russell 
5575e05bf58STetsuo Handa 	pr_debug("Added buffer head %i to %p\n", head, vq);
5585e05bf58STetsuo Handa 	END_USE(vq);
5595e05bf58STetsuo Handa 
560ee7cd898SRusty Russell 	/* This is very unlikely, but theoretically possible.  Kick
561ee7cd898SRusty Russell 	 * just in case. */
562ee7cd898SRusty Russell 	if (unlikely(vq->num_added == (1 << 16) - 1))
563ee7cd898SRusty Russell 		virtqueue_kick(_vq);
564ee7cd898SRusty Russell 
56598e8c6bcSRusty Russell 	return 0;
566780bc790SAndy Lutomirski 
567780bc790SAndy Lutomirski unmap_release:
568780bc790SAndy Lutomirski 	err_idx = i;
569cf8f1696SMatthias Lange 
570cf8f1696SMatthias Lange 	if (indirect)
571cf8f1696SMatthias Lange 		i = 0;
572cf8f1696SMatthias Lange 	else
573780bc790SAndy Lutomirski 		i = head;
574780bc790SAndy Lutomirski 
575780bc790SAndy Lutomirski 	for (n = 0; n < total_sg; n++) {
576780bc790SAndy Lutomirski 		if (i == err_idx)
577780bc790SAndy Lutomirski 			break;
578138fd251STiwei Bie 		vring_unmap_one_split(vq, &desc[i]);
579cf8f1696SMatthias Lange 		i = virtio16_to_cpu(_vq->vdev, desc[i].next);
580780bc790SAndy Lutomirski 	}
581780bc790SAndy Lutomirski 
582780bc790SAndy Lutomirski 	if (indirect)
583780bc790SAndy Lutomirski 		kfree(desc);
584780bc790SAndy Lutomirski 
5853cc36f6eSMichael S. Tsirkin 	END_USE(vq);
586*f7728002SHalil Pasic 	return -ENOMEM;
5870a8a69ddSRusty Russell }
58813816c76SRusty Russell 
589138fd251STiwei Bie static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
5900a8a69ddSRusty Russell {
5910a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
592a5c262c5SMichael S. Tsirkin 	u16 new, old;
59341f0377fSRusty Russell 	bool needs_kick;
59441f0377fSRusty Russell 
5950a8a69ddSRusty Russell 	START_USE(vq);
596a72caae2SJason Wang 	/* We need to expose available array entries before checking avail
597a72caae2SJason Wang 	 * event. */
598a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
5990a8a69ddSRusty Russell 
600e593bf97STiwei Bie 	old = vq->split.avail_idx_shadow - vq->num_added;
601e593bf97STiwei Bie 	new = vq->split.avail_idx_shadow;
6020a8a69ddSRusty Russell 	vq->num_added = 0;
6030a8a69ddSRusty Russell 
6044d6a105eSTiwei Bie 	LAST_ADD_TIME_CHECK(vq);
6054d6a105eSTiwei Bie 	LAST_ADD_TIME_INVALID(vq);
606e93300b1SRusty Russell 
60741f0377fSRusty Russell 	if (vq->event) {
608e593bf97STiwei Bie 		needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
609e593bf97STiwei Bie 					vring_avail_event(&vq->split.vring)),
61041f0377fSRusty Russell 					      new, old);
61141f0377fSRusty Russell 	} else {
612e593bf97STiwei Bie 		needs_kick = !(vq->split.vring.used->flags &
613e593bf97STiwei Bie 					cpu_to_virtio16(_vq->vdev,
614e593bf97STiwei Bie 						VRING_USED_F_NO_NOTIFY));
61541f0377fSRusty Russell 	}
6160a8a69ddSRusty Russell 	END_USE(vq);
61741f0377fSRusty Russell 	return needs_kick;
61841f0377fSRusty Russell }
619138fd251STiwei Bie 
620138fd251STiwei Bie static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
6215a08b04fSMichael S. Tsirkin 			     void **ctx)
6220a8a69ddSRusty Russell {
623780bc790SAndy Lutomirski 	unsigned int i, j;
624c60923cbSGonglei 	__virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
6250a8a69ddSRusty Russell 
6260a8a69ddSRusty Russell 	/* Clear data ptr. */
627cbeedb72STiwei Bie 	vq->split.desc_state[head].data = NULL;
6280a8a69ddSRusty Russell 
629780bc790SAndy Lutomirski 	/* Put back on free list: unmap first-level descriptors and find end */
6300a8a69ddSRusty Russell 	i = head;
6319fa29b9dSMark McLoughlin 
632e593bf97STiwei Bie 	while (vq->split.vring.desc[i].flags & nextflag) {
633e593bf97STiwei Bie 		vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
634e593bf97STiwei Bie 		i = virtio16_to_cpu(vq->vq.vdev, vq->split.vring.desc[i].next);
63506ca287dSRusty Russell 		vq->vq.num_free++;
6360a8a69ddSRusty Russell 	}
6370a8a69ddSRusty Russell 
638e593bf97STiwei Bie 	vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
639e593bf97STiwei Bie 	vq->split.vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev,
640e593bf97STiwei Bie 						vq->free_head);
6410a8a69ddSRusty Russell 	vq->free_head = head;
642780bc790SAndy Lutomirski 
6430a8a69ddSRusty Russell 	/* Plus final descriptor */
64406ca287dSRusty Russell 	vq->vq.num_free++;
645780bc790SAndy Lutomirski 
6465a08b04fSMichael S. Tsirkin 	if (vq->indirect) {
647cbeedb72STiwei Bie 		struct vring_desc *indir_desc =
648cbeedb72STiwei Bie 				vq->split.desc_state[head].indir_desc;
6495a08b04fSMichael S. Tsirkin 		u32 len;
6505a08b04fSMichael S. Tsirkin 
6515a08b04fSMichael S. Tsirkin 		/* Free the indirect table, if any, now that it's unmapped. */
6525a08b04fSMichael S. Tsirkin 		if (!indir_desc)
6535a08b04fSMichael S. Tsirkin 			return;
6545a08b04fSMichael S. Tsirkin 
655e593bf97STiwei Bie 		len = virtio32_to_cpu(vq->vq.vdev,
656e593bf97STiwei Bie 				vq->split.vring.desc[head].len);
657780bc790SAndy Lutomirski 
658e593bf97STiwei Bie 		BUG_ON(!(vq->split.vring.desc[head].flags &
659780bc790SAndy Lutomirski 			 cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)));
660780bc790SAndy Lutomirski 		BUG_ON(len == 0 || len % sizeof(struct vring_desc));
661780bc790SAndy Lutomirski 
662780bc790SAndy Lutomirski 		for (j = 0; j < len / sizeof(struct vring_desc); j++)
663138fd251STiwei Bie 			vring_unmap_one_split(vq, &indir_desc[j]);
664780bc790SAndy Lutomirski 
6655a08b04fSMichael S. Tsirkin 		kfree(indir_desc);
666cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = NULL;
6675a08b04fSMichael S. Tsirkin 	} else if (ctx) {
668cbeedb72STiwei Bie 		*ctx = vq->split.desc_state[head].indir_desc;
669780bc790SAndy Lutomirski 	}
6700a8a69ddSRusty Russell }
6710a8a69ddSRusty Russell 
672138fd251STiwei Bie static inline bool more_used_split(const struct vring_virtqueue *vq)
6730a8a69ddSRusty Russell {
674e593bf97STiwei Bie 	return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
675e593bf97STiwei Bie 			vq->split.vring.used->idx);
6760a8a69ddSRusty Russell }
6770a8a69ddSRusty Russell 
678138fd251STiwei Bie static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
679138fd251STiwei Bie 					 unsigned int *len,
6805a08b04fSMichael S. Tsirkin 					 void **ctx)
6810a8a69ddSRusty Russell {
6820a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
6830a8a69ddSRusty Russell 	void *ret;
6840a8a69ddSRusty Russell 	unsigned int i;
6853b720b8cSRusty Russell 	u16 last_used;
6860a8a69ddSRusty Russell 
6870a8a69ddSRusty Russell 	START_USE(vq);
6880a8a69ddSRusty Russell 
6895ef82752SRusty Russell 	if (unlikely(vq->broken)) {
6905ef82752SRusty Russell 		END_USE(vq);
6915ef82752SRusty Russell 		return NULL;
6925ef82752SRusty Russell 	}
6935ef82752SRusty Russell 
694138fd251STiwei Bie 	if (!more_used_split(vq)) {
6950a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
6960a8a69ddSRusty Russell 		END_USE(vq);
6970a8a69ddSRusty Russell 		return NULL;
6980a8a69ddSRusty Russell 	}
6990a8a69ddSRusty Russell 
7002d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
701a9a0fef7SRusty Russell 	virtio_rmb(vq->weak_barriers);
7022d61ba95SMichael S. Tsirkin 
703e593bf97STiwei Bie 	last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
704e593bf97STiwei Bie 	i = virtio32_to_cpu(_vq->vdev,
705e593bf97STiwei Bie 			vq->split.vring.used->ring[last_used].id);
706e593bf97STiwei Bie 	*len = virtio32_to_cpu(_vq->vdev,
707e593bf97STiwei Bie 			vq->split.vring.used->ring[last_used].len);
7080a8a69ddSRusty Russell 
709e593bf97STiwei Bie 	if (unlikely(i >= vq->split.vring.num)) {
7100a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
7110a8a69ddSRusty Russell 		return NULL;
7120a8a69ddSRusty Russell 	}
713cbeedb72STiwei Bie 	if (unlikely(!vq->split.desc_state[i].data)) {
7140a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
7150a8a69ddSRusty Russell 		return NULL;
7160a8a69ddSRusty Russell 	}
7170a8a69ddSRusty Russell 
718138fd251STiwei Bie 	/* detach_buf_split clears data, so grab it now. */
719cbeedb72STiwei Bie 	ret = vq->split.desc_state[i].data;
720138fd251STiwei Bie 	detach_buf_split(vq, i, ctx);
7210a8a69ddSRusty Russell 	vq->last_used_idx++;
722a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
723a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
724a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
725e593bf97STiwei Bie 	if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
726788e5b3aSMichael S. Tsirkin 		virtio_store_mb(vq->weak_barriers,
727e593bf97STiwei Bie 				&vring_used_event(&vq->split.vring),
728788e5b3aSMichael S. Tsirkin 				cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
729a5c262c5SMichael S. Tsirkin 
7304d6a105eSTiwei Bie 	LAST_ADD_TIME_INVALID(vq);
731e93300b1SRusty Russell 
7320a8a69ddSRusty Russell 	END_USE(vq);
7330a8a69ddSRusty Russell 	return ret;
7340a8a69ddSRusty Russell }
735138fd251STiwei Bie 
736138fd251STiwei Bie static void virtqueue_disable_cb_split(struct virtqueue *_vq)
737138fd251STiwei Bie {
738138fd251STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
739138fd251STiwei Bie 
740e593bf97STiwei Bie 	if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
741e593bf97STiwei Bie 		vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
742138fd251STiwei Bie 		if (!vq->event)
743e593bf97STiwei Bie 			vq->split.vring.avail->flags =
744e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
745e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
746138fd251STiwei Bie 	}
747138fd251STiwei Bie }
748138fd251STiwei Bie 
749138fd251STiwei Bie static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
750cc229884SMichael S. Tsirkin {
751cc229884SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
752cc229884SMichael S. Tsirkin 	u16 last_used_idx;
753cc229884SMichael S. Tsirkin 
754cc229884SMichael S. Tsirkin 	START_USE(vq);
755cc229884SMichael S. Tsirkin 
756cc229884SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
757cc229884SMichael S. Tsirkin 	 * more to do. */
758cc229884SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
759cc229884SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
760cc229884SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
761e593bf97STiwei Bie 	if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
762e593bf97STiwei Bie 		vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
7630ea1e4a6SLadi Prosek 		if (!vq->event)
764e593bf97STiwei Bie 			vq->split.vring.avail->flags =
765e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
766e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
767f277ec42SVenkatesh Srinivas 	}
768e593bf97STiwei Bie 	vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
769e593bf97STiwei Bie 			last_used_idx = vq->last_used_idx);
770cc229884SMichael S. Tsirkin 	END_USE(vq);
771cc229884SMichael S. Tsirkin 	return last_used_idx;
772cc229884SMichael S. Tsirkin }
773138fd251STiwei Bie 
774138fd251STiwei Bie static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx)
775138fd251STiwei Bie {
776138fd251STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
777138fd251STiwei Bie 
778138fd251STiwei Bie 	return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
779e593bf97STiwei Bie 			vq->split.vring.used->idx);
780138fd251STiwei Bie }
781138fd251STiwei Bie 
782138fd251STiwei Bie static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
7837ab358c2SMichael S. Tsirkin {
7847ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
7857ab358c2SMichael S. Tsirkin 	u16 bufs;
7867ab358c2SMichael S. Tsirkin 
7877ab358c2SMichael S. Tsirkin 	START_USE(vq);
7887ab358c2SMichael S. Tsirkin 
7897ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
7907ab358c2SMichael S. Tsirkin 	 * more to do. */
7917ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
7927ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
7930ea1e4a6SLadi Prosek 	 * entry. Always update the event index to keep code simple. */
794e593bf97STiwei Bie 	if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
795e593bf97STiwei Bie 		vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
7960ea1e4a6SLadi Prosek 		if (!vq->event)
797e593bf97STiwei Bie 			vq->split.vring.avail->flags =
798e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
799e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
800f277ec42SVenkatesh Srinivas 	}
8017ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
802e593bf97STiwei Bie 	bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
803788e5b3aSMichael S. Tsirkin 
804788e5b3aSMichael S. Tsirkin 	virtio_store_mb(vq->weak_barriers,
805e593bf97STiwei Bie 			&vring_used_event(&vq->split.vring),
806788e5b3aSMichael S. Tsirkin 			cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
807788e5b3aSMichael S. Tsirkin 
808e593bf97STiwei Bie 	if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
809e593bf97STiwei Bie 					- vq->last_used_idx) > bufs)) {
8107ab358c2SMichael S. Tsirkin 		END_USE(vq);
8117ab358c2SMichael S. Tsirkin 		return false;
8127ab358c2SMichael S. Tsirkin 	}
8137ab358c2SMichael S. Tsirkin 
8147ab358c2SMichael S. Tsirkin 	END_USE(vq);
8157ab358c2SMichael S. Tsirkin 	return true;
8167ab358c2SMichael S. Tsirkin }
8177ab358c2SMichael S. Tsirkin 
818138fd251STiwei Bie static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
819c021eac4SShirley Ma {
820c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
821c021eac4SShirley Ma 	unsigned int i;
822c021eac4SShirley Ma 	void *buf;
823c021eac4SShirley Ma 
824c021eac4SShirley Ma 	START_USE(vq);
825c021eac4SShirley Ma 
826e593bf97STiwei Bie 	for (i = 0; i < vq->split.vring.num; i++) {
827cbeedb72STiwei Bie 		if (!vq->split.desc_state[i].data)
828c021eac4SShirley Ma 			continue;
829138fd251STiwei Bie 		/* detach_buf_split clears data, so grab it now. */
830cbeedb72STiwei Bie 		buf = vq->split.desc_state[i].data;
831138fd251STiwei Bie 		detach_buf_split(vq, i, NULL);
832e593bf97STiwei Bie 		vq->split.avail_idx_shadow--;
833e593bf97STiwei Bie 		vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
834e593bf97STiwei Bie 				vq->split.avail_idx_shadow);
835c021eac4SShirley Ma 		END_USE(vq);
836c021eac4SShirley Ma 		return buf;
837c021eac4SShirley Ma 	}
838c021eac4SShirley Ma 	/* That should have freed everything. */
839e593bf97STiwei Bie 	BUG_ON(vq->vq.num_free != vq->split.vring.num);
840c021eac4SShirley Ma 
841c021eac4SShirley Ma 	END_USE(vq);
842c021eac4SShirley Ma 	return NULL;
843c021eac4SShirley Ma }
844138fd251STiwei Bie 
845d79dca75STiwei Bie static struct virtqueue *vring_create_virtqueue_split(
846d79dca75STiwei Bie 	unsigned int index,
847d79dca75STiwei Bie 	unsigned int num,
848d79dca75STiwei Bie 	unsigned int vring_align,
849d79dca75STiwei Bie 	struct virtio_device *vdev,
850d79dca75STiwei Bie 	bool weak_barriers,
851d79dca75STiwei Bie 	bool may_reduce_num,
852d79dca75STiwei Bie 	bool context,
853d79dca75STiwei Bie 	bool (*notify)(struct virtqueue *),
854d79dca75STiwei Bie 	void (*callback)(struct virtqueue *),
855d79dca75STiwei Bie 	const char *name)
856d79dca75STiwei Bie {
857d79dca75STiwei Bie 	struct virtqueue *vq;
858d79dca75STiwei Bie 	void *queue = NULL;
859d79dca75STiwei Bie 	dma_addr_t dma_addr;
860d79dca75STiwei Bie 	size_t queue_size_in_bytes;
861d79dca75STiwei Bie 	struct vring vring;
862d79dca75STiwei Bie 
863d79dca75STiwei Bie 	/* We assume num is a power of 2. */
864d79dca75STiwei Bie 	if (num & (num - 1)) {
865d79dca75STiwei Bie 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
866d79dca75STiwei Bie 		return NULL;
867d79dca75STiwei Bie 	}
868d79dca75STiwei Bie 
869d79dca75STiwei Bie 	/* TODO: allocate each queue chunk individually */
870d79dca75STiwei Bie 	for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
871d79dca75STiwei Bie 		queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
872d79dca75STiwei Bie 					  &dma_addr,
873d79dca75STiwei Bie 					  GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
874d79dca75STiwei Bie 		if (queue)
875d79dca75STiwei Bie 			break;
876cf94db21SCornelia Huck 		if (!may_reduce_num)
877cf94db21SCornelia Huck 			return NULL;
878d79dca75STiwei Bie 	}
879d79dca75STiwei Bie 
880d79dca75STiwei Bie 	if (!num)
881d79dca75STiwei Bie 		return NULL;
882d79dca75STiwei Bie 
883d79dca75STiwei Bie 	if (!queue) {
884d79dca75STiwei Bie 		/* Try to get a single page. You are my only hope! */
885d79dca75STiwei Bie 		queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
886d79dca75STiwei Bie 					  &dma_addr, GFP_KERNEL|__GFP_ZERO);
887d79dca75STiwei Bie 	}
888d79dca75STiwei Bie 	if (!queue)
889d79dca75STiwei Bie 		return NULL;
890d79dca75STiwei Bie 
891d79dca75STiwei Bie 	queue_size_in_bytes = vring_size(num, vring_align);
892d79dca75STiwei Bie 	vring_init(&vring, num, queue, vring_align);
893d79dca75STiwei Bie 
894d79dca75STiwei Bie 	vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
895d79dca75STiwei Bie 				   notify, callback, name);
896d79dca75STiwei Bie 	if (!vq) {
897d79dca75STiwei Bie 		vring_free_queue(vdev, queue_size_in_bytes, queue,
898d79dca75STiwei Bie 				 dma_addr);
899d79dca75STiwei Bie 		return NULL;
900d79dca75STiwei Bie 	}
901d79dca75STiwei Bie 
902d79dca75STiwei Bie 	to_vvq(vq)->split.queue_dma_addr = dma_addr;
903d79dca75STiwei Bie 	to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes;
904d79dca75STiwei Bie 	to_vvq(vq)->we_own_ring = true;
905d79dca75STiwei Bie 
906d79dca75STiwei Bie 	return vq;
907d79dca75STiwei Bie }
908d79dca75STiwei Bie 
909e6f633e5STiwei Bie 
910e6f633e5STiwei Bie /*
9111ce9e605STiwei Bie  * Packed ring specific functions - *_packed().
9121ce9e605STiwei Bie  */
9131ce9e605STiwei Bie 
9141ce9e605STiwei Bie static void vring_unmap_state_packed(const struct vring_virtqueue *vq,
9151ce9e605STiwei Bie 				     struct vring_desc_extra_packed *state)
9161ce9e605STiwei Bie {
9171ce9e605STiwei Bie 	u16 flags;
9181ce9e605STiwei Bie 
9191ce9e605STiwei Bie 	if (!vq->use_dma_api)
9201ce9e605STiwei Bie 		return;
9211ce9e605STiwei Bie 
9221ce9e605STiwei Bie 	flags = state->flags;
9231ce9e605STiwei Bie 
9241ce9e605STiwei Bie 	if (flags & VRING_DESC_F_INDIRECT) {
9251ce9e605STiwei Bie 		dma_unmap_single(vring_dma_dev(vq),
9261ce9e605STiwei Bie 				 state->addr, state->len,
9271ce9e605STiwei Bie 				 (flags & VRING_DESC_F_WRITE) ?
9281ce9e605STiwei Bie 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
9291ce9e605STiwei Bie 	} else {
9301ce9e605STiwei Bie 		dma_unmap_page(vring_dma_dev(vq),
9311ce9e605STiwei Bie 			       state->addr, state->len,
9321ce9e605STiwei Bie 			       (flags & VRING_DESC_F_WRITE) ?
9331ce9e605STiwei Bie 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
9341ce9e605STiwei Bie 	}
9351ce9e605STiwei Bie }
9361ce9e605STiwei Bie 
9371ce9e605STiwei Bie static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
9381ce9e605STiwei Bie 				   struct vring_packed_desc *desc)
9391ce9e605STiwei Bie {
9401ce9e605STiwei Bie 	u16 flags;
9411ce9e605STiwei Bie 
9421ce9e605STiwei Bie 	if (!vq->use_dma_api)
9431ce9e605STiwei Bie 		return;
9441ce9e605STiwei Bie 
9451ce9e605STiwei Bie 	flags = le16_to_cpu(desc->flags);
9461ce9e605STiwei Bie 
9471ce9e605STiwei Bie 	if (flags & VRING_DESC_F_INDIRECT) {
9481ce9e605STiwei Bie 		dma_unmap_single(vring_dma_dev(vq),
9491ce9e605STiwei Bie 				 le64_to_cpu(desc->addr),
9501ce9e605STiwei Bie 				 le32_to_cpu(desc->len),
9511ce9e605STiwei Bie 				 (flags & VRING_DESC_F_WRITE) ?
9521ce9e605STiwei Bie 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
9531ce9e605STiwei Bie 	} else {
9541ce9e605STiwei Bie 		dma_unmap_page(vring_dma_dev(vq),
9551ce9e605STiwei Bie 			       le64_to_cpu(desc->addr),
9561ce9e605STiwei Bie 			       le32_to_cpu(desc->len),
9571ce9e605STiwei Bie 			       (flags & VRING_DESC_F_WRITE) ?
9581ce9e605STiwei Bie 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
9591ce9e605STiwei Bie 	}
9601ce9e605STiwei Bie }
9611ce9e605STiwei Bie 
9621ce9e605STiwei Bie static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
9631ce9e605STiwei Bie 						       gfp_t gfp)
9641ce9e605STiwei Bie {
9651ce9e605STiwei Bie 	struct vring_packed_desc *desc;
9661ce9e605STiwei Bie 
9671ce9e605STiwei Bie 	/*
9681ce9e605STiwei Bie 	 * We require lowmem mappings for the descriptors because
9691ce9e605STiwei Bie 	 * otherwise virt_to_phys will give us bogus addresses in the
9701ce9e605STiwei Bie 	 * virtqueue.
9711ce9e605STiwei Bie 	 */
9721ce9e605STiwei Bie 	gfp &= ~__GFP_HIGHMEM;
9731ce9e605STiwei Bie 
9741ce9e605STiwei Bie 	desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
9751ce9e605STiwei Bie 
9761ce9e605STiwei Bie 	return desc;
9771ce9e605STiwei Bie }
9781ce9e605STiwei Bie 
9791ce9e605STiwei Bie static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
9801ce9e605STiwei Bie 				       struct scatterlist *sgs[],
9811ce9e605STiwei Bie 				       unsigned int total_sg,
9821ce9e605STiwei Bie 				       unsigned int out_sgs,
9831ce9e605STiwei Bie 				       unsigned int in_sgs,
9841ce9e605STiwei Bie 				       void *data,
9851ce9e605STiwei Bie 				       gfp_t gfp)
9861ce9e605STiwei Bie {
9871ce9e605STiwei Bie 	struct vring_packed_desc *desc;
9881ce9e605STiwei Bie 	struct scatterlist *sg;
9891ce9e605STiwei Bie 	unsigned int i, n, err_idx;
9901ce9e605STiwei Bie 	u16 head, id;
9911ce9e605STiwei Bie 	dma_addr_t addr;
9921ce9e605STiwei Bie 
9931ce9e605STiwei Bie 	head = vq->packed.next_avail_idx;
9941ce9e605STiwei Bie 	desc = alloc_indirect_packed(total_sg, gfp);
9951ce9e605STiwei Bie 
9961ce9e605STiwei Bie 	if (unlikely(vq->vq.num_free < 1)) {
9971ce9e605STiwei Bie 		pr_debug("Can't add buf len 1 - avail = 0\n");
998df0bfe75SYueHaibing 		kfree(desc);
9991ce9e605STiwei Bie 		END_USE(vq);
10001ce9e605STiwei Bie 		return -ENOSPC;
10011ce9e605STiwei Bie 	}
10021ce9e605STiwei Bie 
10031ce9e605STiwei Bie 	i = 0;
10041ce9e605STiwei Bie 	id = vq->free_head;
10051ce9e605STiwei Bie 	BUG_ON(id == vq->packed.vring.num);
10061ce9e605STiwei Bie 
10071ce9e605STiwei Bie 	for (n = 0; n < out_sgs + in_sgs; n++) {
10081ce9e605STiwei Bie 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
10091ce9e605STiwei Bie 			addr = vring_map_one_sg(vq, sg, n < out_sgs ?
10101ce9e605STiwei Bie 					DMA_TO_DEVICE : DMA_FROM_DEVICE);
10111ce9e605STiwei Bie 			if (vring_mapping_error(vq, addr))
10121ce9e605STiwei Bie 				goto unmap_release;
10131ce9e605STiwei Bie 
10141ce9e605STiwei Bie 			desc[i].flags = cpu_to_le16(n < out_sgs ?
10151ce9e605STiwei Bie 						0 : VRING_DESC_F_WRITE);
10161ce9e605STiwei Bie 			desc[i].addr = cpu_to_le64(addr);
10171ce9e605STiwei Bie 			desc[i].len = cpu_to_le32(sg->length);
10181ce9e605STiwei Bie 			i++;
10191ce9e605STiwei Bie 		}
10201ce9e605STiwei Bie 	}
10211ce9e605STiwei Bie 
10221ce9e605STiwei Bie 	/* Now that the indirect table is filled in, map it. */
10231ce9e605STiwei Bie 	addr = vring_map_single(vq, desc,
10241ce9e605STiwei Bie 			total_sg * sizeof(struct vring_packed_desc),
10251ce9e605STiwei Bie 			DMA_TO_DEVICE);
10261ce9e605STiwei Bie 	if (vring_mapping_error(vq, addr))
10271ce9e605STiwei Bie 		goto unmap_release;
10281ce9e605STiwei Bie 
10291ce9e605STiwei Bie 	vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
10301ce9e605STiwei Bie 	vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
10311ce9e605STiwei Bie 				sizeof(struct vring_packed_desc));
10321ce9e605STiwei Bie 	vq->packed.vring.desc[head].id = cpu_to_le16(id);
10331ce9e605STiwei Bie 
10341ce9e605STiwei Bie 	if (vq->use_dma_api) {
10351ce9e605STiwei Bie 		vq->packed.desc_extra[id].addr = addr;
10361ce9e605STiwei Bie 		vq->packed.desc_extra[id].len = total_sg *
10371ce9e605STiwei Bie 				sizeof(struct vring_packed_desc);
10381ce9e605STiwei Bie 		vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
10391ce9e605STiwei Bie 						  vq->packed.avail_used_flags;
10401ce9e605STiwei Bie 	}
10411ce9e605STiwei Bie 
10421ce9e605STiwei Bie 	/*
10431ce9e605STiwei Bie 	 * A driver MUST NOT make the first descriptor in the list
10441ce9e605STiwei Bie 	 * available before all subsequent descriptors comprising
10451ce9e605STiwei Bie 	 * the list are made available.
10461ce9e605STiwei Bie 	 */
10471ce9e605STiwei Bie 	virtio_wmb(vq->weak_barriers);
10481ce9e605STiwei Bie 	vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
10491ce9e605STiwei Bie 						vq->packed.avail_used_flags);
10501ce9e605STiwei Bie 
10511ce9e605STiwei Bie 	/* We're using some buffers from the free list. */
10521ce9e605STiwei Bie 	vq->vq.num_free -= 1;
10531ce9e605STiwei Bie 
10541ce9e605STiwei Bie 	/* Update free pointer */
10551ce9e605STiwei Bie 	n = head + 1;
10561ce9e605STiwei Bie 	if (n >= vq->packed.vring.num) {
10571ce9e605STiwei Bie 		n = 0;
10581ce9e605STiwei Bie 		vq->packed.avail_wrap_counter ^= 1;
10591ce9e605STiwei Bie 		vq->packed.avail_used_flags ^=
10601ce9e605STiwei Bie 				1 << VRING_PACKED_DESC_F_AVAIL |
10611ce9e605STiwei Bie 				1 << VRING_PACKED_DESC_F_USED;
10621ce9e605STiwei Bie 	}
10631ce9e605STiwei Bie 	vq->packed.next_avail_idx = n;
10641ce9e605STiwei Bie 	vq->free_head = vq->packed.desc_state[id].next;
10651ce9e605STiwei Bie 
10661ce9e605STiwei Bie 	/* Store token and indirect buffer state. */
10671ce9e605STiwei Bie 	vq->packed.desc_state[id].num = 1;
10681ce9e605STiwei Bie 	vq->packed.desc_state[id].data = data;
10691ce9e605STiwei Bie 	vq->packed.desc_state[id].indir_desc = desc;
10701ce9e605STiwei Bie 	vq->packed.desc_state[id].last = id;
10711ce9e605STiwei Bie 
10721ce9e605STiwei Bie 	vq->num_added += 1;
10731ce9e605STiwei Bie 
10741ce9e605STiwei Bie 	pr_debug("Added buffer head %i to %p\n", head, vq);
10751ce9e605STiwei Bie 	END_USE(vq);
10761ce9e605STiwei Bie 
10771ce9e605STiwei Bie 	return 0;
10781ce9e605STiwei Bie 
10791ce9e605STiwei Bie unmap_release:
10801ce9e605STiwei Bie 	err_idx = i;
10811ce9e605STiwei Bie 
10821ce9e605STiwei Bie 	for (i = 0; i < err_idx; i++)
10831ce9e605STiwei Bie 		vring_unmap_desc_packed(vq, &desc[i]);
10841ce9e605STiwei Bie 
10851ce9e605STiwei Bie 	kfree(desc);
10861ce9e605STiwei Bie 
10871ce9e605STiwei Bie 	END_USE(vq);
1088*f7728002SHalil Pasic 	return -ENOMEM;
10891ce9e605STiwei Bie }
10901ce9e605STiwei Bie 
10911ce9e605STiwei Bie static inline int virtqueue_add_packed(struct virtqueue *_vq,
10921ce9e605STiwei Bie 				       struct scatterlist *sgs[],
10931ce9e605STiwei Bie 				       unsigned int total_sg,
10941ce9e605STiwei Bie 				       unsigned int out_sgs,
10951ce9e605STiwei Bie 				       unsigned int in_sgs,
10961ce9e605STiwei Bie 				       void *data,
10971ce9e605STiwei Bie 				       void *ctx,
10981ce9e605STiwei Bie 				       gfp_t gfp)
10991ce9e605STiwei Bie {
11001ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
11011ce9e605STiwei Bie 	struct vring_packed_desc *desc;
11021ce9e605STiwei Bie 	struct scatterlist *sg;
11031ce9e605STiwei Bie 	unsigned int i, n, c, descs_used, err_idx;
11041ce9e605STiwei Bie 	__le16 uninitialized_var(head_flags), flags;
11051ce9e605STiwei Bie 	u16 head, id, uninitialized_var(prev), curr, avail_used_flags;
11061ce9e605STiwei Bie 
11071ce9e605STiwei Bie 	START_USE(vq);
11081ce9e605STiwei Bie 
11091ce9e605STiwei Bie 	BUG_ON(data == NULL);
11101ce9e605STiwei Bie 	BUG_ON(ctx && vq->indirect);
11111ce9e605STiwei Bie 
11121ce9e605STiwei Bie 	if (unlikely(vq->broken)) {
11131ce9e605STiwei Bie 		END_USE(vq);
11141ce9e605STiwei Bie 		return -EIO;
11151ce9e605STiwei Bie 	}
11161ce9e605STiwei Bie 
11171ce9e605STiwei Bie 	LAST_ADD_TIME_UPDATE(vq);
11181ce9e605STiwei Bie 
11191ce9e605STiwei Bie 	BUG_ON(total_sg == 0);
11201ce9e605STiwei Bie 
11211ce9e605STiwei Bie 	if (virtqueue_use_indirect(_vq, total_sg))
11221ce9e605STiwei Bie 		return virtqueue_add_indirect_packed(vq, sgs, total_sg,
11231ce9e605STiwei Bie 				out_sgs, in_sgs, data, gfp);
11241ce9e605STiwei Bie 
11251ce9e605STiwei Bie 	head = vq->packed.next_avail_idx;
11261ce9e605STiwei Bie 	avail_used_flags = vq->packed.avail_used_flags;
11271ce9e605STiwei Bie 
11281ce9e605STiwei Bie 	WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
11291ce9e605STiwei Bie 
11301ce9e605STiwei Bie 	desc = vq->packed.vring.desc;
11311ce9e605STiwei Bie 	i = head;
11321ce9e605STiwei Bie 	descs_used = total_sg;
11331ce9e605STiwei Bie 
11341ce9e605STiwei Bie 	if (unlikely(vq->vq.num_free < descs_used)) {
11351ce9e605STiwei Bie 		pr_debug("Can't add buf len %i - avail = %i\n",
11361ce9e605STiwei Bie 			 descs_used, vq->vq.num_free);
11371ce9e605STiwei Bie 		END_USE(vq);
11381ce9e605STiwei Bie 		return -ENOSPC;
11391ce9e605STiwei Bie 	}
11401ce9e605STiwei Bie 
11411ce9e605STiwei Bie 	id = vq->free_head;
11421ce9e605STiwei Bie 	BUG_ON(id == vq->packed.vring.num);
11431ce9e605STiwei Bie 
11441ce9e605STiwei Bie 	curr = id;
11451ce9e605STiwei Bie 	c = 0;
11461ce9e605STiwei Bie 	for (n = 0; n < out_sgs + in_sgs; n++) {
11471ce9e605STiwei Bie 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
11481ce9e605STiwei Bie 			dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ?
11491ce9e605STiwei Bie 					DMA_TO_DEVICE : DMA_FROM_DEVICE);
11501ce9e605STiwei Bie 			if (vring_mapping_error(vq, addr))
11511ce9e605STiwei Bie 				goto unmap_release;
11521ce9e605STiwei Bie 
11531ce9e605STiwei Bie 			flags = cpu_to_le16(vq->packed.avail_used_flags |
11541ce9e605STiwei Bie 				    (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
11551ce9e605STiwei Bie 				    (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
11561ce9e605STiwei Bie 			if (i == head)
11571ce9e605STiwei Bie 				head_flags = flags;
11581ce9e605STiwei Bie 			else
11591ce9e605STiwei Bie 				desc[i].flags = flags;
11601ce9e605STiwei Bie 
11611ce9e605STiwei Bie 			desc[i].addr = cpu_to_le64(addr);
11621ce9e605STiwei Bie 			desc[i].len = cpu_to_le32(sg->length);
11631ce9e605STiwei Bie 			desc[i].id = cpu_to_le16(id);
11641ce9e605STiwei Bie 
11651ce9e605STiwei Bie 			if (unlikely(vq->use_dma_api)) {
11661ce9e605STiwei Bie 				vq->packed.desc_extra[curr].addr = addr;
11671ce9e605STiwei Bie 				vq->packed.desc_extra[curr].len = sg->length;
11681ce9e605STiwei Bie 				vq->packed.desc_extra[curr].flags =
11691ce9e605STiwei Bie 					le16_to_cpu(flags);
11701ce9e605STiwei Bie 			}
11711ce9e605STiwei Bie 			prev = curr;
11721ce9e605STiwei Bie 			curr = vq->packed.desc_state[curr].next;
11731ce9e605STiwei Bie 
11741ce9e605STiwei Bie 			if ((unlikely(++i >= vq->packed.vring.num))) {
11751ce9e605STiwei Bie 				i = 0;
11761ce9e605STiwei Bie 				vq->packed.avail_used_flags ^=
11771ce9e605STiwei Bie 					1 << VRING_PACKED_DESC_F_AVAIL |
11781ce9e605STiwei Bie 					1 << VRING_PACKED_DESC_F_USED;
11791ce9e605STiwei Bie 			}
11801ce9e605STiwei Bie 		}
11811ce9e605STiwei Bie 	}
11821ce9e605STiwei Bie 
11831ce9e605STiwei Bie 	if (i < head)
11841ce9e605STiwei Bie 		vq->packed.avail_wrap_counter ^= 1;
11851ce9e605STiwei Bie 
11861ce9e605STiwei Bie 	/* We're using some buffers from the free list. */
11871ce9e605STiwei Bie 	vq->vq.num_free -= descs_used;
11881ce9e605STiwei Bie 
11891ce9e605STiwei Bie 	/* Update free pointer */
11901ce9e605STiwei Bie 	vq->packed.next_avail_idx = i;
11911ce9e605STiwei Bie 	vq->free_head = curr;
11921ce9e605STiwei Bie 
11931ce9e605STiwei Bie 	/* Store token. */
11941ce9e605STiwei Bie 	vq->packed.desc_state[id].num = descs_used;
11951ce9e605STiwei Bie 	vq->packed.desc_state[id].data = data;
11961ce9e605STiwei Bie 	vq->packed.desc_state[id].indir_desc = ctx;
11971ce9e605STiwei Bie 	vq->packed.desc_state[id].last = prev;
11981ce9e605STiwei Bie 
11991ce9e605STiwei Bie 	/*
12001ce9e605STiwei Bie 	 * A driver MUST NOT make the first descriptor in the list
12011ce9e605STiwei Bie 	 * available before all subsequent descriptors comprising
12021ce9e605STiwei Bie 	 * the list are made available.
12031ce9e605STiwei Bie 	 */
12041ce9e605STiwei Bie 	virtio_wmb(vq->weak_barriers);
12051ce9e605STiwei Bie 	vq->packed.vring.desc[head].flags = head_flags;
12061ce9e605STiwei Bie 	vq->num_added += descs_used;
12071ce9e605STiwei Bie 
12081ce9e605STiwei Bie 	pr_debug("Added buffer head %i to %p\n", head, vq);
12091ce9e605STiwei Bie 	END_USE(vq);
12101ce9e605STiwei Bie 
12111ce9e605STiwei Bie 	return 0;
12121ce9e605STiwei Bie 
12131ce9e605STiwei Bie unmap_release:
12141ce9e605STiwei Bie 	err_idx = i;
12151ce9e605STiwei Bie 	i = head;
12161ce9e605STiwei Bie 
12171ce9e605STiwei Bie 	vq->packed.avail_used_flags = avail_used_flags;
12181ce9e605STiwei Bie 
12191ce9e605STiwei Bie 	for (n = 0; n < total_sg; n++) {
12201ce9e605STiwei Bie 		if (i == err_idx)
12211ce9e605STiwei Bie 			break;
12221ce9e605STiwei Bie 		vring_unmap_desc_packed(vq, &desc[i]);
12231ce9e605STiwei Bie 		i++;
12241ce9e605STiwei Bie 		if (i >= vq->packed.vring.num)
12251ce9e605STiwei Bie 			i = 0;
12261ce9e605STiwei Bie 	}
12271ce9e605STiwei Bie 
12281ce9e605STiwei Bie 	END_USE(vq);
12291ce9e605STiwei Bie 	return -EIO;
12301ce9e605STiwei Bie }
12311ce9e605STiwei Bie 
12321ce9e605STiwei Bie static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
12331ce9e605STiwei Bie {
12341ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1235f51f9826STiwei Bie 	u16 new, old, off_wrap, flags, wrap_counter, event_idx;
12361ce9e605STiwei Bie 	bool needs_kick;
12371ce9e605STiwei Bie 	union {
12381ce9e605STiwei Bie 		struct {
12391ce9e605STiwei Bie 			__le16 off_wrap;
12401ce9e605STiwei Bie 			__le16 flags;
12411ce9e605STiwei Bie 		};
12421ce9e605STiwei Bie 		u32 u32;
12431ce9e605STiwei Bie 	} snapshot;
12441ce9e605STiwei Bie 
12451ce9e605STiwei Bie 	START_USE(vq);
12461ce9e605STiwei Bie 
12471ce9e605STiwei Bie 	/*
12481ce9e605STiwei Bie 	 * We need to expose the new flags value before checking notification
12491ce9e605STiwei Bie 	 * suppressions.
12501ce9e605STiwei Bie 	 */
12511ce9e605STiwei Bie 	virtio_mb(vq->weak_barriers);
12521ce9e605STiwei Bie 
1253f51f9826STiwei Bie 	old = vq->packed.next_avail_idx - vq->num_added;
1254f51f9826STiwei Bie 	new = vq->packed.next_avail_idx;
12551ce9e605STiwei Bie 	vq->num_added = 0;
12561ce9e605STiwei Bie 
12571ce9e605STiwei Bie 	snapshot.u32 = *(u32 *)vq->packed.vring.device;
12581ce9e605STiwei Bie 	flags = le16_to_cpu(snapshot.flags);
12591ce9e605STiwei Bie 
12601ce9e605STiwei Bie 	LAST_ADD_TIME_CHECK(vq);
12611ce9e605STiwei Bie 	LAST_ADD_TIME_INVALID(vq);
12621ce9e605STiwei Bie 
1263f51f9826STiwei Bie 	if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
12641ce9e605STiwei Bie 		needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1265f51f9826STiwei Bie 		goto out;
1266f51f9826STiwei Bie 	}
1267f51f9826STiwei Bie 
1268f51f9826STiwei Bie 	off_wrap = le16_to_cpu(snapshot.off_wrap);
1269f51f9826STiwei Bie 
1270f51f9826STiwei Bie 	wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1271f51f9826STiwei Bie 	event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1272f51f9826STiwei Bie 	if (wrap_counter != vq->packed.avail_wrap_counter)
1273f51f9826STiwei Bie 		event_idx -= vq->packed.vring.num;
1274f51f9826STiwei Bie 
1275f51f9826STiwei Bie 	needs_kick = vring_need_event(event_idx, new, old);
1276f51f9826STiwei Bie out:
12771ce9e605STiwei Bie 	END_USE(vq);
12781ce9e605STiwei Bie 	return needs_kick;
12791ce9e605STiwei Bie }
12801ce9e605STiwei Bie 
12811ce9e605STiwei Bie static void detach_buf_packed(struct vring_virtqueue *vq,
12821ce9e605STiwei Bie 			      unsigned int id, void **ctx)
12831ce9e605STiwei Bie {
12841ce9e605STiwei Bie 	struct vring_desc_state_packed *state = NULL;
12851ce9e605STiwei Bie 	struct vring_packed_desc *desc;
12861ce9e605STiwei Bie 	unsigned int i, curr;
12871ce9e605STiwei Bie 
12881ce9e605STiwei Bie 	state = &vq->packed.desc_state[id];
12891ce9e605STiwei Bie 
12901ce9e605STiwei Bie 	/* Clear data ptr. */
12911ce9e605STiwei Bie 	state->data = NULL;
12921ce9e605STiwei Bie 
12931ce9e605STiwei Bie 	vq->packed.desc_state[state->last].next = vq->free_head;
12941ce9e605STiwei Bie 	vq->free_head = id;
12951ce9e605STiwei Bie 	vq->vq.num_free += state->num;
12961ce9e605STiwei Bie 
12971ce9e605STiwei Bie 	if (unlikely(vq->use_dma_api)) {
12981ce9e605STiwei Bie 		curr = id;
12991ce9e605STiwei Bie 		for (i = 0; i < state->num; i++) {
13001ce9e605STiwei Bie 			vring_unmap_state_packed(vq,
13011ce9e605STiwei Bie 				&vq->packed.desc_extra[curr]);
13021ce9e605STiwei Bie 			curr = vq->packed.desc_state[curr].next;
13031ce9e605STiwei Bie 		}
13041ce9e605STiwei Bie 	}
13051ce9e605STiwei Bie 
13061ce9e605STiwei Bie 	if (vq->indirect) {
13071ce9e605STiwei Bie 		u32 len;
13081ce9e605STiwei Bie 
13091ce9e605STiwei Bie 		/* Free the indirect table, if any, now that it's unmapped. */
13101ce9e605STiwei Bie 		desc = state->indir_desc;
13111ce9e605STiwei Bie 		if (!desc)
13121ce9e605STiwei Bie 			return;
13131ce9e605STiwei Bie 
13141ce9e605STiwei Bie 		if (vq->use_dma_api) {
13151ce9e605STiwei Bie 			len = vq->packed.desc_extra[id].len;
13161ce9e605STiwei Bie 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
13171ce9e605STiwei Bie 					i++)
13181ce9e605STiwei Bie 				vring_unmap_desc_packed(vq, &desc[i]);
13191ce9e605STiwei Bie 		}
13201ce9e605STiwei Bie 		kfree(desc);
13211ce9e605STiwei Bie 		state->indir_desc = NULL;
13221ce9e605STiwei Bie 	} else if (ctx) {
13231ce9e605STiwei Bie 		*ctx = state->indir_desc;
13241ce9e605STiwei Bie 	}
13251ce9e605STiwei Bie }
13261ce9e605STiwei Bie 
13271ce9e605STiwei Bie static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
13281ce9e605STiwei Bie 				       u16 idx, bool used_wrap_counter)
13291ce9e605STiwei Bie {
13301ce9e605STiwei Bie 	bool avail, used;
13311ce9e605STiwei Bie 	u16 flags;
13321ce9e605STiwei Bie 
13331ce9e605STiwei Bie 	flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
13341ce9e605STiwei Bie 	avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
13351ce9e605STiwei Bie 	used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
13361ce9e605STiwei Bie 
13371ce9e605STiwei Bie 	return avail == used && used == used_wrap_counter;
13381ce9e605STiwei Bie }
13391ce9e605STiwei Bie 
13401ce9e605STiwei Bie static inline bool more_used_packed(const struct vring_virtqueue *vq)
13411ce9e605STiwei Bie {
13421ce9e605STiwei Bie 	return is_used_desc_packed(vq, vq->last_used_idx,
13431ce9e605STiwei Bie 			vq->packed.used_wrap_counter);
13441ce9e605STiwei Bie }
13451ce9e605STiwei Bie 
13461ce9e605STiwei Bie static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
13471ce9e605STiwei Bie 					  unsigned int *len,
13481ce9e605STiwei Bie 					  void **ctx)
13491ce9e605STiwei Bie {
13501ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
13511ce9e605STiwei Bie 	u16 last_used, id;
13521ce9e605STiwei Bie 	void *ret;
13531ce9e605STiwei Bie 
13541ce9e605STiwei Bie 	START_USE(vq);
13551ce9e605STiwei Bie 
13561ce9e605STiwei Bie 	if (unlikely(vq->broken)) {
13571ce9e605STiwei Bie 		END_USE(vq);
13581ce9e605STiwei Bie 		return NULL;
13591ce9e605STiwei Bie 	}
13601ce9e605STiwei Bie 
13611ce9e605STiwei Bie 	if (!more_used_packed(vq)) {
13621ce9e605STiwei Bie 		pr_debug("No more buffers in queue\n");
13631ce9e605STiwei Bie 		END_USE(vq);
13641ce9e605STiwei Bie 		return NULL;
13651ce9e605STiwei Bie 	}
13661ce9e605STiwei Bie 
13671ce9e605STiwei Bie 	/* Only get used elements after they have been exposed by host. */
13681ce9e605STiwei Bie 	virtio_rmb(vq->weak_barriers);
13691ce9e605STiwei Bie 
13701ce9e605STiwei Bie 	last_used = vq->last_used_idx;
13711ce9e605STiwei Bie 	id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
13721ce9e605STiwei Bie 	*len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
13731ce9e605STiwei Bie 
13741ce9e605STiwei Bie 	if (unlikely(id >= vq->packed.vring.num)) {
13751ce9e605STiwei Bie 		BAD_RING(vq, "id %u out of range\n", id);
13761ce9e605STiwei Bie 		return NULL;
13771ce9e605STiwei Bie 	}
13781ce9e605STiwei Bie 	if (unlikely(!vq->packed.desc_state[id].data)) {
13791ce9e605STiwei Bie 		BAD_RING(vq, "id %u is not a head!\n", id);
13801ce9e605STiwei Bie 		return NULL;
13811ce9e605STiwei Bie 	}
13821ce9e605STiwei Bie 
13831ce9e605STiwei Bie 	/* detach_buf_packed clears data, so grab it now. */
13841ce9e605STiwei Bie 	ret = vq->packed.desc_state[id].data;
13851ce9e605STiwei Bie 	detach_buf_packed(vq, id, ctx);
13861ce9e605STiwei Bie 
13871ce9e605STiwei Bie 	vq->last_used_idx += vq->packed.desc_state[id].num;
13881ce9e605STiwei Bie 	if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) {
13891ce9e605STiwei Bie 		vq->last_used_idx -= vq->packed.vring.num;
13901ce9e605STiwei Bie 		vq->packed.used_wrap_counter ^= 1;
13911ce9e605STiwei Bie 	}
13921ce9e605STiwei Bie 
1393f51f9826STiwei Bie 	/*
1394f51f9826STiwei Bie 	 * If we expect an interrupt for the next entry, tell host
1395f51f9826STiwei Bie 	 * by writing event index and flush out the write before
1396f51f9826STiwei Bie 	 * the read in the next get_buf call.
1397f51f9826STiwei Bie 	 */
1398f51f9826STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1399f51f9826STiwei Bie 		virtio_store_mb(vq->weak_barriers,
1400f51f9826STiwei Bie 				&vq->packed.vring.driver->off_wrap,
1401f51f9826STiwei Bie 				cpu_to_le16(vq->last_used_idx |
1402f51f9826STiwei Bie 					(vq->packed.used_wrap_counter <<
1403f51f9826STiwei Bie 					 VRING_PACKED_EVENT_F_WRAP_CTR)));
1404f51f9826STiwei Bie 
14051ce9e605STiwei Bie 	LAST_ADD_TIME_INVALID(vq);
14061ce9e605STiwei Bie 
14071ce9e605STiwei Bie 	END_USE(vq);
14081ce9e605STiwei Bie 	return ret;
14091ce9e605STiwei Bie }
14101ce9e605STiwei Bie 
14111ce9e605STiwei Bie static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
14121ce9e605STiwei Bie {
14131ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14141ce9e605STiwei Bie 
14151ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
14161ce9e605STiwei Bie 		vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
14171ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
14181ce9e605STiwei Bie 			cpu_to_le16(vq->packed.event_flags_shadow);
14191ce9e605STiwei Bie 	}
14201ce9e605STiwei Bie }
14211ce9e605STiwei Bie 
14221ce9e605STiwei Bie static unsigned virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
14231ce9e605STiwei Bie {
14241ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14251ce9e605STiwei Bie 
14261ce9e605STiwei Bie 	START_USE(vq);
14271ce9e605STiwei Bie 
14281ce9e605STiwei Bie 	/*
14291ce9e605STiwei Bie 	 * We optimistically turn back on interrupts, then check if there was
14301ce9e605STiwei Bie 	 * more to do.
14311ce9e605STiwei Bie 	 */
14321ce9e605STiwei Bie 
1433f51f9826STiwei Bie 	if (vq->event) {
1434f51f9826STiwei Bie 		vq->packed.vring.driver->off_wrap =
1435f51f9826STiwei Bie 			cpu_to_le16(vq->last_used_idx |
1436f51f9826STiwei Bie 				(vq->packed.used_wrap_counter <<
1437f51f9826STiwei Bie 				 VRING_PACKED_EVENT_F_WRAP_CTR));
1438f51f9826STiwei Bie 		/*
1439f51f9826STiwei Bie 		 * We need to update event offset and event wrap
1440f51f9826STiwei Bie 		 * counter first before updating event flags.
1441f51f9826STiwei Bie 		 */
1442f51f9826STiwei Bie 		virtio_wmb(vq->weak_barriers);
1443f51f9826STiwei Bie 	}
1444f51f9826STiwei Bie 
14451ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1446f51f9826STiwei Bie 		vq->packed.event_flags_shadow = vq->event ?
1447f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_DESC :
1448f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_ENABLE;
14491ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
14501ce9e605STiwei Bie 				cpu_to_le16(vq->packed.event_flags_shadow);
14511ce9e605STiwei Bie 	}
14521ce9e605STiwei Bie 
14531ce9e605STiwei Bie 	END_USE(vq);
14541ce9e605STiwei Bie 	return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter <<
14551ce9e605STiwei Bie 			VRING_PACKED_EVENT_F_WRAP_CTR);
14561ce9e605STiwei Bie }
14571ce9e605STiwei Bie 
14581ce9e605STiwei Bie static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
14591ce9e605STiwei Bie {
14601ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14611ce9e605STiwei Bie 	bool wrap_counter;
14621ce9e605STiwei Bie 	u16 used_idx;
14631ce9e605STiwei Bie 
14641ce9e605STiwei Bie 	wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
14651ce9e605STiwei Bie 	used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
14661ce9e605STiwei Bie 
14671ce9e605STiwei Bie 	return is_used_desc_packed(vq, used_idx, wrap_counter);
14681ce9e605STiwei Bie }
14691ce9e605STiwei Bie 
14701ce9e605STiwei Bie static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
14711ce9e605STiwei Bie {
14721ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14731ce9e605STiwei Bie 	u16 used_idx, wrap_counter;
1474f51f9826STiwei Bie 	u16 bufs;
14751ce9e605STiwei Bie 
14761ce9e605STiwei Bie 	START_USE(vq);
14771ce9e605STiwei Bie 
14781ce9e605STiwei Bie 	/*
14791ce9e605STiwei Bie 	 * We optimistically turn back on interrupts, then check if there was
14801ce9e605STiwei Bie 	 * more to do.
14811ce9e605STiwei Bie 	 */
14821ce9e605STiwei Bie 
1483f51f9826STiwei Bie 	if (vq->event) {
1484f51f9826STiwei Bie 		/* TODO: tune this threshold */
1485f51f9826STiwei Bie 		bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
14861ce9e605STiwei Bie 		wrap_counter = vq->packed.used_wrap_counter;
14871ce9e605STiwei Bie 
1488f51f9826STiwei Bie 		used_idx = vq->last_used_idx + bufs;
1489f51f9826STiwei Bie 		if (used_idx >= vq->packed.vring.num) {
1490f51f9826STiwei Bie 			used_idx -= vq->packed.vring.num;
1491f51f9826STiwei Bie 			wrap_counter ^= 1;
1492f51f9826STiwei Bie 		}
1493f51f9826STiwei Bie 
1494f51f9826STiwei Bie 		vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1495f51f9826STiwei Bie 			(wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1496f51f9826STiwei Bie 
1497f51f9826STiwei Bie 		/*
1498f51f9826STiwei Bie 		 * We need to update event offset and event wrap
1499f51f9826STiwei Bie 		 * counter first before updating event flags.
1500f51f9826STiwei Bie 		 */
1501f51f9826STiwei Bie 		virtio_wmb(vq->weak_barriers);
1502f51f9826STiwei Bie 	}
1503f51f9826STiwei Bie 
15041ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1505f51f9826STiwei Bie 		vq->packed.event_flags_shadow = vq->event ?
1506f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_DESC :
1507f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_ENABLE;
15081ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
15091ce9e605STiwei Bie 				cpu_to_le16(vq->packed.event_flags_shadow);
15101ce9e605STiwei Bie 	}
15111ce9e605STiwei Bie 
15121ce9e605STiwei Bie 	/*
15131ce9e605STiwei Bie 	 * We need to update event suppression structure first
15141ce9e605STiwei Bie 	 * before re-checking for more used buffers.
15151ce9e605STiwei Bie 	 */
15161ce9e605STiwei Bie 	virtio_mb(vq->weak_barriers);
15171ce9e605STiwei Bie 
151840ce7919SMarvin Liu 	if (is_used_desc_packed(vq,
151940ce7919SMarvin Liu 				vq->last_used_idx,
152040ce7919SMarvin Liu 				vq->packed.used_wrap_counter)) {
15211ce9e605STiwei Bie 		END_USE(vq);
15221ce9e605STiwei Bie 		return false;
15231ce9e605STiwei Bie 	}
15241ce9e605STiwei Bie 
15251ce9e605STiwei Bie 	END_USE(vq);
15261ce9e605STiwei Bie 	return true;
15271ce9e605STiwei Bie }
15281ce9e605STiwei Bie 
15291ce9e605STiwei Bie static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
15301ce9e605STiwei Bie {
15311ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
15321ce9e605STiwei Bie 	unsigned int i;
15331ce9e605STiwei Bie 	void *buf;
15341ce9e605STiwei Bie 
15351ce9e605STiwei Bie 	START_USE(vq);
15361ce9e605STiwei Bie 
15371ce9e605STiwei Bie 	for (i = 0; i < vq->packed.vring.num; i++) {
15381ce9e605STiwei Bie 		if (!vq->packed.desc_state[i].data)
15391ce9e605STiwei Bie 			continue;
15401ce9e605STiwei Bie 		/* detach_buf clears data, so grab it now. */
15411ce9e605STiwei Bie 		buf = vq->packed.desc_state[i].data;
15421ce9e605STiwei Bie 		detach_buf_packed(vq, i, NULL);
15431ce9e605STiwei Bie 		END_USE(vq);
15441ce9e605STiwei Bie 		return buf;
15451ce9e605STiwei Bie 	}
15461ce9e605STiwei Bie 	/* That should have freed everything. */
15471ce9e605STiwei Bie 	BUG_ON(vq->vq.num_free != vq->packed.vring.num);
15481ce9e605STiwei Bie 
15491ce9e605STiwei Bie 	END_USE(vq);
15501ce9e605STiwei Bie 	return NULL;
15511ce9e605STiwei Bie }
15521ce9e605STiwei Bie 
15531ce9e605STiwei Bie static struct virtqueue *vring_create_virtqueue_packed(
15541ce9e605STiwei Bie 	unsigned int index,
15551ce9e605STiwei Bie 	unsigned int num,
15561ce9e605STiwei Bie 	unsigned int vring_align,
15571ce9e605STiwei Bie 	struct virtio_device *vdev,
15581ce9e605STiwei Bie 	bool weak_barriers,
15591ce9e605STiwei Bie 	bool may_reduce_num,
15601ce9e605STiwei Bie 	bool context,
15611ce9e605STiwei Bie 	bool (*notify)(struct virtqueue *),
15621ce9e605STiwei Bie 	void (*callback)(struct virtqueue *),
15631ce9e605STiwei Bie 	const char *name)
15641ce9e605STiwei Bie {
15651ce9e605STiwei Bie 	struct vring_virtqueue *vq;
15661ce9e605STiwei Bie 	struct vring_packed_desc *ring;
15671ce9e605STiwei Bie 	struct vring_packed_desc_event *driver, *device;
15681ce9e605STiwei Bie 	dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
15691ce9e605STiwei Bie 	size_t ring_size_in_bytes, event_size_in_bytes;
15701ce9e605STiwei Bie 	unsigned int i;
15711ce9e605STiwei Bie 
15721ce9e605STiwei Bie 	ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
15731ce9e605STiwei Bie 
15741ce9e605STiwei Bie 	ring = vring_alloc_queue(vdev, ring_size_in_bytes,
15751ce9e605STiwei Bie 				 &ring_dma_addr,
15761ce9e605STiwei Bie 				 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
15771ce9e605STiwei Bie 	if (!ring)
15781ce9e605STiwei Bie 		goto err_ring;
15791ce9e605STiwei Bie 
15801ce9e605STiwei Bie 	event_size_in_bytes = sizeof(struct vring_packed_desc_event);
15811ce9e605STiwei Bie 
15821ce9e605STiwei Bie 	driver = vring_alloc_queue(vdev, event_size_in_bytes,
15831ce9e605STiwei Bie 				   &driver_event_dma_addr,
15841ce9e605STiwei Bie 				   GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
15851ce9e605STiwei Bie 	if (!driver)
15861ce9e605STiwei Bie 		goto err_driver;
15871ce9e605STiwei Bie 
15881ce9e605STiwei Bie 	device = vring_alloc_queue(vdev, event_size_in_bytes,
15891ce9e605STiwei Bie 				   &device_event_dma_addr,
15901ce9e605STiwei Bie 				   GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
15911ce9e605STiwei Bie 	if (!device)
15921ce9e605STiwei Bie 		goto err_device;
15931ce9e605STiwei Bie 
15941ce9e605STiwei Bie 	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
15951ce9e605STiwei Bie 	if (!vq)
15961ce9e605STiwei Bie 		goto err_vq;
15971ce9e605STiwei Bie 
15981ce9e605STiwei Bie 	vq->vq.callback = callback;
15991ce9e605STiwei Bie 	vq->vq.vdev = vdev;
16001ce9e605STiwei Bie 	vq->vq.name = name;
16011ce9e605STiwei Bie 	vq->vq.num_free = num;
16021ce9e605STiwei Bie 	vq->vq.index = index;
16031ce9e605STiwei Bie 	vq->we_own_ring = true;
16041ce9e605STiwei Bie 	vq->notify = notify;
16051ce9e605STiwei Bie 	vq->weak_barriers = weak_barriers;
16061ce9e605STiwei Bie 	vq->broken = false;
16071ce9e605STiwei Bie 	vq->last_used_idx = 0;
16081ce9e605STiwei Bie 	vq->num_added = 0;
16091ce9e605STiwei Bie 	vq->packed_ring = true;
16101ce9e605STiwei Bie 	vq->use_dma_api = vring_use_dma_api(vdev);
16111ce9e605STiwei Bie 	list_add_tail(&vq->vq.list, &vdev->vqs);
16121ce9e605STiwei Bie #ifdef DEBUG
16131ce9e605STiwei Bie 	vq->in_use = false;
16141ce9e605STiwei Bie 	vq->last_add_time_valid = false;
16151ce9e605STiwei Bie #endif
16161ce9e605STiwei Bie 
16171ce9e605STiwei Bie 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
16181ce9e605STiwei Bie 		!context;
16191ce9e605STiwei Bie 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
16201ce9e605STiwei Bie 
162145383fb0STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
162245383fb0STiwei Bie 		vq->weak_barriers = false;
162345383fb0STiwei Bie 
16241ce9e605STiwei Bie 	vq->packed.ring_dma_addr = ring_dma_addr;
16251ce9e605STiwei Bie 	vq->packed.driver_event_dma_addr = driver_event_dma_addr;
16261ce9e605STiwei Bie 	vq->packed.device_event_dma_addr = device_event_dma_addr;
16271ce9e605STiwei Bie 
16281ce9e605STiwei Bie 	vq->packed.ring_size_in_bytes = ring_size_in_bytes;
16291ce9e605STiwei Bie 	vq->packed.event_size_in_bytes = event_size_in_bytes;
16301ce9e605STiwei Bie 
16311ce9e605STiwei Bie 	vq->packed.vring.num = num;
16321ce9e605STiwei Bie 	vq->packed.vring.desc = ring;
16331ce9e605STiwei Bie 	vq->packed.vring.driver = driver;
16341ce9e605STiwei Bie 	vq->packed.vring.device = device;
16351ce9e605STiwei Bie 
16361ce9e605STiwei Bie 	vq->packed.next_avail_idx = 0;
16371ce9e605STiwei Bie 	vq->packed.avail_wrap_counter = 1;
16381ce9e605STiwei Bie 	vq->packed.used_wrap_counter = 1;
16391ce9e605STiwei Bie 	vq->packed.event_flags_shadow = 0;
16401ce9e605STiwei Bie 	vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
16411ce9e605STiwei Bie 
16421ce9e605STiwei Bie 	vq->packed.desc_state = kmalloc_array(num,
16431ce9e605STiwei Bie 			sizeof(struct vring_desc_state_packed),
16441ce9e605STiwei Bie 			GFP_KERNEL);
16451ce9e605STiwei Bie 	if (!vq->packed.desc_state)
16461ce9e605STiwei Bie 		goto err_desc_state;
16471ce9e605STiwei Bie 
16481ce9e605STiwei Bie 	memset(vq->packed.desc_state, 0,
16491ce9e605STiwei Bie 		num * sizeof(struct vring_desc_state_packed));
16501ce9e605STiwei Bie 
16511ce9e605STiwei Bie 	/* Put everything in free lists. */
16521ce9e605STiwei Bie 	vq->free_head = 0;
16531ce9e605STiwei Bie 	for (i = 0; i < num-1; i++)
16541ce9e605STiwei Bie 		vq->packed.desc_state[i].next = i + 1;
16551ce9e605STiwei Bie 
16561ce9e605STiwei Bie 	vq->packed.desc_extra = kmalloc_array(num,
16571ce9e605STiwei Bie 			sizeof(struct vring_desc_extra_packed),
16581ce9e605STiwei Bie 			GFP_KERNEL);
16591ce9e605STiwei Bie 	if (!vq->packed.desc_extra)
16601ce9e605STiwei Bie 		goto err_desc_extra;
16611ce9e605STiwei Bie 
16621ce9e605STiwei Bie 	memset(vq->packed.desc_extra, 0,
16631ce9e605STiwei Bie 		num * sizeof(struct vring_desc_extra_packed));
16641ce9e605STiwei Bie 
16651ce9e605STiwei Bie 	/* No callback?  Tell other side not to bother us. */
16661ce9e605STiwei Bie 	if (!callback) {
16671ce9e605STiwei Bie 		vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
16681ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
16691ce9e605STiwei Bie 			cpu_to_le16(vq->packed.event_flags_shadow);
16701ce9e605STiwei Bie 	}
16711ce9e605STiwei Bie 
16721ce9e605STiwei Bie 	return &vq->vq;
16731ce9e605STiwei Bie 
16741ce9e605STiwei Bie err_desc_extra:
16751ce9e605STiwei Bie 	kfree(vq->packed.desc_state);
16761ce9e605STiwei Bie err_desc_state:
16771ce9e605STiwei Bie 	kfree(vq);
16781ce9e605STiwei Bie err_vq:
16791ce9e605STiwei Bie 	vring_free_queue(vdev, event_size_in_bytes, device, ring_dma_addr);
16801ce9e605STiwei Bie err_device:
16811ce9e605STiwei Bie 	vring_free_queue(vdev, event_size_in_bytes, driver, ring_dma_addr);
16821ce9e605STiwei Bie err_driver:
16831ce9e605STiwei Bie 	vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr);
16841ce9e605STiwei Bie err_ring:
16851ce9e605STiwei Bie 	return NULL;
16861ce9e605STiwei Bie }
16871ce9e605STiwei Bie 
16881ce9e605STiwei Bie 
16891ce9e605STiwei Bie /*
1690e6f633e5STiwei Bie  * Generic functions and exported symbols.
1691e6f633e5STiwei Bie  */
1692e6f633e5STiwei Bie 
1693e6f633e5STiwei Bie static inline int virtqueue_add(struct virtqueue *_vq,
1694e6f633e5STiwei Bie 				struct scatterlist *sgs[],
1695e6f633e5STiwei Bie 				unsigned int total_sg,
1696e6f633e5STiwei Bie 				unsigned int out_sgs,
1697e6f633e5STiwei Bie 				unsigned int in_sgs,
1698e6f633e5STiwei Bie 				void *data,
1699e6f633e5STiwei Bie 				void *ctx,
1700e6f633e5STiwei Bie 				gfp_t gfp)
1701e6f633e5STiwei Bie {
17021ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
17031ce9e605STiwei Bie 
17041ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
17051ce9e605STiwei Bie 					out_sgs, in_sgs, data, ctx, gfp) :
17061ce9e605STiwei Bie 				 virtqueue_add_split(_vq, sgs, total_sg,
1707e6f633e5STiwei Bie 					out_sgs, in_sgs, data, ctx, gfp);
1708e6f633e5STiwei Bie }
1709e6f633e5STiwei Bie 
1710e6f633e5STiwei Bie /**
1711e6f633e5STiwei Bie  * virtqueue_add_sgs - expose buffers to other end
1712a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1713e6f633e5STiwei Bie  * @sgs: array of terminated scatterlists.
1714a5581206SJiang Biao  * @out_sgs: the number of scatterlists readable by other side
1715a5581206SJiang Biao  * @in_sgs: the number of scatterlists which are writable (after readable ones)
1716e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1717e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1718e6f633e5STiwei Bie  *
1719e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1720e6f633e5STiwei Bie  * at the same time (except where noted).
1721e6f633e5STiwei Bie  *
1722e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1723e6f633e5STiwei Bie  */
1724e6f633e5STiwei Bie int virtqueue_add_sgs(struct virtqueue *_vq,
1725e6f633e5STiwei Bie 		      struct scatterlist *sgs[],
1726e6f633e5STiwei Bie 		      unsigned int out_sgs,
1727e6f633e5STiwei Bie 		      unsigned int in_sgs,
1728e6f633e5STiwei Bie 		      void *data,
1729e6f633e5STiwei Bie 		      gfp_t gfp)
1730e6f633e5STiwei Bie {
1731e6f633e5STiwei Bie 	unsigned int i, total_sg = 0;
1732e6f633e5STiwei Bie 
1733e6f633e5STiwei Bie 	/* Count them first. */
1734e6f633e5STiwei Bie 	for (i = 0; i < out_sgs + in_sgs; i++) {
1735e6f633e5STiwei Bie 		struct scatterlist *sg;
1736e6f633e5STiwei Bie 
1737e6f633e5STiwei Bie 		for (sg = sgs[i]; sg; sg = sg_next(sg))
1738e6f633e5STiwei Bie 			total_sg++;
1739e6f633e5STiwei Bie 	}
1740e6f633e5STiwei Bie 	return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
1741e6f633e5STiwei Bie 			     data, NULL, gfp);
1742e6f633e5STiwei Bie }
1743e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
1744e6f633e5STiwei Bie 
1745e6f633e5STiwei Bie /**
1746e6f633e5STiwei Bie  * virtqueue_add_outbuf - expose output buffers to other end
1747e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
1748e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
1749e6f633e5STiwei Bie  * @num: the number of entries in @sg readable by other side
1750e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1751e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1752e6f633e5STiwei Bie  *
1753e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1754e6f633e5STiwei Bie  * at the same time (except where noted).
1755e6f633e5STiwei Bie  *
1756e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1757e6f633e5STiwei Bie  */
1758e6f633e5STiwei Bie int virtqueue_add_outbuf(struct virtqueue *vq,
1759e6f633e5STiwei Bie 			 struct scatterlist *sg, unsigned int num,
1760e6f633e5STiwei Bie 			 void *data,
1761e6f633e5STiwei Bie 			 gfp_t gfp)
1762e6f633e5STiwei Bie {
1763e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
1764e6f633e5STiwei Bie }
1765e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
1766e6f633e5STiwei Bie 
1767e6f633e5STiwei Bie /**
1768e6f633e5STiwei Bie  * virtqueue_add_inbuf - expose input buffers to other end
1769e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
1770e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
1771e6f633e5STiwei Bie  * @num: the number of entries in @sg writable by other side
1772e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1773e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1774e6f633e5STiwei Bie  *
1775e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1776e6f633e5STiwei Bie  * at the same time (except where noted).
1777e6f633e5STiwei Bie  *
1778e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1779e6f633e5STiwei Bie  */
1780e6f633e5STiwei Bie int virtqueue_add_inbuf(struct virtqueue *vq,
1781e6f633e5STiwei Bie 			struct scatterlist *sg, unsigned int num,
1782e6f633e5STiwei Bie 			void *data,
1783e6f633e5STiwei Bie 			gfp_t gfp)
1784e6f633e5STiwei Bie {
1785e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
1786e6f633e5STiwei Bie }
1787e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
1788e6f633e5STiwei Bie 
1789e6f633e5STiwei Bie /**
1790e6f633e5STiwei Bie  * virtqueue_add_inbuf_ctx - expose input buffers to other end
1791e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
1792e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
1793e6f633e5STiwei Bie  * @num: the number of entries in @sg writable by other side
1794e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1795e6f633e5STiwei Bie  * @ctx: extra context for the token
1796e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1797e6f633e5STiwei Bie  *
1798e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1799e6f633e5STiwei Bie  * at the same time (except where noted).
1800e6f633e5STiwei Bie  *
1801e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1802e6f633e5STiwei Bie  */
1803e6f633e5STiwei Bie int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
1804e6f633e5STiwei Bie 			struct scatterlist *sg, unsigned int num,
1805e6f633e5STiwei Bie 			void *data,
1806e6f633e5STiwei Bie 			void *ctx,
1807e6f633e5STiwei Bie 			gfp_t gfp)
1808e6f633e5STiwei Bie {
1809e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
1810e6f633e5STiwei Bie }
1811e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
1812e6f633e5STiwei Bie 
1813e6f633e5STiwei Bie /**
1814e6f633e5STiwei Bie  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
1815a5581206SJiang Biao  * @_vq: the struct virtqueue
1816e6f633e5STiwei Bie  *
1817e6f633e5STiwei Bie  * Instead of virtqueue_kick(), you can do:
1818e6f633e5STiwei Bie  *	if (virtqueue_kick_prepare(vq))
1819e6f633e5STiwei Bie  *		virtqueue_notify(vq);
1820e6f633e5STiwei Bie  *
1821e6f633e5STiwei Bie  * This is sometimes useful because the virtqueue_kick_prepare() needs
1822e6f633e5STiwei Bie  * to be serialized, but the actual virtqueue_notify() call does not.
1823e6f633e5STiwei Bie  */
1824e6f633e5STiwei Bie bool virtqueue_kick_prepare(struct virtqueue *_vq)
1825e6f633e5STiwei Bie {
18261ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
18271ce9e605STiwei Bie 
18281ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
18291ce9e605STiwei Bie 				 virtqueue_kick_prepare_split(_vq);
1830e6f633e5STiwei Bie }
1831e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
1832e6f633e5STiwei Bie 
1833e6f633e5STiwei Bie /**
1834e6f633e5STiwei Bie  * virtqueue_notify - second half of split virtqueue_kick call.
1835a5581206SJiang Biao  * @_vq: the struct virtqueue
1836e6f633e5STiwei Bie  *
1837e6f633e5STiwei Bie  * This does not need to be serialized.
1838e6f633e5STiwei Bie  *
1839e6f633e5STiwei Bie  * Returns false if host notify failed or queue is broken, otherwise true.
1840e6f633e5STiwei Bie  */
1841e6f633e5STiwei Bie bool virtqueue_notify(struct virtqueue *_vq)
1842e6f633e5STiwei Bie {
1843e6f633e5STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1844e6f633e5STiwei Bie 
1845e6f633e5STiwei Bie 	if (unlikely(vq->broken))
1846e6f633e5STiwei Bie 		return false;
1847e6f633e5STiwei Bie 
1848e6f633e5STiwei Bie 	/* Prod other side to tell it about changes. */
1849e6f633e5STiwei Bie 	if (!vq->notify(_vq)) {
1850e6f633e5STiwei Bie 		vq->broken = true;
1851e6f633e5STiwei Bie 		return false;
1852e6f633e5STiwei Bie 	}
1853e6f633e5STiwei Bie 	return true;
1854e6f633e5STiwei Bie }
1855e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_notify);
1856e6f633e5STiwei Bie 
1857e6f633e5STiwei Bie /**
1858e6f633e5STiwei Bie  * virtqueue_kick - update after add_buf
1859e6f633e5STiwei Bie  * @vq: the struct virtqueue
1860e6f633e5STiwei Bie  *
1861e6f633e5STiwei Bie  * After one or more virtqueue_add_* calls, invoke this to kick
1862e6f633e5STiwei Bie  * the other side.
1863e6f633e5STiwei Bie  *
1864e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1865e6f633e5STiwei Bie  * operations at the same time (except where noted).
1866e6f633e5STiwei Bie  *
1867e6f633e5STiwei Bie  * Returns false if kick failed, otherwise true.
1868e6f633e5STiwei Bie  */
1869e6f633e5STiwei Bie bool virtqueue_kick(struct virtqueue *vq)
1870e6f633e5STiwei Bie {
1871e6f633e5STiwei Bie 	if (virtqueue_kick_prepare(vq))
1872e6f633e5STiwei Bie 		return virtqueue_notify(vq);
1873e6f633e5STiwei Bie 	return true;
1874e6f633e5STiwei Bie }
1875e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick);
1876e6f633e5STiwei Bie 
1877e6f633e5STiwei Bie /**
1878e6f633e5STiwei Bie  * virtqueue_get_buf - get the next used buffer
1879a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1880e6f633e5STiwei Bie  * @len: the length written into the buffer
1881a5581206SJiang Biao  * @ctx: extra context for the token
1882e6f633e5STiwei Bie  *
1883e6f633e5STiwei Bie  * If the device wrote data into the buffer, @len will be set to the
1884e6f633e5STiwei Bie  * amount written.  This means you don't need to clear the buffer
1885e6f633e5STiwei Bie  * beforehand to ensure there's no data leakage in the case of short
1886e6f633e5STiwei Bie  * writes.
1887e6f633e5STiwei Bie  *
1888e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1889e6f633e5STiwei Bie  * operations at the same time (except where noted).
1890e6f633e5STiwei Bie  *
1891e6f633e5STiwei Bie  * Returns NULL if there are no used buffers, or the "data" token
1892e6f633e5STiwei Bie  * handed to virtqueue_add_*().
1893e6f633e5STiwei Bie  */
1894e6f633e5STiwei Bie void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
1895e6f633e5STiwei Bie 			    void **ctx)
1896e6f633e5STiwei Bie {
18971ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
18981ce9e605STiwei Bie 
18991ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
19001ce9e605STiwei Bie 				 virtqueue_get_buf_ctx_split(_vq, len, ctx);
1901e6f633e5STiwei Bie }
1902e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
1903e6f633e5STiwei Bie 
1904e6f633e5STiwei Bie void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
1905e6f633e5STiwei Bie {
1906e6f633e5STiwei Bie 	return virtqueue_get_buf_ctx(_vq, len, NULL);
1907e6f633e5STiwei Bie }
1908e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf);
1909e6f633e5STiwei Bie /**
1910e6f633e5STiwei Bie  * virtqueue_disable_cb - disable callbacks
1911a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1912e6f633e5STiwei Bie  *
1913e6f633e5STiwei Bie  * Note that this is not necessarily synchronous, hence unreliable and only
1914e6f633e5STiwei Bie  * useful as an optimization.
1915e6f633e5STiwei Bie  *
1916e6f633e5STiwei Bie  * Unlike other operations, this need not be serialized.
1917e6f633e5STiwei Bie  */
1918e6f633e5STiwei Bie void virtqueue_disable_cb(struct virtqueue *_vq)
1919e6f633e5STiwei Bie {
19201ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
19211ce9e605STiwei Bie 
19221ce9e605STiwei Bie 	if (vq->packed_ring)
19231ce9e605STiwei Bie 		virtqueue_disable_cb_packed(_vq);
19241ce9e605STiwei Bie 	else
1925e6f633e5STiwei Bie 		virtqueue_disable_cb_split(_vq);
1926e6f633e5STiwei Bie }
1927e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
1928e6f633e5STiwei Bie 
1929e6f633e5STiwei Bie /**
1930e6f633e5STiwei Bie  * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
1931a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1932e6f633e5STiwei Bie  *
1933e6f633e5STiwei Bie  * This re-enables callbacks; it returns current queue state
1934e6f633e5STiwei Bie  * in an opaque unsigned value. This value should be later tested by
1935e6f633e5STiwei Bie  * virtqueue_poll, to detect a possible race between the driver checking for
1936e6f633e5STiwei Bie  * more work, and enabling callbacks.
1937e6f633e5STiwei Bie  *
1938e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1939e6f633e5STiwei Bie  * operations at the same time (except where noted).
1940e6f633e5STiwei Bie  */
1941e6f633e5STiwei Bie unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
1942e6f633e5STiwei Bie {
19431ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
19441ce9e605STiwei Bie 
19451ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
19461ce9e605STiwei Bie 				 virtqueue_enable_cb_prepare_split(_vq);
1947e6f633e5STiwei Bie }
1948e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
1949e6f633e5STiwei Bie 
1950e6f633e5STiwei Bie /**
1951e6f633e5STiwei Bie  * virtqueue_poll - query pending used buffers
1952a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1953e6f633e5STiwei Bie  * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
1954e6f633e5STiwei Bie  *
1955e6f633e5STiwei Bie  * Returns "true" if there are pending used buffers in the queue.
1956e6f633e5STiwei Bie  *
1957e6f633e5STiwei Bie  * This does not need to be serialized.
1958e6f633e5STiwei Bie  */
1959e6f633e5STiwei Bie bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
1960e6f633e5STiwei Bie {
1961e6f633e5STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1962e6f633e5STiwei Bie 
1963e6f633e5STiwei Bie 	virtio_mb(vq->weak_barriers);
19641ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
19651ce9e605STiwei Bie 				 virtqueue_poll_split(_vq, last_used_idx);
1966e6f633e5STiwei Bie }
1967e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_poll);
1968e6f633e5STiwei Bie 
1969e6f633e5STiwei Bie /**
1970e6f633e5STiwei Bie  * virtqueue_enable_cb - restart callbacks after disable_cb.
1971a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1972e6f633e5STiwei Bie  *
1973e6f633e5STiwei Bie  * This re-enables callbacks; it returns "false" if there are pending
1974e6f633e5STiwei Bie  * buffers in the queue, to detect a possible race between the driver
1975e6f633e5STiwei Bie  * checking for more work, and enabling callbacks.
1976e6f633e5STiwei Bie  *
1977e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1978e6f633e5STiwei Bie  * operations at the same time (except where noted).
1979e6f633e5STiwei Bie  */
1980e6f633e5STiwei Bie bool virtqueue_enable_cb(struct virtqueue *_vq)
1981e6f633e5STiwei Bie {
1982e6f633e5STiwei Bie 	unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
1983e6f633e5STiwei Bie 
1984e6f633e5STiwei Bie 	return !virtqueue_poll(_vq, last_used_idx);
1985e6f633e5STiwei Bie }
1986e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
1987e6f633e5STiwei Bie 
1988e6f633e5STiwei Bie /**
1989e6f633e5STiwei Bie  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
1990a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1991e6f633e5STiwei Bie  *
1992e6f633e5STiwei Bie  * This re-enables callbacks but hints to the other side to delay
1993e6f633e5STiwei Bie  * interrupts until most of the available buffers have been processed;
1994e6f633e5STiwei Bie  * it returns "false" if there are many pending buffers in the queue,
1995e6f633e5STiwei Bie  * to detect a possible race between the driver checking for more work,
1996e6f633e5STiwei Bie  * and enabling callbacks.
1997e6f633e5STiwei Bie  *
1998e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1999e6f633e5STiwei Bie  * operations at the same time (except where noted).
2000e6f633e5STiwei Bie  */
2001e6f633e5STiwei Bie bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
2002e6f633e5STiwei Bie {
20031ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
20041ce9e605STiwei Bie 
20051ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
20061ce9e605STiwei Bie 				 virtqueue_enable_cb_delayed_split(_vq);
2007e6f633e5STiwei Bie }
2008e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2009e6f633e5STiwei Bie 
2010138fd251STiwei Bie /**
2011138fd251STiwei Bie  * virtqueue_detach_unused_buf - detach first unused buffer
2012a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2013138fd251STiwei Bie  *
2014138fd251STiwei Bie  * Returns NULL or the "data" token handed to virtqueue_add_*().
2015138fd251STiwei Bie  * This is not valid on an active queue; it is useful only for device
2016138fd251STiwei Bie  * shutdown.
2017138fd251STiwei Bie  */
2018138fd251STiwei Bie void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2019138fd251STiwei Bie {
20201ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
20211ce9e605STiwei Bie 
20221ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
20231ce9e605STiwei Bie 				 virtqueue_detach_unused_buf_split(_vq);
2024138fd251STiwei Bie }
20257c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
2026c021eac4SShirley Ma 
2027138fd251STiwei Bie static inline bool more_used(const struct vring_virtqueue *vq)
2028138fd251STiwei Bie {
20291ce9e605STiwei Bie 	return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
2030138fd251STiwei Bie }
2031138fd251STiwei Bie 
20320a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
20330a8a69ddSRusty Russell {
20340a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
20350a8a69ddSRusty Russell 
20360a8a69ddSRusty Russell 	if (!more_used(vq)) {
20370a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
20380a8a69ddSRusty Russell 		return IRQ_NONE;
20390a8a69ddSRusty Russell 	}
20400a8a69ddSRusty Russell 
20410a8a69ddSRusty Russell 	if (unlikely(vq->broken))
20420a8a69ddSRusty Russell 		return IRQ_HANDLED;
20430a8a69ddSRusty Russell 
20440a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
204518445c4dSRusty Russell 	if (vq->vq.callback)
204618445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
20470a8a69ddSRusty Russell 
20480a8a69ddSRusty Russell 	return IRQ_HANDLED;
20490a8a69ddSRusty Russell }
2050c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
20510a8a69ddSRusty Russell 
20521ce9e605STiwei Bie /* Only available for split ring */
20532a2d1382SAndy Lutomirski struct virtqueue *__vring_new_virtqueue(unsigned int index,
20542a2d1382SAndy Lutomirski 					struct vring vring,
20550a8a69ddSRusty Russell 					struct virtio_device *vdev,
20567b21e34fSRusty Russell 					bool weak_barriers,
2057f94682ddSMichael S. Tsirkin 					bool context,
205846f9c2b9SHeinz Graalfs 					bool (*notify)(struct virtqueue *),
20599499f5e7SRusty Russell 					void (*callback)(struct virtqueue *),
20609499f5e7SRusty Russell 					const char *name)
20610a8a69ddSRusty Russell {
20620a8a69ddSRusty Russell 	unsigned int i;
20632a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq;
20640a8a69ddSRusty Russell 
20651ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
20661ce9e605STiwei Bie 		return NULL;
20671ce9e605STiwei Bie 
2068cbeedb72STiwei Bie 	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
20690a8a69ddSRusty Russell 	if (!vq)
20700a8a69ddSRusty Russell 		return NULL;
20710a8a69ddSRusty Russell 
20721ce9e605STiwei Bie 	vq->packed_ring = false;
20730a8a69ddSRusty Russell 	vq->vq.callback = callback;
20740a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
20759499f5e7SRusty Russell 	vq->vq.name = name;
20762a2d1382SAndy Lutomirski 	vq->vq.num_free = vring.num;
207706ca287dSRusty Russell 	vq->vq.index = index;
20782a2d1382SAndy Lutomirski 	vq->we_own_ring = false;
20790a8a69ddSRusty Russell 	vq->notify = notify;
20807b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
20810a8a69ddSRusty Russell 	vq->broken = false;
20820a8a69ddSRusty Russell 	vq->last_used_idx = 0;
20830a8a69ddSRusty Russell 	vq->num_added = 0;
2084fb3fba6bSTiwei Bie 	vq->use_dma_api = vring_use_dma_api(vdev);
20859499f5e7SRusty Russell 	list_add_tail(&vq->vq.list, &vdev->vqs);
20860a8a69ddSRusty Russell #ifdef DEBUG
20870a8a69ddSRusty Russell 	vq->in_use = false;
2088e93300b1SRusty Russell 	vq->last_add_time_valid = false;
20890a8a69ddSRusty Russell #endif
20900a8a69ddSRusty Russell 
20915a08b04fSMichael S. Tsirkin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
20925a08b04fSMichael S. Tsirkin 		!context;
2093a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
20949fa29b9dSMark McLoughlin 
209545383fb0STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
209645383fb0STiwei Bie 		vq->weak_barriers = false;
209745383fb0STiwei Bie 
2098d79dca75STiwei Bie 	vq->split.queue_dma_addr = 0;
2099d79dca75STiwei Bie 	vq->split.queue_size_in_bytes = 0;
2100d79dca75STiwei Bie 
2101e593bf97STiwei Bie 	vq->split.vring = vring;
2102e593bf97STiwei Bie 	vq->split.avail_flags_shadow = 0;
2103e593bf97STiwei Bie 	vq->split.avail_idx_shadow = 0;
2104e593bf97STiwei Bie 
21050a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
2106f277ec42SVenkatesh Srinivas 	if (!callback) {
2107e593bf97STiwei Bie 		vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
21080ea1e4a6SLadi Prosek 		if (!vq->event)
2109e593bf97STiwei Bie 			vq->split.vring.avail->flags = cpu_to_virtio16(vdev,
2110e593bf97STiwei Bie 					vq->split.avail_flags_shadow);
2111f277ec42SVenkatesh Srinivas 	}
21120a8a69ddSRusty Russell 
2113cbeedb72STiwei Bie 	vq->split.desc_state = kmalloc_array(vring.num,
2114cbeedb72STiwei Bie 			sizeof(struct vring_desc_state_split), GFP_KERNEL);
2115cbeedb72STiwei Bie 	if (!vq->split.desc_state) {
2116cbeedb72STiwei Bie 		kfree(vq);
2117cbeedb72STiwei Bie 		return NULL;
2118cbeedb72STiwei Bie 	}
2119cbeedb72STiwei Bie 
21200a8a69ddSRusty Russell 	/* Put everything in free lists. */
21210a8a69ddSRusty Russell 	vq->free_head = 0;
21222a2d1382SAndy Lutomirski 	for (i = 0; i < vring.num-1; i++)
2123e593bf97STiwei Bie 		vq->split.vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
2124cbeedb72STiwei Bie 	memset(vq->split.desc_state, 0, vring.num *
2125cbeedb72STiwei Bie 			sizeof(struct vring_desc_state_split));
21260a8a69ddSRusty Russell 
21270a8a69ddSRusty Russell 	return &vq->vq;
21280a8a69ddSRusty Russell }
21292a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
21302a2d1382SAndy Lutomirski 
21312a2d1382SAndy Lutomirski struct virtqueue *vring_create_virtqueue(
21322a2d1382SAndy Lutomirski 	unsigned int index,
21332a2d1382SAndy Lutomirski 	unsigned int num,
21342a2d1382SAndy Lutomirski 	unsigned int vring_align,
21352a2d1382SAndy Lutomirski 	struct virtio_device *vdev,
21362a2d1382SAndy Lutomirski 	bool weak_barriers,
21372a2d1382SAndy Lutomirski 	bool may_reduce_num,
2138f94682ddSMichael S. Tsirkin 	bool context,
21392a2d1382SAndy Lutomirski 	bool (*notify)(struct virtqueue *),
21402a2d1382SAndy Lutomirski 	void (*callback)(struct virtqueue *),
21412a2d1382SAndy Lutomirski 	const char *name)
21422a2d1382SAndy Lutomirski {
21431ce9e605STiwei Bie 
21441ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
21451ce9e605STiwei Bie 		return vring_create_virtqueue_packed(index, num, vring_align,
21461ce9e605STiwei Bie 				vdev, weak_barriers, may_reduce_num,
21471ce9e605STiwei Bie 				context, notify, callback, name);
21481ce9e605STiwei Bie 
2149d79dca75STiwei Bie 	return vring_create_virtqueue_split(index, num, vring_align,
2150d79dca75STiwei Bie 			vdev, weak_barriers, may_reduce_num,
2151d79dca75STiwei Bie 			context, notify, callback, name);
21522a2d1382SAndy Lutomirski }
21532a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(vring_create_virtqueue);
21542a2d1382SAndy Lutomirski 
21551ce9e605STiwei Bie /* Only available for split ring */
21562a2d1382SAndy Lutomirski struct virtqueue *vring_new_virtqueue(unsigned int index,
21572a2d1382SAndy Lutomirski 				      unsigned int num,
21582a2d1382SAndy Lutomirski 				      unsigned int vring_align,
21592a2d1382SAndy Lutomirski 				      struct virtio_device *vdev,
21602a2d1382SAndy Lutomirski 				      bool weak_barriers,
2161f94682ddSMichael S. Tsirkin 				      bool context,
21622a2d1382SAndy Lutomirski 				      void *pages,
21632a2d1382SAndy Lutomirski 				      bool (*notify)(struct virtqueue *vq),
21642a2d1382SAndy Lutomirski 				      void (*callback)(struct virtqueue *vq),
21652a2d1382SAndy Lutomirski 				      const char *name)
21662a2d1382SAndy Lutomirski {
21672a2d1382SAndy Lutomirski 	struct vring vring;
21681ce9e605STiwei Bie 
21691ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
21701ce9e605STiwei Bie 		return NULL;
21711ce9e605STiwei Bie 
21722a2d1382SAndy Lutomirski 	vring_init(&vring, num, pages, vring_align);
2173f94682ddSMichael S. Tsirkin 	return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
21742a2d1382SAndy Lutomirski 				     notify, callback, name);
21752a2d1382SAndy Lutomirski }
2176c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
21770a8a69ddSRusty Russell 
21782a2d1382SAndy Lutomirski void vring_del_virtqueue(struct virtqueue *_vq)
21790a8a69ddSRusty Russell {
21802a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq = to_vvq(_vq);
21812a2d1382SAndy Lutomirski 
21822a2d1382SAndy Lutomirski 	if (vq->we_own_ring) {
21831ce9e605STiwei Bie 		if (vq->packed_ring) {
21841ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
21851ce9e605STiwei Bie 					 vq->packed.ring_size_in_bytes,
21861ce9e605STiwei Bie 					 vq->packed.vring.desc,
21871ce9e605STiwei Bie 					 vq->packed.ring_dma_addr);
21881ce9e605STiwei Bie 
21891ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
21901ce9e605STiwei Bie 					 vq->packed.event_size_in_bytes,
21911ce9e605STiwei Bie 					 vq->packed.vring.driver,
21921ce9e605STiwei Bie 					 vq->packed.driver_event_dma_addr);
21931ce9e605STiwei Bie 
21941ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
21951ce9e605STiwei Bie 					 vq->packed.event_size_in_bytes,
21961ce9e605STiwei Bie 					 vq->packed.vring.device,
21971ce9e605STiwei Bie 					 vq->packed.device_event_dma_addr);
21981ce9e605STiwei Bie 
21991ce9e605STiwei Bie 			kfree(vq->packed.desc_state);
22001ce9e605STiwei Bie 			kfree(vq->packed.desc_extra);
22011ce9e605STiwei Bie 		} else {
2202d79dca75STiwei Bie 			vring_free_queue(vq->vq.vdev,
2203d79dca75STiwei Bie 					 vq->split.queue_size_in_bytes,
2204d79dca75STiwei Bie 					 vq->split.vring.desc,
2205d79dca75STiwei Bie 					 vq->split.queue_dma_addr);
22061ce9e605STiwei Bie 
2207cbeedb72STiwei Bie 			kfree(vq->split.desc_state);
22082a2d1382SAndy Lutomirski 		}
22091ce9e605STiwei Bie 	}
22102a2d1382SAndy Lutomirski 	list_del(&_vq->list);
22112a2d1382SAndy Lutomirski 	kfree(vq);
22120a8a69ddSRusty Russell }
2213c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
22140a8a69ddSRusty Russell 
2215e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
2216e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
2217e34f8725SRusty Russell {
2218e34f8725SRusty Russell 	unsigned int i;
2219e34f8725SRusty Russell 
2220e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2221e34f8725SRusty Russell 		switch (i) {
22229fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
22239fa29b9dSMark McLoughlin 			break;
2224a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
2225a5c262c5SMichael S. Tsirkin 			break;
2226747ae34aSMichael S. Tsirkin 		case VIRTIO_F_VERSION_1:
2227747ae34aSMichael S. Tsirkin 			break;
22281a937693SMichael S. Tsirkin 		case VIRTIO_F_IOMMU_PLATFORM:
22291a937693SMichael S. Tsirkin 			break;
2230f959a128STiwei Bie 		case VIRTIO_F_RING_PACKED:
2231f959a128STiwei Bie 			break;
223245383fb0STiwei Bie 		case VIRTIO_F_ORDER_PLATFORM:
223345383fb0STiwei Bie 			break;
2234e34f8725SRusty Russell 		default:
2235e34f8725SRusty Russell 			/* We don't understand this bit. */
2236e16e12beSMichael S. Tsirkin 			__virtio_clear_bit(vdev, i);
2237e34f8725SRusty Russell 		}
2238e34f8725SRusty Russell 	}
2239e34f8725SRusty Russell }
2240e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
2241e34f8725SRusty Russell 
22425dfc1762SRusty Russell /**
22435dfc1762SRusty Russell  * virtqueue_get_vring_size - return the size of the virtqueue's vring
2244a5581206SJiang Biao  * @_vq: the struct virtqueue containing the vring of interest.
22455dfc1762SRusty Russell  *
22465dfc1762SRusty Russell  * Returns the size of the vring.  This is mainly used for boasting to
22475dfc1762SRusty Russell  * userspace.  Unlike other operations, this need not be serialized.
22485dfc1762SRusty Russell  */
22498f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
22508f9f4668SRick Jones {
22518f9f4668SRick Jones 
22528f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
22538f9f4668SRick Jones 
22541ce9e605STiwei Bie 	return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
22558f9f4668SRick Jones }
22568f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
22578f9f4668SRick Jones 
2258b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq)
2259b3b32c94SHeinz Graalfs {
2260b3b32c94SHeinz Graalfs 	struct vring_virtqueue *vq = to_vvq(_vq);
2261b3b32c94SHeinz Graalfs 
2262b3b32c94SHeinz Graalfs 	return vq->broken;
2263b3b32c94SHeinz Graalfs }
2264b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken);
2265b3b32c94SHeinz Graalfs 
2266e2dcdfe9SRusty Russell /*
2267e2dcdfe9SRusty Russell  * This should prevent the device from being used, allowing drivers to
2268e2dcdfe9SRusty Russell  * recover.  You may need to grab appropriate locks to flush.
2269e2dcdfe9SRusty Russell  */
2270e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev)
2271e2dcdfe9SRusty Russell {
2272e2dcdfe9SRusty Russell 	struct virtqueue *_vq;
2273e2dcdfe9SRusty Russell 
2274e2dcdfe9SRusty Russell 	list_for_each_entry(_vq, &dev->vqs, list) {
2275e2dcdfe9SRusty Russell 		struct vring_virtqueue *vq = to_vvq(_vq);
2276e2dcdfe9SRusty Russell 		vq->broken = true;
2277e2dcdfe9SRusty Russell 	}
2278e2dcdfe9SRusty Russell }
2279e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device);
2280e2dcdfe9SRusty Russell 
22812a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
228289062652SCornelia Huck {
228389062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
228489062652SCornelia Huck 
22852a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
228689062652SCornelia Huck 
22871ce9e605STiwei Bie 	if (vq->packed_ring)
22881ce9e605STiwei Bie 		return vq->packed.ring_dma_addr;
22891ce9e605STiwei Bie 
2290d79dca75STiwei Bie 	return vq->split.queue_dma_addr;
22912a2d1382SAndy Lutomirski }
22922a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
22932a2d1382SAndy Lutomirski 
22942a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
229589062652SCornelia Huck {
229689062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
229789062652SCornelia Huck 
22982a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
22992a2d1382SAndy Lutomirski 
23001ce9e605STiwei Bie 	if (vq->packed_ring)
23011ce9e605STiwei Bie 		return vq->packed.driver_event_dma_addr;
23021ce9e605STiwei Bie 
2303d79dca75STiwei Bie 	return vq->split.queue_dma_addr +
2304e593bf97STiwei Bie 		((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
230589062652SCornelia Huck }
23062a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
23072a2d1382SAndy Lutomirski 
23082a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
23092a2d1382SAndy Lutomirski {
23102a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq = to_vvq(_vq);
23112a2d1382SAndy Lutomirski 
23122a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
23132a2d1382SAndy Lutomirski 
23141ce9e605STiwei Bie 	if (vq->packed_ring)
23151ce9e605STiwei Bie 		return vq->packed.device_event_dma_addr;
23161ce9e605STiwei Bie 
2317d79dca75STiwei Bie 	return vq->split.queue_dma_addr +
2318e593bf97STiwei Bie 		((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
23192a2d1382SAndy Lutomirski }
23202a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
23212a2d1382SAndy Lutomirski 
23221ce9e605STiwei Bie /* Only available for split ring */
23232a2d1382SAndy Lutomirski const struct vring *virtqueue_get_vring(struct virtqueue *vq)
23242a2d1382SAndy Lutomirski {
2325e593bf97STiwei Bie 	return &to_vvq(vq)->split.vring;
23262a2d1382SAndy Lutomirski }
23272a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_vring);
232889062652SCornelia Huck 
2329c6fd4701SRusty Russell MODULE_LICENSE("GPL");
2330