Merge tag 'kvm-s390-master-5.14-1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/kernel/linux-rpi.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/flow_offload.h>
35 #include <net/sch_generic.h>
36 #include <net/pkt_cls.h>
37 #include <net/tc_act/tc_gact.h>
38 #include <net/tc_act/tc_skbedit.h>
39 #include <linux/mlx5/fs.h>
40 #include <linux/mlx5/device.h>
41 #include <linux/rhashtable.h>
42 #include <linux/refcount.h>
43 #include <linux/completion.h>
44 #include <net/tc_act/tc_mirred.h>
45 #include <net/tc_act/tc_vlan.h>
46 #include <net/tc_act/tc_tunnel_key.h>
47 #include <net/tc_act/tc_pedit.h>
48 #include <net/tc_act/tc_csum.h>
49 #include <net/tc_act/tc_mpls.h>
50 #include <net/psample.h>
51 #include <net/arp.h>
52 #include <net/ipv6_stubs.h>
53 #include <net/bareudp.h>
54 #include <net/bonding.h>
55 #include "en.h"
56 #include "en_rep.h"
57 #include "en/rep/tc.h"
58 #include "en/rep/neigh.h"
59 #include "en_tc.h"
60 #include "eswitch.h"
61 #include "fs_core.h"
62 #include "en/port.h"
63 #include "en/tc_tun.h"
64 #include "en/mapping.h"
65 #include "en/tc_ct.h"
66 #include "en/mod_hdr.h"
67 #include "en/tc_priv.h"
68 #include "en/tc_tun_encap.h"
69 #include "esw/sample.h"
70 #include "lib/devcom.h"
71 #include "lib/geneve.h"
72 #include "lib/fs_chains.h"
73 #include "diag/en_tc_tracepoint.h"
74 #include <asm/div64.h>
75
76 #define nic_chains(priv) ((priv)->fs.tc.chains)
77 #define MLX5_MH_ACT_SZ MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto)
78
79 #define MLX5E_TC_TABLE_NUM_GROUPS 4
80 #define MLX5E_TC_TABLE_MAX_GROUP_SIZE BIT(18)
81
82 struct mlx5e_tc_attr_to_reg_mapping mlx5e_tc_attr_to_reg_mappings[] = {
83         [CHAIN_TO_REG] = {
84                 .mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_C_0,
85                 .moffset = 0,
86                 .mlen = 2,
87         },
88         [VPORT_TO_REG] = {
89                 .mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_C_0,
90                 .moffset = 2,
91                 .mlen = 2,
92         },
93         [TUNNEL_TO_REG] = {
94                 .mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_C_1,
95                 .moffset = 1,
96                 .mlen = ((ESW_TUN_OPTS_BITS + ESW_TUN_ID_BITS) / 8),
97                 .soffset = MLX5_BYTE_OFF(fte_match_param,
98                                          misc_parameters_2.metadata_reg_c_1),
99         },
100         [ZONE_TO_REG] = zone_to_reg_ct,
101         [ZONE_RESTORE_TO_REG] = zone_restore_to_reg_ct,
102         [CTSTATE_TO_REG] = ctstate_to_reg_ct,
103         [MARK_TO_REG] = mark_to_reg_ct,
104         [LABELS_TO_REG] = labels_to_reg_ct,
105         [FTEID_TO_REG] = fteid_to_reg_ct,
106         /* For NIC rules we store the retore metadata directly
107          * into reg_b that is passed to SW since we don't
108          * jump between steering domains.
109          */
110         [NIC_CHAIN_TO_REG] = {
111                 .mfield = MLX5_ACTION_IN_FIELD_METADATA_REG_B,
112                 .moffset = 0,
113                 .mlen = 2,
114         },
115         [NIC_ZONE_RESTORE_TO_REG] = nic_zone_restore_to_reg_ct,
116 };
117
118 /* To avoid false lock dependency warning set the tc_ht lock
119  * class different than the lock class of the ht being used when deleting
120  * last flow from a group and then deleting a group, we get into del_sw_flow_group()
121  * which call rhashtable_destroy on fg->ftes_hash which will take ht->mutex but
122  * it's different than the ht->mutex here.
123  */
124 static struct lock_class_key tc_ht_lock_key;
125
126 static void mlx5e_put_flow_tunnel_id(struct mlx5e_tc_flow *flow);
127
128 void
129 mlx5e_tc_match_to_reg_match(struct mlx5_flow_spec *spec,
130                             enum mlx5e_tc_attr_to_reg type,
131                             u32 data,
132                             u32 mask)
133 {
134         int soffset = mlx5e_tc_attr_to_reg_mappings[type].soffset;
135         int match_len = mlx5e_tc_attr_to_reg_mappings[type].mlen;
136         void *headers_c = spec->match_criteria;
137         void *headers_v = spec->match_value;
138         void *fmask, *fval;
139
140         fmask = headers_c + soffset;
141         fval = headers_v + soffset;
142
143         mask = (__force u32)(cpu_to_be32(mask)) >> (32 - (match_len * 8));
144         data = (__force u32)(cpu_to_be32(data)) >> (32 - (match_len * 8));
145
146         memcpy(fmask, &mask, match_len);
147         memcpy(fval, &data, match_len);
148
149         spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS_2;
150 }
151
152 void
153 mlx5e_tc_match_to_reg_get_match(struct mlx5_flow_spec *spec,
154                                 enum mlx5e_tc_attr_to_reg type,
155                                 u32 *data,
156                                 u32 *mask)
157 {
158         int soffset = mlx5e_tc_attr_to_reg_mappings[type].soffset;
159         int match_len = mlx5e_tc_attr_to_reg_mappings[type].mlen;
160         void *headers_c = spec->match_criteria;
161         void *headers_v = spec->match_value;
162         void *fmask, *fval;
163
164         fmask = headers_c + soffset;
165         fval = headers_v + soffset;
166
167         memcpy(mask, fmask, match_len);
168         memcpy(data, fval, match_len);
169
170         *mask = be32_to_cpu((__force __be32)(*mask << (32 - (match_len * 8))));
171         *data = be32_to_cpu((__force __be32)(*data << (32 - (match_len * 8))));
172 }
173
174 int
175 mlx5e_tc_match_to_reg_set_and_get_id(struct mlx5_core_dev *mdev,
176                                      struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts,
177                                      enum mlx5_flow_namespace_type ns,
178                                      enum mlx5e_tc_attr_to_reg type,
179                                      u32 data)
180 {
181         int moffset = mlx5e_tc_attr_to_reg_mappings[type].moffset;
182         int mfield = mlx5e_tc_attr_to_reg_mappings[type].mfield;
183         int mlen = mlx5e_tc_attr_to_reg_mappings[type].mlen;
184         char *modact;
185         int err;
186
187         err = alloc_mod_hdr_actions(mdev, ns, mod_hdr_acts);
188         if (err)
189                 return err;
190
191         modact = mod_hdr_acts->actions +
192                  (mod_hdr_acts->num_actions * MLX5_MH_ACT_SZ);
193
194         /* Firmware has 5bit length field and 0 means 32bits */
195         if (mlen == 4)
196                 mlen = 0;
197
198         MLX5_SET(set_action_in, modact, action_type, MLX5_ACTION_TYPE_SET);
199         MLX5_SET(set_action_in, modact, field, mfield);
200         MLX5_SET(set_action_in, modact, offset, moffset * 8);
201         MLX5_SET(set_action_in, modact, length, mlen * 8);
202         MLX5_SET(set_action_in, modact, data, data);
203         err = mod_hdr_acts->num_actions;
204         mod_hdr_acts->num_actions++;
205
206         return err;
207 }
208
209 static struct mlx5_tc_ct_priv *
210 get_ct_priv(struct mlx5e_priv *priv)
211 {
212         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
213         struct mlx5_rep_uplink_priv *uplink_priv;
214         struct mlx5e_rep_priv *uplink_rpriv;
215
216         if (is_mdev_switchdev_mode(priv->mdev)) {
217                 uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
218                 uplink_priv = &uplink_rpriv->uplink_priv;
219
220                 return uplink_priv->ct_priv;
221         }
222
223         return priv->fs.tc.ct;
224 }
225
226 #if IS_ENABLED(CONFIG_MLX5_TC_SAMPLE)
227 static struct mlx5_esw_psample *
228 get_sample_priv(struct mlx5e_priv *priv)
229 {
230         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
231         struct mlx5_rep_uplink_priv *uplink_priv;
232         struct mlx5e_rep_priv *uplink_rpriv;
233
234         if (is_mdev_switchdev_mode(priv->mdev)) {
235                 uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
236                 uplink_priv = &uplink_rpriv->uplink_priv;
237
238                 return uplink_priv->esw_psample;
239         }
240
241         return NULL;
242 }
243 #endif
244
245 struct mlx5_flow_handle *
246 mlx5_tc_rule_insert(struct mlx5e_priv *priv,
247                     struct mlx5_flow_spec *spec,
248                     struct mlx5_flow_attr *attr)
249 {
250         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
251
252         if (is_mdev_switchdev_mode(priv->mdev))
253                 return mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
254
255         return  mlx5e_add_offloaded_nic_rule(priv, spec, attr);
256 }
257
258 void
259 mlx5_tc_rule_delete(struct mlx5e_priv *priv,
260                     struct mlx5_flow_handle *rule,
261                     struct mlx5_flow_attr *attr)
262 {
263         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
264
265         if (is_mdev_switchdev_mode(priv->mdev)) {
266                 mlx5_eswitch_del_offloaded_rule(esw, rule, attr);
267
268                 return;
269         }
270
271         mlx5e_del_offloaded_nic_rule(priv, rule, attr);
272 }
273
274 int
275 mlx5e_tc_match_to_reg_set(struct mlx5_core_dev *mdev,
276                           struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts,
277                           enum mlx5_flow_namespace_type ns,
278                           enum mlx5e_tc_attr_to_reg type,
279                           u32 data)
280 {
281         int ret = mlx5e_tc_match_to_reg_set_and_get_id(mdev, mod_hdr_acts, ns, type, data);
282
283         return ret < 0 ? ret : 0;
284 }
285
286 void mlx5e_tc_match_to_reg_mod_hdr_change(struct mlx5_core_dev *mdev,
287                                           struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts,
288                                           enum mlx5e_tc_attr_to_reg type,
289                                           int act_id, u32 data)
290 {
291         int moffset = mlx5e_tc_attr_to_reg_mappings[type].moffset;
292         int mfield = mlx5e_tc_attr_to_reg_mappings[type].mfield;
293         int mlen = mlx5e_tc_attr_to_reg_mappings[type].mlen;
294         char *modact;
295
296         modact = mod_hdr_acts->actions + (act_id * MLX5_MH_ACT_SZ);
297
298         /* Firmware has 5bit length field and 0 means 32bits */
299         if (mlen == 4)
300                 mlen = 0;
301
302         MLX5_SET(set_action_in, modact, action_type, MLX5_ACTION_TYPE_SET);
303         MLX5_SET(set_action_in, modact, field, mfield);
304         MLX5_SET(set_action_in, modact, offset, moffset * 8);
305         MLX5_SET(set_action_in, modact, length, mlen * 8);
306         MLX5_SET(set_action_in, modact, data, data);
307 }
308
309 struct mlx5e_hairpin {
310         struct mlx5_hairpin *pair;
311
312         struct mlx5_core_dev *func_mdev;
313         struct mlx5e_priv *func_priv;
314         u32 tdn;
315         u32 tirn;
316
317         int num_channels;
318         struct mlx5e_rqt indir_rqt;
319         u32 indir_tirn[MLX5E_NUM_INDIR_TIRS];
320         struct mlx5e_ttc_table ttc;
321 };
322
323 struct mlx5e_hairpin_entry {
324         /* a node of a hash table which keeps all the  hairpin entries */
325         struct hlist_node hairpin_hlist;
326
327         /* protects flows list */
328         spinlock_t flows_lock;
329         /* flows sharing the same hairpin */
330         struct list_head flows;
331         /* hpe's that were not fully initialized when dead peer update event
332          * function traversed them.
333          */
334         struct list_head dead_peer_wait_list;
335
336         u16 peer_vhca_id;
337         u8 prio;
338         struct mlx5e_hairpin *hp;
339         refcount_t refcnt;
340         struct completion res_ready;
341 };
342
343 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
344                               struct mlx5e_tc_flow *flow);
345
346 struct mlx5e_tc_flow *mlx5e_flow_get(struct mlx5e_tc_flow *flow)
347 {
348         if (!flow || !refcount_inc_not_zero(&flow->refcnt))
349                 return ERR_PTR(-EINVAL);
350         return flow;
351 }
352
353 void mlx5e_flow_put(struct mlx5e_priv *priv, struct mlx5e_tc_flow *flow)
354 {
355         if (refcount_dec_and_test(&flow->refcnt)) {
356                 mlx5e_tc_del_flow(priv, flow);
357                 kfree_rcu(flow, rcu_head);
358         }
359 }
360
361 bool mlx5e_is_eswitch_flow(struct mlx5e_tc_flow *flow)
362 {
363         return flow_flag_test(flow, ESWITCH);
364 }
365
366 static bool mlx5e_is_ft_flow(struct mlx5e_tc_flow *flow)
367 {
368         return flow_flag_test(flow, FT);
369 }
370
371 bool mlx5e_is_offloaded_flow(struct mlx5e_tc_flow *flow)
372 {
373         return flow_flag_test(flow, OFFLOADED);
374 }
375
376 static int get_flow_name_space(struct mlx5e_tc_flow *flow)
377 {
378         return mlx5e_is_eswitch_flow(flow) ?
379                 MLX5_FLOW_NAMESPACE_FDB : MLX5_FLOW_NAMESPACE_KERNEL;
380 }
381
382 static struct mod_hdr_tbl *
383 get_mod_hdr_table(struct mlx5e_priv *priv, struct mlx5e_tc_flow *flow)
384 {
385         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
386
387         return get_flow_name_space(flow) == MLX5_FLOW_NAMESPACE_FDB ?
388                 &esw->offloads.mod_hdr :
389                 &priv->fs.tc.mod_hdr;
390 }
391
392 static int mlx5e_attach_mod_hdr(struct mlx5e_priv *priv,
393                                 struct mlx5e_tc_flow *flow,
394                                 struct mlx5e_tc_flow_parse_attr *parse_attr)
395 {
396         struct mlx5_modify_hdr *modify_hdr;
397         struct mlx5e_mod_hdr_handle *mh;
398
399         mh = mlx5e_mod_hdr_attach(priv->mdev, get_mod_hdr_table(priv, flow),
400                                   get_flow_name_space(flow),
401                                   &parse_attr->mod_hdr_acts);
402         if (IS_ERR(mh))
403                 return PTR_ERR(mh);
404
405         modify_hdr = mlx5e_mod_hdr_get(mh);
406         flow->attr->modify_hdr = modify_hdr;
407         flow->mh = mh;
408
409         return 0;
410 }
411
412 static void mlx5e_detach_mod_hdr(struct mlx5e_priv *priv,
413                                  struct mlx5e_tc_flow *flow)
414 {
415         /* flow wasn't fully initialized */
416         if (!flow->mh)
417                 return;
418
419         mlx5e_mod_hdr_detach(priv->mdev, get_mod_hdr_table(priv, flow),
420                              flow->mh);
421         flow->mh = NULL;
422 }
423
424 static
425 struct mlx5_core_dev *mlx5e_hairpin_get_mdev(struct net *net, int ifindex)
426 {
427         struct net_device *netdev;
428         struct mlx5e_priv *priv;
429
430         netdev = __dev_get_by_index(net, ifindex);
431         priv = netdev_priv(netdev);
432         return priv->mdev;
433 }
434
435 static int mlx5e_hairpin_create_transport(struct mlx5e_hairpin *hp)
436 {
437         u32 in[MLX5_ST_SZ_DW(create_tir_in)] = {};
438         void *tirc;
439         int err;
440
441         err = mlx5_core_alloc_transport_domain(hp->func_mdev, &hp->tdn);
442         if (err)
443                 goto alloc_tdn_err;
444
445         tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
446
447         MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
448         MLX5_SET(tirc, tirc, inline_rqn, hp->pair->rqn[0]);
449         MLX5_SET(tirc, tirc, transport_domain, hp->tdn);
450
451         err = mlx5_core_create_tir(hp->func_mdev, in, &hp->tirn);
452         if (err)
453                 goto create_tir_err;
454
455         return 0;
456
457 create_tir_err:
458         mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
459 alloc_tdn_err:
460         return err;
461 }
462
463 static void mlx5e_hairpin_destroy_transport(struct mlx5e_hairpin *hp)
464 {
465         mlx5_core_destroy_tir(hp->func_mdev, hp->tirn);
466         mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
467 }
468
469 static int mlx5e_hairpin_fill_rqt_rqns(struct mlx5e_hairpin *hp, void *rqtc)
470 {
471         struct mlx5e_priv *priv = hp->func_priv;
472         int i, ix, sz = MLX5E_INDIR_RQT_SIZE;
473         u32 *indirection_rqt, rqn;
474
475         indirection_rqt = kcalloc(sz, sizeof(*indirection_rqt), GFP_KERNEL);
476         if (!indirection_rqt)
477                 return -ENOMEM;
478
479         mlx5e_build_default_indir_rqt(indirection_rqt, sz,
480                                       hp->num_channels);
481
482         for (i = 0; i < sz; i++) {
483                 ix = i;
484                 if (priv->rss_params.hfunc == ETH_RSS_HASH_XOR)
485                         ix = mlx5e_bits_invert(i, ilog2(sz));
486                 ix = indirection_rqt[ix];
487                 rqn = hp->pair->rqn[ix];
488                 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
489         }
490
491         kfree(indirection_rqt);
492         return 0;
493 }
494
495 static int mlx5e_hairpin_create_indirect_rqt(struct mlx5e_hairpin *hp)
496 {
497         int inlen, err, sz = MLX5E_INDIR_RQT_SIZE;
498         struct mlx5e_priv *priv = hp->func_priv;
499         struct mlx5_core_dev *mdev = priv->mdev;
500         void *rqtc;
501         u32 *in;
502
503         inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
504         in = kvzalloc(inlen, GFP_KERNEL);
505         if (!in)
506                 return -ENOMEM;
507
508         rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
509
510         MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
511         MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
512
513         err = mlx5e_hairpin_fill_rqt_rqns(hp, rqtc);
514         if (err)
515                 goto out;
516
517         err = mlx5_core_create_rqt(mdev, in, inlen, &hp->indir_rqt.rqtn);
518         if (!err)
519                 hp->indir_rqt.enabled = true;
520
521 out:
522         kvfree(in);
523         return err;
524 }
525
526 static int mlx5e_hairpin_create_indirect_tirs(struct mlx5e_hairpin *hp)
527 {
528         struct mlx5e_priv *priv = hp->func_priv;
529         u32 in[MLX5_ST_SZ_DW(create_tir_in)];
530         int tt, i, err;
531         void *tirc;
532
533         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
534                 struct mlx5e_tirc_config ttconfig = mlx5e_tirc_get_default_config(tt);
535
536                 memset(in, 0, MLX5_ST_SZ_BYTES(create_tir_in));
537                 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
538
539                 MLX5_SET(tirc, tirc, transport_domain, hp->tdn);
540                 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
541                 MLX5_SET(tirc, tirc, indirect_table, hp->indir_rqt.rqtn);
542                 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params, &ttconfig, tirc, false);
543
544                 err = mlx5_core_create_tir(hp->func_mdev, in,
545                                            &hp->indir_tirn[tt]);
546                 if (err) {
547                         mlx5_core_warn(hp->func_mdev, "create indirect tirs failed, %d\n", err);
548                         goto err_destroy_tirs;
549                 }
550         }
551         return 0;
552
553 err_destroy_tirs:
554         for (i = 0; i < tt; i++)
555                 mlx5_core_destroy_tir(hp->func_mdev, hp->indir_tirn[i]);
556         return err;
557 }
558
559 static void mlx5e_hairpin_destroy_indirect_tirs(struct mlx5e_hairpin *hp)
560 {
561         int tt;
562
563         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
564                 mlx5_core_destroy_tir(hp->func_mdev, hp->indir_tirn[tt]);
565 }
566
567 static void mlx5e_hairpin_set_ttc_params(struct mlx5e_hairpin *hp,
568                                          struct ttc_params *ttc_params)
569 {
570         struct mlx5_flow_table_attr *ft_attr = &ttc_params->ft_attr;
571         int tt;
572
573         memset(ttc_params, 0, sizeof(*ttc_params));
574
575         ttc_params->any_tt_tirn = hp->tirn;
576
577         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
578                 ttc_params->indir_tirn[tt] = hp->indir_tirn[tt];
579
580         ft_attr->max_fte = MLX5E_TTC_TABLE_SIZE;
581         ft_attr->level = MLX5E_TC_TTC_FT_LEVEL;
582         ft_attr->prio = MLX5E_TC_PRIO;
583 }
584
585 static int mlx5e_hairpin_rss_init(struct mlx5e_hairpin *hp)
586 {
587         struct mlx5e_priv *priv = hp->func_priv;
588         struct ttc_params ttc_params;
589         int err;
590
591         err = mlx5e_hairpin_create_indirect_rqt(hp);
592         if (err)
593                 return err;
594
595         err = mlx5e_hairpin_create_indirect_tirs(hp);
596         if (err)
597                 goto err_create_indirect_tirs;
598
599         mlx5e_hairpin_set_ttc_params(hp, &ttc_params);
600         err = mlx5e_create_ttc_table(priv, &ttc_params, &hp->ttc);
601         if (err)
602                 goto err_create_ttc_table;
603
604         netdev_dbg(priv->netdev, "add hairpin: using %d channels rss ttc table id %x\n",
605                    hp->num_channels, hp->ttc.ft.t->id);
606
607         return 0;
608
609 err_create_ttc_table:
610         mlx5e_hairpin_destroy_indirect_tirs(hp);
611 err_create_indirect_tirs:
612         mlx5e_destroy_rqt(priv, &hp->indir_rqt);
613
614         return err;
615 }
616
617 static void mlx5e_hairpin_rss_cleanup(struct mlx5e_hairpin *hp)
618 {
619         struct mlx5e_priv *priv = hp->func_priv;
620
621         mlx5e_destroy_ttc_table(priv, &hp->ttc);
622         mlx5e_hairpin_destroy_indirect_tirs(hp);
623         mlx5e_destroy_rqt(priv, &hp->indir_rqt);
624 }
625
626 static struct mlx5e_hairpin *
627 mlx5e_hairpin_create(struct mlx5e_priv *priv, struct mlx5_hairpin_params *params,
628                      int peer_ifindex)
629 {
630         struct mlx5_core_dev *func_mdev, *peer_mdev;
631         struct mlx5e_hairpin *hp;
632         struct mlx5_hairpin *pair;
633         int err;
634
635         hp = kzalloc(sizeof(*hp), GFP_KERNEL);
636         if (!hp)
637                 return ERR_PTR(-ENOMEM);
638
639         func_mdev = priv->mdev;
640         peer_mdev = mlx5e_hairpin_get_mdev(dev_net(priv->netdev), peer_ifindex);
641
642         pair = mlx5_core_hairpin_create(func_mdev, peer_mdev, params);
643         if (IS_ERR(pair)) {
644                 err = PTR_ERR(pair);
645                 goto create_pair_err;
646         }
647         hp->pair = pair;
648         hp->func_mdev = func_mdev;
649         hp->func_priv = priv;
650         hp->num_channels = params->num_channels;
651
652         err = mlx5e_hairpin_create_transport(hp);
653         if (err)
654                 goto create_transport_err;
655
656         if (hp->num_channels > 1) {
657                 err = mlx5e_hairpin_rss_init(hp);
658                 if (err)
659                         goto rss_init_err;
660         }
661
662         return hp;
663
664 rss_init_err:
665         mlx5e_hairpin_destroy_transport(hp);
666 create_transport_err:
667         mlx5_core_hairpin_destroy(hp->pair);
668 create_pair_err:
669         kfree(hp);
670         return ERR_PTR(err);
671 }
672
673 static void mlx5e_hairpin_destroy(struct mlx5e_hairpin *hp)
674 {
675         if (hp->num_channels > 1)
676                 mlx5e_hairpin_rss_cleanup(hp);
677         mlx5e_hairpin_destroy_transport(hp);
678         mlx5_core_hairpin_destroy(hp->pair);
679         kvfree(hp);
680 }
681
682 static inline u32 hash_hairpin_info(u16 peer_vhca_id, u8 prio)
683 {
684         return (peer_vhca_id << 16 | prio);
685 }
686
687 static struct mlx5e_hairpin_entry *mlx5e_hairpin_get(struct mlx5e_priv *priv,
688                                                      u16 peer_vhca_id, u8 prio)
689 {
690         struct mlx5e_hairpin_entry *hpe;
691         u32 hash_key = hash_hairpin_info(peer_vhca_id, prio);
692
693         hash_for_each_possible(priv->fs.tc.hairpin_tbl, hpe,
694                                hairpin_hlist, hash_key) {
695                 if (hpe->peer_vhca_id == peer_vhca_id && hpe->prio == prio) {
696                         refcount_inc(&hpe->refcnt);
697                         return hpe;
698                 }
699         }
700
701         return NULL;
702 }
703
704 static void mlx5e_hairpin_put(struct mlx5e_priv *priv,
705                               struct mlx5e_hairpin_entry *hpe)
706 {
707         /* no more hairpin flows for us, release the hairpin pair */
708         if (!refcount_dec_and_mutex_lock(&hpe->refcnt, &priv->fs.tc.hairpin_tbl_lock))
709                 return;
710         hash_del(&hpe->hairpin_hlist);
711         mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
712
713         if (!IS_ERR_OR_NULL(hpe->hp)) {
714                 netdev_dbg(priv->netdev, "del hairpin: peer %s\n",
715                            dev_name(hpe->hp->pair->peer_mdev->device));
716
717                 mlx5e_hairpin_destroy(hpe->hp);
718         }
719
720         WARN_ON(!list_empty(&hpe->flows));
721         kfree(hpe);
722 }
723
724 #define UNKNOWN_MATCH_PRIO 8
725
726 static int mlx5e_hairpin_get_prio(struct mlx5e_priv *priv,
727                                   struct mlx5_flow_spec *spec, u8 *match_prio,
728                                   struct netlink_ext_ack *extack)
729 {
730         void *headers_c, *headers_v;
731         u8 prio_val, prio_mask = 0;
732         bool vlan_present;
733
734 #ifdef CONFIG_MLX5_CORE_EN_DCB
735         if (priv->dcbx_dp.trust_state != MLX5_QPTS_TRUST_PCP) {
736                 NL_SET_ERR_MSG_MOD(extack,
737                                    "only PCP trust state supported for hairpin");
738                 return -EOPNOTSUPP;
739         }
740 #endif
741         headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, outer_headers);
742         headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
743
744         vlan_present = MLX5_GET(fte_match_set_lyr_2_4, headers_v, cvlan_tag);
745         if (vlan_present) {
746                 prio_mask = MLX5_GET(fte_match_set_lyr_2_4, headers_c, first_prio);
747                 prio_val = MLX5_GET(fte_match_set_lyr_2_4, headers_v, first_prio);
748         }
749
750         if (!vlan_present || !prio_mask) {
751                 prio_val = UNKNOWN_MATCH_PRIO;
752         } else if (prio_mask != 0x7) {
753                 NL_SET_ERR_MSG_MOD(extack,
754                                    "masked priority match not supported for hairpin");
755                 return -EOPNOTSUPP;
756         }
757
758         *match_prio = prio_val;
759         return 0;
760 }
761
762 static int mlx5e_hairpin_flow_add(struct mlx5e_priv *priv,
763                                   struct mlx5e_tc_flow *flow,
764                                   struct mlx5e_tc_flow_parse_attr *parse_attr,
765                                   struct netlink_ext_ack *extack)
766 {
767         int peer_ifindex = parse_attr->mirred_ifindex[0];
768         struct mlx5_hairpin_params params;
769         struct mlx5_core_dev *peer_mdev;
770         struct mlx5e_hairpin_entry *hpe;
771         struct mlx5e_hairpin *hp;
772         u64 link_speed64;
773         u32 link_speed;
774         u8 match_prio;
775         u16 peer_id;
776         int err;
777
778         peer_mdev = mlx5e_hairpin_get_mdev(dev_net(priv->netdev), peer_ifindex);
779         if (!MLX5_CAP_GEN(priv->mdev, hairpin) || !MLX5_CAP_GEN(peer_mdev, hairpin)) {
780                 NL_SET_ERR_MSG_MOD(extack, "hairpin is not supported");
781                 return -EOPNOTSUPP;
782         }
783
784         peer_id = MLX5_CAP_GEN(peer_mdev, vhca_id);
785         err = mlx5e_hairpin_get_prio(priv, &parse_attr->spec, &match_prio,
786                                      extack);
787         if (err)
788                 return err;
789
790         mutex_lock(&priv->fs.tc.hairpin_tbl_lock);
791         hpe = mlx5e_hairpin_get(priv, peer_id, match_prio);
792         if (hpe) {
793                 mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
794                 wait_for_completion(&hpe->res_ready);
795
796                 if (IS_ERR(hpe->hp)) {
797                         err = -EREMOTEIO;
798                         goto out_err;
799                 }
800                 goto attach_flow;
801         }
802
803         hpe = kzalloc(sizeof(*hpe), GFP_KERNEL);
804         if (!hpe) {
805                 mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
806                 return -ENOMEM;
807         }
808
809         spin_lock_init(&hpe->flows_lock);
810         INIT_LIST_HEAD(&hpe->flows);
811         INIT_LIST_HEAD(&hpe->dead_peer_wait_list);
812         hpe->peer_vhca_id = peer_id;
813         hpe->prio = match_prio;
814         refcount_set(&hpe->refcnt, 1);
815         init_completion(&hpe->res_ready);
816
817         hash_add(priv->fs.tc.hairpin_tbl, &hpe->hairpin_hlist,
818                  hash_hairpin_info(peer_id, match_prio));
819         mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
820
821         params.log_data_size = 15;
822         params.log_data_size = min_t(u8, params.log_data_size,
823                                      MLX5_CAP_GEN(priv->mdev, log_max_hairpin_wq_data_sz));
824         params.log_data_size = max_t(u8, params.log_data_size,
825                                      MLX5_CAP_GEN(priv->mdev, log_min_hairpin_wq_data_sz));
826
827         params.log_num_packets = params.log_data_size -
828                                  MLX5_MPWRQ_MIN_LOG_STRIDE_SZ(priv->mdev);
829         params.log_num_packets = min_t(u8, params.log_num_packets,
830                                        MLX5_CAP_GEN(priv->mdev, log_max_hairpin_num_packets));
831
832         params.q_counter = priv->q_counter;
833         /* set hairpin pair per each 50Gbs share of the link */
834         mlx5e_port_max_linkspeed(priv->mdev, &link_speed);
835         link_speed = max_t(u32, link_speed, 50000);
836         link_speed64 = link_speed;
837         do_div(link_speed64, 50000);
838         params.num_channels = link_speed64;
839
840         hp = mlx5e_hairpin_create(priv, &params, peer_ifindex);
841         hpe->hp = hp;
842         complete_all(&hpe->res_ready);
843         if (IS_ERR(hp)) {
844                 err = PTR_ERR(hp);
845                 goto out_err;
846         }
847
848         netdev_dbg(priv->netdev, "add hairpin: tirn %x rqn %x peer %s sqn %x prio %d (log) data %d packets %d\n",
849                    hp->tirn, hp->pair->rqn[0],
850                    dev_name(hp->pair->peer_mdev->device),
851                    hp->pair->sqn[0], match_prio, params.log_data_size, params.log_num_packets);
852
853 attach_flow:
854         if (hpe->hp->num_channels > 1) {
855                 flow_flag_set(flow, HAIRPIN_RSS);
856                 flow->attr->nic_attr->hairpin_ft = hpe->hp->ttc.ft.t;
857         } else {
858                 flow->attr->nic_attr->hairpin_tirn = hpe->hp->tirn;
859         }
860
861         flow->hpe = hpe;
862         spin_lock(&hpe->flows_lock);
863         list_add(&flow->hairpin, &hpe->flows);
864         spin_unlock(&hpe->flows_lock);
865
866         return 0;
867
868 out_err:
869         mlx5e_hairpin_put(priv, hpe);
870         return err;
871 }
872
873 static void mlx5e_hairpin_flow_del(struct mlx5e_priv *priv,
874                                    struct mlx5e_tc_flow *flow)
875 {
876         /* flow wasn't fully initialized */
877         if (!flow->hpe)
878                 return;
879
880         spin_lock(&flow->hpe->flows_lock);
881         list_del(&flow->hairpin);
882         spin_unlock(&flow->hpe->flows_lock);
883
884         mlx5e_hairpin_put(priv, flow->hpe);
885         flow->hpe = NULL;
886 }
887
888 struct mlx5_flow_handle *
889 mlx5e_add_offloaded_nic_rule(struct mlx5e_priv *priv,
890                              struct mlx5_flow_spec *spec,
891                              struct mlx5_flow_attr *attr)
892 {
893         struct mlx5_flow_context *flow_context = &spec->flow_context;
894         struct mlx5_fs_chains *nic_chains = nic_chains(priv);
895         struct mlx5_nic_flow_attr *nic_attr = attr->nic_attr;
896         struct mlx5e_tc_table *tc = &priv->fs.tc;
897         struct mlx5_flow_destination dest[2] = {};
898         struct mlx5_flow_act flow_act = {
899                 .action = attr->action,
900                 .flags    = FLOW_ACT_NO_APPEND,
901         };
902         struct mlx5_flow_handle *rule;
903         struct mlx5_flow_table *ft;
904         int dest_ix = 0;
905
906         flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
907         flow_context->flow_tag = nic_attr->flow_tag;
908
909         if (attr->dest_ft) {
910                 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
911                 dest[dest_ix].ft = attr->dest_ft;
912                 dest_ix++;
913         } else if (nic_attr->hairpin_ft) {
914                 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
915                 dest[dest_ix].ft = nic_attr->hairpin_ft;
916                 dest_ix++;
917         } else if (nic_attr->hairpin_tirn) {
918                 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_TIR;
919                 dest[dest_ix].tir_num = nic_attr->hairpin_tirn;
920                 dest_ix++;
921         } else if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
922                 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
923                 if (attr->dest_chain) {
924                         dest[dest_ix].ft = mlx5_chains_get_table(nic_chains,
925                                                                  attr->dest_chain, 1,
926                                                                  MLX5E_TC_FT_LEVEL);
927                         if (IS_ERR(dest[dest_ix].ft))
928                                 return ERR_CAST(dest[dest_ix].ft);
929                 } else {
930                         dest[dest_ix].ft = mlx5e_vlan_get_flowtable(priv->fs.vlan);
931                 }
932                 dest_ix++;
933         }
934
935         if (dest[0].type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
936             MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, ignore_flow_level))
937                 flow_act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
938
939         if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
940                 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
941                 dest[dest_ix].counter_id = mlx5_fc_id(attr->counter);
942                 dest_ix++;
943         }
944
945         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
946                 flow_act.modify_hdr = attr->modify_hdr;
947
948         mutex_lock(&tc->t_lock);
949         if (IS_ERR_OR_NULL(tc->t)) {
950                 /* Create the root table here if doesn't exist yet */
951                 tc->t =
952                         mlx5_chains_get_table(nic_chains, 0, 1, MLX5E_TC_FT_LEVEL);
953
954                 if (IS_ERR(tc->t)) {
955                         mutex_unlock(&tc->t_lock);
956                         netdev_err(priv->netdev,
957                                    "Failed to create tc offload table\n");
958                         rule = ERR_CAST(priv->fs.tc.t);
959                         goto err_ft_get;
960                 }
961         }
962         mutex_unlock(&tc->t_lock);
963
964         if (attr->chain || attr->prio)
965                 ft = mlx5_chains_get_table(nic_chains,
966                                            attr->chain, attr->prio,
967                                            MLX5E_TC_FT_LEVEL);
968         else
969                 ft = attr->ft;
970
971         if (IS_ERR(ft)) {
972                 rule = ERR_CAST(ft);
973                 goto err_ft_get;
974         }
975
976         if (attr->outer_match_level != MLX5_MATCH_NONE)
977                 spec->match_criteria_enable |= MLX5_MATCH_OUTER_HEADERS;
978
979         rule = mlx5_add_flow_rules(ft, spec,
980                                    &flow_act, dest, dest_ix);
981         if (IS_ERR(rule))
982                 goto err_rule;
983
984         return rule;
985
986 err_rule:
987         if (attr->chain || attr->prio)
988                 mlx5_chains_put_table(nic_chains,
989                                       attr->chain, attr->prio,
990                                       MLX5E_TC_FT_LEVEL);
991 err_ft_get:
992         if (attr->dest_chain)
993                 mlx5_chains_put_table(nic_chains,
994                                       attr->dest_chain, 1,
995                                       MLX5E_TC_FT_LEVEL);
996
997         return ERR_CAST(rule);
998 }
999
1000 static int
1001 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
1002                       struct mlx5e_tc_flow_parse_attr *parse_attr,
1003                       struct mlx5e_tc_flow *flow,
1004                       struct netlink_ext_ack *extack)
1005 {
1006         struct mlx5_flow_attr *attr = flow->attr;
1007         struct mlx5_core_dev *dev = priv->mdev;
1008         struct mlx5_fc *counter = NULL;
1009         int err;
1010
1011         if (flow_flag_test(flow, HAIRPIN)) {
1012                 err = mlx5e_hairpin_flow_add(priv, flow, parse_attr, extack);
1013                 if (err)
1014                         return err;
1015         }
1016
1017         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
1018                 counter = mlx5_fc_create(dev, true);
1019                 if (IS_ERR(counter))
1020                         return PTR_ERR(counter);
1021
1022                 attr->counter = counter;
1023         }
1024
1025         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
1026                 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
1027                 dealloc_mod_hdr_actions(&parse_attr->mod_hdr_acts);
1028                 if (err)
1029                         return err;
1030         }
1031
1032         if (flow_flag_test(flow, CT))
1033                 flow->rule[0] = mlx5_tc_ct_flow_offload(get_ct_priv(priv), flow, &parse_attr->spec,
1034                                                         attr, &parse_attr->mod_hdr_acts);
1035         else
1036                 flow->rule[0] = mlx5e_add_offloaded_nic_rule(priv, &parse_attr->spec,
1037                                                              attr);
1038
1039         return PTR_ERR_OR_ZERO(flow->rule[0]);
1040 }
1041
1042 void mlx5e_del_offloaded_nic_rule(struct mlx5e_priv *priv,
1043                                   struct mlx5_flow_handle *rule,
1044                                   struct mlx5_flow_attr *attr)
1045 {
1046         struct mlx5_fs_chains *nic_chains = nic_chains(priv);
1047
1048         mlx5_del_flow_rules(rule);
1049
1050         if (attr->chain || attr->prio)
1051                 mlx5_chains_put_table(nic_chains, attr->chain, attr->prio,
1052                                       MLX5E_TC_FT_LEVEL);
1053
1054         if (attr->dest_chain)
1055                 mlx5_chains_put_table(nic_chains, attr->dest_chain, 1,
1056                                       MLX5E_TC_FT_LEVEL);
1057 }
1058
1059 static void mlx5e_tc_del_nic_flow(struct mlx5e_priv *priv,
1060                                   struct mlx5e_tc_flow *flow)
1061 {
1062         struct mlx5_flow_attr *attr = flow->attr;
1063         struct mlx5e_tc_table *tc = &priv->fs.tc;
1064
1065         flow_flag_clear(flow, OFFLOADED);
1066
1067         if (flow_flag_test(flow, CT))
1068                 mlx5_tc_ct_delete_flow(get_ct_priv(flow->priv), flow, attr);
1069         else if (!IS_ERR_OR_NULL(flow->rule[0]))
1070                 mlx5e_del_offloaded_nic_rule(priv, flow->rule[0], attr);
1071
1072         /* Remove root table if no rules are left to avoid
1073          * extra steering hops.
1074          */
1075         mutex_lock(&priv->fs.tc.t_lock);
1076         if (!mlx5e_tc_num_filters(priv, MLX5_TC_FLAG(NIC_OFFLOAD)) &&
1077             !IS_ERR_OR_NULL(tc->t)) {
1078                 mlx5_chains_put_table(nic_chains(priv), 0, 1, MLX5E_TC_FT_LEVEL);
1079                 priv->fs.tc.t = NULL;
1080         }
1081         mutex_unlock(&priv->fs.tc.t_lock);
1082
1083         kvfree(attr->parse_attr);
1084
1085         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
1086                 mlx5e_detach_mod_hdr(priv, flow);
1087
1088         mlx5_fc_destroy(priv->mdev, attr->counter);
1089
1090         if (flow_flag_test(flow, HAIRPIN))
1091                 mlx5e_hairpin_flow_del(priv, flow);
1092
1093         kfree(flow->attr);
1094 }
1095
1096 struct mlx5_flow_handle *
1097 mlx5e_tc_offload_fdb_rules(struct mlx5_eswitch *esw,
1098                            struct mlx5e_tc_flow *flow,
1099                            struct mlx5_flow_spec *spec,
1100                            struct mlx5_flow_attr *attr)
1101 {
1102         struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts;
1103         struct mlx5_flow_handle *rule;
1104
1105         if (attr->flags & MLX5_ESW_ATTR_FLAG_SLOW_PATH)
1106                 return mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
1107
1108         if (flow_flag_test(flow, CT)) {
1109                 mod_hdr_acts = &attr->parse_attr->mod_hdr_acts;
1110
1111                 rule = mlx5_tc_ct_flow_offload(get_ct_priv(flow->priv),
1112                                                flow, spec, attr,
1113                                                mod_hdr_acts);
1114 #if IS_ENABLED(CONFIG_MLX5_TC_SAMPLE)
1115         } else if (flow_flag_test(flow, SAMPLE)) {
1116                 rule = mlx5_esw_sample_offload(get_sample_priv(flow->priv), spec, attr);
1117 #endif
1118         } else {
1119                 rule = mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
1120         }
1121
1122         if (IS_ERR(rule))
1123                 return rule;
1124
1125         if (attr->esw_attr->split_count) {
1126                 flow->rule[1] = mlx5_eswitch_add_fwd_rule(esw, spec, attr);
1127                 if (IS_ERR(flow->rule[1])) {
1128                         if (flow_flag_test(flow, CT))
1129                                 mlx5_tc_ct_delete_flow(get_ct_priv(flow->priv), flow, attr);
1130                         else
1131                                 mlx5_eswitch_del_offloaded_rule(esw, rule, attr);
1132                         return flow->rule[1];
1133                 }
1134         }
1135
1136         return rule;
1137 }
1138
1139 void mlx5e_tc_unoffload_fdb_rules(struct mlx5_eswitch *esw,
1140                                   struct mlx5e_tc_flow *flow,
1141                                   struct mlx5_flow_attr *attr)
1142 {
1143         flow_flag_clear(flow, OFFLOADED);
1144
1145         if (attr->flags & MLX5_ESW_ATTR_FLAG_SLOW_PATH)
1146                 goto offload_rule_0;
1147
1148         if (flow_flag_test(flow, CT)) {
1149                 mlx5_tc_ct_delete_flow(get_ct_priv(flow->priv), flow, attr);
1150                 return;
1151         }
1152
1153 #if IS_ENABLED(CONFIG_MLX5_TC_SAMPLE)
1154         if (flow_flag_test(flow, SAMPLE)) {
1155                 mlx5_esw_sample_unoffload(get_sample_priv(flow->priv), flow->rule[0], attr);
1156                 return;
1157         }
1158 #endif
1159
1160         if (attr->esw_attr->split_count)
1161                 mlx5_eswitch_del_fwd_rule(esw, flow->rule[1], attr);
1162
1163 offload_rule_0:
1164         mlx5_eswitch_del_offloaded_rule(esw, flow->rule[0], attr);
1165 }
1166
1167 struct mlx5_flow_handle *
1168 mlx5e_tc_offload_to_slow_path(struct mlx5_eswitch *esw,
1169                               struct mlx5e_tc_flow *flow,
1170                               struct mlx5_flow_spec *spec)
1171 {
1172         struct mlx5_flow_attr *slow_attr;
1173         struct mlx5_flow_handle *rule;
1174
1175         slow_attr = mlx5_alloc_flow_attr(MLX5_FLOW_NAMESPACE_FDB);
1176         if (!slow_attr)
1177                 return ERR_PTR(-ENOMEM);
1178
1179         memcpy(slow_attr, flow->attr, ESW_FLOW_ATTR_SZ);
1180         slow_attr->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1181         slow_attr->esw_attr->split_count = 0;
1182         slow_attr->flags |= MLX5_ESW_ATTR_FLAG_SLOW_PATH;
1183
1184         rule = mlx5e_tc_offload_fdb_rules(esw, flow, spec, slow_attr);
1185         if (!IS_ERR(rule))
1186                 flow_flag_set(flow, SLOW);
1187
1188         kfree(slow_attr);
1189
1190         return rule;
1191 }
1192
1193 void mlx5e_tc_unoffload_from_slow_path(struct mlx5_eswitch *esw,
1194                                        struct mlx5e_tc_flow *flow)
1195 {
1196         struct mlx5_flow_attr *slow_attr;
1197
1198         slow_attr = mlx5_alloc_flow_attr(MLX5_FLOW_NAMESPACE_FDB);
1199         if (!slow_attr) {
1200                 mlx5_core_warn(flow->priv->mdev, "Unable to alloc attr to unoffload slow path rule\n");
1201                 return;
1202         }
1203
1204         memcpy(slow_attr, flow->attr, ESW_FLOW_ATTR_SZ);
1205         slow_attr->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1206         slow_attr->esw_attr->split_count = 0;
1207         slow_attr->flags |= MLX5_ESW_ATTR_FLAG_SLOW_PATH;
1208         mlx5e_tc_unoffload_fdb_rules(esw, flow, slow_attr);
1209         flow_flag_clear(flow, SLOW);
1210         kfree(slow_attr);
1211 }
1212
1213 /* Caller must obtain uplink_priv->unready_flows_lock mutex before calling this
1214  * function.
1215  */
1216 static void unready_flow_add(struct mlx5e_tc_flow *flow,
1217                              struct list_head *unready_flows)
1218 {
1219         flow_flag_set(flow, NOT_READY);
1220         list_add_tail(&flow->unready, unready_flows);
1221 }
1222
1223 /* Caller must obtain uplink_priv->unready_flows_lock mutex before calling this
1224  * function.
1225  */
1226 static void unready_flow_del(struct mlx5e_tc_flow *flow)
1227 {
1228         list_del(&flow->unready);
1229         flow_flag_clear(flow, NOT_READY);
1230 }
1231
1232 static void add_unready_flow(struct mlx5e_tc_flow *flow)
1233 {
1234         struct mlx5_rep_uplink_priv *uplink_priv;
1235         struct mlx5e_rep_priv *rpriv;
1236         struct mlx5_eswitch *esw;
1237
1238         esw = flow->priv->mdev->priv.eswitch;
1239         rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
1240         uplink_priv = &rpriv->uplink_priv;
1241
1242         mutex_lock(&uplink_priv->unready_flows_lock);
1243         unready_flow_add(flow, &uplink_priv->unready_flows);
1244         mutex_unlock(&uplink_priv->unready_flows_lock);
1245 }
1246
1247 static void remove_unready_flow(struct mlx5e_tc_flow *flow)
1248 {
1249         struct mlx5_rep_uplink_priv *uplink_priv;
1250         struct mlx5e_rep_priv *rpriv;
1251         struct mlx5_eswitch *esw;
1252
1253         esw = flow->priv->mdev->priv.eswitch;
1254         rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
1255         uplink_priv = &rpriv->uplink_priv;
1256
1257         mutex_lock(&uplink_priv->unready_flows_lock);
1258         unready_flow_del(flow);
1259         mutex_unlock(&uplink_priv->unready_flows_lock);
1260 }
1261
1262 static bool same_hw_devs(struct mlx5e_priv *priv, struct mlx5e_priv *peer_priv);
1263
1264 bool mlx5e_tc_is_vf_tunnel(struct net_device *out_dev, struct net_device *route_dev)
1265 {
1266         struct mlx5_core_dev *out_mdev, *route_mdev;
1267         struct mlx5e_priv *out_priv, *route_priv;
1268
1269         out_priv = netdev_priv(out_dev);
1270         out_mdev = out_priv->mdev;
1271         route_priv = netdev_priv(route_dev);
1272         route_mdev = route_priv->mdev;
1273
1274         if (out_mdev->coredev_type != MLX5_COREDEV_PF ||
1275             route_mdev->coredev_type != MLX5_COREDEV_VF)
1276                 return false;
1277
1278         return same_hw_devs(out_priv, route_priv);
1279 }
1280
1281 int mlx5e_tc_query_route_vport(struct net_device *out_dev, struct net_device *route_dev, u16 *vport)
1282 {
1283         struct mlx5e_priv *out_priv, *route_priv;
1284         struct mlx5_core_dev *route_mdev;
1285         struct mlx5_eswitch *esw;
1286         u16 vhca_id;
1287         int err;
1288
1289         out_priv = netdev_priv(out_dev);
1290         esw = out_priv->mdev->priv.eswitch;
1291         route_priv = netdev_priv(route_dev);
1292         route_mdev = route_priv->mdev;
1293
1294         vhca_id = MLX5_CAP_GEN(route_mdev, vhca_id);
1295         err = mlx5_eswitch_vhca_id_to_vport(esw, vhca_id, vport);
1296         return err;
1297 }
1298
1299 int mlx5e_tc_add_flow_mod_hdr(struct mlx5e_priv *priv,
1300                               struct mlx5e_tc_flow_parse_attr *parse_attr,
1301                               struct mlx5e_tc_flow *flow)
1302 {
1303         struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts = &parse_attr->mod_hdr_acts;
1304         struct mlx5_modify_hdr *mod_hdr;
1305
1306         mod_hdr = mlx5_modify_header_alloc(priv->mdev,
1307                                            get_flow_name_space(flow),
1308                                            mod_hdr_acts->num_actions,
1309                                            mod_hdr_acts->actions);
1310         if (IS_ERR(mod_hdr))
1311                 return PTR_ERR(mod_hdr);
1312
1313         WARN_ON(flow->attr->modify_hdr);
1314         flow->attr->modify_hdr = mod_hdr;
1315
1316         return 0;
1317 }
1318
1319 static int
1320 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
1321                       struct mlx5e_tc_flow *flow,
1322                       struct netlink_ext_ack *extack)
1323 {
1324         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1325         struct mlx5e_tc_flow_parse_attr *parse_attr;
1326         struct mlx5_flow_attr *attr = flow->attr;
1327         bool vf_tun = false, encap_valid = true;
1328         struct net_device *encap_dev = NULL;
1329         struct mlx5_esw_flow_attr *esw_attr;
1330         struct mlx5_fc *counter = NULL;
1331         struct mlx5e_rep_priv *rpriv;
1332         struct mlx5e_priv *out_priv;
1333         u32 max_prio, max_chain;
1334         int err = 0;
1335         int out_index;
1336
1337         /* We check chain range only for tc flows.
1338          * For ft flows, we checked attr->chain was originally 0 and set it to
1339          * FDB_FT_CHAIN which is outside tc range.
1340          * See mlx5e_rep_setup_ft_cb().
1341          */
1342         max_chain = mlx5_chains_get_chain_range(esw_chains(esw));
1343         if (!mlx5e_is_ft_flow(flow) && attr->chain > max_chain) {
1344                 NL_SET_ERR_MSG_MOD(extack,
1345                                    "Requested chain is out of supported range");
1346                 err = -EOPNOTSUPP;
1347                 goto err_out;
1348         }
1349
1350         max_prio = mlx5_chains_get_prio_range(esw_chains(esw));
1351         if (attr->prio > max_prio) {
1352                 NL_SET_ERR_MSG_MOD(extack,
1353                                    "Requested priority is out of supported range");
1354                 err = -EOPNOTSUPP;
1355                 goto err_out;
1356         }
1357
1358         if (flow_flag_test(flow, TUN_RX)) {
1359                 err = mlx5e_attach_decap_route(priv, flow);
1360                 if (err)
1361                         goto err_out;
1362         }
1363
1364         if (flow_flag_test(flow, L3_TO_L2_DECAP)) {
1365                 err = mlx5e_attach_decap(priv, flow, extack);
1366                 if (err)
1367                         goto err_out;
1368         }
1369
1370         parse_attr = attr->parse_attr;
1371         esw_attr = attr->esw_attr;
1372
1373         for (out_index = 0; out_index < MLX5_MAX_FLOW_FWD_VPORTS; out_index++) {
1374                 struct net_device *out_dev;
1375                 int mirred_ifindex;
1376
1377                 if (!(esw_attr->dests[out_index].flags & MLX5_ESW_DEST_ENCAP))
1378                         continue;
1379
1380                 mirred_ifindex = parse_attr->mirred_ifindex[out_index];
1381                 out_dev = dev_get_by_index(dev_net(priv->netdev), mirred_ifindex);
1382                 if (!out_dev) {
1383                         NL_SET_ERR_MSG_MOD(extack, "Requested mirred device not found");
1384                         err = -ENODEV;
1385                         goto err_out;
1386                 }
1387                 err = mlx5e_attach_encap(priv, flow, out_dev, out_index,
1388                                          extack, &encap_dev, &encap_valid);
1389                 dev_put(out_dev);
1390                 if (err)
1391                         goto err_out;
1392
1393                 if (esw_attr->dests[out_index].flags &
1394                     MLX5_ESW_DEST_CHAIN_WITH_SRC_PORT_CHANGE)
1395                         vf_tun = true;
1396                 out_priv = netdev_priv(encap_dev);
1397                 rpriv = out_priv->ppriv;
1398                 esw_attr->dests[out_index].rep = rpriv->rep;
1399                 esw_attr->dests[out_index].mdev = out_priv->mdev;
1400         }
1401
1402         if (vf_tun && esw_attr->out_count > 1) {
1403                 NL_SET_ERR_MSG_MOD(extack, "VF tunnel encap with mirroring is not supported");
1404                 err = -EOPNOTSUPP;
1405                 goto err_out;
1406         }
1407
1408         err = mlx5_eswitch_add_vlan_action(esw, attr);
1409         if (err)
1410                 goto err_out;
1411
1412         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR &&
1413             !(attr->ct_attr.ct_action & TCA_CT_ACT_CLEAR)) {
1414                 if (vf_tun) {
1415                         err = mlx5e_tc_add_flow_mod_hdr(priv, parse_attr, flow);
1416                         if (err)
1417                                 goto err_out;
1418                 } else {
1419                         err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
1420                         if (err)
1421                                 goto err_out;
1422                 }
1423         }
1424
1425         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
1426                 counter = mlx5_fc_create(esw_attr->counter_dev, true);
1427                 if (IS_ERR(counter)) {
1428                         err = PTR_ERR(counter);
1429                         goto err_out;
1430                 }
1431
1432                 attr->counter = counter;
1433         }
1434
1435         /* we get here if one of the following takes place:
1436          * (1) there's no error
1437          * (2) there's an encap action and we don't have valid neigh
1438          */
1439         if (!encap_valid)
1440                 flow->rule[0] = mlx5e_tc_offload_to_slow_path(esw, flow, &parse_attr->spec);
1441         else
1442                 flow->rule[0] = mlx5e_tc_offload_fdb_rules(esw, flow, &parse_attr->spec, attr);
1443
1444         if (IS_ERR(flow->rule[0])) {
1445                 err = PTR_ERR(flow->rule[0]);
1446                 goto err_out;
1447         }
1448         flow_flag_set(flow, OFFLOADED);
1449
1450         return 0;
1451
1452 err_out:
1453         flow_flag_set(flow, FAILED);
1454         return err;
1455 }
1456
1457 static bool mlx5_flow_has_geneve_opt(struct mlx5e_tc_flow *flow)
1458 {
1459         struct mlx5_flow_spec *spec = &flow->attr->parse_attr->spec;
1460         void *headers_v = MLX5_ADDR_OF(fte_match_param,
1461                                        spec->match_value,
1462                                        misc_parameters_3);
1463         u32 geneve_tlv_opt_0_data = MLX5_GET(fte_match_set_misc3,
1464                                              headers_v,
1465                                              geneve_tlv_option_0_data);
1466
1467         return !!geneve_tlv_opt_0_data;
1468 }
1469
1470 static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
1471                                   struct mlx5e_tc_flow *flow)
1472 {
1473         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1474         struct mlx5_flow_attr *attr = flow->attr;
1475         struct mlx5_esw_flow_attr *esw_attr;
1476         bool vf_tun = false;
1477         int out_index;
1478
1479         esw_attr = attr->esw_attr;
1480         mlx5e_put_flow_tunnel_id(flow);
1481
1482         if (flow_flag_test(flow, NOT_READY))
1483                 remove_unready_flow(flow);
1484
1485         if (mlx5e_is_offloaded_flow(flow)) {
1486                 if (flow_flag_test(flow, SLOW))
1487                         mlx5e_tc_unoffload_from_slow_path(esw, flow);
1488                 else
1489                         mlx5e_tc_unoffload_fdb_rules(esw, flow, attr);
1490         }
1491
1492         if (mlx5_flow_has_geneve_opt(flow))
1493                 mlx5_geneve_tlv_option_del(priv->mdev->geneve);
1494
1495         mlx5_eswitch_del_vlan_action(esw, attr);
1496
1497         if (flow->decap_route)
1498                 mlx5e_detach_decap_route(priv, flow);
1499
1500         for (out_index = 0; out_index < MLX5_MAX_FLOW_FWD_VPORTS; out_index++) {
1501                 if (esw_attr->dests[out_index].flags &
1502                     MLX5_ESW_DEST_CHAIN_WITH_SRC_PORT_CHANGE)
1503                         vf_tun = true;
1504                 if (esw_attr->dests[out_index].flags & MLX5_ESW_DEST_ENCAP) {
1505                         mlx5e_detach_encap(priv, flow, out_index);
1506                         kfree(attr->parse_attr->tun_info[out_index]);
1507                 }
1508         }
1509
1510         mlx5_tc_ct_match_del(get_ct_priv(priv), &flow->attr->ct_attr);
1511
1512         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
1513                 dealloc_mod_hdr_actions(&attr->parse_attr->mod_hdr_acts);
1514                 if (vf_tun && attr->modify_hdr)
1515                         mlx5_modify_header_dealloc(priv->mdev, attr->modify_hdr);
1516                 else
1517                         mlx5e_detach_mod_hdr(priv, flow);
1518         }
1519         kvfree(attr->parse_attr);
1520         kvfree(attr->esw_attr->rx_tun_attr);
1521
1522         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
1523                 mlx5_fc_destroy(esw_attr->counter_dev, attr->counter);
1524
1525         if (flow_flag_test(flow, L3_TO_L2_DECAP))
1526                 mlx5e_detach_decap(priv, flow);
1527
1528         kfree(flow->attr->esw_attr->sample);
1529         kfree(flow->attr);
1530 }
1531
1532 struct mlx5_fc *mlx5e_tc_get_counter(struct mlx5e_tc_flow *flow)
1533 {
1534         return flow->attr->counter;
1535 }
1536
1537 /* Iterate over tmp_list of flows attached to flow_list head. */
1538 void mlx5e_put_flow_list(struct mlx5e_priv *priv, struct list_head *flow_list)
1539 {
1540         struct mlx5e_tc_flow *flow, *tmp;
1541
1542         list_for_each_entry_safe(flow, tmp, flow_list, tmp_list)
1543                 mlx5e_flow_put(priv, flow);
1544 }
1545
1546 static void __mlx5e_tc_del_fdb_peer_flow(struct mlx5e_tc_flow *flow)
1547 {
1548         struct mlx5_eswitch *esw = flow->priv->mdev->priv.eswitch;
1549
1550         if (!flow_flag_test(flow, ESWITCH) ||
1551             !flow_flag_test(flow, DUP))
1552                 return;
1553
1554         mutex_lock(&esw->offloads.peer_mutex);
1555         list_del(&flow->peer);
1556         mutex_unlock(&esw->offloads.peer_mutex);
1557
1558         flow_flag_clear(flow, DUP);
1559
1560         if (refcount_dec_and_test(&flow->peer_flow->refcnt)) {
1561                 mlx5e_tc_del_fdb_flow(flow->peer_flow->priv, flow->peer_flow);
1562                 kfree(flow->peer_flow);
1563         }
1564
1565         flow->peer_flow = NULL;
1566 }
1567
1568 static void mlx5e_tc_del_fdb_peer_flow(struct mlx5e_tc_flow *flow)
1569 {
1570         struct mlx5_core_dev *dev = flow->priv->mdev;
1571         struct mlx5_devcom *devcom = dev->priv.devcom;
1572         struct mlx5_eswitch *peer_esw;
1573
1574         peer_esw = mlx5_devcom_get_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
1575         if (!peer_esw)
1576                 return;
1577
1578         __mlx5e_tc_del_fdb_peer_flow(flow);
1579         mlx5_devcom_release_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
1580 }
1581
1582 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
1583                               struct mlx5e_tc_flow *flow)
1584 {
1585         if (mlx5e_is_eswitch_flow(flow)) {
1586                 mlx5e_tc_del_fdb_peer_flow(flow);
1587                 mlx5e_tc_del_fdb_flow(priv, flow);
1588         } else {
1589                 mlx5e_tc_del_nic_flow(priv, flow);
1590         }
1591 }
1592
1593 static int flow_has_tc_fwd_action(struct flow_cls_offload *f)
1594 {
1595         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1596         struct flow_action *flow_action = &rule->action;
1597         const struct flow_action_entry *act;
1598         int i;
1599
1600         flow_action_for_each(i, act, flow_action) {
1601                 switch (act->id) {
1602                 case FLOW_ACTION_GOTO:
1603                         return true;
1604                 default:
1605                         continue;
1606                 }
1607         }
1608
1609         return false;
1610 }
1611
1612 static int
1613 enc_opts_is_dont_care_or_full_match(struct mlx5e_priv *priv,
1614                                     struct flow_dissector_key_enc_opts *opts,
1615                                     struct netlink_ext_ack *extack,
1616                                     bool *dont_care)
1617 {
1618         struct geneve_opt *opt;
1619         int off = 0;
1620
1621         *dont_care = true;
1622
1623         while (opts->len > off) {
1624                 opt = (struct geneve_opt *)&opts->data[off];
1625
1626                 if (!(*dont_care) || opt->opt_class || opt->type ||
1627                     memchr_inv(opt->opt_data, 0, opt->length * 4)) {
1628                         *dont_care = false;
1629
1630                         if (opt->opt_class != htons(U16_MAX) ||
1631                             opt->type != U8_MAX) {
1632                                 NL_SET_ERR_MSG(extack,
1633                                                "Partial match of tunnel options in chain > 0 isn't supported");
1634                                 netdev_warn(priv->netdev,
1635                                             "Partial match of tunnel options in chain > 0 isn't supported");
1636                                 return -EOPNOTSUPP;
1637                         }
1638                 }
1639
1640                 off += sizeof(struct geneve_opt) + opt->length * 4;
1641         }
1642
1643         return 0;
1644 }
1645
1646 #define COPY_DISSECTOR(rule, diss_key, dst)\
1647 ({ \
1648         struct flow_rule *__rule = (rule);\
1649         typeof(dst) __dst = dst;\
1650 \
1651         memcpy(__dst,\
1652                skb_flow_dissector_target(__rule->match.dissector,\
1653                                          diss_key,\
1654                                          __rule->match.key),\
1655                sizeof(*__dst));\
1656 })
1657
1658 static int mlx5e_get_flow_tunnel_id(struct mlx5e_priv *priv,
1659                                     struct mlx5e_tc_flow *flow,
1660                                     struct flow_cls_offload *f,
1661                                     struct net_device *filter_dev)
1662 {
1663         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1664         struct netlink_ext_ack *extack = f->common.extack;
1665         struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts;
1666         struct flow_match_enc_opts enc_opts_match;
1667         struct tunnel_match_enc_opts tun_enc_opts;
1668         struct mlx5_rep_uplink_priv *uplink_priv;
1669         struct mlx5_flow_attr *attr = flow->attr;
1670         struct mlx5e_rep_priv *uplink_rpriv;
1671         struct tunnel_match_key tunnel_key;
1672         bool enc_opts_is_dont_care = true;
1673         u32 tun_id, enc_opts_id = 0;
1674         struct mlx5_eswitch *esw;
1675         u32 value, mask;
1676         int err;
1677
1678         esw = priv->mdev->priv.eswitch;
1679         uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
1680         uplink_priv = &uplink_rpriv->uplink_priv;
1681
1682         memset(&tunnel_key, 0, sizeof(tunnel_key));
1683         COPY_DISSECTOR(rule, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1684                        &tunnel_key.enc_control);
1685         if (tunnel_key.enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS)
1686                 COPY_DISSECTOR(rule, FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
1687                                &tunnel_key.enc_ipv4);
1688         else
1689                 COPY_DISSECTOR(rule, FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
1690                                &tunnel_key.enc_ipv6);
1691         COPY_DISSECTOR(rule, FLOW_DISSECTOR_KEY_ENC_IP, &tunnel_key.enc_ip);
1692         COPY_DISSECTOR(rule, FLOW_DISSECTOR_KEY_ENC_PORTS,
1693                        &tunnel_key.enc_tp);
1694         COPY_DISSECTOR(rule, FLOW_DISSECTOR_KEY_ENC_KEYID,
1695                        &tunnel_key.enc_key_id);
1696         tunnel_key.filter_ifindex = filter_dev->ifindex;
1697
1698         err = mapping_add(uplink_priv->tunnel_mapping, &tunnel_key, &tun_id);
1699         if (err)
1700                 return err;
1701
1702         flow_rule_match_enc_opts(rule, &enc_opts_match);
1703         err = enc_opts_is_dont_care_or_full_match(priv,
1704                                                   enc_opts_match.mask,
1705                                                   extack,
1706                                                   &enc_opts_is_dont_care);
1707         if (err)
1708                 goto err_enc_opts;
1709
1710         if (!enc_opts_is_dont_care) {
1711                 memset(&tun_enc_opts, 0, sizeof(tun_enc_opts));
1712                 memcpy(&tun_enc_opts.key, enc_opts_match.key,
1713                        sizeof(*enc_opts_match.key));
1714                 memcpy(&tun_enc_opts.mask, enc_opts_match.mask,
1715                        sizeof(*enc_opts_match.mask));
1716
1717                 err = mapping_add(uplink_priv->tunnel_enc_opts_mapping,
1718                                   &tun_enc_opts, &enc_opts_id);
1719                 if (err)
1720                         goto err_enc_opts;
1721         }
1722
1723         value = tun_id << ENC_OPTS_BITS | enc_opts_id;
1724         mask = enc_opts_id ? TUNNEL_ID_MASK :
1725                              (TUNNEL_ID_MASK & ~ENC_OPTS_BITS_MASK);
1726
1727         if (attr->chain) {
1728                 mlx5e_tc_match_to_reg_match(&attr->parse_attr->spec,
1729                                             TUNNEL_TO_REG, value, mask);
1730         } else {
1731                 mod_hdr_acts = &attr->parse_attr->mod_hdr_acts;
1732                 err = mlx5e_tc_match_to_reg_set(priv->mdev,
1733                                                 mod_hdr_acts, MLX5_FLOW_NAMESPACE_FDB,
1734                                                 TUNNEL_TO_REG, value);
1735                 if (err)
1736                         goto err_set;
1737
1738                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
1739         }
1740
1741         flow->tunnel_id = value;
1742         return 0;
1743
1744 err_set:
1745         if (enc_opts_id)
1746                 mapping_remove(uplink_priv->tunnel_enc_opts_mapping,
1747                                enc_opts_id);
1748 err_enc_opts:
1749         mapping_remove(uplink_priv->tunnel_mapping, tun_id);
1750         return err;
1751 }
1752
1753 static void mlx5e_put_flow_tunnel_id(struct mlx5e_tc_flow *flow)
1754 {
1755         u32 enc_opts_id = flow->tunnel_id & ENC_OPTS_BITS_MASK;
1756         u32 tun_id = flow->tunnel_id >> ENC_OPTS_BITS;
1757         struct mlx5_rep_uplink_priv *uplink_priv;
1758         struct mlx5e_rep_priv *uplink_rpriv;
1759         struct mlx5_eswitch *esw;
1760
1761         esw = flow->priv->mdev->priv.eswitch;
1762         uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
1763         uplink_priv = &uplink_rpriv->uplink_priv;
1764
1765         if (tun_id)
1766                 mapping_remove(uplink_priv->tunnel_mapping, tun_id);
1767         if (enc_opts_id)
1768                 mapping_remove(uplink_priv->tunnel_enc_opts_mapping,
1769                                enc_opts_id);
1770 }
1771
1772 u32 mlx5e_tc_get_flow_tun_id(struct mlx5e_tc_flow *flow)
1773 {
1774         return flow->tunnel_id;
1775 }
1776
1777 void mlx5e_tc_set_ethertype(struct mlx5_core_dev *mdev,
1778                             struct flow_match_basic *match, bool outer,
1779                             void *headers_c, void *headers_v)
1780 {
1781         bool ip_version_cap;
1782
1783         ip_version_cap = outer ?
1784                 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1785                                           ft_field_support.outer_ip_version) :
1786                 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1787                                           ft_field_support.inner_ip_version);
1788
1789         if (ip_version_cap && match->mask->n_proto == htons(0xFFFF) &&
1790             (match->key->n_proto == htons(ETH_P_IP) ||
1791              match->key->n_proto == htons(ETH_P_IPV6))) {
1792                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_version);
1793                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_version,
1794                          match->key->n_proto == htons(ETH_P_IP) ? 4 : 6);
1795         } else {
1796                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
1797                          ntohs(match->mask->n_proto));
1798                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
1799                          ntohs(match->key->n_proto));
1800         }
1801 }
1802
1803 u8 mlx5e_tc_get_ip_version(struct mlx5_flow_spec *spec, bool outer)
1804 {
1805         void *headers_v;
1806         u16 ethertype;
1807         u8 ip_version;
1808
1809         if (outer)
1810                 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
1811         else
1812                 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, inner_headers);
1813
1814         ip_version = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ip_version);
1815         /* Return ip_version converted from ethertype anyway */
1816         if (!ip_version) {
1817                 ethertype = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ethertype);
1818                 if (ethertype == ETH_P_IP || ethertype == ETH_P_ARP)
1819                         ip_version = 4;
1820                 else if (ethertype == ETH_P_IPV6)
1821                         ip_version = 6;
1822         }
1823         return ip_version;
1824 }
1825
1826 static int parse_tunnel_attr(struct mlx5e_priv *priv,
1827                              struct mlx5e_tc_flow *flow,
1828                              struct mlx5_flow_spec *spec,
1829                              struct flow_cls_offload *f,
1830                              struct net_device *filter_dev,
1831                              u8 *match_level,
1832                              bool *match_inner)
1833 {
1834         struct mlx5e_tc_tunnel *tunnel = mlx5e_get_tc_tun(filter_dev);
1835         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1836         struct netlink_ext_ack *extack = f->common.extack;
1837         bool needs_mapping, sets_mapping;
1838         int err;
1839
1840         if (!mlx5e_is_eswitch_flow(flow))
1841                 return -EOPNOTSUPP;
1842
1843         needs_mapping = !!flow->attr->chain;
1844         sets_mapping = !flow->attr->chain && flow_has_tc_fwd_action(f);
1845         *match_inner = !needs_mapping;
1846
1847         if ((needs_mapping || sets_mapping) &&
1848             !mlx5_eswitch_reg_c1_loopback_enabled(esw)) {
1849                 NL_SET_ERR_MSG(extack,
1850                                "Chains on tunnel devices isn't supported without register loopback support");
1851                 netdev_warn(priv->netdev,
1852                             "Chains on tunnel devices isn't supported without register loopback support");
1853                 return -EOPNOTSUPP;
1854         }
1855
1856         if (!flow->attr->chain) {
1857                 err = mlx5e_tc_tun_parse(filter_dev, priv, spec, f,
1858                                          match_level);
1859                 if (err) {
1860                         NL_SET_ERR_MSG_MOD(extack,
1861                                            "Failed to parse tunnel attributes");
1862                         netdev_warn(priv->netdev,
1863                                     "Failed to parse tunnel attributes");
1864                         return err;
1865                 }
1866
1867                 /* With mpls over udp we decapsulate using packet reformat
1868                  * object
1869                  */
1870                 if (!netif_is_bareudp(filter_dev))
1871                         flow->attr->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
1872                 err = mlx5e_tc_set_attr_rx_tun(flow, spec);
1873                 if (err)
1874                         return err;
1875         } else if (tunnel && tunnel->tunnel_type == MLX5E_TC_TUNNEL_TYPE_VXLAN) {
1876                 struct mlx5_flow_spec *tmp_spec;
1877
1878                 tmp_spec = kvzalloc(sizeof(*tmp_spec), GFP_KERNEL);
1879                 if (!tmp_spec) {
1880                         NL_SET_ERR_MSG_MOD(extack, "Failed to allocate memory for vxlan tmp spec");
1881                         netdev_warn(priv->netdev, "Failed to allocate memory for vxlan tmp spec");
1882                         return -ENOMEM;
1883                 }
1884                 memcpy(tmp_spec, spec, sizeof(*tmp_spec));
1885
1886                 err = mlx5e_tc_tun_parse(filter_dev, priv, tmp_spec, f, match_level);
1887                 if (err) {
1888                         kvfree(tmp_spec);
1889                         NL_SET_ERR_MSG_MOD(extack, "Failed to parse tunnel attributes");
1890                         netdev_warn(priv->netdev, "Failed to parse tunnel attributes");
1891                         return err;
1892                 }
1893                 err = mlx5e_tc_set_attr_rx_tun(flow, tmp_spec);
1894                 kvfree(tmp_spec);
1895                 if (err)
1896                         return err;
1897         }
1898
1899         if (!needs_mapping && !sets_mapping)
1900                 return 0;
1901
1902         return mlx5e_get_flow_tunnel_id(priv, flow, f, filter_dev);
1903 }
1904
1905 static void *get_match_inner_headers_criteria(struct mlx5_flow_spec *spec)
1906 {
1907         return MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1908                             inner_headers);
1909 }
1910
1911 static void *get_match_inner_headers_value(struct mlx5_flow_spec *spec)
1912 {
1913         return MLX5_ADDR_OF(fte_match_param, spec->match_value,
1914                             inner_headers);
1915 }
1916
1917 static void *get_match_outer_headers_criteria(struct mlx5_flow_spec *spec)
1918 {
1919         return MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1920                             outer_headers);
1921 }
1922
1923 static void *get_match_outer_headers_value(struct mlx5_flow_spec *spec)
1924 {
1925         return MLX5_ADDR_OF(fte_match_param, spec->match_value,
1926                             outer_headers);
1927 }
1928
1929 static void *get_match_headers_value(u32 flags,
1930                                      struct mlx5_flow_spec *spec)
1931 {
1932         return (flags & MLX5_FLOW_CONTEXT_ACTION_DECAP) ?
1933                 get_match_inner_headers_value(spec) :
1934                 get_match_outer_headers_value(spec);
1935 }
1936
1937 static void *get_match_headers_criteria(u32 flags,
1938                                         struct mlx5_flow_spec *spec)
1939 {
1940         return (flags & MLX5_FLOW_CONTEXT_ACTION_DECAP) ?
1941                 get_match_inner_headers_criteria(spec) :
1942                 get_match_outer_headers_criteria(spec);
1943 }
1944
1945 static int mlx5e_flower_parse_meta(struct net_device *filter_dev,
1946                                    struct flow_cls_offload *f)
1947 {
1948         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1949         struct netlink_ext_ack *extack = f->common.extack;
1950         struct net_device *ingress_dev;
1951         struct flow_match_meta match;
1952
1953         if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_META))
1954                 return 0;
1955
1956         flow_rule_match_meta(rule, &match);
1957         if (!match.mask->ingress_ifindex)
1958                 return 0;
1959
1960         if (match.mask->ingress_ifindex != 0xFFFFFFFF) {
1961                 NL_SET_ERR_MSG_MOD(extack, "Unsupported ingress ifindex mask");
1962                 return -EOPNOTSUPP;
1963         }
1964
1965         ingress_dev = __dev_get_by_index(dev_net(filter_dev),
1966                                          match.key->ingress_ifindex);
1967         if (!ingress_dev) {
1968                 NL_SET_ERR_MSG_MOD(extack,
1969                                    "Can't find the ingress port to match on");
1970                 return -ENOENT;
1971         }
1972
1973         if (ingress_dev != filter_dev) {
1974                 NL_SET_ERR_MSG_MOD(extack,
1975                                    "Can't match on the ingress filter port");
1976                 return -EOPNOTSUPP;
1977         }
1978
1979         return 0;
1980 }
1981
1982 static bool skip_key_basic(struct net_device *filter_dev,
1983                            struct flow_cls_offload *f)
1984 {
1985         /* When doing mpls over udp decap, the user needs to provide
1986          * MPLS_UC as the protocol in order to be able to match on mpls
1987          * label fields.  However, the actual ethertype is IP so we want to
1988          * avoid matching on this, otherwise we'll fail the match.
1989          */
1990         if (netif_is_bareudp(filter_dev) && f->common.chain_index == 0)
1991                 return true;
1992
1993         return false;
1994 }
1995
1996 static int __parse_cls_flower(struct mlx5e_priv *priv,
1997                               struct mlx5e_tc_flow *flow,
1998                               struct mlx5_flow_spec *spec,
1999                               struct flow_cls_offload *f,
2000                               struct net_device *filter_dev,
2001                               u8 *inner_match_level, u8 *outer_match_level)
2002 {
2003         struct netlink_ext_ack *extack = f->common.extack;
2004         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
2005                                        outer_headers);
2006         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2007                                        outer_headers);
2008         void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
2009                                     misc_parameters);
2010         void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2011                                     misc_parameters);
2012         void *misc_c_3 = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
2013                                     misc_parameters_3);
2014         void *misc_v_3 = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2015                                     misc_parameters_3);
2016         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
2017         struct flow_dissector *dissector = rule->match.dissector;
2018         enum fs_flow_table_type fs_type;
2019         u16 addr_type = 0;
2020         u8 ip_proto = 0;
2021         u8 *match_level;
2022         int err;
2023
2024         fs_type = mlx5e_is_eswitch_flow(flow) ? FS_FT_FDB : FS_FT_NIC_RX;
2025         match_level = outer_match_level;
2026
2027         if (dissector->used_keys &
2028             ~(BIT(FLOW_DISSECTOR_KEY_META) |
2029               BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2030               BIT(FLOW_DISSECTOR_KEY_BASIC) |
2031               BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2032               BIT(FLOW_DISSECTOR_KEY_VLAN) |
2033               BIT(FLOW_DISSECTOR_KEY_CVLAN) |
2034               BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2035               BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2036               BIT(FLOW_DISSECTOR_KEY_PORTS) |
2037               BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
2038               BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
2039               BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
2040               BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
2041               BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
2042               BIT(FLOW_DISSECTOR_KEY_TCP) |
2043               BIT(FLOW_DISSECTOR_KEY_IP)  |
2044               BIT(FLOW_DISSECTOR_KEY_CT) |
2045               BIT(FLOW_DISSECTOR_KEY_ENC_IP) |
2046               BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) |
2047               BIT(FLOW_DISSECTOR_KEY_ICMP) |
2048               BIT(FLOW_DISSECTOR_KEY_MPLS))) {
2049                 NL_SET_ERR_MSG_MOD(extack, "Unsupported key");
2050                 netdev_dbg(priv->netdev, "Unsupported key used: 0x%x\n",
2051                            dissector->used_keys);
2052                 return -EOPNOTSUPP;
2053         }
2054
2055         if (mlx5e_get_tc_tun(filter_dev)) {
2056                 bool match_inner = false;
2057
2058                 err = parse_tunnel_attr(priv, flow, spec, f, filter_dev,
2059                                         outer_match_level, &match_inner);
2060                 if (err)
2061                         return err;
2062
2063                 if (match_inner) {
2064                         /* header pointers should point to the inner headers
2065                          * if the packet was decapsulated already.
2066                          * outer headers are set by parse_tunnel_attr.
2067                          */
2068                         match_level = inner_match_level;
2069                         headers_c = get_match_inner_headers_criteria(spec);
2070                         headers_v = get_match_inner_headers_value(spec);
2071                 }
2072         }
2073
2074         err = mlx5e_flower_parse_meta(filter_dev, f);
2075         if (err)
2076                 return err;
2077
2078         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC) &&
2079             !skip_key_basic(filter_dev, f)) {
2080                 struct flow_match_basic match;
2081
2082                 flow_rule_match_basic(rule, &match);
2083                 mlx5e_tc_set_ethertype(priv->mdev, &match,
2084                                        match_level == outer_match_level,
2085                                        headers_c, headers_v);
2086
2087                 if (match.mask->n_proto)
2088                         *match_level = MLX5_MATCH_L2;
2089         }
2090         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
2091             is_vlan_dev(filter_dev)) {
2092                 struct flow_dissector_key_vlan filter_dev_mask;
2093                 struct flow_dissector_key_vlan filter_dev_key;
2094                 struct flow_match_vlan match;
2095
2096                 if (is_vlan_dev(filter_dev)) {
2097                         match.key = &filter_dev_key;
2098                         match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
2099                         match.key->vlan_tpid = vlan_dev_vlan_proto(filter_dev);
2100                         match.key->vlan_priority = 0;
2101                         match.mask = &filter_dev_mask;
2102                         memset(match.mask, 0xff, sizeof(*match.mask));
2103                         match.mask->vlan_priority = 0;
2104                 } else {
2105                         flow_rule_match_vlan(rule, &match);
2106                 }
2107                 if (match.mask->vlan_id ||
2108                     match.mask->vlan_priority ||
2109                     match.mask->vlan_tpid) {
2110                         if (match.key->vlan_tpid == htons(ETH_P_8021AD)) {
2111                                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2112                                          svlan_tag, 1);
2113                                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2114                                          svlan_tag, 1);
2115                         } else {
2116                                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2117                                          cvlan_tag, 1);
2118                                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2119                                          cvlan_tag, 1);
2120                         }
2121
2122                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid,
2123                                  match.mask->vlan_id);
2124                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid,
2125                                  match.key->vlan_id);
2126
2127                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio,
2128                                  match.mask->vlan_priority);
2129                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio,
2130                                  match.key->vlan_priority);
2131
2132                         *match_level = MLX5_MATCH_L2;
2133                 }
2134         } else if (*match_level != MLX5_MATCH_NONE) {
2135                 /* cvlan_tag enabled in match criteria and
2136                  * disabled in match value means both S & C tags
2137                  * don't exist (untagged of both)
2138                  */
2139                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
2140                 *match_level = MLX5_MATCH_L2;
2141         }
2142
2143         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
2144                 struct flow_match_vlan match;
2145
2146                 flow_rule_match_cvlan(rule, &match);
2147                 if (match.mask->vlan_id ||
2148                     match.mask->vlan_priority ||
2149                     match.mask->vlan_tpid) {
2150                         if (!MLX5_CAP_FLOWTABLE_TYPE(priv->mdev, ft_field_support.outer_second_vid,
2151                                                      fs_type)) {
2152                                 NL_SET_ERR_MSG_MOD(extack,
2153                                                    "Matching on CVLAN is not supported");
2154                                 return -EOPNOTSUPP;
2155                         }
2156
2157                         if (match.key->vlan_tpid == htons(ETH_P_8021AD)) {
2158                                 MLX5_SET(fte_match_set_misc, misc_c,
2159                                          outer_second_svlan_tag, 1);
2160                                 MLX5_SET(fte_match_set_misc, misc_v,
2161                                          outer_second_svlan_tag, 1);
2162                         } else {
2163                                 MLX5_SET(fte_match_set_misc, misc_c,
2164                                          outer_second_cvlan_tag, 1);
2165                                 MLX5_SET(fte_match_set_misc, misc_v,
2166                                          outer_second_cvlan_tag, 1);
2167                         }
2168
2169                         MLX5_SET(fte_match_set_misc, misc_c, outer_second_vid,
2170                                  match.mask->vlan_id);
2171                         MLX5_SET(fte_match_set_misc, misc_v, outer_second_vid,
2172                                  match.key->vlan_id);
2173                         MLX5_SET(fte_match_set_misc, misc_c, outer_second_prio,
2174                                  match.mask->vlan_priority);
2175                         MLX5_SET(fte_match_set_misc, misc_v, outer_second_prio,
2176                                  match.key->vlan_priority);
2177
2178                         *match_level = MLX5_MATCH_L2;
2179                         spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS;
2180                 }
2181         }
2182
2183         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2184                 struct flow_match_eth_addrs match;
2185
2186                 flow_rule_match_eth_addrs(rule, &match);
2187                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2188                                              dmac_47_16),
2189                                 match.mask->dst);
2190                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2191                                              dmac_47_16),
2192                                 match.key->dst);
2193
2194                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2195                                              smac_47_16),
2196                                 match.mask->src);
2197                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2198                                              smac_47_16),
2199                                 match.key->src);
2200
2201                 if (!is_zero_ether_addr(match.mask->src) ||
2202                     !is_zero_ether_addr(match.mask->dst))
2203                         *match_level = MLX5_MATCH_L2;
2204         }
2205
2206         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
2207                 struct flow_match_control match;
2208
2209                 flow_rule_match_control(rule, &match);
2210                 addr_type = match.key->addr_type;
2211
2212                 /* the HW doesn't support frag first/later */
2213                 if (match.mask->flags & FLOW_DIS_FIRST_FRAG)
2214                         return -EOPNOTSUPP;
2215
2216                 if (match.mask->flags & FLOW_DIS_IS_FRAGMENT) {
2217                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
2218                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
2219                                  match.key->flags & FLOW_DIS_IS_FRAGMENT);
2220
2221                         /* the HW doesn't need L3 inline to match on frag=no */
2222                         if (!(match.key->flags & FLOW_DIS_IS_FRAGMENT))
2223                                 *match_level = MLX5_MATCH_L2;
2224         /* ***  L2 attributes parsing up to here *** */
2225                         else
2226                                 *match_level = MLX5_MATCH_L3;
2227                 }
2228         }
2229
2230         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
2231                 struct flow_match_basic match;
2232
2233                 flow_rule_match_basic(rule, &match);
2234                 ip_proto = match.key->ip_proto;
2235
2236                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2237                          match.mask->ip_proto);
2238                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2239                          match.key->ip_proto);
2240
2241                 if (match.mask->ip_proto)
2242                         *match_level = MLX5_MATCH_L3;
2243         }
2244
2245         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2246                 struct flow_match_ipv4_addrs match;
2247
2248                 flow_rule_match_ipv4_addrs(rule, &match);
2249                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2250                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
2251                        &match.mask->src, sizeof(match.mask->src));
2252                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2253                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
2254                        &match.key->src, sizeof(match.key->src));
2255                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2256                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2257                        &match.mask->dst, sizeof(match.mask->dst));
2258                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2259                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2260                        &match.key->dst, sizeof(match.key->dst));
2261
2262                 if (match.mask->src || match.mask->dst)
2263                         *match_level = MLX5_MATCH_L3;
2264         }
2265
2266         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2267                 struct flow_match_ipv6_addrs match;
2268
2269                 flow_rule_match_ipv6_addrs(rule, &match);
2270                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2271                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
2272                        &match.mask->src, sizeof(match.mask->src));
2273                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2274                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
2275                        &match.key->src, sizeof(match.key->src));
2276
2277                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2278                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2279                        &match.mask->dst, sizeof(match.mask->dst));
2280                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2281                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2282                        &match.key->dst, sizeof(match.key->dst));
2283
2284                 if (ipv6_addr_type(&match.mask->src) != IPV6_ADDR_ANY ||
2285                     ipv6_addr_type(&match.mask->dst) != IPV6_ADDR_ANY)
2286                         *match_level = MLX5_MATCH_L3;
2287         }
2288
2289         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
2290                 struct flow_match_ip match;
2291
2292                 flow_rule_match_ip(rule, &match);
2293                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn,
2294                          match.mask->tos & 0x3);
2295                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn,
2296                          match.key->tos & 0x3);
2297
2298                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp,
2299                          match.mask->tos >> 2);
2300                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp,
2301                          match.key->tos  >> 2);
2302
2303                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit,
2304                          match.mask->ttl);
2305                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit,
2306                          match.key->ttl);
2307
2308                 if (match.mask->ttl &&
2309                     !MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
2310                                                 ft_field_support.outer_ipv4_ttl)) {
2311                         NL_SET_ERR_MSG_MOD(extack,
2312                                            "Matching on TTL is not supported");
2313                         return -EOPNOTSUPP;
2314                 }
2315
2316                 if (match.mask->tos || match.mask->ttl)
2317                         *match_level = MLX5_MATCH_L3;
2318         }
2319
2320         /* ***  L3 attributes parsing up to here *** */
2321
2322         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
2323                 struct flow_match_ports match;
2324
2325                 flow_rule_match_ports(rule, &match);
2326                 switch (ip_proto) {
2327                 case IPPROTO_TCP:
2328                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2329                                  tcp_sport, ntohs(match.mask->src));
2330                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2331                                  tcp_sport, ntohs(match.key->src));
2332
2333                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2334                                  tcp_dport, ntohs(match.mask->dst));
2335                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2336                                  tcp_dport, ntohs(match.key->dst));
2337                         break;
2338
2339                 case IPPROTO_UDP:
2340                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2341                                  udp_sport, ntohs(match.mask->src));
2342                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2343                                  udp_sport, ntohs(match.key->src));
2344
2345                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2346                                  udp_dport, ntohs(match.mask->dst));
2347                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2348                                  udp_dport, ntohs(match.key->dst));
2349                         break;
2350                 default:
2351                         NL_SET_ERR_MSG_MOD(extack,
2352                                            "Only UDP and TCP transports are supported for L4 matching");
2353                         netdev_err(priv->netdev,
2354                                    "Only UDP and TCP transport are supported\n");
2355                         return -EINVAL;
2356                 }
2357
2358                 if (match.mask->src || match.mask->dst)
2359                         *match_level = MLX5_MATCH_L4;
2360         }
2361
2362         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_TCP)) {
2363                 struct flow_match_tcp match;
2364
2365                 flow_rule_match_tcp(rule, &match);
2366                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_flags,
2367                          ntohs(match.mask->flags));
2368                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
2369                          ntohs(match.key->flags));
2370
2371                 if (match.mask->flags)
2372                         *match_level = MLX5_MATCH_L4;
2373         }
2374         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ICMP)) {
2375                 struct flow_match_icmp match;
2376
2377                 flow_rule_match_icmp(rule, &match);
2378                 switch (ip_proto) {
2379                 case IPPROTO_ICMP:
2380                         if (!(MLX5_CAP_GEN(priv->mdev, flex_parser_protocols) &
2381                               MLX5_FLEX_PROTO_ICMP))
2382                                 return -EOPNOTSUPP;
2383                         MLX5_SET(fte_match_set_misc3, misc_c_3, icmp_type,
2384                                  match.mask->type);
2385                         MLX5_SET(fte_match_set_misc3, misc_v_3, icmp_type,
2386                                  match.key->type);
2387                         MLX5_SET(fte_match_set_misc3, misc_c_3, icmp_code,
2388                                  match.mask->code);
2389                         MLX5_SET(fte_match_set_misc3, misc_v_3, icmp_code,
2390                                  match.key->code);
2391                         break;
2392                 case IPPROTO_ICMPV6:
2393                         if (!(MLX5_CAP_GEN(priv->mdev, flex_parser_protocols) &
2394                               MLX5_FLEX_PROTO_ICMPV6))
2395                                 return -EOPNOTSUPP;
2396                         MLX5_SET(fte_match_set_misc3, misc_c_3, icmpv6_type,
2397                                  match.mask->type);
2398                         MLX5_SET(fte_match_set_misc3, misc_v_3, icmpv6_type,
2399                                  match.key->type);
2400                         MLX5_SET(fte_match_set_misc3, misc_c_3, icmpv6_code,
2401                                  match.mask->code);
2402                         MLX5_SET(fte_match_set_misc3, misc_v_3, icmpv6_code,
2403                                  match.key->code);
2404                         break;
2405                 default:
2406                         NL_SET_ERR_MSG_MOD(extack,
2407                                            "Code and type matching only with ICMP and ICMPv6");
2408                         netdev_err(priv->netdev,
2409                                    "Code and type matching only with ICMP and ICMPv6\n");
2410                         return -EINVAL;
2411                 }
2412                 if (match.mask->code || match.mask->type) {
2413                         *match_level = MLX5_MATCH_L4;
2414                         spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS_3;
2415                 }
2416         }
2417         /* Currenlty supported only for MPLS over UDP */
2418         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_MPLS) &&
2419             !netif_is_bareudp(filter_dev)) {
2420                 NL_SET_ERR_MSG_MOD(extack,
2421                                    "Matching on MPLS is supported only for MPLS over UDP");
2422                 netdev_err(priv->netdev,
2423                            "Matching on MPLS is supported only for MPLS over UDP\n");
2424                 return -EOPNOTSUPP;
2425         }
2426
2427         return 0;
2428 }
2429
2430 static int parse_cls_flower(struct mlx5e_priv *priv,
2431                             struct mlx5e_tc_flow *flow,
2432                             struct mlx5_flow_spec *spec,
2433                             struct flow_cls_offload *f,
2434                             struct net_device *filter_dev)
2435 {
2436         u8 inner_match_level, outer_match_level, non_tunnel_match_level;
2437         struct netlink_ext_ack *extack = f->common.extack;
2438         struct mlx5_core_dev *dev = priv->mdev;
2439         struct mlx5_eswitch *esw = dev->priv.eswitch;
2440         struct mlx5e_rep_priv *rpriv = priv->ppriv;
2441         struct mlx5_eswitch_rep *rep;
2442         bool is_eswitch_flow;
2443         int err;
2444
2445         inner_match_level = MLX5_MATCH_NONE;
2446         outer_match_level = MLX5_MATCH_NONE;
2447
2448         err = __parse_cls_flower(priv, flow, spec, f, filter_dev,
2449                                  &inner_match_level, &outer_match_level);
2450         non_tunnel_match_level = (inner_match_level == MLX5_MATCH_NONE) ?
2451                                  outer_match_level : inner_match_level;
2452
2453         is_eswitch_flow = mlx5e_is_eswitch_flow(flow);
2454         if (!err && is_eswitch_flow) {
2455                 rep = rpriv->rep;
2456                 if (rep->vport != MLX5_VPORT_UPLINK &&
2457                     (esw->offloads.inline_mode != MLX5_INLINE_MODE_NONE &&
2458                     esw->offloads.inline_mode < non_tunnel_match_level)) {
2459                         NL_SET_ERR_MSG_MOD(extack,
2460                                            "Flow is not offloaded due to min inline setting");
2461                         netdev_warn(priv->netdev,
2462                                     "Flow is not offloaded due to min inline setting, required %d actual %d\n",
2463                                     non_tunnel_match_level, esw->offloads.inline_mode);
2464                         return -EOPNOTSUPP;
2465                 }
2466         }
2467
2468         flow->attr->inner_match_level = inner_match_level;
2469         flow->attr->outer_match_level = outer_match_level;
2470
2471
2472         return err;
2473 }
2474
2475 struct pedit_headers {
2476         struct ethhdr  eth;
2477         struct vlan_hdr vlan;
2478         struct iphdr   ip4;
2479         struct ipv6hdr ip6;
2480         struct tcphdr  tcp;
2481         struct udphdr  udp;
2482 };
2483
2484 struct pedit_headers_action {
2485         struct pedit_headers    vals;
2486         struct pedit_headers    masks;
2487         u32                     pedits;
2488 };
2489
2490 static int pedit_header_offsets[] = {
2491         [FLOW_ACT_MANGLE_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth),
2492         [FLOW_ACT_MANGLE_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4),
2493         [FLOW_ACT_MANGLE_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6),
2494         [FLOW_ACT_MANGLE_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp),
2495         [FLOW_ACT_MANGLE_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp),
2496 };
2497
2498 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype])
2499
2500 static int set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset,
2501                          struct pedit_headers_action *hdrs)
2502 {
2503         u32 *curr_pmask, *curr_pval;
2504
2505         curr_pmask = (u32 *)(pedit_header(&hdrs->masks, hdr_type) + offset);
2506         curr_pval  = (u32 *)(pedit_header(&hdrs->vals, hdr_type) + offset);
2507
2508         if (*curr_pmask & mask)  /* disallow acting twice on the same location */
2509                 goto out_err;
2510
2511         *curr_pmask |= mask;
2512         *curr_pval  |= (val & mask);
2513
2514         return 0;
2515
2516 out_err:
2517         return -EOPNOTSUPP;
2518 }
2519
2520 struct mlx5_fields {
2521         u8  field;
2522         u8  field_bsize;
2523         u32 field_mask;
2524         u32 offset;
2525         u32 match_offset;
2526 };
2527
2528 #define OFFLOAD(fw_field, field_bsize, field_mask, field, off, match_field) \
2529                 {MLX5_ACTION_IN_FIELD_OUT_ ## fw_field, field_bsize, field_mask, \
2530                  offsetof(struct pedit_headers, field) + (off), \
2531                  MLX5_BYTE_OFF(fte_match_set_lyr_2_4, match_field)}
2532
2533 /* masked values are the same and there are no rewrites that do not have a
2534  * match.
2535  */
2536 #define SAME_VAL_MASK(type, valp, maskp, matchvalp, matchmaskp) ({ \
2537         type matchmaskx = *(type *)(matchmaskp); \
2538         type matchvalx = *(type *)(matchvalp); \
2539         type maskx = *(type *)(maskp); \
2540         type valx = *(type *)(valp); \
2541         \
2542         (valx & maskx) == (matchvalx & matchmaskx) && !(maskx & (maskx ^ \
2543                                                                  matchmaskx)); \
2544 })
2545
2546 static bool cmp_val_mask(void *valp, void *maskp, void *matchvalp,
2547                          void *matchmaskp, u8 bsize)
2548 {
2549         bool same = false;
2550
2551         switch (bsize) {
2552         case 8:
2553                 same = SAME_VAL_MASK(u8, valp, maskp, matchvalp, matchmaskp);
2554                 break;
2555         case 16:
2556                 same = SAME_VAL_MASK(u16, valp, maskp, matchvalp, matchmaskp);
2557                 break;
2558         case 32:
2559                 same = SAME_VAL_MASK(u32, valp, maskp, matchvalp, matchmaskp);
2560                 break;
2561         }
2562
2563         return same;
2564 }
2565
2566 static struct mlx5_fields fields[] = {
2567         OFFLOAD(DMAC_47_16, 32, U32_MAX, eth.h_dest[0], 0, dmac_47_16),
2568         OFFLOAD(DMAC_15_0,  16, U16_MAX, eth.h_dest[4], 0, dmac_15_0),
2569         OFFLOAD(SMAC_47_16, 32, U32_MAX, eth.h_source[0], 0, smac_47_16),
2570         OFFLOAD(SMAC_15_0,  16, U16_MAX, eth.h_source[4], 0, smac_15_0),
2571         OFFLOAD(ETHERTYPE,  16, U16_MAX, eth.h_proto, 0, ethertype),
2572         OFFLOAD(FIRST_VID,  16, U16_MAX, vlan.h_vlan_TCI, 0, first_vid),
2573
2574         OFFLOAD(IP_DSCP, 8,    0xfc, ip4.tos,   0, ip_dscp),
2575         OFFLOAD(IP_TTL,  8,  U8_MAX, ip4.ttl,   0, ttl_hoplimit),
2576         OFFLOAD(SIPV4,  32, U32_MAX, ip4.saddr, 0, src_ipv4_src_ipv6.ipv4_layout.ipv4),
2577         OFFLOAD(DIPV4,  32, U32_MAX, ip4.daddr, 0, dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2578
2579         OFFLOAD(SIPV6_127_96, 32, U32_MAX, ip6.saddr.s6_addr32[0], 0,
2580                 src_ipv4_src_ipv6.ipv6_layout.ipv6[0]),
2581         OFFLOAD(SIPV6_95_64,  32, U32_MAX, ip6.saddr.s6_addr32[1], 0,
2582                 src_ipv4_src_ipv6.ipv6_layout.ipv6[4]),
2583         OFFLOAD(SIPV6_63_32,  32, U32_MAX, ip6.saddr.s6_addr32[2], 0,
2584                 src_ipv4_src_ipv6.ipv6_layout.ipv6[8]),
2585         OFFLOAD(SIPV6_31_0,   32, U32_MAX, ip6.saddr.s6_addr32[3], 0,
2586                 src_ipv4_src_ipv6.ipv6_layout.ipv6[12]),
2587         OFFLOAD(DIPV6_127_96, 32, U32_MAX, ip6.daddr.s6_addr32[0], 0,
2588                 dst_ipv4_dst_ipv6.ipv6_layout.ipv6[0]),
2589         OFFLOAD(DIPV6_95_64,  32, U32_MAX, ip6.daddr.s6_addr32[1], 0,
2590                 dst_ipv4_dst_ipv6.ipv6_layout.ipv6[4]),
2591         OFFLOAD(DIPV6_63_32,  32, U32_MAX, ip6.daddr.s6_addr32[2], 0,
2592                 dst_ipv4_dst_ipv6.ipv6_layout.ipv6[8]),
2593         OFFLOAD(DIPV6_31_0,   32, U32_MAX, ip6.daddr.s6_addr32[3], 0,
2594                 dst_ipv4_dst_ipv6.ipv6_layout.ipv6[12]),
2595         OFFLOAD(IPV6_HOPLIMIT, 8,  U8_MAX, ip6.hop_limit, 0, ttl_hoplimit),
2596         OFFLOAD(IP_DSCP, 16,  0xc00f, ip6, 0, ip_dscp),
2597
2598         OFFLOAD(TCP_SPORT, 16, U16_MAX, tcp.source,  0, tcp_sport),
2599         OFFLOAD(TCP_DPORT, 16, U16_MAX, tcp.dest,    0, tcp_dport),
2600         /* in linux iphdr tcp_flags is 8 bits long */
2601         OFFLOAD(TCP_FLAGS,  8,  U8_MAX, tcp.ack_seq, 5, tcp_flags),
2602
2603         OFFLOAD(UDP_SPORT, 16, U16_MAX, udp.source, 0, udp_sport),
2604         OFFLOAD(UDP_DPORT, 16, U16_MAX, udp.dest,   0, udp_dport),
2605 };
2606
2607 static unsigned long mask_to_le(unsigned long mask, int size)
2608 {
2609         __be32 mask_be32;
2610         __be16 mask_be16;
2611
2612         if (size == 32) {
2613                 mask_be32 = (__force __be32)(mask);
2614                 mask = (__force unsigned long)cpu_to_le32(be32_to_cpu(mask_be32));
2615         } else if (size == 16) {
2616                 mask_be32 = (__force __be32)(mask);
2617                 mask_be16 = *(__be16 *)&mask_be32;
2618                 mask = (__force unsigned long)cpu_to_le16(be16_to_cpu(mask_be16));
2619         }
2620
2621         return mask;
2622 }
2623 static int offload_pedit_fields(struct mlx5e_priv *priv,
2624                                 int namespace,
2625                                 struct pedit_headers_action *hdrs,
2626                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
2627                                 u32 *action_flags,
2628                                 struct netlink_ext_ack *extack)
2629 {
2630         struct pedit_headers *set_masks, *add_masks, *set_vals, *add_vals;
2631         int i, action_size, first, last, next_z;
2632         void *headers_c, *headers_v, *action, *vals_p;
2633         u32 *s_masks_p, *a_masks_p, s_mask, a_mask;
2634         struct mlx5e_tc_mod_hdr_acts *mod_acts;
2635         struct mlx5_fields *f;
2636         unsigned long mask, field_mask;
2637         int err;
2638         u8 cmd;
2639
2640         mod_acts = &parse_attr->mod_hdr_acts;
2641         headers_c = get_match_headers_criteria(*action_flags, &parse_attr->spec);
2642         headers_v = get_match_headers_value(*action_flags, &parse_attr->spec);
2643
2644         set_masks = &hdrs[0].masks;
2645         add_masks = &hdrs[1].masks;
2646         set_vals = &hdrs[0].vals;
2647         add_vals = &hdrs[1].vals;
2648
2649         action_size = MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto);
2650
2651         for (i = 0; i < ARRAY_SIZE(fields); i++) {
2652                 bool skip;
2653
2654                 f = &fields[i];
2655                 /* avoid seeing bits set from previous iterations */
2656                 s_mask = 0;
2657                 a_mask = 0;
2658
2659                 s_masks_p = (void *)set_masks + f->offset;
2660                 a_masks_p = (void *)add_masks + f->offset;
2661
2662                 s_mask = *s_masks_p & f->field_mask;
2663                 a_mask = *a_masks_p & f->field_mask;
2664
2665                 if (!s_mask && !a_mask) /* nothing to offload here */
2666                         continue;
2667
2668                 if (s_mask && a_mask) {
2669                         NL_SET_ERR_MSG_MOD(extack,
2670                                            "can't set and add to the same HW field");
2671                         printk(KERN_WARNING "mlx5: can't set and add to the same HW field (%x)\n", f->field);
2672                         return -EOPNOTSUPP;
2673                 }
2674
2675                 skip = false;
2676                 if (s_mask) {
2677                         void *match_mask = headers_c + f->match_offset;
2678                         void *match_val = headers_v + f->match_offset;
2679
2680                         cmd  = MLX5_ACTION_TYPE_SET;
2681                         mask = s_mask;
2682                         vals_p = (void *)set_vals + f->offset;
2683                         /* don't rewrite if we have a match on the same value */
2684                         if (cmp_val_mask(vals_p, s_masks_p, match_val,
2685                                          match_mask, f->field_bsize))
2686                                 skip = true;
2687                         /* clear to denote we consumed this field */
2688                         *s_masks_p &= ~f->field_mask;
2689                 } else {
2690                         cmd  = MLX5_ACTION_TYPE_ADD;
2691                         mask = a_mask;
2692                         vals_p = (void *)add_vals + f->offset;
2693                         /* add 0 is no change */
2694                         if ((*(u32 *)vals_p & f->field_mask) == 0)
2695                                 skip = true;
2696                         /* clear to denote we consumed this field */
2697                         *a_masks_p &= ~f->field_mask;
2698                 }
2699                 if (skip)
2700                         continue;
2701
2702                 mask = mask_to_le(mask, f->field_bsize);
2703
2704                 first = find_first_bit(&mask, f->field_bsize);
2705                 next_z = find_next_zero_bit(&mask, f->field_bsize, first);
2706                 last  = find_last_bit(&mask, f->field_bsize);
2707                 if (first < next_z && next_z < last) {
2708                         NL_SET_ERR_MSG_MOD(extack,
2709                                            "rewrite of few sub-fields isn't supported");
2710                         printk(KERN_WARNING "mlx5: rewrite of few sub-fields (mask %lx) isn't offloaded\n",
2711                                mask);
2712                         return -EOPNOTSUPP;
2713                 }
2714
2715                 err = alloc_mod_hdr_actions(priv->mdev, namespace, mod_acts);
2716                 if (err) {
2717                         NL_SET_ERR_MSG_MOD(extack,
2718                                            "too many pedit actions, can't offload");
2719                         mlx5_core_warn(priv->mdev,
2720                                        "mlx5: parsed %d pedit actions, can't do more\n",
2721                                        mod_acts->num_actions);
2722                         return err;
2723                 }
2724
2725                 action = mod_acts->actions +
2726                          (mod_acts->num_actions * action_size);
2727                 MLX5_SET(set_action_in, action, action_type, cmd);
2728                 MLX5_SET(set_action_in, action, field, f->field);
2729
2730                 if (cmd == MLX5_ACTION_TYPE_SET) {
2731                         int start;
2732
2733                         field_mask = mask_to_le(f->field_mask, f->field_bsize);
2734
2735                         /* if field is bit sized it can start not from first bit */
2736                         start = find_first_bit(&field_mask, f->field_bsize);
2737
2738                         MLX5_SET(set_action_in, action, offset, first - start);
2739                         /* length is num of bits to be written, zero means length of 32 */
2740                         MLX5_SET(set_action_in, action, length, (last - first + 1));
2741                 }
2742
2743                 if (f->field_bsize == 32)
2744                         MLX5_SET(set_action_in, action, data, ntohl(*(__be32 *)vals_p) >> first);
2745                 else if (f->field_bsize == 16)
2746                         MLX5_SET(set_action_in, action, data, ntohs(*(__be16 *)vals_p) >> first);
2747                 else if (f->field_bsize == 8)
2748                         MLX5_SET(set_action_in, action, data, *(u8 *)vals_p >> first);
2749
2750                 ++mod_acts->num_actions;
2751         }
2752
2753         return 0;
2754 }
2755
2756 static int mlx5e_flow_namespace_max_modify_action(struct mlx5_core_dev *mdev,
2757                                                   int namespace)
2758 {
2759         if (namespace == MLX5_FLOW_NAMESPACE_FDB) /* FDB offloading */
2760                 return MLX5_CAP_ESW_FLOWTABLE_FDB(mdev, max_modify_header_actions);
2761         else /* namespace is MLX5_FLOW_NAMESPACE_KERNEL - NIC offloading */
2762                 return MLX5_CAP_FLOWTABLE_NIC_RX(mdev, max_modify_header_actions);
2763 }
2764
2765 int alloc_mod_hdr_actions(struct mlx5_core_dev *mdev,
2766                           int namespace,
2767                           struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts)
2768 {
2769         int action_size, new_num_actions, max_hw_actions;
2770         size_t new_sz, old_sz;
2771         void *ret;
2772
2773         if (mod_hdr_acts->num_actions < mod_hdr_acts->max_actions)
2774                 return 0;
2775
2776         action_size = MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto);
2777
2778         max_hw_actions = mlx5e_flow_namespace_max_modify_action(mdev,
2779                                                                 namespace);
2780         new_num_actions = min(max_hw_actions,
2781                               mod_hdr_acts->actions ?
2782                               mod_hdr_acts->max_actions * 2 : 1);
2783         if (mod_hdr_acts->max_actions == new_num_actions)
2784                 return -ENOSPC;
2785
2786         new_sz = action_size * new_num_actions;
2787         old_sz = mod_hdr_acts->max_actions * action_size;
2788         ret = krealloc(mod_hdr_acts->actions, new_sz, GFP_KERNEL);
2789         if (!ret)
2790                 return -ENOMEM;
2791
2792         memset(ret + old_sz, 0, new_sz - old_sz);
2793         mod_hdr_acts->actions = ret;
2794         mod_hdr_acts->max_actions = new_num_actions;
2795
2796         return 0;
2797 }
2798
2799 void dealloc_mod_hdr_actions(struct mlx5e_tc_mod_hdr_acts *mod_hdr_acts)
2800 {
2801         kfree(mod_hdr_acts->actions);
2802         mod_hdr_acts->actions = NULL;
2803         mod_hdr_acts->num_actions = 0;
2804         mod_hdr_acts->max_actions = 0;
2805 }
2806
2807 static const struct pedit_headers zero_masks = {};
2808
2809 static int
2810 parse_pedit_to_modify_hdr(struct mlx5e_priv *priv,
2811                           const struct flow_action_entry *act, int namespace,
2812                           struct mlx5e_tc_flow_parse_attr *parse_attr,
2813                           struct pedit_headers_action *hdrs,
2814                           struct netlink_ext_ack *extack)
2815 {
2816         u8 cmd = (act->id == FLOW_ACTION_MANGLE) ? 0 : 1;
2817         int err = -EOPNOTSUPP;
2818         u32 mask, val, offset;
2819         u8 htype;
2820
2821         htype = act->mangle.htype;
2822         err = -EOPNOTSUPP; /* can't be all optimistic */
2823
2824         if (htype == FLOW_ACT_MANGLE_UNSPEC) {
2825                 NL_SET_ERR_MSG_MOD(extack, "legacy pedit isn't offloaded");
2826                 goto out_err;
2827         }
2828
2829         if (!mlx5e_flow_namespace_max_modify_action(priv->mdev, namespace)) {
2830                 NL_SET_ERR_MSG_MOD(extack,
2831                                    "The pedit offload action is not supported");
2832                 goto out_err;
2833         }
2834
2835         mask = act->mangle.mask;
2836         val = act->mangle.val;
2837         offset = act->mangle.offset;
2838
2839         err = set_pedit_val(htype, ~mask, val, offset, &hdrs[cmd]);
2840         if (err)
2841                 goto out_err;
2842
2843         hdrs[cmd].pedits++;
2844
2845         return 0;
2846 out_err:
2847         return err;
2848 }
2849
2850 static int
2851 parse_pedit_to_reformat(struct mlx5e_priv *priv,
2852                         const struct flow_action_entry *act,
2853                         struct mlx5e_tc_flow_parse_attr *parse_attr,
2854                         struct netlink_ext_ack *extack)
2855 {
2856         u32 mask, val, offset;
2857         u32 *p;
2858
2859         if (act->id != FLOW_ACTION_MANGLE)
2860                 return -EOPNOTSUPP;
2861
2862         if (act->mangle.htype != FLOW_ACT_MANGLE_HDR_TYPE_ETH) {
2863                 NL_SET_ERR_MSG_MOD(extack, "Only Ethernet modification is supported");
2864                 return -EOPNOTSUPP;
2865         }
2866
2867         mask = ~act->mangle.mask;
2868         val = act->mangle.val;
2869         offset = act->mangle.offset;
2870         p = (u32 *)&parse_attr->eth;
2871         *(p + (offset >> 2)) |= (val & mask);
2872
2873         return 0;
2874 }
2875
2876 static int parse_tc_pedit_action(struct mlx5e_priv *priv,
2877                                  const struct flow_action_entry *act, int namespace,
2878                                  struct mlx5e_tc_flow_parse_attr *parse_attr,
2879                                  struct pedit_headers_action *hdrs,
2880                                  struct mlx5e_tc_flow *flow,
2881                                  struct netlink_ext_ack *extack)
2882 {
2883         if (flow && flow_flag_test(flow, L3_TO_L2_DECAP))
2884                 return parse_pedit_to_reformat(priv, act, parse_attr, extack);
2885
2886         return parse_pedit_to_modify_hdr(priv, act, namespace,
2887                                          parse_attr, hdrs, extack);
2888 }
2889
2890 static int alloc_tc_pedit_action(struct mlx5e_priv *priv, int namespace,
2891                                  struct mlx5e_tc_flow_parse_attr *parse_attr,
2892                                  struct pedit_headers_action *hdrs,
2893                                  u32 *action_flags,
2894                                  struct netlink_ext_ack *extack)
2895 {
2896         struct pedit_headers *cmd_masks;
2897         int err;
2898         u8 cmd;
2899
2900         err = offload_pedit_fields(priv, namespace, hdrs, parse_attr,
2901                                    action_flags, extack);
2902         if (err < 0)
2903                 goto out_dealloc_parsed_actions;
2904
2905         for (cmd = 0; cmd < __PEDIT_CMD_MAX; cmd++) {
2906                 cmd_masks = &hdrs[cmd].masks;
2907                 if (memcmp(cmd_masks, &zero_masks, sizeof(zero_masks))) {
2908                         NL_SET_ERR_MSG_MOD(extack,
2909                                            "attempt to offload an unsupported field");
2910                         netdev_warn(priv->netdev, "attempt to offload an unsupported field (cmd %d)\n", cmd);
2911                         print_hex_dump(KERN_WARNING, "mask: ", DUMP_PREFIX_ADDRESS,
2912                                        16, 1, cmd_masks, sizeof(zero_masks), true);
2913                         err = -EOPNOTSUPP;
2914                         goto out_dealloc_parsed_actions;
2915                 }
2916         }
2917
2918         return 0;
2919
2920 out_dealloc_parsed_actions:
2921         dealloc_mod_hdr_actions(&parse_attr->mod_hdr_acts);
2922         return err;
2923 }
2924
2925 static bool csum_offload_supported(struct mlx5e_priv *priv,
2926                                    u32 action,
2927                                    u32 update_flags,
2928                                    struct netlink_ext_ack *extack)
2929 {
2930         u32 prot_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR | TCA_CSUM_UPDATE_FLAG_TCP |
2931                          TCA_CSUM_UPDATE_FLAG_UDP;
2932
2933         /*  The HW recalcs checksums only if re-writing headers */
2934         if (!(action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)) {
2935                 NL_SET_ERR_MSG_MOD(extack,
2936                                    "TC csum action is only offloaded with pedit");
2937                 netdev_warn(priv->netdev,
2938                             "TC csum action is only offloaded with pedit\n");
2939                 return false;
2940         }
2941
2942         if (update_flags & ~prot_flags) {
2943                 NL_SET_ERR_MSG_MOD(extack,
2944                                    "can't offload TC csum action for some header/s");
2945                 netdev_warn(priv->netdev,
2946                             "can't offload TC csum action for some header/s - flags %#x\n",
2947                             update_flags);
2948                 return false;
2949         }
2950
2951         return true;
2952 }
2953
2954 struct ip_ttl_word {
2955         __u8    ttl;
2956         __u8    protocol;
2957         __sum16 check;
2958 };
2959
2960 struct ipv6_hoplimit_word {
2961         __be16  payload_len;
2962         __u8    nexthdr;
2963         __u8    hop_limit;
2964 };
2965
2966 static int is_action_keys_supported(const struct flow_action_entry *act,
2967                                     bool ct_flow, bool *modify_ip_header,
2968                                     bool *modify_tuple,
2969                                     struct netlink_ext_ack *extack)
2970 {
2971         u32 mask, offset;
2972         u8 htype;
2973
2974         htype = act->mangle.htype;
2975         offset = act->mangle.offset;
2976         mask = ~act->mangle.mask;
2977         /* For IPv4 & IPv6 header check 4 byte word,
2978          * to determine that modified fields
2979          * are NOT ttl & hop_limit only.
2980          */
2981         if (htype == FLOW_ACT_MANGLE_HDR_TYPE_IP4) {
2982                 struct ip_ttl_word *ttl_word =
2983                         (struct ip_ttl_word *)&mask;
2984
2985                 if (offset != offsetof(struct iphdr, ttl) ||
2986                     ttl_word->protocol ||
2987                     ttl_word->check) {
2988                         *modify_ip_header = true;
2989                 }
2990
2991                 if (offset >= offsetof(struct iphdr, saddr))
2992                         *modify_tuple = true;
2993
2994                 if (ct_flow && *modify_tuple) {
2995                         NL_SET_ERR_MSG_MOD(extack,
2996                                            "can't offload re-write of ipv4 address with action ct");
2997                         return -EOPNOTSUPP;
2998                 }
2999         } else if (htype == FLOW_ACT_MANGLE_HDR_TYPE_IP6) {
3000                 struct ipv6_hoplimit_word *hoplimit_word =
3001                         (struct ipv6_hoplimit_word *)&mask;
3002
3003                 if (offset != offsetof(struct ipv6hdr, payload_len) ||
3004                     hoplimit_word->payload_len ||
3005                     hoplimit_word->nexthdr) {
3006                         *modify_ip_header = true;
3007                 }
3008
3009                 if (ct_flow && offset >= offsetof(struct ipv6hdr, saddr))
3010                         *modify_tuple = true;
3011
3012                 if (ct_flow && *modify_tuple) {
3013                         NL_SET_ERR_MSG_MOD(extack,
3014                                            "can't offload re-write of ipv6 address with action ct");
3015                         return -EOPNOTSUPP;
3016                 }
3017         } else if (htype == FLOW_ACT_MANGLE_HDR_TYPE_TCP ||
3018                    htype == FLOW_ACT_MANGLE_HDR_TYPE_UDP) {
3019                 *modify_tuple = true;
3020                 if (ct_flow) {
3021                         NL_SET_ERR_MSG_MOD(extack,
3022                                            "can't offload re-write of transport header ports with action ct");
3023                         return -EOPNOTSUPP;
3024                 }
3025         }
3026
3027         return 0;
3028 }
3029
3030 static bool modify_tuple_supported(bool modify_tuple, bool ct_clear,
3031                                    bool ct_flow, struct netlink_ext_ack *extack,
3032                                    struct mlx5e_priv *priv,
3033                                    struct mlx5_flow_spec *spec)
3034 {
3035         if (!modify_tuple || ct_clear)
3036                 return true;
3037
3038         if (ct_flow) {
3039                 NL_SET_ERR_MSG_MOD(extack,
3040                                    "can't offload tuple modification with non-clear ct()");
3041                 netdev_info(priv->netdev,
3042                             "can't offload tuple modification with non-clear ct()");
3043                 return false;
3044         }
3045
3046         /* Add ct_state=-trk match so it will be offloaded for non ct flows
3047          * (or after clear action), as otherwise, since the tuple is changed,
3048          * we can't restore ct state
3049          */
3050         if (mlx5_tc_ct_add_no_trk_match(spec)) {
3051                 NL_SET_ERR_MSG_MOD(extack,
3052                                    "can't offload tuple modification with ct matches and no ct(clear) action");
3053                 netdev_info(priv->netdev,
3054                             "can't offload tuple modification with ct matches and no ct(clear) action");
3055                 return false;
3056         }
3057
3058         return true;
3059 }
3060
3061 static bool modify_header_match_supported(struct mlx5e_priv *priv,
3062                                           struct mlx5_flow_spec *spec,
3063                                           struct flow_action *flow_action,
3064                                           u32 actions, bool ct_flow,
3065                                           bool ct_clear,
3066                                           struct netlink_ext_ack *extack)
3067 {
3068         const struct flow_action_entry *act;
3069         bool modify_ip_header, modify_tuple;
3070         void *headers_c;
3071         void *headers_v;
3072         u16 ethertype;
3073         u8 ip_proto;
3074         int i, err;
3075
3076         headers_c = get_match_headers_criteria(actions, spec);
3077         headers_v = get_match_headers_value(actions, spec);
3078         ethertype = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ethertype);
3079
3080         /* for non-IP we only re-write MACs, so we're okay */
3081         if (MLX5_GET(fte_match_set_lyr_2_4, headers_c, ip_version) == 0 &&
3082             ethertype != ETH_P_IP && ethertype != ETH_P_IPV6)
3083                 goto out_ok;
3084
3085         modify_ip_header = false;
3086         modify_tuple = false;
3087         flow_action_for_each(i, act, flow_action) {
3088                 if (act->id != FLOW_ACTION_MANGLE &&
3089                     act->id != FLOW_ACTION_ADD)
3090                         continue;
3091
3092                 err = is_action_keys_supported(act, ct_flow,
3093                                                &modify_ip_header,
3094                                                &modify_tuple, extack);
3095                 if (err)
3096                         return err;
3097         }
3098
3099         if (!modify_tuple_supported(modify_tuple, ct_clear, ct_flow, extack,
3100                                     priv, spec))
3101                 return false;
3102
3103         ip_proto = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ip_protocol);
3104         if (modify_ip_header && ip_proto != IPPROTO_TCP &&
3105             ip_proto != IPPROTO_UDP && ip_proto != IPPROTO_ICMP) {
3106                 NL_SET_ERR_MSG_MOD(extack,
3107                                    "can't offload re-write of non TCP/UDP");
3108                 netdev_info(priv->netdev, "can't offload re-write of ip proto %d\n",
3109                             ip_proto);
3110                 return false;
3111         }
3112
3113 out_ok:
3114         return true;
3115 }
3116
3117 static bool actions_match_supported(struct mlx5e_priv *priv,
3118                                     struct flow_action *flow_action,
3119                                     struct mlx5e_tc_flow_parse_attr *parse_attr,
3120                                     struct mlx5e_tc_flow *flow,
3121                                     struct netlink_ext_ack *extack)
3122 {
3123         bool ct_flow = false, ct_clear = false;
3124         u32 actions;
3125
3126         ct_clear = flow->attr->ct_attr.ct_action &
3127                 TCA_CT_ACT_CLEAR;
3128         ct_flow = flow_flag_test(flow, CT) && !ct_clear;
3129         actions = flow->attr->action;
3130
3131         if (mlx5e_is_eswitch_flow(flow)) {
3132                 if (flow->attr->esw_attr->split_count && ct_flow &&
3133                     !MLX5_CAP_GEN(flow->attr->esw_attr->in_mdev, reg_c_preserve)) {
3134                         /* All registers used by ct are cleared when using
3135                          * split rules.
3136                          */
3137                         NL_SET_ERR_MSG_MOD(extack,
3138                                            "Can't offload mirroring with action ct");
3139                         return false;
3140                 }
3141         }
3142
3143         if (actions & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
3144                 return modify_header_match_supported(priv, &parse_attr->spec,
3145                                                      flow_action, actions,
3146                                                      ct_flow, ct_clear,
3147                                                      extack);
3148
3149         return true;
3150 }
3151
3152 static bool same_port_devs(struct mlx5e_priv *priv, struct mlx5e_priv *peer_priv)
3153 {
3154         return priv->mdev == peer_priv->mdev;
3155 }
3156
3157 static bool same_hw_devs(struct mlx5e_priv *priv, struct mlx5e_priv *peer_priv)
3158 {
3159         struct mlx5_core_dev *fmdev, *pmdev;
3160         u64 fsystem_guid, psystem_guid;
3161
3162         fmdev = priv->mdev;
3163         pmdev = peer_priv->mdev;
3164
3165         fsystem_guid = mlx5_query_nic_system_image_guid(fmdev);
3166         psystem_guid = mlx5_query_nic_system_image_guid(pmdev);
3167
3168         return (fsystem_guid == psystem_guid);
3169 }
3170
3171 static bool same_vf_reps(struct mlx5e_priv *priv,
3172                          struct net_device *out_dev)
3173 {
3174         return mlx5e_eswitch_vf_rep(priv->netdev) &&
3175                priv->netdev == out_dev;
3176 }
3177
3178 static int add_vlan_rewrite_action(struct mlx5e_priv *priv, int namespace,
3179                                    const struct flow_action_entry *act,
3180                                    struct mlx5e_tc_flow_parse_attr *parse_attr,
3181                                    struct pedit_headers_action *hdrs,
3182                                    u32 *action, struct netlink_ext_ack *extack)
3183 {
3184         u16 mask16 = VLAN_VID_MASK;
3185         u16 val16 = act->vlan.vid & VLAN_VID_MASK;
3186         const struct flow_action_entry pedit_act = {
3187                 .id = FLOW_ACTION_MANGLE,
3188                 .mangle.htype = FLOW_ACT_MANGLE_HDR_TYPE_ETH,
3189                 .mangle.offset = offsetof(struct vlan_ethhdr, h_vlan_TCI),
3190                 .mangle.mask = ~(u32)be16_to_cpu(*(__be16 *)&mask16),
3191                 .mangle.val = (u32)be16_to_cpu(*(__be16 *)&val16),
3192         };
3193         u8 match_prio_mask, match_prio_val;
3194         void *headers_c, *headers_v;
3195         int err;
3196
3197         headers_c = get_match_headers_criteria(*action, &parse_attr->spec);
3198         headers_v = get_match_headers_value(*action, &parse_attr->spec);
3199
3200         if (!(MLX5_GET(fte_match_set_lyr_2_4, headers_c, cvlan_tag) &&
3201               MLX5_GET(fte_match_set_lyr_2_4, headers_v, cvlan_tag))) {
3202                 NL_SET_ERR_MSG_MOD(extack,
3203                                    "VLAN rewrite action must have VLAN protocol match");
3204                 return -EOPNOTSUPP;
3205         }
3206
3207         match_prio_mask = MLX5_GET(fte_match_set_lyr_2_4, headers_c, first_prio);
3208         match_prio_val = MLX5_GET(fte_match_set_lyr_2_4, headers_v, first_prio);
3209         if (act->vlan.prio != (match_prio_val & match_prio_mask)) {
3210                 NL_SET_ERR_MSG_MOD(extack,
3211                                    "Changing VLAN prio is not supported");
3212                 return -EOPNOTSUPP;
3213         }
3214
3215         err = parse_tc_pedit_action(priv, &pedit_act, namespace, parse_attr, hdrs, NULL, extack);
3216         *action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
3217
3218         return err;
3219 }
3220
3221 static int
3222 add_vlan_prio_tag_rewrite_action(struct mlx5e_priv *priv,
3223                                  struct mlx5e_tc_flow_parse_attr *parse_attr,
3224                                  struct pedit_headers_action *hdrs,
3225                                  u32 *action, struct netlink_ext_ack *extack)
3226 {
3227         const struct flow_action_entry prio_tag_act = {
3228                 .vlan.vid = 0,
3229                 .vlan.prio =
3230                         MLX5_GET(fte_match_set_lyr_2_4,
3231                                  get_match_headers_value(*action,
3232                                                          &parse_attr->spec),
3233                                  first_prio) &
3234                         MLX5_GET(fte_match_set_lyr_2_4,
3235                                  get_match_headers_criteria(*action,
3236                                                             &parse_attr->spec),
3237                                  first_prio),
3238         };
3239
3240         return add_vlan_rewrite_action(priv, MLX5_FLOW_NAMESPACE_FDB,
3241                                        &prio_tag_act, parse_attr, hdrs, action,
3242                                        extack);
3243 }
3244
3245 static int validate_goto_chain(struct mlx5e_priv *priv,
3246                                struct mlx5e_tc_flow *flow,
3247                                const struct flow_action_entry *act,
3248                                u32 actions,
3249                                struct netlink_ext_ack *extack)
3250 {
3251         bool is_esw = mlx5e_is_eswitch_flow(flow);
3252         struct mlx5_flow_attr *attr = flow->attr;
3253         bool ft_flow = mlx5e_is_ft_flow(flow);
3254         u32 dest_chain = act->chain_index;
3255         struct mlx5_fs_chains *chains;
3256         struct mlx5_eswitch *esw;
3257         u32 reformat_and_fwd;
3258         u32 max_chain;
3259
3260         esw = priv->mdev->priv.eswitch;
3261         chains = is_esw ? esw_chains(esw) : nic_chains(priv);
3262         max_chain = mlx5_chains_get_chain_range(chains);
3263         reformat_and_fwd = is_esw ?
3264                            MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, reformat_and_fwd_to_table) :
3265                            MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, reformat_and_fwd_to_table);
3266
3267         if (ft_flow) {
3268                 NL_SET_ERR_MSG_MOD(extack, "Goto action is not supported");
3269                 return -EOPNOTSUPP;
3270         }
3271
3272         if (!mlx5_chains_backwards_supported(chains) &&
3273             dest_chain <= attr->chain) {
3274                 NL_SET_ERR_MSG_MOD(extack,
3275                                    "Goto lower numbered chain isn't supported");
3276                 return -EOPNOTSUPP;
3277         }
3278
3279         if (dest_chain > max_chain) {
3280                 NL_SET_ERR_MSG_MOD(extack,
3281                                    "Requested destination chain is out of supported range");
3282                 return -EOPNOTSUPP;
3283         }
3284
3285         if (actions & (MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
3286                        MLX5_FLOW_CONTEXT_ACTION_DECAP) &&
3287             !reformat_and_fwd) {
3288                 NL_SET_ERR_MSG_MOD(extack,
3289                                    "Goto chain is not allowed if action has reformat or decap");
3290                 return -EOPNOTSUPP;
3291         }
3292
3293         return 0;
3294 }
3295
3296 static int parse_tc_nic_actions(struct mlx5e_priv *priv,
3297                                 struct flow_action *flow_action,
3298                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
3299                                 struct mlx5e_tc_flow *flow,
3300                                 struct netlink_ext_ack *extack)
3301 {
3302         struct mlx5_flow_attr *attr = flow->attr;
3303         struct pedit_headers_action hdrs[2] = {};
3304         const struct flow_action_entry *act;
3305         struct mlx5_nic_flow_attr *nic_attr;
3306         u32 action = 0;
3307         int err, i;
3308
3309         if (!flow_action_has_entries(flow_action))
3310                 return -EINVAL;
3311
3312         if (!flow_action_hw_stats_check(flow_action, extack,
3313                                         FLOW_ACTION_HW_STATS_DELAYED_BIT))
3314                 return -EOPNOTSUPP;
3315
3316         nic_attr = attr->nic_attr;
3317
3318         nic_attr->flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
3319
3320         flow_action_for_each(i, act, flow_action) {
3321                 switch (act->id) {
3322                 case FLOW_ACTION_ACCEPT:
3323                         action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
3324                                   MLX5_FLOW_CONTEXT_ACTION_COUNT;
3325                         break;
3326                 case FLOW_ACTION_DROP:
3327                         action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
3328                         if (MLX5_CAP_FLOWTABLE(priv->mdev,
3329                                                flow_table_properties_nic_receive.flow_counter))
3330                                 action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
3331                         break;
3332                 case FLOW_ACTION_MANGLE:
3333                 case FLOW_ACTION_ADD:
3334                         err = parse_tc_pedit_action(priv, act, MLX5_FLOW_NAMESPACE_KERNEL,
3335                                                     parse_attr, hdrs, NULL, extack);
3336                         if (err)
3337                                 return err;
3338
3339                         action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
3340                         break;
3341                 case FLOW_ACTION_VLAN_MANGLE:
3342                         err = add_vlan_rewrite_action(priv,
3343                                                       MLX5_FLOW_NAMESPACE_KERNEL,
3344                                                       act, parse_attr, hdrs,
3345                                                       &action, extack);
3346                         if (err)
3347                                 return err;
3348
3349                         break;
3350                 case FLOW_ACTION_CSUM:
3351                         if (csum_offload_supported(priv, action,
3352                                                    act->csum_flags,
3353                                                    extack))
3354                                 break;
3355
3356                         return -EOPNOTSUPP;
3357                 case FLOW_ACTION_REDIRECT: {
3358                         struct net_device *peer_dev = act->dev;
3359
3360                         if (priv->netdev->netdev_ops == peer_dev->netdev_ops &&
3361                             same_hw_devs(priv, netdev_priv(peer_dev))) {
3362                                 parse_attr->mirred_ifindex[0] = peer_dev->ifindex;
3363                                 flow_flag_set(flow, HAIRPIN);
3364                                 action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
3365                                           MLX5_FLOW_CONTEXT_ACTION_COUNT;
3366                         } else {
3367                                 NL_SET_ERR_MSG_MOD(extack,
3368                                                    "device is not on same HW, can't offload");
3369                                 netdev_warn(priv->netdev, "device %s not on same HW, can't offload\n",
3370                                             peer_dev->name);
3371                                 return -EINVAL;
3372                         }
3373                         }
3374                         break;
3375                 case FLOW_ACTION_MARK: {
3376                         u32 mark = act->mark;
3377
3378                         if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
3379                                 NL_SET_ERR_MSG_MOD(extack,
3380                                                    "Bad flow mark - only 16 bit is supported");
3381                                 return -EINVAL;
3382                         }
3383
3384                         nic_attr->flow_tag = mark;
3385                         action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3386                         }
3387                         break;
3388                 case FLOW_ACTION_GOTO:
3389                         err = validate_goto_chain(priv, flow, act, action,
3390                                                   extack);
3391                         if (err)
3392                                 return err;
3393
3394                         action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
3395                         attr->dest_chain = act->chain_index;
3396                         break;
3397                 case FLOW_ACTION_CT:
3398                         err = mlx5_tc_ct_parse_action(get_ct_priv(priv), attr, act, extack);
3399                         if (err)
3400                                 return err;
3401
3402                         flow_flag_set(flow, CT);
3403                         break;
3404                 default:
3405                         NL_SET_ERR_MSG_MOD(extack, "The offload action is not supported");
3406                         return -EOPNOTSUPP;
3407                 }
3408         }
3409
3410         if (hdrs[TCA_PEDIT_KEY_EX_CMD_SET].pedits ||
3411             hdrs[TCA_PEDIT_KEY_EX_CMD_ADD].pedits) {
3412                 err = alloc_tc_pedit_action(priv, MLX5_FLOW_NAMESPACE_KERNEL,
3413                                             parse_attr, hdrs, &action, extack);
3414                 if (err)
3415                         return err;
3416                 /* in case all pedit actions are skipped, remove the MOD_HDR
3417                  * flag.
3418                  */
3419                 if (parse_attr->mod_hdr_acts.num_actions == 0) {
3420                         action &= ~MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
3421                         dealloc_mod_hdr_actions(&parse_attr->mod_hdr_acts);
3422                 }
3423         }
3424
3425         attr->action = action;
3426
3427         if (attr->dest_chain) {
3428                 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
3429                         NL_SET_ERR_MSG(extack, "Mirroring goto chain rules isn't supported");
3430                         return -EOPNOTSUPP;
3431                 }
3432                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3433         }
3434
3435         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
3436                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3437
3438         if (!actions_match_supported(priv, flow_action, parse_attr, flow, extack))
3439                 return -EOPNOTSUPP;
3440
3441         return 0;
3442 }
3443
3444 static bool is_merged_eswitch_vfs(struct mlx5e_priv *priv,
3445                                   struct net_device *peer_netdev)
3446 {
3447         struct mlx5e_priv *peer_priv;
3448
3449         peer_priv = netdev_priv(peer_netdev);
3450
3451         return (MLX5_CAP_ESW(priv->mdev, merged_eswitch) &&
3452                 mlx5e_eswitch_vf_rep(priv->netdev) &&
3453                 mlx5e_eswitch_vf_rep(peer_netdev) &&
3454                 same_hw_devs(priv, peer_priv));
3455 }
3456
3457 static int parse_tc_vlan_action(struct mlx5e_priv *priv,
3458                                 const struct flow_action_entry *act,
3459                                 struct mlx5_esw_flow_attr *attr,
3460                                 u32 *action)
3461 {
3462         u8 vlan_idx = attr->total_vlan;
3463
3464         if (vlan_idx >= MLX5_FS_VLAN_DEPTH)
3465                 return -EOPNOTSUPP;
3466
3467         switch (act->id) {
3468         case FLOW_ACTION_VLAN_POP:
3469                 if (vlan_idx) {
3470                         if (!mlx5_eswitch_vlan_actions_supported(priv->mdev,
3471                                                                  MLX5_FS_VLAN_DEPTH))
3472                                 return -EOPNOTSUPP;
3473
3474                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2;
3475                 } else {
3476                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
3477                 }
3478                 break;
3479         case FLOW_ACTION_VLAN_PUSH:
3480                 attr->vlan_vid[vlan_idx] = act->vlan.vid;
3481                 attr->vlan_prio[vlan_idx] = act->vlan.prio;
3482                 attr->vlan_proto[vlan_idx] = act->vlan.proto;
3483                 if (!attr->vlan_proto[vlan_idx])
3484                         attr->vlan_proto[vlan_idx] = htons(ETH_P_8021Q);
3485
3486                 if (vlan_idx) {
3487                         if (!mlx5_eswitch_vlan_actions_supported(priv->mdev,
3488                                                                  MLX5_FS_VLAN_DEPTH))
3489                                 return -EOPNOTSUPP;
3490
3491                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
3492                 } else {
3493                         if (!mlx5_eswitch_vlan_actions_supported(priv->mdev, 1) &&
3494                             (act->vlan.proto != htons(ETH_P_8021Q) ||
3495                              act->vlan.prio))
3496                                 return -EOPNOTSUPP;
3497
3498                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
3499                 }
3500                 break;
3501         default:
3502                 return -EINVAL;
3503         }
3504
3505         attr->total_vlan = vlan_idx + 1;
3506
3507         return 0;
3508 }
3509
3510 static struct net_device *get_fdb_out_dev(struct net_device *uplink_dev,
3511                                           struct net_device *out_dev)
3512 {
3513         struct net_device *fdb_out_dev = out_dev;
3514         struct net_device *uplink_upper;
3515
3516         rcu_read_lock();
3517         uplink_upper = netdev_master_upper_dev_get_rcu(uplink_dev);
3518         if (uplink_upper && netif_is_lag_master(uplink_upper) &&
3519             uplink_upper == out_dev) {
3520                 fdb_out_dev = uplink_dev;
3521         } else if (netif_is_lag_master(out_dev)) {
3522                 fdb_out_dev = bond_option_active_slave_get_rcu(netdev_priv(out_dev));
3523                 if (fdb_out_dev &&
3524                     (!mlx5e_eswitch_rep(fdb_out_dev) ||
3525                      !netdev_port_same_parent_id(fdb_out_dev, uplink_dev)))
3526                         fdb_out_dev = NULL;
3527         }
3528         rcu_read_unlock();
3529         return fdb_out_dev;
3530 }
3531
3532 static int add_vlan_push_action(struct mlx5e_priv *priv,
3533                                 struct mlx5_flow_attr *attr,
3534                                 struct net_device **out_dev,
3535                                 u32 *action)
3536 {
3537         struct net_device *vlan_dev = *out_dev;
3538         struct flow_action_entry vlan_act = {
3539                 .id = FLOW_ACTION_VLAN_PUSH,
3540                 .vlan.vid = vlan_dev_vlan_id(vlan_dev),
3541                 .vlan.proto = vlan_dev_vlan_proto(vlan_dev),
3542                 .vlan.prio = 0,
3543         };
3544         int err;
3545
3546         err = parse_tc_vlan_action(priv, &vlan_act, attr->esw_attr, action);
3547         if (err)
3548                 return err;
3549
3550         rcu_read_lock();
3551         *out_dev = dev_get_by_index_rcu(dev_net(vlan_dev), dev_get_iflink(vlan_dev));
3552         rcu_read_unlock();
3553         if (!*out_dev)
3554                 return -ENODEV;
3555
3556         if (is_vlan_dev(*out_dev))
3557                 err = add_vlan_push_action(priv, attr, out_dev, action);
3558
3559         return err;
3560 }
3561
3562 static int add_vlan_pop_action(struct mlx5e_priv *priv,
3563                                struct mlx5_flow_attr *attr,
3564                                u32 *action)
3565 {
3566         struct flow_action_entry vlan_act = {
3567                 .id = FLOW_ACTION_VLAN_POP,
3568         };
3569         int nest_level, err = 0;
3570
3571         nest_level = attr->parse_attr->filter_dev->lower_level -
3572                                                 priv->netdev->lower_level;
3573         while (nest_level--) {
3574                 err = parse_tc_vlan_action(priv, &vlan_act, attr->esw_attr, action);
3575                 if (err)
3576                         return err;
3577         }
3578
3579         return err;
3580 }
3581
3582 static bool same_hw_reps(struct mlx5e_priv *priv,
3583                          struct net_device *peer_netdev)
3584 {
3585         struct mlx5e_priv *peer_priv;
3586
3587         peer_priv = netdev_priv(peer_netdev);
3588
3589         return mlx5e_eswitch_rep(priv->netdev) &&
3590                mlx5e_eswitch_rep(peer_netdev) &&
3591                same_hw_devs(priv, peer_priv);
3592 }
3593
3594 static bool is_lag_dev(struct mlx5e_priv *priv,
3595                        struct net_device *peer_netdev)
3596 {
3597         return ((mlx5_lag_is_sriov(priv->mdev) ||
3598                  mlx5_lag_is_multipath(priv->mdev)) &&
3599                  same_hw_reps(priv, peer_netdev));
3600 }
3601
3602 bool mlx5e_is_valid_eswitch_fwd_dev(struct mlx5e_priv *priv,
3603                                     struct net_device *out_dev)
3604 {
3605         if (is_merged_eswitch_vfs(priv, out_dev))
3606                 return true;
3607
3608         if (is_lag_dev(priv, out_dev))
3609                 return true;
3610
3611         return mlx5e_eswitch_rep(out_dev) &&
3612                same_port_devs(priv, netdev_priv(out_dev));
3613 }
3614
3615 static bool is_duplicated_output_device(struct net_device *dev,
3616                                         struct net_device *out_dev,
3617                                         int *ifindexes, int if_count,
3618                                         struct netlink_ext_ack *extack)
3619 {
3620         int i;
3621
3622         for (i = 0; i < if_count; i++) {
3623                 if (ifindexes[i] == out_dev->ifindex) {
3624                         NL_SET_ERR_MSG_MOD(extack,
3625                                            "can't duplicate output to same device");
3626                         netdev_err(dev, "can't duplicate output to same device: %s\n",
3627                                    out_dev->name);
3628                         return true;
3629                 }
3630         }
3631
3632         return false;
3633 }
3634
3635 static int verify_uplink_forwarding(struct mlx5e_priv *priv,
3636                                     struct mlx5e_tc_flow *flow,
3637                                     struct net_device *out_dev,
3638                                     struct netlink_ext_ack *extack)
3639 {
3640         struct mlx5_esw_flow_attr *attr = flow->attr->esw_attr;
3641         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
3642         struct mlx5e_rep_priv *rep_priv;
3643
3644         /* Forwarding non encapsulated traffic between
3645          * uplink ports is allowed only if
3646          * termination_table_raw_traffic cap is set.
3647          *
3648          * Input vport was stored attr->in_rep.
3649          * In LAG case, *priv* is the private data of
3650          * uplink which may be not the input vport.
3651          */
3652         rep_priv = mlx5e_rep_to_rep_priv(attr->in_rep);
3653
3654         if (!(mlx5e_eswitch_uplink_rep(rep_priv->netdev) &&
3655               mlx5e_eswitch_uplink_rep(out_dev)))
3656                 return 0;
3657
3658         if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev,
3659                                         termination_table_raw_traffic)) {
3660                 NL_SET_ERR_MSG_MOD(extack,
3661                                    "devices are both uplink, can't offload forwarding");
3662                         pr_err("devices %s %s are both uplink, can't offload forwarding\n",
3663                                priv->netdev->name, out_dev->name);
3664                         return -EOPNOTSUPP;
3665         } else if (out_dev != rep_priv->netdev) {
3666                 NL_SET_ERR_MSG_MOD(extack,
3667                                    "devices are not the same uplink, can't offload forwarding");
3668                 pr_err("devices %s %s are both uplink but not the same, can't offload forwarding\n",
3669                        priv->netdev->name, out_dev->name);
3670                 return -EOPNOTSUPP;
3671         }
3672         return 0;
3673 }
3674
3675 static int parse_tc_fdb_actions(struct mlx5e_priv *priv,
3676                                 struct flow_action *flow_action,
3677                                 struct mlx5e_tc_flow *flow,
3678                                 struct netlink_ext_ack *extack,
3679                                 struct net_device *filter_dev)
3680 {
3681         struct pedit_headers_action hdrs[2] = {};
3682         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
3683         struct mlx5e_tc_flow_parse_attr *parse_attr;
3684         struct mlx5e_rep_priv *rpriv = priv->ppriv;
3685         const struct ip_tunnel_info *info = NULL;
3686         struct mlx5_flow_attr *attr = flow->attr;
3687         int ifindexes[MLX5_MAX_FLOW_FWD_VPORTS];
3688         bool ft_flow = mlx5e_is_ft_flow(flow);
3689         const struct flow_action_entry *act;
3690         struct mlx5_esw_flow_attr *esw_attr;
3691         struct mlx5_sample_attr sample = {};
3692         bool encap = false, decap = false;
3693         u32 action = attr->action;
3694         int err, i, if_count = 0;
3695         bool mpls_push = false;
3696
3697         if (!flow_action_has_entries(flow_action))
3698                 return -EINVAL;
3699
3700         if (!flow_action_hw_stats_check(flow_action, extack,
3701                                         FLOW_ACTION_HW_STATS_DELAYED_BIT))
3702                 return -EOPNOTSUPP;
3703
3704         esw_attr = attr->esw_attr;
3705         parse_attr = attr->parse_attr;
3706
3707         flow_action_for_each(i, act, flow_action) {
3708                 switch (act->id) {
3709                 case FLOW_ACTION_DROP:
3710                         action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
3711                                   MLX5_FLOW_CONTEXT_ACTION_COUNT;
3712                         break;
3713                 case FLOW_ACTION_TRAP:
3714                         if (!flow_offload_has_one_action(flow_action)) {
3715                                 NL_SET_ERR_MSG_MOD(extack,
3716                                                    "action trap is supported as a sole action only");
3717                                 return -EOPNOTSUPP;
3718                         }
3719                         action |= (MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
3720                                    MLX5_FLOW_CONTEXT_ACTION_COUNT);
3721                         attr->flags |= MLX5_ESW_ATTR_FLAG_SLOW_PATH;
3722                         break;
3723                 case FLOW_ACTION_MPLS_PUSH:
3724                         if (!MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
3725                                                         reformat_l2_to_l3_tunnel) ||
3726                             act->mpls_push.proto != htons(ETH_P_MPLS_UC)) {
3727                                 NL_SET_ERR_MSG_MOD(extack,
3728                                                    "mpls push is supported only for mpls_uc protocol");
3729                                 return -EOPNOTSUPP;
3730                         }
3731                         mpls_push = true;
3732                         break;
3733                 case FLOW_ACTION_MPLS_POP:
3734                         /* we only support mpls pop if it is the first action
3735                          * and the filter net device is bareudp. Subsequent
3736                          * actions can be pedit and the last can be mirred
3737                          * egress redirect.
3738                          */
3739                         if (i) {
3740                                 NL_SET_ERR_MSG_MOD(extack,
3741                                                    "mpls pop supported only as first action");
3742                                 return -EOPNOTSUPP;
3743                         }
3744                         if (!netif_is_bareudp(filter_dev)) {
3745                                 NL_SET_ERR_MSG_MOD(extack,
3746                                                    "mpls pop supported only on bareudp devices");
3747                                 return -EOPNOTSUPP;
3748                         }
3749
3750                         parse_attr->eth.h_proto = act->mpls_pop.proto;
3751                         action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
3752                         flow_flag_set(flow, L3_TO_L2_DECAP);
3753                         break;
3754                 case FLOW_ACTION_MANGLE:
3755                 case FLOW_ACTION_ADD:
3756                         err = parse_tc_pedit_action(priv, act, MLX5_FLOW_NAMESPACE_FDB,
3757                                                     parse_attr, hdrs, flow, extack);
3758                         if (err)
3759                                 return err;
3760
3761                         if (!flow_flag_test(flow, L3_TO_L2_DECAP)) {
3762                                 action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
3763                                 esw_attr->split_count = esw_attr->out_count;
3764                         }
3765                         break;
3766                 case FLOW_ACTION_CSUM:
3767                         if (csum_offload_supported(priv, action,
3768                                                    act->csum_flags, extack))
3769                                 break;
3770
3771                         return -EOPNOTSUPP;
3772                 case FLOW_ACTION_REDIRECT:
3773                 case FLOW_ACTION_MIRRED: {
3774                         struct mlx5e_priv *out_priv;
3775                         struct net_device *out_dev;
3776
3777                         out_dev = act->dev;
3778                         if (!out_dev) {
3779                                 /* out_dev is NULL when filters with
3780                                  * non-existing mirred device are replayed to
3781                                  * the driver.
3782                                  */
3783                                 return -EINVAL;
3784                         }
3785
3786                         if (mpls_push && !netif_is_bareudp(out_dev)) {
3787                                 NL_SET_ERR_MSG_MOD(extack,
3788                                                    "mpls is supported only through a bareudp device");
3789                                 return -EOPNOTSUPP;
3790                         }
3791
3792                         if (ft_flow && out_dev == priv->netdev) {
3793                                 /* Ignore forward to self rules generated
3794                                  * by adding both mlx5 devs to the flow table
3795                                  * block on a normal nft offload setup.
3796                                  */
3797                                 return -EOPNOTSUPP;
3798                         }
3799
3800                         if (esw_attr->out_count >= MLX5_MAX_FLOW_FWD_VPORTS) {
3801                                 NL_SET_ERR_MSG_MOD(extack,
3802                                                    "can't support more output ports, can't offload forwarding");
3803                                 netdev_warn(priv->netdev,
3804                                             "can't support more than %d output ports, can't offload forwarding\n",
3805                                             esw_attr->out_count);
3806                                 return -EOPNOTSUPP;
3807                         }
3808
3809                         action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
3810                                   MLX5_FLOW_CONTEXT_ACTION_COUNT;
3811                         if (encap) {
3812                                 parse_attr->mirred_ifindex[esw_attr->out_count] =
3813                                         out_dev->ifindex;
3814                                 parse_attr->tun_info[esw_attr->out_count] =
3815                                         mlx5e_dup_tun_info(info);
3816                                 if (!parse_attr->tun_info[esw_attr->out_count])
3817                                         return -ENOMEM;
3818                                 encap = false;
3819                                 esw_attr->dests[esw_attr->out_count].flags |=
3820                                         MLX5_ESW_DEST_ENCAP;
3821                                 esw_attr->out_count++;
3822                                 /* attr->dests[].rep is resolved when we
3823                                  * handle encap
3824                                  */
3825                         } else if (netdev_port_same_parent_id(priv->netdev, out_dev)) {
3826                                 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
3827                                 struct net_device *uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
3828
3829                                 if (is_duplicated_output_device(priv->netdev,
3830                                                                 out_dev,
3831                                                                 ifindexes,
3832                                                                 if_count,
3833                                                                 extack))
3834                                         return -EOPNOTSUPP;
3835
3836                                 ifindexes[if_count] = out_dev->ifindex;
3837                                 if_count++;
3838
3839                                 out_dev = get_fdb_out_dev(uplink_dev, out_dev);
3840                                 if (!out_dev)
3841                                         return -ENODEV;
3842
3843                                 if (is_vlan_dev(out_dev)) {
3844                                         err = add_vlan_push_action(priv, attr,
3845                                                                    &out_dev,
3846                                                                    &action);
3847                                         if (err)
3848                                                 return err;
3849                                 }
3850
3851                                 if (is_vlan_dev(parse_attr->filter_dev)) {
3852                                         err = add_vlan_pop_action(priv, attr,
3853                                                                   &action);
3854                                         if (err)
3855                                                 return err;
3856                                 }
3857
3858                                 err = verify_uplink_forwarding(priv, flow, out_dev, extack);
3859                                 if (err)
3860                                         return err;
3861
3862                                 if (!mlx5e_is_valid_eswitch_fwd_dev(priv, out_dev)) {
3863                                         NL_SET_ERR_MSG_MOD(extack,
3864                                                            "devices are not on same switch HW, can't offload forwarding");
3865                                         return -EOPNOTSUPP;
3866                                 }
3867
3868                                 if (same_vf_reps(priv, out_dev)) {
3869                                         NL_SET_ERR_MSG_MOD(extack,
3870                                                            "can't forward from a VF to itself");
3871                                         return -EOPNOTSUPP;
3872                                 }
3873
3874                                 out_priv = netdev_priv(out_dev);
3875                                 rpriv = out_priv->ppriv;
3876                                 esw_attr->dests[esw_attr->out_count].rep = rpriv->rep;
3877                                 esw_attr->dests[esw_attr->out_count].mdev = out_priv->mdev;
3878                                 esw_attr->out_count++;
3879                         } else if (parse_attr->filter_dev != priv->netdev) {
3880                                 /* All mlx5 devices are called to configure
3881                                  * high level device filters. Therefore, the
3882                                  * *attempt* to  install a filter on invalid
3883                                  * eswitch should not trigger an explicit error
3884                                  */
3885                                 return -EINVAL;
3886                         } else {
3887                                 NL_SET_ERR_MSG_MOD(extack,
3888                                                    "devices are not on same switch HW, can't offload forwarding");
3889                                 netdev_warn(priv->netdev,
3890                                             "devices %s %s not on same switch HW, can't offload forwarding\n",
3891                                             priv->netdev->name,
3892                                             out_dev->name);
3893                                 return -EINVAL;
3894                         }
3895                         }
3896                         break;
3897                 case FLOW_ACTION_TUNNEL_ENCAP:
3898                         info = act->tunnel;
3899                         if (info)
3900                                 encap = true;
3901                         else
3902                                 return -EOPNOTSUPP;
3903
3904                         break;
3905                 case FLOW_ACTION_VLAN_PUSH:
3906                 case FLOW_ACTION_VLAN_POP:
3907                         if (act->id == FLOW_ACTION_VLAN_PUSH &&
3908                             (action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP)) {
3909                                 /* Replace vlan pop+push with vlan modify */
3910                                 action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
3911                                 err = add_vlan_rewrite_action(priv,
3912                                                               MLX5_FLOW_NAMESPACE_FDB,
3913                                                               act, parse_attr, hdrs,
3914                                                               &action, extack);
3915                         } else {
3916                                 err = parse_tc_vlan_action(priv, act, esw_attr, &action);
3917                         }
3918                         if (err)
3919                                 return err;
3920
3921                         esw_attr->split_count = esw_attr->out_count;
3922                         break;
3923                 case FLOW_ACTION_VLAN_MANGLE:
3924                         err = add_vlan_rewrite_action(priv,
3925                                                       MLX5_FLOW_NAMESPACE_FDB,
3926                                                       act, parse_attr, hdrs,
3927                                                       &action, extack);
3928                         if (err)
3929                                 return err;
3930
3931                         esw_attr->split_count = esw_attr->out_count;
3932                         break;
3933                 case FLOW_ACTION_TUNNEL_DECAP:
3934                         decap = true;
3935                         break;
3936                 case FLOW_ACTION_GOTO:
3937                         err = validate_goto_chain(priv, flow, act, action,
3938                                                   extack);
3939                         if (err)
3940                                 return err;
3941
3942                         action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
3943                         attr->dest_chain = act->chain_index;
3944                         break;
3945                 case FLOW_ACTION_CT:
3946                         if (flow_flag_test(flow, SAMPLE)) {
3947                                 NL_SET_ERR_MSG_MOD(extack, "Sample action with connection tracking is not supported");
3948                                 return -EOPNOTSUPP;
3949                         }
3950                         err = mlx5_tc_ct_parse_action(get_ct_priv(priv), attr, act, extack);
3951                         if (err)
3952                                 return err;
3953
3954                         flow_flag_set(flow, CT);
3955                         esw_attr->split_count = esw_attr->out_count;
3956                         break;
3957                 case FLOW_ACTION_SAMPLE:
3958                         if (flow_flag_test(flow, CT)) {
3959                                 NL_SET_ERR_MSG_MOD(extack, "Sample action with connection tracking is not supported");
3960                                 return -EOPNOTSUPP;
3961                         }
3962                         sample.rate = act->sample.rate;
3963                         sample.group_num = act->sample.psample_group->group_num;
3964                         if (act->sample.truncate)
3965                                 sample.trunc_size = act->sample.trunc_size;
3966                         flow_flag_set(flow, SAMPLE);
3967                         break;
3968                 default:
3969                         NL_SET_ERR_MSG_MOD(extack, "The offload action is not supported");
3970                         return -EOPNOTSUPP;
3971                 }
3972         }
3973
3974         /* always set IP version for indirect table handling */
3975         attr->ip_version = mlx5e_tc_get_ip_version(&parse_attr->spec, true);
3976
3977         if (MLX5_CAP_GEN(esw->dev, prio_tag_required) &&
3978             action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) {
3979                 /* For prio tag mode, replace vlan pop with rewrite vlan prio
3980                  * tag rewrite.
3981                  */
3982                 action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
3983                 err = add_vlan_prio_tag_rewrite_action(priv, parse_attr, hdrs,
3984                                                        &action, extack);
3985                 if (err)
3986                         return err;
3987         }
3988
3989         if (hdrs[TCA_PEDIT_KEY_EX_CMD_SET].pedits ||
3990             hdrs[TCA_PEDIT_KEY_EX_CMD_ADD].pedits) {
3991                 err = alloc_tc_pedit_action(priv, MLX5_FLOW_NAMESPACE_FDB,
3992                                             parse_attr, hdrs, &action, extack);
3993                 if (err)
3994                         return err;
3995                 /* in case all pedit actions are skipped, remove the MOD_HDR
3996                  * flag. we might have set split_count either by pedit or
3997                  * pop/push. if there is no pop/push either, reset it too.
3998                  */
3999                 if (parse_attr->mod_hdr_acts.num_actions == 0) {
4000                         action &= ~MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
4001                         dealloc_mod_hdr_actions(&parse_attr->mod_hdr_acts);
4002                         if (!((action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) ||
4003                               (action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH)))
4004                                 esw_attr->split_count = 0;
4005                 }
4006         }
4007
4008         attr->action = action;
4009         if (!actions_match_supported(priv, flow_action, parse_attr, flow, extack))
4010                 return -EOPNOTSUPP;
4011
4012         if (attr->dest_chain) {
4013                 if (decap) {
4014                         /* It can be supported if we'll create a mapping for
4015                          * the tunnel device only (without tunnel), and set
4016                          * this tunnel id with this decap flow.
4017                          *
4018                          * On restore (miss), we'll just set this saved tunnel
4019                          * device.
4020                          */
4021
4022                         NL_SET_ERR_MSG(extack,
4023                                        "Decap with goto isn't supported");
4024                         netdev_warn(priv->netdev,
4025                                     "Decap with goto isn't supported");
4026                         return -EOPNOTSUPP;
4027                 }
4028
4029                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4030         }
4031
4032         if (!(attr->action &
4033               (MLX5_FLOW_CONTEXT_ACTION_FWD_DEST | MLX5_FLOW_CONTEXT_ACTION_DROP))) {
4034                 NL_SET_ERR_MSG_MOD(extack,
4035                                    "Rule must have at least one forward/drop action");
4036                 return -EOPNOTSUPP;
4037         }
4038
4039         if (esw_attr->split_count > 0 && !mlx5_esw_has_fwd_fdb(priv->mdev)) {
4040                 NL_SET_ERR_MSG_MOD(extack,
4041                                    "current firmware doesn't support split rule for port mirroring");
4042                 netdev_warn_once(priv->netdev, "current firmware doesn't support split rule for port mirroring\n");
4043                 return -EOPNOTSUPP;
4044         }
4045
4046         /* Allocate sample attribute only when there is a sample action and
4047          * no errors after parsing.
4048          */
4049         if (flow_flag_test(flow, SAMPLE)) {
4050                 esw_attr->sample = kzalloc(sizeof(*esw_attr->sample), GFP_KERNEL);
4051                 if (!esw_attr->sample)
4052                         return -ENOMEM;
4053                 *esw_attr->sample = sample;
4054         }
4055
4056         return 0;
4057 }
4058
4059 static void get_flags(int flags, unsigned long *flow_flags)
4060 {
4061         unsigned long __flow_flags = 0;
4062
4063         if (flags & MLX5_TC_FLAG(INGRESS))
4064                 __flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_INGRESS);
4065         if (flags & MLX5_TC_FLAG(EGRESS))
4066                 __flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_EGRESS);
4067
4068         if (flags & MLX5_TC_FLAG(ESW_OFFLOAD))
4069                 __flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_ESWITCH);
4070         if (flags & MLX5_TC_FLAG(NIC_OFFLOAD))
4071                 __flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_NIC);
4072         if (flags & MLX5_TC_FLAG(FT_OFFLOAD))
4073                 __flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_FT);
4074
4075         *flow_flags = __flow_flags;
4076 }
4077
4078 static const struct rhashtable_params tc_ht_params = {
4079         .head_offset = offsetof(struct mlx5e_tc_flow, node),
4080         .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
4081         .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
4082         .automatic_shrinking = true,
4083 };
4084
4085 static struct rhashtable *get_tc_ht(struct mlx5e_priv *priv,
4086                                     unsigned long flags)
4087 {
4088         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
4089         struct mlx5e_rep_priv *uplink_rpriv;
4090
4091         if (flags & MLX5_TC_FLAG(ESW_OFFLOAD)) {
4092                 uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
4093                 return &uplink_rpriv->uplink_priv.tc_ht;
4094         } else /* NIC offload */
4095                 return &priv->fs.tc.ht;
4096 }
4097
4098 static bool is_peer_flow_needed(struct mlx5e_tc_flow *flow)
4099 {
4100         struct mlx5_esw_flow_attr *esw_attr = flow->attr->esw_attr;
4101         struct mlx5_flow_attr *attr = flow->attr;
4102         bool is_rep_ingress = esw_attr->in_rep->vport != MLX5_VPORT_UPLINK &&
4103                 flow_flag_test(flow, INGRESS);
4104         bool act_is_encap = !!(attr->action &
4105                                MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT);
4106         bool esw_paired = mlx5_devcom_is_paired(esw_attr->in_mdev->priv.devcom,
4107                                                 MLX5_DEVCOM_ESW_OFFLOADS);
4108
4109         if (!esw_paired)
4110                 return false;
4111
4112         if ((mlx5_lag_is_sriov(esw_attr->in_mdev) ||
4113              mlx5_lag_is_multipath(esw_attr->in_mdev)) &&
4114             (is_rep_ingress || act_is_encap))
4115                 return true;
4116
4117         return false;
4118 }
4119
4120 struct mlx5_flow_attr *
4121 mlx5_alloc_flow_attr(enum mlx5_flow_namespace_type type)
4122 {
4123         u32 ex_attr_size = (type == MLX5_FLOW_NAMESPACE_FDB)  ?
4124                                 sizeof(struct mlx5_esw_flow_attr) :
4125                                 sizeof(struct mlx5_nic_flow_attr);
4126         struct mlx5_flow_attr *attr;
4127
4128         return kzalloc(sizeof(*attr) + ex_attr_size, GFP_KERNEL);
4129 }
4130
4131 static int
4132 mlx5e_alloc_flow(struct mlx5e_priv *priv, int attr_size,
4133                  struct flow_cls_offload *f, unsigned long flow_flags,
4134                  struct mlx5e_tc_flow_parse_attr **__parse_attr,
4135                  struct mlx5e_tc_flow **__flow)
4136 {
4137         struct mlx5e_tc_flow_parse_attr *parse_attr;
4138         struct mlx5_flow_attr *attr;
4139         struct mlx5e_tc_flow *flow;
4140         int err = -ENOMEM;
4141         int out_index;
4142
4143         flow = kzalloc(sizeof(*flow), GFP_KERNEL);
4144         parse_attr = kvzalloc(sizeof(*parse_attr), GFP_KERNEL);
4145         if (!parse_attr || !flow)
4146                 goto err_free;
4147
4148         flow->flags = flow_flags;
4149         flow->cookie = f->cookie;
4150         flow->priv = priv;
4151
4152         attr = mlx5_alloc_flow_attr(get_flow_name_space(flow));
4153         if (!attr)
4154                 goto err_free;
4155
4156         flow->attr = attr;
4157
4158         for (out_index = 0; out_index < MLX5_MAX_FLOW_FWD_VPORTS; out_index++)
4159                 INIT_LIST_HEAD(&flow->encaps[out_index].list);
4160         INIT_LIST_HEAD(&flow->hairpin);
4161         INIT_LIST_HEAD(&flow->l3_to_l2_reformat);
4162         refcount_set(&flow->refcnt, 1);
4163         init_completion(&flow->init_done);
4164
4165         *__flow = flow;
4166         *__parse_attr = parse_attr;
4167
4168         return 0;
4169
4170 err_free:
4171         kfree(flow);
4172         kvfree(parse_attr);
4173         return err;
4174 }
4175
4176 static void
4177 mlx5e_flow_attr_init(struct mlx5_flow_attr *attr,
4178                      struct mlx5e_tc_flow_parse_attr *parse_attr,
4179                      struct flow_cls_offload *f)
4180 {
4181         attr->parse_attr = parse_attr;
4182         attr->chain = f->common.chain_index;
4183         attr->prio = f->common.prio;
4184 }
4185
4186 static void
4187 mlx5e_flow_esw_attr_init(struct mlx5_flow_attr *attr,
4188                          struct mlx5e_priv *priv,
4189                          struct mlx5e_tc_flow_parse_attr *parse_attr,
4190                          struct flow_cls_offload *f,
4191                          struct mlx5_eswitch_rep *in_rep,
4192                          struct mlx5_core_dev *in_mdev)
4193 {
4194         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
4195         struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
4196
4197         mlx5e_flow_attr_init(attr, parse_attr, f);
4198
4199         esw_attr->in_rep = in_rep;
4200         esw_attr->in_mdev = in_mdev;
4201
4202         if (MLX5_CAP_ESW(esw->dev, counter_eswitch_affinity) ==
4203             MLX5_COUNTER_SOURCE_ESWITCH)
4204                 esw_attr->counter_dev = in_mdev;
4205         else
4206                 esw_attr->counter_dev = priv->mdev;
4207 }
4208
4209 static struct mlx5e_tc_flow *
4210 __mlx5e_add_fdb_flow(struct mlx5e_priv *priv,
4211                      struct flow_cls_offload *f,
4212                      unsigned long flow_flags,
4213                      struct net_device *filter_dev,
4214                      struct mlx5_eswitch_rep *in_rep,
4215                      struct mlx5_core_dev *in_mdev)
4216 {
4217         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
4218         struct netlink_ext_ack *extack = f->common.extack;
4219         struct mlx5e_tc_flow_parse_attr *parse_attr;
4220         struct mlx5e_tc_flow *flow;
4221         int attr_size, err;
4222
4223         flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_ESWITCH);
4224         attr_size  = sizeof(struct mlx5_esw_flow_attr);
4225         err = mlx5e_alloc_flow(priv, attr_size, f, flow_flags,
4226                                &parse_attr, &flow);
4227         if (err)
4228                 goto out;
4229
4230         parse_attr->filter_dev = filter_dev;
4231         mlx5e_flow_esw_attr_init(flow->attr,
4232                                  priv, parse_attr,
4233                                  f, in_rep, in_mdev);
4234
4235         err = parse_cls_flower(flow->priv, flow, &parse_attr->spec,
4236                                f, filter_dev);
4237         if (err)
4238                 goto err_free;
4239
4240         /* actions validation depends on parsing the ct matches first */
4241         err = mlx5_tc_ct_match_add(get_ct_priv(priv), &parse_attr->spec, f,
4242                                    &flow->attr->ct_attr, extack);
4243         if (err)
4244                 goto err_free;
4245
4246         err = parse_tc_fdb_actions(priv, &rule->action, flow, extack, filter_dev);
4247         if (err)
4248                 goto err_free;
4249
4250         err = mlx5e_tc_add_fdb_flow(priv, flow, extack);
4251         complete_all(&flow->init_done);
4252         if (err) {
4253                 if (!(err == -ENETUNREACH && mlx5_lag_is_multipath(in_mdev)))
4254                         goto err_free;
4255
4256                 add_unready_flow(flow);
4257         }
4258
4259         return flow;
4260
4261 err_free:
4262         mlx5e_flow_put(priv, flow);
4263 out:
4264         return ERR_PTR(err);
4265 }
4266
4267 static int mlx5e_tc_add_fdb_peer_flow(struct flow_cls_offload *f,
4268                                       struct mlx5e_tc_flow *flow,
4269                                       unsigned long flow_flags)
4270 {
4271         struct mlx5e_priv *priv = flow->priv, *peer_priv;
4272         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch, *peer_esw;
4273         struct mlx5_esw_flow_attr *attr = flow->attr->esw_attr;
4274         struct mlx5_devcom *devcom = priv->mdev->priv.devcom;
4275         struct mlx5e_tc_flow_parse_attr *parse_attr;
4276         struct mlx5e_rep_priv *peer_urpriv;
4277         struct mlx5e_tc_flow *peer_flow;
4278         struct mlx5_core_dev *in_mdev;
4279         int err = 0;
4280
4281         peer_esw = mlx5_devcom_get_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
4282         if (!peer_esw)
4283                 return -ENODEV;
4284
4285         peer_urpriv = mlx5_eswitch_get_uplink_priv(peer_esw, REP_ETH);
4286         peer_priv = netdev_priv(peer_urpriv->netdev);
4287
4288         /* in_mdev is assigned of which the packet originated from.
4289          * So packets redirected to uplink use the same mdev of the
4290          * original flow and packets redirected from uplink use the
4291          * peer mdev.
4292          */
4293         if (attr->in_rep->vport == MLX5_VPORT_UPLINK)
4294                 in_mdev = peer_priv->mdev;
4295         else
4296                 in_mdev = priv->mdev;
4297
4298         parse_attr = flow->attr->parse_attr;
4299         peer_flow = __mlx5e_add_fdb_flow(peer_priv, f, flow_flags,
4300                                          parse_attr->filter_dev,
4301                                          attr->in_rep, in_mdev);
4302         if (IS_ERR(peer_flow)) {
4303                 err = PTR_ERR(peer_flow);
4304                 goto out;
4305         }
4306
4307         flow->peer_flow = peer_flow;
4308         flow_flag_set(flow, DUP);
4309         mutex_lock(&esw->offloads.peer_mutex);
4310         list_add_tail(&flow->peer, &esw->offloads.peer_flows);
4311         mutex_unlock(&esw->offloads.peer_mutex);
4312
4313 out:
4314         mlx5_devcom_release_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
4315         return err;
4316 }
4317
4318 static int
4319 mlx5e_add_fdb_flow(struct mlx5e_priv *priv,
4320                    struct flow_cls_offload *f,
4321                    unsigned long flow_flags,
4322                    struct net_device *filter_dev,
4323                    struct mlx5e_tc_flow **__flow)
4324 {
4325         struct mlx5e_rep_priv *rpriv = priv->ppriv;
4326         struct mlx5_eswitch_rep *in_rep = rpriv->rep;
4327         struct mlx5_core_dev *in_mdev = priv->mdev;
4328         struct mlx5e_tc_flow *flow;
4329         int err;
4330
4331         flow = __mlx5e_add_fdb_flow(priv, f, flow_flags, filter_dev, in_rep,
4332                                     in_mdev);
4333         if (IS_ERR(flow))
4334                 return PTR_ERR(flow);
4335
4336         if (is_peer_flow_needed(flow)) {
4337                 err = mlx5e_tc_add_fdb_peer_flow(f, flow, flow_flags);
4338                 if (err) {
4339                         mlx5e_tc_del_fdb_flow(priv, flow);
4340                         goto out;
4341                 }
4342         }
4343
4344         *__flow = flow;
4345
4346         return 0;
4347
4348 out:
4349         return err;
4350 }
4351
4352 static int
4353 mlx5e_add_nic_flow(struct mlx5e_priv *priv,
4354                    struct flow_cls_offload *f,
4355                    unsigned long flow_flags,
4356                    struct net_device *filter_dev,
4357                    struct mlx5e_tc_flow **__flow)
4358 {
4359         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
4360         struct netlink_ext_ack *extack = f->common.extack;
4361         struct mlx5e_tc_flow_parse_attr *parse_attr;
4362         struct mlx5e_tc_flow *flow;
4363         int attr_size, err;
4364
4365         if (!MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, ignore_flow_level)) {
4366                 if (!tc_cls_can_offload_and_chain0(priv->netdev, &f->common))
4367                         return -EOPNOTSUPP;
4368         } else if (!tc_can_offload_extack(priv->netdev, f->common.extack)) {
4369                 return -EOPNOTSUPP;
4370         }
4371
4372         flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_NIC);
4373         attr_size  = sizeof(struct mlx5_nic_flow_attr);
4374         err = mlx5e_alloc_flow(priv, attr_size, f, flow_flags,
4375                                &parse_attr, &flow);
4376         if (err)
4377                 goto out;
4378
4379         parse_attr->filter_dev = filter_dev;
4380         mlx5e_flow_attr_init(flow->attr, parse_attr, f);
4381
4382         err = parse_cls_flower(flow->priv, flow, &parse_attr->spec,
4383                                f, filter_dev);
4384         if (err)
4385                 goto err_free;
4386
4387         err = mlx5_tc_ct_match_add(get_ct_priv(priv), &parse_attr->spec, f,
4388                                    &flow->attr->ct_attr, extack);
4389         if (err)
4390                 goto err_free;
4391
4392         err = parse_tc_nic_actions(priv, &rule->action, parse_attr, flow, extack);
4393         if (err)
4394                 goto err_free;
4395
4396         err = mlx5e_tc_add_nic_flow(priv, parse_attr, flow, extack);
4397         if (err)
4398                 goto err_free;
4399
4400         flow_flag_set(flow, OFFLOADED);
4401         *__flow = flow;
4402
4403         return 0;
4404
4405 err_free:
4406         flow_flag_set(flow, FAILED);
4407         dealloc_mod_hdr_actions(&parse_attr->mod_hdr_acts);
4408         mlx5e_flow_put(priv, flow);
4409 out:
4410         return err;
4411 }
4412
4413 static int
4414 mlx5e_tc_add_flow(struct mlx5e_priv *priv,
4415                   struct flow_cls_offload *f,
4416                   unsigned long flags,
4417                   struct net_device *filter_dev,
4418                   struct mlx5e_tc_flow **flow)
4419 {
4420         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
4421         unsigned long flow_flags;
4422         int err;
4423
4424         get_flags(flags, &flow_flags);
4425
4426         if (!tc_can_offload_extack(priv->netdev, f->common.extack))
4427                 return -EOPNOTSUPP;
4428
4429         if (esw && esw->mode == MLX5_ESWITCH_OFFLOADS)
4430                 err = mlx5e_add_fdb_flow(priv, f, flow_flags,
4431                                          filter_dev, flow);
4432         else
4433                 err = mlx5e_add_nic_flow(priv, f, flow_flags,
4434                                          filter_dev, flow);
4435
4436         return err;
4437 }
4438
4439 static bool is_flow_rule_duplicate_allowed(struct net_device *dev,
4440                                            struct mlx5e_rep_priv *rpriv)
4441 {
4442         /* Offloaded flow rule is allowed to duplicate on non-uplink representor
4443          * sharing tc block with other slaves of a lag device. Rpriv can be NULL if this
4444          * function is called from NIC mode.
4445          */
4446         return netif_is_lag_port(dev) && rpriv && rpriv->rep->vport != MLX5_VPORT_UPLINK;
4447 }
4448
4449 int mlx5e_configure_flower(struct net_device *dev, struct mlx5e_priv *priv,
4450                            struct flow_cls_offload *f, unsigned long flags)
4451 {
4452         struct netlink_ext_ack *extack = f->common.extack;
4453         struct rhashtable *tc_ht = get_tc_ht(priv, flags);
4454         struct mlx5e_rep_priv *rpriv = priv->ppriv;
4455         struct mlx5e_tc_flow *flow;
4456         int err = 0;
4457
4458         if (!mlx5_esw_hold(priv->mdev))
4459                 return -EAGAIN;
4460
4461         mlx5_esw_get(priv->mdev);
4462
4463         rcu_read_lock();
4464         flow = rhashtable_lookup(tc_ht, &f->cookie, tc_ht_params);
4465         if (flow) {
4466                 /* Same flow rule offloaded to non-uplink representor sharing tc block,
4467                  * just return 0.
4468                  */
4469                 if (is_flow_rule_duplicate_allowed(dev, rpriv) && flow->orig_dev != dev)
4470                         goto rcu_unlock;
4471
4472                 NL_SET_ERR_MSG_MOD(extack,
4473                                    "flow cookie already exists, ignoring");
4474                 netdev_warn_once(priv->netdev,
4475                                  "flow cookie %lx already exists, ignoring\n",
4476                                  f->cookie);
4477                 err = -EEXIST;
4478                 goto rcu_unlock;
4479         }
4480 rcu_unlock:
4481         rcu_read_unlock();
4482         if (flow)
4483                 goto out;
4484
4485         trace_mlx5e_configure_flower(f);
4486         err = mlx5e_tc_add_flow(priv, f, flags, dev, &flow);
4487         if (err)
4488                 goto out;
4489
4490         /* Flow rule offloaded to non-uplink representor sharing tc block,
4491          * set the flow's owner dev.
4492          */
4493         if (is_flow_rule_duplicate_allowed(dev, rpriv))
4494                 flow->orig_dev = dev;
4495
4496         err = rhashtable_lookup_insert_fast(tc_ht, &flow->node, tc_ht_params);
4497         if (err)
4498                 goto err_free;
4499
4500         mlx5_esw_release(priv->mdev);
4501         return 0;
4502
4503 err_free:
4504         mlx5e_flow_put(priv, flow);
4505 out:
4506         mlx5_esw_put(priv->mdev);
4507         mlx5_esw_release(priv->mdev);
4508         return err;
4509 }
4510
4511 static bool same_flow_direction(struct mlx5e_tc_flow *flow, int flags)
4512 {
4513         bool dir_ingress = !!(flags & MLX5_TC_FLAG(INGRESS));
4514         bool dir_egress = !!(flags & MLX5_TC_FLAG(EGRESS));
4515
4516         return flow_flag_test(flow, INGRESS) == dir_ingress &&
4517                 flow_flag_test(flow, EGRESS) == dir_egress;
4518 }
4519
4520 int mlx5e_delete_flower(struct net_device *dev, struct mlx5e_priv *priv,
4521                         struct flow_cls_offload *f, unsigned long flags)
4522 {
4523         struct rhashtable *tc_ht = get_tc_ht(priv, flags);
4524         struct mlx5e_tc_flow *flow;
4525         int err;
4526
4527         rcu_read_lock();
4528         flow = rhashtable_lookup(tc_ht, &f->cookie, tc_ht_params);
4529         if (!flow || !same_flow_direction(flow, flags)) {
4530                 err = -EINVAL;
4531                 goto errout;
4532         }
4533
4534         /* Only delete the flow if it doesn't have MLX5E_TC_FLOW_DELETED flag
4535          * set.
4536          */
4537         if (flow_flag_test_and_set(flow, DELETED)) {
4538                 err = -EINVAL;
4539                 goto errout;
4540         }
4541         rhashtable_remove_fast(tc_ht, &flow->node, tc_ht_params);
4542         rcu_read_unlock();
4543
4544         trace_mlx5e_delete_flower(f);
4545         mlx5e_flow_put(priv, flow);
4546
4547         mlx5_esw_put(priv->mdev);
4548         return 0;
4549
4550 errout:
4551         rcu_read_unlock();
4552         return err;
4553 }
4554
4555 int mlx5e_stats_flower(struct net_device *dev, struct mlx5e_priv *priv,
4556                        struct flow_cls_offload *f, unsigned long flags)
4557 {
4558         struct mlx5_devcom *devcom = priv->mdev->priv.devcom;
4559         struct rhashtable *tc_ht = get_tc_ht(priv, flags);
4560         struct mlx5_eswitch *peer_esw;
4561         struct mlx5e_tc_flow *flow;
4562         struct mlx5_fc *counter;
4563         u64 lastuse = 0;
4564         u64 packets = 0;
4565         u64 bytes = 0;
4566         int err = 0;
4567
4568         rcu_read_lock();
4569         flow = mlx5e_flow_get(rhashtable_lookup(tc_ht, &f->cookie,
4570                                                 tc_ht_params));
4571         rcu_read_unlock();
4572         if (IS_ERR(flow))
4573                 return PTR_ERR(flow);
4574
4575         if (!same_flow_direction(flow, flags)) {
4576                 err = -EINVAL;
4577                 goto errout;
4578         }
4579
4580         if (mlx5e_is_offloaded_flow(flow) || flow_flag_test(flow, CT)) {
4581                 counter = mlx5e_tc_get_counter(flow);
4582                 if (!counter)
4583                         goto errout;
4584
4585                 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
4586         }
4587
4588         /* Under multipath it's possible for one rule to be currently
4589          * un-offloaded while the other rule is offloaded.
4590          */
4591         peer_esw = mlx5_devcom_get_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
4592         if (!peer_esw)
4593                 goto out;
4594
4595         if (flow_flag_test(flow, DUP) &&
4596             flow_flag_test(flow->peer_flow, OFFLOADED)) {
4597                 u64 bytes2;
4598                 u64 packets2;
4599                 u64 lastuse2;
4600
4601                 counter = mlx5e_tc_get_counter(flow->peer_flow);
4602                 if (!counter)
4603                         goto no_peer_counter;
4604                 mlx5_fc_query_cached(counter, &bytes2, &packets2, &lastuse2);
4605
4606                 bytes += bytes2;
4607                 packets += packets2;
4608                 lastuse = max_t(u64, lastuse, lastuse2);
4609         }
4610
4611 no_peer_counter:
4612         mlx5_devcom_release_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
4613 out:
4614         flow_stats_update(&f->stats, bytes, packets, 0, lastuse,
4615                           FLOW_ACTION_HW_STATS_DELAYED);
4616         trace_mlx5e_stats_flower(f);
4617 errout:
4618         mlx5e_flow_put(priv, flow);
4619         return err;
4620 }
4621
4622 static int apply_police_params(struct mlx5e_priv *priv, u64 rate,
4623                                struct netlink_ext_ack *extack)
4624 {
4625         struct mlx5e_rep_priv *rpriv = priv->ppriv;
4626         struct mlx5_eswitch *esw;
4627         u32 rate_mbps = 0;
4628         u16 vport_num;
4629         int err;
4630
4631         vport_num = rpriv->rep->vport;
4632         if (vport_num >= MLX5_VPORT_ECPF) {
4633                 NL_SET_ERR_MSG_MOD(extack,
4634                                    "Ingress rate limit is supported only for Eswitch ports connected to VFs");
4635                 return -EOPNOTSUPP;
4636         }
4637
4638         esw = priv->mdev->priv.eswitch;
4639         /* rate is given in bytes/sec.
4640          * First convert to bits/sec and then round to the nearest mbit/secs.
4641          * mbit means million bits.
4642          * Moreover, if rate is non zero we choose to configure to a minimum of
4643          * 1 mbit/sec.
4644          */
4645         if (rate) {
4646                 rate = (rate * BITS_PER_BYTE) + 500000;
4647                 do_div(rate, 1000000);
4648                 rate_mbps = max_t(u32, rate, 1);
4649         }
4650
4651         err = mlx5_esw_modify_vport_rate(esw, vport_num, rate_mbps);
4652         if (err)
4653                 NL_SET_ERR_MSG_MOD(extack, "failed applying action to hardware");
4654
4655         return err;
4656 }
4657
4658 static int scan_tc_matchall_fdb_actions(struct mlx5e_priv *priv,
4659                                         struct flow_action *flow_action,
4660                                         struct netlink_ext_ack *extack)
4661 {
4662         struct mlx5e_rep_priv *rpriv = priv->ppriv;
4663         const struct flow_action_entry *act;
4664         int err;
4665         int i;
4666
4667         if (!flow_action_has_entries(flow_action)) {
4668                 NL_SET_ERR_MSG_MOD(extack, "matchall called with no action");
4669                 return -EINVAL;
4670         }
4671
4672         if (!flow_offload_has_one_action(flow_action)) {
4673                 NL_SET_ERR_MSG_MOD(extack, "matchall policing support only a single action");
4674                 return -EOPNOTSUPP;
4675         }
4676
4677         if (!flow_action_basic_hw_stats_check(flow_action, extack))
4678                 return -EOPNOTSUPP;
4679
4680         flow_action_for_each(i, act, flow_action) {
4681                 switch (act->id) {
4682                 case FLOW_ACTION_POLICE:
4683                         if (act->police.rate_pkt_ps) {
4684                                 NL_SET_ERR_MSG_MOD(extack, "QoS offload not support packets per second");
4685                                 return -EOPNOTSUPP;
4686                         }
4687                         err = apply_police_params(priv, act->police.rate_bytes_ps, extack);
4688                         if (err)
4689                                 return err;
4690
4691                         rpriv->prev_vf_vport_stats = priv->stats.vf_vport;
4692                         break;
4693                 default:
4694                         NL_SET_ERR_MSG_MOD(extack, "mlx5 supports only police action for matchall");
4695                         return -EOPNOTSUPP;
4696                 }
4697         }
4698
4699         return 0;
4700 }
4701
4702 int mlx5e_tc_configure_matchall(struct mlx5e_priv *priv,
4703                                 struct tc_cls_matchall_offload *ma)
4704 {
4705         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
4706         struct netlink_ext_ack *extack = ma->common.extack;
4707
4708         if (!mlx5_esw_qos_enabled(esw)) {
4709                 NL_SET_ERR_MSG_MOD(extack, "QoS is not supported on this device");
4710                 return -EOPNOTSUPP;
4711         }
4712
4713         if (ma->common.prio != 1) {
4714                 NL_SET_ERR_MSG_MOD(extack, "only priority 1 is supported");
4715                 return -EINVAL;
4716         }
4717
4718         return scan_tc_matchall_fdb_actions(priv, &ma->rule->action, extack);
4719 }
4720
4721 int mlx5e_tc_delete_matchall(struct mlx5e_priv *priv,
4722                              struct tc_cls_matchall_offload *ma)
4723 {
4724         struct netlink_ext_ack *extack = ma->common.extack;
4725
4726         return apply_police_params(priv, 0, extack);
4727 }
4728
4729 void mlx5e_tc_stats_matchall(struct mlx5e_priv *priv,
4730                              struct tc_cls_matchall_offload *ma)
4731 {
4732         struct mlx5e_rep_priv *rpriv = priv->ppriv;
4733         struct rtnl_link_stats64 cur_stats;
4734         u64 dbytes;
4735         u64 dpkts;
4736
4737         cur_stats = priv->stats.vf_vport;
4738         dpkts = cur_stats.rx_packets - rpriv->prev_vf_vport_stats.rx_packets;
4739         dbytes = cur_stats.rx_bytes - rpriv->prev_vf_vport_stats.rx_bytes;
4740         rpriv->prev_vf_vport_stats = cur_stats;
4741         flow_stats_update(&ma->stats, dbytes, dpkts, 0, jiffies,
4742                           FLOW_ACTION_HW_STATS_DELAYED);
4743 }
4744
4745 static void mlx5e_tc_hairpin_update_dead_peer(struct mlx5e_priv *priv,
4746                                               struct mlx5e_priv *peer_priv)
4747 {
4748         struct mlx5_core_dev *peer_mdev = peer_priv->mdev;
4749         struct mlx5e_hairpin_entry *hpe, *tmp;
4750         LIST_HEAD(init_wait_list);
4751         u16 peer_vhca_id;
4752         int bkt;
4753
4754         if (!same_hw_devs(priv, peer_priv))
4755                 return;
4756
4757         peer_vhca_id = MLX5_CAP_GEN(peer_mdev, vhca_id);
4758
4759         mutex_lock(&priv->fs.tc.hairpin_tbl_lock);
4760         hash_for_each(priv->fs.tc.hairpin_tbl, bkt, hpe, hairpin_hlist)
4761                 if (refcount_inc_not_zero(&hpe->refcnt))
4762                         list_add(&hpe->dead_peer_wait_list, &init_wait_list);
4763         mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
4764
4765         list_for_each_entry_safe(hpe, tmp, &init_wait_list, dead_peer_wait_list) {
4766                 wait_for_completion(&hpe->res_ready);
4767                 if (!IS_ERR_OR_NULL(hpe->hp) && hpe->peer_vhca_id == peer_vhca_id)
4768                         mlx5_core_hairpin_clear_dead_peer(hpe->hp->pair);
4769
4770                 mlx5e_hairpin_put(priv, hpe);
4771         }
4772 }
4773
4774 static int mlx5e_tc_netdev_event(struct notifier_block *this,
4775                                  unsigned long event, void *ptr)
4776 {
4777         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4778         struct mlx5e_flow_steering *fs;
4779         struct mlx5e_priv *peer_priv;
4780         struct mlx5e_tc_table *tc;
4781         struct mlx5e_priv *priv;
4782
4783         if (ndev->netdev_ops != &mlx5e_netdev_ops ||
4784             event != NETDEV_UNREGISTER ||
4785             ndev->reg_state == NETREG_REGISTERED)
4786                 return NOTIFY_DONE;
4787
4788         tc = container_of(this, struct mlx5e_tc_table, netdevice_nb);
4789         fs = container_of(tc, struct mlx5e_flow_steering, tc);
4790         priv = container_of(fs, struct mlx5e_priv, fs);
4791         peer_priv = netdev_priv(ndev);
4792         if (priv == peer_priv ||
4793             !(priv->netdev->features & NETIF_F_HW_TC))
4794                 return NOTIFY_DONE;
4795
4796         mlx5e_tc_hairpin_update_dead_peer(priv, peer_priv);
4797
4798         return NOTIFY_DONE;
4799 }
4800
4801 static int mlx5e_tc_nic_get_ft_size(struct mlx5_core_dev *dev)
4802 {
4803         int tc_grp_size, tc_tbl_size;
4804         u32 max_flow_counter;
4805
4806         max_flow_counter = (MLX5_CAP_GEN(dev, max_flow_counter_31_16) << 16) |
4807                             MLX5_CAP_GEN(dev, max_flow_counter_15_0);
4808
4809         tc_grp_size = min_t(int, max_flow_counter, MLX5E_TC_TABLE_MAX_GROUP_SIZE);
4810
4811         tc_tbl_size = min_t(int, tc_grp_size * MLX5E_TC_TABLE_NUM_GROUPS,
4812                             BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev, log_max_ft_size)));
4813
4814         return tc_tbl_size;
4815 }
4816
4817 int mlx5e_tc_nic_init(struct mlx5e_priv *priv)
4818 {
4819         struct mlx5e_tc_table *tc = &priv->fs.tc;
4820         struct mlx5_core_dev *dev = priv->mdev;
4821         struct mapping_ctx *chains_mapping;
4822         struct mlx5_chains_attr attr = {};
4823         int err;
4824
4825         mlx5e_mod_hdr_tbl_init(&tc->mod_hdr);
4826         mutex_init(&tc->t_lock);
4827         mutex_init(&tc->hairpin_tbl_lock);
4828         hash_init(tc->hairpin_tbl);
4829
4830         err = rhashtable_init(&tc->ht, &tc_ht_params);
4831         if (err)
4832                 return err;
4833
4834         lockdep_set_class(&tc->ht.mutex, &tc_ht_lock_key);
4835
4836         chains_mapping = mapping_create(sizeof(struct mlx5_mapped_obj),
4837                                         MLX5E_TC_TABLE_CHAIN_TAG_MASK, true);
4838         if (IS_ERR(chains_mapping)) {
4839                 err = PTR_ERR(chains_mapping);
4840                 goto err_mapping;
4841         }
4842         tc->mapping = chains_mapping;
4843
4844         if (MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, ignore_flow_level))
4845                 attr.flags = MLX5_CHAINS_AND_PRIOS_SUPPORTED |
4846                         MLX5_CHAINS_IGNORE_FLOW_LEVEL_SUPPORTED;
4847         attr.ns = MLX5_FLOW_NAMESPACE_KERNEL;
4848         attr.max_ft_sz = mlx5e_tc_nic_get_ft_size(dev);
4849         attr.max_grp_num = MLX5E_TC_TABLE_NUM_GROUPS;
4850         attr.default_ft = mlx5e_vlan_get_flowtable(priv->fs.vlan);
4851         attr.mapping = chains_mapping;
4852
4853         tc->chains = mlx5_chains_create(dev, &attr);
4854         if (IS_ERR(tc->chains)) {
4855                 err = PTR_ERR(tc->chains);
4856                 goto err_chains;
4857         }
4858
4859         tc->ct = mlx5_tc_ct_init(priv, tc->chains, &priv->fs.tc.mod_hdr,
4860                                  MLX5_FLOW_NAMESPACE_KERNEL);
4861
4862         tc->netdevice_nb.notifier_call = mlx5e_tc_netdev_event;
4863         err = register_netdevice_notifier_dev_net(priv->netdev,
4864                                                   &tc->netdevice_nb,
4865                                                   &tc->netdevice_nn);
4866         if (err) {
4867                 tc->netdevice_nb.notifier_call = NULL;
4868                 mlx5_core_warn(priv->mdev, "Failed to register netdev notifier\n");
4869                 goto err_reg;
4870         }
4871
4872         return 0;
4873
4874 err_reg:
4875         mlx5_tc_ct_clean(tc->ct);
4876         mlx5_chains_destroy(tc->chains);
4877 err_chains:
4878         mapping_destroy(chains_mapping);
4879 err_mapping:
4880         rhashtable_destroy(&tc->ht);
4881         return err;
4882 }
4883
4884 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
4885 {
4886         struct mlx5e_tc_flow *flow = ptr;
4887         struct mlx5e_priv *priv = flow->priv;
4888
4889         mlx5e_tc_del_flow(priv, flow);
4890         kfree(flow);
4891 }
4892
4893 void mlx5e_tc_nic_cleanup(struct mlx5e_priv *priv)
4894 {
4895         struct mlx5e_tc_table *tc = &priv->fs.tc;
4896
4897         if (tc->netdevice_nb.notifier_call)
4898                 unregister_netdevice_notifier_dev_net(priv->netdev,
4899                                                       &tc->netdevice_nb,
4900                                                       &tc->netdevice_nn);
4901
4902         mlx5e_mod_hdr_tbl_destroy(&tc->mod_hdr);
4903         mutex_destroy(&tc->hairpin_tbl_lock);
4904
4905         rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, NULL);
4906
4907         if (!IS_ERR_OR_NULL(tc->t)) {
4908                 mlx5_chains_put_table(tc->chains, 0, 1, MLX5E_TC_FT_LEVEL);
4909                 tc->t = NULL;
4910         }
4911         mutex_destroy(&tc->t_lock);
4912
4913         mlx5_tc_ct_clean(tc->ct);
4914         mapping_destroy(tc->mapping);
4915         mlx5_chains_destroy(tc->chains);
4916 }
4917
4918 int mlx5e_tc_esw_init(struct rhashtable *tc_ht)
4919 {
4920         const size_t sz_enc_opts = sizeof(struct tunnel_match_enc_opts);
4921         struct mlx5_rep_uplink_priv *uplink_priv;
4922         struct mlx5e_rep_priv *rpriv;
4923         struct mapping_ctx *mapping;
4924         struct mlx5_eswitch *esw;
4925         struct mlx5e_priv *priv;
4926         int err = 0;
4927
4928         uplink_priv = container_of(tc_ht, struct mlx5_rep_uplink_priv, tc_ht);
4929         rpriv = container_of(uplink_priv, struct mlx5e_rep_priv, uplink_priv);
4930         priv = netdev_priv(rpriv->netdev);
4931         esw = priv->mdev->priv.eswitch;
4932
4933         uplink_priv->ct_priv = mlx5_tc_ct_init(netdev_priv(priv->netdev),
4934                                                esw_chains(esw),
4935                                                &esw->offloads.mod_hdr,
4936                                                MLX5_FLOW_NAMESPACE_FDB);
4937
4938 #if IS_ENABLED(CONFIG_MLX5_TC_SAMPLE)
4939         uplink_priv->esw_psample = mlx5_esw_sample_init(netdev_priv(priv->netdev));
4940 #endif
4941
4942         mapping = mapping_create(sizeof(struct tunnel_match_key),
4943                                  TUNNEL_INFO_BITS_MASK, true);
4944         if (IS_ERR(mapping)) {
4945                 err = PTR_ERR(mapping);
4946                 goto err_tun_mapping;
4947         }
4948         uplink_priv->tunnel_mapping = mapping;
4949
4950         /* 0xFFF is reserved for stack devices slow path table mark */
4951         mapping = mapping_create(sz_enc_opts, ENC_OPTS_BITS_MASK - 1, true);
4952         if (IS_ERR(mapping)) {
4953                 err = PTR_ERR(mapping);
4954                 goto err_enc_opts_mapping;
4955         }
4956         uplink_priv->tunnel_enc_opts_mapping = mapping;
4957
4958         err = rhashtable_init(tc_ht, &tc_ht_params);
4959         if (err)
4960                 goto err_ht_init;
4961
4962         lockdep_set_class(&tc_ht->mutex, &tc_ht_lock_key);
4963
4964         uplink_priv->encap = mlx5e_tc_tun_init(priv);
4965         if (IS_ERR(uplink_priv->encap)) {
4966                 err = PTR_ERR(uplink_priv->encap);
4967                 goto err_register_fib_notifier;
4968         }
4969
4970         return 0;
4971
4972 err_register_fib_notifier:
4973         rhashtable_destroy(tc_ht);
4974 err_ht_init:
4975         mapping_destroy(uplink_priv->tunnel_enc_opts_mapping);
4976 err_enc_opts_mapping:
4977         mapping_destroy(uplink_priv->tunnel_mapping);
4978 err_tun_mapping:
4979 #if IS_ENABLED(CONFIG_MLX5_TC_SAMPLE)
4980         mlx5_esw_sample_cleanup(uplink_priv->esw_psample);
4981 #endif
4982         mlx5_tc_ct_clean(uplink_priv->ct_priv);
4983         netdev_warn(priv->netdev,
4984                     "Failed to initialize tc (eswitch), err: %d", err);
4985         return err;
4986 }
4987
4988 void mlx5e_tc_esw_cleanup(struct rhashtable *tc_ht)
4989 {
4990         struct mlx5_rep_uplink_priv *uplink_priv;
4991
4992         uplink_priv = container_of(tc_ht, struct mlx5_rep_uplink_priv, tc_ht);
4993
4994         rhashtable_free_and_destroy(tc_ht, _mlx5e_tc_del_flow, NULL);
4995         mlx5e_tc_tun_cleanup(uplink_priv->encap);
4996
4997         mapping_destroy(uplink_priv->tunnel_enc_opts_mapping);
4998         mapping_destroy(uplink_priv->tunnel_mapping);
4999
5000 #if IS_ENABLED(CONFIG_MLX5_TC_SAMPLE)
5001         mlx5_esw_sample_cleanup(uplink_priv->esw_psample);
5002 #endif
5003         mlx5_tc_ct_clean(uplink_priv->ct_priv);
5004 }
5005
5006 int mlx5e_tc_num_filters(struct mlx5e_priv *priv, unsigned long flags)
5007 {
5008         struct rhashtable *tc_ht = get_tc_ht(priv, flags);
5009
5010         return atomic_read(&tc_ht->nelems);
5011 }
5012
5013 void mlx5e_tc_clean_fdb_peer_flows(struct mlx5_eswitch *esw)
5014 {
5015         struct mlx5e_tc_flow *flow, *tmp;
5016
5017         list_for_each_entry_safe(flow, tmp, &esw->offloads.peer_flows, peer)
5018                 __mlx5e_tc_del_fdb_peer_flow(flow);
5019 }
5020
5021 void mlx5e_tc_reoffload_flows_work(struct work_struct *work)
5022 {
5023         struct mlx5_rep_uplink_priv *rpriv =
5024                 container_of(work, struct mlx5_rep_uplink_priv,
5025                              reoffload_flows_work);
5026         struct mlx5e_tc_flow *flow, *tmp;
5027
5028         mutex_lock(&rpriv->unready_flows_lock);
5029         list_for_each_entry_safe(flow, tmp, &rpriv->unready_flows, unready) {
5030                 if (!mlx5e_tc_add_fdb_flow(flow->priv, flow, NULL))
5031                         unready_flow_del(flow);
5032         }
5033         mutex_unlock(&rpriv->unready_flows_lock);
5034 }
5035
5036 static int mlx5e_setup_tc_cls_flower(struct mlx5e_priv *priv,
5037                                      struct flow_cls_offload *cls_flower,
5038                                      unsigned long flags)
5039 {
5040         switch (cls_flower->command) {
5041         case FLOW_CLS_REPLACE:
5042                 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
5043                                               flags);
5044         case FLOW_CLS_DESTROY:
5045                 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
5046                                            flags);
5047         case FLOW_CLS_STATS:
5048                 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
5049                                           flags);
5050         default:
5051                 return -EOPNOTSUPP;
5052         }
5053 }
5054
5055 int mlx5e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
5056                             void *cb_priv)
5057 {
5058         unsigned long flags = MLX5_TC_FLAG(INGRESS);
5059         struct mlx5e_priv *priv = cb_priv;
5060
5061         if (!priv->netdev || !netif_device_present(priv->netdev))
5062                 return -EOPNOTSUPP;
5063
5064         if (mlx5e_is_uplink_rep(priv))
5065                 flags |= MLX5_TC_FLAG(ESW_OFFLOAD);
5066         else
5067                 flags |= MLX5_TC_FLAG(NIC_OFFLOAD);
5068
5069         switch (type) {
5070         case TC_SETUP_CLSFLOWER:
5071                 return mlx5e_setup_tc_cls_flower(priv, type_data, flags);
5072         default:
5073                 return -EOPNOTSUPP;
5074         }
5075 }
5076
5077 bool mlx5e_tc_update_skb(struct mlx5_cqe64 *cqe,
5078                          struct sk_buff *skb)
5079 {
5080 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
5081         u32 chain = 0, chain_tag, reg_b, zone_restore_id;
5082         struct mlx5e_priv *priv = netdev_priv(skb->dev);
5083         struct mlx5e_tc_table *tc = &priv->fs.tc;
5084         struct mlx5_mapped_obj mapped_obj;
5085         struct tc_skb_ext *tc_skb_ext;
5086         int err;
5087
5088         reg_b = be32_to_cpu(cqe->ft_metadata);
5089
5090         chain_tag = reg_b & MLX5E_TC_TABLE_CHAIN_TAG_MASK;
5091
5092         err = mapping_find(tc->mapping, chain_tag, &mapped_obj);
5093         if (err) {
5094                 netdev_dbg(priv->netdev,
5095                            "Couldn't find chain for chain tag: %d, err: %d\n",
5096                            chain_tag, err);
5097                 return false;
5098         }
5099
5100         if (mapped_obj.type == MLX5_MAPPED_OBJ_CHAIN) {
5101                 chain = mapped_obj.chain;
5102                 tc_skb_ext = tc_skb_ext_alloc(skb);
5103                 if (WARN_ON(!tc_skb_ext))
5104                         return false;
5105
5106                 tc_skb_ext->chain = chain;
5107
5108                 zone_restore_id = (reg_b >> REG_MAPPING_SHIFT(NIC_ZONE_RESTORE_TO_REG)) &
5109                         ESW_ZONE_ID_MASK;
5110
5111                 if (!mlx5e_tc_ct_restore_flow(tc->ct, skb,
5112                                               zone_restore_id))
5113                         return false;
5114         } else {
5115                 netdev_dbg(priv->netdev, "Invalid mapped object type: %d\n", mapped_obj.type);
5116                 return false;
5117         }
5118 #endif /* CONFIG_NET_TC_SKB_EXT */
5119
5120         return true;
5121 }