01c9d291a385b3a90353e97f82cc0cc27eb393ba
[dpdk.git] / drivers / net / cnxk / cn10k_rx.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4 #ifndef __CN10K_RX_H__
5 #define __CN10K_RX_H__
6
7 #include <rte_ether.h>
8
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)
14
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
18  */
19 #define NIX_RX_MULTI_SEG_F BIT(15)
20
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)
25
26 union mbuf_initializer {
27         struct {
28                 uint16_t data_off;
29                 uint16_t refcnt;
30                 uint16_t nb_segs;
31                 uint16_t port;
32         } fields;
33         uint64_t value;
34 };
35
36 static __rte_always_inline uint64_t
37 nix_clear_data_off(uint64_t oldval)
38 {
39         union mbuf_initializer mbuf_init = {.value = oldval};
40
41         mbuf_init.fields.data_off = 0;
42         return mbuf_init.value;
43 }
44
45 static __rte_always_inline struct rte_mbuf *
46 nix_get_mbuf_from_cqe(void *cq, const uint64_t data_off)
47 {
48         rte_iova_t buff;
49
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);
53 }
54
55 static __rte_always_inline uint32_t
56 nix_ptype_get(const void *const lookup_mem, const uint64_t in)
57 {
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];
62
63         return (il4_tu << PTYPE_NON_TUNNEL_WIDTH) | tu_l2;
64 }
65
66 static __rte_always_inline uint32_t
67 nix_rx_olflags_get(const void *const lookup_mem, const uint64_t in)
68 {
69         const uint32_t *const ol_flags =
70                 (const uint32_t *)((const uint8_t *)lookup_mem +
71                                    PTYPE_ARRAY_SZ);
72
73         return ol_flags[(in & 0xfff00000) >> 20];
74 }
75
76 static inline uint64_t
77 nix_update_match_id(const uint16_t match_id, uint64_t ol_flags,
78                     struct rte_mbuf *mbuf)
79 {
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
93          */
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;
99                 }
100         }
101
102         return ol_flags;
103 }
104
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)
109 {
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;
115
116         /* Mark mempool obj as "get" as it is alloc'ed by NIX */
117         __mempool_check_cookies(mbuf->pool, (void **)&mbuf, 1, 1);
118
119         if (flag & NIX_RX_OFFLOAD_PTYPE_F)
120                 mbuf->packet_type = nix_ptype_get(lookup_mem, w1);
121         else
122                 mbuf->packet_type = 0;
123
124         if (flag & NIX_RX_OFFLOAD_RSS_F) {
125                 mbuf->hash.rss = tag;
126                 ol_flags |= PKT_RX_RSS_HASH;
127         }
128
129         if (flag & NIX_RX_OFFLOAD_CHECKSUM_F)
130                 ol_flags |= nix_rx_olflags_get(lookup_mem, w1);
131
132         if (flag & NIX_RX_OFFLOAD_MARK_UPDATE_F)
133                 ol_flags = nix_update_match_id(rx->match_id, ol_flags, mbuf);
134
135         mbuf->ol_flags = ol_flags;
136         *(uint64_t *)(&mbuf->rearm_data) = val;
137         mbuf->pkt_len = len;
138
139         mbuf->data_len = len;
140         mbuf->next = NULL;
141 }
142
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)
146 {
147         uint32_t available = rxq->available;
148
149         /* Update the available count if cached value is not enough */
150         if (unlikely(available < pkts)) {
151                 uint64_t reg, head, tail;
152
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))
158                         return 0;
159
160                 tail = reg & 0xFFFFF;
161                 head = (reg >> 20) & 0xFFFFF;
162                 if (tail < head)
163                         available = tail - head + qmask + 1;
164                 else
165                         available = tail - head;
166
167                 rxq->available = available;
168         }
169
170         return RTE_MIN(pkts, available);
171 }
172
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)
176 {
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;
188
189         nb_pkts = nix_rx_nb_pkts(rxq, wdata, pkts, qmask);
190
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));
196
197                 mbuf = nix_get_mbuf_from_cqe(cq, data_off);
198
199                 cn10k_nix_cqe_to_mbuf(cq, cq->tag, mbuf, lookup_mem, mbuf_init,
200                                       flags);
201                 rx_pkts[packets++] = mbuf;
202                 roc_prefetch_store_keep(mbuf);
203                 head++;
204                 head &= qmask;
205         }
206
207         rxq->head = head;
208         rxq->available -= nb_pkts;
209
210         /* Free all the CQs that we've processed */
211         plt_write64((wdata | nb_pkts), rxq->cq_door);
212
213         return nb_pkts;
214 }
215
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
220
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)
239
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);
243
244 NIX_RX_FASTPATH_MODES
245 #undef R
246
247 #endif /* __CN10K_RX_H__ */