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 LIST_HEAD(dsa_tree_list);
22 static DEFINE_MUTEX(dsa2_mutex);
24 static const struct devlink_ops dsa_devlink_ops = {
27 static struct dsa_switch_tree *dsa_tree_find(int index)
29 struct dsa_switch_tree *dst;
31 list_for_each_entry(dst, &dsa_tree_list, list)
32 if (dst->index == index)
38 static struct dsa_switch_tree *dsa_tree_alloc(int index)
40 struct dsa_switch_tree *dst;
42 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
48 INIT_LIST_HEAD(&dst->list);
49 list_add_tail(&dsa_tree_list, &dst->list);
51 kref_init(&dst->refcount);
56 static void dsa_tree_free(struct dsa_switch_tree *dst)
62 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
65 kref_get(&dst->refcount);
70 static struct dsa_switch_tree *dsa_tree_touch(int index)
72 struct dsa_switch_tree *dst;
74 dst = dsa_tree_find(index);
76 return dsa_tree_get(dst);
78 return dsa_tree_alloc(index);
81 static void dsa_tree_release(struct kref *ref)
83 struct dsa_switch_tree *dst;
85 dst = container_of(ref, struct dsa_switch_tree, refcount);
90 static void dsa_tree_put(struct dsa_switch_tree *dst)
93 kref_put(&dst->refcount, dsa_tree_release);
96 static bool dsa_port_is_dsa(struct dsa_port *port)
98 return port->type == DSA_PORT_TYPE_DSA;
101 static bool dsa_port_is_cpu(struct dsa_port *port)
103 return port->type == DSA_PORT_TYPE_CPU;
106 static bool dsa_port_is_user(struct dsa_port *dp)
108 return dp->type == DSA_PORT_TYPE_USER;
111 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
112 struct device_node *dn)
114 struct dsa_switch *ds;
118 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
119 ds = dst->ds[device];
123 for (port = 0; port < ds->num_ports; port++) {
124 dp = &ds->ports[port];
134 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
136 struct dsa_switch *ds = dp->ds;
137 struct dsa_switch_tree *dst = ds->dst;
138 struct device_node *dn = dp->dn;
139 struct of_phandle_iterator it;
140 struct dsa_port *link_dp;
143 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
144 link_dp = dsa_tree_find_port_by_node(dst, it.node);
146 of_node_put(it.node);
150 ds->rtable[link_dp->ds->index] = dp->index;
156 static bool dsa_switch_setup_routing_table(struct dsa_switch *ds)
158 bool complete = true;
162 for (i = 0; i < DSA_MAX_SWITCHES; i++)
163 ds->rtable[i] = DSA_RTABLE_NONE;
165 for (i = 0; i < ds->num_ports; i++) {
168 if (dsa_port_is_dsa(dp)) {
169 complete = dsa_port_setup_routing_table(dp);
178 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
180 struct dsa_switch *ds;
181 bool complete = true;
184 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
185 ds = dst->ds[device];
189 complete = dsa_switch_setup_routing_table(ds);
197 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
199 struct dsa_switch *ds;
203 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
204 ds = dst->ds[device];
208 for (port = 0; port < ds->num_ports; port++) {
209 dp = &ds->ports[port];
211 if (dsa_port_is_cpu(dp))
219 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
221 struct dsa_switch *ds;
225 /* DSA currently only supports a single CPU port */
226 dst->cpu_dp = dsa_tree_find_first_cpu(dst);
228 pr_warn("Tree has no master device\n");
232 /* Assign the default CPU port to all ports of the fabric */
233 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
234 ds = dst->ds[device];
238 for (port = 0; port < ds->num_ports; port++) {
239 dp = &ds->ports[port];
241 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
242 dp->cpu_dp = dst->cpu_dp;
249 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
251 /* DSA currently only supports a single CPU port */
255 static int dsa_port_setup(struct dsa_port *dp)
257 enum devlink_port_flavour flavour;
258 struct dsa_switch *ds = dp->ds;
259 struct dsa_switch_tree *dst = ds->dst;
262 if (dp->type == DSA_PORT_TYPE_UNUSED)
265 memset(&dp->devlink_port, 0, sizeof(dp->devlink_port));
266 dp->mac = of_get_mac_address(dp->dn);
269 case DSA_PORT_TYPE_CPU:
270 flavour = DEVLINK_PORT_FLAVOUR_CPU;
272 case DSA_PORT_TYPE_DSA:
273 flavour = DEVLINK_PORT_FLAVOUR_DSA;
275 case DSA_PORT_TYPE_USER: /* fall-through */
277 flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
281 /* dp->index is used now as port_number. However
282 * CPU and DSA ports should have separate numbering
283 * independent from front panel port numbers.
285 devlink_port_attrs_set(&dp->devlink_port, flavour,
287 (const char *) &dst->index, sizeof(dst->index));
288 err = devlink_port_register(ds->devlink, &dp->devlink_port,
294 case DSA_PORT_TYPE_UNUSED:
296 case DSA_PORT_TYPE_CPU:
297 err = dsa_port_link_register_of(dp);
299 dev_err(ds->dev, "failed to setup link for port %d.%d\n",
300 ds->index, dp->index);
304 case DSA_PORT_TYPE_DSA:
305 err = dsa_port_link_register_of(dp);
307 dev_err(ds->dev, "failed to setup link for port %d.%d\n",
308 ds->index, dp->index);
312 case DSA_PORT_TYPE_USER:
313 err = dsa_slave_create(dp);
315 dev_err(ds->dev, "failed to create slave for port %d.%d\n",
316 ds->index, dp->index);
318 devlink_port_type_eth_set(&dp->devlink_port, dp->slave);
325 static void dsa_port_teardown(struct dsa_port *dp)
327 if (dp->type != DSA_PORT_TYPE_UNUSED)
328 devlink_port_unregister(&dp->devlink_port);
331 case DSA_PORT_TYPE_UNUSED:
333 case DSA_PORT_TYPE_CPU:
334 dsa_tag_driver_put(dp->tag_ops);
336 case DSA_PORT_TYPE_DSA:
337 dsa_port_link_unregister_of(dp);
339 case DSA_PORT_TYPE_USER:
341 dsa_slave_destroy(dp->slave);
348 static int dsa_switch_setup(struct dsa_switch *ds)
352 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
353 * driver and before ops->setup() has run, since the switch drivers and
354 * the slave MDIO bus driver rely on these values for probing PHY
357 ds->phys_mii_mask |= dsa_user_ports(ds);
359 /* Add the switch to devlink before calling setup, so that setup can
362 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
366 err = devlink_register(ds->devlink, ds->dev);
370 err = dsa_switch_register_notifier(ds);
374 err = ds->ops->setup(ds);
378 if (!ds->slave_mii_bus && ds->ops->phy_read) {
379 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
380 if (!ds->slave_mii_bus)
383 dsa_slave_mii_bus_init(ds);
385 err = mdiobus_register(ds->slave_mii_bus);
393 static void dsa_switch_teardown(struct dsa_switch *ds)
395 if (ds->slave_mii_bus && ds->ops->phy_read)
396 mdiobus_unregister(ds->slave_mii_bus);
398 dsa_switch_unregister_notifier(ds);
401 devlink_unregister(ds->devlink);
402 devlink_free(ds->devlink);
408 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
410 struct dsa_switch *ds;
415 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
416 ds = dst->ds[device];
420 err = dsa_switch_setup(ds);
424 for (port = 0; port < ds->num_ports; port++) {
425 dp = &ds->ports[port];
427 err = dsa_port_setup(dp);
436 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
438 struct dsa_switch *ds;
442 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
443 ds = dst->ds[device];
447 for (port = 0; port < ds->num_ports; port++) {
448 dp = &ds->ports[port];
450 dsa_port_teardown(dp);
453 dsa_switch_teardown(ds);
457 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
459 struct dsa_port *cpu_dp = dst->cpu_dp;
460 struct net_device *master = cpu_dp->master;
462 /* DSA currently supports a single pair of CPU port and master device */
463 return dsa_master_setup(master, cpu_dp);
466 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
468 struct dsa_port *cpu_dp = dst->cpu_dp;
469 struct net_device *master = cpu_dp->master;
471 return dsa_master_teardown(master);
474 static int dsa_tree_setup(struct dsa_switch_tree *dst)
480 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
485 complete = dsa_tree_setup_routing_table(dst);
489 err = dsa_tree_setup_default_cpu(dst);
493 err = dsa_tree_setup_switches(dst);
497 err = dsa_tree_setup_master(dst);
503 pr_info("DSA: tree %d setup\n", dst->index);
508 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
513 dsa_tree_teardown_master(dst);
515 dsa_tree_teardown_switches(dst);
517 dsa_tree_teardown_default_cpu(dst);
519 pr_info("DSA: tree %d torn down\n", dst->index);
524 static void dsa_tree_remove_switch(struct dsa_switch_tree *dst,
527 dsa_tree_teardown(dst);
529 dst->ds[index] = NULL;
533 static int dsa_tree_add_switch(struct dsa_switch_tree *dst,
534 struct dsa_switch *ds)
536 unsigned int index = ds->index;
545 err = dsa_tree_setup(dst);
547 dsa_tree_remove_switch(dst, index);
552 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
557 dp->type = DSA_PORT_TYPE_USER;
563 static int dsa_port_parse_dsa(struct dsa_port *dp)
565 dp->type = DSA_PORT_TYPE_DSA;
570 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
572 struct dsa_switch *ds = dp->ds;
573 struct dsa_switch_tree *dst = ds->dst;
574 const struct dsa_device_ops *tag_ops;
575 enum dsa_tag_protocol tag_protocol;
577 tag_protocol = ds->ops->get_tag_protocol(ds, dp->index);
578 tag_ops = dsa_tag_driver_get(tag_protocol);
579 if (IS_ERR(tag_ops)) {
580 dev_warn(ds->dev, "No tagger for this switch\n");
581 return PTR_ERR(tag_ops);
584 dp->type = DSA_PORT_TYPE_CPU;
585 dp->filter = tag_ops->filter;
586 dp->rcv = tag_ops->rcv;
587 dp->tag_ops = tag_ops;
594 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
596 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
597 const char *name = of_get_property(dn, "label", NULL);
598 bool link = of_property_read_bool(dn, "link");
603 struct net_device *master;
605 master = of_find_net_device_by_node(ethernet);
607 return -EPROBE_DEFER;
609 return dsa_port_parse_cpu(dp, master);
613 return dsa_port_parse_dsa(dp);
615 return dsa_port_parse_user(dp, name);
618 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
619 struct device_node *dn)
621 struct device_node *ports, *port;
626 ports = of_get_child_by_name(dn, "ports");
628 dev_err(ds->dev, "no ports child node found\n");
632 for_each_available_child_of_node(ports, port) {
633 err = of_property_read_u32(port, "reg", ®);
637 if (reg >= ds->num_ports) {
642 dp = &ds->ports[reg];
644 err = dsa_port_parse_of(dp, port);
654 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
655 struct device_node *dn)
660 /* Don't error out if this optional property isn't found */
661 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
662 if (sz < 0 && sz != -EINVAL)
666 if (ds->index >= DSA_MAX_SWITCHES)
669 ds->dst = dsa_tree_touch(m[0]);
676 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
680 err = dsa_switch_parse_member_of(ds, dn);
684 return dsa_switch_parse_ports_of(ds, dn);
687 static int dsa_port_parse(struct dsa_port *dp, const char *name,
690 if (!strcmp(name, "cpu")) {
691 struct net_device *master;
693 master = dsa_dev_to_net_device(dev);
695 return -EPROBE_DEFER;
699 return dsa_port_parse_cpu(dp, master);
702 if (!strcmp(name, "dsa"))
703 return dsa_port_parse_dsa(dp);
705 return dsa_port_parse_user(dp, name);
708 static int dsa_switch_parse_ports(struct dsa_switch *ds,
709 struct dsa_chip_data *cd)
711 bool valid_name_found = false;
718 for (i = 0; i < DSA_MAX_PORTS; i++) {
719 name = cd->port_names[i];
726 err = dsa_port_parse(dp, name, dev);
730 valid_name_found = true;
733 if (!valid_name_found && i == DSA_MAX_PORTS)
739 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
743 /* We don't support interconnected switches nor multiple trees via
744 * platform data, so this is the unique switch of the tree.
747 ds->dst = dsa_tree_touch(0);
751 return dsa_switch_parse_ports(ds, cd);
754 static int dsa_switch_add(struct dsa_switch *ds)
756 struct dsa_switch_tree *dst = ds->dst;
758 return dsa_tree_add_switch(dst, ds);
761 static int dsa_switch_probe(struct dsa_switch *ds)
763 struct dsa_chip_data *pdata = ds->dev->platform_data;
764 struct device_node *np = ds->dev->of_node;
768 err = dsa_switch_parse_of(ds, np);
770 err = dsa_switch_parse(ds, pdata);
777 return dsa_switch_add(ds);
780 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
782 struct dsa_switch *ds;
785 ds = devm_kzalloc(dev, struct_size(ds, ports, n), GFP_KERNEL);
789 /* We avoid allocating memory outside dsa_switch
790 * if it is not needed.
792 if (n <= sizeof(ds->_bitmap) * 8) {
793 ds->bitmap = &ds->_bitmap;
795 ds->bitmap = devm_kcalloc(dev,
797 sizeof(unsigned long),
799 if (unlikely(!ds->bitmap))
806 for (i = 0; i < ds->num_ports; ++i) {
807 ds->ports[i].index = i;
808 ds->ports[i].ds = ds;
813 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
815 int dsa_register_switch(struct dsa_switch *ds)
819 mutex_lock(&dsa2_mutex);
820 err = dsa_switch_probe(ds);
821 dsa_tree_put(ds->dst);
822 mutex_unlock(&dsa2_mutex);
826 EXPORT_SYMBOL_GPL(dsa_register_switch);
828 static void dsa_switch_remove(struct dsa_switch *ds)
830 struct dsa_switch_tree *dst = ds->dst;
831 unsigned int index = ds->index;
833 dsa_tree_remove_switch(dst, index);
836 void dsa_unregister_switch(struct dsa_switch *ds)
838 mutex_lock(&dsa2_mutex);
839 dsa_switch_remove(ds);
840 mutex_unlock(&dsa2_mutex);
842 EXPORT_SYMBOL_GPL(dsa_unregister_switch);