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