net/mlx5: remove Rx queues indexes correlation
[dpdk.git] / drivers / net / mlx5 / mlx5_rxtx.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5
6 #ifndef RTE_PMD_MLX5_RXTX_H_
7 #define RTE_PMD_MLX5_RXTX_H_
8
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <sys/queue.h>
12
13 /* Verbs header. */
14 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
15 #ifdef PEDANTIC
16 #pragma GCC diagnostic ignored "-Wpedantic"
17 #endif
18 #include <infiniband/verbs.h>
19 #include <infiniband/mlx5dv.h>
20 #ifdef PEDANTIC
21 #pragma GCC diagnostic error "-Wpedantic"
22 #endif
23
24 #include <rte_mbuf.h>
25 #include <rte_mempool.h>
26 #include <rte_common.h>
27 #include <rte_hexdump.h>
28 #include <rte_atomic.h>
29 #include <rte_spinlock.h>
30 #include <rte_io.h>
31 #include <rte_bus_pci.h>
32
33 #include "mlx5_utils.h"
34 #include "mlx5.h"
35 #include "mlx5_mr.h"
36 #include "mlx5_autoconf.h"
37 #include "mlx5_defs.h"
38 #include "mlx5_prm.h"
39
40 /* Support tunnel matching. */
41 #define MLX5_FLOW_TUNNEL 5
42
43 struct mlx5_rxq_stats {
44 #ifdef MLX5_PMD_SOFT_COUNTERS
45         uint64_t ipackets; /**< Total of successfully received packets. */
46         uint64_t ibytes; /**< Total of successfully received bytes. */
47 #endif
48         uint64_t idropped; /**< Total of packets dropped when RX ring full. */
49         uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
50 };
51
52 struct mlx5_txq_stats {
53 #ifdef MLX5_PMD_SOFT_COUNTERS
54         uint64_t opackets; /**< Total of successfully sent packets. */
55         uint64_t obytes; /**< Total of successfully sent bytes. */
56 #endif
57         uint64_t oerrors; /**< Total number of failed transmitted packets. */
58 };
59
60 struct mlx5_priv;
61
62 /* Compressed CQE context. */
63 struct rxq_zip {
64         uint16_t ai; /* Array index. */
65         uint16_t ca; /* Current array index. */
66         uint16_t na; /* Next array index. */
67         uint16_t cq_ci; /* The next CQE. */
68         uint32_t cqe_cnt; /* Number of CQEs. */
69 };
70
71 /* Multi-Packet RQ buffer header. */
72 struct mlx5_mprq_buf {
73         struct rte_mempool *mp;
74         rte_atomic16_t refcnt; /* Atomically accessed refcnt. */
75         uint8_t pad[RTE_PKTMBUF_HEADROOM]; /* Headroom for the first packet. */
76 } __rte_cache_aligned;
77
78 /* Get pointer to the first stride. */
79 #define mlx5_mprq_buf_addr(ptr) ((ptr) + 1)
80
81 /* RX queue descriptor. */
82 struct mlx5_rxq_data {
83         unsigned int csum:1; /* Enable checksum offloading. */
84         unsigned int hw_timestamp:1; /* Enable HW timestamp. */
85         unsigned int vlan_strip:1; /* Enable VLAN stripping. */
86         unsigned int crc_present:1; /* CRC must be subtracted. */
87         unsigned int sges_n:2; /* Log 2 of SGEs (max buffers per packet). */
88         unsigned int cqe_n:4; /* Log 2 of CQ elements. */
89         unsigned int elts_n:4; /* Log 2 of Mbufs. */
90         unsigned int rss_hash:1; /* RSS hash result is enabled. */
91         unsigned int mark:1; /* Marked flow available on the queue. */
92         unsigned int strd_num_n:5; /* Log 2 of the number of stride. */
93         unsigned int strd_sz_n:4; /* Log 2 of stride size. */
94         unsigned int strd_shift_en:1; /* Enable 2bytes shift on a stride. */
95         unsigned int :6; /* Remaining bits. */
96         volatile uint32_t *rq_db;
97         volatile uint32_t *cq_db;
98         uint16_t port_id;
99         uint32_t rq_ci;
100         uint16_t consumed_strd; /* Number of consumed strides in WQE. */
101         uint32_t rq_pi;
102         uint32_t cq_ci;
103         uint16_t rq_repl_thresh; /* Threshold for buffer replenishment. */
104         union {
105                 struct rxq_zip zip; /* Compressed context. */
106                 uint16_t decompressed;
107                 /* Number of ready mbufs decompressed from the CQ. */
108         };
109         struct mlx5_mr_ctrl mr_ctrl; /* MR control descriptor. */
110         uint16_t mprq_max_memcpy_len; /* Maximum size of packet to memcpy. */
111         volatile void *wqes;
112         volatile struct mlx5_cqe(*cqes)[];
113         RTE_STD_C11
114         union  {
115                 struct rte_mbuf *(*elts)[];
116                 struct mlx5_mprq_buf *(*mprq_bufs)[];
117         };
118         struct rte_mempool *mp;
119         struct rte_mempool *mprq_mp; /* Mempool for Multi-Packet RQ. */
120         struct mlx5_mprq_buf *mprq_repl; /* Stashed mbuf for replenish. */
121         uint16_t idx; /* Queue index. */
122         struct mlx5_rxq_stats stats;
123         uint64_t mbuf_initializer; /* Default rearm_data for vectorized Rx. */
124         struct rte_mbuf fake_mbuf; /* elts padding for vectorized Rx. */
125         void *cq_uar; /* CQ user access region. */
126         uint32_t cqn; /* CQ number. */
127         uint8_t cq_arm_sn; /* CQ arm seq number. */
128 #ifndef RTE_ARCH_64
129         rte_spinlock_t *uar_lock_cq;
130         /* CQ (UAR) access lock required for 32bit implementations */
131 #endif
132         uint32_t tunnel; /* Tunnel information. */
133 } __rte_cache_aligned;
134
135 /* Verbs Rx queue elements. */
136 struct mlx5_rxq_ibv {
137         LIST_ENTRY(mlx5_rxq_ibv) next; /* Pointer to the next element. */
138         rte_atomic32_t refcnt; /* Reference counter. */
139         struct mlx5_rxq_ctrl *rxq_ctrl; /* Back pointer to parent. */
140         struct ibv_cq *cq; /* Completion Queue. */
141         struct ibv_wq *wq; /* Work Queue. */
142         struct ibv_comp_channel *channel;
143 };
144
145 /* RX queue control descriptor. */
146 struct mlx5_rxq_ctrl {
147         struct mlx5_rxq_data rxq; /* Data path structure. */
148         LIST_ENTRY(mlx5_rxq_ctrl) next; /* Pointer to the next element. */
149         rte_atomic32_t refcnt; /* Reference counter. */
150         struct mlx5_rxq_ibv *ibv; /* Verbs elements. */
151         struct mlx5_priv *priv; /* Back pointer to private data. */
152         unsigned int socket; /* CPU socket ID for allocations. */
153         unsigned int irq:1; /* Whether IRQ is enabled. */
154         uint32_t flow_mark_n; /* Number of Mark/Flag flows using this Queue. */
155         uint32_t flow_tunnels_n[MLX5_FLOW_TUNNEL]; /* Tunnels counters. */
156 };
157
158 /* Indirection table. */
159 struct mlx5_ind_table_ibv {
160         LIST_ENTRY(mlx5_ind_table_ibv) next; /* Pointer to the next element. */
161         rte_atomic32_t refcnt; /* Reference counter. */
162         struct ibv_rwq_ind_table *ind_table; /**< Indirection table. */
163         uint32_t queues_n; /**< Number of queues in the list. */
164         uint16_t queues[]; /**< Queue list. */
165 };
166
167 /* Hash Rx queue. */
168 struct mlx5_hrxq {
169         LIST_ENTRY(mlx5_hrxq) next; /* Pointer to the next element. */
170         rte_atomic32_t refcnt; /* Reference counter. */
171         struct mlx5_ind_table_ibv *ind_table; /* Indirection table. */
172         struct ibv_qp *qp; /* Verbs queue pair. */
173 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
174         void *action; /* DV QP action pointer. */
175 #endif
176         uint64_t hash_fields; /* Verbs Hash fields. */
177         uint32_t rss_key_len; /* Hash key length in bytes. */
178         uint8_t rss_key[]; /* Hash key. */
179 };
180
181 /* TX queue descriptor. */
182 __extension__
183 struct mlx5_txq_data {
184         uint16_t elts_head; /* Current counter in (*elts)[]. */
185         uint16_t elts_tail; /* Counter of first element awaiting completion. */
186         uint16_t elts_comp; /* Counter since last completion request. */
187         uint16_t mpw_comp; /* WQ index since last completion request. */
188         uint16_t cq_ci; /* Consumer index for completion queue. */
189 #ifndef NDEBUG
190         uint16_t cq_pi; /* Producer index for completion queue. */
191 #endif
192         uint16_t wqe_ci; /* Consumer index for work queue. */
193         uint16_t wqe_pi; /* Producer index for work queue. */
194         uint16_t elts_n:4; /* (*elts)[] length (in log2). */
195         uint16_t cqe_n:4; /* Number of CQ elements (in log2). */
196         uint16_t wqe_n:4; /* Number of of WQ elements (in log2). */
197         uint16_t tso_en:1; /* When set hardware TSO is enabled. */
198         uint16_t tunnel_en:1;
199         /* When set TX offload for tunneled packets are supported. */
200         uint16_t swp_en:1; /* Whether SW parser is enabled. */
201         uint16_t mpw_hdr_dseg:1; /* Enable DSEGs in the title WQEBB. */
202         uint16_t max_inline; /* Multiple of RTE_CACHE_LINE_SIZE to inline. */
203         uint16_t inline_max_packet_sz; /* Max packet size for inlining. */
204         uint32_t qp_num_8s; /* QP number shifted by 8. */
205         uint64_t offloads; /* Offloads for Tx Queue. */
206         struct mlx5_mr_ctrl mr_ctrl; /* MR control descriptor. */
207         volatile struct mlx5_cqe (*cqes)[]; /* Completion queue. */
208         volatile void *wqes; /* Work queue (use volatile to write into). */
209         volatile uint32_t *qp_db; /* Work queue doorbell. */
210         volatile uint32_t *cq_db; /* Completion queue doorbell. */
211         struct rte_mbuf *(*elts)[]; /* TX elements. */
212         uint16_t port_id; /* Port ID of device. */
213         uint16_t idx; /* Queue index. */
214         struct mlx5_txq_stats stats; /* TX queue counters. */
215 #ifndef RTE_ARCH_64
216         rte_spinlock_t *uar_lock;
217         /* UAR access lock required for 32bit implementations */
218 #endif
219 } __rte_cache_aligned;
220
221 /* Verbs Rx queue elements. */
222 struct mlx5_txq_ibv {
223         LIST_ENTRY(mlx5_txq_ibv) next; /* Pointer to the next element. */
224         rte_atomic32_t refcnt; /* Reference counter. */
225         struct mlx5_txq_ctrl *txq_ctrl; /* Pointer to the control queue. */
226         struct ibv_cq *cq; /* Completion Queue. */
227         struct ibv_qp *qp; /* Queue Pair. */
228 };
229
230 /* TX queue control descriptor. */
231 struct mlx5_txq_ctrl {
232         struct mlx5_txq_data txq; /* Data path structure. */
233         LIST_ENTRY(mlx5_txq_ctrl) next; /* Pointer to the next element. */
234         rte_atomic32_t refcnt; /* Reference counter. */
235         unsigned int socket; /* CPU socket ID for allocations. */
236         unsigned int max_inline_data; /* Max inline data. */
237         unsigned int max_tso_header; /* Max TSO header size. */
238         struct mlx5_txq_ibv *ibv; /* Verbs queue object. */
239         struct mlx5_priv *priv; /* Back pointer to private data. */
240         off_t uar_mmap_offset; /* UAR mmap offset for non-primary process. */
241         void *bf_reg; /* BlueFlame register from Verbs. */
242 };
243
244 #define MLX5_TX_BFREG(txq) \
245                 (MLX5_PROC_PRIV((txq)->port_id)->uar_table[(txq)->idx])
246
247 /* mlx5_rxq.c */
248
249 extern uint8_t rss_hash_default_key[];
250
251 int mlx5_check_mprq_support(struct rte_eth_dev *dev);
252 int mlx5_rxq_mprq_enabled(struct mlx5_rxq_data *rxq);
253 int mlx5_mprq_enabled(struct rte_eth_dev *dev);
254 int mlx5_mprq_free_mp(struct rte_eth_dev *dev);
255 int mlx5_mprq_alloc_mp(struct rte_eth_dev *dev);
256 int mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
257                         unsigned int socket, const struct rte_eth_rxconf *conf,
258                         struct rte_mempool *mp);
259 void mlx5_rx_queue_release(void *dpdk_rxq);
260 int mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev);
261 void mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev);
262 int mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
263 int mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
264 struct mlx5_rxq_ibv *mlx5_rxq_ibv_new(struct rte_eth_dev *dev, uint16_t idx);
265 int mlx5_rxq_ibv_verify(struct rte_eth_dev *dev);
266 struct mlx5_rxq_ctrl *mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx,
267                                    uint16_t desc, unsigned int socket,
268                                    const struct rte_eth_rxconf *conf,
269                                    struct rte_mempool *mp);
270 struct mlx5_rxq_ctrl *mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx);
271 int mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx);
272 int mlx5_rxq_verify(struct rte_eth_dev *dev);
273 int rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl);
274 int mlx5_ind_table_ibv_verify(struct rte_eth_dev *dev);
275 struct mlx5_hrxq *mlx5_hrxq_new(struct rte_eth_dev *dev,
276                                 const uint8_t *rss_key, uint32_t rss_key_len,
277                                 uint64_t hash_fields,
278                                 const uint16_t *queues, uint32_t queues_n,
279                                 int tunnel __rte_unused);
280 struct mlx5_hrxq *mlx5_hrxq_get(struct rte_eth_dev *dev,
281                                 const uint8_t *rss_key, uint32_t rss_key_len,
282                                 uint64_t hash_fields,
283                                 const uint16_t *queues, uint32_t queues_n);
284 int mlx5_hrxq_release(struct rte_eth_dev *dev, struct mlx5_hrxq *hxrq);
285 int mlx5_hrxq_ibv_verify(struct rte_eth_dev *dev);
286 struct mlx5_hrxq *mlx5_hrxq_drop_new(struct rte_eth_dev *dev);
287 void mlx5_hrxq_drop_release(struct rte_eth_dev *dev);
288 uint64_t mlx5_get_rx_port_offloads(void);
289 uint64_t mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev);
290
291 /* mlx5_txq.c */
292
293 int mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
294                         unsigned int socket, const struct rte_eth_txconf *conf);
295 void mlx5_tx_queue_release(void *dpdk_txq);
296 int mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd);
297 struct mlx5_txq_ibv *mlx5_txq_ibv_new(struct rte_eth_dev *dev, uint16_t idx);
298 struct mlx5_txq_ibv *mlx5_txq_ibv_get(struct rte_eth_dev *dev, uint16_t idx);
299 int mlx5_txq_ibv_release(struct mlx5_txq_ibv *txq_ibv);
300 int mlx5_txq_ibv_verify(struct rte_eth_dev *dev);
301 struct mlx5_txq_ctrl *mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx,
302                                    uint16_t desc, unsigned int socket,
303                                    const struct rte_eth_txconf *conf);
304 struct mlx5_txq_ctrl *mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx);
305 int mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx);
306 int mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx);
307 int mlx5_txq_verify(struct rte_eth_dev *dev);
308 void txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl);
309 uint64_t mlx5_get_tx_port_offloads(struct rte_eth_dev *dev);
310
311 /* mlx5_rxtx.c */
312
313 extern uint32_t mlx5_ptype_table[];
314 extern uint8_t mlx5_cksum_table[];
315 extern uint8_t mlx5_swp_types_table[];
316
317 void mlx5_set_ptype_table(void);
318 void mlx5_set_cksum_table(void);
319 void mlx5_set_swp_types_table(void);
320 uint16_t mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts,
321                        uint16_t pkts_n);
322 uint16_t mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts,
323                            uint16_t pkts_n);
324 uint16_t mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
325                                   uint16_t pkts_n);
326 uint16_t mlx5_tx_burst_empw(void *dpdk_txq, struct rte_mbuf **pkts,
327                             uint16_t pkts_n);
328 uint16_t mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
329 void mlx5_mprq_buf_free_cb(void *addr, void *opaque);
330 void mlx5_mprq_buf_free(struct mlx5_mprq_buf *buf);
331 uint16_t mlx5_rx_burst_mprq(void *dpdk_rxq, struct rte_mbuf **pkts,
332                             uint16_t pkts_n);
333 uint16_t removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts,
334                           uint16_t pkts_n);
335 uint16_t removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts,
336                           uint16_t pkts_n);
337 int mlx5_rx_descriptor_status(void *rx_queue, uint16_t offset);
338 int mlx5_tx_descriptor_status(void *tx_queue, uint16_t offset);
339 uint32_t mlx5_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id);
340
341 /* Vectorized version of mlx5_rxtx.c */
342 int mlx5_check_raw_vec_tx_support(struct rte_eth_dev *dev);
343 int mlx5_check_vec_tx_support(struct rte_eth_dev *dev);
344 int mlx5_rxq_check_vec_support(struct mlx5_rxq_data *rxq_data);
345 int mlx5_check_vec_rx_support(struct rte_eth_dev *dev);
346 uint16_t mlx5_tx_burst_raw_vec(void *dpdk_txq, struct rte_mbuf **pkts,
347                                uint16_t pkts_n);
348 uint16_t mlx5_tx_burst_vec(void *dpdk_txq, struct rte_mbuf **pkts,
349                            uint16_t pkts_n);
350 uint16_t mlx5_rx_burst_vec(void *dpdk_txq, struct rte_mbuf **pkts,
351                            uint16_t pkts_n);
352
353 /* mlx5_mr.c */
354
355 void mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl);
356 uint32_t mlx5_rx_addr2mr_bh(struct mlx5_rxq_data *rxq, uintptr_t addr);
357 uint32_t mlx5_tx_mb2mr_bh(struct mlx5_txq_data *txq, struct rte_mbuf *mb);
358 uint32_t mlx5_tx_update_ext_mp(struct mlx5_txq_data *txq, uintptr_t addr,
359                                struct rte_mempool *mp);
360 int mlx5_dma_map(struct rte_pci_device *pdev, void *addr, uint64_t iova,
361                  size_t len);
362 int mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr, uint64_t iova,
363                    size_t len);
364
365 /**
366  * Provide safe 64bit store operation to mlx5 UAR region for both 32bit and
367  * 64bit architectures.
368  *
369  * @param val
370  *   value to write in CPU endian format.
371  * @param addr
372  *   Address to write to.
373  * @param lock
374  *   Address of the lock to use for that UAR access.
375  */
376 static __rte_always_inline void
377 __mlx5_uar_write64_relaxed(uint64_t val, void *addr,
378                            rte_spinlock_t *lock __rte_unused)
379 {
380 #ifdef RTE_ARCH_64
381         *(uint64_t *)addr = val;
382 #else /* !RTE_ARCH_64 */
383         rte_spinlock_lock(lock);
384         *(uint32_t *)addr = val;
385         rte_io_wmb();
386         *((uint32_t *)addr + 1) = val >> 32;
387         rte_spinlock_unlock(lock);
388 #endif
389 }
390
391 /**
392  * Provide safe 64bit store operation to mlx5 UAR region for both 32bit and
393  * 64bit architectures while guaranteeing the order of execution with the
394  * code being executed.
395  *
396  * @param val
397  *   value to write in CPU endian format.
398  * @param addr
399  *   Address to write to.
400  * @param lock
401  *   Address of the lock to use for that UAR access.
402  */
403 static __rte_always_inline void
404 __mlx5_uar_write64(uint64_t val, void *addr, rte_spinlock_t *lock)
405 {
406         rte_io_wmb();
407         __mlx5_uar_write64_relaxed(val, addr, lock);
408 }
409
410 /* Assist macros, used instead of directly calling the functions they wrap. */
411 #ifdef RTE_ARCH_64
412 #define mlx5_uar_write64_relaxed(val, dst, lock) \
413                 __mlx5_uar_write64_relaxed(val, dst, NULL)
414 #define mlx5_uar_write64(val, dst, lock) __mlx5_uar_write64(val, dst, NULL)
415 #else
416 #define mlx5_uar_write64_relaxed(val, dst, lock) \
417                 __mlx5_uar_write64_relaxed(val, dst, lock)
418 #define mlx5_uar_write64(val, dst, lock) __mlx5_uar_write64(val, dst, lock)
419 #endif
420
421 #ifndef NDEBUG
422 /**
423  * Verify or set magic value in CQE.
424  *
425  * @param cqe
426  *   Pointer to CQE.
427  *
428  * @return
429  *   0 the first time.
430  */
431 static inline int
432 check_cqe_seen(volatile struct mlx5_cqe *cqe)
433 {
434         static const uint8_t magic[] = "seen";
435         volatile uint8_t (*buf)[sizeof(cqe->rsvd1)] = &cqe->rsvd1;
436         int ret = 1;
437         unsigned int i;
438
439         for (i = 0; i < sizeof(magic) && i < sizeof(*buf); ++i)
440                 if (!ret || (*buf)[i] != magic[i]) {
441                         ret = 0;
442                         (*buf)[i] = magic[i];
443                 }
444         return ret;
445 }
446 #endif /* NDEBUG */
447
448 /**
449  * Check whether CQE is valid.
450  *
451  * @param cqe
452  *   Pointer to CQE.
453  * @param cqes_n
454  *   Size of completion queue.
455  * @param ci
456  *   Consumer index.
457  *
458  * @return
459  *   0 on success, 1 on failure.
460  */
461 static __rte_always_inline int
462 check_cqe(volatile struct mlx5_cqe *cqe,
463           unsigned int cqes_n, const uint16_t ci)
464 {
465         uint16_t idx = ci & cqes_n;
466         uint8_t op_own = cqe->op_own;
467         uint8_t op_owner = MLX5_CQE_OWNER(op_own);
468         uint8_t op_code = MLX5_CQE_OPCODE(op_own);
469
470         if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
471                 return 1; /* No CQE. */
472 #ifndef NDEBUG
473         if ((op_code == MLX5_CQE_RESP_ERR) ||
474             (op_code == MLX5_CQE_REQ_ERR)) {
475                 volatile struct mlx5_err_cqe *err_cqe = (volatile void *)cqe;
476                 uint8_t syndrome = err_cqe->syndrome;
477
478                 if ((syndrome == MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR) ||
479                     (syndrome == MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR))
480                         return 0;
481                 if (!check_cqe_seen(cqe)) {
482                         DRV_LOG(ERR,
483                                 "unexpected CQE error %u (0x%02x) syndrome"
484                                 " 0x%02x",
485                                 op_code, op_code, syndrome);
486                         rte_hexdump(stderr, "MLX5 Error CQE:",
487                                     (const void *)((uintptr_t)err_cqe),
488                                     sizeof(*cqe));
489                 }
490                 return 1;
491         } else if ((op_code != MLX5_CQE_RESP_SEND) &&
492                    (op_code != MLX5_CQE_REQ)) {
493                 if (!check_cqe_seen(cqe)) {
494                         DRV_LOG(ERR, "unexpected CQE opcode %u (0x%02x)",
495                                 op_code, op_code);
496                         rte_hexdump(stderr, "MLX5 CQE:",
497                                     (const void *)((uintptr_t)cqe),
498                                     sizeof(*cqe));
499                 }
500                 return 1;
501         }
502 #endif /* NDEBUG */
503         return 0;
504 }
505
506 /**
507  * Return the address of the WQE.
508  *
509  * @param txq
510  *   Pointer to TX queue structure.
511  * @param  wqe_ci
512  *   WQE consumer index.
513  *
514  * @return
515  *   WQE address.
516  */
517 static inline uintptr_t *
518 tx_mlx5_wqe(struct mlx5_txq_data *txq, uint16_t ci)
519 {
520         ci &= ((1 << txq->wqe_n) - 1);
521         return (uintptr_t *)((uintptr_t)txq->wqes + ci * MLX5_WQE_SIZE);
522 }
523
524 /**
525  * Manage TX completions.
526  *
527  * When sending a burst, mlx5_tx_burst() posts several WRs.
528  *
529  * @param txq
530  *   Pointer to TX queue structure.
531  */
532 static __rte_always_inline void
533 mlx5_tx_complete(struct mlx5_txq_data *txq)
534 {
535         const uint16_t elts_n = 1 << txq->elts_n;
536         const uint16_t elts_m = elts_n - 1;
537         const unsigned int cqe_n = 1 << txq->cqe_n;
538         const unsigned int cqe_cnt = cqe_n - 1;
539         uint16_t elts_free = txq->elts_tail;
540         uint16_t elts_tail;
541         uint16_t cq_ci = txq->cq_ci;
542         volatile struct mlx5_cqe *cqe = NULL;
543         volatile struct mlx5_wqe_ctrl *ctrl;
544         struct rte_mbuf *m, *free[elts_n];
545         struct rte_mempool *pool = NULL;
546         unsigned int blk_n = 0;
547
548         cqe = &(*txq->cqes)[cq_ci & cqe_cnt];
549         if (unlikely(check_cqe(cqe, cqe_n, cq_ci)))
550                 return;
551 #ifndef NDEBUG
552         if ((MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_RESP_ERR) ||
553             (MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_REQ_ERR)) {
554                 if (!check_cqe_seen(cqe)) {
555                         DRV_LOG(ERR, "unexpected error CQE, Tx stopped");
556                         rte_hexdump(stderr, "MLX5 TXQ:",
557                                     (const void *)((uintptr_t)txq->wqes),
558                                     ((1 << txq->wqe_n) *
559                                      MLX5_WQE_SIZE));
560                 }
561                 return;
562         }
563 #endif /* NDEBUG */
564         ++cq_ci;
565         rte_cio_rmb();
566         txq->wqe_pi = rte_be_to_cpu_16(cqe->wqe_counter);
567         ctrl = (volatile struct mlx5_wqe_ctrl *)
568                 tx_mlx5_wqe(txq, txq->wqe_pi);
569         elts_tail = ctrl->ctrl3;
570         assert((elts_tail & elts_m) < (1 << txq->wqe_n));
571         /* Free buffers. */
572         while (elts_free != elts_tail) {
573                 m = rte_pktmbuf_prefree_seg((*txq->elts)[elts_free++ & elts_m]);
574                 if (likely(m != NULL)) {
575                         if (likely(m->pool == pool)) {
576                                 free[blk_n++] = m;
577                         } else {
578                                 if (likely(pool != NULL))
579                                         rte_mempool_put_bulk(pool,
580                                                              (void *)free,
581                                                              blk_n);
582                                 free[0] = m;
583                                 pool = m->pool;
584                                 blk_n = 1;
585                         }
586                 }
587         }
588         if (blk_n)
589                 rte_mempool_put_bulk(pool, (void *)free, blk_n);
590 #ifndef NDEBUG
591         elts_free = txq->elts_tail;
592         /* Poisoning. */
593         while (elts_free != elts_tail) {
594                 memset(&(*txq->elts)[elts_free & elts_m],
595                        0x66,
596                        sizeof((*txq->elts)[elts_free & elts_m]));
597                 ++elts_free;
598         }
599 #endif
600         txq->cq_ci = cq_ci;
601         txq->elts_tail = elts_tail;
602         /* Update the consumer index. */
603         rte_compiler_barrier();
604         *txq->cq_db = rte_cpu_to_be_32(cq_ci);
605 }
606
607 /**
608  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which the
609  * cloned mbuf is allocated is returned instead.
610  *
611  * @param buf
612  *   Pointer to mbuf.
613  *
614  * @return
615  *   Memory pool where data is located for given mbuf.
616  */
617 static inline struct rte_mempool *
618 mlx5_mb2mp(struct rte_mbuf *buf)
619 {
620         if (unlikely(RTE_MBUF_CLONED(buf)))
621                 return rte_mbuf_from_indirect(buf)->pool;
622         return buf->pool;
623 }
624
625 /**
626  * Query LKey from a packet buffer for Rx. No need to flush local caches for Rx
627  * as mempool is pre-configured and static.
628  *
629  * @param rxq
630  *   Pointer to Rx queue structure.
631  * @param addr
632  *   Address to search.
633  *
634  * @return
635  *   Searched LKey on success, UINT32_MAX on no match.
636  */
637 static __rte_always_inline uint32_t
638 mlx5_rx_addr2mr(struct mlx5_rxq_data *rxq, uintptr_t addr)
639 {
640         struct mlx5_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
641         uint32_t lkey;
642
643         /* Linear search on MR cache array. */
644         lkey = mlx5_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru,
645                                     MLX5_MR_CACHE_N, addr);
646         if (likely(lkey != UINT32_MAX))
647                 return lkey;
648         /* Take slower bottom-half (Binary Search) on miss. */
649         return mlx5_rx_addr2mr_bh(rxq, addr);
650 }
651
652 #define mlx5_rx_mb2mr(rxq, mb) mlx5_rx_addr2mr(rxq, (uintptr_t)((mb)->buf_addr))
653
654 /**
655  * Query LKey from a packet buffer for Tx. If not found, add the mempool.
656  *
657  * @param txq
658  *   Pointer to Tx queue structure.
659  * @param addr
660  *   Address to search.
661  *
662  * @return
663  *   Searched LKey on success, UINT32_MAX on no match.
664  */
665 static __rte_always_inline uint32_t
666 mlx5_tx_mb2mr(struct mlx5_txq_data *txq, struct rte_mbuf *mb)
667 {
668         struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
669         uintptr_t addr = (uintptr_t)mb->buf_addr;
670         uint32_t lkey;
671
672         /* Check generation bit to see if there's any change on existing MRs. */
673         if (unlikely(*mr_ctrl->dev_gen_ptr != mr_ctrl->cur_gen))
674                 mlx5_mr_flush_local_cache(mr_ctrl);
675         /* Linear search on MR cache array. */
676         lkey = mlx5_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru,
677                                     MLX5_MR_CACHE_N, addr);
678         if (likely(lkey != UINT32_MAX))
679                 return lkey;
680         /* Take slower bottom-half on miss. */
681         return mlx5_tx_mb2mr_bh(txq, mb);
682 }
683
684 /**
685  * Ring TX queue doorbell and flush the update if requested.
686  *
687  * @param txq
688  *   Pointer to TX queue structure.
689  * @param wqe
690  *   Pointer to the last WQE posted in the NIC.
691  * @param cond
692  *   Request for write memory barrier after BlueFlame update.
693  */
694 static __rte_always_inline void
695 mlx5_tx_dbrec_cond_wmb(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe,
696                        int cond)
697 {
698         uint64_t *dst = MLX5_TX_BFREG(txq);
699         volatile uint64_t *src = ((volatile uint64_t *)wqe);
700
701         rte_cio_wmb();
702         *txq->qp_db = rte_cpu_to_be_32(txq->wqe_ci);
703         /* Ensure ordering between DB record and BF copy. */
704         rte_wmb();
705         mlx5_uar_write64_relaxed(*src, dst, txq->uar_lock);
706         if (cond)
707                 rte_wmb();
708 }
709
710 /**
711  * Ring TX queue doorbell and flush the update by write memory barrier.
712  *
713  * @param txq
714  *   Pointer to TX queue structure.
715  * @param wqe
716  *   Pointer to the last WQE posted in the NIC.
717  */
718 static __rte_always_inline void
719 mlx5_tx_dbrec(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe)
720 {
721         mlx5_tx_dbrec_cond_wmb(txq, wqe, 1);
722 }
723
724 /**
725  * Convert mbuf to Verb SWP.
726  *
727  * @param txq_data
728  *   Pointer to the Tx queue.
729  * @param buf
730  *   Pointer to the mbuf.
731  * @param offsets
732  *   Pointer to the SWP header offsets.
733  * @param swp_types
734  *   Pointer to the SWP header types.
735  */
736 static __rte_always_inline void
737 txq_mbuf_to_swp(struct mlx5_txq_data *txq, struct rte_mbuf *buf,
738                 uint8_t *offsets, uint8_t *swp_types)
739 {
740         const uint64_t vlan = buf->ol_flags & PKT_TX_VLAN_PKT;
741         const uint64_t tunnel = buf->ol_flags & PKT_TX_TUNNEL_MASK;
742         const uint64_t tso = buf->ol_flags & PKT_TX_TCP_SEG;
743         const uint64_t csum_flags = buf->ol_flags & PKT_TX_L4_MASK;
744         const uint64_t inner_ip =
745                 buf->ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6);
746         const uint64_t ol_flags_mask = PKT_TX_L4_MASK | PKT_TX_IPV6 |
747                                        PKT_TX_OUTER_IPV6;
748         uint16_t idx;
749         uint16_t off;
750
751         if (likely(!txq->swp_en || (tunnel != PKT_TX_TUNNEL_UDP &&
752                                     tunnel != PKT_TX_TUNNEL_IP)))
753                 return;
754         /*
755          * The index should have:
756          * bit[0:1] = PKT_TX_L4_MASK
757          * bit[4] = PKT_TX_IPV6
758          * bit[8] = PKT_TX_OUTER_IPV6
759          * bit[9] = PKT_TX_OUTER_UDP
760          */
761         idx = (buf->ol_flags & ol_flags_mask) >> 52;
762         if (tunnel == PKT_TX_TUNNEL_UDP)
763                 idx |= 1 << 9;
764         *swp_types = mlx5_swp_types_table[idx];
765         /*
766          * Set offsets for SW parser. Since ConnectX-5, SW parser just
767          * complements HW parser. SW parser starts to engage only if HW parser
768          * can't reach a header. For the older devices, HW parser will not kick
769          * in if any of SWP offsets is set. Therefore, all of the L3 offsets
770          * should be set regardless of HW offload.
771          */
772         off = buf->outer_l2_len + (vlan ? sizeof(struct rte_vlan_hdr) : 0);
773         offsets[1] = off >> 1; /* Outer L3 offset. */
774         off += buf->outer_l3_len;
775         if (tunnel == PKT_TX_TUNNEL_UDP)
776                 offsets[0] = off >> 1; /* Outer L4 offset. */
777         if (inner_ip) {
778                 off += buf->l2_len;
779                 offsets[3] = off >> 1; /* Inner L3 offset. */
780                 if (csum_flags == PKT_TX_TCP_CKSUM || tso ||
781                     csum_flags == PKT_TX_UDP_CKSUM) {
782                         off += buf->l3_len;
783                         offsets[2] = off >> 1; /* Inner L4 offset. */
784                 }
785         }
786 }
787
788 /**
789  * Convert the Checksum offloads to Verbs.
790  *
791  * @param buf
792  *   Pointer to the mbuf.
793  *
794  * @return
795  *   Converted checksum flags.
796  */
797 static __rte_always_inline uint8_t
798 txq_ol_cksum_to_cs(struct rte_mbuf *buf)
799 {
800         uint32_t idx;
801         uint8_t is_tunnel = !!(buf->ol_flags & PKT_TX_TUNNEL_MASK);
802         const uint64_t ol_flags_mask = PKT_TX_TCP_SEG | PKT_TX_L4_MASK |
803                                        PKT_TX_IP_CKSUM | PKT_TX_OUTER_IP_CKSUM;
804
805         /*
806          * The index should have:
807          * bit[0] = PKT_TX_TCP_SEG
808          * bit[2:3] = PKT_TX_UDP_CKSUM, PKT_TX_TCP_CKSUM
809          * bit[4] = PKT_TX_IP_CKSUM
810          * bit[8] = PKT_TX_OUTER_IP_CKSUM
811          * bit[9] = tunnel
812          */
813         idx = ((buf->ol_flags & ol_flags_mask) >> 50) | (!!is_tunnel << 9);
814         return mlx5_cksum_table[idx];
815 }
816
817 /**
818  * Count the number of contiguous single segment packets.
819  *
820  * @param pkts
821  *   Pointer to array of packets.
822  * @param pkts_n
823  *   Number of packets.
824  *
825  * @return
826  *   Number of contiguous single segment packets.
827  */
828 static __rte_always_inline unsigned int
829 txq_count_contig_single_seg(struct rte_mbuf **pkts, uint16_t pkts_n)
830 {
831         unsigned int pos;
832
833         if (!pkts_n)
834                 return 0;
835         /* Count the number of contiguous single segment packets. */
836         for (pos = 0; pos < pkts_n; ++pos)
837                 if (NB_SEGS(pkts[pos]) > 1)
838                         break;
839         return pos;
840 }
841
842 /**
843  * Count the number of contiguous multi-segment packets.
844  *
845  * @param pkts
846  *   Pointer to array of packets.
847  * @param pkts_n
848  *   Number of packets.
849  *
850  * @return
851  *   Number of contiguous multi-segment packets.
852  */
853 static __rte_always_inline unsigned int
854 txq_count_contig_multi_seg(struct rte_mbuf **pkts, uint16_t pkts_n)
855 {
856         unsigned int pos;
857
858         if (!pkts_n)
859                 return 0;
860         /* Count the number of contiguous multi-segment packets. */
861         for (pos = 0; pos < pkts_n; ++pos)
862                 if (NB_SEGS(pkts[pos]) == 1)
863                         break;
864         return pos;
865 }
866
867 #endif /* RTE_PMD_MLX5_RXTX_H_ */