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