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