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