1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Virtio ring implementation.
4 * Copyright 2007 Rusty Russell IBM Corporation
6 #include <linux/virtio.h>
7 #include <linux/virtio_ring.h>
8 #include <linux/virtio_config.h>
9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/hrtimer.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/kmsan.h>
15 #include <linux/spinlock.h>
19 /* For development, we want to crash whenever the ring is screwed. */
20 #define BAD_RING(_vq, fmt, args...) \
22 dev_err(&(_vq)->vq.vdev->dev, \
23 "%s:"fmt, (_vq)->vq.name, ##args); \
26 /* Caller is supposed to guarantee no reentry. */
27 #define START_USE(_vq) \
30 panic("%s:in_use = %i\n", \
31 (_vq)->vq.name, (_vq)->in_use); \
32 (_vq)->in_use = __LINE__; \
34 #define END_USE(_vq) \
35 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
36 #define LAST_ADD_TIME_UPDATE(_vq) \
38 ktime_t now = ktime_get(); \
40 /* No kick or get, with .1 second between? Warn. */ \
41 if ((_vq)->last_add_time_valid) \
42 WARN_ON(ktime_to_ms(ktime_sub(now, \
43 (_vq)->last_add_time)) > 100); \
44 (_vq)->last_add_time = now; \
45 (_vq)->last_add_time_valid = true; \
47 #define LAST_ADD_TIME_CHECK(_vq) \
49 if ((_vq)->last_add_time_valid) { \
50 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \
51 (_vq)->last_add_time)) > 100); \
54 #define LAST_ADD_TIME_INVALID(_vq) \
55 ((_vq)->last_add_time_valid = false)
57 #define BAD_RING(_vq, fmt, args...) \
59 dev_err(&_vq->vq.vdev->dev, \
60 "%s:"fmt, (_vq)->vq.name, ##args); \
61 (_vq)->broken = true; \
65 #define LAST_ADD_TIME_UPDATE(vq)
66 #define LAST_ADD_TIME_CHECK(vq)
67 #define LAST_ADD_TIME_INVALID(vq)
70 struct vring_desc_state_split {
71 void *data; /* Data for callback. */
72 struct vring_desc *indir_desc; /* Indirect descriptor, if any. */
75 struct vring_desc_state_packed {
76 void *data; /* Data for callback. */
77 struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */
78 u16 num; /* Descriptor list length. */
79 u16 last; /* The last desc state in a list. */
82 struct vring_desc_extra {
83 dma_addr_t addr; /* Descriptor DMA addr. */
84 u32 len; /* Descriptor length. */
85 u16 flags; /* Descriptor flags. */
86 u16 next; /* The next desc state in a list. */
89 struct vring_virtqueue_split {
90 /* Actual memory layout for this queue. */
93 /* Last written value to avail->flags */
94 u16 avail_flags_shadow;
97 * Last written value to avail->idx in
100 u16 avail_idx_shadow;
102 /* Per-descriptor state. */
103 struct vring_desc_state_split *desc_state;
104 struct vring_desc_extra *desc_extra;
106 /* DMA address and size information */
107 dma_addr_t queue_dma_addr;
108 size_t queue_size_in_bytes;
111 * The parameters for creating vrings are reserved for creating new
118 struct vring_virtqueue_packed {
119 /* Actual memory layout for this queue. */
122 struct vring_packed_desc *desc;
123 struct vring_packed_desc_event *driver;
124 struct vring_packed_desc_event *device;
127 /* Driver ring wrap counter. */
128 bool avail_wrap_counter;
130 /* Avail used flags. */
131 u16 avail_used_flags;
133 /* Index of the next avail descriptor. */
137 * Last written value to driver->flags in
140 u16 event_flags_shadow;
142 /* Per-descriptor state. */
143 struct vring_desc_state_packed *desc_state;
144 struct vring_desc_extra *desc_extra;
146 /* DMA address and size information */
147 dma_addr_t ring_dma_addr;
148 dma_addr_t driver_event_dma_addr;
149 dma_addr_t device_event_dma_addr;
150 size_t ring_size_in_bytes;
151 size_t event_size_in_bytes;
154 struct vring_virtqueue {
157 /* Is this a packed ring? */
160 /* Is DMA API used? */
163 /* Can we use weak barriers? */
166 /* Other side has made a mess, don't try any more. */
169 /* Host supports indirect buffers */
172 /* Host publishes avail event idx */
175 /* Head of free buffer list. */
176 unsigned int free_head;
177 /* Number we've added since last sync. */
178 unsigned int num_added;
180 /* Last used index we've seen.
181 * for split ring, it just contains last used index
183 * bits up to VRING_PACKED_EVENT_F_WRAP_CTR include the last used index.
184 * bits from VRING_PACKED_EVENT_F_WRAP_CTR include the used wrap counter.
188 /* Hint for event idx: already triggered no need to disable. */
189 bool event_triggered;
192 /* Available for split ring */
193 struct vring_virtqueue_split split;
195 /* Available for packed ring */
196 struct vring_virtqueue_packed packed;
199 /* How to notify other side. FIXME: commonalize hcalls! */
200 bool (*notify)(struct virtqueue *vq);
202 /* DMA, allocation, and size information */
205 /* Device used for doing DMA */
206 struct device *dma_dev;
209 /* They're supposed to lock for us. */
212 /* Figure out if their kicks are too delayed. */
213 bool last_add_time_valid;
214 ktime_t last_add_time;
218 static struct virtqueue *__vring_new_virtqueue(unsigned int index,
219 struct vring_virtqueue_split *vring_split,
220 struct virtio_device *vdev,
223 bool (*notify)(struct virtqueue *),
224 void (*callback)(struct virtqueue *),
226 struct device *dma_dev);
227 static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num);
228 static void vring_free(struct virtqueue *_vq);
234 #define to_vvq(_vq) container_of_const(_vq, struct vring_virtqueue, vq)
236 static bool virtqueue_use_indirect(const struct vring_virtqueue *vq,
237 unsigned int total_sg)
240 * If the host supports indirect descriptor tables, and we have multiple
241 * buffers, then go indirect. FIXME: tune this threshold
243 return (vq->indirect && total_sg > 1 && vq->vq.num_free);
247 * Modern virtio devices have feature bits to specify whether they need a
248 * quirk and bypass the IOMMU. If not there, just use the DMA API.
250 * If there, the interaction between virtio and DMA API is messy.
252 * On most systems with virtio, physical addresses match bus addresses,
253 * and it doesn't particularly matter whether we use the DMA API.
255 * On some systems, including Xen and any system with a physical device
256 * that speaks virtio behind a physical IOMMU, we must use the DMA API
257 * for virtio DMA to work at all.
259 * On other systems, including SPARC and PPC64, virtio-pci devices are
260 * enumerated as though they are behind an IOMMU, but the virtio host
261 * ignores the IOMMU, so we must either pretend that the IOMMU isn't
262 * there or somehow map everything as the identity.
264 * For the time being, we preserve historic behavior and bypass the DMA
267 * TODO: install a per-device DMA ops structure that does the right thing
268 * taking into account all the above quirks, and use the DMA API
269 * unconditionally on data path.
272 static bool vring_use_dma_api(const struct virtio_device *vdev)
274 if (!virtio_has_dma_quirk(vdev))
277 /* Otherwise, we are left to guess. */
279 * In theory, it's possible to have a buggy QEMU-supposed
280 * emulated Q35 IOMMU and Xen enabled at the same time. On
281 * such a configuration, virtio has never worked and will
282 * not work without an even larger kludge. Instead, enable
283 * the DMA API if we're a Xen guest, which at least allows
284 * all of the sensible Xen configurations to work correctly.
292 size_t virtio_max_dma_size(const struct virtio_device *vdev)
294 size_t max_segment_size = SIZE_MAX;
296 if (vring_use_dma_api(vdev))
297 max_segment_size = dma_max_mapping_size(vdev->dev.parent);
299 return max_segment_size;
301 EXPORT_SYMBOL_GPL(virtio_max_dma_size);
303 static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
304 dma_addr_t *dma_handle, gfp_t flag,
305 struct device *dma_dev)
307 if (vring_use_dma_api(vdev)) {
308 return dma_alloc_coherent(dma_dev, size,
311 void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
314 phys_addr_t phys_addr = virt_to_phys(queue);
315 *dma_handle = (dma_addr_t)phys_addr;
318 * Sanity check: make sure we dind't truncate
319 * the address. The only arches I can find that
320 * have 64-bit phys_addr_t but 32-bit dma_addr_t
321 * are certain non-highmem MIPS and x86
322 * configurations, but these configurations
323 * should never allocate physical pages above 32
324 * bits, so this is fine. Just in case, throw a
325 * warning and abort if we end up with an
326 * unrepresentable address.
328 if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
329 free_pages_exact(queue, PAGE_ALIGN(size));
337 static void vring_free_queue(struct virtio_device *vdev, size_t size,
338 void *queue, dma_addr_t dma_handle,
339 struct device *dma_dev)
341 if (vring_use_dma_api(vdev))
342 dma_free_coherent(dma_dev, size, queue, dma_handle);
344 free_pages_exact(queue, PAGE_ALIGN(size));
348 * The DMA ops on various arches are rather gnarly right now, and
349 * making all of the arch DMA ops work on the vring device itself
352 static struct device *vring_dma_dev(const struct vring_virtqueue *vq)
357 /* Map one sg entry. */
358 static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg,
359 enum dma_data_direction direction, dma_addr_t *addr)
361 if (!vq->use_dma_api) {
363 * If DMA is not used, KMSAN doesn't know that the scatterlist
364 * is initialized by the hardware. Explicitly check/unpoison it
365 * depending on the direction.
367 kmsan_handle_dma(sg_page(sg), sg->offset, sg->length, direction);
368 *addr = (dma_addr_t)sg_phys(sg);
373 * We can't use dma_map_sg, because we don't use scatterlists in
374 * the way it expects (we don't guarantee that the scatterlist
375 * will exist for the lifetime of the mapping).
377 *addr = dma_map_page(vring_dma_dev(vq),
378 sg_page(sg), sg->offset, sg->length,
381 if (dma_mapping_error(vring_dma_dev(vq), *addr))
387 static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
388 void *cpu_addr, size_t size,
389 enum dma_data_direction direction)
391 if (!vq->use_dma_api)
392 return (dma_addr_t)virt_to_phys(cpu_addr);
394 return dma_map_single(vring_dma_dev(vq),
395 cpu_addr, size, direction);
398 static int vring_mapping_error(const struct vring_virtqueue *vq,
401 if (!vq->use_dma_api)
404 return dma_mapping_error(vring_dma_dev(vq), addr);
407 static void virtqueue_init(struct vring_virtqueue *vq, u32 num)
409 vq->vq.num_free = num;
412 vq->last_used_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR);
414 vq->last_used_idx = 0;
416 vq->event_triggered = false;
421 vq->last_add_time_valid = false;
427 * Split ring specific functions - *_split().
430 static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
431 const struct vring_desc *desc)
435 if (!vq->use_dma_api)
438 flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
440 dma_unmap_page(vring_dma_dev(vq),
441 virtio64_to_cpu(vq->vq.vdev, desc->addr),
442 virtio32_to_cpu(vq->vq.vdev, desc->len),
443 (flags & VRING_DESC_F_WRITE) ?
444 DMA_FROM_DEVICE : DMA_TO_DEVICE);
447 static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
450 struct vring_desc_extra *extra = vq->split.desc_extra;
453 if (!vq->use_dma_api)
456 flags = extra[i].flags;
458 if (flags & VRING_DESC_F_INDIRECT) {
459 dma_unmap_single(vring_dma_dev(vq),
462 (flags & VRING_DESC_F_WRITE) ?
463 DMA_FROM_DEVICE : DMA_TO_DEVICE);
465 dma_unmap_page(vring_dma_dev(vq),
468 (flags & VRING_DESC_F_WRITE) ?
469 DMA_FROM_DEVICE : DMA_TO_DEVICE);
473 return extra[i].next;
476 static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
477 unsigned int total_sg,
480 struct vring_desc *desc;
484 * We require lowmem mappings for the descriptors because
485 * otherwise virt_to_phys will give us bogus addresses in the
488 gfp &= ~__GFP_HIGHMEM;
490 desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
494 for (i = 0; i < total_sg; i++)
495 desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
499 static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
500 struct vring_desc *desc,
507 struct vring_virtqueue *vring = to_vvq(vq);
508 struct vring_desc_extra *extra = vring->split.desc_extra;
511 desc[i].flags = cpu_to_virtio16(vq->vdev, flags);
512 desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
513 desc[i].len = cpu_to_virtio32(vq->vdev, len);
516 next = extra[i].next;
517 desc[i].next = cpu_to_virtio16(vq->vdev, next);
519 extra[i].addr = addr;
521 extra[i].flags = flags;
523 next = virtio16_to_cpu(vq->vdev, desc[i].next);
528 static inline int virtqueue_add_split(struct virtqueue *_vq,
529 struct scatterlist *sgs[],
530 unsigned int total_sg,
531 unsigned int out_sgs,
537 struct vring_virtqueue *vq = to_vvq(_vq);
538 struct scatterlist *sg;
539 struct vring_desc *desc;
540 unsigned int i, n, avail, descs_used, prev, err_idx;
546 BUG_ON(data == NULL);
547 BUG_ON(ctx && vq->indirect);
549 if (unlikely(vq->broken)) {
554 LAST_ADD_TIME_UPDATE(vq);
556 BUG_ON(total_sg == 0);
558 head = vq->free_head;
560 if (virtqueue_use_indirect(vq, total_sg))
561 desc = alloc_indirect_split(_vq, total_sg, gfp);
564 WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
568 /* Use a single buffer which doesn't continue */
570 /* Set up rest to use this indirect table. */
575 desc = vq->split.vring.desc;
577 descs_used = total_sg;
580 if (unlikely(vq->vq.num_free < descs_used)) {
581 pr_debug("Can't add buf len %i - avail = %i\n",
582 descs_used, vq->vq.num_free);
583 /* FIXME: for historical reasons, we force a notify here if
584 * there are outgoing parts to the buffer. Presumably the
585 * host should service the ring ASAP. */
594 for (n = 0; n < out_sgs; n++) {
595 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
598 if (vring_map_one_sg(vq, sg, DMA_TO_DEVICE, &addr))
602 /* Note that we trust indirect descriptor
603 * table since it use stream DMA mapping.
605 i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length,
610 for (; n < (out_sgs + in_sgs); n++) {
611 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
614 if (vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr))
618 /* Note that we trust indirect descriptor
619 * table since it use stream DMA mapping.
621 i = virtqueue_add_desc_split(_vq, desc, i, addr,
628 /* Last one doesn't continue. */
629 desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
630 if (!indirect && vq->use_dma_api)
631 vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
635 /* Now that the indirect table is filled in, map it. */
636 dma_addr_t addr = vring_map_single(
637 vq, desc, total_sg * sizeof(struct vring_desc),
639 if (vring_mapping_error(vq, addr))
642 virtqueue_add_desc_split(_vq, vq->split.vring.desc,
644 total_sg * sizeof(struct vring_desc),
645 VRING_DESC_F_INDIRECT,
649 /* We're using some buffers from the free list. */
650 vq->vq.num_free -= descs_used;
652 /* Update free pointer */
654 vq->free_head = vq->split.desc_extra[head].next;
658 /* Store token and indirect buffer state. */
659 vq->split.desc_state[head].data = data;
661 vq->split.desc_state[head].indir_desc = desc;
663 vq->split.desc_state[head].indir_desc = ctx;
665 /* Put entry in available array (but don't update avail->idx until they
667 avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
668 vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
670 /* Descriptors and available array need to be set before we expose the
671 * new available array entries. */
672 virtio_wmb(vq->weak_barriers);
673 vq->split.avail_idx_shadow++;
674 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
675 vq->split.avail_idx_shadow);
678 pr_debug("Added buffer head %i to %p\n", head, vq);
681 /* This is very unlikely, but theoretically possible. Kick
683 if (unlikely(vq->num_added == (1 << 16) - 1))
696 for (n = 0; n < total_sg; n++) {
700 vring_unmap_one_split_indirect(vq, &desc[i]);
701 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
703 i = vring_unmap_one_split(vq, i);
713 static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
715 struct vring_virtqueue *vq = to_vvq(_vq);
720 /* We need to expose available array entries before checking avail
722 virtio_mb(vq->weak_barriers);
724 old = vq->split.avail_idx_shadow - vq->num_added;
725 new = vq->split.avail_idx_shadow;
728 LAST_ADD_TIME_CHECK(vq);
729 LAST_ADD_TIME_INVALID(vq);
732 needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
733 vring_avail_event(&vq->split.vring)),
736 needs_kick = !(vq->split.vring.used->flags &
737 cpu_to_virtio16(_vq->vdev,
738 VRING_USED_F_NO_NOTIFY));
744 static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
748 __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
750 /* Clear data ptr. */
751 vq->split.desc_state[head].data = NULL;
753 /* Put back on free list: unmap first-level descriptors and find end */
756 while (vq->split.vring.desc[i].flags & nextflag) {
757 vring_unmap_one_split(vq, i);
758 i = vq->split.desc_extra[i].next;
762 vring_unmap_one_split(vq, i);
763 vq->split.desc_extra[i].next = vq->free_head;
764 vq->free_head = head;
766 /* Plus final descriptor */
770 struct vring_desc *indir_desc =
771 vq->split.desc_state[head].indir_desc;
774 /* Free the indirect table, if any, now that it's unmapped. */
778 len = vq->split.desc_extra[head].len;
780 BUG_ON(!(vq->split.desc_extra[head].flags &
781 VRING_DESC_F_INDIRECT));
782 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
784 if (vq->use_dma_api) {
785 for (j = 0; j < len / sizeof(struct vring_desc); j++)
786 vring_unmap_one_split_indirect(vq, &indir_desc[j]);
790 vq->split.desc_state[head].indir_desc = NULL;
792 *ctx = vq->split.desc_state[head].indir_desc;
796 static bool more_used_split(const struct vring_virtqueue *vq)
798 return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
799 vq->split.vring.used->idx);
802 static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
806 struct vring_virtqueue *vq = to_vvq(_vq);
813 if (unlikely(vq->broken)) {
818 if (!more_used_split(vq)) {
819 pr_debug("No more buffers in queue\n");
824 /* Only get used array entries after they have been exposed by host. */
825 virtio_rmb(vq->weak_barriers);
827 last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
828 i = virtio32_to_cpu(_vq->vdev,
829 vq->split.vring.used->ring[last_used].id);
830 *len = virtio32_to_cpu(_vq->vdev,
831 vq->split.vring.used->ring[last_used].len);
833 if (unlikely(i >= vq->split.vring.num)) {
834 BAD_RING(vq, "id %u out of range\n", i);
837 if (unlikely(!vq->split.desc_state[i].data)) {
838 BAD_RING(vq, "id %u is not a head!\n", i);
842 /* detach_buf_split clears data, so grab it now. */
843 ret = vq->split.desc_state[i].data;
844 detach_buf_split(vq, i, ctx);
846 /* If we expect an interrupt for the next entry, tell host
847 * by writing event index and flush out the write before
848 * the read in the next get_buf call. */
849 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
850 virtio_store_mb(vq->weak_barriers,
851 &vring_used_event(&vq->split.vring),
852 cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
854 LAST_ADD_TIME_INVALID(vq);
860 static void virtqueue_disable_cb_split(struct virtqueue *_vq)
862 struct vring_virtqueue *vq = to_vvq(_vq);
864 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
865 vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
868 * If device triggered an event already it won't trigger one again:
869 * no need to disable.
871 if (vq->event_triggered)
875 /* TODO: this is a hack. Figure out a cleaner value to write. */
876 vring_used_event(&vq->split.vring) = 0x0;
878 vq->split.vring.avail->flags =
879 cpu_to_virtio16(_vq->vdev,
880 vq->split.avail_flags_shadow);
884 static unsigned int virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
886 struct vring_virtqueue *vq = to_vvq(_vq);
891 /* We optimistically turn back on interrupts, then check if there was
893 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
894 * either clear the flags bit or point the event index at the next
895 * entry. Always do both to keep code simple. */
896 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
897 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
899 vq->split.vring.avail->flags =
900 cpu_to_virtio16(_vq->vdev,
901 vq->split.avail_flags_shadow);
903 vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
904 last_used_idx = vq->last_used_idx);
906 return last_used_idx;
909 static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned int last_used_idx)
911 struct vring_virtqueue *vq = to_vvq(_vq);
913 return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
914 vq->split.vring.used->idx);
917 static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
919 struct vring_virtqueue *vq = to_vvq(_vq);
924 /* We optimistically turn back on interrupts, then check if there was
926 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
927 * either clear the flags bit or point the event index at the next
928 * entry. Always update the event index to keep code simple. */
929 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
930 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
932 vq->split.vring.avail->flags =
933 cpu_to_virtio16(_vq->vdev,
934 vq->split.avail_flags_shadow);
936 /* TODO: tune this threshold */
937 bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
939 virtio_store_mb(vq->weak_barriers,
940 &vring_used_event(&vq->split.vring),
941 cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
943 if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
944 - vq->last_used_idx) > bufs)) {
953 static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
955 struct vring_virtqueue *vq = to_vvq(_vq);
961 for (i = 0; i < vq->split.vring.num; i++) {
962 if (!vq->split.desc_state[i].data)
964 /* detach_buf_split clears data, so grab it now. */
965 buf = vq->split.desc_state[i].data;
966 detach_buf_split(vq, i, NULL);
967 vq->split.avail_idx_shadow--;
968 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
969 vq->split.avail_idx_shadow);
973 /* That should have freed everything. */
974 BUG_ON(vq->vq.num_free != vq->split.vring.num);
980 static void virtqueue_vring_init_split(struct vring_virtqueue_split *vring_split,
981 struct vring_virtqueue *vq)
983 struct virtio_device *vdev;
987 vring_split->avail_flags_shadow = 0;
988 vring_split->avail_idx_shadow = 0;
990 /* No callback? Tell other side not to bother us. */
991 if (!vq->vq.callback) {
992 vring_split->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
994 vring_split->vring.avail->flags = cpu_to_virtio16(vdev,
995 vring_split->avail_flags_shadow);
999 static void virtqueue_reinit_split(struct vring_virtqueue *vq)
1003 num = vq->split.vring.num;
1005 vq->split.vring.avail->flags = 0;
1006 vq->split.vring.avail->idx = 0;
1008 /* reset avail event */
1009 vq->split.vring.avail->ring[num] = 0;
1011 vq->split.vring.used->flags = 0;
1012 vq->split.vring.used->idx = 0;
1014 /* reset used event */
1015 *(__virtio16 *)&(vq->split.vring.used->ring[num]) = 0;
1017 virtqueue_init(vq, num);
1019 virtqueue_vring_init_split(&vq->split, vq);
1022 static void virtqueue_vring_attach_split(struct vring_virtqueue *vq,
1023 struct vring_virtqueue_split *vring_split)
1025 vq->split = *vring_split;
1027 /* Put everything in free lists. */
1031 static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vring_split)
1033 struct vring_desc_state_split *state;
1034 struct vring_desc_extra *extra;
1035 u32 num = vring_split->vring.num;
1037 state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL);
1041 extra = vring_alloc_desc_extra(num);
1045 memset(state, 0, num * sizeof(struct vring_desc_state_split));
1047 vring_split->desc_state = state;
1048 vring_split->desc_extra = extra;
1057 static void vring_free_split(struct vring_virtqueue_split *vring_split,
1058 struct virtio_device *vdev, struct device *dma_dev)
1060 vring_free_queue(vdev, vring_split->queue_size_in_bytes,
1061 vring_split->vring.desc,
1062 vring_split->queue_dma_addr,
1065 kfree(vring_split->desc_state);
1066 kfree(vring_split->desc_extra);
1069 static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
1070 struct virtio_device *vdev,
1072 unsigned int vring_align,
1073 bool may_reduce_num,
1074 struct device *dma_dev)
1077 dma_addr_t dma_addr;
1079 /* We assume num is a power of 2. */
1080 if (!is_power_of_2(num)) {
1081 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
1085 /* TODO: allocate each queue chunk individually */
1086 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
1087 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1089 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1093 if (!may_reduce_num)
1101 /* Try to get a single page. You are my only hope! */
1102 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1103 &dma_addr, GFP_KERNEL | __GFP_ZERO,
1109 vring_init(&vring_split->vring, num, queue, vring_align);
1111 vring_split->queue_dma_addr = dma_addr;
1112 vring_split->queue_size_in_bytes = vring_size(num, vring_align);
1114 vring_split->vring_align = vring_align;
1115 vring_split->may_reduce_num = may_reduce_num;
1120 static struct virtqueue *vring_create_virtqueue_split(
1123 unsigned int vring_align,
1124 struct virtio_device *vdev,
1126 bool may_reduce_num,
1128 bool (*notify)(struct virtqueue *),
1129 void (*callback)(struct virtqueue *),
1131 struct device *dma_dev)
1133 struct vring_virtqueue_split vring_split = {};
1134 struct virtqueue *vq;
1137 err = vring_alloc_queue_split(&vring_split, vdev, num, vring_align,
1138 may_reduce_num, dma_dev);
1142 vq = __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
1143 context, notify, callback, name, dma_dev);
1145 vring_free_split(&vring_split, vdev, dma_dev);
1149 to_vvq(vq)->we_own_ring = true;
1154 static int virtqueue_resize_split(struct virtqueue *_vq, u32 num)
1156 struct vring_virtqueue_split vring_split = {};
1157 struct vring_virtqueue *vq = to_vvq(_vq);
1158 struct virtio_device *vdev = _vq->vdev;
1161 err = vring_alloc_queue_split(&vring_split, vdev, num,
1162 vq->split.vring_align,
1163 vq->split.may_reduce_num,
1168 err = vring_alloc_state_extra_split(&vring_split);
1170 goto err_state_extra;
1172 vring_free(&vq->vq);
1174 virtqueue_vring_init_split(&vring_split, vq);
1176 virtqueue_init(vq, vring_split.vring.num);
1177 virtqueue_vring_attach_split(vq, &vring_split);
1182 vring_free_split(&vring_split, vdev, vring_dma_dev(vq));
1184 virtqueue_reinit_split(vq);
1190 * Packed ring specific functions - *_packed().
1192 static bool packed_used_wrap_counter(u16 last_used_idx)
1194 return !!(last_used_idx & (1 << VRING_PACKED_EVENT_F_WRAP_CTR));
1197 static u16 packed_last_used(u16 last_used_idx)
1199 return last_used_idx & ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR));
1202 static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
1203 const struct vring_desc_extra *extra)
1207 if (!vq->use_dma_api)
1210 flags = extra->flags;
1212 if (flags & VRING_DESC_F_INDIRECT) {
1213 dma_unmap_single(vring_dma_dev(vq),
1214 extra->addr, extra->len,
1215 (flags & VRING_DESC_F_WRITE) ?
1216 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1218 dma_unmap_page(vring_dma_dev(vq),
1219 extra->addr, extra->len,
1220 (flags & VRING_DESC_F_WRITE) ?
1221 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1225 static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
1226 const struct vring_packed_desc *desc)
1230 if (!vq->use_dma_api)
1233 flags = le16_to_cpu(desc->flags);
1235 dma_unmap_page(vring_dma_dev(vq),
1236 le64_to_cpu(desc->addr),
1237 le32_to_cpu(desc->len),
1238 (flags & VRING_DESC_F_WRITE) ?
1239 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1242 static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
1245 struct vring_packed_desc *desc;
1248 * We require lowmem mappings for the descriptors because
1249 * otherwise virt_to_phys will give us bogus addresses in the
1252 gfp &= ~__GFP_HIGHMEM;
1254 desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
1259 static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
1260 struct scatterlist *sgs[],
1261 unsigned int total_sg,
1262 unsigned int out_sgs,
1263 unsigned int in_sgs,
1267 struct vring_packed_desc *desc;
1268 struct scatterlist *sg;
1269 unsigned int i, n, err_idx;
1273 head = vq->packed.next_avail_idx;
1274 desc = alloc_indirect_packed(total_sg, gfp);
1278 if (unlikely(vq->vq.num_free < 1)) {
1279 pr_debug("Can't add buf len 1 - avail = 0\n");
1287 BUG_ON(id == vq->packed.vring.num);
1289 for (n = 0; n < out_sgs + in_sgs; n++) {
1290 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1291 if (vring_map_one_sg(vq, sg, n < out_sgs ?
1292 DMA_TO_DEVICE : DMA_FROM_DEVICE, &addr))
1295 desc[i].flags = cpu_to_le16(n < out_sgs ?
1296 0 : VRING_DESC_F_WRITE);
1297 desc[i].addr = cpu_to_le64(addr);
1298 desc[i].len = cpu_to_le32(sg->length);
1303 /* Now that the indirect table is filled in, map it. */
1304 addr = vring_map_single(vq, desc,
1305 total_sg * sizeof(struct vring_packed_desc),
1307 if (vring_mapping_error(vq, addr))
1310 vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
1311 vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
1312 sizeof(struct vring_packed_desc));
1313 vq->packed.vring.desc[head].id = cpu_to_le16(id);
1315 if (vq->use_dma_api) {
1316 vq->packed.desc_extra[id].addr = addr;
1317 vq->packed.desc_extra[id].len = total_sg *
1318 sizeof(struct vring_packed_desc);
1319 vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
1320 vq->packed.avail_used_flags;
1324 * A driver MUST NOT make the first descriptor in the list
1325 * available before all subsequent descriptors comprising
1326 * the list are made available.
1328 virtio_wmb(vq->weak_barriers);
1329 vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
1330 vq->packed.avail_used_flags);
1332 /* We're using some buffers from the free list. */
1333 vq->vq.num_free -= 1;
1335 /* Update free pointer */
1337 if (n >= vq->packed.vring.num) {
1339 vq->packed.avail_wrap_counter ^= 1;
1340 vq->packed.avail_used_flags ^=
1341 1 << VRING_PACKED_DESC_F_AVAIL |
1342 1 << VRING_PACKED_DESC_F_USED;
1344 vq->packed.next_avail_idx = n;
1345 vq->free_head = vq->packed.desc_extra[id].next;
1347 /* Store token and indirect buffer state. */
1348 vq->packed.desc_state[id].num = 1;
1349 vq->packed.desc_state[id].data = data;
1350 vq->packed.desc_state[id].indir_desc = desc;
1351 vq->packed.desc_state[id].last = id;
1355 pr_debug("Added buffer head %i to %p\n", head, vq);
1363 for (i = 0; i < err_idx; i++)
1364 vring_unmap_desc_packed(vq, &desc[i]);
1372 static inline int virtqueue_add_packed(struct virtqueue *_vq,
1373 struct scatterlist *sgs[],
1374 unsigned int total_sg,
1375 unsigned int out_sgs,
1376 unsigned int in_sgs,
1381 struct vring_virtqueue *vq = to_vvq(_vq);
1382 struct vring_packed_desc *desc;
1383 struct scatterlist *sg;
1384 unsigned int i, n, c, descs_used, err_idx;
1385 __le16 head_flags, flags;
1386 u16 head, id, prev, curr, avail_used_flags;
1391 BUG_ON(data == NULL);
1392 BUG_ON(ctx && vq->indirect);
1394 if (unlikely(vq->broken)) {
1399 LAST_ADD_TIME_UPDATE(vq);
1401 BUG_ON(total_sg == 0);
1403 if (virtqueue_use_indirect(vq, total_sg)) {
1404 err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
1406 if (err != -ENOMEM) {
1411 /* fall back on direct */
1414 head = vq->packed.next_avail_idx;
1415 avail_used_flags = vq->packed.avail_used_flags;
1417 WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
1419 desc = vq->packed.vring.desc;
1421 descs_used = total_sg;
1423 if (unlikely(vq->vq.num_free < descs_used)) {
1424 pr_debug("Can't add buf len %i - avail = %i\n",
1425 descs_used, vq->vq.num_free);
1431 BUG_ON(id == vq->packed.vring.num);
1435 for (n = 0; n < out_sgs + in_sgs; n++) {
1436 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1439 if (vring_map_one_sg(vq, sg, n < out_sgs ?
1440 DMA_TO_DEVICE : DMA_FROM_DEVICE, &addr))
1443 flags = cpu_to_le16(vq->packed.avail_used_flags |
1444 (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
1445 (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
1449 desc[i].flags = flags;
1451 desc[i].addr = cpu_to_le64(addr);
1452 desc[i].len = cpu_to_le32(sg->length);
1453 desc[i].id = cpu_to_le16(id);
1455 if (unlikely(vq->use_dma_api)) {
1456 vq->packed.desc_extra[curr].addr = addr;
1457 vq->packed.desc_extra[curr].len = sg->length;
1458 vq->packed.desc_extra[curr].flags =
1462 curr = vq->packed.desc_extra[curr].next;
1464 if ((unlikely(++i >= vq->packed.vring.num))) {
1466 vq->packed.avail_used_flags ^=
1467 1 << VRING_PACKED_DESC_F_AVAIL |
1468 1 << VRING_PACKED_DESC_F_USED;
1474 vq->packed.avail_wrap_counter ^= 1;
1476 /* We're using some buffers from the free list. */
1477 vq->vq.num_free -= descs_used;
1479 /* Update free pointer */
1480 vq->packed.next_avail_idx = i;
1481 vq->free_head = curr;
1484 vq->packed.desc_state[id].num = descs_used;
1485 vq->packed.desc_state[id].data = data;
1486 vq->packed.desc_state[id].indir_desc = ctx;
1487 vq->packed.desc_state[id].last = prev;
1490 * A driver MUST NOT make the first descriptor in the list
1491 * available before all subsequent descriptors comprising
1492 * the list are made available.
1494 virtio_wmb(vq->weak_barriers);
1495 vq->packed.vring.desc[head].flags = head_flags;
1496 vq->num_added += descs_used;
1498 pr_debug("Added buffer head %i to %p\n", head, vq);
1506 curr = vq->free_head;
1508 vq->packed.avail_used_flags = avail_used_flags;
1510 for (n = 0; n < total_sg; n++) {
1513 vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]);
1514 curr = vq->packed.desc_extra[curr].next;
1516 if (i >= vq->packed.vring.num)
1524 static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
1526 struct vring_virtqueue *vq = to_vvq(_vq);
1527 u16 new, old, off_wrap, flags, wrap_counter, event_idx;
1540 * We need to expose the new flags value before checking notification
1543 virtio_mb(vq->weak_barriers);
1545 old = vq->packed.next_avail_idx - vq->num_added;
1546 new = vq->packed.next_avail_idx;
1549 snapshot.u32 = *(u32 *)vq->packed.vring.device;
1550 flags = le16_to_cpu(snapshot.flags);
1552 LAST_ADD_TIME_CHECK(vq);
1553 LAST_ADD_TIME_INVALID(vq);
1555 if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
1556 needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1560 off_wrap = le16_to_cpu(snapshot.off_wrap);
1562 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1563 event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1564 if (wrap_counter != vq->packed.avail_wrap_counter)
1565 event_idx -= vq->packed.vring.num;
1567 needs_kick = vring_need_event(event_idx, new, old);
1573 static void detach_buf_packed(struct vring_virtqueue *vq,
1574 unsigned int id, void **ctx)
1576 struct vring_desc_state_packed *state = NULL;
1577 struct vring_packed_desc *desc;
1578 unsigned int i, curr;
1580 state = &vq->packed.desc_state[id];
1582 /* Clear data ptr. */
1585 vq->packed.desc_extra[state->last].next = vq->free_head;
1587 vq->vq.num_free += state->num;
1589 if (unlikely(vq->use_dma_api)) {
1591 for (i = 0; i < state->num; i++) {
1592 vring_unmap_extra_packed(vq,
1593 &vq->packed.desc_extra[curr]);
1594 curr = vq->packed.desc_extra[curr].next;
1601 /* Free the indirect table, if any, now that it's unmapped. */
1602 desc = state->indir_desc;
1606 if (vq->use_dma_api) {
1607 len = vq->packed.desc_extra[id].len;
1608 for (i = 0; i < len / sizeof(struct vring_packed_desc);
1610 vring_unmap_desc_packed(vq, &desc[i]);
1613 state->indir_desc = NULL;
1615 *ctx = state->indir_desc;
1619 static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
1620 u16 idx, bool used_wrap_counter)
1625 flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
1626 avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
1627 used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
1629 return avail == used && used == used_wrap_counter;
1632 static bool more_used_packed(const struct vring_virtqueue *vq)
1636 bool used_wrap_counter;
1638 last_used_idx = READ_ONCE(vq->last_used_idx);
1639 last_used = packed_last_used(last_used_idx);
1640 used_wrap_counter = packed_used_wrap_counter(last_used_idx);
1641 return is_used_desc_packed(vq, last_used, used_wrap_counter);
1644 static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
1648 struct vring_virtqueue *vq = to_vvq(_vq);
1649 u16 last_used, id, last_used_idx;
1650 bool used_wrap_counter;
1655 if (unlikely(vq->broken)) {
1660 if (!more_used_packed(vq)) {
1661 pr_debug("No more buffers in queue\n");
1666 /* Only get used elements after they have been exposed by host. */
1667 virtio_rmb(vq->weak_barriers);
1669 last_used_idx = READ_ONCE(vq->last_used_idx);
1670 used_wrap_counter = packed_used_wrap_counter(last_used_idx);
1671 last_used = packed_last_used(last_used_idx);
1672 id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
1673 *len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
1675 if (unlikely(id >= vq->packed.vring.num)) {
1676 BAD_RING(vq, "id %u out of range\n", id);
1679 if (unlikely(!vq->packed.desc_state[id].data)) {
1680 BAD_RING(vq, "id %u is not a head!\n", id);
1684 /* detach_buf_packed clears data, so grab it now. */
1685 ret = vq->packed.desc_state[id].data;
1686 detach_buf_packed(vq, id, ctx);
1688 last_used += vq->packed.desc_state[id].num;
1689 if (unlikely(last_used >= vq->packed.vring.num)) {
1690 last_used -= vq->packed.vring.num;
1691 used_wrap_counter ^= 1;
1694 last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1695 WRITE_ONCE(vq->last_used_idx, last_used);
1698 * If we expect an interrupt for the next entry, tell host
1699 * by writing event index and flush out the write before
1700 * the read in the next get_buf call.
1702 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1703 virtio_store_mb(vq->weak_barriers,
1704 &vq->packed.vring.driver->off_wrap,
1705 cpu_to_le16(vq->last_used_idx));
1707 LAST_ADD_TIME_INVALID(vq);
1713 static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
1715 struct vring_virtqueue *vq = to_vvq(_vq);
1717 if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
1718 vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1721 * If device triggered an event already it won't trigger one again:
1722 * no need to disable.
1724 if (vq->event_triggered)
1727 vq->packed.vring.driver->flags =
1728 cpu_to_le16(vq->packed.event_flags_shadow);
1732 static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
1734 struct vring_virtqueue *vq = to_vvq(_vq);
1739 * We optimistically turn back on interrupts, then check if there was
1744 vq->packed.vring.driver->off_wrap =
1745 cpu_to_le16(vq->last_used_idx);
1747 * We need to update event offset and event wrap
1748 * counter first before updating event flags.
1750 virtio_wmb(vq->weak_barriers);
1753 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1754 vq->packed.event_flags_shadow = vq->event ?
1755 VRING_PACKED_EVENT_FLAG_DESC :
1756 VRING_PACKED_EVENT_FLAG_ENABLE;
1757 vq->packed.vring.driver->flags =
1758 cpu_to_le16(vq->packed.event_flags_shadow);
1762 return vq->last_used_idx;
1765 static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
1767 struct vring_virtqueue *vq = to_vvq(_vq);
1771 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1772 used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1774 return is_used_desc_packed(vq, used_idx, wrap_counter);
1777 static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
1779 struct vring_virtqueue *vq = to_vvq(_vq);
1780 u16 used_idx, wrap_counter, last_used_idx;
1786 * We optimistically turn back on interrupts, then check if there was
1791 /* TODO: tune this threshold */
1792 bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
1793 last_used_idx = READ_ONCE(vq->last_used_idx);
1794 wrap_counter = packed_used_wrap_counter(last_used_idx);
1796 used_idx = packed_last_used(last_used_idx) + bufs;
1797 if (used_idx >= vq->packed.vring.num) {
1798 used_idx -= vq->packed.vring.num;
1802 vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1803 (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1806 * We need to update event offset and event wrap
1807 * counter first before updating event flags.
1809 virtio_wmb(vq->weak_barriers);
1812 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1813 vq->packed.event_flags_shadow = vq->event ?
1814 VRING_PACKED_EVENT_FLAG_DESC :
1815 VRING_PACKED_EVENT_FLAG_ENABLE;
1816 vq->packed.vring.driver->flags =
1817 cpu_to_le16(vq->packed.event_flags_shadow);
1821 * We need to update event suppression structure first
1822 * before re-checking for more used buffers.
1824 virtio_mb(vq->weak_barriers);
1826 last_used_idx = READ_ONCE(vq->last_used_idx);
1827 wrap_counter = packed_used_wrap_counter(last_used_idx);
1828 used_idx = packed_last_used(last_used_idx);
1829 if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
1838 static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
1840 struct vring_virtqueue *vq = to_vvq(_vq);
1846 for (i = 0; i < vq->packed.vring.num; i++) {
1847 if (!vq->packed.desc_state[i].data)
1849 /* detach_buf clears data, so grab it now. */
1850 buf = vq->packed.desc_state[i].data;
1851 detach_buf_packed(vq, i, NULL);
1855 /* That should have freed everything. */
1856 BUG_ON(vq->vq.num_free != vq->packed.vring.num);
1862 static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num)
1864 struct vring_desc_extra *desc_extra;
1867 desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra),
1872 memset(desc_extra, 0, num * sizeof(struct vring_desc_extra));
1874 for (i = 0; i < num - 1; i++)
1875 desc_extra[i].next = i + 1;
1880 static void vring_free_packed(struct vring_virtqueue_packed *vring_packed,
1881 struct virtio_device *vdev,
1882 struct device *dma_dev)
1884 if (vring_packed->vring.desc)
1885 vring_free_queue(vdev, vring_packed->ring_size_in_bytes,
1886 vring_packed->vring.desc,
1887 vring_packed->ring_dma_addr,
1890 if (vring_packed->vring.driver)
1891 vring_free_queue(vdev, vring_packed->event_size_in_bytes,
1892 vring_packed->vring.driver,
1893 vring_packed->driver_event_dma_addr,
1896 if (vring_packed->vring.device)
1897 vring_free_queue(vdev, vring_packed->event_size_in_bytes,
1898 vring_packed->vring.device,
1899 vring_packed->device_event_dma_addr,
1902 kfree(vring_packed->desc_state);
1903 kfree(vring_packed->desc_extra);
1906 static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed,
1907 struct virtio_device *vdev,
1908 u32 num, struct device *dma_dev)
1910 struct vring_packed_desc *ring;
1911 struct vring_packed_desc_event *driver, *device;
1912 dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
1913 size_t ring_size_in_bytes, event_size_in_bytes;
1915 ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
1917 ring = vring_alloc_queue(vdev, ring_size_in_bytes,
1919 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1924 vring_packed->vring.desc = ring;
1925 vring_packed->ring_dma_addr = ring_dma_addr;
1926 vring_packed->ring_size_in_bytes = ring_size_in_bytes;
1928 event_size_in_bytes = sizeof(struct vring_packed_desc_event);
1930 driver = vring_alloc_queue(vdev, event_size_in_bytes,
1931 &driver_event_dma_addr,
1932 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1937 vring_packed->vring.driver = driver;
1938 vring_packed->event_size_in_bytes = event_size_in_bytes;
1939 vring_packed->driver_event_dma_addr = driver_event_dma_addr;
1941 device = vring_alloc_queue(vdev, event_size_in_bytes,
1942 &device_event_dma_addr,
1943 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1948 vring_packed->vring.device = device;
1949 vring_packed->device_event_dma_addr = device_event_dma_addr;
1951 vring_packed->vring.num = num;
1956 vring_free_packed(vring_packed, vdev, dma_dev);
1960 static int vring_alloc_state_extra_packed(struct vring_virtqueue_packed *vring_packed)
1962 struct vring_desc_state_packed *state;
1963 struct vring_desc_extra *extra;
1964 u32 num = vring_packed->vring.num;
1966 state = kmalloc_array(num, sizeof(struct vring_desc_state_packed), GFP_KERNEL);
1968 goto err_desc_state;
1970 memset(state, 0, num * sizeof(struct vring_desc_state_packed));
1972 extra = vring_alloc_desc_extra(num);
1974 goto err_desc_extra;
1976 vring_packed->desc_state = state;
1977 vring_packed->desc_extra = extra;
1987 static void virtqueue_vring_init_packed(struct vring_virtqueue_packed *vring_packed,
1990 vring_packed->next_avail_idx = 0;
1991 vring_packed->avail_wrap_counter = 1;
1992 vring_packed->event_flags_shadow = 0;
1993 vring_packed->avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
1995 /* No callback? Tell other side not to bother us. */
1997 vring_packed->event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1998 vring_packed->vring.driver->flags =
1999 cpu_to_le16(vring_packed->event_flags_shadow);
2003 static void virtqueue_vring_attach_packed(struct vring_virtqueue *vq,
2004 struct vring_virtqueue_packed *vring_packed)
2006 vq->packed = *vring_packed;
2008 /* Put everything in free lists. */
2012 static void virtqueue_reinit_packed(struct vring_virtqueue *vq)
2014 memset(vq->packed.vring.device, 0, vq->packed.event_size_in_bytes);
2015 memset(vq->packed.vring.driver, 0, vq->packed.event_size_in_bytes);
2017 /* we need to reset the desc.flags. For more, see is_used_desc_packed() */
2018 memset(vq->packed.vring.desc, 0, vq->packed.ring_size_in_bytes);
2020 virtqueue_init(vq, vq->packed.vring.num);
2021 virtqueue_vring_init_packed(&vq->packed, !!vq->vq.callback);
2024 static struct virtqueue *vring_create_virtqueue_packed(
2027 unsigned int vring_align,
2028 struct virtio_device *vdev,
2030 bool may_reduce_num,
2032 bool (*notify)(struct virtqueue *),
2033 void (*callback)(struct virtqueue *),
2035 struct device *dma_dev)
2037 struct vring_virtqueue_packed vring_packed = {};
2038 struct vring_virtqueue *vq;
2041 if (vring_alloc_queue_packed(&vring_packed, vdev, num, dma_dev))
2044 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
2048 vq->vq.callback = callback;
2051 vq->vq.index = index;
2052 vq->vq.reset = false;
2053 vq->we_own_ring = true;
2054 vq->notify = notify;
2055 vq->weak_barriers = weak_barriers;
2056 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
2061 vq->packed_ring = true;
2062 vq->dma_dev = dma_dev;
2063 vq->use_dma_api = vring_use_dma_api(vdev);
2065 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
2067 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
2069 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
2070 vq->weak_barriers = false;
2072 err = vring_alloc_state_extra_packed(&vring_packed);
2074 goto err_state_extra;
2076 virtqueue_vring_init_packed(&vring_packed, !!callback);
2078 virtqueue_init(vq, num);
2079 virtqueue_vring_attach_packed(vq, &vring_packed);
2081 spin_lock(&vdev->vqs_list_lock);
2082 list_add_tail(&vq->vq.list, &vdev->vqs);
2083 spin_unlock(&vdev->vqs_list_lock);
2089 vring_free_packed(&vring_packed, vdev, dma_dev);
2094 static int virtqueue_resize_packed(struct virtqueue *_vq, u32 num)
2096 struct vring_virtqueue_packed vring_packed = {};
2097 struct vring_virtqueue *vq = to_vvq(_vq);
2098 struct virtio_device *vdev = _vq->vdev;
2101 if (vring_alloc_queue_packed(&vring_packed, vdev, num, vring_dma_dev(vq)))
2104 err = vring_alloc_state_extra_packed(&vring_packed);
2106 goto err_state_extra;
2108 vring_free(&vq->vq);
2110 virtqueue_vring_init_packed(&vring_packed, !!vq->vq.callback);
2112 virtqueue_init(vq, vring_packed.vring.num);
2113 virtqueue_vring_attach_packed(vq, &vring_packed);
2118 vring_free_packed(&vring_packed, vdev, vring_dma_dev(vq));
2120 virtqueue_reinit_packed(vq);
2126 * Generic functions and exported symbols.
2129 static inline int virtqueue_add(struct virtqueue *_vq,
2130 struct scatterlist *sgs[],
2131 unsigned int total_sg,
2132 unsigned int out_sgs,
2133 unsigned int in_sgs,
2138 struct vring_virtqueue *vq = to_vvq(_vq);
2140 return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
2141 out_sgs, in_sgs, data, ctx, gfp) :
2142 virtqueue_add_split(_vq, sgs, total_sg,
2143 out_sgs, in_sgs, data, ctx, gfp);
2147 * virtqueue_add_sgs - expose buffers to other end
2148 * @_vq: the struct virtqueue we're talking about.
2149 * @sgs: array of terminated scatterlists.
2150 * @out_sgs: the number of scatterlists readable by other side
2151 * @in_sgs: the number of scatterlists which are writable (after readable ones)
2152 * @data: the token identifying the buffer.
2153 * @gfp: how to do memory allocations (if necessary).
2155 * Caller must ensure we don't call this with other virtqueue operations
2156 * at the same time (except where noted).
2158 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2160 int virtqueue_add_sgs(struct virtqueue *_vq,
2161 struct scatterlist *sgs[],
2162 unsigned int out_sgs,
2163 unsigned int in_sgs,
2167 unsigned int i, total_sg = 0;
2169 /* Count them first. */
2170 for (i = 0; i < out_sgs + in_sgs; i++) {
2171 struct scatterlist *sg;
2173 for (sg = sgs[i]; sg; sg = sg_next(sg))
2176 return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
2179 EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
2182 * virtqueue_add_outbuf - expose output buffers to other end
2183 * @vq: the struct virtqueue we're talking about.
2184 * @sg: scatterlist (must be well-formed and terminated!)
2185 * @num: the number of entries in @sg readable by other side
2186 * @data: the token identifying the buffer.
2187 * @gfp: how to do memory allocations (if necessary).
2189 * Caller must ensure we don't call this with other virtqueue operations
2190 * at the same time (except where noted).
2192 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2194 int virtqueue_add_outbuf(struct virtqueue *vq,
2195 struct scatterlist *sg, unsigned int num,
2199 return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
2201 EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
2204 * virtqueue_add_inbuf - expose input buffers to other end
2205 * @vq: the struct virtqueue we're talking about.
2206 * @sg: scatterlist (must be well-formed and terminated!)
2207 * @num: the number of entries in @sg writable by other side
2208 * @data: the token identifying the buffer.
2209 * @gfp: how to do memory allocations (if necessary).
2211 * Caller must ensure we don't call this with other virtqueue operations
2212 * at the same time (except where noted).
2214 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2216 int virtqueue_add_inbuf(struct virtqueue *vq,
2217 struct scatterlist *sg, unsigned int num,
2221 return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
2223 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
2226 * virtqueue_add_inbuf_ctx - expose input buffers to other end
2227 * @vq: the struct virtqueue we're talking about.
2228 * @sg: scatterlist (must be well-formed and terminated!)
2229 * @num: the number of entries in @sg writable by other side
2230 * @data: the token identifying the buffer.
2231 * @ctx: extra context for the token
2232 * @gfp: how to do memory allocations (if necessary).
2234 * Caller must ensure we don't call this with other virtqueue operations
2235 * at the same time (except where noted).
2237 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2239 int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
2240 struct scatterlist *sg, unsigned int num,
2245 return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
2247 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
2250 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
2251 * @_vq: the struct virtqueue
2253 * Instead of virtqueue_kick(), you can do:
2254 * if (virtqueue_kick_prepare(vq))
2255 * virtqueue_notify(vq);
2257 * This is sometimes useful because the virtqueue_kick_prepare() needs
2258 * to be serialized, but the actual virtqueue_notify() call does not.
2260 bool virtqueue_kick_prepare(struct virtqueue *_vq)
2262 struct vring_virtqueue *vq = to_vvq(_vq);
2264 return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
2265 virtqueue_kick_prepare_split(_vq);
2267 EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
2270 * virtqueue_notify - second half of split virtqueue_kick call.
2271 * @_vq: the struct virtqueue
2273 * This does not need to be serialized.
2275 * Returns false if host notify failed or queue is broken, otherwise true.
2277 bool virtqueue_notify(struct virtqueue *_vq)
2279 struct vring_virtqueue *vq = to_vvq(_vq);
2281 if (unlikely(vq->broken))
2284 /* Prod other side to tell it about changes. */
2285 if (!vq->notify(_vq)) {
2291 EXPORT_SYMBOL_GPL(virtqueue_notify);
2294 * virtqueue_kick - update after add_buf
2295 * @vq: the struct virtqueue
2297 * After one or more virtqueue_add_* calls, invoke this to kick
2300 * Caller must ensure we don't call this with other virtqueue
2301 * operations at the same time (except where noted).
2303 * Returns false if kick failed, otherwise true.
2305 bool virtqueue_kick(struct virtqueue *vq)
2307 if (virtqueue_kick_prepare(vq))
2308 return virtqueue_notify(vq);
2311 EXPORT_SYMBOL_GPL(virtqueue_kick);
2314 * virtqueue_get_buf_ctx - get the next used buffer
2315 * @_vq: the struct virtqueue we're talking about.
2316 * @len: the length written into the buffer
2317 * @ctx: extra context for the token
2319 * If the device wrote data into the buffer, @len will be set to the
2320 * amount written. This means you don't need to clear the buffer
2321 * beforehand to ensure there's no data leakage in the case of short
2324 * Caller must ensure we don't call this with other virtqueue
2325 * operations at the same time (except where noted).
2327 * Returns NULL if there are no used buffers, or the "data" token
2328 * handed to virtqueue_add_*().
2330 void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
2333 struct vring_virtqueue *vq = to_vvq(_vq);
2335 return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
2336 virtqueue_get_buf_ctx_split(_vq, len, ctx);
2338 EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
2340 void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
2342 return virtqueue_get_buf_ctx(_vq, len, NULL);
2344 EXPORT_SYMBOL_GPL(virtqueue_get_buf);
2346 * virtqueue_disable_cb - disable callbacks
2347 * @_vq: the struct virtqueue we're talking about.
2349 * Note that this is not necessarily synchronous, hence unreliable and only
2350 * useful as an optimization.
2352 * Unlike other operations, this need not be serialized.
2354 void virtqueue_disable_cb(struct virtqueue *_vq)
2356 struct vring_virtqueue *vq = to_vvq(_vq);
2358 if (vq->packed_ring)
2359 virtqueue_disable_cb_packed(_vq);
2361 virtqueue_disable_cb_split(_vq);
2363 EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
2366 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
2367 * @_vq: the struct virtqueue we're talking about.
2369 * This re-enables callbacks; it returns current queue state
2370 * in an opaque unsigned value. This value should be later tested by
2371 * virtqueue_poll, to detect a possible race between the driver checking for
2372 * more work, and enabling callbacks.
2374 * Caller must ensure we don't call this with other virtqueue
2375 * operations at the same time (except where noted).
2377 unsigned int virtqueue_enable_cb_prepare(struct virtqueue *_vq)
2379 struct vring_virtqueue *vq = to_vvq(_vq);
2381 if (vq->event_triggered)
2382 vq->event_triggered = false;
2384 return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
2385 virtqueue_enable_cb_prepare_split(_vq);
2387 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
2390 * virtqueue_poll - query pending used buffers
2391 * @_vq: the struct virtqueue we're talking about.
2392 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
2394 * Returns "true" if there are pending used buffers in the queue.
2396 * This does not need to be serialized.
2398 bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx)
2400 struct vring_virtqueue *vq = to_vvq(_vq);
2402 if (unlikely(vq->broken))
2405 virtio_mb(vq->weak_barriers);
2406 return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
2407 virtqueue_poll_split(_vq, last_used_idx);
2409 EXPORT_SYMBOL_GPL(virtqueue_poll);
2412 * virtqueue_enable_cb - restart callbacks after disable_cb.
2413 * @_vq: the struct virtqueue we're talking about.
2415 * This re-enables callbacks; it returns "false" if there are pending
2416 * buffers in the queue, to detect a possible race between the driver
2417 * checking for more work, and enabling callbacks.
2419 * Caller must ensure we don't call this with other virtqueue
2420 * operations at the same time (except where noted).
2422 bool virtqueue_enable_cb(struct virtqueue *_vq)
2424 unsigned int last_used_idx = virtqueue_enable_cb_prepare(_vq);
2426 return !virtqueue_poll(_vq, last_used_idx);
2428 EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
2431 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
2432 * @_vq: the struct virtqueue we're talking about.
2434 * This re-enables callbacks but hints to the other side to delay
2435 * interrupts until most of the available buffers have been processed;
2436 * it returns "false" if there are many pending buffers in the queue,
2437 * to detect a possible race between the driver checking for more work,
2438 * and enabling callbacks.
2440 * Caller must ensure we don't call this with other virtqueue
2441 * operations at the same time (except where noted).
2443 bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
2445 struct vring_virtqueue *vq = to_vvq(_vq);
2447 if (vq->event_triggered)
2448 vq->event_triggered = false;
2450 return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
2451 virtqueue_enable_cb_delayed_split(_vq);
2453 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2456 * virtqueue_detach_unused_buf - detach first unused buffer
2457 * @_vq: the struct virtqueue we're talking about.
2459 * Returns NULL or the "data" token handed to virtqueue_add_*().
2460 * This is not valid on an active queue; it is useful for device
2461 * shutdown or the reset queue.
2463 void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2465 struct vring_virtqueue *vq = to_vvq(_vq);
2467 return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
2468 virtqueue_detach_unused_buf_split(_vq);
2470 EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
2472 static inline bool more_used(const struct vring_virtqueue *vq)
2474 return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
2478 * vring_interrupt - notify a virtqueue on an interrupt
2479 * @irq: the IRQ number (ignored)
2480 * @_vq: the struct virtqueue to notify
2482 * Calls the callback function of @_vq to process the virtqueue
2485 irqreturn_t vring_interrupt(int irq, void *_vq)
2487 struct vring_virtqueue *vq = to_vvq(_vq);
2489 if (!more_used(vq)) {
2490 pr_debug("virtqueue interrupt with no work for %p\n", vq);
2494 if (unlikely(vq->broken)) {
2495 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
2496 dev_warn_once(&vq->vq.vdev->dev,
2497 "virtio vring IRQ raised before DRIVER_OK");
2504 /* Just a hint for performance: so it's ok that this can be racy! */
2506 vq->event_triggered = true;
2508 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
2509 if (vq->vq.callback)
2510 vq->vq.callback(&vq->vq);
2514 EXPORT_SYMBOL_GPL(vring_interrupt);
2516 /* Only available for split ring */
2517 static struct virtqueue *__vring_new_virtqueue(unsigned int index,
2518 struct vring_virtqueue_split *vring_split,
2519 struct virtio_device *vdev,
2522 bool (*notify)(struct virtqueue *),
2523 void (*callback)(struct virtqueue *),
2525 struct device *dma_dev)
2527 struct vring_virtqueue *vq;
2530 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2533 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
2537 vq->packed_ring = false;
2538 vq->vq.callback = callback;
2541 vq->vq.index = index;
2542 vq->vq.reset = false;
2543 vq->we_own_ring = false;
2544 vq->notify = notify;
2545 vq->weak_barriers = weak_barriers;
2546 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
2551 vq->dma_dev = dma_dev;
2552 vq->use_dma_api = vring_use_dma_api(vdev);
2554 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
2556 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
2558 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
2559 vq->weak_barriers = false;
2561 err = vring_alloc_state_extra_split(vring_split);
2567 virtqueue_vring_init_split(vring_split, vq);
2569 virtqueue_init(vq, vring_split->vring.num);
2570 virtqueue_vring_attach_split(vq, vring_split);
2572 spin_lock(&vdev->vqs_list_lock);
2573 list_add_tail(&vq->vq.list, &vdev->vqs);
2574 spin_unlock(&vdev->vqs_list_lock);
2578 struct virtqueue *vring_create_virtqueue(
2581 unsigned int vring_align,
2582 struct virtio_device *vdev,
2584 bool may_reduce_num,
2586 bool (*notify)(struct virtqueue *),
2587 void (*callback)(struct virtqueue *),
2591 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2592 return vring_create_virtqueue_packed(index, num, vring_align,
2593 vdev, weak_barriers, may_reduce_num,
2594 context, notify, callback, name, vdev->dev.parent);
2596 return vring_create_virtqueue_split(index, num, vring_align,
2597 vdev, weak_barriers, may_reduce_num,
2598 context, notify, callback, name, vdev->dev.parent);
2600 EXPORT_SYMBOL_GPL(vring_create_virtqueue);
2602 struct virtqueue *vring_create_virtqueue_dma(
2605 unsigned int vring_align,
2606 struct virtio_device *vdev,
2608 bool may_reduce_num,
2610 bool (*notify)(struct virtqueue *),
2611 void (*callback)(struct virtqueue *),
2613 struct device *dma_dev)
2616 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2617 return vring_create_virtqueue_packed(index, num, vring_align,
2618 vdev, weak_barriers, may_reduce_num,
2619 context, notify, callback, name, dma_dev);
2621 return vring_create_virtqueue_split(index, num, vring_align,
2622 vdev, weak_barriers, may_reduce_num,
2623 context, notify, callback, name, dma_dev);
2625 EXPORT_SYMBOL_GPL(vring_create_virtqueue_dma);
2628 * virtqueue_resize - resize the vring of vq
2629 * @_vq: the struct virtqueue we're talking about.
2630 * @num: new ring num
2631 * @recycle: callback for recycle the useless buffer
2633 * When it is really necessary to create a new vring, it will set the current vq
2634 * into the reset state. Then call the passed callback to recycle the buffer
2635 * that is no longer used. Only after the new vring is successfully created, the
2636 * old vring will be released.
2638 * Caller must ensure we don't call this with other virtqueue operations
2639 * at the same time (except where noted).
2641 * Returns zero or a negative error.
2643 * -ENOMEM: Failed to allocate a new ring, fall back to the original ring size.
2644 * vq can still work normally
2645 * -EBUSY: Failed to sync with device, vq may not work properly
2646 * -ENOENT: Transport or device not supported
2647 * -E2BIG/-EINVAL: num error
2648 * -EPERM: Operation not permitted
2651 int virtqueue_resize(struct virtqueue *_vq, u32 num,
2652 void (*recycle)(struct virtqueue *vq, void *buf))
2654 struct vring_virtqueue *vq = to_vvq(_vq);
2655 struct virtio_device *vdev = vq->vq.vdev;
2659 if (!vq->we_own_ring)
2662 if (num > vq->vq.num_max)
2668 if ((vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num) == num)
2671 if (!vdev->config->disable_vq_and_reset)
2674 if (!vdev->config->enable_vq_after_reset)
2677 err = vdev->config->disable_vq_and_reset(_vq);
2681 while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL)
2684 if (vq->packed_ring)
2685 err = virtqueue_resize_packed(_vq, num);
2687 err = virtqueue_resize_split(_vq, num);
2689 if (vdev->config->enable_vq_after_reset(_vq))
2694 EXPORT_SYMBOL_GPL(virtqueue_resize);
2696 /* Only available for split ring */
2697 struct virtqueue *vring_new_virtqueue(unsigned int index,
2699 unsigned int vring_align,
2700 struct virtio_device *vdev,
2704 bool (*notify)(struct virtqueue *vq),
2705 void (*callback)(struct virtqueue *vq),
2708 struct vring_virtqueue_split vring_split = {};
2710 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2713 vring_init(&vring_split.vring, num, pages, vring_align);
2714 return __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
2715 context, notify, callback, name,
2718 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
2720 static void vring_free(struct virtqueue *_vq)
2722 struct vring_virtqueue *vq = to_vvq(_vq);
2724 if (vq->we_own_ring) {
2725 if (vq->packed_ring) {
2726 vring_free_queue(vq->vq.vdev,
2727 vq->packed.ring_size_in_bytes,
2728 vq->packed.vring.desc,
2729 vq->packed.ring_dma_addr,
2732 vring_free_queue(vq->vq.vdev,
2733 vq->packed.event_size_in_bytes,
2734 vq->packed.vring.driver,
2735 vq->packed.driver_event_dma_addr,
2738 vring_free_queue(vq->vq.vdev,
2739 vq->packed.event_size_in_bytes,
2740 vq->packed.vring.device,
2741 vq->packed.device_event_dma_addr,
2744 kfree(vq->packed.desc_state);
2745 kfree(vq->packed.desc_extra);
2747 vring_free_queue(vq->vq.vdev,
2748 vq->split.queue_size_in_bytes,
2749 vq->split.vring.desc,
2750 vq->split.queue_dma_addr,
2754 if (!vq->packed_ring) {
2755 kfree(vq->split.desc_state);
2756 kfree(vq->split.desc_extra);
2760 void vring_del_virtqueue(struct virtqueue *_vq)
2762 struct vring_virtqueue *vq = to_vvq(_vq);
2764 spin_lock(&vq->vq.vdev->vqs_list_lock);
2765 list_del(&_vq->list);
2766 spin_unlock(&vq->vq.vdev->vqs_list_lock);
2772 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
2774 u32 vring_notification_data(struct virtqueue *_vq)
2776 struct vring_virtqueue *vq = to_vvq(_vq);
2779 if (vq->packed_ring)
2780 next = (vq->packed.next_avail_idx &
2781 ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR))) |
2782 vq->packed.avail_wrap_counter <<
2783 VRING_PACKED_EVENT_F_WRAP_CTR;
2785 next = vq->split.avail_idx_shadow;
2787 return next << 16 | _vq->index;
2789 EXPORT_SYMBOL_GPL(vring_notification_data);
2791 /* Manipulates transport-specific feature bits. */
2792 void vring_transport_features(struct virtio_device *vdev)
2796 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2798 case VIRTIO_RING_F_INDIRECT_DESC:
2800 case VIRTIO_RING_F_EVENT_IDX:
2802 case VIRTIO_F_VERSION_1:
2804 case VIRTIO_F_ACCESS_PLATFORM:
2806 case VIRTIO_F_RING_PACKED:
2808 case VIRTIO_F_ORDER_PLATFORM:
2810 case VIRTIO_F_NOTIFICATION_DATA:
2813 /* We don't understand this bit. */
2814 __virtio_clear_bit(vdev, i);
2818 EXPORT_SYMBOL_GPL(vring_transport_features);
2821 * virtqueue_get_vring_size - return the size of the virtqueue's vring
2822 * @_vq: the struct virtqueue containing the vring of interest.
2824 * Returns the size of the vring. This is mainly used for boasting to
2825 * userspace. Unlike other operations, this need not be serialized.
2827 unsigned int virtqueue_get_vring_size(const struct virtqueue *_vq)
2830 const struct vring_virtqueue *vq = to_vvq(_vq);
2832 return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
2834 EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
2837 * This function should only be called by the core, not directly by the driver.
2839 void __virtqueue_break(struct virtqueue *_vq)
2841 struct vring_virtqueue *vq = to_vvq(_vq);
2843 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2844 WRITE_ONCE(vq->broken, true);
2846 EXPORT_SYMBOL_GPL(__virtqueue_break);
2849 * This function should only be called by the core, not directly by the driver.
2851 void __virtqueue_unbreak(struct virtqueue *_vq)
2853 struct vring_virtqueue *vq = to_vvq(_vq);
2855 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2856 WRITE_ONCE(vq->broken, false);
2858 EXPORT_SYMBOL_GPL(__virtqueue_unbreak);
2860 bool virtqueue_is_broken(const struct virtqueue *_vq)
2862 const struct vring_virtqueue *vq = to_vvq(_vq);
2864 return READ_ONCE(vq->broken);
2866 EXPORT_SYMBOL_GPL(virtqueue_is_broken);
2869 * This should prevent the device from being used, allowing drivers to
2870 * recover. You may need to grab appropriate locks to flush.
2872 void virtio_break_device(struct virtio_device *dev)
2874 struct virtqueue *_vq;
2876 spin_lock(&dev->vqs_list_lock);
2877 list_for_each_entry(_vq, &dev->vqs, list) {
2878 struct vring_virtqueue *vq = to_vvq(_vq);
2880 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2881 WRITE_ONCE(vq->broken, true);
2883 spin_unlock(&dev->vqs_list_lock);
2885 EXPORT_SYMBOL_GPL(virtio_break_device);
2888 * This should allow the device to be used by the driver. You may
2889 * need to grab appropriate locks to flush the write to
2890 * vq->broken. This should only be used in some specific case e.g
2891 * (probing and restoring). This function should only be called by the
2892 * core, not directly by the driver.
2894 void __virtio_unbreak_device(struct virtio_device *dev)
2896 struct virtqueue *_vq;
2898 spin_lock(&dev->vqs_list_lock);
2899 list_for_each_entry(_vq, &dev->vqs, list) {
2900 struct vring_virtqueue *vq = to_vvq(_vq);
2902 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2903 WRITE_ONCE(vq->broken, false);
2905 spin_unlock(&dev->vqs_list_lock);
2907 EXPORT_SYMBOL_GPL(__virtio_unbreak_device);
2909 dma_addr_t virtqueue_get_desc_addr(const struct virtqueue *_vq)
2911 const struct vring_virtqueue *vq = to_vvq(_vq);
2913 BUG_ON(!vq->we_own_ring);
2915 if (vq->packed_ring)
2916 return vq->packed.ring_dma_addr;
2918 return vq->split.queue_dma_addr;
2920 EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
2922 dma_addr_t virtqueue_get_avail_addr(const struct virtqueue *_vq)
2924 const struct vring_virtqueue *vq = to_vvq(_vq);
2926 BUG_ON(!vq->we_own_ring);
2928 if (vq->packed_ring)
2929 return vq->packed.driver_event_dma_addr;
2931 return vq->split.queue_dma_addr +
2932 ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
2934 EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
2936 dma_addr_t virtqueue_get_used_addr(const struct virtqueue *_vq)
2938 const struct vring_virtqueue *vq = to_vvq(_vq);
2940 BUG_ON(!vq->we_own_ring);
2942 if (vq->packed_ring)
2943 return vq->packed.device_event_dma_addr;
2945 return vq->split.queue_dma_addr +
2946 ((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
2948 EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
2950 /* Only available for split ring */
2951 const struct vring *virtqueue_get_vring(const struct virtqueue *vq)
2953 return &to_vvq(vq)->split.vring;
2955 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
2957 MODULE_LICENSE("GPL");