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