net/mlx5: add reference counter on DPDK Rx queues
[dpdk.git] / drivers / net / mlx5 / mlx5_rxtx.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef RTE_PMD_MLX5_RXTX_H_
35 #define RTE_PMD_MLX5_RXTX_H_
36
37 #include <stddef.h>
38 #include <stdint.h>
39 #include <sys/queue.h>
40
41 /* Verbs header. */
42 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
43 #ifdef PEDANTIC
44 #pragma GCC diagnostic ignored "-Wpedantic"
45 #endif
46 #include <infiniband/verbs.h>
47 #include <infiniband/mlx5dv.h>
48 #ifdef PEDANTIC
49 #pragma GCC diagnostic error "-Wpedantic"
50 #endif
51
52 #include <rte_mbuf.h>
53 #include <rte_mempool.h>
54 #include <rte_common.h>
55 #include <rte_hexdump.h>
56 #include <rte_atomic.h>
57
58 #include "mlx5_utils.h"
59 #include "mlx5.h"
60 #include "mlx5_autoconf.h"
61 #include "mlx5_defs.h"
62 #include "mlx5_prm.h"
63
64 struct mlx5_rxq_stats {
65         unsigned int idx; /**< Mapping index. */
66 #ifdef MLX5_PMD_SOFT_COUNTERS
67         uint64_t ipackets; /**< Total of successfully received packets. */
68         uint64_t ibytes; /**< Total of successfully received bytes. */
69 #endif
70         uint64_t idropped; /**< Total of packets dropped when RX ring full. */
71         uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
72 };
73
74 struct mlx5_txq_stats {
75         unsigned int idx; /**< Mapping index. */
76 #ifdef MLX5_PMD_SOFT_COUNTERS
77         uint64_t opackets; /**< Total of successfully sent packets. */
78         uint64_t obytes; /**< Total of successfully sent bytes. */
79 #endif
80         uint64_t oerrors; /**< Total number of failed transmitted packets. */
81 };
82
83 struct priv;
84
85 /* Memory region queue object. */
86 struct mlx5_mr {
87         LIST_ENTRY(mlx5_mr) next; /**< Pointer to the next element. */
88         rte_atomic32_t refcnt; /*<< Reference counter. */
89         uint32_t lkey; /*<< rte_cpu_to_be_32(mr->lkey) */
90         uintptr_t start; /* Start address of MR */
91         uintptr_t end; /* End address of MR */
92         struct ibv_mr *mr; /*<< Memory Region. */
93         struct rte_mempool *mp; /*<< Memory Pool. */
94 };
95
96 /* Compressed CQE context. */
97 struct rxq_zip {
98         uint16_t ai; /* Array index. */
99         uint16_t ca; /* Current array index. */
100         uint16_t na; /* Next array index. */
101         uint16_t cq_ci; /* The next CQE. */
102         uint32_t cqe_cnt; /* Number of CQEs. */
103 };
104
105 /* RX queue descriptor. */
106 struct mlx5_rxq_data {
107         unsigned int csum:1; /* Enable checksum offloading. */
108         unsigned int csum_l2tun:1; /* Same for L2 tunnels. */
109         unsigned int vlan_strip:1; /* Enable VLAN stripping. */
110         unsigned int crc_present:1; /* CRC must be subtracted. */
111         unsigned int sges_n:2; /* 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 pending_err:1; /* CQE error needs to be handled. */
117         unsigned int :15; /* Remaining bits. */
118         volatile uint32_t *rq_db;
119         volatile uint32_t *cq_db;
120         uint16_t port_id;
121         uint16_t rq_ci;
122         uint16_t rq_pi;
123         uint16_t cq_ci;
124         volatile struct mlx5_wqe_data_seg(*wqes)[];
125         volatile struct mlx5_cqe(*cqes)[];
126         struct rxq_zip zip; /* Compressed context. */
127         struct rte_mbuf *(*elts)[];
128         struct rte_mempool *mp;
129         struct mlx5_rxq_stats stats;
130         uint64_t mbuf_initializer; /* Default rearm_data for vectorized Rx. */
131         struct rte_mbuf fake_mbuf; /* elts padding for vectorized Rx. */
132         void *cq_uar; /* CQ user access region. */
133         uint32_t cqn; /* CQ number. */
134         uint8_t cq_arm_sn; /* CQ arm seq number. */
135 } __rte_cache_aligned;
136
137 /* Verbs Rx queue elements. */
138 struct mlx5_rxq_ibv {
139         LIST_ENTRY(mlx5_rxq_ibv) next; /* Pointer to the next element. */
140         rte_atomic32_t refcnt; /* Reference counter. */
141         struct mlx5_rxq_ctrl *rxq_ctrl; /* Back pointer to parent. */
142         struct ibv_cq *cq; /* Completion Queue. */
143         struct ibv_wq *wq; /* Work Queue. */
144         struct ibv_comp_channel *channel;
145         struct mlx5_mr *mr; /* Memory Region (for mp). */
146 };
147
148 /* RX queue control descriptor. */
149 struct mlx5_rxq_ctrl {
150         LIST_ENTRY(mlx5_rxq_ctrl) next; /* Pointer to the next element. */
151         rte_atomic32_t refcnt; /* Reference counter. */
152         struct priv *priv; /* Back pointer to private data. */
153         struct mlx5_rxq_ibv *ibv; /* Verbs elements. */
154         struct mlx5_rxq_data rxq; /* Data path structure. */
155         unsigned int socket; /* CPU socket ID for allocations. */
156         unsigned int irq:1; /* Whether IRQ is enabled. */
157 };
158
159 /* Hash RX queue types. */
160 enum hash_rxq_type {
161         HASH_RXQ_TCPV4,
162         HASH_RXQ_UDPV4,
163         HASH_RXQ_IPV4,
164         HASH_RXQ_TCPV6,
165         HASH_RXQ_UDPV6,
166         HASH_RXQ_IPV6,
167         HASH_RXQ_ETH,
168 };
169
170 /* Flow structure with Ethernet specification. It is packed to prevent padding
171  * between attr and spec as this layout is expected by libibverbs. */
172 struct flow_attr_spec_eth {
173         struct ibv_flow_attr attr;
174         struct ibv_flow_spec_eth spec;
175 } __attribute__((packed));
176
177 /* Define a struct flow_attr_spec_eth object as an array of at least
178  * "size" bytes. Room after the first index is normally used to store
179  * extra flow specifications. */
180 #define FLOW_ATTR_SPEC_ETH(name, size) \
181         struct flow_attr_spec_eth name \
182                 [((size) / sizeof(struct flow_attr_spec_eth)) + \
183                  !!((size) % sizeof(struct flow_attr_spec_eth))]
184
185 /* Initialization data for hash RX queue. */
186 struct hash_rxq_init {
187         uint64_t hash_fields; /* Fields that participate in the hash. */
188         uint64_t dpdk_rss_hf; /* Matching DPDK RSS hash fields. */
189         unsigned int flow_priority; /* Flow priority to use. */
190         union {
191                 struct {
192                         enum ibv_flow_spec_type type;
193                         uint16_t size;
194                 } hdr;
195                 struct ibv_flow_spec_tcp_udp tcp_udp;
196                 struct ibv_flow_spec_ipv4 ipv4;
197                 struct ibv_flow_spec_ipv6 ipv6;
198                 struct ibv_flow_spec_eth eth;
199         } flow_spec; /* Flow specification template. */
200         const struct hash_rxq_init *underlayer; /* Pointer to underlayer. */
201 };
202
203 /* Initialization data for indirection table. */
204 struct ind_table_init {
205         unsigned int max_size; /* Maximum number of WQs. */
206         /* Hash RX queues using this table. */
207         unsigned int hash_types;
208         unsigned int hash_types_n;
209 };
210
211 /* Initialization data for special flows. */
212 struct special_flow_init {
213         uint8_t dst_mac_val[6];
214         uint8_t dst_mac_mask[6];
215         unsigned int hash_types;
216         unsigned int per_vlan:1;
217 };
218
219 enum hash_rxq_flow_type {
220         HASH_RXQ_FLOW_TYPE_PROMISC,
221         HASH_RXQ_FLOW_TYPE_ALLMULTI,
222         HASH_RXQ_FLOW_TYPE_BROADCAST,
223         HASH_RXQ_FLOW_TYPE_IPV6MULTI,
224         HASH_RXQ_FLOW_TYPE_MAC,
225 };
226
227 #ifndef NDEBUG
228 static inline const char *
229 hash_rxq_flow_type_str(enum hash_rxq_flow_type flow_type)
230 {
231         switch (flow_type) {
232         case HASH_RXQ_FLOW_TYPE_PROMISC:
233                 return "promiscuous";
234         case HASH_RXQ_FLOW_TYPE_ALLMULTI:
235                 return "allmulticast";
236         case HASH_RXQ_FLOW_TYPE_BROADCAST:
237                 return "broadcast";
238         case HASH_RXQ_FLOW_TYPE_IPV6MULTI:
239                 return "IPv6 multicast";
240         case HASH_RXQ_FLOW_TYPE_MAC:
241                 return "MAC";
242         }
243         return NULL;
244 }
245 #endif /* NDEBUG */
246
247 struct hash_rxq {
248         struct priv *priv; /* Back pointer to private data. */
249         struct ibv_qp *qp; /* Hash RX QP. */
250         enum hash_rxq_type type; /* Hash RX queue type. */
251         /* MAC flow steering rules, one per VLAN ID. */
252         struct ibv_flow *mac_flow
253                 [MLX5_MAX_MAC_ADDRESSES][MLX5_MAX_VLAN_IDS];
254         struct ibv_flow *special_flow
255                 [MLX5_MAX_SPECIAL_FLOWS][MLX5_MAX_VLAN_IDS];
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; /* Counter since last completion request. */
264         uint16_t mpw_comp; /* WQ index since last completion request. */
265         uint16_t cq_ci; /* Consumer index for completion queue. */
266         uint16_t cq_pi; /* Producer index for completion queue. */
267         uint16_t wqe_ci; /* Consumer index for work queue. */
268         uint16_t wqe_pi; /* Producer index for work queue. */
269         uint16_t elts_n:4; /* (*elts)[] length (in log2). */
270         uint16_t cqe_n:4; /* Number of CQ elements (in log2). */
271         uint16_t wqe_n:4; /* Number of of WQ elements (in log2). */
272         uint16_t inline_en:1; /* When set inline is enabled. */
273         uint16_t tso_en:1; /* When set hardware TSO is enabled. */
274         uint16_t tunnel_en:1;
275         /* When set TX offload for tunneled packets are supported. */
276         uint16_t mpw_hdr_dseg:1; /* Enable DSEGs in the title WQEBB. */
277         uint16_t max_inline; /* Multiple of RTE_CACHE_LINE_SIZE to inline. */
278         uint16_t inline_max_packet_sz; /* Max packet size for inlining. */
279         uint16_t mr_cache_idx; /* Index of last hit entry. */
280         uint32_t qp_num_8s; /* QP number shifted by 8. */
281         uint32_t flags; /* Flags for Tx Queue. */
282         volatile struct mlx5_cqe (*cqes)[]; /* Completion queue. */
283         volatile void *wqes; /* Work queue (use volatile to write into). */
284         volatile uint32_t *qp_db; /* Work queue doorbell. */
285         volatile uint32_t *cq_db; /* Completion queue doorbell. */
286         volatile void *bf_reg; /* Blueflame register. */
287         struct mlx5_mr *mp2mr[MLX5_PMD_TX_MP_CACHE]; /* MR translation table. */
288         struct rte_mbuf *(*elts)[]; /* TX elements. */
289         struct mlx5_txq_stats stats; /* TX queue counters. */
290 } __rte_cache_aligned;
291
292 /* Verbs Rx queue elements. */
293 struct mlx5_txq_ibv {
294         LIST_ENTRY(mlx5_txq_ibv) next; /* Pointer to the next element. */
295         rte_atomic32_t refcnt; /* Reference counter. */
296         struct ibv_cq *cq; /* Completion Queue. */
297         struct ibv_qp *qp; /* Queue Pair. */
298 };
299
300 /* TX queue control descriptor. */
301 struct mlx5_txq_ctrl {
302         LIST_ENTRY(mlx5_txq_ctrl) next; /* Pointer to the next element. */
303         rte_atomic32_t refcnt; /* Reference counter. */
304         struct priv *priv; /* Back pointer to private data. */
305         unsigned int socket; /* CPU socket ID for allocations. */
306         unsigned int max_inline_data; /* Max inline data. */
307         unsigned int max_tso_header; /* Max TSO header size. */
308         struct mlx5_txq_ibv *ibv; /* Verbs queue object. */
309         struct mlx5_txq_data txq; /* Data path structure. */
310         off_t uar_mmap_offset; /* UAR mmap offset for non-primary process. */
311 };
312
313 /* mlx5_rxq.c */
314
315 extern const struct hash_rxq_init hash_rxq_init[];
316 extern const unsigned int hash_rxq_init_n;
317
318 extern uint8_t rss_hash_default_key[];
319 extern const size_t rss_hash_default_key_len;
320
321 size_t priv_flow_attr(struct priv *, struct ibv_flow_attr *,
322                       size_t, enum hash_rxq_type);
323 int priv_create_hash_rxqs(struct priv *);
324 void priv_destroy_hash_rxqs(struct priv *);
325 int priv_allow_flow_type(struct priv *, enum hash_rxq_flow_type);
326 int priv_rehash_flows(struct priv *);
327 void mlx5_rxq_cleanup(struct mlx5_rxq_ctrl *);
328 int mlx5_rx_queue_setup(struct rte_eth_dev *, uint16_t, uint16_t, unsigned int,
329                         const struct rte_eth_rxconf *, struct rte_mempool *);
330 void mlx5_rx_queue_release(void *);
331 int priv_rx_intr_vec_enable(struct priv *priv);
332 void priv_rx_intr_vec_disable(struct priv *priv);
333 int mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
334 int mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id);
335 struct mlx5_rxq_ibv *mlx5_priv_rxq_ibv_new(struct priv *, uint16_t);
336 struct mlx5_rxq_ibv *mlx5_priv_rxq_ibv_get(struct priv *, uint16_t);
337 int mlx5_priv_rxq_ibv_release(struct priv *, struct mlx5_rxq_ibv *);
338 int mlx5_priv_rxq_ibv_releasable(struct priv *, struct mlx5_rxq_ibv *);
339 int mlx5_priv_rxq_ibv_verify(struct priv *);
340 struct mlx5_rxq_ctrl *mlx5_priv_rxq_new(struct priv *, uint16_t,
341                                         uint16_t, unsigned int,
342                                         struct rte_mempool *);
343 struct mlx5_rxq_ctrl *mlx5_priv_rxq_get(struct priv *, uint16_t);
344 int mlx5_priv_rxq_release(struct priv *, uint16_t);
345 int mlx5_priv_rxq_releasable(struct priv *, uint16_t);
346 int mlx5_priv_rxq_verify(struct priv *);
347 int rxq_alloc_elts(struct mlx5_rxq_ctrl *);
348
349 /* mlx5_txq.c */
350
351 int mlx5_tx_queue_setup(struct rte_eth_dev *, uint16_t, uint16_t, unsigned int,
352                         const struct rte_eth_txconf *);
353 void mlx5_tx_queue_release(void *);
354 int priv_tx_uar_remap(struct priv *priv, int fd);
355 struct mlx5_txq_ibv *mlx5_priv_txq_ibv_new(struct priv *, uint16_t);
356 struct mlx5_txq_ibv *mlx5_priv_txq_ibv_get(struct priv *, uint16_t);
357 int mlx5_priv_txq_ibv_release(struct priv *, struct mlx5_txq_ibv *);
358 int mlx5_priv_txq_ibv_releasable(struct priv *, struct mlx5_txq_ibv *);
359 int mlx5_priv_txq_ibv_verify(struct priv *);
360 struct mlx5_txq_ctrl *mlx5_priv_txq_new(struct priv *, uint16_t,
361                                         uint16_t, unsigned int,
362                                         const struct rte_eth_txconf *);
363 struct mlx5_txq_ctrl *mlx5_priv_txq_get(struct priv *, uint16_t);
364 int mlx5_priv_txq_release(struct priv *, uint16_t);
365 int mlx5_priv_txq_releasable(struct priv *, uint16_t);
366 int mlx5_priv_txq_verify(struct priv *);
367 void txq_alloc_elts(struct mlx5_txq_ctrl *);
368
369 /* mlx5_rxtx.c */
370
371 extern uint32_t mlx5_ptype_table[];
372
373 void mlx5_set_ptype_table(void);
374 uint16_t mlx5_tx_burst(void *, struct rte_mbuf **, uint16_t);
375 uint16_t mlx5_tx_burst_mpw(void *, struct rte_mbuf **, uint16_t);
376 uint16_t mlx5_tx_burst_mpw_inline(void *, struct rte_mbuf **, uint16_t);
377 uint16_t mlx5_tx_burst_empw(void *, struct rte_mbuf **, uint16_t);
378 uint16_t mlx5_rx_burst(void *, struct rte_mbuf **, uint16_t);
379 uint16_t removed_tx_burst(void *, struct rte_mbuf **, uint16_t);
380 uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
381 int mlx5_rx_descriptor_status(void *, uint16_t);
382 int mlx5_tx_descriptor_status(void *, uint16_t);
383
384 /* Vectorized version of mlx5_rxtx.c */
385 int priv_check_raw_vec_tx_support(struct priv *);
386 int priv_check_vec_tx_support(struct priv *);
387 int rxq_check_vec_support(struct mlx5_rxq_data *);
388 int priv_check_vec_rx_support(struct priv *);
389 uint16_t mlx5_tx_burst_raw_vec(void *, struct rte_mbuf **, uint16_t);
390 uint16_t mlx5_tx_burst_vec(void *, struct rte_mbuf **, uint16_t);
391 uint16_t mlx5_rx_burst_vec(void *, struct rte_mbuf **, uint16_t);
392
393 /* mlx5_mr.c */
394
395 void mlx5_mp2mr_iter(struct rte_mempool *, void *);
396 struct mlx5_mr *priv_txq_mp2mr_reg(struct priv *priv, struct mlx5_txq_data *,
397                                    struct rte_mempool *, unsigned int);
398 struct mlx5_mr *mlx5_txq_mp2mr_reg(struct mlx5_txq_data *, struct rte_mempool *,
399                                    unsigned int);
400
401 #ifndef NDEBUG
402 /**
403  * Verify or set magic value in CQE.
404  *
405  * @param cqe
406  *   Pointer to CQE.
407  *
408  * @return
409  *   0 the first time.
410  */
411 static inline int
412 check_cqe_seen(volatile struct mlx5_cqe *cqe)
413 {
414         static const uint8_t magic[] = "seen";
415         volatile uint8_t (*buf)[sizeof(cqe->rsvd0)] = &cqe->rsvd0;
416         int ret = 1;
417         unsigned int i;
418
419         for (i = 0; i < sizeof(magic) && i < sizeof(*buf); ++i)
420                 if (!ret || (*buf)[i] != magic[i]) {
421                         ret = 0;
422                         (*buf)[i] = magic[i];
423                 }
424         return ret;
425 }
426 #endif /* NDEBUG */
427
428 /**
429  * Check whether CQE is valid.
430  *
431  * @param cqe
432  *   Pointer to CQE.
433  * @param cqes_n
434  *   Size of completion queue.
435  * @param ci
436  *   Consumer index.
437  *
438  * @return
439  *   0 on success, 1 on failure.
440  */
441 static __rte_always_inline int
442 check_cqe(volatile struct mlx5_cqe *cqe,
443           unsigned int cqes_n, const uint16_t ci)
444 {
445         uint16_t idx = ci & cqes_n;
446         uint8_t op_own = cqe->op_own;
447         uint8_t op_owner = MLX5_CQE_OWNER(op_own);
448         uint8_t op_code = MLX5_CQE_OPCODE(op_own);
449
450         if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
451                 return 1; /* No CQE. */
452 #ifndef NDEBUG
453         if ((op_code == MLX5_CQE_RESP_ERR) ||
454             (op_code == MLX5_CQE_REQ_ERR)) {
455                 volatile struct mlx5_err_cqe *err_cqe = (volatile void *)cqe;
456                 uint8_t syndrome = err_cqe->syndrome;
457
458                 if ((syndrome == MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR) ||
459                     (syndrome == MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR))
460                         return 0;
461                 if (!check_cqe_seen(cqe)) {
462                         ERROR("unexpected CQE error %u (0x%02x)"
463                               " syndrome 0x%02x",
464                               op_code, op_code, syndrome);
465                         rte_hexdump(stderr, "MLX5 Error CQE:",
466                                     (const void *)((uintptr_t)err_cqe),
467                                     sizeof(*err_cqe));
468                 }
469                 return 1;
470         } else if ((op_code != MLX5_CQE_RESP_SEND) &&
471                    (op_code != MLX5_CQE_REQ)) {
472                 if (!check_cqe_seen(cqe)) {
473                         ERROR("unexpected CQE opcode %u (0x%02x)",
474                               op_code, op_code);
475                         rte_hexdump(stderr, "MLX5 CQE:",
476                                     (const void *)((uintptr_t)cqe),
477                                     sizeof(*cqe));
478                 }
479                 return 1;
480         }
481 #endif /* NDEBUG */
482         return 0;
483 }
484
485 /**
486  * Return the address of the WQE.
487  *
488  * @param txq
489  *   Pointer to TX queue structure.
490  * @param  wqe_ci
491  *   WQE consumer index.
492  *
493  * @return
494  *   WQE address.
495  */
496 static inline uintptr_t *
497 tx_mlx5_wqe(struct mlx5_txq_data *txq, uint16_t ci)
498 {
499         ci &= ((1 << txq->wqe_n) - 1);
500         return (uintptr_t *)((uintptr_t)txq->wqes + ci * MLX5_WQE_SIZE);
501 }
502
503 /**
504  * Manage TX completions.
505  *
506  * When sending a burst, mlx5_tx_burst() posts several WRs.
507  *
508  * @param txq
509  *   Pointer to TX queue structure.
510  */
511 static __rte_always_inline void
512 mlx5_tx_complete(struct mlx5_txq_data *txq)
513 {
514         const uint16_t elts_n = 1 << txq->elts_n;
515         const uint16_t elts_m = elts_n - 1;
516         const unsigned int cqe_n = 1 << txq->cqe_n;
517         const unsigned int cqe_cnt = cqe_n - 1;
518         uint16_t elts_free = txq->elts_tail;
519         uint16_t elts_tail;
520         uint16_t cq_ci = txq->cq_ci;
521         volatile struct mlx5_cqe *cqe = NULL;
522         volatile struct mlx5_wqe_ctrl *ctrl;
523         struct rte_mbuf *m, *free[elts_n];
524         struct rte_mempool *pool = NULL;
525         unsigned int blk_n = 0;
526
527         cqe = &(*txq->cqes)[cq_ci & cqe_cnt];
528         if (unlikely(check_cqe(cqe, cqe_n, cq_ci)))
529                 return;
530 #ifndef NDEBUG
531         if ((MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_RESP_ERR) ||
532             (MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_REQ_ERR)) {
533                 if (!check_cqe_seen(cqe)) {
534                         ERROR("unexpected error CQE, TX stopped");
535                         rte_hexdump(stderr, "MLX5 TXQ:",
536                                     (const void *)((uintptr_t)txq->wqes),
537                                     ((1 << txq->wqe_n) *
538                                      MLX5_WQE_SIZE));
539                 }
540                 return;
541         }
542 #endif /* NDEBUG */
543         ++cq_ci;
544         txq->wqe_pi = rte_be_to_cpu_16(cqe->wqe_counter);
545         ctrl = (volatile struct mlx5_wqe_ctrl *)
546                 tx_mlx5_wqe(txq, txq->wqe_pi);
547         elts_tail = ctrl->ctrl3;
548         assert((elts_tail & elts_m) < (1 << txq->wqe_n));
549         /* Free buffers. */
550         while (elts_free != elts_tail) {
551                 m = rte_pktmbuf_prefree_seg((*txq->elts)[elts_free++ & elts_m]);
552                 if (likely(m != NULL)) {
553                         if (likely(m->pool == pool)) {
554                                 free[blk_n++] = m;
555                         } else {
556                                 if (likely(pool != NULL))
557                                         rte_mempool_put_bulk(pool,
558                                                              (void *)free,
559                                                              blk_n);
560                                 free[0] = m;
561                                 pool = m->pool;
562                                 blk_n = 1;
563                         }
564                 }
565         }
566         if (blk_n)
567                 rte_mempool_put_bulk(pool, (void *)free, blk_n);
568 #ifndef NDEBUG
569         elts_free = txq->elts_tail;
570         /* Poisoning. */
571         while (elts_free != elts_tail) {
572                 memset(&(*txq->elts)[elts_free & elts_m],
573                        0x66,
574                        sizeof((*txq->elts)[elts_free & elts_m]));
575                 ++elts_free;
576         }
577 #endif
578         txq->cq_ci = cq_ci;
579         txq->elts_tail = elts_tail;
580         /* Update the consumer index. */
581         rte_wmb();
582         *txq->cq_db = rte_cpu_to_be_32(cq_ci);
583 }
584
585 /**
586  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
587  * the cloned mbuf is allocated is returned instead.
588  *
589  * @param buf
590  *   Pointer to mbuf.
591  *
592  * @return
593  *   Memory pool where data is located for given mbuf.
594  */
595 static struct rte_mempool *
596 mlx5_tx_mb2mp(struct rte_mbuf *buf)
597 {
598         if (unlikely(RTE_MBUF_INDIRECT(buf)))
599                 return rte_mbuf_from_indirect(buf)->pool;
600         return buf->pool;
601 }
602
603 /**
604  * Get Memory Region (MR) <-> rte_mbuf association from txq->mp2mr[].
605  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
606  * remove an entry first.
607  *
608  * @param txq
609  *   Pointer to TX queue structure.
610  * @param[in] mp
611  *   Memory Pool for which a Memory Region lkey must be returned.
612  *
613  * @return
614  *   mr->lkey on success, (uint32_t)-1 on failure.
615  */
616 static __rte_always_inline uint32_t
617 mlx5_tx_mb2mr(struct mlx5_txq_data *txq, struct rte_mbuf *mb)
618 {
619         uint16_t i = txq->mr_cache_idx;
620         uintptr_t addr = rte_pktmbuf_mtod(mb, uintptr_t);
621         struct mlx5_mr *mr;
622
623         assert(i < RTE_DIM(txq->mp2mr));
624         if (likely(txq->mp2mr[i]->start <= addr && txq->mp2mr[i]->end >= addr))
625                 return txq->mp2mr[i]->lkey;
626         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
627                 if (unlikely(txq->mp2mr[i]->mr == NULL)) {
628                         /* Unknown MP, add a new MR for it. */
629                         break;
630                 }
631                 if (txq->mp2mr[i]->start <= addr &&
632                     txq->mp2mr[i]->end >= addr) {
633                         assert(txq->mp2mr[i]->lkey != (uint32_t)-1);
634                         assert(rte_cpu_to_be_32(txq->mp2mr[i]->mr->lkey) ==
635                                txq->mp2mr[i]->lkey);
636                         txq->mr_cache_idx = i;
637                         return txq->mp2mr[i]->lkey;
638                 }
639         }
640         txq->mr_cache_idx = 0;
641         mr = mlx5_txq_mp2mr_reg(txq, mlx5_tx_mb2mp(mb), i);
642         /*
643          * Request the reference to use in this queue, the original one is
644          * kept by the control plane.
645          */
646         if (mr) {
647                 rte_atomic32_inc(&mr->refcnt);
648                 return mr->lkey;
649         }
650         return (uint32_t)-1;
651 }
652
653 /**
654  * Ring TX queue doorbell.
655  *
656  * @param txq
657  *   Pointer to TX queue structure.
658  * @param wqe
659  *   Pointer to the last WQE posted in the NIC.
660  */
661 static __rte_always_inline void
662 mlx5_tx_dbrec(struct mlx5_txq_data *txq, volatile struct mlx5_wqe *wqe)
663 {
664         uint64_t *dst = (uint64_t *)((uintptr_t)txq->bf_reg);
665         volatile uint64_t *src = ((volatile uint64_t *)wqe);
666
667         rte_io_wmb();
668         *txq->qp_db = rte_cpu_to_be_32(txq->wqe_ci);
669         /* Ensure ordering between DB record and BF copy. */
670         rte_wmb();
671         *dst = *src;
672 }
673
674 #endif /* RTE_PMD_MLX5_RXTX_H_ */