net/mlx5e: Convert rep stats to mlx5e_stats_grp-based infra
[linux-2.6-microblaze.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_rep.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 <generated/utsrelease.h>
34 #include <linux/mlx5/fs.h>
35 #include <net/switchdev.h>
36 #include <net/pkt_cls.h>
37 #include <net/act_api.h>
38 #include <net/netevent.h>
39 #include <net/arp.h>
40 #include <net/devlink.h>
41 #include <net/ipv6_stubs.h>
42
43 #include "eswitch.h"
44 #include "eswitch_offloads_chains.h"
45 #include "en.h"
46 #include "en_rep.h"
47 #include "en_tc.h"
48 #include "en/tc_tun.h"
49 #include "fs_core.h"
50 #include "lib/port_tun.h"
51 #include "lib/mlx5.h"
52 #define CREATE_TRACE_POINTS
53 #include "diag/en_rep_tracepoint.h"
54
55 #define MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE \
56         max(0x7, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)
57 #define MLX5E_REP_PARAMS_DEF_NUM_CHANNELS 1
58
59 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
60
61 struct mlx5e_rep_indr_block_priv {
62         struct net_device *netdev;
63         struct mlx5e_rep_priv *rpriv;
64
65         struct list_head list;
66 };
67
68 static void mlx5e_rep_indr_unregister_block(struct mlx5e_rep_priv *rpriv,
69                                             struct net_device *netdev);
70
71 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
72                                   struct ethtool_drvinfo *drvinfo)
73 {
74         struct mlx5e_priv *priv = netdev_priv(dev);
75         struct mlx5_core_dev *mdev = priv->mdev;
76
77         strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
78                 sizeof(drvinfo->driver));
79         strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
80         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
81                  "%d.%d.%04d (%.16s)",
82                  fw_rev_maj(mdev), fw_rev_min(mdev),
83                  fw_rev_sub(mdev), mdev->board_id);
84 }
85
86 static void mlx5e_uplink_rep_get_drvinfo(struct net_device *dev,
87                                          struct ethtool_drvinfo *drvinfo)
88 {
89         struct mlx5e_priv *priv = netdev_priv(dev);
90
91         mlx5e_rep_get_drvinfo(dev, drvinfo);
92         strlcpy(drvinfo->bus_info, pci_name(priv->mdev->pdev),
93                 sizeof(drvinfo->bus_info));
94 }
95
96 static const struct counter_desc sw_rep_stats_desc[] = {
97         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
98         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
99         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
100         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
101 };
102
103 struct vport_stats {
104         u64 vport_rx_packets;
105         u64 vport_tx_packets;
106         u64 vport_rx_bytes;
107         u64 vport_tx_bytes;
108 };
109
110 static const struct counter_desc vport_rep_stats_desc[] = {
111         { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_packets) },
112         { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_bytes) },
113         { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_packets) },
114         { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_bytes) },
115 };
116
117 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
118 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
119
120 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(sw_rep)
121 {
122         return NUM_VPORT_REP_SW_COUNTERS;
123 }
124
125 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(sw_rep)
126 {
127         int i;
128
129         for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
130                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
131                        sw_rep_stats_desc[i].format);
132         return idx;
133 }
134
135 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(sw_rep)
136 {
137         int i;
138
139         for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
140                 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
141                                                    sw_rep_stats_desc, i);
142         return idx;
143 }
144
145 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(sw_rep)
146 {
147         struct mlx5e_sw_stats *s = &priv->stats.sw;
148         struct rtnl_link_stats64 stats64 = {};
149
150         memset(s, 0, sizeof(*s));
151         mlx5e_fold_sw_stats64(priv, &stats64);
152
153         s->rx_packets = stats64.rx_packets;
154         s->rx_bytes   = stats64.rx_bytes;
155         s->tx_packets = stats64.tx_packets;
156         s->tx_bytes   = stats64.tx_bytes;
157         s->tx_queue_dropped = stats64.tx_dropped;
158 }
159
160 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(vport_rep)
161 {
162         return NUM_VPORT_REP_HW_COUNTERS;
163 }
164
165 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(vport_rep)
166 {
167         int i;
168
169         for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
170                 strcpy(data + (idx++) * ETH_GSTRING_LEN, vport_rep_stats_desc[i].format);
171         return idx;
172 }
173
174 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(vport_rep)
175 {
176         int i;
177
178         for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
179                 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
180                                                    vport_rep_stats_desc, i);
181         return idx;
182 }
183
184 static void mlx5e_vf_rep_update_hw_counters(struct mlx5e_priv *priv)
185 {
186         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
187         struct mlx5e_rep_priv *rpriv = priv->ppriv;
188         struct mlx5_eswitch_rep *rep = rpriv->rep;
189         struct rtnl_link_stats64 *vport_stats;
190         struct ifla_vf_stats vf_stats;
191         int err;
192
193         err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
194         if (err) {
195                 pr_warn("vport %d error %d reading stats\n", rep->vport, err);
196                 return;
197         }
198
199         vport_stats = &priv->stats.vf_vport;
200         /* flip tx/rx as we are reporting the counters for the switch vport */
201         vport_stats->rx_packets = vf_stats.tx_packets;
202         vport_stats->rx_bytes   = vf_stats.tx_bytes;
203         vport_stats->tx_packets = vf_stats.rx_packets;
204         vport_stats->tx_bytes   = vf_stats.rx_bytes;
205 }
206
207 static void mlx5e_uplink_rep_update_hw_counters(struct mlx5e_priv *priv)
208 {
209         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
210         struct rtnl_link_stats64 *vport_stats;
211
212         MLX5E_STATS_GRP_OP(802_3, update_stats)(priv);
213
214         vport_stats = &priv->stats.vf_vport;
215
216         vport_stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
217         vport_stats->rx_bytes   = PPORT_802_3_GET(pstats, a_octets_received_ok);
218         vport_stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
219         vport_stats->tx_bytes   = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
220 }
221
222 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(vport_rep)
223 {
224         struct mlx5e_rep_priv *rpriv = priv->ppriv;
225         struct mlx5_eswitch_rep *rep = rpriv->rep;
226
227         if (rep->vport == MLX5_VPORT_UPLINK)
228                 mlx5e_uplink_rep_update_hw_counters(priv);
229         else
230                 mlx5e_vf_rep_update_hw_counters(priv);
231 }
232
233 static void mlx5e_rep_get_strings(struct net_device *dev,
234                                   u32 stringset, uint8_t *data)
235 {
236         struct mlx5e_priv *priv = netdev_priv(dev);
237
238         switch (stringset) {
239         case ETH_SS_STATS:
240                 mlx5e_stats_fill_strings(priv, data);
241                 break;
242         }
243 }
244
245 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
246                                         struct ethtool_stats *stats, u64 *data)
247 {
248         struct mlx5e_priv *priv = netdev_priv(dev);
249
250         mlx5e_ethtool_get_ethtool_stats(priv, stats, data);
251 }
252
253 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
254 {
255         struct mlx5e_priv *priv = netdev_priv(dev);
256
257         switch (sset) {
258         case ETH_SS_STATS:
259                 return mlx5e_stats_total_num(priv);
260         default:
261                 return -EOPNOTSUPP;
262         }
263 }
264
265 static void mlx5e_rep_get_ringparam(struct net_device *dev,
266                                 struct ethtool_ringparam *param)
267 {
268         struct mlx5e_priv *priv = netdev_priv(dev);
269
270         mlx5e_ethtool_get_ringparam(priv, param);
271 }
272
273 static int mlx5e_rep_set_ringparam(struct net_device *dev,
274                                struct ethtool_ringparam *param)
275 {
276         struct mlx5e_priv *priv = netdev_priv(dev);
277
278         return mlx5e_ethtool_set_ringparam(priv, param);
279 }
280
281 static int mlx5e_replace_rep_vport_rx_rule(struct mlx5e_priv *priv,
282                                            struct mlx5_flow_destination *dest)
283 {
284         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
285         struct mlx5e_rep_priv *rpriv = priv->ppriv;
286         struct mlx5_eswitch_rep *rep = rpriv->rep;
287         struct mlx5_flow_handle *flow_rule;
288
289         flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
290                                                       rep->vport,
291                                                       dest);
292         if (IS_ERR(flow_rule))
293                 return PTR_ERR(flow_rule);
294
295         mlx5_del_flow_rules(rpriv->vport_rx_rule);
296         rpriv->vport_rx_rule = flow_rule;
297         return 0;
298 }
299
300 static void mlx5e_rep_get_channels(struct net_device *dev,
301                                    struct ethtool_channels *ch)
302 {
303         struct mlx5e_priv *priv = netdev_priv(dev);
304
305         mlx5e_ethtool_get_channels(priv, ch);
306 }
307
308 static int mlx5e_rep_set_channels(struct net_device *dev,
309                                   struct ethtool_channels *ch)
310 {
311         struct mlx5e_priv *priv = netdev_priv(dev);
312         u16 curr_channels_amount = priv->channels.params.num_channels;
313         u32 new_channels_amount = ch->combined_count;
314         struct mlx5_flow_destination new_dest;
315         int err = 0;
316
317         err = mlx5e_ethtool_set_channels(priv, ch);
318         if (err)
319                 return err;
320
321         if (curr_channels_amount == 1 && new_channels_amount > 1) {
322                 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
323                 new_dest.ft = priv->fs.ttc.ft.t;
324         } else if (new_channels_amount == 1 && curr_channels_amount > 1) {
325                 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
326                 new_dest.tir_num = priv->direct_tir[0].tirn;
327         } else {
328                 return 0;
329         }
330
331         err = mlx5e_replace_rep_vport_rx_rule(priv, &new_dest);
332         if (err) {
333                 netdev_warn(priv->netdev, "Failed to update vport rx rule, when going from (%d) channels to (%d) channels\n",
334                             curr_channels_amount, new_channels_amount);
335                 return err;
336         }
337
338         return 0;
339 }
340
341 static int mlx5e_rep_get_coalesce(struct net_device *netdev,
342                                   struct ethtool_coalesce *coal)
343 {
344         struct mlx5e_priv *priv = netdev_priv(netdev);
345
346         return mlx5e_ethtool_get_coalesce(priv, coal);
347 }
348
349 static int mlx5e_rep_set_coalesce(struct net_device *netdev,
350                                   struct ethtool_coalesce *coal)
351 {
352         struct mlx5e_priv *priv = netdev_priv(netdev);
353
354         return mlx5e_ethtool_set_coalesce(priv, coal);
355 }
356
357 static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
358 {
359         struct mlx5e_priv *priv = netdev_priv(netdev);
360
361         return mlx5e_ethtool_get_rxfh_key_size(priv);
362 }
363
364 static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
365 {
366         struct mlx5e_priv *priv = netdev_priv(netdev);
367
368         return mlx5e_ethtool_get_rxfh_indir_size(priv);
369 }
370
371 static void mlx5e_uplink_rep_get_pauseparam(struct net_device *netdev,
372                                             struct ethtool_pauseparam *pauseparam)
373 {
374         struct mlx5e_priv *priv = netdev_priv(netdev);
375
376         mlx5e_ethtool_get_pauseparam(priv, pauseparam);
377 }
378
379 static int mlx5e_uplink_rep_set_pauseparam(struct net_device *netdev,
380                                            struct ethtool_pauseparam *pauseparam)
381 {
382         struct mlx5e_priv *priv = netdev_priv(netdev);
383
384         return mlx5e_ethtool_set_pauseparam(priv, pauseparam);
385 }
386
387 static int mlx5e_uplink_rep_get_link_ksettings(struct net_device *netdev,
388                                                struct ethtool_link_ksettings *link_ksettings)
389 {
390         struct mlx5e_priv *priv = netdev_priv(netdev);
391
392         return mlx5e_ethtool_get_link_ksettings(priv, link_ksettings);
393 }
394
395 static int mlx5e_uplink_rep_set_link_ksettings(struct net_device *netdev,
396                                                const struct ethtool_link_ksettings *link_ksettings)
397 {
398         struct mlx5e_priv *priv = netdev_priv(netdev);
399
400         return mlx5e_ethtool_set_link_ksettings(priv, link_ksettings);
401 }
402
403 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
404         .get_drvinfo       = mlx5e_rep_get_drvinfo,
405         .get_link          = ethtool_op_get_link,
406         .get_strings       = mlx5e_rep_get_strings,
407         .get_sset_count    = mlx5e_rep_get_sset_count,
408         .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
409         .get_ringparam     = mlx5e_rep_get_ringparam,
410         .set_ringparam     = mlx5e_rep_set_ringparam,
411         .get_channels      = mlx5e_rep_get_channels,
412         .set_channels      = mlx5e_rep_set_channels,
413         .get_coalesce      = mlx5e_rep_get_coalesce,
414         .set_coalesce      = mlx5e_rep_set_coalesce,
415         .get_rxfh_key_size   = mlx5e_rep_get_rxfh_key_size,
416         .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
417 };
418
419 static const struct ethtool_ops mlx5e_uplink_rep_ethtool_ops = {
420         .get_drvinfo       = mlx5e_uplink_rep_get_drvinfo,
421         .get_link          = ethtool_op_get_link,
422         .get_strings       = mlx5e_rep_get_strings,
423         .get_sset_count    = mlx5e_rep_get_sset_count,
424         .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
425         .get_ringparam     = mlx5e_rep_get_ringparam,
426         .set_ringparam     = mlx5e_rep_set_ringparam,
427         .get_channels      = mlx5e_rep_get_channels,
428         .set_channels      = mlx5e_rep_set_channels,
429         .get_coalesce      = mlx5e_rep_get_coalesce,
430         .set_coalesce      = mlx5e_rep_set_coalesce,
431         .get_link_ksettings = mlx5e_uplink_rep_get_link_ksettings,
432         .set_link_ksettings = mlx5e_uplink_rep_set_link_ksettings,
433         .get_rxfh_key_size   = mlx5e_rep_get_rxfh_key_size,
434         .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
435         .get_pauseparam    = mlx5e_uplink_rep_get_pauseparam,
436         .set_pauseparam    = mlx5e_uplink_rep_set_pauseparam,
437 };
438
439 static void mlx5e_rep_get_port_parent_id(struct net_device *dev,
440                                          struct netdev_phys_item_id *ppid)
441 {
442         struct mlx5e_priv *priv;
443         u64 parent_id;
444
445         priv = netdev_priv(dev);
446
447         parent_id = mlx5_query_nic_system_image_guid(priv->mdev);
448         ppid->id_len = sizeof(parent_id);
449         memcpy(ppid->id, &parent_id, sizeof(parent_id));
450 }
451
452 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
453                                  struct mlx5_eswitch_rep *rep)
454 {
455         struct mlx5e_rep_sq *rep_sq, *tmp;
456         struct mlx5e_rep_priv *rpriv;
457
458         if (esw->mode != MLX5_ESWITCH_OFFLOADS)
459                 return;
460
461         rpriv = mlx5e_rep_to_rep_priv(rep);
462         list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
463                 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
464                 list_del(&rep_sq->list);
465                 kfree(rep_sq);
466         }
467 }
468
469 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
470                                  struct mlx5_eswitch_rep *rep,
471                                  u32 *sqns_array, int sqns_num)
472 {
473         struct mlx5_flow_handle *flow_rule;
474         struct mlx5e_rep_priv *rpriv;
475         struct mlx5e_rep_sq *rep_sq;
476         int err;
477         int i;
478
479         if (esw->mode != MLX5_ESWITCH_OFFLOADS)
480                 return 0;
481
482         rpriv = mlx5e_rep_to_rep_priv(rep);
483         for (i = 0; i < sqns_num; i++) {
484                 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
485                 if (!rep_sq) {
486                         err = -ENOMEM;
487                         goto out_err;
488                 }
489
490                 /* Add re-inject rule to the PF/representor sqs */
491                 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
492                                                                 rep->vport,
493                                                                 sqns_array[i]);
494                 if (IS_ERR(flow_rule)) {
495                         err = PTR_ERR(flow_rule);
496                         kfree(rep_sq);
497                         goto out_err;
498                 }
499                 rep_sq->send_to_vport_rule = flow_rule;
500                 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
501         }
502         return 0;
503
504 out_err:
505         mlx5e_sqs2vport_stop(esw, rep);
506         return err;
507 }
508
509 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
510 {
511         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
512         struct mlx5e_rep_priv *rpriv = priv->ppriv;
513         struct mlx5_eswitch_rep *rep = rpriv->rep;
514         struct mlx5e_channel *c;
515         int n, tc, num_sqs = 0;
516         int err = -ENOMEM;
517         u32 *sqs;
518
519         sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(*sqs), GFP_KERNEL);
520         if (!sqs)
521                 goto out;
522
523         for (n = 0; n < priv->channels.num; n++) {
524                 c = priv->channels.c[n];
525                 for (tc = 0; tc < c->num_tc; tc++)
526                         sqs[num_sqs++] = c->sq[tc].sqn;
527         }
528
529         err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
530         kfree(sqs);
531
532 out:
533         if (err)
534                 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
535         return err;
536 }
537
538 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
539 {
540         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
541         struct mlx5e_rep_priv *rpriv = priv->ppriv;
542         struct mlx5_eswitch_rep *rep = rpriv->rep;
543
544         mlx5e_sqs2vport_stop(esw, rep);
545 }
546
547 static unsigned long mlx5e_rep_ipv6_interval(void)
548 {
549         if (IS_ENABLED(CONFIG_IPV6) && ipv6_stub->nd_tbl)
550                 return NEIGH_VAR(&ipv6_stub->nd_tbl->parms, DELAY_PROBE_TIME);
551
552         return ~0UL;
553 }
554
555 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv *rpriv)
556 {
557         unsigned long ipv4_interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
558         unsigned long ipv6_interval = mlx5e_rep_ipv6_interval();
559         struct net_device *netdev = rpriv->netdev;
560         struct mlx5e_priv *priv = netdev_priv(netdev);
561
562         rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);
563         mlx5_fc_update_sampling_interval(priv->mdev, rpriv->neigh_update.min_interval);
564 }
565
566 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv)
567 {
568         struct mlx5e_rep_priv *rpriv = priv->ppriv;
569         struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
570
571         mlx5_fc_queue_stats_work(priv->mdev,
572                                  &neigh_update->neigh_stats_work,
573                                  neigh_update->min_interval);
574 }
575
576 static bool mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry *nhe)
577 {
578         return refcount_inc_not_zero(&nhe->refcnt);
579 }
580
581 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_neigh_hash_entry *nhe);
582
583 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry *nhe)
584 {
585         if (refcount_dec_and_test(&nhe->refcnt)) {
586                 mlx5e_rep_neigh_entry_remove(nhe);
587                 kfree_rcu(nhe, rcu);
588         }
589 }
590
591 static struct mlx5e_neigh_hash_entry *
592 mlx5e_get_next_nhe(struct mlx5e_rep_priv *rpriv,
593                    struct mlx5e_neigh_hash_entry *nhe)
594 {
595         struct mlx5e_neigh_hash_entry *next = NULL;
596
597         rcu_read_lock();
598
599         for (next = nhe ?
600                      list_next_or_null_rcu(&rpriv->neigh_update.neigh_list,
601                                            &nhe->neigh_list,
602                                            struct mlx5e_neigh_hash_entry,
603                                            neigh_list) :
604                      list_first_or_null_rcu(&rpriv->neigh_update.neigh_list,
605                                             struct mlx5e_neigh_hash_entry,
606                                             neigh_list);
607              next;
608              next = list_next_or_null_rcu(&rpriv->neigh_update.neigh_list,
609                                           &next->neigh_list,
610                                           struct mlx5e_neigh_hash_entry,
611                                           neigh_list))
612                 if (mlx5e_rep_neigh_entry_hold(next))
613                         break;
614
615         rcu_read_unlock();
616
617         if (nhe)
618                 mlx5e_rep_neigh_entry_release(nhe);
619
620         return next;
621 }
622
623 static void mlx5e_rep_neigh_stats_work(struct work_struct *work)
624 {
625         struct mlx5e_rep_priv *rpriv = container_of(work, struct mlx5e_rep_priv,
626                                                     neigh_update.neigh_stats_work.work);
627         struct net_device *netdev = rpriv->netdev;
628         struct mlx5e_priv *priv = netdev_priv(netdev);
629         struct mlx5e_neigh_hash_entry *nhe = NULL;
630
631         rtnl_lock();
632         if (!list_empty(&rpriv->neigh_update.neigh_list))
633                 mlx5e_rep_queue_neigh_stats_work(priv);
634
635         while ((nhe = mlx5e_get_next_nhe(rpriv, nhe)) != NULL)
636                 mlx5e_tc_update_neigh_used_value(nhe);
637
638         rtnl_unlock();
639 }
640
641 static void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
642                                    struct mlx5e_encap_entry *e,
643                                    bool neigh_connected,
644                                    unsigned char ha[ETH_ALEN])
645 {
646         struct ethhdr *eth = (struct ethhdr *)e->encap_header;
647         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
648         bool encap_connected;
649         LIST_HEAD(flow_list);
650
651         ASSERT_RTNL();
652
653         /* wait for encap to be fully initialized */
654         wait_for_completion(&e->res_ready);
655
656         mutex_lock(&esw->offloads.encap_tbl_lock);
657         encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
658         if (e->compl_result < 0 || (encap_connected == neigh_connected &&
659                                     ether_addr_equal(e->h_dest, ha)))
660                 goto unlock;
661
662         mlx5e_take_all_encap_flows(e, &flow_list);
663
664         if ((e->flags & MLX5_ENCAP_ENTRY_VALID) &&
665             (!neigh_connected || !ether_addr_equal(e->h_dest, ha)))
666                 mlx5e_tc_encap_flows_del(priv, e, &flow_list);
667
668         if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
669                 ether_addr_copy(e->h_dest, ha);
670                 ether_addr_copy(eth->h_dest, ha);
671                 /* Update the encap source mac, in case that we delete
672                  * the flows when encap source mac changed.
673                  */
674                 ether_addr_copy(eth->h_source, e->route_dev->dev_addr);
675
676                 mlx5e_tc_encap_flows_add(priv, e, &flow_list);
677         }
678 unlock:
679         mutex_unlock(&esw->offloads.encap_tbl_lock);
680         mlx5e_put_encap_flow_list(priv, &flow_list);
681 }
682
683 static void mlx5e_rep_neigh_update(struct work_struct *work)
684 {
685         struct mlx5e_neigh_hash_entry *nhe =
686                 container_of(work, struct mlx5e_neigh_hash_entry, neigh_update_work);
687         struct neighbour *n = nhe->n;
688         struct mlx5e_encap_entry *e;
689         unsigned char ha[ETH_ALEN];
690         struct mlx5e_priv *priv;
691         bool neigh_connected;
692         u8 nud_state, dead;
693
694         rtnl_lock();
695
696         /* If these parameters are changed after we release the lock,
697          * we'll receive another event letting us know about it.
698          * We use this lock to avoid inconsistency between the neigh validity
699          * and it's hw address.
700          */
701         read_lock_bh(&n->lock);
702         memcpy(ha, n->ha, ETH_ALEN);
703         nud_state = n->nud_state;
704         dead = n->dead;
705         read_unlock_bh(&n->lock);
706
707         neigh_connected = (nud_state & NUD_VALID) && !dead;
708
709         trace_mlx5e_rep_neigh_update(nhe, ha, neigh_connected);
710
711         list_for_each_entry(e, &nhe->encap_list, encap_list) {
712                 if (!mlx5e_encap_take(e))
713                         continue;
714
715                 priv = netdev_priv(e->out_dev);
716                 mlx5e_rep_update_flows(priv, e, neigh_connected, ha);
717                 mlx5e_encap_put(priv, e);
718         }
719         mlx5e_rep_neigh_entry_release(nhe);
720         rtnl_unlock();
721         neigh_release(n);
722 }
723
724 static struct mlx5e_rep_indr_block_priv *
725 mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv *rpriv,
726                                  struct net_device *netdev)
727 {
728         struct mlx5e_rep_indr_block_priv *cb_priv;
729
730         /* All callback list access should be protected by RTNL. */
731         ASSERT_RTNL();
732
733         list_for_each_entry(cb_priv,
734                             &rpriv->uplink_priv.tc_indr_block_priv_list,
735                             list)
736                 if (cb_priv->netdev == netdev)
737                         return cb_priv;
738
739         return NULL;
740 }
741
742 static void mlx5e_rep_indr_clean_block_privs(struct mlx5e_rep_priv *rpriv)
743 {
744         struct mlx5e_rep_indr_block_priv *cb_priv, *temp;
745         struct list_head *head = &rpriv->uplink_priv.tc_indr_block_priv_list;
746
747         list_for_each_entry_safe(cb_priv, temp, head, list) {
748                 mlx5e_rep_indr_unregister_block(rpriv, cb_priv->netdev);
749                 kfree(cb_priv);
750         }
751 }
752
753 static int
754 mlx5e_rep_indr_offload(struct net_device *netdev,
755                        struct flow_cls_offload *flower,
756                        struct mlx5e_rep_indr_block_priv *indr_priv)
757 {
758         unsigned long flags = MLX5_TC_FLAG(EGRESS) | MLX5_TC_FLAG(ESW_OFFLOAD);
759         struct mlx5e_priv *priv = netdev_priv(indr_priv->rpriv->netdev);
760         int err = 0;
761
762         switch (flower->command) {
763         case FLOW_CLS_REPLACE:
764                 err = mlx5e_configure_flower(netdev, priv, flower, flags);
765                 break;
766         case FLOW_CLS_DESTROY:
767                 err = mlx5e_delete_flower(netdev, priv, flower, flags);
768                 break;
769         case FLOW_CLS_STATS:
770                 err = mlx5e_stats_flower(netdev, priv, flower, flags);
771                 break;
772         default:
773                 err = -EOPNOTSUPP;
774         }
775
776         return err;
777 }
778
779 static int mlx5e_rep_indr_setup_block_cb(enum tc_setup_type type,
780                                          void *type_data, void *indr_priv)
781 {
782         struct mlx5e_rep_indr_block_priv *priv = indr_priv;
783
784         switch (type) {
785         case TC_SETUP_CLSFLOWER:
786                 return mlx5e_rep_indr_offload(priv->netdev, type_data, priv);
787         default:
788                 return -EOPNOTSUPP;
789         }
790 }
791
792 static void mlx5e_rep_indr_tc_block_unbind(void *cb_priv)
793 {
794         struct mlx5e_rep_indr_block_priv *indr_priv = cb_priv;
795
796         list_del(&indr_priv->list);
797         kfree(indr_priv);
798 }
799
800 static LIST_HEAD(mlx5e_block_cb_list);
801
802 static int
803 mlx5e_rep_indr_setup_tc_block(struct net_device *netdev,
804                               struct mlx5e_rep_priv *rpriv,
805                               struct flow_block_offload *f)
806 {
807         struct mlx5e_rep_indr_block_priv *indr_priv;
808         struct flow_block_cb *block_cb;
809
810         if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
811                 return -EOPNOTSUPP;
812
813         f->unlocked_driver_cb = true;
814         f->driver_block_list = &mlx5e_block_cb_list;
815
816         switch (f->command) {
817         case FLOW_BLOCK_BIND:
818                 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
819                 if (indr_priv)
820                         return -EEXIST;
821
822                 indr_priv = kmalloc(sizeof(*indr_priv), GFP_KERNEL);
823                 if (!indr_priv)
824                         return -ENOMEM;
825
826                 indr_priv->netdev = netdev;
827                 indr_priv->rpriv = rpriv;
828                 list_add(&indr_priv->list,
829                          &rpriv->uplink_priv.tc_indr_block_priv_list);
830
831                 block_cb = flow_block_cb_alloc(mlx5e_rep_indr_setup_block_cb,
832                                                indr_priv, indr_priv,
833                                                mlx5e_rep_indr_tc_block_unbind);
834                 if (IS_ERR(block_cb)) {
835                         list_del(&indr_priv->list);
836                         kfree(indr_priv);
837                         return PTR_ERR(block_cb);
838                 }
839                 flow_block_cb_add(block_cb, f);
840                 list_add_tail(&block_cb->driver_list, &mlx5e_block_cb_list);
841
842                 return 0;
843         case FLOW_BLOCK_UNBIND:
844                 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
845                 if (!indr_priv)
846                         return -ENOENT;
847
848                 block_cb = flow_block_cb_lookup(f->block,
849                                                 mlx5e_rep_indr_setup_block_cb,
850                                                 indr_priv);
851                 if (!block_cb)
852                         return -ENOENT;
853
854                 flow_block_cb_remove(block_cb, f);
855                 list_del(&block_cb->driver_list);
856                 return 0;
857         default:
858                 return -EOPNOTSUPP;
859         }
860         return 0;
861 }
862
863 static
864 int mlx5e_rep_indr_setup_tc_cb(struct net_device *netdev, void *cb_priv,
865                                enum tc_setup_type type, void *type_data)
866 {
867         switch (type) {
868         case TC_SETUP_BLOCK:
869                 return mlx5e_rep_indr_setup_tc_block(netdev, cb_priv,
870                                                       type_data);
871         default:
872                 return -EOPNOTSUPP;
873         }
874 }
875
876 static int mlx5e_rep_indr_register_block(struct mlx5e_rep_priv *rpriv,
877                                          struct net_device *netdev)
878 {
879         int err;
880
881         err = __flow_indr_block_cb_register(netdev, rpriv,
882                                             mlx5e_rep_indr_setup_tc_cb,
883                                             rpriv);
884         if (err) {
885                 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
886
887                 mlx5_core_err(priv->mdev, "Failed to register remote block notifier for %s err=%d\n",
888                               netdev_name(netdev), err);
889         }
890         return err;
891 }
892
893 static void mlx5e_rep_indr_unregister_block(struct mlx5e_rep_priv *rpriv,
894                                             struct net_device *netdev)
895 {
896         __flow_indr_block_cb_unregister(netdev, mlx5e_rep_indr_setup_tc_cb,
897                                         rpriv);
898 }
899
900 static int mlx5e_nic_rep_netdevice_event(struct notifier_block *nb,
901                                          unsigned long event, void *ptr)
902 {
903         struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
904                                                      uplink_priv.netdevice_nb);
905         struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
906         struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
907
908         if (!mlx5e_tc_tun_device_to_offload(priv, netdev) &&
909             !(is_vlan_dev(netdev) && vlan_dev_real_dev(netdev) == rpriv->netdev))
910                 return NOTIFY_OK;
911
912         switch (event) {
913         case NETDEV_REGISTER:
914                 mlx5e_rep_indr_register_block(rpriv, netdev);
915                 break;
916         case NETDEV_UNREGISTER:
917                 mlx5e_rep_indr_unregister_block(rpriv, netdev);
918                 break;
919         }
920         return NOTIFY_OK;
921 }
922
923 static void
924 mlx5e_rep_queue_neigh_update_work(struct mlx5e_priv *priv,
925                                   struct mlx5e_neigh_hash_entry *nhe,
926                                   struct neighbour *n)
927 {
928         /* Take a reference to ensure the neighbour and mlx5 encap
929          * entry won't be destructed until we drop the reference in
930          * delayed work.
931          */
932         neigh_hold(n);
933
934         /* This assignment is valid as long as the the neigh reference
935          * is taken
936          */
937         nhe->n = n;
938
939         if (!queue_work(priv->wq, &nhe->neigh_update_work)) {
940                 mlx5e_rep_neigh_entry_release(nhe);
941                 neigh_release(n);
942         }
943 }
944
945 static struct mlx5e_neigh_hash_entry *
946 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
947                              struct mlx5e_neigh *m_neigh);
948
949 static int mlx5e_rep_netevent_event(struct notifier_block *nb,
950                                     unsigned long event, void *ptr)
951 {
952         struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
953                                                     neigh_update.netevent_nb);
954         struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
955         struct net_device *netdev = rpriv->netdev;
956         struct mlx5e_priv *priv = netdev_priv(netdev);
957         struct mlx5e_neigh_hash_entry *nhe = NULL;
958         struct mlx5e_neigh m_neigh = {};
959         struct neigh_parms *p;
960         struct neighbour *n;
961         bool found = false;
962
963         switch (event) {
964         case NETEVENT_NEIGH_UPDATE:
965                 n = ptr;
966 #if IS_ENABLED(CONFIG_IPV6)
967                 if (n->tbl != ipv6_stub->nd_tbl && n->tbl != &arp_tbl)
968 #else
969                 if (n->tbl != &arp_tbl)
970 #endif
971                         return NOTIFY_DONE;
972
973                 m_neigh.dev = n->dev;
974                 m_neigh.family = n->ops->family;
975                 memcpy(&m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
976
977                 rcu_read_lock();
978                 nhe = mlx5e_rep_neigh_entry_lookup(priv, &m_neigh);
979                 rcu_read_unlock();
980                 if (!nhe)
981                         return NOTIFY_DONE;
982
983                 mlx5e_rep_queue_neigh_update_work(priv, nhe, n);
984                 break;
985
986         case NETEVENT_DELAY_PROBE_TIME_UPDATE:
987                 p = ptr;
988
989                 /* We check the device is present since we don't care about
990                  * changes in the default table, we only care about changes
991                  * done per device delay prob time parameter.
992                  */
993 #if IS_ENABLED(CONFIG_IPV6)
994                 if (!p->dev || (p->tbl != ipv6_stub->nd_tbl && p->tbl != &arp_tbl))
995 #else
996                 if (!p->dev || p->tbl != &arp_tbl)
997 #endif
998                         return NOTIFY_DONE;
999
1000                 rcu_read_lock();
1001                 list_for_each_entry_rcu(nhe, &neigh_update->neigh_list,
1002                                         neigh_list) {
1003                         if (p->dev == nhe->m_neigh.dev) {
1004                                 found = true;
1005                                 break;
1006                         }
1007                 }
1008                 rcu_read_unlock();
1009                 if (!found)
1010                         return NOTIFY_DONE;
1011
1012                 neigh_update->min_interval = min_t(unsigned long,
1013                                                    NEIGH_VAR(p, DELAY_PROBE_TIME),
1014                                                    neigh_update->min_interval);
1015                 mlx5_fc_update_sampling_interval(priv->mdev,
1016                                                  neigh_update->min_interval);
1017                 break;
1018         }
1019         return NOTIFY_DONE;
1020 }
1021
1022 static const struct rhashtable_params mlx5e_neigh_ht_params = {
1023         .head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
1024         .key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
1025         .key_len = sizeof(struct mlx5e_neigh),
1026         .automatic_shrinking = true,
1027 };
1028
1029 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv *rpriv)
1030 {
1031         struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
1032         int err;
1033
1034         err = rhashtable_init(&neigh_update->neigh_ht, &mlx5e_neigh_ht_params);
1035         if (err)
1036                 return err;
1037
1038         INIT_LIST_HEAD(&neigh_update->neigh_list);
1039         mutex_init(&neigh_update->encap_lock);
1040         INIT_DELAYED_WORK(&neigh_update->neigh_stats_work,
1041                           mlx5e_rep_neigh_stats_work);
1042         mlx5e_rep_neigh_update_init_interval(rpriv);
1043
1044         rpriv->neigh_update.netevent_nb.notifier_call = mlx5e_rep_netevent_event;
1045         err = register_netevent_notifier(&rpriv->neigh_update.netevent_nb);
1046         if (err)
1047                 goto out_err;
1048         return 0;
1049
1050 out_err:
1051         rhashtable_destroy(&neigh_update->neigh_ht);
1052         return err;
1053 }
1054
1055 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv *rpriv)
1056 {
1057         struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
1058         struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1059
1060         unregister_netevent_notifier(&neigh_update->netevent_nb);
1061
1062         flush_workqueue(priv->wq); /* flush neigh update works */
1063
1064         cancel_delayed_work_sync(&rpriv->neigh_update.neigh_stats_work);
1065
1066         mutex_destroy(&neigh_update->encap_lock);
1067         rhashtable_destroy(&neigh_update->neigh_ht);
1068 }
1069
1070 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv *priv,
1071                                         struct mlx5e_neigh_hash_entry *nhe)
1072 {
1073         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1074         int err;
1075
1076         err = rhashtable_insert_fast(&rpriv->neigh_update.neigh_ht,
1077                                      &nhe->rhash_node,
1078                                      mlx5e_neigh_ht_params);
1079         if (err)
1080                 return err;
1081
1082         list_add_rcu(&nhe->neigh_list, &rpriv->neigh_update.neigh_list);
1083
1084         return err;
1085 }
1086
1087 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_neigh_hash_entry *nhe)
1088 {
1089         struct mlx5e_rep_priv *rpriv = nhe->priv->ppriv;
1090
1091         mutex_lock(&rpriv->neigh_update.encap_lock);
1092
1093         list_del_rcu(&nhe->neigh_list);
1094
1095         rhashtable_remove_fast(&rpriv->neigh_update.neigh_ht,
1096                                &nhe->rhash_node,
1097                                mlx5e_neigh_ht_params);
1098         mutex_unlock(&rpriv->neigh_update.encap_lock);
1099 }
1100
1101 /* This function must only be called under the representor's encap_lock or
1102  * inside rcu read lock section.
1103  */
1104 static struct mlx5e_neigh_hash_entry *
1105 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
1106                              struct mlx5e_neigh *m_neigh)
1107 {
1108         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1109         struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
1110         struct mlx5e_neigh_hash_entry *nhe;
1111
1112         nhe = rhashtable_lookup_fast(&neigh_update->neigh_ht, m_neigh,
1113                                      mlx5e_neigh_ht_params);
1114         return nhe && mlx5e_rep_neigh_entry_hold(nhe) ? nhe : NULL;
1115 }
1116
1117 static int mlx5e_rep_neigh_entry_create(struct mlx5e_priv *priv,
1118                                         struct mlx5e_encap_entry *e,
1119                                         struct mlx5e_neigh_hash_entry **nhe)
1120 {
1121         int err;
1122
1123         *nhe = kzalloc(sizeof(**nhe), GFP_KERNEL);
1124         if (!*nhe)
1125                 return -ENOMEM;
1126
1127         (*nhe)->priv = priv;
1128         memcpy(&(*nhe)->m_neigh, &e->m_neigh, sizeof(e->m_neigh));
1129         INIT_WORK(&(*nhe)->neigh_update_work, mlx5e_rep_neigh_update);
1130         spin_lock_init(&(*nhe)->encap_list_lock);
1131         INIT_LIST_HEAD(&(*nhe)->encap_list);
1132         refcount_set(&(*nhe)->refcnt, 1);
1133
1134         err = mlx5e_rep_neigh_entry_insert(priv, *nhe);
1135         if (err)
1136                 goto out_free;
1137         return 0;
1138
1139 out_free:
1140         kfree(*nhe);
1141         return err;
1142 }
1143
1144 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
1145                                  struct mlx5e_encap_entry *e)
1146 {
1147         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1148         struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
1149         struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
1150         struct mlx5e_neigh_hash_entry *nhe;
1151         int err;
1152
1153         err = mlx5_tun_entropy_refcount_inc(tun_entropy, e->reformat_type);
1154         if (err)
1155                 return err;
1156
1157         mutex_lock(&rpriv->neigh_update.encap_lock);
1158         nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
1159         if (!nhe) {
1160                 err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
1161                 if (err) {
1162                         mutex_unlock(&rpriv->neigh_update.encap_lock);
1163                         mlx5_tun_entropy_refcount_dec(tun_entropy,
1164                                                       e->reformat_type);
1165                         return err;
1166                 }
1167         }
1168
1169         e->nhe = nhe;
1170         spin_lock(&nhe->encap_list_lock);
1171         list_add_rcu(&e->encap_list, &nhe->encap_list);
1172         spin_unlock(&nhe->encap_list_lock);
1173
1174         mutex_unlock(&rpriv->neigh_update.encap_lock);
1175
1176         return 0;
1177 }
1178
1179 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
1180                                   struct mlx5e_encap_entry *e)
1181 {
1182         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1183         struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
1184         struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
1185
1186         if (!e->nhe)
1187                 return;
1188
1189         spin_lock(&e->nhe->encap_list_lock);
1190         list_del_rcu(&e->encap_list);
1191         spin_unlock(&e->nhe->encap_list_lock);
1192
1193         mlx5e_rep_neigh_entry_release(e->nhe);
1194         e->nhe = NULL;
1195         mlx5_tun_entropy_refcount_dec(tun_entropy, e->reformat_type);
1196 }
1197
1198 static int mlx5e_rep_open(struct net_device *dev)
1199 {
1200         struct mlx5e_priv *priv = netdev_priv(dev);
1201         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1202         struct mlx5_eswitch_rep *rep = rpriv->rep;
1203         int err;
1204
1205         mutex_lock(&priv->state_lock);
1206         err = mlx5e_open_locked(dev);
1207         if (err)
1208                 goto unlock;
1209
1210         if (!mlx5_modify_vport_admin_state(priv->mdev,
1211                                            MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1212                                            rep->vport, 1,
1213                                            MLX5_VPORT_ADMIN_STATE_UP))
1214                 netif_carrier_on(dev);
1215
1216 unlock:
1217         mutex_unlock(&priv->state_lock);
1218         return err;
1219 }
1220
1221 static int mlx5e_rep_close(struct net_device *dev)
1222 {
1223         struct mlx5e_priv *priv = netdev_priv(dev);
1224         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1225         struct mlx5_eswitch_rep *rep = rpriv->rep;
1226         int ret;
1227
1228         mutex_lock(&priv->state_lock);
1229         mlx5_modify_vport_admin_state(priv->mdev,
1230                                       MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1231                                       rep->vport, 1,
1232                                       MLX5_VPORT_ADMIN_STATE_DOWN);
1233         ret = mlx5e_close_locked(dev);
1234         mutex_unlock(&priv->state_lock);
1235         return ret;
1236 }
1237
1238 static int
1239 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
1240                               struct flow_cls_offload *cls_flower, int flags)
1241 {
1242         switch (cls_flower->command) {
1243         case FLOW_CLS_REPLACE:
1244                 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
1245                                               flags);
1246         case FLOW_CLS_DESTROY:
1247                 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
1248                                            flags);
1249         case FLOW_CLS_STATS:
1250                 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
1251                                           flags);
1252         default:
1253                 return -EOPNOTSUPP;
1254         }
1255 }
1256
1257 static
1258 int mlx5e_rep_setup_tc_cls_matchall(struct mlx5e_priv *priv,
1259                                     struct tc_cls_matchall_offload *ma)
1260 {
1261         switch (ma->command) {
1262         case TC_CLSMATCHALL_REPLACE:
1263                 return mlx5e_tc_configure_matchall(priv, ma);
1264         case TC_CLSMATCHALL_DESTROY:
1265                 return mlx5e_tc_delete_matchall(priv, ma);
1266         case TC_CLSMATCHALL_STATS:
1267                 mlx5e_tc_stats_matchall(priv, ma);
1268                 return 0;
1269         default:
1270                 return -EOPNOTSUPP;
1271         }
1272 }
1273
1274 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
1275                                  void *cb_priv)
1276 {
1277         unsigned long flags = MLX5_TC_FLAG(INGRESS) | MLX5_TC_FLAG(ESW_OFFLOAD);
1278         struct mlx5e_priv *priv = cb_priv;
1279
1280         switch (type) {
1281         case TC_SETUP_CLSFLOWER:
1282                 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, flags);
1283         case TC_SETUP_CLSMATCHALL:
1284                 return mlx5e_rep_setup_tc_cls_matchall(priv, type_data);
1285         default:
1286                 return -EOPNOTSUPP;
1287         }
1288 }
1289
1290 static int mlx5e_rep_setup_ft_cb(enum tc_setup_type type, void *type_data,
1291                                  void *cb_priv)
1292 {
1293         struct flow_cls_offload tmp, *f = type_data;
1294         struct mlx5e_priv *priv = cb_priv;
1295         struct mlx5_eswitch *esw;
1296         unsigned long flags;
1297         int err;
1298
1299         flags = MLX5_TC_FLAG(INGRESS) |
1300                 MLX5_TC_FLAG(ESW_OFFLOAD) |
1301                 MLX5_TC_FLAG(FT_OFFLOAD);
1302         esw = priv->mdev->priv.eswitch;
1303
1304         switch (type) {
1305         case TC_SETUP_CLSFLOWER:
1306                 memcpy(&tmp, f, sizeof(*f));
1307
1308                 if (!mlx5_esw_chains_prios_supported(esw) ||
1309                     tmp.common.chain_index)
1310                         return -EOPNOTSUPP;
1311
1312                 /* Re-use tc offload path by moving the ft flow to the
1313                  * reserved ft chain.
1314                  *
1315                  * FT offload can use prio range [0, INT_MAX], so we normalize
1316                  * it to range [1, mlx5_esw_chains_get_prio_range(esw)]
1317                  * as with tc, where prio 0 isn't supported.
1318                  *
1319                  * We only support chain 0 of FT offload.
1320                  */
1321                 if (tmp.common.prio >= mlx5_esw_chains_get_prio_range(esw))
1322                         return -EOPNOTSUPP;
1323                 if (tmp.common.chain_index != 0)
1324                         return -EOPNOTSUPP;
1325
1326                 tmp.common.chain_index = mlx5_esw_chains_get_ft_chain(esw);
1327                 tmp.common.prio++;
1328                 err = mlx5e_rep_setup_tc_cls_flower(priv, &tmp, flags);
1329                 memcpy(&f->stats, &tmp.stats, sizeof(f->stats));
1330                 return err;
1331         default:
1332                 return -EOPNOTSUPP;
1333         }
1334 }
1335
1336 static LIST_HEAD(mlx5e_rep_block_tc_cb_list);
1337 static LIST_HEAD(mlx5e_rep_block_ft_cb_list);
1338 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
1339                               void *type_data)
1340 {
1341         struct mlx5e_priv *priv = netdev_priv(dev);
1342         struct flow_block_offload *f = type_data;
1343
1344         f->unlocked_driver_cb = true;
1345
1346         switch (type) {
1347         case TC_SETUP_BLOCK:
1348                 return flow_block_cb_setup_simple(type_data,
1349                                                   &mlx5e_rep_block_tc_cb_list,
1350                                                   mlx5e_rep_setup_tc_cb,
1351                                                   priv, priv, true);
1352         case TC_SETUP_FT:
1353                 return flow_block_cb_setup_simple(type_data,
1354                                                   &mlx5e_rep_block_ft_cb_list,
1355                                                   mlx5e_rep_setup_ft_cb,
1356                                                   priv, priv, true);
1357         default:
1358                 return -EOPNOTSUPP;
1359         }
1360 }
1361
1362 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
1363 {
1364         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1365         struct mlx5_eswitch_rep *rep;
1366
1367         if (!MLX5_ESWITCH_MANAGER(priv->mdev))
1368                 return false;
1369
1370         if (!rpriv) /* non vport rep mlx5e instances don't use this field */
1371                 return false;
1372
1373         rep = rpriv->rep;
1374         return (rep->vport == MLX5_VPORT_UPLINK);
1375 }
1376
1377 static bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
1378 {
1379         switch (attr_id) {
1380         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1381                         return true;
1382         }
1383
1384         return false;
1385 }
1386
1387 static int
1388 mlx5e_get_sw_stats64(const struct net_device *dev,
1389                      struct rtnl_link_stats64 *stats)
1390 {
1391         struct mlx5e_priv *priv = netdev_priv(dev);
1392
1393         mlx5e_fold_sw_stats64(priv, stats);
1394         return 0;
1395 }
1396
1397 static int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
1398                                        void *sp)
1399 {
1400         switch (attr_id) {
1401         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1402                 return mlx5e_get_sw_stats64(dev, sp);
1403         }
1404
1405         return -EINVAL;
1406 }
1407
1408 static void
1409 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
1410 {
1411         struct mlx5e_priv *priv = netdev_priv(dev);
1412
1413         /* update HW stats in background for next time */
1414         mlx5e_queue_update_stats(priv);
1415         memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
1416 }
1417
1418 static int mlx5e_rep_change_mtu(struct net_device *netdev, int new_mtu)
1419 {
1420         return mlx5e_change_mtu(netdev, new_mtu, NULL);
1421 }
1422
1423 static int mlx5e_uplink_rep_change_mtu(struct net_device *netdev, int new_mtu)
1424 {
1425         return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
1426 }
1427
1428 static int mlx5e_uplink_rep_set_mac(struct net_device *netdev, void *addr)
1429 {
1430         struct sockaddr *saddr = addr;
1431
1432         if (!is_valid_ether_addr(saddr->sa_data))
1433                 return -EADDRNOTAVAIL;
1434
1435         ether_addr_copy(netdev->dev_addr, saddr->sa_data);
1436         return 0;
1437 }
1438
1439 static int mlx5e_uplink_rep_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
1440                                         __be16 vlan_proto)
1441 {
1442         netdev_warn_once(dev, "legacy vf vlan setting isn't supported in switchdev mode\n");
1443
1444         if (vlan != 0)
1445                 return -EOPNOTSUPP;
1446
1447         /* allow setting 0-vid for compatibility with libvirt */
1448         return 0;
1449 }
1450
1451 static struct devlink_port *mlx5e_get_devlink_port(struct net_device *dev)
1452 {
1453         struct mlx5e_priv *priv = netdev_priv(dev);
1454         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1455
1456         return &rpriv->dl_port;
1457 }
1458
1459 static const struct net_device_ops mlx5e_netdev_ops_rep = {
1460         .ndo_open                = mlx5e_rep_open,
1461         .ndo_stop                = mlx5e_rep_close,
1462         .ndo_start_xmit          = mlx5e_xmit,
1463         .ndo_setup_tc            = mlx5e_rep_setup_tc,
1464         .ndo_get_devlink_port = mlx5e_get_devlink_port,
1465         .ndo_get_stats64         = mlx5e_rep_get_stats,
1466         .ndo_has_offload_stats   = mlx5e_rep_has_offload_stats,
1467         .ndo_get_offload_stats   = mlx5e_rep_get_offload_stats,
1468         .ndo_change_mtu          = mlx5e_rep_change_mtu,
1469 };
1470
1471 static const struct net_device_ops mlx5e_netdev_ops_uplink_rep = {
1472         .ndo_open                = mlx5e_open,
1473         .ndo_stop                = mlx5e_close,
1474         .ndo_start_xmit          = mlx5e_xmit,
1475         .ndo_set_mac_address     = mlx5e_uplink_rep_set_mac,
1476         .ndo_setup_tc            = mlx5e_rep_setup_tc,
1477         .ndo_get_devlink_port = mlx5e_get_devlink_port,
1478         .ndo_get_stats64         = mlx5e_get_stats,
1479         .ndo_has_offload_stats   = mlx5e_rep_has_offload_stats,
1480         .ndo_get_offload_stats   = mlx5e_rep_get_offload_stats,
1481         .ndo_change_mtu          = mlx5e_uplink_rep_change_mtu,
1482         .ndo_udp_tunnel_add      = mlx5e_add_vxlan_port,
1483         .ndo_udp_tunnel_del      = mlx5e_del_vxlan_port,
1484         .ndo_features_check      = mlx5e_features_check,
1485         .ndo_set_vf_mac          = mlx5e_set_vf_mac,
1486         .ndo_set_vf_rate         = mlx5e_set_vf_rate,
1487         .ndo_get_vf_config       = mlx5e_get_vf_config,
1488         .ndo_get_vf_stats        = mlx5e_get_vf_stats,
1489         .ndo_set_vf_vlan         = mlx5e_uplink_rep_set_vf_vlan,
1490         .ndo_set_features        = mlx5e_set_features,
1491 };
1492
1493 bool mlx5e_eswitch_rep(struct net_device *netdev)
1494 {
1495         if (netdev->netdev_ops == &mlx5e_netdev_ops_rep ||
1496             netdev->netdev_ops == &mlx5e_netdev_ops_uplink_rep)
1497                 return true;
1498
1499         return false;
1500 }
1501
1502 static void mlx5e_build_rep_params(struct net_device *netdev)
1503 {
1504         struct mlx5e_priv *priv = netdev_priv(netdev);
1505         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1506         struct mlx5_eswitch_rep *rep = rpriv->rep;
1507         struct mlx5_core_dev *mdev = priv->mdev;
1508         struct mlx5e_params *params;
1509
1510         u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
1511                                          MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
1512                                          MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1513
1514         params = &priv->channels.params;
1515         params->hard_mtu    = MLX5E_ETH_HARD_MTU;
1516         params->sw_mtu      = netdev->mtu;
1517
1518         /* SQ */
1519         if (rep->vport == MLX5_VPORT_UPLINK)
1520                 params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
1521         else
1522                 params->log_sq_size = MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE;
1523
1524         /* RQ */
1525         mlx5e_build_rq_params(mdev, params);
1526
1527         /* CQ moderation params */
1528         params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
1529         mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
1530
1531         params->num_tc                = 1;
1532         params->tunneled_offload_en = false;
1533
1534         mlx5_query_min_inline(mdev, &params->tx_min_inline_mode);
1535
1536         /* RSS */
1537         mlx5e_build_rss_params(&priv->rss_params, params->num_channels);
1538 }
1539
1540 static void mlx5e_build_rep_netdev(struct net_device *netdev)
1541 {
1542         struct mlx5e_priv *priv = netdev_priv(netdev);
1543         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1544         struct mlx5_eswitch_rep *rep = rpriv->rep;
1545         struct mlx5_core_dev *mdev = priv->mdev;
1546
1547         if (rep->vport == MLX5_VPORT_UPLINK) {
1548                 SET_NETDEV_DEV(netdev, mdev->device);
1549                 netdev->netdev_ops = &mlx5e_netdev_ops_uplink_rep;
1550                 /* we want a persistent mac for the uplink rep */
1551                 mlx5_query_mac_address(mdev, netdev->dev_addr);
1552                 netdev->ethtool_ops = &mlx5e_uplink_rep_ethtool_ops;
1553 #ifdef CONFIG_MLX5_CORE_EN_DCB
1554                 if (MLX5_CAP_GEN(mdev, qos))
1555                         netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
1556 #endif
1557         } else {
1558                 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
1559                 eth_hw_addr_random(netdev);
1560                 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
1561         }
1562
1563         netdev->watchdog_timeo    = 15 * HZ;
1564
1565         netdev->features       |= NETIF_F_NETNS_LOCAL;
1566
1567         netdev->hw_features    |= NETIF_F_HW_TC;
1568         netdev->hw_features    |= NETIF_F_SG;
1569         netdev->hw_features    |= NETIF_F_IP_CSUM;
1570         netdev->hw_features    |= NETIF_F_IPV6_CSUM;
1571         netdev->hw_features    |= NETIF_F_GRO;
1572         netdev->hw_features    |= NETIF_F_TSO;
1573         netdev->hw_features    |= NETIF_F_TSO6;
1574         netdev->hw_features    |= NETIF_F_RXCSUM;
1575
1576         if (rep->vport == MLX5_VPORT_UPLINK)
1577                 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
1578         else
1579                 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1580
1581         netdev->features |= netdev->hw_features;
1582 }
1583
1584 static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
1585                           struct net_device *netdev,
1586                           const struct mlx5e_profile *profile,
1587                           void *ppriv)
1588 {
1589         struct mlx5e_priv *priv = netdev_priv(netdev);
1590         int err;
1591
1592         err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
1593         if (err)
1594                 return err;
1595
1596         priv->channels.params.num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;
1597
1598         mlx5e_build_rep_params(netdev);
1599         mlx5e_build_rep_netdev(netdev);
1600
1601         mlx5e_timestamp_init(priv);
1602
1603         return 0;
1604 }
1605
1606 static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
1607 {
1608         mlx5e_netdev_cleanup(priv->netdev, priv);
1609 }
1610
1611 static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
1612 {
1613         struct ttc_params ttc_params = {};
1614         int tt, err;
1615
1616         priv->fs.ns = mlx5_get_flow_namespace(priv->mdev,
1617                                               MLX5_FLOW_NAMESPACE_KERNEL);
1618
1619         /* The inner_ttc in the ttc params is intentionally not set */
1620         ttc_params.any_tt_tirn = priv->direct_tir[0].tirn;
1621         mlx5e_set_ttc_ft_params(&ttc_params);
1622         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
1623                 ttc_params.indir_tirn[tt] = priv->indir_tir[tt].tirn;
1624
1625         err = mlx5e_create_ttc_table(priv, &ttc_params, &priv->fs.ttc);
1626         if (err) {
1627                 netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n", err);
1628                 return err;
1629         }
1630         return 0;
1631 }
1632
1633 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
1634 {
1635         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1636         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1637         struct mlx5_eswitch_rep *rep = rpriv->rep;
1638         struct mlx5_flow_handle *flow_rule;
1639         struct mlx5_flow_destination dest;
1640
1641         dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1642         dest.tir_num = priv->direct_tir[0].tirn;
1643         flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
1644                                                       rep->vport,
1645                                                       &dest);
1646         if (IS_ERR(flow_rule))
1647                 return PTR_ERR(flow_rule);
1648         rpriv->vport_rx_rule = flow_rule;
1649         return 0;
1650 }
1651
1652 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
1653 {
1654         struct mlx5_core_dev *mdev = priv->mdev;
1655         int err;
1656
1657         mlx5e_init_l2_addr(priv);
1658
1659         err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
1660         if (err) {
1661                 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
1662                 return err;
1663         }
1664
1665         err = mlx5e_create_indirect_rqt(priv);
1666         if (err)
1667                 goto err_close_drop_rq;
1668
1669         err = mlx5e_create_direct_rqts(priv, priv->direct_tir);
1670         if (err)
1671                 goto err_destroy_indirect_rqts;
1672
1673         err = mlx5e_create_indirect_tirs(priv, false);
1674         if (err)
1675                 goto err_destroy_direct_rqts;
1676
1677         err = mlx5e_create_direct_tirs(priv, priv->direct_tir);
1678         if (err)
1679                 goto err_destroy_indirect_tirs;
1680
1681         err = mlx5e_create_rep_ttc_table(priv);
1682         if (err)
1683                 goto err_destroy_direct_tirs;
1684
1685         err = mlx5e_create_rep_vport_rx_rule(priv);
1686         if (err)
1687                 goto err_destroy_ttc_table;
1688
1689         return 0;
1690
1691 err_destroy_ttc_table:
1692         mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1693 err_destroy_direct_tirs:
1694         mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
1695 err_destroy_indirect_tirs:
1696         mlx5e_destroy_indirect_tirs(priv, false);
1697 err_destroy_direct_rqts:
1698         mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
1699 err_destroy_indirect_rqts:
1700         mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1701 err_close_drop_rq:
1702         mlx5e_close_drop_rq(&priv->drop_rq);
1703         return err;
1704 }
1705
1706 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
1707 {
1708         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1709
1710         mlx5_del_flow_rules(rpriv->vport_rx_rule);
1711         mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1712         mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
1713         mlx5e_destroy_indirect_tirs(priv, false);
1714         mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
1715         mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1716         mlx5e_close_drop_rq(&priv->drop_rq);
1717 }
1718
1719 static int mlx5e_init_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
1720 {
1721         struct mlx5_rep_uplink_priv *uplink_priv;
1722         struct net_device *netdev;
1723         struct mlx5e_priv *priv;
1724         int err;
1725
1726         netdev = rpriv->netdev;
1727         priv = netdev_priv(netdev);
1728         uplink_priv = &rpriv->uplink_priv;
1729
1730         mutex_init(&uplink_priv->unready_flows_lock);
1731         INIT_LIST_HEAD(&uplink_priv->unready_flows);
1732
1733         /* init shared tc flow table */
1734         err = mlx5e_tc_esw_init(&uplink_priv->tc_ht);
1735         if (err)
1736                 return err;
1737
1738         mlx5_init_port_tun_entropy(&uplink_priv->tun_entropy, priv->mdev);
1739
1740         /* init indirect block notifications */
1741         INIT_LIST_HEAD(&uplink_priv->tc_indr_block_priv_list);
1742         uplink_priv->netdevice_nb.notifier_call = mlx5e_nic_rep_netdevice_event;
1743         err = register_netdevice_notifier(&uplink_priv->netdevice_nb);
1744         if (err) {
1745                 mlx5_core_err(priv->mdev, "Failed to register netdev notifier\n");
1746                 goto tc_esw_cleanup;
1747         }
1748
1749         return 0;
1750
1751 tc_esw_cleanup:
1752         mlx5e_tc_esw_cleanup(&uplink_priv->tc_ht);
1753         return err;
1754 }
1755
1756 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1757 {
1758         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1759         int err;
1760
1761         err = mlx5e_create_tises(priv);
1762         if (err) {
1763                 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1764                 return err;
1765         }
1766
1767         if (rpriv->rep->vport == MLX5_VPORT_UPLINK) {
1768                 err = mlx5e_init_uplink_rep_tx(rpriv);
1769                 if (err)
1770                         goto destroy_tises;
1771         }
1772
1773         return 0;
1774
1775 destroy_tises:
1776         mlx5e_destroy_tises(priv);
1777         return err;
1778 }
1779
1780 static void mlx5e_cleanup_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
1781 {
1782         /* clean indirect TC block notifications */
1783         unregister_netdevice_notifier(&rpriv->uplink_priv.netdevice_nb);
1784         mlx5e_rep_indr_clean_block_privs(rpriv);
1785
1786         /* delete shared tc flow table */
1787         mlx5e_tc_esw_cleanup(&rpriv->uplink_priv.tc_ht);
1788         mutex_destroy(&rpriv->uplink_priv.unready_flows_lock);
1789 }
1790
1791 static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
1792 {
1793         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1794
1795         mlx5e_destroy_tises(priv);
1796
1797         if (rpriv->rep->vport == MLX5_VPORT_UPLINK)
1798                 mlx5e_cleanup_uplink_rep_tx(rpriv);
1799 }
1800
1801 static void mlx5e_rep_enable(struct mlx5e_priv *priv)
1802 {
1803         mlx5e_set_netdev_mtu_boundaries(priv);
1804 }
1805
1806 static int mlx5e_update_rep_rx(struct mlx5e_priv *priv)
1807 {
1808         return 0;
1809 }
1810
1811 static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
1812 {
1813         struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
1814
1815         if (event == MLX5_EVENT_TYPE_PORT_CHANGE) {
1816                 struct mlx5_eqe *eqe = data;
1817
1818                 switch (eqe->sub_type) {
1819                 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
1820                 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
1821                         queue_work(priv->wq, &priv->update_carrier_work);
1822                         break;
1823                 default:
1824                         return NOTIFY_DONE;
1825                 }
1826
1827                 return NOTIFY_OK;
1828         }
1829
1830         if (event == MLX5_DEV_EVENT_PORT_AFFINITY) {
1831                 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1832
1833                 queue_work(priv->wq, &rpriv->uplink_priv.reoffload_flows_work);
1834
1835                 return NOTIFY_OK;
1836         }
1837
1838         return NOTIFY_DONE;
1839 }
1840
1841 static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
1842 {
1843         struct net_device *netdev = priv->netdev;
1844         struct mlx5_core_dev *mdev = priv->mdev;
1845         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1846         u16 max_mtu;
1847
1848         netdev->min_mtu = ETH_MIN_MTU;
1849         mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
1850         netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1851         mlx5e_set_dev_port_mtu(priv);
1852
1853         INIT_WORK(&rpriv->uplink_priv.reoffload_flows_work,
1854                   mlx5e_tc_reoffload_flows_work);
1855
1856         mlx5_lag_add(mdev, netdev);
1857         priv->events_nb.notifier_call = uplink_rep_async_event;
1858         mlx5_notifier_register(mdev, &priv->events_nb);
1859 #ifdef CONFIG_MLX5_CORE_EN_DCB
1860         mlx5e_dcbnl_initialize(priv);
1861         mlx5e_dcbnl_init_app(priv);
1862 #endif
1863 }
1864
1865 static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
1866 {
1867         struct mlx5_core_dev *mdev = priv->mdev;
1868         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1869
1870 #ifdef CONFIG_MLX5_CORE_EN_DCB
1871         mlx5e_dcbnl_delete_app(priv);
1872 #endif
1873         mlx5_notifier_unregister(mdev, &priv->events_nb);
1874         cancel_work_sync(&rpriv->uplink_priv.reoffload_flows_work);
1875         mlx5_lag_remove(mdev);
1876 }
1877
1878 static MLX5E_DEFINE_STATS_GRP(sw_rep, 0);
1879 static MLX5E_DEFINE_STATS_GRP(vport_rep, MLX5E_NDO_UPDATE_STATS);
1880
1881 /* The stats groups order is opposite to the update_stats() order calls */
1882 static mlx5e_stats_grp_t mlx5e_rep_stats_grps[] = {
1883         &MLX5E_STATS_GRP(sw_rep),
1884         &MLX5E_STATS_GRP(vport_rep),
1885 };
1886
1887 static unsigned int mlx5e_rep_stats_grps_num(struct mlx5e_priv *priv)
1888 {
1889         return ARRAY_SIZE(mlx5e_rep_stats_grps);
1890 }
1891
1892 /* The stats groups order is opposite to the update_stats() order calls */
1893 static mlx5e_stats_grp_t mlx5e_ul_rep_stats_grps[] = {
1894         &MLX5E_STATS_GRP(sw_rep),
1895         &MLX5E_STATS_GRP(vport_rep),
1896 };
1897
1898 static unsigned int mlx5e_ul_rep_stats_grps_num(struct mlx5e_priv *priv)
1899 {
1900         return ARRAY_SIZE(mlx5e_ul_rep_stats_grps);
1901 }
1902
1903 static const struct mlx5e_profile mlx5e_rep_profile = {
1904         .init                   = mlx5e_init_rep,
1905         .cleanup                = mlx5e_cleanup_rep,
1906         .init_rx                = mlx5e_init_rep_rx,
1907         .cleanup_rx             = mlx5e_cleanup_rep_rx,
1908         .init_tx                = mlx5e_init_rep_tx,
1909         .cleanup_tx             = mlx5e_cleanup_rep_tx,
1910         .enable                 = mlx5e_rep_enable,
1911         .update_rx              = mlx5e_update_rep_rx,
1912         .update_stats           = mlx5e_update_ndo_stats,
1913         .rx_handlers.handle_rx_cqe       = mlx5e_handle_rx_cqe_rep,
1914         .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1915         .max_tc                 = 1,
1916         .rq_groups              = MLX5E_NUM_RQ_GROUPS(REGULAR),
1917         .stats_grps             = mlx5e_rep_stats_grps,
1918         .stats_grps_num         = mlx5e_rep_stats_grps_num,
1919 };
1920
1921 static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
1922         .init                   = mlx5e_init_rep,
1923         .cleanup                = mlx5e_cleanup_rep,
1924         .init_rx                = mlx5e_init_rep_rx,
1925         .cleanup_rx             = mlx5e_cleanup_rep_rx,
1926         .init_tx                = mlx5e_init_rep_tx,
1927         .cleanup_tx             = mlx5e_cleanup_rep_tx,
1928         .enable                 = mlx5e_uplink_rep_enable,
1929         .disable                = mlx5e_uplink_rep_disable,
1930         .update_rx              = mlx5e_update_rep_rx,
1931         .update_stats           = mlx5e_update_ndo_stats,
1932         .update_carrier         = mlx5e_update_carrier,
1933         .rx_handlers.handle_rx_cqe       = mlx5e_handle_rx_cqe_rep,
1934         .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1935         .max_tc                 = MLX5E_MAX_NUM_TC,
1936         .rq_groups              = MLX5E_NUM_RQ_GROUPS(REGULAR),
1937         .stats_grps             = mlx5e_ul_rep_stats_grps,
1938         .stats_grps_num         = mlx5e_ul_rep_stats_grps_num,
1939 };
1940
1941 static bool
1942 is_devlink_port_supported(const struct mlx5_core_dev *dev,
1943                           const struct mlx5e_rep_priv *rpriv)
1944 {
1945         return rpriv->rep->vport == MLX5_VPORT_UPLINK ||
1946                rpriv->rep->vport == MLX5_VPORT_PF ||
1947                mlx5_eswitch_is_vf_vport(dev->priv.eswitch, rpriv->rep->vport);
1948 }
1949
1950 static unsigned int
1951 vport_to_devlink_port_index(const struct mlx5_core_dev *dev, u16 vport_num)
1952 {
1953         return (MLX5_CAP_GEN(dev, vhca_id) << 16) | vport_num;
1954 }
1955
1956 static int register_devlink_port(struct mlx5_core_dev *dev,
1957                                  struct mlx5e_rep_priv *rpriv)
1958 {
1959         struct devlink *devlink = priv_to_devlink(dev);
1960         struct mlx5_eswitch_rep *rep = rpriv->rep;
1961         struct netdev_phys_item_id ppid = {};
1962         unsigned int dl_port_index = 0;
1963
1964         if (!is_devlink_port_supported(dev, rpriv))
1965                 return 0;
1966
1967         mlx5e_rep_get_port_parent_id(rpriv->netdev, &ppid);
1968
1969         if (rep->vport == MLX5_VPORT_UPLINK) {
1970                 devlink_port_attrs_set(&rpriv->dl_port,
1971                                        DEVLINK_PORT_FLAVOUR_PHYSICAL,
1972                                        PCI_FUNC(dev->pdev->devfn), false, 0,
1973                                        &ppid.id[0], ppid.id_len);
1974                 dl_port_index = vport_to_devlink_port_index(dev, rep->vport);
1975         } else if (rep->vport == MLX5_VPORT_PF) {
1976                 devlink_port_attrs_pci_pf_set(&rpriv->dl_port,
1977                                               &ppid.id[0], ppid.id_len,
1978                                               dev->pdev->devfn);
1979                 dl_port_index = rep->vport;
1980         } else if (mlx5_eswitch_is_vf_vport(dev->priv.eswitch,
1981                                             rpriv->rep->vport)) {
1982                 devlink_port_attrs_pci_vf_set(&rpriv->dl_port,
1983                                               &ppid.id[0], ppid.id_len,
1984                                               dev->pdev->devfn,
1985                                               rep->vport - 1);
1986                 dl_port_index = vport_to_devlink_port_index(dev, rep->vport);
1987         }
1988
1989         return devlink_port_register(devlink, &rpriv->dl_port, dl_port_index);
1990 }
1991
1992 static void unregister_devlink_port(struct mlx5_core_dev *dev,
1993                                     struct mlx5e_rep_priv *rpriv)
1994 {
1995         if (is_devlink_port_supported(dev, rpriv))
1996                 devlink_port_unregister(&rpriv->dl_port);
1997 }
1998
1999 /* e-Switch vport representors */
2000 static int
2001 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
2002 {
2003         const struct mlx5e_profile *profile;
2004         struct mlx5e_rep_priv *rpriv;
2005         struct net_device *netdev;
2006         int nch, err;
2007
2008         rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
2009         if (!rpriv)
2010                 return -ENOMEM;
2011
2012         /* rpriv->rep to be looked up when profile->init() is called */
2013         rpriv->rep = rep;
2014
2015         nch = mlx5e_get_max_num_channels(dev);
2016         profile = (rep->vport == MLX5_VPORT_UPLINK) ?
2017                   &mlx5e_uplink_rep_profile : &mlx5e_rep_profile;
2018         netdev = mlx5e_create_netdev(dev, profile, nch, rpriv);
2019         if (!netdev) {
2020                 pr_warn("Failed to create representor netdev for vport %d\n",
2021                         rep->vport);
2022                 kfree(rpriv);
2023                 return -EINVAL;
2024         }
2025
2026         dev_net_set(netdev, mlx5_core_net(dev));
2027         rpriv->netdev = netdev;
2028         rep->rep_data[REP_ETH].priv = rpriv;
2029         INIT_LIST_HEAD(&rpriv->vport_sqs_list);
2030
2031         if (rep->vport == MLX5_VPORT_UPLINK) {
2032                 err = mlx5e_create_mdev_resources(dev);
2033                 if (err)
2034                         goto err_destroy_netdev;
2035         }
2036
2037         err = mlx5e_attach_netdev(netdev_priv(netdev));
2038         if (err) {
2039                 pr_warn("Failed to attach representor netdev for vport %d\n",
2040                         rep->vport);
2041                 goto err_destroy_mdev_resources;
2042         }
2043
2044         err = mlx5e_rep_neigh_init(rpriv);
2045         if (err) {
2046                 pr_warn("Failed to initialized neighbours handling for vport %d\n",
2047                         rep->vport);
2048                 goto err_detach_netdev;
2049         }
2050
2051         err = register_devlink_port(dev, rpriv);
2052         if (err) {
2053                 esw_warn(dev, "Failed to register devlink port %d\n",
2054                          rep->vport);
2055                 goto err_neigh_cleanup;
2056         }
2057
2058         err = register_netdev(netdev);
2059         if (err) {
2060                 pr_warn("Failed to register representor netdev for vport %d\n",
2061                         rep->vport);
2062                 goto err_devlink_cleanup;
2063         }
2064
2065         if (is_devlink_port_supported(dev, rpriv))
2066                 devlink_port_type_eth_set(&rpriv->dl_port, netdev);
2067         return 0;
2068
2069 err_devlink_cleanup:
2070         unregister_devlink_port(dev, rpriv);
2071
2072 err_neigh_cleanup:
2073         mlx5e_rep_neigh_cleanup(rpriv);
2074
2075 err_detach_netdev:
2076         mlx5e_detach_netdev(netdev_priv(netdev));
2077
2078 err_destroy_mdev_resources:
2079         if (rep->vport == MLX5_VPORT_UPLINK)
2080                 mlx5e_destroy_mdev_resources(dev);
2081
2082 err_destroy_netdev:
2083         mlx5e_destroy_netdev(netdev_priv(netdev));
2084         kfree(rpriv);
2085         return err;
2086 }
2087
2088 static void
2089 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
2090 {
2091         struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
2092         struct net_device *netdev = rpriv->netdev;
2093         struct mlx5e_priv *priv = netdev_priv(netdev);
2094         struct mlx5_core_dev *dev = priv->mdev;
2095         void *ppriv = priv->ppriv;
2096
2097         if (is_devlink_port_supported(dev, rpriv))
2098                 devlink_port_type_clear(&rpriv->dl_port);
2099         unregister_netdev(netdev);
2100         unregister_devlink_port(dev, rpriv);
2101         mlx5e_rep_neigh_cleanup(rpriv);
2102         mlx5e_detach_netdev(priv);
2103         if (rep->vport == MLX5_VPORT_UPLINK)
2104                 mlx5e_destroy_mdev_resources(priv->mdev);
2105         mlx5e_destroy_netdev(priv);
2106         kfree(ppriv); /* mlx5e_rep_priv */
2107 }
2108
2109 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
2110 {
2111         struct mlx5e_rep_priv *rpriv;
2112
2113         rpriv = mlx5e_rep_to_rep_priv(rep);
2114
2115         return rpriv->netdev;
2116 }
2117
2118 static const struct mlx5_eswitch_rep_ops rep_ops = {
2119         .load = mlx5e_vport_rep_load,
2120         .unload = mlx5e_vport_rep_unload,
2121         .get_proto_dev = mlx5e_vport_rep_get_proto_dev
2122 };
2123
2124 void mlx5e_rep_register_vport_reps(struct mlx5_core_dev *mdev)
2125 {
2126         struct mlx5_eswitch *esw = mdev->priv.eswitch;
2127
2128         mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_ETH);
2129 }
2130
2131 void mlx5e_rep_unregister_vport_reps(struct mlx5_core_dev *mdev)
2132 {
2133         struct mlx5_eswitch *esw = mdev->priv.eswitch;
2134
2135         mlx5_eswitch_unregister_vport_reps(esw, REP_ETH);
2136 }