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 rtnl_link_stats64 stats64 = {};
167 memset(s, 0, sizeof(*s));
168 mlx5e_fold_sw_stats64(priv, &stats64);
170 s->rx_packets = stats64.rx_packets;
171 s->rx_bytes = stats64.rx_bytes;
172 s->tx_packets = stats64.tx_packets;
173 s->tx_bytes = stats64.tx_bytes;
174 s->tx_queue_dropped = stats64.tx_dropped;
177 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
178 struct ethtool_stats *stats, u64 *data)
180 struct mlx5e_priv *priv = netdev_priv(dev);
186 mutex_lock(&priv->state_lock);
187 mlx5e_rep_update_sw_counters(priv);
188 mlx5e_rep_update_hw_counters(priv);
189 mutex_unlock(&priv->state_lock);
191 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
192 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
193 sw_rep_stats_desc, i);
195 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
196 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
197 vport_rep_stats_desc, j);
200 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
204 return NUM_VPORT_REP_SW_COUNTERS + NUM_VPORT_REP_HW_COUNTERS;
210 static void mlx5e_rep_get_ringparam(struct net_device *dev,
211 struct ethtool_ringparam *param)
213 struct mlx5e_priv *priv = netdev_priv(dev);
215 mlx5e_ethtool_get_ringparam(priv, param);
218 static int mlx5e_rep_set_ringparam(struct net_device *dev,
219 struct ethtool_ringparam *param)
221 struct mlx5e_priv *priv = netdev_priv(dev);
223 return mlx5e_ethtool_set_ringparam(priv, param);
226 static int mlx5e_replace_rep_vport_rx_rule(struct mlx5e_priv *priv,
227 struct mlx5_flow_destination *dest)
229 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
230 struct mlx5e_rep_priv *rpriv = priv->ppriv;
231 struct mlx5_eswitch_rep *rep = rpriv->rep;
232 struct mlx5_flow_handle *flow_rule;
234 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
237 if (IS_ERR(flow_rule))
238 return PTR_ERR(flow_rule);
240 mlx5_del_flow_rules(rpriv->vport_rx_rule);
241 rpriv->vport_rx_rule = flow_rule;
245 static void mlx5e_rep_get_channels(struct net_device *dev,
246 struct ethtool_channels *ch)
248 struct mlx5e_priv *priv = netdev_priv(dev);
250 mlx5e_ethtool_get_channels(priv, ch);
253 static int mlx5e_rep_set_channels(struct net_device *dev,
254 struct ethtool_channels *ch)
256 struct mlx5e_priv *priv = netdev_priv(dev);
257 u16 curr_channels_amount = priv->channels.params.num_channels;
258 u32 new_channels_amount = ch->combined_count;
259 struct mlx5_flow_destination new_dest;
262 err = mlx5e_ethtool_set_channels(priv, ch);
266 if (curr_channels_amount == 1 && new_channels_amount > 1) {
267 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
268 new_dest.ft = priv->fs.ttc.ft.t;
269 } else if (new_channels_amount == 1 && curr_channels_amount > 1) {
270 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
271 new_dest.tir_num = priv->direct_tir[0].tirn;
276 err = mlx5e_replace_rep_vport_rx_rule(priv, &new_dest);
278 netdev_warn(priv->netdev, "Failed to update vport rx rule, when going from (%d) channels to (%d) channels\n",
279 curr_channels_amount, new_channels_amount);
286 static int mlx5e_rep_get_coalesce(struct net_device *netdev,
287 struct ethtool_coalesce *coal)
289 struct mlx5e_priv *priv = netdev_priv(netdev);
291 return mlx5e_ethtool_get_coalesce(priv, coal);
294 static int mlx5e_rep_set_coalesce(struct net_device *netdev,
295 struct ethtool_coalesce *coal)
297 struct mlx5e_priv *priv = netdev_priv(netdev);
299 return mlx5e_ethtool_set_coalesce(priv, coal);
302 static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
304 struct mlx5e_priv *priv = netdev_priv(netdev);
306 return mlx5e_ethtool_get_rxfh_key_size(priv);
309 static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
311 struct mlx5e_priv *priv = netdev_priv(netdev);
313 return mlx5e_ethtool_get_rxfh_indir_size(priv);
316 static void mlx5e_uplink_rep_get_pauseparam(struct net_device *netdev,
317 struct ethtool_pauseparam *pauseparam)
319 struct mlx5e_priv *priv = netdev_priv(netdev);
321 mlx5e_ethtool_get_pauseparam(priv, pauseparam);
324 static int mlx5e_uplink_rep_set_pauseparam(struct net_device *netdev,
325 struct ethtool_pauseparam *pauseparam)
327 struct mlx5e_priv *priv = netdev_priv(netdev);
329 return mlx5e_ethtool_set_pauseparam(priv, pauseparam);
332 static int mlx5e_uplink_rep_get_link_ksettings(struct net_device *netdev,
333 struct ethtool_link_ksettings *link_ksettings)
335 struct mlx5e_priv *priv = netdev_priv(netdev);
337 return mlx5e_ethtool_get_link_ksettings(priv, link_ksettings);
340 static int mlx5e_uplink_rep_set_link_ksettings(struct net_device *netdev,
341 const struct ethtool_link_ksettings *link_ksettings)
343 struct mlx5e_priv *priv = netdev_priv(netdev);
345 return mlx5e_ethtool_set_link_ksettings(priv, link_ksettings);
348 static const struct ethtool_ops mlx5e_vf_rep_ethtool_ops = {
349 .get_drvinfo = mlx5e_rep_get_drvinfo,
350 .get_link = ethtool_op_get_link,
351 .get_strings = mlx5e_rep_get_strings,
352 .get_sset_count = mlx5e_rep_get_sset_count,
353 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
354 .get_ringparam = mlx5e_rep_get_ringparam,
355 .set_ringparam = mlx5e_rep_set_ringparam,
356 .get_channels = mlx5e_rep_get_channels,
357 .set_channels = mlx5e_rep_set_channels,
358 .get_coalesce = mlx5e_rep_get_coalesce,
359 .set_coalesce = mlx5e_rep_set_coalesce,
360 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
361 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
364 static const struct ethtool_ops mlx5e_uplink_rep_ethtool_ops = {
365 .get_drvinfo = mlx5e_rep_get_drvinfo,
366 .get_link = ethtool_op_get_link,
367 .get_strings = mlx5e_rep_get_strings,
368 .get_sset_count = mlx5e_rep_get_sset_count,
369 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
370 .get_ringparam = mlx5e_rep_get_ringparam,
371 .set_ringparam = mlx5e_rep_set_ringparam,
372 .get_channels = mlx5e_rep_get_channels,
373 .set_channels = mlx5e_rep_set_channels,
374 .get_coalesce = mlx5e_rep_get_coalesce,
375 .set_coalesce = mlx5e_rep_set_coalesce,
376 .get_link_ksettings = mlx5e_uplink_rep_get_link_ksettings,
377 .set_link_ksettings = mlx5e_uplink_rep_set_link_ksettings,
378 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
379 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
380 .get_pauseparam = mlx5e_uplink_rep_get_pauseparam,
381 .set_pauseparam = mlx5e_uplink_rep_set_pauseparam,
384 static int mlx5e_rep_get_port_parent_id(struct net_device *dev,
385 struct netdev_phys_item_id *ppid)
387 struct mlx5e_priv *priv = netdev_priv(dev);
388 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
389 struct net_device *uplink_upper = NULL;
390 struct mlx5e_priv *uplink_priv = NULL;
391 struct net_device *uplink_dev;
393 if (esw->mode == SRIOV_NONE)
396 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
398 uplink_upper = netdev_master_upper_dev_get(uplink_dev);
399 uplink_priv = netdev_priv(uplink_dev);
402 ppid->id_len = ETH_ALEN;
403 if (uplink_upper && mlx5_lag_is_sriov(uplink_priv->mdev)) {
404 ether_addr_copy(ppid->id, uplink_upper->dev_addr);
406 struct mlx5e_rep_priv *rpriv = priv->ppriv;
407 struct mlx5_eswitch_rep *rep = rpriv->rep;
409 ether_addr_copy(ppid->id, rep->hw_id);
415 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
416 struct mlx5_eswitch_rep *rep)
418 struct mlx5e_rep_sq *rep_sq, *tmp;
419 struct mlx5e_rep_priv *rpriv;
421 if (esw->mode != SRIOV_OFFLOADS)
424 rpriv = mlx5e_rep_to_rep_priv(rep);
425 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
426 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
427 list_del(&rep_sq->list);
432 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
433 struct mlx5_eswitch_rep *rep,
434 u32 *sqns_array, int sqns_num)
436 struct mlx5_flow_handle *flow_rule;
437 struct mlx5e_rep_priv *rpriv;
438 struct mlx5e_rep_sq *rep_sq;
442 if (esw->mode != SRIOV_OFFLOADS)
445 rpriv = mlx5e_rep_to_rep_priv(rep);
446 for (i = 0; i < sqns_num; i++) {
447 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
453 /* Add re-inject rule to the PF/representor sqs */
454 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
457 if (IS_ERR(flow_rule)) {
458 err = PTR_ERR(flow_rule);
462 rep_sq->send_to_vport_rule = flow_rule;
463 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
468 mlx5e_sqs2vport_stop(esw, rep);
472 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
474 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
475 struct mlx5e_rep_priv *rpriv = priv->ppriv;
476 struct mlx5_eswitch_rep *rep = rpriv->rep;
477 struct mlx5e_channel *c;
478 int n, tc, num_sqs = 0;
482 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(*sqs), GFP_KERNEL);
486 for (n = 0; n < priv->channels.num; n++) {
487 c = priv->channels.c[n];
488 for (tc = 0; tc < c->num_tc; tc++)
489 sqs[num_sqs++] = c->sq[tc].sqn;
492 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
497 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
501 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
503 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
504 struct mlx5e_rep_priv *rpriv = priv->ppriv;
505 struct mlx5_eswitch_rep *rep = rpriv->rep;
507 mlx5e_sqs2vport_stop(esw, rep);
510 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv *rpriv)
512 #if IS_ENABLED(CONFIG_IPV6)
513 unsigned long ipv6_interval = NEIGH_VAR(&nd_tbl.parms,
516 unsigned long ipv6_interval = ~0UL;
518 unsigned long ipv4_interval = NEIGH_VAR(&arp_tbl.parms,
520 struct net_device *netdev = rpriv->netdev;
521 struct mlx5e_priv *priv = netdev_priv(netdev);
523 rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);
524 mlx5_fc_update_sampling_interval(priv->mdev, rpriv->neigh_update.min_interval);
527 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv)
529 struct mlx5e_rep_priv *rpriv = priv->ppriv;
530 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
532 mlx5_fc_queue_stats_work(priv->mdev,
533 &neigh_update->neigh_stats_work,
534 neigh_update->min_interval);
537 static void mlx5e_rep_neigh_stats_work(struct work_struct *work)
539 struct mlx5e_rep_priv *rpriv = container_of(work, struct mlx5e_rep_priv,
540 neigh_update.neigh_stats_work.work);
541 struct net_device *netdev = rpriv->netdev;
542 struct mlx5e_priv *priv = netdev_priv(netdev);
543 struct mlx5e_neigh_hash_entry *nhe;
546 if (!list_empty(&rpriv->neigh_update.neigh_list))
547 mlx5e_rep_queue_neigh_stats_work(priv);
549 list_for_each_entry(nhe, &rpriv->neigh_update.neigh_list, neigh_list)
550 mlx5e_tc_update_neigh_used_value(nhe);
555 static void mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry *nhe)
557 refcount_inc(&nhe->refcnt);
560 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry *nhe)
562 if (refcount_dec_and_test(&nhe->refcnt))
566 static void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
567 struct mlx5e_encap_entry *e,
568 bool neigh_connected,
569 unsigned char ha[ETH_ALEN])
571 struct ethhdr *eth = (struct ethhdr *)e->encap_header;
575 if ((e->flags & MLX5_ENCAP_ENTRY_VALID) &&
576 (!neigh_connected || !ether_addr_equal(e->h_dest, ha)))
577 mlx5e_tc_encap_flows_del(priv, e);
579 if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
580 ether_addr_copy(e->h_dest, ha);
581 ether_addr_copy(eth->h_dest, ha);
582 /* Update the encap source mac, in case that we delete
583 * the flows when encap source mac changed.
585 ether_addr_copy(eth->h_source, e->route_dev->dev_addr);
587 mlx5e_tc_encap_flows_add(priv, e);
591 static void mlx5e_rep_neigh_update(struct work_struct *work)
593 struct mlx5e_neigh_hash_entry *nhe =
594 container_of(work, struct mlx5e_neigh_hash_entry, neigh_update_work);
595 struct neighbour *n = nhe->n;
596 struct mlx5e_encap_entry *e;
597 unsigned char ha[ETH_ALEN];
598 struct mlx5e_priv *priv;
599 bool neigh_connected;
600 bool encap_connected;
605 /* If these parameters are changed after we release the lock,
606 * we'll receive another event letting us know about it.
607 * We use this lock to avoid inconsistency between the neigh validity
608 * and it's hw address.
610 read_lock_bh(&n->lock);
611 memcpy(ha, n->ha, ETH_ALEN);
612 nud_state = n->nud_state;
614 read_unlock_bh(&n->lock);
616 neigh_connected = (nud_state & NUD_VALID) && !dead;
618 list_for_each_entry(e, &nhe->encap_list, encap_list) {
619 encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
620 priv = netdev_priv(e->out_dev);
622 if (encap_connected != neigh_connected ||
623 !ether_addr_equal(e->h_dest, ha))
624 mlx5e_rep_update_flows(priv, e, neigh_connected, ha);
626 mlx5e_rep_neigh_entry_release(nhe);
631 static struct mlx5e_rep_indr_block_priv *
632 mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv *rpriv,
633 struct net_device *netdev)
635 struct mlx5e_rep_indr_block_priv *cb_priv;
637 /* All callback list access should be protected by RTNL. */
640 list_for_each_entry(cb_priv,
641 &rpriv->uplink_priv.tc_indr_block_priv_list,
643 if (cb_priv->netdev == netdev)
649 static void mlx5e_rep_indr_clean_block_privs(struct mlx5e_rep_priv *rpriv)
651 struct mlx5e_rep_indr_block_priv *cb_priv, *temp;
652 struct list_head *head = &rpriv->uplink_priv.tc_indr_block_priv_list;
654 list_for_each_entry_safe(cb_priv, temp, head, list) {
655 mlx5e_rep_indr_unregister_block(rpriv, cb_priv->netdev);
661 mlx5e_rep_indr_offload(struct net_device *netdev,
662 struct tc_cls_flower_offload *flower,
663 struct mlx5e_rep_indr_block_priv *indr_priv)
665 struct mlx5e_priv *priv = netdev_priv(indr_priv->rpriv->netdev);
666 int flags = MLX5E_TC_EGRESS | MLX5E_TC_ESW_OFFLOAD;
669 switch (flower->command) {
670 case TC_CLSFLOWER_REPLACE:
671 err = mlx5e_configure_flower(netdev, priv, flower, flags);
673 case TC_CLSFLOWER_DESTROY:
674 err = mlx5e_delete_flower(netdev, priv, flower, flags);
676 case TC_CLSFLOWER_STATS:
677 err = mlx5e_stats_flower(netdev, priv, flower, flags);
686 static int mlx5e_rep_indr_setup_block_cb(enum tc_setup_type type,
687 void *type_data, void *indr_priv)
689 struct mlx5e_rep_indr_block_priv *priv = indr_priv;
692 case TC_SETUP_CLSFLOWER:
693 return mlx5e_rep_indr_offload(priv->netdev, type_data, priv);
700 mlx5e_rep_indr_setup_tc_block(struct net_device *netdev,
701 struct mlx5e_rep_priv *rpriv,
702 struct tc_block_offload *f)
704 struct mlx5e_rep_indr_block_priv *indr_priv;
707 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
710 switch (f->command) {
712 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
716 indr_priv = kmalloc(sizeof(*indr_priv), GFP_KERNEL);
720 indr_priv->netdev = netdev;
721 indr_priv->rpriv = rpriv;
722 list_add(&indr_priv->list,
723 &rpriv->uplink_priv.tc_indr_block_priv_list);
725 err = tcf_block_cb_register(f->block,
726 mlx5e_rep_indr_setup_block_cb,
727 indr_priv, indr_priv, f->extack);
729 list_del(&indr_priv->list);
734 case TC_BLOCK_UNBIND:
735 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
739 tcf_block_cb_unregister(f->block,
740 mlx5e_rep_indr_setup_block_cb,
742 list_del(&indr_priv->list);
753 int mlx5e_rep_indr_setup_tc_cb(struct net_device *netdev, void *cb_priv,
754 enum tc_setup_type type, void *type_data)
758 return mlx5e_rep_indr_setup_tc_block(netdev, cb_priv,
765 static int mlx5e_rep_indr_register_block(struct mlx5e_rep_priv *rpriv,
766 struct net_device *netdev)
770 err = __tc_indr_block_cb_register(netdev, rpriv,
771 mlx5e_rep_indr_setup_tc_cb,
774 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
776 mlx5_core_err(priv->mdev, "Failed to register remote block notifier for %s err=%d\n",
777 netdev_name(netdev), err);
782 static void mlx5e_rep_indr_unregister_block(struct mlx5e_rep_priv *rpriv,
783 struct net_device *netdev)
785 __tc_indr_block_cb_unregister(netdev, mlx5e_rep_indr_setup_tc_cb,
789 static int mlx5e_nic_rep_netdevice_event(struct notifier_block *nb,
790 unsigned long event, void *ptr)
792 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
793 uplink_priv.netdevice_nb);
794 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
795 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
797 if (!mlx5e_tc_tun_device_to_offload(priv, netdev))
801 case NETDEV_REGISTER:
802 mlx5e_rep_indr_register_block(rpriv, netdev);
804 case NETDEV_UNREGISTER:
805 mlx5e_rep_indr_unregister_block(rpriv, netdev);
811 static struct mlx5e_neigh_hash_entry *
812 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
813 struct mlx5e_neigh *m_neigh);
815 static int mlx5e_rep_netevent_event(struct notifier_block *nb,
816 unsigned long event, void *ptr)
818 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
819 neigh_update.netevent_nb);
820 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
821 struct net_device *netdev = rpriv->netdev;
822 struct mlx5e_priv *priv = netdev_priv(netdev);
823 struct mlx5e_neigh_hash_entry *nhe = NULL;
824 struct mlx5e_neigh m_neigh = {};
825 struct neigh_parms *p;
830 case NETEVENT_NEIGH_UPDATE:
832 #if IS_ENABLED(CONFIG_IPV6)
833 if (n->tbl != &nd_tbl && n->tbl != &arp_tbl)
835 if (n->tbl != &arp_tbl)
839 m_neigh.dev = n->dev;
840 m_neigh.family = n->ops->family;
841 memcpy(&m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
843 /* We are in atomic context and can't take RTNL mutex, so use
844 * spin_lock_bh to lookup the neigh table. bh is used since
845 * netevent can be called from a softirq context.
847 spin_lock_bh(&neigh_update->encap_lock);
848 nhe = mlx5e_rep_neigh_entry_lookup(priv, &m_neigh);
850 spin_unlock_bh(&neigh_update->encap_lock);
854 /* This assignment is valid as long as the the neigh reference
859 /* Take a reference to ensure the neighbour and mlx5 encap
860 * entry won't be destructed until we drop the reference in
864 mlx5e_rep_neigh_entry_hold(nhe);
866 if (!queue_work(priv->wq, &nhe->neigh_update_work)) {
867 mlx5e_rep_neigh_entry_release(nhe);
870 spin_unlock_bh(&neigh_update->encap_lock);
873 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
876 /* We check the device is present since we don't care about
877 * changes in the default table, we only care about changes
878 * done per device delay prob time parameter.
880 #if IS_ENABLED(CONFIG_IPV6)
881 if (!p->dev || (p->tbl != &nd_tbl && p->tbl != &arp_tbl))
883 if (!p->dev || p->tbl != &arp_tbl)
887 /* We are in atomic context and can't take RTNL mutex,
888 * so use spin_lock_bh to walk the neigh list and look for
889 * the relevant device. bh is used since netevent can be
890 * called from a softirq context.
892 spin_lock_bh(&neigh_update->encap_lock);
893 list_for_each_entry(nhe, &neigh_update->neigh_list, neigh_list) {
894 if (p->dev == nhe->m_neigh.dev) {
899 spin_unlock_bh(&neigh_update->encap_lock);
903 neigh_update->min_interval = min_t(unsigned long,
904 NEIGH_VAR(p, DELAY_PROBE_TIME),
905 neigh_update->min_interval);
906 mlx5_fc_update_sampling_interval(priv->mdev,
907 neigh_update->min_interval);
913 static const struct rhashtable_params mlx5e_neigh_ht_params = {
914 .head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
915 .key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
916 .key_len = sizeof(struct mlx5e_neigh),
917 .automatic_shrinking = true,
920 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv *rpriv)
922 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
925 err = rhashtable_init(&neigh_update->neigh_ht, &mlx5e_neigh_ht_params);
929 INIT_LIST_HEAD(&neigh_update->neigh_list);
930 spin_lock_init(&neigh_update->encap_lock);
931 INIT_DELAYED_WORK(&neigh_update->neigh_stats_work,
932 mlx5e_rep_neigh_stats_work);
933 mlx5e_rep_neigh_update_init_interval(rpriv);
935 rpriv->neigh_update.netevent_nb.notifier_call = mlx5e_rep_netevent_event;
936 err = register_netevent_notifier(&rpriv->neigh_update.netevent_nb);
942 rhashtable_destroy(&neigh_update->neigh_ht);
946 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv *rpriv)
948 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
949 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
951 unregister_netevent_notifier(&neigh_update->netevent_nb);
953 flush_workqueue(priv->wq); /* flush neigh update works */
955 cancel_delayed_work_sync(&rpriv->neigh_update.neigh_stats_work);
957 rhashtable_destroy(&neigh_update->neigh_ht);
960 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv *priv,
961 struct mlx5e_neigh_hash_entry *nhe)
963 struct mlx5e_rep_priv *rpriv = priv->ppriv;
966 err = rhashtable_insert_fast(&rpriv->neigh_update.neigh_ht,
968 mlx5e_neigh_ht_params);
972 list_add(&nhe->neigh_list, &rpriv->neigh_update.neigh_list);
977 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_priv *priv,
978 struct mlx5e_neigh_hash_entry *nhe)
980 struct mlx5e_rep_priv *rpriv = priv->ppriv;
982 spin_lock_bh(&rpriv->neigh_update.encap_lock);
984 list_del(&nhe->neigh_list);
986 rhashtable_remove_fast(&rpriv->neigh_update.neigh_ht,
988 mlx5e_neigh_ht_params);
989 spin_unlock_bh(&rpriv->neigh_update.encap_lock);
992 /* This function must only be called under RTNL lock or under the
993 * representor's encap_lock in case RTNL mutex can't be held.
995 static struct mlx5e_neigh_hash_entry *
996 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
997 struct mlx5e_neigh *m_neigh)
999 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1000 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
1002 return rhashtable_lookup_fast(&neigh_update->neigh_ht, m_neigh,
1003 mlx5e_neigh_ht_params);
1006 static int mlx5e_rep_neigh_entry_create(struct mlx5e_priv *priv,
1007 struct mlx5e_encap_entry *e,
1008 struct mlx5e_neigh_hash_entry **nhe)
1012 *nhe = kzalloc(sizeof(**nhe), GFP_KERNEL);
1016 memcpy(&(*nhe)->m_neigh, &e->m_neigh, sizeof(e->m_neigh));
1017 INIT_WORK(&(*nhe)->neigh_update_work, mlx5e_rep_neigh_update);
1018 INIT_LIST_HEAD(&(*nhe)->encap_list);
1019 refcount_set(&(*nhe)->refcnt, 1);
1021 err = mlx5e_rep_neigh_entry_insert(priv, *nhe);
1031 static void mlx5e_rep_neigh_entry_destroy(struct mlx5e_priv *priv,
1032 struct mlx5e_neigh_hash_entry *nhe)
1034 /* The neigh hash entry must be removed from the hash table regardless
1035 * of the reference count value, so it won't be found by the next
1036 * neigh notification call. The neigh hash entry reference count is
1037 * incremented only during creation and neigh notification calls and
1038 * protects from freeing the nhe struct.
1040 mlx5e_rep_neigh_entry_remove(priv, nhe);
1041 mlx5e_rep_neigh_entry_release(nhe);
1044 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
1045 struct mlx5e_encap_entry *e)
1047 struct mlx5e_neigh_hash_entry *nhe;
1050 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
1052 err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
1056 list_add(&e->encap_list, &nhe->encap_list);
1060 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
1061 struct mlx5e_encap_entry *e)
1063 struct mlx5e_neigh_hash_entry *nhe;
1065 list_del(&e->encap_list);
1066 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
1068 if (list_empty(&nhe->encap_list))
1069 mlx5e_rep_neigh_entry_destroy(priv, nhe);
1072 static int mlx5e_vf_rep_open(struct net_device *dev)
1074 struct mlx5e_priv *priv = netdev_priv(dev);
1075 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1076 struct mlx5_eswitch_rep *rep = rpriv->rep;
1079 mutex_lock(&priv->state_lock);
1080 err = mlx5e_open_locked(dev);
1084 if (!mlx5_modify_vport_admin_state(priv->mdev,
1085 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1086 rep->vport, MLX5_VPORT_ADMIN_STATE_UP))
1087 netif_carrier_on(dev);
1090 mutex_unlock(&priv->state_lock);
1094 static int mlx5e_vf_rep_close(struct net_device *dev)
1096 struct mlx5e_priv *priv = netdev_priv(dev);
1097 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1098 struct mlx5_eswitch_rep *rep = rpriv->rep;
1101 mutex_lock(&priv->state_lock);
1102 mlx5_modify_vport_admin_state(priv->mdev,
1103 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1104 rep->vport, MLX5_VPORT_ADMIN_STATE_DOWN);
1105 ret = mlx5e_close_locked(dev);
1106 mutex_unlock(&priv->state_lock);
1110 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
1111 char *buf, size_t len)
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 ret = mlx5_lag_get_pf_num(priv->mdev, &pf_num);
1122 if (rep->vport == FDB_UPLINK_VPORT)
1123 ret = snprintf(buf, len, "p%d", pf_num);
1125 ret = snprintf(buf, len, "pf%dvf%d", pf_num, rep->vport - 1);
1134 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
1135 struct tc_cls_flower_offload *cls_flower, int flags)
1137 switch (cls_flower->command) {
1138 case TC_CLSFLOWER_REPLACE:
1139 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
1141 case TC_CLSFLOWER_DESTROY:
1142 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
1144 case TC_CLSFLOWER_STATS:
1145 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
1152 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
1155 struct mlx5e_priv *priv = cb_priv;
1158 case TC_SETUP_CLSFLOWER:
1159 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS |
1160 MLX5E_TC_ESW_OFFLOAD);
1166 static int mlx5e_rep_setup_tc_block(struct net_device *dev,
1167 struct tc_block_offload *f)
1169 struct mlx5e_priv *priv = netdev_priv(dev);
1171 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1174 switch (f->command) {
1176 return tcf_block_cb_register(f->block, mlx5e_rep_setup_tc_cb,
1177 priv, priv, f->extack);
1178 case TC_BLOCK_UNBIND:
1179 tcf_block_cb_unregister(f->block, mlx5e_rep_setup_tc_cb, priv);
1186 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
1190 case TC_SETUP_BLOCK:
1191 return mlx5e_rep_setup_tc_block(dev, type_data);
1197 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
1199 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1200 struct mlx5_eswitch_rep *rep;
1202 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
1205 if (!rpriv) /* non vport rep mlx5e instances don't use this field */
1209 return (rep->vport == FDB_UPLINK_VPORT);
1212 static bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
1215 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1223 mlx5e_get_sw_stats64(const struct net_device *dev,
1224 struct rtnl_link_stats64 *stats)
1226 struct mlx5e_priv *priv = netdev_priv(dev);
1228 mlx5e_fold_sw_stats64(priv, stats);
1232 static int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
1236 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1237 return mlx5e_get_sw_stats64(dev, sp);
1244 mlx5e_vf_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
1246 struct mlx5e_priv *priv = netdev_priv(dev);
1248 /* update HW stats in background for next time */
1249 mlx5e_queue_update_stats(priv);
1250 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
1253 static int mlx5e_vf_rep_change_mtu(struct net_device *netdev, int new_mtu)
1255 return mlx5e_change_mtu(netdev, new_mtu, NULL);
1258 static int mlx5e_uplink_rep_change_mtu(struct net_device *netdev, int new_mtu)
1260 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
1263 static int mlx5e_uplink_rep_set_mac(struct net_device *netdev, void *addr)
1265 struct sockaddr *saddr = addr;
1267 if (!is_valid_ether_addr(saddr->sa_data))
1268 return -EADDRNOTAVAIL;
1270 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
1274 static int mlx5e_uplink_rep_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
1277 netdev_warn_once(dev, "legacy vf vlan setting isn't supported in switchdev mode\n");
1282 /* allow setting 0-vid for compatibility with libvirt */
1286 static const struct net_device_ops mlx5e_netdev_ops_vf_rep = {
1287 .ndo_open = mlx5e_vf_rep_open,
1288 .ndo_stop = mlx5e_vf_rep_close,
1289 .ndo_start_xmit = mlx5e_xmit,
1290 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
1291 .ndo_setup_tc = mlx5e_rep_setup_tc,
1292 .ndo_get_stats64 = mlx5e_vf_rep_get_stats,
1293 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
1294 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
1295 .ndo_change_mtu = mlx5e_vf_rep_change_mtu,
1296 .ndo_get_port_parent_id = mlx5e_rep_get_port_parent_id,
1299 static const struct net_device_ops mlx5e_netdev_ops_uplink_rep = {
1300 .ndo_open = mlx5e_open,
1301 .ndo_stop = mlx5e_close,
1302 .ndo_start_xmit = mlx5e_xmit,
1303 .ndo_set_mac_address = mlx5e_uplink_rep_set_mac,
1304 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
1305 .ndo_setup_tc = mlx5e_rep_setup_tc,
1306 .ndo_get_stats64 = mlx5e_get_stats,
1307 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
1308 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
1309 .ndo_change_mtu = mlx5e_uplink_rep_change_mtu,
1310 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
1311 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
1312 .ndo_features_check = mlx5e_features_check,
1313 .ndo_set_vf_mac = mlx5e_set_vf_mac,
1314 .ndo_set_vf_rate = mlx5e_set_vf_rate,
1315 .ndo_get_vf_config = mlx5e_get_vf_config,
1316 .ndo_get_vf_stats = mlx5e_get_vf_stats,
1317 .ndo_set_vf_vlan = mlx5e_uplink_rep_set_vf_vlan,
1318 .ndo_get_port_parent_id = mlx5e_rep_get_port_parent_id,
1321 bool mlx5e_eswitch_rep(struct net_device *netdev)
1323 if (netdev->netdev_ops == &mlx5e_netdev_ops_vf_rep ||
1324 netdev->netdev_ops == &mlx5e_netdev_ops_uplink_rep)
1330 static void mlx5e_build_rep_params(struct net_device *netdev)
1332 struct mlx5e_priv *priv = netdev_priv(netdev);
1333 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1334 struct mlx5_eswitch_rep *rep = rpriv->rep;
1335 struct mlx5_core_dev *mdev = priv->mdev;
1336 struct mlx5e_params *params;
1338 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
1339 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
1340 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1342 params = &priv->channels.params;
1343 params->hard_mtu = MLX5E_ETH_HARD_MTU;
1344 params->sw_mtu = netdev->mtu;
1347 if (rep->vport == FDB_UPLINK_VPORT)
1348 params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
1350 params->log_sq_size = MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE;
1353 mlx5e_build_rq_params(mdev, params);
1355 /* CQ moderation params */
1356 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
1357 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
1361 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
1364 mlx5e_build_rss_params(&priv->rss_params, params->num_channels);
1367 static void mlx5e_build_rep_netdev(struct net_device *netdev)
1369 struct mlx5e_priv *priv = netdev_priv(netdev);
1370 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1371 struct mlx5_eswitch_rep *rep = rpriv->rep;
1372 struct mlx5_core_dev *mdev = priv->mdev;
1374 if (rep->vport == FDB_UPLINK_VPORT) {
1375 SET_NETDEV_DEV(netdev, &priv->mdev->pdev->dev);
1376 netdev->netdev_ops = &mlx5e_netdev_ops_uplink_rep;
1377 /* we want a persistent mac for the uplink rep */
1378 mlx5_query_nic_vport_mac_address(mdev, 0, netdev->dev_addr);
1379 netdev->ethtool_ops = &mlx5e_uplink_rep_ethtool_ops;
1380 #ifdef CONFIG_MLX5_CORE_EN_DCB
1381 if (MLX5_CAP_GEN(mdev, qos))
1382 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
1385 netdev->netdev_ops = &mlx5e_netdev_ops_vf_rep;
1386 eth_hw_addr_random(netdev);
1387 netdev->ethtool_ops = &mlx5e_vf_rep_ethtool_ops;
1390 netdev->watchdog_timeo = 15 * HZ;
1393 netdev->features |= NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
1394 netdev->hw_features |= NETIF_F_HW_TC;
1396 netdev->hw_features |= NETIF_F_SG;
1397 netdev->hw_features |= NETIF_F_IP_CSUM;
1398 netdev->hw_features |= NETIF_F_IPV6_CSUM;
1399 netdev->hw_features |= NETIF_F_GRO;
1400 netdev->hw_features |= NETIF_F_TSO;
1401 netdev->hw_features |= NETIF_F_TSO6;
1402 netdev->hw_features |= NETIF_F_RXCSUM;
1404 if (rep->vport != FDB_UPLINK_VPORT)
1405 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1407 netdev->features |= netdev->hw_features;
1410 static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
1411 struct net_device *netdev,
1412 const struct mlx5e_profile *profile,
1415 struct mlx5e_priv *priv = netdev_priv(netdev);
1418 err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
1422 priv->channels.params.num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;
1424 mlx5e_build_rep_params(netdev);
1425 mlx5e_build_rep_netdev(netdev);
1427 mlx5e_timestamp_init(priv);
1432 static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
1434 mlx5e_netdev_cleanup(priv->netdev, priv);
1437 static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
1439 struct ttc_params ttc_params = {};
1442 priv->fs.ns = mlx5_get_flow_namespace(priv->mdev,
1443 MLX5_FLOW_NAMESPACE_KERNEL);
1445 /* The inner_ttc in the ttc params is intentionally not set */
1446 ttc_params.any_tt_tirn = priv->direct_tir[0].tirn;
1447 mlx5e_set_ttc_ft_params(&ttc_params);
1448 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
1449 ttc_params.indir_tirn[tt] = priv->indir_tir[tt].tirn;
1451 err = mlx5e_create_ttc_table(priv, &ttc_params, &priv->fs.ttc);
1453 netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n", err);
1459 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
1461 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1462 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1463 struct mlx5_eswitch_rep *rep = rpriv->rep;
1464 struct mlx5_flow_handle *flow_rule;
1465 struct mlx5_flow_destination dest;
1467 dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1468 dest.tir_num = priv->direct_tir[0].tirn;
1469 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
1472 if (IS_ERR(flow_rule))
1473 return PTR_ERR(flow_rule);
1474 rpriv->vport_rx_rule = flow_rule;
1478 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
1480 struct mlx5_core_dev *mdev = priv->mdev;
1483 mlx5e_init_l2_addr(priv);
1485 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
1487 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
1491 err = mlx5e_create_indirect_rqt(priv);
1493 goto err_close_drop_rq;
1495 err = mlx5e_create_direct_rqts(priv);
1497 goto err_destroy_indirect_rqts;
1499 err = mlx5e_create_indirect_tirs(priv, false);
1501 goto err_destroy_direct_rqts;
1503 err = mlx5e_create_direct_tirs(priv);
1505 goto err_destroy_indirect_tirs;
1507 err = mlx5e_create_rep_ttc_table(priv);
1509 goto err_destroy_direct_tirs;
1511 err = mlx5e_create_rep_vport_rx_rule(priv);
1513 goto err_destroy_ttc_table;
1517 err_destroy_ttc_table:
1518 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1519 err_destroy_direct_tirs:
1520 mlx5e_destroy_direct_tirs(priv);
1521 err_destroy_indirect_tirs:
1522 mlx5e_destroy_indirect_tirs(priv, false);
1523 err_destroy_direct_rqts:
1524 mlx5e_destroy_direct_rqts(priv);
1525 err_destroy_indirect_rqts:
1526 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1528 mlx5e_close_drop_rq(&priv->drop_rq);
1532 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
1534 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1536 mlx5_del_flow_rules(rpriv->vport_rx_rule);
1537 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1538 mlx5e_destroy_direct_tirs(priv);
1539 mlx5e_destroy_indirect_tirs(priv, false);
1540 mlx5e_destroy_direct_rqts(priv);
1541 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1542 mlx5e_close_drop_rq(&priv->drop_rq);
1545 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1547 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1548 struct mlx5_rep_uplink_priv *uplink_priv;
1551 err = mlx5e_create_tises(priv);
1553 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1557 if (rpriv->rep->vport == FDB_UPLINK_VPORT) {
1558 uplink_priv = &rpriv->uplink_priv;
1560 /* init shared tc flow table */
1561 err = mlx5e_tc_esw_init(&uplink_priv->tc_ht);
1565 /* init indirect block notifications */
1566 INIT_LIST_HEAD(&uplink_priv->tc_indr_block_priv_list);
1567 uplink_priv->netdevice_nb.notifier_call = mlx5e_nic_rep_netdevice_event;
1568 err = register_netdevice_notifier(&uplink_priv->netdevice_nb);
1570 mlx5_core_err(priv->mdev, "Failed to register netdev notifier\n");
1571 goto tc_esw_cleanup;
1578 mlx5e_tc_esw_cleanup(&uplink_priv->tc_ht);
1580 for (tc = 0; tc < priv->profile->max_tc; tc++)
1581 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
1585 static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
1587 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1590 for (tc = 0; tc < priv->profile->max_tc; tc++)
1591 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
1593 if (rpriv->rep->vport == FDB_UPLINK_VPORT) {
1594 /* clean indirect TC block notifications */
1595 unregister_netdevice_notifier(&rpriv->uplink_priv.netdevice_nb);
1596 mlx5e_rep_indr_clean_block_privs(rpriv);
1598 /* delete shared tc flow table */
1599 mlx5e_tc_esw_cleanup(&rpriv->uplink_priv.tc_ht);
1603 static void mlx5e_vf_rep_enable(struct mlx5e_priv *priv)
1605 struct net_device *netdev = priv->netdev;
1606 struct mlx5_core_dev *mdev = priv->mdev;
1609 netdev->min_mtu = ETH_MIN_MTU;
1610 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
1611 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1614 static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
1616 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
1617 struct mlx5_eqe *eqe = data;
1619 if (event != MLX5_EVENT_TYPE_PORT_CHANGE)
1622 switch (eqe->sub_type) {
1623 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
1624 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
1625 queue_work(priv->wq, &priv->update_carrier_work);
1634 static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
1636 struct net_device *netdev = priv->netdev;
1637 struct mlx5_core_dev *mdev = priv->mdev;
1640 netdev->min_mtu = ETH_MIN_MTU;
1641 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
1642 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1643 mlx5e_set_dev_port_mtu(priv);
1645 mlx5_lag_add(mdev, netdev);
1646 priv->events_nb.notifier_call = uplink_rep_async_event;
1647 mlx5_notifier_register(mdev, &priv->events_nb);
1648 #ifdef CONFIG_MLX5_CORE_EN_DCB
1649 mlx5e_dcbnl_initialize(priv);
1650 mlx5e_dcbnl_init_app(priv);
1654 static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
1656 struct mlx5_core_dev *mdev = priv->mdev;
1658 #ifdef CONFIG_MLX5_CORE_EN_DCB
1659 mlx5e_dcbnl_delete_app(priv);
1661 mlx5_notifier_unregister(mdev, &priv->events_nb);
1662 mlx5_lag_remove(mdev);
1665 static const struct mlx5e_profile mlx5e_vf_rep_profile = {
1666 .init = mlx5e_init_rep,
1667 .cleanup = mlx5e_cleanup_rep,
1668 .init_rx = mlx5e_init_rep_rx,
1669 .cleanup_rx = mlx5e_cleanup_rep_rx,
1670 .init_tx = mlx5e_init_rep_tx,
1671 .cleanup_tx = mlx5e_cleanup_rep_tx,
1672 .enable = mlx5e_vf_rep_enable,
1673 .update_stats = mlx5e_vf_rep_update_hw_counters,
1674 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1675 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1679 static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
1680 .init = mlx5e_init_rep,
1681 .cleanup = mlx5e_cleanup_rep,
1682 .init_rx = mlx5e_init_rep_rx,
1683 .cleanup_rx = mlx5e_cleanup_rep_rx,
1684 .init_tx = mlx5e_init_rep_tx,
1685 .cleanup_tx = mlx5e_cleanup_rep_tx,
1686 .enable = mlx5e_uplink_rep_enable,
1687 .disable = mlx5e_uplink_rep_disable,
1688 .update_stats = mlx5e_uplink_rep_update_hw_counters,
1689 .update_carrier = mlx5e_update_carrier,
1690 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1691 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1692 .max_tc = MLX5E_MAX_NUM_TC,
1695 /* e-Switch vport representors */
1697 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1699 const struct mlx5e_profile *profile;
1700 struct mlx5e_rep_priv *rpriv;
1701 struct net_device *netdev;
1704 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1708 /* rpriv->rep to be looked up when profile->init() is called */
1711 nch = mlx5e_get_max_num_channels(dev);
1712 profile = (rep->vport == FDB_UPLINK_VPORT) ? &mlx5e_uplink_rep_profile : &mlx5e_vf_rep_profile;
1713 netdev = mlx5e_create_netdev(dev, profile, nch, rpriv);
1715 pr_warn("Failed to create representor netdev for vport %d\n",
1721 rpriv->netdev = netdev;
1722 rep->rep_if[REP_ETH].priv = rpriv;
1723 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1725 if (rep->vport == FDB_UPLINK_VPORT) {
1726 err = mlx5e_create_mdev_resources(dev);
1728 goto err_destroy_netdev;
1731 err = mlx5e_attach_netdev(netdev_priv(netdev));
1733 pr_warn("Failed to attach representor netdev for vport %d\n",
1735 goto err_destroy_mdev_resources;
1738 err = mlx5e_rep_neigh_init(rpriv);
1740 pr_warn("Failed to initialized neighbours handling for vport %d\n",
1742 goto err_detach_netdev;
1745 err = register_netdev(netdev);
1747 pr_warn("Failed to register representor netdev for vport %d\n",
1749 goto err_neigh_cleanup;
1755 mlx5e_rep_neigh_cleanup(rpriv);
1758 mlx5e_detach_netdev(netdev_priv(netdev));
1760 err_destroy_mdev_resources:
1761 if (rep->vport == FDB_UPLINK_VPORT)
1762 mlx5e_destroy_mdev_resources(dev);
1765 mlx5e_destroy_netdev(netdev_priv(netdev));
1771 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1773 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1774 struct net_device *netdev = rpriv->netdev;
1775 struct mlx5e_priv *priv = netdev_priv(netdev);
1776 void *ppriv = priv->ppriv;
1778 unregister_netdev(netdev);
1779 mlx5e_rep_neigh_cleanup(rpriv);
1780 mlx5e_detach_netdev(priv);
1781 if (rep->vport == FDB_UPLINK_VPORT)
1782 mlx5e_destroy_mdev_resources(priv->mdev);
1783 mlx5e_destroy_netdev(priv);
1784 kfree(ppriv); /* mlx5e_rep_priv */
1787 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1789 struct mlx5e_rep_priv *rpriv;
1791 rpriv = mlx5e_rep_to_rep_priv(rep);
1793 return rpriv->netdev;
1796 void mlx5e_rep_register_vport_reps(struct mlx5_core_dev *mdev)
1798 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1799 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1802 for (vport = 0; vport < total_vfs; vport++) {
1803 struct mlx5_eswitch_rep_if rep_if = {};
1805 rep_if.load = mlx5e_vport_rep_load;
1806 rep_if.unload = mlx5e_vport_rep_unload;
1807 rep_if.get_proto_dev = mlx5e_vport_rep_get_proto_dev;
1808 mlx5_eswitch_register_vport_rep(esw, vport, &rep_if, REP_ETH);
1812 void mlx5e_rep_unregister_vport_reps(struct mlx5_core_dev *mdev)
1814 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1815 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1818 for (vport = total_vfs - 1; vport >= 0; vport--)
1819 mlx5_eswitch_unregister_vport_rep(esw, vport, REP_ETH);