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/slab.h>
17 #include <linux/rtnetlink.h>
20 #include <linux/of_net.h>
23 static LIST_HEAD(dsa_switch_trees
);
24 static DEFINE_MUTEX(dsa2_mutex
);
26 static struct dsa_switch_tree
*dsa_get_dst(u32 tree
)
28 struct dsa_switch_tree
*dst
;
30 list_for_each_entry(dst
, &dsa_switch_trees
, list
)
31 if (dst
->tree
== tree
) {
32 kref_get(&dst
->refcount
);
38 static void dsa_free_dst(struct kref
*ref
)
40 struct dsa_switch_tree
*dst
= container_of(ref
, struct dsa_switch_tree
,
47 static void dsa_put_dst(struct dsa_switch_tree
*dst
)
49 kref_put(&dst
->refcount
, dsa_free_dst
);
52 static struct dsa_switch_tree
*dsa_add_dst(u32 tree
)
54 struct dsa_switch_tree
*dst
;
56 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
61 INIT_LIST_HEAD(&dst
->list
);
62 list_add_tail(&dsa_switch_trees
, &dst
->list
);
63 kref_init(&dst
->refcount
);
68 static void dsa_dst_add_ds(struct dsa_switch_tree
*dst
,
69 struct dsa_switch
*ds
, u32 index
)
71 kref_get(&dst
->refcount
);
75 static void dsa_dst_del_ds(struct dsa_switch_tree
*dst
,
76 struct dsa_switch
*ds
, u32 index
)
78 dst
->ds
[index
] = NULL
;
79 kref_put(&dst
->refcount
, dsa_free_dst
);
82 static bool dsa_port_is_dsa(struct device_node
*port
)
86 name
= of_get_property(port
, "label", NULL
);
90 if (!strcmp(name
, "dsa"))
96 static bool dsa_port_is_cpu(struct device_node
*port
)
100 name
= of_get_property(port
, "label", NULL
);
104 if (!strcmp(name
, "cpu"))
110 static bool dsa_ds_find_port(struct dsa_switch
*ds
,
111 struct device_node
*port
)
115 for (index
= 0; index
< DSA_MAX_PORTS
; index
++)
116 if (ds
->ports
[index
].dn
== port
)
121 static struct dsa_switch
*dsa_dst_find_port(struct dsa_switch_tree
*dst
,
122 struct device_node
*port
)
124 struct dsa_switch
*ds
;
127 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
132 if (dsa_ds_find_port(ds
, port
))
139 static int dsa_port_complete(struct dsa_switch_tree
*dst
,
140 struct dsa_switch
*src_ds
,
141 struct device_node
*port
,
144 struct device_node
*link
;
146 struct dsa_switch
*dst_ds
;
148 for (index
= 0;; index
++) {
149 link
= of_parse_phandle(port
, "link", index
);
153 dst_ds
= dsa_dst_find_port(dst
, link
);
159 src_ds
->rtable
[dst_ds
->index
] = src_port
;
165 /* A switch is complete if all the DSA ports phandles point to ports
166 * known in the tree. A return value of 1 means the tree is not
167 * complete. This is not an error condition. A value of 0 is
170 static int dsa_ds_complete(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
172 struct device_node
*port
;
176 for (index
= 0; index
< DSA_MAX_PORTS
; index
++) {
177 port
= ds
->ports
[index
].dn
;
181 if (!dsa_port_is_dsa(port
))
184 err
= dsa_port_complete(dst
, ds
, port
, index
);
188 ds
->dsa_port_mask
|= BIT(index
);
194 /* A tree is complete if all the DSA ports phandles point to ports
195 * known in the tree. A return value of 1 means the tree is not
196 * complete. This is not an error condition. A value of 0 is
199 static int dsa_dst_complete(struct dsa_switch_tree
*dst
)
201 struct dsa_switch
*ds
;
205 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
210 err
= dsa_ds_complete(dst
, ds
);
218 static int dsa_dsa_port_apply(struct device_node
*port
, u32 index
,
219 struct dsa_switch
*ds
)
223 err
= dsa_cpu_dsa_setup(ds
, ds
->dev
, port
, index
);
225 dev_warn(ds
->dev
, "Failed to setup dsa port %d: %d\n",
233 static void dsa_dsa_port_unapply(struct device_node
*port
, u32 index
,
234 struct dsa_switch
*ds
)
236 dsa_cpu_dsa_destroy(port
);
239 static int dsa_cpu_port_apply(struct device_node
*port
, u32 index
,
240 struct dsa_switch
*ds
)
244 err
= dsa_cpu_dsa_setup(ds
, ds
->dev
, port
, index
);
246 dev_warn(ds
->dev
, "Failed to setup cpu port %d: %d\n",
251 ds
->cpu_port_mask
|= BIT(index
);
256 static void dsa_cpu_port_unapply(struct device_node
*port
, u32 index
,
257 struct dsa_switch
*ds
)
259 dsa_cpu_dsa_destroy(port
);
260 ds
->cpu_port_mask
&= ~BIT(index
);
264 static int dsa_user_port_apply(struct device_node
*port
, u32 index
,
265 struct dsa_switch
*ds
)
270 name
= of_get_property(port
, "label", NULL
);
272 err
= dsa_slave_create(ds
, ds
->dev
, index
, name
);
274 dev_warn(ds
->dev
, "Failed to create slave %d: %d\n",
282 static void dsa_user_port_unapply(struct device_node
*port
, u32 index
,
283 struct dsa_switch
*ds
)
285 if (ds
->ports
[index
].netdev
) {
286 dsa_slave_destroy(ds
->ports
[index
].netdev
);
287 ds
->ports
[index
].netdev
= NULL
;
288 ds
->enabled_port_mask
&= ~(1 << index
);
292 static int dsa_ds_apply(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
294 struct device_node
*port
;
298 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
299 * driver and before ops->setup() has run, since the switch drivers and
300 * the slave MDIO bus driver rely on these values for probing PHY
303 ds
->phys_mii_mask
= ds
->enabled_port_mask
;
305 err
= ds
->ops
->setup(ds
);
309 if (ds
->ops
->set_addr
) {
310 err
= ds
->ops
->set_addr(ds
, dst
->master_netdev
->dev_addr
);
315 if (!ds
->slave_mii_bus
&& ds
->ops
->phy_read
) {
316 ds
->slave_mii_bus
= devm_mdiobus_alloc(ds
->dev
);
317 if (!ds
->slave_mii_bus
)
320 dsa_slave_mii_bus_init(ds
);
322 err
= mdiobus_register(ds
->slave_mii_bus
);
327 for (index
= 0; index
< DSA_MAX_PORTS
; index
++) {
328 port
= ds
->ports
[index
].dn
;
332 if (dsa_port_is_dsa(port
)) {
333 err
= dsa_dsa_port_apply(port
, index
, ds
);
339 if (dsa_port_is_cpu(port
)) {
340 err
= dsa_cpu_port_apply(port
, index
, ds
);
346 err
= dsa_user_port_apply(port
, index
, ds
);
354 static void dsa_ds_unapply(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
356 struct device_node
*port
;
359 for (index
= 0; index
< DSA_MAX_PORTS
; index
++) {
360 port
= ds
->ports
[index
].dn
;
364 if (dsa_port_is_dsa(port
)) {
365 dsa_dsa_port_unapply(port
, index
, ds
);
369 if (dsa_port_is_cpu(port
)) {
370 dsa_cpu_port_unapply(port
, index
, ds
);
374 dsa_user_port_unapply(port
, index
, ds
);
377 if (ds
->slave_mii_bus
&& ds
->ops
->phy_read
)
378 mdiobus_unregister(ds
->slave_mii_bus
);
381 static int dsa_dst_apply(struct dsa_switch_tree
*dst
)
383 struct dsa_switch
*ds
;
387 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
392 err
= dsa_ds_apply(dst
, ds
);
398 err
= dsa_cpu_port_ethtool_setup(dst
->ds
[0]);
403 /* If we use a tagging format that doesn't have an ethertype
404 * field, make sure that all packets from this point on get
405 * sent to the tag format's receive function.
408 dst
->master_netdev
->dsa_ptr
= (void *)dst
;
414 static void dsa_dst_unapply(struct dsa_switch_tree
*dst
)
416 struct dsa_switch
*ds
;
422 dst
->master_netdev
->dsa_ptr
= NULL
;
424 /* If we used a tagging format that doesn't have an ethertype
425 * field, make sure that all packets from this point get sent
426 * without the tag and go through the regular receive path.
430 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
435 dsa_ds_unapply(dst
, ds
);
439 dsa_cpu_port_ethtool_restore(dst
->ds
[0]);
441 pr_info("DSA: tree %d unapplied\n", dst
->tree
);
442 dst
->applied
= false;
445 static int dsa_cpu_parse(struct device_node
*port
, u32 index
,
446 struct dsa_switch_tree
*dst
,
447 struct dsa_switch
*ds
)
449 enum dsa_tag_protocol tag_protocol
;
450 struct net_device
*ethernet_dev
;
451 struct device_node
*ethernet
;
453 ethernet
= of_parse_phandle(port
, "ethernet", 0);
457 ethernet_dev
= of_find_net_device_by_node(ethernet
);
459 return -EPROBE_DEFER
;
461 if (!ds
->master_netdev
)
462 ds
->master_netdev
= ethernet_dev
;
464 if (!dst
->master_netdev
)
465 dst
->master_netdev
= ethernet_dev
;
467 if (dst
->cpu_switch
== -1) {
468 dst
->cpu_switch
= ds
->index
;
469 dst
->cpu_port
= index
;
472 tag_protocol
= ds
->ops
->get_tag_protocol(ds
);
473 dst
->tag_ops
= dsa_resolve_tag_protocol(tag_protocol
);
474 if (IS_ERR(dst
->tag_ops
)) {
475 dev_warn(ds
->dev
, "No tagger for this switch\n");
476 return PTR_ERR(dst
->tag_ops
);
479 dst
->rcv
= dst
->tag_ops
->rcv
;
484 static int dsa_ds_parse(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
486 struct device_node
*port
;
490 for (index
= 0; index
< DSA_MAX_PORTS
; index
++) {
491 port
= ds
->ports
[index
].dn
;
495 if (dsa_port_is_cpu(port
)) {
496 err
= dsa_cpu_parse(port
, index
, dst
, ds
);
502 pr_info("DSA: switch %d %d parsed\n", dst
->tree
, ds
->index
);
507 static int dsa_dst_parse(struct dsa_switch_tree
*dst
)
509 struct dsa_switch
*ds
;
513 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
518 err
= dsa_ds_parse(dst
, ds
);
523 if (!dst
->master_netdev
) {
524 pr_warn("Tree has no master device\n");
528 pr_info("DSA: tree %d parsed\n", dst
->tree
);
533 static int dsa_parse_ports_dn(struct device_node
*ports
, struct dsa_switch
*ds
)
535 struct device_node
*port
;
539 for_each_available_child_of_node(ports
, port
) {
540 err
= of_property_read_u32(port
, "reg", ®
);
544 if (reg
>= DSA_MAX_PORTS
)
547 ds
->ports
[reg
].dn
= port
;
549 /* Initialize enabled_port_mask now for ops->setup()
550 * to have access to a correct value, just like what
551 * net/dsa/dsa.c::dsa_switch_setup_one does.
553 if (!dsa_port_is_cpu(port
))
554 ds
->enabled_port_mask
|= 1 << reg
;
560 static int dsa_parse_member(struct device_node
*np
, u32
*tree
, u32
*index
)
566 err
= of_property_read_u32_index(np
, "dsa,member", 0, tree
);
568 /* Does not exist, but it is optional */
574 err
= of_property_read_u32_index(np
, "dsa,member", 1, index
);
578 if (*index
>= DSA_MAX_SWITCHES
)
584 static struct device_node
*dsa_get_ports(struct dsa_switch
*ds
,
585 struct device_node
*np
)
587 struct device_node
*ports
;
589 ports
= of_get_child_by_name(np
, "ports");
591 dev_err(ds
->dev
, "no ports child node found\n");
592 return ERR_PTR(-EINVAL
);
598 static int _dsa_register_switch(struct dsa_switch
*ds
, struct device_node
*np
)
600 struct device_node
*ports
= dsa_get_ports(ds
, np
);
601 struct dsa_switch_tree
*dst
;
605 err
= dsa_parse_member(np
, &tree
, &index
);
610 return PTR_ERR(ports
);
612 err
= dsa_parse_ports_dn(ports
, ds
);
616 dst
= dsa_get_dst(tree
);
618 dst
= dsa_add_dst(tree
);
623 if (dst
->ds
[index
]) {
631 /* Initialize the routing table */
632 for (i
= 0; i
< DSA_MAX_SWITCHES
; ++i
)
633 ds
->rtable
[i
] = DSA_RTABLE_NONE
;
635 dsa_dst_add_ds(dst
, ds
, index
);
637 err
= dsa_dst_complete(dst
);
642 /* Not all switches registered yet */
648 pr_info("DSA: Disjoint trees?\n");
652 err
= dsa_dst_parse(dst
);
656 err
= dsa_dst_apply(dst
);
658 dsa_dst_unapply(dst
);
666 dsa_dst_del_ds(dst
, ds
, ds
->index
);
673 int dsa_register_switch(struct dsa_switch
*ds
, struct device_node
*np
)
677 mutex_lock(&dsa2_mutex
);
678 err
= _dsa_register_switch(ds
, np
);
679 mutex_unlock(&dsa2_mutex
);
683 EXPORT_SYMBOL_GPL(dsa_register_switch
);
685 static void _dsa_unregister_switch(struct dsa_switch
*ds
)
687 struct dsa_switch_tree
*dst
= ds
->dst
;
689 dsa_dst_unapply(dst
);
691 dsa_dst_del_ds(dst
, ds
, ds
->index
);
694 void dsa_unregister_switch(struct dsa_switch
*ds
)
696 mutex_lock(&dsa2_mutex
);
697 _dsa_unregister_switch(ds
);
698 mutex_unlock(&dsa2_mutex
);
700 EXPORT_SYMBOL_GPL(dsa_unregister_switch
);