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;
673 mutex_lock(&priv->state_lock);
674 err = mlx5e_open_locked(dev);
678 if (!mlx5_modify_vport_admin_state(priv->mdev,
679 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_ESW_VPORT,
680 rep->vport, 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;
695 mutex_lock(&priv->state_lock);
696 mlx5_modify_vport_admin_state(priv->mdev,
697 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_ESW_VPORT,
698 rep->vport, MLX5_ESW_VPORT_ADMIN_STATE_DOWN);
699 ret = mlx5e_close_locked(dev);
700 mutex_unlock(&priv->state_lock);
704 static int mlx5e_rep_get_phys_port_name(struct net_device *dev,
705 char *buf, size_t len)
707 struct mlx5e_priv *priv = netdev_priv(dev);
708 struct mlx5e_rep_priv *rpriv = priv->ppriv;
709 struct mlx5_eswitch_rep *rep = rpriv->rep;
712 ret = snprintf(buf, len, "%d", rep->vport - 1);
720 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
721 struct tc_cls_flower_offload *cls_flower)
723 switch (cls_flower->command) {
724 case TC_CLSFLOWER_REPLACE:
725 return mlx5e_configure_flower(priv, cls_flower);
726 case TC_CLSFLOWER_DESTROY:
727 return mlx5e_delete_flower(priv, cls_flower);
728 case TC_CLSFLOWER_STATS:
729 return mlx5e_stats_flower(priv, cls_flower);
735 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
738 struct mlx5e_priv *priv = cb_priv;
740 if (!tc_cls_can_offload_and_chain0(priv->netdev, type_data))
744 case TC_SETUP_CLSFLOWER:
745 return mlx5e_rep_setup_tc_cls_flower(priv, type_data);
751 static int mlx5e_rep_setup_tc_block(struct net_device *dev,
752 struct tc_block_offload *f)
754 struct mlx5e_priv *priv = netdev_priv(dev);
756 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
759 switch (f->command) {
761 return tcf_block_cb_register(f->block, mlx5e_rep_setup_tc_cb,
763 case TC_BLOCK_UNBIND:
764 tcf_block_cb_unregister(f->block, mlx5e_rep_setup_tc_cb, priv);
771 static int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
776 return mlx5e_rep_setup_tc_block(dev, type_data);
782 bool mlx5e_is_uplink_rep(struct mlx5e_priv *priv)
784 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
785 struct mlx5e_rep_priv *rpriv = priv->ppriv;
786 struct mlx5_eswitch_rep *rep;
788 if (!MLX5_CAP_GEN(priv->mdev, vport_group_manager))
792 if (esw->mode == SRIOV_OFFLOADS &&
793 rep && rep->vport == FDB_UPLINK_VPORT)
799 static bool mlx5e_is_vf_vport_rep(struct mlx5e_priv *priv)
801 struct mlx5e_rep_priv *rpriv = priv->ppriv;
802 struct mlx5_eswitch_rep *rep = rpriv->rep;
804 if (rep && rep->vport != FDB_UPLINK_VPORT)
810 bool mlx5e_has_offload_stats(const struct net_device *dev, int attr_id)
812 struct mlx5e_priv *priv = netdev_priv(dev);
815 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
816 if (mlx5e_is_vf_vport_rep(priv) || mlx5e_is_uplink_rep(priv))
824 mlx5e_get_sw_stats64(const struct net_device *dev,
825 struct rtnl_link_stats64 *stats)
827 struct mlx5e_priv *priv = netdev_priv(dev);
828 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
830 stats->rx_packets = sstats->rx_packets;
831 stats->rx_bytes = sstats->rx_bytes;
832 stats->tx_packets = sstats->tx_packets;
833 stats->tx_bytes = sstats->tx_bytes;
835 stats->tx_dropped = sstats->tx_queue_dropped;
840 int mlx5e_get_offload_stats(int attr_id, const struct net_device *dev,
844 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
845 return mlx5e_get_sw_stats64(dev, sp);
852 mlx5e_rep_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
854 struct mlx5e_priv *priv = netdev_priv(dev);
856 memcpy(stats, &priv->stats.vf_vport, sizeof(*stats));
859 static const struct switchdev_ops mlx5e_rep_switchdev_ops = {
860 .switchdev_port_attr_get = mlx5e_attr_get,
863 static const struct net_device_ops mlx5e_netdev_ops_rep = {
864 .ndo_open = mlx5e_rep_open,
865 .ndo_stop = mlx5e_rep_close,
866 .ndo_start_xmit = mlx5e_xmit,
867 .ndo_get_phys_port_name = mlx5e_rep_get_phys_port_name,
868 .ndo_setup_tc = mlx5e_rep_setup_tc,
869 .ndo_get_stats64 = mlx5e_rep_get_stats,
870 .ndo_has_offload_stats = mlx5e_has_offload_stats,
871 .ndo_get_offload_stats = mlx5e_get_offload_stats,
874 static void mlx5e_build_rep_params(struct mlx5_core_dev *mdev,
875 struct mlx5e_params *params)
877 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
878 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
879 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
881 params->log_sq_size = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
882 params->rq_wq_type = MLX5_WQ_TYPE_LINKED_LIST;
883 params->log_rq_size = MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE;
885 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
886 mlx5e_set_rx_cq_mode_params(params, cq_period_mode);
888 params->tx_max_inline = mlx5e_get_max_inline_cap(mdev);
890 params->lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
892 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode);
895 static void mlx5e_build_rep_netdev(struct net_device *netdev)
897 netdev->netdev_ops = &mlx5e_netdev_ops_rep;
899 netdev->watchdog_timeo = 15 * HZ;
901 netdev->ethtool_ops = &mlx5e_rep_ethtool_ops;
903 #ifdef CONFIG_NET_SWITCHDEV
904 netdev->switchdev_ops = &mlx5e_rep_switchdev_ops;
907 netdev->features |= NETIF_F_VLAN_CHALLENGED | NETIF_F_HW_TC | NETIF_F_NETNS_LOCAL;
908 netdev->hw_features |= NETIF_F_HW_TC;
910 eth_hw_addr_random(netdev);
913 static void mlx5e_init_rep(struct mlx5_core_dev *mdev,
914 struct net_device *netdev,
915 const struct mlx5e_profile *profile,
918 struct mlx5e_priv *priv = netdev_priv(netdev);
921 priv->netdev = netdev;
922 priv->profile = profile;
925 mutex_init(&priv->state_lock);
927 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
929 priv->channels.params.num_channels = profile->max_nch(mdev);
931 priv->hard_mtu = MLX5E_ETH_HARD_MTU;
933 mlx5e_build_rep_params(mdev, &priv->channels.params);
934 mlx5e_build_rep_netdev(netdev);
936 mlx5e_timestamp_init(priv);
939 static int mlx5e_init_rep_rx(struct mlx5e_priv *priv)
941 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
942 struct mlx5e_rep_priv *rpriv = priv->ppriv;
943 struct mlx5_eswitch_rep *rep = rpriv->rep;
944 struct mlx5_flow_handle *flow_rule;
947 mlx5e_init_l2_addr(priv);
949 err = mlx5e_create_direct_rqts(priv);
953 err = mlx5e_create_direct_tirs(priv);
955 goto err_destroy_direct_rqts;
957 flow_rule = mlx5_eswitch_create_vport_rx_rule(esw,
959 priv->direct_tir[0].tirn);
960 if (IS_ERR(flow_rule)) {
961 err = PTR_ERR(flow_rule);
962 goto err_destroy_direct_tirs;
964 rpriv->vport_rx_rule = flow_rule;
966 err = mlx5e_tc_init(priv);
968 goto err_del_flow_rule;
973 mlx5_del_flow_rules(rpriv->vport_rx_rule);
974 err_destroy_direct_tirs:
975 mlx5e_destroy_direct_tirs(priv);
976 err_destroy_direct_rqts:
977 mlx5e_destroy_direct_rqts(priv);
981 static void mlx5e_cleanup_rep_rx(struct mlx5e_priv *priv)
983 struct mlx5e_rep_priv *rpriv = priv->ppriv;
985 mlx5e_tc_cleanup(priv);
986 mlx5_del_flow_rules(rpriv->vport_rx_rule);
987 mlx5e_destroy_direct_tirs(priv);
988 mlx5e_destroy_direct_rqts(priv);
991 static int mlx5e_init_rep_tx(struct mlx5e_priv *priv)
995 err = mlx5e_create_tises(priv);
997 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
1003 static int mlx5e_get_rep_max_num_channels(struct mlx5_core_dev *mdev)
1005 #define MLX5E_PORT_REPRESENTOR_NCH 1
1006 return MLX5E_PORT_REPRESENTOR_NCH;
1009 static const struct mlx5e_profile mlx5e_rep_profile = {
1010 .init = mlx5e_init_rep,
1011 .init_rx = mlx5e_init_rep_rx,
1012 .cleanup_rx = mlx5e_cleanup_rep_rx,
1013 .init_tx = mlx5e_init_rep_tx,
1014 .cleanup_tx = mlx5e_cleanup_nic_tx,
1015 .update_stats = mlx5e_rep_update_stats,
1016 .max_nch = mlx5e_get_rep_max_num_channels,
1017 .update_carrier = NULL,
1018 .rx_handlers.handle_rx_cqe = mlx5e_handle_rx_cqe_rep,
1019 .rx_handlers.handle_rx_cqe_mpwqe = NULL /* Not supported */,
1023 /* e-Switch vport representors */
1026 mlx5e_nic_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1028 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1029 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1033 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
1034 err = mlx5e_add_sqs_fwd_rules(priv);
1039 err = mlx5e_rep_neigh_init(rpriv);
1041 goto err_remove_sqs;
1046 mlx5e_remove_sqs_fwd_rules(priv);
1051 mlx5e_nic_rep_unload(struct mlx5_eswitch_rep *rep)
1053 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1054 struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
1056 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1057 mlx5e_remove_sqs_fwd_rules(priv);
1059 /* clean (and re-init) existing uplink offloaded TC rules */
1060 mlx5e_tc_cleanup(priv);
1061 mlx5e_tc_init(priv);
1063 mlx5e_rep_neigh_cleanup(rpriv);
1067 mlx5e_vport_rep_load(struct mlx5_core_dev *dev, struct mlx5_eswitch_rep *rep)
1069 struct mlx5e_rep_priv *uplink_rpriv;
1070 struct mlx5e_rep_priv *rpriv;
1071 struct net_device *netdev;
1072 struct mlx5e_priv *upriv;
1075 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1079 netdev = mlx5e_create_netdev(dev, &mlx5e_rep_profile, rpriv);
1081 pr_warn("Failed to create representor netdev for vport %d\n",
1087 rpriv->netdev = netdev;
1089 rep->rep_if[REP_ETH].priv = rpriv;
1090 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1092 err = mlx5e_attach_netdev(netdev_priv(netdev));
1094 pr_warn("Failed to attach representor netdev for vport %d\n",
1096 goto err_destroy_netdev;
1099 err = mlx5e_rep_neigh_init(rpriv);
1101 pr_warn("Failed to initialized neighbours handling for vport %d\n",
1103 goto err_detach_netdev;
1106 uplink_rpriv = mlx5_eswitch_get_uplink_priv(dev->priv.eswitch, REP_ETH);
1107 upriv = netdev_priv(uplink_rpriv->netdev);
1108 err = tc_setup_cb_egdev_register(netdev, mlx5e_setup_tc_block_cb,
1111 goto err_neigh_cleanup;
1113 err = register_netdev(netdev);
1115 pr_warn("Failed to register representor netdev for vport %d\n",
1117 goto err_egdev_cleanup;
1123 tc_setup_cb_egdev_unregister(netdev, mlx5e_setup_tc_block_cb,
1127 mlx5e_rep_neigh_cleanup(rpriv);
1130 mlx5e_detach_netdev(netdev_priv(netdev));
1133 mlx5e_destroy_netdev(netdev_priv(netdev));
1139 mlx5e_vport_rep_unload(struct mlx5_eswitch_rep *rep)
1141 struct mlx5e_rep_priv *rpriv = mlx5e_rep_to_rep_priv(rep);
1142 struct net_device *netdev = rpriv->netdev;
1143 struct mlx5e_priv *priv = netdev_priv(netdev);
1144 struct mlx5e_rep_priv *uplink_rpriv;
1145 void *ppriv = priv->ppriv;
1146 struct mlx5e_priv *upriv;
1148 unregister_netdev(netdev);
1149 uplink_rpriv = mlx5_eswitch_get_uplink_priv(priv->mdev->priv.eswitch,
1151 upriv = netdev_priv(uplink_rpriv->netdev);
1152 tc_setup_cb_egdev_unregister(netdev, mlx5e_setup_tc_block_cb,
1154 mlx5e_rep_neigh_cleanup(rpriv);
1155 mlx5e_detach_netdev(priv);
1156 mlx5e_destroy_netdev(priv);
1157 kfree(ppriv); /* mlx5e_rep_priv */
1160 static void mlx5e_rep_register_vf_vports(struct mlx5e_priv *priv)
1162 struct mlx5_core_dev *mdev = priv->mdev;
1163 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1164 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1167 for (vport = 1; vport < total_vfs; vport++) {
1168 struct mlx5_eswitch_rep_if rep_if = {};
1170 rep_if.load = mlx5e_vport_rep_load;
1171 rep_if.unload = mlx5e_vport_rep_unload;
1172 mlx5_eswitch_register_vport_rep(esw, vport, &rep_if, REP_ETH);
1176 static void mlx5e_rep_unregister_vf_vports(struct mlx5e_priv *priv)
1178 struct mlx5_core_dev *mdev = priv->mdev;
1179 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1180 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
1183 for (vport = 1; vport < total_vfs; vport++)
1184 mlx5_eswitch_unregister_vport_rep(esw, vport, REP_ETH);
1187 void mlx5e_register_vport_reps(struct mlx5e_priv *priv)
1189 struct mlx5_core_dev *mdev = priv->mdev;
1190 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1191 struct mlx5_eswitch_rep_if rep_if;
1192 struct mlx5e_rep_priv *rpriv;
1194 rpriv = priv->ppriv;
1195 rpriv->netdev = priv->netdev;
1197 rep_if.load = mlx5e_nic_rep_load;
1198 rep_if.unload = mlx5e_nic_rep_unload;
1199 rep_if.priv = rpriv;
1200 INIT_LIST_HEAD(&rpriv->vport_sqs_list);
1201 mlx5_eswitch_register_vport_rep(esw, 0, &rep_if, REP_ETH); /* UPLINK PF vport*/
1203 mlx5e_rep_register_vf_vports(priv); /* VFs vports */
1206 void mlx5e_unregister_vport_reps(struct mlx5e_priv *priv)
1208 struct mlx5_core_dev *mdev = priv->mdev;
1209 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1211 mlx5e_rep_unregister_vf_vports(priv); /* VFs vports */
1212 mlx5_eswitch_unregister_vport_rep(esw, 0, REP_ETH); /* UPLINK PF*/
1215 void *mlx5e_alloc_nic_rep_priv(struct mlx5_core_dev *mdev)
1217 struct mlx5_eswitch *esw = mdev->priv.eswitch;
1218 struct mlx5e_rep_priv *rpriv;
1220 rpriv = kzalloc(sizeof(*rpriv), GFP_KERNEL);
1224 rpriv->rep = &esw->offloads.vport_reps[0];