net/mlx4: merge Tx path functions
[dpdk.git] / drivers / net / mlx4 / mlx4_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 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 /**
35  * @file
36  * Data plane functions for mlx4 driver.
37  */
38
39 #include <assert.h>
40 #include <stdint.h>
41 #include <string.h>
42
43 /* Verbs headers do not support -pedantic. */
44 #ifdef PEDANTIC
45 #pragma GCC diagnostic ignored "-Wpedantic"
46 #endif
47 #include <infiniband/verbs.h>
48 #ifdef PEDANTIC
49 #pragma GCC diagnostic error "-Wpedantic"
50 #endif
51
52 #include <rte_branch_prediction.h>
53 #include <rte_common.h>
54 #include <rte_io.h>
55 #include <rte_mbuf.h>
56 #include <rte_mempool.h>
57 #include <rte_prefetch.h>
58
59 #include "mlx4.h"
60 #include "mlx4_prm.h"
61 #include "mlx4_rxtx.h"
62 #include "mlx4_utils.h"
63
64 /**
65  * Pointer-value pair structure used in tx_post_send for saving the first
66  * DWORD (32 byte) of a TXBB.
67  */
68 struct pv {
69         struct mlx4_wqe_data_seg *dseg;
70         uint32_t val;
71 };
72
73 /**
74  * Stamp a WQE so it won't be reused by the HW.
75  *
76  * Routine is used when freeing WQE used by the chip or when failing
77  * building an WQ entry has failed leaving partial information on the queue.
78  *
79  * @param sq
80  *   Pointer to the SQ structure.
81  * @param index
82  *   Index of the freed WQE.
83  * @param num_txbbs
84  *   Number of blocks to stamp.
85  *   If < 0 the routine will use the size written in the WQ entry.
86  * @param owner
87  *   The value of the WQE owner bit to use in the stamp.
88  *
89  * @return
90  *   The number of Tx basic blocs (TXBB) the WQE contained.
91  */
92 static int
93 mlx4_txq_stamp_freed_wqe(struct mlx4_sq *sq, uint16_t index, uint8_t owner)
94 {
95         uint32_t stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
96                                           (!!owner << MLX4_SQ_STAMP_SHIFT));
97         uint8_t *wqe = mlx4_get_send_wqe(sq, (index & sq->txbb_cnt_mask));
98         uint32_t *ptr = (uint32_t *)wqe;
99         int i;
100         int txbbs_size;
101         int num_txbbs;
102
103         /* Extract the size from the control segment of the WQE. */
104         num_txbbs = MLX4_SIZE_TO_TXBBS((((struct mlx4_wqe_ctrl_seg *)
105                                          wqe)->fence_size & 0x3f) << 4);
106         txbbs_size = num_txbbs * MLX4_TXBB_SIZE;
107         /* Optimize the common case when there is no wrap-around. */
108         if (wqe + txbbs_size <= sq->eob) {
109                 /* Stamp the freed descriptor. */
110                 for (i = 0; i < txbbs_size; i += MLX4_SQ_STAMP_STRIDE) {
111                         *ptr = stamp;
112                         ptr += MLX4_SQ_STAMP_DWORDS;
113                 }
114         } else {
115                 /* Stamp the freed descriptor. */
116                 for (i = 0; i < txbbs_size; i += MLX4_SQ_STAMP_STRIDE) {
117                         *ptr = stamp;
118                         ptr += MLX4_SQ_STAMP_DWORDS;
119                         if ((uint8_t *)ptr >= sq->eob) {
120                                 ptr = (uint32_t *)sq->buf;
121                                 stamp ^= RTE_BE32(0x80000000);
122                         }
123                 }
124         }
125         return num_txbbs;
126 }
127
128 /**
129  * Manage Tx completions.
130  *
131  * When sending a burst, mlx4_tx_burst() posts several WRs.
132  * To improve performance, a completion event is only required once every
133  * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
134  * for other WRs, but this information would not be used anyway.
135  *
136  * @param txq
137  *   Pointer to Tx queue structure.
138  *
139  * @return
140  *   0 on success, -1 on failure.
141  */
142 static int
143 mlx4_txq_complete(struct txq *txq)
144 {
145         unsigned int elts_comp = txq->elts_comp;
146         unsigned int elts_tail = txq->elts_tail;
147         const unsigned int elts_n = txq->elts_n;
148         struct mlx4_cq *cq = &txq->mcq;
149         struct mlx4_sq *sq = &txq->msq;
150         struct mlx4_cqe *cqe;
151         uint32_t cons_index = cq->cons_index;
152         uint16_t new_index;
153         uint16_t nr_txbbs = 0;
154         int pkts = 0;
155
156         if (unlikely(elts_comp == 0))
157                 return 0;
158         /*
159          * Traverse over all CQ entries reported and handle each WQ entry
160          * reported by them.
161          */
162         do {
163                 cqe = (struct mlx4_cqe *)mlx4_get_cqe(cq, cons_index);
164                 if (unlikely(!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
165                     !!(cons_index & cq->cqe_cnt)))
166                         break;
167                 /*
168                  * Make sure we read the CQE after we read the ownership bit.
169                  */
170                 rte_rmb();
171 #ifndef NDEBUG
172                 if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
173                              MLX4_CQE_OPCODE_ERROR)) {
174                         struct mlx4_err_cqe *cqe_err =
175                                 (struct mlx4_err_cqe *)cqe;
176                         ERROR("%p CQE error - vendor syndrome: 0x%x"
177                               " syndrome: 0x%x\n",
178                               (void *)txq, cqe_err->vendor_err,
179                               cqe_err->syndrome);
180                 }
181 #endif /* NDEBUG */
182                 /* Get WQE index reported in the CQE. */
183                 new_index =
184                         rte_be_to_cpu_16(cqe->wqe_index) & sq->txbb_cnt_mask;
185                 do {
186                         /* Free next descriptor. */
187                         nr_txbbs +=
188                                 mlx4_txq_stamp_freed_wqe(sq,
189                                      (sq->tail + nr_txbbs) & sq->txbb_cnt_mask,
190                                      !!((sq->tail + nr_txbbs) & sq->txbb_cnt));
191                         pkts++;
192                 } while (((sq->tail + nr_txbbs) & sq->txbb_cnt_mask) !=
193                          new_index);
194                 cons_index++;
195         } while (1);
196         if (unlikely(pkts == 0))
197                 return 0;
198         /*
199          * Update CQ.
200          * To prevent CQ overflow we first update CQ consumer and only then
201          * the ring consumer.
202          */
203         cq->cons_index = cons_index;
204         *cq->set_ci_db = rte_cpu_to_be_32(cq->cons_index & MLX4_CQ_DB_CI_MASK);
205         rte_wmb();
206         sq->tail = sq->tail + nr_txbbs;
207         /* Update the list of packets posted for transmission. */
208         elts_comp -= pkts;
209         assert(elts_comp <= txq->elts_comp);
210         /*
211          * Assume completion status is successful as nothing can be done about
212          * it anyway.
213          */
214         elts_tail += pkts;
215         if (elts_tail >= elts_n)
216                 elts_tail -= elts_n;
217         txq->elts_tail = elts_tail;
218         txq->elts_comp = elts_comp;
219         return 0;
220 }
221
222 /**
223  * Get memory pool (MP) from mbuf. If mbuf is indirect, the pool from which
224  * the cloned mbuf is allocated is returned instead.
225  *
226  * @param buf
227  *   Pointer to mbuf.
228  *
229  * @return
230  *   Memory pool where data is located for given mbuf.
231  */
232 static struct rte_mempool *
233 mlx4_txq_mb2mp(struct rte_mbuf *buf)
234 {
235         if (unlikely(RTE_MBUF_INDIRECT(buf)))
236                 return rte_mbuf_from_indirect(buf)->pool;
237         return buf->pool;
238 }
239
240 /**
241  * DPDK callback for Tx.
242  *
243  * @param dpdk_txq
244  *   Generic pointer to Tx queue structure.
245  * @param[in] pkts
246  *   Packets to transmit.
247  * @param pkts_n
248  *   Number of packets in array.
249  *
250  * @return
251  *   Number of packets successfully transmitted (<= pkts_n).
252  */
253 uint16_t
254 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
255 {
256         struct txq *txq = (struct txq *)dpdk_txq;
257         unsigned int elts_head = txq->elts_head;
258         const unsigned int elts_n = txq->elts_n;
259         unsigned int elts_comp = 0;
260         unsigned int bytes_sent = 0;
261         unsigned int i;
262         unsigned int max;
263         struct mlx4_sq *sq = &txq->msq;
264         struct pv *pv = (struct pv *)txq->bounce_buf;
265
266         assert(txq->elts_comp_cd != 0);
267         mlx4_txq_complete(txq);
268         max = (elts_n - (elts_head - txq->elts_tail));
269         if (max > elts_n)
270                 max -= elts_n;
271         assert(max >= 1);
272         assert(max <= elts_n);
273         /* Always leave one free entry in the ring. */
274         --max;
275         if (max == 0)
276                 return 0;
277         if (max > pkts_n)
278                 max = pkts_n;
279         for (i = 0; (i != max); ++i) {
280                 struct rte_mbuf *buf = pkts[i];
281                 unsigned int elts_head_next =
282                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
283                 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
284                 struct txq_elt *elt = &(*txq->elts)[elts_head];
285                 uint32_t owner_opcode = MLX4_OPCODE_SEND;
286                 struct mlx4_wqe_ctrl_seg *ctrl;
287                 struct mlx4_wqe_data_seg *dseg;
288                 struct rte_mbuf *sbuf;
289                 union {
290                         uint32_t flags;
291                         uint16_t flags16[2];
292                 } srcrb;
293                 uint32_t head_idx = sq->head & sq->txbb_cnt_mask;
294                 uint32_t lkey;
295                 uintptr_t addr;
296                 uint32_t byte_count;
297                 int wqe_real_size;
298                 int nr_txbbs;
299                 int pv_counter = 0;
300
301                 /* Clean up old buffer. */
302                 if (likely(elt->buf != NULL)) {
303                         struct rte_mbuf *tmp = elt->buf;
304
305 #ifndef NDEBUG
306                         /* Poisoning. */
307                         memset(elt, 0x66, sizeof(*elt));
308 #endif
309                         /* Faster than rte_pktmbuf_free(). */
310                         do {
311                                 struct rte_mbuf *next = tmp->next;
312
313                                 rte_pktmbuf_free_seg(tmp);
314                                 tmp = next;
315                         } while (tmp != NULL);
316                 }
317                 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
318                 /*
319                  * Calculate the needed work queue entry size
320                  * for this packet.
321                  */
322                 wqe_real_size = sizeof(struct mlx4_wqe_ctrl_seg) +
323                                 buf->nb_segs * sizeof(struct mlx4_wqe_data_seg);
324                 nr_txbbs = MLX4_SIZE_TO_TXBBS(wqe_real_size);
325                 /*
326                  * Check that there is room for this WQE in the send
327                  * queue and that the WQE size is legal.
328                  */
329                 if (((sq->head - sq->tail) + nr_txbbs +
330                      sq->headroom_txbbs) >= sq->txbb_cnt ||
331                     nr_txbbs > MLX4_MAX_WQE_TXBBS) {
332                         elt->buf = NULL;
333                         break;
334                 }
335                 /* Get the control and data entries of the WQE. */
336                 ctrl = (struct mlx4_wqe_ctrl_seg *)
337                                 mlx4_get_send_wqe(sq, head_idx);
338                 dseg = (struct mlx4_wqe_data_seg *)((uintptr_t)ctrl +
339                                 sizeof(struct mlx4_wqe_ctrl_seg));
340                 /* Fill the data segments with buffer information. */
341                 for (sbuf = buf; sbuf != NULL; sbuf = sbuf->next, dseg++) {
342                         addr = rte_pktmbuf_mtod(sbuf, uintptr_t);
343                         rte_prefetch0((volatile void *)addr);
344                         /* Handle WQE wraparound. */
345                         if (dseg >= (struct mlx4_wqe_data_seg *)sq->eob)
346                                 dseg = (struct mlx4_wqe_data_seg *)sq->buf;
347                         dseg->addr = rte_cpu_to_be_64(addr);
348                         /* Memory region key (big endian). */
349                         lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(sbuf));
350                         dseg->lkey = rte_cpu_to_be_32(lkey);
351 #ifndef NDEBUG
352                         if (unlikely(dseg->lkey ==
353                                 rte_cpu_to_be_32((uint32_t)-1))) {
354                                 /* MR does not exist. */
355                                 DEBUG("%p: unable to get MP <-> MR association",
356                                       (void *)txq);
357                                 /*
358                                  * Restamp entry in case of failure.
359                                  * Make sure that size is written correctly
360                                  * Note that we give ownership to the SW,
361                                  * not the HW.
362                                  */
363                                 ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
364                                 mlx4_txq_stamp_freed_wqe(sq, head_idx,
365                                              (sq->head & sq->txbb_cnt) ? 0 : 1);
366                                 elt->buf = NULL;
367                                 break;
368                         }
369 #endif /* NDEBUG */
370                         if (likely(sbuf->data_len)) {
371                                 byte_count = rte_cpu_to_be_32(sbuf->data_len);
372                         } else {
373                                 /*
374                                  * Zero length segment is treated as inline
375                                  * segment with zero data.
376                                  */
377                                 byte_count = RTE_BE32(0x80000000);
378                         }
379                         /*
380                          * If the data segment is not at the beginning
381                          * of a Tx basic block (TXBB) then write the
382                          * byte count, else postpone the writing to
383                          * just before updating the control segment.
384                          */
385                         if ((uintptr_t)dseg & (uintptr_t)(MLX4_TXBB_SIZE - 1)) {
386                                 /*
387                                  * Need a barrier here before writing the
388                                  * byte_count fields to make sure that all the
389                                  * data is visible before the byte_count field
390                                  * is set. otherwise, if the segment begins a
391                                  * new cacheline, the HCA prefetcher could grab
392                                  * the 64-byte chunk and get a valid
393                                  * (!= 0xffffffff) byte count but stale data,
394                                  * and end up sending the wrong data.
395                                  */
396                                 rte_io_wmb();
397                                 dseg->byte_count = byte_count;
398                         } else {
399                                 /*
400                                  * This data segment starts at the beginning of
401                                  * a new TXBB, so we need to postpone its
402                                  * byte_count writing for later.
403                                  */
404                                 pv[pv_counter].dseg = dseg;
405                                 pv[pv_counter++].val = byte_count;
406                         }
407                 }
408                 /* Write the first DWORD of each TXBB save earlier. */
409                 if (pv_counter) {
410                         /* Need a barrier before writing the byte_count. */
411                         rte_io_wmb();
412                         for (--pv_counter; pv_counter  >= 0; pv_counter--)
413                                 pv[pv_counter].dseg->byte_count =
414                                                 pv[pv_counter].val;
415                 }
416                 /* Fill the control parameters for this packet. */
417                 ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
418                 /*
419                  * For raw Ethernet, the SOLICIT flag is used to indicate
420                  * that no ICRC should be calculated.
421                  */
422                 txq->elts_comp_cd -= nr_txbbs;
423                 if (unlikely(txq->elts_comp_cd <= 0)) {
424                         txq->elts_comp_cd = txq->elts_comp_cd_init;
425                         srcrb.flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT |
426                                                MLX4_WQE_CTRL_CQ_UPDATE);
427                 } else {
428                         srcrb.flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT);
429                 }
430                 /* Enable HW checksum offload if requested */
431                 if (txq->csum &&
432                     (buf->ol_flags &
433                      (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))) {
434                         const uint64_t is_tunneled = (buf->ol_flags &
435                                                       (PKT_TX_TUNNEL_GRE |
436                                                        PKT_TX_TUNNEL_VXLAN));
437
438                         if (is_tunneled && txq->csum_l2tun) {
439                                 owner_opcode |= MLX4_WQE_CTRL_IIP_HDR_CSUM |
440                                                 MLX4_WQE_CTRL_IL4_HDR_CSUM;
441                                 if (buf->ol_flags & PKT_TX_OUTER_IP_CKSUM)
442                                         srcrb.flags |=
443                                             RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM);
444                         } else {
445                                 srcrb.flags |=
446                                         RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM |
447                                                 MLX4_WQE_CTRL_TCP_UDP_CSUM);
448                         }
449                 }
450                 if (txq->lb) {
451                         /*
452                          * Copy destination MAC address to the WQE, this allows
453                          * loopback in eSwitch, so that VFs and PF can
454                          * communicate with each other.
455                          */
456                         srcrb.flags16[0] = *(rte_pktmbuf_mtod(buf, uint16_t *));
457                         ctrl->imm = *(rte_pktmbuf_mtod_offset(buf, uint32_t *,
458                                               sizeof(uint16_t)));
459                 } else {
460                         ctrl->imm = 0;
461                 }
462                 ctrl->srcrb_flags = srcrb.flags;
463                 /*
464                  * Make sure descriptor is fully written before
465                  * setting ownership bit (because HW can start
466                  * executing as soon as we do).
467                  */
468                 rte_wmb();
469                 ctrl->owner_opcode = rte_cpu_to_be_32(owner_opcode |
470                                               ((sq->head & sq->txbb_cnt) ?
471                                                        MLX4_BIT_WQE_OWN : 0));
472                 sq->head += nr_txbbs;
473                 elt->buf = buf;
474                 bytes_sent += buf->pkt_len;
475                 ++elts_comp;
476                 elts_head = elts_head_next;
477         }
478         /* Take a shortcut if nothing must be sent. */
479         if (unlikely(i == 0))
480                 return 0;
481         /* Increment send statistics counters. */
482         txq->stats.opackets += i;
483         txq->stats.obytes += bytes_sent;
484         /* Make sure that descriptors are written before doorbell record. */
485         rte_wmb();
486         /* Ring QP doorbell. */
487         rte_write32(txq->msq.doorbell_qpn, txq->msq.db);
488         txq->elts_head = elts_head;
489         txq->elts_comp += elts_comp;
490         return i;
491 }
492
493 /**
494  * Translate Rx completion flags to packet type.
495  *
496  * @param flags
497  *   Rx completion flags returned by mlx4_cqe_flags().
498  *
499  * @return
500  *   Packet type in mbuf format.
501  */
502 static inline uint32_t
503 rxq_cq_to_pkt_type(uint32_t flags)
504 {
505         uint32_t pkt_type;
506
507         if (flags & MLX4_CQE_L2_TUNNEL)
508                 pkt_type =
509                         mlx4_transpose(flags,
510                                        MLX4_CQE_L2_TUNNEL_IPV4,
511                                        RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
512                         mlx4_transpose(flags,
513                                        MLX4_CQE_STATUS_IPV4_PKT,
514                                        RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN);
515         else
516                 pkt_type = mlx4_transpose(flags,
517                                           MLX4_CQE_STATUS_IPV4_PKT,
518                                           RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
519         return pkt_type;
520 }
521
522 /**
523  * Translate Rx completion flags to offload flags.
524  *
525  * @param flags
526  *   Rx completion flags returned by mlx4_cqe_flags().
527  * @param csum
528  *   Whether Rx checksums are enabled.
529  * @param csum_l2tun
530  *   Whether Rx L2 tunnel checksums are enabled.
531  *
532  * @return
533  *   Offload flags (ol_flags) in mbuf format.
534  */
535 static inline uint32_t
536 rxq_cq_to_ol_flags(uint32_t flags, int csum, int csum_l2tun)
537 {
538         uint32_t ol_flags = 0;
539
540         if (csum)
541                 ol_flags |=
542                         mlx4_transpose(flags,
543                                        MLX4_CQE_STATUS_IP_HDR_CSUM_OK,
544                                        PKT_RX_IP_CKSUM_GOOD) |
545                         mlx4_transpose(flags,
546                                        MLX4_CQE_STATUS_TCP_UDP_CSUM_OK,
547                                        PKT_RX_L4_CKSUM_GOOD);
548         if ((flags & MLX4_CQE_L2_TUNNEL) && csum_l2tun)
549                 ol_flags |=
550                         mlx4_transpose(flags,
551                                        MLX4_CQE_L2_TUNNEL_IPOK,
552                                        PKT_RX_IP_CKSUM_GOOD) |
553                         mlx4_transpose(flags,
554                                        MLX4_CQE_L2_TUNNEL_L4_CSUM,
555                                        PKT_RX_L4_CKSUM_GOOD);
556         return ol_flags;
557 }
558
559 /**
560  * Extract checksum information from CQE flags.
561  *
562  * @param cqe
563  *   Pointer to CQE structure.
564  * @param csum
565  *   Whether Rx checksums are enabled.
566  * @param csum_l2tun
567  *   Whether Rx L2 tunnel checksums are enabled.
568  *
569  * @return
570  *   CQE checksum information.
571  */
572 static inline uint32_t
573 mlx4_cqe_flags(struct mlx4_cqe *cqe, int csum, int csum_l2tun)
574 {
575         uint32_t flags = 0;
576
577         /*
578          * The relevant bits are in different locations on their
579          * CQE fields therefore we can join them in one 32bit
580          * variable.
581          */
582         if (csum)
583                 flags = (rte_be_to_cpu_32(cqe->status) &
584                          MLX4_CQE_STATUS_IPV4_CSUM_OK);
585         if (csum_l2tun)
586                 flags |= (rte_be_to_cpu_32(cqe->vlan_my_qpn) &
587                           (MLX4_CQE_L2_TUNNEL |
588                            MLX4_CQE_L2_TUNNEL_IPOK |
589                            MLX4_CQE_L2_TUNNEL_L4_CSUM |
590                            MLX4_CQE_L2_TUNNEL_IPV4));
591         return flags;
592 }
593
594 /**
595  * Poll one CQE from CQ.
596  *
597  * @param rxq
598  *   Pointer to the receive queue structure.
599  * @param[out] out
600  *   Just polled CQE.
601  *
602  * @return
603  *   Number of bytes of the CQE, 0 in case there is no completion.
604  */
605 static unsigned int
606 mlx4_cq_poll_one(struct rxq *rxq, struct mlx4_cqe **out)
607 {
608         int ret = 0;
609         struct mlx4_cqe *cqe = NULL;
610         struct mlx4_cq *cq = &rxq->mcq;
611
612         cqe = (struct mlx4_cqe *)mlx4_get_cqe(cq, cq->cons_index);
613         if (!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
614             !!(cq->cons_index & cq->cqe_cnt))
615                 goto out;
616         /*
617          * Make sure we read CQ entry contents after we've checked the
618          * ownership bit.
619          */
620         rte_rmb();
621         assert(!(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK));
622         assert((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) !=
623                MLX4_CQE_OPCODE_ERROR);
624         ret = rte_be_to_cpu_32(cqe->byte_cnt);
625         ++cq->cons_index;
626 out:
627         *out = cqe;
628         return ret;
629 }
630
631 /**
632  * DPDK callback for Rx with scattered packets support.
633  *
634  * @param dpdk_rxq
635  *   Generic pointer to Rx queue structure.
636  * @param[out] pkts
637  *   Array to store received packets.
638  * @param pkts_n
639  *   Maximum number of packets in array.
640  *
641  * @return
642  *   Number of packets successfully received (<= pkts_n).
643  */
644 uint16_t
645 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
646 {
647         struct rxq *rxq = dpdk_rxq;
648         const uint32_t wr_cnt = (1 << rxq->elts_n) - 1;
649         const uint16_t sges_n = rxq->sges_n;
650         struct rte_mbuf *pkt = NULL;
651         struct rte_mbuf *seg = NULL;
652         unsigned int i = 0;
653         uint32_t rq_ci = rxq->rq_ci << sges_n;
654         int len = 0;
655
656         while (pkts_n) {
657                 struct mlx4_cqe *cqe;
658                 uint32_t idx = rq_ci & wr_cnt;
659                 struct rte_mbuf *rep = (*rxq->elts)[idx];
660                 volatile struct mlx4_wqe_data_seg *scat = &(*rxq->wqes)[idx];
661
662                 /* Update the 'next' pointer of the previous segment. */
663                 if (pkt)
664                         seg->next = rep;
665                 seg = rep;
666                 rte_prefetch0(seg);
667                 rte_prefetch0(scat);
668                 rep = rte_mbuf_raw_alloc(rxq->mp);
669                 if (unlikely(rep == NULL)) {
670                         ++rxq->stats.rx_nombuf;
671                         if (!pkt) {
672                                 /*
673                                  * No buffers before we even started,
674                                  * bail out silently.
675                                  */
676                                 break;
677                         }
678                         while (pkt != seg) {
679                                 assert(pkt != (*rxq->elts)[idx]);
680                                 rep = pkt->next;
681                                 pkt->next = NULL;
682                                 pkt->nb_segs = 1;
683                                 rte_mbuf_raw_free(pkt);
684                                 pkt = rep;
685                         }
686                         break;
687                 }
688                 if (!pkt) {
689                         /* Looking for the new packet. */
690                         len = mlx4_cq_poll_one(rxq, &cqe);
691                         if (!len) {
692                                 rte_mbuf_raw_free(rep);
693                                 break;
694                         }
695                         if (unlikely(len < 0)) {
696                                 /* Rx error, packet is likely too large. */
697                                 rte_mbuf_raw_free(rep);
698                                 ++rxq->stats.idropped;
699                                 goto skip;
700                         }
701                         pkt = seg;
702                         if (rxq->csum | rxq->csum_l2tun) {
703                                 uint32_t flags =
704                                         mlx4_cqe_flags(cqe,
705                                                        rxq->csum,
706                                                        rxq->csum_l2tun);
707
708                                 pkt->ol_flags =
709                                         rxq_cq_to_ol_flags(flags,
710                                                            rxq->csum,
711                                                            rxq->csum_l2tun);
712                                 pkt->packet_type = rxq_cq_to_pkt_type(flags);
713                         } else {
714                                 pkt->packet_type = 0;
715                                 pkt->ol_flags = 0;
716                         }
717                         pkt->pkt_len = len;
718                 }
719                 rep->nb_segs = 1;
720                 rep->port = rxq->port_id;
721                 rep->data_len = seg->data_len;
722                 rep->data_off = seg->data_off;
723                 (*rxq->elts)[idx] = rep;
724                 /*
725                  * Fill NIC descriptor with the new buffer. The lkey and size
726                  * of the buffers are already known, only the buffer address
727                  * changes.
728                  */
729                 scat->addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(rep, uintptr_t));
730                 if (len > seg->data_len) {
731                         len -= seg->data_len;
732                         ++pkt->nb_segs;
733                         ++rq_ci;
734                         continue;
735                 }
736                 /* The last segment. */
737                 seg->data_len = len;
738                 /* Increment bytes counter. */
739                 rxq->stats.ibytes += pkt->pkt_len;
740                 /* Return packet. */
741                 *(pkts++) = pkt;
742                 pkt = NULL;
743                 --pkts_n;
744                 ++i;
745 skip:
746                 /* Align consumer index to the next stride. */
747                 rq_ci >>= sges_n;
748                 ++rq_ci;
749                 rq_ci <<= sges_n;
750         }
751         if (unlikely(i == 0 && (rq_ci >> sges_n) == rxq->rq_ci))
752                 return 0;
753         /* Update the consumer index. */
754         rxq->rq_ci = rq_ci >> sges_n;
755         rte_wmb();
756         *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
757         *rxq->mcq.set_ci_db =
758                 rte_cpu_to_be_32(rxq->mcq.cons_index & MLX4_CQ_DB_CI_MASK);
759         /* Increment packets counter. */
760         rxq->stats.ipackets += i;
761         return i;
762 }
763
764 /**
765  * Dummy DPDK callback for Tx.
766  *
767  * This function is used to temporarily replace the real callback during
768  * unsafe control operations on the queue, or in case of error.
769  *
770  * @param dpdk_txq
771  *   Generic pointer to Tx queue structure.
772  * @param[in] pkts
773  *   Packets to transmit.
774  * @param pkts_n
775  *   Number of packets in array.
776  *
777  * @return
778  *   Number of packets successfully transmitted (<= pkts_n).
779  */
780 uint16_t
781 mlx4_tx_burst_removed(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
782 {
783         (void)dpdk_txq;
784         (void)pkts;
785         (void)pkts_n;
786         return 0;
787 }
788
789 /**
790  * Dummy DPDK callback for Rx.
791  *
792  * This function is used to temporarily replace the real callback during
793  * unsafe control operations on the queue, or in case of error.
794  *
795  * @param dpdk_rxq
796  *   Generic pointer to Rx queue structure.
797  * @param[out] pkts
798  *   Array to store received packets.
799  * @param pkts_n
800  *   Maximum number of packets in array.
801  *
802  * @return
803  *   Number of packets successfully received (<= pkts_n).
804  */
805 uint16_t
806 mlx4_rx_burst_removed(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
807 {
808         (void)dpdk_rxq;
809         (void)pkts;
810         (void)pkts_n;
811         return 0;
812 }