2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Implements the virtqueue interface as basically described
31 * in the original VirtIO paper.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/sglist.h>
45 #include <machine/cpu.h>
46 #include <machine/bus.h>
47 #include <machine/atomic.h>
48 #include <machine/resource.h>
52 #include <dev/virtio/virtio.h>
53 #include <dev/virtio/virtqueue.h>
54 #include <dev/virtio/virtio_ring.h>
56 #include "virtio_bus_if.h"
60 uint16_t vq_queue_index;
63 #define VIRTQUEUE_FLAG_MODERN 0x0001
64 #define VIRTQUEUE_FLAG_INDIRECT 0x0002
65 #define VIRTQUEUE_FLAG_EVENT_IDX 0x0004
67 int vq_max_indirect_size;
68 bus_size_t vq_notify_offset;
69 virtqueue_intr_t *vq_intrhand;
70 void *vq_intrhand_arg;
74 uint16_t vq_queued_cnt;
76 * Head of the free chain in the descriptor table. If
77 * there are no free descriptors, this will be set to
78 * VQ_RING_DESC_CHAIN_END.
80 uint16_t vq_desc_head_idx;
82 * Last consumed descriptor in the used table,
83 * trails vq_ring.used->idx.
85 uint16_t vq_used_cons_idx;
88 int vq_indirect_mem_size;
91 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
93 struct vq_desc_extra {
95 struct vring_desc *indirect;
96 vm_paddr_t indirect_paddr;
102 * The maximum virtqueue size is 2^15. Use that value as the end of
103 * descriptor chain terminator since it will never be a valid index
104 * in the descriptor table. This is used to verify we are correctly
105 * handling vq_free_cnt.
107 #define VQ_RING_DESC_CHAIN_END 32768
109 #define VQASSERT(_vq, _exp, _msg, ...) \
110 KASSERT((_exp),("%s: %s - "_msg, __func__, (_vq)->vq_name, \
113 #define VQ_RING_ASSERT_VALID_IDX(_vq, _idx) \
114 VQASSERT((_vq), (_idx) < (_vq)->vq_nentries, \
115 "invalid ring index: %d, max: %d", (_idx), \
118 #define VQ_RING_ASSERT_CHAIN_TERM(_vq) \
119 VQASSERT((_vq), (_vq)->vq_desc_head_idx == \
120 VQ_RING_DESC_CHAIN_END, "full ring terminated " \
121 "incorrectly: head idx: %d", (_vq)->vq_desc_head_idx)
123 static int virtqueue_init_indirect(struct virtqueue *vq, int);
124 static void virtqueue_free_indirect(struct virtqueue *vq);
125 static void virtqueue_init_indirect_list(struct virtqueue *,
126 struct vring_desc *);
128 static void vq_ring_init(struct virtqueue *);
129 static void vq_ring_update_avail(struct virtqueue *, uint16_t);
130 static uint16_t vq_ring_enqueue_segments(struct virtqueue *,
131 struct vring_desc *, uint16_t, struct sglist *, int, int);
132 static int vq_ring_use_indirect(struct virtqueue *, int);
133 static void vq_ring_enqueue_indirect(struct virtqueue *, void *,
134 struct sglist *, int, int);
135 static int vq_ring_enable_interrupt(struct virtqueue *, uint16_t);
136 static int vq_ring_must_notify_host(struct virtqueue *);
137 static void vq_ring_notify_host(struct virtqueue *);
138 static void vq_ring_free_chain(struct virtqueue *, uint16_t);
140 #define vq_modern(_vq) (((_vq)->vq_flags & VIRTQUEUE_FLAG_MODERN) != 0)
141 #define vq_htog16(_vq, _val) virtio_htog16(vq_modern(_vq), _val)
142 #define vq_htog32(_vq, _val) virtio_htog32(vq_modern(_vq), _val)
143 #define vq_htog64(_vq, _val) virtio_htog64(vq_modern(_vq), _val)
144 #define vq_gtoh16(_vq, _val) virtio_gtoh16(vq_modern(_vq), _val)
145 #define vq_gtoh32(_vq, _val) virtio_gtoh32(vq_modern(_vq), _val)
146 #define vq_gtoh64(_vq, _val) virtio_gtoh64(vq_modern(_vq), _val)
149 virtqueue_alloc(device_t dev, uint16_t queue, uint16_t size,
150 bus_size_t notify_offset, int align, vm_paddr_t highaddr,
151 struct vq_alloc_info *info, struct virtqueue **vqp)
153 struct virtqueue *vq;
161 "virtqueue %d (%s) does not exist (size is zero)\n",
162 queue, info->vqai_name);
164 } else if (!powerof2(size)) {
166 "virtqueue %d (%s) size is not a power of 2: %d\n",
167 queue, info->vqai_name, size);
169 } else if (info->vqai_maxindirsz > VIRTIO_MAX_INDIRECT) {
170 device_printf(dev, "virtqueue %d (%s) requested too many "
171 "indirect descriptors: %d, max %d\n",
172 queue, info->vqai_name, info->vqai_maxindirsz,
173 VIRTIO_MAX_INDIRECT);
177 vq = malloc(sizeof(struct virtqueue) +
178 size * sizeof(struct vq_desc_extra), M_DEVBUF, M_NOWAIT | M_ZERO);
180 device_printf(dev, "cannot allocate virtqueue\n");
185 strlcpy(vq->vq_name, info->vqai_name, sizeof(vq->vq_name));
186 vq->vq_queue_index = queue;
187 vq->vq_notify_offset = notify_offset;
188 vq->vq_alignment = align;
189 vq->vq_nentries = size;
190 vq->vq_free_cnt = size;
191 vq->vq_intrhand = info->vqai_intr;
192 vq->vq_intrhand_arg = info->vqai_intr_arg;
194 if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_F_VERSION_1) != 0)
195 vq->vq_flags |= VIRTQUEUE_FLAG_MODERN;
196 if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_RING_F_EVENT_IDX) != 0)
197 vq->vq_flags |= VIRTQUEUE_FLAG_EVENT_IDX;
199 if (info->vqai_maxindirsz > 1) {
200 error = virtqueue_init_indirect(vq, info->vqai_maxindirsz);
205 vq->vq_ring_size = round_page(vring_size(size, align));
206 vq->vq_ring_mem = contigmalloc(vq->vq_ring_size, M_DEVBUF,
207 M_NOWAIT | M_ZERO, 0, highaddr, PAGE_SIZE, 0);
208 if (vq->vq_ring_mem == NULL) {
210 "cannot allocate memory for virtqueue ring\n");
216 virtqueue_disable_intr(vq);
228 virtqueue_init_indirect(struct virtqueue *vq, int indirect_size)
231 struct vq_desc_extra *dxp;
236 if (VIRTIO_BUS_WITH_FEATURE(dev, VIRTIO_RING_F_INDIRECT_DESC) == 0) {
238 * Indirect descriptors requested by the driver but not
239 * negotiated. Return zero to keep the initialization
240 * going: we'll run fine without.
243 device_printf(dev, "virtqueue %d (%s) requested "
244 "indirect descriptors but not negotiated\n",
245 vq->vq_queue_index, vq->vq_name);
249 size = indirect_size * sizeof(struct vring_desc);
250 vq->vq_max_indirect_size = indirect_size;
251 vq->vq_indirect_mem_size = size;
252 vq->vq_flags |= VIRTQUEUE_FLAG_INDIRECT;
254 for (i = 0; i < vq->vq_nentries; i++) {
255 dxp = &vq->vq_descx[i];
257 dxp->indirect = malloc(size, M_DEVBUF, M_NOWAIT);
258 if (dxp->indirect == NULL) {
259 device_printf(dev, "cannot allocate indirect list\n");
263 dxp->indirect_paddr = vtophys(dxp->indirect);
264 virtqueue_init_indirect_list(vq, dxp->indirect);
271 virtqueue_free_indirect(struct virtqueue *vq)
273 struct vq_desc_extra *dxp;
276 for (i = 0; i < vq->vq_nentries; i++) {
277 dxp = &vq->vq_descx[i];
279 if (dxp->indirect == NULL)
282 free(dxp->indirect, M_DEVBUF);
283 dxp->indirect = NULL;
284 dxp->indirect_paddr = 0;
287 vq->vq_flags &= ~VIRTQUEUE_FLAG_INDIRECT;
288 vq->vq_indirect_mem_size = 0;
292 virtqueue_init_indirect_list(struct virtqueue *vq,
293 struct vring_desc *indirect)
297 bzero(indirect, vq->vq_indirect_mem_size);
299 for (i = 0; i < vq->vq_max_indirect_size - 1; i++)
300 indirect[i].next = vq_gtoh16(vq, i + 1);
301 indirect[i].next = vq_gtoh16(vq, VQ_RING_DESC_CHAIN_END);
305 virtqueue_reinit(struct virtqueue *vq, uint16_t size)
307 struct vq_desc_extra *dxp;
310 if (vq->vq_nentries != size) {
311 device_printf(vq->vq_dev,
312 "%s: '%s' changed size; old=%hu, new=%hu\n",
313 __func__, vq->vq_name, vq->vq_nentries, size);
317 /* Warn if the virtqueue was not properly cleaned up. */
318 if (vq->vq_free_cnt != vq->vq_nentries) {
319 device_printf(vq->vq_dev,
320 "%s: warning '%s' virtqueue not empty, "
321 "leaking %d entries\n", __func__, vq->vq_name,
322 vq->vq_nentries - vq->vq_free_cnt);
325 vq->vq_desc_head_idx = 0;
326 vq->vq_used_cons_idx = 0;
327 vq->vq_queued_cnt = 0;
328 vq->vq_free_cnt = vq->vq_nentries;
330 /* To be safe, reset all our allocated memory. */
331 bzero(vq->vq_ring_mem, vq->vq_ring_size);
332 for (i = 0; i < vq->vq_nentries; i++) {
333 dxp = &vq->vq_descx[i];
336 if (vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT)
337 virtqueue_init_indirect_list(vq, dxp->indirect);
341 virtqueue_disable_intr(vq);
347 virtqueue_free(struct virtqueue *vq)
350 if (vq->vq_free_cnt != vq->vq_nentries) {
351 device_printf(vq->vq_dev, "%s: freeing non-empty virtqueue, "
352 "leaking %d entries\n", vq->vq_name,
353 vq->vq_nentries - vq->vq_free_cnt);
356 if (vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT)
357 virtqueue_free_indirect(vq);
359 if (vq->vq_ring_mem != NULL) {
360 contigfree(vq->vq_ring_mem, vq->vq_ring_size, M_DEVBUF);
361 vq->vq_ring_size = 0;
362 vq->vq_ring_mem = NULL;
369 virtqueue_paddr(struct virtqueue *vq)
372 return (vtophys(vq->vq_ring_mem));
376 virtqueue_desc_paddr(struct virtqueue *vq)
379 return (vtophys(vq->vq_ring.desc));
383 virtqueue_avail_paddr(struct virtqueue *vq)
386 return (vtophys(vq->vq_ring.avail));
390 virtqueue_used_paddr(struct virtqueue *vq)
393 return (vtophys(vq->vq_ring.used));
397 virtqueue_index(struct virtqueue *vq)
400 return (vq->vq_queue_index);
404 virtqueue_size(struct virtqueue *vq)
407 return (vq->vq_nentries);
411 virtqueue_nfree(struct virtqueue *vq)
414 return (vq->vq_free_cnt);
418 virtqueue_empty(struct virtqueue *vq)
421 return (vq->vq_nentries == vq->vq_free_cnt);
425 virtqueue_full(struct virtqueue *vq)
428 return (vq->vq_free_cnt == 0);
432 virtqueue_notify(struct virtqueue *vq)
435 /* Ensure updated avail->idx is visible to host. */
438 if (vq_ring_must_notify_host(vq))
439 vq_ring_notify_host(vq);
440 vq->vq_queued_cnt = 0;
444 virtqueue_nused(struct virtqueue *vq)
446 uint16_t used_idx, nused;
448 used_idx = vq_htog16(vq, vq->vq_ring.used->idx);
450 nused = (uint16_t)(used_idx - vq->vq_used_cons_idx);
451 VQASSERT(vq, nused <= vq->vq_nentries, "used more than available");
457 virtqueue_intr_filter(struct virtqueue *vq)
460 if (vq->vq_used_cons_idx == vq_htog16(vq, vq->vq_ring.used->idx))
463 virtqueue_disable_intr(vq);
469 virtqueue_intr(struct virtqueue *vq)
472 vq->vq_intrhand(vq->vq_intrhand_arg);
476 virtqueue_enable_intr(struct virtqueue *vq)
479 return (vq_ring_enable_interrupt(vq, 0));
483 virtqueue_postpone_intr(struct virtqueue *vq, vq_postpone_t hint)
485 uint16_t ndesc, avail_idx;
487 avail_idx = vq_htog16(vq, vq->vq_ring.avail->idx);
488 ndesc = (uint16_t)(avail_idx - vq->vq_used_cons_idx);
491 case VQ_POSTPONE_SHORT:
494 case VQ_POSTPONE_LONG:
495 ndesc = (ndesc * 3) / 4;
497 case VQ_POSTPONE_EMPTIED:
501 return (vq_ring_enable_interrupt(vq, ndesc));
505 * Note this is only considered a hint to the host.
508 virtqueue_disable_intr(struct virtqueue *vq)
511 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) {
512 vring_used_event(&vq->vq_ring) = vq_gtoh16(vq,
513 vq->vq_used_cons_idx - vq->vq_nentries - 1);
517 vq->vq_ring.avail->flags |= vq_gtoh16(vq, VRING_AVAIL_F_NO_INTERRUPT);
521 virtqueue_enqueue(struct virtqueue *vq, void *cookie, struct sglist *sg,
522 int readable, int writable)
524 struct vq_desc_extra *dxp;
526 uint16_t head_idx, idx;
528 needed = readable + writable;
530 VQASSERT(vq, cookie != NULL, "enqueuing with no cookie");
531 VQASSERT(vq, needed == sg->sg_nseg,
532 "segment count mismatch, %d, %d", needed, sg->sg_nseg);
534 needed <= vq->vq_nentries || needed <= vq->vq_max_indirect_size,
535 "too many segments to enqueue: %d, %d/%d", needed,
536 vq->vq_nentries, vq->vq_max_indirect_size);
540 if (vq->vq_free_cnt == 0)
543 if (vq_ring_use_indirect(vq, needed)) {
544 vq_ring_enqueue_indirect(vq, cookie, sg, readable, writable);
546 } else if (vq->vq_free_cnt < needed)
549 head_idx = vq->vq_desc_head_idx;
550 VQ_RING_ASSERT_VALID_IDX(vq, head_idx);
551 dxp = &vq->vq_descx[head_idx];
553 VQASSERT(vq, dxp->cookie == NULL,
554 "cookie already exists for index %d", head_idx);
555 dxp->cookie = cookie;
556 dxp->ndescs = needed;
558 idx = vq_ring_enqueue_segments(vq, vq->vq_ring.desc, head_idx,
559 sg, readable, writable);
561 vq->vq_desc_head_idx = idx;
562 vq->vq_free_cnt -= needed;
563 if (vq->vq_free_cnt == 0)
564 VQ_RING_ASSERT_CHAIN_TERM(vq);
566 VQ_RING_ASSERT_VALID_IDX(vq, idx);
568 vq_ring_update_avail(vq, head_idx);
574 virtqueue_dequeue(struct virtqueue *vq, uint32_t *len)
576 struct vring_used_elem *uep;
578 uint16_t used_idx, desc_idx;
580 if (vq->vq_used_cons_idx == vq_htog16(vq, vq->vq_ring.used->idx))
583 used_idx = vq->vq_used_cons_idx++ & (vq->vq_nentries - 1);
584 uep = &vq->vq_ring.used->ring[used_idx];
587 desc_idx = (uint16_t) vq_htog32(vq, uep->id);
589 *len = vq_htog32(vq, uep->len);
591 vq_ring_free_chain(vq, desc_idx);
593 cookie = vq->vq_descx[desc_idx].cookie;
594 VQASSERT(vq, cookie != NULL, "no cookie for index %d", desc_idx);
595 vq->vq_descx[desc_idx].cookie = NULL;
601 virtqueue_poll(struct virtqueue *vq, uint32_t *len)
605 VIRTIO_BUS_POLL(vq->vq_dev);
606 while ((cookie = virtqueue_dequeue(vq, len)) == NULL) {
608 VIRTIO_BUS_POLL(vq->vq_dev);
615 virtqueue_drain(struct virtqueue *vq, int *last)
623 while (idx < vq->vq_nentries && cookie == NULL) {
624 if ((cookie = vq->vq_descx[idx].cookie) != NULL) {
625 vq->vq_descx[idx].cookie = NULL;
626 /* Free chain to keep free count consistent. */
627 vq_ring_free_chain(vq, idx);
638 virtqueue_dump(struct virtqueue *vq)
644 printf("VQ: %s - size=%d; free=%d; used=%d; queued=%d; "
645 "desc_head_idx=%d; avail.idx=%d; used_cons_idx=%d; "
646 "used.idx=%d; used_event_idx=%d; avail.flags=0x%x; used.flags=0x%x\n",
647 vq->vq_name, vq->vq_nentries, vq->vq_free_cnt, virtqueue_nused(vq),
648 vq->vq_queued_cnt, vq->vq_desc_head_idx,
649 vq_htog16(vq, vq->vq_ring.avail->idx), vq->vq_used_cons_idx,
650 vq_htog16(vq, vq->vq_ring.used->idx),
651 vq_htog16(vq, vring_used_event(&vq->vq_ring)),
652 vq_htog16(vq, vq->vq_ring.avail->flags),
653 vq_htog16(vq, vq->vq_ring.used->flags));
657 vq_ring_init(struct virtqueue *vq)
663 ring_mem = vq->vq_ring_mem;
664 size = vq->vq_nentries;
667 vring_init(vr, size, ring_mem, vq->vq_alignment);
669 for (i = 0; i < size - 1; i++)
670 vr->desc[i].next = vq_gtoh16(vq, i + 1);
671 vr->desc[i].next = vq_gtoh16(vq, VQ_RING_DESC_CHAIN_END);
675 vq_ring_update_avail(struct virtqueue *vq, uint16_t desc_idx)
677 uint16_t avail_idx, avail_ring_idx;
680 * Place the head of the descriptor chain into the next slot and make
681 * it usable to the host. The chain is made available now rather than
682 * deferring to virtqueue_notify() in the hopes that if the host is
683 * currently running on another CPU, we can keep it processing the new
686 avail_idx = vq_htog16(vq, vq->vq_ring.avail->idx);
687 avail_ring_idx = avail_idx & (vq->vq_nentries - 1);
688 vq->vq_ring.avail->ring[avail_ring_idx] = vq_gtoh16(vq, desc_idx);
691 vq->vq_ring.avail->idx = vq_gtoh16(vq, avail_idx + 1);
693 /* Keep pending count until virtqueue_notify(). */
698 vq_ring_enqueue_segments(struct virtqueue *vq, struct vring_desc *desc,
699 uint16_t head_idx, struct sglist *sg, int readable, int writable)
701 struct sglist_seg *seg;
702 struct vring_desc *dp;
706 needed = readable + writable;
708 for (i = 0, idx = head_idx, seg = sg->sg_segs;
710 i++, idx = vq_htog16(vq, dp->next), seg++) {
711 VQASSERT(vq, idx != VQ_RING_DESC_CHAIN_END,
712 "premature end of free desc chain");
715 dp->addr = vq_gtoh64(vq, seg->ss_paddr);
716 dp->len = vq_gtoh32(vq, seg->ss_len);
720 dp->flags |= vq_gtoh16(vq, VRING_DESC_F_NEXT);
722 dp->flags |= vq_gtoh16(vq, VRING_DESC_F_WRITE);
729 vq_ring_use_indirect(struct virtqueue *vq, int needed)
732 if ((vq->vq_flags & VIRTQUEUE_FLAG_INDIRECT) == 0)
735 if (vq->vq_max_indirect_size < needed)
745 vq_ring_enqueue_indirect(struct virtqueue *vq, void *cookie,
746 struct sglist *sg, int readable, int writable)
748 struct vring_desc *dp;
749 struct vq_desc_extra *dxp;
753 needed = readable + writable;
754 VQASSERT(vq, needed <= vq->vq_max_indirect_size,
755 "enqueuing too many indirect descriptors");
757 head_idx = vq->vq_desc_head_idx;
758 VQ_RING_ASSERT_VALID_IDX(vq, head_idx);
759 dp = &vq->vq_ring.desc[head_idx];
760 dxp = &vq->vq_descx[head_idx];
762 VQASSERT(vq, dxp->cookie == NULL,
763 "cookie already exists for index %d", head_idx);
764 dxp->cookie = cookie;
767 dp->addr = vq_gtoh64(vq, dxp->indirect_paddr);
768 dp->len = vq_gtoh32(vq, needed * sizeof(struct vring_desc));
769 dp->flags = vq_gtoh16(vq, VRING_DESC_F_INDIRECT);
771 vq_ring_enqueue_segments(vq, dxp->indirect, 0,
772 sg, readable, writable);
774 vq->vq_desc_head_idx = vq_htog16(vq, dp->next);
776 if (vq->vq_free_cnt == 0)
777 VQ_RING_ASSERT_CHAIN_TERM(vq);
779 VQ_RING_ASSERT_VALID_IDX(vq, vq->vq_desc_head_idx);
781 vq_ring_update_avail(vq, head_idx);
785 vq_ring_enable_interrupt(struct virtqueue *vq, uint16_t ndesc)
789 * Enable interrupts, making sure we get the latest index of
790 * what's already been consumed.
792 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) {
793 vring_used_event(&vq->vq_ring) =
794 vq_gtoh16(vq, vq->vq_used_cons_idx + ndesc);
796 vq->vq_ring.avail->flags &=
797 vq_gtoh16(vq, ~VRING_AVAIL_F_NO_INTERRUPT);
803 * Enough items may have already been consumed to meet our threshold
804 * since we last checked. Let our caller know so it processes the new
807 if (virtqueue_nused(vq) > ndesc)
814 vq_ring_must_notify_host(struct virtqueue *vq)
816 uint16_t new_idx, prev_idx, event_idx, flags;
818 if (vq->vq_flags & VIRTQUEUE_FLAG_EVENT_IDX) {
819 new_idx = vq_htog16(vq, vq->vq_ring.avail->idx);
820 prev_idx = new_idx - vq->vq_queued_cnt;
821 event_idx = vq_htog16(vq, vring_avail_event(&vq->vq_ring));
823 return (vring_need_event(event_idx, new_idx, prev_idx) != 0);
826 flags = vq->vq_ring.used->flags;
827 return ((flags & vq_gtoh16(vq, VRING_USED_F_NO_NOTIFY)) == 0);
831 vq_ring_notify_host(struct virtqueue *vq)
834 VIRTIO_BUS_NOTIFY_VQ(vq->vq_dev, vq->vq_queue_index,
835 vq->vq_notify_offset);
839 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
841 struct vring_desc *dp;
842 struct vq_desc_extra *dxp;
844 VQ_RING_ASSERT_VALID_IDX(vq, desc_idx);
845 dp = &vq->vq_ring.desc[desc_idx];
846 dxp = &vq->vq_descx[desc_idx];
848 if (vq->vq_free_cnt == 0)
849 VQ_RING_ASSERT_CHAIN_TERM(vq);
851 vq->vq_free_cnt += dxp->ndescs;
854 if ((dp->flags & vq_gtoh16(vq, VRING_DESC_F_INDIRECT)) == 0) {
855 while (dp->flags & vq_gtoh16(vq, VRING_DESC_F_NEXT)) {
856 uint16_t next_idx = vq_htog16(vq, dp->next);
857 VQ_RING_ASSERT_VALID_IDX(vq, next_idx);
858 dp = &vq->vq_ring.desc[next_idx];
863 VQASSERT(vq, dxp->ndescs == 0,
864 "failed to free entire desc chain, remaining: %d", dxp->ndescs);
867 * We must append the existing free chain, if any, to the end of
868 * newly freed chain. If the virtqueue was completely used, then
869 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
871 dp->next = vq_gtoh16(vq, vq->vq_desc_head_idx);
872 vq->vq_desc_head_idx = desc_idx;