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