1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DSA topology and switch handling
5 * Copyright (c) 2008-2009 Marvell Semiconductor
6 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
7 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
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 static struct dsa_port *
407 dsa_switch_preferred_default_local_cpu_port(struct dsa_switch *ds)
409 struct dsa_port *cpu_dp;
411 if (!ds->ops->preferred_default_local_cpu_port)
414 cpu_dp = ds->ops->preferred_default_local_cpu_port(ds);
418 if (WARN_ON(!dsa_port_is_cpu(cpu_dp) || cpu_dp->ds != ds))
424 /* Perform initial assignment of CPU ports to user ports and DSA links in the
425 * fabric, giving preference to CPU ports local to each switch. Default to
426 * using the first CPU port in the switch tree if the port does not have a CPU
427 * port local to this switch.
429 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
431 struct dsa_port *preferred_cpu_dp, *cpu_dp, *dp;
433 list_for_each_entry(cpu_dp, &dst->ports, list) {
434 if (!dsa_port_is_cpu(cpu_dp))
437 preferred_cpu_dp = dsa_switch_preferred_default_local_cpu_port(cpu_dp->ds);
438 if (preferred_cpu_dp && preferred_cpu_dp != cpu_dp)
441 /* Prefer a local CPU port */
442 dsa_switch_for_each_port(dp, cpu_dp->ds) {
443 /* Prefer the first local CPU port found */
447 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
452 return dsa_tree_setup_default_cpu(dst);
455 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
459 list_for_each_entry(dp, &dst->ports, list)
460 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
464 static int dsa_port_setup(struct dsa_port *dp)
466 bool dsa_port_link_registered = false;
467 struct dsa_switch *ds = dp->ds;
468 bool dsa_port_enabled = false;
474 err = dsa_port_devlink_setup(dp);
479 case DSA_PORT_TYPE_UNUSED:
480 dsa_port_disable(dp);
482 case DSA_PORT_TYPE_CPU:
484 err = dsa_shared_port_link_register_of(dp);
487 dsa_port_link_registered = true;
490 "skipping link registration for CPU port %d\n",
494 err = dsa_port_enable(dp, NULL);
497 dsa_port_enabled = true;
500 case DSA_PORT_TYPE_DSA:
502 err = dsa_shared_port_link_register_of(dp);
505 dsa_port_link_registered = true;
508 "skipping link registration for DSA port %d\n",
512 err = dsa_port_enable(dp, NULL);
515 dsa_port_enabled = true;
518 case DSA_PORT_TYPE_USER:
519 of_get_mac_address(dp->dn, dp->mac);
520 err = dsa_slave_create(dp);
524 if (err && dsa_port_enabled)
525 dsa_port_disable(dp);
526 if (err && dsa_port_link_registered)
527 dsa_shared_port_link_unregister_of(dp);
529 dsa_port_devlink_teardown(dp);
538 static void dsa_port_teardown(struct dsa_port *dp)
544 case DSA_PORT_TYPE_UNUSED:
546 case DSA_PORT_TYPE_CPU:
547 dsa_port_disable(dp);
549 dsa_shared_port_link_unregister_of(dp);
551 case DSA_PORT_TYPE_DSA:
552 dsa_port_disable(dp);
554 dsa_shared_port_link_unregister_of(dp);
556 case DSA_PORT_TYPE_USER:
558 dsa_slave_destroy(dp->slave);
564 dsa_port_devlink_teardown(dp);
569 static int dsa_port_setup_as_unused(struct dsa_port *dp)
571 dp->type = DSA_PORT_TYPE_UNUSED;
572 return dsa_port_setup(dp);
575 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
577 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
578 struct dsa_switch_tree *dst = ds->dst;
581 if (tag_ops->proto == dst->default_proto)
585 err = ds->ops->change_tag_protocol(ds, tag_ops->proto);
588 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
589 tag_ops->name, ERR_PTR(err));
594 if (tag_ops->connect) {
595 err = tag_ops->connect(ds);
600 if (ds->ops->connect_tag_protocol) {
601 err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
604 "Unable to connect to tag protocol \"%s\": %pe\n",
605 tag_ops->name, ERR_PTR(err));
613 if (tag_ops->disconnect)
614 tag_ops->disconnect(ds);
619 static void dsa_switch_teardown_tag_protocol(struct dsa_switch *ds)
621 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
623 if (tag_ops->disconnect)
624 tag_ops->disconnect(ds);
627 static int dsa_switch_setup(struct dsa_switch *ds)
629 struct device_node *dn;
635 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
636 * driver and before ops->setup() has run, since the switch drivers and
637 * the slave MDIO bus driver rely on these values for probing PHY
640 ds->phys_mii_mask |= dsa_user_ports(ds);
642 err = dsa_switch_devlink_alloc(ds);
646 err = dsa_switch_register_notifier(ds);
650 ds->configure_vlan_while_not_filtering = true;
652 err = ds->ops->setup(ds);
654 goto unregister_notifier;
656 err = dsa_switch_setup_tag_protocol(ds);
660 if (!ds->slave_mii_bus && ds->ops->phy_read) {
661 ds->slave_mii_bus = mdiobus_alloc();
662 if (!ds->slave_mii_bus) {
667 dsa_slave_mii_bus_init(ds);
669 dn = of_get_child_by_name(ds->dev->of_node, "mdio");
671 err = of_mdiobus_register(ds->slave_mii_bus, dn);
674 goto free_slave_mii_bus;
677 dsa_switch_devlink_register(ds);
683 if (ds->slave_mii_bus && ds->ops->phy_read)
684 mdiobus_free(ds->slave_mii_bus);
686 if (ds->ops->teardown)
687 ds->ops->teardown(ds);
689 dsa_switch_unregister_notifier(ds);
691 dsa_switch_devlink_free(ds);
695 static void dsa_switch_teardown(struct dsa_switch *ds)
700 dsa_switch_devlink_unregister(ds);
702 if (ds->slave_mii_bus && ds->ops->phy_read) {
703 mdiobus_unregister(ds->slave_mii_bus);
704 mdiobus_free(ds->slave_mii_bus);
705 ds->slave_mii_bus = NULL;
708 dsa_switch_teardown_tag_protocol(ds);
710 if (ds->ops->teardown)
711 ds->ops->teardown(ds);
713 dsa_switch_unregister_notifier(ds);
715 dsa_switch_devlink_free(ds);
720 /* First tear down the non-shared, then the shared ports. This ensures that
721 * all work items scheduled by our switchdev handlers for user ports have
722 * completed before we destroy the refcounting kept on the shared ports.
724 static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
728 list_for_each_entry(dp, &dst->ports, list)
729 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
730 dsa_port_teardown(dp);
732 dsa_flush_workqueue();
734 list_for_each_entry(dp, &dst->ports, list)
735 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
736 dsa_port_teardown(dp);
739 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
743 list_for_each_entry(dp, &dst->ports, list)
744 dsa_switch_teardown(dp->ds);
747 /* Bring shared ports up first, then non-shared ports */
748 static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
753 list_for_each_entry(dp, &dst->ports, list) {
754 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
755 err = dsa_port_setup(dp);
761 list_for_each_entry(dp, &dst->ports, list) {
762 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
763 err = dsa_port_setup(dp);
765 err = dsa_port_setup_as_unused(dp);
775 dsa_tree_teardown_ports(dst);
780 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
785 list_for_each_entry(dp, &dst->ports, list) {
786 err = dsa_switch_setup(dp->ds);
788 dsa_tree_teardown_switches(dst);
796 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
798 struct dsa_port *cpu_dp;
803 dsa_tree_for_each_cpu_port(cpu_dp, dst) {
804 struct net_device *master = cpu_dp->master;
805 bool admin_up = (master->flags & IFF_UP) &&
806 !qdisc_tx_is_noop(master);
808 err = dsa_master_setup(master, cpu_dp);
812 /* Replay master state event */
813 dsa_tree_master_admin_state_change(dst, master, admin_up);
814 dsa_tree_master_oper_state_change(dst, master,
815 netif_oper_up(master));
823 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
825 struct dsa_port *cpu_dp;
829 dsa_tree_for_each_cpu_port(cpu_dp, dst) {
830 struct net_device *master = cpu_dp->master;
832 /* Synthesizing an "admin down" state is sufficient for
833 * the switches to get a notification if the master is
834 * currently up and running.
836 dsa_tree_master_admin_state_change(dst, master, false);
838 dsa_master_teardown(master);
844 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
846 unsigned int len = 0;
849 list_for_each_entry(dp, &dst->ports, list) {
850 if (dp->ds->num_lag_ids > len)
851 len = dp->ds->num_lag_ids;
857 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
865 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
870 static int dsa_tree_setup(struct dsa_switch_tree *dst)
876 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
881 complete = dsa_tree_setup_routing_table(dst);
885 err = dsa_tree_setup_cpu_ports(dst);
889 err = dsa_tree_setup_switches(dst);
891 goto teardown_cpu_ports;
893 err = dsa_tree_setup_ports(dst);
895 goto teardown_switches;
897 err = dsa_tree_setup_master(dst);
901 err = dsa_tree_setup_lags(dst);
903 goto teardown_master;
907 pr_info("DSA: tree %d setup\n", dst->index);
912 dsa_tree_teardown_master(dst);
914 dsa_tree_teardown_ports(dst);
916 dsa_tree_teardown_switches(dst);
918 dsa_tree_teardown_cpu_ports(dst);
923 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
925 struct dsa_link *dl, *next;
930 dsa_tree_teardown_lags(dst);
932 dsa_tree_teardown_master(dst);
934 dsa_tree_teardown_ports(dst);
936 dsa_tree_teardown_switches(dst);
938 dsa_tree_teardown_cpu_ports(dst);
940 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
945 pr_info("DSA: tree %d torn down\n", dst->index);
950 static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
951 const struct dsa_device_ops *tag_ops)
953 const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
954 struct dsa_notifier_tag_proto_info info;
957 dst->tag_ops = tag_ops;
959 /* Notify the switches from this tree about the connection
962 info.tag_ops = tag_ops;
963 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
964 if (err && err != -EOPNOTSUPP)
967 /* Notify the old tagger about the disconnection from this tree */
968 info.tag_ops = old_tag_ops;
969 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
974 info.tag_ops = tag_ops;
975 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
976 dst->tag_ops = old_tag_ops;
981 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
982 * is that all DSA switches within a tree share the same tagger, otherwise
983 * they would have formed disjoint trees (different "dsa,member" values).
985 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
986 const struct dsa_device_ops *tag_ops,
987 const struct dsa_device_ops *old_tag_ops)
989 struct dsa_notifier_tag_proto_info info;
994 return restart_syscall();
996 /* At the moment we don't allow changing the tag protocol under
997 * traffic. The rtnl_mutex also happens to serialize concurrent
998 * attempts to change the tagging protocol. If we ever lift the IFF_UP
999 * restriction, there needs to be another mutex which serializes this.
1001 dsa_tree_for_each_user_port(dp, dst) {
1002 if (dsa_port_to_master(dp)->flags & IFF_UP)
1005 if (dp->slave->flags & IFF_UP)
1009 /* Notify the tag protocol change */
1010 info.tag_ops = tag_ops;
1011 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1013 goto out_unwind_tagger;
1015 err = dsa_tree_bind_tag_proto(dst, tag_ops);
1017 goto out_unwind_tagger;
1024 info.tag_ops = old_tag_ops;
1025 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1031 static void dsa_tree_master_state_change(struct dsa_switch_tree *dst,
1032 struct net_device *master)
1034 struct dsa_notifier_master_state_info info;
1035 struct dsa_port *cpu_dp = master->dsa_ptr;
1037 info.master = master;
1038 info.operational = dsa_port_master_is_operational(cpu_dp);
1040 dsa_tree_notify(dst, DSA_NOTIFIER_MASTER_STATE_CHANGE, &info);
1043 void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
1044 struct net_device *master,
1047 struct dsa_port *cpu_dp = master->dsa_ptr;
1048 bool notify = false;
1050 /* Don't keep track of admin state on LAG DSA masters,
1051 * but rather just of physical DSA masters
1053 if (netif_is_lag_master(master))
1056 if ((dsa_port_master_is_operational(cpu_dp)) !=
1057 (up && cpu_dp->master_oper_up))
1060 cpu_dp->master_admin_up = up;
1063 dsa_tree_master_state_change(dst, master);
1066 void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
1067 struct net_device *master,
1070 struct dsa_port *cpu_dp = master->dsa_ptr;
1071 bool notify = false;
1073 /* Don't keep track of oper state on LAG DSA masters,
1074 * but rather just of physical DSA masters
1076 if (netif_is_lag_master(master))
1079 if ((dsa_port_master_is_operational(cpu_dp)) !=
1080 (cpu_dp->master_admin_up && up))
1083 cpu_dp->master_oper_up = up;
1086 dsa_tree_master_state_change(dst, master);
1089 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1091 struct dsa_switch_tree *dst = ds->dst;
1092 struct dsa_port *dp;
1094 dsa_switch_for_each_port(dp, ds)
1095 if (dp->index == index)
1098 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1105 mutex_init(&dp->addr_lists_lock);
1106 mutex_init(&dp->vlans_lock);
1107 INIT_LIST_HEAD(&dp->fdbs);
1108 INIT_LIST_HEAD(&dp->mdbs);
1109 INIT_LIST_HEAD(&dp->vlans); /* also initializes &dp->user_vlans */
1110 INIT_LIST_HEAD(&dp->list);
1111 list_add_tail(&dp->list, &dst->ports);
1116 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1118 dp->type = DSA_PORT_TYPE_USER;
1124 static int dsa_port_parse_dsa(struct dsa_port *dp)
1126 dp->type = DSA_PORT_TYPE_DSA;
1131 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1132 struct net_device *master)
1134 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1135 struct dsa_switch *mds, *ds = dp->ds;
1136 unsigned int mdp_upstream;
1137 struct dsa_port *mdp;
1139 /* It is possible to stack DSA switches onto one another when that
1140 * happens the switch driver may want to know if its tagging protocol
1141 * is going to work in such a configuration.
1143 if (dsa_slave_dev_check(master)) {
1144 mdp = dsa_slave_to_port(master);
1146 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1147 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1148 DSA_TAG_PROTO_NONE);
1151 /* If the master device is not itself a DSA slave in a disjoint DSA
1152 * tree, then return immediately.
1154 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1157 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1158 const char *user_protocol)
1160 const struct dsa_device_ops *tag_ops = NULL;
1161 struct dsa_switch *ds = dp->ds;
1162 struct dsa_switch_tree *dst = ds->dst;
1163 enum dsa_tag_protocol default_proto;
1165 /* Find out which protocol the switch would prefer. */
1166 default_proto = dsa_get_tag_protocol(dp, master);
1167 if (dst->default_proto) {
1168 if (dst->default_proto != default_proto) {
1170 "A DSA switch tree can have only one tagging protocol\n");
1174 dst->default_proto = default_proto;
1177 /* See if the user wants to override that preference. */
1178 if (user_protocol) {
1179 if (!ds->ops->change_tag_protocol) {
1180 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1184 tag_ops = dsa_tag_driver_get_by_name(user_protocol);
1185 if (IS_ERR(tag_ops)) {
1187 "Failed to find a tagging driver for protocol %s, using default\n",
1194 tag_ops = dsa_tag_driver_get_by_id(default_proto);
1196 if (IS_ERR(tag_ops)) {
1197 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1198 return -EPROBE_DEFER;
1200 dev_warn(ds->dev, "No tagger for this switch\n");
1201 return PTR_ERR(tag_ops);
1205 if (dst->tag_ops != tag_ops) {
1207 "A DSA switch tree can have only one tagging protocol\n");
1209 dsa_tag_driver_put(tag_ops);
1213 /* In the case of multiple CPU ports per switch, the tagging
1214 * protocol is still reference-counted only per switch tree.
1216 dsa_tag_driver_put(tag_ops);
1218 dst->tag_ops = tag_ops;
1221 dp->master = master;
1222 dp->type = DSA_PORT_TYPE_CPU;
1223 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1226 /* At this point, the tree may be configured to use a different
1227 * tagger than the one chosen by the switch driver during
1228 * .setup, in the case when a user selects a custom protocol
1231 * This is resolved by syncing the driver with the tree in
1232 * dsa_switch_setup_tag_protocol once .setup has run and the
1233 * driver is ready to accept calls to .change_tag_protocol. If
1234 * the driver does not support the custom protocol at that
1235 * point, the tree is wholly rejected, thereby ensuring that the
1236 * tree and driver are always in agreement on the protocol to
1242 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1244 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1245 const char *name = of_get_property(dn, "label", NULL);
1246 bool link = of_property_read_bool(dn, "link");
1251 struct net_device *master;
1252 const char *user_protocol;
1254 master = of_find_net_device_by_node(ethernet);
1255 of_node_put(ethernet);
1257 return -EPROBE_DEFER;
1259 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1260 return dsa_port_parse_cpu(dp, master, user_protocol);
1264 return dsa_port_parse_dsa(dp);
1266 return dsa_port_parse_user(dp, name);
1269 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1270 struct device_node *dn)
1272 struct device_node *ports, *port;
1273 struct dsa_port *dp;
1277 ports = of_get_child_by_name(dn, "ports");
1279 /* The second possibility is "ethernet-ports" */
1280 ports = of_get_child_by_name(dn, "ethernet-ports");
1282 dev_err(ds->dev, "no ports child node found\n");
1287 for_each_available_child_of_node(ports, port) {
1288 err = of_property_read_u32(port, "reg", ®);
1294 if (reg >= ds->num_ports) {
1295 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
1296 port, reg, ds->num_ports);
1302 dp = dsa_to_port(ds, reg);
1304 err = dsa_port_parse_of(dp, port);
1316 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1317 struct device_node *dn)
1319 u32 m[2] = { 0, 0 };
1322 /* Don't error out if this optional property isn't found */
1323 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1324 if (sz < 0 && sz != -EINVAL)
1329 ds->dst = dsa_tree_touch(m[0]);
1333 if (dsa_switch_find(ds->dst->index, ds->index)) {
1335 "A DSA switch with index %d already exists in tree %d\n",
1336 ds->index, ds->dst->index);
1340 if (ds->dst->last_switch < ds->index)
1341 ds->dst->last_switch = ds->index;
1346 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1348 struct dsa_port *dp;
1351 for (port = 0; port < ds->num_ports; port++) {
1352 dp = dsa_port_touch(ds, port);
1360 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1364 err = dsa_switch_parse_member_of(ds, dn);
1368 err = dsa_switch_touch_ports(ds);
1372 return dsa_switch_parse_ports_of(ds, dn);
1375 static int dev_is_class(struct device *dev, void *class)
1377 if (dev->class != NULL && !strcmp(dev->class->name, class))
1383 static struct device *dev_find_class(struct device *parent, char *class)
1385 if (dev_is_class(parent, class)) {
1390 return device_find_child(parent, class, dev_is_class);
1393 static struct net_device *dsa_dev_to_net_device(struct device *dev)
1397 d = dev_find_class(dev, "net");
1399 struct net_device *nd;
1411 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1414 if (!strcmp(name, "cpu")) {
1415 struct net_device *master;
1417 master = dsa_dev_to_net_device(dev);
1419 return -EPROBE_DEFER;
1423 return dsa_port_parse_cpu(dp, master, NULL);
1426 if (!strcmp(name, "dsa"))
1427 return dsa_port_parse_dsa(dp);
1429 return dsa_port_parse_user(dp, name);
1432 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1433 struct dsa_chip_data *cd)
1435 bool valid_name_found = false;
1436 struct dsa_port *dp;
1442 for (i = 0; i < DSA_MAX_PORTS; i++) {
1443 name = cd->port_names[i];
1444 dev = cd->netdev[i];
1445 dp = dsa_to_port(ds, i);
1450 err = dsa_port_parse(dp, name, dev);
1454 valid_name_found = true;
1457 if (!valid_name_found && i == DSA_MAX_PORTS)
1463 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1469 /* We don't support interconnected switches nor multiple trees via
1470 * platform data, so this is the unique switch of the tree.
1473 ds->dst = dsa_tree_touch(0);
1477 err = dsa_switch_touch_ports(ds);
1481 return dsa_switch_parse_ports(ds, cd);
1484 static void dsa_switch_release_ports(struct dsa_switch *ds)
1486 struct dsa_port *dp, *next;
1488 dsa_switch_for_each_port_safe(dp, next, ds) {
1489 WARN_ON(!list_empty(&dp->fdbs));
1490 WARN_ON(!list_empty(&dp->mdbs));
1491 WARN_ON(!list_empty(&dp->vlans));
1492 list_del(&dp->list);
1497 static int dsa_switch_probe(struct dsa_switch *ds)
1499 struct dsa_switch_tree *dst;
1500 struct dsa_chip_data *pdata;
1501 struct device_node *np;
1507 pdata = ds->dev->platform_data;
1508 np = ds->dev->of_node;
1514 err = dsa_switch_parse_of(ds, np);
1516 dsa_switch_release_ports(ds);
1518 err = dsa_switch_parse(ds, pdata);
1520 dsa_switch_release_ports(ds);
1530 err = dsa_tree_setup(dst);
1532 dsa_switch_release_ports(ds);
1539 int dsa_register_switch(struct dsa_switch *ds)
1543 mutex_lock(&dsa2_mutex);
1544 err = dsa_switch_probe(ds);
1545 dsa_tree_put(ds->dst);
1546 mutex_unlock(&dsa2_mutex);
1550 EXPORT_SYMBOL_GPL(dsa_register_switch);
1552 static void dsa_switch_remove(struct dsa_switch *ds)
1554 struct dsa_switch_tree *dst = ds->dst;
1556 dsa_tree_teardown(dst);
1557 dsa_switch_release_ports(ds);
1561 void dsa_unregister_switch(struct dsa_switch *ds)
1563 mutex_lock(&dsa2_mutex);
1564 dsa_switch_remove(ds);
1565 mutex_unlock(&dsa2_mutex);
1567 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1569 /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1570 * blocking that operation from completion, due to the dev_hold taken inside
1571 * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1572 * the DSA master, so that the system can reboot successfully.
1574 void dsa_switch_shutdown(struct dsa_switch *ds)
1576 struct net_device *master, *slave_dev;
1577 struct dsa_port *dp;
1579 mutex_lock(&dsa2_mutex);
1586 dsa_switch_for_each_user_port(dp, ds) {
1587 master = dsa_port_to_master(dp);
1588 slave_dev = dp->slave;
1590 netdev_upper_dev_unlink(master, slave_dev);
1593 /* Disconnect from further netdevice notifiers on the master,
1594 * since netdev_uses_dsa() will now return false.
1596 dsa_switch_for_each_cpu_port(dp, ds)
1597 dp->master->dsa_ptr = NULL;
1601 mutex_unlock(&dsa2_mutex);
1603 EXPORT_SYMBOL_GPL(dsa_switch_shutdown);
1605 #ifdef CONFIG_PM_SLEEP
1606 static bool dsa_port_is_initialized(const struct dsa_port *dp)
1608 return dp->type == DSA_PORT_TYPE_USER && dp->slave;
1611 int dsa_switch_suspend(struct dsa_switch *ds)
1613 struct dsa_port *dp;
1616 /* Suspend slave network devices */
1617 dsa_switch_for_each_port(dp, ds) {
1618 if (!dsa_port_is_initialized(dp))
1621 ret = dsa_slave_suspend(dp->slave);
1626 if (ds->ops->suspend)
1627 ret = ds->ops->suspend(ds);
1631 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
1633 int dsa_switch_resume(struct dsa_switch *ds)
1635 struct dsa_port *dp;
1638 if (ds->ops->resume)
1639 ret = ds->ops->resume(ds);
1644 /* Resume slave network devices */
1645 dsa_switch_for_each_port(dp, ds) {
1646 if (!dsa_port_is_initialized(dp))
1649 ret = dsa_slave_resume(dp->slave);
1656 EXPORT_SYMBOL_GPL(dsa_switch_resume);
1659 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev)
1661 if (!netdev || !dsa_slave_dev_check(netdev))
1662 return ERR_PTR(-ENODEV);
1664 return dsa_slave_to_port(netdev);
1666 EXPORT_SYMBOL_GPL(dsa_port_from_netdev);
1668 bool dsa_db_equal(const struct dsa_db *a, const struct dsa_db *b)
1670 if (a->type != b->type)
1675 return a->dp == b->dp;
1677 return a->lag.dev == b->lag.dev;
1679 return a->bridge.num == b->bridge.num;
1686 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
1687 const unsigned char *addr, u16 vid,
1690 struct dsa_port *dp = dsa_to_port(ds, port);
1691 struct dsa_mac_addr *a;
1693 lockdep_assert_held(&dp->addr_lists_lock);
1695 list_for_each_entry(a, &dp->fdbs, list) {
1696 if (!ether_addr_equal(a->addr, addr) || a->vid != vid)
1699 if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
1705 EXPORT_SYMBOL_GPL(dsa_fdb_present_in_other_db);
1707 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
1708 const struct switchdev_obj_port_mdb *mdb,
1711 struct dsa_port *dp = dsa_to_port(ds, port);
1712 struct dsa_mac_addr *a;
1714 lockdep_assert_held(&dp->addr_lists_lock);
1716 list_for_each_entry(a, &dp->mdbs, list) {
1717 if (!ether_addr_equal(a->addr, mdb->addr) || a->vid != mdb->vid)
1720 if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
1726 EXPORT_SYMBOL_GPL(dsa_mdb_present_in_other_db);
1728 static const struct dsa_stubs __dsa_stubs = {
1729 .master_hwtstamp_validate = __dsa_master_hwtstamp_validate,
1732 static void dsa_register_stubs(void)
1734 dsa_stubs = &__dsa_stubs;
1737 static void dsa_unregister_stubs(void)
1742 static int __init dsa_init_module(void)
1746 dsa_owq = alloc_ordered_workqueue("dsa_ordered",
1751 rc = dsa_slave_register_notifier();
1753 goto register_notifier_fail;
1755 dev_add_pack(&dsa_pack_type);
1757 rc = rtnl_link_register(&dsa_link_ops);
1759 goto netlink_register_fail;
1761 dsa_register_stubs();
1765 netlink_register_fail:
1766 dsa_slave_unregister_notifier();
1767 dev_remove_pack(&dsa_pack_type);
1768 register_notifier_fail:
1769 destroy_workqueue(dsa_owq);
1773 module_init(dsa_init_module);
1775 static void __exit dsa_cleanup_module(void)
1777 dsa_unregister_stubs();
1779 rtnl_link_unregister(&dsa_link_ops);
1781 dsa_slave_unregister_notifier();
1782 dev_remove_pack(&dsa_pack_type);
1783 destroy_workqueue(dsa_owq);
1785 module_exit(dsa_cleanup_module);
1787 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
1788 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1789 MODULE_LICENSE("GPL");
1790 MODULE_ALIAS("platform:dsa");