btrfs: migrate the block group ref counting stuff
[linux/fpc-iii.git] / net / dsa / dsa2.c
blob3abd173ebacbd134d6e420f05ddee765f0c9fdd1
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
6 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
7 */
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/of.h>
16 #include <linux/of_net.h>
17 #include <net/devlink.h>
19 #include "dsa_priv.h"
21 static LIST_HEAD(dsa_tree_list);
22 static DEFINE_MUTEX(dsa2_mutex);
24 static const struct devlink_ops dsa_devlink_ops = {
27 static struct dsa_switch_tree *dsa_tree_find(int index)
29 struct dsa_switch_tree *dst;
31 list_for_each_entry(dst, &dsa_tree_list, list)
32 if (dst->index == index)
33 return dst;
35 return NULL;
38 static struct dsa_switch_tree *dsa_tree_alloc(int index)
40 struct dsa_switch_tree *dst;
42 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
43 if (!dst)
44 return NULL;
46 dst->index = index;
48 INIT_LIST_HEAD(&dst->list);
49 list_add_tail(&dsa_tree_list, &dst->list);
51 kref_init(&dst->refcount);
53 return dst;
56 static void dsa_tree_free(struct dsa_switch_tree *dst)
58 list_del(&dst->list);
59 kfree(dst);
62 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
64 if (dst)
65 kref_get(&dst->refcount);
67 return dst;
70 static struct dsa_switch_tree *dsa_tree_touch(int index)
72 struct dsa_switch_tree *dst;
74 dst = dsa_tree_find(index);
75 if (dst)
76 return dsa_tree_get(dst);
77 else
78 return dsa_tree_alloc(index);
81 static void dsa_tree_release(struct kref *ref)
83 struct dsa_switch_tree *dst;
85 dst = container_of(ref, struct dsa_switch_tree, refcount);
87 dsa_tree_free(dst);
90 static void dsa_tree_put(struct dsa_switch_tree *dst)
92 if (dst)
93 kref_put(&dst->refcount, dsa_tree_release);
96 static bool dsa_port_is_dsa(struct dsa_port *port)
98 return port->type == DSA_PORT_TYPE_DSA;
101 static bool dsa_port_is_cpu(struct dsa_port *port)
103 return port->type == DSA_PORT_TYPE_CPU;
106 static bool dsa_port_is_user(struct dsa_port *dp)
108 return dp->type == DSA_PORT_TYPE_USER;
111 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
112 struct device_node *dn)
114 struct dsa_switch *ds;
115 struct dsa_port *dp;
116 int device, port;
118 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
119 ds = dst->ds[device];
120 if (!ds)
121 continue;
123 for (port = 0; port < ds->num_ports; port++) {
124 dp = &ds->ports[port];
126 if (dp->dn == dn)
127 return dp;
131 return NULL;
134 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
136 struct dsa_switch *ds = dp->ds;
137 struct dsa_switch_tree *dst = ds->dst;
138 struct device_node *dn = dp->dn;
139 struct of_phandle_iterator it;
140 struct dsa_port *link_dp;
141 int err;
143 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
144 link_dp = dsa_tree_find_port_by_node(dst, it.node);
145 if (!link_dp) {
146 of_node_put(it.node);
147 return false;
150 ds->rtable[link_dp->ds->index] = dp->index;
153 return true;
156 static bool dsa_switch_setup_routing_table(struct dsa_switch *ds)
158 bool complete = true;
159 struct dsa_port *dp;
160 int i;
162 for (i = 0; i < DSA_MAX_SWITCHES; i++)
163 ds->rtable[i] = DSA_RTABLE_NONE;
165 for (i = 0; i < ds->num_ports; i++) {
166 dp = &ds->ports[i];
168 if (dsa_port_is_dsa(dp)) {
169 complete = dsa_port_setup_routing_table(dp);
170 if (!complete)
171 break;
175 return complete;
178 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
180 struct dsa_switch *ds;
181 bool complete = true;
182 int device;
184 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
185 ds = dst->ds[device];
186 if (!ds)
187 continue;
189 complete = dsa_switch_setup_routing_table(ds);
190 if (!complete)
191 break;
194 return complete;
197 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
199 struct dsa_switch *ds;
200 struct dsa_port *dp;
201 int device, port;
203 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
204 ds = dst->ds[device];
205 if (!ds)
206 continue;
208 for (port = 0; port < ds->num_ports; port++) {
209 dp = &ds->ports[port];
211 if (dsa_port_is_cpu(dp))
212 return dp;
216 return NULL;
219 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
221 struct dsa_switch *ds;
222 struct dsa_port *dp;
223 int device, port;
225 /* DSA currently only supports a single CPU port */
226 dst->cpu_dp = dsa_tree_find_first_cpu(dst);
227 if (!dst->cpu_dp) {
228 pr_warn("Tree has no master device\n");
229 return -EINVAL;
232 /* Assign the default CPU port to all ports of the fabric */
233 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
234 ds = dst->ds[device];
235 if (!ds)
236 continue;
238 for (port = 0; port < ds->num_ports; port++) {
239 dp = &ds->ports[port];
241 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
242 dp->cpu_dp = dst->cpu_dp;
246 return 0;
249 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
251 /* DSA currently only supports a single CPU port */
252 dst->cpu_dp = NULL;
255 static int dsa_port_setup(struct dsa_port *dp)
257 enum devlink_port_flavour flavour;
258 struct dsa_switch *ds = dp->ds;
259 struct dsa_switch_tree *dst = ds->dst;
260 int err = 0;
262 if (dp->type == DSA_PORT_TYPE_UNUSED)
263 return 0;
265 memset(&dp->devlink_port, 0, sizeof(dp->devlink_port));
266 dp->mac = of_get_mac_address(dp->dn);
268 switch (dp->type) {
269 case DSA_PORT_TYPE_CPU:
270 flavour = DEVLINK_PORT_FLAVOUR_CPU;
271 break;
272 case DSA_PORT_TYPE_DSA:
273 flavour = DEVLINK_PORT_FLAVOUR_DSA;
274 break;
275 case DSA_PORT_TYPE_USER: /* fall-through */
276 default:
277 flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
278 break;
281 /* dp->index is used now as port_number. However
282 * CPU and DSA ports should have separate numbering
283 * independent from front panel port numbers.
285 devlink_port_attrs_set(&dp->devlink_port, flavour,
286 dp->index, false, 0,
287 (const char *) &dst->index, sizeof(dst->index));
288 err = devlink_port_register(ds->devlink, &dp->devlink_port,
289 dp->index);
290 if (err)
291 return err;
293 switch (dp->type) {
294 case DSA_PORT_TYPE_UNUSED:
295 break;
296 case DSA_PORT_TYPE_CPU:
297 err = dsa_port_link_register_of(dp);
298 if (err)
299 dev_err(ds->dev, "failed to setup link for port %d.%d\n",
300 ds->index, dp->index);
301 break;
302 case DSA_PORT_TYPE_DSA:
303 err = dsa_port_link_register_of(dp);
304 if (err)
305 dev_err(ds->dev, "failed to setup link for port %d.%d\n",
306 ds->index, dp->index);
307 break;
308 case DSA_PORT_TYPE_USER:
309 err = dsa_slave_create(dp);
310 if (err)
311 dev_err(ds->dev, "failed to create slave for port %d.%d\n",
312 ds->index, dp->index);
313 else
314 devlink_port_type_eth_set(&dp->devlink_port, dp->slave);
315 break;
318 if (err)
319 devlink_port_unregister(&dp->devlink_port);
321 return err;
324 static void dsa_port_teardown(struct dsa_port *dp)
326 if (dp->type != DSA_PORT_TYPE_UNUSED)
327 devlink_port_unregister(&dp->devlink_port);
329 switch (dp->type) {
330 case DSA_PORT_TYPE_UNUSED:
331 break;
332 case DSA_PORT_TYPE_CPU:
333 dsa_tag_driver_put(dp->tag_ops);
334 /* fall-through */
335 case DSA_PORT_TYPE_DSA:
336 dsa_port_link_unregister_of(dp);
337 break;
338 case DSA_PORT_TYPE_USER:
339 if (dp->slave) {
340 dsa_slave_destroy(dp->slave);
341 dp->slave = NULL;
343 break;
347 static int dsa_switch_setup(struct dsa_switch *ds)
349 int err = 0;
351 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
352 * driver and before ops->setup() has run, since the switch drivers and
353 * the slave MDIO bus driver rely on these values for probing PHY
354 * devices or not
356 ds->phys_mii_mask |= dsa_user_ports(ds);
358 /* Add the switch to devlink before calling setup, so that setup can
359 * add dpipe tables
361 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
362 if (!ds->devlink)
363 return -ENOMEM;
365 err = devlink_register(ds->devlink, ds->dev);
366 if (err)
367 goto free_devlink;
369 err = dsa_switch_register_notifier(ds);
370 if (err)
371 goto unregister_devlink;
373 err = ds->ops->setup(ds);
374 if (err < 0)
375 goto unregister_notifier;
377 if (!ds->slave_mii_bus && ds->ops->phy_read) {
378 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
379 if (!ds->slave_mii_bus) {
380 err = -ENOMEM;
381 goto unregister_notifier;
384 dsa_slave_mii_bus_init(ds);
386 err = mdiobus_register(ds->slave_mii_bus);
387 if (err < 0)
388 goto unregister_notifier;
391 return 0;
393 unregister_notifier:
394 dsa_switch_unregister_notifier(ds);
395 unregister_devlink:
396 devlink_unregister(ds->devlink);
397 free_devlink:
398 devlink_free(ds->devlink);
399 ds->devlink = NULL;
401 return err;
404 static void dsa_switch_teardown(struct dsa_switch *ds)
406 if (ds->slave_mii_bus && ds->ops->phy_read)
407 mdiobus_unregister(ds->slave_mii_bus);
409 dsa_switch_unregister_notifier(ds);
411 if (ds->ops->teardown)
412 ds->ops->teardown(ds);
414 if (ds->devlink) {
415 devlink_unregister(ds->devlink);
416 devlink_free(ds->devlink);
417 ds->devlink = NULL;
422 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
424 struct dsa_switch *ds;
425 struct dsa_port *dp;
426 int device, port, i;
427 int err = 0;
429 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
430 ds = dst->ds[device];
431 if (!ds)
432 continue;
434 err = dsa_switch_setup(ds);
435 if (err)
436 goto switch_teardown;
438 for (port = 0; port < ds->num_ports; port++) {
439 dp = &ds->ports[port];
441 err = dsa_port_setup(dp);
442 if (err)
443 goto ports_teardown;
447 return 0;
449 ports_teardown:
450 for (i = 0; i < port; i++)
451 dsa_port_teardown(&ds->ports[i]);
453 dsa_switch_teardown(ds);
455 switch_teardown:
456 for (i = 0; i < device; i++) {
457 ds = dst->ds[i];
458 if (!ds)
459 continue;
461 for (port = 0; port < ds->num_ports; port++) {
462 dp = &ds->ports[port];
464 dsa_port_teardown(dp);
467 dsa_switch_teardown(ds);
470 return err;
473 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
475 struct dsa_switch *ds;
476 struct dsa_port *dp;
477 int device, port;
479 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
480 ds = dst->ds[device];
481 if (!ds)
482 continue;
484 for (port = 0; port < ds->num_ports; port++) {
485 dp = &ds->ports[port];
487 dsa_port_teardown(dp);
490 dsa_switch_teardown(ds);
494 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
496 struct dsa_port *cpu_dp = dst->cpu_dp;
497 struct net_device *master = cpu_dp->master;
499 /* DSA currently supports a single pair of CPU port and master device */
500 return dsa_master_setup(master, cpu_dp);
503 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
505 struct dsa_port *cpu_dp = dst->cpu_dp;
506 struct net_device *master = cpu_dp->master;
508 return dsa_master_teardown(master);
511 static int dsa_tree_setup(struct dsa_switch_tree *dst)
513 bool complete;
514 int err;
516 if (dst->setup) {
517 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
518 dst->index);
519 return -EEXIST;
522 complete = dsa_tree_setup_routing_table(dst);
523 if (!complete)
524 return 0;
526 err = dsa_tree_setup_default_cpu(dst);
527 if (err)
528 return err;
530 err = dsa_tree_setup_switches(dst);
531 if (err)
532 goto teardown_default_cpu;
534 err = dsa_tree_setup_master(dst);
535 if (err)
536 goto teardown_switches;
538 dst->setup = true;
540 pr_info("DSA: tree %d setup\n", dst->index);
542 return 0;
544 teardown_switches:
545 dsa_tree_teardown_switches(dst);
546 teardown_default_cpu:
547 dsa_tree_teardown_default_cpu(dst);
549 return err;
552 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
554 if (!dst->setup)
555 return;
557 dsa_tree_teardown_master(dst);
559 dsa_tree_teardown_switches(dst);
561 dsa_tree_teardown_default_cpu(dst);
563 pr_info("DSA: tree %d torn down\n", dst->index);
565 dst->setup = false;
568 static void dsa_tree_remove_switch(struct dsa_switch_tree *dst,
569 unsigned int index)
571 dsa_tree_teardown(dst);
573 dst->ds[index] = NULL;
574 dsa_tree_put(dst);
577 static int dsa_tree_add_switch(struct dsa_switch_tree *dst,
578 struct dsa_switch *ds)
580 unsigned int index = ds->index;
581 int err;
583 if (dst->ds[index])
584 return -EBUSY;
586 dsa_tree_get(dst);
587 dst->ds[index] = ds;
589 err = dsa_tree_setup(dst);
590 if (err) {
591 dst->ds[index] = NULL;
592 dsa_tree_put(dst);
595 return err;
598 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
600 if (!name)
601 name = "eth%d";
603 dp->type = DSA_PORT_TYPE_USER;
604 dp->name = name;
606 return 0;
609 static int dsa_port_parse_dsa(struct dsa_port *dp)
611 dp->type = DSA_PORT_TYPE_DSA;
613 return 0;
616 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
618 struct dsa_switch *ds = dp->ds;
619 struct dsa_switch_tree *dst = ds->dst;
620 const struct dsa_device_ops *tag_ops;
621 enum dsa_tag_protocol tag_protocol;
623 tag_protocol = ds->ops->get_tag_protocol(ds, dp->index);
624 tag_ops = dsa_tag_driver_get(tag_protocol);
625 if (IS_ERR(tag_ops)) {
626 dev_warn(ds->dev, "No tagger for this switch\n");
627 return PTR_ERR(tag_ops);
630 dp->type = DSA_PORT_TYPE_CPU;
631 dp->filter = tag_ops->filter;
632 dp->rcv = tag_ops->rcv;
633 dp->tag_ops = tag_ops;
634 dp->master = master;
635 dp->dst = dst;
637 return 0;
640 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
642 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
643 const char *name = of_get_property(dn, "label", NULL);
644 bool link = of_property_read_bool(dn, "link");
646 dp->dn = dn;
648 if (ethernet) {
649 struct net_device *master;
651 master = of_find_net_device_by_node(ethernet);
652 if (!master)
653 return -EPROBE_DEFER;
655 return dsa_port_parse_cpu(dp, master);
658 if (link)
659 return dsa_port_parse_dsa(dp);
661 return dsa_port_parse_user(dp, name);
664 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
665 struct device_node *dn)
667 struct device_node *ports, *port;
668 struct dsa_port *dp;
669 int err = 0;
670 u32 reg;
672 ports = of_get_child_by_name(dn, "ports");
673 if (!ports) {
674 dev_err(ds->dev, "no ports child node found\n");
675 return -EINVAL;
678 for_each_available_child_of_node(ports, port) {
679 err = of_property_read_u32(port, "reg", &reg);
680 if (err)
681 goto out_put_node;
683 if (reg >= ds->num_ports) {
684 err = -EINVAL;
685 goto out_put_node;
688 dp = &ds->ports[reg];
690 err = dsa_port_parse_of(dp, port);
691 if (err)
692 goto out_put_node;
695 out_put_node:
696 of_node_put(ports);
697 return err;
700 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
701 struct device_node *dn)
703 u32 m[2] = { 0, 0 };
704 int sz;
706 /* Don't error out if this optional property isn't found */
707 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
708 if (sz < 0 && sz != -EINVAL)
709 return sz;
711 ds->index = m[1];
712 if (ds->index >= DSA_MAX_SWITCHES)
713 return -EINVAL;
715 ds->dst = dsa_tree_touch(m[0]);
716 if (!ds->dst)
717 return -ENOMEM;
719 return 0;
722 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
724 int err;
726 err = dsa_switch_parse_member_of(ds, dn);
727 if (err)
728 return err;
730 return dsa_switch_parse_ports_of(ds, dn);
733 static int dsa_port_parse(struct dsa_port *dp, const char *name,
734 struct device *dev)
736 if (!strcmp(name, "cpu")) {
737 struct net_device *master;
739 master = dsa_dev_to_net_device(dev);
740 if (!master)
741 return -EPROBE_DEFER;
743 dev_put(master);
745 return dsa_port_parse_cpu(dp, master);
748 if (!strcmp(name, "dsa"))
749 return dsa_port_parse_dsa(dp);
751 return dsa_port_parse_user(dp, name);
754 static int dsa_switch_parse_ports(struct dsa_switch *ds,
755 struct dsa_chip_data *cd)
757 bool valid_name_found = false;
758 struct dsa_port *dp;
759 struct device *dev;
760 const char *name;
761 unsigned int i;
762 int err;
764 for (i = 0; i < DSA_MAX_PORTS; i++) {
765 name = cd->port_names[i];
766 dev = cd->netdev[i];
767 dp = &ds->ports[i];
769 if (!name)
770 continue;
772 err = dsa_port_parse(dp, name, dev);
773 if (err)
774 return err;
776 valid_name_found = true;
779 if (!valid_name_found && i == DSA_MAX_PORTS)
780 return -EINVAL;
782 return 0;
785 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
787 ds->cd = cd;
789 /* We don't support interconnected switches nor multiple trees via
790 * platform data, so this is the unique switch of the tree.
792 ds->index = 0;
793 ds->dst = dsa_tree_touch(0);
794 if (!ds->dst)
795 return -ENOMEM;
797 return dsa_switch_parse_ports(ds, cd);
800 static int dsa_switch_add(struct dsa_switch *ds)
802 struct dsa_switch_tree *dst = ds->dst;
804 return dsa_tree_add_switch(dst, ds);
807 static int dsa_switch_probe(struct dsa_switch *ds)
809 struct dsa_chip_data *pdata = ds->dev->platform_data;
810 struct device_node *np = ds->dev->of_node;
811 int err;
813 if (np)
814 err = dsa_switch_parse_of(ds, np);
815 else if (pdata)
816 err = dsa_switch_parse(ds, pdata);
817 else
818 err = -ENODEV;
820 if (err)
821 return err;
823 return dsa_switch_add(ds);
826 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
828 struct dsa_switch *ds;
829 int i;
831 ds = devm_kzalloc(dev, struct_size(ds, ports, n), GFP_KERNEL);
832 if (!ds)
833 return NULL;
835 /* We avoid allocating memory outside dsa_switch
836 * if it is not needed.
838 if (n <= sizeof(ds->_bitmap) * 8) {
839 ds->bitmap = &ds->_bitmap;
840 } else {
841 ds->bitmap = devm_kcalloc(dev,
842 BITS_TO_LONGS(n),
843 sizeof(unsigned long),
844 GFP_KERNEL);
845 if (unlikely(!ds->bitmap))
846 return NULL;
849 ds->dev = dev;
850 ds->num_ports = n;
852 for (i = 0; i < ds->num_ports; ++i) {
853 ds->ports[i].index = i;
854 ds->ports[i].ds = ds;
857 return ds;
859 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
861 int dsa_register_switch(struct dsa_switch *ds)
863 int err;
865 mutex_lock(&dsa2_mutex);
866 err = dsa_switch_probe(ds);
867 dsa_tree_put(ds->dst);
868 mutex_unlock(&dsa2_mutex);
870 return err;
872 EXPORT_SYMBOL_GPL(dsa_register_switch);
874 static void dsa_switch_remove(struct dsa_switch *ds)
876 struct dsa_switch_tree *dst = ds->dst;
877 unsigned int index = ds->index;
879 dsa_tree_remove_switch(dst, index);
882 void dsa_unregister_switch(struct dsa_switch *ds)
884 mutex_lock(&dsa2_mutex);
885 dsa_switch_remove(ds);
886 mutex_unlock(&dsa2_mutex);
888 EXPORT_SYMBOL_GPL(dsa_unregister_switch);