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);
25 * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
26 * @dst: collection of struct dsa_switch devices to notify.
27 * @e: event, must be of type DSA_NOTIFIER_*
28 * @v: event-specific value.
30 * Given a struct dsa_switch_tree, this can be used to run a function once for
31 * each member DSA switch. The other alternative of traversing the tree is only
32 * through its ports list, which does not uniquely list the switches.
34 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
36 struct raw_notifier_head *nh = &dst->nh;
39 err = raw_notifier_call_chain(nh, e, v);
41 return notifier_to_errno(err);
45 * dsa_broadcast - Notify all DSA trees in the system.
46 * @e: event, must be of type DSA_NOTIFIER_*
47 * @v: event-specific value.
49 * Can be used to notify the switching fabric of events such as cross-chip
50 * bridging between disjoint trees (such as islands of tagger-compatible
51 * switches bridged by an incompatible middle switch).
53 * WARNING: this function is not reliable during probe time, because probing
54 * between trees is asynchronous and not all DSA trees might have probed.
56 int dsa_broadcast(unsigned long e, void *v)
58 struct dsa_switch_tree *dst;
61 list_for_each_entry(dst, &dsa_tree_list, list) {
62 err = dsa_tree_notify(dst, e, v);
71 * dsa_lag_map() - Map LAG netdev to a linear LAG ID
72 * @dst: Tree in which to record the mapping.
73 * @lag: Netdev that is to be mapped to an ID.
75 * dsa_lag_id/dsa_lag_dev can then be used to translate between the
76 * two spaces. The size of the mapping space is determined by the
77 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
78 * it unset if it is not needed, in which case these functions become
81 void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag)
85 if (dsa_lag_id(dst, lag) >= 0)
89 for (id = 0; id < dst->lags_len; id++) {
90 if (!dsa_lag_dev(dst, id)) {
96 /* No IDs left, which is OK. Some drivers do not need it. The
97 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
98 * returns an error for this device when joining the LAG. The
99 * driver can then return -EOPNOTSUPP back to DSA, which will
100 * fall back to a software LAG.
105 * dsa_lag_unmap() - Remove a LAG ID mapping
106 * @dst: Tree in which the mapping is recorded.
107 * @lag: Netdev that was mapped.
109 * As there may be multiple users of the mapping, it is only removed
110 * if there are no other references to it.
112 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag)
117 dsa_lag_foreach_port(dp, dst, lag)
118 /* There are remaining users of this mapping */
121 dsa_lags_foreach_id(id, dst) {
122 if (dsa_lag_dev(dst, id) == lag) {
123 dst->lags[id] = NULL;
129 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
131 struct dsa_switch_tree *dst;
134 list_for_each_entry(dst, &dsa_tree_list, list) {
135 if (dst->index != tree_index)
138 list_for_each_entry(dp, &dst->ports, list) {
139 if (dp->ds->index != sw_index)
148 EXPORT_SYMBOL_GPL(dsa_switch_find);
150 static struct dsa_switch_tree *dsa_tree_find(int index)
152 struct dsa_switch_tree *dst;
154 list_for_each_entry(dst, &dsa_tree_list, list)
155 if (dst->index == index)
161 static struct dsa_switch_tree *dsa_tree_alloc(int index)
163 struct dsa_switch_tree *dst;
165 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
171 INIT_LIST_HEAD(&dst->rtable);
173 INIT_LIST_HEAD(&dst->ports);
175 INIT_LIST_HEAD(&dst->list);
176 list_add_tail(&dst->list, &dsa_tree_list);
178 kref_init(&dst->refcount);
183 static void dsa_tree_free(struct dsa_switch_tree *dst)
186 dsa_tag_driver_put(dst->tag_ops);
187 list_del(&dst->list);
191 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
194 kref_get(&dst->refcount);
199 static struct dsa_switch_tree *dsa_tree_touch(int index)
201 struct dsa_switch_tree *dst;
203 dst = dsa_tree_find(index);
205 return dsa_tree_get(dst);
207 return dsa_tree_alloc(index);
210 static void dsa_tree_release(struct kref *ref)
212 struct dsa_switch_tree *dst;
214 dst = container_of(ref, struct dsa_switch_tree, refcount);
219 static void dsa_tree_put(struct dsa_switch_tree *dst)
222 kref_put(&dst->refcount, dsa_tree_release);
225 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
226 struct device_node *dn)
230 list_for_each_entry(dp, &dst->ports, list)
237 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
238 struct dsa_port *link_dp)
240 struct dsa_switch *ds = dp->ds;
241 struct dsa_switch_tree *dst;
246 list_for_each_entry(dl, &dst->rtable, list)
247 if (dl->dp == dp && dl->link_dp == link_dp)
250 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
255 dl->link_dp = link_dp;
257 INIT_LIST_HEAD(&dl->list);
258 list_add_tail(&dl->list, &dst->rtable);
263 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
265 struct dsa_switch *ds = dp->ds;
266 struct dsa_switch_tree *dst = ds->dst;
267 struct device_node *dn = dp->dn;
268 struct of_phandle_iterator it;
269 struct dsa_port *link_dp;
273 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
274 link_dp = dsa_tree_find_port_by_node(dst, it.node);
276 of_node_put(it.node);
280 dl = dsa_link_touch(dp, link_dp);
282 of_node_put(it.node);
290 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
292 bool complete = true;
295 list_for_each_entry(dp, &dst->ports, list) {
296 if (dsa_port_is_dsa(dp)) {
297 complete = dsa_port_setup_routing_table(dp);
306 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
310 list_for_each_entry(dp, &dst->ports, list)
311 if (dsa_port_is_cpu(dp))
317 /* Assign the default CPU port (the first one in the tree) to all ports of the
318 * fabric which don't already have one as part of their own switch.
320 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
322 struct dsa_port *cpu_dp, *dp;
324 cpu_dp = dsa_tree_find_first_cpu(dst);
326 pr_err("DSA: tree %d has no CPU port\n", dst->index);
330 list_for_each_entry(dp, &dst->ports, list) {
334 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
341 /* Perform initial assignment of CPU ports to user ports and DSA links in the
342 * fabric, giving preference to CPU ports local to each switch. Default to
343 * using the first CPU port in the switch tree if the port does not have a CPU
344 * port local to this switch.
346 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
348 struct dsa_port *cpu_dp, *dp;
350 list_for_each_entry(cpu_dp, &dst->ports, list) {
351 if (!dsa_port_is_cpu(cpu_dp))
354 list_for_each_entry(dp, &dst->ports, list) {
355 /* Prefer a local CPU port */
356 if (dp->ds != cpu_dp->ds)
359 /* Prefer the first local CPU port found */
363 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
368 return dsa_tree_setup_default_cpu(dst);
371 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
375 list_for_each_entry(dp, &dst->ports, list)
376 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
380 static int dsa_port_setup(struct dsa_port *dp)
382 struct devlink_port *dlp = &dp->devlink_port;
383 bool dsa_port_link_registered = false;
384 bool dsa_port_enabled = false;
390 INIT_LIST_HEAD(&dp->fdbs);
391 INIT_LIST_HEAD(&dp->mdbs);
394 case DSA_PORT_TYPE_UNUSED:
395 dsa_port_disable(dp);
397 case DSA_PORT_TYPE_CPU:
398 err = dsa_port_link_register_of(dp);
401 dsa_port_link_registered = true;
403 err = dsa_port_enable(dp, NULL);
406 dsa_port_enabled = true;
409 case DSA_PORT_TYPE_DSA:
410 err = dsa_port_link_register_of(dp);
413 dsa_port_link_registered = true;
415 err = dsa_port_enable(dp, NULL);
418 dsa_port_enabled = true;
421 case DSA_PORT_TYPE_USER:
422 of_get_mac_address(dp->dn, dp->mac);
423 err = dsa_slave_create(dp);
427 devlink_port_type_eth_set(dlp, dp->slave);
431 if (err && dsa_port_enabled)
432 dsa_port_disable(dp);
433 if (err && dsa_port_link_registered)
434 dsa_port_link_unregister_of(dp);
443 static int dsa_port_devlink_setup(struct dsa_port *dp)
445 struct devlink_port *dlp = &dp->devlink_port;
446 struct dsa_switch_tree *dst = dp->ds->dst;
447 struct devlink_port_attrs attrs = {};
448 struct devlink *dl = dp->ds->devlink;
449 const unsigned char *id;
453 id = (const unsigned char *)&dst->index;
454 len = sizeof(dst->index);
456 attrs.phys.port_number = dp->index;
457 memcpy(attrs.switch_id.id, id, len);
458 attrs.switch_id.id_len = len;
459 memset(dlp, 0, sizeof(*dlp));
462 case DSA_PORT_TYPE_UNUSED:
463 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
465 case DSA_PORT_TYPE_CPU:
466 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
468 case DSA_PORT_TYPE_DSA:
469 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
471 case DSA_PORT_TYPE_USER:
472 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
476 devlink_port_attrs_set(dlp, &attrs);
477 err = devlink_port_register(dl, dlp, dp->index);
480 dp->devlink_port_setup = true;
485 static void dsa_port_teardown(struct dsa_port *dp)
487 struct devlink_port *dlp = &dp->devlink_port;
488 struct dsa_mac_addr *a, *tmp;
493 devlink_port_type_clear(dlp);
496 case DSA_PORT_TYPE_UNUSED:
498 case DSA_PORT_TYPE_CPU:
499 dsa_port_disable(dp);
500 dsa_port_link_unregister_of(dp);
502 case DSA_PORT_TYPE_DSA:
503 dsa_port_disable(dp);
504 dsa_port_link_unregister_of(dp);
506 case DSA_PORT_TYPE_USER:
508 dsa_slave_destroy(dp->slave);
514 list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
519 list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
527 static void dsa_port_devlink_teardown(struct dsa_port *dp)
529 struct devlink_port *dlp = &dp->devlink_port;
531 if (dp->devlink_port_setup)
532 devlink_port_unregister(dlp);
533 dp->devlink_port_setup = false;
536 static int dsa_devlink_info_get(struct devlink *dl,
537 struct devlink_info_req *req,
538 struct netlink_ext_ack *extack)
540 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
542 if (ds->ops->devlink_info_get)
543 return ds->ops->devlink_info_get(ds, req, extack);
548 static int dsa_devlink_sb_pool_get(struct devlink *dl,
549 unsigned int sb_index, u16 pool_index,
550 struct devlink_sb_pool_info *pool_info)
552 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
554 if (!ds->ops->devlink_sb_pool_get)
557 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
561 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
562 u16 pool_index, u32 size,
563 enum devlink_sb_threshold_type threshold_type,
564 struct netlink_ext_ack *extack)
566 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
568 if (!ds->ops->devlink_sb_pool_set)
571 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
572 threshold_type, extack);
575 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
576 unsigned int sb_index, u16 pool_index,
579 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
580 int port = dsa_devlink_port_to_port(dlp);
582 if (!ds->ops->devlink_sb_port_pool_get)
585 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
586 pool_index, p_threshold);
589 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
590 unsigned int sb_index, u16 pool_index,
592 struct netlink_ext_ack *extack)
594 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
595 int port = dsa_devlink_port_to_port(dlp);
597 if (!ds->ops->devlink_sb_port_pool_set)
600 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
601 pool_index, threshold, extack);
605 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
606 unsigned int sb_index, u16 tc_index,
607 enum devlink_sb_pool_type pool_type,
608 u16 *p_pool_index, u32 *p_threshold)
610 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
611 int port = dsa_devlink_port_to_port(dlp);
613 if (!ds->ops->devlink_sb_tc_pool_bind_get)
616 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
618 p_pool_index, p_threshold);
622 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
623 unsigned int sb_index, u16 tc_index,
624 enum devlink_sb_pool_type pool_type,
625 u16 pool_index, u32 threshold,
626 struct netlink_ext_ack *extack)
628 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
629 int port = dsa_devlink_port_to_port(dlp);
631 if (!ds->ops->devlink_sb_tc_pool_bind_set)
634 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
636 pool_index, threshold,
640 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
641 unsigned int sb_index)
643 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
645 if (!ds->ops->devlink_sb_occ_snapshot)
648 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
651 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
652 unsigned int sb_index)
654 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
656 if (!ds->ops->devlink_sb_occ_max_clear)
659 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
662 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
663 unsigned int sb_index,
664 u16 pool_index, u32 *p_cur,
667 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
668 int port = dsa_devlink_port_to_port(dlp);
670 if (!ds->ops->devlink_sb_occ_port_pool_get)
673 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
674 pool_index, p_cur, p_max);
678 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
679 unsigned int sb_index, u16 tc_index,
680 enum devlink_sb_pool_type pool_type,
681 u32 *p_cur, u32 *p_max)
683 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
684 int port = dsa_devlink_port_to_port(dlp);
686 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
689 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
695 static const struct devlink_ops dsa_devlink_ops = {
696 .info_get = dsa_devlink_info_get,
697 .sb_pool_get = dsa_devlink_sb_pool_get,
698 .sb_pool_set = dsa_devlink_sb_pool_set,
699 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
700 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
701 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
702 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
703 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
704 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
705 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
706 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
709 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
711 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
712 struct dsa_switch_tree *dst = ds->dst;
715 if (tag_ops->proto == dst->default_proto)
718 for (port = 0; port < ds->num_ports; port++) {
719 if (!dsa_is_cpu_port(ds, port))
722 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
724 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
725 tag_ops->name, ERR_PTR(err));
733 static int dsa_switch_setup(struct dsa_switch *ds)
735 struct dsa_devlink_priv *dl_priv;
742 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
743 * driver and before ops->setup() has run, since the switch drivers and
744 * the slave MDIO bus driver rely on these values for probing PHY
747 ds->phys_mii_mask |= dsa_user_ports(ds);
749 /* Add the switch to devlink before calling setup, so that setup can
753 devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
756 dl_priv = devlink_priv(ds->devlink);
759 err = devlink_register(ds->devlink);
763 /* Setup devlink port instances now, so that the switch
764 * setup() can register regions etc, against the ports
766 list_for_each_entry(dp, &ds->dst->ports, list) {
768 err = dsa_port_devlink_setup(dp);
770 goto unregister_devlink_ports;
774 err = dsa_switch_register_notifier(ds);
776 goto unregister_devlink_ports;
778 ds->configure_vlan_while_not_filtering = true;
780 err = ds->ops->setup(ds);
782 goto unregister_notifier;
784 err = dsa_switch_setup_tag_protocol(ds);
788 devlink_params_publish(ds->devlink);
790 if (!ds->slave_mii_bus && ds->ops->phy_read) {
791 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
792 if (!ds->slave_mii_bus) {
797 dsa_slave_mii_bus_init(ds);
799 err = mdiobus_register(ds->slave_mii_bus);
809 if (ds->ops->teardown)
810 ds->ops->teardown(ds);
812 dsa_switch_unregister_notifier(ds);
813 unregister_devlink_ports:
814 list_for_each_entry(dp, &ds->dst->ports, list)
816 dsa_port_devlink_teardown(dp);
817 devlink_unregister(ds->devlink);
819 devlink_free(ds->devlink);
825 static void dsa_switch_teardown(struct dsa_switch *ds)
832 if (ds->slave_mii_bus && ds->ops->phy_read)
833 mdiobus_unregister(ds->slave_mii_bus);
835 dsa_switch_unregister_notifier(ds);
837 if (ds->ops->teardown)
838 ds->ops->teardown(ds);
841 list_for_each_entry(dp, &ds->dst->ports, list)
843 dsa_port_devlink_teardown(dp);
844 devlink_unregister(ds->devlink);
845 devlink_free(ds->devlink);
852 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
857 list_for_each_entry(dp, &dst->ports, list) {
858 err = dsa_switch_setup(dp->ds);
863 list_for_each_entry(dp, &dst->ports, list) {
864 err = dsa_port_setup(dp);
866 dsa_port_devlink_teardown(dp);
867 dp->type = DSA_PORT_TYPE_UNUSED;
868 err = dsa_port_devlink_setup(dp);
878 list_for_each_entry(dp, &dst->ports, list)
879 dsa_port_teardown(dp);
881 list_for_each_entry(dp, &dst->ports, list)
882 dsa_switch_teardown(dp->ds);
887 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
891 list_for_each_entry(dp, &dst->ports, list)
892 dsa_port_teardown(dp);
894 list_for_each_entry(dp, &dst->ports, list)
895 dsa_switch_teardown(dp->ds);
898 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
903 list_for_each_entry(dp, &dst->ports, list) {
904 if (dsa_port_is_cpu(dp)) {
905 err = dsa_master_setup(dp->master, dp);
914 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
918 list_for_each_entry(dp, &dst->ports, list)
919 if (dsa_port_is_cpu(dp))
920 dsa_master_teardown(dp->master);
923 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
925 unsigned int len = 0;
928 list_for_each_entry(dp, &dst->ports, list) {
929 if (dp->ds->num_lag_ids > len)
930 len = dp->ds->num_lag_ids;
936 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
944 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
949 static int dsa_tree_setup(struct dsa_switch_tree *dst)
955 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
960 complete = dsa_tree_setup_routing_table(dst);
964 err = dsa_tree_setup_cpu_ports(dst);
968 err = dsa_tree_setup_switches(dst);
970 goto teardown_cpu_ports;
972 err = dsa_tree_setup_master(dst);
974 goto teardown_switches;
976 err = dsa_tree_setup_lags(dst);
978 goto teardown_master;
982 pr_info("DSA: tree %d setup\n", dst->index);
987 dsa_tree_teardown_master(dst);
989 dsa_tree_teardown_switches(dst);
991 dsa_tree_teardown_cpu_ports(dst);
996 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
998 struct dsa_link *dl, *next;
1003 dsa_tree_teardown_lags(dst);
1005 dsa_tree_teardown_master(dst);
1007 dsa_tree_teardown_switches(dst);
1009 dsa_tree_teardown_cpu_ports(dst);
1011 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
1012 list_del(&dl->list);
1016 pr_info("DSA: tree %d torn down\n", dst->index);
1021 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
1022 * is that all DSA switches within a tree share the same tagger, otherwise
1023 * they would have formed disjoint trees (different "dsa,member" values).
1025 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
1026 struct net_device *master,
1027 const struct dsa_device_ops *tag_ops,
1028 const struct dsa_device_ops *old_tag_ops)
1030 struct dsa_notifier_tag_proto_info info;
1031 struct dsa_port *dp;
1034 if (!rtnl_trylock())
1035 return restart_syscall();
1037 /* At the moment we don't allow changing the tag protocol under
1038 * traffic. The rtnl_mutex also happens to serialize concurrent
1039 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1040 * restriction, there needs to be another mutex which serializes this.
1042 if (master->flags & IFF_UP)
1045 list_for_each_entry(dp, &dst->ports, list) {
1046 if (!dsa_is_user_port(dp->ds, dp->index))
1049 if (dp->slave->flags & IFF_UP)
1053 info.tag_ops = tag_ops;
1054 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1056 goto out_unwind_tagger;
1058 dst->tag_ops = tag_ops;
1065 info.tag_ops = old_tag_ops;
1066 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1072 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1074 struct dsa_switch_tree *dst = ds->dst;
1075 struct dsa_port *dp;
1077 list_for_each_entry(dp, &dst->ports, list)
1078 if (dp->ds == ds && dp->index == index)
1081 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1087 dp->bridge_num = -1;
1089 INIT_LIST_HEAD(&dp->list);
1090 list_add_tail(&dp->list, &dst->ports);
1095 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1100 dp->type = DSA_PORT_TYPE_USER;
1106 static int dsa_port_parse_dsa(struct dsa_port *dp)
1108 dp->type = DSA_PORT_TYPE_DSA;
1113 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1114 struct net_device *master)
1116 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1117 struct dsa_switch *mds, *ds = dp->ds;
1118 unsigned int mdp_upstream;
1119 struct dsa_port *mdp;
1121 /* It is possible to stack DSA switches onto one another when that
1122 * happens the switch driver may want to know if its tagging protocol
1123 * is going to work in such a configuration.
1125 if (dsa_slave_dev_check(master)) {
1126 mdp = dsa_slave_to_port(master);
1128 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1129 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1130 DSA_TAG_PROTO_NONE);
1133 /* If the master device is not itself a DSA slave in a disjoint DSA
1134 * tree, then return immediately.
1136 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1139 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1140 const char *user_protocol)
1142 struct dsa_switch *ds = dp->ds;
1143 struct dsa_switch_tree *dst = ds->dst;
1144 const struct dsa_device_ops *tag_ops;
1145 enum dsa_tag_protocol default_proto;
1147 /* Find out which protocol the switch would prefer. */
1148 default_proto = dsa_get_tag_protocol(dp, master);
1149 if (dst->default_proto) {
1150 if (dst->default_proto != default_proto) {
1152 "A DSA switch tree can have only one tagging protocol\n");
1156 dst->default_proto = default_proto;
1159 /* See if the user wants to override that preference. */
1160 if (user_protocol) {
1161 if (!ds->ops->change_tag_protocol) {
1162 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1166 tag_ops = dsa_find_tagger_by_name(user_protocol);
1168 tag_ops = dsa_tag_driver_get(default_proto);
1171 if (IS_ERR(tag_ops)) {
1172 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1173 return -EPROBE_DEFER;
1175 dev_warn(ds->dev, "No tagger for this switch\n");
1176 return PTR_ERR(tag_ops);
1180 if (dst->tag_ops != tag_ops) {
1182 "A DSA switch tree can have only one tagging protocol\n");
1184 dsa_tag_driver_put(tag_ops);
1188 /* In the case of multiple CPU ports per switch, the tagging
1189 * protocol is still reference-counted only per switch tree.
1191 dsa_tag_driver_put(tag_ops);
1193 dst->tag_ops = tag_ops;
1196 dp->master = master;
1197 dp->type = DSA_PORT_TYPE_CPU;
1198 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1201 /* At this point, the tree may be configured to use a different
1202 * tagger than the one chosen by the switch driver during
1203 * .setup, in the case when a user selects a custom protocol
1206 * This is resolved by syncing the driver with the tree in
1207 * dsa_switch_setup_tag_protocol once .setup has run and the
1208 * driver is ready to accept calls to .change_tag_protocol. If
1209 * the driver does not support the custom protocol at that
1210 * point, the tree is wholly rejected, thereby ensuring that the
1211 * tree and driver are always in agreement on the protocol to
1217 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1219 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1220 const char *name = of_get_property(dn, "label", NULL);
1221 bool link = of_property_read_bool(dn, "link");
1226 struct net_device *master;
1227 const char *user_protocol;
1229 master = of_find_net_device_by_node(ethernet);
1231 return -EPROBE_DEFER;
1233 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1234 return dsa_port_parse_cpu(dp, master, user_protocol);
1238 return dsa_port_parse_dsa(dp);
1240 return dsa_port_parse_user(dp, name);
1243 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1244 struct device_node *dn)
1246 struct device_node *ports, *port;
1247 struct dsa_port *dp;
1251 ports = of_get_child_by_name(dn, "ports");
1253 /* The second possibility is "ethernet-ports" */
1254 ports = of_get_child_by_name(dn, "ethernet-ports");
1256 dev_err(ds->dev, "no ports child node found\n");
1261 for_each_available_child_of_node(ports, port) {
1262 err = of_property_read_u32(port, "reg", ®);
1266 if (reg >= ds->num_ports) {
1267 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1268 port, reg, ds->num_ports);
1273 dp = dsa_to_port(ds, reg);
1275 err = dsa_port_parse_of(dp, port);
1285 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1286 struct device_node *dn)
1288 u32 m[2] = { 0, 0 };
1291 /* Don't error out if this optional property isn't found */
1292 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1293 if (sz < 0 && sz != -EINVAL)
1298 ds->dst = dsa_tree_touch(m[0]);
1302 if (dsa_switch_find(ds->dst->index, ds->index)) {
1304 "A DSA switch with index %d already exists in tree %d\n",
1305 ds->index, ds->dst->index);
1309 if (ds->dst->last_switch < ds->index)
1310 ds->dst->last_switch = ds->index;
1315 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1317 struct dsa_port *dp;
1320 for (port = 0; port < ds->num_ports; port++) {
1321 dp = dsa_port_touch(ds, port);
1329 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1333 err = dsa_switch_parse_member_of(ds, dn);
1337 err = dsa_switch_touch_ports(ds);
1341 return dsa_switch_parse_ports_of(ds, dn);
1344 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1347 if (!strcmp(name, "cpu")) {
1348 struct net_device *master;
1350 master = dsa_dev_to_net_device(dev);
1352 return -EPROBE_DEFER;
1356 return dsa_port_parse_cpu(dp, master, NULL);
1359 if (!strcmp(name, "dsa"))
1360 return dsa_port_parse_dsa(dp);
1362 return dsa_port_parse_user(dp, name);
1365 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1366 struct dsa_chip_data *cd)
1368 bool valid_name_found = false;
1369 struct dsa_port *dp;
1375 for (i = 0; i < DSA_MAX_PORTS; i++) {
1376 name = cd->port_names[i];
1377 dev = cd->netdev[i];
1378 dp = dsa_to_port(ds, i);
1383 err = dsa_port_parse(dp, name, dev);
1387 valid_name_found = true;
1390 if (!valid_name_found && i == DSA_MAX_PORTS)
1396 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1402 /* We don't support interconnected switches nor multiple trees via
1403 * platform data, so this is the unique switch of the tree.
1406 ds->dst = dsa_tree_touch(0);
1410 err = dsa_switch_touch_ports(ds);
1414 return dsa_switch_parse_ports(ds, cd);
1417 static void dsa_switch_release_ports(struct dsa_switch *ds)
1419 struct dsa_switch_tree *dst = ds->dst;
1420 struct dsa_port *dp, *next;
1422 list_for_each_entry_safe(dp, next, &dst->ports, list) {
1425 list_del(&dp->list);
1430 static int dsa_switch_probe(struct dsa_switch *ds)
1432 struct dsa_switch_tree *dst;
1433 struct dsa_chip_data *pdata;
1434 struct device_node *np;
1440 pdata = ds->dev->platform_data;
1441 np = ds->dev->of_node;
1447 err = dsa_switch_parse_of(ds, np);
1449 dsa_switch_release_ports(ds);
1451 err = dsa_switch_parse(ds, pdata);
1453 dsa_switch_release_ports(ds);
1463 err = dsa_tree_setup(dst);
1465 dsa_switch_release_ports(ds);
1472 int dsa_register_switch(struct dsa_switch *ds)
1476 mutex_lock(&dsa2_mutex);
1477 err = dsa_switch_probe(ds);
1478 dsa_tree_put(ds->dst);
1479 mutex_unlock(&dsa2_mutex);
1483 EXPORT_SYMBOL_GPL(dsa_register_switch);
1485 static void dsa_switch_remove(struct dsa_switch *ds)
1487 struct dsa_switch_tree *dst = ds->dst;
1489 dsa_tree_teardown(dst);
1490 dsa_switch_release_ports(ds);
1494 void dsa_unregister_switch(struct dsa_switch *ds)
1496 mutex_lock(&dsa2_mutex);
1497 dsa_switch_remove(ds);
1498 mutex_unlock(&dsa2_mutex);
1500 EXPORT_SYMBOL_GPL(dsa_unregister_switch);