Merge branch 'next-lockdown' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[platform/kernel/linux-starfive.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_tc.c
1 /*
2  * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <net/flow_dissector.h>
34 #include <net/sch_generic.h>
35 #include <net/pkt_cls.h>
36 #include <net/tc_act/tc_gact.h>
37 #include <net/tc_act/tc_skbedit.h>
38 #include <linux/mlx5/fs.h>
39 #include <linux/mlx5/device.h>
40 #include <linux/rhashtable.h>
41 #include <linux/refcount.h>
42 #include <linux/completion.h>
43 #include <net/tc_act/tc_mirred.h>
44 #include <net/tc_act/tc_vlan.h>
45 #include <net/tc_act/tc_tunnel_key.h>
46 #include <net/tc_act/tc_pedit.h>
47 #include <net/tc_act/tc_csum.h>
48 #include <net/arp.h>
49 #include <net/ipv6_stubs.h>
50 #include "en.h"
51 #include "en_rep.h"
52 #include "en_tc.h"
53 #include "eswitch.h"
54 #include "fs_core.h"
55 #include "en/port.h"
56 #include "en/tc_tun.h"
57 #include "lib/devcom.h"
58 #include "lib/geneve.h"
59 #include "diag/en_tc_tracepoint.h"
60
61 struct mlx5_nic_flow_attr {
62         u32 action;
63         u32 flow_tag;
64         struct mlx5_modify_hdr *modify_hdr;
65         u32 hairpin_tirn;
66         u8 match_level;
67         struct mlx5_flow_table  *hairpin_ft;
68         struct mlx5_fc          *counter;
69 };
70
71 #define MLX5E_TC_FLOW_BASE (MLX5E_TC_FLAG_LAST_EXPORTED_BIT + 1)
72
73 enum {
74         MLX5E_TC_FLOW_FLAG_INGRESS      = MLX5E_TC_FLAG_INGRESS_BIT,
75         MLX5E_TC_FLOW_FLAG_EGRESS       = MLX5E_TC_FLAG_EGRESS_BIT,
76         MLX5E_TC_FLOW_FLAG_ESWITCH      = MLX5E_TC_FLAG_ESW_OFFLOAD_BIT,
77         MLX5E_TC_FLOW_FLAG_NIC          = MLX5E_TC_FLAG_NIC_OFFLOAD_BIT,
78         MLX5E_TC_FLOW_FLAG_OFFLOADED    = MLX5E_TC_FLOW_BASE,
79         MLX5E_TC_FLOW_FLAG_HAIRPIN      = MLX5E_TC_FLOW_BASE + 1,
80         MLX5E_TC_FLOW_FLAG_HAIRPIN_RSS  = MLX5E_TC_FLOW_BASE + 2,
81         MLX5E_TC_FLOW_FLAG_SLOW         = MLX5E_TC_FLOW_BASE + 3,
82         MLX5E_TC_FLOW_FLAG_DUP          = MLX5E_TC_FLOW_BASE + 4,
83         MLX5E_TC_FLOW_FLAG_NOT_READY    = MLX5E_TC_FLOW_BASE + 5,
84         MLX5E_TC_FLOW_FLAG_DELETED      = MLX5E_TC_FLOW_BASE + 6,
85 };
86
87 #define MLX5E_TC_MAX_SPLITS 1
88
89 /* Helper struct for accessing a struct containing list_head array.
90  * Containing struct
91  *   |- Helper array
92  *      [0] Helper item 0
93  *          |- list_head item 0
94  *          |- index (0)
95  *      [1] Helper item 1
96  *          |- list_head item 1
97  *          |- index (1)
98  * To access the containing struct from one of the list_head items:
99  * 1. Get the helper item from the list_head item using
100  *    helper item =
101  *        container_of(list_head item, helper struct type, list_head field)
102  * 2. Get the contining struct from the helper item and its index in the array:
103  *    containing struct =
104  *        container_of(helper item, containing struct type, helper field[index])
105  */
106 struct encap_flow_item {
107         struct mlx5e_encap_entry *e; /* attached encap instance */
108         struct list_head list;
109         int index;
110 };
111
112 struct mlx5e_tc_flow {
113         struct rhash_head       node;
114         struct mlx5e_priv       *priv;
115         u64                     cookie;
116         unsigned long           flags;
117         struct mlx5_flow_handle *rule[MLX5E_TC_MAX_SPLITS + 1];
118         /* Flow can be associated with multiple encap IDs.
119          * The number of encaps is bounded by the number of supported
120          * destinations.
121          */
122         struct encap_flow_item encaps[MLX5_MAX_FLOW_FWD_VPORTS];
123         struct mlx5e_tc_flow    *peer_flow;
124         struct mlx5e_mod_hdr_entry *mh; /* attached mod header instance */
125         struct list_head        mod_hdr; /* flows sharing the same mod hdr ID */
126         struct mlx5e_hairpin_entry *hpe; /* attached hairpin instance */
127         struct list_head        hairpin; /* flows sharing the same hairpin */
128         struct list_head        peer;    /* flows with peer flow */
129         struct list_head        unready; /* flows not ready to be offloaded (e.g due to missing route) */
130         int                     tmp_efi_index;
131         struct list_head        tmp_list; /* temporary flow list used by neigh update */
132         refcount_t              refcnt;
133         struct rcu_head         rcu_head;
134         struct completion       init_done;
135         union {
136                 struct mlx5_esw_flow_attr esw_attr[0];
137                 struct mlx5_nic_flow_attr nic_attr[0];
138         };
139 };
140
141 struct mlx5e_tc_flow_parse_attr {
142         const struct ip_tunnel_info *tun_info[MLX5_MAX_FLOW_FWD_VPORTS];
143         struct net_device *filter_dev;
144         struct mlx5_flow_spec spec;
145         int num_mod_hdr_actions;
146         int max_mod_hdr_actions;
147         void *mod_hdr_actions;
148         int mirred_ifindex[MLX5_MAX_FLOW_FWD_VPORTS];
149 };
150
151 #define MLX5E_TC_TABLE_NUM_GROUPS 4
152 #define MLX5E_TC_TABLE_MAX_GROUP_SIZE BIT(16)
153
154 struct mlx5e_hairpin {
155         struct mlx5_hairpin *pair;
156
157         struct mlx5_core_dev *func_mdev;
158         struct mlx5e_priv *func_priv;
159         u32 tdn;
160         u32 tirn;
161
162         int num_channels;
163         struct mlx5e_rqt indir_rqt;
164         u32 indir_tirn[MLX5E_NUM_INDIR_TIRS];
165         struct mlx5e_ttc_table ttc;
166 };
167
168 struct mlx5e_hairpin_entry {
169         /* a node of a hash table which keeps all the  hairpin entries */
170         struct hlist_node hairpin_hlist;
171
172         /* protects flows list */
173         spinlock_t flows_lock;
174         /* flows sharing the same hairpin */
175         struct list_head flows;
176         /* hpe's that were not fully initialized when dead peer update event
177          * function traversed them.
178          */
179         struct list_head dead_peer_wait_list;
180
181         u16 peer_vhca_id;
182         u8 prio;
183         struct mlx5e_hairpin *hp;
184         refcount_t refcnt;
185         struct completion res_ready;
186 };
187
188 struct mod_hdr_key {
189         int num_actions;
190         void *actions;
191 };
192
193 struct mlx5e_mod_hdr_entry {
194         /* a node of a hash table which keeps all the mod_hdr entries */
195         struct hlist_node mod_hdr_hlist;
196
197         /* protects flows list */
198         spinlock_t flows_lock;
199         /* flows sharing the same mod_hdr entry */
200         struct list_head flows;
201
202         struct mod_hdr_key key;
203
204         struct mlx5_modify_hdr *modify_hdr;
205
206         refcount_t refcnt;
207         struct completion res_ready;
208         int compl_result;
209 };
210
211 #define MLX5_MH_ACT_SZ MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto)
212
213 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
214                               struct mlx5e_tc_flow *flow);
215
216 static struct mlx5e_tc_flow *mlx5e_flow_get(struct mlx5e_tc_flow *flow)
217 {
218         if (!flow || !refcount_inc_not_zero(&flow->refcnt))
219                 return ERR_PTR(-EINVAL);
220         return flow;
221 }
222
223 static void mlx5e_flow_put(struct mlx5e_priv *priv,
224                            struct mlx5e_tc_flow *flow)
225 {
226         if (refcount_dec_and_test(&flow->refcnt)) {
227                 mlx5e_tc_del_flow(priv, flow);
228                 kfree_rcu(flow, rcu_head);
229         }
230 }
231
232 static void __flow_flag_set(struct mlx5e_tc_flow *flow, unsigned long flag)
233 {
234         /* Complete all memory stores before setting bit. */
235         smp_mb__before_atomic();
236         set_bit(flag, &flow->flags);
237 }
238
239 #define flow_flag_set(flow, flag) __flow_flag_set(flow, MLX5E_TC_FLOW_FLAG_##flag)
240
241 static bool __flow_flag_test_and_set(struct mlx5e_tc_flow *flow,
242                                      unsigned long flag)
243 {
244         /* test_and_set_bit() provides all necessary barriers */
245         return test_and_set_bit(flag, &flow->flags);
246 }
247
248 #define flow_flag_test_and_set(flow, flag)                      \
249         __flow_flag_test_and_set(flow,                          \
250                                  MLX5E_TC_FLOW_FLAG_##flag)
251
252 static void __flow_flag_clear(struct mlx5e_tc_flow *flow, unsigned long flag)
253 {
254         /* Complete all memory stores before clearing bit. */
255         smp_mb__before_atomic();
256         clear_bit(flag, &flow->flags);
257 }
258
259 #define flow_flag_clear(flow, flag) __flow_flag_clear(flow, \
260                                                       MLX5E_TC_FLOW_FLAG_##flag)
261
262 static bool __flow_flag_test(struct mlx5e_tc_flow *flow, unsigned long flag)
263 {
264         bool ret = test_bit(flag, &flow->flags);
265
266         /* Read fields of flow structure only after checking flags. */
267         smp_mb__after_atomic();
268         return ret;
269 }
270
271 #define flow_flag_test(flow, flag) __flow_flag_test(flow, \
272                                                     MLX5E_TC_FLOW_FLAG_##flag)
273
274 static bool mlx5e_is_eswitch_flow(struct mlx5e_tc_flow *flow)
275 {
276         return flow_flag_test(flow, ESWITCH);
277 }
278
279 static bool mlx5e_is_offloaded_flow(struct mlx5e_tc_flow *flow)
280 {
281         return flow_flag_test(flow, OFFLOADED);
282 }
283
284 static inline u32 hash_mod_hdr_info(struct mod_hdr_key *key)
285 {
286         return jhash(key->actions,
287                      key->num_actions * MLX5_MH_ACT_SZ, 0);
288 }
289
290 static inline int cmp_mod_hdr_info(struct mod_hdr_key *a,
291                                    struct mod_hdr_key *b)
292 {
293         if (a->num_actions != b->num_actions)
294                 return 1;
295
296         return memcmp(a->actions, b->actions, a->num_actions * MLX5_MH_ACT_SZ);
297 }
298
299 static struct mod_hdr_tbl *
300 get_mod_hdr_table(struct mlx5e_priv *priv, int namespace)
301 {
302         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
303
304         return namespace == MLX5_FLOW_NAMESPACE_FDB ? &esw->offloads.mod_hdr :
305                 &priv->fs.tc.mod_hdr;
306 }
307
308 static struct mlx5e_mod_hdr_entry *
309 mlx5e_mod_hdr_get(struct mod_hdr_tbl *tbl, struct mod_hdr_key *key, u32 hash_key)
310 {
311         struct mlx5e_mod_hdr_entry *mh, *found = NULL;
312
313         hash_for_each_possible(tbl->hlist, mh, mod_hdr_hlist, hash_key) {
314                 if (!cmp_mod_hdr_info(&mh->key, key)) {
315                         refcount_inc(&mh->refcnt);
316                         found = mh;
317                         break;
318                 }
319         }
320
321         return found;
322 }
323
324 static void mlx5e_mod_hdr_put(struct mlx5e_priv *priv,
325                               struct mlx5e_mod_hdr_entry *mh,
326                               int namespace)
327 {
328         struct mod_hdr_tbl *tbl = get_mod_hdr_table(priv, namespace);
329
330         if (!refcount_dec_and_mutex_lock(&mh->refcnt, &tbl->lock))
331                 return;
332         hash_del(&mh->mod_hdr_hlist);
333         mutex_unlock(&tbl->lock);
334
335         WARN_ON(!list_empty(&mh->flows));
336         if (mh->compl_result > 0)
337                 mlx5_modify_header_dealloc(priv->mdev, mh->modify_hdr);
338
339         kfree(mh);
340 }
341
342 static int get_flow_name_space(struct mlx5e_tc_flow *flow)
343 {
344         return mlx5e_is_eswitch_flow(flow) ?
345                 MLX5_FLOW_NAMESPACE_FDB : MLX5_FLOW_NAMESPACE_KERNEL;
346 }
347 static int mlx5e_attach_mod_hdr(struct mlx5e_priv *priv,
348                                 struct mlx5e_tc_flow *flow,
349                                 struct mlx5e_tc_flow_parse_attr *parse_attr)
350 {
351         int num_actions, actions_size, namespace, err;
352         struct mlx5e_mod_hdr_entry *mh;
353         struct mod_hdr_tbl *tbl;
354         struct mod_hdr_key key;
355         u32 hash_key;
356
357         num_actions  = parse_attr->num_mod_hdr_actions;
358         actions_size = MLX5_MH_ACT_SZ * num_actions;
359
360         key.actions = parse_attr->mod_hdr_actions;
361         key.num_actions = num_actions;
362
363         hash_key = hash_mod_hdr_info(&key);
364
365         namespace = get_flow_name_space(flow);
366         tbl = get_mod_hdr_table(priv, namespace);
367
368         mutex_lock(&tbl->lock);
369         mh = mlx5e_mod_hdr_get(tbl, &key, hash_key);
370         if (mh) {
371                 mutex_unlock(&tbl->lock);
372                 wait_for_completion(&mh->res_ready);
373
374                 if (mh->compl_result < 0) {
375                         err = -EREMOTEIO;
376                         goto attach_header_err;
377                 }
378                 goto attach_flow;
379         }
380
381         mh = kzalloc(sizeof(*mh) + actions_size, GFP_KERNEL);
382         if (!mh) {
383                 mutex_unlock(&tbl->lock);
384                 return -ENOMEM;
385         }
386
387         mh->key.actions = (void *)mh + sizeof(*mh);
388         memcpy(mh->key.actions, key.actions, actions_size);
389         mh->key.num_actions = num_actions;
390         spin_lock_init(&mh->flows_lock);
391         INIT_LIST_HEAD(&mh->flows);
392         refcount_set(&mh->refcnt, 1);
393         init_completion(&mh->res_ready);
394
395         hash_add(tbl->hlist, &mh->mod_hdr_hlist, hash_key);
396         mutex_unlock(&tbl->lock);
397
398         mh->modify_hdr = mlx5_modify_header_alloc(priv->mdev, namespace,
399                                                   mh->key.num_actions,
400                                                   mh->key.actions);
401         if (IS_ERR(mh->modify_hdr)) {
402                 err = PTR_ERR(mh->modify_hdr);
403                 mh->compl_result = err;
404                 goto alloc_header_err;
405         }
406         mh->compl_result = 1;
407         complete_all(&mh->res_ready);
408
409 attach_flow:
410         flow->mh = mh;
411         spin_lock(&mh->flows_lock);
412         list_add(&flow->mod_hdr, &mh->flows);
413         spin_unlock(&mh->flows_lock);
414         if (mlx5e_is_eswitch_flow(flow))
415                 flow->esw_attr->modify_hdr = mh->modify_hdr;
416         else
417                 flow->nic_attr->modify_hdr = mh->modify_hdr;
418
419         return 0;
420
421 alloc_header_err:
422         complete_all(&mh->res_ready);
423 attach_header_err:
424         mlx5e_mod_hdr_put(priv, mh, namespace);
425         return err;
426 }
427
428 static void mlx5e_detach_mod_hdr(struct mlx5e_priv *priv,
429                                  struct mlx5e_tc_flow *flow)
430 {
431         /* flow wasn't fully initialized */
432         if (!flow->mh)
433                 return;
434
435         spin_lock(&flow->mh->flows_lock);
436         list_del(&flow->mod_hdr);
437         spin_unlock(&flow->mh->flows_lock);
438
439         mlx5e_mod_hdr_put(priv, flow->mh, get_flow_name_space(flow));
440         flow->mh = NULL;
441 }
442
443 static
444 struct mlx5_core_dev *mlx5e_hairpin_get_mdev(struct net *net, int ifindex)
445 {
446         struct net_device *netdev;
447         struct mlx5e_priv *priv;
448
449         netdev = __dev_get_by_index(net, ifindex);
450         priv = netdev_priv(netdev);
451         return priv->mdev;
452 }
453
454 static int mlx5e_hairpin_create_transport(struct mlx5e_hairpin *hp)
455 {
456         u32 in[MLX5_ST_SZ_DW(create_tir_in)] = {0};
457         void *tirc;
458         int err;
459
460         err = mlx5_core_alloc_transport_domain(hp->func_mdev, &hp->tdn);
461         if (err)
462                 goto alloc_tdn_err;
463
464         tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
465
466         MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
467         MLX5_SET(tirc, tirc, inline_rqn, hp->pair->rqn[0]);
468         MLX5_SET(tirc, tirc, transport_domain, hp->tdn);
469
470         err = mlx5_core_create_tir(hp->func_mdev, in, MLX5_ST_SZ_BYTES(create_tir_in), &hp->tirn);
471         if (err)
472                 goto create_tir_err;
473
474         return 0;
475
476 create_tir_err:
477         mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
478 alloc_tdn_err:
479         return err;
480 }
481
482 static void mlx5e_hairpin_destroy_transport(struct mlx5e_hairpin *hp)
483 {
484         mlx5_core_destroy_tir(hp->func_mdev, hp->tirn);
485         mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
486 }
487
488 static void mlx5e_hairpin_fill_rqt_rqns(struct mlx5e_hairpin *hp, void *rqtc)
489 {
490         u32 indirection_rqt[MLX5E_INDIR_RQT_SIZE], rqn;
491         struct mlx5e_priv *priv = hp->func_priv;
492         int i, ix, sz = MLX5E_INDIR_RQT_SIZE;
493
494         mlx5e_build_default_indir_rqt(indirection_rqt, sz,
495                                       hp->num_channels);
496
497         for (i = 0; i < sz; i++) {
498                 ix = i;
499                 if (priv->rss_params.hfunc == ETH_RSS_HASH_XOR)
500                         ix = mlx5e_bits_invert(i, ilog2(sz));
501                 ix = indirection_rqt[ix];
502                 rqn = hp->pair->rqn[ix];
503                 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
504         }
505 }
506
507 static int mlx5e_hairpin_create_indirect_rqt(struct mlx5e_hairpin *hp)
508 {
509         int inlen, err, sz = MLX5E_INDIR_RQT_SIZE;
510         struct mlx5e_priv *priv = hp->func_priv;
511         struct mlx5_core_dev *mdev = priv->mdev;
512         void *rqtc;
513         u32 *in;
514
515         inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
516         in = kvzalloc(inlen, GFP_KERNEL);
517         if (!in)
518                 return -ENOMEM;
519
520         rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
521
522         MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
523         MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
524
525         mlx5e_hairpin_fill_rqt_rqns(hp, rqtc);
526
527         err = mlx5_core_create_rqt(mdev, in, inlen, &hp->indir_rqt.rqtn);
528         if (!err)
529                 hp->indir_rqt.enabled = true;
530
531         kvfree(in);
532         return err;
533 }
534
535 static int mlx5e_hairpin_create_indirect_tirs(struct mlx5e_hairpin *hp)
536 {
537         struct mlx5e_priv *priv = hp->func_priv;
538         u32 in[MLX5_ST_SZ_DW(create_tir_in)];
539         int tt, i, err;
540         void *tirc;
541
542         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
543                 struct mlx5e_tirc_config ttconfig = mlx5e_tirc_get_default_config(tt);
544
545                 memset(in, 0, MLX5_ST_SZ_BYTES(create_tir_in));
546                 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
547
548                 MLX5_SET(tirc, tirc, transport_domain, hp->tdn);
549                 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
550                 MLX5_SET(tirc, tirc, indirect_table, hp->indir_rqt.rqtn);
551                 mlx5e_build_indir_tir_ctx_hash(&priv->rss_params, &ttconfig, tirc, false);
552
553                 err = mlx5_core_create_tir(hp->func_mdev, in,
554                                            MLX5_ST_SZ_BYTES(create_tir_in), &hp->indir_tirn[tt]);
555                 if (err) {
556                         mlx5_core_warn(hp->func_mdev, "create indirect tirs failed, %d\n", err);
557                         goto err_destroy_tirs;
558                 }
559         }
560         return 0;
561
562 err_destroy_tirs:
563         for (i = 0; i < tt; i++)
564                 mlx5_core_destroy_tir(hp->func_mdev, hp->indir_tirn[i]);
565         return err;
566 }
567
568 static void mlx5e_hairpin_destroy_indirect_tirs(struct mlx5e_hairpin *hp)
569 {
570         int tt;
571
572         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
573                 mlx5_core_destroy_tir(hp->func_mdev, hp->indir_tirn[tt]);
574 }
575
576 static void mlx5e_hairpin_set_ttc_params(struct mlx5e_hairpin *hp,
577                                          struct ttc_params *ttc_params)
578 {
579         struct mlx5_flow_table_attr *ft_attr = &ttc_params->ft_attr;
580         int tt;
581
582         memset(ttc_params, 0, sizeof(*ttc_params));
583
584         ttc_params->any_tt_tirn = hp->tirn;
585
586         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
587                 ttc_params->indir_tirn[tt] = hp->indir_tirn[tt];
588
589         ft_attr->max_fte = MLX5E_NUM_TT;
590         ft_attr->level = MLX5E_TC_TTC_FT_LEVEL;
591         ft_attr->prio = MLX5E_TC_PRIO;
592 }
593
594 static int mlx5e_hairpin_rss_init(struct mlx5e_hairpin *hp)
595 {
596         struct mlx5e_priv *priv = hp->func_priv;
597         struct ttc_params ttc_params;
598         int err;
599
600         err = mlx5e_hairpin_create_indirect_rqt(hp);
601         if (err)
602                 return err;
603
604         err = mlx5e_hairpin_create_indirect_tirs(hp);
605         if (err)
606                 goto err_create_indirect_tirs;
607
608         mlx5e_hairpin_set_ttc_params(hp, &ttc_params);
609         err = mlx5e_create_ttc_table(priv, &ttc_params, &hp->ttc);
610         if (err)
611                 goto err_create_ttc_table;
612
613         netdev_dbg(priv->netdev, "add hairpin: using %d channels rss ttc table id %x\n",
614                    hp->num_channels, hp->ttc.ft.t->id);
615
616         return 0;
617
618 err_create_ttc_table:
619         mlx5e_hairpin_destroy_indirect_tirs(hp);
620 err_create_indirect_tirs:
621         mlx5e_destroy_rqt(priv, &hp->indir_rqt);
622
623         return err;
624 }
625
626 static void mlx5e_hairpin_rss_cleanup(struct mlx5e_hairpin *hp)
627 {
628         struct mlx5e_priv *priv = hp->func_priv;
629
630         mlx5e_destroy_ttc_table(priv, &hp->ttc);
631         mlx5e_hairpin_destroy_indirect_tirs(hp);
632         mlx5e_destroy_rqt(priv, &hp->indir_rqt);
633 }
634
635 static struct mlx5e_hairpin *
636 mlx5e_hairpin_create(struct mlx5e_priv *priv, struct mlx5_hairpin_params *params,
637                      int peer_ifindex)
638 {
639         struct mlx5_core_dev *func_mdev, *peer_mdev;
640         struct mlx5e_hairpin *hp;
641         struct mlx5_hairpin *pair;
642         int err;
643
644         hp = kzalloc(sizeof(*hp), GFP_KERNEL);
645         if (!hp)
646                 return ERR_PTR(-ENOMEM);
647
648         func_mdev = priv->mdev;
649         peer_mdev = mlx5e_hairpin_get_mdev(dev_net(priv->netdev), peer_ifindex);
650
651         pair = mlx5_core_hairpin_create(func_mdev, peer_mdev, params);
652         if (IS_ERR(pair)) {
653                 err = PTR_ERR(pair);
654                 goto create_pair_err;
655         }
656         hp->pair = pair;
657         hp->func_mdev = func_mdev;
658         hp->func_priv = priv;
659         hp->num_channels = params->num_channels;
660
661         err = mlx5e_hairpin_create_transport(hp);
662         if (err)
663                 goto create_transport_err;
664
665         if (hp->num_channels > 1) {
666                 err = mlx5e_hairpin_rss_init(hp);
667                 if (err)
668                         goto rss_init_err;
669         }
670
671         return hp;
672
673 rss_init_err:
674         mlx5e_hairpin_destroy_transport(hp);
675 create_transport_err:
676         mlx5_core_hairpin_destroy(hp->pair);
677 create_pair_err:
678         kfree(hp);
679         return ERR_PTR(err);
680 }
681
682 static void mlx5e_hairpin_destroy(struct mlx5e_hairpin *hp)
683 {
684         if (hp->num_channels > 1)
685                 mlx5e_hairpin_rss_cleanup(hp);
686         mlx5e_hairpin_destroy_transport(hp);
687         mlx5_core_hairpin_destroy(hp->pair);
688         kvfree(hp);
689 }
690
691 static inline u32 hash_hairpin_info(u16 peer_vhca_id, u8 prio)
692 {
693         return (peer_vhca_id << 16 | prio);
694 }
695
696 static struct mlx5e_hairpin_entry *mlx5e_hairpin_get(struct mlx5e_priv *priv,
697                                                      u16 peer_vhca_id, u8 prio)
698 {
699         struct mlx5e_hairpin_entry *hpe;
700         u32 hash_key = hash_hairpin_info(peer_vhca_id, prio);
701
702         hash_for_each_possible(priv->fs.tc.hairpin_tbl, hpe,
703                                hairpin_hlist, hash_key) {
704                 if (hpe->peer_vhca_id == peer_vhca_id && hpe->prio == prio) {
705                         refcount_inc(&hpe->refcnt);
706                         return hpe;
707                 }
708         }
709
710         return NULL;
711 }
712
713 static void mlx5e_hairpin_put(struct mlx5e_priv *priv,
714                               struct mlx5e_hairpin_entry *hpe)
715 {
716         /* no more hairpin flows for us, release the hairpin pair */
717         if (!refcount_dec_and_mutex_lock(&hpe->refcnt, &priv->fs.tc.hairpin_tbl_lock))
718                 return;
719         hash_del(&hpe->hairpin_hlist);
720         mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
721
722         if (!IS_ERR_OR_NULL(hpe->hp)) {
723                 netdev_dbg(priv->netdev, "del hairpin: peer %s\n",
724                            dev_name(hpe->hp->pair->peer_mdev->device));
725
726                 mlx5e_hairpin_destroy(hpe->hp);
727         }
728
729         WARN_ON(!list_empty(&hpe->flows));
730         kfree(hpe);
731 }
732
733 #define UNKNOWN_MATCH_PRIO 8
734
735 static int mlx5e_hairpin_get_prio(struct mlx5e_priv *priv,
736                                   struct mlx5_flow_spec *spec, u8 *match_prio,
737                                   struct netlink_ext_ack *extack)
738 {
739         void *headers_c, *headers_v;
740         u8 prio_val, prio_mask = 0;
741         bool vlan_present;
742
743 #ifdef CONFIG_MLX5_CORE_EN_DCB
744         if (priv->dcbx_dp.trust_state != MLX5_QPTS_TRUST_PCP) {
745                 NL_SET_ERR_MSG_MOD(extack,
746                                    "only PCP trust state supported for hairpin");
747                 return -EOPNOTSUPP;
748         }
749 #endif
750         headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, outer_headers);
751         headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
752
753         vlan_present = MLX5_GET(fte_match_set_lyr_2_4, headers_v, cvlan_tag);
754         if (vlan_present) {
755                 prio_mask = MLX5_GET(fte_match_set_lyr_2_4, headers_c, first_prio);
756                 prio_val = MLX5_GET(fte_match_set_lyr_2_4, headers_v, first_prio);
757         }
758
759         if (!vlan_present || !prio_mask) {
760                 prio_val = UNKNOWN_MATCH_PRIO;
761         } else if (prio_mask != 0x7) {
762                 NL_SET_ERR_MSG_MOD(extack,
763                                    "masked priority match not supported for hairpin");
764                 return -EOPNOTSUPP;
765         }
766
767         *match_prio = prio_val;
768         return 0;
769 }
770
771 static int mlx5e_hairpin_flow_add(struct mlx5e_priv *priv,
772                                   struct mlx5e_tc_flow *flow,
773                                   struct mlx5e_tc_flow_parse_attr *parse_attr,
774                                   struct netlink_ext_ack *extack)
775 {
776         int peer_ifindex = parse_attr->mirred_ifindex[0];
777         struct mlx5_hairpin_params params;
778         struct mlx5_core_dev *peer_mdev;
779         struct mlx5e_hairpin_entry *hpe;
780         struct mlx5e_hairpin *hp;
781         u64 link_speed64;
782         u32 link_speed;
783         u8 match_prio;
784         u16 peer_id;
785         int err;
786
787         peer_mdev = mlx5e_hairpin_get_mdev(dev_net(priv->netdev), peer_ifindex);
788         if (!MLX5_CAP_GEN(priv->mdev, hairpin) || !MLX5_CAP_GEN(peer_mdev, hairpin)) {
789                 NL_SET_ERR_MSG_MOD(extack, "hairpin is not supported");
790                 return -EOPNOTSUPP;
791         }
792
793         peer_id = MLX5_CAP_GEN(peer_mdev, vhca_id);
794         err = mlx5e_hairpin_get_prio(priv, &parse_attr->spec, &match_prio,
795                                      extack);
796         if (err)
797                 return err;
798
799         mutex_lock(&priv->fs.tc.hairpin_tbl_lock);
800         hpe = mlx5e_hairpin_get(priv, peer_id, match_prio);
801         if (hpe) {
802                 mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
803                 wait_for_completion(&hpe->res_ready);
804
805                 if (IS_ERR(hpe->hp)) {
806                         err = -EREMOTEIO;
807                         goto out_err;
808                 }
809                 goto attach_flow;
810         }
811
812         hpe = kzalloc(sizeof(*hpe), GFP_KERNEL);
813         if (!hpe) {
814                 mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
815                 return -ENOMEM;
816         }
817
818         spin_lock_init(&hpe->flows_lock);
819         INIT_LIST_HEAD(&hpe->flows);
820         INIT_LIST_HEAD(&hpe->dead_peer_wait_list);
821         hpe->peer_vhca_id = peer_id;
822         hpe->prio = match_prio;
823         refcount_set(&hpe->refcnt, 1);
824         init_completion(&hpe->res_ready);
825
826         hash_add(priv->fs.tc.hairpin_tbl, &hpe->hairpin_hlist,
827                  hash_hairpin_info(peer_id, match_prio));
828         mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
829
830         params.log_data_size = 15;
831         params.log_data_size = min_t(u8, params.log_data_size,
832                                      MLX5_CAP_GEN(priv->mdev, log_max_hairpin_wq_data_sz));
833         params.log_data_size = max_t(u8, params.log_data_size,
834                                      MLX5_CAP_GEN(priv->mdev, log_min_hairpin_wq_data_sz));
835
836         params.log_num_packets = params.log_data_size -
837                                  MLX5_MPWRQ_MIN_LOG_STRIDE_SZ(priv->mdev);
838         params.log_num_packets = min_t(u8, params.log_num_packets,
839                                        MLX5_CAP_GEN(priv->mdev, log_max_hairpin_num_packets));
840
841         params.q_counter = priv->q_counter;
842         /* set hairpin pair per each 50Gbs share of the link */
843         mlx5e_port_max_linkspeed(priv->mdev, &link_speed);
844         link_speed = max_t(u32, link_speed, 50000);
845         link_speed64 = link_speed;
846         do_div(link_speed64, 50000);
847         params.num_channels = link_speed64;
848
849         hp = mlx5e_hairpin_create(priv, &params, peer_ifindex);
850         hpe->hp = hp;
851         complete_all(&hpe->res_ready);
852         if (IS_ERR(hp)) {
853                 err = PTR_ERR(hp);
854                 goto out_err;
855         }
856
857         netdev_dbg(priv->netdev, "add hairpin: tirn %x rqn %x peer %s sqn %x prio %d (log) data %d packets %d\n",
858                    hp->tirn, hp->pair->rqn[0],
859                    dev_name(hp->pair->peer_mdev->device),
860                    hp->pair->sqn[0], match_prio, params.log_data_size, params.log_num_packets);
861
862 attach_flow:
863         if (hpe->hp->num_channels > 1) {
864                 flow_flag_set(flow, HAIRPIN_RSS);
865                 flow->nic_attr->hairpin_ft = hpe->hp->ttc.ft.t;
866         } else {
867                 flow->nic_attr->hairpin_tirn = hpe->hp->tirn;
868         }
869
870         flow->hpe = hpe;
871         spin_lock(&hpe->flows_lock);
872         list_add(&flow->hairpin, &hpe->flows);
873         spin_unlock(&hpe->flows_lock);
874
875         return 0;
876
877 out_err:
878         mlx5e_hairpin_put(priv, hpe);
879         return err;
880 }
881
882 static void mlx5e_hairpin_flow_del(struct mlx5e_priv *priv,
883                                    struct mlx5e_tc_flow *flow)
884 {
885         /* flow wasn't fully initialized */
886         if (!flow->hpe)
887                 return;
888
889         spin_lock(&flow->hpe->flows_lock);
890         list_del(&flow->hairpin);
891         spin_unlock(&flow->hpe->flows_lock);
892
893         mlx5e_hairpin_put(priv, flow->hpe);
894         flow->hpe = NULL;
895 }
896
897 static int
898 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
899                       struct mlx5e_tc_flow_parse_attr *parse_attr,
900                       struct mlx5e_tc_flow *flow,
901                       struct netlink_ext_ack *extack)
902 {
903         struct mlx5_flow_context *flow_context = &parse_attr->spec.flow_context;
904         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
905         struct mlx5_core_dev *dev = priv->mdev;
906         struct mlx5_flow_destination dest[2] = {};
907         struct mlx5_flow_act flow_act = {
908                 .action = attr->action,
909                 .flags    = FLOW_ACT_NO_APPEND,
910         };
911         struct mlx5_fc *counter = NULL;
912         int err, dest_ix = 0;
913
914         flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
915         flow_context->flow_tag = attr->flow_tag;
916
917         if (flow_flag_test(flow, HAIRPIN)) {
918                 err = mlx5e_hairpin_flow_add(priv, flow, parse_attr, extack);
919                 if (err)
920                         return err;
921
922                 if (flow_flag_test(flow, HAIRPIN_RSS)) {
923                         dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
924                         dest[dest_ix].ft = attr->hairpin_ft;
925                 } else {
926                         dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_TIR;
927                         dest[dest_ix].tir_num = attr->hairpin_tirn;
928                 }
929                 dest_ix++;
930         } else if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
931                 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
932                 dest[dest_ix].ft = priv->fs.vlan.ft.t;
933                 dest_ix++;
934         }
935
936         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
937                 counter = mlx5_fc_create(dev, true);
938                 if (IS_ERR(counter))
939                         return PTR_ERR(counter);
940
941                 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
942                 dest[dest_ix].counter_id = mlx5_fc_id(counter);
943                 dest_ix++;
944                 attr->counter = counter;
945         }
946
947         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
948                 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
949                 flow_act.modify_hdr = attr->modify_hdr;
950                 kfree(parse_attr->mod_hdr_actions);
951                 if (err)
952                         return err;
953         }
954
955         mutex_lock(&priv->fs.tc.t_lock);
956         if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
957                 int tc_grp_size, tc_tbl_size;
958                 u32 max_flow_counter;
959
960                 max_flow_counter = (MLX5_CAP_GEN(dev, max_flow_counter_31_16) << 16) |
961                                     MLX5_CAP_GEN(dev, max_flow_counter_15_0);
962
963                 tc_grp_size = min_t(int, max_flow_counter, MLX5E_TC_TABLE_MAX_GROUP_SIZE);
964
965                 tc_tbl_size = min_t(int, tc_grp_size * MLX5E_TC_TABLE_NUM_GROUPS,
966                                     BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev, log_max_ft_size)));
967
968                 priv->fs.tc.t =
969                         mlx5_create_auto_grouped_flow_table(priv->fs.ns,
970                                                             MLX5E_TC_PRIO,
971                                                             tc_tbl_size,
972                                                             MLX5E_TC_TABLE_NUM_GROUPS,
973                                                             MLX5E_TC_FT_LEVEL, 0);
974                 if (IS_ERR(priv->fs.tc.t)) {
975                         mutex_unlock(&priv->fs.tc.t_lock);
976                         NL_SET_ERR_MSG_MOD(extack,
977                                            "Failed to create tc offload table\n");
978                         netdev_err(priv->netdev,
979                                    "Failed to create tc offload table\n");
980                         return PTR_ERR(priv->fs.tc.t);
981                 }
982         }
983
984         if (attr->match_level != MLX5_MATCH_NONE)
985                 parse_attr->spec.match_criteria_enable |= MLX5_MATCH_OUTER_HEADERS;
986
987         flow->rule[0] = mlx5_add_flow_rules(priv->fs.tc.t, &parse_attr->spec,
988                                             &flow_act, dest, dest_ix);
989         mutex_unlock(&priv->fs.tc.t_lock);
990
991         return PTR_ERR_OR_ZERO(flow->rule[0]);
992 }
993
994 static void mlx5e_tc_del_nic_flow(struct mlx5e_priv *priv,
995                                   struct mlx5e_tc_flow *flow)
996 {
997         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
998         struct mlx5_fc *counter = NULL;
999
1000         counter = attr->counter;
1001         if (!IS_ERR_OR_NULL(flow->rule[0]))
1002                 mlx5_del_flow_rules(flow->rule[0]);
1003         mlx5_fc_destroy(priv->mdev, counter);
1004
1005         mutex_lock(&priv->fs.tc.t_lock);
1006         if (!mlx5e_tc_num_filters(priv, MLX5_TC_FLAG(NIC_OFFLOAD)) && priv->fs.tc.t) {
1007                 mlx5_destroy_flow_table(priv->fs.tc.t);
1008                 priv->fs.tc.t = NULL;
1009         }
1010         mutex_unlock(&priv->fs.tc.t_lock);
1011
1012         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
1013                 mlx5e_detach_mod_hdr(priv, flow);
1014
1015         if (flow_flag_test(flow, HAIRPIN))
1016                 mlx5e_hairpin_flow_del(priv, flow);
1017 }
1018
1019 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
1020                                struct mlx5e_tc_flow *flow, int out_index);
1021
1022 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
1023                               struct mlx5e_tc_flow *flow,
1024                               struct net_device *mirred_dev,
1025                               int out_index,
1026                               struct netlink_ext_ack *extack,
1027                               struct net_device **encap_dev,
1028                               bool *encap_valid);
1029
1030 static struct mlx5_flow_handle *
1031 mlx5e_tc_offload_fdb_rules(struct mlx5_eswitch *esw,
1032                            struct mlx5e_tc_flow *flow,
1033                            struct mlx5_flow_spec *spec,
1034                            struct mlx5_esw_flow_attr *attr)
1035 {
1036         struct mlx5_flow_handle *rule;
1037
1038         rule = mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
1039         if (IS_ERR(rule))
1040                 return rule;
1041
1042         if (attr->split_count) {
1043                 flow->rule[1] = mlx5_eswitch_add_fwd_rule(esw, spec, attr);
1044                 if (IS_ERR(flow->rule[1])) {
1045                         mlx5_eswitch_del_offloaded_rule(esw, rule, attr);
1046                         return flow->rule[1];
1047                 }
1048         }
1049
1050         return rule;
1051 }
1052
1053 static void
1054 mlx5e_tc_unoffload_fdb_rules(struct mlx5_eswitch *esw,
1055                              struct mlx5e_tc_flow *flow,
1056                            struct mlx5_esw_flow_attr *attr)
1057 {
1058         flow_flag_clear(flow, OFFLOADED);
1059
1060         if (attr->split_count)
1061                 mlx5_eswitch_del_fwd_rule(esw, flow->rule[1], attr);
1062
1063         mlx5_eswitch_del_offloaded_rule(esw, flow->rule[0], attr);
1064 }
1065
1066 static struct mlx5_flow_handle *
1067 mlx5e_tc_offload_to_slow_path(struct mlx5_eswitch *esw,
1068                               struct mlx5e_tc_flow *flow,
1069                               struct mlx5_flow_spec *spec,
1070                               struct mlx5_esw_flow_attr *slow_attr)
1071 {
1072         struct mlx5_flow_handle *rule;
1073
1074         memcpy(slow_attr, flow->esw_attr, sizeof(*slow_attr));
1075         slow_attr->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1076         slow_attr->split_count = 0;
1077         slow_attr->dest_chain = FDB_SLOW_PATH_CHAIN;
1078
1079         rule = mlx5e_tc_offload_fdb_rules(esw, flow, spec, slow_attr);
1080         if (!IS_ERR(rule))
1081                 flow_flag_set(flow, SLOW);
1082
1083         return rule;
1084 }
1085
1086 static void
1087 mlx5e_tc_unoffload_from_slow_path(struct mlx5_eswitch *esw,
1088                                   struct mlx5e_tc_flow *flow,
1089                                   struct mlx5_esw_flow_attr *slow_attr)
1090 {
1091         memcpy(slow_attr, flow->esw_attr, sizeof(*slow_attr));
1092         slow_attr->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1093         slow_attr->split_count = 0;
1094         slow_attr->dest_chain = FDB_SLOW_PATH_CHAIN;
1095         mlx5e_tc_unoffload_fdb_rules(esw, flow, slow_attr);
1096         flow_flag_clear(flow, SLOW);
1097 }
1098
1099 /* Caller must obtain uplink_priv->unready_flows_lock mutex before calling this
1100  * function.
1101  */
1102 static void unready_flow_add(struct mlx5e_tc_flow *flow,
1103                              struct list_head *unready_flows)
1104 {
1105         flow_flag_set(flow, NOT_READY);
1106         list_add_tail(&flow->unready, unready_flows);
1107 }
1108
1109 /* Caller must obtain uplink_priv->unready_flows_lock mutex before calling this
1110  * function.
1111  */
1112 static void unready_flow_del(struct mlx5e_tc_flow *flow)
1113 {
1114         list_del(&flow->unready);
1115         flow_flag_clear(flow, NOT_READY);
1116 }
1117
1118 static void add_unready_flow(struct mlx5e_tc_flow *flow)
1119 {
1120         struct mlx5_rep_uplink_priv *uplink_priv;
1121         struct mlx5e_rep_priv *rpriv;
1122         struct mlx5_eswitch *esw;
1123
1124         esw = flow->priv->mdev->priv.eswitch;
1125         rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
1126         uplink_priv = &rpriv->uplink_priv;
1127
1128         mutex_lock(&uplink_priv->unready_flows_lock);
1129         unready_flow_add(flow, &uplink_priv->unready_flows);
1130         mutex_unlock(&uplink_priv->unready_flows_lock);
1131 }
1132
1133 static void remove_unready_flow(struct mlx5e_tc_flow *flow)
1134 {
1135         struct mlx5_rep_uplink_priv *uplink_priv;
1136         struct mlx5e_rep_priv *rpriv;
1137         struct mlx5_eswitch *esw;
1138
1139         esw = flow->priv->mdev->priv.eswitch;
1140         rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
1141         uplink_priv = &rpriv->uplink_priv;
1142
1143         mutex_lock(&uplink_priv->unready_flows_lock);
1144         unready_flow_del(flow);
1145         mutex_unlock(&uplink_priv->unready_flows_lock);
1146 }
1147
1148 static int
1149 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
1150                       struct mlx5e_tc_flow *flow,
1151                       struct netlink_ext_ack *extack)
1152 {
1153         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1154         u32 max_chain = mlx5_eswitch_get_chain_range(esw);
1155         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
1156         struct mlx5e_tc_flow_parse_attr *parse_attr = attr->parse_attr;
1157         u16 max_prio = mlx5_eswitch_get_prio_range(esw);
1158         struct net_device *out_dev, *encap_dev = NULL;
1159         struct mlx5_fc *counter = NULL;
1160         struct mlx5e_rep_priv *rpriv;
1161         struct mlx5e_priv *out_priv;
1162         bool encap_valid = true;
1163         int err = 0;
1164         int out_index;
1165
1166         if (!mlx5_eswitch_prios_supported(esw) && attr->prio != 1) {
1167                 NL_SET_ERR_MSG(extack, "E-switch priorities unsupported, upgrade FW");
1168                 return -EOPNOTSUPP;
1169         }
1170
1171         if (attr->chain > max_chain) {
1172                 NL_SET_ERR_MSG(extack, "Requested chain is out of supported range");
1173                 return -EOPNOTSUPP;
1174         }
1175
1176         if (attr->prio > max_prio) {
1177                 NL_SET_ERR_MSG(extack, "Requested priority is out of supported range");
1178                 return -EOPNOTSUPP;
1179         }
1180
1181         for (out_index = 0; out_index < MLX5_MAX_FLOW_FWD_VPORTS; out_index++) {
1182                 int mirred_ifindex;
1183
1184                 if (!(attr->dests[out_index].flags & MLX5_ESW_DEST_ENCAP))
1185                         continue;
1186
1187                 mirred_ifindex = parse_attr->mirred_ifindex[out_index];
1188                 out_dev = __dev_get_by_index(dev_net(priv->netdev),
1189                                              mirred_ifindex);
1190                 err = mlx5e_attach_encap(priv, flow, out_dev, out_index,
1191                                          extack, &encap_dev, &encap_valid);
1192                 if (err)
1193                         return err;
1194
1195                 out_priv = netdev_priv(encap_dev);
1196                 rpriv = out_priv->ppriv;
1197                 attr->dests[out_index].rep = rpriv->rep;
1198                 attr->dests[out_index].mdev = out_priv->mdev;
1199         }
1200
1201         err = mlx5_eswitch_add_vlan_action(esw, attr);
1202         if (err)
1203                 return err;
1204
1205         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
1206                 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
1207                 kfree(parse_attr->mod_hdr_actions);
1208                 if (err)
1209                         return err;
1210         }
1211
1212         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
1213                 counter = mlx5_fc_create(attr->counter_dev, true);
1214                 if (IS_ERR(counter))
1215                         return PTR_ERR(counter);
1216
1217                 attr->counter = counter;
1218         }
1219
1220         /* we get here if one of the following takes place:
1221          * (1) there's no error
1222          * (2) there's an encap action and we don't have valid neigh
1223          */
1224         if (!encap_valid) {
1225                 /* continue with goto slow path rule instead */
1226                 struct mlx5_esw_flow_attr slow_attr;
1227
1228                 flow->rule[0] = mlx5e_tc_offload_to_slow_path(esw, flow, &parse_attr->spec, &slow_attr);
1229         } else {
1230                 flow->rule[0] = mlx5e_tc_offload_fdb_rules(esw, flow, &parse_attr->spec, attr);
1231         }
1232
1233         if (IS_ERR(flow->rule[0]))
1234                 return PTR_ERR(flow->rule[0]);
1235         else
1236                 flow_flag_set(flow, OFFLOADED);
1237
1238         return 0;
1239 }
1240
1241 static bool mlx5_flow_has_geneve_opt(struct mlx5e_tc_flow *flow)
1242 {
1243         struct mlx5_flow_spec *spec = &flow->esw_attr->parse_attr->spec;
1244         void *headers_v = MLX5_ADDR_OF(fte_match_param,
1245                                        spec->match_value,
1246                                        misc_parameters_3);
1247         u32 geneve_tlv_opt_0_data = MLX5_GET(fte_match_set_misc3,
1248                                              headers_v,
1249                                              geneve_tlv_option_0_data);
1250
1251         return !!geneve_tlv_opt_0_data;
1252 }
1253
1254 static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
1255                                   struct mlx5e_tc_flow *flow)
1256 {
1257         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1258         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
1259         struct mlx5_esw_flow_attr slow_attr;
1260         int out_index;
1261
1262         if (flow_flag_test(flow, NOT_READY)) {
1263                 remove_unready_flow(flow);
1264                 kvfree(attr->parse_attr);
1265                 return;
1266         }
1267
1268         if (mlx5e_is_offloaded_flow(flow)) {
1269                 if (flow_flag_test(flow, SLOW))
1270                         mlx5e_tc_unoffload_from_slow_path(esw, flow, &slow_attr);
1271                 else
1272                         mlx5e_tc_unoffload_fdb_rules(esw, flow, attr);
1273         }
1274
1275         if (mlx5_flow_has_geneve_opt(flow))
1276                 mlx5_geneve_tlv_option_del(priv->mdev->geneve);
1277
1278         mlx5_eswitch_del_vlan_action(esw, attr);
1279
1280         for (out_index = 0; out_index < MLX5_MAX_FLOW_FWD_VPORTS; out_index++)
1281                 if (attr->dests[out_index].flags & MLX5_ESW_DEST_ENCAP)
1282                         mlx5e_detach_encap(priv, flow, out_index);
1283         kvfree(attr->parse_attr);
1284
1285         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
1286                 mlx5e_detach_mod_hdr(priv, flow);
1287
1288         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
1289                 mlx5_fc_destroy(attr->counter_dev, attr->counter);
1290 }
1291
1292 void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
1293                               struct mlx5e_encap_entry *e,
1294                               struct list_head *flow_list)
1295 {
1296         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1297         struct mlx5_esw_flow_attr slow_attr, *esw_attr;
1298         struct mlx5_flow_handle *rule;
1299         struct mlx5_flow_spec *spec;
1300         struct mlx5e_tc_flow *flow;
1301         int err;
1302
1303         e->pkt_reformat = mlx5_packet_reformat_alloc(priv->mdev,
1304                                                      e->reformat_type,
1305                                                      e->encap_size, e->encap_header,
1306                                                      MLX5_FLOW_NAMESPACE_FDB);
1307         if (IS_ERR(e->pkt_reformat)) {
1308                 mlx5_core_warn(priv->mdev, "Failed to offload cached encapsulation header, %lu\n",
1309                                PTR_ERR(e->pkt_reformat));
1310                 return;
1311         }
1312         e->flags |= MLX5_ENCAP_ENTRY_VALID;
1313         mlx5e_rep_queue_neigh_stats_work(priv);
1314
1315         list_for_each_entry(flow, flow_list, tmp_list) {
1316                 bool all_flow_encaps_valid = true;
1317                 int i;
1318
1319                 if (!mlx5e_is_offloaded_flow(flow))
1320                         continue;
1321                 esw_attr = flow->esw_attr;
1322                 spec = &esw_attr->parse_attr->spec;
1323
1324                 esw_attr->dests[flow->tmp_efi_index].pkt_reformat = e->pkt_reformat;
1325                 esw_attr->dests[flow->tmp_efi_index].flags |= MLX5_ESW_DEST_ENCAP_VALID;
1326                 /* Flow can be associated with multiple encap entries.
1327                  * Before offloading the flow verify that all of them have
1328                  * a valid neighbour.
1329                  */
1330                 for (i = 0; i < MLX5_MAX_FLOW_FWD_VPORTS; i++) {
1331                         if (!(esw_attr->dests[i].flags & MLX5_ESW_DEST_ENCAP))
1332                                 continue;
1333                         if (!(esw_attr->dests[i].flags & MLX5_ESW_DEST_ENCAP_VALID)) {
1334                                 all_flow_encaps_valid = false;
1335                                 break;
1336                         }
1337                 }
1338                 /* Do not offload flows with unresolved neighbors */
1339                 if (!all_flow_encaps_valid)
1340                         continue;
1341                 /* update from slow path rule to encap rule */
1342                 rule = mlx5e_tc_offload_fdb_rules(esw, flow, spec, esw_attr);
1343                 if (IS_ERR(rule)) {
1344                         err = PTR_ERR(rule);
1345                         mlx5_core_warn(priv->mdev, "Failed to update cached encapsulation flow, %d\n",
1346                                        err);
1347                         continue;
1348                 }
1349
1350                 mlx5e_tc_unoffload_from_slow_path(esw, flow, &slow_attr);
1351                 flow->rule[0] = rule;
1352                 /* was unset when slow path rule removed */
1353                 flow_flag_set(flow, OFFLOADED);
1354         }
1355 }
1356
1357 void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
1358                               struct mlx5e_encap_entry *e,
1359                               struct list_head *flow_list)
1360 {
1361         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1362         struct mlx5_esw_flow_attr slow_attr;
1363         struct mlx5_flow_handle *rule;
1364         struct mlx5_flow_spec *spec;
1365         struct mlx5e_tc_flow *flow;
1366         int err;
1367
1368         list_for_each_entry(flow, flow_list, tmp_list) {
1369                 if (!mlx5e_is_offloaded_flow(flow))
1370                         continue;
1371                 spec = &flow->esw_attr->parse_attr->spec;
1372
1373                 /* update from encap rule to slow path rule */
1374                 rule = mlx5e_tc_offload_to_slow_path(esw, flow, spec, &slow_attr);
1375                 /* mark the flow's encap dest as non-valid */
1376                 flow->esw_attr->dests[flow->tmp_efi_index].flags &= ~MLX5_ESW_DEST_ENCAP_VALID;
1377
1378                 if (IS_ERR(rule)) {
1379                         err = PTR_ERR(rule);
1380                         mlx5_core_warn(priv->mdev, "Failed to update slow path (encap) flow, %d\n",
1381                                        err);
1382                         continue;
1383                 }
1384
1385                 mlx5e_tc_unoffload_fdb_rules(esw, flow, flow->esw_attr);
1386                 flow->rule[0] = rule;
1387                 /* was unset when fast path rule removed */
1388                 flow_flag_set(flow, OFFLOADED);
1389         }
1390
1391         /* we know that the encap is valid */
1392         e->flags &= ~MLX5_ENCAP_ENTRY_VALID;
1393         mlx5_packet_reformat_dealloc(priv->mdev, e->pkt_reformat);
1394 }
1395
1396 static struct mlx5_fc *mlx5e_tc_get_counter(struct mlx5e_tc_flow *flow)
1397 {
1398         if (mlx5e_is_eswitch_flow(flow))
1399                 return flow->esw_attr->counter;
1400         else
1401                 return flow->nic_attr->counter;
1402 }
1403
1404 /* Takes reference to all flows attached to encap and adds the flows to
1405  * flow_list using 'tmp_list' list_head in mlx5e_tc_flow.
1406  */
1407 void mlx5e_take_all_encap_flows(struct mlx5e_encap_entry *e, struct list_head *flow_list)
1408 {
1409         struct encap_flow_item *efi;
1410         struct mlx5e_tc_flow *flow;
1411
1412         list_for_each_entry(efi, &e->flows, list) {
1413                 flow = container_of(efi, struct mlx5e_tc_flow, encaps[efi->index]);
1414                 if (IS_ERR(mlx5e_flow_get(flow)))
1415                         continue;
1416                 wait_for_completion(&flow->init_done);
1417
1418                 flow->tmp_efi_index = efi->index;
1419                 list_add(&flow->tmp_list, flow_list);
1420         }
1421 }
1422
1423 /* Iterate over tmp_list of flows attached to flow_list head. */
1424 void mlx5e_put_encap_flow_list(struct mlx5e_priv *priv, struct list_head *flow_list)
1425 {
1426         struct mlx5e_tc_flow *flow, *tmp;
1427
1428         list_for_each_entry_safe(flow, tmp, flow_list, tmp_list)
1429                 mlx5e_flow_put(priv, flow);
1430 }
1431
1432 static struct mlx5e_encap_entry *
1433 mlx5e_get_next_valid_encap(struct mlx5e_neigh_hash_entry *nhe,
1434                            struct mlx5e_encap_entry *e)
1435 {
1436         struct mlx5e_encap_entry *next = NULL;
1437
1438 retry:
1439         rcu_read_lock();
1440
1441         /* find encap with non-zero reference counter value */
1442         for (next = e ?
1443                      list_next_or_null_rcu(&nhe->encap_list,
1444                                            &e->encap_list,
1445                                            struct mlx5e_encap_entry,
1446                                            encap_list) :
1447                      list_first_or_null_rcu(&nhe->encap_list,
1448                                             struct mlx5e_encap_entry,
1449                                             encap_list);
1450              next;
1451              next = list_next_or_null_rcu(&nhe->encap_list,
1452                                           &next->encap_list,
1453                                           struct mlx5e_encap_entry,
1454                                           encap_list))
1455                 if (mlx5e_encap_take(next))
1456                         break;
1457
1458         rcu_read_unlock();
1459
1460         /* release starting encap */
1461         if (e)
1462                 mlx5e_encap_put(netdev_priv(e->out_dev), e);
1463         if (!next)
1464                 return next;
1465
1466         /* wait for encap to be fully initialized */
1467         wait_for_completion(&next->res_ready);
1468         /* continue searching if encap entry is not in valid state after completion */
1469         if (!(next->flags & MLX5_ENCAP_ENTRY_VALID)) {
1470                 e = next;
1471                 goto retry;
1472         }
1473
1474         return next;
1475 }
1476
1477 void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
1478 {
1479         struct mlx5e_neigh *m_neigh = &nhe->m_neigh;
1480         struct mlx5e_encap_entry *e = NULL;
1481         struct mlx5e_tc_flow *flow;
1482         struct mlx5_fc *counter;
1483         struct neigh_table *tbl;
1484         bool neigh_used = false;
1485         struct neighbour *n;
1486         u64 lastuse;
1487
1488         if (m_neigh->family == AF_INET)
1489                 tbl = &arp_tbl;
1490 #if IS_ENABLED(CONFIG_IPV6)
1491         else if (m_neigh->family == AF_INET6)
1492                 tbl = ipv6_stub->nd_tbl;
1493 #endif
1494         else
1495                 return;
1496
1497         /* mlx5e_get_next_valid_encap() releases previous encap before returning
1498          * next one.
1499          */
1500         while ((e = mlx5e_get_next_valid_encap(nhe, e)) != NULL) {
1501                 struct mlx5e_priv *priv = netdev_priv(e->out_dev);
1502                 struct encap_flow_item *efi, *tmp;
1503                 struct mlx5_eswitch *esw;
1504                 LIST_HEAD(flow_list);
1505
1506                 esw = priv->mdev->priv.eswitch;
1507                 mutex_lock(&esw->offloads.encap_tbl_lock);
1508                 list_for_each_entry_safe(efi, tmp, &e->flows, list) {
1509                         flow = container_of(efi, struct mlx5e_tc_flow,
1510                                             encaps[efi->index]);
1511                         if (IS_ERR(mlx5e_flow_get(flow)))
1512                                 continue;
1513                         list_add(&flow->tmp_list, &flow_list);
1514
1515                         if (mlx5e_is_offloaded_flow(flow)) {
1516                                 counter = mlx5e_tc_get_counter(flow);
1517                                 lastuse = mlx5_fc_query_lastuse(counter);
1518                                 if (time_after((unsigned long)lastuse, nhe->reported_lastuse)) {
1519                                         neigh_used = true;
1520                                         break;
1521                                 }
1522                         }
1523                 }
1524                 mutex_unlock(&esw->offloads.encap_tbl_lock);
1525
1526                 mlx5e_put_encap_flow_list(priv, &flow_list);
1527                 if (neigh_used) {
1528                         /* release current encap before breaking the loop */
1529                         mlx5e_encap_put(priv, e);
1530                         break;
1531                 }
1532         }
1533
1534         trace_mlx5e_tc_update_neigh_used_value(nhe, neigh_used);
1535
1536         if (neigh_used) {
1537                 nhe->reported_lastuse = jiffies;
1538
1539                 /* find the relevant neigh according to the cached device and
1540                  * dst ip pair
1541                  */
1542                 n = neigh_lookup(tbl, &m_neigh->dst_ip, m_neigh->dev);
1543                 if (!n)
1544                         return;
1545
1546                 neigh_event_send(n, NULL);
1547                 neigh_release(n);
1548         }
1549 }
1550
1551 static void mlx5e_encap_dealloc(struct mlx5e_priv *priv, struct mlx5e_encap_entry *e)
1552 {
1553         WARN_ON(!list_empty(&e->flows));
1554
1555         if (e->compl_result > 0) {
1556                 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1557
1558                 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
1559                         mlx5_packet_reformat_dealloc(priv->mdev, e->pkt_reformat);
1560         }
1561
1562         kfree(e->encap_header);
1563         kfree_rcu(e, rcu);
1564 }
1565
1566 void mlx5e_encap_put(struct mlx5e_priv *priv, struct mlx5e_encap_entry *e)
1567 {
1568         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1569
1570         if (!refcount_dec_and_mutex_lock(&e->refcnt, &esw->offloads.encap_tbl_lock))
1571                 return;
1572         hash_del_rcu(&e->encap_hlist);
1573         mutex_unlock(&esw->offloads.encap_tbl_lock);
1574
1575         mlx5e_encap_dealloc(priv, e);
1576 }
1577
1578 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
1579                                struct mlx5e_tc_flow *flow, int out_index)
1580 {
1581         struct mlx5e_encap_entry *e = flow->encaps[out_index].e;
1582         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1583
1584         /* flow wasn't fully initialized */
1585         if (!e)
1586                 return;
1587
1588         mutex_lock(&esw->offloads.encap_tbl_lock);
1589         list_del(&flow->encaps[out_index].list);
1590         flow->encaps[out_index].e = NULL;
1591         if (!refcount_dec_and_test(&e->refcnt)) {
1592                 mutex_unlock(&esw->offloads.encap_tbl_lock);
1593                 return;
1594         }
1595         hash_del_rcu(&e->encap_hlist);
1596         mutex_unlock(&esw->offloads.encap_tbl_lock);
1597
1598         mlx5e_encap_dealloc(priv, e);
1599 }
1600
1601 static void __mlx5e_tc_del_fdb_peer_flow(struct mlx5e_tc_flow *flow)
1602 {
1603         struct mlx5_eswitch *esw = flow->priv->mdev->priv.eswitch;
1604
1605         if (!flow_flag_test(flow, ESWITCH) ||
1606             !flow_flag_test(flow, DUP))
1607                 return;
1608
1609         mutex_lock(&esw->offloads.peer_mutex);
1610         list_del(&flow->peer);
1611         mutex_unlock(&esw->offloads.peer_mutex);
1612
1613         flow_flag_clear(flow, DUP);
1614
1615         mlx5e_tc_del_fdb_flow(flow->peer_flow->priv, flow->peer_flow);
1616         kvfree(flow->peer_flow);
1617         flow->peer_flow = NULL;
1618 }
1619
1620 static void mlx5e_tc_del_fdb_peer_flow(struct mlx5e_tc_flow *flow)
1621 {
1622         struct mlx5_core_dev *dev = flow->priv->mdev;
1623         struct mlx5_devcom *devcom = dev->priv.devcom;
1624         struct mlx5_eswitch *peer_esw;
1625
1626         peer_esw = mlx5_devcom_get_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
1627         if (!peer_esw)
1628                 return;
1629
1630         __mlx5e_tc_del_fdb_peer_flow(flow);
1631         mlx5_devcom_release_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
1632 }
1633
1634 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
1635                               struct mlx5e_tc_flow *flow)
1636 {
1637         if (mlx5e_is_eswitch_flow(flow)) {
1638                 mlx5e_tc_del_fdb_peer_flow(flow);
1639                 mlx5e_tc_del_fdb_flow(priv, flow);
1640         } else {
1641                 mlx5e_tc_del_nic_flow(priv, flow);
1642         }
1643 }
1644
1645
1646 static int parse_tunnel_attr(struct mlx5e_priv *priv,
1647                              struct mlx5_flow_spec *spec,
1648                              struct flow_cls_offload *f,
1649                              struct net_device *filter_dev, u8 *match_level)
1650 {
1651         struct netlink_ext_ack *extack = f->common.extack;
1652         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1653                                        outer_headers);
1654         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1655                                        outer_headers);
1656         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1657         int err;
1658
1659         err = mlx5e_tc_tun_parse(filter_dev, priv, spec, f,
1660                                  headers_c, headers_v, match_level);
1661         if (err) {
1662                 NL_SET_ERR_MSG_MOD(extack,
1663                                    "failed to parse tunnel attributes");
1664                 return err;
1665         }
1666
1667         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) {
1668                 struct flow_match_ipv4_addrs match;
1669
1670                 flow_rule_match_enc_ipv4_addrs(rule, &match);
1671                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1672                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
1673                          ntohl(match.mask->src));
1674                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1675                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
1676                          ntohl(match.key->src));
1677
1678                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1679                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
1680                          ntohl(match.mask->dst));
1681                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1682                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
1683                          ntohl(match.key->dst));
1684
1685                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
1686                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IP);
1687         } else if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) {
1688                 struct flow_match_ipv6_addrs match;
1689
1690                 flow_rule_match_enc_ipv6_addrs(rule, &match);
1691                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1692                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
1693                        &match.mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1694                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1695                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
1696                        &match.key->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1697
1698                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1699                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1700                        &match.mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1701                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1702                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1703                        &match.key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1704
1705                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
1706                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IPV6);
1707         }
1708
1709         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
1710                 struct flow_match_ip match;
1711
1712                 flow_rule_match_enc_ip(rule, &match);
1713                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn,
1714                          match.mask->tos & 0x3);
1715                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn,
1716                          match.key->tos & 0x3);
1717
1718                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp,
1719                          match.mask->tos >> 2);
1720                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp,
1721                          match.key->tos  >> 2);
1722
1723                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit,
1724                          match.mask->ttl);
1725                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit,
1726                          match.key->ttl);
1727
1728                 if (match.mask->ttl &&
1729                     !MLX5_CAP_ESW_FLOWTABLE_FDB
1730                         (priv->mdev,
1731                          ft_field_support.outer_ipv4_ttl)) {
1732                         NL_SET_ERR_MSG_MOD(extack,
1733                                            "Matching on TTL is not supported");
1734                         return -EOPNOTSUPP;
1735                 }
1736
1737         }
1738
1739         /* Enforce DMAC when offloading incoming tunneled flows.
1740          * Flow counters require a match on the DMAC.
1741          */
1742         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_47_16);
1743         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_15_0);
1744         ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1745                                      dmac_47_16), priv->netdev->dev_addr);
1746
1747         /* let software handle IP fragments */
1748         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
1749         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
1750
1751         return 0;
1752 }
1753
1754 static void *get_match_headers_criteria(u32 flags,
1755                                         struct mlx5_flow_spec *spec)
1756 {
1757         return (flags & MLX5_FLOW_CONTEXT_ACTION_DECAP) ?
1758                 MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1759                              inner_headers) :
1760                 MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1761                              outer_headers);
1762 }
1763
1764 static void *get_match_headers_value(u32 flags,
1765                                      struct mlx5_flow_spec *spec)
1766 {
1767         return (flags & MLX5_FLOW_CONTEXT_ACTION_DECAP) ?
1768                 MLX5_ADDR_OF(fte_match_param, spec->match_value,
1769                              inner_headers) :
1770                 MLX5_ADDR_OF(fte_match_param, spec->match_value,
1771                              outer_headers);
1772 }
1773
1774 static int __parse_cls_flower(struct mlx5e_priv *priv,
1775                               struct mlx5_flow_spec *spec,
1776                               struct flow_cls_offload *f,
1777                               struct net_device *filter_dev,
1778                               u8 *inner_match_level, u8 *outer_match_level)
1779 {
1780         struct netlink_ext_ack *extack = f->common.extack;
1781         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1782                                        outer_headers);
1783         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1784                                        outer_headers);
1785         void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1786                                     misc_parameters);
1787         void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1788                                     misc_parameters);
1789         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1790         struct flow_dissector *dissector = rule->match.dissector;
1791         u16 addr_type = 0;
1792         u8 ip_proto = 0;
1793         u8 *match_level;
1794
1795         match_level = outer_match_level;
1796
1797         if (dissector->used_keys &
1798             ~(BIT(FLOW_DISSECTOR_KEY_META) |
1799               BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1800               BIT(FLOW_DISSECTOR_KEY_BASIC) |
1801               BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1802               BIT(FLOW_DISSECTOR_KEY_VLAN) |
1803               BIT(FLOW_DISSECTOR_KEY_CVLAN) |
1804               BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1805               BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1806               BIT(FLOW_DISSECTOR_KEY_PORTS) |
1807               BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1808               BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1809               BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1810               BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1811               BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1812               BIT(FLOW_DISSECTOR_KEY_TCP) |
1813               BIT(FLOW_DISSECTOR_KEY_IP)  |
1814               BIT(FLOW_DISSECTOR_KEY_ENC_IP) |
1815               BIT(FLOW_DISSECTOR_KEY_ENC_OPTS))) {
1816                 NL_SET_ERR_MSG_MOD(extack, "Unsupported key");
1817                 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
1818                             dissector->used_keys);
1819                 return -EOPNOTSUPP;
1820         }
1821
1822         if (mlx5e_get_tc_tun(filter_dev)) {
1823                 if (parse_tunnel_attr(priv, spec, f, filter_dev,
1824                                       outer_match_level))
1825                         return -EOPNOTSUPP;
1826
1827                 /* At this point, header pointers should point to the inner
1828                  * headers, outer header were already set by parse_tunnel_attr
1829                  */
1830                 match_level = inner_match_level;
1831                 headers_c = get_match_headers_criteria(MLX5_FLOW_CONTEXT_ACTION_DECAP,
1832                                                        spec);
1833                 headers_v = get_match_headers_value(MLX5_FLOW_CONTEXT_ACTION_DECAP,
1834                                                     spec);
1835         }
1836
1837         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1838                 struct flow_match_basic match;
1839
1840                 flow_rule_match_basic(rule, &match);
1841                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
1842                          ntohs(match.mask->n_proto));
1843                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
1844                          ntohs(match.key->n_proto));
1845
1846                 if (match.mask->n_proto)
1847                         *match_level = MLX5_MATCH_L2;
1848         }
1849         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
1850             is_vlan_dev(filter_dev)) {
1851                 struct flow_dissector_key_vlan filter_dev_mask;
1852                 struct flow_dissector_key_vlan filter_dev_key;
1853                 struct flow_match_vlan match;
1854
1855                 if (is_vlan_dev(filter_dev)) {
1856                         match.key = &filter_dev_key;
1857                         match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
1858                         match.key->vlan_tpid = vlan_dev_vlan_proto(filter_dev);
1859                         match.key->vlan_priority = 0;
1860                         match.mask = &filter_dev_mask;
1861                         memset(match.mask, 0xff, sizeof(*match.mask));
1862                         match.mask->vlan_priority = 0;
1863                 } else {
1864                         flow_rule_match_vlan(rule, &match);
1865                 }
1866                 if (match.mask->vlan_id ||
1867                     match.mask->vlan_priority ||
1868                     match.mask->vlan_tpid) {
1869                         if (match.key->vlan_tpid == htons(ETH_P_8021AD)) {
1870                                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1871                                          svlan_tag, 1);
1872                                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1873                                          svlan_tag, 1);
1874                         } else {
1875                                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1876                                          cvlan_tag, 1);
1877                                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1878                                          cvlan_tag, 1);
1879                         }
1880
1881                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid,
1882                                  match.mask->vlan_id);
1883                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid,
1884                                  match.key->vlan_id);
1885
1886                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio,
1887                                  match.mask->vlan_priority);
1888                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio,
1889                                  match.key->vlan_priority);
1890
1891                         *match_level = MLX5_MATCH_L2;
1892                 }
1893         } else if (*match_level != MLX5_MATCH_NONE) {
1894                 /* cvlan_tag enabled in match criteria and
1895                  * disabled in match value means both S & C tags
1896                  * don't exist (untagged of both)
1897                  */
1898                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
1899                 *match_level = MLX5_MATCH_L2;
1900         }
1901
1902         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
1903                 struct flow_match_vlan match;
1904
1905                 flow_rule_match_cvlan(rule, &match);
1906                 if (match.mask->vlan_id ||
1907                     match.mask->vlan_priority ||
1908                     match.mask->vlan_tpid) {
1909                         if (match.key->vlan_tpid == htons(ETH_P_8021AD)) {
1910                                 MLX5_SET(fte_match_set_misc, misc_c,
1911                                          outer_second_svlan_tag, 1);
1912                                 MLX5_SET(fte_match_set_misc, misc_v,
1913                                          outer_second_svlan_tag, 1);
1914                         } else {
1915                                 MLX5_SET(fte_match_set_misc, misc_c,
1916                                          outer_second_cvlan_tag, 1);
1917                                 MLX5_SET(fte_match_set_misc, misc_v,
1918                                          outer_second_cvlan_tag, 1);
1919                         }
1920
1921                         MLX5_SET(fte_match_set_misc, misc_c, outer_second_vid,
1922                                  match.mask->vlan_id);
1923                         MLX5_SET(fte_match_set_misc, misc_v, outer_second_vid,
1924                                  match.key->vlan_id);
1925                         MLX5_SET(fte_match_set_misc, misc_c, outer_second_prio,
1926                                  match.mask->vlan_priority);
1927                         MLX5_SET(fte_match_set_misc, misc_v, outer_second_prio,
1928                                  match.key->vlan_priority);
1929
1930                         *match_level = MLX5_MATCH_L2;
1931                 }
1932         }
1933
1934         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1935                 struct flow_match_eth_addrs match;
1936
1937                 flow_rule_match_eth_addrs(rule, &match);
1938                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1939                                              dmac_47_16),
1940                                 match.mask->dst);
1941                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1942                                              dmac_47_16),
1943                                 match.key->dst);
1944
1945                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1946                                              smac_47_16),
1947                                 match.mask->src);
1948                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1949                                              smac_47_16),
1950                                 match.key->src);
1951
1952                 if (!is_zero_ether_addr(match.mask->src) ||
1953                     !is_zero_ether_addr(match.mask->dst))
1954                         *match_level = MLX5_MATCH_L2;
1955         }
1956
1957         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
1958                 struct flow_match_control match;
1959
1960                 flow_rule_match_control(rule, &match);
1961                 addr_type = match.key->addr_type;
1962
1963                 /* the HW doesn't support frag first/later */
1964                 if (match.mask->flags & FLOW_DIS_FIRST_FRAG)
1965                         return -EOPNOTSUPP;
1966
1967                 if (match.mask->flags & FLOW_DIS_IS_FRAGMENT) {
1968                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
1969                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
1970                                  match.key->flags & FLOW_DIS_IS_FRAGMENT);
1971
1972                         /* the HW doesn't need L3 inline to match on frag=no */
1973                         if (!(match.key->flags & FLOW_DIS_IS_FRAGMENT))
1974                                 *match_level = MLX5_MATCH_L2;
1975         /* ***  L2 attributes parsing up to here *** */
1976                         else
1977                                 *match_level = MLX5_MATCH_L3;
1978                 }
1979         }
1980
1981         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1982                 struct flow_match_basic match;
1983
1984                 flow_rule_match_basic(rule, &match);
1985                 ip_proto = match.key->ip_proto;
1986
1987                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1988                          match.mask->ip_proto);
1989                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1990                          match.key->ip_proto);
1991
1992                 if (match.mask->ip_proto)
1993                         *match_level = MLX5_MATCH_L3;
1994         }
1995
1996         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1997                 struct flow_match_ipv4_addrs match;
1998
1999                 flow_rule_match_ipv4_addrs(rule, &match);
2000                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2001                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
2002                        &match.mask->src, sizeof(match.mask->src));
2003                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2004                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
2005                        &match.key->src, sizeof(match.key->src));
2006                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2007                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2008                        &match.mask->dst, sizeof(match.mask->dst));
2009                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2010                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2011                        &match.key->dst, sizeof(match.key->dst));
2012
2013                 if (match.mask->src || match.mask->dst)
2014                         *match_level = MLX5_MATCH_L3;
2015         }
2016
2017         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2018                 struct flow_match_ipv6_addrs match;
2019
2020                 flow_rule_match_ipv6_addrs(rule, &match);
2021                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2022                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
2023                        &match.mask->src, sizeof(match.mask->src));
2024                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2025                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
2026                        &match.key->src, sizeof(match.key->src));
2027
2028                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2029                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2030                        &match.mask->dst, sizeof(match.mask->dst));
2031                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2032                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2033                        &match.key->dst, sizeof(match.key->dst));
2034
2035                 if (ipv6_addr_type(&match.mask->src) != IPV6_ADDR_ANY ||
2036                     ipv6_addr_type(&match.mask->dst) != IPV6_ADDR_ANY)
2037                         *match_level = MLX5_MATCH_L3;
2038         }
2039
2040         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
2041                 struct flow_match_ip match;
2042
2043                 flow_rule_match_ip(rule, &match);
2044                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn,
2045                          match.mask->tos & 0x3);
2046                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn,
2047                          match.key->tos & 0x3);
2048
2049                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp,
2050                          match.mask->tos >> 2);
2051                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp,
2052                          match.key->tos  >> 2);
2053
2054                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit,
2055                          match.mask->ttl);
2056                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit,
2057                          match.key->ttl);
2058
2059                 if (match.mask->ttl &&
2060                     !MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
2061                                                 ft_field_support.outer_ipv4_ttl)) {
2062                         NL_SET_ERR_MSG_MOD(extack,
2063                                            "Matching on TTL is not supported");
2064                         return -EOPNOTSUPP;
2065                 }
2066
2067                 if (match.mask->tos || match.mask->ttl)
2068                         *match_level = MLX5_MATCH_L3;
2069         }
2070
2071         /* ***  L3 attributes parsing up to here *** */
2072
2073         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
2074                 struct flow_match_ports match;
2075
2076                 flow_rule_match_ports(rule, &match);
2077                 switch (ip_proto) {
2078                 case IPPROTO_TCP:
2079                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2080                                  tcp_sport, ntohs(match.mask->src));
2081                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2082                                  tcp_sport, ntohs(match.key->src));
2083
2084                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2085                                  tcp_dport, ntohs(match.mask->dst));
2086                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2087                                  tcp_dport, ntohs(match.key->dst));
2088                         break;
2089
2090                 case IPPROTO_UDP:
2091                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2092                                  udp_sport, ntohs(match.mask->src));
2093                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2094                                  udp_sport, ntohs(match.key->src));
2095
2096                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2097                                  udp_dport, ntohs(match.mask->dst));
2098                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2099                                  udp_dport, ntohs(match.key->dst));
2100                         break;
2101                 default:
2102                         NL_SET_ERR_MSG_MOD(extack,
2103                                            "Only UDP and TCP transports are supported for L4 matching");
2104                         netdev_err(priv->netdev,
2105                                    "Only UDP and TCP transport are supported\n");
2106                         return -EINVAL;
2107                 }
2108
2109                 if (match.mask->src || match.mask->dst)
2110                         *match_level = MLX5_MATCH_L4;
2111         }
2112
2113         if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_TCP)) {
2114                 struct flow_match_tcp match;
2115
2116                 flow_rule_match_tcp(rule, &match);
2117                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_flags,
2118                          ntohs(match.mask->flags));
2119                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
2120                          ntohs(match.key->flags));
2121
2122                 if (match.mask->flags)
2123                         *match_level = MLX5_MATCH_L4;
2124         }
2125
2126         return 0;
2127 }
2128
2129 static int parse_cls_flower(struct mlx5e_priv *priv,
2130                             struct mlx5e_tc_flow *flow,
2131                             struct mlx5_flow_spec *spec,
2132                             struct flow_cls_offload *f,
2133                             struct net_device *filter_dev)
2134 {
2135         u8 inner_match_level, outer_match_level, non_tunnel_match_level;
2136         struct netlink_ext_ack *extack = f->common.extack;
2137         struct mlx5_core_dev *dev = priv->mdev;
2138         struct mlx5_eswitch *esw = dev->priv.eswitch;
2139         struct mlx5e_rep_priv *rpriv = priv->ppriv;
2140         struct mlx5_eswitch_rep *rep;
2141         bool is_eswitch_flow;
2142         int err;
2143
2144         inner_match_level = MLX5_MATCH_NONE;
2145         outer_match_level = MLX5_MATCH_NONE;
2146
2147         err = __parse_cls_flower(priv, spec, f, filter_dev, &inner_match_level,
2148                                  &outer_match_level);
2149         non_tunnel_match_level = (inner_match_level == MLX5_MATCH_NONE) ?
2150                                  outer_match_level : inner_match_level;
2151
2152         is_eswitch_flow = mlx5e_is_eswitch_flow(flow);
2153         if (!err && is_eswitch_flow) {
2154                 rep = rpriv->rep;
2155                 if (rep->vport != MLX5_VPORT_UPLINK &&
2156                     (esw->offloads.inline_mode != MLX5_INLINE_MODE_NONE &&
2157                     esw->offloads.inline_mode < non_tunnel_match_level)) {
2158                         NL_SET_ERR_MSG_MOD(extack,
2159                                            "Flow is not offloaded due to min inline setting");
2160                         netdev_warn(priv->netdev,
2161                                     "Flow is not offloaded due to min inline setting, required %d actual %d\n",
2162                                     non_tunnel_match_level, esw->offloads.inline_mode);
2163                         return -EOPNOTSUPP;
2164                 }
2165         }
2166
2167         if (is_eswitch_flow) {
2168                 flow->esw_attr->inner_match_level = inner_match_level;
2169                 flow->esw_attr->outer_match_level = outer_match_level;
2170         } else {
2171                 flow->nic_attr->match_level = non_tunnel_match_level;
2172         }
2173
2174         return err;
2175 }
2176
2177 struct pedit_headers {
2178         struct ethhdr  eth;
2179         struct vlan_hdr vlan;
2180         struct iphdr   ip4;
2181         struct ipv6hdr ip6;
2182         struct tcphdr  tcp;
2183         struct udphdr  udp;
2184 };
2185
2186 struct pedit_headers_action {
2187         struct pedit_headers    vals;
2188         struct pedit_headers    masks;
2189         u32                     pedits;
2190 };
2191
2192 static int pedit_header_offsets[] = {
2193         [FLOW_ACT_MANGLE_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth),
2194         [FLOW_ACT_MANGLE_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4),
2195         [FLOW_ACT_MANGLE_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6),
2196         [FLOW_ACT_MANGLE_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp),
2197         [FLOW_ACT_MANGLE_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp),
2198 };
2199
2200 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype])
2201
2202 static int set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset,
2203                          struct pedit_headers_action *hdrs)
2204 {
2205         u32 *curr_pmask, *curr_pval;
2206
2207         curr_pmask = (u32 *)(pedit_header(&hdrs->masks, hdr_type) + offset);
2208         curr_pval  = (u32 *)(pedit_header(&hdrs->vals, hdr_type) + offset);
2209
2210         if (*curr_pmask & mask)  /* disallow acting twice on the same location */
2211                 goto out_err;
2212
2213         *curr_pmask |= mask;
2214         *curr_pval  |= (val & mask);
2215
2216         return 0;
2217
2218 out_err:
2219         return -EOPNOTSUPP;
2220 }
2221
2222 struct mlx5_fields {
2223         u8  field;
2224         u8  size;
2225         u32 offset;
2226         u32 match_offset;
2227 };
2228
2229 #define OFFLOAD(fw_field, size, field, off, match_field) \
2230                 {MLX5_ACTION_IN_FIELD_OUT_ ## fw_field, size, \
2231                  offsetof(struct pedit_headers, field) + (off), \
2232                  MLX5_BYTE_OFF(fte_match_set_lyr_2_4, match_field)}
2233
2234 /* masked values are the same and there are no rewrites that do not have a
2235  * match.
2236  */
2237 #define SAME_VAL_MASK(type, valp, maskp, matchvalp, matchmaskp) ({ \
2238         type matchmaskx = *(type *)(matchmaskp); \
2239         type matchvalx = *(type *)(matchvalp); \
2240         type maskx = *(type *)(maskp); \
2241         type valx = *(type *)(valp); \
2242         \
2243         (valx & maskx) == (matchvalx & matchmaskx) && !(maskx & (maskx ^ \
2244                                                                  matchmaskx)); \
2245 })
2246
2247 static bool cmp_val_mask(void *valp, void *maskp, void *matchvalp,
2248                          void *matchmaskp, int size)
2249 {
2250         bool same = false;
2251
2252         switch (size) {
2253         case sizeof(u8):
2254                 same = SAME_VAL_MASK(u8, valp, maskp, matchvalp, matchmaskp);
2255                 break;
2256         case sizeof(u16):
2257                 same = SAME_VAL_MASK(u16, valp, maskp, matchvalp, matchmaskp);
2258                 break;
2259         case sizeof(u32):
2260                 same = SAME_VAL_MASK(u32, valp, maskp, matchvalp, matchmaskp);
2261                 break;
2262         }
2263
2264         return same;
2265 }
2266
2267 static struct mlx5_fields fields[] = {
2268         OFFLOAD(DMAC_47_16, 4, eth.h_dest[0], 0, dmac_47_16),
2269         OFFLOAD(DMAC_15_0,  2, eth.h_dest[4], 0, dmac_15_0),
2270         OFFLOAD(SMAC_47_16, 4, eth.h_source[0], 0, smac_47_16),
2271         OFFLOAD(SMAC_15_0,  2, eth.h_source[4], 0, smac_15_0),
2272         OFFLOAD(ETHERTYPE,  2, eth.h_proto, 0, ethertype),
2273         OFFLOAD(FIRST_VID,  2, vlan.h_vlan_TCI, 0, first_vid),
2274
2275         OFFLOAD(IP_TTL, 1, ip4.ttl,   0, ttl_hoplimit),
2276         OFFLOAD(SIPV4,  4, ip4.saddr, 0, src_ipv4_src_ipv6.ipv4_layout.ipv4),
2277         OFFLOAD(DIPV4,  4, ip4.daddr, 0, dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2278
2279         OFFLOAD(SIPV6_127_96, 4, ip6.saddr.s6_addr32[0], 0,
2280                 src_ipv4_src_ipv6.ipv6_layout.ipv6[0]),
2281         OFFLOAD(SIPV6_95_64,  4, ip6.saddr.s6_addr32[1], 0,
2282                 src_ipv4_src_ipv6.ipv6_layout.ipv6[4]),
2283         OFFLOAD(SIPV6_63_32,  4, ip6.saddr.s6_addr32[2], 0,
2284                 src_ipv4_src_ipv6.ipv6_layout.ipv6[8]),
2285         OFFLOAD(SIPV6_31_0,   4, ip6.saddr.s6_addr32[3], 0,
2286                 src_ipv4_src_ipv6.ipv6_layout.ipv6[12]),
2287         OFFLOAD(DIPV6_127_96, 4, ip6.daddr.s6_addr32[0], 0,
2288                 dst_ipv4_dst_ipv6.ipv6_layout.ipv6[0]),
2289         OFFLOAD(DIPV6_95_64,  4, ip6.daddr.s6_addr32[1], 0,
2290                 dst_ipv4_dst_ipv6.ipv6_layout.ipv6[4]),
2291         OFFLOAD(DIPV6_63_32,  4, ip6.daddr.s6_addr32[2], 0,
2292                 dst_ipv4_dst_ipv6.ipv6_layout.ipv6[8]),
2293         OFFLOAD(DIPV6_31_0,   4, ip6.daddr.s6_addr32[3], 0,
2294                 dst_ipv4_dst_ipv6.ipv6_layout.ipv6[12]),
2295         OFFLOAD(IPV6_HOPLIMIT, 1, ip6.hop_limit, 0, ttl_hoplimit),
2296
2297         OFFLOAD(TCP_SPORT, 2, tcp.source,  0, tcp_sport),
2298         OFFLOAD(TCP_DPORT, 2, tcp.dest,    0, tcp_dport),
2299         OFFLOAD(TCP_FLAGS, 1, tcp.ack_seq, 5, tcp_flags),
2300
2301         OFFLOAD(UDP_SPORT, 2, udp.source, 0, udp_sport),
2302         OFFLOAD(UDP_DPORT, 2, udp.dest,   0, udp_dport),
2303 };
2304
2305 /* On input attr->max_mod_hdr_actions tells how many HW actions can be parsed at
2306  * max from the SW pedit action. On success, attr->num_mod_hdr_actions
2307  * says how many HW actions were actually parsed.
2308  */
2309 static int offload_pedit_fields(struct pedit_headers_action *hdrs,
2310                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
2311                                 u32 *action_flags,
2312                                 struct netlink_ext_ack *extack)
2313 {
2314         struct pedit_headers *set_masks, *add_masks, *set_vals, *add_vals;
2315         void *headers_c = get_match_headers_criteria(*action_flags,
2316                                                      &parse_attr->spec);
2317         void *headers_v = get_match_headers_value(*action_flags,
2318                                                   &parse_attr->spec);
2319         int i, action_size, nactions, max_actions, first, last, next_z;
2320         void *s_masks_p, *a_masks_p, *vals_p;
2321         struct mlx5_fields *f;
2322         u8 cmd, field_bsize;
2323         u32 s_mask, a_mask;
2324         unsigned long mask;
2325         __be32 mask_be32;
2326         __be16 mask_be16;
2327         void *action;
2328
2329         set_masks = &hdrs[0].masks;
2330         add_masks = &hdrs[1].masks;
2331         set_vals = &hdrs[0].vals;
2332         add_vals = &hdrs[1].vals;
2333
2334         action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
2335         action = parse_attr->mod_hdr_actions +
2336                  parse_attr->num_mod_hdr_actions * action_size;
2337
2338         max_actions = parse_attr->max_mod_hdr_actions;
2339         nactions = parse_attr->num_mod_hdr_actions;
2340
2341         for (i = 0; i < ARRAY_SIZE(fields); i++) {
2342                 bool skip;
2343
2344                 f = &fields[i];
2345                 /* avoid seeing bits set from previous iterations */
2346                 s_mask = 0;
2347                 a_mask = 0;
2348
2349                 s_masks_p = (void *)set_masks + f->offset;
2350                 a_masks_p = (void *)add_masks + f->offset;
2351
2352                 memcpy(&s_mask, s_masks_p, f->size);
2353                 memcpy(&a_mask, a_masks_p, f->size);
2354
2355                 if (!s_mask && !a_mask) /* nothing to offload here */
2356                         continue;
2357
2358                 if (s_mask && a_mask) {
2359                         NL_SET_ERR_MSG_MOD(extack,
2360                                            "can't set and add to the same HW field");
2361                         printk(KERN_WARNING "mlx5: can't set and add to the same HW field (%x)\n", f->field);
2362                         return -EOPNOTSUPP;
2363                 }
2364
2365                 if (nactions == max_actions) {
2366                         NL_SET_ERR_MSG_MOD(extack,
2367                                            "too many pedit actions, can't offload");
2368                         printk(KERN_WARNING "mlx5: parsed %d pedit actions, can't do more\n", nactions);
2369                         return -EOPNOTSUPP;
2370                 }
2371
2372                 skip = false;
2373                 if (s_mask) {
2374                         void *match_mask = headers_c + f->match_offset;
2375                         void *match_val = headers_v + f->match_offset;
2376
2377                         cmd  = MLX5_ACTION_TYPE_SET;
2378                         mask = s_mask;
2379                         vals_p = (void *)set_vals + f->offset;
2380                         /* don't rewrite if we have a match on the same value */
2381                         if (cmp_val_mask(vals_p, s_masks_p, match_val,
2382                                          match_mask, f->size))
2383                                 skip = true;
2384                         /* clear to denote we consumed this field */
2385                         memset(s_masks_p, 0, f->size);
2386                 } else {
2387                         u32 zero = 0;
2388
2389                         cmd  = MLX5_ACTION_TYPE_ADD;
2390                         mask = a_mask;
2391                         vals_p = (void *)add_vals + f->offset;
2392                         /* add 0 is no change */
2393                         if (!memcmp(vals_p, &zero, f->size))
2394                                 skip = true;
2395                         /* clear to denote we consumed this field */
2396                         memset(a_masks_p, 0, f->size);
2397                 }
2398                 if (skip)
2399                         continue;
2400
2401                 field_bsize = f->size * BITS_PER_BYTE;
2402
2403                 if (field_bsize == 32) {
2404                         mask_be32 = *(__be32 *)&mask;
2405                         mask = (__force unsigned long)cpu_to_le32(be32_to_cpu(mask_be32));
2406                 } else if (field_bsize == 16) {
2407                         mask_be16 = *(__be16 *)&mask;
2408                         mask = (__force unsigned long)cpu_to_le16(be16_to_cpu(mask_be16));
2409                 }
2410
2411                 first = find_first_bit(&mask, field_bsize);
2412                 next_z = find_next_zero_bit(&mask, field_bsize, first);
2413                 last  = find_last_bit(&mask, field_bsize);
2414                 if (first < next_z && next_z < last) {
2415                         NL_SET_ERR_MSG_MOD(extack,
2416                                            "rewrite of few sub-fields isn't supported");
2417                         printk(KERN_WARNING "mlx5: rewrite of few sub-fields (mask %lx) isn't offloaded\n",
2418                                mask);
2419                         return -EOPNOTSUPP;
2420                 }
2421
2422                 MLX5_SET(set_action_in, action, action_type, cmd);
2423                 MLX5_SET(set_action_in, action, field, f->field);
2424
2425                 if (cmd == MLX5_ACTION_TYPE_SET) {
2426                         MLX5_SET(set_action_in, action, offset, first);
2427                         /* length is num of bits to be written, zero means length of 32 */
2428                         MLX5_SET(set_action_in, action, length, (last - first + 1));
2429                 }
2430
2431                 if (field_bsize == 32)
2432                         MLX5_SET(set_action_in, action, data, ntohl(*(__be32 *)vals_p) >> first);
2433                 else if (field_bsize == 16)
2434                         MLX5_SET(set_action_in, action, data, ntohs(*(__be16 *)vals_p) >> first);
2435                 else if (field_bsize == 8)
2436                         MLX5_SET(set_action_in, action, data, *(u8 *)vals_p >> first);
2437
2438                 action += action_size;
2439                 nactions++;
2440         }
2441
2442         parse_attr->num_mod_hdr_actions = nactions;
2443         return 0;
2444 }
2445
2446 static int mlx5e_flow_namespace_max_modify_action(struct mlx5_core_dev *mdev,
2447                                                   int namespace)
2448 {
2449         if (namespace == MLX5_FLOW_NAMESPACE_FDB) /* FDB offloading */
2450                 return MLX5_CAP_ESW_FLOWTABLE_FDB(mdev, max_modify_header_actions);
2451         else /* namespace is MLX5_FLOW_NAMESPACE_KERNEL - NIC offloading */
2452                 return MLX5_CAP_FLOWTABLE_NIC_RX(mdev, max_modify_header_actions);
2453 }
2454
2455 static int alloc_mod_hdr_actions(struct mlx5e_priv *priv,
2456                                  struct pedit_headers_action *hdrs,
2457                                  int namespace,
2458                                  struct mlx5e_tc_flow_parse_attr *parse_attr)
2459 {
2460         int nkeys, action_size, max_actions;
2461
2462         nkeys = hdrs[TCA_PEDIT_KEY_EX_CMD_SET].pedits +
2463                 hdrs[TCA_PEDIT_KEY_EX_CMD_ADD].pedits;
2464         action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
2465
2466         max_actions = mlx5e_flow_namespace_max_modify_action(priv->mdev, namespace);
2467         /* can get up to crazingly 16 HW actions in 32 bits pedit SW key */
2468         max_actions = min(max_actions, nkeys * 16);
2469
2470         parse_attr->mod_hdr_actions = kcalloc(max_actions, action_size, GFP_KERNEL);
2471         if (!parse_attr->mod_hdr_actions)
2472                 return -ENOMEM;
2473
2474         parse_attr->max_mod_hdr_actions = max_actions;
2475         return 0;
2476 }
2477
2478 static const struct pedit_headers zero_masks = {};
2479
2480 static int parse_tc_pedit_action(struct mlx5e_priv *priv,
2481                                  const struct flow_action_entry *act, int namespace,
2482                                  struct mlx5e_tc_flow_parse_attr *parse_attr,
2483                                  struct pedit_headers_action *hdrs,
2484                                  struct netlink_ext_ack *extack)
2485 {
2486         u8 cmd = (act->id == FLOW_ACTION_MANGLE) ? 0 : 1;
2487         int err = -EOPNOTSUPP;
2488         u32 mask, val, offset;
2489         u8 htype;
2490
2491         htype = act->mangle.htype;
2492         err = -EOPNOTSUPP; /* can't be all optimistic */
2493
2494         if (htype == FLOW_ACT_MANGLE_UNSPEC) {
2495                 NL_SET_ERR_MSG_MOD(extack, "legacy pedit isn't offloaded");
2496                 goto out_err;
2497         }
2498
2499         if (!mlx5e_flow_namespace_max_modify_action(priv->mdev, namespace)) {
2500                 NL_SET_ERR_MSG_MOD(extack,
2501                                    "The pedit offload action is not supported");
2502                 goto out_err;
2503         }
2504
2505         mask = act->mangle.mask;
2506         val = act->mangle.val;
2507         offset = act->mangle.offset;
2508
2509         err = set_pedit_val(htype, ~mask, val, offset, &hdrs[cmd]);
2510         if (err)
2511                 goto out_err;
2512
2513         hdrs[cmd].pedits++;
2514
2515         return 0;
2516 out_err:
2517         return err;
2518 }
2519
2520 static int alloc_tc_pedit_action(struct mlx5e_priv *priv, int namespace,
2521                                  struct mlx5e_tc_flow_parse_attr *parse_attr,
2522                                  struct pedit_headers_action *hdrs,
2523                                  u32 *action_flags,
2524                                  struct netlink_ext_ack *extack)
2525 {
2526         struct pedit_headers *cmd_masks;
2527         int err;
2528         u8 cmd;
2529
2530         if (!parse_attr->mod_hdr_actions) {
2531                 err = alloc_mod_hdr_actions(priv, hdrs, namespace, parse_attr);
2532                 if (err)
2533                         goto out_err;
2534         }
2535
2536         err = offload_pedit_fields(hdrs, parse_attr, action_flags, extack);
2537         if (err < 0)
2538                 goto out_dealloc_parsed_actions;
2539
2540         for (cmd = 0; cmd < __PEDIT_CMD_MAX; cmd++) {
2541                 cmd_masks = &hdrs[cmd].masks;
2542                 if (memcmp(cmd_masks, &zero_masks, sizeof(zero_masks))) {
2543                         NL_SET_ERR_MSG_MOD(extack,
2544                                            "attempt to offload an unsupported field");
2545                         netdev_warn(priv->netdev, "attempt to offload an unsupported field (cmd %d)\n", cmd);
2546                         print_hex_dump(KERN_WARNING, "mask: ", DUMP_PREFIX_ADDRESS,
2547                                        16, 1, cmd_masks, sizeof(zero_masks), true);
2548                         err = -EOPNOTSUPP;
2549                         goto out_dealloc_parsed_actions;
2550                 }
2551         }
2552
2553         return 0;
2554
2555 out_dealloc_parsed_actions:
2556         kfree(parse_attr->mod_hdr_actions);
2557 out_err:
2558         return err;
2559 }
2560
2561 static bool csum_offload_supported(struct mlx5e_priv *priv,
2562                                    u32 action,
2563                                    u32 update_flags,
2564                                    struct netlink_ext_ack *extack)
2565 {
2566         u32 prot_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR | TCA_CSUM_UPDATE_FLAG_TCP |
2567                          TCA_CSUM_UPDATE_FLAG_UDP;
2568
2569         /*  The HW recalcs checksums only if re-writing headers */
2570         if (!(action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)) {
2571                 NL_SET_ERR_MSG_MOD(extack,
2572                                    "TC csum action is only offloaded with pedit");
2573                 netdev_warn(priv->netdev,
2574                             "TC csum action is only offloaded with pedit\n");
2575                 return false;
2576         }
2577
2578         if (update_flags & ~prot_flags) {
2579                 NL_SET_ERR_MSG_MOD(extack,
2580                                    "can't offload TC csum action for some header/s");
2581                 netdev_warn(priv->netdev,
2582                             "can't offload TC csum action for some header/s - flags %#x\n",
2583                             update_flags);
2584                 return false;
2585         }
2586
2587         return true;
2588 }
2589
2590 struct ip_ttl_word {
2591         __u8    ttl;
2592         __u8    protocol;
2593         __sum16 check;
2594 };
2595
2596 struct ipv6_hoplimit_word {
2597         __be16  payload_len;
2598         __u8    nexthdr;
2599         __u8    hop_limit;
2600 };
2601
2602 static bool is_action_keys_supported(const struct flow_action_entry *act)
2603 {
2604         u32 mask, offset;
2605         u8 htype;
2606
2607         htype = act->mangle.htype;
2608         offset = act->mangle.offset;
2609         mask = ~act->mangle.mask;
2610         /* For IPv4 & IPv6 header check 4 byte word,
2611          * to determine that modified fields
2612          * are NOT ttl & hop_limit only.
2613          */
2614         if (htype == FLOW_ACT_MANGLE_HDR_TYPE_IP4) {
2615                 struct ip_ttl_word *ttl_word =
2616                         (struct ip_ttl_word *)&mask;
2617
2618                 if (offset != offsetof(struct iphdr, ttl) ||
2619                     ttl_word->protocol ||
2620                     ttl_word->check) {
2621                         return true;
2622                 }
2623         } else if (htype == FLOW_ACT_MANGLE_HDR_TYPE_IP6) {
2624                 struct ipv6_hoplimit_word *hoplimit_word =
2625                         (struct ipv6_hoplimit_word *)&mask;
2626
2627                 if (offset != offsetof(struct ipv6hdr, payload_len) ||
2628                     hoplimit_word->payload_len ||
2629                     hoplimit_word->nexthdr) {
2630                         return true;
2631                 }
2632         }
2633         return false;
2634 }
2635
2636 static bool modify_header_match_supported(struct mlx5_flow_spec *spec,
2637                                           struct flow_action *flow_action,
2638                                           u32 actions,
2639                                           struct netlink_ext_ack *extack)
2640 {
2641         const struct flow_action_entry *act;
2642         bool modify_ip_header;
2643         void *headers_v;
2644         u16 ethertype;
2645         u8 ip_proto;
2646         int i;
2647
2648         headers_v = get_match_headers_value(actions, spec);
2649         ethertype = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ethertype);
2650
2651         /* for non-IP we only re-write MACs, so we're okay */
2652         if (ethertype != ETH_P_IP && ethertype != ETH_P_IPV6)
2653                 goto out_ok;
2654
2655         modify_ip_header = false;
2656         flow_action_for_each(i, act, flow_action) {
2657                 if (act->id != FLOW_ACTION_MANGLE &&
2658                     act->id != FLOW_ACTION_ADD)
2659                         continue;
2660
2661                 if (is_action_keys_supported(act)) {
2662                         modify_ip_header = true;
2663                         break;
2664                 }
2665         }
2666
2667         ip_proto = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ip_protocol);
2668         if (modify_ip_header && ip_proto != IPPROTO_TCP &&
2669             ip_proto != IPPROTO_UDP && ip_proto != IPPROTO_ICMP) {
2670                 NL_SET_ERR_MSG_MOD(extack,
2671                                    "can't offload re-write of non TCP/UDP");
2672                 pr_info("can't offload re-write of ip proto %d\n", ip_proto);
2673                 return false;
2674         }
2675
2676 out_ok:
2677         return true;
2678 }
2679
2680 static bool actions_match_supported(struct mlx5e_priv *priv,
2681                                     struct flow_action *flow_action,
2682                                     struct mlx5e_tc_flow_parse_attr *parse_attr,
2683                                     struct mlx5e_tc_flow *flow,
2684                                     struct netlink_ext_ack *extack)
2685 {
2686         u32 actions;
2687
2688         if (mlx5e_is_eswitch_flow(flow))
2689                 actions = flow->esw_attr->action;
2690         else
2691                 actions = flow->nic_attr->action;
2692
2693         if (flow_flag_test(flow, EGRESS) &&
2694             !((actions & MLX5_FLOW_CONTEXT_ACTION_DECAP) ||
2695               (actions & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) ||
2696               (actions & MLX5_FLOW_CONTEXT_ACTION_DROP)))
2697                 return false;
2698
2699         if (actions & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2700                 return modify_header_match_supported(&parse_attr->spec,
2701                                                      flow_action, actions,
2702                                                      extack);
2703
2704         return true;
2705 }
2706
2707 static bool same_hw_devs(struct mlx5e_priv *priv, struct mlx5e_priv *peer_priv)
2708 {
2709         struct mlx5_core_dev *fmdev, *pmdev;
2710         u64 fsystem_guid, psystem_guid;
2711
2712         fmdev = priv->mdev;
2713         pmdev = peer_priv->mdev;
2714
2715         fsystem_guid = mlx5_query_nic_system_image_guid(fmdev);
2716         psystem_guid = mlx5_query_nic_system_image_guid(pmdev);
2717
2718         return (fsystem_guid == psystem_guid);
2719 }
2720
2721 static int add_vlan_rewrite_action(struct mlx5e_priv *priv, int namespace,
2722                                    const struct flow_action_entry *act,
2723                                    struct mlx5e_tc_flow_parse_attr *parse_attr,
2724                                    struct pedit_headers_action *hdrs,
2725                                    u32 *action, struct netlink_ext_ack *extack)
2726 {
2727         u16 mask16 = VLAN_VID_MASK;
2728         u16 val16 = act->vlan.vid & VLAN_VID_MASK;
2729         const struct flow_action_entry pedit_act = {
2730                 .id = FLOW_ACTION_MANGLE,
2731                 .mangle.htype = FLOW_ACT_MANGLE_HDR_TYPE_ETH,
2732                 .mangle.offset = offsetof(struct vlan_ethhdr, h_vlan_TCI),
2733                 .mangle.mask = ~(u32)be16_to_cpu(*(__be16 *)&mask16),
2734                 .mangle.val = (u32)be16_to_cpu(*(__be16 *)&val16),
2735         };
2736         u8 match_prio_mask, match_prio_val;
2737         void *headers_c, *headers_v;
2738         int err;
2739
2740         headers_c = get_match_headers_criteria(*action, &parse_attr->spec);
2741         headers_v = get_match_headers_value(*action, &parse_attr->spec);
2742
2743         if (!(MLX5_GET(fte_match_set_lyr_2_4, headers_c, cvlan_tag) &&
2744               MLX5_GET(fte_match_set_lyr_2_4, headers_v, cvlan_tag))) {
2745                 NL_SET_ERR_MSG_MOD(extack,
2746                                    "VLAN rewrite action must have VLAN protocol match");
2747                 return -EOPNOTSUPP;
2748         }
2749
2750         match_prio_mask = MLX5_GET(fte_match_set_lyr_2_4, headers_c, first_prio);
2751         match_prio_val = MLX5_GET(fte_match_set_lyr_2_4, headers_v, first_prio);
2752         if (act->vlan.prio != (match_prio_val & match_prio_mask)) {
2753                 NL_SET_ERR_MSG_MOD(extack,
2754                                    "Changing VLAN prio is not supported");
2755                 return -EOPNOTSUPP;
2756         }
2757
2758         err = parse_tc_pedit_action(priv, &pedit_act, namespace, parse_attr,
2759                                     hdrs, NULL);
2760         *action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2761
2762         return err;
2763 }
2764
2765 static int
2766 add_vlan_prio_tag_rewrite_action(struct mlx5e_priv *priv,
2767                                  struct mlx5e_tc_flow_parse_attr *parse_attr,
2768                                  struct pedit_headers_action *hdrs,
2769                                  u32 *action, struct netlink_ext_ack *extack)
2770 {
2771         const struct flow_action_entry prio_tag_act = {
2772                 .vlan.vid = 0,
2773                 .vlan.prio =
2774                         MLX5_GET(fte_match_set_lyr_2_4,
2775                                  get_match_headers_value(*action,
2776                                                          &parse_attr->spec),
2777                                  first_prio) &
2778                         MLX5_GET(fte_match_set_lyr_2_4,
2779                                  get_match_headers_criteria(*action,
2780                                                             &parse_attr->spec),
2781                                  first_prio),
2782         };
2783
2784         return add_vlan_rewrite_action(priv, MLX5_FLOW_NAMESPACE_FDB,
2785                                        &prio_tag_act, parse_attr, hdrs, action,
2786                                        extack);
2787 }
2788
2789 static int parse_tc_nic_actions(struct mlx5e_priv *priv,
2790                                 struct flow_action *flow_action,
2791                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
2792                                 struct mlx5e_tc_flow *flow,
2793                                 struct netlink_ext_ack *extack)
2794 {
2795         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
2796         struct pedit_headers_action hdrs[2] = {};
2797         const struct flow_action_entry *act;
2798         u32 action = 0;
2799         int err, i;
2800
2801         if (!flow_action_has_entries(flow_action))
2802                 return -EINVAL;
2803
2804         attr->flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2805
2806         flow_action_for_each(i, act, flow_action) {
2807                 switch (act->id) {
2808                 case FLOW_ACTION_DROP:
2809                         action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2810                         if (MLX5_CAP_FLOWTABLE(priv->mdev,
2811                                                flow_table_properties_nic_receive.flow_counter))
2812                                 action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
2813                         break;
2814                 case FLOW_ACTION_MANGLE:
2815                 case FLOW_ACTION_ADD:
2816                         err = parse_tc_pedit_action(priv, act, MLX5_FLOW_NAMESPACE_KERNEL,
2817                                                     parse_attr, hdrs, extack);
2818                         if (err)
2819                                 return err;
2820
2821                         action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
2822                                   MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2823                         break;
2824                 case FLOW_ACTION_VLAN_MANGLE:
2825                         err = add_vlan_rewrite_action(priv,
2826                                                       MLX5_FLOW_NAMESPACE_KERNEL,
2827                                                       act, parse_attr, hdrs,
2828                                                       &action, extack);
2829                         if (err)
2830                                 return err;
2831
2832                         break;
2833                 case FLOW_ACTION_CSUM:
2834                         if (csum_offload_supported(priv, action,
2835                                                    act->csum_flags,
2836                                                    extack))
2837                                 break;
2838
2839                         return -EOPNOTSUPP;
2840                 case FLOW_ACTION_REDIRECT: {
2841                         struct net_device *peer_dev = act->dev;
2842
2843                         if (priv->netdev->netdev_ops == peer_dev->netdev_ops &&
2844                             same_hw_devs(priv, netdev_priv(peer_dev))) {
2845                                 parse_attr->mirred_ifindex[0] = peer_dev->ifindex;
2846                                 flow_flag_set(flow, HAIRPIN);
2847                                 action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
2848                                           MLX5_FLOW_CONTEXT_ACTION_COUNT;
2849                         } else {
2850                                 NL_SET_ERR_MSG_MOD(extack,
2851                                                    "device is not on same HW, can't offload");
2852                                 netdev_warn(priv->netdev, "device %s not on same HW, can't offload\n",
2853                                             peer_dev->name);
2854                                 return -EINVAL;
2855                         }
2856                         }
2857                         break;
2858                 case FLOW_ACTION_MARK: {
2859                         u32 mark = act->mark;
2860
2861                         if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
2862                                 NL_SET_ERR_MSG_MOD(extack,
2863                                                    "Bad flow mark - only 16 bit is supported");
2864                                 return -EINVAL;
2865                         }
2866
2867                         attr->flow_tag = mark;
2868                         action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2869                         }
2870                         break;
2871                 default:
2872                         NL_SET_ERR_MSG_MOD(extack, "The offload action is not supported");
2873                         return -EOPNOTSUPP;
2874                 }
2875         }
2876
2877         if (hdrs[TCA_PEDIT_KEY_EX_CMD_SET].pedits ||
2878             hdrs[TCA_PEDIT_KEY_EX_CMD_ADD].pedits) {
2879                 err = alloc_tc_pedit_action(priv, MLX5_FLOW_NAMESPACE_KERNEL,
2880                                             parse_attr, hdrs, &action, extack);
2881                 if (err)
2882                         return err;
2883                 /* in case all pedit actions are skipped, remove the MOD_HDR
2884                  * flag.
2885                  */
2886                 if (parse_attr->num_mod_hdr_actions == 0) {
2887                         action &= ~MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2888                         kfree(parse_attr->mod_hdr_actions);
2889                 }
2890         }
2891
2892         attr->action = action;
2893         if (!actions_match_supported(priv, flow_action, parse_attr, flow, extack))
2894                 return -EOPNOTSUPP;
2895
2896         return 0;
2897 }
2898
2899 struct encap_key {
2900         const struct ip_tunnel_key *ip_tun_key;
2901         struct mlx5e_tc_tunnel *tc_tunnel;
2902 };
2903
2904 static inline int cmp_encap_info(struct encap_key *a,
2905                                  struct encap_key *b)
2906 {
2907         return memcmp(a->ip_tun_key, b->ip_tun_key, sizeof(*a->ip_tun_key)) ||
2908                a->tc_tunnel->tunnel_type != b->tc_tunnel->tunnel_type;
2909 }
2910
2911 static inline int hash_encap_info(struct encap_key *key)
2912 {
2913         return jhash(key->ip_tun_key, sizeof(*key->ip_tun_key),
2914                      key->tc_tunnel->tunnel_type);
2915 }
2916
2917
2918 static bool is_merged_eswitch_dev(struct mlx5e_priv *priv,
2919                                   struct net_device *peer_netdev)
2920 {
2921         struct mlx5e_priv *peer_priv;
2922
2923         peer_priv = netdev_priv(peer_netdev);
2924
2925         return (MLX5_CAP_ESW(priv->mdev, merged_eswitch) &&
2926                 mlx5e_eswitch_rep(priv->netdev) &&
2927                 mlx5e_eswitch_rep(peer_netdev) &&
2928                 same_hw_devs(priv, peer_priv));
2929 }
2930
2931
2932
2933 bool mlx5e_encap_take(struct mlx5e_encap_entry *e)
2934 {
2935         return refcount_inc_not_zero(&e->refcnt);
2936 }
2937
2938 static struct mlx5e_encap_entry *
2939 mlx5e_encap_get(struct mlx5e_priv *priv, struct encap_key *key,
2940                 uintptr_t hash_key)
2941 {
2942         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2943         struct mlx5e_encap_entry *e;
2944         struct encap_key e_key;
2945
2946         hash_for_each_possible_rcu(esw->offloads.encap_tbl, e,
2947                                    encap_hlist, hash_key) {
2948                 e_key.ip_tun_key = &e->tun_info->key;
2949                 e_key.tc_tunnel = e->tunnel;
2950                 if (!cmp_encap_info(&e_key, key) &&
2951                     mlx5e_encap_take(e))
2952                         return e;
2953         }
2954
2955         return NULL;
2956 }
2957
2958 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
2959                               struct mlx5e_tc_flow *flow,
2960                               struct net_device *mirred_dev,
2961                               int out_index,
2962                               struct netlink_ext_ack *extack,
2963                               struct net_device **encap_dev,
2964                               bool *encap_valid)
2965 {
2966         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2967         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
2968         struct mlx5e_tc_flow_parse_attr *parse_attr;
2969         const struct ip_tunnel_info *tun_info;
2970         struct encap_key key;
2971         struct mlx5e_encap_entry *e;
2972         unsigned short family;
2973         uintptr_t hash_key;
2974         int err = 0;
2975
2976         parse_attr = attr->parse_attr;
2977         tun_info = parse_attr->tun_info[out_index];
2978         family = ip_tunnel_info_af(tun_info);
2979         key.ip_tun_key = &tun_info->key;
2980         key.tc_tunnel = mlx5e_get_tc_tun(mirred_dev);
2981         if (!key.tc_tunnel) {
2982                 NL_SET_ERR_MSG_MOD(extack, "Unsupported tunnel");
2983                 return -EOPNOTSUPP;
2984         }
2985
2986         hash_key = hash_encap_info(&key);
2987
2988         mutex_lock(&esw->offloads.encap_tbl_lock);
2989         e = mlx5e_encap_get(priv, &key, hash_key);
2990
2991         /* must verify if encap is valid or not */
2992         if (e) {
2993                 mutex_unlock(&esw->offloads.encap_tbl_lock);
2994                 wait_for_completion(&e->res_ready);
2995
2996                 /* Protect against concurrent neigh update. */
2997                 mutex_lock(&esw->offloads.encap_tbl_lock);
2998                 if (e->compl_result < 0) {
2999                         err = -EREMOTEIO;
3000                         goto out_err;
3001                 }
3002                 goto attach_flow;
3003         }
3004
3005         e = kzalloc(sizeof(*e), GFP_KERNEL);
3006         if (!e) {
3007                 err = -ENOMEM;
3008                 goto out_err;
3009         }
3010
3011         refcount_set(&e->refcnt, 1);
3012         init_completion(&e->res_ready);
3013
3014         e->tun_info = tun_info;
3015         err = mlx5e_tc_tun_init_encap_attr(mirred_dev, priv, e, extack);
3016         if (err) {
3017                 kfree(e);
3018                 e = NULL;
3019                 goto out_err;
3020         }
3021
3022         INIT_LIST_HEAD(&e->flows);
3023         hash_add_rcu(esw->offloads.encap_tbl, &e->encap_hlist, hash_key);
3024         mutex_unlock(&esw->offloads.encap_tbl_lock);
3025
3026         if (family == AF_INET)
3027                 err = mlx5e_tc_tun_create_header_ipv4(priv, mirred_dev, e);
3028         else if (family == AF_INET6)
3029                 err = mlx5e_tc_tun_create_header_ipv6(priv, mirred_dev, e);
3030
3031         /* Protect against concurrent neigh update. */
3032         mutex_lock(&esw->offloads.encap_tbl_lock);
3033         complete_all(&e->res_ready);
3034         if (err) {
3035                 e->compl_result = err;
3036                 goto out_err;
3037         }
3038         e->compl_result = 1;
3039
3040 attach_flow:
3041         flow->encaps[out_index].e = e;
3042         list_add(&flow->encaps[out_index].list, &e->flows);
3043         flow->encaps[out_index].index = out_index;
3044         *encap_dev = e->out_dev;
3045         if (e->flags & MLX5_ENCAP_ENTRY_VALID) {
3046                 attr->dests[out_index].pkt_reformat = e->pkt_reformat;
3047                 attr->dests[out_index].flags |= MLX5_ESW_DEST_ENCAP_VALID;
3048                 *encap_valid = true;
3049         } else {
3050                 *encap_valid = false;
3051         }
3052         mutex_unlock(&esw->offloads.encap_tbl_lock);
3053
3054         return err;
3055
3056 out_err:
3057         mutex_unlock(&esw->offloads.encap_tbl_lock);
3058         if (e)
3059                 mlx5e_encap_put(priv, e);
3060         return err;
3061 }
3062
3063 static int parse_tc_vlan_action(struct mlx5e_priv *priv,
3064                                 const struct flow_action_entry *act,
3065                                 struct mlx5_esw_flow_attr *attr,
3066                                 u32 *action)
3067 {
3068         u8 vlan_idx = attr->total_vlan;
3069
3070         if (vlan_idx >= MLX5_FS_VLAN_DEPTH)
3071                 return -EOPNOTSUPP;
3072
3073         switch (act->id) {
3074         case FLOW_ACTION_VLAN_POP:
3075                 if (vlan_idx) {
3076                         if (!mlx5_eswitch_vlan_actions_supported(priv->mdev,
3077                                                                  MLX5_FS_VLAN_DEPTH))
3078                                 return -EOPNOTSUPP;
3079
3080                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2;
3081                 } else {
3082                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
3083                 }
3084                 break;
3085         case FLOW_ACTION_VLAN_PUSH:
3086                 attr->vlan_vid[vlan_idx] = act->vlan.vid;
3087                 attr->vlan_prio[vlan_idx] = act->vlan.prio;
3088                 attr->vlan_proto[vlan_idx] = act->vlan.proto;
3089                 if (!attr->vlan_proto[vlan_idx])
3090                         attr->vlan_proto[vlan_idx] = htons(ETH_P_8021Q);
3091
3092                 if (vlan_idx) {
3093                         if (!mlx5_eswitch_vlan_actions_supported(priv->mdev,
3094                                                                  MLX5_FS_VLAN_DEPTH))
3095                                 return -EOPNOTSUPP;
3096
3097                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
3098                 } else {
3099                         if (!mlx5_eswitch_vlan_actions_supported(priv->mdev, 1) &&
3100                             (act->vlan.proto != htons(ETH_P_8021Q) ||
3101                              act->vlan.prio))
3102                                 return -EOPNOTSUPP;
3103
3104                         *action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
3105                 }
3106                 break;
3107         default:
3108                 return -EINVAL;
3109         }
3110
3111         attr->total_vlan = vlan_idx + 1;
3112
3113         return 0;
3114 }
3115
3116 static int add_vlan_push_action(struct mlx5e_priv *priv,
3117                                 struct mlx5_esw_flow_attr *attr,
3118                                 struct net_device **out_dev,
3119                                 u32 *action)
3120 {
3121         struct net_device *vlan_dev = *out_dev;
3122         struct flow_action_entry vlan_act = {
3123                 .id = FLOW_ACTION_VLAN_PUSH,
3124                 .vlan.vid = vlan_dev_vlan_id(vlan_dev),
3125                 .vlan.proto = vlan_dev_vlan_proto(vlan_dev),
3126                 .vlan.prio = 0,
3127         };
3128         int err;
3129
3130         err = parse_tc_vlan_action(priv, &vlan_act, attr, action);
3131         if (err)
3132                 return err;
3133
3134         *out_dev = dev_get_by_index_rcu(dev_net(vlan_dev),
3135                                         dev_get_iflink(vlan_dev));
3136         if (is_vlan_dev(*out_dev))
3137                 err = add_vlan_push_action(priv, attr, out_dev, action);
3138
3139         return err;
3140 }
3141
3142 static int add_vlan_pop_action(struct mlx5e_priv *priv,
3143                                struct mlx5_esw_flow_attr *attr,
3144                                u32 *action)
3145 {
3146         int nest_level = vlan_get_encap_level(attr->parse_attr->filter_dev);
3147         struct flow_action_entry vlan_act = {
3148                 .id = FLOW_ACTION_VLAN_POP,
3149         };
3150         int err = 0;
3151
3152         while (nest_level--) {
3153                 err = parse_tc_vlan_action(priv, &vlan_act, attr, action);
3154                 if (err)
3155                         return err;
3156         }
3157
3158         return err;
3159 }
3160
3161 bool mlx5e_is_valid_eswitch_fwd_dev(struct mlx5e_priv *priv,
3162                                     struct net_device *out_dev)
3163 {
3164         if (is_merged_eswitch_dev(priv, out_dev))
3165                 return true;
3166
3167         return mlx5e_eswitch_rep(out_dev) &&
3168                same_hw_devs(priv, netdev_priv(out_dev));
3169 }
3170
3171 static int parse_tc_fdb_actions(struct mlx5e_priv *priv,
3172                                 struct flow_action *flow_action,
3173                                 struct mlx5e_tc_flow *flow,
3174                                 struct netlink_ext_ack *extack)
3175 {
3176         struct pedit_headers_action hdrs[2] = {};
3177         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
3178         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
3179         struct mlx5e_tc_flow_parse_attr *parse_attr = attr->parse_attr;
3180         struct mlx5e_rep_priv *rpriv = priv->ppriv;
3181         const struct ip_tunnel_info *info = NULL;
3182         const struct flow_action_entry *act;
3183         bool encap = false;
3184         u32 action = 0;
3185         int err, i;
3186
3187         if (!flow_action_has_entries(flow_action))
3188                 return -EINVAL;
3189
3190         flow_action_for_each(i, act, flow_action) {
3191                 switch (act->id) {
3192                 case FLOW_ACTION_DROP:
3193                         action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
3194                                   MLX5_FLOW_CONTEXT_ACTION_COUNT;
3195                         break;
3196                 case FLOW_ACTION_MANGLE:
3197                 case FLOW_ACTION_ADD:
3198                         err = parse_tc_pedit_action(priv, act, MLX5_FLOW_NAMESPACE_FDB,
3199                                                     parse_attr, hdrs, extack);
3200                         if (err)
3201                                 return err;
3202
3203                         action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
3204                         attr->split_count = attr->out_count;
3205                         break;
3206                 case FLOW_ACTION_CSUM:
3207                         if (csum_offload_supported(priv, action,
3208                                                    act->csum_flags, extack))
3209                                 break;
3210
3211                         return -EOPNOTSUPP;
3212                 case FLOW_ACTION_REDIRECT:
3213                 case FLOW_ACTION_MIRRED: {
3214                         struct mlx5e_priv *out_priv;
3215                         struct net_device *out_dev;
3216
3217                         out_dev = act->dev;
3218                         if (!out_dev) {
3219                                 /* out_dev is NULL when filters with
3220                                  * non-existing mirred device are replayed to
3221                                  * the driver.
3222                                  */
3223                                 return -EINVAL;
3224                         }
3225
3226                         if (attr->out_count >= MLX5_MAX_FLOW_FWD_VPORTS) {
3227                                 NL_SET_ERR_MSG_MOD(extack,
3228                                                    "can't support more output ports, can't offload forwarding");
3229                                 pr_err("can't support more than %d output ports, can't offload forwarding\n",
3230                                        attr->out_count);
3231                                 return -EOPNOTSUPP;
3232                         }
3233
3234                         action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
3235                                   MLX5_FLOW_CONTEXT_ACTION_COUNT;
3236                         if (netdev_port_same_parent_id(priv->netdev, out_dev)) {
3237                                 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
3238                                 struct net_device *uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
3239                                 struct net_device *uplink_upper;
3240
3241                                 rcu_read_lock();
3242                                 uplink_upper =
3243                                         netdev_master_upper_dev_get_rcu(uplink_dev);
3244                                 if (uplink_upper &&
3245                                     netif_is_lag_master(uplink_upper) &&
3246                                     uplink_upper == out_dev)
3247                                         out_dev = uplink_dev;
3248                                 rcu_read_unlock();
3249
3250                                 if (is_vlan_dev(out_dev)) {
3251                                         err = add_vlan_push_action(priv, attr,
3252                                                                    &out_dev,
3253                                                                    &action);
3254                                         if (err)
3255                                                 return err;
3256                                 }
3257
3258                                 if (is_vlan_dev(parse_attr->filter_dev)) {
3259                                         err = add_vlan_pop_action(priv, attr,
3260                                                                   &action);
3261                                         if (err)
3262                                                 return err;
3263                                 }
3264
3265                                 if (!mlx5e_is_valid_eswitch_fwd_dev(priv, out_dev)) {
3266                                         NL_SET_ERR_MSG_MOD(extack,
3267                                                            "devices are not on same switch HW, can't offload forwarding");
3268                                         pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
3269                                                priv->netdev->name, out_dev->name);
3270                                         return -EOPNOTSUPP;
3271                                 }
3272
3273                                 out_priv = netdev_priv(out_dev);
3274                                 rpriv = out_priv->ppriv;
3275                                 attr->dests[attr->out_count].rep = rpriv->rep;
3276                                 attr->dests[attr->out_count].mdev = out_priv->mdev;
3277                                 attr->out_count++;
3278                         } else if (encap) {
3279                                 parse_attr->mirred_ifindex[attr->out_count] =
3280                                         out_dev->ifindex;
3281                                 parse_attr->tun_info[attr->out_count] = info;
3282                                 encap = false;
3283                                 attr->dests[attr->out_count].flags |=
3284                                         MLX5_ESW_DEST_ENCAP;
3285                                 attr->out_count++;
3286                                 /* attr->dests[].rep is resolved when we
3287                                  * handle encap
3288                                  */
3289                         } else if (parse_attr->filter_dev != priv->netdev) {
3290                                 /* All mlx5 devices are called to configure
3291                                  * high level device filters. Therefore, the
3292                                  * *attempt* to  install a filter on invalid
3293                                  * eswitch should not trigger an explicit error
3294                                  */
3295                                 return -EINVAL;
3296                         } else {
3297                                 NL_SET_ERR_MSG_MOD(extack,
3298                                                    "devices are not on same switch HW, can't offload forwarding");
3299                                 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
3300                                        priv->netdev->name, out_dev->name);
3301                                 return -EINVAL;
3302                         }
3303                         }
3304                         break;
3305                 case FLOW_ACTION_TUNNEL_ENCAP:
3306                         info = act->tunnel;
3307                         if (info)
3308                                 encap = true;
3309                         else
3310                                 return -EOPNOTSUPP;
3311
3312                         break;
3313                 case FLOW_ACTION_VLAN_PUSH:
3314                 case FLOW_ACTION_VLAN_POP:
3315                         if (act->id == FLOW_ACTION_VLAN_PUSH &&
3316                             (action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP)) {
3317                                 /* Replace vlan pop+push with vlan modify */
3318                                 action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
3319                                 err = add_vlan_rewrite_action(priv,
3320                                                               MLX5_FLOW_NAMESPACE_FDB,
3321                                                               act, parse_attr, hdrs,
3322                                                               &action, extack);
3323                         } else {
3324                                 err = parse_tc_vlan_action(priv, act, attr, &action);
3325                         }
3326                         if (err)
3327                                 return err;
3328
3329                         attr->split_count = attr->out_count;
3330                         break;
3331                 case FLOW_ACTION_VLAN_MANGLE:
3332                         err = add_vlan_rewrite_action(priv,
3333                                                       MLX5_FLOW_NAMESPACE_FDB,
3334                                                       act, parse_attr, hdrs,
3335                                                       &action, extack);
3336                         if (err)
3337                                 return err;
3338
3339                         attr->split_count = attr->out_count;
3340                         break;
3341                 case FLOW_ACTION_TUNNEL_DECAP:
3342                         action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
3343                         break;
3344                 case FLOW_ACTION_GOTO: {
3345                         u32 dest_chain = act->chain_index;
3346                         u32 max_chain = mlx5_eswitch_get_chain_range(esw);
3347
3348                         if (dest_chain <= attr->chain) {
3349                                 NL_SET_ERR_MSG(extack, "Goto earlier chain isn't supported");
3350                                 return -EOPNOTSUPP;
3351                         }
3352                         if (dest_chain > max_chain) {
3353                                 NL_SET_ERR_MSG(extack, "Requested destination chain is out of supported range");
3354                                 return -EOPNOTSUPP;
3355                         }
3356                         action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
3357                         attr->dest_chain = dest_chain;
3358                         break;
3359                         }
3360                 default:
3361                         NL_SET_ERR_MSG_MOD(extack, "The offload action is not supported");
3362                         return -EOPNOTSUPP;
3363                 }
3364         }
3365
3366         if (MLX5_CAP_GEN(esw->dev, prio_tag_required) &&
3367             action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) {
3368                 /* For prio tag mode, replace vlan pop with rewrite vlan prio
3369                  * tag rewrite.
3370                  */
3371                 action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
3372                 err = add_vlan_prio_tag_rewrite_action(priv, parse_attr, hdrs,
3373                                                        &action, extack);
3374                 if (err)
3375                         return err;
3376         }
3377
3378         if (hdrs[TCA_PEDIT_KEY_EX_CMD_SET].pedits ||
3379             hdrs[TCA_PEDIT_KEY_EX_CMD_ADD].pedits) {
3380                 err = alloc_tc_pedit_action(priv, MLX5_FLOW_NAMESPACE_FDB,
3381                                             parse_attr, hdrs, &action, extack);
3382                 if (err)
3383                         return err;
3384                 /* in case all pedit actions are skipped, remove the MOD_HDR
3385                  * flag. we might have set split_count either by pedit or
3386                  * pop/push. if there is no pop/push either, reset it too.
3387                  */
3388                 if (parse_attr->num_mod_hdr_actions == 0) {
3389                         action &= ~MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
3390                         kfree(parse_attr->mod_hdr_actions);
3391                         if (!((action & MLX5_FLOW_CONTEXT_ACTION_VLAN_POP) ||
3392                               (action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH)))
3393                                 attr->split_count = 0;
3394                 }
3395         }
3396
3397         attr->action = action;
3398         if (!actions_match_supported(priv, flow_action, parse_attr, flow, extack))
3399                 return -EOPNOTSUPP;
3400
3401         if (attr->dest_chain) {
3402                 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
3403                         NL_SET_ERR_MSG(extack, "Mirroring goto chain rules isn't supported");
3404                         return -EOPNOTSUPP;
3405                 }
3406                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3407         }
3408
3409         if (attr->split_count > 0 && !mlx5_esw_has_fwd_fdb(priv->mdev)) {
3410                 NL_SET_ERR_MSG_MOD(extack,
3411                                    "current firmware doesn't support split rule for port mirroring");
3412                 netdev_warn_once(priv->netdev, "current firmware doesn't support split rule for port mirroring\n");
3413                 return -EOPNOTSUPP;
3414         }
3415
3416         return 0;
3417 }
3418
3419 static void get_flags(int flags, unsigned long *flow_flags)
3420 {
3421         unsigned long __flow_flags = 0;
3422
3423         if (flags & MLX5_TC_FLAG(INGRESS))
3424                 __flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_INGRESS);
3425         if (flags & MLX5_TC_FLAG(EGRESS))
3426                 __flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_EGRESS);
3427
3428         if (flags & MLX5_TC_FLAG(ESW_OFFLOAD))
3429                 __flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_ESWITCH);
3430         if (flags & MLX5_TC_FLAG(NIC_OFFLOAD))
3431                 __flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_NIC);
3432
3433         *flow_flags = __flow_flags;
3434 }
3435
3436 static const struct rhashtable_params tc_ht_params = {
3437         .head_offset = offsetof(struct mlx5e_tc_flow, node),
3438         .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
3439         .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
3440         .automatic_shrinking = true,
3441 };
3442
3443 static struct rhashtable *get_tc_ht(struct mlx5e_priv *priv,
3444                                     unsigned long flags)
3445 {
3446         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
3447         struct mlx5e_rep_priv *uplink_rpriv;
3448
3449         if (flags & MLX5_TC_FLAG(ESW_OFFLOAD)) {
3450                 uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
3451                 return &uplink_rpriv->uplink_priv.tc_ht;
3452         } else /* NIC offload */
3453                 return &priv->fs.tc.ht;
3454 }
3455
3456 static bool is_peer_flow_needed(struct mlx5e_tc_flow *flow)
3457 {
3458         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
3459         bool is_rep_ingress = attr->in_rep->vport != MLX5_VPORT_UPLINK &&
3460                 flow_flag_test(flow, INGRESS);
3461         bool act_is_encap = !!(attr->action &
3462                                MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT);
3463         bool esw_paired = mlx5_devcom_is_paired(attr->in_mdev->priv.devcom,
3464                                                 MLX5_DEVCOM_ESW_OFFLOADS);
3465
3466         if (!esw_paired)
3467                 return false;
3468
3469         if ((mlx5_lag_is_sriov(attr->in_mdev) ||
3470              mlx5_lag_is_multipath(attr->in_mdev)) &&
3471             (is_rep_ingress || act_is_encap))
3472                 return true;
3473
3474         return false;
3475 }
3476
3477 static int
3478 mlx5e_alloc_flow(struct mlx5e_priv *priv, int attr_size,
3479                  struct flow_cls_offload *f, unsigned long flow_flags,
3480                  struct mlx5e_tc_flow_parse_attr **__parse_attr,
3481                  struct mlx5e_tc_flow **__flow)
3482 {
3483         struct mlx5e_tc_flow_parse_attr *parse_attr;
3484         struct mlx5e_tc_flow *flow;
3485         int out_index, err;
3486
3487         flow = kzalloc(sizeof(*flow) + attr_size, GFP_KERNEL);
3488         parse_attr = kvzalloc(sizeof(*parse_attr), GFP_KERNEL);
3489         if (!parse_attr || !flow) {
3490                 err = -ENOMEM;
3491                 goto err_free;
3492         }
3493
3494         flow->cookie = f->cookie;
3495         flow->flags = flow_flags;
3496         flow->priv = priv;
3497         for (out_index = 0; out_index < MLX5_MAX_FLOW_FWD_VPORTS; out_index++)
3498                 INIT_LIST_HEAD(&flow->encaps[out_index].list);
3499         INIT_LIST_HEAD(&flow->mod_hdr);
3500         INIT_LIST_HEAD(&flow->hairpin);
3501         refcount_set(&flow->refcnt, 1);
3502         init_completion(&flow->init_done);
3503
3504         *__flow = flow;
3505         *__parse_attr = parse_attr;
3506
3507         return 0;
3508
3509 err_free:
3510         kfree(flow);
3511         kvfree(parse_attr);
3512         return err;
3513 }
3514
3515 static void
3516 mlx5e_flow_esw_attr_init(struct mlx5_esw_flow_attr *esw_attr,
3517                          struct mlx5e_priv *priv,
3518                          struct mlx5e_tc_flow_parse_attr *parse_attr,
3519                          struct flow_cls_offload *f,
3520                          struct mlx5_eswitch_rep *in_rep,
3521                          struct mlx5_core_dev *in_mdev)
3522 {
3523         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
3524
3525         esw_attr->parse_attr = parse_attr;
3526         esw_attr->chain = f->common.chain_index;
3527         esw_attr->prio = f->common.prio;
3528
3529         esw_attr->in_rep = in_rep;
3530         esw_attr->in_mdev = in_mdev;
3531
3532         if (MLX5_CAP_ESW(esw->dev, counter_eswitch_affinity) ==
3533             MLX5_COUNTER_SOURCE_ESWITCH)
3534                 esw_attr->counter_dev = in_mdev;
3535         else
3536                 esw_attr->counter_dev = priv->mdev;
3537 }
3538
3539 static struct mlx5e_tc_flow *
3540 __mlx5e_add_fdb_flow(struct mlx5e_priv *priv,
3541                      struct flow_cls_offload *f,
3542                      unsigned long flow_flags,
3543                      struct net_device *filter_dev,
3544                      struct mlx5_eswitch_rep *in_rep,
3545                      struct mlx5_core_dev *in_mdev)
3546 {
3547         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
3548         struct netlink_ext_ack *extack = f->common.extack;
3549         struct mlx5e_tc_flow_parse_attr *parse_attr;
3550         struct mlx5e_tc_flow *flow;
3551         int attr_size, err;
3552
3553         flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_ESWITCH);
3554         attr_size  = sizeof(struct mlx5_esw_flow_attr);
3555         err = mlx5e_alloc_flow(priv, attr_size, f, flow_flags,
3556                                &parse_attr, &flow);
3557         if (err)
3558                 goto out;
3559
3560         parse_attr->filter_dev = filter_dev;
3561         mlx5e_flow_esw_attr_init(flow->esw_attr,
3562                                  priv, parse_attr,
3563                                  f, in_rep, in_mdev);
3564
3565         err = parse_cls_flower(flow->priv, flow, &parse_attr->spec,
3566                                f, filter_dev);
3567         if (err)
3568                 goto err_free;
3569
3570         err = parse_tc_fdb_actions(priv, &rule->action, flow, extack);
3571         if (err)
3572                 goto err_free;
3573
3574         err = mlx5e_tc_add_fdb_flow(priv, flow, extack);
3575         complete_all(&flow->init_done);
3576         if (err) {
3577                 if (!(err == -ENETUNREACH && mlx5_lag_is_multipath(in_mdev)))
3578                         goto err_free;
3579
3580                 add_unready_flow(flow);
3581         }
3582
3583         return flow;
3584
3585 err_free:
3586         mlx5e_flow_put(priv, flow);
3587 out:
3588         return ERR_PTR(err);
3589 }
3590
3591 static int mlx5e_tc_add_fdb_peer_flow(struct flow_cls_offload *f,
3592                                       struct mlx5e_tc_flow *flow,
3593                                       unsigned long flow_flags)
3594 {
3595         struct mlx5e_priv *priv = flow->priv, *peer_priv;
3596         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch, *peer_esw;
3597         struct mlx5_devcom *devcom = priv->mdev->priv.devcom;
3598         struct mlx5e_tc_flow_parse_attr *parse_attr;
3599         struct mlx5e_rep_priv *peer_urpriv;
3600         struct mlx5e_tc_flow *peer_flow;
3601         struct mlx5_core_dev *in_mdev;
3602         int err = 0;
3603
3604         peer_esw = mlx5_devcom_get_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
3605         if (!peer_esw)
3606                 return -ENODEV;
3607
3608         peer_urpriv = mlx5_eswitch_get_uplink_priv(peer_esw, REP_ETH);
3609         peer_priv = netdev_priv(peer_urpriv->netdev);
3610
3611         /* in_mdev is assigned of which the packet originated from.
3612          * So packets redirected to uplink use the same mdev of the
3613          * original flow and packets redirected from uplink use the
3614          * peer mdev.
3615          */
3616         if (flow->esw_attr->in_rep->vport == MLX5_VPORT_UPLINK)
3617                 in_mdev = peer_priv->mdev;
3618         else
3619                 in_mdev = priv->mdev;
3620
3621         parse_attr = flow->esw_attr->parse_attr;
3622         peer_flow = __mlx5e_add_fdb_flow(peer_priv, f, flow_flags,
3623                                          parse_attr->filter_dev,
3624                                          flow->esw_attr->in_rep, in_mdev);
3625         if (IS_ERR(peer_flow)) {
3626                 err = PTR_ERR(peer_flow);
3627                 goto out;
3628         }
3629
3630         flow->peer_flow = peer_flow;
3631         flow_flag_set(flow, DUP);
3632         mutex_lock(&esw->offloads.peer_mutex);
3633         list_add_tail(&flow->peer, &esw->offloads.peer_flows);
3634         mutex_unlock(&esw->offloads.peer_mutex);
3635
3636 out:
3637         mlx5_devcom_release_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
3638         return err;
3639 }
3640
3641 static int
3642 mlx5e_add_fdb_flow(struct mlx5e_priv *priv,
3643                    struct flow_cls_offload *f,
3644                    unsigned long flow_flags,
3645                    struct net_device *filter_dev,
3646                    struct mlx5e_tc_flow **__flow)
3647 {
3648         struct mlx5e_rep_priv *rpriv = priv->ppriv;
3649         struct mlx5_eswitch_rep *in_rep = rpriv->rep;
3650         struct mlx5_core_dev *in_mdev = priv->mdev;
3651         struct mlx5e_tc_flow *flow;
3652         int err;
3653
3654         flow = __mlx5e_add_fdb_flow(priv, f, flow_flags, filter_dev, in_rep,
3655                                     in_mdev);
3656         if (IS_ERR(flow))
3657                 return PTR_ERR(flow);
3658
3659         if (is_peer_flow_needed(flow)) {
3660                 err = mlx5e_tc_add_fdb_peer_flow(f, flow, flow_flags);
3661                 if (err) {
3662                         mlx5e_tc_del_fdb_flow(priv, flow);
3663                         goto out;
3664                 }
3665         }
3666
3667         *__flow = flow;
3668
3669         return 0;
3670
3671 out:
3672         return err;
3673 }
3674
3675 static int
3676 mlx5e_add_nic_flow(struct mlx5e_priv *priv,
3677                    struct flow_cls_offload *f,
3678                    unsigned long flow_flags,
3679                    struct net_device *filter_dev,
3680                    struct mlx5e_tc_flow **__flow)
3681 {
3682         struct flow_rule *rule = flow_cls_offload_flow_rule(f);
3683         struct netlink_ext_ack *extack = f->common.extack;
3684         struct mlx5e_tc_flow_parse_attr *parse_attr;
3685         struct mlx5e_tc_flow *flow;
3686         int attr_size, err;
3687
3688         /* multi-chain not supported for NIC rules */
3689         if (!tc_cls_can_offload_and_chain0(priv->netdev, &f->common))
3690                 return -EOPNOTSUPP;
3691
3692         flow_flags |= BIT(MLX5E_TC_FLOW_FLAG_NIC);
3693         attr_size  = sizeof(struct mlx5_nic_flow_attr);
3694         err = mlx5e_alloc_flow(priv, attr_size, f, flow_flags,
3695                                &parse_attr, &flow);
3696         if (err)
3697                 goto out;
3698
3699         parse_attr->filter_dev = filter_dev;
3700         err = parse_cls_flower(flow->priv, flow, &parse_attr->spec,
3701                                f, filter_dev);
3702         if (err)
3703                 goto err_free;
3704
3705         err = parse_tc_nic_actions(priv, &rule->action, parse_attr, flow, extack);
3706         if (err)
3707                 goto err_free;
3708
3709         err = mlx5e_tc_add_nic_flow(priv, parse_attr, flow, extack);
3710         if (err)
3711                 goto err_free;
3712
3713         flow_flag_set(flow, OFFLOADED);
3714         kvfree(parse_attr);
3715         *__flow = flow;
3716
3717         return 0;
3718
3719 err_free:
3720         mlx5e_flow_put(priv, flow);
3721         kvfree(parse_attr);
3722 out:
3723         return err;
3724 }
3725
3726 static int
3727 mlx5e_tc_add_flow(struct mlx5e_priv *priv,
3728                   struct flow_cls_offload *f,
3729                   unsigned long flags,
3730                   struct net_device *filter_dev,
3731                   struct mlx5e_tc_flow **flow)
3732 {
3733         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
3734         unsigned long flow_flags;
3735         int err;
3736
3737         get_flags(flags, &flow_flags);
3738
3739         if (!tc_can_offload_extack(priv->netdev, f->common.extack))
3740                 return -EOPNOTSUPP;
3741
3742         if (esw && esw->mode == MLX5_ESWITCH_OFFLOADS)
3743                 err = mlx5e_add_fdb_flow(priv, f, flow_flags,
3744                                          filter_dev, flow);
3745         else
3746                 err = mlx5e_add_nic_flow(priv, f, flow_flags,
3747                                          filter_dev, flow);
3748
3749         return err;
3750 }
3751
3752 int mlx5e_configure_flower(struct net_device *dev, struct mlx5e_priv *priv,
3753                            struct flow_cls_offload *f, unsigned long flags)
3754 {
3755         struct netlink_ext_ack *extack = f->common.extack;
3756         struct rhashtable *tc_ht = get_tc_ht(priv, flags);
3757         struct mlx5e_tc_flow *flow;
3758         int err = 0;
3759
3760         rcu_read_lock();
3761         flow = rhashtable_lookup(tc_ht, &f->cookie, tc_ht_params);
3762         rcu_read_unlock();
3763         if (flow) {
3764                 NL_SET_ERR_MSG_MOD(extack,
3765                                    "flow cookie already exists, ignoring");
3766                 netdev_warn_once(priv->netdev,
3767                                  "flow cookie %lx already exists, ignoring\n",
3768                                  f->cookie);
3769                 err = -EEXIST;
3770                 goto out;
3771         }
3772
3773         trace_mlx5e_configure_flower(f);
3774         err = mlx5e_tc_add_flow(priv, f, flags, dev, &flow);
3775         if (err)
3776                 goto out;
3777
3778         err = rhashtable_lookup_insert_fast(tc_ht, &flow->node, tc_ht_params);
3779         if (err)
3780                 goto err_free;
3781
3782         return 0;
3783
3784 err_free:
3785         mlx5e_flow_put(priv, flow);
3786 out:
3787         return err;
3788 }
3789
3790 static bool same_flow_direction(struct mlx5e_tc_flow *flow, int flags)
3791 {
3792         bool dir_ingress = !!(flags & MLX5_TC_FLAG(INGRESS));
3793         bool dir_egress = !!(flags & MLX5_TC_FLAG(EGRESS));
3794
3795         return flow_flag_test(flow, INGRESS) == dir_ingress &&
3796                 flow_flag_test(flow, EGRESS) == dir_egress;
3797 }
3798
3799 int mlx5e_delete_flower(struct net_device *dev, struct mlx5e_priv *priv,
3800                         struct flow_cls_offload *f, unsigned long flags)
3801 {
3802         struct rhashtable *tc_ht = get_tc_ht(priv, flags);
3803         struct mlx5e_tc_flow *flow;
3804         int err;
3805
3806         rcu_read_lock();
3807         flow = rhashtable_lookup_fast(tc_ht, &f->cookie, tc_ht_params);
3808         if (!flow || !same_flow_direction(flow, flags)) {
3809                 err = -EINVAL;
3810                 goto errout;
3811         }
3812
3813         /* Only delete the flow if it doesn't have MLX5E_TC_FLOW_DELETED flag
3814          * set.
3815          */
3816         if (flow_flag_test_and_set(flow, DELETED)) {
3817                 err = -EINVAL;
3818                 goto errout;
3819         }
3820         rhashtable_remove_fast(tc_ht, &flow->node, tc_ht_params);
3821         rcu_read_unlock();
3822
3823         trace_mlx5e_delete_flower(f);
3824         mlx5e_flow_put(priv, flow);
3825
3826         return 0;
3827
3828 errout:
3829         rcu_read_unlock();
3830         return err;
3831 }
3832
3833 int mlx5e_stats_flower(struct net_device *dev, struct mlx5e_priv *priv,
3834                        struct flow_cls_offload *f, unsigned long flags)
3835 {
3836         struct mlx5_devcom *devcom = priv->mdev->priv.devcom;
3837         struct rhashtable *tc_ht = get_tc_ht(priv, flags);
3838         struct mlx5_eswitch *peer_esw;
3839         struct mlx5e_tc_flow *flow;
3840         struct mlx5_fc *counter;
3841         u64 lastuse = 0;
3842         u64 packets = 0;
3843         u64 bytes = 0;
3844         int err = 0;
3845
3846         rcu_read_lock();
3847         flow = mlx5e_flow_get(rhashtable_lookup(tc_ht, &f->cookie,
3848                                                 tc_ht_params));
3849         rcu_read_unlock();
3850         if (IS_ERR(flow))
3851                 return PTR_ERR(flow);
3852
3853         if (!same_flow_direction(flow, flags)) {
3854                 err = -EINVAL;
3855                 goto errout;
3856         }
3857
3858         if (mlx5e_is_offloaded_flow(flow)) {
3859                 counter = mlx5e_tc_get_counter(flow);
3860                 if (!counter)
3861                         goto errout;
3862
3863                 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
3864         }
3865
3866         /* Under multipath it's possible for one rule to be currently
3867          * un-offloaded while the other rule is offloaded.
3868          */
3869         peer_esw = mlx5_devcom_get_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
3870         if (!peer_esw)
3871                 goto out;
3872
3873         if (flow_flag_test(flow, DUP) &&
3874             flow_flag_test(flow->peer_flow, OFFLOADED)) {
3875                 u64 bytes2;
3876                 u64 packets2;
3877                 u64 lastuse2;
3878
3879                 counter = mlx5e_tc_get_counter(flow->peer_flow);
3880                 if (!counter)
3881                         goto no_peer_counter;
3882                 mlx5_fc_query_cached(counter, &bytes2, &packets2, &lastuse2);
3883
3884                 bytes += bytes2;
3885                 packets += packets2;
3886                 lastuse = max_t(u64, lastuse, lastuse2);
3887         }
3888
3889 no_peer_counter:
3890         mlx5_devcom_release_peer_data(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
3891 out:
3892         flow_stats_update(&f->stats, bytes, packets, lastuse);
3893         trace_mlx5e_stats_flower(f);
3894 errout:
3895         mlx5e_flow_put(priv, flow);
3896         return err;
3897 }
3898
3899 static int apply_police_params(struct mlx5e_priv *priv, u32 rate,
3900                                struct netlink_ext_ack *extack)
3901 {
3902         struct mlx5e_rep_priv *rpriv = priv->ppriv;
3903         struct mlx5_eswitch *esw;
3904         u16 vport_num;
3905         u32 rate_mbps;
3906         int err;
3907
3908         esw = priv->mdev->priv.eswitch;
3909         /* rate is given in bytes/sec.
3910          * First convert to bits/sec and then round to the nearest mbit/secs.
3911          * mbit means million bits.
3912          * Moreover, if rate is non zero we choose to configure to a minimum of
3913          * 1 mbit/sec.
3914          */
3915         rate_mbps = rate ? max_t(u32, (rate * 8 + 500000) / 1000000, 1) : 0;
3916         vport_num = rpriv->rep->vport;
3917
3918         err = mlx5_esw_modify_vport_rate(esw, vport_num, rate_mbps);
3919         if (err)
3920                 NL_SET_ERR_MSG_MOD(extack, "failed applying action to hardware");
3921
3922         return err;
3923 }
3924
3925 static int scan_tc_matchall_fdb_actions(struct mlx5e_priv *priv,
3926                                         struct flow_action *flow_action,
3927                                         struct netlink_ext_ack *extack)
3928 {
3929         struct mlx5e_rep_priv *rpriv = priv->ppriv;
3930         const struct flow_action_entry *act;
3931         int err;
3932         int i;
3933
3934         if (!flow_action_has_entries(flow_action)) {
3935                 NL_SET_ERR_MSG_MOD(extack, "matchall called with no action");
3936                 return -EINVAL;
3937         }
3938
3939         if (!flow_offload_has_one_action(flow_action)) {
3940                 NL_SET_ERR_MSG_MOD(extack, "matchall policing support only a single action");
3941                 return -EOPNOTSUPP;
3942         }
3943
3944         flow_action_for_each(i, act, flow_action) {
3945                 switch (act->id) {
3946                 case FLOW_ACTION_POLICE:
3947                         err = apply_police_params(priv, act->police.rate_bytes_ps, extack);
3948                         if (err)
3949                                 return err;
3950
3951                         rpriv->prev_vf_vport_stats = priv->stats.vf_vport;
3952                         break;
3953                 default:
3954                         NL_SET_ERR_MSG_MOD(extack, "mlx5 supports only police action for matchall");
3955                         return -EOPNOTSUPP;
3956                 }
3957         }
3958
3959         return 0;
3960 }
3961
3962 int mlx5e_tc_configure_matchall(struct mlx5e_priv *priv,
3963                                 struct tc_cls_matchall_offload *ma)
3964 {
3965         struct netlink_ext_ack *extack = ma->common.extack;
3966         int prio = TC_H_MAJ(ma->common.prio) >> 16;
3967
3968         if (prio != 1) {
3969                 NL_SET_ERR_MSG_MOD(extack, "only priority 1 is supported");
3970                 return -EINVAL;
3971         }
3972
3973         return scan_tc_matchall_fdb_actions(priv, &ma->rule->action, extack);
3974 }
3975
3976 int mlx5e_tc_delete_matchall(struct mlx5e_priv *priv,
3977                              struct tc_cls_matchall_offload *ma)
3978 {
3979         struct netlink_ext_ack *extack = ma->common.extack;
3980
3981         return apply_police_params(priv, 0, extack);
3982 }
3983
3984 void mlx5e_tc_stats_matchall(struct mlx5e_priv *priv,
3985                              struct tc_cls_matchall_offload *ma)
3986 {
3987         struct mlx5e_rep_priv *rpriv = priv->ppriv;
3988         struct rtnl_link_stats64 cur_stats;
3989         u64 dbytes;
3990         u64 dpkts;
3991
3992         cur_stats = priv->stats.vf_vport;
3993         dpkts = cur_stats.rx_packets - rpriv->prev_vf_vport_stats.rx_packets;
3994         dbytes = cur_stats.rx_bytes - rpriv->prev_vf_vport_stats.rx_bytes;
3995         rpriv->prev_vf_vport_stats = cur_stats;
3996         flow_stats_update(&ma->stats, dpkts, dbytes, jiffies);
3997 }
3998
3999 static void mlx5e_tc_hairpin_update_dead_peer(struct mlx5e_priv *priv,
4000                                               struct mlx5e_priv *peer_priv)
4001 {
4002         struct mlx5_core_dev *peer_mdev = peer_priv->mdev;
4003         struct mlx5e_hairpin_entry *hpe, *tmp;
4004         LIST_HEAD(init_wait_list);
4005         u16 peer_vhca_id;
4006         int bkt;
4007
4008         if (!same_hw_devs(priv, peer_priv))
4009                 return;
4010
4011         peer_vhca_id = MLX5_CAP_GEN(peer_mdev, vhca_id);
4012
4013         mutex_lock(&priv->fs.tc.hairpin_tbl_lock);
4014         hash_for_each(priv->fs.tc.hairpin_tbl, bkt, hpe, hairpin_hlist)
4015                 if (refcount_inc_not_zero(&hpe->refcnt))
4016                         list_add(&hpe->dead_peer_wait_list, &init_wait_list);
4017         mutex_unlock(&priv->fs.tc.hairpin_tbl_lock);
4018
4019         list_for_each_entry_safe(hpe, tmp, &init_wait_list, dead_peer_wait_list) {
4020                 wait_for_completion(&hpe->res_ready);
4021                 if (!IS_ERR_OR_NULL(hpe->hp) && hpe->peer_vhca_id == peer_vhca_id)
4022                         hpe->hp->pair->peer_gone = true;
4023
4024                 mlx5e_hairpin_put(priv, hpe);
4025         }
4026 }
4027
4028 static int mlx5e_tc_netdev_event(struct notifier_block *this,
4029                                  unsigned long event, void *ptr)
4030 {
4031         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4032         struct mlx5e_flow_steering *fs;
4033         struct mlx5e_priv *peer_priv;
4034         struct mlx5e_tc_table *tc;
4035         struct mlx5e_priv *priv;
4036
4037         if (ndev->netdev_ops != &mlx5e_netdev_ops ||
4038             event != NETDEV_UNREGISTER ||
4039             ndev->reg_state == NETREG_REGISTERED)
4040                 return NOTIFY_DONE;
4041
4042         tc = container_of(this, struct mlx5e_tc_table, netdevice_nb);
4043         fs = container_of(tc, struct mlx5e_flow_steering, tc);
4044         priv = container_of(fs, struct mlx5e_priv, fs);
4045         peer_priv = netdev_priv(ndev);
4046         if (priv == peer_priv ||
4047             !(priv->netdev->features & NETIF_F_HW_TC))
4048                 return NOTIFY_DONE;
4049
4050         mlx5e_tc_hairpin_update_dead_peer(priv, peer_priv);
4051
4052         return NOTIFY_DONE;
4053 }
4054
4055 int mlx5e_tc_nic_init(struct mlx5e_priv *priv)
4056 {
4057         struct mlx5e_tc_table *tc = &priv->fs.tc;
4058         int err;
4059
4060         mutex_init(&tc->t_lock);
4061         mutex_init(&tc->mod_hdr.lock);
4062         hash_init(tc->mod_hdr.hlist);
4063         mutex_init(&tc->hairpin_tbl_lock);
4064         hash_init(tc->hairpin_tbl);
4065
4066         err = rhashtable_init(&tc->ht, &tc_ht_params);
4067         if (err)
4068                 return err;
4069
4070         tc->netdevice_nb.notifier_call = mlx5e_tc_netdev_event;
4071         if (register_netdevice_notifier(&tc->netdevice_nb)) {
4072                 tc->netdevice_nb.notifier_call = NULL;
4073                 mlx5_core_warn(priv->mdev, "Failed to register netdev notifier\n");
4074         }
4075
4076         return err;
4077 }
4078
4079 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
4080 {
4081         struct mlx5e_tc_flow *flow = ptr;
4082         struct mlx5e_priv *priv = flow->priv;
4083
4084         mlx5e_tc_del_flow(priv, flow);
4085         kfree(flow);
4086 }
4087
4088 void mlx5e_tc_nic_cleanup(struct mlx5e_priv *priv)
4089 {
4090         struct mlx5e_tc_table *tc = &priv->fs.tc;
4091
4092         if (tc->netdevice_nb.notifier_call)
4093                 unregister_netdevice_notifier(&tc->netdevice_nb);
4094
4095         mutex_destroy(&tc->mod_hdr.lock);
4096         mutex_destroy(&tc->hairpin_tbl_lock);
4097
4098         rhashtable_destroy(&tc->ht);
4099
4100         if (!IS_ERR_OR_NULL(tc->t)) {
4101                 mlx5_destroy_flow_table(tc->t);
4102                 tc->t = NULL;
4103         }
4104         mutex_destroy(&tc->t_lock);
4105 }
4106
4107 int mlx5e_tc_esw_init(struct rhashtable *tc_ht)
4108 {
4109         return rhashtable_init(tc_ht, &tc_ht_params);
4110 }
4111
4112 void mlx5e_tc_esw_cleanup(struct rhashtable *tc_ht)
4113 {
4114         rhashtable_free_and_destroy(tc_ht, _mlx5e_tc_del_flow, NULL);
4115 }
4116
4117 int mlx5e_tc_num_filters(struct mlx5e_priv *priv, unsigned long flags)
4118 {
4119         struct rhashtable *tc_ht = get_tc_ht(priv, flags);
4120
4121         return atomic_read(&tc_ht->nelems);
4122 }
4123
4124 void mlx5e_tc_clean_fdb_peer_flows(struct mlx5_eswitch *esw)
4125 {
4126         struct mlx5e_tc_flow *flow, *tmp;
4127
4128         list_for_each_entry_safe(flow, tmp, &esw->offloads.peer_flows, peer)
4129                 __mlx5e_tc_del_fdb_peer_flow(flow);
4130 }
4131
4132 void mlx5e_tc_reoffload_flows_work(struct work_struct *work)
4133 {
4134         struct mlx5_rep_uplink_priv *rpriv =
4135                 container_of(work, struct mlx5_rep_uplink_priv,
4136                              reoffload_flows_work);
4137         struct mlx5e_tc_flow *flow, *tmp;
4138
4139         mutex_lock(&rpriv->unready_flows_lock);
4140         list_for_each_entry_safe(flow, tmp, &rpriv->unready_flows, unready) {
4141                 if (!mlx5e_tc_add_fdb_flow(flow->priv, flow, NULL))
4142                         unready_flow_del(flow);
4143         }
4144         mutex_unlock(&rpriv->unready_flows_lock);
4145 }