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