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>
47 #define MLX5E_REP_PARAMS_LOG_SQ_SIZE \
48 max(0x6, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE)
50 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
52 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
53 struct ethtool_drvinfo *drvinfo)
55 strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
56 sizeof(drvinfo->driver));
57 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
60 static const struct counter_desc sw_rep_stats_desc[] = {
61 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
62 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
63 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
64 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
74 static const struct counter_desc vport_rep_stats_desc[] = {
75 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_packets) },
76 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_bytes) },
77 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_packets) },
78 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_bytes) },
81 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
82 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
84 static void mlx5e_rep_get_strings(struct net_device *dev,
85 u32 stringset, uint8_t *data)
91 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
92 strcpy(data + (i * ETH_GSTRING_LEN),
93 sw_rep_stats_desc[i].format);
94 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
95 strcpy(data + (i * ETH_GSTRING_LEN),
96 vport_rep_stats_desc[j].format);
101 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv *priv)
103 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
104 struct mlx5e_rep_priv *rpriv = priv->ppriv;
105 struct mlx5_eswitch_rep *rep = rpriv->rep;
106 struct rtnl_link_stats64 *vport_stats;
107 struct ifla_vf_stats vf_stats;
110 err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
112 pr_warn("vport %d error %d reading stats\n", rep->vport, err);
116 vport_stats = &priv->stats.vf_vport;
117 /* flip tx/rx as we are reporting the counters for the switch vport */
118 vport_stats->rx_packets = vf_stats.tx_packets;
119 vport_stats->rx_bytes = vf_stats.tx_bytes;
120 vport_stats->tx_packets = vf_stats.rx_packets;
121 vport_stats->tx_bytes = vf_stats.rx_bytes;
124 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv *priv)
126 struct mlx5e_sw_stats *s = &priv->stats.sw;
127 struct mlx5e_rq_stats *rq_stats;
128 struct mlx5e_sq_stats *sq_stats;
131 memset(s, 0, sizeof(*s));
132 for (i = 0; i < priv->channels.num; i++) {
133 struct mlx5e_channel *c = priv->channels.c[i];
135 rq_stats = c->rq.stats;
137 s->rx_packets += rq_stats->packets;
138 s->rx_bytes += rq_stats->bytes;
140 for (j = 0; j < priv->channels.params.num_tc; j++) {
141 sq_stats = c->sq[j].stats;
143 s->tx_packets += sq_stats->packets;
144 s->tx_bytes += sq_stats->bytes;
149 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
150 struct ethtool_stats *stats, u64 *data)
152 struct mlx5e_priv *priv = netdev_priv(dev);
158 mutex_lock(&priv->state_lock);
159 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
160 mlx5e_rep_update_sw_counters(priv);
161 mlx5e_rep_update_hw_counters(priv);
162 mutex_unlock(&priv->state_lock);
164 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
165 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
166 sw_rep_stats_desc, i);
168 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
169 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
170 vport_rep_stats_desc, j);
173 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
177 return NUM_VPORT_REP_SW_COUNTERS + NUM_VPORT_REP_HW_COUNTERS;
183 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
184 .get_drvinfo = mlx5e_rep_get_drvinfo,
185 .get_link = ethtool_op_get_link,
186 .get_strings = mlx5e_rep_get_strings,
187 .get_sset_count = mlx5e_rep_get_sset_count,
188 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
191 int mlx5e_attr_get(struct net_device *dev, struct switchdev_attr *attr)
193 struct mlx5e_priv *priv = netdev_priv(dev);
194 struct mlx5e_rep_priv *rpriv = priv->ppriv;
195 struct mlx5_eswitch_rep *rep = rpriv->rep;
196 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
198 if (esw->mode == SRIOV_NONE)
202 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
203 attr->u.ppid.id_len = ETH_ALEN;
204 ether_addr_copy(attr->u.ppid.id, rep->hw_id);
213 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
214 struct mlx5_eswitch_rep *rep)
216 struct mlx5e_rep_sq *rep_sq, *tmp;
217 struct mlx5e_rep_priv *rpriv;
219 if (esw->mode != SRIOV_OFFLOADS)
222 rpriv = mlx5e_rep_to_rep_priv(rep);
223 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
224 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
225 list_del(&rep_sq->list);
230 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
231 struct mlx5_eswitch_rep *rep,
232 u32 *sqns_array, int sqns_num)
234 struct mlx5_flow_handle *flow_rule;
235 struct mlx5e_rep_priv *rpriv;
236 struct mlx5e_rep_sq *rep_sq;
240 if (esw->mode != SRIOV_OFFLOADS)
243 rpriv = mlx5e_rep_to_rep_priv(rep);
244 for (i = 0; i < sqns_num; i++) {
245 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
251 /* Add re-inject rule to the PF/representor sqs */
252 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
255 if (IS_ERR(flow_rule)) {
256 err = PTR_ERR(flow_rule);
260 rep_sq->send_to_vport_rule = flow_rule;
261 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
266 mlx5e_sqs2vport_stop(esw, rep);
270 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
272 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
273 struct mlx5e_rep_priv *rpriv = priv->ppriv;
274 struct mlx5_eswitch_rep *rep = rpriv->rep;
275 struct mlx5e_channel *c;
276 int n, tc, num_sqs = 0;
280 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(*sqs), GFP_KERNEL);
284 for (n = 0; n < priv->channels.num; n++) {
285 c = priv->channels.c[n];
286 for (tc = 0; tc < c->num_tc; tc++)
287 sqs[num_sqs++] = c->sq[tc].sqn;
290 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
295 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
299 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
301 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
302 struct mlx5e_rep_priv *rpriv = priv->ppriv;
303 struct mlx5_eswitch_rep *rep = rpriv->rep;
305 mlx5e_sqs2vport_stop(esw, rep);
308 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv *rpriv)
310 #if IS_ENABLED(CONFIG_IPV6)
311 unsigned long ipv6_interval = NEIGH_VAR(&nd_tbl.parms,
314 unsigned long ipv6_interval = ~0UL;
316 unsigned long ipv4_interval = NEIGH_VAR(&arp_tbl.parms,
318 struct net_device *netdev = rpriv->netdev;
319 struct mlx5e_priv *priv = netdev_priv(netdev);
321 rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);
322 mlx5_fc_update_sampling_interval(priv->mdev, rpriv->neigh_update.min_interval);
325 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv)
327 struct mlx5e_rep_priv *rpriv = priv->ppriv;
328 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
330 mlx5_fc_queue_stats_work(priv->mdev,
331 &neigh_update->neigh_stats_work,
332 neigh_update->min_interval);
335 static void mlx5e_rep_neigh_stats_work(struct work_struct *work)
337 struct mlx5e_rep_priv *rpriv = container_of(work, struct mlx5e_rep_priv,
338 neigh_update.neigh_stats_work.work);
339 struct net_device *netdev = rpriv->netdev;
340 struct mlx5e_priv *priv = netdev_priv(netdev);
341 struct mlx5e_neigh_hash_entry *nhe;
344 if (!list_empty(&rpriv->neigh_update.neigh_list))
345 mlx5e_rep_queue_neigh_stats_work(priv);
347 list_for_each_entry(nhe, &rpriv->neigh_update.neigh_list, neigh_list)
348 mlx5e_tc_update_neigh_used_value(nhe);
353 static void mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry *nhe)
355 refcount_inc(&nhe->refcnt);
358 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry *nhe)
360 if (refcount_dec_and_test(&nhe->refcnt))
364 static void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
365 struct mlx5e_encap_entry *e,
366 bool neigh_connected,
367 unsigned char ha[ETH_ALEN])
369 struct ethhdr *eth = (struct ethhdr *)e->encap_header;
373 if ((!neigh_connected && (e->flags & MLX5_ENCAP_ENTRY_VALID)) ||
374 !ether_addr_equal(e->h_dest, ha))
375 mlx5e_tc_encap_flows_del(priv, e);
377 if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
378 ether_addr_copy(e->h_dest, ha);
379 ether_addr_copy(eth->h_dest, ha);
381 mlx5e_tc_encap_flows_add(priv, e);
385 static void mlx5e_rep_neigh_update(struct work_struct *work)
387 struct mlx5e_neigh_hash_entry *nhe =
388 container_of(work, struct mlx5e_neigh_hash_entry, neigh_update_work);
389 struct neighbour *n = nhe->n;
390 struct mlx5e_encap_entry *e;
391 unsigned char ha[ETH_ALEN];
392 struct mlx5e_priv *priv;
393 bool neigh_connected;
394 bool encap_connected;
399 /* If these parameters are changed after we release the lock,
400 * we'll receive another event letting us know about it.
401 * We use this lock to avoid inconsistency between the neigh validity
402 * and it's hw address.
404 read_lock_bh(&n->lock);
405 memcpy(ha, n->ha, ETH_ALEN);
406 nud_state = n->nud_state;
408 read_unlock_bh(&n->lock);
410 neigh_connected = (nud_state & NUD_VALID) && !dead;
412 list_for_each_entry(e, &nhe->encap_list, encap_list) {
413 encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
414 priv = netdev_priv(e->out_dev);
416 if (encap_connected != neigh_connected ||
417 !ether_addr_equal(e->h_dest, ha))
418 mlx5e_rep_update_flows(priv, e, neigh_connected, ha);
420 mlx5e_rep_neigh_entry_release(nhe);
425 static struct mlx5e_neigh_hash_entry *
426 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
427 struct mlx5e_neigh *m_neigh);
429 static int mlx5e_rep_netevent_event(struct notifier_block *nb,
430 unsigned long event, void *ptr)
432 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
433 neigh_update.netevent_nb);
434 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
435 struct net_device *netdev = rpriv->netdev;
436 struct mlx5e_priv *priv = netdev_priv(netdev);
437 struct mlx5e_neigh_hash_entry *nhe = NULL;
438 struct mlx5e_neigh m_neigh = {};
439 struct neigh_parms *p;
444 case NETEVENT_NEIGH_UPDATE:
446 #if IS_ENABLED(CONFIG_IPV6)
447 if (n->tbl != &nd_tbl && n->tbl != &arp_tbl)
449 if (n->tbl != &arp_tbl)
453 m_neigh.dev = n->dev;
454 m_neigh.family = n->ops->family;
455 memcpy(&m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
457 /* We are in atomic context and can't take RTNL mutex, so use
458 * spin_lock_bh to lookup the neigh table. bh is used since
459 * netevent can be called from a softirq context.
461 spin_lock_bh(&neigh_update->encap_lock);
462 nhe = mlx5e_rep_neigh_entry_lookup(priv, &m_neigh);
464 spin_unlock_bh(&neigh_update->encap_lock);
468 /* This assignment is valid as long as the the neigh reference
473 /* Take a reference to ensure the neighbour and mlx5 encap
474 * entry won't be destructed until we drop the reference in
478 mlx5e_rep_neigh_entry_hold(nhe);
480 if (!queue_work(priv->wq, &nhe->neigh_update_work)) {
481 mlx5e_rep_neigh_entry_release(nhe);
484 spin_unlock_bh(&neigh_update->encap_lock);
487 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
490 /* We check the device is present since we don't care about
491 * changes in the default table, we only care about changes
492 * done per device delay prob time parameter.
494 #if IS_ENABLED(CONFIG_IPV6)
495 if (!p->dev || (p->tbl != &nd_tbl && p->tbl != &arp_tbl))
497 if (!p->dev || p->tbl != &arp_tbl)
501 /* We are in atomic context and can't take RTNL mutex,
502 * so use spin_lock_bh to walk the neigh list and look for
503 * the relevant device. bh is used since netevent can be
504 * called from a softirq context.
506 spin_lock_bh(&neigh_update->encap_lock);
507 list_for_each_entry(nhe, &neigh_update->neigh_list, neigh_list) {
508 if (p->dev == nhe->m_neigh.dev) {
513 spin_unlock_bh(&neigh_update->encap_lock);
517 neigh_update->min_interval = min_t(unsigned long,
518 NEIGH_VAR(p, DELAY_PROBE_TIME),
519 neigh_update->min_interval);
520 mlx5_fc_update_sampling_interval(priv->mdev,
521 neigh_update->min_interval);
527 static const struct rhashtable_params mlx5e_neigh_ht_params = {
528 .head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
529 .key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
530 .key_len = sizeof(struct mlx5e_neigh),
531 .automatic_shrinking = true,
534 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv *rpriv)
536 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
539 err = rhashtable_init(&neigh_update->neigh_ht, &mlx5e_neigh_ht_params);
543 INIT_LIST_HEAD(&neigh_update->neigh_list);
544 spin_lock_init(&neigh_update->encap_lock);
545 INIT_DELAYED_WORK(&neigh_update->neigh_stats_work,
546 mlx5e_rep_neigh_stats_work);
547 mlx5e_rep_neigh_update_init_interval(rpriv);
549 rpriv->neigh_update.netevent_nb.notifier_call = mlx5e_rep_netevent_event;
550 err = register_netevent_notifier(&rpriv->neigh_update.netevent_nb);
556 rhashtable_destroy(&neigh_update->neigh_ht);
560 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv *rpriv)
562 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
563 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
565 unregister_netevent_notifier(&neigh_update->netevent_nb);
567 flush_workqueue(priv->wq); /* flush neigh update works */
569 cancel_delayed_work_sync(&rpriv->neigh_update.neigh_stats_work);
571 rhashtable_destroy(&neigh_update->neigh_ht);
574 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv *priv,
575 struct mlx5e_neigh_hash_entry *nhe)
577 struct mlx5e_rep_priv *rpriv = priv->ppriv;
580 err = rhashtable_insert_fast(&rpriv->neigh_update.neigh_ht,
582 mlx5e_neigh_ht_params);
586 list_add(&nhe->neigh_list, &rpriv->neigh_update.neigh_list);
591 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_priv *priv,
592 struct mlx5e_neigh_hash_entry *nhe)
594 struct mlx5e_rep_priv *rpriv = priv->ppriv;
596 spin_lock_bh(&rpriv->neigh_update.encap_lock);
598 list_del(&nhe->neigh_list);
600 rhashtable_remove_fast(&rpriv->neigh_update.neigh_ht,
602 mlx5e_neigh_ht_params);
603 spin_unlock_bh(&rpriv->neigh_update.encap_lock);
606 /* This function must only be called under RTNL lock or under the
607 * representor's encap_lock in case RTNL mutex can't be held.
609 static struct mlx5e_neigh_hash_entry *
610 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
611 struct mlx5e_neigh *m_neigh)
613 struct mlx5e_rep_priv *rpriv = priv->ppriv;
614 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
616 return rhashtable_lookup_fast(&neigh_update->neigh_ht, m_neigh,
617 mlx5e_neigh_ht_params);
620 static int mlx5e_rep_neigh_entry_create(struct mlx5e_priv *priv,
621 struct mlx5e_encap_entry *e,
622 struct mlx5e_neigh_hash_entry **nhe)
626 *nhe = kzalloc(sizeof(**nhe), GFP_KERNEL);
630 memcpy(&(*nhe)->m_neigh, &e->m_neigh, sizeof(e->m_neigh));
631 INIT_WORK(&(*nhe)->neigh_update_work, mlx5e_rep_neigh_update);
632 INIT_LIST_HEAD(&(*nhe)->encap_list);
633 refcount_set(&(*nhe)->refcnt, 1);
635 err = mlx5e_rep_neigh_entry_insert(priv, *nhe);
645 static void mlx5e_rep_neigh_entry_destroy(struct mlx5e_priv *priv,
646 struct mlx5e_neigh_hash_entry *nhe)
648 /* The neigh hash entry must be removed from the hash table regardless
649 * of the reference count value, so it won't be found by the next
650 * neigh notification call. The neigh hash entry reference count is
651 * incremented only during creation and neigh notification calls and
652 * protects from freeing the nhe struct.
654 mlx5e_rep_neigh_entry_remove(priv, nhe);
655 mlx5e_rep_neigh_entry_release(nhe);
658 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
659 struct mlx5e_encap_entry *e)
661 struct mlx5e_neigh_hash_entry *nhe;
664 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
666 err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
670 list_add(&e->encap_list, &nhe->encap_list);
674 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
675 struct mlx5e_encap_entry *e)
677 struct mlx5e_neigh_hash_entry *nhe;
679 list_del(&e->encap_list);
680 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
682 if (list_empty(&nhe->encap_list))
683 mlx5e_rep_neigh_entry_destroy(priv, nhe);
686 static int mlx5e_rep_open(struct net_device *dev)
688 struct mlx5e_priv *priv = netdev_priv(dev);
689 struct mlx5e_rep_priv *rpriv = priv->ppriv;
690 struct mlx5_eswitch_rep *rep = rpriv->rep;
693 mutex_lock(&priv->state_lock);
694 err = mlx5e_open_locked(dev);
698 if (!mlx5_modify_vport_admin_state(priv->mdev,
699 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
700 rep->vport, MLX5_VPORT_ADMIN_STATE_UP))
701 netif_carrier_on(dev);
704 mutex_unlock(&priv->state_lock);
708 static int mlx5e_rep_close(struct net_device *dev)
710 struct mlx5e_priv *priv = netdev_priv(dev);
711 struct mlx5e_rep_priv *rpriv = priv->ppriv;
712 struct mlx5_eswitch_rep *rep = rpriv->rep;
715 mutex_lock(&priv->state_lock);
716 mlx5_modify_vport_admin_state(priv->mdev,
717 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
718 rep->vport, MLX5_VPORT_ADMIN_STATE_DOWN);
719 ret = mlx5e_close_locked(dev);
720 mutex_unlock(&priv->state_lock);
724 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
725 char *buf, size_t len)
727 struct mlx5e_priv *priv = netdev_priv(dev);
728 struct mlx5e_rep_priv *rpriv = priv->ppriv;
729 struct mlx5_eswitch_rep *rep = rpriv->rep;
732 ret = snprintf(buf, len, "%d", rep->vport - 1);
740 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
741 struct tc_cls_flower_offload *cls_flower, int flags)
743 switch (cls_flower->command) {
744 case TC_CLSFLOWER_REPLACE:
745 return mlx5e_configure_flower(priv, cls_flower, flags);
746 case TC_CLSFLOWER_DESTROY:
747 return mlx5e_delete_flower(priv, cls_flower, flags);
748 case TC_CLSFLOWER_STATS:
749 return mlx5e_stats_flower(priv, cls_flower, flags);
755 static int mlx5e_rep_setup_tc_cb_egdev(enum tc_setup_type type, void *type_data,
758 struct mlx5e_priv *priv = cb_priv;
760 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
764 case TC_SETUP_CLSFLOWER:
765 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, MLX5E_TC_EGRESS);
771 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
774 struct mlx5e_priv *priv = cb_priv;
776 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
780 case TC_SETUP_CLSFLOWER:
781 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS);
787 static int mlx5e_rep_setup_tc_block(struct net_device *dev,
788 struct tc_block_offload *f)
790 struct mlx5e_priv *priv = netdev_priv(dev);
792 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
795 switch (f->command) {
797 return tcf_block_cb_register(f->block, mlx5e_rep_setup_tc_cb,
798 priv, priv, f->extack);
799 case TC_BLOCK_UNBIND:
800 tcf_block_cb_unregister(f->block, mlx5e_rep_setup_tc_cb, priv);
807 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
812 return mlx5e_rep_setup_tc_block(dev, type_data);
818 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
820 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
821 struct mlx5e_rep_priv *rpriv = priv->ppriv;
822 struct mlx5_eswitch_rep *rep;
824 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
828 if (esw->mode == SRIOV_OFFLOADS &&
829 rep && rep->vport == FDB_UPLINK_VPORT)
835 static bool mlx5e_is_vf_vport_rep(struct mlx5e_priv *priv)
837 struct mlx5e_rep_priv *rpriv = priv->ppriv;
838 struct mlx5_eswitch_rep *rep;
840 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
844 if (rep && rep->vport != FDB_UPLINK_VPORT)
850 bool mlx5e_has_offload_stats(const struct net_device *dev, int attr_id)
852 struct mlx5e_priv *priv = netdev_priv(dev);
855 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
856 if (mlx5e_is_vf_vport_rep(priv) || mlx5e_is_uplink_rep(priv))
864 mlx5e_get_sw_stats64(const struct net_device *dev,
865 struct rtnl_link_stats64 *stats)
867 struct mlx5e_priv *priv = netdev_priv(dev);
868 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
870 mlx5e_rep_update_sw_counters(priv);
872 stats->rx_packets = sstats->rx_packets;
873 stats->rx_bytes = sstats->rx_bytes;
874 stats->tx_packets = sstats->tx_packets;
875 stats->tx_bytes = sstats->tx_bytes;
877 stats->tx_dropped = sstats->tx_queue_dropped;
882 int mlx5e_get_offload_stats(int attr_id, const struct net_device *dev,
886 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
887 return mlx5e_get_sw_stats64(dev, sp);
894 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
896 struct mlx5e_priv *priv = netdev_priv(dev);
898 /* update HW stats in background for next time */
899 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
901 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
904 static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
905 .switchdev_port_attr_get = mlx5e_attr_get,
908 static int mlx5e_change_rep_mtu(struct net_device *netdev, int new_mtu)
910 return mlx5e_change_mtu(netdev, new_mtu, NULL);
913 static const struct net_device_ops mlx5e_netdev_ops_rep = {
914 .ndo_open = mlx5e_rep_open,
915 .ndo_stop = mlx5e_rep_close,
916 .ndo_start_xmit = mlx5e_xmit,
917 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
918 .ndo_setup_tc = mlx5e_rep_setup_tc,
919 .ndo_get_stats64 = mlx5e_rep_get_stats,
920 .ndo_has_offload_stats = mlx5e_has_offload_stats,
921 .ndo_get_offload_stats = mlx5e_get_offload_stats,
922 .ndo_change_mtu = mlx5e_change_rep_mtu,
925 static void mlx5e_build_rep_params(struct mlx5_core_dev *mdev,
926 struct mlx5e_params *params, u16 mtu)
928 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
929 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
930 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
932 params->hard_mtu = MLX5E_ETH_HARD_MTU;
933 params->sw_mtu = mtu;
934 params->log_sq_size = MLX5E_REP_PARAMS_LOG_SQ_SIZE;
937 mlx5e_build_rq_params(mdev, params);
939 /* CQ moderation params */
940 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
941 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
945 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
948 static void mlx5e_build_rep_netdev(struct net_device *netdev)
950 struct mlx5e_priv *priv = netdev_priv(netdev);
951 struct mlx5_core_dev *mdev = priv->mdev;
954 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
956 netdev->watchdog_timeo = 15 * HZ;
958 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
960 netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
962 netdev->features |= NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
963 netdev->hw_features |= NETIF_F_HW_TC;
965 netdev->hw_features |= NETIF_F_SG;
966 netdev->hw_features |= NETIF_F_IP_CSUM;
967 netdev->hw_features |= NETIF_F_IPV6_CSUM;
968 netdev->hw_features |= NETIF_F_GRO;
969 netdev->hw_features |= NETIF_F_TSO;
970 netdev->hw_features |= NETIF_F_TSO6;
971 netdev->hw_features |= NETIF_F_RXCSUM;
973 netdev->features |= netdev->hw_features;
975 eth_hw_addr_random(netdev);
977 netdev->min_mtu = ETH_MIN_MTU;
978 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
979 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
982 static void mlx5e_init_rep(struct mlx5_core_dev *mdev,
983 struct net_device *netdev,
984 const struct mlx5e_profile *profile,
987 struct mlx5e_priv *priv = netdev_priv(netdev);
990 priv->netdev = netdev;
991 priv->profile = profile;
994 mutex_init(&priv->state_lock);
996 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
998 priv->channels.params.num_channels = profile->max_nch(mdev);
1000 mlx5e_build_rep_params(mdev, &priv->channels.params, netdev->mtu);
1001 mlx5e_build_rep_netdev(netdev);
1003 mlx5e_timestamp_init(priv);
1006 static int mlx5e_create_rep_vport_rx_rule(struct mlx5e_priv *priv)
1008 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1009 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1010 struct mlx5_eswitch_rep *rep = rpriv->rep;
1011 struct mlx5_flow_handle *flow_rule;
1012 struct mlx5_flow_destination dest;
1014 dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1015 dest.tir_num = priv->direct_tir[0].tirn;
1016 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
1019 if (IS_ERR(flow_rule))
1020 return PTR_ERR(flow_rule);
1021 rpriv->vport_rx_rule = flow_rule;
1025 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
1027 struct mlx5_core_dev *mdev = priv->mdev;
1030 mlx5e_init_l2_addr(priv);
1032 err = mlx5e_open_drop_rq(priv, &priv->drop_rq);
1034 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
1038 err = mlx5e_create_direct_rqts(priv);
1040 goto err_close_drop_rq;
1042 err = mlx5e_create_direct_tirs(priv);
1044 goto err_destroy_direct_rqts;
1046 err = mlx5e_create_rep_vport_rx_rule(priv);
1048 goto err_destroy_direct_tirs;
1052 err_destroy_direct_tirs:
1053 mlx5e_destroy_direct_tirs(priv);
1054 err_destroy_direct_rqts:
1055 mlx5e_destroy_direct_rqts(priv);
1057 mlx5e_close_drop_rq(&priv->drop_rq);
1061 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
1063 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1065 mlx5_del_flow_rules(rpriv->vport_rx_rule);
1066 mlx5e_destroy_direct_tirs(priv);
1067 mlx5e_destroy_direct_rqts(priv);
1068 mlx5e_close_drop_rq(&priv->drop_rq);
1071 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1075 err = mlx5e_create_tises(priv);
1077 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1083 static int mlx5e_get_rep_max_num_channels(struct mlx5_core_dev *mdev)
1085 #define MLX5E_PORT_REPRESENTOR_NCH 1
1086 return MLX5E_PORT_REPRESENTOR_NCH;
1089 static const struct mlx5e_profile mlx5e_rep_profile = {
1090 .init = mlx5e_init_rep,
1091 .init_rx = mlx5e_init_rep_rx,
1092 .cleanup_rx = mlx5e_cleanup_rep_rx,
1093 .init_tx = mlx5e_init_rep_tx,
1094 .cleanup_tx = mlx5e_cleanup_nic_tx,
1095 .update_stats = mlx5e_rep_update_hw_counters,
1096 .max_nch = mlx5e_get_rep_max_num_channels,
1097 .update_carrier = NULL,
1098 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1099 .rx_handlers.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
1103 /* e-Switch vport representors */
1106 mlx5e_nic_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1108 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1109 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1113 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
1114 err = mlx5e_add_sqs_fwd_rules(priv);
1119 err = mlx5e_rep_neigh_init(rpriv);
1121 goto err_remove_sqs;
1123 /* init shared tc flow table */
1124 err = mlx5e_tc_esw_init(&rpriv->tc_ht);
1126 goto err_neigh_cleanup;
1131 mlx5e_rep_neigh_cleanup(rpriv);
1133 mlx5e_remove_sqs_fwd_rules(priv);
1138 mlx5e_nic_rep_unload(struct mlx5_eswitch_rep *rep)
1140 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1141 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1143 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1144 mlx5e_remove_sqs_fwd_rules(priv);
1146 /* clean uplink offloaded TC rules, delete shared tc flow table */
1147 mlx5e_tc_esw_cleanup(&rpriv->tc_ht);
1149 mlx5e_rep_neigh_cleanup(rpriv);
1153 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1155 struct mlx5e_rep_priv *uplink_rpriv;
1156 struct mlx5e_rep_priv *rpriv;
1157 struct net_device *netdev;
1158 struct mlx5e_priv *upriv;
1161 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1165 netdev = mlx5e_create_netdev(dev, &mlx5e_rep_profile, rpriv);
1167 pr_warn("Failed to create representor netdev for vport %d\n",
1173 rpriv->netdev = netdev;
1175 rep->rep_if[REP_ETH].priv = rpriv;
1176 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1178 err = mlx5e_attach_netdev(netdev_priv(netdev));
1180 pr_warn("Failed to attach representor netdev for vport %d\n",
1182 goto err_destroy_netdev;
1185 err = mlx5e_rep_neigh_init(rpriv);
1187 pr_warn("Failed to initialized neighbours handling for vport %d\n",
1189 goto err_detach_netdev;
1192 uplink_rpriv = mlx5_eswitch_get_uplink_priv(dev->priv.eswitch, REP_ETH);
1193 upriv = netdev_priv(uplink_rpriv->netdev);
1194 err = tc_setup_cb_egdev_register(netdev, mlx5e_rep_setup_tc_cb_egdev,
1197 goto err_neigh_cleanup;
1199 err = register_netdev(netdev);
1201 pr_warn("Failed to register representor netdev for vport %d\n",
1203 goto err_egdev_cleanup;
1209 tc_setup_cb_egdev_unregister(netdev, mlx5e_rep_setup_tc_cb_egdev,
1213 mlx5e_rep_neigh_cleanup(rpriv);
1216 mlx5e_detach_netdev(netdev_priv(netdev));
1219 mlx5e_destroy_netdev(netdev_priv(netdev));
1225 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1227 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1228 struct net_device *netdev = rpriv->netdev;
1229 struct mlx5e_priv *priv = netdev_priv(netdev);
1230 struct mlx5e_rep_priv *uplink_rpriv;
1231 void *ppriv = priv->ppriv;
1232 struct mlx5e_priv *upriv;
1234 unregister_netdev(netdev);
1235 uplink_rpriv = mlx5_eswitch_get_uplink_priv(priv->mdev->priv.eswitch,
1237 upriv = netdev_priv(uplink_rpriv->netdev);
1238 tc_setup_cb_egdev_unregister(netdev, mlx5e_rep_setup_tc_cb_egdev,
1240 mlx5e_rep_neigh_cleanup(rpriv);
1241 mlx5e_detach_netdev(priv);
1242 mlx5e_destroy_netdev(priv);
1243 kfree(ppriv); /* mlx5e_rep_priv */
1246 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1248 struct mlx5e_rep_priv *rpriv;
1250 rpriv = mlx5e_rep_to_rep_priv(rep);
1252 return rpriv->netdev;
1255 static void mlx5e_rep_register_vf_vports(struct mlx5e_priv *priv)
1257 struct mlx5_core_dev *mdev = priv->mdev;
1258 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1259 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1262 for (vport = 1; vport < total_vfs; vport++) {
1263 struct mlx5_eswitch_rep_if rep_if = {};
1265 rep_if.load = mlx5e_vport_rep_load;
1266 rep_if.unload = mlx5e_vport_rep_unload;
1267 rep_if.get_proto_dev = mlx5e_vport_rep_get_proto_dev;
1268 mlx5_eswitch_register_vport_rep(esw, vport, &rep_if, REP_ETH);
1272 static void mlx5e_rep_unregister_vf_vports(struct mlx5e_priv *priv)
1274 struct mlx5_core_dev *mdev = priv->mdev;
1275 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1276 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1279 for (vport = 1; vport < total_vfs; vport++)
1280 mlx5_eswitch_unregister_vport_rep(esw, vport, REP_ETH);
1283 void mlx5e_register_vport_reps(struct mlx5e_priv *priv)
1285 struct mlx5_core_dev *mdev = priv->mdev;
1286 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1287 struct mlx5_eswitch_rep_if rep_if;
1288 struct mlx5e_rep_priv *rpriv;
1290 rpriv = priv->ppriv;
1291 rpriv->netdev = priv->netdev;
1293 rep_if.load = mlx5e_nic_rep_load;
1294 rep_if.unload = mlx5e_nic_rep_unload;
1295 rep_if.get_proto_dev = mlx5e_vport_rep_get_proto_dev;
1296 rep_if.priv = rpriv;
1297 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1298 mlx5_eswitch_register_vport_rep(esw, 0, &rep_if, REP_ETH); /* UPLINK PF vport*/
1300 mlx5e_rep_register_vf_vports(priv); /* VFs vports */
1303 void mlx5e_unregister_vport_reps(struct mlx5e_priv *priv)
1305 struct mlx5_core_dev *mdev = priv->mdev;
1306 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1308 mlx5e_rep_unregister_vf_vports(priv); /* VFs vports */
1309 mlx5_eswitch_unregister_vport_rep(esw, 0, REP_ETH); /* UPLINK PF*/
1312 void *mlx5e_alloc_nic_rep_priv(struct mlx5_core_dev *mdev)
1314 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1315 struct mlx5e_rep_priv *rpriv;
1317 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1321 rpriv->rep = &esw->offloads.vport_reps[0];