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