Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[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/devlink.h>
39 #include <net/ipv6_stubs.h>
40
41 #include "eswitch.h"
42 #include "en.h"
43 #include "en_rep.h"
44 #include "en/txrx.h"
45 #include "en_tc.h"
46 #include "en/rep/tc.h"
47 #include "en/rep/neigh.h"
48 #include "fs_core.h"
49 #include "lib/mlx5.h"
50 #define CREATE_TRACE_POINTS
51 #include "diag/en_rep_tracepoint.h"
52
53 #define MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE \
54         max(0x7, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)
55 #define MLX5E_REP_PARAMS_DEF_NUM_CHANNELS 1
56
57 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
58
59 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
60                                   struct ethtool_drvinfo *drvinfo)
61 {
62         struct mlx5e_priv *priv = netdev_priv(dev);
63         struct mlx5_core_dev *mdev = priv->mdev;
64
65         strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
66                 sizeof(drvinfo->driver));
67         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
68                  "%d.%d.%04d (%.16s)",
69                  fw_rev_maj(mdev), fw_rev_min(mdev),
70                  fw_rev_sub(mdev), mdev->board_id);
71 }
72
73 static void mlx5e_uplink_rep_get_drvinfo(struct net_device *dev,
74                                          struct ethtool_drvinfo *drvinfo)
75 {
76         struct mlx5e_priv *priv = netdev_priv(dev);
77
78         mlx5e_rep_get_drvinfo(dev, drvinfo);
79         strlcpy(drvinfo->bus_info, pci_name(priv->mdev->pdev),
80                 sizeof(drvinfo->bus_info));
81 }
82
83 static const struct counter_desc sw_rep_stats_desc[] = {
84         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
85         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
86         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
87         { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
88 };
89
90 struct vport_stats {
91         u64 vport_rx_packets;
92         u64 vport_tx_packets;
93         u64 vport_rx_bytes;
94         u64 vport_tx_bytes;
95 };
96
97 static const struct counter_desc vport_rep_stats_desc[] = {
98         { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_packets) },
99         { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_bytes) },
100         { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_packets) },
101         { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_bytes) },
102 };
103
104 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
105 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
106
107 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(sw_rep)
108 {
109         return NUM_VPORT_REP_SW_COUNTERS;
110 }
111
112 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(sw_rep)
113 {
114         int i;
115
116         for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
117                 strcpy(data + (idx++) * ETH_GSTRING_LEN,
118                        sw_rep_stats_desc[i].format);
119         return idx;
120 }
121
122 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(sw_rep)
123 {
124         int i;
125
126         for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
127                 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
128                                                    sw_rep_stats_desc, i);
129         return idx;
130 }
131
132 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(sw_rep)
133 {
134         struct mlx5e_sw_stats *s = &priv->stats.sw;
135         struct rtnl_link_stats64 stats64 = {};
136
137         memset(s, 0, sizeof(*s));
138         mlx5e_fold_sw_stats64(priv, &stats64);
139
140         s->rx_packets = stats64.rx_packets;
141         s->rx_bytes   = stats64.rx_bytes;
142         s->tx_packets = stats64.tx_packets;
143         s->tx_bytes   = stats64.tx_bytes;
144         s->tx_queue_dropped = stats64.tx_dropped;
145 }
146
147 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(vport_rep)
148 {
149         return NUM_VPORT_REP_HW_COUNTERS;
150 }
151
152 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(vport_rep)
153 {
154         int i;
155
156         for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
157                 strcpy(data + (idx++) * ETH_GSTRING_LEN, vport_rep_stats_desc[i].format);
158         return idx;
159 }
160
161 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(vport_rep)
162 {
163         int i;
164
165         for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
166                 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
167                                                    vport_rep_stats_desc, i);
168         return idx;
169 }
170
171 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(vport_rep)
172 {
173         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
174         struct mlx5e_rep_priv *rpriv = priv->ppriv;
175         struct mlx5_eswitch_rep *rep = rpriv->rep;
176         struct rtnl_link_stats64 *vport_stats;
177         struct ifla_vf_stats vf_stats;
178         int err;
179
180         err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
181         if (err) {
182                 netdev_warn(priv->netdev, "vport %d error %d reading stats\n",
183                             rep->vport, err);
184                 return;
185         }
186
187         vport_stats = &priv->stats.vf_vport;
188         /* flip tx/rx as we are reporting the counters for the switch vport */
189         vport_stats->rx_packets = vf_stats.tx_packets;
190         vport_stats->rx_bytes   = vf_stats.tx_bytes;
191         vport_stats->tx_packets = vf_stats.rx_packets;
192         vport_stats->tx_bytes   = vf_stats.rx_bytes;
193 }
194
195 static void mlx5e_rep_get_strings(struct net_device *dev,
196                                   u32 stringset, uint8_t *data)
197 {
198         struct mlx5e_priv *priv = netdev_priv(dev);
199
200         switch (stringset) {
201         case ETH_SS_STATS:
202                 mlx5e_stats_fill_strings(priv, data);
203                 break;
204         }
205 }
206
207 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
208                                         struct ethtool_stats *stats, u64 *data)
209 {
210         struct mlx5e_priv *priv = netdev_priv(dev);
211
212         mlx5e_ethtool_get_ethtool_stats(priv, stats, data);
213 }
214
215 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
216 {
217         struct mlx5e_priv *priv = netdev_priv(dev);
218
219         switch (sset) {
220         case ETH_SS_STATS:
221                 return mlx5e_stats_total_num(priv);
222         default:
223                 return -EOPNOTSUPP;
224         }
225 }
226
227 static void mlx5e_rep_get_ringparam(struct net_device *dev,
228                                 struct ethtool_ringparam *param)
229 {
230         struct mlx5e_priv *priv = netdev_priv(dev);
231
232         mlx5e_ethtool_get_ringparam(priv, param);
233 }
234
235 static int mlx5e_rep_set_ringparam(struct net_device *dev,
236                                struct ethtool_ringparam *param)
237 {
238         struct mlx5e_priv *priv = netdev_priv(dev);
239
240         return mlx5e_ethtool_set_ringparam(priv, param);
241 }
242
243 static void mlx5e_rep_get_channels(struct net_device *dev,
244                                    struct ethtool_channels *ch)
245 {
246         struct mlx5e_priv *priv = netdev_priv(dev);
247
248         mlx5e_ethtool_get_channels(priv, ch);
249 }
250
251 static int mlx5e_rep_set_channels(struct net_device *dev,
252                                   struct ethtool_channels *ch)
253 {
254         struct mlx5e_priv *priv = netdev_priv(dev);
255
256         return mlx5e_ethtool_set_channels(priv, ch);
257 }
258
259 static int mlx5e_rep_get_coalesce(struct net_device *netdev,
260                                   struct ethtool_coalesce *coal)
261 {
262         struct mlx5e_priv *priv = netdev_priv(netdev);
263
264         return mlx5e_ethtool_get_coalesce(priv, coal);
265 }
266
267 static int mlx5e_rep_set_coalesce(struct net_device *netdev,
268                                   struct ethtool_coalesce *coal)
269 {
270         struct mlx5e_priv *priv = netdev_priv(netdev);
271
272         return mlx5e_ethtool_set_coalesce(priv, coal);
273 }
274
275 static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
276 {
277         struct mlx5e_priv *priv = netdev_priv(netdev);
278
279         return mlx5e_ethtool_get_rxfh_key_size(priv);
280 }
281
282 static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
283 {
284         struct mlx5e_priv *priv = netdev_priv(netdev);
285
286         return mlx5e_ethtool_get_rxfh_indir_size(priv);
287 }
288
289 static void mlx5e_uplink_rep_get_pause_stats(struct net_device *netdev,
290                                              struct ethtool_pause_stats *stats)
291 {
292         struct mlx5e_priv *priv = netdev_priv(netdev);
293
294         mlx5e_stats_pause_get(priv, stats);
295 }
296
297 static void mlx5e_uplink_rep_get_pauseparam(struct net_device *netdev,
298                                             struct ethtool_pauseparam *pauseparam)
299 {
300         struct mlx5e_priv *priv = netdev_priv(netdev);
301
302         mlx5e_ethtool_get_pauseparam(priv, pauseparam);
303 }
304
305 static int mlx5e_uplink_rep_set_pauseparam(struct net_device *netdev,
306                                            struct ethtool_pauseparam *pauseparam)
307 {
308         struct mlx5e_priv *priv = netdev_priv(netdev);
309
310         return mlx5e_ethtool_set_pauseparam(priv, pauseparam);
311 }
312
313 static int mlx5e_uplink_rep_get_link_ksettings(struct net_device *netdev,
314                                                struct ethtool_link_ksettings *link_ksettings)
315 {
316         struct mlx5e_priv *priv = netdev_priv(netdev);
317
318         return mlx5e_ethtool_get_link_ksettings(priv, link_ksettings);
319 }
320
321 static int mlx5e_uplink_rep_set_link_ksettings(struct net_device *netdev,
322                                                const struct ethtool_link_ksettings *link_ksettings)
323 {
324         struct mlx5e_priv *priv = netdev_priv(netdev);
325
326         return mlx5e_ethtool_set_link_ksettings(priv, link_ksettings);
327 }
328
329 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
330         .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
331                                      ETHTOOL_COALESCE_MAX_FRAMES |
332                                      ETHTOOL_COALESCE_USE_ADAPTIVE,
333         .get_drvinfo       = mlx5e_rep_get_drvinfo,
334         .get_link          = ethtool_op_get_link,
335         .get_strings       = mlx5e_rep_get_strings,
336         .get_sset_count    = mlx5e_rep_get_sset_count,
337         .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
338         .get_ringparam     = mlx5e_rep_get_ringparam,
339         .set_ringparam     = mlx5e_rep_set_ringparam,
340         .get_channels      = mlx5e_rep_get_channels,
341         .set_channels      = mlx5e_rep_set_channels,
342         .get_coalesce      = mlx5e_rep_get_coalesce,
343         .set_coalesce      = mlx5e_rep_set_coalesce,
344         .get_rxfh_key_size   = mlx5e_rep_get_rxfh_key_size,
345         .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
346 };
347
348 static const struct ethtool_ops mlx5e_uplink_rep_ethtool_ops = {
349         .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
350                                      ETHTOOL_COALESCE_MAX_FRAMES |
351                                      ETHTOOL_COALESCE_USE_ADAPTIVE,
352         .get_drvinfo       = mlx5e_uplink_rep_get_drvinfo,
353         .get_link          = ethtool_op_get_link,
354         .get_strings       = mlx5e_rep_get_strings,
355         .get_sset_count    = mlx5e_rep_get_sset_count,
356         .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
357         .get_ringparam     = mlx5e_rep_get_ringparam,
358         .set_ringparam     = mlx5e_rep_set_ringparam,
359         .get_channels      = mlx5e_rep_get_channels,
360         .set_channels      = mlx5e_rep_set_channels,
361         .get_coalesce      = mlx5e_rep_get_coalesce,
362         .set_coalesce      = mlx5e_rep_set_coalesce,
363         .get_link_ksettings = mlx5e_uplink_rep_get_link_ksettings,
364         .set_link_ksettings = mlx5e_uplink_rep_set_link_ksettings,
365         .get_rxfh_key_size   = mlx5e_rep_get_rxfh_key_size,
366         .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
367         .get_rxfh          = mlx5e_get_rxfh,
368         .set_rxfh          = mlx5e_set_rxfh,
369         .get_rxnfc         = mlx5e_get_rxnfc,
370         .set_rxnfc         = mlx5e_set_rxnfc,
371         .get_pause_stats   = mlx5e_uplink_rep_get_pause_stats,
372         .get_pauseparam    = mlx5e_uplink_rep_get_pauseparam,
373         .set_pauseparam    = mlx5e_uplink_rep_set_pauseparam,
374 };
375
376 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
377                                  struct mlx5_eswitch_rep *rep)
378 {
379         struct mlx5e_rep_sq *rep_sq, *tmp;
380         struct mlx5e_rep_priv *rpriv;
381
382         if (esw->mode != MLX5_ESWITCH_OFFLOADS)
383                 return;
384
385         rpriv = mlx5e_rep_to_rep_priv(rep);
386         list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
387                 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
388                 list_del(&rep_sq->list);
389                 kfree(rep_sq);
390         }
391 }
392
393 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
394                                  struct mlx5_eswitch_rep *rep,
395                                  u32 *sqns_array, int sqns_num)
396 {
397         struct mlx5_flow_handle *flow_rule;
398         struct mlx5e_rep_priv *rpriv;
399         struct mlx5e_rep_sq *rep_sq;
400         int err;
401         int i;
402
403         if (esw->mode != MLX5_ESWITCH_OFFLOADS)
404                 return 0;
405
406         rpriv = mlx5e_rep_to_rep_priv(rep);
407         for (i = 0; i < sqns_num; i++) {
408                 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
409                 if (!rep_sq) {
410                         err = -ENOMEM;
411                         goto out_err;
412                 }
413
414                 /* Add re-inject rule to the PF/representor sqs */
415                 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
416                                                                 rep->vport,
417                                                                 sqns_array[i]);
418                 if (IS_ERR(flow_rule)) {
419                         err = PTR_ERR(flow_rule);
420                         kfree(rep_sq);
421                         goto out_err;
422                 }
423                 rep_sq->send_to_vport_rule = flow_rule;
424                 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
425         }
426         return 0;
427
428 out_err:
429         mlx5e_sqs2vport_stop(esw, rep);
430         return err;
431 }
432
433 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
434 {
435         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
436         struct mlx5e_rep_priv *rpriv = priv->ppriv;
437         struct mlx5_eswitch_rep *rep = rpriv->rep;
438         struct mlx5e_channel *c;
439         int n, tc, num_sqs = 0;
440         int err = -ENOMEM;
441         u32 *sqs;
442
443         sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(*sqs), GFP_KERNEL);
444         if (!sqs)
445                 goto out;
446
447         for (n = 0; n < priv->channels.num; n++) {
448                 c = priv->channels.c[n];
449                 for (tc = 0; tc < c->num_tc; tc++)
450                         sqs[num_sqs++] = c->sq[tc].sqn;
451         }
452
453         err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
454         kfree(sqs);
455
456 out:
457         if (err)
458                 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
459         return err;
460 }
461
462 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
463 {
464         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
465         struct mlx5e_rep_priv *rpriv = priv->ppriv;
466         struct mlx5_eswitch_rep *rep = rpriv->rep;
467
468         mlx5e_sqs2vport_stop(esw, rep);
469 }
470
471 static int mlx5e_rep_open(struct net_device *dev)
472 {
473         struct mlx5e_priv *priv = netdev_priv(dev);
474         struct mlx5e_rep_priv *rpriv = priv->ppriv;
475         struct mlx5_eswitch_rep *rep = rpriv->rep;
476         int err;
477
478         mutex_lock(&priv->state_lock);
479         err = mlx5e_open_locked(dev);
480         if (err)
481                 goto unlock;
482
483         if (!mlx5_modify_vport_admin_state(priv->mdev,
484                                            MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
485                                            rep->vport, 1,
486                                            MLX5_VPORT_ADMIN_STATE_UP))
487                 netif_carrier_on(dev);
488
489 unlock:
490         mutex_unlock(&priv->state_lock);
491         return err;
492 }
493
494 static int mlx5e_rep_close(struct net_device *dev)
495 {
496         struct mlx5e_priv *priv = netdev_priv(dev);
497         struct mlx5e_rep_priv *rpriv = priv->ppriv;
498         struct mlx5_eswitch_rep *rep = rpriv->rep;
499         int ret;
500
501         mutex_lock(&priv->state_lock);
502         mlx5_modify_vport_admin_state(priv->mdev,
503                                       MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
504                                       rep->vport, 1,
505                                       MLX5_VPORT_ADMIN_STATE_DOWN);
506         ret = mlx5e_close_locked(dev);
507         mutex_unlock(&priv->state_lock);
508         return ret;
509 }
510
511 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
512 {
513         struct mlx5e_rep_priv *rpriv = priv->ppriv;
514         struct mlx5_eswitch_rep *rep;
515
516         if (!MLX5_ESWITCH_MANAGER(priv->mdev))
517                 return false;
518
519         if (!rpriv) /* non vport rep mlx5e instances don't use this field */
520                 return false;
521
522         rep = rpriv->rep;
523         return (rep->vport == MLX5_VPORT_UPLINK);
524 }
525
526 static bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
527 {
528         switch (attr_id) {
529         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
530                         return true;
531         }
532
533         return false;
534 }
535
536 static int
537 mlx5e_get_sw_stats64(const struct net_device *dev,
538                      struct rtnl_link_stats64 *stats)
539 {
540         struct mlx5e_priv *priv = netdev_priv(dev);
541
542         mlx5e_fold_sw_stats64(priv, stats);
543         return 0;
544 }
545
546 static int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
547                                        void *sp)
548 {
549         switch (attr_id) {
550         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
551                 return mlx5e_get_sw_stats64(dev, sp);
552         }
553
554         return -EINVAL;
555 }
556
557 static void
558 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
559 {
560         struct mlx5e_priv *priv = netdev_priv(dev);
561
562         /* update HW stats in background for next time */
563         mlx5e_queue_update_stats(priv);
564         memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
565 }
566
567 static int mlx5e_rep_change_mtu(struct net_device *netdev, int new_mtu)
568 {
569         return mlx5e_change_mtu(netdev, new_mtu, NULL);
570 }
571
572 static int mlx5e_uplink_rep_change_mtu(struct net_device *netdev, int new_mtu)
573 {
574         return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu_ctx);
575 }
576
577 static int mlx5e_uplink_rep_set_mac(struct net_device *netdev, void *addr)
578 {
579         struct sockaddr *saddr = addr;
580
581         if (!is_valid_ether_addr(saddr->sa_data))
582                 return -EADDRNOTAVAIL;
583
584         ether_addr_copy(netdev->dev_addr, saddr->sa_data);
585         return 0;
586 }
587
588 static int mlx5e_uplink_rep_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
589                                         __be16 vlan_proto)
590 {
591         netdev_warn_once(dev, "legacy vf vlan setting isn't supported in switchdev mode\n");
592
593         if (vlan != 0)
594                 return -EOPNOTSUPP;
595
596         /* allow setting 0-vid for compatibility with libvirt */
597         return 0;
598 }
599
600 static struct devlink_port *mlx5e_rep_get_devlink_port(struct net_device *netdev)
601 {
602         struct mlx5e_priv *priv = netdev_priv(netdev);
603         struct mlx5e_rep_priv *rpriv = priv->ppriv;
604         struct mlx5_core_dev *dev = priv->mdev;
605
606         return mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
607 }
608
609 static int mlx5e_rep_change_carrier(struct net_device *dev, bool new_carrier)
610 {
611         struct mlx5e_priv *priv = netdev_priv(dev);
612         struct mlx5e_rep_priv *rpriv = priv->ppriv;
613         struct mlx5_eswitch_rep *rep = rpriv->rep;
614         int err;
615
616         if (new_carrier) {
617                 err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
618                                                     rep->vport, 1, MLX5_VPORT_ADMIN_STATE_UP);
619                 if (err)
620                         return err;
621                 netif_carrier_on(dev);
622         } else {
623                 err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
624                                                     rep->vport, 1, MLX5_VPORT_ADMIN_STATE_DOWN);
625                 if (err)
626                         return err;
627                 netif_carrier_off(dev);
628         }
629         return 0;
630 }
631
632 static const struct net_device_ops mlx5e_netdev_ops_rep = {
633         .ndo_open                = mlx5e_rep_open,
634         .ndo_stop                = mlx5e_rep_close,
635         .ndo_start_xmit          = mlx5e_xmit,
636         .ndo_setup_tc            = mlx5e_rep_setup_tc,
637         .ndo_get_devlink_port    = mlx5e_rep_get_devlink_port,
638         .ndo_get_stats64         = mlx5e_rep_get_stats,
639         .ndo_has_offload_stats   = mlx5e_rep_has_offload_stats,
640         .ndo_get_offload_stats   = mlx5e_rep_get_offload_stats,
641         .ndo_change_mtu          = mlx5e_rep_change_mtu,
642         .ndo_change_carrier      = mlx5e_rep_change_carrier,
643 };
644
645 static const struct net_device_ops mlx5e_netdev_ops_uplink_rep = {
646         .ndo_open                = mlx5e_open,
647         .ndo_stop                = mlx5e_close,
648         .ndo_start_xmit          = mlx5e_xmit,
649         .ndo_set_mac_address     = mlx5e_uplink_rep_set_mac,
650         .ndo_setup_tc            = mlx5e_rep_setup_tc,
651         .ndo_get_devlink_port    = mlx5e_rep_get_devlink_port,
652         .ndo_get_stats64         = mlx5e_get_stats,
653         .ndo_has_offload_stats   = mlx5e_rep_has_offload_stats,
654         .ndo_get_offload_stats   = mlx5e_rep_get_offload_stats,
655         .ndo_change_mtu          = mlx5e_uplink_rep_change_mtu,
656         .ndo_features_check      = mlx5e_features_check,
657         .ndo_set_vf_mac          = mlx5e_set_vf_mac,
658         .ndo_set_vf_rate         = mlx5e_set_vf_rate,
659         .ndo_get_vf_config       = mlx5e_get_vf_config,
660         .ndo_get_vf_stats        = mlx5e_get_vf_stats,
661         .ndo_set_vf_vlan         = mlx5e_uplink_rep_set_vf_vlan,
662         .ndo_set_features        = mlx5e_set_features,
663 };
664
665 bool mlx5e_eswitch_uplink_rep(struct net_device *netdev)
666 {
667         return netdev->netdev_ops == &mlx5e_netdev_ops_uplink_rep;
668 }
669
670 bool mlx5e_eswitch_vf_rep(struct net_device *netdev)
671 {
672         return netdev->netdev_ops == &mlx5e_netdev_ops_rep;
673 }
674
675 static void mlx5e_build_rep_params(struct net_device *netdev)
676 {
677         struct mlx5e_priv *priv = netdev_priv(netdev);
678         struct mlx5e_rep_priv *rpriv = priv->ppriv;
679         struct mlx5_eswitch_rep *rep = rpriv->rep;
680         struct mlx5_core_dev *mdev = priv->mdev;
681         struct mlx5e_params *params;
682
683         u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
684                                          MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
685                                          MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
686
687         params = &priv->channels.params;
688         params->hard_mtu    = MLX5E_ETH_HARD_MTU;
689         params->sw_mtu      = netdev->mtu;
690
691         /* SQ */
692         if (rep->vport == MLX5_VPORT_UPLINK)
693                 params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
694         else
695                 params->log_sq_size = MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE;
696
697         /* RQ */
698         mlx5e_build_rq_params(mdev, params);
699
700         /* CQ moderation params */
701         params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
702         mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
703
704         params->num_tc                = 1;
705         params->tunneled_offload_en = false;
706
707         mlx5_query_min_inline(mdev, &params->tx_min_inline_mode);
708
709         /* RSS */
710         mlx5e_build_rss_params(&priv->rss_params, params->num_channels);
711 }
712
713 static void mlx5e_build_rep_netdev(struct net_device *netdev)
714 {
715         struct mlx5e_priv *priv = netdev_priv(netdev);
716         struct mlx5e_rep_priv *rpriv = priv->ppriv;
717         struct mlx5_eswitch_rep *rep = rpriv->rep;
718         struct mlx5_core_dev *mdev = priv->mdev;
719
720         SET_NETDEV_DEV(netdev, mdev->device);
721         if (rep->vport == MLX5_VPORT_UPLINK) {
722                 netdev->netdev_ops = &mlx5e_netdev_ops_uplink_rep;
723                 /* we want a persistent mac for the uplink rep */
724                 mlx5_query_mac_address(mdev, netdev->dev_addr);
725                 netdev->ethtool_ops = &mlx5e_uplink_rep_ethtool_ops;
726                 mlx5e_vxlan_set_netdev_info(priv);
727                 mlx5e_dcbnl_build_rep_netdev(netdev);
728         } else {
729                 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
730                 eth_hw_addr_random(netdev);
731                 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
732         }
733
734         netdev->watchdog_timeo    = 15 * HZ;
735
736         netdev->features       |= NETIF_F_NETNS_LOCAL;
737
738 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
739         netdev->hw_features    |= NETIF_F_HW_TC;
740 #endif
741         netdev->hw_features    |= NETIF_F_SG;
742         netdev->hw_features    |= NETIF_F_IP_CSUM;
743         netdev->hw_features    |= NETIF_F_IPV6_CSUM;
744         netdev->hw_features    |= NETIF_F_GRO;
745         netdev->hw_features    |= NETIF_F_TSO;
746         netdev->hw_features    |= NETIF_F_TSO6;
747         netdev->hw_features    |= NETIF_F_RXCSUM;
748
749         if (rep->vport == MLX5_VPORT_UPLINK)
750                 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
751         else
752                 netdev->features |= NETIF_F_VLAN_CHALLENGED;
753
754         netdev->features |= netdev->hw_features;
755 }
756
757 static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
758                           struct net_device *netdev,
759                           const struct mlx5e_profile *profile,
760                           void *ppriv)
761 {
762         struct mlx5e_priv *priv = netdev_priv(netdev);
763         int err;
764
765         err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
766         if (err)
767                 return err;
768
769         priv->channels.params.num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;
770
771         mlx5e_build_rep_params(netdev);
772         mlx5e_build_rep_netdev(netdev);
773
774         mlx5e_timestamp_init(priv);
775
776         return 0;
777 }
778
779 static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
780 {
781         mlx5e_netdev_cleanup(priv->netdev, priv);
782 }
783
784 static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
785 {
786         struct mlx5e_rep_priv *rpriv = priv->ppriv;
787         struct mlx5_eswitch_rep *rep = rpriv->rep;
788         struct ttc_params ttc_params = {};
789         int tt, err;
790
791         priv->fs.ns = mlx5_get_flow_namespace(priv->mdev,
792                                               MLX5_FLOW_NAMESPACE_KERNEL);
793
794         /* The inner_ttc in the ttc params is intentionally not set */
795         ttc_params.any_tt_tirn = priv->direct_tir[0].tirn;
796         mlx5e_set_ttc_ft_params(&ttc_params);
797
798         if (rep->vport != MLX5_VPORT_UPLINK)
799                 /* To give uplik rep TTC a lower level for chaining from root ft */
800                 ttc_params.ft_attr.level = MLX5E_TTC_FT_LEVEL + 1;
801
802         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
803                 ttc_params.indir_tirn[tt] = priv->indir_tir[tt].tirn;
804
805         err = mlx5e_create_ttc_table(priv, &ttc_params, &priv->fs.ttc);
806         if (err) {
807                 netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n", err);
808                 return err;
809         }
810         return 0;
811 }
812
813 static int mlx5e_create_rep_root_ft(struct mlx5e_priv *priv)
814 {
815         struct mlx5e_rep_priv *rpriv = priv->ppriv;
816         struct mlx5_eswitch_rep *rep = rpriv->rep;
817         struct mlx5_flow_table_attr ft_attr = {};
818         struct mlx5_flow_namespace *ns;
819         int err = 0;
820
821         if (rep->vport != MLX5_VPORT_UPLINK) {
822                 /* non uplik reps will skip any bypass tables and go directly to
823                  * their own ttc
824                  */
825                 rpriv->root_ft = priv->fs.ttc.ft.t;
826                 return 0;
827         }
828
829         /* uplink root ft will be used to auto chain, to ethtool or ttc tables */
830         ns = mlx5_get_flow_namespace(priv->mdev, MLX5_FLOW_NAMESPACE_OFFLOADS);
831         if (!ns) {
832                 netdev_err(priv->netdev, "Failed to get reps offloads namespace\n");
833                 return -EOPNOTSUPP;
834         }
835
836         ft_attr.max_fte = 0; /* Empty table, miss rule will always point to next table */
837         ft_attr.prio = 1;
838         ft_attr.level = 1;
839
840         rpriv->root_ft = mlx5_create_flow_table(ns, &ft_attr);
841         if (IS_ERR(rpriv->root_ft)) {
842                 err = PTR_ERR(rpriv->root_ft);
843                 rpriv->root_ft = NULL;
844         }
845
846         return err;
847 }
848
849 static void mlx5e_destroy_rep_root_ft(struct mlx5e_priv *priv)
850 {
851         struct mlx5e_rep_priv *rpriv = priv->ppriv;
852         struct mlx5_eswitch_rep *rep = rpriv->rep;
853
854         if (rep->vport != MLX5_VPORT_UPLINK)
855                 return;
856         mlx5_destroy_flow_table(rpriv->root_ft);
857 }
858
859 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
860 {
861         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
862         struct mlx5e_rep_priv *rpriv = priv->ppriv;
863         struct mlx5_eswitch_rep *rep = rpriv->rep;
864         struct mlx5_flow_handle *flow_rule;
865         struct mlx5_flow_destination dest;
866
867         dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
868         dest.ft = rpriv->root_ft;
869
870         flow_rule = mlx5_eswitch_create_vport_rx_rule(esw, rep->vport, &dest);
871         if (IS_ERR(flow_rule))
872                 return PTR_ERR(flow_rule);
873         rpriv->vport_rx_rule = flow_rule;
874         return 0;
875 }
876
877 static void rep_vport_rx_rule_destroy(struct mlx5e_priv *priv)
878 {
879         struct mlx5e_rep_priv *rpriv = priv->ppriv;
880
881         if (!rpriv->vport_rx_rule)
882                 return;
883
884         mlx5_del_flow_rules(rpriv->vport_rx_rule);
885         rpriv->vport_rx_rule = NULL;
886 }
887
888 int mlx5e_rep_bond_update(struct mlx5e_priv *priv, bool cleanup)
889 {
890         rep_vport_rx_rule_destroy(priv);
891
892         return cleanup ? 0 : mlx5e_create_rep_vport_rx_rule(priv);
893 }
894
895 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
896 {
897         struct mlx5_core_dev *mdev = priv->mdev;
898         int err;
899
900         mlx5e_init_l2_addr(priv);
901
902         err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
903         if (err) {
904                 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
905                 return err;
906         }
907
908         err = mlx5e_create_indirect_rqt(priv);
909         if (err)
910                 goto err_close_drop_rq;
911
912         err = mlx5e_create_direct_rqts(priv, priv->direct_tir);
913         if (err)
914                 goto err_destroy_indirect_rqts;
915
916         err = mlx5e_create_indirect_tirs(priv, false);
917         if (err)
918                 goto err_destroy_direct_rqts;
919
920         err = mlx5e_create_direct_tirs(priv, priv->direct_tir);
921         if (err)
922                 goto err_destroy_indirect_tirs;
923
924         err = mlx5e_create_rep_ttc_table(priv);
925         if (err)
926                 goto err_destroy_direct_tirs;
927
928         err = mlx5e_create_rep_root_ft(priv);
929         if (err)
930                 goto err_destroy_ttc_table;
931
932         err = mlx5e_create_rep_vport_rx_rule(priv);
933         if (err)
934                 goto err_destroy_root_ft;
935
936         mlx5e_ethtool_init_steering(priv);
937
938         return 0;
939
940 err_destroy_root_ft:
941         mlx5e_destroy_rep_root_ft(priv);
942 err_destroy_ttc_table:
943         mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
944 err_destroy_direct_tirs:
945         mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
946 err_destroy_indirect_tirs:
947         mlx5e_destroy_indirect_tirs(priv);
948 err_destroy_direct_rqts:
949         mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
950 err_destroy_indirect_rqts:
951         mlx5e_destroy_rqt(priv, &priv->indir_rqt);
952 err_close_drop_rq:
953         mlx5e_close_drop_rq(&priv->drop_rq);
954         return err;
955 }
956
957 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
958 {
959         mlx5e_ethtool_cleanup_steering(priv);
960         rep_vport_rx_rule_destroy(priv);
961         mlx5e_destroy_rep_root_ft(priv);
962         mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
963         mlx5e_destroy_direct_tirs(priv, priv->direct_tir);
964         mlx5e_destroy_indirect_tirs(priv);
965         mlx5e_destroy_direct_rqts(priv, priv->direct_tir);
966         mlx5e_destroy_rqt(priv, &priv->indir_rqt);
967         mlx5e_close_drop_rq(&priv->drop_rq);
968 }
969
970 static int mlx5e_init_ul_rep_rx(struct mlx5e_priv *priv)
971 {
972         mlx5e_create_q_counters(priv);
973         return mlx5e_init_rep_rx(priv);
974 }
975
976 static void mlx5e_cleanup_ul_rep_rx(struct mlx5e_priv *priv)
977 {
978         mlx5e_cleanup_rep_rx(priv);
979         mlx5e_destroy_q_counters(priv);
980 }
981
982 static int mlx5e_init_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
983 {
984         struct mlx5_rep_uplink_priv *uplink_priv;
985         struct net_device *netdev;
986         struct mlx5e_priv *priv;
987         int err;
988
989         netdev = rpriv->netdev;
990         priv = netdev_priv(netdev);
991         uplink_priv = &rpriv->uplink_priv;
992
993         err = mlx5e_rep_tc_init(rpriv);
994         if (err)
995                 return err;
996
997         mlx5_init_port_tun_entropy(&uplink_priv->tun_entropy, priv->mdev);
998
999         mlx5e_rep_bond_init(rpriv);
1000         err = mlx5e_rep_tc_netdevice_event_register(rpriv);
1001         if (err) {
1002                 mlx5_core_err(priv->mdev, "Failed to register netdev notifier, err: %d\n",
1003                               err);
1004                 goto err_event_reg;
1005         }
1006
1007         return 0;
1008
1009 err_event_reg:
1010         mlx5e_rep_bond_cleanup(rpriv);
1011         mlx5e_rep_tc_cleanup(rpriv);
1012         return err;
1013 }
1014
1015 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1016 {
1017         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1018         int err;
1019
1020         err = mlx5e_create_tises(priv);
1021         if (err) {
1022                 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1023                 return err;
1024         }
1025
1026         if (rpriv->rep->vport == MLX5_VPORT_UPLINK) {
1027                 err = mlx5e_init_uplink_rep_tx(rpriv);
1028                 if (err)
1029                         goto destroy_tises;
1030         }
1031
1032         return 0;
1033
1034 destroy_tises:
1035         mlx5e_destroy_tises(priv);
1036         return err;
1037 }
1038
1039 static void mlx5e_cleanup_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
1040 {
1041         mlx5e_rep_tc_netdevice_event_unregister(rpriv);
1042         mlx5e_rep_bond_cleanup(rpriv);
1043         mlx5e_rep_tc_cleanup(rpriv);
1044 }
1045
1046 static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
1047 {
1048         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1049
1050         mlx5e_destroy_tises(priv);
1051
1052         if (rpriv->rep->vport == MLX5_VPORT_UPLINK)
1053                 mlx5e_cleanup_uplink_rep_tx(rpriv);
1054 }
1055
1056 static void mlx5e_rep_enable(struct mlx5e_priv *priv)
1057 {
1058         mlx5e_set_netdev_mtu_boundaries(priv);
1059 }
1060
1061 static int mlx5e_update_rep_rx(struct mlx5e_priv *priv)
1062 {
1063         return 0;
1064 }
1065
1066 static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
1067 {
1068         struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
1069
1070         if (event == MLX5_EVENT_TYPE_PORT_CHANGE) {
1071                 struct mlx5_eqe *eqe = data;
1072
1073                 switch (eqe->sub_type) {
1074                 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
1075                 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
1076                         queue_work(priv->wq, &priv->update_carrier_work);
1077                         break;
1078                 default:
1079                         return NOTIFY_DONE;
1080                 }
1081
1082                 return NOTIFY_OK;
1083         }
1084
1085         if (event == MLX5_DEV_EVENT_PORT_AFFINITY)
1086                 return mlx5e_rep_tc_event_port_affinity(priv);
1087
1088         return NOTIFY_DONE;
1089 }
1090
1091 static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
1092 {
1093         struct net_device *netdev = priv->netdev;
1094         struct mlx5_core_dev *mdev = priv->mdev;
1095         u16 max_mtu;
1096
1097         netdev->min_mtu = ETH_MIN_MTU;
1098         mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
1099         netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1100         mlx5e_set_dev_port_mtu(priv);
1101
1102         mlx5e_rep_tc_enable(priv);
1103
1104         mlx5_modify_vport_admin_state(mdev, MLX5_VPORT_STATE_OP_MOD_UPLINK,
1105                                       0, 0, MLX5_VPORT_ADMIN_STATE_AUTO);
1106         mlx5_lag_add(mdev, netdev);
1107         priv->events_nb.notifier_call = uplink_rep_async_event;
1108         mlx5_notifier_register(mdev, &priv->events_nb);
1109         mlx5e_dcbnl_initialize(priv);
1110         mlx5e_dcbnl_init_app(priv);
1111 }
1112
1113 static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
1114 {
1115         struct mlx5_core_dev *mdev = priv->mdev;
1116
1117         mlx5e_dcbnl_delete_app(priv);
1118         mlx5_notifier_unregister(mdev, &priv->events_nb);
1119         mlx5e_rep_tc_disable(priv);
1120         mlx5_lag_remove(mdev);
1121 }
1122
1123 static MLX5E_DEFINE_STATS_GRP(sw_rep, 0);
1124 static MLX5E_DEFINE_STATS_GRP(vport_rep, MLX5E_NDO_UPDATE_STATS);
1125
1126 /* The stats groups order is opposite to the update_stats() order calls */
1127 static mlx5e_stats_grp_t mlx5e_rep_stats_grps[] = {
1128         &MLX5E_STATS_GRP(sw_rep),
1129         &MLX5E_STATS_GRP(vport_rep),
1130 };
1131
1132 static unsigned int mlx5e_rep_stats_grps_num(struct mlx5e_priv *priv)
1133 {
1134         return ARRAY_SIZE(mlx5e_rep_stats_grps);
1135 }
1136
1137 /* The stats groups order is opposite to the update_stats() order calls */
1138 static mlx5e_stats_grp_t mlx5e_ul_rep_stats_grps[] = {
1139         &MLX5E_STATS_GRP(sw),
1140         &MLX5E_STATS_GRP(qcnt),
1141         &MLX5E_STATS_GRP(vnic_env),
1142         &MLX5E_STATS_GRP(vport),
1143         &MLX5E_STATS_GRP(802_3),
1144         &MLX5E_STATS_GRP(2863),
1145         &MLX5E_STATS_GRP(2819),
1146         &MLX5E_STATS_GRP(phy),
1147         &MLX5E_STATS_GRP(eth_ext),
1148         &MLX5E_STATS_GRP(pcie),
1149         &MLX5E_STATS_GRP(per_prio),
1150         &MLX5E_STATS_GRP(pme),
1151         &MLX5E_STATS_GRP(channels),
1152         &MLX5E_STATS_GRP(per_port_buff_congest),
1153 };
1154
1155 static unsigned int mlx5e_ul_rep_stats_grps_num(struct mlx5e_priv *priv)
1156 {
1157         return ARRAY_SIZE(mlx5e_ul_rep_stats_grps);
1158 }
1159
1160 static const struct mlx5e_profile mlx5e_rep_profile = {
1161         .init                   = mlx5e_init_rep,
1162         .cleanup                = mlx5e_cleanup_rep,
1163         .init_rx                = mlx5e_init_rep_rx,
1164         .cleanup_rx             = mlx5e_cleanup_rep_rx,
1165         .init_tx                = mlx5e_init_rep_tx,
1166         .cleanup_tx             = mlx5e_cleanup_rep_tx,
1167         .enable                 = mlx5e_rep_enable,
1168         .update_rx              = mlx5e_update_rep_rx,
1169         .update_stats           = mlx5e_stats_update_ndo_stats,
1170         .rx_handlers            = &mlx5e_rx_handlers_rep,
1171         .max_tc                 = 1,
1172         .rq_groups              = MLX5E_NUM_RQ_GROUPS(REGULAR),
1173         .stats_grps             = mlx5e_rep_stats_grps,
1174         .stats_grps_num         = mlx5e_rep_stats_grps_num,
1175 };
1176
1177 static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
1178         .init                   = mlx5e_init_rep,
1179         .cleanup                = mlx5e_cleanup_rep,
1180         .init_rx                = mlx5e_init_ul_rep_rx,
1181         .cleanup_rx             = mlx5e_cleanup_ul_rep_rx,
1182         .init_tx                = mlx5e_init_rep_tx,
1183         .cleanup_tx             = mlx5e_cleanup_rep_tx,
1184         .enable                 = mlx5e_uplink_rep_enable,
1185         .disable                = mlx5e_uplink_rep_disable,
1186         .update_rx              = mlx5e_update_rep_rx,
1187         .update_stats           = mlx5e_stats_update_ndo_stats,
1188         .update_carrier         = mlx5e_update_carrier,
1189         .rx_handlers            = &mlx5e_rx_handlers_rep,
1190         .max_tc                 = MLX5E_MAX_NUM_TC,
1191         .rq_groups              = MLX5E_NUM_RQ_GROUPS(REGULAR),
1192         .stats_grps             = mlx5e_ul_rep_stats_grps,
1193         .stats_grps_num         = mlx5e_ul_rep_stats_grps_num,
1194 };
1195
1196 /* e-Switch vport representors */
1197 static int
1198 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1199 {
1200         const struct mlx5e_profile *profile;
1201         struct mlx5e_rep_priv *rpriv;
1202         struct devlink_port *dl_port;
1203         struct net_device *netdev;
1204         int nch, err;
1205
1206         rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1207         if (!rpriv)
1208                 return -ENOMEM;
1209
1210         /* rpriv->rep to be looked up when profile->init() is called */
1211         rpriv->rep = rep;
1212
1213         nch = mlx5e_get_max_num_channels(dev);
1214         profile = (rep->vport == MLX5_VPORT_UPLINK) ?
1215                   &mlx5e_uplink_rep_profile : &mlx5e_rep_profile;
1216         netdev = mlx5e_create_netdev(dev, profile, nch, rpriv);
1217         if (!netdev) {
1218                 mlx5_core_warn(dev,
1219                                "Failed to create representor netdev for vport %d\n",
1220                                rep->vport);
1221                 kfree(rpriv);
1222                 return -EINVAL;
1223         }
1224
1225         dev_net_set(netdev, mlx5_core_net(dev));
1226         rpriv->netdev = netdev;
1227         rep->rep_data[REP_ETH].priv = rpriv;
1228         INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1229
1230         if (rep->vport == MLX5_VPORT_UPLINK) {
1231                 err = mlx5e_create_mdev_resources(dev);
1232                 if (err)
1233                         goto err_destroy_netdev;
1234         }
1235
1236         err = mlx5e_attach_netdev(netdev_priv(netdev));
1237         if (err) {
1238                 netdev_warn(netdev,
1239                             "Failed to attach representor netdev for vport %d\n",
1240                             rep->vport);
1241                 goto err_destroy_mdev_resources;
1242         }
1243
1244         err = mlx5e_rep_neigh_init(rpriv);
1245         if (err) {
1246                 netdev_warn(netdev,
1247                             "Failed to initialized neighbours handling for vport %d\n",
1248                             rep->vport);
1249                 goto err_detach_netdev;
1250         }
1251
1252         err = register_netdev(netdev);
1253         if (err) {
1254                 netdev_warn(netdev,
1255                             "Failed to register representor netdev for vport %d\n",
1256                             rep->vport);
1257                 goto err_neigh_cleanup;
1258         }
1259
1260         dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
1261         if (dl_port)
1262                 devlink_port_type_eth_set(dl_port, netdev);
1263         return 0;
1264
1265 err_neigh_cleanup:
1266         mlx5e_rep_neigh_cleanup(rpriv);
1267
1268 err_detach_netdev:
1269         mlx5e_detach_netdev(netdev_priv(netdev));
1270
1271 err_destroy_mdev_resources:
1272         if (rep->vport == MLX5_VPORT_UPLINK)
1273                 mlx5e_destroy_mdev_resources(dev);
1274
1275 err_destroy_netdev:
1276         mlx5e_destroy_netdev(netdev_priv(netdev));
1277         kfree(rpriv);
1278         return err;
1279 }
1280
1281 static void
1282 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1283 {
1284         struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1285         struct net_device *netdev = rpriv->netdev;
1286         struct mlx5e_priv *priv = netdev_priv(netdev);
1287         struct mlx5_core_dev *dev = priv->mdev;
1288         struct devlink_port *dl_port;
1289         void *ppriv = priv->ppriv;
1290
1291         dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch, rpriv->rep->vport);
1292         if (dl_port)
1293                 devlink_port_type_clear(dl_port);
1294         unregister_netdev(netdev);
1295         mlx5e_rep_neigh_cleanup(rpriv);
1296         mlx5e_detach_netdev(priv);
1297         if (rep->vport == MLX5_VPORT_UPLINK)
1298                 mlx5e_destroy_mdev_resources(priv->mdev);
1299         mlx5e_destroy_netdev(priv);
1300         kfree(ppriv); /* mlx5e_rep_priv */
1301 }
1302
1303 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1304 {
1305         struct mlx5e_rep_priv *rpriv;
1306
1307         rpriv = mlx5e_rep_to_rep_priv(rep);
1308
1309         return rpriv->netdev;
1310 }
1311
1312 static const struct mlx5_eswitch_rep_ops rep_ops = {
1313         .load = mlx5e_vport_rep_load,
1314         .unload = mlx5e_vport_rep_unload,
1315         .get_proto_dev = mlx5e_vport_rep_get_proto_dev
1316 };
1317
1318 static int mlx5e_rep_probe(struct auxiliary_device *adev,
1319                            const struct auxiliary_device_id *id)
1320 {
1321         struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
1322         struct mlx5_core_dev *mdev = edev->mdev;
1323         struct mlx5_eswitch *esw;
1324
1325         esw = mdev->priv.eswitch;
1326         mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_ETH);
1327         return 0;
1328 }
1329
1330 static void mlx5e_rep_remove(struct auxiliary_device *adev)
1331 {
1332         struct mlx5_adev *vdev = container_of(adev, struct mlx5_adev, adev);
1333         struct mlx5_core_dev *mdev = vdev->mdev;
1334         struct mlx5_eswitch *esw;
1335
1336         esw = mdev->priv.eswitch;
1337         mlx5_eswitch_unregister_vport_reps(esw, REP_ETH);
1338 }
1339
1340 static const struct auxiliary_device_id mlx5e_rep_id_table[] = {
1341         { .name = MLX5_ADEV_NAME ".eth-rep", },
1342         {},
1343 };
1344
1345 MODULE_DEVICE_TABLE(auxiliary, mlx5e_rep_id_table);
1346
1347 static struct auxiliary_driver mlx5e_rep_driver = {
1348         .name = "eth-rep",
1349         .probe = mlx5e_rep_probe,
1350         .remove = mlx5e_rep_remove,
1351         .id_table = mlx5e_rep_id_table,
1352 };
1353
1354 int mlx5e_rep_init(void)
1355 {
1356         return auxiliary_driver_register(&mlx5e_rep_driver);
1357 }
1358
1359 void mlx5e_rep_cleanup(void)
1360 {
1361         auxiliary_driver_unregister(&mlx5e_rep_driver);
1362 }