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 static const char mlx5e_rep_driver_name[] = "mlx5e_rep";
49 static void mlx5e_rep_get_drvinfo(struct net_device *dev,
50 struct ethtool_drvinfo *drvinfo)
52 strlcpy(drvinfo->driver, mlx5e_rep_driver_name,
53 sizeof(drvinfo->driver));
54 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
57 static const struct counter_desc sw_rep_stats_desc[] = {
58 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_packets) },
59 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, rx_bytes) },
60 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_packets) },
61 { MLX5E_DECLARE_STAT(struct mlx5e_sw_stats, tx_bytes) },
64 #define NUM_VPORT_REP_COUNTERS ARRAY_SIZE(sw_rep_stats_desc)
66 static void mlx5e_rep_get_strings(struct net_device *dev,
67 u32 stringset, uint8_t *data)
73 for (i = 0; i < NUM_VPORT_REP_COUNTERS; i++)
74 strcpy(data + (i * ETH_GSTRING_LEN),
75 sw_rep_stats_desc[i].format);
80 static void mlx5e_rep_update_hw_counters(struct mlx5e_priv *priv)
82 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
83 struct mlx5e_rep_priv *rpriv = priv->ppriv;
84 struct mlx5_eswitch_rep *rep = rpriv->rep;
85 struct rtnl_link_stats64 *vport_stats;
86 struct ifla_vf_stats vf_stats;
89 err = mlx5_eswitch_get_vport_stats(esw, rep->vport, &vf_stats);
91 pr_warn("vport %d error %d reading stats\n", rep->vport, err);
95 vport_stats = &priv->stats.vf_vport;
96 /* flip tx/rx as we are reporting the counters for the switch vport */
97 vport_stats->rx_packets = vf_stats.tx_packets;
98 vport_stats->rx_bytes = vf_stats.tx_bytes;
99 vport_stats->tx_packets = vf_stats.rx_packets;
100 vport_stats->tx_bytes = vf_stats.rx_bytes;
103 static void mlx5e_rep_update_sw_counters(struct mlx5e_priv *priv)
105 struct mlx5e_sw_stats *s = &priv->stats.sw;
106 struct mlx5e_rq_stats *rq_stats;
107 struct mlx5e_sq_stats *sq_stats;
110 memset(s, 0, sizeof(*s));
111 for (i = 0; i < priv->channels.num; i++) {
112 struct mlx5e_channel *c = priv->channels.c[i];
114 rq_stats = &c->rq.stats;
116 s->rx_packets += rq_stats->packets;
117 s->rx_bytes += rq_stats->bytes;
119 for (j = 0; j < priv->channels.params.num_tc; j++) {
120 sq_stats = &c->sq[j].stats;
122 s->tx_packets += sq_stats->packets;
123 s->tx_bytes += sq_stats->bytes;
128 static void mlx5e_rep_update_stats(struct mlx5e_priv *priv)
130 mlx5e_rep_update_sw_counters(priv);
131 mlx5e_rep_update_hw_counters(priv);
134 static void mlx5e_rep_get_ethtool_stats(struct net_device *dev,
135 struct ethtool_stats *stats, u64 *data)
137 struct mlx5e_priv *priv = netdev_priv(dev);
143 mutex_lock(&priv->state_lock);
144 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
145 mlx5e_rep_update_sw_counters(priv);
146 mutex_unlock(&priv->state_lock);
148 for (i = 0; i < NUM_VPORT_REP_COUNTERS; i++)
149 data[i] = MLX5E_READ_CTR64_CPU(&priv->stats.sw,
150 sw_rep_stats_desc, i);
153 static int mlx5e_rep_get_sset_count(struct net_device *dev, int sset)
157 return NUM_VPORT_REP_COUNTERS;
163 static const struct ethtool_ops mlx5e_rep_ethtool_ops = {
164 .get_drvinfo = mlx5e_rep_get_drvinfo,
165 .get_link = ethtool_op_get_link,
166 .get_strings = mlx5e_rep_get_strings,
167 .get_sset_count = mlx5e_rep_get_sset_count,
168 .get_ethtool_stats = mlx5e_rep_get_ethtool_stats,
171 int mlx5e_attr_get(struct net_device *dev, struct switchdev_attr *attr)
173 struct mlx5e_priv *priv = netdev_priv(dev);
174 struct mlx5e_rep_priv *rpriv = priv->ppriv;
175 struct mlx5_eswitch_rep *rep = rpriv->rep;
176 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
178 if (esw->mode == SRIOV_NONE)
182 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
183 attr->u.ppid.id_len = ETH_ALEN;
184 ether_addr_copy(attr->u.ppid.id, rep->hw_id);
193 static void mlx5e_sqs2vport_stop(struct mlx5_eswitch *esw,
194 struct mlx5_eswitch_rep *rep)
196 struct mlx5e_rep_sq *rep_sq, *tmp;
197 struct mlx5e_rep_priv *rpriv;
199 if (esw->mode != SRIOV_OFFLOADS)
202 rpriv = mlx5e_rep_to_rep_priv(rep);
203 list_for_each_entry_safe(rep_sq, tmp, &rpriv->vport_sqs_list, list) {
204 mlx5_eswitch_del_send_to_vport_rule(rep_sq->send_to_vport_rule);
205 list_del(&rep_sq->list);
210 static int mlx5e_sqs2vport_start(struct mlx5_eswitch *esw,
211 struct mlx5_eswitch_rep *rep,
212 u16 *sqns_array, int sqns_num)
214 struct mlx5_flow_handle *flow_rule;
215 struct mlx5e_rep_priv *rpriv;
216 struct mlx5e_rep_sq *rep_sq;
220 if (esw->mode != SRIOV_OFFLOADS)
223 rpriv = mlx5e_rep_to_rep_priv(rep);
224 for (i = 0; i < sqns_num; i++) {
225 rep_sq = kzalloc(sizeof(*rep_sq), GFP_KERNEL);
231 /* Add re-inject rule to the PF/representor sqs */
232 flow_rule = mlx5_eswitch_add_send_to_vport_rule(esw,
235 if (IS_ERR(flow_rule)) {
236 err = PTR_ERR(flow_rule);
240 rep_sq->send_to_vport_rule = flow_rule;
241 list_add(&rep_sq->list, &rpriv->vport_sqs_list);
246 mlx5e_sqs2vport_stop(esw, rep);
250 int mlx5e_add_sqs_fwd_rules(struct mlx5e_priv *priv)
252 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
253 struct mlx5e_rep_priv *rpriv = priv->ppriv;
254 struct mlx5_eswitch_rep *rep = rpriv->rep;
255 struct mlx5e_channel *c;
256 int n, tc, num_sqs = 0;
260 sqs = kcalloc(priv->channels.num * priv->channels.params.num_tc, sizeof(u16), GFP_KERNEL);
264 for (n = 0; n < priv->channels.num; n++) {
265 c = priv->channels.c[n];
266 for (tc = 0; tc < c->num_tc; tc++)
267 sqs[num_sqs++] = c->sq[tc].sqn;
270 err = mlx5e_sqs2vport_start(esw, rep, sqs, num_sqs);
275 netdev_warn(priv->netdev, "Failed to add SQs FWD rules %d\n", err);
279 void mlx5e_remove_sqs_fwd_rules(struct mlx5e_priv *priv)
281 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
282 struct mlx5e_rep_priv *rpriv = priv->ppriv;
283 struct mlx5_eswitch_rep *rep = rpriv->rep;
285 mlx5e_sqs2vport_stop(esw, rep);
288 static void mlx5e_rep_neigh_update_init_interval(struct mlx5e_rep_priv *rpriv)
290 #if IS_ENABLED(CONFIG_IPV6)
291 unsigned long ipv6_interval = NEIGH_VAR(&ipv6_stub->nd_tbl->parms,
294 unsigned long ipv6_interval = ~0UL;
296 unsigned long ipv4_interval = NEIGH_VAR(&arp_tbl.parms,
298 struct net_device *netdev = rpriv->netdev;
299 struct mlx5e_priv *priv = netdev_priv(netdev);
301 rpriv->neigh_update.min_interval = min_t(unsigned long, ipv6_interval, ipv4_interval);
302 mlx5_fc_update_sampling_interval(priv->mdev, rpriv->neigh_update.min_interval);
305 void mlx5e_rep_queue_neigh_stats_work(struct mlx5e_priv *priv)
307 struct mlx5e_rep_priv *rpriv = priv->ppriv;
308 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
310 mlx5_fc_queue_stats_work(priv->mdev,
311 &neigh_update->neigh_stats_work,
312 neigh_update->min_interval);
315 static void mlx5e_rep_neigh_stats_work(struct work_struct *work)
317 struct mlx5e_rep_priv *rpriv = container_of(work, struct mlx5e_rep_priv,
318 neigh_update.neigh_stats_work.work);
319 struct net_device *netdev = rpriv->netdev;
320 struct mlx5e_priv *priv = netdev_priv(netdev);
321 struct mlx5e_neigh_hash_entry *nhe;
324 if (!list_empty(&rpriv->neigh_update.neigh_list))
325 mlx5e_rep_queue_neigh_stats_work(priv);
327 list_for_each_entry(nhe, &rpriv->neigh_update.neigh_list, neigh_list)
328 mlx5e_tc_update_neigh_used_value(nhe);
333 static void mlx5e_rep_neigh_entry_hold(struct mlx5e_neigh_hash_entry *nhe)
335 refcount_inc(&nhe->refcnt);
338 static void mlx5e_rep_neigh_entry_release(struct mlx5e_neigh_hash_entry *nhe)
340 if (refcount_dec_and_test(&nhe->refcnt))
344 static void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
345 struct mlx5e_encap_entry *e,
346 bool neigh_connected,
347 unsigned char ha[ETH_ALEN])
349 struct ethhdr *eth = (struct ethhdr *)e->encap_header;
353 if ((!neigh_connected && (e->flags & MLX5_ENCAP_ENTRY_VALID)) ||
354 !ether_addr_equal(e->h_dest, ha))
355 mlx5e_tc_encap_flows_del(priv, e);
357 if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
358 ether_addr_copy(e->h_dest, ha);
359 ether_addr_copy(eth->h_dest, ha);
361 mlx5e_tc_encap_flows_add(priv, e);
365 static void mlx5e_rep_neigh_update(struct work_struct *work)
367 struct mlx5e_neigh_hash_entry *nhe =
368 container_of(work, struct mlx5e_neigh_hash_entry, neigh_update_work);
369 struct neighbour *n = nhe->n;
370 struct mlx5e_encap_entry *e;
371 unsigned char ha[ETH_ALEN];
372 struct mlx5e_priv *priv;
373 bool neigh_connected;
374 bool encap_connected;
379 /* If these parameters are changed after we release the lock,
380 * we'll receive another event letting us know about it.
381 * We use this lock to avoid inconsistency between the neigh validity
382 * and it's hw address.
384 read_lock_bh(&n->lock);
385 memcpy(ha, n->ha, ETH_ALEN);
386 nud_state = n->nud_state;
388 read_unlock_bh(&n->lock);
390 neigh_connected = (nud_state & NUD_VALID) && !dead;
392 list_for_each_entry(e, &nhe->encap_list, encap_list) {
393 encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
394 priv = netdev_priv(e->out_dev);
396 if (encap_connected != neigh_connected ||
397 !ether_addr_equal(e->h_dest, ha))
398 mlx5e_rep_update_flows(priv, e, neigh_connected, ha);
400 mlx5e_rep_neigh_entry_release(nhe);
405 static struct mlx5e_neigh_hash_entry *
406 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
407 struct mlx5e_neigh *m_neigh);
409 static int mlx5e_rep_netevent_event(struct notifier_block *nb,
410 unsigned long event, void *ptr)
412 struct mlx5e_rep_priv *rpriv = container_of(nb, struct mlx5e_rep_priv,
413 neigh_update.netevent_nb);
414 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
415 struct net_device *netdev = rpriv->netdev;
416 struct mlx5e_priv *priv = netdev_priv(netdev);
417 struct mlx5e_neigh_hash_entry *nhe = NULL;
418 struct mlx5e_neigh m_neigh = {};
419 struct neigh_parms *p;
424 case NETEVENT_NEIGH_UPDATE:
426 #if IS_ENABLED(CONFIG_IPV6)
427 if (n->tbl != ipv6_stub->nd_tbl && n->tbl != &arp_tbl)
429 if (n->tbl != &arp_tbl)
433 m_neigh.dev = n->dev;
434 m_neigh.family = n->ops->family;
435 memcpy(&m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
437 /* We are in atomic context and can't take RTNL mutex, so use
438 * spin_lock_bh to lookup the neigh table. bh is used since
439 * netevent can be called from a softirq context.
441 spin_lock_bh(&neigh_update->encap_lock);
442 nhe = mlx5e_rep_neigh_entry_lookup(priv, &m_neigh);
444 spin_unlock_bh(&neigh_update->encap_lock);
448 /* This assignment is valid as long as the the neigh reference
453 /* Take a reference to ensure the neighbour and mlx5 encap
454 * entry won't be destructed until we drop the reference in
458 mlx5e_rep_neigh_entry_hold(nhe);
460 if (!queue_work(priv->wq, &nhe->neigh_update_work)) {
461 mlx5e_rep_neigh_entry_release(nhe);
464 spin_unlock_bh(&neigh_update->encap_lock);
467 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
470 /* We check the device is present since we don't care about
471 * changes in the default table, we only care about changes
472 * done per device delay prob time parameter.
474 #if IS_ENABLED(CONFIG_IPV6)
475 if (!p->dev || (p->tbl != ipv6_stub->nd_tbl && p->tbl != &arp_tbl))
477 if (!p->dev || p->tbl != &arp_tbl)
481 /* We are in atomic context and can't take RTNL mutex,
482 * so use spin_lock_bh to walk the neigh list and look for
483 * the relevant device. bh is used since netevent can be
484 * called from a softirq context.
486 spin_lock_bh(&neigh_update->encap_lock);
487 list_for_each_entry(nhe, &neigh_update->neigh_list, neigh_list) {
488 if (p->dev == nhe->m_neigh.dev) {
493 spin_unlock_bh(&neigh_update->encap_lock);
497 neigh_update->min_interval = min_t(unsigned long,
498 NEIGH_VAR(p, DELAY_PROBE_TIME),
499 neigh_update->min_interval);
500 mlx5_fc_update_sampling_interval(priv->mdev,
501 neigh_update->min_interval);
507 static const struct rhashtable_params mlx5e_neigh_ht_params = {
508 .head_offset = offsetof(struct mlx5e_neigh_hash_entry, rhash_node),
509 .key_offset = offsetof(struct mlx5e_neigh_hash_entry, m_neigh),
510 .key_len = sizeof(struct mlx5e_neigh),
511 .automatic_shrinking = true,
514 static int mlx5e_rep_neigh_init(struct mlx5e_rep_priv *rpriv)
516 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
519 err = rhashtable_init(&neigh_update->neigh_ht, &mlx5e_neigh_ht_params);
523 INIT_LIST_HEAD(&neigh_update->neigh_list);
524 spin_lock_init(&neigh_update->encap_lock);
525 INIT_DELAYED_WORK(&neigh_update->neigh_stats_work,
526 mlx5e_rep_neigh_stats_work);
527 mlx5e_rep_neigh_update_init_interval(rpriv);
529 rpriv->neigh_update.netevent_nb.notifier_call = mlx5e_rep_netevent_event;
530 err = register_netevent_notifier(&rpriv->neigh_update.netevent_nb);
536 rhashtable_destroy(&neigh_update->neigh_ht);
540 static void mlx5e_rep_neigh_cleanup(struct mlx5e_rep_priv *rpriv)
542 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
543 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
545 unregister_netevent_notifier(&neigh_update->netevent_nb);
547 flush_workqueue(priv->wq); /* flush neigh update works */
549 cancel_delayed_work_sync(&rpriv->neigh_update.neigh_stats_work);
551 rhashtable_destroy(&neigh_update->neigh_ht);
554 static int mlx5e_rep_neigh_entry_insert(struct mlx5e_priv *priv,
555 struct mlx5e_neigh_hash_entry *nhe)
557 struct mlx5e_rep_priv *rpriv = priv->ppriv;
560 err = rhashtable_insert_fast(&rpriv->neigh_update.neigh_ht,
562 mlx5e_neigh_ht_params);
566 list_add(&nhe->neigh_list, &rpriv->neigh_update.neigh_list);
571 static void mlx5e_rep_neigh_entry_remove(struct mlx5e_priv *priv,
572 struct mlx5e_neigh_hash_entry *nhe)
574 struct mlx5e_rep_priv *rpriv = priv->ppriv;
576 spin_lock_bh(&rpriv->neigh_update.encap_lock);
578 list_del(&nhe->neigh_list);
580 rhashtable_remove_fast(&rpriv->neigh_update.neigh_ht,
582 mlx5e_neigh_ht_params);
583 spin_unlock_bh(&rpriv->neigh_update.encap_lock);
586 /* This function must only be called under RTNL lock or under the
587 * representor's encap_lock in case RTNL mutex can't be held.
589 static struct mlx5e_neigh_hash_entry *
590 mlx5e_rep_neigh_entry_lookup(struct mlx5e_priv *priv,
591 struct mlx5e_neigh *m_neigh)
593 struct mlx5e_rep_priv *rpriv = priv->ppriv;
594 struct mlx5e_neigh_update_table *neigh_update = &rpriv->neigh_update;
596 return rhashtable_lookup_fast(&neigh_update->neigh_ht, m_neigh,
597 mlx5e_neigh_ht_params);
600 static int mlx5e_rep_neigh_entry_create(struct mlx5e_priv *priv,
601 struct mlx5e_encap_entry *e,
602 struct mlx5e_neigh_hash_entry **nhe)
606 *nhe = kzalloc(sizeof(**nhe), GFP_KERNEL);
610 memcpy(&(*nhe)->m_neigh, &e->m_neigh, sizeof(e->m_neigh));
611 INIT_WORK(&(*nhe)->neigh_update_work, mlx5e_rep_neigh_update);
612 INIT_LIST_HEAD(&(*nhe)->encap_list);
613 refcount_set(&(*nhe)->refcnt, 1);
615 err = mlx5e_rep_neigh_entry_insert(priv, *nhe);
625 static void mlx5e_rep_neigh_entry_destroy(struct mlx5e_priv *priv,
626 struct mlx5e_neigh_hash_entry *nhe)
628 /* The neigh hash entry must be removed from the hash table regardless
629 * of the reference count value, so it won't be found by the next
630 * neigh notification call. The neigh hash entry reference count is
631 * incremented only during creation and neigh notification calls and
632 * protects from freeing the nhe struct.
634 mlx5e_rep_neigh_entry_remove(priv, nhe);
635 mlx5e_rep_neigh_entry_release(nhe);
638 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
639 struct mlx5e_encap_entry *e)
641 struct mlx5e_neigh_hash_entry *nhe;
644 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
646 err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
650 list_add(&e->encap_list, &nhe->encap_list);
654 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
655 struct mlx5e_encap_entry *e)
657 struct mlx5e_neigh_hash_entry *nhe;
659 list_del(&e->encap_list);
660 nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
662 if (list_empty(&nhe->encap_list))
663 mlx5e_rep_neigh_entry_destroy(priv, nhe);
666 static int mlx5e_rep_open(struct net_device *dev)
668 struct mlx5e_priv *priv = netdev_priv(dev);
669 struct mlx5e_rep_priv *rpriv = priv->ppriv;
670 struct mlx5_eswitch_rep *rep = rpriv->rep;
671 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
674 mutex_lock(&priv->state_lock);
675 err = mlx5e_open_locked(dev);
679 if (!mlx5_eswitch_set_vport_state(esw, rep->vport,
680 MLX5_ESW_VPORT_ADMIN_STATE_UP))
681 netif_carrier_on(dev);
684 mutex_unlock(&priv->state_lock);
688 static int mlx5e_rep_close(struct net_device *dev)
690 struct mlx5e_priv *priv = netdev_priv(dev);
691 struct mlx5e_rep_priv *rpriv = priv->ppriv;
692 struct mlx5_eswitch_rep *rep = rpriv->rep;
693 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
696 mutex_lock(&priv->state_lock);
697 (void)mlx5_eswitch_set_vport_state(esw, rep->vport, MLX5_ESW_VPORT_ADMIN_STATE_DOWN);
698 ret = mlx5e_close_locked(dev);
699 mutex_unlock(&priv->state_lock);
703 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
704 char *buf, size_t len)
706 struct mlx5e_priv *priv = netdev_priv(dev);
707 struct mlx5e_rep_priv *rpriv = priv->ppriv;
708 struct mlx5_eswitch_rep *rep = rpriv->rep;
711 ret = snprintf(buf, len, "%d", rep->vport - 1);
719 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
720 struct tc_cls_flower_offload *cls_flower)
722 switch (cls_flower->command) {
723 case TC_CLSFLOWER_REPLACE:
724 return mlx5e_configure_flower(priv, cls_flower);
725 case TC_CLSFLOWER_DESTROY:
726 return mlx5e_delete_flower(priv, cls_flower);
727 case TC_CLSFLOWER_STATS:
728 return mlx5e_stats_flower(priv, cls_flower);
734 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
737 struct mlx5e_priv *priv = cb_priv;
739 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
743 case TC_SETUP_CLSFLOWER:
744 return mlx5e_rep_setup_tc_cls_flower(priv, type_data);
750 static int mlx5e_rep_setup_tc_block(struct net_device *dev,
751 struct tc_block_offload *f)
753 struct mlx5e_priv *priv = netdev_priv(dev);
755 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
758 switch (f->command) {
760 return tcf_block_cb_register(f->block, mlx5e_rep_setup_tc_cb,
762 case TC_BLOCK_UNBIND:
763 tcf_block_cb_unregister(f->block, mlx5e_rep_setup_tc_cb, priv);
770 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
775 return mlx5e_rep_setup_tc_block(dev, type_data);
781 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
783 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
784 struct mlx5e_rep_priv *rpriv = priv->ppriv;
785 struct mlx5_eswitch_rep *rep;
787 if (!MLX5_CAP_GEN(priv->mdev, vport_group_manager))
791 if (esw->mode == SRIOV_OFFLOADS &&
792 rep && rep->vport == FDB_UPLINK_VPORT)
798 static bool mlx5e_is_vf_vport_rep(struct mlx5e_priv *priv)
800 struct mlx5e_rep_priv *rpriv = priv->ppriv;
801 struct mlx5_eswitch_rep *rep = rpriv->rep;
803 if (rep && rep->vport != FDB_UPLINK_VPORT)
809 bool mlx5e_has_offload_stats(const struct net_device *dev, int attr_id)
811 struct mlx5e_priv *priv = netdev_priv(dev);
814 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
815 if (mlx5e_is_vf_vport_rep(priv) || mlx5e_is_uplink_rep(priv))
823 mlx5e_get_sw_stats64(const struct net_device *dev,
824 struct rtnl_link_stats64 *stats)
826 struct mlx5e_priv *priv = netdev_priv(dev);
827 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
829 stats->rx_packets = sstats->rx_packets;
830 stats->rx_bytes = sstats->rx_bytes;
831 stats->tx_packets = sstats->tx_packets;
832 stats->tx_bytes = sstats->tx_bytes;
834 stats->tx_dropped = sstats->tx_queue_dropped;
839 int mlx5e_get_offload_stats(int attr_id, const struct net_device *dev,
843 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
844 return mlx5e_get_sw_stats64(dev, sp);
851 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
853 struct mlx5e_priv *priv = netdev_priv(dev);
855 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
858 static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
859 .switchdev_port_attr_get = mlx5e_attr_get,
862 static const struct net_device_ops mlx5e_netdev_ops_rep = {
863 .ndo_open = mlx5e_rep_open,
864 .ndo_stop = mlx5e_rep_close,
865 .ndo_start_xmit = mlx5e_xmit,
866 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
867 .ndo_setup_tc = mlx5e_rep_setup_tc,
868 .ndo_get_stats64 = mlx5e_rep_get_stats,
869 .ndo_has_offload_stats = mlx5e_has_offload_stats,
870 .ndo_get_offload_stats = mlx5e_get_offload_stats,
873 static void mlx5e_build_rep_params(struct mlx5_core_dev *mdev,
874 struct mlx5e_params *params)
876 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
877 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
878 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
880 params->log_sq_size = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
881 params->rq_wq_type = MLX5_WQ_TYPE_LINKED_LIST;
882 params->log_rq_size = MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE;
884 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
885 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
887 params->tx_max_inline = mlx5e_get_max_inline_cap(mdev);
889 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
891 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
894 static void mlx5e_build_rep_netdev(struct net_device *netdev)
896 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
898 netdev->watchdog_timeo = 15 * HZ;
900 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
902 #ifdef CONFIG_NET_SWITCHDEV
903 netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
906 netdev->features |= NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
907 netdev->hw_features |= NETIF_F_HW_TC;
909 eth_hw_addr_random(netdev);
912 static void mlx5e_init_rep(struct mlx5_core_dev *mdev,
913 struct net_device *netdev,
914 const struct mlx5e_profile *profile,
917 struct mlx5e_priv *priv = netdev_priv(netdev);
920 priv->netdev = netdev;
921 priv->profile = profile;
924 mutex_init(&priv->state_lock);
926 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
928 priv->channels.params.num_channels = profile->max_nch(mdev);
930 priv->hard_mtu = MLX5E_ETH_HARD_MTU;
932 mlx5e_build_rep_params(mdev, &priv->channels.params);
933 mlx5e_build_rep_netdev(netdev);
935 mlx5e_timestamp_init(priv);
938 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
940 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
941 struct mlx5e_rep_priv *rpriv = priv->ppriv;
942 struct mlx5_eswitch_rep *rep = rpriv->rep;
943 struct mlx5_flow_handle *flow_rule;
946 mlx5e_init_l2_addr(priv);
948 err = mlx5e_create_direct_rqts(priv);
952 err = mlx5e_create_direct_tirs(priv);
954 goto err_destroy_direct_rqts;
956 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
958 priv->direct_tir[0].tirn);
959 if (IS_ERR(flow_rule)) {
960 err = PTR_ERR(flow_rule);
961 goto err_destroy_direct_tirs;
963 rpriv->vport_rx_rule = flow_rule;
965 err = mlx5e_tc_init(priv);
967 goto err_del_flow_rule;
972 mlx5_del_flow_rules(rpriv->vport_rx_rule);
973 err_destroy_direct_tirs:
974 mlx5e_destroy_direct_tirs(priv);
975 err_destroy_direct_rqts:
976 mlx5e_destroy_direct_rqts(priv);
980 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
982 struct mlx5e_rep_priv *rpriv = priv->ppriv;
984 mlx5e_tc_cleanup(priv);
985 mlx5_del_flow_rules(rpriv->vport_rx_rule);
986 mlx5e_destroy_direct_tirs(priv);
987 mlx5e_destroy_direct_rqts(priv);
990 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
994 err = mlx5e_create_tises(priv);
996 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1002 static int mlx5e_get_rep_max_num_channels(struct mlx5_core_dev *mdev)
1004 #define MLX5E_PORT_REPRESENTOR_NCH 1
1005 return MLX5E_PORT_REPRESENTOR_NCH;
1008 static const struct mlx5e_profile mlx5e_rep_profile = {
1009 .init = mlx5e_init_rep,
1010 .init_rx = mlx5e_init_rep_rx,
1011 .cleanup_rx = mlx5e_cleanup_rep_rx,
1012 .init_tx = mlx5e_init_rep_tx,
1013 .cleanup_tx = mlx5e_cleanup_nic_tx,
1014 .update_stats = mlx5e_rep_update_stats,
1015 .max_nch = mlx5e_get_rep_max_num_channels,
1016 .update_carrier = NULL,
1017 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1018 .rx_handlers.handle_rx_cqe_mpwqe = NULL /* Not supported */,
1022 /* e-Switch vport representors */
1025 mlx5e_nic_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1027 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1028 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1032 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
1033 err = mlx5e_add_sqs_fwd_rules(priv);
1038 err = mlx5e_rep_neigh_init(rpriv);
1040 goto err_remove_sqs;
1045 mlx5e_remove_sqs_fwd_rules(priv);
1050 mlx5e_nic_rep_unload(struct mlx5_eswitch_rep *rep)
1052 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1053 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1055 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1056 mlx5e_remove_sqs_fwd_rules(priv);
1058 /* clean (and re-init) existing uplink offloaded TC rules */
1059 mlx5e_tc_cleanup(priv);
1060 mlx5e_tc_init(priv);
1062 mlx5e_rep_neigh_cleanup(rpriv);
1066 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1068 struct mlx5e_rep_priv *uplink_rpriv;
1069 struct mlx5e_rep_priv *rpriv;
1070 struct net_device *netdev;
1071 struct mlx5e_priv *upriv;
1074 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1078 netdev = mlx5e_create_netdev(dev, &mlx5e_rep_profile, rpriv);
1080 pr_warn("Failed to create representor netdev for vport %d\n",
1086 rpriv->netdev = netdev;
1088 rep->rep_if[REP_ETH].priv = rpriv;
1089 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1091 err = mlx5e_attach_netdev(netdev_priv(netdev));
1093 pr_warn("Failed to attach representor netdev for vport %d\n",
1095 goto err_destroy_netdev;
1098 err = mlx5e_rep_neigh_init(rpriv);
1100 pr_warn("Failed to initialized neighbours handling for vport %d\n",
1102 goto err_detach_netdev;
1105 uplink_rpriv = mlx5_eswitch_get_uplink_priv(dev->priv.eswitch, REP_ETH);
1106 upriv = netdev_priv(uplink_rpriv->netdev);
1107 err = tc_setup_cb_egdev_register(netdev, mlx5e_setup_tc_block_cb,
1110 goto err_neigh_cleanup;
1112 err = register_netdev(netdev);
1114 pr_warn("Failed to register representor netdev for vport %d\n",
1116 goto err_egdev_cleanup;
1122 tc_setup_cb_egdev_unregister(netdev, mlx5e_setup_tc_block_cb,
1126 mlx5e_rep_neigh_cleanup(rpriv);
1129 mlx5e_detach_netdev(netdev_priv(netdev));
1132 mlx5e_destroy_netdev(netdev_priv(netdev));
1138 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1140 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1141 struct net_device *netdev = rpriv->netdev;
1142 struct mlx5e_priv *priv = netdev_priv(netdev);
1143 struct mlx5e_rep_priv *uplink_rpriv;
1144 void *ppriv = priv->ppriv;
1145 struct mlx5e_priv *upriv;
1147 unregister_netdev(netdev);
1148 uplink_rpriv = mlx5_eswitch_get_uplink_priv(priv->mdev->priv.eswitch,
1150 upriv = netdev_priv(uplink_rpriv->netdev);
1151 tc_setup_cb_egdev_unregister(netdev, mlx5e_setup_tc_block_cb,
1153 mlx5e_rep_neigh_cleanup(rpriv);
1154 mlx5e_detach_netdev(priv);
1155 mlx5e_destroy_netdev(priv);
1156 kfree(ppriv); /* mlx5e_rep_priv */
1159 static void mlx5e_rep_register_vf_vports(struct mlx5e_priv *priv)
1161 struct mlx5_core_dev *mdev = priv->mdev;
1162 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1163 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1166 for (vport = 1; vport < total_vfs; vport++) {
1167 struct mlx5_eswitch_rep_if rep_if = {};
1169 rep_if.load = mlx5e_vport_rep_load;
1170 rep_if.unload = mlx5e_vport_rep_unload;
1171 mlx5_eswitch_register_vport_rep(esw, vport, &rep_if, REP_ETH);
1175 static void mlx5e_rep_unregister_vf_vports(struct mlx5e_priv *priv)
1177 struct mlx5_core_dev *mdev = priv->mdev;
1178 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1179 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1182 for (vport = 1; vport < total_vfs; vport++)
1183 mlx5_eswitch_unregister_vport_rep(esw, vport, REP_ETH);
1186 void mlx5e_register_vport_reps(struct mlx5e_priv *priv)
1188 struct mlx5_core_dev *mdev = priv->mdev;
1189 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1190 struct mlx5_eswitch_rep_if rep_if;
1191 struct mlx5e_rep_priv *rpriv;
1193 rpriv = priv->ppriv;
1194 rpriv->netdev = priv->netdev;
1196 rep_if.load = mlx5e_nic_rep_load;
1197 rep_if.unload = mlx5e_nic_rep_unload;
1198 rep_if.priv = rpriv;
1199 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1200 mlx5_eswitch_register_vport_rep(esw, 0, &rep_if, REP_ETH); /* UPLINK PF vport*/
1202 mlx5e_rep_register_vf_vports(priv); /* VFs vports */
1205 void mlx5e_unregister_vport_reps(struct mlx5e_priv *priv)
1207 struct mlx5_core_dev *mdev = priv->mdev;
1208 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1210 mlx5e_rep_unregister_vf_vports(priv); /* VFs vports */
1211 mlx5_eswitch_unregister_vport_rep(esw, 0, REP_ETH); /* UPLINK PF*/
1214 void *mlx5e_alloc_nic_rep_priv(struct mlx5_core_dev *mdev)
1216 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1217 struct mlx5e_rep_priv *rpriv;
1219 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1223 rpriv->rep = &esw->offloads.vport_reps[0];