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