xref: /openbmc/linux/drivers/virtio/virtio_ring.c (revision fd534e9b5fdcf9bab33c03cb3ade1a1ae5b23c20)
1*fd534e9bSThomas 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;
569780bc790SAndy Lutomirski 	i = head;
570780bc790SAndy Lutomirski 
571780bc790SAndy Lutomirski 	for (n = 0; n < total_sg; n++) {
572780bc790SAndy Lutomirski 		if (i == err_idx)
573780bc790SAndy Lutomirski 			break;
574138fd251STiwei Bie 		vring_unmap_one_split(vq, &desc[i]);
575e593bf97STiwei Bie 		i = virtio16_to_cpu(_vq->vdev, vq->split.vring.desc[i].next);
576780bc790SAndy Lutomirski 	}
577780bc790SAndy Lutomirski 
578780bc790SAndy Lutomirski 	if (indirect)
579780bc790SAndy Lutomirski 		kfree(desc);
580780bc790SAndy Lutomirski 
5813cc36f6eSMichael S. Tsirkin 	END_USE(vq);
582780bc790SAndy Lutomirski 	return -EIO;
5830a8a69ddSRusty Russell }
58413816c76SRusty Russell 
585138fd251STiwei Bie static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
5860a8a69ddSRusty Russell {
5870a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
588a5c262c5SMichael S. Tsirkin 	u16 new, old;
58941f0377fSRusty Russell 	bool needs_kick;
59041f0377fSRusty Russell 
5910a8a69ddSRusty Russell 	START_USE(vq);
592a72caae2SJason Wang 	/* We need to expose available array entries before checking avail
593a72caae2SJason Wang 	 * event. */
594a9a0fef7SRusty Russell 	virtio_mb(vq->weak_barriers);
5950a8a69ddSRusty Russell 
596e593bf97STiwei Bie 	old = vq->split.avail_idx_shadow - vq->num_added;
597e593bf97STiwei Bie 	new = vq->split.avail_idx_shadow;
5980a8a69ddSRusty Russell 	vq->num_added = 0;
5990a8a69ddSRusty Russell 
6004d6a105eSTiwei Bie 	LAST_ADD_TIME_CHECK(vq);
6014d6a105eSTiwei Bie 	LAST_ADD_TIME_INVALID(vq);
602e93300b1SRusty Russell 
60341f0377fSRusty Russell 	if (vq->event) {
604e593bf97STiwei Bie 		needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
605e593bf97STiwei Bie 					vring_avail_event(&vq->split.vring)),
60641f0377fSRusty Russell 					      new, old);
60741f0377fSRusty Russell 	} else {
608e593bf97STiwei Bie 		needs_kick = !(vq->split.vring.used->flags &
609e593bf97STiwei Bie 					cpu_to_virtio16(_vq->vdev,
610e593bf97STiwei Bie 						VRING_USED_F_NO_NOTIFY));
61141f0377fSRusty Russell 	}
6120a8a69ddSRusty Russell 	END_USE(vq);
61341f0377fSRusty Russell 	return needs_kick;
61441f0377fSRusty Russell }
615138fd251STiwei Bie 
616138fd251STiwei Bie static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
6175a08b04fSMichael S. Tsirkin 			     void **ctx)
6180a8a69ddSRusty Russell {
619780bc790SAndy Lutomirski 	unsigned int i, j;
620c60923cbSGonglei 	__virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
6210a8a69ddSRusty Russell 
6220a8a69ddSRusty Russell 	/* Clear data ptr. */
623cbeedb72STiwei Bie 	vq->split.desc_state[head].data = NULL;
6240a8a69ddSRusty Russell 
625780bc790SAndy Lutomirski 	/* Put back on free list: unmap first-level descriptors and find end */
6260a8a69ddSRusty Russell 	i = head;
6279fa29b9dSMark McLoughlin 
628e593bf97STiwei Bie 	while (vq->split.vring.desc[i].flags & nextflag) {
629e593bf97STiwei Bie 		vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
630e593bf97STiwei Bie 		i = virtio16_to_cpu(vq->vq.vdev, vq->split.vring.desc[i].next);
63106ca287dSRusty Russell 		vq->vq.num_free++;
6320a8a69ddSRusty Russell 	}
6330a8a69ddSRusty Russell 
634e593bf97STiwei Bie 	vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
635e593bf97STiwei Bie 	vq->split.vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev,
636e593bf97STiwei Bie 						vq->free_head);
6370a8a69ddSRusty Russell 	vq->free_head = head;
638780bc790SAndy Lutomirski 
6390a8a69ddSRusty Russell 	/* Plus final descriptor */
64006ca287dSRusty Russell 	vq->vq.num_free++;
641780bc790SAndy Lutomirski 
6425a08b04fSMichael S. Tsirkin 	if (vq->indirect) {
643cbeedb72STiwei Bie 		struct vring_desc *indir_desc =
644cbeedb72STiwei Bie 				vq->split.desc_state[head].indir_desc;
6455a08b04fSMichael S. Tsirkin 		u32 len;
6465a08b04fSMichael S. Tsirkin 
6475a08b04fSMichael S. Tsirkin 		/* Free the indirect table, if any, now that it's unmapped. */
6485a08b04fSMichael S. Tsirkin 		if (!indir_desc)
6495a08b04fSMichael S. Tsirkin 			return;
6505a08b04fSMichael S. Tsirkin 
651e593bf97STiwei Bie 		len = virtio32_to_cpu(vq->vq.vdev,
652e593bf97STiwei Bie 				vq->split.vring.desc[head].len);
653780bc790SAndy Lutomirski 
654e593bf97STiwei Bie 		BUG_ON(!(vq->split.vring.desc[head].flags &
655780bc790SAndy Lutomirski 			 cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)));
656780bc790SAndy Lutomirski 		BUG_ON(len == 0 || len % sizeof(struct vring_desc));
657780bc790SAndy Lutomirski 
658780bc790SAndy Lutomirski 		for (j = 0; j < len / sizeof(struct vring_desc); j++)
659138fd251STiwei Bie 			vring_unmap_one_split(vq, &indir_desc[j]);
660780bc790SAndy Lutomirski 
6615a08b04fSMichael S. Tsirkin 		kfree(indir_desc);
662cbeedb72STiwei Bie 		vq->split.desc_state[head].indir_desc = NULL;
6635a08b04fSMichael S. Tsirkin 	} else if (ctx) {
664cbeedb72STiwei Bie 		*ctx = vq->split.desc_state[head].indir_desc;
665780bc790SAndy Lutomirski 	}
6660a8a69ddSRusty Russell }
6670a8a69ddSRusty Russell 
668138fd251STiwei Bie static inline bool more_used_split(const struct vring_virtqueue *vq)
6690a8a69ddSRusty Russell {
670e593bf97STiwei Bie 	return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
671e593bf97STiwei Bie 			vq->split.vring.used->idx);
6720a8a69ddSRusty Russell }
6730a8a69ddSRusty Russell 
674138fd251STiwei Bie static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
675138fd251STiwei Bie 					 unsigned int *len,
6765a08b04fSMichael S. Tsirkin 					 void **ctx)
6770a8a69ddSRusty Russell {
6780a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
6790a8a69ddSRusty Russell 	void *ret;
6800a8a69ddSRusty Russell 	unsigned int i;
6813b720b8cSRusty Russell 	u16 last_used;
6820a8a69ddSRusty Russell 
6830a8a69ddSRusty Russell 	START_USE(vq);
6840a8a69ddSRusty Russell 
6855ef82752SRusty Russell 	if (unlikely(vq->broken)) {
6865ef82752SRusty Russell 		END_USE(vq);
6875ef82752SRusty Russell 		return NULL;
6885ef82752SRusty Russell 	}
6895ef82752SRusty Russell 
690138fd251STiwei Bie 	if (!more_used_split(vq)) {
6910a8a69ddSRusty Russell 		pr_debug("No more buffers in queue\n");
6920a8a69ddSRusty Russell 		END_USE(vq);
6930a8a69ddSRusty Russell 		return NULL;
6940a8a69ddSRusty Russell 	}
6950a8a69ddSRusty Russell 
6962d61ba95SMichael S. Tsirkin 	/* Only get used array entries after they have been exposed by host. */
697a9a0fef7SRusty Russell 	virtio_rmb(vq->weak_barriers);
6982d61ba95SMichael S. Tsirkin 
699e593bf97STiwei Bie 	last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
700e593bf97STiwei Bie 	i = virtio32_to_cpu(_vq->vdev,
701e593bf97STiwei Bie 			vq->split.vring.used->ring[last_used].id);
702e593bf97STiwei Bie 	*len = virtio32_to_cpu(_vq->vdev,
703e593bf97STiwei Bie 			vq->split.vring.used->ring[last_used].len);
7040a8a69ddSRusty Russell 
705e593bf97STiwei Bie 	if (unlikely(i >= vq->split.vring.num)) {
7060a8a69ddSRusty Russell 		BAD_RING(vq, "id %u out of range\n", i);
7070a8a69ddSRusty Russell 		return NULL;
7080a8a69ddSRusty Russell 	}
709cbeedb72STiwei Bie 	if (unlikely(!vq->split.desc_state[i].data)) {
7100a8a69ddSRusty Russell 		BAD_RING(vq, "id %u is not a head!\n", i);
7110a8a69ddSRusty Russell 		return NULL;
7120a8a69ddSRusty Russell 	}
7130a8a69ddSRusty Russell 
714138fd251STiwei Bie 	/* detach_buf_split clears data, so grab it now. */
715cbeedb72STiwei Bie 	ret = vq->split.desc_state[i].data;
716138fd251STiwei Bie 	detach_buf_split(vq, i, ctx);
7170a8a69ddSRusty Russell 	vq->last_used_idx++;
718a5c262c5SMichael S. Tsirkin 	/* If we expect an interrupt for the next entry, tell host
719a5c262c5SMichael S. Tsirkin 	 * by writing event index and flush out the write before
720a5c262c5SMichael S. Tsirkin 	 * the read in the next get_buf call. */
721e593bf97STiwei Bie 	if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
722788e5b3aSMichael S. Tsirkin 		virtio_store_mb(vq->weak_barriers,
723e593bf97STiwei Bie 				&vring_used_event(&vq->split.vring),
724788e5b3aSMichael S. Tsirkin 				cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
725a5c262c5SMichael S. Tsirkin 
7264d6a105eSTiwei Bie 	LAST_ADD_TIME_INVALID(vq);
727e93300b1SRusty Russell 
7280a8a69ddSRusty Russell 	END_USE(vq);
7290a8a69ddSRusty Russell 	return ret;
7300a8a69ddSRusty Russell }
731138fd251STiwei Bie 
732138fd251STiwei Bie static void virtqueue_disable_cb_split(struct virtqueue *_vq)
733138fd251STiwei Bie {
734138fd251STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
735138fd251STiwei Bie 
736e593bf97STiwei Bie 	if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
737e593bf97STiwei Bie 		vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
738138fd251STiwei Bie 		if (!vq->event)
739e593bf97STiwei Bie 			vq->split.vring.avail->flags =
740e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
741e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
742138fd251STiwei Bie 	}
743138fd251STiwei Bie }
744138fd251STiwei Bie 
745138fd251STiwei Bie static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
746cc229884SMichael S. Tsirkin {
747cc229884SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
748cc229884SMichael S. Tsirkin 	u16 last_used_idx;
749cc229884SMichael S. Tsirkin 
750cc229884SMichael S. Tsirkin 	START_USE(vq);
751cc229884SMichael S. Tsirkin 
752cc229884SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
753cc229884SMichael S. Tsirkin 	 * more to do. */
754cc229884SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
755cc229884SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
756cc229884SMichael S. Tsirkin 	 * entry. Always do both to keep code simple. */
757e593bf97STiwei Bie 	if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
758e593bf97STiwei Bie 		vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
7590ea1e4a6SLadi Prosek 		if (!vq->event)
760e593bf97STiwei Bie 			vq->split.vring.avail->flags =
761e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
762e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
763f277ec42SVenkatesh Srinivas 	}
764e593bf97STiwei Bie 	vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
765e593bf97STiwei Bie 			last_used_idx = vq->last_used_idx);
766cc229884SMichael S. Tsirkin 	END_USE(vq);
767cc229884SMichael S. Tsirkin 	return last_used_idx;
768cc229884SMichael S. Tsirkin }
769138fd251STiwei Bie 
770138fd251STiwei Bie static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx)
771138fd251STiwei Bie {
772138fd251STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
773138fd251STiwei Bie 
774138fd251STiwei Bie 	return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
775e593bf97STiwei Bie 			vq->split.vring.used->idx);
776138fd251STiwei Bie }
777138fd251STiwei Bie 
778138fd251STiwei Bie static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
7797ab358c2SMichael S. Tsirkin {
7807ab358c2SMichael S. Tsirkin 	struct vring_virtqueue *vq = to_vvq(_vq);
7817ab358c2SMichael S. Tsirkin 	u16 bufs;
7827ab358c2SMichael S. Tsirkin 
7837ab358c2SMichael S. Tsirkin 	START_USE(vq);
7847ab358c2SMichael S. Tsirkin 
7857ab358c2SMichael S. Tsirkin 	/* We optimistically turn back on interrupts, then check if there was
7867ab358c2SMichael S. Tsirkin 	 * more to do. */
7877ab358c2SMichael S. Tsirkin 	/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
7887ab358c2SMichael S. Tsirkin 	 * either clear the flags bit or point the event index at the next
7890ea1e4a6SLadi Prosek 	 * entry. Always update the event index to keep code simple. */
790e593bf97STiwei Bie 	if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
791e593bf97STiwei Bie 		vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
7920ea1e4a6SLadi Prosek 		if (!vq->event)
793e593bf97STiwei Bie 			vq->split.vring.avail->flags =
794e593bf97STiwei Bie 				cpu_to_virtio16(_vq->vdev,
795e593bf97STiwei Bie 						vq->split.avail_flags_shadow);
796f277ec42SVenkatesh Srinivas 	}
7977ab358c2SMichael S. Tsirkin 	/* TODO: tune this threshold */
798e593bf97STiwei Bie 	bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
799788e5b3aSMichael S. Tsirkin 
800788e5b3aSMichael S. Tsirkin 	virtio_store_mb(vq->weak_barriers,
801e593bf97STiwei Bie 			&vring_used_event(&vq->split.vring),
802788e5b3aSMichael S. Tsirkin 			cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
803788e5b3aSMichael S. Tsirkin 
804e593bf97STiwei Bie 	if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
805e593bf97STiwei Bie 					- vq->last_used_idx) > bufs)) {
8067ab358c2SMichael S. Tsirkin 		END_USE(vq);
8077ab358c2SMichael S. Tsirkin 		return false;
8087ab358c2SMichael S. Tsirkin 	}
8097ab358c2SMichael S. Tsirkin 
8107ab358c2SMichael S. Tsirkin 	END_USE(vq);
8117ab358c2SMichael S. Tsirkin 	return true;
8127ab358c2SMichael S. Tsirkin }
8137ab358c2SMichael S. Tsirkin 
814138fd251STiwei Bie static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
815c021eac4SShirley Ma {
816c021eac4SShirley Ma 	struct vring_virtqueue *vq = to_vvq(_vq);
817c021eac4SShirley Ma 	unsigned int i;
818c021eac4SShirley Ma 	void *buf;
819c021eac4SShirley Ma 
820c021eac4SShirley Ma 	START_USE(vq);
821c021eac4SShirley Ma 
822e593bf97STiwei Bie 	for (i = 0; i < vq->split.vring.num; i++) {
823cbeedb72STiwei Bie 		if (!vq->split.desc_state[i].data)
824c021eac4SShirley Ma 			continue;
825138fd251STiwei Bie 		/* detach_buf_split clears data, so grab it now. */
826cbeedb72STiwei Bie 		buf = vq->split.desc_state[i].data;
827138fd251STiwei Bie 		detach_buf_split(vq, i, NULL);
828e593bf97STiwei Bie 		vq->split.avail_idx_shadow--;
829e593bf97STiwei Bie 		vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
830e593bf97STiwei Bie 				vq->split.avail_idx_shadow);
831c021eac4SShirley Ma 		END_USE(vq);
832c021eac4SShirley Ma 		return buf;
833c021eac4SShirley Ma 	}
834c021eac4SShirley Ma 	/* That should have freed everything. */
835e593bf97STiwei Bie 	BUG_ON(vq->vq.num_free != vq->split.vring.num);
836c021eac4SShirley Ma 
837c021eac4SShirley Ma 	END_USE(vq);
838c021eac4SShirley Ma 	return NULL;
839c021eac4SShirley Ma }
840138fd251STiwei Bie 
841d79dca75STiwei Bie static struct virtqueue *vring_create_virtqueue_split(
842d79dca75STiwei Bie 	unsigned int index,
843d79dca75STiwei Bie 	unsigned int num,
844d79dca75STiwei Bie 	unsigned int vring_align,
845d79dca75STiwei Bie 	struct virtio_device *vdev,
846d79dca75STiwei Bie 	bool weak_barriers,
847d79dca75STiwei Bie 	bool may_reduce_num,
848d79dca75STiwei Bie 	bool context,
849d79dca75STiwei Bie 	bool (*notify)(struct virtqueue *),
850d79dca75STiwei Bie 	void (*callback)(struct virtqueue *),
851d79dca75STiwei Bie 	const char *name)
852d79dca75STiwei Bie {
853d79dca75STiwei Bie 	struct virtqueue *vq;
854d79dca75STiwei Bie 	void *queue = NULL;
855d79dca75STiwei Bie 	dma_addr_t dma_addr;
856d79dca75STiwei Bie 	size_t queue_size_in_bytes;
857d79dca75STiwei Bie 	struct vring vring;
858d79dca75STiwei Bie 
859d79dca75STiwei Bie 	/* We assume num is a power of 2. */
860d79dca75STiwei Bie 	if (num & (num - 1)) {
861d79dca75STiwei Bie 		dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
862d79dca75STiwei Bie 		return NULL;
863d79dca75STiwei Bie 	}
864d79dca75STiwei Bie 
865d79dca75STiwei Bie 	/* TODO: allocate each queue chunk individually */
866d79dca75STiwei Bie 	for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
867d79dca75STiwei Bie 		queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
868d79dca75STiwei Bie 					  &dma_addr,
869d79dca75STiwei Bie 					  GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
870d79dca75STiwei Bie 		if (queue)
871d79dca75STiwei Bie 			break;
872cf94db21SCornelia Huck 		if (!may_reduce_num)
873cf94db21SCornelia Huck 			return NULL;
874d79dca75STiwei Bie 	}
875d79dca75STiwei Bie 
876d79dca75STiwei Bie 	if (!num)
877d79dca75STiwei Bie 		return NULL;
878d79dca75STiwei Bie 
879d79dca75STiwei Bie 	if (!queue) {
880d79dca75STiwei Bie 		/* Try to get a single page. You are my only hope! */
881d79dca75STiwei Bie 		queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
882d79dca75STiwei Bie 					  &dma_addr, GFP_KERNEL|__GFP_ZERO);
883d79dca75STiwei Bie 	}
884d79dca75STiwei Bie 	if (!queue)
885d79dca75STiwei Bie 		return NULL;
886d79dca75STiwei Bie 
887d79dca75STiwei Bie 	queue_size_in_bytes = vring_size(num, vring_align);
888d79dca75STiwei Bie 	vring_init(&vring, num, queue, vring_align);
889d79dca75STiwei Bie 
890d79dca75STiwei Bie 	vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
891d79dca75STiwei Bie 				   notify, callback, name);
892d79dca75STiwei Bie 	if (!vq) {
893d79dca75STiwei Bie 		vring_free_queue(vdev, queue_size_in_bytes, queue,
894d79dca75STiwei Bie 				 dma_addr);
895d79dca75STiwei Bie 		return NULL;
896d79dca75STiwei Bie 	}
897d79dca75STiwei Bie 
898d79dca75STiwei Bie 	to_vvq(vq)->split.queue_dma_addr = dma_addr;
899d79dca75STiwei Bie 	to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes;
900d79dca75STiwei Bie 	to_vvq(vq)->we_own_ring = true;
901d79dca75STiwei Bie 
902d79dca75STiwei Bie 	return vq;
903d79dca75STiwei Bie }
904d79dca75STiwei Bie 
905e6f633e5STiwei Bie 
906e6f633e5STiwei Bie /*
9071ce9e605STiwei Bie  * Packed ring specific functions - *_packed().
9081ce9e605STiwei Bie  */
9091ce9e605STiwei Bie 
9101ce9e605STiwei Bie static void vring_unmap_state_packed(const struct vring_virtqueue *vq,
9111ce9e605STiwei Bie 				     struct vring_desc_extra_packed *state)
9121ce9e605STiwei Bie {
9131ce9e605STiwei Bie 	u16 flags;
9141ce9e605STiwei Bie 
9151ce9e605STiwei Bie 	if (!vq->use_dma_api)
9161ce9e605STiwei Bie 		return;
9171ce9e605STiwei Bie 
9181ce9e605STiwei Bie 	flags = state->flags;
9191ce9e605STiwei Bie 
9201ce9e605STiwei Bie 	if (flags & VRING_DESC_F_INDIRECT) {
9211ce9e605STiwei Bie 		dma_unmap_single(vring_dma_dev(vq),
9221ce9e605STiwei Bie 				 state->addr, state->len,
9231ce9e605STiwei Bie 				 (flags & VRING_DESC_F_WRITE) ?
9241ce9e605STiwei Bie 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
9251ce9e605STiwei Bie 	} else {
9261ce9e605STiwei Bie 		dma_unmap_page(vring_dma_dev(vq),
9271ce9e605STiwei Bie 			       state->addr, state->len,
9281ce9e605STiwei Bie 			       (flags & VRING_DESC_F_WRITE) ?
9291ce9e605STiwei Bie 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
9301ce9e605STiwei Bie 	}
9311ce9e605STiwei Bie }
9321ce9e605STiwei Bie 
9331ce9e605STiwei Bie static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
9341ce9e605STiwei Bie 				   struct vring_packed_desc *desc)
9351ce9e605STiwei Bie {
9361ce9e605STiwei Bie 	u16 flags;
9371ce9e605STiwei Bie 
9381ce9e605STiwei Bie 	if (!vq->use_dma_api)
9391ce9e605STiwei Bie 		return;
9401ce9e605STiwei Bie 
9411ce9e605STiwei Bie 	flags = le16_to_cpu(desc->flags);
9421ce9e605STiwei Bie 
9431ce9e605STiwei Bie 	if (flags & VRING_DESC_F_INDIRECT) {
9441ce9e605STiwei Bie 		dma_unmap_single(vring_dma_dev(vq),
9451ce9e605STiwei Bie 				 le64_to_cpu(desc->addr),
9461ce9e605STiwei Bie 				 le32_to_cpu(desc->len),
9471ce9e605STiwei Bie 				 (flags & VRING_DESC_F_WRITE) ?
9481ce9e605STiwei Bie 				 DMA_FROM_DEVICE : DMA_TO_DEVICE);
9491ce9e605STiwei Bie 	} else {
9501ce9e605STiwei Bie 		dma_unmap_page(vring_dma_dev(vq),
9511ce9e605STiwei Bie 			       le64_to_cpu(desc->addr),
9521ce9e605STiwei Bie 			       le32_to_cpu(desc->len),
9531ce9e605STiwei Bie 			       (flags & VRING_DESC_F_WRITE) ?
9541ce9e605STiwei Bie 			       DMA_FROM_DEVICE : DMA_TO_DEVICE);
9551ce9e605STiwei Bie 	}
9561ce9e605STiwei Bie }
9571ce9e605STiwei Bie 
9581ce9e605STiwei Bie static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
9591ce9e605STiwei Bie 						       gfp_t gfp)
9601ce9e605STiwei Bie {
9611ce9e605STiwei Bie 	struct vring_packed_desc *desc;
9621ce9e605STiwei Bie 
9631ce9e605STiwei Bie 	/*
9641ce9e605STiwei Bie 	 * We require lowmem mappings for the descriptors because
9651ce9e605STiwei Bie 	 * otherwise virt_to_phys will give us bogus addresses in the
9661ce9e605STiwei Bie 	 * virtqueue.
9671ce9e605STiwei Bie 	 */
9681ce9e605STiwei Bie 	gfp &= ~__GFP_HIGHMEM;
9691ce9e605STiwei Bie 
9701ce9e605STiwei Bie 	desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
9711ce9e605STiwei Bie 
9721ce9e605STiwei Bie 	return desc;
9731ce9e605STiwei Bie }
9741ce9e605STiwei Bie 
9751ce9e605STiwei Bie static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
9761ce9e605STiwei Bie 				       struct scatterlist *sgs[],
9771ce9e605STiwei Bie 				       unsigned int total_sg,
9781ce9e605STiwei Bie 				       unsigned int out_sgs,
9791ce9e605STiwei Bie 				       unsigned int in_sgs,
9801ce9e605STiwei Bie 				       void *data,
9811ce9e605STiwei Bie 				       gfp_t gfp)
9821ce9e605STiwei Bie {
9831ce9e605STiwei Bie 	struct vring_packed_desc *desc;
9841ce9e605STiwei Bie 	struct scatterlist *sg;
9851ce9e605STiwei Bie 	unsigned int i, n, err_idx;
9861ce9e605STiwei Bie 	u16 head, id;
9871ce9e605STiwei Bie 	dma_addr_t addr;
9881ce9e605STiwei Bie 
9891ce9e605STiwei Bie 	head = vq->packed.next_avail_idx;
9901ce9e605STiwei Bie 	desc = alloc_indirect_packed(total_sg, gfp);
9911ce9e605STiwei Bie 
9921ce9e605STiwei Bie 	if (unlikely(vq->vq.num_free < 1)) {
9931ce9e605STiwei Bie 		pr_debug("Can't add buf len 1 - avail = 0\n");
994df0bfe75SYueHaibing 		kfree(desc);
9951ce9e605STiwei Bie 		END_USE(vq);
9961ce9e605STiwei Bie 		return -ENOSPC;
9971ce9e605STiwei Bie 	}
9981ce9e605STiwei Bie 
9991ce9e605STiwei Bie 	i = 0;
10001ce9e605STiwei Bie 	id = vq->free_head;
10011ce9e605STiwei Bie 	BUG_ON(id == vq->packed.vring.num);
10021ce9e605STiwei Bie 
10031ce9e605STiwei Bie 	for (n = 0; n < out_sgs + in_sgs; n++) {
10041ce9e605STiwei Bie 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
10051ce9e605STiwei Bie 			addr = vring_map_one_sg(vq, sg, n < out_sgs ?
10061ce9e605STiwei Bie 					DMA_TO_DEVICE : DMA_FROM_DEVICE);
10071ce9e605STiwei Bie 			if (vring_mapping_error(vq, addr))
10081ce9e605STiwei Bie 				goto unmap_release;
10091ce9e605STiwei Bie 
10101ce9e605STiwei Bie 			desc[i].flags = cpu_to_le16(n < out_sgs ?
10111ce9e605STiwei Bie 						0 : VRING_DESC_F_WRITE);
10121ce9e605STiwei Bie 			desc[i].addr = cpu_to_le64(addr);
10131ce9e605STiwei Bie 			desc[i].len = cpu_to_le32(sg->length);
10141ce9e605STiwei Bie 			i++;
10151ce9e605STiwei Bie 		}
10161ce9e605STiwei Bie 	}
10171ce9e605STiwei Bie 
10181ce9e605STiwei Bie 	/* Now that the indirect table is filled in, map it. */
10191ce9e605STiwei Bie 	addr = vring_map_single(vq, desc,
10201ce9e605STiwei Bie 			total_sg * sizeof(struct vring_packed_desc),
10211ce9e605STiwei Bie 			DMA_TO_DEVICE);
10221ce9e605STiwei Bie 	if (vring_mapping_error(vq, addr))
10231ce9e605STiwei Bie 		goto unmap_release;
10241ce9e605STiwei Bie 
10251ce9e605STiwei Bie 	vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
10261ce9e605STiwei Bie 	vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
10271ce9e605STiwei Bie 				sizeof(struct vring_packed_desc));
10281ce9e605STiwei Bie 	vq->packed.vring.desc[head].id = cpu_to_le16(id);
10291ce9e605STiwei Bie 
10301ce9e605STiwei Bie 	if (vq->use_dma_api) {
10311ce9e605STiwei Bie 		vq->packed.desc_extra[id].addr = addr;
10321ce9e605STiwei Bie 		vq->packed.desc_extra[id].len = total_sg *
10331ce9e605STiwei Bie 				sizeof(struct vring_packed_desc);
10341ce9e605STiwei Bie 		vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
10351ce9e605STiwei Bie 						  vq->packed.avail_used_flags;
10361ce9e605STiwei Bie 	}
10371ce9e605STiwei Bie 
10381ce9e605STiwei Bie 	/*
10391ce9e605STiwei Bie 	 * A driver MUST NOT make the first descriptor in the list
10401ce9e605STiwei Bie 	 * available before all subsequent descriptors comprising
10411ce9e605STiwei Bie 	 * the list are made available.
10421ce9e605STiwei Bie 	 */
10431ce9e605STiwei Bie 	virtio_wmb(vq->weak_barriers);
10441ce9e605STiwei Bie 	vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
10451ce9e605STiwei Bie 						vq->packed.avail_used_flags);
10461ce9e605STiwei Bie 
10471ce9e605STiwei Bie 	/* We're using some buffers from the free list. */
10481ce9e605STiwei Bie 	vq->vq.num_free -= 1;
10491ce9e605STiwei Bie 
10501ce9e605STiwei Bie 	/* Update free pointer */
10511ce9e605STiwei Bie 	n = head + 1;
10521ce9e605STiwei Bie 	if (n >= vq->packed.vring.num) {
10531ce9e605STiwei Bie 		n = 0;
10541ce9e605STiwei Bie 		vq->packed.avail_wrap_counter ^= 1;
10551ce9e605STiwei Bie 		vq->packed.avail_used_flags ^=
10561ce9e605STiwei Bie 				1 << VRING_PACKED_DESC_F_AVAIL |
10571ce9e605STiwei Bie 				1 << VRING_PACKED_DESC_F_USED;
10581ce9e605STiwei Bie 	}
10591ce9e605STiwei Bie 	vq->packed.next_avail_idx = n;
10601ce9e605STiwei Bie 	vq->free_head = vq->packed.desc_state[id].next;
10611ce9e605STiwei Bie 
10621ce9e605STiwei Bie 	/* Store token and indirect buffer state. */
10631ce9e605STiwei Bie 	vq->packed.desc_state[id].num = 1;
10641ce9e605STiwei Bie 	vq->packed.desc_state[id].data = data;
10651ce9e605STiwei Bie 	vq->packed.desc_state[id].indir_desc = desc;
10661ce9e605STiwei Bie 	vq->packed.desc_state[id].last = id;
10671ce9e605STiwei Bie 
10681ce9e605STiwei Bie 	vq->num_added += 1;
10691ce9e605STiwei Bie 
10701ce9e605STiwei Bie 	pr_debug("Added buffer head %i to %p\n", head, vq);
10711ce9e605STiwei Bie 	END_USE(vq);
10721ce9e605STiwei Bie 
10731ce9e605STiwei Bie 	return 0;
10741ce9e605STiwei Bie 
10751ce9e605STiwei Bie unmap_release:
10761ce9e605STiwei Bie 	err_idx = i;
10771ce9e605STiwei Bie 
10781ce9e605STiwei Bie 	for (i = 0; i < err_idx; i++)
10791ce9e605STiwei Bie 		vring_unmap_desc_packed(vq, &desc[i]);
10801ce9e605STiwei Bie 
10811ce9e605STiwei Bie 	kfree(desc);
10821ce9e605STiwei Bie 
10831ce9e605STiwei Bie 	END_USE(vq);
10841ce9e605STiwei Bie 	return -EIO;
10851ce9e605STiwei Bie }
10861ce9e605STiwei Bie 
10871ce9e605STiwei Bie static inline int virtqueue_add_packed(struct virtqueue *_vq,
10881ce9e605STiwei Bie 				       struct scatterlist *sgs[],
10891ce9e605STiwei Bie 				       unsigned int total_sg,
10901ce9e605STiwei Bie 				       unsigned int out_sgs,
10911ce9e605STiwei Bie 				       unsigned int in_sgs,
10921ce9e605STiwei Bie 				       void *data,
10931ce9e605STiwei Bie 				       void *ctx,
10941ce9e605STiwei Bie 				       gfp_t gfp)
10951ce9e605STiwei Bie {
10961ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
10971ce9e605STiwei Bie 	struct vring_packed_desc *desc;
10981ce9e605STiwei Bie 	struct scatterlist *sg;
10991ce9e605STiwei Bie 	unsigned int i, n, c, descs_used, err_idx;
11001ce9e605STiwei Bie 	__le16 uninitialized_var(head_flags), flags;
11011ce9e605STiwei Bie 	u16 head, id, uninitialized_var(prev), curr, avail_used_flags;
11021ce9e605STiwei Bie 
11031ce9e605STiwei Bie 	START_USE(vq);
11041ce9e605STiwei Bie 
11051ce9e605STiwei Bie 	BUG_ON(data == NULL);
11061ce9e605STiwei Bie 	BUG_ON(ctx && vq->indirect);
11071ce9e605STiwei Bie 
11081ce9e605STiwei Bie 	if (unlikely(vq->broken)) {
11091ce9e605STiwei Bie 		END_USE(vq);
11101ce9e605STiwei Bie 		return -EIO;
11111ce9e605STiwei Bie 	}
11121ce9e605STiwei Bie 
11131ce9e605STiwei Bie 	LAST_ADD_TIME_UPDATE(vq);
11141ce9e605STiwei Bie 
11151ce9e605STiwei Bie 	BUG_ON(total_sg == 0);
11161ce9e605STiwei Bie 
11171ce9e605STiwei Bie 	if (virtqueue_use_indirect(_vq, total_sg))
11181ce9e605STiwei Bie 		return virtqueue_add_indirect_packed(vq, sgs, total_sg,
11191ce9e605STiwei Bie 				out_sgs, in_sgs, data, gfp);
11201ce9e605STiwei Bie 
11211ce9e605STiwei Bie 	head = vq->packed.next_avail_idx;
11221ce9e605STiwei Bie 	avail_used_flags = vq->packed.avail_used_flags;
11231ce9e605STiwei Bie 
11241ce9e605STiwei Bie 	WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
11251ce9e605STiwei Bie 
11261ce9e605STiwei Bie 	desc = vq->packed.vring.desc;
11271ce9e605STiwei Bie 	i = head;
11281ce9e605STiwei Bie 	descs_used = total_sg;
11291ce9e605STiwei Bie 
11301ce9e605STiwei Bie 	if (unlikely(vq->vq.num_free < descs_used)) {
11311ce9e605STiwei Bie 		pr_debug("Can't add buf len %i - avail = %i\n",
11321ce9e605STiwei Bie 			 descs_used, vq->vq.num_free);
11331ce9e605STiwei Bie 		END_USE(vq);
11341ce9e605STiwei Bie 		return -ENOSPC;
11351ce9e605STiwei Bie 	}
11361ce9e605STiwei Bie 
11371ce9e605STiwei Bie 	id = vq->free_head;
11381ce9e605STiwei Bie 	BUG_ON(id == vq->packed.vring.num);
11391ce9e605STiwei Bie 
11401ce9e605STiwei Bie 	curr = id;
11411ce9e605STiwei Bie 	c = 0;
11421ce9e605STiwei Bie 	for (n = 0; n < out_sgs + in_sgs; n++) {
11431ce9e605STiwei Bie 		for (sg = sgs[n]; sg; sg = sg_next(sg)) {
11441ce9e605STiwei Bie 			dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ?
11451ce9e605STiwei Bie 					DMA_TO_DEVICE : DMA_FROM_DEVICE);
11461ce9e605STiwei Bie 			if (vring_mapping_error(vq, addr))
11471ce9e605STiwei Bie 				goto unmap_release;
11481ce9e605STiwei Bie 
11491ce9e605STiwei Bie 			flags = cpu_to_le16(vq->packed.avail_used_flags |
11501ce9e605STiwei Bie 				    (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
11511ce9e605STiwei Bie 				    (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
11521ce9e605STiwei Bie 			if (i == head)
11531ce9e605STiwei Bie 				head_flags = flags;
11541ce9e605STiwei Bie 			else
11551ce9e605STiwei Bie 				desc[i].flags = flags;
11561ce9e605STiwei Bie 
11571ce9e605STiwei Bie 			desc[i].addr = cpu_to_le64(addr);
11581ce9e605STiwei Bie 			desc[i].len = cpu_to_le32(sg->length);
11591ce9e605STiwei Bie 			desc[i].id = cpu_to_le16(id);
11601ce9e605STiwei Bie 
11611ce9e605STiwei Bie 			if (unlikely(vq->use_dma_api)) {
11621ce9e605STiwei Bie 				vq->packed.desc_extra[curr].addr = addr;
11631ce9e605STiwei Bie 				vq->packed.desc_extra[curr].len = sg->length;
11641ce9e605STiwei Bie 				vq->packed.desc_extra[curr].flags =
11651ce9e605STiwei Bie 					le16_to_cpu(flags);
11661ce9e605STiwei Bie 			}
11671ce9e605STiwei Bie 			prev = curr;
11681ce9e605STiwei Bie 			curr = vq->packed.desc_state[curr].next;
11691ce9e605STiwei Bie 
11701ce9e605STiwei Bie 			if ((unlikely(++i >= vq->packed.vring.num))) {
11711ce9e605STiwei Bie 				i = 0;
11721ce9e605STiwei Bie 				vq->packed.avail_used_flags ^=
11731ce9e605STiwei Bie 					1 << VRING_PACKED_DESC_F_AVAIL |
11741ce9e605STiwei Bie 					1 << VRING_PACKED_DESC_F_USED;
11751ce9e605STiwei Bie 			}
11761ce9e605STiwei Bie 		}
11771ce9e605STiwei Bie 	}
11781ce9e605STiwei Bie 
11791ce9e605STiwei Bie 	if (i < head)
11801ce9e605STiwei Bie 		vq->packed.avail_wrap_counter ^= 1;
11811ce9e605STiwei Bie 
11821ce9e605STiwei Bie 	/* We're using some buffers from the free list. */
11831ce9e605STiwei Bie 	vq->vq.num_free -= descs_used;
11841ce9e605STiwei Bie 
11851ce9e605STiwei Bie 	/* Update free pointer */
11861ce9e605STiwei Bie 	vq->packed.next_avail_idx = i;
11871ce9e605STiwei Bie 	vq->free_head = curr;
11881ce9e605STiwei Bie 
11891ce9e605STiwei Bie 	/* Store token. */
11901ce9e605STiwei Bie 	vq->packed.desc_state[id].num = descs_used;
11911ce9e605STiwei Bie 	vq->packed.desc_state[id].data = data;
11921ce9e605STiwei Bie 	vq->packed.desc_state[id].indir_desc = ctx;
11931ce9e605STiwei Bie 	vq->packed.desc_state[id].last = prev;
11941ce9e605STiwei Bie 
11951ce9e605STiwei Bie 	/*
11961ce9e605STiwei Bie 	 * A driver MUST NOT make the first descriptor in the list
11971ce9e605STiwei Bie 	 * available before all subsequent descriptors comprising
11981ce9e605STiwei Bie 	 * the list are made available.
11991ce9e605STiwei Bie 	 */
12001ce9e605STiwei Bie 	virtio_wmb(vq->weak_barriers);
12011ce9e605STiwei Bie 	vq->packed.vring.desc[head].flags = head_flags;
12021ce9e605STiwei Bie 	vq->num_added += descs_used;
12031ce9e605STiwei Bie 
12041ce9e605STiwei Bie 	pr_debug("Added buffer head %i to %p\n", head, vq);
12051ce9e605STiwei Bie 	END_USE(vq);
12061ce9e605STiwei Bie 
12071ce9e605STiwei Bie 	return 0;
12081ce9e605STiwei Bie 
12091ce9e605STiwei Bie unmap_release:
12101ce9e605STiwei Bie 	err_idx = i;
12111ce9e605STiwei Bie 	i = head;
12121ce9e605STiwei Bie 
12131ce9e605STiwei Bie 	vq->packed.avail_used_flags = avail_used_flags;
12141ce9e605STiwei Bie 
12151ce9e605STiwei Bie 	for (n = 0; n < total_sg; n++) {
12161ce9e605STiwei Bie 		if (i == err_idx)
12171ce9e605STiwei Bie 			break;
12181ce9e605STiwei Bie 		vring_unmap_desc_packed(vq, &desc[i]);
12191ce9e605STiwei Bie 		i++;
12201ce9e605STiwei Bie 		if (i >= vq->packed.vring.num)
12211ce9e605STiwei Bie 			i = 0;
12221ce9e605STiwei Bie 	}
12231ce9e605STiwei Bie 
12241ce9e605STiwei Bie 	END_USE(vq);
12251ce9e605STiwei Bie 	return -EIO;
12261ce9e605STiwei Bie }
12271ce9e605STiwei Bie 
12281ce9e605STiwei Bie static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
12291ce9e605STiwei Bie {
12301ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1231f51f9826STiwei Bie 	u16 new, old, off_wrap, flags, wrap_counter, event_idx;
12321ce9e605STiwei Bie 	bool needs_kick;
12331ce9e605STiwei Bie 	union {
12341ce9e605STiwei Bie 		struct {
12351ce9e605STiwei Bie 			__le16 off_wrap;
12361ce9e605STiwei Bie 			__le16 flags;
12371ce9e605STiwei Bie 		};
12381ce9e605STiwei Bie 		u32 u32;
12391ce9e605STiwei Bie 	} snapshot;
12401ce9e605STiwei Bie 
12411ce9e605STiwei Bie 	START_USE(vq);
12421ce9e605STiwei Bie 
12431ce9e605STiwei Bie 	/*
12441ce9e605STiwei Bie 	 * We need to expose the new flags value before checking notification
12451ce9e605STiwei Bie 	 * suppressions.
12461ce9e605STiwei Bie 	 */
12471ce9e605STiwei Bie 	virtio_mb(vq->weak_barriers);
12481ce9e605STiwei Bie 
1249f51f9826STiwei Bie 	old = vq->packed.next_avail_idx - vq->num_added;
1250f51f9826STiwei Bie 	new = vq->packed.next_avail_idx;
12511ce9e605STiwei Bie 	vq->num_added = 0;
12521ce9e605STiwei Bie 
12531ce9e605STiwei Bie 	snapshot.u32 = *(u32 *)vq->packed.vring.device;
12541ce9e605STiwei Bie 	flags = le16_to_cpu(snapshot.flags);
12551ce9e605STiwei Bie 
12561ce9e605STiwei Bie 	LAST_ADD_TIME_CHECK(vq);
12571ce9e605STiwei Bie 	LAST_ADD_TIME_INVALID(vq);
12581ce9e605STiwei Bie 
1259f51f9826STiwei Bie 	if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
12601ce9e605STiwei Bie 		needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1261f51f9826STiwei Bie 		goto out;
1262f51f9826STiwei Bie 	}
1263f51f9826STiwei Bie 
1264f51f9826STiwei Bie 	off_wrap = le16_to_cpu(snapshot.off_wrap);
1265f51f9826STiwei Bie 
1266f51f9826STiwei Bie 	wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1267f51f9826STiwei Bie 	event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1268f51f9826STiwei Bie 	if (wrap_counter != vq->packed.avail_wrap_counter)
1269f51f9826STiwei Bie 		event_idx -= vq->packed.vring.num;
1270f51f9826STiwei Bie 
1271f51f9826STiwei Bie 	needs_kick = vring_need_event(event_idx, new, old);
1272f51f9826STiwei Bie out:
12731ce9e605STiwei Bie 	END_USE(vq);
12741ce9e605STiwei Bie 	return needs_kick;
12751ce9e605STiwei Bie }
12761ce9e605STiwei Bie 
12771ce9e605STiwei Bie static void detach_buf_packed(struct vring_virtqueue *vq,
12781ce9e605STiwei Bie 			      unsigned int id, void **ctx)
12791ce9e605STiwei Bie {
12801ce9e605STiwei Bie 	struct vring_desc_state_packed *state = NULL;
12811ce9e605STiwei Bie 	struct vring_packed_desc *desc;
12821ce9e605STiwei Bie 	unsigned int i, curr;
12831ce9e605STiwei Bie 
12841ce9e605STiwei Bie 	state = &vq->packed.desc_state[id];
12851ce9e605STiwei Bie 
12861ce9e605STiwei Bie 	/* Clear data ptr. */
12871ce9e605STiwei Bie 	state->data = NULL;
12881ce9e605STiwei Bie 
12891ce9e605STiwei Bie 	vq->packed.desc_state[state->last].next = vq->free_head;
12901ce9e605STiwei Bie 	vq->free_head = id;
12911ce9e605STiwei Bie 	vq->vq.num_free += state->num;
12921ce9e605STiwei Bie 
12931ce9e605STiwei Bie 	if (unlikely(vq->use_dma_api)) {
12941ce9e605STiwei Bie 		curr = id;
12951ce9e605STiwei Bie 		for (i = 0; i < state->num; i++) {
12961ce9e605STiwei Bie 			vring_unmap_state_packed(vq,
12971ce9e605STiwei Bie 				&vq->packed.desc_extra[curr]);
12981ce9e605STiwei Bie 			curr = vq->packed.desc_state[curr].next;
12991ce9e605STiwei Bie 		}
13001ce9e605STiwei Bie 	}
13011ce9e605STiwei Bie 
13021ce9e605STiwei Bie 	if (vq->indirect) {
13031ce9e605STiwei Bie 		u32 len;
13041ce9e605STiwei Bie 
13051ce9e605STiwei Bie 		/* Free the indirect table, if any, now that it's unmapped. */
13061ce9e605STiwei Bie 		desc = state->indir_desc;
13071ce9e605STiwei Bie 		if (!desc)
13081ce9e605STiwei Bie 			return;
13091ce9e605STiwei Bie 
13101ce9e605STiwei Bie 		if (vq->use_dma_api) {
13111ce9e605STiwei Bie 			len = vq->packed.desc_extra[id].len;
13121ce9e605STiwei Bie 			for (i = 0; i < len / sizeof(struct vring_packed_desc);
13131ce9e605STiwei Bie 					i++)
13141ce9e605STiwei Bie 				vring_unmap_desc_packed(vq, &desc[i]);
13151ce9e605STiwei Bie 		}
13161ce9e605STiwei Bie 		kfree(desc);
13171ce9e605STiwei Bie 		state->indir_desc = NULL;
13181ce9e605STiwei Bie 	} else if (ctx) {
13191ce9e605STiwei Bie 		*ctx = state->indir_desc;
13201ce9e605STiwei Bie 	}
13211ce9e605STiwei Bie }
13221ce9e605STiwei Bie 
13231ce9e605STiwei Bie static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
13241ce9e605STiwei Bie 				       u16 idx, bool used_wrap_counter)
13251ce9e605STiwei Bie {
13261ce9e605STiwei Bie 	bool avail, used;
13271ce9e605STiwei Bie 	u16 flags;
13281ce9e605STiwei Bie 
13291ce9e605STiwei Bie 	flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
13301ce9e605STiwei Bie 	avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
13311ce9e605STiwei Bie 	used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
13321ce9e605STiwei Bie 
13331ce9e605STiwei Bie 	return avail == used && used == used_wrap_counter;
13341ce9e605STiwei Bie }
13351ce9e605STiwei Bie 
13361ce9e605STiwei Bie static inline bool more_used_packed(const struct vring_virtqueue *vq)
13371ce9e605STiwei Bie {
13381ce9e605STiwei Bie 	return is_used_desc_packed(vq, vq->last_used_idx,
13391ce9e605STiwei Bie 			vq->packed.used_wrap_counter);
13401ce9e605STiwei Bie }
13411ce9e605STiwei Bie 
13421ce9e605STiwei Bie static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
13431ce9e605STiwei Bie 					  unsigned int *len,
13441ce9e605STiwei Bie 					  void **ctx)
13451ce9e605STiwei Bie {
13461ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
13471ce9e605STiwei Bie 	u16 last_used, id;
13481ce9e605STiwei Bie 	void *ret;
13491ce9e605STiwei Bie 
13501ce9e605STiwei Bie 	START_USE(vq);
13511ce9e605STiwei Bie 
13521ce9e605STiwei Bie 	if (unlikely(vq->broken)) {
13531ce9e605STiwei Bie 		END_USE(vq);
13541ce9e605STiwei Bie 		return NULL;
13551ce9e605STiwei Bie 	}
13561ce9e605STiwei Bie 
13571ce9e605STiwei Bie 	if (!more_used_packed(vq)) {
13581ce9e605STiwei Bie 		pr_debug("No more buffers in queue\n");
13591ce9e605STiwei Bie 		END_USE(vq);
13601ce9e605STiwei Bie 		return NULL;
13611ce9e605STiwei Bie 	}
13621ce9e605STiwei Bie 
13631ce9e605STiwei Bie 	/* Only get used elements after they have been exposed by host. */
13641ce9e605STiwei Bie 	virtio_rmb(vq->weak_barriers);
13651ce9e605STiwei Bie 
13661ce9e605STiwei Bie 	last_used = vq->last_used_idx;
13671ce9e605STiwei Bie 	id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
13681ce9e605STiwei Bie 	*len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
13691ce9e605STiwei Bie 
13701ce9e605STiwei Bie 	if (unlikely(id >= vq->packed.vring.num)) {
13711ce9e605STiwei Bie 		BAD_RING(vq, "id %u out of range\n", id);
13721ce9e605STiwei Bie 		return NULL;
13731ce9e605STiwei Bie 	}
13741ce9e605STiwei Bie 	if (unlikely(!vq->packed.desc_state[id].data)) {
13751ce9e605STiwei Bie 		BAD_RING(vq, "id %u is not a head!\n", id);
13761ce9e605STiwei Bie 		return NULL;
13771ce9e605STiwei Bie 	}
13781ce9e605STiwei Bie 
13791ce9e605STiwei Bie 	/* detach_buf_packed clears data, so grab it now. */
13801ce9e605STiwei Bie 	ret = vq->packed.desc_state[id].data;
13811ce9e605STiwei Bie 	detach_buf_packed(vq, id, ctx);
13821ce9e605STiwei Bie 
13831ce9e605STiwei Bie 	vq->last_used_idx += vq->packed.desc_state[id].num;
13841ce9e605STiwei Bie 	if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) {
13851ce9e605STiwei Bie 		vq->last_used_idx -= vq->packed.vring.num;
13861ce9e605STiwei Bie 		vq->packed.used_wrap_counter ^= 1;
13871ce9e605STiwei Bie 	}
13881ce9e605STiwei Bie 
1389f51f9826STiwei Bie 	/*
1390f51f9826STiwei Bie 	 * If we expect an interrupt for the next entry, tell host
1391f51f9826STiwei Bie 	 * by writing event index and flush out the write before
1392f51f9826STiwei Bie 	 * the read in the next get_buf call.
1393f51f9826STiwei Bie 	 */
1394f51f9826STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1395f51f9826STiwei Bie 		virtio_store_mb(vq->weak_barriers,
1396f51f9826STiwei Bie 				&vq->packed.vring.driver->off_wrap,
1397f51f9826STiwei Bie 				cpu_to_le16(vq->last_used_idx |
1398f51f9826STiwei Bie 					(vq->packed.used_wrap_counter <<
1399f51f9826STiwei Bie 					 VRING_PACKED_EVENT_F_WRAP_CTR)));
1400f51f9826STiwei Bie 
14011ce9e605STiwei Bie 	LAST_ADD_TIME_INVALID(vq);
14021ce9e605STiwei Bie 
14031ce9e605STiwei Bie 	END_USE(vq);
14041ce9e605STiwei Bie 	return ret;
14051ce9e605STiwei Bie }
14061ce9e605STiwei Bie 
14071ce9e605STiwei Bie static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
14081ce9e605STiwei Bie {
14091ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14101ce9e605STiwei Bie 
14111ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
14121ce9e605STiwei Bie 		vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
14131ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
14141ce9e605STiwei Bie 			cpu_to_le16(vq->packed.event_flags_shadow);
14151ce9e605STiwei Bie 	}
14161ce9e605STiwei Bie }
14171ce9e605STiwei Bie 
14181ce9e605STiwei Bie static unsigned virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
14191ce9e605STiwei Bie {
14201ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14211ce9e605STiwei Bie 
14221ce9e605STiwei Bie 	START_USE(vq);
14231ce9e605STiwei Bie 
14241ce9e605STiwei Bie 	/*
14251ce9e605STiwei Bie 	 * We optimistically turn back on interrupts, then check if there was
14261ce9e605STiwei Bie 	 * more to do.
14271ce9e605STiwei Bie 	 */
14281ce9e605STiwei Bie 
1429f51f9826STiwei Bie 	if (vq->event) {
1430f51f9826STiwei Bie 		vq->packed.vring.driver->off_wrap =
1431f51f9826STiwei Bie 			cpu_to_le16(vq->last_used_idx |
1432f51f9826STiwei Bie 				(vq->packed.used_wrap_counter <<
1433f51f9826STiwei Bie 				 VRING_PACKED_EVENT_F_WRAP_CTR));
1434f51f9826STiwei Bie 		/*
1435f51f9826STiwei Bie 		 * We need to update event offset and event wrap
1436f51f9826STiwei Bie 		 * counter first before updating event flags.
1437f51f9826STiwei Bie 		 */
1438f51f9826STiwei Bie 		virtio_wmb(vq->weak_barriers);
1439f51f9826STiwei Bie 	}
1440f51f9826STiwei Bie 
14411ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1442f51f9826STiwei Bie 		vq->packed.event_flags_shadow = vq->event ?
1443f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_DESC :
1444f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_ENABLE;
14451ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
14461ce9e605STiwei Bie 				cpu_to_le16(vq->packed.event_flags_shadow);
14471ce9e605STiwei Bie 	}
14481ce9e605STiwei Bie 
14491ce9e605STiwei Bie 	END_USE(vq);
14501ce9e605STiwei Bie 	return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter <<
14511ce9e605STiwei Bie 			VRING_PACKED_EVENT_F_WRAP_CTR);
14521ce9e605STiwei Bie }
14531ce9e605STiwei Bie 
14541ce9e605STiwei Bie static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
14551ce9e605STiwei Bie {
14561ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14571ce9e605STiwei Bie 	bool wrap_counter;
14581ce9e605STiwei Bie 	u16 used_idx;
14591ce9e605STiwei Bie 
14601ce9e605STiwei Bie 	wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
14611ce9e605STiwei Bie 	used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
14621ce9e605STiwei Bie 
14631ce9e605STiwei Bie 	return is_used_desc_packed(vq, used_idx, wrap_counter);
14641ce9e605STiwei Bie }
14651ce9e605STiwei Bie 
14661ce9e605STiwei Bie static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
14671ce9e605STiwei Bie {
14681ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
14691ce9e605STiwei Bie 	u16 used_idx, wrap_counter;
1470f51f9826STiwei Bie 	u16 bufs;
14711ce9e605STiwei Bie 
14721ce9e605STiwei Bie 	START_USE(vq);
14731ce9e605STiwei Bie 
14741ce9e605STiwei Bie 	/*
14751ce9e605STiwei Bie 	 * We optimistically turn back on interrupts, then check if there was
14761ce9e605STiwei Bie 	 * more to do.
14771ce9e605STiwei Bie 	 */
14781ce9e605STiwei Bie 
1479f51f9826STiwei Bie 	if (vq->event) {
1480f51f9826STiwei Bie 		/* TODO: tune this threshold */
1481f51f9826STiwei Bie 		bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
14821ce9e605STiwei Bie 		wrap_counter = vq->packed.used_wrap_counter;
14831ce9e605STiwei Bie 
1484f51f9826STiwei Bie 		used_idx = vq->last_used_idx + bufs;
1485f51f9826STiwei Bie 		if (used_idx >= vq->packed.vring.num) {
1486f51f9826STiwei Bie 			used_idx -= vq->packed.vring.num;
1487f51f9826STiwei Bie 			wrap_counter ^= 1;
1488f51f9826STiwei Bie 		}
1489f51f9826STiwei Bie 
1490f51f9826STiwei Bie 		vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1491f51f9826STiwei Bie 			(wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1492f51f9826STiwei Bie 
1493f51f9826STiwei Bie 		/*
1494f51f9826STiwei Bie 		 * We need to update event offset and event wrap
1495f51f9826STiwei Bie 		 * counter first before updating event flags.
1496f51f9826STiwei Bie 		 */
1497f51f9826STiwei Bie 		virtio_wmb(vq->weak_barriers);
1498f51f9826STiwei Bie 	} else {
1499f51f9826STiwei Bie 		used_idx = vq->last_used_idx;
1500f51f9826STiwei Bie 		wrap_counter = vq->packed.used_wrap_counter;
1501f51f9826STiwei Bie 	}
1502f51f9826STiwei Bie 
15031ce9e605STiwei Bie 	if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1504f51f9826STiwei Bie 		vq->packed.event_flags_shadow = vq->event ?
1505f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_DESC :
1506f51f9826STiwei Bie 				VRING_PACKED_EVENT_FLAG_ENABLE;
15071ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
15081ce9e605STiwei Bie 				cpu_to_le16(vq->packed.event_flags_shadow);
15091ce9e605STiwei Bie 	}
15101ce9e605STiwei Bie 
15111ce9e605STiwei Bie 	/*
15121ce9e605STiwei Bie 	 * We need to update event suppression structure first
15131ce9e605STiwei Bie 	 * before re-checking for more used buffers.
15141ce9e605STiwei Bie 	 */
15151ce9e605STiwei Bie 	virtio_mb(vq->weak_barriers);
15161ce9e605STiwei Bie 
15171ce9e605STiwei Bie 	if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
15181ce9e605STiwei Bie 		END_USE(vq);
15191ce9e605STiwei Bie 		return false;
15201ce9e605STiwei Bie 	}
15211ce9e605STiwei Bie 
15221ce9e605STiwei Bie 	END_USE(vq);
15231ce9e605STiwei Bie 	return true;
15241ce9e605STiwei Bie }
15251ce9e605STiwei Bie 
15261ce9e605STiwei Bie static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
15271ce9e605STiwei Bie {
15281ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
15291ce9e605STiwei Bie 	unsigned int i;
15301ce9e605STiwei Bie 	void *buf;
15311ce9e605STiwei Bie 
15321ce9e605STiwei Bie 	START_USE(vq);
15331ce9e605STiwei Bie 
15341ce9e605STiwei Bie 	for (i = 0; i < vq->packed.vring.num; i++) {
15351ce9e605STiwei Bie 		if (!vq->packed.desc_state[i].data)
15361ce9e605STiwei Bie 			continue;
15371ce9e605STiwei Bie 		/* detach_buf clears data, so grab it now. */
15381ce9e605STiwei Bie 		buf = vq->packed.desc_state[i].data;
15391ce9e605STiwei Bie 		detach_buf_packed(vq, i, NULL);
15401ce9e605STiwei Bie 		END_USE(vq);
15411ce9e605STiwei Bie 		return buf;
15421ce9e605STiwei Bie 	}
15431ce9e605STiwei Bie 	/* That should have freed everything. */
15441ce9e605STiwei Bie 	BUG_ON(vq->vq.num_free != vq->packed.vring.num);
15451ce9e605STiwei Bie 
15461ce9e605STiwei Bie 	END_USE(vq);
15471ce9e605STiwei Bie 	return NULL;
15481ce9e605STiwei Bie }
15491ce9e605STiwei Bie 
15501ce9e605STiwei Bie static struct virtqueue *vring_create_virtqueue_packed(
15511ce9e605STiwei Bie 	unsigned int index,
15521ce9e605STiwei Bie 	unsigned int num,
15531ce9e605STiwei Bie 	unsigned int vring_align,
15541ce9e605STiwei Bie 	struct virtio_device *vdev,
15551ce9e605STiwei Bie 	bool weak_barriers,
15561ce9e605STiwei Bie 	bool may_reduce_num,
15571ce9e605STiwei Bie 	bool context,
15581ce9e605STiwei Bie 	bool (*notify)(struct virtqueue *),
15591ce9e605STiwei Bie 	void (*callback)(struct virtqueue *),
15601ce9e605STiwei Bie 	const char *name)
15611ce9e605STiwei Bie {
15621ce9e605STiwei Bie 	struct vring_virtqueue *vq;
15631ce9e605STiwei Bie 	struct vring_packed_desc *ring;
15641ce9e605STiwei Bie 	struct vring_packed_desc_event *driver, *device;
15651ce9e605STiwei Bie 	dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
15661ce9e605STiwei Bie 	size_t ring_size_in_bytes, event_size_in_bytes;
15671ce9e605STiwei Bie 	unsigned int i;
15681ce9e605STiwei Bie 
15691ce9e605STiwei Bie 	ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
15701ce9e605STiwei Bie 
15711ce9e605STiwei Bie 	ring = vring_alloc_queue(vdev, ring_size_in_bytes,
15721ce9e605STiwei Bie 				 &ring_dma_addr,
15731ce9e605STiwei Bie 				 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
15741ce9e605STiwei Bie 	if (!ring)
15751ce9e605STiwei Bie 		goto err_ring;
15761ce9e605STiwei Bie 
15771ce9e605STiwei Bie 	event_size_in_bytes = sizeof(struct vring_packed_desc_event);
15781ce9e605STiwei Bie 
15791ce9e605STiwei Bie 	driver = vring_alloc_queue(vdev, event_size_in_bytes,
15801ce9e605STiwei Bie 				   &driver_event_dma_addr,
15811ce9e605STiwei Bie 				   GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
15821ce9e605STiwei Bie 	if (!driver)
15831ce9e605STiwei Bie 		goto err_driver;
15841ce9e605STiwei Bie 
15851ce9e605STiwei Bie 	device = vring_alloc_queue(vdev, event_size_in_bytes,
15861ce9e605STiwei Bie 				   &device_event_dma_addr,
15871ce9e605STiwei Bie 				   GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
15881ce9e605STiwei Bie 	if (!device)
15891ce9e605STiwei Bie 		goto err_device;
15901ce9e605STiwei Bie 
15911ce9e605STiwei Bie 	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
15921ce9e605STiwei Bie 	if (!vq)
15931ce9e605STiwei Bie 		goto err_vq;
15941ce9e605STiwei Bie 
15951ce9e605STiwei Bie 	vq->vq.callback = callback;
15961ce9e605STiwei Bie 	vq->vq.vdev = vdev;
15971ce9e605STiwei Bie 	vq->vq.name = name;
15981ce9e605STiwei Bie 	vq->vq.num_free = num;
15991ce9e605STiwei Bie 	vq->vq.index = index;
16001ce9e605STiwei Bie 	vq->we_own_ring = true;
16011ce9e605STiwei Bie 	vq->notify = notify;
16021ce9e605STiwei Bie 	vq->weak_barriers = weak_barriers;
16031ce9e605STiwei Bie 	vq->broken = false;
16041ce9e605STiwei Bie 	vq->last_used_idx = 0;
16051ce9e605STiwei Bie 	vq->num_added = 0;
16061ce9e605STiwei Bie 	vq->packed_ring = true;
16071ce9e605STiwei Bie 	vq->use_dma_api = vring_use_dma_api(vdev);
16081ce9e605STiwei Bie 	list_add_tail(&vq->vq.list, &vdev->vqs);
16091ce9e605STiwei Bie #ifdef DEBUG
16101ce9e605STiwei Bie 	vq->in_use = false;
16111ce9e605STiwei Bie 	vq->last_add_time_valid = false;
16121ce9e605STiwei Bie #endif
16131ce9e605STiwei Bie 
16141ce9e605STiwei Bie 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
16151ce9e605STiwei Bie 		!context;
16161ce9e605STiwei Bie 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
16171ce9e605STiwei Bie 
161845383fb0STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
161945383fb0STiwei Bie 		vq->weak_barriers = false;
162045383fb0STiwei Bie 
16211ce9e605STiwei Bie 	vq->packed.ring_dma_addr = ring_dma_addr;
16221ce9e605STiwei Bie 	vq->packed.driver_event_dma_addr = driver_event_dma_addr;
16231ce9e605STiwei Bie 	vq->packed.device_event_dma_addr = device_event_dma_addr;
16241ce9e605STiwei Bie 
16251ce9e605STiwei Bie 	vq->packed.ring_size_in_bytes = ring_size_in_bytes;
16261ce9e605STiwei Bie 	vq->packed.event_size_in_bytes = event_size_in_bytes;
16271ce9e605STiwei Bie 
16281ce9e605STiwei Bie 	vq->packed.vring.num = num;
16291ce9e605STiwei Bie 	vq->packed.vring.desc = ring;
16301ce9e605STiwei Bie 	vq->packed.vring.driver = driver;
16311ce9e605STiwei Bie 	vq->packed.vring.device = device;
16321ce9e605STiwei Bie 
16331ce9e605STiwei Bie 	vq->packed.next_avail_idx = 0;
16341ce9e605STiwei Bie 	vq->packed.avail_wrap_counter = 1;
16351ce9e605STiwei Bie 	vq->packed.used_wrap_counter = 1;
16361ce9e605STiwei Bie 	vq->packed.event_flags_shadow = 0;
16371ce9e605STiwei Bie 	vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
16381ce9e605STiwei Bie 
16391ce9e605STiwei Bie 	vq->packed.desc_state = kmalloc_array(num,
16401ce9e605STiwei Bie 			sizeof(struct vring_desc_state_packed),
16411ce9e605STiwei Bie 			GFP_KERNEL);
16421ce9e605STiwei Bie 	if (!vq->packed.desc_state)
16431ce9e605STiwei Bie 		goto err_desc_state;
16441ce9e605STiwei Bie 
16451ce9e605STiwei Bie 	memset(vq->packed.desc_state, 0,
16461ce9e605STiwei Bie 		num * sizeof(struct vring_desc_state_packed));
16471ce9e605STiwei Bie 
16481ce9e605STiwei Bie 	/* Put everything in free lists. */
16491ce9e605STiwei Bie 	vq->free_head = 0;
16501ce9e605STiwei Bie 	for (i = 0; i < num-1; i++)
16511ce9e605STiwei Bie 		vq->packed.desc_state[i].next = i + 1;
16521ce9e605STiwei Bie 
16531ce9e605STiwei Bie 	vq->packed.desc_extra = kmalloc_array(num,
16541ce9e605STiwei Bie 			sizeof(struct vring_desc_extra_packed),
16551ce9e605STiwei Bie 			GFP_KERNEL);
16561ce9e605STiwei Bie 	if (!vq->packed.desc_extra)
16571ce9e605STiwei Bie 		goto err_desc_extra;
16581ce9e605STiwei Bie 
16591ce9e605STiwei Bie 	memset(vq->packed.desc_extra, 0,
16601ce9e605STiwei Bie 		num * sizeof(struct vring_desc_extra_packed));
16611ce9e605STiwei Bie 
16621ce9e605STiwei Bie 	/* No callback?  Tell other side not to bother us. */
16631ce9e605STiwei Bie 	if (!callback) {
16641ce9e605STiwei Bie 		vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
16651ce9e605STiwei Bie 		vq->packed.vring.driver->flags =
16661ce9e605STiwei Bie 			cpu_to_le16(vq->packed.event_flags_shadow);
16671ce9e605STiwei Bie 	}
16681ce9e605STiwei Bie 
16691ce9e605STiwei Bie 	return &vq->vq;
16701ce9e605STiwei Bie 
16711ce9e605STiwei Bie err_desc_extra:
16721ce9e605STiwei Bie 	kfree(vq->packed.desc_state);
16731ce9e605STiwei Bie err_desc_state:
16741ce9e605STiwei Bie 	kfree(vq);
16751ce9e605STiwei Bie err_vq:
16761ce9e605STiwei Bie 	vring_free_queue(vdev, event_size_in_bytes, device, ring_dma_addr);
16771ce9e605STiwei Bie err_device:
16781ce9e605STiwei Bie 	vring_free_queue(vdev, event_size_in_bytes, driver, ring_dma_addr);
16791ce9e605STiwei Bie err_driver:
16801ce9e605STiwei Bie 	vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr);
16811ce9e605STiwei Bie err_ring:
16821ce9e605STiwei Bie 	return NULL;
16831ce9e605STiwei Bie }
16841ce9e605STiwei Bie 
16851ce9e605STiwei Bie 
16861ce9e605STiwei Bie /*
1687e6f633e5STiwei Bie  * Generic functions and exported symbols.
1688e6f633e5STiwei Bie  */
1689e6f633e5STiwei Bie 
1690e6f633e5STiwei Bie static inline int virtqueue_add(struct virtqueue *_vq,
1691e6f633e5STiwei Bie 				struct scatterlist *sgs[],
1692e6f633e5STiwei Bie 				unsigned int total_sg,
1693e6f633e5STiwei Bie 				unsigned int out_sgs,
1694e6f633e5STiwei Bie 				unsigned int in_sgs,
1695e6f633e5STiwei Bie 				void *data,
1696e6f633e5STiwei Bie 				void *ctx,
1697e6f633e5STiwei Bie 				gfp_t gfp)
1698e6f633e5STiwei Bie {
16991ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
17001ce9e605STiwei Bie 
17011ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
17021ce9e605STiwei Bie 					out_sgs, in_sgs, data, ctx, gfp) :
17031ce9e605STiwei Bie 				 virtqueue_add_split(_vq, sgs, total_sg,
1704e6f633e5STiwei Bie 					out_sgs, in_sgs, data, ctx, gfp);
1705e6f633e5STiwei Bie }
1706e6f633e5STiwei Bie 
1707e6f633e5STiwei Bie /**
1708e6f633e5STiwei Bie  * virtqueue_add_sgs - expose buffers to other end
1709a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1710e6f633e5STiwei Bie  * @sgs: array of terminated scatterlists.
1711a5581206SJiang Biao  * @out_sgs: the number of scatterlists readable by other side
1712a5581206SJiang Biao  * @in_sgs: the number of scatterlists which are writable (after readable ones)
1713e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1714e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1715e6f633e5STiwei Bie  *
1716e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1717e6f633e5STiwei Bie  * at the same time (except where noted).
1718e6f633e5STiwei Bie  *
1719e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1720e6f633e5STiwei Bie  */
1721e6f633e5STiwei Bie int virtqueue_add_sgs(struct virtqueue *_vq,
1722e6f633e5STiwei Bie 		      struct scatterlist *sgs[],
1723e6f633e5STiwei Bie 		      unsigned int out_sgs,
1724e6f633e5STiwei Bie 		      unsigned int in_sgs,
1725e6f633e5STiwei Bie 		      void *data,
1726e6f633e5STiwei Bie 		      gfp_t gfp)
1727e6f633e5STiwei Bie {
1728e6f633e5STiwei Bie 	unsigned int i, total_sg = 0;
1729e6f633e5STiwei Bie 
1730e6f633e5STiwei Bie 	/* Count them first. */
1731e6f633e5STiwei Bie 	for (i = 0; i < out_sgs + in_sgs; i++) {
1732e6f633e5STiwei Bie 		struct scatterlist *sg;
1733e6f633e5STiwei Bie 
1734e6f633e5STiwei Bie 		for (sg = sgs[i]; sg; sg = sg_next(sg))
1735e6f633e5STiwei Bie 			total_sg++;
1736e6f633e5STiwei Bie 	}
1737e6f633e5STiwei Bie 	return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
1738e6f633e5STiwei Bie 			     data, NULL, gfp);
1739e6f633e5STiwei Bie }
1740e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
1741e6f633e5STiwei Bie 
1742e6f633e5STiwei Bie /**
1743e6f633e5STiwei Bie  * virtqueue_add_outbuf - expose output buffers to other end
1744e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
1745e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
1746e6f633e5STiwei Bie  * @num: the number of entries in @sg readable by other side
1747e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1748e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1749e6f633e5STiwei Bie  *
1750e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1751e6f633e5STiwei Bie  * at the same time (except where noted).
1752e6f633e5STiwei Bie  *
1753e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1754e6f633e5STiwei Bie  */
1755e6f633e5STiwei Bie int virtqueue_add_outbuf(struct virtqueue *vq,
1756e6f633e5STiwei Bie 			 struct scatterlist *sg, unsigned int num,
1757e6f633e5STiwei Bie 			 void *data,
1758e6f633e5STiwei Bie 			 gfp_t gfp)
1759e6f633e5STiwei Bie {
1760e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
1761e6f633e5STiwei Bie }
1762e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
1763e6f633e5STiwei Bie 
1764e6f633e5STiwei Bie /**
1765e6f633e5STiwei Bie  * virtqueue_add_inbuf - expose input buffers to other end
1766e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
1767e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
1768e6f633e5STiwei Bie  * @num: the number of entries in @sg writable by other side
1769e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1770e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1771e6f633e5STiwei Bie  *
1772e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1773e6f633e5STiwei Bie  * at the same time (except where noted).
1774e6f633e5STiwei Bie  *
1775e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1776e6f633e5STiwei Bie  */
1777e6f633e5STiwei Bie int virtqueue_add_inbuf(struct virtqueue *vq,
1778e6f633e5STiwei Bie 			struct scatterlist *sg, unsigned int num,
1779e6f633e5STiwei Bie 			void *data,
1780e6f633e5STiwei Bie 			gfp_t gfp)
1781e6f633e5STiwei Bie {
1782e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
1783e6f633e5STiwei Bie }
1784e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
1785e6f633e5STiwei Bie 
1786e6f633e5STiwei Bie /**
1787e6f633e5STiwei Bie  * virtqueue_add_inbuf_ctx - expose input buffers to other end
1788e6f633e5STiwei Bie  * @vq: the struct virtqueue we're talking about.
1789e6f633e5STiwei Bie  * @sg: scatterlist (must be well-formed and terminated!)
1790e6f633e5STiwei Bie  * @num: the number of entries in @sg writable by other side
1791e6f633e5STiwei Bie  * @data: the token identifying the buffer.
1792e6f633e5STiwei Bie  * @ctx: extra context for the token
1793e6f633e5STiwei Bie  * @gfp: how to do memory allocations (if necessary).
1794e6f633e5STiwei Bie  *
1795e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue operations
1796e6f633e5STiwei Bie  * at the same time (except where noted).
1797e6f633e5STiwei Bie  *
1798e6f633e5STiwei Bie  * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1799e6f633e5STiwei Bie  */
1800e6f633e5STiwei Bie int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
1801e6f633e5STiwei Bie 			struct scatterlist *sg, unsigned int num,
1802e6f633e5STiwei Bie 			void *data,
1803e6f633e5STiwei Bie 			void *ctx,
1804e6f633e5STiwei Bie 			gfp_t gfp)
1805e6f633e5STiwei Bie {
1806e6f633e5STiwei Bie 	return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
1807e6f633e5STiwei Bie }
1808e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
1809e6f633e5STiwei Bie 
1810e6f633e5STiwei Bie /**
1811e6f633e5STiwei Bie  * virtqueue_kick_prepare - first half of split virtqueue_kick call.
1812a5581206SJiang Biao  * @_vq: the struct virtqueue
1813e6f633e5STiwei Bie  *
1814e6f633e5STiwei Bie  * Instead of virtqueue_kick(), you can do:
1815e6f633e5STiwei Bie  *	if (virtqueue_kick_prepare(vq))
1816e6f633e5STiwei Bie  *		virtqueue_notify(vq);
1817e6f633e5STiwei Bie  *
1818e6f633e5STiwei Bie  * This is sometimes useful because the virtqueue_kick_prepare() needs
1819e6f633e5STiwei Bie  * to be serialized, but the actual virtqueue_notify() call does not.
1820e6f633e5STiwei Bie  */
1821e6f633e5STiwei Bie bool virtqueue_kick_prepare(struct virtqueue *_vq)
1822e6f633e5STiwei Bie {
18231ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
18241ce9e605STiwei Bie 
18251ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
18261ce9e605STiwei Bie 				 virtqueue_kick_prepare_split(_vq);
1827e6f633e5STiwei Bie }
1828e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
1829e6f633e5STiwei Bie 
1830e6f633e5STiwei Bie /**
1831e6f633e5STiwei Bie  * virtqueue_notify - second half of split virtqueue_kick call.
1832a5581206SJiang Biao  * @_vq: the struct virtqueue
1833e6f633e5STiwei Bie  *
1834e6f633e5STiwei Bie  * This does not need to be serialized.
1835e6f633e5STiwei Bie  *
1836e6f633e5STiwei Bie  * Returns false if host notify failed or queue is broken, otherwise true.
1837e6f633e5STiwei Bie  */
1838e6f633e5STiwei Bie bool virtqueue_notify(struct virtqueue *_vq)
1839e6f633e5STiwei Bie {
1840e6f633e5STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1841e6f633e5STiwei Bie 
1842e6f633e5STiwei Bie 	if (unlikely(vq->broken))
1843e6f633e5STiwei Bie 		return false;
1844e6f633e5STiwei Bie 
1845e6f633e5STiwei Bie 	/* Prod other side to tell it about changes. */
1846e6f633e5STiwei Bie 	if (!vq->notify(_vq)) {
1847e6f633e5STiwei Bie 		vq->broken = true;
1848e6f633e5STiwei Bie 		return false;
1849e6f633e5STiwei Bie 	}
1850e6f633e5STiwei Bie 	return true;
1851e6f633e5STiwei Bie }
1852e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_notify);
1853e6f633e5STiwei Bie 
1854e6f633e5STiwei Bie /**
1855e6f633e5STiwei Bie  * virtqueue_kick - update after add_buf
1856e6f633e5STiwei Bie  * @vq: the struct virtqueue
1857e6f633e5STiwei Bie  *
1858e6f633e5STiwei Bie  * After one or more virtqueue_add_* calls, invoke this to kick
1859e6f633e5STiwei Bie  * the other side.
1860e6f633e5STiwei Bie  *
1861e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1862e6f633e5STiwei Bie  * operations at the same time (except where noted).
1863e6f633e5STiwei Bie  *
1864e6f633e5STiwei Bie  * Returns false if kick failed, otherwise true.
1865e6f633e5STiwei Bie  */
1866e6f633e5STiwei Bie bool virtqueue_kick(struct virtqueue *vq)
1867e6f633e5STiwei Bie {
1868e6f633e5STiwei Bie 	if (virtqueue_kick_prepare(vq))
1869e6f633e5STiwei Bie 		return virtqueue_notify(vq);
1870e6f633e5STiwei Bie 	return true;
1871e6f633e5STiwei Bie }
1872e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_kick);
1873e6f633e5STiwei Bie 
1874e6f633e5STiwei Bie /**
1875e6f633e5STiwei Bie  * virtqueue_get_buf - get the next used buffer
1876a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1877e6f633e5STiwei Bie  * @len: the length written into the buffer
1878a5581206SJiang Biao  * @ctx: extra context for the token
1879e6f633e5STiwei Bie  *
1880e6f633e5STiwei Bie  * If the device wrote data into the buffer, @len will be set to the
1881e6f633e5STiwei Bie  * amount written.  This means you don't need to clear the buffer
1882e6f633e5STiwei Bie  * beforehand to ensure there's no data leakage in the case of short
1883e6f633e5STiwei Bie  * writes.
1884e6f633e5STiwei Bie  *
1885e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1886e6f633e5STiwei Bie  * operations at the same time (except where noted).
1887e6f633e5STiwei Bie  *
1888e6f633e5STiwei Bie  * Returns NULL if there are no used buffers, or the "data" token
1889e6f633e5STiwei Bie  * handed to virtqueue_add_*().
1890e6f633e5STiwei Bie  */
1891e6f633e5STiwei Bie void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
1892e6f633e5STiwei Bie 			    void **ctx)
1893e6f633e5STiwei Bie {
18941ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
18951ce9e605STiwei Bie 
18961ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
18971ce9e605STiwei Bie 				 virtqueue_get_buf_ctx_split(_vq, len, ctx);
1898e6f633e5STiwei Bie }
1899e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
1900e6f633e5STiwei Bie 
1901e6f633e5STiwei Bie void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
1902e6f633e5STiwei Bie {
1903e6f633e5STiwei Bie 	return virtqueue_get_buf_ctx(_vq, len, NULL);
1904e6f633e5STiwei Bie }
1905e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_get_buf);
1906e6f633e5STiwei Bie /**
1907e6f633e5STiwei Bie  * virtqueue_disable_cb - disable callbacks
1908a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1909e6f633e5STiwei Bie  *
1910e6f633e5STiwei Bie  * Note that this is not necessarily synchronous, hence unreliable and only
1911e6f633e5STiwei Bie  * useful as an optimization.
1912e6f633e5STiwei Bie  *
1913e6f633e5STiwei Bie  * Unlike other operations, this need not be serialized.
1914e6f633e5STiwei Bie  */
1915e6f633e5STiwei Bie void virtqueue_disable_cb(struct virtqueue *_vq)
1916e6f633e5STiwei Bie {
19171ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
19181ce9e605STiwei Bie 
19191ce9e605STiwei Bie 	if (vq->packed_ring)
19201ce9e605STiwei Bie 		virtqueue_disable_cb_packed(_vq);
19211ce9e605STiwei Bie 	else
1922e6f633e5STiwei Bie 		virtqueue_disable_cb_split(_vq);
1923e6f633e5STiwei Bie }
1924e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
1925e6f633e5STiwei Bie 
1926e6f633e5STiwei Bie /**
1927e6f633e5STiwei Bie  * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
1928a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1929e6f633e5STiwei Bie  *
1930e6f633e5STiwei Bie  * This re-enables callbacks; it returns current queue state
1931e6f633e5STiwei Bie  * in an opaque unsigned value. This value should be later tested by
1932e6f633e5STiwei Bie  * virtqueue_poll, to detect a possible race between the driver checking for
1933e6f633e5STiwei Bie  * more work, and enabling callbacks.
1934e6f633e5STiwei Bie  *
1935e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1936e6f633e5STiwei Bie  * operations at the same time (except where noted).
1937e6f633e5STiwei Bie  */
1938e6f633e5STiwei Bie unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
1939e6f633e5STiwei Bie {
19401ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
19411ce9e605STiwei Bie 
19421ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
19431ce9e605STiwei Bie 				 virtqueue_enable_cb_prepare_split(_vq);
1944e6f633e5STiwei Bie }
1945e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
1946e6f633e5STiwei Bie 
1947e6f633e5STiwei Bie /**
1948e6f633e5STiwei Bie  * virtqueue_poll - query pending used buffers
1949a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1950e6f633e5STiwei Bie  * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
1951e6f633e5STiwei Bie  *
1952e6f633e5STiwei Bie  * Returns "true" if there are pending used buffers in the queue.
1953e6f633e5STiwei Bie  *
1954e6f633e5STiwei Bie  * This does not need to be serialized.
1955e6f633e5STiwei Bie  */
1956e6f633e5STiwei Bie bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
1957e6f633e5STiwei Bie {
1958e6f633e5STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
1959e6f633e5STiwei Bie 
1960e6f633e5STiwei Bie 	virtio_mb(vq->weak_barriers);
19611ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
19621ce9e605STiwei Bie 				 virtqueue_poll_split(_vq, last_used_idx);
1963e6f633e5STiwei Bie }
1964e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_poll);
1965e6f633e5STiwei Bie 
1966e6f633e5STiwei Bie /**
1967e6f633e5STiwei Bie  * virtqueue_enable_cb - restart callbacks after disable_cb.
1968a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1969e6f633e5STiwei Bie  *
1970e6f633e5STiwei Bie  * This re-enables callbacks; it returns "false" if there are pending
1971e6f633e5STiwei Bie  * buffers in the queue, to detect a possible race between the driver
1972e6f633e5STiwei Bie  * checking for more work, and enabling callbacks.
1973e6f633e5STiwei Bie  *
1974e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1975e6f633e5STiwei Bie  * operations at the same time (except where noted).
1976e6f633e5STiwei Bie  */
1977e6f633e5STiwei Bie bool virtqueue_enable_cb(struct virtqueue *_vq)
1978e6f633e5STiwei Bie {
1979e6f633e5STiwei Bie 	unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
1980e6f633e5STiwei Bie 
1981e6f633e5STiwei Bie 	return !virtqueue_poll(_vq, last_used_idx);
1982e6f633e5STiwei Bie }
1983e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
1984e6f633e5STiwei Bie 
1985e6f633e5STiwei Bie /**
1986e6f633e5STiwei Bie  * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
1987a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
1988e6f633e5STiwei Bie  *
1989e6f633e5STiwei Bie  * This re-enables callbacks but hints to the other side to delay
1990e6f633e5STiwei Bie  * interrupts until most of the available buffers have been processed;
1991e6f633e5STiwei Bie  * it returns "false" if there are many pending buffers in the queue,
1992e6f633e5STiwei Bie  * to detect a possible race between the driver checking for more work,
1993e6f633e5STiwei Bie  * and enabling callbacks.
1994e6f633e5STiwei Bie  *
1995e6f633e5STiwei Bie  * Caller must ensure we don't call this with other virtqueue
1996e6f633e5STiwei Bie  * operations at the same time (except where noted).
1997e6f633e5STiwei Bie  */
1998e6f633e5STiwei Bie bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
1999e6f633e5STiwei Bie {
20001ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
20011ce9e605STiwei Bie 
20021ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
20031ce9e605STiwei Bie 				 virtqueue_enable_cb_delayed_split(_vq);
2004e6f633e5STiwei Bie }
2005e6f633e5STiwei Bie EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2006e6f633e5STiwei Bie 
2007138fd251STiwei Bie /**
2008138fd251STiwei Bie  * virtqueue_detach_unused_buf - detach first unused buffer
2009a5581206SJiang Biao  * @_vq: the struct virtqueue we're talking about.
2010138fd251STiwei Bie  *
2011138fd251STiwei Bie  * Returns NULL or the "data" token handed to virtqueue_add_*().
2012138fd251STiwei Bie  * This is not valid on an active queue; it is useful only for device
2013138fd251STiwei Bie  * shutdown.
2014138fd251STiwei Bie  */
2015138fd251STiwei Bie void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2016138fd251STiwei Bie {
20171ce9e605STiwei Bie 	struct vring_virtqueue *vq = to_vvq(_vq);
20181ce9e605STiwei Bie 
20191ce9e605STiwei Bie 	return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
20201ce9e605STiwei Bie 				 virtqueue_detach_unused_buf_split(_vq);
2021138fd251STiwei Bie }
20227c5e9ed0SMichael S. Tsirkin EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
2023c021eac4SShirley Ma 
2024138fd251STiwei Bie static inline bool more_used(const struct vring_virtqueue *vq)
2025138fd251STiwei Bie {
20261ce9e605STiwei Bie 	return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
2027138fd251STiwei Bie }
2028138fd251STiwei Bie 
20290a8a69ddSRusty Russell irqreturn_t vring_interrupt(int irq, void *_vq)
20300a8a69ddSRusty Russell {
20310a8a69ddSRusty Russell 	struct vring_virtqueue *vq = to_vvq(_vq);
20320a8a69ddSRusty Russell 
20330a8a69ddSRusty Russell 	if (!more_used(vq)) {
20340a8a69ddSRusty Russell 		pr_debug("virtqueue interrupt with no work for %p\n", vq);
20350a8a69ddSRusty Russell 		return IRQ_NONE;
20360a8a69ddSRusty Russell 	}
20370a8a69ddSRusty Russell 
20380a8a69ddSRusty Russell 	if (unlikely(vq->broken))
20390a8a69ddSRusty Russell 		return IRQ_HANDLED;
20400a8a69ddSRusty Russell 
20410a8a69ddSRusty Russell 	pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
204218445c4dSRusty Russell 	if (vq->vq.callback)
204318445c4dSRusty Russell 		vq->vq.callback(&vq->vq);
20440a8a69ddSRusty Russell 
20450a8a69ddSRusty Russell 	return IRQ_HANDLED;
20460a8a69ddSRusty Russell }
2047c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_interrupt);
20480a8a69ddSRusty Russell 
20491ce9e605STiwei Bie /* Only available for split ring */
20502a2d1382SAndy Lutomirski struct virtqueue *__vring_new_virtqueue(unsigned int index,
20512a2d1382SAndy Lutomirski 					struct vring vring,
20520a8a69ddSRusty Russell 					struct virtio_device *vdev,
20537b21e34fSRusty Russell 					bool weak_barriers,
2054f94682ddSMichael S. Tsirkin 					bool context,
205546f9c2b9SHeinz Graalfs 					bool (*notify)(struct virtqueue *),
20569499f5e7SRusty Russell 					void (*callback)(struct virtqueue *),
20579499f5e7SRusty Russell 					const char *name)
20580a8a69ddSRusty Russell {
20590a8a69ddSRusty Russell 	unsigned int i;
20602a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq;
20610a8a69ddSRusty Russell 
20621ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
20631ce9e605STiwei Bie 		return NULL;
20641ce9e605STiwei Bie 
2065cbeedb72STiwei Bie 	vq = kmalloc(sizeof(*vq), GFP_KERNEL);
20660a8a69ddSRusty Russell 	if (!vq)
20670a8a69ddSRusty Russell 		return NULL;
20680a8a69ddSRusty Russell 
20691ce9e605STiwei Bie 	vq->packed_ring = false;
20700a8a69ddSRusty Russell 	vq->vq.callback = callback;
20710a8a69ddSRusty Russell 	vq->vq.vdev = vdev;
20729499f5e7SRusty Russell 	vq->vq.name = name;
20732a2d1382SAndy Lutomirski 	vq->vq.num_free = vring.num;
207406ca287dSRusty Russell 	vq->vq.index = index;
20752a2d1382SAndy Lutomirski 	vq->we_own_ring = false;
20760a8a69ddSRusty Russell 	vq->notify = notify;
20777b21e34fSRusty Russell 	vq->weak_barriers = weak_barriers;
20780a8a69ddSRusty Russell 	vq->broken = false;
20790a8a69ddSRusty Russell 	vq->last_used_idx = 0;
20800a8a69ddSRusty Russell 	vq->num_added = 0;
2081fb3fba6bSTiwei Bie 	vq->use_dma_api = vring_use_dma_api(vdev);
20829499f5e7SRusty Russell 	list_add_tail(&vq->vq.list, &vdev->vqs);
20830a8a69ddSRusty Russell #ifdef DEBUG
20840a8a69ddSRusty Russell 	vq->in_use = false;
2085e93300b1SRusty Russell 	vq->last_add_time_valid = false;
20860a8a69ddSRusty Russell #endif
20870a8a69ddSRusty Russell 
20885a08b04fSMichael S. Tsirkin 	vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
20895a08b04fSMichael S. Tsirkin 		!context;
2090a5c262c5SMichael S. Tsirkin 	vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
20919fa29b9dSMark McLoughlin 
209245383fb0STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
209345383fb0STiwei Bie 		vq->weak_barriers = false;
209445383fb0STiwei Bie 
2095d79dca75STiwei Bie 	vq->split.queue_dma_addr = 0;
2096d79dca75STiwei Bie 	vq->split.queue_size_in_bytes = 0;
2097d79dca75STiwei Bie 
2098e593bf97STiwei Bie 	vq->split.vring = vring;
2099e593bf97STiwei Bie 	vq->split.avail_flags_shadow = 0;
2100e593bf97STiwei Bie 	vq->split.avail_idx_shadow = 0;
2101e593bf97STiwei Bie 
21020a8a69ddSRusty Russell 	/* No callback?  Tell other side not to bother us. */
2103f277ec42SVenkatesh Srinivas 	if (!callback) {
2104e593bf97STiwei Bie 		vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
21050ea1e4a6SLadi Prosek 		if (!vq->event)
2106e593bf97STiwei Bie 			vq->split.vring.avail->flags = cpu_to_virtio16(vdev,
2107e593bf97STiwei Bie 					vq->split.avail_flags_shadow);
2108f277ec42SVenkatesh Srinivas 	}
21090a8a69ddSRusty Russell 
2110cbeedb72STiwei Bie 	vq->split.desc_state = kmalloc_array(vring.num,
2111cbeedb72STiwei Bie 			sizeof(struct vring_desc_state_split), GFP_KERNEL);
2112cbeedb72STiwei Bie 	if (!vq->split.desc_state) {
2113cbeedb72STiwei Bie 		kfree(vq);
2114cbeedb72STiwei Bie 		return NULL;
2115cbeedb72STiwei Bie 	}
2116cbeedb72STiwei Bie 
21170a8a69ddSRusty Russell 	/* Put everything in free lists. */
21180a8a69ddSRusty Russell 	vq->free_head = 0;
21192a2d1382SAndy Lutomirski 	for (i = 0; i < vring.num-1; i++)
2120e593bf97STiwei Bie 		vq->split.vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
2121cbeedb72STiwei Bie 	memset(vq->split.desc_state, 0, vring.num *
2122cbeedb72STiwei Bie 			sizeof(struct vring_desc_state_split));
21230a8a69ddSRusty Russell 
21240a8a69ddSRusty Russell 	return &vq->vq;
21250a8a69ddSRusty Russell }
21262a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
21272a2d1382SAndy Lutomirski 
21282a2d1382SAndy Lutomirski struct virtqueue *vring_create_virtqueue(
21292a2d1382SAndy Lutomirski 	unsigned int index,
21302a2d1382SAndy Lutomirski 	unsigned int num,
21312a2d1382SAndy Lutomirski 	unsigned int vring_align,
21322a2d1382SAndy Lutomirski 	struct virtio_device *vdev,
21332a2d1382SAndy Lutomirski 	bool weak_barriers,
21342a2d1382SAndy Lutomirski 	bool may_reduce_num,
2135f94682ddSMichael S. Tsirkin 	bool context,
21362a2d1382SAndy Lutomirski 	bool (*notify)(struct virtqueue *),
21372a2d1382SAndy Lutomirski 	void (*callback)(struct virtqueue *),
21382a2d1382SAndy Lutomirski 	const char *name)
21392a2d1382SAndy Lutomirski {
21401ce9e605STiwei Bie 
21411ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
21421ce9e605STiwei Bie 		return vring_create_virtqueue_packed(index, num, vring_align,
21431ce9e605STiwei Bie 				vdev, weak_barriers, may_reduce_num,
21441ce9e605STiwei Bie 				context, notify, callback, name);
21451ce9e605STiwei Bie 
2146d79dca75STiwei Bie 	return vring_create_virtqueue_split(index, num, vring_align,
2147d79dca75STiwei Bie 			vdev, weak_barriers, may_reduce_num,
2148d79dca75STiwei Bie 			context, notify, callback, name);
21492a2d1382SAndy Lutomirski }
21502a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(vring_create_virtqueue);
21512a2d1382SAndy Lutomirski 
21521ce9e605STiwei Bie /* Only available for split ring */
21532a2d1382SAndy Lutomirski struct virtqueue *vring_new_virtqueue(unsigned int index,
21542a2d1382SAndy Lutomirski 				      unsigned int num,
21552a2d1382SAndy Lutomirski 				      unsigned int vring_align,
21562a2d1382SAndy Lutomirski 				      struct virtio_device *vdev,
21572a2d1382SAndy Lutomirski 				      bool weak_barriers,
2158f94682ddSMichael S. Tsirkin 				      bool context,
21592a2d1382SAndy Lutomirski 				      void *pages,
21602a2d1382SAndy Lutomirski 				      bool (*notify)(struct virtqueue *vq),
21612a2d1382SAndy Lutomirski 				      void (*callback)(struct virtqueue *vq),
21622a2d1382SAndy Lutomirski 				      const char *name)
21632a2d1382SAndy Lutomirski {
21642a2d1382SAndy Lutomirski 	struct vring vring;
21651ce9e605STiwei Bie 
21661ce9e605STiwei Bie 	if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
21671ce9e605STiwei Bie 		return NULL;
21681ce9e605STiwei Bie 
21692a2d1382SAndy Lutomirski 	vring_init(&vring, num, pages, vring_align);
2170f94682ddSMichael S. Tsirkin 	return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
21712a2d1382SAndy Lutomirski 				     notify, callback, name);
21722a2d1382SAndy Lutomirski }
2173c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_new_virtqueue);
21740a8a69ddSRusty Russell 
21752a2d1382SAndy Lutomirski void vring_del_virtqueue(struct virtqueue *_vq)
21760a8a69ddSRusty Russell {
21772a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq = to_vvq(_vq);
21782a2d1382SAndy Lutomirski 
21792a2d1382SAndy Lutomirski 	if (vq->we_own_ring) {
21801ce9e605STiwei Bie 		if (vq->packed_ring) {
21811ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
21821ce9e605STiwei Bie 					 vq->packed.ring_size_in_bytes,
21831ce9e605STiwei Bie 					 vq->packed.vring.desc,
21841ce9e605STiwei Bie 					 vq->packed.ring_dma_addr);
21851ce9e605STiwei Bie 
21861ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
21871ce9e605STiwei Bie 					 vq->packed.event_size_in_bytes,
21881ce9e605STiwei Bie 					 vq->packed.vring.driver,
21891ce9e605STiwei Bie 					 vq->packed.driver_event_dma_addr);
21901ce9e605STiwei Bie 
21911ce9e605STiwei Bie 			vring_free_queue(vq->vq.vdev,
21921ce9e605STiwei Bie 					 vq->packed.event_size_in_bytes,
21931ce9e605STiwei Bie 					 vq->packed.vring.device,
21941ce9e605STiwei Bie 					 vq->packed.device_event_dma_addr);
21951ce9e605STiwei Bie 
21961ce9e605STiwei Bie 			kfree(vq->packed.desc_state);
21971ce9e605STiwei Bie 			kfree(vq->packed.desc_extra);
21981ce9e605STiwei Bie 		} else {
2199d79dca75STiwei Bie 			vring_free_queue(vq->vq.vdev,
2200d79dca75STiwei Bie 					 vq->split.queue_size_in_bytes,
2201d79dca75STiwei Bie 					 vq->split.vring.desc,
2202d79dca75STiwei Bie 					 vq->split.queue_dma_addr);
22031ce9e605STiwei Bie 
2204cbeedb72STiwei Bie 			kfree(vq->split.desc_state);
22052a2d1382SAndy Lutomirski 		}
22061ce9e605STiwei Bie 	}
22072a2d1382SAndy Lutomirski 	list_del(&_vq->list);
22082a2d1382SAndy Lutomirski 	kfree(vq);
22090a8a69ddSRusty Russell }
2210c6fd4701SRusty Russell EXPORT_SYMBOL_GPL(vring_del_virtqueue);
22110a8a69ddSRusty Russell 
2212e34f8725SRusty Russell /* Manipulates transport-specific feature bits. */
2213e34f8725SRusty Russell void vring_transport_features(struct virtio_device *vdev)
2214e34f8725SRusty Russell {
2215e34f8725SRusty Russell 	unsigned int i;
2216e34f8725SRusty Russell 
2217e34f8725SRusty Russell 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2218e34f8725SRusty Russell 		switch (i) {
22199fa29b9dSMark McLoughlin 		case VIRTIO_RING_F_INDIRECT_DESC:
22209fa29b9dSMark McLoughlin 			break;
2221a5c262c5SMichael S. Tsirkin 		case VIRTIO_RING_F_EVENT_IDX:
2222a5c262c5SMichael S. Tsirkin 			break;
2223747ae34aSMichael S. Tsirkin 		case VIRTIO_F_VERSION_1:
2224747ae34aSMichael S. Tsirkin 			break;
22251a937693SMichael S. Tsirkin 		case VIRTIO_F_IOMMU_PLATFORM:
22261a937693SMichael S. Tsirkin 			break;
2227f959a128STiwei Bie 		case VIRTIO_F_RING_PACKED:
2228f959a128STiwei Bie 			break;
222945383fb0STiwei Bie 		case VIRTIO_F_ORDER_PLATFORM:
223045383fb0STiwei Bie 			break;
2231e34f8725SRusty Russell 		default:
2232e34f8725SRusty Russell 			/* We don't understand this bit. */
2233e16e12beSMichael S. Tsirkin 			__virtio_clear_bit(vdev, i);
2234e34f8725SRusty Russell 		}
2235e34f8725SRusty Russell 	}
2236e34f8725SRusty Russell }
2237e34f8725SRusty Russell EXPORT_SYMBOL_GPL(vring_transport_features);
2238e34f8725SRusty Russell 
22395dfc1762SRusty Russell /**
22405dfc1762SRusty Russell  * virtqueue_get_vring_size - return the size of the virtqueue's vring
2241a5581206SJiang Biao  * @_vq: the struct virtqueue containing the vring of interest.
22425dfc1762SRusty Russell  *
22435dfc1762SRusty Russell  * Returns the size of the vring.  This is mainly used for boasting to
22445dfc1762SRusty Russell  * userspace.  Unlike other operations, this need not be serialized.
22455dfc1762SRusty Russell  */
22468f9f4668SRick Jones unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
22478f9f4668SRick Jones {
22488f9f4668SRick Jones 
22498f9f4668SRick Jones 	struct vring_virtqueue *vq = to_vvq(_vq);
22508f9f4668SRick Jones 
22511ce9e605STiwei Bie 	return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
22528f9f4668SRick Jones }
22538f9f4668SRick Jones EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
22548f9f4668SRick Jones 
2255b3b32c94SHeinz Graalfs bool virtqueue_is_broken(struct virtqueue *_vq)
2256b3b32c94SHeinz Graalfs {
2257b3b32c94SHeinz Graalfs 	struct vring_virtqueue *vq = to_vvq(_vq);
2258b3b32c94SHeinz Graalfs 
2259b3b32c94SHeinz Graalfs 	return vq->broken;
2260b3b32c94SHeinz Graalfs }
2261b3b32c94SHeinz Graalfs EXPORT_SYMBOL_GPL(virtqueue_is_broken);
2262b3b32c94SHeinz Graalfs 
2263e2dcdfe9SRusty Russell /*
2264e2dcdfe9SRusty Russell  * This should prevent the device from being used, allowing drivers to
2265e2dcdfe9SRusty Russell  * recover.  You may need to grab appropriate locks to flush.
2266e2dcdfe9SRusty Russell  */
2267e2dcdfe9SRusty Russell void virtio_break_device(struct virtio_device *dev)
2268e2dcdfe9SRusty Russell {
2269e2dcdfe9SRusty Russell 	struct virtqueue *_vq;
2270e2dcdfe9SRusty Russell 
2271e2dcdfe9SRusty Russell 	list_for_each_entry(_vq, &dev->vqs, list) {
2272e2dcdfe9SRusty Russell 		struct vring_virtqueue *vq = to_vvq(_vq);
2273e2dcdfe9SRusty Russell 		vq->broken = true;
2274e2dcdfe9SRusty Russell 	}
2275e2dcdfe9SRusty Russell }
2276e2dcdfe9SRusty Russell EXPORT_SYMBOL_GPL(virtio_break_device);
2277e2dcdfe9SRusty Russell 
22782a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
227989062652SCornelia Huck {
228089062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
228189062652SCornelia Huck 
22822a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
228389062652SCornelia Huck 
22841ce9e605STiwei Bie 	if (vq->packed_ring)
22851ce9e605STiwei Bie 		return vq->packed.ring_dma_addr;
22861ce9e605STiwei Bie 
2287d79dca75STiwei Bie 	return vq->split.queue_dma_addr;
22882a2d1382SAndy Lutomirski }
22892a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
22902a2d1382SAndy Lutomirski 
22912a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
229289062652SCornelia Huck {
229389062652SCornelia Huck 	struct vring_virtqueue *vq = to_vvq(_vq);
229489062652SCornelia Huck 
22952a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
22962a2d1382SAndy Lutomirski 
22971ce9e605STiwei Bie 	if (vq->packed_ring)
22981ce9e605STiwei Bie 		return vq->packed.driver_event_dma_addr;
22991ce9e605STiwei Bie 
2300d79dca75STiwei Bie 	return vq->split.queue_dma_addr +
2301e593bf97STiwei Bie 		((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
230289062652SCornelia Huck }
23032a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
23042a2d1382SAndy Lutomirski 
23052a2d1382SAndy Lutomirski dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
23062a2d1382SAndy Lutomirski {
23072a2d1382SAndy Lutomirski 	struct vring_virtqueue *vq = to_vvq(_vq);
23082a2d1382SAndy Lutomirski 
23092a2d1382SAndy Lutomirski 	BUG_ON(!vq->we_own_ring);
23102a2d1382SAndy Lutomirski 
23111ce9e605STiwei Bie 	if (vq->packed_ring)
23121ce9e605STiwei Bie 		return vq->packed.device_event_dma_addr;
23131ce9e605STiwei Bie 
2314d79dca75STiwei Bie 	return vq->split.queue_dma_addr +
2315e593bf97STiwei Bie 		((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
23162a2d1382SAndy Lutomirski }
23172a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
23182a2d1382SAndy Lutomirski 
23191ce9e605STiwei Bie /* Only available for split ring */
23202a2d1382SAndy Lutomirski const struct vring *virtqueue_get_vring(struct virtqueue *vq)
23212a2d1382SAndy Lutomirski {
2322e593bf97STiwei Bie 	return &to_vvq(vq)->split.vring;
23232a2d1382SAndy Lutomirski }
23242a2d1382SAndy Lutomirski EXPORT_SYMBOL_GPL(virtqueue_get_vring);
232589062652SCornelia Huck 
2326c6fd4701SRusty Russell MODULE_LICENSE("GPL");
2327