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 <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/netevent.h>
45 #include "en/tc_tun.h"
48 #define MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE \
49 max(0x7, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)
50 #define MLX5E_REP_PARAMS_DEF_NUM_CHANNELS 1
52 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
54 struct mlx5e_rep_indr_block_priv {
55 struct net_device *netdev;
56 struct mlx5e_rep_priv *rpriv;
58 struct list_head list;
61 static void mlx5e_rep_indr_unregister_block(struct mlx5e_rep_priv *rpriv,
62 struct net_device *netdev);
64 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
65 struct ethtool_drvinfo *drvinfo)
67 strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
68 sizeof(drvinfo->driver));
69 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
72 static const struct counter_desc sw_rep_stats_desc[] = {
73 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
74 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
75 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
76 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
86 static const struct counter_desc vport_rep_stats_desc[] = {
87 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_packets) },
88 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_bytes) },
89 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_packets) },
90 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_bytes) },
93 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
94 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
96 static void mlx5e_rep_get_strings(struct net_device *dev,
97 u32 stringset, uint8_t *data)
103 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
104 strcpy(data + (i * ETH_GSTRING_LEN),
105 sw_rep_stats_desc[i].format);
106 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
107 strcpy(data + (i * ETH_GSTRING_LEN),
108 vport_rep_stats_desc[j].format);
113 static void mlx5e_vf_rep_update_hw_counters(struct mlx5e_priv *priv)
115 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
116 struct mlx5e_rep_priv *rpriv = priv->ppriv;
117 struct mlx5_eswitch_rep *rep = rpriv->rep;
118 struct rtnl_link_stats64 *vport_stats;
119 struct ifla_vf_stats vf_stats;
122 err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
124 pr_warn("vport %d error %d reading stats\n", rep->vport, err);
128 vport_stats = &priv->stats.vf_vport;
129 /* flip tx/rx as we are reporting the counters for the switch vport */
130 vport_stats->rx_packets = vf_stats.tx_packets;
131 vport_stats->rx_bytes = vf_stats.tx_bytes;
132 vport_stats->tx_packets = vf_stats.rx_packets;
133 vport_stats->tx_bytes = vf_stats.rx_bytes;
136 static void mlx5e_uplink_rep_update_hw_counters(struct mlx5e_priv *priv)
138 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
139 struct rtnl_link_stats64 *vport_stats;
141 mlx5e_grp_802_3_update_stats(priv);
143 vport_stats = &priv->stats.vf_vport;
145 vport_stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
146 vport_stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
147 vport_stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
148 vport_stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
151 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv *priv)
153 struct mlx5e_rep_priv *rpriv = priv->ppriv;
154 struct mlx5_eswitch_rep *rep = rpriv->rep;
156 if (rep->vport == FDB_UPLINK_VPORT)
157 mlx5e_uplink_rep_update_hw_counters(priv);
159 mlx5e_vf_rep_update_hw_counters(priv);
162 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv *priv)
164 struct mlx5e_sw_stats *s = &priv->stats.sw;
165 struct mlx5e_rq_stats *rq_stats;
166 struct mlx5e_sq_stats *sq_stats;
169 memset(s, 0, sizeof(*s));
170 for (i = 0; i < priv->channels.num; i++) {
171 struct mlx5e_channel *c = priv->channels.c[i];
173 rq_stats = c->rq.stats;
175 s->rx_packets += rq_stats->packets;
176 s->rx_bytes += rq_stats->bytes;
178 for (j = 0; j < priv->channels.params.num_tc; j++) {
179 sq_stats = c->sq[j].stats;
181 s->tx_packets += sq_stats->packets;
182 s->tx_bytes += sq_stats->bytes;
183 s->tx_queue_dropped += sq_stats->dropped;
188 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
189 struct ethtool_stats *stats, u64 *data)
191 struct mlx5e_priv *priv = netdev_priv(dev);
197 mutex_lock(&priv->state_lock);
198 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
199 mlx5e_rep_update_sw_counters(priv);
200 mlx5e_rep_update_hw_counters(priv);
201 mutex_unlock(&priv->state_lock);
203 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
204 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
205 sw_rep_stats_desc, i);
207 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
208 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
209 vport_rep_stats_desc, j);
212 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
216 return NUM_VPORT_REP_SW_COUNTERS + NUM_VPORT_REP_HW_COUNTERS;
222 static void mlx5e_rep_get_ringparam(struct net_device *dev,
223 struct ethtool_ringparam *param)
225 struct mlx5e_priv *priv = netdev_priv(dev);
227 mlx5e_ethtool_get_ringparam(priv, param);
230 static int mlx5e_rep_set_ringparam(struct net_device *dev,
231 struct ethtool_ringparam *param)
233 struct mlx5e_priv *priv = netdev_priv(dev);
235 return mlx5e_ethtool_set_ringparam(priv, param);
238 static int mlx5e_replace_rep_vport_rx_rule(struct mlx5e_priv *priv,
239 struct mlx5_flow_destination *dest)
241 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
242 struct mlx5e_rep_priv *rpriv = priv->ppriv;
243 struct mlx5_eswitch_rep *rep = rpriv->rep;
244 struct mlx5_flow_handle *flow_rule;
246 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
249 if (IS_ERR(flow_rule))
250 return PTR_ERR(flow_rule);
252 mlx5_del_flow_rules(rpriv->vport_rx_rule);
253 rpriv->vport_rx_rule = flow_rule;
257 static void mlx5e_rep_get_channels(struct net_device *dev,
258 struct ethtool_channels *ch)
260 struct mlx5e_priv *priv = netdev_priv(dev);
262 mlx5e_ethtool_get_channels(priv, ch);
265 static int mlx5e_rep_set_channels(struct net_device *dev,
266 struct ethtool_channels *ch)
268 struct mlx5e_priv *priv = netdev_priv(dev);
269 u16 curr_channels_amount = priv->channels.params.num_channels;
270 u32 new_channels_amount = ch->combined_count;
271 struct mlx5_flow_destination new_dest;
274 err = mlx5e_ethtool_set_channels(priv, ch);
278 if (curr_channels_amount == 1 && new_channels_amount > 1) {
279 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
280 new_dest.ft = priv->fs.ttc.ft.t;
281 } else if (new_channels_amount == 1 && curr_channels_amount > 1) {
282 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
283 new_dest.tir_num = priv->direct_tir[0].tirn;
288 err = mlx5e_replace_rep_vport_rx_rule(priv, &new_dest);
290 netdev_warn(priv->netdev, "Failed to update vport rx rule, when going from (%d) channels to (%d) channels\n",
291 curr_channels_amount, new_channels_amount);
298 static int mlx5e_rep_get_coalesce(struct net_device *netdev,
299 struct ethtool_coalesce *coal)
301 struct mlx5e_priv *priv = netdev_priv(netdev);
303 return mlx5e_ethtool_get_coalesce(priv, coal);
306 static int mlx5e_rep_set_coalesce(struct net_device *netdev,
307 struct ethtool_coalesce *coal)
309 struct mlx5e_priv *priv = netdev_priv(netdev);
311 return mlx5e_ethtool_set_coalesce(priv, coal);
314 static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
316 struct mlx5e_priv *priv = netdev_priv(netdev);
318 return mlx5e_ethtool_get_rxfh_key_size(priv);
321 static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
323 struct mlx5e_priv *priv = netdev_priv(netdev);
325 return mlx5e_ethtool_get_rxfh_indir_size(priv);
328 static void mlx5e_uplink_rep_get_pauseparam(struct net_device *netdev,
329 struct ethtool_pauseparam *pauseparam)
331 struct mlx5e_priv *priv = netdev_priv(netdev);
333 mlx5e_ethtool_get_pauseparam(priv, pauseparam);
336 static int mlx5e_uplink_rep_set_pauseparam(struct net_device *netdev,
337 struct ethtool_pauseparam *pauseparam)
339 struct mlx5e_priv *priv = netdev_priv(netdev);
341 return mlx5e_ethtool_set_pauseparam(priv, pauseparam);
344 static int mlx5e_uplink_rep_get_link_ksettings(struct net_device *netdev,
345 struct ethtool_link_ksettings *link_ksettings)
347 struct mlx5e_priv *priv = netdev_priv(netdev);
349 return mlx5e_ethtool_get_link_ksettings(priv, link_ksettings);
352 static int mlx5e_uplink_rep_set_link_ksettings(struct net_device *netdev,
353 const struct ethtool_link_ksettings *link_ksettings)
355 struct mlx5e_priv *priv = netdev_priv(netdev);
357 return mlx5e_ethtool_set_link_ksettings(priv, link_ksettings);
360 static const struct ethtool_ops mlx5e_vf_rep_ethtool_ops = {
361 .get_drvinfo = mlx5e_rep_get_drvinfo,
362 .get_link = ethtool_op_get_link,
363 .get_strings = mlx5e_rep_get_strings,
364 .get_sset_count = mlx5e_rep_get_sset_count,
365 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
366 .get_ringparam = mlx5e_rep_get_ringparam,
367 .set_ringparam = mlx5e_rep_set_ringparam,
368 .get_channels = mlx5e_rep_get_channels,
369 .set_channels = mlx5e_rep_set_channels,
370 .get_coalesce = mlx5e_rep_get_coalesce,
371 .set_coalesce = mlx5e_rep_set_coalesce,
372 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
373 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
376 static const struct ethtool_ops mlx5e_uplink_rep_ethtool_ops = {
377 .get_drvinfo = mlx5e_rep_get_drvinfo,
378 .get_link = ethtool_op_get_link,
379 .get_strings = mlx5e_rep_get_strings,
380 .get_sset_count = mlx5e_rep_get_sset_count,
381 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
382 .get_ringparam = mlx5e_rep_get_ringparam,
383 .set_ringparam = mlx5e_rep_set_ringparam,
384 .get_channels = mlx5e_rep_get_channels,
385 .set_channels = mlx5e_rep_set_channels,
386 .get_coalesce = mlx5e_rep_get_coalesce,
387 .set_coalesce = mlx5e_rep_set_coalesce,
388 .get_link_ksettings = mlx5e_uplink_rep_get_link_ksettings,
389 .set_link_ksettings = mlx5e_uplink_rep_set_link_ksettings,
390 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
391 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
392 .get_pauseparam = mlx5e_uplink_rep_get_pauseparam,
393 .set_pauseparam = mlx5e_uplink_rep_set_pauseparam,
396 static int mlx5e_attr_get(struct net_device *dev, struct switchdev_attr *attr)
398 struct mlx5e_priv *priv = netdev_priv(dev);
399 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
400 struct net_device *uplink_upper = NULL;
401 struct mlx5e_priv *uplink_priv = NULL;
402 struct net_device *uplink_dev;
404 if (esw->mode == SRIOV_NONE)
407 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
409 uplink_upper = netdev_master_upper_dev_get(uplink_dev);
410 uplink_priv = netdev_priv(uplink_dev);
414 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
415 attr->u.ppid.id_len = ETH_ALEN;
416 if (uplink_upper && mlx5_lag_is_sriov(uplink_priv->mdev)) {
417 ether_addr_copy(attr->u.ppid.id, uplink_upper->dev_addr);
419 struct mlx5e_rep_priv *rpriv = priv->ppriv;
420 struct mlx5_eswitch_rep *rep = rpriv->rep;
422 ether_addr_copy(attr->u.ppid.id, rep->hw_id);
432 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
433 struct mlx5_eswitch_rep *rep)
435 struct mlx5e_rep_sq *rep_sq, *tmp;
436 struct mlx5e_rep_priv *rpriv;
438 if (esw->mode != SRIOV_OFFLOADS)
441 rpriv = mlx5e_rep_to_rep_priv(rep);
442 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
443 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
444 list_del(&rep_sq->list);
449 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
450 struct mlx5_eswitch_rep *rep,
451 u32 *sqns_array, int sqns_num)
453 struct mlx5_flow_handle *flow_rule;
454 struct mlx5e_rep_priv *rpriv;
455 struct mlx5e_rep_sq *rep_sq;
459 if (esw->mode != SRIOV_OFFLOADS)
462 rpriv = mlx5e_rep_to_rep_priv(rep);
463 for (i = 0; i < sqns_num; i++) {
464 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
470 /* Add re-inject rule to the PF/representor sqs */
471 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
474 if (IS_ERR(flow_rule)) {
475 err = PTR_ERR(flow_rule);
479 rep_sq->send_to_vport_rule = flow_rule;
480 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
485 mlx5e_sqs2vport_stop(esw, rep);
489 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
491 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
492 struct mlx5e_rep_priv *rpriv = priv->ppriv;
493 struct mlx5_eswitch_rep *rep = rpriv->rep;
494 struct mlx5e_channel *c;
495 int n, tc, num_sqs = 0;
499 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(*sqs), GFP_KERNEL);
503 for (n = 0; n < priv->channels.num; n++) {
504 c = priv->channels.c[n];
505 for (tc = 0; tc < c->num_tc; tc++)
506 sqs[num_sqs++] = c->sq[tc].sqn;
509 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
514 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
518 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
520 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
521 struct mlx5e_rep_priv *rpriv = priv->ppriv;
522 struct mlx5_eswitch_rep *rep = rpriv->rep;
524 mlx5e_sqs2vport_stop(esw, rep);
527 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv *rpriv)
529 #if IS_ENABLED(CONFIG_IPV6)
530 unsigned long ipv6_interval = NEIGH_VAR(&nd_tbl.parms,
533 unsigned long ipv6_interval = ~0UL;
535 unsigned long ipv4_interval = NEIGH_VAR(&arp_tbl.parms,
537 struct net_device *netdev = rpriv->netdev;
538 struct mlx5e_priv *priv = netdev_priv(netdev);
540 rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);
541 mlx5_fc_update_sampling_interval(priv->mdev, rpriv->neigh_update.min_interval);
544 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv)
546 struct mlx5e_rep_priv *rpriv = priv->ppriv;
547 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
549 mlx5_fc_queue_stats_work(priv->mdev,
550 &neigh_update->neigh_stats_work,
551 neigh_update->min_interval);
554 static void mlx5e_rep_neigh_stats_work(struct work_struct *work)
556 struct mlx5e_rep_priv *rpriv = container_of(work, struct mlx5e_rep_priv,
557 neigh_update.neigh_stats_work.work);
558 struct net_device *netdev = rpriv->netdev;
559 struct mlx5e_priv *priv = netdev_priv(netdev);
560 struct mlx5e_neigh_hash_entry *nhe;
563 if (!list_empty(&rpriv->neigh_update.neigh_list))
564 mlx5e_rep_queue_neigh_stats_work(priv);
566 list_for_each_entry(nhe, &rpriv->neigh_update.neigh_list, neigh_list)
567 mlx5e_tc_update_neigh_used_value(nhe);
572 static void mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry *nhe)
574 refcount_inc(&nhe->refcnt);
577 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry *nhe)
579 if (refcount_dec_and_test(&nhe->refcnt))
583 static void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
584 struct mlx5e_encap_entry *e,
585 bool neigh_connected,
586 unsigned char ha[ETH_ALEN])
588 struct ethhdr *eth = (struct ethhdr *)e->encap_header;
592 if ((e->flags & MLX5_ENCAP_ENTRY_VALID) &&
593 (!neigh_connected || !ether_addr_equal(e->h_dest, ha)))
594 mlx5e_tc_encap_flows_del(priv, e);
596 if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
597 ether_addr_copy(e->h_dest, ha);
598 ether_addr_copy(eth->h_dest, ha);
599 /* Update the encap source mac, in case that we delete
600 * the flows when encap source mac changed.
602 ether_addr_copy(eth->h_source, e->route_dev->dev_addr);
604 mlx5e_tc_encap_flows_add(priv, e);
608 static void mlx5e_rep_neigh_update(struct work_struct *work)
610 struct mlx5e_neigh_hash_entry *nhe =
611 container_of(work, struct mlx5e_neigh_hash_entry, neigh_update_work);
612 struct neighbour *n = nhe->n;
613 struct mlx5e_encap_entry *e;
614 unsigned char ha[ETH_ALEN];
615 struct mlx5e_priv *priv;
616 bool neigh_connected;
617 bool encap_connected;
622 /* If these parameters are changed after we release the lock,
623 * we'll receive another event letting us know about it.
624 * We use this lock to avoid inconsistency between the neigh validity
625 * and it's hw address.
627 read_lock_bh(&n->lock);
628 memcpy(ha, n->ha, ETH_ALEN);
629 nud_state = n->nud_state;
631 read_unlock_bh(&n->lock);
633 neigh_connected = (nud_state & NUD_VALID) && !dead;
635 list_for_each_entry(e, &nhe->encap_list, encap_list) {
636 encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
637 priv = netdev_priv(e->out_dev);
639 if (encap_connected != neigh_connected ||
640 !ether_addr_equal(e->h_dest, ha))
641 mlx5e_rep_update_flows(priv, e, neigh_connected, ha);
643 mlx5e_rep_neigh_entry_release(nhe);
648 static struct mlx5e_rep_indr_block_priv *
649 mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv *rpriv,
650 struct net_device *netdev)
652 struct mlx5e_rep_indr_block_priv *cb_priv;
654 /* All callback list access should be protected by RTNL. */
657 list_for_each_entry(cb_priv,
658 &rpriv->uplink_priv.tc_indr_block_priv_list,
660 if (cb_priv->netdev == netdev)
666 static void mlx5e_rep_indr_clean_block_privs(struct mlx5e_rep_priv *rpriv)
668 struct mlx5e_rep_indr_block_priv *cb_priv, *temp;
669 struct list_head *head = &rpriv->uplink_priv.tc_indr_block_priv_list;
671 list_for_each_entry_safe(cb_priv, temp, head, list) {
672 mlx5e_rep_indr_unregister_block(rpriv, cb_priv->netdev);
678 mlx5e_rep_indr_offload(struct net_device *netdev,
679 struct tc_cls_flower_offload *flower,
680 struct mlx5e_rep_indr_block_priv *indr_priv)
682 struct mlx5e_priv *priv = netdev_priv(indr_priv->rpriv->netdev);
683 int flags = MLX5E_TC_EGRESS | MLX5E_TC_ESW_OFFLOAD;
686 switch (flower->command) {
687 case TC_CLSFLOWER_REPLACE:
688 err = mlx5e_configure_flower(netdev, priv, flower, flags);
690 case TC_CLSFLOWER_DESTROY:
691 err = mlx5e_delete_flower(netdev, priv, flower, flags);
693 case TC_CLSFLOWER_STATS:
694 err = mlx5e_stats_flower(netdev, priv, flower, flags);
703 static int mlx5e_rep_indr_setup_block_cb(enum tc_setup_type type,
704 void *type_data, void *indr_priv)
706 struct mlx5e_rep_indr_block_priv *priv = indr_priv;
709 case TC_SETUP_CLSFLOWER:
710 return mlx5e_rep_indr_offload(priv->netdev, type_data, priv);
717 mlx5e_rep_indr_setup_tc_block(struct net_device *netdev,
718 struct mlx5e_rep_priv *rpriv,
719 struct tc_block_offload *f)
721 struct mlx5e_rep_indr_block_priv *indr_priv;
724 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
727 switch (f->command) {
729 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
733 indr_priv = kmalloc(sizeof(*indr_priv), GFP_KERNEL);
737 indr_priv->netdev = netdev;
738 indr_priv->rpriv = rpriv;
739 list_add(&indr_priv->list,
740 &rpriv->uplink_priv.tc_indr_block_priv_list);
742 err = tcf_block_cb_register(f->block,
743 mlx5e_rep_indr_setup_block_cb,
744 indr_priv, indr_priv, f->extack);
746 list_del(&indr_priv->list);
751 case TC_BLOCK_UNBIND:
752 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
756 tcf_block_cb_unregister(f->block,
757 mlx5e_rep_indr_setup_block_cb,
759 list_del(&indr_priv->list);
770 int mlx5e_rep_indr_setup_tc_cb(struct net_device *netdev, void *cb_priv,
771 enum tc_setup_type type, void *type_data)
775 return mlx5e_rep_indr_setup_tc_block(netdev, cb_priv,
782 static int mlx5e_rep_indr_register_block(struct mlx5e_rep_priv *rpriv,
783 struct net_device *netdev)
787 err = __tc_indr_block_cb_register(netdev, rpriv,
788 mlx5e_rep_indr_setup_tc_cb,
791 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
793 mlx5_core_err(priv->mdev, "Failed to register remote block notifier for %s err=%d\n",
794 netdev_name(netdev), err);
799 static void mlx5e_rep_indr_unregister_block(struct mlx5e_rep_priv *rpriv,
800 struct net_device *netdev)
802 __tc_indr_block_cb_unregister(netdev, mlx5e_rep_indr_setup_tc_cb,
806 static int mlx5e_nic_rep_netdevice_event(struct notifier_block *nb,
807 unsigned long event, void *ptr)
809 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
810 uplink_priv.netdevice_nb);
811 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
812 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
814 if (!mlx5e_tc_tun_device_to_offload(priv, netdev))
818 case NETDEV_REGISTER:
819 mlx5e_rep_indr_register_block(rpriv, netdev);
821 case NETDEV_UNREGISTER:
822 mlx5e_rep_indr_unregister_block(rpriv, netdev);
828 static struct mlx5e_neigh_hash_entry *
829 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
830 struct mlx5e_neigh *m_neigh);
832 static int mlx5e_rep_netevent_event(struct notifier_block *nb,
833 unsigned long event, void *ptr)
835 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
836 neigh_update.netevent_nb);
837 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
838 struct net_device *netdev = rpriv->netdev;
839 struct mlx5e_priv *priv = netdev_priv(netdev);
840 struct mlx5e_neigh_hash_entry *nhe = NULL;
841 struct mlx5e_neigh m_neigh = {};
842 struct neigh_parms *p;
847 case NETEVENT_NEIGH_UPDATE:
849 #if IS_ENABLED(CONFIG_IPV6)
850 if (n->tbl != &nd_tbl && n->tbl != &arp_tbl)
852 if (n->tbl != &arp_tbl)
856 m_neigh.dev = n->dev;
857 m_neigh.family = n->ops->family;
858 memcpy(&m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
860 /* We are in atomic context and can't take RTNL mutex, so use
861 * spin_lock_bh to lookup the neigh table. bh is used since
862 * netevent can be called from a softirq context.
864 spin_lock_bh(&neigh_update->encap_lock);
865 nhe = mlx5e_rep_neigh_entry_lookup(priv, &m_neigh);
867 spin_unlock_bh(&neigh_update->encap_lock);
871 /* This assignment is valid as long as the the neigh reference
876 /* Take a reference to ensure the neighbour and mlx5 encap
877 * entry won't be destructed until we drop the reference in
881 mlx5e_rep_neigh_entry_hold(nhe);
883 if (!queue_work(priv->wq, &nhe->neigh_update_work)) {
884 mlx5e_rep_neigh_entry_release(nhe);
887 spin_unlock_bh(&neigh_update->encap_lock);
890 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
893 /* We check the device is present since we don't care about
894 * changes in the default table, we only care about changes
895 * done per device delay prob time parameter.
897 #if IS_ENABLED(CONFIG_IPV6)
898 if (!p->dev || (p->tbl != &nd_tbl && p->tbl != &arp_tbl))
900 if (!p->dev || p->tbl != &arp_tbl)
904 /* We are in atomic context and can't take RTNL mutex,
905 * so use spin_lock_bh to walk the neigh list and look for
906 * the relevant device. bh is used since netevent can be
907 * called from a softirq context.
909 spin_lock_bh(&neigh_update->encap_lock);
910 list_for_each_entry(nhe, &neigh_update->neigh_list, neigh_list) {
911 if (p->dev == nhe->m_neigh.dev) {
916 spin_unlock_bh(&neigh_update->encap_lock);
920 neigh_update->min_interval = min_t(unsigned long,
921 NEIGH_VAR(p, DELAY_PROBE_TIME),
922 neigh_update->min_interval);
923 mlx5_fc_update_sampling_interval(priv->mdev,
924 neigh_update->min_interval);
930 static const struct rhashtable_params mlx5e_neigh_ht_params = {
931 .head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
932 .key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
933 .key_len = sizeof(struct mlx5e_neigh),
934 .automatic_shrinking = true,
937 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv *rpriv)
939 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
942 err = rhashtable_init(&neigh_update->neigh_ht, &mlx5e_neigh_ht_params);
946 INIT_LIST_HEAD(&neigh_update->neigh_list);
947 spin_lock_init(&neigh_update->encap_lock);
948 INIT_DELAYED_WORK(&neigh_update->neigh_stats_work,
949 mlx5e_rep_neigh_stats_work);
950 mlx5e_rep_neigh_update_init_interval(rpriv);
952 rpriv->neigh_update.netevent_nb.notifier_call = mlx5e_rep_netevent_event;
953 err = register_netevent_notifier(&rpriv->neigh_update.netevent_nb);
959 rhashtable_destroy(&neigh_update->neigh_ht);
963 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv *rpriv)
965 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
966 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
968 unregister_netevent_notifier(&neigh_update->netevent_nb);
970 flush_workqueue(priv->wq); /* flush neigh update works */
972 cancel_delayed_work_sync(&rpriv->neigh_update.neigh_stats_work);
974 rhashtable_destroy(&neigh_update->neigh_ht);
977 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv *priv,
978 struct mlx5e_neigh_hash_entry *nhe)
980 struct mlx5e_rep_priv *rpriv = priv->ppriv;
983 err = rhashtable_insert_fast(&rpriv->neigh_update.neigh_ht,
985 mlx5e_neigh_ht_params);
989 list_add(&nhe->neigh_list, &rpriv->neigh_update.neigh_list);
994 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_priv *priv,
995 struct mlx5e_neigh_hash_entry *nhe)
997 struct mlx5e_rep_priv *rpriv = priv->ppriv;
999 spin_lock_bh(&rpriv->neigh_update.encap_lock);
1001 list_del(&nhe->neigh_list);
1003 rhashtable_remove_fast(&rpriv->neigh_update.neigh_ht,
1005 mlx5e_neigh_ht_params);
1006 spin_unlock_bh(&rpriv->neigh_update.encap_lock);
1009 /* This function must only be called under RTNL lock or under the
1010 * representor's encap_lock in case RTNL mutex can't be held.
1012 static struct mlx5e_neigh_hash_entry *
1013 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
1014 struct mlx5e_neigh *m_neigh)
1016 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1017 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
1019 return rhashtable_lookup_fast(&neigh_update->neigh_ht, m_neigh,
1020 mlx5e_neigh_ht_params);
1023 static int mlx5e_rep_neigh_entry_create(struct mlx5e_priv *priv,
1024 struct mlx5e_encap_entry *e,
1025 struct mlx5e_neigh_hash_entry **nhe)
1029 *nhe = kzalloc(sizeof(**nhe), GFP_KERNEL);
1033 memcpy(&(*nhe)->m_neigh, &e->m_neigh, sizeof(e->m_neigh));
1034 INIT_WORK(&(*nhe)->neigh_update_work, mlx5e_rep_neigh_update);
1035 INIT_LIST_HEAD(&(*nhe)->encap_list);
1036 refcount_set(&(*nhe)->refcnt, 1);
1038 err = mlx5e_rep_neigh_entry_insert(priv, *nhe);
1048 static void mlx5e_rep_neigh_entry_destroy(struct mlx5e_priv *priv,
1049 struct mlx5e_neigh_hash_entry *nhe)
1051 /* The neigh hash entry must be removed from the hash table regardless
1052 * of the reference count value, so it won't be found by the next
1053 * neigh notification call. The neigh hash entry reference count is
1054 * incremented only during creation and neigh notification calls and
1055 * protects from freeing the nhe struct.
1057 mlx5e_rep_neigh_entry_remove(priv, nhe);
1058 mlx5e_rep_neigh_entry_release(nhe);
1061 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
1062 struct mlx5e_encap_entry *e)
1064 struct mlx5e_neigh_hash_entry *nhe;
1067 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
1069 err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
1073 list_add(&e->encap_list, &nhe->encap_list);
1077 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
1078 struct mlx5e_encap_entry *e)
1080 struct mlx5e_neigh_hash_entry *nhe;
1082 list_del(&e->encap_list);
1083 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
1085 if (list_empty(&nhe->encap_list))
1086 mlx5e_rep_neigh_entry_destroy(priv, nhe);
1089 static int mlx5e_vf_rep_open(struct net_device *dev)
1091 struct mlx5e_priv *priv = netdev_priv(dev);
1092 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1093 struct mlx5_eswitch_rep *rep = rpriv->rep;
1096 mutex_lock(&priv->state_lock);
1097 err = mlx5e_open_locked(dev);
1101 if (!mlx5_modify_vport_admin_state(priv->mdev,
1102 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1103 rep->vport, MLX5_VPORT_ADMIN_STATE_UP))
1104 netif_carrier_on(dev);
1107 mutex_unlock(&priv->state_lock);
1111 static int mlx5e_vf_rep_close(struct net_device *dev)
1113 struct mlx5e_priv *priv = netdev_priv(dev);
1114 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1115 struct mlx5_eswitch_rep *rep = rpriv->rep;
1118 mutex_lock(&priv->state_lock);
1119 mlx5_modify_vport_admin_state(priv->mdev,
1120 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1121 rep->vport, MLX5_VPORT_ADMIN_STATE_DOWN);
1122 ret = mlx5e_close_locked(dev);
1123 mutex_unlock(&priv->state_lock);
1127 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
1128 char *buf, size_t len)
1130 struct mlx5e_priv *priv = netdev_priv(dev);
1131 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1132 struct mlx5_eswitch_rep *rep = rpriv->rep;
1135 ret = mlx5_lag_get_pf_num(priv->mdev, &pf_num);
1139 if (rep->vport == FDB_UPLINK_VPORT)
1140 ret = snprintf(buf, len, "p%d", pf_num);
1142 ret = snprintf(buf, len, "pf%dvf%d", pf_num, rep->vport - 1);
1151 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
1152 struct tc_cls_flower_offload *cls_flower, int flags)
1154 switch (cls_flower->command) {
1155 case TC_CLSFLOWER_REPLACE:
1156 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
1158 case TC_CLSFLOWER_DESTROY:
1159 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
1161 case TC_CLSFLOWER_STATS:
1162 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
1169 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
1172 struct mlx5e_priv *priv = cb_priv;
1175 case TC_SETUP_CLSFLOWER:
1176 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS |
1177 MLX5E_TC_ESW_OFFLOAD);
1183 static int mlx5e_rep_setup_tc_block(struct net_device *dev,
1184 struct tc_block_offload *f)
1186 struct mlx5e_priv *priv = netdev_priv(dev);
1188 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1191 switch (f->command) {
1193 return tcf_block_cb_register(f->block, mlx5e_rep_setup_tc_cb,
1194 priv, priv, f->extack);
1195 case TC_BLOCK_UNBIND:
1196 tcf_block_cb_unregister(f->block, mlx5e_rep_setup_tc_cb, priv);
1203 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
1207 case TC_SETUP_BLOCK:
1208 return mlx5e_rep_setup_tc_block(dev, type_data);
1214 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
1216 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1217 struct mlx5_eswitch_rep *rep;
1219 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
1222 if (!rpriv) /* non vport rep mlx5e instances don't use this field */
1226 return (rep->vport == FDB_UPLINK_VPORT);
1229 static bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
1232 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1240 mlx5e_get_sw_stats64(const struct net_device *dev,
1241 struct rtnl_link_stats64 *stats)
1243 struct mlx5e_priv *priv = netdev_priv(dev);
1244 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
1246 mlx5e_rep_update_sw_counters(priv);
1248 stats->rx_packets = sstats->rx_packets;
1249 stats->rx_bytes = sstats->rx_bytes;
1250 stats->tx_packets = sstats->tx_packets;
1251 stats->tx_bytes = sstats->tx_bytes;
1253 stats->tx_dropped = sstats->tx_queue_dropped;
1258 static int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
1262 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1263 return mlx5e_get_sw_stats64(dev, sp);
1270 mlx5e_vf_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
1272 struct mlx5e_priv *priv = netdev_priv(dev);
1274 /* update HW stats in background for next time */
1275 mlx5e_queue_update_stats(priv);
1276 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
1279 static int mlx5e_vf_rep_change_mtu(struct net_device *netdev, int new_mtu)
1281 return mlx5e_change_mtu(netdev, new_mtu, NULL);
1284 static int mlx5e_uplink_rep_change_mtu(struct net_device *netdev, int new_mtu)
1286 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
1289 static int mlx5e_uplink_rep_set_mac(struct net_device *netdev, void *addr)
1291 struct sockaddr *saddr = addr;
1293 if (!is_valid_ether_addr(saddr->sa_data))
1294 return -EADDRNOTAVAIL;
1296 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
1300 static int mlx5e_uplink_rep_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
1303 netdev_warn_once(dev, "legacy vf vlan setting isn't supported in switchdev mode\n");
1308 /* allow setting 0-vid for compatibility with libvirt */
1312 static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
1313 .switchdev_port_attr_get = mlx5e_attr_get,
1316 static const struct net_device_ops mlx5e_netdev_ops_vf_rep = {
1317 .ndo_open = mlx5e_vf_rep_open,
1318 .ndo_stop = mlx5e_vf_rep_close,
1319 .ndo_start_xmit = mlx5e_xmit,
1320 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
1321 .ndo_setup_tc = mlx5e_rep_setup_tc,
1322 .ndo_get_stats64 = mlx5e_vf_rep_get_stats,
1323 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
1324 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
1325 .ndo_change_mtu = mlx5e_vf_rep_change_mtu,
1328 static const struct net_device_ops mlx5e_netdev_ops_uplink_rep = {
1329 .ndo_open = mlx5e_open,
1330 .ndo_stop = mlx5e_close,
1331 .ndo_start_xmit = mlx5e_xmit,
1332 .ndo_set_mac_address = mlx5e_uplink_rep_set_mac,
1333 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
1334 .ndo_setup_tc = mlx5e_rep_setup_tc,
1335 .ndo_get_stats64 = mlx5e_get_stats,
1336 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
1337 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
1338 .ndo_change_mtu = mlx5e_uplink_rep_change_mtu,
1339 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
1340 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
1341 .ndo_features_check = mlx5e_features_check,
1342 .ndo_set_vf_mac = mlx5e_set_vf_mac,
1343 .ndo_set_vf_rate = mlx5e_set_vf_rate,
1344 .ndo_get_vf_config = mlx5e_get_vf_config,
1345 .ndo_get_vf_stats = mlx5e_get_vf_stats,
1346 .ndo_set_vf_vlan = mlx5e_uplink_rep_set_vf_vlan,
1349 bool mlx5e_eswitch_rep(struct net_device *netdev)
1351 if (netdev->netdev_ops == &mlx5e_netdev_ops_vf_rep ||
1352 netdev->netdev_ops == &mlx5e_netdev_ops_uplink_rep)
1358 static void mlx5e_build_rep_params(struct net_device *netdev)
1360 struct mlx5e_priv *priv = netdev_priv(netdev);
1361 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1362 struct mlx5_eswitch_rep *rep = rpriv->rep;
1363 struct mlx5_core_dev *mdev = priv->mdev;
1364 struct mlx5e_params *params;
1366 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
1367 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
1368 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1370 params = &priv->channels.params;
1371 params->hard_mtu = MLX5E_ETH_HARD_MTU;
1372 params->sw_mtu = netdev->mtu;
1375 if (rep->vport == FDB_UPLINK_VPORT)
1376 params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
1378 params->log_sq_size = MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE;
1381 mlx5e_build_rq_params(mdev, params);
1383 /* CQ moderation params */
1384 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
1385 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
1389 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
1392 mlx5e_build_rss_params(&priv->rss_params, params->num_channels);
1395 static void mlx5e_build_rep_netdev(struct net_device *netdev)
1397 struct mlx5e_priv *priv = netdev_priv(netdev);
1398 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1399 struct mlx5_eswitch_rep *rep = rpriv->rep;
1400 struct mlx5_core_dev *mdev = priv->mdev;
1402 if (rep->vport == FDB_UPLINK_VPORT) {
1403 SET_NETDEV_DEV(netdev, &priv->mdev->pdev->dev);
1404 netdev->netdev_ops = &mlx5e_netdev_ops_uplink_rep;
1405 /* we want a persistent mac for the uplink rep */
1406 mlx5_query_nic_vport_mac_address(mdev, 0, netdev->dev_addr);
1407 netdev->ethtool_ops = &mlx5e_uplink_rep_ethtool_ops;
1408 #ifdef CONFIG_MLX5_CORE_EN_DCB
1409 if (MLX5_CAP_GEN(mdev, qos))
1410 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
1413 netdev->netdev_ops = &mlx5e_netdev_ops_vf_rep;
1414 eth_hw_addr_random(netdev);
1415 netdev->ethtool_ops = &mlx5e_vf_rep_ethtool_ops;
1418 netdev->watchdog_timeo = 15 * HZ;
1421 netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
1423 netdev->features |= NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
1424 netdev->hw_features |= NETIF_F_HW_TC;
1426 netdev->hw_features |= NETIF_F_SG;
1427 netdev->hw_features |= NETIF_F_IP_CSUM;
1428 netdev->hw_features |= NETIF_F_IPV6_CSUM;
1429 netdev->hw_features |= NETIF_F_GRO;
1430 netdev->hw_features |= NETIF_F_TSO;
1431 netdev->hw_features |= NETIF_F_TSO6;
1432 netdev->hw_features |= NETIF_F_RXCSUM;
1434 if (rep->vport != FDB_UPLINK_VPORT)
1435 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1437 netdev->features |= netdev->hw_features;
1440 static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
1441 struct net_device *netdev,
1442 const struct mlx5e_profile *profile,
1445 struct mlx5e_priv *priv = netdev_priv(netdev);
1448 err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
1452 priv->channels.params.num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;
1454 mlx5e_build_rep_params(netdev);
1455 mlx5e_build_rep_netdev(netdev);
1457 mlx5e_timestamp_init(priv);
1462 static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
1464 mlx5e_netdev_cleanup(priv->netdev, priv);
1467 static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
1469 struct ttc_params ttc_params = {};
1472 priv->fs.ns = mlx5_get_flow_namespace(priv->mdev,
1473 MLX5_FLOW_NAMESPACE_KERNEL);
1475 /* The inner_ttc in the ttc params is intentionally not set */
1476 ttc_params.any_tt_tirn = priv->direct_tir[0].tirn;
1477 mlx5e_set_ttc_ft_params(&ttc_params);
1478 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
1479 ttc_params.indir_tirn[tt] = priv->indir_tir[tt].tirn;
1481 err = mlx5e_create_ttc_table(priv, &ttc_params, &priv->fs.ttc);
1483 netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n", err);
1489 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
1491 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1492 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1493 struct mlx5_eswitch_rep *rep = rpriv->rep;
1494 struct mlx5_flow_handle *flow_rule;
1495 struct mlx5_flow_destination dest;
1497 dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1498 dest.tir_num = priv->direct_tir[0].tirn;
1499 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
1502 if (IS_ERR(flow_rule))
1503 return PTR_ERR(flow_rule);
1504 rpriv->vport_rx_rule = flow_rule;
1508 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
1510 struct mlx5_core_dev *mdev = priv->mdev;
1513 mlx5e_init_l2_addr(priv);
1515 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
1517 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
1521 err = mlx5e_create_indirect_rqt(priv);
1523 goto err_close_drop_rq;
1525 err = mlx5e_create_direct_rqts(priv);
1527 goto err_destroy_indirect_rqts;
1529 err = mlx5e_create_indirect_tirs(priv, false);
1531 goto err_destroy_direct_rqts;
1533 err = mlx5e_create_direct_tirs(priv);
1535 goto err_destroy_indirect_tirs;
1537 err = mlx5e_create_rep_ttc_table(priv);
1539 goto err_destroy_direct_tirs;
1541 err = mlx5e_create_rep_vport_rx_rule(priv);
1543 goto err_destroy_ttc_table;
1547 err_destroy_ttc_table:
1548 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1549 err_destroy_direct_tirs:
1550 mlx5e_destroy_direct_tirs(priv);
1551 err_destroy_indirect_tirs:
1552 mlx5e_destroy_indirect_tirs(priv, false);
1553 err_destroy_direct_rqts:
1554 mlx5e_destroy_direct_rqts(priv);
1555 err_destroy_indirect_rqts:
1556 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1558 mlx5e_close_drop_rq(&priv->drop_rq);
1562 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
1564 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1566 mlx5_del_flow_rules(rpriv->vport_rx_rule);
1567 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1568 mlx5e_destroy_direct_tirs(priv);
1569 mlx5e_destroy_indirect_tirs(priv, false);
1570 mlx5e_destroy_direct_rqts(priv);
1571 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1572 mlx5e_close_drop_rq(&priv->drop_rq);
1575 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1577 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1578 struct mlx5_rep_uplink_priv *uplink_priv;
1581 err = mlx5e_create_tises(priv);
1583 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1587 if (rpriv->rep->vport == FDB_UPLINK_VPORT) {
1588 uplink_priv = &rpriv->uplink_priv;
1590 /* init shared tc flow table */
1591 err = mlx5e_tc_esw_init(&uplink_priv->tc_ht);
1595 /* init indirect block notifications */
1596 INIT_LIST_HEAD(&uplink_priv->tc_indr_block_priv_list);
1597 uplink_priv->netdevice_nb.notifier_call = mlx5e_nic_rep_netdevice_event;
1598 err = register_netdevice_notifier(&uplink_priv->netdevice_nb);
1600 mlx5_core_err(priv->mdev, "Failed to register netdev notifier\n");
1601 goto tc_esw_cleanup;
1608 mlx5e_tc_esw_cleanup(&uplink_priv->tc_ht);
1610 for (tc = 0; tc < priv->profile->max_tc; tc++)
1611 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
1615 static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
1617 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1620 for (tc = 0; tc < priv->profile->max_tc; tc++)
1621 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
1623 if (rpriv->rep->vport == FDB_UPLINK_VPORT) {
1624 /* clean indirect TC block notifications */
1625 unregister_netdevice_notifier(&rpriv->uplink_priv.netdevice_nb);
1626 mlx5e_rep_indr_clean_block_privs(rpriv);
1628 /* delete shared tc flow table */
1629 mlx5e_tc_esw_cleanup(&rpriv->uplink_priv.tc_ht);
1633 static void mlx5e_vf_rep_enable(struct mlx5e_priv *priv)
1635 struct net_device *netdev = priv->netdev;
1636 struct mlx5_core_dev *mdev = priv->mdev;
1639 netdev->min_mtu = ETH_MIN_MTU;
1640 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
1641 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1644 static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
1646 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
1647 struct mlx5_eqe *eqe = data;
1649 if (event != MLX5_EVENT_TYPE_PORT_CHANGE)
1652 switch (eqe->sub_type) {
1653 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
1654 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
1655 queue_work(priv->wq, &priv->update_carrier_work);
1664 static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
1666 struct net_device *netdev = priv->netdev;
1667 struct mlx5_core_dev *mdev = priv->mdev;
1670 netdev->min_mtu = ETH_MIN_MTU;
1671 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
1672 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1673 mlx5e_set_dev_port_mtu(priv);
1675 mlx5_lag_add(mdev, netdev);
1676 priv->events_nb.notifier_call = uplink_rep_async_event;
1677 mlx5_notifier_register(mdev, &priv->events_nb);
1678 #ifdef CONFIG_MLX5_CORE_EN_DCB
1679 mlx5e_dcbnl_initialize(priv);
1680 mlx5e_dcbnl_init_app(priv);
1684 static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
1686 struct mlx5_core_dev *mdev = priv->mdev;
1688 #ifdef CONFIG_MLX5_CORE_EN_DCB
1689 mlx5e_dcbnl_delete_app(priv);
1691 mlx5_notifier_unregister(mdev, &priv->events_nb);
1692 mlx5_lag_remove(mdev);
1695 static const struct mlx5e_profile mlx5e_vf_rep_profile = {
1696 .init = mlx5e_init_rep,
1697 .cleanup = mlx5e_cleanup_rep,
1698 .init_rx = mlx5e_init_rep_rx,
1699 .cleanup_rx = mlx5e_cleanup_rep_rx,
1700 .init_tx = mlx5e_init_rep_tx,
1701 .cleanup_tx = mlx5e_cleanup_rep_tx,
1702 .enable = mlx5e_vf_rep_enable,
1703 .update_stats = mlx5e_vf_rep_update_hw_counters,
1704 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1705 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1709 static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
1710 .init = mlx5e_init_rep,
1711 .cleanup = mlx5e_cleanup_rep,
1712 .init_rx = mlx5e_init_rep_rx,
1713 .cleanup_rx = mlx5e_cleanup_rep_rx,
1714 .init_tx = mlx5e_init_rep_tx,
1715 .cleanup_tx = mlx5e_cleanup_rep_tx,
1716 .enable = mlx5e_uplink_rep_enable,
1717 .disable = mlx5e_uplink_rep_disable,
1718 .update_stats = mlx5e_uplink_rep_update_hw_counters,
1719 .update_carrier = mlx5e_update_carrier,
1720 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1721 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1722 .max_tc = MLX5E_MAX_NUM_TC,
1725 /* e-Switch vport representors */
1727 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1729 const struct mlx5e_profile *profile;
1730 struct mlx5e_rep_priv *rpriv;
1731 struct net_device *netdev;
1734 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1738 /* rpriv->rep to be looked up when profile->init() is called */
1741 nch = mlx5e_get_max_num_channels(dev);
1742 profile = (rep->vport == FDB_UPLINK_VPORT) ? &mlx5e_uplink_rep_profile : &mlx5e_vf_rep_profile;
1743 netdev = mlx5e_create_netdev(dev, profile, nch, rpriv);
1745 pr_warn("Failed to create representor netdev for vport %d\n",
1751 rpriv->netdev = netdev;
1752 rep->rep_if[REP_ETH].priv = rpriv;
1753 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1755 if (rep->vport == FDB_UPLINK_VPORT) {
1756 err = mlx5e_create_mdev_resources(dev);
1758 goto err_destroy_netdev;
1761 err = mlx5e_attach_netdev(netdev_priv(netdev));
1763 pr_warn("Failed to attach representor netdev for vport %d\n",
1765 goto err_destroy_mdev_resources;
1768 err = mlx5e_rep_neigh_init(rpriv);
1770 pr_warn("Failed to initialized neighbours handling for vport %d\n",
1772 goto err_detach_netdev;
1775 err = register_netdev(netdev);
1777 pr_warn("Failed to register representor netdev for vport %d\n",
1779 goto err_neigh_cleanup;
1785 mlx5e_rep_neigh_cleanup(rpriv);
1788 mlx5e_detach_netdev(netdev_priv(netdev));
1790 err_destroy_mdev_resources:
1791 if (rep->vport == FDB_UPLINK_VPORT)
1792 mlx5e_destroy_mdev_resources(dev);
1795 mlx5e_destroy_netdev(netdev_priv(netdev));
1801 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1803 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1804 struct net_device *netdev = rpriv->netdev;
1805 struct mlx5e_priv *priv = netdev_priv(netdev);
1806 void *ppriv = priv->ppriv;
1808 unregister_netdev(netdev);
1809 mlx5e_rep_neigh_cleanup(rpriv);
1810 mlx5e_detach_netdev(priv);
1811 if (rep->vport == FDB_UPLINK_VPORT)
1812 mlx5e_destroy_mdev_resources(priv->mdev);
1813 mlx5e_destroy_netdev(priv);
1814 kfree(ppriv); /* mlx5e_rep_priv */
1817 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1819 struct mlx5e_rep_priv *rpriv;
1821 rpriv = mlx5e_rep_to_rep_priv(rep);
1823 return rpriv->netdev;
1826 void mlx5e_rep_register_vport_reps(struct mlx5_core_dev *mdev)
1828 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1829 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1832 for (vport = 0; vport < total_vfs; vport++) {
1833 struct mlx5_eswitch_rep_if rep_if = {};
1835 rep_if.load = mlx5e_vport_rep_load;
1836 rep_if.unload = mlx5e_vport_rep_unload;
1837 rep_if.get_proto_dev = mlx5e_vport_rep_get_proto_dev;
1838 mlx5_eswitch_register_vport_rep(esw, vport, &rep_if, REP_ETH);
1842 void mlx5e_rep_unregister_vport_reps(struct mlx5_core_dev *mdev)
1844 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1845 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1848 for (vport = total_vfs - 1; vport >= 0; vport--)
1849 mlx5_eswitch_unregister_vport_rep(esw, vport, REP_ETH);