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