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