1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
6 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/rtnetlink.h>
16 #include <linux/of_net.h>
17 #include <net/devlink.h>
21 static DEFINE_MUTEX(dsa2_mutex);
22 LIST_HEAD(dsa_tree_list);
24 /* Track the bridges with forwarding offload enabled */
25 static unsigned long dsa_fwd_offloading_bridges;
28 * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
29 * @dst: collection of struct dsa_switch devices to notify.
30 * @e: event, must be of type DSA_NOTIFIER_*
31 * @v: event-specific value.
33 * Given a struct dsa_switch_tree, this can be used to run a function once for
34 * each member DSA switch. The other alternative of traversing the tree is only
35 * through its ports list, which does not uniquely list the switches.
37 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
39 struct raw_notifier_head *nh = &dst->nh;
42 err = raw_notifier_call_chain(nh, e, v);
44 return notifier_to_errno(err);
48 * dsa_broadcast - Notify all DSA trees in the system.
49 * @e: event, must be of type DSA_NOTIFIER_*
50 * @v: event-specific value.
52 * Can be used to notify the switching fabric of events such as cross-chip
53 * bridging between disjoint trees (such as islands of tagger-compatible
54 * switches bridged by an incompatible middle switch).
56 * WARNING: this function is not reliable during probe time, because probing
57 * between trees is asynchronous and not all DSA trees might have probed.
59 int dsa_broadcast(unsigned long e, void *v)
61 struct dsa_switch_tree *dst;
64 list_for_each_entry(dst, &dsa_tree_list, list) {
65 err = dsa_tree_notify(dst, e, v);
74 * dsa_lag_map() - Map LAG netdev to a linear LAG ID
75 * @dst: Tree in which to record the mapping.
76 * @lag: Netdev that is to be mapped to an ID.
78 * dsa_lag_id/dsa_lag_dev can then be used to translate between the
79 * two spaces. The size of the mapping space is determined by the
80 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
81 * it unset if it is not needed, in which case these functions become
84 void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag)
88 if (dsa_lag_id(dst, lag) >= 0)
92 for (id = 0; id < dst->lags_len; id++) {
93 if (!dsa_lag_dev(dst, id)) {
99 /* No IDs left, which is OK. Some drivers do not need it. The
100 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
101 * returns an error for this device when joining the LAG. The
102 * driver can then return -EOPNOTSUPP back to DSA, which will
103 * fall back to a software LAG.
108 * dsa_lag_unmap() - Remove a LAG ID mapping
109 * @dst: Tree in which the mapping is recorded.
110 * @lag: Netdev that was mapped.
112 * As there may be multiple users of the mapping, it is only removed
113 * if there are no other references to it.
115 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag)
120 dsa_lag_foreach_port(dp, dst, lag)
121 /* There are remaining users of this mapping */
124 dsa_lags_foreach_id(id, dst) {
125 if (dsa_lag_dev(dst, id) == lag) {
126 dst->lags[id] = NULL;
132 static int dsa_bridge_num_find(const struct net_device *bridge_dev)
134 struct dsa_switch_tree *dst;
137 /* When preparing the offload for a port, it will have a valid
138 * dp->bridge_dev pointer but a not yet valid dp->bridge_num.
139 * However there might be other ports having the same dp->bridge_dev
140 * and a valid dp->bridge_num, so just ignore this port.
142 list_for_each_entry(dst, &dsa_tree_list, list)
143 list_for_each_entry(dp, &dst->ports, list)
144 if (dp->bridge_dev == bridge_dev &&
145 dp->bridge_num != -1)
146 return dp->bridge_num;
151 int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
153 int bridge_num = dsa_bridge_num_find(bridge_dev);
155 if (bridge_num < 0) {
156 /* First port that offloads TX forwarding for this bridge */
157 bridge_num = find_first_zero_bit(&dsa_fwd_offloading_bridges,
158 DSA_MAX_NUM_OFFLOADING_BRIDGES);
159 if (bridge_num >= max)
162 set_bit(bridge_num, &dsa_fwd_offloading_bridges);
168 void dsa_bridge_num_put(const struct net_device *bridge_dev, int bridge_num)
170 /* Check if the bridge is still in use, otherwise it is time
171 * to clean it up so we can reuse this bridge_num later.
173 if (dsa_bridge_num_find(bridge_dev) < 0)
174 clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
177 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
179 struct dsa_switch_tree *dst;
182 list_for_each_entry(dst, &dsa_tree_list, list) {
183 if (dst->index != tree_index)
186 list_for_each_entry(dp, &dst->ports, list) {
187 if (dp->ds->index != sw_index)
196 EXPORT_SYMBOL_GPL(dsa_switch_find);
198 static struct dsa_switch_tree *dsa_tree_find(int index)
200 struct dsa_switch_tree *dst;
202 list_for_each_entry(dst, &dsa_tree_list, list)
203 if (dst->index == index)
209 static struct dsa_switch_tree *dsa_tree_alloc(int index)
211 struct dsa_switch_tree *dst;
213 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
219 INIT_LIST_HEAD(&dst->rtable);
221 INIT_LIST_HEAD(&dst->ports);
223 INIT_LIST_HEAD(&dst->list);
224 list_add_tail(&dst->list, &dsa_tree_list);
226 kref_init(&dst->refcount);
231 static void dsa_tree_free(struct dsa_switch_tree *dst)
234 dsa_tag_driver_put(dst->tag_ops);
235 list_del(&dst->list);
239 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
242 kref_get(&dst->refcount);
247 static struct dsa_switch_tree *dsa_tree_touch(int index)
249 struct dsa_switch_tree *dst;
251 dst = dsa_tree_find(index);
253 return dsa_tree_get(dst);
255 return dsa_tree_alloc(index);
258 static void dsa_tree_release(struct kref *ref)
260 struct dsa_switch_tree *dst;
262 dst = container_of(ref, struct dsa_switch_tree, refcount);
267 static void dsa_tree_put(struct dsa_switch_tree *dst)
270 kref_put(&dst->refcount, dsa_tree_release);
273 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
274 struct device_node *dn)
278 list_for_each_entry(dp, &dst->ports, list)
285 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
286 struct dsa_port *link_dp)
288 struct dsa_switch *ds = dp->ds;
289 struct dsa_switch_tree *dst;
294 list_for_each_entry(dl, &dst->rtable, list)
295 if (dl->dp == dp && dl->link_dp == link_dp)
298 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
303 dl->link_dp = link_dp;
305 INIT_LIST_HEAD(&dl->list);
306 list_add_tail(&dl->list, &dst->rtable);
311 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
313 struct dsa_switch *ds = dp->ds;
314 struct dsa_switch_tree *dst = ds->dst;
315 struct device_node *dn = dp->dn;
316 struct of_phandle_iterator it;
317 struct dsa_port *link_dp;
321 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
322 link_dp = dsa_tree_find_port_by_node(dst, it.node);
324 of_node_put(it.node);
328 dl = dsa_link_touch(dp, link_dp);
330 of_node_put(it.node);
338 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
340 bool complete = true;
343 list_for_each_entry(dp, &dst->ports, list) {
344 if (dsa_port_is_dsa(dp)) {
345 complete = dsa_port_setup_routing_table(dp);
354 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
358 list_for_each_entry(dp, &dst->ports, list)
359 if (dsa_port_is_cpu(dp))
365 /* Assign the default CPU port (the first one in the tree) to all ports of the
366 * fabric which don't already have one as part of their own switch.
368 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
370 struct dsa_port *cpu_dp, *dp;
372 cpu_dp = dsa_tree_find_first_cpu(dst);
374 pr_err("DSA: tree %d has no CPU port\n", dst->index);
378 list_for_each_entry(dp, &dst->ports, list) {
382 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
389 /* Perform initial assignment of CPU ports to user ports and DSA links in the
390 * fabric, giving preference to CPU ports local to each switch. Default to
391 * using the first CPU port in the switch tree if the port does not have a CPU
392 * port local to this switch.
394 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
396 struct dsa_port *cpu_dp, *dp;
398 list_for_each_entry(cpu_dp, &dst->ports, list) {
399 if (!dsa_port_is_cpu(cpu_dp))
402 list_for_each_entry(dp, &dst->ports, list) {
403 /* Prefer a local CPU port */
404 if (dp->ds != cpu_dp->ds)
407 /* Prefer the first local CPU port found */
411 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
416 return dsa_tree_setup_default_cpu(dst);
419 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
423 list_for_each_entry(dp, &dst->ports, list)
424 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
428 static int dsa_port_setup(struct dsa_port *dp)
430 struct devlink_port *dlp = &dp->devlink_port;
431 bool dsa_port_link_registered = false;
432 struct dsa_switch *ds = dp->ds;
433 bool dsa_port_enabled = false;
439 INIT_LIST_HEAD(&dp->fdbs);
440 INIT_LIST_HEAD(&dp->mdbs);
442 if (ds->ops->port_setup) {
443 err = ds->ops->port_setup(ds, dp->index);
449 case DSA_PORT_TYPE_UNUSED:
450 dsa_port_disable(dp);
452 case DSA_PORT_TYPE_CPU:
453 err = dsa_port_link_register_of(dp);
456 dsa_port_link_registered = true;
458 err = dsa_port_enable(dp, NULL);
461 dsa_port_enabled = true;
464 case DSA_PORT_TYPE_DSA:
465 err = dsa_port_link_register_of(dp);
468 dsa_port_link_registered = true;
470 err = dsa_port_enable(dp, NULL);
473 dsa_port_enabled = true;
476 case DSA_PORT_TYPE_USER:
477 of_get_mac_address(dp->dn, dp->mac);
478 err = dsa_slave_create(dp);
482 devlink_port_type_eth_set(dlp, dp->slave);
486 if (err && dsa_port_enabled)
487 dsa_port_disable(dp);
488 if (err && dsa_port_link_registered)
489 dsa_port_link_unregister_of(dp);
491 if (ds->ops->port_teardown)
492 ds->ops->port_teardown(ds, dp->index);
501 static int dsa_port_devlink_setup(struct dsa_port *dp)
503 struct devlink_port *dlp = &dp->devlink_port;
504 struct dsa_switch_tree *dst = dp->ds->dst;
505 struct devlink_port_attrs attrs = {};
506 struct devlink *dl = dp->ds->devlink;
507 const unsigned char *id;
511 id = (const unsigned char *)&dst->index;
512 len = sizeof(dst->index);
514 attrs.phys.port_number = dp->index;
515 memcpy(attrs.switch_id.id, id, len);
516 attrs.switch_id.id_len = len;
517 memset(dlp, 0, sizeof(*dlp));
520 case DSA_PORT_TYPE_UNUSED:
521 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
523 case DSA_PORT_TYPE_CPU:
524 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
526 case DSA_PORT_TYPE_DSA:
527 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
529 case DSA_PORT_TYPE_USER:
530 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
534 devlink_port_attrs_set(dlp, &attrs);
535 err = devlink_port_register(dl, dlp, dp->index);
538 dp->devlink_port_setup = true;
543 static void dsa_port_teardown(struct dsa_port *dp)
545 struct devlink_port *dlp = &dp->devlink_port;
546 struct dsa_switch *ds = dp->ds;
547 struct dsa_mac_addr *a, *tmp;
552 if (ds->ops->port_teardown)
553 ds->ops->port_teardown(ds, dp->index);
555 devlink_port_type_clear(dlp);
558 case DSA_PORT_TYPE_UNUSED:
560 case DSA_PORT_TYPE_CPU:
561 dsa_port_disable(dp);
562 dsa_port_link_unregister_of(dp);
564 case DSA_PORT_TYPE_DSA:
565 dsa_port_disable(dp);
566 dsa_port_link_unregister_of(dp);
568 case DSA_PORT_TYPE_USER:
570 dsa_slave_destroy(dp->slave);
576 list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
581 list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
589 static void dsa_port_devlink_teardown(struct dsa_port *dp)
591 struct devlink_port *dlp = &dp->devlink_port;
593 if (dp->devlink_port_setup)
594 devlink_port_unregister(dlp);
595 dp->devlink_port_setup = false;
598 /* Destroy the current devlink port, and create a new one which has the UNUSED
599 * flavour. At this point, any call to ds->ops->port_setup has been already
600 * balanced out by a call to ds->ops->port_teardown, so we know that any
601 * devlink port regions the driver had are now unregistered. We then call its
602 * ds->ops->port_setup again, in order for the driver to re-create them on the
605 static int dsa_port_reinit_as_unused(struct dsa_port *dp)
607 struct dsa_switch *ds = dp->ds;
610 dsa_port_devlink_teardown(dp);
611 dp->type = DSA_PORT_TYPE_UNUSED;
612 err = dsa_port_devlink_setup(dp);
616 if (ds->ops->port_setup) {
617 /* On error, leave the devlink port registered,
618 * dsa_switch_teardown will clean it up later.
620 err = ds->ops->port_setup(ds, dp->index);
628 static int dsa_devlink_info_get(struct devlink *dl,
629 struct devlink_info_req *req,
630 struct netlink_ext_ack *extack)
632 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
634 if (ds->ops->devlink_info_get)
635 return ds->ops->devlink_info_get(ds, req, extack);
640 static int dsa_devlink_sb_pool_get(struct devlink *dl,
641 unsigned int sb_index, u16 pool_index,
642 struct devlink_sb_pool_info *pool_info)
644 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
646 if (!ds->ops->devlink_sb_pool_get)
649 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
653 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
654 u16 pool_index, u32 size,
655 enum devlink_sb_threshold_type threshold_type,
656 struct netlink_ext_ack *extack)
658 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
660 if (!ds->ops->devlink_sb_pool_set)
663 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
664 threshold_type, extack);
667 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
668 unsigned int sb_index, u16 pool_index,
671 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
672 int port = dsa_devlink_port_to_port(dlp);
674 if (!ds->ops->devlink_sb_port_pool_get)
677 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
678 pool_index, p_threshold);
681 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
682 unsigned int sb_index, u16 pool_index,
684 struct netlink_ext_ack *extack)
686 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
687 int port = dsa_devlink_port_to_port(dlp);
689 if (!ds->ops->devlink_sb_port_pool_set)
692 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
693 pool_index, threshold, extack);
697 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
698 unsigned int sb_index, u16 tc_index,
699 enum devlink_sb_pool_type pool_type,
700 u16 *p_pool_index, u32 *p_threshold)
702 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
703 int port = dsa_devlink_port_to_port(dlp);
705 if (!ds->ops->devlink_sb_tc_pool_bind_get)
708 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
710 p_pool_index, p_threshold);
714 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
715 unsigned int sb_index, u16 tc_index,
716 enum devlink_sb_pool_type pool_type,
717 u16 pool_index, u32 threshold,
718 struct netlink_ext_ack *extack)
720 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
721 int port = dsa_devlink_port_to_port(dlp);
723 if (!ds->ops->devlink_sb_tc_pool_bind_set)
726 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
728 pool_index, threshold,
732 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
733 unsigned int sb_index)
735 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
737 if (!ds->ops->devlink_sb_occ_snapshot)
740 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
743 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
744 unsigned int sb_index)
746 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
748 if (!ds->ops->devlink_sb_occ_max_clear)
751 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
754 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
755 unsigned int sb_index,
756 u16 pool_index, u32 *p_cur,
759 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
760 int port = dsa_devlink_port_to_port(dlp);
762 if (!ds->ops->devlink_sb_occ_port_pool_get)
765 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
766 pool_index, p_cur, p_max);
770 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
771 unsigned int sb_index, u16 tc_index,
772 enum devlink_sb_pool_type pool_type,
773 u32 *p_cur, u32 *p_max)
775 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
776 int port = dsa_devlink_port_to_port(dlp);
778 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
781 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
787 static const struct devlink_ops dsa_devlink_ops = {
788 .info_get = dsa_devlink_info_get,
789 .sb_pool_get = dsa_devlink_sb_pool_get,
790 .sb_pool_set = dsa_devlink_sb_pool_set,
791 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
792 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
793 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
794 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
795 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
796 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
797 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
798 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
801 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
803 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
804 struct dsa_switch_tree *dst = ds->dst;
807 if (tag_ops->proto == dst->default_proto)
810 for (port = 0; port < ds->num_ports; port++) {
811 if (!dsa_is_cpu_port(ds, port))
815 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
818 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
819 tag_ops->name, ERR_PTR(err));
827 static int dsa_switch_setup(struct dsa_switch *ds)
829 struct dsa_devlink_priv *dl_priv;
836 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
837 * driver and before ops->setup() has run, since the switch drivers and
838 * the slave MDIO bus driver rely on these values for probing PHY
841 ds->phys_mii_mask |= dsa_user_ports(ds);
843 /* Add the switch to devlink before calling setup, so that setup can
847 devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
850 dl_priv = devlink_priv(ds->devlink);
853 err = devlink_register(ds->devlink);
857 /* Setup devlink port instances now, so that the switch
858 * setup() can register regions etc, against the ports
860 list_for_each_entry(dp, &ds->dst->ports, list) {
862 err = dsa_port_devlink_setup(dp);
864 goto unregister_devlink_ports;
868 err = dsa_switch_register_notifier(ds);
870 goto unregister_devlink_ports;
872 ds->configure_vlan_while_not_filtering = true;
874 err = ds->ops->setup(ds);
876 goto unregister_notifier;
878 err = dsa_switch_setup_tag_protocol(ds);
882 devlink_params_publish(ds->devlink);
884 if (!ds->slave_mii_bus && ds->ops->phy_read) {
885 ds->slave_mii_bus = mdiobus_alloc();
886 if (!ds->slave_mii_bus) {
891 dsa_slave_mii_bus_init(ds);
893 err = mdiobus_register(ds->slave_mii_bus);
895 goto free_slave_mii_bus;
903 if (ds->slave_mii_bus && ds->ops->phy_read)
904 mdiobus_free(ds->slave_mii_bus);
906 if (ds->ops->teardown)
907 ds->ops->teardown(ds);
909 dsa_switch_unregister_notifier(ds);
910 unregister_devlink_ports:
911 list_for_each_entry(dp, &ds->dst->ports, list)
913 dsa_port_devlink_teardown(dp);
914 devlink_unregister(ds->devlink);
916 devlink_free(ds->devlink);
922 static void dsa_switch_teardown(struct dsa_switch *ds)
929 if (ds->slave_mii_bus && ds->ops->phy_read) {
930 mdiobus_unregister(ds->slave_mii_bus);
931 mdiobus_free(ds->slave_mii_bus);
932 ds->slave_mii_bus = NULL;
935 dsa_switch_unregister_notifier(ds);
937 if (ds->ops->teardown)
938 ds->ops->teardown(ds);
941 list_for_each_entry(dp, &ds->dst->ports, list)
943 dsa_port_devlink_teardown(dp);
944 devlink_unregister(ds->devlink);
945 devlink_free(ds->devlink);
952 /* First tear down the non-shared, then the shared ports. This ensures that
953 * all work items scheduled by our switchdev handlers for user ports have
954 * completed before we destroy the refcounting kept on the shared ports.
956 static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
960 list_for_each_entry(dp, &dst->ports, list)
961 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
962 dsa_port_teardown(dp);
964 dsa_flush_workqueue();
966 list_for_each_entry(dp, &dst->ports, list)
967 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
968 dsa_port_teardown(dp);
971 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
975 list_for_each_entry(dp, &dst->ports, list)
976 dsa_switch_teardown(dp->ds);
979 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
984 list_for_each_entry(dp, &dst->ports, list) {
985 err = dsa_switch_setup(dp->ds);
990 list_for_each_entry(dp, &dst->ports, list) {
991 err = dsa_port_setup(dp);
993 err = dsa_port_reinit_as_unused(dp);
1002 dsa_tree_teardown_ports(dst);
1004 dsa_tree_teardown_switches(dst);
1009 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
1011 struct dsa_port *dp;
1014 list_for_each_entry(dp, &dst->ports, list) {
1015 if (dsa_port_is_cpu(dp)) {
1016 err = dsa_master_setup(dp->master, dp);
1025 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
1027 struct dsa_port *dp;
1029 list_for_each_entry(dp, &dst->ports, list)
1030 if (dsa_port_is_cpu(dp))
1031 dsa_master_teardown(dp->master);
1034 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
1036 unsigned int len = 0;
1037 struct dsa_port *dp;
1039 list_for_each_entry(dp, &dst->ports, list) {
1040 if (dp->ds->num_lag_ids > len)
1041 len = dp->ds->num_lag_ids;
1047 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
1051 dst->lags_len = len;
1055 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
1060 static int dsa_tree_setup(struct dsa_switch_tree *dst)
1066 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
1071 complete = dsa_tree_setup_routing_table(dst);
1075 err = dsa_tree_setup_cpu_ports(dst);
1079 err = dsa_tree_setup_switches(dst);
1081 goto teardown_cpu_ports;
1083 err = dsa_tree_setup_master(dst);
1085 goto teardown_switches;
1087 err = dsa_tree_setup_lags(dst);
1089 goto teardown_master;
1093 pr_info("DSA: tree %d setup\n", dst->index);
1098 dsa_tree_teardown_master(dst);
1100 dsa_tree_teardown_ports(dst);
1101 dsa_tree_teardown_switches(dst);
1103 dsa_tree_teardown_cpu_ports(dst);
1108 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
1110 struct dsa_link *dl, *next;
1115 dsa_tree_teardown_lags(dst);
1117 dsa_tree_teardown_master(dst);
1119 dsa_tree_teardown_ports(dst);
1121 dsa_tree_teardown_switches(dst);
1123 dsa_tree_teardown_cpu_ports(dst);
1125 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
1126 list_del(&dl->list);
1130 pr_info("DSA: tree %d torn down\n", dst->index);
1135 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
1136 * is that all DSA switches within a tree share the same tagger, otherwise
1137 * they would have formed disjoint trees (different "dsa,member" values).
1139 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
1140 struct net_device *master,
1141 const struct dsa_device_ops *tag_ops,
1142 const struct dsa_device_ops *old_tag_ops)
1144 struct dsa_notifier_tag_proto_info info;
1145 struct dsa_port *dp;
1148 if (!rtnl_trylock())
1149 return restart_syscall();
1151 /* At the moment we don't allow changing the tag protocol under
1152 * traffic. The rtnl_mutex also happens to serialize concurrent
1153 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1154 * restriction, there needs to be another mutex which serializes this.
1156 if (master->flags & IFF_UP)
1159 list_for_each_entry(dp, &dst->ports, list) {
1160 if (!dsa_is_user_port(dp->ds, dp->index))
1163 if (dp->slave->flags & IFF_UP)
1167 info.tag_ops = tag_ops;
1168 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1170 goto out_unwind_tagger;
1172 dst->tag_ops = tag_ops;
1179 info.tag_ops = old_tag_ops;
1180 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1186 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1188 struct dsa_switch_tree *dst = ds->dst;
1189 struct dsa_port *dp;
1191 list_for_each_entry(dp, &dst->ports, list)
1192 if (dp->ds == ds && dp->index == index)
1195 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1201 dp->bridge_num = -1;
1203 INIT_LIST_HEAD(&dp->list);
1204 list_add_tail(&dp->list, &dst->ports);
1209 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1214 dp->type = DSA_PORT_TYPE_USER;
1220 static int dsa_port_parse_dsa(struct dsa_port *dp)
1222 dp->type = DSA_PORT_TYPE_DSA;
1227 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1228 struct net_device *master)
1230 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1231 struct dsa_switch *mds, *ds = dp->ds;
1232 unsigned int mdp_upstream;
1233 struct dsa_port *mdp;
1235 /* It is possible to stack DSA switches onto one another when that
1236 * happens the switch driver may want to know if its tagging protocol
1237 * is going to work in such a configuration.
1239 if (dsa_slave_dev_check(master)) {
1240 mdp = dsa_slave_to_port(master);
1242 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1243 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1244 DSA_TAG_PROTO_NONE);
1247 /* If the master device is not itself a DSA slave in a disjoint DSA
1248 * tree, then return immediately.
1250 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1253 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1254 const char *user_protocol)
1256 const struct dsa_device_ops *tag_ops = NULL;
1257 struct dsa_switch *ds = dp->ds;
1258 struct dsa_switch_tree *dst = ds->dst;
1259 enum dsa_tag_protocol default_proto;
1261 /* Find out which protocol the switch would prefer. */
1262 default_proto = dsa_get_tag_protocol(dp, master);
1263 if (dst->default_proto) {
1264 if (dst->default_proto != default_proto) {
1266 "A DSA switch tree can have only one tagging protocol\n");
1270 dst->default_proto = default_proto;
1273 /* See if the user wants to override that preference. */
1274 if (user_protocol) {
1275 if (!ds->ops->change_tag_protocol) {
1276 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1280 tag_ops = dsa_find_tagger_by_name(user_protocol);
1281 if (IS_ERR(tag_ops)) {
1283 "Failed to find a tagging driver for protocol %s, using default\n",
1290 tag_ops = dsa_tag_driver_get(default_proto);
1292 if (IS_ERR(tag_ops)) {
1293 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1294 return -EPROBE_DEFER;
1296 dev_warn(ds->dev, "No tagger for this switch\n");
1297 return PTR_ERR(tag_ops);
1301 if (dst->tag_ops != tag_ops) {
1303 "A DSA switch tree can have only one tagging protocol\n");
1305 dsa_tag_driver_put(tag_ops);
1309 /* In the case of multiple CPU ports per switch, the tagging
1310 * protocol is still reference-counted only per switch tree.
1312 dsa_tag_driver_put(tag_ops);
1314 dst->tag_ops = tag_ops;
1317 dp->master = master;
1318 dp->type = DSA_PORT_TYPE_CPU;
1319 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1322 /* At this point, the tree may be configured to use a different
1323 * tagger than the one chosen by the switch driver during
1324 * .setup, in the case when a user selects a custom protocol
1327 * This is resolved by syncing the driver with the tree in
1328 * dsa_switch_setup_tag_protocol once .setup has run and the
1329 * driver is ready to accept calls to .change_tag_protocol. If
1330 * the driver does not support the custom protocol at that
1331 * point, the tree is wholly rejected, thereby ensuring that the
1332 * tree and driver are always in agreement on the protocol to
1338 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1340 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1341 const char *name = of_get_property(dn, "label", NULL);
1342 bool link = of_property_read_bool(dn, "link");
1347 struct net_device *master;
1348 const char *user_protocol;
1350 master = of_find_net_device_by_node(ethernet);
1351 of_node_put(ethernet);
1353 return -EPROBE_DEFER;
1355 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1356 return dsa_port_parse_cpu(dp, master, user_protocol);
1360 return dsa_port_parse_dsa(dp);
1362 return dsa_port_parse_user(dp, name);
1365 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1366 struct device_node *dn)
1368 struct device_node *ports, *port;
1369 struct dsa_port *dp;
1373 ports = of_get_child_by_name(dn, "ports");
1375 /* The second possibility is "ethernet-ports" */
1376 ports = of_get_child_by_name(dn, "ethernet-ports");
1378 dev_err(ds->dev, "no ports child node found\n");
1383 for_each_available_child_of_node(ports, port) {
1384 err = of_property_read_u32(port, "reg", ®);
1390 if (reg >= ds->num_ports) {
1391 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1392 port, reg, ds->num_ports);
1398 dp = dsa_to_port(ds, reg);
1400 err = dsa_port_parse_of(dp, port);
1412 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1413 struct device_node *dn)
1415 u32 m[2] = { 0, 0 };
1418 /* Don't error out if this optional property isn't found */
1419 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1420 if (sz < 0 && sz != -EINVAL)
1425 ds->dst = dsa_tree_touch(m[0]);
1429 if (dsa_switch_find(ds->dst->index, ds->index)) {
1431 "A DSA switch with index %d already exists in tree %d\n",
1432 ds->index, ds->dst->index);
1436 if (ds->dst->last_switch < ds->index)
1437 ds->dst->last_switch = ds->index;
1442 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1444 struct dsa_port *dp;
1447 for (port = 0; port < ds->num_ports; port++) {
1448 dp = dsa_port_touch(ds, port);
1456 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1460 err = dsa_switch_parse_member_of(ds, dn);
1464 err = dsa_switch_touch_ports(ds);
1468 return dsa_switch_parse_ports_of(ds, dn);
1471 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1474 if (!strcmp(name, "cpu")) {
1475 struct net_device *master;
1477 master = dsa_dev_to_net_device(dev);
1479 return -EPROBE_DEFER;
1483 return dsa_port_parse_cpu(dp, master, NULL);
1486 if (!strcmp(name, "dsa"))
1487 return dsa_port_parse_dsa(dp);
1489 return dsa_port_parse_user(dp, name);
1492 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1493 struct dsa_chip_data *cd)
1495 bool valid_name_found = false;
1496 struct dsa_port *dp;
1502 for (i = 0; i < DSA_MAX_PORTS; i++) {
1503 name = cd->port_names[i];
1504 dev = cd->netdev[i];
1505 dp = dsa_to_port(ds, i);
1510 err = dsa_port_parse(dp, name, dev);
1514 valid_name_found = true;
1517 if (!valid_name_found && i == DSA_MAX_PORTS)
1523 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1529 /* We don't support interconnected switches nor multiple trees via
1530 * platform data, so this is the unique switch of the tree.
1533 ds->dst = dsa_tree_touch(0);
1537 err = dsa_switch_touch_ports(ds);
1541 return dsa_switch_parse_ports(ds, cd);
1544 static void dsa_switch_release_ports(struct dsa_switch *ds)
1546 struct dsa_switch_tree *dst = ds->dst;
1547 struct dsa_port *dp, *next;
1549 list_for_each_entry_safe(dp, next, &dst->ports, list) {
1552 list_del(&dp->list);
1557 static int dsa_switch_probe(struct dsa_switch *ds)
1559 struct dsa_switch_tree *dst;
1560 struct dsa_chip_data *pdata;
1561 struct device_node *np;
1567 pdata = ds->dev->platform_data;
1568 np = ds->dev->of_node;
1574 err = dsa_switch_parse_of(ds, np);
1576 dsa_switch_release_ports(ds);
1578 err = dsa_switch_parse(ds, pdata);
1580 dsa_switch_release_ports(ds);
1590 err = dsa_tree_setup(dst);
1592 dsa_switch_release_ports(ds);
1599 int dsa_register_switch(struct dsa_switch *ds)
1603 mutex_lock(&dsa2_mutex);
1604 err = dsa_switch_probe(ds);
1605 dsa_tree_put(ds->dst);
1606 mutex_unlock(&dsa2_mutex);
1610 EXPORT_SYMBOL_GPL(dsa_register_switch);
1612 static void dsa_switch_remove(struct dsa_switch *ds)
1614 struct dsa_switch_tree *dst = ds->dst;
1616 dsa_tree_teardown(dst);
1617 dsa_switch_release_ports(ds);
1621 void dsa_unregister_switch(struct dsa_switch *ds)
1623 mutex_lock(&dsa2_mutex);
1624 dsa_switch_remove(ds);
1625 mutex_unlock(&dsa2_mutex);
1627 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1629 /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1630 * blocking that operation from completion, due to the dev_hold taken inside
1631 * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1632 * the DSA master, so that the system can reboot successfully.
1634 void dsa_switch_shutdown(struct dsa_switch *ds)
1636 struct net_device *master, *slave_dev;
1637 LIST_HEAD(unregister_list);
1638 struct dsa_port *dp;
1640 mutex_lock(&dsa2_mutex);
1647 list_for_each_entry(dp, &ds->dst->ports, list) {
1651 if (!dsa_port_is_user(dp))
1654 master = dp->cpu_dp->master;
1655 slave_dev = dp->slave;
1657 netdev_upper_dev_unlink(master, slave_dev);
1658 /* Just unlinking ourselves as uppers of the master is not
1659 * sufficient. When the master net device unregisters, that will
1660 * also call dev_close, which we will catch as NETDEV_GOING_DOWN
1661 * and trigger a dev_close on our own devices (dsa_slave_close).
1662 * In turn, that will call dev_mc_unsync on the master's net
1663 * device. If the master is also a DSA switch port, this will
1664 * trigger dsa_slave_set_rx_mode which will call dev_mc_sync on
1665 * its own master. Lockdep will complain about the fact that
1666 * all cascaded masters have the same dsa_master_addr_list_lock_key,
1667 * which it normally would not do if the cascaded masters would
1668 * be in a proper upper/lower relationship, which we've just
1670 * To suppress the lockdep warnings, let's actually unregister
1671 * the DSA slave interfaces too, to avoid the nonsensical
1672 * multicast address list synchronization on shutdown.
1674 unregister_netdevice_queue(slave_dev, &unregister_list);
1676 unregister_netdevice_many(&unregister_list);
1680 mutex_unlock(&dsa2_mutex);
1682 EXPORT_SYMBOL_GPL(dsa_switch_shutdown);