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>
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 bool dsa_port_is_dsa(struct dsa_port *port)
224 return port->type == DSA_PORT_TYPE_DSA;
227 static bool dsa_port_is_cpu(struct dsa_port *port)
229 return port->type == DSA_PORT_TYPE_CPU;
232 static bool dsa_port_is_user(struct dsa_port *dp)
234 return dp->type == DSA_PORT_TYPE_USER;
237 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
238 struct device_node *dn)
242 list_for_each_entry(dp, &dst->ports, list)
249 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
250 struct dsa_port *link_dp)
252 struct dsa_switch *ds = dp->ds;
253 struct dsa_switch_tree *dst;
258 list_for_each_entry(dl, &dst->rtable, list)
259 if (dl->dp == dp && dl->link_dp == link_dp)
262 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
267 dl->link_dp = link_dp;
269 INIT_LIST_HEAD(&dl->list);
270 list_add_tail(&dl->list, &dst->rtable);
275 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
277 struct dsa_switch *ds = dp->ds;
278 struct dsa_switch_tree *dst = ds->dst;
279 struct device_node *dn = dp->dn;
280 struct of_phandle_iterator it;
281 struct dsa_port *link_dp;
285 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
286 link_dp = dsa_tree_find_port_by_node(dst, it.node);
288 of_node_put(it.node);
292 dl = dsa_link_touch(dp, link_dp);
294 of_node_put(it.node);
302 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
304 bool complete = true;
307 list_for_each_entry(dp, &dst->ports, list) {
308 if (dsa_port_is_dsa(dp)) {
309 complete = dsa_port_setup_routing_table(dp);
318 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
322 list_for_each_entry(dp, &dst->ports, list)
323 if (dsa_port_is_cpu(dp))
329 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
331 struct dsa_port *cpu_dp, *dp;
333 cpu_dp = dsa_tree_find_first_cpu(dst);
335 pr_err("DSA: tree %d has no CPU port\n", dst->index);
339 /* Assign the default CPU port to all ports of the fabric */
340 list_for_each_entry(dp, &dst->ports, list)
341 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
347 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
351 list_for_each_entry(dp, &dst->ports, list)
352 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
356 static int dsa_port_setup(struct dsa_port *dp)
358 struct devlink_port *dlp = &dp->devlink_port;
359 bool dsa_port_link_registered = false;
360 bool dsa_port_enabled = false;
367 case DSA_PORT_TYPE_UNUSED:
368 dsa_port_disable(dp);
370 case DSA_PORT_TYPE_CPU:
371 err = dsa_port_link_register_of(dp);
374 dsa_port_link_registered = true;
376 err = dsa_port_enable(dp, NULL);
379 dsa_port_enabled = true;
382 case DSA_PORT_TYPE_DSA:
383 err = dsa_port_link_register_of(dp);
386 dsa_port_link_registered = true;
388 err = dsa_port_enable(dp, NULL);
391 dsa_port_enabled = true;
394 case DSA_PORT_TYPE_USER:
395 of_get_mac_address(dp->dn, dp->mac);
396 err = dsa_slave_create(dp);
400 devlink_port_type_eth_set(dlp, dp->slave);
404 if (err && dsa_port_enabled)
405 dsa_port_disable(dp);
406 if (err && dsa_port_link_registered)
407 dsa_port_link_unregister_of(dp);
416 static int dsa_port_devlink_setup(struct dsa_port *dp)
418 struct devlink_port *dlp = &dp->devlink_port;
419 struct dsa_switch_tree *dst = dp->ds->dst;
420 struct devlink_port_attrs attrs = {};
421 struct devlink *dl = dp->ds->devlink;
422 const unsigned char *id;
426 id = (const unsigned char *)&dst->index;
427 len = sizeof(dst->index);
429 attrs.phys.port_number = dp->index;
430 memcpy(attrs.switch_id.id, id, len);
431 attrs.switch_id.id_len = len;
432 memset(dlp, 0, sizeof(*dlp));
435 case DSA_PORT_TYPE_UNUSED:
436 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
438 case DSA_PORT_TYPE_CPU:
439 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
441 case DSA_PORT_TYPE_DSA:
442 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
444 case DSA_PORT_TYPE_USER:
445 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
449 devlink_port_attrs_set(dlp, &attrs);
450 err = devlink_port_register(dl, dlp, dp->index);
453 dp->devlink_port_setup = true;
458 static void dsa_port_teardown(struct dsa_port *dp)
460 struct devlink_port *dlp = &dp->devlink_port;
465 devlink_port_type_clear(dlp);
468 case DSA_PORT_TYPE_UNUSED:
470 case DSA_PORT_TYPE_CPU:
471 dsa_port_disable(dp);
472 dsa_port_link_unregister_of(dp);
474 case DSA_PORT_TYPE_DSA:
475 dsa_port_disable(dp);
476 dsa_port_link_unregister_of(dp);
478 case DSA_PORT_TYPE_USER:
480 dsa_slave_destroy(dp->slave);
489 static void dsa_port_devlink_teardown(struct dsa_port *dp)
491 struct devlink_port *dlp = &dp->devlink_port;
493 if (dp->devlink_port_setup)
494 devlink_port_unregister(dlp);
495 dp->devlink_port_setup = false;
498 static int dsa_devlink_info_get(struct devlink *dl,
499 struct devlink_info_req *req,
500 struct netlink_ext_ack *extack)
502 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
504 if (ds->ops->devlink_info_get)
505 return ds->ops->devlink_info_get(ds, req, extack);
510 static int dsa_devlink_sb_pool_get(struct devlink *dl,
511 unsigned int sb_index, u16 pool_index,
512 struct devlink_sb_pool_info *pool_info)
514 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
516 if (!ds->ops->devlink_sb_pool_get)
519 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
523 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
524 u16 pool_index, u32 size,
525 enum devlink_sb_threshold_type threshold_type,
526 struct netlink_ext_ack *extack)
528 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
530 if (!ds->ops->devlink_sb_pool_set)
533 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
534 threshold_type, extack);
537 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
538 unsigned int sb_index, u16 pool_index,
541 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
542 int port = dsa_devlink_port_to_port(dlp);
544 if (!ds->ops->devlink_sb_port_pool_get)
547 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
548 pool_index, p_threshold);
551 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
552 unsigned int sb_index, u16 pool_index,
554 struct netlink_ext_ack *extack)
556 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
557 int port = dsa_devlink_port_to_port(dlp);
559 if (!ds->ops->devlink_sb_port_pool_set)
562 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
563 pool_index, threshold, extack);
567 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
568 unsigned int sb_index, u16 tc_index,
569 enum devlink_sb_pool_type pool_type,
570 u16 *p_pool_index, u32 *p_threshold)
572 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
573 int port = dsa_devlink_port_to_port(dlp);
575 if (!ds->ops->devlink_sb_tc_pool_bind_get)
578 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
580 p_pool_index, p_threshold);
584 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
585 unsigned int sb_index, u16 tc_index,
586 enum devlink_sb_pool_type pool_type,
587 u16 pool_index, u32 threshold,
588 struct netlink_ext_ack *extack)
590 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
591 int port = dsa_devlink_port_to_port(dlp);
593 if (!ds->ops->devlink_sb_tc_pool_bind_set)
596 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
598 pool_index, threshold,
602 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
603 unsigned int sb_index)
605 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
607 if (!ds->ops->devlink_sb_occ_snapshot)
610 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
613 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
614 unsigned int sb_index)
616 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
618 if (!ds->ops->devlink_sb_occ_max_clear)
621 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
624 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
625 unsigned int sb_index,
626 u16 pool_index, u32 *p_cur,
629 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
630 int port = dsa_devlink_port_to_port(dlp);
632 if (!ds->ops->devlink_sb_occ_port_pool_get)
635 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
636 pool_index, p_cur, p_max);
640 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
641 unsigned int sb_index, u16 tc_index,
642 enum devlink_sb_pool_type pool_type,
643 u32 *p_cur, u32 *p_max)
645 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
646 int port = dsa_devlink_port_to_port(dlp);
648 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
651 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
657 static const struct devlink_ops dsa_devlink_ops = {
658 .info_get = dsa_devlink_info_get,
659 .sb_pool_get = dsa_devlink_sb_pool_get,
660 .sb_pool_set = dsa_devlink_sb_pool_set,
661 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
662 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
663 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
664 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
665 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
666 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
667 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
668 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
671 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
673 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
674 struct dsa_switch_tree *dst = ds->dst;
677 if (tag_ops->proto == dst->default_proto)
680 for (port = 0; port < ds->num_ports; port++) {
681 if (!dsa_is_cpu_port(ds, port))
684 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
686 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
687 tag_ops->name, ERR_PTR(err));
695 static int dsa_switch_setup(struct dsa_switch *ds)
697 struct dsa_devlink_priv *dl_priv;
704 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
705 * driver and before ops->setup() has run, since the switch drivers and
706 * the slave MDIO bus driver rely on these values for probing PHY
709 ds->phys_mii_mask |= dsa_user_ports(ds);
711 /* Add the switch to devlink before calling setup, so that setup can
714 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
717 dl_priv = devlink_priv(ds->devlink);
720 err = devlink_register(ds->devlink, ds->dev);
724 /* Setup devlink port instances now, so that the switch
725 * setup() can register regions etc, against the ports
727 list_for_each_entry(dp, &ds->dst->ports, list) {
729 err = dsa_port_devlink_setup(dp);
731 goto unregister_devlink_ports;
735 err = dsa_switch_register_notifier(ds);
737 goto unregister_devlink_ports;
739 ds->configure_vlan_while_not_filtering = true;
741 err = ds->ops->setup(ds);
743 goto unregister_notifier;
745 err = dsa_switch_setup_tag_protocol(ds);
749 devlink_params_publish(ds->devlink);
751 if (!ds->slave_mii_bus && ds->ops->phy_read) {
752 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
753 if (!ds->slave_mii_bus) {
758 dsa_slave_mii_bus_init(ds);
760 err = mdiobus_register(ds->slave_mii_bus);
770 if (ds->ops->teardown)
771 ds->ops->teardown(ds);
773 dsa_switch_unregister_notifier(ds);
774 unregister_devlink_ports:
775 list_for_each_entry(dp, &ds->dst->ports, list)
777 dsa_port_devlink_teardown(dp);
778 devlink_unregister(ds->devlink);
780 devlink_free(ds->devlink);
786 static void dsa_switch_teardown(struct dsa_switch *ds)
793 if (ds->slave_mii_bus && ds->ops->phy_read)
794 mdiobus_unregister(ds->slave_mii_bus);
796 dsa_switch_unregister_notifier(ds);
798 if (ds->ops->teardown)
799 ds->ops->teardown(ds);
802 list_for_each_entry(dp, &ds->dst->ports, list)
804 dsa_port_devlink_teardown(dp);
805 devlink_unregister(ds->devlink);
806 devlink_free(ds->devlink);
813 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
818 list_for_each_entry(dp, &dst->ports, list) {
819 err = dsa_switch_setup(dp->ds);
824 list_for_each_entry(dp, &dst->ports, list) {
825 err = dsa_port_setup(dp);
827 dsa_port_devlink_teardown(dp);
828 dp->type = DSA_PORT_TYPE_UNUSED;
829 err = dsa_port_devlink_setup(dp);
839 list_for_each_entry(dp, &dst->ports, list)
840 dsa_port_teardown(dp);
842 list_for_each_entry(dp, &dst->ports, list)
843 dsa_switch_teardown(dp->ds);
848 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
852 list_for_each_entry(dp, &dst->ports, list)
853 dsa_port_teardown(dp);
855 list_for_each_entry(dp, &dst->ports, list)
856 dsa_switch_teardown(dp->ds);
859 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
864 list_for_each_entry(dp, &dst->ports, list) {
865 if (dsa_port_is_cpu(dp)) {
866 err = dsa_master_setup(dp->master, dp);
875 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
879 list_for_each_entry(dp, &dst->ports, list)
880 if (dsa_port_is_cpu(dp))
881 dsa_master_teardown(dp->master);
884 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
886 unsigned int len = 0;
889 list_for_each_entry(dp, &dst->ports, list) {
890 if (dp->ds->num_lag_ids > len)
891 len = dp->ds->num_lag_ids;
897 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
905 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
910 static int dsa_tree_setup(struct dsa_switch_tree *dst)
916 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
921 complete = dsa_tree_setup_routing_table(dst);
925 err = dsa_tree_setup_default_cpu(dst);
929 err = dsa_tree_setup_switches(dst);
931 goto teardown_default_cpu;
933 err = dsa_tree_setup_master(dst);
935 goto teardown_switches;
937 err = dsa_tree_setup_lags(dst);
939 goto teardown_master;
943 pr_info("DSA: tree %d setup\n", dst->index);
948 dsa_tree_teardown_master(dst);
950 dsa_tree_teardown_switches(dst);
951 teardown_default_cpu:
952 dsa_tree_teardown_default_cpu(dst);
957 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
959 struct dsa_link *dl, *next;
964 dsa_tree_teardown_lags(dst);
966 dsa_tree_teardown_master(dst);
968 dsa_tree_teardown_switches(dst);
970 dsa_tree_teardown_default_cpu(dst);
972 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
977 pr_info("DSA: tree %d torn down\n", dst->index);
982 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
983 * is that all DSA switches within a tree share the same tagger, otherwise
984 * they would have formed disjoint trees (different "dsa,member" values).
986 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
987 struct net_device *master,
988 const struct dsa_device_ops *tag_ops,
989 const struct dsa_device_ops *old_tag_ops)
991 struct dsa_notifier_tag_proto_info info;
996 return restart_syscall();
998 /* At the moment we don't allow changing the tag protocol under
999 * traffic. The rtnl_mutex also happens to serialize concurrent
1000 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1001 * restriction, there needs to be another mutex which serializes this.
1003 if (master->flags & IFF_UP)
1006 list_for_each_entry(dp, &dst->ports, list) {
1007 if (!dsa_is_user_port(dp->ds, dp->index))
1010 if (dp->slave->flags & IFF_UP)
1014 info.tag_ops = tag_ops;
1015 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1017 goto out_unwind_tagger;
1019 dst->tag_ops = tag_ops;
1026 info.tag_ops = old_tag_ops;
1027 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1033 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1035 struct dsa_switch_tree *dst = ds->dst;
1036 struct dsa_port *dp;
1038 list_for_each_entry(dp, &dst->ports, list)
1039 if (dp->ds == ds && dp->index == index)
1042 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1049 INIT_LIST_HEAD(&dp->list);
1050 list_add_tail(&dp->list, &dst->ports);
1055 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1060 dp->type = DSA_PORT_TYPE_USER;
1066 static int dsa_port_parse_dsa(struct dsa_port *dp)
1068 dp->type = DSA_PORT_TYPE_DSA;
1073 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1074 struct net_device *master)
1076 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1077 struct dsa_switch *mds, *ds = dp->ds;
1078 unsigned int mdp_upstream;
1079 struct dsa_port *mdp;
1081 /* It is possible to stack DSA switches onto one another when that
1082 * happens the switch driver may want to know if its tagging protocol
1083 * is going to work in such a configuration.
1085 if (dsa_slave_dev_check(master)) {
1086 mdp = dsa_slave_to_port(master);
1088 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1089 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1090 DSA_TAG_PROTO_NONE);
1093 /* If the master device is not itself a DSA slave in a disjoint DSA
1094 * tree, then return immediately.
1096 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1099 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1100 const char *user_protocol)
1102 struct dsa_switch *ds = dp->ds;
1103 struct dsa_switch_tree *dst = ds->dst;
1104 const struct dsa_device_ops *tag_ops;
1105 enum dsa_tag_protocol default_proto;
1107 /* Find out which protocol the switch would prefer. */
1108 default_proto = dsa_get_tag_protocol(dp, master);
1109 if (dst->default_proto) {
1110 if (dst->default_proto != default_proto) {
1112 "A DSA switch tree can have only one tagging protocol\n");
1116 dst->default_proto = default_proto;
1119 /* See if the user wants to override that preference. */
1120 if (user_protocol) {
1121 if (!ds->ops->change_tag_protocol) {
1122 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1126 tag_ops = dsa_find_tagger_by_name(user_protocol);
1128 tag_ops = dsa_tag_driver_get(default_proto);
1131 if (IS_ERR(tag_ops)) {
1132 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1133 return -EPROBE_DEFER;
1135 dev_warn(ds->dev, "No tagger for this switch\n");
1136 return PTR_ERR(tag_ops);
1140 if (dst->tag_ops != tag_ops) {
1142 "A DSA switch tree can have only one tagging protocol\n");
1144 dsa_tag_driver_put(tag_ops);
1148 /* In the case of multiple CPU ports per switch, the tagging
1149 * protocol is still reference-counted only per switch tree.
1151 dsa_tag_driver_put(tag_ops);
1153 dst->tag_ops = tag_ops;
1156 dp->master = master;
1157 dp->type = DSA_PORT_TYPE_CPU;
1158 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1161 /* At this point, the tree may be configured to use a different
1162 * tagger than the one chosen by the switch driver during
1163 * .setup, in the case when a user selects a custom protocol
1166 * This is resolved by syncing the driver with the tree in
1167 * dsa_switch_setup_tag_protocol once .setup has run and the
1168 * driver is ready to accept calls to .change_tag_protocol. If
1169 * the driver does not support the custom protocol at that
1170 * point, the tree is wholly rejected, thereby ensuring that the
1171 * tree and driver are always in agreement on the protocol to
1177 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1179 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1180 const char *name = of_get_property(dn, "label", NULL);
1181 bool link = of_property_read_bool(dn, "link");
1186 struct net_device *master;
1187 const char *user_protocol;
1189 master = of_find_net_device_by_node(ethernet);
1191 return -EPROBE_DEFER;
1193 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1194 return dsa_port_parse_cpu(dp, master, user_protocol);
1198 return dsa_port_parse_dsa(dp);
1200 return dsa_port_parse_user(dp, name);
1203 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1204 struct device_node *dn)
1206 struct device_node *ports, *port;
1207 struct dsa_port *dp;
1211 ports = of_get_child_by_name(dn, "ports");
1213 /* The second possibility is "ethernet-ports" */
1214 ports = of_get_child_by_name(dn, "ethernet-ports");
1216 dev_err(ds->dev, "no ports child node found\n");
1221 for_each_available_child_of_node(ports, port) {
1222 err = of_property_read_u32(port, "reg", ®);
1226 if (reg >= ds->num_ports) {
1227 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1228 port, reg, ds->num_ports);
1233 dp = dsa_to_port(ds, reg);
1235 err = dsa_port_parse_of(dp, port);
1245 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1246 struct device_node *dn)
1248 u32 m[2] = { 0, 0 };
1251 /* Don't error out if this optional property isn't found */
1252 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1253 if (sz < 0 && sz != -EINVAL)
1258 ds->dst = dsa_tree_touch(m[0]);
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);