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