1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
6 #ifndef RTE_PMD_MLX5_RXTX_VEC_SSE_H_
7 #define RTE_PMD_MLX5_RXTX_VEC_SSE_H_
13 #include <smmintrin.h>
16 #include <rte_mempool.h>
17 #include <rte_prefetch.h>
20 #include "mlx5_utils.h"
21 #include "mlx5_rxtx.h"
22 #include "mlx5_rxtx_vec.h"
23 #include "mlx5_autoconf.h"
24 #include "mlx5_defs.h"
27 #ifndef __INTEL_COMPILER
28 #pragma GCC diagnostic ignored "-Wcast-qual"
32 * Fill in buffer descriptors in a multi-packet send descriptor.
35 * Pointer to TX queue structure.
37 * Pointer to buffer descriptor to be written.
39 * Pointer to array of packets to be sent.
41 * Number of packets to be filled.
44 txq_wr_dseg_v(struct mlx5_txq_data *txq, __m128i *dseg,
45 struct rte_mbuf **pkts, unsigned int n)
49 const __m128i shuf_mask_dseg =
50 _mm_set_epi8(8, 9, 10, 11, /* addr, bswap64 */
52 7, 6, 5, 4, /* lkey */
53 0, 1, 2, 3 /* length, bswap32 */);
54 #ifdef MLX5_PMD_SOFT_COUNTERS
58 for (pos = 0; pos < n; ++pos, ++dseg) {
60 struct rte_mbuf *pkt = pkts[pos];
62 addr = rte_pktmbuf_mtod(pkt, uintptr_t);
63 desc = _mm_set_epi32(addr >> 32,
65 mlx5_tx_mb2mr(txq, pkt),
67 desc = _mm_shuffle_epi8(desc, shuf_mask_dseg);
68 _mm_store_si128(dseg, desc);
69 #ifdef MLX5_PMD_SOFT_COUNTERS
70 tx_byte += DATA_LEN(pkt);
73 #ifdef MLX5_PMD_SOFT_COUNTERS
74 txq->stats.obytes += tx_byte;
79 * Send multi-segmented packets until it encounters a single segment packet in
83 * Pointer to TX queue structure.
85 * Pointer to array of packets to be sent.
87 * Number of packets to be sent.
90 * Number of packets successfully transmitted (<= pkts_n).
93 txq_scatter_v(struct mlx5_txq_data *txq, struct rte_mbuf **pkts,
96 uint16_t elts_head = txq->elts_head;
97 const uint16_t elts_n = 1 << txq->elts_n;
98 const uint16_t elts_m = elts_n - 1;
99 const uint16_t wq_n = 1 << txq->wqe_n;
100 const uint16_t wq_mask = wq_n - 1;
101 const unsigned int nb_dword_per_wqebb =
102 MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE;
103 const unsigned int nb_dword_in_hdr =
104 sizeof(struct mlx5_wqe) / MLX5_WQE_DWORD_SIZE;
106 volatile struct mlx5_wqe *wqe = NULL;
108 assert(elts_n > pkts_n);
109 mlx5_tx_complete(txq);
110 if (unlikely(!pkts_n))
112 for (n = 0; n < pkts_n; ++n) {
113 struct rte_mbuf *buf = pkts[n];
114 unsigned int segs_n = buf->nb_segs;
115 unsigned int ds = nb_dword_in_hdr;
116 unsigned int len = PKT_LEN(buf);
117 uint16_t wqe_ci = txq->wqe_ci;
118 const __m128i shuf_mask_ctrl =
119 _mm_set_epi8(15, 14, 13, 12,
120 8, 9, 10, 11, /* bswap32 */
121 4, 5, 6, 7, /* bswap32 */
122 0, 1, 2, 3 /* bswap32 */);
126 __m128i *t_wqe, *dseg;
130 max_elts = elts_n - (elts_head - txq->elts_tail);
131 max_wqe = wq_n - (txq->wqe_ci - txq->wqe_pi);
133 * A MPW session consumes 2 WQEs at most to
134 * include MLX5_MPW_DSEG_MAX pointers.
137 max_elts < segs_n || max_wqe < 2)
139 if (segs_n > MLX5_MPW_DSEG_MAX) {
140 txq->stats.oerrors++;
143 wqe = &((volatile struct mlx5_wqe64 *)
144 txq->wqes)[wqe_ci & wq_mask].hdr;
145 cs_flags = txq_ol_cksum_to_cs(buf);
146 /* Title WQEBB pointer. */
147 t_wqe = (__m128i *)wqe;
148 dseg = (__m128i *)(wqe + 1);
150 if (!(ds++ % nb_dword_per_wqebb)) {
152 &((volatile struct mlx5_wqe64 *)
153 txq->wqes)[++wqe_ci & wq_mask];
155 txq_wr_dseg_v(txq, dseg++, &buf, 1);
156 (*txq->elts)[elts_head++ & elts_m] = buf;
160 /* Fill CTRL in the header. */
161 ctrl = _mm_set_epi32(0, 0, txq->qp_num_8s | ds,
162 MLX5_OPC_MOD_MPW << 24 |
163 txq->wqe_ci << 8 | MLX5_OPCODE_TSO);
164 ctrl = _mm_shuffle_epi8(ctrl, shuf_mask_ctrl);
165 _mm_store_si128(t_wqe, ctrl);
166 /* Fill ESEG in the header. */
167 _mm_store_si128(t_wqe + 1,
168 _mm_set_epi16(0, 0, 0, 0,
169 rte_cpu_to_be_16(len), cs_flags,
171 txq->wqe_ci = wqe_ci;
175 txq->elts_comp += (uint16_t)(elts_head - txq->elts_head);
176 txq->elts_head = elts_head;
177 if (txq->elts_comp >= MLX5_TX_COMP_THRESH) {
178 /* A CQE slot must always be available. */
179 assert((1u << txq->cqe_n) - (txq->cq_pi++ - txq->cq_ci));
180 wqe->ctrl[2] = rte_cpu_to_be_32(8);
181 wqe->ctrl[3] = txq->elts_head;
184 #ifdef MLX5_PMD_SOFT_COUNTERS
185 txq->stats.opackets += n;
187 mlx5_tx_dbrec(txq, wqe);
192 * Send burst of packets with Enhanced MPW. If it encounters a multi-seg packet,
193 * it returns to make it processed by txq_scatter_v(). All the packets in
194 * the pkts list should be single segment packets having same offload flags.
195 * This must be checked by txq_count_contig_single_seg() and txq_calc_offload().
198 * Pointer to TX queue structure.
200 * Pointer to array of packets to be sent.
202 * Number of packets to be sent (<= MLX5_VPMD_TX_MAX_BURST).
204 * Checksum offload flags to be written in the descriptor.
207 * Number of packets successfully transmitted (<= pkts_n).
209 static inline uint16_t
210 txq_burst_v(struct mlx5_txq_data *txq, struct rte_mbuf **pkts, uint16_t pkts_n,
213 struct rte_mbuf **elts;
214 uint16_t elts_head = txq->elts_head;
215 const uint16_t elts_n = 1 << txq->elts_n;
216 const uint16_t elts_m = elts_n - 1;
217 const unsigned int nb_dword_per_wqebb =
218 MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE;
219 const unsigned int nb_dword_in_hdr =
220 sizeof(struct mlx5_wqe) / MLX5_WQE_DWORD_SIZE;
225 uint32_t comp_req = 0;
226 const uint16_t wq_n = 1 << txq->wqe_n;
227 const uint16_t wq_mask = wq_n - 1;
228 uint16_t wq_idx = txq->wqe_ci & wq_mask;
229 volatile struct mlx5_wqe64 *wq =
230 &((volatile struct mlx5_wqe64 *)txq->wqes)[wq_idx];
231 volatile struct mlx5_wqe *wqe = (volatile struct mlx5_wqe *)wq;
232 const __m128i shuf_mask_ctrl =
233 _mm_set_epi8(15, 14, 13, 12,
234 8, 9, 10, 11, /* bswap32 */
235 4, 5, 6, 7, /* bswap32 */
236 0, 1, 2, 3 /* bswap32 */);
237 __m128i *t_wqe, *dseg;
240 /* Make sure all packets can fit into a single WQE. */
241 assert(elts_n > pkts_n);
242 mlx5_tx_complete(txq);
243 max_elts = (elts_n - (elts_head - txq->elts_tail));
244 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
245 pkts_n = RTE_MIN((unsigned int)RTE_MIN(pkts_n, max_wqe), max_elts);
246 assert(pkts_n <= MLX5_DSEG_MAX - nb_dword_in_hdr);
247 if (unlikely(!pkts_n))
249 elts = &(*txq->elts)[elts_head & elts_m];
250 /* Loop for available tailroom first. */
251 n = RTE_MIN(elts_n - (elts_head & elts_m), pkts_n);
252 for (pos = 0; pos < (n & -2); pos += 2)
253 _mm_storeu_si128((__m128i *)&elts[pos],
254 _mm_loadu_si128((__m128i *)&pkts[pos]));
256 elts[pos] = pkts[pos];
257 /* Check if it crosses the end of the queue. */
258 if (unlikely(n < pkts_n)) {
259 elts = &(*txq->elts)[0];
260 for (pos = 0; pos < pkts_n - n; ++pos)
261 elts[pos] = pkts[n + pos];
263 txq->elts_head += pkts_n;
264 /* Save title WQEBB pointer. */
265 t_wqe = (__m128i *)wqe;
266 dseg = (__m128i *)(wqe + 1);
267 /* Calculate the number of entries to the end. */
269 (wq_n - wq_idx) * nb_dword_per_wqebb - nb_dword_in_hdr,
272 txq_wr_dseg_v(txq, dseg, pkts, n);
273 /* Check if it crosses the end of the queue. */
275 dseg = (__m128i *)txq->wqes;
276 txq_wr_dseg_v(txq, dseg, &pkts[n], pkts_n - n);
278 if (txq->elts_comp + pkts_n < MLX5_TX_COMP_THRESH) {
279 txq->elts_comp += pkts_n;
281 /* A CQE slot must always be available. */
282 assert((1u << txq->cqe_n) - (txq->cq_pi++ - txq->cq_ci));
283 /* Request a completion. */
287 /* Fill CTRL in the header. */
288 ctrl = _mm_set_epi32(txq->elts_head, comp_req,
289 txq->qp_num_8s | (pkts_n + 2),
290 MLX5_OPC_MOD_ENHANCED_MPSW << 24 |
291 txq->wqe_ci << 8 | MLX5_OPCODE_ENHANCED_MPSW);
292 ctrl = _mm_shuffle_epi8(ctrl, shuf_mask_ctrl);
293 _mm_store_si128(t_wqe, ctrl);
294 /* Fill ESEG in the header. */
295 _mm_store_si128(t_wqe + 1,
296 _mm_set_epi8(0, 0, 0, 0,
300 #ifdef MLX5_PMD_SOFT_COUNTERS
301 txq->stats.opackets += pkts_n;
303 txq->wqe_ci += (nb_dword_in_hdr + pkts_n + (nb_dword_per_wqebb - 1)) /
305 /* Ring QP doorbell. */
306 mlx5_tx_dbrec_cond_wmb(txq, wqe, pkts_n < MLX5_VPMD_TX_MAX_BURST);
311 * Store free buffers to RX SW ring.
314 * Pointer to RX queue structure.
316 * Pointer to array of packets to be stored.
318 * Number of packets to be stored.
321 rxq_copy_mbuf_v(struct mlx5_rxq_data *rxq, struct rte_mbuf **pkts, uint16_t n)
323 const uint16_t q_mask = (1 << rxq->elts_n) - 1;
324 struct rte_mbuf **elts = &(*rxq->elts)[rxq->rq_pi & q_mask];
328 for (pos = 0; pos < p; pos += 2) {
331 mbp = _mm_loadu_si128((__m128i *)&elts[pos]);
332 _mm_storeu_si128((__m128i *)&pkts[pos], mbp);
335 pkts[pos] = elts[pos];
339 * Decompress a compressed completion and fill in mbufs in RX SW ring with data
340 * extracted from the title completion descriptor.
343 * Pointer to RX queue structure.
345 * Pointer to completion array having a compressed completion at first.
347 * Pointer to SW ring to be filled. The first mbuf has to be pre-built from
348 * the title completion descriptor to be copied to the rest of mbufs.
351 rxq_cq_decompress_v(struct mlx5_rxq_data *rxq, volatile struct mlx5_cqe *cq,
352 struct rte_mbuf **elts)
354 volatile struct mlx5_mini_cqe8 *mcq = (void *)(cq + 1);
355 struct rte_mbuf *t_pkt = elts[0]; /* Title packet is pre-built. */
358 unsigned int inv = 0;
359 /* Mask to shuffle from extracted mini CQE to mbuf. */
360 const __m128i shuf_mask1 =
361 _mm_set_epi8(0, 1, 2, 3, /* rss, bswap32 */
362 -1, -1, /* skip vlan_tci */
363 6, 7, /* data_len, bswap16 */
364 -1, -1, 6, 7, /* pkt_len, bswap16 */
365 -1, -1, -1, -1 /* skip packet_type */);
366 const __m128i shuf_mask2 =
367 _mm_set_epi8(8, 9, 10, 11, /* rss, bswap32 */
368 -1, -1, /* skip vlan_tci */
369 14, 15, /* data_len, bswap16 */
370 -1, -1, 14, 15, /* pkt_len, bswap16 */
371 -1, -1, -1, -1 /* skip packet_type */);
372 /* Restore the compressed count. Must be 16 bits. */
373 const uint16_t mcqe_n = t_pkt->data_len +
374 (rxq->crc_present * ETHER_CRC_LEN);
375 const __m128i rearm =
376 _mm_loadu_si128((__m128i *)&t_pkt->rearm_data);
378 _mm_loadu_si128((__m128i *)&t_pkt->rx_descriptor_fields1);
379 const __m128i crc_adj =
380 _mm_set_epi16(0, 0, 0,
381 rxq->crc_present * ETHER_CRC_LEN,
383 rxq->crc_present * ETHER_CRC_LEN,
385 const uint32_t flow_tag = t_pkt->hash.fdir.hi;
386 #ifdef MLX5_PMD_SOFT_COUNTERS
387 const __m128i zero = _mm_setzero_si128();
388 const __m128i ones = _mm_cmpeq_epi32(zero, zero);
389 uint32_t rcvd_byte = 0;
390 /* Mask to shuffle byte_cnt to add up stats. Do bswap16 for all. */
391 const __m128i len_shuf_mask =
392 _mm_set_epi8(-1, -1, -1, -1,
399 * A. load mCQEs into a 128bit register.
400 * B. store rearm data to mbuf.
401 * C. combine data from mCQEs with rx_descriptor_fields1.
402 * D. store rx_descriptor_fields1.
403 * E. store flow tag (rte_flow mark).
405 for (pos = 0; pos < mcqe_n; ) {
406 __m128i mcqe1, mcqe2;
407 __m128i rxdf1, rxdf2;
408 #ifdef MLX5_PMD_SOFT_COUNTERS
409 __m128i byte_cnt, invalid_mask;
412 if (!(pos & 0x7) && pos + 8 < mcqe_n)
413 rte_prefetch0((void *)(cq + pos + 8));
414 /* A.1 load mCQEs into a 128bit register. */
415 mcqe1 = _mm_loadu_si128((__m128i *)&mcq[pos % 8]);
416 mcqe2 = _mm_loadu_si128((__m128i *)&mcq[pos % 8 + 2]);
417 /* B.1 store rearm data to mbuf. */
418 _mm_storeu_si128((__m128i *)&elts[pos]->rearm_data, rearm);
419 _mm_storeu_si128((__m128i *)&elts[pos + 1]->rearm_data, rearm);
420 /* C.1 combine data from mCQEs with rx_descriptor_fields1. */
421 rxdf1 = _mm_shuffle_epi8(mcqe1, shuf_mask1);
422 rxdf2 = _mm_shuffle_epi8(mcqe1, shuf_mask2);
423 rxdf1 = _mm_sub_epi16(rxdf1, crc_adj);
424 rxdf2 = _mm_sub_epi16(rxdf2, crc_adj);
425 rxdf1 = _mm_blend_epi16(rxdf1, rxdf, 0x23);
426 rxdf2 = _mm_blend_epi16(rxdf2, rxdf, 0x23);
427 /* D.1 store rx_descriptor_fields1. */
428 _mm_storeu_si128((__m128i *)
429 &elts[pos]->rx_descriptor_fields1,
431 _mm_storeu_si128((__m128i *)
432 &elts[pos + 1]->rx_descriptor_fields1,
434 /* B.1 store rearm data to mbuf. */
435 _mm_storeu_si128((__m128i *)&elts[pos + 2]->rearm_data, rearm);
436 _mm_storeu_si128((__m128i *)&elts[pos + 3]->rearm_data, rearm);
437 /* C.1 combine data from mCQEs with rx_descriptor_fields1. */
438 rxdf1 = _mm_shuffle_epi8(mcqe2, shuf_mask1);
439 rxdf2 = _mm_shuffle_epi8(mcqe2, shuf_mask2);
440 rxdf1 = _mm_sub_epi16(rxdf1, crc_adj);
441 rxdf2 = _mm_sub_epi16(rxdf2, crc_adj);
442 rxdf1 = _mm_blend_epi16(rxdf1, rxdf, 0x23);
443 rxdf2 = _mm_blend_epi16(rxdf2, rxdf, 0x23);
444 /* D.1 store rx_descriptor_fields1. */
445 _mm_storeu_si128((__m128i *)
446 &elts[pos + 2]->rx_descriptor_fields1,
448 _mm_storeu_si128((__m128i *)
449 &elts[pos + 3]->rx_descriptor_fields1,
451 #ifdef MLX5_PMD_SOFT_COUNTERS
452 invalid_mask = _mm_set_epi64x(0,
454 sizeof(uint16_t) * 8);
455 invalid_mask = _mm_sll_epi64(ones, invalid_mask);
456 mcqe1 = _mm_srli_si128(mcqe1, 4);
457 byte_cnt = _mm_blend_epi16(mcqe1, mcqe2, 0xcc);
458 byte_cnt = _mm_shuffle_epi8(byte_cnt, len_shuf_mask);
459 byte_cnt = _mm_andnot_si128(invalid_mask, byte_cnt);
460 byte_cnt = _mm_hadd_epi16(byte_cnt, zero);
461 rcvd_byte += _mm_cvtsi128_si64(_mm_hadd_epi16(byte_cnt, zero));
464 /* E.1 store flow tag (rte_flow mark). */
465 elts[pos]->hash.fdir.hi = flow_tag;
466 elts[pos + 1]->hash.fdir.hi = flow_tag;
467 elts[pos + 2]->hash.fdir.hi = flow_tag;
468 elts[pos + 3]->hash.fdir.hi = flow_tag;
470 pos += MLX5_VPMD_DESCS_PER_LOOP;
471 /* Move to next CQE and invalidate consumed CQEs. */
472 if (!(pos & 0x7) && pos < mcqe_n) {
473 mcq = (void *)(cq + pos);
474 for (i = 0; i < 8; ++i)
475 cq[inv++].op_own = MLX5_CQE_INVALIDATE;
478 /* Invalidate the rest of CQEs. */
479 for (; inv < mcqe_n; ++inv)
480 cq[inv].op_own = MLX5_CQE_INVALIDATE;
481 #ifdef MLX5_PMD_SOFT_COUNTERS
482 rxq->stats.ipackets += mcqe_n;
483 rxq->stats.ibytes += rcvd_byte;
485 rxq->cq_ci += mcqe_n;
489 * Calculate packet type and offload flag for mbuf and store it.
492 * Pointer to RX queue structure.
494 * Array of four 16bytes completions extracted from the original completion
497 * Opcode vector having responder error status. Each field is 4B.
499 * Pointer to array of packets to be filled.
502 rxq_cq_to_ptype_oflags_v(struct mlx5_rxq_data *rxq, __m128i cqes[4],
503 __m128i op_err, struct rte_mbuf **pkts)
505 __m128i pinfo0, pinfo1;
506 __m128i pinfo, ptype;
507 __m128i ol_flags = _mm_set1_epi32(rxq->rss_hash * PKT_RX_RSS_HASH |
508 rxq->hw_timestamp * PKT_RX_TIMESTAMP);
510 const __m128i zero = _mm_setzero_si128();
511 const __m128i ptype_mask =
512 _mm_set_epi32(0xfd06, 0xfd06, 0xfd06, 0xfd06);
513 const __m128i ptype_ol_mask =
514 _mm_set_epi32(0x106, 0x106, 0x106, 0x106);
515 const __m128i pinfo_mask =
516 _mm_set_epi32(0x3, 0x3, 0x3, 0x3);
517 const __m128i cv_flag_sel =
518 _mm_set_epi8(0, 0, 0, 0, 0, 0, 0, 0, 0,
519 (uint8_t)((PKT_RX_IP_CKSUM_GOOD |
520 PKT_RX_L4_CKSUM_GOOD) >> 1),
522 (uint8_t)(PKT_RX_L4_CKSUM_GOOD >> 1),
524 (uint8_t)(PKT_RX_IP_CKSUM_GOOD >> 1),
525 (uint8_t)(PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED),
527 const __m128i cv_mask =
528 _mm_set_epi32(PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD |
529 PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED,
530 PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD |
531 PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED,
532 PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD |
533 PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED,
534 PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD |
535 PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED);
536 const __m128i mbuf_init =
537 _mm_loadl_epi64((__m128i *)&rxq->mbuf_initializer);
538 __m128i rearm0, rearm1, rearm2, rearm3;
539 uint8_t pt_idx0, pt_idx1, pt_idx2, pt_idx3;
541 /* Extract pkt_info field. */
542 pinfo0 = _mm_unpacklo_epi32(cqes[0], cqes[1]);
543 pinfo1 = _mm_unpacklo_epi32(cqes[2], cqes[3]);
544 pinfo = _mm_unpacklo_epi64(pinfo0, pinfo1);
545 /* Extract hdr_type_etc field. */
546 pinfo0 = _mm_unpackhi_epi32(cqes[0], cqes[1]);
547 pinfo1 = _mm_unpackhi_epi32(cqes[2], cqes[3]);
548 ptype = _mm_unpacklo_epi64(pinfo0, pinfo1);
550 const __m128i pinfo_ft_mask =
551 _mm_set_epi32(0xffffff00, 0xffffff00,
552 0xffffff00, 0xffffff00);
553 const __m128i fdir_flags = _mm_set1_epi32(PKT_RX_FDIR);
554 __m128i fdir_id_flags = _mm_set1_epi32(PKT_RX_FDIR_ID);
555 __m128i flow_tag, invalid_mask;
557 flow_tag = _mm_and_si128(pinfo, pinfo_ft_mask);
558 /* Check if flow tag is non-zero then set PKT_RX_FDIR. */
559 invalid_mask = _mm_cmpeq_epi32(flow_tag, zero);
560 ol_flags = _mm_or_si128(ol_flags,
561 _mm_andnot_si128(invalid_mask,
563 /* Mask out invalid entries. */
564 fdir_id_flags = _mm_andnot_si128(invalid_mask, fdir_id_flags);
565 /* Check if flow tag MLX5_FLOW_MARK_DEFAULT. */
566 ol_flags = _mm_or_si128(ol_flags,
568 _mm_cmpeq_epi32(flow_tag,
573 * Merge the two fields to generate the following:
577 * bit[11:10] = l3_hdr_type
578 * bit[14:12] = l4_hdr_type
581 * bit[17] = outer_l3_type
583 ptype = _mm_and_si128(ptype, ptype_mask);
584 pinfo = _mm_and_si128(pinfo, pinfo_mask);
585 pinfo = _mm_slli_epi32(pinfo, 16);
586 /* Make pinfo has merged fields for ol_flags calculation. */
587 pinfo = _mm_or_si128(ptype, pinfo);
588 ptype = _mm_srli_epi32(pinfo, 10);
589 ptype = _mm_packs_epi32(ptype, zero);
590 /* Errored packets will have RTE_PTYPE_ALL_MASK. */
591 op_err = _mm_srli_epi16(op_err, 8);
592 ptype = _mm_or_si128(ptype, op_err);
593 pt_idx0 = _mm_extract_epi8(ptype, 0);
594 pt_idx1 = _mm_extract_epi8(ptype, 2);
595 pt_idx2 = _mm_extract_epi8(ptype, 4);
596 pt_idx3 = _mm_extract_epi8(ptype, 6);
597 pkts[0]->packet_type = mlx5_ptype_table[pt_idx0] |
598 !!(pt_idx0 & (1 << 6)) * rxq->tunnel;
599 pkts[1]->packet_type = mlx5_ptype_table[pt_idx1] |
600 !!(pt_idx1 & (1 << 6)) * rxq->tunnel;
601 pkts[2]->packet_type = mlx5_ptype_table[pt_idx2] |
602 !!(pt_idx2 & (1 << 6)) * rxq->tunnel;
603 pkts[3]->packet_type = mlx5_ptype_table[pt_idx3] |
604 !!(pt_idx3 & (1 << 6)) * rxq->tunnel;
605 /* Fill flags for checksum and VLAN. */
606 pinfo = _mm_and_si128(pinfo, ptype_ol_mask);
607 pinfo = _mm_shuffle_epi8(cv_flag_sel, pinfo);
608 /* Locate checksum flags at byte[2:1] and merge with VLAN flags. */
609 cv_flags = _mm_slli_epi32(pinfo, 9);
610 cv_flags = _mm_or_si128(pinfo, cv_flags);
611 /* Move back flags to start from byte[0]. */
612 cv_flags = _mm_srli_epi32(cv_flags, 8);
613 /* Mask out garbage bits. */
614 cv_flags = _mm_and_si128(cv_flags, cv_mask);
615 /* Merge to ol_flags. */
616 ol_flags = _mm_or_si128(ol_flags, cv_flags);
617 /* Merge mbuf_init and ol_flags. */
618 rearm0 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(ol_flags, 8), 0x30);
619 rearm1 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(ol_flags, 4), 0x30);
620 rearm2 = _mm_blend_epi16(mbuf_init, ol_flags, 0x30);
621 rearm3 = _mm_blend_epi16(mbuf_init, _mm_srli_si128(ol_flags, 4), 0x30);
622 /* Write 8B rearm_data and 8B ol_flags. */
623 _mm_store_si128((__m128i *)&pkts[0]->rearm_data, rearm0);
624 _mm_store_si128((__m128i *)&pkts[1]->rearm_data, rearm1);
625 _mm_store_si128((__m128i *)&pkts[2]->rearm_data, rearm2);
626 _mm_store_si128((__m128i *)&pkts[3]->rearm_data, rearm3);
630 * Receive burst of packets. An errored completion also consumes a mbuf, but the
631 * packet_type is set to be RTE_PTYPE_ALL_MASK. Marked mbufs should be freed
632 * before returning to application.
635 * Pointer to RX queue structure.
637 * Array to store received packets.
639 * Maximum number of packets in array.
641 * Pointer to a flag. Set non-zero value if pkts array has at least one error
645 * Number of packets received including errors (<= pkts_n).
647 static inline uint16_t
648 rxq_burst_v(struct mlx5_rxq_data *rxq, struct rte_mbuf **pkts, uint16_t pkts_n,
651 const uint16_t q_n = 1 << rxq->cqe_n;
652 const uint16_t q_mask = q_n - 1;
653 volatile struct mlx5_cqe *cq;
654 struct rte_mbuf **elts;
658 uint64_t comp_idx = MLX5_VPMD_DESCS_PER_LOOP;
659 uint16_t nocmp_n = 0;
660 uint16_t rcvd_pkt = 0;
661 unsigned int cq_idx = rxq->cq_ci & q_mask;
662 unsigned int elts_idx;
663 unsigned int ownership = !!(rxq->cq_ci & (q_mask + 1));
664 const __m128i owner_check =
665 _mm_set_epi64x(0x0100000001000000LL, 0x0100000001000000LL);
666 const __m128i opcode_check =
667 _mm_set_epi64x(0xf0000000f0000000LL, 0xf0000000f0000000LL);
668 const __m128i format_check =
669 _mm_set_epi64x(0x0c0000000c000000LL, 0x0c0000000c000000LL);
670 const __m128i resp_err_check =
671 _mm_set_epi64x(0xe0000000e0000000LL, 0xe0000000e0000000LL);
672 #ifdef MLX5_PMD_SOFT_COUNTERS
673 uint32_t rcvd_byte = 0;
674 /* Mask to shuffle byte_cnt to add up stats. Do bswap16 for all. */
675 const __m128i len_shuf_mask =
676 _mm_set_epi8(-1, -1, -1, -1,
681 /* Mask to shuffle from extracted CQE to mbuf. */
682 const __m128i shuf_mask =
683 _mm_set_epi8(-1, 3, 2, 1, /* fdir.hi */
684 12, 13, 14, 15, /* rss, bswap32 */
685 10, 11, /* vlan_tci, bswap16 */
686 4, 5, /* data_len, bswap16 */
687 -1, -1, /* zero out 2nd half of pkt_len */
688 4, 5 /* pkt_len, bswap16 */);
689 /* Mask to blend from the last Qword to the first DQword. */
690 const __m128i blend_mask =
691 _mm_set_epi8(-1, -1, -1, -1,
695 const __m128i zero = _mm_setzero_si128();
696 const __m128i ones = _mm_cmpeq_epi32(zero, zero);
697 const __m128i crc_adj =
698 _mm_set_epi16(0, 0, 0, 0, 0,
699 rxq->crc_present * ETHER_CRC_LEN,
701 rxq->crc_present * ETHER_CRC_LEN);
702 const __m128i flow_mark_adj = _mm_set_epi32(rxq->mark * (-1), 0, 0, 0);
704 assert(rxq->sges_n == 0);
705 assert(rxq->cqe_n == rxq->elts_n);
706 cq = &(*rxq->cqes)[cq_idx];
708 rte_prefetch0(cq + 1);
709 rte_prefetch0(cq + 2);
710 rte_prefetch0(cq + 3);
711 pkts_n = RTE_MIN(pkts_n, MLX5_VPMD_RX_MAX_BURST);
714 * rq_ci >= cq_ci >= rq_pi
715 * Definition of indexes:
716 * rq_ci - cq_ci := # of buffers owned by HW (posted).
717 * cq_ci - rq_pi := # of buffers not returned to app (decompressed).
718 * N - (rq_ci - rq_pi) := # of buffers consumed (to be replenished).
720 repl_n = q_n - (rxq->rq_ci - rxq->rq_pi);
721 if (repl_n >= MLX5_VPMD_RXQ_RPLNSH_THRESH(q_n))
722 mlx5_rx_replenish_bulk_mbuf(rxq, repl_n);
723 /* See if there're unreturned mbufs from compressed CQE. */
724 rcvd_pkt = rxq->cq_ci - rxq->rq_pi;
726 rcvd_pkt = RTE_MIN(rcvd_pkt, pkts_n);
727 rxq_copy_mbuf_v(rxq, pkts, rcvd_pkt);
728 rxq->rq_pi += rcvd_pkt;
731 elts_idx = rxq->rq_pi & q_mask;
732 elts = &(*rxq->elts)[elts_idx];
733 /* Not to overflow pkts array. */
734 pkts_n = RTE_ALIGN_FLOOR(pkts_n - rcvd_pkt, MLX5_VPMD_DESCS_PER_LOOP);
735 /* Not to cross queue end. */
736 pkts_n = RTE_MIN(pkts_n, q_n - elts_idx);
739 /* At this point, there shouldn't be any remained packets. */
740 assert(rxq->rq_pi == rxq->cq_ci);
742 * A. load first Qword (8bytes) in one loop.
743 * B. copy 4 mbuf pointers from elts ring to returing pkts.
744 * C. load remained CQE data and extract necessary fields.
745 * Final 16bytes cqes[] extracted from original 64bytes CQE has the
746 * following structure:
749 * uint8_t flow_tag[3];
753 * uint16_t hdr_type_etc;
754 * uint16_t vlan_info;
755 * uint32_t rx_has_res;
759 * F. find compressed CQE.
763 pos += MLX5_VPMD_DESCS_PER_LOOP) {
764 __m128i cqes[MLX5_VPMD_DESCS_PER_LOOP];
765 __m128i cqe_tmp1, cqe_tmp2;
766 __m128i pkt_mb0, pkt_mb1, pkt_mb2, pkt_mb3;
767 __m128i op_own, op_own_tmp1, op_own_tmp2;
768 __m128i opcode, owner_mask, invalid_mask;
771 #ifdef MLX5_PMD_SOFT_COUNTERS
775 __m128i p = _mm_set_epi16(0, 0, 0, 0, 3, 2, 1, 0);
776 unsigned int p1, p2, p3;
778 /* Prefetch next 4 CQEs. */
779 if (pkts_n - pos >= 2 * MLX5_VPMD_DESCS_PER_LOOP) {
780 rte_prefetch0(&cq[pos + MLX5_VPMD_DESCS_PER_LOOP]);
781 rte_prefetch0(&cq[pos + MLX5_VPMD_DESCS_PER_LOOP + 1]);
782 rte_prefetch0(&cq[pos + MLX5_VPMD_DESCS_PER_LOOP + 2]);
783 rte_prefetch0(&cq[pos + MLX5_VPMD_DESCS_PER_LOOP + 3]);
785 /* A.0 do not cross the end of CQ. */
786 mask = _mm_set_epi64x(0, (pkts_n - pos) * sizeof(uint16_t) * 8);
787 mask = _mm_sll_epi64(ones, mask);
788 p = _mm_andnot_si128(mask, p);
790 p3 = _mm_extract_epi16(p, 3);
791 cqes[3] = _mm_loadl_epi64((__m128i *)
792 &cq[pos + p3].sop_drop_qpn);
793 rte_compiler_barrier();
794 p2 = _mm_extract_epi16(p, 2);
795 cqes[2] = _mm_loadl_epi64((__m128i *)
796 &cq[pos + p2].sop_drop_qpn);
797 rte_compiler_barrier();
798 /* B.1 load mbuf pointers. */
799 mbp1 = _mm_loadu_si128((__m128i *)&elts[pos]);
800 mbp2 = _mm_loadu_si128((__m128i *)&elts[pos + 2]);
801 /* A.1 load a block having op_own. */
802 p1 = _mm_extract_epi16(p, 1);
803 cqes[1] = _mm_loadl_epi64((__m128i *)
804 &cq[pos + p1].sop_drop_qpn);
805 rte_compiler_barrier();
806 cqes[0] = _mm_loadl_epi64((__m128i *)
807 &cq[pos].sop_drop_qpn);
808 /* B.2 copy mbuf pointers. */
809 _mm_storeu_si128((__m128i *)&pkts[pos], mbp1);
810 _mm_storeu_si128((__m128i *)&pkts[pos + 2], mbp2);
812 /* C.1 load remained CQE data and extract necessary fields. */
813 cqe_tmp2 = _mm_load_si128((__m128i *)&cq[pos + p3]);
814 cqe_tmp1 = _mm_load_si128((__m128i *)&cq[pos + p2]);
815 cqes[3] = _mm_blendv_epi8(cqes[3], cqe_tmp2, blend_mask);
816 cqes[2] = _mm_blendv_epi8(cqes[2], cqe_tmp1, blend_mask);
817 cqe_tmp2 = _mm_loadu_si128((__m128i *)&cq[pos + p3].rsvd1[3]);
818 cqe_tmp1 = _mm_loadu_si128((__m128i *)&cq[pos + p2].rsvd1[3]);
819 cqes[3] = _mm_blend_epi16(cqes[3], cqe_tmp2, 0x30);
820 cqes[2] = _mm_blend_epi16(cqes[2], cqe_tmp1, 0x30);
821 cqe_tmp2 = _mm_loadl_epi64((__m128i *)&cq[pos + p3].rsvd2[10]);
822 cqe_tmp1 = _mm_loadl_epi64((__m128i *)&cq[pos + p2].rsvd2[10]);
823 cqes[3] = _mm_blend_epi16(cqes[3], cqe_tmp2, 0x04);
824 cqes[2] = _mm_blend_epi16(cqes[2], cqe_tmp1, 0x04);
825 /* C.2 generate final structure for mbuf with swapping bytes. */
826 pkt_mb3 = _mm_shuffle_epi8(cqes[3], shuf_mask);
827 pkt_mb2 = _mm_shuffle_epi8(cqes[2], shuf_mask);
828 /* C.3 adjust CRC length. */
829 pkt_mb3 = _mm_sub_epi16(pkt_mb3, crc_adj);
830 pkt_mb2 = _mm_sub_epi16(pkt_mb2, crc_adj);
831 /* C.4 adjust flow mark. */
832 pkt_mb3 = _mm_add_epi32(pkt_mb3, flow_mark_adj);
833 pkt_mb2 = _mm_add_epi32(pkt_mb2, flow_mark_adj);
834 /* D.1 fill in mbuf - rx_descriptor_fields1. */
835 _mm_storeu_si128((void *)&pkts[pos + 3]->pkt_len, pkt_mb3);
836 _mm_storeu_si128((void *)&pkts[pos + 2]->pkt_len, pkt_mb2);
837 /* E.1 extract op_own field. */
838 op_own_tmp2 = _mm_unpacklo_epi32(cqes[2], cqes[3]);
839 /* C.1 load remained CQE data and extract necessary fields. */
840 cqe_tmp2 = _mm_load_si128((__m128i *)&cq[pos + p1]);
841 cqe_tmp1 = _mm_load_si128((__m128i *)&cq[pos]);
842 cqes[1] = _mm_blendv_epi8(cqes[1], cqe_tmp2, blend_mask);
843 cqes[0] = _mm_blendv_epi8(cqes[0], cqe_tmp1, blend_mask);
844 cqe_tmp2 = _mm_loadu_si128((__m128i *)&cq[pos + p1].rsvd1[3]);
845 cqe_tmp1 = _mm_loadu_si128((__m128i *)&cq[pos].rsvd1[3]);
846 cqes[1] = _mm_blend_epi16(cqes[1], cqe_tmp2, 0x30);
847 cqes[0] = _mm_blend_epi16(cqes[0], cqe_tmp1, 0x30);
848 cqe_tmp2 = _mm_loadl_epi64((__m128i *)&cq[pos + p1].rsvd2[10]);
849 cqe_tmp1 = _mm_loadl_epi64((__m128i *)&cq[pos].rsvd2[10]);
850 cqes[1] = _mm_blend_epi16(cqes[1], cqe_tmp2, 0x04);
851 cqes[0] = _mm_blend_epi16(cqes[0], cqe_tmp1, 0x04);
852 /* C.2 generate final structure for mbuf with swapping bytes. */
853 pkt_mb1 = _mm_shuffle_epi8(cqes[1], shuf_mask);
854 pkt_mb0 = _mm_shuffle_epi8(cqes[0], shuf_mask);
855 /* C.3 adjust CRC length. */
856 pkt_mb1 = _mm_sub_epi16(pkt_mb1, crc_adj);
857 pkt_mb0 = _mm_sub_epi16(pkt_mb0, crc_adj);
858 /* C.4 adjust flow mark. */
859 pkt_mb1 = _mm_add_epi32(pkt_mb1, flow_mark_adj);
860 pkt_mb0 = _mm_add_epi32(pkt_mb0, flow_mark_adj);
861 /* E.1 extract op_own byte. */
862 op_own_tmp1 = _mm_unpacklo_epi32(cqes[0], cqes[1]);
863 op_own = _mm_unpackhi_epi64(op_own_tmp1, op_own_tmp2);
864 /* D.1 fill in mbuf - rx_descriptor_fields1. */
865 _mm_storeu_si128((void *)&pkts[pos + 1]->pkt_len, pkt_mb1);
866 _mm_storeu_si128((void *)&pkts[pos]->pkt_len, pkt_mb0);
867 /* E.2 flip owner bit to mark CQEs from last round. */
868 owner_mask = _mm_and_si128(op_own, owner_check);
870 owner_mask = _mm_xor_si128(owner_mask, owner_check);
871 owner_mask = _mm_cmpeq_epi32(owner_mask, owner_check);
872 owner_mask = _mm_packs_epi32(owner_mask, zero);
873 /* E.3 get mask for invalidated CQEs. */
874 opcode = _mm_and_si128(op_own, opcode_check);
875 invalid_mask = _mm_cmpeq_epi32(opcode_check, opcode);
876 invalid_mask = _mm_packs_epi32(invalid_mask, zero);
877 /* E.4 mask out beyond boundary. */
878 invalid_mask = _mm_or_si128(invalid_mask, mask);
879 /* E.5 merge invalid_mask with invalid owner. */
880 invalid_mask = _mm_or_si128(invalid_mask, owner_mask);
881 /* F.1 find compressed CQE format. */
882 comp_mask = _mm_and_si128(op_own, format_check);
883 comp_mask = _mm_cmpeq_epi32(comp_mask, format_check);
884 comp_mask = _mm_packs_epi32(comp_mask, zero);
885 /* F.2 mask out invalid entries. */
886 comp_mask = _mm_andnot_si128(invalid_mask, comp_mask);
887 comp_idx = _mm_cvtsi128_si64(comp_mask);
888 /* F.3 get the first compressed CQE. */
889 comp_idx = comp_idx ?
890 __builtin_ctzll(comp_idx) /
891 (sizeof(uint16_t) * 8) :
892 MLX5_VPMD_DESCS_PER_LOOP;
893 /* E.6 mask out entries after the compressed CQE. */
894 mask = _mm_set_epi64x(0, comp_idx * sizeof(uint16_t) * 8);
895 mask = _mm_sll_epi64(ones, mask);
896 invalid_mask = _mm_or_si128(invalid_mask, mask);
897 /* E.7 count non-compressed valid CQEs. */
898 n = _mm_cvtsi128_si64(invalid_mask);
899 n = n ? __builtin_ctzll(n) / (sizeof(uint16_t) * 8) :
900 MLX5_VPMD_DESCS_PER_LOOP;
902 /* D.2 get the final invalid mask. */
903 mask = _mm_set_epi64x(0, n * sizeof(uint16_t) * 8);
904 mask = _mm_sll_epi64(ones, mask);
905 invalid_mask = _mm_or_si128(invalid_mask, mask);
906 /* D.3 check error in opcode. */
907 opcode = _mm_cmpeq_epi32(resp_err_check, opcode);
908 opcode = _mm_packs_epi32(opcode, zero);
909 opcode = _mm_andnot_si128(invalid_mask, opcode);
910 /* D.4 mark if any error is set */
911 *err |= _mm_cvtsi128_si64(opcode);
912 /* D.5 fill in mbuf - rearm_data and packet_type. */
913 rxq_cq_to_ptype_oflags_v(rxq, cqes, opcode, &pkts[pos]);
914 if (rxq->hw_timestamp) {
915 pkts[pos]->timestamp =
916 rte_be_to_cpu_64(cq[pos].timestamp);
917 pkts[pos + 1]->timestamp =
918 rte_be_to_cpu_64(cq[pos + p1].timestamp);
919 pkts[pos + 2]->timestamp =
920 rte_be_to_cpu_64(cq[pos + p2].timestamp);
921 pkts[pos + 3]->timestamp =
922 rte_be_to_cpu_64(cq[pos + p3].timestamp);
924 #ifdef MLX5_PMD_SOFT_COUNTERS
925 /* Add up received bytes count. */
926 byte_cnt = _mm_shuffle_epi8(op_own, len_shuf_mask);
927 byte_cnt = _mm_andnot_si128(invalid_mask, byte_cnt);
928 byte_cnt = _mm_hadd_epi16(byte_cnt, zero);
929 rcvd_byte += _mm_cvtsi128_si64(_mm_hadd_epi16(byte_cnt, zero));
932 * Break the loop unless more valid CQE is expected, or if
933 * there's a compressed CQE.
935 if (n != MLX5_VPMD_DESCS_PER_LOOP)
938 /* If no new CQE seen, return without updating cq_db. */
939 if (unlikely(!nocmp_n && comp_idx == MLX5_VPMD_DESCS_PER_LOOP))
941 /* Update the consumer indexes for non-compressed CQEs. */
942 assert(nocmp_n <= pkts_n);
943 rxq->cq_ci += nocmp_n;
944 rxq->rq_pi += nocmp_n;
946 #ifdef MLX5_PMD_SOFT_COUNTERS
947 rxq->stats.ipackets += nocmp_n;
948 rxq->stats.ibytes += rcvd_byte;
950 /* Decompress the last CQE if compressed. */
951 if (comp_idx < MLX5_VPMD_DESCS_PER_LOOP && comp_idx == n) {
952 assert(comp_idx == (nocmp_n % MLX5_VPMD_DESCS_PER_LOOP));
953 rxq_cq_decompress_v(rxq, &cq[nocmp_n], &elts[nocmp_n]);
954 /* Return more packets if needed. */
955 if (nocmp_n < pkts_n) {
956 uint16_t n = rxq->cq_ci - rxq->rq_pi;
958 n = RTE_MIN(n, pkts_n - nocmp_n);
959 rxq_copy_mbuf_v(rxq, &pkts[nocmp_n], n);
964 rte_compiler_barrier();
965 *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
969 #endif /* RTE_PMD_MLX5_RXTX_VEC_SSE_H_ */