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