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(_vq, struct vring_virtqueue, vq)
236 static inline bool virtqueue_use_indirect(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(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(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 inline struct device *vring_dma_dev(const struct vring_virtqueue *vq)
357 /* Map one sg entry. */
358 static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
359 struct scatterlist *sg,
360 enum dma_data_direction direction)
362 if (!vq->use_dma_api) {
364 * If DMA is not used, KMSAN doesn't know that the scatterlist
365 * is initialized by the hardware. Explicitly check/unpoison it
366 * depending on the direction.
368 kmsan_handle_dma(sg_page(sg), sg->offset, sg->length, direction);
369 return (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 return dma_map_page(vring_dma_dev(vq),
378 sg_page(sg), sg->offset, sg->length,
382 static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
383 void *cpu_addr, size_t size,
384 enum dma_data_direction direction)
386 if (!vq->use_dma_api)
387 return (dma_addr_t)virt_to_phys(cpu_addr);
389 return dma_map_single(vring_dma_dev(vq),
390 cpu_addr, size, direction);
393 static int vring_mapping_error(const struct vring_virtqueue *vq,
396 if (!vq->use_dma_api)
399 return dma_mapping_error(vring_dma_dev(vq), addr);
402 static void virtqueue_init(struct vring_virtqueue *vq, u32 num)
404 vq->vq.num_free = num;
407 vq->last_used_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR);
409 vq->last_used_idx = 0;
411 vq->event_triggered = false;
416 vq->last_add_time_valid = false;
422 * Split ring specific functions - *_split().
425 static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
426 struct vring_desc *desc)
430 if (!vq->use_dma_api)
433 flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
435 dma_unmap_page(vring_dma_dev(vq),
436 virtio64_to_cpu(vq->vq.vdev, desc->addr),
437 virtio32_to_cpu(vq->vq.vdev, desc->len),
438 (flags & VRING_DESC_F_WRITE) ?
439 DMA_FROM_DEVICE : DMA_TO_DEVICE);
442 static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
445 struct vring_desc_extra *extra = vq->split.desc_extra;
448 if (!vq->use_dma_api)
451 flags = extra[i].flags;
453 if (flags & VRING_DESC_F_INDIRECT) {
454 dma_unmap_single(vring_dma_dev(vq),
457 (flags & VRING_DESC_F_WRITE) ?
458 DMA_FROM_DEVICE : DMA_TO_DEVICE);
460 dma_unmap_page(vring_dma_dev(vq),
463 (flags & VRING_DESC_F_WRITE) ?
464 DMA_FROM_DEVICE : DMA_TO_DEVICE);
468 return extra[i].next;
471 static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
472 unsigned int total_sg,
475 struct vring_desc *desc;
479 * We require lowmem mappings for the descriptors because
480 * otherwise virt_to_phys will give us bogus addresses in the
483 gfp &= ~__GFP_HIGHMEM;
485 desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
489 for (i = 0; i < total_sg; i++)
490 desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
494 static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
495 struct vring_desc *desc,
502 struct vring_virtqueue *vring = to_vvq(vq);
503 struct vring_desc_extra *extra = vring->split.desc_extra;
506 desc[i].flags = cpu_to_virtio16(vq->vdev, flags);
507 desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
508 desc[i].len = cpu_to_virtio32(vq->vdev, len);
511 next = extra[i].next;
512 desc[i].next = cpu_to_virtio16(vq->vdev, next);
514 extra[i].addr = addr;
516 extra[i].flags = flags;
518 next = virtio16_to_cpu(vq->vdev, desc[i].next);
523 static inline int virtqueue_add_split(struct virtqueue *_vq,
524 struct scatterlist *sgs[],
525 unsigned int total_sg,
526 unsigned int out_sgs,
532 struct vring_virtqueue *vq = to_vvq(_vq);
533 struct scatterlist *sg;
534 struct vring_desc *desc;
535 unsigned int i, n, avail, descs_used, prev, err_idx;
541 BUG_ON(data == NULL);
542 BUG_ON(ctx && vq->indirect);
544 if (unlikely(vq->broken)) {
549 LAST_ADD_TIME_UPDATE(vq);
551 BUG_ON(total_sg == 0);
553 head = vq->free_head;
555 if (virtqueue_use_indirect(vq, total_sg))
556 desc = alloc_indirect_split(_vq, total_sg, gfp);
559 WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
563 /* Use a single buffer which doesn't continue */
565 /* Set up rest to use this indirect table. */
570 desc = vq->split.vring.desc;
572 descs_used = total_sg;
575 if (unlikely(vq->vq.num_free < descs_used)) {
576 pr_debug("Can't add buf len %i - avail = %i\n",
577 descs_used, vq->vq.num_free);
578 /* FIXME: for historical reasons, we force a notify here if
579 * there are outgoing parts to the buffer. Presumably the
580 * host should service the ring ASAP. */
589 for (n = 0; n < out_sgs; n++) {
590 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
591 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
592 if (vring_mapping_error(vq, addr))
596 /* Note that we trust indirect descriptor
597 * table since it use stream DMA mapping.
599 i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length,
604 for (; n < (out_sgs + in_sgs); n++) {
605 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
606 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
607 if (vring_mapping_error(vq, addr))
611 /* Note that we trust indirect descriptor
612 * table since it use stream DMA mapping.
614 i = virtqueue_add_desc_split(_vq, desc, i, addr,
621 /* Last one doesn't continue. */
622 desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
623 if (!indirect && vq->use_dma_api)
624 vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
628 /* Now that the indirect table is filled in, map it. */
629 dma_addr_t addr = vring_map_single(
630 vq, desc, total_sg * sizeof(struct vring_desc),
632 if (vring_mapping_error(vq, addr))
635 virtqueue_add_desc_split(_vq, vq->split.vring.desc,
637 total_sg * sizeof(struct vring_desc),
638 VRING_DESC_F_INDIRECT,
642 /* We're using some buffers from the free list. */
643 vq->vq.num_free -= descs_used;
645 /* Update free pointer */
647 vq->free_head = vq->split.desc_extra[head].next;
651 /* Store token and indirect buffer state. */
652 vq->split.desc_state[head].data = data;
654 vq->split.desc_state[head].indir_desc = desc;
656 vq->split.desc_state[head].indir_desc = ctx;
658 /* Put entry in available array (but don't update avail->idx until they
660 avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
661 vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
663 /* Descriptors and available array need to be set before we expose the
664 * new available array entries. */
665 virtio_wmb(vq->weak_barriers);
666 vq->split.avail_idx_shadow++;
667 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
668 vq->split.avail_idx_shadow);
671 pr_debug("Added buffer head %i to %p\n", head, vq);
674 /* This is very unlikely, but theoretically possible. Kick
676 if (unlikely(vq->num_added == (1 << 16) - 1))
689 for (n = 0; n < total_sg; n++) {
693 vring_unmap_one_split_indirect(vq, &desc[i]);
694 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
696 i = vring_unmap_one_split(vq, i);
706 static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
708 struct vring_virtqueue *vq = to_vvq(_vq);
713 /* We need to expose available array entries before checking avail
715 virtio_mb(vq->weak_barriers);
717 old = vq->split.avail_idx_shadow - vq->num_added;
718 new = vq->split.avail_idx_shadow;
721 LAST_ADD_TIME_CHECK(vq);
722 LAST_ADD_TIME_INVALID(vq);
725 needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
726 vring_avail_event(&vq->split.vring)),
729 needs_kick = !(vq->split.vring.used->flags &
730 cpu_to_virtio16(_vq->vdev,
731 VRING_USED_F_NO_NOTIFY));
737 static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
741 __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
743 /* Clear data ptr. */
744 vq->split.desc_state[head].data = NULL;
746 /* Put back on free list: unmap first-level descriptors and find end */
749 while (vq->split.vring.desc[i].flags & nextflag) {
750 vring_unmap_one_split(vq, i);
751 i = vq->split.desc_extra[i].next;
755 vring_unmap_one_split(vq, i);
756 vq->split.desc_extra[i].next = vq->free_head;
757 vq->free_head = head;
759 /* Plus final descriptor */
763 struct vring_desc *indir_desc =
764 vq->split.desc_state[head].indir_desc;
767 /* Free the indirect table, if any, now that it's unmapped. */
771 len = vq->split.desc_extra[head].len;
773 BUG_ON(!(vq->split.desc_extra[head].flags &
774 VRING_DESC_F_INDIRECT));
775 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
777 for (j = 0; j < len / sizeof(struct vring_desc); j++)
778 vring_unmap_one_split_indirect(vq, &indir_desc[j]);
781 vq->split.desc_state[head].indir_desc = NULL;
783 *ctx = vq->split.desc_state[head].indir_desc;
787 static inline bool more_used_split(const struct vring_virtqueue *vq)
789 return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
790 vq->split.vring.used->idx);
793 static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
797 struct vring_virtqueue *vq = to_vvq(_vq);
804 if (unlikely(vq->broken)) {
809 if (!more_used_split(vq)) {
810 pr_debug("No more buffers in queue\n");
815 /* Only get used array entries after they have been exposed by host. */
816 virtio_rmb(vq->weak_barriers);
818 last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
819 i = virtio32_to_cpu(_vq->vdev,
820 vq->split.vring.used->ring[last_used].id);
821 *len = virtio32_to_cpu(_vq->vdev,
822 vq->split.vring.used->ring[last_used].len);
824 if (unlikely(i >= vq->split.vring.num)) {
825 BAD_RING(vq, "id %u out of range\n", i);
828 if (unlikely(!vq->split.desc_state[i].data)) {
829 BAD_RING(vq, "id %u is not a head!\n", i);
833 /* detach_buf_split clears data, so grab it now. */
834 ret = vq->split.desc_state[i].data;
835 detach_buf_split(vq, i, ctx);
837 /* If we expect an interrupt for the next entry, tell host
838 * by writing event index and flush out the write before
839 * the read in the next get_buf call. */
840 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
841 virtio_store_mb(vq->weak_barriers,
842 &vring_used_event(&vq->split.vring),
843 cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
845 LAST_ADD_TIME_INVALID(vq);
851 static void virtqueue_disable_cb_split(struct virtqueue *_vq)
853 struct vring_virtqueue *vq = to_vvq(_vq);
855 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
856 vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
858 /* TODO: this is a hack. Figure out a cleaner value to write. */
859 vring_used_event(&vq->split.vring) = 0x0;
861 vq->split.vring.avail->flags =
862 cpu_to_virtio16(_vq->vdev,
863 vq->split.avail_flags_shadow);
867 static unsigned int virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
869 struct vring_virtqueue *vq = to_vvq(_vq);
874 /* We optimistically turn back on interrupts, then check if there was
876 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
877 * either clear the flags bit or point the event index at the next
878 * entry. Always do both to keep code simple. */
879 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
880 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
882 vq->split.vring.avail->flags =
883 cpu_to_virtio16(_vq->vdev,
884 vq->split.avail_flags_shadow);
886 vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
887 last_used_idx = vq->last_used_idx);
889 return last_used_idx;
892 static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned int last_used_idx)
894 struct vring_virtqueue *vq = to_vvq(_vq);
896 return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
897 vq->split.vring.used->idx);
900 static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
902 struct vring_virtqueue *vq = to_vvq(_vq);
907 /* We optimistically turn back on interrupts, then check if there was
909 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
910 * either clear the flags bit or point the event index at the next
911 * entry. Always update the event index to keep code simple. */
912 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
913 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
915 vq->split.vring.avail->flags =
916 cpu_to_virtio16(_vq->vdev,
917 vq->split.avail_flags_shadow);
919 /* TODO: tune this threshold */
920 bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
922 virtio_store_mb(vq->weak_barriers,
923 &vring_used_event(&vq->split.vring),
924 cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
926 if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
927 - vq->last_used_idx) > bufs)) {
936 static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
938 struct vring_virtqueue *vq = to_vvq(_vq);
944 for (i = 0; i < vq->split.vring.num; i++) {
945 if (!vq->split.desc_state[i].data)
947 /* detach_buf_split clears data, so grab it now. */
948 buf = vq->split.desc_state[i].data;
949 detach_buf_split(vq, i, NULL);
950 vq->split.avail_idx_shadow--;
951 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
952 vq->split.avail_idx_shadow);
956 /* That should have freed everything. */
957 BUG_ON(vq->vq.num_free != vq->split.vring.num);
963 static void virtqueue_vring_init_split(struct vring_virtqueue_split *vring_split,
964 struct vring_virtqueue *vq)
966 struct virtio_device *vdev;
970 vring_split->avail_flags_shadow = 0;
971 vring_split->avail_idx_shadow = 0;
973 /* No callback? Tell other side not to bother us. */
974 if (!vq->vq.callback) {
975 vring_split->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
977 vring_split->vring.avail->flags = cpu_to_virtio16(vdev,
978 vring_split->avail_flags_shadow);
982 static void virtqueue_reinit_split(struct vring_virtqueue *vq)
986 num = vq->split.vring.num;
988 vq->split.vring.avail->flags = 0;
989 vq->split.vring.avail->idx = 0;
991 /* reset avail event */
992 vq->split.vring.avail->ring[num] = 0;
994 vq->split.vring.used->flags = 0;
995 vq->split.vring.used->idx = 0;
997 /* reset used event */
998 *(__virtio16 *)&(vq->split.vring.used->ring[num]) = 0;
1000 virtqueue_init(vq, num);
1002 virtqueue_vring_init_split(&vq->split, vq);
1005 static void virtqueue_vring_attach_split(struct vring_virtqueue *vq,
1006 struct vring_virtqueue_split *vring_split)
1008 vq->split = *vring_split;
1010 /* Put everything in free lists. */
1014 static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vring_split)
1016 struct vring_desc_state_split *state;
1017 struct vring_desc_extra *extra;
1018 u32 num = vring_split->vring.num;
1020 state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL);
1024 extra = vring_alloc_desc_extra(num);
1028 memset(state, 0, num * sizeof(struct vring_desc_state_split));
1030 vring_split->desc_state = state;
1031 vring_split->desc_extra = extra;
1040 static void vring_free_split(struct vring_virtqueue_split *vring_split,
1041 struct virtio_device *vdev, struct device *dma_dev)
1043 vring_free_queue(vdev, vring_split->queue_size_in_bytes,
1044 vring_split->vring.desc,
1045 vring_split->queue_dma_addr,
1048 kfree(vring_split->desc_state);
1049 kfree(vring_split->desc_extra);
1052 static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
1053 struct virtio_device *vdev,
1055 unsigned int vring_align,
1056 bool may_reduce_num,
1057 struct device *dma_dev)
1060 dma_addr_t dma_addr;
1062 /* We assume num is a power of 2. */
1063 if (!is_power_of_2(num)) {
1064 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
1068 /* TODO: allocate each queue chunk individually */
1069 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
1070 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1072 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1076 if (!may_reduce_num)
1084 /* Try to get a single page. You are my only hope! */
1085 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
1086 &dma_addr, GFP_KERNEL | __GFP_ZERO,
1092 vring_init(&vring_split->vring, num, queue, vring_align);
1094 vring_split->queue_dma_addr = dma_addr;
1095 vring_split->queue_size_in_bytes = vring_size(num, vring_align);
1097 vring_split->vring_align = vring_align;
1098 vring_split->may_reduce_num = may_reduce_num;
1103 static struct virtqueue *vring_create_virtqueue_split(
1106 unsigned int vring_align,
1107 struct virtio_device *vdev,
1109 bool may_reduce_num,
1111 bool (*notify)(struct virtqueue *),
1112 void (*callback)(struct virtqueue *),
1114 struct device *dma_dev)
1116 struct vring_virtqueue_split vring_split = {};
1117 struct virtqueue *vq;
1120 err = vring_alloc_queue_split(&vring_split, vdev, num, vring_align,
1121 may_reduce_num, dma_dev);
1125 vq = __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
1126 context, notify, callback, name, dma_dev);
1128 vring_free_split(&vring_split, vdev, dma_dev);
1132 to_vvq(vq)->we_own_ring = true;
1137 static int virtqueue_resize_split(struct virtqueue *_vq, u32 num)
1139 struct vring_virtqueue_split vring_split = {};
1140 struct vring_virtqueue *vq = to_vvq(_vq);
1141 struct virtio_device *vdev = _vq->vdev;
1144 err = vring_alloc_queue_split(&vring_split, vdev, num,
1145 vq->split.vring_align,
1146 vq->split.may_reduce_num,
1151 err = vring_alloc_state_extra_split(&vring_split);
1153 goto err_state_extra;
1155 vring_free(&vq->vq);
1157 virtqueue_vring_init_split(&vring_split, vq);
1159 virtqueue_init(vq, vring_split.vring.num);
1160 virtqueue_vring_attach_split(vq, &vring_split);
1165 vring_free_split(&vring_split, vdev, vring_dma_dev(vq));
1167 virtqueue_reinit_split(vq);
1173 * Packed ring specific functions - *_packed().
1175 static inline bool packed_used_wrap_counter(u16 last_used_idx)
1177 return !!(last_used_idx & (1 << VRING_PACKED_EVENT_F_WRAP_CTR));
1180 static inline u16 packed_last_used(u16 last_used_idx)
1182 return last_used_idx & ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR));
1185 static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
1186 struct vring_desc_extra *extra)
1190 if (!vq->use_dma_api)
1193 flags = extra->flags;
1195 if (flags & VRING_DESC_F_INDIRECT) {
1196 dma_unmap_single(vring_dma_dev(vq),
1197 extra->addr, extra->len,
1198 (flags & VRING_DESC_F_WRITE) ?
1199 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1201 dma_unmap_page(vring_dma_dev(vq),
1202 extra->addr, extra->len,
1203 (flags & VRING_DESC_F_WRITE) ?
1204 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1208 static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
1209 struct vring_packed_desc *desc)
1213 if (!vq->use_dma_api)
1216 flags = le16_to_cpu(desc->flags);
1218 dma_unmap_page(vring_dma_dev(vq),
1219 le64_to_cpu(desc->addr),
1220 le32_to_cpu(desc->len),
1221 (flags & VRING_DESC_F_WRITE) ?
1222 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1225 static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
1228 struct vring_packed_desc *desc;
1231 * We require lowmem mappings for the descriptors because
1232 * otherwise virt_to_phys will give us bogus addresses in the
1235 gfp &= ~__GFP_HIGHMEM;
1237 desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
1242 static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
1243 struct scatterlist *sgs[],
1244 unsigned int total_sg,
1245 unsigned int out_sgs,
1246 unsigned int in_sgs,
1250 struct vring_packed_desc *desc;
1251 struct scatterlist *sg;
1252 unsigned int i, n, err_idx;
1256 head = vq->packed.next_avail_idx;
1257 desc = alloc_indirect_packed(total_sg, gfp);
1261 if (unlikely(vq->vq.num_free < 1)) {
1262 pr_debug("Can't add buf len 1 - avail = 0\n");
1270 BUG_ON(id == vq->packed.vring.num);
1272 for (n = 0; n < out_sgs + in_sgs; n++) {
1273 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1274 addr = vring_map_one_sg(vq, sg, n < out_sgs ?
1275 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1276 if (vring_mapping_error(vq, addr))
1279 desc[i].flags = cpu_to_le16(n < out_sgs ?
1280 0 : VRING_DESC_F_WRITE);
1281 desc[i].addr = cpu_to_le64(addr);
1282 desc[i].len = cpu_to_le32(sg->length);
1287 /* Now that the indirect table is filled in, map it. */
1288 addr = vring_map_single(vq, desc,
1289 total_sg * sizeof(struct vring_packed_desc),
1291 if (vring_mapping_error(vq, addr))
1294 vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
1295 vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
1296 sizeof(struct vring_packed_desc));
1297 vq->packed.vring.desc[head].id = cpu_to_le16(id);
1299 if (vq->use_dma_api) {
1300 vq->packed.desc_extra[id].addr = addr;
1301 vq->packed.desc_extra[id].len = total_sg *
1302 sizeof(struct vring_packed_desc);
1303 vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
1304 vq->packed.avail_used_flags;
1308 * A driver MUST NOT make the first descriptor in the list
1309 * available before all subsequent descriptors comprising
1310 * the list are made available.
1312 virtio_wmb(vq->weak_barriers);
1313 vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
1314 vq->packed.avail_used_flags);
1316 /* We're using some buffers from the free list. */
1317 vq->vq.num_free -= 1;
1319 /* Update free pointer */
1321 if (n >= vq->packed.vring.num) {
1323 vq->packed.avail_wrap_counter ^= 1;
1324 vq->packed.avail_used_flags ^=
1325 1 << VRING_PACKED_DESC_F_AVAIL |
1326 1 << VRING_PACKED_DESC_F_USED;
1328 vq->packed.next_avail_idx = n;
1329 vq->free_head = vq->packed.desc_extra[id].next;
1331 /* Store token and indirect buffer state. */
1332 vq->packed.desc_state[id].num = 1;
1333 vq->packed.desc_state[id].data = data;
1334 vq->packed.desc_state[id].indir_desc = desc;
1335 vq->packed.desc_state[id].last = id;
1339 pr_debug("Added buffer head %i to %p\n", head, vq);
1347 for (i = 0; i < err_idx; i++)
1348 vring_unmap_desc_packed(vq, &desc[i]);
1356 static inline int virtqueue_add_packed(struct virtqueue *_vq,
1357 struct scatterlist *sgs[],
1358 unsigned int total_sg,
1359 unsigned int out_sgs,
1360 unsigned int in_sgs,
1365 struct vring_virtqueue *vq = to_vvq(_vq);
1366 struct vring_packed_desc *desc;
1367 struct scatterlist *sg;
1368 unsigned int i, n, c, descs_used, err_idx;
1369 __le16 head_flags, flags;
1370 u16 head, id, prev, curr, avail_used_flags;
1375 BUG_ON(data == NULL);
1376 BUG_ON(ctx && vq->indirect);
1378 if (unlikely(vq->broken)) {
1383 LAST_ADD_TIME_UPDATE(vq);
1385 BUG_ON(total_sg == 0);
1387 if (virtqueue_use_indirect(vq, total_sg)) {
1388 err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
1390 if (err != -ENOMEM) {
1395 /* fall back on direct */
1398 head = vq->packed.next_avail_idx;
1399 avail_used_flags = vq->packed.avail_used_flags;
1401 WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
1403 desc = vq->packed.vring.desc;
1405 descs_used = total_sg;
1407 if (unlikely(vq->vq.num_free < descs_used)) {
1408 pr_debug("Can't add buf len %i - avail = %i\n",
1409 descs_used, vq->vq.num_free);
1415 BUG_ON(id == vq->packed.vring.num);
1419 for (n = 0; n < out_sgs + in_sgs; n++) {
1420 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1421 dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ?
1422 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1423 if (vring_mapping_error(vq, addr))
1426 flags = cpu_to_le16(vq->packed.avail_used_flags |
1427 (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
1428 (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
1432 desc[i].flags = flags;
1434 desc[i].addr = cpu_to_le64(addr);
1435 desc[i].len = cpu_to_le32(sg->length);
1436 desc[i].id = cpu_to_le16(id);
1438 if (unlikely(vq->use_dma_api)) {
1439 vq->packed.desc_extra[curr].addr = addr;
1440 vq->packed.desc_extra[curr].len = sg->length;
1441 vq->packed.desc_extra[curr].flags =
1445 curr = vq->packed.desc_extra[curr].next;
1447 if ((unlikely(++i >= vq->packed.vring.num))) {
1449 vq->packed.avail_used_flags ^=
1450 1 << VRING_PACKED_DESC_F_AVAIL |
1451 1 << VRING_PACKED_DESC_F_USED;
1457 vq->packed.avail_wrap_counter ^= 1;
1459 /* We're using some buffers from the free list. */
1460 vq->vq.num_free -= descs_used;
1462 /* Update free pointer */
1463 vq->packed.next_avail_idx = i;
1464 vq->free_head = curr;
1467 vq->packed.desc_state[id].num = descs_used;
1468 vq->packed.desc_state[id].data = data;
1469 vq->packed.desc_state[id].indir_desc = ctx;
1470 vq->packed.desc_state[id].last = prev;
1473 * A driver MUST NOT make the first descriptor in the list
1474 * available before all subsequent descriptors comprising
1475 * the list are made available.
1477 virtio_wmb(vq->weak_barriers);
1478 vq->packed.vring.desc[head].flags = head_flags;
1479 vq->num_added += descs_used;
1481 pr_debug("Added buffer head %i to %p\n", head, vq);
1489 curr = vq->free_head;
1491 vq->packed.avail_used_flags = avail_used_flags;
1493 for (n = 0; n < total_sg; n++) {
1496 vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]);
1497 curr = vq->packed.desc_extra[curr].next;
1499 if (i >= vq->packed.vring.num)
1507 static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
1509 struct vring_virtqueue *vq = to_vvq(_vq);
1510 u16 new, old, off_wrap, flags, wrap_counter, event_idx;
1523 * We need to expose the new flags value before checking notification
1526 virtio_mb(vq->weak_barriers);
1528 old = vq->packed.next_avail_idx - vq->num_added;
1529 new = vq->packed.next_avail_idx;
1532 snapshot.u32 = *(u32 *)vq->packed.vring.device;
1533 flags = le16_to_cpu(snapshot.flags);
1535 LAST_ADD_TIME_CHECK(vq);
1536 LAST_ADD_TIME_INVALID(vq);
1538 if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
1539 needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1543 off_wrap = le16_to_cpu(snapshot.off_wrap);
1545 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1546 event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1547 if (wrap_counter != vq->packed.avail_wrap_counter)
1548 event_idx -= vq->packed.vring.num;
1550 needs_kick = vring_need_event(event_idx, new, old);
1556 static void detach_buf_packed(struct vring_virtqueue *vq,
1557 unsigned int id, void **ctx)
1559 struct vring_desc_state_packed *state = NULL;
1560 struct vring_packed_desc *desc;
1561 unsigned int i, curr;
1563 state = &vq->packed.desc_state[id];
1565 /* Clear data ptr. */
1568 vq->packed.desc_extra[state->last].next = vq->free_head;
1570 vq->vq.num_free += state->num;
1572 if (unlikely(vq->use_dma_api)) {
1574 for (i = 0; i < state->num; i++) {
1575 vring_unmap_extra_packed(vq,
1576 &vq->packed.desc_extra[curr]);
1577 curr = vq->packed.desc_extra[curr].next;
1584 /* Free the indirect table, if any, now that it's unmapped. */
1585 desc = state->indir_desc;
1589 if (vq->use_dma_api) {
1590 len = vq->packed.desc_extra[id].len;
1591 for (i = 0; i < len / sizeof(struct vring_packed_desc);
1593 vring_unmap_desc_packed(vq, &desc[i]);
1596 state->indir_desc = NULL;
1598 *ctx = state->indir_desc;
1602 static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
1603 u16 idx, bool used_wrap_counter)
1608 flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
1609 avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
1610 used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
1612 return avail == used && used == used_wrap_counter;
1615 static inline bool more_used_packed(const struct vring_virtqueue *vq)
1619 bool used_wrap_counter;
1621 last_used_idx = READ_ONCE(vq->last_used_idx);
1622 last_used = packed_last_used(last_used_idx);
1623 used_wrap_counter = packed_used_wrap_counter(last_used_idx);
1624 return is_used_desc_packed(vq, last_used, used_wrap_counter);
1627 static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
1631 struct vring_virtqueue *vq = to_vvq(_vq);
1632 u16 last_used, id, last_used_idx;
1633 bool used_wrap_counter;
1638 if (unlikely(vq->broken)) {
1643 if (!more_used_packed(vq)) {
1644 pr_debug("No more buffers in queue\n");
1649 /* Only get used elements after they have been exposed by host. */
1650 virtio_rmb(vq->weak_barriers);
1652 last_used_idx = READ_ONCE(vq->last_used_idx);
1653 used_wrap_counter = packed_used_wrap_counter(last_used_idx);
1654 last_used = packed_last_used(last_used_idx);
1655 id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
1656 *len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
1658 if (unlikely(id >= vq->packed.vring.num)) {
1659 BAD_RING(vq, "id %u out of range\n", id);
1662 if (unlikely(!vq->packed.desc_state[id].data)) {
1663 BAD_RING(vq, "id %u is not a head!\n", id);
1667 /* detach_buf_packed clears data, so grab it now. */
1668 ret = vq->packed.desc_state[id].data;
1669 detach_buf_packed(vq, id, ctx);
1671 last_used += vq->packed.desc_state[id].num;
1672 if (unlikely(last_used >= vq->packed.vring.num)) {
1673 last_used -= vq->packed.vring.num;
1674 used_wrap_counter ^= 1;
1677 last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1678 WRITE_ONCE(vq->last_used_idx, last_used);
1681 * If we expect an interrupt for the next entry, tell host
1682 * by writing event index and flush out the write before
1683 * the read in the next get_buf call.
1685 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1686 virtio_store_mb(vq->weak_barriers,
1687 &vq->packed.vring.driver->off_wrap,
1688 cpu_to_le16(vq->last_used_idx));
1690 LAST_ADD_TIME_INVALID(vq);
1696 static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
1698 struct vring_virtqueue *vq = to_vvq(_vq);
1700 if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
1701 vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1702 vq->packed.vring.driver->flags =
1703 cpu_to_le16(vq->packed.event_flags_shadow);
1707 static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
1709 struct vring_virtqueue *vq = to_vvq(_vq);
1714 * We optimistically turn back on interrupts, then check if there was
1719 vq->packed.vring.driver->off_wrap =
1720 cpu_to_le16(vq->last_used_idx);
1722 * We need to update event offset and event wrap
1723 * counter first before updating event flags.
1725 virtio_wmb(vq->weak_barriers);
1728 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1729 vq->packed.event_flags_shadow = vq->event ?
1730 VRING_PACKED_EVENT_FLAG_DESC :
1731 VRING_PACKED_EVENT_FLAG_ENABLE;
1732 vq->packed.vring.driver->flags =
1733 cpu_to_le16(vq->packed.event_flags_shadow);
1737 return vq->last_used_idx;
1740 static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
1742 struct vring_virtqueue *vq = to_vvq(_vq);
1746 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1747 used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1749 return is_used_desc_packed(vq, used_idx, wrap_counter);
1752 static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
1754 struct vring_virtqueue *vq = to_vvq(_vq);
1755 u16 used_idx, wrap_counter, last_used_idx;
1761 * We optimistically turn back on interrupts, then check if there was
1766 /* TODO: tune this threshold */
1767 bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
1768 last_used_idx = READ_ONCE(vq->last_used_idx);
1769 wrap_counter = packed_used_wrap_counter(last_used_idx);
1771 used_idx = packed_last_used(last_used_idx) + bufs;
1772 if (used_idx >= vq->packed.vring.num) {
1773 used_idx -= vq->packed.vring.num;
1777 vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1778 (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1781 * We need to update event offset and event wrap
1782 * counter first before updating event flags.
1784 virtio_wmb(vq->weak_barriers);
1787 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1788 vq->packed.event_flags_shadow = vq->event ?
1789 VRING_PACKED_EVENT_FLAG_DESC :
1790 VRING_PACKED_EVENT_FLAG_ENABLE;
1791 vq->packed.vring.driver->flags =
1792 cpu_to_le16(vq->packed.event_flags_shadow);
1796 * We need to update event suppression structure first
1797 * before re-checking for more used buffers.
1799 virtio_mb(vq->weak_barriers);
1801 last_used_idx = READ_ONCE(vq->last_used_idx);
1802 wrap_counter = packed_used_wrap_counter(last_used_idx);
1803 used_idx = packed_last_used(last_used_idx);
1804 if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
1813 static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
1815 struct vring_virtqueue *vq = to_vvq(_vq);
1821 for (i = 0; i < vq->packed.vring.num; i++) {
1822 if (!vq->packed.desc_state[i].data)
1824 /* detach_buf clears data, so grab it now. */
1825 buf = vq->packed.desc_state[i].data;
1826 detach_buf_packed(vq, i, NULL);
1830 /* That should have freed everything. */
1831 BUG_ON(vq->vq.num_free != vq->packed.vring.num);
1837 static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num)
1839 struct vring_desc_extra *desc_extra;
1842 desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra),
1847 memset(desc_extra, 0, num * sizeof(struct vring_desc_extra));
1849 for (i = 0; i < num - 1; i++)
1850 desc_extra[i].next = i + 1;
1855 static void vring_free_packed(struct vring_virtqueue_packed *vring_packed,
1856 struct virtio_device *vdev,
1857 struct device *dma_dev)
1859 if (vring_packed->vring.desc)
1860 vring_free_queue(vdev, vring_packed->ring_size_in_bytes,
1861 vring_packed->vring.desc,
1862 vring_packed->ring_dma_addr,
1865 if (vring_packed->vring.driver)
1866 vring_free_queue(vdev, vring_packed->event_size_in_bytes,
1867 vring_packed->vring.driver,
1868 vring_packed->driver_event_dma_addr,
1871 if (vring_packed->vring.device)
1872 vring_free_queue(vdev, vring_packed->event_size_in_bytes,
1873 vring_packed->vring.device,
1874 vring_packed->device_event_dma_addr,
1877 kfree(vring_packed->desc_state);
1878 kfree(vring_packed->desc_extra);
1881 static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed,
1882 struct virtio_device *vdev,
1883 u32 num, struct device *dma_dev)
1885 struct vring_packed_desc *ring;
1886 struct vring_packed_desc_event *driver, *device;
1887 dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
1888 size_t ring_size_in_bytes, event_size_in_bytes;
1890 ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
1892 ring = vring_alloc_queue(vdev, ring_size_in_bytes,
1894 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1899 vring_packed->vring.desc = ring;
1900 vring_packed->ring_dma_addr = ring_dma_addr;
1901 vring_packed->ring_size_in_bytes = ring_size_in_bytes;
1903 event_size_in_bytes = sizeof(struct vring_packed_desc_event);
1905 driver = vring_alloc_queue(vdev, event_size_in_bytes,
1906 &driver_event_dma_addr,
1907 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1912 vring_packed->vring.driver = driver;
1913 vring_packed->event_size_in_bytes = event_size_in_bytes;
1914 vring_packed->driver_event_dma_addr = driver_event_dma_addr;
1916 device = vring_alloc_queue(vdev, event_size_in_bytes,
1917 &device_event_dma_addr,
1918 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1923 vring_packed->vring.device = device;
1924 vring_packed->device_event_dma_addr = device_event_dma_addr;
1926 vring_packed->vring.num = num;
1931 vring_free_packed(vring_packed, vdev, dma_dev);
1935 static int vring_alloc_state_extra_packed(struct vring_virtqueue_packed *vring_packed)
1937 struct vring_desc_state_packed *state;
1938 struct vring_desc_extra *extra;
1939 u32 num = vring_packed->vring.num;
1941 state = kmalloc_array(num, sizeof(struct vring_desc_state_packed), GFP_KERNEL);
1943 goto err_desc_state;
1945 memset(state, 0, num * sizeof(struct vring_desc_state_packed));
1947 extra = vring_alloc_desc_extra(num);
1949 goto err_desc_extra;
1951 vring_packed->desc_state = state;
1952 vring_packed->desc_extra = extra;
1962 static void virtqueue_vring_init_packed(struct vring_virtqueue_packed *vring_packed,
1965 vring_packed->next_avail_idx = 0;
1966 vring_packed->avail_wrap_counter = 1;
1967 vring_packed->event_flags_shadow = 0;
1968 vring_packed->avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
1970 /* No callback? Tell other side not to bother us. */
1972 vring_packed->event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1973 vring_packed->vring.driver->flags =
1974 cpu_to_le16(vring_packed->event_flags_shadow);
1978 static void virtqueue_vring_attach_packed(struct vring_virtqueue *vq,
1979 struct vring_virtqueue_packed *vring_packed)
1981 vq->packed = *vring_packed;
1983 /* Put everything in free lists. */
1987 static void virtqueue_reinit_packed(struct vring_virtqueue *vq)
1989 memset(vq->packed.vring.device, 0, vq->packed.event_size_in_bytes);
1990 memset(vq->packed.vring.driver, 0, vq->packed.event_size_in_bytes);
1992 /* we need to reset the desc.flags. For more, see is_used_desc_packed() */
1993 memset(vq->packed.vring.desc, 0, vq->packed.ring_size_in_bytes);
1995 virtqueue_init(vq, vq->packed.vring.num);
1996 virtqueue_vring_init_packed(&vq->packed, !!vq->vq.callback);
1999 static struct virtqueue *vring_create_virtqueue_packed(
2002 unsigned int vring_align,
2003 struct virtio_device *vdev,
2005 bool may_reduce_num,
2007 bool (*notify)(struct virtqueue *),
2008 void (*callback)(struct virtqueue *),
2010 struct device *dma_dev)
2012 struct vring_virtqueue_packed vring_packed = {};
2013 struct vring_virtqueue *vq;
2016 if (vring_alloc_queue_packed(&vring_packed, vdev, num, dma_dev))
2019 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
2023 vq->vq.callback = callback;
2026 vq->vq.index = index;
2027 vq->vq.reset = false;
2028 vq->we_own_ring = true;
2029 vq->notify = notify;
2030 vq->weak_barriers = weak_barriers;
2031 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
2036 vq->packed_ring = true;
2037 vq->dma_dev = dma_dev;
2038 vq->use_dma_api = vring_use_dma_api(vdev);
2040 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
2042 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
2044 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
2045 vq->weak_barriers = false;
2047 err = vring_alloc_state_extra_packed(&vring_packed);
2049 goto err_state_extra;
2051 virtqueue_vring_init_packed(&vring_packed, !!callback);
2053 virtqueue_init(vq, num);
2054 virtqueue_vring_attach_packed(vq, &vring_packed);
2056 spin_lock(&vdev->vqs_list_lock);
2057 list_add_tail(&vq->vq.list, &vdev->vqs);
2058 spin_unlock(&vdev->vqs_list_lock);
2064 vring_free_packed(&vring_packed, vdev, dma_dev);
2069 static int virtqueue_resize_packed(struct virtqueue *_vq, u32 num)
2071 struct vring_virtqueue_packed vring_packed = {};
2072 struct vring_virtqueue *vq = to_vvq(_vq);
2073 struct virtio_device *vdev = _vq->vdev;
2076 if (vring_alloc_queue_packed(&vring_packed, vdev, num, vring_dma_dev(vq)))
2079 err = vring_alloc_state_extra_packed(&vring_packed);
2081 goto err_state_extra;
2083 vring_free(&vq->vq);
2085 virtqueue_vring_init_packed(&vring_packed, !!vq->vq.callback);
2087 virtqueue_init(vq, vring_packed.vring.num);
2088 virtqueue_vring_attach_packed(vq, &vring_packed);
2093 vring_free_packed(&vring_packed, vdev, vring_dma_dev(vq));
2095 virtqueue_reinit_packed(vq);
2101 * Generic functions and exported symbols.
2104 static inline int virtqueue_add(struct virtqueue *_vq,
2105 struct scatterlist *sgs[],
2106 unsigned int total_sg,
2107 unsigned int out_sgs,
2108 unsigned int in_sgs,
2113 struct vring_virtqueue *vq = to_vvq(_vq);
2115 return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
2116 out_sgs, in_sgs, data, ctx, gfp) :
2117 virtqueue_add_split(_vq, sgs, total_sg,
2118 out_sgs, in_sgs, data, ctx, gfp);
2122 * virtqueue_add_sgs - expose buffers to other end
2123 * @_vq: the struct virtqueue we're talking about.
2124 * @sgs: array of terminated scatterlists.
2125 * @out_sgs: the number of scatterlists readable by other side
2126 * @in_sgs: the number of scatterlists which are writable (after readable ones)
2127 * @data: the token identifying the buffer.
2128 * @gfp: how to do memory allocations (if necessary).
2130 * Caller must ensure we don't call this with other virtqueue operations
2131 * at the same time (except where noted).
2133 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2135 int virtqueue_add_sgs(struct virtqueue *_vq,
2136 struct scatterlist *sgs[],
2137 unsigned int out_sgs,
2138 unsigned int in_sgs,
2142 unsigned int i, total_sg = 0;
2144 /* Count them first. */
2145 for (i = 0; i < out_sgs + in_sgs; i++) {
2146 struct scatterlist *sg;
2148 for (sg = sgs[i]; sg; sg = sg_next(sg))
2151 return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
2154 EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
2157 * virtqueue_add_outbuf - expose output buffers to other end
2158 * @vq: the struct virtqueue we're talking about.
2159 * @sg: scatterlist (must be well-formed and terminated!)
2160 * @num: the number of entries in @sg readable by other side
2161 * @data: the token identifying the buffer.
2162 * @gfp: how to do memory allocations (if necessary).
2164 * Caller must ensure we don't call this with other virtqueue operations
2165 * at the same time (except where noted).
2167 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2169 int virtqueue_add_outbuf(struct virtqueue *vq,
2170 struct scatterlist *sg, unsigned int num,
2174 return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
2176 EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
2179 * virtqueue_add_inbuf - expose input buffers to other end
2180 * @vq: the struct virtqueue we're talking about.
2181 * @sg: scatterlist (must be well-formed and terminated!)
2182 * @num: the number of entries in @sg writable by other side
2183 * @data: the token identifying the buffer.
2184 * @gfp: how to do memory allocations (if necessary).
2186 * Caller must ensure we don't call this with other virtqueue operations
2187 * at the same time (except where noted).
2189 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2191 int virtqueue_add_inbuf(struct virtqueue *vq,
2192 struct scatterlist *sg, unsigned int num,
2196 return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
2198 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
2201 * virtqueue_add_inbuf_ctx - expose input buffers to other end
2202 * @vq: the struct virtqueue we're talking about.
2203 * @sg: scatterlist (must be well-formed and terminated!)
2204 * @num: the number of entries in @sg writable by other side
2205 * @data: the token identifying the buffer.
2206 * @ctx: extra context for the token
2207 * @gfp: how to do memory allocations (if necessary).
2209 * Caller must ensure we don't call this with other virtqueue operations
2210 * at the same time (except where noted).
2212 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
2214 int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
2215 struct scatterlist *sg, unsigned int num,
2220 return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
2222 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
2225 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
2226 * @_vq: the struct virtqueue
2228 * Instead of virtqueue_kick(), you can do:
2229 * if (virtqueue_kick_prepare(vq))
2230 * virtqueue_notify(vq);
2232 * This is sometimes useful because the virtqueue_kick_prepare() needs
2233 * to be serialized, but the actual virtqueue_notify() call does not.
2235 bool virtqueue_kick_prepare(struct virtqueue *_vq)
2237 struct vring_virtqueue *vq = to_vvq(_vq);
2239 return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
2240 virtqueue_kick_prepare_split(_vq);
2242 EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
2245 * virtqueue_notify - second half of split virtqueue_kick call.
2246 * @_vq: the struct virtqueue
2248 * This does not need to be serialized.
2250 * Returns false if host notify failed or queue is broken, otherwise true.
2252 bool virtqueue_notify(struct virtqueue *_vq)
2254 struct vring_virtqueue *vq = to_vvq(_vq);
2256 if (unlikely(vq->broken))
2259 /* Prod other side to tell it about changes. */
2260 if (!vq->notify(_vq)) {
2266 EXPORT_SYMBOL_GPL(virtqueue_notify);
2269 * virtqueue_kick - update after add_buf
2270 * @vq: the struct virtqueue
2272 * After one or more virtqueue_add_* calls, invoke this to kick
2275 * Caller must ensure we don't call this with other virtqueue
2276 * operations at the same time (except where noted).
2278 * Returns false if kick failed, otherwise true.
2280 bool virtqueue_kick(struct virtqueue *vq)
2282 if (virtqueue_kick_prepare(vq))
2283 return virtqueue_notify(vq);
2286 EXPORT_SYMBOL_GPL(virtqueue_kick);
2289 * virtqueue_get_buf_ctx - get the next used buffer
2290 * @_vq: the struct virtqueue we're talking about.
2291 * @len: the length written into the buffer
2292 * @ctx: extra context for the token
2294 * If the device wrote data into the buffer, @len will be set to the
2295 * amount written. This means you don't need to clear the buffer
2296 * beforehand to ensure there's no data leakage in the case of short
2299 * Caller must ensure we don't call this with other virtqueue
2300 * operations at the same time (except where noted).
2302 * Returns NULL if there are no used buffers, or the "data" token
2303 * handed to virtqueue_add_*().
2305 void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
2308 struct vring_virtqueue *vq = to_vvq(_vq);
2310 return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
2311 virtqueue_get_buf_ctx_split(_vq, len, ctx);
2313 EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
2315 void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
2317 return virtqueue_get_buf_ctx(_vq, len, NULL);
2319 EXPORT_SYMBOL_GPL(virtqueue_get_buf);
2321 * virtqueue_disable_cb - disable callbacks
2322 * @_vq: the struct virtqueue we're talking about.
2324 * Note that this is not necessarily synchronous, hence unreliable and only
2325 * useful as an optimization.
2327 * Unlike other operations, this need not be serialized.
2329 void virtqueue_disable_cb(struct virtqueue *_vq)
2331 struct vring_virtqueue *vq = to_vvq(_vq);
2333 /* If device triggered an event already it won't trigger one again:
2334 * no need to disable.
2336 if (vq->event_triggered)
2339 if (vq->packed_ring)
2340 virtqueue_disable_cb_packed(_vq);
2342 virtqueue_disable_cb_split(_vq);
2344 EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
2347 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
2348 * @_vq: the struct virtqueue we're talking about.
2350 * This re-enables callbacks; it returns current queue state
2351 * in an opaque unsigned value. This value should be later tested by
2352 * virtqueue_poll, to detect a possible race between the driver checking for
2353 * more work, and enabling callbacks.
2355 * Caller must ensure we don't call this with other virtqueue
2356 * operations at the same time (except where noted).
2358 unsigned int virtqueue_enable_cb_prepare(struct virtqueue *_vq)
2360 struct vring_virtqueue *vq = to_vvq(_vq);
2362 if (vq->event_triggered)
2363 vq->event_triggered = false;
2365 return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
2366 virtqueue_enable_cb_prepare_split(_vq);
2368 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
2371 * virtqueue_poll - query pending used buffers
2372 * @_vq: the struct virtqueue we're talking about.
2373 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
2375 * Returns "true" if there are pending used buffers in the queue.
2377 * This does not need to be serialized.
2379 bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx)
2381 struct vring_virtqueue *vq = to_vvq(_vq);
2383 if (unlikely(vq->broken))
2386 virtio_mb(vq->weak_barriers);
2387 return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
2388 virtqueue_poll_split(_vq, last_used_idx);
2390 EXPORT_SYMBOL_GPL(virtqueue_poll);
2393 * virtqueue_enable_cb - restart callbacks after disable_cb.
2394 * @_vq: the struct virtqueue we're talking about.
2396 * This re-enables callbacks; it returns "false" if there are pending
2397 * buffers in the queue, to detect a possible race between the driver
2398 * checking for more work, and enabling callbacks.
2400 * Caller must ensure we don't call this with other virtqueue
2401 * operations at the same time (except where noted).
2403 bool virtqueue_enable_cb(struct virtqueue *_vq)
2405 unsigned int last_used_idx = virtqueue_enable_cb_prepare(_vq);
2407 return !virtqueue_poll(_vq, last_used_idx);
2409 EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
2412 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
2413 * @_vq: the struct virtqueue we're talking about.
2415 * This re-enables callbacks but hints to the other side to delay
2416 * interrupts until most of the available buffers have been processed;
2417 * it returns "false" if there are many pending buffers in the queue,
2418 * to detect a possible race between the driver checking for more work,
2419 * and enabling callbacks.
2421 * Caller must ensure we don't call this with other virtqueue
2422 * operations at the same time (except where noted).
2424 bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
2426 struct vring_virtqueue *vq = to_vvq(_vq);
2428 if (vq->event_triggered)
2429 vq->event_triggered = false;
2431 return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
2432 virtqueue_enable_cb_delayed_split(_vq);
2434 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2437 * virtqueue_detach_unused_buf - detach first unused buffer
2438 * @_vq: the struct virtqueue we're talking about.
2440 * Returns NULL or the "data" token handed to virtqueue_add_*().
2441 * This is not valid on an active queue; it is useful for device
2442 * shutdown or the reset queue.
2444 void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2446 struct vring_virtqueue *vq = to_vvq(_vq);
2448 return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
2449 virtqueue_detach_unused_buf_split(_vq);
2451 EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
2453 static inline bool more_used(const struct vring_virtqueue *vq)
2455 return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
2459 * vring_interrupt - notify a virtqueue on an interrupt
2460 * @irq: the IRQ number (ignored)
2461 * @_vq: the struct virtqueue to notify
2463 * Calls the callback function of @_vq to process the virtqueue
2466 irqreturn_t vring_interrupt(int irq, void *_vq)
2468 struct vring_virtqueue *vq = to_vvq(_vq);
2470 if (!more_used(vq)) {
2471 pr_debug("virtqueue interrupt with no work for %p\n", vq);
2475 if (unlikely(vq->broken)) {
2476 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
2477 dev_warn_once(&vq->vq.vdev->dev,
2478 "virtio vring IRQ raised before DRIVER_OK");
2485 /* Just a hint for performance: so it's ok that this can be racy! */
2487 vq->event_triggered = true;
2489 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
2490 if (vq->vq.callback)
2491 vq->vq.callback(&vq->vq);
2495 EXPORT_SYMBOL_GPL(vring_interrupt);
2497 /* Only available for split ring */
2498 static struct virtqueue *__vring_new_virtqueue(unsigned int index,
2499 struct vring_virtqueue_split *vring_split,
2500 struct virtio_device *vdev,
2503 bool (*notify)(struct virtqueue *),
2504 void (*callback)(struct virtqueue *),
2506 struct device *dma_dev)
2508 struct vring_virtqueue *vq;
2511 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2514 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
2518 vq->packed_ring = false;
2519 vq->vq.callback = callback;
2522 vq->vq.index = index;
2523 vq->vq.reset = false;
2524 vq->we_own_ring = false;
2525 vq->notify = notify;
2526 vq->weak_barriers = weak_barriers;
2527 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
2532 vq->dma_dev = dma_dev;
2533 vq->use_dma_api = vring_use_dma_api(vdev);
2535 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
2537 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
2539 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
2540 vq->weak_barriers = false;
2542 err = vring_alloc_state_extra_split(vring_split);
2548 virtqueue_vring_init_split(vring_split, vq);
2550 virtqueue_init(vq, vring_split->vring.num);
2551 virtqueue_vring_attach_split(vq, vring_split);
2553 spin_lock(&vdev->vqs_list_lock);
2554 list_add_tail(&vq->vq.list, &vdev->vqs);
2555 spin_unlock(&vdev->vqs_list_lock);
2559 struct virtqueue *vring_create_virtqueue(
2562 unsigned int vring_align,
2563 struct virtio_device *vdev,
2565 bool may_reduce_num,
2567 bool (*notify)(struct virtqueue *),
2568 void (*callback)(struct virtqueue *),
2572 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2573 return vring_create_virtqueue_packed(index, num, vring_align,
2574 vdev, weak_barriers, may_reduce_num,
2575 context, notify, callback, name, vdev->dev.parent);
2577 return vring_create_virtqueue_split(index, num, vring_align,
2578 vdev, weak_barriers, may_reduce_num,
2579 context, notify, callback, name, vdev->dev.parent);
2581 EXPORT_SYMBOL_GPL(vring_create_virtqueue);
2583 struct virtqueue *vring_create_virtqueue_dma(
2586 unsigned int vring_align,
2587 struct virtio_device *vdev,
2589 bool may_reduce_num,
2591 bool (*notify)(struct virtqueue *),
2592 void (*callback)(struct virtqueue *),
2594 struct device *dma_dev)
2597 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2598 return vring_create_virtqueue_packed(index, num, vring_align,
2599 vdev, weak_barriers, may_reduce_num,
2600 context, notify, callback, name, dma_dev);
2602 return vring_create_virtqueue_split(index, num, vring_align,
2603 vdev, weak_barriers, may_reduce_num,
2604 context, notify, callback, name, dma_dev);
2606 EXPORT_SYMBOL_GPL(vring_create_virtqueue_dma);
2609 * virtqueue_resize - resize the vring of vq
2610 * @_vq: the struct virtqueue we're talking about.
2611 * @num: new ring num
2612 * @recycle: callback for recycle the useless buffer
2614 * When it is really necessary to create a new vring, it will set the current vq
2615 * into the reset state. Then call the passed callback to recycle the buffer
2616 * that is no longer used. Only after the new vring is successfully created, the
2617 * old vring will be released.
2619 * Caller must ensure we don't call this with other virtqueue operations
2620 * at the same time (except where noted).
2622 * Returns zero or a negative error.
2624 * -ENOMEM: Failed to allocate a new ring, fall back to the original ring size.
2625 * vq can still work normally
2626 * -EBUSY: Failed to sync with device, vq may not work properly
2627 * -ENOENT: Transport or device not supported
2628 * -E2BIG/-EINVAL: num error
2629 * -EPERM: Operation not permitted
2632 int virtqueue_resize(struct virtqueue *_vq, u32 num,
2633 void (*recycle)(struct virtqueue *vq, void *buf))
2635 struct vring_virtqueue *vq = to_vvq(_vq);
2636 struct virtio_device *vdev = vq->vq.vdev;
2640 if (!vq->we_own_ring)
2643 if (num > vq->vq.num_max)
2649 if ((vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num) == num)
2652 if (!vdev->config->disable_vq_and_reset)
2655 if (!vdev->config->enable_vq_after_reset)
2658 err = vdev->config->disable_vq_and_reset(_vq);
2662 while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL)
2665 if (vq->packed_ring)
2666 err = virtqueue_resize_packed(_vq, num);
2668 err = virtqueue_resize_split(_vq, num);
2670 if (vdev->config->enable_vq_after_reset(_vq))
2675 EXPORT_SYMBOL_GPL(virtqueue_resize);
2677 /* Only available for split ring */
2678 struct virtqueue *vring_new_virtqueue(unsigned int index,
2680 unsigned int vring_align,
2681 struct virtio_device *vdev,
2685 bool (*notify)(struct virtqueue *vq),
2686 void (*callback)(struct virtqueue *vq),
2689 struct vring_virtqueue_split vring_split = {};
2691 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2694 vring_init(&vring_split.vring, num, pages, vring_align);
2695 return __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
2696 context, notify, callback, name,
2699 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
2701 static void vring_free(struct virtqueue *_vq)
2703 struct vring_virtqueue *vq = to_vvq(_vq);
2705 if (vq->we_own_ring) {
2706 if (vq->packed_ring) {
2707 vring_free_queue(vq->vq.vdev,
2708 vq->packed.ring_size_in_bytes,
2709 vq->packed.vring.desc,
2710 vq->packed.ring_dma_addr,
2713 vring_free_queue(vq->vq.vdev,
2714 vq->packed.event_size_in_bytes,
2715 vq->packed.vring.driver,
2716 vq->packed.driver_event_dma_addr,
2719 vring_free_queue(vq->vq.vdev,
2720 vq->packed.event_size_in_bytes,
2721 vq->packed.vring.device,
2722 vq->packed.device_event_dma_addr,
2725 kfree(vq->packed.desc_state);
2726 kfree(vq->packed.desc_extra);
2728 vring_free_queue(vq->vq.vdev,
2729 vq->split.queue_size_in_bytes,
2730 vq->split.vring.desc,
2731 vq->split.queue_dma_addr,
2735 if (!vq->packed_ring) {
2736 kfree(vq->split.desc_state);
2737 kfree(vq->split.desc_extra);
2741 void vring_del_virtqueue(struct virtqueue *_vq)
2743 struct vring_virtqueue *vq = to_vvq(_vq);
2745 spin_lock(&vq->vq.vdev->vqs_list_lock);
2746 list_del(&_vq->list);
2747 spin_unlock(&vq->vq.vdev->vqs_list_lock);
2753 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
2755 /* Manipulates transport-specific feature bits. */
2756 void vring_transport_features(struct virtio_device *vdev)
2760 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2762 case VIRTIO_RING_F_INDIRECT_DESC:
2764 case VIRTIO_RING_F_EVENT_IDX:
2766 case VIRTIO_F_VERSION_1:
2768 case VIRTIO_F_ACCESS_PLATFORM:
2770 case VIRTIO_F_RING_PACKED:
2772 case VIRTIO_F_ORDER_PLATFORM:
2775 /* We don't understand this bit. */
2776 __virtio_clear_bit(vdev, i);
2780 EXPORT_SYMBOL_GPL(vring_transport_features);
2783 * virtqueue_get_vring_size - return the size of the virtqueue's vring
2784 * @_vq: the struct virtqueue containing the vring of interest.
2786 * Returns the size of the vring. This is mainly used for boasting to
2787 * userspace. Unlike other operations, this need not be serialized.
2789 unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
2792 struct vring_virtqueue *vq = to_vvq(_vq);
2794 return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
2796 EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
2799 * This function should only be called by the core, not directly by the driver.
2801 void __virtqueue_break(struct virtqueue *_vq)
2803 struct vring_virtqueue *vq = to_vvq(_vq);
2805 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2806 WRITE_ONCE(vq->broken, true);
2808 EXPORT_SYMBOL_GPL(__virtqueue_break);
2811 * This function should only be called by the core, not directly by the driver.
2813 void __virtqueue_unbreak(struct virtqueue *_vq)
2815 struct vring_virtqueue *vq = to_vvq(_vq);
2817 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2818 WRITE_ONCE(vq->broken, false);
2820 EXPORT_SYMBOL_GPL(__virtqueue_unbreak);
2822 bool virtqueue_is_broken(struct virtqueue *_vq)
2824 struct vring_virtqueue *vq = to_vvq(_vq);
2826 return READ_ONCE(vq->broken);
2828 EXPORT_SYMBOL_GPL(virtqueue_is_broken);
2831 * This should prevent the device from being used, allowing drivers to
2832 * recover. You may need to grab appropriate locks to flush.
2834 void virtio_break_device(struct virtio_device *dev)
2836 struct virtqueue *_vq;
2838 spin_lock(&dev->vqs_list_lock);
2839 list_for_each_entry(_vq, &dev->vqs, list) {
2840 struct vring_virtqueue *vq = to_vvq(_vq);
2842 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2843 WRITE_ONCE(vq->broken, true);
2845 spin_unlock(&dev->vqs_list_lock);
2847 EXPORT_SYMBOL_GPL(virtio_break_device);
2850 * This should allow the device to be used by the driver. You may
2851 * need to grab appropriate locks to flush the write to
2852 * vq->broken. This should only be used in some specific case e.g
2853 * (probing and restoring). This function should only be called by the
2854 * core, not directly by the driver.
2856 void __virtio_unbreak_device(struct virtio_device *dev)
2858 struct virtqueue *_vq;
2860 spin_lock(&dev->vqs_list_lock);
2861 list_for_each_entry(_vq, &dev->vqs, list) {
2862 struct vring_virtqueue *vq = to_vvq(_vq);
2864 /* Pairs with READ_ONCE() in virtqueue_is_broken(). */
2865 WRITE_ONCE(vq->broken, false);
2867 spin_unlock(&dev->vqs_list_lock);
2869 EXPORT_SYMBOL_GPL(__virtio_unbreak_device);
2871 dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
2873 struct vring_virtqueue *vq = to_vvq(_vq);
2875 BUG_ON(!vq->we_own_ring);
2877 if (vq->packed_ring)
2878 return vq->packed.ring_dma_addr;
2880 return vq->split.queue_dma_addr;
2882 EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
2884 dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
2886 struct vring_virtqueue *vq = to_vvq(_vq);
2888 BUG_ON(!vq->we_own_ring);
2890 if (vq->packed_ring)
2891 return vq->packed.driver_event_dma_addr;
2893 return vq->split.queue_dma_addr +
2894 ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
2896 EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
2898 dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
2900 struct vring_virtqueue *vq = to_vvq(_vq);
2902 BUG_ON(!vq->we_own_ring);
2904 if (vq->packed_ring)
2905 return vq->packed.device_event_dma_addr;
2907 return vq->split.queue_dma_addr +
2908 ((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
2910 EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
2912 /* Only available for split ring */
2913 const struct vring *virtqueue_get_vring(struct virtqueue *vq)
2915 return &to_vvq(vq)->split.vring;
2917 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
2919 MODULE_LICENSE("GPL");