1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4 
5 #ifndef _IAVF_RXTX_VEC_COMMON_H_
6 #define _IAVF_RXTX_VEC_COMMON_H_
7 #include <stdint.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_malloc.h>
10 
11 #include "iavf.h"
12 #include "iavf_rxtx.h"
13 
14 static inline uint16_t
reassemble_packets(struct iavf_rx_queue * rxq,struct rte_mbuf ** rx_bufs,uint16_t nb_bufs,uint8_t * split_flags)15 reassemble_packets(struct iavf_rx_queue *rxq, struct rte_mbuf **rx_bufs,
16 		   uint16_t nb_bufs, uint8_t *split_flags)
17 {
18 	struct rte_mbuf *pkts[IAVF_VPMD_RX_MAX_BURST];
19 	struct rte_mbuf *start = rxq->pkt_first_seg;
20 	struct rte_mbuf *end =  rxq->pkt_last_seg;
21 	unsigned int pkt_idx, buf_idx;
22 
23 	for (buf_idx = 0, pkt_idx = 0; buf_idx < nb_bufs; buf_idx++) {
24 		if (end) {
25 			/* processing a split packet */
26 			end->next = rx_bufs[buf_idx];
27 			rx_bufs[buf_idx]->data_len += rxq->crc_len;
28 
29 			start->nb_segs++;
30 			start->pkt_len += rx_bufs[buf_idx]->data_len;
31 			end = end->next;
32 
33 			if (!split_flags[buf_idx]) {
34 				/* it's the last packet of the set */
35 				start->hash = end->hash;
36 				start->vlan_tci = end->vlan_tci;
37 				start->ol_flags = end->ol_flags;
38 				/* we need to strip crc for the whole packet */
39 				start->pkt_len -= rxq->crc_len;
40 				if (end->data_len > rxq->crc_len) {
41 					end->data_len -= rxq->crc_len;
42 				} else {
43 					/* free up last mbuf */
44 					struct rte_mbuf *secondlast = start;
45 
46 					start->nb_segs--;
47 					while (secondlast->next != end)
48 						secondlast = secondlast->next;
49 					secondlast->data_len -= (rxq->crc_len -
50 							end->data_len);
51 					secondlast->next = NULL;
52 					rte_pktmbuf_free_seg(end);
53 				}
54 				pkts[pkt_idx++] = start;
55 				start = NULL;
56 				end = NULL;
57 			}
58 		} else {
59 			/* not processing a split packet */
60 			if (!split_flags[buf_idx]) {
61 				/* not a split packet, save and skip */
62 				pkts[pkt_idx++] = rx_bufs[buf_idx];
63 				continue;
64 			}
65 			end = start = rx_bufs[buf_idx];
66 			rx_bufs[buf_idx]->data_len += rxq->crc_len;
67 			rx_bufs[buf_idx]->pkt_len += rxq->crc_len;
68 		}
69 	}
70 
71 	/* save the partial packet for next time */
72 	rxq->pkt_first_seg = start;
73 	rxq->pkt_last_seg = end;
74 	memcpy(rx_bufs, pkts, pkt_idx * (sizeof(*pkts)));
75 	return pkt_idx;
76 }
77 
78 static __rte_always_inline int
iavf_tx_free_bufs(struct iavf_tx_queue * txq)79 iavf_tx_free_bufs(struct iavf_tx_queue *txq)
80 {
81 	struct iavf_tx_entry *txep;
82 	uint32_t n;
83 	uint32_t i;
84 	int nb_free = 0;
85 	struct rte_mbuf *m, *free[IAVF_VPMD_TX_MAX_FREE_BUF];
86 
87 	/* check DD bits on threshold descriptor */
88 	if ((txq->tx_ring[txq->next_dd].cmd_type_offset_bsz &
89 			rte_cpu_to_le_64(IAVF_TXD_QW1_DTYPE_MASK)) !=
90 			rte_cpu_to_le_64(IAVF_TX_DESC_DTYPE_DESC_DONE))
91 		return 0;
92 
93 	n = txq->rs_thresh;
94 
95 	 /* first buffer to free from S/W ring is at index
96 	  * tx_next_dd - (tx_rs_thresh-1)
97 	  */
98 	txep = &txq->sw_ring[txq->next_dd - (n - 1)];
99 	m = rte_pktmbuf_prefree_seg(txep[0].mbuf);
100 	if (likely(m != NULL)) {
101 		free[0] = m;
102 		nb_free = 1;
103 		for (i = 1; i < n; i++) {
104 			m = rte_pktmbuf_prefree_seg(txep[i].mbuf);
105 			if (likely(m != NULL)) {
106 				if (likely(m->pool == free[0]->pool)) {
107 					free[nb_free++] = m;
108 				} else {
109 					rte_mempool_put_bulk(free[0]->pool,
110 							     (void *)free,
111 							     nb_free);
112 					free[0] = m;
113 					nb_free = 1;
114 				}
115 			}
116 		}
117 		rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
118 	} else {
119 		for (i = 1; i < n; i++) {
120 			m = rte_pktmbuf_prefree_seg(txep[i].mbuf);
121 			if (m)
122 				rte_mempool_put(m->pool, m);
123 		}
124 	}
125 
126 	/* buffers were freed, update counters */
127 	txq->nb_free = (uint16_t)(txq->nb_free + txq->rs_thresh);
128 	txq->next_dd = (uint16_t)(txq->next_dd + txq->rs_thresh);
129 	if (txq->next_dd >= txq->nb_tx_desc)
130 		txq->next_dd = (uint16_t)(txq->rs_thresh - 1);
131 
132 	return txq->rs_thresh;
133 }
134 
135 static __rte_always_inline void
tx_backlog_entry(struct iavf_tx_entry * txep,struct rte_mbuf ** tx_pkts,uint16_t nb_pkts)136 tx_backlog_entry(struct iavf_tx_entry *txep,
137 		 struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
138 {
139 	int i;
140 
141 	for (i = 0; i < (int)nb_pkts; ++i)
142 		txep[i].mbuf = tx_pkts[i];
143 }
144 
145 static inline void
_iavf_rx_queue_release_mbufs_vec(struct iavf_rx_queue * rxq)146 _iavf_rx_queue_release_mbufs_vec(struct iavf_rx_queue *rxq)
147 {
148 	const unsigned int mask = rxq->nb_rx_desc - 1;
149 	unsigned int i;
150 
151 	if (!rxq->sw_ring || rxq->rxrearm_nb >= rxq->nb_rx_desc)
152 		return;
153 
154 	/* free all mbufs that are valid in the ring */
155 	if (rxq->rxrearm_nb == 0) {
156 		for (i = 0; i < rxq->nb_rx_desc; i++) {
157 			if (rxq->sw_ring[i])
158 				rte_pktmbuf_free_seg(rxq->sw_ring[i]);
159 		}
160 	} else {
161 		for (i = rxq->rx_tail;
162 		     i != rxq->rxrearm_start;
163 		     i = (i + 1) & mask) {
164 			if (rxq->sw_ring[i])
165 				rte_pktmbuf_free_seg(rxq->sw_ring[i]);
166 		}
167 	}
168 
169 	rxq->rxrearm_nb = rxq->nb_rx_desc;
170 
171 	/* set all entries to NULL */
172 	memset(rxq->sw_ring, 0, sizeof(rxq->sw_ring[0]) * rxq->nb_rx_desc);
173 }
174 
175 static inline void
_iavf_tx_queue_release_mbufs_vec(struct iavf_tx_queue * txq)176 _iavf_tx_queue_release_mbufs_vec(struct iavf_tx_queue *txq)
177 {
178 	unsigned i;
179 	const uint16_t max_desc = (uint16_t)(txq->nb_tx_desc - 1);
180 
181 	if (!txq->sw_ring || txq->nb_free == max_desc)
182 		return;
183 
184 	i = txq->next_dd - txq->rs_thresh + 1;
185 	if (txq->tx_tail < i) {
186 		for (; i < txq->nb_tx_desc; i++) {
187 			rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
188 			txq->sw_ring[i].mbuf = NULL;
189 		}
190 		i = 0;
191 	}
192 }
193 
194 static inline int
iavf_rxq_vec_setup_default(struct iavf_rx_queue * rxq)195 iavf_rxq_vec_setup_default(struct iavf_rx_queue *rxq)
196 {
197 	uintptr_t p;
198 	struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
199 
200 	mb_def.nb_segs = 1;
201 	mb_def.data_off = RTE_PKTMBUF_HEADROOM;
202 	mb_def.port = rxq->port_id;
203 	rte_mbuf_refcnt_set(&mb_def, 1);
204 
205 	/* prevent compiler reordering: rearm_data covers previous fields */
206 	rte_compiler_barrier();
207 	p = (uintptr_t)&mb_def.rearm_data;
208 	rxq->mbuf_initializer = *(uint64_t *)p;
209 	return 0;
210 }
211 
212 static inline int
iavf_rx_vec_queue_default(struct iavf_rx_queue * rxq)213 iavf_rx_vec_queue_default(struct iavf_rx_queue *rxq)
214 {
215 	if (!rxq)
216 		return -1;
217 
218 	if (!rte_is_power_of_2(rxq->nb_rx_desc))
219 		return -1;
220 
221 	if (rxq->rx_free_thresh < IAVF_VPMD_RX_MAX_BURST)
222 		return -1;
223 
224 	if (rxq->nb_rx_desc % rxq->rx_free_thresh)
225 		return -1;
226 
227 	if (rxq->proto_xtr != IAVF_PROTO_XTR_NONE)
228 		return -1;
229 
230 	return 0;
231 }
232 
233 static inline int
iavf_tx_vec_queue_default(struct iavf_tx_queue * txq)234 iavf_tx_vec_queue_default(struct iavf_tx_queue *txq)
235 {
236 	if (!txq)
237 		return -1;
238 
239 	if (txq->offloads & IAVF_NO_VECTOR_FLAGS)
240 		return -1;
241 
242 	if (txq->rs_thresh < IAVF_VPMD_TX_MAX_BURST ||
243 	    txq->rs_thresh > IAVF_VPMD_TX_MAX_FREE_BUF)
244 		return -1;
245 
246 	return 0;
247 }
248 
249 static inline int
iavf_rx_vec_dev_check_default(struct rte_eth_dev * dev)250 iavf_rx_vec_dev_check_default(struct rte_eth_dev *dev)
251 {
252 	int i;
253 	struct iavf_rx_queue *rxq;
254 
255 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
256 		rxq = dev->data->rx_queues[i];
257 		if (iavf_rx_vec_queue_default(rxq))
258 			return -1;
259 	}
260 
261 	return 0;
262 }
263 
264 static inline int
iavf_tx_vec_dev_check_default(struct rte_eth_dev * dev)265 iavf_tx_vec_dev_check_default(struct rte_eth_dev *dev)
266 {
267 	int i;
268 	struct iavf_tx_queue *txq;
269 
270 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
271 		txq = dev->data->tx_queues[i];
272 		if (iavf_tx_vec_queue_default(txq))
273 			return -1;
274 	}
275 
276 	return 0;
277 }
278 
279 #endif
280