1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DSA topology and switch handling
5 * Copyright (c) 2008-2009 Marvell Semiconductor
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/slab.h>
16 #include <linux/rtnetlink.h>
18 #include <linux/of_mdio.h>
19 #include <linux/of_net.h>
20 #include <net/dsa_stubs.h>
21 #include <net/sch_generic.h>
32 #define DSA_MAX_NUM_OFFLOADING_BRIDGES BITS_PER_LONG
34 static DEFINE_MUTEX(dsa2_mutex);
35 LIST_HEAD(dsa_tree_list);
37 static struct workqueue_struct *dsa_owq;
39 /* Track the bridges with forwarding offload enabled */
40 static unsigned long dsa_fwd_offloading_bridges;
42 bool dsa_schedule_work(struct work_struct *work)
44 return queue_work(dsa_owq, work);
47 void dsa_flush_workqueue(void)
49 flush_workqueue(dsa_owq);
51 EXPORT_SYMBOL_GPL(dsa_flush_workqueue);
54 * dsa_lag_map() - Map LAG structure to a linear LAG array
55 * @dst: Tree in which to record the mapping.
56 * @lag: LAG structure that is to be mapped to the tree's array.
58 * dsa_lag_id/dsa_lag_by_id can then be used to translate between the
59 * two spaces. The size of the mapping space is determined by the
60 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
61 * it unset if it is not needed, in which case these functions become
64 void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag)
68 for (id = 1; id <= dst->lags_len; id++) {
69 if (!dsa_lag_by_id(dst, id)) {
70 dst->lags[id - 1] = lag;
76 /* No IDs left, which is OK. Some drivers do not need it. The
77 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
78 * returns an error for this device when joining the LAG. The
79 * driver can then return -EOPNOTSUPP back to DSA, which will
80 * fall back to a software LAG.
85 * dsa_lag_unmap() - Remove a LAG ID mapping
86 * @dst: Tree in which the mapping is recorded.
87 * @lag: LAG structure that was mapped.
89 * As there may be multiple users of the mapping, it is only removed
90 * if there are no other references to it.
92 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag)
96 dsa_lags_foreach_id(id, dst) {
97 if (dsa_lag_by_id(dst, id) == lag) {
98 dst->lags[id - 1] = NULL;
105 struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
106 const struct net_device *lag_dev)
110 list_for_each_entry(dp, &dst->ports, list)
111 if (dsa_port_lag_dev_get(dp) == lag_dev)
117 struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
118 const struct net_device *br)
122 list_for_each_entry(dp, &dst->ports, list)
123 if (dsa_port_bridge_dev_get(dp) == br)
129 static int dsa_bridge_num_find(const struct net_device *bridge_dev)
131 struct dsa_switch_tree *dst;
133 list_for_each_entry(dst, &dsa_tree_list, list) {
134 struct dsa_bridge *bridge;
136 bridge = dsa_tree_bridge_find(dst, bridge_dev);
144 unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
146 unsigned int bridge_num = dsa_bridge_num_find(bridge_dev);
148 /* Switches without FDB isolation support don't get unique
155 /* First port that requests FDB isolation or TX forwarding
156 * offload for this bridge
158 bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges,
159 DSA_MAX_NUM_OFFLOADING_BRIDGES,
161 if (bridge_num >= max)
164 set_bit(bridge_num, &dsa_fwd_offloading_bridges);
170 void dsa_bridge_num_put(const struct net_device *bridge_dev,
171 unsigned int bridge_num)
173 /* Since we refcount bridges, we know that when we call this function
174 * it is no longer in use, so we can just go ahead and remove it from
177 clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
180 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
182 struct dsa_switch_tree *dst;
185 list_for_each_entry(dst, &dsa_tree_list, list) {
186 if (dst->index != tree_index)
189 list_for_each_entry(dp, &dst->ports, list) {
190 if (dp->ds->index != sw_index)
199 EXPORT_SYMBOL_GPL(dsa_switch_find);
201 static struct dsa_switch_tree *dsa_tree_find(int index)
203 struct dsa_switch_tree *dst;
205 list_for_each_entry(dst, &dsa_tree_list, list)
206 if (dst->index == index)
212 static struct dsa_switch_tree *dsa_tree_alloc(int index)
214 struct dsa_switch_tree *dst;
216 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
222 INIT_LIST_HEAD(&dst->rtable);
224 INIT_LIST_HEAD(&dst->ports);
226 INIT_LIST_HEAD(&dst->list);
227 list_add_tail(&dst->list, &dsa_tree_list);
229 kref_init(&dst->refcount);
234 static void dsa_tree_free(struct dsa_switch_tree *dst)
237 dsa_tag_driver_put(dst->tag_ops);
238 list_del(&dst->list);
242 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
245 kref_get(&dst->refcount);
250 static struct dsa_switch_tree *dsa_tree_touch(int index)
252 struct dsa_switch_tree *dst;
254 dst = dsa_tree_find(index);
256 return dsa_tree_get(dst);
258 return dsa_tree_alloc(index);
261 static void dsa_tree_release(struct kref *ref)
263 struct dsa_switch_tree *dst;
265 dst = container_of(ref, struct dsa_switch_tree, refcount);
270 static void dsa_tree_put(struct dsa_switch_tree *dst)
273 kref_put(&dst->refcount, dsa_tree_release);
276 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
277 struct device_node *dn)
281 list_for_each_entry(dp, &dst->ports, list)
288 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
289 struct dsa_port *link_dp)
291 struct dsa_switch *ds = dp->ds;
292 struct dsa_switch_tree *dst;
297 list_for_each_entry(dl, &dst->rtable, list)
298 if (dl->dp == dp && dl->link_dp == link_dp)
301 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
306 dl->link_dp = link_dp;
308 INIT_LIST_HEAD(&dl->list);
309 list_add_tail(&dl->list, &dst->rtable);
314 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
316 struct dsa_switch *ds = dp->ds;
317 struct dsa_switch_tree *dst = ds->dst;
318 struct device_node *dn = dp->dn;
319 struct of_phandle_iterator it;
320 struct dsa_port *link_dp;
324 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
325 link_dp = dsa_tree_find_port_by_node(dst, it.node);
327 of_node_put(it.node);
331 dl = dsa_link_touch(dp, link_dp);
333 of_node_put(it.node);
341 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
343 bool complete = true;
346 list_for_each_entry(dp, &dst->ports, list) {
347 if (dsa_port_is_dsa(dp)) {
348 complete = dsa_port_setup_routing_table(dp);
357 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
361 list_for_each_entry(dp, &dst->ports, list)
362 if (dsa_port_is_cpu(dp))
368 struct net_device *dsa_tree_find_first_master(struct dsa_switch_tree *dst)
370 struct device_node *ethernet;
371 struct net_device *master;
372 struct dsa_port *cpu_dp;
374 cpu_dp = dsa_tree_find_first_cpu(dst);
375 ethernet = of_parse_phandle(cpu_dp->dn, "ethernet", 0);
376 master = of_find_net_device_by_node(ethernet);
377 of_node_put(ethernet);
382 /* Assign the default CPU port (the first one in the tree) to all ports of the
383 * fabric which don't already have one as part of their own switch.
385 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
387 struct dsa_port *cpu_dp, *dp;
389 cpu_dp = dsa_tree_find_first_cpu(dst);
391 pr_err("DSA: tree %d has no CPU port\n", dst->index);
395 list_for_each_entry(dp, &dst->ports, list) {
399 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
406 /* Perform initial assignment of CPU ports to user ports and DSA links in the
407 * fabric, giving preference to CPU ports local to each switch. Default to
408 * using the first CPU port in the switch tree if the port does not have a CPU
409 * port local to this switch.
411 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
413 struct dsa_port *cpu_dp, *dp;
415 list_for_each_entry(cpu_dp, &dst->ports, list) {
416 if (!dsa_port_is_cpu(cpu_dp))
419 /* Prefer a local CPU port */
420 dsa_switch_for_each_port(dp, cpu_dp->ds) {
421 /* Prefer the first local CPU port found */
425 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
430 return dsa_tree_setup_default_cpu(dst);
433 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
437 list_for_each_entry(dp, &dst->ports, list)
438 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
442 static int dsa_port_setup(struct dsa_port *dp)
444 bool dsa_port_link_registered = false;
445 struct dsa_switch *ds = dp->ds;
446 bool dsa_port_enabled = false;
452 err = dsa_port_devlink_setup(dp);
457 case DSA_PORT_TYPE_UNUSED:
458 dsa_port_disable(dp);
460 case DSA_PORT_TYPE_CPU:
462 err = dsa_shared_port_link_register_of(dp);
465 dsa_port_link_registered = true;
468 "skipping link registration for CPU port %d\n",
472 err = dsa_port_enable(dp, NULL);
475 dsa_port_enabled = true;
478 case DSA_PORT_TYPE_DSA:
480 err = dsa_shared_port_link_register_of(dp);
483 dsa_port_link_registered = true;
486 "skipping link registration for DSA port %d\n",
490 err = dsa_port_enable(dp, NULL);
493 dsa_port_enabled = true;
496 case DSA_PORT_TYPE_USER:
497 of_get_mac_address(dp->dn, dp->mac);
498 err = dsa_slave_create(dp);
502 if (err && dsa_port_enabled)
503 dsa_port_disable(dp);
504 if (err && dsa_port_link_registered)
505 dsa_shared_port_link_unregister_of(dp);
507 dsa_port_devlink_teardown(dp);
516 static void dsa_port_teardown(struct dsa_port *dp)
522 case DSA_PORT_TYPE_UNUSED:
524 case DSA_PORT_TYPE_CPU:
525 dsa_port_disable(dp);
527 dsa_shared_port_link_unregister_of(dp);
529 case DSA_PORT_TYPE_DSA:
530 dsa_port_disable(dp);
532 dsa_shared_port_link_unregister_of(dp);
534 case DSA_PORT_TYPE_USER:
536 dsa_slave_destroy(dp->slave);
542 dsa_port_devlink_teardown(dp);
547 static int dsa_port_setup_as_unused(struct dsa_port *dp)
549 dp->type = DSA_PORT_TYPE_UNUSED;
550 return dsa_port_setup(dp);
553 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
555 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
556 struct dsa_switch_tree *dst = ds->dst;
559 if (tag_ops->proto == dst->default_proto)
563 err = ds->ops->change_tag_protocol(ds, tag_ops->proto);
566 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
567 tag_ops->name, ERR_PTR(err));
572 if (tag_ops->connect) {
573 err = tag_ops->connect(ds);
578 if (ds->ops->connect_tag_protocol) {
579 err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
582 "Unable to connect to tag protocol \"%s\": %pe\n",
583 tag_ops->name, ERR_PTR(err));
591 if (tag_ops->disconnect)
592 tag_ops->disconnect(ds);
597 static void dsa_switch_teardown_tag_protocol(struct dsa_switch *ds)
599 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
601 if (tag_ops->disconnect)
602 tag_ops->disconnect(ds);
605 static int dsa_switch_setup(struct dsa_switch *ds)
607 struct device_node *dn;
613 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
614 * driver and before ops->setup() has run, since the switch drivers and
615 * the slave MDIO bus driver rely on these values for probing PHY
618 ds->phys_mii_mask |= dsa_user_ports(ds);
620 err = dsa_switch_devlink_alloc(ds);
624 err = dsa_switch_register_notifier(ds);
628 ds->configure_vlan_while_not_filtering = true;
630 err = ds->ops->setup(ds);
632 goto unregister_notifier;
634 err = dsa_switch_setup_tag_protocol(ds);
638 if (!ds->slave_mii_bus && ds->ops->phy_read) {
639 ds->slave_mii_bus = mdiobus_alloc();
640 if (!ds->slave_mii_bus) {
645 dsa_slave_mii_bus_init(ds);
647 dn = of_get_child_by_name(ds->dev->of_node, "mdio");
649 err = of_mdiobus_register(ds->slave_mii_bus, dn);
652 goto free_slave_mii_bus;
655 dsa_switch_devlink_register(ds);
661 if (ds->slave_mii_bus && ds->ops->phy_read)
662 mdiobus_free(ds->slave_mii_bus);
664 if (ds->ops->teardown)
665 ds->ops->teardown(ds);
667 dsa_switch_unregister_notifier(ds);
669 dsa_switch_devlink_free(ds);
673 static void dsa_switch_teardown(struct dsa_switch *ds)
678 dsa_switch_devlink_unregister(ds);
680 if (ds->slave_mii_bus && ds->ops->phy_read) {
681 mdiobus_unregister(ds->slave_mii_bus);
682 mdiobus_free(ds->slave_mii_bus);
683 ds->slave_mii_bus = NULL;
686 dsa_switch_teardown_tag_protocol(ds);
688 if (ds->ops->teardown)
689 ds->ops->teardown(ds);
691 dsa_switch_unregister_notifier(ds);
693 dsa_switch_devlink_free(ds);
698 /* First tear down the non-shared, then the shared ports. This ensures that
699 * all work items scheduled by our switchdev handlers for user ports have
700 * completed before we destroy the refcounting kept on the shared ports.
702 static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
706 list_for_each_entry(dp, &dst->ports, list)
707 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
708 dsa_port_teardown(dp);
710 dsa_flush_workqueue();
712 list_for_each_entry(dp, &dst->ports, list)
713 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
714 dsa_port_teardown(dp);
717 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
721 list_for_each_entry(dp, &dst->ports, list)
722 dsa_switch_teardown(dp->ds);
725 /* Bring shared ports up first, then non-shared ports */
726 static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
731 list_for_each_entry(dp, &dst->ports, list) {
732 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
733 err = dsa_port_setup(dp);
739 list_for_each_entry(dp, &dst->ports, list) {
740 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
741 err = dsa_port_setup(dp);
743 err = dsa_port_setup_as_unused(dp);
753 dsa_tree_teardown_ports(dst);
758 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
763 list_for_each_entry(dp, &dst->ports, list) {
764 err = dsa_switch_setup(dp->ds);
766 dsa_tree_teardown_switches(dst);
774 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
776 struct dsa_port *cpu_dp;
781 dsa_tree_for_each_cpu_port(cpu_dp, dst) {
782 struct net_device *master = cpu_dp->master;
783 bool admin_up = (master->flags & IFF_UP) &&
784 !qdisc_tx_is_noop(master);
786 err = dsa_master_setup(master, cpu_dp);
790 /* Replay master state event */
791 dsa_tree_master_admin_state_change(dst, master, admin_up);
792 dsa_tree_master_oper_state_change(dst, master,
793 netif_oper_up(master));
801 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
803 struct dsa_port *cpu_dp;
807 dsa_tree_for_each_cpu_port(cpu_dp, dst) {
808 struct net_device *master = cpu_dp->master;
810 /* Synthesizing an "admin down" state is sufficient for
811 * the switches to get a notification if the master is
812 * currently up and running.
814 dsa_tree_master_admin_state_change(dst, master, false);
816 dsa_master_teardown(master);
822 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
824 unsigned int len = 0;
827 list_for_each_entry(dp, &dst->ports, list) {
828 if (dp->ds->num_lag_ids > len)
829 len = dp->ds->num_lag_ids;
835 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
843 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
848 static int dsa_tree_setup(struct dsa_switch_tree *dst)
854 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
859 complete = dsa_tree_setup_routing_table(dst);
863 err = dsa_tree_setup_cpu_ports(dst);
867 err = dsa_tree_setup_switches(dst);
869 goto teardown_cpu_ports;
871 err = dsa_tree_setup_ports(dst);
873 goto teardown_switches;
875 err = dsa_tree_setup_master(dst);
879 err = dsa_tree_setup_lags(dst);
881 goto teardown_master;
885 pr_info("DSA: tree %d setup\n", dst->index);
890 dsa_tree_teardown_master(dst);
892 dsa_tree_teardown_ports(dst);
894 dsa_tree_teardown_switches(dst);
896 dsa_tree_teardown_cpu_ports(dst);
901 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
903 struct dsa_link *dl, *next;
908 dsa_tree_teardown_lags(dst);
910 dsa_tree_teardown_master(dst);
912 dsa_tree_teardown_ports(dst);
914 dsa_tree_teardown_switches(dst);
916 dsa_tree_teardown_cpu_ports(dst);
918 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
923 pr_info("DSA: tree %d torn down\n", dst->index);
928 static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
929 const struct dsa_device_ops *tag_ops)
931 const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
932 struct dsa_notifier_tag_proto_info info;
935 dst->tag_ops = tag_ops;
937 /* Notify the switches from this tree about the connection
940 info.tag_ops = tag_ops;
941 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
942 if (err && err != -EOPNOTSUPP)
945 /* Notify the old tagger about the disconnection from this tree */
946 info.tag_ops = old_tag_ops;
947 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
952 info.tag_ops = tag_ops;
953 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
954 dst->tag_ops = old_tag_ops;
959 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
960 * is that all DSA switches within a tree share the same tagger, otherwise
961 * they would have formed disjoint trees (different "dsa,member" values).
963 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
964 const struct dsa_device_ops *tag_ops,
965 const struct dsa_device_ops *old_tag_ops)
967 struct dsa_notifier_tag_proto_info info;
972 return restart_syscall();
974 /* At the moment we don't allow changing the tag protocol under
975 * traffic. The rtnl_mutex also happens to serialize concurrent
976 * attempts to change the tagging protocol. If we ever lift the IFF_UP
977 * restriction, there needs to be another mutex which serializes this.
979 dsa_tree_for_each_user_port(dp, dst) {
980 if (dsa_port_to_master(dp)->flags & IFF_UP)
983 if (dp->slave->flags & IFF_UP)
987 /* Notify the tag protocol change */
988 info.tag_ops = tag_ops;
989 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
991 goto out_unwind_tagger;
993 err = dsa_tree_bind_tag_proto(dst, tag_ops);
995 goto out_unwind_tagger;
1002 info.tag_ops = old_tag_ops;
1003 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1009 static void dsa_tree_master_state_change(struct dsa_switch_tree *dst,
1010 struct net_device *master)
1012 struct dsa_notifier_master_state_info info;
1013 struct dsa_port *cpu_dp = master->dsa_ptr;
1015 info.master = master;
1016 info.operational = dsa_port_master_is_operational(cpu_dp);
1018 dsa_tree_notify(dst, DSA_NOTIFIER_MASTER_STATE_CHANGE, &info);
1021 void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
1022 struct net_device *master,
1025 struct dsa_port *cpu_dp = master->dsa_ptr;
1026 bool notify = false;
1028 /* Don't keep track of admin state on LAG DSA masters,
1029 * but rather just of physical DSA masters
1031 if (netif_is_lag_master(master))
1034 if ((dsa_port_master_is_operational(cpu_dp)) !=
1035 (up && cpu_dp->master_oper_up))
1038 cpu_dp->master_admin_up = up;
1041 dsa_tree_master_state_change(dst, master);
1044 void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
1045 struct net_device *master,
1048 struct dsa_port *cpu_dp = master->dsa_ptr;
1049 bool notify = false;
1051 /* Don't keep track of oper state on LAG DSA masters,
1052 * but rather just of physical DSA masters
1054 if (netif_is_lag_master(master))
1057 if ((dsa_port_master_is_operational(cpu_dp)) !=
1058 (cpu_dp->master_admin_up && up))
1061 cpu_dp->master_oper_up = up;
1064 dsa_tree_master_state_change(dst, master);
1067 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1069 struct dsa_switch_tree *dst = ds->dst;
1070 struct dsa_port *dp;
1072 dsa_switch_for_each_port(dp, ds)
1073 if (dp->index == index)
1076 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1083 mutex_init(&dp->addr_lists_lock);
1084 mutex_init(&dp->vlans_lock);
1085 INIT_LIST_HEAD(&dp->fdbs);
1086 INIT_LIST_HEAD(&dp->mdbs);
1087 INIT_LIST_HEAD(&dp->vlans);
1088 INIT_LIST_HEAD(&dp->list);
1089 list_add_tail(&dp->list, &dst->ports);
1094 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1096 dp->type = DSA_PORT_TYPE_USER;
1102 static int dsa_port_parse_dsa(struct dsa_port *dp)
1104 dp->type = DSA_PORT_TYPE_DSA;
1109 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1110 struct net_device *master)
1112 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1113 struct dsa_switch *mds, *ds = dp->ds;
1114 unsigned int mdp_upstream;
1115 struct dsa_port *mdp;
1117 /* It is possible to stack DSA switches onto one another when that
1118 * happens the switch driver may want to know if its tagging protocol
1119 * is going to work in such a configuration.
1121 if (dsa_slave_dev_check(master)) {
1122 mdp = dsa_slave_to_port(master);
1124 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1125 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1126 DSA_TAG_PROTO_NONE);
1129 /* If the master device is not itself a DSA slave in a disjoint DSA
1130 * tree, then return immediately.
1132 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1135 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1136 const char *user_protocol)
1138 const struct dsa_device_ops *tag_ops = NULL;
1139 struct dsa_switch *ds = dp->ds;
1140 struct dsa_switch_tree *dst = ds->dst;
1141 enum dsa_tag_protocol default_proto;
1143 /* Find out which protocol the switch would prefer. */
1144 default_proto = dsa_get_tag_protocol(dp, master);
1145 if (dst->default_proto) {
1146 if (dst->default_proto != default_proto) {
1148 "A DSA switch tree can have only one tagging protocol\n");
1152 dst->default_proto = default_proto;
1155 /* See if the user wants to override that preference. */
1156 if (user_protocol) {
1157 if (!ds->ops->change_tag_protocol) {
1158 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1162 tag_ops = dsa_tag_driver_get_by_name(user_protocol);
1163 if (IS_ERR(tag_ops)) {
1165 "Failed to find a tagging driver for protocol %s, using default\n",
1172 tag_ops = dsa_tag_driver_get_by_id(default_proto);
1174 if (IS_ERR(tag_ops)) {
1175 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1176 return -EPROBE_DEFER;
1178 dev_warn(ds->dev, "No tagger for this switch\n");
1179 return PTR_ERR(tag_ops);
1183 if (dst->tag_ops != tag_ops) {
1185 "A DSA switch tree can have only one tagging protocol\n");
1187 dsa_tag_driver_put(tag_ops);
1191 /* In the case of multiple CPU ports per switch, the tagging
1192 * protocol is still reference-counted only per switch tree.
1194 dsa_tag_driver_put(tag_ops);
1196 dst->tag_ops = tag_ops;
1199 dp->master = master;
1200 dp->type = DSA_PORT_TYPE_CPU;
1201 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1204 /* At this point, the tree may be configured to use a different
1205 * tagger than the one chosen by the switch driver during
1206 * .setup, in the case when a user selects a custom protocol
1209 * This is resolved by syncing the driver with the tree in
1210 * dsa_switch_setup_tag_protocol once .setup has run and the
1211 * driver is ready to accept calls to .change_tag_protocol. If
1212 * the driver does not support the custom protocol at that
1213 * point, the tree is wholly rejected, thereby ensuring that the
1214 * tree and driver are always in agreement on the protocol to
1220 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1222 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1223 const char *name = of_get_property(dn, "label", NULL);
1224 bool link = of_property_read_bool(dn, "link");
1229 struct net_device *master;
1230 const char *user_protocol;
1232 master = of_find_net_device_by_node(ethernet);
1233 of_node_put(ethernet);
1235 return -EPROBE_DEFER;
1237 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1238 return dsa_port_parse_cpu(dp, master, user_protocol);
1242 return dsa_port_parse_dsa(dp);
1244 return dsa_port_parse_user(dp, name);
1247 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1248 struct device_node *dn)
1250 struct device_node *ports, *port;
1251 struct dsa_port *dp;
1255 ports = of_get_child_by_name(dn, "ports");
1257 /* The second possibility is "ethernet-ports" */
1258 ports = of_get_child_by_name(dn, "ethernet-ports");
1260 dev_err(ds->dev, "no ports child node found\n");
1265 for_each_available_child_of_node(ports, port) {
1266 err = of_property_read_u32(port, "reg", ®);
1272 if (reg >= ds->num_ports) {
1273 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
1274 port, reg, ds->num_ports);
1280 dp = dsa_to_port(ds, reg);
1282 err = dsa_port_parse_of(dp, port);
1294 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1295 struct device_node *dn)
1297 u32 m[2] = { 0, 0 };
1300 /* Don't error out if this optional property isn't found */
1301 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1302 if (sz < 0 && sz != -EINVAL)
1307 ds->dst = dsa_tree_touch(m[0]);
1311 if (dsa_switch_find(ds->dst->index, ds->index)) {
1313 "A DSA switch with index %d already exists in tree %d\n",
1314 ds->index, ds->dst->index);
1318 if (ds->dst->last_switch < ds->index)
1319 ds->dst->last_switch = ds->index;
1324 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1326 struct dsa_port *dp;
1329 for (port = 0; port < ds->num_ports; port++) {
1330 dp = dsa_port_touch(ds, port);
1338 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1342 err = dsa_switch_parse_member_of(ds, dn);
1346 err = dsa_switch_touch_ports(ds);
1350 return dsa_switch_parse_ports_of(ds, dn);
1353 static int dev_is_class(struct device *dev, void *class)
1355 if (dev->class != NULL && !strcmp(dev->class->name, class))
1361 static struct device *dev_find_class(struct device *parent, char *class)
1363 if (dev_is_class(parent, class)) {
1368 return device_find_child(parent, class, dev_is_class);
1371 static struct net_device *dsa_dev_to_net_device(struct device *dev)
1375 d = dev_find_class(dev, "net");
1377 struct net_device *nd;
1389 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1392 if (!strcmp(name, "cpu")) {
1393 struct net_device *master;
1395 master = dsa_dev_to_net_device(dev);
1397 return -EPROBE_DEFER;
1401 return dsa_port_parse_cpu(dp, master, NULL);
1404 if (!strcmp(name, "dsa"))
1405 return dsa_port_parse_dsa(dp);
1407 return dsa_port_parse_user(dp, name);
1410 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1411 struct dsa_chip_data *cd)
1413 bool valid_name_found = false;
1414 struct dsa_port *dp;
1420 for (i = 0; i < DSA_MAX_PORTS; i++) {
1421 name = cd->port_names[i];
1422 dev = cd->netdev[i];
1423 dp = dsa_to_port(ds, i);
1428 err = dsa_port_parse(dp, name, dev);
1432 valid_name_found = true;
1435 if (!valid_name_found && i == DSA_MAX_PORTS)
1441 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1447 /* We don't support interconnected switches nor multiple trees via
1448 * platform data, so this is the unique switch of the tree.
1451 ds->dst = dsa_tree_touch(0);
1455 err = dsa_switch_touch_ports(ds);
1459 return dsa_switch_parse_ports(ds, cd);
1462 static void dsa_switch_release_ports(struct dsa_switch *ds)
1464 struct dsa_port *dp, *next;
1466 dsa_switch_for_each_port_safe(dp, next, ds) {
1467 WARN_ON(!list_empty(&dp->fdbs));
1468 WARN_ON(!list_empty(&dp->mdbs));
1469 WARN_ON(!list_empty(&dp->vlans));
1470 list_del(&dp->list);
1475 static int dsa_switch_probe(struct dsa_switch *ds)
1477 struct dsa_switch_tree *dst;
1478 struct dsa_chip_data *pdata;
1479 struct device_node *np;
1485 pdata = ds->dev->platform_data;
1486 np = ds->dev->of_node;
1492 err = dsa_switch_parse_of(ds, np);
1494 dsa_switch_release_ports(ds);
1496 err = dsa_switch_parse(ds, pdata);
1498 dsa_switch_release_ports(ds);
1508 err = dsa_tree_setup(dst);
1510 dsa_switch_release_ports(ds);
1517 int dsa_register_switch(struct dsa_switch *ds)
1521 mutex_lock(&dsa2_mutex);
1522 err = dsa_switch_probe(ds);
1523 dsa_tree_put(ds->dst);
1524 mutex_unlock(&dsa2_mutex);
1528 EXPORT_SYMBOL_GPL(dsa_register_switch);
1530 static void dsa_switch_remove(struct dsa_switch *ds)
1532 struct dsa_switch_tree *dst = ds->dst;
1534 dsa_tree_teardown(dst);
1535 dsa_switch_release_ports(ds);
1539 void dsa_unregister_switch(struct dsa_switch *ds)
1541 mutex_lock(&dsa2_mutex);
1542 dsa_switch_remove(ds);
1543 mutex_unlock(&dsa2_mutex);
1545 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1547 /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1548 * blocking that operation from completion, due to the dev_hold taken inside
1549 * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1550 * the DSA master, so that the system can reboot successfully.
1552 void dsa_switch_shutdown(struct dsa_switch *ds)
1554 struct net_device *master, *slave_dev;
1555 struct dsa_port *dp;
1557 mutex_lock(&dsa2_mutex);
1564 dsa_switch_for_each_user_port(dp, ds) {
1565 master = dsa_port_to_master(dp);
1566 slave_dev = dp->slave;
1568 netdev_upper_dev_unlink(master, slave_dev);
1571 /* Disconnect from further netdevice notifiers on the master,
1572 * since netdev_uses_dsa() will now return false.
1574 dsa_switch_for_each_cpu_port(dp, ds)
1575 dp->master->dsa_ptr = NULL;
1579 mutex_unlock(&dsa2_mutex);
1581 EXPORT_SYMBOL_GPL(dsa_switch_shutdown);
1583 #ifdef CONFIG_PM_SLEEP
1584 static bool dsa_port_is_initialized(const struct dsa_port *dp)
1586 return dp->type == DSA_PORT_TYPE_USER && dp->slave;
1589 int dsa_switch_suspend(struct dsa_switch *ds)
1591 struct dsa_port *dp;
1594 /* Suspend slave network devices */
1595 dsa_switch_for_each_port(dp, ds) {
1596 if (!dsa_port_is_initialized(dp))
1599 ret = dsa_slave_suspend(dp->slave);
1604 if (ds->ops->suspend)
1605 ret = ds->ops->suspend(ds);
1609 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
1611 int dsa_switch_resume(struct dsa_switch *ds)
1613 struct dsa_port *dp;
1616 if (ds->ops->resume)
1617 ret = ds->ops->resume(ds);
1622 /* Resume slave network devices */
1623 dsa_switch_for_each_port(dp, ds) {
1624 if (!dsa_port_is_initialized(dp))
1627 ret = dsa_slave_resume(dp->slave);
1634 EXPORT_SYMBOL_GPL(dsa_switch_resume);
1637 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev)
1639 if (!netdev || !dsa_slave_dev_check(netdev))
1640 return ERR_PTR(-ENODEV);
1642 return dsa_slave_to_port(netdev);
1644 EXPORT_SYMBOL_GPL(dsa_port_from_netdev);
1646 bool dsa_db_equal(const struct dsa_db *a, const struct dsa_db *b)
1648 if (a->type != b->type)
1653 return a->dp == b->dp;
1655 return a->lag.dev == b->lag.dev;
1657 return a->bridge.num == b->bridge.num;
1664 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
1665 const unsigned char *addr, u16 vid,
1668 struct dsa_port *dp = dsa_to_port(ds, port);
1669 struct dsa_mac_addr *a;
1671 lockdep_assert_held(&dp->addr_lists_lock);
1673 list_for_each_entry(a, &dp->fdbs, list) {
1674 if (!ether_addr_equal(a->addr, addr) || a->vid != vid)
1677 if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
1683 EXPORT_SYMBOL_GPL(dsa_fdb_present_in_other_db);
1685 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
1686 const struct switchdev_obj_port_mdb *mdb,
1689 struct dsa_port *dp = dsa_to_port(ds, port);
1690 struct dsa_mac_addr *a;
1692 lockdep_assert_held(&dp->addr_lists_lock);
1694 list_for_each_entry(a, &dp->mdbs, list) {
1695 if (!ether_addr_equal(a->addr, mdb->addr) || a->vid != mdb->vid)
1698 if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
1704 EXPORT_SYMBOL_GPL(dsa_mdb_present_in_other_db);
1706 static const struct dsa_stubs __dsa_stubs = {
1707 .master_hwtstamp_validate = __dsa_master_hwtstamp_validate,
1710 static void dsa_register_stubs(void)
1712 dsa_stubs = &__dsa_stubs;
1715 static void dsa_unregister_stubs(void)
1720 static int __init dsa_init_module(void)
1724 dsa_owq = alloc_ordered_workqueue("dsa_ordered",
1729 rc = dsa_slave_register_notifier();
1731 goto register_notifier_fail;
1733 dev_add_pack(&dsa_pack_type);
1735 rc = rtnl_link_register(&dsa_link_ops);
1737 goto netlink_register_fail;
1739 dsa_register_stubs();
1743 netlink_register_fail:
1744 dsa_slave_unregister_notifier();
1745 dev_remove_pack(&dsa_pack_type);
1746 register_notifier_fail:
1747 destroy_workqueue(dsa_owq);
1751 module_init(dsa_init_module);
1753 static void __exit dsa_cleanup_module(void)
1755 dsa_unregister_stubs();
1757 rtnl_link_unregister(&dsa_link_ops);
1759 dsa_slave_unregister_notifier();
1760 dev_remove_pack(&dsa_pack_type);
1761 destroy_workqueue(dsa_owq);
1763 module_exit(dsa_cleanup_module);
1766 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1767 MODULE_LICENSE("GPL");
1768 MODULE_ALIAS("platform:dsa");