net/mlx5e: Basic setup of hairpin object
[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
55 struct mlx5_nic_flow_attr {
56         u32 action;
57         u32 flow_tag;
58         u32 mod_hdr_id;
59 };
60
61 enum {
62         MLX5E_TC_FLOW_ESWITCH   = BIT(0),
63         MLX5E_TC_FLOW_NIC       = BIT(1),
64         MLX5E_TC_FLOW_OFFLOADED = BIT(2),
65 };
66
67 struct mlx5e_tc_flow {
68         struct rhash_head       node;
69         u64                     cookie;
70         u8                      flags;
71         struct mlx5_flow_handle *rule;
72         struct list_head        encap;   /* flows sharing the same encap ID */
73         struct list_head        mod_hdr; /* flows sharing the same mod hdr ID */
74         union {
75                 struct mlx5_esw_flow_attr esw_attr[0];
76                 struct mlx5_nic_flow_attr nic_attr[0];
77         };
78 };
79
80 struct mlx5e_tc_flow_parse_attr {
81         struct ip_tunnel_info tun_info;
82         struct mlx5_flow_spec spec;
83         int num_mod_hdr_actions;
84         void *mod_hdr_actions;
85         int mirred_ifindex;
86 };
87
88 enum {
89         MLX5_HEADER_TYPE_VXLAN = 0x0,
90         MLX5_HEADER_TYPE_NVGRE = 0x1,
91 };
92
93 #define MLX5E_TC_TABLE_NUM_GROUPS 4
94 #define MLX5E_TC_TABLE_MAX_GROUP_SIZE (1 << 16)
95
96 struct mlx5e_hairpin {
97         struct mlx5_hairpin *pair;
98
99         struct mlx5_core_dev *func_mdev;
100         u32 tdn;
101         u32 tirn;
102 };
103
104 struct mod_hdr_key {
105         int num_actions;
106         void *actions;
107 };
108
109 struct mlx5e_mod_hdr_entry {
110         /* a node of a hash table which keeps all the mod_hdr entries */
111         struct hlist_node mod_hdr_hlist;
112
113         /* flows sharing the same mod_hdr entry */
114         struct list_head flows;
115
116         struct mod_hdr_key key;
117
118         u32 mod_hdr_id;
119 };
120
121 #define MLX5_MH_ACT_SZ MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto)
122
123 static inline u32 hash_mod_hdr_info(struct mod_hdr_key *key)
124 {
125         return jhash(key->actions,
126                      key->num_actions * MLX5_MH_ACT_SZ, 0);
127 }
128
129 static inline int cmp_mod_hdr_info(struct mod_hdr_key *a,
130                                    struct mod_hdr_key *b)
131 {
132         if (a->num_actions != b->num_actions)
133                 return 1;
134
135         return memcmp(a->actions, b->actions, a->num_actions * MLX5_MH_ACT_SZ);
136 }
137
138 static int mlx5e_attach_mod_hdr(struct mlx5e_priv *priv,
139                                 struct mlx5e_tc_flow *flow,
140                                 struct mlx5e_tc_flow_parse_attr *parse_attr)
141 {
142         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
143         int num_actions, actions_size, namespace, err;
144         struct mlx5e_mod_hdr_entry *mh;
145         struct mod_hdr_key key;
146         bool found = false;
147         u32 hash_key;
148
149         num_actions  = parse_attr->num_mod_hdr_actions;
150         actions_size = MLX5_MH_ACT_SZ * num_actions;
151
152         key.actions = parse_attr->mod_hdr_actions;
153         key.num_actions = num_actions;
154
155         hash_key = hash_mod_hdr_info(&key);
156
157         if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
158                 namespace = MLX5_FLOW_NAMESPACE_FDB;
159                 hash_for_each_possible(esw->offloads.mod_hdr_tbl, mh,
160                                        mod_hdr_hlist, hash_key) {
161                         if (!cmp_mod_hdr_info(&mh->key, &key)) {
162                                 found = true;
163                                 break;
164                         }
165                 }
166         } else {
167                 namespace = MLX5_FLOW_NAMESPACE_KERNEL;
168                 hash_for_each_possible(priv->fs.tc.mod_hdr_tbl, mh,
169                                        mod_hdr_hlist, hash_key) {
170                         if (!cmp_mod_hdr_info(&mh->key, &key)) {
171                                 found = true;
172                                 break;
173                         }
174                 }
175         }
176
177         if (found)
178                 goto attach_flow;
179
180         mh = kzalloc(sizeof(*mh) + actions_size, GFP_KERNEL);
181         if (!mh)
182                 return -ENOMEM;
183
184         mh->key.actions = (void *)mh + sizeof(*mh);
185         memcpy(mh->key.actions, key.actions, actions_size);
186         mh->key.num_actions = num_actions;
187         INIT_LIST_HEAD(&mh->flows);
188
189         err = mlx5_modify_header_alloc(priv->mdev, namespace,
190                                        mh->key.num_actions,
191                                        mh->key.actions,
192                                        &mh->mod_hdr_id);
193         if (err)
194                 goto out_err;
195
196         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
197                 hash_add(esw->offloads.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
198         else
199                 hash_add(priv->fs.tc.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
200
201 attach_flow:
202         list_add(&flow->mod_hdr, &mh->flows);
203         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
204                 flow->esw_attr->mod_hdr_id = mh->mod_hdr_id;
205         else
206                 flow->nic_attr->mod_hdr_id = mh->mod_hdr_id;
207
208         return 0;
209
210 out_err:
211         kfree(mh);
212         return err;
213 }
214
215 static void mlx5e_detach_mod_hdr(struct mlx5e_priv *priv,
216                                  struct mlx5e_tc_flow *flow)
217 {
218         struct list_head *next = flow->mod_hdr.next;
219
220         list_del(&flow->mod_hdr);
221
222         if (list_empty(next)) {
223                 struct mlx5e_mod_hdr_entry *mh;
224
225                 mh = list_entry(next, struct mlx5e_mod_hdr_entry, flows);
226
227                 mlx5_modify_header_dealloc(priv->mdev, mh->mod_hdr_id);
228                 hash_del(&mh->mod_hdr_hlist);
229                 kfree(mh);
230         }
231 }
232
233 static
234 struct mlx5_core_dev *mlx5e_hairpin_get_mdev(struct net *net, int ifindex)
235 {
236         struct net_device *netdev;
237         struct mlx5e_priv *priv;
238
239         netdev = __dev_get_by_index(net, ifindex);
240         priv = netdev_priv(netdev);
241         return priv->mdev;
242 }
243
244 static int mlx5e_hairpin_create_transport(struct mlx5e_hairpin *hp)
245 {
246         u32 in[MLX5_ST_SZ_DW(create_tir_in)] = {0};
247         void *tirc;
248         int err;
249
250         err = mlx5_core_alloc_transport_domain(hp->func_mdev, &hp->tdn);
251         if (err)
252                 goto alloc_tdn_err;
253
254         tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
255
256         MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
257         MLX5_SET(tirc, tirc, inline_rqn, hp->pair->rqn);
258         MLX5_SET(tirc, tirc, transport_domain, hp->tdn);
259
260         err = mlx5_core_create_tir(hp->func_mdev, in, MLX5_ST_SZ_BYTES(create_tir_in), &hp->tirn);
261         if (err)
262                 goto create_tir_err;
263
264         return 0;
265
266 create_tir_err:
267         mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
268 alloc_tdn_err:
269         return err;
270 }
271
272 static void mlx5e_hairpin_destroy_transport(struct mlx5e_hairpin *hp)
273 {
274         mlx5_core_destroy_tir(hp->func_mdev, hp->tirn);
275         mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
276 }
277
278 static struct mlx5e_hairpin *
279 mlx5e_hairpin_create(struct mlx5e_priv *priv, struct mlx5_hairpin_params *params,
280                      int peer_ifindex)
281 {
282         struct mlx5_core_dev *func_mdev, *peer_mdev;
283         struct mlx5e_hairpin *hp;
284         struct mlx5_hairpin *pair;
285         int err;
286
287         hp = kzalloc(sizeof(*hp), GFP_KERNEL);
288         if (!hp)
289                 return ERR_PTR(-ENOMEM);
290
291         func_mdev = priv->mdev;
292         peer_mdev = mlx5e_hairpin_get_mdev(dev_net(priv->netdev), peer_ifindex);
293
294         pair = mlx5_core_hairpin_create(func_mdev, peer_mdev, params);
295         if (IS_ERR(pair)) {
296                 err = PTR_ERR(pair);
297                 goto create_pair_err;
298         }
299         hp->pair = pair;
300         hp->func_mdev = func_mdev;
301
302         err = mlx5e_hairpin_create_transport(hp);
303         if (err)
304                 goto create_transport_err;
305
306         return hp;
307
308 create_transport_err:
309         mlx5_core_hairpin_destroy(hp->pair);
310 create_pair_err:
311         kfree(hp);
312         return ERR_PTR(err);
313 }
314
315 static void mlx5e_hairpin_destroy(struct mlx5e_hairpin *hp)
316 {
317         mlx5e_hairpin_destroy_transport(hp);
318         mlx5_core_hairpin_destroy(hp->pair);
319         kvfree(hp);
320 }
321
322 static struct mlx5_flow_handle *
323 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
324                       struct mlx5e_tc_flow_parse_attr *parse_attr,
325                       struct mlx5e_tc_flow *flow)
326 {
327         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
328         struct mlx5_core_dev *dev = priv->mdev;
329         struct mlx5_flow_destination dest = {};
330         struct mlx5_flow_act flow_act = {
331                 .action = attr->action,
332                 .flow_tag = attr->flow_tag,
333                 .encap_id = 0,
334         };
335         struct mlx5_fc *counter = NULL;
336         struct mlx5_flow_handle *rule;
337         bool table_created = false;
338         int err;
339
340         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
341                 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
342                 dest.ft = priv->fs.vlan.ft.t;
343         } else if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
344                 counter = mlx5_fc_create(dev, true);
345                 if (IS_ERR(counter))
346                         return ERR_CAST(counter);
347
348                 dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
349                 dest.counter = counter;
350         }
351
352         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
353                 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
354                 flow_act.modify_id = attr->mod_hdr_id;
355                 kfree(parse_attr->mod_hdr_actions);
356                 if (err) {
357                         rule = ERR_PTR(err);
358                         goto err_create_mod_hdr_id;
359                 }
360         }
361
362         if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
363                 int tc_grp_size, tc_tbl_size;
364                 u32 max_flow_counter;
365
366                 max_flow_counter = (MLX5_CAP_GEN(dev, max_flow_counter_31_16) << 16) |
367                                     MLX5_CAP_GEN(dev, max_flow_counter_15_0);
368
369                 tc_grp_size = min_t(int, max_flow_counter, MLX5E_TC_TABLE_MAX_GROUP_SIZE);
370
371                 tc_tbl_size = min_t(int, tc_grp_size * MLX5E_TC_TABLE_NUM_GROUPS,
372                                     BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev, log_max_ft_size)));
373
374                 priv->fs.tc.t =
375                         mlx5_create_auto_grouped_flow_table(priv->fs.ns,
376                                                             MLX5E_TC_PRIO,
377                                                             tc_tbl_size,
378                                                             MLX5E_TC_TABLE_NUM_GROUPS,
379                                                             0, 0);
380                 if (IS_ERR(priv->fs.tc.t)) {
381                         netdev_err(priv->netdev,
382                                    "Failed to create tc offload table\n");
383                         rule = ERR_CAST(priv->fs.tc.t);
384                         goto err_create_ft;
385                 }
386
387                 table_created = true;
388         }
389
390         parse_attr->spec.match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
391         rule = mlx5_add_flow_rules(priv->fs.tc.t, &parse_attr->spec,
392                                    &flow_act, &dest, 1);
393
394         if (IS_ERR(rule))
395                 goto err_add_rule;
396
397         return rule;
398
399 err_add_rule:
400         if (table_created) {
401                 mlx5_destroy_flow_table(priv->fs.tc.t);
402                 priv->fs.tc.t = NULL;
403         }
404 err_create_ft:
405         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
406                 mlx5e_detach_mod_hdr(priv, flow);
407 err_create_mod_hdr_id:
408         mlx5_fc_destroy(dev, counter);
409
410         return rule;
411 }
412
413 static void mlx5e_tc_del_nic_flow(struct mlx5e_priv *priv,
414                                   struct mlx5e_tc_flow *flow)
415 {
416         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
417         struct mlx5_fc *counter = NULL;
418
419         counter = mlx5_flow_rule_counter(flow->rule);
420         mlx5_del_flow_rules(flow->rule);
421         mlx5_fc_destroy(priv->mdev, counter);
422
423         if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
424                 mlx5_destroy_flow_table(priv->fs.tc.t);
425                 priv->fs.tc.t = NULL;
426         }
427
428         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
429                 mlx5e_detach_mod_hdr(priv, flow);
430 }
431
432 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
433                                struct mlx5e_tc_flow *flow);
434
435 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
436                               struct ip_tunnel_info *tun_info,
437                               struct net_device *mirred_dev,
438                               struct net_device **encap_dev,
439                               struct mlx5e_tc_flow *flow);
440
441 static struct mlx5_flow_handle *
442 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
443                       struct mlx5e_tc_flow_parse_attr *parse_attr,
444                       struct mlx5e_tc_flow *flow)
445 {
446         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
447         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
448         struct net_device *out_dev, *encap_dev = NULL;
449         struct mlx5_flow_handle *rule = NULL;
450         struct mlx5e_rep_priv *rpriv;
451         struct mlx5e_priv *out_priv;
452         int err;
453
454         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
455                 out_dev = __dev_get_by_index(dev_net(priv->netdev),
456                                              attr->parse_attr->mirred_ifindex);
457                 err = mlx5e_attach_encap(priv, &parse_attr->tun_info,
458                                          out_dev, &encap_dev, flow);
459                 if (err) {
460                         rule = ERR_PTR(err);
461                         if (err != -EAGAIN)
462                                 goto err_attach_encap;
463                 }
464                 out_priv = netdev_priv(encap_dev);
465                 rpriv = out_priv->ppriv;
466                 attr->out_rep = rpriv->rep;
467         }
468
469         err = mlx5_eswitch_add_vlan_action(esw, attr);
470         if (err) {
471                 rule = ERR_PTR(err);
472                 goto err_add_vlan;
473         }
474
475         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
476                 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
477                 kfree(parse_attr->mod_hdr_actions);
478                 if (err) {
479                         rule = ERR_PTR(err);
480                         goto err_mod_hdr;
481                 }
482         }
483
484         /* we get here if (1) there's no error (rule being null) or when
485          * (2) there's an encap action and we're on -EAGAIN (no valid neigh)
486          */
487         if (rule != ERR_PTR(-EAGAIN)) {
488                 rule = mlx5_eswitch_add_offloaded_rule(esw, &parse_attr->spec, attr);
489                 if (IS_ERR(rule))
490                         goto err_add_rule;
491         }
492         return rule;
493
494 err_add_rule:
495         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
496                 mlx5e_detach_mod_hdr(priv, flow);
497 err_mod_hdr:
498         mlx5_eswitch_del_vlan_action(esw, attr);
499 err_add_vlan:
500         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
501                 mlx5e_detach_encap(priv, flow);
502 err_attach_encap:
503         return rule;
504 }
505
506 static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
507                                   struct mlx5e_tc_flow *flow)
508 {
509         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
510         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
511
512         if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
513                 flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
514                 mlx5_eswitch_del_offloaded_rule(esw, flow->rule, attr);
515         }
516
517         mlx5_eswitch_del_vlan_action(esw, attr);
518
519         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
520                 mlx5e_detach_encap(priv, flow);
521                 kvfree(attr->parse_attr);
522         }
523
524         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
525                 mlx5e_detach_mod_hdr(priv, flow);
526 }
527
528 void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
529                               struct mlx5e_encap_entry *e)
530 {
531         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
532         struct mlx5_esw_flow_attr *esw_attr;
533         struct mlx5e_tc_flow *flow;
534         int err;
535
536         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
537                                e->encap_size, e->encap_header,
538                                &e->encap_id);
539         if (err) {
540                 mlx5_core_warn(priv->mdev, "Failed to offload cached encapsulation header, %d\n",
541                                err);
542                 return;
543         }
544         e->flags |= MLX5_ENCAP_ENTRY_VALID;
545         mlx5e_rep_queue_neigh_stats_work(priv);
546
547         list_for_each_entry(flow, &e->flows, encap) {
548                 esw_attr = flow->esw_attr;
549                 esw_attr->encap_id = e->encap_id;
550                 flow->rule = mlx5_eswitch_add_offloaded_rule(esw, &esw_attr->parse_attr->spec, esw_attr);
551                 if (IS_ERR(flow->rule)) {
552                         err = PTR_ERR(flow->rule);
553                         mlx5_core_warn(priv->mdev, "Failed to update cached encapsulation flow, %d\n",
554                                        err);
555                         continue;
556                 }
557                 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
558         }
559 }
560
561 void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
562                               struct mlx5e_encap_entry *e)
563 {
564         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
565         struct mlx5e_tc_flow *flow;
566
567         list_for_each_entry(flow, &e->flows, encap) {
568                 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
569                         flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
570                         mlx5_eswitch_del_offloaded_rule(esw, flow->rule, flow->esw_attr);
571                 }
572         }
573
574         if (e->flags & MLX5_ENCAP_ENTRY_VALID) {
575                 e->flags &= ~MLX5_ENCAP_ENTRY_VALID;
576                 mlx5_encap_dealloc(priv->mdev, e->encap_id);
577         }
578 }
579
580 void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
581 {
582         struct mlx5e_neigh *m_neigh = &nhe->m_neigh;
583         u64 bytes, packets, lastuse = 0;
584         struct mlx5e_tc_flow *flow;
585         struct mlx5e_encap_entry *e;
586         struct mlx5_fc *counter;
587         struct neigh_table *tbl;
588         bool neigh_used = false;
589         struct neighbour *n;
590
591         if (m_neigh->family == AF_INET)
592                 tbl = &arp_tbl;
593 #if IS_ENABLED(CONFIG_IPV6)
594         else if (m_neigh->family == AF_INET6)
595                 tbl = ipv6_stub->nd_tbl;
596 #endif
597         else
598                 return;
599
600         list_for_each_entry(e, &nhe->encap_list, encap_list) {
601                 if (!(e->flags & MLX5_ENCAP_ENTRY_VALID))
602                         continue;
603                 list_for_each_entry(flow, &e->flows, encap) {
604                         if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
605                                 counter = mlx5_flow_rule_counter(flow->rule);
606                                 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
607                                 if (time_after((unsigned long)lastuse, nhe->reported_lastuse)) {
608                                         neigh_used = true;
609                                         break;
610                                 }
611                         }
612                 }
613         }
614
615         if (neigh_used) {
616                 nhe->reported_lastuse = jiffies;
617
618                 /* find the relevant neigh according to the cached device and
619                  * dst ip pair
620                  */
621                 n = neigh_lookup(tbl, &m_neigh->dst_ip, m_neigh->dev);
622                 if (!n) {
623                         WARN(1, "The neighbour already freed\n");
624                         return;
625                 }
626
627                 neigh_event_send(n, NULL);
628                 neigh_release(n);
629         }
630 }
631
632 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
633                                struct mlx5e_tc_flow *flow)
634 {
635         struct list_head *next = flow->encap.next;
636
637         list_del(&flow->encap);
638         if (list_empty(next)) {
639                 struct mlx5e_encap_entry *e;
640
641                 e = list_entry(next, struct mlx5e_encap_entry, flows);
642                 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
643
644                 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
645                         mlx5_encap_dealloc(priv->mdev, e->encap_id);
646
647                 hash_del_rcu(&e->encap_hlist);
648                 kfree(e->encap_header);
649                 kfree(e);
650         }
651 }
652
653 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
654                               struct mlx5e_tc_flow *flow)
655 {
656         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
657                 mlx5e_tc_del_fdb_flow(priv, flow);
658         else
659                 mlx5e_tc_del_nic_flow(priv, flow);
660 }
661
662 static void parse_vxlan_attr(struct mlx5_flow_spec *spec,
663                              struct tc_cls_flower_offload *f)
664 {
665         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
666                                        outer_headers);
667         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
668                                        outer_headers);
669         void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
670                                     misc_parameters);
671         void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
672                                     misc_parameters);
673
674         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
675         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
676
677         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
678                 struct flow_dissector_key_keyid *key =
679                         skb_flow_dissector_target(f->dissector,
680                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
681                                                   f->key);
682                 struct flow_dissector_key_keyid *mask =
683                         skb_flow_dissector_target(f->dissector,
684                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
685                                                   f->mask);
686                 MLX5_SET(fte_match_set_misc, misc_c, vxlan_vni,
687                          be32_to_cpu(mask->keyid));
688                 MLX5_SET(fte_match_set_misc, misc_v, vxlan_vni,
689                          be32_to_cpu(key->keyid));
690         }
691 }
692
693 static int parse_tunnel_attr(struct mlx5e_priv *priv,
694                              struct mlx5_flow_spec *spec,
695                              struct tc_cls_flower_offload *f)
696 {
697         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
698                                        outer_headers);
699         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
700                                        outer_headers);
701
702         struct flow_dissector_key_control *enc_control =
703                 skb_flow_dissector_target(f->dissector,
704                                           FLOW_DISSECTOR_KEY_ENC_CONTROL,
705                                           f->key);
706
707         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
708                 struct flow_dissector_key_ports *key =
709                         skb_flow_dissector_target(f->dissector,
710                                                   FLOW_DISSECTOR_KEY_ENC_PORTS,
711                                                   f->key);
712                 struct flow_dissector_key_ports *mask =
713                         skb_flow_dissector_target(f->dissector,
714                                                   FLOW_DISSECTOR_KEY_ENC_PORTS,
715                                                   f->mask);
716                 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
717                 struct mlx5e_rep_priv *uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
718                 struct net_device *up_dev = uplink_rpriv->netdev;
719                 struct mlx5e_priv *up_priv = netdev_priv(up_dev);
720
721                 /* Full udp dst port must be given */
722                 if (memchr_inv(&mask->dst, 0xff, sizeof(mask->dst)))
723                         goto vxlan_match_offload_err;
724
725                 if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->dst)) &&
726                     MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap))
727                         parse_vxlan_attr(spec, f);
728                 else {
729                         netdev_warn(priv->netdev,
730                                     "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->dst));
731                         return -EOPNOTSUPP;
732                 }
733
734                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
735                          udp_dport, ntohs(mask->dst));
736                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
737                          udp_dport, ntohs(key->dst));
738
739                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
740                          udp_sport, ntohs(mask->src));
741                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
742                          udp_sport, ntohs(key->src));
743         } else { /* udp dst port must be given */
744 vxlan_match_offload_err:
745                 netdev_warn(priv->netdev,
746                             "IP tunnel decap offload supported only for vxlan, must set UDP dport\n");
747                 return -EOPNOTSUPP;
748         }
749
750         if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
751                 struct flow_dissector_key_ipv4_addrs *key =
752                         skb_flow_dissector_target(f->dissector,
753                                                   FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
754                                                   f->key);
755                 struct flow_dissector_key_ipv4_addrs *mask =
756                         skb_flow_dissector_target(f->dissector,
757                                                   FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
758                                                   f->mask);
759                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
760                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
761                          ntohl(mask->src));
762                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
763                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
764                          ntohl(key->src));
765
766                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
767                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
768                          ntohl(mask->dst));
769                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
770                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
771                          ntohl(key->dst));
772
773                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
774                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IP);
775         } else if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
776                 struct flow_dissector_key_ipv6_addrs *key =
777                         skb_flow_dissector_target(f->dissector,
778                                                   FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
779                                                   f->key);
780                 struct flow_dissector_key_ipv6_addrs *mask =
781                         skb_flow_dissector_target(f->dissector,
782                                                   FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
783                                                   f->mask);
784
785                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
786                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
787                        &mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
788                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
789                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
790                        &key->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
791
792                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
793                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
794                        &mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
795                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
796                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
797                        &key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
798
799                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
800                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IPV6);
801         }
802
803         /* Enforce DMAC when offloading incoming tunneled flows.
804          * Flow counters require a match on the DMAC.
805          */
806         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_47_16);
807         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_15_0);
808         ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
809                                      dmac_47_16), priv->netdev->dev_addr);
810
811         /* let software handle IP fragments */
812         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
813         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
814
815         return 0;
816 }
817
818 static int __parse_cls_flower(struct mlx5e_priv *priv,
819                               struct mlx5_flow_spec *spec,
820                               struct tc_cls_flower_offload *f,
821                               u8 *min_inline)
822 {
823         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
824                                        outer_headers);
825         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
826                                        outer_headers);
827         u16 addr_type = 0;
828         u8 ip_proto = 0;
829
830         *min_inline = MLX5_INLINE_MODE_L2;
831
832         if (f->dissector->used_keys &
833             ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
834               BIT(FLOW_DISSECTOR_KEY_BASIC) |
835               BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
836               BIT(FLOW_DISSECTOR_KEY_VLAN) |
837               BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
838               BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
839               BIT(FLOW_DISSECTOR_KEY_PORTS) |
840               BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
841               BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
842               BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
843               BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
844               BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
845               BIT(FLOW_DISSECTOR_KEY_TCP) |
846               BIT(FLOW_DISSECTOR_KEY_IP))) {
847                 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
848                             f->dissector->used_keys);
849                 return -EOPNOTSUPP;
850         }
851
852         if ((dissector_uses_key(f->dissector,
853                                 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) ||
854              dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID) ||
855              dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) &&
856             dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
857                 struct flow_dissector_key_control *key =
858                         skb_flow_dissector_target(f->dissector,
859                                                   FLOW_DISSECTOR_KEY_ENC_CONTROL,
860                                                   f->key);
861                 switch (key->addr_type) {
862                 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
863                 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
864                         if (parse_tunnel_attr(priv, spec, f))
865                                 return -EOPNOTSUPP;
866                         break;
867                 default:
868                         return -EOPNOTSUPP;
869                 }
870
871                 /* In decap flow, header pointers should point to the inner
872                  * headers, outer header were already set by parse_tunnel_attr
873                  */
874                 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
875                                          inner_headers);
876                 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
877                                          inner_headers);
878         }
879
880         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
881                 struct flow_dissector_key_control *key =
882                         skb_flow_dissector_target(f->dissector,
883                                                   FLOW_DISSECTOR_KEY_CONTROL,
884                                                   f->key);
885
886                 struct flow_dissector_key_control *mask =
887                         skb_flow_dissector_target(f->dissector,
888                                                   FLOW_DISSECTOR_KEY_CONTROL,
889                                                   f->mask);
890                 addr_type = key->addr_type;
891
892                 if (mask->flags & FLOW_DIS_IS_FRAGMENT) {
893                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
894                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
895                                  key->flags & FLOW_DIS_IS_FRAGMENT);
896
897                         /* the HW doesn't need L3 inline to match on frag=no */
898                         if (key->flags & FLOW_DIS_IS_FRAGMENT)
899                                 *min_inline = MLX5_INLINE_MODE_IP;
900                 }
901         }
902
903         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
904                 struct flow_dissector_key_basic *key =
905                         skb_flow_dissector_target(f->dissector,
906                                                   FLOW_DISSECTOR_KEY_BASIC,
907                                                   f->key);
908                 struct flow_dissector_key_basic *mask =
909                         skb_flow_dissector_target(f->dissector,
910                                                   FLOW_DISSECTOR_KEY_BASIC,
911                                                   f->mask);
912                 ip_proto = key->ip_proto;
913
914                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
915                          ntohs(mask->n_proto));
916                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
917                          ntohs(key->n_proto));
918
919                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
920                          mask->ip_proto);
921                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
922                          key->ip_proto);
923
924                 if (mask->ip_proto)
925                         *min_inline = MLX5_INLINE_MODE_IP;
926         }
927
928         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
929                 struct flow_dissector_key_eth_addrs *key =
930                         skb_flow_dissector_target(f->dissector,
931                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
932                                                   f->key);
933                 struct flow_dissector_key_eth_addrs *mask =
934                         skb_flow_dissector_target(f->dissector,
935                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
936                                                   f->mask);
937
938                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
939                                              dmac_47_16),
940                                 mask->dst);
941                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
942                                              dmac_47_16),
943                                 key->dst);
944
945                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
946                                              smac_47_16),
947                                 mask->src);
948                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
949                                              smac_47_16),
950                                 key->src);
951         }
952
953         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
954                 struct flow_dissector_key_vlan *key =
955                         skb_flow_dissector_target(f->dissector,
956                                                   FLOW_DISSECTOR_KEY_VLAN,
957                                                   f->key);
958                 struct flow_dissector_key_vlan *mask =
959                         skb_flow_dissector_target(f->dissector,
960                                                   FLOW_DISSECTOR_KEY_VLAN,
961                                                   f->mask);
962                 if (mask->vlan_id || mask->vlan_priority) {
963                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
964                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
965
966                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
967                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
968
969                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio, mask->vlan_priority);
970                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, key->vlan_priority);
971                 }
972         }
973
974         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
975                 struct flow_dissector_key_ipv4_addrs *key =
976                         skb_flow_dissector_target(f->dissector,
977                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
978                                                   f->key);
979                 struct flow_dissector_key_ipv4_addrs *mask =
980                         skb_flow_dissector_target(f->dissector,
981                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
982                                                   f->mask);
983
984                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
985                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
986                        &mask->src, sizeof(mask->src));
987                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
988                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
989                        &key->src, sizeof(key->src));
990                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
991                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
992                        &mask->dst, sizeof(mask->dst));
993                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
994                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
995                        &key->dst, sizeof(key->dst));
996
997                 if (mask->src || mask->dst)
998                         *min_inline = MLX5_INLINE_MODE_IP;
999         }
1000
1001         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1002                 struct flow_dissector_key_ipv6_addrs *key =
1003                         skb_flow_dissector_target(f->dissector,
1004                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1005                                                   f->key);
1006                 struct flow_dissector_key_ipv6_addrs *mask =
1007                         skb_flow_dissector_target(f->dissector,
1008                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1009                                                   f->mask);
1010
1011                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1012                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
1013                        &mask->src, sizeof(mask->src));
1014                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1015                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
1016                        &key->src, sizeof(key->src));
1017
1018                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1019                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1020                        &mask->dst, sizeof(mask->dst));
1021                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1022                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1023                        &key->dst, sizeof(key->dst));
1024
1025                 if (ipv6_addr_type(&mask->src) != IPV6_ADDR_ANY ||
1026                     ipv6_addr_type(&mask->dst) != IPV6_ADDR_ANY)
1027                         *min_inline = MLX5_INLINE_MODE_IP;
1028         }
1029
1030         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_IP)) {
1031                 struct flow_dissector_key_ip *key =
1032                         skb_flow_dissector_target(f->dissector,
1033                                                   FLOW_DISSECTOR_KEY_IP,
1034                                                   f->key);
1035                 struct flow_dissector_key_ip *mask =
1036                         skb_flow_dissector_target(f->dissector,
1037                                                   FLOW_DISSECTOR_KEY_IP,
1038                                                   f->mask);
1039
1040                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn, mask->tos & 0x3);
1041                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, key->tos & 0x3);
1042
1043                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp, mask->tos >> 2);
1044                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, key->tos  >> 2);
1045
1046                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit, mask->ttl);
1047                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit, key->ttl);
1048
1049                 if (mask->ttl &&
1050                     !MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
1051                                                 ft_field_support.outer_ipv4_ttl))
1052                         return -EOPNOTSUPP;
1053
1054                 if (mask->tos || mask->ttl)
1055                         *min_inline = MLX5_INLINE_MODE_IP;
1056         }
1057
1058         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
1059                 struct flow_dissector_key_ports *key =
1060                         skb_flow_dissector_target(f->dissector,
1061                                                   FLOW_DISSECTOR_KEY_PORTS,
1062                                                   f->key);
1063                 struct flow_dissector_key_ports *mask =
1064                         skb_flow_dissector_target(f->dissector,
1065                                                   FLOW_DISSECTOR_KEY_PORTS,
1066                                                   f->mask);
1067                 switch (ip_proto) {
1068                 case IPPROTO_TCP:
1069                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1070                                  tcp_sport, ntohs(mask->src));
1071                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1072                                  tcp_sport, ntohs(key->src));
1073
1074                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1075                                  tcp_dport, ntohs(mask->dst));
1076                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1077                                  tcp_dport, ntohs(key->dst));
1078                         break;
1079
1080                 case IPPROTO_UDP:
1081                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1082                                  udp_sport, ntohs(mask->src));
1083                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1084                                  udp_sport, ntohs(key->src));
1085
1086                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1087                                  udp_dport, ntohs(mask->dst));
1088                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1089                                  udp_dport, ntohs(key->dst));
1090                         break;
1091                 default:
1092                         netdev_err(priv->netdev,
1093                                    "Only UDP and TCP transport are supported\n");
1094                         return -EINVAL;
1095                 }
1096
1097                 if (mask->src || mask->dst)
1098                         *min_inline = MLX5_INLINE_MODE_TCP_UDP;
1099         }
1100
1101         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_TCP)) {
1102                 struct flow_dissector_key_tcp *key =
1103                         skb_flow_dissector_target(f->dissector,
1104                                                   FLOW_DISSECTOR_KEY_TCP,
1105                                                   f->key);
1106                 struct flow_dissector_key_tcp *mask =
1107                         skb_flow_dissector_target(f->dissector,
1108                                                   FLOW_DISSECTOR_KEY_TCP,
1109                                                   f->mask);
1110
1111                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_flags,
1112                          ntohs(mask->flags));
1113                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
1114                          ntohs(key->flags));
1115
1116                 if (mask->flags)
1117                         *min_inline = MLX5_INLINE_MODE_TCP_UDP;
1118         }
1119
1120         return 0;
1121 }
1122
1123 static int parse_cls_flower(struct mlx5e_priv *priv,
1124                             struct mlx5e_tc_flow *flow,
1125                             struct mlx5_flow_spec *spec,
1126                             struct tc_cls_flower_offload *f)
1127 {
1128         struct mlx5_core_dev *dev = priv->mdev;
1129         struct mlx5_eswitch *esw = dev->priv.eswitch;
1130         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1131         struct mlx5_eswitch_rep *rep;
1132         u8 min_inline;
1133         int err;
1134
1135         err = __parse_cls_flower(priv, spec, f, &min_inline);
1136
1137         if (!err && (flow->flags & MLX5E_TC_FLOW_ESWITCH)) {
1138                 rep = rpriv->rep;
1139                 if (rep->vport != FDB_UPLINK_VPORT &&
1140                     (esw->offloads.inline_mode != MLX5_INLINE_MODE_NONE &&
1141                     esw->offloads.inline_mode < min_inline)) {
1142                         netdev_warn(priv->netdev,
1143                                     "Flow is not offloaded due to min inline setting, required %d actual %d\n",
1144                                     min_inline, esw->offloads.inline_mode);
1145                         return -EOPNOTSUPP;
1146                 }
1147         }
1148
1149         return err;
1150 }
1151
1152 struct pedit_headers {
1153         struct ethhdr  eth;
1154         struct iphdr   ip4;
1155         struct ipv6hdr ip6;
1156         struct tcphdr  tcp;
1157         struct udphdr  udp;
1158 };
1159
1160 static int pedit_header_offsets[] = {
1161         [TCA_PEDIT_KEY_EX_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth),
1162         [TCA_PEDIT_KEY_EX_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4),
1163         [TCA_PEDIT_KEY_EX_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6),
1164         [TCA_PEDIT_KEY_EX_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp),
1165         [TCA_PEDIT_KEY_EX_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp),
1166 };
1167
1168 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype])
1169
1170 static int set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset,
1171                          struct pedit_headers *masks,
1172                          struct pedit_headers *vals)
1173 {
1174         u32 *curr_pmask, *curr_pval;
1175
1176         if (hdr_type >= __PEDIT_HDR_TYPE_MAX)
1177                 goto out_err;
1178
1179         curr_pmask = (u32 *)(pedit_header(masks, hdr_type) + offset);
1180         curr_pval  = (u32 *)(pedit_header(vals, hdr_type) + offset);
1181
1182         if (*curr_pmask & mask)  /* disallow acting twice on the same location */
1183                 goto out_err;
1184
1185         *curr_pmask |= mask;
1186         *curr_pval  |= (val & mask);
1187
1188         return 0;
1189
1190 out_err:
1191         return -EOPNOTSUPP;
1192 }
1193
1194 struct mlx5_fields {
1195         u8  field;
1196         u8  size;
1197         u32 offset;
1198 };
1199
1200 #define OFFLOAD(fw_field, size, field, off) \
1201                 {MLX5_ACTION_IN_FIELD_OUT_ ## fw_field, size, offsetof(struct pedit_headers, field) + (off)}
1202
1203 static struct mlx5_fields fields[] = {
1204         OFFLOAD(DMAC_47_16, 4, eth.h_dest[0], 0),
1205         OFFLOAD(DMAC_47_16, 4, eth.h_dest[0], 0),
1206         OFFLOAD(DMAC_15_0,  2, eth.h_dest[4], 0),
1207         OFFLOAD(SMAC_47_16, 4, eth.h_source[0], 0),
1208         OFFLOAD(SMAC_15_0,  2, eth.h_source[4], 0),
1209         OFFLOAD(ETHERTYPE,  2, eth.h_proto, 0),
1210
1211         OFFLOAD(IP_TTL, 1, ip4.ttl,   0),
1212         OFFLOAD(SIPV4,  4, ip4.saddr, 0),
1213         OFFLOAD(DIPV4,  4, ip4.daddr, 0),
1214
1215         OFFLOAD(SIPV6_127_96, 4, ip6.saddr.s6_addr32[0], 0),
1216         OFFLOAD(SIPV6_95_64,  4, ip6.saddr.s6_addr32[1], 0),
1217         OFFLOAD(SIPV6_63_32,  4, ip6.saddr.s6_addr32[2], 0),
1218         OFFLOAD(SIPV6_31_0,   4, ip6.saddr.s6_addr32[3], 0),
1219         OFFLOAD(DIPV6_127_96, 4, ip6.daddr.s6_addr32[0], 0),
1220         OFFLOAD(DIPV6_95_64,  4, ip6.daddr.s6_addr32[1], 0),
1221         OFFLOAD(DIPV6_63_32,  4, ip6.daddr.s6_addr32[2], 0),
1222         OFFLOAD(DIPV6_31_0,   4, ip6.daddr.s6_addr32[3], 0),
1223         OFFLOAD(IPV6_HOPLIMIT, 1, ip6.hop_limit, 0),
1224
1225         OFFLOAD(TCP_SPORT, 2, tcp.source,  0),
1226         OFFLOAD(TCP_DPORT, 2, tcp.dest,    0),
1227         OFFLOAD(TCP_FLAGS, 1, tcp.ack_seq, 5),
1228
1229         OFFLOAD(UDP_SPORT, 2, udp.source, 0),
1230         OFFLOAD(UDP_DPORT, 2, udp.dest,   0),
1231 };
1232
1233 /* On input attr->num_mod_hdr_actions tells how many HW actions can be parsed at
1234  * max from the SW pedit action. On success, it says how many HW actions were
1235  * actually parsed.
1236  */
1237 static int offload_pedit_fields(struct pedit_headers *masks,
1238                                 struct pedit_headers *vals,
1239                                 struct mlx5e_tc_flow_parse_attr *parse_attr)
1240 {
1241         struct pedit_headers *set_masks, *add_masks, *set_vals, *add_vals;
1242         int i, action_size, nactions, max_actions, first, last, next_z;
1243         void *s_masks_p, *a_masks_p, *vals_p;
1244         struct mlx5_fields *f;
1245         u8 cmd, field_bsize;
1246         u32 s_mask, a_mask;
1247         unsigned long mask;
1248         __be32 mask_be32;
1249         __be16 mask_be16;
1250         void *action;
1251
1252         set_masks = &masks[TCA_PEDIT_KEY_EX_CMD_SET];
1253         add_masks = &masks[TCA_PEDIT_KEY_EX_CMD_ADD];
1254         set_vals = &vals[TCA_PEDIT_KEY_EX_CMD_SET];
1255         add_vals = &vals[TCA_PEDIT_KEY_EX_CMD_ADD];
1256
1257         action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1258         action = parse_attr->mod_hdr_actions;
1259         max_actions = parse_attr->num_mod_hdr_actions;
1260         nactions = 0;
1261
1262         for (i = 0; i < ARRAY_SIZE(fields); i++) {
1263                 f = &fields[i];
1264                 /* avoid seeing bits set from previous iterations */
1265                 s_mask = 0;
1266                 a_mask = 0;
1267
1268                 s_masks_p = (void *)set_masks + f->offset;
1269                 a_masks_p = (void *)add_masks + f->offset;
1270
1271                 memcpy(&s_mask, s_masks_p, f->size);
1272                 memcpy(&a_mask, a_masks_p, f->size);
1273
1274                 if (!s_mask && !a_mask) /* nothing to offload here */
1275                         continue;
1276
1277                 if (s_mask && a_mask) {
1278                         printk(KERN_WARNING "mlx5: can't set and add to the same HW field (%x)\n", f->field);
1279                         return -EOPNOTSUPP;
1280                 }
1281
1282                 if (nactions == max_actions) {
1283                         printk(KERN_WARNING "mlx5: parsed %d pedit actions, can't do more\n", nactions);
1284                         return -EOPNOTSUPP;
1285                 }
1286
1287                 if (s_mask) {
1288                         cmd  = MLX5_ACTION_TYPE_SET;
1289                         mask = s_mask;
1290                         vals_p = (void *)set_vals + f->offset;
1291                         /* clear to denote we consumed this field */
1292                         memset(s_masks_p, 0, f->size);
1293                 } else {
1294                         cmd  = MLX5_ACTION_TYPE_ADD;
1295                         mask = a_mask;
1296                         vals_p = (void *)add_vals + f->offset;
1297                         /* clear to denote we consumed this field */
1298                         memset(a_masks_p, 0, f->size);
1299                 }
1300
1301                 field_bsize = f->size * BITS_PER_BYTE;
1302
1303                 if (field_bsize == 32) {
1304                         mask_be32 = *(__be32 *)&mask;
1305                         mask = (__force unsigned long)cpu_to_le32(be32_to_cpu(mask_be32));
1306                 } else if (field_bsize == 16) {
1307                         mask_be16 = *(__be16 *)&mask;
1308                         mask = (__force unsigned long)cpu_to_le16(be16_to_cpu(mask_be16));
1309                 }
1310
1311                 first = find_first_bit(&mask, field_bsize);
1312                 next_z = find_next_zero_bit(&mask, field_bsize, first);
1313                 last  = find_last_bit(&mask, field_bsize);
1314                 if (first < next_z && next_z < last) {
1315                         printk(KERN_WARNING "mlx5: rewrite of few sub-fields (mask %lx) isn't offloaded\n",
1316                                mask);
1317                         return -EOPNOTSUPP;
1318                 }
1319
1320                 MLX5_SET(set_action_in, action, action_type, cmd);
1321                 MLX5_SET(set_action_in, action, field, f->field);
1322
1323                 if (cmd == MLX5_ACTION_TYPE_SET) {
1324                         MLX5_SET(set_action_in, action, offset, first);
1325                         /* length is num of bits to be written, zero means length of 32 */
1326                         MLX5_SET(set_action_in, action, length, (last - first + 1));
1327                 }
1328
1329                 if (field_bsize == 32)
1330                         MLX5_SET(set_action_in, action, data, ntohl(*(__be32 *)vals_p) >> first);
1331                 else if (field_bsize == 16)
1332                         MLX5_SET(set_action_in, action, data, ntohs(*(__be16 *)vals_p) >> first);
1333                 else if (field_bsize == 8)
1334                         MLX5_SET(set_action_in, action, data, *(u8 *)vals_p >> first);
1335
1336                 action += action_size;
1337                 nactions++;
1338         }
1339
1340         parse_attr->num_mod_hdr_actions = nactions;
1341         return 0;
1342 }
1343
1344 static int alloc_mod_hdr_actions(struct mlx5e_priv *priv,
1345                                  const struct tc_action *a, int namespace,
1346                                  struct mlx5e_tc_flow_parse_attr *parse_attr)
1347 {
1348         int nkeys, action_size, max_actions;
1349
1350         nkeys = tcf_pedit_nkeys(a);
1351         action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1352
1353         if (namespace == MLX5_FLOW_NAMESPACE_FDB) /* FDB offloading */
1354                 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, max_modify_header_actions);
1355         else /* namespace is MLX5_FLOW_NAMESPACE_KERNEL - NIC offloading */
1356                 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, max_modify_header_actions);
1357
1358         /* can get up to crazingly 16 HW actions in 32 bits pedit SW key */
1359         max_actions = min(max_actions, nkeys * 16);
1360
1361         parse_attr->mod_hdr_actions = kcalloc(max_actions, action_size, GFP_KERNEL);
1362         if (!parse_attr->mod_hdr_actions)
1363                 return -ENOMEM;
1364
1365         parse_attr->num_mod_hdr_actions = max_actions;
1366         return 0;
1367 }
1368
1369 static const struct pedit_headers zero_masks = {};
1370
1371 static int parse_tc_pedit_action(struct mlx5e_priv *priv,
1372                                  const struct tc_action *a, int namespace,
1373                                  struct mlx5e_tc_flow_parse_attr *parse_attr)
1374 {
1375         struct pedit_headers masks[__PEDIT_CMD_MAX], vals[__PEDIT_CMD_MAX], *cmd_masks;
1376         int nkeys, i, err = -EOPNOTSUPP;
1377         u32 mask, val, offset;
1378         u8 cmd, htype;
1379
1380         nkeys = tcf_pedit_nkeys(a);
1381
1382         memset(masks, 0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1383         memset(vals,  0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1384
1385         for (i = 0; i < nkeys; i++) {
1386                 htype = tcf_pedit_htype(a, i);
1387                 cmd = tcf_pedit_cmd(a, i);
1388                 err = -EOPNOTSUPP; /* can't be all optimistic */
1389
1390                 if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK) {
1391                         printk(KERN_WARNING "mlx5: legacy pedit isn't offloaded\n");
1392                         goto out_err;
1393                 }
1394
1395                 if (cmd != TCA_PEDIT_KEY_EX_CMD_SET && cmd != TCA_PEDIT_KEY_EX_CMD_ADD) {
1396                         printk(KERN_WARNING "mlx5: pedit cmd %d isn't offloaded\n", cmd);
1397                         goto out_err;
1398                 }
1399
1400                 mask = tcf_pedit_mask(a, i);
1401                 val = tcf_pedit_val(a, i);
1402                 offset = tcf_pedit_offset(a, i);
1403
1404                 err = set_pedit_val(htype, ~mask, val, offset, &masks[cmd], &vals[cmd]);
1405                 if (err)
1406                         goto out_err;
1407         }
1408
1409         err = alloc_mod_hdr_actions(priv, a, namespace, parse_attr);
1410         if (err)
1411                 goto out_err;
1412
1413         err = offload_pedit_fields(masks, vals, parse_attr);
1414         if (err < 0)
1415                 goto out_dealloc_parsed_actions;
1416
1417         for (cmd = 0; cmd < __PEDIT_CMD_MAX; cmd++) {
1418                 cmd_masks = &masks[cmd];
1419                 if (memcmp(cmd_masks, &zero_masks, sizeof(zero_masks))) {
1420                         printk(KERN_WARNING "mlx5: attempt to offload an unsupported field (cmd %d)\n",
1421                                cmd);
1422                         print_hex_dump(KERN_WARNING, "mask: ", DUMP_PREFIX_ADDRESS,
1423                                        16, 1, cmd_masks, sizeof(zero_masks), true);
1424                         err = -EOPNOTSUPP;
1425                         goto out_dealloc_parsed_actions;
1426                 }
1427         }
1428
1429         return 0;
1430
1431 out_dealloc_parsed_actions:
1432         kfree(parse_attr->mod_hdr_actions);
1433 out_err:
1434         return err;
1435 }
1436
1437 static bool csum_offload_supported(struct mlx5e_priv *priv, u32 action, u32 update_flags)
1438 {
1439         u32 prot_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR | TCA_CSUM_UPDATE_FLAG_TCP |
1440                          TCA_CSUM_UPDATE_FLAG_UDP;
1441
1442         /*  The HW recalcs checksums only if re-writing headers */
1443         if (!(action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)) {
1444                 netdev_warn(priv->netdev,
1445                             "TC csum action is only offloaded with pedit\n");
1446                 return false;
1447         }
1448
1449         if (update_flags & ~prot_flags) {
1450                 netdev_warn(priv->netdev,
1451                             "can't offload TC csum action for some header/s - flags %#x\n",
1452                             update_flags);
1453                 return false;
1454         }
1455
1456         return true;
1457 }
1458
1459 static bool modify_header_match_supported(struct mlx5_flow_spec *spec,
1460                                           struct tcf_exts *exts)
1461 {
1462         const struct tc_action *a;
1463         bool modify_ip_header;
1464         LIST_HEAD(actions);
1465         u8 htype, ip_proto;
1466         void *headers_v;
1467         u16 ethertype;
1468         int nkeys, i;
1469
1470         headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
1471         ethertype = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ethertype);
1472
1473         /* for non-IP we only re-write MACs, so we're okay */
1474         if (ethertype != ETH_P_IP && ethertype != ETH_P_IPV6)
1475                 goto out_ok;
1476
1477         modify_ip_header = false;
1478         tcf_exts_to_list(exts, &actions);
1479         list_for_each_entry(a, &actions, list) {
1480                 if (!is_tcf_pedit(a))
1481                         continue;
1482
1483                 nkeys = tcf_pedit_nkeys(a);
1484                 for (i = 0; i < nkeys; i++) {
1485                         htype = tcf_pedit_htype(a, i);
1486                         if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_IP4 ||
1487                             htype == TCA_PEDIT_KEY_EX_HDR_TYPE_IP6) {
1488                                 modify_ip_header = true;
1489                                 break;
1490                         }
1491                 }
1492         }
1493
1494         ip_proto = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ip_protocol);
1495         if (modify_ip_header && ip_proto != IPPROTO_TCP && ip_proto != IPPROTO_UDP) {
1496                 pr_info("can't offload re-write of ip proto %d\n", ip_proto);
1497                 return false;
1498         }
1499
1500 out_ok:
1501         return true;
1502 }
1503
1504 static bool actions_match_supported(struct mlx5e_priv *priv,
1505                                     struct tcf_exts *exts,
1506                                     struct mlx5e_tc_flow_parse_attr *parse_attr,
1507                                     struct mlx5e_tc_flow *flow)
1508 {
1509         u32 actions;
1510
1511         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
1512                 actions = flow->esw_attr->action;
1513         else
1514                 actions = flow->nic_attr->action;
1515
1516         if (actions & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
1517                 return modify_header_match_supported(&parse_attr->spec, exts);
1518
1519         return true;
1520 }
1521
1522 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
1523                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
1524                                 struct mlx5e_tc_flow *flow)
1525 {
1526         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
1527         const struct tc_action *a;
1528         LIST_HEAD(actions);
1529         int err;
1530
1531         if (!tcf_exts_has_actions(exts))
1532                 return -EINVAL;
1533
1534         attr->flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
1535         attr->action = 0;
1536
1537         tcf_exts_to_list(exts, &actions);
1538         list_for_each_entry(a, &actions, list) {
1539                 if (is_tcf_gact_shot(a)) {
1540                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
1541                         if (MLX5_CAP_FLOWTABLE(priv->mdev,
1542                                                flow_table_properties_nic_receive.flow_counter))
1543                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
1544                         continue;
1545                 }
1546
1547                 if (is_tcf_pedit(a)) {
1548                         err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_KERNEL,
1549                                                     parse_attr);
1550                         if (err)
1551                                 return err;
1552
1553                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1554                                         MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1555                         continue;
1556                 }
1557
1558                 if (is_tcf_csum(a)) {
1559                         if (csum_offload_supported(priv, attr->action,
1560                                                    tcf_csum_update_flags(a)))
1561                                 continue;
1562
1563                         return -EOPNOTSUPP;
1564                 }
1565
1566                 if (is_tcf_skbedit_mark(a)) {
1567                         u32 mark = tcf_skbedit_mark(a);
1568
1569                         if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
1570                                 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
1571                                             mark);
1572                                 return -EINVAL;
1573                         }
1574
1575                         attr->flow_tag = mark;
1576                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1577                         continue;
1578                 }
1579
1580                 return -EINVAL;
1581         }
1582
1583         if (!actions_match_supported(priv, exts, parse_attr, flow))
1584                 return -EOPNOTSUPP;
1585
1586         return 0;
1587 }
1588
1589 static inline int cmp_encap_info(struct ip_tunnel_key *a,
1590                                  struct ip_tunnel_key *b)
1591 {
1592         return memcmp(a, b, sizeof(*a));
1593 }
1594
1595 static inline int hash_encap_info(struct ip_tunnel_key *key)
1596 {
1597         return jhash(key, sizeof(*key), 0);
1598 }
1599
1600 static int mlx5e_route_lookup_ipv4(struct mlx5e_priv *priv,
1601                                    struct net_device *mirred_dev,
1602                                    struct net_device **out_dev,
1603                                    struct flowi4 *fl4,
1604                                    struct neighbour **out_n,
1605                                    int *out_ttl)
1606 {
1607         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1608         struct mlx5e_rep_priv *uplink_rpriv;
1609         struct rtable *rt;
1610         struct neighbour *n = NULL;
1611
1612 #if IS_ENABLED(CONFIG_INET)
1613         int ret;
1614
1615         rt = ip_route_output_key(dev_net(mirred_dev), fl4);
1616         ret = PTR_ERR_OR_ZERO(rt);
1617         if (ret)
1618                 return ret;
1619 #else
1620         return -EOPNOTSUPP;
1621 #endif
1622         uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
1623         /* if the egress device isn't on the same HW e-switch, we use the uplink */
1624         if (!switchdev_port_same_parent_id(priv->netdev, rt->dst.dev))
1625                 *out_dev = uplink_rpriv->netdev;
1626         else
1627                 *out_dev = rt->dst.dev;
1628
1629         *out_ttl = ip4_dst_hoplimit(&rt->dst);
1630         n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
1631         ip_rt_put(rt);
1632         if (!n)
1633                 return -ENOMEM;
1634
1635         *out_n = n;
1636         return 0;
1637 }
1638
1639 static int mlx5e_route_lookup_ipv6(struct mlx5e_priv *priv,
1640                                    struct net_device *mirred_dev,
1641                                    struct net_device **out_dev,
1642                                    struct flowi6 *fl6,
1643                                    struct neighbour **out_n,
1644                                    int *out_ttl)
1645 {
1646         struct neighbour *n = NULL;
1647         struct dst_entry *dst;
1648
1649 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
1650         struct mlx5e_rep_priv *uplink_rpriv;
1651         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1652         int ret;
1653
1654         ret = ipv6_stub->ipv6_dst_lookup(dev_net(mirred_dev), NULL, &dst,
1655                                          fl6);
1656         if (ret < 0)
1657                 return ret;
1658
1659         *out_ttl = ip6_dst_hoplimit(dst);
1660
1661         uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
1662         /* if the egress device isn't on the same HW e-switch, we use the uplink */
1663         if (!switchdev_port_same_parent_id(priv->netdev, dst->dev))
1664                 *out_dev = uplink_rpriv->netdev;
1665         else
1666                 *out_dev = dst->dev;
1667 #else
1668         return -EOPNOTSUPP;
1669 #endif
1670
1671         n = dst_neigh_lookup(dst, &fl6->daddr);
1672         dst_release(dst);
1673         if (!n)
1674                 return -ENOMEM;
1675
1676         *out_n = n;
1677         return 0;
1678 }
1679
1680 static void gen_vxlan_header_ipv4(struct net_device *out_dev,
1681                                   char buf[], int encap_size,
1682                                   unsigned char h_dest[ETH_ALEN],
1683                                   int ttl,
1684                                   __be32 daddr,
1685                                   __be32 saddr,
1686                                   __be16 udp_dst_port,
1687                                   __be32 vx_vni)
1688 {
1689         struct ethhdr *eth = (struct ethhdr *)buf;
1690         struct iphdr  *ip = (struct iphdr *)((char *)eth + sizeof(struct ethhdr));
1691         struct udphdr *udp = (struct udphdr *)((char *)ip + sizeof(struct iphdr));
1692         struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
1693
1694         memset(buf, 0, encap_size);
1695
1696         ether_addr_copy(eth->h_dest, h_dest);
1697         ether_addr_copy(eth->h_source, out_dev->dev_addr);
1698         eth->h_proto = htons(ETH_P_IP);
1699
1700         ip->daddr = daddr;
1701         ip->saddr = saddr;
1702
1703         ip->ttl = ttl;
1704         ip->protocol = IPPROTO_UDP;
1705         ip->version = 0x4;
1706         ip->ihl = 0x5;
1707
1708         udp->dest = udp_dst_port;
1709         vxh->vx_flags = VXLAN_HF_VNI;
1710         vxh->vx_vni = vxlan_vni_field(vx_vni);
1711 }
1712
1713 static void gen_vxlan_header_ipv6(struct net_device *out_dev,
1714                                   char buf[], int encap_size,
1715                                   unsigned char h_dest[ETH_ALEN],
1716                                   int ttl,
1717                                   struct in6_addr *daddr,
1718                                   struct in6_addr *saddr,
1719                                   __be16 udp_dst_port,
1720                                   __be32 vx_vni)
1721 {
1722         struct ethhdr *eth = (struct ethhdr *)buf;
1723         struct ipv6hdr *ip6h = (struct ipv6hdr *)((char *)eth + sizeof(struct ethhdr));
1724         struct udphdr *udp = (struct udphdr *)((char *)ip6h + sizeof(struct ipv6hdr));
1725         struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
1726
1727         memset(buf, 0, encap_size);
1728
1729         ether_addr_copy(eth->h_dest, h_dest);
1730         ether_addr_copy(eth->h_source, out_dev->dev_addr);
1731         eth->h_proto = htons(ETH_P_IPV6);
1732
1733         ip6_flow_hdr(ip6h, 0, 0);
1734         /* the HW fills up ipv6 payload len */
1735         ip6h->nexthdr     = IPPROTO_UDP;
1736         ip6h->hop_limit   = ttl;
1737         ip6h->daddr       = *daddr;
1738         ip6h->saddr       = *saddr;
1739
1740         udp->dest = udp_dst_port;
1741         vxh->vx_flags = VXLAN_HF_VNI;
1742         vxh->vx_vni = vxlan_vni_field(vx_vni);
1743 }
1744
1745 static int mlx5e_create_encap_header_ipv4(struct mlx5e_priv *priv,
1746                                           struct net_device *mirred_dev,
1747                                           struct mlx5e_encap_entry *e)
1748 {
1749         int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
1750         int ipv4_encap_size = ETH_HLEN + sizeof(struct iphdr) + VXLAN_HLEN;
1751         struct ip_tunnel_key *tun_key = &e->tun_info.key;
1752         struct net_device *out_dev;
1753         struct neighbour *n = NULL;
1754         struct flowi4 fl4 = {};
1755         char *encap_header;
1756         int ttl, err;
1757         u8 nud_state;
1758
1759         if (max_encap_size < ipv4_encap_size) {
1760                 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
1761                                ipv4_encap_size, max_encap_size);
1762                 return -EOPNOTSUPP;
1763         }
1764
1765         encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
1766         if (!encap_header)
1767                 return -ENOMEM;
1768
1769         switch (e->tunnel_type) {
1770         case MLX5_HEADER_TYPE_VXLAN:
1771                 fl4.flowi4_proto = IPPROTO_UDP;
1772                 fl4.fl4_dport = tun_key->tp_dst;
1773                 break;
1774         default:
1775                 err = -EOPNOTSUPP;
1776                 goto free_encap;
1777         }
1778         fl4.flowi4_tos = tun_key->tos;
1779         fl4.daddr = tun_key->u.ipv4.dst;
1780         fl4.saddr = tun_key->u.ipv4.src;
1781
1782         err = mlx5e_route_lookup_ipv4(priv, mirred_dev, &out_dev,
1783                                       &fl4, &n, &ttl);
1784         if (err)
1785                 goto free_encap;
1786
1787         /* used by mlx5e_detach_encap to lookup a neigh hash table
1788          * entry in the neigh hash table when a user deletes a rule
1789          */
1790         e->m_neigh.dev = n->dev;
1791         e->m_neigh.family = n->ops->family;
1792         memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
1793         e->out_dev = out_dev;
1794
1795         /* It's importent to add the neigh to the hash table before checking
1796          * the neigh validity state. So if we'll get a notification, in case the
1797          * neigh changes it's validity state, we would find the relevant neigh
1798          * in the hash.
1799          */
1800         err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
1801         if (err)
1802                 goto free_encap;
1803
1804         read_lock_bh(&n->lock);
1805         nud_state = n->nud_state;
1806         ether_addr_copy(e->h_dest, n->ha);
1807         read_unlock_bh(&n->lock);
1808
1809         switch (e->tunnel_type) {
1810         case MLX5_HEADER_TYPE_VXLAN:
1811                 gen_vxlan_header_ipv4(out_dev, encap_header,
1812                                       ipv4_encap_size, e->h_dest, ttl,
1813                                       fl4.daddr,
1814                                       fl4.saddr, tun_key->tp_dst,
1815                                       tunnel_id_to_key32(tun_key->tun_id));
1816                 break;
1817         default:
1818                 err = -EOPNOTSUPP;
1819                 goto destroy_neigh_entry;
1820         }
1821         e->encap_size = ipv4_encap_size;
1822         e->encap_header = encap_header;
1823
1824         if (!(nud_state & NUD_VALID)) {
1825                 neigh_event_send(n, NULL);
1826                 err = -EAGAIN;
1827                 goto out;
1828         }
1829
1830         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
1831                                ipv4_encap_size, encap_header, &e->encap_id);
1832         if (err)
1833                 goto destroy_neigh_entry;
1834
1835         e->flags |= MLX5_ENCAP_ENTRY_VALID;
1836         mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
1837         neigh_release(n);
1838         return err;
1839
1840 destroy_neigh_entry:
1841         mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1842 free_encap:
1843         kfree(encap_header);
1844 out:
1845         if (n)
1846                 neigh_release(n);
1847         return err;
1848 }
1849
1850 static int mlx5e_create_encap_header_ipv6(struct mlx5e_priv *priv,
1851                                           struct net_device *mirred_dev,
1852                                           struct mlx5e_encap_entry *e)
1853 {
1854         int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
1855         int ipv6_encap_size = ETH_HLEN + sizeof(struct ipv6hdr) + VXLAN_HLEN;
1856         struct ip_tunnel_key *tun_key = &e->tun_info.key;
1857         struct net_device *out_dev;
1858         struct neighbour *n = NULL;
1859         struct flowi6 fl6 = {};
1860         char *encap_header;
1861         int err, ttl = 0;
1862         u8 nud_state;
1863
1864         if (max_encap_size < ipv6_encap_size) {
1865                 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
1866                                ipv6_encap_size, max_encap_size);
1867                 return -EOPNOTSUPP;
1868         }
1869
1870         encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
1871         if (!encap_header)
1872                 return -ENOMEM;
1873
1874         switch (e->tunnel_type) {
1875         case MLX5_HEADER_TYPE_VXLAN:
1876                 fl6.flowi6_proto = IPPROTO_UDP;
1877                 fl6.fl6_dport = tun_key->tp_dst;
1878                 break;
1879         default:
1880                 err = -EOPNOTSUPP;
1881                 goto free_encap;
1882         }
1883
1884         fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
1885         fl6.daddr = tun_key->u.ipv6.dst;
1886         fl6.saddr = tun_key->u.ipv6.src;
1887
1888         err = mlx5e_route_lookup_ipv6(priv, mirred_dev, &out_dev,
1889                                       &fl6, &n, &ttl);
1890         if (err)
1891                 goto free_encap;
1892
1893         /* used by mlx5e_detach_encap to lookup a neigh hash table
1894          * entry in the neigh hash table when a user deletes a rule
1895          */
1896         e->m_neigh.dev = n->dev;
1897         e->m_neigh.family = n->ops->family;
1898         memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
1899         e->out_dev = out_dev;
1900
1901         /* It's importent to add the neigh to the hash table before checking
1902          * the neigh validity state. So if we'll get a notification, in case the
1903          * neigh changes it's validity state, we would find the relevant neigh
1904          * in the hash.
1905          */
1906         err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
1907         if (err)
1908                 goto free_encap;
1909
1910         read_lock_bh(&n->lock);
1911         nud_state = n->nud_state;
1912         ether_addr_copy(e->h_dest, n->ha);
1913         read_unlock_bh(&n->lock);
1914
1915         switch (e->tunnel_type) {
1916         case MLX5_HEADER_TYPE_VXLAN:
1917                 gen_vxlan_header_ipv6(out_dev, encap_header,
1918                                       ipv6_encap_size, e->h_dest, ttl,
1919                                       &fl6.daddr,
1920                                       &fl6.saddr, tun_key->tp_dst,
1921                                       tunnel_id_to_key32(tun_key->tun_id));
1922                 break;
1923         default:
1924                 err = -EOPNOTSUPP;
1925                 goto destroy_neigh_entry;
1926         }
1927
1928         e->encap_size = ipv6_encap_size;
1929         e->encap_header = encap_header;
1930
1931         if (!(nud_state & NUD_VALID)) {
1932                 neigh_event_send(n, NULL);
1933                 err = -EAGAIN;
1934                 goto out;
1935         }
1936
1937         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
1938                                ipv6_encap_size, encap_header, &e->encap_id);
1939         if (err)
1940                 goto destroy_neigh_entry;
1941
1942         e->flags |= MLX5_ENCAP_ENTRY_VALID;
1943         mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
1944         neigh_release(n);
1945         return err;
1946
1947 destroy_neigh_entry:
1948         mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1949 free_encap:
1950         kfree(encap_header);
1951 out:
1952         if (n)
1953                 neigh_release(n);
1954         return err;
1955 }
1956
1957 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
1958                               struct ip_tunnel_info *tun_info,
1959                               struct net_device *mirred_dev,
1960                               struct net_device **encap_dev,
1961                               struct mlx5e_tc_flow *flow)
1962 {
1963         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1964         struct mlx5e_rep_priv *uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw,
1965                                                                            REP_ETH);
1966         struct net_device *up_dev = uplink_rpriv->netdev;
1967         unsigned short family = ip_tunnel_info_af(tun_info);
1968         struct mlx5e_priv *up_priv = netdev_priv(up_dev);
1969         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
1970         struct ip_tunnel_key *key = &tun_info->key;
1971         struct mlx5e_encap_entry *e;
1972         int tunnel_type, err = 0;
1973         uintptr_t hash_key;
1974         bool found = false;
1975
1976         /* udp dst port must be set */
1977         if (!memchr_inv(&key->tp_dst, 0, sizeof(key->tp_dst)))
1978                 goto vxlan_encap_offload_err;
1979
1980         /* setting udp src port isn't supported */
1981         if (memchr_inv(&key->tp_src, 0, sizeof(key->tp_src))) {
1982 vxlan_encap_offload_err:
1983                 netdev_warn(priv->netdev,
1984                             "must set udp dst port and not set udp src port\n");
1985                 return -EOPNOTSUPP;
1986         }
1987
1988         if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->tp_dst)) &&
1989             MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap)) {
1990                 tunnel_type = MLX5_HEADER_TYPE_VXLAN;
1991         } else {
1992                 netdev_warn(priv->netdev,
1993                             "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->tp_dst));
1994                 return -EOPNOTSUPP;
1995         }
1996
1997         hash_key = hash_encap_info(key);
1998
1999         hash_for_each_possible_rcu(esw->offloads.encap_tbl, e,
2000                                    encap_hlist, hash_key) {
2001                 if (!cmp_encap_info(&e->tun_info.key, key)) {
2002                         found = true;
2003                         break;
2004                 }
2005         }
2006
2007         /* must verify if encap is valid or not */
2008         if (found)
2009                 goto attach_flow;
2010
2011         e = kzalloc(sizeof(*e), GFP_KERNEL);
2012         if (!e)
2013                 return -ENOMEM;
2014
2015         e->tun_info = *tun_info;
2016         e->tunnel_type = tunnel_type;
2017         INIT_LIST_HEAD(&e->flows);
2018
2019         if (family == AF_INET)
2020                 err = mlx5e_create_encap_header_ipv4(priv, mirred_dev, e);
2021         else if (family == AF_INET6)
2022                 err = mlx5e_create_encap_header_ipv6(priv, mirred_dev, e);
2023
2024         if (err && err != -EAGAIN)
2025                 goto out_err;
2026
2027         hash_add_rcu(esw->offloads.encap_tbl, &e->encap_hlist, hash_key);
2028
2029 attach_flow:
2030         list_add(&flow->encap, &e->flows);
2031         *encap_dev = e->out_dev;
2032         if (e->flags & MLX5_ENCAP_ENTRY_VALID)
2033                 attr->encap_id = e->encap_id;
2034         else
2035                 err = -EAGAIN;
2036
2037         return err;
2038
2039 out_err:
2040         kfree(e);
2041         return err;
2042 }
2043
2044 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
2045                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
2046                                 struct mlx5e_tc_flow *flow)
2047 {
2048         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
2049         struct mlx5e_rep_priv *rpriv = priv->ppriv;
2050         struct ip_tunnel_info *info = NULL;
2051         const struct tc_action *a;
2052         LIST_HEAD(actions);
2053         bool encap = false;
2054         int err = 0;
2055
2056         if (!tcf_exts_has_actions(exts))
2057                 return -EINVAL;
2058
2059         memset(attr, 0, sizeof(*attr));
2060         attr->in_rep = rpriv->rep;
2061
2062         tcf_exts_to_list(exts, &actions);
2063         list_for_each_entry(a, &actions, list) {
2064                 if (is_tcf_gact_shot(a)) {
2065                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
2066                                         MLX5_FLOW_CONTEXT_ACTION_COUNT;
2067                         continue;
2068                 }
2069
2070                 if (is_tcf_pedit(a)) {
2071                         err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_FDB,
2072                                                     parse_attr);
2073                         if (err)
2074                                 return err;
2075
2076                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2077                         continue;
2078                 }
2079
2080                 if (is_tcf_csum(a)) {
2081                         if (csum_offload_supported(priv, attr->action,
2082                                                    tcf_csum_update_flags(a)))
2083                                 continue;
2084
2085                         return -EOPNOTSUPP;
2086                 }
2087
2088                 if (is_tcf_mirred_egress_redirect(a)) {
2089                         struct net_device *out_dev;
2090                         struct mlx5e_priv *out_priv;
2091
2092                         out_dev = tcf_mirred_dev(a);
2093
2094                         if (switchdev_port_same_parent_id(priv->netdev,
2095                                                           out_dev)) {
2096                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
2097                                         MLX5_FLOW_CONTEXT_ACTION_COUNT;
2098                                 out_priv = netdev_priv(out_dev);
2099                                 rpriv = out_priv->ppriv;
2100                                 attr->out_rep = rpriv->rep;
2101                         } else if (encap) {
2102                                 parse_attr->mirred_ifindex = out_dev->ifindex;
2103                                 parse_attr->tun_info = *info;
2104                                 attr->parse_attr = parse_attr;
2105                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_ENCAP |
2106                                         MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
2107                                         MLX5_FLOW_CONTEXT_ACTION_COUNT;
2108                                 /* attr->out_rep is resolved when we handle encap */
2109                         } else {
2110                                 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
2111                                        priv->netdev->name, out_dev->name);
2112                                 return -EINVAL;
2113                         }
2114                         continue;
2115                 }
2116
2117                 if (is_tcf_tunnel_set(a)) {
2118                         info = tcf_tunnel_info(a);
2119                         if (info)
2120                                 encap = true;
2121                         else
2122                                 return -EOPNOTSUPP;
2123                         continue;
2124                 }
2125
2126                 if (is_tcf_vlan(a)) {
2127                         if (tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
2128                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
2129                         } else if (tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
2130                                 if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q))
2131                                         return -EOPNOTSUPP;
2132
2133                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
2134                                 attr->vlan = tcf_vlan_push_vid(a);
2135                         } else { /* action is TCA_VLAN_ACT_MODIFY */
2136                                 return -EOPNOTSUPP;
2137                         }
2138                         continue;
2139                 }
2140
2141                 if (is_tcf_tunnel_release(a)) {
2142                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2143                         continue;
2144                 }
2145
2146                 return -EINVAL;
2147         }
2148
2149         if (!actions_match_supported(priv, exts, parse_attr, flow))
2150                 return -EOPNOTSUPP;
2151
2152         return err;
2153 }
2154
2155 int mlx5e_configure_flower(struct mlx5e_priv *priv,
2156                            struct tc_cls_flower_offload *f)
2157 {
2158         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2159         struct mlx5e_tc_flow_parse_attr *parse_attr;
2160         struct mlx5e_tc_table *tc = &priv->fs.tc;
2161         struct mlx5e_tc_flow *flow;
2162         int attr_size, err = 0;
2163         u8 flow_flags = 0;
2164
2165         if (esw && esw->mode == SRIOV_OFFLOADS) {
2166                 flow_flags = MLX5E_TC_FLOW_ESWITCH;
2167                 attr_size  = sizeof(struct mlx5_esw_flow_attr);
2168         } else {
2169                 flow_flags = MLX5E_TC_FLOW_NIC;
2170                 attr_size  = sizeof(struct mlx5_nic_flow_attr);
2171         }
2172
2173         flow = kzalloc(sizeof(*flow) + attr_size, GFP_KERNEL);
2174         parse_attr = kvzalloc(sizeof(*parse_attr), GFP_KERNEL);
2175         if (!parse_attr || !flow) {
2176                 err = -ENOMEM;
2177                 goto err_free;
2178         }
2179
2180         flow->cookie = f->cookie;
2181         flow->flags = flow_flags;
2182
2183         err = parse_cls_flower(priv, flow, &parse_attr->spec, f);
2184         if (err < 0)
2185                 goto err_free;
2186
2187         if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
2188                 err = parse_tc_fdb_actions(priv, f->exts, parse_attr, flow);
2189                 if (err < 0)
2190                         goto err_free;
2191                 flow->rule = mlx5e_tc_add_fdb_flow(priv, parse_attr, flow);
2192         } else {
2193                 err = parse_tc_nic_actions(priv, f->exts, parse_attr, flow);
2194                 if (err < 0)
2195                         goto err_free;
2196                 flow->rule = mlx5e_tc_add_nic_flow(priv, parse_attr, flow);
2197         }
2198
2199         if (IS_ERR(flow->rule)) {
2200                 err = PTR_ERR(flow->rule);
2201                 if (err != -EAGAIN)
2202                         goto err_free;
2203         }
2204
2205         if (err != -EAGAIN)
2206                 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
2207
2208         err = rhashtable_insert_fast(&tc->ht, &flow->node,
2209                                      tc->ht_params);
2210         if (err)
2211                 goto err_del_rule;
2212
2213         if (flow->flags & MLX5E_TC_FLOW_ESWITCH &&
2214             !(flow->esw_attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP))
2215                 kvfree(parse_attr);
2216         return err;
2217
2218 err_del_rule:
2219         mlx5e_tc_del_flow(priv, flow);
2220
2221 err_free:
2222         kvfree(parse_attr);
2223         kfree(flow);
2224         return err;
2225 }
2226
2227 int mlx5e_delete_flower(struct mlx5e_priv *priv,
2228                         struct tc_cls_flower_offload *f)
2229 {
2230         struct mlx5e_tc_flow *flow;
2231         struct mlx5e_tc_table *tc = &priv->fs.tc;
2232
2233         flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2234                                       tc->ht_params);
2235         if (!flow)
2236                 return -EINVAL;
2237
2238         rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
2239
2240         mlx5e_tc_del_flow(priv, flow);
2241
2242         kfree(flow);
2243
2244         return 0;
2245 }
2246
2247 int mlx5e_stats_flower(struct mlx5e_priv *priv,
2248                        struct tc_cls_flower_offload *f)
2249 {
2250         struct mlx5e_tc_table *tc = &priv->fs.tc;
2251         struct mlx5e_tc_flow *flow;
2252         struct mlx5_fc *counter;
2253         u64 bytes;
2254         u64 packets;
2255         u64 lastuse;
2256
2257         flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2258                                       tc->ht_params);
2259         if (!flow)
2260                 return -EINVAL;
2261
2262         if (!(flow->flags & MLX5E_TC_FLOW_OFFLOADED))
2263                 return 0;
2264
2265         counter = mlx5_flow_rule_counter(flow->rule);
2266         if (!counter)
2267                 return 0;
2268
2269         mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
2270
2271         tcf_exts_stats_update(f->exts, bytes, packets, lastuse);
2272
2273         return 0;
2274 }
2275
2276 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
2277         .head_offset = offsetof(struct mlx5e_tc_flow, node),
2278         .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
2279         .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
2280         .automatic_shrinking = true,
2281 };
2282
2283 int mlx5e_tc_init(struct mlx5e_priv *priv)
2284 {
2285         struct mlx5e_tc_table *tc = &priv->fs.tc;
2286
2287         hash_init(tc->mod_hdr_tbl);
2288
2289         tc->ht_params = mlx5e_tc_flow_ht_params;
2290         return rhashtable_init(&tc->ht, &tc->ht_params);
2291 }
2292
2293 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
2294 {
2295         struct mlx5e_tc_flow *flow = ptr;
2296         struct mlx5e_priv *priv = arg;
2297
2298         mlx5e_tc_del_flow(priv, flow);
2299         kfree(flow);
2300 }
2301
2302 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
2303 {
2304         struct mlx5e_tc_table *tc = &priv->fs.tc;
2305
2306         rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
2307
2308         if (!IS_ERR_OR_NULL(tc->t)) {
2309                 mlx5_destroy_flow_table(tc->t);
2310                 tc->t = NULL;
2311         }
2312 }