net/mlx5: remove Tx gather support
[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 "-pedantic"
43 #endif
44 #include <infiniband/verbs.h>
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic error "-pedantic"
47 #endif
48
49 /* DPDK headers don't like -pedantic. */
50 #ifdef PEDANTIC
51 #pragma GCC diagnostic ignored "-pedantic"
52 #endif
53 #include <rte_mbuf.h>
54 #include <rte_mempool.h>
55 #include <rte_prefetch.h>
56 #include <rte_common.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_memory.h>
59 #ifdef PEDANTIC
60 #pragma GCC diagnostic error "-pedantic"
61 #endif
62
63 #include "mlx5.h"
64 #include "mlx5_utils.h"
65 #include "mlx5_rxtx.h"
66 #include "mlx5_autoconf.h"
67 #include "mlx5_defs.h"
68
69 /**
70  * Manage TX completions.
71  *
72  * When sending a burst, mlx5_tx_burst() posts several WRs.
73  * To improve performance, a completion event is only required once every
74  * MLX5_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
75  * for other WRs, but this information would not be used anyway.
76  *
77  * @param txq
78  *   Pointer to TX queue structure.
79  *
80  * @return
81  *   0 on success, -1 on failure.
82  */
83 static int
84 txq_complete(struct txq *txq)
85 {
86         unsigned int elts_comp = txq->elts_comp;
87         unsigned int elts_tail = txq->elts_tail;
88         unsigned int elts_free = txq->elts_tail;
89         const unsigned int elts_n = txq->elts_n;
90         int wcs_n;
91
92         if (unlikely(elts_comp == 0))
93                 return 0;
94 #ifdef DEBUG_SEND
95         DEBUG("%p: processing %u work requests completions",
96               (void *)txq, elts_comp);
97 #endif
98         wcs_n = txq->poll_cnt(txq->cq, elts_comp);
99         if (unlikely(wcs_n == 0))
100                 return 0;
101         if (unlikely(wcs_n < 0)) {
102                 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
103                       (void *)txq, wcs_n);
104                 return -1;
105         }
106         elts_comp -= wcs_n;
107         assert(elts_comp <= txq->elts_comp);
108         /*
109          * Assume WC status is successful as nothing can be done about it
110          * anyway.
111          */
112         elts_tail += wcs_n * txq->elts_comp_cd_init;
113         if (elts_tail >= elts_n)
114                 elts_tail -= elts_n;
115
116         while (elts_free != elts_tail) {
117                 struct txq_elt *elt = &(*txq->elts)[elts_free];
118                 unsigned int elts_free_next =
119                         (((elts_free + 1) == elts_n) ? 0 : elts_free + 1);
120                 struct rte_mbuf *tmp = elt->buf;
121                 struct txq_elt *elt_next = &(*txq->elts)[elts_free_next];
122
123 #ifndef NDEBUG
124                 /* Poisoning. */
125                 memset(elt, 0x66, sizeof(*elt));
126 #endif
127                 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
128                 /* Faster than rte_pktmbuf_free(). */
129                 do {
130                         struct rte_mbuf *next = NEXT(tmp);
131
132                         rte_pktmbuf_free_seg(tmp);
133                         tmp = next;
134                 } while (tmp != NULL);
135                 elts_free = elts_free_next;
136         }
137
138         txq->elts_tail = elts_tail;
139         txq->elts_comp = elts_comp;
140         return 0;
141 }
142
143 /**
144  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
145  * the cloned mbuf is allocated is returned instead.
146  *
147  * @param buf
148  *   Pointer to mbuf.
149  *
150  * @return
151  *   Memory pool where data is located for given mbuf.
152  */
153 static struct rte_mempool *
154 txq_mb2mp(struct rte_mbuf *buf)
155 {
156         if (unlikely(RTE_MBUF_INDIRECT(buf)))
157                 return rte_mbuf_from_indirect(buf)->pool;
158         return buf->pool;
159 }
160
161 static inline uint32_t
162 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
163         __attribute__((always_inline));
164
165 /**
166  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
167  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
168  * remove an entry first.
169  *
170  * @param txq
171  *   Pointer to TX queue structure.
172  * @param[in] mp
173  *   Memory Pool for which a Memory Region lkey must be returned.
174  *
175  * @return
176  *   mr->lkey on success, (uint32_t)-1 on failure.
177  */
178 static inline uint32_t
179 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
180 {
181         unsigned int i;
182         uint32_t lkey = (uint32_t)-1;
183
184         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
185                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
186                         /* Unknown MP, add a new MR for it. */
187                         break;
188                 }
189                 if (txq->mp2mr[i].mp == mp) {
190                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
191                         assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
192                         lkey = txq->mp2mr[i].lkey;
193                         break;
194                 }
195         }
196         if (unlikely(lkey == (uint32_t)-1))
197                 lkey = txq_mp2mr_reg(txq, mp, i);
198         return lkey;
199 }
200
201 /**
202  * Insert VLAN using mbuf headroom space.
203  *
204  * @param buf
205  *   Buffer for VLAN insertion.
206  *
207  * @return
208  *   0 on success, errno value on failure.
209  */
210 static inline int
211 insert_vlan_sw(struct rte_mbuf *buf)
212 {
213         uintptr_t addr;
214         uint32_t vlan;
215         uint16_t head_room_len = rte_pktmbuf_headroom(buf);
216
217         if (head_room_len < 4)
218                 return EINVAL;
219
220         addr = rte_pktmbuf_mtod(buf, uintptr_t);
221         vlan = htonl(0x81000000 | buf->vlan_tci);
222         memmove((void *)(addr - 4), (void *)addr, 12);
223         memcpy((void *)(addr + 8), &vlan, sizeof(vlan));
224
225         SET_DATA_OFF(buf, head_room_len - 4);
226         DATA_LEN(buf) += 4;
227
228         return 0;
229 }
230
231 /**
232  * DPDK callback for TX.
233  *
234  * @param dpdk_txq
235  *   Generic pointer to TX queue structure.
236  * @param[in] pkts
237  *   Packets to transmit.
238  * @param pkts_n
239  *   Number of packets in array.
240  *
241  * @return
242  *   Number of packets successfully transmitted (<= pkts_n).
243  */
244 uint16_t
245 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
246 {
247         struct txq *txq = (struct txq *)dpdk_txq;
248         unsigned int elts_head = txq->elts_head;
249         const unsigned int elts_n = txq->elts_n;
250         unsigned int elts_comp_cd = txq->elts_comp_cd;
251         unsigned int elts_comp = 0;
252         unsigned int i;
253         unsigned int max;
254         int err;
255         struct rte_mbuf *buf = pkts[0];
256
257         assert(elts_comp_cd != 0);
258         /* Prefetch first packet cacheline. */
259         rte_prefetch0(buf);
260         txq_complete(txq);
261         max = (elts_n - (elts_head - txq->elts_tail));
262         if (max > elts_n)
263                 max -= elts_n;
264         assert(max >= 1);
265         assert(max <= elts_n);
266         /* Always leave one free entry in the ring. */
267         --max;
268         if (max == 0)
269                 return 0;
270         if (max > pkts_n)
271                 max = pkts_n;
272         for (i = 0; (i != max); ++i) {
273                 struct rte_mbuf *buf_next = pkts[i + 1];
274                 unsigned int elts_head_next =
275                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
276                 struct txq_elt *elt = &(*txq->elts)[elts_head];
277                 uint32_t send_flags = 0;
278 #ifdef HAVE_VERBS_VLAN_INSERTION
279                 int insert_vlan = 0;
280 #endif /* HAVE_VERBS_VLAN_INSERTION */
281                 uintptr_t addr;
282                 uint32_t length;
283                 uint32_t lkey;
284                 uintptr_t buf_next_addr;
285
286                 if (i + 1 < max)
287                         rte_prefetch0(buf_next);
288                 /* Request TX completion. */
289                 if (unlikely(--elts_comp_cd == 0)) {
290                         elts_comp_cd = txq->elts_comp_cd_init;
291                         ++elts_comp;
292                         send_flags |= IBV_EXP_QP_BURST_SIGNALED;
293                 }
294                 /* Should we enable HW CKSUM offload */
295                 if (buf->ol_flags &
296                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
297                         send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
298                         /* HW does not support checksum offloads at arbitrary
299                          * offsets but automatically recognizes the packet
300                          * type. For inner L3/L4 checksums, only VXLAN (UDP)
301                          * tunnels are currently supported. */
302                         if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
303                                 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
304                 }
305                 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
306 #ifdef HAVE_VERBS_VLAN_INSERTION
307                         if (!txq->priv->mps)
308                                 insert_vlan = 1;
309                         else
310 #endif /* HAVE_VERBS_VLAN_INSERTION */
311                         {
312                                 err = insert_vlan_sw(buf);
313                                 if (unlikely(err))
314                                         goto stop;
315                         }
316                 }
317                 /* Retrieve buffer information. */
318                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
319                 length = DATA_LEN(buf);
320                 /* Update element. */
321                 elt->buf = buf;
322                 if (txq->priv->sriov)
323                         rte_prefetch0((volatile void *)
324                                       (uintptr_t)addr);
325                 /* Prefetch next buffer data. */
326                 if (i + 1 < max) {
327                         buf_next_addr =
328                                 rte_pktmbuf_mtod(buf_next, uintptr_t);
329                         rte_prefetch0((volatile void *)
330                                       (uintptr_t)buf_next_addr);
331                 }
332                 /* Put packet into send queue. */
333 #if MLX5_PMD_MAX_INLINE > 0
334                 if (length <= txq->max_inline) {
335 #ifdef HAVE_VERBS_VLAN_INSERTION
336                         if (insert_vlan)
337                                 err = txq->send_pending_inline_vlan
338                                         (txq->qp,
339                                          (void *)addr,
340                                          length,
341                                          send_flags,
342                                          &buf->vlan_tci);
343                         else
344 #endif /* HAVE_VERBS_VLAN_INSERTION */
345                                 err = txq->send_pending_inline
346                                         (txq->qp,
347                                          (void *)addr,
348                                          length,
349                                          send_flags);
350                 } else
351 #endif
352                 {
353                         /*
354                          * Retrieve Memory Region key for this
355                          * memory pool.
356                          */
357                         lkey = txq_mp2mr(txq, txq_mb2mp(buf));
358                         if (unlikely(lkey == (uint32_t)-1)) {
359                                 /* MR does not exist. */
360                                 DEBUG("%p: unable to get MP <-> MR"
361                                       " association", (void *)txq);
362                                 /* Clean up TX element. */
363                                 elt->buf = NULL;
364                                 goto stop;
365                         }
366 #ifdef HAVE_VERBS_VLAN_INSERTION
367                         if (insert_vlan)
368                                 err = txq->send_pending_vlan
369                                         (txq->qp,
370                                          addr,
371                                          length,
372                                          lkey,
373                                          send_flags,
374                                          &buf->vlan_tci);
375                         else
376 #endif /* HAVE_VERBS_VLAN_INSERTION */
377                                 err = txq->send_pending
378                                         (txq->qp,
379                                          addr,
380                                          length,
381                                          lkey,
382                                          send_flags);
383                 }
384                 if (unlikely(err))
385                         goto stop;
386 #ifdef MLX5_PMD_SOFT_COUNTERS
387                 /* Increment sent bytes counter. */
388                 txq->stats.obytes += length;
389 #endif
390 stop:
391                 elts_head = elts_head_next;
392                 buf = buf_next;
393         }
394         /* Take a shortcut if nothing must be sent. */
395         if (unlikely(i == 0))
396                 return 0;
397 #ifdef MLX5_PMD_SOFT_COUNTERS
398         /* Increment sent packets counter. */
399         txq->stats.opackets += i;
400 #endif
401         /* Ring QP doorbell. */
402         err = txq->send_flush(txq->qp);
403         if (unlikely(err)) {
404                 /* A nonzero value is not supposed to be returned.
405                  * Nothing can be done about it. */
406                 DEBUG("%p: send_flush() failed with error %d",
407                       (void *)txq, err);
408         }
409         txq->elts_head = elts_head;
410         txq->elts_comp += elts_comp;
411         txq->elts_comp_cd = elts_comp_cd;
412         return i;
413 }
414
415 /**
416  * Translate RX completion flags to packet type.
417  *
418  * @param flags
419  *   RX completion flags returned by poll_length_flags().
420  *
421  * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
422  *
423  * @return
424  *   Packet type for struct rte_mbuf.
425  */
426 static inline uint32_t
427 rxq_cq_to_pkt_type(uint32_t flags)
428 {
429         uint32_t pkt_type;
430
431         if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
432                 pkt_type =
433                         TRANSPOSE(flags,
434                                   IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
435                                   RTE_PTYPE_L3_IPV4) |
436                         TRANSPOSE(flags,
437                                   IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
438                                   RTE_PTYPE_L3_IPV6) |
439                         TRANSPOSE(flags,
440                                   IBV_EXP_CQ_RX_IPV4_PACKET,
441                                   RTE_PTYPE_INNER_L3_IPV4) |
442                         TRANSPOSE(flags,
443                                   IBV_EXP_CQ_RX_IPV6_PACKET,
444                                   RTE_PTYPE_INNER_L3_IPV6);
445         else
446                 pkt_type =
447                         TRANSPOSE(flags,
448                                   IBV_EXP_CQ_RX_IPV4_PACKET,
449                                   RTE_PTYPE_L3_IPV4) |
450                         TRANSPOSE(flags,
451                                   IBV_EXP_CQ_RX_IPV6_PACKET,
452                                   RTE_PTYPE_L3_IPV6);
453         return pkt_type;
454 }
455
456 /**
457  * Translate RX completion flags to offload flags.
458  *
459  * @param[in] rxq
460  *   Pointer to RX queue structure.
461  * @param flags
462  *   RX completion flags returned by poll_length_flags().
463  *
464  * @return
465  *   Offload flags (ol_flags) for struct rte_mbuf.
466  */
467 static inline uint32_t
468 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
469 {
470         uint32_t ol_flags = 0;
471
472         if (rxq->csum) {
473                 /* Set IP checksum flag only for IPv4/IPv6 packets. */
474                 if (flags &
475                     (IBV_EXP_CQ_RX_IPV4_PACKET | IBV_EXP_CQ_RX_IPV6_PACKET))
476                         ol_flags |=
477                                 TRANSPOSE(~flags,
478                                         IBV_EXP_CQ_RX_IP_CSUM_OK,
479                                         PKT_RX_IP_CKSUM_BAD);
480 #ifdef HAVE_EXP_CQ_RX_TCP_PACKET
481                 /* Set L4 checksum flag only for TCP/UDP packets. */
482                 if (flags &
483                     (IBV_EXP_CQ_RX_TCP_PACKET | IBV_EXP_CQ_RX_UDP_PACKET))
484 #endif /* HAVE_EXP_CQ_RX_TCP_PACKET */
485                         ol_flags |=
486                                 TRANSPOSE(~flags,
487                                         IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
488                                         PKT_RX_L4_CKSUM_BAD);
489         }
490         /*
491          * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
492          * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
493          * (its value is 0).
494          */
495         if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
496                 ol_flags |=
497                         TRANSPOSE(~flags,
498                                   IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
499                                   PKT_RX_IP_CKSUM_BAD) |
500                         TRANSPOSE(~flags,
501                                   IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
502                                   PKT_RX_L4_CKSUM_BAD);
503         return ol_flags;
504 }
505
506 /**
507  * DPDK callback for RX with scattered packets support.
508  *
509  * @param dpdk_rxq
510  *   Generic pointer to RX queue structure.
511  * @param[out] pkts
512  *   Array to store received packets.
513  * @param pkts_n
514  *   Maximum number of packets in array.
515  *
516  * @return
517  *   Number of packets successfully received (<= pkts_n).
518  */
519 uint16_t
520 mlx5_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
521 {
522         struct rxq *rxq = (struct rxq *)dpdk_rxq;
523         struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
524         const unsigned int elts_n = rxq->elts_n;
525         unsigned int elts_head = rxq->elts_head;
526         unsigned int i;
527         unsigned int pkts_ret = 0;
528         int ret;
529
530         if (unlikely(!rxq->sp))
531                 return mlx5_rx_burst(dpdk_rxq, pkts, pkts_n);
532         if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
533                 return 0;
534         for (i = 0; (i != pkts_n); ++i) {
535                 struct rxq_elt_sp *elt = &(*elts)[elts_head];
536                 unsigned int len;
537                 unsigned int pkt_buf_len;
538                 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
539                 struct rte_mbuf **pkt_buf_next = &pkt_buf;
540                 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
541                 unsigned int j = 0;
542                 uint32_t flags;
543                 uint16_t vlan_tci;
544
545                 /* Sanity checks. */
546                 assert(elts_head < rxq->elts_n);
547                 assert(rxq->elts_head < rxq->elts_n);
548                 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
549                 if (unlikely(ret < 0)) {
550                         struct ibv_wc wc;
551                         int wcs_n;
552
553                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
554                               (void *)rxq, ret);
555                         /* ibv_poll_cq() must be used in case of failure. */
556                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
557                         if (unlikely(wcs_n == 0))
558                                 break;
559                         if (unlikely(wcs_n < 0)) {
560                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
561                                       (void *)rxq, wcs_n);
562                                 break;
563                         }
564                         assert(wcs_n == 1);
565                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
566                                 /* Whatever, just repost the offending WR. */
567                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
568                                       " completion status (%d): %s",
569                                       (void *)rxq, wc.wr_id, wc.status,
570                                       ibv_wc_status_str(wc.status));
571 #ifdef MLX5_PMD_SOFT_COUNTERS
572                                 /* Increment dropped packets counter. */
573                                 ++rxq->stats.idropped;
574 #endif
575                                 goto repost;
576                         }
577                         ret = wc.byte_len;
578                 }
579                 if (ret == 0)
580                         break;
581                 assert(ret >= (rxq->crc_present << 2));
582                 len = ret - (rxq->crc_present << 2);
583                 pkt_buf_len = len;
584                 /*
585                  * Replace spent segments with new ones, concatenate and
586                  * return them as pkt_buf.
587                  */
588                 while (1) {
589                         struct ibv_sge *sge = &elt->sges[j];
590                         struct rte_mbuf *seg = elt->bufs[j];
591                         struct rte_mbuf *rep;
592                         unsigned int seg_tailroom;
593
594                         assert(seg != NULL);
595                         /*
596                          * Fetch initial bytes of packet descriptor into a
597                          * cacheline while allocating rep.
598                          */
599                         rte_prefetch0(seg);
600                         rep = rte_mbuf_raw_alloc(rxq->mp);
601                         if (unlikely(rep == NULL)) {
602                                 /*
603                                  * Unable to allocate a replacement mbuf,
604                                  * repost WR.
605                                  */
606                                 DEBUG("rxq=%p: can't allocate a new mbuf",
607                                       (void *)rxq);
608                                 if (pkt_buf != NULL) {
609                                         *pkt_buf_next = NULL;
610                                         rte_pktmbuf_free(pkt_buf);
611                                 }
612                                 /* Increment out of memory counters. */
613                                 ++rxq->stats.rx_nombuf;
614                                 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
615                                 goto repost;
616                         }
617 #ifndef NDEBUG
618                         /* Poison user-modifiable fields in rep. */
619                         NEXT(rep) = (void *)((uintptr_t)-1);
620                         SET_DATA_OFF(rep, 0xdead);
621                         DATA_LEN(rep) = 0xd00d;
622                         PKT_LEN(rep) = 0xdeadd00d;
623                         NB_SEGS(rep) = 0x2a;
624                         PORT(rep) = 0x2a;
625                         rep->ol_flags = -1;
626 #endif
627                         assert(rep->buf_len == seg->buf_len);
628                         /* Reconfigure sge to use rep instead of seg. */
629                         assert(sge->lkey == rxq->mr->lkey);
630                         sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
631                         elt->bufs[j] = rep;
632                         ++j;
633                         /* Update pkt_buf if it's the first segment, or link
634                          * seg to the previous one and update pkt_buf_next. */
635                         *pkt_buf_next = seg;
636                         pkt_buf_next = &NEXT(seg);
637                         /* Update seg information. */
638                         seg_tailroom = (seg->buf_len - seg_headroom);
639                         assert(sge->length == seg_tailroom);
640                         SET_DATA_OFF(seg, seg_headroom);
641                         if (likely(len <= seg_tailroom)) {
642                                 /* Last segment. */
643                                 DATA_LEN(seg) = len;
644                                 PKT_LEN(seg) = len;
645                                 /* Sanity check. */
646                                 assert(rte_pktmbuf_headroom(seg) ==
647                                        seg_headroom);
648                                 assert(rte_pktmbuf_tailroom(seg) ==
649                                        (seg_tailroom - len));
650                                 break;
651                         }
652                         DATA_LEN(seg) = seg_tailroom;
653                         PKT_LEN(seg) = seg_tailroom;
654                         /* Sanity check. */
655                         assert(rte_pktmbuf_headroom(seg) == seg_headroom);
656                         assert(rte_pktmbuf_tailroom(seg) == 0);
657                         /* Fix len and clear headroom for next segments. */
658                         len -= seg_tailroom;
659                         seg_headroom = 0;
660                 }
661                 /* Update head and tail segments. */
662                 *pkt_buf_next = NULL;
663                 assert(pkt_buf != NULL);
664                 assert(j != 0);
665                 NB_SEGS(pkt_buf) = j;
666                 PORT(pkt_buf) = rxq->port_id;
667                 PKT_LEN(pkt_buf) = pkt_buf_len;
668                 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
669                         pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
670                         pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
671 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
672                         if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
673                                 pkt_buf->ol_flags |= PKT_RX_VLAN_PKT |
674                                         PKT_RX_VLAN_STRIPPED;
675                                 pkt_buf->vlan_tci = vlan_tci;
676                         }
677 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
678                 }
679
680                 /* Return packet. */
681                 *(pkts++) = pkt_buf;
682                 ++pkts_ret;
683 #ifdef MLX5_PMD_SOFT_COUNTERS
684                 /* Increment bytes counter. */
685                 rxq->stats.ibytes += pkt_buf_len;
686 #endif
687 repost:
688                 ret = rxq->recv(rxq->wq, elt->sges, RTE_DIM(elt->sges));
689                 if (unlikely(ret)) {
690                         /* Inability to repost WRs is fatal. */
691                         DEBUG("%p: recv_sg_list(): failed (ret=%d)",
692                               (void *)rxq->priv,
693                               ret);
694                         abort();
695                 }
696                 if (++elts_head >= elts_n)
697                         elts_head = 0;
698                 continue;
699         }
700         if (unlikely(i == 0))
701                 return 0;
702         rxq->elts_head = elts_head;
703 #ifdef MLX5_PMD_SOFT_COUNTERS
704         /* Increment packets counter. */
705         rxq->stats.ipackets += pkts_ret;
706 #endif
707         return pkts_ret;
708 }
709
710 /**
711  * DPDK callback for RX.
712  *
713  * The following function is the same as mlx5_rx_burst_sp(), except it doesn't
714  * manage scattered packets. Improves performance when MRU is lower than the
715  * size of the first segment.
716  *
717  * @param dpdk_rxq
718  *   Generic pointer to RX queue structure.
719  * @param[out] pkts
720  *   Array to store received packets.
721  * @param pkts_n
722  *   Maximum number of packets in array.
723  *
724  * @return
725  *   Number of packets successfully received (<= pkts_n).
726  */
727 uint16_t
728 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
729 {
730         struct rxq *rxq = (struct rxq *)dpdk_rxq;
731         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
732         const unsigned int elts_n = rxq->elts_n;
733         unsigned int elts_head = rxq->elts_head;
734         struct ibv_sge sges[pkts_n];
735         unsigned int i;
736         unsigned int pkts_ret = 0;
737         int ret;
738
739         if (unlikely(rxq->sp))
740                 return mlx5_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
741         for (i = 0; (i != pkts_n); ++i) {
742                 struct rxq_elt *elt = &(*elts)[elts_head];
743                 unsigned int len;
744                 struct rte_mbuf *seg = elt->buf;
745                 struct rte_mbuf *rep;
746                 uint32_t flags;
747                 uint16_t vlan_tci;
748
749                 /* Sanity checks. */
750                 assert(seg != NULL);
751                 assert(elts_head < rxq->elts_n);
752                 assert(rxq->elts_head < rxq->elts_n);
753                 /*
754                  * Fetch initial bytes of packet descriptor into a
755                  * cacheline while allocating rep.
756                  */
757                 rte_mbuf_prefetch_part1(seg);
758                 rte_mbuf_prefetch_part2(seg);
759                 ret = rxq->poll(rxq->cq, NULL, NULL, &flags, &vlan_tci);
760                 if (unlikely(ret < 0)) {
761                         struct ibv_wc wc;
762                         int wcs_n;
763
764                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
765                               (void *)rxq, ret);
766                         /* ibv_poll_cq() must be used in case of failure. */
767                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
768                         if (unlikely(wcs_n == 0))
769                                 break;
770                         if (unlikely(wcs_n < 0)) {
771                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
772                                       (void *)rxq, wcs_n);
773                                 break;
774                         }
775                         assert(wcs_n == 1);
776                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
777                                 /* Whatever, just repost the offending WR. */
778                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
779                                       " completion status (%d): %s",
780                                       (void *)rxq, wc.wr_id, wc.status,
781                                       ibv_wc_status_str(wc.status));
782 #ifdef MLX5_PMD_SOFT_COUNTERS
783                                 /* Increment dropped packets counter. */
784                                 ++rxq->stats.idropped;
785 #endif
786                                 /* Add SGE to array for repost. */
787                                 sges[i] = elt->sge;
788                                 goto repost;
789                         }
790                         ret = wc.byte_len;
791                 }
792                 if (ret == 0)
793                         break;
794                 assert(ret >= (rxq->crc_present << 2));
795                 len = ret - (rxq->crc_present << 2);
796                 rep = rte_mbuf_raw_alloc(rxq->mp);
797                 if (unlikely(rep == NULL)) {
798                         /*
799                          * Unable to allocate a replacement mbuf,
800                          * repost WR.
801                          */
802                         DEBUG("rxq=%p: can't allocate a new mbuf",
803                               (void *)rxq);
804                         /* Increment out of memory counters. */
805                         ++rxq->stats.rx_nombuf;
806                         ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
807                         goto repost;
808                 }
809
810                 /* Reconfigure sge to use rep instead of seg. */
811                 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
812                 assert(elt->sge.lkey == rxq->mr->lkey);
813                 elt->buf = rep;
814
815                 /* Add SGE to array for repost. */
816                 sges[i] = elt->sge;
817
818                 /* Update seg information. */
819                 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
820                 NB_SEGS(seg) = 1;
821                 PORT(seg) = rxq->port_id;
822                 NEXT(seg) = NULL;
823                 PKT_LEN(seg) = len;
824                 DATA_LEN(seg) = len;
825                 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip) {
826                         seg->packet_type = rxq_cq_to_pkt_type(flags);
827                         seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
828 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
829                         if (flags & IBV_EXP_CQ_RX_CVLAN_STRIPPED_V1) {
830                                 seg->ol_flags |= PKT_RX_VLAN_PKT |
831                                         PKT_RX_VLAN_STRIPPED;
832                                 seg->vlan_tci = vlan_tci;
833                         }
834 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
835                 }
836                 /* Return packet. */
837                 *(pkts++) = seg;
838                 ++pkts_ret;
839 #ifdef MLX5_PMD_SOFT_COUNTERS
840                 /* Increment bytes counter. */
841                 rxq->stats.ibytes += len;
842 #endif
843 repost:
844                 if (++elts_head >= elts_n)
845                         elts_head = 0;
846                 continue;
847         }
848         if (unlikely(i == 0))
849                 return 0;
850         /* Repost WRs. */
851 #ifdef DEBUG_RECV
852         DEBUG("%p: reposting %u WRs", (void *)rxq, i);
853 #endif
854         ret = rxq->recv(rxq->wq, sges, i);
855         if (unlikely(ret)) {
856                 /* Inability to repost WRs is fatal. */
857                 DEBUG("%p: recv_burst(): failed (ret=%d)",
858                       (void *)rxq->priv,
859                       ret);
860                 abort();
861         }
862         rxq->elts_head = elts_head;
863 #ifdef MLX5_PMD_SOFT_COUNTERS
864         /* Increment packets counter. */
865         rxq->stats.ipackets += pkts_ret;
866 #endif
867         return pkts_ret;
868 }
869
870 /**
871  * Dummy DPDK callback for TX.
872  *
873  * This function is used to temporarily replace the real callback during
874  * unsafe control operations on the queue, or in case of error.
875  *
876  * @param dpdk_txq
877  *   Generic pointer to TX queue structure.
878  * @param[in] pkts
879  *   Packets to transmit.
880  * @param pkts_n
881  *   Number of packets in array.
882  *
883  * @return
884  *   Number of packets successfully transmitted (<= pkts_n).
885  */
886 uint16_t
887 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
888 {
889         (void)dpdk_txq;
890         (void)pkts;
891         (void)pkts_n;
892         return 0;
893 }
894
895 /**
896  * Dummy DPDK callback for RX.
897  *
898  * This function is used to temporarily replace the real callback during
899  * unsafe control operations on the queue, or in case of error.
900  *
901  * @param dpdk_rxq
902  *   Generic pointer to RX queue structure.
903  * @param[out] pkts
904  *   Array to store received packets.
905  * @param pkts_n
906  *   Maximum number of packets in array.
907  *
908  * @return
909  *   Number of packets successfully received (<= pkts_n).
910  */
911 uint16_t
912 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
913 {
914         (void)dpdk_rxq;
915         (void)pkts;
916         (void)pkts_n;
917         return 0;
918 }