x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / net / dsa / dsa2.c
blobffaa4fb33d0ac94f604402446bf503c8ebff121e
1 /*
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>
18 #include <net/dsa.h>
19 #include <linux/of.h>
20 #include <linux/of_net.h>
21 #include "dsa_priv.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);
33 return dst;
35 return NULL;
38 static void dsa_free_dst(struct kref *ref)
40 struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree,
41 refcount);
43 list_del(&dst->list);
44 kfree(dst);
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);
57 if (!dst)
58 return NULL;
59 dst->tree = tree;
60 INIT_LIST_HEAD(&dst->list);
61 list_add_tail(&dsa_switch_trees, &dst->list);
62 kref_init(&dst->refcount);
64 return dst;
67 static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
68 struct dsa_switch *ds, u32 index)
70 kref_get(&dst->refcount);
71 dst->ds[index] = ds;
74 static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
75 struct dsa_switch *ds, u32 index)
77 dst->ds[index] = NULL;
78 kref_put(&dst->refcount, dsa_free_dst);
81 /* For platform data configurations, we need to have a valid name argument to
82 * differentiate a disabled port from an enabled one
84 static bool dsa_port_is_valid(struct dsa_port *port)
86 return !!(port->dn || port->name);
89 static bool dsa_port_is_dsa(struct dsa_port *port)
91 if (port->name && !strcmp(port->name, "dsa"))
92 return true;
93 else
94 return !!of_parse_phandle(port->dn, "link", 0);
97 static bool dsa_port_is_cpu(struct dsa_port *port)
99 if (port->name && !strcmp(port->name, "cpu"))
100 return true;
101 else
102 return !!of_parse_phandle(port->dn, "ethernet", 0);
105 static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
106 struct device_node *port)
108 u32 index;
110 for (index = 0; index < ds->num_ports; index++)
111 if (ds->ports[index].dn == port)
112 return true;
113 return false;
116 static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
117 struct device_node *port)
119 struct dsa_switch *ds;
120 u32 index;
122 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
123 ds = dst->ds[index];
124 if (!ds)
125 continue;
127 if (dsa_ds_find_port_dn(ds, port))
128 return ds;
131 return NULL;
134 static int dsa_port_complete(struct dsa_switch_tree *dst,
135 struct dsa_switch *src_ds,
136 struct dsa_port *port,
137 u32 src_port)
139 struct device_node *link;
140 int index;
141 struct dsa_switch *dst_ds;
143 for (index = 0;; index++) {
144 link = of_parse_phandle(port->dn, "link", index);
145 if (!link)
146 break;
148 dst_ds = dsa_dst_find_port_dn(dst, link);
149 of_node_put(link);
151 if (!dst_ds)
152 return 1;
154 src_ds->rtable[dst_ds->index] = src_port;
157 return 0;
160 /* A switch is complete if all the DSA ports phandles point to ports
161 * known in the tree. A return value of 1 means the tree is not
162 * complete. This is not an error condition. A value of 0 is
163 * success.
165 static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
167 struct dsa_port *port;
168 u32 index;
169 int err;
171 for (index = 0; index < ds->num_ports; index++) {
172 port = &ds->ports[index];
173 if (!dsa_port_is_valid(port))
174 continue;
176 if (!dsa_port_is_dsa(port))
177 continue;
179 err = dsa_port_complete(dst, ds, port, index);
180 if (err != 0)
181 return err;
183 ds->dsa_port_mask |= BIT(index);
186 return 0;
189 /* A tree is complete if all the DSA ports phandles point to ports
190 * known in the tree. A return value of 1 means the tree is not
191 * complete. This is not an error condition. A value of 0 is
192 * success.
194 static int dsa_dst_complete(struct dsa_switch_tree *dst)
196 struct dsa_switch *ds;
197 u32 index;
198 int err;
200 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
201 ds = dst->ds[index];
202 if (!ds)
203 continue;
205 err = dsa_ds_complete(dst, ds);
206 if (err != 0)
207 return err;
210 return 0;
213 static int dsa_dsa_port_apply(struct dsa_port *port, u32 index,
214 struct dsa_switch *ds)
216 int err;
218 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
219 if (err) {
220 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
221 index, err);
222 return err;
225 return 0;
228 static void dsa_dsa_port_unapply(struct dsa_port *port, u32 index,
229 struct dsa_switch *ds)
231 dsa_cpu_dsa_destroy(port);
234 static int dsa_cpu_port_apply(struct dsa_port *port, u32 index,
235 struct dsa_switch *ds)
237 int err;
239 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
240 if (err) {
241 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
242 index, err);
243 return err;
246 ds->cpu_port_mask |= BIT(index);
248 return 0;
251 static void dsa_cpu_port_unapply(struct dsa_port *port, u32 index,
252 struct dsa_switch *ds)
254 dsa_cpu_dsa_destroy(port);
255 ds->cpu_port_mask &= ~BIT(index);
259 static int dsa_user_port_apply(struct dsa_port *port, u32 index,
260 struct dsa_switch *ds)
262 const char *name = port->name;
263 int err;
265 if (port->dn)
266 name = of_get_property(port->dn, "label", NULL);
267 if (!name)
268 name = "eth%d";
270 err = dsa_slave_create(ds, ds->dev, index, name);
271 if (err) {
272 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
273 index, err);
274 ds->ports[index].netdev = NULL;
275 return err;
278 return 0;
281 static void dsa_user_port_unapply(struct dsa_port *port, u32 index,
282 struct dsa_switch *ds)
284 if (ds->ports[index].netdev) {
285 dsa_slave_destroy(ds->ports[index].netdev);
286 ds->ports[index].netdev = NULL;
287 ds->enabled_port_mask &= ~(1 << index);
291 static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
293 struct dsa_port *port;
294 u32 index;
295 int err;
297 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
298 * driver and before ops->setup() has run, since the switch drivers and
299 * the slave MDIO bus driver rely on these values for probing PHY
300 * devices or not
302 ds->phys_mii_mask = ds->enabled_port_mask;
304 err = ds->ops->setup(ds);
305 if (err < 0)
306 return err;
308 err = dsa_switch_register_notifier(ds);
309 if (err)
310 return err;
312 if (ds->ops->set_addr) {
313 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
314 if (err < 0)
315 return err;
318 if (!ds->slave_mii_bus && ds->ops->phy_read) {
319 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
320 if (!ds->slave_mii_bus)
321 return -ENOMEM;
323 dsa_slave_mii_bus_init(ds);
325 err = mdiobus_register(ds->slave_mii_bus);
326 if (err < 0)
327 return err;
330 for (index = 0; index < ds->num_ports; index++) {
331 port = &ds->ports[index];
332 if (!dsa_port_is_valid(port))
333 continue;
335 if (dsa_port_is_dsa(port)) {
336 err = dsa_dsa_port_apply(port, index, ds);
337 if (err)
338 return err;
339 continue;
342 if (dsa_port_is_cpu(port)) {
343 err = dsa_cpu_port_apply(port, index, ds);
344 if (err)
345 return err;
346 continue;
349 err = dsa_user_port_apply(port, index, ds);
350 if (err)
351 continue;
354 return 0;
357 static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
359 struct dsa_port *port;
360 u32 index;
362 for (index = 0; index < ds->num_ports; index++) {
363 port = &ds->ports[index];
364 if (!dsa_port_is_valid(port))
365 continue;
367 if (dsa_port_is_dsa(port)) {
368 dsa_dsa_port_unapply(port, index, ds);
369 continue;
372 if (dsa_port_is_cpu(port)) {
373 dsa_cpu_port_unapply(port, index, ds);
374 continue;
377 dsa_user_port_unapply(port, index, ds);
380 if (ds->slave_mii_bus && ds->ops->phy_read)
381 mdiobus_unregister(ds->slave_mii_bus);
383 dsa_switch_unregister_notifier(ds);
386 static int dsa_dst_apply(struct dsa_switch_tree *dst)
388 struct dsa_switch *ds;
389 u32 index;
390 int err;
392 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
393 ds = dst->ds[index];
394 if (!ds)
395 continue;
397 err = dsa_ds_apply(dst, ds);
398 if (err)
399 return err;
402 if (dst->cpu_switch) {
403 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
404 if (err)
405 return err;
408 /* If we use a tagging format that doesn't have an ethertype
409 * field, make sure that all packets from this point on get
410 * sent to the tag format's receive function.
412 wmb();
413 dst->master_netdev->dsa_ptr = (void *)dst;
414 dst->applied = true;
416 return 0;
419 static void dsa_dst_unapply(struct dsa_switch_tree *dst)
421 struct dsa_switch *ds;
422 u32 index;
424 if (!dst->applied)
425 return;
427 dst->master_netdev->dsa_ptr = NULL;
429 /* If we used a tagging format that doesn't have an ethertype
430 * field, make sure that all packets from this point get sent
431 * without the tag and go through the regular receive path.
433 wmb();
435 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
436 ds = dst->ds[index];
437 if (!ds)
438 continue;
440 dsa_ds_unapply(dst, ds);
443 if (dst->cpu_switch) {
444 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
445 dst->cpu_switch = NULL;
448 pr_info("DSA: tree %d unapplied\n", dst->tree);
449 dst->applied = false;
452 static int dsa_cpu_parse(struct dsa_port *port, u32 index,
453 struct dsa_switch_tree *dst,
454 struct dsa_switch *ds)
456 enum dsa_tag_protocol tag_protocol;
457 struct net_device *ethernet_dev;
458 struct device_node *ethernet;
460 if (port->dn) {
461 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
462 if (!ethernet)
463 return -EINVAL;
464 ethernet_dev = of_find_net_device_by_node(ethernet);
465 } else {
466 ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]);
467 dev_put(ethernet_dev);
470 if (!ethernet_dev)
471 return -EPROBE_DEFER;
473 if (!ds->master_netdev)
474 ds->master_netdev = ethernet_dev;
476 if (!dst->master_netdev)
477 dst->master_netdev = ethernet_dev;
479 if (!dst->cpu_switch) {
480 dst->cpu_switch = ds;
481 dst->cpu_port = index;
484 tag_protocol = ds->ops->get_tag_protocol(ds);
485 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
486 if (IS_ERR(dst->tag_ops)) {
487 dev_warn(ds->dev, "No tagger for this switch\n");
488 return PTR_ERR(dst->tag_ops);
491 dst->rcv = dst->tag_ops->rcv;
493 return 0;
496 static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
498 struct dsa_port *port;
499 u32 index;
500 int err;
502 for (index = 0; index < ds->num_ports; index++) {
503 port = &ds->ports[index];
504 if (!dsa_port_is_valid(port))
505 continue;
507 if (dsa_port_is_cpu(port)) {
508 err = dsa_cpu_parse(port, index, dst, ds);
509 if (err)
510 return err;
514 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
516 return 0;
519 static int dsa_dst_parse(struct dsa_switch_tree *dst)
521 struct dsa_switch *ds;
522 u32 index;
523 int err;
525 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
526 ds = dst->ds[index];
527 if (!ds)
528 continue;
530 err = dsa_ds_parse(dst, ds);
531 if (err)
532 return err;
535 if (!dst->master_netdev) {
536 pr_warn("Tree has no master device\n");
537 return -EINVAL;
540 pr_info("DSA: tree %d parsed\n", dst->tree);
542 return 0;
545 static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
547 struct device_node *port;
548 int err;
549 u32 reg;
551 for_each_available_child_of_node(ports, port) {
552 err = of_property_read_u32(port, "reg", &reg);
553 if (err)
554 return err;
556 if (reg >= ds->num_ports)
557 return -EINVAL;
559 ds->ports[reg].dn = port;
561 /* Initialize enabled_port_mask now for ops->setup()
562 * to have access to a correct value, just like what
563 * net/dsa/dsa.c::dsa_switch_setup_one does.
565 if (!dsa_port_is_cpu(&ds->ports[reg]))
566 ds->enabled_port_mask |= 1 << reg;
569 return 0;
572 static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
574 bool valid_name_found = false;
575 unsigned int i;
577 for (i = 0; i < DSA_MAX_PORTS; i++) {
578 if (!cd->port_names[i])
579 continue;
581 ds->ports[i].name = cd->port_names[i];
583 /* Initialize enabled_port_mask now for drv->setup()
584 * to have access to a correct value, just like what
585 * net/dsa/dsa.c::dsa_switch_setup_one does.
587 if (!dsa_port_is_cpu(&ds->ports[i]))
588 ds->enabled_port_mask |= 1 << i;
590 valid_name_found = true;
593 if (!valid_name_found && i == DSA_MAX_PORTS)
594 return -EINVAL;
596 return 0;
599 static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
601 int err;
603 *tree = *index = 0;
605 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
606 if (err) {
607 /* Does not exist, but it is optional */
608 if (err == -EINVAL)
609 return 0;
610 return err;
613 err = of_property_read_u32_index(np, "dsa,member", 1, index);
614 if (err)
615 return err;
617 if (*index >= DSA_MAX_SWITCHES)
618 return -EINVAL;
620 return 0;
623 static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
625 if (!pd)
626 return -ENODEV;
628 /* We do not support complex trees with dsa_chip_data */
629 *tree = 0;
630 *index = 0;
632 return 0;
635 static struct device_node *dsa_get_ports(struct dsa_switch *ds,
636 struct device_node *np)
638 struct device_node *ports;
640 ports = of_get_child_by_name(np, "ports");
641 if (!ports) {
642 dev_err(ds->dev, "no ports child node found\n");
643 return ERR_PTR(-EINVAL);
646 return ports;
649 static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
651 struct dsa_chip_data *pdata = dev->platform_data;
652 struct device_node *np = dev->of_node;
653 struct dsa_switch_tree *dst;
654 struct device_node *ports;
655 u32 tree, index;
656 int i, err;
658 if (np) {
659 err = dsa_parse_member_dn(np, &tree, &index);
660 if (err)
661 return err;
663 ports = dsa_get_ports(ds, np);
664 if (IS_ERR(ports))
665 return PTR_ERR(ports);
667 err = dsa_parse_ports_dn(ports, ds);
668 if (err)
669 return err;
670 } else {
671 err = dsa_parse_member(pdata, &tree, &index);
672 if (err)
673 return err;
675 err = dsa_parse_ports(pdata, ds);
676 if (err)
677 return err;
680 dst = dsa_get_dst(tree);
681 if (!dst) {
682 dst = dsa_add_dst(tree);
683 if (!dst)
684 return -ENOMEM;
687 if (dst->ds[index]) {
688 err = -EBUSY;
689 goto out;
692 ds->dst = dst;
693 ds->index = index;
694 ds->cd = pdata;
696 /* Initialize the routing table */
697 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
698 ds->rtable[i] = DSA_RTABLE_NONE;
700 dsa_dst_add_ds(dst, ds, index);
702 err = dsa_dst_complete(dst);
703 if (err < 0)
704 goto out_del_dst;
706 if (err == 1) {
707 /* Not all switches registered yet */
708 err = 0;
709 goto out;
712 if (dst->applied) {
713 pr_info("DSA: Disjoint trees?\n");
714 return -EINVAL;
717 err = dsa_dst_parse(dst);
718 if (err) {
719 if (err == -EPROBE_DEFER) {
720 dsa_dst_del_ds(dst, ds, ds->index);
721 return err;
724 goto out_del_dst;
727 err = dsa_dst_apply(dst);
728 if (err) {
729 dsa_dst_unapply(dst);
730 goto out_del_dst;
733 dsa_put_dst(dst);
734 return 0;
736 out_del_dst:
737 dsa_dst_del_ds(dst, ds, ds->index);
738 out:
739 dsa_put_dst(dst);
741 return err;
744 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
746 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
747 struct dsa_switch *ds;
748 int i;
750 ds = devm_kzalloc(dev, size, GFP_KERNEL);
751 if (!ds)
752 return NULL;
754 ds->dev = dev;
755 ds->num_ports = n;
757 for (i = 0; i < ds->num_ports; ++i) {
758 ds->ports[i].index = i;
759 ds->ports[i].ds = ds;
762 return ds;
764 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
766 int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
768 int err;
770 mutex_lock(&dsa2_mutex);
771 err = _dsa_register_switch(ds, dev);
772 mutex_unlock(&dsa2_mutex);
774 return err;
776 EXPORT_SYMBOL_GPL(dsa_register_switch);
778 static void _dsa_unregister_switch(struct dsa_switch *ds)
780 struct dsa_switch_tree *dst = ds->dst;
782 dsa_dst_unapply(dst);
784 dsa_dst_del_ds(dst, ds, ds->index);
787 void dsa_unregister_switch(struct dsa_switch *ds)
789 mutex_lock(&dsa2_mutex);
790 _dsa_unregister_switch(ds);
791 mutex_unlock(&dsa2_mutex);
793 EXPORT_SYMBOL_GPL(dsa_unregister_switch);