1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4 * Copyright (c) 2008-2009 Marvell Semiconductor
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/rtnetlink.h>
16 #include <linux/of_net.h>
17 #include <net/devlink.h>
18 #include <net/sch_generic.h>
22 static DEFINE_MUTEX(dsa2_mutex);
23 LIST_HEAD(dsa_tree_list);
25 /* Track the bridges with forwarding offload enabled */
26 static unsigned long dsa_fwd_offloading_bridges;
29 * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
30 * @dst: collection of struct dsa_switch devices to notify.
31 * @e: event, must be of type DSA_NOTIFIER_*
32 * @v: event-specific value.
34 * Given a struct dsa_switch_tree, this can be used to run a function once for
35 * each member DSA switch. The other alternative of traversing the tree is only
36 * through its ports list, which does not uniquely list the switches.
38 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
40 struct raw_notifier_head *nh = &dst->nh;
43 err = raw_notifier_call_chain(nh, e, v);
45 return notifier_to_errno(err);
49 * dsa_broadcast - Notify all DSA trees in the system.
50 * @e: event, must be of type DSA_NOTIFIER_*
51 * @v: event-specific value.
53 * Can be used to notify the switching fabric of events such as cross-chip
54 * bridging between disjoint trees (such as islands of tagger-compatible
55 * switches bridged by an incompatible middle switch).
57 * WARNING: this function is not reliable during probe time, because probing
58 * between trees is asynchronous and not all DSA trees might have probed.
60 int dsa_broadcast(unsigned long e, void *v)
62 struct dsa_switch_tree *dst;
65 list_for_each_entry(dst, &dsa_tree_list, list) {
66 err = dsa_tree_notify(dst, e, v);
75 * dsa_lag_map() - Map LAG structure to a linear LAG array
76 * @dst: Tree in which to record the mapping.
77 * @lag: LAG structure that is to be mapped to the tree's array.
79 * dsa_lag_id/dsa_lag_by_id can then be used to translate between the
80 * two spaces. The size of the mapping space is determined by the
81 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
82 * it unset if it is not needed, in which case these functions become
85 void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag)
89 for (id = 1; id <= dst->lags_len; id++) {
90 if (!dsa_lag_by_id(dst, id)) {
91 dst->lags[id - 1] = lag;
97 /* No IDs left, which is OK. Some drivers do not need it. The
98 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
99 * returns an error for this device when joining the LAG. The
100 * driver can then return -EOPNOTSUPP back to DSA, which will
101 * fall back to a software LAG.
106 * dsa_lag_unmap() - Remove a LAG ID mapping
107 * @dst: Tree in which the mapping is recorded.
108 * @lag: LAG structure that was mapped.
110 * As there may be multiple users of the mapping, it is only removed
111 * if there are no other references to it.
113 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag)
117 dsa_lags_foreach_id(id, dst) {
118 if (dsa_lag_by_id(dst, id) == lag) {
119 dst->lags[id - 1] = NULL;
126 struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
127 const struct net_device *lag_dev)
131 list_for_each_entry(dp, &dst->ports, list)
132 if (dsa_port_lag_dev_get(dp) == lag_dev)
138 struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
139 const struct net_device *br)
143 list_for_each_entry(dp, &dst->ports, list)
144 if (dsa_port_bridge_dev_get(dp) == br)
150 static int dsa_bridge_num_find(const struct net_device *bridge_dev)
152 struct dsa_switch_tree *dst;
154 list_for_each_entry(dst, &dsa_tree_list, list) {
155 struct dsa_bridge *bridge;
157 bridge = dsa_tree_bridge_find(dst, bridge_dev);
165 unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
167 unsigned int bridge_num = dsa_bridge_num_find(bridge_dev);
169 /* Switches without FDB isolation support don't get unique
176 /* First port that requests FDB isolation or TX forwarding
177 * offload for this bridge
179 bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges,
180 DSA_MAX_NUM_OFFLOADING_BRIDGES,
182 if (bridge_num >= max)
185 set_bit(bridge_num, &dsa_fwd_offloading_bridges);
191 void dsa_bridge_num_put(const struct net_device *bridge_dev,
192 unsigned int bridge_num)
194 /* Since we refcount bridges, we know that when we call this function
195 * it is no longer in use, so we can just go ahead and remove it from
198 clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
201 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
203 struct dsa_switch_tree *dst;
206 list_for_each_entry(dst, &dsa_tree_list, list) {
207 if (dst->index != tree_index)
210 list_for_each_entry(dp, &dst->ports, list) {
211 if (dp->ds->index != sw_index)
220 EXPORT_SYMBOL_GPL(dsa_switch_find);
222 static struct dsa_switch_tree *dsa_tree_find(int index)
224 struct dsa_switch_tree *dst;
226 list_for_each_entry(dst, &dsa_tree_list, list)
227 if (dst->index == index)
233 static struct dsa_switch_tree *dsa_tree_alloc(int index)
235 struct dsa_switch_tree *dst;
237 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
243 INIT_LIST_HEAD(&dst->rtable);
245 INIT_LIST_HEAD(&dst->ports);
247 INIT_LIST_HEAD(&dst->list);
248 list_add_tail(&dst->list, &dsa_tree_list);
250 kref_init(&dst->refcount);
255 static void dsa_tree_free(struct dsa_switch_tree *dst)
258 dsa_tag_driver_put(dst->tag_ops);
259 list_del(&dst->list);
263 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
266 kref_get(&dst->refcount);
271 static struct dsa_switch_tree *dsa_tree_touch(int index)
273 struct dsa_switch_tree *dst;
275 dst = dsa_tree_find(index);
277 return dsa_tree_get(dst);
279 return dsa_tree_alloc(index);
282 static void dsa_tree_release(struct kref *ref)
284 struct dsa_switch_tree *dst;
286 dst = container_of(ref, struct dsa_switch_tree, refcount);
291 static void dsa_tree_put(struct dsa_switch_tree *dst)
294 kref_put(&dst->refcount, dsa_tree_release);
297 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
298 struct device_node *dn)
302 list_for_each_entry(dp, &dst->ports, list)
309 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
310 struct dsa_port *link_dp)
312 struct dsa_switch *ds = dp->ds;
313 struct dsa_switch_tree *dst;
318 list_for_each_entry(dl, &dst->rtable, list)
319 if (dl->dp == dp && dl->link_dp == link_dp)
322 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
327 dl->link_dp = link_dp;
329 INIT_LIST_HEAD(&dl->list);
330 list_add_tail(&dl->list, &dst->rtable);
335 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
337 struct dsa_switch *ds = dp->ds;
338 struct dsa_switch_tree *dst = ds->dst;
339 struct device_node *dn = dp->dn;
340 struct of_phandle_iterator it;
341 struct dsa_port *link_dp;
345 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
346 link_dp = dsa_tree_find_port_by_node(dst, it.node);
348 of_node_put(it.node);
352 dl = dsa_link_touch(dp, link_dp);
354 of_node_put(it.node);
362 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
364 bool complete = true;
367 list_for_each_entry(dp, &dst->ports, list) {
368 if (dsa_port_is_dsa(dp)) {
369 complete = dsa_port_setup_routing_table(dp);
378 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
382 list_for_each_entry(dp, &dst->ports, list)
383 if (dsa_port_is_cpu(dp))
389 /* Assign the default CPU port (the first one in the tree) to all ports of the
390 * fabric which don't already have one as part of their own switch.
392 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
394 struct dsa_port *cpu_dp, *dp;
396 cpu_dp = dsa_tree_find_first_cpu(dst);
398 pr_err("DSA: tree %d has no CPU port\n", dst->index);
402 list_for_each_entry(dp, &dst->ports, list) {
406 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
413 /* Perform initial assignment of CPU ports to user ports and DSA links in the
414 * fabric, giving preference to CPU ports local to each switch. Default to
415 * using the first CPU port in the switch tree if the port does not have a CPU
416 * port local to this switch.
418 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
420 struct dsa_port *cpu_dp, *dp;
422 list_for_each_entry(cpu_dp, &dst->ports, list) {
423 if (!dsa_port_is_cpu(cpu_dp))
426 /* Prefer a local CPU port */
427 dsa_switch_for_each_port(dp, cpu_dp->ds) {
428 /* Prefer the first local CPU port found */
432 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
437 return dsa_tree_setup_default_cpu(dst);
440 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
444 list_for_each_entry(dp, &dst->ports, list)
445 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
449 static int dsa_port_setup(struct dsa_port *dp)
451 struct devlink_port *dlp = &dp->devlink_port;
452 bool dsa_port_link_registered = false;
453 struct dsa_switch *ds = dp->ds;
454 bool dsa_port_enabled = false;
460 if (ds->ops->port_setup) {
461 err = ds->ops->port_setup(ds, dp->index);
467 case DSA_PORT_TYPE_UNUSED:
468 dsa_port_disable(dp);
470 case DSA_PORT_TYPE_CPU:
471 err = dsa_port_link_register_of(dp);
474 dsa_port_link_registered = true;
476 err = dsa_port_enable(dp, NULL);
479 dsa_port_enabled = true;
482 case DSA_PORT_TYPE_DSA:
483 err = dsa_port_link_register_of(dp);
486 dsa_port_link_registered = true;
488 err = dsa_port_enable(dp, NULL);
491 dsa_port_enabled = true;
494 case DSA_PORT_TYPE_USER:
495 of_get_mac_address(dp->dn, dp->mac);
496 err = dsa_slave_create(dp);
500 devlink_port_type_eth_set(dlp, dp->slave);
504 if (err && dsa_port_enabled)
505 dsa_port_disable(dp);
506 if (err && dsa_port_link_registered)
507 dsa_port_link_unregister_of(dp);
509 if (ds->ops->port_teardown)
510 ds->ops->port_teardown(ds, dp->index);
519 static int dsa_port_devlink_setup(struct dsa_port *dp)
521 struct devlink_port *dlp = &dp->devlink_port;
522 struct dsa_switch_tree *dst = dp->ds->dst;
523 struct devlink_port_attrs attrs = {};
524 struct devlink *dl = dp->ds->devlink;
525 const unsigned char *id;
529 id = (const unsigned char *)&dst->index;
530 len = sizeof(dst->index);
532 attrs.phys.port_number = dp->index;
533 memcpy(attrs.switch_id.id, id, len);
534 attrs.switch_id.id_len = len;
535 memset(dlp, 0, sizeof(*dlp));
538 case DSA_PORT_TYPE_UNUSED:
539 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
541 case DSA_PORT_TYPE_CPU:
542 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
544 case DSA_PORT_TYPE_DSA:
545 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
547 case DSA_PORT_TYPE_USER:
548 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
552 devlink_port_attrs_set(dlp, &attrs);
553 err = devlink_port_register(dl, dlp, dp->index);
556 dp->devlink_port_setup = true;
561 static void dsa_port_teardown(struct dsa_port *dp)
563 struct devlink_port *dlp = &dp->devlink_port;
564 struct dsa_switch *ds = dp->ds;
565 struct net_device *slave;
570 if (ds->ops->port_teardown)
571 ds->ops->port_teardown(ds, dp->index);
573 devlink_port_type_clear(dlp);
576 case DSA_PORT_TYPE_UNUSED:
578 case DSA_PORT_TYPE_CPU:
579 dsa_port_disable(dp);
580 dsa_port_link_unregister_of(dp);
582 case DSA_PORT_TYPE_DSA:
583 dsa_port_disable(dp);
584 dsa_port_link_unregister_of(dp);
586 case DSA_PORT_TYPE_USER:
591 dsa_slave_destroy(slave);
599 static void dsa_port_devlink_teardown(struct dsa_port *dp)
601 struct devlink_port *dlp = &dp->devlink_port;
603 if (dp->devlink_port_setup)
604 devlink_port_unregister(dlp);
605 dp->devlink_port_setup = false;
608 /* Destroy the current devlink port, and create a new one which has the UNUSED
609 * flavour. At this point, any call to ds->ops->port_setup has been already
610 * balanced out by a call to ds->ops->port_teardown, so we know that any
611 * devlink port regions the driver had are now unregistered. We then call its
612 * ds->ops->port_setup again, in order for the driver to re-create them on the
615 static int dsa_port_reinit_as_unused(struct dsa_port *dp)
617 struct dsa_switch *ds = dp->ds;
620 dsa_port_devlink_teardown(dp);
621 dp->type = DSA_PORT_TYPE_UNUSED;
622 err = dsa_port_devlink_setup(dp);
626 if (ds->ops->port_setup) {
627 /* On error, leave the devlink port registered,
628 * dsa_switch_teardown will clean it up later.
630 err = ds->ops->port_setup(ds, dp->index);
638 static int dsa_devlink_info_get(struct devlink *dl,
639 struct devlink_info_req *req,
640 struct netlink_ext_ack *extack)
642 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
644 if (ds->ops->devlink_info_get)
645 return ds->ops->devlink_info_get(ds, req, extack);
650 static int dsa_devlink_sb_pool_get(struct devlink *dl,
651 unsigned int sb_index, u16 pool_index,
652 struct devlink_sb_pool_info *pool_info)
654 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
656 if (!ds->ops->devlink_sb_pool_get)
659 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
663 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
664 u16 pool_index, u32 size,
665 enum devlink_sb_threshold_type threshold_type,
666 struct netlink_ext_ack *extack)
668 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
670 if (!ds->ops->devlink_sb_pool_set)
673 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
674 threshold_type, extack);
677 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
678 unsigned int sb_index, u16 pool_index,
681 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
682 int port = dsa_devlink_port_to_port(dlp);
684 if (!ds->ops->devlink_sb_port_pool_get)
687 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
688 pool_index, p_threshold);
691 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
692 unsigned int sb_index, u16 pool_index,
694 struct netlink_ext_ack *extack)
696 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
697 int port = dsa_devlink_port_to_port(dlp);
699 if (!ds->ops->devlink_sb_port_pool_set)
702 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
703 pool_index, threshold, extack);
707 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
708 unsigned int sb_index, u16 tc_index,
709 enum devlink_sb_pool_type pool_type,
710 u16 *p_pool_index, u32 *p_threshold)
712 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
713 int port = dsa_devlink_port_to_port(dlp);
715 if (!ds->ops->devlink_sb_tc_pool_bind_get)
718 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
720 p_pool_index, p_threshold);
724 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
725 unsigned int sb_index, u16 tc_index,
726 enum devlink_sb_pool_type pool_type,
727 u16 pool_index, u32 threshold,
728 struct netlink_ext_ack *extack)
730 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
731 int port = dsa_devlink_port_to_port(dlp);
733 if (!ds->ops->devlink_sb_tc_pool_bind_set)
736 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
738 pool_index, threshold,
742 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
743 unsigned int sb_index)
745 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
747 if (!ds->ops->devlink_sb_occ_snapshot)
750 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
753 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
754 unsigned int sb_index)
756 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
758 if (!ds->ops->devlink_sb_occ_max_clear)
761 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
764 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
765 unsigned int sb_index,
766 u16 pool_index, u32 *p_cur,
769 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
770 int port = dsa_devlink_port_to_port(dlp);
772 if (!ds->ops->devlink_sb_occ_port_pool_get)
775 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
776 pool_index, p_cur, p_max);
780 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
781 unsigned int sb_index, u16 tc_index,
782 enum devlink_sb_pool_type pool_type,
783 u32 *p_cur, u32 *p_max)
785 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
786 int port = dsa_devlink_port_to_port(dlp);
788 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
791 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
797 static const struct devlink_ops dsa_devlink_ops = {
798 .info_get = dsa_devlink_info_get,
799 .sb_pool_get = dsa_devlink_sb_pool_get,
800 .sb_pool_set = dsa_devlink_sb_pool_set,
801 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
802 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
803 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
804 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
805 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
806 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
807 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
808 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
811 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
813 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
814 struct dsa_switch_tree *dst = ds->dst;
815 struct dsa_port *cpu_dp;
818 if (tag_ops->proto == dst->default_proto)
821 dsa_switch_for_each_cpu_port(cpu_dp, ds) {
823 err = ds->ops->change_tag_protocol(ds, cpu_dp->index,
827 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
828 tag_ops->name, ERR_PTR(err));
834 if (tag_ops->connect) {
835 err = tag_ops->connect(ds);
840 if (ds->ops->connect_tag_protocol) {
841 err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
844 "Unable to connect to tag protocol \"%s\": %pe\n",
845 tag_ops->name, ERR_PTR(err));
853 if (tag_ops->disconnect)
854 tag_ops->disconnect(ds);
859 static int dsa_switch_setup(struct dsa_switch *ds)
861 struct dsa_devlink_priv *dl_priv;
868 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
869 * driver and before ops->setup() has run, since the switch drivers and
870 * the slave MDIO bus driver rely on these values for probing PHY
873 ds->phys_mii_mask |= dsa_user_ports(ds);
875 /* Add the switch to devlink before calling setup, so that setup can
879 devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
882 dl_priv = devlink_priv(ds->devlink);
885 /* Setup devlink port instances now, so that the switch
886 * setup() can register regions etc, against the ports
888 dsa_switch_for_each_port(dp, ds) {
889 err = dsa_port_devlink_setup(dp);
891 goto unregister_devlink_ports;
894 err = dsa_switch_register_notifier(ds);
896 goto unregister_devlink_ports;
898 ds->configure_vlan_while_not_filtering = true;
900 err = ds->ops->setup(ds);
902 goto unregister_notifier;
904 err = dsa_switch_setup_tag_protocol(ds);
908 if (!ds->slave_mii_bus && ds->ops->phy_read) {
909 ds->slave_mii_bus = mdiobus_alloc();
910 if (!ds->slave_mii_bus) {
915 dsa_slave_mii_bus_init(ds);
917 err = mdiobus_register(ds->slave_mii_bus);
919 goto free_slave_mii_bus;
923 devlink_register(ds->devlink);
927 if (ds->slave_mii_bus && ds->ops->phy_read)
928 mdiobus_free(ds->slave_mii_bus);
930 if (ds->ops->teardown)
931 ds->ops->teardown(ds);
933 dsa_switch_unregister_notifier(ds);
934 unregister_devlink_ports:
935 dsa_switch_for_each_port(dp, ds)
936 dsa_port_devlink_teardown(dp);
937 devlink_free(ds->devlink);
942 static void dsa_switch_teardown(struct dsa_switch *ds)
950 devlink_unregister(ds->devlink);
952 if (ds->slave_mii_bus && ds->ops->phy_read) {
953 mdiobus_unregister(ds->slave_mii_bus);
954 mdiobus_free(ds->slave_mii_bus);
955 ds->slave_mii_bus = NULL;
958 if (ds->ops->teardown)
959 ds->ops->teardown(ds);
961 dsa_switch_unregister_notifier(ds);
964 dsa_switch_for_each_port(dp, ds)
965 dsa_port_devlink_teardown(dp);
966 devlink_free(ds->devlink);
973 /* First tear down the non-shared, then the shared ports. This ensures that
974 * all work items scheduled by our switchdev handlers for user ports have
975 * completed before we destroy the refcounting kept on the shared ports.
977 static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
981 list_for_each_entry(dp, &dst->ports, list)
982 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
983 dsa_port_teardown(dp);
985 dsa_flush_workqueue();
987 list_for_each_entry(dp, &dst->ports, list)
988 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
989 dsa_port_teardown(dp);
992 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
996 list_for_each_entry(dp, &dst->ports, list)
997 dsa_switch_teardown(dp->ds);
1000 /* Bring shared ports up first, then non-shared ports */
1001 static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
1003 struct dsa_port *dp;
1006 list_for_each_entry(dp, &dst->ports, list) {
1007 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
1008 err = dsa_port_setup(dp);
1014 list_for_each_entry(dp, &dst->ports, list) {
1015 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
1016 err = dsa_port_setup(dp);
1018 err = dsa_port_reinit_as_unused(dp);
1028 dsa_tree_teardown_ports(dst);
1033 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
1035 struct dsa_port *dp;
1038 list_for_each_entry(dp, &dst->ports, list) {
1039 err = dsa_switch_setup(dp->ds);
1041 dsa_tree_teardown_switches(dst);
1049 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
1051 struct dsa_port *dp;
1056 list_for_each_entry(dp, &dst->ports, list) {
1057 if (dsa_port_is_cpu(dp)) {
1058 struct net_device *master = dp->master;
1059 bool admin_up = (master->flags & IFF_UP) &&
1060 !qdisc_tx_is_noop(master);
1062 err = dsa_master_setup(master, dp);
1066 /* Replay master state event */
1067 dsa_tree_master_admin_state_change(dst, master, admin_up);
1068 dsa_tree_master_oper_state_change(dst, master,
1069 netif_oper_up(master));
1078 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
1080 struct dsa_port *dp;
1084 list_for_each_entry(dp, &dst->ports, list) {
1085 if (dsa_port_is_cpu(dp)) {
1086 struct net_device *master = dp->master;
1088 /* Synthesizing an "admin down" state is sufficient for
1089 * the switches to get a notification if the master is
1090 * currently up and running.
1092 dsa_tree_master_admin_state_change(dst, master, false);
1094 dsa_master_teardown(master);
1101 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
1103 unsigned int len = 0;
1104 struct dsa_port *dp;
1106 list_for_each_entry(dp, &dst->ports, list) {
1107 if (dp->ds->num_lag_ids > len)
1108 len = dp->ds->num_lag_ids;
1114 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
1118 dst->lags_len = len;
1122 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
1127 static int dsa_tree_setup(struct dsa_switch_tree *dst)
1133 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
1138 complete = dsa_tree_setup_routing_table(dst);
1142 err = dsa_tree_setup_cpu_ports(dst);
1146 err = dsa_tree_setup_switches(dst);
1148 goto teardown_cpu_ports;
1150 err = dsa_tree_setup_master(dst);
1152 goto teardown_switches;
1154 err = dsa_tree_setup_ports(dst);
1156 goto teardown_master;
1158 err = dsa_tree_setup_lags(dst);
1160 goto teardown_ports;
1164 pr_info("DSA: tree %d setup\n", dst->index);
1169 dsa_tree_teardown_ports(dst);
1171 dsa_tree_teardown_master(dst);
1173 dsa_tree_teardown_switches(dst);
1175 dsa_tree_teardown_cpu_ports(dst);
1180 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
1182 struct dsa_link *dl, *next;
1187 dsa_tree_teardown_lags(dst);
1189 dsa_tree_teardown_ports(dst);
1191 dsa_tree_teardown_master(dst);
1193 dsa_tree_teardown_switches(dst);
1195 dsa_tree_teardown_cpu_ports(dst);
1197 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
1198 list_del(&dl->list);
1202 pr_info("DSA: tree %d torn down\n", dst->index);
1207 static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
1208 const struct dsa_device_ops *tag_ops)
1210 const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
1211 struct dsa_notifier_tag_proto_info info;
1214 dst->tag_ops = tag_ops;
1216 /* Notify the switches from this tree about the connection
1219 info.tag_ops = tag_ops;
1220 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
1221 if (err && err != -EOPNOTSUPP)
1222 goto out_disconnect;
1224 /* Notify the old tagger about the disconnection from this tree */
1225 info.tag_ops = old_tag_ops;
1226 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1231 info.tag_ops = tag_ops;
1232 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1233 dst->tag_ops = old_tag_ops;
1238 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
1239 * is that all DSA switches within a tree share the same tagger, otherwise
1240 * they would have formed disjoint trees (different "dsa,member" values).
1242 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
1243 struct net_device *master,
1244 const struct dsa_device_ops *tag_ops,
1245 const struct dsa_device_ops *old_tag_ops)
1247 struct dsa_notifier_tag_proto_info info;
1248 struct dsa_port *dp;
1251 if (!rtnl_trylock())
1252 return restart_syscall();
1254 /* At the moment we don't allow changing the tag protocol under
1255 * traffic. The rtnl_mutex also happens to serialize concurrent
1256 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1257 * restriction, there needs to be another mutex which serializes this.
1259 if (master->flags & IFF_UP)
1262 list_for_each_entry(dp, &dst->ports, list) {
1263 if (!dsa_port_is_user(dp))
1266 if (dp->slave->flags & IFF_UP)
1270 /* Notify the tag protocol change */
1271 info.tag_ops = tag_ops;
1272 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1274 goto out_unwind_tagger;
1276 err = dsa_tree_bind_tag_proto(dst, tag_ops);
1278 goto out_unwind_tagger;
1285 info.tag_ops = old_tag_ops;
1286 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1292 static void dsa_tree_master_state_change(struct dsa_switch_tree *dst,
1293 struct net_device *master)
1295 struct dsa_notifier_master_state_info info;
1296 struct dsa_port *cpu_dp = master->dsa_ptr;
1298 info.master = master;
1299 info.operational = dsa_port_master_is_operational(cpu_dp);
1301 dsa_tree_notify(dst, DSA_NOTIFIER_MASTER_STATE_CHANGE, &info);
1304 void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
1305 struct net_device *master,
1308 struct dsa_port *cpu_dp = master->dsa_ptr;
1309 bool notify = false;
1311 if ((dsa_port_master_is_operational(cpu_dp)) !=
1312 (up && cpu_dp->master_oper_up))
1315 cpu_dp->master_admin_up = up;
1318 dsa_tree_master_state_change(dst, master);
1321 void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
1322 struct net_device *master,
1325 struct dsa_port *cpu_dp = master->dsa_ptr;
1326 bool notify = false;
1328 if ((dsa_port_master_is_operational(cpu_dp)) !=
1329 (cpu_dp->master_admin_up && up))
1332 cpu_dp->master_oper_up = up;
1335 dsa_tree_master_state_change(dst, master);
1338 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1340 struct dsa_switch_tree *dst = ds->dst;
1341 struct dsa_port *dp;
1343 dsa_switch_for_each_port(dp, ds)
1344 if (dp->index == index)
1347 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1354 mutex_init(&dp->addr_lists_lock);
1355 mutex_init(&dp->vlans_lock);
1356 INIT_LIST_HEAD(&dp->fdbs);
1357 INIT_LIST_HEAD(&dp->mdbs);
1358 INIT_LIST_HEAD(&dp->vlans);
1359 INIT_LIST_HEAD(&dp->list);
1360 list_add_tail(&dp->list, &dst->ports);
1365 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1370 dp->type = DSA_PORT_TYPE_USER;
1376 static int dsa_port_parse_dsa(struct dsa_port *dp)
1378 dp->type = DSA_PORT_TYPE_DSA;
1383 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1384 struct net_device *master)
1386 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1387 struct dsa_switch *mds, *ds = dp->ds;
1388 unsigned int mdp_upstream;
1389 struct dsa_port *mdp;
1391 /* It is possible to stack DSA switches onto one another when that
1392 * happens the switch driver may want to know if its tagging protocol
1393 * is going to work in such a configuration.
1395 if (dsa_slave_dev_check(master)) {
1396 mdp = dsa_slave_to_port(master);
1398 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1399 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1400 DSA_TAG_PROTO_NONE);
1403 /* If the master device is not itself a DSA slave in a disjoint DSA
1404 * tree, then return immediately.
1406 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1409 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1410 const char *user_protocol)
1412 struct dsa_switch *ds = dp->ds;
1413 struct dsa_switch_tree *dst = ds->dst;
1414 const struct dsa_device_ops *tag_ops;
1415 enum dsa_tag_protocol default_proto;
1417 /* Find out which protocol the switch would prefer. */
1418 default_proto = dsa_get_tag_protocol(dp, master);
1419 if (dst->default_proto) {
1420 if (dst->default_proto != default_proto) {
1422 "A DSA switch tree can have only one tagging protocol\n");
1426 dst->default_proto = default_proto;
1429 /* See if the user wants to override that preference. */
1430 if (user_protocol) {
1431 if (!ds->ops->change_tag_protocol) {
1432 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1436 tag_ops = dsa_find_tagger_by_name(user_protocol);
1438 tag_ops = dsa_tag_driver_get(default_proto);
1441 if (IS_ERR(tag_ops)) {
1442 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1443 return -EPROBE_DEFER;
1445 dev_warn(ds->dev, "No tagger for this switch\n");
1446 return PTR_ERR(tag_ops);
1450 if (dst->tag_ops != tag_ops) {
1452 "A DSA switch tree can have only one tagging protocol\n");
1454 dsa_tag_driver_put(tag_ops);
1458 /* In the case of multiple CPU ports per switch, the tagging
1459 * protocol is still reference-counted only per switch tree.
1461 dsa_tag_driver_put(tag_ops);
1463 dst->tag_ops = tag_ops;
1466 dp->master = master;
1467 dp->type = DSA_PORT_TYPE_CPU;
1468 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1471 /* At this point, the tree may be configured to use a different
1472 * tagger than the one chosen by the switch driver during
1473 * .setup, in the case when a user selects a custom protocol
1476 * This is resolved by syncing the driver with the tree in
1477 * dsa_switch_setup_tag_protocol once .setup has run and the
1478 * driver is ready to accept calls to .change_tag_protocol. If
1479 * the driver does not support the custom protocol at that
1480 * point, the tree is wholly rejected, thereby ensuring that the
1481 * tree and driver are always in agreement on the protocol to
1487 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1489 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1490 const char *name = of_get_property(dn, "label", NULL);
1491 bool link = of_property_read_bool(dn, "link");
1496 struct net_device *master;
1497 const char *user_protocol;
1499 master = of_find_net_device_by_node(ethernet);
1500 of_node_put(ethernet);
1502 return -EPROBE_DEFER;
1504 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1505 return dsa_port_parse_cpu(dp, master, user_protocol);
1509 return dsa_port_parse_dsa(dp);
1511 return dsa_port_parse_user(dp, name);
1514 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1515 struct device_node *dn)
1517 struct device_node *ports, *port;
1518 struct dsa_port *dp;
1522 ports = of_get_child_by_name(dn, "ports");
1524 /* The second possibility is "ethernet-ports" */
1525 ports = of_get_child_by_name(dn, "ethernet-ports");
1527 dev_err(ds->dev, "no ports child node found\n");
1532 for_each_available_child_of_node(ports, port) {
1533 err = of_property_read_u32(port, "reg", ®);
1539 if (reg >= ds->num_ports) {
1540 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
1541 port, reg, ds->num_ports);
1547 dp = dsa_to_port(ds, reg);
1549 err = dsa_port_parse_of(dp, port);
1561 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1562 struct device_node *dn)
1564 u32 m[2] = { 0, 0 };
1567 /* Don't error out if this optional property isn't found */
1568 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1569 if (sz < 0 && sz != -EINVAL)
1574 ds->dst = dsa_tree_touch(m[0]);
1578 if (dsa_switch_find(ds->dst->index, ds->index)) {
1580 "A DSA switch with index %d already exists in tree %d\n",
1581 ds->index, ds->dst->index);
1585 if (ds->dst->last_switch < ds->index)
1586 ds->dst->last_switch = ds->index;
1591 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1593 struct dsa_port *dp;
1596 for (port = 0; port < ds->num_ports; port++) {
1597 dp = dsa_port_touch(ds, port);
1605 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1609 err = dsa_switch_parse_member_of(ds, dn);
1613 err = dsa_switch_touch_ports(ds);
1617 return dsa_switch_parse_ports_of(ds, dn);
1620 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1623 if (!strcmp(name, "cpu")) {
1624 struct net_device *master;
1626 master = dsa_dev_to_net_device(dev);
1628 return -EPROBE_DEFER;
1632 return dsa_port_parse_cpu(dp, master, NULL);
1635 if (!strcmp(name, "dsa"))
1636 return dsa_port_parse_dsa(dp);
1638 return dsa_port_parse_user(dp, name);
1641 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1642 struct dsa_chip_data *cd)
1644 bool valid_name_found = false;
1645 struct dsa_port *dp;
1651 for (i = 0; i < DSA_MAX_PORTS; i++) {
1652 name = cd->port_names[i];
1653 dev = cd->netdev[i];
1654 dp = dsa_to_port(ds, i);
1659 err = dsa_port_parse(dp, name, dev);
1663 valid_name_found = true;
1666 if (!valid_name_found && i == DSA_MAX_PORTS)
1672 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1678 /* We don't support interconnected switches nor multiple trees via
1679 * platform data, so this is the unique switch of the tree.
1682 ds->dst = dsa_tree_touch(0);
1686 err = dsa_switch_touch_ports(ds);
1690 return dsa_switch_parse_ports(ds, cd);
1693 static void dsa_switch_release_ports(struct dsa_switch *ds)
1695 struct dsa_port *dp, *next;
1697 dsa_switch_for_each_port_safe(dp, next, ds) {
1698 WARN_ON(!list_empty(&dp->fdbs));
1699 WARN_ON(!list_empty(&dp->mdbs));
1700 WARN_ON(!list_empty(&dp->vlans));
1701 list_del(&dp->list);
1706 static int dsa_switch_probe(struct dsa_switch *ds)
1708 struct dsa_switch_tree *dst;
1709 struct dsa_chip_data *pdata;
1710 struct device_node *np;
1716 pdata = ds->dev->platform_data;
1717 np = ds->dev->of_node;
1723 err = dsa_switch_parse_of(ds, np);
1725 dsa_switch_release_ports(ds);
1727 err = dsa_switch_parse(ds, pdata);
1729 dsa_switch_release_ports(ds);
1739 err = dsa_tree_setup(dst);
1741 dsa_switch_release_ports(ds);
1748 int dsa_register_switch(struct dsa_switch *ds)
1752 mutex_lock(&dsa2_mutex);
1753 err = dsa_switch_probe(ds);
1754 dsa_tree_put(ds->dst);
1755 mutex_unlock(&dsa2_mutex);
1759 EXPORT_SYMBOL_GPL(dsa_register_switch);
1761 static void dsa_switch_remove(struct dsa_switch *ds)
1763 struct dsa_switch_tree *dst = ds->dst;
1765 dsa_tree_teardown(dst);
1766 dsa_switch_release_ports(ds);
1770 void dsa_unregister_switch(struct dsa_switch *ds)
1772 mutex_lock(&dsa2_mutex);
1773 dsa_switch_remove(ds);
1774 mutex_unlock(&dsa2_mutex);
1776 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1778 /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1779 * blocking that operation from completion, due to the dev_hold taken inside
1780 * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1781 * the DSA master, so that the system can reboot successfully.
1783 void dsa_switch_shutdown(struct dsa_switch *ds)
1785 struct net_device *master, *slave_dev;
1786 struct dsa_port *dp;
1788 mutex_lock(&dsa2_mutex);
1795 dsa_switch_for_each_user_port(dp, ds) {
1796 master = dp->cpu_dp->master;
1797 slave_dev = dp->slave;
1799 netdev_upper_dev_unlink(master, slave_dev);
1802 /* Disconnect from further netdevice notifiers on the master,
1803 * since netdev_uses_dsa() will now return false.
1805 dsa_switch_for_each_cpu_port(dp, ds)
1806 dp->master->dsa_ptr = NULL;
1810 mutex_unlock(&dsa2_mutex);
1812 EXPORT_SYMBOL_GPL(dsa_switch_shutdown);