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