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"
65 * Pointer-value pair structure used in tx_post_send for saving the first
66 * DWORD (32 byte) of a TXBB.
69 volatile struct mlx4_wqe_data_seg *dseg;
73 /** A table to translate Rx completion flags to packet type. */
74 uint32_t mlx4_ptype_table[0x100] __rte_cache_aligned = {
76 * The index to the array should have:
77 * bit[7] - MLX4_CQE_L2_TUNNEL
78 * bit[6] - MLX4_CQE_L2_TUNNEL_IPV4
79 * bit[5] - MLX4_CQE_STATUS_UDP
80 * bit[4] - MLX4_CQE_STATUS_TCP
81 * bit[3] - MLX4_CQE_STATUS_IPV4OPT
82 * bit[2] - MLX4_CQE_STATUS_IPV6
83 * bit[1] - MLX4_CQE_STATUS_IPV4F
84 * bit[0] - MLX4_CQE_STATUS_IPV4
85 * giving a total of up to 256 entries.
87 [0x00] = RTE_PTYPE_L2_ETHER,
88 [0x01] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
89 [0x02] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
91 [0x03] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
93 [0x04] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
94 [0x09] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT,
95 [0x0a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
97 [0x11] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
99 [0x12] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
101 [0x14] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
103 [0x18] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
105 [0x19] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
107 [0x1a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
109 [0x21] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
111 [0x22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
113 [0x24] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
115 [0x28] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
117 [0x29] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
119 [0x2a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT |
121 /* Tunneled - L3 IPV6 */
122 [0x80] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
123 [0x81] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
124 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
125 [0x82] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
126 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
127 RTE_PTYPE_INNER_L4_FRAG,
128 [0x83] = 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 [0x84] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
132 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
133 [0x88] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
134 RTE_PTYPE_INNER_L3_IPV4_EXT,
135 [0x89] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
136 RTE_PTYPE_INNER_L3_IPV4_EXT,
137 [0x8a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
138 RTE_PTYPE_INNER_L3_IPV4_EXT | RTE_PTYPE_INNER_L4_FRAG,
139 /* Tunneled - L3 IPV6, TCP */
140 [0x91] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
141 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
142 RTE_PTYPE_INNER_L4_TCP,
143 [0x92] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
144 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
145 RTE_PTYPE_INNER_L4_FRAG |
146 RTE_PTYPE_INNER_L4_TCP,
147 [0x93] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
148 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
149 RTE_PTYPE_INNER_L4_FRAG |
150 RTE_PTYPE_INNER_L4_TCP,
151 [0x94] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
152 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
153 RTE_PTYPE_INNER_L4_TCP,
154 [0x98] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
155 RTE_PTYPE_INNER_L3_IPV4_EXT |
156 RTE_PTYPE_INNER_L4_TCP,
157 [0x99] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
158 RTE_PTYPE_INNER_L3_IPV4_EXT |
159 RTE_PTYPE_INNER_L4_TCP,
160 [0x9a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
161 RTE_PTYPE_INNER_L3_IPV4_EXT | RTE_PTYPE_INNER_L4_FRAG |
162 RTE_PTYPE_INNER_L4_TCP,
163 /* Tunneled - L3 IPV6, UDP */
164 [0xa1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
165 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
166 RTE_PTYPE_INNER_L4_UDP,
167 [0xa2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
168 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
169 RTE_PTYPE_INNER_L4_FRAG |
170 RTE_PTYPE_INNER_L4_UDP,
171 [0xa3] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
172 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
173 RTE_PTYPE_INNER_L4_FRAG |
174 RTE_PTYPE_INNER_L4_UDP,
175 [0xa4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
176 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
177 RTE_PTYPE_INNER_L4_UDP,
178 [0xa8] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
179 RTE_PTYPE_INNER_L3_IPV4_EXT |
180 RTE_PTYPE_INNER_L4_UDP,
181 [0xa9] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
182 RTE_PTYPE_INNER_L3_IPV4_EXT |
183 RTE_PTYPE_INNER_L4_UDP,
184 [0xaa] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
185 RTE_PTYPE_INNER_L3_IPV4_EXT | RTE_PTYPE_INNER_L4_FRAG |
186 RTE_PTYPE_INNER_L4_UDP,
187 /* Tunneled - L3 IPV4 */
188 [0xc0] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
189 [0xc1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
190 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
191 [0xc2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
192 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
193 RTE_PTYPE_INNER_L4_FRAG,
194 [0xc3] = 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 [0xc4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
198 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
199 [0xc8] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
200 RTE_PTYPE_INNER_L3_IPV4_EXT,
201 [0xc9] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
202 RTE_PTYPE_INNER_L3_IPV4_EXT,
203 [0xca] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
204 RTE_PTYPE_INNER_L3_IPV4_EXT |
205 RTE_PTYPE_INNER_L4_FRAG,
206 /* Tunneled - L3 IPV4, TCP */
207 [0xd0] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
208 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
209 RTE_PTYPE_INNER_L4_TCP,
210 [0xd1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
211 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
212 RTE_PTYPE_INNER_L4_TCP,
213 [0xd2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
214 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
215 RTE_PTYPE_INNER_L4_FRAG |
216 RTE_PTYPE_INNER_L4_TCP,
217 [0xd3] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
218 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
219 RTE_PTYPE_INNER_L4_FRAG |
220 RTE_PTYPE_INNER_L4_TCP,
221 [0xd4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
222 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
223 RTE_PTYPE_INNER_L4_TCP,
224 [0xd8] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
225 RTE_PTYPE_INNER_L3_IPV4_EXT |
226 RTE_PTYPE_INNER_L4_TCP,
227 [0xd9] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
228 RTE_PTYPE_INNER_L3_IPV4_EXT |
229 RTE_PTYPE_INNER_L4_TCP,
230 [0xda] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
231 RTE_PTYPE_INNER_L3_IPV4_EXT | RTE_PTYPE_INNER_L4_FRAG |
232 RTE_PTYPE_INNER_L4_TCP,
233 /* Tunneled - L3 IPV4, UDP */
234 [0xe0] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
235 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
236 RTE_PTYPE_INNER_L4_UDP,
237 [0xe1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
238 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
239 RTE_PTYPE_INNER_L4_UDP,
240 [0xe2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
241 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
242 RTE_PTYPE_INNER_L4_FRAG |
243 RTE_PTYPE_INNER_L4_UDP,
244 [0xe3] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
245 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
246 RTE_PTYPE_INNER_L4_FRAG |
247 RTE_PTYPE_INNER_L4_UDP,
248 [0xe4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
249 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
250 RTE_PTYPE_INNER_L4_UDP,
251 [0xe8] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
252 RTE_PTYPE_INNER_L3_IPV4_EXT | RTE_PTYPE_INNER_L4_UDP,
253 [0xe9] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
254 RTE_PTYPE_INNER_L3_IPV4_EXT | RTE_PTYPE_INNER_L4_UDP,
255 [0xea] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
256 RTE_PTYPE_INNER_L3_IPV4_EXT | RTE_PTYPE_INNER_L4_FRAG |
257 RTE_PTYPE_INNER_L4_UDP,
261 * Stamp TXBB burst so it won't be reused by the HW.
263 * Routine is used when freeing WQE used by the chip or when failing
264 * building an WQ entry has failed leaving partial information on the queue.
267 * Pointer to the SQ structure.
269 * Pointer to the first TXBB to stamp.
271 * Pointer to the followed end TXBB to stamp.
274 * Stamping burst size in byte units.
277 mlx4_txq_stamp_freed_wqe(struct mlx4_sq *sq, volatile uint32_t *start,
278 volatile uint32_t *end)
280 uint32_t stamp = sq->stamp;
281 int32_t size = (intptr_t)end - (intptr_t)start;
283 assert(start != end);
284 /* Hold SQ ring wrap around. */
286 size = (int32_t)sq->size + size;
289 start += MLX4_SQ_STAMP_DWORDS;
290 } while (start != (volatile uint32_t *)sq->eob);
291 start = (volatile uint32_t *)sq->buf;
292 /* Flip invalid stamping ownership. */
293 stamp ^= RTE_BE32(0x1 << MLX4_SQ_OWNER_BIT);
300 start += MLX4_SQ_STAMP_DWORDS;
301 } while (start != end);
302 return (uint32_t)size;
306 * Manage Tx completions.
308 * When sending a burst, mlx4_tx_burst() posts several WRs.
309 * To improve performance, a completion event is only required once every
310 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
311 * for other WRs, but this information would not be used anyway.
314 * Pointer to Tx queue structure.
317 mlx4_txq_complete(struct txq *txq, const unsigned int elts_n,
320 unsigned int elts_tail = txq->elts_tail;
321 struct mlx4_cq *cq = &txq->mcq;
322 volatile struct mlx4_cqe *cqe;
324 uint32_t cons_index = cq->cons_index;
325 volatile uint32_t *first_txbb;
328 * Traverse over all CQ entries reported and handle each WQ entry
332 cqe = (volatile struct mlx4_cqe *)mlx4_get_cqe(cq, cons_index);
333 if (unlikely(!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
334 !!(cons_index & cq->cqe_cnt)))
338 * Make sure we read the CQE after we read the ownership bit.
341 if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
342 MLX4_CQE_OPCODE_ERROR)) {
343 volatile struct mlx4_err_cqe *cqe_err =
344 (volatile struct mlx4_err_cqe *)cqe;
345 ERROR("%p CQE error - vendor syndrome: 0x%x"
347 (void *)txq, cqe_err->vendor_err,
354 completed = (cons_index - cq->cons_index) * txq->elts_comp_cd_init;
355 if (unlikely(!completed))
357 /* First stamping address is the end of the last one. */
358 first_txbb = (&(*txq->elts)[elts_tail])->eocb;
359 elts_tail += completed;
360 if (elts_tail >= elts_n)
362 /* The new tail element holds the end address. */
363 sq->remain_size += mlx4_txq_stamp_freed_wqe(sq, first_txbb,
364 (&(*txq->elts)[elts_tail])->eocb);
365 /* Update CQ consumer index. */
366 cq->cons_index = cons_index;
367 *cq->set_ci_db = rte_cpu_to_be_32(cons_index & MLX4_CQ_DB_CI_MASK);
368 txq->elts_comp -= completed;
369 txq->elts_tail = elts_tail;
373 * Get memory pool (MP) from mbuf. If mbuf is indirect, the pool from which
374 * the cloned mbuf is allocated is returned instead.
380 * Memory pool where data is located for given mbuf.
382 static struct rte_mempool *
383 mlx4_txq_mb2mp(struct rte_mbuf *buf)
385 if (unlikely(RTE_MBUF_INDIRECT(buf)))
386 return rte_mbuf_from_indirect(buf)->pool;
391 * Write Tx data segment to the SQ.
394 * Pointer to data segment in SQ.
396 * Memory region lkey.
400 * Big endian bytes count of the data to send.
403 mlx4_fill_tx_data_seg(volatile struct mlx4_wqe_data_seg *dseg,
404 uint32_t lkey, uintptr_t addr, rte_be32_t byte_count)
406 dseg->addr = rte_cpu_to_be_64(addr);
407 dseg->lkey = rte_cpu_to_be_32(lkey);
408 #if RTE_CACHE_LINE_SIZE < 64
410 * Need a barrier here before writing the byte_count
411 * fields to make sure that all the data is visible
412 * before the byte_count field is set.
413 * Otherwise, if the segment begins a new cacheline,
414 * the HCA prefetcher could grab the 64-byte chunk and
415 * get a valid (!= 0xffffffff) byte count but stale
416 * data, and end up sending the wrong data.
419 #endif /* RTE_CACHE_LINE_SIZE */
420 dseg->byte_count = byte_count;
424 * Write data segments of multi-segment packet.
427 * Pointer to the first packet mbuf.
429 * Pointer to Tx queue structure.
431 * Pointer to the WQE control segment.
434 * Pointer to the next WQE control segment on success, NULL otherwise.
436 static volatile struct mlx4_wqe_ctrl_seg *
437 mlx4_tx_burst_segs(struct rte_mbuf *buf, struct txq *txq,
438 volatile struct mlx4_wqe_ctrl_seg *ctrl)
440 struct pv *pv = (struct pv *)txq->bounce_buf;
441 struct mlx4_sq *sq = &txq->msq;
442 struct rte_mbuf *sbuf = buf;
445 int nb_segs = buf->nb_segs;
447 volatile struct mlx4_wqe_data_seg *dseg =
448 (volatile struct mlx4_wqe_data_seg *)(ctrl + 1);
450 ctrl->fence_size = 1 + nb_segs;
451 wqe_size = RTE_ALIGN((uint32_t)(ctrl->fence_size << MLX4_SEG_SHIFT),
453 /* Validate WQE size and WQE space in the send queue. */
454 if (sq->remain_size < wqe_size ||
455 wqe_size > MLX4_MAX_WQE_SIZE)
458 * Fill the data segments with buffer information.
459 * First WQE TXBB head segment is always control segment,
460 * so jump to tail TXBB data segments code for the first
461 * WQE data segments filling.
465 /* Memory region key (big endian) for this memory pool. */
466 lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(sbuf));
467 if (unlikely(lkey == (uint32_t)-1)) {
468 DEBUG("%p: unable to get MP <-> MR association",
472 /* Handle WQE wraparound. */
474 (volatile struct mlx4_wqe_data_seg *)sq->eob)
475 dseg = (volatile struct mlx4_wqe_data_seg *)
477 dseg->addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(sbuf, uintptr_t));
478 dseg->lkey = rte_cpu_to_be_32(lkey);
480 * This data segment starts at the beginning of a new
481 * TXBB, so we need to postpone its byte_count writing
484 pv[pv_counter].dseg = dseg;
486 * Zero length segment is treated as inline segment
489 pv[pv_counter++].val = rte_cpu_to_be_32(sbuf->data_len ?
490 sbuf->data_len : 0x80000000);
495 /* Jump to default if there are more than two segments remaining. */
498 lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(sbuf));
499 if (unlikely(lkey == (uint32_t)-1)) {
500 DEBUG("%p: unable to get MP <-> MR association",
504 mlx4_fill_tx_data_seg(dseg, lkey,
505 rte_pktmbuf_mtod(sbuf, uintptr_t),
506 rte_cpu_to_be_32(sbuf->data_len ?
514 lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(sbuf));
515 if (unlikely(lkey == (uint32_t)-1)) {
516 DEBUG("%p: unable to get MP <-> MR association",
520 mlx4_fill_tx_data_seg(dseg, lkey,
521 rte_pktmbuf_mtod(sbuf, uintptr_t),
522 rte_cpu_to_be_32(sbuf->data_len ?
530 lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(sbuf));
531 if (unlikely(lkey == (uint32_t)-1)) {
532 DEBUG("%p: unable to get MP <-> MR association",
536 mlx4_fill_tx_data_seg(dseg, lkey,
537 rte_pktmbuf_mtod(sbuf, uintptr_t),
538 rte_cpu_to_be_32(sbuf->data_len ?
551 /* Write the first DWORD of each TXBB save earlier. */
553 /* Need a barrier here before writing the byte_count. */
555 for (--pv_counter; pv_counter >= 0; pv_counter--)
556 pv[pv_counter].dseg->byte_count = pv[pv_counter].val;
558 sq->remain_size -= wqe_size;
559 /* Align next WQE address to the next TXBB. */
560 return (volatile struct mlx4_wqe_ctrl_seg *)
561 ((volatile uint8_t *)ctrl + wqe_size);
565 * DPDK callback for Tx.
568 * Generic pointer to Tx queue structure.
570 * Packets to transmit.
572 * Number of packets in array.
575 * Number of packets successfully transmitted (<= pkts_n).
578 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
580 struct txq *txq = (struct txq *)dpdk_txq;
581 unsigned int elts_head = txq->elts_head;
582 const unsigned int elts_n = txq->elts_n;
583 unsigned int bytes_sent = 0;
586 struct mlx4_sq *sq = &txq->msq;
587 volatile struct mlx4_wqe_ctrl_seg *ctrl;
590 assert(txq->elts_comp_cd != 0);
591 if (likely(txq->elts_comp != 0))
592 mlx4_txq_complete(txq, elts_n, sq);
593 max = (elts_n - (elts_head - txq->elts_tail));
597 assert(max <= elts_n);
598 /* Always leave one free entry in the ring. */
602 elt = &(*txq->elts)[elts_head];
603 /* First Tx burst element saves the next WQE control segment. */
605 for (i = 0; (i != max); ++i) {
606 struct rte_mbuf *buf = pkts[i];
607 unsigned int elts_head_next =
608 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
609 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
610 uint32_t owner_opcode = sq->owner_opcode;
611 volatile struct mlx4_wqe_data_seg *dseg =
612 (volatile struct mlx4_wqe_data_seg *)(ctrl + 1);
613 volatile struct mlx4_wqe_ctrl_seg *ctrl_next;
620 /* Clean up old buffer. */
621 if (likely(elt->buf != NULL)) {
622 struct rte_mbuf *tmp = elt->buf;
626 memset(&elt->buf, 0x66, sizeof(struct rte_mbuf *));
628 /* Faster than rte_pktmbuf_free(). */
630 struct rte_mbuf *next = tmp->next;
632 rte_pktmbuf_free_seg(tmp);
634 } while (tmp != NULL);
636 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
637 if (buf->nb_segs == 1) {
638 /* Validate WQE space in the send queue. */
639 if (sq->remain_size < MLX4_TXBB_SIZE) {
643 lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(buf));
644 if (unlikely(lkey == (uint32_t)-1)) {
645 /* MR does not exist. */
646 DEBUG("%p: unable to get MP <-> MR association",
651 mlx4_fill_tx_data_seg(dseg++, lkey,
652 rte_pktmbuf_mtod(buf, uintptr_t),
653 rte_cpu_to_be_32(buf->data_len));
654 /* Set WQE size in 16-byte units. */
655 ctrl->fence_size = 0x2;
656 sq->remain_size -= MLX4_TXBB_SIZE;
657 /* Align next WQE address to the next TXBB. */
658 ctrl_next = ctrl + 0x4;
660 ctrl_next = mlx4_tx_burst_segs(buf, txq, ctrl);
666 /* Hold SQ ring wrap around. */
667 if ((volatile uint8_t *)ctrl_next >= sq->eob) {
668 ctrl_next = (volatile struct mlx4_wqe_ctrl_seg *)
669 ((volatile uint8_t *)ctrl_next - sq->size);
670 /* Flip HW valid ownership. */
671 sq->owner_opcode ^= 0x1 << MLX4_SQ_OWNER_BIT;
674 * For raw Ethernet, the SOLICIT flag is used to indicate
675 * that no ICRC should be calculated.
677 if (--txq->elts_comp_cd == 0) {
678 /* Save the completion burst end address. */
679 elt_next->eocb = (volatile uint32_t *)ctrl_next;
680 txq->elts_comp_cd = txq->elts_comp_cd_init;
681 srcrb.flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT |
682 MLX4_WQE_CTRL_CQ_UPDATE);
684 srcrb.flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT);
686 /* Enable HW checksum offload if requested */
689 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))) {
690 const uint64_t is_tunneled = (buf->ol_flags &
692 PKT_TX_TUNNEL_VXLAN));
694 if (is_tunneled && txq->csum_l2tun) {
695 owner_opcode |= MLX4_WQE_CTRL_IIP_HDR_CSUM |
696 MLX4_WQE_CTRL_IL4_HDR_CSUM;
697 if (buf->ol_flags & PKT_TX_OUTER_IP_CKSUM)
699 RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM);
702 RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM |
703 MLX4_WQE_CTRL_TCP_UDP_CSUM);
708 * Copy destination MAC address to the WQE, this allows
709 * loopback in eSwitch, so that VFs and PF can
710 * communicate with each other.
712 srcrb.flags16[0] = *(rte_pktmbuf_mtod(buf, uint16_t *));
713 ctrl->imm = *(rte_pktmbuf_mtod_offset(buf, uint32_t *,
718 ctrl->srcrb_flags = srcrb.flags;
720 * Make sure descriptor is fully written before
721 * setting ownership bit (because HW can start
722 * executing as soon as we do).
725 ctrl->owner_opcode = rte_cpu_to_be_32(owner_opcode);
727 bytes_sent += buf->pkt_len;
728 elts_head = elts_head_next;
732 /* Take a shortcut if nothing must be sent. */
733 if (unlikely(i == 0))
735 /* Save WQE address of the next Tx burst element. */
737 /* Increment send statistics counters. */
738 txq->stats.opackets += i;
739 txq->stats.obytes += bytes_sent;
740 /* Make sure that descriptors are written before doorbell record. */
742 /* Ring QP doorbell. */
743 rte_write32(txq->msq.doorbell_qpn, txq->msq.db);
744 txq->elts_head = elts_head;
750 * Translate Rx completion flags to packet type.
756 * Packet type for struct rte_mbuf.
758 static inline uint32_t
759 rxq_cq_to_pkt_type(volatile struct mlx4_cqe *cqe,
760 uint32_t l2tun_offload)
763 uint32_t pinfo = rte_be_to_cpu_32(cqe->vlan_my_qpn);
764 uint32_t status = rte_be_to_cpu_32(cqe->status);
767 * The index to the array should have:
768 * bit[7] - MLX4_CQE_L2_TUNNEL
769 * bit[6] - MLX4_CQE_L2_TUNNEL_IPV4
771 if (l2tun_offload && (pinfo & MLX4_CQE_L2_TUNNEL))
772 idx |= ((pinfo & MLX4_CQE_L2_TUNNEL) >> 20) |
773 ((pinfo & MLX4_CQE_L2_TUNNEL_IPV4) >> 19);
775 * The index to the array should have:
776 * bit[5] - MLX4_CQE_STATUS_UDP
777 * bit[4] - MLX4_CQE_STATUS_TCP
778 * bit[3] - MLX4_CQE_STATUS_IPV4OPT
779 * bit[2] - MLX4_CQE_STATUS_IPV6
780 * bit[1] - MLX4_CQE_STATUS_IPV4F
781 * bit[0] - MLX4_CQE_STATUS_IPV4
782 * giving a total of up to 256 entries.
784 idx |= ((status & MLX4_CQE_STATUS_PTYPE_MASK) >> 22);
785 return mlx4_ptype_table[idx];
789 * Translate Rx completion flags to offload flags.
792 * Rx completion flags returned by mlx4_cqe_flags().
794 * Whether Rx checksums are enabled.
796 * Whether Rx L2 tunnel checksums are enabled.
799 * Offload flags (ol_flags) in mbuf format.
801 static inline uint32_t
802 rxq_cq_to_ol_flags(uint32_t flags, int csum, int csum_l2tun)
804 uint32_t ol_flags = 0;
808 mlx4_transpose(flags,
809 MLX4_CQE_STATUS_IP_HDR_CSUM_OK,
810 PKT_RX_IP_CKSUM_GOOD) |
811 mlx4_transpose(flags,
812 MLX4_CQE_STATUS_TCP_UDP_CSUM_OK,
813 PKT_RX_L4_CKSUM_GOOD);
814 if ((flags & MLX4_CQE_L2_TUNNEL) && csum_l2tun)
816 mlx4_transpose(flags,
817 MLX4_CQE_L2_TUNNEL_IPOK,
818 PKT_RX_IP_CKSUM_GOOD) |
819 mlx4_transpose(flags,
820 MLX4_CQE_L2_TUNNEL_L4_CSUM,
821 PKT_RX_L4_CKSUM_GOOD);
826 * Extract checksum information from CQE flags.
829 * Pointer to CQE structure.
831 * Whether Rx checksums are enabled.
833 * Whether Rx L2 tunnel checksums are enabled.
836 * CQE checksum information.
838 static inline uint32_t
839 mlx4_cqe_flags(volatile struct mlx4_cqe *cqe, int csum, int csum_l2tun)
844 * The relevant bits are in different locations on their
845 * CQE fields therefore we can join them in one 32bit
849 flags = (rte_be_to_cpu_32(cqe->status) &
850 MLX4_CQE_STATUS_IPV4_CSUM_OK);
852 flags |= (rte_be_to_cpu_32(cqe->vlan_my_qpn) &
853 (MLX4_CQE_L2_TUNNEL |
854 MLX4_CQE_L2_TUNNEL_IPOK |
855 MLX4_CQE_L2_TUNNEL_L4_CSUM |
856 MLX4_CQE_L2_TUNNEL_IPV4));
861 * Poll one CQE from CQ.
864 * Pointer to the receive queue structure.
869 * Number of bytes of the CQE, 0 in case there is no completion.
872 mlx4_cq_poll_one(struct rxq *rxq, volatile struct mlx4_cqe **out)
875 volatile struct mlx4_cqe *cqe = NULL;
876 struct mlx4_cq *cq = &rxq->mcq;
878 cqe = (volatile struct mlx4_cqe *)mlx4_get_cqe(cq, cq->cons_index);
879 if (!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
880 !!(cq->cons_index & cq->cqe_cnt))
883 * Make sure we read CQ entry contents after we've checked the
887 assert(!(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK));
888 assert((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) !=
889 MLX4_CQE_OPCODE_ERROR);
890 ret = rte_be_to_cpu_32(cqe->byte_cnt);
898 * DPDK callback for Rx with scattered packets support.
901 * Generic pointer to Rx queue structure.
903 * Array to store received packets.
905 * Maximum number of packets in array.
908 * Number of packets successfully received (<= pkts_n).
911 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
913 struct rxq *rxq = dpdk_rxq;
914 const uint32_t wr_cnt = (1 << rxq->elts_n) - 1;
915 const uint16_t sges_n = rxq->sges_n;
916 struct rte_mbuf *pkt = NULL;
917 struct rte_mbuf *seg = NULL;
919 uint32_t rq_ci = rxq->rq_ci << sges_n;
923 volatile struct mlx4_cqe *cqe;
924 uint32_t idx = rq_ci & wr_cnt;
925 struct rte_mbuf *rep = (*rxq->elts)[idx];
926 volatile struct mlx4_wqe_data_seg *scat = &(*rxq->wqes)[idx];
928 /* Update the 'next' pointer of the previous segment. */
934 rep = rte_mbuf_raw_alloc(rxq->mp);
935 if (unlikely(rep == NULL)) {
936 ++rxq->stats.rx_nombuf;
939 * No buffers before we even started,
945 assert(pkt != (*rxq->elts)[idx]);
949 rte_mbuf_raw_free(pkt);
955 /* Looking for the new packet. */
956 len = mlx4_cq_poll_one(rxq, &cqe);
958 rte_mbuf_raw_free(rep);
961 if (unlikely(len < 0)) {
962 /* Rx error, packet is likely too large. */
963 rte_mbuf_raw_free(rep);
964 ++rxq->stats.idropped;
968 /* Update packet information. */
970 rxq_cq_to_pkt_type(cqe, rxq->l2tun_offload);
971 pkt->ol_flags = PKT_RX_RSS_HASH;
972 pkt->hash.rss = cqe->immed_rss_invalid;
974 if (rxq->csum | rxq->csum_l2tun) {
981 rxq_cq_to_ol_flags(flags,
987 rep->port = rxq->port_id;
988 rep->data_len = seg->data_len;
989 rep->data_off = seg->data_off;
990 (*rxq->elts)[idx] = rep;
992 * Fill NIC descriptor with the new buffer. The lkey and size
993 * of the buffers are already known, only the buffer address
996 scat->addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(rep, uintptr_t));
997 if (len > seg->data_len) {
998 len -= seg->data_len;
1003 /* The last segment. */
1004 seg->data_len = len;
1005 /* Increment bytes counter. */
1006 rxq->stats.ibytes += pkt->pkt_len;
1007 /* Return packet. */
1013 /* Align consumer index to the next stride. */
1018 if (unlikely(i == 0 && (rq_ci >> sges_n) == rxq->rq_ci))
1020 /* Update the consumer index. */
1021 rxq->rq_ci = rq_ci >> sges_n;
1023 *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
1024 *rxq->mcq.set_ci_db =
1025 rte_cpu_to_be_32(rxq->mcq.cons_index & MLX4_CQ_DB_CI_MASK);
1026 /* Increment packets counter. */
1027 rxq->stats.ipackets += i;
1032 * Dummy DPDK callback for Tx.
1034 * This function is used to temporarily replace the real callback during
1035 * unsafe control operations on the queue, or in case of error.
1038 * Generic pointer to Tx queue structure.
1040 * Packets to transmit.
1042 * Number of packets in array.
1045 * Number of packets successfully transmitted (<= pkts_n).
1048 mlx4_tx_burst_removed(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1057 * Dummy DPDK callback for Rx.
1059 * This function is used to temporarily replace the real callback during
1060 * unsafe control operations on the queue, or in case of error.
1063 * Generic pointer to Rx queue structure.
1065 * Array to store received packets.
1067 * Maximum number of packets in array.
1070 * Number of packets successfully received (<= pkts_n).
1073 mlx4_rx_burst_removed(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)