net/mlx4: fix ring wraparound compiler hint
[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  * Posts a single work request to a send queue.
242  *
243  * @param txq
244  *   Target Tx queue.
245  * @param pkt
246  *   Packet to transmit.
247  *
248  * @return
249  *   0 on success, negative errno value otherwise.
250  */
251 static inline int
252 mlx4_post_send(struct txq *txq, struct rte_mbuf *pkt)
253 {
254         struct mlx4_wqe_ctrl_seg *ctrl;
255         struct mlx4_wqe_data_seg *dseg;
256         struct mlx4_sq *sq = &txq->msq;
257         struct rte_mbuf *buf;
258         union {
259                 uint32_t flags;
260                 uint16_t flags16[2];
261         } srcrb;
262         uint32_t head_idx = sq->head & sq->txbb_cnt_mask;
263         uint32_t lkey;
264         uintptr_t addr;
265         uint32_t owner_opcode = MLX4_OPCODE_SEND;
266         uint32_t byte_count;
267         int wqe_real_size;
268         int nr_txbbs;
269         struct pv *pv = (struct pv *)txq->bounce_buf;
270         int pv_counter = 0;
271
272         /* Calculate the needed work queue entry size for this packet. */
273         wqe_real_size = sizeof(struct mlx4_wqe_ctrl_seg) +
274                         pkt->nb_segs * sizeof(struct mlx4_wqe_data_seg);
275         nr_txbbs = MLX4_SIZE_TO_TXBBS(wqe_real_size);
276         /*
277          * Check that there is room for this WQE in the send queue and that
278          * the WQE size is legal.
279          */
280         if (((sq->head - sq->tail) + nr_txbbs +
281              sq->headroom_txbbs) >= sq->txbb_cnt ||
282             nr_txbbs > MLX4_MAX_WQE_TXBBS) {
283                 return -ENOSPC;
284         }
285         /* Get the control and data entries of the WQE. */
286         ctrl = (struct mlx4_wqe_ctrl_seg *)mlx4_get_send_wqe(sq, head_idx);
287         dseg = (struct mlx4_wqe_data_seg *)((uintptr_t)ctrl +
288                                             sizeof(struct mlx4_wqe_ctrl_seg));
289         /* Fill the data segments with buffer information. */
290         for (buf = pkt; buf != NULL; buf = buf->next, dseg++) {
291                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
292                 rte_prefetch0((volatile void *)addr);
293                 /* Handle WQE wraparound. */
294                 if (dseg >= (struct mlx4_wqe_data_seg *)sq->eob)
295                         dseg = (struct mlx4_wqe_data_seg *)sq->buf;
296                 dseg->addr = rte_cpu_to_be_64(addr);
297                 /* Memory region key for this memory pool. */
298                 lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(buf));
299 #ifndef NDEBUG
300                 if (unlikely(lkey == (uint32_t)-1)) {
301                         /* MR does not exist. */
302                         DEBUG("%p: unable to get MP <-> MR association",
303                               (void *)txq);
304                         /*
305                          * Restamp entry in case of failure.
306                          * Make sure that size is written correctly
307                          * Note that we give ownership to the SW, not the HW.
308                          */
309                         ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
310                         mlx4_txq_stamp_freed_wqe(sq, head_idx,
311                                      (sq->head & sq->txbb_cnt) ? 0 : 1);
312                         return -EFAULT;
313                 }
314 #endif /* NDEBUG */
315                 dseg->lkey = rte_cpu_to_be_32(lkey);
316                 if (likely(buf->data_len)) {
317                         byte_count = rte_cpu_to_be_32(buf->data_len);
318                 } else {
319                         /*
320                          * Zero length segment is treated as inline segment
321                          * with zero data.
322                          */
323                         byte_count = RTE_BE32(0x80000000);
324                 }
325                 /*
326                  * If the data segment is not at the beginning of a
327                  * Tx basic block (TXBB) then write the byte count,
328                  * else postpone the writing to just before updating the
329                  * control segment.
330                  */
331                 if ((uintptr_t)dseg & (uintptr_t)(MLX4_TXBB_SIZE - 1)) {
332                         /*
333                          * Need a barrier here before writing the byte_count
334                          * fields to make sure that all the data is visible
335                          * before the byte_count field is set.
336                          * Otherwise, if the segment begins a new cacheline,
337                          * the HCA prefetcher could grab the 64-byte chunk and
338                          * get a valid (!= 0xffffffff) byte count but stale
339                          * data, and end up sending the wrong data.
340                          */
341                         rte_io_wmb();
342                         dseg->byte_count = byte_count;
343                 } else {
344                         /*
345                          * This data segment starts at the beginning of a new
346                          * TXBB, so we need to postpone its byte_count writing
347                          * for later.
348                          */
349                         pv[pv_counter].dseg = dseg;
350                         pv[pv_counter++].val = byte_count;
351                 }
352         }
353         /* Write the first DWORD of each TXBB save earlier. */
354         if (pv_counter) {
355                 /* Need a barrier here before writing the byte_count. */
356                 rte_io_wmb();
357                 for (--pv_counter; pv_counter  >= 0; pv_counter--)
358                         pv[pv_counter].dseg->byte_count = pv[pv_counter].val;
359         }
360         /* Fill the control parameters for this packet. */
361         ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
362         /*
363          * For raw Ethernet, the SOLICIT flag is used to indicate that no ICRC
364          * should be calculated.
365          */
366         txq->elts_comp_cd -= nr_txbbs;
367         if (unlikely(txq->elts_comp_cd <= 0)) {
368                 txq->elts_comp_cd = txq->elts_comp_cd_init;
369                 srcrb.flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT |
370                                        MLX4_WQE_CTRL_CQ_UPDATE);
371         } else {
372                 srcrb.flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT);
373         }
374         /* Enable HW checksum offload if requested */
375         if (txq->csum &&
376             (pkt->ol_flags &
377              (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))) {
378                 const uint64_t is_tunneled = (pkt->ol_flags &
379                                               (PKT_TX_TUNNEL_GRE |
380                                                PKT_TX_TUNNEL_VXLAN));
381
382                 if (is_tunneled && txq->csum_l2tun) {
383                         owner_opcode |= MLX4_WQE_CTRL_IIP_HDR_CSUM |
384                                         MLX4_WQE_CTRL_IL4_HDR_CSUM;
385                         if (pkt->ol_flags & PKT_TX_OUTER_IP_CKSUM)
386                                 srcrb.flags |=
387                                         RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM);
388                 } else {
389                         srcrb.flags |= RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM |
390                                                 MLX4_WQE_CTRL_TCP_UDP_CSUM);
391                 }
392         }
393         if (txq->lb) {
394                 /*
395                  * Copy destination MAC address to the WQE, this allows
396                  * loopback in eSwitch, so that VFs and PF can communicate
397                  * with each other.
398                  */
399                 srcrb.flags16[0] = *(rte_pktmbuf_mtod(pkt, uint16_t *));
400                 ctrl->imm = *(rte_pktmbuf_mtod_offset(pkt, uint32_t *,
401                                                       sizeof(uint16_t)));
402         } else {
403                 ctrl->imm = 0;
404         }
405         ctrl->srcrb_flags = srcrb.flags;
406         /*
407          * Make sure descriptor is fully written before
408          * setting ownership bit (because HW can start
409          * executing as soon as we do).
410          */
411         rte_wmb();
412         ctrl->owner_opcode = rte_cpu_to_be_32(owner_opcode |
413                                               ((sq->head & sq->txbb_cnt) ?
414                                                MLX4_BIT_WQE_OWN : 0));
415         sq->head += nr_txbbs;
416         return 0;
417 }
418
419 /**
420  * DPDK callback for Tx.
421  *
422  * @param dpdk_txq
423  *   Generic pointer to Tx queue structure.
424  * @param[in] pkts
425  *   Packets to transmit.
426  * @param pkts_n
427  *   Number of packets in array.
428  *
429  * @return
430  *   Number of packets successfully transmitted (<= pkts_n).
431  */
432 uint16_t
433 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
434 {
435         struct txq *txq = (struct txq *)dpdk_txq;
436         unsigned int elts_head = txq->elts_head;
437         const unsigned int elts_n = txq->elts_n;
438         unsigned int elts_comp = 0;
439         unsigned int bytes_sent = 0;
440         unsigned int i;
441         unsigned int max;
442         int err;
443
444         assert(txq->elts_comp_cd != 0);
445         mlx4_txq_complete(txq);
446         max = (elts_n - (elts_head - txq->elts_tail));
447         if (max > elts_n)
448                 max -= elts_n;
449         assert(max >= 1);
450         assert(max <= elts_n);
451         /* Always leave one free entry in the ring. */
452         --max;
453         if (max == 0)
454                 return 0;
455         if (max > pkts_n)
456                 max = pkts_n;
457         for (i = 0; (i != max); ++i) {
458                 struct rte_mbuf *buf = pkts[i];
459                 unsigned int elts_head_next =
460                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
461                 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
462                 struct txq_elt *elt = &(*txq->elts)[elts_head];
463
464                 /* Clean up old buffer. */
465                 if (likely(elt->buf != NULL)) {
466                         struct rte_mbuf *tmp = elt->buf;
467
468 #ifndef NDEBUG
469                         /* Poisoning. */
470                         memset(elt, 0x66, sizeof(*elt));
471 #endif
472                         /* Faster than rte_pktmbuf_free(). */
473                         do {
474                                 struct rte_mbuf *next = tmp->next;
475
476                                 rte_pktmbuf_free_seg(tmp);
477                                 tmp = next;
478                         } while (tmp != NULL);
479                 }
480                 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
481                 /* Post the packet for sending. */
482                 err = mlx4_post_send(txq, buf);
483                 if (unlikely(err)) {
484                         elt->buf = NULL;
485                         goto stop;
486                 }
487                 elt->buf = buf;
488                 bytes_sent += buf->pkt_len;
489                 ++elts_comp;
490                 elts_head = elts_head_next;
491         }
492 stop:
493         /* Take a shortcut if nothing must be sent. */
494         if (unlikely(i == 0))
495                 return 0;
496         /* Increment send statistics counters. */
497         txq->stats.opackets += i;
498         txq->stats.obytes += bytes_sent;
499         /* Make sure that descriptors are written before doorbell record. */
500         rte_wmb();
501         /* Ring QP doorbell. */
502         rte_write32(txq->msq.doorbell_qpn, txq->msq.db);
503         txq->elts_head = elts_head;
504         txq->elts_comp += elts_comp;
505         return i;
506 }
507
508 /**
509  * Translate Rx completion flags to packet type.
510  *
511  * @param flags
512  *   Rx completion flags returned by mlx4_cqe_flags().
513  *
514  * @return
515  *   Packet type in mbuf format.
516  */
517 static inline uint32_t
518 rxq_cq_to_pkt_type(uint32_t flags)
519 {
520         uint32_t pkt_type;
521
522         if (flags & MLX4_CQE_L2_TUNNEL)
523                 pkt_type =
524                         mlx4_transpose(flags,
525                                        MLX4_CQE_L2_TUNNEL_IPV4,
526                                        RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
527                         mlx4_transpose(flags,
528                                        MLX4_CQE_STATUS_IPV4_PKT,
529                                        RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN);
530         else
531                 pkt_type = mlx4_transpose(flags,
532                                           MLX4_CQE_STATUS_IPV4_PKT,
533                                           RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
534         return pkt_type;
535 }
536
537 /**
538  * Translate Rx completion flags to offload flags.
539  *
540  * @param flags
541  *   Rx completion flags returned by mlx4_cqe_flags().
542  * @param csum
543  *   Whether Rx checksums are enabled.
544  * @param csum_l2tun
545  *   Whether Rx L2 tunnel checksums are enabled.
546  *
547  * @return
548  *   Offload flags (ol_flags) in mbuf format.
549  */
550 static inline uint32_t
551 rxq_cq_to_ol_flags(uint32_t flags, int csum, int csum_l2tun)
552 {
553         uint32_t ol_flags = 0;
554
555         if (csum)
556                 ol_flags |=
557                         mlx4_transpose(flags,
558                                        MLX4_CQE_STATUS_IP_HDR_CSUM_OK,
559                                        PKT_RX_IP_CKSUM_GOOD) |
560                         mlx4_transpose(flags,
561                                        MLX4_CQE_STATUS_TCP_UDP_CSUM_OK,
562                                        PKT_RX_L4_CKSUM_GOOD);
563         if ((flags & MLX4_CQE_L2_TUNNEL) && csum_l2tun)
564                 ol_flags |=
565                         mlx4_transpose(flags,
566                                        MLX4_CQE_L2_TUNNEL_IPOK,
567                                        PKT_RX_IP_CKSUM_GOOD) |
568                         mlx4_transpose(flags,
569                                        MLX4_CQE_L2_TUNNEL_L4_CSUM,
570                                        PKT_RX_L4_CKSUM_GOOD);
571         return ol_flags;
572 }
573
574 /**
575  * Extract checksum information from CQE flags.
576  *
577  * @param cqe
578  *   Pointer to CQE structure.
579  * @param csum
580  *   Whether Rx checksums are enabled.
581  * @param csum_l2tun
582  *   Whether Rx L2 tunnel checksums are enabled.
583  *
584  * @return
585  *   CQE checksum information.
586  */
587 static inline uint32_t
588 mlx4_cqe_flags(struct mlx4_cqe *cqe, int csum, int csum_l2tun)
589 {
590         uint32_t flags = 0;
591
592         /*
593          * The relevant bits are in different locations on their
594          * CQE fields therefore we can join them in one 32bit
595          * variable.
596          */
597         if (csum)
598                 flags = (rte_be_to_cpu_32(cqe->status) &
599                          MLX4_CQE_STATUS_IPV4_CSUM_OK);
600         if (csum_l2tun)
601                 flags |= (rte_be_to_cpu_32(cqe->vlan_my_qpn) &
602                           (MLX4_CQE_L2_TUNNEL |
603                            MLX4_CQE_L2_TUNNEL_IPOK |
604                            MLX4_CQE_L2_TUNNEL_L4_CSUM |
605                            MLX4_CQE_L2_TUNNEL_IPV4));
606         return flags;
607 }
608
609 /**
610  * Poll one CQE from CQ.
611  *
612  * @param rxq
613  *   Pointer to the receive queue structure.
614  * @param[out] out
615  *   Just polled CQE.
616  *
617  * @return
618  *   Number of bytes of the CQE, 0 in case there is no completion.
619  */
620 static unsigned int
621 mlx4_cq_poll_one(struct rxq *rxq, struct mlx4_cqe **out)
622 {
623         int ret = 0;
624         struct mlx4_cqe *cqe = NULL;
625         struct mlx4_cq *cq = &rxq->mcq;
626
627         cqe = (struct mlx4_cqe *)mlx4_get_cqe(cq, cq->cons_index);
628         if (!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
629             !!(cq->cons_index & cq->cqe_cnt))
630                 goto out;
631         /*
632          * Make sure we read CQ entry contents after we've checked the
633          * ownership bit.
634          */
635         rte_rmb();
636         assert(!(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK));
637         assert((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) !=
638                MLX4_CQE_OPCODE_ERROR);
639         ret = rte_be_to_cpu_32(cqe->byte_cnt);
640         ++cq->cons_index;
641 out:
642         *out = cqe;
643         return ret;
644 }
645
646 /**
647  * DPDK callback for Rx with scattered packets support.
648  *
649  * @param dpdk_rxq
650  *   Generic pointer to Rx queue structure.
651  * @param[out] pkts
652  *   Array to store received packets.
653  * @param pkts_n
654  *   Maximum number of packets in array.
655  *
656  * @return
657  *   Number of packets successfully received (<= pkts_n).
658  */
659 uint16_t
660 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
661 {
662         struct rxq *rxq = dpdk_rxq;
663         const uint32_t wr_cnt = (1 << rxq->elts_n) - 1;
664         const uint16_t sges_n = rxq->sges_n;
665         struct rte_mbuf *pkt = NULL;
666         struct rte_mbuf *seg = NULL;
667         unsigned int i = 0;
668         uint32_t rq_ci = rxq->rq_ci << sges_n;
669         int len = 0;
670
671         while (pkts_n) {
672                 struct mlx4_cqe *cqe;
673                 uint32_t idx = rq_ci & wr_cnt;
674                 struct rte_mbuf *rep = (*rxq->elts)[idx];
675                 volatile struct mlx4_wqe_data_seg *scat = &(*rxq->wqes)[idx];
676
677                 /* Update the 'next' pointer of the previous segment. */
678                 if (pkt)
679                         seg->next = rep;
680                 seg = rep;
681                 rte_prefetch0(seg);
682                 rte_prefetch0(scat);
683                 rep = rte_mbuf_raw_alloc(rxq->mp);
684                 if (unlikely(rep == NULL)) {
685                         ++rxq->stats.rx_nombuf;
686                         if (!pkt) {
687                                 /*
688                                  * No buffers before we even started,
689                                  * bail out silently.
690                                  */
691                                 break;
692                         }
693                         while (pkt != seg) {
694                                 assert(pkt != (*rxq->elts)[idx]);
695                                 rep = pkt->next;
696                                 pkt->next = NULL;
697                                 pkt->nb_segs = 1;
698                                 rte_mbuf_raw_free(pkt);
699                                 pkt = rep;
700                         }
701                         break;
702                 }
703                 if (!pkt) {
704                         /* Looking for the new packet. */
705                         len = mlx4_cq_poll_one(rxq, &cqe);
706                         if (!len) {
707                                 rte_mbuf_raw_free(rep);
708                                 break;
709                         }
710                         if (unlikely(len < 0)) {
711                                 /* Rx error, packet is likely too large. */
712                                 rte_mbuf_raw_free(rep);
713                                 ++rxq->stats.idropped;
714                                 goto skip;
715                         }
716                         pkt = seg;
717                         if (rxq->csum | rxq->csum_l2tun) {
718                                 uint32_t flags =
719                                         mlx4_cqe_flags(cqe,
720                                                        rxq->csum,
721                                                        rxq->csum_l2tun);
722
723                                 pkt->ol_flags =
724                                         rxq_cq_to_ol_flags(flags,
725                                                            rxq->csum,
726                                                            rxq->csum_l2tun);
727                                 pkt->packet_type = rxq_cq_to_pkt_type(flags);
728                         } else {
729                                 pkt->packet_type = 0;
730                                 pkt->ol_flags = 0;
731                         }
732                         pkt->pkt_len = len;
733                 }
734                 rep->nb_segs = 1;
735                 rep->port = rxq->port_id;
736                 rep->data_len = seg->data_len;
737                 rep->data_off = seg->data_off;
738                 (*rxq->elts)[idx] = rep;
739                 /*
740                  * Fill NIC descriptor with the new buffer. The lkey and size
741                  * of the buffers are already known, only the buffer address
742                  * changes.
743                  */
744                 scat->addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(rep, uintptr_t));
745                 if (len > seg->data_len) {
746                         len -= seg->data_len;
747                         ++pkt->nb_segs;
748                         ++rq_ci;
749                         continue;
750                 }
751                 /* The last segment. */
752                 seg->data_len = len;
753                 /* Increment bytes counter. */
754                 rxq->stats.ibytes += pkt->pkt_len;
755                 /* Return packet. */
756                 *(pkts++) = pkt;
757                 pkt = NULL;
758                 --pkts_n;
759                 ++i;
760 skip:
761                 /* Align consumer index to the next stride. */
762                 rq_ci >>= sges_n;
763                 ++rq_ci;
764                 rq_ci <<= sges_n;
765         }
766         if (unlikely(i == 0 && (rq_ci >> sges_n) == rxq->rq_ci))
767                 return 0;
768         /* Update the consumer index. */
769         rxq->rq_ci = rq_ci >> sges_n;
770         rte_wmb();
771         *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
772         *rxq->mcq.set_ci_db =
773                 rte_cpu_to_be_32(rxq->mcq.cons_index & MLX4_CQ_DB_CI_MASK);
774         /* Increment packets counter. */
775         rxq->stats.ipackets += i;
776         return i;
777 }
778
779 /**
780  * Dummy DPDK callback for Tx.
781  *
782  * This function is used to temporarily replace the real callback during
783  * unsafe control operations on the queue, or in case of error.
784  *
785  * @param dpdk_txq
786  *   Generic pointer to Tx queue structure.
787  * @param[in] pkts
788  *   Packets to transmit.
789  * @param pkts_n
790  *   Number of packets in array.
791  *
792  * @return
793  *   Number of packets successfully transmitted (<= pkts_n).
794  */
795 uint16_t
796 mlx4_tx_burst_removed(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
797 {
798         (void)dpdk_txq;
799         (void)pkts;
800         (void)pkts_n;
801         return 0;
802 }
803
804 /**
805  * Dummy DPDK callback for Rx.
806  *
807  * This function is used to temporarily replace the real callback during
808  * unsafe control operations on the queue, or in case of error.
809  *
810  * @param dpdk_rxq
811  *   Generic pointer to Rx queue structure.
812  * @param[out] pkts
813  *   Array to store received packets.
814  * @param pkts_n
815  *   Maximum number of packets in array.
816  *
817  * @return
818  *   Number of packets successfully received (<= pkts_n).
819  */
820 uint16_t
821 mlx4_rx_burst_removed(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
822 {
823         (void)dpdk_rxq;
824         (void)pkts;
825         (void)pkts_n;
826         return 0;
827 }