net/mlx5e: Verify dev is present in some ndos
[platform/kernel/linux-starfive.git] / drivers / net / ethernet / mellanox / mlx5 / core / en / rep / tc.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020 Mellanox Technologies. */
3
4 #include <net/dst_metadata.h>
5 #include <linux/netdevice.h>
6 #include <linux/list.h>
7 #include <linux/rculist.h>
8 #include <linux/rtnetlink.h>
9 #include <linux/workqueue.h>
10 #include <linux/spinlock.h>
11 #include "tc.h"
12 #include "neigh.h"
13 #include "en_rep.h"
14 #include "eswitch.h"
15 #include "lib/fs_chains.h"
16 #include "en/tc_ct.h"
17 #include "en/mapping.h"
18 #include "en/tc_tun.h"
19 #include "lib/port_tun.h"
20
21 struct mlx5e_rep_indr_block_priv {
22         struct net_device *netdev;
23         struct mlx5e_rep_priv *rpriv;
24
25         struct list_head list;
26 };
27
28 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
29                                  struct mlx5e_encap_entry *e,
30                                  struct mlx5e_neigh *m_neigh,
31                                  struct net_device *neigh_dev)
32 {
33         struct mlx5e_rep_priv *rpriv = priv->ppriv;
34         struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
35         struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
36         struct mlx5e_neigh_hash_entry *nhe;
37         int err;
38
39         err = mlx5_tun_entropy_refcount_inc(tun_entropy, e->reformat_type);
40         if (err)
41                 return err;
42
43         mutex_lock(&rpriv->neigh_update.encap_lock);
44         nhe = mlx5e_rep_neigh_entry_lookup(priv, m_neigh);
45         if (!nhe) {
46                 err = mlx5e_rep_neigh_entry_create(priv, m_neigh, neigh_dev, &nhe);
47                 if (err) {
48                         mutex_unlock(&rpriv->neigh_update.encap_lock);
49                         mlx5_tun_entropy_refcount_dec(tun_entropy,
50                                                       e->reformat_type);
51                         return err;
52                 }
53         }
54
55         e->nhe = nhe;
56         spin_lock(&nhe->encap_list_lock);
57         list_add_rcu(&e->encap_list, &nhe->encap_list);
58         spin_unlock(&nhe->encap_list_lock);
59
60         mutex_unlock(&rpriv->neigh_update.encap_lock);
61
62         return 0;
63 }
64
65 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
66                                   struct mlx5e_encap_entry *e)
67 {
68         struct mlx5e_rep_priv *rpriv = priv->ppriv;
69         struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
70         struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
71
72         if (!e->nhe)
73                 return;
74
75         spin_lock(&e->nhe->encap_list_lock);
76         list_del_rcu(&e->encap_list);
77         spin_unlock(&e->nhe->encap_list_lock);
78
79         mlx5e_rep_neigh_entry_release(e->nhe);
80         e->nhe = NULL;
81         mlx5_tun_entropy_refcount_dec(tun_entropy, e->reformat_type);
82 }
83
84 void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
85                             struct mlx5e_encap_entry *e,
86                             bool neigh_connected,
87                             unsigned char ha[ETH_ALEN])
88 {
89         struct ethhdr *eth = (struct ethhdr *)e->encap_header;
90         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
91         bool encap_connected;
92         LIST_HEAD(flow_list);
93
94         ASSERT_RTNL();
95
96         /* wait for encap to be fully initialized */
97         wait_for_completion(&e->res_ready);
98
99         mutex_lock(&esw->offloads.encap_tbl_lock);
100         encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
101         if (e->compl_result < 0 || (encap_connected == neigh_connected &&
102                                     ether_addr_equal(e->h_dest, ha)))
103                 goto unlock;
104
105         mlx5e_take_all_encap_flows(e, &flow_list);
106
107         if ((e->flags & MLX5_ENCAP_ENTRY_VALID) &&
108             (!neigh_connected || !ether_addr_equal(e->h_dest, ha)))
109                 mlx5e_tc_encap_flows_del(priv, e, &flow_list);
110
111         if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
112                 struct net_device *route_dev;
113
114                 ether_addr_copy(e->h_dest, ha);
115                 ether_addr_copy(eth->h_dest, ha);
116                 /* Update the encap source mac, in case that we delete
117                  * the flows when encap source mac changed.
118                  */
119                 route_dev = __dev_get_by_index(dev_net(priv->netdev), e->route_dev_ifindex);
120                 if (route_dev)
121                         ether_addr_copy(eth->h_source, route_dev->dev_addr);
122
123                 mlx5e_tc_encap_flows_add(priv, e, &flow_list);
124         }
125 unlock:
126         mutex_unlock(&esw->offloads.encap_tbl_lock);
127         mlx5e_put_flow_list(priv, &flow_list);
128 }
129
130 static int
131 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
132                               struct flow_cls_offload *cls_flower, int flags)
133 {
134         switch (cls_flower->command) {
135         case FLOW_CLS_REPLACE:
136                 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
137                                               flags);
138         case FLOW_CLS_DESTROY:
139                 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
140                                            flags);
141         case FLOW_CLS_STATS:
142                 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
143                                           flags);
144         default:
145                 return -EOPNOTSUPP;
146         }
147 }
148
149 static
150 int mlx5e_rep_setup_tc_cls_matchall(struct mlx5e_priv *priv,
151                                     struct tc_cls_matchall_offload *ma)
152 {
153         switch (ma->command) {
154         case TC_CLSMATCHALL_REPLACE:
155                 return mlx5e_tc_configure_matchall(priv, ma);
156         case TC_CLSMATCHALL_DESTROY:
157                 return mlx5e_tc_delete_matchall(priv, ma);
158         case TC_CLSMATCHALL_STATS:
159                 mlx5e_tc_stats_matchall(priv, ma);
160                 return 0;
161         default:
162                 return -EOPNOTSUPP;
163         }
164 }
165
166 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
167                                  void *cb_priv)
168 {
169         unsigned long flags = MLX5_TC_FLAG(INGRESS) | MLX5_TC_FLAG(ESW_OFFLOAD);
170         struct mlx5e_priv *priv = cb_priv;
171
172         if (!priv->netdev || !netif_device_present(priv->netdev))
173                 return -EOPNOTSUPP;
174
175         switch (type) {
176         case TC_SETUP_CLSFLOWER:
177                 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, flags);
178         case TC_SETUP_CLSMATCHALL:
179                 return mlx5e_rep_setup_tc_cls_matchall(priv, type_data);
180         default:
181                 return -EOPNOTSUPP;
182         }
183 }
184
185 static int mlx5e_rep_setup_ft_cb(enum tc_setup_type type, void *type_data,
186                                  void *cb_priv)
187 {
188         struct flow_cls_offload tmp, *f = type_data;
189         struct mlx5e_priv *priv = cb_priv;
190         struct mlx5_eswitch *esw;
191         unsigned long flags;
192         int err;
193
194         flags = MLX5_TC_FLAG(INGRESS) |
195                 MLX5_TC_FLAG(ESW_OFFLOAD) |
196                 MLX5_TC_FLAG(FT_OFFLOAD);
197         esw = priv->mdev->priv.eswitch;
198
199         switch (type) {
200         case TC_SETUP_CLSFLOWER:
201                 memcpy(&tmp, f, sizeof(*f));
202
203                 if (!mlx5_chains_prios_supported(esw_chains(esw)))
204                         return -EOPNOTSUPP;
205
206                 /* Re-use tc offload path by moving the ft flow to the
207                  * reserved ft chain.
208                  *
209                  * FT offload can use prio range [0, INT_MAX], so we normalize
210                  * it to range [1, mlx5_esw_chains_get_prio_range(esw)]
211                  * as with tc, where prio 0 isn't supported.
212                  *
213                  * We only support chain 0 of FT offload.
214                  */
215                 if (tmp.common.prio >= mlx5_chains_get_prio_range(esw_chains(esw)))
216                         return -EOPNOTSUPP;
217                 if (tmp.common.chain_index != 0)
218                         return -EOPNOTSUPP;
219
220                 tmp.common.chain_index = mlx5_chains_get_nf_ft_chain(esw_chains(esw));
221                 tmp.common.prio++;
222                 err = mlx5e_rep_setup_tc_cls_flower(priv, &tmp, flags);
223                 memcpy(&f->stats, &tmp.stats, sizeof(f->stats));
224                 return err;
225         default:
226                 return -EOPNOTSUPP;
227         }
228 }
229
230 static LIST_HEAD(mlx5e_rep_block_tc_cb_list);
231 static LIST_HEAD(mlx5e_rep_block_ft_cb_list);
232 int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
233                        void *type_data)
234 {
235         struct mlx5e_priv *priv = netdev_priv(dev);
236         struct flow_block_offload *f = type_data;
237
238         f->unlocked_driver_cb = true;
239
240         switch (type) {
241         case TC_SETUP_BLOCK:
242                 return flow_block_cb_setup_simple(type_data,
243                                                   &mlx5e_rep_block_tc_cb_list,
244                                                   mlx5e_rep_setup_tc_cb,
245                                                   priv, priv, true);
246         case TC_SETUP_FT:
247                 return flow_block_cb_setup_simple(type_data,
248                                                   &mlx5e_rep_block_ft_cb_list,
249                                                   mlx5e_rep_setup_ft_cb,
250                                                   priv, priv, true);
251         default:
252                 return -EOPNOTSUPP;
253         }
254 }
255
256 int mlx5e_rep_tc_init(struct mlx5e_rep_priv *rpriv)
257 {
258         struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
259         int err;
260
261         mutex_init(&uplink_priv->unready_flows_lock);
262         INIT_LIST_HEAD(&uplink_priv->unready_flows);
263
264         /* init shared tc flow table */
265         err = mlx5e_tc_esw_init(&uplink_priv->tc_ht);
266         return err;
267 }
268
269 void mlx5e_rep_tc_cleanup(struct mlx5e_rep_priv *rpriv)
270 {
271         /* delete shared tc flow table */
272         mlx5e_tc_esw_cleanup(&rpriv->uplink_priv.tc_ht);
273         mutex_destroy(&rpriv->uplink_priv.unready_flows_lock);
274 }
275
276 void mlx5e_rep_tc_enable(struct mlx5e_priv *priv)
277 {
278         struct mlx5e_rep_priv *rpriv = priv->ppriv;
279
280         INIT_WORK(&rpriv->uplink_priv.reoffload_flows_work,
281                   mlx5e_tc_reoffload_flows_work);
282 }
283
284 void mlx5e_rep_tc_disable(struct mlx5e_priv *priv)
285 {
286         struct mlx5e_rep_priv *rpriv = priv->ppriv;
287
288         cancel_work_sync(&rpriv->uplink_priv.reoffload_flows_work);
289 }
290
291 int mlx5e_rep_tc_event_port_affinity(struct mlx5e_priv *priv)
292 {
293         struct mlx5e_rep_priv *rpriv = priv->ppriv;
294
295         queue_work(priv->wq, &rpriv->uplink_priv.reoffload_flows_work);
296
297         return NOTIFY_OK;
298 }
299
300 static struct mlx5e_rep_indr_block_priv *
301 mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv *rpriv,
302                                  struct net_device *netdev)
303 {
304         struct mlx5e_rep_indr_block_priv *cb_priv;
305
306         /* All callback list access should be protected by RTNL. */
307         ASSERT_RTNL();
308
309         list_for_each_entry(cb_priv,
310                             &rpriv->uplink_priv.tc_indr_block_priv_list,
311                             list)
312                 if (cb_priv->netdev == netdev)
313                         return cb_priv;
314
315         return NULL;
316 }
317
318 static int
319 mlx5e_rep_indr_offload(struct net_device *netdev,
320                        struct flow_cls_offload *flower,
321                        struct mlx5e_rep_indr_block_priv *indr_priv,
322                        unsigned long flags)
323 {
324         struct mlx5e_priv *priv = netdev_priv(indr_priv->rpriv->netdev);
325         int err = 0;
326
327         if (!netif_device_present(indr_priv->rpriv->netdev))
328                 return -EOPNOTSUPP;
329
330         switch (flower->command) {
331         case FLOW_CLS_REPLACE:
332                 err = mlx5e_configure_flower(netdev, priv, flower, flags);
333                 break;
334         case FLOW_CLS_DESTROY:
335                 err = mlx5e_delete_flower(netdev, priv, flower, flags);
336                 break;
337         case FLOW_CLS_STATS:
338                 err = mlx5e_stats_flower(netdev, priv, flower, flags);
339                 break;
340         default:
341                 err = -EOPNOTSUPP;
342         }
343
344         return err;
345 }
346
347 static int mlx5e_rep_indr_setup_tc_cb(enum tc_setup_type type,
348                                       void *type_data, void *indr_priv)
349 {
350         unsigned long flags = MLX5_TC_FLAG(EGRESS) | MLX5_TC_FLAG(ESW_OFFLOAD);
351         struct mlx5e_rep_indr_block_priv *priv = indr_priv;
352
353         switch (type) {
354         case TC_SETUP_CLSFLOWER:
355                 return mlx5e_rep_indr_offload(priv->netdev, type_data, priv,
356                                               flags);
357         default:
358                 return -EOPNOTSUPP;
359         }
360 }
361
362 static int mlx5e_rep_indr_setup_ft_cb(enum tc_setup_type type,
363                                       void *type_data, void *indr_priv)
364 {
365         struct mlx5e_rep_indr_block_priv *priv = indr_priv;
366         struct flow_cls_offload *f = type_data;
367         struct flow_cls_offload tmp;
368         struct mlx5e_priv *mpriv;
369         struct mlx5_eswitch *esw;
370         unsigned long flags;
371         int err;
372
373         mpriv = netdev_priv(priv->rpriv->netdev);
374         esw = mpriv->mdev->priv.eswitch;
375
376         flags = MLX5_TC_FLAG(EGRESS) |
377                 MLX5_TC_FLAG(ESW_OFFLOAD) |
378                 MLX5_TC_FLAG(FT_OFFLOAD);
379
380         switch (type) {
381         case TC_SETUP_CLSFLOWER:
382                 memcpy(&tmp, f, sizeof(*f));
383
384                 /* Re-use tc offload path by moving the ft flow to the
385                  * reserved ft chain.
386                  *
387                  * FT offload can use prio range [0, INT_MAX], so we normalize
388                  * it to range [1, mlx5_esw_chains_get_prio_range(esw)]
389                  * as with tc, where prio 0 isn't supported.
390                  *
391                  * We only support chain 0 of FT offload.
392                  */
393                 if (!mlx5_chains_prios_supported(esw_chains(esw)) ||
394                     tmp.common.prio >= mlx5_chains_get_prio_range(esw_chains(esw)) ||
395                     tmp.common.chain_index)
396                         return -EOPNOTSUPP;
397
398                 tmp.common.chain_index = mlx5_chains_get_nf_ft_chain(esw_chains(esw));
399                 tmp.common.prio++;
400                 err = mlx5e_rep_indr_offload(priv->netdev, &tmp, priv, flags);
401                 memcpy(&f->stats, &tmp.stats, sizeof(f->stats));
402                 return err;
403         default:
404                 return -EOPNOTSUPP;
405         }
406 }
407
408 static void mlx5e_rep_indr_block_unbind(void *cb_priv)
409 {
410         struct mlx5e_rep_indr_block_priv *indr_priv = cb_priv;
411
412         list_del(&indr_priv->list);
413         kfree(indr_priv);
414 }
415
416 static LIST_HEAD(mlx5e_block_cb_list);
417
418 static int
419 mlx5e_rep_indr_setup_block(struct net_device *netdev, struct Qdisc *sch,
420                            struct mlx5e_rep_priv *rpriv,
421                            struct flow_block_offload *f,
422                            flow_setup_cb_t *setup_cb,
423                            void *data,
424                            void (*cleanup)(struct flow_block_cb *block_cb))
425 {
426         struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
427         struct mlx5e_rep_indr_block_priv *indr_priv;
428         struct flow_block_cb *block_cb;
429
430         if (!mlx5e_tc_tun_device_to_offload(priv, netdev) &&
431             !(is_vlan_dev(netdev) && vlan_dev_real_dev(netdev) == rpriv->netdev))
432                 return -EOPNOTSUPP;
433
434         if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
435                 return -EOPNOTSUPP;
436
437         f->unlocked_driver_cb = true;
438         f->driver_block_list = &mlx5e_block_cb_list;
439
440         switch (f->command) {
441         case FLOW_BLOCK_BIND:
442                 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
443                 if (indr_priv)
444                         return -EEXIST;
445
446                 indr_priv = kmalloc(sizeof(*indr_priv), GFP_KERNEL);
447                 if (!indr_priv)
448                         return -ENOMEM;
449
450                 indr_priv->netdev = netdev;
451                 indr_priv->rpriv = rpriv;
452                 list_add(&indr_priv->list,
453                          &rpriv->uplink_priv.tc_indr_block_priv_list);
454
455                 block_cb = flow_indr_block_cb_alloc(setup_cb, indr_priv, indr_priv,
456                                                     mlx5e_rep_indr_block_unbind,
457                                                     f, netdev, sch, data, rpriv,
458                                                     cleanup);
459                 if (IS_ERR(block_cb)) {
460                         list_del(&indr_priv->list);
461                         kfree(indr_priv);
462                         return PTR_ERR(block_cb);
463                 }
464                 flow_block_cb_add(block_cb, f);
465                 list_add_tail(&block_cb->driver_list, &mlx5e_block_cb_list);
466
467                 return 0;
468         case FLOW_BLOCK_UNBIND:
469                 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
470                 if (!indr_priv)
471                         return -ENOENT;
472
473                 block_cb = flow_block_cb_lookup(f->block, setup_cb, indr_priv);
474                 if (!block_cb)
475                         return -ENOENT;
476
477                 flow_indr_block_cb_remove(block_cb, f);
478                 list_del(&block_cb->driver_list);
479                 return 0;
480         default:
481                 return -EOPNOTSUPP;
482         }
483         return 0;
484 }
485
486 static
487 int mlx5e_rep_indr_setup_cb(struct net_device *netdev, struct Qdisc *sch, void *cb_priv,
488                             enum tc_setup_type type, void *type_data,
489                             void *data,
490                             void (*cleanup)(struct flow_block_cb *block_cb))
491 {
492         switch (type) {
493         case TC_SETUP_BLOCK:
494                 return mlx5e_rep_indr_setup_block(netdev, sch, cb_priv, type_data,
495                                                   mlx5e_rep_indr_setup_tc_cb,
496                                                   data, cleanup);
497         case TC_SETUP_FT:
498                 return mlx5e_rep_indr_setup_block(netdev, sch, cb_priv, type_data,
499                                                   mlx5e_rep_indr_setup_ft_cb,
500                                                   data, cleanup);
501         default:
502                 return -EOPNOTSUPP;
503         }
504 }
505
506 int mlx5e_rep_tc_netdevice_event_register(struct mlx5e_rep_priv *rpriv)
507 {
508         struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
509
510         /* init indirect block notifications */
511         INIT_LIST_HEAD(&uplink_priv->tc_indr_block_priv_list);
512
513         return flow_indr_dev_register(mlx5e_rep_indr_setup_cb, rpriv);
514 }
515
516 void mlx5e_rep_tc_netdevice_event_unregister(struct mlx5e_rep_priv *rpriv)
517 {
518         flow_indr_dev_unregister(mlx5e_rep_indr_setup_cb, rpriv,
519                                  mlx5e_rep_indr_block_unbind);
520 }
521
522 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
523 static bool mlx5e_restore_tunnel(struct mlx5e_priv *priv, struct sk_buff *skb,
524                                  struct mlx5e_tc_update_priv *tc_priv,
525                                  u32 tunnel_id)
526 {
527         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
528         struct tunnel_match_enc_opts enc_opts = {};
529         struct mlx5_rep_uplink_priv *uplink_priv;
530         struct mlx5e_rep_priv *uplink_rpriv;
531         struct metadata_dst *tun_dst;
532         struct tunnel_match_key key;
533         u32 tun_id, enc_opts_id;
534         struct net_device *dev;
535         int err;
536
537         enc_opts_id = tunnel_id & ENC_OPTS_BITS_MASK;
538         tun_id = tunnel_id >> ENC_OPTS_BITS;
539
540         if (!tun_id)
541                 return true;
542
543         uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
544         uplink_priv = &uplink_rpriv->uplink_priv;
545
546         err = mapping_find(uplink_priv->tunnel_mapping, tun_id, &key);
547         if (err) {
548                 WARN_ON_ONCE(true);
549                 netdev_dbg(priv->netdev,
550                            "Couldn't find tunnel for tun_id: %d, err: %d\n",
551                            tun_id, err);
552                 return false;
553         }
554
555         if (enc_opts_id) {
556                 err = mapping_find(uplink_priv->tunnel_enc_opts_mapping,
557                                    enc_opts_id, &enc_opts);
558                 if (err) {
559                         netdev_dbg(priv->netdev,
560                                    "Couldn't find tunnel (opts) for tun_id: %d, err: %d\n",
561                                    enc_opts_id, err);
562                         return false;
563                 }
564         }
565
566         if (key.enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
567                 tun_dst = __ip_tun_set_dst(key.enc_ipv4.src, key.enc_ipv4.dst,
568                                            key.enc_ip.tos, key.enc_ip.ttl,
569                                            key.enc_tp.dst, TUNNEL_KEY,
570                                            key32_to_tunnel_id(key.enc_key_id.keyid),
571                                            enc_opts.key.len);
572         } else if (key.enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
573                 tun_dst = __ipv6_tun_set_dst(&key.enc_ipv6.src, &key.enc_ipv6.dst,
574                                              key.enc_ip.tos, key.enc_ip.ttl,
575                                              key.enc_tp.dst, 0, TUNNEL_KEY,
576                                              key32_to_tunnel_id(key.enc_key_id.keyid),
577                                              enc_opts.key.len);
578         } else {
579                 netdev_dbg(priv->netdev,
580                            "Couldn't restore tunnel, unsupported addr_type: %d\n",
581                            key.enc_control.addr_type);
582                 return false;
583         }
584
585         if (!tun_dst) {
586                 netdev_dbg(priv->netdev, "Couldn't restore tunnel, no tun_dst\n");
587                 return false;
588         }
589
590         tun_dst->u.tun_info.key.tp_src = key.enc_tp.src;
591
592         if (enc_opts.key.len)
593                 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
594                                         enc_opts.key.data,
595                                         enc_opts.key.len,
596                                         enc_opts.key.dst_opt_type);
597
598         skb_dst_set(skb, (struct dst_entry *)tun_dst);
599         dev = dev_get_by_index(&init_net, key.filter_ifindex);
600         if (!dev) {
601                 netdev_dbg(priv->netdev,
602                            "Couldn't find tunnel device with ifindex: %d\n",
603                            key.filter_ifindex);
604                 return false;
605         }
606
607         /* Set tun_dev so we do dev_put() after datapath */
608         tc_priv->tun_dev = dev;
609
610         skb->dev = dev;
611
612         return true;
613 }
614 #endif /* CONFIG_NET_TC_SKB_EXT */
615
616 bool mlx5e_rep_tc_update_skb(struct mlx5_cqe64 *cqe,
617                              struct sk_buff *skb,
618                              struct mlx5e_tc_update_priv *tc_priv)
619 {
620 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
621         u32 chain = 0, reg_c0, reg_c1, tunnel_id, zone_restore_id;
622         struct mlx5_rep_uplink_priv *uplink_priv;
623         struct mlx5e_rep_priv *uplink_rpriv;
624         struct tc_skb_ext *tc_skb_ext;
625         struct mlx5_eswitch *esw;
626         struct mlx5e_priv *priv;
627         int err;
628
629         reg_c0 = (be32_to_cpu(cqe->sop_drop_qpn) & MLX5E_TC_FLOW_ID_MASK);
630         if (!reg_c0 || reg_c0 == MLX5_FS_DEFAULT_FLOW_TAG)
631                 return true;
632
633         /* If reg_c0 is not equal to the default flow tag then skb->mark
634          * is not supported and must be reset back to 0.
635          */
636         skb->mark = 0;
637
638         reg_c1 = be32_to_cpu(cqe->ft_metadata);
639
640         priv = netdev_priv(skb->dev);
641         esw = priv->mdev->priv.eswitch;
642
643         err = mlx5_get_chain_for_tag(esw_chains(esw), reg_c0, &chain);
644         if (err) {
645                 netdev_dbg(priv->netdev,
646                            "Couldn't find chain for chain tag: %d, err: %d\n",
647                            reg_c0, err);
648                 return false;
649         }
650
651         if (chain) {
652                 tc_skb_ext = skb_ext_add(skb, TC_SKB_EXT);
653                 if (!tc_skb_ext) {
654                         WARN_ON(1);
655                         return false;
656                 }
657
658                 tc_skb_ext->chain = chain;
659
660                 zone_restore_id = reg_c1 & ESW_ZONE_ID_MASK;
661
662                 uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
663                 uplink_priv = &uplink_rpriv->uplink_priv;
664                 if (!mlx5e_tc_ct_restore_flow(uplink_priv->ct_priv, skb,
665                                               zone_restore_id))
666                         return false;
667         }
668
669         tunnel_id = reg_c1 >> ESW_TUN_OFFSET;
670         return mlx5e_restore_tunnel(priv, skb, tc_priv, tunnel_id);
671 #endif /* CONFIG_NET_TC_SKB_EXT */
672
673         return true;
674 }
675
676 void mlx5_rep_tc_post_napi_receive(struct mlx5e_tc_update_priv *tc_priv)
677 {
678         if (tc_priv->tun_dev)
679                 dev_put(tc_priv->tun_dev);
680 }