1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
9 #include "virtio_logs.h"
10 #include "virtio_pci.h"
11 #include "virtio_rxtx_simple.h"
14 * Two types of mbuf to be cleaned:
15 * 1) mbuf that has been consumed by backend but not used by virtio.
16 * 2) mbuf that hasn't been consued by backend.
19 virtqueue_detach_unused(struct virtqueue *vq)
21 struct rte_mbuf *cookie;
30 type = virtio_get_queue_type(hw, vq->vq_queue_index);
31 start = vq->vq_avail_idx & (vq->vq_nentries - 1);
32 end = (vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1);
34 for (idx = 0; idx < vq->vq_nentries; idx++) {
35 if (hw->use_vec_rx && !vtpci_packed_queue(hw) &&
37 if (start <= end && idx >= start && idx < end)
39 if (start > end && (idx >= start || idx < end))
41 cookie = vq->sw_ring[idx];
43 vq->sw_ring[idx] = NULL;
47 cookie = vq->vq_descx[idx].cookie;
49 vq->vq_descx[idx].cookie = NULL;
58 /* Flush used descs */
60 virtqueue_rxvq_flush_packed(struct virtqueue *vq)
62 struct vq_desc_extra *dxp;
65 struct vring_packed_desc *descs = vq->vq_packed.ring.desc;
68 i = vq->vq_used_cons_idx;
69 while (desc_is_used(&descs[i], vq) && cnt++ < vq->vq_nentries) {
70 dxp = &vq->vq_descx[descs[i].id];
71 if (dxp->cookie != NULL) {
72 rte_pktmbuf_free(dxp->cookie);
76 vq->vq_used_cons_idx++;
77 if (vq->vq_used_cons_idx >= vq->vq_nentries) {
78 vq->vq_used_cons_idx -= vq->vq_nentries;
79 vq->vq_packed.used_wrap_counter ^= 1;
81 i = vq->vq_used_cons_idx;
85 /* Flush the elements in the used ring. */
87 virtqueue_rxvq_flush_split(struct virtqueue *vq)
89 struct virtnet_rx *rxq = &vq->rxq;
90 struct virtio_hw *hw = vq->hw;
91 struct vring_used_elem *uep;
92 struct vq_desc_extra *dxp;
93 uint16_t used_idx, desc_idx;
96 nb_used = virtqueue_nused(vq);
98 for (i = 0; i < nb_used; i++) {
99 used_idx = vq->vq_used_cons_idx & (vq->vq_nentries - 1);
100 uep = &vq->vq_split.ring.used->ring[used_idx];
101 if (hw->use_vec_rx) {
103 rte_pktmbuf_free(vq->sw_ring[desc_idx]);
105 } else if (hw->use_inorder_rx) {
106 desc_idx = (uint16_t)uep->id;
107 dxp = &vq->vq_descx[desc_idx];
108 if (dxp->cookie != NULL) {
109 rte_pktmbuf_free(dxp->cookie);
112 vq_ring_free_inorder(vq, desc_idx, 1);
114 desc_idx = (uint16_t)uep->id;
115 dxp = &vq->vq_descx[desc_idx];
116 if (dxp->cookie != NULL) {
117 rte_pktmbuf_free(dxp->cookie);
120 vq_ring_free_chain(vq, desc_idx);
122 vq->vq_used_cons_idx++;
125 if (hw->use_vec_rx) {
126 while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
127 virtio_rxq_rearm_vec(rxq);
128 if (virtqueue_kick_prepare(vq))
129 virtqueue_notify(vq);
134 /* Flush the elements in the used ring. */
136 virtqueue_rxvq_flush(struct virtqueue *vq)
138 struct virtio_hw *hw = vq->hw;
140 if (vtpci_packed_queue(hw))
141 virtqueue_rxvq_flush_packed(vq);
143 virtqueue_rxvq_flush_split(vq);
147 virtqueue_rxvq_reset_packed(struct virtqueue *vq)
149 int size = vq->vq_nentries;
150 struct vq_desc_extra *dxp;
151 struct virtnet_rx *rxvq;
154 vq->vq_used_cons_idx = 0;
155 vq->vq_desc_head_idx = 0;
156 vq->vq_avail_idx = 0;
157 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
158 vq->vq_free_cnt = vq->vq_nentries;
160 vq->vq_packed.used_wrap_counter = 1;
161 vq->vq_packed.cached_flags = VRING_PACKED_DESC_F_AVAIL;
162 vq->vq_packed.event_flags_shadow = 0;
163 vq->vq_packed.cached_flags |= VRING_DESC_F_WRITE;
166 memset(rxvq->mz->addr, 0, rxvq->mz->len);
168 for (desc_idx = 0; desc_idx < vq->vq_nentries; desc_idx++) {
169 dxp = &vq->vq_descx[desc_idx];
170 if (dxp->cookie != NULL) {
171 rte_pktmbuf_free(dxp->cookie);
176 vring_desc_init_packed(vq, size);
178 virtqueue_disable_intr(vq);
183 virtqueue_txvq_reset_packed(struct virtqueue *vq)
185 int size = vq->vq_nentries;
186 struct vq_desc_extra *dxp;
187 struct virtnet_tx *txvq;
190 vq->vq_used_cons_idx = 0;
191 vq->vq_desc_head_idx = 0;
192 vq->vq_avail_idx = 0;
193 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
194 vq->vq_free_cnt = vq->vq_nentries;
196 vq->vq_packed.used_wrap_counter = 1;
197 vq->vq_packed.cached_flags = VRING_PACKED_DESC_F_AVAIL;
198 vq->vq_packed.event_flags_shadow = 0;
201 memset(txvq->mz->addr, 0, txvq->mz->len);
202 memset(txvq->virtio_net_hdr_mz->addr, 0,
203 txvq->virtio_net_hdr_mz->len);
205 for (desc_idx = 0; desc_idx < vq->vq_nentries; desc_idx++) {
206 dxp = &vq->vq_descx[desc_idx];
207 if (dxp->cookie != NULL) {
208 rte_pktmbuf_free(dxp->cookie);
213 vring_desc_init_packed(vq, size);
215 virtqueue_disable_intr(vq);