bab9008d9b9ffb8cc7858f0deb6ea5d6a2031b4a
[dpdk.git] / drivers / net / mlx5 / mlx5_tx.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2021 6WIND S.A.
3  * Copyright 2021 Mellanox Technologies, Ltd
4  */
5
6 #ifndef RTE_PMD_MLX5_TX_H_
7 #define RTE_PMD_MLX5_TX_H_
8
9 #include <stdint.h>
10 #include <sys/queue.h>
11
12 #include <rte_mbuf.h>
13 #include <rte_mempool.h>
14 #include <rte_common.h>
15 #include <rte_spinlock.h>
16
17 #include <mlx5_common_mr.h>
18
19 #include "mlx5.h"
20 #include "mlx5_autoconf.h"
21
22 /* TX burst subroutines return codes. */
23 enum mlx5_txcmp_code {
24         MLX5_TXCMP_CODE_EXIT = 0,
25         MLX5_TXCMP_CODE_ERROR,
26         MLX5_TXCMP_CODE_SINGLE,
27         MLX5_TXCMP_CODE_MULTI,
28         MLX5_TXCMP_CODE_TSO,
29         MLX5_TXCMP_CODE_EMPW,
30 };
31
32 /*
33  * These defines are used to configure Tx burst routine option set supported
34  * at compile time. The not specified options are optimized out due to if
35  * conditions can be explicitly calculated at compile time.
36  * The offloads with bigger runtime check (require more CPU cycles toskip)
37  * overhead should have the bigger index - this is needed to select the better
38  * matching routine function if no exact match and some offloads are not
39  * actually requested.
40  */
41 #define MLX5_TXOFF_CONFIG_MULTI (1u << 0) /* Multi-segment packets.*/
42 #define MLX5_TXOFF_CONFIG_TSO (1u << 1) /* TCP send offload supported.*/
43 #define MLX5_TXOFF_CONFIG_SWP (1u << 2) /* Tunnels/SW Parser offloads.*/
44 #define MLX5_TXOFF_CONFIG_CSUM (1u << 3) /* Check Sums offloaded. */
45 #define MLX5_TXOFF_CONFIG_INLINE (1u << 4) /* Data inlining supported. */
46 #define MLX5_TXOFF_CONFIG_VLAN (1u << 5) /* VLAN insertion supported.*/
47 #define MLX5_TXOFF_CONFIG_METADATA (1u << 6) /* Flow metadata. */
48 #define MLX5_TXOFF_CONFIG_EMPW (1u << 8) /* Enhanced MPW supported.*/
49 #define MLX5_TXOFF_CONFIG_MPW (1u << 9) /* Legacy MPW supported.*/
50 #define MLX5_TXOFF_CONFIG_TXPP (1u << 10) /* Scheduling on timestamp.*/
51
52 /* The most common offloads groups. */
53 #define MLX5_TXOFF_CONFIG_NONE 0
54 #define MLX5_TXOFF_CONFIG_FULL (MLX5_TXOFF_CONFIG_MULTI | \
55                                 MLX5_TXOFF_CONFIG_TSO | \
56                                 MLX5_TXOFF_CONFIG_SWP | \
57                                 MLX5_TXOFF_CONFIG_CSUM | \
58                                 MLX5_TXOFF_CONFIG_INLINE | \
59                                 MLX5_TXOFF_CONFIG_VLAN | \
60                                 MLX5_TXOFF_CONFIG_METADATA)
61
62 #define MLX5_TXOFF_CONFIG(mask) (olx & MLX5_TXOFF_CONFIG_##mask)
63
64 #define MLX5_TXOFF_PRE_DECL(func) \
65 uint16_t mlx5_tx_burst_##func(void *txq, \
66                               struct rte_mbuf **pkts, \
67                               uint16_t pkts_n)
68
69 #define MLX5_TXOFF_DECL(func, olx) \
70 uint16_t mlx5_tx_burst_##func(void *txq, \
71                               struct rte_mbuf **pkts, \
72                               uint16_t pkts_n) \
73 { \
74         return mlx5_tx_burst_tmpl((struct mlx5_txq_data *)txq, \
75                     pkts, pkts_n, (olx)); \
76 }
77
78 /* Mbuf dynamic flag offset for inline. */
79 extern uint64_t rte_net_mlx5_dynf_inline_mask;
80 #define PKT_TX_DYNF_NOINLINE rte_net_mlx5_dynf_inline_mask
81
82 extern uint32_t mlx5_ptype_table[] __rte_cache_aligned;
83 extern uint8_t mlx5_cksum_table[1 << 10] __rte_cache_aligned;
84 extern uint8_t mlx5_swp_types_table[1 << 10] __rte_cache_aligned;
85
86 struct mlx5_txq_stats {
87 #ifdef MLX5_PMD_SOFT_COUNTERS
88         uint64_t opackets; /**< Total of successfully sent packets. */
89         uint64_t obytes; /**< Total of successfully sent bytes. */
90 #endif
91         uint64_t oerrors; /**< Total number of failed transmitted packets. */
92 };
93
94 /* TX queue send local data. */
95 __extension__
96 struct mlx5_txq_local {
97         struct mlx5_wqe *wqe_last; /* last sent WQE pointer. */
98         struct rte_mbuf *mbuf; /* first mbuf to process. */
99         uint16_t pkts_copy; /* packets copied to elts. */
100         uint16_t pkts_sent; /* packets sent. */
101         uint16_t pkts_loop; /* packets sent on loop entry. */
102         uint16_t elts_free; /* available elts remain. */
103         uint16_t wqe_free; /* available wqe remain. */
104         uint16_t mbuf_off; /* data offset in current mbuf. */
105         uint16_t mbuf_nseg; /* number of remaining mbuf. */
106         uint16_t mbuf_free; /* number of inline mbufs to free. */
107 };
108
109 /* TX queue descriptor. */
110 __extension__
111 struct mlx5_txq_data {
112         uint16_t elts_head; /* Current counter in (*elts)[]. */
113         uint16_t elts_tail; /* Counter of first element awaiting completion. */
114         uint16_t elts_comp; /* elts index since last completion request. */
115         uint16_t elts_s; /* Number of mbuf elements. */
116         uint16_t elts_m; /* Mask for mbuf elements indices. */
117         /* Fields related to elts mbuf storage. */
118         uint16_t wqe_ci; /* Consumer index for work queue. */
119         uint16_t wqe_pi; /* Producer index for work queue. */
120         uint16_t wqe_s; /* Number of WQ elements. */
121         uint16_t wqe_m; /* Mask Number for WQ elements. */
122         uint16_t wqe_comp; /* WQE index since last completion request. */
123         uint16_t wqe_thres; /* WQE threshold to request completion in CQ. */
124         /* WQ related fields. */
125         uint16_t cq_ci; /* Consumer index for completion queue. */
126         uint16_t cq_pi; /* Production index for completion queue. */
127         uint16_t cqe_s; /* Number of CQ elements. */
128         uint16_t cqe_m; /* Mask for CQ indices. */
129         /* CQ related fields. */
130         uint16_t elts_n:4; /* elts[] length (in log2). */
131         uint16_t cqe_n:4; /* Number of CQ elements (in log2). */
132         uint16_t wqe_n:4; /* Number of WQ elements (in log2). */
133         uint16_t tso_en:1; /* When set hardware TSO is enabled. */
134         uint16_t tunnel_en:1;
135         /* When set TX offload for tunneled packets are supported. */
136         uint16_t swp_en:1; /* Whether SW parser is enabled. */
137         uint16_t vlan_en:1; /* VLAN insertion in WQE is supported. */
138         uint16_t db_nc:1; /* Doorbell mapped to non-cached region. */
139         uint16_t db_heu:1; /* Doorbell heuristic write barrier. */
140         uint16_t fast_free:1; /* mbuf fast free on Tx is enabled. */
141         uint16_t inlen_send; /* Ordinary send data inline size. */
142         uint16_t inlen_empw; /* eMPW max packet size to inline. */
143         uint16_t inlen_mode; /* Minimal data length to inline. */
144         uint32_t qp_num_8s; /* QP number shifted by 8. */
145         uint64_t offloads; /* Offloads for Tx Queue. */
146         struct mlx5_mr_ctrl mr_ctrl; /* MR control descriptor. */
147         struct mlx5_wqe *wqes; /* Work queue. */
148         struct mlx5_wqe *wqes_end; /* Work queue array limit. */
149 #ifdef RTE_LIBRTE_MLX5_DEBUG
150         uint32_t *fcqs; /* Free completion queue (debug extended). */
151 #else
152         uint16_t *fcqs; /* Free completion queue. */
153 #endif
154         volatile struct mlx5_cqe *cqes; /* Completion queue. */
155         volatile uint32_t *qp_db; /* Work queue doorbell. */
156         volatile uint32_t *cq_db; /* Completion queue doorbell. */
157         uint16_t port_id; /* Port ID of device. */
158         uint16_t idx; /* Queue index. */
159         uint64_t ts_mask; /* Timestamp flag dynamic mask. */
160         int32_t ts_offset; /* Timestamp field dynamic offset. */
161         struct mlx5_dev_ctx_shared *sh; /* Shared context. */
162         struct mlx5_txq_stats stats; /* TX queue counters. */
163 #ifndef RTE_ARCH_64
164         rte_spinlock_t *uar_lock;
165         /* UAR access lock required for 32bit implementations */
166 #endif
167         struct rte_mbuf *elts[0];
168         /* Storage for queued packets, must be the last field. */
169 } __rte_cache_aligned;
170
171 enum mlx5_txq_type {
172         MLX5_TXQ_TYPE_STANDARD, /* Standard Tx queue. */
173         MLX5_TXQ_TYPE_HAIRPIN, /* Hairpin Tx queue. */
174 };
175
176 /* TX queue control descriptor. */
177 struct mlx5_txq_ctrl {
178         LIST_ENTRY(mlx5_txq_ctrl) next; /* Pointer to the next element. */
179         uint32_t refcnt; /* Reference counter. */
180         unsigned int socket; /* CPU socket ID for allocations. */
181         enum mlx5_txq_type type; /* The txq ctrl type. */
182         unsigned int max_inline_data; /* Max inline data. */
183         unsigned int max_tso_header; /* Max TSO header size. */
184         struct mlx5_txq_obj *obj; /* Verbs/DevX queue object. */
185         struct mlx5_priv *priv; /* Back pointer to private data. */
186         off_t uar_mmap_offset; /* UAR mmap offset for non-primary process. */
187         void *bf_reg; /* BlueFlame register from Verbs. */
188         uint16_t dump_file_n; /* Number of dump files. */
189         struct rte_eth_hairpin_conf hairpin_conf; /* Hairpin configuration. */
190         uint32_t hairpin_status; /* Hairpin binding status. */
191         struct mlx5_txq_data txq; /* Data path structure. */
192         /* Must be the last field in the structure, contains elts[]. */
193 };
194
195 /* mlx5_txq.c */
196
197 int mlx5_tx_queue_start(struct rte_eth_dev *dev, uint16_t queue_id);
198 int mlx5_tx_queue_stop(struct rte_eth_dev *dev, uint16_t queue_id);
199 int mlx5_tx_queue_start_primary(struct rte_eth_dev *dev, uint16_t queue_id);
200 int mlx5_tx_queue_stop_primary(struct rte_eth_dev *dev, uint16_t queue_id);
201 int mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
202                         unsigned int socket, const struct rte_eth_txconf *conf);
203 int mlx5_tx_hairpin_queue_setup
204         (struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
205          const struct rte_eth_hairpin_conf *hairpin_conf);
206 void mlx5_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid);
207 void txq_uar_init(struct mlx5_txq_ctrl *txq_ctrl);
208 int mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd);
209 void mlx5_tx_uar_uninit_secondary(struct rte_eth_dev *dev);
210 int mlx5_txq_obj_verify(struct rte_eth_dev *dev);
211 struct mlx5_txq_ctrl *mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx,
212                                    uint16_t desc, unsigned int socket,
213                                    const struct rte_eth_txconf *conf);
214 struct mlx5_txq_ctrl *mlx5_txq_hairpin_new
215         (struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
216          const struct rte_eth_hairpin_conf *hairpin_conf);
217 struct mlx5_txq_ctrl *mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx);
218 int mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx);
219 int mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx);
220 int mlx5_txq_verify(struct rte_eth_dev *dev);
221 void txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl);
222 void txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl);
223 uint64_t mlx5_get_tx_port_offloads(struct rte_eth_dev *dev);
224 void mlx5_txq_dynf_timestamp_set(struct rte_eth_dev *dev);
225
226 /* mlx5_tx.c */
227
228 uint16_t removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts,
229                           uint16_t pkts_n);
230 void mlx5_tx_handle_completion(struct mlx5_txq_data *__rte_restrict txq,
231                                unsigned int olx __rte_unused);
232 int mlx5_tx_descriptor_status(void *tx_queue, uint16_t offset);
233 void mlx5_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
234                        struct rte_eth_txq_info *qinfo);
235 int mlx5_tx_burst_mode_get(struct rte_eth_dev *dev, uint16_t tx_queue_id,
236                            struct rte_eth_burst_mode *mode);
237
238 /* mlx5_mr.c */
239
240 uint32_t mlx5_tx_mb2mr_bh(struct mlx5_txq_data *txq, struct rte_mbuf *mb);
241
242 /* mlx5_tx_empw.c */
243
244 MLX5_TXOFF_PRE_DECL(full_empw);
245 MLX5_TXOFF_PRE_DECL(none_empw);
246 MLX5_TXOFF_PRE_DECL(md_empw);
247 MLX5_TXOFF_PRE_DECL(mt_empw);
248 MLX5_TXOFF_PRE_DECL(mtsc_empw);
249 MLX5_TXOFF_PRE_DECL(mti_empw);
250 MLX5_TXOFF_PRE_DECL(mtv_empw);
251 MLX5_TXOFF_PRE_DECL(mtiv_empw);
252 MLX5_TXOFF_PRE_DECL(sc_empw);
253 MLX5_TXOFF_PRE_DECL(sci_empw);
254 MLX5_TXOFF_PRE_DECL(scv_empw);
255 MLX5_TXOFF_PRE_DECL(sciv_empw);
256 MLX5_TXOFF_PRE_DECL(i_empw);
257 MLX5_TXOFF_PRE_DECL(v_empw);
258 MLX5_TXOFF_PRE_DECL(iv_empw);
259
260 /* mlx5_tx_nompw.c */
261
262 MLX5_TXOFF_PRE_DECL(full);
263 MLX5_TXOFF_PRE_DECL(none);
264 MLX5_TXOFF_PRE_DECL(md);
265 MLX5_TXOFF_PRE_DECL(mt);
266 MLX5_TXOFF_PRE_DECL(mtsc);
267 MLX5_TXOFF_PRE_DECL(mti);
268 MLX5_TXOFF_PRE_DECL(mtv);
269 MLX5_TXOFF_PRE_DECL(mtiv);
270 MLX5_TXOFF_PRE_DECL(sc);
271 MLX5_TXOFF_PRE_DECL(sci);
272 MLX5_TXOFF_PRE_DECL(scv);
273 MLX5_TXOFF_PRE_DECL(sciv);
274 MLX5_TXOFF_PRE_DECL(i);
275 MLX5_TXOFF_PRE_DECL(v);
276 MLX5_TXOFF_PRE_DECL(iv);
277
278 /* mlx5_tx_txpp.c */
279
280 MLX5_TXOFF_PRE_DECL(full_ts_nompw);
281 MLX5_TXOFF_PRE_DECL(full_ts_nompwi);
282 MLX5_TXOFF_PRE_DECL(full_ts);
283 MLX5_TXOFF_PRE_DECL(full_ts_noi);
284 MLX5_TXOFF_PRE_DECL(none_ts);
285 MLX5_TXOFF_PRE_DECL(mdi_ts);
286 MLX5_TXOFF_PRE_DECL(mti_ts);
287 MLX5_TXOFF_PRE_DECL(mtiv_ts);
288
289 /* mlx5_tx_mpw.c */
290
291 MLX5_TXOFF_PRE_DECL(none_mpw);
292 MLX5_TXOFF_PRE_DECL(mci_mpw);
293 MLX5_TXOFF_PRE_DECL(mc_mpw);
294 MLX5_TXOFF_PRE_DECL(i_mpw);
295
296 static __rte_always_inline uint64_t *
297 mlx5_tx_bfreg(struct mlx5_txq_data *txq)
298 {
299         return MLX5_PROC_PRIV(txq->port_id)->uar_table[txq->idx];
300 }
301
302 /**
303  * Provide safe 64bit store operation to mlx5 UAR region for both 32bit and
304  * 64bit architectures.
305  *
306  * @param val
307  *   value to write in CPU endian format.
308  * @param addr
309  *   Address to write to.
310  * @param lock
311  *   Address of the lock to use for that UAR access.
312  */
313 static __rte_always_inline void
314 __mlx5_uar_write64_relaxed(uint64_t val, void *addr,
315                            rte_spinlock_t *lock __rte_unused)
316 {
317 #ifdef RTE_ARCH_64
318         *(uint64_t *)addr = val;
319 #else /* !RTE_ARCH_64 */
320         rte_spinlock_lock(lock);
321         *(uint32_t *)addr = val;
322         rte_io_wmb();
323         *((uint32_t *)addr + 1) = val >> 32;
324         rte_spinlock_unlock(lock);
325 #endif
326 }
327
328 /**
329  * Provide safe 64bit store operation to mlx5 UAR region for both 32bit and
330  * 64bit architectures while guaranteeing the order of execution with the
331  * code being executed.
332  *
333  * @param val
334  *   value to write in CPU endian format.
335  * @param addr
336  *   Address to write to.
337  * @param lock
338  *   Address of the lock to use for that UAR access.
339  */
340 static __rte_always_inline void
341 __mlx5_uar_write64(uint64_t val, void *addr, rte_spinlock_t *lock)
342 {
343         rte_io_wmb();
344         __mlx5_uar_write64_relaxed(val, addr, lock);
345 }
346
347 /* Assist macros, used instead of directly calling the functions they wrap. */
348 #ifdef RTE_ARCH_64
349 #define mlx5_uar_write64_relaxed(val, dst, lock) \
350                 __mlx5_uar_write64_relaxed(val, dst, NULL)
351 #define mlx5_uar_write64(val, dst, lock) __mlx5_uar_write64(val, dst, NULL)
352 #else
353 #define mlx5_uar_write64_relaxed(val, dst, lock) \
354                 __mlx5_uar_write64_relaxed(val, dst, lock)
355 #define mlx5_uar_write64(val, dst, lock) __mlx5_uar_write64(val, dst, lock)
356 #endif
357
358 /**
359  * Query LKey from a packet buffer for Tx. If not found, add the mempool.
360  *
361  * @param txq
362  *   Pointer to Tx queue structure.
363  * @param addr
364  *   Address to search.
365  *
366  * @return
367  *   Searched LKey on success, UINT32_MAX on no match.
368  */
369 static __rte_always_inline uint32_t
370 mlx5_tx_mb2mr(struct mlx5_txq_data *txq, struct rte_mbuf *mb)
371 {
372         struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
373         uintptr_t addr = (uintptr_t)mb->buf_addr;
374         uint32_t lkey;
375
376         /* Check generation bit to see if there's any change on existing MRs. */
377         if (unlikely(*mr_ctrl->dev_gen_ptr != mr_ctrl->cur_gen))
378                 mlx5_mr_flush_local_cache(mr_ctrl);
379         /* Linear search on MR cache array. */
380         lkey = mlx5_mr_lookup_lkey(mr_ctrl->cache, &mr_ctrl->mru,
381                                    MLX5_MR_CACHE_N, addr);
382         if (likely(lkey != UINT32_MAX))
383                 return lkey;
384         /* Take slower bottom-half on miss. */
385         return mlx5_tx_mb2mr_bh(txq, mb);
386 }
387
388 /**
389  * Ring TX queue doorbell and flush the update if requested.
390  *
391  * @param txq
392  *   Pointer to TX queue structure.
393  * @param wqe
394  *   Pointer to the last WQE posted in the NIC.
395  * @param cond
396  *   Request for write memory barrier after BlueFlame update.
397  */
398 static __rte_always_inline void
399 mlx5_tx_dbrec_cond_wmb(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe,
400                        int cond)
401 {
402         uint64_t *dst = mlx5_tx_bfreg(txq);
403         volatile uint64_t *src = ((volatile uint64_t *)wqe);
404
405         rte_io_wmb();
406         *txq->qp_db = rte_cpu_to_be_32(txq->wqe_ci);
407         /* Ensure ordering between DB record and BF copy. */
408         rte_wmb();
409         mlx5_uar_write64_relaxed(*src, dst, txq->uar_lock);
410         if (cond)
411                 rte_wmb();
412 }
413
414 /**
415  * Ring TX queue doorbell and flush the update by write memory barrier.
416  *
417  * @param txq
418  *   Pointer to TX queue structure.
419  * @param wqe
420  *   Pointer to the last WQE posted in the NIC.
421  */
422 static __rte_always_inline void
423 mlx5_tx_dbrec(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe)
424 {
425         mlx5_tx_dbrec_cond_wmb(txq, wqe, 1);
426 }
427
428 /**
429  * Convert timestamp from mbuf format to linear counter
430  * of Clock Queue completions (24 bits).
431  *
432  * @param sh
433  *   Pointer to the device shared context to fetch Tx
434  *   packet pacing timestamp and parameters.
435  * @param ts
436  *   Timestamp from mbuf to convert.
437  * @return
438  *   positive or zero value - completion ID to wait.
439  *   negative value - conversion error.
440  */
441 static __rte_always_inline int32_t
442 mlx5_txpp_convert_tx_ts(struct mlx5_dev_ctx_shared *sh, uint64_t mts)
443 {
444         uint64_t ts, ci;
445         uint32_t tick;
446
447         do {
448                 /*
449                  * Read atomically two uint64_t fields and compare lsb bits.
450                  * It there is no match - the timestamp was updated in
451                  * the service thread, data should be re-read.
452                  */
453                 rte_compiler_barrier();
454                 ci = __atomic_load_n(&sh->txpp.ts.ci_ts, __ATOMIC_RELAXED);
455                 ts = __atomic_load_n(&sh->txpp.ts.ts, __ATOMIC_RELAXED);
456                 rte_compiler_barrier();
457                 if (!((ts ^ ci) << (64 - MLX5_CQ_INDEX_WIDTH)))
458                         break;
459         } while (true);
460         /* Perform the skew correction, positive value to send earlier. */
461         mts -= sh->txpp.skew;
462         mts -= ts;
463         if (unlikely(mts >= UINT64_MAX / 2)) {
464                 /* We have negative integer, mts is in the past. */
465                 __atomic_fetch_add(&sh->txpp.err_ts_past,
466                                    1, __ATOMIC_RELAXED);
467                 return -1;
468         }
469         tick = sh->txpp.tick;
470         MLX5_ASSERT(tick);
471         /* Convert delta to completions, round up. */
472         mts = (mts + tick - 1) / tick;
473         if (unlikely(mts >= (1 << MLX5_CQ_INDEX_WIDTH) / 2 - 1)) {
474                 /* We have mts is too distant future. */
475                 __atomic_fetch_add(&sh->txpp.err_ts_future,
476                                    1, __ATOMIC_RELAXED);
477                 return -1;
478         }
479         mts <<= 64 - MLX5_CQ_INDEX_WIDTH;
480         ci += mts;
481         ci >>= 64 - MLX5_CQ_INDEX_WIDTH;
482         return ci;
483 }
484
485 /**
486  * Set Software Parser flags and offsets in Ethernet Segment of WQE.
487  * Flags must be preliminary initialized to zero.
488  *
489  * @param loc
490  *   Pointer to burst routine local context.
491  * @param swp_flags
492  *   Pointer to store Software Parser flags.
493  * @param olx
494  *   Configured Tx offloads mask. It is fully defined at
495  *   compile time and may be used for optimization.
496  *
497  * @return
498  *   Software Parser offsets packed in dword.
499  *   Software Parser flags are set by pointer.
500  */
501 static __rte_always_inline uint32_t
502 txq_mbuf_to_swp(struct mlx5_txq_local *__rte_restrict loc,
503                 uint8_t *swp_flags,
504                 unsigned int olx)
505 {
506         uint64_t ol, tunnel;
507         unsigned int idx, off;
508         uint32_t set;
509
510         if (!MLX5_TXOFF_CONFIG(SWP))
511                 return 0;
512         ol = loc->mbuf->ol_flags;
513         tunnel = ol & PKT_TX_TUNNEL_MASK;
514         /*
515          * Check whether Software Parser is required.
516          * Only customized tunnels may ask for.
517          */
518         if (likely(tunnel != PKT_TX_TUNNEL_UDP && tunnel != PKT_TX_TUNNEL_IP))
519                 return 0;
520         /*
521          * The index should have:
522          * bit[0:1] = PKT_TX_L4_MASK
523          * bit[4] = PKT_TX_IPV6
524          * bit[8] = PKT_TX_OUTER_IPV6
525          * bit[9] = PKT_TX_OUTER_UDP
526          */
527         idx = (ol & (PKT_TX_L4_MASK | PKT_TX_IPV6 | PKT_TX_OUTER_IPV6)) >> 52;
528         idx |= (tunnel == PKT_TX_TUNNEL_UDP) ? (1 << 9) : 0;
529         *swp_flags = mlx5_swp_types_table[idx];
530         /*
531          * Set offsets for SW parser. Since ConnectX-5, SW parser just
532          * complements HW parser. SW parser starts to engage only if HW parser
533          * can't reach a header. For the older devices, HW parser will not kick
534          * in if any of SWP offsets is set. Therefore, all of the L3 offsets
535          * should be set regardless of HW offload.
536          */
537         off = loc->mbuf->outer_l2_len;
538         if (MLX5_TXOFF_CONFIG(VLAN) && ol & PKT_TX_VLAN_PKT)
539                 off += sizeof(struct rte_vlan_hdr);
540         set = (off >> 1) << 8; /* Outer L3 offset. */
541         off += loc->mbuf->outer_l3_len;
542         if (tunnel == PKT_TX_TUNNEL_UDP)
543                 set |= off >> 1; /* Outer L4 offset. */
544         if (ol & (PKT_TX_IPV4 | PKT_TX_IPV6)) { /* Inner IP. */
545                 const uint64_t csum = ol & PKT_TX_L4_MASK;
546                         off += loc->mbuf->l2_len;
547                 set |= (off >> 1) << 24; /* Inner L3 offset. */
548                 if (csum == PKT_TX_TCP_CKSUM ||
549                     csum == PKT_TX_UDP_CKSUM ||
550                     (MLX5_TXOFF_CONFIG(TSO) && ol & PKT_TX_TCP_SEG)) {
551                         off += loc->mbuf->l3_len;
552                         set |= (off >> 1) << 16; /* Inner L4 offset. */
553                 }
554         }
555         set = rte_cpu_to_le_32(set);
556         return set;
557 }
558
559 /**
560  * Convert the Checksum offloads to Verbs.
561  *
562  * @param buf
563  *   Pointer to the mbuf.
564  *
565  * @return
566  *   Converted checksum flags.
567  */
568 static __rte_always_inline uint8_t
569 txq_ol_cksum_to_cs(struct rte_mbuf *buf)
570 {
571         uint32_t idx;
572         uint8_t is_tunnel = !!(buf->ol_flags & PKT_TX_TUNNEL_MASK);
573         const uint64_t ol_flags_mask = PKT_TX_TCP_SEG | PKT_TX_L4_MASK |
574                                        PKT_TX_IP_CKSUM | PKT_TX_OUTER_IP_CKSUM;
575
576         /*
577          * The index should have:
578          * bit[0] = PKT_TX_TCP_SEG
579          * bit[2:3] = PKT_TX_UDP_CKSUM, PKT_TX_TCP_CKSUM
580          * bit[4] = PKT_TX_IP_CKSUM
581          * bit[8] = PKT_TX_OUTER_IP_CKSUM
582          * bit[9] = tunnel
583          */
584         idx = ((buf->ol_flags & ol_flags_mask) >> 50) | (!!is_tunnel << 9);
585         return mlx5_cksum_table[idx];
586 }
587
588 /**
589  * Free the mbufs from the linear array of pointers.
590  *
591  * @param txq
592  *   Pointer to Tx queue structure.
593  * @param pkts
594  *   Pointer to array of packets to be free.
595  * @param pkts_n
596  *   Number of packets to be freed.
597  * @param olx
598  *   Configured Tx offloads mask. It is fully defined at
599  *   compile time and may be used for optimization.
600  */
601 static __rte_always_inline void
602 mlx5_tx_free_mbuf(struct mlx5_txq_data *__rte_restrict txq,
603                   struct rte_mbuf **__rte_restrict pkts,
604                   unsigned int pkts_n,
605                   unsigned int olx __rte_unused)
606 {
607         struct rte_mempool *pool = NULL;
608         struct rte_mbuf **p_free = NULL;
609         struct rte_mbuf *mbuf;
610         unsigned int n_free = 0;
611
612         /*
613          * The implemented algorithm eliminates
614          * copying pointers to temporary array
615          * for rte_mempool_put_bulk() calls.
616          */
617         MLX5_ASSERT(pkts);
618         MLX5_ASSERT(pkts_n);
619         /*
620          * Free mbufs directly to the pool in bulk
621          * if fast free offload is engaged
622          */
623         if (!MLX5_TXOFF_CONFIG(MULTI) && txq->fast_free) {
624                 mbuf = *pkts;
625                 pool = mbuf->pool;
626                 rte_mempool_put_bulk(pool, (void *)pkts, pkts_n);
627                 return;
628         }
629         for (;;) {
630                 for (;;) {
631                         /*
632                          * Decrement mbuf reference counter, detach
633                          * indirect and external buffers if needed.
634                          */
635                         mbuf = rte_pktmbuf_prefree_seg(*pkts);
636                         if (likely(mbuf != NULL)) {
637                                 MLX5_ASSERT(mbuf == *pkts);
638                                 if (likely(n_free != 0)) {
639                                         if (unlikely(pool != mbuf->pool))
640                                                 /* From different pool. */
641                                                 break;
642                                 } else {
643                                         /* Start new scan array. */
644                                         pool = mbuf->pool;
645                                         p_free = pkts;
646                                 }
647                                 ++n_free;
648                                 ++pkts;
649                                 --pkts_n;
650                                 if (unlikely(pkts_n == 0)) {
651                                         mbuf = NULL;
652                                         break;
653                                 }
654                         } else {
655                                 /*
656                                  * This happens if mbuf is still referenced.
657                                  * We can't put it back to the pool, skip.
658                                  */
659                                 ++pkts;
660                                 --pkts_n;
661                                 if (unlikely(n_free != 0))
662                                         /* There is some array to free.*/
663                                         break;
664                                 if (unlikely(pkts_n == 0))
665                                         /* Last mbuf, nothing to free. */
666                                         return;
667                         }
668                 }
669                 for (;;) {
670                         /*
671                          * This loop is implemented to avoid multiple
672                          * inlining of rte_mempool_put_bulk().
673                          */
674                         MLX5_ASSERT(pool);
675                         MLX5_ASSERT(p_free);
676                         MLX5_ASSERT(n_free);
677                         /*
678                          * Free the array of pre-freed mbufs
679                          * belonging to the same memory pool.
680                          */
681                         rte_mempool_put_bulk(pool, (void *)p_free, n_free);
682                         if (unlikely(mbuf != NULL)) {
683                                 /* There is the request to start new scan. */
684                                 pool = mbuf->pool;
685                                 p_free = pkts++;
686                                 n_free = 1;
687                                 --pkts_n;
688                                 if (likely(pkts_n != 0))
689                                         break;
690                                 /*
691                                  * This is the last mbuf to be freed.
692                                  * Do one more loop iteration to complete.
693                                  * This is rare case of the last unique mbuf.
694                                  */
695                                 mbuf = NULL;
696                                 continue;
697                         }
698                         if (likely(pkts_n == 0))
699                                 return;
700                         n_free = 0;
701                         break;
702                 }
703         }
704 }
705
706 /**
707  * No inline version to free buffers for optimal call
708  * on the tx_burst completion.
709  */
710 static __rte_noinline void
711 __mlx5_tx_free_mbuf(struct mlx5_txq_data *__rte_restrict txq,
712                     struct rte_mbuf **__rte_restrict pkts,
713                     unsigned int pkts_n,
714                     unsigned int olx __rte_unused)
715 {
716         mlx5_tx_free_mbuf(txq, pkts, pkts_n, olx);
717 }
718
719 /**
720  * Free the mbuf from the elts ring buffer till new tail.
721  *
722  * @param txq
723  *   Pointer to Tx queue structure.
724  * @param tail
725  *   Index in elts to free up to, becomes new elts tail.
726  * @param olx
727  *   Configured Tx offloads mask. It is fully defined at
728  *   compile time and may be used for optimization.
729  */
730 static __rte_always_inline void
731 mlx5_tx_free_elts(struct mlx5_txq_data *__rte_restrict txq,
732                   uint16_t tail,
733                   unsigned int olx __rte_unused)
734 {
735         uint16_t n_elts = tail - txq->elts_tail;
736
737         MLX5_ASSERT(n_elts);
738         MLX5_ASSERT(n_elts <= txq->elts_s);
739         /*
740          * Implement a loop to support ring buffer wraparound
741          * with single inlining of mlx5_tx_free_mbuf().
742          */
743         do {
744                 unsigned int part;
745
746                 part = txq->elts_s - (txq->elts_tail & txq->elts_m);
747                 part = RTE_MIN(part, n_elts);
748                 MLX5_ASSERT(part);
749                 MLX5_ASSERT(part <= txq->elts_s);
750                 mlx5_tx_free_mbuf(txq,
751                                   &txq->elts[txq->elts_tail & txq->elts_m],
752                                   part, olx);
753                 txq->elts_tail += part;
754                 n_elts -= part;
755         } while (n_elts);
756 }
757
758 /**
759  * Store the mbuf being sent into elts ring buffer.
760  * On Tx completion these mbufs will be freed.
761  *
762  * @param txq
763  *   Pointer to Tx queue structure.
764  * @param pkts
765  *   Pointer to array of packets to be stored.
766  * @param pkts_n
767  *   Number of packets to be stored.
768  * @param olx
769  *   Configured Tx offloads mask. It is fully defined at
770  *   compile time and may be used for optimization.
771  */
772 static __rte_always_inline void
773 mlx5_tx_copy_elts(struct mlx5_txq_data *__rte_restrict txq,
774                   struct rte_mbuf **__rte_restrict pkts,
775                   unsigned int pkts_n,
776                   unsigned int olx __rte_unused)
777 {
778         unsigned int part;
779         struct rte_mbuf **elts = (struct rte_mbuf **)txq->elts;
780
781         MLX5_ASSERT(pkts);
782         MLX5_ASSERT(pkts_n);
783         part = txq->elts_s - (txq->elts_head & txq->elts_m);
784         MLX5_ASSERT(part);
785         MLX5_ASSERT(part <= txq->elts_s);
786         /* This code is a good candidate for vectorizing with SIMD. */
787         rte_memcpy((void *)(elts + (txq->elts_head & txq->elts_m)),
788                    (void *)pkts,
789                    RTE_MIN(part, pkts_n) * sizeof(struct rte_mbuf *));
790         txq->elts_head += pkts_n;
791         if (unlikely(part < pkts_n))
792                 /* The copy is wrapping around the elts array. */
793                 rte_memcpy((void *)elts, (void *)(pkts + part),
794                            (pkts_n - part) * sizeof(struct rte_mbuf *));
795 }
796
797 /**
798  * Check if the completion request flag should be set in the last WQE.
799  * Both pushed mbufs and WQEs are monitored and the completion request
800  * flag is set if any of thresholds is reached.
801  *
802  * @param txq
803  *   Pointer to TX queue structure.
804  * @param loc
805  *   Pointer to burst routine local context.
806  * @param olx
807  *   Configured Tx offloads mask. It is fully defined at
808  *   compile time and may be used for optimization.
809  */
810 static __rte_always_inline void
811 mlx5_tx_request_completion(struct mlx5_txq_data *__rte_restrict txq,
812                            struct mlx5_txq_local *__rte_restrict loc,
813                            unsigned int olx)
814 {
815         uint16_t head = txq->elts_head;
816         unsigned int part;
817
818         part = MLX5_TXOFF_CONFIG(INLINE) ?
819                0 : loc->pkts_sent - loc->pkts_copy;
820         head += part;
821         if ((uint16_t)(head - txq->elts_comp) >= MLX5_TX_COMP_THRESH ||
822              (MLX5_TXOFF_CONFIG(INLINE) &&
823              (uint16_t)(txq->wqe_ci - txq->wqe_comp) >= txq->wqe_thres)) {
824                 volatile struct mlx5_wqe *last = loc->wqe_last;
825
826                 MLX5_ASSERT(last);
827                 txq->elts_comp = head;
828                 if (MLX5_TXOFF_CONFIG(INLINE))
829                         txq->wqe_comp = txq->wqe_ci;
830                 /* Request unconditional completion on last WQE. */
831                 last->cseg.flags = RTE_BE32(MLX5_COMP_ALWAYS <<
832                                             MLX5_COMP_MODE_OFFSET);
833                 /* Save elts_head in dedicated free on completion queue. */
834 #ifdef RTE_LIBRTE_MLX5_DEBUG
835                 txq->fcqs[txq->cq_pi++ & txq->cqe_m] = head |
836                           (last->cseg.opcode >> 8) << 16;
837 #else
838                 txq->fcqs[txq->cq_pi++ & txq->cqe_m] = head;
839 #endif
840                 /* A CQE slot must always be available. */
841                 MLX5_ASSERT((txq->cq_pi - txq->cq_ci) <= txq->cqe_s);
842         }
843 }
844
845 /**
846  * Build the Control Segment with specified opcode:
847  * - MLX5_OPCODE_SEND
848  * - MLX5_OPCODE_ENHANCED_MPSW
849  * - MLX5_OPCODE_TSO
850  *
851  * @param txq
852  *   Pointer to TX queue structure.
853  * @param loc
854  *   Pointer to burst routine local context.
855  * @param wqe
856  *   Pointer to WQE to fill with built Control Segment.
857  * @param ds
858  *   Supposed length of WQE in segments.
859  * @param opcode
860  *   SQ WQE opcode to put into Control Segment.
861  * @param olx
862  *   Configured Tx offloads mask. It is fully defined at
863  *   compile time and may be used for optimization.
864  */
865 static __rte_always_inline void
866 mlx5_tx_cseg_init(struct mlx5_txq_data *__rte_restrict txq,
867                   struct mlx5_txq_local *__rte_restrict loc __rte_unused,
868                   struct mlx5_wqe *__rte_restrict wqe,
869                   unsigned int ds,
870                   unsigned int opcode,
871                   unsigned int olx __rte_unused)
872 {
873         struct mlx5_wqe_cseg *__rte_restrict cs = &wqe->cseg;
874
875         /* For legacy MPW replace the EMPW by TSO with modifier. */
876         if (MLX5_TXOFF_CONFIG(MPW) && opcode == MLX5_OPCODE_ENHANCED_MPSW)
877                 opcode = MLX5_OPCODE_TSO | MLX5_OPC_MOD_MPW << 24;
878         cs->opcode = rte_cpu_to_be_32((txq->wqe_ci << 8) | opcode);
879         cs->sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | ds);
880         cs->flags = RTE_BE32(MLX5_COMP_ONLY_FIRST_ERR <<
881                              MLX5_COMP_MODE_OFFSET);
882         cs->misc = RTE_BE32(0);
883 }
884
885 /**
886  * Build the Synchronize Queue Segment with specified completion index.
887  *
888  * @param txq
889  *   Pointer to TX queue structure.
890  * @param loc
891  *   Pointer to burst routine local context.
892  * @param wqe
893  *   Pointer to WQE to fill with built Control Segment.
894  * @param wci
895  *   Completion index in Clock Queue to wait.
896  * @param olx
897  *   Configured Tx offloads mask. It is fully defined at
898  *   compile time and may be used for optimization.
899  */
900 static __rte_always_inline void
901 mlx5_tx_wseg_init(struct mlx5_txq_data *restrict txq,
902                   struct mlx5_txq_local *restrict loc __rte_unused,
903                   struct mlx5_wqe *restrict wqe,
904                   unsigned int wci,
905                   unsigned int olx __rte_unused)
906 {
907         struct mlx5_wqe_qseg *qs;
908
909         qs = RTE_PTR_ADD(wqe, MLX5_WSEG_SIZE);
910         qs->max_index = rte_cpu_to_be_32(wci);
911         qs->qpn_cqn = rte_cpu_to_be_32(txq->sh->txpp.clock_queue.cq_obj.cq->id);
912         qs->reserved0 = RTE_BE32(0);
913         qs->reserved1 = RTE_BE32(0);
914 }
915
916 /**
917  * Build the Ethernet Segment without inlined data.
918  * Supports Software Parser, Checksums and VLAN insertion Tx offload features.
919  *
920  * @param txq
921  *   Pointer to TX queue structure.
922  * @param loc
923  *   Pointer to burst routine local context.
924  * @param wqe
925  *   Pointer to WQE to fill with built Ethernet Segment.
926  * @param olx
927  *   Configured Tx offloads mask. It is fully defined at
928  *   compile time and may be used for optimization.
929  */
930 static __rte_always_inline void
931 mlx5_tx_eseg_none(struct mlx5_txq_data *__rte_restrict txq __rte_unused,
932                   struct mlx5_txq_local *__rte_restrict loc,
933                   struct mlx5_wqe *__rte_restrict wqe,
934                   unsigned int olx)
935 {
936         struct mlx5_wqe_eseg *__rte_restrict es = &wqe->eseg;
937         uint32_t csum;
938
939         /*
940          * Calculate and set check sum flags first, dword field
941          * in segment may be shared with Software Parser flags.
942          */
943         csum = MLX5_TXOFF_CONFIG(CSUM) ? txq_ol_cksum_to_cs(loc->mbuf) : 0;
944         es->flags = rte_cpu_to_le_32(csum);
945         /*
946          * Calculate and set Software Parser offsets and flags.
947          * These flags a set for custom UDP and IP tunnel packets.
948          */
949         es->swp_offs = txq_mbuf_to_swp(loc, &es->swp_flags, olx);
950         /* Fill metadata field if needed. */
951         es->metadata = MLX5_TXOFF_CONFIG(METADATA) ?
952                        loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
953                        rte_cpu_to_be_32(*RTE_FLOW_DYNF_METADATA(loc->mbuf)) :
954                        0 : 0;
955         /* Engage VLAN tag insertion feature if requested. */
956         if (MLX5_TXOFF_CONFIG(VLAN) &&
957             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
958                 /*
959                  * We should get here only if device support
960                  * this feature correctly.
961                  */
962                 MLX5_ASSERT(txq->vlan_en);
963                 es->inline_hdr = rte_cpu_to_be_32(MLX5_ETH_WQE_VLAN_INSERT |
964                                                   loc->mbuf->vlan_tci);
965         } else {
966                 es->inline_hdr = RTE_BE32(0);
967         }
968 }
969
970 /**
971  * Build the Ethernet Segment with minimal inlined data
972  * of MLX5_ESEG_MIN_INLINE_SIZE bytes length. This is
973  * used to fill the gap in single WQEBB WQEs.
974  * Supports Software Parser, Checksums and VLAN
975  * insertion Tx offload features.
976  *
977  * @param txq
978  *   Pointer to TX queue structure.
979  * @param loc
980  *   Pointer to burst routine local context.
981  * @param wqe
982  *   Pointer to WQE to fill with built Ethernet Segment.
983  * @param vlan
984  *   Length of VLAN tag insertion if any.
985  * @param olx
986  *   Configured Tx offloads mask. It is fully defined at
987  *   compile time and may be used for optimization.
988  */
989 static __rte_always_inline void
990 mlx5_tx_eseg_dmin(struct mlx5_txq_data *__rte_restrict txq __rte_unused,
991                   struct mlx5_txq_local *__rte_restrict loc,
992                   struct mlx5_wqe *__rte_restrict wqe,
993                   unsigned int vlan,
994                   unsigned int olx)
995 {
996         struct mlx5_wqe_eseg *__rte_restrict es = &wqe->eseg;
997         uint32_t csum;
998         uint8_t *psrc, *pdst;
999
1000         /*
1001          * Calculate and set check sum flags first, dword field
1002          * in segment may be shared with Software Parser flags.
1003          */
1004         csum = MLX5_TXOFF_CONFIG(CSUM) ? txq_ol_cksum_to_cs(loc->mbuf) : 0;
1005         es->flags = rte_cpu_to_le_32(csum);
1006         /*
1007          * Calculate and set Software Parser offsets and flags.
1008          * These flags a set for custom UDP and IP tunnel packets.
1009          */
1010         es->swp_offs = txq_mbuf_to_swp(loc, &es->swp_flags, olx);
1011         /* Fill metadata field if needed. */
1012         es->metadata = MLX5_TXOFF_CONFIG(METADATA) ?
1013                        loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
1014                        rte_cpu_to_be_32(*RTE_FLOW_DYNF_METADATA(loc->mbuf)) :
1015                        0 : 0;
1016         psrc = rte_pktmbuf_mtod(loc->mbuf, uint8_t *);
1017         es->inline_hdr_sz = RTE_BE16(MLX5_ESEG_MIN_INLINE_SIZE);
1018         es->inline_data = *(unaligned_uint16_t *)psrc;
1019         psrc += sizeof(uint16_t);
1020         pdst = (uint8_t *)(es + 1);
1021         if (MLX5_TXOFF_CONFIG(VLAN) && vlan) {
1022                 /* Implement VLAN tag insertion as part inline data. */
1023                 memcpy(pdst, psrc, 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t));
1024                 pdst += 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t);
1025                 psrc += 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t);
1026                 /* Insert VLAN ethertype + VLAN tag. */
1027                 *(unaligned_uint32_t *)pdst = rte_cpu_to_be_32
1028                                                 ((RTE_ETHER_TYPE_VLAN << 16) |
1029                                                  loc->mbuf->vlan_tci);
1030                 pdst += sizeof(struct rte_vlan_hdr);
1031                 /* Copy the rest two bytes from packet data. */
1032                 MLX5_ASSERT(pdst == RTE_PTR_ALIGN(pdst, sizeof(uint16_t)));
1033                 *(uint16_t *)pdst = *(unaligned_uint16_t *)psrc;
1034         } else {
1035                 /* Fill the gap in the title WQEBB with inline data. */
1036                 rte_mov16(pdst, psrc);
1037         }
1038 }
1039
1040 /**
1041  * Build the Ethernet Segment with entire packet data inlining. Checks the
1042  * boundary of WQEBB and ring buffer wrapping, supports Software Parser,
1043  * Checksums and VLAN insertion Tx offload features.
1044  *
1045  * @param txq
1046  *   Pointer to TX queue structure.
1047  * @param loc
1048  *   Pointer to burst routine local context.
1049  * @param wqe
1050  *   Pointer to WQE to fill with built Ethernet Segment.
1051  * @param vlan
1052  *   Length of VLAN tag insertion if any.
1053  * @param inlen
1054  *   Length of data to inline (VLAN included, if any).
1055  * @param tso
1056  *   TSO flag, set mss field from the packet.
1057  * @param olx
1058  *   Configured Tx offloads mask. It is fully defined at
1059  *   compile time and may be used for optimization.
1060  *
1061  * @return
1062  *   Pointer to the next Data Segment (aligned and wrapped around).
1063  */
1064 static __rte_always_inline struct mlx5_wqe_dseg *
1065 mlx5_tx_eseg_data(struct mlx5_txq_data *__rte_restrict txq,
1066                   struct mlx5_txq_local *__rte_restrict loc,
1067                   struct mlx5_wqe *__rte_restrict wqe,
1068                   unsigned int vlan,
1069                   unsigned int inlen,
1070                   unsigned int tso,
1071                   unsigned int olx)
1072 {
1073         struct mlx5_wqe_eseg *__rte_restrict es = &wqe->eseg;
1074         uint32_t csum;
1075         uint8_t *psrc, *pdst;
1076         unsigned int part;
1077
1078         /*
1079          * Calculate and set check sum flags first, dword field
1080          * in segment may be shared with Software Parser flags.
1081          */
1082         csum = MLX5_TXOFF_CONFIG(CSUM) ? txq_ol_cksum_to_cs(loc->mbuf) : 0;
1083         if (tso) {
1084                 csum <<= 24;
1085                 csum |= loc->mbuf->tso_segsz;
1086                 es->flags = rte_cpu_to_be_32(csum);
1087         } else {
1088                 es->flags = rte_cpu_to_le_32(csum);
1089         }
1090         /*
1091          * Calculate and set Software Parser offsets and flags.
1092          * These flags a set for custom UDP and IP tunnel packets.
1093          */
1094         es->swp_offs = txq_mbuf_to_swp(loc, &es->swp_flags, olx);
1095         /* Fill metadata field if needed. */
1096         es->metadata = MLX5_TXOFF_CONFIG(METADATA) ?
1097                        loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
1098                        rte_cpu_to_be_32(*RTE_FLOW_DYNF_METADATA(loc->mbuf)) :
1099                        0 : 0;
1100         psrc = rte_pktmbuf_mtod(loc->mbuf, uint8_t *);
1101         es->inline_hdr_sz = rte_cpu_to_be_16(inlen);
1102         es->inline_data = *(unaligned_uint16_t *)psrc;
1103         psrc += sizeof(uint16_t);
1104         pdst = (uint8_t *)(es + 1);
1105         if (MLX5_TXOFF_CONFIG(VLAN) && vlan) {
1106                 /* Implement VLAN tag insertion as part inline data. */
1107                 memcpy(pdst, psrc, 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t));
1108                 pdst += 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t);
1109                 psrc += 2 * RTE_ETHER_ADDR_LEN - sizeof(uint16_t);
1110                 /* Insert VLAN ethertype + VLAN tag. */
1111                 *(unaligned_uint32_t *)pdst = rte_cpu_to_be_32
1112                                                 ((RTE_ETHER_TYPE_VLAN << 16) |
1113                                                  loc->mbuf->vlan_tci);
1114                 pdst += sizeof(struct rte_vlan_hdr);
1115                 /* Copy the rest two bytes from packet data. */
1116                 MLX5_ASSERT(pdst == RTE_PTR_ALIGN(pdst, sizeof(uint16_t)));
1117                 *(uint16_t *)pdst = *(unaligned_uint16_t *)psrc;
1118                 psrc += sizeof(uint16_t);
1119         } else {
1120                 /* Fill the gap in the title WQEBB with inline data. */
1121                 rte_mov16(pdst, psrc);
1122                 psrc += sizeof(rte_v128u32_t);
1123         }
1124         pdst = (uint8_t *)(es + 2);
1125         MLX5_ASSERT(inlen >= MLX5_ESEG_MIN_INLINE_SIZE);
1126         MLX5_ASSERT(pdst < (uint8_t *)txq->wqes_end);
1127         inlen -= MLX5_ESEG_MIN_INLINE_SIZE;
1128         if (!inlen) {
1129                 MLX5_ASSERT(pdst == RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE));
1130                 return (struct mlx5_wqe_dseg *)pdst;
1131         }
1132         /*
1133          * The WQEBB space availability is checked by caller.
1134          * Here we should be aware of WQE ring buffer wraparound only.
1135          */
1136         part = (uint8_t *)txq->wqes_end - pdst;
1137         part = RTE_MIN(part, inlen);
1138         do {
1139                 rte_memcpy(pdst, psrc, part);
1140                 inlen -= part;
1141                 if (likely(!inlen)) {
1142                         /*
1143                          * If return value is not used by the caller
1144                          * the code below will be optimized out.
1145                          */
1146                         pdst += part;
1147                         pdst = RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE);
1148                         if (unlikely(pdst >= (uint8_t *)txq->wqes_end))
1149                                 pdst = (uint8_t *)txq->wqes;
1150                         return (struct mlx5_wqe_dseg *)pdst;
1151                 }
1152                 pdst = (uint8_t *)txq->wqes;
1153                 psrc += part;
1154                 part = inlen;
1155         } while (true);
1156 }
1157
1158 /**
1159  * Copy data from chain of mbuf to the specified linear buffer.
1160  * Checksums and VLAN insertion Tx offload features. If data
1161  * from some mbuf copied completely this mbuf is freed. Local
1162  * structure is used to keep the byte stream state.
1163  *
1164  * @param pdst
1165  *   Pointer to the destination linear buffer.
1166  * @param loc
1167  *   Pointer to burst routine local context.
1168  * @param len
1169  *   Length of data to be copied.
1170  * @param must
1171  *   Length of data to be copied ignoring no inline hint.
1172  * @param olx
1173  *   Configured Tx offloads mask. It is fully defined at
1174  *   compile time and may be used for optimization.
1175  *
1176  * @return
1177  *   Number of actual copied data bytes. This is always greater than or
1178  *   equal to must parameter and might be lesser than len in no inline
1179  *   hint flag is encountered.
1180  */
1181 static __rte_always_inline unsigned int
1182 mlx5_tx_mseg_memcpy(uint8_t *pdst,
1183                     struct mlx5_txq_local *__rte_restrict loc,
1184                     unsigned int len,
1185                     unsigned int must,
1186                     unsigned int olx __rte_unused)
1187 {
1188         struct rte_mbuf *mbuf;
1189         unsigned int part, dlen, copy = 0;
1190         uint8_t *psrc;
1191
1192         MLX5_ASSERT(len);
1193         MLX5_ASSERT(must <= len);
1194         do {
1195                 /* Allow zero length packets, must check first. */
1196                 dlen = rte_pktmbuf_data_len(loc->mbuf);
1197                 if (dlen <= loc->mbuf_off) {
1198                         /* Exhausted packet, just free. */
1199                         mbuf = loc->mbuf;
1200                         loc->mbuf = mbuf->next;
1201                         rte_pktmbuf_free_seg(mbuf);
1202                         loc->mbuf_off = 0;
1203                         MLX5_ASSERT(loc->mbuf_nseg > 1);
1204                         MLX5_ASSERT(loc->mbuf);
1205                         --loc->mbuf_nseg;
1206                         if (loc->mbuf->ol_flags & PKT_TX_DYNF_NOINLINE) {
1207                                 unsigned int diff;
1208
1209                                 if (copy >= must) {
1210                                         /*
1211                                          * We already copied the minimal
1212                                          * requested amount of data.
1213                                          */
1214                                         return copy;
1215                                 }
1216                                 diff = must - copy;
1217                                 if (diff <= rte_pktmbuf_data_len(loc->mbuf)) {
1218                                         /*
1219                                          * Copy only the minimal required
1220                                          * part of the data buffer.
1221                                          */
1222                                         len = diff;
1223                                 }
1224                         }
1225                         continue;
1226                 }
1227                 dlen -= loc->mbuf_off;
1228                 psrc = rte_pktmbuf_mtod_offset(loc->mbuf, uint8_t *,
1229                                                loc->mbuf_off);
1230                 part = RTE_MIN(len, dlen);
1231                 rte_memcpy(pdst, psrc, part);
1232                 copy += part;
1233                 loc->mbuf_off += part;
1234                 len -= part;
1235                 if (!len) {
1236                         if (loc->mbuf_off >= rte_pktmbuf_data_len(loc->mbuf)) {
1237                                 loc->mbuf_off = 0;
1238                                 /* Exhausted packet, just free. */
1239                                 mbuf = loc->mbuf;
1240                                 loc->mbuf = mbuf->next;
1241                                 rte_pktmbuf_free_seg(mbuf);
1242                                 loc->mbuf_off = 0;
1243                                 MLX5_ASSERT(loc->mbuf_nseg >= 1);
1244                                 --loc->mbuf_nseg;
1245                         }
1246                         return copy;
1247                 }
1248                 pdst += part;
1249         } while (true);
1250 }
1251
1252 /**
1253  * Build the Ethernet Segment with inlined data from multi-segment packet.
1254  * Checks the boundary of WQEBB and ring buffer wrapping, supports Software
1255  * Parser, Checksums and VLAN insertion Tx offload features.
1256  *
1257  * @param txq
1258  *   Pointer to TX queue structure.
1259  * @param loc
1260  *   Pointer to burst routine local context.
1261  * @param wqe
1262  *   Pointer to WQE to fill with built Ethernet Segment.
1263  * @param vlan
1264  *   Length of VLAN tag insertion if any.
1265  * @param inlen
1266  *   Length of data to inline (VLAN included, if any).
1267  * @param tso
1268  *   TSO flag, set mss field from the packet.
1269  * @param olx
1270  *   Configured Tx offloads mask. It is fully defined at
1271  *   compile time and may be used for optimization.
1272  *
1273  * @return
1274  *   Pointer to the next Data Segment (aligned and possible NOT wrapped
1275  *   around - caller should do wrapping check on its own).
1276  */
1277 static __rte_always_inline struct mlx5_wqe_dseg *
1278 mlx5_tx_eseg_mdat(struct mlx5_txq_data *__rte_restrict txq,
1279                   struct mlx5_txq_local *__rte_restrict loc,
1280                   struct mlx5_wqe *__rte_restrict wqe,
1281                   unsigned int vlan,
1282                   unsigned int inlen,
1283                   unsigned int tso,
1284                   unsigned int olx)
1285 {
1286         struct mlx5_wqe_eseg *__rte_restrict es = &wqe->eseg;
1287         uint32_t csum;
1288         uint8_t *pdst;
1289         unsigned int part, tlen = 0;
1290
1291         /*
1292          * Calculate and set check sum flags first, uint32_t field
1293          * in segment may be shared with Software Parser flags.
1294          */
1295         csum = MLX5_TXOFF_CONFIG(CSUM) ? txq_ol_cksum_to_cs(loc->mbuf) : 0;
1296         if (tso) {
1297                 csum <<= 24;
1298                 csum |= loc->mbuf->tso_segsz;
1299                 es->flags = rte_cpu_to_be_32(csum);
1300         } else {
1301                 es->flags = rte_cpu_to_le_32(csum);
1302         }
1303         /*
1304          * Calculate and set Software Parser offsets and flags.
1305          * These flags a set for custom UDP and IP tunnel packets.
1306          */
1307         es->swp_offs = txq_mbuf_to_swp(loc, &es->swp_flags, olx);
1308         /* Fill metadata field if needed. */
1309         es->metadata = MLX5_TXOFF_CONFIG(METADATA) ?
1310                        loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
1311                        rte_cpu_to_be_32(*RTE_FLOW_DYNF_METADATA(loc->mbuf)) :
1312                        0 : 0;
1313         MLX5_ASSERT(inlen >= MLX5_ESEG_MIN_INLINE_SIZE);
1314         pdst = (uint8_t *)&es->inline_data;
1315         if (MLX5_TXOFF_CONFIG(VLAN) && vlan) {
1316                 /* Implement VLAN tag insertion as part inline data. */
1317                 mlx5_tx_mseg_memcpy(pdst, loc,
1318                                     2 * RTE_ETHER_ADDR_LEN,
1319                                     2 * RTE_ETHER_ADDR_LEN, olx);
1320                 pdst += 2 * RTE_ETHER_ADDR_LEN;
1321                 *(unaligned_uint32_t *)pdst = rte_cpu_to_be_32
1322                                                 ((RTE_ETHER_TYPE_VLAN << 16) |
1323                                                  loc->mbuf->vlan_tci);
1324                 pdst += sizeof(struct rte_vlan_hdr);
1325                 tlen += 2 * RTE_ETHER_ADDR_LEN + sizeof(struct rte_vlan_hdr);
1326         }
1327         MLX5_ASSERT(pdst < (uint8_t *)txq->wqes_end);
1328         /*
1329          * The WQEBB space availability is checked by caller.
1330          * Here we should be aware of WQE ring buffer wraparound only.
1331          */
1332         part = (uint8_t *)txq->wqes_end - pdst;
1333         part = RTE_MIN(part, inlen - tlen);
1334         MLX5_ASSERT(part);
1335         do {
1336                 unsigned int copy;
1337
1338                 /*
1339                  * Copying may be interrupted inside the routine
1340                  * if run into no inline hint flag.
1341                  */
1342                 copy = tso ? inlen : txq->inlen_mode;
1343                 copy = tlen >= copy ? 0 : (copy - tlen);
1344                 copy = mlx5_tx_mseg_memcpy(pdst, loc, part, copy, olx);
1345                 tlen += copy;
1346                 if (likely(inlen <= tlen) || copy < part) {
1347                         es->inline_hdr_sz = rte_cpu_to_be_16(tlen);
1348                         pdst += copy;
1349                         pdst = RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE);
1350                         return (struct mlx5_wqe_dseg *)pdst;
1351                 }
1352                 pdst = (uint8_t *)txq->wqes;
1353                 part = inlen - tlen;
1354         } while (true);
1355 }
1356
1357 /**
1358  * Build the Data Segment of pointer type.
1359  *
1360  * @param txq
1361  *   Pointer to TX queue structure.
1362  * @param loc
1363  *   Pointer to burst routine local context.
1364  * @param dseg
1365  *   Pointer to WQE to fill with built Data Segment.
1366  * @param buf
1367  *   Data buffer to point.
1368  * @param len
1369  *   Data buffer length.
1370  * @param olx
1371  *   Configured Tx offloads mask. It is fully defined at
1372  *   compile time and may be used for optimization.
1373  */
1374 static __rte_always_inline void
1375 mlx5_tx_dseg_ptr(struct mlx5_txq_data *__rte_restrict txq,
1376                  struct mlx5_txq_local *__rte_restrict loc,
1377                  struct mlx5_wqe_dseg *__rte_restrict dseg,
1378                  uint8_t *buf,
1379                  unsigned int len,
1380                  unsigned int olx __rte_unused)
1381
1382 {
1383         MLX5_ASSERT(len);
1384         dseg->bcount = rte_cpu_to_be_32(len);
1385         dseg->lkey = mlx5_tx_mb2mr(txq, loc->mbuf);
1386         dseg->pbuf = rte_cpu_to_be_64((uintptr_t)buf);
1387 }
1388
1389 /**
1390  * Build the Data Segment of pointer type or inline if data length is less than
1391  * buffer in minimal Data Segment size.
1392  *
1393  * @param txq
1394  *   Pointer to TX queue structure.
1395  * @param loc
1396  *   Pointer to burst routine local context.
1397  * @param dseg
1398  *   Pointer to WQE to fill with built Data Segment.
1399  * @param buf
1400  *   Data buffer to point.
1401  * @param len
1402  *   Data buffer length.
1403  * @param olx
1404  *   Configured Tx offloads mask. It is fully defined at
1405  *   compile time and may be used for optimization.
1406  */
1407 static __rte_always_inline void
1408 mlx5_tx_dseg_iptr(struct mlx5_txq_data *__rte_restrict txq,
1409                   struct mlx5_txq_local *__rte_restrict loc,
1410                   struct mlx5_wqe_dseg *__rte_restrict dseg,
1411                   uint8_t *buf,
1412                   unsigned int len,
1413                   unsigned int olx __rte_unused)
1414
1415 {
1416         uintptr_t dst, src;
1417
1418         MLX5_ASSERT(len);
1419         if (len > MLX5_DSEG_MIN_INLINE_SIZE) {
1420                 dseg->bcount = rte_cpu_to_be_32(len);
1421                 dseg->lkey = mlx5_tx_mb2mr(txq, loc->mbuf);
1422                 dseg->pbuf = rte_cpu_to_be_64((uintptr_t)buf);
1423
1424                 return;
1425         }
1426         dseg->bcount = rte_cpu_to_be_32(len | MLX5_ETH_WQE_DATA_INLINE);
1427         /* Unrolled implementation of generic rte_memcpy. */
1428         dst = (uintptr_t)&dseg->inline_data[0];
1429         src = (uintptr_t)buf;
1430         if (len & 0x08) {
1431 #ifdef RTE_ARCH_STRICT_ALIGN
1432                 MLX5_ASSERT(dst == RTE_PTR_ALIGN(dst, sizeof(uint32_t)));
1433                 *(uint32_t *)dst = *(unaligned_uint32_t *)src;
1434                 dst += sizeof(uint32_t);
1435                 src += sizeof(uint32_t);
1436                 *(uint32_t *)dst = *(unaligned_uint32_t *)src;
1437                 dst += sizeof(uint32_t);
1438                 src += sizeof(uint32_t);
1439 #else
1440                 *(uint64_t *)dst = *(unaligned_uint64_t *)src;
1441                 dst += sizeof(uint64_t);
1442                 src += sizeof(uint64_t);
1443 #endif
1444         }
1445         if (len & 0x04) {
1446                 *(uint32_t *)dst = *(unaligned_uint32_t *)src;
1447                 dst += sizeof(uint32_t);
1448                 src += sizeof(uint32_t);
1449         }
1450         if (len & 0x02) {
1451                 *(uint16_t *)dst = *(unaligned_uint16_t *)src;
1452                 dst += sizeof(uint16_t);
1453                 src += sizeof(uint16_t);
1454         }
1455         if (len & 0x01)
1456                 *(uint8_t *)dst = *(uint8_t *)src;
1457 }
1458
1459 /**
1460  * Build the Data Segment of inlined data from single
1461  * segment packet, no VLAN insertion.
1462  *
1463  * @param txq
1464  *   Pointer to TX queue structure.
1465  * @param loc
1466  *   Pointer to burst routine local context.
1467  * @param dseg
1468  *   Pointer to WQE to fill with built Data Segment.
1469  * @param buf
1470  *   Data buffer to point.
1471  * @param len
1472  *   Data buffer length.
1473  * @param olx
1474  *   Configured Tx offloads mask. It is fully defined at
1475  *   compile time and may be used for optimization.
1476  *
1477  * @return
1478  *   Pointer to the next Data Segment after inlined data.
1479  *   Ring buffer wraparound check is needed. We do not do it here because it
1480  *   may not be needed for the last packet in the eMPW session.
1481  */
1482 static __rte_always_inline struct mlx5_wqe_dseg *
1483 mlx5_tx_dseg_empw(struct mlx5_txq_data *__rte_restrict txq,
1484                   struct mlx5_txq_local *__rte_restrict loc __rte_unused,
1485                   struct mlx5_wqe_dseg *__rte_restrict dseg,
1486                   uint8_t *buf,
1487                   unsigned int len,
1488                   unsigned int olx __rte_unused)
1489 {
1490         unsigned int part;
1491         uint8_t *pdst;
1492
1493         if (!MLX5_TXOFF_CONFIG(MPW)) {
1494                 /* Store the descriptor byte counter for eMPW sessions. */
1495                 dseg->bcount = rte_cpu_to_be_32(len | MLX5_ETH_WQE_DATA_INLINE);
1496                 pdst = &dseg->inline_data[0];
1497         } else {
1498                 /* The entire legacy MPW session counter is stored on close. */
1499                 pdst = (uint8_t *)dseg;
1500         }
1501         /*
1502          * The WQEBB space availability is checked by caller.
1503          * Here we should be aware of WQE ring buffer wraparound only.
1504          */
1505         part = (uint8_t *)txq->wqes_end - pdst;
1506         part = RTE_MIN(part, len);
1507         do {
1508                 rte_memcpy(pdst, buf, part);
1509                 len -= part;
1510                 if (likely(!len)) {
1511                         pdst += part;
1512                         if (!MLX5_TXOFF_CONFIG(MPW))
1513                                 pdst = RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE);
1514                         /* Note: no final wraparound check here. */
1515                         return (struct mlx5_wqe_dseg *)pdst;
1516                 }
1517                 pdst = (uint8_t *)txq->wqes;
1518                 buf += part;
1519                 part = len;
1520         } while (true);
1521 }
1522
1523 /**
1524  * Build the Data Segment of inlined data from single
1525  * segment packet with VLAN insertion.
1526  *
1527  * @param txq
1528  *   Pointer to TX queue structure.
1529  * @param loc
1530  *   Pointer to burst routine local context.
1531  * @param dseg
1532  *   Pointer to the dseg fill with built Data Segment.
1533  * @param buf
1534  *   Data buffer to point.
1535  * @param len
1536  *   Data buffer length.
1537  * @param olx
1538  *   Configured Tx offloads mask. It is fully defined at
1539  *   compile time and may be used for optimization.
1540  *
1541  * @return
1542  *   Pointer to the next Data Segment after inlined data.
1543  *   Ring buffer wraparound check is needed.
1544  */
1545 static __rte_always_inline struct mlx5_wqe_dseg *
1546 mlx5_tx_dseg_vlan(struct mlx5_txq_data *__rte_restrict txq,
1547                   struct mlx5_txq_local *__rte_restrict loc __rte_unused,
1548                   struct mlx5_wqe_dseg *__rte_restrict dseg,
1549                   uint8_t *buf,
1550                   unsigned int len,
1551                   unsigned int olx __rte_unused)
1552
1553 {
1554         unsigned int part;
1555         uint8_t *pdst;
1556
1557         MLX5_ASSERT(len > MLX5_ESEG_MIN_INLINE_SIZE);
1558         if (!MLX5_TXOFF_CONFIG(MPW)) {
1559                 /* Store the descriptor byte counter for eMPW sessions. */
1560                 dseg->bcount = rte_cpu_to_be_32
1561                                 ((len + sizeof(struct rte_vlan_hdr)) |
1562                                  MLX5_ETH_WQE_DATA_INLINE);
1563                 pdst = &dseg->inline_data[0];
1564         } else {
1565                 /* The entire legacy MPW session counter is stored on close. */
1566                 pdst = (uint8_t *)dseg;
1567         }
1568         memcpy(pdst, buf, MLX5_DSEG_MIN_INLINE_SIZE);
1569         buf += MLX5_DSEG_MIN_INLINE_SIZE;
1570         pdst += MLX5_DSEG_MIN_INLINE_SIZE;
1571         len -= MLX5_DSEG_MIN_INLINE_SIZE;
1572         /* Insert VLAN ethertype + VLAN tag. Pointer is aligned. */
1573         MLX5_ASSERT(pdst == RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE));
1574         if (unlikely(pdst >= (uint8_t *)txq->wqes_end))
1575                 pdst = (uint8_t *)txq->wqes;
1576         *(uint32_t *)pdst = rte_cpu_to_be_32((RTE_ETHER_TYPE_VLAN << 16) |
1577                                               loc->mbuf->vlan_tci);
1578         pdst += sizeof(struct rte_vlan_hdr);
1579         /*
1580          * The WQEBB space availability is checked by caller.
1581          * Here we should be aware of WQE ring buffer wraparound only.
1582          */
1583         part = (uint8_t *)txq->wqes_end - pdst;
1584         part = RTE_MIN(part, len);
1585         do {
1586                 rte_memcpy(pdst, buf, part);
1587                 len -= part;
1588                 if (likely(!len)) {
1589                         pdst += part;
1590                         if (!MLX5_TXOFF_CONFIG(MPW))
1591                                 pdst = RTE_PTR_ALIGN(pdst, MLX5_WSEG_SIZE);
1592                         /* Note: no final wraparound check here. */
1593                         return (struct mlx5_wqe_dseg *)pdst;
1594                 }
1595                 pdst = (uint8_t *)txq->wqes;
1596                 buf += part;
1597                 part = len;
1598         } while (true);
1599 }
1600
1601 /**
1602  * Build the Ethernet Segment with optionally inlined data with
1603  * VLAN insertion and following Data Segments (if any) from
1604  * multi-segment packet. Used by ordinary send and TSO.
1605  *
1606  * @param txq
1607  *   Pointer to TX queue structure.
1608  * @param loc
1609  *   Pointer to burst routine local context.
1610  * @param wqe
1611  *   Pointer to WQE to fill with built Ethernet/Data Segments.
1612  * @param vlan
1613  *   Length of VLAN header to insert, 0 means no VLAN insertion.
1614  * @param inlen
1615  *   Data length to inline. For TSO this parameter specifies exact value,
1616  *   for ordinary send routine can be aligned by caller to provide better WQE
1617  *   space saving and data buffer start address alignment.
1618  *   This length includes VLAN header being inserted.
1619  * @param tso
1620  *   Zero means ordinary send, inlined data can be extended,
1621  *   otherwise this is TSO, inlined data length is fixed.
1622  * @param olx
1623  *   Configured Tx offloads mask. It is fully defined at
1624  *   compile time and may be used for optimization.
1625  *
1626  * @return
1627  *   Actual size of built WQE in segments.
1628  */
1629 static __rte_always_inline unsigned int
1630 mlx5_tx_mseg_build(struct mlx5_txq_data *__rte_restrict txq,
1631                    struct mlx5_txq_local *__rte_restrict loc,
1632                    struct mlx5_wqe *__rte_restrict wqe,
1633                    unsigned int vlan,
1634                    unsigned int inlen,
1635                    unsigned int tso,
1636                    unsigned int olx __rte_unused)
1637 {
1638         struct mlx5_wqe_dseg *__rte_restrict dseg;
1639         unsigned int ds;
1640
1641         MLX5_ASSERT((rte_pktmbuf_pkt_len(loc->mbuf) + vlan) >= inlen);
1642         loc->mbuf_nseg = NB_SEGS(loc->mbuf);
1643         loc->mbuf_off = 0;
1644
1645         dseg = mlx5_tx_eseg_mdat(txq, loc, wqe, vlan, inlen, tso, olx);
1646         if (!loc->mbuf_nseg)
1647                 goto dseg_done;
1648         /*
1649          * There are still some mbuf remaining, not inlined.
1650          * The first mbuf may be partially inlined and we
1651          * must process the possible non-zero data offset.
1652          */
1653         if (loc->mbuf_off) {
1654                 unsigned int dlen;
1655                 uint8_t *dptr;
1656
1657                 /*
1658                  * Exhausted packets must be dropped before.
1659                  * Non-zero offset means there are some data
1660                  * remained in the packet.
1661                  */
1662                 MLX5_ASSERT(loc->mbuf_off < rte_pktmbuf_data_len(loc->mbuf));
1663                 MLX5_ASSERT(rte_pktmbuf_data_len(loc->mbuf));
1664                 dptr = rte_pktmbuf_mtod_offset(loc->mbuf, uint8_t *,
1665                                                loc->mbuf_off);
1666                 dlen = rte_pktmbuf_data_len(loc->mbuf) - loc->mbuf_off;
1667                 /*
1668                  * Build the pointer/minimal Data Segment.
1669                  * Do ring buffer wrapping check in advance.
1670                  */
1671                 if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
1672                         dseg = (struct mlx5_wqe_dseg *)txq->wqes;
1673                 mlx5_tx_dseg_iptr(txq, loc, dseg, dptr, dlen, olx);
1674                 /* Store the mbuf to be freed on completion. */
1675                 MLX5_ASSERT(loc->elts_free);
1676                 txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
1677                 --loc->elts_free;
1678                 ++dseg;
1679                 if (--loc->mbuf_nseg == 0)
1680                         goto dseg_done;
1681                 loc->mbuf = loc->mbuf->next;
1682                 loc->mbuf_off = 0;
1683         }
1684         do {
1685                 if (unlikely(!rte_pktmbuf_data_len(loc->mbuf))) {
1686                         struct rte_mbuf *mbuf;
1687
1688                         /* Zero length segment found, just skip. */
1689                         mbuf = loc->mbuf;
1690                         loc->mbuf = loc->mbuf->next;
1691                         rte_pktmbuf_free_seg(mbuf);
1692                         if (--loc->mbuf_nseg == 0)
1693                                 break;
1694                 } else {
1695                         if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
1696                                 dseg = (struct mlx5_wqe_dseg *)txq->wqes;
1697                         mlx5_tx_dseg_iptr
1698                                 (txq, loc, dseg,
1699                                  rte_pktmbuf_mtod(loc->mbuf, uint8_t *),
1700                                  rte_pktmbuf_data_len(loc->mbuf), olx);
1701                         MLX5_ASSERT(loc->elts_free);
1702                         txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
1703                         --loc->elts_free;
1704                         ++dseg;
1705                         if (--loc->mbuf_nseg == 0)
1706                                 break;
1707                         loc->mbuf = loc->mbuf->next;
1708                 }
1709         } while (true);
1710
1711 dseg_done:
1712         /* Calculate actual segments used from the dseg pointer. */
1713         if ((uintptr_t)wqe < (uintptr_t)dseg)
1714                 ds = ((uintptr_t)dseg - (uintptr_t)wqe) / MLX5_WSEG_SIZE;
1715         else
1716                 ds = (((uintptr_t)dseg - (uintptr_t)wqe) +
1717                       txq->wqe_s * MLX5_WQE_SIZE) / MLX5_WSEG_SIZE;
1718         return ds;
1719 }
1720
1721 /**
1722  * The routine checks timestamp flag in the current packet,
1723  * and push WAIT WQE into the queue if scheduling is required.
1724  *
1725  * @param txq
1726  *   Pointer to TX queue structure.
1727  * @param loc
1728  *   Pointer to burst routine local context.
1729  * @param olx
1730  *   Configured Tx offloads mask. It is fully defined at
1731  *   compile time and may be used for optimization.
1732  *
1733  * @return
1734  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
1735  *   MLX5_TXCMP_CODE_SINGLE - continue processing with the packet.
1736  *   MLX5_TXCMP_CODE_MULTI - the WAIT inserted, continue processing.
1737  * Local context variables partially updated.
1738  */
1739 static __rte_always_inline enum mlx5_txcmp_code
1740 mlx5_tx_schedule_send(struct mlx5_txq_data *restrict txq,
1741                       struct mlx5_txq_local *restrict loc,
1742                       unsigned int olx)
1743 {
1744         if (MLX5_TXOFF_CONFIG(TXPP) &&
1745             loc->mbuf->ol_flags & txq->ts_mask) {
1746                 struct mlx5_wqe *wqe;
1747                 uint64_t ts;
1748                 int32_t wci;
1749
1750                 /*
1751                  * Estimate the required space quickly and roughly.
1752                  * We would like to ensure the packet can be pushed
1753                  * to the queue and we won't get the orphan WAIT WQE.
1754                  */
1755                 if (loc->wqe_free <= MLX5_WQE_SIZE_MAX / MLX5_WQE_SIZE ||
1756                     loc->elts_free < NB_SEGS(loc->mbuf))
1757                         return MLX5_TXCMP_CODE_EXIT;
1758                 /* Convert the timestamp into completion to wait. */
1759                 ts = *RTE_MBUF_DYNFIELD(loc->mbuf, txq->ts_offset, uint64_t *);
1760                 wci = mlx5_txpp_convert_tx_ts(txq->sh, ts);
1761                 if (unlikely(wci < 0))
1762                         return MLX5_TXCMP_CODE_SINGLE;
1763                 /* Build the WAIT WQE with specified completion. */
1764                 wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
1765                 mlx5_tx_cseg_init(txq, loc, wqe, 2, MLX5_OPCODE_WAIT, olx);
1766                 mlx5_tx_wseg_init(txq, loc, wqe, wci, olx);
1767                 ++txq->wqe_ci;
1768                 --loc->wqe_free;
1769                 return MLX5_TXCMP_CODE_MULTI;
1770         }
1771         return MLX5_TXCMP_CODE_SINGLE;
1772 }
1773
1774 /**
1775  * Tx one packet function for multi-segment TSO. Supports all
1776  * types of Tx offloads, uses MLX5_OPCODE_TSO to build WQEs,
1777  * sends one packet per WQE.
1778  *
1779  * This routine is responsible for storing processed mbuf
1780  * into elts ring buffer and update elts_head.
1781  *
1782  * @param txq
1783  *   Pointer to TX queue structure.
1784  * @param loc
1785  *   Pointer to burst routine local context.
1786  * @param olx
1787  *   Configured Tx offloads mask. It is fully defined at
1788  *   compile time and may be used for optimization.
1789  *
1790  * @return
1791  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
1792  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
1793  * Local context variables partially updated.
1794  */
1795 static __rte_always_inline enum mlx5_txcmp_code
1796 mlx5_tx_packet_multi_tso(struct mlx5_txq_data *__rte_restrict txq,
1797                         struct mlx5_txq_local *__rte_restrict loc,
1798                         unsigned int olx)
1799 {
1800         struct mlx5_wqe *__rte_restrict wqe;
1801         unsigned int ds, dlen, inlen, ntcp, vlan = 0;
1802
1803         if (MLX5_TXOFF_CONFIG(TXPP)) {
1804                 enum mlx5_txcmp_code wret;
1805
1806                 /* Generate WAIT for scheduling if requested. */
1807                 wret = mlx5_tx_schedule_send(txq, loc, olx);
1808                 if (wret == MLX5_TXCMP_CODE_EXIT)
1809                         return MLX5_TXCMP_CODE_EXIT;
1810                 if (wret == MLX5_TXCMP_CODE_ERROR)
1811                         return MLX5_TXCMP_CODE_ERROR;
1812         }
1813         /*
1814          * Calculate data length to be inlined to estimate
1815          * the required space in WQE ring buffer.
1816          */
1817         dlen = rte_pktmbuf_pkt_len(loc->mbuf);
1818         if (MLX5_TXOFF_CONFIG(VLAN) && loc->mbuf->ol_flags & PKT_TX_VLAN_PKT)
1819                 vlan = sizeof(struct rte_vlan_hdr);
1820         inlen = loc->mbuf->l2_len + vlan +
1821                 loc->mbuf->l3_len + loc->mbuf->l4_len;
1822         if (unlikely((!inlen || !loc->mbuf->tso_segsz)))
1823                 return MLX5_TXCMP_CODE_ERROR;
1824         if (loc->mbuf->ol_flags & PKT_TX_TUNNEL_MASK)
1825                 inlen += loc->mbuf->outer_l2_len + loc->mbuf->outer_l3_len;
1826         /* Packet must contain all TSO headers. */
1827         if (unlikely(inlen > MLX5_MAX_TSO_HEADER ||
1828                      inlen <= MLX5_ESEG_MIN_INLINE_SIZE ||
1829                      inlen > (dlen + vlan)))
1830                 return MLX5_TXCMP_CODE_ERROR;
1831         MLX5_ASSERT(inlen >= txq->inlen_mode);
1832         /*
1833          * Check whether there are enough free WQEBBs:
1834          * - Control Segment
1835          * - Ethernet Segment
1836          * - First Segment of inlined Ethernet data
1837          * - ... data continued ...
1838          * - Data Segments of pointer/min inline type
1839          */
1840         ds = NB_SEGS(loc->mbuf) + 2 + (inlen -
1841                                        MLX5_ESEG_MIN_INLINE_SIZE +
1842                                        MLX5_WSEG_SIZE +
1843                                        MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
1844         if (unlikely(loc->wqe_free < ((ds + 3) / 4)))
1845                 return MLX5_TXCMP_CODE_EXIT;
1846         /* Check for maximal WQE size. */
1847         if (unlikely((MLX5_WQE_SIZE_MAX / MLX5_WSEG_SIZE) < ((ds + 3) / 4)))
1848                 return MLX5_TXCMP_CODE_ERROR;
1849 #ifdef MLX5_PMD_SOFT_COUNTERS
1850         /* Update sent data bytes/packets counters. */
1851         ntcp = (dlen - (inlen - vlan) + loc->mbuf->tso_segsz - 1) /
1852                 loc->mbuf->tso_segsz;
1853         /*
1854          * One will be added for mbuf itself at the end of the mlx5_tx_burst
1855          * from loc->pkts_sent field.
1856          */
1857         --ntcp;
1858         txq->stats.opackets += ntcp;
1859         txq->stats.obytes += dlen + vlan + ntcp * inlen;
1860 #endif
1861         wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
1862         loc->wqe_last = wqe;
1863         mlx5_tx_cseg_init(txq, loc, wqe, 0, MLX5_OPCODE_TSO, olx);
1864         ds = mlx5_tx_mseg_build(txq, loc, wqe, vlan, inlen, 1, olx);
1865         wqe->cseg.sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | ds);
1866         txq->wqe_ci += (ds + 3) / 4;
1867         loc->wqe_free -= (ds + 3) / 4;
1868         return MLX5_TXCMP_CODE_MULTI;
1869 }
1870
1871 /**
1872  * Tx one packet function for multi-segment SEND. Supports all types of Tx
1873  * offloads, uses MLX5_OPCODE_SEND to build WQEs, sends one packet per WQE,
1874  * without any data inlining in Ethernet Segment.
1875  *
1876  * This routine is responsible for storing processed mbuf
1877  * into elts ring buffer and update elts_head.
1878  *
1879  * @param txq
1880  *   Pointer to TX queue structure.
1881  * @param loc
1882  *   Pointer to burst routine local context.
1883  * @param olx
1884  *   Configured Tx offloads mask. It is fully defined at
1885  *   compile time and may be used for optimization.
1886  *
1887  * @return
1888  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
1889  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
1890  * Local context variables partially updated.
1891  */
1892 static __rte_always_inline enum mlx5_txcmp_code
1893 mlx5_tx_packet_multi_send(struct mlx5_txq_data *__rte_restrict txq,
1894                           struct mlx5_txq_local *__rte_restrict loc,
1895                           unsigned int olx)
1896 {
1897         struct mlx5_wqe_dseg *__rte_restrict dseg;
1898         struct mlx5_wqe *__rte_restrict wqe;
1899         unsigned int ds, nseg;
1900
1901         MLX5_ASSERT(NB_SEGS(loc->mbuf) > 1);
1902         if (MLX5_TXOFF_CONFIG(TXPP)) {
1903                 enum mlx5_txcmp_code wret;
1904
1905                 /* Generate WAIT for scheduling if requested. */
1906                 wret = mlx5_tx_schedule_send(txq, loc, olx);
1907                 if (wret == MLX5_TXCMP_CODE_EXIT)
1908                         return MLX5_TXCMP_CODE_EXIT;
1909                 if (wret == MLX5_TXCMP_CODE_ERROR)
1910                         return MLX5_TXCMP_CODE_ERROR;
1911         }
1912         /*
1913          * No inline at all, it means the CPU cycles saving is prioritized at
1914          * configuration, we should not copy any packet data to WQE.
1915          */
1916         nseg = NB_SEGS(loc->mbuf);
1917         ds = 2 + nseg;
1918         if (unlikely(loc->wqe_free < ((ds + 3) / 4)))
1919                 return MLX5_TXCMP_CODE_EXIT;
1920         /* Check for maximal WQE size. */
1921         if (unlikely((MLX5_WQE_SIZE_MAX / MLX5_WSEG_SIZE) < ((ds + 3) / 4)))
1922                 return MLX5_TXCMP_CODE_ERROR;
1923         /*
1924          * Some Tx offloads may cause an error if packet is not long enough,
1925          * check against assumed minimal length.
1926          */
1927         if (rte_pktmbuf_pkt_len(loc->mbuf) <= MLX5_ESEG_MIN_INLINE_SIZE)
1928                 return MLX5_TXCMP_CODE_ERROR;
1929 #ifdef MLX5_PMD_SOFT_COUNTERS
1930         /* Update sent data bytes counter. */
1931         txq->stats.obytes += rte_pktmbuf_pkt_len(loc->mbuf);
1932         if (MLX5_TXOFF_CONFIG(VLAN) &&
1933             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT)
1934                 txq->stats.obytes += sizeof(struct rte_vlan_hdr);
1935 #endif
1936         /*
1937          * SEND WQE, one WQEBB:
1938          * - Control Segment, SEND opcode
1939          * - Ethernet Segment, optional VLAN, no inline
1940          * - Data Segments, pointer only type
1941          */
1942         wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
1943         loc->wqe_last = wqe;
1944         mlx5_tx_cseg_init(txq, loc, wqe, ds, MLX5_OPCODE_SEND, olx);
1945         mlx5_tx_eseg_none(txq, loc, wqe, olx);
1946         dseg = &wqe->dseg[0];
1947         do {
1948                 if (unlikely(!rte_pktmbuf_data_len(loc->mbuf))) {
1949                         struct rte_mbuf *mbuf;
1950
1951                         /*
1952                          * Zero length segment found, have to correct total
1953                          * size of WQE in segments.
1954                          * It is supposed to be rare occasion, so in normal
1955                          * case (no zero length segments) we avoid extra
1956                          * writing to the Control Segment.
1957                          */
1958                         --ds;
1959                         wqe->cseg.sq_ds -= RTE_BE32(1);
1960                         mbuf = loc->mbuf;
1961                         loc->mbuf = mbuf->next;
1962                         rte_pktmbuf_free_seg(mbuf);
1963                         if (--nseg == 0)
1964                                 break;
1965                 } else {
1966                         mlx5_tx_dseg_ptr
1967                                 (txq, loc, dseg,
1968                                  rte_pktmbuf_mtod(loc->mbuf, uint8_t *),
1969                                  rte_pktmbuf_data_len(loc->mbuf), olx);
1970                         txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
1971                         --loc->elts_free;
1972                         if (--nseg == 0)
1973                                 break;
1974                         ++dseg;
1975                         if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
1976                                 dseg = (struct mlx5_wqe_dseg *)txq->wqes;
1977                         loc->mbuf = loc->mbuf->next;
1978                 }
1979         } while (true);
1980         txq->wqe_ci += (ds + 3) / 4;
1981         loc->wqe_free -= (ds + 3) / 4;
1982         return MLX5_TXCMP_CODE_MULTI;
1983 }
1984
1985 /**
1986  * Tx one packet function for multi-segment SEND. Supports all
1987  * types of Tx offloads, uses MLX5_OPCODE_SEND to build WQEs,
1988  * sends one packet per WQE, with data inlining in
1989  * Ethernet Segment and minimal Data Segments.
1990  *
1991  * This routine is responsible for storing processed mbuf
1992  * into elts ring buffer and update elts_head.
1993  *
1994  * @param txq
1995  *   Pointer to TX queue structure.
1996  * @param loc
1997  *   Pointer to burst routine local context.
1998  * @param olx
1999  *   Configured Tx offloads mask. It is fully defined at
2000  *   compile time and may be used for optimization.
2001  *
2002  * @return
2003  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
2004  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
2005  * Local context variables partially updated.
2006  */
2007 static __rte_always_inline enum mlx5_txcmp_code
2008 mlx5_tx_packet_multi_inline(struct mlx5_txq_data *__rte_restrict txq,
2009                             struct mlx5_txq_local *__rte_restrict loc,
2010                             unsigned int olx)
2011 {
2012         struct mlx5_wqe *__rte_restrict wqe;
2013         unsigned int ds, inlen, dlen, vlan = 0;
2014
2015         MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
2016         MLX5_ASSERT(NB_SEGS(loc->mbuf) > 1);
2017         if (MLX5_TXOFF_CONFIG(TXPP)) {
2018                 enum mlx5_txcmp_code wret;
2019
2020                 /* Generate WAIT for scheduling if requested. */
2021                 wret = mlx5_tx_schedule_send(txq, loc, olx);
2022                 if (wret == MLX5_TXCMP_CODE_EXIT)
2023                         return MLX5_TXCMP_CODE_EXIT;
2024                 if (wret == MLX5_TXCMP_CODE_ERROR)
2025                         return MLX5_TXCMP_CODE_ERROR;
2026         }
2027         /*
2028          * First calculate data length to be inlined
2029          * to estimate the required space for WQE.
2030          */
2031         dlen = rte_pktmbuf_pkt_len(loc->mbuf);
2032         if (MLX5_TXOFF_CONFIG(VLAN) && loc->mbuf->ol_flags & PKT_TX_VLAN_PKT)
2033                 vlan = sizeof(struct rte_vlan_hdr);
2034         inlen = dlen + vlan;
2035         /* Check against minimal length. */
2036         if (inlen <= MLX5_ESEG_MIN_INLINE_SIZE)
2037                 return MLX5_TXCMP_CODE_ERROR;
2038         MLX5_ASSERT(txq->inlen_send >= MLX5_ESEG_MIN_INLINE_SIZE);
2039         if (inlen > txq->inlen_send ||
2040             loc->mbuf->ol_flags & PKT_TX_DYNF_NOINLINE) {
2041                 struct rte_mbuf *mbuf;
2042                 unsigned int nxlen;
2043                 uintptr_t start;
2044
2045                 mbuf = loc->mbuf;
2046                 nxlen = rte_pktmbuf_data_len(mbuf);
2047                 /*
2048                  * Packet length exceeds the allowed inline data length,
2049                  * check whether the minimal inlining is required.
2050                  */
2051                 if (txq->inlen_mode) {
2052                         MLX5_ASSERT(txq->inlen_mode >=
2053                                     MLX5_ESEG_MIN_INLINE_SIZE);
2054                         MLX5_ASSERT(txq->inlen_mode <= txq->inlen_send);
2055                         inlen = txq->inlen_mode;
2056                 } else if (vlan && !txq->vlan_en) {
2057                         /*
2058                          * VLAN insertion is requested and hardware does not
2059                          * support the offload, will do with software inline.
2060                          */
2061                         inlen = MLX5_ESEG_MIN_INLINE_SIZE;
2062                 } else if (mbuf->ol_flags & PKT_TX_DYNF_NOINLINE ||
2063                            nxlen > txq->inlen_send) {
2064                         return mlx5_tx_packet_multi_send(txq, loc, olx);
2065                 } else {
2066                         goto do_first;
2067                 }
2068                 /*
2069                  * Now we know the minimal amount of data is requested
2070                  * to inline. Check whether we should inline the buffers
2071                  * from the chain beginning to eliminate some mbufs.
2072                  */
2073                 if (unlikely(nxlen <= txq->inlen_send)) {
2074                         /* We can inline first mbuf at least. */
2075                         if (nxlen < inlen) {
2076                                 unsigned int smlen;
2077
2078                                 /* Scan mbufs till inlen filled. */
2079                                 do {
2080                                         smlen = nxlen;
2081                                         mbuf = NEXT(mbuf);
2082                                         MLX5_ASSERT(mbuf);
2083                                         nxlen = rte_pktmbuf_data_len(mbuf);
2084                                         nxlen += smlen;
2085                                 } while (unlikely(nxlen < inlen));
2086                                 if (unlikely(nxlen > txq->inlen_send)) {
2087                                         /* We cannot inline entire mbuf. */
2088                                         smlen = inlen - smlen;
2089                                         start = rte_pktmbuf_mtod_offset
2090                                                     (mbuf, uintptr_t, smlen);
2091                                         goto do_align;
2092                                 }
2093                         }
2094 do_first:
2095                         do {
2096                                 inlen = nxlen;
2097                                 mbuf = NEXT(mbuf);
2098                                 /* There should be not end of packet. */
2099                                 MLX5_ASSERT(mbuf);
2100                                 nxlen = inlen + rte_pktmbuf_data_len(mbuf);
2101                         } while (unlikely(nxlen < txq->inlen_send));
2102                 }
2103                 start = rte_pktmbuf_mtod(mbuf, uintptr_t);
2104                 /*
2105                  * Check whether we can do inline to align start
2106                  * address of data buffer to cacheline.
2107                  */
2108 do_align:
2109                 start = (~start + 1) & (RTE_CACHE_LINE_SIZE - 1);
2110                 if (unlikely(start)) {
2111                         start += inlen;
2112                         if (start <= txq->inlen_send)
2113                                 inlen = start;
2114                 }
2115         }
2116         /*
2117          * Check whether there are enough free WQEBBs:
2118          * - Control Segment
2119          * - Ethernet Segment
2120          * - First Segment of inlined Ethernet data
2121          * - ... data continued ...
2122          * - Data Segments of pointer/min inline type
2123          *
2124          * Estimate the number of Data Segments conservatively,
2125          * supposing no any mbufs is being freed during inlining.
2126          */
2127         MLX5_ASSERT(inlen <= txq->inlen_send);
2128         ds = NB_SEGS(loc->mbuf) + 2 + (inlen -
2129                                        MLX5_ESEG_MIN_INLINE_SIZE +
2130                                        MLX5_WSEG_SIZE +
2131                                        MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
2132         if (unlikely(loc->wqe_free < ((ds + 3) / 4)))
2133                 return MLX5_TXCMP_CODE_EXIT;
2134         /* Check for maximal WQE size. */
2135         if (unlikely((MLX5_WQE_SIZE_MAX / MLX5_WSEG_SIZE) < ((ds + 3) / 4)))
2136                 return MLX5_TXCMP_CODE_ERROR;
2137 #ifdef MLX5_PMD_SOFT_COUNTERS
2138         /* Update sent data bytes/packets counters. */
2139         txq->stats.obytes += dlen + vlan;
2140 #endif
2141         wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
2142         loc->wqe_last = wqe;
2143         mlx5_tx_cseg_init(txq, loc, wqe, 0, MLX5_OPCODE_SEND, olx);
2144         ds = mlx5_tx_mseg_build(txq, loc, wqe, vlan, inlen, 0, olx);
2145         wqe->cseg.sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | ds);
2146         txq->wqe_ci += (ds + 3) / 4;
2147         loc->wqe_free -= (ds + 3) / 4;
2148         return MLX5_TXCMP_CODE_MULTI;
2149 }
2150
2151 /**
2152  * Tx burst function for multi-segment packets. Supports all
2153  * types of Tx offloads, uses MLX5_OPCODE_SEND/TSO to build WQEs,
2154  * sends one packet per WQE. Function stops sending if it
2155  * encounters the single-segment packet.
2156  *
2157  * This routine is responsible for storing processed mbuf
2158  * into elts ring buffer and update elts_head.
2159  *
2160  * @param txq
2161  *   Pointer to TX queue structure.
2162  * @param[in] pkts
2163  *   Packets to transmit.
2164  * @param pkts_n
2165  *   Number of packets in array.
2166  * @param loc
2167  *   Pointer to burst routine local context.
2168  * @param olx
2169  *   Configured Tx offloads mask. It is fully defined at
2170  *   compile time and may be used for optimization.
2171  *
2172  * @return
2173  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
2174  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
2175  *   MLX5_TXCMP_CODE_SINGLE - single-segment packet encountered.
2176  *   MLX5_TXCMP_CODE_TSO - TSO single-segment packet encountered.
2177  * Local context variables updated.
2178  */
2179 static __rte_always_inline enum mlx5_txcmp_code
2180 mlx5_tx_burst_mseg(struct mlx5_txq_data *__rte_restrict txq,
2181                    struct rte_mbuf **__rte_restrict pkts,
2182                    unsigned int pkts_n,
2183                    struct mlx5_txq_local *__rte_restrict loc,
2184                    unsigned int olx)
2185 {
2186         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
2187         MLX5_ASSERT(pkts_n > loc->pkts_sent);
2188         pkts += loc->pkts_sent + 1;
2189         pkts_n -= loc->pkts_sent;
2190         for (;;) {
2191                 enum mlx5_txcmp_code ret;
2192
2193                 MLX5_ASSERT(NB_SEGS(loc->mbuf) > 1);
2194                 /*
2195                  * Estimate the number of free elts quickly but conservatively.
2196                  * Some segment may be fully inlined and freed,
2197                  * ignore this here - precise estimation is costly.
2198                  */
2199                 if (loc->elts_free < NB_SEGS(loc->mbuf))
2200                         return MLX5_TXCMP_CODE_EXIT;
2201                 if (MLX5_TXOFF_CONFIG(TSO) &&
2202                     unlikely(loc->mbuf->ol_flags & PKT_TX_TCP_SEG)) {
2203                         /* Proceed with multi-segment TSO. */
2204                         ret = mlx5_tx_packet_multi_tso(txq, loc, olx);
2205                 } else if (MLX5_TXOFF_CONFIG(INLINE)) {
2206                         /* Proceed with multi-segment SEND with inlining. */
2207                         ret = mlx5_tx_packet_multi_inline(txq, loc, olx);
2208                 } else {
2209                         /* Proceed with multi-segment SEND w/o inlining. */
2210                         ret = mlx5_tx_packet_multi_send(txq, loc, olx);
2211                 }
2212                 if (ret == MLX5_TXCMP_CODE_EXIT)
2213                         return MLX5_TXCMP_CODE_EXIT;
2214                 if (ret == MLX5_TXCMP_CODE_ERROR)
2215                         return MLX5_TXCMP_CODE_ERROR;
2216                 /* WQE is built, go to the next packet. */
2217                 ++loc->pkts_sent;
2218                 --pkts_n;
2219                 if (unlikely(!pkts_n || !loc->elts_free || !loc->wqe_free))
2220                         return MLX5_TXCMP_CODE_EXIT;
2221                 loc->mbuf = *pkts++;
2222                 if (pkts_n > 1)
2223                         rte_prefetch0(*pkts);
2224                 if (likely(NB_SEGS(loc->mbuf) > 1))
2225                         continue;
2226                 /* Here ends the series of multi-segment packets. */
2227                 if (MLX5_TXOFF_CONFIG(TSO) &&
2228                     unlikely(loc->mbuf->ol_flags & PKT_TX_TCP_SEG))
2229                         return MLX5_TXCMP_CODE_TSO;
2230                 return MLX5_TXCMP_CODE_SINGLE;
2231         }
2232         MLX5_ASSERT(false);
2233 }
2234
2235 /**
2236  * Tx burst function for single-segment packets with TSO.
2237  * Supports all types of Tx offloads, except multi-packets.
2238  * Uses MLX5_OPCODE_TSO to build WQEs, sends one packet per WQE.
2239  * Function stops sending if it encounters the multi-segment
2240  * packet or packet without TSO requested.
2241  *
2242  * The routine is responsible for storing processed mbuf into elts ring buffer
2243  * and update elts_head if inline offloads is requested due to possible early
2244  * freeing of the inlined mbufs (can not store pkts array in elts as a batch).
2245  *
2246  * @param txq
2247  *   Pointer to TX queue structure.
2248  * @param[in] pkts
2249  *   Packets to transmit.
2250  * @param pkts_n
2251  *   Number of packets in array.
2252  * @param loc
2253  *   Pointer to burst routine local context.
2254  * @param olx
2255  *   Configured Tx offloads mask. It is fully defined at
2256  *   compile time and may be used for optimization.
2257  *
2258  * @return
2259  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
2260  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
2261  *   MLX5_TXCMP_CODE_SINGLE - single-segment packet encountered.
2262  *   MLX5_TXCMP_CODE_MULTI - multi-segment packet encountered.
2263  * Local context variables updated.
2264  */
2265 static __rte_always_inline enum mlx5_txcmp_code
2266 mlx5_tx_burst_tso(struct mlx5_txq_data *__rte_restrict txq,
2267                   struct rte_mbuf **__rte_restrict pkts,
2268                   unsigned int pkts_n,
2269                   struct mlx5_txq_local *__rte_restrict loc,
2270                   unsigned int olx)
2271 {
2272         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
2273         MLX5_ASSERT(pkts_n > loc->pkts_sent);
2274         pkts += loc->pkts_sent + 1;
2275         pkts_n -= loc->pkts_sent;
2276         for (;;) {
2277                 struct mlx5_wqe_dseg *__rte_restrict dseg;
2278                 struct mlx5_wqe *__rte_restrict wqe;
2279                 unsigned int ds, dlen, hlen, ntcp, vlan = 0;
2280                 uint8_t *dptr;
2281
2282                 MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
2283                 if (MLX5_TXOFF_CONFIG(TXPP)) {
2284                         enum mlx5_txcmp_code wret;
2285
2286                         /* Generate WAIT for scheduling if requested. */
2287                         wret = mlx5_tx_schedule_send(txq, loc, olx);
2288                         if (wret == MLX5_TXCMP_CODE_EXIT)
2289                                 return MLX5_TXCMP_CODE_EXIT;
2290                         if (wret == MLX5_TXCMP_CODE_ERROR)
2291                                 return MLX5_TXCMP_CODE_ERROR;
2292                 }
2293                 dlen = rte_pktmbuf_data_len(loc->mbuf);
2294                 if (MLX5_TXOFF_CONFIG(VLAN) &&
2295                     loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
2296                         vlan = sizeof(struct rte_vlan_hdr);
2297                 }
2298                 /*
2299                  * First calculate the WQE size to check
2300                  * whether we have enough space in ring buffer.
2301                  */
2302                 hlen = loc->mbuf->l2_len + vlan +
2303                        loc->mbuf->l3_len + loc->mbuf->l4_len;
2304                 if (unlikely((!hlen || !loc->mbuf->tso_segsz)))
2305                         return MLX5_TXCMP_CODE_ERROR;
2306                 if (loc->mbuf->ol_flags & PKT_TX_TUNNEL_MASK)
2307                         hlen += loc->mbuf->outer_l2_len +
2308                                 loc->mbuf->outer_l3_len;
2309                 /* Segment must contain all TSO headers. */
2310                 if (unlikely(hlen > MLX5_MAX_TSO_HEADER ||
2311                              hlen <= MLX5_ESEG_MIN_INLINE_SIZE ||
2312                              hlen > (dlen + vlan)))
2313                         return MLX5_TXCMP_CODE_ERROR;
2314                 /*
2315                  * Check whether there are enough free WQEBBs:
2316                  * - Control Segment
2317                  * - Ethernet Segment
2318                  * - First Segment of inlined Ethernet data
2319                  * - ... data continued ...
2320                  * - Finishing Data Segment of pointer type
2321                  */
2322                 ds = 4 + (hlen - MLX5_ESEG_MIN_INLINE_SIZE +
2323                           MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
2324                 if (loc->wqe_free < ((ds + 3) / 4))
2325                         return MLX5_TXCMP_CODE_EXIT;
2326 #ifdef MLX5_PMD_SOFT_COUNTERS
2327                 /* Update sent data bytes/packets counters. */
2328                 ntcp = (dlen + vlan - hlen +
2329                         loc->mbuf->tso_segsz - 1) /
2330                         loc->mbuf->tso_segsz;
2331                 /*
2332                  * One will be added for mbuf itself at the end
2333                  * of the mlx5_tx_burst from loc->pkts_sent field.
2334                  */
2335                 --ntcp;
2336                 txq->stats.opackets += ntcp;
2337                 txq->stats.obytes += dlen + vlan + ntcp * hlen;
2338 #endif
2339                 /*
2340                  * Build the TSO WQE:
2341                  * - Control Segment
2342                  * - Ethernet Segment with hlen bytes inlined
2343                  * - Data Segment of pointer type
2344                  */
2345                 wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
2346                 loc->wqe_last = wqe;
2347                 mlx5_tx_cseg_init(txq, loc, wqe, ds,
2348                                   MLX5_OPCODE_TSO, olx);
2349                 dseg = mlx5_tx_eseg_data(txq, loc, wqe, vlan, hlen, 1, olx);
2350                 dptr = rte_pktmbuf_mtod(loc->mbuf, uint8_t *) + hlen - vlan;
2351                 dlen -= hlen - vlan;
2352                 mlx5_tx_dseg_ptr(txq, loc, dseg, dptr, dlen, olx);
2353                 /*
2354                  * WQE is built, update the loop parameters
2355                  * and go to the next packet.
2356                  */
2357                 txq->wqe_ci += (ds + 3) / 4;
2358                 loc->wqe_free -= (ds + 3) / 4;
2359                 if (MLX5_TXOFF_CONFIG(INLINE))
2360                         txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
2361                 --loc->elts_free;
2362                 ++loc->pkts_sent;
2363                 --pkts_n;
2364                 if (unlikely(!pkts_n || !loc->elts_free || !loc->wqe_free))
2365                         return MLX5_TXCMP_CODE_EXIT;
2366                 loc->mbuf = *pkts++;
2367                 if (pkts_n > 1)
2368                         rte_prefetch0(*pkts);
2369                 if (MLX5_TXOFF_CONFIG(MULTI) &&
2370                     unlikely(NB_SEGS(loc->mbuf) > 1))
2371                         return MLX5_TXCMP_CODE_MULTI;
2372                 if (likely(!(loc->mbuf->ol_flags & PKT_TX_TCP_SEG)))
2373                         return MLX5_TXCMP_CODE_SINGLE;
2374                 /* Continue with the next TSO packet. */
2375         }
2376         MLX5_ASSERT(false);
2377 }
2378
2379 /**
2380  * Analyze the packet and select the best method to send.
2381  *
2382  * @param txq
2383  *   Pointer to TX queue structure.
2384  * @param loc
2385  *   Pointer to burst routine local context.
2386  * @param olx
2387  *   Configured Tx offloads mask. It is fully defined at
2388  *   compile time and may be used for optimization.
2389  * @param newp
2390  *   The predefined flag whether do complete check for
2391  *   multi-segment packets and TSO.
2392  *
2393  * @return
2394  *  MLX5_TXCMP_CODE_MULTI - multi-segment packet encountered.
2395  *  MLX5_TXCMP_CODE_TSO - TSO required, use TSO/LSO.
2396  *  MLX5_TXCMP_CODE_SINGLE - single-segment packet, use SEND.
2397  *  MLX5_TXCMP_CODE_EMPW - single-segment packet, use MPW.
2398  */
2399 static __rte_always_inline enum mlx5_txcmp_code
2400 mlx5_tx_able_to_empw(struct mlx5_txq_data *__rte_restrict txq,
2401                      struct mlx5_txq_local *__rte_restrict loc,
2402                      unsigned int olx,
2403                      bool newp)
2404 {
2405         /* Check for multi-segment packet. */
2406         if (newp &&
2407             MLX5_TXOFF_CONFIG(MULTI) &&
2408             unlikely(NB_SEGS(loc->mbuf) > 1))
2409                 return MLX5_TXCMP_CODE_MULTI;
2410         /* Check for TSO packet. */
2411         if (newp &&
2412             MLX5_TXOFF_CONFIG(TSO) &&
2413             unlikely(loc->mbuf->ol_flags & PKT_TX_TCP_SEG))
2414                 return MLX5_TXCMP_CODE_TSO;
2415         /* Check if eMPW is enabled at all. */
2416         if (!MLX5_TXOFF_CONFIG(EMPW))
2417                 return MLX5_TXCMP_CODE_SINGLE;
2418         /* Check if eMPW can be engaged. */
2419         if (MLX5_TXOFF_CONFIG(VLAN) &&
2420             unlikely(loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) &&
2421                 (!MLX5_TXOFF_CONFIG(INLINE) ||
2422                  unlikely((rte_pktmbuf_data_len(loc->mbuf) +
2423                            sizeof(struct rte_vlan_hdr)) > txq->inlen_empw))) {
2424                 /*
2425                  * eMPW does not support VLAN insertion offload, we have to
2426                  * inline the entire packet but packet is too long for inlining.
2427                  */
2428                 return MLX5_TXCMP_CODE_SINGLE;
2429         }
2430         return MLX5_TXCMP_CODE_EMPW;
2431 }
2432
2433 /**
2434  * Check the next packet attributes to match with the eMPW batch ones.
2435  * In addition, for legacy MPW the packet length is checked either.
2436  *
2437  * @param txq
2438  *   Pointer to TX queue structure.
2439  * @param es
2440  *   Pointer to Ethernet Segment of eMPW batch.
2441  * @param loc
2442  *   Pointer to burst routine local context.
2443  * @param dlen
2444  *   Length of previous packet in MPW descriptor.
2445  * @param olx
2446  *   Configured Tx offloads mask. It is fully defined at
2447  *   compile time and may be used for optimization.
2448  *
2449  * @return
2450  *  true - packet match with eMPW batch attributes.
2451  *  false - no match, eMPW should be restarted.
2452  */
2453 static __rte_always_inline bool
2454 mlx5_tx_match_empw(struct mlx5_txq_data *__rte_restrict txq,
2455                    struct mlx5_wqe_eseg *__rte_restrict es,
2456                    struct mlx5_txq_local *__rte_restrict loc,
2457                    uint32_t dlen,
2458                    unsigned int olx)
2459 {
2460         uint8_t swp_flags = 0;
2461
2462         /* Compare the checksum flags, if any. */
2463         if (MLX5_TXOFF_CONFIG(CSUM) &&
2464             txq_ol_cksum_to_cs(loc->mbuf) != es->cs_flags)
2465                 return false;
2466         /* Compare the Software Parser offsets and flags. */
2467         if (MLX5_TXOFF_CONFIG(SWP) &&
2468             (es->swp_offs != txq_mbuf_to_swp(loc, &swp_flags, olx) ||
2469              es->swp_flags != swp_flags))
2470                 return false;
2471         /* Fill metadata field if needed. */
2472         if (MLX5_TXOFF_CONFIG(METADATA) &&
2473                 es->metadata != (loc->mbuf->ol_flags & PKT_TX_DYNF_METADATA ?
2474                 rte_cpu_to_be_32(*RTE_FLOW_DYNF_METADATA(loc->mbuf)) : 0))
2475                 return false;
2476         /* Legacy MPW can send packets with the same length only. */
2477         if (MLX5_TXOFF_CONFIG(MPW) &&
2478             dlen != rte_pktmbuf_data_len(loc->mbuf))
2479                 return false;
2480         /* There must be no VLAN packets in eMPW loop. */
2481         if (MLX5_TXOFF_CONFIG(VLAN))
2482                 MLX5_ASSERT(!(loc->mbuf->ol_flags & PKT_TX_VLAN_PKT));
2483         /* Check if the scheduling is requested. */
2484         if (MLX5_TXOFF_CONFIG(TXPP) &&
2485             loc->mbuf->ol_flags & txq->ts_mask)
2486                 return false;
2487         return true;
2488 }
2489
2490 /**
2491  * Update send loop variables and WQE for eMPW loop without data inlining.
2492  * Number of Data Segments is equal to the number of sent packets.
2493  *
2494  * @param txq
2495  *   Pointer to TX queue structure.
2496  * @param loc
2497  *   Pointer to burst routine local context.
2498  * @param ds
2499  *   Number of packets/Data Segments/Packets.
2500  * @param slen
2501  *   Accumulated statistics, bytes sent.
2502  * @param olx
2503  *   Configured Tx offloads mask. It is fully defined at
2504  *   compile time and may be used for optimization.
2505  *
2506  * @return
2507  *  true - packet match with eMPW batch attributes.
2508  *  false - no match, eMPW should be restarted.
2509  */
2510 static __rte_always_inline void
2511 mlx5_tx_sdone_empw(struct mlx5_txq_data *__rte_restrict txq,
2512                    struct mlx5_txq_local *__rte_restrict loc,
2513                    unsigned int ds,
2514                    unsigned int slen,
2515                    unsigned int olx __rte_unused)
2516 {
2517         MLX5_ASSERT(!MLX5_TXOFF_CONFIG(INLINE));
2518 #ifdef MLX5_PMD_SOFT_COUNTERS
2519         /* Update sent data bytes counter. */
2520          txq->stats.obytes += slen;
2521 #else
2522         (void)slen;
2523 #endif
2524         loc->elts_free -= ds;
2525         loc->pkts_sent += ds;
2526         ds += 2;
2527         loc->wqe_last->cseg.sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | ds);
2528         txq->wqe_ci += (ds + 3) / 4;
2529         loc->wqe_free -= (ds + 3) / 4;
2530 }
2531
2532 /**
2533  * Update send loop variables and WQE for eMPW loop with data inlining.
2534  * Gets the size of pushed descriptors and data to the WQE.
2535  *
2536  * @param txq
2537  *   Pointer to TX queue structure.
2538  * @param loc
2539  *   Pointer to burst routine local context.
2540  * @param len
2541  *   Total size of descriptor/data in bytes.
2542  * @param slen
2543  *   Accumulated statistics, data bytes sent.
2544  * @param wqem
2545  *   The base WQE for the eMPW/MPW descriptor.
2546  * @param olx
2547  *   Configured Tx offloads mask. It is fully defined at
2548  *   compile time and may be used for optimization.
2549  *
2550  * @return
2551  *  true - packet match with eMPW batch attributes.
2552  *  false - no match, eMPW should be restarted.
2553  */
2554 static __rte_always_inline void
2555 mlx5_tx_idone_empw(struct mlx5_txq_data *__rte_restrict txq,
2556                    struct mlx5_txq_local *__rte_restrict loc,
2557                    unsigned int len,
2558                    unsigned int slen,
2559                    struct mlx5_wqe *__rte_restrict wqem,
2560                    unsigned int olx __rte_unused)
2561 {
2562         struct mlx5_wqe_dseg *dseg = &wqem->dseg[0];
2563
2564         MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
2565 #ifdef MLX5_PMD_SOFT_COUNTERS
2566         /* Update sent data bytes counter. */
2567          txq->stats.obytes += slen;
2568 #else
2569         (void)slen;
2570 #endif
2571         if (MLX5_TXOFF_CONFIG(MPW) && dseg->bcount == RTE_BE32(0)) {
2572                 /*
2573                  * If the legacy MPW session contains the inline packets
2574                  * we should set the only inline data segment length
2575                  * and align the total length to the segment size.
2576                  */
2577                 MLX5_ASSERT(len > sizeof(dseg->bcount));
2578                 dseg->bcount = rte_cpu_to_be_32((len - sizeof(dseg->bcount)) |
2579                                                 MLX5_ETH_WQE_DATA_INLINE);
2580                 len = (len + MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE + 2;
2581         } else {
2582                 /*
2583                  * The session is not legacy MPW or contains the
2584                  * data buffer pointer segments.
2585                  */
2586                 MLX5_ASSERT((len % MLX5_WSEG_SIZE) == 0);
2587                 len = len / MLX5_WSEG_SIZE + 2;
2588         }
2589         wqem->cseg.sq_ds = rte_cpu_to_be_32(txq->qp_num_8s | len);
2590         txq->wqe_ci += (len + 3) / 4;
2591         loc->wqe_free -= (len + 3) / 4;
2592         loc->wqe_last = wqem;
2593 }
2594
2595 /**
2596  * The set of Tx burst functions for single-segment packets without TSO
2597  * and with Multi-Packet Writing feature support.
2598  * Supports all types of Tx offloads, except multi-packets and TSO.
2599  *
2600  * Uses MLX5_OPCODE_EMPW to build WQEs if possible and sends as many packet
2601  * per WQE as it can. If eMPW is not configured or packet can not be sent with
2602  * eMPW (VLAN insertion) the ordinary SEND opcode is used and only one packet
2603  * placed in WQE.
2604  *
2605  * Functions stop sending if it encounters the multi-segment packet or packet
2606  * with TSO requested.
2607  *
2608  * The routines are responsible for storing processed mbuf into elts ring buffer
2609  * and update elts_head if inlining offload is requested. Otherwise the copying
2610  * mbufs to elts can be postponed and completed at the end of burst routine.
2611  *
2612  * @param txq
2613  *   Pointer to TX queue structure.
2614  * @param[in] pkts
2615  *   Packets to transmit.
2616  * @param pkts_n
2617  *   Number of packets in array.
2618  * @param loc
2619  *   Pointer to burst routine local context.
2620  * @param olx
2621  *   Configured Tx offloads mask. It is fully defined at
2622  *   compile time and may be used for optimization.
2623  *
2624  * @return
2625  *   MLX5_TXCMP_CODE_EXIT - sending is done or impossible.
2626  *   MLX5_TXCMP_CODE_ERROR - some unrecoverable error occurred.
2627  *   MLX5_TXCMP_CODE_MULTI - multi-segment packet encountered.
2628  *   MLX5_TXCMP_CODE_TSO - TSO packet encountered.
2629  *   MLX5_TXCMP_CODE_SINGLE - used inside functions set.
2630  *   MLX5_TXCMP_CODE_EMPW - used inside functions set.
2631  *
2632  * Local context variables updated.
2633  *
2634  *
2635  * The routine sends packets with MLX5_OPCODE_EMPW
2636  * without inlining, this is dedicated optimized branch.
2637  * No VLAN insertion is supported.
2638  */
2639 static __rte_always_inline enum mlx5_txcmp_code
2640 mlx5_tx_burst_empw_simple(struct mlx5_txq_data *__rte_restrict txq,
2641                           struct rte_mbuf **__rte_restrict pkts,
2642                           unsigned int pkts_n,
2643                           struct mlx5_txq_local *__rte_restrict loc,
2644                           unsigned int olx)
2645 {
2646         /*
2647          * Subroutine is the part of mlx5_tx_burst_single() and sends
2648          * single-segment packet with eMPW opcode without data inlining.
2649          */
2650         MLX5_ASSERT(!MLX5_TXOFF_CONFIG(INLINE));
2651         MLX5_ASSERT(MLX5_TXOFF_CONFIG(EMPW));
2652         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
2653         MLX5_ASSERT(pkts_n > loc->pkts_sent);
2654         pkts += loc->pkts_sent + 1;
2655         pkts_n -= loc->pkts_sent;
2656         for (;;) {
2657                 struct mlx5_wqe_dseg *__rte_restrict dseg;
2658                 struct mlx5_wqe_eseg *__rte_restrict eseg;
2659                 enum mlx5_txcmp_code ret;
2660                 unsigned int part, loop;
2661                 unsigned int slen = 0;
2662
2663 next_empw:
2664                 MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
2665                 if (MLX5_TXOFF_CONFIG(TXPP)) {
2666                         enum mlx5_txcmp_code wret;
2667
2668                         /* Generate WAIT for scheduling if requested. */
2669                         wret = mlx5_tx_schedule_send(txq, loc, olx);
2670                         if (wret == MLX5_TXCMP_CODE_EXIT)
2671                                 return MLX5_TXCMP_CODE_EXIT;
2672                         if (wret == MLX5_TXCMP_CODE_ERROR)
2673                                 return MLX5_TXCMP_CODE_ERROR;
2674                 }
2675                 part = RTE_MIN(pkts_n, MLX5_TXOFF_CONFIG(MPW) ?
2676                                        MLX5_MPW_MAX_PACKETS :
2677                                        MLX5_EMPW_MAX_PACKETS);
2678                 if (unlikely(loc->elts_free < part)) {
2679                         /* We have no enough elts to save all mbufs. */
2680                         if (unlikely(loc->elts_free < MLX5_EMPW_MIN_PACKETS))
2681                                 return MLX5_TXCMP_CODE_EXIT;
2682                         /* But we still able to send at least minimal eMPW. */
2683                         part = loc->elts_free;
2684                 }
2685                 /* Check whether we have enough WQEs */
2686                 if (unlikely(loc->wqe_free < ((2 + part + 3) / 4))) {
2687                         if (unlikely(loc->wqe_free <
2688                                 ((2 + MLX5_EMPW_MIN_PACKETS + 3) / 4)))
2689                                 return MLX5_TXCMP_CODE_EXIT;
2690                         part = (loc->wqe_free * 4) - 2;
2691                 }
2692                 if (likely(part > 1))
2693                         rte_prefetch0(*pkts);
2694                 loc->wqe_last = txq->wqes + (txq->wqe_ci & txq->wqe_m);
2695                 /*
2696                  * Build eMPW title WQEBB:
2697                  * - Control Segment, eMPW opcode
2698                  * - Ethernet Segment, no inline
2699                  */
2700                 mlx5_tx_cseg_init(txq, loc, loc->wqe_last, part + 2,
2701                                   MLX5_OPCODE_ENHANCED_MPSW, olx);
2702                 mlx5_tx_eseg_none(txq, loc, loc->wqe_last,
2703                                   olx & ~MLX5_TXOFF_CONFIG_VLAN);
2704                 eseg = &loc->wqe_last->eseg;
2705                 dseg = &loc->wqe_last->dseg[0];
2706                 loop = part;
2707                 /* Store the packet length for legacy MPW. */
2708                 if (MLX5_TXOFF_CONFIG(MPW))
2709                         eseg->mss = rte_cpu_to_be_16
2710                                         (rte_pktmbuf_data_len(loc->mbuf));
2711                 for (;;) {
2712                         uint32_t dlen = rte_pktmbuf_data_len(loc->mbuf);
2713 #ifdef MLX5_PMD_SOFT_COUNTERS
2714                         /* Update sent data bytes counter. */
2715                         slen += dlen;
2716 #endif
2717                         mlx5_tx_dseg_ptr
2718                                 (txq, loc, dseg,
2719                                  rte_pktmbuf_mtod(loc->mbuf, uint8_t *),
2720                                  dlen, olx);
2721                         if (unlikely(--loop == 0))
2722                                 break;
2723                         loc->mbuf = *pkts++;
2724                         if (likely(loop > 1))
2725                                 rte_prefetch0(*pkts);
2726                         ret = mlx5_tx_able_to_empw(txq, loc, olx, true);
2727                         /*
2728                          * Unroll the completion code to avoid
2729                          * returning variable value - it results in
2730                          * unoptimized sequent checking in caller.
2731                          */
2732                         if (ret == MLX5_TXCMP_CODE_MULTI) {
2733                                 part -= loop;
2734                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
2735                                 if (unlikely(!loc->elts_free ||
2736                                              !loc->wqe_free))
2737                                         return MLX5_TXCMP_CODE_EXIT;
2738                                 return MLX5_TXCMP_CODE_MULTI;
2739                         }
2740                         MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
2741                         if (ret == MLX5_TXCMP_CODE_TSO) {
2742                                 part -= loop;
2743                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
2744                                 if (unlikely(!loc->elts_free ||
2745                                              !loc->wqe_free))
2746                                         return MLX5_TXCMP_CODE_EXIT;
2747                                 return MLX5_TXCMP_CODE_TSO;
2748                         }
2749                         if (ret == MLX5_TXCMP_CODE_SINGLE) {
2750                                 part -= loop;
2751                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
2752                                 if (unlikely(!loc->elts_free ||
2753                                              !loc->wqe_free))
2754                                         return MLX5_TXCMP_CODE_EXIT;
2755                                 return MLX5_TXCMP_CODE_SINGLE;
2756                         }
2757                         if (ret != MLX5_TXCMP_CODE_EMPW) {
2758                                 MLX5_ASSERT(false);
2759                                 part -= loop;
2760                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
2761                                 return MLX5_TXCMP_CODE_ERROR;
2762                         }
2763                         /*
2764                          * Check whether packet parameters coincide
2765                          * within assumed eMPW batch:
2766                          * - check sum settings
2767                          * - metadata value
2768                          * - software parser settings
2769                          * - packets length (legacy MPW only)
2770                          * - scheduling is not required
2771                          */
2772                         if (!mlx5_tx_match_empw(txq, eseg, loc, dlen, olx)) {
2773                                 MLX5_ASSERT(loop);
2774                                 part -= loop;
2775                                 mlx5_tx_sdone_empw(txq, loc, part, slen, olx);
2776                                 if (unlikely(!loc->elts_free ||
2777                                              !loc->wqe_free))
2778                                         return MLX5_TXCMP_CODE_EXIT;
2779                                 pkts_n -= part;
2780                                 goto next_empw;
2781                         }
2782                         /* Packet attributes match, continue the same eMPW. */
2783                         ++dseg;
2784                         if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
2785                                 dseg = (struct mlx5_wqe_dseg *)txq->wqes;
2786                 }
2787                 /* eMPW is built successfully, update loop parameters. */
2788                 MLX5_ASSERT(!loop);
2789                 MLX5_ASSERT(pkts_n >= part);
2790 #ifdef MLX5_PMD_SOFT_COUNTERS
2791                 /* Update sent data bytes counter. */
2792                 txq->stats.obytes += slen;
2793 #endif
2794                 loc->elts_free -= part;
2795                 loc->pkts_sent += part;
2796                 txq->wqe_ci += (2 + part + 3) / 4;
2797                 loc->wqe_free -= (2 + part + 3) / 4;
2798                 pkts_n -= part;
2799                 if (unlikely(!pkts_n || !loc->elts_free || !loc->wqe_free))
2800                         return MLX5_TXCMP_CODE_EXIT;
2801                 loc->mbuf = *pkts++;
2802                 ret = mlx5_tx_able_to_empw(txq, loc, olx, true);
2803                 if (unlikely(ret != MLX5_TXCMP_CODE_EMPW))
2804                         return ret;
2805                 /* Continue sending eMPW batches. */
2806         }
2807         MLX5_ASSERT(false);
2808 }
2809
2810 /**
2811  * The routine sends packets with MLX5_OPCODE_EMPW
2812  * with inlining, optionally supports VLAN insertion.
2813  */
2814 static __rte_always_inline enum mlx5_txcmp_code
2815 mlx5_tx_burst_empw_inline(struct mlx5_txq_data *__rte_restrict txq,
2816                           struct rte_mbuf **__rte_restrict pkts,
2817                           unsigned int pkts_n,
2818                           struct mlx5_txq_local *__rte_restrict loc,
2819                           unsigned int olx)
2820 {
2821         /*
2822          * Subroutine is the part of mlx5_tx_burst_single() and sends
2823          * single-segment packet with eMPW opcode with data inlining.
2824          */
2825         MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
2826         MLX5_ASSERT(MLX5_TXOFF_CONFIG(EMPW));
2827         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
2828         MLX5_ASSERT(pkts_n > loc->pkts_sent);
2829         pkts += loc->pkts_sent + 1;
2830         pkts_n -= loc->pkts_sent;
2831         for (;;) {
2832                 struct mlx5_wqe_dseg *__rte_restrict dseg;
2833                 struct mlx5_wqe *__rte_restrict wqem;
2834                 enum mlx5_txcmp_code ret;
2835                 unsigned int room, part, nlim;
2836                 unsigned int slen = 0;
2837
2838                 MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
2839                 if (MLX5_TXOFF_CONFIG(TXPP)) {
2840                         enum mlx5_txcmp_code wret;
2841
2842                         /* Generate WAIT for scheduling if requested. */
2843                         wret = mlx5_tx_schedule_send(txq, loc, olx);
2844                         if (wret == MLX5_TXCMP_CODE_EXIT)
2845                                 return MLX5_TXCMP_CODE_EXIT;
2846                         if (wret == MLX5_TXCMP_CODE_ERROR)
2847                                 return MLX5_TXCMP_CODE_ERROR;
2848                 }
2849                 /*
2850                  * Limits the amount of packets in one WQE
2851                  * to improve CQE latency generation.
2852                  */
2853                 nlim = RTE_MIN(pkts_n, MLX5_TXOFF_CONFIG(MPW) ?
2854                                        MLX5_MPW_INLINE_MAX_PACKETS :
2855                                        MLX5_EMPW_MAX_PACKETS);
2856                 /* Check whether we have minimal amount WQEs */
2857                 if (unlikely(loc->wqe_free <
2858                             ((2 + MLX5_EMPW_MIN_PACKETS + 3) / 4)))
2859                         return MLX5_TXCMP_CODE_EXIT;
2860                 if (likely(pkts_n > 1))
2861                         rte_prefetch0(*pkts);
2862                 wqem = txq->wqes + (txq->wqe_ci & txq->wqe_m);
2863                 /*
2864                  * Build eMPW title WQEBB:
2865                  * - Control Segment, eMPW opcode, zero DS
2866                  * - Ethernet Segment, no inline
2867                  */
2868                 mlx5_tx_cseg_init(txq, loc, wqem, 0,
2869                                   MLX5_OPCODE_ENHANCED_MPSW, olx);
2870                 mlx5_tx_eseg_none(txq, loc, wqem,
2871                                   olx & ~MLX5_TXOFF_CONFIG_VLAN);
2872                 dseg = &wqem->dseg[0];
2873                 /* Store the packet length for legacy MPW. */
2874                 if (MLX5_TXOFF_CONFIG(MPW))
2875                         wqem->eseg.mss = rte_cpu_to_be_16
2876                                          (rte_pktmbuf_data_len(loc->mbuf));
2877                 room = RTE_MIN(MLX5_WQE_SIZE_MAX / MLX5_WQE_SIZE,
2878                                loc->wqe_free) * MLX5_WQE_SIZE -
2879                                         MLX5_WQE_CSEG_SIZE -
2880                                         MLX5_WQE_ESEG_SIZE;
2881                 /* Limit the room for legacy MPW sessions for performance. */
2882                 if (MLX5_TXOFF_CONFIG(MPW))
2883                         room = RTE_MIN(room,
2884                                        RTE_MAX(txq->inlen_empw +
2885                                                sizeof(dseg->bcount) +
2886                                                (MLX5_TXOFF_CONFIG(VLAN) ?
2887                                                sizeof(struct rte_vlan_hdr) : 0),
2888                                                MLX5_MPW_INLINE_MAX_PACKETS *
2889                                                MLX5_WQE_DSEG_SIZE));
2890                 /* Build WQE till we have space, packets and resources. */
2891                 part = room;
2892                 for (;;) {
2893                         uint32_t dlen = rte_pktmbuf_data_len(loc->mbuf);
2894                         uint8_t *dptr = rte_pktmbuf_mtod(loc->mbuf, uint8_t *);
2895                         unsigned int tlen;
2896
2897                         MLX5_ASSERT(room >= MLX5_WQE_DSEG_SIZE);
2898                         MLX5_ASSERT((room % MLX5_WQE_DSEG_SIZE) == 0);
2899                         MLX5_ASSERT((uintptr_t)dseg < (uintptr_t)txq->wqes_end);
2900                         /*
2901                          * Some Tx offloads may cause an error if packet is not
2902                          * long enough, check against assumed minimal length.
2903                          */
2904                         if (unlikely(dlen <= MLX5_ESEG_MIN_INLINE_SIZE)) {
2905                                 part -= room;
2906                                 if (unlikely(!part))
2907                                         return MLX5_TXCMP_CODE_ERROR;
2908                                 /*
2909                                  * We have some successfully built
2910                                  * packet Data Segments to send.
2911                                  */
2912                                 mlx5_tx_idone_empw(txq, loc, part,
2913                                                    slen, wqem, olx);
2914                                 return MLX5_TXCMP_CODE_ERROR;
2915                         }
2916                         /* Inline or not inline - that's the Question. */
2917                         if (dlen > txq->inlen_empw ||
2918                             loc->mbuf->ol_flags & PKT_TX_DYNF_NOINLINE)
2919                                 goto pointer_empw;
2920                         if (MLX5_TXOFF_CONFIG(MPW)) {
2921                                 if (dlen > txq->inlen_send)
2922                                         goto pointer_empw;
2923                                 tlen = dlen;
2924                                 if (part == room) {
2925                                         /* Open new inline MPW session. */
2926                                         tlen += sizeof(dseg->bcount);
2927                                         dseg->bcount = RTE_BE32(0);
2928                                         dseg = RTE_PTR_ADD
2929                                                 (dseg, sizeof(dseg->bcount));
2930                                 } else {
2931                                         /*
2932                                          * No pointer and inline descriptor
2933                                          * intermix for legacy MPW sessions.
2934                                          */
2935                                         if (wqem->dseg[0].bcount)
2936                                                 break;
2937                                 }
2938                         } else {
2939                                 tlen = sizeof(dseg->bcount) + dlen;
2940                         }
2941                         /* Inline entire packet, optional VLAN insertion. */
2942                         if (MLX5_TXOFF_CONFIG(VLAN) &&
2943                             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
2944                                 /*
2945                                  * The packet length must be checked in
2946                                  * mlx5_tx_able_to_empw() and packet
2947                                  * fits into inline length guaranteed.
2948                                  */
2949                                 MLX5_ASSERT((dlen +
2950                                              sizeof(struct rte_vlan_hdr)) <=
2951                                             txq->inlen_empw);
2952                                 tlen += sizeof(struct rte_vlan_hdr);
2953                                 if (room < tlen)
2954                                         break;
2955                                 dseg = mlx5_tx_dseg_vlan(txq, loc, dseg,
2956                                                          dptr, dlen, olx);
2957 #ifdef MLX5_PMD_SOFT_COUNTERS
2958                                 /* Update sent data bytes counter. */
2959                                 slen += sizeof(struct rte_vlan_hdr);
2960 #endif
2961                         } else {
2962                                 if (room < tlen)
2963                                         break;
2964                                 dseg = mlx5_tx_dseg_empw(txq, loc, dseg,
2965                                                          dptr, dlen, olx);
2966                         }
2967                         if (!MLX5_TXOFF_CONFIG(MPW))
2968                                 tlen = RTE_ALIGN(tlen, MLX5_WSEG_SIZE);
2969                         MLX5_ASSERT(room >= tlen);
2970                         room -= tlen;
2971                         /*
2972                          * Packet data are completely inline,
2973                          * we can try to free the packet.
2974                          */
2975                         if (likely(loc->pkts_sent == loc->mbuf_free)) {
2976                                 /*
2977                                  * All the packets from the burst beginning
2978                                  * are inline, we can free mbufs directly
2979                                  * from the origin array on tx_burst exit().
2980                                  */
2981                                 loc->mbuf_free++;
2982                                 goto next_mbuf;
2983                         }
2984                         /*
2985                          * In order no to call rte_pktmbuf_free_seg() here,
2986                          * in the most inner loop (that might be very
2987                          * expensive) we just save the mbuf in elts.
2988                          */
2989                         txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
2990                         loc->elts_free--;
2991                         goto next_mbuf;
2992 pointer_empw:
2993                         /*
2994                          * No pointer and inline descriptor
2995                          * intermix for legacy MPW sessions.
2996                          */
2997                         if (MLX5_TXOFF_CONFIG(MPW) &&
2998                             part != room &&
2999                             wqem->dseg[0].bcount == RTE_BE32(0))
3000                                 break;
3001                         /*
3002                          * Not inlinable VLAN packets are
3003                          * proceeded outside of this routine.
3004                          */
3005                         MLX5_ASSERT(room >= MLX5_WQE_DSEG_SIZE);
3006                         if (MLX5_TXOFF_CONFIG(VLAN))
3007                                 MLX5_ASSERT(!(loc->mbuf->ol_flags &
3008                                             PKT_TX_VLAN_PKT));
3009                         mlx5_tx_dseg_ptr(txq, loc, dseg, dptr, dlen, olx);
3010                         /* We have to store mbuf in elts.*/
3011                         txq->elts[txq->elts_head++ & txq->elts_m] = loc->mbuf;
3012                         loc->elts_free--;
3013                         room -= MLX5_WQE_DSEG_SIZE;
3014                         /* Ring buffer wraparound is checked at the loop end.*/
3015                         ++dseg;
3016 next_mbuf:
3017 #ifdef MLX5_PMD_SOFT_COUNTERS
3018                         /* Update sent data bytes counter. */
3019                         slen += dlen;
3020 #endif
3021                         loc->pkts_sent++;
3022                         pkts_n--;
3023                         if (unlikely(!pkts_n || !loc->elts_free)) {
3024                                 /*
3025                                  * We have no resources/packets to
3026                                  * continue build descriptors.
3027                                  */
3028                                 part -= room;
3029                                 mlx5_tx_idone_empw(txq, loc, part,
3030                                                    slen, wqem, olx);
3031                                 return MLX5_TXCMP_CODE_EXIT;
3032                         }
3033                         loc->mbuf = *pkts++;
3034                         if (likely(pkts_n > 1))
3035                                 rte_prefetch0(*pkts);
3036                         ret = mlx5_tx_able_to_empw(txq, loc, olx, true);
3037                         /*
3038                          * Unroll the completion code to avoid
3039                          * returning variable value - it results in
3040                          * unoptimized sequent checking in caller.
3041                          */
3042                         if (ret == MLX5_TXCMP_CODE_MULTI) {
3043                                 part -= room;
3044                                 mlx5_tx_idone_empw(txq, loc, part,
3045                                                    slen, wqem, olx);
3046                                 if (unlikely(!loc->elts_free ||
3047                                              !loc->wqe_free))
3048                                         return MLX5_TXCMP_CODE_EXIT;
3049                                 return MLX5_TXCMP_CODE_MULTI;
3050                         }
3051                         MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
3052                         if (ret == MLX5_TXCMP_CODE_TSO) {
3053                                 part -= room;
3054                                 mlx5_tx_idone_empw(txq, loc, part,
3055                                                    slen, wqem, olx);
3056                                 if (unlikely(!loc->elts_free ||
3057                                              !loc->wqe_free))
3058                                         return MLX5_TXCMP_CODE_EXIT;
3059                                 return MLX5_TXCMP_CODE_TSO;
3060                         }
3061                         if (ret == MLX5_TXCMP_CODE_SINGLE) {
3062                                 part -= room;
3063                                 mlx5_tx_idone_empw(txq, loc, part,
3064                                                    slen, wqem, olx);
3065                                 if (unlikely(!loc->elts_free ||
3066                                              !loc->wqe_free))
3067                                         return MLX5_TXCMP_CODE_EXIT;
3068                                 return MLX5_TXCMP_CODE_SINGLE;
3069                         }
3070                         if (ret != MLX5_TXCMP_CODE_EMPW) {
3071                                 MLX5_ASSERT(false);
3072                                 part -= room;
3073                                 mlx5_tx_idone_empw(txq, loc, part,
3074                                                    slen, wqem, olx);
3075                                 return MLX5_TXCMP_CODE_ERROR;
3076                         }
3077                         /* Check if we have minimal room left. */
3078                         nlim--;
3079                         if (unlikely(!nlim || room < MLX5_WQE_DSEG_SIZE))
3080                                 break;
3081                         /*
3082                          * Check whether packet parameters coincide
3083                          * within assumed eMPW batch:
3084                          * - check sum settings
3085                          * - metadata value
3086                          * - software parser settings
3087                          * - packets length (legacy MPW only)
3088                          * - scheduling is not required
3089                          */
3090                         if (!mlx5_tx_match_empw(txq, &wqem->eseg,
3091                                                 loc, dlen, olx))
3092                                 break;
3093                         /* Packet attributes match, continue the same eMPW. */
3094                         if ((uintptr_t)dseg >= (uintptr_t)txq->wqes_end)
3095                                 dseg = (struct mlx5_wqe_dseg *)txq->wqes;
3096                 }
3097                 /*
3098                  * We get here to close an existing eMPW
3099                  * session and start the new one.
3100                  */
3101                 MLX5_ASSERT(pkts_n);
3102                 part -= room;
3103                 if (unlikely(!part))
3104                         return MLX5_TXCMP_CODE_EXIT;
3105                 mlx5_tx_idone_empw(txq, loc, part, slen, wqem, olx);
3106                 if (unlikely(!loc->elts_free ||
3107                              !loc->wqe_free))
3108                         return MLX5_TXCMP_CODE_EXIT;
3109                 /* Continue the loop with new eMPW session. */
3110         }
3111         MLX5_ASSERT(false);
3112 }
3113
3114 /**
3115  * The routine sends packets with ordinary MLX5_OPCODE_SEND.
3116  * Data inlining and VLAN insertion are supported.
3117  */
3118 static __rte_always_inline enum mlx5_txcmp_code
3119 mlx5_tx_burst_single_send(struct mlx5_txq_data *__rte_restrict txq,
3120                           struct rte_mbuf **__rte_restrict pkts,
3121                           unsigned int pkts_n,
3122                           struct mlx5_txq_local *__rte_restrict loc,
3123                           unsigned int olx)
3124 {
3125         /*
3126          * Subroutine is the part of mlx5_tx_burst_single()
3127          * and sends single-segment packet with SEND opcode.
3128          */
3129         MLX5_ASSERT(loc->elts_free && loc->wqe_free);
3130         MLX5_ASSERT(pkts_n > loc->pkts_sent);
3131         pkts += loc->pkts_sent + 1;
3132         pkts_n -= loc->pkts_sent;
3133         for (;;) {
3134                 struct mlx5_wqe *__rte_restrict wqe;
3135                 enum mlx5_txcmp_code ret;
3136
3137                 MLX5_ASSERT(NB_SEGS(loc->mbuf) == 1);
3138                 if (MLX5_TXOFF_CONFIG(TXPP)) {
3139                         enum mlx5_txcmp_code wret;
3140
3141                         /* Generate WAIT for scheduling if requested. */
3142                         wret = mlx5_tx_schedule_send(txq, loc, olx);
3143                         if (wret == MLX5_TXCMP_CODE_EXIT)
3144                                 return MLX5_TXCMP_CODE_EXIT;
3145                         if (wret == MLX5_TXCMP_CODE_ERROR)
3146                                 return MLX5_TXCMP_CODE_ERROR;
3147                 }
3148                 if (MLX5_TXOFF_CONFIG(INLINE)) {
3149                         unsigned int inlen, vlan = 0;
3150
3151                         inlen = rte_pktmbuf_data_len(loc->mbuf);
3152                         if (MLX5_TXOFF_CONFIG(VLAN) &&
3153                             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
3154                                 vlan = sizeof(struct rte_vlan_hdr);
3155                                 inlen += vlan;
3156                         }
3157                         /*
3158                          * If inlining is enabled at configuration time
3159                          * the limit must be not less than minimal size.
3160                          * Otherwise we would do extra check for data
3161                          * size to avoid crashes due to length overflow.
3162                          */
3163                         MLX5_ASSERT(txq->inlen_send >=
3164                                     MLX5_ESEG_MIN_INLINE_SIZE);
3165                         if (inlen <= txq->inlen_send) {
3166                                 unsigned int seg_n, wqe_n;
3167
3168                                 rte_prefetch0(rte_pktmbuf_mtod
3169                                                 (loc->mbuf, uint8_t *));
3170                                 /* Check against minimal length. */
3171                                 if (inlen <= MLX5_ESEG_MIN_INLINE_SIZE)
3172                                         return MLX5_TXCMP_CODE_ERROR;
3173                                 if (loc->mbuf->ol_flags &
3174                                     PKT_TX_DYNF_NOINLINE) {
3175                                         /*
3176                                          * The hint flag not to inline packet
3177                                          * data is set. Check whether we can
3178                                          * follow the hint.
3179                                          */
3180                                         if ((!MLX5_TXOFF_CONFIG(EMPW) &&
3181                                               txq->inlen_mode) ||
3182                                             (MLX5_TXOFF_CONFIG(MPW) &&
3183                                              txq->inlen_mode)) {
3184                                                 if (inlen <= txq->inlen_send)
3185                                                         goto single_inline;
3186                                                 /*
3187                                                  * The hardware requires the
3188                                                  * minimal inline data header.
3189                                                  */
3190                                                 goto single_min_inline;
3191                                         }
3192                                         if (MLX5_TXOFF_CONFIG(VLAN) &&
3193                                             vlan && !txq->vlan_en) {
3194                                                 /*
3195                                                  * We must insert VLAN tag
3196                                                  * by software means.
3197                                                  */
3198                                                 goto single_part_inline;
3199                                         }
3200                                         goto single_no_inline;
3201                                 }
3202 single_inline:
3203                                 /*
3204                                  * Completely inlined packet data WQE:
3205                                  * - Control Segment, SEND opcode
3206                                  * - Ethernet Segment, no VLAN insertion
3207                                  * - Data inlined, VLAN optionally inserted
3208                                  * - Alignment to MLX5_WSEG_SIZE
3209                                  * Have to estimate amount of WQEBBs
3210                                  */
3211                                 seg_n = (inlen + 3 * MLX5_WSEG_SIZE -
3212                                          MLX5_ESEG_MIN_INLINE_SIZE +
3213                                          MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
3214                                 /* Check if there are enough WQEBBs. */
3215                                 wqe_n = (seg_n + 3) / 4;
3216                                 if (wqe_n > loc->wqe_free)
3217                                         return MLX5_TXCMP_CODE_EXIT;
3218                                 wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
3219                                 loc->wqe_last = wqe;
3220                                 mlx5_tx_cseg_init(txq, loc, wqe, seg_n,
3221                                                   MLX5_OPCODE_SEND, olx);
3222                                 mlx5_tx_eseg_data(txq, loc, wqe,
3223                                                   vlan, inlen, 0, olx);
3224                                 txq->wqe_ci += wqe_n;
3225                                 loc->wqe_free -= wqe_n;
3226                                 /*
3227                                  * Packet data are completely inlined,
3228                                  * free the packet immediately.
3229                                  */
3230                                 rte_pktmbuf_free_seg(loc->mbuf);
3231                         } else if ((!MLX5_TXOFF_CONFIG(EMPW) ||
3232                                      MLX5_TXOFF_CONFIG(MPW)) &&
3233                                         txq->inlen_mode) {
3234                                 /*
3235                                  * If minimal inlining is requested the eMPW
3236                                  * feature should be disabled due to data is
3237                                  * inlined into Ethernet Segment, which can
3238                                  * not contain inlined data for eMPW due to
3239                                  * segment shared for all packets.
3240                                  */
3241                                 struct mlx5_wqe_dseg *__rte_restrict dseg;
3242                                 unsigned int ds;
3243                                 uint8_t *dptr;
3244
3245                                 /*
3246                                  * The inline-mode settings require
3247                                  * to inline the specified amount of
3248                                  * data bytes to the Ethernet Segment.
3249                                  * We should check the free space in
3250                                  * WQE ring buffer to inline partially.
3251                                  */
3252 single_min_inline:
3253                                 MLX5_ASSERT(txq->inlen_send >= txq->inlen_mode);
3254                                 MLX5_ASSERT(inlen > txq->inlen_mode);
3255                                 MLX5_ASSERT(txq->inlen_mode >=
3256                                             MLX5_ESEG_MIN_INLINE_SIZE);
3257                                 /*
3258                                  * Check whether there are enough free WQEBBs:
3259                                  * - Control Segment
3260                                  * - Ethernet Segment
3261                                  * - First Segment of inlined Ethernet data
3262                                  * - ... data continued ...
3263                                  * - Finishing Data Segment of pointer type
3264                                  */
3265                                 ds = (MLX5_WQE_CSEG_SIZE +
3266                                       MLX5_WQE_ESEG_SIZE +
3267                                       MLX5_WQE_DSEG_SIZE +
3268                                       txq->inlen_mode -
3269                                       MLX5_ESEG_MIN_INLINE_SIZE +
3270                                       MLX5_WQE_DSEG_SIZE +
3271                                       MLX5_WSEG_SIZE - 1) / MLX5_WSEG_SIZE;
3272                                 if (loc->wqe_free < ((ds + 3) / 4))
3273                                         return MLX5_TXCMP_CODE_EXIT;
3274                                 /*
3275                                  * Build the ordinary SEND WQE:
3276                                  * - Control Segment
3277                                  * - Ethernet Segment, inline inlen_mode bytes
3278                                  * - Data Segment of pointer type
3279                                  */
3280                                 wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
3281                                 loc->wqe_last = wqe;
3282                                 mlx5_tx_cseg_init(txq, loc, wqe, ds,
3283                                                   MLX5_OPCODE_SEND, olx);
3284                                 dseg = mlx5_tx_eseg_data(txq, loc, wqe, vlan,
3285                                                          txq->inlen_mode,
3286                                                          0, olx);
3287                                 dptr = rte_pktmbuf_mtod(loc->mbuf, uint8_t *) +
3288                                        txq->inlen_mode - vlan;
3289                                 inlen -= txq->inlen_mode;
3290                                 mlx5_tx_dseg_ptr(txq, loc, dseg,
3291                                                  dptr, inlen, olx);
3292                                 /*
3293                                  * WQE is built, update the loop parameters
3294                                  * and got to the next packet.
3295                                  */
3296                                 txq->wqe_ci += (ds + 3) / 4;
3297                                 loc->wqe_free -= (ds + 3) / 4;
3298                                 /* We have to store mbuf in elts.*/
3299                                 MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
3300                                 txq->elts[txq->elts_head++ & txq->elts_m] =
3301                                                 loc->mbuf;
3302                                 --loc->elts_free;
3303                         } else {
3304                                 uint8_t *dptr;
3305                                 unsigned int dlen;
3306
3307                                 /*
3308                                  * Partially inlined packet data WQE, we have
3309                                  * some space in title WQEBB, we can fill it
3310                                  * with some packet data. It takes one WQEBB,
3311                                  * it is available, no extra space check:
3312                                  * - Control Segment, SEND opcode
3313                                  * - Ethernet Segment, no VLAN insertion
3314                                  * - MLX5_ESEG_MIN_INLINE_SIZE bytes of Data
3315                                  * - Data Segment, pointer type
3316                                  *
3317                                  * We also get here if VLAN insertion is not
3318                                  * supported by HW, the inline is enabled.
3319                                  */
3320 single_part_inline:
3321                                 wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
3322                                 loc->wqe_last = wqe;
3323                                 mlx5_tx_cseg_init(txq, loc, wqe, 4,
3324                                                   MLX5_OPCODE_SEND, olx);
3325                                 mlx5_tx_eseg_dmin(txq, loc, wqe, vlan, olx);
3326                                 dptr = rte_pktmbuf_mtod(loc->mbuf, uint8_t *) +
3327                                        MLX5_ESEG_MIN_INLINE_SIZE - vlan;
3328                                 /*
3329                                  * The length check is performed above, by
3330                                  * comparing with txq->inlen_send. We should
3331                                  * not get overflow here.
3332                                  */
3333                                 MLX5_ASSERT(inlen > MLX5_ESEG_MIN_INLINE_SIZE);
3334                                 dlen = inlen - MLX5_ESEG_MIN_INLINE_SIZE;
3335                                 mlx5_tx_dseg_ptr(txq, loc, &wqe->dseg[1],
3336                                                  dptr, dlen, olx);
3337                                 ++txq->wqe_ci;
3338                                 --loc->wqe_free;
3339                                 /* We have to store mbuf in elts.*/
3340                                 MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE));
3341                                 txq->elts[txq->elts_head++ & txq->elts_m] =
3342                                                 loc->mbuf;
3343                                 --loc->elts_free;
3344                         }
3345 #ifdef MLX5_PMD_SOFT_COUNTERS
3346                         /* Update sent data bytes counter. */
3347                         txq->stats.obytes += vlan +
3348                                         rte_pktmbuf_data_len(loc->mbuf);
3349 #endif
3350                 } else {
3351                         /*
3352                          * No inline at all, it means the CPU cycles saving
3353                          * is prioritized at configuration, we should not
3354                          * copy any packet data to WQE.
3355                          *
3356                          * SEND WQE, one WQEBB:
3357                          * - Control Segment, SEND opcode
3358                          * - Ethernet Segment, optional VLAN, no inline
3359                          * - Data Segment, pointer type
3360                          */
3361 single_no_inline:
3362                         wqe = txq->wqes + (txq->wqe_ci & txq->wqe_m);
3363                         loc->wqe_last = wqe;
3364                         mlx5_tx_cseg_init(txq, loc, wqe, 3,
3365                                           MLX5_OPCODE_SEND, olx);
3366                         mlx5_tx_eseg_none(txq, loc, wqe, olx);
3367                         mlx5_tx_dseg_ptr
3368                                 (txq, loc, &wqe->dseg[0],
3369                                  rte_pktmbuf_mtod(loc->mbuf, uint8_t *),
3370                                  rte_pktmbuf_data_len(loc->mbuf), olx);
3371                         ++txq->wqe_ci;
3372                         --loc->wqe_free;
3373                         /*
3374                          * We should not store mbuf pointer in elts
3375                          * if no inlining is configured, this is done
3376                          * by calling routine in a batch copy.
3377                          */
3378                         MLX5_ASSERT(!MLX5_TXOFF_CONFIG(INLINE));
3379                         --loc->elts_free;
3380 #ifdef MLX5_PMD_SOFT_COUNTERS
3381                         /* Update sent data bytes counter. */
3382                         txq->stats.obytes += rte_pktmbuf_data_len(loc->mbuf);
3383                         if (MLX5_TXOFF_CONFIG(VLAN) &&
3384                             loc->mbuf->ol_flags & PKT_TX_VLAN_PKT)
3385                                 txq->stats.obytes +=
3386                                         sizeof(struct rte_vlan_hdr);
3387 #endif
3388                 }
3389                 ++loc->pkts_sent;
3390                 --pkts_n;
3391                 if (unlikely(!pkts_n || !loc->elts_free || !loc->wqe_free))
3392                         return MLX5_TXCMP_CODE_EXIT;
3393                 loc->mbuf = *pkts++;
3394                 if (pkts_n > 1)
3395                         rte_prefetch0(*pkts);
3396                 ret = mlx5_tx_able_to_empw(txq, loc, olx, true);
3397                 if (unlikely(ret != MLX5_TXCMP_CODE_SINGLE))
3398                         return ret;
3399         }
3400         MLX5_ASSERT(false);
3401 }
3402
3403 static __rte_always_inline enum mlx5_txcmp_code
3404 mlx5_tx_burst_single(struct mlx5_txq_data *__rte_restrict txq,
3405                      struct rte_mbuf **__rte_restrict pkts,
3406                      unsigned int pkts_n,
3407                      struct mlx5_txq_local *__rte_restrict loc,
3408                      unsigned int olx)
3409 {
3410         enum mlx5_txcmp_code ret;
3411
3412         ret = mlx5_tx_able_to_empw(txq, loc, olx, false);
3413         if (ret == MLX5_TXCMP_CODE_SINGLE)
3414                 goto ordinary_send;
3415         MLX5_ASSERT(ret == MLX5_TXCMP_CODE_EMPW);
3416         for (;;) {
3417                 /* Optimize for inline/no inline eMPW send. */
3418                 ret = (MLX5_TXOFF_CONFIG(INLINE)) ?
3419                         mlx5_tx_burst_empw_inline
3420                                 (txq, pkts, pkts_n, loc, olx) :
3421                         mlx5_tx_burst_empw_simple
3422                                 (txq, pkts, pkts_n, loc, olx);
3423                 if (ret != MLX5_TXCMP_CODE_SINGLE)
3424                         return ret;
3425                 /* The resources to send one packet should remain. */
3426                 MLX5_ASSERT(loc->elts_free && loc->wqe_free);
3427 ordinary_send:
3428                 ret = mlx5_tx_burst_single_send(txq, pkts, pkts_n, loc, olx);
3429                 MLX5_ASSERT(ret != MLX5_TXCMP_CODE_SINGLE);
3430                 if (ret != MLX5_TXCMP_CODE_EMPW)
3431                         return ret;
3432                 /* The resources to send one packet should remain. */
3433                 MLX5_ASSERT(loc->elts_free && loc->wqe_free);
3434         }
3435 }
3436
3437 /**
3438  * DPDK Tx callback template. This is configured template used to generate
3439  * routines optimized for specified offload setup.
3440  * One of this generated functions is chosen at SQ configuration time.
3441  *
3442  * @param txq
3443  *   Generic pointer to TX queue structure.
3444  * @param[in] pkts
3445  *   Packets to transmit.
3446  * @param pkts_n
3447  *   Number of packets in array.
3448  * @param olx
3449  *   Configured offloads mask, presents the bits of MLX5_TXOFF_CONFIG_xxx
3450  *   values. Should be static to take compile time static configuration
3451  *   advantages.
3452  *
3453  * @return
3454  *   Number of packets successfully transmitted (<= pkts_n).
3455  */
3456 static __rte_always_inline uint16_t
3457 mlx5_tx_burst_tmpl(struct mlx5_txq_data *__rte_restrict txq,
3458                    struct rte_mbuf **__rte_restrict pkts,
3459                    uint16_t pkts_n,
3460                    unsigned int olx)
3461 {
3462         struct mlx5_txq_local loc;
3463         enum mlx5_txcmp_code ret;
3464         unsigned int part;
3465
3466         MLX5_ASSERT(txq->elts_s >= (uint16_t)(txq->elts_head - txq->elts_tail));
3467         MLX5_ASSERT(txq->wqe_s >= (uint16_t)(txq->wqe_ci - txq->wqe_pi));
3468         if (unlikely(!pkts_n))
3469                 return 0;
3470         if (MLX5_TXOFF_CONFIG(INLINE))
3471                 loc.mbuf_free = 0;
3472         loc.pkts_sent = 0;
3473         loc.pkts_copy = 0;
3474         loc.wqe_last = NULL;
3475
3476 send_loop:
3477         loc.pkts_loop = loc.pkts_sent;
3478         /*
3479          * Check if there are some CQEs, if any:
3480          * - process an encountered errors
3481          * - process the completed WQEs
3482          * - free related mbufs
3483          * - doorbell the NIC about processed CQEs
3484          */
3485         rte_prefetch0(*(pkts + loc.pkts_sent));
3486         mlx5_tx_handle_completion(txq, olx);
3487         /*
3488          * Calculate the number of available resources - elts and WQEs.
3489          * There are two possible different scenarios:
3490          * - no data inlining into WQEs, one WQEBB may contains up to
3491          *   four packets, in this case elts become scarce resource
3492          * - data inlining into WQEs, one packet may require multiple
3493          *   WQEBBs, the WQEs become the limiting factor.
3494          */
3495         MLX5_ASSERT(txq->elts_s >= (uint16_t)(txq->elts_head - txq->elts_tail));
3496         loc.elts_free = txq->elts_s -
3497                                 (uint16_t)(txq->elts_head - txq->elts_tail);
3498         MLX5_ASSERT(txq->wqe_s >= (uint16_t)(txq->wqe_ci - txq->wqe_pi));
3499         loc.wqe_free = txq->wqe_s -
3500                                 (uint16_t)(txq->wqe_ci - txq->wqe_pi);
3501         if (unlikely(!loc.elts_free || !loc.wqe_free))
3502                 goto burst_exit;
3503         for (;;) {
3504                 /*
3505                  * Fetch the packet from array. Usually this is the first
3506                  * packet in series of multi/single segment packets.
3507                  */
3508                 loc.mbuf = *(pkts + loc.pkts_sent);
3509                 /* Dedicated branch for multi-segment packets. */
3510                 if (MLX5_TXOFF_CONFIG(MULTI) &&
3511                     unlikely(NB_SEGS(loc.mbuf) > 1)) {
3512                         /*
3513                          * Multi-segment packet encountered.
3514                          * Hardware is able to process it only
3515                          * with SEND/TSO opcodes, one packet
3516                          * per WQE, do it in dedicated routine.
3517                          */
3518 enter_send_multi:
3519                         MLX5_ASSERT(loc.pkts_sent >= loc.pkts_copy);
3520                         part = loc.pkts_sent - loc.pkts_copy;
3521                         if (!MLX5_TXOFF_CONFIG(INLINE) && part) {
3522                                 /*
3523                                  * There are some single-segment mbufs not
3524                                  * stored in elts. The mbufs must be in the
3525                                  * same order as WQEs, so we must copy the
3526                                  * mbufs to elts here, before the coming
3527                                  * multi-segment packet mbufs is appended.
3528                                  */
3529                                 mlx5_tx_copy_elts(txq, pkts + loc.pkts_copy,
3530                                                   part, olx);
3531                                 loc.pkts_copy = loc.pkts_sent;
3532                         }
3533                         MLX5_ASSERT(pkts_n > loc.pkts_sent);
3534                         ret = mlx5_tx_burst_mseg(txq, pkts, pkts_n, &loc, olx);
3535                         if (!MLX5_TXOFF_CONFIG(INLINE))
3536                                 loc.pkts_copy = loc.pkts_sent;
3537                         /*
3538                          * These returned code checks are supposed
3539                          * to be optimized out due to routine inlining.
3540                          */
3541                         if (ret == MLX5_TXCMP_CODE_EXIT) {
3542                                 /*
3543                                  * The routine returns this code when
3544                                  * all packets are sent or there is no
3545                                  * enough resources to complete request.
3546                                  */
3547                                 break;
3548                         }
3549                         if (ret == MLX5_TXCMP_CODE_ERROR) {
3550                                 /*
3551                                  * The routine returns this code when some error
3552                                  * in the incoming packets format occurred.
3553                                  */
3554                                 txq->stats.oerrors++;
3555                                 break;
3556                         }
3557                         if (ret == MLX5_TXCMP_CODE_SINGLE) {
3558                                 /*
3559                                  * The single-segment packet was encountered
3560                                  * in the array, try to send it with the
3561                                  * best optimized way, possible engaging eMPW.
3562                                  */
3563                                 goto enter_send_single;
3564                         }
3565                         if (MLX5_TXOFF_CONFIG(TSO) &&
3566                             ret == MLX5_TXCMP_CODE_TSO) {
3567                                 /*
3568                                  * The single-segment TSO packet was
3569                                  * encountered in the array.
3570                                  */
3571                                 goto enter_send_tso;
3572                         }
3573                         /* We must not get here. Something is going wrong. */
3574                         MLX5_ASSERT(false);
3575                         txq->stats.oerrors++;
3576                         break;
3577                 }
3578                 /* Dedicated branch for single-segment TSO packets. */
3579                 if (MLX5_TXOFF_CONFIG(TSO) &&
3580                     unlikely(loc.mbuf->ol_flags & PKT_TX_TCP_SEG)) {
3581                         /*
3582                          * TSO might require special way for inlining
3583                          * (dedicated parameters) and is sent with
3584                          * MLX5_OPCODE_TSO opcode only, provide this
3585                          * in dedicated branch.
3586                          */
3587 enter_send_tso:
3588                         MLX5_ASSERT(NB_SEGS(loc.mbuf) == 1);
3589                         MLX5_ASSERT(pkts_n > loc.pkts_sent);
3590                         ret = mlx5_tx_burst_tso(txq, pkts, pkts_n, &loc, olx);
3591                         /*
3592                          * These returned code checks are supposed
3593                          * to be optimized out due to routine inlining.
3594                          */
3595                         if (ret == MLX5_TXCMP_CODE_EXIT)
3596                                 break;
3597                         if (ret == MLX5_TXCMP_CODE_ERROR) {
3598                                 txq->stats.oerrors++;
3599                                 break;
3600                         }
3601                         if (ret == MLX5_TXCMP_CODE_SINGLE)
3602                                 goto enter_send_single;
3603                         if (MLX5_TXOFF_CONFIG(MULTI) &&
3604                             ret == MLX5_TXCMP_CODE_MULTI) {
3605                                 /*
3606                                  * The multi-segment packet was
3607                                  * encountered in the array.
3608                                  */
3609                                 goto enter_send_multi;
3610                         }
3611                         /* We must not get here. Something is going wrong. */
3612                         MLX5_ASSERT(false);
3613                         txq->stats.oerrors++;
3614                         break;
3615                 }
3616                 /*
3617                  * The dedicated branch for the single-segment packets
3618                  * without TSO. Often these ones can be sent using
3619                  * MLX5_OPCODE_EMPW with multiple packets in one WQE.
3620                  * The routine builds the WQEs till it encounters
3621                  * the TSO or multi-segment packet (in case if these
3622                  * offloads are requested at SQ configuration time).
3623                  */
3624 enter_send_single:
3625                 MLX5_ASSERT(pkts_n > loc.pkts_sent);
3626                 ret = mlx5_tx_burst_single(txq, pkts, pkts_n, &loc, olx);
3627                 /*
3628                  * These returned code checks are supposed
3629                  * to be optimized out due to routine inlining.
3630                  */
3631                 if (ret == MLX5_TXCMP_CODE_EXIT)
3632                         break;
3633                 if (ret == MLX5_TXCMP_CODE_ERROR) {
3634                         txq->stats.oerrors++;
3635                         break;
3636                 }
3637                 if (MLX5_TXOFF_CONFIG(MULTI) &&
3638                     ret == MLX5_TXCMP_CODE_MULTI) {
3639                         /*
3640                          * The multi-segment packet was
3641                          * encountered in the array.
3642                          */
3643                         goto enter_send_multi;
3644                 }
3645                 if (MLX5_TXOFF_CONFIG(TSO) &&
3646                     ret == MLX5_TXCMP_CODE_TSO) {
3647                         /*
3648                          * The single-segment TSO packet was
3649                          * encountered in the array.
3650                          */
3651                         goto enter_send_tso;
3652                 }
3653                 /* We must not get here. Something is going wrong. */
3654                 MLX5_ASSERT(false);
3655                 txq->stats.oerrors++;
3656                 break;
3657         }
3658         /*
3659          * Main Tx loop is completed, do the rest:
3660          * - set completion request if thresholds are reached
3661          * - doorbell the hardware
3662          * - copy the rest of mbufs to elts (if any)
3663          */
3664         MLX5_ASSERT(MLX5_TXOFF_CONFIG(INLINE) ||
3665                     loc.pkts_sent >= loc.pkts_copy);
3666         /* Take a shortcut if nothing is sent. */
3667         if (unlikely(loc.pkts_sent == loc.pkts_loop))
3668                 goto burst_exit;
3669         /* Request CQE generation if limits are reached. */
3670         mlx5_tx_request_completion(txq, &loc, olx);
3671         /*
3672          * Ring QP doorbell immediately after WQE building completion
3673          * to improve latencies. The pure software related data treatment
3674          * can be completed after doorbell. Tx CQEs for this SQ are
3675          * processed in this thread only by the polling.
3676          *
3677          * The rdma core library can map doorbell register in two ways,
3678          * depending on the environment variable "MLX5_SHUT_UP_BF":
3679          *
3680          * - as regular cached memory, the variable is either missing or
3681          *   set to zero. This type of mapping may cause the significant
3682          *   doorbell register writing latency and requires explicit memory
3683          *   write barrier to mitigate this issue and prevent write combining.
3684          *
3685          * - as non-cached memory, the variable is present and set to not "0"
3686          *   value. This type of mapping may cause performance impact under
3687          *   heavy loading conditions but the explicit write memory barrier is
3688          *   not required and it may improve core performance.
3689          *
3690          * - the legacy behaviour (prior 19.08 release) was to use some
3691          *   heuristics to decide whether write memory barrier should
3692          *   be performed. This behavior is supported with specifying
3693          *   tx_db_nc=2, write barrier is skipped if application provides
3694          *   the full recommended burst of packets, it supposes the next
3695          *   packets are coming and the write barrier will be issued on
3696          *   the next burst (after descriptor writing, at least).
3697          */
3698         mlx5_tx_dbrec_cond_wmb(txq, loc.wqe_last, !txq->db_nc &&
3699                         (!txq->db_heu || pkts_n % MLX5_TX_DEFAULT_BURST));
3700         /* Not all of the mbufs may be stored into elts yet. */
3701         part = MLX5_TXOFF_CONFIG(INLINE) ? 0 : loc.pkts_sent - loc.pkts_copy;
3702         if (!MLX5_TXOFF_CONFIG(INLINE) && part) {
3703                 /*
3704                  * There are some single-segment mbufs not stored in elts.
3705                  * It can be only if the last packet was single-segment.
3706                  * The copying is gathered into one place due to it is
3707                  * a good opportunity to optimize that with SIMD.
3708                  * Unfortunately if inlining is enabled the gaps in pointer
3709                  * array may happen due to early freeing of the inlined mbufs.
3710                  */
3711                 mlx5_tx_copy_elts(txq, pkts + loc.pkts_copy, part, olx);
3712                 loc.pkts_copy = loc.pkts_sent;
3713         }
3714         MLX5_ASSERT(txq->elts_s >= (uint16_t)(txq->elts_head - txq->elts_tail));
3715         MLX5_ASSERT(txq->wqe_s >= (uint16_t)(txq->wqe_ci - txq->wqe_pi));
3716         if (pkts_n > loc.pkts_sent) {
3717                 /*
3718                  * If burst size is large there might be no enough CQE
3719                  * fetched from completion queue and no enough resources
3720                  * freed to send all the packets.
3721                  */
3722                 goto send_loop;
3723         }
3724 burst_exit:
3725 #ifdef MLX5_PMD_SOFT_COUNTERS
3726         /* Increment sent packets counter. */
3727         txq->stats.opackets += loc.pkts_sent;
3728 #endif
3729         if (MLX5_TXOFF_CONFIG(INLINE) && loc.mbuf_free)
3730                 __mlx5_tx_free_mbuf(txq, pkts, loc.mbuf_free, olx);
3731         return loc.pkts_sent;
3732 }
3733
3734 #endif /* RTE_PMD_MLX5_TX_H_ */