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