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(&dst
->list
, &dsa_tree_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 if (PTR_ERR(tag_ops
) == -ENOPROTOOPT
)
627 return -EPROBE_DEFER
;
628 dev_warn(ds
->dev
, "No tagger for this switch\n");
629 return PTR_ERR(tag_ops
);
632 dp
->type
= DSA_PORT_TYPE_CPU
;
633 dp
->filter
= tag_ops
->filter
;
634 dp
->rcv
= tag_ops
->rcv
;
635 dp
->tag_ops
= tag_ops
;
642 static int dsa_port_parse_of(struct dsa_port
*dp
, struct device_node
*dn
)
644 struct device_node
*ethernet
= of_parse_phandle(dn
, "ethernet", 0);
645 const char *name
= of_get_property(dn
, "label", NULL
);
646 bool link
= of_property_read_bool(dn
, "link");
651 struct net_device
*master
;
653 master
= of_find_net_device_by_node(ethernet
);
655 return -EPROBE_DEFER
;
657 return dsa_port_parse_cpu(dp
, master
);
661 return dsa_port_parse_dsa(dp
);
663 return dsa_port_parse_user(dp
, name
);
666 static int dsa_switch_parse_ports_of(struct dsa_switch
*ds
,
667 struct device_node
*dn
)
669 struct device_node
*ports
, *port
;
674 ports
= of_get_child_by_name(dn
, "ports");
676 dev_err(ds
->dev
, "no ports child node found\n");
680 for_each_available_child_of_node(ports
, port
) {
681 err
= of_property_read_u32(port
, "reg", ®
);
685 if (reg
>= ds
->num_ports
) {
690 dp
= &ds
->ports
[reg
];
692 err
= dsa_port_parse_of(dp
, port
);
702 static int dsa_switch_parse_member_of(struct dsa_switch
*ds
,
703 struct device_node
*dn
)
708 /* Don't error out if this optional property isn't found */
709 sz
= of_property_read_variable_u32_array(dn
, "dsa,member", m
, 2, 2);
710 if (sz
< 0 && sz
!= -EINVAL
)
714 if (ds
->index
>= DSA_MAX_SWITCHES
)
717 ds
->dst
= dsa_tree_touch(m
[0]);
724 static int dsa_switch_parse_of(struct dsa_switch
*ds
, struct device_node
*dn
)
728 err
= dsa_switch_parse_member_of(ds
, dn
);
732 return dsa_switch_parse_ports_of(ds
, dn
);
735 static int dsa_port_parse(struct dsa_port
*dp
, const char *name
,
738 if (!strcmp(name
, "cpu")) {
739 struct net_device
*master
;
741 master
= dsa_dev_to_net_device(dev
);
743 return -EPROBE_DEFER
;
747 return dsa_port_parse_cpu(dp
, master
);
750 if (!strcmp(name
, "dsa"))
751 return dsa_port_parse_dsa(dp
);
753 return dsa_port_parse_user(dp
, name
);
756 static int dsa_switch_parse_ports(struct dsa_switch
*ds
,
757 struct dsa_chip_data
*cd
)
759 bool valid_name_found
= false;
766 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
767 name
= cd
->port_names
[i
];
774 err
= dsa_port_parse(dp
, name
, dev
);
778 valid_name_found
= true;
781 if (!valid_name_found
&& i
== DSA_MAX_PORTS
)
787 static int dsa_switch_parse(struct dsa_switch
*ds
, struct dsa_chip_data
*cd
)
791 /* We don't support interconnected switches nor multiple trees via
792 * platform data, so this is the unique switch of the tree.
795 ds
->dst
= dsa_tree_touch(0);
799 return dsa_switch_parse_ports(ds
, cd
);
802 static int dsa_switch_add(struct dsa_switch
*ds
)
804 struct dsa_switch_tree
*dst
= ds
->dst
;
806 return dsa_tree_add_switch(dst
, ds
);
809 static int dsa_switch_probe(struct dsa_switch
*ds
)
811 struct dsa_chip_data
*pdata
= ds
->dev
->platform_data
;
812 struct device_node
*np
= ds
->dev
->of_node
;
816 err
= dsa_switch_parse_of(ds
, np
);
818 err
= dsa_switch_parse(ds
, pdata
);
825 return dsa_switch_add(ds
);
828 struct dsa_switch
*dsa_switch_alloc(struct device
*dev
, size_t n
)
830 struct dsa_switch
*ds
;
833 ds
= devm_kzalloc(dev
, struct_size(ds
, ports
, n
), GFP_KERNEL
);
837 /* We avoid allocating memory outside dsa_switch
838 * if it is not needed.
840 if (n
<= sizeof(ds
->_bitmap
) * 8) {
841 ds
->bitmap
= &ds
->_bitmap
;
843 ds
->bitmap
= devm_kcalloc(dev
,
845 sizeof(unsigned long),
847 if (unlikely(!ds
->bitmap
))
854 for (i
= 0; i
< ds
->num_ports
; ++i
) {
855 ds
->ports
[i
].index
= i
;
856 ds
->ports
[i
].ds
= ds
;
861 EXPORT_SYMBOL_GPL(dsa_switch_alloc
);
863 int dsa_register_switch(struct dsa_switch
*ds
)
867 mutex_lock(&dsa2_mutex
);
868 err
= dsa_switch_probe(ds
);
869 dsa_tree_put(ds
->dst
);
870 mutex_unlock(&dsa2_mutex
);
874 EXPORT_SYMBOL_GPL(dsa_register_switch
);
876 static void dsa_switch_remove(struct dsa_switch
*ds
)
878 struct dsa_switch_tree
*dst
= ds
->dst
;
879 unsigned int index
= ds
->index
;
881 dsa_tree_remove_switch(dst
, index
);
884 void dsa_unregister_switch(struct dsa_switch
*ds
)
886 mutex_lock(&dsa2_mutex
);
887 dsa_switch_remove(ds
);
888 mutex_unlock(&dsa2_mutex
);
890 EXPORT_SYMBOL_GPL(dsa_unregister_switch
);