mlx5: support scattered Rx and Tx
[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 Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
119  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
120  * remove an entry first.
121  *
122  * @param txq
123  *   Pointer to TX queue structure.
124  * @param[in] mp
125  *   Memory Pool for which a Memory Region lkey must be returned.
126  *
127  * @return
128  *   mr->lkey on success, (uint32_t)-1 on failure.
129  */
130 static uint32_t
131 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
132 {
133         unsigned int i;
134         struct ibv_mr *mr;
135
136         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
137                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
138                         /* Unknown MP, add a new MR for it. */
139                         break;
140                 }
141                 if (txq->mp2mr[i].mp == mp) {
142                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
143                         assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
144                         return txq->mp2mr[i].lkey;
145                 }
146         }
147         /* Add a new entry, register MR first. */
148         DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
149         mr = ibv_reg_mr(txq->priv->pd,
150                         (void *)mp->elt_va_start,
151                         (mp->elt_va_end - mp->elt_va_start),
152                         (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
153         if (unlikely(mr == NULL)) {
154                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
155                       (void *)txq);
156                 return (uint32_t)-1;
157         }
158         if (unlikely(i == RTE_DIM(txq->mp2mr))) {
159                 /* Table is full, remove oldest entry. */
160                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
161                       (void *)txq);
162                 --i;
163                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
164                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
165                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
166         }
167         /* Store the new entry. */
168         txq->mp2mr[i].mp = mp;
169         txq->mp2mr[i].mr = mr;
170         txq->mp2mr[i].lkey = mr->lkey;
171         DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
172               (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
173         return txq->mp2mr[i].lkey;
174 }
175
176 #if MLX5_PMD_SGE_WR_N > 1
177
178 /**
179  * Copy scattered mbuf contents to a single linear buffer.
180  *
181  * @param[out] linear
182  *   Linear output buffer.
183  * @param[in] buf
184  *   Scattered input buffer.
185  *
186  * @return
187  *   Number of bytes copied to the output buffer or 0 if not large enough.
188  */
189 static unsigned int
190 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
191 {
192         unsigned int size = 0;
193         unsigned int offset;
194
195         do {
196                 unsigned int len = DATA_LEN(buf);
197
198                 offset = size;
199                 size += len;
200                 if (unlikely(size > sizeof(*linear)))
201                         return 0;
202                 memcpy(&(*linear)[offset],
203                        rte_pktmbuf_mtod(buf, uint8_t *),
204                        len);
205                 buf = NEXT(buf);
206         } while (buf != NULL);
207         return size;
208 }
209
210 /**
211  * Handle scattered buffers for mlx5_tx_burst().
212  *
213  * @param txq
214  *   TX queue structure.
215  * @param segs
216  *   Number of segments in buf.
217  * @param elt
218  *   TX queue element to fill.
219  * @param[in] buf
220  *   Buffer to process.
221  * @param elts_head
222  *   Index of the linear buffer to use if necessary (normally txq->elts_head).
223  * @param[out] sges
224  *   Array filled with SGEs on success.
225  *
226  * @return
227  *   A structure containing the processed packet size in bytes and the
228  *   number of SGEs. Both fields are set to (unsigned int)-1 in case of
229  *   failure.
230  */
231 static struct tx_burst_sg_ret {
232         unsigned int length;
233         unsigned int num;
234 }
235 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
236             struct rte_mbuf *buf, unsigned int elts_head,
237             struct ibv_sge (*sges)[MLX5_PMD_SGE_WR_N])
238 {
239         unsigned int sent_size = 0;
240         unsigned int j;
241         int linearize = 0;
242
243         /* When there are too many segments, extra segments are
244          * linearized in the last SGE. */
245         if (unlikely(segs > RTE_DIM(*sges))) {
246                 segs = (RTE_DIM(*sges) - 1);
247                 linearize = 1;
248         }
249         /* Update element. */
250         elt->buf = buf;
251         /* Register segments as SGEs. */
252         for (j = 0; (j != segs); ++j) {
253                 struct ibv_sge *sge = &(*sges)[j];
254                 uint32_t lkey;
255
256                 /* Retrieve Memory Region key for this memory pool. */
257                 lkey = txq_mp2mr(txq, buf->pool);
258                 if (unlikely(lkey == (uint32_t)-1)) {
259                         /* MR does not exist. */
260                         DEBUG("%p: unable to get MP <-> MR association",
261                               (void *)txq);
262                         /* Clean up TX element. */
263                         elt->buf = NULL;
264                         goto stop;
265                 }
266                 /* Update SGE. */
267                 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
268                 if (txq->priv->vf)
269                         rte_prefetch0((volatile void *)
270                                       (uintptr_t)sge->addr);
271                 sge->length = DATA_LEN(buf);
272                 sge->lkey = lkey;
273                 sent_size += sge->length;
274                 buf = NEXT(buf);
275         }
276         /* If buf is not NULL here and is not going to be linearized,
277          * nb_segs is not valid. */
278         assert(j == segs);
279         assert((buf == NULL) || (linearize));
280         /* Linearize extra segments. */
281         if (linearize) {
282                 struct ibv_sge *sge = &(*sges)[segs];
283                 linear_t *linear = &(*txq->elts_linear)[elts_head];
284                 unsigned int size = linearize_mbuf(linear, buf);
285
286                 assert(segs == (RTE_DIM(*sges) - 1));
287                 if (size == 0) {
288                         /* Invalid packet. */
289                         DEBUG("%p: packet too large to be linearized.",
290                               (void *)txq);
291                         /* Clean up TX element. */
292                         elt->buf = NULL;
293                         goto stop;
294                 }
295                 /* If MLX5_PMD_SGE_WR_N is 1, free mbuf immediately. */
296                 if (RTE_DIM(*sges) == 1) {
297                         do {
298                                 struct rte_mbuf *next = NEXT(buf);
299
300                                 rte_pktmbuf_free_seg(buf);
301                                 buf = next;
302                         } while (buf != NULL);
303                         elt->buf = NULL;
304                 }
305                 /* Update SGE. */
306                 sge->addr = (uintptr_t)&(*linear)[0];
307                 sge->length = size;
308                 sge->lkey = txq->mr_linear->lkey;
309                 sent_size += size;
310         }
311         return (struct tx_burst_sg_ret){
312                 .length = sent_size,
313                 .num = segs,
314         };
315 stop:
316         return (struct tx_burst_sg_ret){
317                 .length = -1,
318                 .num = -1,
319         };
320 }
321
322 #endif /* MLX5_PMD_SGE_WR_N > 1 */
323
324 /**
325  * DPDK callback for TX.
326  *
327  * @param dpdk_txq
328  *   Generic pointer to TX queue structure.
329  * @param[in] pkts
330  *   Packets to transmit.
331  * @param pkts_n
332  *   Number of packets in array.
333  *
334  * @return
335  *   Number of packets successfully transmitted (<= pkts_n).
336  */
337 uint16_t
338 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
339 {
340         struct txq *txq = (struct txq *)dpdk_txq;
341         unsigned int elts_head = txq->elts_head;
342         const unsigned int elts_tail = txq->elts_tail;
343         const unsigned int elts_n = txq->elts_n;
344         unsigned int elts_comp_cd = txq->elts_comp_cd;
345         unsigned int elts_comp = 0;
346         unsigned int i;
347         unsigned int max;
348         int err;
349
350         assert(elts_comp_cd != 0);
351         txq_complete(txq);
352         max = (elts_n - (elts_head - elts_tail));
353         if (max > elts_n)
354                 max -= elts_n;
355         assert(max >= 1);
356         assert(max <= elts_n);
357         /* Always leave one free entry in the ring. */
358         --max;
359         if (max == 0)
360                 return 0;
361         if (max > pkts_n)
362                 max = pkts_n;
363         for (i = 0; (i != max); ++i) {
364                 struct rte_mbuf *buf = pkts[i];
365                 unsigned int elts_head_next =
366                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
367                 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
368                 struct txq_elt *elt = &(*txq->elts)[elts_head];
369                 unsigned int segs = NB_SEGS(buf);
370                 uint32_t send_flags = 0;
371
372                 /* Clean up old buffer. */
373                 if (likely(elt->buf != NULL)) {
374                         struct rte_mbuf *tmp = elt->buf;
375
376                         /* Faster than rte_pktmbuf_free(). */
377                         do {
378                                 struct rte_mbuf *next = NEXT(tmp);
379
380                                 rte_pktmbuf_free_seg(tmp);
381                                 tmp = next;
382                         } while (tmp != NULL);
383                 }
384                 /* Request TX completion. */
385                 if (unlikely(--elts_comp_cd == 0)) {
386                         elts_comp_cd = txq->elts_comp_cd_init;
387                         ++elts_comp;
388                         send_flags |= IBV_EXP_QP_BURST_SIGNALED;
389                 }
390                 if (likely(segs == 1)) {
391                         uintptr_t addr;
392                         uint32_t length;
393                         uint32_t lkey;
394
395                         /* Retrieve buffer information. */
396                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
397                         length = DATA_LEN(buf);
398                         /* Retrieve Memory Region key for this memory pool. */
399                         lkey = txq_mp2mr(txq, buf->pool);
400                         if (unlikely(lkey == (uint32_t)-1)) {
401                                 /* MR does not exist. */
402                                 DEBUG("%p: unable to get MP <-> MR"
403                                       " association", (void *)txq);
404                                 /* Clean up TX element. */
405                                 elt->buf = NULL;
406                                 goto stop;
407                         }
408                         /* Update element. */
409                         elt->buf = buf;
410                         if (txq->priv->vf)
411                                 rte_prefetch0((volatile void *)
412                                               (uintptr_t)addr);
413                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
414                         /* Put packet into send queue. */
415 #if MLX5_PMD_MAX_INLINE > 0
416                         if (length <= txq->max_inline)
417                                 err = txq->if_qp->send_pending_inline
418                                         (txq->qp,
419                                          (void *)addr,
420                                          length,
421                                          send_flags);
422                         else
423 #endif
424                                 err = txq->if_qp->send_pending
425                                         (txq->qp,
426                                          addr,
427                                          length,
428                                          lkey,
429                                          send_flags);
430                         if (unlikely(err))
431                                 goto stop;
432                 } else {
433 #if MLX5_PMD_SGE_WR_N > 1
434                         struct ibv_sge sges[MLX5_PMD_SGE_WR_N];
435                         struct tx_burst_sg_ret ret;
436
437                         ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
438                                           &sges);
439                         if (ret.length == (unsigned int)-1)
440                                 goto stop;
441                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
442                         /* Put SG list into send queue. */
443                         err = txq->if_qp->send_pending_sg_list
444                                 (txq->qp,
445                                  sges,
446                                  ret.num,
447                                  send_flags);
448                         if (unlikely(err))
449                                 goto stop;
450 #else /* MLX5_PMD_SGE_WR_N > 1 */
451                         DEBUG("%p: TX scattered buffers support not"
452                               " compiled in", (void *)txq);
453                         goto stop;
454 #endif /* MLX5_PMD_SGE_WR_N > 1 */
455                 }
456                 elts_head = elts_head_next;
457         }
458 stop:
459         /* Take a shortcut if nothing must be sent. */
460         if (unlikely(i == 0))
461                 return 0;
462         /* Ring QP doorbell. */
463         err = txq->if_qp->send_flush(txq->qp);
464         if (unlikely(err)) {
465                 /* A nonzero value is not supposed to be returned.
466                  * Nothing can be done about it. */
467                 DEBUG("%p: send_flush() failed with error %d",
468                       (void *)txq, err);
469         }
470         txq->elts_head = elts_head;
471         txq->elts_comp += elts_comp;
472         txq->elts_comp_cd = elts_comp_cd;
473         return i;
474 }
475
476 /**
477  * DPDK callback for RX with scattered packets support.
478  *
479  * @param dpdk_rxq
480  *   Generic pointer to RX queue structure.
481  * @param[out] pkts
482  *   Array to store received packets.
483  * @param pkts_n
484  *   Maximum number of packets in array.
485  *
486  * @return
487  *   Number of packets successfully received (<= pkts_n).
488  */
489 uint16_t
490 mlx5_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
491 {
492         struct rxq *rxq = (struct rxq *)dpdk_rxq;
493         struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
494         const unsigned int elts_n = rxq->elts_n;
495         unsigned int elts_head = rxq->elts_head;
496         struct ibv_recv_wr head;
497         struct ibv_recv_wr **next = &head.next;
498         struct ibv_recv_wr *bad_wr;
499         unsigned int i;
500         unsigned int pkts_ret = 0;
501         int ret;
502
503         if (unlikely(!rxq->sp))
504                 return mlx5_rx_burst(dpdk_rxq, pkts, pkts_n);
505         if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
506                 return 0;
507         for (i = 0; (i != pkts_n); ++i) {
508                 struct rxq_elt_sp *elt = &(*elts)[elts_head];
509                 struct ibv_recv_wr *wr = &elt->wr;
510                 uint64_t wr_id = wr->wr_id;
511                 unsigned int len;
512                 unsigned int pkt_buf_len;
513                 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
514                 struct rte_mbuf **pkt_buf_next = &pkt_buf;
515                 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
516                 unsigned int j = 0;
517                 uint32_t flags;
518
519                 /* Sanity checks. */
520 #ifdef NDEBUG
521                 (void)wr_id;
522 #endif
523                 assert(wr_id < rxq->elts_n);
524                 assert(wr->sg_list == elt->sges);
525                 assert(wr->num_sge == RTE_DIM(elt->sges));
526                 assert(elts_head < rxq->elts_n);
527                 assert(rxq->elts_head < rxq->elts_n);
528                 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
529                                                     &flags);
530                 if (unlikely(ret < 0)) {
531                         struct ibv_wc wc;
532                         int wcs_n;
533
534                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
535                               (void *)rxq, ret);
536                         /* ibv_poll_cq() must be used in case of failure. */
537                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
538                         if (unlikely(wcs_n == 0))
539                                 break;
540                         if (unlikely(wcs_n < 0)) {
541                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
542                                       (void *)rxq, wcs_n);
543                                 break;
544                         }
545                         assert(wcs_n == 1);
546                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
547                                 /* Whatever, just repost the offending WR. */
548                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
549                                       " completion status (%d): %s",
550                                       (void *)rxq, wc.wr_id, wc.status,
551                                       ibv_wc_status_str(wc.status));
552                                 /* Link completed WRs together for repost. */
553                                 *next = wr;
554                                 next = &wr->next;
555                                 goto repost;
556                         }
557                         ret = wc.byte_len;
558                 }
559                 if (ret == 0)
560                         break;
561                 len = ret;
562                 pkt_buf_len = len;
563                 /* Link completed WRs together for repost. */
564                 *next = wr;
565                 next = &wr->next;
566                 /*
567                  * Replace spent segments with new ones, concatenate and
568                  * return them as pkt_buf.
569                  */
570                 while (1) {
571                         struct ibv_sge *sge = &elt->sges[j];
572                         struct rte_mbuf *seg = elt->bufs[j];
573                         struct rte_mbuf *rep;
574                         unsigned int seg_tailroom;
575
576                         /*
577                          * Fetch initial bytes of packet descriptor into a
578                          * cacheline while allocating rep.
579                          */
580                         rte_prefetch0(seg);
581                         rep = __rte_mbuf_raw_alloc(rxq->mp);
582                         if (unlikely(rep == NULL)) {
583                                 /*
584                                  * Unable to allocate a replacement mbuf,
585                                  * repost WR.
586                                  */
587                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
588                                       " can't allocate a new mbuf",
589                                       (void *)rxq, wr_id);
590                                 if (pkt_buf != NULL) {
591                                         *pkt_buf_next = NULL;
592                                         rte_pktmbuf_free(pkt_buf);
593                                 }
594                                 /* Increment out of memory counters. */
595                                 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
596                                 goto repost;
597                         }
598 #ifndef NDEBUG
599                         /* Poison user-modifiable fields in rep. */
600                         NEXT(rep) = (void *)((uintptr_t)-1);
601                         SET_DATA_OFF(rep, 0xdead);
602                         DATA_LEN(rep) = 0xd00d;
603                         PKT_LEN(rep) = 0xdeadd00d;
604                         NB_SEGS(rep) = 0x2a;
605                         PORT(rep) = 0x2a;
606                         rep->ol_flags = -1;
607 #endif
608                         assert(rep->buf_len == seg->buf_len);
609                         assert(rep->buf_len == rxq->mb_len);
610                         /* Reconfigure sge to use rep instead of seg. */
611                         assert(sge->lkey == rxq->mr->lkey);
612                         sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
613                         elt->bufs[j] = rep;
614                         ++j;
615                         /* Update pkt_buf if it's the first segment, or link
616                          * seg to the previous one and update pkt_buf_next. */
617                         *pkt_buf_next = seg;
618                         pkt_buf_next = &NEXT(seg);
619                         /* Update seg information. */
620                         seg_tailroom = (seg->buf_len - seg_headroom);
621                         assert(sge->length == seg_tailroom);
622                         SET_DATA_OFF(seg, seg_headroom);
623                         if (likely(len <= seg_tailroom)) {
624                                 /* Last segment. */
625                                 DATA_LEN(seg) = len;
626                                 PKT_LEN(seg) = len;
627                                 /* Sanity check. */
628                                 assert(rte_pktmbuf_headroom(seg) ==
629                                        seg_headroom);
630                                 assert(rte_pktmbuf_tailroom(seg) ==
631                                        (seg_tailroom - len));
632                                 break;
633                         }
634                         DATA_LEN(seg) = seg_tailroom;
635                         PKT_LEN(seg) = seg_tailroom;
636                         /* Sanity check. */
637                         assert(rte_pktmbuf_headroom(seg) == seg_headroom);
638                         assert(rte_pktmbuf_tailroom(seg) == 0);
639                         /* Fix len and clear headroom for next segments. */
640                         len -= seg_tailroom;
641                         seg_headroom = 0;
642                 }
643                 /* Update head and tail segments. */
644                 *pkt_buf_next = NULL;
645                 assert(pkt_buf != NULL);
646                 assert(j != 0);
647                 NB_SEGS(pkt_buf) = j;
648                 PORT(pkt_buf) = rxq->port_id;
649                 PKT_LEN(pkt_buf) = pkt_buf_len;
650
651                 /* Return packet. */
652                 *(pkts++) = pkt_buf;
653                 ++pkts_ret;
654 repost:
655                 if (++elts_head >= elts_n)
656                         elts_head = 0;
657                 continue;
658         }
659         if (unlikely(i == 0))
660                 return 0;
661         *next = NULL;
662         /* Repost WRs. */
663 #ifdef DEBUG_RECV
664         DEBUG("%p: reposting %d WRs", (void *)rxq, i);
665 #endif
666         ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
667         if (unlikely(ret)) {
668                 /* Inability to repost WRs is fatal. */
669                 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
670                       (void *)rxq->priv,
671                       (void *)bad_wr,
672                       strerror(ret));
673                 abort();
674         }
675         rxq->elts_head = elts_head;
676         return pkts_ret;
677 }
678
679 /**
680  * DPDK callback for RX.
681  *
682  * The following function is the same as mlx5_rx_burst_sp(), except it doesn't
683  * manage scattered packets. Improves performance when MRU is lower than the
684  * size of the first segment.
685  *
686  * @param dpdk_rxq
687  *   Generic pointer to RX queue structure.
688  * @param[out] pkts
689  *   Array to store received packets.
690  * @param pkts_n
691  *   Maximum number of packets in array.
692  *
693  * @return
694  *   Number of packets successfully received (<= pkts_n).
695  */
696 uint16_t
697 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
698 {
699         struct rxq *rxq = (struct rxq *)dpdk_rxq;
700         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
701         const unsigned int elts_n = rxq->elts_n;
702         unsigned int elts_head = rxq->elts_head;
703         struct ibv_sge sges[pkts_n];
704         unsigned int i;
705         unsigned int pkts_ret = 0;
706         int ret;
707
708         if (unlikely(rxq->sp))
709                 return mlx5_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
710         for (i = 0; (i != pkts_n); ++i) {
711                 struct rxq_elt *elt = &(*elts)[elts_head];
712                 struct ibv_recv_wr *wr = &elt->wr;
713                 uint64_t wr_id = wr->wr_id;
714                 unsigned int len;
715                 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
716                         WR_ID(wr_id).offset);
717                 struct rte_mbuf *rep;
718                 uint32_t flags;
719
720                 /* Sanity checks. */
721                 assert(WR_ID(wr_id).id < rxq->elts_n);
722                 assert(wr->sg_list == &elt->sge);
723                 assert(wr->num_sge == 1);
724                 assert(elts_head < rxq->elts_n);
725                 assert(rxq->elts_head < rxq->elts_n);
726                 /*
727                  * Fetch initial bytes of packet descriptor into a
728                  * cacheline while allocating rep.
729                  */
730                 rte_prefetch0(seg);
731                 rte_prefetch0(&seg->cacheline1);
732                 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
733                                                     &flags);
734                 if (unlikely(ret < 0)) {
735                         struct ibv_wc wc;
736                         int wcs_n;
737
738                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
739                               (void *)rxq, ret);
740                         /* ibv_poll_cq() must be used in case of failure. */
741                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
742                         if (unlikely(wcs_n == 0))
743                                 break;
744                         if (unlikely(wcs_n < 0)) {
745                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
746                                       (void *)rxq, wcs_n);
747                                 break;
748                         }
749                         assert(wcs_n == 1);
750                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
751                                 /* Whatever, just repost the offending WR. */
752                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
753                                       " completion status (%d): %s",
754                                       (void *)rxq, wc.wr_id, wc.status,
755                                       ibv_wc_status_str(wc.status));
756                                 /* Add SGE to array for repost. */
757                                 sges[i] = elt->sge;
758                                 goto repost;
759                         }
760                         ret = wc.byte_len;
761                 }
762                 if (ret == 0)
763                         break;
764                 len = ret;
765                 rep = __rte_mbuf_raw_alloc(rxq->mp);
766                 if (unlikely(rep == NULL)) {
767                         /*
768                          * Unable to allocate a replacement mbuf,
769                          * repost WR.
770                          */
771                         DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
772                               " can't allocate a new mbuf",
773                               (void *)rxq, WR_ID(wr_id).id);
774                         /* Increment out of memory counters. */
775                         ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
776                         goto repost;
777                 }
778
779                 /* Reconfigure sge to use rep instead of seg. */
780                 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
781                 assert(elt->sge.lkey == rxq->mr->lkey);
782                 WR_ID(wr->wr_id).offset =
783                         (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
784                          (uintptr_t)rep);
785                 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
786
787                 /* Add SGE to array for repost. */
788                 sges[i] = elt->sge;
789
790                 /* Update seg information. */
791                 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
792                 NB_SEGS(seg) = 1;
793                 PORT(seg) = rxq->port_id;
794                 NEXT(seg) = NULL;
795                 PKT_LEN(seg) = len;
796                 DATA_LEN(seg) = len;
797
798                 /* Return packet. */
799                 *(pkts++) = seg;
800                 ++pkts_ret;
801 repost:
802                 if (++elts_head >= elts_n)
803                         elts_head = 0;
804                 continue;
805         }
806         if (unlikely(i == 0))
807                 return 0;
808         /* Repost WRs. */
809 #ifdef DEBUG_RECV
810         DEBUG("%p: reposting %u WRs", (void *)rxq, i);
811 #endif
812         ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
813         if (unlikely(ret)) {
814                 /* Inability to repost WRs is fatal. */
815                 DEBUG("%p: recv_burst(): failed (ret=%d)",
816                       (void *)rxq->priv,
817                       ret);
818                 abort();
819         }
820         rxq->elts_head = elts_head;
821         return pkts_ret;
822 }
823
824 /**
825  * Dummy DPDK callback for TX.
826  *
827  * This function is used to temporarily replace the real callback during
828  * unsafe control operations on the queue, or in case of error.
829  *
830  * @param dpdk_txq
831  *   Generic pointer to TX queue structure.
832  * @param[in] pkts
833  *   Packets to transmit.
834  * @param pkts_n
835  *   Number of packets in array.
836  *
837  * @return
838  *   Number of packets successfully transmitted (<= pkts_n).
839  */
840 uint16_t
841 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
842 {
843         (void)dpdk_txq;
844         (void)pkts;
845         (void)pkts_n;
846         return 0;
847 }
848
849 /**
850  * Dummy DPDK callback for RX.
851  *
852  * This function is used to temporarily replace the real callback during
853  * unsafe control operations on the queue, or in case of error.
854  *
855  * @param dpdk_rxq
856  *   Generic pointer to RX queue structure.
857  * @param[out] pkts
858  *   Array to store received packets.
859  * @param pkts_n
860  *   Maximum number of packets in array.
861  *
862  * @return
863  *   Number of packets successfully received (<= pkts_n).
864  */
865 uint16_t
866 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
867 {
868         (void)dpdk_rxq;
869         (void)pkts;
870         (void)pkts_n;
871         return 0;
872 }