net/mlx5: separate Rx function declarations to another file
[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_spinlock.h>
18 #include <rte_io.h>
19 #include <rte_bus_pci.h>
20 #include <rte_malloc.h>
21 #include <rte_cycles.h>
22
23 #include <mlx5_glue.h>
24 #include <mlx5_prm.h>
25 #include <mlx5_common.h>
26 #include <mlx5_common_mr.h>
27
28 #include "mlx5_defs.h"
29 #include "mlx5_utils.h"
30 #include "mlx5.h"
31 #include "mlx5_autoconf.h"
32 #include "mlx5_mr.h"
33
34
35 /* Mbuf dynamic flag offset for inline. */
36 extern uint64_t rte_net_mlx5_dynf_inline_mask;
37
38 struct mlx5_txq_stats {
39 #ifdef MLX5_PMD_SOFT_COUNTERS
40         uint64_t opackets; /**< Total of successfully sent packets. */
41         uint64_t obytes; /**< Total of successfully sent bytes. */
42 #endif
43         uint64_t oerrors; /**< Total number of failed transmitted packets. */
44 };
45
46 struct mlx5_priv;
47
48 /* TX queue send local data. */
49 __extension__
50 struct mlx5_txq_local {
51         struct mlx5_wqe *wqe_last; /* last sent WQE pointer. */
52         struct rte_mbuf *mbuf; /* first mbuf to process. */
53         uint16_t pkts_copy; /* packets copied to elts. */
54         uint16_t pkts_sent; /* packets sent. */
55         uint16_t pkts_loop; /* packets sent on loop entry. */
56         uint16_t elts_free; /* available elts remain. */
57         uint16_t wqe_free; /* available wqe remain. */
58         uint16_t mbuf_off; /* data offset in current mbuf. */
59         uint16_t mbuf_nseg; /* number of remaining mbuf. */
60         uint16_t mbuf_free; /* number of inline mbufs to free. */
61 };
62
63 /* TX queue descriptor. */
64 __extension__
65 struct mlx5_txq_data {
66         uint16_t elts_head; /* Current counter in (*elts)[]. */
67         uint16_t elts_tail; /* Counter of first element awaiting completion. */
68         uint16_t elts_comp; /* elts index since last completion request. */
69         uint16_t elts_s; /* Number of mbuf elements. */
70         uint16_t elts_m; /* Mask for mbuf elements indices. */
71         /* Fields related to elts mbuf storage. */
72         uint16_t wqe_ci; /* Consumer index for work queue. */
73         uint16_t wqe_pi; /* Producer index for work queue. */
74         uint16_t wqe_s; /* Number of WQ elements. */
75         uint16_t wqe_m; /* Mask Number for WQ elements. */
76         uint16_t wqe_comp; /* WQE index since last completion request. */
77         uint16_t wqe_thres; /* WQE threshold to request completion in CQ. */
78         /* WQ related fields. */
79         uint16_t cq_ci; /* Consumer index for completion queue. */
80         uint16_t cq_pi; /* Production index for completion queue. */
81         uint16_t cqe_s; /* Number of CQ elements. */
82         uint16_t cqe_m; /* Mask for CQ indices. */
83         /* CQ related fields. */
84         uint16_t elts_n:4; /* elts[] length (in log2). */
85         uint16_t cqe_n:4; /* Number of CQ elements (in log2). */
86         uint16_t wqe_n:4; /* Number of WQ elements (in log2). */
87         uint16_t tso_en:1; /* When set hardware TSO is enabled. */
88         uint16_t tunnel_en:1;
89         /* When set TX offload for tunneled packets are supported. */
90         uint16_t swp_en:1; /* Whether SW parser is enabled. */
91         uint16_t vlan_en:1; /* VLAN insertion in WQE is supported. */
92         uint16_t db_nc:1; /* Doorbell mapped to non-cached region. */
93         uint16_t db_heu:1; /* Doorbell heuristic write barrier. */
94         uint16_t fast_free:1; /* mbuf fast free on Tx is enabled. */
95         uint16_t inlen_send; /* Ordinary send data inline size. */
96         uint16_t inlen_empw; /* eMPW max packet size to inline. */
97         uint16_t inlen_mode; /* Minimal data length to inline. */
98         uint32_t qp_num_8s; /* QP number shifted by 8. */
99         uint64_t offloads; /* Offloads for Tx Queue. */
100         struct mlx5_mr_ctrl mr_ctrl; /* MR control descriptor. */
101         struct mlx5_wqe *wqes; /* Work queue. */
102         struct mlx5_wqe *wqes_end; /* Work queue array limit. */
103 #ifdef RTE_LIBRTE_MLX5_DEBUG
104         uint32_t *fcqs; /* Free completion queue (debug extended). */
105 #else
106         uint16_t *fcqs; /* Free completion queue. */
107 #endif
108         volatile struct mlx5_cqe *cqes; /* Completion queue. */
109         volatile uint32_t *qp_db; /* Work queue doorbell. */
110         volatile uint32_t *cq_db; /* Completion queue doorbell. */
111         uint16_t port_id; /* Port ID of device. */
112         uint16_t idx; /* Queue index. */
113         uint64_t ts_mask; /* Timestamp flag dynamic mask. */
114         int32_t ts_offset; /* Timestamp field dynamic offset. */
115         struct mlx5_dev_ctx_shared *sh; /* Shared context. */
116         struct mlx5_txq_stats stats; /* TX queue counters. */
117 #ifndef RTE_ARCH_64
118         rte_spinlock_t *uar_lock;
119         /* UAR access lock required for 32bit implementations */
120 #endif
121         struct rte_mbuf *elts[0];
122         /* Storage for queued packets, must be the last field. */
123 } __rte_cache_aligned;
124
125 enum mlx5_txq_type {
126         MLX5_TXQ_TYPE_STANDARD, /* Standard Tx queue. */
127         MLX5_TXQ_TYPE_HAIRPIN, /* Hairpin Rx queue. */
128 };
129
130 /* TX queue control descriptor. */
131 struct mlx5_txq_ctrl {
132         LIST_ENTRY(mlx5_txq_ctrl) next; /* Pointer to the next element. */
133         uint32_t refcnt; /* Reference counter. */
134         unsigned int socket; /* CPU socket ID for allocations. */
135         enum mlx5_txq_type type; /* The txq ctrl type. */
136         unsigned int max_inline_data; /* Max inline data. */
137         unsigned int max_tso_header; /* Max TSO header size. */
138         struct mlx5_txq_obj *obj; /* Verbs/DevX queue object. */
139         struct mlx5_priv *priv; /* Back pointer to private data. */
140         off_t uar_mmap_offset; /* UAR mmap offset for non-primary process. */
141         void *bf_reg; /* BlueFlame register from Verbs. */
142         uint16_t dump_file_n; /* Number of dump files. */
143         struct rte_eth_hairpin_conf hairpin_conf; /* Hairpin configuration. */
144         uint32_t hairpin_status; /* Hairpin binding status. */
145         struct mlx5_txq_data txq; /* Data path structure. */
146         /* Must be the last field in the structure, contains elts[]. */
147 };
148
149 #define MLX5_TX_BFREG(txq) \
150                 (MLX5_PROC_PRIV((txq)->port_id)->uar_table[(txq)->idx])
151
152 /* mlx5_txq.c */
153
154 int mlx5_tx_queue_start(struct rte_eth_dev *dev, uint16_t queue_id);
155 int mlx5_tx_queue_stop(struct rte_eth_dev *dev, uint16_t queue_id);
156 int mlx5_tx_queue_start_primary(struct rte_eth_dev *dev, uint16_t queue_id);
157 int mlx5_tx_queue_stop_primary(struct rte_eth_dev *dev, uint16_t queue_id);
158 int mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
159                         unsigned int socket, const struct rte_eth_txconf *conf);
160 int mlx5_tx_hairpin_queue_setup
161         (struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
162          const struct rte_eth_hairpin_conf *hairpin_conf);
163 void mlx5_tx_queue_release(void *dpdk_txq);
164 void txq_uar_init(struct mlx5_txq_ctrl *txq_ctrl);
165 int mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd);
166 void mlx5_tx_uar_uninit_secondary(struct rte_eth_dev *dev);
167 int mlx5_txq_obj_verify(struct rte_eth_dev *dev);
168 struct mlx5_txq_ctrl *mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx,
169                                    uint16_t desc, unsigned int socket,
170                                    const struct rte_eth_txconf *conf);
171 struct mlx5_txq_ctrl *mlx5_txq_hairpin_new
172         (struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
173          const struct rte_eth_hairpin_conf *hairpin_conf);
174 struct mlx5_txq_ctrl *mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx);
175 int mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx);
176 int mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx);
177 int mlx5_txq_verify(struct rte_eth_dev *dev);
178 void txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl);
179 void txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl);
180 uint64_t mlx5_get_tx_port_offloads(struct rte_eth_dev *dev);
181 void mlx5_txq_dynf_timestamp_set(struct rte_eth_dev *dev);
182
183 /* mlx5_rxtx.c */
184
185 extern uint32_t mlx5_ptype_table[];
186 extern uint8_t mlx5_cksum_table[];
187 extern uint8_t mlx5_swp_types_table[];
188
189 void mlx5_set_ptype_table(void);
190 void mlx5_set_cksum_table(void);
191 void mlx5_set_swp_types_table(void);
192 uint16_t removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts,
193                           uint16_t pkts_n);
194 int mlx5_tx_descriptor_status(void *tx_queue, uint16_t offset);
195 void mlx5_dump_debug_information(const char *path, const char *title,
196                                  const void *buf, unsigned int len);
197 int mlx5_queue_state_modify_primary(struct rte_eth_dev *dev,
198                         const struct mlx5_mp_arg_queue_state_modify *sm);
199 void mlx5_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
200                        struct rte_eth_txq_info *qinfo);
201 int mlx5_tx_burst_mode_get(struct rte_eth_dev *dev, uint16_t tx_queue_id,
202                            struct rte_eth_burst_mode *mode);
203
204 /* mlx5_mr.c */
205
206 void mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl);
207 uint32_t mlx5_tx_mb2mr_bh(struct mlx5_txq_data *txq, struct rte_mbuf *mb);
208 uint32_t mlx5_tx_update_ext_mp(struct mlx5_txq_data *txq, uintptr_t addr,
209                                struct rte_mempool *mp);
210 int mlx5_dma_map(struct rte_pci_device *pdev, void *addr, uint64_t iova,
211                  size_t len);
212 int mlx5_dma_unmap(struct rte_pci_device *pdev, void *addr, uint64_t iova,
213                    size_t len);
214
215 /**
216  * Provide safe 64bit store operation to mlx5 UAR region for both 32bit and
217  * 64bit architectures.
218  *
219  * @param val
220  *   value to write in CPU endian format.
221  * @param addr
222  *   Address to write to.
223  * @param lock
224  *   Address of the lock to use for that UAR access.
225  */
226 static __rte_always_inline void
227 __mlx5_uar_write64_relaxed(uint64_t val, void *addr,
228                            rte_spinlock_t *lock __rte_unused)
229 {
230 #ifdef RTE_ARCH_64
231         *(uint64_t *)addr = val;
232 #else /* !RTE_ARCH_64 */
233         rte_spinlock_lock(lock);
234         *(uint32_t *)addr = val;
235         rte_io_wmb();
236         *((uint32_t *)addr + 1) = val >> 32;
237         rte_spinlock_unlock(lock);
238 #endif
239 }
240
241 /**
242  * Provide safe 64bit store operation to mlx5 UAR region for both 32bit and
243  * 64bit architectures while guaranteeing the order of execution with the
244  * code being executed.
245  *
246  * @param val
247  *   value to write in CPU endian format.
248  * @param addr
249  *   Address to write to.
250  * @param lock
251  *   Address of the lock to use for that UAR access.
252  */
253 static __rte_always_inline void
254 __mlx5_uar_write64(uint64_t val, void *addr, rte_spinlock_t *lock)
255 {
256         rte_io_wmb();
257         __mlx5_uar_write64_relaxed(val, addr, lock);
258 }
259
260 /* Assist macros, used instead of directly calling the functions they wrap. */
261 #ifdef RTE_ARCH_64
262 #define mlx5_uar_write64_relaxed(val, dst, lock) \
263                 __mlx5_uar_write64_relaxed(val, dst, NULL)
264 #define mlx5_uar_write64(val, dst, lock) __mlx5_uar_write64(val, dst, NULL)
265 #else
266 #define mlx5_uar_write64_relaxed(val, dst, lock) \
267                 __mlx5_uar_write64_relaxed(val, dst, lock)
268 #define mlx5_uar_write64(val, dst, lock) __mlx5_uar_write64(val, dst, lock)
269 #endif
270
271 /**
272  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which the
273  * cloned mbuf is allocated is returned instead.
274  *
275  * @param buf
276  *   Pointer to mbuf.
277  *
278  * @return
279  *   Memory pool where data is located for given mbuf.
280  */
281 static inline struct rte_mempool *
282 mlx5_mb2mp(struct rte_mbuf *buf)
283 {
284         if (unlikely(RTE_MBUF_CLONED(buf)))
285                 return rte_mbuf_from_indirect(buf)->pool;
286         return buf->pool;
287 }
288
289 /**
290  * Query LKey from a packet buffer for Tx. If not found, add the mempool.
291  *
292  * @param txq
293  *   Pointer to Tx queue structure.
294  * @param addr
295  *   Address to search.
296  *
297  * @return
298  *   Searched LKey on success, UINT32_MAX on no match.
299  */
300 static __rte_always_inline uint32_t
301 mlx5_tx_mb2mr(struct mlx5_txq_data *txq, struct rte_mbuf *mb)
302 {
303         struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
304         uintptr_t addr = (uintptr_t)mb->buf_addr;
305         uint32_t lkey;
306
307         /* Check generation bit to see if there's any change on existing MRs. */
308         if (unlikely(*mr_ctrl->dev_gen_ptr != mr_ctrl->cur_gen))
309                 mlx5_mr_flush_local_cache(mr_ctrl);
310         /* Linear search on MR cache array. */
311         lkey = mlx5_mr_lookup_lkey(mr_ctrl->cache, &mr_ctrl->mru,
312                                    MLX5_MR_CACHE_N, addr);
313         if (likely(lkey != UINT32_MAX))
314                 return lkey;
315         /* Take slower bottom-half on miss. */
316         return mlx5_tx_mb2mr_bh(txq, mb);
317 }
318
319 /**
320  * Ring TX queue doorbell and flush the update if requested.
321  *
322  * @param txq
323  *   Pointer to TX queue structure.
324  * @param wqe
325  *   Pointer to the last WQE posted in the NIC.
326  * @param cond
327  *   Request for write memory barrier after BlueFlame update.
328  */
329 static __rte_always_inline void
330 mlx5_tx_dbrec_cond_wmb(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe,
331                        int cond)
332 {
333         uint64_t *dst = MLX5_TX_BFREG(txq);
334         volatile uint64_t *src = ((volatile uint64_t *)wqe);
335
336         rte_io_wmb();
337         *txq->qp_db = rte_cpu_to_be_32(txq->wqe_ci);
338         /* Ensure ordering between DB record and BF copy. */
339         rte_wmb();
340         mlx5_uar_write64_relaxed(*src, dst, txq->uar_lock);
341         if (cond)
342                 rte_wmb();
343 }
344
345 /**
346  * Ring TX queue doorbell and flush the update by write memory barrier.
347  *
348  * @param txq
349  *   Pointer to TX queue structure.
350  * @param wqe
351  *   Pointer to the last WQE posted in the NIC.
352  */
353 static __rte_always_inline void
354 mlx5_tx_dbrec(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe)
355 {
356         mlx5_tx_dbrec_cond_wmb(txq, wqe, 1);
357 }
358
359 /**
360  * Convert timestamp from mbuf format to linear counter
361  * of Clock Queue completions (24 bits)
362  *
363  * @param sh
364  *   Pointer to the device shared context to fetch Tx
365  *   packet pacing timestamp and parameters.
366  * @param ts
367  *   Timestamp from mbuf to convert.
368  * @return
369  *   positive or zero value - completion ID to wait
370  *   negative value - conversion error
371  */
372 static __rte_always_inline int32_t
373 mlx5_txpp_convert_tx_ts(struct mlx5_dev_ctx_shared *sh, uint64_t mts)
374 {
375         uint64_t ts, ci;
376         uint32_t tick;
377
378         do {
379                 /*
380                  * Read atomically two uint64_t fields and compare lsb bits.
381                  * It there is no match - the timestamp was updated in
382                  * the service thread, data should be re-read.
383                  */
384                 rte_compiler_barrier();
385                 ci = __atomic_load_n(&sh->txpp.ts.ci_ts, __ATOMIC_RELAXED);
386                 ts = __atomic_load_n(&sh->txpp.ts.ts, __ATOMIC_RELAXED);
387                 rte_compiler_barrier();
388                 if (!((ts ^ ci) << (64 - MLX5_CQ_INDEX_WIDTH)))
389                         break;
390         } while (true);
391         /* Perform the skew correction, positive value to send earlier. */
392         mts -= sh->txpp.skew;
393         mts -= ts;
394         if (unlikely(mts >= UINT64_MAX / 2)) {
395                 /* We have negative integer, mts is in the past. */
396                 __atomic_fetch_add(&sh->txpp.err_ts_past,
397                                    1, __ATOMIC_RELAXED);
398                 return -1;
399         }
400         tick = sh->txpp.tick;
401         MLX5_ASSERT(tick);
402         /* Convert delta to completions, round up. */
403         mts = (mts + tick - 1) / tick;
404         if (unlikely(mts >= (1 << MLX5_CQ_INDEX_WIDTH) / 2 - 1)) {
405                 /* We have mts is too distant future. */
406                 __atomic_fetch_add(&sh->txpp.err_ts_future,
407                                    1, __ATOMIC_RELAXED);
408                 return -1;
409         }
410         mts <<= 64 - MLX5_CQ_INDEX_WIDTH;
411         ci += mts;
412         ci >>= 64 - MLX5_CQ_INDEX_WIDTH;
413         return ci;
414 }
415
416 #endif /* RTE_PMD_MLX5_RXTX_H_ */