351ad0fcb43d5c0213a80ef9b719e835f18d5f4a
[dpdk.git] / drivers / net / octeontx2 / otx2_rx.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #ifndef __OTX2_RX_H__
6 #define __OTX2_RX_H__
7
8 /* Default mark value used when none is provided. */
9 #define OTX2_FLOW_ACTION_FLAG_DEFAULT   0xffff
10
11 #define PTYPE_NON_TUNNEL_WIDTH          16
12 #define PTYPE_TUNNEL_WIDTH              12
13 #define PTYPE_NON_TUNNEL_ARRAY_SZ       BIT(PTYPE_NON_TUNNEL_WIDTH)
14 #define PTYPE_TUNNEL_ARRAY_SZ           BIT(PTYPE_TUNNEL_WIDTH)
15 #define PTYPE_ARRAY_SZ                  ((PTYPE_NON_TUNNEL_ARRAY_SZ +\
16                                          PTYPE_TUNNEL_ARRAY_SZ) *\
17                                          sizeof(uint16_t))
18
19 #define NIX_RX_OFFLOAD_NONE            (0)
20 #define NIX_RX_OFFLOAD_RSS_F           BIT(0)
21 #define NIX_RX_OFFLOAD_PTYPE_F         BIT(1)
22 #define NIX_RX_OFFLOAD_CHECKSUM_F      BIT(2)
23 #define NIX_RX_OFFLOAD_VLAN_STRIP_F    BIT(3)
24 #define NIX_RX_OFFLOAD_MARK_UPDATE_F   BIT(4)
25 #define NIX_RX_OFFLOAD_TSTAMP_F        BIT(5)
26
27 /* Flags to control cqe_to_mbuf conversion function.
28  * Defining it from backwards to denote its been
29  * not used as offload flags to pick function
30  */
31 #define NIX_RX_MULTI_SEG_F            BIT(15)
32 #define NIX_TIMESYNC_RX_OFFSET          8
33
34 struct otx2_timesync_info {
35         uint64_t        rx_tstamp;
36         rte_iova_t      tx_tstamp_iova;
37         uint64_t        *tx_tstamp;
38         uint8_t         tx_ready;
39         uint8_t         rx_ready;
40 } __rte_cache_aligned;
41
42 union mbuf_initializer {
43         struct {
44                 uint16_t data_off;
45                 uint16_t refcnt;
46                 uint16_t nb_segs;
47                 uint16_t port;
48         } fields;
49         uint64_t value;
50 };
51
52 static __rte_always_inline void
53 otx2_nix_mbuf_to_tstamp(struct rte_mbuf *mbuf,
54                         struct otx2_timesync_info *tstamp, const uint16_t flag,
55                         uint64_t *tstamp_ptr)
56 {
57         if ((flag & NIX_RX_OFFLOAD_TSTAMP_F) &&
58             (mbuf->data_off == RTE_PKTMBUF_HEADROOM +
59              NIX_TIMESYNC_RX_OFFSET)) {
60
61                 mbuf->pkt_len -= NIX_TIMESYNC_RX_OFFSET;
62
63                 /* Reading the rx timestamp inserted by CGX, viz at
64                  * starting of the packet data.
65                  */
66                 mbuf->timestamp = rte_be_to_cpu_64(*tstamp_ptr);
67                 /* PKT_RX_IEEE1588_TMST flag needs to be set only in case
68                  * PTP packets are received.
69                  */
70                 if (mbuf->packet_type == RTE_PTYPE_L2_ETHER_TIMESYNC) {
71                         tstamp->rx_tstamp = mbuf->timestamp;
72                         tstamp->rx_ready = 1;
73                         mbuf->ol_flags |= PKT_RX_IEEE1588_PTP |
74                                 PKT_RX_IEEE1588_TMST | PKT_RX_TIMESTAMP;
75                 }
76         }
77 }
78
79 static __rte_always_inline uint64_t
80 nix_clear_data_off(uint64_t oldval)
81 {
82         union mbuf_initializer mbuf_init = { .value = oldval };
83
84         mbuf_init.fields.data_off = 0;
85         return mbuf_init.value;
86 }
87
88 static __rte_always_inline struct rte_mbuf *
89 nix_get_mbuf_from_cqe(void *cq, const uint64_t data_off)
90 {
91         rte_iova_t buff;
92
93         /* Skip CQE, NIX_RX_PARSE_S and SG HDR(9 DWORDs) and peek buff addr */
94         buff = *((rte_iova_t *)((uint64_t *)cq + 9));
95         return (struct rte_mbuf *)(buff - data_off);
96 }
97
98
99 static __rte_always_inline uint32_t
100 nix_ptype_get(const void * const lookup_mem, const uint64_t in)
101 {
102         const uint16_t * const ptype = lookup_mem;
103         const uint16_t lh_lg_lf = (in & 0xFFF0000000000000) >> 52;
104         const uint16_t tu_l2 = ptype[(in & 0x000FFFF000000000) >> 36];
105         const uint16_t il4_tu = ptype[PTYPE_NON_TUNNEL_ARRAY_SZ + lh_lg_lf];
106
107         return (il4_tu << PTYPE_NON_TUNNEL_WIDTH) | tu_l2;
108 }
109
110 static __rte_always_inline uint32_t
111 nix_rx_olflags_get(const void * const lookup_mem, const uint64_t in)
112 {
113         const uint32_t * const ol_flags = (const uint32_t *)
114                         ((const uint8_t *)lookup_mem + PTYPE_ARRAY_SZ);
115
116         return ol_flags[(in & 0xfff00000) >> 20];
117 }
118
119 static inline uint64_t
120 nix_update_match_id(const uint16_t match_id, uint64_t ol_flags,
121                     struct rte_mbuf *mbuf)
122 {
123         /* There is no separate bit to check match_id
124          * is valid or not? and no flag to identify it is an
125          * RTE_FLOW_ACTION_TYPE_FLAG vs RTE_FLOW_ACTION_TYPE_MARK
126          * action. The former case addressed through 0 being invalid
127          * value and inc/dec match_id pair when MARK is activated.
128          * The later case addressed through defining
129          * OTX2_FLOW_MARK_DEFAULT as value for
130          * RTE_FLOW_ACTION_TYPE_MARK.
131          * This would translate to not use
132          * OTX2_FLOW_ACTION_FLAG_DEFAULT - 1 and
133          * OTX2_FLOW_ACTION_FLAG_DEFAULT for match_id.
134          * i.e valid mark_id's are from
135          * 0 to OTX2_FLOW_ACTION_FLAG_DEFAULT - 2
136          */
137         if (likely(match_id)) {
138                 ol_flags |= PKT_RX_FDIR;
139                 if (match_id != OTX2_FLOW_ACTION_FLAG_DEFAULT) {
140                         ol_flags |= PKT_RX_FDIR_ID;
141                         mbuf->hash.fdir.hi = match_id - 1;
142                 }
143         }
144
145         return ol_flags;
146 }
147
148 static __rte_always_inline void
149 nix_cqe_xtract_mseg(const struct nix_rx_parse_s *rx,
150                     struct rte_mbuf *mbuf, uint64_t rearm)
151 {
152         const rte_iova_t *iova_list;
153         struct rte_mbuf *head;
154         const rte_iova_t *eol;
155         uint8_t nb_segs;
156         uint64_t sg;
157
158         sg = *(const uint64_t *)(rx + 1);
159         nb_segs = (sg >> 48) & 0x3;
160         mbuf->nb_segs = nb_segs;
161         mbuf->data_len = sg & 0xFFFF;
162         sg = sg >> 16;
163
164         eol = ((const rte_iova_t *)(rx + 1) + ((rx->desc_sizem1 + 1) << 1));
165         /* Skip SG_S and first IOVA*/
166         iova_list = ((const rte_iova_t *)(rx + 1)) + 2;
167         nb_segs--;
168
169         rearm = rearm & ~0xFFFF;
170
171         head = mbuf;
172         while (nb_segs) {
173                 mbuf->next = ((struct rte_mbuf *)*iova_list) - 1;
174                 mbuf = mbuf->next;
175
176                 __mempool_check_cookies(mbuf->pool, (void **)&mbuf, 1, 1);
177
178                 mbuf->data_len = sg & 0xFFFF;
179                 sg = sg >> 16;
180                 *(uint64_t *)(&mbuf->rearm_data) = rearm;
181                 nb_segs--;
182                 iova_list++;
183
184                 if (!nb_segs && (iova_list + 1 < eol)) {
185                         sg = *(const uint64_t *)(iova_list);
186                         nb_segs = (sg >> 48) & 0x3;
187                         head->nb_segs += nb_segs;
188                         iova_list = (const rte_iova_t *)(iova_list + 1);
189                 }
190         }
191 }
192
193 static __rte_always_inline void
194 otx2_nix_cqe_to_mbuf(const struct nix_cqe_hdr_s *cq, const uint32_t tag,
195                      struct rte_mbuf *mbuf, const void *lookup_mem,
196                      const uint64_t val, const uint16_t flag)
197 {
198         const struct nix_rx_parse_s *rx =
199                  (const struct nix_rx_parse_s *)((const uint64_t *)cq + 1);
200         const uint64_t w1 = *(const uint64_t *)rx;
201         const uint16_t len = rx->pkt_lenm1 + 1;
202         uint64_t ol_flags = 0;
203
204         /* Mark mempool obj as "get" as it is alloc'ed by NIX */
205         __mempool_check_cookies(mbuf->pool, (void **)&mbuf, 1, 1);
206
207         if (flag & NIX_RX_OFFLOAD_PTYPE_F)
208                 mbuf->packet_type = nix_ptype_get(lookup_mem, w1);
209         else
210                 mbuf->packet_type = 0;
211
212         if (flag & NIX_RX_OFFLOAD_RSS_F) {
213                 mbuf->hash.rss = tag;
214                 ol_flags |= PKT_RX_RSS_HASH;
215         }
216
217         if (flag & NIX_RX_OFFLOAD_CHECKSUM_F)
218                 ol_flags |= nix_rx_olflags_get(lookup_mem, w1);
219
220         if (flag & NIX_RX_OFFLOAD_VLAN_STRIP_F) {
221                 if (rx->vtag0_gone) {
222                         ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
223                         mbuf->vlan_tci = rx->vtag0_tci;
224                 }
225                 if (rx->vtag1_gone) {
226                         ol_flags |= PKT_RX_QINQ | PKT_RX_QINQ_STRIPPED;
227                         mbuf->vlan_tci_outer = rx->vtag1_tci;
228                 }
229         }
230
231         if (flag & NIX_RX_OFFLOAD_MARK_UPDATE_F)
232                 ol_flags = nix_update_match_id(rx->match_id, ol_flags, mbuf);
233
234         mbuf->ol_flags = ol_flags;
235         *(uint64_t *)(&mbuf->rearm_data) = val;
236         mbuf->pkt_len = len;
237
238         if (flag & NIX_RX_MULTI_SEG_F)
239                 nix_cqe_xtract_mseg(rx, mbuf, val);
240         else
241                 mbuf->data_len = len;
242 }
243
244 #define CKSUM_F NIX_RX_OFFLOAD_CHECKSUM_F
245 #define PTYPE_F NIX_RX_OFFLOAD_PTYPE_F
246 #define RSS_F   NIX_RX_OFFLOAD_RSS_F
247 #define RX_VLAN_F  NIX_RX_OFFLOAD_VLAN_STRIP_F
248 #define MARK_F  NIX_RX_OFFLOAD_MARK_UPDATE_F
249 #define TS_F    NIX_RX_OFFLOAD_TSTAMP_F
250
251 /* [TSMP] [MARK] [VLAN] [CKSUM] [PTYPE] [RSS] */
252 #define NIX_RX_FASTPATH_MODES                                                  \
253 R(no_offload,                   0, 0, 0, 0, 0, 0, NIX_RX_OFFLOAD_NONE)  \
254 R(rss,                          0, 0, 0, 0, 0, 1, RSS_F)                \
255 R(ptype,                        0, 0, 0, 0, 1, 0, PTYPE_F)              \
256 R(ptype_rss,                    0, 0, 0, 0, 1, 1, PTYPE_F | RSS_F)      \
257 R(cksum,                        0, 0, 0, 1, 0, 0, CKSUM_F)              \
258 R(cksum_rss,                    0, 0, 0, 1, 0, 1, CKSUM_F | RSS_F)      \
259 R(cksum_ptype,                  0, 0, 0, 1, 1, 0, CKSUM_F | PTYPE_F)    \
260 R(cksum_ptype_rss,              0, 0, 0, 1, 1, 1, CKSUM_F | PTYPE_F | RSS_F)\
261 R(vlan,                         0, 0, 1, 0, 0, 0, RX_VLAN_F)            \
262 R(vlan_rss,                     0, 0, 1, 0, 0, 1, RX_VLAN_F | RSS_F)    \
263 R(vlan_ptype,                   0, 0, 1, 0, 1, 0, RX_VLAN_F | PTYPE_F)  \
264 R(vlan_ptype_rss,               0, 0, 1, 0, 1, 1, RX_VLAN_F | PTYPE_F | RSS_F)\
265 R(vlan_cksum,                   0, 0, 1, 1, 0, 0, RX_VLAN_F | CKSUM_F)  \
266 R(vlan_cksum_rss,               0, 0, 1, 1, 0, 1, RX_VLAN_F | CKSUM_F | RSS_F)\
267 R(vlan_cksum_ptype,             0, 0, 1, 1, 1, 0,                       \
268                         RX_VLAN_F | CKSUM_F | PTYPE_F)                  \
269 R(vlan_cksum_ptype_rss,         0, 0, 1, 1, 1, 1,                       \
270                         RX_VLAN_F | CKSUM_F | PTYPE_F | RSS_F)          \
271 R(mark,                         0, 1, 0, 0, 0, 0, MARK_F)               \
272 R(mark_rss,                     0, 1, 0, 0, 0, 1, MARK_F | RSS_F)       \
273 R(mark_ptype,                   0, 1, 0, 0, 1, 0, MARK_F | PTYPE_F)     \
274 R(mark_ptype_rss,               0, 1, 0, 0, 1, 1, MARK_F | PTYPE_F | RSS_F)\
275 R(mark_cksum,                   0, 1, 0, 1, 0, 0, MARK_F | CKSUM_F)     \
276 R(mark_cksum_rss,               0, 1, 0, 1, 0, 1, MARK_F | CKSUM_F | RSS_F)\
277 R(mark_cksum_ptype,             0, 1, 0, 1, 1, 0, MARK_F | CKSUM_F | PTYPE_F)\
278 R(mark_cksum_ptype_rss,         0, 1, 0, 1, 1, 1,                       \
279                         MARK_F | CKSUM_F | PTYPE_F | RSS_F)             \
280 R(mark_vlan,                    0, 1, 1, 0, 0, 0, MARK_F | RX_VLAN_F)   \
281 R(mark_vlan_rss,                0, 1, 1, 0, 0, 1, MARK_F | RX_VLAN_F | RSS_F)\
282 R(mark_vlan_ptype,              0, 1, 1, 0, 1, 0,                       \
283                         MARK_F | RX_VLAN_F | PTYPE_F)                   \
284 R(mark_vlan_ptype_rss,          0, 1, 1, 0, 1, 1,                       \
285                         MARK_F | RX_VLAN_F | PTYPE_F | RSS_F)           \
286 R(mark_vlan_cksum,              0, 1, 1, 1, 0, 0,                       \
287                         MARK_F | RX_VLAN_F | CKSUM_F)                   \
288 R(mark_vlan_cksum_rss,          0, 1, 1, 1, 0, 1,                       \
289                         MARK_F | RX_VLAN_F | CKSUM_F | RSS_F)           \
290 R(mark_vlan_cksum_ptype,        0, 1, 1, 1, 1, 0,                       \
291                         MARK_F | RX_VLAN_F | CKSUM_F | PTYPE_F)         \
292 R(mark_vlan_cksum_ptype_rss,    0, 1, 1, 1, 1, 1,                       \
293                         MARK_F | RX_VLAN_F | CKSUM_F | PTYPE_F | RSS_F) \
294 R(ts,                           1, 0, 0, 0, 0, 0, TS_F)                 \
295 R(ts_rss,                       1, 0, 0, 0, 0, 1, TS_F | RSS_F)         \
296 R(ts_ptype,                     1, 0, 0, 0, 1, 0, TS_F | PTYPE_F)       \
297 R(ts_ptype_rss,                 1, 0, 0, 0, 1, 1, TS_F | PTYPE_F | RSS_F)\
298 R(ts_cksum,                     1, 0, 0, 1, 0, 0, TS_F | CKSUM_F)       \
299 R(ts_cksum_rss,                 1, 0, 0, 1, 0, 1, TS_F | CKSUM_F | RSS_F)\
300 R(ts_cksum_ptype,               1, 0, 0, 1, 1, 0, TS_F | CKSUM_F | PTYPE_F)\
301 R(ts_cksum_ptype_rss,           1, 0, 0, 1, 1, 1,                       \
302                         TS_F | CKSUM_F | PTYPE_F | RSS_F)               \
303 R(ts_vlan,                      1, 0, 1, 0, 0, 0, TS_F | RX_VLAN_F)     \
304 R(ts_vlan_rss,                  1, 0, 1, 0, 0, 1, TS_F | RX_VLAN_F | RSS_F)\
305 R(ts_vlan_ptype,                1, 0, 1, 0, 1, 0, TS_F | RX_VLAN_F | PTYPE_F)\
306 R(ts_vlan_ptype_rss,            1, 0, 1, 0, 1, 1,                       \
307                         TS_F | RX_VLAN_F | PTYPE_F | RSS_F)             \
308 R(ts_vlan_cksum,                1, 0, 1, 1, 0, 0,                       \
309                         TS_F | RX_VLAN_F | CKSUM_F)                     \
310 R(ts_vlan_cksum_rss,            1, 0, 1, 1, 0, 1,                       \
311                         MARK_F | RX_VLAN_F | CKSUM_F | RSS_F)           \
312 R(ts_vlan_cksum_ptype,          1, 0, 1, 1, 1, 0,                       \
313                         TS_F | RX_VLAN_F | CKSUM_F | PTYPE_F)           \
314 R(ts_vlan_cksum_ptype_rss,      1, 0, 1, 1, 1, 1,                       \
315                         TS_F | RX_VLAN_F | CKSUM_F | PTYPE_F | RSS_F)   \
316 R(ts_mark,                      1, 1, 0, 0, 0, 0, TS_F | MARK_F)        \
317 R(ts_mark_rss,                  1, 1, 0, 0, 0, 1, TS_F | MARK_F | RSS_F)\
318 R(ts_mark_ptype,                1, 1, 0, 0, 1, 0, TS_F | MARK_F | PTYPE_F)\
319 R(ts_mark_ptype_rss,            1, 1, 0, 0, 1, 1,                       \
320                         TS_F | MARK_F | PTYPE_F | RSS_F)                \
321 R(ts_mark_cksum,                1, 1, 0, 1, 0, 0, TS_F | MARK_F | CKSUM_F)\
322 R(ts_mark_cksum_rss,            1, 1, 0, 1, 0, 1,                       \
323                         TS_F | MARK_F | CKSUM_F | RSS_F)\
324 R(ts_mark_cksum_ptype,          1, 1, 0, 1, 1, 0,                       \
325                         TS_F | MARK_F | CKSUM_F | PTYPE_F)              \
326 R(ts_mark_cksum_ptype_rss,      1, 1, 0, 1, 1, 1,                       \
327                         TS_F | MARK_F | CKSUM_F | PTYPE_F | RSS_F)      \
328 R(ts_mark_vlan,                 1, 1, 1, 0, 0, 0, TS_F | MARK_F | RX_VLAN_F)\
329 R(ts_mark_vlan_rss,             1, 1, 1, 0, 0, 1,                       \
330                         TS_F | MARK_F | RX_VLAN_F | RSS_F)\
331 R(ts_mark_vlan_ptype,           1, 1, 1, 0, 1, 0,                       \
332                         TS_F | MARK_F | RX_VLAN_F | PTYPE_F)            \
333 R(ts_mark_vlan_ptype_rss,       1, 1, 1, 0, 1, 1,                       \
334                         TS_F | MARK_F | RX_VLAN_F | PTYPE_F | RSS_F)    \
335 R(ts_mark_vlan_cksum_ptype,     1, 1, 1, 1, 1, 0,                       \
336                         TS_F | MARK_F | RX_VLAN_F | CKSUM_F | PTYPE_F)  \
337 R(ts_mark_vlan_cksum_ptype_rss, 1, 1, 1, 1, 1, 1,                       \
338                         TS_F | MARK_F | RX_VLAN_F | CKSUM_F | PTYPE_F | RSS_F)
339
340 #endif /* __OTX2_RX_H__ */