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 net_device *netdev);
63 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
64 struct ethtool_drvinfo *drvinfo)
66 strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
67 sizeof(drvinfo->driver));
68 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
71 static const struct counter_desc sw_rep_stats_desc[] = {
72 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
73 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
74 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
75 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
85 static const struct counter_desc vport_rep_stats_desc[] = {
86 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_packets) },
87 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_bytes) },
88 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_packets) },
89 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_bytes) },
92 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
93 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
95 static void mlx5e_rep_get_strings(struct net_device *dev,
96 u32 stringset, uint8_t *data)
102 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
103 strcpy(data + (i * ETH_GSTRING_LEN),
104 sw_rep_stats_desc[i].format);
105 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
106 strcpy(data + (i * ETH_GSTRING_LEN),
107 vport_rep_stats_desc[j].format);
112 static void mlx5e_vf_rep_update_hw_counters(struct mlx5e_priv *priv)
114 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
115 struct mlx5e_rep_priv *rpriv = priv->ppriv;
116 struct mlx5_eswitch_rep *rep = rpriv->rep;
117 struct rtnl_link_stats64 *vport_stats;
118 struct ifla_vf_stats vf_stats;
121 err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
123 pr_warn("vport %d error %d reading stats\n", rep->vport, err);
127 vport_stats = &priv->stats.vf_vport;
128 /* flip tx/rx as we are reporting the counters for the switch vport */
129 vport_stats->rx_packets = vf_stats.tx_packets;
130 vport_stats->rx_bytes = vf_stats.tx_bytes;
131 vport_stats->tx_packets = vf_stats.rx_packets;
132 vport_stats->tx_bytes = vf_stats.rx_bytes;
135 static void mlx5e_uplink_rep_update_hw_counters(struct mlx5e_priv *priv)
137 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
138 struct rtnl_link_stats64 *vport_stats;
140 mlx5e_grp_802_3_update_stats(priv);
142 vport_stats = &priv->stats.vf_vport;
144 vport_stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
145 vport_stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
146 vport_stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
147 vport_stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
150 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv *priv)
152 struct mlx5e_rep_priv *rpriv = priv->ppriv;
153 struct mlx5_eswitch_rep *rep = rpriv->rep;
155 if (rep->vport == FDB_UPLINK_VPORT)
156 mlx5e_uplink_rep_update_hw_counters(priv);
158 mlx5e_vf_rep_update_hw_counters(priv);
161 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv *priv)
163 struct mlx5e_sw_stats *s = &priv->stats.sw;
164 struct mlx5e_rq_stats *rq_stats;
165 struct mlx5e_sq_stats *sq_stats;
168 memset(s, 0, sizeof(*s));
169 for (i = 0; i < priv->channels.num; i++) {
170 struct mlx5e_channel *c = priv->channels.c[i];
172 rq_stats = c->rq.stats;
174 s->rx_packets += rq_stats->packets;
175 s->rx_bytes += rq_stats->bytes;
177 for (j = 0; j < priv->channels.params.num_tc; j++) {
178 sq_stats = c->sq[j].stats;
180 s->tx_packets += sq_stats->packets;
181 s->tx_bytes += sq_stats->bytes;
186 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
187 struct ethtool_stats *stats, u64 *data)
189 struct mlx5e_priv *priv = netdev_priv(dev);
195 mutex_lock(&priv->state_lock);
196 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
197 mlx5e_rep_update_sw_counters(priv);
198 mlx5e_rep_update_hw_counters(priv);
199 mutex_unlock(&priv->state_lock);
201 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
202 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
203 sw_rep_stats_desc, i);
205 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
206 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
207 vport_rep_stats_desc, j);
210 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
214 return NUM_VPORT_REP_SW_COUNTERS + NUM_VPORT_REP_HW_COUNTERS;
220 static void mlx5e_rep_get_ringparam(struct net_device *dev,
221 struct ethtool_ringparam *param)
223 struct mlx5e_priv *priv = netdev_priv(dev);
225 mlx5e_ethtool_get_ringparam(priv, param);
228 static int mlx5e_rep_set_ringparam(struct net_device *dev,
229 struct ethtool_ringparam *param)
231 struct mlx5e_priv *priv = netdev_priv(dev);
233 return mlx5e_ethtool_set_ringparam(priv, param);
236 static int mlx5e_replace_rep_vport_rx_rule(struct mlx5e_priv *priv,
237 struct mlx5_flow_destination *dest)
239 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
240 struct mlx5e_rep_priv *rpriv = priv->ppriv;
241 struct mlx5_eswitch_rep *rep = rpriv->rep;
242 struct mlx5_flow_handle *flow_rule;
244 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
247 if (IS_ERR(flow_rule))
248 return PTR_ERR(flow_rule);
250 mlx5_del_flow_rules(rpriv->vport_rx_rule);
251 rpriv->vport_rx_rule = flow_rule;
255 static void mlx5e_rep_get_channels(struct net_device *dev,
256 struct ethtool_channels *ch)
258 struct mlx5e_priv *priv = netdev_priv(dev);
260 mlx5e_ethtool_get_channels(priv, ch);
263 static int mlx5e_rep_set_channels(struct net_device *dev,
264 struct ethtool_channels *ch)
266 struct mlx5e_priv *priv = netdev_priv(dev);
267 u16 curr_channels_amount = priv->channels.params.num_channels;
268 u32 new_channels_amount = ch->combined_count;
269 struct mlx5_flow_destination new_dest;
272 err = mlx5e_ethtool_set_channels(priv, ch);
276 if (curr_channels_amount == 1 && new_channels_amount > 1) {
277 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
278 new_dest.ft = priv->fs.ttc.ft.t;
279 } else if (new_channels_amount == 1 && curr_channels_amount > 1) {
280 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
281 new_dest.tir_num = priv->direct_tir[0].tirn;
286 err = mlx5e_replace_rep_vport_rx_rule(priv, &new_dest);
288 netdev_warn(priv->netdev, "Failed to update vport rx rule, when going from (%d) channels to (%d) channels\n",
289 curr_channels_amount, new_channels_amount);
296 static int mlx5e_rep_get_coalesce(struct net_device *netdev,
297 struct ethtool_coalesce *coal)
299 struct mlx5e_priv *priv = netdev_priv(netdev);
301 return mlx5e_ethtool_get_coalesce(priv, coal);
304 static int mlx5e_rep_set_coalesce(struct net_device *netdev,
305 struct ethtool_coalesce *coal)
307 struct mlx5e_priv *priv = netdev_priv(netdev);
309 return mlx5e_ethtool_set_coalesce(priv, coal);
312 static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
314 struct mlx5e_priv *priv = netdev_priv(netdev);
316 return mlx5e_ethtool_get_rxfh_key_size(priv);
319 static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
321 struct mlx5e_priv *priv = netdev_priv(netdev);
323 return mlx5e_ethtool_get_rxfh_indir_size(priv);
326 static void mlx5e_uplink_rep_get_pauseparam(struct net_device *netdev,
327 struct ethtool_pauseparam *pauseparam)
329 struct mlx5e_priv *priv = netdev_priv(netdev);
331 mlx5e_ethtool_get_pauseparam(priv, pauseparam);
334 static int mlx5e_uplink_rep_set_pauseparam(struct net_device *netdev,
335 struct ethtool_pauseparam *pauseparam)
337 struct mlx5e_priv *priv = netdev_priv(netdev);
339 return mlx5e_ethtool_set_pauseparam(priv, pauseparam);
342 static int mlx5e_uplink_rep_get_link_ksettings(struct net_device *netdev,
343 struct ethtool_link_ksettings *link_ksettings)
345 struct mlx5e_priv *priv = netdev_priv(netdev);
347 return mlx5e_ethtool_get_link_ksettings(priv, link_ksettings);
350 static int mlx5e_uplink_rep_set_link_ksettings(struct net_device *netdev,
351 const struct ethtool_link_ksettings *link_ksettings)
353 struct mlx5e_priv *priv = netdev_priv(netdev);
355 return mlx5e_ethtool_set_link_ksettings(priv, link_ksettings);
358 static const struct ethtool_ops mlx5e_vf_rep_ethtool_ops = {
359 .get_drvinfo = mlx5e_rep_get_drvinfo,
360 .get_link = ethtool_op_get_link,
361 .get_strings = mlx5e_rep_get_strings,
362 .get_sset_count = mlx5e_rep_get_sset_count,
363 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
364 .get_ringparam = mlx5e_rep_get_ringparam,
365 .set_ringparam = mlx5e_rep_set_ringparam,
366 .get_channels = mlx5e_rep_get_channels,
367 .set_channels = mlx5e_rep_set_channels,
368 .get_coalesce = mlx5e_rep_get_coalesce,
369 .set_coalesce = mlx5e_rep_set_coalesce,
370 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
371 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
374 static const struct ethtool_ops mlx5e_uplink_rep_ethtool_ops = {
375 .get_drvinfo = mlx5e_rep_get_drvinfo,
376 .get_link = ethtool_op_get_link,
377 .get_strings = mlx5e_rep_get_strings,
378 .get_sset_count = mlx5e_rep_get_sset_count,
379 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
380 .get_ringparam = mlx5e_rep_get_ringparam,
381 .set_ringparam = mlx5e_rep_set_ringparam,
382 .get_channels = mlx5e_rep_get_channels,
383 .set_channels = mlx5e_rep_set_channels,
384 .get_coalesce = mlx5e_rep_get_coalesce,
385 .set_coalesce = mlx5e_rep_set_coalesce,
386 .get_link_ksettings = mlx5e_uplink_rep_get_link_ksettings,
387 .set_link_ksettings = mlx5e_uplink_rep_set_link_ksettings,
388 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
389 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
390 .get_pauseparam = mlx5e_uplink_rep_get_pauseparam,
391 .set_pauseparam = mlx5e_uplink_rep_set_pauseparam,
394 static int mlx5e_attr_get(struct net_device *dev, struct switchdev_attr *attr)
396 struct mlx5e_priv *priv = netdev_priv(dev);
397 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
398 struct net_device *uplink_upper = NULL;
399 struct mlx5e_priv *uplink_priv = NULL;
400 struct net_device *uplink_dev;
402 if (esw->mode == SRIOV_NONE)
405 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
407 uplink_upper = netdev_master_upper_dev_get(uplink_dev);
408 uplink_priv = netdev_priv(uplink_dev);
412 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
413 attr->u.ppid.id_len = ETH_ALEN;
414 if (uplink_upper && mlx5_lag_is_sriov(uplink_priv->mdev)) {
415 ether_addr_copy(attr->u.ppid.id, uplink_upper->dev_addr);
417 struct mlx5e_rep_priv *rpriv = priv->ppriv;
418 struct mlx5_eswitch_rep *rep = rpriv->rep;
420 ether_addr_copy(attr->u.ppid.id, rep->hw_id);
430 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
431 struct mlx5_eswitch_rep *rep)
433 struct mlx5e_rep_sq *rep_sq, *tmp;
434 struct mlx5e_rep_priv *rpriv;
436 if (esw->mode != SRIOV_OFFLOADS)
439 rpriv = mlx5e_rep_to_rep_priv(rep);
440 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
441 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
442 list_del(&rep_sq->list);
447 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
448 struct mlx5_eswitch_rep *rep,
449 u32 *sqns_array, int sqns_num)
451 struct mlx5_flow_handle *flow_rule;
452 struct mlx5e_rep_priv *rpriv;
453 struct mlx5e_rep_sq *rep_sq;
457 if (esw->mode != SRIOV_OFFLOADS)
460 rpriv = mlx5e_rep_to_rep_priv(rep);
461 for (i = 0; i < sqns_num; i++) {
462 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
468 /* Add re-inject rule to the PF/representor sqs */
469 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
472 if (IS_ERR(flow_rule)) {
473 err = PTR_ERR(flow_rule);
477 rep_sq->send_to_vport_rule = flow_rule;
478 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
483 mlx5e_sqs2vport_stop(esw, rep);
487 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
489 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
490 struct mlx5e_rep_priv *rpriv = priv->ppriv;
491 struct mlx5_eswitch_rep *rep = rpriv->rep;
492 struct mlx5e_channel *c;
493 int n, tc, num_sqs = 0;
497 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(*sqs), GFP_KERNEL);
501 for (n = 0; n < priv->channels.num; n++) {
502 c = priv->channels.c[n];
503 for (tc = 0; tc < c->num_tc; tc++)
504 sqs[num_sqs++] = c->sq[tc].sqn;
507 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
512 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
516 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
518 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
519 struct mlx5e_rep_priv *rpriv = priv->ppriv;
520 struct mlx5_eswitch_rep *rep = rpriv->rep;
522 mlx5e_sqs2vport_stop(esw, rep);
525 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv *rpriv)
527 #if IS_ENABLED(CONFIG_IPV6)
528 unsigned long ipv6_interval = NEIGH_VAR(&nd_tbl.parms,
531 unsigned long ipv6_interval = ~0UL;
533 unsigned long ipv4_interval = NEIGH_VAR(&arp_tbl.parms,
535 struct net_device *netdev = rpriv->netdev;
536 struct mlx5e_priv *priv = netdev_priv(netdev);
538 rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);
539 mlx5_fc_update_sampling_interval(priv->mdev, rpriv->neigh_update.min_interval);
542 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv)
544 struct mlx5e_rep_priv *rpriv = priv->ppriv;
545 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
547 mlx5_fc_queue_stats_work(priv->mdev,
548 &neigh_update->neigh_stats_work,
549 neigh_update->min_interval);
552 static void mlx5e_rep_neigh_stats_work(struct work_struct *work)
554 struct mlx5e_rep_priv *rpriv = container_of(work, struct mlx5e_rep_priv,
555 neigh_update.neigh_stats_work.work);
556 struct net_device *netdev = rpriv->netdev;
557 struct mlx5e_priv *priv = netdev_priv(netdev);
558 struct mlx5e_neigh_hash_entry *nhe;
561 if (!list_empty(&rpriv->neigh_update.neigh_list))
562 mlx5e_rep_queue_neigh_stats_work(priv);
564 list_for_each_entry(nhe, &rpriv->neigh_update.neigh_list, neigh_list)
565 mlx5e_tc_update_neigh_used_value(nhe);
570 static void mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry *nhe)
572 refcount_inc(&nhe->refcnt);
575 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry *nhe)
577 if (refcount_dec_and_test(&nhe->refcnt))
581 static void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
582 struct mlx5e_encap_entry *e,
583 bool neigh_connected,
584 unsigned char ha[ETH_ALEN])
586 struct ethhdr *eth = (struct ethhdr *)e->encap_header;
590 if ((e->flags & MLX5_ENCAP_ENTRY_VALID) &&
591 (!neigh_connected || !ether_addr_equal(e->h_dest, ha)))
592 mlx5e_tc_encap_flows_del(priv, e);
594 if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
595 ether_addr_copy(e->h_dest, ha);
596 ether_addr_copy(eth->h_dest, ha);
598 mlx5e_tc_encap_flows_add(priv, e);
602 static void mlx5e_rep_neigh_update(struct work_struct *work)
604 struct mlx5e_neigh_hash_entry *nhe =
605 container_of(work, struct mlx5e_neigh_hash_entry, neigh_update_work);
606 struct neighbour *n = nhe->n;
607 struct mlx5e_encap_entry *e;
608 unsigned char ha[ETH_ALEN];
609 struct mlx5e_priv *priv;
610 bool neigh_connected;
611 bool encap_connected;
616 /* If these parameters are changed after we release the lock,
617 * we'll receive another event letting us know about it.
618 * We use this lock to avoid inconsistency between the neigh validity
619 * and it's hw address.
621 read_lock_bh(&n->lock);
622 memcpy(ha, n->ha, ETH_ALEN);
623 nud_state = n->nud_state;
625 read_unlock_bh(&n->lock);
627 neigh_connected = (nud_state & NUD_VALID) && !dead;
629 list_for_each_entry(e, &nhe->encap_list, encap_list) {
630 encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
631 priv = netdev_priv(e->out_dev);
633 if (encap_connected != neigh_connected ||
634 !ether_addr_equal(e->h_dest, ha))
635 mlx5e_rep_update_flows(priv, e, neigh_connected, ha);
637 mlx5e_rep_neigh_entry_release(nhe);
642 static struct mlx5e_rep_indr_block_priv *
643 mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv *rpriv,
644 struct net_device *netdev)
646 struct mlx5e_rep_indr_block_priv *cb_priv;
648 /* All callback list access should be protected by RTNL. */
651 list_for_each_entry(cb_priv,
652 &rpriv->uplink_priv.tc_indr_block_priv_list,
654 if (cb_priv->netdev == netdev)
660 static void mlx5e_rep_indr_clean_block_privs(struct mlx5e_rep_priv *rpriv)
662 struct mlx5e_rep_indr_block_priv *cb_priv, *temp;
663 struct list_head *head = &rpriv->uplink_priv.tc_indr_block_priv_list;
665 list_for_each_entry_safe(cb_priv, temp, head, list) {
666 mlx5e_rep_indr_unregister_block(cb_priv->netdev);
672 mlx5e_rep_indr_offload(struct net_device *netdev,
673 struct tc_cls_flower_offload *flower,
674 struct mlx5e_rep_indr_block_priv *indr_priv)
676 struct mlx5e_priv *priv = netdev_priv(indr_priv->rpriv->netdev);
677 int flags = MLX5E_TC_EGRESS | MLX5E_TC_ESW_OFFLOAD;
680 switch (flower->command) {
681 case TC_CLSFLOWER_REPLACE:
682 err = mlx5e_configure_flower(netdev, priv, flower, flags);
684 case TC_CLSFLOWER_DESTROY:
685 err = mlx5e_delete_flower(netdev, priv, flower, flags);
687 case TC_CLSFLOWER_STATS:
688 err = mlx5e_stats_flower(netdev, priv, flower, flags);
697 static int mlx5e_rep_indr_setup_block_cb(enum tc_setup_type type,
698 void *type_data, void *indr_priv)
700 struct mlx5e_rep_indr_block_priv *priv = indr_priv;
703 case TC_SETUP_CLSFLOWER:
704 return mlx5e_rep_indr_offload(priv->netdev, type_data, priv);
711 mlx5e_rep_indr_setup_tc_block(struct net_device *netdev,
712 struct mlx5e_rep_priv *rpriv,
713 struct tc_block_offload *f)
715 struct mlx5e_rep_indr_block_priv *indr_priv;
718 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
721 switch (f->command) {
723 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
727 indr_priv = kmalloc(sizeof(*indr_priv), GFP_KERNEL);
731 indr_priv->netdev = netdev;
732 indr_priv->rpriv = rpriv;
733 list_add(&indr_priv->list,
734 &rpriv->uplink_priv.tc_indr_block_priv_list);
736 err = tcf_block_cb_register(f->block,
737 mlx5e_rep_indr_setup_block_cb,
738 netdev, indr_priv, f->extack);
740 list_del(&indr_priv->list);
745 case TC_BLOCK_UNBIND:
746 tcf_block_cb_unregister(f->block,
747 mlx5e_rep_indr_setup_block_cb,
749 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
751 list_del(&indr_priv->list);
763 int mlx5e_rep_indr_setup_tc_cb(struct net_device *netdev, void *cb_priv,
764 enum tc_setup_type type, void *type_data)
768 return mlx5e_rep_indr_setup_tc_block(netdev, cb_priv,
775 static int mlx5e_rep_indr_register_block(struct mlx5e_rep_priv *rpriv,
776 struct net_device *netdev)
780 err = __tc_indr_block_cb_register(netdev, rpriv,
781 mlx5e_rep_indr_setup_tc_cb,
784 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
786 mlx5_core_err(priv->mdev, "Failed to register remote block notifier for %s err=%d\n",
787 netdev_name(netdev), err);
792 static void mlx5e_rep_indr_unregister_block(struct net_device *netdev)
794 __tc_indr_block_cb_unregister(netdev, mlx5e_rep_indr_setup_tc_cb,
798 static int mlx5e_nic_rep_netdevice_event(struct notifier_block *nb,
799 unsigned long event, void *ptr)
801 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
802 uplink_priv.netdevice_nb);
803 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
804 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
806 if (!mlx5e_tc_tun_device_to_offload(priv, netdev))
810 case NETDEV_REGISTER:
811 mlx5e_rep_indr_register_block(rpriv, netdev);
813 case NETDEV_UNREGISTER:
814 mlx5e_rep_indr_unregister_block(netdev);
820 static struct mlx5e_neigh_hash_entry *
821 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
822 struct mlx5e_neigh *m_neigh);
824 static int mlx5e_rep_netevent_event(struct notifier_block *nb,
825 unsigned long event, void *ptr)
827 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
828 neigh_update.netevent_nb);
829 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
830 struct net_device *netdev = rpriv->netdev;
831 struct mlx5e_priv *priv = netdev_priv(netdev);
832 struct mlx5e_neigh_hash_entry *nhe = NULL;
833 struct mlx5e_neigh m_neigh = {};
834 struct neigh_parms *p;
839 case NETEVENT_NEIGH_UPDATE:
841 #if IS_ENABLED(CONFIG_IPV6)
842 if (n->tbl != &nd_tbl && n->tbl != &arp_tbl)
844 if (n->tbl != &arp_tbl)
848 m_neigh.dev = n->dev;
849 m_neigh.family = n->ops->family;
850 memcpy(&m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
852 /* We are in atomic context and can't take RTNL mutex, so use
853 * spin_lock_bh to lookup the neigh table. bh is used since
854 * netevent can be called from a softirq context.
856 spin_lock_bh(&neigh_update->encap_lock);
857 nhe = mlx5e_rep_neigh_entry_lookup(priv, &m_neigh);
859 spin_unlock_bh(&neigh_update->encap_lock);
863 /* This assignment is valid as long as the the neigh reference
868 /* Take a reference to ensure the neighbour and mlx5 encap
869 * entry won't be destructed until we drop the reference in
873 mlx5e_rep_neigh_entry_hold(nhe);
875 if (!queue_work(priv->wq, &nhe->neigh_update_work)) {
876 mlx5e_rep_neigh_entry_release(nhe);
879 spin_unlock_bh(&neigh_update->encap_lock);
882 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
885 /* We check the device is present since we don't care about
886 * changes in the default table, we only care about changes
887 * done per device delay prob time parameter.
889 #if IS_ENABLED(CONFIG_IPV6)
890 if (!p->dev || (p->tbl != &nd_tbl && p->tbl != &arp_tbl))
892 if (!p->dev || p->tbl != &arp_tbl)
896 /* We are in atomic context and can't take RTNL mutex,
897 * so use spin_lock_bh to walk the neigh list and look for
898 * the relevant device. bh is used since netevent can be
899 * called from a softirq context.
901 spin_lock_bh(&neigh_update->encap_lock);
902 list_for_each_entry(nhe, &neigh_update->neigh_list, neigh_list) {
903 if (p->dev == nhe->m_neigh.dev) {
908 spin_unlock_bh(&neigh_update->encap_lock);
912 neigh_update->min_interval = min_t(unsigned long,
913 NEIGH_VAR(p, DELAY_PROBE_TIME),
914 neigh_update->min_interval);
915 mlx5_fc_update_sampling_interval(priv->mdev,
916 neigh_update->min_interval);
922 static const struct rhashtable_params mlx5e_neigh_ht_params = {
923 .head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
924 .key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
925 .key_len = sizeof(struct mlx5e_neigh),
926 .automatic_shrinking = true,
929 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv *rpriv)
931 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
934 err = rhashtable_init(&neigh_update->neigh_ht, &mlx5e_neigh_ht_params);
938 INIT_LIST_HEAD(&neigh_update->neigh_list);
939 spin_lock_init(&neigh_update->encap_lock);
940 INIT_DELAYED_WORK(&neigh_update->neigh_stats_work,
941 mlx5e_rep_neigh_stats_work);
942 mlx5e_rep_neigh_update_init_interval(rpriv);
944 rpriv->neigh_update.netevent_nb.notifier_call = mlx5e_rep_netevent_event;
945 err = register_netevent_notifier(&rpriv->neigh_update.netevent_nb);
951 rhashtable_destroy(&neigh_update->neigh_ht);
955 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv *rpriv)
957 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
958 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
960 unregister_netevent_notifier(&neigh_update->netevent_nb);
962 flush_workqueue(priv->wq); /* flush neigh update works */
964 cancel_delayed_work_sync(&rpriv->neigh_update.neigh_stats_work);
966 rhashtable_destroy(&neigh_update->neigh_ht);
969 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv *priv,
970 struct mlx5e_neigh_hash_entry *nhe)
972 struct mlx5e_rep_priv *rpriv = priv->ppriv;
975 err = rhashtable_insert_fast(&rpriv->neigh_update.neigh_ht,
977 mlx5e_neigh_ht_params);
981 list_add(&nhe->neigh_list, &rpriv->neigh_update.neigh_list);
986 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_priv *priv,
987 struct mlx5e_neigh_hash_entry *nhe)
989 struct mlx5e_rep_priv *rpriv = priv->ppriv;
991 spin_lock_bh(&rpriv->neigh_update.encap_lock);
993 list_del(&nhe->neigh_list);
995 rhashtable_remove_fast(&rpriv->neigh_update.neigh_ht,
997 mlx5e_neigh_ht_params);
998 spin_unlock_bh(&rpriv->neigh_update.encap_lock);
1001 /* This function must only be called under RTNL lock or under the
1002 * representor's encap_lock in case RTNL mutex can't be held.
1004 static struct mlx5e_neigh_hash_entry *
1005 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
1006 struct mlx5e_neigh *m_neigh)
1008 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1009 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
1011 return rhashtable_lookup_fast(&neigh_update->neigh_ht, m_neigh,
1012 mlx5e_neigh_ht_params);
1015 static int mlx5e_rep_neigh_entry_create(struct mlx5e_priv *priv,
1016 struct mlx5e_encap_entry *e,
1017 struct mlx5e_neigh_hash_entry **nhe)
1021 *nhe = kzalloc(sizeof(**nhe), GFP_KERNEL);
1025 memcpy(&(*nhe)->m_neigh, &e->m_neigh, sizeof(e->m_neigh));
1026 INIT_WORK(&(*nhe)->neigh_update_work, mlx5e_rep_neigh_update);
1027 INIT_LIST_HEAD(&(*nhe)->encap_list);
1028 refcount_set(&(*nhe)->refcnt, 1);
1030 err = mlx5e_rep_neigh_entry_insert(priv, *nhe);
1040 static void mlx5e_rep_neigh_entry_destroy(struct mlx5e_priv *priv,
1041 struct mlx5e_neigh_hash_entry *nhe)
1043 /* The neigh hash entry must be removed from the hash table regardless
1044 * of the reference count value, so it won't be found by the next
1045 * neigh notification call. The neigh hash entry reference count is
1046 * incremented only during creation and neigh notification calls and
1047 * protects from freeing the nhe struct.
1049 mlx5e_rep_neigh_entry_remove(priv, nhe);
1050 mlx5e_rep_neigh_entry_release(nhe);
1053 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
1054 struct mlx5e_encap_entry *e)
1056 struct mlx5e_neigh_hash_entry *nhe;
1059 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
1061 err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
1065 list_add(&e->encap_list, &nhe->encap_list);
1069 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
1070 struct mlx5e_encap_entry *e)
1072 struct mlx5e_neigh_hash_entry *nhe;
1074 list_del(&e->encap_list);
1075 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
1077 if (list_empty(&nhe->encap_list))
1078 mlx5e_rep_neigh_entry_destroy(priv, nhe);
1081 static int mlx5e_vf_rep_open(struct net_device *dev)
1083 struct mlx5e_priv *priv = netdev_priv(dev);
1084 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1085 struct mlx5_eswitch_rep *rep = rpriv->rep;
1088 mutex_lock(&priv->state_lock);
1089 err = mlx5e_open_locked(dev);
1093 if (!mlx5_modify_vport_admin_state(priv->mdev,
1094 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1095 rep->vport, MLX5_VPORT_ADMIN_STATE_UP))
1096 netif_carrier_on(dev);
1099 mutex_unlock(&priv->state_lock);
1103 static int mlx5e_vf_rep_close(struct net_device *dev)
1105 struct mlx5e_priv *priv = netdev_priv(dev);
1106 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1107 struct mlx5_eswitch_rep *rep = rpriv->rep;
1110 mutex_lock(&priv->state_lock);
1111 mlx5_modify_vport_admin_state(priv->mdev,
1112 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1113 rep->vport, MLX5_VPORT_ADMIN_STATE_DOWN);
1114 ret = mlx5e_close_locked(dev);
1115 mutex_unlock(&priv->state_lock);
1119 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
1120 char *buf, size_t len)
1122 struct mlx5e_priv *priv = netdev_priv(dev);
1123 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1124 struct mlx5_eswitch_rep *rep = rpriv->rep;
1127 ret = snprintf(buf, len, "%d", rep->vport - 1);
1135 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
1136 struct tc_cls_flower_offload *cls_flower, int flags)
1138 switch (cls_flower->command) {
1139 case TC_CLSFLOWER_REPLACE:
1140 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
1142 case TC_CLSFLOWER_DESTROY:
1143 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
1145 case TC_CLSFLOWER_STATS:
1146 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
1153 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
1156 struct mlx5e_priv *priv = cb_priv;
1159 case TC_SETUP_CLSFLOWER:
1160 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS |
1161 MLX5E_TC_ESW_OFFLOAD);
1167 static int mlx5e_rep_setup_tc_block(struct net_device *dev,
1168 struct tc_block_offload *f)
1170 struct mlx5e_priv *priv = netdev_priv(dev);
1172 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1175 switch (f->command) {
1177 return tcf_block_cb_register(f->block, mlx5e_rep_setup_tc_cb,
1178 priv, priv, f->extack);
1179 case TC_BLOCK_UNBIND:
1180 tcf_block_cb_unregister(f->block, mlx5e_rep_setup_tc_cb, priv);
1187 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
1191 case TC_SETUP_BLOCK:
1192 return mlx5e_rep_setup_tc_block(dev, type_data);
1198 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
1200 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1201 struct mlx5_eswitch_rep *rep;
1203 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
1206 if (!rpriv) /* non vport rep mlx5e instances don't use this field */
1210 return (rep->vport == FDB_UPLINK_VPORT);
1213 static bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
1216 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1224 mlx5e_get_sw_stats64(const struct net_device *dev,
1225 struct rtnl_link_stats64 *stats)
1227 struct mlx5e_priv *priv = netdev_priv(dev);
1228 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
1230 mlx5e_rep_update_sw_counters(priv);
1232 stats->rx_packets = sstats->rx_packets;
1233 stats->rx_bytes = sstats->rx_bytes;
1234 stats->tx_packets = sstats->tx_packets;
1235 stats->tx_bytes = sstats->tx_bytes;
1237 stats->tx_dropped = sstats->tx_queue_dropped;
1242 static int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
1246 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1247 return mlx5e_get_sw_stats64(dev, sp);
1254 mlx5e_vf_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
1256 struct mlx5e_priv *priv = netdev_priv(dev);
1258 /* update HW stats in background for next time */
1259 mlx5e_queue_update_stats(priv);
1260 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
1263 static int mlx5e_vf_rep_change_mtu(struct net_device *netdev, int new_mtu)
1265 return mlx5e_change_mtu(netdev, new_mtu, NULL);
1268 static int mlx5e_uplink_rep_change_mtu(struct net_device *netdev, int new_mtu)
1270 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
1273 static int mlx5e_uplink_rep_set_mac(struct net_device *netdev, void *addr)
1275 struct sockaddr *saddr = addr;
1277 if (!is_valid_ether_addr(saddr->sa_data))
1278 return -EADDRNOTAVAIL;
1280 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
1284 static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
1285 .switchdev_port_attr_get = mlx5e_attr_get,
1288 static const struct net_device_ops mlx5e_netdev_ops_vf_rep = {
1289 .ndo_open = mlx5e_vf_rep_open,
1290 .ndo_stop = mlx5e_vf_rep_close,
1291 .ndo_start_xmit = mlx5e_xmit,
1292 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
1293 .ndo_setup_tc = mlx5e_rep_setup_tc,
1294 .ndo_get_stats64 = mlx5e_vf_rep_get_stats,
1295 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
1296 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
1297 .ndo_change_mtu = mlx5e_vf_rep_change_mtu,
1300 static const struct net_device_ops mlx5e_netdev_ops_uplink_rep = {
1301 .ndo_open = mlx5e_open,
1302 .ndo_stop = mlx5e_close,
1303 .ndo_start_xmit = mlx5e_xmit,
1304 .ndo_set_mac_address = mlx5e_uplink_rep_set_mac,
1305 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
1306 .ndo_setup_tc = mlx5e_rep_setup_tc,
1307 .ndo_get_stats64 = mlx5e_get_stats,
1308 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
1309 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
1310 .ndo_change_mtu = mlx5e_uplink_rep_change_mtu,
1311 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
1312 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
1313 .ndo_features_check = mlx5e_features_check,
1314 .ndo_set_vf_mac = mlx5e_set_vf_mac,
1315 .ndo_set_vf_rate = mlx5e_set_vf_rate,
1316 .ndo_get_vf_config = mlx5e_get_vf_config,
1317 .ndo_get_vf_stats = mlx5e_get_vf_stats,
1320 bool mlx5e_eswitch_rep(struct net_device *netdev)
1322 if (netdev->netdev_ops == &mlx5e_netdev_ops_vf_rep ||
1323 netdev->netdev_ops == &mlx5e_netdev_ops_uplink_rep)
1329 static void mlx5e_build_rep_params(struct net_device *netdev)
1331 struct mlx5e_priv *priv = netdev_priv(netdev);
1332 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1333 struct mlx5_eswitch_rep *rep = rpriv->rep;
1334 struct mlx5_core_dev *mdev = priv->mdev;
1335 struct mlx5e_params *params;
1337 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
1338 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
1339 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1341 params = &priv->channels.params;
1342 params->hard_mtu = MLX5E_ETH_HARD_MTU;
1343 params->sw_mtu = netdev->mtu;
1346 if (rep->vport == FDB_UPLINK_VPORT)
1347 params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
1349 params->log_sq_size = MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE;
1352 mlx5e_build_rq_params(mdev, params);
1354 /* CQ moderation params */
1355 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
1356 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
1360 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
1363 mlx5e_build_rss_params(&priv->rss_params, params->num_channels);
1366 static void mlx5e_build_rep_netdev(struct net_device *netdev)
1368 struct mlx5e_priv *priv = netdev_priv(netdev);
1369 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1370 struct mlx5_eswitch_rep *rep = rpriv->rep;
1371 struct mlx5_core_dev *mdev = priv->mdev;
1373 if (rep->vport == FDB_UPLINK_VPORT) {
1374 SET_NETDEV_DEV(netdev, &priv->mdev->pdev->dev);
1375 netdev->netdev_ops = &mlx5e_netdev_ops_uplink_rep;
1376 /* we want a persistent mac for the uplink rep */
1377 mlx5_query_nic_vport_mac_address(mdev, 0, netdev->dev_addr);
1378 netdev->ethtool_ops = &mlx5e_uplink_rep_ethtool_ops;
1379 #ifdef CONFIG_MLX5_CORE_EN_DCB
1380 if (MLX5_CAP_GEN(mdev, qos))
1381 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
1384 netdev->netdev_ops = &mlx5e_netdev_ops_vf_rep;
1385 eth_hw_addr_random(netdev);
1386 netdev->ethtool_ops = &mlx5e_vf_rep_ethtool_ops;
1389 netdev->watchdog_timeo = 15 * HZ;
1392 netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
1394 netdev->features |= NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
1395 netdev->hw_features |= NETIF_F_HW_TC;
1397 netdev->hw_features |= NETIF_F_SG;
1398 netdev->hw_features |= NETIF_F_IP_CSUM;
1399 netdev->hw_features |= NETIF_F_IPV6_CSUM;
1400 netdev->hw_features |= NETIF_F_GRO;
1401 netdev->hw_features |= NETIF_F_TSO;
1402 netdev->hw_features |= NETIF_F_TSO6;
1403 netdev->hw_features |= NETIF_F_RXCSUM;
1405 if (rep->vport != FDB_UPLINK_VPORT)
1406 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1408 netdev->features |= netdev->hw_features;
1411 static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
1412 struct net_device *netdev,
1413 const struct mlx5e_profile *profile,
1416 struct mlx5e_priv *priv = netdev_priv(netdev);
1419 err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
1423 priv->channels.params.num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;
1425 mlx5e_build_rep_params(netdev);
1426 mlx5e_build_rep_netdev(netdev);
1428 mlx5e_timestamp_init(priv);
1433 static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
1435 mlx5e_netdev_cleanup(priv->netdev, priv);
1438 static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
1440 struct ttc_params ttc_params = {};
1443 priv->fs.ns = mlx5_get_flow_namespace(priv->mdev,
1444 MLX5_FLOW_NAMESPACE_KERNEL);
1446 /* The inner_ttc in the ttc params is intentionally not set */
1447 ttc_params.any_tt_tirn = priv->direct_tir[0].tirn;
1448 mlx5e_set_ttc_ft_params(&ttc_params);
1449 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
1450 ttc_params.indir_tirn[tt] = priv->indir_tir[tt].tirn;
1452 err = mlx5e_create_ttc_table(priv, &ttc_params, &priv->fs.ttc);
1454 netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n", err);
1460 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
1462 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1463 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1464 struct mlx5_eswitch_rep *rep = rpriv->rep;
1465 struct mlx5_flow_handle *flow_rule;
1466 struct mlx5_flow_destination dest;
1468 dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1469 dest.tir_num = priv->direct_tir[0].tirn;
1470 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
1473 if (IS_ERR(flow_rule))
1474 return PTR_ERR(flow_rule);
1475 rpriv->vport_rx_rule = flow_rule;
1479 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
1481 struct mlx5_core_dev *mdev = priv->mdev;
1484 mlx5e_init_l2_addr(priv);
1486 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
1488 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
1492 err = mlx5e_create_indirect_rqt(priv);
1494 goto err_close_drop_rq;
1496 err = mlx5e_create_direct_rqts(priv);
1498 goto err_destroy_indirect_rqts;
1500 err = mlx5e_create_indirect_tirs(priv, false);
1502 goto err_destroy_direct_rqts;
1504 err = mlx5e_create_direct_tirs(priv);
1506 goto err_destroy_indirect_tirs;
1508 err = mlx5e_create_rep_ttc_table(priv);
1510 goto err_destroy_direct_tirs;
1512 err = mlx5e_create_rep_vport_rx_rule(priv);
1514 goto err_destroy_ttc_table;
1518 err_destroy_ttc_table:
1519 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1520 err_destroy_direct_tirs:
1521 mlx5e_destroy_direct_tirs(priv);
1522 err_destroy_indirect_tirs:
1523 mlx5e_destroy_indirect_tirs(priv, false);
1524 err_destroy_direct_rqts:
1525 mlx5e_destroy_direct_rqts(priv);
1526 err_destroy_indirect_rqts:
1527 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1529 mlx5e_close_drop_rq(&priv->drop_rq);
1533 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
1535 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1537 mlx5_del_flow_rules(rpriv->vport_rx_rule);
1538 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1539 mlx5e_destroy_direct_tirs(priv);
1540 mlx5e_destroy_indirect_tirs(priv, false);
1541 mlx5e_destroy_direct_rqts(priv);
1542 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1543 mlx5e_close_drop_rq(&priv->drop_rq);
1546 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1548 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1549 struct mlx5_rep_uplink_priv *uplink_priv;
1552 err = mlx5e_create_tises(priv);
1554 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1558 if (rpriv->rep->vport == FDB_UPLINK_VPORT) {
1559 uplink_priv = &rpriv->uplink_priv;
1561 /* init shared tc flow table */
1562 err = mlx5e_tc_esw_init(&uplink_priv->tc_ht);
1566 /* init indirect block notifications */
1567 INIT_LIST_HEAD(&uplink_priv->tc_indr_block_priv_list);
1568 uplink_priv->netdevice_nb.notifier_call = mlx5e_nic_rep_netdevice_event;
1569 err = register_netdevice_notifier(&uplink_priv->netdevice_nb);
1571 mlx5_core_err(priv->mdev, "Failed to register netdev notifier\n");
1572 goto tc_esw_cleanup;
1579 mlx5e_tc_esw_cleanup(&uplink_priv->tc_ht);
1581 for (tc = 0; tc < priv->profile->max_tc; tc++)
1582 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
1586 static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
1588 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1591 for (tc = 0; tc < priv->profile->max_tc; tc++)
1592 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
1594 if (rpriv->rep->vport == FDB_UPLINK_VPORT) {
1595 /* clean indirect TC block notifications */
1596 unregister_netdevice_notifier(&rpriv->uplink_priv.netdevice_nb);
1597 mlx5e_rep_indr_clean_block_privs(rpriv);
1599 /* delete shared tc flow table */
1600 mlx5e_tc_esw_cleanup(&rpriv->uplink_priv.tc_ht);
1604 static void mlx5e_vf_rep_enable(struct mlx5e_priv *priv)
1606 struct net_device *netdev = priv->netdev;
1607 struct mlx5_core_dev *mdev = priv->mdev;
1610 netdev->min_mtu = ETH_MIN_MTU;
1611 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
1612 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1615 static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
1617 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
1618 struct mlx5_eqe *eqe = data;
1620 if (event != MLX5_EVENT_TYPE_PORT_CHANGE)
1623 switch (eqe->sub_type) {
1624 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
1625 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
1626 queue_work(priv->wq, &priv->update_carrier_work);
1635 static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
1637 struct net_device *netdev = priv->netdev;
1638 struct mlx5_core_dev *mdev = priv->mdev;
1641 netdev->min_mtu = ETH_MIN_MTU;
1642 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
1643 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1644 mlx5e_set_dev_port_mtu(priv);
1646 mlx5_lag_add(mdev, netdev);
1647 priv->events_nb.notifier_call = uplink_rep_async_event;
1648 mlx5_notifier_register(mdev, &priv->events_nb);
1649 #ifdef CONFIG_MLX5_CORE_EN_DCB
1650 mlx5e_dcbnl_initialize(priv);
1651 mlx5e_dcbnl_init_app(priv);
1655 static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
1657 struct mlx5_core_dev *mdev = priv->mdev;
1659 #ifdef CONFIG_MLX5_CORE_EN_DCB
1660 mlx5e_dcbnl_delete_app(priv);
1662 mlx5_notifier_unregister(mdev, &priv->events_nb);
1663 mlx5_lag_remove(mdev);
1666 static const struct mlx5e_profile mlx5e_vf_rep_profile = {
1667 .init = mlx5e_init_rep,
1668 .cleanup = mlx5e_cleanup_rep,
1669 .init_rx = mlx5e_init_rep_rx,
1670 .cleanup_rx = mlx5e_cleanup_rep_rx,
1671 .init_tx = mlx5e_init_rep_tx,
1672 .cleanup_tx = mlx5e_cleanup_rep_tx,
1673 .enable = mlx5e_vf_rep_enable,
1674 .update_stats = mlx5e_vf_rep_update_hw_counters,
1675 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1676 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1680 static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
1681 .init = mlx5e_init_rep,
1682 .cleanup = mlx5e_cleanup_rep,
1683 .init_rx = mlx5e_init_rep_rx,
1684 .cleanup_rx = mlx5e_cleanup_rep_rx,
1685 .init_tx = mlx5e_init_rep_tx,
1686 .cleanup_tx = mlx5e_cleanup_rep_tx,
1687 .enable = mlx5e_uplink_rep_enable,
1688 .disable = mlx5e_uplink_rep_disable,
1689 .update_stats = mlx5e_uplink_rep_update_hw_counters,
1690 .update_carrier = mlx5e_update_carrier,
1691 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1692 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1693 .max_tc = MLX5E_MAX_NUM_TC,
1696 /* e-Switch vport representors */
1698 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1700 const struct mlx5e_profile *profile;
1701 struct mlx5e_rep_priv *rpriv;
1702 struct net_device *netdev;
1705 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1709 /* rpriv->rep to be looked up when profile->init() is called */
1712 nch = mlx5e_get_max_num_channels(dev);
1713 profile = (rep->vport == FDB_UPLINK_VPORT) ? &mlx5e_uplink_rep_profile : &mlx5e_vf_rep_profile;
1714 netdev = mlx5e_create_netdev(dev, profile, nch, rpriv);
1716 pr_warn("Failed to create representor netdev for vport %d\n",
1722 rpriv->netdev = netdev;
1723 rep->rep_if[REP_ETH].priv = rpriv;
1724 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1726 if (rep->vport == FDB_UPLINK_VPORT) {
1727 err = mlx5e_create_mdev_resources(dev);
1729 goto err_destroy_netdev;
1732 err = mlx5e_attach_netdev(netdev_priv(netdev));
1734 pr_warn("Failed to attach representor netdev for vport %d\n",
1736 goto err_destroy_mdev_resources;
1739 err = mlx5e_rep_neigh_init(rpriv);
1741 pr_warn("Failed to initialized neighbours handling for vport %d\n",
1743 goto err_detach_netdev;
1746 err = register_netdev(netdev);
1748 pr_warn("Failed to register representor netdev for vport %d\n",
1750 goto err_neigh_cleanup;
1756 mlx5e_rep_neigh_cleanup(rpriv);
1759 mlx5e_detach_netdev(netdev_priv(netdev));
1761 err_destroy_mdev_resources:
1762 if (rep->vport == FDB_UPLINK_VPORT)
1763 mlx5e_destroy_mdev_resources(dev);
1766 mlx5e_destroy_netdev(netdev_priv(netdev));
1772 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1774 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1775 struct net_device *netdev = rpriv->netdev;
1776 struct mlx5e_priv *priv = netdev_priv(netdev);
1777 void *ppriv = priv->ppriv;
1779 unregister_netdev(netdev);
1780 mlx5e_rep_neigh_cleanup(rpriv);
1781 mlx5e_detach_netdev(priv);
1782 if (rep->vport == FDB_UPLINK_VPORT)
1783 mlx5e_destroy_mdev_resources(priv->mdev);
1784 mlx5e_destroy_netdev(priv);
1785 kfree(ppriv); /* mlx5e_rep_priv */
1788 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1790 struct mlx5e_rep_priv *rpriv;
1792 rpriv = mlx5e_rep_to_rep_priv(rep);
1794 return rpriv->netdev;
1797 void mlx5e_rep_register_vport_reps(struct mlx5_core_dev *mdev)
1799 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1800 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1803 for (vport = 0; vport < total_vfs; vport++) {
1804 struct mlx5_eswitch_rep_if rep_if = {};
1806 rep_if.load = mlx5e_vport_rep_load;
1807 rep_if.unload = mlx5e_vport_rep_unload;
1808 rep_if.get_proto_dev = mlx5e_vport_rep_get_proto_dev;
1809 mlx5_eswitch_register_vport_rep(esw, vport, &rep_if, REP_ETH);
1813 void mlx5e_rep_unregister_vport_reps(struct mlx5_core_dev *mdev)
1815 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1816 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1819 for (vport = total_vfs - 1; vport >= 0; vport--)
1820 mlx5_eswitch_unregister_vport_rep(esw, vport, REP_ETH);