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