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