2 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
3 * Copyright (c) 2008-2009 Marvell Semiconductor
4 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
5 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/list.h>
16 #include <linux/netdevice.h>
17 #include <linux/slab.h>
18 #include <linux/rtnetlink.h>
20 #include <linux/of_net.h>
24 static LIST_HEAD(dsa_tree_list
);
25 static DEFINE_MUTEX(dsa2_mutex
);
27 static const struct devlink_ops dsa_devlink_ops
= {
30 static struct dsa_switch_tree
*dsa_tree_find(int index
)
32 struct dsa_switch_tree
*dst
;
34 list_for_each_entry(dst
, &dsa_tree_list
, list
)
35 if (dst
->index
== index
)
41 static struct dsa_switch_tree
*dsa_tree_alloc(int index
)
43 struct dsa_switch_tree
*dst
;
45 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
51 INIT_LIST_HEAD(&dst
->list
);
52 list_add_tail(&dsa_tree_list
, &dst
->list
);
54 /* Initialize the reference counter to the number of switches, not 1 */
55 kref_init(&dst
->refcount
);
56 refcount_set(&dst
->refcount
.refcount
, 0);
61 static void dsa_tree_free(struct dsa_switch_tree
*dst
)
67 static struct dsa_switch_tree
*dsa_tree_touch(int index
)
69 struct dsa_switch_tree
*dst
;
71 dst
= dsa_tree_find(index
);
73 dst
= dsa_tree_alloc(index
);
78 static void dsa_tree_get(struct dsa_switch_tree
*dst
)
80 kref_get(&dst
->refcount
);
83 static void dsa_tree_release(struct kref
*ref
)
85 struct dsa_switch_tree
*dst
;
87 dst
= container_of(ref
, struct dsa_switch_tree
, refcount
);
92 static void dsa_tree_put(struct dsa_switch_tree
*dst
)
94 kref_put(&dst
->refcount
, dsa_tree_release
);
97 static bool dsa_port_is_dsa(struct dsa_port
*port
)
99 return port
->type
== DSA_PORT_TYPE_DSA
;
102 static bool dsa_port_is_cpu(struct dsa_port
*port
)
104 return port
->type
== DSA_PORT_TYPE_CPU
;
107 static bool dsa_port_is_user(struct dsa_port
*dp
)
109 return dp
->type
== DSA_PORT_TYPE_USER
;
112 static struct dsa_port
*dsa_tree_find_port_by_node(struct dsa_switch_tree
*dst
,
113 struct device_node
*dn
)
115 struct dsa_switch
*ds
;
119 for (device
= 0; device
< DSA_MAX_SWITCHES
; device
++) {
120 ds
= dst
->ds
[device
];
124 for (port
= 0; port
< ds
->num_ports
; port
++) {
125 dp
= &ds
->ports
[port
];
135 static bool dsa_port_setup_routing_table(struct dsa_port
*dp
)
137 struct dsa_switch
*ds
= dp
->ds
;
138 struct dsa_switch_tree
*dst
= ds
->dst
;
139 struct device_node
*dn
= dp
->dn
;
140 struct of_phandle_iterator it
;
141 struct dsa_port
*link_dp
;
144 of_for_each_phandle(&it
, err
, dn
, "link", NULL
, 0) {
145 link_dp
= dsa_tree_find_port_by_node(dst
, it
.node
);
147 of_node_put(it
.node
);
151 ds
->rtable
[link_dp
->ds
->index
] = dp
->index
;
157 static bool dsa_switch_setup_routing_table(struct dsa_switch
*ds
)
159 bool complete
= true;
163 for (i
= 0; i
< DSA_MAX_SWITCHES
; i
++)
164 ds
->rtable
[i
] = DSA_RTABLE_NONE
;
166 for (i
= 0; i
< ds
->num_ports
; i
++) {
169 if (dsa_port_is_dsa(dp
)) {
170 complete
= dsa_port_setup_routing_table(dp
);
179 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree
*dst
)
181 struct dsa_switch
*ds
;
182 bool complete
= true;
185 for (device
= 0; device
< DSA_MAX_SWITCHES
; device
++) {
186 ds
= dst
->ds
[device
];
190 complete
= dsa_switch_setup_routing_table(ds
);
198 static struct dsa_port
*dsa_tree_find_first_cpu(struct dsa_switch_tree
*dst
)
200 struct dsa_switch
*ds
;
204 for (device
= 0; device
< DSA_MAX_SWITCHES
; device
++) {
205 ds
= dst
->ds
[device
];
209 for (port
= 0; port
< ds
->num_ports
; port
++) {
210 dp
= &ds
->ports
[port
];
212 if (dsa_port_is_cpu(dp
))
220 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree
*dst
)
222 struct dsa_switch
*ds
;
226 /* DSA currently only supports a single CPU port */
227 dst
->cpu_dp
= dsa_tree_find_first_cpu(dst
);
229 pr_warn("Tree has no master device\n");
233 /* Assign the default CPU port to all ports of the fabric */
234 for (device
= 0; device
< DSA_MAX_SWITCHES
; device
++) {
235 ds
= dst
->ds
[device
];
239 for (port
= 0; port
< ds
->num_ports
; port
++) {
240 dp
= &ds
->ports
[port
];
242 if (dsa_port_is_user(dp
))
243 dp
->cpu_dp
= dst
->cpu_dp
;
250 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree
*dst
)
252 /* DSA currently only supports a single CPU port */
256 static int dsa_port_setup(struct dsa_port
*dp
)
258 struct dsa_switch
*ds
= dp
->ds
;
261 memset(&dp
->devlink_port
, 0, sizeof(dp
->devlink_port
));
263 err
= devlink_port_register(ds
->devlink
, &dp
->devlink_port
, dp
->index
);
268 case DSA_PORT_TYPE_UNUSED
:
270 case DSA_PORT_TYPE_CPU
:
271 case DSA_PORT_TYPE_DSA
:
272 err
= dsa_port_fixed_link_register_of(dp
);
274 dev_err(ds
->dev
, "failed to register fixed link for port %d.%d\n",
275 ds
->index
, dp
->index
);
280 case DSA_PORT_TYPE_USER
:
281 err
= dsa_slave_create(dp
);
283 dev_err(ds
->dev
, "failed to create slave for port %d.%d\n",
284 ds
->index
, dp
->index
);
286 devlink_port_type_eth_set(&dp
->devlink_port
, dp
->slave
);
293 static void dsa_port_teardown(struct dsa_port
*dp
)
295 devlink_port_unregister(&dp
->devlink_port
);
298 case DSA_PORT_TYPE_UNUSED
:
300 case DSA_PORT_TYPE_CPU
:
301 case DSA_PORT_TYPE_DSA
:
302 dsa_port_fixed_link_unregister_of(dp
);
304 case DSA_PORT_TYPE_USER
:
306 dsa_slave_destroy(dp
->slave
);
313 static int dsa_switch_setup(struct dsa_switch
*ds
)
317 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
318 * driver and before ops->setup() has run, since the switch drivers and
319 * the slave MDIO bus driver rely on these values for probing PHY
322 ds
->phys_mii_mask
|= dsa_user_ports(ds
);
324 /* Add the switch to devlink before calling setup, so that setup can
327 ds
->devlink
= devlink_alloc(&dsa_devlink_ops
, 0);
331 err
= devlink_register(ds
->devlink
, ds
->dev
);
335 err
= ds
->ops
->setup(ds
);
339 err
= dsa_switch_register_notifier(ds
);
343 if (!ds
->slave_mii_bus
&& ds
->ops
->phy_read
) {
344 ds
->slave_mii_bus
= devm_mdiobus_alloc(ds
->dev
);
345 if (!ds
->slave_mii_bus
)
348 dsa_slave_mii_bus_init(ds
);
350 err
= mdiobus_register(ds
->slave_mii_bus
);
358 static void dsa_switch_teardown(struct dsa_switch
*ds
)
360 if (ds
->slave_mii_bus
&& ds
->ops
->phy_read
)
361 mdiobus_unregister(ds
->slave_mii_bus
);
363 dsa_switch_unregister_notifier(ds
);
366 devlink_unregister(ds
->devlink
);
367 devlink_free(ds
->devlink
);
373 static int dsa_tree_setup_switches(struct dsa_switch_tree
*dst
)
375 struct dsa_switch
*ds
;
380 for (device
= 0; device
< DSA_MAX_SWITCHES
; device
++) {
381 ds
= dst
->ds
[device
];
385 err
= dsa_switch_setup(ds
);
389 for (port
= 0; port
< ds
->num_ports
; port
++) {
390 dp
= &ds
->ports
[port
];
392 err
= dsa_port_setup(dp
);
401 static void dsa_tree_teardown_switches(struct dsa_switch_tree
*dst
)
403 struct dsa_switch
*ds
;
407 for (device
= 0; device
< DSA_MAX_SWITCHES
; device
++) {
408 ds
= dst
->ds
[device
];
412 for (port
= 0; port
< ds
->num_ports
; port
++) {
413 dp
= &ds
->ports
[port
];
415 dsa_port_teardown(dp
);
418 dsa_switch_teardown(ds
);
422 static int dsa_tree_setup_master(struct dsa_switch_tree
*dst
)
424 struct dsa_port
*cpu_dp
= dst
->cpu_dp
;
425 struct net_device
*master
= cpu_dp
->master
;
427 /* DSA currently supports a single pair of CPU port and master device */
428 return dsa_master_setup(master
, cpu_dp
);
431 static void dsa_tree_teardown_master(struct dsa_switch_tree
*dst
)
433 struct dsa_port
*cpu_dp
= dst
->cpu_dp
;
434 struct net_device
*master
= cpu_dp
->master
;
436 return dsa_master_teardown(master
);
439 static int dsa_tree_setup(struct dsa_switch_tree
*dst
)
445 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
450 complete
= dsa_tree_setup_routing_table(dst
);
454 err
= dsa_tree_setup_default_cpu(dst
);
458 err
= dsa_tree_setup_switches(dst
);
462 err
= dsa_tree_setup_master(dst
);
468 pr_info("DSA: tree %d setup\n", dst
->index
);
473 static void dsa_tree_teardown(struct dsa_switch_tree
*dst
)
478 dsa_tree_teardown_master(dst
);
480 dsa_tree_teardown_switches(dst
);
482 dsa_tree_teardown_default_cpu(dst
);
484 pr_info("DSA: tree %d torn down\n", dst
->index
);
489 static void dsa_tree_remove_switch(struct dsa_switch_tree
*dst
,
492 dsa_tree_teardown(dst
);
494 dst
->ds
[index
] = NULL
;
498 static int dsa_tree_add_switch(struct dsa_switch_tree
*dst
,
499 struct dsa_switch
*ds
)
501 unsigned int index
= ds
->index
;
510 err
= dsa_tree_setup(dst
);
512 dsa_tree_remove_switch(dst
, index
);
517 static int dsa_port_parse_user(struct dsa_port
*dp
, const char *name
)
522 dp
->type
= DSA_PORT_TYPE_USER
;
528 static int dsa_port_parse_dsa(struct dsa_port
*dp
)
530 dp
->type
= DSA_PORT_TYPE_DSA
;
535 static int dsa_port_parse_cpu(struct dsa_port
*dp
, struct net_device
*master
)
537 struct dsa_switch
*ds
= dp
->ds
;
538 struct dsa_switch_tree
*dst
= ds
->dst
;
539 const struct dsa_device_ops
*tag_ops
;
540 enum dsa_tag_protocol tag_protocol
;
542 tag_protocol
= ds
->ops
->get_tag_protocol(ds
, dp
->index
);
543 tag_ops
= dsa_resolve_tag_protocol(tag_protocol
);
544 if (IS_ERR(tag_ops
)) {
545 dev_warn(ds
->dev
, "No tagger for this switch\n");
546 return PTR_ERR(tag_ops
);
549 dp
->type
= DSA_PORT_TYPE_CPU
;
550 dp
->rcv
= tag_ops
->rcv
;
551 dp
->tag_ops
= tag_ops
;
558 static int dsa_port_parse_of(struct dsa_port
*dp
, struct device_node
*dn
)
560 struct device_node
*ethernet
= of_parse_phandle(dn
, "ethernet", 0);
561 const char *name
= of_get_property(dn
, "label", NULL
);
562 bool link
= of_property_read_bool(dn
, "link");
567 struct net_device
*master
;
569 master
= of_find_net_device_by_node(ethernet
);
571 return -EPROBE_DEFER
;
573 return dsa_port_parse_cpu(dp
, master
);
577 return dsa_port_parse_dsa(dp
);
579 return dsa_port_parse_user(dp
, name
);
582 static int dsa_switch_parse_ports_of(struct dsa_switch
*ds
,
583 struct device_node
*dn
)
585 struct device_node
*ports
, *port
;
590 ports
= of_get_child_by_name(dn
, "ports");
592 dev_err(ds
->dev
, "no ports child node found\n");
596 for_each_available_child_of_node(ports
, port
) {
597 err
= of_property_read_u32(port
, "reg", ®
);
601 if (reg
>= ds
->num_ports
)
604 dp
= &ds
->ports
[reg
];
606 err
= dsa_port_parse_of(dp
, port
);
614 static int dsa_switch_parse_member_of(struct dsa_switch
*ds
,
615 struct device_node
*dn
)
620 /* Don't error out if this optional property isn't found */
621 sz
= of_property_read_variable_u32_array(dn
, "dsa,member", m
, 2, 2);
622 if (sz
< 0 && sz
!= -EINVAL
)
626 if (ds
->index
>= DSA_MAX_SWITCHES
)
629 ds
->dst
= dsa_tree_touch(m
[0]);
636 static int dsa_switch_parse_of(struct dsa_switch
*ds
, struct device_node
*dn
)
640 err
= dsa_switch_parse_member_of(ds
, dn
);
644 return dsa_switch_parse_ports_of(ds
, dn
);
647 static int dsa_port_parse(struct dsa_port
*dp
, const char *name
,
650 if (!strcmp(name
, "cpu")) {
651 struct net_device
*master
;
653 master
= dsa_dev_to_net_device(dev
);
655 return -EPROBE_DEFER
;
659 return dsa_port_parse_cpu(dp
, master
);
662 if (!strcmp(name
, "dsa"))
663 return dsa_port_parse_dsa(dp
);
665 return dsa_port_parse_user(dp
, name
);
668 static int dsa_switch_parse_ports(struct dsa_switch
*ds
,
669 struct dsa_chip_data
*cd
)
671 bool valid_name_found
= false;
678 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
679 name
= cd
->port_names
[i
];
686 err
= dsa_port_parse(dp
, name
, dev
);
690 valid_name_found
= true;
693 if (!valid_name_found
&& i
== DSA_MAX_PORTS
)
699 static int dsa_switch_parse(struct dsa_switch
*ds
, struct dsa_chip_data
*cd
)
703 /* We don't support interconnected switches nor multiple trees via
704 * platform data, so this is the unique switch of the tree.
707 ds
->dst
= dsa_tree_touch(0);
711 return dsa_switch_parse_ports(ds
, cd
);
714 static int dsa_switch_add(struct dsa_switch
*ds
)
716 struct dsa_switch_tree
*dst
= ds
->dst
;
718 return dsa_tree_add_switch(dst
, ds
);
721 static int dsa_switch_probe(struct dsa_switch
*ds
)
723 struct dsa_chip_data
*pdata
= ds
->dev
->platform_data
;
724 struct device_node
*np
= ds
->dev
->of_node
;
728 err
= dsa_switch_parse_of(ds
, np
);
730 err
= dsa_switch_parse(ds
, pdata
);
737 return dsa_switch_add(ds
);
740 struct dsa_switch
*dsa_switch_alloc(struct device
*dev
, size_t n
)
742 size_t size
= sizeof(struct dsa_switch
) + n
* sizeof(struct dsa_port
);
743 struct dsa_switch
*ds
;
746 ds
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
753 for (i
= 0; i
< ds
->num_ports
; ++i
) {
754 ds
->ports
[i
].index
= i
;
755 ds
->ports
[i
].ds
= ds
;
760 EXPORT_SYMBOL_GPL(dsa_switch_alloc
);
762 int dsa_register_switch(struct dsa_switch
*ds
)
766 mutex_lock(&dsa2_mutex
);
767 err
= dsa_switch_probe(ds
);
768 mutex_unlock(&dsa2_mutex
);
772 EXPORT_SYMBOL_GPL(dsa_register_switch
);
774 static void dsa_switch_remove(struct dsa_switch
*ds
)
776 struct dsa_switch_tree
*dst
= ds
->dst
;
777 unsigned int index
= ds
->index
;
779 dsa_tree_remove_switch(dst
, index
);
782 void dsa_unregister_switch(struct dsa_switch
*ds
)
784 mutex_lock(&dsa2_mutex
);
785 dsa_switch_remove(ds
);
786 mutex_unlock(&dsa2_mutex
);
788 EXPORT_SYMBOL_GPL(dsa_unregister_switch
);