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 /* Do DMA mapping by driver */
178 /* Do unmap or not for desc. Just when premapped is False and
179 * use_dma_api is true, this is true.
183 /* Head of free buffer list. */
184 unsigned int free_head;
185 /* Number we've added since last sync. */
186 unsigned int num_added;
188 /* Last used index we've seen.
189 * for split ring, it just contains last used index
191 * bits up to VRING_PACKED_EVENT_F_WRAP_CTR include the last used index.
192 * bits from VRING_PACKED_EVENT_F_WRAP_CTR include the used wrap counter.
196 /* Hint for event idx: already triggered no need to disable. */
197 bool event_triggered;
200 /* Available for split ring */
201 struct vring_virtqueue_split split;
203 /* Available for packed ring */
204 struct vring_virtqueue_packed packed;
207 /* How to notify other side. FIXME: commonalize hcalls! */
208 bool (*notify)(struct virtqueue *vq);
210 /* DMA, allocation, and size information */
213 /* Device used for doing DMA */
214 struct device *dma_dev;
217 /* They're supposed to lock for us. */
220 /* Figure out if their kicks are too delayed. */
221 bool last_add_time_valid;
222 ktime_t last_add_time;
226 static struct virtqueue *__vring_new_virtqueue(unsigned int index,
227 struct vring_virtqueue_split *vring_split,
228 struct virtio_device *vdev,
231 bool (*notify)(struct virtqueue *),
232 void (*callback)(struct virtqueue *),
234 struct device *dma_dev);
235 static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num);
236 static void vring_free(struct virtqueue *_vq);
242 #define to_vvq(_vq) container_of_const(_vq, struct vring_virtqueue, vq)
244 static bool virtqueue_use_indirect(const struct vring_virtqueue *vq,
245 unsigned int total_sg)
248 * If the host supports indirect descriptor tables, and we have multiple
249 * buffers, then go indirect. FIXME: tune this threshold
251 return (vq->indirect && total_sg > 1 && vq->vq.num_free);
255 * Modern virtio devices have feature bits to specify whether they need a
256 * quirk and bypass the IOMMU. If not there, just use the DMA API.
258 * If there, the interaction between virtio and DMA API is messy.
260 * On most systems with virtio, physical addresses match bus addresses,
261 * and it doesn't particularly matter whether we use the DMA API.
263 * On some systems, including Xen and any system with a physical device
264 * that speaks virtio behind a physical IOMMU, we must use the DMA API
265 * for virtio DMA to work at all.
267 * On other systems, including SPARC and PPC64, virtio-pci devices are
268 * enumerated as though they are behind an IOMMU, but the virtio host
269 * ignores the IOMMU, so we must either pretend that the IOMMU isn't
270 * there or somehow map everything as the identity.
272 * For the time being, we preserve historic behavior and bypass the DMA
275 * TODO: install a per-device DMA ops structure that does the right thing
276 * taking into account all the above quirks, and use the DMA API
277 * unconditionally on data path.
280 static bool vring_use_dma_api(const struct virtio_device *vdev)
282 if (!virtio_has_dma_quirk(vdev))
285 /* Otherwise, we are left to guess. */
287 * In theory, it's possible to have a buggy QEMU-supposed
288 * emulated Q35 IOMMU and Xen enabled at the same time. On
289 * such a configuration, virtio has never worked and will
290 * not work without an even larger kludge. Instead, enable
291 * the DMA API if we're a Xen guest, which at least allows
292 * all of the sensible Xen configurations to work correctly.
300 size_t virtio_max_dma_size(const struct virtio_device *vdev)
302 size_t max_segment_size = SIZE_MAX;
304 if (vring_use_dma_api(vdev))
305 max_segment_size = dma_max_mapping_size(vdev->dev.parent);
307 return max_segment_size;
309 EXPORT_SYMBOL_GPL(virtio_max_dma_size);
311 static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
312 dma_addr_t *dma_handle, gfp_t flag,
313 struct device *dma_dev)
315 if (vring_use_dma_api(vdev)) {
316 return dma_alloc_coherent(dma_dev, size,
319 void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
322 phys_addr_t phys_addr = virt_to_phys(queue);
323 *dma_handle = (dma_addr_t)phys_addr;
326 * Sanity check: make sure we dind't truncate
327 * the address. The only arches I can find that
328 * have 64-bit phys_addr_t but 32-bit dma_addr_t
329 * are certain non-highmem MIPS and x86
330 * configurations, but these configurations
331 * should never allocate physical pages above 32
332 * bits, so this is fine. Just in case, throw a
333 * warning and abort if we end up with an
334 * unrepresentable address.
336 if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
337 free_pages_exact(queue, PAGE_ALIGN(size));
345 static void vring_free_queue(struct virtio_device *vdev, size_t size,
346 void *queue, dma_addr_t dma_handle,
347 struct device *dma_dev)
349 if (vring_use_dma_api(vdev))
350 dma_free_coherent(dma_dev, size, queue, dma_handle);
352 free_pages_exact(queue, PAGE_ALIGN(size));
356 * The DMA ops on various arches are rather gnarly right now, and
357 * making all of the arch DMA ops work on the vring device itself
360 static struct device *vring_dma_dev(const struct vring_virtqueue *vq)
365 /* Map one sg entry. */
366 static int vring_map_one_sg(const struct vring_virtqueue *vq, struct scatterlist *sg,
367 enum dma_data_direction direction, dma_addr_t *addr)
370 *addr = sg_dma_address(sg);
374 if (!vq->use_dma_api) {
376 * If DMA is not used, KMSAN doesn't know that the scatterlist
377 * is initialized by the hardware. Explicitly check/unpoison it
378 * depending on the direction.
380 kmsan_handle_dma(sg_page(sg), sg->offset, sg->length, direction);
381 *addr = (dma_addr_t)sg_phys(sg);
386 * We can't use dma_map_sg, because we don't use scatterlists in
387 * the way it expects (we don't guarantee that the scatterlist
388 * will exist for the lifetime of the mapping).
390 *addr = dma_map_page(vring_dma_dev(vq),
391 sg_page(sg), sg->offset, sg->length,
394 if (dma_mapping_error(vring_dma_dev(vq), *addr))
400 static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
401 void *cpu_addr, size_t size,
402 enum dma_data_direction direction)
404 if (!vq->use_dma_api)
405 return (dma_addr_t)virt_to_phys(cpu_addr);
407 return dma_map_single(vring_dma_dev(vq),
408 cpu_addr, size, direction);
411 static int vring_mapping_error(const struct vring_virtqueue *vq,
414 if (!vq->use_dma_api)
417 return dma_mapping_error(vring_dma_dev(vq), addr);
420 static void virtqueue_init(struct vring_virtqueue *vq, u32 num)
422 vq->vq.num_free = num;
425 vq->last_used_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR);
427 vq->last_used_idx = 0;
429 vq->event_triggered = false;
434 vq->last_add_time_valid = false;
440 * Split ring specific functions - *_split().
443 static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
444 const struct vring_desc *desc)
451 flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
453 dma_unmap_page(vring_dma_dev(vq),
454 virtio64_to_cpu(vq->vq.vdev, desc->addr),
455 virtio32_to_cpu(vq->vq.vdev, desc->len),
456 (flags & VRING_DESC_F_WRITE) ?
457 DMA_FROM_DEVICE : DMA_TO_DEVICE);
460 static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
463 struct vring_desc_extra *extra = vq->split.desc_extra;
466 flags = extra[i].flags;
468 if (flags & VRING_DESC_F_INDIRECT) {
469 if (!vq->use_dma_api)
472 dma_unmap_single(vring_dma_dev(vq),
475 (flags & VRING_DESC_F_WRITE) ?
476 DMA_FROM_DEVICE : DMA_TO_DEVICE);
481 dma_unmap_page(vring_dma_dev(vq),
484 (flags & VRING_DESC_F_WRITE) ?
485 DMA_FROM_DEVICE : DMA_TO_DEVICE);
489 return extra[i].next;
492 static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
493 unsigned int total_sg,
496 struct vring_desc *desc;
500 * We require lowmem mappings for the descriptors because
501 * otherwise virt_to_phys will give us bogus addresses in the
504 gfp &= ~__GFP_HIGHMEM;
506 desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
510 for (i = 0; i < total_sg; i++)
511 desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
515 static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
516 struct vring_desc *desc,
523 struct vring_virtqueue *vring = to_vvq(vq);
524 struct vring_desc_extra *extra = vring->split.desc_extra;
527 desc[i].flags = cpu_to_virtio16(vq->vdev, flags);
528 desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
529 desc[i].len = cpu_to_virtio32(vq->vdev, len);
532 next = extra[i].next;
533 desc[i].next = cpu_to_virtio16(vq->vdev, next);
535 extra[i].addr = addr;
537 extra[i].flags = flags;
539 next = virtio16_to_cpu(vq->vdev, desc[i].next);
544 static inline int virtqueue_add_split(struct virtqueue *_vq,
545 struct scatterlist *sgs[],
546 unsigned int total_sg,
547 unsigned int out_sgs,
553 struct vring_virtqueue *vq = to_vvq(_vq);
554 struct scatterlist *sg;
555 struct vring_desc *desc;
556 unsigned int i, n, avail, descs_used, prev, err_idx;
562 BUG_ON(data == NULL);
563 BUG_ON(ctx && vq->indirect);
565 if (unlikely(vq->broken)) {
570 LAST_ADD_TIME_UPDATE(vq);
572 BUG_ON(total_sg == 0);
574 head = vq->free_head;
576 if (virtqueue_use_indirect(vq, total_sg))
577 desc = alloc_indirect_split(_vq, total_sg, gfp);
580 WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
584 /* Use a single buffer which doesn't continue */
586 /* Set up rest to use this indirect table. */
591 desc = vq->split.vring.desc;
593 descs_used = total_sg;
596 if (unlikely(vq->vq.num_free < descs_used)) {
597 pr_debug("Can't add buf len %i - avail = %i\n",
598 descs_used, vq->vq.num_free);
599 /* FIXME: for historical reasons, we force a notify here if
600 * there are outgoing parts to the buffer. Presumably the
601 * host should service the ring ASAP. */
610 for (n = 0; n < out_sgs; n++) {
611 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
614 if (vring_map_one_sg(vq, sg, DMA_TO_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, sg->length,
626 for (; n < (out_sgs + in_sgs); n++) {
627 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
630 if (vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr))
634 /* Note that we trust indirect descriptor
635 * table since it use stream DMA mapping.
637 i = virtqueue_add_desc_split(_vq, desc, i, addr,
644 /* Last one doesn't continue. */
645 desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
646 if (!indirect && vq->do_unmap)
647 vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
651 /* Now that the indirect table is filled in, map it. */
652 dma_addr_t addr = vring_map_single(
653 vq, desc, total_sg * sizeof(struct vring_desc),
655 if (vring_mapping_error(vq, addr)) {
662 virtqueue_add_desc_split(_vq, vq->split.vring.desc,
664 total_sg * sizeof(struct vring_desc),
665 VRING_DESC_F_INDIRECT,
669 /* We're using some buffers from the free list. */
670 vq->vq.num_free -= descs_used;
672 /* Update free pointer */
674 vq->free_head = vq->split.desc_extra[head].next;
678 /* Store token and indirect buffer state. */
679 vq->split.desc_state[head].data = data;
681 vq->split.desc_state[head].indir_desc = desc;
683 vq->split.desc_state[head].indir_desc = ctx;
685 /* Put entry in available array (but don't update avail->idx until they
687 avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
688 vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
690 /* Descriptors and available array need to be set before we expose the
691 * new available array entries. */
692 virtio_wmb(vq->weak_barriers);
693 vq->split.avail_idx_shadow++;
694 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
695 vq->split.avail_idx_shadow);
698 pr_debug("Added buffer head %i to %p\n", head, vq);
701 /* This is very unlikely, but theoretically possible. Kick
703 if (unlikely(vq->num_added == (1 << 16) - 1))
716 for (n = 0; n < total_sg; n++) {
720 vring_unmap_one_split_indirect(vq, &desc[i]);
721 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
723 i = vring_unmap_one_split(vq, i);
734 static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
736 struct vring_virtqueue *vq = to_vvq(_vq);
741 /* We need to expose available array entries before checking avail
743 virtio_mb(vq->weak_barriers);
745 old = vq->split.avail_idx_shadow - vq->num_added;
746 new = vq->split.avail_idx_shadow;
749 LAST_ADD_TIME_CHECK(vq);
750 LAST_ADD_TIME_INVALID(vq);
753 needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
754 vring_avail_event(&vq->split.vring)),
757 needs_kick = !(vq->split.vring.used->flags &
758 cpu_to_virtio16(_vq->vdev,
759 VRING_USED_F_NO_NOTIFY));
765 static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
769 __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
771 /* Clear data ptr. */
772 vq->split.desc_state[head].data = NULL;
774 /* Put back on free list: unmap first-level descriptors and find end */
777 while (vq->split.vring.desc[i].flags & nextflag) {
778 vring_unmap_one_split(vq, i);
779 i = vq->split.desc_extra[i].next;
783 vring_unmap_one_split(vq, i);
784 vq->split.desc_extra[i].next = vq->free_head;
785 vq->free_head = head;
787 /* Plus final descriptor */
791 struct vring_desc *indir_desc =
792 vq->split.desc_state[head].indir_desc;
795 /* Free the indirect table, if any, now that it's unmapped. */
799 len = vq->split.desc_extra[head].len;
801 BUG_ON(!(vq->split.desc_extra[head].flags &
802 VRING_DESC_F_INDIRECT));
803 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
806 for (j = 0; j < len / sizeof(struct vring_desc); j++)
807 vring_unmap_one_split_indirect(vq, &indir_desc[j]);
811 vq->split.desc_state[head].indir_desc = NULL;
813 *ctx = vq->split.desc_state[head].indir_desc;
817 static bool more_used_split(const struct vring_virtqueue *vq)
819 return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
820 vq->split.vring.used->idx);
823 static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
827 struct vring_virtqueue *vq = to_vvq(_vq);
834 if (unlikely(vq->broken)) {
839 if (!more_used_split(vq)) {
840 pr_debug("No more buffers in queue\n");
845 /* Only get used array entries after they have been exposed by host. */
846 virtio_rmb(vq->weak_barriers);
848 last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
849 i = virtio32_to_cpu(_vq->vdev,
850 vq->split.vring.used->ring[last_used].id);
851 *len = virtio32_to_cpu(_vq->vdev,
852 vq->split.vring.used->ring[last_used].len);
854 if (unlikely(i >= vq->split.vring.num)) {
855 BAD_RING(vq, "id %u out of range\n", i);
858 if (unlikely(!vq->split.desc_state[i].data)) {
859 BAD_RING(vq, "id %u is not a head!\n", i);
863 /* detach_buf_split clears data, so grab it now. */
864 ret = vq->split.desc_state[i].data;
865 detach_buf_split(vq, i, ctx);
867 /* If we expect an interrupt for the next entry, tell host
868 * by writing event index and flush out the write before
869 * the read in the next get_buf call. */
870 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
871 virtio_store_mb(vq->weak_barriers,
872 &vring_used_event(&vq->split.vring),
873 cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
875 LAST_ADD_TIME_INVALID(vq);
881 static void virtqueue_disable_cb_split(struct virtqueue *_vq)
883 struct vring_virtqueue *vq = to_vvq(_vq);
885 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
886 vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
889 * If device triggered an event already it won't trigger one again:
890 * no need to disable.
892 if (vq->event_triggered)
896 /* TODO: this is a hack. Figure out a cleaner value to write. */
897 vring_used_event(&vq->split.vring) = 0x0;
899 vq->split.vring.avail->flags =
900 cpu_to_virtio16(_vq->vdev,
901 vq->split.avail_flags_shadow);
905 static unsigned int virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
907 struct vring_virtqueue *vq = to_vvq(_vq);
912 /* We optimistically turn back on interrupts, then check if there was
914 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
915 * either clear the flags bit or point the event index at the next
916 * entry. Always do both to keep code simple. */
917 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
918 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
920 vq->split.vring.avail->flags =
921 cpu_to_virtio16(_vq->vdev,
922 vq->split.avail_flags_shadow);
924 vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
925 last_used_idx = vq->last_used_idx);
927 return last_used_idx;
930 static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned int last_used_idx)
932 struct vring_virtqueue *vq = to_vvq(_vq);
934 return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
935 vq->split.vring.used->idx);
938 static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
940 struct vring_virtqueue *vq = to_vvq(_vq);
945 /* We optimistically turn back on interrupts, then check if there was
947 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
948 * either clear the flags bit or point the event index at the next
949 * entry. Always update the event index to keep code simple. */
950 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
951 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
953 vq->split.vring.avail->flags =
954 cpu_to_virtio16(_vq->vdev,
955 vq->split.avail_flags_shadow);
957 /* TODO: tune this threshold */
958 bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
960 virtio_store_mb(vq->weak_barriers,
961 &vring_used_event(&vq->split.vring),
962 cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
964 if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
965 - vq->last_used_idx) > bufs)) {
974 static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
976 struct vring_virtqueue *vq = to_vvq(_vq);
982 for (i = 0; i < vq->split.vring.num; i++) {
983 if (!vq->split.desc_state[i].data)
985 /* detach_buf_split clears data, so grab it now. */
986 buf = vq->split.desc_state[i].data;
987 detach_buf_split(vq, i, NULL);
988 vq->split.avail_idx_shadow--;
989 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
990 vq->split.avail_idx_shadow);
994 /* That should have freed everything. */
995 BUG_ON(vq->vq.num_free != vq->split.vring.num);
1001 static void virtqueue_vring_init_split(struct vring_virtqueue_split *vring_split,
1002 struct vring_virtqueue *vq)
1004 struct virtio_device *vdev;
1008 vring_split->avail_flags_shadow = 0;
1009 vring_split->avail_idx_shadow = 0;
1011 /* No callback? Tell other side not to bother us. */
1012 if (!vq->vq.callback) {
1013 vring_split->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
1015 vring_split->vring.avail->flags = cpu_to_virtio16(vdev,
1016 vring_split->avail_flags_shadow);
1020 static void virtqueue_reinit_split(struct vring_virtqueue *vq)
1024 num = vq->split.vring.num;
1026 vq->split.vring.avail->flags = 0;
1027 vq->split.vring.avail->idx = 0;
1029 /* reset avail event */
1030 vq->split.vring.avail->ring[num] = 0;
1032 vq->split.vring.used->flags = 0;
1033 vq->split.vring.used->idx = 0;
1035 /* reset used event */
1036 *(__virtio16 *)&(vq->split.vring.used->ring[num]) = 0;
1038 virtqueue_init(vq, num);
1040 virtqueue_vring_init_split(&vq->split, vq);
1043 static void virtqueue_vring_attach_split(struct vring_virtqueue *vq,
1044 struct vring_virtqueue_split *vring_split)
1046 vq->split = *vring_split;
1048 /* Put everything in free lists. */
1052 static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vring_split)
1054 struct vring_desc_state_split *state;
1055 struct vring_desc_extra *extra;
1056 u32 num = vring_split->vring.num;
1058 state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL);
1062 extra = vring_alloc_desc_extra(num);
1066 memset(state, 0, num * sizeof(struct vring_desc_state_split));
1068 vring_split->desc_state = state;
1069 vring_split->desc_extra = extra;
1078 static void vring_free_split(struct vring_virtqueue_split *vring_split,
1079 struct virtio_device *vdev, struct device *dma_dev)
1081 vring_free_queue(vdev, vring_split->queue_size_in_bytes,
1082 vring_split->vring.desc,
1083 vring_split->queue_dma_addr,
1086 kfree(vring_split->desc_state);
1087 kfree(vring_split->desc_extra);
1090 static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
1091 struct virtio_device *vdev,
1093 unsigned int vring_align,
1094 bool may_reduce_num,
1095 struct device *dma_dev)
1098 dma_addr_t dma_addr;
1100 /* We assume num is a power of 2. */
1101 if (!is_power_of_2(num)) {
1102 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
1106 /* TODO: allocate each queue chunk individually */
1107 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
1108 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1110 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1114 if (!may_reduce_num)
1122 /* Try to get a single page. You are my only hope! */
1123 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1124 &dma_addr, GFP_KERNEL | __GFP_ZERO,
1130 vring_init(&vring_split->vring, num, queue, vring_align);
1132 vring_split->queue_dma_addr = dma_addr;
1133 vring_split->queue_size_in_bytes = vring_size(num, vring_align);
1135 vring_split->vring_align = vring_align;
1136 vring_split->may_reduce_num = may_reduce_num;
1141 static struct virtqueue *vring_create_virtqueue_split(
1144 unsigned int vring_align,
1145 struct virtio_device *vdev,
1147 bool may_reduce_num,
1149 bool (*notify)(struct virtqueue *),
1150 void (*callback)(struct virtqueue *),
1152 struct device *dma_dev)
1154 struct vring_virtqueue_split vring_split = {};
1155 struct virtqueue *vq;
1158 err = vring_alloc_queue_split(&vring_split, vdev, num, vring_align,
1159 may_reduce_num, dma_dev);
1163 vq = __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
1164 context, notify, callback, name, dma_dev);
1166 vring_free_split(&vring_split, vdev, dma_dev);
1170 to_vvq(vq)->we_own_ring = true;
1175 static int virtqueue_resize_split(struct virtqueue *_vq, u32 num)
1177 struct vring_virtqueue_split vring_split = {};
1178 struct vring_virtqueue *vq = to_vvq(_vq);
1179 struct virtio_device *vdev = _vq->vdev;
1182 err = vring_alloc_queue_split(&vring_split, vdev, num,
1183 vq->split.vring_align,
1184 vq->split.may_reduce_num,
1189 err = vring_alloc_state_extra_split(&vring_split);
1191 goto err_state_extra;
1193 vring_free(&vq->vq);
1195 virtqueue_vring_init_split(&vring_split, vq);
1197 virtqueue_init(vq, vring_split.vring.num);
1198 virtqueue_vring_attach_split(vq, &vring_split);
1203 vring_free_split(&vring_split, vdev, vring_dma_dev(vq));
1205 virtqueue_reinit_split(vq);
1211 * Packed ring specific functions - *_packed().
1213 static bool packed_used_wrap_counter(u16 last_used_idx)
1215 return !!(last_used_idx & (1 << VRING_PACKED_EVENT_F_WRAP_CTR));
1218 static u16 packed_last_used(u16 last_used_idx)
1220 return last_used_idx & ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR));
1223 static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
1224 const struct vring_desc_extra *extra)
1228 flags = extra->flags;
1230 if (flags & VRING_DESC_F_INDIRECT) {
1231 if (!vq->use_dma_api)
1234 dma_unmap_single(vring_dma_dev(vq),
1235 extra->addr, extra->len,
1236 (flags & VRING_DESC_F_WRITE) ?
1237 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1242 dma_unmap_page(vring_dma_dev(vq),
1243 extra->addr, extra->len,
1244 (flags & VRING_DESC_F_WRITE) ?
1245 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1249 static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
1250 const struct vring_packed_desc *desc)
1257 flags = le16_to_cpu(desc->flags);
1259 dma_unmap_page(vring_dma_dev(vq),
1260 le64_to_cpu(desc->addr),
1261 le32_to_cpu(desc->len),
1262 (flags & VRING_DESC_F_WRITE) ?
1263 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1266 static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
1269 struct vring_packed_desc *desc;
1272 * We require lowmem mappings for the descriptors because
1273 * otherwise virt_to_phys will give us bogus addresses in the
1276 gfp &= ~__GFP_HIGHMEM;
1278 desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
1283 static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
1284 struct scatterlist *sgs[],
1285 unsigned int total_sg,
1286 unsigned int out_sgs,
1287 unsigned int in_sgs,
1291 struct vring_packed_desc *desc;
1292 struct scatterlist *sg;
1293 unsigned int i, n, err_idx;
1297 head = vq->packed.next_avail_idx;
1298 desc = alloc_indirect_packed(total_sg, gfp);
1302 if (unlikely(vq->vq.num_free < 1)) {
1303 pr_debug("Can't add buf len 1 - avail = 0\n");
1311 BUG_ON(id == vq->packed.vring.num);
1313 for (n = 0; n < out_sgs + in_sgs; n++) {
1314 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1315 if (vring_map_one_sg(vq, sg, n < out_sgs ?
1316 DMA_TO_DEVICE : DMA_FROM_DEVICE, &addr))
1319 desc[i].flags = cpu_to_le16(n < out_sgs ?
1320 0 : VRING_DESC_F_WRITE);
1321 desc[i].addr = cpu_to_le64(addr);
1322 desc[i].len = cpu_to_le32(sg->length);
1327 /* Now that the indirect table is filled in, map it. */
1328 addr = vring_map_single(vq, desc,
1329 total_sg * sizeof(struct vring_packed_desc),
1331 if (vring_mapping_error(vq, addr)) {
1338 vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
1339 vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
1340 sizeof(struct vring_packed_desc));
1341 vq->packed.vring.desc[head].id = cpu_to_le16(id);
1344 vq->packed.desc_extra[id].addr = addr;
1345 vq->packed.desc_extra[id].len = total_sg *
1346 sizeof(struct vring_packed_desc);
1347 vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
1348 vq->packed.avail_used_flags;
1352 * A driver MUST NOT make the first descriptor in the list
1353 * available before all subsequent descriptors comprising
1354 * the list are made available.
1356 virtio_wmb(vq->weak_barriers);
1357 vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
1358 vq->packed.avail_used_flags);
1360 /* We're using some buffers from the free list. */
1361 vq->vq.num_free -= 1;
1363 /* Update free pointer */
1365 if (n >= vq->packed.vring.num) {
1367 vq->packed.avail_wrap_counter ^= 1;
1368 vq->packed.avail_used_flags ^=
1369 1 << VRING_PACKED_DESC_F_AVAIL |
1370 1 << VRING_PACKED_DESC_F_USED;
1372 vq->packed.next_avail_idx = n;
1373 vq->free_head = vq->packed.desc_extra[id].next;
1375 /* Store token and indirect buffer state. */
1376 vq->packed.desc_state[id].num = 1;
1377 vq->packed.desc_state[id].data = data;
1378 vq->packed.desc_state[id].indir_desc = desc;
1379 vq->packed.desc_state[id].last = id;
1383 pr_debug("Added buffer head %i to %p\n", head, vq);
1391 for (i = 0; i < err_idx; i++)
1392 vring_unmap_desc_packed(vq, &desc[i]);
1401 static inline int virtqueue_add_packed(struct virtqueue *_vq,
1402 struct scatterlist *sgs[],
1403 unsigned int total_sg,
1404 unsigned int out_sgs,
1405 unsigned int in_sgs,
1410 struct vring_virtqueue *vq = to_vvq(_vq);
1411 struct vring_packed_desc *desc;
1412 struct scatterlist *sg;
1413 unsigned int i, n, c, descs_used, err_idx;
1414 __le16 head_flags, flags;
1415 u16 head, id, prev, curr, avail_used_flags;
1420 BUG_ON(data == NULL);
1421 BUG_ON(ctx && vq->indirect);
1423 if (unlikely(vq->broken)) {
1428 LAST_ADD_TIME_UPDATE(vq);
1430 BUG_ON(total_sg == 0);
1432 if (virtqueue_use_indirect(vq, total_sg)) {
1433 err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
1435 if (err != -ENOMEM) {
1440 /* fall back on direct */
1443 head = vq->packed.next_avail_idx;
1444 avail_used_flags = vq->packed.avail_used_flags;
1446 WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
1448 desc = vq->packed.vring.desc;
1450 descs_used = total_sg;
1452 if (unlikely(vq->vq.num_free < descs_used)) {
1453 pr_debug("Can't add buf len %i - avail = %i\n",
1454 descs_used, vq->vq.num_free);
1460 BUG_ON(id == vq->packed.vring.num);
1464 for (n = 0; n < out_sgs + in_sgs; n++) {
1465 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1468 if (vring_map_one_sg(vq, sg, n < out_sgs ?
1469 DMA_TO_DEVICE : DMA_FROM_DEVICE, &addr))
1472 flags = cpu_to_le16(vq->packed.avail_used_flags |
1473 (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
1474 (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
1478 desc[i].flags = flags;
1480 desc[i].addr = cpu_to_le64(addr);
1481 desc[i].len = cpu_to_le32(sg->length);
1482 desc[i].id = cpu_to_le16(id);
1484 if (unlikely(vq->do_unmap)) {
1485 vq->packed.desc_extra[curr].addr = addr;
1486 vq->packed.desc_extra[curr].len = sg->length;
1487 vq->packed.desc_extra[curr].flags =
1491 curr = vq->packed.desc_extra[curr].next;
1493 if ((unlikely(++i >= vq->packed.vring.num))) {
1495 vq->packed.avail_used_flags ^=
1496 1 << VRING_PACKED_DESC_F_AVAIL |
1497 1 << VRING_PACKED_DESC_F_USED;
1503 vq->packed.avail_wrap_counter ^= 1;
1505 /* We're using some buffers from the free list. */
1506 vq->vq.num_free -= descs_used;
1508 /* Update free pointer */
1509 vq->packed.next_avail_idx = i;
1510 vq->free_head = curr;
1513 vq->packed.desc_state[id].num = descs_used;
1514 vq->packed.desc_state[id].data = data;
1515 vq->packed.desc_state[id].indir_desc = ctx;
1516 vq->packed.desc_state[id].last = prev;
1519 * A driver MUST NOT make the first descriptor in the list
1520 * available before all subsequent descriptors comprising
1521 * the list are made available.
1523 virtio_wmb(vq->weak_barriers);
1524 vq->packed.vring.desc[head].flags = head_flags;
1525 vq->num_added += descs_used;
1527 pr_debug("Added buffer head %i to %p\n", head, vq);
1535 curr = vq->free_head;
1537 vq->packed.avail_used_flags = avail_used_flags;
1539 for (n = 0; n < total_sg; n++) {
1542 vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]);
1543 curr = vq->packed.desc_extra[curr].next;
1545 if (i >= vq->packed.vring.num)
1553 static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
1555 struct vring_virtqueue *vq = to_vvq(_vq);
1556 u16 new, old, off_wrap, flags, wrap_counter, event_idx;
1569 * We need to expose the new flags value before checking notification
1572 virtio_mb(vq->weak_barriers);
1574 old = vq->packed.next_avail_idx - vq->num_added;
1575 new = vq->packed.next_avail_idx;
1578 snapshot.u32 = *(u32 *)vq->packed.vring.device;
1579 flags = le16_to_cpu(snapshot.flags);
1581 LAST_ADD_TIME_CHECK(vq);
1582 LAST_ADD_TIME_INVALID(vq);
1584 if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
1585 needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1589 off_wrap = le16_to_cpu(snapshot.off_wrap);
1591 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1592 event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1593 if (wrap_counter != vq->packed.avail_wrap_counter)
1594 event_idx -= vq->packed.vring.num;
1596 needs_kick = vring_need_event(event_idx, new, old);
1602 static void detach_buf_packed(struct vring_virtqueue *vq,
1603 unsigned int id, void **ctx)
1605 struct vring_desc_state_packed *state = NULL;
1606 struct vring_packed_desc *desc;
1607 unsigned int i, curr;
1609 state = &vq->packed.desc_state[id];
1611 /* Clear data ptr. */
1614 vq->packed.desc_extra[state->last].next = vq->free_head;
1616 vq->vq.num_free += state->num;
1618 if (unlikely(vq->do_unmap)) {
1620 for (i = 0; i < state->num; i++) {
1621 vring_unmap_extra_packed(vq,
1622 &vq->packed.desc_extra[curr]);
1623 curr = vq->packed.desc_extra[curr].next;
1630 /* Free the indirect table, if any, now that it's unmapped. */
1631 desc = state->indir_desc;
1636 len = vq->packed.desc_extra[id].len;
1637 for (i = 0; i < len / sizeof(struct vring_packed_desc);
1639 vring_unmap_desc_packed(vq, &desc[i]);
1642 state->indir_desc = NULL;
1644 *ctx = state->indir_desc;
1648 static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
1649 u16 idx, bool used_wrap_counter)
1654 flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
1655 avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
1656 used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
1658 return avail == used && used == used_wrap_counter;
1661 static bool more_used_packed(const struct vring_virtqueue *vq)
1665 bool used_wrap_counter;
1667 last_used_idx = READ_ONCE(vq->last_used_idx);
1668 last_used = packed_last_used(last_used_idx);
1669 used_wrap_counter = packed_used_wrap_counter(last_used_idx);
1670 return is_used_desc_packed(vq, last_used, used_wrap_counter);
1673 static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
1677 struct vring_virtqueue *vq = to_vvq(_vq);
1678 u16 last_used, id, last_used_idx;
1679 bool used_wrap_counter;
1684 if (unlikely(vq->broken)) {
1689 if (!more_used_packed(vq)) {
1690 pr_debug("No more buffers in queue\n");
1695 /* Only get used elements after they have been exposed by host. */
1696 virtio_rmb(vq->weak_barriers);
1698 last_used_idx = READ_ONCE(vq->last_used_idx);
1699 used_wrap_counter = packed_used_wrap_counter(last_used_idx);
1700 last_used = packed_last_used(last_used_idx);
1701 id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
1702 *len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
1704 if (unlikely(id >= vq->packed.vring.num)) {
1705 BAD_RING(vq, "id %u out of range\n", id);
1708 if (unlikely(!vq->packed.desc_state[id].data)) {
1709 BAD_RING(vq, "id %u is not a head!\n", id);
1713 /* detach_buf_packed clears data, so grab it now. */
1714 ret = vq->packed.desc_state[id].data;
1715 detach_buf_packed(vq, id, ctx);
1717 last_used += vq->packed.desc_state[id].num;
1718 if (unlikely(last_used >= vq->packed.vring.num)) {
1719 last_used -= vq->packed.vring.num;
1720 used_wrap_counter ^= 1;
1723 last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1724 WRITE_ONCE(vq->last_used_idx, last_used);
1727 * If we expect an interrupt for the next entry, tell host
1728 * by writing event index and flush out the write before
1729 * the read in the next get_buf call.
1731 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1732 virtio_store_mb(vq->weak_barriers,
1733 &vq->packed.vring.driver->off_wrap,
1734 cpu_to_le16(vq->last_used_idx));
1736 LAST_ADD_TIME_INVALID(vq);
1742 static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
1744 struct vring_virtqueue *vq = to_vvq(_vq);
1746 if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
1747 vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1750 * If device triggered an event already it won't trigger one again:
1751 * no need to disable.
1753 if (vq->event_triggered)
1756 vq->packed.vring.driver->flags =
1757 cpu_to_le16(vq->packed.event_flags_shadow);
1761 static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
1763 struct vring_virtqueue *vq = to_vvq(_vq);
1768 * We optimistically turn back on interrupts, then check if there was
1773 vq->packed.vring.driver->off_wrap =
1774 cpu_to_le16(vq->last_used_idx);
1776 * We need to update event offset and event wrap
1777 * counter first before updating event flags.
1779 virtio_wmb(vq->weak_barriers);
1782 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1783 vq->packed.event_flags_shadow = vq->event ?
1784 VRING_PACKED_EVENT_FLAG_DESC :
1785 VRING_PACKED_EVENT_FLAG_ENABLE;
1786 vq->packed.vring.driver->flags =
1787 cpu_to_le16(vq->packed.event_flags_shadow);
1791 return vq->last_used_idx;
1794 static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
1796 struct vring_virtqueue *vq = to_vvq(_vq);
1800 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1801 used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1803 return is_used_desc_packed(vq, used_idx, wrap_counter);
1806 static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
1808 struct vring_virtqueue *vq = to_vvq(_vq);
1809 u16 used_idx, wrap_counter, last_used_idx;
1815 * We optimistically turn back on interrupts, then check if there was
1820 /* TODO: tune this threshold */
1821 bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
1822 last_used_idx = READ_ONCE(vq->last_used_idx);
1823 wrap_counter = packed_used_wrap_counter(last_used_idx);
1825 used_idx = packed_last_used(last_used_idx) + bufs;
1826 if (used_idx >= vq->packed.vring.num) {
1827 used_idx -= vq->packed.vring.num;
1831 vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1832 (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1835 * We need to update event offset and event wrap
1836 * counter first before updating event flags.
1838 virtio_wmb(vq->weak_barriers);
1841 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1842 vq->packed.event_flags_shadow = vq->event ?
1843 VRING_PACKED_EVENT_FLAG_DESC :
1844 VRING_PACKED_EVENT_FLAG_ENABLE;
1845 vq->packed.vring.driver->flags =
1846 cpu_to_le16(vq->packed.event_flags_shadow);
1850 * We need to update event suppression structure first
1851 * before re-checking for more used buffers.
1853 virtio_mb(vq->weak_barriers);
1855 last_used_idx = READ_ONCE(vq->last_used_idx);
1856 wrap_counter = packed_used_wrap_counter(last_used_idx);
1857 used_idx = packed_last_used(last_used_idx);
1858 if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
1867 static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
1869 struct vring_virtqueue *vq = to_vvq(_vq);
1875 for (i = 0; i < vq->packed.vring.num; i++) {
1876 if (!vq->packed.desc_state[i].data)
1878 /* detach_buf clears data, so grab it now. */
1879 buf = vq->packed.desc_state[i].data;
1880 detach_buf_packed(vq, i, NULL);
1884 /* That should have freed everything. */
1885 BUG_ON(vq->vq.num_free != vq->packed.vring.num);
1891 static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num)
1893 struct vring_desc_extra *desc_extra;
1896 desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra),
1901 memset(desc_extra, 0, num * sizeof(struct vring_desc_extra));
1903 for (i = 0; i < num - 1; i++)
1904 desc_extra[i].next = i + 1;
1909 static void vring_free_packed(struct vring_virtqueue_packed *vring_packed,
1910 struct virtio_device *vdev,
1911 struct device *dma_dev)
1913 if (vring_packed->vring.desc)
1914 vring_free_queue(vdev, vring_packed->ring_size_in_bytes,
1915 vring_packed->vring.desc,
1916 vring_packed->ring_dma_addr,
1919 if (vring_packed->vring.driver)
1920 vring_free_queue(vdev, vring_packed->event_size_in_bytes,
1921 vring_packed->vring.driver,
1922 vring_packed->driver_event_dma_addr,
1925 if (vring_packed->vring.device)
1926 vring_free_queue(vdev, vring_packed->event_size_in_bytes,
1927 vring_packed->vring.device,
1928 vring_packed->device_event_dma_addr,
1931 kfree(vring_packed->desc_state);
1932 kfree(vring_packed->desc_extra);
1935 static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed,
1936 struct virtio_device *vdev,
1937 u32 num, struct device *dma_dev)
1939 struct vring_packed_desc *ring;
1940 struct vring_packed_desc_event *driver, *device;
1941 dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
1942 size_t ring_size_in_bytes, event_size_in_bytes;
1944 ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
1946 ring = vring_alloc_queue(vdev, ring_size_in_bytes,
1948 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1953 vring_packed->vring.desc = ring;
1954 vring_packed->ring_dma_addr = ring_dma_addr;
1955 vring_packed->ring_size_in_bytes = ring_size_in_bytes;
1957 event_size_in_bytes = sizeof(struct vring_packed_desc_event);
1959 driver = vring_alloc_queue(vdev, event_size_in_bytes,
1960 &driver_event_dma_addr,
1961 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1966 vring_packed->vring.driver = driver;
1967 vring_packed->event_size_in_bytes = event_size_in_bytes;
1968 vring_packed->driver_event_dma_addr = driver_event_dma_addr;
1970 device = vring_alloc_queue(vdev, event_size_in_bytes,
1971 &device_event_dma_addr,
1972 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1977 vring_packed->vring.device = device;
1978 vring_packed->device_event_dma_addr = device_event_dma_addr;
1980 vring_packed->vring.num = num;
1985 vring_free_packed(vring_packed, vdev, dma_dev);
1989 static int vring_alloc_state_extra_packed(struct vring_virtqueue_packed *vring_packed)
1991 struct vring_desc_state_packed *state;
1992 struct vring_desc_extra *extra;
1993 u32 num = vring_packed->vring.num;
1995 state = kmalloc_array(num, sizeof(struct vring_desc_state_packed), GFP_KERNEL);
1997 goto err_desc_state;
1999 memset(state, 0, num * sizeof(struct vring_desc_state_packed));
2001 extra = vring_alloc_desc_extra(num);
2003 goto err_desc_extra;
2005 vring_packed->desc_state = state;
2006 vring_packed->desc_extra = extra;
2016 static void virtqueue_vring_init_packed(struct vring_virtqueue_packed *vring_packed,
2019 vring_packed->next_avail_idx = 0;
2020 vring_packed->avail_wrap_counter = 1;
2021 vring_packed->event_flags_shadow = 0;
2022 vring_packed->avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
2024 /* No callback? Tell other side not to bother us. */
2026 vring_packed->event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
2027 vring_packed->vring.driver->flags =
2028 cpu_to_le16(vring_packed->event_flags_shadow);
2032 static void virtqueue_vring_attach_packed(struct vring_virtqueue *vq,
2033 struct vring_virtqueue_packed *vring_packed)
2035 vq->packed = *vring_packed;
2037 /* Put everything in free lists. */
2041 static void virtqueue_reinit_packed(struct vring_virtqueue *vq)
2043 memset(vq->packed.vring.device, 0, vq->packed.event_size_in_bytes);
2044 memset(vq->packed.vring.driver, 0, vq->packed.event_size_in_bytes);
2046 /* we need to reset the desc.flags. For more, see is_used_desc_packed() */
2047 memset(vq->packed.vring.desc, 0, vq->packed.ring_size_in_bytes);
2049 virtqueue_init(vq, vq->packed.vring.num);
2050 virtqueue_vring_init_packed(&vq->packed, !!vq->vq.callback);
2053 static struct virtqueue *vring_create_virtqueue_packed(
2056 unsigned int vring_align,
2057 struct virtio_device *vdev,
2059 bool may_reduce_num,
2061 bool (*notify)(struct virtqueue *),
2062 void (*callback)(struct virtqueue *),
2064 struct device *dma_dev)
2066 struct vring_virtqueue_packed vring_packed = {};
2067 struct vring_virtqueue *vq;
2070 if (vring_alloc_queue_packed(&vring_packed, vdev, num, dma_dev))
2073 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
2077 vq->vq.callback = callback;
2080 vq->vq.index = index;
2081 vq->vq.reset = false;
2082 vq->we_own_ring = true;
2083 vq->notify = notify;
2084 vq->weak_barriers = weak_barriers;
2085 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
2090 vq->packed_ring = true;
2091 vq->dma_dev = dma_dev;
2092 vq->use_dma_api = vring_use_dma_api(vdev);
2093 vq->premapped = false;
2094 vq->do_unmap = vq->use_dma_api;
2096 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
2098 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
2100 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
2101 vq->weak_barriers = false;
2103 err = vring_alloc_state_extra_packed(&vring_packed);
2105 goto err_state_extra;
2107 virtqueue_vring_init_packed(&vring_packed, !!callback);
2109 virtqueue_init(vq, num);
2110 virtqueue_vring_attach_packed(vq, &vring_packed);
2112 spin_lock(&vdev->vqs_list_lock);
2113 list_add_tail(&vq->vq.list, &vdev->vqs);
2114 spin_unlock(&vdev->vqs_list_lock);
2120 vring_free_packed(&vring_packed, vdev, dma_dev);
2125 static int virtqueue_resize_packed(struct virtqueue *_vq, u32 num)
2127 struct vring_virtqueue_packed vring_packed = {};
2128 struct vring_virtqueue *vq = to_vvq(_vq);
2129 struct virtio_device *vdev = _vq->vdev;
2132 if (vring_alloc_queue_packed(&vring_packed, vdev, num, vring_dma_dev(vq)))
2135 err = vring_alloc_state_extra_packed(&vring_packed);
2137 goto err_state_extra;
2139 vring_free(&vq->vq);
2141 virtqueue_vring_init_packed(&vring_packed, !!vq->vq.callback);
2143 virtqueue_init(vq, vring_packed.vring.num);
2144 virtqueue_vring_attach_packed(vq, &vring_packed);
2149 vring_free_packed(&vring_packed, vdev, vring_dma_dev(vq));
2151 virtqueue_reinit_packed(vq);
2155 static int virtqueue_disable_and_recycle(struct virtqueue *_vq,
2156 void (*recycle)(struct virtqueue *vq, void *buf))
2158 struct vring_virtqueue *vq = to_vvq(_vq);
2159 struct virtio_device *vdev = vq->vq.vdev;
2163 if (!vq->we_own_ring)
2166 if (!vdev->config->disable_vq_and_reset)
2169 if (!vdev->config->enable_vq_after_reset)
2172 err = vdev->config->disable_vq_and_reset(_vq);
2176 while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL)
2182 static int virtqueue_enable_after_reset(struct virtqueue *_vq)
2184 struct vring_virtqueue *vq = to_vvq(_vq);
2185 struct virtio_device *vdev = vq->vq.vdev;
2187 if (vdev->config->enable_vq_after_reset(_vq))
2194 * Generic functions and exported symbols.
2197 static inline int virtqueue_add(struct virtqueue *_vq,
2198 struct scatterlist *sgs[],
2199 unsigned int total_sg,
2200 unsigned int out_sgs,
2201 unsigned int in_sgs,
2206 struct vring_virtqueue *vq = to_vvq(_vq);
2208 return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
2209 out_sgs, in_sgs, data, ctx, gfp) :
2210 virtqueue_add_split(_vq, sgs, total_sg,
2211 out_sgs, in_sgs, data, ctx, gfp);
2215 * virtqueue_add_sgs - expose buffers to other end
2216 * @_vq: the struct virtqueue we're talking about.
2217 * @sgs: array of terminated scatterlists.
2218 * @out_sgs: the number of scatterlists readable by other side
2219 * @in_sgs: the number of scatterlists which are writable (after readable ones)
2220 * @data: the token identifying the buffer.
2221 * @gfp: how to do memory allocations (if necessary).
2223 * Caller must ensure we don't call this with other virtqueue operations
2224 * at the same time (except where noted).
2226 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2228 int virtqueue_add_sgs(struct virtqueue *_vq,
2229 struct scatterlist *sgs[],
2230 unsigned int out_sgs,
2231 unsigned int in_sgs,
2235 unsigned int i, total_sg = 0;
2237 /* Count them first. */
2238 for (i = 0; i < out_sgs + in_sgs; i++) {
2239 struct scatterlist *sg;
2241 for (sg = sgs[i]; sg; sg = sg_next(sg))
2244 return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
2247 EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
2250 * virtqueue_add_outbuf - expose output buffers to other end
2251 * @vq: the struct virtqueue we're talking about.
2252 * @sg: scatterlist (must be well-formed and terminated!)
2253 * @num: the number of entries in @sg readable by other side
2254 * @data: the token identifying the buffer.
2255 * @gfp: how to do memory allocations (if necessary).
2257 * Caller must ensure we don't call this with other virtqueue operations
2258 * at the same time (except where noted).
2260 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2262 int virtqueue_add_outbuf(struct virtqueue *vq,
2263 struct scatterlist *sg, unsigned int num,
2267 return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
2269 EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
2272 * virtqueue_add_inbuf - expose input buffers to other end
2273 * @vq: the struct virtqueue we're talking about.
2274 * @sg: scatterlist (must be well-formed and terminated!)
2275 * @num: the number of entries in @sg writable by other side
2276 * @data: the token identifying the buffer.
2277 * @gfp: how to do memory allocations (if necessary).
2279 * Caller must ensure we don't call this with other virtqueue operations
2280 * at the same time (except where noted).
2282 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2284 int virtqueue_add_inbuf(struct virtqueue *vq,
2285 struct scatterlist *sg, unsigned int num,
2289 return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
2291 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
2294 * virtqueue_add_inbuf_ctx - expose input buffers to other end
2295 * @vq: the struct virtqueue we're talking about.
2296 * @sg: scatterlist (must be well-formed and terminated!)
2297 * @num: the number of entries in @sg writable by other side
2298 * @data: the token identifying the buffer.
2299 * @ctx: extra context for the token
2300 * @gfp: how to do memory allocations (if necessary).
2302 * Caller must ensure we don't call this with other virtqueue operations
2303 * at the same time (except where noted).
2305 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2307 int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
2308 struct scatterlist *sg, unsigned int num,
2313 return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
2315 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
2318 * virtqueue_dma_dev - get the dma dev
2319 * @_vq: the struct virtqueue we're talking about.
2321 * Returns the dma dev. That can been used for dma api.
2323 struct device *virtqueue_dma_dev(struct virtqueue *_vq)
2325 struct vring_virtqueue *vq = to_vvq(_vq);
2327 if (vq->use_dma_api)
2328 return vring_dma_dev(vq);
2332 EXPORT_SYMBOL_GPL(virtqueue_dma_dev);
2335 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
2336 * @_vq: the struct virtqueue
2338 * Instead of virtqueue_kick(), you can do:
2339 * if (virtqueue_kick_prepare(vq))
2340 * virtqueue_notify(vq);
2342 * This is sometimes useful because the virtqueue_kick_prepare() needs
2343 * to be serialized, but the actual virtqueue_notify() call does not.
2345 bool virtqueue_kick_prepare(struct virtqueue *_vq)
2347 struct vring_virtqueue *vq = to_vvq(_vq);
2349 return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
2350 virtqueue_kick_prepare_split(_vq);
2352 EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
2355 * virtqueue_notify - second half of split virtqueue_kick call.
2356 * @_vq: the struct virtqueue
2358 * This does not need to be serialized.
2360 * Returns false if host notify failed or queue is broken, otherwise true.
2362 bool virtqueue_notify(struct virtqueue *_vq)
2364 struct vring_virtqueue *vq = to_vvq(_vq);
2366 if (unlikely(vq->broken))
2369 /* Prod other side to tell it about changes. */
2370 if (!vq->notify(_vq)) {
2376 EXPORT_SYMBOL_GPL(virtqueue_notify);
2379 * virtqueue_kick - update after add_buf
2380 * @vq: the struct virtqueue
2382 * After one or more virtqueue_add_* calls, invoke this to kick
2385 * Caller must ensure we don't call this with other virtqueue
2386 * operations at the same time (except where noted).
2388 * Returns false if kick failed, otherwise true.
2390 bool virtqueue_kick(struct virtqueue *vq)
2392 if (virtqueue_kick_prepare(vq))
2393 return virtqueue_notify(vq);
2396 EXPORT_SYMBOL_GPL(virtqueue_kick);
2399 * virtqueue_get_buf_ctx - get the next used buffer
2400 * @_vq: the struct virtqueue we're talking about.
2401 * @len: the length written into the buffer
2402 * @ctx: extra context for the token
2404 * If the device wrote data into the buffer, @len will be set to the
2405 * amount written. This means you don't need to clear the buffer
2406 * beforehand to ensure there's no data leakage in the case of short
2409 * Caller must ensure we don't call this with other virtqueue
2410 * operations at the same time (except where noted).
2412 * Returns NULL if there are no used buffers, or the "data" token
2413 * handed to virtqueue_add_*().
2415 void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
2418 struct vring_virtqueue *vq = to_vvq(_vq);
2420 return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
2421 virtqueue_get_buf_ctx_split(_vq, len, ctx);
2423 EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
2425 void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
2427 return virtqueue_get_buf_ctx(_vq, len, NULL);
2429 EXPORT_SYMBOL_GPL(virtqueue_get_buf);
2431 * virtqueue_disable_cb - disable callbacks
2432 * @_vq: the struct virtqueue we're talking about.
2434 * Note that this is not necessarily synchronous, hence unreliable and only
2435 * useful as an optimization.
2437 * Unlike other operations, this need not be serialized.
2439 void virtqueue_disable_cb(struct virtqueue *_vq)
2441 struct vring_virtqueue *vq = to_vvq(_vq);
2443 if (vq->packed_ring)
2444 virtqueue_disable_cb_packed(_vq);
2446 virtqueue_disable_cb_split(_vq);
2448 EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
2451 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
2452 * @_vq: the struct virtqueue we're talking about.
2454 * This re-enables callbacks; it returns current queue state
2455 * in an opaque unsigned value. This value should be later tested by
2456 * virtqueue_poll, to detect a possible race between the driver checking for
2457 * more work, and enabling callbacks.
2459 * Caller must ensure we don't call this with other virtqueue
2460 * operations at the same time (except where noted).
2462 unsigned int virtqueue_enable_cb_prepare(struct virtqueue *_vq)
2464 struct vring_virtqueue *vq = to_vvq(_vq);
2466 if (vq->event_triggered)
2467 vq->event_triggered = false;
2469 return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
2470 virtqueue_enable_cb_prepare_split(_vq);
2472 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
2475 * virtqueue_poll - query pending used buffers
2476 * @_vq: the struct virtqueue we're talking about.
2477 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
2479 * Returns "true" if there are pending used buffers in the queue.
2481 * This does not need to be serialized.
2483 bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx)
2485 struct vring_virtqueue *vq = to_vvq(_vq);
2487 if (unlikely(vq->broken))
2490 virtio_mb(vq->weak_barriers);
2491 return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
2492 virtqueue_poll_split(_vq, last_used_idx);
2494 EXPORT_SYMBOL_GPL(virtqueue_poll);
2497 * virtqueue_enable_cb - restart callbacks after disable_cb.
2498 * @_vq: the struct virtqueue we're talking about.
2500 * This re-enables callbacks; it returns "false" if there are pending
2501 * buffers in the queue, to detect a possible race between the driver
2502 * checking for more work, and enabling callbacks.
2504 * Caller must ensure we don't call this with other virtqueue
2505 * operations at the same time (except where noted).
2507 bool virtqueue_enable_cb(struct virtqueue *_vq)
2509 unsigned int last_used_idx = virtqueue_enable_cb_prepare(_vq);
2511 return !virtqueue_poll(_vq, last_used_idx);
2513 EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
2516 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
2517 * @_vq: the struct virtqueue we're talking about.
2519 * This re-enables callbacks but hints to the other side to delay
2520 * interrupts until most of the available buffers have been processed;
2521 * it returns "false" if there are many pending buffers in the queue,
2522 * to detect a possible race between the driver checking for more work,
2523 * and enabling callbacks.
2525 * Caller must ensure we don't call this with other virtqueue
2526 * operations at the same time (except where noted).
2528 bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
2530 struct vring_virtqueue *vq = to_vvq(_vq);
2532 if (vq->event_triggered)
2533 vq->event_triggered = false;
2535 return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
2536 virtqueue_enable_cb_delayed_split(_vq);
2538 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2541 * virtqueue_detach_unused_buf - detach first unused buffer
2542 * @_vq: the struct virtqueue we're talking about.
2544 * Returns NULL or the "data" token handed to virtqueue_add_*().
2545 * This is not valid on an active queue; it is useful for device
2546 * shutdown or the reset queue.
2548 void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2550 struct vring_virtqueue *vq = to_vvq(_vq);
2552 return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
2553 virtqueue_detach_unused_buf_split(_vq);
2555 EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
2557 static inline bool more_used(const struct vring_virtqueue *vq)
2559 return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
2563 * vring_interrupt - notify a virtqueue on an interrupt
2564 * @irq: the IRQ number (ignored)
2565 * @_vq: the struct virtqueue to notify
2567 * Calls the callback function of @_vq to process the virtqueue
2570 irqreturn_t vring_interrupt(int irq, void *_vq)
2572 struct vring_virtqueue *vq = to_vvq(_vq);
2574 if (!more_used(vq)) {
2575 pr_debug("virtqueue interrupt with no work for %p\n", vq);
2579 if (unlikely(vq->broken)) {
2580 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
2581 dev_warn_once(&vq->vq.vdev->dev,
2582 "virtio vring IRQ raised before DRIVER_OK");
2589 /* Just a hint for performance: so it's ok that this can be racy! */
2591 vq->event_triggered = true;
2593 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
2594 if (vq->vq.callback)
2595 vq->vq.callback(&vq->vq);
2599 EXPORT_SYMBOL_GPL(vring_interrupt);
2601 /* Only available for split ring */
2602 static struct virtqueue *__vring_new_virtqueue(unsigned int index,
2603 struct vring_virtqueue_split *vring_split,
2604 struct virtio_device *vdev,
2607 bool (*notify)(struct virtqueue *),
2608 void (*callback)(struct virtqueue *),
2610 struct device *dma_dev)
2612 struct vring_virtqueue *vq;
2615 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2618 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
2622 vq->packed_ring = false;
2623 vq->vq.callback = callback;
2626 vq->vq.index = index;
2627 vq->vq.reset = false;
2628 vq->we_own_ring = false;
2629 vq->notify = notify;
2630 vq->weak_barriers = weak_barriers;
2631 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
2636 vq->dma_dev = dma_dev;
2637 vq->use_dma_api = vring_use_dma_api(vdev);
2638 vq->premapped = false;
2639 vq->do_unmap = vq->use_dma_api;
2641 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
2643 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
2645 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
2646 vq->weak_barriers = false;
2648 err = vring_alloc_state_extra_split(vring_split);
2654 virtqueue_vring_init_split(vring_split, vq);
2656 virtqueue_init(vq, vring_split->vring.num);
2657 virtqueue_vring_attach_split(vq, vring_split);
2659 spin_lock(&vdev->vqs_list_lock);
2660 list_add_tail(&vq->vq.list, &vdev->vqs);
2661 spin_unlock(&vdev->vqs_list_lock);
2665 struct virtqueue *vring_create_virtqueue(
2668 unsigned int vring_align,
2669 struct virtio_device *vdev,
2671 bool may_reduce_num,
2673 bool (*notify)(struct virtqueue *),
2674 void (*callback)(struct virtqueue *),
2678 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2679 return vring_create_virtqueue_packed(index, num, vring_align,
2680 vdev, weak_barriers, may_reduce_num,
2681 context, notify, callback, name, vdev->dev.parent);
2683 return vring_create_virtqueue_split(index, num, vring_align,
2684 vdev, weak_barriers, may_reduce_num,
2685 context, notify, callback, name, vdev->dev.parent);
2687 EXPORT_SYMBOL_GPL(vring_create_virtqueue);
2689 struct virtqueue *vring_create_virtqueue_dma(
2692 unsigned int vring_align,
2693 struct virtio_device *vdev,
2695 bool may_reduce_num,
2697 bool (*notify)(struct virtqueue *),
2698 void (*callback)(struct virtqueue *),
2700 struct device *dma_dev)
2703 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2704 return vring_create_virtqueue_packed(index, num, vring_align,
2705 vdev, weak_barriers, may_reduce_num,
2706 context, notify, callback, name, dma_dev);
2708 return vring_create_virtqueue_split(index, num, vring_align,
2709 vdev, weak_barriers, may_reduce_num,
2710 context, notify, callback, name, dma_dev);
2712 EXPORT_SYMBOL_GPL(vring_create_virtqueue_dma);
2715 * virtqueue_resize - resize the vring of vq
2716 * @_vq: the struct virtqueue we're talking about.
2717 * @num: new ring num
2718 * @recycle: callback to recycle unused buffers
2720 * When it is really necessary to create a new vring, it will set the current vq
2721 * into the reset state. Then call the passed callback to recycle the buffer
2722 * that is no longer used. Only after the new vring is successfully created, the
2723 * old vring will be released.
2725 * Caller must ensure we don't call this with other virtqueue operations
2726 * at the same time (except where noted).
2728 * Returns zero or a negative error.
2730 * -ENOMEM: Failed to allocate a new ring, fall back to the original ring size.
2731 * vq can still work normally
2732 * -EBUSY: Failed to sync with device, vq may not work properly
2733 * -ENOENT: Transport or device not supported
2734 * -E2BIG/-EINVAL: num error
2735 * -EPERM: Operation not permitted
2738 int virtqueue_resize(struct virtqueue *_vq, u32 num,
2739 void (*recycle)(struct virtqueue *vq, void *buf))
2741 struct vring_virtqueue *vq = to_vvq(_vq);
2744 if (num > vq->vq.num_max)
2750 if ((vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num) == num)
2753 err = virtqueue_disable_and_recycle(_vq, recycle);
2757 if (vq->packed_ring)
2758 err = virtqueue_resize_packed(_vq, num);
2760 err = virtqueue_resize_split(_vq, num);
2762 return virtqueue_enable_after_reset(_vq);
2764 EXPORT_SYMBOL_GPL(virtqueue_resize);
2767 * virtqueue_set_dma_premapped - set the vring premapped mode
2768 * @_vq: the struct virtqueue we're talking about.
2770 * Enable the premapped mode of the vq.
2772 * The vring in premapped mode does not do dma internally, so the driver must
2773 * do dma mapping in advance. The driver must pass the dma_address through
2774 * dma_address of scatterlist. When the driver got a used buffer from
2775 * the vring, it has to unmap the dma address.
2777 * This function must be called immediately after creating the vq, or after vq
2778 * reset, and before adding any buffers to it.
2780 * Caller must ensure we don't call this with other virtqueue operations
2781 * at the same time (except where noted).
2783 * Returns zero or a negative error.
2785 * -EINVAL: vring does not use the dma api, so we can not enable premapped mode.
2787 int virtqueue_set_dma_premapped(struct virtqueue *_vq)
2789 struct vring_virtqueue *vq = to_vvq(_vq);
2794 num = vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
2796 if (num != vq->vq.num_free) {
2801 if (!vq->use_dma_api) {
2806 vq->premapped = true;
2807 vq->do_unmap = false;
2813 EXPORT_SYMBOL_GPL(virtqueue_set_dma_premapped);
2816 * virtqueue_reset - detach and recycle all unused buffers
2817 * @_vq: the struct virtqueue we're talking about.
2818 * @recycle: callback to recycle unused buffers
2820 * Caller must ensure we don't call this with other virtqueue operations
2821 * at the same time (except where noted).
2823 * Returns zero or a negative error.
2825 * -EBUSY: Failed to sync with device, vq may not work properly
2826 * -ENOENT: Transport or device not supported
2827 * -EPERM: Operation not permitted
2829 int virtqueue_reset(struct virtqueue *_vq,
2830 void (*recycle)(struct virtqueue *vq, void *buf))
2832 struct vring_virtqueue *vq = to_vvq(_vq);
2835 err = virtqueue_disable_and_recycle(_vq, recycle);
2839 if (vq->packed_ring)
2840 virtqueue_reinit_packed(vq);
2842 virtqueue_reinit_split(vq);
2844 return virtqueue_enable_after_reset(_vq);
2846 EXPORT_SYMBOL_GPL(virtqueue_reset);
2848 /* Only available for split ring */
2849 struct virtqueue *vring_new_virtqueue(unsigned int index,
2851 unsigned int vring_align,
2852 struct virtio_device *vdev,
2856 bool (*notify)(struct virtqueue *vq),
2857 void (*callback)(struct virtqueue *vq),
2860 struct vring_virtqueue_split vring_split = {};
2862 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2865 vring_init(&vring_split.vring, num, pages, vring_align);
2866 return __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
2867 context, notify, callback, name,
2870 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
2872 static void vring_free(struct virtqueue *_vq)
2874 struct vring_virtqueue *vq = to_vvq(_vq);
2876 if (vq->we_own_ring) {
2877 if (vq->packed_ring) {
2878 vring_free_queue(vq->vq.vdev,
2879 vq->packed.ring_size_in_bytes,
2880 vq->packed.vring.desc,
2881 vq->packed.ring_dma_addr,
2884 vring_free_queue(vq->vq.vdev,
2885 vq->packed.event_size_in_bytes,
2886 vq->packed.vring.driver,
2887 vq->packed.driver_event_dma_addr,
2890 vring_free_queue(vq->vq.vdev,
2891 vq->packed.event_size_in_bytes,
2892 vq->packed.vring.device,
2893 vq->packed.device_event_dma_addr,
2896 kfree(vq->packed.desc_state);
2897 kfree(vq->packed.desc_extra);
2899 vring_free_queue(vq->vq.vdev,
2900 vq->split.queue_size_in_bytes,
2901 vq->split.vring.desc,
2902 vq->split.queue_dma_addr,
2906 if (!vq->packed_ring) {
2907 kfree(vq->split.desc_state);
2908 kfree(vq->split.desc_extra);
2912 void vring_del_virtqueue(struct virtqueue *_vq)
2914 struct vring_virtqueue *vq = to_vvq(_vq);
2916 spin_lock(&vq->vq.vdev->vqs_list_lock);
2917 list_del(&_vq->list);
2918 spin_unlock(&vq->vq.vdev->vqs_list_lock);
2924 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
2926 u32 vring_notification_data(struct virtqueue *_vq)
2928 struct vring_virtqueue *vq = to_vvq(_vq);
2931 if (vq->packed_ring)
2932 next = (vq->packed.next_avail_idx &
2933 ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR))) |
2934 vq->packed.avail_wrap_counter <<
2935 VRING_PACKED_EVENT_F_WRAP_CTR;
2937 next = vq->split.avail_idx_shadow;
2939 return next << 16 | _vq->index;
2941 EXPORT_SYMBOL_GPL(vring_notification_data);
2943 /* Manipulates transport-specific feature bits. */
2944 void vring_transport_features(struct virtio_device *vdev)
2948 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2950 case VIRTIO_RING_F_INDIRECT_DESC:
2952 case VIRTIO_RING_F_EVENT_IDX:
2954 case VIRTIO_F_VERSION_1:
2956 case VIRTIO_F_ACCESS_PLATFORM:
2958 case VIRTIO_F_RING_PACKED:
2960 case VIRTIO_F_ORDER_PLATFORM:
2962 case VIRTIO_F_NOTIFICATION_DATA:
2965 /* We don't understand this bit. */
2966 __virtio_clear_bit(vdev, i);
2970 EXPORT_SYMBOL_GPL(vring_transport_features);
2973 * virtqueue_get_vring_size - return the size of the virtqueue's vring
2974 * @_vq: the struct virtqueue containing the vring of interest.
2976 * Returns the size of the vring. This is mainly used for boasting to
2977 * userspace. Unlike other operations, this need not be serialized.
2979 unsigned int virtqueue_get_vring_size(const struct virtqueue *_vq)
2982 const struct vring_virtqueue *vq = to_vvq(_vq);
2984 return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
2986 EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
2989 * This function should only be called by the core, not directly by the driver.
2991 void __virtqueue_break(struct virtqueue *_vq)
2993 struct vring_virtqueue *vq = to_vvq(_vq);
2995 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2996 WRITE_ONCE(vq->broken, true);
2998 EXPORT_SYMBOL_GPL(__virtqueue_break);
3001 * This function should only be called by the core, not directly by the driver.
3003 void __virtqueue_unbreak(struct virtqueue *_vq)
3005 struct vring_virtqueue *vq = to_vvq(_vq);
3007 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
3008 WRITE_ONCE(vq->broken, false);
3010 EXPORT_SYMBOL_GPL(__virtqueue_unbreak);
3012 bool virtqueue_is_broken(const struct virtqueue *_vq)
3014 const struct vring_virtqueue *vq = to_vvq(_vq);
3016 return READ_ONCE(vq->broken);
3018 EXPORT_SYMBOL_GPL(virtqueue_is_broken);
3021 * This should prevent the device from being used, allowing drivers to
3022 * recover. You may need to grab appropriate locks to flush.
3024 void virtio_break_device(struct virtio_device *dev)
3026 struct virtqueue *_vq;
3028 spin_lock(&dev->vqs_list_lock);
3029 list_for_each_entry(_vq, &dev->vqs, list) {
3030 struct vring_virtqueue *vq = to_vvq(_vq);
3032 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
3033 WRITE_ONCE(vq->broken, true);
3035 spin_unlock(&dev->vqs_list_lock);
3037 EXPORT_SYMBOL_GPL(virtio_break_device);
3040 * This should allow the device to be used by the driver. You may
3041 * need to grab appropriate locks to flush the write to
3042 * vq->broken. This should only be used in some specific case e.g
3043 * (probing and restoring). This function should only be called by the
3044 * core, not directly by the driver.
3046 void __virtio_unbreak_device(struct virtio_device *dev)
3048 struct virtqueue *_vq;
3050 spin_lock(&dev->vqs_list_lock);
3051 list_for_each_entry(_vq, &dev->vqs, list) {
3052 struct vring_virtqueue *vq = to_vvq(_vq);
3054 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
3055 WRITE_ONCE(vq->broken, false);
3057 spin_unlock(&dev->vqs_list_lock);
3059 EXPORT_SYMBOL_GPL(__virtio_unbreak_device);
3061 dma_addr_t virtqueue_get_desc_addr(const struct virtqueue *_vq)
3063 const struct vring_virtqueue *vq = to_vvq(_vq);
3065 BUG_ON(!vq->we_own_ring);
3067 if (vq->packed_ring)
3068 return vq->packed.ring_dma_addr;
3070 return vq->split.queue_dma_addr;
3072 EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
3074 dma_addr_t virtqueue_get_avail_addr(const struct virtqueue *_vq)
3076 const struct vring_virtqueue *vq = to_vvq(_vq);
3078 BUG_ON(!vq->we_own_ring);
3080 if (vq->packed_ring)
3081 return vq->packed.driver_event_dma_addr;
3083 return vq->split.queue_dma_addr +
3084 ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
3086 EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
3088 dma_addr_t virtqueue_get_used_addr(const struct virtqueue *_vq)
3090 const struct vring_virtqueue *vq = to_vvq(_vq);
3092 BUG_ON(!vq->we_own_ring);
3094 if (vq->packed_ring)
3095 return vq->packed.device_event_dma_addr;
3097 return vq->split.queue_dma_addr +
3098 ((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
3100 EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
3102 /* Only available for split ring */
3103 const struct vring *virtqueue_get_vring(const struct virtqueue *vq)
3105 return &to_vvq(vq)->split.vring;
3107 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
3110 * virtqueue_dma_map_single_attrs - map DMA for _vq
3111 * @_vq: the struct virtqueue we're talking about.
3112 * @ptr: the pointer of the buffer to do dma
3113 * @size: the size of the buffer to do dma
3114 * @dir: DMA direction
3117 * The caller calls this to do dma mapping in advance. The DMA address can be
3118 * passed to this _vq when it is in pre-mapped mode.
3120 * return DMA address. Caller should check that by virtqueue_dma_mapping_error().
3122 dma_addr_t virtqueue_dma_map_single_attrs(struct virtqueue *_vq, void *ptr,
3124 enum dma_data_direction dir,
3125 unsigned long attrs)
3127 struct vring_virtqueue *vq = to_vvq(_vq);
3129 if (!vq->use_dma_api)
3130 return (dma_addr_t)virt_to_phys(ptr);
3132 return dma_map_single_attrs(vring_dma_dev(vq), ptr, size, dir, attrs);
3134 EXPORT_SYMBOL_GPL(virtqueue_dma_map_single_attrs);
3137 * virtqueue_dma_unmap_single_attrs - unmap DMA for _vq
3138 * @_vq: the struct virtqueue we're talking about.
3139 * @addr: the dma address to unmap
3140 * @size: the size of the buffer
3141 * @dir: DMA direction
3144 * Unmap the address that is mapped by the virtqueue_dma_map_* APIs.
3147 void virtqueue_dma_unmap_single_attrs(struct virtqueue *_vq, dma_addr_t addr,
3148 size_t size, enum dma_data_direction dir,
3149 unsigned long attrs)
3151 struct vring_virtqueue *vq = to_vvq(_vq);
3153 if (!vq->use_dma_api)
3156 dma_unmap_single_attrs(vring_dma_dev(vq), addr, size, dir, attrs);
3158 EXPORT_SYMBOL_GPL(virtqueue_dma_unmap_single_attrs);
3161 * virtqueue_dma_mapping_error - check dma address
3162 * @_vq: the struct virtqueue we're talking about.
3163 * @addr: DMA address
3165 * Returns 0 means dma valid. Other means invalid dma address.
3167 int virtqueue_dma_mapping_error(struct virtqueue *_vq, dma_addr_t addr)
3169 struct vring_virtqueue *vq = to_vvq(_vq);
3171 if (!vq->use_dma_api)
3174 return dma_mapping_error(vring_dma_dev(vq), addr);
3176 EXPORT_SYMBOL_GPL(virtqueue_dma_mapping_error);
3179 * virtqueue_dma_need_sync - check a dma address needs sync
3180 * @_vq: the struct virtqueue we're talking about.
3181 * @addr: DMA address
3183 * Check if the dma address mapped by the virtqueue_dma_map_* APIs needs to be
3188 bool virtqueue_dma_need_sync(struct virtqueue *_vq, dma_addr_t addr)
3190 struct vring_virtqueue *vq = to_vvq(_vq);
3192 if (!vq->use_dma_api)
3195 return dma_need_sync(vring_dma_dev(vq), addr);
3197 EXPORT_SYMBOL_GPL(virtqueue_dma_need_sync);
3200 * virtqueue_dma_sync_single_range_for_cpu - dma sync for cpu
3201 * @_vq: the struct virtqueue we're talking about.
3202 * @addr: DMA address
3203 * @offset: DMA address offset
3204 * @size: buf size for sync
3205 * @dir: DMA direction
3207 * Before calling this function, use virtqueue_dma_need_sync() to confirm that
3208 * the DMA address really needs to be synchronized
3211 void virtqueue_dma_sync_single_range_for_cpu(struct virtqueue *_vq,
3213 unsigned long offset, size_t size,
3214 enum dma_data_direction dir)
3216 struct vring_virtqueue *vq = to_vvq(_vq);
3217 struct device *dev = vring_dma_dev(vq);
3219 if (!vq->use_dma_api)
3222 dma_sync_single_range_for_cpu(dev, addr, offset, size,
3225 EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_cpu);
3228 * virtqueue_dma_sync_single_range_for_device - dma sync for device
3229 * @_vq: the struct virtqueue we're talking about.
3230 * @addr: DMA address
3231 * @offset: DMA address offset
3232 * @size: buf size for sync
3233 * @dir: DMA direction
3235 * Before calling this function, use virtqueue_dma_need_sync() to confirm that
3236 * the DMA address really needs to be synchronized
3238 void virtqueue_dma_sync_single_range_for_device(struct virtqueue *_vq,
3240 unsigned long offset, size_t size,
3241 enum dma_data_direction dir)
3243 struct vring_virtqueue *vq = to_vvq(_vq);
3244 struct device *dev = vring_dma_dev(vq);
3246 if (!vq->use_dma_api)
3249 dma_sync_single_range_for_device(dev, addr, offset, size,
3252 EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_device);
3254 MODULE_LICENSE("GPL");