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