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)
49 #define MLX5E_REP_PARAMS_LOG_RQ_SIZE \
50 max(0x6, MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE)
52 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
54 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
55 struct ethtool_drvinfo *drvinfo)
57 strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
58 sizeof(drvinfo->driver));
59 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
62 static const struct counter_desc sw_rep_stats_desc[] = {
63 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
64 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
65 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
66 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
76 static const struct counter_desc vport_rep_stats_desc[] = {
77 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_packets) },
78 { MLX5E_DECLARE_STAT(struct vport_stats, vport_rx_bytes) },
79 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_packets) },
80 { MLX5E_DECLARE_STAT(struct vport_stats, vport_tx_bytes) },
83 #define NUM_VPORT_REP_SW_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
84 #define NUM_VPORT_REP_HW_COUNTERS ARRAY_SIZE(vport_rep_stats_desc)
86 static void mlx5e_rep_get_strings(struct net_device *dev,
87 u32 stringset, uint8_t *data)
93 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
94 strcpy(data + (i * ETH_GSTRING_LEN),
95 sw_rep_stats_desc[i].format);
96 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
97 strcpy(data + (i * ETH_GSTRING_LEN),
98 vport_rep_stats_desc[j].format);
103 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv *priv)
105 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
106 struct mlx5e_rep_priv *rpriv = priv->ppriv;
107 struct mlx5_eswitch_rep *rep = rpriv->rep;
108 struct rtnl_link_stats64 *vport_stats;
109 struct ifla_vf_stats vf_stats;
112 err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
114 pr_warn("vport %d error %d reading stats\n", rep->vport, err);
118 vport_stats = &priv->stats.vf_vport;
119 /* flip tx/rx as we are reporting the counters for the switch vport */
120 vport_stats->rx_packets = vf_stats.tx_packets;
121 vport_stats->rx_bytes = vf_stats.tx_bytes;
122 vport_stats->tx_packets = vf_stats.rx_packets;
123 vport_stats->tx_bytes = vf_stats.rx_bytes;
126 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv *priv)
128 struct mlx5e_sw_stats *s = &priv->stats.sw;
129 struct mlx5e_rq_stats *rq_stats;
130 struct mlx5e_sq_stats *sq_stats;
133 memset(s, 0, sizeof(*s));
134 for (i = 0; i < priv->channels.num; i++) {
135 struct mlx5e_channel *c = priv->channels.c[i];
137 rq_stats = c->rq.stats;
139 s->rx_packets += rq_stats->packets;
140 s->rx_bytes += rq_stats->bytes;
142 for (j = 0; j < priv->channels.params.num_tc; j++) {
143 sq_stats = c->sq[j].stats;
145 s->tx_packets += sq_stats->packets;
146 s->tx_bytes += sq_stats->bytes;
147 s->tx_queue_dropped += sq_stats->dropped;
152 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
153 struct ethtool_stats *stats, u64 *data)
155 struct mlx5e_priv *priv = netdev_priv(dev);
161 mutex_lock(&priv->state_lock);
162 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
163 mlx5e_rep_update_sw_counters(priv);
164 mlx5e_rep_update_hw_counters(priv);
165 mutex_unlock(&priv->state_lock);
167 for (i = 0; i < NUM_VPORT_REP_SW_COUNTERS; i++)
168 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
169 sw_rep_stats_desc, i);
171 for (j = 0; j < NUM_VPORT_REP_HW_COUNTERS; j++, i++)
172 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.vf_vport,
173 vport_rep_stats_desc, j);
176 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
180 return NUM_VPORT_REP_SW_COUNTERS + NUM_VPORT_REP_HW_COUNTERS;
186 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
187 .get_drvinfo = mlx5e_rep_get_drvinfo,
188 .get_link = ethtool_op_get_link,
189 .get_strings = mlx5e_rep_get_strings,
190 .get_sset_count = mlx5e_rep_get_sset_count,
191 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
194 int mlx5e_attr_get(struct net_device *dev, struct switchdev_attr *attr)
196 struct mlx5e_priv *priv = netdev_priv(dev);
197 struct mlx5e_rep_priv *rpriv = priv->ppriv;
198 struct mlx5_eswitch_rep *rep = rpriv->rep;
199 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
201 if (esw->mode != SRIOV_OFFLOADS)
205 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
206 attr->u.ppid.id_len = ETH_ALEN;
207 ether_addr_copy(attr->u.ppid.id, rep->hw_id);
216 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
217 struct mlx5_eswitch_rep *rep)
219 struct mlx5e_rep_sq *rep_sq, *tmp;
220 struct mlx5e_rep_priv *rpriv;
222 if (esw->mode != SRIOV_OFFLOADS)
225 rpriv = mlx5e_rep_to_rep_priv(rep);
226 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
227 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
228 list_del(&rep_sq->list);
233 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
234 struct mlx5_eswitch_rep *rep,
235 u32 *sqns_array, int sqns_num)
237 struct mlx5_flow_handle *flow_rule;
238 struct mlx5e_rep_priv *rpriv;
239 struct mlx5e_rep_sq *rep_sq;
243 if (esw->mode != SRIOV_OFFLOADS)
246 rpriv = mlx5e_rep_to_rep_priv(rep);
247 for (i = 0; i < sqns_num; i++) {
248 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
254 /* Add re-inject rule to the PF/representor sqs */
255 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
258 if (IS_ERR(flow_rule)) {
259 err = PTR_ERR(flow_rule);
263 rep_sq->send_to_vport_rule = flow_rule;
264 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
269 mlx5e_sqs2vport_stop(esw, rep);
273 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
275 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
276 struct mlx5e_rep_priv *rpriv = priv->ppriv;
277 struct mlx5_eswitch_rep *rep = rpriv->rep;
278 struct mlx5e_channel *c;
279 int n, tc, num_sqs = 0;
283 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(*sqs), GFP_KERNEL);
287 for (n = 0; n < priv->channels.num; n++) {
288 c = priv->channels.c[n];
289 for (tc = 0; tc < c->num_tc; tc++)
290 sqs[num_sqs++] = c->sq[tc].sqn;
293 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
298 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
302 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
304 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
305 struct mlx5e_rep_priv *rpriv = priv->ppriv;
306 struct mlx5_eswitch_rep *rep = rpriv->rep;
308 mlx5e_sqs2vport_stop(esw, rep);
311 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv *rpriv)
313 #if IS_ENABLED(CONFIG_IPV6)
314 unsigned long ipv6_interval = NEIGH_VAR(&nd_tbl.parms,
317 unsigned long ipv6_interval = ~0UL;
319 unsigned long ipv4_interval = NEIGH_VAR(&arp_tbl.parms,
321 struct net_device *netdev = rpriv->netdev;
322 struct mlx5e_priv *priv = netdev_priv(netdev);
324 rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);
325 mlx5_fc_update_sampling_interval(priv->mdev, rpriv->neigh_update.min_interval);
328 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv)
330 struct mlx5e_rep_priv *rpriv = priv->ppriv;
331 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
333 mlx5_fc_queue_stats_work(priv->mdev,
334 &neigh_update->neigh_stats_work,
335 neigh_update->min_interval);
338 static void mlx5e_rep_neigh_stats_work(struct work_struct *work)
340 struct mlx5e_rep_priv *rpriv = container_of(work, struct mlx5e_rep_priv,
341 neigh_update.neigh_stats_work.work);
342 struct net_device *netdev = rpriv->netdev;
343 struct mlx5e_priv *priv = netdev_priv(netdev);
344 struct mlx5e_neigh_hash_entry *nhe;
347 if (!list_empty(&rpriv->neigh_update.neigh_list))
348 mlx5e_rep_queue_neigh_stats_work(priv);
350 list_for_each_entry(nhe, &rpriv->neigh_update.neigh_list, neigh_list)
351 mlx5e_tc_update_neigh_used_value(nhe);
356 static void mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry *nhe)
358 refcount_inc(&nhe->refcnt);
361 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry *nhe)
363 if (refcount_dec_and_test(&nhe->refcnt))
367 static void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
368 struct mlx5e_encap_entry *e,
369 bool neigh_connected,
370 unsigned char ha[ETH_ALEN])
372 struct ethhdr *eth = (struct ethhdr *)e->encap_header;
376 if ((!neigh_connected && (e->flags & MLX5_ENCAP_ENTRY_VALID)) ||
377 !ether_addr_equal(e->h_dest, ha))
378 mlx5e_tc_encap_flows_del(priv, e);
380 if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
381 ether_addr_copy(e->h_dest, ha);
382 ether_addr_copy(eth->h_dest, ha);
384 mlx5e_tc_encap_flows_add(priv, e);
388 static void mlx5e_rep_neigh_update(struct work_struct *work)
390 struct mlx5e_neigh_hash_entry *nhe =
391 container_of(work, struct mlx5e_neigh_hash_entry, neigh_update_work);
392 struct neighbour *n = nhe->n;
393 struct mlx5e_encap_entry *e;
394 unsigned char ha[ETH_ALEN];
395 struct mlx5e_priv *priv;
396 bool neigh_connected;
397 bool encap_connected;
402 /* If these parameters are changed after we release the lock,
403 * we'll receive another event letting us know about it.
404 * We use this lock to avoid inconsistency between the neigh validity
405 * and it's hw address.
407 read_lock_bh(&n->lock);
408 memcpy(ha, n->ha, ETH_ALEN);
409 nud_state = n->nud_state;
411 read_unlock_bh(&n->lock);
413 neigh_connected = (nud_state & NUD_VALID) && !dead;
415 list_for_each_entry(e, &nhe->encap_list, encap_list) {
416 encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
417 priv = netdev_priv(e->out_dev);
419 if (encap_connected != neigh_connected ||
420 !ether_addr_equal(e->h_dest, ha))
421 mlx5e_rep_update_flows(priv, e, neigh_connected, ha);
423 mlx5e_rep_neigh_entry_release(nhe);
428 static struct mlx5e_neigh_hash_entry *
429 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
430 struct mlx5e_neigh *m_neigh);
432 static int mlx5e_rep_netevent_event(struct notifier_block *nb,
433 unsigned long event, void *ptr)
435 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
436 neigh_update.netevent_nb);
437 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
438 struct net_device *netdev = rpriv->netdev;
439 struct mlx5e_priv *priv = netdev_priv(netdev);
440 struct mlx5e_neigh_hash_entry *nhe = NULL;
441 struct mlx5e_neigh m_neigh = {};
442 struct neigh_parms *p;
447 case NETEVENT_NEIGH_UPDATE:
449 #if IS_ENABLED(CONFIG_IPV6)
450 if (n->tbl != &nd_tbl && n->tbl != &arp_tbl)
452 if (n->tbl != &arp_tbl)
456 m_neigh.dev = n->dev;
457 m_neigh.family = n->ops->family;
458 memcpy(&m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
460 /* We are in atomic context and can't take RTNL mutex, so use
461 * spin_lock_bh to lookup the neigh table. bh is used since
462 * netevent can be called from a softirq context.
464 spin_lock_bh(&neigh_update->encap_lock);
465 nhe = mlx5e_rep_neigh_entry_lookup(priv, &m_neigh);
467 spin_unlock_bh(&neigh_update->encap_lock);
471 /* This assignment is valid as long as the the neigh reference
476 /* Take a reference to ensure the neighbour and mlx5 encap
477 * entry won't be destructed until we drop the reference in
481 mlx5e_rep_neigh_entry_hold(nhe);
483 if (!queue_work(priv->wq, &nhe->neigh_update_work)) {
484 mlx5e_rep_neigh_entry_release(nhe);
487 spin_unlock_bh(&neigh_update->encap_lock);
490 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
493 /* We check the device is present since we don't care about
494 * changes in the default table, we only care about changes
495 * done per device delay prob time parameter.
497 #if IS_ENABLED(CONFIG_IPV6)
498 if (!p->dev || (p->tbl != &nd_tbl && p->tbl != &arp_tbl))
500 if (!p->dev || p->tbl != &arp_tbl)
504 /* We are in atomic context and can't take RTNL mutex,
505 * so use spin_lock_bh to walk the neigh list and look for
506 * the relevant device. bh is used since netevent can be
507 * called from a softirq context.
509 spin_lock_bh(&neigh_update->encap_lock);
510 list_for_each_entry(nhe, &neigh_update->neigh_list, neigh_list) {
511 if (p->dev == nhe->m_neigh.dev) {
516 spin_unlock_bh(&neigh_update->encap_lock);
520 neigh_update->min_interval = min_t(unsigned long,
521 NEIGH_VAR(p, DELAY_PROBE_TIME),
522 neigh_update->min_interval);
523 mlx5_fc_update_sampling_interval(priv->mdev,
524 neigh_update->min_interval);
530 static const struct rhashtable_params mlx5e_neigh_ht_params = {
531 .head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
532 .key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
533 .key_len = sizeof(struct mlx5e_neigh),
534 .automatic_shrinking = true,
537 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv *rpriv)
539 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
542 err = rhashtable_init(&neigh_update->neigh_ht, &mlx5e_neigh_ht_params);
546 INIT_LIST_HEAD(&neigh_update->neigh_list);
547 spin_lock_init(&neigh_update->encap_lock);
548 INIT_DELAYED_WORK(&neigh_update->neigh_stats_work,
549 mlx5e_rep_neigh_stats_work);
550 mlx5e_rep_neigh_update_init_interval(rpriv);
552 rpriv->neigh_update.netevent_nb.notifier_call = mlx5e_rep_netevent_event;
553 err = register_netevent_notifier(&rpriv->neigh_update.netevent_nb);
559 rhashtable_destroy(&neigh_update->neigh_ht);
563 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv *rpriv)
565 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
566 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
568 unregister_netevent_notifier(&neigh_update->netevent_nb);
570 flush_workqueue(priv->wq); /* flush neigh update works */
572 cancel_delayed_work_sync(&rpriv->neigh_update.neigh_stats_work);
574 rhashtable_destroy(&neigh_update->neigh_ht);
577 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv *priv,
578 struct mlx5e_neigh_hash_entry *nhe)
580 struct mlx5e_rep_priv *rpriv = priv->ppriv;
583 err = rhashtable_insert_fast(&rpriv->neigh_update.neigh_ht,
585 mlx5e_neigh_ht_params);
589 list_add(&nhe->neigh_list, &rpriv->neigh_update.neigh_list);
594 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_priv *priv,
595 struct mlx5e_neigh_hash_entry *nhe)
597 struct mlx5e_rep_priv *rpriv = priv->ppriv;
599 spin_lock_bh(&rpriv->neigh_update.encap_lock);
601 list_del(&nhe->neigh_list);
603 rhashtable_remove_fast(&rpriv->neigh_update.neigh_ht,
605 mlx5e_neigh_ht_params);
606 spin_unlock_bh(&rpriv->neigh_update.encap_lock);
609 /* This function must only be called under RTNL lock or under the
610 * representor's encap_lock in case RTNL mutex can't be held.
612 static struct mlx5e_neigh_hash_entry *
613 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
614 struct mlx5e_neigh *m_neigh)
616 struct mlx5e_rep_priv *rpriv = priv->ppriv;
617 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
619 return rhashtable_lookup_fast(&neigh_update->neigh_ht, m_neigh,
620 mlx5e_neigh_ht_params);
623 static int mlx5e_rep_neigh_entry_create(struct mlx5e_priv *priv,
624 struct mlx5e_encap_entry *e,
625 struct mlx5e_neigh_hash_entry **nhe)
629 *nhe = kzalloc(sizeof(**nhe), GFP_KERNEL);
633 memcpy(&(*nhe)->m_neigh, &e->m_neigh, sizeof(e->m_neigh));
634 INIT_WORK(&(*nhe)->neigh_update_work, mlx5e_rep_neigh_update);
635 INIT_LIST_HEAD(&(*nhe)->encap_list);
636 refcount_set(&(*nhe)->refcnt, 1);
638 err = mlx5e_rep_neigh_entry_insert(priv, *nhe);
648 static void mlx5e_rep_neigh_entry_destroy(struct mlx5e_priv *priv,
649 struct mlx5e_neigh_hash_entry *nhe)
651 /* The neigh hash entry must be removed from the hash table regardless
652 * of the reference count value, so it won't be found by the next
653 * neigh notification call. The neigh hash entry reference count is
654 * incremented only during creation and neigh notification calls and
655 * protects from freeing the nhe struct.
657 mlx5e_rep_neigh_entry_remove(priv, nhe);
658 mlx5e_rep_neigh_entry_release(nhe);
661 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
662 struct mlx5e_encap_entry *e)
664 struct mlx5e_neigh_hash_entry *nhe;
667 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
669 err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
673 list_add(&e->encap_list, &nhe->encap_list);
677 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
678 struct mlx5e_encap_entry *e)
680 struct mlx5e_neigh_hash_entry *nhe;
682 list_del(&e->encap_list);
683 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
685 if (list_empty(&nhe->encap_list))
686 mlx5e_rep_neigh_entry_destroy(priv, nhe);
689 static int mlx5e_rep_open(struct net_device *dev)
691 struct mlx5e_priv *priv = netdev_priv(dev);
692 struct mlx5e_rep_priv *rpriv = priv->ppriv;
693 struct mlx5_eswitch_rep *rep = rpriv->rep;
696 mutex_lock(&priv->state_lock);
697 err = mlx5e_open_locked(dev);
701 if (!mlx5_modify_vport_admin_state(priv->mdev,
702 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
703 rep->vport, MLX5_VPORT_ADMIN_STATE_UP))
704 netif_carrier_on(dev);
707 mutex_unlock(&priv->state_lock);
711 static int mlx5e_rep_close(struct net_device *dev)
713 struct mlx5e_priv *priv = netdev_priv(dev);
714 struct mlx5e_rep_priv *rpriv = priv->ppriv;
715 struct mlx5_eswitch_rep *rep = rpriv->rep;
718 mutex_lock(&priv->state_lock);
719 mlx5_modify_vport_admin_state(priv->mdev,
720 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
721 rep->vport, MLX5_VPORT_ADMIN_STATE_DOWN);
722 ret = mlx5e_close_locked(dev);
723 mutex_unlock(&priv->state_lock);
727 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
728 char *buf, size_t len)
730 struct mlx5e_priv *priv = netdev_priv(dev);
731 struct mlx5e_rep_priv *rpriv = priv->ppriv;
732 struct mlx5_eswitch_rep *rep = rpriv->rep;
735 ret = snprintf(buf, len, "%d", rep->vport - 1);
743 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
744 struct tc_cls_flower_offload *cls_flower, int flags)
746 switch (cls_flower->command) {
747 case TC_CLSFLOWER_REPLACE:
748 return mlx5e_configure_flower(priv, cls_flower, flags);
749 case TC_CLSFLOWER_DESTROY:
750 return mlx5e_delete_flower(priv, cls_flower, flags);
751 case TC_CLSFLOWER_STATS:
752 return mlx5e_stats_flower(priv, cls_flower, flags);
758 static int mlx5e_rep_setup_tc_cb_egdev(enum tc_setup_type type, void *type_data,
761 struct mlx5e_priv *priv = cb_priv;
763 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
767 case TC_SETUP_CLSFLOWER:
768 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, MLX5E_TC_EGRESS);
774 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
777 struct mlx5e_priv *priv = cb_priv;
779 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
783 case TC_SETUP_CLSFLOWER:
784 return mlx5e_rep_setup_tc_cls_flower(priv, type_data, MLX5E_TC_INGRESS);
790 static int mlx5e_rep_setup_tc_block(struct net_device *dev,
791 struct tc_block_offload *f)
793 struct mlx5e_priv *priv = netdev_priv(dev);
795 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
798 switch (f->command) {
800 return tcf_block_cb_register(f->block, mlx5e_rep_setup_tc_cb,
801 priv, priv, f->extack);
802 case TC_BLOCK_UNBIND:
803 tcf_block_cb_unregister(f->block, mlx5e_rep_setup_tc_cb, priv);
810 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
815 return mlx5e_rep_setup_tc_block(dev, type_data);
821 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
823 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
824 struct mlx5e_rep_priv *rpriv = priv->ppriv;
825 struct mlx5_eswitch_rep *rep;
827 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
831 if (esw->mode == SRIOV_OFFLOADS &&
832 rep && rep->vport == FDB_UPLINK_VPORT)
838 static bool mlx5e_is_vf_vport_rep(struct mlx5e_priv *priv)
840 struct mlx5e_rep_priv *rpriv = priv->ppriv;
841 struct mlx5_eswitch_rep *rep;
843 if (!MLX5_ESWITCH_MANAGER(priv->mdev))
847 if (rep && rep->vport != FDB_UPLINK_VPORT)
853 bool mlx5e_has_offload_stats(const struct net_device *dev, int attr_id)
855 struct mlx5e_priv *priv = netdev_priv(dev);
858 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
859 if (mlx5e_is_vf_vport_rep(priv) || mlx5e_is_uplink_rep(priv))
867 mlx5e_get_sw_stats64(const struct net_device *dev,
868 struct rtnl_link_stats64 *stats)
870 struct mlx5e_priv *priv = netdev_priv(dev);
871 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
873 mlx5e_rep_update_sw_counters(priv);
875 stats->rx_packets = sstats->rx_packets;
876 stats->rx_bytes = sstats->rx_bytes;
877 stats->tx_packets = sstats->tx_packets;
878 stats->tx_bytes = sstats->tx_bytes;
880 stats->tx_dropped = sstats->tx_queue_dropped;
885 int mlx5e_get_offload_stats(int attr_id, const struct net_device *dev,
889 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
890 return mlx5e_get_sw_stats64(dev, sp);
897 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
899 struct mlx5e_priv *priv = netdev_priv(dev);
901 /* update HW stats in background for next time */
902 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
904 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
907 static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
908 .switchdev_port_attr_get = mlx5e_attr_get,
911 static int mlx5e_change_rep_mtu(struct net_device *netdev, int new_mtu)
913 return mlx5e_change_mtu(netdev, new_mtu, NULL);
916 static const struct net_device_ops mlx5e_netdev_ops_rep = {
917 .ndo_open = mlx5e_rep_open,
918 .ndo_stop = mlx5e_rep_close,
919 .ndo_start_xmit = mlx5e_xmit,
920 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
921 .ndo_setup_tc = mlx5e_rep_setup_tc,
922 .ndo_get_stats64 = mlx5e_rep_get_stats,
923 .ndo_has_offload_stats = mlx5e_has_offload_stats,
924 .ndo_get_offload_stats = mlx5e_get_offload_stats,
925 .ndo_change_mtu = mlx5e_change_rep_mtu,
928 static void mlx5e_build_rep_params(struct mlx5_core_dev *mdev,
929 struct mlx5e_params *params, u16 mtu)
931 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
932 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
933 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
935 params->hard_mtu = MLX5E_ETH_HARD_MTU;
936 params->sw_mtu = mtu;
937 params->log_sq_size = MLX5E_REP_PARAMS_LOG_SQ_SIZE;
938 params->rq_wq_type = MLX5_WQ_TYPE_CYCLIC;
939 params->log_rq_mtu_frames = MLX5E_REP_PARAMS_LOG_RQ_SIZE;
941 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
942 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
945 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
947 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
950 static void mlx5e_build_rep_netdev(struct net_device *netdev)
952 struct mlx5e_priv *priv = netdev_priv(netdev);
953 struct mlx5_core_dev *mdev = priv->mdev;
956 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
958 netdev->watchdog_timeo = 15 * HZ;
960 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
962 netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
964 netdev->features |= NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
965 netdev->hw_features |= NETIF_F_HW_TC;
967 eth_hw_addr_random(netdev);
969 netdev->min_mtu = ETH_MIN_MTU;
970 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
971 netdev->max_mtu = MLX5E_HW2SW_MTU(&priv->channels.params, max_mtu);
974 static void mlx5e_init_rep(struct mlx5_core_dev *mdev,
975 struct net_device *netdev,
976 const struct mlx5e_profile *profile,
979 struct mlx5e_priv *priv = netdev_priv(netdev);
982 priv->netdev = netdev;
983 priv->profile = profile;
986 mutex_init(&priv->state_lock);
988 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
990 priv->channels.params.num_channels = profile->max_nch(mdev);
992 mlx5e_build_rep_params(mdev, &priv->channels.params, netdev->mtu);
993 mlx5e_build_rep_netdev(netdev);
995 mlx5e_timestamp_init(priv);
998 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
1000 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1001 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1002 struct mlx5_eswitch_rep *rep = rpriv->rep;
1003 struct mlx5_flow_handle *flow_rule;
1006 mlx5e_init_l2_addr(priv);
1008 err = mlx5e_create_direct_rqts(priv);
1012 err = mlx5e_create_direct_tirs(priv);
1014 goto err_destroy_direct_rqts;
1016 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
1018 priv->direct_tir[0].tirn);
1019 if (IS_ERR(flow_rule)) {
1020 err = PTR_ERR(flow_rule);
1021 goto err_destroy_direct_tirs;
1023 rpriv->vport_rx_rule = flow_rule;
1027 err_destroy_direct_tirs:
1028 mlx5e_destroy_direct_tirs(priv);
1029 err_destroy_direct_rqts:
1030 mlx5e_destroy_direct_rqts(priv);
1034 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
1036 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1038 mlx5_del_flow_rules(rpriv->vport_rx_rule);
1039 mlx5e_destroy_direct_tirs(priv);
1040 mlx5e_destroy_direct_rqts(priv);
1043 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
1047 err = mlx5e_create_tises(priv);
1049 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1055 static int mlx5e_get_rep_max_num_channels(struct mlx5_core_dev *mdev)
1057 #define MLX5E_PORT_REPRESENTOR_NCH 1
1058 return MLX5E_PORT_REPRESENTOR_NCH;
1061 static const struct mlx5e_profile mlx5e_rep_profile = {
1062 .init = mlx5e_init_rep,
1063 .init_rx = mlx5e_init_rep_rx,
1064 .cleanup_rx = mlx5e_cleanup_rep_rx,
1065 .init_tx = mlx5e_init_rep_tx,
1066 .cleanup_tx = mlx5e_cleanup_nic_tx,
1067 .update_stats = mlx5e_rep_update_hw_counters,
1068 .max_nch = mlx5e_get_rep_max_num_channels,
1069 .update_carrier = NULL,
1070 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1071 .rx_handlers.handle_rx_cqe_mpwqe = NULL /* Not supported */,
1075 /* e-Switch vport representors */
1078 mlx5e_nic_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1080 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1081 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1085 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
1086 err = mlx5e_add_sqs_fwd_rules(priv);
1091 err = mlx5e_rep_neigh_init(rpriv);
1093 goto err_remove_sqs;
1095 /* init shared tc flow table */
1096 err = mlx5e_tc_esw_init(&rpriv->tc_ht);
1098 goto err_neigh_cleanup;
1103 mlx5e_rep_neigh_cleanup(rpriv);
1105 mlx5e_remove_sqs_fwd_rules(priv);
1110 mlx5e_nic_rep_unload(struct mlx5_eswitch_rep *rep)
1112 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1113 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1115 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1116 mlx5e_remove_sqs_fwd_rules(priv);
1118 /* clean uplink offloaded TC rules, delete shared tc flow table */
1119 mlx5e_tc_esw_cleanup(&rpriv->tc_ht);
1121 mlx5e_rep_neigh_cleanup(rpriv);
1125 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1127 struct mlx5e_rep_priv *uplink_rpriv;
1128 struct mlx5e_rep_priv *rpriv;
1129 struct net_device *netdev;
1130 struct mlx5e_priv *upriv;
1133 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1137 netdev = mlx5e_create_netdev(dev, &mlx5e_rep_profile, rpriv);
1139 pr_warn("Failed to create representor netdev for vport %d\n",
1145 rpriv->netdev = netdev;
1147 rep->rep_if[REP_ETH].priv = rpriv;
1148 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1150 err = mlx5e_attach_netdev(netdev_priv(netdev));
1152 pr_warn("Failed to attach representor netdev for vport %d\n",
1154 goto err_destroy_netdev;
1157 err = mlx5e_rep_neigh_init(rpriv);
1159 pr_warn("Failed to initialized neighbours handling for vport %d\n",
1161 goto err_detach_netdev;
1164 uplink_rpriv = mlx5_eswitch_get_uplink_priv(dev->priv.eswitch, REP_ETH);
1165 upriv = netdev_priv(uplink_rpriv->netdev);
1166 err = tc_setup_cb_egdev_register(netdev, mlx5e_rep_setup_tc_cb_egdev,
1169 goto err_neigh_cleanup;
1171 err = register_netdev(netdev);
1173 pr_warn("Failed to register representor netdev for vport %d\n",
1175 goto err_egdev_cleanup;
1181 tc_setup_cb_egdev_unregister(netdev, mlx5e_rep_setup_tc_cb_egdev,
1185 mlx5e_rep_neigh_cleanup(rpriv);
1188 mlx5e_detach_netdev(netdev_priv(netdev));
1191 mlx5e_destroy_netdev(netdev_priv(netdev));
1197 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1199 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1200 struct net_device *netdev = rpriv->netdev;
1201 struct mlx5e_priv *priv = netdev_priv(netdev);
1202 struct mlx5e_rep_priv *uplink_rpriv;
1203 void *ppriv = priv->ppriv;
1204 struct mlx5e_priv *upriv;
1206 unregister_netdev(netdev);
1207 uplink_rpriv = mlx5_eswitch_get_uplink_priv(priv->mdev->priv.eswitch,
1209 upriv = netdev_priv(uplink_rpriv->netdev);
1210 tc_setup_cb_egdev_unregister(netdev, mlx5e_rep_setup_tc_cb_egdev,
1212 mlx5e_rep_neigh_cleanup(rpriv);
1213 mlx5e_detach_netdev(priv);
1214 mlx5e_destroy_netdev(priv);
1215 kfree(ppriv); /* mlx5e_rep_priv */
1218 static void *mlx5e_vport_rep_get_proto_dev(struct mlx5_eswitch_rep *rep)
1220 struct mlx5e_rep_priv *rpriv;
1222 rpriv = mlx5e_rep_to_rep_priv(rep);
1224 return rpriv->netdev;
1227 static void mlx5e_rep_register_vf_vports(struct mlx5e_priv *priv)
1229 struct mlx5_core_dev *mdev = priv->mdev;
1230 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1231 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1234 for (vport = 1; vport < total_vfs; vport++) {
1235 struct mlx5_eswitch_rep_if rep_if = {};
1237 rep_if.load = mlx5e_vport_rep_load;
1238 rep_if.unload = mlx5e_vport_rep_unload;
1239 rep_if.get_proto_dev = mlx5e_vport_rep_get_proto_dev;
1240 mlx5_eswitch_register_vport_rep(esw, vport, &rep_if, REP_ETH);
1244 static void mlx5e_rep_unregister_vf_vports(struct mlx5e_priv *priv)
1246 struct mlx5_core_dev *mdev = priv->mdev;
1247 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1248 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1251 for (vport = 1; vport < total_vfs; vport++)
1252 mlx5_eswitch_unregister_vport_rep(esw, vport, REP_ETH);
1255 void mlx5e_register_vport_reps(struct mlx5e_priv *priv)
1257 struct mlx5_core_dev *mdev = priv->mdev;
1258 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1259 struct mlx5_eswitch_rep_if rep_if;
1260 struct mlx5e_rep_priv *rpriv;
1262 rpriv = priv->ppriv;
1263 rpriv->netdev = priv->netdev;
1265 rep_if.load = mlx5e_nic_rep_load;
1266 rep_if.unload = mlx5e_nic_rep_unload;
1267 rep_if.get_proto_dev = mlx5e_vport_rep_get_proto_dev;
1268 rep_if.priv = rpriv;
1269 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1270 mlx5_eswitch_register_vport_rep(esw, 0, &rep_if, REP_ETH); /* UPLINK PF vport*/
1272 mlx5e_rep_register_vf_vports(priv); /* VFs vports */
1275 void mlx5e_unregister_vport_reps(struct mlx5e_priv *priv)
1277 struct mlx5_core_dev *mdev = priv->mdev;
1278 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1280 mlx5e_rep_unregister_vf_vports(priv); /* VFs vports */
1281 mlx5_eswitch_unregister_vport_rep(esw, 0, REP_ETH); /* UPLINK PF*/
1284 void *mlx5e_alloc_nic_rep_priv(struct mlx5_core_dev *mdev)
1286 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1287 struct mlx5e_rep_priv *rpriv;
1289 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1293 rpriv->rep = &esw->offloads.vport_reps[0];