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 int dsa_broadcast(unsigned long e, void *v)
55 struct dsa_switch_tree *dst;
58 list_for_each_entry(dst, &dsa_tree_list, list) {
59 err = dsa_tree_notify(dst, e, v);
68 * dsa_lag_map() - Map LAG netdev to a linear LAG ID
69 * @dst: Tree in which to record the mapping.
70 * @lag: Netdev that is to be mapped to an ID.
72 * dsa_lag_id/dsa_lag_dev can then be used to translate between the
73 * two spaces. The size of the mapping space is determined by the
74 * driver by setting ds->num_lag_ids. It is perfectly legal to leave
75 * it unset if it is not needed, in which case these functions become
78 void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag)
82 if (dsa_lag_id(dst, lag) >= 0)
86 for (id = 0; id < dst->lags_len; id++) {
87 if (!dsa_lag_dev(dst, id)) {
93 /* No IDs left, which is OK. Some drivers do not need it. The
94 * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
95 * returns an error for this device when joining the LAG. The
96 * driver can then return -EOPNOTSUPP back to DSA, which will
97 * fall back to a software LAG.
102 * dsa_lag_unmap() - Remove a LAG ID mapping
103 * @dst: Tree in which the mapping is recorded.
104 * @lag: Netdev that was mapped.
106 * As there may be multiple users of the mapping, it is only removed
107 * if there are no other references to it.
109 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag)
114 dsa_lag_foreach_port(dp, dst, lag)
115 /* There are remaining users of this mapping */
118 dsa_lags_foreach_id(id, dst) {
119 if (dsa_lag_dev(dst, id) == lag) {
120 dst->lags[id] = NULL;
126 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
128 struct dsa_switch_tree *dst;
131 list_for_each_entry(dst, &dsa_tree_list, list) {
132 if (dst->index != tree_index)
135 list_for_each_entry(dp, &dst->ports, list) {
136 if (dp->ds->index != sw_index)
145 EXPORT_SYMBOL_GPL(dsa_switch_find);
147 static struct dsa_switch_tree *dsa_tree_find(int index)
149 struct dsa_switch_tree *dst;
151 list_for_each_entry(dst, &dsa_tree_list, list)
152 if (dst->index == index)
158 static struct dsa_switch_tree *dsa_tree_alloc(int index)
160 struct dsa_switch_tree *dst;
162 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
168 INIT_LIST_HEAD(&dst->rtable);
170 INIT_LIST_HEAD(&dst->ports);
172 INIT_LIST_HEAD(&dst->list);
173 list_add_tail(&dst->list, &dsa_tree_list);
175 kref_init(&dst->refcount);
180 static void dsa_tree_free(struct dsa_switch_tree *dst)
183 dsa_tag_driver_put(dst->tag_ops);
184 list_del(&dst->list);
188 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
191 kref_get(&dst->refcount);
196 static struct dsa_switch_tree *dsa_tree_touch(int index)
198 struct dsa_switch_tree *dst;
200 dst = dsa_tree_find(index);
202 return dsa_tree_get(dst);
204 return dsa_tree_alloc(index);
207 static void dsa_tree_release(struct kref *ref)
209 struct dsa_switch_tree *dst;
211 dst = container_of(ref, struct dsa_switch_tree, refcount);
216 static void dsa_tree_put(struct dsa_switch_tree *dst)
219 kref_put(&dst->refcount, dsa_tree_release);
222 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
223 struct device_node *dn)
227 list_for_each_entry(dp, &dst->ports, list)
234 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
235 struct dsa_port *link_dp)
237 struct dsa_switch *ds = dp->ds;
238 struct dsa_switch_tree *dst;
243 list_for_each_entry(dl, &dst->rtable, list)
244 if (dl->dp == dp && dl->link_dp == link_dp)
247 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
252 dl->link_dp = link_dp;
254 INIT_LIST_HEAD(&dl->list);
255 list_add_tail(&dl->list, &dst->rtable);
260 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
262 struct dsa_switch *ds = dp->ds;
263 struct dsa_switch_tree *dst = ds->dst;
264 struct device_node *dn = dp->dn;
265 struct of_phandle_iterator it;
266 struct dsa_port *link_dp;
270 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
271 link_dp = dsa_tree_find_port_by_node(dst, it.node);
273 of_node_put(it.node);
277 dl = dsa_link_touch(dp, link_dp);
279 of_node_put(it.node);
287 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
289 bool complete = true;
292 list_for_each_entry(dp, &dst->ports, list) {
293 if (dsa_port_is_dsa(dp)) {
294 complete = dsa_port_setup_routing_table(dp);
303 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
307 list_for_each_entry(dp, &dst->ports, list)
308 if (dsa_port_is_cpu(dp))
314 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
316 struct dsa_port *cpu_dp, *dp;
318 cpu_dp = dsa_tree_find_first_cpu(dst);
320 pr_err("DSA: tree %d has no CPU port\n", dst->index);
324 /* Assign the default CPU port to all ports of the fabric */
325 list_for_each_entry(dp, &dst->ports, list)
326 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
332 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
336 list_for_each_entry(dp, &dst->ports, list)
337 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
341 static int dsa_port_setup(struct dsa_port *dp)
343 struct devlink_port *dlp = &dp->devlink_port;
344 bool dsa_port_link_registered = false;
345 bool dsa_port_enabled = false;
351 INIT_LIST_HEAD(&dp->mdbs);
354 case DSA_PORT_TYPE_UNUSED:
355 dsa_port_disable(dp);
357 case DSA_PORT_TYPE_CPU:
358 err = dsa_port_link_register_of(dp);
361 dsa_port_link_registered = true;
363 err = dsa_port_enable(dp, NULL);
366 dsa_port_enabled = true;
369 case DSA_PORT_TYPE_DSA:
370 err = dsa_port_link_register_of(dp);
373 dsa_port_link_registered = true;
375 err = dsa_port_enable(dp, NULL);
378 dsa_port_enabled = true;
381 case DSA_PORT_TYPE_USER:
382 of_get_mac_address(dp->dn, dp->mac);
383 err = dsa_slave_create(dp);
387 devlink_port_type_eth_set(dlp, dp->slave);
391 if (err && dsa_port_enabled)
392 dsa_port_disable(dp);
393 if (err && dsa_port_link_registered)
394 dsa_port_link_unregister_of(dp);
403 static int dsa_port_devlink_setup(struct dsa_port *dp)
405 struct devlink_port *dlp = &dp->devlink_port;
406 struct dsa_switch_tree *dst = dp->ds->dst;
407 struct devlink_port_attrs attrs = {};
408 struct devlink *dl = dp->ds->devlink;
409 const unsigned char *id;
413 id = (const unsigned char *)&dst->index;
414 len = sizeof(dst->index);
416 attrs.phys.port_number = dp->index;
417 memcpy(attrs.switch_id.id, id, len);
418 attrs.switch_id.id_len = len;
419 memset(dlp, 0, sizeof(*dlp));
422 case DSA_PORT_TYPE_UNUSED:
423 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
425 case DSA_PORT_TYPE_CPU:
426 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
428 case DSA_PORT_TYPE_DSA:
429 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
431 case DSA_PORT_TYPE_USER:
432 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
436 devlink_port_attrs_set(dlp, &attrs);
437 err = devlink_port_register(dl, dlp, dp->index);
440 dp->devlink_port_setup = true;
445 static void dsa_port_teardown(struct dsa_port *dp)
447 struct devlink_port *dlp = &dp->devlink_port;
448 struct dsa_mac_addr *a, *tmp;
453 devlink_port_type_clear(dlp);
456 case DSA_PORT_TYPE_UNUSED:
458 case DSA_PORT_TYPE_CPU:
459 dsa_port_disable(dp);
460 dsa_port_link_unregister_of(dp);
462 case DSA_PORT_TYPE_DSA:
463 dsa_port_disable(dp);
464 dsa_port_link_unregister_of(dp);
466 case DSA_PORT_TYPE_USER:
468 dsa_slave_destroy(dp->slave);
474 list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
482 static void dsa_port_devlink_teardown(struct dsa_port *dp)
484 struct devlink_port *dlp = &dp->devlink_port;
486 if (dp->devlink_port_setup)
487 devlink_port_unregister(dlp);
488 dp->devlink_port_setup = false;
491 static int dsa_devlink_info_get(struct devlink *dl,
492 struct devlink_info_req *req,
493 struct netlink_ext_ack *extack)
495 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
497 if (ds->ops->devlink_info_get)
498 return ds->ops->devlink_info_get(ds, req, extack);
503 static int dsa_devlink_sb_pool_get(struct devlink *dl,
504 unsigned int sb_index, u16 pool_index,
505 struct devlink_sb_pool_info *pool_info)
507 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
509 if (!ds->ops->devlink_sb_pool_get)
512 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
516 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
517 u16 pool_index, u32 size,
518 enum devlink_sb_threshold_type threshold_type,
519 struct netlink_ext_ack *extack)
521 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
523 if (!ds->ops->devlink_sb_pool_set)
526 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
527 threshold_type, extack);
530 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
531 unsigned int sb_index, u16 pool_index,
534 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
535 int port = dsa_devlink_port_to_port(dlp);
537 if (!ds->ops->devlink_sb_port_pool_get)
540 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
541 pool_index, p_threshold);
544 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
545 unsigned int sb_index, u16 pool_index,
547 struct netlink_ext_ack *extack)
549 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
550 int port = dsa_devlink_port_to_port(dlp);
552 if (!ds->ops->devlink_sb_port_pool_set)
555 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
556 pool_index, threshold, extack);
560 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
561 unsigned int sb_index, u16 tc_index,
562 enum devlink_sb_pool_type pool_type,
563 u16 *p_pool_index, u32 *p_threshold)
565 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
566 int port = dsa_devlink_port_to_port(dlp);
568 if (!ds->ops->devlink_sb_tc_pool_bind_get)
571 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
573 p_pool_index, p_threshold);
577 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
578 unsigned int sb_index, u16 tc_index,
579 enum devlink_sb_pool_type pool_type,
580 u16 pool_index, u32 threshold,
581 struct netlink_ext_ack *extack)
583 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
584 int port = dsa_devlink_port_to_port(dlp);
586 if (!ds->ops->devlink_sb_tc_pool_bind_set)
589 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
591 pool_index, threshold,
595 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
596 unsigned int sb_index)
598 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
600 if (!ds->ops->devlink_sb_occ_snapshot)
603 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
606 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
607 unsigned int sb_index)
609 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
611 if (!ds->ops->devlink_sb_occ_max_clear)
614 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
617 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
618 unsigned int sb_index,
619 u16 pool_index, u32 *p_cur,
622 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
623 int port = dsa_devlink_port_to_port(dlp);
625 if (!ds->ops->devlink_sb_occ_port_pool_get)
628 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
629 pool_index, p_cur, p_max);
633 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
634 unsigned int sb_index, u16 tc_index,
635 enum devlink_sb_pool_type pool_type,
636 u32 *p_cur, u32 *p_max)
638 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
639 int port = dsa_devlink_port_to_port(dlp);
641 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
644 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
650 static const struct devlink_ops dsa_devlink_ops = {
651 .info_get = dsa_devlink_info_get,
652 .sb_pool_get = dsa_devlink_sb_pool_get,
653 .sb_pool_set = dsa_devlink_sb_pool_set,
654 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
655 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
656 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
657 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
658 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
659 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
660 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
661 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
664 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
666 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
667 struct dsa_switch_tree *dst = ds->dst;
670 if (tag_ops->proto == dst->default_proto)
673 for (port = 0; port < ds->num_ports; port++) {
674 if (!dsa_is_cpu_port(ds, port))
677 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
679 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
680 tag_ops->name, ERR_PTR(err));
688 static int dsa_switch_setup(struct dsa_switch *ds)
690 struct dsa_devlink_priv *dl_priv;
697 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
698 * driver and before ops->setup() has run, since the switch drivers and
699 * the slave MDIO bus driver rely on these values for probing PHY
702 ds->phys_mii_mask |= dsa_user_ports(ds);
704 /* Add the switch to devlink before calling setup, so that setup can
707 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
710 dl_priv = devlink_priv(ds->devlink);
713 err = devlink_register(ds->devlink, ds->dev);
717 /* Setup devlink port instances now, so that the switch
718 * setup() can register regions etc, against the ports
720 list_for_each_entry(dp, &ds->dst->ports, list) {
722 err = dsa_port_devlink_setup(dp);
724 goto unregister_devlink_ports;
728 err = dsa_switch_register_notifier(ds);
730 goto unregister_devlink_ports;
732 ds->configure_vlan_while_not_filtering = true;
734 err = ds->ops->setup(ds);
736 goto unregister_notifier;
738 err = dsa_switch_setup_tag_protocol(ds);
742 devlink_params_publish(ds->devlink);
744 if (!ds->slave_mii_bus && ds->ops->phy_read) {
745 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
746 if (!ds->slave_mii_bus) {
751 dsa_slave_mii_bus_init(ds);
753 err = mdiobus_register(ds->slave_mii_bus);
763 if (ds->ops->teardown)
764 ds->ops->teardown(ds);
766 dsa_switch_unregister_notifier(ds);
767 unregister_devlink_ports:
768 list_for_each_entry(dp, &ds->dst->ports, list)
770 dsa_port_devlink_teardown(dp);
771 devlink_unregister(ds->devlink);
773 devlink_free(ds->devlink);
779 static void dsa_switch_teardown(struct dsa_switch *ds)
786 if (ds->slave_mii_bus && ds->ops->phy_read)
787 mdiobus_unregister(ds->slave_mii_bus);
789 dsa_switch_unregister_notifier(ds);
791 if (ds->ops->teardown)
792 ds->ops->teardown(ds);
795 list_for_each_entry(dp, &ds->dst->ports, list)
797 dsa_port_devlink_teardown(dp);
798 devlink_unregister(ds->devlink);
799 devlink_free(ds->devlink);
806 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
811 list_for_each_entry(dp, &dst->ports, list) {
812 err = dsa_switch_setup(dp->ds);
817 list_for_each_entry(dp, &dst->ports, list) {
818 err = dsa_port_setup(dp);
820 dsa_port_devlink_teardown(dp);
821 dp->type = DSA_PORT_TYPE_UNUSED;
822 err = dsa_port_devlink_setup(dp);
832 list_for_each_entry(dp, &dst->ports, list)
833 dsa_port_teardown(dp);
835 list_for_each_entry(dp, &dst->ports, list)
836 dsa_switch_teardown(dp->ds);
841 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
845 list_for_each_entry(dp, &dst->ports, list)
846 dsa_port_teardown(dp);
848 list_for_each_entry(dp, &dst->ports, list)
849 dsa_switch_teardown(dp->ds);
852 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
857 list_for_each_entry(dp, &dst->ports, list) {
858 if (dsa_port_is_cpu(dp)) {
859 err = dsa_master_setup(dp->master, dp);
868 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
872 list_for_each_entry(dp, &dst->ports, list)
873 if (dsa_port_is_cpu(dp))
874 dsa_master_teardown(dp->master);
877 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
879 unsigned int len = 0;
882 list_for_each_entry(dp, &dst->ports, list) {
883 if (dp->ds->num_lag_ids > len)
884 len = dp->ds->num_lag_ids;
890 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
898 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
903 static int dsa_tree_setup(struct dsa_switch_tree *dst)
909 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
914 complete = dsa_tree_setup_routing_table(dst);
918 err = dsa_tree_setup_default_cpu(dst);
922 err = dsa_tree_setup_switches(dst);
924 goto teardown_default_cpu;
926 err = dsa_tree_setup_master(dst);
928 goto teardown_switches;
930 err = dsa_tree_setup_lags(dst);
932 goto teardown_master;
936 pr_info("DSA: tree %d setup\n", dst->index);
941 dsa_tree_teardown_master(dst);
943 dsa_tree_teardown_switches(dst);
944 teardown_default_cpu:
945 dsa_tree_teardown_default_cpu(dst);
950 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
952 struct dsa_link *dl, *next;
957 dsa_tree_teardown_lags(dst);
959 dsa_tree_teardown_master(dst);
961 dsa_tree_teardown_switches(dst);
963 dsa_tree_teardown_default_cpu(dst);
965 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
970 pr_info("DSA: tree %d torn down\n", dst->index);
975 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
976 * is that all DSA switches within a tree share the same tagger, otherwise
977 * they would have formed disjoint trees (different "dsa,member" values).
979 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
980 struct net_device *master,
981 const struct dsa_device_ops *tag_ops,
982 const struct dsa_device_ops *old_tag_ops)
984 struct dsa_notifier_tag_proto_info info;
989 return restart_syscall();
991 /* At the moment we don't allow changing the tag protocol under
992 * traffic. The rtnl_mutex also happens to serialize concurrent
993 * attempts to change the tagging protocol. If we ever lift the IFF_UP
994 * restriction, there needs to be another mutex which serializes this.
996 if (master->flags & IFF_UP)
999 list_for_each_entry(dp, &dst->ports, list) {
1000 if (!dsa_is_user_port(dp->ds, dp->index))
1003 if (dp->slave->flags & IFF_UP)
1007 info.tag_ops = tag_ops;
1008 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1010 goto out_unwind_tagger;
1012 dst->tag_ops = tag_ops;
1019 info.tag_ops = old_tag_ops;
1020 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1026 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1028 struct dsa_switch_tree *dst = ds->dst;
1029 struct dsa_port *dp;
1031 list_for_each_entry(dp, &dst->ports, list)
1032 if (dp->ds == ds && dp->index == index)
1035 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1042 INIT_LIST_HEAD(&dp->list);
1043 list_add_tail(&dp->list, &dst->ports);
1048 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1053 dp->type = DSA_PORT_TYPE_USER;
1059 static int dsa_port_parse_dsa(struct dsa_port *dp)
1061 dp->type = DSA_PORT_TYPE_DSA;
1066 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1067 struct net_device *master)
1069 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1070 struct dsa_switch *mds, *ds = dp->ds;
1071 unsigned int mdp_upstream;
1072 struct dsa_port *mdp;
1074 /* It is possible to stack DSA switches onto one another when that
1075 * happens the switch driver may want to know if its tagging protocol
1076 * is going to work in such a configuration.
1078 if (dsa_slave_dev_check(master)) {
1079 mdp = dsa_slave_to_port(master);
1081 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1082 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1083 DSA_TAG_PROTO_NONE);
1086 /* If the master device is not itself a DSA slave in a disjoint DSA
1087 * tree, then return immediately.
1089 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1092 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1093 const char *user_protocol)
1095 struct dsa_switch *ds = dp->ds;
1096 struct dsa_switch_tree *dst = ds->dst;
1097 const struct dsa_device_ops *tag_ops;
1098 enum dsa_tag_protocol default_proto;
1100 /* Find out which protocol the switch would prefer. */
1101 default_proto = dsa_get_tag_protocol(dp, master);
1102 if (dst->default_proto) {
1103 if (dst->default_proto != default_proto) {
1105 "A DSA switch tree can have only one tagging protocol\n");
1109 dst->default_proto = default_proto;
1112 /* See if the user wants to override that preference. */
1113 if (user_protocol) {
1114 if (!ds->ops->change_tag_protocol) {
1115 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1119 tag_ops = dsa_find_tagger_by_name(user_protocol);
1121 tag_ops = dsa_tag_driver_get(default_proto);
1124 if (IS_ERR(tag_ops)) {
1125 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1126 return -EPROBE_DEFER;
1128 dev_warn(ds->dev, "No tagger for this switch\n");
1129 return PTR_ERR(tag_ops);
1133 if (dst->tag_ops != tag_ops) {
1135 "A DSA switch tree can have only one tagging protocol\n");
1137 dsa_tag_driver_put(tag_ops);
1141 /* In the case of multiple CPU ports per switch, the tagging
1142 * protocol is still reference-counted only per switch tree.
1144 dsa_tag_driver_put(tag_ops);
1146 dst->tag_ops = tag_ops;
1149 dp->master = master;
1150 dp->type = DSA_PORT_TYPE_CPU;
1151 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1154 /* At this point, the tree may be configured to use a different
1155 * tagger than the one chosen by the switch driver during
1156 * .setup, in the case when a user selects a custom protocol
1159 * This is resolved by syncing the driver with the tree in
1160 * dsa_switch_setup_tag_protocol once .setup has run and the
1161 * driver is ready to accept calls to .change_tag_protocol. If
1162 * the driver does not support the custom protocol at that
1163 * point, the tree is wholly rejected, thereby ensuring that the
1164 * tree and driver are always in agreement on the protocol to
1170 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1172 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1173 const char *name = of_get_property(dn, "label", NULL);
1174 bool link = of_property_read_bool(dn, "link");
1179 struct net_device *master;
1180 const char *user_protocol;
1182 master = of_find_net_device_by_node(ethernet);
1184 return -EPROBE_DEFER;
1186 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1187 return dsa_port_parse_cpu(dp, master, user_protocol);
1191 return dsa_port_parse_dsa(dp);
1193 return dsa_port_parse_user(dp, name);
1196 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1197 struct device_node *dn)
1199 struct device_node *ports, *port;
1200 struct dsa_port *dp;
1204 ports = of_get_child_by_name(dn, "ports");
1206 /* The second possibility is "ethernet-ports" */
1207 ports = of_get_child_by_name(dn, "ethernet-ports");
1209 dev_err(ds->dev, "no ports child node found\n");
1214 for_each_available_child_of_node(ports, port) {
1215 err = of_property_read_u32(port, "reg", ®);
1219 if (reg >= ds->num_ports) {
1220 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1221 port, reg, ds->num_ports);
1226 dp = dsa_to_port(ds, reg);
1228 err = dsa_port_parse_of(dp, port);
1238 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1239 struct device_node *dn)
1241 u32 m[2] = { 0, 0 };
1244 /* Don't error out if this optional property isn't found */
1245 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1246 if (sz < 0 && sz != -EINVAL)
1251 ds->dst = dsa_tree_touch(m[0]);
1255 if (dsa_switch_find(ds->dst->index, ds->index)) {
1257 "A DSA switch with index %d already exists in tree %d\n",
1258 ds->index, ds->dst->index);
1265 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1267 struct dsa_port *dp;
1270 for (port = 0; port < ds->num_ports; port++) {
1271 dp = dsa_port_touch(ds, port);
1279 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1283 err = dsa_switch_parse_member_of(ds, dn);
1287 err = dsa_switch_touch_ports(ds);
1291 return dsa_switch_parse_ports_of(ds, dn);
1294 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1297 if (!strcmp(name, "cpu")) {
1298 struct net_device *master;
1300 master = dsa_dev_to_net_device(dev);
1302 return -EPROBE_DEFER;
1306 return dsa_port_parse_cpu(dp, master, NULL);
1309 if (!strcmp(name, "dsa"))
1310 return dsa_port_parse_dsa(dp);
1312 return dsa_port_parse_user(dp, name);
1315 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1316 struct dsa_chip_data *cd)
1318 bool valid_name_found = false;
1319 struct dsa_port *dp;
1325 for (i = 0; i < DSA_MAX_PORTS; i++) {
1326 name = cd->port_names[i];
1327 dev = cd->netdev[i];
1328 dp = dsa_to_port(ds, i);
1333 err = dsa_port_parse(dp, name, dev);
1337 valid_name_found = true;
1340 if (!valid_name_found && i == DSA_MAX_PORTS)
1346 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1352 /* We don't support interconnected switches nor multiple trees via
1353 * platform data, so this is the unique switch of the tree.
1356 ds->dst = dsa_tree_touch(0);
1360 err = dsa_switch_touch_ports(ds);
1364 return dsa_switch_parse_ports(ds, cd);
1367 static void dsa_switch_release_ports(struct dsa_switch *ds)
1369 struct dsa_switch_tree *dst = ds->dst;
1370 struct dsa_port *dp, *next;
1372 list_for_each_entry_safe(dp, next, &dst->ports, list) {
1375 list_del(&dp->list);
1380 static int dsa_switch_probe(struct dsa_switch *ds)
1382 struct dsa_switch_tree *dst;
1383 struct dsa_chip_data *pdata;
1384 struct device_node *np;
1390 pdata = ds->dev->platform_data;
1391 np = ds->dev->of_node;
1397 err = dsa_switch_parse_of(ds, np);
1399 dsa_switch_release_ports(ds);
1401 err = dsa_switch_parse(ds, pdata);
1403 dsa_switch_release_ports(ds);
1413 err = dsa_tree_setup(dst);
1415 dsa_switch_release_ports(ds);
1422 int dsa_register_switch(struct dsa_switch *ds)
1426 mutex_lock(&dsa2_mutex);
1427 err = dsa_switch_probe(ds);
1428 dsa_tree_put(ds->dst);
1429 mutex_unlock(&dsa2_mutex);
1433 EXPORT_SYMBOL_GPL(dsa_register_switch);
1435 static void dsa_switch_remove(struct dsa_switch *ds)
1437 struct dsa_switch_tree *dst = ds->dst;
1439 dsa_tree_teardown(dst);
1440 dsa_switch_release_ports(ds);
1444 void dsa_unregister_switch(struct dsa_switch *ds)
1446 mutex_lock(&dsa2_mutex);
1447 dsa_switch_remove(ds);
1448 mutex_unlock(&dsa2_mutex);
1450 EXPORT_SYMBOL_GPL(dsa_unregister_switch);