1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
9 #define NIX_RX_OFFLOAD_NONE (0)
10 #define NIX_RX_OFFLOAD_RSS_F BIT(0)
11 #define NIX_RX_OFFLOAD_PTYPE_F BIT(1)
12 #define NIX_RX_OFFLOAD_CHECKSUM_F BIT(2)
13 #define NIX_RX_OFFLOAD_MARK_UPDATE_F BIT(3)
15 /* Flags to control cqe_to_mbuf conversion function.
16 * Defining it from backwards to denote its been
17 * not used as offload flags to pick function
19 #define NIX_RX_MULTI_SEG_F BIT(15)
21 #define CNXK_NIX_CQ_ENTRY_SZ 128
22 #define NIX_DESCS_PER_LOOP 4
23 #define CQE_CAST(x) ((struct nix_cqe_hdr_s *)(x))
24 #define CQE_SZ(x) ((x) * CNXK_NIX_CQ_ENTRY_SZ)
26 union mbuf_initializer {
36 static __rte_always_inline uint64_t
37 nix_clear_data_off(uint64_t oldval)
39 union mbuf_initializer mbuf_init = {.value = oldval};
41 mbuf_init.fields.data_off = 0;
42 return mbuf_init.value;
45 static __rte_always_inline struct rte_mbuf *
46 nix_get_mbuf_from_cqe(void *cq, const uint64_t data_off)
50 /* Skip CQE, NIX_RX_PARSE_S and SG HDR(9 DWORDs) and peek buff addr */
51 buff = *((rte_iova_t *)((uint64_t *)cq + 9));
52 return (struct rte_mbuf *)(buff - data_off);
55 static __rte_always_inline uint32_t
56 nix_ptype_get(const void *const lookup_mem, const uint64_t in)
58 const uint16_t *const ptype = lookup_mem;
59 const uint16_t lh_lg_lf = (in & 0xFFF0000000000000) >> 52;
60 const uint16_t tu_l2 = ptype[(in & 0x000FFFF000000000) >> 36];
61 const uint16_t il4_tu = ptype[PTYPE_NON_TUNNEL_ARRAY_SZ + lh_lg_lf];
63 return (il4_tu << PTYPE_NON_TUNNEL_WIDTH) | tu_l2;
66 static __rte_always_inline uint32_t
67 nix_rx_olflags_get(const void *const lookup_mem, const uint64_t in)
69 const uint32_t *const ol_flags =
70 (const uint32_t *)((const uint8_t *)lookup_mem +
73 return ol_flags[(in & 0xfff00000) >> 20];
76 static inline uint64_t
77 nix_update_match_id(const uint16_t match_id, uint64_t ol_flags,
78 struct rte_mbuf *mbuf)
80 /* There is no separate bit to check match_id
81 * is valid or not? and no flag to identify it is an
82 * RTE_FLOW_ACTION_TYPE_FLAG vs RTE_FLOW_ACTION_TYPE_MARK
83 * action. The former case addressed through 0 being invalid
84 * value and inc/dec match_id pair when MARK is activated.
85 * The later case addressed through defining
86 * CNXK_FLOW_MARK_DEFAULT as value for
87 * RTE_FLOW_ACTION_TYPE_MARK.
88 * This would translate to not use
89 * CNXK_FLOW_ACTION_FLAG_DEFAULT - 1 and
90 * CNXK_FLOW_ACTION_FLAG_DEFAULT for match_id.
91 * i.e valid mark_id's are from
92 * 0 to CNXK_FLOW_ACTION_FLAG_DEFAULT - 2
94 if (likely(match_id)) {
95 ol_flags |= PKT_RX_FDIR;
96 if (match_id != CNXK_FLOW_ACTION_FLAG_DEFAULT) {
97 ol_flags |= PKT_RX_FDIR_ID;
98 mbuf->hash.fdir.hi = match_id - 1;
105 static __rte_always_inline void
106 cn10k_nix_cqe_to_mbuf(const struct nix_cqe_hdr_s *cq, const uint32_t tag,
107 struct rte_mbuf *mbuf, const void *lookup_mem,
108 const uint64_t val, const uint16_t flag)
110 const union nix_rx_parse_u *rx =
111 (const union nix_rx_parse_u *)((const uint64_t *)cq + 1);
112 const uint16_t len = rx->pkt_lenm1 + 1;
113 const uint64_t w1 = *(const uint64_t *)rx;
114 uint64_t ol_flags = 0;
116 /* Mark mempool obj as "get" as it is alloc'ed by NIX */
117 __mempool_check_cookies(mbuf->pool, (void **)&mbuf, 1, 1);
119 if (flag & NIX_RX_OFFLOAD_PTYPE_F)
120 mbuf->packet_type = nix_ptype_get(lookup_mem, w1);
122 mbuf->packet_type = 0;
124 if (flag & NIX_RX_OFFLOAD_RSS_F) {
125 mbuf->hash.rss = tag;
126 ol_flags |= PKT_RX_RSS_HASH;
129 if (flag & NIX_RX_OFFLOAD_CHECKSUM_F)
130 ol_flags |= nix_rx_olflags_get(lookup_mem, w1);
132 if (flag & NIX_RX_OFFLOAD_MARK_UPDATE_F)
133 ol_flags = nix_update_match_id(rx->match_id, ol_flags, mbuf);
135 mbuf->ol_flags = ol_flags;
136 *(uint64_t *)(&mbuf->rearm_data) = val;
139 mbuf->data_len = len;
143 static inline uint16_t
144 nix_rx_nb_pkts(struct cn10k_eth_rxq *rxq, const uint64_t wdata,
145 const uint16_t pkts, const uint32_t qmask)
147 uint32_t available = rxq->available;
149 /* Update the available count if cached value is not enough */
150 if (unlikely(available < pkts)) {
151 uint64_t reg, head, tail;
153 /* Use LDADDA version to avoid reorder */
154 reg = roc_atomic64_add_sync(wdata, rxq->cq_status);
155 /* CQ_OP_STATUS operation error */
156 if (reg & BIT_ULL(NIX_CQ_OP_STAT_OP_ERR) ||
157 reg & BIT_ULL(NIX_CQ_OP_STAT_CQ_ERR))
160 tail = reg & 0xFFFFF;
161 head = (reg >> 20) & 0xFFFFF;
163 available = tail - head + qmask + 1;
165 available = tail - head;
167 rxq->available = available;
170 return RTE_MIN(pkts, available);
173 static __rte_always_inline uint16_t
174 cn10k_nix_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t pkts,
175 const uint16_t flags)
177 struct cn10k_eth_rxq *rxq = rx_queue;
178 const uint64_t mbuf_init = rxq->mbuf_initializer;
179 const void *lookup_mem = rxq->lookup_mem;
180 const uint64_t data_off = rxq->data_off;
181 const uintptr_t desc = rxq->desc;
182 const uint64_t wdata = rxq->wdata;
183 const uint32_t qmask = rxq->qmask;
184 uint16_t packets = 0, nb_pkts;
185 uint32_t head = rxq->head;
186 struct nix_cqe_hdr_s *cq;
187 struct rte_mbuf *mbuf;
189 nb_pkts = nix_rx_nb_pkts(rxq, wdata, pkts, qmask);
191 while (packets < nb_pkts) {
192 /* Prefetch N desc ahead */
193 rte_prefetch_non_temporal(
194 (void *)(desc + (CQE_SZ((head + 2) & qmask))));
195 cq = (struct nix_cqe_hdr_s *)(desc + CQE_SZ(head));
197 mbuf = nix_get_mbuf_from_cqe(cq, data_off);
199 cn10k_nix_cqe_to_mbuf(cq, cq->tag, mbuf, lookup_mem, mbuf_init,
201 rx_pkts[packets++] = mbuf;
202 roc_prefetch_store_keep(mbuf);
208 rxq->available -= nb_pkts;
210 /* Free all the CQs that we've processed */
211 plt_write64((wdata | nb_pkts), rxq->cq_door);
216 #define RSS_F NIX_RX_OFFLOAD_RSS_F
217 #define PTYPE_F NIX_RX_OFFLOAD_PTYPE_F
218 #define CKSUM_F NIX_RX_OFFLOAD_CHECKSUM_F
219 #define MARK_F NIX_RX_OFFLOAD_MARK_UPDATE_F
221 /* [MARK] [CKSUM] [PTYPE] [RSS] */
222 #define NIX_RX_FASTPATH_MODES \
223 R(no_offload, 0, 0, 0, 0, NIX_RX_OFFLOAD_NONE) \
224 R(rss, 0, 0, 0, 1, RSS_F) \
225 R(ptype, 0, 0, 1, 0, PTYPE_F) \
226 R(ptype_rss, 0, 0, 1, 1, PTYPE_F | RSS_F) \
227 R(cksum, 0, 1, 0, 0, CKSUM_F) \
228 R(cksum_rss, 0, 1, 0, 1, CKSUM_F | RSS_F) \
229 R(cksum_ptype, 0, 1, 1, 0, CKSUM_F | PTYPE_F) \
230 R(cksum_ptype_rss, 0, 1, 1, 1, CKSUM_F | PTYPE_F | RSS_F) \
231 R(mark, 1, 0, 0, 0, MARK_F) \
232 R(mark_rss, 1, 0, 0, 1, MARK_F | RSS_F) \
233 R(mark_ptype, 1, 0, 1, 0, MARK_F | PTYPE_F) \
234 R(mark_ptype_rss, 1, 0, 1, 1, MARK_F | PTYPE_F | RSS_F) \
235 R(mark_cksum, 1, 1, 0, 0, MARK_F | CKSUM_F) \
236 R(mark_cksum_rss, 1, 1, 0, 1, MARK_F | CKSUM_F | RSS_F) \
237 R(mark_cksum_ptype, 1, 1, 1, 0, MARK_F | CKSUM_F | PTYPE_F)\
238 R(mark_cksum_ptype_rss, 1, 1, 1, 1, MARK_F | CKSUM_F | PTYPE_F | RSS_F)
240 #define R(name, f3, f2, f1, f0, flags) \
241 uint16_t __rte_noinline __rte_hot cn10k_nix_recv_pkts_##name( \
242 void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t pkts);
244 NIX_RX_FASTPATH_MODES
247 #endif /* __CN10K_RX_H__ */