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 +
65 (CQE_SZ((head + 2) & qmask))));
66 cq = (struct nix_cqe_hdr_s *)(desc + CQE_SZ(head));
68 mbuf = nix_get_mbuf_from_cqe(cq, data_off);
70 otx2_nix_cqe_to_mbuf(cq, cq->tag, mbuf, lookup_mem, mbuf_init,
72 otx2_nix_mbuf_to_tstamp(mbuf, rxq->tstamp, flags,
73 (uint64_t *)((uint8_t *)mbuf + data_off));
74 rx_pkts[packets++] = mbuf;
75 otx2_prefetch_store_keep(mbuf);
81 rxq->available -= nb_pkts;
83 /* Free all the CQs that we've processed */
84 otx2_write64((wdata | nb_pkts), rxq->cq_door);
89 #if defined(RTE_ARCH_ARM64)
91 static __rte_always_inline uint64_t
92 nix_vlan_update(const uint64_t w2, uint64_t ol_flags, uint8x16_t *f)
94 if (w2 & BIT_ULL(21) /* vtag0_gone */) {
95 ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
96 *f = vsetq_lane_u16((uint16_t)(w2 >> 32), *f, 5);
102 static __rte_always_inline uint64_t
103 nix_qinq_update(const uint64_t w2, uint64_t ol_flags, struct rte_mbuf *mbuf)
105 if (w2 & BIT_ULL(23) /* vtag1_gone */) {
106 ol_flags |= PKT_RX_QINQ | PKT_RX_QINQ_STRIPPED;
107 mbuf->vlan_tci_outer = (uint16_t)(w2 >> 48);
113 static __rte_always_inline uint16_t
114 nix_recv_pkts_vector(void *rx_queue, struct rte_mbuf **rx_pkts,
115 uint16_t pkts, const uint16_t flags)
117 struct otx2_eth_rxq *rxq = rx_queue; uint16_t packets = 0;
118 uint64x2_t cq0_w8, cq1_w8, cq2_w8, cq3_w8, mbuf01, mbuf23;
119 const uint64_t mbuf_initializer = rxq->mbuf_initializer;
120 const uint64x2_t data_off = vdupq_n_u64(rxq->data_off);
121 uint64_t ol_flags0, ol_flags1, ol_flags2, ol_flags3;
122 uint64x2_t rearm0 = vdupq_n_u64(mbuf_initializer);
123 uint64x2_t rearm1 = vdupq_n_u64(mbuf_initializer);
124 uint64x2_t rearm2 = vdupq_n_u64(mbuf_initializer);
125 uint64x2_t rearm3 = vdupq_n_u64(mbuf_initializer);
126 struct rte_mbuf *mbuf0, *mbuf1, *mbuf2, *mbuf3;
127 const uint16_t *lookup_mem = rxq->lookup_mem;
128 const uint32_t qmask = rxq->qmask;
129 const uint64_t wdata = rxq->wdata;
130 const uintptr_t desc = rxq->desc;
131 uint8x16_t f0, f1, f2, f3;
132 uint32_t head = rxq->head;
134 pkts = nix_rx_nb_pkts(rxq, wdata, pkts, qmask);
135 /* Packets has to be floor-aligned to NIX_DESCS_PER_LOOP */
136 pkts = RTE_ALIGN_FLOOR(pkts, NIX_DESCS_PER_LOOP);
138 while (packets < pkts) {
139 /* Get the CQ pointers, since the ring size is multiple of
140 * 4, We can avoid checking the wrap around of head
141 * value after the each access unlike scalar version.
143 const uintptr_t cq0 = desc + CQE_SZ(head);
145 /* Prefetch N desc ahead */
146 rte_prefetch_non_temporal((void *)(cq0 + CQE_SZ(8)));
147 rte_prefetch_non_temporal((void *)(cq0 + CQE_SZ(9)));
148 rte_prefetch_non_temporal((void *)(cq0 + CQE_SZ(10)));
149 rte_prefetch_non_temporal((void *)(cq0 + CQE_SZ(11)));
151 /* Get NIX_RX_SG_S for size and buffer pointer */
152 cq0_w8 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(0) + 64));
153 cq1_w8 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(1) + 64));
154 cq2_w8 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(2) + 64));
155 cq3_w8 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(3) + 64));
157 /* Extract mbuf from NIX_RX_SG_S */
158 mbuf01 = vzip2q_u64(cq0_w8, cq1_w8);
159 mbuf23 = vzip2q_u64(cq2_w8, cq3_w8);
160 mbuf01 = vqsubq_u64(mbuf01, data_off);
161 mbuf23 = vqsubq_u64(mbuf23, data_off);
163 /* Move mbufs to scalar registers for future use */
164 mbuf0 = (struct rte_mbuf *)vgetq_lane_u64(mbuf01, 0);
165 mbuf1 = (struct rte_mbuf *)vgetq_lane_u64(mbuf01, 1);
166 mbuf2 = (struct rte_mbuf *)vgetq_lane_u64(mbuf23, 0);
167 mbuf3 = (struct rte_mbuf *)vgetq_lane_u64(mbuf23, 1);
169 /* Mask to get packet len from NIX_RX_SG_S */
170 const uint8x16_t shuf_msk = {
171 0xFF, 0xFF, /* pkt_type set as unknown */
172 0xFF, 0xFF, /* pkt_type set as unknown */
173 0, 1, /* octet 1~0, low 16 bits pkt_len */
174 0xFF, 0xFF, /* skip high 16 bits pkt_len, zero out */
175 0, 1, /* octet 1~0, 16 bits data_len */
177 0xFF, 0xFF, 0xFF, 0xFF
180 /* Form the rx_descriptor_fields1 with pkt_len and data_len */
181 f0 = vqtbl1q_u8(cq0_w8, shuf_msk);
182 f1 = vqtbl1q_u8(cq1_w8, shuf_msk);
183 f2 = vqtbl1q_u8(cq2_w8, shuf_msk);
184 f3 = vqtbl1q_u8(cq3_w8, shuf_msk);
186 /* Load CQE word0 and word 1 */
187 uint64x2_t cq0_w0 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(0)));
188 uint64x2_t cq1_w0 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(1)));
189 uint64x2_t cq2_w0 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(2)));
190 uint64x2_t cq3_w0 = vld1q_u64((uint64_t *)(cq0 + CQE_SZ(3)));
192 if (flags & NIX_RX_OFFLOAD_RSS_F) {
193 /* Fill rss in the rx_descriptor_fields1 */
194 f0 = vsetq_lane_u32(vgetq_lane_u32(cq0_w0, 0), f0, 3);
195 f1 = vsetq_lane_u32(vgetq_lane_u32(cq1_w0, 0), f1, 3);
196 f2 = vsetq_lane_u32(vgetq_lane_u32(cq2_w0, 0), f2, 3);
197 f3 = vsetq_lane_u32(vgetq_lane_u32(cq3_w0, 0), f3, 3);
198 ol_flags0 = PKT_RX_RSS_HASH;
199 ol_flags1 = PKT_RX_RSS_HASH;
200 ol_flags2 = PKT_RX_RSS_HASH;
201 ol_flags3 = PKT_RX_RSS_HASH;
203 ol_flags0 = 0; ol_flags1 = 0;
204 ol_flags2 = 0; ol_flags3 = 0;
207 if (flags & NIX_RX_OFFLOAD_PTYPE_F) {
208 /* Fill packet_type in the rx_descriptor_fields1 */
209 f0 = vsetq_lane_u32(nix_ptype_get(lookup_mem,
210 vgetq_lane_u64(cq0_w0, 1)), f0, 0);
211 f1 = vsetq_lane_u32(nix_ptype_get(lookup_mem,
212 vgetq_lane_u64(cq1_w0, 1)), f1, 0);
213 f2 = vsetq_lane_u32(nix_ptype_get(lookup_mem,
214 vgetq_lane_u64(cq2_w0, 1)), f2, 0);
215 f3 = vsetq_lane_u32(nix_ptype_get(lookup_mem,
216 vgetq_lane_u64(cq3_w0, 1)), f3, 0);
219 if (flags & NIX_RX_OFFLOAD_CHECKSUM_F) {
220 ol_flags0 |= nix_rx_olflags_get(lookup_mem,
221 vgetq_lane_u64(cq0_w0, 1));
222 ol_flags1 |= nix_rx_olflags_get(lookup_mem,
223 vgetq_lane_u64(cq1_w0, 1));
224 ol_flags2 |= nix_rx_olflags_get(lookup_mem,
225 vgetq_lane_u64(cq2_w0, 1));
226 ol_flags3 |= nix_rx_olflags_get(lookup_mem,
227 vgetq_lane_u64(cq3_w0, 1));
230 if (flags & NIX_RX_OFFLOAD_VLAN_STRIP_F) {
231 uint64_t cq0_w2 = *(uint64_t *)(cq0 + CQE_SZ(0) + 16);
232 uint64_t cq1_w2 = *(uint64_t *)(cq0 + CQE_SZ(1) + 16);
233 uint64_t cq2_w2 = *(uint64_t *)(cq0 + CQE_SZ(2) + 16);
234 uint64_t cq3_w2 = *(uint64_t *)(cq0 + CQE_SZ(3) + 16);
236 ol_flags0 = nix_vlan_update(cq0_w2, ol_flags0, &f0);
237 ol_flags1 = nix_vlan_update(cq1_w2, ol_flags1, &f1);
238 ol_flags2 = nix_vlan_update(cq2_w2, ol_flags2, &f2);
239 ol_flags3 = nix_vlan_update(cq3_w2, ol_flags3, &f3);
241 ol_flags0 = nix_qinq_update(cq0_w2, ol_flags0, mbuf0);
242 ol_flags1 = nix_qinq_update(cq1_w2, ol_flags1, mbuf1);
243 ol_flags2 = nix_qinq_update(cq2_w2, ol_flags2, mbuf2);
244 ol_flags3 = nix_qinq_update(cq3_w2, ol_flags3, mbuf3);
247 if (flags & NIX_RX_OFFLOAD_MARK_UPDATE_F) {
248 ol_flags0 = nix_update_match_id(*(uint16_t *)
249 (cq0 + CQE_SZ(0) + 38), ol_flags0, mbuf0);
250 ol_flags1 = nix_update_match_id(*(uint16_t *)
251 (cq0 + CQE_SZ(1) + 38), ol_flags1, mbuf1);
252 ol_flags2 = nix_update_match_id(*(uint16_t *)
253 (cq0 + CQE_SZ(2) + 38), ol_flags2, mbuf2);
254 ol_flags3 = nix_update_match_id(*(uint16_t *)
255 (cq0 + CQE_SZ(3) + 38), ol_flags3, mbuf3);
258 /* Form rearm_data with ol_flags */
259 rearm0 = vsetq_lane_u64(ol_flags0, rearm0, 1);
260 rearm1 = vsetq_lane_u64(ol_flags1, rearm1, 1);
261 rearm2 = vsetq_lane_u64(ol_flags2, rearm2, 1);
262 rearm3 = vsetq_lane_u64(ol_flags3, rearm3, 1);
264 /* Update rx_descriptor_fields1 */
265 vst1q_u64((uint64_t *)mbuf0->rx_descriptor_fields1, f0);
266 vst1q_u64((uint64_t *)mbuf1->rx_descriptor_fields1, f1);
267 vst1q_u64((uint64_t *)mbuf2->rx_descriptor_fields1, f2);
268 vst1q_u64((uint64_t *)mbuf3->rx_descriptor_fields1, f3);
270 /* Update rearm_data */
271 vst1q_u64((uint64_t *)mbuf0->rearm_data, rearm0);
272 vst1q_u64((uint64_t *)mbuf1->rearm_data, rearm1);
273 vst1q_u64((uint64_t *)mbuf2->rearm_data, rearm2);
274 vst1q_u64((uint64_t *)mbuf3->rearm_data, rearm3);
276 /* Store the mbufs to rx_pkts */
277 vst1q_u64((uint64_t *)&rx_pkts[packets], mbuf01);
278 vst1q_u64((uint64_t *)&rx_pkts[packets + 2], mbuf23);
281 otx2_prefetch_store_keep(mbuf0);
282 otx2_prefetch_store_keep(mbuf1);
283 otx2_prefetch_store_keep(mbuf2);
284 otx2_prefetch_store_keep(mbuf3);
286 /* Mark mempool obj as "get" as it is alloc'ed by NIX */
287 __mempool_check_cookies(mbuf0->pool, (void **)&mbuf0, 1, 1);
288 __mempool_check_cookies(mbuf1->pool, (void **)&mbuf1, 1, 1);
289 __mempool_check_cookies(mbuf2->pool, (void **)&mbuf2, 1, 1);
290 __mempool_check_cookies(mbuf3->pool, (void **)&mbuf3, 1, 1);
292 /* Advance head pointer and packets */
293 head += NIX_DESCS_PER_LOOP; head &= qmask;
294 packets += NIX_DESCS_PER_LOOP;
298 rxq->available -= packets;
301 /* Free all the CQs that we've processed */
302 otx2_write64((rxq->wdata | packets), rxq->cq_door);
309 static inline uint16_t
310 nix_recv_pkts_vector(void *rx_queue, struct rte_mbuf **rx_pkts,
311 uint16_t pkts, const uint16_t flags)
313 RTE_SET_USED(rx_queue);
314 RTE_SET_USED(rx_pkts);
323 #define R(name, f5, f4, f3, f2, f1, f0, flags) \
324 static uint16_t __rte_noinline __hot \
325 otx2_nix_recv_pkts_ ## name(void *rx_queue, \
326 struct rte_mbuf **rx_pkts, uint16_t pkts) \
328 return nix_recv_pkts(rx_queue, rx_pkts, pkts, (flags)); \
331 static uint16_t __rte_noinline __hot \
332 otx2_nix_recv_pkts_mseg_ ## name(void *rx_queue, \
333 struct rte_mbuf **rx_pkts, uint16_t pkts) \
335 return nix_recv_pkts(rx_queue, rx_pkts, pkts, \
336 (flags) | NIX_RX_MULTI_SEG_F); \
339 static uint16_t __rte_noinline __hot \
340 otx2_nix_recv_pkts_vec_ ## name(void *rx_queue, \
341 struct rte_mbuf **rx_pkts, uint16_t pkts) \
343 /* TSTMP is not supported by vector */ \
344 if ((flags) & NIX_RX_OFFLOAD_TSTAMP_F) \
346 return nix_recv_pkts_vector(rx_queue, rx_pkts, pkts, (flags)); \
349 NIX_RX_FASTPATH_MODES
353 pick_rx_func(struct rte_eth_dev *eth_dev,
354 const eth_rx_burst_t rx_burst[2][2][2][2][2][2])
356 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
358 /* [TSTMP] [MARK] [VLAN] [CKSUM] [PTYPE] [RSS] */
359 eth_dev->rx_pkt_burst = rx_burst
360 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_TSTAMP_F)]
361 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_MARK_UPDATE_F)]
362 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_VLAN_STRIP_F)]
363 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_CHECKSUM_F)]
364 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_PTYPE_F)]
365 [!!(dev->rx_offload_flags & NIX_RX_OFFLOAD_RSS_F)];
369 otx2_eth_set_rx_function(struct rte_eth_dev *eth_dev)
371 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
373 const eth_rx_burst_t nix_eth_rx_burst[2][2][2][2][2][2] = {
374 #define R(name, f5, f4, f3, f2, f1, f0, flags) \
375 [f5][f4][f3][f2][f1][f0] = otx2_nix_recv_pkts_ ## name,
377 NIX_RX_FASTPATH_MODES
381 const eth_rx_burst_t nix_eth_rx_burst_mseg[2][2][2][2][2][2] = {
382 #define R(name, f5, f4, f3, f2, f1, f0, flags) \
383 [f5][f4][f3][f2][f1][f0] = otx2_nix_recv_pkts_mseg_ ## name,
385 NIX_RX_FASTPATH_MODES
389 const eth_rx_burst_t nix_eth_rx_vec_burst[2][2][2][2][2][2] = {
390 #define R(name, f5, f4, f3, f2, f1, f0, flags) \
391 [f5][f4][f3][f2][f1][f0] = otx2_nix_recv_pkts_vec_ ## name,
393 NIX_RX_FASTPATH_MODES
397 /* For PTP enabled, scalar rx function should be chosen as most of the
398 * PTP apps are implemented to rx burst 1 pkt.
400 if (dev->scalar_ena || dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP)
401 pick_rx_func(eth_dev, nix_eth_rx_burst);
403 pick_rx_func(eth_dev, nix_eth_rx_vec_burst);
405 if (dev->rx_offloads & DEV_RX_OFFLOAD_SCATTER)
406 pick_rx_func(eth_dev, nix_eth_rx_burst_mseg);
408 /* Copy multi seg version with no offload for tear down sequence */
409 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
410 dev->rx_pkt_burst_no_offload =
411 nix_eth_rx_burst_mseg[0][0][0][0][0][0];