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