Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2024 Intel Corporation
3 : : */
4 : :
5 : : #ifndef _COMMON_INTEL_TX_H_
6 : : #define _COMMON_INTEL_TX_H_
7 : :
8 : : #include <stdint.h>
9 : : #include <rte_mbuf.h>
10 : : #include <rte_ethdev.h>
11 : :
12 : : /* forward declaration of the common intel (ci) queue structure */
13 : : struct ci_tx_queue;
14 : :
15 : : /**
16 : : * Structure associated with each descriptor of the TX ring of a TX queue.
17 : : */
18 : : struct ci_tx_entry {
19 : : struct rte_mbuf *mbuf; /* mbuf associated with TX desc, if any. */
20 : : uint16_t next_id; /* Index of next descriptor in ring. */
21 : : uint16_t last_id; /* Index of last scattered descriptor. */
22 : : };
23 : :
24 : : /**
25 : : * Structure associated with each descriptor of the TX ring of a TX queue in vector Tx.
26 : : */
27 : : struct ci_tx_entry_vec {
28 : : struct rte_mbuf *mbuf; /* mbuf associated with TX desc, if any. */
29 : : };
30 : :
31 : : typedef void (*ice_tx_release_mbufs_t)(struct ci_tx_queue *txq);
32 : :
33 : : struct ci_tx_queue {
34 : : union { /* TX ring virtual address */
35 : : volatile struct i40e_tx_desc *i40e_tx_ring;
36 : : volatile struct iavf_tx_desc *iavf_tx_ring;
37 : : volatile struct ice_tx_desc *ice_tx_ring;
38 : : volatile struct idpf_base_tx_desc *idpf_tx_ring;
39 : : volatile union ixgbe_adv_tx_desc *ixgbe_tx_ring;
40 : : };
41 : : volatile uint8_t *qtx_tail; /* register address of tail */
42 : : union {
43 : : struct ci_tx_entry *sw_ring; /* virtual address of SW ring */
44 : : struct ci_tx_entry_vec *sw_ring_vec;
45 : : };
46 : : uint16_t nb_tx_desc; /* number of TX descriptors */
47 : : uint16_t tx_tail; /* current value of tail register */
48 : : uint16_t nb_tx_used; /* number of TX desc used since RS bit set */
49 : : /* index to last TX descriptor to have been cleaned */
50 : : uint16_t last_desc_cleaned;
51 : : /* Total number of TX descriptors ready to be allocated. */
52 : : uint16_t nb_tx_free;
53 : : /* Start freeing TX buffers if there are less free descriptors than
54 : : * this value.
55 : : */
56 : : uint16_t tx_free_thresh;
57 : : /* Number of TX descriptors to use before RS bit is set. */
58 : : uint16_t tx_rs_thresh;
59 : : uint16_t port_id; /* Device port identifier. */
60 : : uint16_t queue_id; /* TX queue index. */
61 : : uint16_t reg_idx;
62 : : uint16_t tx_next_dd;
63 : : uint16_t tx_next_rs;
64 : : uint64_t offloads;
65 : : uint64_t mbuf_errors;
66 : : rte_iova_t tx_ring_dma; /* TX ring DMA address */
67 : : bool tx_deferred_start; /* don't start this queue in dev start */
68 : : bool q_set; /* indicate if tx queue has been configured */
69 : : bool vector_tx; /* port is using vector TX */
70 : : union { /* the VSI this queue belongs to */
71 : : struct i40e_vsi *i40e_vsi;
72 : : struct iavf_vsi *iavf_vsi;
73 : : struct ice_vsi *ice_vsi;
74 : : };
75 : : const struct rte_memzone *mz;
76 : :
77 : : union {
78 : : struct { /* ICE driver specific values */
79 : : uint32_t q_teid; /* TX schedule node id. */
80 : : };
81 : : struct { /* I40E driver specific values */
82 : : uint8_t dcb_tc;
83 : : };
84 : : struct { /* iavf driver specific values */
85 : : uint16_t ipsec_crypto_pkt_md_offset;
86 : : uint8_t rel_mbufs_type;
87 : : #define IAVF_TX_FLAGS_VLAN_TAG_LOC_L2TAG1 BIT(0)
88 : : #define IAVF_TX_FLAGS_VLAN_TAG_LOC_L2TAG2 BIT(1)
89 : : uint8_t vlan_flag;
90 : : uint8_t tc;
91 : : bool use_ctx; /* with ctx info, each pkt needs two descriptors */
92 : : };
93 : : struct { /* ixgbe specific values */
94 : : const struct ixgbe_txq_ops *ops;
95 : : struct ixgbe_advctx_info *ctx_cache;
96 : : uint32_t ctx_curr;
97 : : uint8_t pthresh; /**< Prefetch threshold register. */
98 : : uint8_t hthresh; /**< Host threshold register. */
99 : : uint8_t wthresh; /**< Write-back threshold reg. */
100 : : uint8_t using_ipsec; /**< indicates that IPsec TX feature is in use */
101 : : uint8_t is_vf; /**< indicates that this is a VF queue */
102 : : uint8_t vf_ctx_initialized; /**< VF context descriptors initialized */
103 : : };
104 : : struct { /* idpf specific values */
105 : : volatile union {
106 : : struct idpf_flex_tx_sched_desc *desc_ring;
107 : : struct idpf_splitq_tx_compl_desc *compl_ring;
108 : : };
109 : : struct ci_tx_queue *complq;
110 : : void **txqs; /*only valid for split queue mode*/
111 : : uint32_t tx_start_qid;
112 : : uint16_t sw_nb_desc;
113 : : uint16_t sw_tail;
114 : : uint16_t rs_compl_count;
115 : : uint8_t expected_gen_id;
116 : : };
117 : : };
118 : : };
119 : :
120 : : static __rte_always_inline void
121 : : ci_tx_backlog_entry(struct ci_tx_entry *txep, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
122 : : {
123 : : for (uint16_t i = 0; i < (int)nb_pkts; ++i)
124 : : txep[i].mbuf = tx_pkts[i];
125 : : }
126 : :
127 : : static __rte_always_inline void
128 : : ci_tx_backlog_entry_vec(struct ci_tx_entry_vec *txep, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
129 : : {
130 [ # # # # : 0 : for (uint16_t i = 0; i < nb_pkts; ++i)
# # # # ]
131 : 0 : txep[i].mbuf = tx_pkts[i];
132 : : }
133 : :
134 : : #define IETH_VPMD_TX_MAX_FREE_BUF 64
135 : :
136 : : typedef int (*ci_desc_done_fn)(struct ci_tx_queue *txq, uint16_t idx);
137 : :
138 : : static __rte_always_inline int
139 : : ci_tx_free_bufs_vec(struct ci_tx_queue *txq, ci_desc_done_fn desc_done, bool ctx_descs)
140 : : {
141 : : int nb_free = 0;
142 : : struct rte_mbuf *free[IETH_VPMD_TX_MAX_FREE_BUF];
143 : : struct rte_mbuf *m;
144 : :
145 : : /* check DD bits on threshold descriptor */
146 [ # # # # : 0 : if (!desc_done(txq, txq->tx_next_dd))
# # # # ]
147 : : return 0;
148 : :
149 : 0 : const uint32_t n = txq->tx_rs_thresh >> ctx_descs;
150 : :
151 : : /* first buffer to free from S/W ring is at index
152 : : * tx_next_dd - (tx_rs_thresh - 1)
153 : : */
154 : 0 : struct ci_tx_entry_vec *txep = txq->sw_ring_vec;
155 : 0 : txep += (txq->tx_next_dd >> ctx_descs) - (n - 1);
156 : :
157 [ # # # # : 0 : if (txq->offloads & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE && (n & 31) == 0) {
# # # # #
# # # # #
# # ]
158 [ # # # # : 0 : struct rte_mempool *mp = txep[0].mbuf->pool;
# # # # ]
159 : : void **cache_objs;
160 : : struct rte_mempool_cache *cache = rte_mempool_default_cache(mp, rte_lcore_id());
161 : :
162 [ # # # # : 0 : if (cache == NULL)
# # # # ]
163 : 0 : goto normal;
164 : :
165 : 0 : cache_objs = &cache->objs[cache->len];
166 : :
167 [ # # # # : 0 : if (n > RTE_MEMPOOL_CACHE_MAX_SIZE) {
# # # # ]
168 : 0 : rte_mempool_ops_enqueue_bulk(mp, (void *)txep, n);
169 : 0 : goto done;
170 : : }
171 : :
172 : : /* The cache follows the following algorithm
173 : : * 1. Add the objects to the cache
174 : : * 2. Anything greater than the cache min value (if it
175 : : * crosses the cache flush threshold) is flushed to the ring.
176 : : */
177 : : /* Add elements back into the cache */
178 : : uint32_t copied = 0;
179 : : /* n is multiple of 32 */
180 [ # # # # : 0 : while (copied < n) {
# # # # ]
181 : 0 : memcpy(&cache_objs[copied], &txep[copied], 32 * sizeof(void *));
182 : 0 : copied += 32;
183 : : }
184 : 0 : cache->len += n;
185 : :
186 [ # # # # : 0 : if (cache->len >= cache->flushthresh) {
# # # # ]
187 : 0 : rte_mempool_ops_enqueue_bulk(mp, &cache->objs[cache->size],
188 : 0 : cache->len - cache->size);
189 : 0 : cache->len = cache->size;
190 : : }
191 : 0 : goto done;
192 : : }
193 : :
194 : 0 : normal:
195 [ # # # # : 0 : m = rte_pktmbuf_prefree_seg(txep[0].mbuf);
# # # # ]
196 [ # # # # : 0 : if (likely(m)) {
# # # # ]
197 : 0 : free[0] = m;
198 : : nb_free = 1;
199 [ # # # # : 0 : for (uint32_t i = 1; i < n; i++) {
# # # # ]
200 [ # # # # : 0 : m = rte_pktmbuf_prefree_seg(txep[i].mbuf);
# # # # ]
201 [ # # # # : 0 : if (likely(m)) {
# # # # ]
202 [ # # # # : 0 : if (likely(m->pool == free[0]->pool)) {
# # # # ]
203 : 0 : free[nb_free++] = m;
204 : : } else {
205 [ # # # # : 0 : rte_mempool_put_bulk(free[0]->pool, (void *)free, nb_free);
# # # # ]
206 : 0 : free[0] = m;
207 : : nb_free = 1;
208 : : }
209 : : }
210 : : }
211 [ # # # # : 0 : rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
# # # # ]
212 : : } else {
213 [ # # # # : 0 : for (uint32_t i = 1; i < n; i++) {
# # # # ]
214 [ # # # # : 0 : m = rte_pktmbuf_prefree_seg(txep[i].mbuf);
# # # # ]
215 [ # # # # : 0 : if (m)
# # # # ]
216 [ # # # # : 0 : rte_mempool_put(m->pool, m);
# # # # ]
217 : : }
218 : : }
219 : :
220 : 0 : done:
221 : : /* buffers were freed, update counters */
222 : 0 : txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_rs_thresh);
223 : 0 : txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_rs_thresh);
224 [ # # # # : 0 : if (txq->tx_next_dd >= txq->nb_tx_desc)
# # # # ]
225 : 0 : txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
226 : :
227 : 0 : return txq->tx_rs_thresh;
228 : : }
229 : :
230 : : static inline void
231 : 0 : ci_txq_release_all_mbufs(struct ci_tx_queue *txq, bool use_ctx)
232 : : {
233 [ # # # # ]: 0 : if (unlikely(!txq || !txq->sw_ring))
234 : : return;
235 : :
236 [ # # ]: 0 : if (!txq->vector_tx) {
237 [ # # ]: 0 : for (uint16_t i = 0; i < txq->nb_tx_desc; i++) {
238 [ # # ]: 0 : if (txq->sw_ring[i].mbuf != NULL) {
239 : : rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
240 : 0 : txq->sw_ring[i].mbuf = NULL;
241 : : }
242 : : }
243 : : return;
244 : : }
245 : :
246 : : /**
247 : : * vPMD tx will not set sw_ring's mbuf to NULL after free,
248 : : * so determining buffers to free is a little more complex.
249 : : */
250 : 0 : const uint16_t start = (txq->tx_next_dd - txq->tx_rs_thresh + 1) >> use_ctx;
251 : 0 : const uint16_t nb_desc = txq->nb_tx_desc >> use_ctx;
252 : 0 : const uint16_t end = txq->tx_tail >> use_ctx;
253 : :
254 : : uint16_t i = start;
255 [ # # ]: 0 : if (end < i) {
256 [ # # ]: 0 : for (; i < nb_desc; i++)
257 : 0 : rte_pktmbuf_free_seg(txq->sw_ring_vec[i].mbuf);
258 : : i = 0;
259 : : }
260 [ # # ]: 0 : for (; i < end; i++)
261 : 0 : rte_pktmbuf_free_seg(txq->sw_ring_vec[i].mbuf);
262 : 0 : memset(txq->sw_ring_vec, 0, sizeof(txq->sw_ring_vec[0]) * nb_desc);
263 : : }
264 : :
265 : : #endif /* _COMMON_INTEL_TX_H_ */
|