2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/debugfs.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>
44 #include "en/params.h"
47 #include "en/rep/tc.h"
48 #include "en/rep/neigh.h"
49 #include "en/rep/bridge.h"
50 #include "en/devlink.h"
53 #include "lib/devcom.h"
54 #include "lib/vxlan.h"
55 #define CREATE_TRACE_POINTS
56 #include "diag/en_rep_tracepoint.h"
57 #include "diag/reporter_vnic.h"
58 #include "en_accel/ipsec.h"
59 #include "en/tc/int_port.h"
61 #include "en/fs_ethtool.h"
63 #define MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE \
64 max(0x7, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)
65 #define MLX5E_REP_PARAMS_DEF_NUM_CHANNELS 1
67 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
69 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
70 struct ethtool_drvinfo *drvinfo)
72 struct mlx5e_priv *priv = netdev_priv(dev);
73 struct mlx5_core_dev *mdev = priv->mdev;
75 strscpy(drvinfo->driver, mlx5e_rep_driver_name,
76 sizeof(drvinfo->driver));
77 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
79 fw_rev_maj(mdev), fw_rev_min(mdev),
80 fw_rev_sub(mdev), mdev->board_id);
83 static const struct counter_desc sw_rep_stats_desc[] = {
84 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
85 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
86 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
87 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
90 static const struct counter_desc vport_rep_stats_desc[] = {
91 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, vport_rx_packets) },
92 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, vport_rx_bytes) },
93 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, vport_tx_packets) },
94 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, vport_tx_bytes) },
95 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
96 rx_vport_rdma_unicast_packets) },
97 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, rx_vport_rdma_unicast_bytes) },
98 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
99 tx_vport_rdma_unicast_packets) },
100 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats, tx_vport_rdma_unicast_bytes) },
101 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
102 rx_vport_rdma_multicast_packets) },
103 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
104 rx_vport_rdma_multicast_bytes) },
105 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
106 tx_vport_rdma_multicast_packets) },
107 { MLX5E_DECLARE_STAT(struct mlx5e_rep_stats,
108 tx_vport_rdma_multicast_bytes) },
111 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
112 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
114 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(sw_rep)
116 return NUM_VPORT_REP_SW_COUNTERS;
119 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(sw_rep)
123 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
124 strcpy(data + (idx++) * ETH_GSTRING_LEN,
125 sw_rep_stats_desc[i].format);
129 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(sw_rep)
133 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
134 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
135 sw_rep_stats_desc, i);
139 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(sw_rep)
141 struct mlx5e_sw_stats *s = &priv->stats.sw;
142 struct rtnl_link_stats64 stats64 = {};
144 memset(s, 0, sizeof(*s));
145 mlx5e_fold_sw_stats64(priv, &stats64);
147 s->rx_packets = stats64.rx_packets;
148 s->rx_bytes = stats64.rx_bytes;
149 s->tx_packets = stats64.tx_packets;
150 s->tx_bytes = stats64.tx_bytes;
151 s->tx_queue_dropped = stats64.tx_dropped;
154 static MLX5E_DECLARE_STATS_GRP_OP_NUM_STATS(vport_rep)
156 return NUM_VPORT_REP_HW_COUNTERS;
159 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STRS(vport_rep)
163 for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
164 strcpy(data + (idx++) * ETH_GSTRING_LEN, vport_rep_stats_desc[i].format);
168 static MLX5E_DECLARE_STATS_GRP_OP_FILL_STATS(vport_rep)
172 for (i = 0; i < NUM_VPORT_REP_HW_COUNTERS; i++)
173 data[idx++] = MLX5E_READ_CTR64_CPU(&priv->stats.rep_stats,
174 vport_rep_stats_desc, i);
178 static MLX5E_DECLARE_STATS_GRP_OP_UPDATE_STATS(vport_rep)
180 struct mlx5e_rep_stats *rep_stats = &priv->stats.rep_stats;
181 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
182 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
183 struct mlx5e_rep_priv *rpriv = priv->ppriv;
184 struct mlx5_eswitch_rep *rep = rpriv->rep;
188 out = kvzalloc(outlen, GFP_KERNEL);
192 err = mlx5_core_query_vport_counter(esw->dev, 1, rep->vport - 1, 0, out);
194 netdev_warn(priv->netdev, "vport %d error %d reading stats\n",
199 #define MLX5_GET_CTR(p, x) \
200 MLX5_GET64(query_vport_counter_out, p, x)
201 /* flip tx/rx as we are reporting the counters for the switch vport */
202 rep_stats->vport_rx_packets =
203 MLX5_GET_CTR(out, transmitted_ib_unicast.packets) +
204 MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
205 MLX5_GET_CTR(out, transmitted_ib_multicast.packets) +
206 MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
207 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
209 rep_stats->vport_tx_packets =
210 MLX5_GET_CTR(out, received_ib_unicast.packets) +
211 MLX5_GET_CTR(out, received_eth_unicast.packets) +
212 MLX5_GET_CTR(out, received_ib_multicast.packets) +
213 MLX5_GET_CTR(out, received_eth_multicast.packets) +
214 MLX5_GET_CTR(out, received_eth_broadcast.packets);
216 rep_stats->vport_rx_bytes =
217 MLX5_GET_CTR(out, transmitted_ib_unicast.octets) +
218 MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
219 MLX5_GET_CTR(out, transmitted_ib_multicast.octets) +
220 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
222 rep_stats->vport_tx_bytes =
223 MLX5_GET_CTR(out, received_ib_unicast.octets) +
224 MLX5_GET_CTR(out, received_eth_unicast.octets) +
225 MLX5_GET_CTR(out, received_ib_multicast.octets) +
226 MLX5_GET_CTR(out, received_eth_multicast.octets) +
227 MLX5_GET_CTR(out, received_eth_broadcast.octets);
229 rep_stats->rx_vport_rdma_unicast_packets =
230 MLX5_GET_CTR(out, transmitted_ib_unicast.packets);
231 rep_stats->tx_vport_rdma_unicast_packets =
232 MLX5_GET_CTR(out, received_ib_unicast.packets);
233 rep_stats->rx_vport_rdma_unicast_bytes =
234 MLX5_GET_CTR(out, transmitted_ib_unicast.octets);
235 rep_stats->tx_vport_rdma_unicast_bytes =
236 MLX5_GET_CTR(out, received_ib_unicast.octets);
237 rep_stats->rx_vport_rdma_multicast_packets =
238 MLX5_GET_CTR(out, transmitted_ib_multicast.packets);
239 rep_stats->tx_vport_rdma_multicast_packets =
240 MLX5_GET_CTR(out, received_ib_multicast.packets);
241 rep_stats->rx_vport_rdma_multicast_bytes =
242 MLX5_GET_CTR(out, transmitted_ib_multicast.octets);
243 rep_stats->tx_vport_rdma_multicast_bytes =
244 MLX5_GET_CTR(out, received_ib_multicast.octets);
250 static void mlx5e_rep_get_strings(struct net_device *dev,
251 u32 stringset, uint8_t *data)
253 struct mlx5e_priv *priv = netdev_priv(dev);
257 mlx5e_stats_fill_strings(priv, data);
262 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
263 struct ethtool_stats *stats, u64 *data)
265 struct mlx5e_priv *priv = netdev_priv(dev);
267 mlx5e_ethtool_get_ethtool_stats(priv, stats, data);
270 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
272 struct mlx5e_priv *priv = netdev_priv(dev);
276 return mlx5e_stats_total_num(priv);
283 mlx5e_rep_get_ringparam(struct net_device *dev,
284 struct ethtool_ringparam *param,
285 struct kernel_ethtool_ringparam *kernel_param,
286 struct netlink_ext_ack *extack)
288 struct mlx5e_priv *priv = netdev_priv(dev);
290 mlx5e_ethtool_get_ringparam(priv, param, kernel_param);
294 mlx5e_rep_set_ringparam(struct net_device *dev,
295 struct ethtool_ringparam *param,
296 struct kernel_ethtool_ringparam *kernel_param,
297 struct netlink_ext_ack *extack)
299 struct mlx5e_priv *priv = netdev_priv(dev);
301 return mlx5e_ethtool_set_ringparam(priv, param);
304 static void mlx5e_rep_get_channels(struct net_device *dev,
305 struct ethtool_channels *ch)
307 struct mlx5e_priv *priv = netdev_priv(dev);
309 mlx5e_ethtool_get_channels(priv, ch);
312 static int mlx5e_rep_set_channels(struct net_device *dev,
313 struct ethtool_channels *ch)
315 struct mlx5e_priv *priv = netdev_priv(dev);
317 return mlx5e_ethtool_set_channels(priv, ch);
320 static int mlx5e_rep_get_coalesce(struct net_device *netdev,
321 struct ethtool_coalesce *coal,
322 struct kernel_ethtool_coalesce *kernel_coal,
323 struct netlink_ext_ack *extack)
325 struct mlx5e_priv *priv = netdev_priv(netdev);
327 return mlx5e_ethtool_get_coalesce(priv, coal, kernel_coal);
330 static int mlx5e_rep_set_coalesce(struct net_device *netdev,
331 struct ethtool_coalesce *coal,
332 struct kernel_ethtool_coalesce *kernel_coal,
333 struct netlink_ext_ack *extack)
335 struct mlx5e_priv *priv = netdev_priv(netdev);
337 return mlx5e_ethtool_set_coalesce(priv, coal, kernel_coal, extack);
340 static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
342 struct mlx5e_priv *priv = netdev_priv(netdev);
344 return mlx5e_ethtool_get_rxfh_key_size(priv);
347 static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
349 struct mlx5e_priv *priv = netdev_priv(netdev);
351 return mlx5e_ethtool_get_rxfh_indir_size(priv);
354 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
355 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
356 ETHTOOL_COALESCE_MAX_FRAMES |
357 ETHTOOL_COALESCE_USE_ADAPTIVE,
358 .get_drvinfo = mlx5e_rep_get_drvinfo,
359 .get_link = ethtool_op_get_link,
360 .get_strings = mlx5e_rep_get_strings,
361 .get_sset_count = mlx5e_rep_get_sset_count,
362 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
363 .get_ringparam = mlx5e_rep_get_ringparam,
364 .set_ringparam = mlx5e_rep_set_ringparam,
365 .get_channels = mlx5e_rep_get_channels,
366 .set_channels = mlx5e_rep_set_channels,
367 .get_coalesce = mlx5e_rep_get_coalesce,
368 .set_coalesce = mlx5e_rep_set_coalesce,
369 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
370 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
373 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
374 struct mlx5_eswitch_rep *rep)
376 struct mlx5e_rep_sq *rep_sq, *tmp;
377 struct mlx5e_rep_sq_peer *sq_peer;
378 struct mlx5e_rep_priv *rpriv;
381 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
384 rpriv = mlx5e_rep_to_rep_priv(rep);
385 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
386 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
387 xa_for_each(&rep_sq->sq_peer, i, sq_peer) {
389 mlx5_eswitch_del_send_to_vport_rule(sq_peer->rule);
391 xa_erase(&rep_sq->sq_peer, i);
395 xa_destroy(&rep_sq->sq_peer);
396 list_del(&rep_sq->list);
401 static int mlx5e_sqs2vport_add_peers_rules(struct mlx5_eswitch *esw, struct mlx5_eswitch_rep *rep,
402 struct mlx5_devcom *devcom,
403 struct mlx5e_rep_sq *rep_sq, int i)
405 struct mlx5_eswitch *peer_esw = NULL;
406 struct mlx5_flow_handle *flow_rule;
409 mlx5_devcom_for_each_peer_entry(devcom, MLX5_DEVCOM_ESW_OFFLOADS,
411 u16 peer_rule_idx = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
412 struct mlx5e_rep_sq_peer *sq_peer;
415 sq_peer = kzalloc(sizeof(*sq_peer), GFP_KERNEL);
419 flow_rule = mlx5_eswitch_add_send_to_vport_rule(peer_esw, esw,
421 if (IS_ERR(flow_rule)) {
423 return PTR_ERR(flow_rule);
426 sq_peer->rule = flow_rule;
427 sq_peer->peer = peer_esw;
428 err = xa_insert(&rep_sq->sq_peer, peer_rule_idx, sq_peer, GFP_KERNEL);
431 mlx5_eswitch_del_send_to_vport_rule(flow_rule);
439 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
440 struct mlx5_eswitch_rep *rep,
441 u32 *sqns_array, int sqns_num)
443 struct mlx5_flow_handle *flow_rule;
444 struct mlx5e_rep_priv *rpriv;
445 struct mlx5e_rep_sq *rep_sq;
446 struct mlx5_devcom *devcom;
447 bool devcom_locked = false;
451 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
454 devcom = esw->dev->priv.devcom;
455 rpriv = mlx5e_rep_to_rep_priv(rep);
456 if (mlx5_devcom_comp_is_ready(devcom, MLX5_DEVCOM_ESW_OFFLOADS) &&
457 mlx5_devcom_for_each_peer_begin(devcom, MLX5_DEVCOM_ESW_OFFLOADS))
458 devcom_locked = true;
460 for (i = 0; i < sqns_num; i++) {
461 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
467 /* Add re-inject rule to the PF/representor sqs */
468 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw, esw, rep,
470 if (IS_ERR(flow_rule)) {
471 err = PTR_ERR(flow_rule);
475 rep_sq->send_to_vport_rule = flow_rule;
476 rep_sq->sqn = sqns_array[i];
478 xa_init(&rep_sq->sq_peer);
480 err = mlx5e_sqs2vport_add_peers_rules(esw, rep, devcom, rep_sq, i);
482 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
483 xa_destroy(&rep_sq->sq_peer);
489 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
493 mlx5_devcom_for_each_peer_end(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
498 mlx5e_sqs2vport_stop(esw, rep);
501 mlx5_devcom_for_each_peer_end(devcom, MLX5_DEVCOM_ESW_OFFLOADS);
507 mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
509 int sqs_per_channel = mlx5e_get_dcb_num_tc(&priv->channels.params);
510 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
511 bool is_uplink_rep = mlx5e_is_uplink_rep(priv);
512 struct mlx5e_rep_priv *rpriv = priv->ppriv;
513 struct mlx5_eswitch_rep *rep = rpriv->rep;
514 int n, tc, nch, num_sqs = 0;
515 struct mlx5e_channel *c;
520 ptp_sq = !!(priv->channels.ptp &&
521 MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_TX_PORT_TS));
522 nch = priv->channels.num + ptp_sq;
523 /* +2 for xdpsqs, they don't exist on the ptp channel but will not be
524 * counted for by num_sqs.
527 sqs_per_channel += 2;
529 sqs = kvcalloc(nch * sqs_per_channel, sizeof(*sqs), GFP_KERNEL);
533 for (n = 0; n < priv->channels.num; n++) {
534 c = priv->channels.c[n];
535 for (tc = 0; tc < c->num_tc; tc++)
536 sqs[num_sqs++] = c->sq[tc].sqn;
540 sqs[num_sqs++] = c->rq_xdpsq.sqn;
542 sqs[num_sqs++] = c->xdpsq.sqn;
546 struct mlx5e_ptp *ptp_ch = priv->channels.ptp;
548 for (tc = 0; tc < ptp_ch->num_tc; tc++)
549 sqs[num_sqs++] = ptp_ch->ptpsq[tc].txqsq.sqn;
552 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
557 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
562 mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
564 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
565 struct mlx5e_rep_priv *rpriv = priv->ppriv;
566 struct mlx5_eswitch_rep *rep = rpriv->rep;
568 mlx5e_sqs2vport_stop(esw, rep);
572 mlx5e_rep_add_meta_tunnel_rule(struct mlx5e_priv *priv)
574 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
575 struct mlx5e_rep_priv *rpriv = priv->ppriv;
576 struct mlx5_eswitch_rep *rep = rpriv->rep;
577 struct mlx5_flow_handle *flow_rule;
578 struct mlx5_flow_group *g;
580 g = esw->fdb_table.offloads.send_to_vport_meta_grp;
584 flow_rule = mlx5_eswitch_add_send_to_vport_meta_rule(esw, rep->vport);
585 if (IS_ERR(flow_rule))
586 return PTR_ERR(flow_rule);
588 rpriv->send_to_vport_meta_rule = flow_rule;
594 mlx5e_rep_del_meta_tunnel_rule(struct mlx5e_priv *priv)
596 struct mlx5e_rep_priv *rpriv = priv->ppriv;
598 if (rpriv->send_to_vport_meta_rule)
599 mlx5_eswitch_del_send_to_vport_meta_rule(rpriv->send_to_vport_meta_rule);
602 void mlx5e_rep_activate_channels(struct mlx5e_priv *priv)
604 mlx5e_add_sqs_fwd_rules(priv);
605 mlx5e_rep_add_meta_tunnel_rule(priv);
608 void mlx5e_rep_deactivate_channels(struct mlx5e_priv *priv)
610 mlx5e_rep_del_meta_tunnel_rule(priv);
611 mlx5e_remove_sqs_fwd_rules(priv);
614 static int mlx5e_rep_open(struct net_device *dev)
616 struct mlx5e_priv *priv = netdev_priv(dev);
617 struct mlx5e_rep_priv *rpriv = priv->ppriv;
618 struct mlx5_eswitch_rep *rep = rpriv->rep;
621 mutex_lock(&priv->state_lock);
622 err = mlx5e_open_locked(dev);
626 if (!mlx5_modify_vport_admin_state(priv->mdev,
627 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
629 MLX5_VPORT_ADMIN_STATE_UP))
630 netif_carrier_on(dev);
633 mutex_unlock(&priv->state_lock);
637 static int mlx5e_rep_close(struct net_device *dev)
639 struct mlx5e_priv *priv = netdev_priv(dev);
640 struct mlx5e_rep_priv *rpriv = priv->ppriv;
641 struct mlx5_eswitch_rep *rep = rpriv->rep;
644 mutex_lock(&priv->state_lock);
645 mlx5_modify_vport_admin_state(priv->mdev,
646 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
648 MLX5_VPORT_ADMIN_STATE_DOWN);
649 ret = mlx5e_close_locked(dev);
650 mutex_unlock(&priv->state_lock);
654 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
656 struct mlx5e_rep_priv *rpriv = priv->ppriv;
657 struct mlx5_eswitch_rep *rep;
659 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
662 if (!rpriv) /* non vport rep mlx5e instances don't use this field */
666 return (rep->vport == MLX5_VPORT_UPLINK);
669 bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
672 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
680 mlx5e_get_sw_stats64(const struct net_device *dev,
681 struct rtnl_link_stats64 *stats)
683 struct mlx5e_priv *priv = netdev_priv(dev);
685 mlx5e_fold_sw_stats64(priv, stats);
689 int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
693 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
694 return mlx5e_get_sw_stats64(dev, sp);
701 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
703 struct mlx5e_priv *priv = netdev_priv(dev);
705 /* update HW stats in background for next time */
706 mlx5e_queue_update_stats(priv);
707 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
710 static int mlx5e_rep_change_mtu(struct net_device *netdev, int new_mtu)
712 return mlx5e_change_mtu(netdev, new_mtu, NULL);
715 static int mlx5e_rep_change_carrier(struct net_device *dev, bool new_carrier)
717 struct mlx5e_priv *priv = netdev_priv(dev);
718 struct mlx5e_rep_priv *rpriv = priv->ppriv;
719 struct mlx5_eswitch_rep *rep = rpriv->rep;
723 err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
724 rep->vport, 1, MLX5_VPORT_ADMIN_STATE_UP);
727 netif_carrier_on(dev);
729 err = mlx5_modify_vport_admin_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
730 rep->vport, 1, MLX5_VPORT_ADMIN_STATE_DOWN);
733 netif_carrier_off(dev);
738 static const struct net_device_ops mlx5e_netdev_ops_rep = {
739 .ndo_open = mlx5e_rep_open,
740 .ndo_stop = mlx5e_rep_close,
741 .ndo_start_xmit = mlx5e_xmit,
742 .ndo_setup_tc = mlx5e_rep_setup_tc,
743 .ndo_get_stats64 = mlx5e_rep_get_stats,
744 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
745 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
746 .ndo_change_mtu = mlx5e_rep_change_mtu,
747 .ndo_change_carrier = mlx5e_rep_change_carrier,
750 bool mlx5e_eswitch_uplink_rep(const struct net_device *netdev)
752 return netdev->netdev_ops == &mlx5e_netdev_ops &&
753 mlx5e_is_uplink_rep(netdev_priv(netdev));
756 bool mlx5e_eswitch_vf_rep(const struct net_device *netdev)
758 return netdev->netdev_ops == &mlx5e_netdev_ops_rep;
761 /* One indirect TIR set for outer. Inner not supported in reps. */
762 #define REP_NUM_INDIR_TIRS MLX5E_NUM_INDIR_TIRS
764 static int mlx5e_rep_max_nch_limit(struct mlx5_core_dev *mdev)
766 int max_tir_num = 1 << MLX5_CAP_GEN(mdev, log_max_tir);
767 int num_vports = mlx5_eswitch_get_total_vports(mdev);
769 return (max_tir_num - mlx5e_get_pf_num_tirs(mdev)
770 - (num_vports * REP_NUM_INDIR_TIRS)) / num_vports;
773 static void mlx5e_build_rep_params(struct net_device *netdev)
775 struct mlx5e_priv *priv = netdev_priv(netdev);
776 struct mlx5e_rep_priv *rpriv = priv->ppriv;
777 struct mlx5_eswitch_rep *rep = rpriv->rep;
778 struct mlx5_core_dev *mdev = priv->mdev;
779 struct mlx5e_params *params;
781 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
782 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
783 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
785 params = &priv->channels.params;
787 params->num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;
788 params->hard_mtu = MLX5E_ETH_HARD_MTU;
789 params->sw_mtu = netdev->mtu;
792 if (rep->vport == MLX5_VPORT_UPLINK)
793 params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
795 params->log_sq_size = MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE;
798 mlx5e_build_rq_params(mdev, params);
800 /* update XDP supported features */
801 mlx5e_set_xdp_feature(netdev);
803 /* CQ moderation params */
804 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
805 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
807 params->mqprio.num_tc = 1;
808 if (rep->vport != MLX5_VPORT_UPLINK)
809 params->vlan_strip_disable = true;
811 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
814 static void mlx5e_build_rep_netdev(struct net_device *netdev,
815 struct mlx5_core_dev *mdev)
817 SET_NETDEV_DEV(netdev, mdev->device);
818 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
819 eth_hw_addr_random(netdev);
820 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
822 netdev->watchdog_timeo = 15 * HZ;
824 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
825 netdev->hw_features |= NETIF_F_HW_TC;
827 netdev->hw_features |= NETIF_F_SG;
828 netdev->hw_features |= NETIF_F_IP_CSUM;
829 netdev->hw_features |= NETIF_F_IPV6_CSUM;
830 netdev->hw_features |= NETIF_F_GRO;
831 netdev->hw_features |= NETIF_F_TSO;
832 netdev->hw_features |= NETIF_F_TSO6;
833 netdev->hw_features |= NETIF_F_RXCSUM;
835 netdev->features |= netdev->hw_features;
836 netdev->features |= NETIF_F_NETNS_LOCAL;
839 static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
840 struct net_device *netdev)
842 struct mlx5e_priv *priv = netdev_priv(netdev);
845 mlx5e_fs_init(priv->profile, mdev,
846 !test_bit(MLX5E_STATE_DESTROYING, &priv->state),
849 netdev_err(priv->netdev, "FS allocation failed\n");
853 mlx5e_build_rep_params(netdev);
854 mlx5e_timestamp_init(priv);
859 static int mlx5e_init_ul_rep(struct mlx5_core_dev *mdev,
860 struct net_device *netdev)
862 struct mlx5e_priv *priv = netdev_priv(netdev);
864 priv->dfs_root = debugfs_create_dir("nic",
865 mlx5_debugfs_get_dev_root(mdev));
867 priv->fs = mlx5e_fs_init(priv->profile, mdev,
868 !test_bit(MLX5E_STATE_DESTROYING, &priv->state),
871 netdev_err(priv->netdev, "FS allocation failed\n");
872 debugfs_remove_recursive(priv->dfs_root);
876 mlx5e_vxlan_set_netdev_info(priv);
877 mlx5e_build_rep_params(netdev);
878 mlx5e_timestamp_init(priv);
882 static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
884 mlx5e_fs_cleanup(priv->fs);
885 debugfs_remove_recursive(priv->dfs_root);
889 static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
891 struct mlx5e_rep_priv *rpriv = priv->ppriv;
892 struct mlx5_eswitch_rep *rep = rpriv->rep;
893 struct ttc_params ttc_params = {};
896 mlx5e_fs_set_ns(priv->fs,
897 mlx5_get_flow_namespace(priv->mdev,
898 MLX5_FLOW_NAMESPACE_KERNEL), false);
900 /* The inner_ttc in the ttc params is intentionally not set */
901 mlx5e_set_ttc_params(priv->fs, priv->rx_res, &ttc_params, false);
903 if (rep->vport != MLX5_VPORT_UPLINK)
904 /* To give uplik rep TTC a lower level for chaining from root ft */
905 ttc_params.ft_attr.level = MLX5E_TTC_FT_LEVEL + 1;
907 mlx5e_fs_set_ttc(priv->fs, mlx5_create_ttc_table(priv->mdev, &ttc_params), false);
908 if (IS_ERR(mlx5e_fs_get_ttc(priv->fs, false))) {
909 err = PTR_ERR(mlx5e_fs_get_ttc(priv->fs, false));
910 netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n",
917 static int mlx5e_create_rep_root_ft(struct mlx5e_priv *priv)
919 struct mlx5e_rep_priv *rpriv = priv->ppriv;
920 struct mlx5_eswitch_rep *rep = rpriv->rep;
921 struct mlx5_flow_table_attr ft_attr = {};
922 struct mlx5_flow_namespace *ns;
925 if (rep->vport != MLX5_VPORT_UPLINK) {
926 /* non uplik reps will skip any bypass tables and go directly to
929 rpriv->root_ft = mlx5_get_ttc_flow_table(mlx5e_fs_get_ttc(priv->fs, false));
933 /* uplink root ft will be used to auto chain, to ethtool or ttc tables */
934 ns = mlx5_get_flow_namespace(priv->mdev, MLX5_FLOW_NAMESPACE_OFFLOADS);
936 netdev_err(priv->netdev, "Failed to get reps offloads namespace\n");
940 ft_attr.max_fte = 0; /* Empty table, miss rule will always point to next table */
944 rpriv->root_ft = mlx5_create_flow_table(ns, &ft_attr);
945 if (IS_ERR(rpriv->root_ft)) {
946 err = PTR_ERR(rpriv->root_ft);
947 rpriv->root_ft = NULL;
953 static void mlx5e_destroy_rep_root_ft(struct mlx5e_priv *priv)
955 struct mlx5e_rep_priv *rpriv = priv->ppriv;
956 struct mlx5_eswitch_rep *rep = rpriv->rep;
958 if (rep->vport != MLX5_VPORT_UPLINK)
960 mlx5_destroy_flow_table(rpriv->root_ft);
963 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
965 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
966 struct mlx5e_rep_priv *rpriv = priv->ppriv;
967 struct mlx5_eswitch_rep *rep = rpriv->rep;
968 struct mlx5_flow_handle *flow_rule;
969 struct mlx5_flow_destination dest;
971 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
972 dest.ft = rpriv->root_ft;
974 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw, rep->vport, &dest);
975 if (IS_ERR(flow_rule))
976 return PTR_ERR(flow_rule);
977 rpriv->vport_rx_rule = flow_rule;
981 static void rep_vport_rx_rule_destroy(struct mlx5e_priv *priv)
983 struct mlx5e_rep_priv *rpriv = priv->ppriv;
985 if (!rpriv->vport_rx_rule)
988 mlx5_del_flow_rules(rpriv->vport_rx_rule);
989 rpriv->vport_rx_rule = NULL;
992 int mlx5e_rep_bond_update(struct mlx5e_priv *priv, bool cleanup)
994 rep_vport_rx_rule_destroy(priv);
996 return cleanup ? 0 : mlx5e_create_rep_vport_rx_rule(priv);
999 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
1001 struct mlx5_core_dev *mdev = priv->mdev;
1004 priv->rx_res = mlx5e_rx_res_alloc();
1005 if (!priv->rx_res) {
1010 mlx5e_fs_init_l2_addr(priv->fs, priv->netdev);
1012 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
1014 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
1018 err = mlx5e_rx_res_init(priv->rx_res, priv->mdev, 0,
1019 priv->max_nch, priv->drop_rq.rqn,
1020 &priv->channels.params.packet_merge,
1021 priv->channels.params.num_channels);
1023 goto err_close_drop_rq;
1025 err = mlx5e_create_rep_ttc_table(priv);
1027 goto err_destroy_rx_res;
1029 err = mlx5e_create_rep_root_ft(priv);
1031 goto err_destroy_ttc_table;
1033 err = mlx5e_create_rep_vport_rx_rule(priv);
1035 goto err_destroy_root_ft;
1037 mlx5e_ethtool_init_steering(priv->fs);
1041 err_destroy_root_ft:
1042 mlx5e_destroy_rep_root_ft(priv);
1043 err_destroy_ttc_table:
1044 mlx5_destroy_ttc_table(mlx5e_fs_get_ttc(priv->fs, false));
1046 mlx5e_rx_res_destroy(priv->rx_res);
1048 mlx5e_close_drop_rq(&priv->drop_rq);
1049 mlx5e_rx_res_free(priv->rx_res);
1050 priv->rx_res = NULL;
1052 mlx5e_fs_cleanup(priv->fs);
1057 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
1059 mlx5e_ethtool_cleanup_steering(priv->fs);
1060 rep_vport_rx_rule_destroy(priv);
1061 mlx5e_destroy_rep_root_ft(priv);
1062 mlx5_destroy_ttc_table(mlx5e_fs_get_ttc(priv->fs, false));
1063 mlx5e_rx_res_destroy(priv->rx_res);
1064 mlx5e_close_drop_rq(&priv->drop_rq);
1065 mlx5e_rx_res_free(priv->rx_res);
1066 priv->rx_res = NULL;
1069 static void mlx5e_rep_mpesw_work(struct work_struct *work)
1071 struct mlx5_rep_uplink_priv *uplink_priv =
1072 container_of(work, struct mlx5_rep_uplink_priv,
1074 struct mlx5e_rep_priv *rpriv =
1075 container_of(uplink_priv, struct mlx5e_rep_priv,
1077 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1079 rep_vport_rx_rule_destroy(priv);
1080 mlx5e_create_rep_vport_rx_rule(priv);
1083 static int mlx5e_init_ul_rep_rx(struct mlx5e_priv *priv)
1085 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1088 mlx5e_create_q_counters(priv);
1089 err = mlx5e_init_rep_rx(priv);
1093 mlx5e_tc_int_port_init_rep_rx(priv);
1095 INIT_WORK(&rpriv->uplink_priv.mpesw_work, mlx5e_rep_mpesw_work);
1101 static void mlx5e_cleanup_ul_rep_rx(struct mlx5e_priv *priv)
1103 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1105 cancel_work_sync(&rpriv->uplink_priv.mpesw_work);
1106 mlx5e_tc_int_port_cleanup_rep_rx(priv);
1107 mlx5e_cleanup_rep_rx(priv);
1108 mlx5e_destroy_q_counters(priv);
1111 static int mlx5e_init_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
1113 struct mlx5_rep_uplink_priv *uplink_priv;
1114 struct net_device *netdev;
1115 struct mlx5e_priv *priv;
1118 netdev = rpriv->netdev;
1119 priv = netdev_priv(netdev);
1120 uplink_priv = &rpriv->uplink_priv;
1122 err = mlx5e_rep_tc_init(rpriv);
1126 mlx5_init_port_tun_entropy(&uplink_priv->tun_entropy, priv->mdev);
1128 mlx5e_rep_bond_init(rpriv);
1129 err = mlx5e_rep_tc_netdevice_event_register(rpriv);
1131 mlx5_core_err(priv->mdev, "Failed to register netdev notifier, err: %d\n",
1139 mlx5e_rep_bond_cleanup(rpriv);
1140 mlx5e_rep_tc_cleanup(rpriv);
1144 static void mlx5e_cleanup_uplink_rep_tx(struct mlx5e_rep_priv *rpriv)
1146 mlx5e_rep_tc_netdevice_event_unregister(rpriv);
1147 mlx5e_rep_bond_cleanup(rpriv);
1148 mlx5e_rep_tc_cleanup(rpriv);
1151 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1153 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1156 err = mlx5e_create_tises(priv);
1158 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1162 if (rpriv->rep->vport == MLX5_VPORT_UPLINK) {
1163 err = mlx5e_init_uplink_rep_tx(rpriv);
1168 err = mlx5e_tc_ht_init(&rpriv->tc_ht);
1175 if (rpriv->rep->vport == MLX5_VPORT_UPLINK)
1176 mlx5e_cleanup_uplink_rep_tx(rpriv);
1178 mlx5e_destroy_tises(priv);
1182 static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
1184 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1186 mlx5e_tc_ht_cleanup(&rpriv->tc_ht);
1188 if (rpriv->rep->vport == MLX5_VPORT_UPLINK)
1189 mlx5e_cleanup_uplink_rep_tx(rpriv);
1191 mlx5e_destroy_tises(priv);
1194 static void mlx5e_rep_enable(struct mlx5e_priv *priv)
1196 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1198 mlx5e_set_netdev_mtu_boundaries(priv);
1199 mlx5e_rep_neigh_init(rpriv);
1202 static void mlx5e_rep_disable(struct mlx5e_priv *priv)
1204 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1206 mlx5e_rep_neigh_cleanup(rpriv);
1209 static int mlx5e_update_rep_rx(struct mlx5e_priv *priv)
1214 static int mlx5e_rep_event_mpesw(struct mlx5e_priv *priv)
1216 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1217 struct mlx5_eswitch_rep *rep = rpriv->rep;
1219 if (rep->vport != MLX5_VPORT_UPLINK)
1222 queue_work(priv->wq, &rpriv->uplink_priv.mpesw_work);
1227 static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
1229 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
1231 if (event == MLX5_EVENT_TYPE_PORT_CHANGE) {
1232 struct mlx5_eqe *eqe = data;
1234 switch (eqe->sub_type) {
1235 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
1236 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
1237 queue_work(priv->wq, &priv->update_carrier_work);
1246 if (event == MLX5_DEV_EVENT_PORT_AFFINITY)
1247 return mlx5e_rep_tc_event_port_affinity(priv);
1248 else if (event == MLX5_DEV_EVENT_MULTIPORT_ESW)
1249 return mlx5e_rep_event_mpesw(priv);
1254 static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
1256 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1257 struct net_device *netdev = priv->netdev;
1258 struct mlx5_core_dev *mdev = priv->mdev;
1261 mlx5e_ipsec_init(priv);
1263 netdev->min_mtu = ETH_MIN_MTU;
1264 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
1265 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1266 mlx5e_set_dev_port_mtu(priv);
1268 mlx5e_rep_tc_enable(priv);
1270 if (MLX5_CAP_GEN(mdev, uplink_follow))
1271 mlx5_modify_vport_admin_state(mdev, MLX5_VPORT_STATE_OP_MOD_UPLINK,
1272 0, 0, MLX5_VPORT_ADMIN_STATE_AUTO);
1273 mlx5_lag_add_netdev(mdev, netdev);
1274 priv->events_nb.notifier_call = uplink_rep_async_event;
1275 mlx5_notifier_register(mdev, &priv->events_nb);
1276 mlx5e_dcbnl_initialize(priv);
1277 mlx5e_dcbnl_init_app(priv);
1278 mlx5e_rep_neigh_init(rpriv);
1279 mlx5e_rep_bridge_init(priv);
1281 netdev->wanted_features |= NETIF_F_HW_TC;
1284 if (netif_running(netdev))
1286 udp_tunnel_nic_reset_ntf(priv->netdev);
1287 netif_device_attach(netdev);
1291 static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
1293 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1294 struct mlx5_core_dev *mdev = priv->mdev;
1297 if (netif_running(priv->netdev))
1298 mlx5e_close(priv->netdev);
1299 netif_device_detach(priv->netdev);
1302 mlx5e_rep_bridge_cleanup(priv);
1303 mlx5e_rep_neigh_cleanup(rpriv);
1304 mlx5e_dcbnl_delete_app(priv);
1305 mlx5_notifier_unregister(mdev, &priv->events_nb);
1306 mlx5e_rep_tc_disable(priv);
1307 mlx5_lag_remove_netdev(mdev, priv->netdev);
1308 mlx5_vxlan_reset_to_default(mdev->vxlan);
1310 mlx5e_ipsec_cleanup(priv);
1313 static MLX5E_DEFINE_STATS_GRP(sw_rep, 0);
1314 static MLX5E_DEFINE_STATS_GRP(vport_rep, MLX5E_NDO_UPDATE_STATS);
1316 /* The stats groups order is opposite to the update_stats() order calls */
1317 static mlx5e_stats_grp_t mlx5e_rep_stats_grps[] = {
1318 &MLX5E_STATS_GRP(sw_rep),
1319 &MLX5E_STATS_GRP(vport_rep),
1322 static unsigned int mlx5e_rep_stats_grps_num(struct mlx5e_priv *priv)
1324 return ARRAY_SIZE(mlx5e_rep_stats_grps);
1327 /* The stats groups order is opposite to the update_stats() order calls */
1328 static mlx5e_stats_grp_t mlx5e_ul_rep_stats_grps[] = {
1329 &MLX5E_STATS_GRP(sw),
1330 &MLX5E_STATS_GRP(qcnt),
1331 &MLX5E_STATS_GRP(vnic_env),
1332 &MLX5E_STATS_GRP(vport),
1333 &MLX5E_STATS_GRP(802_3),
1334 &MLX5E_STATS_GRP(2863),
1335 &MLX5E_STATS_GRP(2819),
1336 &MLX5E_STATS_GRP(phy),
1337 &MLX5E_STATS_GRP(eth_ext),
1338 &MLX5E_STATS_GRP(pcie),
1339 &MLX5E_STATS_GRP(per_prio),
1340 &MLX5E_STATS_GRP(pme),
1341 &MLX5E_STATS_GRP(channels),
1342 &MLX5E_STATS_GRP(per_port_buff_congest),
1343 #ifdef CONFIG_MLX5_EN_IPSEC
1344 &MLX5E_STATS_GRP(ipsec_sw),
1346 &MLX5E_STATS_GRP(ptp),
1349 static unsigned int mlx5e_ul_rep_stats_grps_num(struct mlx5e_priv *priv)
1351 return ARRAY_SIZE(mlx5e_ul_rep_stats_grps);
1355 mlx5e_rep_vnic_reporter_diagnose(struct devlink_health_reporter *reporter,
1356 struct devlink_fmsg *fmsg,
1357 struct netlink_ext_ack *extack)
1359 struct mlx5e_rep_priv *rpriv = devlink_health_reporter_priv(reporter);
1360 struct mlx5_eswitch_rep *rep = rpriv->rep;
1362 return mlx5_reporter_vnic_diagnose_counters(rep->esw->dev, fmsg,
1366 static const struct devlink_health_reporter_ops mlx5_rep_vnic_reporter_ops = {
1368 .diagnose = mlx5e_rep_vnic_reporter_diagnose,
1371 static void mlx5e_rep_vnic_reporter_create(struct mlx5e_priv *priv,
1372 struct devlink_port *dl_port)
1374 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1375 struct devlink_health_reporter *reporter;
1377 reporter = devl_port_health_reporter_create(dl_port,
1378 &mlx5_rep_vnic_reporter_ops,
1380 if (IS_ERR(reporter)) {
1381 mlx5_core_err(priv->mdev,
1382 "Failed to create representor vnic reporter, err = %ld\n",
1387 rpriv->rep_vnic_reporter = reporter;
1390 static void mlx5e_rep_vnic_reporter_destroy(struct mlx5e_priv *priv)
1392 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1394 if (!IS_ERR_OR_NULL(rpriv->rep_vnic_reporter))
1395 devl_health_reporter_destroy(rpriv->rep_vnic_reporter);
1398 static const struct mlx5e_profile mlx5e_rep_profile = {
1399 .init = mlx5e_init_rep,
1400 .cleanup = mlx5e_cleanup_rep,
1401 .init_rx = mlx5e_init_rep_rx,
1402 .cleanup_rx = mlx5e_cleanup_rep_rx,
1403 .init_tx = mlx5e_init_rep_tx,
1404 .cleanup_tx = mlx5e_cleanup_rep_tx,
1405 .enable = mlx5e_rep_enable,
1406 .disable = mlx5e_rep_disable,
1407 .update_rx = mlx5e_update_rep_rx,
1408 .update_stats = mlx5e_stats_update_ndo_stats,
1409 .rx_handlers = &mlx5e_rx_handlers_rep,
1411 .stats_grps = mlx5e_rep_stats_grps,
1412 .stats_grps_num = mlx5e_rep_stats_grps_num,
1413 .max_nch_limit = mlx5e_rep_max_nch_limit,
1416 static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
1417 .init = mlx5e_init_ul_rep,
1418 .cleanup = mlx5e_cleanup_rep,
1419 .init_rx = mlx5e_init_ul_rep_rx,
1420 .cleanup_rx = mlx5e_cleanup_ul_rep_rx,
1421 .init_tx = mlx5e_init_rep_tx,
1422 .cleanup_tx = mlx5e_cleanup_rep_tx,
1423 .enable = mlx5e_uplink_rep_enable,
1424 .disable = mlx5e_uplink_rep_disable,
1425 .update_rx = mlx5e_update_rep_rx,
1426 .update_stats = mlx5e_stats_update_ndo_stats,
1427 .update_carrier = mlx5e_update_carrier,
1428 .rx_handlers = &mlx5e_rx_handlers_rep,
1429 .max_tc = MLX5E_MAX_NUM_TC,
1430 .stats_grps = mlx5e_ul_rep_stats_grps,
1431 .stats_grps_num = mlx5e_ul_rep_stats_grps_num,
1434 /* e-Switch vport representors */
1436 mlx5e_vport_uplink_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1438 struct mlx5e_priv *priv = netdev_priv(mlx5_uplink_netdev_get(dev));
1439 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1441 rpriv->netdev = priv->netdev;
1442 return mlx5e_netdev_change_profile(priv, &mlx5e_uplink_rep_profile,
1447 mlx5e_vport_uplink_rep_unload(struct mlx5e_rep_priv *rpriv)
1449 struct net_device *netdev = rpriv->netdev;
1450 struct mlx5e_priv *priv;
1452 priv = netdev_priv(netdev);
1454 mlx5e_netdev_attach_nic_profile(priv);
1458 mlx5e_vport_vf_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1460 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1461 const struct mlx5e_profile *profile;
1462 struct devlink_port *dl_port;
1463 struct net_device *netdev;
1464 struct mlx5e_priv *priv;
1467 profile = &mlx5e_rep_profile;
1468 netdev = mlx5e_create_netdev(dev, profile);
1471 "Failed to create representor netdev for vport %d\n",
1476 mlx5e_build_rep_netdev(netdev, dev);
1477 rpriv->netdev = netdev;
1479 priv = netdev_priv(netdev);
1480 priv->profile = profile;
1481 priv->ppriv = rpriv;
1482 err = profile->init(dev, netdev);
1484 netdev_warn(netdev, "rep profile init failed, %d\n", err);
1485 goto err_destroy_netdev;
1488 err = mlx5e_attach_netdev(netdev_priv(netdev));
1491 "Failed to attach representor netdev for vport %d\n",
1493 goto err_cleanup_profile;
1496 dl_port = mlx5_esw_offloads_devlink_port(dev->priv.eswitch,
1499 SET_NETDEV_DEVLINK_PORT(netdev, dl_port);
1500 mlx5e_rep_vnic_reporter_create(priv, dl_port);
1503 err = register_netdev(netdev);
1506 "Failed to register representor netdev for vport %d\n",
1508 goto err_detach_netdev;
1514 mlx5e_rep_vnic_reporter_destroy(priv);
1515 mlx5e_detach_netdev(netdev_priv(netdev));
1516 err_cleanup_profile:
1517 priv->profile->cleanup(priv);
1520 mlx5e_destroy_netdev(netdev_priv(netdev));
1525 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1527 struct mlx5e_rep_priv *rpriv;
1530 rpriv = kvzalloc(sizeof(*rpriv), GFP_KERNEL);
1534 /* rpriv->rep to be looked up when profile->init() is called */
1536 rep->rep_data[REP_ETH].priv = rpriv;
1537 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1539 if (rep->vport == MLX5_VPORT_UPLINK)
1540 err = mlx5e_vport_uplink_rep_load(dev, rep);
1542 err = mlx5e_vport_vf_rep_load(dev, rep);
1551 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1553 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1554 struct net_device *netdev = rpriv->netdev;
1555 struct mlx5e_priv *priv = netdev_priv(netdev);
1556 void *ppriv = priv->ppriv;
1558 if (rep->vport == MLX5_VPORT_UPLINK) {
1559 mlx5e_vport_uplink_rep_unload(rpriv);
1563 unregister_netdev(netdev);
1564 mlx5e_rep_vnic_reporter_destroy(priv);
1565 mlx5e_detach_netdev(priv);
1566 priv->profile->cleanup(priv);
1567 mlx5e_destroy_netdev(priv);
1569 kvfree(ppriv); /* mlx5e_rep_priv */
1572 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1574 struct mlx5e_rep_priv *rpriv;
1576 rpriv = mlx5e_rep_to_rep_priv(rep);
1578 return rpriv->netdev;
1581 static void mlx5e_vport_rep_event_unpair(struct mlx5_eswitch_rep *rep,
1582 struct mlx5_eswitch *peer_esw)
1584 u16 i = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
1585 struct mlx5e_rep_priv *rpriv;
1586 struct mlx5e_rep_sq *rep_sq;
1588 WARN_ON_ONCE(!peer_esw);
1589 rpriv = mlx5e_rep_to_rep_priv(rep);
1590 list_for_each_entry(rep_sq, &rpriv->vport_sqs_list, list) {
1591 struct mlx5e_rep_sq_peer *sq_peer = xa_load(&rep_sq->sq_peer, i);
1593 if (!sq_peer || sq_peer->peer != peer_esw)
1596 mlx5_eswitch_del_send_to_vport_rule(sq_peer->rule);
1597 xa_erase(&rep_sq->sq_peer, i);
1602 static int mlx5e_vport_rep_event_pair(struct mlx5_eswitch *esw,
1603 struct mlx5_eswitch_rep *rep,
1604 struct mlx5_eswitch *peer_esw)
1606 u16 i = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
1607 struct mlx5_flow_handle *flow_rule;
1608 struct mlx5e_rep_sq_peer *sq_peer;
1609 struct mlx5e_rep_priv *rpriv;
1610 struct mlx5e_rep_sq *rep_sq;
1613 rpriv = mlx5e_rep_to_rep_priv(rep);
1614 list_for_each_entry(rep_sq, &rpriv->vport_sqs_list, list) {
1615 sq_peer = xa_load(&rep_sq->sq_peer, i);
1617 if (sq_peer && sq_peer->peer)
1620 flow_rule = mlx5_eswitch_add_send_to_vport_rule(peer_esw, esw, rep,
1622 if (IS_ERR(flow_rule)) {
1623 err = PTR_ERR(flow_rule);
1628 sq_peer->rule = flow_rule;
1629 sq_peer->peer = peer_esw;
1632 sq_peer = kzalloc(sizeof(*sq_peer), GFP_KERNEL);
1637 err = xa_insert(&rep_sq->sq_peer, i, sq_peer, GFP_KERNEL);
1640 sq_peer->rule = flow_rule;
1641 sq_peer->peer = peer_esw;
1648 mlx5_eswitch_del_send_to_vport_rule(flow_rule);
1650 mlx5e_vport_rep_event_unpair(rep, peer_esw);
1654 static int mlx5e_vport_rep_event(struct mlx5_eswitch *esw,
1655 struct mlx5_eswitch_rep *rep,
1656 enum mlx5_switchdev_event event,
1661 if (event == MLX5_SWITCHDEV_EVENT_PAIR)
1662 err = mlx5e_vport_rep_event_pair(esw, rep, data);
1663 else if (event == MLX5_SWITCHDEV_EVENT_UNPAIR)
1664 mlx5e_vport_rep_event_unpair(rep, data);
1669 static const struct mlx5_eswitch_rep_ops rep_ops = {
1670 .load = mlx5e_vport_rep_load,
1671 .unload = mlx5e_vport_rep_unload,
1672 .get_proto_dev = mlx5e_vport_rep_get_proto_dev,
1673 .event = mlx5e_vport_rep_event,
1676 static int mlx5e_rep_probe(struct auxiliary_device *adev,
1677 const struct auxiliary_device_id *id)
1679 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev);
1680 struct mlx5_core_dev *mdev = edev->mdev;
1681 struct mlx5_eswitch *esw;
1683 esw = mdev->priv.eswitch;
1684 mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_ETH);
1688 static void mlx5e_rep_remove(struct auxiliary_device *adev)
1690 struct mlx5_adev *vdev = container_of(adev, struct mlx5_adev, adev);
1691 struct mlx5_core_dev *mdev = vdev->mdev;
1692 struct mlx5_eswitch *esw;
1694 esw = mdev->priv.eswitch;
1695 mlx5_eswitch_unregister_vport_reps(esw, REP_ETH);
1698 static const struct auxiliary_device_id mlx5e_rep_id_table[] = {
1699 { .name = MLX5_ADEV_NAME ".eth-rep", },
1703 MODULE_DEVICE_TABLE(auxiliary, mlx5e_rep_id_table);
1705 static struct auxiliary_driver mlx5e_rep_driver = {
1707 .probe = mlx5e_rep_probe,
1708 .remove = mlx5e_rep_remove,
1709 .id_table = mlx5e_rep_id_table,
1712 int mlx5e_rep_init(void)
1714 return auxiliary_driver_register(&mlx5e_rep_driver);
1717 void mlx5e_rep_cleanup(void)
1719 auxiliary_driver_unregister(&mlx5e_rep_driver);