2 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.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/virtio_config.h>
52 #include <dev/virtio/virtqueue.h>
53 #include <dev/virtio/virtio_ring.h>
55 #include "virtio_bus_if.h"
59 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
60 uint16_t vq_queue_index;
63 #define VIRTQUEUE_FLAG_INDIRECT 0x0001
64 #define VIRTQUEUE_FLAG_EVENT_IDX 0x0002
69 int vq_max_indirect_size;
70 int vq_indirect_mem_size;
71 virtqueue_intr_t *vq_intrhand;
72 void *vq_intrhand_arg;
76 uint16_t vq_queued_cnt;
78 * Head of the free chain in the descriptor table. If
79 * there are no free descriptors, this will be set to
80 * VQ_RING_DESC_CHAIN_END.
82 uint16_t vq_desc_head_idx;
84 * Last consumed descriptor in the used table,
85 * trails vq_ring.used->idx.
87 uint16_t vq_used_cons_idx;
89 struct vq_desc_extra {
91 struct vring_desc *indirect;
92 vm_paddr_t indirect_paddr;
98 * The maximum virtqueue size is 2^15. Use that value as the end of
99 * descriptor chain terminator since it will never be a valid index
100 * in the descriptor table. This is used to verify we are correctly
101 * handling vq_free_cnt.
103 #define VQ_RING_DESC_CHAIN_END 32768
105 #define VQASSERT(_vq, _exp, _msg, ...) \
106 KASSERT((_exp),("%s: %s - "_msg, __func__, (_vq)->vq_name, \
109 #define VQ_RING_ASSERT_VALID_IDX(_vq, _idx) \
110 VQASSERT((_vq), (_idx) < (_vq)->vq_nentries, \
111 "invalid ring index: %d, max: %d", (_idx), \
114 #define VQ_RING_ASSERT_CHAIN_TERM(_vq) \
115 VQASSERT((_vq), (_vq)->vq_desc_head_idx == \
116 VQ_RING_DESC_CHAIN_END, "full ring terminated " \
117 "incorrectly: head idx: %d", (_vq)->vq_desc_head_idx)
119 static int virtqueue_init_indirect(struct virtqueue *vq, int);
120 static void virtqueue_free_indirect(struct virtqueue *vq);
121 static void virtqueue_init_indirect_list(struct virtqueue *,
122 struct vring_desc *);
124 static void vq_ring_init(struct virtqueue *);
125 static void vq_ring_update_avail(struct virtqueue *, uint16_t);
126 static uint16_t vq_ring_enqueue_segments(struct virtqueue *,
127 struct vring_desc *, uint16_t, struct sglist *, int, int);
128 static int vq_ring_use_indirect(struct virtqueue *, int);
129 static void vq_ring_enqueue_indirect(struct virtqueue *, void *,
130 struct sglist *, int, int);
131 static int vq_ring_enable_interrupt(struct virtqueue *, uint16_t);
132 static int vq_ring_must_notify_host(struct virtqueue *);
133 static void vq_ring_notify_host(struct virtqueue *);
134 static void vq_ring_free_chain(struct virtqueue *, uint16_t);
137 virtqueue_filter_features(uint64_t features)
141 mask = (1 << VIRTIO_TRANSPORT_F_START) - 1;
142 mask |= VIRTIO_RING_F_INDIRECT_DESC;
143 mask |= VIRTIO_RING_F_EVENT_IDX;
145 return (features & mask);
149 virtqueue_alloc(device_t dev, uint16_t queue, uint16_t size, int align,
150 vm_paddr_t highaddr, struct vq_alloc_info *info, struct virtqueue **vqp)
152 struct virtqueue *vq;
160 "virtqueue %d (%s) does not exist (size is zero)\n",
161 queue, info->vqai_name);
163 } else if (!powerof2(size)) {
165 "virtqueue %d (%s) size is not a power of 2: %d\n",
166 queue, info->vqai_name, size);
168 } else if (info->vqai_maxindirsz > VIRTIO_MAX_INDIRECT) {
169 device_printf(dev, "virtqueue %d (%s) requested too many "
170 "indirect descriptors: %d, max %d\n",
171 queue, info->vqai_name, info->vqai_maxindirsz,
172 VIRTIO_MAX_INDIRECT);
176 vq = malloc(sizeof(struct virtqueue) +
177 size * sizeof(struct vq_desc_extra), M_DEVBUF, M_NOWAIT | M_ZERO);
179 device_printf(dev, "cannot allocate virtqueue\n");
184 strlcpy(vq->vq_name, info->vqai_name, sizeof(vq->vq_name));
185 vq->vq_queue_index = queue;
186 vq->vq_alignment = align;
187 vq->vq_nentries = size;
188 vq->vq_free_cnt = size;
189 vq->vq_intrhand = info->vqai_intr;
190 vq->vq_intrhand_arg = info->vqai_intr_arg;
192 if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_RING_F_EVENT_IDX) != 0)
193 vq->vq_flags |= VIRTQUEUE_FLAG_EVENT_IDX;
195 if (info->vqai_maxindirsz > 1) {
196 error = virtqueue_init_indirect(vq, info->vqai_maxindirsz);
201 vq->vq_ring_size = round_page(vring_size(size, align));
202 vq->vq_ring_mem = contigmalloc(vq->vq_ring_size, M_DEVBUF,
203 M_NOWAIT | M_ZERO, 0, highaddr, PAGE_SIZE, 0);
204 if (vq->vq_ring_mem == NULL) {
206 "cannot allocate memory for virtqueue ring\n");
212 virtqueue_disable_intr(vq);
224 virtqueue_init_indirect(struct virtqueue *vq, int indirect_size)
227 struct vq_desc_extra *dxp;
232 if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_RING_F_INDIRECT_DESC) == 0) {
234 * Indirect descriptors requested by the driver but not
235 * negotiated. Return zero to keep the initialization
236 * going: we'll run fine without.
239 device_printf(dev, "virtqueue %d (%s) requested "
240 "indirect descriptors but not negotiated\n",
241 vq->vq_queue_index, vq->vq_name);
245 size = indirect_size * sizeof(struct vring_desc);
246 vq->vq_max_indirect_size = indirect_size;
247 vq->vq_indirect_mem_size = size;
248 vq->vq_flags |= VIRTQUEUE_FLAG_INDIRECT;
250 for (i = 0; i < vq->vq_nentries; i++) {
251 dxp = &vq->vq_descx[i];
253 dxp->indirect = malloc(size, M_DEVBUF, M_NOWAIT);
254 if (dxp->indirect == NULL) {
255 device_printf(dev, "cannot allocate indirect list\n");
259 dxp->indirect_paddr = vtophys(dxp->indirect);
260 virtqueue_init_indirect_list(vq, dxp->indirect);
267 virtqueue_free_indirect(struct virtqueue *vq)
269 struct vq_desc_extra *dxp;
272 for (i = 0; i < vq->vq_nentries; i++) {
273 dxp = &vq->vq_descx[i];
275 if (dxp->indirect == NULL)
278 free(dxp->indirect, M_DEVBUF);
279 dxp->indirect = NULL;
280 dxp->indirect_paddr = 0;
283 vq->vq_flags &= ~VIRTQUEUE_FLAG_INDIRECT;
284 vq->vq_indirect_mem_size = 0;
288 virtqueue_init_indirect_list(struct virtqueue *vq,
289 struct vring_desc *indirect)
293 bzero(indirect, vq->vq_indirect_mem_size);
295 for (i = 0; i < vq->vq_max_indirect_size - 1; i++)
296 indirect[i].next = i + 1;
297 indirect[i].next = VQ_RING_DESC_CHAIN_END;
301 virtqueue_reinit(struct virtqueue *vq, uint16_t size)
303 struct vq_desc_extra *dxp;
306 if (vq->vq_nentries != size) {
307 device_printf(vq->vq_dev,
308 "%s: '%s' changed size; old=%hu, new=%hu\n",
309 __func__, vq->vq_name, vq->vq_nentries, size);
313 /* Warn if the virtqueue was not properly cleaned up. */
314 if (vq->vq_free_cnt != vq->vq_nentries) {
315 device_printf(vq->vq_dev,
316 "%s: warning '%s' virtqueue not empty, "
317 "leaking %d entries\n", __func__, vq->vq_name,
318 vq->vq_nentries - vq->vq_free_cnt);
321 vq->vq_desc_head_idx = 0;
322 vq->vq_used_cons_idx = 0;
323 vq->vq_queued_cnt = 0;
324 vq->vq_free_cnt = vq->vq_nentries;
326 /* To be safe, reset all our allocated memory. */
327 bzero(vq->vq_ring_mem, vq->vq_ring_size);
328 for (i = 0; i < vq->vq_nentries; i++) {
329 dxp = &vq->vq_descx[i];
332 if (vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT)
333 virtqueue_init_indirect_list(vq, dxp->indirect);
337 virtqueue_disable_intr(vq);
343 virtqueue_free(struct virtqueue *vq)
346 if (vq->vq_free_cnt != vq->vq_nentries) {
347 device_printf(vq->vq_dev, "%s: freeing non-empty virtqueue, "
348 "leaking %d entries\n", vq->vq_name,
349 vq->vq_nentries - vq->vq_free_cnt);
352 if (vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT)
353 virtqueue_free_indirect(vq);
355 if (vq->vq_ring_mem != NULL) {
356 contigfree(vq->vq_ring_mem, vq->vq_ring_size, M_DEVBUF);
357 vq->vq_ring_size = 0;
358 vq->vq_ring_mem = NULL;
365 virtqueue_paddr(struct virtqueue *vq)
368 return (vtophys(vq->vq_ring_mem));
372 virtqueue_size(struct virtqueue *vq)
375 return (vq->vq_nentries);
379 virtqueue_empty(struct virtqueue *vq)
382 return (vq->vq_nentries == vq->vq_free_cnt);
386 virtqueue_full(struct virtqueue *vq)
389 return (vq->vq_free_cnt == 0);
393 virtqueue_notify(struct virtqueue *vq)
396 /* Ensure updated avail->idx is visible to host. */
399 if (vq_ring_must_notify_host(vq))
400 vq_ring_notify_host(vq);
401 vq->vq_queued_cnt = 0;
405 virtqueue_nused(struct virtqueue *vq)
407 uint16_t used_idx, nused;
409 used_idx = vq->vq_ring.used->idx;
411 nused = (uint16_t)(used_idx - vq->vq_used_cons_idx);
412 VQASSERT(vq, nused <= vq->vq_nentries, "used more than available");
418 virtqueue_intr_filter(struct virtqueue *vq)
421 if (vq->vq_used_cons_idx == vq->vq_ring.used->idx)
424 virtqueue_disable_intr(vq);
430 virtqueue_intr(struct virtqueue *vq)
433 vq->vq_intrhand(vq->vq_intrhand_arg);
437 virtqueue_enable_intr(struct virtqueue *vq)
440 return (vq_ring_enable_interrupt(vq, 0));
444 virtqueue_postpone_intr(struct virtqueue *vq, vq_postpone_t hint)
446 uint16_t ndesc, avail_idx;
448 avail_idx = vq->vq_ring.avail->idx;
449 ndesc = (uint16_t)(avail_idx - vq->vq_used_cons_idx);
452 case VQ_POSTPONE_SHORT:
455 case VQ_POSTPONE_LONG:
456 ndesc = (ndesc * 3) / 4;
458 case VQ_POSTPONE_EMPTIED:
462 return (vq_ring_enable_interrupt(vq, ndesc));
466 * Note this is only considered a hint to the host.
469 virtqueue_disable_intr(struct virtqueue *vq)
472 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) {
473 vring_used_event(&vq->vq_ring) = vq->vq_used_cons_idx -
476 vq->vq_ring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
480 virtqueue_enqueue(struct virtqueue *vq, void *cookie, struct sglist *sg,
481 int readable, int writable)
483 struct vq_desc_extra *dxp;
485 uint16_t head_idx, idx;
487 needed = readable + writable;
489 VQASSERT(vq, cookie != NULL, "enqueuing with no cookie");
490 VQASSERT(vq, needed == sg->sg_nseg,
491 "segment count mismatch, %d, %d", needed, sg->sg_nseg);
493 needed <= vq->vq_nentries || needed <= vq->vq_max_indirect_size,
494 "too many segments to enqueue: %d, %d/%d", needed,
495 vq->vq_nentries, vq->vq_max_indirect_size);
499 if (vq->vq_free_cnt == 0)
502 if (vq_ring_use_indirect(vq, needed)) {
503 vq_ring_enqueue_indirect(vq, cookie, sg, readable, writable);
505 } else if (vq->vq_free_cnt < needed)
508 head_idx = vq->vq_desc_head_idx;
509 VQ_RING_ASSERT_VALID_IDX(vq, head_idx);
510 dxp = &vq->vq_descx[head_idx];
512 VQASSERT(vq, dxp->cookie == NULL,
513 "cookie already exists for index %d", head_idx);
514 dxp->cookie = cookie;
515 dxp->ndescs = needed;
517 idx = vq_ring_enqueue_segments(vq, vq->vq_ring.desc, head_idx,
518 sg, readable, writable);
520 vq->vq_desc_head_idx = idx;
521 vq->vq_free_cnt -= needed;
522 if (vq->vq_free_cnt == 0)
523 VQ_RING_ASSERT_CHAIN_TERM(vq);
525 VQ_RING_ASSERT_VALID_IDX(vq, idx);
527 vq_ring_update_avail(vq, head_idx);
533 virtqueue_dequeue(struct virtqueue *vq, uint32_t *len)
535 struct vring_used_elem *uep;
537 uint16_t used_idx, desc_idx;
539 if (vq->vq_used_cons_idx == vq->vq_ring.used->idx)
542 used_idx = vq->vq_used_cons_idx++ & (vq->vq_nentries - 1);
543 uep = &vq->vq_ring.used->ring[used_idx];
546 desc_idx = (uint16_t) uep->id;
550 vq_ring_free_chain(vq, desc_idx);
552 cookie = vq->vq_descx[desc_idx].cookie;
553 VQASSERT(vq, cookie != NULL, "no cookie for index %d", desc_idx);
554 vq->vq_descx[desc_idx].cookie = NULL;
560 virtqueue_poll(struct virtqueue *vq, uint32_t *len)
564 while ((cookie = virtqueue_dequeue(vq, len)) == NULL)
571 virtqueue_drain(struct virtqueue *vq, int *last)
579 while (idx < vq->vq_nentries && cookie == NULL) {
580 if ((cookie = vq->vq_descx[idx].cookie) != NULL) {
581 vq->vq_descx[idx].cookie = NULL;
582 /* Free chain to keep free count consistent. */
583 vq_ring_free_chain(vq, idx);
594 virtqueue_dump(struct virtqueue *vq)
600 printf("VQ: %s - size=%d; free=%d; used=%d; queued=%d; "
601 "desc_head_idx=%d; avail.idx=%d; used_cons_idx=%d; "
602 "used.idx=%d; avail.flags=0x%x; used.flags=0x%x\n",
603 vq->vq_name, vq->vq_nentries, vq->vq_free_cnt,
604 virtqueue_nused(vq), vq->vq_queued_cnt, vq->vq_desc_head_idx,
605 vq->vq_ring.avail->idx, vq->vq_used_cons_idx,
606 vq->vq_ring.used->idx, vq->vq_ring.avail->flags,
607 vq->vq_ring.used->flags);
611 vq_ring_init(struct virtqueue *vq)
617 ring_mem = vq->vq_ring_mem;
618 size = vq->vq_nentries;
621 vring_init(vr, size, ring_mem, vq->vq_alignment);
623 for (i = 0; i < size - 1; i++)
624 vr->desc[i].next = i + 1;
625 vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
629 vq_ring_update_avail(struct virtqueue *vq, uint16_t desc_idx)
634 * Place the head of the descriptor chain into the next slot and make
635 * it usable to the host. The chain is made available now rather than
636 * deferring to virtqueue_notify() in the hopes that if the host is
637 * currently running on another CPU, we can keep it processing the new
640 avail_idx = vq->vq_ring.avail->idx & (vq->vq_nentries - 1);
641 vq->vq_ring.avail->ring[avail_idx] = desc_idx;
644 vq->vq_ring.avail->idx++;
646 /* Keep pending count until virtqueue_notify(). */
651 vq_ring_enqueue_segments(struct virtqueue *vq, struct vring_desc *desc,
652 uint16_t head_idx, struct sglist *sg, int readable, int writable)
654 struct sglist_seg *seg;
655 struct vring_desc *dp;
659 needed = readable + writable;
661 for (i = 0, idx = head_idx, seg = sg->sg_segs;
663 i++, idx = dp->next, seg++) {
664 VQASSERT(vq, idx != VQ_RING_DESC_CHAIN_END,
665 "premature end of free desc chain");
668 dp->addr = seg->ss_paddr;
669 dp->len = seg->ss_len;
673 dp->flags |= VRING_DESC_F_NEXT;
675 dp->flags |= VRING_DESC_F_WRITE;
682 vq_ring_use_indirect(struct virtqueue *vq, int needed)
685 if ((vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT) == 0)
688 if (vq->vq_max_indirect_size < needed)
698 vq_ring_enqueue_indirect(struct virtqueue *vq, void *cookie,
699 struct sglist *sg, int readable, int writable)
701 struct vring_desc *dp;
702 struct vq_desc_extra *dxp;
706 needed = readable + writable;
707 VQASSERT(vq, needed <= vq->vq_max_indirect_size,
708 "enqueuing too many indirect descriptors");
710 head_idx = vq->vq_desc_head_idx;
711 VQ_RING_ASSERT_VALID_IDX(vq, head_idx);
712 dp = &vq->vq_ring.desc[head_idx];
713 dxp = &vq->vq_descx[head_idx];
715 VQASSERT(vq, dxp->cookie == NULL,
716 "cookie already exists for index %d", head_idx);
717 dxp->cookie = cookie;
720 dp->addr = dxp->indirect_paddr;
721 dp->len = needed * sizeof(struct vring_desc);
722 dp->flags = VRING_DESC_F_INDIRECT;
724 vq_ring_enqueue_segments(vq, dxp->indirect, 0,
725 sg, readable, writable);
727 vq->vq_desc_head_idx = dp->next;
729 if (vq->vq_free_cnt == 0)
730 VQ_RING_ASSERT_CHAIN_TERM(vq);
732 VQ_RING_ASSERT_VALID_IDX(vq, vq->vq_desc_head_idx);
734 vq_ring_update_avail(vq, head_idx);
738 vq_ring_enable_interrupt(struct virtqueue *vq, uint16_t ndesc)
742 * Enable interrupts, making sure we get the latest index of
743 * what's already been consumed.
745 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX)
746 vring_used_event(&vq->vq_ring) = vq->vq_used_cons_idx + ndesc;
748 vq->vq_ring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
753 * Enough items may have already been consumed to meet our threshold
754 * since we last checked. Let our caller know so it processes the new
757 if (virtqueue_nused(vq) > ndesc)
764 vq_ring_must_notify_host(struct virtqueue *vq)
766 uint16_t new_idx, prev_idx, event_idx;
768 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) {
769 new_idx = vq->vq_ring.avail->idx;
770 prev_idx = new_idx - vq->vq_queued_cnt;
771 event_idx = vring_avail_event(&vq->vq_ring);
773 return (vring_need_event(event_idx, new_idx, prev_idx) != 0);
776 return ((vq->vq_ring.used->flags & VRING_USED_F_NO_NOTIFY) == 0);
780 vq_ring_notify_host(struct virtqueue *vq)
783 VIRTIO_BUS_NOTIFY_VQ(vq->vq_dev, vq->vq_queue_index);
787 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
789 struct vring_desc *dp;
790 struct vq_desc_extra *dxp;
792 VQ_RING_ASSERT_VALID_IDX(vq, desc_idx);
793 dp = &vq->vq_ring.desc[desc_idx];
794 dxp = &vq->vq_descx[desc_idx];
796 if (vq->vq_free_cnt == 0)
797 VQ_RING_ASSERT_CHAIN_TERM(vq);
799 vq->vq_free_cnt += dxp->ndescs;
802 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
803 while (dp->flags & VRING_DESC_F_NEXT) {
804 VQ_RING_ASSERT_VALID_IDX(vq, dp->next);
805 dp = &vq->vq_ring.desc[dp->next];
810 VQASSERT(vq, dxp->ndescs == 0,
811 "failed to free entire desc chain, remaining: %d", dxp->ndescs);
814 * We must append the existing free chain, if any, to the end of
815 * newly freed chain. If the virtqueue was completely used, then
816 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
818 dp->next = vq->vq_desc_head_idx;
819 vq->vq_desc_head_idx = desc_idx;