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