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