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_switch_trees
);
25 static DEFINE_MUTEX(dsa2_mutex
);
27 static const struct devlink_ops dsa_devlink_ops
= {
30 static struct dsa_switch_tree
*dsa_get_dst(u32 tree
)
32 struct dsa_switch_tree
*dst
;
34 list_for_each_entry(dst
, &dsa_switch_trees
, list
)
35 if (dst
->tree
== tree
) {
36 kref_get(&dst
->refcount
);
42 static void dsa_free_dst(struct kref
*ref
)
44 struct dsa_switch_tree
*dst
= container_of(ref
, struct dsa_switch_tree
,
51 static void dsa_put_dst(struct dsa_switch_tree
*dst
)
53 kref_put(&dst
->refcount
, dsa_free_dst
);
56 static struct dsa_switch_tree
*dsa_add_dst(u32 tree
)
58 struct dsa_switch_tree
*dst
;
60 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
64 INIT_LIST_HEAD(&dst
->list
);
65 list_add_tail(&dsa_switch_trees
, &dst
->list
);
66 kref_init(&dst
->refcount
);
71 static void dsa_dst_add_ds(struct dsa_switch_tree
*dst
,
72 struct dsa_switch
*ds
, u32 index
)
74 kref_get(&dst
->refcount
);
78 static void dsa_dst_del_ds(struct dsa_switch_tree
*dst
,
79 struct dsa_switch
*ds
, u32 index
)
81 dst
->ds
[index
] = NULL
;
82 kref_put(&dst
->refcount
, dsa_free_dst
);
85 /* For platform data configurations, we need to have a valid name argument to
86 * differentiate a disabled port from an enabled one
88 static bool dsa_port_is_valid(struct dsa_port
*port
)
90 return !!(port
->dn
|| port
->name
);
93 static bool dsa_port_is_dsa(struct dsa_port
*port
)
95 if (port
->name
&& !strcmp(port
->name
, "dsa"))
98 return !!of_parse_phandle(port
->dn
, "link", 0);
101 static bool dsa_port_is_cpu(struct dsa_port
*port
)
103 if (port
->name
&& !strcmp(port
->name
, "cpu"))
106 return !!of_parse_phandle(port
->dn
, "ethernet", 0);
109 static bool dsa_ds_find_port_dn(struct dsa_switch
*ds
,
110 struct device_node
*port
)
114 for (index
= 0; index
< ds
->num_ports
; index
++)
115 if (ds
->ports
[index
].dn
== port
)
120 static struct dsa_switch
*dsa_dst_find_port_dn(struct dsa_switch_tree
*dst
,
121 struct device_node
*port
)
123 struct dsa_switch
*ds
;
126 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
131 if (dsa_ds_find_port_dn(ds
, port
))
138 static int dsa_port_complete(struct dsa_switch_tree
*dst
,
139 struct dsa_switch
*src_ds
,
140 struct dsa_port
*port
,
143 struct device_node
*link
;
145 struct dsa_switch
*dst_ds
;
147 for (index
= 0;; index
++) {
148 link
= of_parse_phandle(port
->dn
, "link", index
);
152 dst_ds
= dsa_dst_find_port_dn(dst
, link
);
158 src_ds
->rtable
[dst_ds
->index
] = src_port
;
164 /* A switch is complete if all the DSA ports phandles point to ports
165 * known in the tree. A return value of 1 means the tree is not
166 * complete. This is not an error condition. A value of 0 is
169 static int dsa_ds_complete(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
171 struct dsa_port
*port
;
175 for (index
= 0; index
< ds
->num_ports
; index
++) {
176 port
= &ds
->ports
[index
];
177 if (!dsa_port_is_valid(port
))
180 if (!dsa_port_is_dsa(port
))
183 err
= dsa_port_complete(dst
, ds
, port
, index
);
187 ds
->dsa_port_mask
|= BIT(index
);
193 /* A tree is complete if all the DSA ports phandles point to ports
194 * known in the tree. A return value of 1 means the tree is not
195 * complete. This is not an error condition. A value of 0 is
198 static int dsa_dst_complete(struct dsa_switch_tree
*dst
)
200 struct dsa_switch
*ds
;
204 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
209 err
= dsa_ds_complete(dst
, ds
);
217 static int dsa_dsa_port_apply(struct dsa_port
*port
)
219 struct dsa_switch
*ds
= port
->ds
;
222 err
= dsa_cpu_dsa_setup(port
);
224 dev_warn(ds
->dev
, "Failed to setup dsa port %d: %d\n",
229 memset(&port
->devlink_port
, 0, sizeof(port
->devlink_port
));
231 return devlink_port_register(ds
->devlink
, &port
->devlink_port
,
235 static void dsa_dsa_port_unapply(struct dsa_port
*port
)
237 devlink_port_unregister(&port
->devlink_port
);
238 dsa_cpu_dsa_destroy(port
);
241 static int dsa_cpu_port_apply(struct dsa_port
*port
)
243 struct dsa_switch
*ds
= port
->ds
;
246 err
= dsa_cpu_dsa_setup(port
);
248 dev_warn(ds
->dev
, "Failed to setup cpu port %d: %d\n",
253 memset(&port
->devlink_port
, 0, sizeof(port
->devlink_port
));
254 err
= devlink_port_register(ds
->devlink
, &port
->devlink_port
,
259 static void dsa_cpu_port_unapply(struct dsa_port
*port
)
261 devlink_port_unregister(&port
->devlink_port
);
262 dsa_cpu_dsa_destroy(port
);
263 port
->ds
->cpu_port_mask
&= ~BIT(port
->index
);
267 static int dsa_user_port_apply(struct dsa_port
*port
)
269 struct dsa_switch
*ds
= port
->ds
;
270 const char *name
= port
->name
;
274 name
= of_get_property(port
->dn
, "label", NULL
);
278 err
= dsa_slave_create(port
, name
);
280 dev_warn(ds
->dev
, "Failed to create slave %d: %d\n",
286 memset(&port
->devlink_port
, 0, sizeof(port
->devlink_port
));
287 err
= devlink_port_register(ds
->devlink
, &port
->devlink_port
,
292 devlink_port_type_eth_set(&port
->devlink_port
, port
->netdev
);
297 static void dsa_user_port_unapply(struct dsa_port
*port
)
299 devlink_port_unregister(&port
->devlink_port
);
301 dsa_slave_destroy(port
->netdev
);
303 port
->ds
->enabled_port_mask
&= ~(1 << port
->index
);
307 static int dsa_ds_apply(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
309 struct dsa_port
*port
;
313 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
314 * driver and before ops->setup() has run, since the switch drivers and
315 * the slave MDIO bus driver rely on these values for probing PHY
318 ds
->phys_mii_mask
= ds
->enabled_port_mask
;
320 /* Add the switch to devlink before calling setup, so that setup can
323 ds
->devlink
= devlink_alloc(&dsa_devlink_ops
, 0);
327 err
= devlink_register(ds
->devlink
, ds
->dev
);
331 err
= ds
->ops
->setup(ds
);
335 err
= dsa_switch_register_notifier(ds
);
339 if (ds
->ops
->set_addr
) {
340 err
= ds
->ops
->set_addr(ds
, dst
->cpu_dp
->netdev
->dev_addr
);
345 if (!ds
->slave_mii_bus
&& ds
->ops
->phy_read
) {
346 ds
->slave_mii_bus
= devm_mdiobus_alloc(ds
->dev
);
347 if (!ds
->slave_mii_bus
)
350 dsa_slave_mii_bus_init(ds
);
352 err
= mdiobus_register(ds
->slave_mii_bus
);
357 for (index
= 0; index
< ds
->num_ports
; index
++) {
358 port
= &ds
->ports
[index
];
359 if (!dsa_port_is_valid(port
))
362 if (dsa_port_is_dsa(port
)) {
363 err
= dsa_dsa_port_apply(port
);
369 if (dsa_port_is_cpu(port
)) {
370 err
= dsa_cpu_port_apply(port
);
376 err
= dsa_user_port_apply(port
);
384 static void dsa_ds_unapply(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
386 struct dsa_port
*port
;
389 for (index
= 0; index
< ds
->num_ports
; index
++) {
390 port
= &ds
->ports
[index
];
391 if (!dsa_port_is_valid(port
))
394 if (dsa_port_is_dsa(port
)) {
395 dsa_dsa_port_unapply(port
);
399 if (dsa_port_is_cpu(port
)) {
400 dsa_cpu_port_unapply(port
);
404 dsa_user_port_unapply(port
);
407 if (ds
->slave_mii_bus
&& ds
->ops
->phy_read
)
408 mdiobus_unregister(ds
->slave_mii_bus
);
410 dsa_switch_unregister_notifier(ds
);
413 devlink_unregister(ds
->devlink
);
414 devlink_free(ds
->devlink
);
420 static int dsa_dst_apply(struct dsa_switch_tree
*dst
)
422 struct dsa_switch
*ds
;
426 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
431 err
= dsa_ds_apply(dst
, ds
);
437 err
= dsa_cpu_port_ethtool_setup(dst
->cpu_dp
);
442 /* If we use a tagging format that doesn't have an ethertype
443 * field, make sure that all packets from this point on get
444 * sent to the tag format's receive function.
447 dst
->cpu_dp
->netdev
->dsa_ptr
= dst
;
453 static void dsa_dst_unapply(struct dsa_switch_tree
*dst
)
455 struct dsa_switch
*ds
;
461 dst
->cpu_dp
->netdev
->dsa_ptr
= NULL
;
463 /* If we used a tagging format that doesn't have an ethertype
464 * field, make sure that all packets from this point get sent
465 * without the tag and go through the regular receive path.
469 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
474 dsa_ds_unapply(dst
, ds
);
478 dsa_cpu_port_ethtool_restore(dst
->cpu_dp
);
482 pr_info("DSA: tree %d unapplied\n", dst
->tree
);
483 dst
->applied
= false;
486 static int dsa_cpu_parse(struct dsa_port
*port
, u32 index
,
487 struct dsa_switch_tree
*dst
,
488 struct dsa_switch
*ds
)
490 enum dsa_tag_protocol tag_protocol
;
491 struct net_device
*ethernet_dev
;
492 struct device_node
*ethernet
;
495 ethernet
= of_parse_phandle(port
->dn
, "ethernet", 0);
498 ethernet_dev
= of_find_net_device_by_node(ethernet
);
500 return -EPROBE_DEFER
;
502 ethernet_dev
= dsa_dev_to_net_device(ds
->cd
->netdev
[index
]);
504 return -EPROBE_DEFER
;
505 dev_put(ethernet_dev
);
510 dst
->cpu_dp
->netdev
= ethernet_dev
;
513 /* Initialize cpu_port_mask now for drv->setup()
514 * to have access to a correct value, just like what
515 * net/dsa/dsa.c::dsa_switch_setup_one does.
517 ds
->cpu_port_mask
|= BIT(index
);
519 tag_protocol
= ds
->ops
->get_tag_protocol(ds
);
520 dst
->tag_ops
= dsa_resolve_tag_protocol(tag_protocol
);
521 if (IS_ERR(dst
->tag_ops
)) {
522 dev_warn(ds
->dev
, "No tagger for this switch\n");
523 ds
->cpu_port_mask
&= ~BIT(index
);
524 return PTR_ERR(dst
->tag_ops
);
527 dst
->rcv
= dst
->tag_ops
->rcv
;
532 static int dsa_ds_parse(struct dsa_switch_tree
*dst
, struct dsa_switch
*ds
)
534 struct dsa_port
*port
;
538 for (index
= 0; index
< ds
->num_ports
; index
++) {
539 port
= &ds
->ports
[index
];
540 if (!dsa_port_is_valid(port
) ||
541 dsa_port_is_dsa(port
))
544 if (dsa_port_is_cpu(port
)) {
545 err
= dsa_cpu_parse(port
, index
, dst
, ds
);
549 /* Initialize enabled_port_mask now for drv->setup()
550 * to have access to a correct value, just like what
551 * net/dsa/dsa.c::dsa_switch_setup_one does.
553 ds
->enabled_port_mask
|= BIT(index
);
558 pr_info("DSA: switch %d %d parsed\n", dst
->tree
, ds
->index
);
563 static int dsa_dst_parse(struct dsa_switch_tree
*dst
)
565 struct dsa_switch
*ds
;
571 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
576 err
= dsa_ds_parse(dst
, ds
);
582 pr_warn("Tree has no master device\n");
586 /* Assign the default CPU port to all ports of the fabric */
587 for (index
= 0; index
< DSA_MAX_SWITCHES
; index
++) {
592 for (port
= 0; port
< ds
->num_ports
; port
++) {
593 dp
= &ds
->ports
[port
];
594 if (!dsa_port_is_valid(dp
) ||
595 dsa_port_is_dsa(dp
) ||
599 dp
->cpu_dp
= dst
->cpu_dp
;
603 pr_info("DSA: tree %d parsed\n", dst
->tree
);
608 static int dsa_parse_ports_dn(struct device_node
*ports
, struct dsa_switch
*ds
)
610 struct device_node
*port
;
614 for_each_available_child_of_node(ports
, port
) {
615 err
= of_property_read_u32(port
, "reg", ®
);
619 if (reg
>= ds
->num_ports
)
622 ds
->ports
[reg
].dn
= port
;
628 static int dsa_parse_ports(struct dsa_chip_data
*cd
, struct dsa_switch
*ds
)
630 bool valid_name_found
= false;
633 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
634 if (!cd
->port_names
[i
])
637 ds
->ports
[i
].name
= cd
->port_names
[i
];
638 valid_name_found
= true;
641 if (!valid_name_found
&& i
== DSA_MAX_PORTS
)
647 static int dsa_parse_member_dn(struct device_node
*np
, u32
*tree
, u32
*index
)
653 err
= of_property_read_u32_index(np
, "dsa,member", 0, tree
);
655 /* Does not exist, but it is optional */
661 err
= of_property_read_u32_index(np
, "dsa,member", 1, index
);
665 if (*index
>= DSA_MAX_SWITCHES
)
671 static int dsa_parse_member(struct dsa_chip_data
*pd
, u32
*tree
, u32
*index
)
676 /* We do not support complex trees with dsa_chip_data */
683 static struct device_node
*dsa_get_ports(struct dsa_switch
*ds
,
684 struct device_node
*np
)
686 struct device_node
*ports
;
688 ports
= of_get_child_by_name(np
, "ports");
690 dev_err(ds
->dev
, "no ports child node found\n");
691 return ERR_PTR(-EINVAL
);
697 static int _dsa_register_switch(struct dsa_switch
*ds
)
699 struct dsa_chip_data
*pdata
= ds
->dev
->platform_data
;
700 struct device_node
*np
= ds
->dev
->of_node
;
701 struct dsa_switch_tree
*dst
;
702 struct device_node
*ports
;
707 err
= dsa_parse_member_dn(np
, &tree
, &index
);
711 ports
= dsa_get_ports(ds
, np
);
713 return PTR_ERR(ports
);
715 err
= dsa_parse_ports_dn(ports
, ds
);
719 err
= dsa_parse_member(pdata
, &tree
, &index
);
723 err
= dsa_parse_ports(pdata
, ds
);
728 dst
= dsa_get_dst(tree
);
730 dst
= dsa_add_dst(tree
);
735 if (dst
->ds
[index
]) {
744 /* Initialize the routing table */
745 for (i
= 0; i
< DSA_MAX_SWITCHES
; ++i
)
746 ds
->rtable
[i
] = DSA_RTABLE_NONE
;
748 dsa_dst_add_ds(dst
, ds
, index
);
750 err
= dsa_dst_complete(dst
);
755 /* Not all switches registered yet */
761 pr_info("DSA: Disjoint trees?\n");
765 err
= dsa_dst_parse(dst
);
767 if (err
== -EPROBE_DEFER
) {
768 dsa_dst_del_ds(dst
, ds
, ds
->index
);
775 err
= dsa_dst_apply(dst
);
777 dsa_dst_unapply(dst
);
785 dsa_dst_del_ds(dst
, ds
, ds
->index
);
792 struct dsa_switch
*dsa_switch_alloc(struct device
*dev
, size_t n
)
794 size_t size
= sizeof(struct dsa_switch
) + n
* sizeof(struct dsa_port
);
795 struct dsa_switch
*ds
;
798 ds
= devm_kzalloc(dev
, size
, GFP_KERNEL
);
805 for (i
= 0; i
< ds
->num_ports
; ++i
) {
806 ds
->ports
[i
].index
= i
;
807 ds
->ports
[i
].ds
= ds
;
812 EXPORT_SYMBOL_GPL(dsa_switch_alloc
);
814 int dsa_register_switch(struct dsa_switch
*ds
)
818 mutex_lock(&dsa2_mutex
);
819 err
= _dsa_register_switch(ds
);
820 mutex_unlock(&dsa2_mutex
);
824 EXPORT_SYMBOL_GPL(dsa_register_switch
);
826 static void _dsa_unregister_switch(struct dsa_switch
*ds
)
828 struct dsa_switch_tree
*dst
= ds
->dst
;
830 dsa_dst_unapply(dst
);
832 dsa_dst_del_ds(dst
, ds
, ds
->index
);
835 void dsa_unregister_switch(struct dsa_switch
*ds
)
837 mutex_lock(&dsa2_mutex
);
838 _dsa_unregister_switch(ds
);
839 mutex_unlock(&dsa2_mutex
);
841 EXPORT_SYMBOL_GPL(dsa_unregister_switch
);