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 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->fdbs);
352 INIT_LIST_HEAD(&dp->mdbs);
355 case DSA_PORT_TYPE_UNUSED:
356 dsa_port_disable(dp);
358 case DSA_PORT_TYPE_CPU:
359 err = dsa_port_link_register_of(dp);
362 dsa_port_link_registered = true;
364 err = dsa_port_enable(dp, NULL);
367 dsa_port_enabled = true;
370 case DSA_PORT_TYPE_DSA:
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_USER:
383 of_get_mac_address(dp->dn, dp->mac);
384 err = dsa_slave_create(dp);
388 devlink_port_type_eth_set(dlp, dp->slave);
392 if (err && dsa_port_enabled)
393 dsa_port_disable(dp);
394 if (err && dsa_port_link_registered)
395 dsa_port_link_unregister_of(dp);
404 static int dsa_port_devlink_setup(struct dsa_port *dp)
406 struct devlink_port *dlp = &dp->devlink_port;
407 struct dsa_switch_tree *dst = dp->ds->dst;
408 struct devlink_port_attrs attrs = {};
409 struct devlink *dl = dp->ds->devlink;
410 const unsigned char *id;
414 id = (const unsigned char *)&dst->index;
415 len = sizeof(dst->index);
417 attrs.phys.port_number = dp->index;
418 memcpy(attrs.switch_id.id, id, len);
419 attrs.switch_id.id_len = len;
420 memset(dlp, 0, sizeof(*dlp));
423 case DSA_PORT_TYPE_UNUSED:
424 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
426 case DSA_PORT_TYPE_CPU:
427 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
429 case DSA_PORT_TYPE_DSA:
430 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
432 case DSA_PORT_TYPE_USER:
433 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
437 devlink_port_attrs_set(dlp, &attrs);
438 err = devlink_port_register(dl, dlp, dp->index);
441 dp->devlink_port_setup = true;
446 static void dsa_port_teardown(struct dsa_port *dp)
448 struct devlink_port *dlp = &dp->devlink_port;
449 struct dsa_mac_addr *a, *tmp;
454 devlink_port_type_clear(dlp);
457 case DSA_PORT_TYPE_UNUSED:
459 case DSA_PORT_TYPE_CPU:
460 dsa_port_disable(dp);
461 dsa_port_link_unregister_of(dp);
463 case DSA_PORT_TYPE_DSA:
464 dsa_port_disable(dp);
465 dsa_port_link_unregister_of(dp);
467 case DSA_PORT_TYPE_USER:
469 dsa_slave_destroy(dp->slave);
475 list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
480 list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
488 static void dsa_port_devlink_teardown(struct dsa_port *dp)
490 struct devlink_port *dlp = &dp->devlink_port;
492 if (dp->devlink_port_setup)
493 devlink_port_unregister(dlp);
494 dp->devlink_port_setup = false;
497 static int dsa_devlink_info_get(struct devlink *dl,
498 struct devlink_info_req *req,
499 struct netlink_ext_ack *extack)
501 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
503 if (ds->ops->devlink_info_get)
504 return ds->ops->devlink_info_get(ds, req, extack);
509 static int dsa_devlink_sb_pool_get(struct devlink *dl,
510 unsigned int sb_index, u16 pool_index,
511 struct devlink_sb_pool_info *pool_info)
513 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
515 if (!ds->ops->devlink_sb_pool_get)
518 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
522 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
523 u16 pool_index, u32 size,
524 enum devlink_sb_threshold_type threshold_type,
525 struct netlink_ext_ack *extack)
527 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
529 if (!ds->ops->devlink_sb_pool_set)
532 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
533 threshold_type, extack);
536 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
537 unsigned int sb_index, u16 pool_index,
540 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
541 int port = dsa_devlink_port_to_port(dlp);
543 if (!ds->ops->devlink_sb_port_pool_get)
546 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
547 pool_index, p_threshold);
550 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
551 unsigned int sb_index, u16 pool_index,
553 struct netlink_ext_ack *extack)
555 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
556 int port = dsa_devlink_port_to_port(dlp);
558 if (!ds->ops->devlink_sb_port_pool_set)
561 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
562 pool_index, threshold, extack);
566 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
567 unsigned int sb_index, u16 tc_index,
568 enum devlink_sb_pool_type pool_type,
569 u16 *p_pool_index, u32 *p_threshold)
571 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
572 int port = dsa_devlink_port_to_port(dlp);
574 if (!ds->ops->devlink_sb_tc_pool_bind_get)
577 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
579 p_pool_index, p_threshold);
583 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
584 unsigned int sb_index, u16 tc_index,
585 enum devlink_sb_pool_type pool_type,
586 u16 pool_index, u32 threshold,
587 struct netlink_ext_ack *extack)
589 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
590 int port = dsa_devlink_port_to_port(dlp);
592 if (!ds->ops->devlink_sb_tc_pool_bind_set)
595 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
597 pool_index, threshold,
601 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
602 unsigned int sb_index)
604 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
606 if (!ds->ops->devlink_sb_occ_snapshot)
609 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
612 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
613 unsigned int sb_index)
615 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
617 if (!ds->ops->devlink_sb_occ_max_clear)
620 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
623 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
624 unsigned int sb_index,
625 u16 pool_index, u32 *p_cur,
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_occ_port_pool_get)
634 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
635 pool_index, p_cur, p_max);
639 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
640 unsigned int sb_index, u16 tc_index,
641 enum devlink_sb_pool_type pool_type,
642 u32 *p_cur, u32 *p_max)
644 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
645 int port = dsa_devlink_port_to_port(dlp);
647 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
650 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
656 static const struct devlink_ops dsa_devlink_ops = {
657 .info_get = dsa_devlink_info_get,
658 .sb_pool_get = dsa_devlink_sb_pool_get,
659 .sb_pool_set = dsa_devlink_sb_pool_set,
660 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
661 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
662 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
663 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
664 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
665 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
666 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
667 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
670 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
672 const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
673 struct dsa_switch_tree *dst = ds->dst;
676 if (tag_ops->proto == dst->default_proto)
679 for (port = 0; port < ds->num_ports; port++) {
680 if (!dsa_is_cpu_port(ds, port))
683 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
685 dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
686 tag_ops->name, ERR_PTR(err));
694 static int dsa_switch_setup(struct dsa_switch *ds)
696 struct dsa_devlink_priv *dl_priv;
703 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
704 * driver and before ops->setup() has run, since the switch drivers and
705 * the slave MDIO bus driver rely on these values for probing PHY
708 ds->phys_mii_mask |= dsa_user_ports(ds);
710 /* Add the switch to devlink before calling setup, so that setup can
713 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
716 dl_priv = devlink_priv(ds->devlink);
719 err = devlink_register(ds->devlink, ds->dev);
723 /* Setup devlink port instances now, so that the switch
724 * setup() can register regions etc, against the ports
726 list_for_each_entry(dp, &ds->dst->ports, list) {
728 err = dsa_port_devlink_setup(dp);
730 goto unregister_devlink_ports;
734 err = dsa_switch_register_notifier(ds);
736 goto unregister_devlink_ports;
738 ds->configure_vlan_while_not_filtering = true;
740 err = ds->ops->setup(ds);
742 goto unregister_notifier;
744 err = dsa_switch_setup_tag_protocol(ds);
748 devlink_params_publish(ds->devlink);
750 if (!ds->slave_mii_bus && ds->ops->phy_read) {
751 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
752 if (!ds->slave_mii_bus) {
757 dsa_slave_mii_bus_init(ds);
759 err = mdiobus_register(ds->slave_mii_bus);
769 if (ds->ops->teardown)
770 ds->ops->teardown(ds);
772 dsa_switch_unregister_notifier(ds);
773 unregister_devlink_ports:
774 list_for_each_entry(dp, &ds->dst->ports, list)
776 dsa_port_devlink_teardown(dp);
777 devlink_unregister(ds->devlink);
779 devlink_free(ds->devlink);
785 static void dsa_switch_teardown(struct dsa_switch *ds)
792 if (ds->slave_mii_bus && ds->ops->phy_read)
793 mdiobus_unregister(ds->slave_mii_bus);
795 dsa_switch_unregister_notifier(ds);
797 if (ds->ops->teardown)
798 ds->ops->teardown(ds);
801 list_for_each_entry(dp, &ds->dst->ports, list)
803 dsa_port_devlink_teardown(dp);
804 devlink_unregister(ds->devlink);
805 devlink_free(ds->devlink);
812 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
817 list_for_each_entry(dp, &dst->ports, list) {
818 err = dsa_switch_setup(dp->ds);
823 list_for_each_entry(dp, &dst->ports, list) {
824 err = dsa_port_setup(dp);
826 dsa_port_devlink_teardown(dp);
827 dp->type = DSA_PORT_TYPE_UNUSED;
828 err = dsa_port_devlink_setup(dp);
838 list_for_each_entry(dp, &dst->ports, list)
839 dsa_port_teardown(dp);
841 list_for_each_entry(dp, &dst->ports, list)
842 dsa_switch_teardown(dp->ds);
847 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
851 list_for_each_entry(dp, &dst->ports, list)
852 dsa_port_teardown(dp);
854 list_for_each_entry(dp, &dst->ports, list)
855 dsa_switch_teardown(dp->ds);
858 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
863 list_for_each_entry(dp, &dst->ports, list) {
864 if (dsa_port_is_cpu(dp)) {
865 err = dsa_master_setup(dp->master, dp);
874 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
878 list_for_each_entry(dp, &dst->ports, list)
879 if (dsa_port_is_cpu(dp))
880 dsa_master_teardown(dp->master);
883 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
885 unsigned int len = 0;
888 list_for_each_entry(dp, &dst->ports, list) {
889 if (dp->ds->num_lag_ids > len)
890 len = dp->ds->num_lag_ids;
896 dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
904 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
909 static int dsa_tree_setup(struct dsa_switch_tree *dst)
915 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
920 complete = dsa_tree_setup_routing_table(dst);
924 err = dsa_tree_setup_default_cpu(dst);
928 err = dsa_tree_setup_switches(dst);
930 goto teardown_default_cpu;
932 err = dsa_tree_setup_master(dst);
934 goto teardown_switches;
936 err = dsa_tree_setup_lags(dst);
938 goto teardown_master;
942 pr_info("DSA: tree %d setup\n", dst->index);
947 dsa_tree_teardown_master(dst);
949 dsa_tree_teardown_switches(dst);
950 teardown_default_cpu:
951 dsa_tree_teardown_default_cpu(dst);
956 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
958 struct dsa_link *dl, *next;
963 dsa_tree_teardown_lags(dst);
965 dsa_tree_teardown_master(dst);
967 dsa_tree_teardown_switches(dst);
969 dsa_tree_teardown_default_cpu(dst);
971 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
976 pr_info("DSA: tree %d torn down\n", dst->index);
981 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
982 * is that all DSA switches within a tree share the same tagger, otherwise
983 * they would have formed disjoint trees (different "dsa,member" values).
985 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
986 struct net_device *master,
987 const struct dsa_device_ops *tag_ops,
988 const struct dsa_device_ops *old_tag_ops)
990 struct dsa_notifier_tag_proto_info info;
995 return restart_syscall();
997 /* At the moment we don't allow changing the tag protocol under
998 * traffic. The rtnl_mutex also happens to serialize concurrent
999 * attempts to change the tagging protocol. If we ever lift the IFF_UP
1000 * restriction, there needs to be another mutex which serializes this.
1002 if (master->flags & IFF_UP)
1005 list_for_each_entry(dp, &dst->ports, list) {
1006 if (!dsa_is_user_port(dp->ds, dp->index))
1009 if (dp->slave->flags & IFF_UP)
1013 info.tag_ops = tag_ops;
1014 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1016 goto out_unwind_tagger;
1018 dst->tag_ops = tag_ops;
1025 info.tag_ops = old_tag_ops;
1026 dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1032 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1034 struct dsa_switch_tree *dst = ds->dst;
1035 struct dsa_port *dp;
1037 list_for_each_entry(dp, &dst->ports, list)
1038 if (dp->ds == ds && dp->index == index)
1041 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1048 INIT_LIST_HEAD(&dp->list);
1049 list_add_tail(&dp->list, &dst->ports);
1054 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1059 dp->type = DSA_PORT_TYPE_USER;
1065 static int dsa_port_parse_dsa(struct dsa_port *dp)
1067 dp->type = DSA_PORT_TYPE_DSA;
1072 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1073 struct net_device *master)
1075 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1076 struct dsa_switch *mds, *ds = dp->ds;
1077 unsigned int mdp_upstream;
1078 struct dsa_port *mdp;
1080 /* It is possible to stack DSA switches onto one another when that
1081 * happens the switch driver may want to know if its tagging protocol
1082 * is going to work in such a configuration.
1084 if (dsa_slave_dev_check(master)) {
1085 mdp = dsa_slave_to_port(master);
1087 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1088 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1089 DSA_TAG_PROTO_NONE);
1092 /* If the master device is not itself a DSA slave in a disjoint DSA
1093 * tree, then return immediately.
1095 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1098 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1099 const char *user_protocol)
1101 struct dsa_switch *ds = dp->ds;
1102 struct dsa_switch_tree *dst = ds->dst;
1103 const struct dsa_device_ops *tag_ops;
1104 enum dsa_tag_protocol default_proto;
1106 /* Find out which protocol the switch would prefer. */
1107 default_proto = dsa_get_tag_protocol(dp, master);
1108 if (dst->default_proto) {
1109 if (dst->default_proto != default_proto) {
1111 "A DSA switch tree can have only one tagging protocol\n");
1115 dst->default_proto = default_proto;
1118 /* See if the user wants to override that preference. */
1119 if (user_protocol) {
1120 if (!ds->ops->change_tag_protocol) {
1121 dev_err(ds->dev, "Tag protocol cannot be modified\n");
1125 tag_ops = dsa_find_tagger_by_name(user_protocol);
1127 tag_ops = dsa_tag_driver_get(default_proto);
1130 if (IS_ERR(tag_ops)) {
1131 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1132 return -EPROBE_DEFER;
1134 dev_warn(ds->dev, "No tagger for this switch\n");
1135 return PTR_ERR(tag_ops);
1139 if (dst->tag_ops != tag_ops) {
1141 "A DSA switch tree can have only one tagging protocol\n");
1143 dsa_tag_driver_put(tag_ops);
1147 /* In the case of multiple CPU ports per switch, the tagging
1148 * protocol is still reference-counted only per switch tree.
1150 dsa_tag_driver_put(tag_ops);
1152 dst->tag_ops = tag_ops;
1155 dp->master = master;
1156 dp->type = DSA_PORT_TYPE_CPU;
1157 dsa_port_set_tag_protocol(dp, dst->tag_ops);
1160 /* At this point, the tree may be configured to use a different
1161 * tagger than the one chosen by the switch driver during
1162 * .setup, in the case when a user selects a custom protocol
1165 * This is resolved by syncing the driver with the tree in
1166 * dsa_switch_setup_tag_protocol once .setup has run and the
1167 * driver is ready to accept calls to .change_tag_protocol. If
1168 * the driver does not support the custom protocol at that
1169 * point, the tree is wholly rejected, thereby ensuring that the
1170 * tree and driver are always in agreement on the protocol to
1176 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1178 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1179 const char *name = of_get_property(dn, "label", NULL);
1180 bool link = of_property_read_bool(dn, "link");
1185 struct net_device *master;
1186 const char *user_protocol;
1188 master = of_find_net_device_by_node(ethernet);
1190 return -EPROBE_DEFER;
1192 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1193 return dsa_port_parse_cpu(dp, master, user_protocol);
1197 return dsa_port_parse_dsa(dp);
1199 return dsa_port_parse_user(dp, name);
1202 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1203 struct device_node *dn)
1205 struct device_node *ports, *port;
1206 struct dsa_port *dp;
1210 ports = of_get_child_by_name(dn, "ports");
1212 /* The second possibility is "ethernet-ports" */
1213 ports = of_get_child_by_name(dn, "ethernet-ports");
1215 dev_err(ds->dev, "no ports child node found\n");
1220 for_each_available_child_of_node(ports, port) {
1221 err = of_property_read_u32(port, "reg", ®);
1225 if (reg >= ds->num_ports) {
1226 dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1227 port, reg, ds->num_ports);
1232 dp = dsa_to_port(ds, reg);
1234 err = dsa_port_parse_of(dp, port);
1244 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1245 struct device_node *dn)
1247 u32 m[2] = { 0, 0 };
1250 /* Don't error out if this optional property isn't found */
1251 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1252 if (sz < 0 && sz != -EINVAL)
1257 ds->dst = dsa_tree_touch(m[0]);
1261 if (dsa_switch_find(ds->dst->index, ds->index)) {
1263 "A DSA switch with index %d already exists in tree %d\n",
1264 ds->index, ds->dst->index);
1271 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1273 struct dsa_port *dp;
1276 for (port = 0; port < ds->num_ports; port++) {
1277 dp = dsa_port_touch(ds, port);
1285 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1289 err = dsa_switch_parse_member_of(ds, dn);
1293 err = dsa_switch_touch_ports(ds);
1297 return dsa_switch_parse_ports_of(ds, dn);
1300 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1303 if (!strcmp(name, "cpu")) {
1304 struct net_device *master;
1306 master = dsa_dev_to_net_device(dev);
1308 return -EPROBE_DEFER;
1312 return dsa_port_parse_cpu(dp, master, NULL);
1315 if (!strcmp(name, "dsa"))
1316 return dsa_port_parse_dsa(dp);
1318 return dsa_port_parse_user(dp, name);
1321 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1322 struct dsa_chip_data *cd)
1324 bool valid_name_found = false;
1325 struct dsa_port *dp;
1331 for (i = 0; i < DSA_MAX_PORTS; i++) {
1332 name = cd->port_names[i];
1333 dev = cd->netdev[i];
1334 dp = dsa_to_port(ds, i);
1339 err = dsa_port_parse(dp, name, dev);
1343 valid_name_found = true;
1346 if (!valid_name_found && i == DSA_MAX_PORTS)
1352 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1358 /* We don't support interconnected switches nor multiple trees via
1359 * platform data, so this is the unique switch of the tree.
1362 ds->dst = dsa_tree_touch(0);
1366 err = dsa_switch_touch_ports(ds);
1370 return dsa_switch_parse_ports(ds, cd);
1373 static void dsa_switch_release_ports(struct dsa_switch *ds)
1375 struct dsa_switch_tree *dst = ds->dst;
1376 struct dsa_port *dp, *next;
1378 list_for_each_entry_safe(dp, next, &dst->ports, list) {
1381 list_del(&dp->list);
1386 static int dsa_switch_probe(struct dsa_switch *ds)
1388 struct dsa_switch_tree *dst;
1389 struct dsa_chip_data *pdata;
1390 struct device_node *np;
1396 pdata = ds->dev->platform_data;
1397 np = ds->dev->of_node;
1403 err = dsa_switch_parse_of(ds, np);
1405 dsa_switch_release_ports(ds);
1407 err = dsa_switch_parse(ds, pdata);
1409 dsa_switch_release_ports(ds);
1419 err = dsa_tree_setup(dst);
1421 dsa_switch_release_ports(ds);
1428 int dsa_register_switch(struct dsa_switch *ds)
1432 mutex_lock(&dsa2_mutex);
1433 err = dsa_switch_probe(ds);
1434 dsa_tree_put(ds->dst);
1435 mutex_unlock(&dsa2_mutex);
1439 EXPORT_SYMBOL_GPL(dsa_register_switch);
1441 static void dsa_switch_remove(struct dsa_switch *ds)
1443 struct dsa_switch_tree *dst = ds->dst;
1445 dsa_tree_teardown(dst);
1446 dsa_switch_release_ports(ds);
1450 void dsa_unregister_switch(struct dsa_switch *ds)
1452 mutex_lock(&dsa2_mutex);
1453 dsa_switch_remove(ds);
1454 mutex_unlock(&dsa2_mutex);
1456 EXPORT_SYMBOL_GPL(dsa_unregister_switch);