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