Imported Upstream version 16.11.1
[deb_dpdk.git] / drivers / net / mlx5 / mlx5_rxtx.c
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 #include <assert.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 /* Verbs header. */
40 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
41 #ifdef PEDANTIC
42 #pragma GCC diagnostic ignored "-Wpedantic"
43 #endif
44 #include <infiniband/verbs.h>
45 #include <infiniband/mlx5_hw.h>
46 #include <infiniband/arch.h>
47 #ifdef PEDANTIC
48 #pragma GCC diagnostic error "-Wpedantic"
49 #endif
50
51 /* DPDK headers don't like -pedantic. */
52 #ifdef PEDANTIC
53 #pragma GCC diagnostic ignored "-Wpedantic"
54 #endif
55 #include <rte_mbuf.h>
56 #include <rte_mempool.h>
57 #include <rte_prefetch.h>
58 #include <rte_common.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_ether.h>
61 #ifdef PEDANTIC
62 #pragma GCC diagnostic error "-Wpedantic"
63 #endif
64
65 #include "mlx5.h"
66 #include "mlx5_utils.h"
67 #include "mlx5_rxtx.h"
68 #include "mlx5_autoconf.h"
69 #include "mlx5_defs.h"
70 #include "mlx5_prm.h"
71
72 static inline int
73 check_cqe(volatile struct mlx5_cqe *cqe,
74           unsigned int cqes_n, const uint16_t ci)
75           __attribute__((always_inline));
76
77 static inline uint32_t
78 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
79         __attribute__((always_inline));
80
81 static inline void
82 mlx5_tx_dbrec(struct txq *txq, volatile struct mlx5_wqe *wqe)
83         __attribute__((always_inline));
84
85 static inline uint32_t
86 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe)
87         __attribute__((always_inline));
88
89 static inline int
90 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
91                  uint16_t cqe_cnt, uint32_t *rss_hash)
92                  __attribute__((always_inline));
93
94 static inline uint32_t
95 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe)
96                    __attribute__((always_inline));
97
98 #ifndef NDEBUG
99
100 /**
101  * Verify or set magic value in CQE.
102  *
103  * @param cqe
104  *   Pointer to CQE.
105  *
106  * @return
107  *   0 the first time.
108  */
109 static inline int
110 check_cqe_seen(volatile struct mlx5_cqe *cqe)
111 {
112         static const uint8_t magic[] = "seen";
113         volatile uint8_t (*buf)[sizeof(cqe->rsvd3)] = &cqe->rsvd3;
114         int ret = 1;
115         unsigned int i;
116
117         for (i = 0; i < sizeof(magic) && i < sizeof(*buf); ++i)
118                 if (!ret || (*buf)[i] != magic[i]) {
119                         ret = 0;
120                         (*buf)[i] = magic[i];
121                 }
122         return ret;
123 }
124
125 #endif /* NDEBUG */
126
127 /**
128  * Check whether CQE is valid.
129  *
130  * @param cqe
131  *   Pointer to CQE.
132  * @param cqes_n
133  *   Size of completion queue.
134  * @param ci
135  *   Consumer index.
136  *
137  * @return
138  *   0 on success, 1 on failure.
139  */
140 static inline int
141 check_cqe(volatile struct mlx5_cqe *cqe,
142           unsigned int cqes_n, const uint16_t ci)
143 {
144         uint16_t idx = ci & cqes_n;
145         uint8_t op_own = cqe->op_own;
146         uint8_t op_owner = MLX5_CQE_OWNER(op_own);
147         uint8_t op_code = MLX5_CQE_OPCODE(op_own);
148
149         if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
150                 return 1; /* No CQE. */
151 #ifndef NDEBUG
152         if ((op_code == MLX5_CQE_RESP_ERR) ||
153             (op_code == MLX5_CQE_REQ_ERR)) {
154                 volatile struct mlx5_err_cqe *err_cqe = (volatile void *)cqe;
155                 uint8_t syndrome = err_cqe->syndrome;
156
157                 if ((syndrome == MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR) ||
158                     (syndrome == MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR))
159                         return 0;
160                 if (!check_cqe_seen(cqe))
161                         ERROR("unexpected CQE error %u (0x%02x)"
162                               " syndrome 0x%02x",
163                               op_code, op_code, syndrome);
164                 return 1;
165         } else if ((op_code != MLX5_CQE_RESP_SEND) &&
166                    (op_code != MLX5_CQE_REQ)) {
167                 if (!check_cqe_seen(cqe))
168                         ERROR("unexpected CQE opcode %u (0x%02x)",
169                               op_code, op_code);
170                 return 1;
171         }
172 #endif /* NDEBUG */
173         return 0;
174 }
175
176 static inline void
177 txq_complete(struct txq *txq) __attribute__((always_inline));
178
179 /**
180  * Manage TX completions.
181  *
182  * When sending a burst, mlx5_tx_burst() posts several WRs.
183  *
184  * @param txq
185  *   Pointer to TX queue structure.
186  */
187 static inline void
188 txq_complete(struct txq *txq)
189 {
190         const unsigned int elts_n = 1 << txq->elts_n;
191         const unsigned int cqe_n = 1 << txq->cqe_n;
192         const unsigned int cqe_cnt = cqe_n - 1;
193         uint16_t elts_free = txq->elts_tail;
194         uint16_t elts_tail;
195         uint16_t cq_ci = txq->cq_ci;
196         volatile struct mlx5_cqe *cqe = NULL;
197         volatile struct mlx5_wqe *wqe;
198
199         do {
200                 volatile struct mlx5_cqe *tmp;
201
202                 tmp = &(*txq->cqes)[cq_ci & cqe_cnt];
203                 if (check_cqe(tmp, cqe_n, cq_ci))
204                         break;
205                 cqe = tmp;
206 #ifndef NDEBUG
207                 if (MLX5_CQE_FORMAT(cqe->op_own) == MLX5_COMPRESSED) {
208                         if (!check_cqe_seen(cqe))
209                                 ERROR("unexpected compressed CQE, TX stopped");
210                         return;
211                 }
212                 if ((MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_RESP_ERR) ||
213                     (MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_REQ_ERR)) {
214                         if (!check_cqe_seen(cqe))
215                                 ERROR("unexpected error CQE, TX stopped");
216                         return;
217                 }
218 #endif /* NDEBUG */
219                 ++cq_ci;
220         } while (1);
221         if (unlikely(cqe == NULL))
222                 return;
223         wqe = &(*txq->wqes)[ntohs(cqe->wqe_counter) &
224                             ((1 << txq->wqe_n) - 1)].hdr;
225         elts_tail = wqe->ctrl[3];
226         assert(elts_tail < (1 << txq->wqe_n));
227         /* Free buffers. */
228         while (elts_free != elts_tail) {
229                 struct rte_mbuf *elt = (*txq->elts)[elts_free];
230                 unsigned int elts_free_next =
231                         (elts_free + 1) & (elts_n - 1);
232                 struct rte_mbuf *elt_next = (*txq->elts)[elts_free_next];
233
234 #ifndef NDEBUG
235                 /* Poisoning. */
236                 memset(&(*txq->elts)[elts_free],
237                        0x66,
238                        sizeof((*txq->elts)[elts_free]));
239 #endif
240                 RTE_MBUF_PREFETCH_TO_FREE(elt_next);
241                 /* Only one segment needs to be freed. */
242                 rte_pktmbuf_free_seg(elt);
243                 elts_free = elts_free_next;
244         }
245         txq->cq_ci = cq_ci;
246         txq->elts_tail = elts_tail;
247         /* Update the consumer index. */
248         rte_wmb();
249         *txq->cq_db = htonl(cq_ci);
250 }
251
252 /**
253  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
254  * the cloned mbuf is allocated is returned instead.
255  *
256  * @param buf
257  *   Pointer to mbuf.
258  *
259  * @return
260  *   Memory pool where data is located for given mbuf.
261  */
262 static struct rte_mempool *
263 txq_mb2mp(struct rte_mbuf *buf)
264 {
265         if (unlikely(RTE_MBUF_INDIRECT(buf)))
266                 return rte_mbuf_from_indirect(buf)->pool;
267         return buf->pool;
268 }
269
270 /**
271  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
272  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
273  * remove an entry first.
274  *
275  * @param txq
276  *   Pointer to TX queue structure.
277  * @param[in] mp
278  *   Memory Pool for which a Memory Region lkey must be returned.
279  *
280  * @return
281  *   mr->lkey on success, (uint32_t)-1 on failure.
282  */
283 static inline uint32_t
284 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
285 {
286         unsigned int i;
287         uint32_t lkey = (uint32_t)-1;
288
289         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
290                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
291                         /* Unknown MP, add a new MR for it. */
292                         break;
293                 }
294                 if (txq->mp2mr[i].mp == mp) {
295                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
296                         assert(htonl(txq->mp2mr[i].mr->lkey) ==
297                                txq->mp2mr[i].lkey);
298                         lkey = txq->mp2mr[i].lkey;
299                         break;
300                 }
301         }
302         if (unlikely(lkey == (uint32_t)-1))
303                 lkey = txq_mp2mr_reg(txq, mp, i);
304         return lkey;
305 }
306
307 /**
308  * Ring TX queue doorbell.
309  *
310  * @param txq
311  *   Pointer to TX queue structure.
312  * @param wqe
313  *   Pointer to the last WQE posted in the NIC.
314  */
315 static inline void
316 mlx5_tx_dbrec(struct txq *txq, volatile struct mlx5_wqe *wqe)
317 {
318         uint64_t *dst = (uint64_t *)((uintptr_t)txq->bf_reg);
319         volatile uint64_t *src = ((volatile uint64_t *)wqe);
320
321         rte_wmb();
322         *txq->qp_db = htonl(txq->wqe_ci);
323         /* Ensure ordering between DB record and BF copy. */
324         rte_wmb();
325         *dst = *src;
326 }
327
328 /**
329  * Prefetch a CQE.
330  *
331  * @param txq
332  *   Pointer to TX queue structure.
333  * @param cqe_ci
334  *   CQE consumer index.
335  */
336 static inline void
337 tx_prefetch_cqe(struct txq *txq, uint16_t ci)
338 {
339         volatile struct mlx5_cqe *cqe;
340
341         cqe = &(*txq->cqes)[ci & ((1 << txq->cqe_n) - 1)];
342         rte_prefetch0(cqe);
343 }
344
345 /**
346  * Prefetch a WQE.
347  *
348  * @param txq
349  *   Pointer to TX queue structure.
350  * @param  wqe_ci
351  *   WQE consumer index.
352  */
353 static inline void
354 tx_prefetch_wqe(struct txq *txq, uint16_t ci)
355 {
356         volatile struct mlx5_wqe64 *wqe;
357
358         wqe = &(*txq->wqes)[ci & ((1 << txq->wqe_n) - 1)];
359         rte_prefetch0(wqe);
360 }
361
362 /**
363  * DPDK callback for TX.
364  *
365  * @param dpdk_txq
366  *   Generic pointer to TX queue structure.
367  * @param[in] pkts
368  *   Packets to transmit.
369  * @param pkts_n
370  *   Number of packets in array.
371  *
372  * @return
373  *   Number of packets successfully transmitted (<= pkts_n).
374  */
375 uint16_t
376 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
377 {
378         struct txq *txq = (struct txq *)dpdk_txq;
379         uint16_t elts_head = txq->elts_head;
380         const unsigned int elts_n = 1 << txq->elts_n;
381         unsigned int i = 0;
382         unsigned int j = 0;
383         unsigned int max;
384         unsigned int comp;
385         volatile struct mlx5_wqe *wqe = NULL;
386         unsigned int segs_n = 0;
387         struct rte_mbuf *buf = NULL;
388         uint8_t *raw;
389
390         if (unlikely(!pkts_n))
391                 return 0;
392         /* Prefetch first packet cacheline. */
393         tx_prefetch_cqe(txq, txq->cq_ci);
394         tx_prefetch_cqe(txq, txq->cq_ci + 1);
395         rte_prefetch0(*pkts);
396         /* Start processing. */
397         txq_complete(txq);
398         max = (elts_n - (elts_head - txq->elts_tail));
399         if (max > elts_n)
400                 max -= elts_n;
401         do {
402                 volatile struct mlx5_wqe_data_seg *dseg = NULL;
403                 uint32_t length;
404                 unsigned int ds = 0;
405                 uintptr_t addr;
406 #ifdef MLX5_PMD_SOFT_COUNTERS
407                 uint32_t total_length = 0;
408 #endif
409
410                 /* first_seg */
411                 buf = *(pkts++);
412                 segs_n = buf->nb_segs;
413                 /*
414                  * Make sure there is enough room to store this packet and
415                  * that one ring entry remains unused.
416                  */
417                 assert(segs_n);
418                 if (max < segs_n + 1)
419                         break;
420                 max -= segs_n;
421                 --segs_n;
422                 if (!segs_n)
423                         --pkts_n;
424                 wqe = &(*txq->wqes)[txq->wqe_ci &
425                                     ((1 << txq->wqe_n) - 1)].hdr;
426                 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
427                 if (pkts_n > 1)
428                         rte_prefetch0(*pkts);
429                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
430                 length = DATA_LEN(buf);
431 #ifdef MLX5_PMD_SOFT_COUNTERS
432                 total_length = length;
433 #endif
434                 assert(length >= MLX5_WQE_DWORD_SIZE);
435                 /* Update element. */
436                 (*txq->elts)[elts_head] = buf;
437                 elts_head = (elts_head + 1) & (elts_n - 1);
438                 /* Prefetch next buffer data. */
439                 if (pkts_n > 1) {
440                         volatile void *pkt_addr;
441
442                         pkt_addr = rte_pktmbuf_mtod(*pkts, volatile void *);
443                         rte_prefetch0(pkt_addr);
444                 }
445                 /* Should we enable HW CKSUM offload */
446                 if (buf->ol_flags &
447                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
448                         wqe->eseg.cs_flags =
449                                 MLX5_ETH_WQE_L3_CSUM |
450                                 MLX5_ETH_WQE_L4_CSUM;
451                 } else {
452                         wqe->eseg.cs_flags = 0;
453                 }
454                 raw  = (uint8_t *)(uintptr_t)&wqe->eseg.inline_hdr[0];
455                 /* Start the know and common part of the WQE structure. */
456                 wqe->ctrl[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
457                 wqe->ctrl[2] = 0;
458                 wqe->ctrl[3] = 0;
459                 wqe->eseg.rsvd0 = 0;
460                 wqe->eseg.rsvd1 = 0;
461                 wqe->eseg.mss = 0;
462                 wqe->eseg.rsvd2 = 0;
463                 /* Start by copying the Ethernet Header. */
464                 memcpy((uint8_t *)raw, ((uint8_t *)addr), 16);
465                 length -= MLX5_WQE_DWORD_SIZE;
466                 addr += MLX5_WQE_DWORD_SIZE;
467                 /* Replace the Ethernet type by the VLAN if necessary. */
468                 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
469                         uint32_t vlan = htonl(0x81000000 | buf->vlan_tci);
470
471                         memcpy((uint8_t *)(raw + MLX5_WQE_DWORD_SIZE -
472                                            sizeof(vlan)),
473                                &vlan, sizeof(vlan));
474                         addr -= sizeof(vlan);
475                         length += sizeof(vlan);
476                 }
477                 /* Inline if enough room. */
478                 if (txq->max_inline != 0) {
479                         uintptr_t end =
480                                 (uintptr_t)&(*txq->wqes)[1 << txq->wqe_n];
481                         uint16_t max_inline =
482                                 txq->max_inline * RTE_CACHE_LINE_SIZE;
483                         uint16_t pkt_inline_sz = MLX5_WQE_DWORD_SIZE;
484                         uint16_t room;
485
486                         raw += MLX5_WQE_DWORD_SIZE;
487                         room = end - (uintptr_t)raw;
488                         if (room > max_inline) {
489                                 uintptr_t addr_end = (addr + max_inline) &
490                                         ~(RTE_CACHE_LINE_SIZE - 1);
491                                 uint16_t copy_b = ((addr_end - addr) > length) ?
492                                                   length :
493                                                   (addr_end - addr);
494
495                                 rte_memcpy((void *)raw, (void *)addr, copy_b);
496                                 addr += copy_b;
497                                 length -= copy_b;
498                                 pkt_inline_sz += copy_b;
499                                 /* Sanity check. */
500                                 assert(addr <= addr_end);
501                         }
502                         /* Store the inlined packet size in the WQE. */
503                         wqe->eseg.inline_hdr_sz = htons(pkt_inline_sz);
504                         /*
505                          * 2 DWORDs consumed by the WQE header + 1 DSEG +
506                          * the size of the inline part of the packet.
507                          */
508                         ds = 2 + MLX5_WQE_DS(pkt_inline_sz - 2);
509                         if (length > 0) {
510                                 dseg = (struct mlx5_wqe_data_seg *)
511                                         ((uintptr_t)wqe +
512                                          (ds * MLX5_WQE_DWORD_SIZE));
513                                 if ((uintptr_t)dseg >= end)
514                                         dseg = (struct mlx5_wqe_data_seg *)
515                                                 ((uintptr_t)&(*txq->wqes)[0]);
516                                 goto use_dseg;
517                         } else if (!segs_n) {
518                                 goto next_pkt;
519                         } else {
520                                 goto next_seg;
521                         }
522                 } else {
523                         /*
524                          * No inline has been done in the packet, only the
525                          * Ethernet Header as been stored.
526                          */
527                         wqe->eseg.inline_hdr_sz = htons(MLX5_WQE_DWORD_SIZE);
528                         dseg = (struct mlx5_wqe_data_seg *)
529                                 ((uintptr_t)wqe + (3 * MLX5_WQE_DWORD_SIZE));
530                         ds = 3;
531 use_dseg:
532                         /* Add the remaining packet as a simple ds. */
533                         *dseg = (struct mlx5_wqe_data_seg) {
534                                 .addr = htonll(addr),
535                                 .byte_count = htonl(length),
536                                 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
537                         };
538                         ++ds;
539                         if (!segs_n)
540                                 goto next_pkt;
541                 }
542 next_seg:
543                 assert(buf);
544                 assert(ds);
545                 assert(wqe);
546                 /*
547                  * Spill on next WQE when the current one does not have
548                  * enough room left. Size of WQE must a be a multiple
549                  * of data segment size.
550                  */
551                 assert(!(MLX5_WQE_SIZE % MLX5_WQE_DWORD_SIZE));
552                 if (!(ds % (MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE))) {
553                         unsigned int n = (txq->wqe_ci + ((ds + 3) / 4)) &
554                                 ((1 << txq->wqe_n) - 1);
555
556                         dseg = (struct mlx5_wqe_data_seg *)
557                                 ((uintptr_t)&(*txq->wqes)[n]);
558                         tx_prefetch_wqe(txq, n + 1);
559                 } else {
560                         ++dseg;
561                 }
562                 ++ds;
563                 buf = buf->next;
564                 assert(buf);
565                 length = DATA_LEN(buf);
566 #ifdef MLX5_PMD_SOFT_COUNTERS
567                 total_length += length;
568 #endif
569                 /* Store segment information. */
570                 *dseg = (struct mlx5_wqe_data_seg) {
571                         .addr = htonll(rte_pktmbuf_mtod(buf, uintptr_t)),
572                         .byte_count = htonl(length),
573                         .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
574                 };
575                 (*txq->elts)[elts_head] = buf;
576                 elts_head = (elts_head + 1) & (elts_n - 1);
577                 ++j;
578                 --segs_n;
579                 if (segs_n)
580                         goto next_seg;
581                 else
582                         --pkts_n;
583 next_pkt:
584                 ++i;
585                 wqe->ctrl[1] = htonl(txq->qp_num_8s | ds);
586                 txq->wqe_ci += (ds + 3) / 4;
587 #ifdef MLX5_PMD_SOFT_COUNTERS
588                 /* Increment sent bytes counter. */
589                 txq->stats.obytes += total_length;
590 #endif
591         } while (pkts_n);
592         /* Take a shortcut if nothing must be sent. */
593         if (unlikely(i == 0))
594                 return 0;
595         /* Check whether completion threshold has been reached. */
596         comp = txq->elts_comp + i + j;
597         if (comp >= MLX5_TX_COMP_THRESH) {
598                 /* Request completion on last WQE. */
599                 wqe->ctrl[2] = htonl(8);
600                 /* Save elts_head in unused "immediate" field of WQE. */
601                 wqe->ctrl[3] = elts_head;
602                 txq->elts_comp = 0;
603         } else {
604                 txq->elts_comp = comp;
605         }
606 #ifdef MLX5_PMD_SOFT_COUNTERS
607         /* Increment sent packets counter. */
608         txq->stats.opackets += i;
609 #endif
610         /* Ring QP doorbell. */
611         mlx5_tx_dbrec(txq, (volatile struct mlx5_wqe *)wqe);
612         txq->elts_head = elts_head;
613         return i;
614 }
615
616 /**
617  * Open a MPW session.
618  *
619  * @param txq
620  *   Pointer to TX queue structure.
621  * @param mpw
622  *   Pointer to MPW session structure.
623  * @param length
624  *   Packet length.
625  */
626 static inline void
627 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
628 {
629         uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
630         volatile struct mlx5_wqe_data_seg (*dseg)[MLX5_MPW_DSEG_MAX] =
631                 (volatile struct mlx5_wqe_data_seg (*)[])
632                 (uintptr_t)&(*txq->wqes)[(idx + 1) & ((1 << txq->wqe_n) - 1)];
633
634         mpw->state = MLX5_MPW_STATE_OPENED;
635         mpw->pkts_n = 0;
636         mpw->len = length;
637         mpw->total_len = 0;
638         mpw->wqe = (volatile struct mlx5_wqe *)&(*txq->wqes)[idx].hdr;
639         mpw->wqe->eseg.mss = htons(length);
640         mpw->wqe->eseg.inline_hdr_sz = 0;
641         mpw->wqe->eseg.rsvd0 = 0;
642         mpw->wqe->eseg.rsvd1 = 0;
643         mpw->wqe->eseg.rsvd2 = 0;
644         mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
645                                   (txq->wqe_ci << 8) | MLX5_OPCODE_TSO);
646         mpw->wqe->ctrl[2] = 0;
647         mpw->wqe->ctrl[3] = 0;
648         mpw->data.dseg[0] = (volatile struct mlx5_wqe_data_seg *)
649                 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
650         mpw->data.dseg[1] = (volatile struct mlx5_wqe_data_seg *)
651                 (((uintptr_t)mpw->wqe) + (3 * MLX5_WQE_DWORD_SIZE));
652         mpw->data.dseg[2] = &(*dseg)[0];
653         mpw->data.dseg[3] = &(*dseg)[1];
654         mpw->data.dseg[4] = &(*dseg)[2];
655 }
656
657 /**
658  * Close a MPW session.
659  *
660  * @param txq
661  *   Pointer to TX queue structure.
662  * @param mpw
663  *   Pointer to MPW session structure.
664  */
665 static inline void
666 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
667 {
668         unsigned int num = mpw->pkts_n;
669
670         /*
671          * Store size in multiple of 16 bytes. Control and Ethernet segments
672          * count as 2.
673          */
674         mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | (2 + num));
675         mpw->state = MLX5_MPW_STATE_CLOSED;
676         if (num < 3)
677                 ++txq->wqe_ci;
678         else
679                 txq->wqe_ci += 2;
680         tx_prefetch_wqe(txq, txq->wqe_ci);
681         tx_prefetch_wqe(txq, txq->wqe_ci + 1);
682 }
683
684 /**
685  * DPDK callback for TX with MPW support.
686  *
687  * @param dpdk_txq
688  *   Generic pointer to TX queue structure.
689  * @param[in] pkts
690  *   Packets to transmit.
691  * @param pkts_n
692  *   Number of packets in array.
693  *
694  * @return
695  *   Number of packets successfully transmitted (<= pkts_n).
696  */
697 uint16_t
698 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
699 {
700         struct txq *txq = (struct txq *)dpdk_txq;
701         uint16_t elts_head = txq->elts_head;
702         const unsigned int elts_n = 1 << txq->elts_n;
703         unsigned int i = 0;
704         unsigned int j = 0;
705         unsigned int max;
706         unsigned int comp;
707         struct mlx5_mpw mpw = {
708                 .state = MLX5_MPW_STATE_CLOSED,
709         };
710
711         if (unlikely(!pkts_n))
712                 return 0;
713         /* Prefetch first packet cacheline. */
714         tx_prefetch_cqe(txq, txq->cq_ci);
715         tx_prefetch_wqe(txq, txq->wqe_ci);
716         tx_prefetch_wqe(txq, txq->wqe_ci + 1);
717         /* Start processing. */
718         txq_complete(txq);
719         max = (elts_n - (elts_head - txq->elts_tail));
720         if (max > elts_n)
721                 max -= elts_n;
722         do {
723                 struct rte_mbuf *buf = *(pkts++);
724                 unsigned int elts_head_next;
725                 uint32_t length;
726                 unsigned int segs_n = buf->nb_segs;
727                 uint32_t cs_flags = 0;
728
729                 /*
730                  * Make sure there is enough room to store this packet and
731                  * that one ring entry remains unused.
732                  */
733                 assert(segs_n);
734                 if (max < segs_n + 1)
735                         break;
736                 /* Do not bother with large packets MPW cannot handle. */
737                 if (segs_n > MLX5_MPW_DSEG_MAX)
738                         break;
739                 max -= segs_n;
740                 --pkts_n;
741                 /* Should we enable HW CKSUM offload */
742                 if (buf->ol_flags &
743                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
744                         cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
745                 /* Retrieve packet information. */
746                 length = PKT_LEN(buf);
747                 assert(length);
748                 /* Start new session if packet differs. */
749                 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
750                     ((mpw.len != length) ||
751                      (segs_n != 1) ||
752                      (mpw.wqe->eseg.cs_flags != cs_flags)))
753                         mlx5_mpw_close(txq, &mpw);
754                 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
755                         mlx5_mpw_new(txq, &mpw, length);
756                         mpw.wqe->eseg.cs_flags = cs_flags;
757                 }
758                 /* Multi-segment packets must be alone in their MPW. */
759                 assert((segs_n == 1) || (mpw.pkts_n == 0));
760 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
761                 length = 0;
762 #endif
763                 do {
764                         volatile struct mlx5_wqe_data_seg *dseg;
765                         uintptr_t addr;
766
767                         elts_head_next = (elts_head + 1) & (elts_n - 1);
768                         assert(buf);
769                         (*txq->elts)[elts_head] = buf;
770                         dseg = mpw.data.dseg[mpw.pkts_n];
771                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
772                         *dseg = (struct mlx5_wqe_data_seg){
773                                 .byte_count = htonl(DATA_LEN(buf)),
774                                 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
775                                 .addr = htonll(addr),
776                         };
777                         elts_head = elts_head_next;
778 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
779                         length += DATA_LEN(buf);
780 #endif
781                         buf = buf->next;
782                         ++mpw.pkts_n;
783                         ++j;
784                 } while (--segs_n);
785                 assert(length == mpw.len);
786                 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
787                         mlx5_mpw_close(txq, &mpw);
788                 elts_head = elts_head_next;
789 #ifdef MLX5_PMD_SOFT_COUNTERS
790                 /* Increment sent bytes counter. */
791                 txq->stats.obytes += length;
792 #endif
793                 ++i;
794         } while (pkts_n);
795         /* Take a shortcut if nothing must be sent. */
796         if (unlikely(i == 0))
797                 return 0;
798         /* Check whether completion threshold has been reached. */
799         /* "j" includes both packets and segments. */
800         comp = txq->elts_comp + j;
801         if (comp >= MLX5_TX_COMP_THRESH) {
802                 volatile struct mlx5_wqe *wqe = mpw.wqe;
803
804                 /* Request completion on last WQE. */
805                 wqe->ctrl[2] = htonl(8);
806                 /* Save elts_head in unused "immediate" field of WQE. */
807                 wqe->ctrl[3] = elts_head;
808                 txq->elts_comp = 0;
809         } else {
810                 txq->elts_comp = comp;
811         }
812 #ifdef MLX5_PMD_SOFT_COUNTERS
813         /* Increment sent packets counter. */
814         txq->stats.opackets += i;
815 #endif
816         /* Ring QP doorbell. */
817         if (mpw.state == MLX5_MPW_STATE_OPENED)
818                 mlx5_mpw_close(txq, &mpw);
819         mlx5_tx_dbrec(txq, mpw.wqe);
820         txq->elts_head = elts_head;
821         return i;
822 }
823
824 /**
825  * Open a MPW inline session.
826  *
827  * @param txq
828  *   Pointer to TX queue structure.
829  * @param mpw
830  *   Pointer to MPW session structure.
831  * @param length
832  *   Packet length.
833  */
834 static inline void
835 mlx5_mpw_inline_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
836 {
837         uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
838         struct mlx5_wqe_inl_small *inl;
839
840         mpw->state = MLX5_MPW_INL_STATE_OPENED;
841         mpw->pkts_n = 0;
842         mpw->len = length;
843         mpw->total_len = 0;
844         mpw->wqe = (volatile struct mlx5_wqe *)&(*txq->wqes)[idx].hdr;
845         mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
846                                   (txq->wqe_ci << 8) |
847                                   MLX5_OPCODE_TSO);
848         mpw->wqe->ctrl[2] = 0;
849         mpw->wqe->ctrl[3] = 0;
850         mpw->wqe->eseg.mss = htons(length);
851         mpw->wqe->eseg.inline_hdr_sz = 0;
852         mpw->wqe->eseg.cs_flags = 0;
853         mpw->wqe->eseg.rsvd0 = 0;
854         mpw->wqe->eseg.rsvd1 = 0;
855         mpw->wqe->eseg.rsvd2 = 0;
856         inl = (struct mlx5_wqe_inl_small *)
857                 (((uintptr_t)mpw->wqe) + 2 * MLX5_WQE_DWORD_SIZE);
858         mpw->data.raw = (uint8_t *)&inl->raw;
859 }
860
861 /**
862  * Close a MPW inline session.
863  *
864  * @param txq
865  *   Pointer to TX queue structure.
866  * @param mpw
867  *   Pointer to MPW session structure.
868  */
869 static inline void
870 mlx5_mpw_inline_close(struct txq *txq, struct mlx5_mpw *mpw)
871 {
872         unsigned int size;
873         struct mlx5_wqe_inl_small *inl = (struct mlx5_wqe_inl_small *)
874                 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
875
876         size = MLX5_WQE_SIZE - MLX5_MWQE64_INL_DATA + mpw->total_len;
877         /*
878          * Store size in multiple of 16 bytes. Control and Ethernet segments
879          * count as 2.
880          */
881         mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(size));
882         mpw->state = MLX5_MPW_STATE_CLOSED;
883         inl->byte_cnt = htonl(mpw->total_len | MLX5_INLINE_SEG);
884         txq->wqe_ci += (size + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
885 }
886
887 /**
888  * DPDK callback for TX with MPW inline support.
889  *
890  * @param dpdk_txq
891  *   Generic pointer to TX queue structure.
892  * @param[in] pkts
893  *   Packets to transmit.
894  * @param pkts_n
895  *   Number of packets in array.
896  *
897  * @return
898  *   Number of packets successfully transmitted (<= pkts_n).
899  */
900 uint16_t
901 mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
902                          uint16_t pkts_n)
903 {
904         struct txq *txq = (struct txq *)dpdk_txq;
905         uint16_t elts_head = txq->elts_head;
906         const unsigned int elts_n = 1 << txq->elts_n;
907         unsigned int i = 0;
908         unsigned int j = 0;
909         unsigned int max;
910         unsigned int comp;
911         unsigned int inline_room = txq->max_inline * RTE_CACHE_LINE_SIZE;
912         struct mlx5_mpw mpw = {
913                 .state = MLX5_MPW_STATE_CLOSED,
914         };
915
916         if (unlikely(!pkts_n))
917                 return 0;
918         /* Prefetch first packet cacheline. */
919         tx_prefetch_cqe(txq, txq->cq_ci);
920         tx_prefetch_wqe(txq, txq->wqe_ci);
921         tx_prefetch_wqe(txq, txq->wqe_ci + 1);
922         /* Start processing. */
923         txq_complete(txq);
924         max = (elts_n - (elts_head - txq->elts_tail));
925         if (max > elts_n)
926                 max -= elts_n;
927         do {
928                 struct rte_mbuf *buf = *(pkts++);
929                 unsigned int elts_head_next;
930                 uintptr_t addr;
931                 uint32_t length;
932                 unsigned int segs_n = buf->nb_segs;
933                 uint32_t cs_flags = 0;
934
935                 /*
936                  * Make sure there is enough room to store this packet and
937                  * that one ring entry remains unused.
938                  */
939                 assert(segs_n);
940                 if (max < segs_n + 1)
941                         break;
942                 /* Do not bother with large packets MPW cannot handle. */
943                 if (segs_n > MLX5_MPW_DSEG_MAX)
944                         break;
945                 max -= segs_n;
946                 --pkts_n;
947                 /* Should we enable HW CKSUM offload */
948                 if (buf->ol_flags &
949                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
950                         cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
951                 /* Retrieve packet information. */
952                 length = PKT_LEN(buf);
953                 /* Start new session if packet differs. */
954                 if (mpw.state == MLX5_MPW_STATE_OPENED) {
955                         if ((mpw.len != length) ||
956                             (segs_n != 1) ||
957                             (mpw.wqe->eseg.cs_flags != cs_flags))
958                                 mlx5_mpw_close(txq, &mpw);
959                 } else if (mpw.state == MLX5_MPW_INL_STATE_OPENED) {
960                         if ((mpw.len != length) ||
961                             (segs_n != 1) ||
962                             (length > inline_room) ||
963                             (mpw.wqe->eseg.cs_flags != cs_flags)) {
964                                 mlx5_mpw_inline_close(txq, &mpw);
965                                 inline_room =
966                                         txq->max_inline * RTE_CACHE_LINE_SIZE;
967                         }
968                 }
969                 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
970                         if ((segs_n != 1) ||
971                             (length > inline_room)) {
972                                 mlx5_mpw_new(txq, &mpw, length);
973                                 mpw.wqe->eseg.cs_flags = cs_flags;
974                         } else {
975                                 mlx5_mpw_inline_new(txq, &mpw, length);
976                                 mpw.wqe->eseg.cs_flags = cs_flags;
977                         }
978                 }
979                 /* Multi-segment packets must be alone in their MPW. */
980                 assert((segs_n == 1) || (mpw.pkts_n == 0));
981                 if (mpw.state == MLX5_MPW_STATE_OPENED) {
982                         assert(inline_room ==
983                                txq->max_inline * RTE_CACHE_LINE_SIZE);
984 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
985                         length = 0;
986 #endif
987                         do {
988                                 volatile struct mlx5_wqe_data_seg *dseg;
989
990                                 elts_head_next =
991                                         (elts_head + 1) & (elts_n - 1);
992                                 assert(buf);
993                                 (*txq->elts)[elts_head] = buf;
994                                 dseg = mpw.data.dseg[mpw.pkts_n];
995                                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
996                                 *dseg = (struct mlx5_wqe_data_seg){
997                                         .byte_count = htonl(DATA_LEN(buf)),
998                                         .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
999                                         .addr = htonll(addr),
1000                                 };
1001                                 elts_head = elts_head_next;
1002 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1003                                 length += DATA_LEN(buf);
1004 #endif
1005                                 buf = buf->next;
1006                                 ++mpw.pkts_n;
1007                                 ++j;
1008                         } while (--segs_n);
1009                         assert(length == mpw.len);
1010                         if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1011                                 mlx5_mpw_close(txq, &mpw);
1012                 } else {
1013                         unsigned int max;
1014
1015                         assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
1016                         assert(length <= inline_room);
1017                         assert(length == DATA_LEN(buf));
1018                         elts_head_next = (elts_head + 1) & (elts_n - 1);
1019                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
1020                         (*txq->elts)[elts_head] = buf;
1021                         /* Maximum number of bytes before wrapping. */
1022                         max = ((uintptr_t)&(*txq->wqes)[1 << txq->wqe_n] -
1023                                (uintptr_t)mpw.data.raw);
1024                         if (length > max) {
1025                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1026                                            (void *)addr,
1027                                            max);
1028                                 mpw.data.raw =
1029                                         (volatile void *)&(*txq->wqes)[0];
1030                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1031                                            (void *)(addr + max),
1032                                            length - max);
1033                                 mpw.data.raw += length - max;
1034                         } else {
1035                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1036                                            (void *)addr,
1037                                            length);
1038                                 mpw.data.raw += length;
1039                         }
1040                         if ((uintptr_t)mpw.data.raw ==
1041                             (uintptr_t)&(*txq->wqes)[1 << txq->wqe_n])
1042                                 mpw.data.raw =
1043                                         (volatile void *)&(*txq->wqes)[0];
1044                         ++mpw.pkts_n;
1045                         mpw.total_len += length;
1046                         ++j;
1047                         if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1048                                 mlx5_mpw_inline_close(txq, &mpw);
1049                                 inline_room =
1050                                         txq->max_inline * RTE_CACHE_LINE_SIZE;
1051                         } else {
1052                                 inline_room -= length;
1053                         }
1054                 }
1055                 elts_head = elts_head_next;
1056 #ifdef MLX5_PMD_SOFT_COUNTERS
1057                 /* Increment sent bytes counter. */
1058                 txq->stats.obytes += length;
1059 #endif
1060                 ++i;
1061         } while (pkts_n);
1062         /* Take a shortcut if nothing must be sent. */
1063         if (unlikely(i == 0))
1064                 return 0;
1065         /* Check whether completion threshold has been reached. */
1066         /* "j" includes both packets and segments. */
1067         comp = txq->elts_comp + j;
1068         if (comp >= MLX5_TX_COMP_THRESH) {
1069                 volatile struct mlx5_wqe *wqe = mpw.wqe;
1070
1071                 /* Request completion on last WQE. */
1072                 wqe->ctrl[2] = htonl(8);
1073                 /* Save elts_head in unused "immediate" field of WQE. */
1074                 wqe->ctrl[3] = elts_head;
1075                 txq->elts_comp = 0;
1076         } else {
1077                 txq->elts_comp = comp;
1078         }
1079 #ifdef MLX5_PMD_SOFT_COUNTERS
1080         /* Increment sent packets counter. */
1081         txq->stats.opackets += i;
1082 #endif
1083         /* Ring QP doorbell. */
1084         if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1085                 mlx5_mpw_inline_close(txq, &mpw);
1086         else if (mpw.state == MLX5_MPW_STATE_OPENED)
1087                 mlx5_mpw_close(txq, &mpw);
1088         mlx5_tx_dbrec(txq, mpw.wqe);
1089         txq->elts_head = elts_head;
1090         return i;
1091 }
1092
1093 /**
1094  * Translate RX completion flags to packet type.
1095  *
1096  * @param[in] cqe
1097  *   Pointer to CQE.
1098  *
1099  * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1100  *
1101  * @return
1102  *   Packet type for struct rte_mbuf.
1103  */
1104 static inline uint32_t
1105 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe)
1106 {
1107         uint32_t pkt_type;
1108         uint16_t flags = ntohs(cqe->hdr_type_etc);
1109
1110         if (cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) {
1111                 pkt_type =
1112                         TRANSPOSE(flags,
1113                                   MLX5_CQE_RX_IPV4_PACKET,
1114                                   RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
1115                         TRANSPOSE(flags,
1116                                   MLX5_CQE_RX_IPV6_PACKET,
1117                                   RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
1118                 pkt_type |= ((cqe->pkt_info & MLX5_CQE_RX_OUTER_PACKET) ?
1119                              RTE_PTYPE_L3_IPV6_EXT_UNKNOWN :
1120                              RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
1121         } else {
1122                 pkt_type =
1123                         TRANSPOSE(flags,
1124                                   MLX5_CQE_L3_HDR_TYPE_IPV6,
1125                                   RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
1126                         TRANSPOSE(flags,
1127                                   MLX5_CQE_L3_HDR_TYPE_IPV4,
1128                                   RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
1129         }
1130         return pkt_type;
1131 }
1132
1133 /**
1134  * Get size of the next packet for a given CQE. For compressed CQEs, the
1135  * consumer index is updated only once all packets of the current one have
1136  * been processed.
1137  *
1138  * @param rxq
1139  *   Pointer to RX queue.
1140  * @param cqe
1141  *   CQE to process.
1142  * @param[out] rss_hash
1143  *   Packet RSS Hash result.
1144  *
1145  * @return
1146  *   Packet size in bytes (0 if there is none), -1 in case of completion
1147  *   with error.
1148  */
1149 static inline int
1150 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
1151                  uint16_t cqe_cnt, uint32_t *rss_hash)
1152 {
1153         struct rxq_zip *zip = &rxq->zip;
1154         uint16_t cqe_n = cqe_cnt + 1;
1155         int len = 0;
1156
1157         /* Process compressed data in the CQE and mini arrays. */
1158         if (zip->ai) {
1159                 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1160                         (volatile struct mlx5_mini_cqe8 (*)[8])
1161                         (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt]);
1162
1163                 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1164                 *rss_hash = ntohl((*mc)[zip->ai & 7].rx_hash_result);
1165                 if ((++zip->ai & 7) == 0) {
1166                         /*
1167                          * Increment consumer index to skip the number of
1168                          * CQEs consumed. Hardware leaves holes in the CQ
1169                          * ring for software use.
1170                          */
1171                         zip->ca = zip->na;
1172                         zip->na += 8;
1173                 }
1174                 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1175                         uint16_t idx = rxq->cq_ci;
1176                         uint16_t end = zip->cq_ci;
1177
1178                         while (idx != end) {
1179                                 (*rxq->cqes)[idx & cqe_cnt].op_own =
1180                                         MLX5_CQE_INVALIDATE;
1181                                 ++idx;
1182                         }
1183                         rxq->cq_ci = zip->cq_ci;
1184                         zip->ai = 0;
1185                 }
1186         /* No compressed data, get next CQE and verify if it is compressed. */
1187         } else {
1188                 int ret;
1189                 int8_t op_own;
1190
1191                 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
1192                 if (unlikely(ret == 1))
1193                         return 0;
1194                 ++rxq->cq_ci;
1195                 op_own = cqe->op_own;
1196                 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1197                         volatile struct mlx5_mini_cqe8 (*mc)[8] =
1198                                 (volatile struct mlx5_mini_cqe8 (*)[8])
1199                                 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1200                                                           cqe_cnt]);
1201
1202                         /* Fix endianness. */
1203                         zip->cqe_cnt = ntohl(cqe->byte_cnt);
1204                         /*
1205                          * Current mini array position is the one returned by
1206                          * check_cqe64().
1207                          *
1208                          * If completion comprises several mini arrays, as a
1209                          * special case the second one is located 7 CQEs after
1210                          * the initial CQE instead of 8 for subsequent ones.
1211                          */
1212                         zip->ca = rxq->cq_ci & cqe_cnt;
1213                         zip->na = zip->ca + 7;
1214                         /* Compute the next non compressed CQE. */
1215                         --rxq->cq_ci;
1216                         zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1217                         /* Get packet size to return. */
1218                         len = ntohl((*mc)[0].byte_cnt);
1219                         *rss_hash = ntohl((*mc)[0].rx_hash_result);
1220                         zip->ai = 1;
1221                 } else {
1222                         len = ntohl(cqe->byte_cnt);
1223                         *rss_hash = ntohl(cqe->rx_hash_res);
1224                 }
1225                 /* Error while receiving packet. */
1226                 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1227                         return -1;
1228         }
1229         return len;
1230 }
1231
1232 /**
1233  * Translate RX completion flags to offload flags.
1234  *
1235  * @param[in] rxq
1236  *   Pointer to RX queue structure.
1237  * @param[in] cqe
1238  *   Pointer to CQE.
1239  *
1240  * @return
1241  *   Offload flags (ol_flags) for struct rte_mbuf.
1242  */
1243 static inline uint32_t
1244 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe)
1245 {
1246         uint32_t ol_flags = 0;
1247         uint16_t flags = ntohs(cqe->hdr_type_etc);
1248
1249         ol_flags =
1250                 TRANSPOSE(flags,
1251                           MLX5_CQE_RX_L3_HDR_VALID,
1252                           PKT_RX_IP_CKSUM_GOOD) |
1253                 TRANSPOSE(flags,
1254                           MLX5_CQE_RX_L4_HDR_VALID,
1255                           PKT_RX_L4_CKSUM_GOOD);
1256         if ((cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1257                 ol_flags |=
1258                         TRANSPOSE(flags,
1259                                   MLX5_CQE_RX_L3_HDR_VALID,
1260                                   PKT_RX_IP_CKSUM_GOOD) |
1261                         TRANSPOSE(flags,
1262                                   MLX5_CQE_RX_L4_HDR_VALID,
1263                                   PKT_RX_L4_CKSUM_GOOD);
1264         return ol_flags;
1265 }
1266
1267 /**
1268  * DPDK callback for RX.
1269  *
1270  * @param dpdk_rxq
1271  *   Generic pointer to RX queue structure.
1272  * @param[out] pkts
1273  *   Array to store received packets.
1274  * @param pkts_n
1275  *   Maximum number of packets in array.
1276  *
1277  * @return
1278  *   Number of packets successfully received (<= pkts_n).
1279  */
1280 uint16_t
1281 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1282 {
1283         struct rxq *rxq = dpdk_rxq;
1284         const unsigned int wqe_cnt = (1 << rxq->elts_n) - 1;
1285         const unsigned int cqe_cnt = (1 << rxq->cqe_n) - 1;
1286         const unsigned int sges_n = rxq->sges_n;
1287         struct rte_mbuf *pkt = NULL;
1288         struct rte_mbuf *seg = NULL;
1289         volatile struct mlx5_cqe *cqe =
1290                 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1291         unsigned int i = 0;
1292         unsigned int rq_ci = rxq->rq_ci << sges_n;
1293         int len; /* keep its value across iterations. */
1294
1295         while (pkts_n) {
1296                 unsigned int idx = rq_ci & wqe_cnt;
1297                 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1298                 struct rte_mbuf *rep = (*rxq->elts)[idx];
1299                 uint32_t rss_hash_res = 0;
1300
1301                 if (pkt)
1302                         NEXT(seg) = rep;
1303                 seg = rep;
1304                 rte_prefetch0(seg);
1305                 rte_prefetch0(cqe);
1306                 rte_prefetch0(wqe);
1307                 rep = rte_mbuf_raw_alloc(rxq->mp);
1308                 if (unlikely(rep == NULL)) {
1309                         ++rxq->stats.rx_nombuf;
1310                         if (!pkt) {
1311                                 /*
1312                                  * no buffers before we even started,
1313                                  * bail out silently.
1314                                  */
1315                                 break;
1316                         }
1317                         while (pkt != seg) {
1318                                 assert(pkt != (*rxq->elts)[idx]);
1319                                 rep = NEXT(pkt);
1320                                 rte_mbuf_refcnt_set(pkt, 0);
1321                                 __rte_mbuf_raw_free(pkt);
1322                                 pkt = rep;
1323                         }
1324                         break;
1325                 }
1326                 if (!pkt) {
1327                         cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1328                         len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt,
1329                                                &rss_hash_res);
1330                         if (!len) {
1331                                 rte_mbuf_refcnt_set(rep, 0);
1332                                 __rte_mbuf_raw_free(rep);
1333                                 break;
1334                         }
1335                         if (unlikely(len == -1)) {
1336                                 /* RX error, packet is likely too large. */
1337                                 rte_mbuf_refcnt_set(rep, 0);
1338                                 __rte_mbuf_raw_free(rep);
1339                                 ++rxq->stats.idropped;
1340                                 goto skip;
1341                         }
1342                         pkt = seg;
1343                         assert(len >= (rxq->crc_present << 2));
1344                         /* Update packet information. */
1345                         pkt->packet_type = 0;
1346                         pkt->ol_flags = 0;
1347                         if (rss_hash_res && rxq->rss_hash) {
1348                                 pkt->hash.rss = rss_hash_res;
1349                                 pkt->ol_flags = PKT_RX_RSS_HASH;
1350                         }
1351                         if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip |
1352                             rxq->crc_present) {
1353                                 if (rxq->csum) {
1354                                         pkt->packet_type =
1355                                                 rxq_cq_to_pkt_type(cqe);
1356                                         pkt->ol_flags |=
1357                                                 rxq_cq_to_ol_flags(rxq, cqe);
1358                                 }
1359                                 if (cqe->hdr_type_etc &
1360                                     MLX5_CQE_VLAN_STRIPPED) {
1361                                         pkt->ol_flags |= PKT_RX_VLAN_PKT |
1362                                                 PKT_RX_VLAN_STRIPPED;
1363                                         pkt->vlan_tci = ntohs(cqe->vlan_info);
1364                                 }
1365                                 if (rxq->crc_present)
1366                                         len -= ETHER_CRC_LEN;
1367                         }
1368                         PKT_LEN(pkt) = len;
1369                 }
1370                 DATA_LEN(rep) = DATA_LEN(seg);
1371                 PKT_LEN(rep) = PKT_LEN(seg);
1372                 SET_DATA_OFF(rep, DATA_OFF(seg));
1373                 NB_SEGS(rep) = NB_SEGS(seg);
1374                 PORT(rep) = PORT(seg);
1375                 NEXT(rep) = NULL;
1376                 (*rxq->elts)[idx] = rep;
1377                 /*
1378                  * Fill NIC descriptor with the new buffer.  The lkey and size
1379                  * of the buffers are already known, only the buffer address
1380                  * changes.
1381                  */
1382                 wqe->addr = htonll(rte_pktmbuf_mtod(rep, uintptr_t));
1383                 if (len > DATA_LEN(seg)) {
1384                         len -= DATA_LEN(seg);
1385                         ++NB_SEGS(pkt);
1386                         ++rq_ci;
1387                         continue;
1388                 }
1389                 DATA_LEN(seg) = len;
1390 #ifdef MLX5_PMD_SOFT_COUNTERS
1391                 /* Increment bytes counter. */
1392                 rxq->stats.ibytes += PKT_LEN(pkt);
1393 #endif
1394                 /* Return packet. */
1395                 *(pkts++) = pkt;
1396                 pkt = NULL;
1397                 --pkts_n;
1398                 ++i;
1399 skip:
1400                 /* Align consumer index to the next stride. */
1401                 rq_ci >>= sges_n;
1402                 ++rq_ci;
1403                 rq_ci <<= sges_n;
1404         }
1405         if (unlikely((i == 0) && ((rq_ci >> sges_n) == rxq->rq_ci)))
1406                 return 0;
1407         /* Update the consumer index. */
1408         rxq->rq_ci = rq_ci >> sges_n;
1409         rte_wmb();
1410         *rxq->cq_db = htonl(rxq->cq_ci);
1411         rte_wmb();
1412         *rxq->rq_db = htonl(rxq->rq_ci);
1413 #ifdef MLX5_PMD_SOFT_COUNTERS
1414         /* Increment packets counter. */
1415         rxq->stats.ipackets += i;
1416 #endif
1417         return i;
1418 }
1419
1420 /**
1421  * Dummy DPDK callback for TX.
1422  *
1423  * This function is used to temporarily replace the real callback during
1424  * unsafe control operations on the queue, or in case of error.
1425  *
1426  * @param dpdk_txq
1427  *   Generic pointer to TX queue structure.
1428  * @param[in] pkts
1429  *   Packets to transmit.
1430  * @param pkts_n
1431  *   Number of packets in array.
1432  *
1433  * @return
1434  *   Number of packets successfully transmitted (<= pkts_n).
1435  */
1436 uint16_t
1437 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1438 {
1439         (void)dpdk_txq;
1440         (void)pkts;
1441         (void)pkts_n;
1442         return 0;
1443 }
1444
1445 /**
1446  * Dummy DPDK callback for RX.
1447  *
1448  * This function is used to temporarily replace the real callback during
1449  * unsafe control operations on the queue, or in case of error.
1450  *
1451  * @param dpdk_rxq
1452  *   Generic pointer to RX queue structure.
1453  * @param[out] pkts
1454  *   Array to store received packets.
1455  * @param pkts_n
1456  *   Maximum number of packets in array.
1457  *
1458  * @return
1459  *   Number of packets successfully received (<= pkts_n).
1460  */
1461 uint16_t
1462 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1463 {
1464         (void)dpdk_rxq;
1465         (void)pkts;
1466         (void)pkts_n;
1467         return 0;
1468 }