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 size_t virtio_max_dma_size(struct virtio_device *vdev)
276 size_t max_segment_size = SIZE_MAX;
278 if (vring_use_dma_api(vdev))
279 max_segment_size = dma_max_mapping_size(&vdev->dev);
281 return max_segment_size;
283 EXPORT_SYMBOL_GPL(virtio_max_dma_size);
285 static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
286 dma_addr_t *dma_handle, gfp_t flag)
288 if (vring_use_dma_api(vdev)) {
289 return dma_alloc_coherent(vdev->dev.parent, size,
292 void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
295 phys_addr_t phys_addr = virt_to_phys(queue);
296 *dma_handle = (dma_addr_t)phys_addr;
299 * Sanity check: make sure we dind't truncate
300 * the address. The only arches I can find that
301 * have 64-bit phys_addr_t but 32-bit dma_addr_t
302 * are certain non-highmem MIPS and x86
303 * configurations, but these configurations
304 * should never allocate physical pages above 32
305 * bits, so this is fine. Just in case, throw a
306 * warning and abort if we end up with an
307 * unrepresentable address.
309 if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
310 free_pages_exact(queue, PAGE_ALIGN(size));
318 static void vring_free_queue(struct virtio_device *vdev, size_t size,
319 void *queue, dma_addr_t dma_handle)
321 if (vring_use_dma_api(vdev))
322 dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
324 free_pages_exact(queue, PAGE_ALIGN(size));
328 * The DMA ops on various arches are rather gnarly right now, and
329 * making all of the arch DMA ops work on the vring device itself
330 * is a mess. For now, we use the parent device for DMA ops.
332 static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq)
334 return vq->vq.vdev->dev.parent;
337 /* Map one sg entry. */
338 static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
339 struct scatterlist *sg,
340 enum dma_data_direction direction)
342 if (!vq->use_dma_api)
343 return (dma_addr_t)sg_phys(sg);
346 * We can't use dma_map_sg, because we don't use scatterlists in
347 * the way it expects (we don't guarantee that the scatterlist
348 * will exist for the lifetime of the mapping).
350 return dma_map_page(vring_dma_dev(vq),
351 sg_page(sg), sg->offset, sg->length,
355 static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
356 void *cpu_addr, size_t size,
357 enum dma_data_direction direction)
359 if (!vq->use_dma_api)
360 return (dma_addr_t)virt_to_phys(cpu_addr);
362 return dma_map_single(vring_dma_dev(vq),
363 cpu_addr, size, direction);
366 static int vring_mapping_error(const struct vring_virtqueue *vq,
369 if (!vq->use_dma_api)
372 return dma_mapping_error(vring_dma_dev(vq), addr);
377 * Split ring specific functions - *_split().
380 static void vring_unmap_one_split(const struct vring_virtqueue *vq,
381 struct vring_desc *desc)
385 if (!vq->use_dma_api)
388 flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
390 if (flags & VRING_DESC_F_INDIRECT) {
391 dma_unmap_single(vring_dma_dev(vq),
392 virtio64_to_cpu(vq->vq.vdev, desc->addr),
393 virtio32_to_cpu(vq->vq.vdev, desc->len),
394 (flags & VRING_DESC_F_WRITE) ?
395 DMA_FROM_DEVICE : DMA_TO_DEVICE);
397 dma_unmap_page(vring_dma_dev(vq),
398 virtio64_to_cpu(vq->vq.vdev, desc->addr),
399 virtio32_to_cpu(vq->vq.vdev, desc->len),
400 (flags & VRING_DESC_F_WRITE) ?
401 DMA_FROM_DEVICE : DMA_TO_DEVICE);
405 static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
406 unsigned int total_sg,
409 struct vring_desc *desc;
413 * We require lowmem mappings for the descriptors because
414 * otherwise virt_to_phys will give us bogus addresses in the
417 gfp &= ~__GFP_HIGHMEM;
419 desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
423 for (i = 0; i < total_sg; i++)
424 desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
428 static inline int virtqueue_add_split(struct virtqueue *_vq,
429 struct scatterlist *sgs[],
430 unsigned int total_sg,
431 unsigned int out_sgs,
437 struct vring_virtqueue *vq = to_vvq(_vq);
438 struct scatterlist *sg;
439 struct vring_desc *desc;
440 unsigned int i, n, avail, descs_used, uninitialized_var(prev), err_idx;
446 BUG_ON(data == NULL);
447 BUG_ON(ctx && vq->indirect);
449 if (unlikely(vq->broken)) {
454 LAST_ADD_TIME_UPDATE(vq);
456 BUG_ON(total_sg == 0);
458 head = vq->free_head;
460 if (virtqueue_use_indirect(_vq, total_sg))
461 desc = alloc_indirect_split(_vq, total_sg, gfp);
464 WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
468 /* Use a single buffer which doesn't continue */
470 /* Set up rest to use this indirect table. */
475 desc = vq->split.vring.desc;
477 descs_used = total_sg;
480 if (vq->vq.num_free < descs_used) {
481 pr_debug("Can't add buf len %i - avail = %i\n",
482 descs_used, vq->vq.num_free);
483 /* FIXME: for historical reasons, we force a notify here if
484 * there are outgoing parts to the buffer. Presumably the
485 * host should service the ring ASAP. */
494 for (n = 0; n < out_sgs; n++) {
495 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
496 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
497 if (vring_mapping_error(vq, addr))
500 desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT);
501 desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
502 desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
504 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
507 for (; n < (out_sgs + in_sgs); n++) {
508 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
509 dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
510 if (vring_mapping_error(vq, addr))
513 desc[i].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE);
514 desc[i].addr = cpu_to_virtio64(_vq->vdev, addr);
515 desc[i].len = cpu_to_virtio32(_vq->vdev, sg->length);
517 i = virtio16_to_cpu(_vq->vdev, desc[i].next);
520 /* Last one doesn't continue. */
521 desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
524 /* Now that the indirect table is filled in, map it. */
525 dma_addr_t addr = vring_map_single(
526 vq, desc, total_sg * sizeof(struct vring_desc),
528 if (vring_mapping_error(vq, addr))
531 vq->split.vring.desc[head].flags = cpu_to_virtio16(_vq->vdev,
532 VRING_DESC_F_INDIRECT);
533 vq->split.vring.desc[head].addr = cpu_to_virtio64(_vq->vdev,
536 vq->split.vring.desc[head].len = cpu_to_virtio32(_vq->vdev,
537 total_sg * sizeof(struct vring_desc));
540 /* We're using some buffers from the free list. */
541 vq->vq.num_free -= descs_used;
543 /* Update free pointer */
545 vq->free_head = virtio16_to_cpu(_vq->vdev,
546 vq->split.vring.desc[head].next);
550 /* Store token and indirect buffer state. */
551 vq->split.desc_state[head].data = data;
553 vq->split.desc_state[head].indir_desc = desc;
555 vq->split.desc_state[head].indir_desc = ctx;
557 /* Put entry in available array (but don't update avail->idx until they
559 avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
560 vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
562 /* Descriptors and available array need to be set before we expose the
563 * new available array entries. */
564 virtio_wmb(vq->weak_barriers);
565 vq->split.avail_idx_shadow++;
566 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
567 vq->split.avail_idx_shadow);
570 pr_debug("Added buffer head %i to %p\n", head, vq);
573 /* This is very unlikely, but theoretically possible. Kick
575 if (unlikely(vq->num_added == (1 << 16) - 1))
584 for (n = 0; n < total_sg; n++) {
587 vring_unmap_one_split(vq, &desc[i]);
588 i = virtio16_to_cpu(_vq->vdev, vq->split.vring.desc[i].next);
598 static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
600 struct vring_virtqueue *vq = to_vvq(_vq);
605 /* We need to expose available array entries before checking avail
607 virtio_mb(vq->weak_barriers);
609 old = vq->split.avail_idx_shadow - vq->num_added;
610 new = vq->split.avail_idx_shadow;
613 LAST_ADD_TIME_CHECK(vq);
614 LAST_ADD_TIME_INVALID(vq);
617 needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
618 vring_avail_event(&vq->split.vring)),
621 needs_kick = !(vq->split.vring.used->flags &
622 cpu_to_virtio16(_vq->vdev,
623 VRING_USED_F_NO_NOTIFY));
629 static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
633 __virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
635 /* Clear data ptr. */
636 vq->split.desc_state[head].data = NULL;
638 /* Put back on free list: unmap first-level descriptors and find end */
641 while (vq->split.vring.desc[i].flags & nextflag) {
642 vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
643 i = virtio16_to_cpu(vq->vq.vdev, vq->split.vring.desc[i].next);
647 vring_unmap_one_split(vq, &vq->split.vring.desc[i]);
648 vq->split.vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev,
650 vq->free_head = head;
652 /* Plus final descriptor */
656 struct vring_desc *indir_desc =
657 vq->split.desc_state[head].indir_desc;
660 /* Free the indirect table, if any, now that it's unmapped. */
664 len = virtio32_to_cpu(vq->vq.vdev,
665 vq->split.vring.desc[head].len);
667 BUG_ON(!(vq->split.vring.desc[head].flags &
668 cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_INDIRECT)));
669 BUG_ON(len == 0 || len % sizeof(struct vring_desc));
671 for (j = 0; j < len / sizeof(struct vring_desc); j++)
672 vring_unmap_one_split(vq, &indir_desc[j]);
675 vq->split.desc_state[head].indir_desc = NULL;
677 *ctx = vq->split.desc_state[head].indir_desc;
681 static inline bool more_used_split(const struct vring_virtqueue *vq)
683 return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
684 vq->split.vring.used->idx);
687 static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
691 struct vring_virtqueue *vq = to_vvq(_vq);
698 if (unlikely(vq->broken)) {
703 if (!more_used_split(vq)) {
704 pr_debug("No more buffers in queue\n");
709 /* Only get used array entries after they have been exposed by host. */
710 virtio_rmb(vq->weak_barriers);
712 last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
713 i = virtio32_to_cpu(_vq->vdev,
714 vq->split.vring.used->ring[last_used].id);
715 *len = virtio32_to_cpu(_vq->vdev,
716 vq->split.vring.used->ring[last_used].len);
718 if (unlikely(i >= vq->split.vring.num)) {
719 BAD_RING(vq, "id %u out of range\n", i);
722 if (unlikely(!vq->split.desc_state[i].data)) {
723 BAD_RING(vq, "id %u is not a head!\n", i);
727 /* detach_buf_split clears data, so grab it now. */
728 ret = vq->split.desc_state[i].data;
729 detach_buf_split(vq, i, ctx);
731 /* If we expect an interrupt for the next entry, tell host
732 * by writing event index and flush out the write before
733 * the read in the next get_buf call. */
734 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
735 virtio_store_mb(vq->weak_barriers,
736 &vring_used_event(&vq->split.vring),
737 cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
739 LAST_ADD_TIME_INVALID(vq);
745 static void virtqueue_disable_cb_split(struct virtqueue *_vq)
747 struct vring_virtqueue *vq = to_vvq(_vq);
749 if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
750 vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
752 vq->split.vring.avail->flags =
753 cpu_to_virtio16(_vq->vdev,
754 vq->split.avail_flags_shadow);
758 static unsigned virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
760 struct vring_virtqueue *vq = to_vvq(_vq);
765 /* We optimistically turn back on interrupts, then check if there was
767 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
768 * either clear the flags bit or point the event index at the next
769 * entry. Always do both to keep code simple. */
770 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
771 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
773 vq->split.vring.avail->flags =
774 cpu_to_virtio16(_vq->vdev,
775 vq->split.avail_flags_shadow);
777 vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
778 last_used_idx = vq->last_used_idx);
780 return last_used_idx;
783 static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned last_used_idx)
785 struct vring_virtqueue *vq = to_vvq(_vq);
787 return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
788 vq->split.vring.used->idx);
791 static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
793 struct vring_virtqueue *vq = to_vvq(_vq);
798 /* We optimistically turn back on interrupts, then check if there was
800 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
801 * either clear the flags bit or point the event index at the next
802 * entry. Always update the event index to keep code simple. */
803 if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
804 vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
806 vq->split.vring.avail->flags =
807 cpu_to_virtio16(_vq->vdev,
808 vq->split.avail_flags_shadow);
810 /* TODO: tune this threshold */
811 bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
813 virtio_store_mb(vq->weak_barriers,
814 &vring_used_event(&vq->split.vring),
815 cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
817 if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
818 - vq->last_used_idx) > bufs)) {
827 static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
829 struct vring_virtqueue *vq = to_vvq(_vq);
835 for (i = 0; i < vq->split.vring.num; i++) {
836 if (!vq->split.desc_state[i].data)
838 /* detach_buf_split clears data, so grab it now. */
839 buf = vq->split.desc_state[i].data;
840 detach_buf_split(vq, i, NULL);
841 vq->split.avail_idx_shadow--;
842 vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
843 vq->split.avail_idx_shadow);
847 /* That should have freed everything. */
848 BUG_ON(vq->vq.num_free != vq->split.vring.num);
854 static struct virtqueue *vring_create_virtqueue_split(
857 unsigned int vring_align,
858 struct virtio_device *vdev,
862 bool (*notify)(struct virtqueue *),
863 void (*callback)(struct virtqueue *),
866 struct virtqueue *vq;
869 size_t queue_size_in_bytes;
872 /* We assume num is a power of 2. */
873 if (num & (num - 1)) {
874 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
878 /* TODO: allocate each queue chunk individually */
879 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
880 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
882 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
893 /* Try to get a single page. You are my only hope! */
894 queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
895 &dma_addr, GFP_KERNEL|__GFP_ZERO);
900 queue_size_in_bytes = vring_size(num, vring_align);
901 vring_init(&vring, num, queue, vring_align);
903 vq = __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
904 notify, callback, name);
906 vring_free_queue(vdev, queue_size_in_bytes, queue,
911 to_vvq(vq)->split.queue_dma_addr = dma_addr;
912 to_vvq(vq)->split.queue_size_in_bytes = queue_size_in_bytes;
913 to_vvq(vq)->we_own_ring = true;
920 * Packed ring specific functions - *_packed().
923 static void vring_unmap_state_packed(const struct vring_virtqueue *vq,
924 struct vring_desc_extra_packed *state)
928 if (!vq->use_dma_api)
931 flags = state->flags;
933 if (flags & VRING_DESC_F_INDIRECT) {
934 dma_unmap_single(vring_dma_dev(vq),
935 state->addr, state->len,
936 (flags & VRING_DESC_F_WRITE) ?
937 DMA_FROM_DEVICE : DMA_TO_DEVICE);
939 dma_unmap_page(vring_dma_dev(vq),
940 state->addr, state->len,
941 (flags & VRING_DESC_F_WRITE) ?
942 DMA_FROM_DEVICE : DMA_TO_DEVICE);
946 static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
947 struct vring_packed_desc *desc)
951 if (!vq->use_dma_api)
954 flags = le16_to_cpu(desc->flags);
956 if (flags & VRING_DESC_F_INDIRECT) {
957 dma_unmap_single(vring_dma_dev(vq),
958 le64_to_cpu(desc->addr),
959 le32_to_cpu(desc->len),
960 (flags & VRING_DESC_F_WRITE) ?
961 DMA_FROM_DEVICE : DMA_TO_DEVICE);
963 dma_unmap_page(vring_dma_dev(vq),
964 le64_to_cpu(desc->addr),
965 le32_to_cpu(desc->len),
966 (flags & VRING_DESC_F_WRITE) ?
967 DMA_FROM_DEVICE : DMA_TO_DEVICE);
971 static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
974 struct vring_packed_desc *desc;
977 * We require lowmem mappings for the descriptors because
978 * otherwise virt_to_phys will give us bogus addresses in the
981 gfp &= ~__GFP_HIGHMEM;
983 desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
988 static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
989 struct scatterlist *sgs[],
990 unsigned int total_sg,
991 unsigned int out_sgs,
996 struct vring_packed_desc *desc;
997 struct scatterlist *sg;
998 unsigned int i, n, err_idx;
1002 head = vq->packed.next_avail_idx;
1003 desc = alloc_indirect_packed(total_sg, gfp);
1005 if (unlikely(vq->vq.num_free < 1)) {
1006 pr_debug("Can't add buf len 1 - avail = 0\n");
1013 BUG_ON(id == vq->packed.vring.num);
1015 for (n = 0; n < out_sgs + in_sgs; n++) {
1016 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1017 addr = vring_map_one_sg(vq, sg, n < out_sgs ?
1018 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1019 if (vring_mapping_error(vq, addr))
1022 desc[i].flags = cpu_to_le16(n < out_sgs ?
1023 0 : VRING_DESC_F_WRITE);
1024 desc[i].addr = cpu_to_le64(addr);
1025 desc[i].len = cpu_to_le32(sg->length);
1030 /* Now that the indirect table is filled in, map it. */
1031 addr = vring_map_single(vq, desc,
1032 total_sg * sizeof(struct vring_packed_desc),
1034 if (vring_mapping_error(vq, addr))
1037 vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
1038 vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
1039 sizeof(struct vring_packed_desc));
1040 vq->packed.vring.desc[head].id = cpu_to_le16(id);
1042 if (vq->use_dma_api) {
1043 vq->packed.desc_extra[id].addr = addr;
1044 vq->packed.desc_extra[id].len = total_sg *
1045 sizeof(struct vring_packed_desc);
1046 vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
1047 vq->packed.avail_used_flags;
1051 * A driver MUST NOT make the first descriptor in the list
1052 * available before all subsequent descriptors comprising
1053 * the list are made available.
1055 virtio_wmb(vq->weak_barriers);
1056 vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
1057 vq->packed.avail_used_flags);
1059 /* We're using some buffers from the free list. */
1060 vq->vq.num_free -= 1;
1062 /* Update free pointer */
1064 if (n >= vq->packed.vring.num) {
1066 vq->packed.avail_wrap_counter ^= 1;
1067 vq->packed.avail_used_flags ^=
1068 1 << VRING_PACKED_DESC_F_AVAIL |
1069 1 << VRING_PACKED_DESC_F_USED;
1071 vq->packed.next_avail_idx = n;
1072 vq->free_head = vq->packed.desc_state[id].next;
1074 /* Store token and indirect buffer state. */
1075 vq->packed.desc_state[id].num = 1;
1076 vq->packed.desc_state[id].data = data;
1077 vq->packed.desc_state[id].indir_desc = desc;
1078 vq->packed.desc_state[id].last = id;
1082 pr_debug("Added buffer head %i to %p\n", head, vq);
1090 for (i = 0; i < err_idx; i++)
1091 vring_unmap_desc_packed(vq, &desc[i]);
1099 static inline int virtqueue_add_packed(struct virtqueue *_vq,
1100 struct scatterlist *sgs[],
1101 unsigned int total_sg,
1102 unsigned int out_sgs,
1103 unsigned int in_sgs,
1108 struct vring_virtqueue *vq = to_vvq(_vq);
1109 struct vring_packed_desc *desc;
1110 struct scatterlist *sg;
1111 unsigned int i, n, c, descs_used, err_idx;
1112 __le16 uninitialized_var(head_flags), flags;
1113 u16 head, id, uninitialized_var(prev), curr, avail_used_flags;
1117 BUG_ON(data == NULL);
1118 BUG_ON(ctx && vq->indirect);
1120 if (unlikely(vq->broken)) {
1125 LAST_ADD_TIME_UPDATE(vq);
1127 BUG_ON(total_sg == 0);
1129 if (virtqueue_use_indirect(_vq, total_sg))
1130 return virtqueue_add_indirect_packed(vq, sgs, total_sg,
1131 out_sgs, in_sgs, data, gfp);
1133 head = vq->packed.next_avail_idx;
1134 avail_used_flags = vq->packed.avail_used_flags;
1136 WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
1138 desc = vq->packed.vring.desc;
1140 descs_used = total_sg;
1142 if (unlikely(vq->vq.num_free < descs_used)) {
1143 pr_debug("Can't add buf len %i - avail = %i\n",
1144 descs_used, vq->vq.num_free);
1150 BUG_ON(id == vq->packed.vring.num);
1154 for (n = 0; n < out_sgs + in_sgs; n++) {
1155 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
1156 dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ?
1157 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1158 if (vring_mapping_error(vq, addr))
1161 flags = cpu_to_le16(vq->packed.avail_used_flags |
1162 (++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
1163 (n < out_sgs ? 0 : VRING_DESC_F_WRITE));
1167 desc[i].flags = flags;
1169 desc[i].addr = cpu_to_le64(addr);
1170 desc[i].len = cpu_to_le32(sg->length);
1171 desc[i].id = cpu_to_le16(id);
1173 if (unlikely(vq->use_dma_api)) {
1174 vq->packed.desc_extra[curr].addr = addr;
1175 vq->packed.desc_extra[curr].len = sg->length;
1176 vq->packed.desc_extra[curr].flags =
1180 curr = vq->packed.desc_state[curr].next;
1182 if ((unlikely(++i >= vq->packed.vring.num))) {
1184 vq->packed.avail_used_flags ^=
1185 1 << VRING_PACKED_DESC_F_AVAIL |
1186 1 << VRING_PACKED_DESC_F_USED;
1192 vq->packed.avail_wrap_counter ^= 1;
1194 /* We're using some buffers from the free list. */
1195 vq->vq.num_free -= descs_used;
1197 /* Update free pointer */
1198 vq->packed.next_avail_idx = i;
1199 vq->free_head = curr;
1202 vq->packed.desc_state[id].num = descs_used;
1203 vq->packed.desc_state[id].data = data;
1204 vq->packed.desc_state[id].indir_desc = ctx;
1205 vq->packed.desc_state[id].last = prev;
1208 * A driver MUST NOT make the first descriptor in the list
1209 * available before all subsequent descriptors comprising
1210 * the list are made available.
1212 virtio_wmb(vq->weak_barriers);
1213 vq->packed.vring.desc[head].flags = head_flags;
1214 vq->num_added += descs_used;
1216 pr_debug("Added buffer head %i to %p\n", head, vq);
1225 vq->packed.avail_used_flags = avail_used_flags;
1227 for (n = 0; n < total_sg; n++) {
1230 vring_unmap_desc_packed(vq, &desc[i]);
1232 if (i >= vq->packed.vring.num)
1240 static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
1242 struct vring_virtqueue *vq = to_vvq(_vq);
1243 u16 new, old, off_wrap, flags, wrap_counter, event_idx;
1256 * We need to expose the new flags value before checking notification
1259 virtio_mb(vq->weak_barriers);
1261 old = vq->packed.next_avail_idx - vq->num_added;
1262 new = vq->packed.next_avail_idx;
1265 snapshot.u32 = *(u32 *)vq->packed.vring.device;
1266 flags = le16_to_cpu(snapshot.flags);
1268 LAST_ADD_TIME_CHECK(vq);
1269 LAST_ADD_TIME_INVALID(vq);
1271 if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
1272 needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
1276 off_wrap = le16_to_cpu(snapshot.off_wrap);
1278 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1279 event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1280 if (wrap_counter != vq->packed.avail_wrap_counter)
1281 event_idx -= vq->packed.vring.num;
1283 needs_kick = vring_need_event(event_idx, new, old);
1289 static void detach_buf_packed(struct vring_virtqueue *vq,
1290 unsigned int id, void **ctx)
1292 struct vring_desc_state_packed *state = NULL;
1293 struct vring_packed_desc *desc;
1294 unsigned int i, curr;
1296 state = &vq->packed.desc_state[id];
1298 /* Clear data ptr. */
1301 vq->packed.desc_state[state->last].next = vq->free_head;
1303 vq->vq.num_free += state->num;
1305 if (unlikely(vq->use_dma_api)) {
1307 for (i = 0; i < state->num; i++) {
1308 vring_unmap_state_packed(vq,
1309 &vq->packed.desc_extra[curr]);
1310 curr = vq->packed.desc_state[curr].next;
1317 /* Free the indirect table, if any, now that it's unmapped. */
1318 desc = state->indir_desc;
1322 if (vq->use_dma_api) {
1323 len = vq->packed.desc_extra[id].len;
1324 for (i = 0; i < len / sizeof(struct vring_packed_desc);
1326 vring_unmap_desc_packed(vq, &desc[i]);
1329 state->indir_desc = NULL;
1331 *ctx = state->indir_desc;
1335 static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
1336 u16 idx, bool used_wrap_counter)
1341 flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
1342 avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
1343 used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
1345 return avail == used && used == used_wrap_counter;
1348 static inline bool more_used_packed(const struct vring_virtqueue *vq)
1350 return is_used_desc_packed(vq, vq->last_used_idx,
1351 vq->packed.used_wrap_counter);
1354 static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
1358 struct vring_virtqueue *vq = to_vvq(_vq);
1364 if (unlikely(vq->broken)) {
1369 if (!more_used_packed(vq)) {
1370 pr_debug("No more buffers in queue\n");
1375 /* Only get used elements after they have been exposed by host. */
1376 virtio_rmb(vq->weak_barriers);
1378 last_used = vq->last_used_idx;
1379 id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
1380 *len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
1382 if (unlikely(id >= vq->packed.vring.num)) {
1383 BAD_RING(vq, "id %u out of range\n", id);
1386 if (unlikely(!vq->packed.desc_state[id].data)) {
1387 BAD_RING(vq, "id %u is not a head!\n", id);
1391 /* detach_buf_packed clears data, so grab it now. */
1392 ret = vq->packed.desc_state[id].data;
1393 detach_buf_packed(vq, id, ctx);
1395 vq->last_used_idx += vq->packed.desc_state[id].num;
1396 if (unlikely(vq->last_used_idx >= vq->packed.vring.num)) {
1397 vq->last_used_idx -= vq->packed.vring.num;
1398 vq->packed.used_wrap_counter ^= 1;
1402 * If we expect an interrupt for the next entry, tell host
1403 * by writing event index and flush out the write before
1404 * the read in the next get_buf call.
1406 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
1407 virtio_store_mb(vq->weak_barriers,
1408 &vq->packed.vring.driver->off_wrap,
1409 cpu_to_le16(vq->last_used_idx |
1410 (vq->packed.used_wrap_counter <<
1411 VRING_PACKED_EVENT_F_WRAP_CTR)));
1413 LAST_ADD_TIME_INVALID(vq);
1419 static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
1421 struct vring_virtqueue *vq = to_vvq(_vq);
1423 if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
1424 vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1425 vq->packed.vring.driver->flags =
1426 cpu_to_le16(vq->packed.event_flags_shadow);
1430 static unsigned virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
1432 struct vring_virtqueue *vq = to_vvq(_vq);
1437 * We optimistically turn back on interrupts, then check if there was
1442 vq->packed.vring.driver->off_wrap =
1443 cpu_to_le16(vq->last_used_idx |
1444 (vq->packed.used_wrap_counter <<
1445 VRING_PACKED_EVENT_F_WRAP_CTR));
1447 * We need to update event offset and event wrap
1448 * counter first before updating event flags.
1450 virtio_wmb(vq->weak_barriers);
1453 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1454 vq->packed.event_flags_shadow = vq->event ?
1455 VRING_PACKED_EVENT_FLAG_DESC :
1456 VRING_PACKED_EVENT_FLAG_ENABLE;
1457 vq->packed.vring.driver->flags =
1458 cpu_to_le16(vq->packed.event_flags_shadow);
1462 return vq->last_used_idx | ((u16)vq->packed.used_wrap_counter <<
1463 VRING_PACKED_EVENT_F_WRAP_CTR);
1466 static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
1468 struct vring_virtqueue *vq = to_vvq(_vq);
1472 wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
1473 used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
1475 return is_used_desc_packed(vq, used_idx, wrap_counter);
1478 static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
1480 struct vring_virtqueue *vq = to_vvq(_vq);
1481 u16 used_idx, wrap_counter;
1487 * We optimistically turn back on interrupts, then check if there was
1492 /* TODO: tune this threshold */
1493 bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
1494 wrap_counter = vq->packed.used_wrap_counter;
1496 used_idx = vq->last_used_idx + bufs;
1497 if (used_idx >= vq->packed.vring.num) {
1498 used_idx -= vq->packed.vring.num;
1502 vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
1503 (wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
1506 * We need to update event offset and event wrap
1507 * counter first before updating event flags.
1509 virtio_wmb(vq->weak_barriers);
1511 used_idx = vq->last_used_idx;
1512 wrap_counter = vq->packed.used_wrap_counter;
1515 if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
1516 vq->packed.event_flags_shadow = vq->event ?
1517 VRING_PACKED_EVENT_FLAG_DESC :
1518 VRING_PACKED_EVENT_FLAG_ENABLE;
1519 vq->packed.vring.driver->flags =
1520 cpu_to_le16(vq->packed.event_flags_shadow);
1524 * We need to update event suppression structure first
1525 * before re-checking for more used buffers.
1527 virtio_mb(vq->weak_barriers);
1529 if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
1538 static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
1540 struct vring_virtqueue *vq = to_vvq(_vq);
1546 for (i = 0; i < vq->packed.vring.num; i++) {
1547 if (!vq->packed.desc_state[i].data)
1549 /* detach_buf clears data, so grab it now. */
1550 buf = vq->packed.desc_state[i].data;
1551 detach_buf_packed(vq, i, NULL);
1555 /* That should have freed everything. */
1556 BUG_ON(vq->vq.num_free != vq->packed.vring.num);
1562 static struct virtqueue *vring_create_virtqueue_packed(
1565 unsigned int vring_align,
1566 struct virtio_device *vdev,
1568 bool may_reduce_num,
1570 bool (*notify)(struct virtqueue *),
1571 void (*callback)(struct virtqueue *),
1574 struct vring_virtqueue *vq;
1575 struct vring_packed_desc *ring;
1576 struct vring_packed_desc_event *driver, *device;
1577 dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
1578 size_t ring_size_in_bytes, event_size_in_bytes;
1581 ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
1583 ring = vring_alloc_queue(vdev, ring_size_in_bytes,
1585 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1589 event_size_in_bytes = sizeof(struct vring_packed_desc_event);
1591 driver = vring_alloc_queue(vdev, event_size_in_bytes,
1592 &driver_event_dma_addr,
1593 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1597 device = vring_alloc_queue(vdev, event_size_in_bytes,
1598 &device_event_dma_addr,
1599 GFP_KERNEL|__GFP_NOWARN|__GFP_ZERO);
1603 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
1607 vq->vq.callback = callback;
1610 vq->vq.num_free = num;
1611 vq->vq.index = index;
1612 vq->we_own_ring = true;
1613 vq->notify = notify;
1614 vq->weak_barriers = weak_barriers;
1616 vq->last_used_idx = 0;
1618 vq->packed_ring = true;
1619 vq->use_dma_api = vring_use_dma_api(vdev);
1620 list_add_tail(&vq->vq.list, &vdev->vqs);
1623 vq->last_add_time_valid = false;
1626 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
1628 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
1630 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
1631 vq->weak_barriers = false;
1633 vq->packed.ring_dma_addr = ring_dma_addr;
1634 vq->packed.driver_event_dma_addr = driver_event_dma_addr;
1635 vq->packed.device_event_dma_addr = device_event_dma_addr;
1637 vq->packed.ring_size_in_bytes = ring_size_in_bytes;
1638 vq->packed.event_size_in_bytes = event_size_in_bytes;
1640 vq->packed.vring.num = num;
1641 vq->packed.vring.desc = ring;
1642 vq->packed.vring.driver = driver;
1643 vq->packed.vring.device = device;
1645 vq->packed.next_avail_idx = 0;
1646 vq->packed.avail_wrap_counter = 1;
1647 vq->packed.used_wrap_counter = 1;
1648 vq->packed.event_flags_shadow = 0;
1649 vq->packed.avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
1651 vq->packed.desc_state = kmalloc_array(num,
1652 sizeof(struct vring_desc_state_packed),
1654 if (!vq->packed.desc_state)
1655 goto err_desc_state;
1657 memset(vq->packed.desc_state, 0,
1658 num * sizeof(struct vring_desc_state_packed));
1660 /* Put everything in free lists. */
1662 for (i = 0; i < num-1; i++)
1663 vq->packed.desc_state[i].next = i + 1;
1665 vq->packed.desc_extra = kmalloc_array(num,
1666 sizeof(struct vring_desc_extra_packed),
1668 if (!vq->packed.desc_extra)
1669 goto err_desc_extra;
1671 memset(vq->packed.desc_extra, 0,
1672 num * sizeof(struct vring_desc_extra_packed));
1674 /* No callback? Tell other side not to bother us. */
1676 vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
1677 vq->packed.vring.driver->flags =
1678 cpu_to_le16(vq->packed.event_flags_shadow);
1684 kfree(vq->packed.desc_state);
1688 vring_free_queue(vdev, event_size_in_bytes, device, ring_dma_addr);
1690 vring_free_queue(vdev, event_size_in_bytes, driver, ring_dma_addr);
1692 vring_free_queue(vdev, ring_size_in_bytes, ring, ring_dma_addr);
1699 * Generic functions and exported symbols.
1702 static inline int virtqueue_add(struct virtqueue *_vq,
1703 struct scatterlist *sgs[],
1704 unsigned int total_sg,
1705 unsigned int out_sgs,
1706 unsigned int in_sgs,
1711 struct vring_virtqueue *vq = to_vvq(_vq);
1713 return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
1714 out_sgs, in_sgs, data, ctx, gfp) :
1715 virtqueue_add_split(_vq, sgs, total_sg,
1716 out_sgs, in_sgs, data, ctx, gfp);
1720 * virtqueue_add_sgs - expose buffers to other end
1721 * @vq: the struct virtqueue we're talking about.
1722 * @sgs: array of terminated scatterlists.
1723 * @out_num: the number of scatterlists readable by other side
1724 * @in_num: the number of scatterlists which are writable (after readable ones)
1725 * @data: the token identifying the buffer.
1726 * @gfp: how to do memory allocations (if necessary).
1728 * Caller must ensure we don't call this with other virtqueue operations
1729 * at the same time (except where noted).
1731 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1733 int virtqueue_add_sgs(struct virtqueue *_vq,
1734 struct scatterlist *sgs[],
1735 unsigned int out_sgs,
1736 unsigned int in_sgs,
1740 unsigned int i, total_sg = 0;
1742 /* Count them first. */
1743 for (i = 0; i < out_sgs + in_sgs; i++) {
1744 struct scatterlist *sg;
1746 for (sg = sgs[i]; sg; sg = sg_next(sg))
1749 return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
1752 EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
1755 * virtqueue_add_outbuf - expose output buffers to other end
1756 * @vq: the struct virtqueue we're talking about.
1757 * @sg: scatterlist (must be well-formed and terminated!)
1758 * @num: the number of entries in @sg readable by other side
1759 * @data: the token identifying the buffer.
1760 * @gfp: how to do memory allocations (if necessary).
1762 * Caller must ensure we don't call this with other virtqueue operations
1763 * at the same time (except where noted).
1765 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1767 int virtqueue_add_outbuf(struct virtqueue *vq,
1768 struct scatterlist *sg, unsigned int num,
1772 return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
1774 EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
1777 * virtqueue_add_inbuf - expose input buffers to other end
1778 * @vq: the struct virtqueue we're talking about.
1779 * @sg: scatterlist (must be well-formed and terminated!)
1780 * @num: the number of entries in @sg writable by other side
1781 * @data: the token identifying the buffer.
1782 * @gfp: how to do memory allocations (if necessary).
1784 * Caller must ensure we don't call this with other virtqueue operations
1785 * at the same time (except where noted).
1787 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1789 int virtqueue_add_inbuf(struct virtqueue *vq,
1790 struct scatterlist *sg, unsigned int num,
1794 return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
1796 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
1799 * virtqueue_add_inbuf_ctx - expose input buffers to other end
1800 * @vq: the struct virtqueue we're talking about.
1801 * @sg: scatterlist (must be well-formed and terminated!)
1802 * @num: the number of entries in @sg writable by other side
1803 * @data: the token identifying the buffer.
1804 * @ctx: extra context for the token
1805 * @gfp: how to do memory allocations (if necessary).
1807 * Caller must ensure we don't call this with other virtqueue operations
1808 * at the same time (except where noted).
1810 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
1812 int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
1813 struct scatterlist *sg, unsigned int num,
1818 return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
1820 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
1823 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
1824 * @vq: the struct virtqueue
1826 * Instead of virtqueue_kick(), you can do:
1827 * if (virtqueue_kick_prepare(vq))
1828 * virtqueue_notify(vq);
1830 * This is sometimes useful because the virtqueue_kick_prepare() needs
1831 * to be serialized, but the actual virtqueue_notify() call does not.
1833 bool virtqueue_kick_prepare(struct virtqueue *_vq)
1835 struct vring_virtqueue *vq = to_vvq(_vq);
1837 return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
1838 virtqueue_kick_prepare_split(_vq);
1840 EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
1843 * virtqueue_notify - second half of split virtqueue_kick call.
1844 * @vq: the struct virtqueue
1846 * This does not need to be serialized.
1848 * Returns false if host notify failed or queue is broken, otherwise true.
1850 bool virtqueue_notify(struct virtqueue *_vq)
1852 struct vring_virtqueue *vq = to_vvq(_vq);
1854 if (unlikely(vq->broken))
1857 /* Prod other side to tell it about changes. */
1858 if (!vq->notify(_vq)) {
1864 EXPORT_SYMBOL_GPL(virtqueue_notify);
1867 * virtqueue_kick - update after add_buf
1868 * @vq: the struct virtqueue
1870 * After one or more virtqueue_add_* calls, invoke this to kick
1873 * Caller must ensure we don't call this with other virtqueue
1874 * operations at the same time (except where noted).
1876 * Returns false if kick failed, otherwise true.
1878 bool virtqueue_kick(struct virtqueue *vq)
1880 if (virtqueue_kick_prepare(vq))
1881 return virtqueue_notify(vq);
1884 EXPORT_SYMBOL_GPL(virtqueue_kick);
1887 * virtqueue_get_buf - get the next used buffer
1888 * @vq: the struct virtqueue we're talking about.
1889 * @len: the length written into the buffer
1891 * If the device wrote data into the buffer, @len will be set to the
1892 * amount written. This means you don't need to clear the buffer
1893 * beforehand to ensure there's no data leakage in the case of short
1896 * Caller must ensure we don't call this with other virtqueue
1897 * operations at the same time (except where noted).
1899 * Returns NULL if there are no used buffers, or the "data" token
1900 * handed to virtqueue_add_*().
1902 void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
1905 struct vring_virtqueue *vq = to_vvq(_vq);
1907 return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
1908 virtqueue_get_buf_ctx_split(_vq, len, ctx);
1910 EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
1912 void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
1914 return virtqueue_get_buf_ctx(_vq, len, NULL);
1916 EXPORT_SYMBOL_GPL(virtqueue_get_buf);
1918 * virtqueue_disable_cb - disable callbacks
1919 * @vq: the struct virtqueue we're talking about.
1921 * Note that this is not necessarily synchronous, hence unreliable and only
1922 * useful as an optimization.
1924 * Unlike other operations, this need not be serialized.
1926 void virtqueue_disable_cb(struct virtqueue *_vq)
1928 struct vring_virtqueue *vq = to_vvq(_vq);
1930 if (vq->packed_ring)
1931 virtqueue_disable_cb_packed(_vq);
1933 virtqueue_disable_cb_split(_vq);
1935 EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
1938 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
1939 * @vq: the struct virtqueue we're talking about.
1941 * This re-enables callbacks; it returns current queue state
1942 * in an opaque unsigned value. This value should be later tested by
1943 * virtqueue_poll, to detect a possible race between the driver checking for
1944 * more work, and enabling callbacks.
1946 * Caller must ensure we don't call this with other virtqueue
1947 * operations at the same time (except where noted).
1949 unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
1951 struct vring_virtqueue *vq = to_vvq(_vq);
1953 return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
1954 virtqueue_enable_cb_prepare_split(_vq);
1956 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
1959 * virtqueue_poll - query pending used buffers
1960 * @vq: the struct virtqueue we're talking about.
1961 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
1963 * Returns "true" if there are pending used buffers in the queue.
1965 * This does not need to be serialized.
1967 bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
1969 struct vring_virtqueue *vq = to_vvq(_vq);
1971 virtio_mb(vq->weak_barriers);
1972 return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
1973 virtqueue_poll_split(_vq, last_used_idx);
1975 EXPORT_SYMBOL_GPL(virtqueue_poll);
1978 * virtqueue_enable_cb - restart callbacks after disable_cb.
1979 * @vq: the struct virtqueue we're talking about.
1981 * This re-enables callbacks; it returns "false" if there are pending
1982 * buffers in the queue, to detect a possible race between the driver
1983 * checking for more work, and enabling callbacks.
1985 * Caller must ensure we don't call this with other virtqueue
1986 * operations at the same time (except where noted).
1988 bool virtqueue_enable_cb(struct virtqueue *_vq)
1990 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
1992 return !virtqueue_poll(_vq, last_used_idx);
1994 EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
1997 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
1998 * @vq: the struct virtqueue we're talking about.
2000 * This re-enables callbacks but hints to the other side to delay
2001 * interrupts until most of the available buffers have been processed;
2002 * it returns "false" if there are many pending buffers in the queue,
2003 * to detect a possible race between the driver checking for more work,
2004 * and enabling callbacks.
2006 * Caller must ensure we don't call this with other virtqueue
2007 * operations at the same time (except where noted).
2009 bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
2011 struct vring_virtqueue *vq = to_vvq(_vq);
2013 return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
2014 virtqueue_enable_cb_delayed_split(_vq);
2016 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
2019 * virtqueue_detach_unused_buf - detach first unused buffer
2020 * @vq: the struct virtqueue we're talking about.
2022 * Returns NULL or the "data" token handed to virtqueue_add_*().
2023 * This is not valid on an active queue; it is useful only for device
2026 void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
2028 struct vring_virtqueue *vq = to_vvq(_vq);
2030 return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
2031 virtqueue_detach_unused_buf_split(_vq);
2033 EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
2035 static inline bool more_used(const struct vring_virtqueue *vq)
2037 return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
2040 irqreturn_t vring_interrupt(int irq, void *_vq)
2042 struct vring_virtqueue *vq = to_vvq(_vq);
2044 if (!more_used(vq)) {
2045 pr_debug("virtqueue interrupt with no work for %p\n", vq);
2049 if (unlikely(vq->broken))
2052 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
2053 if (vq->vq.callback)
2054 vq->vq.callback(&vq->vq);
2058 EXPORT_SYMBOL_GPL(vring_interrupt);
2060 /* Only available for split ring */
2061 struct virtqueue *__vring_new_virtqueue(unsigned int index,
2063 struct virtio_device *vdev,
2066 bool (*notify)(struct virtqueue *),
2067 void (*callback)(struct virtqueue *),
2071 struct vring_virtqueue *vq;
2073 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2076 vq = kmalloc(sizeof(*vq), GFP_KERNEL);
2080 vq->packed_ring = false;
2081 vq->vq.callback = callback;
2084 vq->vq.num_free = vring.num;
2085 vq->vq.index = index;
2086 vq->we_own_ring = false;
2087 vq->notify = notify;
2088 vq->weak_barriers = weak_barriers;
2090 vq->last_used_idx = 0;
2092 vq->use_dma_api = vring_use_dma_api(vdev);
2093 list_add_tail(&vq->vq.list, &vdev->vqs);
2096 vq->last_add_time_valid = false;
2099 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
2101 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
2103 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
2104 vq->weak_barriers = false;
2106 vq->split.queue_dma_addr = 0;
2107 vq->split.queue_size_in_bytes = 0;
2109 vq->split.vring = vring;
2110 vq->split.avail_flags_shadow = 0;
2111 vq->split.avail_idx_shadow = 0;
2113 /* No callback? Tell other side not to bother us. */
2115 vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
2117 vq->split.vring.avail->flags = cpu_to_virtio16(vdev,
2118 vq->split.avail_flags_shadow);
2121 vq->split.desc_state = kmalloc_array(vring.num,
2122 sizeof(struct vring_desc_state_split), GFP_KERNEL);
2123 if (!vq->split.desc_state) {
2128 /* Put everything in free lists. */
2130 for (i = 0; i < vring.num-1; i++)
2131 vq->split.vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
2132 memset(vq->split.desc_state, 0, vring.num *
2133 sizeof(struct vring_desc_state_split));
2137 EXPORT_SYMBOL_GPL(__vring_new_virtqueue);
2139 struct virtqueue *vring_create_virtqueue(
2142 unsigned int vring_align,
2143 struct virtio_device *vdev,
2145 bool may_reduce_num,
2147 bool (*notify)(struct virtqueue *),
2148 void (*callback)(struct virtqueue *),
2152 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2153 return vring_create_virtqueue_packed(index, num, vring_align,
2154 vdev, weak_barriers, may_reduce_num,
2155 context, notify, callback, name);
2157 return vring_create_virtqueue_split(index, num, vring_align,
2158 vdev, weak_barriers, may_reduce_num,
2159 context, notify, callback, name);
2161 EXPORT_SYMBOL_GPL(vring_create_virtqueue);
2163 /* Only available for split ring */
2164 struct virtqueue *vring_new_virtqueue(unsigned int index,
2166 unsigned int vring_align,
2167 struct virtio_device *vdev,
2171 bool (*notify)(struct virtqueue *vq),
2172 void (*callback)(struct virtqueue *vq),
2177 if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
2180 vring_init(&vring, num, pages, vring_align);
2181 return __vring_new_virtqueue(index, vring, vdev, weak_barriers, context,
2182 notify, callback, name);
2184 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
2186 void vring_del_virtqueue(struct virtqueue *_vq)
2188 struct vring_virtqueue *vq = to_vvq(_vq);
2190 if (vq->we_own_ring) {
2191 if (vq->packed_ring) {
2192 vring_free_queue(vq->vq.vdev,
2193 vq->packed.ring_size_in_bytes,
2194 vq->packed.vring.desc,
2195 vq->packed.ring_dma_addr);
2197 vring_free_queue(vq->vq.vdev,
2198 vq->packed.event_size_in_bytes,
2199 vq->packed.vring.driver,
2200 vq->packed.driver_event_dma_addr);
2202 vring_free_queue(vq->vq.vdev,
2203 vq->packed.event_size_in_bytes,
2204 vq->packed.vring.device,
2205 vq->packed.device_event_dma_addr);
2207 kfree(vq->packed.desc_state);
2208 kfree(vq->packed.desc_extra);
2210 vring_free_queue(vq->vq.vdev,
2211 vq->split.queue_size_in_bytes,
2212 vq->split.vring.desc,
2213 vq->split.queue_dma_addr);
2215 kfree(vq->split.desc_state);
2218 list_del(&_vq->list);
2221 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
2223 /* Manipulates transport-specific feature bits. */
2224 void vring_transport_features(struct virtio_device *vdev)
2228 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
2230 case VIRTIO_RING_F_INDIRECT_DESC:
2232 case VIRTIO_RING_F_EVENT_IDX:
2234 case VIRTIO_F_VERSION_1:
2236 case VIRTIO_F_IOMMU_PLATFORM:
2238 case VIRTIO_F_RING_PACKED:
2240 case VIRTIO_F_ORDER_PLATFORM:
2243 /* We don't understand this bit. */
2244 __virtio_clear_bit(vdev, i);
2248 EXPORT_SYMBOL_GPL(vring_transport_features);
2251 * virtqueue_get_vring_size - return the size of the virtqueue's vring
2252 * @vq: the struct virtqueue containing the vring of interest.
2254 * Returns the size of the vring. This is mainly used for boasting to
2255 * userspace. Unlike other operations, this need not be serialized.
2257 unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
2260 struct vring_virtqueue *vq = to_vvq(_vq);
2262 return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
2264 EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
2266 bool virtqueue_is_broken(struct virtqueue *_vq)
2268 struct vring_virtqueue *vq = to_vvq(_vq);
2272 EXPORT_SYMBOL_GPL(virtqueue_is_broken);
2275 * This should prevent the device from being used, allowing drivers to
2276 * recover. You may need to grab appropriate locks to flush.
2278 void virtio_break_device(struct virtio_device *dev)
2280 struct virtqueue *_vq;
2282 list_for_each_entry(_vq, &dev->vqs, list) {
2283 struct vring_virtqueue *vq = to_vvq(_vq);
2287 EXPORT_SYMBOL_GPL(virtio_break_device);
2289 dma_addr_t virtqueue_get_desc_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.ring_dma_addr;
2298 return vq->split.queue_dma_addr;
2300 EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
2302 dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
2304 struct vring_virtqueue *vq = to_vvq(_vq);
2306 BUG_ON(!vq->we_own_ring);
2308 if (vq->packed_ring)
2309 return vq->packed.driver_event_dma_addr;
2311 return vq->split.queue_dma_addr +
2312 ((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
2314 EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
2316 dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
2318 struct vring_virtqueue *vq = to_vvq(_vq);
2320 BUG_ON(!vq->we_own_ring);
2322 if (vq->packed_ring)
2323 return vq->packed.device_event_dma_addr;
2325 return vq->split.queue_dma_addr +
2326 ((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
2328 EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
2330 /* Only available for split ring */
2331 const struct vring *virtqueue_get_vring(struct virtqueue *vq)
2333 return &to_vvq(vq)->split.vring;
2335 EXPORT_SYMBOL_GPL(virtqueue_get_vring);
2337 MODULE_LICENSE("GPL");