1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
6 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/rtnetlink.h>
16 #include <linux/of_net.h>
17 #include <net/devlink.h>
21 static DEFINE_MUTEX(dsa2_mutex);
22 LIST_HEAD(dsa_tree_list);
24 /* Track the bridges with forwarding offload enabled */
25 static unsigned long dsa_fwd_offloading_bridges;
28 * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
29 * @dst: collection of struct dsa_switch devices to notify.
30 * @e: event, must be of type DSA_NOTIFIER_*
31 * @v: event-specific value.
33 * Given a struct dsa_switch_tree, this can be used to run a function once for
34 * each member DSA switch. The other alternative of traversing the tree is only
35 * through its ports list, which does not uniquely list the switches.
37 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
39 struct raw_notifier_head *nh = &dst->nh;
42 err = raw_notifier_call_chain(nh, e, v);
44 return notifier_to_errno(err);
48 * dsa_broadcast - Notify all DSA trees in the system.
49 * @e: event, must be of type DSA_NOTIFIER_*
50 * @v: event-specific value.
52 * Can be used to notify the switching fabric of events such as cross-chip
53 * bridging between disjoint trees (such as islands of tagger-compatible
54 * switches bridged by an incompatible middle switch).
56 * WARNING: this function is not reliable during probe time, because probing
57 * between trees is asynchronous and not all DSA trees might have probed.
59 int dsa_broadcast(unsigned long e, void *v)
61 struct dsa_switch_tree *dst;
64 list_for_each_entry(dst, &dsa_tree_list, list) {
65 err = dsa_tree_notify(dst, e, v);
74 * dsa_lag_map() - Map LAG netdev to a linear LAG ID
75 * @dst: Tree in which to record the mapping.
76 * @lag: Netdev that is to be mapped to an ID.
78 * dsa_lag_id/dsa_lag_dev can then be used to translate between the
79 * two spaces. The size of the mapping space is determined by the
80 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
81 * it unset if it is not needed, in which case these functions become
84 void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag)
88 if (dsa_lag_id(dst, lag) >= 0)
92 for (id = 0; id < dst->lags_len; id++) {
93 if (!dsa_lag_dev(dst, id)) {
99 /* No IDs left, which is OK. Some drivers do not need it. The
100 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
101 * returns an error for this device when joining the LAG. The
102 * driver can then return -EOPNOTSUPP back to DSA, which will
103 * fall back to a software LAG.
108 * dsa_lag_unmap() - Remove a LAG ID mapping
109 * @dst: Tree in which the mapping is recorded.
110 * @lag: Netdev that was mapped.
112 * As there may be multiple users of the mapping, it is only removed
113 * if there are no other references to it.
115 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag)
120 dsa_lag_foreach_port(dp, dst, lag)
121 /* There are remaining users of this mapping */
124 dsa_lags_foreach_id(id, dst) {
125 if (dsa_lag_dev(dst, id) == lag) {
126 dst->lags[id] = NULL;
132 struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
133 const struct net_device *br)
137 list_for_each_entry(dp, &dst->ports, list)
138 if (dsa_port_bridge_dev_get(dp) == br)
144 static int dsa_bridge_num_find(const struct net_device *bridge_dev)
146 struct dsa_switch_tree *dst;
148 list_for_each_entry(dst, &dsa_tree_list, list) {
149 struct dsa_bridge *bridge;
151 bridge = dsa_tree_bridge_find(dst, bridge_dev);
159 unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
161 unsigned int bridge_num = dsa_bridge_num_find(bridge_dev);
163 /* Switches without FDB isolation support don't get unique
170 /* First port that requests FDB isolation or TX forwarding
171 * offload for this bridge
173 bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges,
174 DSA_MAX_NUM_OFFLOADING_BRIDGES,
176 if (bridge_num >= max)
179 set_bit(bridge_num, &dsa_fwd_offloading_bridges);
185 void dsa_bridge_num_put(const struct net_device *bridge_dev,
186 unsigned int bridge_num)
188 /* Since we refcount bridges, we know that when we call this function
189 * it is no longer in use, so we can just go ahead and remove it from
192 clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
195 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
197 struct dsa_switch_tree *dst;
200 list_for_each_entry(dst, &dsa_tree_list, list) {
201 if (dst->index != tree_index)
204 list_for_each_entry(dp, &dst->ports, list) {
205 if (dp->ds->index != sw_index)
214 EXPORT_SYMBOL_GPL(dsa_switch_find);
216 static struct dsa_switch_tree *dsa_tree_find(int index)
218 struct dsa_switch_tree *dst;
220 list_for_each_entry(dst, &dsa_tree_list, list)
221 if (dst->index == index)
227 static struct dsa_switch_tree *dsa_tree_alloc(int index)
229 struct dsa_switch_tree *dst;
231 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
237 INIT_LIST_HEAD(&dst->rtable);
239 INIT_LIST_HEAD(&dst->ports);
241 INIT_LIST_HEAD(&dst->list);
242 list_add_tail(&dst->list, &dsa_tree_list);
244 kref_init(&dst->refcount);
249 static void dsa_tree_free(struct dsa_switch_tree *dst)
252 dsa_tag_driver_put(dst->tag_ops);
253 list_del(&dst->list);
257 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
260 kref_get(&dst->refcount);
265 static struct dsa_switch_tree *dsa_tree_touch(int index)
267 struct dsa_switch_tree *dst;
269 dst = dsa_tree_find(index);
271 return dsa_tree_get(dst);
273 return dsa_tree_alloc(index);
276 static void dsa_tree_release(struct kref *ref)
278 struct dsa_switch_tree *dst;
280 dst = container_of(ref, struct dsa_switch_tree, refcount);
285 static void dsa_tree_put(struct dsa_switch_tree *dst)
288 kref_put(&dst->refcount, dsa_tree_release);
291 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
292 struct device_node *dn)
296 list_for_each_entry(dp, &dst->ports, list)
303 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
304 struct dsa_port *link_dp)
306 struct dsa_switch *ds = dp->ds;
307 struct dsa_switch_tree *dst;
312 list_for_each_entry(dl, &dst->rtable, list)
313 if (dl->dp == dp && dl->link_dp == link_dp)
316 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
321 dl->link_dp = link_dp;
323 INIT_LIST_HEAD(&dl->list);
324 list_add_tail(&dl->list, &dst->rtable);
329 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
331 struct dsa_switch *ds = dp->ds;
332 struct dsa_switch_tree *dst = ds->dst;
333 struct device_node *dn = dp->dn;
334 struct of_phandle_iterator it;
335 struct dsa_port *link_dp;
339 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
340 link_dp = dsa_tree_find_port_by_node(dst, it.node);
342 of_node_put(it.node);
346 dl = dsa_link_touch(dp, link_dp);
348 of_node_put(it.node);
356 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
358 bool complete = true;
361 list_for_each_entry(dp, &dst->ports, list) {
362 if (dsa_port_is_dsa(dp)) {
363 complete = dsa_port_setup_routing_table(dp);
372 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
376 list_for_each_entry(dp, &dst->ports, list)
377 if (dsa_port_is_cpu(dp))
383 /* Assign the default CPU port (the first one in the tree) to all ports of the
384 * fabric which don't already have one as part of their own switch.
386 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
388 struct dsa_port *cpu_dp, *dp;
390 cpu_dp = dsa_tree_find_first_cpu(dst);
392 pr_err("DSA: tree %d has no CPU port\n", dst->index);
396 list_for_each_entry(dp, &dst->ports, list) {
400 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
407 /* Perform initial assignment of CPU ports to user ports and DSA links in the
408 * fabric, giving preference to CPU ports local to each switch. Default to
409 * using the first CPU port in the switch tree if the port does not have a CPU
410 * port local to this switch.
412 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
414 struct dsa_port *cpu_dp, *dp;
416 list_for_each_entry(cpu_dp, &dst->ports, list) {
417 if (!dsa_port_is_cpu(cpu_dp))
420 /* Prefer a local CPU port */
421 dsa_switch_for_each_port(dp, cpu_dp->ds) {
422 /* Prefer the first local CPU port found */
426 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
431 return dsa_tree_setup_default_cpu(dst);
434 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
438 list_for_each_entry(dp, &dst->ports, list)
439 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
443 static int dsa_port_setup(struct dsa_port *dp)
445 struct devlink_port *dlp = &dp->devlink_port;
446 bool dsa_port_link_registered = false;
447 struct dsa_switch *ds = dp->ds;
448 bool dsa_port_enabled = false;
454 mutex_init(&dp->addr_lists_lock);
455 INIT_LIST_HEAD(&dp->fdbs);
456 INIT_LIST_HEAD(&dp->mdbs);
458 if (ds->ops->port_setup) {
459 err = ds->ops->port_setup(ds, dp->index);
465 case DSA_PORT_TYPE_UNUSED:
466 dsa_port_disable(dp);
468 case DSA_PORT_TYPE_CPU:
469 err = dsa_port_link_register_of(dp);
472 dsa_port_link_registered = true;
474 err = dsa_port_enable(dp, NULL);
477 dsa_port_enabled = true;
480 case DSA_PORT_TYPE_DSA:
481 err = dsa_port_link_register_of(dp);
484 dsa_port_link_registered = true;
486 err = dsa_port_enable(dp, NULL);
489 dsa_port_enabled = true;
492 case DSA_PORT_TYPE_USER:
493 of_get_mac_address(dp->dn, dp->mac);
494 err = dsa_slave_create(dp);
498 devlink_port_type_eth_set(dlp, dp->slave);
502 if (err && dsa_port_enabled)
503 dsa_port_disable(dp);
504 if (err && dsa_port_link_registered)
505 dsa_port_link_unregister_of(dp);
507 if (ds->ops->port_teardown)
508 ds->ops->port_teardown(ds, dp->index);
517 static int dsa_port_devlink_setup(struct dsa_port *dp)
519 struct devlink_port *dlp = &dp->devlink_port;
520 struct dsa_switch_tree *dst = dp->ds->dst;
521 struct devlink_port_attrs attrs = {};
522 struct devlink *dl = dp->ds->devlink;
523 const unsigned char *id;
527 id = (const unsigned char *)&dst->index;
528 len = sizeof(dst->index);
530 attrs.phys.port_number = dp->index;
531 memcpy(attrs.switch_id.id, id, len);
532 attrs.switch_id.id_len = len;
533 memset(dlp, 0, sizeof(*dlp));
536 case DSA_PORT_TYPE_UNUSED:
537 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
539 case DSA_PORT_TYPE_CPU:
540 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
542 case DSA_PORT_TYPE_DSA:
543 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
545 case DSA_PORT_TYPE_USER:
546 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
550 devlink_port_attrs_set(dlp, &attrs);
551 err = devlink_port_register(dl, dlp, dp->index);
554 dp->devlink_port_setup = true;
559 static void dsa_port_teardown(struct dsa_port *dp)
561 struct devlink_port *dlp = &dp->devlink_port;
562 struct dsa_switch *ds = dp->ds;
563 struct dsa_mac_addr *a, *tmp;
564 struct net_device *slave;
569 if (ds->ops->port_teardown)
570 ds->ops->port_teardown(ds, dp->index);
572 devlink_port_type_clear(dlp);
575 case DSA_PORT_TYPE_UNUSED:
577 case DSA_PORT_TYPE_CPU:
578 dsa_port_disable(dp);
579 dsa_port_link_unregister_of(dp);
581 case DSA_PORT_TYPE_DSA:
582 dsa_port_disable(dp);
583 dsa_port_link_unregister_of(dp);
585 case DSA_PORT_TYPE_USER:
590 dsa_slave_destroy(slave);
595 list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
600 list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
608 static void dsa_port_devlink_teardown(struct dsa_port *dp)
610 struct devlink_port *dlp = &dp->devlink_port;
612 if (dp->devlink_port_setup)
613 devlink_port_unregister(dlp);
614 dp->devlink_port_setup = false;
617 /* Destroy the current devlink port, and create a new one which has the UNUSED
618 * flavour. At this point, any call to ds->ops->port_setup has been already
619 * balanced out by a call to ds->ops->port_teardown, so we know that any
620 * devlink port regions the driver had are now unregistered. We then call its
621 * ds->ops->port_setup again, in order for the driver to re-create them on the
624 static int dsa_port_reinit_as_unused(struct dsa_port *dp)
626 struct dsa_switch *ds = dp->ds;
629 dsa_port_devlink_teardown(dp);
630 dp->type = DSA_PORT_TYPE_UNUSED;
631 err = dsa_port_devlink_setup(dp);
635 if (ds->ops->port_setup) {
636 /* On error, leave the devlink port registered,
637 * dsa_switch_teardown will clean it up later.
639 err = ds->ops->port_setup(ds, dp->index);
647 static int dsa_devlink_info_get(struct devlink *dl,
648 struct devlink_info_req *req,
649 struct netlink_ext_ack *extack)
651 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
653 if (ds->ops->devlink_info_get)
654 return ds->ops->devlink_info_get(ds, req, extack);
659 static int dsa_devlink_sb_pool_get(struct devlink *dl,
660 unsigned int sb_index, u16 pool_index,
661 struct devlink_sb_pool_info *pool_info)
663 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
665 if (!ds->ops->devlink_sb_pool_get)
668 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
672 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
673 u16 pool_index, u32 size,
674 enum devlink_sb_threshold_type threshold_type,
675 struct netlink_ext_ack *extack)
677 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
679 if (!ds->ops->devlink_sb_pool_set)
682 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
683 threshold_type, extack);
686 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
687 unsigned int sb_index, u16 pool_index,
690 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
691 int port = dsa_devlink_port_to_port(dlp);
693 if (!ds->ops->devlink_sb_port_pool_get)
696 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
697 pool_index, p_threshold);
700 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
701 unsigned int sb_index, u16 pool_index,
703 struct netlink_ext_ack *extack)
705 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
706 int port = dsa_devlink_port_to_port(dlp);
708 if (!ds->ops->devlink_sb_port_pool_set)
711 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
712 pool_index, threshold, extack);
716 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
717 unsigned int sb_index, u16 tc_index,
718 enum devlink_sb_pool_type pool_type,
719 u16 *p_pool_index, u32 *p_threshold)
721 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
722 int port = dsa_devlink_port_to_port(dlp);
724 if (!ds->ops->devlink_sb_tc_pool_bind_get)
727 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
729 p_pool_index, p_threshold);
733 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
734 unsigned int sb_index, u16 tc_index,
735 enum devlink_sb_pool_type pool_type,
736 u16 pool_index, u32 threshold,
737 struct netlink_ext_ack *extack)
739 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
740 int port = dsa_devlink_port_to_port(dlp);
742 if (!ds->ops->devlink_sb_tc_pool_bind_set)
745 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
747 pool_index, threshold,
751 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
752 unsigned int sb_index)
754 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
756 if (!ds->ops->devlink_sb_occ_snapshot)
759 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
762 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
763 unsigned int sb_index)
765 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
767 if (!ds->ops->devlink_sb_occ_max_clear)
770 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
773 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
774 unsigned int sb_index,
775 u16 pool_index, u32 *p_cur,
778 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
779 int port = dsa_devlink_port_to_port(dlp);
781 if (!ds->ops->devlink_sb_occ_port_pool_get)
784 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
785 pool_index, p_cur, p_max);
789 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
790 unsigned int sb_index, u16 tc_index,
791 enum devlink_sb_pool_type pool_type,
792 u32 *p_cur, u32 *p_max)
794 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
795 int port = dsa_devlink_port_to_port(dlp);
797 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
800 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
806 static const struct devlink_ops dsa_devlink_ops = {
807 .info_get = dsa_devlink_info_get,
808 .sb_pool_get = dsa_devlink_sb_pool_get,
809 .sb_pool_set = dsa_devlink_sb_pool_set,
810 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
811 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
812 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
813 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
814 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
815 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
816 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
817 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
820 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
822 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
823 struct dsa_switch_tree *dst = ds->dst;
824 struct dsa_port *cpu_dp;
827 if (tag_ops->proto == dst->default_proto)
830 dsa_switch_for_each_cpu_port(cpu_dp, ds) {
832 err = ds->ops->change_tag_protocol(ds, cpu_dp->index,
836 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
837 tag_ops->name, ERR_PTR(err));
843 if (tag_ops->connect) {
844 err = tag_ops->connect(ds);
849 if (ds->ops->connect_tag_protocol) {
850 err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
853 "Unable to connect to tag protocol \"%s\": %pe\n",
854 tag_ops->name, ERR_PTR(err));
862 if (tag_ops->disconnect)
863 tag_ops->disconnect(ds);
868 static int dsa_switch_setup(struct dsa_switch *ds)
870 struct dsa_devlink_priv *dl_priv;
877 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
878 * driver and before ops->setup() has run, since the switch drivers and
879 * the slave MDIO bus driver rely on these values for probing PHY
882 ds->phys_mii_mask |= dsa_user_ports(ds);
884 /* Add the switch to devlink before calling setup, so that setup can
888 devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
891 dl_priv = devlink_priv(ds->devlink);
894 /* Setup devlink port instances now, so that the switch
895 * setup() can register regions etc, against the ports
897 dsa_switch_for_each_port(dp, ds) {
898 err = dsa_port_devlink_setup(dp);
900 goto unregister_devlink_ports;
903 err = dsa_switch_register_notifier(ds);
905 goto unregister_devlink_ports;
907 ds->configure_vlan_while_not_filtering = true;
909 err = ds->ops->setup(ds);
911 goto unregister_notifier;
913 err = dsa_switch_setup_tag_protocol(ds);
917 if (!ds->slave_mii_bus && ds->ops->phy_read) {
918 ds->slave_mii_bus = mdiobus_alloc();
919 if (!ds->slave_mii_bus) {
924 dsa_slave_mii_bus_init(ds);
926 err = mdiobus_register(ds->slave_mii_bus);
928 goto free_slave_mii_bus;
932 devlink_register(ds->devlink);
936 if (ds->slave_mii_bus && ds->ops->phy_read)
937 mdiobus_free(ds->slave_mii_bus);
939 if (ds->ops->teardown)
940 ds->ops->teardown(ds);
942 dsa_switch_unregister_notifier(ds);
943 unregister_devlink_ports:
944 dsa_switch_for_each_port(dp, ds)
945 dsa_port_devlink_teardown(dp);
946 devlink_free(ds->devlink);
951 static void dsa_switch_teardown(struct dsa_switch *ds)
959 devlink_unregister(ds->devlink);
961 if (ds->slave_mii_bus && ds->ops->phy_read) {
962 mdiobus_unregister(ds->slave_mii_bus);
963 mdiobus_free(ds->slave_mii_bus);
964 ds->slave_mii_bus = NULL;
967 if (ds->ops->teardown)
968 ds->ops->teardown(ds);
970 dsa_switch_unregister_notifier(ds);
973 dsa_switch_for_each_port(dp, ds)
974 dsa_port_devlink_teardown(dp);
975 devlink_free(ds->devlink);
982 /* First tear down the non-shared, then the shared ports. This ensures that
983 * all work items scheduled by our switchdev handlers for user ports have
984 * completed before we destroy the refcounting kept on the shared ports.
986 static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
990 list_for_each_entry(dp, &dst->ports, list)
991 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
992 dsa_port_teardown(dp);
994 dsa_flush_workqueue();
996 list_for_each_entry(dp, &dst->ports, list)
997 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
998 dsa_port_teardown(dp);
1001 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
1003 struct dsa_port *dp;
1005 list_for_each_entry(dp, &dst->ports, list)
1006 dsa_switch_teardown(dp->ds);
1009 /* Bring shared ports up first, then non-shared ports */
1010 static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
1012 struct dsa_port *dp;
1015 list_for_each_entry(dp, &dst->ports, list) {
1016 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
1017 err = dsa_port_setup(dp);
1023 list_for_each_entry(dp, &dst->ports, list) {
1024 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
1025 err = dsa_port_setup(dp);
1027 err = dsa_port_reinit_as_unused(dp);
1037 dsa_tree_teardown_ports(dst);
1042 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
1044 struct dsa_port *dp;
1047 list_for_each_entry(dp, &dst->ports, list) {
1048 err = dsa_switch_setup(dp->ds);
1050 dsa_tree_teardown_switches(dst);
1058 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
1060 struct dsa_port *dp;
1065 list_for_each_entry(dp, &dst->ports, list) {
1066 if (dsa_port_is_cpu(dp)) {
1067 err = dsa_master_setup(dp->master, dp);
1078 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
1080 struct dsa_port *dp;
1084 list_for_each_entry(dp, &dst->ports, list)
1085 if (dsa_port_is_cpu(dp))
1086 dsa_master_teardown(dp->master);
1091 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
1093 unsigned int len = 0;
1094 struct dsa_port *dp;
1096 list_for_each_entry(dp, &dst->ports, list) {
1097 if (dp->ds->num_lag_ids > len)
1098 len = dp->ds->num_lag_ids;
1104 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
1108 dst->lags_len = len;
1112 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
1117 static int dsa_tree_setup(struct dsa_switch_tree *dst)
1123 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
1128 complete = dsa_tree_setup_routing_table(dst);
1132 err = dsa_tree_setup_cpu_ports(dst);
1136 err = dsa_tree_setup_switches(dst);
1138 goto teardown_cpu_ports;
1140 err = dsa_tree_setup_master(dst);
1142 goto teardown_switches;
1144 err = dsa_tree_setup_ports(dst);
1146 goto teardown_master;
1148 err = dsa_tree_setup_lags(dst);
1150 goto teardown_ports;
1154 pr_info("DSA: tree %d setup\n", dst->index);
1159 dsa_tree_teardown_ports(dst);
1161 dsa_tree_teardown_master(dst);
1163 dsa_tree_teardown_switches(dst);
1165 dsa_tree_teardown_cpu_ports(dst);
1170 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
1172 struct dsa_link *dl, *next;
1177 dsa_tree_teardown_lags(dst);
1179 dsa_tree_teardown_ports(dst);
1181 dsa_tree_teardown_master(dst);
1183 dsa_tree_teardown_switches(dst);
1185 dsa_tree_teardown_cpu_ports(dst);
1187 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
1188 list_del(&dl->list);
1192 pr_info("DSA: tree %d torn down\n", dst->index);
1197 static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
1198 const struct dsa_device_ops *tag_ops)
1200 const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
1201 struct dsa_notifier_tag_proto_info info;
1204 dst->tag_ops = tag_ops;
1206 /* Notify the switches from this tree about the connection
1209 info.tag_ops = tag_ops;
1210 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
1211 if (err && err != -EOPNOTSUPP)
1212 goto out_disconnect;
1214 /* Notify the old tagger about the disconnection from this tree */
1215 info.tag_ops = old_tag_ops;
1216 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1221 info.tag_ops = tag_ops;
1222 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1223 dst->tag_ops = old_tag_ops;
1228 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
1229 * is that all DSA switches within a tree share the same tagger, otherwise
1230 * they would have formed disjoint trees (different "dsa,member" values).
1232 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
1233 struct net_device *master,
1234 const struct dsa_device_ops *tag_ops,
1235 const struct dsa_device_ops *old_tag_ops)
1237 struct dsa_notifier_tag_proto_info info;
1238 struct dsa_port *dp;
1241 if (!rtnl_trylock())
1242 return restart_syscall();
1244 /* At the moment we don't allow changing the tag protocol under
1245 * traffic. The rtnl_mutex also happens to serialize concurrent
1246 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1247 * restriction, there needs to be another mutex which serializes this.
1249 if (master->flags & IFF_UP)
1252 list_for_each_entry(dp, &dst->ports, list) {
1253 if (!dsa_port_is_user(dp))
1256 if (dp->slave->flags & IFF_UP)
1260 /* Notify the tag protocol change */
1261 info.tag_ops = tag_ops;
1262 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1264 goto out_unwind_tagger;
1266 err = dsa_tree_bind_tag_proto(dst, tag_ops);
1268 goto out_unwind_tagger;
1275 info.tag_ops = old_tag_ops;
1276 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1282 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1284 struct dsa_switch_tree *dst = ds->dst;
1285 struct dsa_port *dp;
1287 dsa_switch_for_each_port(dp, ds)
1288 if (dp->index == index)
1291 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1298 INIT_LIST_HEAD(&dp->list);
1299 list_add_tail(&dp->list, &dst->ports);
1304 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1309 dp->type = DSA_PORT_TYPE_USER;
1315 static int dsa_port_parse_dsa(struct dsa_port *dp)
1317 dp->type = DSA_PORT_TYPE_DSA;
1322 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1323 struct net_device *master)
1325 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1326 struct dsa_switch *mds, *ds = dp->ds;
1327 unsigned int mdp_upstream;
1328 struct dsa_port *mdp;
1330 /* It is possible to stack DSA switches onto one another when that
1331 * happens the switch driver may want to know if its tagging protocol
1332 * is going to work in such a configuration.
1334 if (dsa_slave_dev_check(master)) {
1335 mdp = dsa_slave_to_port(master);
1337 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1338 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1339 DSA_TAG_PROTO_NONE);
1342 /* If the master device is not itself a DSA slave in a disjoint DSA
1343 * tree, then return immediately.
1345 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1348 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1349 const char *user_protocol)
1351 struct dsa_switch *ds = dp->ds;
1352 struct dsa_switch_tree *dst = ds->dst;
1353 const struct dsa_device_ops *tag_ops;
1354 enum dsa_tag_protocol default_proto;
1356 /* Find out which protocol the switch would prefer. */
1357 default_proto = dsa_get_tag_protocol(dp, master);
1358 if (dst->default_proto) {
1359 if (dst->default_proto != default_proto) {
1361 "A DSA switch tree can have only one tagging protocol\n");
1365 dst->default_proto = default_proto;
1368 /* See if the user wants to override that preference. */
1369 if (user_protocol) {
1370 if (!ds->ops->change_tag_protocol) {
1371 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1375 tag_ops = dsa_find_tagger_by_name(user_protocol);
1377 tag_ops = dsa_tag_driver_get(default_proto);
1380 if (IS_ERR(tag_ops)) {
1381 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1382 return -EPROBE_DEFER;
1384 dev_warn(ds->dev, "No tagger for this switch\n");
1385 return PTR_ERR(tag_ops);
1389 if (dst->tag_ops != tag_ops) {
1391 "A DSA switch tree can have only one tagging protocol\n");
1393 dsa_tag_driver_put(tag_ops);
1397 /* In the case of multiple CPU ports per switch, the tagging
1398 * protocol is still reference-counted only per switch tree.
1400 dsa_tag_driver_put(tag_ops);
1402 dst->tag_ops = tag_ops;
1405 dp->master = master;
1406 dp->type = DSA_PORT_TYPE_CPU;
1407 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1410 /* At this point, the tree may be configured to use a different
1411 * tagger than the one chosen by the switch driver during
1412 * .setup, in the case when a user selects a custom protocol
1415 * This is resolved by syncing the driver with the tree in
1416 * dsa_switch_setup_tag_protocol once .setup has run and the
1417 * driver is ready to accept calls to .change_tag_protocol. If
1418 * the driver does not support the custom protocol at that
1419 * point, the tree is wholly rejected, thereby ensuring that the
1420 * tree and driver are always in agreement on the protocol to
1426 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1428 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1429 const char *name = of_get_property(dn, "label", NULL);
1430 bool link = of_property_read_bool(dn, "link");
1435 struct net_device *master;
1436 const char *user_protocol;
1438 master = of_find_net_device_by_node(ethernet);
1440 return -EPROBE_DEFER;
1442 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1443 return dsa_port_parse_cpu(dp, master, user_protocol);
1447 return dsa_port_parse_dsa(dp);
1449 return dsa_port_parse_user(dp, name);
1452 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1453 struct device_node *dn)
1455 struct device_node *ports, *port;
1456 struct dsa_port *dp;
1460 ports = of_get_child_by_name(dn, "ports");
1462 /* The second possibility is "ethernet-ports" */
1463 ports = of_get_child_by_name(dn, "ethernet-ports");
1465 dev_err(ds->dev, "no ports child node found\n");
1470 for_each_available_child_of_node(ports, port) {
1471 err = of_property_read_u32(port, "reg", ®);
1477 if (reg >= ds->num_ports) {
1478 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
1479 port, reg, ds->num_ports);
1485 dp = dsa_to_port(ds, reg);
1487 err = dsa_port_parse_of(dp, port);
1499 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1500 struct device_node *dn)
1502 u32 m[2] = { 0, 0 };
1505 /* Don't error out if this optional property isn't found */
1506 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1507 if (sz < 0 && sz != -EINVAL)
1512 ds->dst = dsa_tree_touch(m[0]);
1516 if (dsa_switch_find(ds->dst->index, ds->index)) {
1518 "A DSA switch with index %d already exists in tree %d\n",
1519 ds->index, ds->dst->index);
1523 if (ds->dst->last_switch < ds->index)
1524 ds->dst->last_switch = ds->index;
1529 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1531 struct dsa_port *dp;
1534 for (port = 0; port < ds->num_ports; port++) {
1535 dp = dsa_port_touch(ds, port);
1543 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1547 err = dsa_switch_parse_member_of(ds, dn);
1551 err = dsa_switch_touch_ports(ds);
1555 return dsa_switch_parse_ports_of(ds, dn);
1558 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1561 if (!strcmp(name, "cpu")) {
1562 struct net_device *master;
1564 master = dsa_dev_to_net_device(dev);
1566 return -EPROBE_DEFER;
1570 return dsa_port_parse_cpu(dp, master, NULL);
1573 if (!strcmp(name, "dsa"))
1574 return dsa_port_parse_dsa(dp);
1576 return dsa_port_parse_user(dp, name);
1579 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1580 struct dsa_chip_data *cd)
1582 bool valid_name_found = false;
1583 struct dsa_port *dp;
1589 for (i = 0; i < DSA_MAX_PORTS; i++) {
1590 name = cd->port_names[i];
1591 dev = cd->netdev[i];
1592 dp = dsa_to_port(ds, i);
1597 err = dsa_port_parse(dp, name, dev);
1601 valid_name_found = true;
1604 if (!valid_name_found && i == DSA_MAX_PORTS)
1610 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1616 /* We don't support interconnected switches nor multiple trees via
1617 * platform data, so this is the unique switch of the tree.
1620 ds->dst = dsa_tree_touch(0);
1624 err = dsa_switch_touch_ports(ds);
1628 return dsa_switch_parse_ports(ds, cd);
1631 static void dsa_switch_release_ports(struct dsa_switch *ds)
1633 struct dsa_port *dp, *next;
1635 dsa_switch_for_each_port_safe(dp, next, ds) {
1636 list_del(&dp->list);
1641 static int dsa_switch_probe(struct dsa_switch *ds)
1643 struct dsa_switch_tree *dst;
1644 struct dsa_chip_data *pdata;
1645 struct device_node *np;
1651 pdata = ds->dev->platform_data;
1652 np = ds->dev->of_node;
1658 err = dsa_switch_parse_of(ds, np);
1660 dsa_switch_release_ports(ds);
1662 err = dsa_switch_parse(ds, pdata);
1664 dsa_switch_release_ports(ds);
1674 err = dsa_tree_setup(dst);
1676 dsa_switch_release_ports(ds);
1683 int dsa_register_switch(struct dsa_switch *ds)
1687 mutex_lock(&dsa2_mutex);
1688 err = dsa_switch_probe(ds);
1689 dsa_tree_put(ds->dst);
1690 mutex_unlock(&dsa2_mutex);
1694 EXPORT_SYMBOL_GPL(dsa_register_switch);
1696 static void dsa_switch_remove(struct dsa_switch *ds)
1698 struct dsa_switch_tree *dst = ds->dst;
1700 dsa_tree_teardown(dst);
1701 dsa_switch_release_ports(ds);
1705 void dsa_unregister_switch(struct dsa_switch *ds)
1707 mutex_lock(&dsa2_mutex);
1708 dsa_switch_remove(ds);
1709 mutex_unlock(&dsa2_mutex);
1711 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1713 /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1714 * blocking that operation from completion, due to the dev_hold taken inside
1715 * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1716 * the DSA master, so that the system can reboot successfully.
1718 void dsa_switch_shutdown(struct dsa_switch *ds)
1720 struct net_device *master, *slave_dev;
1721 struct dsa_port *dp;
1723 mutex_lock(&dsa2_mutex);
1726 dsa_switch_for_each_user_port(dp, ds) {
1727 master = dp->cpu_dp->master;
1728 slave_dev = dp->slave;
1730 netdev_upper_dev_unlink(master, slave_dev);
1733 /* Disconnect from further netdevice notifiers on the master,
1734 * since netdev_uses_dsa() will now return false.
1736 dsa_switch_for_each_cpu_port(dp, ds)
1737 dp->master->dsa_ptr = NULL;
1740 mutex_unlock(&dsa2_mutex);
1742 EXPORT_SYMBOL_GPL(dsa_switch_shutdown);