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