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);
24 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
26 struct dsa_switch_tree *dst;
29 list_for_each_entry(dst, &dsa_tree_list, list) {
30 if (dst->index != tree_index)
33 list_for_each_entry(dp, &dst->ports, list) {
34 if (dp->ds->index != sw_index)
43 EXPORT_SYMBOL_GPL(dsa_switch_find);
45 static struct dsa_switch_tree *dsa_tree_find(int index)
47 struct dsa_switch_tree *dst;
49 list_for_each_entry(dst, &dsa_tree_list, list)
50 if (dst->index == index)
56 static struct dsa_switch_tree *dsa_tree_alloc(int index)
58 struct dsa_switch_tree *dst;
60 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
66 INIT_LIST_HEAD(&dst->rtable);
68 INIT_LIST_HEAD(&dst->ports);
70 INIT_LIST_HEAD(&dst->list);
71 list_add_tail(&dst->list, &dsa_tree_list);
73 kref_init(&dst->refcount);
78 static void dsa_tree_free(struct dsa_switch_tree *dst)
84 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
87 kref_get(&dst->refcount);
92 static struct dsa_switch_tree *dsa_tree_touch(int index)
94 struct dsa_switch_tree *dst;
96 dst = dsa_tree_find(index);
98 return dsa_tree_get(dst);
100 return dsa_tree_alloc(index);
103 static void dsa_tree_release(struct kref *ref)
105 struct dsa_switch_tree *dst;
107 dst = container_of(ref, struct dsa_switch_tree, refcount);
112 static void dsa_tree_put(struct dsa_switch_tree *dst)
115 kref_put(&dst->refcount, dsa_tree_release);
118 static bool dsa_port_is_dsa(struct dsa_port *port)
120 return port->type == DSA_PORT_TYPE_DSA;
123 static bool dsa_port_is_cpu(struct dsa_port *port)
125 return port->type == DSA_PORT_TYPE_CPU;
128 static bool dsa_port_is_user(struct dsa_port *dp)
130 return dp->type == DSA_PORT_TYPE_USER;
133 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
134 struct device_node *dn)
138 list_for_each_entry(dp, &dst->ports, list)
145 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
146 struct dsa_port *link_dp)
148 struct dsa_switch *ds = dp->ds;
149 struct dsa_switch_tree *dst;
154 list_for_each_entry(dl, &dst->rtable, list)
155 if (dl->dp == dp && dl->link_dp == link_dp)
158 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
163 dl->link_dp = link_dp;
165 INIT_LIST_HEAD(&dl->list);
166 list_add_tail(&dl->list, &dst->rtable);
171 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
173 struct dsa_switch *ds = dp->ds;
174 struct dsa_switch_tree *dst = ds->dst;
175 struct device_node *dn = dp->dn;
176 struct of_phandle_iterator it;
177 struct dsa_port *link_dp;
181 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
182 link_dp = dsa_tree_find_port_by_node(dst, it.node);
184 of_node_put(it.node);
188 dl = dsa_link_touch(dp, link_dp);
190 of_node_put(it.node);
198 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
200 bool complete = true;
203 list_for_each_entry(dp, &dst->ports, list) {
204 if (dsa_port_is_dsa(dp)) {
205 complete = dsa_port_setup_routing_table(dp);
214 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
218 list_for_each_entry(dp, &dst->ports, list)
219 if (dsa_port_is_cpu(dp))
225 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
227 struct dsa_port *cpu_dp, *dp;
229 cpu_dp = dsa_tree_find_first_cpu(dst);
231 pr_err("DSA: tree %d has no CPU port\n", dst->index);
235 /* Assign the default CPU port to all ports of the fabric */
236 list_for_each_entry(dp, &dst->ports, list)
237 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
243 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
247 list_for_each_entry(dp, &dst->ports, list)
248 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
252 static int dsa_port_setup(struct dsa_port *dp)
254 struct devlink_port *dlp = &dp->devlink_port;
255 bool dsa_port_link_registered = false;
256 bool dsa_port_enabled = false;
263 case DSA_PORT_TYPE_UNUSED:
264 dsa_port_disable(dp);
266 case DSA_PORT_TYPE_CPU:
267 err = dsa_port_link_register_of(dp);
270 dsa_port_link_registered = true;
272 err = dsa_port_enable(dp, NULL);
275 dsa_port_enabled = true;
278 case DSA_PORT_TYPE_DSA:
279 err = dsa_port_link_register_of(dp);
282 dsa_port_link_registered = true;
284 err = dsa_port_enable(dp, NULL);
287 dsa_port_enabled = true;
290 case DSA_PORT_TYPE_USER:
291 dp->mac = of_get_mac_address(dp->dn);
292 err = dsa_slave_create(dp);
296 devlink_port_type_eth_set(dlp, dp->slave);
300 if (err && dsa_port_enabled)
301 dsa_port_disable(dp);
302 if (err && dsa_port_link_registered)
303 dsa_port_link_unregister_of(dp);
312 static int dsa_port_devlink_setup(struct dsa_port *dp)
314 struct devlink_port *dlp = &dp->devlink_port;
315 struct dsa_switch_tree *dst = dp->ds->dst;
316 struct devlink_port_attrs attrs = {};
317 struct devlink *dl = dp->ds->devlink;
318 const unsigned char *id;
322 id = (const unsigned char *)&dst->index;
323 len = sizeof(dst->index);
325 attrs.phys.port_number = dp->index;
326 memcpy(attrs.switch_id.id, id, len);
327 attrs.switch_id.id_len = len;
328 memset(dlp, 0, sizeof(*dlp));
331 case DSA_PORT_TYPE_UNUSED:
332 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
334 case DSA_PORT_TYPE_CPU:
335 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
337 case DSA_PORT_TYPE_DSA:
338 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
340 case DSA_PORT_TYPE_USER:
341 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
345 devlink_port_attrs_set(dlp, &attrs);
346 err = devlink_port_register(dl, dlp, dp->index);
349 dp->devlink_port_setup = true;
354 static void dsa_port_teardown(struct dsa_port *dp)
360 case DSA_PORT_TYPE_UNUSED:
362 case DSA_PORT_TYPE_CPU:
363 dsa_port_disable(dp);
364 dsa_tag_driver_put(dp->tag_ops);
365 dsa_port_link_unregister_of(dp);
367 case DSA_PORT_TYPE_DSA:
368 dsa_port_disable(dp);
369 dsa_port_link_unregister_of(dp);
371 case DSA_PORT_TYPE_USER:
373 dsa_slave_destroy(dp->slave);
382 static void dsa_port_devlink_teardown(struct dsa_port *dp)
384 struct devlink_port *dlp = &dp->devlink_port;
386 if (dp->devlink_port_setup)
387 devlink_port_unregister(dlp);
388 dp->devlink_port_setup = false;
391 static int dsa_devlink_info_get(struct devlink *dl,
392 struct devlink_info_req *req,
393 struct netlink_ext_ack *extack)
395 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
397 if (ds->ops->devlink_info_get)
398 return ds->ops->devlink_info_get(ds, req, extack);
403 static const struct devlink_ops dsa_devlink_ops = {
404 .info_get = dsa_devlink_info_get,
407 static int dsa_switch_setup(struct dsa_switch *ds)
409 struct dsa_devlink_priv *dl_priv;
416 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
417 * driver and before ops->setup() has run, since the switch drivers and
418 * the slave MDIO bus driver rely on these values for probing PHY
421 ds->phys_mii_mask |= dsa_user_ports(ds);
423 /* Add the switch to devlink before calling setup, so that setup can
426 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
429 dl_priv = devlink_priv(ds->devlink);
432 err = devlink_register(ds->devlink, ds->dev);
436 /* Setup devlink port instances now, so that the switch
437 * setup() can register regions etc, against the ports
439 list_for_each_entry(dp, &ds->dst->ports, list) {
441 err = dsa_port_devlink_setup(dp);
443 goto unregister_devlink_ports;
447 err = dsa_switch_register_notifier(ds);
449 goto unregister_devlink_ports;
451 err = ds->ops->setup(ds);
453 goto unregister_notifier;
455 devlink_params_publish(ds->devlink);
457 if (!ds->slave_mii_bus && ds->ops->phy_read) {
458 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
459 if (!ds->slave_mii_bus) {
461 goto unregister_notifier;
464 dsa_slave_mii_bus_init(ds);
466 err = mdiobus_register(ds->slave_mii_bus);
468 goto unregister_notifier;
476 dsa_switch_unregister_notifier(ds);
477 unregister_devlink_ports:
478 list_for_each_entry(dp, &ds->dst->ports, list)
480 dsa_port_devlink_teardown(dp);
481 devlink_unregister(ds->devlink);
483 devlink_free(ds->devlink);
489 static void dsa_switch_teardown(struct dsa_switch *ds)
496 if (ds->slave_mii_bus && ds->ops->phy_read)
497 mdiobus_unregister(ds->slave_mii_bus);
499 dsa_switch_unregister_notifier(ds);
501 if (ds->ops->teardown)
502 ds->ops->teardown(ds);
505 list_for_each_entry(dp, &ds->dst->ports, list)
507 dsa_port_devlink_teardown(dp);
508 devlink_unregister(ds->devlink);
509 devlink_free(ds->devlink);
516 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
521 list_for_each_entry(dp, &dst->ports, list) {
522 err = dsa_switch_setup(dp->ds);
527 list_for_each_entry(dp, &dst->ports, list) {
528 err = dsa_port_setup(dp);
536 list_for_each_entry(dp, &dst->ports, list)
537 dsa_port_teardown(dp);
539 list_for_each_entry(dp, &dst->ports, list)
540 dsa_switch_teardown(dp->ds);
545 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
549 list_for_each_entry(dp, &dst->ports, list)
550 dsa_port_teardown(dp);
552 list_for_each_entry(dp, &dst->ports, list)
553 dsa_switch_teardown(dp->ds);
556 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
561 list_for_each_entry(dp, &dst->ports, list) {
562 if (dsa_port_is_cpu(dp)) {
563 err = dsa_master_setup(dp->master, dp);
572 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
576 list_for_each_entry(dp, &dst->ports, list)
577 if (dsa_port_is_cpu(dp))
578 dsa_master_teardown(dp->master);
581 static int dsa_tree_setup(struct dsa_switch_tree *dst)
587 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
592 complete = dsa_tree_setup_routing_table(dst);
596 err = dsa_tree_setup_default_cpu(dst);
600 err = dsa_tree_setup_switches(dst);
602 goto teardown_default_cpu;
604 err = dsa_tree_setup_master(dst);
606 goto teardown_switches;
610 pr_info("DSA: tree %d setup\n", dst->index);
615 dsa_tree_teardown_switches(dst);
616 teardown_default_cpu:
617 dsa_tree_teardown_default_cpu(dst);
622 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
624 struct dsa_link *dl, *next;
629 dsa_tree_teardown_master(dst);
631 dsa_tree_teardown_switches(dst);
633 dsa_tree_teardown_default_cpu(dst);
635 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
640 pr_info("DSA: tree %d torn down\n", dst->index);
645 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
647 struct dsa_switch_tree *dst = ds->dst;
650 list_for_each_entry(dp, &dst->ports, list)
651 if (dp->ds == ds && dp->index == index)
654 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
661 INIT_LIST_HEAD(&dp->list);
662 list_add_tail(&dp->list, &dst->ports);
667 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
672 dp->type = DSA_PORT_TYPE_USER;
678 static int dsa_port_parse_dsa(struct dsa_port *dp)
680 dp->type = DSA_PORT_TYPE_DSA;
685 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
686 struct net_device *master)
688 enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
689 struct dsa_switch *mds, *ds = dp->ds;
690 unsigned int mdp_upstream;
691 struct dsa_port *mdp;
693 /* It is possible to stack DSA switches onto one another when that
694 * happens the switch driver may want to know if its tagging protocol
695 * is going to work in such a configuration.
697 if (dsa_slave_dev_check(master)) {
698 mdp = dsa_slave_to_port(master);
700 mdp_upstream = dsa_upstream_port(mds, mdp->index);
701 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
705 /* If the master device is not itself a DSA slave in a disjoint DSA
706 * tree, then return immediately.
708 return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
711 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
713 struct dsa_switch *ds = dp->ds;
714 struct dsa_switch_tree *dst = ds->dst;
715 const struct dsa_device_ops *tag_ops;
716 enum dsa_tag_protocol tag_protocol;
718 tag_protocol = dsa_get_tag_protocol(dp, master);
719 tag_ops = dsa_tag_driver_get(tag_protocol);
720 if (IS_ERR(tag_ops)) {
721 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
722 return -EPROBE_DEFER;
723 dev_warn(ds->dev, "No tagger for this switch\n");
725 return PTR_ERR(tag_ops);
729 dp->type = DSA_PORT_TYPE_CPU;
730 dp->filter = tag_ops->filter;
731 dp->rcv = tag_ops->rcv;
732 dp->tag_ops = tag_ops;
738 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
740 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
741 const char *name = of_get_property(dn, "label", NULL);
742 bool link = of_property_read_bool(dn, "link");
747 struct net_device *master;
749 master = of_find_net_device_by_node(ethernet);
751 return -EPROBE_DEFER;
753 return dsa_port_parse_cpu(dp, master);
757 return dsa_port_parse_dsa(dp);
759 return dsa_port_parse_user(dp, name);
762 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
763 struct device_node *dn)
765 struct device_node *ports, *port;
770 ports = of_get_child_by_name(dn, "ports");
772 /* The second possibility is "ethernet-ports" */
773 ports = of_get_child_by_name(dn, "ethernet-ports");
775 dev_err(ds->dev, "no ports child node found\n");
780 for_each_available_child_of_node(ports, port) {
781 err = of_property_read_u32(port, "reg", ®);
785 if (reg >= ds->num_ports) {
790 dp = dsa_to_port(ds, reg);
792 err = dsa_port_parse_of(dp, port);
802 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
803 struct device_node *dn)
808 /* Don't error out if this optional property isn't found */
809 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
810 if (sz < 0 && sz != -EINVAL)
815 ds->dst = dsa_tree_touch(m[0]);
822 static int dsa_switch_touch_ports(struct dsa_switch *ds)
827 for (port = 0; port < ds->num_ports; port++) {
828 dp = dsa_port_touch(ds, port);
836 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
840 err = dsa_switch_parse_member_of(ds, dn);
844 err = dsa_switch_touch_ports(ds);
848 return dsa_switch_parse_ports_of(ds, dn);
851 static int dsa_port_parse(struct dsa_port *dp, const char *name,
854 if (!strcmp(name, "cpu")) {
855 struct net_device *master;
857 master = dsa_dev_to_net_device(dev);
859 return -EPROBE_DEFER;
863 return dsa_port_parse_cpu(dp, master);
866 if (!strcmp(name, "dsa"))
867 return dsa_port_parse_dsa(dp);
869 return dsa_port_parse_user(dp, name);
872 static int dsa_switch_parse_ports(struct dsa_switch *ds,
873 struct dsa_chip_data *cd)
875 bool valid_name_found = false;
882 for (i = 0; i < DSA_MAX_PORTS; i++) {
883 name = cd->port_names[i];
885 dp = dsa_to_port(ds, i);
890 err = dsa_port_parse(dp, name, dev);
894 valid_name_found = true;
897 if (!valid_name_found && i == DSA_MAX_PORTS)
903 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
909 /* We don't support interconnected switches nor multiple trees via
910 * platform data, so this is the unique switch of the tree.
913 ds->dst = dsa_tree_touch(0);
917 err = dsa_switch_touch_ports(ds);
921 return dsa_switch_parse_ports(ds, cd);
924 static void dsa_switch_release_ports(struct dsa_switch *ds)
926 struct dsa_switch_tree *dst = ds->dst;
927 struct dsa_port *dp, *next;
929 list_for_each_entry_safe(dp, next, &dst->ports, list) {
937 static int dsa_switch_probe(struct dsa_switch *ds)
939 struct dsa_switch_tree *dst;
940 struct dsa_chip_data *pdata;
941 struct device_node *np;
947 pdata = ds->dev->platform_data;
948 np = ds->dev->of_node;
954 err = dsa_switch_parse_of(ds, np);
956 dsa_switch_release_ports(ds);
958 err = dsa_switch_parse(ds, pdata);
960 dsa_switch_release_ports(ds);
970 err = dsa_tree_setup(dst);
972 dsa_switch_release_ports(ds);
979 int dsa_register_switch(struct dsa_switch *ds)
983 mutex_lock(&dsa2_mutex);
984 err = dsa_switch_probe(ds);
985 dsa_tree_put(ds->dst);
986 mutex_unlock(&dsa2_mutex);
990 EXPORT_SYMBOL_GPL(dsa_register_switch);
992 static void dsa_switch_remove(struct dsa_switch *ds)
994 struct dsa_switch_tree *dst = ds->dst;
996 dsa_tree_teardown(dst);
997 dsa_switch_release_ports(ds);
1001 void dsa_unregister_switch(struct dsa_switch *ds)
1003 mutex_lock(&dsa2_mutex);
1004 dsa_switch_remove(ds);
1005 mutex_unlock(&dsa2_mutex);
1007 EXPORT_SYMBOL_GPL(dsa_unregister_switch);