net/cnxk: add Rx burst for CN9K
[dpdk.git] / drivers / net / cnxk / cn9k_rx.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #ifndef __CN9K_RX_H__
6 #define __CN9K_RX_H__
7
8 #include <rte_ether.h>
9
10 #define NIX_RX_OFFLOAD_NONE          (0)
11 #define NIX_RX_OFFLOAD_RSS_F         BIT(0)
12 #define NIX_RX_OFFLOAD_PTYPE_F       BIT(1)
13 #define NIX_RX_OFFLOAD_CHECKSUM_F    BIT(2)
14 #define NIX_RX_OFFLOAD_MARK_UPDATE_F BIT(3)
15
16 /* Flags to control cqe_to_mbuf conversion function.
17  * Defining it from backwards to denote its been
18  * not used as offload flags to pick function
19  */
20 #define NIX_RX_MULTI_SEG_F BIT(15)
21
22 #define CNXK_NIX_CQ_ENTRY_SZ 128
23 #define NIX_DESCS_PER_LOOP   4
24 #define CQE_CAST(x)          ((struct nix_cqe_hdr_s *)(x))
25 #define CQE_SZ(x)            ((x) * CNXK_NIX_CQ_ENTRY_SZ)
26
27 union mbuf_initializer {
28         struct {
29                 uint16_t data_off;
30                 uint16_t refcnt;
31                 uint16_t nb_segs;
32                 uint16_t port;
33         } fields;
34         uint64_t value;
35 };
36
37 static __rte_always_inline uint64_t
38 nix_clear_data_off(uint64_t oldval)
39 {
40         union mbuf_initializer mbuf_init = {.value = oldval};
41
42         mbuf_init.fields.data_off = 0;
43         return mbuf_init.value;
44 }
45
46 static __rte_always_inline struct rte_mbuf *
47 nix_get_mbuf_from_cqe(void *cq, const uint64_t data_off)
48 {
49         rte_iova_t buff;
50
51         /* Skip CQE, NIX_RX_PARSE_S and SG HDR(9 DWORDs) and peek buff addr */
52         buff = *((rte_iova_t *)((uint64_t *)cq + 9));
53         return (struct rte_mbuf *)(buff - data_off);
54 }
55
56 static __rte_always_inline uint32_t
57 nix_ptype_get(const void *const lookup_mem, const uint64_t in)
58 {
59         const uint16_t *const ptype = lookup_mem;
60         const uint16_t lh_lg_lf = (in & 0xFFF0000000000000) >> 52;
61         const uint16_t tu_l2 = ptype[(in & 0x000FFFF000000000) >> 36];
62         const uint16_t il4_tu = ptype[PTYPE_NON_TUNNEL_ARRAY_SZ + lh_lg_lf];
63
64         return (il4_tu << PTYPE_NON_TUNNEL_WIDTH) | tu_l2;
65 }
66
67 static __rte_always_inline uint32_t
68 nix_rx_olflags_get(const void *const lookup_mem, const uint64_t in)
69 {
70         const uint32_t *const ol_flags =
71                 (const uint32_t *)((const uint8_t *)lookup_mem +
72                                    PTYPE_ARRAY_SZ);
73
74         return ol_flags[(in & 0xfff00000) >> 20];
75 }
76
77 static inline uint64_t
78 nix_update_match_id(const uint16_t match_id, uint64_t ol_flags,
79                     struct rte_mbuf *mbuf)
80 {
81         /* There is no separate bit to check match_id
82          * is valid or not? and no flag to identify it is an
83          * RTE_FLOW_ACTION_TYPE_FLAG vs RTE_FLOW_ACTION_TYPE_MARK
84          * action. The former case addressed through 0 being invalid
85          * value and inc/dec match_id pair when MARK is activated.
86          * The later case addressed through defining
87          * CNXK_FLOW_MARK_DEFAULT as value for
88          * RTE_FLOW_ACTION_TYPE_MARK.
89          * This would translate to not use
90          * CNXK_FLOW_ACTION_FLAG_DEFAULT - 1 and
91          * CNXK_FLOW_ACTION_FLAG_DEFAULT for match_id.
92          * i.e valid mark_id's are from
93          * 0 to CNXK_FLOW_ACTION_FLAG_DEFAULT - 2
94          */
95         if (likely(match_id)) {
96                 ol_flags |= PKT_RX_FDIR;
97                 if (match_id != CNXK_FLOW_ACTION_FLAG_DEFAULT) {
98                         ol_flags |= PKT_RX_FDIR_ID;
99                         mbuf->hash.fdir.hi = match_id - 1;
100                 }
101         }
102
103         return ol_flags;
104 }
105
106 static __rte_always_inline void
107 cn9k_nix_cqe_to_mbuf(const struct nix_cqe_hdr_s *cq, const uint32_t tag,
108                      struct rte_mbuf *mbuf, const void *lookup_mem,
109                      const uint64_t val, const uint16_t flag)
110 {
111         const union nix_rx_parse_u *rx =
112                 (const union nix_rx_parse_u *)((const uint64_t *)cq + 1);
113         const uint16_t len = rx->cn9k.pkt_lenm1 + 1;
114         const uint64_t w1 = *(const uint64_t *)rx;
115         uint64_t ol_flags = 0;
116
117         /* Mark mempool obj as "get" as it is alloc'ed by NIX */
118         __mempool_check_cookies(mbuf->pool, (void **)&mbuf, 1, 1);
119
120         if (flag & NIX_RX_OFFLOAD_PTYPE_F)
121                 mbuf->packet_type = nix_ptype_get(lookup_mem, w1);
122         else
123                 mbuf->packet_type = 0;
124
125         if (flag & NIX_RX_OFFLOAD_RSS_F) {
126                 mbuf->hash.rss = tag;
127                 ol_flags |= PKT_RX_RSS_HASH;
128         }
129
130         if (flag & NIX_RX_OFFLOAD_CHECKSUM_F)
131                 ol_flags |= nix_rx_olflags_get(lookup_mem, w1);
132
133         if (flag & NIX_RX_OFFLOAD_MARK_UPDATE_F)
134                 ol_flags =
135                         nix_update_match_id(rx->cn9k.match_id, ol_flags, mbuf);
136
137         mbuf->ol_flags = ol_flags;
138         *(uint64_t *)(&mbuf->rearm_data) = val;
139         mbuf->pkt_len = len;
140
141         mbuf->data_len = len;
142         mbuf->next = NULL;
143 }
144
145 static inline uint16_t
146 nix_rx_nb_pkts(struct cn9k_eth_rxq *rxq, const uint64_t wdata,
147                const uint16_t pkts, const uint32_t qmask)
148 {
149         uint32_t available = rxq->available;
150
151         /* Update the available count if cached value is not enough */
152         if (unlikely(available < pkts)) {
153                 uint64_t reg, head, tail;
154
155                 /* Use LDADDA version to avoid reorder */
156                 reg = roc_atomic64_add_sync(wdata, rxq->cq_status);
157                 /* CQ_OP_STATUS operation error */
158                 if (reg & BIT_ULL(NIX_CQ_OP_STAT_OP_ERR) ||
159                     reg & BIT_ULL(NIX_CQ_OP_STAT_CQ_ERR))
160                         return 0;
161
162                 tail = reg & 0xFFFFF;
163                 head = (reg >> 20) & 0xFFFFF;
164                 if (tail < head)
165                         available = tail - head + qmask + 1;
166                 else
167                         available = tail - head;
168
169                 rxq->available = available;
170         }
171
172         return RTE_MIN(pkts, available);
173 }
174
175 static __rte_always_inline uint16_t
176 cn9k_nix_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t pkts,
177                    const uint16_t flags)
178 {
179         struct cn9k_eth_rxq *rxq = rx_queue;
180         const uint64_t mbuf_init = rxq->mbuf_initializer;
181         const void *lookup_mem = rxq->lookup_mem;
182         const uint64_t data_off = rxq->data_off;
183         const uintptr_t desc = rxq->desc;
184         const uint64_t wdata = rxq->wdata;
185         const uint32_t qmask = rxq->qmask;
186         uint16_t packets = 0, nb_pkts;
187         uint32_t head = rxq->head;
188         struct nix_cqe_hdr_s *cq;
189         struct rte_mbuf *mbuf;
190
191         nb_pkts = nix_rx_nb_pkts(rxq, wdata, pkts, qmask);
192
193         while (packets < nb_pkts) {
194                 /* Prefetch N desc ahead */
195                 rte_prefetch_non_temporal(
196                         (void *)(desc + (CQE_SZ((head + 2) & qmask))));
197                 cq = (struct nix_cqe_hdr_s *)(desc + CQE_SZ(head));
198
199                 mbuf = nix_get_mbuf_from_cqe(cq, data_off);
200
201                 cn9k_nix_cqe_to_mbuf(cq, cq->tag, mbuf, lookup_mem, mbuf_init,
202                                      flags);
203                 rx_pkts[packets++] = mbuf;
204                 roc_prefetch_store_keep(mbuf);
205                 head++;
206                 head &= qmask;
207         }
208
209         rxq->head = head;
210         rxq->available -= nb_pkts;
211
212         /* Free all the CQs that we've processed */
213         plt_write64((wdata | nb_pkts), rxq->cq_door);
214
215         return nb_pkts;
216 }
217
218 #define RSS_F     NIX_RX_OFFLOAD_RSS_F
219 #define PTYPE_F   NIX_RX_OFFLOAD_PTYPE_F
220 #define CKSUM_F   NIX_RX_OFFLOAD_CHECKSUM_F
221 #define MARK_F    NIX_RX_OFFLOAD_MARK_UPDATE_F
222
223 /* [MARK] [CKSUM] [PTYPE] [RSS] */
224 #define NIX_RX_FASTPATH_MODES                                          \
225 R(no_offload,                   0, 0, 0, 0, NIX_RX_OFFLOAD_NONE)       \
226 R(rss,                          0, 0, 0, 1, RSS_F)                     \
227 R(ptype,                        0, 0, 1, 0, PTYPE_F)                   \
228 R(ptype_rss,                    0, 0, 1, 1, PTYPE_F | RSS_F)           \
229 R(cksum,                        0, 1, 0, 0, CKSUM_F)                   \
230 R(cksum_rss,                    0, 1, 0, 1, CKSUM_F | RSS_F)           \
231 R(cksum_ptype,                  0, 1, 1, 0, CKSUM_F | PTYPE_F)         \
232 R(cksum_ptype_rss,              0, 1, 1, 1, CKSUM_F | PTYPE_F | RSS_F) \
233 R(mark,                         1, 0, 0, 0, MARK_F)                    \
234 R(mark_rss,                     1, 0, 0, 1, MARK_F | RSS_F)            \
235 R(mark_ptype,                   1, 0, 1, 0, MARK_F | PTYPE_F)          \
236 R(mark_ptype_rss,               1, 0, 1, 1, MARK_F | PTYPE_F | RSS_F)  \
237 R(mark_cksum,                   1, 1, 0, 0, MARK_F | CKSUM_F)          \
238 R(mark_cksum_rss,               1, 1, 0, 1, MARK_F | CKSUM_F | RSS_F)  \
239 R(mark_cksum_ptype,             1, 1, 1, 0, MARK_F | CKSUM_F | PTYPE_F)\
240 R(mark_cksum_ptype_rss,         1, 1, 1, 1, MARK_F | CKSUM_F | PTYPE_F | RSS_F)
241
242 #define R(name, f3, f2, f1, f0, flags)                                         \
243         uint16_t __rte_noinline __rte_hot cn9k_nix_recv_pkts_##name(           \
244                 void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t pkts);
245
246 NIX_RX_FASTPATH_MODES
247 #undef R
248
249 #endif /* __CN9K_RX_H__ */