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