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