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_mdio.h>
17 #include <linux/of_net.h>
18 #include <net/devlink.h>
19 #include <net/sch_generic.h>
23 static DEFINE_MUTEX(dsa2_mutex);
24 LIST_HEAD(dsa_tree_list);
26 /* Track the bridges with forwarding offload enabled */
27 static unsigned long dsa_fwd_offloading_bridges;
30 * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
31 * @dst: collection of struct dsa_switch devices to notify.
32 * @e: event, must be of type DSA_NOTIFIER_*
33 * @v: event-specific value.
35 * Given a struct dsa_switch_tree, this can be used to run a function once for
36 * each member DSA switch. The other alternative of traversing the tree is only
37 * through its ports list, which does not uniquely list the switches.
39 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
41 struct raw_notifier_head *nh = &dst->nh;
44 err = raw_notifier_call_chain(nh, e, v);
46 return notifier_to_errno(err);
50 * dsa_broadcast - Notify all DSA trees in the system.
51 * @e: event, must be of type DSA_NOTIFIER_*
52 * @v: event-specific value.
54 * Can be used to notify the switching fabric of events such as cross-chip
55 * bridging between disjoint trees (such as islands of tagger-compatible
56 * switches bridged by an incompatible middle switch).
58 * WARNING: this function is not reliable during probe time, because probing
59 * between trees is asynchronous and not all DSA trees might have probed.
61 int dsa_broadcast(unsigned long e, void *v)
63 struct dsa_switch_tree *dst;
66 list_for_each_entry(dst, &dsa_tree_list, list) {
67 err = dsa_tree_notify(dst, e, v);
76 * dsa_lag_map() - Map LAG structure to a linear LAG array
77 * @dst: Tree in which to record the mapping.
78 * @lag: LAG structure that is to be mapped to the tree's array.
80 * dsa_lag_id/dsa_lag_by_id can then be used to translate between the
81 * two spaces. The size of the mapping space is determined by the
82 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
83 * it unset if it is not needed, in which case these functions become
86 void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag)
90 for (id = 1; id <= dst->lags_len; id++) {
91 if (!dsa_lag_by_id(dst, id)) {
92 dst->lags[id - 1] = lag;
98 /* No IDs left, which is OK. Some drivers do not need it. The
99 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
100 * returns an error for this device when joining the LAG. The
101 * driver can then return -EOPNOTSUPP back to DSA, which will
102 * fall back to a software LAG.
107 * dsa_lag_unmap() - Remove a LAG ID mapping
108 * @dst: Tree in which the mapping is recorded.
109 * @lag: LAG structure that was mapped.
111 * As there may be multiple users of the mapping, it is only removed
112 * if there are no other references to it.
114 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag)
118 dsa_lags_foreach_id(id, dst) {
119 if (dsa_lag_by_id(dst, id) == lag) {
120 dst->lags[id - 1] = NULL;
127 struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
128 const struct net_device *lag_dev)
132 list_for_each_entry(dp, &dst->ports, list)
133 if (dsa_port_lag_dev_get(dp) == lag_dev)
139 struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
140 const struct net_device *br)
144 list_for_each_entry(dp, &dst->ports, list)
145 if (dsa_port_bridge_dev_get(dp) == br)
151 static int dsa_bridge_num_find(const struct net_device *bridge_dev)
153 struct dsa_switch_tree *dst;
155 list_for_each_entry(dst, &dsa_tree_list, list) {
156 struct dsa_bridge *bridge;
158 bridge = dsa_tree_bridge_find(dst, bridge_dev);
166 unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
168 unsigned int bridge_num = dsa_bridge_num_find(bridge_dev);
170 /* Switches without FDB isolation support don't get unique
177 /* First port that requests FDB isolation or TX forwarding
178 * offload for this bridge
180 bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges,
181 DSA_MAX_NUM_OFFLOADING_BRIDGES,
183 if (bridge_num >= max)
186 set_bit(bridge_num, &dsa_fwd_offloading_bridges);
192 void dsa_bridge_num_put(const struct net_device *bridge_dev,
193 unsigned int bridge_num)
195 /* Since we refcount bridges, we know that when we call this function
196 * it is no longer in use, so we can just go ahead and remove it from
199 clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
202 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
204 struct dsa_switch_tree *dst;
207 list_for_each_entry(dst, &dsa_tree_list, list) {
208 if (dst->index != tree_index)
211 list_for_each_entry(dp, &dst->ports, list) {
212 if (dp->ds->index != sw_index)
221 EXPORT_SYMBOL_GPL(dsa_switch_find);
223 static struct dsa_switch_tree *dsa_tree_find(int index)
225 struct dsa_switch_tree *dst;
227 list_for_each_entry(dst, &dsa_tree_list, list)
228 if (dst->index == index)
234 static struct dsa_switch_tree *dsa_tree_alloc(int index)
236 struct dsa_switch_tree *dst;
238 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
244 INIT_LIST_HEAD(&dst->rtable);
246 INIT_LIST_HEAD(&dst->ports);
248 INIT_LIST_HEAD(&dst->list);
249 list_add_tail(&dst->list, &dsa_tree_list);
251 kref_init(&dst->refcount);
256 static void dsa_tree_free(struct dsa_switch_tree *dst)
259 dsa_tag_driver_put(dst->tag_ops);
260 list_del(&dst->list);
264 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
267 kref_get(&dst->refcount);
272 static struct dsa_switch_tree *dsa_tree_touch(int index)
274 struct dsa_switch_tree *dst;
276 dst = dsa_tree_find(index);
278 return dsa_tree_get(dst);
280 return dsa_tree_alloc(index);
283 static void dsa_tree_release(struct kref *ref)
285 struct dsa_switch_tree *dst;
287 dst = container_of(ref, struct dsa_switch_tree, refcount);
292 static void dsa_tree_put(struct dsa_switch_tree *dst)
295 kref_put(&dst->refcount, dsa_tree_release);
298 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
299 struct device_node *dn)
303 list_for_each_entry(dp, &dst->ports, list)
310 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
311 struct dsa_port *link_dp)
313 struct dsa_switch *ds = dp->ds;
314 struct dsa_switch_tree *dst;
319 list_for_each_entry(dl, &dst->rtable, list)
320 if (dl->dp == dp && dl->link_dp == link_dp)
323 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
328 dl->link_dp = link_dp;
330 INIT_LIST_HEAD(&dl->list);
331 list_add_tail(&dl->list, &dst->rtable);
336 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
338 struct dsa_switch *ds = dp->ds;
339 struct dsa_switch_tree *dst = ds->dst;
340 struct device_node *dn = dp->dn;
341 struct of_phandle_iterator it;
342 struct dsa_port *link_dp;
346 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
347 link_dp = dsa_tree_find_port_by_node(dst, it.node);
349 of_node_put(it.node);
353 dl = dsa_link_touch(dp, link_dp);
355 of_node_put(it.node);
363 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
365 bool complete = true;
368 list_for_each_entry(dp, &dst->ports, list) {
369 if (dsa_port_is_dsa(dp)) {
370 complete = dsa_port_setup_routing_table(dp);
379 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
383 list_for_each_entry(dp, &dst->ports, list)
384 if (dsa_port_is_cpu(dp))
390 /* Assign the default CPU port (the first one in the tree) to all ports of the
391 * fabric which don't already have one as part of their own switch.
393 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
395 struct dsa_port *cpu_dp, *dp;
397 cpu_dp = dsa_tree_find_first_cpu(dst);
399 pr_err("DSA: tree %d has no CPU port\n", dst->index);
403 list_for_each_entry(dp, &dst->ports, list) {
407 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
414 /* Perform initial assignment of CPU ports to user ports and DSA links in the
415 * fabric, giving preference to CPU ports local to each switch. Default to
416 * using the first CPU port in the switch tree if the port does not have a CPU
417 * port local to this switch.
419 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
421 struct dsa_port *cpu_dp, *dp;
423 list_for_each_entry(cpu_dp, &dst->ports, list) {
424 if (!dsa_port_is_cpu(cpu_dp))
427 /* Prefer a local CPU port */
428 dsa_switch_for_each_port(dp, cpu_dp->ds) {
429 /* Prefer the first local CPU port found */
433 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
438 return dsa_tree_setup_default_cpu(dst);
441 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
445 list_for_each_entry(dp, &dst->ports, list)
446 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
450 static int dsa_port_setup(struct dsa_port *dp)
452 struct devlink_port *dlp = &dp->devlink_port;
453 bool dsa_port_link_registered = false;
454 struct dsa_switch *ds = dp->ds;
455 bool dsa_port_enabled = false;
461 if (ds->ops->port_setup) {
462 err = ds->ops->port_setup(ds, dp->index);
468 case DSA_PORT_TYPE_UNUSED:
469 dsa_port_disable(dp);
471 case DSA_PORT_TYPE_CPU:
472 err = dsa_port_link_register_of(dp);
475 dsa_port_link_registered = true;
477 err = dsa_port_enable(dp, NULL);
480 dsa_port_enabled = true;
483 case DSA_PORT_TYPE_DSA:
484 err = dsa_port_link_register_of(dp);
487 dsa_port_link_registered = true;
489 err = dsa_port_enable(dp, NULL);
492 dsa_port_enabled = true;
495 case DSA_PORT_TYPE_USER:
496 of_get_mac_address(dp->dn, dp->mac);
497 err = dsa_slave_create(dp);
501 devlink_port_type_eth_set(dlp, dp->slave);
505 if (err && dsa_port_enabled)
506 dsa_port_disable(dp);
507 if (err && dsa_port_link_registered)
508 dsa_port_link_unregister_of(dp);
510 if (ds->ops->port_teardown)
511 ds->ops->port_teardown(ds, dp->index);
520 static int dsa_port_devlink_setup(struct dsa_port *dp)
522 struct devlink_port *dlp = &dp->devlink_port;
523 struct dsa_switch_tree *dst = dp->ds->dst;
524 struct devlink_port_attrs attrs = {};
525 struct devlink *dl = dp->ds->devlink;
526 const unsigned char *id;
530 id = (const unsigned char *)&dst->index;
531 len = sizeof(dst->index);
533 attrs.phys.port_number = dp->index;
534 memcpy(attrs.switch_id.id, id, len);
535 attrs.switch_id.id_len = len;
536 memset(dlp, 0, sizeof(*dlp));
539 case DSA_PORT_TYPE_UNUSED:
540 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
542 case DSA_PORT_TYPE_CPU:
543 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
545 case DSA_PORT_TYPE_DSA:
546 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
548 case DSA_PORT_TYPE_USER:
549 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
553 devlink_port_attrs_set(dlp, &attrs);
554 err = devlink_port_register(dl, dlp, dp->index);
557 dp->devlink_port_setup = true;
562 static void dsa_port_teardown(struct dsa_port *dp)
564 struct devlink_port *dlp = &dp->devlink_port;
565 struct dsa_switch *ds = dp->ds;
570 if (ds->ops->port_teardown)
571 ds->ops->port_teardown(ds, dp->index);
573 devlink_port_type_clear(dlp);
576 case DSA_PORT_TYPE_UNUSED:
578 case DSA_PORT_TYPE_CPU:
579 dsa_port_disable(dp);
580 dsa_port_link_unregister_of(dp);
582 case DSA_PORT_TYPE_DSA:
583 dsa_port_disable(dp);
584 dsa_port_link_unregister_of(dp);
586 case DSA_PORT_TYPE_USER:
588 dsa_slave_destroy(dp->slave);
597 static void dsa_port_devlink_teardown(struct dsa_port *dp)
599 struct devlink_port *dlp = &dp->devlink_port;
601 if (dp->devlink_port_setup)
602 devlink_port_unregister(dlp);
603 dp->devlink_port_setup = false;
606 /* Destroy the current devlink port, and create a new one which has the UNUSED
607 * flavour. At this point, any call to ds->ops->port_setup has been already
608 * balanced out by a call to ds->ops->port_teardown, so we know that any
609 * devlink port regions the driver had are now unregistered. We then call its
610 * ds->ops->port_setup again, in order for the driver to re-create them on the
613 static int dsa_port_reinit_as_unused(struct dsa_port *dp)
615 struct dsa_switch *ds = dp->ds;
618 dsa_port_devlink_teardown(dp);
619 dp->type = DSA_PORT_TYPE_UNUSED;
620 err = dsa_port_devlink_setup(dp);
624 if (ds->ops->port_setup) {
625 /* On error, leave the devlink port registered,
626 * dsa_switch_teardown will clean it up later.
628 err = ds->ops->port_setup(ds, dp->index);
636 static int dsa_devlink_info_get(struct devlink *dl,
637 struct devlink_info_req *req,
638 struct netlink_ext_ack *extack)
640 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
642 if (ds->ops->devlink_info_get)
643 return ds->ops->devlink_info_get(ds, req, extack);
648 static int dsa_devlink_sb_pool_get(struct devlink *dl,
649 unsigned int sb_index, u16 pool_index,
650 struct devlink_sb_pool_info *pool_info)
652 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
654 if (!ds->ops->devlink_sb_pool_get)
657 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
661 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
662 u16 pool_index, u32 size,
663 enum devlink_sb_threshold_type threshold_type,
664 struct netlink_ext_ack *extack)
666 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
668 if (!ds->ops->devlink_sb_pool_set)
671 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
672 threshold_type, extack);
675 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
676 unsigned int sb_index, u16 pool_index,
679 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
680 int port = dsa_devlink_port_to_port(dlp);
682 if (!ds->ops->devlink_sb_port_pool_get)
685 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
686 pool_index, p_threshold);
689 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
690 unsigned int sb_index, u16 pool_index,
692 struct netlink_ext_ack *extack)
694 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
695 int port = dsa_devlink_port_to_port(dlp);
697 if (!ds->ops->devlink_sb_port_pool_set)
700 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
701 pool_index, threshold, extack);
705 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
706 unsigned int sb_index, u16 tc_index,
707 enum devlink_sb_pool_type pool_type,
708 u16 *p_pool_index, u32 *p_threshold)
710 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
711 int port = dsa_devlink_port_to_port(dlp);
713 if (!ds->ops->devlink_sb_tc_pool_bind_get)
716 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
718 p_pool_index, p_threshold);
722 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
723 unsigned int sb_index, u16 tc_index,
724 enum devlink_sb_pool_type pool_type,
725 u16 pool_index, u32 threshold,
726 struct netlink_ext_ack *extack)
728 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
729 int port = dsa_devlink_port_to_port(dlp);
731 if (!ds->ops->devlink_sb_tc_pool_bind_set)
734 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
736 pool_index, threshold,
740 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
741 unsigned int sb_index)
743 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
745 if (!ds->ops->devlink_sb_occ_snapshot)
748 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
751 static int dsa_devlink_sb_occ_max_clear(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_max_clear)
759 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
762 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
763 unsigned int sb_index,
764 u16 pool_index, u32 *p_cur,
767 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
768 int port = dsa_devlink_port_to_port(dlp);
770 if (!ds->ops->devlink_sb_occ_port_pool_get)
773 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
774 pool_index, p_cur, p_max);
778 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
779 unsigned int sb_index, u16 tc_index,
780 enum devlink_sb_pool_type pool_type,
781 u32 *p_cur, u32 *p_max)
783 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
784 int port = dsa_devlink_port_to_port(dlp);
786 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
789 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
795 static const struct devlink_ops dsa_devlink_ops = {
796 .info_get = dsa_devlink_info_get,
797 .sb_pool_get = dsa_devlink_sb_pool_get,
798 .sb_pool_set = dsa_devlink_sb_pool_set,
799 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
800 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
801 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
802 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
803 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
804 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
805 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
806 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
809 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
811 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
812 struct dsa_switch_tree *dst = ds->dst;
815 if (tag_ops->proto == dst->default_proto)
819 err = ds->ops->change_tag_protocol(ds, tag_ops->proto);
822 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
823 tag_ops->name, ERR_PTR(err));
828 if (tag_ops->connect) {
829 err = tag_ops->connect(ds);
834 if (ds->ops->connect_tag_protocol) {
835 err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
838 "Unable to connect to tag protocol \"%s\": %pe\n",
839 tag_ops->name, ERR_PTR(err));
847 if (tag_ops->disconnect)
848 tag_ops->disconnect(ds);
853 static int dsa_switch_setup(struct dsa_switch *ds)
855 struct dsa_devlink_priv *dl_priv;
856 struct device_node *dn;
863 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
864 * driver and before ops->setup() has run, since the switch drivers and
865 * the slave MDIO bus driver rely on these values for probing PHY
868 ds->phys_mii_mask |= dsa_user_ports(ds);
870 /* Add the switch to devlink before calling setup, so that setup can
874 devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
877 dl_priv = devlink_priv(ds->devlink);
880 /* Setup devlink port instances now, so that the switch
881 * setup() can register regions etc, against the ports
883 dsa_switch_for_each_port(dp, ds) {
884 err = dsa_port_devlink_setup(dp);
886 goto unregister_devlink_ports;
889 err = dsa_switch_register_notifier(ds);
891 goto unregister_devlink_ports;
893 ds->configure_vlan_while_not_filtering = true;
895 err = ds->ops->setup(ds);
897 goto unregister_notifier;
899 err = dsa_switch_setup_tag_protocol(ds);
903 if (!ds->slave_mii_bus && ds->ops->phy_read) {
904 ds->slave_mii_bus = mdiobus_alloc();
905 if (!ds->slave_mii_bus) {
910 dsa_slave_mii_bus_init(ds);
912 dn = of_get_child_by_name(ds->dev->of_node, "mdio");
914 err = of_mdiobus_register(ds->slave_mii_bus, dn);
917 goto free_slave_mii_bus;
921 devlink_register(ds->devlink);
925 if (ds->slave_mii_bus && ds->ops->phy_read)
926 mdiobus_free(ds->slave_mii_bus);
928 if (ds->ops->teardown)
929 ds->ops->teardown(ds);
931 dsa_switch_unregister_notifier(ds);
932 unregister_devlink_ports:
933 dsa_switch_for_each_port(dp, ds)
934 dsa_port_devlink_teardown(dp);
935 devlink_free(ds->devlink);
940 static void dsa_switch_teardown(struct dsa_switch *ds)
948 devlink_unregister(ds->devlink);
950 if (ds->slave_mii_bus && ds->ops->phy_read) {
951 mdiobus_unregister(ds->slave_mii_bus);
952 mdiobus_free(ds->slave_mii_bus);
953 ds->slave_mii_bus = NULL;
956 if (ds->ops->teardown)
957 ds->ops->teardown(ds);
959 dsa_switch_unregister_notifier(ds);
962 dsa_switch_for_each_port(dp, ds)
963 dsa_port_devlink_teardown(dp);
964 devlink_free(ds->devlink);
971 /* First tear down the non-shared, then the shared ports. This ensures that
972 * all work items scheduled by our switchdev handlers for user ports have
973 * completed before we destroy the refcounting kept on the shared ports.
975 static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
979 list_for_each_entry(dp, &dst->ports, list)
980 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
981 dsa_port_teardown(dp);
983 dsa_flush_workqueue();
985 list_for_each_entry(dp, &dst->ports, list)
986 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
987 dsa_port_teardown(dp);
990 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
994 list_for_each_entry(dp, &dst->ports, list)
995 dsa_switch_teardown(dp->ds);
998 /* Bring shared ports up first, then non-shared ports */
999 static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
1001 struct dsa_port *dp;
1004 list_for_each_entry(dp, &dst->ports, list) {
1005 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
1006 err = dsa_port_setup(dp);
1012 list_for_each_entry(dp, &dst->ports, list) {
1013 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
1014 err = dsa_port_setup(dp);
1016 err = dsa_port_reinit_as_unused(dp);
1026 dsa_tree_teardown_ports(dst);
1031 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
1033 struct dsa_port *dp;
1036 list_for_each_entry(dp, &dst->ports, list) {
1037 err = dsa_switch_setup(dp->ds);
1039 dsa_tree_teardown_switches(dst);
1047 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
1049 struct dsa_port *dp;
1054 list_for_each_entry(dp, &dst->ports, list) {
1055 if (dsa_port_is_cpu(dp)) {
1056 struct net_device *master = dp->master;
1057 bool admin_up = (master->flags & IFF_UP) &&
1058 !qdisc_tx_is_noop(master);
1060 err = dsa_master_setup(master, dp);
1064 /* Replay master state event */
1065 dsa_tree_master_admin_state_change(dst, master, admin_up);
1066 dsa_tree_master_oper_state_change(dst, master,
1067 netif_oper_up(master));
1076 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
1078 struct dsa_port *dp;
1082 list_for_each_entry(dp, &dst->ports, list) {
1083 if (dsa_port_is_cpu(dp)) {
1084 struct net_device *master = dp->master;
1086 /* Synthesizing an "admin down" state is sufficient for
1087 * the switches to get a notification if the master is
1088 * currently up and running.
1090 dsa_tree_master_admin_state_change(dst, master, false);
1092 dsa_master_teardown(master);
1099 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
1101 unsigned int len = 0;
1102 struct dsa_port *dp;
1104 list_for_each_entry(dp, &dst->ports, list) {
1105 if (dp->ds->num_lag_ids > len)
1106 len = dp->ds->num_lag_ids;
1112 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
1116 dst->lags_len = len;
1120 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
1125 static int dsa_tree_setup(struct dsa_switch_tree *dst)
1131 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
1136 complete = dsa_tree_setup_routing_table(dst);
1140 err = dsa_tree_setup_cpu_ports(dst);
1144 err = dsa_tree_setup_switches(dst);
1146 goto teardown_cpu_ports;
1148 err = dsa_tree_setup_ports(dst);
1150 goto teardown_switches;
1152 err = dsa_tree_setup_master(dst);
1154 goto teardown_ports;
1156 err = dsa_tree_setup_lags(dst);
1158 goto teardown_master;
1162 pr_info("DSA: tree %d setup\n", dst->index);
1167 dsa_tree_teardown_master(dst);
1169 dsa_tree_teardown_ports(dst);
1171 dsa_tree_teardown_switches(dst);
1173 dsa_tree_teardown_cpu_ports(dst);
1178 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
1180 struct dsa_link *dl, *next;
1185 dsa_tree_teardown_lags(dst);
1187 dsa_tree_teardown_master(dst);
1189 dsa_tree_teardown_ports(dst);
1191 dsa_tree_teardown_switches(dst);
1193 dsa_tree_teardown_cpu_ports(dst);
1195 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
1196 list_del(&dl->list);
1200 pr_info("DSA: tree %d torn down\n", dst->index);
1205 static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
1206 const struct dsa_device_ops *tag_ops)
1208 const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
1209 struct dsa_notifier_tag_proto_info info;
1212 dst->tag_ops = tag_ops;
1214 /* Notify the switches from this tree about the connection
1217 info.tag_ops = tag_ops;
1218 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
1219 if (err && err != -EOPNOTSUPP)
1220 goto out_disconnect;
1222 /* Notify the old tagger about the disconnection from this tree */
1223 info.tag_ops = old_tag_ops;
1224 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1229 info.tag_ops = tag_ops;
1230 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1231 dst->tag_ops = old_tag_ops;
1236 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
1237 * is that all DSA switches within a tree share the same tagger, otherwise
1238 * they would have formed disjoint trees (different "dsa,member" values).
1240 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
1241 struct net_device *master,
1242 const struct dsa_device_ops *tag_ops,
1243 const struct dsa_device_ops *old_tag_ops)
1245 struct dsa_notifier_tag_proto_info info;
1246 struct dsa_port *dp;
1249 if (!rtnl_trylock())
1250 return restart_syscall();
1252 /* At the moment we don't allow changing the tag protocol under
1253 * traffic. The rtnl_mutex also happens to serialize concurrent
1254 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1255 * restriction, there needs to be another mutex which serializes this.
1257 if (master->flags & IFF_UP)
1260 list_for_each_entry(dp, &dst->ports, list) {
1261 if (!dsa_port_is_user(dp))
1264 if (dp->slave->flags & IFF_UP)
1268 /* Notify the tag protocol change */
1269 info.tag_ops = tag_ops;
1270 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1272 goto out_unwind_tagger;
1274 err = dsa_tree_bind_tag_proto(dst, tag_ops);
1276 goto out_unwind_tagger;
1283 info.tag_ops = old_tag_ops;
1284 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1290 static void dsa_tree_master_state_change(struct dsa_switch_tree *dst,
1291 struct net_device *master)
1293 struct dsa_notifier_master_state_info info;
1294 struct dsa_port *cpu_dp = master->dsa_ptr;
1296 info.master = master;
1297 info.operational = dsa_port_master_is_operational(cpu_dp);
1299 dsa_tree_notify(dst, DSA_NOTIFIER_MASTER_STATE_CHANGE, &info);
1302 void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
1303 struct net_device *master,
1306 struct dsa_port *cpu_dp = master->dsa_ptr;
1307 bool notify = false;
1309 if ((dsa_port_master_is_operational(cpu_dp)) !=
1310 (up && cpu_dp->master_oper_up))
1313 cpu_dp->master_admin_up = up;
1316 dsa_tree_master_state_change(dst, master);
1319 void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
1320 struct net_device *master,
1323 struct dsa_port *cpu_dp = master->dsa_ptr;
1324 bool notify = false;
1326 if ((dsa_port_master_is_operational(cpu_dp)) !=
1327 (cpu_dp->master_admin_up && up))
1330 cpu_dp->master_oper_up = up;
1333 dsa_tree_master_state_change(dst, master);
1336 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1338 struct dsa_switch_tree *dst = ds->dst;
1339 struct dsa_port *dp;
1341 dsa_switch_for_each_port(dp, ds)
1342 if (dp->index == index)
1345 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1352 mutex_init(&dp->addr_lists_lock);
1353 mutex_init(&dp->vlans_lock);
1354 INIT_LIST_HEAD(&dp->fdbs);
1355 INIT_LIST_HEAD(&dp->mdbs);
1356 INIT_LIST_HEAD(&dp->vlans);
1357 INIT_LIST_HEAD(&dp->list);
1358 list_add_tail(&dp->list, &dst->ports);
1363 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1368 dp->type = DSA_PORT_TYPE_USER;
1374 static int dsa_port_parse_dsa(struct dsa_port *dp)
1376 dp->type = DSA_PORT_TYPE_DSA;
1381 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1382 struct net_device *master)
1384 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1385 struct dsa_switch *mds, *ds = dp->ds;
1386 unsigned int mdp_upstream;
1387 struct dsa_port *mdp;
1389 /* It is possible to stack DSA switches onto one another when that
1390 * happens the switch driver may want to know if its tagging protocol
1391 * is going to work in such a configuration.
1393 if (dsa_slave_dev_check(master)) {
1394 mdp = dsa_slave_to_port(master);
1396 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1397 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1398 DSA_TAG_PROTO_NONE);
1401 /* If the master device is not itself a DSA slave in a disjoint DSA
1402 * tree, then return immediately.
1404 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1407 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1408 const char *user_protocol)
1410 struct dsa_switch *ds = dp->ds;
1411 struct dsa_switch_tree *dst = ds->dst;
1412 const struct dsa_device_ops *tag_ops;
1413 enum dsa_tag_protocol default_proto;
1415 /* Find out which protocol the switch would prefer. */
1416 default_proto = dsa_get_tag_protocol(dp, master);
1417 if (dst->default_proto) {
1418 if (dst->default_proto != default_proto) {
1420 "A DSA switch tree can have only one tagging protocol\n");
1424 dst->default_proto = default_proto;
1427 /* See if the user wants to override that preference. */
1428 if (user_protocol) {
1429 if (!ds->ops->change_tag_protocol) {
1430 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1434 tag_ops = dsa_find_tagger_by_name(user_protocol);
1436 tag_ops = dsa_tag_driver_get(default_proto);
1439 if (IS_ERR(tag_ops)) {
1440 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1441 return -EPROBE_DEFER;
1443 dev_warn(ds->dev, "No tagger for this switch\n");
1444 return PTR_ERR(tag_ops);
1448 if (dst->tag_ops != tag_ops) {
1450 "A DSA switch tree can have only one tagging protocol\n");
1452 dsa_tag_driver_put(tag_ops);
1456 /* In the case of multiple CPU ports per switch, the tagging
1457 * protocol is still reference-counted only per switch tree.
1459 dsa_tag_driver_put(tag_ops);
1461 dst->tag_ops = tag_ops;
1464 dp->master = master;
1465 dp->type = DSA_PORT_TYPE_CPU;
1466 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1469 /* At this point, the tree may be configured to use a different
1470 * tagger than the one chosen by the switch driver during
1471 * .setup, in the case when a user selects a custom protocol
1474 * This is resolved by syncing the driver with the tree in
1475 * dsa_switch_setup_tag_protocol once .setup has run and the
1476 * driver is ready to accept calls to .change_tag_protocol. If
1477 * the driver does not support the custom protocol at that
1478 * point, the tree is wholly rejected, thereby ensuring that the
1479 * tree and driver are always in agreement on the protocol to
1485 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1487 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1488 const char *name = of_get_property(dn, "label", NULL);
1489 bool link = of_property_read_bool(dn, "link");
1494 struct net_device *master;
1495 const char *user_protocol;
1497 master = of_find_net_device_by_node(ethernet);
1498 of_node_put(ethernet);
1500 return -EPROBE_DEFER;
1502 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1503 return dsa_port_parse_cpu(dp, master, user_protocol);
1507 return dsa_port_parse_dsa(dp);
1509 return dsa_port_parse_user(dp, name);
1512 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1513 struct device_node *dn)
1515 struct device_node *ports, *port;
1516 struct dsa_port *dp;
1520 ports = of_get_child_by_name(dn, "ports");
1522 /* The second possibility is "ethernet-ports" */
1523 ports = of_get_child_by_name(dn, "ethernet-ports");
1525 dev_err(ds->dev, "no ports child node found\n");
1530 for_each_available_child_of_node(ports, port) {
1531 err = of_property_read_u32(port, "reg", ®);
1537 if (reg >= ds->num_ports) {
1538 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
1539 port, reg, ds->num_ports);
1545 dp = dsa_to_port(ds, reg);
1547 err = dsa_port_parse_of(dp, port);
1559 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1560 struct device_node *dn)
1562 u32 m[2] = { 0, 0 };
1565 /* Don't error out if this optional property isn't found */
1566 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1567 if (sz < 0 && sz != -EINVAL)
1572 ds->dst = dsa_tree_touch(m[0]);
1576 if (dsa_switch_find(ds->dst->index, ds->index)) {
1578 "A DSA switch with index %d already exists in tree %d\n",
1579 ds->index, ds->dst->index);
1583 if (ds->dst->last_switch < ds->index)
1584 ds->dst->last_switch = ds->index;
1589 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1591 struct dsa_port *dp;
1594 for (port = 0; port < ds->num_ports; port++) {
1595 dp = dsa_port_touch(ds, port);
1603 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1607 err = dsa_switch_parse_member_of(ds, dn);
1611 err = dsa_switch_touch_ports(ds);
1615 return dsa_switch_parse_ports_of(ds, dn);
1618 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1621 if (!strcmp(name, "cpu")) {
1622 struct net_device *master;
1624 master = dsa_dev_to_net_device(dev);
1626 return -EPROBE_DEFER;
1630 return dsa_port_parse_cpu(dp, master, NULL);
1633 if (!strcmp(name, "dsa"))
1634 return dsa_port_parse_dsa(dp);
1636 return dsa_port_parse_user(dp, name);
1639 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1640 struct dsa_chip_data *cd)
1642 bool valid_name_found = false;
1643 struct dsa_port *dp;
1649 for (i = 0; i < DSA_MAX_PORTS; i++) {
1650 name = cd->port_names[i];
1651 dev = cd->netdev[i];
1652 dp = dsa_to_port(ds, i);
1657 err = dsa_port_parse(dp, name, dev);
1661 valid_name_found = true;
1664 if (!valid_name_found && i == DSA_MAX_PORTS)
1670 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1676 /* We don't support interconnected switches nor multiple trees via
1677 * platform data, so this is the unique switch of the tree.
1680 ds->dst = dsa_tree_touch(0);
1684 err = dsa_switch_touch_ports(ds);
1688 return dsa_switch_parse_ports(ds, cd);
1691 static void dsa_switch_release_ports(struct dsa_switch *ds)
1693 struct dsa_port *dp, *next;
1695 dsa_switch_for_each_port_safe(dp, next, ds) {
1696 WARN_ON(!list_empty(&dp->fdbs));
1697 WARN_ON(!list_empty(&dp->mdbs));
1698 WARN_ON(!list_empty(&dp->vlans));
1699 list_del(&dp->list);
1704 static int dsa_switch_probe(struct dsa_switch *ds)
1706 struct dsa_switch_tree *dst;
1707 struct dsa_chip_data *pdata;
1708 struct device_node *np;
1714 pdata = ds->dev->platform_data;
1715 np = ds->dev->of_node;
1721 err = dsa_switch_parse_of(ds, np);
1723 dsa_switch_release_ports(ds);
1725 err = dsa_switch_parse(ds, pdata);
1727 dsa_switch_release_ports(ds);
1737 err = dsa_tree_setup(dst);
1739 dsa_switch_release_ports(ds);
1746 int dsa_register_switch(struct dsa_switch *ds)
1750 mutex_lock(&dsa2_mutex);
1751 err = dsa_switch_probe(ds);
1752 dsa_tree_put(ds->dst);
1753 mutex_unlock(&dsa2_mutex);
1757 EXPORT_SYMBOL_GPL(dsa_register_switch);
1759 static void dsa_switch_remove(struct dsa_switch *ds)
1761 struct dsa_switch_tree *dst = ds->dst;
1763 dsa_tree_teardown(dst);
1764 dsa_switch_release_ports(ds);
1768 void dsa_unregister_switch(struct dsa_switch *ds)
1770 mutex_lock(&dsa2_mutex);
1771 dsa_switch_remove(ds);
1772 mutex_unlock(&dsa2_mutex);
1774 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1776 /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1777 * blocking that operation from completion, due to the dev_hold taken inside
1778 * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1779 * the DSA master, so that the system can reboot successfully.
1781 void dsa_switch_shutdown(struct dsa_switch *ds)
1783 struct net_device *master, *slave_dev;
1784 struct dsa_port *dp;
1786 mutex_lock(&dsa2_mutex);
1793 dsa_switch_for_each_user_port(dp, ds) {
1794 master = dp->cpu_dp->master;
1795 slave_dev = dp->slave;
1797 netdev_upper_dev_unlink(master, slave_dev);
1800 /* Disconnect from further netdevice notifiers on the master,
1801 * since netdev_uses_dsa() will now return false.
1803 dsa_switch_for_each_cpu_port(dp, ds)
1804 dp->master->dsa_ptr = NULL;
1808 mutex_unlock(&dsa2_mutex);
1810 EXPORT_SYMBOL_GPL(dsa_switch_shutdown);