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