2 * Copyright (c) 2011, Bryan Venteicher <bryanv@daemoninthecloset.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/virtio/virtqueue.c,v 1.2 2012/04/14 05:48:04 grehan Exp $
30 * Implements the virtqueue interface as basically described
31 * in the original VirtIO paper.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/sglist.h>
39 #include <sys/serialize.h>
43 #include <machine/cpu.h>
44 #include <machine/atomic.h>
49 #include "virtqueue.h"
50 #include "virtio_ring.h"
52 #include "virtio_bus_if.h"
56 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
57 uint16_t vq_queue_index;
61 #define VIRTQUEUE_FLAG_INDIRECT 0x0001
62 #define VIRTQUEUE_FLAG_EVENT_IDX 0x0002
67 int vq_max_indirect_size;
68 int vq_indirect_mem_size;
72 uint16_t vq_queued_cnt;
74 * Head of the free chain in the descriptor table. If
75 * there are no free descriptors, this will be set to
76 * VQ_RING_DESC_CHAIN_END.
78 uint16_t vq_desc_head_idx;
80 * Last consumed descriptor in the used table,
81 * trails vq_ring.used->idx.
83 uint16_t vq_used_cons_idx;
85 struct vq_desc_extra {
87 struct vring_desc *indirect;
88 vm_paddr_t indirect_paddr;
94 * The maximum virtqueue size is 2^15. Use that value as the end of
95 * descriptor chain terminator since it will never be a valid index
96 * in the descriptor table. This is used to verify we are correctly
97 * handling vq_free_cnt.
99 #define VQ_RING_DESC_CHAIN_END 32768
101 #define VQASSERT(_vq, _exp, _msg, ...) \
102 KASSERT((_exp),("%s: %s - "_msg, __func__, (_vq)->vq_name, \
105 #define VQ_RING_ASSERT_VALID_IDX(_vq, _idx) \
106 VQASSERT((_vq), (_idx) < (_vq)->vq_nentries, \
107 "invalid ring index: %d, max: %d", (_idx), \
110 #define VQ_RING_ASSERT_CHAIN_TERM(_vq) \
111 VQASSERT((_vq), (_vq)->vq_desc_head_idx == \
112 VQ_RING_DESC_CHAIN_END, "full ring terminated " \
113 "incorrectly: head idx: %d", (_vq)->vq_desc_head_idx)
115 static int virtqueue_init_indirect(struct virtqueue *vq, int);
116 static void virtqueue_free_indirect(struct virtqueue *vq);
117 static void virtqueue_init_indirect_list(struct virtqueue *,
118 struct vring_desc *);
120 static void vq_ring_init(struct virtqueue *);
121 static void vq_ring_update_avail(struct virtqueue *, uint16_t);
122 static uint16_t vq_ring_enqueue_segments(struct virtqueue *,
123 struct vring_desc *, uint16_t, struct sglist *, int, int);
124 static int vq_ring_use_indirect(struct virtqueue *, int);
125 static void vq_ring_enqueue_indirect(struct virtqueue *, void *,
126 struct sglist *, int, int);
127 static int vq_ring_must_notify_host(struct virtqueue *);
128 static void vq_ring_notify_host(struct virtqueue *);
129 static void vq_ring_free_chain(struct virtqueue *, uint16_t);
132 virtqueue_filter_features(uint64_t features)
136 mask = (1 << VIRTIO_TRANSPORT_F_START) - 1;
137 mask |= VIRTIO_RING_F_INDIRECT_DESC;
138 mask |= VIRTIO_RING_F_EVENT_IDX;
140 return (features & mask);
144 virtqueue_alloc(device_t dev, uint16_t queue, uint16_t size, int align,
145 vm_paddr_t highaddr, struct vq_alloc_info *info, struct virtqueue **vqp)
147 struct virtqueue *vq;
155 "virtqueue %d (%s) does not exist (size is zero)\n",
156 queue, info->vqai_name);
158 } else if (!powerof2(size)) {
160 "virtqueue %d (%s) size is not a power of 2: %d\n",
161 queue, info->vqai_name, size);
163 } else if (info->vqai_maxindirsz > VIRTIO_MAX_INDIRECT) {
164 device_printf(dev, "virtqueue %d (%s) requested too many "
165 "indirect descriptors: %d, max %d\n",
166 queue, info->vqai_name, info->vqai_maxindirsz,
167 VIRTIO_MAX_INDIRECT);
171 vq = kmalloc(sizeof(struct virtqueue) +
172 size * sizeof(struct vq_desc_extra), M_DEVBUF, M_INTWAIT | M_ZERO);
174 device_printf(dev, "cannot allocate virtqueue\n");
179 strlcpy(vq->vq_name, info->vqai_name, sizeof(vq->vq_name));
180 vq->vq_queue_index = queue;
181 vq->vq_alignment = align;
182 vq->vq_nentries = size;
183 vq->vq_free_cnt = size;
185 if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_RING_F_EVENT_IDX) != 0)
186 vq->vq_flags |= VIRTQUEUE_FLAG_EVENT_IDX;
188 if (info->vqai_maxindirsz > 1) {
189 error = virtqueue_init_indirect(vq, info->vqai_maxindirsz);
194 vq->vq_ring_size = round_page(vring_size(size, align));
195 vq->vq_ring_mem = contigmalloc(vq->vq_ring_size, M_DEVBUF,
196 M_WAITOK | M_ZERO, 0, highaddr, PAGE_SIZE, 0);
197 if (vq->vq_ring_mem == NULL) {
199 "cannot allocate memory for virtqueue ring\n");
205 virtqueue_disable_intr(vq);
217 virtqueue_init_indirect(struct virtqueue *vq, int indirect_size)
220 struct vq_desc_extra *dxp;
225 if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_RING_F_INDIRECT_DESC) == 0) {
227 * Indirect descriptors requested by the driver but not
228 * negotiated. Return zero to keep the initialization
229 * going: we'll run fine without.
232 device_printf(dev, "virtqueue %d (%s) requested "
233 "indirect descriptors but not negotiated\n",
234 vq->vq_queue_index, vq->vq_name);
238 size = indirect_size * sizeof(struct vring_desc);
239 vq->vq_max_indirect_size = indirect_size;
240 vq->vq_indirect_mem_size = size;
241 vq->vq_flags |= VIRTQUEUE_FLAG_INDIRECT;
243 for (i = 0; i < vq->vq_nentries; i++) {
244 dxp = &vq->vq_descx[i];
246 dxp->indirect = contigmalloc(size, M_DEVBUF, M_WAITOK,
247 0, BUS_SPACE_MAXADDR, 16, 0);
248 if (dxp->indirect == NULL) {
249 device_printf(dev, "cannot allocate indirect list\n");
253 dxp->indirect_paddr = vtophys(dxp->indirect);
254 virtqueue_init_indirect_list(vq, dxp->indirect);
261 virtqueue_free_indirect(struct virtqueue *vq)
263 struct vq_desc_extra *dxp;
266 for (i = 0; i < vq->vq_nentries; i++) {
267 dxp = &vq->vq_descx[i];
269 if (dxp->indirect == NULL)
272 contigfree(dxp->indirect, vq->vq_indirect_mem_size, M_DEVBUF);
273 dxp->indirect = NULL;
274 dxp->indirect_paddr = 0;
277 vq->vq_flags &= ~VIRTQUEUE_FLAG_INDIRECT;
278 vq->vq_indirect_mem_size = 0;
282 virtqueue_init_indirect_list(struct virtqueue *vq,
283 struct vring_desc *indirect)
287 bzero(indirect, vq->vq_indirect_mem_size);
289 for (i = 0; i < vq->vq_max_indirect_size - 1; i++)
290 indirect[i].next = i + 1;
291 indirect[i].next = VQ_RING_DESC_CHAIN_END;
295 virtqueue_reinit(struct virtqueue *vq, uint16_t size)
297 struct vq_desc_extra *dxp;
300 if (vq->vq_nentries != size) {
301 device_printf(vq->vq_dev,
302 "%s: '%s' changed size; old=%hu, new=%hu\n",
303 __func__, vq->vq_name, vq->vq_nentries, size);
307 /* Warn if the virtqueue was not properly cleaned up. */
308 if (vq->vq_free_cnt != vq->vq_nentries) {
309 device_printf(vq->vq_dev,
310 "%s: warning, '%s' virtqueue not empty, "
311 "leaking %d entries\n", __func__, vq->vq_name,
312 vq->vq_nentries - vq->vq_free_cnt);
315 vq->vq_desc_head_idx = 0;
316 vq->vq_used_cons_idx = 0;
317 vq->vq_queued_cnt = 0;
318 vq->vq_free_cnt = vq->vq_nentries;
320 /* To be safe, reset all our allocated memory. */
321 bzero(vq->vq_ring_mem, vq->vq_ring_size);
322 for (i = 0; i < vq->vq_nentries; i++) {
323 dxp = &vq->vq_descx[i];
326 if (vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT)
327 virtqueue_init_indirect_list(vq, dxp->indirect);
331 virtqueue_disable_intr(vq);
337 virtqueue_free(struct virtqueue *vq)
340 if (vq->vq_free_cnt != vq->vq_nentries) {
341 device_printf(vq->vq_dev, "%s: freeing non-empty virtqueue, "
342 "leaking %d entries\n", vq->vq_name,
343 vq->vq_nentries - vq->vq_free_cnt);
346 if (vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT)
347 virtqueue_free_indirect(vq);
349 if (vq->vq_ring_mem != NULL) {
350 contigfree(vq->vq_ring_mem, vq->vq_ring_size, M_DEVBUF);
351 vq->vq_ring_size = 0;
352 vq->vq_ring_mem = NULL;
359 virtqueue_paddr(struct virtqueue *vq)
361 return (vtophys(vq->vq_ring_mem));
365 virtqueue_size(struct virtqueue *vq)
367 return (vq->vq_nentries);
371 virtqueue_empty(struct virtqueue *vq)
374 return (vq->vq_nentries == vq->vq_free_cnt);
378 virtqueue_full(struct virtqueue *vq)
381 return (vq->vq_free_cnt == 0);
385 virtqueue_notify(struct virtqueue *vq, lwkt_serialize_t interlock)
387 /* Ensure updated avail->idx is visible to host. */
390 if (vq_ring_must_notify_host(vq)) {
391 if (interlock != NULL)
392 lwkt_serialize_exit(interlock);
393 vq_ring_notify_host(vq);
394 if (interlock != NULL)
395 lwkt_serialize_enter(interlock);
397 vq->vq_queued_cnt = 0;
401 virtqueue_nused(struct virtqueue *vq)
403 uint16_t used_idx, nused;
405 used_idx = vq->vq_ring.used->idx;
406 nused = (uint16_t)(used_idx - vq->vq_used_cons_idx);
407 VQASSERT(vq, nused <= vq->vq_nentries, "used more than available");
413 virtqueue_pending(struct virtqueue *vq)
415 return (vq->vq_used_cons_idx != vq->vq_ring.used->idx);
419 * Enable interrupts on a given virtqueue. Returns 1 if there are
420 * additional entries to process on the virtqueue after we return.
423 virtqueue_enable_intr(struct virtqueue *vq)
426 * Enable interrupts, making sure we get the latest
427 * index of what's already been consumed.
429 vq->vq_ring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
430 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) {
431 vring_used_event(&vq->vq_ring) = vq->vq_used_cons_idx;
433 vq->vq_ring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
439 * Additional items may have been consumed in the time between
440 * since we last checked and enabled interrupts above. Let our
441 * caller know so it processes the new entries.
443 if (vq->vq_used_cons_idx != vq->vq_ring.used->idx)
450 virtqueue_postpone_intr(struct virtqueue *vq)
455 * Postpone until at least half of the available descriptors
456 * have been consumed.
458 * XXX Adaptive factor? (Linux uses 3/4)
460 ndesc = (uint16_t)(vq->vq_ring.avail->idx - vq->vq_used_cons_idx) / 2;
462 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX)
463 vring_used_event(&vq->vq_ring) = vq->vq_used_cons_idx + ndesc;
465 vq->vq_ring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
470 * Enough items may have already been consumed to meet our
471 * threshold since we last checked. Let our caller know so
472 * it processes the new entries.
474 if (virtqueue_nused(vq) > ndesc)
481 virtqueue_disable_intr(struct virtqueue *vq)
484 * Note this is only considered a hint to the host.
486 if ((vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) == 0)
487 vq->vq_ring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
491 virtqueue_enqueue(struct virtqueue *vq, void *cookie, struct sglist *sg,
492 int readable, int writable)
494 struct vq_desc_extra *dxp;
496 uint16_t head_idx, idx;
498 needed = readable + writable;
500 VQASSERT(vq, cookie != NULL, "enqueuing with no cookie");
501 VQASSERT(vq, needed == sg->sg_nseg,
502 "segment count mismatch, %d, %d", needed, sg->sg_nseg);
504 needed <= vq->vq_nentries || needed <= vq->vq_max_indirect_size,
505 "too many segments to enqueue: %d, %d/%d", needed,
506 vq->vq_nentries, vq->vq_max_indirect_size);
510 if (vq->vq_free_cnt == 0)
513 if (vq_ring_use_indirect(vq, needed)) {
514 vq_ring_enqueue_indirect(vq, cookie, sg, readable, writable);
516 } else if (vq->vq_free_cnt < needed)
519 head_idx = vq->vq_desc_head_idx;
520 VQ_RING_ASSERT_VALID_IDX(vq, head_idx);
521 dxp = &vq->vq_descx[head_idx];
523 VQASSERT(vq, dxp->cookie == NULL,
524 "cookie already exists for index %d", head_idx);
525 dxp->cookie = cookie;
526 dxp->ndescs = needed;
528 idx = vq_ring_enqueue_segments(vq, vq->vq_ring.desc, head_idx,
529 sg, readable, writable);
531 vq->vq_desc_head_idx = idx;
532 vq->vq_free_cnt -= needed;
533 if (vq->vq_free_cnt == 0)
534 VQ_RING_ASSERT_CHAIN_TERM(vq);
536 VQ_RING_ASSERT_VALID_IDX(vq, idx);
538 vq_ring_update_avail(vq, head_idx);
544 virtqueue_dequeue(struct virtqueue *vq, uint32_t *len)
546 struct vring_used_elem *uep;
548 uint16_t used_idx, desc_idx;
550 if (vq->vq_used_cons_idx == vq->vq_ring.used->idx)
553 used_idx = vq->vq_used_cons_idx++ & (vq->vq_nentries - 1);
554 uep = &vq->vq_ring.used->ring[used_idx];
557 desc_idx = (uint16_t) uep->id;
561 vq_ring_free_chain(vq, desc_idx);
563 cookie = vq->vq_descx[desc_idx].cookie;
564 VQASSERT(vq, cookie != NULL, "no cookie for index %d", desc_idx);
565 vq->vq_descx[desc_idx].cookie = NULL;
571 virtqueue_poll(struct virtqueue *vq, uint32_t *len)
575 /* We only poll the virtqueue when dumping to virtio-blk */
576 while ((cookie = virtqueue_dequeue(vq, len)) == NULL)
583 virtqueue_drain(struct virtqueue *vq, int *last)
591 while (idx < vq->vq_nentries && cookie == NULL) {
592 if ((cookie = vq->vq_descx[idx].cookie) != NULL) {
593 vq->vq_descx[idx].cookie = NULL;
594 /* Free chain to keep free count consistent. */
595 vq_ring_free_chain(vq, idx);
606 virtqueue_dump(struct virtqueue *vq)
612 kprintf("VQ: %s - size=%d; free=%d; used=%d; queued=%d; "
613 "desc_head_idx=%d; avail.idx=%d; used_cons_idx=%d; "
614 "used.idx=%d; avail.flags=0x%x; used.flags=0x%x\n",
615 vq->vq_name, vq->vq_nentries, vq->vq_free_cnt,
616 virtqueue_nused(vq), vq->vq_queued_cnt, vq->vq_desc_head_idx,
617 vq->vq_ring.avail->idx, vq->vq_used_cons_idx,
618 vq->vq_ring.used->idx, vq->vq_ring.avail->flags,
619 vq->vq_ring.used->flags);
623 vq_ring_init(struct virtqueue *vq)
629 ring_mem = vq->vq_ring_mem;
630 size = vq->vq_nentries;
633 vring_init(vr, size, ring_mem, vq->vq_alignment);
635 for (i = 0; i < size - 1; i++)
636 vr->desc[i].next = i + 1;
637 vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
641 vq_ring_update_avail(struct virtqueue *vq, uint16_t desc_idx)
646 * Place the head of the descriptor chain into the next slot and make
647 * it usable to the host. The chain is made available now rather than
648 * deferring to virtqueue_notify() in the hopes that if the host is
649 * currently running on another CPU, we can keep it processing the new
652 avail_idx = vq->vq_ring.avail->idx & (vq->vq_nentries - 1);
653 vq->vq_ring.avail->ring[avail_idx] = desc_idx;
656 vq->vq_ring.avail->idx++;
658 /* Keep pending count until virtqueue_notify() for debugging. */
663 vq_ring_enqueue_segments(struct virtqueue *vq, struct vring_desc *desc,
664 uint16_t head_idx, struct sglist *sg, int readable, int writable)
666 struct sglist_seg *seg;
667 struct vring_desc *dp;
671 needed = readable + writable;
673 for (i = 0, idx = head_idx, seg = sg->sg_segs;
675 i++, idx = dp->next, seg++) {
676 VQASSERT(vq, idx != VQ_RING_DESC_CHAIN_END,
677 "premature end of free desc chain");
680 dp->addr = seg->ss_paddr;
681 dp->len = seg->ss_len;
685 dp->flags |= VRING_DESC_F_NEXT;
687 dp->flags |= VRING_DESC_F_WRITE;
694 vq_ring_use_indirect(struct virtqueue *vq, int needed)
697 if ((vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT) == 0)
700 if (vq->vq_max_indirect_size < needed)
710 vq_ring_enqueue_indirect(struct virtqueue *vq, void *cookie,
711 struct sglist *sg, int readable, int writable)
713 struct vring_desc *dp;
714 struct vq_desc_extra *dxp;
718 needed = readable + writable;
719 VQASSERT(vq, needed <= vq->vq_max_indirect_size,
720 "enqueuing too many indirect descriptors");
722 head_idx = vq->vq_desc_head_idx;
723 VQ_RING_ASSERT_VALID_IDX(vq, head_idx);
724 dp = &vq->vq_ring.desc[head_idx];
725 dxp = &vq->vq_descx[head_idx];
727 VQASSERT(vq, dxp->cookie == NULL,
728 "cookie already exists for index %d", head_idx);
729 dxp->cookie = cookie;
732 dp->addr = dxp->indirect_paddr;
733 dp->len = needed * sizeof(struct vring_desc);
734 dp->flags = VRING_DESC_F_INDIRECT;
736 vq_ring_enqueue_segments(vq, dxp->indirect, 0,
737 sg, readable, writable);
739 vq->vq_desc_head_idx = dp->next;
741 if (vq->vq_free_cnt == 0)
742 VQ_RING_ASSERT_CHAIN_TERM(vq);
744 VQ_RING_ASSERT_VALID_IDX(vq, vq->vq_desc_head_idx);
746 vq_ring_update_avail(vq, head_idx);
750 vq_ring_must_notify_host(struct virtqueue *vq)
752 uint16_t new_idx, prev_idx, event_idx;
754 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) {
755 new_idx = vq->vq_ring.avail->idx;
756 prev_idx = new_idx - vq->vq_queued_cnt;
757 event_idx = vring_avail_event(&vq->vq_ring);
759 return (vring_need_event(event_idx, new_idx, prev_idx) != 0);
762 return ((vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY) == 0);
766 vq_ring_notify_host(struct virtqueue *vq)
768 VIRTIO_BUS_NOTIFY_VQ(vq->vq_dev, vq->vq_queue_index);
772 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
774 struct vring_desc *dp;
775 struct vq_desc_extra *dxp;
777 VQ_RING_ASSERT_VALID_IDX(vq, desc_idx);
778 dp = &vq->vq_ring.desc[desc_idx];
779 dxp = &vq->vq_descx[desc_idx];
781 if (vq->vq_free_cnt == 0)
782 VQ_RING_ASSERT_CHAIN_TERM(vq);
784 vq->vq_free_cnt += dxp->ndescs;
787 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
788 while (dp->flags & VRING_DESC_F_NEXT) {
789 VQ_RING_ASSERT_VALID_IDX(vq, dp->next);
790 dp = &vq->vq_ring.desc[dp->next];
794 VQASSERT(vq, dxp->ndescs == 0, "failed to free entire desc chain");
797 * We must append the existing free chain, if any, to the end of
798 * newly freed chain. If the virtqueue was completely used, then
799 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
801 dp->next = vq->vq_desc_head_idx;
802 vq->vq_desc_head_idx = desc_idx;