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