tcp, ulp: remove socket lock assertion on ULP cleanup
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_tc.c
1 /*
2  * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <net/flow_dissector.h>
34 #include <net/sch_generic.h>
35 #include <net/pkt_cls.h>
36 #include <net/tc_act/tc_gact.h>
37 #include <net/tc_act/tc_skbedit.h>
38 #include <linux/mlx5/fs.h>
39 #include <linux/mlx5/device.h>
40 #include <linux/rhashtable.h>
41 #include <net/switchdev.h>
42 #include <net/tc_act/tc_mirred.h>
43 #include <net/tc_act/tc_vlan.h>
44 #include <net/tc_act/tc_tunnel_key.h>
45 #include <net/tc_act/tc_pedit.h>
46 #include <net/tc_act/tc_csum.h>
47 #include <net/vxlan.h>
48 #include <net/arp.h>
49 #include "en.h"
50 #include "en_rep.h"
51 #include "en_tc.h"
52 #include "eswitch.h"
53 #include "lib/vxlan.h"
54 #include "fs_core.h"
55 #include "en/port.h"
56
57 struct mlx5_nic_flow_attr {
58         u32 action;
59         u32 flow_tag;
60         u32 mod_hdr_id;
61         u32 hairpin_tirn;
62         u8 match_level;
63         struct mlx5_flow_table  *hairpin_ft;
64 };
65
66 #define MLX5E_TC_FLOW_BASE (MLX5E_TC_LAST_EXPORTED_BIT + 1)
67
68 enum {
69         MLX5E_TC_FLOW_INGRESS   = MLX5E_TC_INGRESS,
70         MLX5E_TC_FLOW_EGRESS    = MLX5E_TC_EGRESS,
71         MLX5E_TC_FLOW_ESWITCH   = BIT(MLX5E_TC_FLOW_BASE),
72         MLX5E_TC_FLOW_NIC       = BIT(MLX5E_TC_FLOW_BASE + 1),
73         MLX5E_TC_FLOW_OFFLOADED = BIT(MLX5E_TC_FLOW_BASE + 2),
74         MLX5E_TC_FLOW_HAIRPIN   = BIT(MLX5E_TC_FLOW_BASE + 3),
75         MLX5E_TC_FLOW_HAIRPIN_RSS = BIT(MLX5E_TC_FLOW_BASE + 4),
76 };
77
78 #define MLX5E_TC_MAX_SPLITS 1
79
80 struct mlx5e_tc_flow {
81         struct rhash_head       node;
82         struct mlx5e_priv       *priv;
83         u64                     cookie;
84         u8                      flags;
85         struct mlx5_flow_handle *rule[MLX5E_TC_MAX_SPLITS + 1];
86         struct list_head        encap;   /* flows sharing the same encap ID */
87         struct list_head        mod_hdr; /* flows sharing the same mod hdr ID */
88         struct list_head        hairpin; /* flows sharing the same hairpin */
89         union {
90                 struct mlx5_esw_flow_attr esw_attr[0];
91                 struct mlx5_nic_flow_attr nic_attr[0];
92         };
93 };
94
95 struct mlx5e_tc_flow_parse_attr {
96         struct ip_tunnel_info tun_info;
97         struct mlx5_flow_spec spec;
98         int num_mod_hdr_actions;
99         void *mod_hdr_actions;
100         int mirred_ifindex;
101 };
102
103 enum {
104         MLX5_HEADER_TYPE_VXLAN = 0x0,
105         MLX5_HEADER_TYPE_NVGRE = 0x1,
106 };
107
108 #define MLX5E_TC_TABLE_NUM_GROUPS 4
109 #define MLX5E_TC_TABLE_MAX_GROUP_SIZE BIT(16)
110
111 struct mlx5e_hairpin {
112         struct mlx5_hairpin *pair;
113
114         struct mlx5_core_dev *func_mdev;
115         struct mlx5e_priv *func_priv;
116         u32 tdn;
117         u32 tirn;
118
119         int num_channels;
120         struct mlx5e_rqt indir_rqt;
121         u32 indir_tirn[MLX5E_NUM_INDIR_TIRS];
122         struct mlx5e_ttc_table ttc;
123 };
124
125 struct mlx5e_hairpin_entry {
126         /* a node of a hash table which keeps all the  hairpin entries */
127         struct hlist_node hairpin_hlist;
128
129         /* flows sharing the same hairpin */
130         struct list_head flows;
131
132         u16 peer_vhca_id;
133         u8 prio;
134         struct mlx5e_hairpin *hp;
135 };
136
137 struct mod_hdr_key {
138         int num_actions;
139         void *actions;
140 };
141
142 struct mlx5e_mod_hdr_entry {
143         /* a node of a hash table which keeps all the mod_hdr entries */
144         struct hlist_node mod_hdr_hlist;
145
146         /* flows sharing the same mod_hdr entry */
147         struct list_head flows;
148
149         struct mod_hdr_key key;
150
151         u32 mod_hdr_id;
152 };
153
154 #define MLX5_MH_ACT_SZ MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto)
155
156 static inline u32 hash_mod_hdr_info(struct mod_hdr_key *key)
157 {
158         return jhash(key->actions,
159                      key->num_actions * MLX5_MH_ACT_SZ, 0);
160 }
161
162 static inline int cmp_mod_hdr_info(struct mod_hdr_key *a,
163                                    struct mod_hdr_key *b)
164 {
165         if (a->num_actions != b->num_actions)
166                 return 1;
167
168         return memcmp(a->actions, b->actions, a->num_actions * MLX5_MH_ACT_SZ);
169 }
170
171 static int mlx5e_attach_mod_hdr(struct mlx5e_priv *priv,
172                                 struct mlx5e_tc_flow *flow,
173                                 struct mlx5e_tc_flow_parse_attr *parse_attr)
174 {
175         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
176         int num_actions, actions_size, namespace, err;
177         struct mlx5e_mod_hdr_entry *mh;
178         struct mod_hdr_key key;
179         bool found = false;
180         u32 hash_key;
181
182         num_actions  = parse_attr->num_mod_hdr_actions;
183         actions_size = MLX5_MH_ACT_SZ * num_actions;
184
185         key.actions = parse_attr->mod_hdr_actions;
186         key.num_actions = num_actions;
187
188         hash_key = hash_mod_hdr_info(&key);
189
190         if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
191                 namespace = MLX5_FLOW_NAMESPACE_FDB;
192                 hash_for_each_possible(esw->offloads.mod_hdr_tbl, mh,
193                                        mod_hdr_hlist, hash_key) {
194                         if (!cmp_mod_hdr_info(&mh->key, &key)) {
195                                 found = true;
196                                 break;
197                         }
198                 }
199         } else {
200                 namespace = MLX5_FLOW_NAMESPACE_KERNEL;
201                 hash_for_each_possible(priv->fs.tc.mod_hdr_tbl, mh,
202                                        mod_hdr_hlist, hash_key) {
203                         if (!cmp_mod_hdr_info(&mh->key, &key)) {
204                                 found = true;
205                                 break;
206                         }
207                 }
208         }
209
210         if (found)
211                 goto attach_flow;
212
213         mh = kzalloc(sizeof(*mh) + actions_size, GFP_KERNEL);
214         if (!mh)
215                 return -ENOMEM;
216
217         mh->key.actions = (void *)mh + sizeof(*mh);
218         memcpy(mh->key.actions, key.actions, actions_size);
219         mh->key.num_actions = num_actions;
220         INIT_LIST_HEAD(&mh->flows);
221
222         err = mlx5_modify_header_alloc(priv->mdev, namespace,
223                                        mh->key.num_actions,
224                                        mh->key.actions,
225                                        &mh->mod_hdr_id);
226         if (err)
227                 goto out_err;
228
229         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
230                 hash_add(esw->offloads.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
231         else
232                 hash_add(priv->fs.tc.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
233
234 attach_flow:
235         list_add(&flow->mod_hdr, &mh->flows);
236         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
237                 flow->esw_attr->mod_hdr_id = mh->mod_hdr_id;
238         else
239                 flow->nic_attr->mod_hdr_id = mh->mod_hdr_id;
240
241         return 0;
242
243 out_err:
244         kfree(mh);
245         return err;
246 }
247
248 static void mlx5e_detach_mod_hdr(struct mlx5e_priv *priv,
249                                  struct mlx5e_tc_flow *flow)
250 {
251         struct list_head *next = flow->mod_hdr.next;
252
253         list_del(&flow->mod_hdr);
254
255         if (list_empty(next)) {
256                 struct mlx5e_mod_hdr_entry *mh;
257
258                 mh = list_entry(next, struct mlx5e_mod_hdr_entry, flows);
259
260                 mlx5_modify_header_dealloc(priv->mdev, mh->mod_hdr_id);
261                 hash_del(&mh->mod_hdr_hlist);
262                 kfree(mh);
263         }
264 }
265
266 static
267 struct mlx5_core_dev *mlx5e_hairpin_get_mdev(struct net *net, int ifindex)
268 {
269         struct net_device *netdev;
270         struct mlx5e_priv *priv;
271
272         netdev = __dev_get_by_index(net, ifindex);
273         priv = netdev_priv(netdev);
274         return priv->mdev;
275 }
276
277 static int mlx5e_hairpin_create_transport(struct mlx5e_hairpin *hp)
278 {
279         u32 in[MLX5_ST_SZ_DW(create_tir_in)] = {0};
280         void *tirc;
281         int err;
282
283         err = mlx5_core_alloc_transport_domain(hp->func_mdev, &hp->tdn);
284         if (err)
285                 goto alloc_tdn_err;
286
287         tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
288
289         MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
290         MLX5_SET(tirc, tirc, inline_rqn, hp->pair->rqn[0]);
291         MLX5_SET(tirc, tirc, transport_domain, hp->tdn);
292
293         err = mlx5_core_create_tir(hp->func_mdev, in, MLX5_ST_SZ_BYTES(create_tir_in), &hp->tirn);
294         if (err)
295                 goto create_tir_err;
296
297         return 0;
298
299 create_tir_err:
300         mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
301 alloc_tdn_err:
302         return err;
303 }
304
305 static void mlx5e_hairpin_destroy_transport(struct mlx5e_hairpin *hp)
306 {
307         mlx5_core_destroy_tir(hp->func_mdev, hp->tirn);
308         mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
309 }
310
311 static void mlx5e_hairpin_fill_rqt_rqns(struct mlx5e_hairpin *hp, void *rqtc)
312 {
313         u32 indirection_rqt[MLX5E_INDIR_RQT_SIZE], rqn;
314         struct mlx5e_priv *priv = hp->func_priv;
315         int i, ix, sz = MLX5E_INDIR_RQT_SIZE;
316
317         mlx5e_build_default_indir_rqt(indirection_rqt, sz,
318                                       hp->num_channels);
319
320         for (i = 0; i < sz; i++) {
321                 ix = i;
322                 if (priv->channels.params.rss_hfunc == ETH_RSS_HASH_XOR)
323                         ix = mlx5e_bits_invert(i, ilog2(sz));
324                 ix = indirection_rqt[ix];
325                 rqn = hp->pair->rqn[ix];
326                 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
327         }
328 }
329
330 static int mlx5e_hairpin_create_indirect_rqt(struct mlx5e_hairpin *hp)
331 {
332         int inlen, err, sz = MLX5E_INDIR_RQT_SIZE;
333         struct mlx5e_priv *priv = hp->func_priv;
334         struct mlx5_core_dev *mdev = priv->mdev;
335         void *rqtc;
336         u32 *in;
337
338         inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
339         in = kvzalloc(inlen, GFP_KERNEL);
340         if (!in)
341                 return -ENOMEM;
342
343         rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
344
345         MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
346         MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
347
348         mlx5e_hairpin_fill_rqt_rqns(hp, rqtc);
349
350         err = mlx5_core_create_rqt(mdev, in, inlen, &hp->indir_rqt.rqtn);
351         if (!err)
352                 hp->indir_rqt.enabled = true;
353
354         kvfree(in);
355         return err;
356 }
357
358 static int mlx5e_hairpin_create_indirect_tirs(struct mlx5e_hairpin *hp)
359 {
360         struct mlx5e_priv *priv = hp->func_priv;
361         u32 in[MLX5_ST_SZ_DW(create_tir_in)];
362         int tt, i, err;
363         void *tirc;
364
365         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
366                 memset(in, 0, MLX5_ST_SZ_BYTES(create_tir_in));
367                 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
368
369                 MLX5_SET(tirc, tirc, transport_domain, hp->tdn);
370                 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
371                 MLX5_SET(tirc, tirc, indirect_table, hp->indir_rqt.rqtn);
372                 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
373
374                 err = mlx5_core_create_tir(hp->func_mdev, in,
375                                            MLX5_ST_SZ_BYTES(create_tir_in), &hp->indir_tirn[tt]);
376                 if (err) {
377                         mlx5_core_warn(hp->func_mdev, "create indirect tirs failed, %d\n", err);
378                         goto err_destroy_tirs;
379                 }
380         }
381         return 0;
382
383 err_destroy_tirs:
384         for (i = 0; i < tt; i++)
385                 mlx5_core_destroy_tir(hp->func_mdev, hp->indir_tirn[i]);
386         return err;
387 }
388
389 static void mlx5e_hairpin_destroy_indirect_tirs(struct mlx5e_hairpin *hp)
390 {
391         int tt;
392
393         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
394                 mlx5_core_destroy_tir(hp->func_mdev, hp->indir_tirn[tt]);
395 }
396
397 static void mlx5e_hairpin_set_ttc_params(struct mlx5e_hairpin *hp,
398                                          struct ttc_params *ttc_params)
399 {
400         struct mlx5_flow_table_attr *ft_attr = &ttc_params->ft_attr;
401         int tt;
402
403         memset(ttc_params, 0, sizeof(*ttc_params));
404
405         ttc_params->any_tt_tirn = hp->tirn;
406
407         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
408                 ttc_params->indir_tirn[tt] = hp->indir_tirn[tt];
409
410         ft_attr->max_fte = MLX5E_NUM_TT;
411         ft_attr->level = MLX5E_TC_TTC_FT_LEVEL;
412         ft_attr->prio = MLX5E_TC_PRIO;
413 }
414
415 static int mlx5e_hairpin_rss_init(struct mlx5e_hairpin *hp)
416 {
417         struct mlx5e_priv *priv = hp->func_priv;
418         struct ttc_params ttc_params;
419         int err;
420
421         err = mlx5e_hairpin_create_indirect_rqt(hp);
422         if (err)
423                 return err;
424
425         err = mlx5e_hairpin_create_indirect_tirs(hp);
426         if (err)
427                 goto err_create_indirect_tirs;
428
429         mlx5e_hairpin_set_ttc_params(hp, &ttc_params);
430         err = mlx5e_create_ttc_table(priv, &ttc_params, &hp->ttc);
431         if (err)
432                 goto err_create_ttc_table;
433
434         netdev_dbg(priv->netdev, "add hairpin: using %d channels rss ttc table id %x\n",
435                    hp->num_channels, hp->ttc.ft.t->id);
436
437         return 0;
438
439 err_create_ttc_table:
440         mlx5e_hairpin_destroy_indirect_tirs(hp);
441 err_create_indirect_tirs:
442         mlx5e_destroy_rqt(priv, &hp->indir_rqt);
443
444         return err;
445 }
446
447 static void mlx5e_hairpin_rss_cleanup(struct mlx5e_hairpin *hp)
448 {
449         struct mlx5e_priv *priv = hp->func_priv;
450
451         mlx5e_destroy_ttc_table(priv, &hp->ttc);
452         mlx5e_hairpin_destroy_indirect_tirs(hp);
453         mlx5e_destroy_rqt(priv, &hp->indir_rqt);
454 }
455
456 static struct mlx5e_hairpin *
457 mlx5e_hairpin_create(struct mlx5e_priv *priv, struct mlx5_hairpin_params *params,
458                      int peer_ifindex)
459 {
460         struct mlx5_core_dev *func_mdev, *peer_mdev;
461         struct mlx5e_hairpin *hp;
462         struct mlx5_hairpin *pair;
463         int err;
464
465         hp = kzalloc(sizeof(*hp), GFP_KERNEL);
466         if (!hp)
467                 return ERR_PTR(-ENOMEM);
468
469         func_mdev = priv->mdev;
470         peer_mdev = mlx5e_hairpin_get_mdev(dev_net(priv->netdev), peer_ifindex);
471
472         pair = mlx5_core_hairpin_create(func_mdev, peer_mdev, params);
473         if (IS_ERR(pair)) {
474                 err = PTR_ERR(pair);
475                 goto create_pair_err;
476         }
477         hp->pair = pair;
478         hp->func_mdev = func_mdev;
479         hp->func_priv = priv;
480         hp->num_channels = params->num_channels;
481
482         err = mlx5e_hairpin_create_transport(hp);
483         if (err)
484                 goto create_transport_err;
485
486         if (hp->num_channels > 1) {
487                 err = mlx5e_hairpin_rss_init(hp);
488                 if (err)
489                         goto rss_init_err;
490         }
491
492         return hp;
493
494 rss_init_err:
495         mlx5e_hairpin_destroy_transport(hp);
496 create_transport_err:
497         mlx5_core_hairpin_destroy(hp->pair);
498 create_pair_err:
499         kfree(hp);
500         return ERR_PTR(err);
501 }
502
503 static void mlx5e_hairpin_destroy(struct mlx5e_hairpin *hp)
504 {
505         if (hp->num_channels > 1)
506                 mlx5e_hairpin_rss_cleanup(hp);
507         mlx5e_hairpin_destroy_transport(hp);
508         mlx5_core_hairpin_destroy(hp->pair);
509         kvfree(hp);
510 }
511
512 static inline u32 hash_hairpin_info(u16 peer_vhca_id, u8 prio)
513 {
514         return (peer_vhca_id << 16 | prio);
515 }
516
517 static struct mlx5e_hairpin_entry *mlx5e_hairpin_get(struct mlx5e_priv *priv,
518                                                      u16 peer_vhca_id, u8 prio)
519 {
520         struct mlx5e_hairpin_entry *hpe;
521         u32 hash_key = hash_hairpin_info(peer_vhca_id, prio);
522
523         hash_for_each_possible(priv->fs.tc.hairpin_tbl, hpe,
524                                hairpin_hlist, hash_key) {
525                 if (hpe->peer_vhca_id == peer_vhca_id && hpe->prio == prio)
526                         return hpe;
527         }
528
529         return NULL;
530 }
531
532 #define UNKNOWN_MATCH_PRIO 8
533
534 static int mlx5e_hairpin_get_prio(struct mlx5e_priv *priv,
535                                   struct mlx5_flow_spec *spec, u8 *match_prio,
536                                   struct netlink_ext_ack *extack)
537 {
538         void *headers_c, *headers_v;
539         u8 prio_val, prio_mask = 0;
540         bool vlan_present;
541
542 #ifdef CONFIG_MLX5_CORE_EN_DCB
543         if (priv->dcbx_dp.trust_state != MLX5_QPTS_TRUST_PCP) {
544                 NL_SET_ERR_MSG_MOD(extack,
545                                    "only PCP trust state supported for hairpin");
546                 return -EOPNOTSUPP;
547         }
548 #endif
549         headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, outer_headers);
550         headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
551
552         vlan_present = MLX5_GET(fte_match_set_lyr_2_4, headers_v, cvlan_tag);
553         if (vlan_present) {
554                 prio_mask = MLX5_GET(fte_match_set_lyr_2_4, headers_c, first_prio);
555                 prio_val = MLX5_GET(fte_match_set_lyr_2_4, headers_v, first_prio);
556         }
557
558         if (!vlan_present || !prio_mask) {
559                 prio_val = UNKNOWN_MATCH_PRIO;
560         } else if (prio_mask != 0x7) {
561                 NL_SET_ERR_MSG_MOD(extack,
562                                    "masked priority match not supported for hairpin");
563                 return -EOPNOTSUPP;
564         }
565
566         *match_prio = prio_val;
567         return 0;
568 }
569
570 static int mlx5e_hairpin_flow_add(struct mlx5e_priv *priv,
571                                   struct mlx5e_tc_flow *flow,
572                                   struct mlx5e_tc_flow_parse_attr *parse_attr,
573                                   struct netlink_ext_ack *extack)
574 {
575         int peer_ifindex = parse_attr->mirred_ifindex;
576         struct mlx5_hairpin_params params;
577         struct mlx5_core_dev *peer_mdev;
578         struct mlx5e_hairpin_entry *hpe;
579         struct mlx5e_hairpin *hp;
580         u64 link_speed64;
581         u32 link_speed;
582         u8 match_prio;
583         u16 peer_id;
584         int err;
585
586         peer_mdev = mlx5e_hairpin_get_mdev(dev_net(priv->netdev), peer_ifindex);
587         if (!MLX5_CAP_GEN(priv->mdev, hairpin) || !MLX5_CAP_GEN(peer_mdev, hairpin)) {
588                 NL_SET_ERR_MSG_MOD(extack, "hairpin is not supported");
589                 return -EOPNOTSUPP;
590         }
591
592         peer_id = MLX5_CAP_GEN(peer_mdev, vhca_id);
593         err = mlx5e_hairpin_get_prio(priv, &parse_attr->spec, &match_prio,
594                                      extack);
595         if (err)
596                 return err;
597         hpe = mlx5e_hairpin_get(priv, peer_id, match_prio);
598         if (hpe)
599                 goto attach_flow;
600
601         hpe = kzalloc(sizeof(*hpe), GFP_KERNEL);
602         if (!hpe)
603                 return -ENOMEM;
604
605         INIT_LIST_HEAD(&hpe->flows);
606         hpe->peer_vhca_id = peer_id;
607         hpe->prio = match_prio;
608
609         params.log_data_size = 15;
610         params.log_data_size = min_t(u8, params.log_data_size,
611                                      MLX5_CAP_GEN(priv->mdev, log_max_hairpin_wq_data_sz));
612         params.log_data_size = max_t(u8, params.log_data_size,
613                                      MLX5_CAP_GEN(priv->mdev, log_min_hairpin_wq_data_sz));
614
615         params.log_num_packets = params.log_data_size -
616                                  MLX5_MPWRQ_MIN_LOG_STRIDE_SZ(priv->mdev);
617         params.log_num_packets = min_t(u8, params.log_num_packets,
618                                        MLX5_CAP_GEN(priv->mdev, log_max_hairpin_num_packets));
619
620         params.q_counter = priv->q_counter;
621         /* set hairpin pair per each 50Gbs share of the link */
622         mlx5e_port_max_linkspeed(priv->mdev, &link_speed);
623         link_speed = max_t(u32, link_speed, 50000);
624         link_speed64 = link_speed;
625         do_div(link_speed64, 50000);
626         params.num_channels = link_speed64;
627
628         hp = mlx5e_hairpin_create(priv, &params, peer_ifindex);
629         if (IS_ERR(hp)) {
630                 err = PTR_ERR(hp);
631                 goto create_hairpin_err;
632         }
633
634         netdev_dbg(priv->netdev, "add hairpin: tirn %x rqn %x peer %s sqn %x prio %d (log) data %d packets %d\n",
635                    hp->tirn, hp->pair->rqn[0], hp->pair->peer_mdev->priv.name,
636                    hp->pair->sqn[0], match_prio, params.log_data_size, params.log_num_packets);
637
638         hpe->hp = hp;
639         hash_add(priv->fs.tc.hairpin_tbl, &hpe->hairpin_hlist,
640                  hash_hairpin_info(peer_id, match_prio));
641
642 attach_flow:
643         if (hpe->hp->num_channels > 1) {
644                 flow->flags |= MLX5E_TC_FLOW_HAIRPIN_RSS;
645                 flow->nic_attr->hairpin_ft = hpe->hp->ttc.ft.t;
646         } else {
647                 flow->nic_attr->hairpin_tirn = hpe->hp->tirn;
648         }
649         list_add(&flow->hairpin, &hpe->flows);
650
651         return 0;
652
653 create_hairpin_err:
654         kfree(hpe);
655         return err;
656 }
657
658 static void mlx5e_hairpin_flow_del(struct mlx5e_priv *priv,
659                                    struct mlx5e_tc_flow *flow)
660 {
661         struct list_head *next = flow->hairpin.next;
662
663         list_del(&flow->hairpin);
664
665         /* no more hairpin flows for us, release the hairpin pair */
666         if (list_empty(next)) {
667                 struct mlx5e_hairpin_entry *hpe;
668
669                 hpe = list_entry(next, struct mlx5e_hairpin_entry, flows);
670
671                 netdev_dbg(priv->netdev, "del hairpin: peer %s\n",
672                            hpe->hp->pair->peer_mdev->priv.name);
673
674                 mlx5e_hairpin_destroy(hpe->hp);
675                 hash_del(&hpe->hairpin_hlist);
676                 kfree(hpe);
677         }
678 }
679
680 static struct mlx5_flow_handle *
681 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
682                       struct mlx5e_tc_flow_parse_attr *parse_attr,
683                       struct mlx5e_tc_flow *flow,
684                       struct netlink_ext_ack *extack)
685 {
686         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
687         struct mlx5_core_dev *dev = priv->mdev;
688         struct mlx5_flow_destination dest[2] = {};
689         struct mlx5_flow_act flow_act = {
690                 .action = attr->action,
691                 .has_flow_tag = true,
692                 .flow_tag = attr->flow_tag,
693                 .encap_id = 0,
694         };
695         struct mlx5_fc *counter = NULL;
696         struct mlx5_flow_handle *rule;
697         bool table_created = false;
698         int err, dest_ix = 0;
699
700         if (flow->flags & MLX5E_TC_FLOW_HAIRPIN) {
701                 err = mlx5e_hairpin_flow_add(priv, flow, parse_attr, extack);
702                 if (err) {
703                         rule = ERR_PTR(err);
704                         goto err_add_hairpin_flow;
705                 }
706                 if (flow->flags & MLX5E_TC_FLOW_HAIRPIN_RSS) {
707                         dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
708                         dest[dest_ix].ft = attr->hairpin_ft;
709                 } else {
710                         dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_TIR;
711                         dest[dest_ix].tir_num = attr->hairpin_tirn;
712                 }
713                 dest_ix++;
714         } else if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
715                 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
716                 dest[dest_ix].ft = priv->fs.vlan.ft.t;
717                 dest_ix++;
718         }
719
720         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
721                 counter = mlx5_fc_create(dev, true);
722                 if (IS_ERR(counter)) {
723                         rule = ERR_CAST(counter);
724                         goto err_fc_create;
725                 }
726                 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
727                 dest[dest_ix].counter = counter;
728                 dest_ix++;
729         }
730
731         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
732                 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
733                 flow_act.modify_id = attr->mod_hdr_id;
734                 kfree(parse_attr->mod_hdr_actions);
735                 if (err) {
736                         rule = ERR_PTR(err);
737                         goto err_create_mod_hdr_id;
738                 }
739         }
740
741         if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
742                 int tc_grp_size, tc_tbl_size;
743                 u32 max_flow_counter;
744
745                 max_flow_counter = (MLX5_CAP_GEN(dev, max_flow_counter_31_16) << 16) |
746                                     MLX5_CAP_GEN(dev, max_flow_counter_15_0);
747
748                 tc_grp_size = min_t(int, max_flow_counter, MLX5E_TC_TABLE_MAX_GROUP_SIZE);
749
750                 tc_tbl_size = min_t(int, tc_grp_size * MLX5E_TC_TABLE_NUM_GROUPS,
751                                     BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev, log_max_ft_size)));
752
753                 priv->fs.tc.t =
754                         mlx5_create_auto_grouped_flow_table(priv->fs.ns,
755                                                             MLX5E_TC_PRIO,
756                                                             tc_tbl_size,
757                                                             MLX5E_TC_TABLE_NUM_GROUPS,
758                                                             MLX5E_TC_FT_LEVEL, 0);
759                 if (IS_ERR(priv->fs.tc.t)) {
760                         NL_SET_ERR_MSG_MOD(extack,
761                                            "Failed to create tc offload table\n");
762                         netdev_err(priv->netdev,
763                                    "Failed to create tc offload table\n");
764                         rule = ERR_CAST(priv->fs.tc.t);
765                         goto err_create_ft;
766                 }
767
768                 table_created = true;
769         }
770
771         if (attr->match_level != MLX5_MATCH_NONE)
772                 parse_attr->spec.match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
773
774         rule = mlx5_add_flow_rules(priv->fs.tc.t, &parse_attr->spec,
775                                    &flow_act, dest, dest_ix);
776
777         if (IS_ERR(rule))
778                 goto err_add_rule;
779
780         return rule;
781
782 err_add_rule:
783         if (table_created) {
784                 mlx5_destroy_flow_table(priv->fs.tc.t);
785                 priv->fs.tc.t = NULL;
786         }
787 err_create_ft:
788         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
789                 mlx5e_detach_mod_hdr(priv, flow);
790 err_create_mod_hdr_id:
791         mlx5_fc_destroy(dev, counter);
792 err_fc_create:
793         if (flow->flags & MLX5E_TC_FLOW_HAIRPIN)
794                 mlx5e_hairpin_flow_del(priv, flow);
795 err_add_hairpin_flow:
796         return rule;
797 }
798
799 static void mlx5e_tc_del_nic_flow(struct mlx5e_priv *priv,
800                                   struct mlx5e_tc_flow *flow)
801 {
802         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
803         struct mlx5_fc *counter = NULL;
804
805         counter = mlx5_flow_rule_counter(flow->rule[0]);
806         mlx5_del_flow_rules(flow->rule[0]);
807         mlx5_fc_destroy(priv->mdev, counter);
808
809         if (!mlx5e_tc_num_filters(priv) && priv->fs.tc.t) {
810                 mlx5_destroy_flow_table(priv->fs.tc.t);
811                 priv->fs.tc.t = NULL;
812         }
813
814         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
815                 mlx5e_detach_mod_hdr(priv, flow);
816
817         if (flow->flags & MLX5E_TC_FLOW_HAIRPIN)
818                 mlx5e_hairpin_flow_del(priv, flow);
819 }
820
821 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
822                                struct mlx5e_tc_flow *flow);
823
824 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
825                               struct ip_tunnel_info *tun_info,
826                               struct net_device *mirred_dev,
827                               struct net_device **encap_dev,
828                               struct mlx5e_tc_flow *flow,
829                               struct netlink_ext_ack *extack);
830
831 static struct mlx5_flow_handle *
832 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
833                       struct mlx5e_tc_flow_parse_attr *parse_attr,
834                       struct mlx5e_tc_flow *flow,
835                       struct netlink_ext_ack *extack)
836 {
837         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
838         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
839         struct net_device *out_dev, *encap_dev = NULL;
840         struct mlx5_flow_handle *rule = NULL;
841         struct mlx5e_rep_priv *rpriv;
842         struct mlx5e_priv *out_priv;
843         int err;
844
845         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
846                 out_dev = __dev_get_by_index(dev_net(priv->netdev),
847                                              attr->parse_attr->mirred_ifindex);
848                 err = mlx5e_attach_encap(priv, &parse_attr->tun_info,
849                                          out_dev, &encap_dev, flow, extack);
850                 if (err) {
851                         rule = ERR_PTR(err);
852                         if (err != -EAGAIN)
853                                 goto err_attach_encap;
854                 }
855                 out_priv = netdev_priv(encap_dev);
856                 rpriv = out_priv->ppriv;
857                 attr->out_rep[attr->out_count] = rpriv->rep;
858                 attr->out_mdev[attr->out_count++] = out_priv->mdev;
859         }
860
861         err = mlx5_eswitch_add_vlan_action(esw, attr);
862         if (err) {
863                 rule = ERR_PTR(err);
864                 goto err_add_vlan;
865         }
866
867         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
868                 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
869                 kfree(parse_attr->mod_hdr_actions);
870                 if (err) {
871                         rule = ERR_PTR(err);
872                         goto err_mod_hdr;
873                 }
874         }
875
876         /* we get here if (1) there's no error (rule being null) or when
877          * (2) there's an encap action and we're on -EAGAIN (no valid neigh)
878          */
879         if (rule != ERR_PTR(-EAGAIN)) {
880                 rule = mlx5_eswitch_add_offloaded_rule(esw, &parse_attr->spec, attr);
881                 if (IS_ERR(rule))
882                         goto err_add_rule;
883
884                 if (attr->mirror_count) {
885                         flow->rule[1] = mlx5_eswitch_add_fwd_rule(esw, &parse_attr->spec, attr);
886                         if (IS_ERR(flow->rule[1]))
887                                 goto err_fwd_rule;
888                 }
889         }
890         return rule;
891
892 err_fwd_rule:
893         mlx5_eswitch_del_offloaded_rule(esw, rule, attr);
894         rule = flow->rule[1];
895 err_add_rule:
896         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
897                 mlx5e_detach_mod_hdr(priv, flow);
898 err_mod_hdr:
899         mlx5_eswitch_del_vlan_action(esw, attr);
900 err_add_vlan:
901         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
902                 mlx5e_detach_encap(priv, flow);
903 err_attach_encap:
904         return rule;
905 }
906
907 static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
908                                   struct mlx5e_tc_flow *flow)
909 {
910         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
911         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
912
913         if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
914                 flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
915                 if (attr->mirror_count)
916                         mlx5_eswitch_del_offloaded_rule(esw, flow->rule[1], attr);
917                 mlx5_eswitch_del_offloaded_rule(esw, flow->rule[0], attr);
918         }
919
920         mlx5_eswitch_del_vlan_action(esw, attr);
921
922         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
923                 mlx5e_detach_encap(priv, flow);
924                 kvfree(attr->parse_attr);
925         }
926
927         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
928                 mlx5e_detach_mod_hdr(priv, flow);
929 }
930
931 void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
932                               struct mlx5e_encap_entry *e)
933 {
934         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
935         struct mlx5_esw_flow_attr *esw_attr;
936         struct mlx5e_tc_flow *flow;
937         int err;
938
939         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
940                                e->encap_size, e->encap_header,
941                                &e->encap_id);
942         if (err) {
943                 mlx5_core_warn(priv->mdev, "Failed to offload cached encapsulation header, %d\n",
944                                err);
945                 return;
946         }
947         e->flags |= MLX5_ENCAP_ENTRY_VALID;
948         mlx5e_rep_queue_neigh_stats_work(priv);
949
950         list_for_each_entry(flow, &e->flows, encap) {
951                 esw_attr = flow->esw_attr;
952                 esw_attr->encap_id = e->encap_id;
953                 flow->rule[0] = mlx5_eswitch_add_offloaded_rule(esw, &esw_attr->parse_attr->spec, esw_attr);
954                 if (IS_ERR(flow->rule[0])) {
955                         err = PTR_ERR(flow->rule[0]);
956                         mlx5_core_warn(priv->mdev, "Failed to update cached encapsulation flow, %d\n",
957                                        err);
958                         continue;
959                 }
960
961                 if (esw_attr->mirror_count) {
962                         flow->rule[1] = mlx5_eswitch_add_fwd_rule(esw, &esw_attr->parse_attr->spec, esw_attr);
963                         if (IS_ERR(flow->rule[1])) {
964                                 mlx5_eswitch_del_offloaded_rule(esw, flow->rule[0], esw_attr);
965                                 err = PTR_ERR(flow->rule[1]);
966                                 mlx5_core_warn(priv->mdev, "Failed to update cached mirror flow, %d\n",
967                                                err);
968                                 continue;
969                         }
970                 }
971
972                 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
973         }
974 }
975
976 void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
977                               struct mlx5e_encap_entry *e)
978 {
979         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
980         struct mlx5e_tc_flow *flow;
981
982         list_for_each_entry(flow, &e->flows, encap) {
983                 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
984                         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
985
986                         flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
987                         if (attr->mirror_count)
988                                 mlx5_eswitch_del_offloaded_rule(esw, flow->rule[1], attr);
989                         mlx5_eswitch_del_offloaded_rule(esw, flow->rule[0], attr);
990                 }
991         }
992
993         if (e->flags & MLX5_ENCAP_ENTRY_VALID) {
994                 e->flags &= ~MLX5_ENCAP_ENTRY_VALID;
995                 mlx5_encap_dealloc(priv->mdev, e->encap_id);
996         }
997 }
998
999 void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
1000 {
1001         struct mlx5e_neigh *m_neigh = &nhe->m_neigh;
1002         u64 bytes, packets, lastuse = 0;
1003         struct mlx5e_tc_flow *flow;
1004         struct mlx5e_encap_entry *e;
1005         struct mlx5_fc *counter;
1006         struct neigh_table *tbl;
1007         bool neigh_used = false;
1008         struct neighbour *n;
1009
1010         if (m_neigh->family == AF_INET)
1011                 tbl = &arp_tbl;
1012 #if IS_ENABLED(CONFIG_IPV6)
1013         else if (m_neigh->family == AF_INET6)
1014                 tbl = &nd_tbl;
1015 #endif
1016         else
1017                 return;
1018
1019         list_for_each_entry(e, &nhe->encap_list, encap_list) {
1020                 if (!(e->flags & MLX5_ENCAP_ENTRY_VALID))
1021                         continue;
1022                 list_for_each_entry(flow, &e->flows, encap) {
1023                         if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
1024                                 counter = mlx5_flow_rule_counter(flow->rule[0]);
1025                                 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
1026                                 if (time_after((unsigned long)lastuse, nhe->reported_lastuse)) {
1027                                         neigh_used = true;
1028                                         break;
1029                                 }
1030                         }
1031                 }
1032                 if (neigh_used)
1033                         break;
1034         }
1035
1036         if (neigh_used) {
1037                 nhe->reported_lastuse = jiffies;
1038
1039                 /* find the relevant neigh according to the cached device and
1040                  * dst ip pair
1041                  */
1042                 n = neigh_lookup(tbl, &m_neigh->dst_ip, m_neigh->dev);
1043                 if (!n)
1044                         return;
1045
1046                 neigh_event_send(n, NULL);
1047                 neigh_release(n);
1048         }
1049 }
1050
1051 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
1052                                struct mlx5e_tc_flow *flow)
1053 {
1054         struct list_head *next = flow->encap.next;
1055
1056         list_del(&flow->encap);
1057         if (list_empty(next)) {
1058                 struct mlx5e_encap_entry *e;
1059
1060                 e = list_entry(next, struct mlx5e_encap_entry, flows);
1061                 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1062
1063                 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
1064                         mlx5_encap_dealloc(priv->mdev, e->encap_id);
1065
1066                 hash_del_rcu(&e->encap_hlist);
1067                 kfree(e->encap_header);
1068                 kfree(e);
1069         }
1070 }
1071
1072 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
1073                               struct mlx5e_tc_flow *flow)
1074 {
1075         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
1076                 mlx5e_tc_del_fdb_flow(priv, flow);
1077         else
1078                 mlx5e_tc_del_nic_flow(priv, flow);
1079 }
1080
1081 static void parse_vxlan_attr(struct mlx5_flow_spec *spec,
1082                              struct tc_cls_flower_offload *f)
1083 {
1084         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1085                                        outer_headers);
1086         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1087                                        outer_headers);
1088         void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1089                                     misc_parameters);
1090         void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1091                                     misc_parameters);
1092
1093         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
1094         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
1095
1096         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
1097                 struct flow_dissector_key_keyid *key =
1098                         skb_flow_dissector_target(f->dissector,
1099                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
1100                                                   f->key);
1101                 struct flow_dissector_key_keyid *mask =
1102                         skb_flow_dissector_target(f->dissector,
1103                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
1104                                                   f->mask);
1105                 MLX5_SET(fte_match_set_misc, misc_c, vxlan_vni,
1106                          be32_to_cpu(mask->keyid));
1107                 MLX5_SET(fte_match_set_misc, misc_v, vxlan_vni,
1108                          be32_to_cpu(key->keyid));
1109         }
1110 }
1111
1112 static int parse_tunnel_attr(struct mlx5e_priv *priv,
1113                              struct mlx5_flow_spec *spec,
1114                              struct tc_cls_flower_offload *f)
1115 {
1116         struct netlink_ext_ack *extack = f->common.extack;
1117         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1118                                        outer_headers);
1119         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1120                                        outer_headers);
1121
1122         struct flow_dissector_key_control *enc_control =
1123                 skb_flow_dissector_target(f->dissector,
1124                                           FLOW_DISSECTOR_KEY_ENC_CONTROL,
1125                                           f->key);
1126
1127         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
1128                 struct flow_dissector_key_ports *key =
1129                         skb_flow_dissector_target(f->dissector,
1130                                                   FLOW_DISSECTOR_KEY_ENC_PORTS,
1131                                                   f->key);
1132                 struct flow_dissector_key_ports *mask =
1133                         skb_flow_dissector_target(f->dissector,
1134                                                   FLOW_DISSECTOR_KEY_ENC_PORTS,
1135                                                   f->mask);
1136
1137                 /* Full udp dst port must be given */
1138                 if (memchr_inv(&mask->dst, 0xff, sizeof(mask->dst)))
1139                         goto vxlan_match_offload_err;
1140
1141                 if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, be16_to_cpu(key->dst)) &&
1142                     MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap))
1143                         parse_vxlan_attr(spec, f);
1144                 else {
1145                         NL_SET_ERR_MSG_MOD(extack,
1146                                            "port isn't an offloaded vxlan udp dport");
1147                         netdev_warn(priv->netdev,
1148                                     "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->dst));
1149                         return -EOPNOTSUPP;
1150                 }
1151
1152                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1153                          udp_dport, ntohs(mask->dst));
1154                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1155                          udp_dport, ntohs(key->dst));
1156
1157                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1158                          udp_sport, ntohs(mask->src));
1159                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1160                          udp_sport, ntohs(key->src));
1161         } else { /* udp dst port must be given */
1162 vxlan_match_offload_err:
1163                 NL_SET_ERR_MSG_MOD(extack,
1164                                    "IP tunnel decap offload supported only for vxlan, must set UDP dport");
1165                 netdev_warn(priv->netdev,
1166                             "IP tunnel decap offload supported only for vxlan, must set UDP dport\n");
1167                 return -EOPNOTSUPP;
1168         }
1169
1170         if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1171                 struct flow_dissector_key_ipv4_addrs *key =
1172                         skb_flow_dissector_target(f->dissector,
1173                                                   FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
1174                                                   f->key);
1175                 struct flow_dissector_key_ipv4_addrs *mask =
1176                         skb_flow_dissector_target(f->dissector,
1177                                                   FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
1178                                                   f->mask);
1179                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1180                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
1181                          ntohl(mask->src));
1182                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1183                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
1184                          ntohl(key->src));
1185
1186                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1187                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
1188                          ntohl(mask->dst));
1189                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1190                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
1191                          ntohl(key->dst));
1192
1193                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
1194                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IP);
1195         } else if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1196                 struct flow_dissector_key_ipv6_addrs *key =
1197                         skb_flow_dissector_target(f->dissector,
1198                                                   FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
1199                                                   f->key);
1200                 struct flow_dissector_key_ipv6_addrs *mask =
1201                         skb_flow_dissector_target(f->dissector,
1202                                                   FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
1203                                                   f->mask);
1204
1205                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1206                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
1207                        &mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1208                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1209                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
1210                        &key->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1211
1212                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1213                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1214                        &mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1215                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1216                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1217                        &key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1218
1219                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
1220                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IPV6);
1221         }
1222
1223         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_IP)) {
1224                 struct flow_dissector_key_ip *key =
1225                         skb_flow_dissector_target(f->dissector,
1226                                                   FLOW_DISSECTOR_KEY_ENC_IP,
1227                                                   f->key);
1228                 struct flow_dissector_key_ip *mask =
1229                         skb_flow_dissector_target(f->dissector,
1230                                                   FLOW_DISSECTOR_KEY_ENC_IP,
1231                                                   f->mask);
1232
1233                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn, mask->tos & 0x3);
1234                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, key->tos & 0x3);
1235
1236                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp, mask->tos >> 2);
1237                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, key->tos  >> 2);
1238
1239                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit, mask->ttl);
1240                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit, key->ttl);
1241
1242                 if (mask->ttl &&
1243                     !MLX5_CAP_ESW_FLOWTABLE_FDB
1244                         (priv->mdev,
1245                          ft_field_support.outer_ipv4_ttl)) {
1246                         NL_SET_ERR_MSG_MOD(extack,
1247                                            "Matching on TTL is not supported");
1248                         return -EOPNOTSUPP;
1249                 }
1250
1251         }
1252
1253         /* Enforce DMAC when offloading incoming tunneled flows.
1254          * Flow counters require a match on the DMAC.
1255          */
1256         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_47_16);
1257         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_15_0);
1258         ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1259                                      dmac_47_16), priv->netdev->dev_addr);
1260
1261         /* let software handle IP fragments */
1262         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
1263         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
1264
1265         return 0;
1266 }
1267
1268 static int __parse_cls_flower(struct mlx5e_priv *priv,
1269                               struct mlx5_flow_spec *spec,
1270                               struct tc_cls_flower_offload *f,
1271                               u8 *match_level)
1272 {
1273         struct netlink_ext_ack *extack = f->common.extack;
1274         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1275                                        outer_headers);
1276         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1277                                        outer_headers);
1278         void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1279                                     misc_parameters);
1280         void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1281                                     misc_parameters);
1282         u16 addr_type = 0;
1283         u8 ip_proto = 0;
1284
1285         *match_level = MLX5_MATCH_NONE;
1286
1287         if (f->dissector->used_keys &
1288             ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1289               BIT(FLOW_DISSECTOR_KEY_BASIC) |
1290               BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1291               BIT(FLOW_DISSECTOR_KEY_VLAN) |
1292               BIT(FLOW_DISSECTOR_KEY_CVLAN) |
1293               BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1294               BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1295               BIT(FLOW_DISSECTOR_KEY_PORTS) |
1296               BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1297               BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1298               BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1299               BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1300               BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1301               BIT(FLOW_DISSECTOR_KEY_TCP) |
1302               BIT(FLOW_DISSECTOR_KEY_IP)  |
1303               BIT(FLOW_DISSECTOR_KEY_ENC_IP))) {
1304                 NL_SET_ERR_MSG_MOD(extack, "Unsupported key");
1305                 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
1306                             f->dissector->used_keys);
1307                 return -EOPNOTSUPP;
1308         }
1309
1310         if ((dissector_uses_key(f->dissector,
1311                                 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) ||
1312              dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID) ||
1313              dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) &&
1314             dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
1315                 struct flow_dissector_key_control *key =
1316                         skb_flow_dissector_target(f->dissector,
1317                                                   FLOW_DISSECTOR_KEY_ENC_CONTROL,
1318                                                   f->key);
1319                 switch (key->addr_type) {
1320                 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1321                 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1322                         if (parse_tunnel_attr(priv, spec, f))
1323                                 return -EOPNOTSUPP;
1324                         break;
1325                 default:
1326                         return -EOPNOTSUPP;
1327                 }
1328
1329                 /* In decap flow, header pointers should point to the inner
1330                  * headers, outer header were already set by parse_tunnel_attr
1331                  */
1332                 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1333                                          inner_headers);
1334                 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1335                                          inner_headers);
1336         }
1337
1338         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1339                 struct flow_dissector_key_eth_addrs *key =
1340                         skb_flow_dissector_target(f->dissector,
1341                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
1342                                                   f->key);
1343                 struct flow_dissector_key_eth_addrs *mask =
1344                         skb_flow_dissector_target(f->dissector,
1345                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
1346                                                   f->mask);
1347
1348                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1349                                              dmac_47_16),
1350                                 mask->dst);
1351                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1352                                              dmac_47_16),
1353                                 key->dst);
1354
1355                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1356                                              smac_47_16),
1357                                 mask->src);
1358                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1359                                              smac_47_16),
1360                                 key->src);
1361
1362                 if (!is_zero_ether_addr(mask->src) || !is_zero_ether_addr(mask->dst))
1363                         *match_level = MLX5_MATCH_L2;
1364         }
1365
1366         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
1367                 struct flow_dissector_key_vlan *key =
1368                         skb_flow_dissector_target(f->dissector,
1369                                                   FLOW_DISSECTOR_KEY_VLAN,
1370                                                   f->key);
1371                 struct flow_dissector_key_vlan *mask =
1372                         skb_flow_dissector_target(f->dissector,
1373                                                   FLOW_DISSECTOR_KEY_VLAN,
1374                                                   f->mask);
1375                 if (mask->vlan_id || mask->vlan_priority || mask->vlan_tpid) {
1376                         if (key->vlan_tpid == htons(ETH_P_8021AD)) {
1377                                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1378                                          svlan_tag, 1);
1379                                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1380                                          svlan_tag, 1);
1381                         } else {
1382                                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1383                                          cvlan_tag, 1);
1384                                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1385                                          cvlan_tag, 1);
1386                         }
1387
1388                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
1389                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
1390
1391                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio, mask->vlan_priority);
1392                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, key->vlan_priority);
1393
1394                         *match_level = MLX5_MATCH_L2;
1395                 }
1396         } else {
1397                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, svlan_tag, 1);
1398                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
1399         }
1400
1401         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CVLAN)) {
1402                 struct flow_dissector_key_vlan *key =
1403                         skb_flow_dissector_target(f->dissector,
1404                                                   FLOW_DISSECTOR_KEY_CVLAN,
1405                                                   f->key);
1406                 struct flow_dissector_key_vlan *mask =
1407                         skb_flow_dissector_target(f->dissector,
1408                                                   FLOW_DISSECTOR_KEY_CVLAN,
1409                                                   f->mask);
1410                 if (mask->vlan_id || mask->vlan_priority || mask->vlan_tpid) {
1411                         if (key->vlan_tpid == htons(ETH_P_8021AD)) {
1412                                 MLX5_SET(fte_match_set_misc, misc_c,
1413                                          outer_second_svlan_tag, 1);
1414                                 MLX5_SET(fte_match_set_misc, misc_v,
1415                                          outer_second_svlan_tag, 1);
1416                         } else {
1417                                 MLX5_SET(fte_match_set_misc, misc_c,
1418                                          outer_second_cvlan_tag, 1);
1419                                 MLX5_SET(fte_match_set_misc, misc_v,
1420                                          outer_second_cvlan_tag, 1);
1421                         }
1422
1423                         MLX5_SET(fte_match_set_misc, misc_c, outer_second_vid,
1424                                  mask->vlan_id);
1425                         MLX5_SET(fte_match_set_misc, misc_v, outer_second_vid,
1426                                  key->vlan_id);
1427                         MLX5_SET(fte_match_set_misc, misc_c, outer_second_prio,
1428                                  mask->vlan_priority);
1429                         MLX5_SET(fte_match_set_misc, misc_v, outer_second_prio,
1430                                  key->vlan_priority);
1431
1432                         *match_level = MLX5_MATCH_L2;
1433                 }
1434         }
1435
1436         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
1437                 struct flow_dissector_key_basic *key =
1438                         skb_flow_dissector_target(f->dissector,
1439                                                   FLOW_DISSECTOR_KEY_BASIC,
1440                                                   f->key);
1441                 struct flow_dissector_key_basic *mask =
1442                         skb_flow_dissector_target(f->dissector,
1443                                                   FLOW_DISSECTOR_KEY_BASIC,
1444                                                   f->mask);
1445                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
1446                          ntohs(mask->n_proto));
1447                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
1448                          ntohs(key->n_proto));
1449
1450                 if (mask->n_proto)
1451                         *match_level = MLX5_MATCH_L2;
1452         }
1453
1454         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
1455                 struct flow_dissector_key_control *key =
1456                         skb_flow_dissector_target(f->dissector,
1457                                                   FLOW_DISSECTOR_KEY_CONTROL,
1458                                                   f->key);
1459
1460                 struct flow_dissector_key_control *mask =
1461                         skb_flow_dissector_target(f->dissector,
1462                                                   FLOW_DISSECTOR_KEY_CONTROL,
1463                                                   f->mask);
1464                 addr_type = key->addr_type;
1465
1466                 /* the HW doesn't support frag first/later */
1467                 if (mask->flags & FLOW_DIS_FIRST_FRAG)
1468                         return -EOPNOTSUPP;
1469
1470                 if (mask->flags & FLOW_DIS_IS_FRAGMENT) {
1471                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
1472                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
1473                                  key->flags & FLOW_DIS_IS_FRAGMENT);
1474
1475                         /* the HW doesn't need L3 inline to match on frag=no */
1476                         if (!(key->flags & FLOW_DIS_IS_FRAGMENT))
1477                                 *match_level = MLX5_INLINE_MODE_L2;
1478         /* ***  L2 attributes parsing up to here *** */
1479                         else
1480                                 *match_level = MLX5_INLINE_MODE_IP;
1481                 }
1482         }
1483
1484         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
1485                 struct flow_dissector_key_basic *key =
1486                         skb_flow_dissector_target(f->dissector,
1487                                                   FLOW_DISSECTOR_KEY_BASIC,
1488                                                   f->key);
1489                 struct flow_dissector_key_basic *mask =
1490                         skb_flow_dissector_target(f->dissector,
1491                                                   FLOW_DISSECTOR_KEY_BASIC,
1492                                                   f->mask);
1493                 ip_proto = key->ip_proto;
1494
1495                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1496                          mask->ip_proto);
1497                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1498                          key->ip_proto);
1499
1500                 if (mask->ip_proto)
1501                         *match_level = MLX5_MATCH_L3;
1502         }
1503
1504         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1505                 struct flow_dissector_key_ipv4_addrs *key =
1506                         skb_flow_dissector_target(f->dissector,
1507                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1508                                                   f->key);
1509                 struct flow_dissector_key_ipv4_addrs *mask =
1510                         skb_flow_dissector_target(f->dissector,
1511                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1512                                                   f->mask);
1513
1514                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1515                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
1516                        &mask->src, sizeof(mask->src));
1517                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1518                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
1519                        &key->src, sizeof(key->src));
1520                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1521                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1522                        &mask->dst, sizeof(mask->dst));
1523                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1524                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1525                        &key->dst, sizeof(key->dst));
1526
1527                 if (mask->src || mask->dst)
1528                         *match_level = MLX5_MATCH_L3;
1529         }
1530
1531         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1532                 struct flow_dissector_key_ipv6_addrs *key =
1533                         skb_flow_dissector_target(f->dissector,
1534                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1535                                                   f->key);
1536                 struct flow_dissector_key_ipv6_addrs *mask =
1537                         skb_flow_dissector_target(f->dissector,
1538                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1539                                                   f->mask);
1540
1541                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1542                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
1543                        &mask->src, sizeof(mask->src));
1544                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1545                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
1546                        &key->src, sizeof(key->src));
1547
1548                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1549                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1550                        &mask->dst, sizeof(mask->dst));
1551                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1552                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1553                        &key->dst, sizeof(key->dst));
1554
1555                 if (ipv6_addr_type(&mask->src) != IPV6_ADDR_ANY ||
1556                     ipv6_addr_type(&mask->dst) != IPV6_ADDR_ANY)
1557                         *match_level = MLX5_MATCH_L3;
1558         }
1559
1560         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_IP)) {
1561                 struct flow_dissector_key_ip *key =
1562                         skb_flow_dissector_target(f->dissector,
1563                                                   FLOW_DISSECTOR_KEY_IP,
1564                                                   f->key);
1565                 struct flow_dissector_key_ip *mask =
1566                         skb_flow_dissector_target(f->dissector,
1567                                                   FLOW_DISSECTOR_KEY_IP,
1568                                                   f->mask);
1569
1570                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn, mask->tos & 0x3);
1571                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, key->tos & 0x3);
1572
1573                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp, mask->tos >> 2);
1574                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, key->tos  >> 2);
1575
1576                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit, mask->ttl);
1577                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit, key->ttl);
1578
1579                 if (mask->ttl &&
1580                     !MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
1581                                                 ft_field_support.outer_ipv4_ttl)) {
1582                         NL_SET_ERR_MSG_MOD(extack,
1583                                            "Matching on TTL is not supported");
1584                         return -EOPNOTSUPP;
1585                 }
1586
1587                 if (mask->tos || mask->ttl)
1588                         *match_level = MLX5_MATCH_L3;
1589         }
1590
1591         /* ***  L3 attributes parsing up to here *** */
1592
1593         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
1594                 struct flow_dissector_key_ports *key =
1595                         skb_flow_dissector_target(f->dissector,
1596                                                   FLOW_DISSECTOR_KEY_PORTS,
1597                                                   f->key);
1598                 struct flow_dissector_key_ports *mask =
1599                         skb_flow_dissector_target(f->dissector,
1600                                                   FLOW_DISSECTOR_KEY_PORTS,
1601                                                   f->mask);
1602                 switch (ip_proto) {
1603                 case IPPROTO_TCP:
1604                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1605                                  tcp_sport, ntohs(mask->src));
1606                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1607                                  tcp_sport, ntohs(key->src));
1608
1609                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1610                                  tcp_dport, ntohs(mask->dst));
1611                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1612                                  tcp_dport, ntohs(key->dst));
1613                         break;
1614
1615                 case IPPROTO_UDP:
1616                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1617                                  udp_sport, ntohs(mask->src));
1618                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1619                                  udp_sport, ntohs(key->src));
1620
1621                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1622                                  udp_dport, ntohs(mask->dst));
1623                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1624                                  udp_dport, ntohs(key->dst));
1625                         break;
1626                 default:
1627                         NL_SET_ERR_MSG_MOD(extack,
1628                                            "Only UDP and TCP transports are supported for L4 matching");
1629                         netdev_err(priv->netdev,
1630                                    "Only UDP and TCP transport are supported\n");
1631                         return -EINVAL;
1632                 }
1633
1634                 if (mask->src || mask->dst)
1635                         *match_level = MLX5_MATCH_L4;
1636         }
1637
1638         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_TCP)) {
1639                 struct flow_dissector_key_tcp *key =
1640                         skb_flow_dissector_target(f->dissector,
1641                                                   FLOW_DISSECTOR_KEY_TCP,
1642                                                   f->key);
1643                 struct flow_dissector_key_tcp *mask =
1644                         skb_flow_dissector_target(f->dissector,
1645                                                   FLOW_DISSECTOR_KEY_TCP,
1646                                                   f->mask);
1647
1648                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_flags,
1649                          ntohs(mask->flags));
1650                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
1651                          ntohs(key->flags));
1652
1653                 if (mask->flags)
1654                         *match_level = MLX5_MATCH_L4;
1655         }
1656
1657         return 0;
1658 }
1659
1660 static int parse_cls_flower(struct mlx5e_priv *priv,
1661                             struct mlx5e_tc_flow *flow,
1662                             struct mlx5_flow_spec *spec,
1663                             struct tc_cls_flower_offload *f)
1664 {
1665         struct netlink_ext_ack *extack = f->common.extack;
1666         struct mlx5_core_dev *dev = priv->mdev;
1667         struct mlx5_eswitch *esw = dev->priv.eswitch;
1668         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1669         struct mlx5_eswitch_rep *rep;
1670         u8 match_level;
1671         int err;
1672
1673         err = __parse_cls_flower(priv, spec, f, &match_level);
1674
1675         if (!err && (flow->flags & MLX5E_TC_FLOW_ESWITCH)) {
1676                 rep = rpriv->rep;
1677                 if (rep->vport != FDB_UPLINK_VPORT &&
1678                     (esw->offloads.inline_mode != MLX5_INLINE_MODE_NONE &&
1679                     esw->offloads.inline_mode < match_level)) {
1680                         NL_SET_ERR_MSG_MOD(extack,
1681                                            "Flow is not offloaded due to min inline setting");
1682                         netdev_warn(priv->netdev,
1683                                     "Flow is not offloaded due to min inline setting, required %d actual %d\n",
1684                                     match_level, esw->offloads.inline_mode);
1685                         return -EOPNOTSUPP;
1686                 }
1687         }
1688
1689         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
1690                 flow->esw_attr->match_level = match_level;
1691         else
1692                 flow->nic_attr->match_level = match_level;
1693
1694         return err;
1695 }
1696
1697 struct pedit_headers {
1698         struct ethhdr  eth;
1699         struct iphdr   ip4;
1700         struct ipv6hdr ip6;
1701         struct tcphdr  tcp;
1702         struct udphdr  udp;
1703 };
1704
1705 static int pedit_header_offsets[] = {
1706         [TCA_PEDIT_KEY_EX_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth),
1707         [TCA_PEDIT_KEY_EX_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4),
1708         [TCA_PEDIT_KEY_EX_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6),
1709         [TCA_PEDIT_KEY_EX_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp),
1710         [TCA_PEDIT_KEY_EX_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp),
1711 };
1712
1713 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype])
1714
1715 static int set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset,
1716                          struct pedit_headers *masks,
1717                          struct pedit_headers *vals)
1718 {
1719         u32 *curr_pmask, *curr_pval;
1720
1721         if (hdr_type >= __PEDIT_HDR_TYPE_MAX)
1722                 goto out_err;
1723
1724         curr_pmask = (u32 *)(pedit_header(masks, hdr_type) + offset);
1725         curr_pval  = (u32 *)(pedit_header(vals, hdr_type) + offset);
1726
1727         if (*curr_pmask & mask)  /* disallow acting twice on the same location */
1728                 goto out_err;
1729
1730         *curr_pmask |= mask;
1731         *curr_pval  |= (val & mask);
1732
1733         return 0;
1734
1735 out_err:
1736         return -EOPNOTSUPP;
1737 }
1738
1739 struct mlx5_fields {
1740         u8  field;
1741         u8  size;
1742         u32 offset;
1743 };
1744
1745 #define OFFLOAD(fw_field, size, field, off) \
1746                 {MLX5_ACTION_IN_FIELD_OUT_ ## fw_field, size, offsetof(struct pedit_headers, field) + (off)}
1747
1748 static struct mlx5_fields fields[] = {
1749         OFFLOAD(DMAC_47_16, 4, eth.h_dest[0], 0),
1750         OFFLOAD(DMAC_15_0,  2, eth.h_dest[4], 0),
1751         OFFLOAD(SMAC_47_16, 4, eth.h_source[0], 0),
1752         OFFLOAD(SMAC_15_0,  2, eth.h_source[4], 0),
1753         OFFLOAD(ETHERTYPE,  2, eth.h_proto, 0),
1754
1755         OFFLOAD(IP_TTL, 1, ip4.ttl,   0),
1756         OFFLOAD(SIPV4,  4, ip4.saddr, 0),
1757         OFFLOAD(DIPV4,  4, ip4.daddr, 0),
1758
1759         OFFLOAD(SIPV6_127_96, 4, ip6.saddr.s6_addr32[0], 0),
1760         OFFLOAD(SIPV6_95_64,  4, ip6.saddr.s6_addr32[1], 0),
1761         OFFLOAD(SIPV6_63_32,  4, ip6.saddr.s6_addr32[2], 0),
1762         OFFLOAD(SIPV6_31_0,   4, ip6.saddr.s6_addr32[3], 0),
1763         OFFLOAD(DIPV6_127_96, 4, ip6.daddr.s6_addr32[0], 0),
1764         OFFLOAD(DIPV6_95_64,  4, ip6.daddr.s6_addr32[1], 0),
1765         OFFLOAD(DIPV6_63_32,  4, ip6.daddr.s6_addr32[2], 0),
1766         OFFLOAD(DIPV6_31_0,   4, ip6.daddr.s6_addr32[3], 0),
1767         OFFLOAD(IPV6_HOPLIMIT, 1, ip6.hop_limit, 0),
1768
1769         OFFLOAD(TCP_SPORT, 2, tcp.source,  0),
1770         OFFLOAD(TCP_DPORT, 2, tcp.dest,    0),
1771         OFFLOAD(TCP_FLAGS, 1, tcp.ack_seq, 5),
1772
1773         OFFLOAD(UDP_SPORT, 2, udp.source, 0),
1774         OFFLOAD(UDP_DPORT, 2, udp.dest,   0),
1775 };
1776
1777 /* On input attr->num_mod_hdr_actions tells how many HW actions can be parsed at
1778  * max from the SW pedit action. On success, it says how many HW actions were
1779  * actually parsed.
1780  */
1781 static int offload_pedit_fields(struct pedit_headers *masks,
1782                                 struct pedit_headers *vals,
1783                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
1784                                 struct netlink_ext_ack *extack)
1785 {
1786         struct pedit_headers *set_masks, *add_masks, *set_vals, *add_vals;
1787         int i, action_size, nactions, max_actions, first, last, next_z;
1788         void *s_masks_p, *a_masks_p, *vals_p;
1789         struct mlx5_fields *f;
1790         u8 cmd, field_bsize;
1791         u32 s_mask, a_mask;
1792         unsigned long mask;
1793         __be32 mask_be32;
1794         __be16 mask_be16;
1795         void *action;
1796
1797         set_masks = &masks[TCA_PEDIT_KEY_EX_CMD_SET];
1798         add_masks = &masks[TCA_PEDIT_KEY_EX_CMD_ADD];
1799         set_vals = &vals[TCA_PEDIT_KEY_EX_CMD_SET];
1800         add_vals = &vals[TCA_PEDIT_KEY_EX_CMD_ADD];
1801
1802         action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1803         action = parse_attr->mod_hdr_actions;
1804         max_actions = parse_attr->num_mod_hdr_actions;
1805         nactions = 0;
1806
1807         for (i = 0; i < ARRAY_SIZE(fields); i++) {
1808                 f = &fields[i];
1809                 /* avoid seeing bits set from previous iterations */
1810                 s_mask = 0;
1811                 a_mask = 0;
1812
1813                 s_masks_p = (void *)set_masks + f->offset;
1814                 a_masks_p = (void *)add_masks + f->offset;
1815
1816                 memcpy(&s_mask, s_masks_p, f->size);
1817                 memcpy(&a_mask, a_masks_p, f->size);
1818
1819                 if (!s_mask && !a_mask) /* nothing to offload here */
1820                         continue;
1821
1822                 if (s_mask && a_mask) {
1823                         NL_SET_ERR_MSG_MOD(extack,
1824                                            "can't set and add to the same HW field");
1825                         printk(KERN_WARNING "mlx5: can't set and add to the same HW field (%x)\n", f->field);
1826                         return -EOPNOTSUPP;
1827                 }
1828
1829                 if (nactions == max_actions) {
1830                         NL_SET_ERR_MSG_MOD(extack,
1831                                            "too many pedit actions, can't offload");
1832                         printk(KERN_WARNING "mlx5: parsed %d pedit actions, can't do more\n", nactions);
1833                         return -EOPNOTSUPP;
1834                 }
1835
1836                 if (s_mask) {
1837                         cmd  = MLX5_ACTION_TYPE_SET;
1838                         mask = s_mask;
1839                         vals_p = (void *)set_vals + f->offset;
1840                         /* clear to denote we consumed this field */
1841                         memset(s_masks_p, 0, f->size);
1842                 } else {
1843                         cmd  = MLX5_ACTION_TYPE_ADD;
1844                         mask = a_mask;
1845                         vals_p = (void *)add_vals + f->offset;
1846                         /* clear to denote we consumed this field */
1847                         memset(a_masks_p, 0, f->size);
1848                 }
1849
1850                 field_bsize = f->size * BITS_PER_BYTE;
1851
1852                 if (field_bsize == 32) {
1853                         mask_be32 = *(__be32 *)&mask;
1854                         mask = (__force unsigned long)cpu_to_le32(be32_to_cpu(mask_be32));
1855                 } else if (field_bsize == 16) {
1856                         mask_be16 = *(__be16 *)&mask;
1857                         mask = (__force unsigned long)cpu_to_le16(be16_to_cpu(mask_be16));
1858                 }
1859
1860                 first = find_first_bit(&mask, field_bsize);
1861                 next_z = find_next_zero_bit(&mask, field_bsize, first);
1862                 last  = find_last_bit(&mask, field_bsize);
1863                 if (first < next_z && next_z < last) {
1864                         NL_SET_ERR_MSG_MOD(extack,
1865                                            "rewrite of few sub-fields isn't supported");
1866                         printk(KERN_WARNING "mlx5: rewrite of few sub-fields (mask %lx) isn't offloaded\n",
1867                                mask);
1868                         return -EOPNOTSUPP;
1869                 }
1870
1871                 MLX5_SET(set_action_in, action, action_type, cmd);
1872                 MLX5_SET(set_action_in, action, field, f->field);
1873
1874                 if (cmd == MLX5_ACTION_TYPE_SET) {
1875                         MLX5_SET(set_action_in, action, offset, first);
1876                         /* length is num of bits to be written, zero means length of 32 */
1877                         MLX5_SET(set_action_in, action, length, (last - first + 1));
1878                 }
1879
1880                 if (field_bsize == 32)
1881                         MLX5_SET(set_action_in, action, data, ntohl(*(__be32 *)vals_p) >> first);
1882                 else if (field_bsize == 16)
1883                         MLX5_SET(set_action_in, action, data, ntohs(*(__be16 *)vals_p) >> first);
1884                 else if (field_bsize == 8)
1885                         MLX5_SET(set_action_in, action, data, *(u8 *)vals_p >> first);
1886
1887                 action += action_size;
1888                 nactions++;
1889         }
1890
1891         parse_attr->num_mod_hdr_actions = nactions;
1892         return 0;
1893 }
1894
1895 static int alloc_mod_hdr_actions(struct mlx5e_priv *priv,
1896                                  const struct tc_action *a, int namespace,
1897                                  struct mlx5e_tc_flow_parse_attr *parse_attr)
1898 {
1899         int nkeys, action_size, max_actions;
1900
1901         nkeys = tcf_pedit_nkeys(a);
1902         action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1903
1904         if (namespace == MLX5_FLOW_NAMESPACE_FDB) /* FDB offloading */
1905                 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, max_modify_header_actions);
1906         else /* namespace is MLX5_FLOW_NAMESPACE_KERNEL - NIC offloading */
1907                 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, max_modify_header_actions);
1908
1909         /* can get up to crazingly 16 HW actions in 32 bits pedit SW key */
1910         max_actions = min(max_actions, nkeys * 16);
1911
1912         parse_attr->mod_hdr_actions = kcalloc(max_actions, action_size, GFP_KERNEL);
1913         if (!parse_attr->mod_hdr_actions)
1914                 return -ENOMEM;
1915
1916         parse_attr->num_mod_hdr_actions = max_actions;
1917         return 0;
1918 }
1919
1920 static const struct pedit_headers zero_masks = {};
1921
1922 static int parse_tc_pedit_action(struct mlx5e_priv *priv,
1923                                  const struct tc_action *a, int namespace,
1924                                  struct mlx5e_tc_flow_parse_attr *parse_attr,
1925                                  struct netlink_ext_ack *extack)
1926 {
1927         struct pedit_headers masks[__PEDIT_CMD_MAX], vals[__PEDIT_CMD_MAX], *cmd_masks;
1928         int nkeys, i, err = -EOPNOTSUPP;
1929         u32 mask, val, offset;
1930         u8 cmd, htype;
1931
1932         nkeys = tcf_pedit_nkeys(a);
1933
1934         memset(masks, 0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1935         memset(vals,  0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1936
1937         for (i = 0; i < nkeys; i++) {
1938                 htype = tcf_pedit_htype(a, i);
1939                 cmd = tcf_pedit_cmd(a, i);
1940                 err = -EOPNOTSUPP; /* can't be all optimistic */
1941
1942                 if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK) {
1943                         NL_SET_ERR_MSG_MOD(extack,
1944                                            "legacy pedit isn't offloaded");
1945                         goto out_err;
1946                 }
1947
1948                 if (cmd != TCA_PEDIT_KEY_EX_CMD_SET && cmd != TCA_PEDIT_KEY_EX_CMD_ADD) {
1949                         NL_SET_ERR_MSG_MOD(extack, "pedit cmd isn't offloaded");
1950                         goto out_err;
1951                 }
1952
1953                 mask = tcf_pedit_mask(a, i);
1954                 val = tcf_pedit_val(a, i);
1955                 offset = tcf_pedit_offset(a, i);
1956
1957                 err = set_pedit_val(htype, ~mask, val, offset, &masks[cmd], &vals[cmd]);
1958                 if (err)
1959                         goto out_err;
1960         }
1961
1962         err = alloc_mod_hdr_actions(priv, a, namespace, parse_attr);
1963         if (err)
1964                 goto out_err;
1965
1966         err = offload_pedit_fields(masks, vals, parse_attr, extack);
1967         if (err < 0)
1968                 goto out_dealloc_parsed_actions;
1969
1970         for (cmd = 0; cmd < __PEDIT_CMD_MAX; cmd++) {
1971                 cmd_masks = &masks[cmd];
1972                 if (memcmp(cmd_masks, &zero_masks, sizeof(zero_masks))) {
1973                         NL_SET_ERR_MSG_MOD(extack,
1974                                            "attempt to offload an unsupported field");
1975                         netdev_warn(priv->netdev, "attempt to offload an unsupported field (cmd %d)\n", cmd);
1976                         print_hex_dump(KERN_WARNING, "mask: ", DUMP_PREFIX_ADDRESS,
1977                                        16, 1, cmd_masks, sizeof(zero_masks), true);
1978                         err = -EOPNOTSUPP;
1979                         goto out_dealloc_parsed_actions;
1980                 }
1981         }
1982
1983         return 0;
1984
1985 out_dealloc_parsed_actions:
1986         kfree(parse_attr->mod_hdr_actions);
1987 out_err:
1988         return err;
1989 }
1990
1991 static bool csum_offload_supported(struct mlx5e_priv *priv,
1992                                    u32 action,
1993                                    u32 update_flags,
1994                                    struct netlink_ext_ack *extack)
1995 {
1996         u32 prot_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR | TCA_CSUM_UPDATE_FLAG_TCP |
1997                          TCA_CSUM_UPDATE_FLAG_UDP;
1998
1999         /*  The HW recalcs checksums only if re-writing headers */
2000         if (!(action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)) {
2001                 NL_SET_ERR_MSG_MOD(extack,
2002                                    "TC csum action is only offloaded with pedit");
2003                 netdev_warn(priv->netdev,
2004                             "TC csum action is only offloaded with pedit\n");
2005                 return false;
2006         }
2007
2008         if (update_flags & ~prot_flags) {
2009                 NL_SET_ERR_MSG_MOD(extack,
2010                                    "can't offload TC csum action for some header/s");
2011                 netdev_warn(priv->netdev,
2012                             "can't offload TC csum action for some header/s - flags %#x\n",
2013                             update_flags);
2014                 return false;
2015         }
2016
2017         return true;
2018 }
2019
2020 static bool modify_header_match_supported(struct mlx5_flow_spec *spec,
2021                                           struct tcf_exts *exts,
2022                                           struct netlink_ext_ack *extack)
2023 {
2024         const struct tc_action *a;
2025         bool modify_ip_header;
2026         LIST_HEAD(actions);
2027         u8 htype, ip_proto;
2028         void *headers_v;
2029         u16 ethertype;
2030         int nkeys, i;
2031
2032         headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
2033         ethertype = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ethertype);
2034
2035         /* for non-IP we only re-write MACs, so we're okay */
2036         if (ethertype != ETH_P_IP && ethertype != ETH_P_IPV6)
2037                 goto out_ok;
2038
2039         modify_ip_header = false;
2040         tcf_exts_for_each_action(i, a, exts) {
2041                 int k;
2042
2043                 if (!is_tcf_pedit(a))
2044                         continue;
2045
2046                 nkeys = tcf_pedit_nkeys(a);
2047                 for (k = 0; k < nkeys; k++) {
2048                         htype = tcf_pedit_htype(a, k);
2049                         if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_IP4 ||
2050                             htype == TCA_PEDIT_KEY_EX_HDR_TYPE_IP6) {
2051                                 modify_ip_header = true;
2052                                 break;
2053                         }
2054                 }
2055         }
2056
2057         ip_proto = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ip_protocol);
2058         if (modify_ip_header && ip_proto != IPPROTO_TCP &&
2059             ip_proto != IPPROTO_UDP && ip_proto != IPPROTO_ICMP) {
2060                 NL_SET_ERR_MSG_MOD(extack,
2061                                    "can't offload re-write of non TCP/UDP");
2062                 pr_info("can't offload re-write of ip proto %d\n", ip_proto);
2063                 return false;
2064         }
2065
2066 out_ok:
2067         return true;
2068 }
2069
2070 static bool actions_match_supported(struct mlx5e_priv *priv,
2071                                     struct tcf_exts *exts,
2072                                     struct mlx5e_tc_flow_parse_attr *parse_attr,
2073                                     struct mlx5e_tc_flow *flow,
2074                                     struct netlink_ext_ack *extack)
2075 {
2076         u32 actions;
2077
2078         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
2079                 actions = flow->esw_attr->action;
2080         else
2081                 actions = flow->nic_attr->action;
2082
2083         if (flow->flags & MLX5E_TC_FLOW_EGRESS &&
2084             !(actions & MLX5_FLOW_CONTEXT_ACTION_DECAP))
2085                 return false;
2086
2087         if (actions & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2088                 return modify_header_match_supported(&parse_attr->spec, exts,
2089                                                      extack);
2090
2091         return true;
2092 }
2093
2094 static bool same_hw_devs(struct mlx5e_priv *priv, struct mlx5e_priv *peer_priv)
2095 {
2096         struct mlx5_core_dev *fmdev, *pmdev;
2097         u64 fsystem_guid, psystem_guid;
2098
2099         fmdev = priv->mdev;
2100         pmdev = peer_priv->mdev;
2101
2102         fsystem_guid = mlx5_query_nic_system_image_guid(fmdev);
2103         psystem_guid = mlx5_query_nic_system_image_guid(pmdev);
2104
2105         return (fsystem_guid == psystem_guid);
2106 }
2107
2108 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
2109                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
2110                                 struct mlx5e_tc_flow *flow,
2111                                 struct netlink_ext_ack *extack)
2112 {
2113         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
2114         const struct tc_action *a;
2115         LIST_HEAD(actions);
2116         u32 action = 0;
2117         int err, i;
2118
2119         if (!tcf_exts_has_actions(exts))
2120                 return -EINVAL;
2121
2122         attr->flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2123
2124         tcf_exts_for_each_action(i, a, exts) {
2125                 if (is_tcf_gact_shot(a)) {
2126                         action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2127                         if (MLX5_CAP_FLOWTABLE(priv->mdev,
2128                                                flow_table_properties_nic_receive.flow_counter))
2129                                 action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
2130                         continue;
2131                 }
2132
2133                 if (is_tcf_pedit(a)) {
2134                         err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_KERNEL,
2135                                                     parse_attr, extack);
2136                         if (err)
2137                                 return err;
2138
2139                         action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
2140                                   MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2141                         continue;
2142                 }
2143
2144                 if (is_tcf_csum(a)) {
2145                         if (csum_offload_supported(priv, action,
2146                                                    tcf_csum_update_flags(a),
2147                                                    extack))
2148                                 continue;
2149
2150                         return -EOPNOTSUPP;
2151                 }
2152
2153                 if (is_tcf_mirred_egress_redirect(a)) {
2154                         struct net_device *peer_dev = tcf_mirred_dev(a);
2155
2156                         if (priv->netdev->netdev_ops == peer_dev->netdev_ops &&
2157                             same_hw_devs(priv, netdev_priv(peer_dev))) {
2158                                 parse_attr->mirred_ifindex = peer_dev->ifindex;
2159                                 flow->flags |= MLX5E_TC_FLOW_HAIRPIN;
2160                                 action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
2161                                           MLX5_FLOW_CONTEXT_ACTION_COUNT;
2162                         } else {
2163                                 NL_SET_ERR_MSG_MOD(extack,
2164                                                    "device is not on same HW, can't offload");
2165                                 netdev_warn(priv->netdev, "device %s not on same HW, can't offload\n",
2166                                             peer_dev->name);
2167                                 return -EINVAL;
2168                         }
2169                         continue;
2170                 }
2171
2172                 if (is_tcf_skbedit_mark(a)) {
2173                         u32 mark = tcf_skbedit_mark(a);
2174
2175                         if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
2176                                 NL_SET_ERR_MSG_MOD(extack,
2177                                                    "Bad flow mark - only 16 bit is supported");
2178                                 return -EINVAL;
2179                         }
2180
2181                         attr->flow_tag = mark;
2182                         action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2183                         continue;
2184                 }
2185
2186                 return -EINVAL;
2187         }
2188
2189         attr->action = action;
2190         if (!actions_match_supported(priv, exts, parse_attr, flow, extack))
2191                 return -EOPNOTSUPP;
2192
2193         return 0;
2194 }
2195
2196 static inline int cmp_encap_info(struct ip_tunnel_key *a,
2197                                  struct ip_tunnel_key *b)
2198 {
2199         return memcmp(a, b, sizeof(*a));
2200 }
2201
2202 static inline int hash_encap_info(struct ip_tunnel_key *key)
2203 {
2204         return jhash(key, sizeof(*key), 0);
2205 }
2206
2207 static int mlx5e_route_lookup_ipv4(struct mlx5e_priv *priv,
2208                                    struct net_device *mirred_dev,
2209                                    struct net_device **out_dev,
2210                                    struct flowi4 *fl4,
2211                                    struct neighbour **out_n,
2212                                    u8 *out_ttl)
2213 {
2214         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2215         struct mlx5e_rep_priv *uplink_rpriv;
2216         struct rtable *rt;
2217         struct neighbour *n = NULL;
2218
2219 #if IS_ENABLED(CONFIG_INET)
2220         int ret;
2221
2222         rt = ip_route_output_key(dev_net(mirred_dev), fl4);
2223         ret = PTR_ERR_OR_ZERO(rt);
2224         if (ret)
2225                 return ret;
2226 #else
2227         return -EOPNOTSUPP;
2228 #endif
2229         uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
2230         /* if the egress device isn't on the same HW e-switch, we use the uplink */
2231         if (!switchdev_port_same_parent_id(priv->netdev, rt->dst.dev))
2232                 *out_dev = uplink_rpriv->netdev;
2233         else
2234                 *out_dev = rt->dst.dev;
2235
2236         if (!(*out_ttl))
2237                 *out_ttl = ip4_dst_hoplimit(&rt->dst);
2238         n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
2239         ip_rt_put(rt);
2240         if (!n)
2241                 return -ENOMEM;
2242
2243         *out_n = n;
2244         return 0;
2245 }
2246
2247 static bool is_merged_eswitch_dev(struct mlx5e_priv *priv,
2248                                   struct net_device *peer_netdev)
2249 {
2250         struct mlx5e_priv *peer_priv;
2251
2252         peer_priv = netdev_priv(peer_netdev);
2253
2254         return (MLX5_CAP_ESW(priv->mdev, merged_eswitch) &&
2255                 (priv->netdev->netdev_ops == peer_netdev->netdev_ops) &&
2256                 same_hw_devs(priv, peer_priv) &&
2257                 MLX5_VPORT_MANAGER(peer_priv->mdev) &&
2258                 (peer_priv->mdev->priv.eswitch->mode == SRIOV_OFFLOADS));
2259 }
2260
2261 static int mlx5e_route_lookup_ipv6(struct mlx5e_priv *priv,
2262                                    struct net_device *mirred_dev,
2263                                    struct net_device **out_dev,
2264                                    struct flowi6 *fl6,
2265                                    struct neighbour **out_n,
2266                                    u8 *out_ttl)
2267 {
2268         struct neighbour *n = NULL;
2269         struct dst_entry *dst;
2270
2271 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
2272         struct mlx5e_rep_priv *uplink_rpriv;
2273         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2274         int ret;
2275
2276         ret = ipv6_stub->ipv6_dst_lookup(dev_net(mirred_dev), NULL, &dst,
2277                                          fl6);
2278         if (ret < 0)
2279                 return ret;
2280
2281         if (!(*out_ttl))
2282                 *out_ttl = ip6_dst_hoplimit(dst);
2283
2284         uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
2285         /* if the egress device isn't on the same HW e-switch, we use the uplink */
2286         if (!switchdev_port_same_parent_id(priv->netdev, dst->dev))
2287                 *out_dev = uplink_rpriv->netdev;
2288         else
2289                 *out_dev = dst->dev;
2290 #else
2291         return -EOPNOTSUPP;
2292 #endif
2293
2294         n = dst_neigh_lookup(dst, &fl6->daddr);
2295         dst_release(dst);
2296         if (!n)
2297                 return -ENOMEM;
2298
2299         *out_n = n;
2300         return 0;
2301 }
2302
2303 static void gen_vxlan_header_ipv4(struct net_device *out_dev,
2304                                   char buf[], int encap_size,
2305                                   unsigned char h_dest[ETH_ALEN],
2306                                   u8 tos, u8 ttl,
2307                                   __be32 daddr,
2308                                   __be32 saddr,
2309                                   __be16 udp_dst_port,
2310                                   __be32 vx_vni)
2311 {
2312         struct ethhdr *eth = (struct ethhdr *)buf;
2313         struct iphdr  *ip = (struct iphdr *)((char *)eth + sizeof(struct ethhdr));
2314         struct udphdr *udp = (struct udphdr *)((char *)ip + sizeof(struct iphdr));
2315         struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
2316
2317         memset(buf, 0, encap_size);
2318
2319         ether_addr_copy(eth->h_dest, h_dest);
2320         ether_addr_copy(eth->h_source, out_dev->dev_addr);
2321         eth->h_proto = htons(ETH_P_IP);
2322
2323         ip->daddr = daddr;
2324         ip->saddr = saddr;
2325
2326         ip->tos = tos;
2327         ip->ttl = ttl;
2328         ip->protocol = IPPROTO_UDP;
2329         ip->version = 0x4;
2330         ip->ihl = 0x5;
2331
2332         udp->dest = udp_dst_port;
2333         vxh->vx_flags = VXLAN_HF_VNI;
2334         vxh->vx_vni = vxlan_vni_field(vx_vni);
2335 }
2336
2337 static void gen_vxlan_header_ipv6(struct net_device *out_dev,
2338                                   char buf[], int encap_size,
2339                                   unsigned char h_dest[ETH_ALEN],
2340                                   u8 tos, u8 ttl,
2341                                   struct in6_addr *daddr,
2342                                   struct in6_addr *saddr,
2343                                   __be16 udp_dst_port,
2344                                   __be32 vx_vni)
2345 {
2346         struct ethhdr *eth = (struct ethhdr *)buf;
2347         struct ipv6hdr *ip6h = (struct ipv6hdr *)((char *)eth + sizeof(struct ethhdr));
2348         struct udphdr *udp = (struct udphdr *)((char *)ip6h + sizeof(struct ipv6hdr));
2349         struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
2350
2351         memset(buf, 0, encap_size);
2352
2353         ether_addr_copy(eth->h_dest, h_dest);
2354         ether_addr_copy(eth->h_source, out_dev->dev_addr);
2355         eth->h_proto = htons(ETH_P_IPV6);
2356
2357         ip6_flow_hdr(ip6h, tos, 0);
2358         /* the HW fills up ipv6 payload len */
2359         ip6h->nexthdr     = IPPROTO_UDP;
2360         ip6h->hop_limit   = ttl;
2361         ip6h->daddr       = *daddr;
2362         ip6h->saddr       = *saddr;
2363
2364         udp->dest = udp_dst_port;
2365         vxh->vx_flags = VXLAN_HF_VNI;
2366         vxh->vx_vni = vxlan_vni_field(vx_vni);
2367 }
2368
2369 static int mlx5e_create_encap_header_ipv4(struct mlx5e_priv *priv,
2370                                           struct net_device *mirred_dev,
2371                                           struct mlx5e_encap_entry *e)
2372 {
2373         int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
2374         int ipv4_encap_size = ETH_HLEN + sizeof(struct iphdr) + VXLAN_HLEN;
2375         struct ip_tunnel_key *tun_key = &e->tun_info.key;
2376         struct net_device *out_dev;
2377         struct neighbour *n = NULL;
2378         struct flowi4 fl4 = {};
2379         u8 nud_state, tos, ttl;
2380         char *encap_header;
2381         int err;
2382
2383         if (max_encap_size < ipv4_encap_size) {
2384                 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
2385                                ipv4_encap_size, max_encap_size);
2386                 return -EOPNOTSUPP;
2387         }
2388
2389         encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
2390         if (!encap_header)
2391                 return -ENOMEM;
2392
2393         switch (e->tunnel_type) {
2394         case MLX5_HEADER_TYPE_VXLAN:
2395                 fl4.flowi4_proto = IPPROTO_UDP;
2396                 fl4.fl4_dport = tun_key->tp_dst;
2397                 break;
2398         default:
2399                 err = -EOPNOTSUPP;
2400                 goto free_encap;
2401         }
2402
2403         tos = tun_key->tos;
2404         ttl = tun_key->ttl;
2405
2406         fl4.flowi4_tos = tun_key->tos;
2407         fl4.daddr = tun_key->u.ipv4.dst;
2408         fl4.saddr = tun_key->u.ipv4.src;
2409
2410         err = mlx5e_route_lookup_ipv4(priv, mirred_dev, &out_dev,
2411                                       &fl4, &n, &ttl);
2412         if (err)
2413                 goto free_encap;
2414
2415         /* used by mlx5e_detach_encap to lookup a neigh hash table
2416          * entry in the neigh hash table when a user deletes a rule
2417          */
2418         e->m_neigh.dev = n->dev;
2419         e->m_neigh.family = n->ops->family;
2420         memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
2421         e->out_dev = out_dev;
2422
2423         /* It's importent to add the neigh to the hash table before checking
2424          * the neigh validity state. So if we'll get a notification, in case the
2425          * neigh changes it's validity state, we would find the relevant neigh
2426          * in the hash.
2427          */
2428         err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
2429         if (err)
2430                 goto free_encap;
2431
2432         read_lock_bh(&n->lock);
2433         nud_state = n->nud_state;
2434         ether_addr_copy(e->h_dest, n->ha);
2435         read_unlock_bh(&n->lock);
2436
2437         switch (e->tunnel_type) {
2438         case MLX5_HEADER_TYPE_VXLAN:
2439                 gen_vxlan_header_ipv4(out_dev, encap_header,
2440                                       ipv4_encap_size, e->h_dest, tos, ttl,
2441                                       fl4.daddr,
2442                                       fl4.saddr, tun_key->tp_dst,
2443                                       tunnel_id_to_key32(tun_key->tun_id));
2444                 break;
2445         default:
2446                 err = -EOPNOTSUPP;
2447                 goto destroy_neigh_entry;
2448         }
2449         e->encap_size = ipv4_encap_size;
2450         e->encap_header = encap_header;
2451
2452         if (!(nud_state & NUD_VALID)) {
2453                 neigh_event_send(n, NULL);
2454                 err = -EAGAIN;
2455                 goto out;
2456         }
2457
2458         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
2459                                ipv4_encap_size, encap_header, &e->encap_id);
2460         if (err)
2461                 goto destroy_neigh_entry;
2462
2463         e->flags |= MLX5_ENCAP_ENTRY_VALID;
2464         mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
2465         neigh_release(n);
2466         return err;
2467
2468 destroy_neigh_entry:
2469         mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
2470 free_encap:
2471         kfree(encap_header);
2472 out:
2473         if (n)
2474                 neigh_release(n);
2475         return err;
2476 }
2477
2478 static int mlx5e_create_encap_header_ipv6(struct mlx5e_priv *priv,
2479                                           struct net_device *mirred_dev,
2480                                           struct mlx5e_encap_entry *e)
2481 {
2482         int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
2483         int ipv6_encap_size = ETH_HLEN + sizeof(struct ipv6hdr) + VXLAN_HLEN;
2484         struct ip_tunnel_key *tun_key = &e->tun_info.key;
2485         struct net_device *out_dev;
2486         struct neighbour *n = NULL;
2487         struct flowi6 fl6 = {};
2488         u8 nud_state, tos, ttl;
2489         char *encap_header;
2490         int err;
2491
2492         if (max_encap_size < ipv6_encap_size) {
2493                 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
2494                                ipv6_encap_size, max_encap_size);
2495                 return -EOPNOTSUPP;
2496         }
2497
2498         encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
2499         if (!encap_header)
2500                 return -ENOMEM;
2501
2502         switch (e->tunnel_type) {
2503         case MLX5_HEADER_TYPE_VXLAN:
2504                 fl6.flowi6_proto = IPPROTO_UDP;
2505                 fl6.fl6_dport = tun_key->tp_dst;
2506                 break;
2507         default:
2508                 err = -EOPNOTSUPP;
2509                 goto free_encap;
2510         }
2511
2512         tos = tun_key->tos;
2513         ttl = tun_key->ttl;
2514
2515         fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
2516         fl6.daddr = tun_key->u.ipv6.dst;
2517         fl6.saddr = tun_key->u.ipv6.src;
2518
2519         err = mlx5e_route_lookup_ipv6(priv, mirred_dev, &out_dev,
2520                                       &fl6, &n, &ttl);
2521         if (err)
2522                 goto free_encap;
2523
2524         /* used by mlx5e_detach_encap to lookup a neigh hash table
2525          * entry in the neigh hash table when a user deletes a rule
2526          */
2527         e->m_neigh.dev = n->dev;
2528         e->m_neigh.family = n->ops->family;
2529         memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
2530         e->out_dev = out_dev;
2531
2532         /* It's importent to add the neigh to the hash table before checking
2533          * the neigh validity state. So if we'll get a notification, in case the
2534          * neigh changes it's validity state, we would find the relevant neigh
2535          * in the hash.
2536          */
2537         err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
2538         if (err)
2539                 goto free_encap;
2540
2541         read_lock_bh(&n->lock);
2542         nud_state = n->nud_state;
2543         ether_addr_copy(e->h_dest, n->ha);
2544         read_unlock_bh(&n->lock);
2545
2546         switch (e->tunnel_type) {
2547         case MLX5_HEADER_TYPE_VXLAN:
2548                 gen_vxlan_header_ipv6(out_dev, encap_header,
2549                                       ipv6_encap_size, e->h_dest, tos, ttl,
2550                                       &fl6.daddr,
2551                                       &fl6.saddr, tun_key->tp_dst,
2552                                       tunnel_id_to_key32(tun_key->tun_id));
2553                 break;
2554         default:
2555                 err = -EOPNOTSUPP;
2556                 goto destroy_neigh_entry;
2557         }
2558
2559         e->encap_size = ipv6_encap_size;
2560         e->encap_header = encap_header;
2561
2562         if (!(nud_state & NUD_VALID)) {
2563                 neigh_event_send(n, NULL);
2564                 err = -EAGAIN;
2565                 goto out;
2566         }
2567
2568         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
2569                                ipv6_encap_size, encap_header, &e->encap_id);
2570         if (err)
2571                 goto destroy_neigh_entry;
2572
2573         e->flags |= MLX5_ENCAP_ENTRY_VALID;
2574         mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
2575         neigh_release(n);
2576         return err;
2577
2578 destroy_neigh_entry:
2579         mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
2580 free_encap:
2581         kfree(encap_header);
2582 out:
2583         if (n)
2584                 neigh_release(n);
2585         return err;
2586 }
2587
2588 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
2589                               struct ip_tunnel_info *tun_info,
2590                               struct net_device *mirred_dev,
2591                               struct net_device **encap_dev,
2592                               struct mlx5e_tc_flow *flow,
2593                               struct netlink_ext_ack *extack)
2594 {
2595         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2596         unsigned short family = ip_tunnel_info_af(tun_info);
2597         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
2598         struct ip_tunnel_key *key = &tun_info->key;
2599         struct mlx5e_encap_entry *e;
2600         int tunnel_type, err = 0;
2601         uintptr_t hash_key;
2602         bool found = false;
2603
2604         /* udp dst port must be set */
2605         if (!memchr_inv(&key->tp_dst, 0, sizeof(key->tp_dst)))
2606                 goto vxlan_encap_offload_err;
2607
2608         /* setting udp src port isn't supported */
2609         if (memchr_inv(&key->tp_src, 0, sizeof(key->tp_src))) {
2610 vxlan_encap_offload_err:
2611                 NL_SET_ERR_MSG_MOD(extack,
2612                                    "must set udp dst port and not set udp src port");
2613                 netdev_warn(priv->netdev,
2614                             "must set udp dst port and not set udp src port\n");
2615                 return -EOPNOTSUPP;
2616         }
2617
2618         if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, be16_to_cpu(key->tp_dst)) &&
2619             MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap)) {
2620                 tunnel_type = MLX5_HEADER_TYPE_VXLAN;
2621         } else {
2622                 NL_SET_ERR_MSG_MOD(extack,
2623                                    "port isn't an offloaded vxlan udp dport");
2624                 netdev_warn(priv->netdev,
2625                             "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->tp_dst));
2626                 return -EOPNOTSUPP;
2627         }
2628
2629         hash_key = hash_encap_info(key);
2630
2631         hash_for_each_possible_rcu(esw->offloads.encap_tbl, e,
2632                                    encap_hlist, hash_key) {
2633                 if (!cmp_encap_info(&e->tun_info.key, key)) {
2634                         found = true;
2635                         break;
2636                 }
2637         }
2638
2639         /* must verify if encap is valid or not */
2640         if (found)
2641                 goto attach_flow;
2642
2643         e = kzalloc(sizeof(*e), GFP_KERNEL);
2644         if (!e)
2645                 return -ENOMEM;
2646
2647         e->tun_info = *tun_info;
2648         e->tunnel_type = tunnel_type;
2649         INIT_LIST_HEAD(&e->flows);
2650
2651         if (family == AF_INET)
2652                 err = mlx5e_create_encap_header_ipv4(priv, mirred_dev, e);
2653         else if (family == AF_INET6)
2654                 err = mlx5e_create_encap_header_ipv6(priv, mirred_dev, e);
2655
2656         if (err && err != -EAGAIN)
2657                 goto out_err;
2658
2659         hash_add_rcu(esw->offloads.encap_tbl, &e->encap_hlist, hash_key);
2660
2661 attach_flow:
2662         list_add(&flow->encap, &e->flows);
2663         *encap_dev = e->out_dev;
2664         if (e->flags & MLX5_ENCAP_ENTRY_VALID)
2665                 attr->encap_id = e->encap_id;
2666         else
2667                 err = -EAGAIN;
2668
2669         return err;
2670
2671 out_err:
2672         kfree(e);
2673         return err;
2674 }
2675
2676 static int parse_tc_vlan_action(struct mlx5e_priv *priv,
2677                                 const struct tc_action *a,
2678                                 struct mlx5_esw_flow_attr *attr,
2679                                 u32 *action)
2680 {
2681         u8 vlan_idx = attr->total_vlan;
2682
2683         if (vlan_idx >= MLX5_FS_VLAN_DEPTH)
2684                 return -EOPNOTSUPP;
2685
2686         if (tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
2687                 if (vlan_idx) {
2688                         if (!mlx5_eswitch_vlan_actions_supported(priv->mdev,
2689                                                                  MLX5_FS_VLAN_DEPTH))
2690                                 return -EOPNOTSUPP;
2691
2692                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2;
2693                 } else {
2694                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
2695                 }
2696         } else if (tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
2697                 attr->vlan_vid[vlan_idx] = tcf_vlan_push_vid(a);
2698                 attr->vlan_prio[vlan_idx] = tcf_vlan_push_prio(a);
2699                 attr->vlan_proto[vlan_idx] = tcf_vlan_push_proto(a);
2700                 if (!attr->vlan_proto[vlan_idx])
2701                         attr->vlan_proto[vlan_idx] = htons(ETH_P_8021Q);
2702
2703                 if (vlan_idx) {
2704                         if (!mlx5_eswitch_vlan_actions_supported(priv->mdev,
2705                                                                  MLX5_FS_VLAN_DEPTH))
2706                                 return -EOPNOTSUPP;
2707
2708                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
2709                 } else {
2710                         if (!mlx5_eswitch_vlan_actions_supported(priv->mdev, 1) &&
2711                             (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q) ||
2712                              tcf_vlan_push_prio(a)))
2713                                 return -EOPNOTSUPP;
2714
2715                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
2716                 }
2717         } else { /* action is TCA_VLAN_ACT_MODIFY */
2718                 return -EOPNOTSUPP;
2719         }
2720
2721         attr->total_vlan = vlan_idx + 1;
2722
2723         return 0;
2724 }
2725
2726 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
2727                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
2728                                 struct mlx5e_tc_flow *flow,
2729                                 struct netlink_ext_ack *extack)
2730 {
2731         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
2732         struct mlx5e_rep_priv *rpriv = priv->ppriv;
2733         struct ip_tunnel_info *info = NULL;
2734         const struct tc_action *a;
2735         LIST_HEAD(actions);
2736         bool encap = false;
2737         u32 action = 0;
2738         int err, i;
2739
2740         if (!tcf_exts_has_actions(exts))
2741                 return -EINVAL;
2742
2743         attr->in_rep = rpriv->rep;
2744         attr->in_mdev = priv->mdev;
2745
2746         tcf_exts_for_each_action(i, a, exts) {
2747                 if (is_tcf_gact_shot(a)) {
2748                         action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
2749                                   MLX5_FLOW_CONTEXT_ACTION_COUNT;
2750                         continue;
2751                 }
2752
2753                 if (is_tcf_pedit(a)) {
2754                         err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_FDB,
2755                                                     parse_attr, extack);
2756                         if (err)
2757                                 return err;
2758
2759                         action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2760                         attr->mirror_count = attr->out_count;
2761                         continue;
2762                 }
2763
2764                 if (is_tcf_csum(a)) {
2765                         if (csum_offload_supported(priv, action,
2766                                                    tcf_csum_update_flags(a),
2767                                                    extack))
2768                                 continue;
2769
2770                         return -EOPNOTSUPP;
2771                 }
2772
2773                 if (is_tcf_mirred_egress_redirect(a) || is_tcf_mirred_egress_mirror(a)) {
2774                         struct mlx5e_priv *out_priv;
2775                         struct net_device *out_dev;
2776
2777                         out_dev = tcf_mirred_dev(a);
2778
2779                         if (attr->out_count >= MLX5_MAX_FLOW_FWD_VPORTS) {
2780                                 NL_SET_ERR_MSG_MOD(extack,
2781                                                    "can't support more output ports, can't offload forwarding");
2782                                 pr_err("can't support more than %d output ports, can't offload forwarding\n",
2783                                        attr->out_count);
2784                                 return -EOPNOTSUPP;
2785                         }
2786
2787                         if (switchdev_port_same_parent_id(priv->netdev,
2788                                                           out_dev) ||
2789                             is_merged_eswitch_dev(priv, out_dev)) {
2790                                 action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
2791                                           MLX5_FLOW_CONTEXT_ACTION_COUNT;
2792                                 out_priv = netdev_priv(out_dev);
2793                                 rpriv = out_priv->ppriv;
2794                                 attr->out_rep[attr->out_count] = rpriv->rep;
2795                                 attr->out_mdev[attr->out_count++] = out_priv->mdev;
2796                         } else if (encap) {
2797                                 parse_attr->mirred_ifindex = out_dev->ifindex;
2798                                 parse_attr->tun_info = *info;
2799                                 attr->parse_attr = parse_attr;
2800                                 action |= MLX5_FLOW_CONTEXT_ACTION_ENCAP |
2801                                           MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
2802                                           MLX5_FLOW_CONTEXT_ACTION_COUNT;
2803                                 /* attr->out_rep is resolved when we handle encap */
2804                         } else {
2805                                 NL_SET_ERR_MSG_MOD(extack,
2806                                                    "devices are not on same switch HW, can't offload forwarding");
2807                                 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
2808                                        priv->netdev->name, out_dev->name);
2809                                 return -EINVAL;
2810                         }
2811                         continue;
2812                 }
2813
2814                 if (is_tcf_tunnel_set(a)) {
2815                         info = tcf_tunnel_info(a);
2816                         if (info)
2817                                 encap = true;
2818                         else
2819                                 return -EOPNOTSUPP;
2820                         attr->mirror_count = attr->out_count;
2821                         continue;
2822                 }
2823
2824                 if (is_tcf_vlan(a)) {
2825                         err = parse_tc_vlan_action(priv, a, attr, &action);
2826
2827                         if (err)
2828                                 return err;
2829
2830             &