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
)
36 static void dsa_free_dst(struct kref
*ref
)
38 struct dsa_switch_tree
*dst
= container_of(ref
, struct dsa_switch_tree
,
45 static void dsa_put_dst(struct dsa_switch_tree
*dst
)
47 kref_put(&dst
->refcount
, dsa_free_dst
);
50 static struct dsa_switch_tree
*dsa_add_dst(u32 tree
)
52 struct dsa_switch_tree
*dst
;
54 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
59 INIT_LIST_HEAD(&dst
->list
);
60 list_add_tail(&dsa_switch_trees
, &dst
->list
);
61 kref_init(&dst
->refcount
);
66 static void dsa_dst_add_ds(struct dsa_switch_tree
*dst
,
67 struct dsa_switch
*ds
, u32 index
)
69 kref_get(&dst
->refcount
);
73 static void dsa_dst_del_ds(struct dsa_switch_tree
*dst
,
74 struct dsa_switch
*ds
, u32 index
)
76 dst
->ds
[index
] = NULL
;
77 kref_put(&dst
->refcount
, dsa_free_dst
);
80 static bool dsa_port_is_dsa(struct device_node
*port
)
84 name
= of_get_property(port
, "label", NULL
);
88 if (!strcmp(name
, "dsa"))
94 static bool dsa_port_is_cpu(struct device_node
*port
)
98 name
= of_get_property(port
, "label", NULL
);
102 if (!strcmp(name
, "cpu"))
108 static bool dsa_ds_find_port(struct dsa_switch
*ds
,
109 struct device_node
*port
)
113 for (index
= 0; index
< DSA_MAX_PORTS
; index
++)
114 if (ds
->ports
[index
].dn
== port
)
119 static struct dsa_switch
*dsa_dst_find_port(struct dsa_switch_tree
*dst
,
120 struct device_node
*port
)
122 struct dsa_switch
*ds
;
125 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
130 if (dsa_ds_find_port(ds
, port
))
137 static int dsa_port_complete(struct dsa_switch_tree
*dst
,
138 struct dsa_switch
*src_ds
,
139 struct device_node
*port
,
142 struct device_node
*link
;
144 struct dsa_switch
*dst_ds
;
146 for (index
= 0;; index
++) {
147 link
= of_parse_phandle(port
, "link", index
);
151 dst_ds
= dsa_dst_find_port(dst
, link
);
157 src_ds
->rtable
[dst_ds
->index
] = src_port
;
163 /* A switch is complete if all the DSA ports phandles point to ports
164 * known in the tree. A return value of 1 means the tree is not
165 * complete. This is not an error condition. A value of 0 is
168 static int dsa_ds_complete(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
170 struct device_node
*port
;
174 for (index
= 0; index
< DSA_MAX_PORTS
; index
++) {
175 port
= ds
->ports
[index
].dn
;
179 if (!dsa_port_is_dsa(port
))
182 err
= dsa_port_complete(dst
, ds
, port
, index
);
186 ds
->dsa_port_mask
|= BIT(index
);
192 /* A tree is complete if all the DSA ports phandles point to ports
193 * known in the tree. A return value of 1 means the tree is not
194 * complete. This is not an error condition. A value of 0 is
197 static int dsa_dst_complete(struct dsa_switch_tree
*dst
)
199 struct dsa_switch
*ds
;
203 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
208 err
= dsa_ds_complete(dst
, ds
);
216 static int dsa_dsa_port_apply(struct device_node
*port
, u32 index
,
217 struct dsa_switch
*ds
)
221 err
= dsa_cpu_dsa_setup(ds
, ds
->dev
, port
, index
);
223 dev_warn(ds
->dev
, "Failed to setup dsa port %d: %d\n",
231 static void dsa_dsa_port_unapply(struct device_node
*port
, u32 index
,
232 struct dsa_switch
*ds
)
234 dsa_cpu_dsa_destroy(port
);
237 static int dsa_cpu_port_apply(struct device_node
*port
, u32 index
,
238 struct dsa_switch
*ds
)
242 err
= dsa_cpu_dsa_setup(ds
, ds
->dev
, port
, index
);
244 dev_warn(ds
->dev
, "Failed to setup cpu port %d: %d\n",
249 ds
->cpu_port_mask
|= BIT(index
);
254 static void dsa_cpu_port_unapply(struct device_node
*port
, u32 index
,
255 struct dsa_switch
*ds
)
257 dsa_cpu_dsa_destroy(port
);
258 ds
->cpu_port_mask
&= ~BIT(index
);
262 static int dsa_user_port_apply(struct device_node
*port
, u32 index
,
263 struct dsa_switch
*ds
)
268 name
= of_get_property(port
, "label", NULL
);
270 err
= dsa_slave_create(ds
, ds
->dev
, index
, name
);
272 dev_warn(ds
->dev
, "Failed to create slave %d: %d\n",
280 static void dsa_user_port_unapply(struct device_node
*port
, u32 index
,
281 struct dsa_switch
*ds
)
283 if (ds
->ports
[index
].netdev
) {
284 dsa_slave_destroy(ds
->ports
[index
].netdev
);
285 ds
->ports
[index
].netdev
= NULL
;
286 ds
->enabled_port_mask
&= ~(1 << index
);
290 static int dsa_ds_apply(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
292 struct device_node
*port
;
296 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
297 * driver and before drv->setup() has run, since the switch drivers and
298 * the slave MDIO bus driver rely on these values for probing PHY
301 ds
->phys_mii_mask
= ds
->enabled_port_mask
;
303 err
= ds
->drv
->setup(ds
);
307 err
= ds
->drv
->set_addr(ds
, dst
->master_netdev
->dev_addr
);
311 err
= ds
->drv
->set_addr(ds
, dst
->master_netdev
->dev_addr
);
315 if (!ds
->slave_mii_bus
&& ds
->drv
->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
->drv
->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
);
397 /* If we use a tagging format that doesn't have an ethertype
398 * field, make sure that all packets from this point on get
399 * sent to the tag format's receive function.
402 dst
->master_netdev
->dsa_ptr
= (void *)dst
;
408 static void dsa_dst_unapply(struct dsa_switch_tree
*dst
)
410 struct dsa_switch
*ds
;
416 dst
->master_netdev
->dsa_ptr
= NULL
;
418 /* If we used a tagging format that doesn't have an ethertype
419 * field, make sure that all packets from this point get sent
420 * without the tag and go through the regular receive path.
424 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
429 dsa_ds_unapply(dst
, ds
);
432 pr_info("DSA: tree %d unapplied\n", dst
->tree
);
433 dst
->applied
= false;
436 static int dsa_cpu_parse(struct device_node
*port
, u32 index
,
437 struct dsa_switch_tree
*dst
,
438 struct dsa_switch
*ds
)
440 struct net_device
*ethernet_dev
;
441 struct device_node
*ethernet
;
443 ethernet
= of_parse_phandle(port
, "ethernet", 0);
447 ethernet_dev
= of_find_net_device_by_node(ethernet
);
449 return -EPROBE_DEFER
;
451 if (!ds
->master_netdev
)
452 ds
->master_netdev
= ethernet_dev
;
454 if (!dst
->master_netdev
)
455 dst
->master_netdev
= ethernet_dev
;
457 if (dst
->cpu_switch
== -1) {
458 dst
->cpu_switch
= ds
->index
;
459 dst
->cpu_port
= index
;
462 dst
->tag_ops
= dsa_resolve_tag_protocol(ds
->drv
->tag_protocol
);
463 if (IS_ERR(dst
->tag_ops
)) {
464 dev_warn(ds
->dev
, "No tagger for this switch\n");
465 return PTR_ERR(dst
->tag_ops
);
468 dst
->rcv
= dst
->tag_ops
->rcv
;
473 static int dsa_ds_parse(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
475 struct device_node
*port
;
479 for (index
= 0; index
< DSA_MAX_PORTS
; index
++) {
480 port
= ds
->ports
[index
].dn
;
484 if (dsa_port_is_cpu(port
)) {
485 err
= dsa_cpu_parse(port
, index
, dst
, ds
);
491 pr_info("DSA: switch %d %d parsed\n", dst
->tree
, ds
->index
);
496 static int dsa_dst_parse(struct dsa_switch_tree
*dst
)
498 struct dsa_switch
*ds
;
502 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
507 err
= dsa_ds_parse(dst
, ds
);
512 if (!dst
->master_netdev
) {
513 pr_warn("Tree has no master device\n");
517 pr_info("DSA: tree %d parsed\n", dst
->tree
);
522 static int dsa_parse_ports_dn(struct device_node
*ports
, struct dsa_switch
*ds
)
524 struct device_node
*port
;
528 for_each_available_child_of_node(ports
, port
) {
529 err
= of_property_read_u32(port
, "reg", ®
);
533 if (reg
>= DSA_MAX_PORTS
)
536 ds
->ports
[reg
].dn
= port
;
538 /* Initialize enabled_port_mask now for drv->setup()
539 * to have access to a correct value, just like what
540 * net/dsa/dsa.c::dsa_switch_setup_one does.
542 if (!dsa_port_is_cpu(port
))
543 ds
->enabled_port_mask
|= 1 << reg
;
549 static int dsa_parse_member(struct device_node
*np
, u32
*tree
, u32
*index
)
555 err
= of_property_read_u32_index(np
, "dsa,member", 0, tree
);
557 /* Does not exist, but it is optional */
563 err
= of_property_read_u32_index(np
, "dsa,member", 1, index
);
567 if (*index
>= DSA_MAX_SWITCHES
)
573 static struct device_node
*dsa_get_ports(struct dsa_switch
*ds
,
574 struct device_node
*np
)
576 struct device_node
*ports
;
578 ports
= of_get_child_by_name(np
, "ports");
580 dev_err(ds
->dev
, "no ports child node found\n");
581 return ERR_PTR(-EINVAL
);
587 static int _dsa_register_switch(struct dsa_switch
*ds
, struct device_node
*np
)
589 struct device_node
*ports
= dsa_get_ports(ds
, np
);
590 struct dsa_switch_tree
*dst
;
594 err
= dsa_parse_member(np
, &tree
, &index
);
599 return PTR_ERR(ports
);
601 err
= dsa_parse_ports_dn(ports
, ds
);
605 dst
= dsa_get_dst(tree
);
607 dst
= dsa_add_dst(tree
);
612 if (dst
->ds
[index
]) {
619 dsa_dst_add_ds(dst
, ds
, index
);
621 err
= dsa_dst_complete(dst
);
626 /* Not all switches registered yet */
632 pr_info("DSA: Disjoint trees?\n");
636 err
= dsa_dst_parse(dst
);
640 err
= dsa_dst_apply(dst
);
642 dsa_dst_unapply(dst
);
650 dsa_dst_del_ds(dst
, ds
, ds
->index
);
657 int dsa_register_switch(struct dsa_switch
*ds
, struct device_node
*np
)
661 mutex_lock(&dsa2_mutex
);
662 err
= _dsa_register_switch(ds
, np
);
663 mutex_unlock(&dsa2_mutex
);
667 EXPORT_SYMBOL_GPL(dsa_register_switch
);
669 void _dsa_unregister_switch(struct dsa_switch
*ds
)
671 struct dsa_switch_tree
*dst
= ds
->dst
;
673 dsa_dst_unapply(dst
);
675 dsa_dst_del_ds(dst
, ds
, ds
->index
);
678 void dsa_unregister_switch(struct dsa_switch
*ds
)
680 mutex_lock(&dsa2_mutex
);
681 _dsa_unregister_switch(ds
);
682 mutex_unlock(&dsa2_mutex
);
684 EXPORT_SYMBOL_GPL(dsa_unregister_switch
);
This page took 0.10294 seconds and 5 git commands to generate.