1 /* Virtio ring implementation.
3 * Copyright 2007 Rusty Russell IBM Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/virtio.h>
20 #include <linux/virtio_ring.h>
21 #include <linux/virtio_config.h>
22 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/hrtimer.h>
26 #include <linux/dma-mapping.h>
30 /* For development, we want to crash whenever the ring is screwed. */
31 #define BAD_RING(_vq, fmt, args...) \
33 dev_err(&(_vq)->vq.vdev->dev, \
34 "%s:"fmt, (_vq)->vq.name, ##args); \
37 /* Caller is supposed to guarantee no reentry. */
38 #define START_USE(_vq) \
41 panic("%s:in_use = %i\n", \
42 (_vq)->vq.name, (_vq)->in_use); \
43 (_vq)->in_use = __LINE__; \
45 #define END_USE(_vq) \
46 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
47 #define LAST_ADD_TIME_UPDATE(_vq) \
49 ktime_t now = ktime_get(); \
51 /* No kick or get, with .1 second between? Warn. */ \
52 if ((_vq)->last_add_time_valid) \
53 WARN_ON(ktime_to_ms(ktime_sub(now, \
54 (_vq)->last_add_time)) > 100); \
55 (_vq)->last_add_time = now; \
56 (_vq)->last_add_time_valid = true; \
58 #define LAST_ADD_TIME_CHECK(_vq) \
60 if ((_vq)->last_add_time_valid) { \
61 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \
62 (_vq)->last_add_time)) > 100); \
65 #define LAST_ADD_TIME_INVALID(_vq) \
66 ((_vq)->last_add_time_valid = false)
68 #define BAD_RING(_vq, fmt, args...) \
70 dev_err(&_vq->vq.vdev->dev, \
71 "%s:"fmt, (_vq)->vq.name, ##args); \
72 (_vq)->broken = true; \
76 #define LAST_ADD_TIME_UPDATE(vq)
77 #define LAST_ADD_TIME_CHECK(vq)
78 #define LAST_ADD_TIME_INVALID(vq)
81 struct vring_desc_state_split {
82 void *data; /* Data for callback. */
83 struct vring_desc *indir_desc; /* Indirect descriptor, if any. */
86 struct vring_desc_state_packed {
87 void *data; /* Data for callback. */
88 struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */
89 u16 num; /* Descriptor list length. */
90 u16 next; /* The next desc state in a list. */
91 u16 last; /* The last desc state in a list. */
94 struct vring_desc_extra_packed {
95 dma_addr_t addr; /* Buffer DMA addr. */
96 u32 len; /* Buffer length. */
97 u16 flags; /* Descriptor flags. */
100 struct vring_virtqueue {
103 /* Is this a packed ring? */
106 /* Is DMA API used? */
109 /* Can we use weak barriers? */
112 /* Other side has made a mess, don't try any more. */
115 /* Host supports indirect buffers */
118 /* Host publishes avail event idx */
121 /* Head of free buffer list. */
122 unsigned int free_head;
123 /* Number we've added since last sync. */
124 unsigned int num_added;
126 /* Last used index we've seen. */
130 /* Available for split ring */
132 /* Actual memory layout for this queue. */
135 /* Last written value to avail->flags */
136 u16 avail_flags_shadow;
139 * Last written value to avail->idx in
142 u16 avail_idx_shadow;
144 /* Per-descriptor state. */
145 struct vring_desc_state_split *desc_state;
147 /* DMA address and size information */
148 dma_addr_t queue_dma_addr;
149 size_t queue_size_in_bytes;
152 /* Available for packed ring */
154 /* Actual memory layout for this queue. */
157 struct vring_packed_desc *desc;
158 struct vring_packed_desc_event *driver;
159 struct vring_packed_desc_event *device;
162 /* Driver ring wrap counter. */
163 bool avail_wrap_counter;
165 /* Device ring wrap counter. */
166 bool used_wrap_counter;
168 /* Avail used flags. */
169 u16 avail_used_flags;
171 /* Index of the next avail descriptor. */
175 * Last written value to driver->flags in
178 u16 event_flags_shadow;
180 /* Per-descriptor state. */
181 struct vring_desc_state_packed *desc_state;
182 struct vring_desc_extra_packed *desc_extra;
184 /* DMA address and size information */
185 dma_addr_t ring_dma_addr;
186 dma_addr_t driver_event_dma_addr;
187 dma_addr_t device_event_dma_addr;
188 size_t ring_size_in_bytes;
189 size_t event_size_in_bytes;
193 /* How to notify other side. FIXME: commonalize hcalls! */
194 bool (*notify)(struct virtqueue *vq);
196 /* DMA, allocation, and size information */
200 /* They're supposed to lock for us. */
203 /* Figure out if their kicks are too delayed. */
204 bool last_add_time_valid;
205 ktime_t last_add_time;
214 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
216 static inline bool virtqueue_use_indirect(struct virtqueue *_vq,
217 unsigned int total_sg)
219 struct vring_virtqueue *vq = to_vvq(_vq);
222 * If the host supports indirect descriptor tables, and we have multiple
223 * buffers, then go indirect. FIXME: tune this threshold
225 return (vq->indirect && total_sg > 1 && vq->vq.num_free);
229 * Modern virtio devices have feature bits to specify whether they need a
230 * quirk and bypass the IOMMU. If not there, just use the DMA API.
232 * If there, the interaction between virtio and DMA API is messy.
234 * On most systems with virtio, physical addresses match bus addresses,
235 * and it doesn't particularly matter whether we use the DMA API.
237 * On some systems, including Xen and any system with a physical device
238 * that speaks virtio behind a physical IOMMU, we must use the DMA API
239 * for virtio DMA to work at all.
241 * On other systems, including SPARC and PPC64, virtio-pci devices are
242 * enumerated as though they are behind an IOMMU, but the virtio host
243 * ignores the IOMMU, so we must either pretend that the IOMMU isn't
244 * there or somehow map everything as the identity.
246 * For the time being, we preserve historic behavior and bypass the DMA
249 * TODO: install a per-device DMA ops structure that does the right thing
250 * taking into account all the above quirks, and use the DMA API
251 * unconditionally on data path.
254 static bool vring_use_dma_api(struct virtio_device *vdev)
256 if (!virtio_has_iommu_quirk(vdev))
259 /* Otherwise, we are left to guess. */
261 * In theory, it's possible to have a buggy QEMU-supposed
262 * emulated Q35 IOMMU and Xen enabled at the same time. On
263 * such a configuration, virtio has never worked and will
264 * not work without an even larger kludge. Instead, enable
265 * the DMA API if we're a Xen guest, which at least allows
266 * all of the sensible Xen configurations to work correctly.
274 static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
275 dma_addr_t *dma_handle, gfp_t flag)
277 if (vring_use_dma_api(vdev)) {
278 return dma_alloc_coherent(vdev->dev.parent, size,
281 void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
284 phys_addr_t phys_addr = virt_to_phys(queue);
285 *dma_handle = (dma_addr_t)phys_addr;
288 * Sanity check: make sure we dind't truncate
289 * the address. The only arches I can find that
290 * have 64-bit phys_addr_t but 32-bit dma_addr_t
291 * are certain non-highmem MIPS and x86
292 * configurations, but these configurations
293 * should never allocate physical pages above 32
294 * bits, so this is fine. Just in case, throw a
295 * warning and abort if we end up with an
296 * unrepresentable address.
298 if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
299 free_pages_exact(queue, PAGE_ALIGN(size));
307 static void vring_free_queue(struct virtio_device *vdev, size_t size,
308 void *queue, dma_addr_t dma_handle)
310 if (vring_use_dma_api(vdev))
311 dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
313 free_pages_exact(queue, PAGE_ALIGN(size));
317 * The DMA ops on various arches are rather gnarly right now, and
318 * making all of the arch DMA ops work on the vring device itself
319 * is a mess. For now, we use the parent device for DMA ops.
321 static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq)
323 return vq->vq.vdev->dev.parent;
326 /* Map one sg entry. */
327 static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
328 struct scatterlist *sg,
329 enum dma_data_direction direction)
331 if (!vq->use_dma_api)
332 return (dma_addr_t)sg_phys(sg);
335 * We can't use dma_map_sg, because we don't use scatterlists in
336 * the way it expects (we don't guarantee that the scatterlist
337 * will exist for the lifetime of the mapping).
339 return dma_map_page(vring_dma_dev(vq),
340 sg_page(sg), sg->offset, sg->length,
344 static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
345 void *cpu_addr, size_t size,
346 enum dma_data_direction direction)
348 if (!vq->use_dma_api)
349 return (dma_addr_t)virt_to_phys(cpu_addr);
351 return dma_map_single(vring_dma_dev(vq),
352 cpu_addr, size, direction);
355 static int vring_mapping_error(const struct vring_virtqueue *vq,
358 if (!vq->use_dma_api)
361 return dma_mapping_error(vring_dma_dev(vq), addr);
366 * Split ring specific functions - *_split().
369 static void vring_unmap_one_split(const struct vring_virtqueue *vq,
370 struct vring_desc *desc)
374 if (!vq->use_dma_api)
377 flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
379 if (flags & VRING_DESC_F_INDIRECT) {
380 dma_unmap_single(vring_dma_dev(vq),
381 virtio64_to_cpu(vq->vq.vdev, desc->addr),
382 virtio32_to_cpu(vq->vq.vdev, desc->len),
383 (flags & VRING_DESC_F_WRITE) ?
384 DMA_FROM_DEVICE : DMA_TO_DEVICE);
386 dma_unmap_page(vring_dma_dev(vq),
387 virtio64_to_cpu(vq->vq.vdev, desc->addr),
388 virtio32_to_cpu(vq->vq.vdev, desc->len),
389 (flags & VRING_DESC_F_WRITE) ?
390 DMA_FROM_DEVICE : DMA_TO_DEVICE);
394 static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
395 unsigned int total_sg,
398 struct vring_desc *desc;
402 * We require lowmem mappings for the descriptors because
403 * otherwise virt_to_phys will give us bogus addresses in the
406 gfp &= ~__GFP_HIGHMEM;
408 desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
412 for (i = 0; i < total_sg; i++)
413 desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
417 static inline int virtqueue_add_split(struct virtqueue *_vq,
418 struct scatterlist *sgs[],
419 unsigned int total_sg,
420 unsigned int out_sgs,
426 struct vring_virtqueue *vq = to_vvq(_vq);
427 struct scatterlist *sg;
428 struct vring_desc *desc;
429 unsigned int i, n, avail, descs_used, uninitialized_var(prev), err_idx;
435 BUG_ON(data == NULL);
436 BUG_ON(ctx && vq->indirect);
438 if (unlikely(vq->broken)) {
443 LAST_ADD_TIME_UPDATE(vq);
445 BUG_ON(total_sg == 0);
447 head = vq->free_head;
449 if (virtqueue_use_indirect(_vq, total_sg))
450 desc = alloc_indirect_split(_vq, total_sg, gfp);
453 WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
457 /* Use a single buffer which doesn't continue */
459 /* Set up rest to use this indirect table. */
464 desc = vq->split.vring.desc;
466 descs_used = total_sg;
469 if (vq->vq.num_free < descs_used) {
470 pr_debug("Can't add buf len %i - avail = %i\n",
471 descs_used, vq->vq.num_free);
472 /* FIXME: for historical reasons, we force a notify here if
473 * there are outgoing parts to the buffer. Presumably the
474 * host should service the ring ASAP. */
483 for (n = 0; n < out_sgs; n++) {
484 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
485 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
486 if (vring_mapping_error(vq, addr))
489 desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT);
490 desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
491 desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
493 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
496 for (; n < (out_sgs + in_sgs); n++) {
497 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
498 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
499 if (vring_mapping_error(vq, addr))
502 desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE);
503 desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
504 desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
506 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
509 /* Last one doesn't continue. */
510 desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
513 /* Now that the indirect table is filled in, map it. */
514 dma_addr_t addr = vring_map_single(
515 vq, desc, total_sg * sizeof(struct vring_desc),
517 if (vring_mapping_error(vq, addr))
520 vq->split.vring.desc[head].flags = cpu_to_virtio16(_vq->vdev,
521 VRING_DESC_F_INDIRECT);
522 vq->split.vring.desc[head].addr = cpu_to_virtio64(_vq->vdev,
525 vq->split.vring.desc[head].len = cpu_to_virtio32(_vq->vdev,
526 total_sg * sizeof(struct vring_desc));
529 /* We're using some buffers from the free list. */
530 vq->vq.num_free -= descs_used;
532 /* Update free pointer */
534 vq->free_head = virtio16_to_cpu(_vq->vdev,
535 vq->split.vring.desc[head].next);
539 /* Store token and indirect buffer state. */
540 vq->split.desc_state[head].data = data;
542 vq->split.desc_state[head].indir_desc = desc;
544 vq->split.desc_state[head].indir_desc = ctx;
546 /* Put entry in available array (but don't update avail->idx until they
548 avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
549 vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
551 /* Descriptors and available array need to be set before we expose the
552 * new available array entries. */
553 virtio_wmb(vq->weak_barriers);
554 vq->split.avail_idx_shadow++;
555 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
556 vq->split.avail_idx_shadow);
559 pr_debug("Added buffer head %i to %p\n", head, vq);
562 /* This is very unlikely, but theoretically possible. Kick
564 if (unlikely(vq->num_added == (1 << 16) - 1))
573 for (n = 0; n < total_sg; n++) {
576 vring_unmap_one_split(vq, &desc[i]);
577 i = virtio16_to_cpu(_vq->vdev, vq->split.vring.desc[i].next);
587 static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
589 struct vring_virtqueue *vq = to_vvq(_vq);
594 /* We need to expose available array entries before checking avail
596 virtio_mb(vq->weak_barriers);
598 old = vq->split.avail_idx_shadow - vq->num_added;
599 new = vq->split.avail_idx_shadow;
602 LAST_ADD_TIME_CHECK(vq);
603 LAST_ADD_TIME_INVALID(vq);
606 needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
607 vring_avail_event(&vq->split.vring)),
610 needs_kick = !(vq->split.vring.used->flags &
611 cpu_to_virtio16(_vq->vdev,
612 VRING_USED_F_NO_NOTIFY));
618 static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
622 __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
624 /* Clear data ptr. */
625 vq->split.desc_state[head].data = NULL;
627 /* Put back on free list: unmap first-level descriptors and find end */
630 while (vq->split.vring.desc[i].flags & nextflag) {
631 vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
632 i = virtio16_to_cpu(vq->vq.vdev, vq->split.vring.desc[i].next);
636 vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
637 vq->split.vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev,
639 vq->free_head = head;
641 /* Plus final descriptor */
645 struct vring_desc *indir_desc =
646 vq->split.desc_state[head].indir_desc;
649 /* Free the indirect table, if any, now that it's unmapped. */
653 len = virtio32_to_cpu(vq->vq.vdev,
654 vq->split.vring.desc[head].len);
656 BUG_ON(!(vq->split.vring.desc[head].flags &
657 cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)));
658 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
660 for (j = 0; j < len / sizeof(struct vring_desc); j++)
661 vring_unmap_one_split(vq, &indir_desc[j]);
664 vq->split.desc_state[head].indir_desc = NULL;
666 *ctx = vq->split.desc_state[head].indir_desc;
670 static inline bool more_used_split(const struct vring_virtqueue *vq)
672 return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
673 vq->split.vring.used->idx);
676 static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
680 struct vring_virtqueue *vq = to_vvq(_vq);
687 if (unlikely(vq->broken)) {
692 if (!more_used_split(vq)) {
693 pr_debug("No more buffers in queue\n");
698 /* Only get used array entries after they have been exposed by host. */
699 virtio_rmb(vq->weak_barriers);
701 last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
702 i = virtio32_to_cpu(_vq->vdev,
703 vq->split.vring.used->ring[last_used].id);
704 *len = virtio32_to_cpu(_vq->vdev,
705 vq->split.vring.used->ring[last_used].len);
707 if (unlikely(i >= vq->split.vring.num)) {
708 BAD_RING(vq, "id %u out of range\n", i);
711 if (unlikely(!vq->split.desc_state[i].data)) {
712 BAD_RING(vq, "id %u is not a head!\n", i);
716 /* detach_buf_split clears data, so grab it now. */
717 ret = vq->split.desc_state[i].data;
718 detach_buf_split(vq, i, ctx);
720 /* If we expect an interrupt for the next entry, tell host
721 * by writing event index and flush out the write before
722 * the read in the next get_buf call. */
723 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
724 virtio_store_mb(vq->weak_barriers,
725 &vring_used_event(&vq->split.vring),
726 cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
728 LAST_ADD_TIME_INVALID(vq);
734 static void virtqueue_disable_cb_split(struct virtqueue *_vq)
736 struct vring_virtqueue *vq = to_vvq(_vq);
738 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
739 vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
741 vq->split.vring.avail->flags =
742 cpu_to_virtio16(_vq->vdev,
743 vq->split.avail_flags_shadow);
747 static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
749 struct vring_virtqueue *vq = to_vvq(_vq);
754 /* We optimistically turn back on interrupts, then check if there was
756 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
757 * either clear the flags bit or point the event index at the next
758 * entry. Always do both to keep code simple. */
759 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
760 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
762 vq->split.vring.avail->flags =
763 cpu_to_virtio16(_vq->vdev,
764 vq->split.avail_flags_shadow);
766 vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
767 last_used_idx = vq->last_used_idx);
769 return last_used_idx;
772 static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx)
774 struct vring_virtqueue *vq = to_vvq(_vq);
776 return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
777 vq->split.vring.used->idx);
780 static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
782 struct vring_virtqueue *vq = to_vvq(_vq);
787 /* We optimistically turn back on interrupts, then check if there was
789 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
790 * either clear the flags bit or point the event index at the next
791 * entry. Always update the event index to keep code simple. */
792 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
793 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
795 vq->split.vring.avail->flags =
796 cpu_to_virtio16(_vq->vdev,
797 vq->split.avail_flags_shadow);
799 /* TODO: tune this threshold */
800 bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
802 virtio_store_mb(vq->weak_barriers,
803 &vring_used_event(&vq->split.vring),
804 cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
806 if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
807 - vq->last_used_idx) > bufs)) {
816 static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
818 struct vring_virtqueue *vq = to_vvq(_vq);
824 for (i = 0; i < vq->split.vring.num; i++) {
825 if (!vq->split.desc_state[i].data)
827 /* detach_buf_split clears data, so grab it now. */
828 buf = vq->split.desc_state[i].data;
829 detach_buf_split(vq, i, NULL);
830 vq->split.avail_idx_shadow--;
831 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
832 vq->split.avail_idx_shadow);
836 /* That should have freed everything. */
837 BUG_ON(vq->vq.num_free != vq->split.vring.num);
843 static struct virtqueue *vring_create_virtqueue_split(
846 unsigned int vring_align,
847 struct virtio_device *vdev,
851 bool (*notify)(struct virtqueue *),
852 void (*callback)(struct virtqueue *),
855 struct virtqueue *vq;
858 size_t queue_size_in_bytes;
861 /* We assume num is a power of 2. */
862 if (num & (num - 1)) {
863 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
867 /* TODO: allocate each queue chunk individually */
868 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
869 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
871 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
880 /* Try to get a single page. You are my only hope! */
881 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
882 &dma_addr, GFP_KERNEL|__GFP_ZERO);
887 queue_size_in_bytes = vring_size(num, vring_align);
888 vring_init(&vring, num, queue, vring_align);
890 vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
891 notify, callback, name);
893 vring_free_queue(vdev, queue_size_in_bytes, queue,
898 to_vvq(vq)->split.queue_dma_addr = dma_addr;
899 to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes;
900 to_vvq(vq)->we_own_ring = true;
907 * Packed ring specific functions - *_packed().
910 static void vring_unmap_state_packed(const struct vring_virtqueue *vq,
911 struct vring_desc_extra_packed *state)
915 if (!vq->use_dma_api)
918 flags = state->flags;
920 if (flags & VRING_DESC_F_INDIRECT) {
921 dma_unmap_single(vring_dma_dev(vq),
922 state->addr, state->len,
923 (flags & VRING_DESC_F_WRITE) ?
924 DMA_FROM_DEVICE : DMA_TO_DEVICE);
926 dma_unmap_page(vring_dma_dev(vq),
927 state->addr, state->len,
928 (flags & VRING_DESC_F_WRITE) ?
929 DMA_FROM_DEVICE : DMA_TO_DEVICE);
933 static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
934 struct vring_packed_desc *desc)
938 if (!vq->use_dma_api)
941 flags = le16_to_cpu(desc->flags);
943 if (flags & VRING_DESC_F_INDIRECT) {
944 dma_unmap_single(vring_dma_dev(vq),
945 le64_to_cpu(desc->addr),
946 le32_to_cpu(desc->len),
947 (flags & VRING_DESC_F_WRITE) ?
948 DMA_FROM_DEVICE : DMA_TO_DEVICE);
950 dma_unmap_page(vring_dma_dev(vq),
951 le64_to_cpu(desc->addr),
952 le32_to_cpu(desc->len),
953 (flags & VRING_DESC_F_WRITE) ?
954 DMA_FROM_DEVICE : DMA_TO_DEVICE);
958 static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
961 struct vring_packed_desc *desc;
964 * We require lowmem mappings for the descriptors because
965 * otherwise virt_to_phys will give us bogus addresses in the
968 gfp &= ~__GFP_HIGHMEM;
970 desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
975 static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
976 struct scatterlist *sgs[],
977 unsigned int total_sg,
978 unsigned int out_sgs,
983 struct vring_packed_desc *desc;
984 struct scatterlist *sg;
985 unsigned int i, n, err_idx;
989 head = vq->packed.next_avail_idx;
990 desc = alloc_indirect_packed(total_sg, gfp);
992 if (unlikely(vq->vq.num_free < 1)) {
993 pr_debug("Can't add buf len 1 - avail = 0\n");
1000 BUG_ON(id == vq->packed.vring.num);
1002 for (n = 0; n < out_sgs + in_sgs; n++) {
1003 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1004 addr = vring_map_one_sg(vq, sg, n < out_sgs ?
1005 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1006 if (vring_mapping_error(vq, addr))
1009 desc[i].flags = cpu_to_le16(n < out_sgs ?
1010 0 : VRING_DESC_F_WRITE);
1011 desc[i].addr = cpu_to_le64(addr);
1012 desc[i].len = cpu_to_le32(sg->length);
1017 /* Now that the indirect table is filled in, map it. */
1018 addr = vring_map_single(vq, desc,
1019 total_sg * sizeof(struct vring_packed_desc),
1021 if (vring_mapping_error(vq, addr))
1024 vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
1025 vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
1026 sizeof(struct vring_packed_desc));
1027 vq->packed.vring.desc[head].id = cpu_to_le16(id);
1029 if (vq->use_dma_api) {
1030 vq->packed.desc_extra[id].addr = addr;
1031 vq->packed.desc_extra[id].len = total_sg *
1032 sizeof(struct vring_packed_desc);
1033 vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
1034 vq->packed.avail_used_flags;
1038 * A driver MUST NOT make the first descriptor in the list
1039 * available before all subsequent descriptors comprising
1040 * the list are made available.
1042 virtio_wmb(vq->weak_barriers);
1043 vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
1044 vq->packed.avail_used_flags);
1046 /* We're using some buffers from the free list. */
1047 vq->vq.num_free -= 1;
1049 /* Update free pointer */
1051 if (n >= vq->packed.vring.num) {
1053 vq->packed.avail_wrap_counter ^= 1;
1054 vq->packed.avail_used_flags ^=
1055 1 << VRING_PACKED_DESC_F_AVAIL |
1056 1 << VRING_PACKED_DESC_F_USED;
1058 vq->packed.next_avail_idx = n;
1059 vq->free_head = vq->packed.desc_state[id].next;
1061 /* Store token and indirect buffer state. */
1062 vq->packed.desc_state[id].num = 1;
1063 vq->packed.desc_state[id].data = data;
1064 vq->packed.desc_state[id].indir_desc = desc;
1065 vq->packed.desc_state[id].last = id;
1069 pr_debug("Added buffer head %i to %p\n", head, vq);
1077 for (i = 0; i < err_idx; i++)
1078 vring_unmap_desc_packed(vq, &desc[i]);
1086 static inline int virtqueue_add_packed(struct virtqueue *_vq,
1087 struct scatterlist *sgs[],
1088 unsigned int total_sg,
1089 unsigned int out_sgs,
1090 unsigned int in_sgs,
1095 struct vring_virtqueue *vq = to_vvq(_vq);
1096 struct vring_packed_desc *desc;
1097 struct scatterlist *sg;
1098 unsigned int i, n, c, descs_used, err_idx;
1099 __le16 uninitialized_var(head_flags), flags;
1100 u16 head, id, uninitialized_var(prev), curr, avail_used_flags;
1104 BUG_ON(data == NULL);
1105 BUG_ON(ctx && vq->indirect);
1107 if (unlikely(vq->broken)) {
1112 LAST_ADD_TIME_UPDATE(vq);
1114 BUG_ON(total_sg == 0);
1116 if (virtqueue_use_indirect(_vq, total_sg))
1117 return virtqueue_add_indirect_packed(vq, sgs, total_sg,
1118 out_sgs, in_sgs, data, gfp);
1120 head = vq->packed.next_avail_idx;
1121 avail_used_flags = vq->packed.avail_used_flags;
1123 WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
1125 desc = vq->packed.vring.desc;
1127 descs_used = total_sg;
1129 if (unlikely(vq->vq.num_free < descs_used)) {
1130 pr_debug("Can't add buf len %i - avail = %i\n",
1131 descs_used, vq->vq.num_free);
1137 BUG_ON(id == vq->packed.vring.num);
1141 for (n = 0; n < out_sgs + in_sgs; n++) {
1142 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1143 dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ?
1144 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1145 if (vring_mapping_error(vq, addr))
1148 flags = cpu_to_le16(vq->packed.avail_used_flags |
1149 (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
1150 (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
1154 desc[i].flags = flags;
1156 desc[i].addr = cpu_to_le64(addr);
1157 desc[i].len = cpu_to_le32(sg->length);
1158 desc[i].id = cpu_to_le16(id);
1160 if (unlikely(vq->use_dma_api)) {
1161 vq->packed.desc_extra[curr].addr = addr;
1162 vq->packed.desc_extra[curr].len = sg->length;
1163 vq->packed.desc_extra[curr].flags =
1167 curr = vq->packed.desc_state[curr].next;
1169 if ((unlikely(++i >= vq->packed.vring.num))) {
1171 vq->packed.avail_used_flags ^=
1172 1 << VRING_PACKED_DESC_F_AVAIL |
1173 1 << VRING_PACKED_DESC_F_USED;
1179 vq->packed.avail_wrap_counter ^= 1;
1181 /* We're using some buffers from the free list. */
1182 vq->vq.num_free -= descs_used;
1184 /* Update free pointer */
1185 vq->packed.next_avail_idx = i;
1186 vq->free_head = curr;
1189 vq->packed.desc_state[id].num = descs_used;
1190 vq->packed.desc_state[id].data = data;
1191 vq->packed.desc_state[id].indir_desc = ctx;
1192 vq->packed.desc_state[id].last = prev;
1195 * A driver MUST NOT make the first descriptor in the list
1196 * available before all subsequent descriptors comprising
1197 * the list are made available.
1199 virtio_wmb(vq->weak_barriers);
1200 vq->packed.vring.desc[head].flags = head_flags;
1201 vq->num_added += descs_used;
1203 pr_debug("Added buffer head %i to %p\n", head, vq);
1212 vq->packed.avail_used_flags = avail_used_flags;
1214 for (n = 0; n < total_sg; n++) {
1217 vring_unmap_desc_packed(vq, &desc[i]);
1219 if (i >= vq->packed.vring.num)
1227 static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
1229 struct vring_virtqueue *vq = to_vvq(_vq);
1230 u16 new, old, off_wrap, flags, wrap_counter, event_idx;
1243 * We need to expose the new flags value before checking notification
1246 virtio_mb(vq->weak_barriers);
1248 old = vq->packed.next_avail_idx - vq->num_added;
1249 new = vq->packed.next_avail_idx;
1252 snapshot.u32 = *(u32 *)vq->packed.vring.device;
1253 flags = le16_to_cpu(snapshot.flags);
1255 LAST_ADD_TIME_CHECK(vq);
1256 LAST_ADD_TIME_INVALID(vq);
1258 if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
1259 needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1263 off_wrap = le16_to_cpu(snapshot.off_wrap);
1265 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1266 event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1267 if (wrap_counter != vq->packed.avail_wrap_counter)
1268 event_idx -= vq->packed.vring.num;
1270 needs_kick = vring_need_event(event_idx, new, old);
1276 static void detach_buf_packed(struct vring_virtqueue *vq,
1277 unsigned int id, void **ctx)
1279 struct vring_desc_state_packed *state = NULL;
1280 struct vring_packed_desc *desc;
1281 unsigned int i, curr;
1283 state = &vq->packed.desc_state[id];
1285 /* Clear data ptr. */
1288 vq->packed.desc_state[state->last].next = vq->free_head;
1290 vq->vq.num_free += state->num;
1292 if (unlikely(vq->use_dma_api)) {
1294 for (i = 0; i < state->num; i++) {
1295 vring_unmap_state_packed(vq,
1296 &vq->packed.desc_extra[curr]);
1297 curr = vq->packed.desc_state[curr].next;
1304 /* Free the indirect table, if any, now that it's unmapped. */
1305 desc = state->indir_desc;
1309 if (vq->use_dma_api) {
1310 len = vq->packed.desc_extra[id].len;
1311 for (i = 0; i < len / sizeof(struct vring_packed_desc);
1313 vring_unmap_desc_packed(vq, &desc[i]);
1316 state->indir_desc = NULL;
1318 *ctx = state->indir_desc;
1322 static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
1323 u16 idx, bool used_wrap_counter)
1328 flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
1329 avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
1330 used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
1332 return avail == used && used == used_wrap_counter;
1335 static inline bool more_used_packed(const struct vring_virtqueue *vq)
1337 return is_used_desc_packed(vq, vq->last_used_idx,
1338 vq->packed.used_wrap_counter);
1341 static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
1345 struct vring_virtqueue *vq = to_vvq(_vq);
1351 if (unlikely(vq->broken)) {
1356 if (!more_used_packed(vq)) {
1357 pr_debug("No more buffers in queue\n");
1362 /* Only get used elements after they have been exposed by host. */
1363 virtio_rmb(vq->weak_barriers);
1365 last_used = vq->last_used_idx;
1366 id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
1367 *len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
1369 if (unlikely(id >= vq->packed.vring.num)) {
1370 BAD_RING(vq, "id %u out of range\n", id);
1373 if (unlikely(!vq->packed.desc_state[id].data)) {
1374 BAD_RING(vq, "id %u is not a head!\n", id);
1378 /* detach_buf_packed clears data, so grab it now. */
1379 ret = vq->packed.desc_state[id].data;
1380 detach_buf_packed(vq, id, ctx);
1382 vq->last_used_idx += vq->packed.desc_state[id].num;
1383 if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) {
1384 vq->last_used_idx -= vq->packed.vring.num;
1385 vq->packed.used_wrap_counter ^= 1;
1389 * If we expect an interrupt for the next entry, tell host
1390 * by writing event index and flush out the write before
1391 * the read in the next get_buf call.
1393 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1394 virtio_store_mb(vq->weak_barriers,
1395 &vq->packed.vring.driver->off_wrap,
1396 cpu_to_le16(vq->last_used_idx |
1397 (vq->packed.used_wrap_counter <<
1398 VRING_PACKED_EVENT_F_WRAP_CTR)));
1400 LAST_ADD_TIME_INVALID(vq);
1406 static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
1408 struct vring_virtqueue *vq = to_vvq(_vq);
1410 if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
1411 vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1412 vq->packed.vring.driver->flags =
1413 cpu_to_le16(vq->packed.event_flags_shadow);
1417 static unsigned virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
1419 struct vring_virtqueue *vq = to_vvq(_vq);
1424 * We optimistically turn back on interrupts, then check if there was
1429 vq->packed.vring.driver->off_wrap =
1430 cpu_to_le16(vq->last_used_idx |
1431 (vq->packed.used_wrap_counter <<
1432 VRING_PACKED_EVENT_F_WRAP_CTR));
1434 * We need to update event offset and event wrap
1435 * counter first before updating event flags.
1437 virtio_wmb(vq->weak_barriers);
1440 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1441 vq->packed.event_flags_shadow = vq->event ?
1442 VRING_PACKED_EVENT_FLAG_DESC :
1443 VRING_PACKED_EVENT_FLAG_ENABLE;
1444 vq->packed.vring.driver->flags =
1445 cpu_to_le16(vq->packed.event_flags_shadow);
1449 return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter <<
1450 VRING_PACKED_EVENT_F_WRAP_CTR);
1453 static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
1455 struct vring_virtqueue *vq = to_vvq(_vq);
1459 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1460 used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1462 return is_used_desc_packed(vq, used_idx, wrap_counter);
1465 static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
1467 struct vring_virtqueue *vq = to_vvq(_vq);
1468 u16 used_idx, wrap_counter;
1474 * We optimistically turn back on interrupts, then check if there was
1479 /* TODO: tune this threshold */
1480 bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
1481 wrap_counter = vq->packed.used_wrap_counter;
1483 used_idx = vq->last_used_idx + bufs;
1484 if (used_idx >= vq->packed.vring.num) {
1485 used_idx -= vq->packed.vring.num;
1489 vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1490 (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1493 * We need to update event offset and event wrap
1494 * counter first before updating event flags.
1496 virtio_wmb(vq->weak_barriers);
1498 used_idx = vq->last_used_idx;
1499 wrap_counter = vq->packed.used_wrap_counter;
1502 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1503 vq->packed.event_flags_shadow = vq->event ?
1504 VRING_PACKED_EVENT_FLAG_DESC :
1505 VRING_PACKED_EVENT_FLAG_ENABLE;
1506 vq->packed.vring.driver->flags =
1507 cpu_to_le16(vq->packed.event_flags_shadow);
1511 * We need to update event suppression structure first
1512 * before re-checking for more used buffers.
1514 virtio_mb(vq->weak_barriers);
1516 if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
1525 static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
1527 struct vring_virtqueue *vq = to_vvq(_vq);
1533 for (i = 0; i < vq->packed.vring.num; i++) {
1534 if (!vq->packed.desc_state[i].data)
1536 /* detach_buf clears data, so grab it now. */
1537 buf = vq->packed.desc_state[i].data;
1538 detach_buf_packed(vq, i, NULL);
1542 /* That should have freed everything. */
1543 BUG_ON(vq->vq.num_free != vq->packed.vring.num);
1549 static struct virtqueue *vring_create_virtqueue_packed(
1552 unsigned int vring_align,
1553 struct virtio_device *vdev,
1555 bool may_reduce_num,
1557 bool (*notify)(struct virtqueue *),
1558 void (*callback)(struct virtqueue *),
1561 struct vring_virtqueue *vq;
1562 struct vring_packed_desc *ring;
1563 struct vring_packed_desc_event *driver, *device;
1564 dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
1565 size_t ring_size_in_bytes, event_size_in_bytes;
1568 ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
1570 ring = vring_alloc_queue(vdev, ring_size_in_bytes,
1572 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1576 event_size_in_bytes = sizeof(struct vring_packed_desc_event);
1578 driver = vring_alloc_queue(vdev, event_size_in_bytes,
1579 &driver_event_dma_addr,
1580 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1584 device = vring_alloc_queue(vdev, event_size_in_bytes,
1585 &device_event_dma_addr,
1586 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1590 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
1594 vq->vq.callback = callback;
1597 vq->vq.num_free = num;
1598 vq->vq.index = index;
1599 vq->we_own_ring = true;
1600 vq->notify = notify;
1601 vq->weak_barriers = weak_barriers;
1603 vq->last_used_idx = 0;
1605 vq->packed_ring = true;
1606 vq->use_dma_api = vring_use_dma_api(vdev);
1607 list_add_tail(&vq->vq.list, &vdev->vqs);
1610 vq->last_add_time_valid = false;
1613 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
1615 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
1617 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
1618 vq->weak_barriers = false;
1620 vq->packed.ring_dma_addr = ring_dma_addr;
1621 vq->packed.driver_event_dma_addr = driver_event_dma_addr;
1622 vq->packed.device_event_dma_addr = device_event_dma_addr;
1624 vq->packed.ring_size_in_bytes = ring_size_in_bytes;
1625 vq->packed.event_size_in_bytes = event_size_in_bytes;
1627 vq->packed.vring.num = num;
1628 vq->packed.vring.desc = ring;
1629 vq->packed.vring.driver = driver;
1630 vq->packed.vring.device = device;
1632 vq->packed.next_avail_idx = 0;
1633 vq->packed.avail_wrap_counter = 1;
1634 vq->packed.used_wrap_counter = 1;
1635 vq->packed.event_flags_shadow = 0;
1636 vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
1638 vq->packed.desc_state = kmalloc_array(num,
1639 sizeof(struct vring_desc_state_packed),
1641 if (!vq->packed.desc_state)
1642 goto err_desc_state;
1644 memset(vq->packed.desc_state, 0,
1645 num * sizeof(struct vring_desc_state_packed));
1647 /* Put everything in free lists. */
1649 for (i = 0; i < num-1; i++)
1650 vq->packed.desc_state[i].next = i + 1;
1652 vq->packed.desc_extra = kmalloc_array(num,
1653 sizeof(struct vring_desc_extra_packed),
1655 if (!vq->packed.desc_extra)
1656 goto err_desc_extra;
1658 memset(vq->packed.desc_extra, 0,
1659 num * sizeof(struct vring_desc_extra_packed));
1661 /* No callback? Tell other side not to bother us. */
1663 vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1664 vq->packed.vring.driver->flags =
1665 cpu_to_le16(vq->packed.event_flags_shadow);
1671 kfree(vq->packed.desc_state);
1675 vring_free_queue(vdev, event_size_in_bytes, device, ring_dma_addr);
1677 vring_free_queue(vdev, event_size_in_bytes, driver, ring_dma_addr);
1679 vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr);
1686 * Generic functions and exported symbols.
1689 static inline int virtqueue_add(struct virtqueue *_vq,
1690 struct scatterlist *sgs[],
1691 unsigned int total_sg,
1692 unsigned int out_sgs,
1693 unsigned int in_sgs,
1698 struct vring_virtqueue *vq = to_vvq(_vq);
1700 return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
1701 out_sgs, in_sgs, data, ctx, gfp) :
1702 virtqueue_add_split(_vq, sgs, total_sg,
1703 out_sgs, in_sgs, data, ctx, gfp);
1707 * virtqueue_add_sgs - expose buffers to other end
1708 * @vq: the struct virtqueue we're talking about.
1709 * @sgs: array of terminated scatterlists.
1710 * @out_num: the number of scatterlists readable by other side
1711 * @in_num: the number of scatterlists which are writable (after readable ones)
1712 * @data: the token identifying the buffer.
1713 * @gfp: how to do memory allocations (if necessary).
1715 * Caller must ensure we don't call this with other virtqueue operations
1716 * at the same time (except where noted).
1718 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1720 int virtqueue_add_sgs(struct virtqueue *_vq,
1721 struct scatterlist *sgs[],
1722 unsigned int out_sgs,
1723 unsigned int in_sgs,
1727 unsigned int i, total_sg = 0;
1729 /* Count them first. */
1730 for (i = 0; i < out_sgs + in_sgs; i++) {
1731 struct scatterlist *sg;
1733 for (sg = sgs[i]; sg; sg = sg_next(sg))
1736 return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
1739 EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
1742 * virtqueue_add_outbuf - expose output buffers to other end
1743 * @vq: the struct virtqueue we're talking about.
1744 * @sg: scatterlist (must be well-formed and terminated!)
1745 * @num: the number of entries in @sg readable by other side
1746 * @data: the token identifying the buffer.
1747 * @gfp: how to do memory allocations (if necessary).
1749 * Caller must ensure we don't call this with other virtqueue operations
1750 * at the same time (except where noted).
1752 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1754 int virtqueue_add_outbuf(struct virtqueue *vq,
1755 struct scatterlist *sg, unsigned int num,
1759 return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
1761 EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
1764 * virtqueue_add_inbuf - expose input buffers to other end
1765 * @vq: the struct virtqueue we're talking about.
1766 * @sg: scatterlist (must be well-formed and terminated!)
1767 * @num: the number of entries in @sg writable by other side
1768 * @data: the token identifying the buffer.
1769 * @gfp: how to do memory allocations (if necessary).
1771 * Caller must ensure we don't call this with other virtqueue operations
1772 * at the same time (except where noted).
1774 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1776 int virtqueue_add_inbuf(struct virtqueue *vq,
1777 struct scatterlist *sg, unsigned int num,
1781 return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
1783 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
1786 * virtqueue_add_inbuf_ctx - expose input buffers to other end
1787 * @vq: the struct virtqueue we're talking about.
1788 * @sg: scatterlist (must be well-formed and terminated!)
1789 * @num: the number of entries in @sg writable by other side
1790 * @data: the token identifying the buffer.
1791 * @ctx: extra context for the token
1792 * @gfp: how to do memory allocations (if necessary).
1794 * Caller must ensure we don't call this with other virtqueue operations
1795 * at the same time (except where noted).
1797 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1799 int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
1800 struct scatterlist *sg, unsigned int num,
1805 return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
1807 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
1810 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
1811 * @vq: the struct virtqueue
1813 * Instead of virtqueue_kick(), you can do:
1814 * if (virtqueue_kick_prepare(vq))
1815 * virtqueue_notify(vq);
1817 * This is sometimes useful because the virtqueue_kick_prepare() needs
1818 * to be serialized, but the actual virtqueue_notify() call does not.
1820 bool virtqueue_kick_prepare(struct virtqueue *_vq)
1822 struct vring_virtqueue *vq = to_vvq(_vq);
1824 return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
1825 virtqueue_kick_prepare_split(_vq);
1827 EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
1830 * virtqueue_notify - second half of split virtqueue_kick call.
1831 * @vq: the struct virtqueue
1833 * This does not need to be serialized.
1835 * Returns false if host notify failed or queue is broken, otherwise true.
1837 bool virtqueue_notify(struct virtqueue *_vq)
1839 struct vring_virtqueue *vq = to_vvq(_vq);
1841 if (unlikely(vq->broken))
1844 /* Prod other side to tell it about changes. */
1845 if (!vq->notify(_vq)) {
1851 EXPORT_SYMBOL_GPL(virtqueue_notify);
1854 * virtqueue_kick - update after add_buf
1855 * @vq: the struct virtqueue
1857 * After one or more virtqueue_add_* calls, invoke this to kick
1860 * Caller must ensure we don't call this with other virtqueue
1861 * operations at the same time (except where noted).
1863 * Returns false if kick failed, otherwise true.
1865 bool virtqueue_kick(struct virtqueue *vq)
1867 if (virtqueue_kick_prepare(vq))
1868 return virtqueue_notify(vq);
1871 EXPORT_SYMBOL_GPL(virtqueue_kick);
1874 * virtqueue_get_buf - get the next used buffer
1875 * @vq: the struct virtqueue we're talking about.
1876 * @len: the length written into the buffer
1878 * If the device wrote data into the buffer, @len will be set to the
1879 * amount written. This means you don't need to clear the buffer
1880 * beforehand to ensure there's no data leakage in the case of short
1883 * Caller must ensure we don't call this with other virtqueue
1884 * operations at the same time (except where noted).
1886 * Returns NULL if there are no used buffers, or the "data" token
1887 * handed to virtqueue_add_*().
1889 void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
1892 struct vring_virtqueue *vq = to_vvq(_vq);
1894 return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
1895 virtqueue_get_buf_ctx_split(_vq, len, ctx);
1897 EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
1899 void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
1901 return virtqueue_get_buf_ctx(_vq, len, NULL);
1903 EXPORT_SYMBOL_GPL(virtqueue_get_buf);
1905 * virtqueue_disable_cb - disable callbacks
1906 * @vq: the struct virtqueue we're talking about.
1908 * Note that this is not necessarily synchronous, hence unreliable and only
1909 * useful as an optimization.
1911 * Unlike other operations, this need not be serialized.
1913 void virtqueue_disable_cb(struct virtqueue *_vq)
1915 struct vring_virtqueue *vq = to_vvq(_vq);
1917 if (vq->packed_ring)
1918 virtqueue_disable_cb_packed(_vq);
1920 virtqueue_disable_cb_split(_vq);
1922 EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
1925 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
1926 * @vq: the struct virtqueue we're talking about.
1928 * This re-enables callbacks; it returns current queue state
1929 * in an opaque unsigned value. This value should be later tested by
1930 * virtqueue_poll, to detect a possible race between the driver checking for
1931 * more work, and enabling callbacks.
1933 * Caller must ensure we don't call this with other virtqueue
1934 * operations at the same time (except where noted).
1936 unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
1938 struct vring_virtqueue *vq = to_vvq(_vq);
1940 return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
1941 virtqueue_enable_cb_prepare_split(_vq);
1943 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
1946 * virtqueue_poll - query pending used buffers
1947 * @vq: the struct virtqueue we're talking about.
1948 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
1950 * Returns "true" if there are pending used buffers in the queue.
1952 * This does not need to be serialized.
1954 bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
1956 struct vring_virtqueue *vq = to_vvq(_vq);
1958 virtio_mb(vq->weak_barriers);
1959 return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
1960 virtqueue_poll_split(_vq, last_used_idx);
1962 EXPORT_SYMBOL_GPL(virtqueue_poll);
1965 * virtqueue_enable_cb - restart callbacks after disable_cb.
1966 * @vq: the struct virtqueue we're talking about.
1968 * This re-enables callbacks; it returns "false" if there are pending
1969 * buffers in the queue, to detect a possible race between the driver
1970 * checking for more work, and enabling callbacks.
1972 * Caller must ensure we don't call this with other virtqueue
1973 * operations at the same time (except where noted).
1975 bool virtqueue_enable_cb(struct virtqueue *_vq)
1977 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
1979 return !virtqueue_poll(_vq, last_used_idx);
1981 EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
1984 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
1985 * @vq: the struct virtqueue we're talking about.
1987 * This re-enables callbacks but hints to the other side to delay
1988 * interrupts until most of the available buffers have been processed;
1989 * it returns "false" if there are many pending buffers in the queue,
1990 * to detect a possible race between the driver checking for more work,
1991 * and enabling callbacks.
1993 * Caller must ensure we don't call this with other virtqueue
1994 * operations at the same time (except where noted).
1996 bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
1998 struct vring_virtqueue *vq = to_vvq(_vq);
2000 return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
2001 virtqueue_enable_cb_delayed_split(_vq);
2003 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2006 * virtqueue_detach_unused_buf - detach first unused buffer
2007 * @vq: the struct virtqueue we're talking about.
2009 * Returns NULL or the "data" token handed to virtqueue_add_*().
2010 * This is not valid on an active queue; it is useful only for device
2013 void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2015 struct vring_virtqueue *vq = to_vvq(_vq);
2017 return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
2018 virtqueue_detach_unused_buf_split(_vq);
2020 EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
2022 static inline bool more_used(const struct vring_virtqueue *vq)
2024 return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
2027 irqreturn_t vring_interrupt(int irq, void *_vq)
2029 struct vring_virtqueue *vq = to_vvq(_vq);
2031 if (!more_used(vq)) {
2032 pr_debug("virtqueue interrupt with no work for %p\n", vq);
2036 if (unlikely(vq->broken))
2039 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
2040 if (vq->vq.callback)
2041 vq->vq.callback(&vq->vq);
2045 EXPORT_SYMBOL_GPL(vring_interrupt);
2047 /* Only available for split ring */
2048 struct virtqueue *__vring_new_virtqueue(unsigned int index,
2050 struct virtio_device *vdev,
2053 bool (*notify)(struct virtqueue *),
2054 void (*callback)(struct virtqueue *),
2058 struct vring_virtqueue *vq;
2060 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2063 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
2067 vq->packed_ring = false;
2068 vq->vq.callback = callback;
2071 vq->vq.num_free = vring.num;
2072 vq->vq.index = index;
2073 vq->we_own_ring = false;
2074 vq->notify = notify;
2075 vq->weak_barriers = weak_barriers;
2077 vq->last_used_idx = 0;
2079 vq->use_dma_api = vring_use_dma_api(vdev);
2080 list_add_tail(&vq->vq.list, &vdev->vqs);
2083 vq->last_add_time_valid = false;
2086 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
2088 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
2090 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
2091 vq->weak_barriers = false;
2093 vq->split.queue_dma_addr = 0;
2094 vq->split.queue_size_in_bytes = 0;
2096 vq->split.vring = vring;
2097 vq->split.avail_flags_shadow = 0;
2098 vq->split.avail_idx_shadow = 0;
2100 /* No callback? Tell other side not to bother us. */
2102 vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
2104 vq->split.vring.avail->flags = cpu_to_virtio16(vdev,
2105 vq->split.avail_flags_shadow);
2108 vq->split.desc_state = kmalloc_array(vring.num,
2109 sizeof(struct vring_desc_state_split), GFP_KERNEL);
2110 if (!vq->split.desc_state) {
2115 /* Put everything in free lists. */
2117 for (i = 0; i < vring.num-1; i++)
2118 vq->split.vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
2119 memset(vq->split.desc_state, 0, vring.num *
2120 sizeof(struct vring_desc_state_split));
2124 EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
2126 struct virtqueue *vring_create_virtqueue(
2129 unsigned int vring_align,
2130 struct virtio_device *vdev,
2132 bool may_reduce_num,
2134 bool (*notify)(struct virtqueue *),
2135 void (*callback)(struct virtqueue *),
2139 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2140 return vring_create_virtqueue_packed(index, num, vring_align,
2141 vdev, weak_barriers, may_reduce_num,
2142 context, notify, callback, name);
2144 return vring_create_virtqueue_split(index, num, vring_align,
2145 vdev, weak_barriers, may_reduce_num,
2146 context, notify, callback, name);
2148 EXPORT_SYMBOL_GPL(vring_create_virtqueue);
2150 /* Only available for split ring */
2151 struct virtqueue *vring_new_virtqueue(unsigned int index,
2153 unsigned int vring_align,
2154 struct virtio_device *vdev,
2158 bool (*notify)(struct virtqueue *vq),
2159 void (*callback)(struct virtqueue *vq),
2164 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2167 vring_init(&vring, num, pages, vring_align);
2168 return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
2169 notify, callback, name);
2171 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
2173 void vring_del_virtqueue(struct virtqueue *_vq)
2175 struct vring_virtqueue *vq = to_vvq(_vq);
2177 if (vq->we_own_ring) {
2178 if (vq->packed_ring) {
2179 vring_free_queue(vq->vq.vdev,
2180 vq->packed.ring_size_in_bytes,
2181 vq->packed.vring.desc,
2182 vq->packed.ring_dma_addr);
2184 vring_free_queue(vq->vq.vdev,
2185 vq->packed.event_size_in_bytes,
2186 vq->packed.vring.driver,
2187 vq->packed.driver_event_dma_addr);
2189 vring_free_queue(vq->vq.vdev,
2190 vq->packed.event_size_in_bytes,
2191 vq->packed.vring.device,
2192 vq->packed.device_event_dma_addr);
2194 kfree(vq->packed.desc_state);
2195 kfree(vq->packed.desc_extra);
2197 vring_free_queue(vq->vq.vdev,
2198 vq->split.queue_size_in_bytes,
2199 vq->split.vring.desc,
2200 vq->split.queue_dma_addr);
2202 kfree(vq->split.desc_state);
2205 list_del(&_vq->list);
2208 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
2210 /* Manipulates transport-specific feature bits. */
2211 void vring_transport_features(struct virtio_device *vdev)
2215 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2217 case VIRTIO_RING_F_INDIRECT_DESC:
2219 case VIRTIO_RING_F_EVENT_IDX:
2221 case VIRTIO_F_VERSION_1:
2223 case VIRTIO_F_IOMMU_PLATFORM:
2225 case VIRTIO_F_RING_PACKED:
2227 case VIRTIO_F_ORDER_PLATFORM:
2230 /* We don't understand this bit. */
2231 __virtio_clear_bit(vdev, i);
2235 EXPORT_SYMBOL_GPL(vring_transport_features);
2238 * virtqueue_get_vring_size - return the size of the virtqueue's vring
2239 * @vq: the struct virtqueue containing the vring of interest.
2241 * Returns the size of the vring. This is mainly used for boasting to
2242 * userspace. Unlike other operations, this need not be serialized.
2244 unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
2247 struct vring_virtqueue *vq = to_vvq(_vq);
2249 return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
2251 EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
2253 bool virtqueue_is_broken(struct virtqueue *_vq)
2255 struct vring_virtqueue *vq = to_vvq(_vq);
2259 EXPORT_SYMBOL_GPL(virtqueue_is_broken);
2262 * This should prevent the device from being used, allowing drivers to
2263 * recover. You may need to grab appropriate locks to flush.
2265 void virtio_break_device(struct virtio_device *dev)
2267 struct virtqueue *_vq;
2269 list_for_each_entry(_vq, &dev->vqs, list) {
2270 struct vring_virtqueue *vq = to_vvq(_vq);
2274 EXPORT_SYMBOL_GPL(virtio_break_device);
2276 dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
2278 struct vring_virtqueue *vq = to_vvq(_vq);
2280 BUG_ON(!vq->we_own_ring);
2282 if (vq->packed_ring)
2283 return vq->packed.ring_dma_addr;
2285 return vq->split.queue_dma_addr;
2287 EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
2289 dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
2291 struct vring_virtqueue *vq = to_vvq(_vq);
2293 BUG_ON(!vq->we_own_ring);
2295 if (vq->packed_ring)
2296 return vq->packed.driver_event_dma_addr;
2298 return vq->split.queue_dma_addr +
2299 ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
2301 EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
2303 dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
2305 struct vring_virtqueue *vq = to_vvq(_vq);
2307 BUG_ON(!vq->we_own_ring);
2309 if (vq->packed_ring)
2310 return vq->packed.device_event_dma_addr;
2312 return vq->split.queue_dma_addr +
2313 ((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
2315 EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
2317 /* Only available for split ring */
2318 const struct vring *virtqueue_get_vring(struct virtqueue *vq)
2320 return &to_vvq(vq)->split.vring;
2322 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
2324 MODULE_LICENSE("GPL");