net/mlx5: allow implicit LRO flow
[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 #include <rte_malloc.h>
33
34 #include "mlx5_utils.h"
35 #include "mlx5.h"
36 #include "mlx5_mr.h"
37 #include "mlx5_autoconf.h"
38 #include "mlx5_defs.h"
39 #include "mlx5_prm.h"
40 #include "mlx5_glue.h"
41
42 /* Support tunnel matching. */
43 #define MLX5_FLOW_TUNNEL 6
44
45 struct mlx5_rxq_stats {
46 #ifdef MLX5_PMD_SOFT_COUNTERS
47         uint64_t ipackets; /**< Total of successfully received packets. */
48         uint64_t ibytes; /**< Total of successfully received bytes. */
49 #endif
50         uint64_t idropped; /**< Total of packets dropped when RX ring full. */
51         uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
52 };
53
54 struct mlx5_txq_stats {
55 #ifdef MLX5_PMD_SOFT_COUNTERS
56         uint64_t opackets; /**< Total of successfully sent packets. */
57         uint64_t obytes; /**< Total of successfully sent bytes. */
58 #endif
59         uint64_t oerrors; /**< Total number of failed transmitted packets. */
60 };
61
62 struct mlx5_priv;
63
64 /* Compressed CQE context. */
65 struct rxq_zip {
66         uint16_t ai; /* Array index. */
67         uint16_t ca; /* Current array index. */
68         uint16_t na; /* Next array index. */
69         uint16_t cq_ci; /* The next CQE. */
70         uint32_t cqe_cnt; /* Number of CQEs. */
71 };
72
73 /* Multi-Packet RQ buffer header. */
74 struct mlx5_mprq_buf {
75         struct rte_mempool *mp;
76         rte_atomic16_t refcnt; /* Atomically accessed refcnt. */
77         uint8_t pad[RTE_PKTMBUF_HEADROOM]; /* Headroom for the first packet. */
78         struct rte_mbuf_ext_shared_info shinfos[];
79         /*
80          * Shared information per stride.
81          * More memory will be allocated for the first stride head-room and for
82          * the strides data.
83          */
84 } __rte_cache_aligned;
85
86 /* Get pointer to the first stride. */
87 #define mlx5_mprq_buf_addr(ptr, strd_n) (RTE_PTR_ADD((ptr), \
88                                 sizeof(struct mlx5_mprq_buf) + \
89                                 (strd_n) * \
90                                 sizeof(struct rte_mbuf_ext_shared_info) + \
91                                 RTE_PKTMBUF_HEADROOM))
92
93 #define MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES 6
94 #define MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES 9
95
96 enum mlx5_rxq_err_state {
97         MLX5_RXQ_ERR_STATE_NO_ERROR = 0,
98         MLX5_RXQ_ERR_STATE_NEED_RESET,
99         MLX5_RXQ_ERR_STATE_NEED_READY,
100 };
101
102 /* RX queue descriptor. */
103 struct mlx5_rxq_data {
104         unsigned int csum:1; /* Enable checksum offloading. */
105         unsigned int hw_timestamp:1; /* Enable HW timestamp. */
106         unsigned int vlan_strip:1; /* Enable VLAN stripping. */
107         unsigned int crc_present:1; /* CRC must be subtracted. */
108         unsigned int sges_n:3; /* Log 2 of SGEs (max buffers per packet). */
109         unsigned int cqe_n:4; /* Log 2 of CQ elements. */
110         unsigned int elts_n:4; /* Log 2 of Mbufs. */
111         unsigned int rss_hash:1; /* RSS hash result is enabled. */
112         unsigned int mark:1; /* Marked flow available on the queue. */
113         unsigned int strd_num_n:5; /* Log 2 of the number of stride. */
114         unsigned int strd_sz_n:4; /* Log 2 of stride size. */
115         unsigned int strd_shift_en:1; /* Enable 2bytes shift on a stride. */
116         unsigned int err_state:2; /* enum mlx5_rxq_err_state. */
117         unsigned int strd_headroom_en:1; /* Enable mbuf headroom in MPRQ. */
118         unsigned int :2; /* Remaining bits. */
119         volatile uint32_t *rq_db;
120         volatile uint32_t *cq_db;
121         uint16_t port_id;
122         uint32_t rq_ci;
123         uint16_t consumed_strd; /* Number of consumed strides in WQE. */
124         uint32_t rq_pi;
125         uint32_t cq_ci;
126         uint16_t rq_repl_thresh; /* Threshold for buffer replenishment. */
127         union {
128                 struct rxq_zip zip; /* Compressed context. */
129                 uint16_t decompressed;
130                 /* Number of ready mbufs decompressed from the CQ. */
131         };
132         struct mlx5_mr_ctrl mr_ctrl; /* MR control descriptor. */
133         uint16_t mprq_max_memcpy_len; /* Maximum size of packet to memcpy. */
134         volatile void *wqes;
135         volatile struct mlx5_cqe(*cqes)[];
136         RTE_STD_C11
137         union  {
138                 struct rte_mbuf *(*elts)[];
139                 struct mlx5_mprq_buf *(*mprq_bufs)[];
140         };
141         struct rte_mempool *mp;
142         struct rte_mempool *mprq_mp; /* Mempool for Multi-Packet RQ. */
143         struct mlx5_mprq_buf *mprq_repl; /* Stashed mbuf for replenish. */
144         uint16_t idx; /* Queue index. */
145         struct mlx5_rxq_stats stats;
146         uint64_t mbuf_initializer; /* Default rearm_data for vectorized Rx. */
147         struct rte_mbuf fake_mbuf; /* elts padding for vectorized Rx. */
148         void *cq_uar; /* CQ user access region. */
149         uint32_t cqn; /* CQ number. */
150         uint8_t cq_arm_sn; /* CQ arm seq number. */
151 #ifndef RTE_ARCH_64
152         rte_spinlock_t *uar_lock_cq;
153         /* CQ (UAR) access lock required for 32bit implementations */
154 #endif
155         uint32_t tunnel; /* Tunnel information. */
156 } __rte_cache_aligned;
157
158 enum mlx5_rxq_obj_type {
159         MLX5_RXQ_OBJ_TYPE_IBV,          /* mlx5_rxq_obj with ibv_wq. */
160         MLX5_RXQ_OBJ_TYPE_DEVX_RQ,      /* mlx5_rxq_obj with mlx5_devx_rq. */
161 };
162
163 /* Verbs/DevX Rx queue elements. */
164 struct mlx5_rxq_obj {
165         LIST_ENTRY(mlx5_rxq_obj) next; /* Pointer to the next element. */
166         rte_atomic32_t refcnt; /* Reference counter. */
167         struct mlx5_rxq_ctrl *rxq_ctrl; /* Back pointer to parent. */
168         struct ibv_cq *cq; /* Completion Queue. */
169         enum mlx5_rxq_obj_type type;
170         RTE_STD_C11
171         union {
172                 struct ibv_wq *wq; /* Work Queue. */
173                 struct mlx5_devx_obj *rq; /* DevX object for Rx Queue. */
174         };
175         struct ibv_comp_channel *channel;
176 };
177
178 /* RX queue control descriptor. */
179 struct mlx5_rxq_ctrl {
180         struct mlx5_rxq_data rxq; /* Data path structure. */
181         LIST_ENTRY(mlx5_rxq_ctrl) next; /* Pointer to the next element. */
182         rte_atomic32_t refcnt; /* Reference counter. */
183         struct mlx5_rxq_obj *obj; /* Verbs/DevX elements. */
184         struct mlx5_priv *priv; /* Back pointer to private data. */
185         unsigned int socket; /* CPU socket ID for allocations. */
186         unsigned int irq:1; /* Whether IRQ is enabled. */
187         unsigned int dbr_umem_id_valid:1; /* dbr_umem_id holds a valid value. */
188         uint32_t flow_mark_n; /* Number of Mark/Flag flows using this Queue. */
189         uint32_t flow_tunnels_n[MLX5_FLOW_TUNNEL]; /* Tunnels counters. */
190         uint32_t wqn; /* WQ number. */
191         uint16_t dump_file_n; /* Number of dump files. */
192         uint32_t dbr_umem_id; /* Storing door-bell information, */
193         uint64_t dbr_offset;  /* needed when freeing door-bell. */
194         struct mlx5dv_devx_umem *wq_umem; /* WQ buffer registration info. */
195 };
196
197 enum mlx5_ind_tbl_type {
198         MLX5_IND_TBL_TYPE_IBV,
199         MLX5_IND_TBL_TYPE_DEVX,
200 };
201
202 /* Indirection table. */
203 struct mlx5_ind_table_obj {
204         LIST_ENTRY(mlx5_ind_table_obj) next; /* Pointer to the next element. */
205         rte_atomic32_t refcnt; /* Reference counter. */
206         enum mlx5_ind_tbl_type type;
207         RTE_STD_C11
208         union {
209                 struct ibv_rwq_ind_table *ind_table; /**< Indirection table. */
210                 struct mlx5_devx_obj *rqt; /* DevX RQT object. */
211         };
212         uint32_t queues_n; /**< Number of queues in the list. */
213         uint16_t queues[]; /**< Queue list. */
214 };
215
216 /* Hash Rx queue. */
217 struct mlx5_hrxq {
218         LIST_ENTRY(mlx5_hrxq) next; /* Pointer to the next element. */
219         rte_atomic32_t refcnt; /* Reference counter. */
220         struct mlx5_ind_table_obj *ind_table; /* Indirection table. */
221         RTE_STD_C11
222         union {
223                 struct ibv_qp *qp; /* Verbs queue pair. */
224                 struct mlx5_devx_obj *tir; /* DevX TIR object. */
225         };
226 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
227         void *action; /* DV QP action pointer. */
228 #endif
229         uint64_t hash_fields; /* Verbs Hash fields. */
230         uint32_t rss_key_len; /* Hash key length in bytes. */
231         uint8_t rss_key[]; /* Hash key. */
232 };
233
234 /* TX queue send local data. */
235 __extension__
236 struct mlx5_txq_local {
237         struct mlx5_wqe *wqe_last; /* last sent WQE pointer. */
238         struct rte_mbuf *mbuf; /* first mbuf to process. */
239         uint16_t pkts_copy; /* packets copied to elts. */
240         uint16_t pkts_sent; /* packets sent. */
241         uint16_t elts_free; /* available elts remain. */
242         uint16_t wqe_free; /* available wqe remain. */
243         uint16_t mbuf_off; /* data offset in current mbuf. */
244         uint16_t mbuf_nseg; /* number of remaining mbuf. */
245 };
246
247 /* TX queue descriptor. */
248 __extension__
249 struct mlx5_txq_data {
250         uint16_t elts_head; /* Current counter in (*elts)[]. */
251         uint16_t elts_tail; /* Counter of first element awaiting completion. */
252         uint16_t elts_comp; /* elts index since last completion request. */
253         uint16_t elts_s; /* Number of mbuf elements. */
254         uint16_t elts_m; /* Mask for mbuf elements indices. */
255         /* Fields related to elts mbuf storage. */
256         uint16_t wqe_ci; /* Consumer index for work queue. */
257         uint16_t wqe_pi; /* Producer index for work queue. */
258         uint16_t wqe_s; /* Number of WQ elements. */
259         uint16_t wqe_m; /* Mask Number for WQ elements. */
260         uint16_t wqe_comp; /* WQE index since last completion request. */
261         uint16_t wqe_thres; /* WQE threshold to request completion in CQ. */
262         /* WQ related fields. */
263         uint16_t cq_ci; /* Consumer index for completion queue. */
264 #ifndef NDEBUG
265         uint16_t cq_pi; /* Counter of issued CQE "always" requests. */
266 #endif
267         uint16_t cqe_s; /* Number of CQ elements. */
268         uint16_t cqe_m; /* Mask for CQ indices. */
269         /* CQ related fields. */
270         uint16_t elts_n:4; /* elts[] length (in log2). */
271         uint16_t cqe_n:4; /* Number of CQ elements (in log2). */
272         uint16_t wqe_n:4; /* Number of WQ elements (in log2). */
273         uint16_t tso_en:1; /* When set hardware TSO is enabled. */
274         uint16_t tunnel_en:1;
275         /* When set TX offload for tunneled packets are supported. */
276         uint16_t swp_en:1; /* Whether SW parser is enabled. */
277         uint16_t vlan_en:1; /* VLAN insertion in WQE is supported. */
278         uint16_t inlen_send; /* Ordinary send data inline size. */
279         uint16_t inlen_empw; /* eMPW max packet size to inline. */
280         uint16_t inlen_mode; /* Minimal data length to inline. */
281         uint32_t qp_num_8s; /* QP number shifted by 8. */
282         uint64_t offloads; /* Offloads for Tx Queue. */
283         struct mlx5_mr_ctrl mr_ctrl; /* MR control descriptor. */
284         struct mlx5_wqe *wqes; /* Work queue. */
285         struct mlx5_wqe *wqes_end; /* Work queue array limit. */
286         volatile struct mlx5_cqe *cqes; /* Completion queue. */
287         volatile uint32_t *qp_db; /* Work queue doorbell. */
288         volatile uint32_t *cq_db; /* Completion queue doorbell. */
289         uint16_t port_id; /* Port ID of device. */
290         uint16_t idx; /* Queue index. */
291         struct mlx5_txq_stats stats; /* TX queue counters. */
292 #ifndef RTE_ARCH_64
293         rte_spinlock_t *uar_lock;
294         /* UAR access lock required for 32bit implementations */
295 #endif
296         struct rte_mbuf *elts[0];
297         /* Storage for queued packets, must be the last field. */
298 } __rte_cache_aligned;
299
300 /* Verbs Rx queue elements. */
301 struct mlx5_txq_ibv {
302         LIST_ENTRY(mlx5_txq_ibv) next; /* Pointer to the next element. */
303         rte_atomic32_t refcnt; /* Reference counter. */
304         struct mlx5_txq_ctrl *txq_ctrl; /* Pointer to the control queue. */
305         struct ibv_cq *cq; /* Completion Queue. */
306         struct ibv_qp *qp; /* Queue Pair. */
307 };
308
309 /* TX queue control descriptor. */
310 struct mlx5_txq_ctrl {
311         LIST_ENTRY(mlx5_txq_ctrl) next; /* Pointer to the next element. */
312         rte_atomic32_t refcnt; /* Reference counter. */
313         unsigned int socket; /* CPU socket ID for allocations. */
314         unsigned int max_inline_data; /* Max inline data. */
315         unsigned int max_tso_header; /* Max TSO header size. */
316         struct mlx5_txq_ibv *ibv; /* Verbs queue object. */
317         struct mlx5_priv *priv; /* Back pointer to private data. */
318         off_t uar_mmap_offset; /* UAR mmap offset for non-primary process. */
319         void *bf_reg; /* BlueFlame register from Verbs. */
320         uint16_t dump_file_n; /* Number of dump files. */
321         struct mlx5_txq_data txq; /* Data path structure. */
322         /* Must be the last field in the structure, contains elts[]. */
323 };
324
325 #define MLX5_TX_BFREG(txq) \
326                 (MLX5_PROC_PRIV((txq)->port_id)->uar_table[(txq)->idx])
327
328 /* mlx5_rxq.c */
329
330 extern uint8_t rss_hash_default_key[];
331
332 int mlx5_check_mprq_support(struct rte_eth_dev *dev);
333 int mlx5_rxq_mprq_enabled(struct mlx5_rxq_data *rxq);
334 int mlx5_mprq_enabled(struct rte_eth_dev *dev);
335 int mlx5_mprq_free_mp(struct rte_eth_dev *dev);
336 int mlx5_mprq_alloc_mp(struct rte_eth_dev *dev);
337 int mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
338                         unsigned int socket, const struct rte_eth_rxconf *conf,
339                         struct rte_mempool *mp);
340 void mlx5_rx_queue_release(void *dpdk_rxq);
341 int mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev);
342 void mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev);
343 int mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
344 int mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
345 struct mlx5_rxq_obj *mlx5_rxq_obj_new(struct rte_eth_dev *dev, uint16_t idx,
346                                       enum mlx5_rxq_obj_type type);
347 int mlx5_rxq_obj_verify(struct rte_eth_dev *dev);
348 struct mlx5_rxq_ctrl *mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx,
349                                    uint16_t desc, unsigned int socket,
350                                    const struct rte_eth_rxconf *conf,
351                                    struct rte_mempool *mp);
352 struct mlx5_rxq_ctrl *mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx);
353 int mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx);
354 int mlx5_rxq_verify(struct rte_eth_dev *dev);
355 int rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl);
356 int mlx5_ind_table_obj_verify(struct rte_eth_dev *dev);
357 struct mlx5_hrxq *mlx5_hrxq_new(struct rte_eth_dev *dev,
358                                 const uint8_t *rss_key, uint32_t rss_key_len,
359                                 uint64_t hash_fields,
360                                 const uint16_t *queues, uint32_t queues_n,
361                                 int tunnel __rte_unused);
362 struct mlx5_hrxq *mlx5_hrxq_get(struct rte_eth_dev *dev,
363                                 const uint8_t *rss_key, uint32_t rss_key_len,
364                                 uint64_t hash_fields,
365                                 const uint16_t *queues, uint32_t queues_n);
366 int mlx5_hrxq_release(struct rte_eth_dev *dev, struct mlx5_hrxq *hxrq);
367 int mlx5_hrxq_verify(struct rte_eth_dev *dev);
368 struct mlx5_hrxq *mlx5_hrxq_drop_new(struct rte_eth_dev *dev);
369 void mlx5_hrxq_drop_release(struct rte_eth_dev *dev);
370 uint64_t mlx5_get_rx_port_offloads(struct rte_eth_dev *dev);
371 uint64_t mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev);
372 int mlx5_lro_on(struct rte_eth_dev *dev);
373
374 /* mlx5_txq.c */
375
376 int mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
377                         unsigned int socket, const struct rte_eth_txconf *conf);
378 void mlx5_tx_queue_release(void *dpdk_txq);
379 int mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd);
380 struct mlx5_txq_ibv *mlx5_txq_ibv_new(struct rte_eth_dev *dev, uint16_t idx);
381 struct mlx5_txq_ibv *mlx5_txq_ibv_get(struct rte_eth_dev *dev, uint16_t idx);
382 int mlx5_txq_ibv_release(struct mlx5_txq_ibv *txq_ibv);
383 int mlx5_txq_ibv_verify(struct rte_eth_dev *dev);
384 struct mlx5_txq_ctrl *mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx,
385                                    uint16_t desc, unsigned int socket,
386                                    const struct rte_eth_txconf *conf);
387 struct mlx5_txq_ctrl *mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx);
388 int mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx);
389 int mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx);
390 int mlx5_txq_verify(struct rte_eth_dev *dev);
391 void txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl);
392 uint64_t mlx5_get_tx_port_offloads(struct rte_eth_dev *dev);
393
394 /* mlx5_rxtx.c */
395
396 extern uint32_t mlx5_ptype_table[];
397 extern uint8_t mlx5_cksum_table[];
398 extern uint8_t mlx5_swp_types_table[];
399
400 void mlx5_set_ptype_table(void);
401 void mlx5_set_cksum_table(void);
402 void mlx5_set_swp_types_table(void);
403 __rte_noinline uint16_t mlx5_tx_error_cqe_handle
404                                 (struct mlx5_txq_data *restrict txq,
405                                  volatile struct mlx5_err_cqe *err_cqe);
406 uint16_t mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
407 void mlx5_rxq_initialize(struct mlx5_rxq_data *rxq);
408 __rte_noinline int mlx5_rx_err_handle(struct mlx5_rxq_data *rxq,
409                                       uint8_t mbuf_prepare);
410 void mlx5_mprq_buf_free_cb(void *addr, void *opaque);
411 void mlx5_mprq_buf_free(struct mlx5_mprq_buf *buf);
412 uint16_t mlx5_rx_burst_mprq(void *dpdk_rxq, struct rte_mbuf **pkts,
413                             uint16_t pkts_n);
414 uint16_t removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts,
415                           uint16_t pkts_n);
416 uint16_t removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts,
417                           uint16_t pkts_n);
418 int mlx5_rx_descriptor_status(void *rx_queue, uint16_t offset);
419 int mlx5_tx_descriptor_status(void *tx_queue, uint16_t offset);
420 uint32_t mlx5_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id);
421 void mlx5_dump_debug_information(const char *path, const char *title,
422                                  const void *buf, unsigned int len);
423 int mlx5_queue_state_modify_primary(struct rte_eth_dev *dev,
424                         const struct mlx5_mp_arg_queue_state_modify *sm);
425
426 /* Vectorized version of mlx5_rxtx.c */
427 int mlx5_rxq_check_vec_support(struct mlx5_rxq_data *rxq_data);
428 int mlx5_check_vec_rx_support(struct rte_eth_dev *dev);
429 uint16_t mlx5_rx_burst_vec(void *dpdk_txq, struct rte_mbuf **pkts,
430                            uint16_t pkts_n);
431
432 /* mlx5_mr.c */
433
434 void mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl);
435 uint32_t mlx5_rx_addr2mr_bh(struct mlx5_rxq_data *rxq, uintptr_t addr);
436 uint32_t mlx5_tx_mb2mr_bh(struct mlx5_txq_data *txq, struct rte_mbuf *mb);
437 uint32_t mlx5_tx_update_ext_mp(struct mlx5_txq_data *txq, uintptr_t addr,
438                                struct rte_mempool *mp);
439 int mlx5_dma_map(struct rte_pci_device *pdev, void *addr, uint64_t iova,
440                  size_t len);
441 int mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr, uint64_t iova,
442                    size_t len);
443
444 /**
445  * Provide safe 64bit store operation to mlx5 UAR region for both 32bit and
446  * 64bit architectures.
447  *
448  * @param val
449  *   value to write in CPU endian format.
450  * @param addr
451  *   Address to write to.
452  * @param lock
453  *   Address of the lock to use for that UAR access.
454  */
455 static __rte_always_inline void
456 __mlx5_uar_write64_relaxed(uint64_t val, void *addr,
457                            rte_spinlock_t *lock __rte_unused)
458 {
459 #ifdef RTE_ARCH_64
460         *(uint64_t *)addr = val;
461 #else /* !RTE_ARCH_64 */
462         rte_spinlock_lock(lock);
463         *(uint32_t *)addr = val;
464         rte_io_wmb();
465         *((uint32_t *)addr + 1) = val >> 32;
466         rte_spinlock_unlock(lock);
467 #endif
468 }
469
470 /**
471  * Provide safe 64bit store operation to mlx5 UAR region for both 32bit and
472  * 64bit architectures while guaranteeing the order of execution with the
473  * code being executed.
474  *
475  * @param val
476  *   value to write in CPU endian format.
477  * @param addr
478  *   Address to write to.
479  * @param lock
480  *   Address of the lock to use for that UAR access.
481  */
482 static __rte_always_inline void
483 __mlx5_uar_write64(uint64_t val, void *addr, rte_spinlock_t *lock)
484 {
485         rte_io_wmb();
486         __mlx5_uar_write64_relaxed(val, addr, lock);
487 }
488
489 /* Assist macros, used instead of directly calling the functions they wrap. */
490 #ifdef RTE_ARCH_64
491 #define mlx5_uar_write64_relaxed(val, dst, lock) \
492                 __mlx5_uar_write64_relaxed(val, dst, NULL)
493 #define mlx5_uar_write64(val, dst, lock) __mlx5_uar_write64(val, dst, NULL)
494 #else
495 #define mlx5_uar_write64_relaxed(val, dst, lock) \
496                 __mlx5_uar_write64_relaxed(val, dst, lock)
497 #define mlx5_uar_write64(val, dst, lock) __mlx5_uar_write64(val, dst, lock)
498 #endif
499
500 /* CQE status. */
501 enum mlx5_cqe_status {
502         MLX5_CQE_STATUS_SW_OWN,
503         MLX5_CQE_STATUS_HW_OWN,
504         MLX5_CQE_STATUS_ERR,
505 };
506
507 /**
508  * Check whether CQE is valid.
509  *
510  * @param cqe
511  *   Pointer to CQE.
512  * @param cqes_n
513  *   Size of completion queue.
514  * @param ci
515  *   Consumer index.
516  *
517  * @return
518  *   The CQE status.
519  */
520 static __rte_always_inline enum mlx5_cqe_status
521 check_cqe(volatile struct mlx5_cqe *cqe, const uint16_t cqes_n,
522           const uint16_t ci)
523 {
524         const uint16_t idx = ci & cqes_n;
525         const uint8_t op_own = cqe->op_own;
526         const uint8_t op_owner = MLX5_CQE_OWNER(op_own);
527         const uint8_t op_code = MLX5_CQE_OPCODE(op_own);
528
529         if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
530                 return MLX5_CQE_STATUS_HW_OWN;
531         rte_cio_rmb();
532         if (unlikely(op_code == MLX5_CQE_RESP_ERR ||
533                      op_code == MLX5_CQE_REQ_ERR))
534                 return MLX5_CQE_STATUS_ERR;
535         return MLX5_CQE_STATUS_SW_OWN;
536 }
537
538 /**
539  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which the
540  * cloned mbuf is allocated is returned instead.
541  *
542  * @param buf
543  *   Pointer to mbuf.
544  *
545  * @return
546  *   Memory pool where data is located for given mbuf.
547  */
548 static inline struct rte_mempool *
549 mlx5_mb2mp(struct rte_mbuf *buf)
550 {
551         if (unlikely(RTE_MBUF_CLONED(buf)))
552                 return rte_mbuf_from_indirect(buf)->pool;
553         return buf->pool;
554 }
555
556 /**
557  * Query LKey from a packet buffer for Rx. No need to flush local caches for Rx
558  * as mempool is pre-configured and static.
559  *
560  * @param rxq
561  *   Pointer to Rx queue structure.
562  * @param addr
563  *   Address to search.
564  *
565  * @return
566  *   Searched LKey on success, UINT32_MAX on no match.
567  */
568 static __rte_always_inline uint32_t
569 mlx5_rx_addr2mr(struct mlx5_rxq_data *rxq, uintptr_t addr)
570 {
571         struct mlx5_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
572         uint32_t lkey;
573
574         /* Linear search on MR cache array. */
575         lkey = mlx5_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru,
576                                     MLX5_MR_CACHE_N, addr);
577         if (likely(lkey != UINT32_MAX))
578                 return lkey;
579         /* Take slower bottom-half (Binary Search) on miss. */
580         return mlx5_rx_addr2mr_bh(rxq, addr);
581 }
582
583 #define mlx5_rx_mb2mr(rxq, mb) mlx5_rx_addr2mr(rxq, (uintptr_t)((mb)->buf_addr))
584
585 /**
586  * Query LKey from a packet buffer for Tx. If not found, add the mempool.
587  *
588  * @param txq
589  *   Pointer to Tx queue structure.
590  * @param addr
591  *   Address to search.
592  *
593  * @return
594  *   Searched LKey on success, UINT32_MAX on no match.
595  */
596 static __rte_always_inline uint32_t
597 mlx5_tx_mb2mr(struct mlx5_txq_data *txq, struct rte_mbuf *mb)
598 {
599         struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
600         uintptr_t addr = (uintptr_t)mb->buf_addr;
601         uint32_t lkey;
602
603         /* Check generation bit to see if there's any change on existing MRs. */
604         if (unlikely(*mr_ctrl->dev_gen_ptr != mr_ctrl->cur_gen))
605                 mlx5_mr_flush_local_cache(mr_ctrl);
606         /* Linear search on MR cache array. */
607         lkey = mlx5_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru,
608                                     MLX5_MR_CACHE_N, addr);
609         if (likely(lkey != UINT32_MAX))
610                 return lkey;
611         /* Take slower bottom-half on miss. */
612         return mlx5_tx_mb2mr_bh(txq, mb);
613 }
614
615 /**
616  * Ring TX queue doorbell and flush the update if requested.
617  *
618  * @param txq
619  *   Pointer to TX queue structure.
620  * @param wqe
621  *   Pointer to the last WQE posted in the NIC.
622  * @param cond
623  *   Request for write memory barrier after BlueFlame update.
624  */
625 static __rte_always_inline void
626 mlx5_tx_dbrec_cond_wmb(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe,
627                        int cond)
628 {
629         uint64_t *dst = MLX5_TX_BFREG(txq);
630         volatile uint64_t *src = ((volatile uint64_t *)wqe);
631
632         rte_cio_wmb();
633         *txq->qp_db = rte_cpu_to_be_32(txq->wqe_ci);
634         /* Ensure ordering between DB record and BF copy. */
635         rte_wmb();
636         mlx5_uar_write64_relaxed(*src, dst, txq->uar_lock);
637         if (cond)
638                 rte_wmb();
639 }
640
641 /**
642  * Ring TX queue doorbell and flush the update by write memory barrier.
643  *
644  * @param txq
645  *   Pointer to TX queue structure.
646  * @param wqe
647  *   Pointer to the last WQE posted in the NIC.
648  */
649 static __rte_always_inline void
650 mlx5_tx_dbrec(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe)
651 {
652         mlx5_tx_dbrec_cond_wmb(txq, wqe, 1);
653 }
654
655 #endif /* RTE_PMD_MLX5_RXTX_H_ */