2 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
3 * Copyright (c) 2008-2009 Marvell Semiconductor
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, u32 index,
218 struct dsa_switch *ds)
222 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
224 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
229 memset(&ds->ports[index].devlink_port, 0,
230 sizeof(ds->ports[index].devlink_port));
232 return devlink_port_register(ds->devlink,
233 &ds->ports[index].devlink_port,
237 static void dsa_dsa_port_unapply(struct dsa_port *port, u32 index,
238 struct dsa_switch *ds)
240 devlink_port_unregister(&ds->ports[index].devlink_port);
241 dsa_cpu_dsa_destroy(port);
244 static int dsa_cpu_port_apply(struct dsa_port *port, u32 index,
245 struct dsa_switch *ds)
249 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
251 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
256 ds->cpu_port_mask |= BIT(index);
258 memset(&ds->ports[index].devlink_port, 0,
259 sizeof(ds->ports[index].devlink_port));
260 err = devlink_port_register(ds->devlink, &ds->ports[index].devlink_port,
265 static void dsa_cpu_port_unapply(struct dsa_port *port, u32 index,
266 struct dsa_switch *ds)
268 devlink_port_unregister(&ds->ports[index].devlink_port);
269 dsa_cpu_dsa_destroy(port);
270 ds->cpu_port_mask &= ~BIT(index);
274 static int dsa_user_port_apply(struct dsa_port *port, u32 index,
275 struct dsa_switch *ds)
277 const char *name = port->name;
281 name = of_get_property(port->dn, "label", NULL);
285 err = dsa_slave_create(ds, ds->dev, index, name);
287 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
289 ds->ports[index].netdev = NULL;
293 memset(&ds->ports[index].devlink_port, 0,
294 sizeof(ds->ports[index].devlink_port));
295 err = devlink_port_register(ds->devlink, &ds->ports[index].devlink_port,
300 devlink_port_type_eth_set(&ds->ports[index].devlink_port,
301 ds->ports[index].netdev);
306 static void dsa_user_port_unapply(struct dsa_port *port, u32 index,
307 struct dsa_switch *ds)
309 devlink_port_unregister(&ds->ports[index].devlink_port);
310 if (ds->ports[index].netdev) {
311 dsa_slave_destroy(ds->ports[index].netdev);
312 ds->ports[index].netdev = NULL;
313 ds->enabled_port_mask &= ~(1 << index);
317 static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
319 struct dsa_port *port;
323 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
324 * driver and before ops->setup() has run, since the switch drivers and
325 * the slave MDIO bus driver rely on these values for probing PHY
328 ds->phys_mii_mask = ds->enabled_port_mask;
330 /* Add the switch to devlink before calling setup, so that setup can
333 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
337 err = devlink_register(ds->devlink, ds->dev);
341 err = ds->ops->setup(ds);
345 err = dsa_switch_register_notifier(ds);
349 if (ds->ops->set_addr) {
350 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
355 if (!ds->slave_mii_bus && ds->ops->phy_read) {
356 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
357 if (!ds->slave_mii_bus)
360 dsa_slave_mii_bus_init(ds);
362 err = mdiobus_register(ds->slave_mii_bus);
367 for (index = 0; index < ds->num_ports; index++) {
368 port = &ds->ports[index];
369 if (!dsa_port_is_valid(port))
372 if (dsa_port_is_dsa(port)) {
373 err = dsa_dsa_port_apply(port, index, ds);
379 if (dsa_port_is_cpu(port)) {
380 err = dsa_cpu_port_apply(port, index, ds);
386 err = dsa_user_port_apply(port, index, ds);
394 static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
396 struct dsa_port *port;
399 for (index = 0; index < ds->num_ports; index++) {
400 port = &ds->ports[index];
401 if (!dsa_port_is_valid(port))
404 if (dsa_port_is_dsa(port)) {
405 dsa_dsa_port_unapply(port, index, ds);
409 if (dsa_port_is_cpu(port)) {
410 dsa_cpu_port_unapply(port, index, ds);
414 dsa_user_port_unapply(port, index, ds);
417 if (ds->slave_mii_bus && ds->ops->phy_read)
418 mdiobus_unregister(ds->slave_mii_bus);
420 dsa_switch_unregister_notifier(ds);
423 devlink_unregister(ds->devlink);
424 devlink_free(ds->devlink);
430 static int dsa_dst_apply(struct dsa_switch_tree *dst)
432 struct dsa_switch *ds;
436 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
441 err = dsa_ds_apply(dst, ds);
446 if (dst->cpu_switch) {
447 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
452 /* If we use a tagging format that doesn't have an ethertype
453 * field, make sure that all packets from this point on get
454 * sent to the tag format's receive function.
457 dst->master_netdev->dsa_ptr = (void *)dst;
463 static void dsa_dst_unapply(struct dsa_switch_tree *dst)
465 struct dsa_switch *ds;
471 dst->master_netdev->dsa_ptr = NULL;
473 /* If we used a tagging format that doesn't have an ethertype
474 * field, make sure that all packets from this point get sent
475 * without the tag and go through the regular receive path.
479 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
484 dsa_ds_unapply(dst, ds);
487 if (dst->cpu_switch) {
488 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
489 dst->cpu_switch = NULL;
492 pr_info("DSA: tree %d unapplied\n", dst->tree);
493 dst->applied = false;
496 static int dsa_cpu_parse(struct dsa_port *port, u32 index,
497 struct dsa_switch_tree *dst,
498 struct dsa_switch *ds)
500 enum dsa_tag_protocol tag_protocol;
501 struct net_device *ethernet_dev;
502 struct device_node *ethernet;
505 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
508 ethernet_dev = of_find_net_device_by_node(ethernet);
510 ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]);
511 dev_put(ethernet_dev);
515 return -EPROBE_DEFER;
517 if (!ds->master_netdev)
518 ds->master_netdev = ethernet_dev;
520 if (!dst->master_netdev)
521 dst->master_netdev = ethernet_dev;
523 if (!dst->cpu_switch) {
524 dst->cpu_switch = ds;
525 dst->cpu_port = index;
528 tag_protocol = ds->ops->get_tag_protocol(ds);
529 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
530 if (IS_ERR(dst->tag_ops)) {
531 dev_warn(ds->dev, "No tagger for this switch\n");
532 return PTR_ERR(dst->tag_ops);
535 dst->rcv = dst->tag_ops->rcv;
540 static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
542 struct dsa_port *port;
546 for (index = 0; index < ds->num_ports; index++) {
547 port = &ds->ports[index];
548 if (!dsa_port_is_valid(port))
551 if (dsa_port_is_cpu(port)) {
552 err = dsa_cpu_parse(port, index, dst, ds);
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;
569 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
574 err = dsa_ds_parse(dst, ds);
579 if (!dst->master_netdev) {
580 pr_warn("Tree has no master device\n");
584 pr_info("DSA: tree %d parsed\n", dst->tree);
589 static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
591 struct device_node *port;
595 for_each_available_child_of_node(ports, port) {
596 err = of_property_read_u32(port, "reg", ®);
600 if (reg >= ds->num_ports)
603 ds->ports[reg].dn = port;
605 /* Initialize enabled_port_mask now for ops->setup()
606 * to have access to a correct value, just like what
607 * net/dsa/dsa.c::dsa_switch_setup_one does.
609 if (!dsa_port_is_cpu(&ds->ports[reg]))
610 ds->enabled_port_mask |= 1 << reg;
616 static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
618 bool valid_name_found = false;
621 for (i = 0; i < DSA_MAX_PORTS; i++) {
622 if (!cd->port_names[i])
625 ds->ports[i].name = cd->port_names[i];
627 /* Initialize enabled_port_mask now for drv->setup()
628 * to have access to a correct value, just like what
629 * net/dsa/dsa.c::dsa_switch_setup_one does.
631 if (!dsa_port_is_cpu(&ds->ports[i]))
632 ds->enabled_port_mask |= 1 << i;
634 valid_name_found = true;
637 if (!valid_name_found && i == DSA_MAX_PORTS)
643 static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
649 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
651 /* Does not exist, but it is optional */
657 err = of_property_read_u32_index(np, "dsa,member", 1, index);
661 if (*index >= DSA_MAX_SWITCHES)
667 static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
672 /* We do not support complex trees with dsa_chip_data */
679 static struct device_node *dsa_get_ports(struct dsa_switch *ds,
680 struct device_node *np)
682 struct device_node *ports;
684 ports = of_get_child_by_name(np, "ports");
686 dev_err(ds->dev, "no ports child node found\n");
687 return ERR_PTR(-EINVAL);
693 static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
695 struct dsa_chip_data *pdata = dev->platform_data;
696 struct device_node *np = dev->of_node;
697 struct dsa_switch_tree *dst;
698 struct device_node *ports;
703 err = dsa_parse_member_dn(np, &tree, &index);
707 ports = dsa_get_ports(ds, np);
709 return PTR_ERR(ports);
711 err = dsa_parse_ports_dn(ports, ds);
715 err = dsa_parse_member(pdata, &tree, &index);
719 err = dsa_parse_ports(pdata, ds);
724 dst = dsa_get_dst(tree);
726 dst = dsa_add_dst(tree);
731 if (dst->ds[index]) {
740 /* Initialize the routing table */
741 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
742 ds->rtable[i] = DSA_RTABLE_NONE;
744 dsa_dst_add_ds(dst, ds, index);
746 err = dsa_dst_complete(dst);
751 /* Not all switches registered yet */
757 pr_info("DSA: Disjoint trees?\n");
761 err = dsa_dst_parse(dst);
763 if (err == -EPROBE_DEFER) {
764 dsa_dst_del_ds(dst, ds, ds->index);
771 err = dsa_dst_apply(dst);
773 dsa_dst_unapply(dst);
781 dsa_dst_del_ds(dst, ds, ds->index);
788 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
790 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
791 struct dsa_switch *ds;
794 ds = devm_kzalloc(dev, size, GFP_KERNEL);
801 for (i = 0; i < ds->num_ports; ++i) {
802 ds->ports[i].index = i;
803 ds->ports[i].ds = ds;
808 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
810 int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
814 mutex_lock(&dsa2_mutex);
815 err = _dsa_register_switch(ds, dev);
816 mutex_unlock(&dsa2_mutex);
820 EXPORT_SYMBOL_GPL(dsa_register_switch);
822 static void _dsa_unregister_switch(struct dsa_switch *ds)
824 struct dsa_switch_tree *dst = ds->dst;
826 dsa_dst_unapply(dst);
828 dsa_dst_del_ds(dst, ds, ds->index);
831 void dsa_unregister_switch(struct dsa_switch *ds)
833 mutex_lock(&dsa2_mutex);
834 _dsa_unregister_switch(ds);
835 mutex_unlock(&dsa2_mutex);
837 EXPORT_SYMBOL_GPL(dsa_unregister_switch);