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