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"
47 #include "lib/port_tun.h"
49 #define MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE \
50 max(0x7, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)
51 #define MLX5E_REP_PARAMS_DEF_NUM_CHANNELS 1
53 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
55 struct mlx5e_rep_indr_block_priv {
56 struct net_device *netdev;
57 struct mlx5e_rep_priv *rpriv;
59 struct list_head list;
62 static void mlx5e_rep_indr_unregister_block(struct mlx5e_rep_priv *rpriv,
63 struct net_device *netdev);
65 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
66 struct ethtool_drvinfo *drvinfo)
68 struct mlx5e_priv *priv = netdev_priv(dev);
69 struct mlx5_core_dev *mdev = priv->mdev;
71 strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
72 sizeof(drvinfo->driver));
73 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
74 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
76 fw_rev_maj(mdev), fw_rev_min(mdev),
77 fw_rev_sub(mdev), mdev->board_id);
80 static void mlx5e_uplink_rep_get_drvinfo(struct net_device *dev,
81 struct ethtool_drvinfo *drvinfo)
83 struct mlx5e_priv *priv = netdev_priv(dev);
85 mlx5e_rep_get_drvinfo(dev, drvinfo);
86 strlcpy(drvinfo->bus_info, pci_name(priv->mdev->pdev),
87 sizeof(drvinfo->bus_info));
90 static const struct counter_desc sw_rep_stats_desc[] = {
91 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
92 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
93 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
94 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
104 static const struct counter_desc vport_rep_stats_desc[] = {
105 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_packets) },
106 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_bytes) },
107 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_packets) },
108 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_bytes) },
111 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
112 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
114 static void mlx5e_rep_get_strings(struct net_device *dev,
115 u32 stringset, uint8_t *data)
121 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
122 strcpy(data + (i * ETH_GSTRING_LEN),
123 sw_rep_stats_desc[i].format);
124 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
125 strcpy(data + (i * ETH_GSTRING_LEN),
126 vport_rep_stats_desc[j].format);
131 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv *priv)
133 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
134 struct mlx5e_rep_priv *rpriv = priv->ppriv;
135 struct mlx5_eswitch_rep *rep = rpriv->rep;
136 struct rtnl_link_stats64 *vport_stats;
137 struct ifla_vf_stats vf_stats;
140 err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
142 pr_warn("vport %d error %d reading stats\n", rep->vport, err);
146 vport_stats = &priv->stats.vf_vport;
147 /* flip tx/rx as we are reporting the counters for the switch vport */
148 vport_stats->rx_packets = vf_stats.tx_packets;
149 vport_stats->rx_bytes = vf_stats.tx_bytes;
150 vport_stats->tx_packets = vf_stats.rx_packets;
151 vport_stats->tx_bytes = vf_stats.rx_bytes;
154 static void mlx5e_uplink_rep_update_hw_counters(struct mlx5e_priv *priv)
156 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
157 struct rtnl_link_stats64 *vport_stats;
159 mlx5e_grp_802_3_update_stats(priv);
161 vport_stats = &priv->stats.vf_vport;
163 vport_stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok);
164 vport_stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok);
165 vport_stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok);
166 vport_stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok);
169 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv *priv)
171 struct mlx5e_sw_stats *s = &priv->stats.sw;
172 struct rtnl_link_stats64 stats64 = {};
174 memset(s, 0, sizeof(*s));
175 mlx5e_fold_sw_stats64(priv, &stats64);
177 s->rx_packets = stats64.rx_packets;
178 s->rx_bytes = stats64.rx_bytes;
179 s->tx_packets = stats64.tx_packets;
180 s->tx_bytes = stats64.tx_bytes;
181 s->tx_queue_dropped = stats64.tx_dropped;
184 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
185 struct ethtool_stats *stats, u64 *data)
187 struct mlx5e_priv *priv = netdev_priv(dev);
193 mutex_lock(&priv->state_lock);
194 mlx5e_rep_update_sw_counters(priv);
195 priv->profile->update_stats(priv);
196 mutex_unlock(&priv->state_lock);
198 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
199 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
200 sw_rep_stats_desc, i);
202 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
203 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
204 vport_rep_stats_desc, j);
207 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
211 return NUM_VPORT_REP_SW_COUNTERS + NUM_VPORT_REP_HW_COUNTERS;
217 static void mlx5e_rep_get_ringparam(struct net_device *dev,
218 struct ethtool_ringparam *param)
220 struct mlx5e_priv *priv = netdev_priv(dev);
222 mlx5e_ethtool_get_ringparam(priv, param);
225 static int mlx5e_rep_set_ringparam(struct net_device *dev,
226 struct ethtool_ringparam *param)
228 struct mlx5e_priv *priv = netdev_priv(dev);
230 return mlx5e_ethtool_set_ringparam(priv, param);
233 static int mlx5e_replace_rep_vport_rx_rule(struct mlx5e_priv *priv,
234 struct mlx5_flow_destination *dest)
236 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
237 struct mlx5e_rep_priv *rpriv = priv->ppriv;
238 struct mlx5_eswitch_rep *rep = rpriv->rep;
239 struct mlx5_flow_handle *flow_rule;
241 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
244 if (IS_ERR(flow_rule))
245 return PTR_ERR(flow_rule);
247 mlx5_del_flow_rules(rpriv->vport_rx_rule);
248 rpriv->vport_rx_rule = flow_rule;
252 static void mlx5e_rep_get_channels(struct net_device *dev,
253 struct ethtool_channels *ch)
255 struct mlx5e_priv *priv = netdev_priv(dev);
257 mlx5e_ethtool_get_channels(priv, ch);
260 static int mlx5e_rep_set_channels(struct net_device *dev,
261 struct ethtool_channels *ch)
263 struct mlx5e_priv *priv = netdev_priv(dev);
264 u16 curr_channels_amount = priv->channels.params.num_channels;
265 u32 new_channels_amount = ch->combined_count;
266 struct mlx5_flow_destination new_dest;
269 err = mlx5e_ethtool_set_channels(priv, ch);
273 if (curr_channels_amount == 1 && new_channels_amount > 1) {
274 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
275 new_dest.ft = priv->fs.ttc.ft.t;
276 } else if (new_channels_amount == 1 && curr_channels_amount > 1) {
277 new_dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
278 new_dest.tir_num = priv->direct_tir[0].tirn;
283 err = mlx5e_replace_rep_vport_rx_rule(priv, &new_dest);
285 netdev_warn(priv->netdev, "Failed to update vport rx rule, when going from (%d) channels to (%d) channels\n",
286 curr_channels_amount, new_channels_amount);
293 static int mlx5e_rep_get_coalesce(struct net_device *netdev,
294 struct ethtool_coalesce *coal)
296 struct mlx5e_priv *priv = netdev_priv(netdev);
298 return mlx5e_ethtool_get_coalesce(priv, coal);
301 static int mlx5e_rep_set_coalesce(struct net_device *netdev,
302 struct ethtool_coalesce *coal)
304 struct mlx5e_priv *priv = netdev_priv(netdev);
306 return mlx5e_ethtool_set_coalesce(priv, coal);
309 static u32 mlx5e_rep_get_rxfh_key_size(struct net_device *netdev)
311 struct mlx5e_priv *priv = netdev_priv(netdev);
313 return mlx5e_ethtool_get_rxfh_key_size(priv);
316 static u32 mlx5e_rep_get_rxfh_indir_size(struct net_device *netdev)
318 struct mlx5e_priv *priv = netdev_priv(netdev);
320 return mlx5e_ethtool_get_rxfh_indir_size(priv);
323 static void mlx5e_uplink_rep_get_pauseparam(struct net_device *netdev,
324 struct ethtool_pauseparam *pauseparam)
326 struct mlx5e_priv *priv = netdev_priv(netdev);
328 mlx5e_ethtool_get_pauseparam(priv, pauseparam);
331 static int mlx5e_uplink_rep_set_pauseparam(struct net_device *netdev,
332 struct ethtool_pauseparam *pauseparam)
334 struct mlx5e_priv *priv = netdev_priv(netdev);
336 return mlx5e_ethtool_set_pauseparam(priv, pauseparam);
339 static int mlx5e_uplink_rep_get_link_ksettings(struct net_device *netdev,
340 struct ethtool_link_ksettings *link_ksettings)
342 struct mlx5e_priv *priv = netdev_priv(netdev);
344 return mlx5e_ethtool_get_link_ksettings(priv, link_ksettings);
347 static int mlx5e_uplink_rep_set_link_ksettings(struct net_device *netdev,
348 const struct ethtool_link_ksettings *link_ksettings)
350 struct mlx5e_priv *priv = netdev_priv(netdev);
352 return mlx5e_ethtool_set_link_ksettings(priv, link_ksettings);
355 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
356 .get_drvinfo = mlx5e_rep_get_drvinfo,
357 .get_link = ethtool_op_get_link,
358 .get_strings = mlx5e_rep_get_strings,
359 .get_sset_count = mlx5e_rep_get_sset_count,
360 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
361 .get_ringparam = mlx5e_rep_get_ringparam,
362 .set_ringparam = mlx5e_rep_set_ringparam,
363 .get_channels = mlx5e_rep_get_channels,
364 .set_channels = mlx5e_rep_set_channels,
365 .get_coalesce = mlx5e_rep_get_coalesce,
366 .set_coalesce = mlx5e_rep_set_coalesce,
367 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
368 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
371 static const struct ethtool_ops mlx5e_uplink_rep_ethtool_ops = {
372 .get_drvinfo = mlx5e_uplink_rep_get_drvinfo,
373 .get_link = ethtool_op_get_link,
374 .get_strings = mlx5e_rep_get_strings,
375 .get_sset_count = mlx5e_rep_get_sset_count,
376 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
377 .get_ringparam = mlx5e_rep_get_ringparam,
378 .set_ringparam = mlx5e_rep_set_ringparam,
379 .get_channels = mlx5e_rep_get_channels,
380 .set_channels = mlx5e_rep_set_channels,
381 .get_coalesce = mlx5e_rep_get_coalesce,
382 .set_coalesce = mlx5e_rep_set_coalesce,
383 .get_link_ksettings = mlx5e_uplink_rep_get_link_ksettings,
384 .set_link_ksettings = mlx5e_uplink_rep_set_link_ksettings,
385 .get_rxfh_key_size = mlx5e_rep_get_rxfh_key_size,
386 .get_rxfh_indir_size = mlx5e_rep_get_rxfh_indir_size,
387 .get_pauseparam = mlx5e_uplink_rep_get_pauseparam,
388 .set_pauseparam = mlx5e_uplink_rep_set_pauseparam,
391 static int mlx5e_rep_get_port_parent_id(struct net_device *dev,
392 struct netdev_phys_item_id *ppid)
394 struct mlx5_eswitch *esw;
395 struct mlx5e_priv *priv;
398 priv = netdev_priv(dev);
399 esw = priv->mdev->priv.eswitch;
401 if (esw->mode == MLX5_ESWITCH_NONE)
404 parent_id = mlx5_query_nic_system_image_guid(priv->mdev);
405 ppid->id_len = sizeof(parent_id);
406 memcpy(ppid->id, &parent_id, sizeof(parent_id));
411 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
412 struct mlx5_eswitch_rep *rep)
414 struct mlx5e_rep_sq *rep_sq, *tmp;
415 struct mlx5e_rep_priv *rpriv;
417 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
420 rpriv = mlx5e_rep_to_rep_priv(rep);
421 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
422 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
423 list_del(&rep_sq->list);
428 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
429 struct mlx5_eswitch_rep *rep,
430 u32 *sqns_array, int sqns_num)
432 struct mlx5_flow_handle *flow_rule;
433 struct mlx5e_rep_priv *rpriv;
434 struct mlx5e_rep_sq *rep_sq;
438 if (esw->mode != MLX5_ESWITCH_OFFLOADS)
441 rpriv = mlx5e_rep_to_rep_priv(rep);
442 for (i = 0; i < sqns_num; i++) {
443 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
449 /* Add re-inject rule to the PF/representor sqs */
450 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
453 if (IS_ERR(flow_rule)) {
454 err = PTR_ERR(flow_rule);
458 rep_sq->send_to_vport_rule = flow_rule;
459 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
464 mlx5e_sqs2vport_stop(esw, rep);
468 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
470 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
471 struct mlx5e_rep_priv *rpriv = priv->ppriv;
472 struct mlx5_eswitch_rep *rep = rpriv->rep;
473 struct mlx5e_channel *c;
474 int n, tc, num_sqs = 0;
478 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(*sqs), GFP_KERNEL);
482 for (n = 0; n < priv->channels.num; n++) {
483 c = priv->channels.c[n];
484 for (tc = 0; tc < c->num_tc; tc++)
485 sqs[num_sqs++] = c->sq[tc].sqn;
488 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
493 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
497 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
499 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
500 struct mlx5e_rep_priv *rpriv = priv->ppriv;
501 struct mlx5_eswitch_rep *rep = rpriv->rep;
503 mlx5e_sqs2vport_stop(esw, rep);
506 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv *rpriv)
508 #if IS_ENABLED(CONFIG_IPV6)
509 unsigned long ipv6_interval = NEIGH_VAR(&nd_tbl.parms,
512 unsigned long ipv6_interval = ~0UL;
514 unsigned long ipv4_interval = NEIGH_VAR(&arp_tbl.parms,
516 struct net_device *netdev = rpriv->netdev;
517 struct mlx5e_priv *priv = netdev_priv(netdev);
519 rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);
520 mlx5_fc_update_sampling_interval(priv->mdev, rpriv->neigh_update.min_interval);
523 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv)
525 struct mlx5e_rep_priv *rpriv = priv->ppriv;
526 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
528 mlx5_fc_queue_stats_work(priv->mdev,
529 &neigh_update->neigh_stats_work,
530 neigh_update->min_interval);
533 static void mlx5e_rep_neigh_stats_work(struct work_struct *work)
535 struct mlx5e_rep_priv *rpriv = container_of(work, struct mlx5e_rep_priv,
536 neigh_update.neigh_stats_work.work);
537 struct net_device *netdev = rpriv->netdev;
538 struct mlx5e_priv *priv = netdev_priv(netdev);
539 struct mlx5e_neigh_hash_entry *nhe;
542 if (!list_empty(&rpriv->neigh_update.neigh_list))
543 mlx5e_rep_queue_neigh_stats_work(priv);
545 list_for_each_entry(nhe, &rpriv->neigh_update.neigh_list, neigh_list)
546 mlx5e_tc_update_neigh_used_value(nhe);
551 static void mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry *nhe)
553 refcount_inc(&nhe->refcnt);
556 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry *nhe)
558 if (refcount_dec_and_test(&nhe->refcnt))
562 static void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
563 struct mlx5e_encap_entry *e,
564 bool neigh_connected,
565 unsigned char ha[ETH_ALEN])
567 struct ethhdr *eth = (struct ethhdr *)e->encap_header;
571 if ((e->flags & MLX5_ENCAP_ENTRY_VALID) &&
572 (!neigh_connected || !ether_addr_equal(e->h_dest, ha)))
573 mlx5e_tc_encap_flows_del(priv, e);
575 if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
576 ether_addr_copy(e->h_dest, ha);
577 ether_addr_copy(eth->h_dest, ha);
578 /* Update the encap source mac, in case that we delete
579 * the flows when encap source mac changed.
581 ether_addr_copy(eth->h_source, e->route_dev->dev_addr);
583 mlx5e_tc_encap_flows_add(priv, e);
587 static void mlx5e_rep_neigh_update(struct work_struct *work)
589 struct mlx5e_neigh_hash_entry *nhe =
590 container_of(work, struct mlx5e_neigh_hash_entry, neigh_update_work);
591 struct neighbour *n = nhe->n;
592 struct mlx5e_encap_entry *e;
593 unsigned char ha[ETH_ALEN];
594 struct mlx5e_priv *priv;
595 bool neigh_connected;
596 bool encap_connected;
601 /* If these parameters are changed after we release the lock,
602 * we'll receive another event letting us know about it.
603 * We use this lock to avoid inconsistency between the neigh validity
604 * and it's hw address.
606 read_lock_bh(&n->lock);
607 memcpy(ha, n->ha, ETH_ALEN);
608 nud_state = n->nud_state;
610 read_unlock_bh(&n->lock);
612 neigh_connected = (nud_state & NUD_VALID) && !dead;
614 list_for_each_entry(e, &nhe->encap_list, encap_list) {
615 encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
616 priv = netdev_priv(e->out_dev);
618 if (encap_connected != neigh_connected ||
619 !ether_addr_equal(e->h_dest, ha))
620 mlx5e_rep_update_flows(priv, e, neigh_connected, ha);
622 mlx5e_rep_neigh_entry_release(nhe);
627 static struct mlx5e_rep_indr_block_priv *
628 mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv *rpriv,
629 struct net_device *netdev)
631 struct mlx5e_rep_indr_block_priv *cb_priv;
633 /* All callback list access should be protected by RTNL. */
636 list_for_each_entry(cb_priv,
637 &rpriv->uplink_priv.tc_indr_block_priv_list,
639 if (cb_priv->netdev == netdev)
645 static void mlx5e_rep_indr_clean_block_privs(struct mlx5e_rep_priv *rpriv)
647 struct mlx5e_rep_indr_block_priv *cb_priv, *temp;
648 struct list_head *head = &rpriv->uplink_priv.tc_indr_block_priv_list;
650 list_for_each_entry_safe(cb_priv, temp, head, list) {
651 mlx5e_rep_indr_unregister_block(rpriv, cb_priv->netdev);
657 mlx5e_rep_indr_offload(struct net_device *netdev,
658 struct tc_cls_flower_offload *flower,
659 struct mlx5e_rep_indr_block_priv *indr_priv)
661 struct mlx5e_priv *priv = netdev_priv(indr_priv->rpriv->netdev);
662 int flags = MLX5E_TC_EGRESS | MLX5E_TC_ESW_OFFLOAD;
665 switch (flower->command) {
666 case TC_CLSFLOWER_REPLACE:
667 err = mlx5e_configure_flower(netdev, priv, flower, flags);
669 case TC_CLSFLOWER_DESTROY:
670 err = mlx5e_delete_flower(netdev, priv, flower, flags);
672 case TC_CLSFLOWER_STATS:
673 err = mlx5e_stats_flower(netdev, priv, flower, flags);
682 static int mlx5e_rep_indr_setup_block_cb(enum tc_setup_type type,
683 void *type_data, void *indr_priv)
685 struct mlx5e_rep_indr_block_priv *priv = indr_priv;
688 case TC_SETUP_CLSFLOWER:
689 return mlx5e_rep_indr_offload(priv->netdev, type_data, priv);
696 mlx5e_rep_indr_setup_tc_block(struct net_device *netdev,
697 struct mlx5e_rep_priv *rpriv,
698 struct tc_block_offload *f)
700 struct mlx5e_rep_indr_block_priv *indr_priv;
703 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
706 switch (f->command) {
708 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
712 indr_priv = kmalloc(sizeof(*indr_priv), GFP_KERNEL);
716 indr_priv->netdev = netdev;
717 indr_priv->rpriv = rpriv;
718 list_add(&indr_priv->list,
719 &rpriv->uplink_priv.tc_indr_block_priv_list);
721 err = tcf_block_cb_register(f->block,
722 mlx5e_rep_indr_setup_block_cb,
723 indr_priv, indr_priv, f->extack);
725 list_del(&indr_priv->list);
730 case TC_BLOCK_UNBIND:
731 indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
735 tcf_block_cb_unregister(f->block,
736 mlx5e_rep_indr_setup_block_cb,
738 list_del(&indr_priv->list);
749 int mlx5e_rep_indr_setup_tc_cb(struct net_device *netdev, void *cb_priv,
750 enum tc_setup_type type, void *type_data)
754 return mlx5e_rep_indr_setup_tc_block(netdev, cb_priv,
761 static int mlx5e_rep_indr_register_block(struct mlx5e_rep_priv *rpriv,
762 struct net_device *netdev)
766 err = __tc_indr_block_cb_register(netdev, rpriv,
767 mlx5e_rep_indr_setup_tc_cb,
770 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
772 mlx5_core_err(priv->mdev, "Failed to register remote block notifier for %s err=%d\n",
773 netdev_name(netdev), err);
778 static void mlx5e_rep_indr_unregister_block(struct mlx5e_rep_priv *rpriv,
779 struct net_device *netdev)
781 __tc_indr_block_cb_unregister(netdev, mlx5e_rep_indr_setup_tc_cb,
785 static int mlx5e_nic_rep_netdevice_event(struct notifier_block *nb,
786 unsigned long event, void *ptr)
788 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
789 uplink_priv.netdevice_nb);
790 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
791 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
793 if (!mlx5e_tc_tun_device_to_offload(priv, netdev) &&
794 !(is_vlan_dev(netdev) && vlan_dev_real_dev(netdev) == rpriv->netdev))
798 case NETDEV_REGISTER:
799 mlx5e_rep_indr_register_block(rpriv, netdev);
801 case NETDEV_UNREGISTER:
802 mlx5e_rep_indr_unregister_block(rpriv, netdev);
808 static struct mlx5e_neigh_hash_entry *
809 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
810 struct mlx5e_neigh *m_neigh);
812 static int mlx5e_rep_netevent_event(struct notifier_block *nb,
813 unsigned long event, void *ptr)
815 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
816 neigh_update.netevent_nb);
817 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
818 struct net_device *netdev = rpriv->netdev;
819 struct mlx5e_priv *priv = netdev_priv(netdev);
820 struct mlx5e_neigh_hash_entry *nhe = NULL;
821 struct mlx5e_neigh m_neigh = {};
822 struct neigh_parms *p;
827 case NETEVENT_NEIGH_UPDATE:
829 #if IS_ENABLED(CONFIG_IPV6)
830 if (n->tbl != &nd_tbl && n->tbl != &arp_tbl)
832 if (n->tbl != &arp_tbl)
836 m_neigh.dev = n->dev;
837 m_neigh.family = n->ops->family;
838 memcpy(&m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
840 /* We are in atomic context and can't take RTNL mutex, so use
841 * spin_lock_bh to lookup the neigh table. bh is used since
842 * netevent can be called from a softirq context.
844 spin_lock_bh(&neigh_update->encap_lock);
845 nhe = mlx5e_rep_neigh_entry_lookup(priv, &m_neigh);
847 spin_unlock_bh(&neigh_update->encap_lock);
851 /* This assignment is valid as long as the the neigh reference
856 /* Take a reference to ensure the neighbour and mlx5 encap
857 * entry won't be destructed until we drop the reference in
861 mlx5e_rep_neigh_entry_hold(nhe);
863 if (!queue_work(priv->wq, &nhe->neigh_update_work)) {
864 mlx5e_rep_neigh_entry_release(nhe);
867 spin_unlock_bh(&neigh_update->encap_lock);
870 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
873 /* We check the device is present since we don't care about
874 * changes in the default table, we only care about changes
875 * done per device delay prob time parameter.
877 #if IS_ENABLED(CONFIG_IPV6)
878 if (!p->dev || (p->tbl != &nd_tbl && p->tbl != &arp_tbl))
880 if (!p->dev || p->tbl != &arp_tbl)
884 /* We are in atomic context and can't take RTNL mutex,
885 * so use spin_lock_bh to walk the neigh list and look for
886 * the relevant device. bh is used since netevent can be
887 * called from a softirq context.
889 spin_lock_bh(&neigh_update->encap_lock);
890 list_for_each_entry(nhe, &neigh_update->neigh_list, neigh_list) {
891 if (p->dev == nhe->m_neigh.dev) {
896 spin_unlock_bh(&neigh_update->encap_lock);
900 neigh_update->min_interval = min_t(unsigned long,
901 NEIGH_VAR(p, DELAY_PROBE_TIME),
902 neigh_update->min_interval);
903 mlx5_fc_update_sampling_interval(priv->mdev,
904 neigh_update->min_interval);
910 static const struct rhashtable_params mlx5e_neigh_ht_params = {
911 .head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
912 .key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
913 .key_len = sizeof(struct mlx5e_neigh),
914 .automatic_shrinking = true,
917 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv *rpriv)
919 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
922 err = rhashtable_init(&neigh_update->neigh_ht, &mlx5e_neigh_ht_params);
926 INIT_LIST_HEAD(&neigh_update->neigh_list);
927 spin_lock_init(&neigh_update->encap_lock);
928 INIT_DELAYED_WORK(&neigh_update->neigh_stats_work,
929 mlx5e_rep_neigh_stats_work);
930 mlx5e_rep_neigh_update_init_interval(rpriv);
932 rpriv->neigh_update.netevent_nb.notifier_call = mlx5e_rep_netevent_event;
933 err = register_netevent_notifier(&rpriv->neigh_update.netevent_nb);
939 rhashtable_destroy(&neigh_update->neigh_ht);
943 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv *rpriv)
945 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
946 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
948 unregister_netevent_notifier(&neigh_update->netevent_nb);
950 flush_workqueue(priv->wq); /* flush neigh update works */
952 cancel_delayed_work_sync(&rpriv->neigh_update.neigh_stats_work);
954 rhashtable_destroy(&neigh_update->neigh_ht);
957 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv *priv,
958 struct mlx5e_neigh_hash_entry *nhe)
960 struct mlx5e_rep_priv *rpriv = priv->ppriv;
963 err = rhashtable_insert_fast(&rpriv->neigh_update.neigh_ht,
965 mlx5e_neigh_ht_params);
969 list_add(&nhe->neigh_list, &rpriv->neigh_update.neigh_list);
974 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_priv *priv,
975 struct mlx5e_neigh_hash_entry *nhe)
977 struct mlx5e_rep_priv *rpriv = priv->ppriv;
979 spin_lock_bh(&rpriv->neigh_update.encap_lock);
981 list_del(&nhe->neigh_list);
983 rhashtable_remove_fast(&rpriv->neigh_update.neigh_ht,
985 mlx5e_neigh_ht_params);
986 spin_unlock_bh(&rpriv->neigh_update.encap_lock);
989 /* This function must only be called under RTNL lock or under the
990 * representor's encap_lock in case RTNL mutex can't be held.
992 static struct mlx5e_neigh_hash_entry *
993 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
994 struct mlx5e_neigh *m_neigh)
996 struct mlx5e_rep_priv *rpriv = priv->ppriv;
997 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
999 return rhashtable_lookup_fast(&neigh_update->neigh_ht, m_neigh,
1000 mlx5e_neigh_ht_params);
1003 static int mlx5e_rep_neigh_entry_create(struct mlx5e_priv *priv,
1004 struct mlx5e_encap_entry *e,
1005 struct mlx5e_neigh_hash_entry **nhe)
1009 *nhe = kzalloc(sizeof(**nhe), GFP_KERNEL);
1013 memcpy(&(*nhe)->m_neigh, &e->m_neigh, sizeof(e->m_neigh));
1014 INIT_WORK(&(*nhe)->neigh_update_work, mlx5e_rep_neigh_update);
1015 INIT_LIST_HEAD(&(*nhe)->encap_list);
1016 refcount_set(&(*nhe)->refcnt, 1);
1018 err = mlx5e_rep_neigh_entry_insert(priv, *nhe);
1028 static void mlx5e_rep_neigh_entry_destroy(struct mlx5e_priv *priv,
1029 struct mlx5e_neigh_hash_entry *nhe)
1031 /* The neigh hash entry must be removed from the hash table regardless
1032 * of the reference count value, so it won't be found by the next
1033 * neigh notification call. The neigh hash entry reference count is
1034 * incremented only during creation and neigh notification calls and
1035 * protects from freeing the nhe struct.
1037 mlx5e_rep_neigh_entry_remove(priv, nhe);
1038 mlx5e_rep_neigh_entry_release(nhe);
1041 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
1042 struct mlx5e_encap_entry *e)
1044 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1045 struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
1046 struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
1047 struct mlx5e_neigh_hash_entry *nhe;
1050 err = mlx5_tun_entropy_refcount_inc(tun_entropy, e->reformat_type);
1053 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
1055 err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
1057 mlx5_tun_entropy_refcount_dec(tun_entropy,
1062 list_add(&e->encap_list, &nhe->encap_list);
1066 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
1067 struct mlx5e_encap_entry *e)
1069 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1070 struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
1071 struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
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);
1079 mlx5_tun_entropy_refcount_dec(tun_entropy, e->reformat_type);
1082 static int mlx5e_rep_open(struct net_device *dev)
1084 struct mlx5e_priv *priv = netdev_priv(dev);
1085 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1086 struct mlx5_eswitch_rep *rep = rpriv->rep;
1089 mutex_lock(&priv->state_lock);
1090 err = mlx5e_open_locked(dev);
1094 if (!mlx5_modify_vport_admin_state(priv->mdev,
1095 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1097 MLX5_VPORT_ADMIN_STATE_UP))
1098 netif_carrier_on(dev);
1101 mutex_unlock(&priv->state_lock);
1105 static int mlx5e_rep_close(struct net_device *dev)
1107 struct mlx5e_priv *priv = netdev_priv(dev);
1108 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1109 struct mlx5_eswitch_rep *rep = rpriv->rep;
1112 mutex_lock(&priv->state_lock);
1113 mlx5_modify_vport_admin_state(priv->mdev,
1114 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
1116 MLX5_VPORT_ADMIN_STATE_DOWN);
1117 ret = mlx5e_close_locked(dev);
1118 mutex_unlock(&priv->state_lock);
1122 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
1123 char *buf, size_t len)
1125 struct mlx5e_priv *priv = netdev_priv(dev);
1126 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1127 struct mlx5_eswitch_rep *rep = rpriv->rep;
1131 fn = PCI_FUNC(priv->mdev->pdev->devfn);
1132 if (fn >= MLX5_MAX_PORTS)
1135 if (rep->vport == MLX5_VPORT_UPLINK)
1136 ret = snprintf(buf, len, "p%d", fn);
1137 else if (rep->vport == MLX5_VPORT_PF)
1138 ret = snprintf(buf, len, "pf%d", fn);
1140 ret = snprintf(buf, len, "pf%dvf%d", fn, rep->vport - 1);
1149 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
1150 struct tc_cls_flower_offload *cls_flower, int flags)
1152 switch (cls_flower->command) {
1153 case TC_CLSFLOWER_REPLACE:
1154 return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
1156 case TC_CLSFLOWER_DESTROY:
1157 return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
1159 case TC_CLSFLOWER_STATS:
1160 return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
1167 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
1170 struct mlx5e_priv *priv = cb_priv;
1173 case TC_SETUP_CLSFLOWER:
1174 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS |
1175 MLX5E_TC_ESW_OFFLOAD);
1181 static int mlx5e_rep_setup_tc_block(struct net_device *dev,
1182 struct tc_block_offload *f)
1184 struct mlx5e_priv *priv = netdev_priv(dev);
1186 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1189 switch (f->command) {
1191 return tcf_block_cb_register(f->block, mlx5e_rep_setup_tc_cb,
1192 priv, priv, f->extack);
1193 case TC_BLOCK_UNBIND:
1194 tcf_block_cb_unregister(f->block, mlx5e_rep_setup_tc_cb, priv);
1201 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
1205 case TC_SETUP_BLOCK:
1206 return mlx5e_rep_setup_tc_block(dev, type_data);
1212 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
1214 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1215 struct mlx5_eswitch_rep *rep;
1217 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
1220 if (!rpriv) /* non vport rep mlx5e instances don't use this field */
1224 return (rep->vport == MLX5_VPORT_UPLINK);
1227 static bool mlx5e_rep_has_offload_stats(const struct net_device *dev, int attr_id)
1230 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1238 mlx5e_get_sw_stats64(const struct net_device *dev,
1239 struct rtnl_link_stats64 *stats)
1241 struct mlx5e_priv *priv = netdev_priv(dev);
1243 mlx5e_fold_sw_stats64(priv, stats);
1247 static int mlx5e_rep_get_offload_stats(int attr_id, const struct net_device *dev,
1251 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1252 return mlx5e_get_sw_stats64(dev, sp);
1259 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
1261 struct mlx5e_priv *priv = netdev_priv(dev);
1263 /* update HW stats in background for next time */
1264 mlx5e_queue_update_stats(priv);
1265 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
1268 static int mlx5e_rep_change_mtu(struct net_device *netdev, int new_mtu)
1270 return mlx5e_change_mtu(netdev, new_mtu, NULL);
1273 static int mlx5e_uplink_rep_change_mtu(struct net_device *netdev, int new_mtu)
1275 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu);
1278 static int mlx5e_uplink_rep_set_mac(struct net_device *netdev, void *addr)
1280 struct sockaddr *saddr = addr;
1282 if (!is_valid_ether_addr(saddr->sa_data))
1283 return -EADDRNOTAVAIL;
1285 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
1289 static int mlx5e_uplink_rep_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
1292 netdev_warn_once(dev, "legacy vf vlan setting isn't supported in switchdev mode\n");
1297 /* allow setting 0-vid for compatibility with libvirt */
1301 static const struct net_device_ops mlx5e_netdev_ops_rep = {
1302 .ndo_open = mlx5e_rep_open,
1303 .ndo_stop = mlx5e_rep_close,
1304 .ndo_start_xmit = mlx5e_xmit,
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_rep_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_rep_change_mtu,
1311 .ndo_get_port_parent_id = mlx5e_rep_get_port_parent_id,
1314 static const struct net_device_ops mlx5e_netdev_ops_uplink_rep = {
1315 .ndo_open = mlx5e_open,
1316 .ndo_stop = mlx5e_close,
1317 .ndo_start_xmit = mlx5e_xmit,
1318 .ndo_set_mac_address = mlx5e_uplink_rep_set_mac,
1319 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
1320 .ndo_setup_tc = mlx5e_rep_setup_tc,
1321 .ndo_get_stats64 = mlx5e_get_stats,
1322 .ndo_has_offload_stats = mlx5e_rep_has_offload_stats,
1323 .ndo_get_offload_stats = mlx5e_rep_get_offload_stats,
1324 .ndo_change_mtu = mlx5e_uplink_rep_change_mtu,
1325 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
1326 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
1327 .ndo_features_check = mlx5e_features_check,
1328 .ndo_set_vf_mac = mlx5e_set_vf_mac,
1329 .ndo_set_vf_rate = mlx5e_set_vf_rate,
1330 .ndo_get_vf_config = mlx5e_get_vf_config,
1331 .ndo_get_vf_stats = mlx5e_get_vf_stats,
1332 .ndo_set_vf_vlan = mlx5e_uplink_rep_set_vf_vlan,
1333 .ndo_get_port_parent_id = mlx5e_rep_get_port_parent_id,
1334 .ndo_set_features = mlx5e_set_features,
1337 bool mlx5e_eswitch_rep(struct net_device *netdev)
1339 if (netdev->netdev_ops == &mlx5e_netdev_ops_rep ||
1340 netdev->netdev_ops == &mlx5e_netdev_ops_uplink_rep)
1346 static void mlx5e_build_rep_params(struct net_device *netdev)
1348 struct mlx5e_priv *priv = netdev_priv(netdev);
1349 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1350 struct mlx5_eswitch_rep *rep = rpriv->rep;
1351 struct mlx5_core_dev *mdev = priv->mdev;
1352 struct mlx5e_params *params;
1354 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
1355 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
1356 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1358 params = &priv->channels.params;
1359 params->hard_mtu = MLX5E_ETH_HARD_MTU;
1360 params->sw_mtu = netdev->mtu;
1363 if (rep->vport == MLX5_VPORT_UPLINK)
1364 params->log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
1366 params->log_sq_size = MLX5E_REP_PARAMS_DEF_LOG_SQ_SIZE;
1369 mlx5e_build_rq_params(mdev, params);
1371 /* CQ moderation params */
1372 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
1373 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
1376 params->tunneled_offload_en = false;
1378 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
1381 mlx5e_build_rss_params(&priv->rss_params, params->num_channels);
1384 static void mlx5e_build_rep_netdev(struct net_device *netdev)
1386 struct mlx5e_priv *priv = netdev_priv(netdev);
1387 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1388 struct mlx5_eswitch_rep *rep = rpriv->rep;
1389 struct mlx5_core_dev *mdev = priv->mdev;
1391 if (rep->vport == MLX5_VPORT_UPLINK) {
1392 SET_NETDEV_DEV(netdev, mdev->device);
1393 netdev->netdev_ops = &mlx5e_netdev_ops_uplink_rep;
1394 /* we want a persistent mac for the uplink rep */
1395 mlx5_query_mac_address(mdev, netdev->dev_addr);
1396 netdev->ethtool_ops = &mlx5e_uplink_rep_ethtool_ops;
1397 #ifdef CONFIG_MLX5_CORE_EN_DCB
1398 if (MLX5_CAP_GEN(mdev, qos))
1399 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
1402 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
1403 eth_hw_addr_random(netdev);
1404 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
1407 netdev->watchdog_timeo = 15 * HZ;
1409 netdev->features |= NETIF_F_NETNS_LOCAL;
1411 netdev->hw_features |= NETIF_F_HW_TC;
1412 netdev->hw_features |= NETIF_F_SG;
1413 netdev->hw_features |= NETIF_F_IP_CSUM;
1414 netdev->hw_features |= NETIF_F_IPV6_CSUM;
1415 netdev->hw_features |= NETIF_F_GRO;
1416 netdev->hw_features |= NETIF_F_TSO;
1417 netdev->hw_features |= NETIF_F_TSO6;
1418 netdev->hw_features |= NETIF_F_RXCSUM;
1420 if (rep->vport == MLX5_VPORT_UPLINK)
1421 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
1423 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1425 netdev->features |= netdev->hw_features;
1428 static int mlx5e_init_rep(struct mlx5_core_dev *mdev,
1429 struct net_device *netdev,
1430 const struct mlx5e_profile *profile,
1433 struct mlx5e_priv *priv = netdev_priv(netdev);
1436 err = mlx5e_netdev_init(netdev, priv, mdev, profile, ppriv);
1440 priv->channels.params.num_channels = MLX5E_REP_PARAMS_DEF_NUM_CHANNELS;
1442 mlx5e_build_rep_params(netdev);
1443 mlx5e_build_rep_netdev(netdev);
1445 mlx5e_timestamp_init(priv);
1450 static void mlx5e_cleanup_rep(struct mlx5e_priv *priv)
1452 mlx5e_netdev_cleanup(priv->netdev, priv);
1455 static int mlx5e_create_rep_ttc_table(struct mlx5e_priv *priv)
1457 struct ttc_params ttc_params = {};
1460 priv->fs.ns = mlx5_get_flow_namespace(priv->mdev,
1461 MLX5_FLOW_NAMESPACE_KERNEL);
1463 /* The inner_ttc in the ttc params is intentionally not set */
1464 ttc_params.any_tt_tirn = priv->direct_tir[0].tirn;
1465 mlx5e_set_ttc_ft_params(&ttc_params);
1466 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
1467 ttc_params.indir_tirn[tt] = priv->indir_tir[tt].tirn;
1469 err = mlx5e_create_ttc_table(priv, &ttc_params, &priv->fs.ttc);
1471 netdev_err(priv->netdev, "Failed to create rep ttc table, err=%d\n", err);
1477 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
1479 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1480 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1481 struct mlx5_eswitch_rep *rep = rpriv->rep;
1482 struct mlx5_flow_handle *flow_rule;
1483 struct mlx5_flow_destination dest;
1485 dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1486 dest.tir_num = priv->direct_tir[0].tirn;
1487 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
1490 if (IS_ERR(flow_rule))
1491 return PTR_ERR(flow_rule);
1492 rpriv->vport_rx_rule = flow_rule;
1496 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
1498 struct mlx5_core_dev *mdev = priv->mdev;
1501 mlx5e_init_l2_addr(priv);
1503 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
1505 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
1509 err = mlx5e_create_indirect_rqt(priv);
1511 goto err_close_drop_rq;
1513 err = mlx5e_create_direct_rqts(priv);
1515 goto err_destroy_indirect_rqts;
1517 err = mlx5e_create_indirect_tirs(priv, false);
1519 goto err_destroy_direct_rqts;
1521 err = mlx5e_create_direct_tirs(priv);
1523 goto err_destroy_indirect_tirs;
1525 err = mlx5e_create_rep_ttc_table(priv);
1527 goto err_destroy_direct_tirs;
1529 err = mlx5e_create_rep_vport_rx_rule(priv);
1531 goto err_destroy_ttc_table;
1535 err_destroy_ttc_table:
1536 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1537 err_destroy_direct_tirs:
1538 mlx5e_destroy_direct_tirs(priv);
1539 err_destroy_indirect_tirs:
1540 mlx5e_destroy_indirect_tirs(priv, false);
1541 err_destroy_direct_rqts:
1542 mlx5e_destroy_direct_rqts(priv);
1543 err_destroy_indirect_rqts:
1544 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1546 mlx5e_close_drop_rq(&priv->drop_rq);
1550 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
1552 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1554 mlx5_del_flow_rules(rpriv->vport_rx_rule);
1555 mlx5e_destroy_ttc_table(priv, &priv->fs.ttc);
1556 mlx5e_destroy_direct_tirs(priv);
1557 mlx5e_destroy_indirect_tirs(priv, false);
1558 mlx5e_destroy_direct_rqts(priv);
1559 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
1560 mlx5e_close_drop_rq(&priv->drop_rq);
1563 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1565 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1566 struct mlx5_rep_uplink_priv *uplink_priv;
1569 err = mlx5e_create_tises(priv);
1571 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1575 if (rpriv->rep->vport == MLX5_VPORT_UPLINK) {
1576 uplink_priv = &rpriv->uplink_priv;
1578 INIT_LIST_HEAD(&uplink_priv->unready_flows);
1580 /* init shared tc flow table */
1581 err = mlx5e_tc_esw_init(&uplink_priv->tc_ht);
1585 mlx5_init_port_tun_entropy(&uplink_priv->tun_entropy, priv->mdev);
1587 /* init indirect block notifications */
1588 INIT_LIST_HEAD(&uplink_priv->tc_indr_block_priv_list);
1589 uplink_priv->netdevice_nb.notifier_call = mlx5e_nic_rep_netdevice_event;
1590 err = register_netdevice_notifier(&uplink_priv->netdevice_nb);
1592 mlx5_core_err(priv->mdev, "Failed to register netdev notifier\n");
1593 goto tc_esw_cleanup;
1600 mlx5e_tc_esw_cleanup(&uplink_priv->tc_ht);
1602 for (tc = 0; tc < priv->profile->max_tc; tc++)
1603 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
1607 static void mlx5e_cleanup_rep_tx(struct mlx5e_priv *priv)
1609 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1612 for (tc = 0; tc < priv->profile->max_tc; tc++)
1613 mlx5e_destroy_tis(priv->mdev, priv->tisn[tc]);
1615 if (rpriv->rep->vport == MLX5_VPORT_UPLINK) {
1616 /* clean indirect TC block notifications */
1617 unregister_netdevice_notifier(&rpriv->uplink_priv.netdevice_nb);
1618 mlx5e_rep_indr_clean_block_privs(rpriv);
1620 /* delete shared tc flow table */
1621 mlx5e_tc_esw_cleanup(&rpriv->uplink_priv.tc_ht);
1625 static void mlx5e_rep_enable(struct mlx5e_priv *priv)
1627 mlx5e_set_netdev_mtu_boundaries(priv);
1630 static int mlx5e_update_rep_rx(struct mlx5e_priv *priv)
1635 static int uplink_rep_async_event(struct notifier_block *nb, unsigned long event, void *data)
1637 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb);
1639 if (event == MLX5_EVENT_TYPE_PORT_CHANGE) {
1640 struct mlx5_eqe *eqe = data;
1642 switch (eqe->sub_type) {
1643 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
1644 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
1645 queue_work(priv->wq, &priv->update_carrier_work);
1654 if (event == MLX5_DEV_EVENT_PORT_AFFINITY) {
1655 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1657 queue_work(priv->wq, &rpriv->uplink_priv.reoffload_flows_work);
1665 static void mlx5e_uplink_rep_enable(struct mlx5e_priv *priv)
1667 struct net_device *netdev = priv->netdev;
1668 struct mlx5_core_dev *mdev = priv->mdev;
1669 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1672 netdev->min_mtu = ETH_MIN_MTU;
1673 mlx5_query_port_max_mtu(priv->mdev, &max_mtu, 1);
1674 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
1675 mlx5e_set_dev_port_mtu(priv);
1677 INIT_WORK(&rpriv->uplink_priv.reoffload_flows_work,
1678 mlx5e_tc_reoffload_flows_work);
1680 mlx5_lag_add(mdev, netdev);
1681 priv->events_nb.notifier_call = uplink_rep_async_event;
1682 mlx5_notifier_register(mdev, &priv->events_nb);
1683 #ifdef CONFIG_MLX5_CORE_EN_DCB
1684 mlx5e_dcbnl_initialize(priv);
1685 mlx5e_dcbnl_init_app(priv);
1689 static void mlx5e_uplink_rep_disable(struct mlx5e_priv *priv)
1691 struct mlx5_core_dev *mdev = priv->mdev;
1692 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1694 #ifdef CONFIG_MLX5_CORE_EN_DCB
1695 mlx5e_dcbnl_delete_app(priv);
1697 mlx5_notifier_unregister(mdev, &priv->events_nb);
1698 cancel_work_sync(&rpriv->uplink_priv.reoffload_flows_work);
1699 mlx5_lag_remove(mdev);
1702 static const struct mlx5e_profile mlx5e_rep_profile = {
1703 .init = mlx5e_init_rep,
1704 .cleanup = mlx5e_cleanup_rep,
1705 .init_rx = mlx5e_init_rep_rx,
1706 .cleanup_rx = mlx5e_cleanup_rep_rx,
1707 .init_tx = mlx5e_init_rep_tx,
1708 .cleanup_tx = mlx5e_cleanup_rep_tx,
1709 .enable = mlx5e_rep_enable,
1710 .update_rx = mlx5e_update_rep_rx,
1711 .update_stats = mlx5e_rep_update_hw_counters,
1712 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1713 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1717 static const struct mlx5e_profile mlx5e_uplink_rep_profile = {
1718 .init = mlx5e_init_rep,
1719 .cleanup = mlx5e_cleanup_rep,
1720 .init_rx = mlx5e_init_rep_rx,
1721 .cleanup_rx = mlx5e_cleanup_rep_rx,
1722 .init_tx = mlx5e_init_rep_tx,
1723 .cleanup_tx = mlx5e_cleanup_rep_tx,
1724 .enable = mlx5e_uplink_rep_enable,
1725 .disable = mlx5e_uplink_rep_disable,
1726 .update_rx = mlx5e_update_rep_rx,
1727 .update_stats = mlx5e_uplink_rep_update_hw_counters,
1728 .update_carrier = mlx5e_update_carrier,
1729 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1730 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1731 .max_tc = MLX5E_MAX_NUM_TC,
1734 /* e-Switch vport representors */
1736 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1738 const struct mlx5e_profile *profile;
1739 struct mlx5e_rep_priv *rpriv;
1740 struct net_device *netdev;
1743 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1747 /* rpriv->rep to be looked up when profile->init() is called */
1750 nch = mlx5e_get_max_num_channels(dev);
1751 profile = (rep->vport == MLX5_VPORT_UPLINK) ?
1752 &mlx5e_uplink_rep_profile : &mlx5e_rep_profile;
1753 netdev = mlx5e_create_netdev(dev, profile, nch, rpriv);
1755 pr_warn("Failed to create representor netdev for vport %d\n",
1761 rpriv->netdev = netdev;
1762 rep->rep_data[REP_ETH].priv = rpriv;
1763 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1765 if (rep->vport == MLX5_VPORT_UPLINK) {
1766 err = mlx5e_create_mdev_resources(dev);
1768 goto err_destroy_netdev;
1771 err = mlx5e_attach_netdev(netdev_priv(netdev));
1773 pr_warn("Failed to attach representor netdev for vport %d\n",
1775 goto err_destroy_mdev_resources;
1778 err = mlx5e_rep_neigh_init(rpriv);
1780 pr_warn("Failed to initialized neighbours handling for vport %d\n",
1782 goto err_detach_netdev;
1785 err = register_netdev(netdev);
1787 pr_warn("Failed to register representor netdev for vport %d\n",
1789 goto err_neigh_cleanup;
1795 mlx5e_rep_neigh_cleanup(rpriv);
1798 mlx5e_detach_netdev(netdev_priv(netdev));
1800 err_destroy_mdev_resources:
1801 if (rep->vport == MLX5_VPORT_UPLINK)
1802 mlx5e_destroy_mdev_resources(dev);
1805 mlx5e_destroy_netdev(netdev_priv(netdev));
1811 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1813 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1814 struct net_device *netdev = rpriv->netdev;
1815 struct mlx5e_priv *priv = netdev_priv(netdev);
1816 void *ppriv = priv->ppriv;
1818 unregister_netdev(netdev);
1819 mlx5e_rep_neigh_cleanup(rpriv);
1820 mlx5e_detach_netdev(priv);
1821 if (rep->vport == MLX5_VPORT_UPLINK)
1822 mlx5e_destroy_mdev_resources(priv->mdev);
1823 mlx5e_destroy_netdev(priv);
1824 kfree(ppriv); /* mlx5e_rep_priv */
1827 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1829 struct mlx5e_rep_priv *rpriv;
1831 rpriv = mlx5e_rep_to_rep_priv(rep);
1833 return rpriv->netdev;
1836 static const struct mlx5_eswitch_rep_ops rep_ops = {
1837 .load = mlx5e_vport_rep_load,
1838 .unload = mlx5e_vport_rep_unload,
1839 .get_proto_dev = mlx5e_vport_rep_get_proto_dev
1842 void mlx5e_rep_register_vport_reps(struct mlx5_core_dev *mdev)
1844 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1846 mlx5_eswitch_register_vport_reps(esw, &rep_ops, REP_ETH);
1849 void mlx5e_rep_unregister_vport_reps(struct mlx5_core_dev *mdev)
1851 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1853 mlx5_eswitch_unregister_vport_reps(esw, REP_ETH);