4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 * Data plane functions for mlx4 driver.
43 /* Verbs headers do not support -pedantic. */
45 #pragma GCC diagnostic ignored "-Wpedantic"
47 #include <infiniband/verbs.h>
49 #pragma GCC diagnostic error "-Wpedantic"
52 #include <rte_branch_prediction.h>
53 #include <rte_common.h>
56 #include <rte_mempool.h>
57 #include <rte_prefetch.h>
61 #include "mlx4_rxtx.h"
62 #include "mlx4_utils.h"
64 #define WQE_ONE_DATA_SEG_SIZE \
65 (sizeof(struct mlx4_wqe_ctrl_seg) + sizeof(struct mlx4_wqe_data_seg))
68 * Pointer-value pair structure used in tx_post_send for saving the first
69 * DWORD (32 byte) of a TXBB.
72 volatile struct mlx4_wqe_data_seg *dseg;
76 /** A table to translate Rx completion flags to packet type. */
77 uint32_t mlx4_ptype_table[0x100] __rte_cache_aligned = {
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.
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 |
94 [0x03] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
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 |
100 [0x11] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
102 [0x12] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
104 [0x14] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
106 [0x18] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
108 [0x19] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
110 [0x1a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
112 [0x21] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
114 [0x22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
116 [0x24] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
118 [0x28] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
120 [0x29] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
122 [0x2a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
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,
264 * Stamp a WQE so it won't be reused by the HW.
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.
270 * Pointer to the SQ structure.
272 * Index of the freed WQE.
274 * Number of blocks to stamp.
275 * If < 0 the routine will use the size written in the WQ entry.
277 * The value of the WQE owner bit to use in the stamp.
280 * The number of Tx basic blocs (TXBB) the WQE contained.
283 mlx4_txq_stamp_freed_wqe(struct mlx4_sq *sq, uint16_t index, uint8_t owner)
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;
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) {
303 ptr += MLX4_SQ_STAMP_DWORDS;
306 /* Stamp the freed descriptor. */
307 for (i = 0; i < txbbs_size; i += MLX4_SQ_STAMP_STRIDE) {
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);
320 * Manage Tx completions.
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.
328 * Pointer to Tx queue structure.
331 * 0 on success, -1 on failure.
334 mlx4_txq_complete(struct txq *txq, const unsigned int elts_n,
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;
344 uint16_t nr_txbbs = 0;
348 * Traverse over all CQ entries reported and handle each WQ entry
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)))
357 * Make sure we read the CQE after we read the ownership bit.
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"
367 (void *)txq, cqe_err->vendor_err,
371 /* Get WQE index reported in the CQE. */
373 rte_be_to_cpu_16(cqe->wqe_index) & sq->txbb_cnt_mask;
375 /* Free next descriptor. */
378 mlx4_txq_stamp_freed_wqe(sq,
379 sq_tail & sq->txbb_cnt_mask,
380 !!(sq_tail & sq->txbb_cnt));
382 } while ((sq_tail & sq->txbb_cnt_mask) != new_index);
385 if (unlikely(pkts == 0))
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. */
393 assert(elts_comp <= txq->elts_comp);
395 * Assume completion status is successful as nothing can be done about
399 if (elts_tail >= elts_n)
401 txq->elts_tail = elts_tail;
402 txq->elts_comp = elts_comp;
407 * Get memory pool (MP) from mbuf. If mbuf is indirect, the pool from which
408 * the cloned mbuf is allocated is returned instead.
414 * Memory pool where data is located for given mbuf.
416 static struct rte_mempool *
417 mlx4_txq_mb2mp(struct rte_mbuf *buf)
419 if (unlikely(RTE_MBUF_INDIRECT(buf)))
420 return rte_mbuf_from_indirect(buf)->pool;
425 mlx4_tx_burst_segs(struct rte_mbuf *buf, struct txq *txq,
426 volatile struct mlx4_wqe_ctrl_seg **pctrl)
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;
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);
446 * Check that there is room for this WQE in the send queue and that
447 * the WQE size is legal.
449 if (((sq->head - sq->tail) + nr_txbbs +
450 sq->headroom_txbbs) >= sq->txbb_cnt ||
451 nr_txbbs > MLX4_MAX_WQE_TXBBS) {
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));
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);
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",
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.
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);
490 if (likely(sbuf->data_len)) {
491 byte_count = rte_cpu_to_be_32(sbuf->data_len);
494 * Zero length segment is treated as inline segment
497 byte_count = RTE_BE32(0x80000000);
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
505 if ((uintptr_t)dseg & (uintptr_t)(MLX4_TXBB_SIZE - 1)) {
506 #if RTE_CACHE_LINE_SIZE < 64
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.
517 #endif /* RTE_CACHE_LINE_SIZE */
518 dseg->byte_count = byte_count;
521 * This data segment starts at the beginning of a new
522 * TXBB, so we need to postpone its byte_count writing
525 pv[pv_counter].dseg = dseg;
526 pv[pv_counter++].val = byte_count;
529 /* Write the first DWORD of each TXBB save earlier. */
531 /* Need a barrier here before writing the byte_count. */
533 for (--pv_counter; pv_counter >= 0; pv_counter--)
534 pv[pv_counter].dseg->byte_count = pv[pv_counter].val;
536 /* Fill the control parameters for this packet. */
537 ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
542 * DPDK callback for Tx.
545 * Generic pointer to Tx queue structure.
547 * Packets to transmit.
549 * Number of packets in array.
552 * Number of packets successfully transmitted (<= pkts_n).
555 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
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;
563 struct mlx4_sq *sq = &txq->msq;
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));
573 assert(max <= elts_n);
574 /* Always leave one free entry in the ring. */
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;
591 uint32_t head_idx = sq->head & sq->txbb_cnt_mask;
595 /* Clean up old buffer. */
596 if (likely(elt->buf != NULL)) {
597 struct rte_mbuf *tmp = elt->buf;
601 memset(elt, 0x66, sizeof(*elt));
603 /* Faster than rte_pktmbuf_free(). */
605 struct rte_mbuf *next = tmp->next;
607 rte_pktmbuf_free_seg(tmp);
609 } while (tmp != NULL);
611 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
612 if (buf->nb_segs == 1) {
614 * Check that there is room for this WQE in the send
615 * queue and that the WQE size is legal
617 if (((sq->head - sq->tail) + 1 + sq->headroom_txbbs) >=
618 sq->txbb_cnt || 1 > MLX4_MAX_WQE_TXBBS) {
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 *)
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. */
632 (volatile struct mlx4_wqe_data_seg *)sq->eob)
633 dseg = (volatile struct mlx4_wqe_data_seg *)
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);
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",
646 * Restamp entry in case of failure.
647 * Make sure that size is written correctly
648 * Note that we give ownership to the SW,
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);
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;
665 nr_txbbs = mlx4_tx_burst_segs(buf, txq, &ctrl);
672 * For raw Ethernet, the SOLICIT flag is used to indicate
673 * that no ICRC should be calculated.
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);
681 srcrb.flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT);
683 /* Enable HW checksum offload if requested */
686 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))) {
687 const uint64_t is_tunneled = (buf->ol_flags &
689 PKT_TX_TUNNEL_VXLAN));
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)
696 RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM);
699 RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM |
700 MLX4_WQE_CTRL_TCP_UDP_CSUM);
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.
709 srcrb.flags16[0] = *(rte_pktmbuf_mtod(buf, uint16_t *));
710 ctrl->imm = *(rte_pktmbuf_mtod_offset(buf, uint32_t *,
715 ctrl->srcrb_flags = srcrb.flags;
717 * Make sure descriptor is fully written before
718 * setting ownership bit (because HW can start
719 * executing as soon as we do).
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;
727 bytes_sent += buf->pkt_len;
728 elts_head = elts_head_next;
730 /* Take a shortcut if nothing must be sent. */
731 if (unlikely(i == 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. */
738 /* Ring QP doorbell. */
739 rte_write32(txq->msq.doorbell_qpn, txq->msq.db);
740 txq->elts_head = elts_head;
746 * Translate Rx completion flags to packet type.
752 * Packet type for struct rte_mbuf.
754 static inline uint32_t
755 rxq_cq_to_pkt_type(volatile struct mlx4_cqe *cqe,
756 uint32_t l2tun_offload)
759 uint32_t pinfo = rte_be_to_cpu_32(cqe->vlan_my_qpn);
760 uint32_t status = rte_be_to_cpu_32(cqe->status);
763 * The index to the array should have:
764 * bit[7] - MLX4_CQE_L2_TUNNEL
765 * bit[6] - MLX4_CQE_L2_TUNNEL_IPV4
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);
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.
780 idx |= ((status & MLX4_CQE_STATUS_PTYPE_MASK) >> 22);
781 return mlx4_ptype_table[idx];
785 * Translate Rx completion flags to offload flags.
788 * Rx completion flags returned by mlx4_cqe_flags().
790 * Whether Rx checksums are enabled.
792 * Whether Rx L2 tunnel checksums are enabled.
795 * Offload flags (ol_flags) in mbuf format.
797 static inline uint32_t
798 rxq_cq_to_ol_flags(uint32_t flags, int csum, int csum_l2tun)
800 uint32_t ol_flags = 0;
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)
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);
822 * Extract checksum information from CQE flags.
825 * Pointer to CQE structure.
827 * Whether Rx checksums are enabled.
829 * Whether Rx L2 tunnel checksums are enabled.
832 * CQE checksum information.
834 static inline uint32_t
835 mlx4_cqe_flags(volatile struct mlx4_cqe *cqe, int csum, int csum_l2tun)
840 * The relevant bits are in different locations on their
841 * CQE fields therefore we can join them in one 32bit
845 flags = (rte_be_to_cpu_32(cqe->status) &
846 MLX4_CQE_STATUS_IPV4_CSUM_OK);
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));
857 * Poll one CQE from CQ.
860 * Pointer to the receive queue structure.
865 * Number of bytes of the CQE, 0 in case there is no completion.
868 mlx4_cq_poll_one(struct rxq *rxq, volatile struct mlx4_cqe **out)
871 volatile struct mlx4_cqe *cqe = NULL;
872 struct mlx4_cq *cq = &rxq->mcq;
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))
879 * Make sure we read CQ entry contents after we've checked the
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);
894 * DPDK callback for Rx with scattered packets support.
897 * Generic pointer to Rx queue structure.
899 * Array to store received packets.
901 * Maximum number of packets in array.
904 * Number of packets successfully received (<= pkts_n).
907 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
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;
915 uint32_t rq_ci = rxq->rq_ci << sges_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];
924 /* Update the 'next' pointer of the previous segment. */
930 rep = rte_mbuf_raw_alloc(rxq->mp);
931 if (unlikely(rep == NULL)) {
932 ++rxq->stats.rx_nombuf;
935 * No buffers before we even started,
941 assert(pkt != (*rxq->elts)[idx]);
945 rte_mbuf_raw_free(pkt);
951 /* Looking for the new packet. */
952 len = mlx4_cq_poll_one(rxq, &cqe);
954 rte_mbuf_raw_free(rep);
957 if (unlikely(len < 0)) {
958 /* Rx error, packet is likely too large. */
959 rte_mbuf_raw_free(rep);
960 ++rxq->stats.idropped;
964 /* Update packet information. */
966 rxq_cq_to_pkt_type(cqe, rxq->l2tun_offload);
969 if (rxq->csum | rxq->csum_l2tun) {
976 rxq_cq_to_ol_flags(flags,
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;
987 * Fill NIC descriptor with the new buffer. The lkey and size
988 * of the buffers are already known, only the buffer address
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;
998 /* The last segment. */
1000 /* Increment bytes counter. */
1001 rxq->stats.ibytes += pkt->pkt_len;
1002 /* Return packet. */
1008 /* Align consumer index to the next stride. */
1013 if (unlikely(i == 0 && (rq_ci >> sges_n) == rxq->rq_ci))
1015 /* Update the consumer index. */
1016 rxq->rq_ci = rq_ci >> sges_n;
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;
1027 * Dummy DPDK callback for Tx.
1029 * This function is used to temporarily replace the real callback during
1030 * unsafe control operations on the queue, or in case of error.
1033 * Generic pointer to Tx queue structure.
1035 * Packets to transmit.
1037 * Number of packets in array.
1040 * Number of packets successfully transmitted (<= pkts_n).
1043 mlx4_tx_burst_removed(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1052 * Dummy DPDK callback for Rx.
1054 * This function is used to temporarily replace the real callback during
1055 * unsafe control operations on the queue, or in case of error.
1058 * Generic pointer to Rx queue structure.
1060 * Array to store received packets.
1062 * Maximum number of packets in array.
1065 * Number of packets successfully received (<= pkts_n).
1068 mlx4_rx_burst_removed(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)