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