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 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
);
302 case DSA_PORT_TYPE_DSA
:
303 err
= dsa_port_link_register_of(dp
);
305 dev_err(ds
->dev
, "failed to setup link for port %d.%d\n",
306 ds
->index
, dp
->index
);
308 case DSA_PORT_TYPE_USER
:
309 err
= dsa_slave_create(dp
);
311 dev_err(ds
->dev
, "failed to create slave for port %d.%d\n",
312 ds
->index
, dp
->index
);
314 devlink_port_type_eth_set(&dp
->devlink_port
, dp
->slave
);
319 devlink_port_unregister(&dp
->devlink_port
);
324 static void dsa_port_teardown(struct dsa_port
*dp
)
326 if (dp
->type
!= DSA_PORT_TYPE_UNUSED
)
327 devlink_port_unregister(&dp
->devlink_port
);
330 case DSA_PORT_TYPE_UNUSED
:
332 case DSA_PORT_TYPE_CPU
:
333 dsa_tag_driver_put(dp
->tag_ops
);
335 case DSA_PORT_TYPE_DSA
:
336 dsa_port_link_unregister_of(dp
);
338 case DSA_PORT_TYPE_USER
:
340 dsa_slave_destroy(dp
->slave
);
347 static int dsa_switch_setup(struct dsa_switch
*ds
)
351 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
352 * driver and before ops->setup() has run, since the switch drivers and
353 * the slave MDIO bus driver rely on these values for probing PHY
356 ds
->phys_mii_mask
|= dsa_user_ports(ds
);
358 /* Add the switch to devlink before calling setup, so that setup can
361 ds
->devlink
= devlink_alloc(&dsa_devlink_ops
, 0);
365 err
= devlink_register(ds
->devlink
, ds
->dev
);
369 err
= dsa_switch_register_notifier(ds
);
371 goto unregister_devlink
;
373 err
= ds
->ops
->setup(ds
);
375 goto unregister_notifier
;
377 if (!ds
->slave_mii_bus
&& ds
->ops
->phy_read
) {
378 ds
->slave_mii_bus
= devm_mdiobus_alloc(ds
->dev
);
379 if (!ds
->slave_mii_bus
) {
381 goto unregister_notifier
;
384 dsa_slave_mii_bus_init(ds
);
386 err
= mdiobus_register(ds
->slave_mii_bus
);
388 goto unregister_notifier
;
394 dsa_switch_unregister_notifier(ds
);
396 devlink_unregister(ds
->devlink
);
398 devlink_free(ds
->devlink
);
404 static void dsa_switch_teardown(struct dsa_switch
*ds
)
406 if (ds
->slave_mii_bus
&& ds
->ops
->phy_read
)
407 mdiobus_unregister(ds
->slave_mii_bus
);
409 dsa_switch_unregister_notifier(ds
);
411 if (ds
->ops
->teardown
)
412 ds
->ops
->teardown(ds
);
415 devlink_unregister(ds
->devlink
);
416 devlink_free(ds
->devlink
);
422 static int dsa_tree_setup_switches(struct dsa_switch_tree
*dst
)
424 struct dsa_switch
*ds
;
429 for (device
= 0; device
< DSA_MAX_SWITCHES
; device
++) {
430 ds
= dst
->ds
[device
];
434 err
= dsa_switch_setup(ds
);
436 goto switch_teardown
;
438 for (port
= 0; port
< ds
->num_ports
; port
++) {
439 dp
= &ds
->ports
[port
];
441 err
= dsa_port_setup(dp
);
450 for (i
= 0; i
< port
; i
++)
451 dsa_port_teardown(&ds
->ports
[i
]);
453 dsa_switch_teardown(ds
);
456 for (i
= 0; i
< device
; i
++) {
461 for (port
= 0; port
< ds
->num_ports
; port
++) {
462 dp
= &ds
->ports
[port
];
464 dsa_port_teardown(dp
);
467 dsa_switch_teardown(ds
);
473 static void dsa_tree_teardown_switches(struct dsa_switch_tree
*dst
)
475 struct dsa_switch
*ds
;
479 for (device
= 0; device
< DSA_MAX_SWITCHES
; device
++) {
480 ds
= dst
->ds
[device
];
484 for (port
= 0; port
< ds
->num_ports
; port
++) {
485 dp
= &ds
->ports
[port
];
487 dsa_port_teardown(dp
);
490 dsa_switch_teardown(ds
);
494 static int dsa_tree_setup_master(struct dsa_switch_tree
*dst
)
496 struct dsa_port
*cpu_dp
= dst
->cpu_dp
;
497 struct net_device
*master
= cpu_dp
->master
;
499 /* DSA currently supports a single pair of CPU port and master device */
500 return dsa_master_setup(master
, cpu_dp
);
503 static void dsa_tree_teardown_master(struct dsa_switch_tree
*dst
)
505 struct dsa_port
*cpu_dp
= dst
->cpu_dp
;
506 struct net_device
*master
= cpu_dp
->master
;
508 return dsa_master_teardown(master
);
511 static int dsa_tree_setup(struct dsa_switch_tree
*dst
)
517 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
522 complete
= dsa_tree_setup_routing_table(dst
);
526 err
= dsa_tree_setup_default_cpu(dst
);
530 err
= dsa_tree_setup_switches(dst
);
532 goto teardown_default_cpu
;
534 err
= dsa_tree_setup_master(dst
);
536 goto teardown_switches
;
540 pr_info("DSA: tree %d setup\n", dst
->index
);
545 dsa_tree_teardown_switches(dst
);
546 teardown_default_cpu
:
547 dsa_tree_teardown_default_cpu(dst
);
552 static void dsa_tree_teardown(struct dsa_switch_tree
*dst
)
557 dsa_tree_teardown_master(dst
);
559 dsa_tree_teardown_switches(dst
);
561 dsa_tree_teardown_default_cpu(dst
);
563 pr_info("DSA: tree %d torn down\n", dst
->index
);
568 static void dsa_tree_remove_switch(struct dsa_switch_tree
*dst
,
571 dsa_tree_teardown(dst
);
573 dst
->ds
[index
] = NULL
;
577 static int dsa_tree_add_switch(struct dsa_switch_tree
*dst
,
578 struct dsa_switch
*ds
)
580 unsigned int index
= ds
->index
;
589 err
= dsa_tree_setup(dst
);
591 dst
->ds
[index
] = NULL
;
598 static int dsa_port_parse_user(struct dsa_port
*dp
, const char *name
)
603 dp
->type
= DSA_PORT_TYPE_USER
;
609 static int dsa_port_parse_dsa(struct dsa_port
*dp
)
611 dp
->type
= DSA_PORT_TYPE_DSA
;
616 static int dsa_port_parse_cpu(struct dsa_port
*dp
, struct net_device
*master
)
618 struct dsa_switch
*ds
= dp
->ds
;
619 struct dsa_switch_tree
*dst
= ds
->dst
;
620 const struct dsa_device_ops
*tag_ops
;
621 enum dsa_tag_protocol tag_protocol
;
623 tag_protocol
= ds
->ops
->get_tag_protocol(ds
, dp
->index
);
624 tag_ops
= dsa_tag_driver_get(tag_protocol
);
625 if (IS_ERR(tag_ops
)) {
626 dev_warn(ds
->dev
, "No tagger for this switch\n");
627 return PTR_ERR(tag_ops
);
630 dp
->type
= DSA_PORT_TYPE_CPU
;
631 dp
->filter
= tag_ops
->filter
;
632 dp
->rcv
= tag_ops
->rcv
;
633 dp
->tag_ops
= tag_ops
;
640 static int dsa_port_parse_of(struct dsa_port
*dp
, struct device_node
*dn
)
642 struct device_node
*ethernet
= of_parse_phandle(dn
, "ethernet", 0);
643 const char *name
= of_get_property(dn
, "label", NULL
);
644 bool link
= of_property_read_bool(dn
, "link");
649 struct net_device
*master
;
651 master
= of_find_net_device_by_node(ethernet
);
653 return -EPROBE_DEFER
;
655 return dsa_port_parse_cpu(dp
, master
);
659 return dsa_port_parse_dsa(dp
);
661 return dsa_port_parse_user(dp
, name
);
664 static int dsa_switch_parse_ports_of(struct dsa_switch
*ds
,
665 struct device_node
*dn
)
667 struct device_node
*ports
, *port
;
672 ports
= of_get_child_by_name(dn
, "ports");
674 dev_err(ds
->dev
, "no ports child node found\n");
678 for_each_available_child_of_node(ports
, port
) {
679 err
= of_property_read_u32(port
, "reg", ®
);
683 if (reg
>= ds
->num_ports
) {
688 dp
= &ds
->ports
[reg
];
690 err
= dsa_port_parse_of(dp
, port
);
700 static int dsa_switch_parse_member_of(struct dsa_switch
*ds
,
701 struct device_node
*dn
)
706 /* Don't error out if this optional property isn't found */
707 sz
= of_property_read_variable_u32_array(dn
, "dsa,member", m
, 2, 2);
708 if (sz
< 0 && sz
!= -EINVAL
)
712 if (ds
->index
>= DSA_MAX_SWITCHES
)
715 ds
->dst
= dsa_tree_touch(m
[0]);
722 static int dsa_switch_parse_of(struct dsa_switch
*ds
, struct device_node
*dn
)
726 err
= dsa_switch_parse_member_of(ds
, dn
);
730 return dsa_switch_parse_ports_of(ds
, dn
);
733 static int dsa_port_parse(struct dsa_port
*dp
, const char *name
,
736 if (!strcmp(name
, "cpu")) {
737 struct net_device
*master
;
739 master
= dsa_dev_to_net_device(dev
);
741 return -EPROBE_DEFER
;
745 return dsa_port_parse_cpu(dp
, master
);
748 if (!strcmp(name
, "dsa"))
749 return dsa_port_parse_dsa(dp
);
751 return dsa_port_parse_user(dp
, name
);
754 static int dsa_switch_parse_ports(struct dsa_switch
*ds
,
755 struct dsa_chip_data
*cd
)
757 bool valid_name_found
= false;
764 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
765 name
= cd
->port_names
[i
];
772 err
= dsa_port_parse(dp
, name
, dev
);
776 valid_name_found
= true;
779 if (!valid_name_found
&& i
== DSA_MAX_PORTS
)
785 static int dsa_switch_parse(struct dsa_switch
*ds
, struct dsa_chip_data
*cd
)
789 /* We don't support interconnected switches nor multiple trees via
790 * platform data, so this is the unique switch of the tree.
793 ds
->dst
= dsa_tree_touch(0);
797 return dsa_switch_parse_ports(ds
, cd
);
800 static int dsa_switch_add(struct dsa_switch
*ds
)
802 struct dsa_switch_tree
*dst
= ds
->dst
;
804 return dsa_tree_add_switch(dst
, ds
);
807 static int dsa_switch_probe(struct dsa_switch
*ds
)
809 struct dsa_chip_data
*pdata
= ds
->dev
->platform_data
;
810 struct device_node
*np
= ds
->dev
->of_node
;
814 err
= dsa_switch_parse_of(ds
, np
);
816 err
= dsa_switch_parse(ds
, pdata
);
823 return dsa_switch_add(ds
);
826 struct dsa_switch
*dsa_switch_alloc(struct device
*dev
, size_t n
)
828 struct dsa_switch
*ds
;
831 ds
= devm_kzalloc(dev
, struct_size(ds
, ports
, n
), GFP_KERNEL
);
835 /* We avoid allocating memory outside dsa_switch
836 * if it is not needed.
838 if (n
<= sizeof(ds
->_bitmap
) * 8) {
839 ds
->bitmap
= &ds
->_bitmap
;
841 ds
->bitmap
= devm_kcalloc(dev
,
843 sizeof(unsigned long),
845 if (unlikely(!ds
->bitmap
))
852 for (i
= 0; i
< ds
->num_ports
; ++i
) {
853 ds
->ports
[i
].index
= i
;
854 ds
->ports
[i
].ds
= ds
;
859 EXPORT_SYMBOL_GPL(dsa_switch_alloc
);
861 int dsa_register_switch(struct dsa_switch
*ds
)
865 mutex_lock(&dsa2_mutex
);
866 err
= dsa_switch_probe(ds
);
867 dsa_tree_put(ds
->dst
);
868 mutex_unlock(&dsa2_mutex
);
872 EXPORT_SYMBOL_GPL(dsa_register_switch
);
874 static void dsa_switch_remove(struct dsa_switch
*ds
)
876 struct dsa_switch_tree
*dst
= ds
->dst
;
877 unsigned int index
= ds
->index
;
879 dsa_tree_remove_switch(dst
, index
);
882 void dsa_unregister_switch(struct dsa_switch
*ds
)
884 mutex_lock(&dsa2_mutex
);
885 dsa_switch_remove(ds
);
886 mutex_unlock(&dsa2_mutex
);
888 EXPORT_SYMBOL_GPL(dsa_unregister_switch
);