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