1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2019 Marvell International Ltd.
7 #include "otx2_ethdev.h"
10 #define NIX_DESCS_PER_LOOP 4
11 #define CQE_CAST(x) ((struct nix_cqe_hdr_s *)(x))
12 #define CQE_SZ(x) ((x) * NIX_CQ_ENTRY_SZ)
14 static inline uint16_t
15 nix_rx_nb_pkts(struct otx2_eth_rxq *rxq, const uint64_t wdata,
16 const uint16_t pkts, const uint32_t qmask)
18 uint32_t available = rxq->available;
20 /* Update the available count if cached value is not enough */
21 if (unlikely(available < pkts)) {
22 uint64_t reg, head, tail;
24 /* Use LDADDA version to avoid reorder */
25 reg = otx2_atomic64_add_sync(wdata, rxq->cq_status);
26 /* CQ_OP_STATUS operation error */
27 if (reg & BIT_ULL(CQ_OP_STAT_OP_ERR) ||
28 reg & BIT_ULL(CQ_OP_STAT_CQ_ERR))
32 head = (reg >> 20) & 0xFFFFF;
34 available = tail - head + qmask + 1;
36 available = tail - head;
38 rxq->available = available;
41 return RTE_MIN(pkts, available);
44 static __rte_always_inline uint16_t
45 nix_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
46 uint16_t pkts, const uint16_t flags)
48 struct otx2_eth_rxq *rxq = rx_queue;
49 const uint64_t mbuf_init = rxq->mbuf_initializer;
50 const void *lookup_mem = rxq->lookup_mem;
51 const uint64_t data_off = rxq->data_off;
52 const uintptr_t desc = rxq->desc;
53 const uint64_t wdata = rxq->wdata;
54 const uint32_t qmask = rxq->qmask;
55 uint16_t packets = 0, nb_pkts;
56 uint32_t head = rxq->head;
57 struct nix_cqe_hdr_s *cq;
58 struct rte_mbuf *mbuf;
60 nb_pkts = nix_rx_nb_pkts(rxq, wdata, pkts, qmask);
62 while (packets < nb_pkts) {
63 /* Prefetch N desc ahead */
64 rte_prefetch_non_temporal((void *)(desc + (CQE_SZ(head + 2))));
65 cq = (struct nix_cqe_hdr_s *)(desc + CQE_SZ(head));
67 mbuf = nix_get_mbuf_from_cqe(cq, data_off);
69 otx2_nix_cqe_to_mbuf(cq, cq->tag, mbuf, lookup_mem, mbuf_init,
71 otx2_nix_mbuf_to_tstamp(mbuf, rxq->tstamp, flags,
72 (uint64_t *)((uint8_t *)mbuf + data_off));
73 rx_pkts[packets++] = mbuf;
74 otx2_prefetch_store_keep(mbuf);
80 rxq->available -= nb_pkts;
82 /* Free all the CQs that we've processed */
83 otx2_write64((wdata | nb_pkts), rxq->cq_door);
88 #if defined(RTE_ARCH_ARM64)
90 static __rte_always_inline uint64_t
91 nix_vlan_update(const uint64_t w2, uint64_t ol_flags, uint8x16_t *f)
93 if (w2 & BIT_ULL(21) /* vtag0_gone */) {
94 ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
95 *f = vsetq_lane_u16((uint16_t)(w2 >> 32), *f, 5);
101 static __rte_always_inline uint64_t
102 nix_qinq_update(const uint64_t w2, uint64_t ol_flags, struct rte_mbuf *mbuf)
104 if (w2 & BIT_ULL(23) /* vtag1_gone */) {
105 ol_flags |= PKT_RX_QINQ | PKT_RX_QINQ_STRIPPED;
106 mbuf->vlan_tci_outer = (uint16_t)(w2 >> 48);
112 static __rte_always_inline uint16_t
113 nix_recv_pkts_vector(void *rx_queue, struct rte_mbuf **rx_pkts,
114 uint16_t pkts, const uint16_t flags)
116 struct otx2_eth_rxq *rxq = rx_queue; uint16_t packets = 0;
117 uint64x2_t cq0_w8, cq1_w8, cq2_w8, cq3_w8, mbuf01, mbuf23;
118 const uint64_t mbuf_initializer = rxq->mbuf_initializer;
119 const uint64x2_t data_off = vdupq_n_u64(rxq->data_off);
120 uint64_t ol_flags0, ol_flags1, ol_flags2, ol_flags3;
121 uint64x2_t rearm0 = vdupq_n_u64(mbuf_initializer);
122 uint64x2_t rearm1 = vdupq_n_u64(mbuf_initializer);
123 uint64x2_t rearm2 = vdupq_n_u64(mbuf_initializer);
124 uint64x2_t rearm3 = vdupq_n_u64(mbuf_initializer);
125 struct rte_mbuf *mbuf0, *mbuf1, *mbuf2, *mbuf3;
126 const uint16_t *lookup_mem = rxq->lookup_mem;
127 const uint32_t qmask = rxq->qmask;
128 const uint64_t wdata = rxq->wdata;
129 const uintptr_t desc = rxq->desc;
130 uint8x16_t f0, f1, f2, f3;
131 uint32_t head = rxq->head;
133 pkts = nix_rx_nb_pkts(rxq, wdata, pkts, qmask);
134 /* Packets has to be floor-aligned to NIX_DESCS_PER_LOOP */
135 pkts = RTE_ALIGN_FLOOR(pkts, NIX_DESCS_PER_LOOP);
137 while (packets < pkts) {
138 /* Get the CQ pointers, since the ring size is multiple of
139 * 4, We can avoid checking the wrap around of head
140 * value after the each access unlike scalar version.
142 const uintptr_t cq0 = desc + CQE_SZ(head);
144 /* Prefetch N desc ahead */
145 rte_prefetch_non_temporal((void *)(cq0 + CQE_SZ(8)));
146 rte_prefetch_non_temporal((void *)(cq0 + CQE_SZ(9)));
147 rte_prefetch_non_temporal((void *)(cq0 + CQE_SZ(10)));
148 rte_prefetch_non_temporal((void *)(cq0 + CQE_SZ(11)));
150 /* Get NIX_RX_SG_S for size and buffer pointer */
151 cq0_w8 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(0) + 64));
152 cq1_w8 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(1) + 64));
153 cq2_w8 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(2) + 64));
154 cq3_w8 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(3) + 64));
156 /* Extract mbuf from NIX_RX_SG_S */
157 mbuf01 = vzip2q_u64(cq0_w8, cq1_w8);
158 mbuf23 = vzip2q_u64(cq2_w8, cq3_w8);
159 mbuf01 = vqsubq_u64(mbuf01, data_off);
160 mbuf23 = vqsubq_u64(mbuf23, data_off);
162 /* Move mbufs to scalar registers for future use */
163 mbuf0 = (struct rte_mbuf *)vgetq_lane_u64(mbuf01, 0);
164 mbuf1 = (struct rte_mbuf *)vgetq_lane_u64(mbuf01, 1);
165 mbuf2 = (struct rte_mbuf *)vgetq_lane_u64(mbuf23, 0);
166 mbuf3 = (struct rte_mbuf *)vgetq_lane_u64(mbuf23, 1);
168 /* Mask to get packet len from NIX_RX_SG_S */
169 const uint8x16_t shuf_msk = {
170 0xFF, 0xFF, /* pkt_type set as unknown */
171 0xFF, 0xFF, /* pkt_type set as unknown */
172 0, 1, /* octet 1~0, low 16 bits pkt_len */
173 0xFF, 0xFF, /* skip high 16 bits pkt_len, zero out */
174 0, 1, /* octet 1~0, 16 bits data_len */
176 0xFF, 0xFF, 0xFF, 0xFF
179 /* Form the rx_descriptor_fields1 with pkt_len and data_len */
180 f0 = vqtbl1q_u8(cq0_w8, shuf_msk);
181 f1 = vqtbl1q_u8(cq1_w8, shuf_msk);
182 f2 = vqtbl1q_u8(cq2_w8, shuf_msk);
183 f3 = vqtbl1q_u8(cq3_w8, shuf_msk);
185 /* Load CQE word0 and word 1 */
186 uint64x2_t cq0_w0 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(0)));
187 uint64x2_t cq1_w0 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(1)));
188 uint64x2_t cq2_w0 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(2)));
189 uint64x2_t cq3_w0 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(3)));
191 if (flags & NIX_RX_OFFLOAD_RSS_F) {
192 /* Fill rss in the rx_descriptor_fields1 */
193 f0 = vsetq_lane_u32(vgetq_lane_u32(cq0_w0, 0), f0, 3);
194 f1 = vsetq_lane_u32(vgetq_lane_u32(cq1_w0, 0), f1, 3);
195 f2 = vsetq_lane_u32(vgetq_lane_u32(cq2_w0, 0), f2, 3);
196 f3 = vsetq_lane_u32(vgetq_lane_u32(cq3_w0, 0), f3, 3);
197 ol_flags0 = PKT_RX_RSS_HASH;
198 ol_flags1 = PKT_RX_RSS_HASH;
199 ol_flags2 = PKT_RX_RSS_HASH;
200 ol_flags3 = PKT_RX_RSS_HASH;
202 ol_flags0 = 0; ol_flags1 = 0;
203 ol_flags2 = 0; ol_flags3 = 0;
206 if (flags & NIX_RX_OFFLOAD_PTYPE_F) {
207 /* Fill packet_type in the rx_descriptor_fields1 */
208 f0 = vsetq_lane_u32(nix_ptype_get(lookup_mem,
209 vgetq_lane_u64(cq0_w0, 1)), f0, 0);
210 f1 = vsetq_lane_u32(nix_ptype_get(lookup_mem,
211 vgetq_lane_u64(cq1_w0, 1)), f1, 0);
212 f2 = vsetq_lane_u32(nix_ptype_get(lookup_mem,
213 vgetq_lane_u64(cq2_w0, 1)), f2, 0);
214 f3 = vsetq_lane_u32(nix_ptype_get(lookup_mem,
215 vgetq_lane_u64(cq3_w0, 1)), f3, 0);
218 if (flags & NIX_RX_OFFLOAD_CHECKSUM_F) {
219 ol_flags0 |= nix_rx_olflags_get(lookup_mem,
220 vgetq_lane_u64(cq0_w0, 1));
221 ol_flags1 |= nix_rx_olflags_get(lookup_mem,
222 vgetq_lane_u64(cq1_w0, 1));
223 ol_flags2 |= nix_rx_olflags_get(lookup_mem,
224 vgetq_lane_u64(cq2_w0, 1));
225 ol_flags3 |= nix_rx_olflags_get(lookup_mem,
226 vgetq_lane_u64(cq3_w0, 1));
229 if (flags & NIX_RX_OFFLOAD_VLAN_STRIP_F) {
230 uint64_t cq0_w2 = *(uint64_t *)(cq0 + CQE_SZ(0) + 16);
231 uint64_t cq1_w2 = *(uint64_t *)(cq0 + CQE_SZ(1) + 16);
232 uint64_t cq2_w2 = *(uint64_t *)(cq0 + CQE_SZ(2) + 16);
233 uint64_t cq3_w2 = *(uint64_t *)(cq0 + CQE_SZ(3) + 16);
235 ol_flags0 = nix_vlan_update(cq0_w2, ol_flags0, &f0);
236 ol_flags1 = nix_vlan_update(cq1_w2, ol_flags1, &f1);
237 ol_flags2 = nix_vlan_update(cq2_w2, ol_flags2, &f2);
238 ol_flags3 = nix_vlan_update(cq3_w2, ol_flags3, &f3);
240 ol_flags0 = nix_qinq_update(cq0_w2, ol_flags0, mbuf0);
241 ol_flags1 = nix_qinq_update(cq1_w2, ol_flags1, mbuf1);
242 ol_flags2 = nix_qinq_update(cq2_w2, ol_flags2, mbuf2);
243 ol_flags3 = nix_qinq_update(cq3_w2, ol_flags3, mbuf3);
246 if (flags & NIX_RX_OFFLOAD_MARK_UPDATE_F) {
247 ol_flags0 = nix_update_match_id(*(uint16_t *)
248 (cq0 + CQE_SZ(0) + 38), ol_flags0, mbuf0);
249 ol_flags1 = nix_update_match_id(*(uint16_t *)
250 (cq0 + CQE_SZ(1) + 38), ol_flags1, mbuf1);
251 ol_flags2 = nix_update_match_id(*(uint16_t *)
252 (cq0 + CQE_SZ(2) + 38), ol_flags2, mbuf2);
253 ol_flags3 = nix_update_match_id(*(uint16_t *)
254 (cq0 + CQE_SZ(3) + 38), ol_flags3, mbuf3);
257 /* Form rearm_data with ol_flags */
258 rearm0 = vsetq_lane_u64(ol_flags0, rearm0, 1);
259 rearm1 = vsetq_lane_u64(ol_flags1, rearm1, 1);
260 rearm2 = vsetq_lane_u64(ol_flags2, rearm2, 1);
261 rearm3 = vsetq_lane_u64(ol_flags3, rearm3, 1);
263 /* Update rx_descriptor_fields1 */
264 vst1q_u64((uint64_t *)mbuf0->rx_descriptor_fields1, f0);
265 vst1q_u64((uint64_t *)mbuf1->rx_descriptor_fields1, f1);
266 vst1q_u64((uint64_t *)mbuf2->rx_descriptor_fields1, f2);
267 vst1q_u64((uint64_t *)mbuf3->rx_descriptor_fields1, f3);
269 /* Update rearm_data */
270 vst1q_u64((uint64_t *)mbuf0->rearm_data, rearm0);
271 vst1q_u64((uint64_t *)mbuf1->rearm_data, rearm1);
272 vst1q_u64((uint64_t *)mbuf2->rearm_data, rearm2);
273 vst1q_u64((uint64_t *)mbuf3->rearm_data, rearm3);
275 /* Store the mbufs to rx_pkts */
276 vst1q_u64((uint64_t *)&rx_pkts[packets], mbuf01);
277 vst1q_u64((uint64_t *)&rx_pkts[packets + 2], mbuf23);
280 otx2_prefetch_store_keep(mbuf0);
281 otx2_prefetch_store_keep(mbuf1);
282 otx2_prefetch_store_keep(mbuf2);
283 otx2_prefetch_store_keep(mbuf3);
285 /* Mark mempool obj as "get" as it is alloc'ed by NIX */
286 __mempool_check_cookies(mbuf0->pool, (void **)&mbuf0, 1, 1);
287 __mempool_check_cookies(mbuf1->pool, (void **)&mbuf1, 1, 1);
288 __mempool_check_cookies(mbuf2->pool, (void **)&mbuf2, 1, 1);
289 __mempool_check_cookies(mbuf3->pool, (void **)&mbuf3, 1, 1);
291 /* Advance head pointer and packets */
292 head += NIX_DESCS_PER_LOOP; head &= qmask;
293 packets += NIX_DESCS_PER_LOOP;
297 rxq->available -= packets;
300 /* Free all the CQs that we've processed */
301 otx2_write64((rxq->wdata | packets), rxq->cq_door);
308 static inline uint16_t
309 nix_recv_pkts_vector(void *rx_queue, struct rte_mbuf **rx_pkts,
310 uint16_t pkts, const uint16_t flags)
312 RTE_SET_USED(rx_queue);
313 RTE_SET_USED(rx_pkts);
322 #define R(name, f5, f4, f3, f2, f1, f0, flags) \
323 static uint16_t __rte_noinline __hot \
324 otx2_nix_recv_pkts_ ## name(void *rx_queue, \
325 struct rte_mbuf **rx_pkts, uint16_t pkts) \
327 return nix_recv_pkts(rx_queue, rx_pkts, pkts, (flags)); \
330 static uint16_t __rte_noinline __hot \
331 otx2_nix_recv_pkts_mseg_ ## name(void *rx_queue, \
332 struct rte_mbuf **rx_pkts, uint16_t pkts) \
334 return nix_recv_pkts(rx_queue, rx_pkts, pkts, \
335 (flags) | NIX_RX_MULTI_SEG_F); \
338 static uint16_t __rte_noinline __hot \
339 otx2_nix_recv_pkts_vec_ ## name(void *rx_queue, \
340 struct rte_mbuf **rx_pkts, uint16_t pkts) \
342 /* TSTMP is not supported by vector */ \
343 if ((flags) & NIX_RX_OFFLOAD_TSTAMP_F) \
345 return nix_recv_pkts_vector(rx_queue, rx_pkts, pkts, (flags)); \
348 NIX_RX_FASTPATH_MODES
352 pick_rx_func(struct rte_eth_dev *eth_dev,
353 const eth_rx_burst_t rx_burst[2][2][2][2][2][2])
355 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
357 /* [TSTMP] [MARK] [VLAN] [CKSUM] [PTYPE] [RSS] */
358 eth_dev->rx_pkt_burst = rx_burst
359 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_TSTAMP_F)]
360 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_MARK_UPDATE_F)]
361 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_VLAN_STRIP_F)]
362 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_CHECKSUM_F)]
363 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_PTYPE_F)]
364 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_RSS_F)];
368 otx2_eth_set_rx_function(struct rte_eth_dev *eth_dev)
370 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
372 const eth_rx_burst_t nix_eth_rx_burst[2][2][2][2][2][2] = {
373 #define R(name, f5, f4, f3, f2, f1, f0, flags) \
374 [f5][f4][f3][f2][f1][f0] = otx2_nix_recv_pkts_ ## name,
376 NIX_RX_FASTPATH_MODES
380 const eth_rx_burst_t nix_eth_rx_burst_mseg[2][2][2][2][2][2] = {
381 #define R(name, f5, f4, f3, f2, f1, f0, flags) \
382 [f5][f4][f3][f2][f1][f0] = otx2_nix_recv_pkts_mseg_ ## name,
384 NIX_RX_FASTPATH_MODES
388 const eth_rx_burst_t nix_eth_rx_vec_burst[2][2][2][2][2][2] = {
389 #define R(name, f5, f4, f3, f2, f1, f0, flags) \
390 [f5][f4][f3][f2][f1][f0] = otx2_nix_recv_pkts_vec_ ## name,
392 NIX_RX_FASTPATH_MODES
396 /* For PTP enabled, scalar rx function should be chosen as most of the
397 * PTP apps are implemented to rx burst 1 pkt.
399 if (dev->scalar_ena || dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP)
400 pick_rx_func(eth_dev, nix_eth_rx_burst);
402 pick_rx_func(eth_dev, nix_eth_rx_vec_burst);
404 if (dev->rx_offloads & DEV_RX_OFFLOAD_SCATTER)
405 pick_rx_func(eth_dev, nix_eth_rx_burst_mseg);
407 /* Copy multi seg version with no offload for tear down sequence */
408 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
409 dev->rx_pkt_burst_no_offload =
410 nix_eth_rx_burst_mseg[0][0][0][0][0][0];