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
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>
18 #include <net/sch_generic.h>
22 static DEFINE_MUTEX(dsa2_mutex);
23 LIST_HEAD(dsa_tree_list);
25 /* Track the bridges with forwarding offload enabled */
26 static unsigned long dsa_fwd_offloading_bridges;
29 * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
30 * @dst: collection of struct dsa_switch devices to notify.
31 * @e: event, must be of type DSA_NOTIFIER_*
32 * @v: event-specific value.
34 * Given a struct dsa_switch_tree, this can be used to run a function once for
35 * each member DSA switch. The other alternative of traversing the tree is only
36 * through its ports list, which does not uniquely list the switches.
38 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
40 struct raw_notifier_head *nh = &dst->nh;
43 err = raw_notifier_call_chain(nh, e, v);
45 return notifier_to_errno(err);
49 * dsa_broadcast - Notify all DSA trees in the system.
50 * @e: event, must be of type DSA_NOTIFIER_*
51 * @v: event-specific value.
53 * Can be used to notify the switching fabric of events such as cross-chip
54 * bridging between disjoint trees (such as islands of tagger-compatible
55 * switches bridged by an incompatible middle switch).
57 * WARNING: this function is not reliable during probe time, because probing
58 * between trees is asynchronous and not all DSA trees might have probed.
60 int dsa_broadcast(unsigned long e, void *v)
62 struct dsa_switch_tree *dst;
65 list_for_each_entry(dst, &dsa_tree_list, list) {
66 err = dsa_tree_notify(dst, e, v);
75 * dsa_lag_map() - Map LAG structure to a linear LAG array
76 * @dst: Tree in which to record the mapping.
77 * @lag: LAG structure that is to be mapped to the tree's array.
79 * dsa_lag_id/dsa_lag_by_id can then be used to translate between the
80 * two spaces. The size of the mapping space is determined by the
81 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
82 * it unset if it is not needed, in which case these functions become
85 void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag)
89 for (id = 1; id <= dst->lags_len; id++) {
90 if (!dsa_lag_by_id(dst, id)) {
91 dst->lags[id - 1] = lag;
97 /* No IDs left, which is OK. Some drivers do not need it. The
98 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
99 * returns an error for this device when joining the LAG. The
100 * driver can then return -EOPNOTSUPP back to DSA, which will
101 * fall back to a software LAG.
106 * dsa_lag_unmap() - Remove a LAG ID mapping
107 * @dst: Tree in which the mapping is recorded.
108 * @lag: LAG structure that was mapped.
110 * As there may be multiple users of the mapping, it is only removed
111 * if there are no other references to it.
113 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag)
117 dsa_lags_foreach_id(id, dst) {
118 if (dsa_lag_by_id(dst, id) == lag) {
119 dst->lags[id - 1] = NULL;
126 struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
127 const struct net_device *lag_dev)
131 list_for_each_entry(dp, &dst->ports, list)
132 if (dsa_port_lag_dev_get(dp) == lag_dev)
138 struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
139 const struct net_device *br)
143 list_for_each_entry(dp, &dst->ports, list)
144 if (dsa_port_bridge_dev_get(dp) == br)
150 static int dsa_bridge_num_find(const struct net_device *bridge_dev)
152 struct dsa_switch_tree *dst;
154 list_for_each_entry(dst, &dsa_tree_list, list) {
155 struct dsa_bridge *bridge;
157 bridge = dsa_tree_bridge_find(dst, bridge_dev);
165 unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
167 unsigned int bridge_num = dsa_bridge_num_find(bridge_dev);
169 /* Switches without FDB isolation support don't get unique
176 /* First port that requests FDB isolation or TX forwarding
177 * offload for this bridge
179 bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges,
180 DSA_MAX_NUM_OFFLOADING_BRIDGES,
182 if (bridge_num >= max)
185 set_bit(bridge_num, &dsa_fwd_offloading_bridges);
191 void dsa_bridge_num_put(const struct net_device *bridge_dev,
192 unsigned int bridge_num)
194 /* Since we refcount bridges, we know that when we call this function
195 * it is no longer in use, so we can just go ahead and remove it from
198 clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
201 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
203 struct dsa_switch_tree *dst;
206 list_for_each_entry(dst, &dsa_tree_list, list) {
207 if (dst->index != tree_index)
210 list_for_each_entry(dp, &dst->ports, list) {
211 if (dp->ds->index != sw_index)
220 EXPORT_SYMBOL_GPL(dsa_switch_find);
222 static struct dsa_switch_tree *dsa_tree_find(int index)
224 struct dsa_switch_tree *dst;
226 list_for_each_entry(dst, &dsa_tree_list, list)
227 if (dst->index == index)
233 static struct dsa_switch_tree *dsa_tree_alloc(int index)
235 struct dsa_switch_tree *dst;
237 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
243 INIT_LIST_HEAD(&dst->rtable);
245 INIT_LIST_HEAD(&dst->ports);
247 INIT_LIST_HEAD(&dst->list);
248 list_add_tail(&dst->list, &dsa_tree_list);
250 kref_init(&dst->refcount);
255 static void dsa_tree_free(struct dsa_switch_tree *dst)
258 dsa_tag_driver_put(dst->tag_ops);
259 list_del(&dst->list);
263 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
266 kref_get(&dst->refcount);
271 static struct dsa_switch_tree *dsa_tree_touch(int index)
273 struct dsa_switch_tree *dst;
275 dst = dsa_tree_find(index);
277 return dsa_tree_get(dst);
279 return dsa_tree_alloc(index);
282 static void dsa_tree_release(struct kref *ref)
284 struct dsa_switch_tree *dst;
286 dst = container_of(ref, struct dsa_switch_tree, refcount);
291 static void dsa_tree_put(struct dsa_switch_tree *dst)
294 kref_put(&dst->refcount, dsa_tree_release);
297 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
298 struct device_node *dn)
302 list_for_each_entry(dp, &dst->ports, list)
309 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
310 struct dsa_port *link_dp)
312 struct dsa_switch *ds = dp->ds;
313 struct dsa_switch_tree *dst;
318 list_for_each_entry(dl, &dst->rtable, list)
319 if (dl->dp == dp && dl->link_dp == link_dp)
322 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
327 dl->link_dp = link_dp;
329 INIT_LIST_HEAD(&dl->list);
330 list_add_tail(&dl->list, &dst->rtable);
335 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
337 struct dsa_switch *ds = dp->ds;
338 struct dsa_switch_tree *dst = ds->dst;
339 struct device_node *dn = dp->dn;
340 struct of_phandle_iterator it;
341 struct dsa_port *link_dp;
345 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
346 link_dp = dsa_tree_find_port_by_node(dst, it.node);
348 of_node_put(it.node);
352 dl = dsa_link_touch(dp, link_dp);
354 of_node_put(it.node);
362 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
364 bool complete = true;
367 list_for_each_entry(dp, &dst->ports, list) {
368 if (dsa_port_is_dsa(dp)) {
369 complete = dsa_port_setup_routing_table(dp);
378 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
382 list_for_each_entry(dp, &dst->ports, list)
383 if (dsa_port_is_cpu(dp))
389 /* Assign the default CPU port (the first one in the tree) to all ports of the
390 * fabric which don't already have one as part of their own switch.
392 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
394 struct dsa_port *cpu_dp, *dp;
396 cpu_dp = dsa_tree_find_first_cpu(dst);
398 pr_err("DSA: tree %d has no CPU port\n", dst->index);
402 list_for_each_entry(dp, &dst->ports, list) {
406 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
413 /* Perform initial assignment of CPU ports to user ports and DSA links in the
414 * fabric, giving preference to CPU ports local to each switch. Default to
415 * using the first CPU port in the switch tree if the port does not have a CPU
416 * port local to this switch.
418 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
420 struct dsa_port *cpu_dp, *dp;
422 list_for_each_entry(cpu_dp, &dst->ports, list) {
423 if (!dsa_port_is_cpu(cpu_dp))
426 /* Prefer a local CPU port */
427 dsa_switch_for_each_port(dp, cpu_dp->ds) {
428 /* Prefer the first local CPU port found */
432 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
437 return dsa_tree_setup_default_cpu(dst);
440 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
444 list_for_each_entry(dp, &dst->ports, list)
445 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
449 static int dsa_port_setup(struct dsa_port *dp)
451 struct devlink_port *dlp = &dp->devlink_port;
452 bool dsa_port_link_registered = false;
453 struct dsa_switch *ds = dp->ds;
454 bool dsa_port_enabled = false;
460 mutex_init(&dp->addr_lists_lock);
461 mutex_init(&dp->vlans_lock);
462 INIT_LIST_HEAD(&dp->fdbs);
463 INIT_LIST_HEAD(&dp->mdbs);
464 INIT_LIST_HEAD(&dp->vlans);
466 if (ds->ops->port_setup) {
467 err = ds->ops->port_setup(ds, dp->index);
473 case DSA_PORT_TYPE_UNUSED:
474 dsa_port_disable(dp);
476 case DSA_PORT_TYPE_CPU:
477 err = dsa_port_link_register_of(dp);
480 dsa_port_link_registered = true;
482 err = dsa_port_enable(dp, NULL);
485 dsa_port_enabled = true;
488 case DSA_PORT_TYPE_DSA:
489 err = dsa_port_link_register_of(dp);
492 dsa_port_link_registered = true;
494 err = dsa_port_enable(dp, NULL);
497 dsa_port_enabled = true;
500 case DSA_PORT_TYPE_USER:
501 of_get_mac_address(dp->dn, dp->mac);
502 err = dsa_slave_create(dp);
506 devlink_port_type_eth_set(dlp, dp->slave);
510 if (err && dsa_port_enabled)
511 dsa_port_disable(dp);
512 if (err && dsa_port_link_registered)
513 dsa_port_link_unregister_of(dp);
515 if (ds->ops->port_teardown)
516 ds->ops->port_teardown(ds, dp->index);
525 static int dsa_port_devlink_setup(struct dsa_port *dp)
527 struct devlink_port *dlp = &dp->devlink_port;
528 struct dsa_switch_tree *dst = dp->ds->dst;
529 struct devlink_port_attrs attrs = {};
530 struct devlink *dl = dp->ds->devlink;
531 const unsigned char *id;
535 id = (const unsigned char *)&dst->index;
536 len = sizeof(dst->index);
538 attrs.phys.port_number = dp->index;
539 memcpy(attrs.switch_id.id, id, len);
540 attrs.switch_id.id_len = len;
541 memset(dlp, 0, sizeof(*dlp));
544 case DSA_PORT_TYPE_UNUSED:
545 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
547 case DSA_PORT_TYPE_CPU:
548 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
550 case DSA_PORT_TYPE_DSA:
551 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
553 case DSA_PORT_TYPE_USER:
554 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
558 devlink_port_attrs_set(dlp, &attrs);
559 err = devlink_port_register(dl, dlp, dp->index);
562 dp->devlink_port_setup = true;
567 static void dsa_port_teardown(struct dsa_port *dp)
569 struct devlink_port *dlp = &dp->devlink_port;
570 struct dsa_switch *ds = dp->ds;
571 struct dsa_mac_addr *a, *tmp;
572 struct net_device *slave;
573 struct dsa_vlan *v, *n;
578 if (ds->ops->port_teardown)
579 ds->ops->port_teardown(ds, dp->index);
581 devlink_port_type_clear(dlp);
584 case DSA_PORT_TYPE_UNUSED:
586 case DSA_PORT_TYPE_CPU:
587 dsa_port_disable(dp);
588 dsa_port_link_unregister_of(dp);
590 case DSA_PORT_TYPE_DSA:
591 dsa_port_disable(dp);
592 dsa_port_link_unregister_of(dp);
594 case DSA_PORT_TYPE_USER:
599 dsa_slave_destroy(slave);
604 list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
609 list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
614 list_for_each_entry_safe(v, n, &dp->vlans, list) {
622 static void dsa_port_devlink_teardown(struct dsa_port *dp)
624 struct devlink_port *dlp = &dp->devlink_port;
626 if (dp->devlink_port_setup)
627 devlink_port_unregister(dlp);
628 dp->devlink_port_setup = false;
631 /* Destroy the current devlink port, and create a new one which has the UNUSED
632 * flavour. At this point, any call to ds->ops->port_setup has been already
633 * balanced out by a call to ds->ops->port_teardown, so we know that any
634 * devlink port regions the driver had are now unregistered. We then call its
635 * ds->ops->port_setup again, in order for the driver to re-create them on the
638 static int dsa_port_reinit_as_unused(struct dsa_port *dp)
640 struct dsa_switch *ds = dp->ds;
643 dsa_port_devlink_teardown(dp);
644 dp->type = DSA_PORT_TYPE_UNUSED;
645 err = dsa_port_devlink_setup(dp);
649 if (ds->ops->port_setup) {
650 /* On error, leave the devlink port registered,
651 * dsa_switch_teardown will clean it up later.
653 err = ds->ops->port_setup(ds, dp->index);
661 static int dsa_devlink_info_get(struct devlink *dl,
662 struct devlink_info_req *req,
663 struct netlink_ext_ack *extack)
665 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
667 if (ds->ops->devlink_info_get)
668 return ds->ops->devlink_info_get(ds, req, extack);
673 static int dsa_devlink_sb_pool_get(struct devlink *dl,
674 unsigned int sb_index, u16 pool_index,
675 struct devlink_sb_pool_info *pool_info)
677 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
679 if (!ds->ops->devlink_sb_pool_get)
682 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
686 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
687 u16 pool_index, u32 size,
688 enum devlink_sb_threshold_type threshold_type,
689 struct netlink_ext_ack *extack)
691 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
693 if (!ds->ops->devlink_sb_pool_set)
696 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
697 threshold_type, extack);
700 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
701 unsigned int sb_index, u16 pool_index,
704 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
705 int port = dsa_devlink_port_to_port(dlp);
707 if (!ds->ops->devlink_sb_port_pool_get)
710 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
711 pool_index, p_threshold);
714 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
715 unsigned int sb_index, u16 pool_index,
717 struct netlink_ext_ack *extack)
719 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
720 int port = dsa_devlink_port_to_port(dlp);
722 if (!ds->ops->devlink_sb_port_pool_set)
725 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
726 pool_index, threshold, extack);
730 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
731 unsigned int sb_index, u16 tc_index,
732 enum devlink_sb_pool_type pool_type,
733 u16 *p_pool_index, u32 *p_threshold)
735 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
736 int port = dsa_devlink_port_to_port(dlp);
738 if (!ds->ops->devlink_sb_tc_pool_bind_get)
741 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
743 p_pool_index, p_threshold);
747 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
748 unsigned int sb_index, u16 tc_index,
749 enum devlink_sb_pool_type pool_type,
750 u16 pool_index, u32 threshold,
751 struct netlink_ext_ack *extack)
753 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
754 int port = dsa_devlink_port_to_port(dlp);
756 if (!ds->ops->devlink_sb_tc_pool_bind_set)
759 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
761 pool_index, threshold,
765 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
766 unsigned int sb_index)
768 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
770 if (!ds->ops->devlink_sb_occ_snapshot)
773 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
776 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
777 unsigned int sb_index)
779 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
781 if (!ds->ops->devlink_sb_occ_max_clear)
784 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
787 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
788 unsigned int sb_index,
789 u16 pool_index, u32 *p_cur,
792 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
793 int port = dsa_devlink_port_to_port(dlp);
795 if (!ds->ops->devlink_sb_occ_port_pool_get)
798 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
799 pool_index, p_cur, p_max);
803 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
804 unsigned int sb_index, u16 tc_index,
805 enum devlink_sb_pool_type pool_type,
806 u32 *p_cur, u32 *p_max)
808 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
809 int port = dsa_devlink_port_to_port(dlp);
811 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
814 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
820 static const struct devlink_ops dsa_devlink_ops = {
821 .info_get = dsa_devlink_info_get,
822 .sb_pool_get = dsa_devlink_sb_pool_get,
823 .sb_pool_set = dsa_devlink_sb_pool_set,
824 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
825 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
826 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
827 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
828 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
829 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
830 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
831 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
834 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
836 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
837 struct dsa_switch_tree *dst = ds->dst;
838 struct dsa_port *cpu_dp;
841 if (tag_ops->proto == dst->default_proto)
844 dsa_switch_for_each_cpu_port(cpu_dp, ds) {
846 err = ds->ops->change_tag_protocol(ds, cpu_dp->index,
850 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
851 tag_ops->name, ERR_PTR(err));
857 if (tag_ops->connect) {
858 err = tag_ops->connect(ds);
863 if (ds->ops->connect_tag_protocol) {
864 err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
867 "Unable to connect to tag protocol \"%s\": %pe\n",
868 tag_ops->name, ERR_PTR(err));
876 if (tag_ops->disconnect)
877 tag_ops->disconnect(ds);
882 static int dsa_switch_setup(struct dsa_switch *ds)
884 struct dsa_devlink_priv *dl_priv;
891 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
892 * driver and before ops->setup() has run, since the switch drivers and
893 * the slave MDIO bus driver rely on these values for probing PHY
896 ds->phys_mii_mask |= dsa_user_ports(ds);
898 /* Add the switch to devlink before calling setup, so that setup can
902 devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
905 dl_priv = devlink_priv(ds->devlink);
908 /* Setup devlink port instances now, so that the switch
909 * setup() can register regions etc, against the ports
911 dsa_switch_for_each_port(dp, ds) {
912 err = dsa_port_devlink_setup(dp);
914 goto unregister_devlink_ports;
917 err = dsa_switch_register_notifier(ds);
919 goto unregister_devlink_ports;
921 ds->configure_vlan_while_not_filtering = true;
923 err = ds->ops->setup(ds);
925 goto unregister_notifier;
927 err = dsa_switch_setup_tag_protocol(ds);
931 if (!ds->slave_mii_bus && ds->ops->phy_read) {
932 ds->slave_mii_bus = mdiobus_alloc();
933 if (!ds->slave_mii_bus) {
938 dsa_slave_mii_bus_init(ds);
940 err = mdiobus_register(ds->slave_mii_bus);
942 goto free_slave_mii_bus;
946 devlink_register(ds->devlink);
950 if (ds->slave_mii_bus && ds->ops->phy_read)
951 mdiobus_free(ds->slave_mii_bus);
953 if (ds->ops->teardown)
954 ds->ops->teardown(ds);
956 dsa_switch_unregister_notifier(ds);
957 unregister_devlink_ports:
958 dsa_switch_for_each_port(dp, ds)
959 dsa_port_devlink_teardown(dp);
960 devlink_free(ds->devlink);
965 static void dsa_switch_teardown(struct dsa_switch *ds)
973 devlink_unregister(ds->devlink);
975 if (ds->slave_mii_bus && ds->ops->phy_read) {
976 mdiobus_unregister(ds->slave_mii_bus);
977 mdiobus_free(ds->slave_mii_bus);
978 ds->slave_mii_bus = NULL;
981 if (ds->ops->teardown)
982 ds->ops->teardown(ds);
984 dsa_switch_unregister_notifier(ds);
987 dsa_switch_for_each_port(dp, ds)
988 dsa_port_devlink_teardown(dp);
989 devlink_free(ds->devlink);
996 /* First tear down the non-shared, then the shared ports. This ensures that
997 * all work items scheduled by our switchdev handlers for user ports have
998 * completed before we destroy the refcounting kept on the shared ports.
1000 static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
1002 struct dsa_port *dp;
1004 list_for_each_entry(dp, &dst->ports, list)
1005 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
1006 dsa_port_teardown(dp);
1008 dsa_flush_workqueue();
1010 list_for_each_entry(dp, &dst->ports, list)
1011 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
1012 dsa_port_teardown(dp);
1015 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
1017 struct dsa_port *dp;
1019 list_for_each_entry(dp, &dst->ports, list)
1020 dsa_switch_teardown(dp->ds);
1023 /* Bring shared ports up first, then non-shared ports */
1024 static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
1026 struct dsa_port *dp;
1029 list_for_each_entry(dp, &dst->ports, list) {
1030 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
1031 err = dsa_port_setup(dp);
1037 list_for_each_entry(dp, &dst->ports, list) {
1038 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
1039 err = dsa_port_setup(dp);
1041 err = dsa_port_reinit_as_unused(dp);
1051 dsa_tree_teardown_ports(dst);
1056 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
1058 struct dsa_port *dp;
1061 list_for_each_entry(dp, &dst->ports, list) {
1062 err = dsa_switch_setup(dp->ds);
1064 dsa_tree_teardown_switches(dst);
1072 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
1074 struct dsa_port *dp;
1079 list_for_each_entry(dp, &dst->ports, list) {
1080 if (dsa_port_is_cpu(dp)) {
1081 struct net_device *master = dp->master;
1082 bool admin_up = (master->flags & IFF_UP) &&
1083 !qdisc_tx_is_noop(master);
1085 err = dsa_master_setup(master, dp);
1089 /* Replay master state event */
1090 dsa_tree_master_admin_state_change(dst, master, admin_up);
1091 dsa_tree_master_oper_state_change(dst, master,
1092 netif_oper_up(master));
1101 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
1103 struct dsa_port *dp;
1107 list_for_each_entry(dp, &dst->ports, list) {
1108 if (dsa_port_is_cpu(dp)) {
1109 struct net_device *master = dp->master;
1111 /* Synthesizing an "admin down" state is sufficient for
1112 * the switches to get a notification if the master is
1113 * currently up and running.
1115 dsa_tree_master_admin_state_change(dst, master, false);
1117 dsa_master_teardown(master);
1124 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
1126 unsigned int len = 0;
1127 struct dsa_port *dp;
1129 list_for_each_entry(dp, &dst->ports, list) {
1130 if (dp->ds->num_lag_ids > len)
1131 len = dp->ds->num_lag_ids;
1137 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
1141 dst->lags_len = len;
1145 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
1150 static int dsa_tree_setup(struct dsa_switch_tree *dst)
1156 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
1161 complete = dsa_tree_setup_routing_table(dst);
1165 err = dsa_tree_setup_cpu_ports(dst);
1169 err = dsa_tree_setup_switches(dst);
1171 goto teardown_cpu_ports;
1173 err = dsa_tree_setup_master(dst);
1175 goto teardown_switches;
1177 err = dsa_tree_setup_ports(dst);
1179 goto teardown_master;
1181 err = dsa_tree_setup_lags(dst);
1183 goto teardown_ports;
1187 pr_info("DSA: tree %d setup\n", dst->index);
1192 dsa_tree_teardown_ports(dst);
1194 dsa_tree_teardown_master(dst);
1196 dsa_tree_teardown_switches(dst);
1198 dsa_tree_teardown_cpu_ports(dst);
1203 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
1205 struct dsa_link *dl, *next;
1210 dsa_tree_teardown_lags(dst);
1212 dsa_tree_teardown_ports(dst);
1214 dsa_tree_teardown_master(dst);
1216 dsa_tree_teardown_switches(dst);
1218 dsa_tree_teardown_cpu_ports(dst);
1220 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
1221 list_del(&dl->list);
1225 pr_info("DSA: tree %d torn down\n", dst->index);
1230 static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
1231 const struct dsa_device_ops *tag_ops)
1233 const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
1234 struct dsa_notifier_tag_proto_info info;
1237 dst->tag_ops = tag_ops;
1239 /* Notify the switches from this tree about the connection
1242 info.tag_ops = tag_ops;
1243 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
1244 if (err && err != -EOPNOTSUPP)
1245 goto out_disconnect;
1247 /* Notify the old tagger about the disconnection from this tree */
1248 info.tag_ops = old_tag_ops;
1249 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1254 info.tag_ops = tag_ops;
1255 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1256 dst->tag_ops = old_tag_ops;
1261 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
1262 * is that all DSA switches within a tree share the same tagger, otherwise
1263 * they would have formed disjoint trees (different "dsa,member" values).
1265 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
1266 struct net_device *master,
1267 const struct dsa_device_ops *tag_ops,
1268 const struct dsa_device_ops *old_tag_ops)
1270 struct dsa_notifier_tag_proto_info info;
1271 struct dsa_port *dp;
1274 if (!rtnl_trylock())
1275 return restart_syscall();
1277 /* At the moment we don't allow changing the tag protocol under
1278 * traffic. The rtnl_mutex also happens to serialize concurrent
1279 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1280 * restriction, there needs to be another mutex which serializes this.
1282 if (master->flags & IFF_UP)
1285 list_for_each_entry(dp, &dst->ports, list) {
1286 if (!dsa_port_is_user(dp))
1289 if (dp->slave->flags & IFF_UP)
1293 /* Notify the tag protocol change */
1294 info.tag_ops = tag_ops;
1295 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1299 err = dsa_tree_bind_tag_proto(dst, tag_ops);
1301 goto out_unwind_tagger;
1308 info.tag_ops = old_tag_ops;
1309 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1315 static void dsa_tree_master_state_change(struct dsa_switch_tree *dst,
1316 struct net_device *master)
1318 struct dsa_notifier_master_state_info info;
1319 struct dsa_port *cpu_dp = master->dsa_ptr;
1321 info.master = master;
1322 info.operational = dsa_port_master_is_operational(cpu_dp);
1324 dsa_tree_notify(dst, DSA_NOTIFIER_MASTER_STATE_CHANGE, &info);
1327 void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
1328 struct net_device *master,
1331 struct dsa_port *cpu_dp = master->dsa_ptr;
1332 bool notify = false;
1334 if ((dsa_port_master_is_operational(cpu_dp)) !=
1335 (up && cpu_dp->master_oper_up))
1338 cpu_dp->master_admin_up = up;
1341 dsa_tree_master_state_change(dst, master);
1344 void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
1345 struct net_device *master,
1348 struct dsa_port *cpu_dp = master->dsa_ptr;
1349 bool notify = false;
1351 if ((dsa_port_master_is_operational(cpu_dp)) !=
1352 (cpu_dp->master_admin_up && up))
1355 cpu_dp->master_oper_up = up;
1358 dsa_tree_master_state_change(dst, master);
1361 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1363 struct dsa_switch_tree *dst = ds->dst;
1364 struct dsa_port *dp;
1366 dsa_switch_for_each_port(dp, ds)
1367 if (dp->index == index)
1370 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1377 INIT_LIST_HEAD(&dp->list);
1378 list_add_tail(&dp->list, &dst->ports);
1383 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1388 dp->type = DSA_PORT_TYPE_USER;
1394 static int dsa_port_parse_dsa(struct dsa_port *dp)
1396 dp->type = DSA_PORT_TYPE_DSA;
1401 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1402 struct net_device *master)
1404 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1405 struct dsa_switch *mds, *ds = dp->ds;
1406 unsigned int mdp_upstream;
1407 struct dsa_port *mdp;
1409 /* It is possible to stack DSA switches onto one another when that
1410 * happens the switch driver may want to know if its tagging protocol
1411 * is going to work in such a configuration.
1413 if (dsa_slave_dev_check(master)) {
1414 mdp = dsa_slave_to_port(master);
1416 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1417 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1418 DSA_TAG_PROTO_NONE);
1421 /* If the master device is not itself a DSA slave in a disjoint DSA
1422 * tree, then return immediately.
1424 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1427 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1428 const char *user_protocol)
1430 struct dsa_switch *ds = dp->ds;
1431 struct dsa_switch_tree *dst = ds->dst;
1432 const struct dsa_device_ops *tag_ops;
1433 enum dsa_tag_protocol default_proto;
1435 /* Find out which protocol the switch would prefer. */
1436 default_proto = dsa_get_tag_protocol(dp, master);
1437 if (dst->default_proto) {
1438 if (dst->default_proto != default_proto) {
1440 "A DSA switch tree can have only one tagging protocol\n");
1444 dst->default_proto = default_proto;
1447 /* See if the user wants to override that preference. */
1448 if (user_protocol) {
1449 if (!ds->ops->change_tag_protocol) {
1450 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1454 tag_ops = dsa_find_tagger_by_name(user_protocol);
1456 tag_ops = dsa_tag_driver_get(default_proto);
1459 if (IS_ERR(tag_ops)) {
1460 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1461 return -EPROBE_DEFER;
1463 dev_warn(ds->dev, "No tagger for this switch\n");
1464 return PTR_ERR(tag_ops);
1468 if (dst->tag_ops != tag_ops) {
1470 "A DSA switch tree can have only one tagging protocol\n");
1472 dsa_tag_driver_put(tag_ops);
1476 /* In the case of multiple CPU ports per switch, the tagging
1477 * protocol is still reference-counted only per switch tree.
1479 dsa_tag_driver_put(tag_ops);
1481 dst->tag_ops = tag_ops;
1484 dp->master = master;
1485 dp->type = DSA_PORT_TYPE_CPU;
1486 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1489 /* At this point, the tree may be configured to use a different
1490 * tagger than the one chosen by the switch driver during
1491 * .setup, in the case when a user selects a custom protocol
1494 * This is resolved by syncing the driver with the tree in
1495 * dsa_switch_setup_tag_protocol once .setup has run and the
1496 * driver is ready to accept calls to .change_tag_protocol. If
1497 * the driver does not support the custom protocol at that
1498 * point, the tree is wholly rejected, thereby ensuring that the
1499 * tree and driver are always in agreement on the protocol to
1505 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1507 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1508 const char *name = of_get_property(dn, "label", NULL);
1509 bool link = of_property_read_bool(dn, "link");
1514 struct net_device *master;
1515 const char *user_protocol;
1517 master = of_find_net_device_by_node(ethernet);
1519 return -EPROBE_DEFER;
1521 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1522 return dsa_port_parse_cpu(dp, master, user_protocol);
1526 return dsa_port_parse_dsa(dp);
1528 return dsa_port_parse_user(dp, name);
1531 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1532 struct device_node *dn)
1534 struct device_node *ports, *port;
1535 struct dsa_port *dp;
1539 ports = of_get_child_by_name(dn, "ports");
1541 /* The second possibility is "ethernet-ports" */
1542 ports = of_get_child_by_name(dn, "ethernet-ports");
1544 dev_err(ds->dev, "no ports child node found\n");
1549 for_each_available_child_of_node(ports, port) {
1550 err = of_property_read_u32(port, "reg", ®);
1556 if (reg >= ds->num_ports) {
1557 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
1558 port, reg, ds->num_ports);
1564 dp = dsa_to_port(ds, reg);
1566 err = dsa_port_parse_of(dp, port);
1578 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1579 struct device_node *dn)
1581 u32 m[2] = { 0, 0 };
1584 /* Don't error out if this optional property isn't found */
1585 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1586 if (sz < 0 && sz != -EINVAL)
1591 ds->dst = dsa_tree_touch(m[0]);
1595 if (dsa_switch_find(ds->dst->index, ds->index)) {
1597 "A DSA switch with index %d already exists in tree %d\n",
1598 ds->index, ds->dst->index);
1602 if (ds->dst->last_switch < ds->index)
1603 ds->dst->last_switch = ds->index;
1608 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1610 struct dsa_port *dp;
1613 for (port = 0; port < ds->num_ports; port++) {
1614 dp = dsa_port_touch(ds, port);
1622 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1626 err = dsa_switch_parse_member_of(ds, dn);
1630 err = dsa_switch_touch_ports(ds);
1634 return dsa_switch_parse_ports_of(ds, dn);
1637 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1640 if (!strcmp(name, "cpu")) {
1641 struct net_device *master;
1643 master = dsa_dev_to_net_device(dev);
1645 return -EPROBE_DEFER;
1649 return dsa_port_parse_cpu(dp, master, NULL);
1652 if (!strcmp(name, "dsa"))
1653 return dsa_port_parse_dsa(dp);
1655 return dsa_port_parse_user(dp, name);
1658 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1659 struct dsa_chip_data *cd)
1661 bool valid_name_found = false;
1662 struct dsa_port *dp;
1668 for (i = 0; i < DSA_MAX_PORTS; i++) {
1669 name = cd->port_names[i];
1670 dev = cd->netdev[i];
1671 dp = dsa_to_port(ds, i);
1676 err = dsa_port_parse(dp, name, dev);
1680 valid_name_found = true;
1683 if (!valid_name_found && i == DSA_MAX_PORTS)
1689 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1695 /* We don't support interconnected switches nor multiple trees via
1696 * platform data, so this is the unique switch of the tree.
1699 ds->dst = dsa_tree_touch(0);
1703 err = dsa_switch_touch_ports(ds);
1707 return dsa_switch_parse_ports(ds, cd);
1710 static void dsa_switch_release_ports(struct dsa_switch *ds)
1712 struct dsa_port *dp, *next;
1714 dsa_switch_for_each_port_safe(dp, next, ds) {
1715 list_del(&dp->list);
1720 static int dsa_switch_probe(struct dsa_switch *ds)
1722 struct dsa_switch_tree *dst;
1723 struct dsa_chip_data *pdata;
1724 struct device_node *np;
1730 pdata = ds->dev->platform_data;
1731 np = ds->dev->of_node;
1737 err = dsa_switch_parse_of(ds, np);
1739 dsa_switch_release_ports(ds);
1741 err = dsa_switch_parse(ds, pdata);
1743 dsa_switch_release_ports(ds);
1753 err = dsa_tree_setup(dst);
1755 dsa_switch_release_ports(ds);
1762 int dsa_register_switch(struct dsa_switch *ds)
1766 mutex_lock(&dsa2_mutex);
1767 err = dsa_switch_probe(ds);
1768 dsa_tree_put(ds->dst);
1769 mutex_unlock(&dsa2_mutex);
1773 EXPORT_SYMBOL_GPL(dsa_register_switch);
1775 static void dsa_switch_remove(struct dsa_switch *ds)
1777 struct dsa_switch_tree *dst = ds->dst;
1779 dsa_tree_teardown(dst);
1780 dsa_switch_release_ports(ds);
1784 void dsa_unregister_switch(struct dsa_switch *ds)
1786 mutex_lock(&dsa2_mutex);
1787 dsa_switch_remove(ds);
1788 mutex_unlock(&dsa2_mutex);
1790 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1792 /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1793 * blocking that operation from completion, due to the dev_hold taken inside
1794 * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1795 * the DSA master, so that the system can reboot successfully.
1797 void dsa_switch_shutdown(struct dsa_switch *ds)
1799 struct net_device *master, *slave_dev;
1800 struct dsa_port *dp;
1802 mutex_lock(&dsa2_mutex);
1805 dsa_switch_for_each_user_port(dp, ds) {
1806 master = dp->cpu_dp->master;
1807 slave_dev = dp->slave;
1809 netdev_upper_dev_unlink(master, slave_dev);
1812 /* Disconnect from further netdevice notifiers on the master,
1813 * since netdev_uses_dsa() will now return false.
1815 dsa_switch_for_each_cpu_port(dp, ds)
1816 dp->master->dsa_ptr = NULL;
1819 mutex_unlock(&dsa2_mutex);
1821 EXPORT_SYMBOL_GPL(dsa_switch_shutdown);