net/mlx5: remove useless arguments in hrxq 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 /* 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
30 #include "mlx5_utils.h"
31 #include "mlx5.h"
32 #include "mlx5_mr.h"
33 #include "mlx5_autoconf.h"
34 #include "mlx5_defs.h"
35 #include "mlx5_prm.h"
36
37 struct mlx5_rxq_stats {
38         unsigned int idx; /**< Mapping index. */
39 #ifdef MLX5_PMD_SOFT_COUNTERS
40         uint64_t ipackets; /**< Total of successfully received packets. */
41         uint64_t ibytes; /**< Total of successfully received bytes. */
42 #endif
43         uint64_t idropped; /**< Total of packets dropped when RX ring full. */
44         uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
45 };
46
47 struct mlx5_txq_stats {
48         unsigned int idx; /**< Mapping index. */
49 #ifdef MLX5_PMD_SOFT_COUNTERS
50         uint64_t opackets; /**< Total of successfully sent packets. */
51         uint64_t obytes; /**< Total of successfully sent bytes. */
52 #endif
53         uint64_t oerrors; /**< Total number of failed transmitted packets. */
54 };
55
56 struct priv;
57
58 /* Compressed CQE context. */
59 struct rxq_zip {
60         uint16_t ai; /* Array index. */
61         uint16_t ca; /* Current array index. */
62         uint16_t na; /* Next array index. */
63         uint16_t cq_ci; /* The next CQE. */
64         uint32_t cqe_cnt; /* Number of CQEs. */
65 };
66
67 /* Multi-Packet RQ buffer header. */
68 struct mlx5_mprq_buf {
69         struct rte_mempool *mp;
70         rte_atomic16_t refcnt; /* Atomically accessed refcnt. */
71         uint8_t pad[RTE_PKTMBUF_HEADROOM]; /* Headroom for the first packet. */
72 } __rte_cache_aligned;
73
74 /* Get pointer to the first stride. */
75 #define mlx5_mprq_buf_addr(ptr) ((ptr) + 1)
76
77 /* RX queue descriptor. */
78 struct mlx5_rxq_data {
79         unsigned int csum:1; /* Enable checksum offloading. */
80         unsigned int hw_timestamp:1; /* Enable HW timestamp. */
81         unsigned int vlan_strip:1; /* Enable VLAN stripping. */
82         unsigned int crc_present:1; /* CRC must be subtracted. */
83         unsigned int sges_n:2; /* Log 2 of SGEs (max buffers per packet). */
84         unsigned int cqe_n:4; /* Log 2 of CQ elements. */
85         unsigned int elts_n:4; /* Log 2 of Mbufs. */
86         unsigned int rss_hash:1; /* RSS hash result is enabled. */
87         unsigned int mark:1; /* Marked flow available on the queue. */
88         unsigned int strd_num_n:5; /* Log 2 of the number of stride. */
89         unsigned int strd_sz_n:4; /* Log 2 of stride size. */
90         unsigned int strd_shift_en:1; /* Enable 2bytes shift on a stride. */
91         unsigned int :6; /* Remaining bits. */
92         volatile uint32_t *rq_db;
93         volatile uint32_t *cq_db;
94         uint16_t port_id;
95         uint16_t rq_ci;
96         uint16_t consumed_strd; /* Number of consumed strides in WQE. */
97         uint16_t rq_pi;
98         uint16_t cq_ci;
99         struct mlx5_mr_ctrl mr_ctrl; /* MR control descriptor. */
100         uint16_t mprq_max_memcpy_len; /* Maximum size of packet to memcpy. */
101         volatile void *wqes;
102         volatile struct mlx5_cqe(*cqes)[];
103         struct rxq_zip zip; /* Compressed context. */
104         RTE_STD_C11
105         union  {
106                 struct rte_mbuf *(*elts)[];
107                 struct mlx5_mprq_buf *(*mprq_bufs)[];
108         };
109         struct rte_mempool *mp;
110         struct rte_mempool *mprq_mp; /* Mempool for Multi-Packet RQ. */
111         struct mlx5_mprq_buf *mprq_repl; /* Stashed mbuf for replenish. */
112         struct mlx5_rxq_stats stats;
113         uint64_t mbuf_initializer; /* Default rearm_data for vectorized Rx. */
114         struct rte_mbuf fake_mbuf; /* elts padding for vectorized Rx. */
115         void *cq_uar; /* CQ user access region. */
116         uint32_t cqn; /* CQ number. */
117         uint8_t cq_arm_sn; /* CQ arm seq number. */
118         uint32_t tunnel; /* Tunnel information. */
119 } __rte_cache_aligned;
120
121 /* Verbs Rx queue elements. */
122 struct mlx5_rxq_ibv {
123         LIST_ENTRY(mlx5_rxq_ibv) next; /* Pointer to the next element. */
124         rte_atomic32_t refcnt; /* Reference counter. */
125         struct mlx5_rxq_ctrl *rxq_ctrl; /* Back pointer to parent. */
126         struct ibv_cq *cq; /* Completion Queue. */
127         struct ibv_wq *wq; /* Work Queue. */
128         struct ibv_comp_channel *channel;
129 };
130
131 /* RX queue control descriptor. */
132 struct mlx5_rxq_ctrl {
133         LIST_ENTRY(mlx5_rxq_ctrl) next; /* Pointer to the next element. */
134         rte_atomic32_t refcnt; /* Reference counter. */
135         struct mlx5_rxq_ibv *ibv; /* Verbs elements. */
136         struct priv *priv; /* Back pointer to private data. */
137         struct mlx5_rxq_data rxq; /* Data path structure. */
138         unsigned int socket; /* CPU socket ID for allocations. */
139         unsigned int irq:1; /* Whether IRQ is enabled. */
140         uint16_t idx; /* Queue index. */
141         uint32_t flow_mark_n; /* Number of Mark/Flag flows using this Queue. */
142 };
143
144 /* Indirection table. */
145 struct mlx5_ind_table_ibv {
146         LIST_ENTRY(mlx5_ind_table_ibv) next; /* Pointer to the next element. */
147         rte_atomic32_t refcnt; /* Reference counter. */
148         struct ibv_rwq_ind_table *ind_table; /**< Indirection table. */
149         uint32_t queues_n; /**< Number of queues in the list. */
150         uint16_t queues[]; /**< Queue list. */
151 };
152
153 /* Hash Rx queue. */
154 struct mlx5_hrxq {
155         LIST_ENTRY(mlx5_hrxq) next; /* Pointer to the next element. */
156         rte_atomic32_t refcnt; /* Reference counter. */
157         struct mlx5_ind_table_ibv *ind_table; /* Indirection table. */
158         struct ibv_qp *qp; /* Verbs queue pair. */
159         uint64_t hash_fields; /* Verbs Hash fields. */
160         uint32_t rss_key_len; /* Hash key length in bytes. */
161         uint8_t rss_key[]; /* Hash key. */
162 };
163
164 /* TX queue descriptor. */
165 __extension__
166 struct mlx5_txq_data {
167         uint16_t elts_head; /* Current counter in (*elts)[]. */
168         uint16_t elts_tail; /* Counter of first element awaiting completion. */
169         uint16_t elts_comp; /* Counter since last completion request. */
170         uint16_t mpw_comp; /* WQ index since last completion request. */
171         uint16_t cq_ci; /* Consumer index for completion queue. */
172 #ifndef NDEBUG
173         uint16_t cq_pi; /* Producer index for completion queue. */
174 #endif
175         uint16_t wqe_ci; /* Consumer index for work queue. */
176         uint16_t wqe_pi; /* Producer index for work queue. */
177         uint16_t elts_n:4; /* (*elts)[] length (in log2). */
178         uint16_t cqe_n:4; /* Number of CQ elements (in log2). */
179         uint16_t wqe_n:4; /* Number of of WQ elements (in log2). */
180         uint16_t tso_en:1; /* When set hardware TSO is enabled. */
181         uint16_t tunnel_en:1;
182         /* When set TX offload for tunneled packets are supported. */
183         uint16_t swp_en:1; /* Whether SW parser is enabled. */
184         uint16_t mpw_hdr_dseg:1; /* Enable DSEGs in the title WQEBB. */
185         uint16_t max_inline; /* Multiple of RTE_CACHE_LINE_SIZE to inline. */
186         uint16_t inline_max_packet_sz; /* Max packet size for inlining. */
187         uint32_t qp_num_8s; /* QP number shifted by 8. */
188         uint64_t offloads; /* Offloads for Tx Queue. */
189         struct mlx5_mr_ctrl mr_ctrl; /* MR control descriptor. */
190         volatile struct mlx5_cqe (*cqes)[]; /* Completion queue. */
191         volatile void *wqes; /* Work queue (use volatile to write into). */
192         volatile uint32_t *qp_db; /* Work queue doorbell. */
193         volatile uint32_t *cq_db; /* Completion queue doorbell. */
194         volatile void *bf_reg; /* Blueflame register remapped. */
195         struct rte_mbuf *(*elts)[]; /* TX elements. */
196         struct mlx5_txq_stats stats; /* TX queue counters. */
197 } __rte_cache_aligned;
198
199 /* Verbs Rx queue elements. */
200 struct mlx5_txq_ibv {
201         LIST_ENTRY(mlx5_txq_ibv) next; /* Pointer to the next element. */
202         rte_atomic32_t refcnt; /* Reference counter. */
203         struct mlx5_txq_ctrl *txq_ctrl; /* Pointer to the control queue. */
204         struct ibv_cq *cq; /* Completion Queue. */
205         struct ibv_qp *qp; /* Queue Pair. */
206 };
207
208 /* TX queue control descriptor. */
209 struct mlx5_txq_ctrl {
210         LIST_ENTRY(mlx5_txq_ctrl) next; /* Pointer to the next element. */
211         rte_atomic32_t refcnt; /* Reference counter. */
212         unsigned int socket; /* CPU socket ID for allocations. */
213         unsigned int max_inline_data; /* Max inline data. */
214         unsigned int max_tso_header; /* Max TSO header size. */
215         struct mlx5_txq_ibv *ibv; /* Verbs queue object. */
216         struct priv *priv; /* Back pointer to private data. */
217         struct mlx5_txq_data txq; /* Data path structure. */
218         off_t uar_mmap_offset; /* UAR mmap offset for non-primary process. */
219         volatile void *bf_reg_orig; /* Blueflame register from verbs. */
220         uint16_t idx; /* Queue index. */
221 };
222
223 /* mlx5_rxq.c */
224
225 extern uint8_t rss_hash_default_key[];
226
227 int mlx5_check_mprq_support(struct rte_eth_dev *dev);
228 int mlx5_rxq_mprq_enabled(struct mlx5_rxq_data *rxq);
229 int mlx5_mprq_enabled(struct rte_eth_dev *dev);
230 int mlx5_mprq_free_mp(struct rte_eth_dev *dev);
231 int mlx5_mprq_alloc_mp(struct rte_eth_dev *dev);
232 void mlx5_rxq_cleanup(struct mlx5_rxq_ctrl *rxq_ctrl);
233 int mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
234                         unsigned int socket, const struct rte_eth_rxconf *conf,
235                         struct rte_mempool *mp);
236 void mlx5_rx_queue_release(void *dpdk_rxq);
237 int mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev);
238 void mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev);
239 int mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
240 int mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
241 struct mlx5_rxq_ibv *mlx5_rxq_ibv_new(struct rte_eth_dev *dev, uint16_t idx);
242 struct mlx5_rxq_ibv *mlx5_rxq_ibv_get(struct rte_eth_dev *dev, uint16_t idx);
243 int mlx5_rxq_ibv_release(struct mlx5_rxq_ibv *rxq_ibv);
244 int mlx5_rxq_ibv_releasable(struct mlx5_rxq_ibv *rxq_ibv);
245 struct mlx5_rxq_ibv *mlx5_rxq_ibv_drop_new(struct rte_eth_dev *dev);
246 void mlx5_rxq_ibv_drop_release(struct rte_eth_dev *dev);
247 int mlx5_rxq_ibv_verify(struct rte_eth_dev *dev);
248 struct mlx5_rxq_ctrl *mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx,
249                                    uint16_t desc, unsigned int socket,
250                                    const struct rte_eth_rxconf *conf,
251                                    struct rte_mempool *mp);
252 struct mlx5_rxq_ctrl *mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx);
253 int mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx);
254 int mlx5_rxq_releasable(struct rte_eth_dev *dev, uint16_t idx);
255 int mlx5_rxq_verify(struct rte_eth_dev *dev);
256 int rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl);
257 int rxq_alloc_mprq_buf(struct mlx5_rxq_ctrl *rxq_ctrl);
258 struct mlx5_ind_table_ibv *mlx5_ind_table_ibv_new(struct rte_eth_dev *dev,
259                                                   const uint16_t *queues,
260                                                   uint32_t queues_n);
261 struct mlx5_ind_table_ibv *mlx5_ind_table_ibv_get(struct rte_eth_dev *dev,
262                                                   const uint16_t *queues,
263                                                   uint32_t queues_n);
264 int mlx5_ind_table_ibv_release(struct rte_eth_dev *dev,
265                                struct mlx5_ind_table_ibv *ind_tbl);
266 int mlx5_ind_table_ibv_verify(struct rte_eth_dev *dev);
267 struct mlx5_ind_table_ibv *mlx5_ind_table_ibv_drop_new(struct rte_eth_dev *dev);
268 void mlx5_ind_table_ibv_drop_release(struct rte_eth_dev *dev);
269 struct mlx5_hrxq *mlx5_hrxq_new(struct rte_eth_dev *dev,
270                                 const uint8_t *rss_key, uint32_t rss_key_len,
271                                 uint64_t hash_fields,
272                                 const uint16_t *queues, uint32_t queues_n);
273 struct mlx5_hrxq *mlx5_hrxq_get(struct rte_eth_dev *dev,
274                                 const uint8_t *rss_key, uint32_t rss_key_len,
275                                 uint64_t hash_fields,
276                                 const uint16_t *queues, uint32_t queues_n);
277 int mlx5_hrxq_release(struct rte_eth_dev *dev, struct mlx5_hrxq *hxrq);
278 int mlx5_hrxq_ibv_verify(struct rte_eth_dev *dev);
279 struct mlx5_hrxq *mlx5_hrxq_drop_new(struct rte_eth_dev *dev);
280 void mlx5_hrxq_drop_release(struct rte_eth_dev *dev);
281 uint64_t mlx5_get_rx_port_offloads(void);
282 uint64_t mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev);
283
284 /* mlx5_txq.c */
285
286 int mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
287                         unsigned int socket, const struct rte_eth_txconf *conf);
288 void mlx5_tx_queue_release(void *dpdk_txq);
289 int mlx5_tx_uar_remap(struct rte_eth_dev *dev, int fd);
290 struct mlx5_txq_ibv *mlx5_txq_ibv_new(struct rte_eth_dev *dev, uint16_t idx);
291 struct mlx5_txq_ibv *mlx5_txq_ibv_get(struct rte_eth_dev *dev, uint16_t idx);
292 int mlx5_txq_ibv_release(struct mlx5_txq_ibv *txq_ibv);
293 int mlx5_txq_ibv_releasable(struct mlx5_txq_ibv *txq_ibv);
294 int mlx5_txq_ibv_verify(struct rte_eth_dev *dev);
295 struct mlx5_txq_ctrl *mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx,
296                                    uint16_t desc, unsigned int socket,
297                                    const struct rte_eth_txconf *conf);
298 struct mlx5_txq_ctrl *mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx);
299 int mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx);
300 int mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx);
301 int mlx5_txq_verify(struct rte_eth_dev *dev);
302 void txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl);
303 uint64_t mlx5_get_tx_port_offloads(struct rte_eth_dev *dev);
304
305 /* mlx5_rxtx.c */
306
307 extern uint32_t mlx5_ptype_table[];
308 extern uint8_t mlx5_cksum_table[];
309 extern uint8_t mlx5_swp_types_table[];
310
311 void mlx5_set_ptype_table(void);
312 void mlx5_set_cksum_table(void);
313 void mlx5_set_swp_types_table(void);
314 uint16_t mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts,
315                        uint16_t pkts_n);
316 uint16_t mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts,
317                            uint16_t pkts_n);
318 uint16_t mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
319                                   uint16_t pkts_n);
320 uint16_t mlx5_tx_burst_empw(void *dpdk_txq, struct rte_mbuf **pkts,
321                             uint16_t pkts_n);
322 uint16_t mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
323 void mlx5_mprq_buf_free_cb(void *addr, void *opaque);
324 void mlx5_mprq_buf_free(struct mlx5_mprq_buf *buf);
325 uint16_t mlx5_rx_burst_mprq(void *dpdk_rxq, struct rte_mbuf **pkts,
326                             uint16_t pkts_n);
327 uint16_t removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts,
328                           uint16_t pkts_n);
329 uint16_t removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts,
330                           uint16_t pkts_n);
331 int mlx5_rx_descriptor_status(void *rx_queue, uint16_t offset);
332 int mlx5_tx_descriptor_status(void *tx_queue, uint16_t offset);
333
334 /* Vectorized version of mlx5_rxtx.c */
335 int mlx5_check_raw_vec_tx_support(struct rte_eth_dev *dev);
336 int mlx5_check_vec_tx_support(struct rte_eth_dev *dev);
337 int mlx5_rxq_check_vec_support(struct mlx5_rxq_data *rxq_data);
338 int mlx5_check_vec_rx_support(struct rte_eth_dev *dev);
339 uint16_t mlx5_tx_burst_raw_vec(void *dpdk_txq, struct rte_mbuf **pkts,
340                                uint16_t pkts_n);
341 uint16_t mlx5_tx_burst_vec(void *dpdk_txq, struct rte_mbuf **pkts,
342                            uint16_t pkts_n);
343 uint16_t mlx5_rx_burst_vec(void *dpdk_txq, struct rte_mbuf **pkts,
344                            uint16_t pkts_n);
345
346 /* mlx5_mr.c */
347
348 void mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl);
349 uint32_t mlx5_rx_addr2mr_bh(struct mlx5_rxq_data *rxq, uintptr_t addr);
350 uint32_t mlx5_tx_addr2mr_bh(struct mlx5_txq_data *txq, uintptr_t addr);
351
352 #ifndef NDEBUG
353 /**
354  * Verify or set magic value in CQE.
355  *
356  * @param cqe
357  *   Pointer to CQE.
358  *
359  * @return
360  *   0 the first time.
361  */
362 static inline int
363 check_cqe_seen(volatile struct mlx5_cqe *cqe)
364 {
365         static const uint8_t magic[] = "seen";
366         volatile uint8_t (*buf)[sizeof(cqe->rsvd1)] = &cqe->rsvd1;
367         int ret = 1;
368         unsigned int i;
369
370         for (i = 0; i < sizeof(magic) && i < sizeof(*buf); ++i)
371                 if (!ret || (*buf)[i] != magic[i]) {
372                         ret = 0;
373                         (*buf)[i] = magic[i];
374                 }
375         return ret;
376 }
377 #endif /* NDEBUG */
378
379 /**
380  * Check whether CQE is valid.
381  *
382  * @param cqe
383  *   Pointer to CQE.
384  * @param cqes_n
385  *   Size of completion queue.
386  * @param ci
387  *   Consumer index.
388  *
389  * @return
390  *   0 on success, 1 on failure.
391  */
392 static __rte_always_inline int
393 check_cqe(volatile struct mlx5_cqe *cqe,
394           unsigned int cqes_n, const uint16_t ci)
395 {
396         uint16_t idx = ci & cqes_n;
397         uint8_t op_own = cqe->op_own;
398         uint8_t op_owner = MLX5_CQE_OWNER(op_own);
399         uint8_t op_code = MLX5_CQE_OPCODE(op_own);
400
401         if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
402                 return 1; /* No CQE. */
403 #ifndef NDEBUG
404         if ((op_code == MLX5_CQE_RESP_ERR) ||
405             (op_code == MLX5_CQE_REQ_ERR)) {
406                 volatile struct mlx5_err_cqe *err_cqe = (volatile void *)cqe;
407                 uint8_t syndrome = err_cqe->syndrome;
408
409                 if ((syndrome == MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR) ||
410                     (syndrome == MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR))
411                         return 0;
412                 if (!check_cqe_seen(cqe)) {
413                         DRV_LOG(ERR,
414                                 "unexpected CQE error %u (0x%02x) syndrome"
415                                 " 0x%02x",
416                                 op_code, op_code, syndrome);
417                         rte_hexdump(stderr, "MLX5 Error CQE:",
418                                     (const void *)((uintptr_t)err_cqe),
419                                     sizeof(*err_cqe));
420                 }
421                 return 1;
422         } else if ((op_code != MLX5_CQE_RESP_SEND) &&
423                    (op_code != MLX5_CQE_REQ)) {
424                 if (!check_cqe_seen(cqe)) {
425                         DRV_LOG(ERR, "unexpected CQE opcode %u (0x%02x)",
426                                 op_code, op_code);
427                         rte_hexdump(stderr, "MLX5 CQE:",
428                                     (const void *)((uintptr_t)cqe),
429                                     sizeof(*cqe));
430                 }
431                 return 1;
432         }
433 #endif /* NDEBUG */
434         return 0;
435 }
436
437 /**
438  * Return the address of the WQE.
439  *
440  * @param txq
441  *   Pointer to TX queue structure.
442  * @param  wqe_ci
443  *   WQE consumer index.
444  *
445  * @return
446  *   WQE address.
447  */
448 static inline uintptr_t *
449 tx_mlx5_wqe(struct mlx5_txq_data *txq, uint16_t ci)
450 {
451         ci &= ((1 << txq->wqe_n) - 1);
452         return (uintptr_t *)((uintptr_t)txq->wqes + ci * MLX5_WQE_SIZE);
453 }
454
455 /**
456  * Manage TX completions.
457  *
458  * When sending a burst, mlx5_tx_burst() posts several WRs.
459  *
460  * @param txq
461  *   Pointer to TX queue structure.
462  */
463 static __rte_always_inline void
464 mlx5_tx_complete(struct mlx5_txq_data *txq)
465 {
466         const uint16_t elts_n = 1 << txq->elts_n;
467         const uint16_t elts_m = elts_n - 1;
468         const unsigned int cqe_n = 1 << txq->cqe_n;
469         const unsigned int cqe_cnt = cqe_n - 1;
470         uint16_t elts_free = txq->elts_tail;
471         uint16_t elts_tail;
472         uint16_t cq_ci = txq->cq_ci;
473         volatile struct mlx5_cqe *cqe = NULL;
474         volatile struct mlx5_wqe_ctrl *ctrl;
475         struct rte_mbuf *m, *free[elts_n];
476         struct rte_mempool *pool = NULL;
477         unsigned int blk_n = 0;
478
479         cqe = &(*txq->cqes)[cq_ci & cqe_cnt];
480         if (unlikely(check_cqe(cqe, cqe_n, cq_ci)))
481                 return;
482 #ifndef NDEBUG
483         if ((MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_RESP_ERR) ||
484             (MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_REQ_ERR)) {
485                 if (!check_cqe_seen(cqe)) {
486                         DRV_LOG(ERR, "unexpected error CQE, Tx stopped");
487                         rte_hexdump(stderr, "MLX5 TXQ:",
488                                     (const void *)((uintptr_t)txq->wqes),
489                                     ((1 << txq->wqe_n) *
490                                      MLX5_WQE_SIZE));
491                 }
492                 return;
493         }
494 #endif /* NDEBUG */
495         ++cq_ci;
496         txq->wqe_pi = rte_be_to_cpu_16(cqe->wqe_counter);
497         ctrl = (volatile struct mlx5_wqe_ctrl *)
498                 tx_mlx5_wqe(txq, txq->wqe_pi);
499         elts_tail = ctrl->ctrl3;
500         assert((elts_tail & elts_m) < (1 << txq->wqe_n));
501         /* Free buffers. */
502         while (elts_free != elts_tail) {
503                 m = rte_pktmbuf_prefree_seg((*txq->elts)[elts_free++ & elts_m]);
504                 if (likely(m != NULL)) {
505                         if (likely(m->pool == pool)) {
506                                 free[blk_n++] = m;
507                         } else {
508                                 if (likely(pool != NULL))
509                                         rte_mempool_put_bulk(pool,
510                                                              (void *)free,
511                                                              blk_n);
512                                 free[0] = m;
513                                 pool = m->pool;
514                                 blk_n = 1;
515                         }
516                 }
517         }
518         if (blk_n)
519                 rte_mempool_put_bulk(pool, (void *)free, blk_n);
520 #ifndef NDEBUG
521         elts_free = txq->elts_tail;
522         /* Poisoning. */
523         while (elts_free != elts_tail) {
524                 memset(&(*txq->elts)[elts_free & elts_m],
525                        0x66,
526                        sizeof((*txq->elts)[elts_free & elts_m]));
527                 ++elts_free;
528         }
529 #endif
530         txq->cq_ci = cq_ci;
531         txq->elts_tail = elts_tail;
532         /* Update the consumer index. */
533         rte_compiler_barrier();
534         *txq->cq_db = rte_cpu_to_be_32(cq_ci);
535 }
536
537 /**
538  * Query LKey from a packet buffer for Rx. No need to flush local caches for Rx
539  * as mempool is pre-configured and static.
540  *
541  * @param rxq
542  *   Pointer to Rx queue structure.
543  * @param addr
544  *   Address to search.
545  *
546  * @return
547  *   Searched LKey on success, UINT32_MAX on no match.
548  */
549 static __rte_always_inline uint32_t
550 mlx5_rx_addr2mr(struct mlx5_rxq_data *rxq, uintptr_t addr)
551 {
552         struct mlx5_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
553         uint32_t lkey;
554
555         /* Linear search on MR cache array. */
556         lkey = mlx5_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru,
557                                     MLX5_MR_CACHE_N, addr);
558         if (likely(lkey != UINT32_MAX))
559                 return lkey;
560         /* Take slower bottom-half (Binary Search) on miss. */
561         return mlx5_rx_addr2mr_bh(rxq, addr);
562 }
563
564 #define mlx5_rx_mb2mr(rxq, mb) mlx5_rx_addr2mr(rxq, (uintptr_t)((mb)->buf_addr))
565
566 /**
567  * Query LKey from a packet buffer for Tx. If not found, add the mempool.
568  *
569  * @param txq
570  *   Pointer to Tx queue structure.
571  * @param addr
572  *   Address to search.
573  *
574  * @return
575  *   Searched LKey on success, UINT32_MAX on no match.
576  */
577 static __rte_always_inline uint32_t
578 mlx5_tx_addr2mr(struct mlx5_txq_data *txq, uintptr_t addr)
579 {
580         struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
581         uint32_t lkey;
582
583         /* Check generation bit to see if there's any change on existing MRs. */
584         if (unlikely(*mr_ctrl->dev_gen_ptr != mr_ctrl->cur_gen))
585                 mlx5_mr_flush_local_cache(mr_ctrl);
586         /* Linear search on MR cache array. */
587         lkey = mlx5_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru,
588                                     MLX5_MR_CACHE_N, addr);
589         if (likely(lkey != UINT32_MAX))
590                 return lkey;
591         /* Take slower bottom-half (binary search) on miss. */
592         return mlx5_tx_addr2mr_bh(txq, addr);
593 }
594
595 #define mlx5_tx_mb2mr(rxq, mb) mlx5_tx_addr2mr(rxq, (uintptr_t)((mb)->buf_addr))
596
597 /**
598  * Ring TX queue doorbell and flush the update if requested.
599  *
600  * @param txq
601  *   Pointer to TX queue structure.
602  * @param wqe
603  *   Pointer to the last WQE posted in the NIC.
604  * @param cond
605  *   Request for write memory barrier after BlueFlame update.
606  */
607 static __rte_always_inline void
608 mlx5_tx_dbrec_cond_wmb(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe,
609                        int cond)
610 {
611         uint64_t *dst = (uint64_t *)((uintptr_t)txq->bf_reg);
612         volatile uint64_t *src = ((volatile uint64_t *)wqe);
613
614         rte_cio_wmb();
615         *txq->qp_db = rte_cpu_to_be_32(txq->wqe_ci);
616         /* Ensure ordering between DB record and BF copy. */
617         rte_wmb();
618         *dst = *src;
619         if (cond)
620                 rte_wmb();
621 }
622
623 /**
624  * Ring TX queue doorbell and flush the update by write memory barrier.
625  *
626  * @param txq
627  *   Pointer to TX queue structure.
628  * @param wqe
629  *   Pointer to the last WQE posted in the NIC.
630  */
631 static __rte_always_inline void
632 mlx5_tx_dbrec(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe)
633 {
634         mlx5_tx_dbrec_cond_wmb(txq, wqe, 1);
635 }
636
637 /**
638  * Convert mbuf to Verb SWP.
639  *
640  * @param txq_data
641  *   Pointer to the Tx queue.
642  * @param buf
643  *   Pointer to the mbuf.
644  * @param tso
645  *   TSO offloads enabled.
646  * @param vlan
647  *   VLAN offloads enabled
648  * @param offsets
649  *   Pointer to the SWP header offsets.
650  * @param swp_types
651  *   Pointer to the SWP header types.
652  */
653 static __rte_always_inline void
654 txq_mbuf_to_swp(struct mlx5_txq_data *txq, struct rte_mbuf *buf,
655                 uint8_t *offsets, uint8_t *swp_types)
656 {
657         const uint64_t vlan = buf->ol_flags & PKT_TX_VLAN_PKT;
658         const uint64_t tunnel = buf->ol_flags & PKT_TX_TUNNEL_MASK;
659         const uint64_t tso = buf->ol_flags & PKT_TX_TCP_SEG;
660         const uint64_t csum_flags = buf->ol_flags & PKT_TX_L4_MASK;
661         const uint64_t inner_ip =
662                 buf->ol_flags & (PKT_TX_IPV4 | PKT_TX_IPV6);
663         const uint64_t ol_flags_mask = PKT_TX_L4_MASK | PKT_TX_IPV6 |
664                                        PKT_TX_OUTER_IPV6;
665         uint16_t idx;
666         uint16_t off;
667
668         if (likely(!txq->swp_en || (tunnel != PKT_TX_TUNNEL_UDP &&
669                                     tunnel != PKT_TX_TUNNEL_IP)))
670                 return;
671         /*
672          * The index should have:
673          * bit[0:1] = PKT_TX_L4_MASK
674          * bit[4] = PKT_TX_IPV6
675          * bit[8] = PKT_TX_OUTER_IPV6
676          * bit[9] = PKT_TX_OUTER_UDP
677          */
678         idx = (buf->ol_flags & ol_flags_mask) >> 52;
679         if (tunnel == PKT_TX_TUNNEL_UDP)
680                 idx |= 1 << 9;
681         *swp_types = mlx5_swp_types_table[idx];
682         /*
683          * Set offsets for SW parser. Since ConnectX-5, SW parser just
684          * complements HW parser. SW parser starts to engage only if HW parser
685          * can't reach a header. For the older devices, HW parser will not kick
686          * in if any of SWP offsets is set. Therefore, all of the L3 offsets
687          * should be set regardless of HW offload.
688          */
689         off = buf->outer_l2_len + (vlan ? sizeof(struct vlan_hdr) : 0);
690         offsets[1] = off >> 1; /* Outer L3 offset. */
691         off += buf->outer_l3_len;
692         if (tunnel == PKT_TX_TUNNEL_UDP)
693                 offsets[0] = off >> 1; /* Outer L4 offset. */
694         if (inner_ip) {
695                 off += buf->l2_len;
696                 offsets[3] = off >> 1; /* Inner L3 offset. */
697                 if (csum_flags == PKT_TX_TCP_CKSUM || tso ||
698                     csum_flags == PKT_TX_UDP_CKSUM) {
699                         off += buf->l3_len;
700                         offsets[2] = off >> 1; /* Inner L4 offset. */
701                 }
702         }
703 }
704
705 /**
706  * Convert the Checksum offloads to Verbs.
707  *
708  * @param buf
709  *   Pointer to the mbuf.
710  *
711  * @return
712  *   Converted checksum flags.
713  */
714 static __rte_always_inline uint8_t
715 txq_ol_cksum_to_cs(struct rte_mbuf *buf)
716 {
717         uint32_t idx;
718         uint8_t is_tunnel = !!(buf->ol_flags & PKT_TX_TUNNEL_MASK);
719         const uint64_t ol_flags_mask = PKT_TX_TCP_SEG | PKT_TX_L4_MASK |
720                                        PKT_TX_IP_CKSUM | PKT_TX_OUTER_IP_CKSUM;
721
722         /*
723          * The index should have:
724          * bit[0] = PKT_TX_TCP_SEG
725          * bit[2:3] = PKT_TX_UDP_CKSUM, PKT_TX_TCP_CKSUM
726          * bit[4] = PKT_TX_IP_CKSUM
727          * bit[8] = PKT_TX_OUTER_IP_CKSUM
728          * bit[9] = tunnel
729          */
730         idx = ((buf->ol_flags & ol_flags_mask) >> 50) | (!!is_tunnel << 9);
731         return mlx5_cksum_table[idx];
732 }
733
734 /**
735  * Count the number of contiguous single segment packets.
736  *
737  * @param pkts
738  *   Pointer to array of packets.
739  * @param pkts_n
740  *   Number of packets.
741  *
742  * @return
743  *   Number of contiguous single segment packets.
744  */
745 static __rte_always_inline unsigned int
746 txq_count_contig_single_seg(struct rte_mbuf **pkts, uint16_t pkts_n)
747 {
748         unsigned int pos;
749
750         if (!pkts_n)
751                 return 0;
752         /* Count the number of contiguous single segment packets. */
753         for (pos = 0; pos < pkts_n; ++pos)
754                 if (NB_SEGS(pkts[pos]) > 1)
755                         break;
756         return pos;
757 }
758
759 /**
760  * Count the number of contiguous multi-segment packets.
761  *
762  * @param pkts
763  *   Pointer to array of packets.
764  * @param pkts_n
765  *   Number of packets.
766  *
767  * @return
768  *   Number of contiguous multi-segment packets.
769  */
770 static __rte_always_inline unsigned int
771 txq_count_contig_multi_seg(struct rte_mbuf **pkts, uint16_t pkts_n)
772 {
773         unsigned int pos;
774
775         if (!pkts_n)
776                 return 0;
777         /* Count the number of contiguous multi-segment packets. */
778         for (pos = 0; pos < pkts_n; ++pos)
779                 if (NB_SEGS(pkts[pos]) == 1)
780                         break;
781         return pos;
782 }
783
784 #endif /* RTE_PMD_MLX5_RXTX_H_ */