net/mlx5: prepare Tx to support scheduling
[dpdk.git] / drivers / net / mlx5 / mlx5_rxtx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015-2019 Mellanox Technologies, Ltd
4  */
5
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 /* Verbs header. */
11 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
12 #ifdef PEDANTIC
13 #pragma GCC diagnostic ignored "-Wpedantic"
14 #endif
15 #include <infiniband/verbs.h>
16 #include <infiniband/mlx5dv.h>
17 #ifdef PEDANTIC
18 #pragma GCC diagnostic error "-Wpedantic"
19 #endif
20
21 #include <rte_mbuf.h>
22 #include <rte_mempool.h>
23 #include <rte_prefetch.h>
24 #include <rte_common.h>
25 #include <rte_branch_prediction.h>
26 #include <rte_ether.h>
27 #include <rte_cycles.h>
28 #include <rte_flow.h>
29
30 #include <mlx5_devx_cmds.h>
31 #include <mlx5_prm.h>
32 #include <mlx5_common.h>
33
34 #include "mlx5_defs.h"
35 #include "mlx5.h"
36 #include "mlx5_mr.h"
37 #include "mlx5_utils.h"
38 #include "mlx5_rxtx.h"
39 #include "mlx5_autoconf.h"
40
41 /* TX burst subroutines return codes. */
42 enum mlx5_txcmp_code {
43         MLX5_TXCMP_CODE_EXIT = 0,
44         MLX5_TXCMP_CODE_ERROR,
45         MLX5_TXCMP_CODE_SINGLE,
46         MLX5_TXCMP_CODE_MULTI,
47         MLX5_TXCMP_CODE_TSO,
48         MLX5_TXCMP_CODE_EMPW,
49 };
50
51 /*
52  * These defines are used to configure Tx burst routine option set
53  * supported at compile time. The not specified options are optimized out
54  * out due to if conditions can be explicitly calculated at compile time.
55  * The offloads with bigger runtime check (require more CPU cycles to
56  * skip) overhead should have the bigger index - this is needed to
57  * select the better matching routine function if no exact match and
58  * some offloads are not actually requested.
59  */
60 #define MLX5_TXOFF_CONFIG_MULTI (1u << 0) /* Multi-segment packets.*/
61 #define MLX5_TXOFF_CONFIG_TSO (1u << 1) /* TCP send offload supported.*/
62 #define MLX5_TXOFF_CONFIG_SWP (1u << 2) /* Tunnels/SW Parser offloads.*/
63 #define MLX5_TXOFF_CONFIG_CSUM (1u << 3) /* Check Sums offloaded. */
64 #define MLX5_TXOFF_CONFIG_INLINE (1u << 4) /* Data inlining supported. */
65 #define MLX5_TXOFF_CONFIG_VLAN (1u << 5) /* VLAN insertion supported.*/
66 #define MLX5_TXOFF_CONFIG_METADATA (1u << 6) /* Flow metadata. */
67 #define MLX5_TXOFF_CONFIG_EMPW (1u << 8) /* Enhanced MPW supported.*/
68 #define MLX5_TXOFF_CONFIG_MPW (1u << 9) /* Legacy MPW supported.*/
69 #define MLX5_TXOFF_CONFIG_TXPP (1u << 10) /* Scheduling on timestamp.*/
70
71 /* The most common offloads groups. */
72 #define MLX5_TXOFF_CONFIG_NONE 0
73 #define MLX5_TXOFF_CONFIG_FULL (MLX5_TXOFF_CONFIG_MULTI | \
74                                 MLX5_TXOFF_CONFIG_TSO | \
75                                 MLX5_TXOFF_CONFIG_SWP | \
76                                 MLX5_TXOFF_CONFIG_CSUM | \
77                                 MLX5_TXOFF_CONFIG_INLINE | \
78                                 MLX5_TXOFF_CONFIG_VLAN | \
79                                 MLX5_TXOFF_CONFIG_METADATA)
80
81 #define MLX5_TXOFF_CONFIG(mask) (olx & MLX5_TXOFF_CONFIG_##mask)
82
83 #define MLX5_TXOFF_DECL(func, olx) \
84 static uint16_t mlx5_tx_burst_##func(void *txq, \
85                                      struct rte_mbuf **pkts, \
86                                     uint16_t pkts_n) \
87 { \
88         return mlx5_tx_burst_tmpl((struct mlx5_txq_data *)txq, \
89                     pkts, pkts_n, (olx)); \
90 }
91
92 #define MLX5_TXOFF_INFO(func, olx) {mlx5_tx_burst_##func, olx},
93
94 static __rte_always_inline uint32_t
95 rxq_cq_to_pkt_type(struct mlx5_rxq_data *rxq, volatile struct mlx5_cqe *cqe);
96
97 static __rte_always_inline int
98 mlx5_rx_poll_len(struct mlx5_rxq_data *rxq, volatile struct mlx5_cqe *cqe,
99                  uint16_t cqe_cnt, volatile struct mlx5_mini_cqe8 **mcqe);
100
101 static __rte_always_inline uint32_t
102 rxq_cq_to_ol_flags(volatile struct mlx5_cqe *cqe);
103
104 static __rte_always_inline void
105 rxq_cq_to_mbuf(struct mlx5_rxq_data *rxq, struct rte_mbuf *pkt,
106                volatile struct mlx5_cqe *cqe, uint32_t rss_hash_res);
107
108 static __rte_always_inline void
109 mprq_buf_replace(struct mlx5_rxq_data *rxq, uint16_t rq_idx,
110                  const unsigned int strd_n);
111
112 static int
113 mlx5_queue_state_modify(struct rte_eth_dev *dev,
114                         struct mlx5_mp_arg_queue_state_modify *sm);
115
116 static inline void
117 mlx5_lro_update_tcp_hdr(struct rte_tcp_hdr *__rte_restrict tcp,
118                         volatile struct mlx5_cqe *__rte_restrict cqe,
119                         uint32_t phcsum);
120
121 static inline void
122 mlx5_lro_update_hdr(uint8_t *__rte_restrict padd,
123                     volatile struct mlx5_cqe *__rte_restrict cqe,
124                     uint32_t len);
125
126 uint32_t mlx5_ptype_table[] __rte_cache_aligned = {
127         [0xff] = RTE_PTYPE_ALL_MASK, /* Last entry for errored packet. */
128 };
129
130 uint8_t mlx5_cksum_table[1 << 10] __rte_cache_aligned;
131 uint8_t mlx5_swp_types_table[1 << 10] __rte_cache_aligned;
132
133 uint64_t rte_net_mlx5_dynf_inline_mask;
134 #define PKT_TX_DYNF_NOINLINE rte_net_mlx5_dynf_inline_mask
135
136 /**
137  * Build a table to translate Rx completion flags to packet type.
138  *
139  * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
140  */
141 void
142 mlx5_set_ptype_table(void)
143 {
144         unsigned int i;
145         uint32_t (*p)[RTE_DIM(mlx5_ptype_table)] = &mlx5_ptype_table;
146
147         /* Last entry must not be overwritten, reserved for errored packet. */
148         for (i = 0; i < RTE_DIM(mlx5_ptype_table) - 1; ++i)
149                 (*p)[i] = RTE_PTYPE_UNKNOWN;
150         /*
151          * The index to the array should have:
152          * bit[1:0] = l3_hdr_type
153          * bit[4:2] = l4_hdr_type
154          * bit[5] = ip_frag
155          * bit[6] = tunneled
156          * bit[7] = outer_l3_type
157          */
158         /* L2 */
159         (*p)[0x00] = RTE_PTYPE_L2_ETHER;
160         /* L3 */
161         (*p)[0x01] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
162                      RTE_PTYPE_L4_NONFRAG;
163         (*p)[0x02] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
164                      RTE_PTYPE_L4_NONFRAG;
165         /* Fragmented */
166         (*p)[0x21] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
167                      RTE_PTYPE_L4_FRAG;
168         (*p)[0x22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
169                      RTE_PTYPE_L4_FRAG;
170         /* TCP */
171         (*p)[0x05] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
172                      RTE_PTYPE_L4_TCP;
173         (*p)[0x06] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
174                      RTE_PTYPE_L4_TCP;
175         (*p)[0x0d] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
176                      RTE_PTYPE_L4_TCP;
177         (*p)[0x0e] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
178                      RTE_PTYPE_L4_TCP;
179         (*p)[0x11] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
180                      RTE_PTYPE_L4_TCP;
181         (*p)[0x12] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
182                      RTE_PTYPE_L4_TCP;
183         /* UDP */
184         (*p)[0x09] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
185                      RTE_PTYPE_L4_UDP;
186         (*p)[0x0a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
187                      RTE_PTYPE_L4_UDP;
188         /* Repeat with outer_l3_type being set. Just in case. */
189         (*p)[0x81] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
190                      RTE_PTYPE_L4_NONFRAG;
191         (*p)[0x82] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
192                      RTE_PTYPE_L4_NONFRAG;
193         (*p)[0xa1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
194                      RTE_PTYPE_L4_FRAG;
195         (*p)[0xa2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
196                      RTE_PTYPE_L4_FRAG;
197         (*p)[0x85] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
198                      RTE_PTYPE_L4_TCP;
199         (*p)[0x86] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
200                      RTE_PTYPE_L4_TCP;
201         (*p)[0x8d] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
202                      RTE_PTYPE_L4_TCP;
203         (*p)[0x8e] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
204                      RTE_PTYPE_L4_TCP;
205         (*p)[0x91] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
206                      RTE_PTYPE_L4_TCP;
207         (*p)[0x92] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
208                      RTE_PTYPE_L4_TCP;
209         (*p)[0x89] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
210                      RTE_PTYPE_L4_UDP;
211         (*p)[0x8a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
212                      RTE_PTYPE_L4_UDP;
213         /* Tunneled - L3 */
214         (*p)[0x40] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
215         (*p)[0x41] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
216                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
217                      RTE_PTYPE_INNER_L4_NONFRAG;
218         (*p)[0x42] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
219                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
220                      RTE_PTYPE_INNER_L4_NONFRAG;
221         (*p)[0xc0] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
222         (*p)[0xc1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
223                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
224                      RTE_PTYPE_INNER_L4_NONFRAG;
225         (*p)[0xc2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
226                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
227                      RTE_PTYPE_INNER_L4_NONFRAG;
228         /* Tunneled - Fragmented */
229         (*p)[0x61] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
230                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
231                      RTE_PTYPE_INNER_L4_FRAG;
232         (*p)[0x62] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
233                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
234                      RTE_PTYPE_INNER_L4_FRAG;
235         (*p)[0xe1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
236                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
237                      RTE_PTYPE_INNER_L4_FRAG;
238         (*p)[0xe2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
239                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
240                      RTE_PTYPE_INNER_L4_FRAG;
241         /* Tunneled - TCP */
242         (*p)[0x45] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
243                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
244                      RTE_PTYPE_INNER_L4_TCP;
245         (*p)[0x46] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
246                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
247                      RTE_PTYPE_INNER_L4_TCP;
248         (*p)[0x4d] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
249                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
250                      RTE_PTYPE_INNER_L4_TCP;
251         (*p)[0x4e] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
252                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
253                      RTE_PTYPE_INNER_L4_TCP;
254         (*p)[0x51] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
255                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
256                      RTE_PTYPE_INNER_L4_TCP;
257         (*p)[0x52] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
258                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
259                      RTE_PTYPE_INNER_L4_TCP;
260         (*p)[0xc5] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
261                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
262                      RTE_PTYPE_INNER_L4_TCP;
263         (*p)[0xc6] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
264                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
265                      RTE_PTYPE_INNER_L4_TCP;
266         (*p)[0xcd] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
267                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
268                      RTE_PTYPE_INNER_L4_TCP;
269         (*p)[0xce] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
270                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
271                      RTE_PTYPE_INNER_L4_TCP;
272         (*p)[0xd1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
273                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
274                      RTE_PTYPE_INNER_L4_TCP;
275         (*p)[0xd2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
276                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
277                      RTE_PTYPE_INNER_L4_TCP;
278         /* Tunneled - UDP */
279         (*p)[0x49] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
280                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
281                      RTE_PTYPE_INNER_L4_UDP;
282         (*p)[0x4a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
283                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
284                      RTE_PTYPE_INNER_L4_UDP;
285         (*p)[0xc9] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
286                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
287                      RTE_PTYPE_INNER_L4_UDP;
288         (*p)[0xca] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
289                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
290                      RTE_PTYPE_INNER_L4_UDP;
291 }
292
293 /**
294  * Build a table to translate packet to checksum type of Verbs.
295  */
296 void
297 mlx5_set_cksum_table(void)
298 {
299         unsigned int i;
300         uint8_t v;
301
302         /*
303          * The index should have:
304          * bit[0] = PKT_TX_TCP_SEG
305          * bit[2:3] = PKT_TX_UDP_CKSUM, PKT_TX_TCP_CKSUM
306          * bit[4] = PKT_TX_IP_CKSUM
307          * bit[8] = PKT_TX_OUTER_IP_CKSUM
308          * bit[9] = tunnel
309          */
310         for (i = 0; i < RTE_DIM(mlx5_cksum_table); ++i) {
311                 v = 0;
312                 if (i & (1 << 9)) {
313                         /* Tunneled packet. */
314                         if (i & (1 << 8)) /* Outer IP. */
315                                 v |= MLX5_ETH_WQE_L3_CSUM;
316                         if (i & (1 << 4)) /* Inner IP. */
317                                 v |= MLX5_ETH_WQE_L3_INNER_CSUM;
318                         if (i & (3 << 2 | 1 << 0)) /* L4 or TSO. */
319                                 v |= MLX5_ETH_WQE_L4_INNER_CSUM;
320                 } else {
321                         /* No tunnel. */
322                         if (i & (1 << 4)) /* IP. */
323                                 v |= MLX5_ETH_WQE_L3_CSUM;
324                         if (i & (3 << 2 | 1 << 0)) /* L4 or TSO. */
325                                 v |= MLX5_ETH_WQE_L4_CSUM;
326                 }
327                 mlx5_cksum_table[i] = v;
328         }
329 }
330
331 /**
332  * Build a table to translate packet type of mbuf to SWP type of Verbs.
333  */
334 void
335 mlx5_set_swp_types_table(void)
336 {
337         unsigned int i;
338         uint8_t v;
339
340         /*
341          * The index should have:
342          * bit[0:1] = PKT_TX_L4_MASK
343          * bit[4] = PKT_TX_IPV6
344          * bit[8] = PKT_TX_OUTER_IPV6
345          * bit[9] = PKT_TX_OUTER_UDP
346          */
347         for (i = 0; i < RTE_DIM(mlx5_swp_types_table); ++i) {
348                 v = 0;
349                 if (i & (1 << 8))
350                         v |= MLX5_ETH_WQE_L3_OUTER_IPV6;
351                 if (i & (1 << 9))
352                         v |= MLX5_ETH_WQE_L4_OUTER_UDP;
353                 if (i & (1 << 4))
354                         v |= MLX5_ETH_WQE_L3_INNER_IPV6;
355                 if ((i & 3) == (PKT_TX_UDP_CKSUM >> 52))
356                         v |= MLX5_ETH_WQE_L4_INNER_UDP;
357                 mlx5_swp_types_table[i] = v;
358         }
359 }
360
361 /**
362  * Set Software Parser flags and offsets in Ethernet Segment of WQE.
363  * Flags must be preliminary initialized to zero.
364  *
365  * @param loc
366  *   Pointer to burst routine local context.
367  * @param swp_flags
368  *   Pointer to store Software Parser flags
369  * @param olx
370  *   Configured Tx offloads mask. It is fully defined at
371  *   compile time and may be used for optimization.
372  *
373  * @return
374  *   Software Parser offsets packed in dword.
375  *   Software Parser flags are set by pointer.
376  */
377 static __rte_always_inline uint32_t
378 txq_mbuf_to_swp(struct mlx5_txq_local *__rte_restrict loc,
379                 uint8_t *swp_flags,
380                 unsigned int olx)
381 {
382         uint64_t ol, tunnel;
383         unsigned int idx, off;
384         uint32_t set;
385
386         if (!MLX5_TXOFF_CONFIG(SWP))
387                 return 0;
388         ol = loc->mbuf->ol_flags;
389         tunnel = ol & PKT_TX_TUNNEL_MASK;
390         /*
391          * Check whether Software Parser is required.
392          * Only customized tunnels may ask for.
393          */
394         if (likely(tunnel != PKT_TX_TUNNEL_UDP && tunnel != PKT_TX_TUNNEL_IP))
395                 return 0;
396         /*
397          * The index should have:
398          * bit[0:1] = PKT_TX_L4_MASK
399          * bit[4] = PKT_TX_IPV6
400          * bit[8] = PKT_TX_OUTER_IPV6
401          * bit[9] = PKT_TX_OUTER_UDP
402          */
403         idx = (ol & (PKT_TX_L4_MASK | PKT_TX_IPV6 | PKT_TX_OUTER_IPV6)) >> 52;
404         idx |= (tunnel == PKT_TX_TUNNEL_UDP) ? (1 << 9) : 0;
405         *swp_flags = mlx5_swp_types_table[idx];
406         /*
407          * Set offsets for SW parser. Since ConnectX-5, SW parser just
408          * complements HW parser. SW parser starts to engage only if HW parser
409          * can't reach a header. For the older devices, HW parser will not kick
410          * in if any of SWP offsets is set. Therefore, all of the L3 offsets
411          * should be set regardless of HW offload.
412          */
413         off = loc->mbuf->outer_l2_len;
414         if (MLX5_TXOFF_CONFIG(VLAN) && ol & PKT_TX_VLAN_PKT)
415                 off += sizeof(struct rte_vlan_hdr);
416         set = (off >> 1) << 8; /* Outer L3 offset. */
417         off += loc->mbuf->outer_l3_len;
418         if (tunnel == PKT_TX_TUNNEL_UDP)
419                 set |= off >> 1; /* Outer L4 offset. */
420         if (ol & (PKT_TX_IPV4 | PKT_TX_IPV6)) { /* Inner IP. */
421                 const uint64_t csum = ol & PKT_TX_L4_MASK;
422                         off += loc->mbuf->l2_len;
423                 set |= (off >> 1) << 24; /* Inner L3 offset. */
424                 if (csum == PKT_TX_TCP_CKSUM ||
425                     csum == PKT_TX_UDP_CKSUM ||
426                     (MLX5_TXOFF_CONFIG(TSO) && ol & PKT_TX_TCP_SEG)) {
427                         off += loc->mbuf->l3_len;
428                         set |= (off >> 1) << 16; /* Inner L4 offset. */
429                 }
430         }
431         set = rte_cpu_to_le_32(set);
432         return set;
433 }
434
435 /**
436  * Convert the Checksum offloads to Verbs.
437  *
438  * @param buf
439  *   Pointer to the mbuf.
440  *
441  * @return
442  *   Converted checksum flags.
443  */
444 static __rte_always_inline uint8_t
445 txq_ol_cksum_to_cs(struct rte_mbuf *buf)
446 {
447         uint32_t idx;
448         uint8_t is_tunnel = !!(buf->ol_flags & PKT_TX_TUNNEL_MASK);
449         const uint64_t ol_flags_mask = PKT_TX_TCP_SEG | PKT_TX_L4_MASK |
450                                        PKT_TX_IP_CKSUM | PKT_TX_OUTER_IP_CKSUM;
451
452         /*
453          * The index should have:
454          * bit[0] = PKT_TX_TCP_SEG
455          * bit[2:3] = PKT_TX_UDP_CKSUM, PKT_TX_TCP_CKSUM
456          * bit[4] = PKT_TX_IP_CKSUM
457          * bit[8] = PKT_TX_OUTER_IP_CKSUM
458          * bit[9] = tunnel
459          */
460         idx = ((buf->ol_flags & ol_flags_mask) >> 50) | (!!is_tunnel << 9);
461         return mlx5_cksum_table[idx];
462 }
463
464 /**
465  * Internal function to compute the number of used descriptors in an RX queue
466  *
467  * @param rxq
468  *   The Rx queue.
469  *
470  * @return
471  *   The number of used rx descriptor.
472  */
473 static uint32_t
474 rx_queue_count(struct mlx5_rxq_data *rxq)
475 {
476         struct rxq_zip *zip = &rxq->zip;
477         volatile struct mlx5_cqe *cqe;
478         const unsigned int cqe_n = (1 << rxq->cqe_n);
479         const unsigned int cqe_cnt = cqe_n - 1;
480         unsigned int cq_ci;
481         unsigned int used;
482
483         /* if we are processing a compressed cqe */
484         if (zip->ai) {
485                 used = zip->cqe_cnt - zip->ca;
486                 cq_ci = zip->cq_ci;
487         } else {
488                 used = 0;
489                 cq_ci = rxq->cq_ci;
490         }
491         cqe = &(*rxq->cqes)[cq_ci & cqe_cnt];
492         while (check_cqe(cqe, cqe_n, cq_ci) != MLX5_CQE_STATUS_HW_OWN) {
493                 int8_t op_own;
494                 unsigned int n;
495
496                 op_own = cqe->op_own;
497                 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED)
498                         n = rte_be_to_cpu_32(cqe->byte_cnt);
499                 else
500                         n = 1;
501                 cq_ci += n;
502                 used += n;
503                 cqe = &(*rxq->cqes)[cq_ci & cqe_cnt];
504         }
505         used = RTE_MIN(used, (1U << rxq->elts_n) - 1);
506         return used;
507 }
508
509 /**
510  * DPDK callback to check the status of a rx descriptor.
511  *
512  * @param rx_queue
513  *   The Rx queue.
514  * @param[in] offset
515  *   The index of the descriptor in the ring.
516  *
517  * @return
518  *   The status of the tx descriptor.
519  */
520 int
521 mlx5_rx_descriptor_status(void *rx_queue, uint16_t offset)
522 {
523         struct mlx5_rxq_data *rxq = rx_queue;
524         struct mlx5_rxq_ctrl *rxq_ctrl =
525                         container_of(rxq, struct mlx5_rxq_ctrl, rxq);
526         struct rte_eth_dev *dev = ETH_DEV(rxq_ctrl->priv);
527
528         if (dev->rx_pkt_burst != mlx5_rx_burst) {
529                 rte_errno = ENOTSUP;
530                 return -rte_errno;
531         }
532         if (offset >= (1 << rxq->elts_n)) {
533                 rte_errno = EINVAL;
534                 return -rte_errno;
535         }
536         if (offset < rx_queue_count(rxq))
537                 return RTE_ETH_RX_DESC_DONE;
538         return RTE_ETH_RX_DESC_AVAIL;
539 }
540
541 /**
542  * DPDK callback to get the RX queue information
543  *
544  * @param dev
545  *   Pointer to the device structure.
546  *
547  * @param rx_queue_id
548  *   Rx queue identificator.
549  *
550  * @param qinfo
551  *   Pointer to the RX queue information structure.
552  *
553  * @return
554  *   None.
555  */
556
557 void
558 mlx5_rxq_info_get(struct rte_eth_dev *dev, uint16_t rx_queue_id,
559                   struct rte_eth_rxq_info *qinfo)
560 {
561         struct mlx5_priv *priv = dev->data->dev_private;
562         struct mlx5_rxq_data *rxq = (*priv->rxqs)[rx_queue_id];
563         struct mlx5_rxq_ctrl *rxq_ctrl =
564                 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
565
566         if (!rxq)
567                 return;
568         qinfo->mp = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
569                                         rxq->mprq_mp : rxq->mp;
570         qinfo->conf.rx_thresh.pthresh = 0;
571         qinfo->conf.rx_thresh.hthresh = 0;
572         qinfo->conf.rx_thresh.wthresh = 0;
573         qinfo->conf.rx_free_thresh = rxq->rq_repl_thresh;
574         qinfo->conf.rx_drop_en = 1;
575         qinfo->conf.rx_deferred_start = rxq_ctrl ? 0 : 1;
576         qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
577         qinfo->scattered_rx = dev->data->scattered_rx;
578         qinfo->nb_desc = 1 << rxq->elts_n;
579 }
580
581 /**
582  * DPDK callback to get the RX packet burst mode information
583  *
584  * @param dev
585  *   Pointer to the device structure.
586  *
587  * @param rx_queue_id
588  *   Rx queue identificatior.
589  *
590  * @param mode
591  *   Pointer to the burts mode information.
592  *
593  * @return
594  *   0 as success, -EINVAL as failure.
595  */
596
597 int
598 mlx5_rx_burst_mode_get(struct rte_eth_dev *dev,
599                        uint16_t rx_queue_id __rte_unused,
600                        struct rte_eth_burst_mode *mode)
601 {
602         eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
603
604         if (pkt_burst == mlx5_rx_burst) {
605                 snprintf(mode->info, sizeof(mode->info), "%s", "Scalar");
606         } else if (pkt_burst == mlx5_rx_burst_mprq) {
607                 snprintf(mode->info, sizeof(mode->info), "%s", "Multi-Packet RQ");
608         } else if (pkt_burst == mlx5_rx_burst_vec) {
609 #if defined RTE_ARCH_X86_64
610                 snprintf(mode->info, sizeof(mode->info), "%s", "Vector SSE");
611 #elif defined RTE_ARCH_ARM64
612                 snprintf(mode->info, sizeof(mode->info), "%s", "Vector Neon");
613 #elif defined RTE_ARCH_PPC_64
614                 snprintf(mode->info, sizeof(mode->info), "%s", "Vector AltiVec");
615 #else
616                 return -EINVAL;
617 #endif
618         } else {
619                 return -EINVAL;
620         }
621         return 0;
622 }
623
624 /**
625  * DPDK callback to get the number of used descriptors in a RX queue
626  *
627  * @param dev
628  *   Pointer to the device structure.
629  *
630  * @param rx_queue_id
631  *   The Rx queue.
632  *
633  * @return
634  *   The number of used rx descriptor.
635  *   -EINVAL if the queue is invalid
636  */
637 uint32_t
638 mlx5_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
639 {
640         struct mlx5_priv *priv = dev->data->dev_private;
641         struct mlx5_rxq_data *rxq;
642
643         if (dev->rx_pkt_burst != mlx5_rx_burst) {
644                 rte_errno = ENOTSUP;
645                 return -rte_errno;
646         }
647         rxq = (*priv->rxqs)[rx_queue_id];
648         if (!rxq) {
649                 rte_errno = EINVAL;
650                 return -rte_errno;
651         }
652         return rx_queue_count(rxq);
653 }
654
655 #define MLX5_SYSTEM_LOG_DIR "/var/log"
656 /**
657  * Dump debug information to log file.
658  *
659  * @param fname
660  *   The file name.
661  * @param hex_title
662  *   If not NULL this string is printed as a header to the output
663  *   and the output will be in hexadecimal view.
664  * @param buf
665  *   This is the buffer address to print out.
666  * @param len
667  *   The number of bytes to dump out.
668  */
669 void
670 mlx5_dump_debug_information(const char *fname, const char *hex_title,
671                             const void *buf, unsigned int hex_len)
672 {
673         FILE *fd;
674
675         MKSTR(path, "%s/%s", MLX5_SYSTEM_LOG_DIR, fname);
676         fd = fopen(path, "a+");
677         if (!fd) {
678                 DRV_LOG(WARNING, "cannot open %s for debug dump", path);
679                 MKSTR(path2, "./%s", fname);
680                 fd = fopen(path2, "a+");
681                 if (!fd) {
682                         DRV_LOG(ERR, "cannot open %s for debug dump", path2);
683                         return;
684                 }
685                 DRV_LOG(INFO, "New debug dump in file %s", path2);
686         } else {
687                 DRV_LOG(INFO, "New debug dump in file %s", path);
688         }
689         if (hex_title)
690                 rte_hexdump(fd, hex_title, buf, hex_len);
691         else
692                 fprintf(fd, "%s", (const char *)buf);
693         fprintf(fd, "\n\n\n");
694         fclose(fd);
695 }
696
697 /**
698  * Move QP from error state to running state and initialize indexes.
699  *
700  * @param txq_ctrl
701  *   Pointer to TX queue control structure.
702  *
703  * @return
704  *   0 on success, else -1.
705  */
706 static int
707 tx_recover_qp(struct mlx5_txq_ctrl *txq_ctrl)
708 {
709         struct mlx5_mp_arg_queue_state_modify sm = {
710                         .is_wq = 0,
711                         .queue_id = txq_ctrl->txq.idx,
712         };
713
714         if (mlx5_queue_state_modify(ETH_DEV(txq_ctrl->priv), &sm))
715                 return -1;
716         txq_ctrl->txq.wqe_ci = 0;
717         txq_ctrl->txq.wqe_pi = 0;
718         txq_ctrl->txq.elts_comp = 0;
719         return 0;
720 }
721
722 /* Return 1 if the error CQE is signed otherwise, sign it and return 0. */
723 static int
724 check_err_cqe_seen(volatile struct mlx5_err_cqe *err_cqe)
725 {
726         static const uint8_t magic[] = "seen";
727         int ret = 1;
728         unsigned int i;
729
730         for (i = 0; i < sizeof(magic); ++i)
731                 if (!ret || err_cqe->rsvd1[i] != magic[i]) {
732                         ret = 0;
733                         err_cqe->rsvd1[i] = magic[i];
734                 }
735         return ret;
736 }
737
738 /**
739  * Handle error CQE.
740  *
741  * @param txq
742  *   Pointer to TX queue structure.
743  * @param error_cqe
744  *   Pointer to the error CQE.
745  *
746  * @return
747  *   Negative value if queue recovery failed, otherwise
748  *   the error completion entry is handled successfully.
749  */
750 static int
751 mlx5_tx_error_cqe_handle(struct mlx5_txq_data *__rte_restrict txq,
752                          volatile struct mlx5_err_cqe *err_cqe)
753 {
754         if (err_cqe->syndrome != MLX5_CQE_SYNDROME_WR_FLUSH_ERR) {
755                 const uint16_t wqe_m = ((1 << txq->wqe_n) - 1);
756                 struct mlx5_txq_ctrl *txq_ctrl =
757                                 container_of(txq, struct mlx5_txq_ctrl, txq);
758                 uint16_t new_wqe_pi = rte_be_to_cpu_16(err_cqe->wqe_counter);
759                 int seen = check_err_cqe_seen(err_cqe);
760
761                 if (!seen && txq_ctrl->dump_file_n <
762                     txq_ctrl->priv->config.max_dump_files_num) {
763                         MKSTR(err_str, "Unexpected CQE error syndrome "
764                               "0x%02x CQN = %u SQN = %u wqe_counter = %u "
765                               "wq_ci = %u cq_ci = %u", err_cqe->syndrome,
766                               txq->cqe_s, txq->qp_num_8s >> 8,
767                               rte_be_to_cpu_16(err_cqe->wqe_counter),
768                               txq->wqe_ci, txq->cq_ci);
769                         MKSTR(name, "dpdk_mlx5_port_%u_txq_%u_index_%u_%u",
770                               PORT_ID(txq_ctrl->priv), txq->idx,
771                               txq_ctrl->dump_file_n, (uint32_t)rte_rdtsc());
772                         mlx5_dump_debug_information(name, NULL, err_str, 0);
773                         mlx5_dump_debug_information(name, "MLX5 Error CQ:",
774                                                     (const void *)((uintptr_t)
775                                                     txq->cqes),
776                                                     sizeof(*err_cqe) *
777                                                     (1 << txq->cqe_n));
778                         mlx5_dump_debug_information(name, "MLX5 Error SQ:",
779                                                     (const void *)((uintptr_t)
780                                                     txq->wqes),
781                                                     MLX5_WQE_SIZE *
782                                                     (1 << txq->wqe_n));
783                         txq_ctrl->dump_file_n++;
784                 }
785                 if (!seen)
786                         /*
787                          * Count errors in WQEs units.
788                          * Later it can be improved to count error packets,
789                          * for example, by SQ parsing to find how much packets
790                          * should be counted for each WQE.
791                          */
792                         txq->stats.oerrors += ((txq->wqe_ci & wqe_m) -
793                                                 new_wqe_pi) & wqe_m;
794                 if (tx_recover_qp(txq_ctrl)) {
795                         /* Recovering failed - retry later on the same WQE. */
796                         return -1;
797                 }
798                 /* Release all the remaining buffers. */
799                 txq_free_elts(txq_ctrl);
800         }
801         return 0;
802 }
803
804 /**
805  * Translate RX completion flags to packet type.
806  *
807  * @param[in] rxq
808  *   Pointer to RX queue structure.
809  * @param[in] cqe
810  *   Pointer to CQE.
811  *
812  * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
813  *
814  * @return
815  *   Packet type for struct rte_mbuf.
816  */
817 static inline uint32_t
818 rxq_cq_to_pkt_type(struct mlx5_rxq_data *rxq, volatile struct mlx5_cqe *cqe)
819 {
820         uint8_t idx;
821         uint8_t pinfo = cqe->pkt_info;
822         uint16_t ptype = cqe->hdr_type_etc;
823
824         /*
825          * The index to the array should have:
826          * bit[1:0] = l3_hdr_type
827          * bit[4:2] = l4_hdr_type
828          * bit[5] = ip_frag
829          * bit[6] = tunneled
830          * bit[7] = outer_l3_type
831          */
832         idx = ((pinfo & 0x3) << 6) | ((ptype & 0xfc00) >> 10);
833         return mlx5_ptype_table[idx] | rxq->tunnel * !!(idx & (1 << 6));
834 }
835
836 /**
837  * Initialize Rx WQ and indexes.
838  *
839  * @param[in] rxq
840  *   Pointer to RX queue structure.
841  */
842 void
843 mlx5_rxq_initialize(struct mlx5_rxq_data *rxq)
844 {
845         const unsigned int wqe_n = 1 << rxq->elts_n;
846         unsigned int i;
847
848         for (i = 0; (i != wqe_n); ++i) {
849                 volatile struct mlx5_wqe_data_seg *scat;
850                 uintptr_t addr;
851                 uint32_t byte_count;
852
853                 if (mlx5_rxq_mprq_enabled(rxq)) {
854                         struct mlx5_mprq_buf *buf = (*rxq->mprq_bufs)[i];
855
856                         scat = &((volatile struct mlx5_wqe_mprq *)
857                                 rxq->wqes)[i].dseg;
858                         addr = (uintptr_t)mlx5_mprq_buf_addr(buf,
859                                                          1 << rxq->strd_num_n);
860                         byte_count = (1 << rxq->strd_sz_n) *
861                                         (1 << rxq->strd_num_n);
862                 } else {
863                         struct rte_mbuf *buf = (*rxq->elts)[i];
864
865                         scat = &((volatile struct mlx5_wqe_data_seg *)
866                                         rxq->wqes)[i];
867                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
868                         byte_count = DATA_LEN(buf);
869                 }
870                 /* scat->addr must be able to store a pointer. */
871                 MLX5_ASSERT(sizeof(scat->addr) >= sizeof(uintptr_t));
872                 *scat = (struct mlx5_wqe_data_seg){
873                         .addr = rte_cpu_to_be_64(addr),
874                         .byte_count = rte_cpu_to_be_32(byte_count),
875                         .lkey = mlx5_rx_addr2mr(rxq, addr),
876                 };
877         }
878         rxq->consumed_strd = 0;
879         rxq->decompressed = 0;
880         rxq->rq_pi = 0;
881         rxq->zip = (struct rxq_zip){
882                 .ai = 0,
883         };
884         /* Update doorbell counter. */
885         rxq->rq_ci = wqe_n >> rxq->sges_n;
886         rte_cio_wmb();
887         *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
888 }
889
890 /**
891  * Modify a Verbs/DevX queue state.
892  * This must be called from the primary process.
893  *
894  * @param dev
895  *   Pointer to Ethernet device.
896  * @param sm
897  *   State modify request parameters.
898  *
899  * @return
900  *   0 in case of success else non-zero value and rte_errno is set.
901  */
902 int
903 mlx5_queue_state_modify_primary(struct rte_eth_dev *dev,
904                         const struct mlx5_mp_arg_queue_state_modify *sm)
905 {
906         int ret;
907         struct mlx5_priv *priv = dev->data->dev_private;
908
909         if (sm->is_wq) {
910                 struct mlx5_rxq_data *rxq = (*priv->rxqs)[sm->queue_id];
911                 struct mlx5_rxq_ctrl *rxq_ctrl =
912                         container_of(rxq, struct mlx5_rxq_ctrl, rxq);
913
914                 if (rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_IBV) {
915                         struct ibv_wq_attr mod = {
916                                 .attr_mask = IBV_WQ_ATTR_STATE,
917                                 .wq_state = sm->state,
918                         };
919
920                         ret = mlx5_glue->modify_wq(rxq_ctrl->obj->wq, &mod);
921                 } else { /* rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ. */
922                         struct mlx5_devx_modify_rq_attr rq_attr;
923
924                         memset(&rq_attr, 0, sizeof(rq_attr));
925                         if (sm->state == IBV_WQS_RESET) {
926                                 rq_attr.rq_state = MLX5_RQC_STATE_ERR;
927                                 rq_attr.state = MLX5_RQC_STATE_RST;
928                         } else if (sm->state == IBV_WQS_RDY) {
929                                 rq_attr.rq_state = MLX5_RQC_STATE_RST;
930                                 rq_attr.state = MLX5_RQC_STATE_RDY;
931                         } else if (sm->state == IBV_WQS_ERR) {
932                                 rq_attr.rq_state = MLX5_RQC_STATE_RDY;
933                                 rq_attr.state = MLX5_RQC_STATE_ERR;
934                         }
935                         ret = mlx5_devx_cmd_modify_rq(rxq_ctrl->obj->rq,
936                                                       &rq_attr);
937                 }
938                 if (ret) {
939                         DRV_LOG(ERR, "Cannot change Rx WQ state to %u  - %s",
940                                         sm->state, strerror(errno));
941                         rte_errno = errno;
942                         return ret;
943                 }
944         } else {
945                 struct mlx5_txq_data *txq = (*priv->txqs)[sm->queue_id];
946                 struct mlx5_txq_ctrl *txq_ctrl =
947                         container_of(txq, struct mlx5_txq_ctrl, txq);
948
949                 if (txq_ctrl->obj->type == MLX5_TXQ_OBJ_TYPE_DEVX_SQ) {
950                         struct mlx5_devx_modify_sq_attr msq_attr = { 0 };
951
952                         /* Change queue state to reset. */
953                         msq_attr.sq_state = MLX5_SQC_STATE_ERR;
954                         msq_attr.state = MLX5_SQC_STATE_RST;
955                         ret = mlx5_devx_cmd_modify_sq(txq_ctrl->obj->sq_devx,
956                                                       &msq_attr);
957                         if (ret) {
958                                 DRV_LOG(ERR, "Cannot change the "
959                                         "Tx QP state to RESET %s",
960                                         strerror(errno));
961                                 rte_errno = errno;
962                                 return ret;
963                         }
964                         /* Change queue state to ready. */
965                         msq_attr.sq_state = MLX5_SQC_STATE_RST;
966                         msq_attr.state = MLX5_SQC_STATE_RDY;
967                         ret = mlx5_devx_cmd_modify_sq(txq_ctrl->obj->sq_devx,
968                                                       &msq_attr);
969                         if (ret) {
970                                 DRV_LOG(ERR, "Cannot change the "
971                                         "Tx QP state to READY %s",
972                                         strerror(errno));
973                                 rte_errno = errno;
974                                 return ret;
975                         }
976                 } else {
977                         struct ibv_qp_attr mod = {
978                                 .qp_state = IBV_QPS_RESET,
979                                 .port_num = (uint8_t)priv->dev_port,
980                         };
981                         struct ibv_qp *qp = txq_ctrl->obj->qp;
982
983                         MLX5_ASSERT
984                                 (txq_ctrl->obj->type == MLX5_TXQ_OBJ_TYPE_IBV);
985
986                         ret = mlx5_glue->modify_qp(qp, &mod, IBV_QP_STATE);
987                         if (ret) {
988                                 DRV_LOG(ERR, "Cannot change the "
989                                         "Tx QP state to RESET %s",
990                                         strerror(errno));
991                                 rte_errno = errno;
992                                 return ret;
993                         }
994                         mod.qp_state = IBV_QPS_INIT;
995                         ret = mlx5_glue->modify_qp(qp, &mod, IBV_QP_STATE);
996                         if (ret) {
997                                 DRV_LOG(ERR, "Cannot change the "
998                                         "Tx QP state to INIT %s",
999                                         strerror(errno));
1000                                 rte_errno = errno;
1001                                 return ret;
1002                         }
1003                         mod.qp_state = IBV_QPS_RTR;
1004                         ret = mlx5_glue->modify_qp(qp, &mod, IBV_QP_STATE);
1005                         if (ret) {
1006                                 DRV_LOG(ERR, "Cannot change the "
1007                                         "Tx QP state to RTR %s",
1008                                         strerror(errno));
1009                                 rte_errno = errno;
1010                                 return ret;
1011                         }
1012                         mod.qp_state = IBV_QPS_RTS;
1013                         ret = mlx5_glue->modify_qp(qp, &mod, IBV_QP_STATE);
1014                         if (ret) {
1015                                 DRV_LOG(ERR, "Cannot change the "
1016                                         "Tx QP state to RTS %s",
1017                                         strerror(errno));
1018                                 rte_errno = errno;
1019                                 return ret;
1020                         }
1021                 }
1022         }
1023         return 0;
1024 }
1025
1026 /**
1027  * Modify a Verbs queue state.
1028  *
1029  * @param dev
1030  *   Pointer to Ethernet device.
1031  * @param sm
1032  *   State modify request parameters.
1033  *
1034  * @return
1035  *   0 in case of success else non-zero value.
1036  */
1037 static int
1038 mlx5_queue_state_modify(struct rte_eth_dev *dev,
1039                         struct mlx5_mp_arg_queue_state_modify *sm)
1040 {
1041         struct mlx5_priv *priv = dev->data->dev_private;
1042         int ret = 0;
1043
1044         switch (rte_eal_process_type()) {
1045         case RTE_PROC_PRIMARY:
1046                 ret = mlx5_queue_state_modify_primary(dev, sm);
1047                 break;
1048         case RTE_PROC_SECONDARY:
1049                 ret = mlx5_mp_req_queue_state_modify(&priv->mp_id, sm);
1050                 break;
1051         default:
1052                 break;
1053         }
1054         return ret;
1055 }
1056
1057 /**
1058  * Handle a Rx error.
1059  * The function inserts the RQ state to reset when the first error CQE is
1060  * shown, then drains the CQ by the caller function loop. When the CQ is empty,
1061  * it moves the RQ state to ready and initializes the RQ.
1062  * Next CQE identification and error counting are in the caller responsibility.
1063  *
1064  * @param[in] rxq
1065  *   Pointer to RX queue structure.
1066  * @param[in] vec
1067  *   1 when called from vectorized Rx burst, need to prepare mbufs for the RQ.
1068  *   0 when called from non-vectorized Rx burst.
1069  *
1070  * @return
1071  *   -1 in case of recovery error, otherwise the CQE status.
1072  */
1073 int
1074 mlx5_rx_err_handle(struct mlx5_rxq_data *rxq, uint8_t vec)
1075 {
1076         const uint16_t cqe_n = 1 << rxq->cqe_n;
1077         const uint16_t cqe_mask = cqe_n - 1;
1078         const unsigned int wqe_n = 1 << rxq->elts_n;
1079         struct mlx5_rxq_ctrl *rxq_ctrl =
1080                         container_of(rxq, struct mlx5_rxq_ctrl, rxq);
1081         union {
1082                 volatile struct mlx5_cqe *cqe;
1083                 volatile struct mlx5_err_cqe *err_cqe;
1084         } u = {
1085                 .cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_mask],
1086         };
1087         struct mlx5_mp_arg_queue_state_modify sm;
1088         int ret;
1089
1090         switch (rxq->err_state) {
1091         case MLX5_RXQ_ERR_STATE_NO_ERROR:
1092                 rxq->err_state = MLX5_RXQ_ERR_STATE_NEED_RESET;
1093                 /* Fall-through */
1094         case MLX5_RXQ_ERR_STATE_NEED_RESET:
1095                 sm.is_wq = 1;
1096                 sm.queue_id = rxq->idx;
1097                 sm.state = IBV_WQS_RESET;
1098                 if (mlx5_queue_state_modify(ETH_DEV(rxq_ctrl->priv), &sm))
1099                         return -1;
1100                 if (rxq_ctrl->dump_file_n <
1101                     rxq_ctrl->priv->config.max_dump_files_num) {
1102                         MKSTR(err_str, "Unexpected CQE error syndrome "
1103                               "0x%02x CQN = %u RQN = %u wqe_counter = %u"
1104                               " rq_ci = %u cq_ci = %u", u.err_cqe->syndrome,
1105                               rxq->cqn, rxq_ctrl->wqn,
1106                               rte_be_to_cpu_16(u.err_cqe->wqe_counter),
1107                               rxq->rq_ci << rxq->sges_n, rxq->cq_ci);
1108                         MKSTR(name, "dpdk_mlx5_port_%u_rxq_%u_%u",
1109                               rxq->port_id, rxq->idx, (uint32_t)rte_rdtsc());
1110                         mlx5_dump_debug_information(name, NULL, err_str, 0);
1111                         mlx5_dump_debug_information(name, "MLX5 Error CQ:",
1112                                                     (const void *)((uintptr_t)
1113                                                                     rxq->cqes),
1114                                                     sizeof(*u.cqe) * cqe_n);
1115                         mlx5_dump_debug_information(name, "MLX5 Error RQ:",
1116                                                     (const void *)((uintptr_t)
1117                                                                     rxq->wqes),
1118                                                     16 * wqe_n);
1119                         rxq_ctrl->dump_file_n++;
1120                 }
1121                 rxq->err_state = MLX5_RXQ_ERR_STATE_NEED_READY;
1122                 /* Fall-through */
1123         case MLX5_RXQ_ERR_STATE_NEED_READY:
1124                 ret = check_cqe(u.cqe, cqe_n, rxq->cq_ci);
1125                 if (ret == MLX5_CQE_STATUS_HW_OWN) {
1126                         rte_cio_wmb();
1127                         *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
1128                         rte_cio_wmb();
1129                         /*
1130                          * The RQ consumer index must be zeroed while moving
1131                          * from RESET state to RDY state.
1132                          */
1133                         *rxq->rq_db = rte_cpu_to_be_32(0);
1134                         rte_cio_wmb();
1135                         sm.is_wq = 1;
1136                         sm.queue_id = rxq->idx;
1137                         sm.state = IBV_WQS_RDY;
1138                         if (mlx5_queue_state_modify(ETH_DEV(rxq_ctrl->priv),
1139                                                     &sm))
1140                                 return -1;
1141                         if (vec) {
1142                                 const uint16_t q_mask = wqe_n - 1;
1143                                 uint16_t elt_idx;
1144                                 struct rte_mbuf **elt;
1145                                 int i;
1146                                 unsigned int n = wqe_n - (rxq->rq_ci -
1147                                                           rxq->rq_pi);
1148
1149                                 for (i = 0; i < (int)n; ++i) {
1150                                         elt_idx = (rxq->rq_ci + i) & q_mask;
1151                                         elt = &(*rxq->elts)[elt_idx];
1152                                         *elt = rte_mbuf_raw_alloc(rxq->mp);
1153                                         if (!*elt) {
1154                                                 for (i--; i >= 0; --i) {
1155                                                         elt_idx = (rxq->rq_ci +
1156                                                                    i) & q_mask;
1157                                                         elt = &(*rxq->elts)
1158                                                                 [elt_idx];
1159                                                         rte_pktmbuf_free_seg
1160                                                                 (*elt);
1161                                                 }
1162                                                 return -1;
1163                                         }
1164                                 }
1165                                 for (i = 0; i < (int)wqe_n; ++i) {
1166                                         elt = &(*rxq->elts)[i];
1167                                         DATA_LEN(*elt) =
1168                                                 (uint16_t)((*elt)->buf_len -
1169                                                 rte_pktmbuf_headroom(*elt));
1170                                 }
1171                                 /* Padding with a fake mbuf for vec Rx. */
1172                                 for (i = 0; i < MLX5_VPMD_DESCS_PER_LOOP; ++i)
1173                                         (*rxq->elts)[wqe_n + i] =
1174                                                                 &rxq->fake_mbuf;
1175                         }
1176                         mlx5_rxq_initialize(rxq);
1177                         rxq->err_state = MLX5_RXQ_ERR_STATE_NO_ERROR;
1178                 }
1179                 return ret;
1180         default:
1181                 return -1;
1182         }
1183 }
1184
1185 /**
1186  * Get size of the next packet for a given CQE. For compressed CQEs, the
1187  * consumer index is updated only once all packets of the current one have
1188  * been processed.
1189  *
1190  * @param rxq
1191  *   Pointer to RX queue.
1192  * @param cqe
1193  *   CQE to process.
1194  * @param[out] mcqe
1195  *   Store pointer to mini-CQE if compressed. Otherwise, the pointer is not
1196  *   written.
1197  *
1198  * @return
1199  *   0 in case of empty CQE, otherwise the packet size in bytes.
1200  */
1201 static inline int
1202 mlx5_rx_poll_len(struct mlx5_rxq_data *rxq, volatile struct mlx5_cqe *cqe,
1203                  uint16_t cqe_cnt, volatile struct mlx5_mini_cqe8 **mcqe)
1204 {
1205         struct rxq_zip *zip = &rxq->zip;
1206         uint16_t cqe_n = cqe_cnt + 1;
1207         int len;
1208         uint16_t idx, end;
1209
1210         do {
1211                 len = 0;
1212                 /* Process compressed data in the CQE and mini arrays. */
1213                 if (zip->ai) {
1214                         volatile struct mlx5_mini_cqe8 (*mc)[8] =
1215                                 (volatile struct mlx5_mini_cqe8 (*)[8])
1216                                 (uintptr_t)(&(*rxq->cqes)[zip->ca &
1217                                                           cqe_cnt].pkt_info);
1218
1219                         len = rte_be_to_cpu_32((*mc)[zip->ai & 7].byte_cnt);
1220                         *mcqe = &(*mc)[zip->ai & 7];
1221                         if ((++zip->ai & 7) == 0) {
1222                                 /* Invalidate consumed CQEs */
1223                                 idx = zip->ca;
1224                                 end = zip->na;
1225                                 while (idx != end) {
1226                                         (*rxq->cqes)[idx & cqe_cnt].op_own =
1227                                                 MLX5_CQE_INVALIDATE;
1228                                         ++idx;
1229                                 }
1230                                 /*
1231                                  * Increment consumer index to skip the number
1232                                  * of CQEs consumed. Hardware leaves holes in
1233                                  * the CQ ring for software use.
1234                                  */
1235                                 zip->ca = zip->na;
1236                                 zip->na += 8;
1237                         }
1238                         if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1239                                 /* Invalidate the rest */
1240                                 idx = zip->ca;
1241                                 end = zip->cq_ci;
1242
1243                                 while (idx != end) {
1244                                         (*rxq->cqes)[idx & cqe_cnt].op_own =
1245                                                 MLX5_CQE_INVALIDATE;
1246                                         ++idx;
1247                                 }
1248                                 rxq->cq_ci = zip->cq_ci;
1249                                 zip->ai = 0;
1250                         }
1251                 /*
1252                  * No compressed data, get next CQE and verify if it is
1253                  * compressed.
1254                  */
1255                 } else {
1256                         int ret;
1257                         int8_t op_own;
1258
1259                         ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
1260                         if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
1261                                 if (unlikely(ret == MLX5_CQE_STATUS_ERR ||
1262                                              rxq->err_state)) {
1263                                         ret = mlx5_rx_err_handle(rxq, 0);
1264                                         if (ret == MLX5_CQE_STATUS_HW_OWN ||
1265                                             ret == -1)
1266                                                 return 0;
1267                                 } else {
1268                                         return 0;
1269                                 }
1270                         }
1271                         ++rxq->cq_ci;
1272                         op_own = cqe->op_own;
1273                         if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1274                                 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1275                                         (volatile struct mlx5_mini_cqe8 (*)[8])
1276                                         (uintptr_t)(&(*rxq->cqes)
1277                                                 [rxq->cq_ci &
1278                                                  cqe_cnt].pkt_info);
1279
1280                                 /* Fix endianness. */
1281                                 zip->cqe_cnt = rte_be_to_cpu_32(cqe->byte_cnt);
1282                                 /*
1283                                  * Current mini array position is the one
1284                                  * returned by check_cqe64().
1285                                  *
1286                                  * If completion comprises several mini arrays,
1287                                  * as a special case the second one is located
1288                                  * 7 CQEs after the initial CQE instead of 8
1289                                  * for subsequent ones.
1290                                  */
1291                                 zip->ca = rxq->cq_ci;
1292                                 zip->na = zip->ca + 7;
1293                                 /* Compute the next non compressed CQE. */
1294                                 --rxq->cq_ci;
1295                                 zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1296                                 /* Get packet size to return. */
1297                                 len = rte_be_to_cpu_32((*mc)[0].byte_cnt);
1298                                 *mcqe = &(*mc)[0];
1299                                 zip->ai = 1;
1300                                 /* Prefetch all to be invalidated */
1301                                 idx = zip->ca;
1302                                 end = zip->cq_ci;
1303                                 while (idx != end) {
1304                                         rte_prefetch0(&(*rxq->cqes)[(idx) &
1305                                                                     cqe_cnt]);
1306                                         ++idx;
1307                                 }
1308                         } else {
1309                                 len = rte_be_to_cpu_32(cqe->byte_cnt);
1310                         }
1311                 }
1312                 if (unlikely(rxq->err_state)) {
1313                         cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1314                         ++rxq->stats.idropped;
1315                 } else {
1316                         return len;
1317                 }
1318         } while (1);
1319 }
1320
1321 /**
1322  * Translate RX completion flags to offload flags.
1323  *
1324  * @param[in] cqe
1325  *   Pointer to CQE.
1326  *
1327  * @return
1328  *   Offload flags (ol_flags) for struct rte_mbuf.
1329  */
1330 static inline uint32_t
1331 rxq_cq_to_ol_flags(volatile struct mlx5_cqe *cqe)
1332 {
1333         uint32_t ol_flags = 0;
1334         uint16_t flags = rte_be_to_cpu_16(cqe->hdr_type_etc);
1335
1336         ol_flags =
1337                 TRANSPOSE(flags,
1338                           MLX5_CQE_RX_L3_HDR_VALID,
1339                           PKT_RX_IP_CKSUM_GOOD) |
1340                 TRANSPOSE(flags,
1341                           MLX5_CQE_RX_L4_HDR_VALID,
1342                           PKT_RX_L4_CKSUM_GOOD);
1343         return ol_flags;
1344 }
1345
1346 /**
1347  * Fill in mbuf fields from RX completion flags.
1348  * Note that pkt->ol_flags should be initialized outside of this function.
1349  *
1350  * @param rxq
1351  *   Pointer to RX queue.
1352  * @param pkt
1353  *   mbuf to fill.
1354  * @param cqe
1355  *   CQE to process.
1356  * @param rss_hash_res
1357  *   Packet RSS Hash result.
1358  */
1359 static inline void
1360 rxq_cq_to_mbuf(struct mlx5_rxq_data *rxq, struct rte_mbuf *pkt,
1361                volatile struct mlx5_cqe *cqe, uint32_t rss_hash_res)
1362 {
1363         /* Update packet information. */
1364         pkt->packet_type = rxq_cq_to_pkt_type(rxq, cqe);
1365         if (rss_hash_res && rxq->rss_hash) {
1366                 pkt->hash.rss = rss_hash_res;
1367                 pkt->ol_flags |= PKT_RX_RSS_HASH;
1368         }
1369         if (rxq->mark && MLX5_FLOW_MARK_IS_VALID(cqe->sop_drop_qpn)) {
1370                 pkt->ol_flags |= PKT_RX_FDIR;
1371                 if (cqe->sop_drop_qpn !=
1372                     rte_cpu_to_be_32(MLX5_FLOW_MARK_DEFAULT)) {
1373                         uint32_t mark = cqe->sop_drop_qpn;
1374
1375                         pkt->ol_flags |= PKT_RX_FDIR_ID;
1376                         pkt->hash.fdir.hi = mlx5_flow_mark_get(mark);
1377                 }
1378         }
1379         if (rxq->dynf_meta && cqe->flow_table_metadata) {
1380                 pkt->ol_flags |= rxq->flow_meta_mask;
1381                 *RTE_MBUF_DYNFIELD(pkt, rxq->flow_meta_offset, uint32_t *) =
1382                         cqe->flow_table_metadata;
1383         }
1384         if (rxq->csum)
1385                 pkt->ol_flags |= rxq_cq_to_ol_flags(cqe);
1386         if (rxq->vlan_strip &&
1387             (cqe->hdr_type_etc & rte_cpu_to_be_16(MLX5_CQE_VLAN_STRIPPED))) {
1388                 pkt->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
1389                 pkt->vlan_tci = rte_be_to_cpu_16(cqe->vlan_info);
1390         }
1391         if (rxq->hw_timestamp) {
1392                 pkt->timestamp = rte_be_to_cpu_64(cqe->timestamp);
1393                 pkt->ol_flags |= PKT_RX_TIMESTAMP;
1394         }
1395 }
1396
1397 /**
1398  * DPDK callback for RX.
1399  *
1400  * @param dpdk_rxq
1401  *   Generic pointer to RX queue structure.
1402  * @param[out] pkts
1403  *   Array to store received packets.
1404  * @param pkts_n
1405  *   Maximum number of packets in array.
1406  *
1407  * @return
1408  *   Number of packets successfully received (<= pkts_n).
1409  */
1410 uint16_t
1411 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1412 {
1413         struct mlx5_rxq_data *rxq = dpdk_rxq;
1414         const unsigned int wqe_cnt = (1 << rxq->elts_n) - 1;
1415         const unsigned int cqe_cnt = (1 << rxq->cqe_n) - 1;
1416         const unsigned int sges_n = rxq->sges_n;
1417         struct rte_mbuf *pkt = NULL;
1418         struct rte_mbuf *seg = NULL;
1419         volatile struct mlx5_cqe *cqe =
1420                 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1421         unsigned int i = 0;
1422         unsigned int rq_ci = rxq->rq_ci << sges_n;
1423         int len = 0; /* keep its value across iterations. */
1424
1425         while (pkts_n) {
1426                 unsigned int idx = rq_ci & wqe_cnt;
1427                 volatile struct mlx5_wqe_data_seg *wqe =
1428                         &((volatile struct mlx5_wqe_data_seg *)rxq->wqes)[idx];
1429                 struct rte_mbuf *rep = (*rxq->elts)[idx];
1430                 volatile struct mlx5_mini_cqe8 *mcqe = NULL;
1431                 uint32_t rss_hash_res;
1432
1433                 if (pkt)
1434                         NEXT(seg) = rep;
1435                 seg = rep;
1436                 rte_prefetch0(seg);
1437                 rte_prefetch0(cqe);
1438                 rte_prefetch0(wqe);
1439                 rep = rte_mbuf_raw_alloc(rxq->mp);
1440                 if (unlikely(rep == NULL)) {
1441                         ++rxq->stats.rx_nombuf;
1442                         if (!pkt) {
1443                                 /*
1444                                  * no buffers before we even started,
1445                                  * bail out silently.
1446                                  */
1447                                 break;
1448                         }
1449                         while (pkt != seg) {
1450                                 MLX5_ASSERT(pkt != (*rxq->elts)[idx]);
1451                                 rep = NEXT(pkt);
1452                                 NEXT(pkt) = NULL;
1453                                 NB_SEGS(pkt) = 1;
1454                                 rte_mbuf_raw_free(pkt);
1455                                 pkt = rep;
1456                         }
1457                         break;
1458                 }
1459                 if (!pkt) {
1460                         cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1461                         len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt, &mcqe);
1462                         if (!len) {
1463                                 rte_mbuf_raw_free(rep);
1464                                 break;
1465                         }
1466                         pkt = seg;
1467                         MLX5_ASSERT(len >= (rxq->crc_present << 2));
1468                         pkt->ol_flags &= EXT_ATTACHED_MBUF;
1469                         /* If compressed, take hash result from mini-CQE. */
1470                         rss_hash_res = rte_be_to_cpu_32(mcqe == NULL ?
1471                                                         cqe->rx_hash_res :
1472                                                         mcqe->rx_hash_result);
1473                         rxq_cq_to_mbuf(rxq, pkt, cqe, rss_hash_res);
1474                         if (rxq->crc_present)
1475                                 len -= RTE_ETHER_CRC_LEN;
1476                         PKT_LEN(pkt) = len;
1477                         if (cqe->lro_num_seg > 1) {
1478                                 mlx5_lro_update_hdr
1479                                         (rte_pktmbuf_mtod(pkt, uint8_t *), cqe,
1480                                          len);
1481                                 pkt->ol_flags |= PKT_RX_LRO;
1482                                 pkt->tso_segsz = len / cqe->lro_num_seg;
1483                         }
1484                 }
1485                 DATA_LEN(rep) = DATA_LEN(seg);
1486                 PKT_LEN(rep) = PKT_LEN(seg);
1487                 SET_DATA_OFF(rep, DATA_OFF(seg));
1488                 PORT(rep) = PORT(seg);
1489                 (*rxq->elts)[idx] = rep;
1490                 /*
1491                  * Fill NIC descriptor with the new buffer.  The lkey and size
1492                  * of the buffers are already known, only the buffer address
1493                  * changes.
1494                  */
1495                 wqe->addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(rep, uintptr_t));
1496                 /* If there's only one MR, no need to replace LKey in WQE. */
1497                 if (unlikely(mlx5_mr_btree_len(&rxq->mr_ctrl.cache_bh) > 1))
1498                         wqe->lkey = mlx5_rx_mb2mr(rxq, rep);
1499                 if (len > DATA_LEN(seg)) {
1500                         len -= DATA_LEN(seg);
1501                         ++NB_SEGS(pkt);
1502                         ++rq_ci;
1503                         continue;
1504                 }
1505                 DATA_LEN(seg) = len;
1506 #ifdef MLX5_PMD_SOFT_COUNTERS
1507                 /* Increment bytes counter. */
1508                 rxq->stats.ibytes += PKT_LEN(pkt);
1509 #endif
1510                 /* Return packet. */
1511                 *(pkts++) = pkt;
1512                 pkt = NULL;
1513                 --pkts_n;
1514                 ++i;
1515                 /* Align consumer index to the next stride. */
1516                 rq_ci >>= sges_n;
1517                 ++rq_ci;
1518                 rq_ci <<= sges_n;
1519         }
1520         if (unlikely((i == 0) && ((rq_ci >> sges_n) == rxq->rq_ci)))
1521                 return 0;
1522         /* Update the consumer index. */
1523         rxq->rq_ci = rq_ci >> sges_n;
1524         rte_cio_wmb();
1525         *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
1526         rte_cio_wmb();
1527         *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
1528 #ifdef MLX5_PMD_SOFT_COUNTERS
1529         /* Increment packets counter. */
1530         rxq->stats.ipackets += i;
1531 #endif
1532         return i;
1533 }
1534
1535 /**
1536  * Update LRO packet TCP header.
1537  * The HW LRO feature doesn't update the TCP header after coalescing the
1538  * TCP segments but supplies information in CQE to fill it by SW.
1539  *
1540  * @param tcp
1541  *   Pointer to the TCP header.
1542  * @param cqe
1543  *   Pointer to the completion entry..
1544  * @param phcsum
1545  *   The L3 pseudo-header checksum.
1546  */
1547 static inline void
1548 mlx5_lro_update_tcp_hdr(struct rte_tcp_hdr *__rte_restrict tcp,
1549                         volatile struct mlx5_cqe *__rte_restrict cqe,
1550                         uint32_t phcsum)
1551 {
1552         uint8_t l4_type = (rte_be_to_cpu_16(cqe->hdr_type_etc) &
1553                            MLX5_CQE_L4_TYPE_MASK) >> MLX5_CQE_L4_TYPE_SHIFT;
1554         /*
1555          * The HW calculates only the TCP payload checksum, need to complete
1556          * the TCP header checksum and the L3 pseudo-header checksum.
1557          */
1558         uint32_t csum = phcsum + cqe->csum;
1559
1560         if (l4_type == MLX5_L4_HDR_TYPE_TCP_EMPTY_ACK ||
1561             l4_type == MLX5_L4_HDR_TYPE_TCP_WITH_ACL) {
1562                 tcp->tcp_flags |= RTE_TCP_ACK_FLAG;
1563                 tcp->recv_ack = cqe->lro_ack_seq_num;
1564                 tcp->rx_win = cqe->lro_tcp_win;
1565         }
1566         if (cqe->lro_tcppsh_abort_dupack & MLX5_CQE_LRO_PUSH_MASK)
1567                 tcp->tcp_flags |= RTE_TCP_PSH_FLAG;
1568         tcp->cksum = 0;
1569         csum += rte_raw_cksum(tcp, (tcp->data_off >> 4) * 4);
1570         csum = ((csum & 0xffff0000) >> 16) + (csum & 0xffff);
1571         csum = (~csum) & 0xffff;
1572         if (csum == 0)
1573                 csum = 0xffff;
1574         tcp->cksum = csum;
1575 }
1576
1577 /**
1578  * Update LRO packet headers.
1579  * The HW LRO feature doesn't update the L3/TCP headers after coalescing the
1580  * TCP segments but supply information in CQE to fill it by SW.
1581  *
1582  * @param padd
1583  *   The packet address.
1584  * @param cqe
1585  *   Pointer to the completion entry..
1586  * @param len
1587  *   The packet length.
1588  */
1589 static inline void
1590 mlx5_lro_update_hdr(uint8_t *__rte_restrict padd,
1591                     volatile struct mlx5_cqe *__rte_restrict cqe,
1592                     uint32_t len)
1593 {
1594         union {
1595                 struct rte_ether_hdr *eth;
1596                 struct rte_vlan_hdr *vlan;
1597                 struct rte_ipv4_hdr *ipv4;
1598                 struct rte_ipv6_hdr *ipv6;
1599                 struct rte_tcp_hdr *tcp;
1600                 uint8_t *hdr;
1601         } h = {
1602                         .hdr = padd,
1603         };
1604         uint16_t proto = h.eth->ether_type;
1605         uint32_t phcsum;
1606
1607         h.eth++;
1608         while (proto == RTE_BE16(RTE_ETHER_TYPE_VLAN) ||
1609                proto == RTE_BE16(RTE_ETHER_TYPE_QINQ)) {
1610                 proto = h.vlan->eth_proto;
1611                 h.vlan++;
1612         }
1613         if (proto == RTE_BE16(RTE_ETHER_TYPE_IPV4)) {
1614                 h.ipv4->time_to_live = cqe->lro_min_ttl;
1615                 h.ipv4->total_length = rte_cpu_to_be_16(len - (h.hdr - padd));
1616                 h.ipv4->hdr_checksum = 0;
1617                 h.ipv4->hdr_checksum = rte_ipv4_cksum(h.ipv4);
1618                 phcsum = rte_ipv4_phdr_cksum(h.ipv4, 0);
1619                 h.ipv4++;
1620         } else {
1621                 h.ipv6->hop_limits = cqe->lro_min_ttl;
1622                 h.ipv6->payload_len = rte_cpu_to_be_16(len - (h.hdr - padd) -
1623                                                        sizeof(*h.ipv6));
1624                 phcsum = rte_ipv6_phdr_cksum(h.ipv6, 0);
1625                 h.ipv6++;
1626         }
1627         mlx5_lro_update_tcp_hdr(h.tcp, cqe, phcsum);
1628 }
1629
1630 void
1631 mlx5_mprq_buf_free_cb(void *addr __rte_unused, void *opaque)
1632 {
1633         struct mlx5_mprq_buf *buf = opaque;
1634
1635         if (rte_atomic16_read(&buf->refcnt) == 1) {
1636                 rte_mempool_put(buf->mp, buf);
1637         } else if (rte_atomic16_add_return(&buf->refcnt, -1) == 0) {
1638                 rte_atomic16_set(&buf->refcnt, 1);
1639                 rte_mempool_put(buf->mp, buf);
1640         }
1641 }
1642
1643 void
1644 mlx5_mprq_buf_free(struct mlx5_mprq_buf *buf)
1645 {
1646         mlx5_mprq_buf_free_cb(NULL, buf);
1647 }
1648
1649 static inline void
1650 mprq_buf_replace(struct mlx5_rxq_data *rxq, uint16_t rq_idx,
1651                  const unsigned int strd_n)
1652 {
1653         struct mlx5_mprq_buf *rep = rxq->mprq_repl;
1654         volatile struct mlx5_wqe_data_seg *wqe =
1655                 &((volatile struct mlx5_wqe_mprq *)rxq->wqes)[rq_idx].dseg;
1656         void *addr;
1657
1658         MLX5_ASSERT(rep != NULL);
1659         /* Replace MPRQ buf. */
1660         (*rxq->mprq_bufs)[rq_idx] = rep;
1661         /* Replace WQE. */
1662         addr = mlx5_mprq_buf_addr(rep, strd_n);
1663         wqe->addr = rte_cpu_to_be_64((uintptr_t)addr);
1664         /* If there's only one MR, no need to replace LKey in WQE. */
1665         if (unlikely(mlx5_mr_btree_len(&rxq->mr_ctrl.cache_bh) > 1))
1666                 wqe->lkey = mlx5_rx_addr2mr(rxq, (uintptr_t)addr);
1667         /* Stash a mbuf for next replacement. */
1668         if (likely(!rte_mempool_get(rxq->mprq_mp, (void **)&rep)))
1669                 rxq->mprq_repl = rep;
1670         else
1671                 rxq->mprq_repl = NULL;
1672 }
1673
1674 /**
1675  * DPDK callback for RX with Multi-Packet RQ support.
1676  *
1677  * @param dpdk_rxq
1678  *   Generic pointer to RX queue structure.
1679  * @param[out] pkts
1680  *   Array to store received packets.
1681  * @param pkts_n
1682  *   Maximum number of packets in array.
1683  *
1684  * @return
1685  *   Number of packets successfully received (<= pkts_n).
1686  */
1687 uint16_t
1688 mlx5_rx_burst_mprq(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1689 {
1690         struct mlx5_rxq_data *rxq = dpdk_rxq;
1691         const unsigned int strd_n = 1 << rxq->strd_num_n;
1692         const unsigned int strd_sz = 1 << rxq->strd_sz_n;
1693         const unsigned int strd_shift =
1694                 MLX5_MPRQ_STRIDE_SHIFT_BYTE * rxq->strd_shift_en;
1695         const unsigned int cq_mask = (1 << rxq->cqe_n) - 1;
1696         const unsigned int wq_mask = (1 << rxq->elts_n) - 1;
1697         volatile struct mlx5_cqe *cqe = &(*rxq->cqes)[rxq->cq_ci & cq_mask];
1698         unsigned int i = 0;
1699         uint32_t rq_ci = rxq->rq_ci;
1700         uint16_t consumed_strd = rxq->consumed_strd;
1701         struct mlx5_mprq_buf *buf = (*rxq->mprq_bufs)[rq_ci & wq_mask];
1702
1703         while (i < pkts_n) {
1704                 struct rte_mbuf *pkt;
1705                 void *addr;
1706                 int ret;
1707                 uint32_t len;
1708                 uint16_t strd_cnt;
1709                 uint16_t strd_idx;
1710                 uint32_t offset;
1711                 uint32_t byte_cnt;
1712                 int32_t hdrm_overlap;
1713                 volatile struct mlx5_mini_cqe8 *mcqe = NULL;
1714                 uint32_t rss_hash_res = 0;
1715
1716                 if (consumed_strd == strd_n) {
1717                         /* Replace WQE only if the buffer is still in use. */
1718                         if (rte_atomic16_read(&buf->refcnt) > 1) {
1719                                 mprq_buf_replace(rxq, rq_ci & wq_mask, strd_n);
1720                                 /* Release the old buffer. */
1721                                 mlx5_mprq_buf_free(buf);
1722                         } else if (unlikely(rxq->mprq_repl == NULL)) {
1723                                 struct mlx5_mprq_buf *rep;
1724
1725                                 /*
1726                                  * Currently, the MPRQ mempool is out of buffer
1727                                  * and doing memcpy regardless of the size of Rx
1728                                  * packet. Retry allocation to get back to
1729                                  * normal.
1730                                  */
1731                                 if (!rte_mempool_get(rxq->mprq_mp,
1732                                                      (void **)&rep))
1733                                         rxq->mprq_repl = rep;
1734                         }
1735                         /* Advance to the next WQE. */
1736                         consumed_strd = 0;
1737                         ++rq_ci;
1738                         buf = (*rxq->mprq_bufs)[rq_ci & wq_mask];
1739                 }
1740                 cqe = &(*rxq->cqes)[rxq->cq_ci & cq_mask];
1741                 ret = mlx5_rx_poll_len(rxq, cqe, cq_mask, &mcqe);
1742                 if (!ret)
1743                         break;
1744                 byte_cnt = ret;
1745                 strd_cnt = (byte_cnt & MLX5_MPRQ_STRIDE_NUM_MASK) >>
1746                            MLX5_MPRQ_STRIDE_NUM_SHIFT;
1747                 MLX5_ASSERT(strd_cnt);
1748                 consumed_strd += strd_cnt;
1749                 if (byte_cnt & MLX5_MPRQ_FILLER_MASK)
1750                         continue;
1751                 if (mcqe == NULL) {
1752                         rss_hash_res = rte_be_to_cpu_32(cqe->rx_hash_res);
1753                         strd_idx = rte_be_to_cpu_16(cqe->wqe_counter);
1754                 } else {
1755                         /* mini-CQE for MPRQ doesn't have hash result. */
1756                         strd_idx = rte_be_to_cpu_16(mcqe->stride_idx);
1757                 }
1758                 MLX5_ASSERT(strd_idx < strd_n);
1759                 MLX5_ASSERT(!((rte_be_to_cpu_16(cqe->wqe_id) ^ rq_ci) &
1760                             wq_mask));
1761                 pkt = rte_pktmbuf_alloc(rxq->mp);
1762                 if (unlikely(pkt == NULL)) {
1763                         ++rxq->stats.rx_nombuf;
1764                         break;
1765                 }
1766                 len = (byte_cnt & MLX5_MPRQ_LEN_MASK) >> MLX5_MPRQ_LEN_SHIFT;
1767                 MLX5_ASSERT((int)len >= (rxq->crc_present << 2));
1768                 if (rxq->crc_present)
1769                         len -= RTE_ETHER_CRC_LEN;
1770                 offset = strd_idx * strd_sz + strd_shift;
1771                 addr = RTE_PTR_ADD(mlx5_mprq_buf_addr(buf, strd_n), offset);
1772                 hdrm_overlap = len + RTE_PKTMBUF_HEADROOM - strd_cnt * strd_sz;
1773                 /*
1774                  * Memcpy packets to the target mbuf if:
1775                  * - The size of packet is smaller than mprq_max_memcpy_len.
1776                  * - Out of buffer in the Mempool for Multi-Packet RQ.
1777                  * - The packet's stride overlaps a headroom and scatter is off.
1778                  */
1779                 if (len <= rxq->mprq_max_memcpy_len ||
1780                     rxq->mprq_repl == NULL ||
1781                     (hdrm_overlap > 0 && !rxq->strd_scatter_en)) {
1782                         if (likely(rte_pktmbuf_tailroom(pkt) >= len)) {
1783                                 rte_memcpy(rte_pktmbuf_mtod(pkt, void *),
1784                                            addr, len);
1785                                 DATA_LEN(pkt) = len;
1786                         } else if (rxq->strd_scatter_en) {
1787                                 struct rte_mbuf *prev = pkt;
1788                                 uint32_t seg_len =
1789                                         RTE_MIN(rte_pktmbuf_tailroom(pkt), len);
1790                                 uint32_t rem_len = len - seg_len;
1791
1792                                 rte_memcpy(rte_pktmbuf_mtod(pkt, void *),
1793                                            addr, seg_len);
1794                                 DATA_LEN(pkt) = seg_len;
1795                                 while (rem_len) {
1796                                         struct rte_mbuf *next =
1797                                                 rte_pktmbuf_alloc(rxq->mp);
1798
1799                                         if (unlikely(next == NULL)) {
1800                                                 rte_pktmbuf_free(pkt);
1801                                                 ++rxq->stats.rx_nombuf;
1802                                                 goto out;
1803                                         }
1804                                         NEXT(prev) = next;
1805                                         SET_DATA_OFF(next, 0);
1806                                         addr = RTE_PTR_ADD(addr, seg_len);
1807                                         seg_len = RTE_MIN
1808                                                 (rte_pktmbuf_tailroom(next),
1809                                                  rem_len);
1810                                         rte_memcpy
1811                                                 (rte_pktmbuf_mtod(next, void *),
1812                                                  addr, seg_len);
1813                                         DATA_LEN(next) = seg_len;
1814                                         rem_len -= seg_len;
1815                                         prev = next;
1816                                         ++NB_SEGS(pkt);
1817                                 }
1818                         } else {
1819                                 rte_pktmbuf_free_seg(pkt);
1820                                 ++rxq->stats.idropped;
1821                                 continue;
1822                         }
1823                 } else {
1824                         rte_iova_t buf_iova;
1825                         struct rte_mbuf_ext_shared_info *shinfo;
1826                         uint16_t buf_len = strd_cnt * strd_sz;
1827                         void *buf_addr;
1828
1829                         /* Increment the refcnt of the whole chunk. */
1830                         rte_atomic16_add_return(&buf->refcnt, 1);
1831                         MLX5_ASSERT((uint16_t)rte_atomic16_read(&buf->refcnt) <=
1832                                     strd_n + 1);
1833                         buf_addr = RTE_PTR_SUB(addr, RTE_PKTMBUF_HEADROOM);
1834                         /*
1835                          * MLX5 device doesn't use iova but it is necessary in a
1836                          * case where the Rx packet is transmitted via a
1837                          * different PMD.
1838                          */
1839                         buf_iova = rte_mempool_virt2iova(buf) +
1840                                    RTE_PTR_DIFF(buf_addr, buf);
1841                         shinfo = &buf->shinfos[strd_idx];
1842                         rte_mbuf_ext_refcnt_set(shinfo, 1);
1843                         /*
1844                          * EXT_ATTACHED_MBUF will be set to pkt->ol_flags when
1845                          * attaching the stride to mbuf and more offload flags
1846                          * will be added below by calling rxq_cq_to_mbuf().
1847                          * Other fields will be overwritten.
1848                          */
1849                         rte_pktmbuf_attach_extbuf(pkt, buf_addr, buf_iova,
1850                                                   buf_len, shinfo);
1851                         /* Set mbuf head-room. */
1852                         SET_DATA_OFF(pkt, RTE_PKTMBUF_HEADROOM);
1853                         MLX5_ASSERT(pkt->ol_flags == EXT_ATTACHED_MBUF);
1854                         MLX5_ASSERT(rte_pktmbuf_tailroom(pkt) >=
1855                                 len - (hdrm_overlap > 0 ? hdrm_overlap : 0));
1856                         DATA_LEN(pkt) = len;
1857                         /*
1858                          * Copy the last fragment of a packet (up to headroom
1859                          * size bytes) in case there is a stride overlap with
1860                          * a next packet's headroom. Allocate a separate mbuf
1861                          * to store this fragment and link it. Scatter is on.
1862                          */
1863                         if (hdrm_overlap > 0) {
1864                                 MLX5_ASSERT(rxq->strd_scatter_en);
1865                                 struct rte_mbuf *seg =
1866                                         rte_pktmbuf_alloc(rxq->mp);
1867
1868                                 if (unlikely(seg == NULL)) {
1869                                         rte_pktmbuf_free_seg(pkt);
1870                                         ++rxq->stats.rx_nombuf;
1871                                         break;
1872                                 }
1873                                 SET_DATA_OFF(seg, 0);
1874                                 rte_memcpy(rte_pktmbuf_mtod(seg, void *),
1875                                         RTE_PTR_ADD(addr, len - hdrm_overlap),
1876                                         hdrm_overlap);
1877                                 DATA_LEN(seg) = hdrm_overlap;
1878                                 DATA_LEN(pkt) = len - hdrm_overlap;
1879                                 NEXT(pkt) = seg;
1880                                 NB_SEGS(pkt) = 2;
1881                         }
1882                 }
1883                 rxq_cq_to_mbuf(rxq, pkt, cqe, rss_hash_res);
1884                 if (cqe->lro_num_seg > 1) {
1885                         mlx5_lro_update_hdr(addr, cqe, len);
1886                         pkt->ol_flags |= PKT_RX_LRO;
1887                         pkt->tso_segsz = len / cqe->lro_num_seg;
1888                 }
1889                 PKT_LEN(pkt) = len;
1890                 PORT(pkt) = rxq->port_id;
1891 #ifdef MLX5_PMD_SOFT_COUNTERS
1892                 /* Increment bytes counter. */
1893                 rxq->stats.ibytes += PKT_LEN(pkt);
1894 #endif
1895                 /* Return packet. */
1896                 *(pkts++) = pkt;
1897                 ++i;
1898         }
1899 out:
1900         /* Update the consumer indexes. */
1901         rxq->consumed_strd = consumed_strd;
1902         rte_cio_wmb();
1903         *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
1904         if (rq_ci != rxq->rq_ci) {
1905                 rxq->rq_ci = rq_ci;
1906                 rte_cio_wmb();
1907                 *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
1908         }
1909 #ifdef MLX5_PMD_SOFT_COUNTERS
1910         /* Increment packets counter. */
1911         rxq->stats.ipackets += i;
1912 #endif
1913         return i;
1914 }
1915
1916 /**
1917  * Dummy DPDK callback for TX.
1918  *
1919  * This function is used to temporarily replace the real callback during
1920  * unsafe control operations on the queue, or in case of error.
1921  *
1922  * @param dpdk_txq
1923  *   Generic pointer to TX queue structure.
1924  * @param[in] pkts
1925  *   Packets to transmit.
1926  * @param pkts_n
1927  *   Number of packets in array.
1928  *
1929  * @return
1930  *   Number of packets successfully transmitted (<= pkts_n).
1931  */
1932 uint16_t
1933 removed_tx_burst(void *dpdk_txq __rte_unused,
1934                  struct rte_mbuf **pkts __rte_unused,
1935                  uint16_t pkts_n __rte_unused)
1936 {
1937         rte_mb();
1938         return 0;
1939 }
1940
1941 /**
1942  * Dummy DPDK callback for RX.
1943  *
1944  * This function is used to temporarily replace the real callback during
1945  * unsafe control operations on the queue, or in case of error.
1946  *
1947  * @param dpdk_rxq
1948  *   Generic pointer to RX queue structure.
1949  * @param[out] pkts
1950  *   Array to store received packets.
1951  * @param pkts_n
1952  *   Maximum number of packets in array.
1953  *
1954  * @return
1955  *   Number of packets successfully received (<= pkts_n).
1956  */
1957 uint16_t
1958 removed_rx_burst(void *dpdk_txq __rte_unused,
1959                  struct rte_mbuf **pkts __rte_unused,
1960                  uint16_t pkts_n __rte_unused)
1961 {
1962         rte_mb();
1963         return 0;
1964 }
1965
1966 /*
1967  * Vectorized Rx/Tx routines are not compiled in when required vector
1968  * instructions are not supported on a target architecture. The following null
1969  * stubs are needed for linkage when those are not included outside of this file
1970  * (e.g.  mlx5_rxtx_vec_sse.c for x86).
1971  */
1972
1973 __rte_weak uint16_t
1974 mlx5_rx_burst_vec(void *dpdk_txq __rte_unused,
1975                   struct rte_mbuf **pkts __rte_unused,
1976                   uint16_t pkts_n __rte_unused)
1977 {
1978         return 0;
1979 }
1980
1981 __rte_weak int
1982 mlx5_rxq_check_vec_support(struct mlx5_rxq_data *rxq __rte_unused)
1983 {
1984         return -ENOTSUP;
1985 }
1986
1987 __rte_weak int
1988 mlx5_check_vec_rx_support(struct rte_eth_dev *dev __rte_unused)
1989 {
1990         return -ENOTSUP;
1991 }
1992
1993 /**
1994  * Free the mbufs from the linear array of pointers.
1995  *
1996  * @param pkts
1997  *   Pointer to array of packets to be free.
1998  * @param pkts_n
1999  *   Number of packets to be freed.
2000  * @param olx
2001  *   Configured Tx offloads mask. It is fully defined at
2002  *   compile time and may be used for optimization.
2003  */
2004 static __rte_always_inline void
2005 mlx5_tx_free_mbuf(struct rte_mbuf **__rte_restrict pkts,
2006                   unsigned int pkts_n,
2007                   unsigned int olx __rte_unused)
2008 {
2009         struct rte_mempool *pool = NULL;
2010         struct rte_mbuf **p_free = NULL;
2011         struct rte_mbuf *mbuf;
2012         unsigned int n_free = 0;
2013
2014         /*
2015          * The implemented algorithm eliminates
2016          * copying pointers to temporary array
2017          * for rte_mempool_put_bulk() calls.
2018          */
2019         MLX5_ASSERT(pkts);
2020         MLX5_ASSERT(pkts_n);
2021         for (;;) {
2022                 for (;;) {
2023                         /*
2024                          * Decrement mbuf reference counter, detach
2025                          * indirect and external buffers if needed.
2026                          */
2027                         mbuf = rte_pktmbuf_prefree_seg(*pkts);
2028                         if (likely(mbuf != NULL)) {
2029                                 MLX5_ASSERT(mbuf == *pkts);
2030                                 if (likely(n_free != 0)) {
2031                                         if (unlikely(pool != mbuf->pool))
2032                                                 /* From different pool. */
2033                                                 break;
2034                                 } else {
2035                                         /* Start new scan array. */
2036                                         pool = mbuf->pool;
2037                                         p_free = pkts;
2038                                 }
2039                                 ++n_free;
2040                                 ++pkts;
2041                                 --pkts_n;
2042                                 if (unlikely(pkts_n == 0)) {
2043                                         mbuf = NULL;
2044                                         break;
2045                                 }
2046                         } else {
2047                                 /*
2048                                  * This happens if mbuf is still referenced.
2049                                  * We can't put it back to the pool, skip.
2050                                  */
2051                                 ++pkts;
2052                                 --pkts_n;
2053                                 if (unlikely(n_free != 0))
2054                                         /* There is some array to free.*/
2055                                         break;
2056                                 if (unlikely(pkts_n == 0))
2057                                         /* Last mbuf, nothing to free. */
2058                                         return;
2059                         }
2060                 }
2061                 for (;;) {
2062                         /*
2063                          * This loop is implemented to avoid multiple
2064                          * inlining of rte_mempool_put_bulk().
2065                          */
2066                         MLX5_ASSERT(pool);
2067                         MLX5_ASSERT(p_free);
2068                         MLX5_ASSERT(n_free);
2069                         /*
2070                          * Free the array of pre-freed mbufs
2071                          * belonging to the same memory pool.
2072                          */
2073                         rte_mempool_put_bulk(pool, (void *)p_free, n_free);
2074                         if (unlikely(mbuf != NULL)) {
2075                                 /* There is the request to start new scan. */
2076                                 pool = mbuf->pool;
2077                                 p_free = pkts++;
2078                                 n_free = 1;
2079                                 --pkts_n;
2080                                 if (likely(pkts_n != 0))
2081                                         break;
2082                                 /*
2083                                  * This is the last mbuf to be freed.
2084                                  * Do one more loop iteration to complete.
2085                                  * This is rare case of the last unique mbuf.
2086                                  */
2087                                 mbuf = NULL;
2088                                 continue;
2089                         }
2090                         if (likely(pkts_n == 0))
2091                                 return;
2092                         n_free = 0;
2093                         break;
2094                 }
2095         }
2096 }
2097
2098 /**
2099  * Free the mbuf from the elts ring buffer till new tail.
2100  *
2101  * @param txq
2102  *   Pointer to Tx queue structure.
2103  * @param tail
2104  *   Index in elts to free up to, becomes new elts tail.
2105  * @param olx
2106  *   Configured Tx offloads mask. It is fully defined at
2107  *   compile time and may be used for optimization.
2108  */
2109 static __rte_always_inline void
2110 mlx5_tx_free_elts(struct mlx5_txq_data *__rte_restrict txq,
2111                   uint16_t tail,
2112                   unsigned int olx __rte_unused)
2113 {
2114         uint16_t n_elts = tail - txq->elts_tail;
2115
2116         MLX5_ASSERT(n_elts);
2117         MLX5_ASSERT(n_elts <= txq->elts_s);
2118         /*
2119          * Implement a loop to support ring buffer wraparound
2120          * with single inlining of mlx5_tx_free_mbuf().
2121          */
2122         do {
2123                 unsigned int part;
2124
2125                 part = txq->elts_s - (txq->elts_tail & txq->elts_m);
2126                 part = RTE_MIN(part, n_elts);
2127                 MLX5_ASSERT(part);
2128                 MLX5_ASSERT(part <= txq->elts_s);
2129                 mlx5_tx_free_mbuf(&txq->elts[txq->elts_tail & txq->elts_m],
2130                                   part, olx);
2131                 txq->elts_tail += part;
2132                 n_elts -= part;
2133         } while (n_elts);
2134 }
2135
2136 /**
2137  * Store the mbuf being sent into elts ring buffer.
2138  * On Tx completion these mbufs will be freed.
2139  *
2140  * @param txq
2141  *   Pointer to Tx queue structure.
2142  * @param pkts
2143  *   Pointer to array of packets to be stored.
2144  * @param pkts_n
2145  *   Number of packets to be stored.
2146  * @param olx
2147  *   Configured Tx offloads mask. It is fully defined at
2148  *   compile time and may be used for optimization.
2149  */
2150 static __rte_always_inline void
2151 mlx5_tx_copy_elts(struct mlx5_txq_data *__rte_restrict txq,
2152                   struct rte_mbuf **__rte_restrict pkts,
2153                   unsigned int pkts_n,
2154                   unsigned int olx __rte_unused)
2155 {
2156         unsigned int part;
2157         struct rte_mbuf **elts = (struct rte_mbuf **)txq->elts;
2158
2159         MLX5_ASSERT(pkts);
2160         MLX5_ASSERT(pkts_n);
2161         part = txq->elts_s - (txq->elts_head & txq->elts_m);
2162         MLX5_ASSERT(part);
2163         MLX5_ASSERT(part <= txq->elts_s);
2164         /* This code is a good candidate for vectorizing with SIMD. */
2165         rte_memcpy((void *)(elts + (txq->elts_head & txq->elts_m)),
2166                    (void *)pkts,
2167                    RTE_MIN(part, pkts_n) * sizeof(struct rte_mbuf *));
2168         txq->elts_head += pkts_n;
2169         if (unlikely(part < pkts_n))
2170                 /* The copy is wrapping around the elts array. */
2171                 rte_memcpy((void *)elts, (void *)(pkts + part),
2172                            (pkts_n - part) * sizeof(struct rte_mbuf *));
2173 }
2174
2175 /**
2176  * Update completion queue consuming index via doorbell
2177  * and flush the completed data buffers.
2178  *
2179  * @param txq
2180  *   Pointer to TX queue structure.
2181  * @param valid CQE pointer
2182  *   if not NULL update txq->wqe_pi and flush the buffers
2183  * @param olx
2184  *   Configured Tx offloads mask. It is fully defined at
2185  *   compile time and may be used for optimization.
2186  */
2187 static __rte_always_inline void
2188 mlx5_tx_comp_flush(struct mlx5_txq_data *__rte_restrict txq,
2189                    volatile struct mlx5_cqe *last_cqe,
2190                    unsigned int olx __rte_unused)
2191 {
2192         if (likely(last_cqe != NULL)) {
2193                 uint16_t tail;
2194
2195                 txq->wqe_pi = rte_be_to_cpu_16(last_cqe->wqe_counter);
2196                 tail = txq->fcqs[(txq->cq_ci - 1) & txq->cqe_m];
2197                 if (likely(tail != txq->elts_tail)) {
2198                         mlx5_tx_free_elts(txq, tail, olx);
2199                         MLX5_ASSERT(tail == txq->elts_tail);
2200                 }
2201         }
2202 }
2203
2204 /**
2205  * Manage TX completions. This routine checks the CQ for
2206  * arrived CQEs, deduces the last accomplished WQE in SQ,
2207  * updates SQ producing index and frees all completed mbufs.
2208  *
2209  * @param txq
2210  *   Pointer to TX queue structure.
2211  * @param olx
2212  *   Configured Tx offloads mask. It is fully defined at
2213  *   compile time and may be used for optimization.
2214  *
2215  * NOTE: not inlined intentionally, it makes tx_burst
2216  * routine smaller, simple and faster - from experiments.
2217  */
2218 static void
2219 mlx5_tx_handle_completion(struct mlx5_txq_data *__rte_restrict txq,
2220                           unsigned int olx __rte_unused)
2221 {
2222         unsigned int count = MLX5_TX_COMP_MAX_CQE;
2223         volatile struct mlx5_cqe *last_cqe = NULL;
2224         bool ring_doorbell = false;
2225         int ret;
2226
2227         static_assert(MLX5_CQE_STATUS_HW_OWN < 0, "Must be negative value");
2228         static_assert(MLX5_CQE_STATUS_SW_OWN < 0, "Must be negative value");
2229         do {
2230                 volatile struct mlx5_cqe *cqe;
2231
2232                 cqe = &txq->cqes[txq->cq_ci & txq->cqe_m];
2233                 ret = check_cqe(cqe, txq->cqe_s, txq->cq_ci);
2234                 if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
2235                         if (likely(ret != MLX5_CQE_STATUS_ERR)) {
2236                                 /* No new CQEs in completion queue. */
2237                                 MLX5_ASSERT(ret == MLX5_CQE_STATUS_HW_OWN);
2238                                 break;
2239                         }
2240                         /*
2241                          * Some error occurred, try to restart.
2242                          * We have no barrier after WQE related Doorbell
2243                          * written, make sure all writes are completed
2244                          * here, before we might perform SQ reset.
2245                          */
2246                         rte_wmb();
2247                         ret = mlx5_tx_error_cqe_handle
2248                                 (txq, (volatile struct mlx5_err_cqe *)cqe);
2249                         if (unlikely(ret < 0)) {
2250                                 /*
2251                                  * Some error occurred on queue error
2252                                  * handling, we do not advance the index
2253                                  * here, allowing to retry on next call.
2254                                  */
2255                                 return;
2256                         }
2257                         /*
2258                          * We are going to fetch all entries with
2259                          * MLX5_CQE_SYNDROME_WR_FLUSH_ERR status.
2260                          * The send queue is supposed to be empty.
2261                          */
2262                         ring_doorbell = true;
2263                         ++txq->cq_ci;
2264                         txq->cq_pi = txq->cq_ci;
2265                         last_cqe = NULL;
2266                         continue;
2267                 }
2268                 /* Normal transmit completion. */
2269                 MLX5_ASSERT(txq->cq_ci != txq->cq_pi);
2270                 MLX5_ASSERT((txq->fcqs[txq->cq_ci & txq->cqe_m] >> 16) ==
2271                             cqe->wqe_counter);
2272                 ring_doorbell = true;
2273                 ++txq->cq_ci;
2274                 last_cqe = cqe;
2275                 /*
2276                  * We have to restrict the amount of processed CQEs
2277                  * in one tx_burst routine call. The CQ may be large
2278                  * and many CQEs may be updated by the NIC in one
2279                  * transaction. Buffers freeing is time consuming,
2280                  * multiple iterations may introduce significant
2281                  * latency.
2282                  */
2283                 if (likely(--count == 0))
2284                         break;
2285         } while (true);
2286         if (likely(ring_doorbell)) {
2287                 /* Ring doorbell to notify hardware. */
2288                 rte_compiler_barrier();
2289                 *txq->cq_db = rte_cpu_to_be_32(txq->cq_ci);
2290                 mlx5_tx_comp_flush(txq, last_cqe, olx);
2291         }
2292 }
2293
2294 /**
2295  * Check if the completion request flag should be set in the last WQE.
2296  * Both pushed mbufs and WQEs are monitored and the completion request
2297  * flag is set if any of thresholds is reached.
2298  *
2299  * @param txq
2300  *   Pointer to TX queue structure.
2301  * @param loc
2302  *   Pointer to burst routine local context.
2303  * @param olx
2304  *   Configured Tx offloads mask. It is fully defined at
2305  *   compile time and may be used for optimization.
2306  */
2307 static __rte_always_inline void
2308 mlx5_tx_request_completion(struct mlx5_txq_data *__rte_restrict txq,
2309                            struct mlx5_txq_local *__rte_restrict loc,
2310                            unsigned int olx)
2311 {
2312         uint16_t head = txq->elts_head;
2313         unsigned int part;
2314
2315         part = MLX5_TXOFF_CONFIG(INLINE) ?
2316                0 : loc->pkts_sent - loc->pkts_copy;
2317         head += part;
2318         if ((uint16_t)(head - txq->elts_comp) >= MLX5_TX_COMP_THRESH ||
2319              (MLX5_TXOFF_CONFIG(INLINE) &&
2320              (uint16_t)(txq->wqe_ci - txq->wqe_comp) >= txq->wqe_thres)) {
2321                 volatile struct mlx5_wqe *last = loc->wqe_last;
2322
2323                 MLX5_ASSERT(last);
2324                 txq->elts_comp = head;
2325                 if (MLX5_TXOFF_CONFIG(INLINE))
2326                         txq->wqe_comp = txq->wqe_ci;
2327                 /* Request unconditional completion on last WQE. */
2328                 last->cseg.flags = RTE_BE32(MLX5_COMP_ALWAYS <<
2329                                             MLX5_COMP_MODE_OFFSET);
2330                 /* Save elts_head in dedicated free on completion queue. */
2331 #ifdef RTE_LIBRTE_MLX5_DEBUG
2332                 txq->fcqs[txq->cq_pi++ & txq->cqe_m] = head |
2333                           (last->cseg.opcode >> 8) << 16;
2334 #else
2335                 txq->fcqs[txq->cq_pi++ & txq->cqe_m] = head;
2336 #endif
2337                 /* A CQE slot must always be available. */
2338                 MLX5_ASSERT((txq->cq_pi - txq->cq_ci) <= txq->cqe_s);
2339         }
2340 }
2341
2342 /**
2343  * DPDK callback to check the status of a tx descriptor.
2344  *
2345  * @param tx_queue
2346  *   The tx queue.
2347  * @param[in] offset
2348  *   The index of the descriptor in the ring.
2349  *
2350  * @return
2351  *   The status of the tx descriptor.
2352  */
2353 int
2354 mlx5_tx_descriptor_status(void *tx_queue, uint16_t offset)
2355 {
2356         struct mlx5_txq_data *__rte_restrict txq = tx_queue;
2357         uint16_t used;
2358
2359         mlx5_tx_handle_completion(txq, 0);
2360         used = txq->elts_head - txq->elts_tail;
2361         if (offset < used)
2362                 return RTE_ETH_TX_DESC_FULL;
2363         return RTE_ETH_TX_DESC_DONE;
2364 }
2365
2366 /**
2367  * Build the Control Segment with specified opcode:
2368  * - MLX5_OPCODE_SEND
2369  * - MLX5_OPCODE_ENHANCED_MPSW
2370  * - MLX5_OPCODE_TSO
2371  *
2372  * @param txq
2373  *   Pointer to TX queue structure.
2374  * @param loc
2375  *   Pointer to burst routine local context.
2376  * @param wqe
2377  *   Pointer to WQE to fill with built Control Segment.
2378  * @param ds
2379  *   Supposed length of WQE in segments.
2380  * @param opcode
2381  *   SQ WQE opcode to put into Control Segment.
2382  * @param olx
2383  *   Configured Tx offloads mask. It is fully defined at
2384  *   compile time and may be used for optimization.
2385  */
2386 static __rte_always_inline void
2387 mlx5_tx_cseg_init(struct mlx5_txq_data *__rte_restrict txq,
2388                   struct mlx5_txq_local *__rte_restrict loc __rte_unused,
2389                   struct mlx5_wqe *__rte_restrict wqe,
2390                   unsigned int ds,
2391                   unsigned int opcode,
2392                   unsigned int olx __rte_unused)
2393 {
2394         struct mlx5_wqe_cseg *__rte_restrict cs = &wqe->cseg;
2395
2396         /* For legacy MPW replace the EMPW by TSO with modifier. */
2397         if (MLX5_TXOFF_CONFIG(MPW) && opcode == MLX5_OPCODE_ENHANCED_MPSW)
2398                 opcode = MLX5_OPCODE_TSO | MLX5_OPC_MOD_MPW << 24;
2399         cs->opcode = rte_cpu_to_be_32((txq->wqe_ci << 8) | opcode);
2400         cs->sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | ds);
2401         cs->flags = RTE_BE32(MLX5_COMP_ONLY_FIRST_ERR <<
2402                              MLX5_COMP_MODE_OFFSET);
2403         cs->misc = RTE_BE32(0);
2404 }
2405
2406 /**
2407  * Build the Ethernet Segment without inlined data.
2408  * Supports Software Parser, Checksums and VLAN
2409  * insertion Tx offload features.
2410  *
2411  * @param txq
2412  *   Pointer to TX queue structure.
2413  * @param loc
2414  *   Pointer to burst routine local context.
2415  * @param wqe
2416  *   Pointer to WQE to fill with built Ethernet Segment.
2417  * @param olx
2418  *   Configured Tx offloads mask. It is fully defined at
2419  *   compile time and may be used for optimization.
2420  */
2421 static __rte_always_inline void
2422 mlx5_tx_eseg_none(struct mlx5_txq_data *__rte_restrict txq __rte_unused,
2423                   struct mlx5_txq_local *__rte_restrict loc,
2424                   struct mlx5_wqe *__rte_restrict wqe,
2425                   unsigned int olx)
2426 {
2427         struct mlx5_wqe_eseg *__rte_restrict es = &wqe->eseg;
2428         uint32_t csum;
2429
2430         /*
2431          * Calculate and set check sum flags first, dword field
2432          * in segment may be shared with Software Parser flags.
2433          */
2434         csum = MLX5_TXOFF_CONFIG(CSUM) ? txq_ol_cksum_to_cs(loc->mbuf) : 0;
2435         es->flags = rte_cpu_to_le_32(csum);
2436         /*
2437          * Calculate and set Software Parser offsets and flags.
2438          * These flags a set for custom UDP and IP tunnel packets.
2439          */
2440         es->swp_offs = txq_mbuf_to_swp(loc, &es->swp_flags, olx);
2441         /* Fill metadata field if needed. */
2442         es->metadata = MLX5_TXOFF_CONFIG(METADATA) ?
2443                        loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
2444                        *RTE_FLOW_DYNF_METADATA(loc->mbuf) : 0 : 0;
2445         /* Engage VLAN tag insertion feature if requested. */
2446         if (MLX5_TXOFF_CONFIG(VLAN) &&
2447             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
2448                 /*
2449                  * We should get here only if device support
2450                  * this feature correctly.
2451                  */
2452                 MLX5_ASSERT(txq->vlan_en);
2453                 es->inline_hdr = rte_cpu_to_be_32(MLX5_ETH_WQE_VLAN_INSERT |
2454                                                   loc->mbuf->vlan_tci);
2455         } else {
2456                 es->inline_hdr = RTE_BE32(0);
2457         }
2458 }
2459
2460 /**
2461  * Build the Ethernet Segment with minimal inlined data
2462  * of MLX5_ESEG_MIN_INLINE_SIZE bytes length. This is
2463  * used to fill the gap in single WQEBB WQEs.
2464  * Supports Software Parser, Checksums and VLAN
2465  * insertion Tx offload features.
2466  *
2467  * @param txq
2468  *   Pointer to TX queue structure.
2469  * @param loc
2470  *   Pointer to burst routine local context.
2471  * @param wqe
2472  *   Pointer to WQE to fill with built Ethernet Segment.
2473  * @param vlan
2474  *   Length of VLAN tag insertion if any.
2475  * @param olx
2476  *   Configured Tx offloads mask. It is fully defined at
2477  *   compile time and may be used for optimization.
2478  */
2479 static __rte_always_inline void
2480 mlx5_tx_eseg_dmin(struct mlx5_txq_data *__rte_restrict txq __rte_unused,
2481                   struct mlx5_txq_local *__rte_restrict loc,
2482                   struct mlx5_wqe *__rte_restrict wqe,
2483                   unsigned int vlan,
2484                   unsigned int olx)
2485 {
2486         struct mlx5_wqe_eseg *__rte_restrict es = &wqe->eseg;
2487         uint32_t csum;
2488         uint8_t *psrc, *pdst;
2489
2490         /*
2491          * Calculate and set check sum flags first, dword field
2492          * in segment may be shared with Software Parser flags.
2493          */
2494         csum = MLX5_TXOFF_CONFIG(CSUM) ? txq_ol_cksum_to_cs(loc->mbuf) : 0;
2495         es->flags = rte_cpu_to_le_32(csum);
2496         /*
2497          * Calculate and set Software Parser offsets and flags.
2498          * These flags a set for custom UDP and IP tunnel packets.
2499          */
2500         es->swp_offs = txq_mbuf_to_swp(loc, &es->swp_flags, olx);
2501         /* Fill metadata field if needed. */
2502         es->metadata = MLX5_TXOFF_CONFIG(METADATA) ?
2503                        loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
2504                        *RTE_FLOW_DYNF_METADATA(loc->mbuf) : 0 : 0;
2505         static_assert(MLX5_ESEG_MIN_INLINE_SIZE ==
2506                                 (sizeof(uint16_t) +
2507                                  sizeof(rte_v128u32_t)),
2508                       "invalid Ethernet Segment data size");
2509         static_assert(MLX5_ESEG_MIN_INLINE_SIZE ==
2510                                 (sizeof(uint16_t) +
2511                                  sizeof(struct rte_vlan_hdr) +
2512                                  2 * RTE_ETHER_ADDR_LEN),
2513                       "invalid Ethernet Segment data size");
2514         psrc = rte_pktmbuf_mtod(loc->mbuf, uint8_t *);
2515         es->inline_hdr_sz = RTE_BE16(MLX5_ESEG_MIN_INLINE_SIZE);
2516         es->inline_data = *(unaligned_uint16_t *)psrc;
2517         psrc += sizeof(uint16_t);
2518         pdst = (uint8_t *)(es + 1);
2519         if (MLX5_TXOFF_CONFIG(VLAN) && vlan) {
2520                 /* Implement VLAN tag insertion as part inline data. */
2521                 memcpy(pdst, psrc, 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t));
2522                 pdst += 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t);
2523                 psrc += 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t);
2524                 /* Insert VLAN ethertype + VLAN tag. */
2525                 *(unaligned_uint32_t *)pdst = rte_cpu_to_be_32
2526                                                 ((RTE_ETHER_TYPE_VLAN << 16) |
2527                                                  loc->mbuf->vlan_tci);
2528                 pdst += sizeof(struct rte_vlan_hdr);
2529                 /* Copy the rest two bytes from packet data. */
2530                 MLX5_ASSERT(pdst == RTE_PTR_ALIGN(pdst, sizeof(uint16_t)));
2531                 *(uint16_t *)pdst = *(unaligned_uint16_t *)psrc;
2532         } else {
2533                 /* Fill the gap in the title WQEBB with inline data. */
2534                 rte_mov16(pdst, psrc);
2535         }
2536 }
2537
2538 /**
2539  * Build the Ethernet Segment with entire packet
2540  * data inlining. Checks the boundary of WQEBB and
2541  * ring buffer wrapping, supports Software Parser,
2542  * Checksums and VLAN insertion Tx offload features.
2543  *
2544  * @param txq
2545  *   Pointer to TX queue structure.
2546  * @param loc
2547  *   Pointer to burst routine local context.
2548  * @param wqe
2549  *   Pointer to WQE to fill with built Ethernet Segment.
2550  * @param vlan
2551  *   Length of VLAN tag insertion if any.
2552  * @param inlen
2553  *   Length of data to inline (VLAN included, if any).
2554  * @param tso
2555  *   TSO flag, set mss field from the packet.
2556  * @param olx
2557  *   Configured Tx offloads mask. It is fully defined at
2558  *   compile time and may be used for optimization.
2559  *
2560  * @return
2561  *   Pointer to the next Data Segment (aligned and wrapped around).
2562  */
2563 static __rte_always_inline struct mlx5_wqe_dseg *
2564 mlx5_tx_eseg_data(struct mlx5_txq_data *__rte_restrict txq,
2565                   struct mlx5_txq_local *__rte_restrict loc,
2566                   struct mlx5_wqe *__rte_restrict wqe,
2567                   unsigned int vlan,
2568                   unsigned int inlen,
2569                   unsigned int tso,
2570                   unsigned int olx)
2571 {
2572         struct mlx5_wqe_eseg *__rte_restrict es = &wqe->eseg;
2573         uint32_t csum;
2574         uint8_t *psrc, *pdst;
2575         unsigned int part;
2576
2577         /*
2578          * Calculate and set check sum flags first, dword field
2579          * in segment may be shared with Software Parser flags.
2580          */
2581         csum = MLX5_TXOFF_CONFIG(CSUM) ? txq_ol_cksum_to_cs(loc->mbuf) : 0;
2582         if (tso) {
2583                 csum <<= 24;
2584                 csum |= loc->mbuf->tso_segsz;
2585                 es->flags = rte_cpu_to_be_32(csum);
2586         } else {
2587                 es->flags = rte_cpu_to_le_32(csum);
2588         }
2589         /*
2590          * Calculate and set Software Parser offsets and flags.
2591          * These flags a set for custom UDP and IP tunnel packets.
2592          */
2593         es->swp_offs = txq_mbuf_to_swp(loc, &es->swp_flags, olx);
2594         /* Fill metadata field if needed. */
2595         es->metadata = MLX5_TXOFF_CONFIG(METADATA) ?
2596                        loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
2597                        *RTE_FLOW_DYNF_METADATA(loc->mbuf) : 0 : 0;
2598         static_assert(MLX5_ESEG_MIN_INLINE_SIZE ==
2599                                 (sizeof(uint16_t) +
2600                                  sizeof(rte_v128u32_t)),
2601                       "invalid Ethernet Segment data size");
2602         static_assert(MLX5_ESEG_MIN_INLINE_SIZE ==
2603                                 (sizeof(uint16_t) +
2604                                  sizeof(struct rte_vlan_hdr) +
2605                                  2 * RTE_ETHER_ADDR_LEN),
2606                       "invalid Ethernet Segment data size");
2607         psrc = rte_pktmbuf_mtod(loc->mbuf, uint8_t *);
2608         es->inline_hdr_sz = rte_cpu_to_be_16(inlen);
2609         es->inline_data = *(unaligned_uint16_t *)psrc;
2610         psrc += sizeof(uint16_t);
2611         pdst = (uint8_t *)(es + 1);
2612         if (MLX5_TXOFF_CONFIG(VLAN) && vlan) {
2613                 /* Implement VLAN tag insertion as part inline data. */
2614                 memcpy(pdst, psrc, 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t));
2615                 pdst += 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t);
2616                 psrc += 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t);
2617                 /* Insert VLAN ethertype + VLAN tag. */
2618                 *(unaligned_uint32_t *)pdst = rte_cpu_to_be_32
2619                                                 ((RTE_ETHER_TYPE_VLAN << 16) |
2620                                                  loc->mbuf->vlan_tci);
2621                 pdst += sizeof(struct rte_vlan_hdr);
2622                 /* Copy the rest two bytes from packet data. */
2623                 MLX5_ASSERT(pdst == RTE_PTR_ALIGN(pdst, sizeof(uint16_t)));
2624                 *(uint16_t *)pdst = *(unaligned_uint16_t *)psrc;
2625                 psrc += sizeof(uint16_t);
2626         } else {
2627                 /* Fill the gap in the title WQEBB with inline data. */
2628                 rte_mov16(pdst, psrc);
2629                 psrc += sizeof(rte_v128u32_t);
2630         }
2631         pdst = (uint8_t *)(es + 2);
2632         MLX5_ASSERT(inlen >= MLX5_ESEG_MIN_INLINE_SIZE);
2633         MLX5_ASSERT(pdst < (uint8_t *)txq->wqes_end);
2634         inlen -= MLX5_ESEG_MIN_INLINE_SIZE;
2635         if (!inlen) {
2636                 MLX5_ASSERT(pdst == RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE));
2637                 return (struct mlx5_wqe_dseg *)pdst;
2638         }
2639         /*
2640          * The WQEBB space availability is checked by caller.
2641          * Here we should be aware of WQE ring buffer wraparound only.
2642          */
2643         part = (uint8_t *)txq->wqes_end - pdst;
2644         part = RTE_MIN(part, inlen);
2645         do {
2646                 rte_memcpy(pdst, psrc, part);
2647                 inlen -= part;
2648                 if (likely(!inlen)) {
2649                         /*
2650                          * If return value is not used by the caller
2651                          * the code below will be optimized out.
2652                          */
2653                         pdst += part;
2654                         pdst = RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE);
2655                         if (unlikely(pdst >= (uint8_t *)txq->wqes_end))
2656                                 pdst = (uint8_t *)txq->wqes;
2657                         return (struct mlx5_wqe_dseg *)pdst;
2658                 }
2659                 pdst = (uint8_t *)txq->wqes;
2660                 psrc += part;
2661                 part = inlen;
2662         } while (true);
2663 }
2664
2665 /**
2666  * Copy data from chain of mbuf to the specified linear buffer.
2667  * Checksums and VLAN insertion Tx offload features. If data
2668  * from some mbuf copied completely this mbuf is freed. Local
2669  * structure is used to keep the byte stream state.
2670  *
2671  * @param pdst
2672  *   Pointer to the destination linear buffer.
2673  * @param loc
2674  *   Pointer to burst routine local context.
2675  * @param len
2676  *   Length of data to be copied.
2677  * @param must
2678  *   Length of data to be copied ignoring no inline hint.
2679  * @param olx
2680  *   Configured Tx offloads mask. It is fully defined at
2681  *   compile time and may be used for optimization.
2682  *
2683  * @return
2684  *   Number of actual copied data bytes. This is always greater than or
2685  *   equal to must parameter and might be lesser than len in no inline
2686  *   hint flag is encountered.
2687  */
2688 static __rte_always_inline unsigned int
2689 mlx5_tx_mseg_memcpy(uint8_t *pdst,
2690                     struct mlx5_txq_local *__rte_restrict loc,
2691                     unsigned int len,
2692                     unsigned int must,
2693                     unsigned int olx __rte_unused)
2694 {
2695         struct rte_mbuf *mbuf;
2696         unsigned int part, dlen, copy = 0;
2697         uint8_t *psrc;
2698
2699         MLX5_ASSERT(len);
2700         MLX5_ASSERT(must <= len);
2701         do {
2702                 /* Allow zero length packets, must check first. */
2703                 dlen = rte_pktmbuf_data_len(loc->mbuf);
2704                 if (dlen <= loc->mbuf_off) {
2705                         /* Exhausted packet, just free. */
2706                         mbuf = loc->mbuf;
2707                         loc->mbuf = mbuf->next;
2708                         rte_pktmbuf_free_seg(mbuf);
2709                         loc->mbuf_off = 0;
2710                         MLX5_ASSERT(loc->mbuf_nseg > 1);
2711                         MLX5_ASSERT(loc->mbuf);
2712                         --loc->mbuf_nseg;
2713                         if (loc->mbuf->ol_flags & PKT_TX_DYNF_NOINLINE) {
2714                                 unsigned int diff;
2715
2716                                 if (copy >= must) {
2717                                         /*
2718                                          * We already copied the minimal
2719                                          * requested amount of data.
2720                                          */
2721                                         return copy;
2722                                 }
2723                                 diff = must - copy;
2724                                 if (diff <= rte_pktmbuf_data_len(loc->mbuf)) {
2725                                         /*
2726                                          * Copy only the minimal required
2727                                          * part of the data buffer.
2728                                          */
2729                                         len = diff;
2730                                 }
2731                         }
2732                         continue;
2733                 }
2734                 dlen -= loc->mbuf_off;
2735                 psrc = rte_pktmbuf_mtod_offset(loc->mbuf, uint8_t *,
2736                                                loc->mbuf_off);
2737                 part = RTE_MIN(len, dlen);
2738                 rte_memcpy(pdst, psrc, part);
2739                 copy += part;
2740                 loc->mbuf_off += part;
2741                 len -= part;
2742                 if (!len) {
2743                         if (loc->mbuf_off >= rte_pktmbuf_data_len(loc->mbuf)) {
2744                                 loc->mbuf_off = 0;
2745                                 /* Exhausted packet, just free. */
2746                                 mbuf = loc->mbuf;
2747                                 loc->mbuf = mbuf->next;
2748                                 rte_pktmbuf_free_seg(mbuf);
2749                                 loc->mbuf_off = 0;
2750                                 MLX5_ASSERT(loc->mbuf_nseg >= 1);
2751                                 --loc->mbuf_nseg;
2752                         }
2753                         return copy;
2754                 }
2755                 pdst += part;
2756         } while (true);
2757 }
2758
2759 /**
2760  * Build the Ethernet Segment with inlined data from
2761  * multi-segment packet. Checks the boundary of WQEBB
2762  * and ring buffer wrapping, supports Software Parser,
2763  * Checksums and VLAN insertion Tx offload features.
2764  *
2765  * @param txq
2766  *   Pointer to TX queue structure.
2767  * @param loc
2768  *   Pointer to burst routine local context.
2769  * @param wqe
2770  *   Pointer to WQE to fill with built Ethernet Segment.
2771  * @param vlan
2772  *   Length of VLAN tag insertion if any.
2773  * @param inlen
2774  *   Length of data to inline (VLAN included, if any).
2775  * @param tso
2776  *   TSO flag, set mss field from the packet.
2777  * @param olx
2778  *   Configured Tx offloads mask. It is fully defined at
2779  *   compile time and may be used for optimization.
2780  *
2781  * @return
2782  *   Pointer to the next Data Segment (aligned and
2783  *   possible NOT wrapped around - caller should do
2784  *   wrapping check on its own).
2785  */
2786 static __rte_always_inline struct mlx5_wqe_dseg *
2787 mlx5_tx_eseg_mdat(struct mlx5_txq_data *__rte_restrict txq,
2788                   struct mlx5_txq_local *__rte_restrict loc,
2789                   struct mlx5_wqe *__rte_restrict wqe,
2790                   unsigned int vlan,
2791                   unsigned int inlen,
2792                   unsigned int tso,
2793                   unsigned int olx)
2794 {
2795         struct mlx5_wqe_eseg *__rte_restrict es = &wqe->eseg;
2796         uint32_t csum;
2797         uint8_t *pdst;
2798         unsigned int part, tlen = 0;
2799
2800         /*
2801          * Calculate and set check sum flags first, uint32_t field
2802          * in segment may be shared with Software Parser flags.
2803          */
2804         csum = MLX5_TXOFF_CONFIG(CSUM) ? txq_ol_cksum_to_cs(loc->mbuf) : 0;
2805         if (tso) {
2806                 csum <<= 24;
2807                 csum |= loc->mbuf->tso_segsz;
2808                 es->flags = rte_cpu_to_be_32(csum);
2809         } else {
2810                 es->flags = rte_cpu_to_le_32(csum);
2811         }
2812         /*
2813          * Calculate and set Software Parser offsets and flags.
2814          * These flags a set for custom UDP and IP tunnel packets.
2815          */
2816         es->swp_offs = txq_mbuf_to_swp(loc, &es->swp_flags, olx);
2817         /* Fill metadata field if needed. */
2818         es->metadata = MLX5_TXOFF_CONFIG(METADATA) ?
2819                        loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
2820                        *RTE_FLOW_DYNF_METADATA(loc->mbuf) : 0 : 0;
2821         static_assert(MLX5_ESEG_MIN_INLINE_SIZE ==
2822                                 (sizeof(uint16_t) +
2823                                  sizeof(rte_v128u32_t)),
2824                       "invalid Ethernet Segment data size");
2825         static_assert(MLX5_ESEG_MIN_INLINE_SIZE ==
2826                                 (sizeof(uint16_t) +
2827                                  sizeof(struct rte_vlan_hdr) +
2828                                  2 * RTE_ETHER_ADDR_LEN),
2829                       "invalid Ethernet Segment data size");
2830         MLX5_ASSERT(inlen >= MLX5_ESEG_MIN_INLINE_SIZE);
2831         pdst = (uint8_t *)&es->inline_data;
2832         if (MLX5_TXOFF_CONFIG(VLAN) && vlan) {
2833                 /* Implement VLAN tag insertion as part inline data. */
2834                 mlx5_tx_mseg_memcpy(pdst, loc,
2835                                     2 * RTE_ETHER_ADDR_LEN,
2836                                     2 * RTE_ETHER_ADDR_LEN, olx);
2837                 pdst += 2 * RTE_ETHER_ADDR_LEN;
2838                 *(unaligned_uint32_t *)pdst = rte_cpu_to_be_32
2839                                                 ((RTE_ETHER_TYPE_VLAN << 16) |
2840                                                  loc->mbuf->vlan_tci);
2841                 pdst += sizeof(struct rte_vlan_hdr);
2842                 tlen += 2 * RTE_ETHER_ADDR_LEN + sizeof(struct rte_vlan_hdr);
2843         }
2844         MLX5_ASSERT(pdst < (uint8_t *)txq->wqes_end);
2845         /*
2846          * The WQEBB space availability is checked by caller.
2847          * Here we should be aware of WQE ring buffer wraparound only.
2848          */
2849         part = (uint8_t *)txq->wqes_end - pdst;
2850         part = RTE_MIN(part, inlen - tlen);
2851         MLX5_ASSERT(part);
2852         do {
2853                 unsigned int copy;
2854
2855                 /*
2856                  * Copying may be interrupted inside the routine
2857                  * if run into no inline hint flag.
2858                  */
2859                 copy = tlen >= txq->inlen_mode ? 0 : (txq->inlen_mode - tlen);
2860                 copy = mlx5_tx_mseg_memcpy(pdst, loc, part, copy, olx);
2861                 tlen += copy;
2862                 if (likely(inlen <= tlen) || copy < part) {
2863                         es->inline_hdr_sz = rte_cpu_to_be_16(tlen);
2864                         pdst += copy;
2865                         pdst = RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE);
2866                         return (struct mlx5_wqe_dseg *)pdst;
2867                 }
2868                 pdst = (uint8_t *)txq->wqes;
2869                 part = inlen - tlen;
2870         } while (true);
2871 }
2872
2873 /**
2874  * Build the Data Segment of pointer type.
2875  *
2876  * @param txq
2877  *   Pointer to TX queue structure.
2878  * @param loc
2879  *   Pointer to burst routine local context.
2880  * @param dseg
2881  *   Pointer to WQE to fill with built Data Segment.
2882  * @param buf
2883  *   Data buffer to point.
2884  * @param len
2885  *   Data buffer length.
2886  * @param olx
2887  *   Configured Tx offloads mask. It is fully defined at
2888  *   compile time and may be used for optimization.
2889  */
2890 static __rte_always_inline void
2891 mlx5_tx_dseg_ptr(struct mlx5_txq_data *__rte_restrict txq,
2892                  struct mlx5_txq_local *__rte_restrict loc,
2893                  struct mlx5_wqe_dseg *__rte_restrict dseg,
2894                  uint8_t *buf,
2895                  unsigned int len,
2896                  unsigned int olx __rte_unused)
2897
2898 {
2899         MLX5_ASSERT(len);
2900         dseg->bcount = rte_cpu_to_be_32(len);
2901         dseg->lkey = mlx5_tx_mb2mr(txq, loc->mbuf);
2902         dseg->pbuf = rte_cpu_to_be_64((uintptr_t)buf);
2903 }
2904
2905 /**
2906  * Build the Data Segment of pointer type or inline
2907  * if data length is less than buffer in minimal
2908  * Data Segment size.
2909  *
2910  * @param txq
2911  *   Pointer to TX queue structure.
2912  * @param loc
2913  *   Pointer to burst routine local context.
2914  * @param dseg
2915  *   Pointer to WQE to fill with built Data Segment.
2916  * @param buf
2917  *   Data buffer to point.
2918  * @param len
2919  *   Data buffer length.
2920  * @param olx
2921  *   Configured Tx offloads mask. It is fully defined at
2922  *   compile time and may be used for optimization.
2923  */
2924 static __rte_always_inline void
2925 mlx5_tx_dseg_iptr(struct mlx5_txq_data *__rte_restrict txq,
2926                   struct mlx5_txq_local *__rte_restrict loc,
2927                   struct mlx5_wqe_dseg *__rte_restrict dseg,
2928                   uint8_t *buf,
2929                   unsigned int len,
2930                   unsigned int olx __rte_unused)
2931
2932 {
2933         uintptr_t dst, src;
2934
2935         MLX5_ASSERT(len);
2936         if (len > MLX5_DSEG_MIN_INLINE_SIZE) {
2937                 dseg->bcount = rte_cpu_to_be_32(len);
2938                 dseg->lkey = mlx5_tx_mb2mr(txq, loc->mbuf);
2939                 dseg->pbuf = rte_cpu_to_be_64((uintptr_t)buf);
2940
2941                 return;
2942         }
2943         dseg->bcount = rte_cpu_to_be_32(len | MLX5_ETH_WQE_DATA_INLINE);
2944         /* Unrolled implementation of generic rte_memcpy. */
2945         dst = (uintptr_t)&dseg->inline_data[0];
2946         src = (uintptr_t)buf;
2947         if (len & 0x08) {
2948 #ifdef RTE_ARCH_STRICT_ALIGN
2949                 MLX5_ASSERT(dst == RTE_PTR_ALIGN(dst, sizeof(uint32_t)));
2950                 *(uint32_t *)dst = *(unaligned_uint32_t *)src;
2951                 dst += sizeof(uint32_t);
2952                 src += sizeof(uint32_t);
2953                 *(uint32_t *)dst = *(unaligned_uint32_t *)src;
2954                 dst += sizeof(uint32_t);
2955                 src += sizeof(uint32_t);
2956 #else
2957                 *(uint64_t *)dst = *(unaligned_uint64_t *)src;
2958                 dst += sizeof(uint64_t);
2959                 src += sizeof(uint64_t);
2960 #endif
2961         }
2962         if (len & 0x04) {
2963                 *(uint32_t *)dst = *(unaligned_uint32_t *)src;
2964                 dst += sizeof(uint32_t);
2965                 src += sizeof(uint32_t);
2966         }
2967         if (len & 0x02) {
2968                 *(uint16_t *)dst = *(unaligned_uint16_t *)src;
2969                 dst += sizeof(uint16_t);
2970                 src += sizeof(uint16_t);
2971         }
2972         if (len & 0x01)
2973                 *(uint8_t *)dst = *(uint8_t *)src;
2974 }
2975
2976 /**
2977  * Build the Data Segment of inlined data from single
2978  * segment packet, no VLAN insertion.
2979  *
2980  * @param txq
2981  *   Pointer to TX queue structure.
2982  * @param loc
2983  *   Pointer to burst routine local context.
2984  * @param dseg
2985  *   Pointer to WQE to fill with built Data Segment.
2986  * @param buf
2987  *   Data buffer to point.
2988  * @param len
2989  *   Data buffer length.
2990  * @param olx
2991  *   Configured Tx offloads mask. It is fully defined at
2992  *   compile time and may be used for optimization.
2993  *
2994  * @return
2995  *   Pointer to the next Data Segment after inlined data.
2996  *   Ring buffer wraparound check is needed. We do not
2997  *   do it here because it may not be needed for the
2998  *   last packet in the eMPW session.
2999  */
3000 static __rte_always_inline struct mlx5_wqe_dseg *
3001 mlx5_tx_dseg_empw(struct mlx5_txq_data *__rte_restrict txq,
3002                   struct mlx5_txq_local *__rte_restrict loc __rte_unused,
3003                   struct mlx5_wqe_dseg *__rte_restrict dseg,
3004                   uint8_t *buf,
3005                   unsigned int len,
3006                   unsigned int olx __rte_unused)
3007 {
3008         unsigned int part;
3009         uint8_t *pdst;
3010
3011         if (!MLX5_TXOFF_CONFIG(MPW)) {
3012                 /* Store the descriptor byte counter for eMPW sessions. */
3013                 dseg->bcount = rte_cpu_to_be_32(len | MLX5_ETH_WQE_DATA_INLINE);
3014                 pdst = &dseg->inline_data[0];
3015         } else {
3016                 /* The entire legacy MPW session counter is stored on close. */
3017                 pdst = (uint8_t *)dseg;
3018         }
3019         /*
3020          * The WQEBB space availability is checked by caller.
3021          * Here we should be aware of WQE ring buffer wraparound only.
3022          */
3023         part = (uint8_t *)txq->wqes_end - pdst;
3024         part = RTE_MIN(part, len);
3025         do {
3026                 rte_memcpy(pdst, buf, part);
3027                 len -= part;
3028                 if (likely(!len)) {
3029                         pdst += part;
3030                         if (!MLX5_TXOFF_CONFIG(MPW))
3031                                 pdst = RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE);
3032                         /* Note: no final wraparound check here. */
3033                         return (struct mlx5_wqe_dseg *)pdst;
3034                 }
3035                 pdst = (uint8_t *)txq->wqes;
3036                 buf += part;
3037                 part = len;
3038         } while (true);
3039 }
3040
3041 /**
3042  * Build the Data Segment of inlined data from single
3043  * segment packet with VLAN insertion.
3044  *
3045  * @param txq
3046  *   Pointer to TX queue structure.
3047  * @param loc
3048  *   Pointer to burst routine local context.
3049  * @param dseg
3050  *   Pointer to the dseg fill with built Data Segment.
3051  * @param buf
3052  *   Data buffer to point.
3053  * @param len
3054  *   Data buffer length.
3055  * @param olx
3056  *   Configured Tx offloads mask. It is fully defined at
3057  *   compile time and may be used for optimization.
3058  *
3059  * @return
3060  *   Pointer to the next Data Segment after inlined data.
3061  *   Ring buffer wraparound check is needed.
3062  */
3063 static __rte_always_inline struct mlx5_wqe_dseg *
3064 mlx5_tx_dseg_vlan(struct mlx5_txq_data *__rte_restrict txq,
3065                   struct mlx5_txq_local *__rte_restrict loc __rte_unused,
3066                   struct mlx5_wqe_dseg *__rte_restrict dseg,
3067                   uint8_t *buf,
3068                   unsigned int len,
3069                   unsigned int olx __rte_unused)
3070
3071 {
3072         unsigned int part;
3073         uint8_t *pdst;
3074
3075         MLX5_ASSERT(len > MLX5_ESEG_MIN_INLINE_SIZE);
3076         static_assert(MLX5_DSEG_MIN_INLINE_SIZE ==
3077                                  (2 * RTE_ETHER_ADDR_LEN),
3078                       "invalid Data Segment data size");
3079         if (!MLX5_TXOFF_CONFIG(MPW)) {
3080                 /* Store the descriptor byte counter for eMPW sessions. */
3081                 dseg->bcount = rte_cpu_to_be_32
3082                                 ((len + sizeof(struct rte_vlan_hdr)) |
3083                                  MLX5_ETH_WQE_DATA_INLINE);
3084                 pdst = &dseg->inline_data[0];
3085         } else {
3086                 /* The entire legacy MPW session counter is stored on close. */
3087                 pdst = (uint8_t *)dseg;
3088         }
3089         memcpy(pdst, buf, MLX5_DSEG_MIN_INLINE_SIZE);
3090         buf += MLX5_DSEG_MIN_INLINE_SIZE;
3091         pdst += MLX5_DSEG_MIN_INLINE_SIZE;
3092         len -= MLX5_DSEG_MIN_INLINE_SIZE;
3093         /* Insert VLAN ethertype + VLAN tag. Pointer is aligned. */
3094         MLX5_ASSERT(pdst == RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE));
3095         if (unlikely(pdst >= (uint8_t *)txq->wqes_end))
3096                 pdst = (uint8_t *)txq->wqes;
3097         *(uint32_t *)pdst = rte_cpu_to_be_32((RTE_ETHER_TYPE_VLAN << 16) |
3098                                               loc->mbuf->vlan_tci);
3099         pdst += sizeof(struct rte_vlan_hdr);
3100         /*
3101          * The WQEBB space availability is checked by caller.
3102          * Here we should be aware of WQE ring buffer wraparound only.
3103          */
3104         part = (uint8_t *)txq->wqes_end - pdst;
3105         part = RTE_MIN(part, len);
3106         do {
3107                 rte_memcpy(pdst, buf, part);
3108                 len -= part;
3109                 if (likely(!len)) {
3110                         pdst += part;
3111                         if (!MLX5_TXOFF_CONFIG(MPW))
3112                                 pdst = RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE);
3113                         /* Note: no final wraparound check here. */
3114                         return (struct mlx5_wqe_dseg *)pdst;
3115                 }
3116                 pdst = (uint8_t *)txq->wqes;
3117                 buf += part;
3118                 part = len;
3119         } while (true);
3120 }
3121
3122 /**
3123  * Build the Ethernet Segment with optionally inlined data with
3124  * VLAN insertion and following Data Segments (if any) from
3125  * multi-segment packet. Used by ordinary send and TSO.
3126  *
3127  * @param txq
3128  *   Pointer to TX queue structure.
3129  * @param loc
3130  *   Pointer to burst routine local context.
3131  * @param wqe
3132  *   Pointer to WQE to fill with built Ethernet/Data Segments.
3133  * @param vlan
3134  *   Length of VLAN header to insert, 0 means no VLAN insertion.
3135  * @param inlen
3136  *   Data length to inline. For TSO this parameter specifies
3137  *   exact value, for ordinary send routine can be aligned by
3138  *   caller to provide better WQE space saving and data buffer
3139  *   start address alignment. This length includes VLAN header
3140  *   being inserted.
3141  * @param tso
3142  *   Zero means ordinary send, inlined data can be extended,
3143  *   otherwise this is TSO, inlined data length is fixed.
3144  * @param olx
3145  *   Configured Tx offloads mask. It is fully defined at
3146  *   compile time and may be used for optimization.
3147  *
3148  * @return
3149  *   Actual size of built WQE in segments.
3150  */
3151 static __rte_always_inline unsigned int
3152 mlx5_tx_mseg_build(struct mlx5_txq_data *__rte_restrict txq,
3153                    struct mlx5_txq_local *__rte_restrict loc,
3154                    struct mlx5_wqe *__rte_restrict wqe,
3155                    unsigned int vlan,
3156                    unsigned int inlen,
3157                    unsigned int tso,
3158                    unsigned int olx __rte_unused)
3159 {
3160         struct mlx5_wqe_dseg *__rte_restrict dseg;
3161         unsigned int ds;
3162
3163         MLX5_ASSERT((rte_pktmbuf_pkt_len(loc->mbuf) + vlan) >= inlen);
3164         loc->mbuf_nseg = NB_SEGS(loc->mbuf);
3165         loc->mbuf_off = 0;
3166
3167         dseg = mlx5_tx_eseg_mdat(txq, loc, wqe, vlan, inlen, tso, olx);
3168         if (!loc->mbuf_nseg)
3169                 goto dseg_done;
3170         /*
3171          * There are still some mbuf remaining, not inlined.
3172          * The first mbuf may be partially inlined and we
3173          * must process the possible non-zero data offset.
3174          */
3175         if (loc->mbuf_off) {
3176                 unsigned int dlen;
3177                 uint8_t *dptr;
3178
3179                 /*
3180                  * Exhausted packets must be dropped before.
3181                  * Non-zero offset means there are some data
3182                  * remained in the packet.
3183                  */
3184                 MLX5_ASSERT(loc->mbuf_off < rte_pktmbuf_data_len(loc->mbuf));
3185                 MLX5_ASSERT(rte_pktmbuf_data_len(loc->mbuf));
3186                 dptr = rte_pktmbuf_mtod_offset(loc->mbuf, uint8_t *,
3187                                                loc->mbuf_off);
3188                 dlen = rte_pktmbuf_data_len(loc->mbuf) - loc->mbuf_off;
3189                 /*
3190                  * Build the pointer/minimal data Data Segment.
3191                  * Do ring buffer wrapping check in advance.
3192                  */
3193                 if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
3194                         dseg = (struct mlx5_wqe_dseg *)txq->wqes;
3195                 mlx5_tx_dseg_iptr(txq, loc, dseg, dptr, dlen, olx);
3196                 /* Store the mbuf to be freed on completion. */
3197                 MLX5_ASSERT(loc->elts_free);
3198                 txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
3199                 --loc->elts_free;
3200                 ++dseg;
3201                 if (--loc->mbuf_nseg == 0)
3202                         goto dseg_done;
3203                 loc->mbuf = loc->mbuf->next;
3204                 loc->mbuf_off = 0;
3205         }
3206         do {
3207                 if (unlikely(!rte_pktmbuf_data_len(loc->mbuf))) {
3208                         struct rte_mbuf *mbuf;
3209
3210                         /* Zero length segment found, just skip. */
3211                         mbuf = loc->mbuf;
3212                         loc->mbuf = loc->mbuf->next;
3213                         rte_pktmbuf_free_seg(mbuf);
3214                         if (--loc->mbuf_nseg == 0)
3215                                 break;
3216                 } else {
3217                         if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
3218                                 dseg = (struct mlx5_wqe_dseg *)txq->wqes;
3219                         mlx5_tx_dseg_iptr
3220                                 (txq, loc, dseg,
3221                                  rte_pktmbuf_mtod(loc->mbuf, uint8_t *),
3222                                  rte_pktmbuf_data_len(loc->mbuf), olx);
3223                         MLX5_ASSERT(loc->elts_free);
3224                         txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
3225                         --loc->elts_free;
3226                         ++dseg;
3227                         if (--loc->mbuf_nseg == 0)
3228                                 break;
3229                         loc->mbuf = loc->mbuf->next;
3230                 }
3231         } while (true);
3232
3233 dseg_done:
3234         /* Calculate actual segments used from the dseg pointer. */
3235         if ((uintptr_t)wqe < (uintptr_t)dseg)
3236                 ds = ((uintptr_t)dseg - (uintptr_t)wqe) / MLX5_WSEG_SIZE;
3237         else
3238                 ds = (((uintptr_t)dseg - (uintptr_t)wqe) +
3239                       txq->wqe_s * MLX5_WQE_SIZE) / MLX5_WSEG_SIZE;
3240         return ds;
3241 }
3242
3243 /**
3244  * Tx one packet function for multi-segment TSO. Supports all
3245  * types of Tx offloads, uses MLX5_OPCODE_TSO to build WQEs,
3246  * sends one packet per WQE.
3247  *
3248  * This routine is responsible for storing processed mbuf
3249  * into elts ring buffer and update elts_head.
3250  *
3251  * @param txq
3252  *   Pointer to TX queue structure.
3253  * @param loc
3254  *   Pointer to burst routine local context.
3255  * @param olx
3256  *   Configured Tx offloads mask. It is fully defined at
3257  *   compile time and may be used for optimization.
3258  *
3259  * @return
3260  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
3261  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
3262  * Local context variables partially updated.
3263  */
3264 static __rte_always_inline enum mlx5_txcmp_code
3265 mlx5_tx_packet_multi_tso(struct mlx5_txq_data *__rte_restrict txq,
3266                         struct mlx5_txq_local *__rte_restrict loc,
3267                         unsigned int olx)
3268 {
3269         struct mlx5_wqe *__rte_restrict wqe;
3270         unsigned int ds, dlen, inlen, ntcp, vlan = 0;
3271
3272         /*
3273          * Calculate data length to be inlined to estimate
3274          * the required space in WQE ring buffer.
3275          */
3276         dlen = rte_pktmbuf_pkt_len(loc->mbuf);
3277         if (MLX5_TXOFF_CONFIG(VLAN) && loc->mbuf->ol_flags & PKT_TX_VLAN_PKT)
3278                 vlan = sizeof(struct rte_vlan_hdr);
3279         inlen = loc->mbuf->l2_len + vlan +
3280                 loc->mbuf->l3_len + loc->mbuf->l4_len;
3281         if (unlikely((!inlen || !loc->mbuf->tso_segsz)))
3282                 return MLX5_TXCMP_CODE_ERROR;
3283         if (loc->mbuf->ol_flags & PKT_TX_TUNNEL_MASK)
3284                 inlen += loc->mbuf->outer_l2_len + loc->mbuf->outer_l3_len;
3285         /* Packet must contain all TSO headers. */
3286         if (unlikely(inlen > MLX5_MAX_TSO_HEADER ||
3287                      inlen <= MLX5_ESEG_MIN_INLINE_SIZE ||
3288                      inlen > (dlen + vlan)))
3289                 return MLX5_TXCMP_CODE_ERROR;
3290         MLX5_ASSERT(inlen >= txq->inlen_mode);
3291         /*
3292          * Check whether there are enough free WQEBBs:
3293          * - Control Segment
3294          * - Ethernet Segment
3295          * - First Segment of inlined Ethernet data
3296          * - ... data continued ...
3297          * - Data Segments of pointer/min inline type
3298          */
3299         ds = NB_SEGS(loc->mbuf) + 2 + (inlen -
3300                                        MLX5_ESEG_MIN_INLINE_SIZE +
3301                                        MLX5_WSEG_SIZE +
3302                                        MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
3303         if (unlikely(loc->wqe_free < ((ds + 3) / 4)))
3304                 return MLX5_TXCMP_CODE_EXIT;
3305         /* Check for maximal WQE size. */
3306         if (unlikely((MLX5_WQE_SIZE_MAX / MLX5_WSEG_SIZE) < ((ds + 3) / 4)))
3307                 return MLX5_TXCMP_CODE_ERROR;
3308 #ifdef MLX5_PMD_SOFT_COUNTERS
3309         /* Update sent data bytes/packets counters. */
3310         ntcp = (dlen - (inlen - vlan) + loc->mbuf->tso_segsz - 1) /
3311                 loc->mbuf->tso_segsz;
3312         /*
3313          * One will be added for mbuf itself
3314          * at the end of the mlx5_tx_burst from
3315          * loc->pkts_sent field.
3316          */
3317         --ntcp;
3318         txq->stats.opackets += ntcp;
3319         txq->stats.obytes += dlen + vlan + ntcp * inlen;
3320 #endif
3321         wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
3322         loc->wqe_last = wqe;
3323         mlx5_tx_cseg_init(txq, loc, wqe, 0, MLX5_OPCODE_TSO, olx);
3324         ds = mlx5_tx_mseg_build(txq, loc, wqe, vlan, inlen, 1, olx);
3325         wqe->cseg.sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | ds);
3326         txq->wqe_ci += (ds + 3) / 4;
3327         loc->wqe_free -= (ds + 3) / 4;
3328         return MLX5_TXCMP_CODE_MULTI;
3329 }
3330
3331 /**
3332  * Tx one packet function for multi-segment SEND. Supports all
3333  * types of Tx offloads, uses MLX5_OPCODE_SEND to build WQEs,
3334  * sends one packet per WQE, without any data inlining in
3335  * Ethernet Segment.
3336  *
3337  * This routine is responsible for storing processed mbuf
3338  * into elts ring buffer and update elts_head.
3339  *
3340  * @param txq
3341  *   Pointer to TX queue structure.
3342  * @param loc
3343  *   Pointer to burst routine local context.
3344  * @param olx
3345  *   Configured Tx offloads mask. It is fully defined at
3346  *   compile time and may be used for optimization.
3347  *
3348  * @return
3349  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
3350  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
3351  * Local context variables partially updated.
3352  */
3353 static __rte_always_inline enum mlx5_txcmp_code
3354 mlx5_tx_packet_multi_send(struct mlx5_txq_data *__rte_restrict txq,
3355                           struct mlx5_txq_local *__rte_restrict loc,
3356                           unsigned int olx)
3357 {
3358         struct mlx5_wqe_dseg *__rte_restrict dseg;
3359         struct mlx5_wqe *__rte_restrict wqe;
3360         unsigned int ds, nseg;
3361
3362         MLX5_ASSERT(NB_SEGS(loc->mbuf) > 1);
3363         /*
3364          * No inline at all, it means the CPU cycles saving
3365          * is prioritized at configuration, we should not
3366          * copy any packet data to WQE.
3367          */
3368         nseg = NB_SEGS(loc->mbuf);
3369         ds = 2 + nseg;
3370         if (unlikely(loc->wqe_free < ((ds + 3) / 4)))
3371                 return MLX5_TXCMP_CODE_EXIT;
3372         /* Check for maximal WQE size. */
3373         if (unlikely((MLX5_WQE_SIZE_MAX / MLX5_WSEG_SIZE) < ((ds + 3) / 4)))
3374                 return MLX5_TXCMP_CODE_ERROR;
3375         /*
3376          * Some Tx offloads may cause an error if
3377          * packet is not long enough, check against
3378          * assumed minimal length.
3379          */
3380         if (rte_pktmbuf_pkt_len(loc->mbuf) <= MLX5_ESEG_MIN_INLINE_SIZE)
3381                 return MLX5_TXCMP_CODE_ERROR;
3382 #ifdef MLX5_PMD_SOFT_COUNTERS
3383         /* Update sent data bytes counter. */
3384         txq->stats.obytes += rte_pktmbuf_pkt_len(loc->mbuf);
3385         if (MLX5_TXOFF_CONFIG(VLAN) &&
3386             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT)
3387                 txq->stats.obytes += sizeof(struct rte_vlan_hdr);
3388 #endif
3389         /*
3390          * SEND WQE, one WQEBB:
3391          * - Control Segment, SEND opcode
3392          * - Ethernet Segment, optional VLAN, no inline
3393          * - Data Segments, pointer only type
3394          */
3395         wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
3396         loc->wqe_last = wqe;
3397         mlx5_tx_cseg_init(txq, loc, wqe, ds, MLX5_OPCODE_SEND, olx);
3398         mlx5_tx_eseg_none(txq, loc, wqe, olx);
3399         dseg = &wqe->dseg[0];
3400         do {
3401                 if (unlikely(!rte_pktmbuf_data_len(loc->mbuf))) {
3402                         struct rte_mbuf *mbuf;
3403
3404                         /*
3405                          * Zero length segment found, have to
3406                          * correct total size of WQE in segments.
3407                          * It is supposed to be rare occasion, so
3408                          * in normal case (no zero length segments)
3409                          * we avoid extra writing to the Control
3410                          * Segment.
3411                          */
3412                         --ds;
3413                         wqe->cseg.sq_ds -= RTE_BE32(1);
3414                         mbuf = loc->mbuf;
3415                         loc->mbuf = mbuf->next;
3416                         rte_pktmbuf_free_seg(mbuf);
3417                         if (--nseg == 0)
3418                                 break;
3419                 } else {
3420                         mlx5_tx_dseg_ptr
3421                                 (txq, loc, dseg,
3422                                  rte_pktmbuf_mtod(loc->mbuf, uint8_t *),
3423                                  rte_pktmbuf_data_len(loc->mbuf), olx);
3424                         txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
3425                         --loc->elts_free;
3426                         if (--nseg == 0)
3427                                 break;
3428                         ++dseg;
3429                         if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
3430                                 dseg = (struct mlx5_wqe_dseg *)txq->wqes;
3431                         loc->mbuf = loc->mbuf->next;
3432                 }
3433         } while (true);
3434         txq->wqe_ci += (ds + 3) / 4;
3435         loc->wqe_free -= (ds + 3) / 4;
3436         return MLX5_TXCMP_CODE_MULTI;
3437 }
3438
3439 /**
3440  * Tx one packet function for multi-segment SEND. Supports all
3441  * types of Tx offloads, uses MLX5_OPCODE_SEND to build WQEs,
3442  * sends one packet per WQE, with data inlining in
3443  * Ethernet Segment and minimal Data Segments.
3444  *
3445  * This routine is responsible for storing processed mbuf
3446  * into elts ring buffer and update elts_head.
3447  *
3448  * @param txq
3449  *   Pointer to TX queue structure.
3450  * @param loc
3451  *   Pointer to burst routine local context.
3452  * @param olx
3453  *   Configured Tx offloads mask. It is fully defined at
3454  *   compile time and may be used for optimization.
3455  *
3456  * @return
3457  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
3458  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
3459  * Local context variables partially updated.
3460  */
3461 static __rte_always_inline enum mlx5_txcmp_code
3462 mlx5_tx_packet_multi_inline(struct mlx5_txq_data *__rte_restrict txq,
3463                             struct mlx5_txq_local *__rte_restrict loc,
3464                             unsigned int olx)
3465 {
3466         struct mlx5_wqe *__rte_restrict wqe;
3467         unsigned int ds, inlen, dlen, vlan = 0;
3468
3469         MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
3470         MLX5_ASSERT(NB_SEGS(loc->mbuf) > 1);
3471         /*
3472          * First calculate data length to be inlined
3473          * to estimate the required space for WQE.
3474          */
3475         dlen = rte_pktmbuf_pkt_len(loc->mbuf);
3476         if (MLX5_TXOFF_CONFIG(VLAN) && loc->mbuf->ol_flags & PKT_TX_VLAN_PKT)
3477                 vlan = sizeof(struct rte_vlan_hdr);
3478         inlen = dlen + vlan;
3479         /* Check against minimal length. */
3480         if (inlen <= MLX5_ESEG_MIN_INLINE_SIZE)
3481                 return MLX5_TXCMP_CODE_ERROR;
3482         MLX5_ASSERT(txq->inlen_send >= MLX5_ESEG_MIN_INLINE_SIZE);
3483         if (inlen > txq->inlen_send ||
3484             loc->mbuf->ol_flags & PKT_TX_DYNF_NOINLINE) {
3485                 struct rte_mbuf *mbuf;
3486                 unsigned int nxlen;
3487                 uintptr_t start;
3488
3489                 /*
3490                  * Packet length exceeds the allowed inline
3491                  * data length, check whether the minimal
3492                  * inlining is required.
3493                  */
3494                 if (txq->inlen_mode) {
3495                         MLX5_ASSERT(txq->inlen_mode >=
3496                                     MLX5_ESEG_MIN_INLINE_SIZE);
3497                         MLX5_ASSERT(txq->inlen_mode <= txq->inlen_send);
3498                         inlen = txq->inlen_mode;
3499                 } else {
3500                         if (loc->mbuf->ol_flags & PKT_TX_DYNF_NOINLINE ||
3501                             !vlan || txq->vlan_en) {
3502                                 /*
3503                                  * VLAN insertion will be done inside by HW.
3504                                  * It is not utmost effective - VLAN flag is
3505                                  * checked twice, but we should proceed the
3506                                  * inlining length correctly and take into
3507                                  * account the VLAN header being inserted.
3508                                  */
3509                                 return mlx5_tx_packet_multi_send
3510                                                         (txq, loc, olx);
3511                         }
3512                         inlen = MLX5_ESEG_MIN_INLINE_SIZE;
3513                 }
3514                 /*
3515                  * Now we know the minimal amount of data is requested
3516                  * to inline. Check whether we should inline the buffers
3517                  * from the chain beginning to eliminate some mbufs.
3518                  */
3519                 mbuf = loc->mbuf;
3520                 nxlen = rte_pktmbuf_data_len(mbuf);
3521                 if (unlikely(nxlen <= txq->inlen_send)) {
3522                         /* We can inline first mbuf at least. */
3523                         if (nxlen < inlen) {
3524                                 unsigned int smlen;
3525
3526                                 /* Scan mbufs till inlen filled. */
3527                                 do {
3528                                         smlen = nxlen;
3529                                         mbuf = NEXT(mbuf);
3530                                         MLX5_ASSERT(mbuf);
3531                                         nxlen = rte_pktmbuf_data_len(mbuf);
3532                                         nxlen += smlen;
3533                                 } while (unlikely(nxlen < inlen));
3534                                 if (unlikely(nxlen > txq->inlen_send)) {
3535                                         /* We cannot inline entire mbuf. */
3536                                         smlen = inlen - smlen;
3537                                         start = rte_pktmbuf_mtod_offset
3538                                                     (mbuf, uintptr_t, smlen);
3539                                         goto do_align;
3540                                 }
3541                         }
3542                         do {
3543                                 inlen = nxlen;
3544                                 mbuf = NEXT(mbuf);
3545                                 /* There should be not end of packet. */
3546                                 MLX5_ASSERT(mbuf);
3547                                 nxlen = inlen + rte_pktmbuf_data_len(mbuf);
3548                         } while (unlikely(nxlen < txq->inlen_send));
3549                 }
3550                 start = rte_pktmbuf_mtod(mbuf, uintptr_t);
3551                 /*
3552                  * Check whether we can do inline to align start
3553                  * address of data buffer to cacheline.
3554                  */
3555 do_align:
3556                 start = (~start + 1) & (RTE_CACHE_LINE_SIZE - 1);
3557                 if (unlikely(start)) {
3558                         start += inlen;
3559                         if (start <= txq->inlen_send)
3560                                 inlen = start;
3561                 }
3562         }
3563         /*
3564          * Check whether there are enough free WQEBBs:
3565          * - Control Segment
3566          * - Ethernet Segment
3567          * - First Segment of inlined Ethernet data
3568          * - ... data continued ...
3569          * - Data Segments of pointer/min inline type
3570          *
3571          * Estimate the number of Data Segments conservatively,
3572          * supposing no any mbufs is being freed during inlining.
3573          */
3574         MLX5_ASSERT(inlen <= txq->inlen_send);
3575         ds = NB_SEGS(loc->mbuf) + 2 + (inlen -
3576                                        MLX5_ESEG_MIN_INLINE_SIZE +
3577                                        MLX5_WSEG_SIZE +
3578                                        MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
3579         if (unlikely(loc->wqe_free < ((ds + 3) / 4)))
3580                 return MLX5_TXCMP_CODE_EXIT;
3581         /* Check for maximal WQE size. */
3582         if (unlikely((MLX5_WQE_SIZE_MAX / MLX5_WSEG_SIZE) < ((ds + 3) / 4)))
3583                 return MLX5_TXCMP_CODE_ERROR;
3584 #ifdef MLX5_PMD_SOFT_COUNTERS
3585         /* Update sent data bytes/packets counters. */
3586         txq->stats.obytes += dlen + vlan;
3587 #endif
3588         wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
3589         loc->wqe_last = wqe;
3590         mlx5_tx_cseg_init(txq, loc, wqe, 0, MLX5_OPCODE_SEND, olx);
3591         ds = mlx5_tx_mseg_build(txq, loc, wqe, vlan, inlen, 0, olx);
3592         wqe->cseg.sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | ds);
3593         txq->wqe_ci += (ds + 3) / 4;
3594         loc->wqe_free -= (ds + 3) / 4;
3595         return MLX5_TXCMP_CODE_MULTI;
3596 }
3597
3598 /**
3599  * Tx burst function for multi-segment packets. Supports all
3600  * types of Tx offloads, uses MLX5_OPCODE_SEND/TSO to build WQEs,
3601  * sends one packet per WQE. Function stops sending if it
3602  * encounters the single-segment packet.
3603  *
3604  * This routine is responsible for storing processed mbuf
3605  * into elts ring buffer and update elts_head.
3606  *
3607  * @param txq
3608  *   Pointer to TX queue structure.
3609  * @param[in] pkts
3610  *   Packets to transmit.
3611  * @param pkts_n
3612  *   Number of packets in array.
3613  * @param loc
3614  *   Pointer to burst routine local context.
3615  * @param olx
3616  *   Configured Tx offloads mask. It is fully defined at
3617  *   compile time and may be used for optimization.
3618  *
3619  * @return
3620  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
3621  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
3622  *   MLX5_TXCMP_CODE_SINGLE - single-segment packet encountered.
3623  *   MLX5_TXCMP_CODE_TSO - TSO single-segment packet encountered.
3624  * Local context variables updated.
3625  */
3626 static __rte_always_inline enum mlx5_txcmp_code
3627 mlx5_tx_burst_mseg(struct mlx5_txq_data *__rte_restrict txq,
3628                    struct rte_mbuf **__rte_restrict pkts,
3629                    unsigned int pkts_n,
3630                    struct mlx5_txq_local *__rte_restrict loc,
3631                    unsigned int olx)
3632 {
3633         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
3634         MLX5_ASSERT(pkts_n > loc->pkts_sent);
3635         pkts += loc->pkts_sent + 1;
3636         pkts_n -= loc->pkts_sent;
3637         for (;;) {
3638                 enum mlx5_txcmp_code ret;
3639
3640                 MLX5_ASSERT(NB_SEGS(loc->mbuf) > 1);
3641                 /*
3642                  * Estimate the number of free elts quickly but
3643                  * conservatively. Some segment may be fully inlined
3644                  * and freed, ignore this here - precise estimation
3645                  * is costly.
3646                  */
3647                 if (loc->elts_free < NB_SEGS(loc->mbuf))
3648                         return MLX5_TXCMP_CODE_EXIT;
3649                 if (MLX5_TXOFF_CONFIG(TSO) &&
3650                     unlikely(loc->mbuf->ol_flags & PKT_TX_TCP_SEG)) {
3651                         /* Proceed with multi-segment TSO. */
3652                         ret = mlx5_tx_packet_multi_tso(txq, loc, olx);
3653                 } else if (MLX5_TXOFF_CONFIG(INLINE)) {
3654                         /* Proceed with multi-segment SEND with inlining. */
3655                         ret = mlx5_tx_packet_multi_inline(txq, loc, olx);
3656                 } else {
3657                         /* Proceed with multi-segment SEND w/o inlining. */
3658                         ret = mlx5_tx_packet_multi_send(txq, loc, olx);
3659                 }
3660                 if (ret == MLX5_TXCMP_CODE_EXIT)
3661                         return MLX5_TXCMP_CODE_EXIT;
3662                 if (ret == MLX5_TXCMP_CODE_ERROR)
3663                         return MLX5_TXCMP_CODE_ERROR;
3664                 /* WQE is built, go to the next packet. */
3665                 ++loc->pkts_sent;
3666                 --pkts_n;
3667                 if (unlikely(!pkts_n || !loc->elts_free || !loc->wqe_free))
3668                         return MLX5_TXCMP_CODE_EXIT;
3669                 loc->mbuf = *pkts++;
3670                 if (pkts_n > 1)
3671                         rte_prefetch0(*pkts);
3672                 if (likely(NB_SEGS(loc->mbuf) > 1))
3673                         continue;
3674                 /* Here ends the series of multi-segment packets. */
3675                 if (MLX5_TXOFF_CONFIG(TSO) &&
3676                     unlikely(loc->mbuf->ol_flags & PKT_TX_TCP_SEG))
3677                         return MLX5_TXCMP_CODE_TSO;
3678                 return MLX5_TXCMP_CODE_SINGLE;
3679         }
3680         MLX5_ASSERT(false);
3681 }
3682
3683 /**
3684  * Tx burst function for single-segment packets with TSO.
3685  * Supports all types of Tx offloads, except multi-packets.
3686  * Uses MLX5_OPCODE_TSO to build WQEs, sends one packet per WQE.
3687  * Function stops sending if it encounters the multi-segment
3688  * packet or packet without TSO requested.
3689  *
3690  * The routine is responsible for storing processed mbuf
3691  * into elts ring buffer and update elts_head if inline
3692  * offloads is requested due to possible early freeing
3693  * of the inlined mbufs (can not store pkts array in elts
3694  * as a batch).
3695  *
3696  * @param txq
3697  *   Pointer to TX queue structure.
3698  * @param[in] pkts
3699  *   Packets to transmit.
3700  * @param pkts_n
3701  *   Number of packets in array.
3702  * @param loc
3703  *   Pointer to burst routine local context.
3704  * @param olx
3705  *   Configured Tx offloads mask. It is fully defined at
3706  *   compile time and may be used for optimization.
3707  *
3708  * @return
3709  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
3710  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
3711  *   MLX5_TXCMP_CODE_SINGLE - single-segment packet encountered.
3712  *   MLX5_TXCMP_CODE_MULTI - multi-segment packet encountered.
3713  * Local context variables updated.
3714  */
3715 static __rte_always_inline enum mlx5_txcmp_code
3716 mlx5_tx_burst_tso(struct mlx5_txq_data *__rte_restrict txq,
3717                   struct rte_mbuf **__rte_restrict pkts,
3718                   unsigned int pkts_n,
3719                   struct mlx5_txq_local *__rte_restrict loc,
3720                   unsigned int olx)
3721 {
3722         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
3723         MLX5_ASSERT(pkts_n > loc->pkts_sent);
3724         pkts += loc->pkts_sent + 1;
3725         pkts_n -= loc->pkts_sent;
3726         for (;;) {
3727                 struct mlx5_wqe_dseg *__rte_restrict dseg;
3728                 struct mlx5_wqe *__rte_restrict wqe;
3729                 unsigned int ds, dlen, hlen, ntcp, vlan = 0;
3730                 uint8_t *dptr;
3731
3732                 MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
3733                 dlen = rte_pktmbuf_data_len(loc->mbuf);
3734                 if (MLX5_TXOFF_CONFIG(VLAN) &&
3735                     loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
3736                         vlan = sizeof(struct rte_vlan_hdr);
3737                 }
3738                 /*
3739                  * First calculate the WQE size to check
3740                  * whether we have enough space in ring buffer.
3741                  */
3742                 hlen = loc->mbuf->l2_len + vlan +
3743                        loc->mbuf->l3_len + loc->mbuf->l4_len;
3744                 if (unlikely((!hlen || !loc->mbuf->tso_segsz)))
3745                         return MLX5_TXCMP_CODE_ERROR;
3746                 if (loc->mbuf->ol_flags & PKT_TX_TUNNEL_MASK)
3747                         hlen += loc->mbuf->outer_l2_len +
3748                                 loc->mbuf->outer_l3_len;
3749                 /* Segment must contain all TSO headers. */
3750                 if (unlikely(hlen > MLX5_MAX_TSO_HEADER ||
3751                              hlen <= MLX5_ESEG_MIN_INLINE_SIZE ||
3752                              hlen > (dlen + vlan)))
3753                         return MLX5_TXCMP_CODE_ERROR;
3754                 /*
3755                  * Check whether there are enough free WQEBBs:
3756                  * - Control Segment
3757                  * - Ethernet Segment
3758                  * - First Segment of inlined Ethernet data
3759                  * - ... data continued ...
3760                  * - Finishing Data Segment of pointer type
3761                  */
3762                 ds = 4 + (hlen - MLX5_ESEG_MIN_INLINE_SIZE +
3763                           MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
3764                 if (loc->wqe_free < ((ds + 3) / 4))
3765                         return MLX5_TXCMP_CODE_EXIT;
3766 #ifdef MLX5_PMD_SOFT_COUNTERS
3767                 /* Update sent data bytes/packets counters. */
3768                 ntcp = (dlen + vlan - hlen +
3769                         loc->mbuf->tso_segsz - 1) /
3770                         loc->mbuf->tso_segsz;
3771                 /*
3772                  * One will be added for mbuf itself at the end
3773                  * of the mlx5_tx_burst from loc->pkts_sent field.
3774                  */
3775                 --ntcp;
3776                 txq->stats.opackets += ntcp;
3777                 txq->stats.obytes += dlen + vlan + ntcp * hlen;
3778 #endif
3779                 /*
3780                  * Build the TSO WQE:
3781                  * - Control Segment
3782                  * - Ethernet Segment with hlen bytes inlined
3783                  * - Data Segment of pointer type
3784                  */
3785                 wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
3786                 loc->wqe_last = wqe;
3787                 mlx5_tx_cseg_init(txq, loc, wqe, ds,
3788                                   MLX5_OPCODE_TSO, olx);
3789                 dseg = mlx5_tx_eseg_data(txq, loc, wqe, vlan, hlen, 1, olx);
3790                 dptr = rte_pktmbuf_mtod(loc->mbuf, uint8_t *) + hlen - vlan;
3791                 dlen -= hlen - vlan;
3792                 mlx5_tx_dseg_ptr(txq, loc, dseg, dptr, dlen, olx);
3793                 /*
3794                  * WQE is built, update the loop parameters
3795                  * and go to the next packet.
3796                  */
3797                 txq->wqe_ci += (ds + 3) / 4;
3798                 loc->wqe_free -= (ds + 3) / 4;
3799                 if (MLX5_TXOFF_CONFIG(INLINE))
3800                         txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
3801                 --loc->elts_free;
3802                 ++loc->pkts_sent;
3803                 --pkts_n;
3804                 if (unlikely(!pkts_n || !loc->elts_free || !loc->wqe_free))
3805                         return MLX5_TXCMP_CODE_EXIT;
3806                 loc->mbuf = *pkts++;
3807                 if (pkts_n > 1)
3808                         rte_prefetch0(*pkts);
3809                 if (MLX5_TXOFF_CONFIG(MULTI) &&
3810                     unlikely(NB_SEGS(loc->mbuf) > 1))
3811                         return MLX5_TXCMP_CODE_MULTI;
3812                 if (likely(!(loc->mbuf->ol_flags & PKT_TX_TCP_SEG)))
3813                         return MLX5_TXCMP_CODE_SINGLE;
3814                 /* Continue with the next TSO packet. */
3815         }
3816         MLX5_ASSERT(false);
3817 }
3818
3819 /**
3820  * Analyze the packet and select the best method to send.
3821  *
3822  * @param txq
3823  *   Pointer to TX queue structure.
3824  * @param loc
3825  *   Pointer to burst routine local context.
3826  * @param olx
3827  *   Configured Tx offloads mask. It is fully defined at
3828  *   compile time and may be used for optimization.
3829  * @param newp
3830  *   The predefined flag whether do complete check for
3831  *   multi-segment packets and TSO.
3832  *
3833  * @return
3834  *  MLX5_TXCMP_CODE_MULTI - multi-segment packet encountered.
3835  *  MLX5_TXCMP_CODE_TSO - TSO required, use TSO/LSO.
3836  *  MLX5_TXCMP_CODE_SINGLE - single-segment packet, use SEND.
3837  *  MLX5_TXCMP_CODE_EMPW - single-segment packet, use MPW.
3838  */
3839 static __rte_always_inline enum mlx5_txcmp_code
3840 mlx5_tx_able_to_empw(struct mlx5_txq_data *__rte_restrict txq,
3841                      struct mlx5_txq_local *__rte_restrict loc,
3842                      unsigned int olx,
3843                      bool newp)
3844 {
3845         /* Check for multi-segment packet. */
3846         if (newp &&
3847             MLX5_TXOFF_CONFIG(MULTI) &&
3848             unlikely(NB_SEGS(loc->mbuf) > 1))
3849                 return MLX5_TXCMP_CODE_MULTI;
3850         /* Check for TSO packet. */
3851         if (newp &&
3852             MLX5_TXOFF_CONFIG(TSO) &&
3853             unlikely(loc->mbuf->ol_flags & PKT_TX_TCP_SEG))
3854                 return MLX5_TXCMP_CODE_TSO;
3855         /* Check if eMPW is enabled at all. */
3856         if (!MLX5_TXOFF_CONFIG(EMPW))
3857                 return MLX5_TXCMP_CODE_SINGLE;
3858         /* Check if eMPW can be engaged. */
3859         if (MLX5_TXOFF_CONFIG(VLAN) &&
3860             unlikely(loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) &&
3861                 (!MLX5_TXOFF_CONFIG(INLINE) ||
3862                  unlikely((rte_pktmbuf_data_len(loc->mbuf) +
3863                            sizeof(struct rte_vlan_hdr)) > txq->inlen_empw))) {
3864                 /*
3865                  * eMPW does not support VLAN insertion offload,
3866                  * we have to inline the entire packet but
3867                  * packet is too long for inlining.
3868                  */
3869                 return MLX5_TXCMP_CODE_SINGLE;
3870         }
3871         return MLX5_TXCMP_CODE_EMPW;
3872 }
3873
3874 /**
3875  * Check the next packet attributes to match with the eMPW batch ones.
3876  * In addition, for legacy MPW the packet length is checked either.
3877  *
3878  * @param txq
3879  *   Pointer to TX queue structure.
3880  * @param es
3881  *   Pointer to Ethernet Segment of eMPW batch.
3882  * @param loc
3883  *   Pointer to burst routine local context.
3884  * @param dlen
3885  *   Length of previous packet in MPW descriptor.
3886  * @param olx
3887  *   Configured Tx offloads mask. It is fully defined at
3888  *   compile time and may be used for optimization.
3889  *
3890  * @return
3891  *  true - packet match with eMPW batch attributes.
3892  *  false - no match, eMPW should be restarted.
3893  */
3894 static __rte_always_inline bool
3895 mlx5_tx_match_empw(struct mlx5_txq_data *__rte_restrict txq __rte_unused,
3896                    struct mlx5_wqe_eseg *__rte_restrict es,
3897                    struct mlx5_txq_local *__rte_restrict loc,
3898                    uint32_t dlen,
3899                    unsigned int olx)
3900 {
3901         uint8_t swp_flags = 0;
3902
3903         /* Compare the checksum flags, if any. */
3904         if (MLX5_TXOFF_CONFIG(CSUM) &&
3905             txq_ol_cksum_to_cs(loc->mbuf) != es->cs_flags)
3906                 return false;
3907         /* Compare the Software Parser offsets and flags. */
3908         if (MLX5_TXOFF_CONFIG(SWP) &&
3909             (es->swp_offs != txq_mbuf_to_swp(loc, &swp_flags, olx) ||
3910              es->swp_flags != swp_flags))
3911                 return false;
3912         /* Fill metadata field if needed. */
3913         if (MLX5_TXOFF_CONFIG(METADATA) &&
3914                 es->metadata != (loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
3915                                  *RTE_FLOW_DYNF_METADATA(loc->mbuf) : 0))
3916                 return false;
3917         /* Legacy MPW can send packets with the same lengt only. */
3918         if (MLX5_TXOFF_CONFIG(MPW) &&
3919             dlen != rte_pktmbuf_data_len(loc->mbuf))
3920                 return false;
3921         /* There must be no VLAN packets in eMPW loop. */
3922         if (MLX5_TXOFF_CONFIG(VLAN))
3923                 MLX5_ASSERT(!(loc->mbuf->ol_flags & PKT_TX_VLAN_PKT));
3924         return true;
3925 }
3926
3927 /*
3928  * Update send loop variables and WQE for eMPW loop
3929  * without data inlining. Number of Data Segments is
3930  * equal to the number of sent packets.
3931  *
3932  * @param txq
3933  *   Pointer to TX queue structure.
3934  * @param loc
3935  *   Pointer to burst routine local context.
3936  * @param ds
3937  *   Number of packets/Data Segments/Packets.
3938  * @param slen
3939  *   Accumulated statistics, bytes sent
3940  * @param olx
3941  *   Configured Tx offloads mask. It is fully defined at
3942  *   compile time and may be used for optimization.
3943  *
3944  * @return
3945  *  true - packet match with eMPW batch attributes.
3946  *  false - no match, eMPW should be restarted.
3947  */
3948 static __rte_always_inline void
3949 mlx5_tx_sdone_empw(struct mlx5_txq_data *__rte_restrict txq,
3950                    struct mlx5_txq_local *__rte_restrict loc,
3951                    unsigned int ds,
3952                    unsigned int slen,
3953                    unsigned int olx __rte_unused)
3954 {
3955         MLX5_ASSERT(!MLX5_TXOFF_CONFIG(INLINE));
3956 #ifdef MLX5_PMD_SOFT_COUNTERS
3957         /* Update sent data bytes counter. */
3958          txq->stats.obytes += slen;
3959 #else
3960         (void)slen;
3961 #endif
3962         loc->elts_free -= ds;
3963         loc->pkts_sent += ds;
3964         ds += 2;
3965         loc->wqe_last->cseg.sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | ds);
3966         txq->wqe_ci += (ds + 3) / 4;
3967         loc->wqe_free -= (ds + 3) / 4;
3968 }
3969
3970 /*
3971  * Update send loop variables and WQE for eMPW loop
3972  * with data inlining. Gets the size of pushed descriptors
3973  * and data to the WQE.
3974  *
3975  * @param txq
3976  *   Pointer to TX queue structure.
3977  * @param loc
3978  *   Pointer to burst routine local context.
3979  * @param len
3980  *   Total size of descriptor/data in bytes.
3981  * @param slen
3982  *   Accumulated statistics, data bytes sent.
3983  * @param wqem
3984  *   The base WQE for the eMPW/MPW descriptor.
3985  * @param olx
3986  *   Configured Tx offloads mask. It is fully defined at
3987  *   compile time and may be used for optimization.
3988  *
3989  * @return
3990  *  true - packet match with eMPW batch attributes.
3991  *  false - no match, eMPW should be restarted.
3992  */
3993 static __rte_always_inline void
3994 mlx5_tx_idone_empw(struct mlx5_txq_data *__rte_restrict txq,
3995                    struct mlx5_txq_local *__rte_restrict loc,
3996                    unsigned int len,
3997                    unsigned int slen,
3998                    struct mlx5_wqe *__rte_restrict wqem,
3999                    unsigned int olx __rte_unused)
4000 {
4001         struct mlx5_wqe_dseg *dseg = &wqem->dseg[0];
4002
4003         MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
4004 #ifdef MLX5_PMD_SOFT_COUNTERS
4005         /* Update sent data bytes counter. */
4006          txq->stats.obytes += slen;
4007 #else
4008         (void)slen;
4009 #endif
4010         if (MLX5_TXOFF_CONFIG(MPW) && dseg->bcount == RTE_BE32(0)) {
4011                 /*
4012                  * If the legacy MPW session contains the inline packets
4013                  * we should set the only inline data segment length
4014                  * and align the total length to the segment size.
4015                  */
4016                 MLX5_ASSERT(len > sizeof(dseg->bcount));
4017                 dseg->bcount = rte_cpu_to_be_32((len - sizeof(dseg->bcount)) |
4018                                                 MLX5_ETH_WQE_DATA_INLINE);
4019                 len = (len + MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE + 2;
4020         } else {
4021                 /*
4022                  * The session is not legacy MPW or contains the
4023                  * data buffer pointer segments.
4024                  */
4025                 MLX5_ASSERT((len % MLX5_WSEG_SIZE) == 0);
4026                 len = len / MLX5_WSEG_SIZE + 2;
4027         }
4028         wqem->cseg.sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | len);
4029         txq->wqe_ci += (len + 3) / 4;
4030         loc->wqe_free -= (len + 3) / 4;
4031         loc->wqe_last = wqem;
4032 }
4033
4034 /**
4035  * The set of Tx burst functions for single-segment packets
4036  * without TSO and with Multi-Packet Writing feature support.
4037  * Supports all types of Tx offloads, except multi-packets
4038  * and TSO.
4039  *
4040  * Uses MLX5_OPCODE_EMPW to build WQEs if possible and sends
4041  * as many packet per WQE as it can. If eMPW is not configured
4042  * or packet can not be sent with eMPW (VLAN insertion) the
4043  * ordinary SEND opcode is used and only one packet placed
4044  * in WQE.
4045  *
4046  * Functions stop sending if it encounters the multi-segment
4047  * packet or packet with TSO requested.
4048  *
4049  * The routines are responsible for storing processed mbuf
4050  * into elts ring buffer and update elts_head if inlining
4051  * offload is requested. Otherwise the copying mbufs to elts
4052  * can be postponed and completed at the end of burst routine.
4053  *
4054  * @param txq
4055  *   Pointer to TX queue structure.
4056  * @param[in] pkts
4057  *   Packets to transmit.
4058  * @param pkts_n
4059  *   Number of packets in array.
4060  * @param loc
4061  *   Pointer to burst routine local context.
4062  * @param olx
4063  *   Configured Tx offloads mask. It is fully defined at
4064  *   compile time and may be used for optimization.
4065  *
4066  * @return
4067  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
4068  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
4069  *   MLX5_TXCMP_CODE_MULTI - multi-segment packet encountered.
4070  *   MLX5_TXCMP_CODE_TSO - TSO packet encountered.
4071  *   MLX5_TXCMP_CODE_SINGLE - used inside functions set.
4072  *   MLX5_TXCMP_CODE_EMPW - used inside functions set.
4073  *
4074  * Local context variables updated.
4075  *
4076  *
4077  * The routine sends packets with MLX5_OPCODE_EMPW
4078  * without inlining, this is dedicated optimized branch.
4079  * No VLAN insertion is supported.
4080  */
4081 static __rte_always_inline enum mlx5_txcmp_code
4082 mlx5_tx_burst_empw_simple(struct mlx5_txq_data *__rte_restrict txq,
4083                           struct rte_mbuf **__rte_restrict pkts,
4084                           unsigned int pkts_n,
4085                           struct mlx5_txq_local *__rte_restrict loc,
4086                           unsigned int olx)
4087 {
4088         /*
4089          * Subroutine is the part of mlx5_tx_burst_single()
4090          * and sends single-segment packet with eMPW opcode
4091          * without data inlining.
4092          */
4093         MLX5_ASSERT(!MLX5_TXOFF_CONFIG(INLINE));
4094         MLX5_ASSERT(MLX5_TXOFF_CONFIG(EMPW));
4095         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
4096         MLX5_ASSERT(pkts_n > loc->pkts_sent);
4097         static_assert(MLX5_EMPW_MIN_PACKETS >= 2, "invalid min size");
4098         pkts += loc->pkts_sent + 1;
4099         pkts_n -= loc->pkts_sent;
4100         for (;;) {
4101                 struct mlx5_wqe_dseg *__rte_restrict dseg;
4102                 struct mlx5_wqe_eseg *__rte_restrict eseg;
4103                 enum mlx5_txcmp_code ret;
4104                 unsigned int part, loop;
4105                 unsigned int slen = 0;
4106
4107 next_empw:
4108                 MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
4109                 part = RTE_MIN(pkts_n, MLX5_TXOFF_CONFIG(MPW) ?
4110                                        MLX5_MPW_MAX_PACKETS :
4111                                        MLX5_EMPW_MAX_PACKETS);
4112                 if (unlikely(loc->elts_free < part)) {
4113                         /* We have no enough elts to save all mbufs. */
4114                         if (unlikely(loc->elts_free < MLX5_EMPW_MIN_PACKETS))
4115                                 return MLX5_TXCMP_CODE_EXIT;
4116                         /* But we still able to send at least minimal eMPW. */
4117                         part = loc->elts_free;
4118                 }
4119                 /* Check whether we have enough WQEs */
4120                 if (unlikely(loc->wqe_free < ((2 + part + 3) / 4))) {
4121                         if (unlikely(loc->wqe_free <
4122                                 ((2 + MLX5_EMPW_MIN_PACKETS + 3) / 4)))
4123                                 return MLX5_TXCMP_CODE_EXIT;
4124                         part = (loc->wqe_free * 4) - 2;
4125                 }
4126                 if (likely(part > 1))
4127                         rte_prefetch0(*pkts);
4128                 loc->wqe_last = txq->wqes + (txq->wqe_ci & txq->wqe_m);
4129                 /*
4130                  * Build eMPW title WQEBB:
4131                  * - Control Segment, eMPW opcode
4132                  * - Ethernet Segment, no inline
4133                  */
4134                 mlx5_tx_cseg_init(txq, loc, loc->wqe_last, part + 2,
4135                                   MLX5_OPCODE_ENHANCED_MPSW, olx);
4136                 mlx5_tx_eseg_none(txq, loc, loc->wqe_last,
4137                                   olx & ~MLX5_TXOFF_CONFIG_VLAN);
4138                 eseg = &loc->wqe_last->eseg;
4139                 dseg = &loc->wqe_last->dseg[0];
4140                 loop = part;
4141                 /* Store the packet length for legacy MPW. */
4142                 if (MLX5_TXOFF_CONFIG(MPW))
4143                         eseg->mss = rte_cpu_to_be_16
4144                                         (rte_pktmbuf_data_len(loc->mbuf));
4145                 for (;;) {
4146                         uint32_t dlen = rte_pktmbuf_data_len(loc->mbuf);
4147 #ifdef MLX5_PMD_SOFT_COUNTERS
4148                         /* Update sent data bytes counter. */
4149                         slen += dlen;
4150 #endif
4151                         mlx5_tx_dseg_ptr
4152                                 (txq, loc, dseg,
4153                                  rte_pktmbuf_mtod(loc->mbuf, uint8_t *),
4154                                  dlen, olx);
4155                         if (unlikely(--loop == 0))
4156                                 break;
4157                         loc->mbuf = *pkts++;
4158                         if (likely(loop > 1))
4159                                 rte_prefetch0(*pkts);
4160                         ret = mlx5_tx_able_to_empw(txq, loc, olx, true);
4161                         /*
4162                          * Unroll the completion code to avoid
4163                          * returning variable value - it results in
4164                          * unoptimized sequent checking in caller.
4165                          */
4166                         if (ret == MLX5_TXCMP_CODE_MULTI) {
4167                                 part -= loop;
4168                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
4169                                 if (unlikely(!loc->elts_free ||
4170                                              !loc->wqe_free))
4171                                         return MLX5_TXCMP_CODE_EXIT;
4172                                 return MLX5_TXCMP_CODE_MULTI;
4173                         }
4174                         MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
4175                         if (ret == MLX5_TXCMP_CODE_TSO) {
4176                                 part -= loop;
4177                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
4178                                 if (unlikely(!loc->elts_free ||
4179                                              !loc->wqe_free))
4180                                         return MLX5_TXCMP_CODE_EXIT;
4181                                 return MLX5_TXCMP_CODE_TSO;
4182                         }
4183                         if (ret == MLX5_TXCMP_CODE_SINGLE) {
4184                                 part -= loop;
4185                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
4186                                 if (unlikely(!loc->elts_free ||
4187                                              !loc->wqe_free))
4188                                         return MLX5_TXCMP_CODE_EXIT;
4189                                 return MLX5_TXCMP_CODE_SINGLE;
4190                         }
4191                         if (ret != MLX5_TXCMP_CODE_EMPW) {
4192                                 MLX5_ASSERT(false);
4193                                 part -= loop;
4194                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
4195                                 return MLX5_TXCMP_CODE_ERROR;
4196                         }
4197                         /*
4198                          * Check whether packet parameters coincide
4199                          * within assumed eMPW batch:
4200                          * - check sum settings
4201                          * - metadata value
4202                          * - software parser settings
4203                          * - packets length (legacy MPW only)
4204                          */
4205                         if (!mlx5_tx_match_empw(txq, eseg, loc, dlen, olx)) {
4206                                 MLX5_ASSERT(loop);
4207                                 part -= loop;
4208                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
4209                                 if (unlikely(!loc->elts_free ||
4210                                              !loc->wqe_free))
4211                                         return MLX5_TXCMP_CODE_EXIT;
4212                                 pkts_n -= part;
4213                                 goto next_empw;
4214                         }
4215                         /* Packet attributes match, continue the same eMPW. */
4216                         ++dseg;
4217                         if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
4218                                 dseg = (struct mlx5_wqe_dseg *)txq->wqes;
4219                 }
4220                 /* eMPW is built successfully, update loop parameters. */
4221                 MLX5_ASSERT(!loop);
4222                 MLX5_ASSERT(pkts_n >= part);
4223 #ifdef MLX5_PMD_SOFT_COUNTERS
4224                 /* Update sent data bytes counter. */
4225                 txq->stats.obytes += slen;
4226 #endif
4227                 loc->elts_free -= part;
4228                 loc->pkts_sent += part;
4229                 txq->wqe_ci += (2 + part + 3) / 4;
4230                 loc->wqe_free -= (2 + part + 3) / 4;
4231                 pkts_n -= part;
4232                 if (unlikely(!pkts_n || !loc->elts_free || !loc->wqe_free))
4233                         return MLX5_TXCMP_CODE_EXIT;
4234                 loc->mbuf = *pkts++;
4235                 ret = mlx5_tx_able_to_empw(txq, loc, olx, true);
4236                 if (unlikely(ret != MLX5_TXCMP_CODE_EMPW))
4237                         return ret;
4238                 /* Continue sending eMPW batches. */
4239         }
4240         MLX5_ASSERT(false);
4241 }
4242
4243 /**
4244  * The routine sends packets with MLX5_OPCODE_EMPW
4245  * with inlining, optionally supports VLAN insertion.
4246  */
4247 static __rte_always_inline enum mlx5_txcmp_code
4248 mlx5_tx_burst_empw_inline(struct mlx5_txq_data *__rte_restrict txq,
4249                           struct rte_mbuf **__rte_restrict pkts,
4250                           unsigned int pkts_n,
4251                           struct mlx5_txq_local *__rte_restrict loc,
4252                           unsigned int olx)
4253 {
4254         /*
4255          * Subroutine is the part of mlx5_tx_burst_single()
4256          * and sends single-segment packet with eMPW opcode
4257          * with data inlining.
4258          */
4259         MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
4260         MLX5_ASSERT(MLX5_TXOFF_CONFIG(EMPW));
4261         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
4262         MLX5_ASSERT(pkts_n > loc->pkts_sent);
4263         static_assert(MLX5_EMPW_MIN_PACKETS >= 2, "invalid min size");
4264         pkts += loc->pkts_sent + 1;
4265         pkts_n -= loc->pkts_sent;
4266         for (;;) {
4267                 struct mlx5_wqe_dseg *__rte_restrict dseg;
4268                 struct mlx5_wqe *__rte_restrict wqem;
4269                 enum mlx5_txcmp_code ret;
4270                 unsigned int room, part, nlim;
4271                 unsigned int slen = 0;
4272
4273                 MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
4274                 /*
4275                  * Limits the amount of packets in one WQE
4276                  * to improve CQE latency generation.
4277                  */
4278                 nlim = RTE_MIN(pkts_n, MLX5_TXOFF_CONFIG(MPW) ?
4279                                        MLX5_MPW_INLINE_MAX_PACKETS :
4280                                        MLX5_EMPW_MAX_PACKETS);
4281                 /* Check whether we have minimal amount WQEs */
4282                 if (unlikely(loc->wqe_free <
4283                             ((2 + MLX5_EMPW_MIN_PACKETS + 3) / 4)))
4284                         return MLX5_TXCMP_CODE_EXIT;
4285                 if (likely(pkts_n > 1))
4286                         rte_prefetch0(*pkts);
4287                 wqem = txq->wqes + (txq->wqe_ci & txq->wqe_m);
4288                 /*
4289                  * Build eMPW title WQEBB:
4290                  * - Control Segment, eMPW opcode, zero DS
4291                  * - Ethernet Segment, no inline
4292                  */
4293                 mlx5_tx_cseg_init(txq, loc, wqem, 0,
4294                                   MLX5_OPCODE_ENHANCED_MPSW, olx);
4295                 mlx5_tx_eseg_none(txq, loc, wqem,
4296                                   olx & ~MLX5_TXOFF_CONFIG_VLAN);
4297                 dseg = &wqem->dseg[0];
4298                 /* Store the packet length for legacy MPW. */
4299                 if (MLX5_TXOFF_CONFIG(MPW))
4300                         wqem->eseg.mss = rte_cpu_to_be_16
4301                                          (rte_pktmbuf_data_len(loc->mbuf));
4302                 room = RTE_MIN(MLX5_WQE_SIZE_MAX / MLX5_WQE_SIZE,
4303                                loc->wqe_free) * MLX5_WQE_SIZE -
4304                                         MLX5_WQE_CSEG_SIZE -
4305                                         MLX5_WQE_ESEG_SIZE;
4306                 /* Limit the room for legacy MPW sessions for performance. */
4307                 if (MLX5_TXOFF_CONFIG(MPW))
4308                         room = RTE_MIN(room,
4309                                        RTE_MAX(txq->inlen_empw +
4310                                                sizeof(dseg->bcount) +
4311                                                (MLX5_TXOFF_CONFIG(VLAN) ?
4312                                                sizeof(struct rte_vlan_hdr) : 0),
4313                                                MLX5_MPW_INLINE_MAX_PACKETS *
4314                                                MLX5_WQE_DSEG_SIZE));
4315                 /* Build WQE till we have space, packets and resources. */
4316                 part = room;
4317                 for (;;) {
4318                         uint32_t dlen = rte_pktmbuf_data_len(loc->mbuf);
4319                         uint8_t *dptr = rte_pktmbuf_mtod(loc->mbuf, uint8_t *);
4320                         unsigned int tlen;
4321
4322                         MLX5_ASSERT(room >= MLX5_WQE_DSEG_SIZE);
4323                         MLX5_ASSERT((room % MLX5_WQE_DSEG_SIZE) == 0);
4324                         MLX5_ASSERT((uintptr_t)dseg < (uintptr_t)txq->wqes_end);
4325                         /*
4326                          * Some Tx offloads may cause an error if
4327                          * packet is not long enough, check against
4328                          * assumed minimal length.
4329                          */
4330                         if (unlikely(dlen <= MLX5_ESEG_MIN_INLINE_SIZE)) {
4331                                 part -= room;
4332                                 if (unlikely(!part))
4333                                         return MLX5_TXCMP_CODE_ERROR;
4334                                 /*
4335                                  * We have some successfully built
4336                                  * packet Data Segments to send.
4337                                  */
4338                                 mlx5_tx_idone_empw(txq, loc, part,
4339                                                    slen, wqem, olx);
4340                                 return MLX5_TXCMP_CODE_ERROR;
4341                         }
4342                         /* Inline or not inline - that's the Question. */
4343                         if (dlen > txq->inlen_empw ||
4344                             loc->mbuf->ol_flags & PKT_TX_DYNF_NOINLINE)
4345                                 goto pointer_empw;
4346                         if (MLX5_TXOFF_CONFIG(MPW)) {
4347                                 if (dlen > txq->inlen_send)
4348                                         goto pointer_empw;
4349                                 tlen = dlen;
4350                                 if (part == room) {
4351                                         /* Open new inline MPW session. */
4352                                         tlen += sizeof(dseg->bcount);
4353                                         dseg->bcount = RTE_BE32(0);
4354                                         dseg = RTE_PTR_ADD
4355                                                 (dseg, sizeof(dseg->bcount));
4356                                 } else {
4357                                         /*
4358                                          * No pointer and inline descriptor
4359                                          * intermix for legacy MPW sessions.
4360                                          */
4361                                         if (wqem->dseg[0].bcount)
4362                                                 break;
4363                                 }
4364                         } else {
4365                                 tlen = sizeof(dseg->bcount) + dlen;
4366                         }
4367                         /* Inline entire packet, optional VLAN insertion. */
4368                         if (MLX5_TXOFF_CONFIG(VLAN) &&
4369                             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
4370                                 /*
4371                                  * The packet length must be checked in
4372                                  * mlx5_tx_able_to_empw() and packet
4373                                  * fits into inline length guaranteed.
4374                                  */
4375                                 MLX5_ASSERT((dlen +
4376                                              sizeof(struct rte_vlan_hdr)) <=
4377                                             txq->inlen_empw);
4378                                 tlen += sizeof(struct rte_vlan_hdr);
4379                                 if (room < tlen)
4380                                         break;
4381                                 dseg = mlx5_tx_dseg_vlan(txq, loc, dseg,
4382                                                          dptr, dlen, olx);
4383 #ifdef MLX5_PMD_SOFT_COUNTERS
4384                                 /* Update sent data bytes counter. */
4385                                 slen += sizeof(struct rte_vlan_hdr);
4386 #endif
4387                         } else {
4388                                 if (room < tlen)
4389                                         break;
4390                                 dseg = mlx5_tx_dseg_empw(txq, loc, dseg,
4391                                                          dptr, dlen, olx);
4392                         }
4393                         if (!MLX5_TXOFF_CONFIG(MPW))
4394                                 tlen = RTE_ALIGN(tlen, MLX5_WSEG_SIZE);
4395                         MLX5_ASSERT(room >= tlen);
4396                         room -= tlen;
4397                         /*
4398                          * Packet data are completely inlined,
4399                          * free the packet immediately.
4400                          */
4401                         rte_pktmbuf_free_seg(loc->mbuf);
4402                         goto next_mbuf;
4403 pointer_empw:
4404                         /*
4405                          * No pointer and inline descriptor
4406                          * intermix for legacy MPW sessions.
4407                          */
4408                         if (MLX5_TXOFF_CONFIG(MPW) &&
4409                             part != room &&
4410                             wqem->dseg[0].bcount == RTE_BE32(0))
4411                                 break;
4412                         /*
4413                          * Not inlinable VLAN packets are
4414                          * proceeded outside of this routine.
4415                          */
4416                         MLX5_ASSERT(room >= MLX5_WQE_DSEG_SIZE);
4417                         if (MLX5_TXOFF_CONFIG(VLAN))
4418                                 MLX5_ASSERT(!(loc->mbuf->ol_flags &
4419                                             PKT_TX_VLAN_PKT));
4420                         mlx5_tx_dseg_ptr(txq, loc, dseg, dptr, dlen, olx);
4421                         /* We have to store mbuf in elts.*/
4422                         txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
4423                         room -= MLX5_WQE_DSEG_SIZE;
4424                         /* Ring buffer wraparound is checked at the loop end.*/
4425                         ++dseg;
4426 next_mbuf:
4427 #ifdef MLX5_PMD_SOFT_COUNTERS
4428                         /* Update sent data bytes counter. */
4429                         slen += dlen;
4430 #endif
4431                         loc->pkts_sent++;
4432                         loc->elts_free--;
4433                         pkts_n--;
4434                         if (unlikely(!pkts_n || !loc->elts_free)) {
4435                                 /*
4436                                  * We have no resources/packets to
4437                                  * continue build descriptors.
4438                                  */
4439                                 part -= room;
4440                                 mlx5_tx_idone_empw(txq, loc, part,
4441                                                    slen, wqem, olx);
4442                                 return MLX5_TXCMP_CODE_EXIT;
4443                         }
4444                         loc->mbuf = *pkts++;
4445                         if (likely(pkts_n > 1))
4446                                 rte_prefetch0(*pkts);
4447                         ret = mlx5_tx_able_to_empw(txq, loc, olx, true);
4448                         /*
4449                          * Unroll the completion code to avoid
4450                          * returning variable value - it results in
4451                          * unoptimized sequent checking in caller.
4452                          */
4453                         if (ret == MLX5_TXCMP_CODE_MULTI) {
4454                                 part -= room;
4455                                 mlx5_tx_idone_empw(txq, loc, part,
4456                                                    slen, wqem, olx);
4457                                 if (unlikely(!loc->elts_free ||
4458                                              !loc->wqe_free))
4459                                         return MLX5_TXCMP_CODE_EXIT;
4460                                 return MLX5_TXCMP_CODE_MULTI;
4461                         }
4462                         MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
4463                         if (ret == MLX5_TXCMP_CODE_TSO) {
4464                                 part -= room;
4465                                 mlx5_tx_idone_empw(txq, loc, part,
4466                                                    slen, wqem, olx);
4467                                 if (unlikely(!loc->elts_free ||
4468                                              !loc->wqe_free))
4469                                         return MLX5_TXCMP_CODE_EXIT;
4470                                 return MLX5_TXCMP_CODE_TSO;
4471                         }
4472                         if (ret == MLX5_TXCMP_CODE_SINGLE) {
4473                                 part -= room;
4474                                 mlx5_tx_idone_empw(txq, loc, part,
4475                                                    slen, wqem, olx);
4476                                 if (unlikely(!loc->elts_free ||
4477                                              !loc->wqe_free))
4478                                         return MLX5_TXCMP_CODE_EXIT;
4479                                 return MLX5_TXCMP_CODE_SINGLE;
4480                         }
4481                         if (ret != MLX5_TXCMP_CODE_EMPW) {
4482                                 MLX5_ASSERT(false);
4483                                 part -= room;
4484                                 mlx5_tx_idone_empw(txq, loc, part,
4485                                                    slen, wqem, olx);
4486                                 return MLX5_TXCMP_CODE_ERROR;
4487                         }
4488                         /* Check if we have minimal room left. */
4489                         nlim--;
4490                         if (unlikely(!nlim || room < MLX5_WQE_DSEG_SIZE))
4491                                 break;
4492                         /*
4493                          * Check whether packet parameters coincide
4494                          * within assumed eMPW batch:
4495                          * - check sum settings
4496                          * - metadata value
4497                          * - software parser settings
4498                          * - packets length (legacy MPW only)
4499                          */
4500                         if (!mlx5_tx_match_empw(txq, &wqem->eseg,
4501                                                 loc, dlen, olx))
4502                                 break;
4503                         /* Packet attributes match, continue the same eMPW. */
4504                         if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
4505                                 dseg = (struct mlx5_wqe_dseg *)txq->wqes;
4506                 }
4507                 /*
4508                  * We get here to close an existing eMPW
4509                  * session and start the new one.
4510                  */
4511                 MLX5_ASSERT(pkts_n);
4512                 part -= room;
4513                 if (unlikely(!part))
4514                         return MLX5_TXCMP_CODE_EXIT;
4515                 mlx5_tx_idone_empw(txq, loc, part, slen, wqem, olx);
4516                 if (unlikely(!loc->elts_free ||
4517                              !loc->wqe_free))
4518                         return MLX5_TXCMP_CODE_EXIT;
4519                 /* Continue the loop with new eMPW session. */
4520         }
4521         MLX5_ASSERT(false);
4522 }
4523
4524 /**
4525  * The routine sends packets with ordinary MLX5_OPCODE_SEND.
4526  * Data inlining and VLAN insertion are supported.
4527  */
4528 static __rte_always_inline enum mlx5_txcmp_code
4529 mlx5_tx_burst_single_send(struct mlx5_txq_data *__rte_restrict txq,
4530                           struct rte_mbuf **__rte_restrict pkts,
4531                           unsigned int pkts_n,
4532                           struct mlx5_txq_local *__rte_restrict loc,
4533                           unsigned int olx)
4534 {
4535         /*
4536          * Subroutine is the part of mlx5_tx_burst_single()
4537          * and sends single-segment packet with SEND opcode.
4538          */
4539         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
4540         MLX5_ASSERT(pkts_n > loc->pkts_sent);
4541         pkts += loc->pkts_sent + 1;
4542         pkts_n -= loc->pkts_sent;
4543         for (;;) {
4544                 struct mlx5_wqe *__rte_restrict wqe;
4545                 enum mlx5_txcmp_code ret;
4546
4547                 MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
4548                 if (MLX5_TXOFF_CONFIG(INLINE)) {
4549                         unsigned int inlen, vlan = 0;
4550
4551                         inlen = rte_pktmbuf_data_len(loc->mbuf);
4552                         if (MLX5_TXOFF_CONFIG(VLAN) &&
4553                             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
4554                                 vlan = sizeof(struct rte_vlan_hdr);
4555                                 inlen += vlan;
4556                                 static_assert((sizeof(struct rte_vlan_hdr) +
4557                                                sizeof(struct rte_ether_hdr)) ==
4558                                                MLX5_ESEG_MIN_INLINE_SIZE,
4559                                                "invalid min inline data size");
4560                         }
4561                         /*
4562                          * If inlining is enabled at configuration time
4563                          * the limit must be not less than minimal size.
4564                          * Otherwise we would do extra check for data
4565                          * size to avoid crashes due to length overflow.
4566                          */
4567                         MLX5_ASSERT(txq->inlen_send >=
4568                                     MLX5_ESEG_MIN_INLINE_SIZE);
4569                         if (inlen <= txq->inlen_send) {
4570                                 unsigned int seg_n, wqe_n;
4571
4572                                 rte_prefetch0(rte_pktmbuf_mtod
4573                                                 (loc->mbuf, uint8_t *));
4574                                 /* Check against minimal length. */
4575                                 if (inlen <= MLX5_ESEG_MIN_INLINE_SIZE)
4576                                         return MLX5_TXCMP_CODE_ERROR;
4577                                 if (loc->mbuf->ol_flags &
4578                                     PKT_TX_DYNF_NOINLINE) {
4579                                         /*
4580                                          * The hint flag not to inline packet
4581                                          * data is set. Check whether we can
4582                                          * follow the hint.
4583                                          */
4584                                         if ((!MLX5_TXOFF_CONFIG(EMPW) &&
4585                                               txq->inlen_mode) ||
4586                                             (MLX5_TXOFF_CONFIG(MPW) &&
4587                                              txq->inlen_mode)) {
4588                                                 /*
4589                                                  * The hardware requires the
4590                                                  * minimal inline data header.
4591                                                  */
4592                                                 goto single_min_inline;
4593                                         }
4594                                         if (MLX5_TXOFF_CONFIG(VLAN) &&
4595                                             vlan && !txq->vlan_en) {
4596                                                 /*
4597                                                  * We must insert VLAN tag
4598                                                  * by software means.
4599                                                  */
4600                                                 goto single_part_inline;
4601                                         }
4602                                         goto single_no_inline;
4603                                 }
4604                                 /*
4605                                  * Completely inlined packet data WQE:
4606                                  * - Control Segment, SEND opcode
4607                                  * - Ethernet Segment, no VLAN insertion
4608                                  * - Data inlined, VLAN optionally inserted
4609                                  * - Alignment to MLX5_WSEG_SIZE
4610                                  * Have to estimate amount of WQEBBs
4611                                  */
4612                                 seg_n = (inlen + 3 * MLX5_WSEG_SIZE -
4613                                          MLX5_ESEG_MIN_INLINE_SIZE +
4614                                          MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
4615                                 /* Check if there are enough WQEBBs. */
4616                                 wqe_n = (seg_n + 3) / 4;
4617                                 if (wqe_n > loc->wqe_free)
4618                                         return MLX5_TXCMP_CODE_EXIT;
4619                                 wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
4620                                 loc->wqe_last = wqe;
4621                                 mlx5_tx_cseg_init(txq, loc, wqe, seg_n,
4622                                                   MLX5_OPCODE_SEND, olx);
4623                                 mlx5_tx_eseg_data(txq, loc, wqe,
4624                                                   vlan, inlen, 0, olx);
4625                                 txq->wqe_ci += wqe_n;
4626                                 loc->wqe_free -= wqe_n;
4627                                 /*
4628                                  * Packet data are completely inlined,
4629                                  * free the packet immediately.
4630                                  */
4631                                 rte_pktmbuf_free_seg(loc->mbuf);
4632                         } else if ((!MLX5_TXOFF_CONFIG(EMPW) ||
4633                                      MLX5_TXOFF_CONFIG(MPW)) &&
4634                                         txq->inlen_mode) {
4635                                 /*
4636                                  * If minimal inlining is requested the eMPW
4637                                  * feature should be disabled due to data is
4638                                  * inlined into Ethernet Segment, which can
4639                                  * not contain inlined data for eMPW due to
4640                                  * segment shared for all packets.
4641                                  */
4642                                 struct mlx5_wqe_dseg *__rte_restrict dseg;
4643                                 unsigned int ds;
4644                                 uint8_t *dptr;
4645
4646                                 /*
4647                                  * The inline-mode settings require
4648                                  * to inline the specified amount of
4649                                  * data bytes to the Ethernet Segment.
4650                                  * We should check the free space in
4651                                  * WQE ring buffer to inline partially.
4652                                  */
4653 single_min_inline:
4654                                 MLX5_ASSERT(txq->inlen_send >= txq->inlen_mode);
4655                                 MLX5_ASSERT(inlen > txq->inlen_mode);
4656                                 MLX5_ASSERT(txq->inlen_mode >=
4657                                             MLX5_ESEG_MIN_INLINE_SIZE);
4658                                 /*
4659                                  * Check whether there are enough free WQEBBs:
4660                                  * - Control Segment
4661                                  * - Ethernet Segment
4662                                  * - First Segment of inlined Ethernet data
4663                                  * - ... data continued ...
4664                                  * - Finishing Data Segment of pointer type
4665                                  */
4666                                 ds = (MLX5_WQE_CSEG_SIZE +
4667                                       MLX5_WQE_ESEG_SIZE +
4668                                       MLX5_WQE_DSEG_SIZE +
4669                                       txq->inlen_mode -
4670                                       MLX5_ESEG_MIN_INLINE_SIZE +
4671                                       MLX5_WQE_DSEG_SIZE +
4672                                       MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
4673                                 if (loc->wqe_free < ((ds + 3) / 4))
4674                                         return MLX5_TXCMP_CODE_EXIT;
4675                                 /*
4676                                  * Build the ordinary SEND WQE:
4677                                  * - Control Segment
4678                                  * - Ethernet Segment, inline inlen_mode bytes
4679                                  * - Data Segment of pointer type
4680                                  */
4681                                 wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
4682                                 loc->wqe_last = wqe;
4683                                 mlx5_tx_cseg_init(txq, loc, wqe, ds,
4684                                                   MLX5_OPCODE_SEND, olx);
4685                                 dseg = mlx5_tx_eseg_data(txq, loc, wqe, vlan,
4686                                                          txq->inlen_mode,
4687                                                          0, olx);
4688                                 dptr = rte_pktmbuf_mtod(loc->mbuf, uint8_t *) +
4689                                        txq->inlen_mode - vlan;
4690                                 inlen -= txq->inlen_mode;
4691                                 mlx5_tx_dseg_ptr(txq, loc, dseg,
4692                                                  dptr, inlen, olx);
4693                                 /*
4694                                  * WQE is built, update the loop parameters
4695                                  * and got to the next packet.
4696                                  */
4697                                 txq->wqe_ci += (ds + 3) / 4;
4698                                 loc->wqe_free -= (ds + 3) / 4;
4699                                 /* We have to store mbuf in elts.*/
4700                                 MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
4701                                 txq->elts[txq->elts_head++ & txq->elts_m] =
4702                                                 loc->mbuf;
4703                                 --loc->elts_free;
4704                         } else {
4705                                 uint8_t *dptr;
4706                                 unsigned int dlen;
4707
4708                                 /*
4709                                  * Partially inlined packet data WQE, we have
4710                                  * some space in title WQEBB, we can fill it
4711                                  * with some packet data. It takes one WQEBB,
4712                                  * it is available, no extra space check:
4713                                  * - Control Segment, SEND opcode
4714                                  * - Ethernet Segment, no VLAN insertion
4715                                  * - MLX5_ESEG_MIN_INLINE_SIZE bytes of Data
4716                                  * - Data Segment, pointer type
4717                                  *
4718                                  * We also get here if VLAN insertion is not
4719                                  * supported by HW, the inline is enabled.
4720                                  */
4721 single_part_inline:
4722                                 wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
4723                                 loc->wqe_last = wqe;
4724                                 mlx5_tx_cseg_init(txq, loc, wqe, 4,
4725                                                   MLX5_OPCODE_SEND, olx);
4726                                 mlx5_tx_eseg_dmin(txq, loc, wqe, vlan, olx);
4727                                 dptr = rte_pktmbuf_mtod(loc->mbuf, uint8_t *) +
4728                                        MLX5_ESEG_MIN_INLINE_SIZE - vlan;
4729                                 /*
4730                                  * The length check is performed above, by
4731                                  * comparing with txq->inlen_send. We should
4732                                  * not get overflow here.
4733                                  */
4734                                 MLX5_ASSERT(inlen > MLX5_ESEG_MIN_INLINE_SIZE);
4735                                 dlen = inlen - MLX5_ESEG_MIN_INLINE_SIZE;
4736                                 mlx5_tx_dseg_ptr(txq, loc, &wqe->dseg[1],
4737                                                  dptr, dlen, olx);
4738                                 ++txq->wqe_ci;
4739                                 --loc->wqe_free;
4740                                 /* We have to store mbuf in elts.*/
4741                                 MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
4742                                 txq->elts[txq->elts_head++ & txq->elts_m] =
4743                                                 loc->mbuf;
4744                                 --loc->elts_free;
4745                         }
4746 #ifdef MLX5_PMD_SOFT_COUNTERS
4747                         /* Update sent data bytes counter. */
4748                         txq->stats.obytes += vlan +
4749                                         rte_pktmbuf_data_len(loc->mbuf);
4750 #endif
4751                 } else {
4752                         /*
4753                          * No inline at all, it means the CPU cycles saving
4754                          * is prioritized at configuration, we should not
4755                          * copy any packet data to WQE.
4756                          *
4757                          * SEND WQE, one WQEBB:
4758                          * - Control Segment, SEND opcode
4759                          * - Ethernet Segment, optional VLAN, no inline
4760                          * - Data Segment, pointer type
4761                          */
4762 single_no_inline:
4763                         wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
4764                         loc->wqe_last = wqe;
4765                         mlx5_tx_cseg_init(txq, loc, wqe, 3,
4766                                           MLX5_OPCODE_SEND, olx);
4767                         mlx5_tx_eseg_none(txq, loc, wqe, olx);
4768                         mlx5_tx_dseg_ptr
4769                                 (txq, loc, &wqe->dseg[0],
4770                                  rte_pktmbuf_mtod(loc->mbuf, uint8_t *),
4771                                  rte_pktmbuf_data_len(loc->mbuf), olx);
4772                         ++txq->wqe_ci;
4773                         --loc->wqe_free;
4774                         /*
4775                          * We should not store mbuf pointer in elts
4776                          * if no inlining is configured, this is done
4777                          * by calling routine in a batch copy.
4778                          */
4779                         MLX5_ASSERT(!MLX5_TXOFF_CONFIG(INLINE));
4780                         --loc->elts_free;
4781 #ifdef MLX5_PMD_SOFT_COUNTERS
4782                         /* Update sent data bytes counter. */
4783                         txq->stats.obytes += rte_pktmbuf_data_len(loc->mbuf);
4784                         if (MLX5_TXOFF_CONFIG(VLAN) &&
4785                             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT)
4786                                 txq->stats.obytes +=
4787                                         sizeof(struct rte_vlan_hdr);
4788 #endif
4789                 }
4790                 ++loc->pkts_sent;
4791                 --pkts_n;
4792                 if (unlikely(!pkts_n || !loc->elts_free || !loc->wqe_free))
4793                         return MLX5_TXCMP_CODE_EXIT;
4794                 loc->mbuf = *pkts++;
4795                 if (pkts_n > 1)
4796                         rte_prefetch0(*pkts);
4797                 ret = mlx5_tx_able_to_empw(txq, loc, olx, true);
4798                 if (unlikely(ret != MLX5_TXCMP_CODE_SINGLE))
4799                         return ret;
4800         }
4801         MLX5_ASSERT(false);
4802 }
4803
4804 static __rte_always_inline enum mlx5_txcmp_code
4805 mlx5_tx_burst_single(struct mlx5_txq_data *__rte_restrict txq,
4806                      struct rte_mbuf **__rte_restrict pkts,
4807                      unsigned int pkts_n,
4808                      struct mlx5_txq_local *__rte_restrict loc,
4809                      unsigned int olx)
4810 {
4811         enum mlx5_txcmp_code ret;
4812
4813         ret = mlx5_tx_able_to_empw(txq, loc, olx, false);
4814         if (ret == MLX5_TXCMP_CODE_SINGLE)
4815                 goto ordinary_send;
4816         MLX5_ASSERT(ret == MLX5_TXCMP_CODE_EMPW);
4817         for (;;) {
4818                 /* Optimize for inline/no inline eMPW send. */
4819                 ret = (MLX5_TXOFF_CONFIG(INLINE)) ?
4820                         mlx5_tx_burst_empw_inline
4821                                 (txq, pkts, pkts_n, loc, olx) :
4822                         mlx5_tx_burst_empw_simple
4823                                 (txq, pkts, pkts_n, loc, olx);
4824                 if (ret != MLX5_TXCMP_CODE_SINGLE)
4825                         return ret;
4826                 /* The resources to send one packet should remain. */
4827                 MLX5_ASSERT(loc->elts_free && loc->wqe_free);
4828 ordinary_send:
4829                 ret = mlx5_tx_burst_single_send(txq, pkts, pkts_n, loc, olx);
4830                 MLX5_ASSERT(ret != MLX5_TXCMP_CODE_SINGLE);
4831                 if (ret != MLX5_TXCMP_CODE_EMPW)
4832                         return ret;
4833                 /* The resources to send one packet should remain. */
4834                 MLX5_ASSERT(loc->elts_free && loc->wqe_free);
4835         }
4836 }
4837
4838 /**
4839  * DPDK Tx callback template. This is configured template
4840  * used to generate routines optimized for specified offload setup.
4841  * One of this generated functions is chosen at SQ configuration
4842  * time.
4843  *
4844  * @param txq
4845  *   Generic pointer to TX queue structure.
4846  * @param[in] pkts
4847  *   Packets to transmit.
4848  * @param pkts_n
4849  *   Number of packets in array.
4850  * @param olx
4851  *   Configured offloads mask, presents the bits of MLX5_TXOFF_CONFIG_xxx
4852  *   values. Should be static to take compile time static configuration
4853  *   advantages.
4854  *
4855  * @return
4856  *   Number of packets successfully transmitted (<= pkts_n).
4857  */
4858 static __rte_always_inline uint16_t
4859 mlx5_tx_burst_tmpl(struct mlx5_txq_data *__rte_restrict txq,
4860                    struct rte_mbuf **__rte_restrict pkts,
4861                    uint16_t pkts_n,
4862                    unsigned int olx)
4863 {
4864         struct mlx5_txq_local loc;
4865         enum mlx5_txcmp_code ret;
4866         unsigned int part;
4867
4868         MLX5_ASSERT(txq->elts_s >= (uint16_t)(txq->elts_head - txq->elts_tail));
4869         MLX5_ASSERT(txq->wqe_s >= (uint16_t)(txq->wqe_ci - txq->wqe_pi));
4870         if (unlikely(!pkts_n))
4871                 return 0;
4872         loc.pkts_sent = 0;
4873         loc.pkts_copy = 0;
4874         loc.wqe_last = NULL;
4875
4876 send_loop:
4877         loc.pkts_loop = loc.pkts_sent;
4878         /*
4879          * Check if there are some CQEs, if any:
4880          * - process an encountered errors
4881          * - process the completed WQEs
4882          * - free related mbufs
4883          * - doorbell the NIC about processed CQEs
4884          */
4885         rte_prefetch0(*(pkts + loc.pkts_sent));
4886         mlx5_tx_handle_completion(txq, olx);
4887         /*
4888          * Calculate the number of available resources - elts and WQEs.
4889          * There are two possible different scenarios:
4890          * - no data inlining into WQEs, one WQEBB may contains up to
4891          *   four packets, in this case elts become scarce resource
4892          * - data inlining into WQEs, one packet may require multiple
4893          *   WQEBBs, the WQEs become the limiting factor.
4894          */
4895         MLX5_ASSERT(txq->elts_s >= (uint16_t)(txq->elts_head - txq->elts_tail));
4896         loc.elts_free = txq->elts_s -
4897                                 (uint16_t)(txq->elts_head - txq->elts_tail);
4898         MLX5_ASSERT(txq->wqe_s >= (uint16_t)(txq->wqe_ci - txq->wqe_pi));
4899         loc.wqe_free = txq->wqe_s -
4900                                 (uint16_t)(txq->wqe_ci - txq->wqe_pi);
4901         if (unlikely(!loc.elts_free || !loc.wqe_free))
4902                 goto burst_exit;
4903         for (;;) {
4904                 /*
4905                  * Fetch the packet from array. Usually this is
4906                  * the first packet in series of multi/single
4907                  * segment packets.
4908                  */
4909                 loc.mbuf = *(pkts + loc.pkts_sent);
4910                 /* Dedicated branch for multi-segment packets. */
4911                 if (MLX5_TXOFF_CONFIG(MULTI) &&
4912                     unlikely(NB_SEGS(loc.mbuf) > 1)) {
4913                         /*
4914                          * Multi-segment packet encountered.
4915                          * Hardware is able to process it only
4916                          * with SEND/TSO opcodes, one packet
4917                          * per WQE, do it in dedicated routine.
4918                          */
4919 enter_send_multi:
4920                         MLX5_ASSERT(loc.pkts_sent >= loc.pkts_copy);
4921                         part = loc.pkts_sent - loc.pkts_copy;
4922                         if (!MLX5_TXOFF_CONFIG(INLINE) && part) {
4923                                 /*
4924                                  * There are some single-segment mbufs not
4925                                  * stored in elts. The mbufs must be in the
4926                                  * same order as WQEs, so we must copy the
4927                                  * mbufs to elts here, before the coming
4928                                  * multi-segment packet mbufs is appended.
4929                                  */
4930                                 mlx5_tx_copy_elts(txq, pkts + loc.pkts_copy,
4931                                                   part, olx);
4932                                 loc.pkts_copy = loc.pkts_sent;
4933                         }
4934                         MLX5_ASSERT(pkts_n > loc.pkts_sent);
4935                         ret = mlx5_tx_burst_mseg(txq, pkts, pkts_n, &loc, olx);
4936                         if (!MLX5_TXOFF_CONFIG(INLINE))
4937                                 loc.pkts_copy = loc.pkts_sent;
4938                         /*
4939                          * These returned code checks are supposed
4940                          * to be optimized out due to routine inlining.
4941                          */
4942                         if (ret == MLX5_TXCMP_CODE_EXIT) {
4943                                 /*
4944                                  * The routine returns this code when
4945                                  * all packets are sent or there is no
4946                                  * enough resources to complete request.
4947                                  */
4948                                 break;
4949                         }
4950                         if (ret == MLX5_TXCMP_CODE_ERROR) {
4951                                 /*
4952                                  * The routine returns this code when
4953                                  * some error in the incoming packets
4954                                  * format occurred.
4955                                  */
4956                                 txq->stats.oerrors++;
4957                                 break;
4958                         }
4959                         if (ret == MLX5_TXCMP_CODE_SINGLE) {
4960                                 /*
4961                                  * The single-segment packet was encountered
4962                                  * in the array, try to send it with the
4963                                  * best optimized way, possible engaging eMPW.
4964                                  */
4965                                 goto enter_send_single;
4966                         }
4967                         if (MLX5_TXOFF_CONFIG(TSO) &&
4968                             ret == MLX5_TXCMP_CODE_TSO) {
4969                                 /*
4970                                  * The single-segment TSO packet was
4971                                  * encountered in the array.
4972                                  */
4973                                 goto enter_send_tso;
4974                         }
4975                         /* We must not get here. Something is going wrong. */
4976                         MLX5_ASSERT(false);
4977                         txq->stats.oerrors++;
4978                         break;
4979                 }
4980                 /* Dedicated branch for single-segment TSO packets. */
4981                 if (MLX5_TXOFF_CONFIG(TSO) &&
4982                     unlikely(loc.mbuf->ol_flags & PKT_TX_TCP_SEG)) {
4983                         /*
4984                          * TSO might require special way for inlining
4985                          * (dedicated parameters) and is sent with
4986                          * MLX5_OPCODE_TSO opcode only, provide this
4987                          * in dedicated branch.
4988                          */
4989 enter_send_tso:
4990                         MLX5_ASSERT(NB_SEGS(loc.mbuf) == 1);
4991                         MLX5_ASSERT(pkts_n > loc.pkts_sent);
4992                         ret = mlx5_tx_burst_tso(txq, pkts, pkts_n, &loc, olx);
4993                         /*
4994                          * These returned code checks are supposed
4995                          * to be optimized out due to routine inlining.
4996                          */
4997                         if (ret == MLX5_TXCMP_CODE_EXIT)
4998                                 break;
4999                         if (ret == MLX5_TXCMP_CODE_ERROR) {
5000                                 txq->stats.oerrors++;
5001                                 break;
5002                         }
5003                         if (ret == MLX5_TXCMP_CODE_SINGLE)
5004                                 goto enter_send_single;
5005                         if (MLX5_TXOFF_CONFIG(MULTI) &&
5006                             ret == MLX5_TXCMP_CODE_MULTI) {
5007                                 /*
5008                                  * The multi-segment packet was
5009                                  * encountered in the array.
5010                                  */
5011                                 goto enter_send_multi;
5012                         }
5013                         /* We must not get here. Something is going wrong. */
5014                         MLX5_ASSERT(false);
5015                         txq->stats.oerrors++;
5016                         break;
5017                 }
5018                 /*
5019                  * The dedicated branch for the single-segment packets
5020                  * without TSO. Often these ones can be sent using
5021                  * MLX5_OPCODE_EMPW with multiple packets in one WQE.
5022                  * The routine builds the WQEs till it encounters
5023                  * the TSO or multi-segment packet (in case if these
5024                  * offloads are requested at SQ configuration time).
5025                  */
5026 enter_send_single:
5027                 MLX5_ASSERT(pkts_n > loc.pkts_sent);
5028                 ret = mlx5_tx_burst_single(txq, pkts, pkts_n, &loc, olx);
5029                 /*
5030                  * These returned code checks are supposed
5031                  * to be optimized out due to routine inlining.
5032                  */
5033                 if (ret == MLX5_TXCMP_CODE_EXIT)
5034                         break;
5035                 if (ret == MLX5_TXCMP_CODE_ERROR) {
5036                         txq->stats.oerrors++;
5037                         break;
5038                 }
5039                 if (MLX5_TXOFF_CONFIG(MULTI) &&
5040                     ret == MLX5_TXCMP_CODE_MULTI) {
5041                         /*
5042                          * The multi-segment packet was
5043                          * encountered in the array.
5044                          */
5045                         goto enter_send_multi;
5046                 }
5047                 if (MLX5_TXOFF_CONFIG(TSO) &&
5048                     ret == MLX5_TXCMP_CODE_TSO) {
5049                         /*
5050                          * The single-segment TSO packet was
5051                          * encountered in the array.
5052                          */
5053                         goto enter_send_tso;
5054                 }
5055                 /* We must not get here. Something is going wrong. */
5056                 MLX5_ASSERT(false);
5057                 txq->stats.oerrors++;
5058                 break;
5059         }
5060         /*
5061          * Main Tx loop is completed, do the rest:
5062          * - set completion request if thresholds are reached
5063          * - doorbell the hardware
5064          * - copy the rest of mbufs to elts (if any)
5065          */
5066         MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE) ||
5067                     loc.pkts_sent >= loc.pkts_copy);
5068         /* Take a shortcut if nothing is sent. */
5069         if (unlikely(loc.pkts_sent == loc.pkts_loop))
5070                 goto burst_exit;
5071         /* Request CQE generation if limits are reached. */
5072         mlx5_tx_request_completion(txq, &loc, olx);
5073         /*
5074          * Ring QP doorbell immediately after WQE building completion
5075          * to improve latencies. The pure software related data treatment
5076          * can be completed after doorbell. Tx CQEs for this SQ are
5077          * processed in this thread only by the polling.
5078          *
5079          * The rdma core library can map doorbell register in two ways,
5080          * depending on the environment variable "MLX5_SHUT_UP_BF":
5081          *
5082          * - as regular cached memory, the variable is either missing or
5083          *   set to zero. This type of mapping may cause the significant
5084          *   doorbell register writing latency and requires explicit
5085          *   memory write barrier to mitigate this issue and prevent
5086          *   write combining.
5087          *
5088          * - as non-cached memory, the variable is present and set to
5089          *   not "0" value. This type of mapping may cause performance
5090          *   impact under heavy loading conditions but the explicit write
5091          *   memory barrier is not required and it may improve core
5092          *   performance.
5093          *
5094          * - the legacy behaviour (prior 19.08 release) was to use some
5095          *   heuristics to decide whether write memory barrier should
5096          *   be performed. This behavior is supported with specifying
5097          *   tx_db_nc=2, write barrier is skipped if application
5098          *   provides the full recommended burst of packets, it
5099          *   supposes the next packets are coming and the write barrier
5100          *   will be issued on the next burst (after descriptor writing,
5101          *   at least).
5102          */
5103         mlx5_tx_dbrec_cond_wmb(txq, loc.wqe_last, !txq->db_nc &&
5104                         (!txq->db_heu || pkts_n % MLX5_TX_DEFAULT_BURST));
5105         /* Not all of the mbufs may be stored into elts yet. */
5106         part = MLX5_TXOFF_CONFIG(INLINE) ? 0 : loc.pkts_sent - loc.pkts_copy;
5107         if (!MLX5_TXOFF_CONFIG(INLINE) && part) {
5108                 /*
5109                  * There are some single-segment mbufs not stored in elts.
5110                  * It can be only if the last packet was single-segment.
5111                  * The copying is gathered into one place due to it is
5112                  * a good opportunity to optimize that with SIMD.
5113                  * Unfortunately if inlining is enabled the gaps in
5114                  * pointer array may happen due to early freeing of the
5115                  * inlined mbufs.
5116                  */
5117                 mlx5_tx_copy_elts(txq, pkts + loc.pkts_copy, part, olx);
5118                 loc.pkts_copy = loc.pkts_sent;
5119         }
5120         MLX5_ASSERT(txq->elts_s >= (uint16_t)(txq->elts_head - txq->elts_tail));
5121         MLX5_ASSERT(txq->wqe_s >= (uint16_t)(txq->wqe_ci - txq->wqe_pi));
5122         if (pkts_n > loc.pkts_sent) {
5123                 /*
5124                  * If burst size is large there might be no enough CQE
5125                  * fetched from completion queue and no enough resources
5126                  * freed to send all the packets.
5127                  */
5128                 goto send_loop;
5129         }
5130 burst_exit:
5131 #ifdef MLX5_PMD_SOFT_COUNTERS
5132         /* Increment sent packets counter. */
5133         txq->stats.opackets += loc.pkts_sent;
5134 #endif
5135         return loc.pkts_sent;
5136 }
5137
5138 /* Generate routines with Enhanced Multi-Packet Write support. */
5139 MLX5_TXOFF_DECL(full_empw,
5140                 MLX5_TXOFF_CONFIG_FULL | MLX5_TXOFF_CONFIG_EMPW)
5141
5142 MLX5_TXOFF_DECL(none_empw,
5143                 MLX5_TXOFF_CONFIG_NONE | MLX5_TXOFF_CONFIG_EMPW)
5144
5145 MLX5_TXOFF_DECL(md_empw,
5146                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5147
5148 MLX5_TXOFF_DECL(mt_empw,
5149                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5150                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5151
5152 MLX5_TXOFF_DECL(mtsc_empw,
5153                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5154                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5155                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5156
5157 MLX5_TXOFF_DECL(mti_empw,
5158                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5159                 MLX5_TXOFF_CONFIG_INLINE |
5160                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5161
5162 MLX5_TXOFF_DECL(mtv_empw,
5163                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5164                 MLX5_TXOFF_CONFIG_VLAN |
5165                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5166
5167 MLX5_TXOFF_DECL(mtiv_empw,
5168                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5169                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5170                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5171
5172 MLX5_TXOFF_DECL(sc_empw,
5173                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5174                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5175
5176 MLX5_TXOFF_DECL(sci_empw,
5177                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5178                 MLX5_TXOFF_CONFIG_INLINE |
5179                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5180
5181 MLX5_TXOFF_DECL(scv_empw,
5182                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5183                 MLX5_TXOFF_CONFIG_VLAN |
5184                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5185
5186 MLX5_TXOFF_DECL(sciv_empw,
5187                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5188                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5189                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5190
5191 MLX5_TXOFF_DECL(i_empw,
5192                 MLX5_TXOFF_CONFIG_INLINE |
5193                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5194
5195 MLX5_TXOFF_DECL(v_empw,
5196                 MLX5_TXOFF_CONFIG_VLAN |
5197                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5198
5199 MLX5_TXOFF_DECL(iv_empw,
5200                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5201                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5202
5203 /* Generate routines without Enhanced Multi-Packet Write support. */
5204 MLX5_TXOFF_DECL(full,
5205                 MLX5_TXOFF_CONFIG_FULL)
5206
5207 MLX5_TXOFF_DECL(none,
5208                 MLX5_TXOFF_CONFIG_NONE)
5209
5210 MLX5_TXOFF_DECL(md,
5211                 MLX5_TXOFF_CONFIG_METADATA)
5212
5213 MLX5_TXOFF_DECL(mt,
5214                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5215                 MLX5_TXOFF_CONFIG_METADATA)
5216
5217 MLX5_TXOFF_DECL(mtsc,
5218                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5219                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5220                 MLX5_TXOFF_CONFIG_METADATA)
5221
5222 MLX5_TXOFF_DECL(mti,
5223                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5224                 MLX5_TXOFF_CONFIG_INLINE |
5225                 MLX5_TXOFF_CONFIG_METADATA)
5226
5227
5228 MLX5_TXOFF_DECL(mtv,
5229                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5230                 MLX5_TXOFF_CONFIG_VLAN |
5231                 MLX5_TXOFF_CONFIG_METADATA)
5232
5233
5234 MLX5_TXOFF_DECL(mtiv,
5235                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5236                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5237                 MLX5_TXOFF_CONFIG_METADATA)
5238
5239 MLX5_TXOFF_DECL(sc,
5240                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5241                 MLX5_TXOFF_CONFIG_METADATA)
5242
5243 MLX5_TXOFF_DECL(sci,
5244                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5245                 MLX5_TXOFF_CONFIG_INLINE |
5246                 MLX5_TXOFF_CONFIG_METADATA)
5247
5248
5249 MLX5_TXOFF_DECL(scv,
5250                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5251                 MLX5_TXOFF_CONFIG_VLAN |
5252                 MLX5_TXOFF_CONFIG_METADATA)
5253
5254
5255 MLX5_TXOFF_DECL(sciv,
5256                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5257                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5258                 MLX5_TXOFF_CONFIG_METADATA)
5259
5260 MLX5_TXOFF_DECL(i,
5261                 MLX5_TXOFF_CONFIG_INLINE |
5262                 MLX5_TXOFF_CONFIG_METADATA)
5263
5264 MLX5_TXOFF_DECL(v,
5265                 MLX5_TXOFF_CONFIG_VLAN |
5266                 MLX5_TXOFF_CONFIG_METADATA)
5267
5268 MLX5_TXOFF_DECL(iv,
5269                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5270                 MLX5_TXOFF_CONFIG_METADATA)
5271
5272 /* Generate routines with timestamp scheduling. */
5273 MLX5_TXOFF_DECL(full_ts_nompw,
5274                 MLX5_TXOFF_CONFIG_FULL | MLX5_TXOFF_CONFIG_TXPP)
5275
5276 MLX5_TXOFF_DECL(full_ts_nompwi,
5277                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5278                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5279                 MLX5_TXOFF_CONFIG_VLAN | MLX5_TXOFF_CONFIG_METADATA |
5280                 MLX5_TXOFF_CONFIG_TXPP)
5281
5282 MLX5_TXOFF_DECL(full_ts,
5283                 MLX5_TXOFF_CONFIG_FULL | MLX5_TXOFF_CONFIG_TXPP |
5284                 MLX5_TXOFF_CONFIG_EMPW)
5285
5286 MLX5_TXOFF_DECL(full_ts_noi,
5287                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5288                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5289                 MLX5_TXOFF_CONFIG_VLAN | MLX5_TXOFF_CONFIG_METADATA |
5290                 MLX5_TXOFF_CONFIG_TXPP | MLX5_TXOFF_CONFIG_EMPW)
5291
5292 MLX5_TXOFF_DECL(none_ts,
5293                 MLX5_TXOFF_CONFIG_NONE | MLX5_TXOFF_CONFIG_TXPP |
5294                 MLX5_TXOFF_CONFIG_EMPW)
5295
5296 MLX5_TXOFF_DECL(mdi_ts,
5297                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_METADATA |
5298                 MLX5_TXOFF_CONFIG_TXPP | MLX5_TXOFF_CONFIG_EMPW)
5299
5300 MLX5_TXOFF_DECL(mti_ts,
5301                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5302                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_METADATA |
5303                 MLX5_TXOFF_CONFIG_TXPP | MLX5_TXOFF_CONFIG_EMPW)
5304
5305 MLX5_TXOFF_DECL(mtiv_ts,
5306                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5307                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5308                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_TXPP |
5309                 MLX5_TXOFF_CONFIG_EMPW)
5310
5311 /*
5312  * Generate routines with Legacy Multi-Packet Write support.
5313  * This mode is supported by ConnectX-4 Lx only and imposes
5314  * offload limitations, not supported:
5315  *   - ACL/Flows (metadata are becoming meaningless)
5316  *   - WQE Inline headers
5317  *   - SRIOV (E-Switch offloads)
5318  *   - VLAN insertion
5319  *   - tunnel encapsulation/decapsulation
5320  *   - TSO
5321  */
5322 MLX5_TXOFF_DECL(none_mpw,
5323                 MLX5_TXOFF_CONFIG_NONE | MLX5_TXOFF_CONFIG_EMPW |
5324                 MLX5_TXOFF_CONFIG_MPW)
5325
5326 MLX5_TXOFF_DECL(mci_mpw,
5327                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_CSUM |
5328                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_EMPW |
5329                 MLX5_TXOFF_CONFIG_MPW)
5330
5331 MLX5_TXOFF_DECL(mc_mpw,
5332                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_CSUM |
5333                 MLX5_TXOFF_CONFIG_EMPW | MLX5_TXOFF_CONFIG_MPW)
5334
5335 MLX5_TXOFF_DECL(i_mpw,
5336                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_EMPW |
5337                 MLX5_TXOFF_CONFIG_MPW)
5338
5339 /*
5340  * Array of declared and compiled Tx burst function and corresponding
5341  * supported offloads set. The array is used to select the Tx burst
5342  * function for specified offloads set at Tx queue configuration time.
5343  */
5344 const struct {
5345         eth_tx_burst_t func;
5346         unsigned int olx;
5347 } txoff_func[] = {
5348 MLX5_TXOFF_INFO(full_empw,
5349                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5350                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5351                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5352                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5353
5354 MLX5_TXOFF_INFO(none_empw,
5355                 MLX5_TXOFF_CONFIG_NONE | MLX5_TXOFF_CONFIG_EMPW)
5356
5357 MLX5_TXOFF_INFO(md_empw,
5358                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5359
5360 MLX5_TXOFF_INFO(mt_empw,
5361                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5362                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5363
5364 MLX5_TXOFF_INFO(mtsc_empw,
5365                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5366                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5367                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5368
5369 MLX5_TXOFF_INFO(mti_empw,
5370                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5371                 MLX5_TXOFF_CONFIG_INLINE |
5372                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5373
5374 MLX5_TXOFF_INFO(mtv_empw,
5375                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5376                 MLX5_TXOFF_CONFIG_VLAN |
5377                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5378
5379 MLX5_TXOFF_INFO(mtiv_empw,
5380                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5381                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5382                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5383
5384 MLX5_TXOFF_INFO(sc_empw,
5385                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5386                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5387
5388 MLX5_TXOFF_INFO(sci_empw,
5389                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5390                 MLX5_TXOFF_CONFIG_INLINE |
5391                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5392
5393 MLX5_TXOFF_INFO(scv_empw,
5394                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5395                 MLX5_TXOFF_CONFIG_VLAN |
5396                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5397
5398 MLX5_TXOFF_INFO(sciv_empw,
5399                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5400                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5401                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5402
5403 MLX5_TXOFF_INFO(i_empw,
5404                 MLX5_TXOFF_CONFIG_INLINE |
5405                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5406
5407 MLX5_TXOFF_INFO(v_empw,
5408                 MLX5_TXOFF_CONFIG_VLAN |
5409                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5410
5411 MLX5_TXOFF_INFO(iv_empw,
5412                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5413                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_EMPW)
5414
5415 MLX5_TXOFF_INFO(full_ts_nompw,
5416                 MLX5_TXOFF_CONFIG_FULL | MLX5_TXOFF_CONFIG_TXPP)
5417
5418 MLX5_TXOFF_INFO(full_ts_nompwi,
5419                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5420                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5421                 MLX5_TXOFF_CONFIG_VLAN | MLX5_TXOFF_CONFIG_METADATA |
5422                 MLX5_TXOFF_CONFIG_TXPP)
5423
5424 MLX5_TXOFF_INFO(full_ts,
5425                 MLX5_TXOFF_CONFIG_FULL | MLX5_TXOFF_CONFIG_TXPP |
5426                 MLX5_TXOFF_CONFIG_EMPW)
5427
5428 MLX5_TXOFF_INFO(full_ts_noi,
5429                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5430                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5431                 MLX5_TXOFF_CONFIG_VLAN | MLX5_TXOFF_CONFIG_METADATA |
5432                 MLX5_TXOFF_CONFIG_TXPP | MLX5_TXOFF_CONFIG_EMPW)
5433
5434 MLX5_TXOFF_INFO(none_ts,
5435                 MLX5_TXOFF_CONFIG_NONE | MLX5_TXOFF_CONFIG_TXPP |
5436                 MLX5_TXOFF_CONFIG_EMPW)
5437
5438 MLX5_TXOFF_INFO(mdi_ts,
5439                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_METADATA |
5440                 MLX5_TXOFF_CONFIG_TXPP | MLX5_TXOFF_CONFIG_EMPW)
5441
5442 MLX5_TXOFF_INFO(mti_ts,
5443                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5444                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_METADATA |
5445                 MLX5_TXOFF_CONFIG_TXPP | MLX5_TXOFF_CONFIG_EMPW)
5446
5447 MLX5_TXOFF_INFO(mtiv_ts,
5448                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5449                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5450                 MLX5_TXOFF_CONFIG_METADATA | MLX5_TXOFF_CONFIG_TXPP |
5451                 MLX5_TXOFF_CONFIG_EMPW)
5452
5453 MLX5_TXOFF_INFO(full,
5454                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5455                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5456                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5457                 MLX5_TXOFF_CONFIG_METADATA)
5458
5459 MLX5_TXOFF_INFO(none,
5460                 MLX5_TXOFF_CONFIG_NONE)
5461
5462 MLX5_TXOFF_INFO(md,
5463                 MLX5_TXOFF_CONFIG_METADATA)
5464
5465 MLX5_TXOFF_INFO(mt,
5466                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5467                 MLX5_TXOFF_CONFIG_METADATA)
5468
5469 MLX5_TXOFF_INFO(mtsc,
5470                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5471                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5472                 MLX5_TXOFF_CONFIG_METADATA)
5473
5474 MLX5_TXOFF_INFO(mti,
5475                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5476                 MLX5_TXOFF_CONFIG_INLINE |
5477                 MLX5_TXOFF_CONFIG_METADATA)
5478
5479 MLX5_TXOFF_INFO(mtv,
5480                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5481                 MLX5_TXOFF_CONFIG_VLAN |
5482                 MLX5_TXOFF_CONFIG_METADATA)
5483
5484 MLX5_TXOFF_INFO(mtiv,
5485                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_TSO |
5486                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5487                 MLX5_TXOFF_CONFIG_METADATA)
5488
5489 MLX5_TXOFF_INFO(sc,
5490                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5491                 MLX5_TXOFF_CONFIG_METADATA)
5492
5493 MLX5_TXOFF_INFO(sci,
5494                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5495                 MLX5_TXOFF_CONFIG_INLINE |
5496                 MLX5_TXOFF_CONFIG_METADATA)
5497
5498 MLX5_TXOFF_INFO(scv,
5499                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5500                 MLX5_TXOFF_CONFIG_VLAN |
5501                 MLX5_TXOFF_CONFIG_METADATA)
5502
5503 MLX5_TXOFF_INFO(sciv,
5504                 MLX5_TXOFF_CONFIG_SWP | MLX5_TXOFF_CONFIG_CSUM |
5505                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5506                 MLX5_TXOFF_CONFIG_METADATA)
5507
5508 MLX5_TXOFF_INFO(i,
5509                 MLX5_TXOFF_CONFIG_INLINE |
5510                 MLX5_TXOFF_CONFIG_METADATA)
5511
5512 MLX5_TXOFF_INFO(v,
5513                 MLX5_TXOFF_CONFIG_VLAN |
5514                 MLX5_TXOFF_CONFIG_METADATA)
5515
5516 MLX5_TXOFF_INFO(iv,
5517                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_VLAN |
5518                 MLX5_TXOFF_CONFIG_METADATA)
5519
5520 MLX5_TXOFF_INFO(none_mpw,
5521                 MLX5_TXOFF_CONFIG_NONE | MLX5_TXOFF_CONFIG_EMPW |
5522                 MLX5_TXOFF_CONFIG_MPW)
5523
5524 MLX5_TXOFF_INFO(mci_mpw,
5525                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_CSUM |
5526                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_EMPW |
5527                 MLX5_TXOFF_CONFIG_MPW)
5528
5529 MLX5_TXOFF_INFO(mc_mpw,
5530                 MLX5_TXOFF_CONFIG_MULTI | MLX5_TXOFF_CONFIG_CSUM |
5531                 MLX5_TXOFF_CONFIG_EMPW | MLX5_TXOFF_CONFIG_MPW)
5532
5533 MLX5_TXOFF_INFO(i_mpw,
5534                 MLX5_TXOFF_CONFIG_INLINE | MLX5_TXOFF_CONFIG_EMPW |
5535                 MLX5_TXOFF_CONFIG_MPW)
5536 };
5537
5538 /**
5539  * Configure the Tx function to use. The routine checks configured
5540  * Tx offloads for the device and selects appropriate Tx burst
5541  * routine. There are multiple Tx burst routines compiled from
5542  * the same template in the most optimal way for the dedicated
5543  * Tx offloads set.
5544  *
5545  * @param dev
5546  *   Pointer to private data structure.
5547  *
5548  * @return
5549  *   Pointer to selected Tx burst function.
5550  */
5551 eth_tx_burst_t
5552 mlx5_select_tx_function(struct rte_eth_dev *dev)
5553 {
5554         struct mlx5_priv *priv = dev->data->dev_private;
5555         struct mlx5_dev_config *config = &priv->config;
5556         uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
5557         unsigned int diff = 0, olx = 0, i, m;
5558
5559         static_assert(MLX5_WQE_SIZE_MAX / MLX5_WSEG_SIZE <=
5560                       MLX5_DSEG_MAX, "invalid WQE max size");
5561         static_assert(MLX5_WQE_CSEG_SIZE == MLX5_WSEG_SIZE,
5562                       "invalid WQE Control Segment size");
5563         static_assert(MLX5_WQE_ESEG_SIZE == MLX5_WSEG_SIZE,
5564                       "invalid WQE Ethernet Segment size");
5565         static_assert(MLX5_WQE_DSEG_SIZE == MLX5_WSEG_SIZE,
5566                       "invalid WQE Data Segment size");
5567         static_assert(MLX5_WQE_SIZE == 4 * MLX5_WSEG_SIZE,
5568                       "invalid WQE size");
5569         MLX5_ASSERT(priv);
5570         if (tx_offloads & DEV_TX_OFFLOAD_MULTI_SEGS) {
5571                 /* We should support Multi-Segment Packets. */
5572                 olx |= MLX5_TXOFF_CONFIG_MULTI;
5573         }
5574         if (tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
5575                            DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
5576                            DEV_TX_OFFLOAD_GRE_TNL_TSO |
5577                            DEV_TX_OFFLOAD_IP_TNL_TSO |
5578                            DEV_TX_OFFLOAD_UDP_TNL_TSO)) {
5579                 /* We should support TCP Send Offload. */
5580                 olx |= MLX5_TXOFF_CONFIG_TSO;
5581         }
5582         if (tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
5583                            DEV_TX_OFFLOAD_UDP_TNL_TSO |
5584                            DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM)) {
5585                 /* We should support Software Parser for Tunnels. */
5586                 olx |= MLX5_TXOFF_CONFIG_SWP;
5587         }
5588         if (tx_offloads & (DEV_TX_OFFLOAD_IPV4_CKSUM |
5589                            DEV_TX_OFFLOAD_UDP_CKSUM |
5590                            DEV_TX_OFFLOAD_TCP_CKSUM |
5591                            DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM)) {
5592                 /* We should support IP/TCP/UDP Checksums. */
5593                 olx |= MLX5_TXOFF_CONFIG_CSUM;
5594         }
5595         if (tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT) {
5596                 /* We should support VLAN insertion. */
5597                 olx |= MLX5_TXOFF_CONFIG_VLAN;
5598         }
5599         if (tx_offloads & DEV_TX_OFFLOAD_SEND_ON_TIMESTAMP &&
5600             rte_mbuf_dynflag_lookup
5601                         (RTE_MBUF_DYNFLAG_TX_TIMESTAMP_NAME, NULL) > 0 &&
5602             rte_mbuf_dynfield_lookup
5603                         (RTE_MBUF_DYNFIELD_TIMESTAMP_NAME, NULL) > 0) {
5604                 /* Offload configured, dynamic entities registered. */
5605                 olx |= MLX5_TXOFF_CONFIG_TXPP;
5606         }
5607         if (priv->txqs_n && (*priv->txqs)[0]) {
5608                 struct mlx5_txq_data *txd = (*priv->txqs)[0];
5609
5610                 if (txd->inlen_send) {
5611                         /*
5612                          * Check the data inline requirements. Data inline
5613                          * is enabled on per device basis, we can check
5614                          * the first Tx queue only.
5615                          *
5616                          * If device does not support VLAN insertion in WQE
5617                          * and some queues are requested to perform VLAN
5618                          * insertion offload than inline must be enabled.
5619                          */
5620                         olx |= MLX5_TXOFF_CONFIG_INLINE;
5621                 }
5622         }
5623         if (config->mps == MLX5_MPW_ENHANCED &&
5624             config->txq_inline_min <= 0) {
5625                 /*
5626                  * The NIC supports Enhanced Multi-Packet Write
5627                  * and does not require minimal inline data.
5628                  */
5629                 olx |= MLX5_TXOFF_CONFIG_EMPW;
5630         }
5631         if (rte_flow_dynf_metadata_avail()) {
5632                 /* We should support Flow metadata. */
5633                 olx |= MLX5_TXOFF_CONFIG_METADATA;
5634         }
5635         if (config->mps == MLX5_MPW) {
5636                 /*
5637                  * The NIC supports Legacy Multi-Packet Write.
5638                  * The MLX5_TXOFF_CONFIG_MPW controls the
5639                  * descriptor building method in combination
5640                  * with MLX5_TXOFF_CONFIG_EMPW.
5641                  */
5642                 if (!(olx & (MLX5_TXOFF_CONFIG_TSO |
5643                              MLX5_TXOFF_CONFIG_SWP |
5644                              MLX5_TXOFF_CONFIG_VLAN |
5645                              MLX5_TXOFF_CONFIG_METADATA)))
5646                         olx |= MLX5_TXOFF_CONFIG_EMPW |
5647                                MLX5_TXOFF_CONFIG_MPW;
5648         }
5649         /*
5650          * Scan the routines table to find the minimal
5651          * satisfying routine with requested offloads.
5652          */
5653         m = RTE_DIM(txoff_func);
5654         for (i = 0; i < RTE_DIM(txoff_func); i++) {
5655                 unsigned int tmp;
5656
5657                 tmp = txoff_func[i].olx;
5658                 if (tmp == olx) {
5659                         /* Meets requested offloads exactly.*/
5660                         m = i;
5661                         break;
5662                 }
5663                 if ((tmp & olx) != olx) {
5664                         /* Does not meet requested offloads at all. */
5665                         continue;
5666                 }
5667                 if ((olx ^ tmp) & MLX5_TXOFF_CONFIG_MPW)
5668                         /* Do not enable legacy MPW if not configured. */
5669                         continue;
5670                 if ((olx ^ tmp) & MLX5_TXOFF_CONFIG_EMPW)
5671                         /* Do not enable eMPW if not configured. */
5672                         continue;
5673                 if ((olx ^ tmp) & MLX5_TXOFF_CONFIG_INLINE)
5674                         /* Do not enable inlining if not configured. */
5675                         continue;
5676                 if ((olx ^ tmp) & MLX5_TXOFF_CONFIG_TXPP)
5677                         /* Do not enable scheduling if not configured. */
5678                         continue;
5679                 /*
5680                  * Some routine meets the requirements.
5681                  * Check whether it has minimal amount
5682                  * of not requested offloads.
5683                  */
5684                 tmp = __builtin_popcountl(tmp & ~olx);
5685                 if (m >= RTE_DIM(txoff_func) || tmp < diff) {
5686                         /* First or better match, save and continue. */
5687                         m = i;
5688                         diff = tmp;
5689                         continue;
5690                 }
5691                 if (tmp == diff) {
5692                         tmp = txoff_func[i].olx ^ txoff_func[m].olx;
5693                         if (__builtin_ffsl(txoff_func[i].olx & ~tmp) <
5694                             __builtin_ffsl(txoff_func[m].olx & ~tmp)) {
5695                                 /* Lighter not requested offload. */
5696                                 m = i;
5697                         }
5698                 }
5699         }
5700         if (m >= RTE_DIM(txoff_func)) {
5701                 DRV_LOG(DEBUG, "port %u has no selected Tx function"
5702                                " for requested offloads %04X",
5703                                 dev->data->port_id, olx);
5704                 return NULL;
5705         }
5706         DRV_LOG(DEBUG, "port %u has selected Tx function"
5707                        " supporting offloads %04X/%04X",
5708                         dev->data->port_id, olx, txoff_func[m].olx);
5709         if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_MULTI)
5710                 DRV_LOG(DEBUG, "\tMULTI (multi segment)");
5711         if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_TSO)
5712                 DRV_LOG(DEBUG, "\tTSO   (TCP send offload)");
5713         if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_SWP)
5714                 DRV_LOG(DEBUG, "\tSWP   (software parser)");
5715         if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_CSUM)
5716                 DRV_LOG(DEBUG, "\tCSUM  (checksum offload)");
5717         if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_INLINE)
5718                 DRV_LOG(DEBUG, "\tINLIN (inline data)");
5719         if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_VLAN)
5720                 DRV_LOG(DEBUG, "\tVLANI (VLAN insertion)");
5721         if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_METADATA)
5722                 DRV_LOG(DEBUG, "\tMETAD (tx Flow metadata)");
5723         if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_TXPP)
5724                 DRV_LOG(DEBUG, "\tMETAD (tx Scheduling)");
5725         if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_EMPW) {
5726                 if (txoff_func[m].olx & MLX5_TXOFF_CONFIG_MPW)
5727                         DRV_LOG(DEBUG, "\tMPW   (Legacy MPW)");
5728                 else
5729                         DRV_LOG(DEBUG, "\tEMPW  (Enhanced MPW)");
5730         }
5731         return txoff_func[m].func;
5732 }
5733
5734 /**
5735  * DPDK callback to get the TX queue information
5736  *
5737  * @param dev
5738  *   Pointer to the device structure.
5739  *
5740  * @param tx_queue_id
5741  *   Tx queue identificator.
5742  *
5743  * @param qinfo
5744  *   Pointer to the TX queue information structure.
5745  *
5746  * @return
5747  *   None.
5748  */
5749
5750 void
5751 mlx5_txq_info_get(struct rte_eth_dev *dev, uint16_t tx_queue_id,
5752                   struct rte_eth_txq_info *qinfo)
5753 {
5754         struct mlx5_priv *priv = dev->data->dev_private;
5755         struct mlx5_txq_data *txq = (*priv->txqs)[tx_queue_id];
5756         struct mlx5_txq_ctrl *txq_ctrl =
5757                         container_of(txq, struct mlx5_txq_ctrl, txq);
5758
5759         if (!txq)
5760                 return;
5761         qinfo->nb_desc = txq->elts_s;
5762         qinfo->conf.tx_thresh.pthresh = 0;
5763         qinfo->conf.tx_thresh.hthresh = 0;
5764         qinfo->conf.tx_thresh.wthresh = 0;
5765         qinfo->conf.tx_rs_thresh = 0;
5766         qinfo->conf.tx_free_thresh = 0;
5767         qinfo->conf.tx_deferred_start = txq_ctrl ? 0 : 1;
5768         qinfo->conf.offloads = dev->data->dev_conf.txmode.offloads;
5769 }
5770
5771 /**
5772  * DPDK callback to get the TX packet burst mode information
5773  *
5774  * @param dev
5775  *   Pointer to the device structure.
5776  *
5777  * @param tx_queue_id
5778  *   Tx queue identificatior.
5779  *
5780  * @param mode
5781  *   Pointer to the burts mode information.
5782  *
5783  * @return
5784  *   0 as success, -EINVAL as failure.
5785  */
5786
5787 int
5788 mlx5_tx_burst_mode_get(struct rte_eth_dev *dev,
5789                        uint16_t tx_queue_id __rte_unused,
5790                        struct rte_eth_burst_mode *mode)
5791 {
5792         eth_tx_burst_t pkt_burst = dev->tx_pkt_burst;
5793         unsigned int i, olx;
5794
5795         for (i = 0; i < RTE_DIM(txoff_func); i++) {
5796                 if (pkt_burst == txoff_func[i].func) {
5797                         olx = txoff_func[i].olx;
5798                         snprintf(mode->info, sizeof(mode->info),
5799                                  "%s%s%s%s%s%s%s%s%s",
5800                                  (olx & MLX5_TXOFF_CONFIG_EMPW) ?
5801                                  ((olx & MLX5_TXOFF_CONFIG_MPW) ?
5802                                  "Legacy MPW" : "Enhanced MPW") : "No MPW",
5803                                  (olx & MLX5_TXOFF_CONFIG_MULTI) ?
5804                                  " + MULTI" : "",
5805                                  (olx & MLX5_TXOFF_CONFIG_TSO) ?
5806                                  " + TSO" : "",
5807                                  (olx & MLX5_TXOFF_CONFIG_SWP) ?
5808                                  " + SWP" : "",
5809                                  (olx & MLX5_TXOFF_CONFIG_CSUM) ?
5810                                  "  + CSUM" : "",
5811                                  (olx & MLX5_TXOFF_CONFIG_INLINE) ?
5812                                  " + INLINE" : "",
5813                                  (olx & MLX5_TXOFF_CONFIG_VLAN) ?
5814                                  " + VLAN" : "",
5815                                  (olx & MLX5_TXOFF_CONFIG_METADATA) ?
5816                                  " + METADATA" : "",
5817                                  (olx & MLX5_TXOFF_CONFIG_TXPP) ?
5818                                  " + TXPP" : "");
5819                         return 0;
5820                 }
5821         }
5822         return -EINVAL;
5823 }