xref: /dpdk/drivers/net/virtio/virtqueue.c (revision 5dba3b9c4c131b88a78bcecfef39db23ebc47873)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4 #include <stdint.h>
5 
6 #include <rte_mbuf.h>
7 
8 #include "virtqueue.h"
9 #include "virtio_logs.h"
10 #include "virtio_pci.h"
11 #include "virtio_rxtx_simple.h"
12 
13 /*
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.
17  */
18 struct rte_mbuf *
19 virtqueue_detatch_unused(struct virtqueue *vq)
20 {
21 	struct rte_mbuf *cookie;
22 	int idx;
23 
24 	if (vq != NULL)
25 		for (idx = 0; idx < vq->vq_nentries; idx++) {
26 			cookie = vq->vq_descx[idx].cookie;
27 			if (cookie != NULL) {
28 				vq->vq_descx[idx].cookie = NULL;
29 				return cookie;
30 			}
31 		}
32 	return NULL;
33 }
34 
35 /* Flush the elements in the used ring. */
36 void
37 virtqueue_rxvq_flush(struct virtqueue *vq)
38 {
39 	struct virtnet_rx *rxq = &vq->rxq;
40 	struct virtio_hw *hw = vq->hw;
41 	struct vring_used_elem *uep;
42 	struct vq_desc_extra *dxp;
43 	uint16_t used_idx, desc_idx;
44 	uint16_t nb_used, i;
45 
46 	nb_used = VIRTQUEUE_NUSED(vq);
47 
48 	for (i = 0; i < nb_used; i++) {
49 		used_idx = vq->vq_used_cons_idx & (vq->vq_nentries - 1);
50 		uep = &vq->vq_ring.used->ring[used_idx];
51 		if (hw->use_simple_rx) {
52 			desc_idx = used_idx;
53 			rte_pktmbuf_free(vq->sw_ring[desc_idx]);
54 			vq->vq_free_cnt++;
55 		} else {
56 			desc_idx = (uint16_t)uep->id;
57 			dxp = &vq->vq_descx[desc_idx];
58 			if (dxp->cookie != NULL) {
59 				rte_pktmbuf_free(dxp->cookie);
60 				dxp->cookie = NULL;
61 			}
62 			vq_ring_free_chain(vq, desc_idx);
63 		}
64 		vq->vq_used_cons_idx++;
65 	}
66 
67 	if (hw->use_simple_rx) {
68 		while (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
69 			virtio_rxq_rearm_vec(rxq);
70 			if (virtqueue_kick_prepare(vq))
71 				virtqueue_notify(vq);
72 		}
73 	}
74 }
75