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.
28 * Implements the virtqueue interface as basically described
29 * in the original VirtIO paper.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/sglist.h>
43 #include <machine/cpu.h>
44 #include <machine/bus.h>
45 #include <machine/atomic.h>
46 #include <machine/resource.h>
50 #include <dev/virtio/virtio.h>
51 #include <dev/virtio/virtqueue.h>
52 #include <dev/virtio/virtio_ring.h>
54 #include "virtio_bus_if.h"
58 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
59 uint16_t vq_queue_index;
62 #define VIRTQUEUE_FLAG_INDIRECT 0x0001
63 #define VIRTQUEUE_FLAG_EVENT_IDX 0x0002
68 int vq_max_indirect_size;
69 int vq_indirect_mem_size;
70 virtqueue_intr_t *vq_intrhand;
71 void *vq_intrhand_arg;
75 uint16_t vq_queued_cnt;
77 * Head of the free chain in the descriptor table. If
78 * there are no free descriptors, this will be set to
79 * VQ_RING_DESC_CHAIN_END.
81 uint16_t vq_desc_head_idx;
83 * Last consumed descriptor in the used table,
84 * trails vq_ring.used->idx.
86 uint16_t vq_used_cons_idx;
88 struct vq_desc_extra {
90 struct vring_desc *indirect;
91 vm_paddr_t indirect_paddr;
97 * The maximum virtqueue size is 2^15. Use that value as the end of
98 * descriptor chain terminator since it will never be a valid index
99 * in the descriptor table. This is used to verify we are correctly
100 * handling vq_free_cnt.
102 #define VQ_RING_DESC_CHAIN_END 32768
104 #define VQASSERT(_vq, _exp, _msg, ...) \
105 KASSERT((_exp),("%s: %s - "_msg, __func__, (_vq)->vq_name, \
108 #define VQ_RING_ASSERT_VALID_IDX(_vq, _idx) \
109 VQASSERT((_vq), (_idx) < (_vq)->vq_nentries, \
110 "invalid ring index: %d, max: %d", (_idx), \
113 #define VQ_RING_ASSERT_CHAIN_TERM(_vq) \
114 VQASSERT((_vq), (_vq)->vq_desc_head_idx == \
115 VQ_RING_DESC_CHAIN_END, "full ring terminated " \
116 "incorrectly: head idx: %d", (_vq)->vq_desc_head_idx)
118 static int virtqueue_init_indirect(struct virtqueue *vq, int);
119 static void virtqueue_free_indirect(struct virtqueue *vq);
120 static void virtqueue_init_indirect_list(struct virtqueue *,
121 struct vring_desc *);
123 static void vq_ring_init(struct virtqueue *);
124 static void vq_ring_update_avail(struct virtqueue *, uint16_t);
125 static uint16_t vq_ring_enqueue_segments(struct virtqueue *,
126 struct vring_desc *, uint16_t, struct sglist *, int, int);
127 static int vq_ring_use_indirect(struct virtqueue *, int);
128 static void vq_ring_enqueue_indirect(struct virtqueue *, void *,
129 struct sglist *, int, int);
130 static int vq_ring_enable_interrupt(struct virtqueue *, uint16_t);
131 static int vq_ring_must_notify_host(struct virtqueue *);
132 static void vq_ring_notify_host(struct virtqueue *);
133 static void vq_ring_free_chain(struct virtqueue *, uint16_t);
136 virtqueue_filter_features(uint64_t features)
140 mask = (1 << VIRTIO_TRANSPORT_F_START) - 1;
141 mask |= VIRTIO_RING_F_INDIRECT_DESC;
142 mask |= VIRTIO_RING_F_EVENT_IDX;
144 return (features & mask);
148 virtqueue_alloc(device_t dev, uint16_t queue, uint16_t size, int align,
149 vm_paddr_t highaddr, struct vq_alloc_info *info, struct virtqueue **vqp)
151 struct virtqueue *vq;
159 "virtqueue %d (%s) does not exist (size is zero)\n",
160 queue, info->vqai_name);
162 } else if (!powerof2(size)) {
164 "virtqueue %d (%s) size is not a power of 2: %d\n",
165 queue, info->vqai_name, size);
167 } else if (info->vqai_maxindirsz > VIRTIO_MAX_INDIRECT) {
168 device_printf(dev, "virtqueue %d (%s) requested too many "
169 "indirect descriptors: %d, max %d\n",
170 queue, info->vqai_name, info->vqai_maxindirsz,
171 VIRTIO_MAX_INDIRECT);
175 vq = malloc(sizeof(struct virtqueue) +
176 size * sizeof(struct vq_desc_extra), M_DEVBUF, M_NOWAIT | M_ZERO);
178 device_printf(dev, "cannot allocate virtqueue\n");
183 strlcpy(vq->vq_name, info->vqai_name, sizeof(vq->vq_name));
184 vq->vq_queue_index = queue;
185 vq->vq_alignment = align;
186 vq->vq_nentries = size;
187 vq->vq_free_cnt = size;
188 vq->vq_intrhand = info->vqai_intr;
189 vq->vq_intrhand_arg = info->vqai_intr_arg;
191 if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_RING_F_EVENT_IDX) != 0)
192 vq->vq_flags |= VIRTQUEUE_FLAG_EVENT_IDX;
194 if (info->vqai_maxindirsz > 1) {
195 error = virtqueue_init_indirect(vq, info->vqai_maxindirsz);
200 vq->vq_ring_size = round_page(vring_size(size, align));
201 vq->vq_ring_mem = contigmalloc(vq->vq_ring_size, M_DEVBUF,
202 M_NOWAIT | M_ZERO, 0, highaddr, PAGE_SIZE, 0);
203 if (vq->vq_ring_mem == NULL) {
205 "cannot allocate memory for virtqueue ring\n");
211 virtqueue_disable_intr(vq);
223 virtqueue_init_indirect(struct virtqueue *vq, int indirect_size)
226 struct vq_desc_extra *dxp;
231 if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_RING_F_INDIRECT_DESC) == 0) {
233 * Indirect descriptors requested by the driver but not
234 * negotiated. Return zero to keep the initialization
235 * going: we'll run fine without.
238 device_printf(dev, "virtqueue %d (%s) requested "
239 "indirect descriptors but not negotiated\n",
240 vq->vq_queue_index, vq->vq_name);
244 size = indirect_size * sizeof(struct vring_desc);
245 vq->vq_max_indirect_size = indirect_size;
246 vq->vq_indirect_mem_size = size;
247 vq->vq_flags |= VIRTQUEUE_FLAG_INDIRECT;
249 for (i = 0; i < vq->vq_nentries; i++) {
250 dxp = &vq->vq_descx[i];
252 dxp->indirect = malloc(size, M_DEVBUF, M_NOWAIT);
253 if (dxp->indirect == NULL) {
254 device_printf(dev, "cannot allocate indirect list\n");
258 dxp->indirect_paddr = vtophys(dxp->indirect);
259 virtqueue_init_indirect_list(vq, dxp->indirect);
266 virtqueue_free_indirect(struct virtqueue *vq)
268 struct vq_desc_extra *dxp;
271 for (i = 0; i < vq->vq_nentries; i++) {
272 dxp = &vq->vq_descx[i];
274 if (dxp->indirect == NULL)
277 free(dxp->indirect, M_DEVBUF);
278 dxp->indirect = NULL;
279 dxp->indirect_paddr = 0;
282 vq->vq_flags &= ~VIRTQUEUE_FLAG_INDIRECT;
283 vq->vq_indirect_mem_size = 0;
287 virtqueue_init_indirect_list(struct virtqueue *vq,
288 struct vring_desc *indirect)
292 bzero(indirect, vq->vq_indirect_mem_size);
294 for (i = 0; i < vq->vq_max_indirect_size - 1; i++)
295 indirect[i].next = i + 1;
296 indirect[i].next = VQ_RING_DESC_CHAIN_END;
300 virtqueue_reinit(struct virtqueue *vq, uint16_t size)
302 struct vq_desc_extra *dxp;
305 if (vq->vq_nentries != size) {
306 device_printf(vq->vq_dev,
307 "%s: '%s' changed size; old=%hu, new=%hu\n",
308 __func__, vq->vq_name, vq->vq_nentries, size);
312 /* Warn if the virtqueue was not properly cleaned up. */
313 if (vq->vq_free_cnt != vq->vq_nentries) {
314 device_printf(vq->vq_dev,
315 "%s: warning '%s' virtqueue not empty, "
316 "leaking %d entries\n", __func__, vq->vq_name,
317 vq->vq_nentries - vq->vq_free_cnt);
320 vq->vq_desc_head_idx = 0;
321 vq->vq_used_cons_idx = 0;
322 vq->vq_queued_cnt = 0;
323 vq->vq_free_cnt = vq->vq_nentries;
325 /* To be safe, reset all our allocated memory. */
326 bzero(vq->vq_ring_mem, vq->vq_ring_size);
327 for (i = 0; i < vq->vq_nentries; i++) {
328 dxp = &vq->vq_descx[i];
331 if (vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT)
332 virtqueue_init_indirect_list(vq, dxp->indirect);
336 virtqueue_disable_intr(vq);
342 virtqueue_free(struct virtqueue *vq)
345 if (vq->vq_free_cnt != vq->vq_nentries) {
346 device_printf(vq->vq_dev, "%s: freeing non-empty virtqueue, "
347 "leaking %d entries\n", vq->vq_name,
348 vq->vq_nentries - vq->vq_free_cnt);
351 if (vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT)
352 virtqueue_free_indirect(vq);
354 if (vq->vq_ring_mem != NULL) {
355 contigfree(vq->vq_ring_mem, vq->vq_ring_size, M_DEVBUF);
356 vq->vq_ring_size = 0;
357 vq->vq_ring_mem = NULL;
364 virtqueue_paddr(struct virtqueue *vq)
367 return (vtophys(vq->vq_ring_mem));
371 virtqueue_size(struct virtqueue *vq)
374 return (vq->vq_nentries);
378 virtqueue_empty(struct virtqueue *vq)
381 return (vq->vq_nentries == vq->vq_free_cnt);
385 virtqueue_full(struct virtqueue *vq)
388 return (vq->vq_free_cnt == 0);
392 virtqueue_notify(struct virtqueue *vq)
395 /* Ensure updated avail->idx is visible to host. */
398 if (vq_ring_must_notify_host(vq))
399 vq_ring_notify_host(vq);
400 vq->vq_queued_cnt = 0;
404 virtqueue_nused(struct virtqueue *vq)
406 uint16_t used_idx, nused;
408 used_idx = vq->vq_ring.used->idx;
410 nused = (uint16_t)(used_idx - vq->vq_used_cons_idx);
411 VQASSERT(vq, nused <= vq->vq_nentries, "used more than available");
417 virtqueue_intr(struct virtqueue *vq)
420 if (vq->vq_intrhand == NULL ||
421 vq->vq_used_cons_idx == vq->vq_ring.used->idx)
424 vq->vq_intrhand(vq->vq_intrhand_arg);
430 virtqueue_enable_intr(struct virtqueue *vq)
433 return (vq_ring_enable_interrupt(vq, 0));
437 virtqueue_postpone_intr(struct virtqueue *vq)
439 uint16_t ndesc, avail_idx;
442 * Request the next interrupt be postponed until at least half
443 * of the available descriptors have been consumed.
445 avail_idx = vq->vq_ring.avail->idx;
446 ndesc = (uint16_t)(avail_idx - vq->vq_used_cons_idx) / 2;
448 return (vq_ring_enable_interrupt(vq, ndesc));
452 virtqueue_disable_intr(struct virtqueue *vq)
456 * Note this is only considered a hint to the host.
458 if ((vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) == 0)
459 vq->vq_ring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
463 virtqueue_enqueue(struct virtqueue *vq, void *cookie, struct sglist *sg,
464 int readable, int writable)
466 struct vq_desc_extra *dxp;
468 uint16_t head_idx, idx;
470 needed = readable + writable;
472 VQASSERT(vq, cookie != NULL, "enqueuing with no cookie");
473 VQASSERT(vq, needed == sg->sg_nseg,
474 "segment count mismatch, %d, %d", needed, sg->sg_nseg);
476 needed <= vq->vq_nentries || needed <= vq->vq_max_indirect_size,
477 "too many segments to enqueue: %d, %d/%d", needed,
478 vq->vq_nentries, vq->vq_max_indirect_size);
482 if (vq->vq_free_cnt == 0)
485 if (vq_ring_use_indirect(vq, needed)) {
486 vq_ring_enqueue_indirect(vq, cookie, sg, readable, writable);
488 } else if (vq->vq_free_cnt < needed)
491 head_idx = vq->vq_desc_head_idx;
492 VQ_RING_ASSERT_VALID_IDX(vq, head_idx);
493 dxp = &vq->vq_descx[head_idx];
495 VQASSERT(vq, dxp->cookie == NULL,
496 "cookie already exists for index %d", head_idx);
497 dxp->cookie = cookie;
498 dxp->ndescs = needed;
500 idx = vq_ring_enqueue_segments(vq, vq->vq_ring.desc, head_idx,
501 sg, readable, writable);
503 vq->vq_desc_head_idx = idx;
504 vq->vq_free_cnt -= needed;
505 if (vq->vq_free_cnt == 0)
506 VQ_RING_ASSERT_CHAIN_TERM(vq);
508 VQ_RING_ASSERT_VALID_IDX(vq, idx);
510 vq_ring_update_avail(vq, head_idx);
516 virtqueue_dequeue(struct virtqueue *vq, uint32_t *len)
518 struct vring_used_elem *uep;
520 uint16_t used_idx, desc_idx;
522 if (vq->vq_used_cons_idx == vq->vq_ring.used->idx)
525 used_idx = vq->vq_used_cons_idx++ & (vq->vq_nentries - 1);
526 uep = &vq->vq_ring.used->ring[used_idx];
529 desc_idx = (uint16_t) uep->id;
533 vq_ring_free_chain(vq, desc_idx);
535 cookie = vq->vq_descx[desc_idx].cookie;
536 VQASSERT(vq, cookie != NULL, "no cookie for index %d", desc_idx);
537 vq->vq_descx[desc_idx].cookie = NULL;
543 virtqueue_poll(struct virtqueue *vq, uint32_t *len)
547 while ((cookie = virtqueue_dequeue(vq, len)) == NULL)
554 virtqueue_drain(struct virtqueue *vq, int *last)
562 while (idx < vq->vq_nentries && cookie == NULL) {
563 if ((cookie = vq->vq_descx[idx].cookie) != NULL) {
564 vq->vq_descx[idx].cookie = NULL;
565 /* Free chain to keep free count consistent. */
566 vq_ring_free_chain(vq, idx);
577 virtqueue_dump(struct virtqueue *vq)
583 printf("VQ: %s - size=%d; free=%d; used=%d; queued=%d; "
584 "desc_head_idx=%d; avail.idx=%d; used_cons_idx=%d; "
585 "used.idx=%d; avail.flags=0x%x; used.flags=0x%x\n",
586 vq->vq_name, vq->vq_nentries, vq->vq_free_cnt,
587 virtqueue_nused(vq), vq->vq_queued_cnt, vq->vq_desc_head_idx,
588 vq->vq_ring.avail->idx, vq->vq_used_cons_idx,
589 vq->vq_ring.used->idx, vq->vq_ring.avail->flags,
590 vq->vq_ring.used->flags);
594 vq_ring_init(struct virtqueue *vq)
600 ring_mem = vq->vq_ring_mem;
601 size = vq->vq_nentries;
604 vring_init(vr, size, ring_mem, vq->vq_alignment);
606 for (i = 0; i < size - 1; i++)
607 vr->desc[i].next = i + 1;
608 vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
612 vq_ring_update_avail(struct virtqueue *vq, uint16_t desc_idx)
617 * Place the head of the descriptor chain into the next slot and make
618 * it usable to the host. The chain is made available now rather than
619 * deferring to virtqueue_notify() in the hopes that if the host is
620 * currently running on another CPU, we can keep it processing the new
623 avail_idx = vq->vq_ring.avail->idx & (vq->vq_nentries - 1);
624 vq->vq_ring.avail->ring[avail_idx] = desc_idx;
627 vq->vq_ring.avail->idx++;
629 /* Keep pending count until virtqueue_notify(). */
634 vq_ring_enqueue_segments(struct virtqueue *vq, struct vring_desc *desc,
635 uint16_t head_idx, struct sglist *sg, int readable, int writable)
637 struct sglist_seg *seg;
638 struct vring_desc *dp;
642 needed = readable + writable;
644 for (i = 0, idx = head_idx, seg = sg->sg_segs;
646 i++, idx = dp->next, seg++) {
647 VQASSERT(vq, idx != VQ_RING_DESC_CHAIN_END,
648 "premature end of free desc chain");
651 dp->addr = seg->ss_paddr;
652 dp->len = seg->ss_len;
656 dp->flags |= VRING_DESC_F_NEXT;
658 dp->flags |= VRING_DESC_F_WRITE;
665 vq_ring_use_indirect(struct virtqueue *vq, int needed)
668 if ((vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT) == 0)
671 if (vq->vq_max_indirect_size < needed)
681 vq_ring_enqueue_indirect(struct virtqueue *vq, void *cookie,
682 struct sglist *sg, int readable, int writable)
684 struct vring_desc *dp;
685 struct vq_desc_extra *dxp;
689 needed = readable + writable;
690 VQASSERT(vq, needed <= vq->vq_max_indirect_size,
691 "enqueuing too many indirect descriptors");
693 head_idx = vq->vq_desc_head_idx;
694 VQ_RING_ASSERT_VALID_IDX(vq, head_idx);
695 dp = &vq->vq_ring.desc[head_idx];
696 dxp = &vq->vq_descx[head_idx];
698 VQASSERT(vq, dxp->cookie == NULL,
699 "cookie already exists for index %d", head_idx);
700 dxp->cookie = cookie;
703 dp->addr = dxp->indirect_paddr;
704 dp->len = needed * sizeof(struct vring_desc);
705 dp->flags = VRING_DESC_F_INDIRECT;
707 vq_ring_enqueue_segments(vq, dxp->indirect, 0,
708 sg, readable, writable);
710 vq->vq_desc_head_idx = dp->next;
712 if (vq->vq_free_cnt == 0)
713 VQ_RING_ASSERT_CHAIN_TERM(vq);
715 VQ_RING_ASSERT_VALID_IDX(vq, vq->vq_desc_head_idx);
717 vq_ring_update_avail(vq, head_idx);
721 vq_ring_enable_interrupt(struct virtqueue *vq, uint16_t ndesc)
725 * Enable interrupts, making sure we get the latest index of
726 * what's already been consumed.
728 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX)
729 vring_used_event(&vq->vq_ring) = vq->vq_used_cons_idx + ndesc;
731 vq->vq_ring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
736 * Enough items may have already been consumed to meet our threshold
737 * since we last checked. Let our caller know so it processes the new
740 if (virtqueue_nused(vq) > ndesc)
747 vq_ring_must_notify_host(struct virtqueue *vq)
749 uint16_t new_idx, prev_idx, event_idx;
751 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) {
752 new_idx = vq->vq_ring.avail->idx;
753 prev_idx = new_idx - vq->vq_queued_cnt;
754 event_idx = vring_avail_event(&vq->vq_ring);
756 return (vring_need_event(event_idx, new_idx, prev_idx) != 0);
759 return ((vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY) == 0);
763 vq_ring_notify_host(struct virtqueue *vq)
766 VIRTIO_BUS_NOTIFY_VQ(vq->vq_dev, vq->vq_queue_index);
770 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
772 struct vring_desc *dp;
773 struct vq_desc_extra *dxp;
775 VQ_RING_ASSERT_VALID_IDX(vq, desc_idx);
776 dp = &vq->vq_ring.desc[desc_idx];
777 dxp = &vq->vq_descx[desc_idx];
779 if (vq->vq_free_cnt == 0)
780 VQ_RING_ASSERT_CHAIN_TERM(vq);
782 vq->vq_free_cnt += dxp->ndescs;
785 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
786 while (dp->flags & VRING_DESC_F_NEXT) {
787 VQ_RING_ASSERT_VALID_IDX(vq, dp->next);
788 dp = &vq->vq_ring.desc[dp->next];
793 VQASSERT(vq, dxp->ndescs == 0,
794 "failed to free entire desc chain, remaining: %d", dxp->ndescs);
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;