147d2ce03SVladimir Oltean // SPDX-License-Identifier: GPL-2.0-or-later
247d2ce03SVladimir Oltean /*
347d2ce03SVladimir Oltean * DSA topology and switch handling
447d2ce03SVladimir Oltean *
547d2ce03SVladimir Oltean * Copyright (c) 2008-2009 Marvell Semiconductor
647d2ce03SVladimir Oltean * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
747d2ce03SVladimir Oltean * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
847d2ce03SVladimir Oltean */
947d2ce03SVladimir Oltean
1047d2ce03SVladimir Oltean #include <linux/device.h>
1147d2ce03SVladimir Oltean #include <linux/err.h>
1247d2ce03SVladimir Oltean #include <linux/list.h>
1347d2ce03SVladimir Oltean #include <linux/module.h>
1447d2ce03SVladimir Oltean #include <linux/netdevice.h>
1547d2ce03SVladimir Oltean #include <linux/slab.h>
1647d2ce03SVladimir Oltean #include <linux/rtnetlink.h>
1747d2ce03SVladimir Oltean #include <linux/of.h>
1847d2ce03SVladimir Oltean #include <linux/of_mdio.h>
1947d2ce03SVladimir Oltean #include <linux/of_net.h>
205a178186SVladimir Oltean #include <net/dsa_stubs.h>
2147d2ce03SVladimir Oltean #include <net/sch_generic.h>
2247d2ce03SVladimir Oltean
2347d2ce03SVladimir Oltean #include "devlink.h"
2447d2ce03SVladimir Oltean #include "dsa.h"
2547d2ce03SVladimir Oltean #include "master.h"
265917bfe6SVladimir Oltean #include "netlink.h"
2747d2ce03SVladimir Oltean #include "port.h"
2847d2ce03SVladimir Oltean #include "slave.h"
2947d2ce03SVladimir Oltean #include "switch.h"
3047d2ce03SVladimir Oltean #include "tag.h"
3147d2ce03SVladimir Oltean
325917bfe6SVladimir Oltean #define DSA_MAX_NUM_OFFLOADING_BRIDGES BITS_PER_LONG
335917bfe6SVladimir Oltean
3447d2ce03SVladimir Oltean static DEFINE_MUTEX(dsa2_mutex);
3547d2ce03SVladimir Oltean LIST_HEAD(dsa_tree_list);
3647d2ce03SVladimir Oltean
3747d2ce03SVladimir Oltean static struct workqueue_struct *dsa_owq;
3847d2ce03SVladimir Oltean
3947d2ce03SVladimir Oltean /* Track the bridges with forwarding offload enabled */
4047d2ce03SVladimir Oltean static unsigned long dsa_fwd_offloading_bridges;
4147d2ce03SVladimir Oltean
dsa_schedule_work(struct work_struct * work)4247d2ce03SVladimir Oltean bool dsa_schedule_work(struct work_struct *work)
4347d2ce03SVladimir Oltean {
4447d2ce03SVladimir Oltean return queue_work(dsa_owq, work);
4547d2ce03SVladimir Oltean }
4647d2ce03SVladimir Oltean
dsa_flush_workqueue(void)4747d2ce03SVladimir Oltean void dsa_flush_workqueue(void)
4847d2ce03SVladimir Oltean {
4947d2ce03SVladimir Oltean flush_workqueue(dsa_owq);
5047d2ce03SVladimir Oltean }
5147d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_flush_workqueue);
5247d2ce03SVladimir Oltean
5347d2ce03SVladimir Oltean /**
5447d2ce03SVladimir Oltean * dsa_lag_map() - Map LAG structure to a linear LAG array
5547d2ce03SVladimir Oltean * @dst: Tree in which to record the mapping.
5647d2ce03SVladimir Oltean * @lag: LAG structure that is to be mapped to the tree's array.
5747d2ce03SVladimir Oltean *
5847d2ce03SVladimir Oltean * dsa_lag_id/dsa_lag_by_id can then be used to translate between the
5947d2ce03SVladimir Oltean * two spaces. The size of the mapping space is determined by the
6047d2ce03SVladimir Oltean * driver by setting ds->num_lag_ids. It is perfectly legal to leave
6147d2ce03SVladimir Oltean * it unset if it is not needed, in which case these functions become
6247d2ce03SVladimir Oltean * no-ops.
6347d2ce03SVladimir Oltean */
dsa_lag_map(struct dsa_switch_tree * dst,struct dsa_lag * lag)6447d2ce03SVladimir Oltean void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag)
6547d2ce03SVladimir Oltean {
6647d2ce03SVladimir Oltean unsigned int id;
6747d2ce03SVladimir Oltean
6847d2ce03SVladimir Oltean for (id = 1; id <= dst->lags_len; id++) {
6947d2ce03SVladimir Oltean if (!dsa_lag_by_id(dst, id)) {
7047d2ce03SVladimir Oltean dst->lags[id - 1] = lag;
7147d2ce03SVladimir Oltean lag->id = id;
7247d2ce03SVladimir Oltean return;
7347d2ce03SVladimir Oltean }
7447d2ce03SVladimir Oltean }
7547d2ce03SVladimir Oltean
7647d2ce03SVladimir Oltean /* No IDs left, which is OK. Some drivers do not need it. The
7747d2ce03SVladimir Oltean * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
7847d2ce03SVladimir Oltean * returns an error for this device when joining the LAG. The
7947d2ce03SVladimir Oltean * driver can then return -EOPNOTSUPP back to DSA, which will
8047d2ce03SVladimir Oltean * fall back to a software LAG.
8147d2ce03SVladimir Oltean */
8247d2ce03SVladimir Oltean }
8347d2ce03SVladimir Oltean
8447d2ce03SVladimir Oltean /**
8547d2ce03SVladimir Oltean * dsa_lag_unmap() - Remove a LAG ID mapping
8647d2ce03SVladimir Oltean * @dst: Tree in which the mapping is recorded.
8747d2ce03SVladimir Oltean * @lag: LAG structure that was mapped.
8847d2ce03SVladimir Oltean *
8947d2ce03SVladimir Oltean * As there may be multiple users of the mapping, it is only removed
9047d2ce03SVladimir Oltean * if there are no other references to it.
9147d2ce03SVladimir Oltean */
dsa_lag_unmap(struct dsa_switch_tree * dst,struct dsa_lag * lag)9247d2ce03SVladimir Oltean void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag)
9347d2ce03SVladimir Oltean {
9447d2ce03SVladimir Oltean unsigned int id;
9547d2ce03SVladimir Oltean
9647d2ce03SVladimir Oltean dsa_lags_foreach_id(id, dst) {
9747d2ce03SVladimir Oltean if (dsa_lag_by_id(dst, id) == lag) {
9847d2ce03SVladimir Oltean dst->lags[id - 1] = NULL;
9947d2ce03SVladimir Oltean lag->id = 0;
10047d2ce03SVladimir Oltean break;
10147d2ce03SVladimir Oltean }
10247d2ce03SVladimir Oltean }
10347d2ce03SVladimir Oltean }
10447d2ce03SVladimir Oltean
dsa_tree_lag_find(struct dsa_switch_tree * dst,const struct net_device * lag_dev)10547d2ce03SVladimir Oltean struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
10647d2ce03SVladimir Oltean const struct net_device *lag_dev)
10747d2ce03SVladimir Oltean {
10847d2ce03SVladimir Oltean struct dsa_port *dp;
10947d2ce03SVladimir Oltean
11047d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list)
11147d2ce03SVladimir Oltean if (dsa_port_lag_dev_get(dp) == lag_dev)
11247d2ce03SVladimir Oltean return dp->lag;
11347d2ce03SVladimir Oltean
11447d2ce03SVladimir Oltean return NULL;
11547d2ce03SVladimir Oltean }
11647d2ce03SVladimir Oltean
dsa_tree_bridge_find(struct dsa_switch_tree * dst,const struct net_device * br)11747d2ce03SVladimir Oltean struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
11847d2ce03SVladimir Oltean const struct net_device *br)
11947d2ce03SVladimir Oltean {
12047d2ce03SVladimir Oltean struct dsa_port *dp;
12147d2ce03SVladimir Oltean
12247d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list)
12347d2ce03SVladimir Oltean if (dsa_port_bridge_dev_get(dp) == br)
12447d2ce03SVladimir Oltean return dp->bridge;
12547d2ce03SVladimir Oltean
12647d2ce03SVladimir Oltean return NULL;
12747d2ce03SVladimir Oltean }
12847d2ce03SVladimir Oltean
dsa_bridge_num_find(const struct net_device * bridge_dev)12947d2ce03SVladimir Oltean static int dsa_bridge_num_find(const struct net_device *bridge_dev)
13047d2ce03SVladimir Oltean {
13147d2ce03SVladimir Oltean struct dsa_switch_tree *dst;
13247d2ce03SVladimir Oltean
13347d2ce03SVladimir Oltean list_for_each_entry(dst, &dsa_tree_list, list) {
13447d2ce03SVladimir Oltean struct dsa_bridge *bridge;
13547d2ce03SVladimir Oltean
13647d2ce03SVladimir Oltean bridge = dsa_tree_bridge_find(dst, bridge_dev);
13747d2ce03SVladimir Oltean if (bridge)
13847d2ce03SVladimir Oltean return bridge->num;
13947d2ce03SVladimir Oltean }
14047d2ce03SVladimir Oltean
14147d2ce03SVladimir Oltean return 0;
14247d2ce03SVladimir Oltean }
14347d2ce03SVladimir Oltean
dsa_bridge_num_get(const struct net_device * bridge_dev,int max)14447d2ce03SVladimir Oltean unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
14547d2ce03SVladimir Oltean {
14647d2ce03SVladimir Oltean unsigned int bridge_num = dsa_bridge_num_find(bridge_dev);
14747d2ce03SVladimir Oltean
14847d2ce03SVladimir Oltean /* Switches without FDB isolation support don't get unique
14947d2ce03SVladimir Oltean * bridge numbering
15047d2ce03SVladimir Oltean */
15147d2ce03SVladimir Oltean if (!max)
15247d2ce03SVladimir Oltean return 0;
15347d2ce03SVladimir Oltean
15447d2ce03SVladimir Oltean if (!bridge_num) {
15547d2ce03SVladimir Oltean /* First port that requests FDB isolation or TX forwarding
15647d2ce03SVladimir Oltean * offload for this bridge
15747d2ce03SVladimir Oltean */
15847d2ce03SVladimir Oltean bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges,
15947d2ce03SVladimir Oltean DSA_MAX_NUM_OFFLOADING_BRIDGES,
16047d2ce03SVladimir Oltean 1);
16147d2ce03SVladimir Oltean if (bridge_num >= max)
16247d2ce03SVladimir Oltean return 0;
16347d2ce03SVladimir Oltean
16447d2ce03SVladimir Oltean set_bit(bridge_num, &dsa_fwd_offloading_bridges);
16547d2ce03SVladimir Oltean }
16647d2ce03SVladimir Oltean
16747d2ce03SVladimir Oltean return bridge_num;
16847d2ce03SVladimir Oltean }
16947d2ce03SVladimir Oltean
dsa_bridge_num_put(const struct net_device * bridge_dev,unsigned int bridge_num)17047d2ce03SVladimir Oltean void dsa_bridge_num_put(const struct net_device *bridge_dev,
17147d2ce03SVladimir Oltean unsigned int bridge_num)
17247d2ce03SVladimir Oltean {
17347d2ce03SVladimir Oltean /* Since we refcount bridges, we know that when we call this function
17447d2ce03SVladimir Oltean * it is no longer in use, so we can just go ahead and remove it from
17547d2ce03SVladimir Oltean * the bit mask.
17647d2ce03SVladimir Oltean */
17747d2ce03SVladimir Oltean clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
17847d2ce03SVladimir Oltean }
17947d2ce03SVladimir Oltean
dsa_switch_find(int tree_index,int sw_index)18047d2ce03SVladimir Oltean struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
18147d2ce03SVladimir Oltean {
18247d2ce03SVladimir Oltean struct dsa_switch_tree *dst;
18347d2ce03SVladimir Oltean struct dsa_port *dp;
18447d2ce03SVladimir Oltean
18547d2ce03SVladimir Oltean list_for_each_entry(dst, &dsa_tree_list, list) {
18647d2ce03SVladimir Oltean if (dst->index != tree_index)
18747d2ce03SVladimir Oltean continue;
18847d2ce03SVladimir Oltean
18947d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) {
19047d2ce03SVladimir Oltean if (dp->ds->index != sw_index)
19147d2ce03SVladimir Oltean continue;
19247d2ce03SVladimir Oltean
19347d2ce03SVladimir Oltean return dp->ds;
19447d2ce03SVladimir Oltean }
19547d2ce03SVladimir Oltean }
19647d2ce03SVladimir Oltean
19747d2ce03SVladimir Oltean return NULL;
19847d2ce03SVladimir Oltean }
19947d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_switch_find);
20047d2ce03SVladimir Oltean
dsa_tree_find(int index)20147d2ce03SVladimir Oltean static struct dsa_switch_tree *dsa_tree_find(int index)
20247d2ce03SVladimir Oltean {
20347d2ce03SVladimir Oltean struct dsa_switch_tree *dst;
20447d2ce03SVladimir Oltean
20547d2ce03SVladimir Oltean list_for_each_entry(dst, &dsa_tree_list, list)
20647d2ce03SVladimir Oltean if (dst->index == index)
20747d2ce03SVladimir Oltean return dst;
20847d2ce03SVladimir Oltean
20947d2ce03SVladimir Oltean return NULL;
21047d2ce03SVladimir Oltean }
21147d2ce03SVladimir Oltean
dsa_tree_alloc(int index)21247d2ce03SVladimir Oltean static struct dsa_switch_tree *dsa_tree_alloc(int index)
21347d2ce03SVladimir Oltean {
21447d2ce03SVladimir Oltean struct dsa_switch_tree *dst;
21547d2ce03SVladimir Oltean
21647d2ce03SVladimir Oltean dst = kzalloc(sizeof(*dst), GFP_KERNEL);
21747d2ce03SVladimir Oltean if (!dst)
21847d2ce03SVladimir Oltean return NULL;
21947d2ce03SVladimir Oltean
22047d2ce03SVladimir Oltean dst->index = index;
22147d2ce03SVladimir Oltean
22247d2ce03SVladimir Oltean INIT_LIST_HEAD(&dst->rtable);
22347d2ce03SVladimir Oltean
22447d2ce03SVladimir Oltean INIT_LIST_HEAD(&dst->ports);
22547d2ce03SVladimir Oltean
22647d2ce03SVladimir Oltean INIT_LIST_HEAD(&dst->list);
22747d2ce03SVladimir Oltean list_add_tail(&dst->list, &dsa_tree_list);
22847d2ce03SVladimir Oltean
22947d2ce03SVladimir Oltean kref_init(&dst->refcount);
23047d2ce03SVladimir Oltean
23147d2ce03SVladimir Oltean return dst;
23247d2ce03SVladimir Oltean }
23347d2ce03SVladimir Oltean
dsa_tree_free(struct dsa_switch_tree * dst)23447d2ce03SVladimir Oltean static void dsa_tree_free(struct dsa_switch_tree *dst)
23547d2ce03SVladimir Oltean {
23647d2ce03SVladimir Oltean if (dst->tag_ops)
23747d2ce03SVladimir Oltean dsa_tag_driver_put(dst->tag_ops);
23847d2ce03SVladimir Oltean list_del(&dst->list);
23947d2ce03SVladimir Oltean kfree(dst);
24047d2ce03SVladimir Oltean }
24147d2ce03SVladimir Oltean
dsa_tree_get(struct dsa_switch_tree * dst)24247d2ce03SVladimir Oltean static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
24347d2ce03SVladimir Oltean {
24447d2ce03SVladimir Oltean if (dst)
24547d2ce03SVladimir Oltean kref_get(&dst->refcount);
24647d2ce03SVladimir Oltean
24747d2ce03SVladimir Oltean return dst;
24847d2ce03SVladimir Oltean }
24947d2ce03SVladimir Oltean
dsa_tree_touch(int index)25047d2ce03SVladimir Oltean static struct dsa_switch_tree *dsa_tree_touch(int index)
25147d2ce03SVladimir Oltean {
25247d2ce03SVladimir Oltean struct dsa_switch_tree *dst;
25347d2ce03SVladimir Oltean
25447d2ce03SVladimir Oltean dst = dsa_tree_find(index);
25547d2ce03SVladimir Oltean if (dst)
25647d2ce03SVladimir Oltean return dsa_tree_get(dst);
25747d2ce03SVladimir Oltean else
25847d2ce03SVladimir Oltean return dsa_tree_alloc(index);
25947d2ce03SVladimir Oltean }
26047d2ce03SVladimir Oltean
dsa_tree_release(struct kref * ref)26147d2ce03SVladimir Oltean static void dsa_tree_release(struct kref *ref)
26247d2ce03SVladimir Oltean {
26347d2ce03SVladimir Oltean struct dsa_switch_tree *dst;
26447d2ce03SVladimir Oltean
26547d2ce03SVladimir Oltean dst = container_of(ref, struct dsa_switch_tree, refcount);
26647d2ce03SVladimir Oltean
26747d2ce03SVladimir Oltean dsa_tree_free(dst);
26847d2ce03SVladimir Oltean }
26947d2ce03SVladimir Oltean
dsa_tree_put(struct dsa_switch_tree * dst)27047d2ce03SVladimir Oltean static void dsa_tree_put(struct dsa_switch_tree *dst)
27147d2ce03SVladimir Oltean {
27247d2ce03SVladimir Oltean if (dst)
27347d2ce03SVladimir Oltean kref_put(&dst->refcount, dsa_tree_release);
27447d2ce03SVladimir Oltean }
27547d2ce03SVladimir Oltean
dsa_tree_find_port_by_node(struct dsa_switch_tree * dst,struct device_node * dn)27647d2ce03SVladimir Oltean static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
27747d2ce03SVladimir Oltean struct device_node *dn)
27847d2ce03SVladimir Oltean {
27947d2ce03SVladimir Oltean struct dsa_port *dp;
28047d2ce03SVladimir Oltean
28147d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list)
28247d2ce03SVladimir Oltean if (dp->dn == dn)
28347d2ce03SVladimir Oltean return dp;
28447d2ce03SVladimir Oltean
28547d2ce03SVladimir Oltean return NULL;
28647d2ce03SVladimir Oltean }
28747d2ce03SVladimir Oltean
dsa_link_touch(struct dsa_port * dp,struct dsa_port * link_dp)28847d2ce03SVladimir Oltean static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
28947d2ce03SVladimir Oltean struct dsa_port *link_dp)
29047d2ce03SVladimir Oltean {
29147d2ce03SVladimir Oltean struct dsa_switch *ds = dp->ds;
29247d2ce03SVladimir Oltean struct dsa_switch_tree *dst;
29347d2ce03SVladimir Oltean struct dsa_link *dl;
29447d2ce03SVladimir Oltean
29547d2ce03SVladimir Oltean dst = ds->dst;
29647d2ce03SVladimir Oltean
29747d2ce03SVladimir Oltean list_for_each_entry(dl, &dst->rtable, list)
29847d2ce03SVladimir Oltean if (dl->dp == dp && dl->link_dp == link_dp)
29947d2ce03SVladimir Oltean return dl;
30047d2ce03SVladimir Oltean
30147d2ce03SVladimir Oltean dl = kzalloc(sizeof(*dl), GFP_KERNEL);
30247d2ce03SVladimir Oltean if (!dl)
30347d2ce03SVladimir Oltean return NULL;
30447d2ce03SVladimir Oltean
30547d2ce03SVladimir Oltean dl->dp = dp;
30647d2ce03SVladimir Oltean dl->link_dp = link_dp;
30747d2ce03SVladimir Oltean
30847d2ce03SVladimir Oltean INIT_LIST_HEAD(&dl->list);
30947d2ce03SVladimir Oltean list_add_tail(&dl->list, &dst->rtable);
31047d2ce03SVladimir Oltean
31147d2ce03SVladimir Oltean return dl;
31247d2ce03SVladimir Oltean }
31347d2ce03SVladimir Oltean
dsa_port_setup_routing_table(struct dsa_port * dp)31447d2ce03SVladimir Oltean static bool dsa_port_setup_routing_table(struct dsa_port *dp)
31547d2ce03SVladimir Oltean {
31647d2ce03SVladimir Oltean struct dsa_switch *ds = dp->ds;
31747d2ce03SVladimir Oltean struct dsa_switch_tree *dst = ds->dst;
31847d2ce03SVladimir Oltean struct device_node *dn = dp->dn;
31947d2ce03SVladimir Oltean struct of_phandle_iterator it;
32047d2ce03SVladimir Oltean struct dsa_port *link_dp;
32147d2ce03SVladimir Oltean struct dsa_link *dl;
32247d2ce03SVladimir Oltean int err;
32347d2ce03SVladimir Oltean
32447d2ce03SVladimir Oltean of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
32547d2ce03SVladimir Oltean link_dp = dsa_tree_find_port_by_node(dst, it.node);
32647d2ce03SVladimir Oltean if (!link_dp) {
32747d2ce03SVladimir Oltean of_node_put(it.node);
32847d2ce03SVladimir Oltean return false;
32947d2ce03SVladimir Oltean }
33047d2ce03SVladimir Oltean
33147d2ce03SVladimir Oltean dl = dsa_link_touch(dp, link_dp);
33247d2ce03SVladimir Oltean if (!dl) {
33347d2ce03SVladimir Oltean of_node_put(it.node);
33447d2ce03SVladimir Oltean return false;
33547d2ce03SVladimir Oltean }
33647d2ce03SVladimir Oltean }
33747d2ce03SVladimir Oltean
33847d2ce03SVladimir Oltean return true;
33947d2ce03SVladimir Oltean }
34047d2ce03SVladimir Oltean
dsa_tree_setup_routing_table(struct dsa_switch_tree * dst)34147d2ce03SVladimir Oltean static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
34247d2ce03SVladimir Oltean {
34347d2ce03SVladimir Oltean bool complete = true;
34447d2ce03SVladimir Oltean struct dsa_port *dp;
34547d2ce03SVladimir Oltean
34647d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) {
34747d2ce03SVladimir Oltean if (dsa_port_is_dsa(dp)) {
34847d2ce03SVladimir Oltean complete = dsa_port_setup_routing_table(dp);
34947d2ce03SVladimir Oltean if (!complete)
35047d2ce03SVladimir Oltean break;
35147d2ce03SVladimir Oltean }
35247d2ce03SVladimir Oltean }
35347d2ce03SVladimir Oltean
35447d2ce03SVladimir Oltean return complete;
35547d2ce03SVladimir Oltean }
35647d2ce03SVladimir Oltean
dsa_tree_find_first_cpu(struct dsa_switch_tree * dst)35747d2ce03SVladimir Oltean static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
35847d2ce03SVladimir Oltean {
35947d2ce03SVladimir Oltean struct dsa_port *dp;
36047d2ce03SVladimir Oltean
36147d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list)
36247d2ce03SVladimir Oltean if (dsa_port_is_cpu(dp))
36347d2ce03SVladimir Oltean return dp;
36447d2ce03SVladimir Oltean
36547d2ce03SVladimir Oltean return NULL;
36647d2ce03SVladimir Oltean }
36747d2ce03SVladimir Oltean
dsa_tree_find_first_master(struct dsa_switch_tree * dst)36847d2ce03SVladimir Oltean struct net_device *dsa_tree_find_first_master(struct dsa_switch_tree *dst)
36947d2ce03SVladimir Oltean {
37047d2ce03SVladimir Oltean struct device_node *ethernet;
37147d2ce03SVladimir Oltean struct net_device *master;
37247d2ce03SVladimir Oltean struct dsa_port *cpu_dp;
37347d2ce03SVladimir Oltean
37447d2ce03SVladimir Oltean cpu_dp = dsa_tree_find_first_cpu(dst);
37547d2ce03SVladimir Oltean ethernet = of_parse_phandle(cpu_dp->dn, "ethernet", 0);
37647d2ce03SVladimir Oltean master = of_find_net_device_by_node(ethernet);
37747d2ce03SVladimir Oltean of_node_put(ethernet);
37847d2ce03SVladimir Oltean
37947d2ce03SVladimir Oltean return master;
38047d2ce03SVladimir Oltean }
38147d2ce03SVladimir Oltean
38247d2ce03SVladimir Oltean /* Assign the default CPU port (the first one in the tree) to all ports of the
38347d2ce03SVladimir Oltean * fabric which don't already have one as part of their own switch.
38447d2ce03SVladimir Oltean */
dsa_tree_setup_default_cpu(struct dsa_switch_tree * dst)38547d2ce03SVladimir Oltean static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
38647d2ce03SVladimir Oltean {
38747d2ce03SVladimir Oltean struct dsa_port *cpu_dp, *dp;
38847d2ce03SVladimir Oltean
38947d2ce03SVladimir Oltean cpu_dp = dsa_tree_find_first_cpu(dst);
39047d2ce03SVladimir Oltean if (!cpu_dp) {
39147d2ce03SVladimir Oltean pr_err("DSA: tree %d has no CPU port\n", dst->index);
39247d2ce03SVladimir Oltean return -EINVAL;
39347d2ce03SVladimir Oltean }
39447d2ce03SVladimir Oltean
39547d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) {
39647d2ce03SVladimir Oltean if (dp->cpu_dp)
39747d2ce03SVladimir Oltean continue;
39847d2ce03SVladimir Oltean
39947d2ce03SVladimir Oltean if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
40047d2ce03SVladimir Oltean dp->cpu_dp = cpu_dp;
40147d2ce03SVladimir Oltean }
40247d2ce03SVladimir Oltean
40347d2ce03SVladimir Oltean return 0;
40447d2ce03SVladimir Oltean }
40547d2ce03SVladimir Oltean
406*b79d7c14SVladimir Oltean static struct dsa_port *
dsa_switch_preferred_default_local_cpu_port(struct dsa_switch * ds)407*b79d7c14SVladimir Oltean dsa_switch_preferred_default_local_cpu_port(struct dsa_switch *ds)
408*b79d7c14SVladimir Oltean {
409*b79d7c14SVladimir Oltean struct dsa_port *cpu_dp;
410*b79d7c14SVladimir Oltean
411*b79d7c14SVladimir Oltean if (!ds->ops->preferred_default_local_cpu_port)
412*b79d7c14SVladimir Oltean return NULL;
413*b79d7c14SVladimir Oltean
414*b79d7c14SVladimir Oltean cpu_dp = ds->ops->preferred_default_local_cpu_port(ds);
415*b79d7c14SVladimir Oltean if (!cpu_dp)
416*b79d7c14SVladimir Oltean return NULL;
417*b79d7c14SVladimir Oltean
418*b79d7c14SVladimir Oltean if (WARN_ON(!dsa_port_is_cpu(cpu_dp) || cpu_dp->ds != ds))
419*b79d7c14SVladimir Oltean return NULL;
420*b79d7c14SVladimir Oltean
421*b79d7c14SVladimir Oltean return cpu_dp;
422*b79d7c14SVladimir Oltean }
423*b79d7c14SVladimir Oltean
42447d2ce03SVladimir Oltean /* Perform initial assignment of CPU ports to user ports and DSA links in the
42547d2ce03SVladimir Oltean * fabric, giving preference to CPU ports local to each switch. Default to
42647d2ce03SVladimir Oltean * using the first CPU port in the switch tree if the port does not have a CPU
42747d2ce03SVladimir Oltean * port local to this switch.
42847d2ce03SVladimir Oltean */
dsa_tree_setup_cpu_ports(struct dsa_switch_tree * dst)42947d2ce03SVladimir Oltean static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
43047d2ce03SVladimir Oltean {
431*b79d7c14SVladimir Oltean struct dsa_port *preferred_cpu_dp, *cpu_dp, *dp;
43247d2ce03SVladimir Oltean
43347d2ce03SVladimir Oltean list_for_each_entry(cpu_dp, &dst->ports, list) {
43447d2ce03SVladimir Oltean if (!dsa_port_is_cpu(cpu_dp))
43547d2ce03SVladimir Oltean continue;
43647d2ce03SVladimir Oltean
437*b79d7c14SVladimir Oltean preferred_cpu_dp = dsa_switch_preferred_default_local_cpu_port(cpu_dp->ds);
438*b79d7c14SVladimir Oltean if (preferred_cpu_dp && preferred_cpu_dp != cpu_dp)
439*b79d7c14SVladimir Oltean continue;
440*b79d7c14SVladimir Oltean
44147d2ce03SVladimir Oltean /* Prefer a local CPU port */
44247d2ce03SVladimir Oltean dsa_switch_for_each_port(dp, cpu_dp->ds) {
44347d2ce03SVladimir Oltean /* Prefer the first local CPU port found */
44447d2ce03SVladimir Oltean if (dp->cpu_dp)
44547d2ce03SVladimir Oltean continue;
44647d2ce03SVladimir Oltean
44747d2ce03SVladimir Oltean if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
44847d2ce03SVladimir Oltean dp->cpu_dp = cpu_dp;
44947d2ce03SVladimir Oltean }
45047d2ce03SVladimir Oltean }
45147d2ce03SVladimir Oltean
45247d2ce03SVladimir Oltean return dsa_tree_setup_default_cpu(dst);
45347d2ce03SVladimir Oltean }
45447d2ce03SVladimir Oltean
dsa_tree_teardown_cpu_ports(struct dsa_switch_tree * dst)45547d2ce03SVladimir Oltean static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
45647d2ce03SVladimir Oltean {
45747d2ce03SVladimir Oltean struct dsa_port *dp;
45847d2ce03SVladimir Oltean
45947d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list)
46047d2ce03SVladimir Oltean if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
46147d2ce03SVladimir Oltean dp->cpu_dp = NULL;
46247d2ce03SVladimir Oltean }
46347d2ce03SVladimir Oltean
dsa_port_setup(struct dsa_port * dp)46447d2ce03SVladimir Oltean static int dsa_port_setup(struct dsa_port *dp)
46547d2ce03SVladimir Oltean {
46647d2ce03SVladimir Oltean bool dsa_port_link_registered = false;
46747d2ce03SVladimir Oltean struct dsa_switch *ds = dp->ds;
46847d2ce03SVladimir Oltean bool dsa_port_enabled = false;
46947d2ce03SVladimir Oltean int err = 0;
47047d2ce03SVladimir Oltean
47147d2ce03SVladimir Oltean if (dp->setup)
47247d2ce03SVladimir Oltean return 0;
47347d2ce03SVladimir Oltean
47447d2ce03SVladimir Oltean err = dsa_port_devlink_setup(dp);
47547d2ce03SVladimir Oltean if (err)
47647d2ce03SVladimir Oltean return err;
47747d2ce03SVladimir Oltean
47847d2ce03SVladimir Oltean switch (dp->type) {
47947d2ce03SVladimir Oltean case DSA_PORT_TYPE_UNUSED:
48047d2ce03SVladimir Oltean dsa_port_disable(dp);
48147d2ce03SVladimir Oltean break;
48247d2ce03SVladimir Oltean case DSA_PORT_TYPE_CPU:
48347d2ce03SVladimir Oltean if (dp->dn) {
48447d2ce03SVladimir Oltean err = dsa_shared_port_link_register_of(dp);
48547d2ce03SVladimir Oltean if (err)
48647d2ce03SVladimir Oltean break;
48747d2ce03SVladimir Oltean dsa_port_link_registered = true;
48847d2ce03SVladimir Oltean } else {
48947d2ce03SVladimir Oltean dev_warn(ds->dev,
49047d2ce03SVladimir Oltean "skipping link registration for CPU port %d\n",
49147d2ce03SVladimir Oltean dp->index);
49247d2ce03SVladimir Oltean }
49347d2ce03SVladimir Oltean
49447d2ce03SVladimir Oltean err = dsa_port_enable(dp, NULL);
49547d2ce03SVladimir Oltean if (err)
49647d2ce03SVladimir Oltean break;
49747d2ce03SVladimir Oltean dsa_port_enabled = true;
49847d2ce03SVladimir Oltean
49947d2ce03SVladimir Oltean break;
50047d2ce03SVladimir Oltean case DSA_PORT_TYPE_DSA:
50147d2ce03SVladimir Oltean if (dp->dn) {
50247d2ce03SVladimir Oltean err = dsa_shared_port_link_register_of(dp);
50347d2ce03SVladimir Oltean if (err)
50447d2ce03SVladimir Oltean break;
50547d2ce03SVladimir Oltean dsa_port_link_registered = true;
50647d2ce03SVladimir Oltean } else {
50747d2ce03SVladimir Oltean dev_warn(ds->dev,
50847d2ce03SVladimir Oltean "skipping link registration for DSA port %d\n",
50947d2ce03SVladimir Oltean dp->index);
51047d2ce03SVladimir Oltean }
51147d2ce03SVladimir Oltean
51247d2ce03SVladimir Oltean err = dsa_port_enable(dp, NULL);
51347d2ce03SVladimir Oltean if (err)
51447d2ce03SVladimir Oltean break;
51547d2ce03SVladimir Oltean dsa_port_enabled = true;
51647d2ce03SVladimir Oltean
51747d2ce03SVladimir Oltean break;
51847d2ce03SVladimir Oltean case DSA_PORT_TYPE_USER:
51947d2ce03SVladimir Oltean of_get_mac_address(dp->dn, dp->mac);
52047d2ce03SVladimir Oltean err = dsa_slave_create(dp);
52147d2ce03SVladimir Oltean break;
52247d2ce03SVladimir Oltean }
52347d2ce03SVladimir Oltean
52447d2ce03SVladimir Oltean if (err && dsa_port_enabled)
52547d2ce03SVladimir Oltean dsa_port_disable(dp);
52647d2ce03SVladimir Oltean if (err && dsa_port_link_registered)
52747d2ce03SVladimir Oltean dsa_shared_port_link_unregister_of(dp);
52847d2ce03SVladimir Oltean if (err) {
52947d2ce03SVladimir Oltean dsa_port_devlink_teardown(dp);
53047d2ce03SVladimir Oltean return err;
53147d2ce03SVladimir Oltean }
53247d2ce03SVladimir Oltean
53347d2ce03SVladimir Oltean dp->setup = true;
53447d2ce03SVladimir Oltean
53547d2ce03SVladimir Oltean return 0;
53647d2ce03SVladimir Oltean }
53747d2ce03SVladimir Oltean
dsa_port_teardown(struct dsa_port * dp)53847d2ce03SVladimir Oltean static void dsa_port_teardown(struct dsa_port *dp)
53947d2ce03SVladimir Oltean {
54047d2ce03SVladimir Oltean if (!dp->setup)
54147d2ce03SVladimir Oltean return;
54247d2ce03SVladimir Oltean
54347d2ce03SVladimir Oltean switch (dp->type) {
54447d2ce03SVladimir Oltean case DSA_PORT_TYPE_UNUSED:
54547d2ce03SVladimir Oltean break;
54647d2ce03SVladimir Oltean case DSA_PORT_TYPE_CPU:
54747d2ce03SVladimir Oltean dsa_port_disable(dp);
54847d2ce03SVladimir Oltean if (dp->dn)
54947d2ce03SVladimir Oltean dsa_shared_port_link_unregister_of(dp);
55047d2ce03SVladimir Oltean break;
55147d2ce03SVladimir Oltean case DSA_PORT_TYPE_DSA:
55247d2ce03SVladimir Oltean dsa_port_disable(dp);
55347d2ce03SVladimir Oltean if (dp->dn)
55447d2ce03SVladimir Oltean dsa_shared_port_link_unregister_of(dp);
55547d2ce03SVladimir Oltean break;
55647d2ce03SVladimir Oltean case DSA_PORT_TYPE_USER:
55747d2ce03SVladimir Oltean if (dp->slave) {
55847d2ce03SVladimir Oltean dsa_slave_destroy(dp->slave);
55947d2ce03SVladimir Oltean dp->slave = NULL;
56047d2ce03SVladimir Oltean }
56147d2ce03SVladimir Oltean break;
56247d2ce03SVladimir Oltean }
56347d2ce03SVladimir Oltean
56447d2ce03SVladimir Oltean dsa_port_devlink_teardown(dp);
56547d2ce03SVladimir Oltean
56647d2ce03SVladimir Oltean dp->setup = false;
56747d2ce03SVladimir Oltean }
56847d2ce03SVladimir Oltean
dsa_port_setup_as_unused(struct dsa_port * dp)56947d2ce03SVladimir Oltean static int dsa_port_setup_as_unused(struct dsa_port *dp)
57047d2ce03SVladimir Oltean {
57147d2ce03SVladimir Oltean dp->type = DSA_PORT_TYPE_UNUSED;
57247d2ce03SVladimir Oltean return dsa_port_setup(dp);
57347d2ce03SVladimir Oltean }
57447d2ce03SVladimir Oltean
dsa_switch_setup_tag_protocol(struct dsa_switch * ds)57547d2ce03SVladimir Oltean static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
57647d2ce03SVladimir Oltean {
57747d2ce03SVladimir Oltean const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
57847d2ce03SVladimir Oltean struct dsa_switch_tree *dst = ds->dst;
57947d2ce03SVladimir Oltean int err;
58047d2ce03SVladimir Oltean
58147d2ce03SVladimir Oltean if (tag_ops->proto == dst->default_proto)
58247d2ce03SVladimir Oltean goto connect;
58347d2ce03SVladimir Oltean
58447d2ce03SVladimir Oltean rtnl_lock();
58547d2ce03SVladimir Oltean err = ds->ops->change_tag_protocol(ds, tag_ops->proto);
58647d2ce03SVladimir Oltean rtnl_unlock();
58747d2ce03SVladimir Oltean if (err) {
58847d2ce03SVladimir Oltean dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
58947d2ce03SVladimir Oltean tag_ops->name, ERR_PTR(err));
59047d2ce03SVladimir Oltean return err;
59147d2ce03SVladimir Oltean }
59247d2ce03SVladimir Oltean
59347d2ce03SVladimir Oltean connect:
59447d2ce03SVladimir Oltean if (tag_ops->connect) {
59547d2ce03SVladimir Oltean err = tag_ops->connect(ds);
59647d2ce03SVladimir Oltean if (err)
59747d2ce03SVladimir Oltean return err;
59847d2ce03SVladimir Oltean }
59947d2ce03SVladimir Oltean
60047d2ce03SVladimir Oltean if (ds->ops->connect_tag_protocol) {
60147d2ce03SVladimir Oltean err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
60247d2ce03SVladimir Oltean if (err) {
60347d2ce03SVladimir Oltean dev_err(ds->dev,
60447d2ce03SVladimir Oltean "Unable to connect to tag protocol \"%s\": %pe\n",
60547d2ce03SVladimir Oltean tag_ops->name, ERR_PTR(err));
60647d2ce03SVladimir Oltean goto disconnect;
60747d2ce03SVladimir Oltean }
60847d2ce03SVladimir Oltean }
60947d2ce03SVladimir Oltean
61047d2ce03SVladimir Oltean return 0;
61147d2ce03SVladimir Oltean
61247d2ce03SVladimir Oltean disconnect:
61347d2ce03SVladimir Oltean if (tag_ops->disconnect)
61447d2ce03SVladimir Oltean tag_ops->disconnect(ds);
61547d2ce03SVladimir Oltean
61647d2ce03SVladimir Oltean return err;
61747d2ce03SVladimir Oltean }
61847d2ce03SVladimir Oltean
dsa_switch_teardown_tag_protocol(struct dsa_switch * ds)61947d2ce03SVladimir Oltean static void dsa_switch_teardown_tag_protocol(struct dsa_switch *ds)
62047d2ce03SVladimir Oltean {
62147d2ce03SVladimir Oltean const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
62247d2ce03SVladimir Oltean
62347d2ce03SVladimir Oltean if (tag_ops->disconnect)
62447d2ce03SVladimir Oltean tag_ops->disconnect(ds);
62547d2ce03SVladimir Oltean }
62647d2ce03SVladimir Oltean
dsa_switch_setup(struct dsa_switch * ds)62747d2ce03SVladimir Oltean static int dsa_switch_setup(struct dsa_switch *ds)
62847d2ce03SVladimir Oltean {
62947d2ce03SVladimir Oltean struct device_node *dn;
63047d2ce03SVladimir Oltean int err;
63147d2ce03SVladimir Oltean
63247d2ce03SVladimir Oltean if (ds->setup)
63347d2ce03SVladimir Oltean return 0;
63447d2ce03SVladimir Oltean
63547d2ce03SVladimir Oltean /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
63647d2ce03SVladimir Oltean * driver and before ops->setup() has run, since the switch drivers and
63747d2ce03SVladimir Oltean * the slave MDIO bus driver rely on these values for probing PHY
63847d2ce03SVladimir Oltean * devices or not
63947d2ce03SVladimir Oltean */
64047d2ce03SVladimir Oltean ds->phys_mii_mask |= dsa_user_ports(ds);
64147d2ce03SVladimir Oltean
64247d2ce03SVladimir Oltean err = dsa_switch_devlink_alloc(ds);
64347d2ce03SVladimir Oltean if (err)
64447d2ce03SVladimir Oltean return err;
64547d2ce03SVladimir Oltean
64647d2ce03SVladimir Oltean err = dsa_switch_register_notifier(ds);
64747d2ce03SVladimir Oltean if (err)
64847d2ce03SVladimir Oltean goto devlink_free;
64947d2ce03SVladimir Oltean
65047d2ce03SVladimir Oltean ds->configure_vlan_while_not_filtering = true;
65147d2ce03SVladimir Oltean
65247d2ce03SVladimir Oltean err = ds->ops->setup(ds);
65347d2ce03SVladimir Oltean if (err < 0)
65447d2ce03SVladimir Oltean goto unregister_notifier;
65547d2ce03SVladimir Oltean
65647d2ce03SVladimir Oltean err = dsa_switch_setup_tag_protocol(ds);
65747d2ce03SVladimir Oltean if (err)
65847d2ce03SVladimir Oltean goto teardown;
65947d2ce03SVladimir Oltean
66047d2ce03SVladimir Oltean if (!ds->slave_mii_bus && ds->ops->phy_read) {
66147d2ce03SVladimir Oltean ds->slave_mii_bus = mdiobus_alloc();
66247d2ce03SVladimir Oltean if (!ds->slave_mii_bus) {
66347d2ce03SVladimir Oltean err = -ENOMEM;
66447d2ce03SVladimir Oltean goto teardown;
66547d2ce03SVladimir Oltean }
66647d2ce03SVladimir Oltean
66747d2ce03SVladimir Oltean dsa_slave_mii_bus_init(ds);
66847d2ce03SVladimir Oltean
66947d2ce03SVladimir Oltean dn = of_get_child_by_name(ds->dev->of_node, "mdio");
67047d2ce03SVladimir Oltean
67147d2ce03SVladimir Oltean err = of_mdiobus_register(ds->slave_mii_bus, dn);
67247d2ce03SVladimir Oltean of_node_put(dn);
67347d2ce03SVladimir Oltean if (err < 0)
67447d2ce03SVladimir Oltean goto free_slave_mii_bus;
67547d2ce03SVladimir Oltean }
67647d2ce03SVladimir Oltean
67747d2ce03SVladimir Oltean dsa_switch_devlink_register(ds);
67847d2ce03SVladimir Oltean
67947d2ce03SVladimir Oltean ds->setup = true;
68047d2ce03SVladimir Oltean return 0;
68147d2ce03SVladimir Oltean
68247d2ce03SVladimir Oltean free_slave_mii_bus:
68347d2ce03SVladimir Oltean if (ds->slave_mii_bus && ds->ops->phy_read)
68447d2ce03SVladimir Oltean mdiobus_free(ds->slave_mii_bus);
68547d2ce03SVladimir Oltean teardown:
68647d2ce03SVladimir Oltean if (ds->ops->teardown)
68747d2ce03SVladimir Oltean ds->ops->teardown(ds);
68847d2ce03SVladimir Oltean unregister_notifier:
68947d2ce03SVladimir Oltean dsa_switch_unregister_notifier(ds);
69047d2ce03SVladimir Oltean devlink_free:
69147d2ce03SVladimir Oltean dsa_switch_devlink_free(ds);
69247d2ce03SVladimir Oltean return err;
69347d2ce03SVladimir Oltean }
69447d2ce03SVladimir Oltean
dsa_switch_teardown(struct dsa_switch * ds)69547d2ce03SVladimir Oltean static void dsa_switch_teardown(struct dsa_switch *ds)
69647d2ce03SVladimir Oltean {
69747d2ce03SVladimir Oltean if (!ds->setup)
69847d2ce03SVladimir Oltean return;
69947d2ce03SVladimir Oltean
70047d2ce03SVladimir Oltean dsa_switch_devlink_unregister(ds);
70147d2ce03SVladimir Oltean
70247d2ce03SVladimir Oltean if (ds->slave_mii_bus && ds->ops->phy_read) {
70347d2ce03SVladimir Oltean mdiobus_unregister(ds->slave_mii_bus);
70447d2ce03SVladimir Oltean mdiobus_free(ds->slave_mii_bus);
70547d2ce03SVladimir Oltean ds->slave_mii_bus = NULL;
70647d2ce03SVladimir Oltean }
70747d2ce03SVladimir Oltean
70847d2ce03SVladimir Oltean dsa_switch_teardown_tag_protocol(ds);
70947d2ce03SVladimir Oltean
71047d2ce03SVladimir Oltean if (ds->ops->teardown)
71147d2ce03SVladimir Oltean ds->ops->teardown(ds);
71247d2ce03SVladimir Oltean
71347d2ce03SVladimir Oltean dsa_switch_unregister_notifier(ds);
71447d2ce03SVladimir Oltean
71547d2ce03SVladimir Oltean dsa_switch_devlink_free(ds);
71647d2ce03SVladimir Oltean
71747d2ce03SVladimir Oltean ds->setup = false;
71847d2ce03SVladimir Oltean }
71947d2ce03SVladimir Oltean
72047d2ce03SVladimir Oltean /* First tear down the non-shared, then the shared ports. This ensures that
72147d2ce03SVladimir Oltean * all work items scheduled by our switchdev handlers for user ports have
72247d2ce03SVladimir Oltean * completed before we destroy the refcounting kept on the shared ports.
72347d2ce03SVladimir Oltean */
dsa_tree_teardown_ports(struct dsa_switch_tree * dst)72447d2ce03SVladimir Oltean static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
72547d2ce03SVladimir Oltean {
72647d2ce03SVladimir Oltean struct dsa_port *dp;
72747d2ce03SVladimir Oltean
72847d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list)
72947d2ce03SVladimir Oltean if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
73047d2ce03SVladimir Oltean dsa_port_teardown(dp);
73147d2ce03SVladimir Oltean
73247d2ce03SVladimir Oltean dsa_flush_workqueue();
73347d2ce03SVladimir Oltean
73447d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list)
73547d2ce03SVladimir Oltean if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
73647d2ce03SVladimir Oltean dsa_port_teardown(dp);
73747d2ce03SVladimir Oltean }
73847d2ce03SVladimir Oltean
dsa_tree_teardown_switches(struct dsa_switch_tree * dst)73947d2ce03SVladimir Oltean static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
74047d2ce03SVladimir Oltean {
74147d2ce03SVladimir Oltean struct dsa_port *dp;
74247d2ce03SVladimir Oltean
74347d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list)
74447d2ce03SVladimir Oltean dsa_switch_teardown(dp->ds);
74547d2ce03SVladimir Oltean }
74647d2ce03SVladimir Oltean
74747d2ce03SVladimir Oltean /* Bring shared ports up first, then non-shared ports */
dsa_tree_setup_ports(struct dsa_switch_tree * dst)74847d2ce03SVladimir Oltean static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
74947d2ce03SVladimir Oltean {
75047d2ce03SVladimir Oltean struct dsa_port *dp;
75147d2ce03SVladimir Oltean int err = 0;
75247d2ce03SVladimir Oltean
75347d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) {
75447d2ce03SVladimir Oltean if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
75547d2ce03SVladimir Oltean err = dsa_port_setup(dp);
75647d2ce03SVladimir Oltean if (err)
75747d2ce03SVladimir Oltean goto teardown;
75847d2ce03SVladimir Oltean }
75947d2ce03SVladimir Oltean }
76047d2ce03SVladimir Oltean
76147d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) {
76247d2ce03SVladimir Oltean if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
76347d2ce03SVladimir Oltean err = dsa_port_setup(dp);
76447d2ce03SVladimir Oltean if (err) {
76547d2ce03SVladimir Oltean err = dsa_port_setup_as_unused(dp);
76647d2ce03SVladimir Oltean if (err)
76747d2ce03SVladimir Oltean goto teardown;
76847d2ce03SVladimir Oltean }
76947d2ce03SVladimir Oltean }
77047d2ce03SVladimir Oltean }
77147d2ce03SVladimir Oltean
77247d2ce03SVladimir Oltean return 0;
77347d2ce03SVladimir Oltean
77447d2ce03SVladimir Oltean teardown:
77547d2ce03SVladimir Oltean dsa_tree_teardown_ports(dst);
77647d2ce03SVladimir Oltean
77747d2ce03SVladimir Oltean return err;
77847d2ce03SVladimir Oltean }
77947d2ce03SVladimir Oltean
dsa_tree_setup_switches(struct dsa_switch_tree * dst)78047d2ce03SVladimir Oltean static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
78147d2ce03SVladimir Oltean {
78247d2ce03SVladimir Oltean struct dsa_port *dp;
78347d2ce03SVladimir Oltean int err = 0;
78447d2ce03SVladimir Oltean
78547d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) {
78647d2ce03SVladimir Oltean err = dsa_switch_setup(dp->ds);
78747d2ce03SVladimir Oltean if (err) {
78847d2ce03SVladimir Oltean dsa_tree_teardown_switches(dst);
78947d2ce03SVladimir Oltean break;
79047d2ce03SVladimir Oltean }
79147d2ce03SVladimir Oltean }
79247d2ce03SVladimir Oltean
79347d2ce03SVladimir Oltean return err;
79447d2ce03SVladimir Oltean }
79547d2ce03SVladimir Oltean
dsa_tree_setup_master(struct dsa_switch_tree * dst)79647d2ce03SVladimir Oltean static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
79747d2ce03SVladimir Oltean {
79847d2ce03SVladimir Oltean struct dsa_port *cpu_dp;
79947d2ce03SVladimir Oltean int err = 0;
80047d2ce03SVladimir Oltean
80147d2ce03SVladimir Oltean rtnl_lock();
80247d2ce03SVladimir Oltean
80347d2ce03SVladimir Oltean dsa_tree_for_each_cpu_port(cpu_dp, dst) {
80447d2ce03SVladimir Oltean struct net_device *master = cpu_dp->master;
80547d2ce03SVladimir Oltean bool admin_up = (master->flags & IFF_UP) &&
80647d2ce03SVladimir Oltean !qdisc_tx_is_noop(master);
80747d2ce03SVladimir Oltean
80847d2ce03SVladimir Oltean err = dsa_master_setup(master, cpu_dp);
80947d2ce03SVladimir Oltean if (err)
81047d2ce03SVladimir Oltean break;
81147d2ce03SVladimir Oltean
81247d2ce03SVladimir Oltean /* Replay master state event */
81347d2ce03SVladimir Oltean dsa_tree_master_admin_state_change(dst, master, admin_up);
81447d2ce03SVladimir Oltean dsa_tree_master_oper_state_change(dst, master,
81547d2ce03SVladimir Oltean netif_oper_up(master));
81647d2ce03SVladimir Oltean }
81747d2ce03SVladimir Oltean
81847d2ce03SVladimir Oltean rtnl_unlock();
81947d2ce03SVladimir Oltean
82047d2ce03SVladimir Oltean return err;
82147d2ce03SVladimir Oltean }
82247d2ce03SVladimir Oltean
dsa_tree_teardown_master(struct dsa_switch_tree * dst)82347d2ce03SVladimir Oltean static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
82447d2ce03SVladimir Oltean {
82547d2ce03SVladimir Oltean struct dsa_port *cpu_dp;
82647d2ce03SVladimir Oltean
82747d2ce03SVladimir Oltean rtnl_lock();
82847d2ce03SVladimir Oltean
82947d2ce03SVladimir Oltean dsa_tree_for_each_cpu_port(cpu_dp, dst) {
83047d2ce03SVladimir Oltean struct net_device *master = cpu_dp->master;
83147d2ce03SVladimir Oltean
83247d2ce03SVladimir Oltean /* Synthesizing an "admin down" state is sufficient for
83347d2ce03SVladimir Oltean * the switches to get a notification if the master is
83447d2ce03SVladimir Oltean * currently up and running.
83547d2ce03SVladimir Oltean */
83647d2ce03SVladimir Oltean dsa_tree_master_admin_state_change(dst, master, false);
83747d2ce03SVladimir Oltean
83847d2ce03SVladimir Oltean dsa_master_teardown(master);
83947d2ce03SVladimir Oltean }
84047d2ce03SVladimir Oltean
84147d2ce03SVladimir Oltean rtnl_unlock();
84247d2ce03SVladimir Oltean }
84347d2ce03SVladimir Oltean
dsa_tree_setup_lags(struct dsa_switch_tree * dst)84447d2ce03SVladimir Oltean static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
84547d2ce03SVladimir Oltean {
84647d2ce03SVladimir Oltean unsigned int len = 0;
84747d2ce03SVladimir Oltean struct dsa_port *dp;
84847d2ce03SVladimir Oltean
84947d2ce03SVladimir Oltean list_for_each_entry(dp, &dst->ports, list) {
85047d2ce03SVladimir Oltean if (dp->ds->num_lag_ids > len)
85147d2ce03SVladimir Oltean len = dp->ds->num_lag_ids;
85247d2ce03SVladimir Oltean }
85347d2ce03SVladimir Oltean
85447d2ce03SVladimir Oltean if (!len)
85547d2ce03SVladimir Oltean return 0;
85647d2ce03SVladimir Oltean
85747d2ce03SVladimir Oltean dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
85847d2ce03SVladimir Oltean if (!dst->lags)
85947d2ce03SVladimir Oltean return -ENOMEM;
86047d2ce03SVladimir Oltean
86147d2ce03SVladimir Oltean dst->lags_len = len;
86247d2ce03SVladimir Oltean return 0;
86347d2ce03SVladimir Oltean }
86447d2ce03SVladimir Oltean
dsa_tree_teardown_lags(struct dsa_switch_tree * dst)86547d2ce03SVladimir Oltean static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
86647d2ce03SVladimir Oltean {
86747d2ce03SVladimir Oltean kfree(dst->lags);
86847d2ce03SVladimir Oltean }
86947d2ce03SVladimir Oltean
dsa_tree_setup(struct dsa_switch_tree * dst)87047d2ce03SVladimir Oltean static int dsa_tree_setup(struct dsa_switch_tree *dst)
87147d2ce03SVladimir Oltean {
87247d2ce03SVladimir Oltean bool complete;
87347d2ce03SVladimir Oltean int err;
87447d2ce03SVladimir Oltean
87547d2ce03SVladimir Oltean if (dst->setup) {
87647d2ce03SVladimir Oltean pr_err("DSA: tree %d already setup! Disjoint trees?\n",
87747d2ce03SVladimir Oltean dst->index);
87847d2ce03SVladimir Oltean return -EEXIST;
87947d2ce03SVladimir Oltean }
88047d2ce03SVladimir Oltean
88147d2ce03SVladimir Oltean complete = dsa_tree_setup_routing_table(dst);
88247d2ce03SVladimir Oltean if (!complete)
88347d2ce03SVladimir Oltean return 0;
88447d2ce03SVladimir Oltean
88547d2ce03SVladimir Oltean err = dsa_tree_setup_cpu_ports(dst);
88647d2ce03SVladimir Oltean if (err)
88747d2ce03SVladimir Oltean return err;
88847d2ce03SVladimir Oltean
88947d2ce03SVladimir Oltean err = dsa_tree_setup_switches(dst);
89047d2ce03SVladimir Oltean if (err)
89147d2ce03SVladimir Oltean goto teardown_cpu_ports;
89247d2ce03SVladimir Oltean
89347d2ce03SVladimir Oltean err = dsa_tree_setup_ports(dst);
89447d2ce03SVladimir Oltean if (err)
89547d2ce03SVladimir Oltean goto teardown_switches;
89647d2ce03SVladimir Oltean
89747d2ce03SVladimir Oltean err = dsa_tree_setup_master(dst);
89847d2ce03SVladimir Oltean if (err)
89947d2ce03SVladimir Oltean goto teardown_ports;
90047d2ce03SVladimir Oltean
90147d2ce03SVladimir Oltean err = dsa_tree_setup_lags(dst);
90247d2ce03SVladimir Oltean if (err)
90347d2ce03SVladimir Oltean goto teardown_master;
90447d2ce03SVladimir Oltean
90547d2ce03SVladimir Oltean dst->setup = true;
90647d2ce03SVladimir Oltean
90747d2ce03SVladimir Oltean pr_info("DSA: tree %d setup\n", dst->index);
90847d2ce03SVladimir Oltean
90947d2ce03SVladimir Oltean return 0;
91047d2ce03SVladimir Oltean
91147d2ce03SVladimir Oltean teardown_master:
91247d2ce03SVladimir Oltean dsa_tree_teardown_master(dst);
91347d2ce03SVladimir Oltean teardown_ports:
91447d2ce03SVladimir Oltean dsa_tree_teardown_ports(dst);
91547d2ce03SVladimir Oltean teardown_switches:
91647d2ce03SVladimir Oltean dsa_tree_teardown_switches(dst);
91747d2ce03SVladimir Oltean teardown_cpu_ports:
91847d2ce03SVladimir Oltean dsa_tree_teardown_cpu_ports(dst);
91947d2ce03SVladimir Oltean
92047d2ce03SVladimir Oltean return err;
92147d2ce03SVladimir Oltean }
92247d2ce03SVladimir Oltean
dsa_tree_teardown(struct dsa_switch_tree * dst)92347d2ce03SVladimir Oltean static void dsa_tree_teardown(struct dsa_switch_tree *dst)
92447d2ce03SVladimir Oltean {
92547d2ce03SVladimir Oltean struct dsa_link *dl, *next;
92647d2ce03SVladimir Oltean
92747d2ce03SVladimir Oltean if (!dst->setup)
92847d2ce03SVladimir Oltean return;
92947d2ce03SVladimir Oltean
93047d2ce03SVladimir Oltean dsa_tree_teardown_lags(dst);
93147d2ce03SVladimir Oltean
93247d2ce03SVladimir Oltean dsa_tree_teardown_master(dst);
93347d2ce03SVladimir Oltean
93447d2ce03SVladimir Oltean dsa_tree_teardown_ports(dst);
93547d2ce03SVladimir Oltean
93647d2ce03SVladimir Oltean dsa_tree_teardown_switches(dst);
93747d2ce03SVladimir Oltean
93847d2ce03SVladimir Oltean dsa_tree_teardown_cpu_ports(dst);
93947d2ce03SVladimir Oltean
94047d2ce03SVladimir Oltean list_for_each_entry_safe(dl, next, &dst->rtable, list) {
94147d2ce03SVladimir Oltean list_del(&dl->list);
94247d2ce03SVladimir Oltean kfree(dl);
94347d2ce03SVladimir Oltean }
94447d2ce03SVladimir Oltean
94547d2ce03SVladimir Oltean pr_info("DSA: tree %d torn down\n", dst->index);
94647d2ce03SVladimir Oltean
94747d2ce03SVladimir Oltean dst->setup = false;
94847d2ce03SVladimir Oltean }
94947d2ce03SVladimir Oltean
dsa_tree_bind_tag_proto(struct dsa_switch_tree * dst,const struct dsa_device_ops * tag_ops)95047d2ce03SVladimir Oltean static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
95147d2ce03SVladimir Oltean const struct dsa_device_ops *tag_ops)
95247d2ce03SVladimir Oltean {
95347d2ce03SVladimir Oltean const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
95447d2ce03SVladimir Oltean struct dsa_notifier_tag_proto_info info;
95547d2ce03SVladimir Oltean int err;
95647d2ce03SVladimir Oltean
95747d2ce03SVladimir Oltean dst->tag_ops = tag_ops;
95847d2ce03SVladimir Oltean
95947d2ce03SVladimir Oltean /* Notify the switches from this tree about the connection
96047d2ce03SVladimir Oltean * to the new tagger
96147d2ce03SVladimir Oltean */
96247d2ce03SVladimir Oltean info.tag_ops = tag_ops;
96347d2ce03SVladimir Oltean err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
96447d2ce03SVladimir Oltean if (err && err != -EOPNOTSUPP)
96547d2ce03SVladimir Oltean goto out_disconnect;
96647d2ce03SVladimir Oltean
96747d2ce03SVladimir Oltean /* Notify the old tagger about the disconnection from this tree */
96847d2ce03SVladimir Oltean info.tag_ops = old_tag_ops;
96947d2ce03SVladimir Oltean dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
97047d2ce03SVladimir Oltean
97147d2ce03SVladimir Oltean return 0;
97247d2ce03SVladimir Oltean
97347d2ce03SVladimir Oltean out_disconnect:
97447d2ce03SVladimir Oltean info.tag_ops = tag_ops;
97547d2ce03SVladimir Oltean dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
97647d2ce03SVladimir Oltean dst->tag_ops = old_tag_ops;
97747d2ce03SVladimir Oltean
97847d2ce03SVladimir Oltean return err;
97947d2ce03SVladimir Oltean }
98047d2ce03SVladimir Oltean
98147d2ce03SVladimir Oltean /* Since the dsa/tagging sysfs device attribute is per master, the assumption
98247d2ce03SVladimir Oltean * is that all DSA switches within a tree share the same tagger, otherwise
98347d2ce03SVladimir Oltean * they would have formed disjoint trees (different "dsa,member" values).
98447d2ce03SVladimir Oltean */
dsa_tree_change_tag_proto(struct dsa_switch_tree * dst,const struct dsa_device_ops * tag_ops,const struct dsa_device_ops * old_tag_ops)98547d2ce03SVladimir Oltean int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
98647d2ce03SVladimir Oltean const struct dsa_device_ops *tag_ops,
98747d2ce03SVladimir Oltean const struct dsa_device_ops *old_tag_ops)
98847d2ce03SVladimir Oltean {
98947d2ce03SVladimir Oltean struct dsa_notifier_tag_proto_info info;
99047d2ce03SVladimir Oltean struct dsa_port *dp;
99147d2ce03SVladimir Oltean int err = -EBUSY;
99247d2ce03SVladimir Oltean
99347d2ce03SVladimir Oltean if (!rtnl_trylock())
99447d2ce03SVladimir Oltean return restart_syscall();
99547d2ce03SVladimir Oltean
99647d2ce03SVladimir Oltean /* At the moment we don't allow changing the tag protocol under
99747d2ce03SVladimir Oltean * traffic. The rtnl_mutex also happens to serialize concurrent
99847d2ce03SVladimir Oltean * attempts to change the tagging protocol. If we ever lift the IFF_UP
99947d2ce03SVladimir Oltean * restriction, there needs to be another mutex which serializes this.
100047d2ce03SVladimir Oltean */
100147d2ce03SVladimir Oltean dsa_tree_for_each_user_port(dp, dst) {
100247d2ce03SVladimir Oltean if (dsa_port_to_master(dp)->flags & IFF_UP)
100347d2ce03SVladimir Oltean goto out_unlock;
100447d2ce03SVladimir Oltean
100547d2ce03SVladimir Oltean if (dp->slave->flags & IFF_UP)
100647d2ce03SVladimir Oltean goto out_unlock;
100747d2ce03SVladimir Oltean }
100847d2ce03SVladimir Oltean
100947d2ce03SVladimir Oltean /* Notify the tag protocol change */
101047d2ce03SVladimir Oltean info.tag_ops = tag_ops;
101147d2ce03SVladimir Oltean err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
101247d2ce03SVladimir Oltean if (err)
101347d2ce03SVladimir Oltean goto out_unwind_tagger;
101447d2ce03SVladimir Oltean
101547d2ce03SVladimir Oltean err = dsa_tree_bind_tag_proto(dst, tag_ops);
101647d2ce03SVladimir Oltean if (err)
101747d2ce03SVladimir Oltean goto out_unwind_tagger;
101847d2ce03SVladimir Oltean
101947d2ce03SVladimir Oltean rtnl_unlock();
102047d2ce03SVladimir Oltean
102147d2ce03SVladimir Oltean return 0;
102247d2ce03SVladimir Oltean
102347d2ce03SVladimir Oltean out_unwind_tagger:
102447d2ce03SVladimir Oltean info.tag_ops = old_tag_ops;
102547d2ce03SVladimir Oltean dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
102647d2ce03SVladimir Oltean out_unlock:
102747d2ce03SVladimir Oltean rtnl_unlock();
102847d2ce03SVladimir Oltean return err;
102947d2ce03SVladimir Oltean }
103047d2ce03SVladimir Oltean
dsa_tree_master_state_change(struct dsa_switch_tree * dst,struct net_device * master)103147d2ce03SVladimir Oltean static void dsa_tree_master_state_change(struct dsa_switch_tree *dst,
103247d2ce03SVladimir Oltean struct net_device *master)
103347d2ce03SVladimir Oltean {
103447d2ce03SVladimir Oltean struct dsa_notifier_master_state_info info;
103547d2ce03SVladimir Oltean struct dsa_port *cpu_dp = master->dsa_ptr;
103647d2ce03SVladimir Oltean
103747d2ce03SVladimir Oltean info.master = master;
103847d2ce03SVladimir Oltean info.operational = dsa_port_master_is_operational(cpu_dp);
103947d2ce03SVladimir Oltean
104047d2ce03SVladimir Oltean dsa_tree_notify(dst, DSA_NOTIFIER_MASTER_STATE_CHANGE, &info);
104147d2ce03SVladimir Oltean }
104247d2ce03SVladimir Oltean
dsa_tree_master_admin_state_change(struct dsa_switch_tree * dst,struct net_device * master,bool up)104347d2ce03SVladimir Oltean void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
104447d2ce03SVladimir Oltean struct net_device *master,
104547d2ce03SVladimir Oltean bool up)
104647d2ce03SVladimir Oltean {
104747d2ce03SVladimir Oltean struct dsa_port *cpu_dp = master->dsa_ptr;
104847d2ce03SVladimir Oltean bool notify = false;
104947d2ce03SVladimir Oltean
105047d2ce03SVladimir Oltean /* Don't keep track of admin state on LAG DSA masters,
105147d2ce03SVladimir Oltean * but rather just of physical DSA masters
105247d2ce03SVladimir Oltean */
105347d2ce03SVladimir Oltean if (netif_is_lag_master(master))
105447d2ce03SVladimir Oltean return;
105547d2ce03SVladimir Oltean
105647d2ce03SVladimir Oltean if ((dsa_port_master_is_operational(cpu_dp)) !=
105747d2ce03SVladimir Oltean (up && cpu_dp->master_oper_up))
105847d2ce03SVladimir Oltean notify = true;
105947d2ce03SVladimir Oltean
106047d2ce03SVladimir Oltean cpu_dp->master_admin_up = up;
106147d2ce03SVladimir Oltean
106247d2ce03SVladimir Oltean if (notify)
106347d2ce03SVladimir Oltean dsa_tree_master_state_change(dst, master);
106447d2ce03SVladimir Oltean }
106547d2ce03SVladimir Oltean
dsa_tree_master_oper_state_change(struct dsa_switch_tree * dst,struct net_device * master,bool up)106647d2ce03SVladimir Oltean void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
106747d2ce03SVladimir Oltean struct net_device *master,
106847d2ce03SVladimir Oltean bool up)
106947d2ce03SVladimir Oltean {
107047d2ce03SVladimir Oltean struct dsa_port *cpu_dp = master->dsa_ptr;
107147d2ce03SVladimir Oltean bool notify = false;
107247d2ce03SVladimir Oltean
107347d2ce03SVladimir Oltean /* Don't keep track of oper state on LAG DSA masters,
107447d2ce03SVladimir Oltean * but rather just of physical DSA masters
107547d2ce03SVladimir Oltean */
107647d2ce03SVladimir Oltean if (netif_is_lag_master(master))
107747d2ce03SVladimir Oltean return;
107847d2ce03SVladimir Oltean
107947d2ce03SVladimir Oltean if ((dsa_port_master_is_operational(cpu_dp)) !=
108047d2ce03SVladimir Oltean (cpu_dp->master_admin_up && up))
108147d2ce03SVladimir Oltean notify = true;
108247d2ce03SVladimir Oltean
108347d2ce03SVladimir Oltean cpu_dp->master_oper_up = up;
108447d2ce03SVladimir Oltean
108547d2ce03SVladimir Oltean if (notify)
108647d2ce03SVladimir Oltean dsa_tree_master_state_change(dst, master);
108747d2ce03SVladimir Oltean }
108847d2ce03SVladimir Oltean
dsa_port_touch(struct dsa_switch * ds,int index)108947d2ce03SVladimir Oltean static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
109047d2ce03SVladimir Oltean {
109147d2ce03SVladimir Oltean struct dsa_switch_tree *dst = ds->dst;
109247d2ce03SVladimir Oltean struct dsa_port *dp;
109347d2ce03SVladimir Oltean
109447d2ce03SVladimir Oltean dsa_switch_for_each_port(dp, ds)
109547d2ce03SVladimir Oltean if (dp->index == index)
109647d2ce03SVladimir Oltean return dp;
109747d2ce03SVladimir Oltean
109847d2ce03SVladimir Oltean dp = kzalloc(sizeof(*dp), GFP_KERNEL);
109947d2ce03SVladimir Oltean if (!dp)
110047d2ce03SVladimir Oltean return NULL;
110147d2ce03SVladimir Oltean
110247d2ce03SVladimir Oltean dp->ds = ds;
110347d2ce03SVladimir Oltean dp->index = index;
110447d2ce03SVladimir Oltean
110547d2ce03SVladimir Oltean mutex_init(&dp->addr_lists_lock);
110647d2ce03SVladimir Oltean mutex_init(&dp->vlans_lock);
110747d2ce03SVladimir Oltean INIT_LIST_HEAD(&dp->fdbs);
110847d2ce03SVladimir Oltean INIT_LIST_HEAD(&dp->mdbs);
110947d2ce03SVladimir Oltean INIT_LIST_HEAD(&dp->vlans); /* also initializes &dp->user_vlans */
111047d2ce03SVladimir Oltean INIT_LIST_HEAD(&dp->list);
111147d2ce03SVladimir Oltean list_add_tail(&dp->list, &dst->ports);
111247d2ce03SVladimir Oltean
111347d2ce03SVladimir Oltean return dp;
111447d2ce03SVladimir Oltean }
111547d2ce03SVladimir Oltean
dsa_port_parse_user(struct dsa_port * dp,const char * name)111647d2ce03SVladimir Oltean static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
111747d2ce03SVladimir Oltean {
111847d2ce03SVladimir Oltean dp->type = DSA_PORT_TYPE_USER;
111947d2ce03SVladimir Oltean dp->name = name;
112047d2ce03SVladimir Oltean
112147d2ce03SVladimir Oltean return 0;
112247d2ce03SVladimir Oltean }
112347d2ce03SVladimir Oltean
dsa_port_parse_dsa(struct dsa_port * dp)112447d2ce03SVladimir Oltean static int dsa_port_parse_dsa(struct dsa_port *dp)
112547d2ce03SVladimir Oltean {
112647d2ce03SVladimir Oltean dp->type = DSA_PORT_TYPE_DSA;
112747d2ce03SVladimir Oltean
112847d2ce03SVladimir Oltean return 0;
112947d2ce03SVladimir Oltean }
113047d2ce03SVladimir Oltean
dsa_get_tag_protocol(struct dsa_port * dp,struct net_device * master)113147d2ce03SVladimir Oltean static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
113247d2ce03SVladimir Oltean struct net_device *master)
113347d2ce03SVladimir Oltean {
113447d2ce03SVladimir Oltean enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
113547d2ce03SVladimir Oltean struct dsa_switch *mds, *ds = dp->ds;
113647d2ce03SVladimir Oltean unsigned int mdp_upstream;
113747d2ce03SVladimir Oltean struct dsa_port *mdp;
113847d2ce03SVladimir Oltean
113947d2ce03SVladimir Oltean /* It is possible to stack DSA switches onto one another when that
114047d2ce03SVladimir Oltean * happens the switch driver may want to know if its tagging protocol
114147d2ce03SVladimir Oltean * is going to work in such a configuration.
114247d2ce03SVladimir Oltean */
114347d2ce03SVladimir Oltean if (dsa_slave_dev_check(master)) {
114447d2ce03SVladimir Oltean mdp = dsa_slave_to_port(master);
114547d2ce03SVladimir Oltean mds = mdp->ds;
114647d2ce03SVladimir Oltean mdp_upstream = dsa_upstream_port(mds, mdp->index);
114747d2ce03SVladimir Oltean tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
114847d2ce03SVladimir Oltean DSA_TAG_PROTO_NONE);
114947d2ce03SVladimir Oltean }
115047d2ce03SVladimir Oltean
115147d2ce03SVladimir Oltean /* If the master device is not itself a DSA slave in a disjoint DSA
115247d2ce03SVladimir Oltean * tree, then return immediately.
115347d2ce03SVladimir Oltean */
115447d2ce03SVladimir Oltean return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
115547d2ce03SVladimir Oltean }
115647d2ce03SVladimir Oltean
dsa_port_parse_cpu(struct dsa_port * dp,struct net_device * master,const char * user_protocol)115747d2ce03SVladimir Oltean static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
115847d2ce03SVladimir Oltean const char *user_protocol)
115947d2ce03SVladimir Oltean {
116047d2ce03SVladimir Oltean const struct dsa_device_ops *tag_ops = NULL;
116147d2ce03SVladimir Oltean struct dsa_switch *ds = dp->ds;
116247d2ce03SVladimir Oltean struct dsa_switch_tree *dst = ds->dst;
116347d2ce03SVladimir Oltean enum dsa_tag_protocol default_proto;
116447d2ce03SVladimir Oltean
116547d2ce03SVladimir Oltean /* Find out which protocol the switch would prefer. */
116647d2ce03SVladimir Oltean default_proto = dsa_get_tag_protocol(dp, master);
116747d2ce03SVladimir Oltean if (dst->default_proto) {
116847d2ce03SVladimir Oltean if (dst->default_proto != default_proto) {
116947d2ce03SVladimir Oltean dev_err(ds->dev,
117047d2ce03SVladimir Oltean "A DSA switch tree can have only one tagging protocol\n");
117147d2ce03SVladimir Oltean return -EINVAL;
117247d2ce03SVladimir Oltean }
117347d2ce03SVladimir Oltean } else {
117447d2ce03SVladimir Oltean dst->default_proto = default_proto;
117547d2ce03SVladimir Oltean }
117647d2ce03SVladimir Oltean
117747d2ce03SVladimir Oltean /* See if the user wants to override that preference. */
117847d2ce03SVladimir Oltean if (user_protocol) {
117947d2ce03SVladimir Oltean if (!ds->ops->change_tag_protocol) {
118047d2ce03SVladimir Oltean dev_err(ds->dev, "Tag protocol cannot be modified\n");
118147d2ce03SVladimir Oltean return -EINVAL;
118247d2ce03SVladimir Oltean }
118347d2ce03SVladimir Oltean
118447d2ce03SVladimir Oltean tag_ops = dsa_tag_driver_get_by_name(user_protocol);
118547d2ce03SVladimir Oltean if (IS_ERR(tag_ops)) {
118647d2ce03SVladimir Oltean dev_warn(ds->dev,
118747d2ce03SVladimir Oltean "Failed to find a tagging driver for protocol %s, using default\n",
118847d2ce03SVladimir Oltean user_protocol);
118947d2ce03SVladimir Oltean tag_ops = NULL;
119047d2ce03SVladimir Oltean }
119147d2ce03SVladimir Oltean }
119247d2ce03SVladimir Oltean
119347d2ce03SVladimir Oltean if (!tag_ops)
119447d2ce03SVladimir Oltean tag_ops = dsa_tag_driver_get_by_id(default_proto);
119547d2ce03SVladimir Oltean
119647d2ce03SVladimir Oltean if (IS_ERR(tag_ops)) {
119747d2ce03SVladimir Oltean if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
119847d2ce03SVladimir Oltean return -EPROBE_DEFER;
119947d2ce03SVladimir Oltean
120047d2ce03SVladimir Oltean dev_warn(ds->dev, "No tagger for this switch\n");
120147d2ce03SVladimir Oltean return PTR_ERR(tag_ops);
120247d2ce03SVladimir Oltean }
120347d2ce03SVladimir Oltean
120447d2ce03SVladimir Oltean if (dst->tag_ops) {
120547d2ce03SVladimir Oltean if (dst->tag_ops != tag_ops) {
120647d2ce03SVladimir Oltean dev_err(ds->dev,
120747d2ce03SVladimir Oltean "A DSA switch tree can have only one tagging protocol\n");
120847d2ce03SVladimir Oltean
120947d2ce03SVladimir Oltean dsa_tag_driver_put(tag_ops);
121047d2ce03SVladimir Oltean return -EINVAL;
121147d2ce03SVladimir Oltean }
121247d2ce03SVladimir Oltean
121347d2ce03SVladimir Oltean /* In the case of multiple CPU ports per switch, the tagging
121447d2ce03SVladimir Oltean * protocol is still reference-counted only per switch tree.
121547d2ce03SVladimir Oltean */
121647d2ce03SVladimir Oltean dsa_tag_driver_put(tag_ops);
121747d2ce03SVladimir Oltean } else {
121847d2ce03SVladimir Oltean dst->tag_ops = tag_ops;
121947d2ce03SVladimir Oltean }
122047d2ce03SVladimir Oltean
122147d2ce03SVladimir Oltean dp->master = master;
122247d2ce03SVladimir Oltean dp->type = DSA_PORT_TYPE_CPU;
122347d2ce03SVladimir Oltean dsa_port_set_tag_protocol(dp, dst->tag_ops);
122447d2ce03SVladimir Oltean dp->dst = dst;
122547d2ce03SVladimir Oltean
122647d2ce03SVladimir Oltean /* At this point, the tree may be configured to use a different
122747d2ce03SVladimir Oltean * tagger than the one chosen by the switch driver during
122847d2ce03SVladimir Oltean * .setup, in the case when a user selects a custom protocol
122947d2ce03SVladimir Oltean * through the DT.
123047d2ce03SVladimir Oltean *
123147d2ce03SVladimir Oltean * This is resolved by syncing the driver with the tree in
123247d2ce03SVladimir Oltean * dsa_switch_setup_tag_protocol once .setup has run and the
123347d2ce03SVladimir Oltean * driver is ready to accept calls to .change_tag_protocol. If
123447d2ce03SVladimir Oltean * the driver does not support the custom protocol at that
123547d2ce03SVladimir Oltean * point, the tree is wholly rejected, thereby ensuring that the
123647d2ce03SVladimir Oltean * tree and driver are always in agreement on the protocol to
123747d2ce03SVladimir Oltean * use.
123847d2ce03SVladimir Oltean */
123947d2ce03SVladimir Oltean return 0;
124047d2ce03SVladimir Oltean }
124147d2ce03SVladimir Oltean
dsa_port_parse_of(struct dsa_port * dp,struct device_node * dn)124247d2ce03SVladimir Oltean static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
124347d2ce03SVladimir Oltean {
124447d2ce03SVladimir Oltean struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
124547d2ce03SVladimir Oltean const char *name = of_get_property(dn, "label", NULL);
124647d2ce03SVladimir Oltean bool link = of_property_read_bool(dn, "link");
124747d2ce03SVladimir Oltean
124847d2ce03SVladimir Oltean dp->dn = dn;
124947d2ce03SVladimir Oltean
125047d2ce03SVladimir Oltean if (ethernet) {
125147d2ce03SVladimir Oltean struct net_device *master;
125247d2ce03SVladimir Oltean const char *user_protocol;
125347d2ce03SVladimir Oltean
125447d2ce03SVladimir Oltean master = of_find_net_device_by_node(ethernet);
125547d2ce03SVladimir Oltean of_node_put(ethernet);
125647d2ce03SVladimir Oltean if (!master)
125747d2ce03SVladimir Oltean return -EPROBE_DEFER;
125847d2ce03SVladimir Oltean
125947d2ce03SVladimir Oltean user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
126047d2ce03SVladimir Oltean return dsa_port_parse_cpu(dp, master, user_protocol);
126147d2ce03SVladimir Oltean }
126247d2ce03SVladimir Oltean
126347d2ce03SVladimir Oltean if (link)
126447d2ce03SVladimir Oltean return dsa_port_parse_dsa(dp);
126547d2ce03SVladimir Oltean
126647d2ce03SVladimir Oltean return dsa_port_parse_user(dp, name);
126747d2ce03SVladimir Oltean }
126847d2ce03SVladimir Oltean
dsa_switch_parse_ports_of(struct dsa_switch * ds,struct device_node * dn)126947d2ce03SVladimir Oltean static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
127047d2ce03SVladimir Oltean struct device_node *dn)
127147d2ce03SVladimir Oltean {
127247d2ce03SVladimir Oltean struct device_node *ports, *port;
127347d2ce03SVladimir Oltean struct dsa_port *dp;
127447d2ce03SVladimir Oltean int err = 0;
127547d2ce03SVladimir Oltean u32 reg;
127647d2ce03SVladimir Oltean
127747d2ce03SVladimir Oltean ports = of_get_child_by_name(dn, "ports");
127847d2ce03SVladimir Oltean if (!ports) {
127947d2ce03SVladimir Oltean /* The second possibility is "ethernet-ports" */
128047d2ce03SVladimir Oltean ports = of_get_child_by_name(dn, "ethernet-ports");
128147d2ce03SVladimir Oltean if (!ports) {
128247d2ce03SVladimir Oltean dev_err(ds->dev, "no ports child node found\n");
128347d2ce03SVladimir Oltean return -EINVAL;
128447d2ce03SVladimir Oltean }
128547d2ce03SVladimir Oltean }
128647d2ce03SVladimir Oltean
128747d2ce03SVladimir Oltean for_each_available_child_of_node(ports, port) {
128847d2ce03SVladimir Oltean err = of_property_read_u32(port, "reg", ®);
128947d2ce03SVladimir Oltean if (err) {
129047d2ce03SVladimir Oltean of_node_put(port);
129147d2ce03SVladimir Oltean goto out_put_node;
129247d2ce03SVladimir Oltean }
129347d2ce03SVladimir Oltean
129447d2ce03SVladimir Oltean if (reg >= ds->num_ports) {
129547d2ce03SVladimir Oltean dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
129647d2ce03SVladimir Oltean port, reg, ds->num_ports);
129747d2ce03SVladimir Oltean of_node_put(port);
129847d2ce03SVladimir Oltean err = -EINVAL;
129947d2ce03SVladimir Oltean goto out_put_node;
130047d2ce03SVladimir Oltean }
130147d2ce03SVladimir Oltean
130247d2ce03SVladimir Oltean dp = dsa_to_port(ds, reg);
130347d2ce03SVladimir Oltean
130447d2ce03SVladimir Oltean err = dsa_port_parse_of(dp, port);
130547d2ce03SVladimir Oltean if (err) {
130647d2ce03SVladimir Oltean of_node_put(port);
130747d2ce03SVladimir Oltean goto out_put_node;
130847d2ce03SVladimir Oltean }
130947d2ce03SVladimir Oltean }
131047d2ce03SVladimir Oltean
131147d2ce03SVladimir Oltean out_put_node:
131247d2ce03SVladimir Oltean of_node_put(ports);
131347d2ce03SVladimir Oltean return err;
131447d2ce03SVladimir Oltean }
131547d2ce03SVladimir Oltean
dsa_switch_parse_member_of(struct dsa_switch * ds,struct device_node * dn)131647d2ce03SVladimir Oltean static int dsa_switch_parse_member_of(struct dsa_switch *ds,
131747d2ce03SVladimir Oltean struct device_node *dn)
131847d2ce03SVladimir Oltean {
131947d2ce03SVladimir Oltean u32 m[2] = { 0, 0 };
132047d2ce03SVladimir Oltean int sz;
132147d2ce03SVladimir Oltean
132247d2ce03SVladimir Oltean /* Don't error out if this optional property isn't found */
132347d2ce03SVladimir Oltean sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
132447d2ce03SVladimir Oltean if (sz < 0 && sz != -EINVAL)
132547d2ce03SVladimir Oltean return sz;
132647d2ce03SVladimir Oltean
132747d2ce03SVladimir Oltean ds->index = m[1];
132847d2ce03SVladimir Oltean
132947d2ce03SVladimir Oltean ds->dst = dsa_tree_touch(m[0]);
133047d2ce03SVladimir Oltean if (!ds->dst)
133147d2ce03SVladimir Oltean return -ENOMEM;
133247d2ce03SVladimir Oltean
133347d2ce03SVladimir Oltean if (dsa_switch_find(ds->dst->index, ds->index)) {
133447d2ce03SVladimir Oltean dev_err(ds->dev,
133547d2ce03SVladimir Oltean "A DSA switch with index %d already exists in tree %d\n",
133647d2ce03SVladimir Oltean ds->index, ds->dst->index);
133747d2ce03SVladimir Oltean return -EEXIST;
133847d2ce03SVladimir Oltean }
133947d2ce03SVladimir Oltean
134047d2ce03SVladimir Oltean if (ds->dst->last_switch < ds->index)
134147d2ce03SVladimir Oltean ds->dst->last_switch = ds->index;
134247d2ce03SVladimir Oltean
134347d2ce03SVladimir Oltean return 0;
134447d2ce03SVladimir Oltean }
134547d2ce03SVladimir Oltean
dsa_switch_touch_ports(struct dsa_switch * ds)134647d2ce03SVladimir Oltean static int dsa_switch_touch_ports(struct dsa_switch *ds)
134747d2ce03SVladimir Oltean {
134847d2ce03SVladimir Oltean struct dsa_port *dp;
134947d2ce03SVladimir Oltean int port;
135047d2ce03SVladimir Oltean
135147d2ce03SVladimir Oltean for (port = 0; port < ds->num_ports; port++) {
135247d2ce03SVladimir Oltean dp = dsa_port_touch(ds, port);
135347d2ce03SVladimir Oltean if (!dp)
135447d2ce03SVladimir Oltean return -ENOMEM;
135547d2ce03SVladimir Oltean }
135647d2ce03SVladimir Oltean
135747d2ce03SVladimir Oltean return 0;
135847d2ce03SVladimir Oltean }
135947d2ce03SVladimir Oltean
dsa_switch_parse_of(struct dsa_switch * ds,struct device_node * dn)136047d2ce03SVladimir Oltean static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
136147d2ce03SVladimir Oltean {
136247d2ce03SVladimir Oltean int err;
136347d2ce03SVladimir Oltean
136447d2ce03SVladimir Oltean err = dsa_switch_parse_member_of(ds, dn);
136547d2ce03SVladimir Oltean if (err)
136647d2ce03SVladimir Oltean return err;
136747d2ce03SVladimir Oltean
136847d2ce03SVladimir Oltean err = dsa_switch_touch_ports(ds);
136947d2ce03SVladimir Oltean if (err)
137047d2ce03SVladimir Oltean return err;
137147d2ce03SVladimir Oltean
137247d2ce03SVladimir Oltean return dsa_switch_parse_ports_of(ds, dn);
137347d2ce03SVladimir Oltean }
137447d2ce03SVladimir Oltean
dev_is_class(struct device * dev,void * class)137547d2ce03SVladimir Oltean static int dev_is_class(struct device *dev, void *class)
137647d2ce03SVladimir Oltean {
137747d2ce03SVladimir Oltean if (dev->class != NULL && !strcmp(dev->class->name, class))
137847d2ce03SVladimir Oltean return 1;
137947d2ce03SVladimir Oltean
138047d2ce03SVladimir Oltean return 0;
138147d2ce03SVladimir Oltean }
138247d2ce03SVladimir Oltean
dev_find_class(struct device * parent,char * class)138347d2ce03SVladimir Oltean static struct device *dev_find_class(struct device *parent, char *class)
138447d2ce03SVladimir Oltean {
138547d2ce03SVladimir Oltean if (dev_is_class(parent, class)) {
138647d2ce03SVladimir Oltean get_device(parent);
138747d2ce03SVladimir Oltean return parent;
138847d2ce03SVladimir Oltean }
138947d2ce03SVladimir Oltean
139047d2ce03SVladimir Oltean return device_find_child(parent, class, dev_is_class);
139147d2ce03SVladimir Oltean }
139247d2ce03SVladimir Oltean
dsa_dev_to_net_device(struct device * dev)139347d2ce03SVladimir Oltean static struct net_device *dsa_dev_to_net_device(struct device *dev)
139447d2ce03SVladimir Oltean {
139547d2ce03SVladimir Oltean struct device *d;
139647d2ce03SVladimir Oltean
139747d2ce03SVladimir Oltean d = dev_find_class(dev, "net");
139847d2ce03SVladimir Oltean if (d != NULL) {
139947d2ce03SVladimir Oltean struct net_device *nd;
140047d2ce03SVladimir Oltean
140147d2ce03SVladimir Oltean nd = to_net_dev(d);
140247d2ce03SVladimir Oltean dev_hold(nd);
140347d2ce03SVladimir Oltean put_device(d);
140447d2ce03SVladimir Oltean
140547d2ce03SVladimir Oltean return nd;
140647d2ce03SVladimir Oltean }
140747d2ce03SVladimir Oltean
140847d2ce03SVladimir Oltean return NULL;
140947d2ce03SVladimir Oltean }
141047d2ce03SVladimir Oltean
dsa_port_parse(struct dsa_port * dp,const char * name,struct device * dev)141147d2ce03SVladimir Oltean static int dsa_port_parse(struct dsa_port *dp, const char *name,
141247d2ce03SVladimir Oltean struct device *dev)
141347d2ce03SVladimir Oltean {
141447d2ce03SVladimir Oltean if (!strcmp(name, "cpu")) {
141547d2ce03SVladimir Oltean struct net_device *master;
141647d2ce03SVladimir Oltean
141747d2ce03SVladimir Oltean master = dsa_dev_to_net_device(dev);
141847d2ce03SVladimir Oltean if (!master)
141947d2ce03SVladimir Oltean return -EPROBE_DEFER;
142047d2ce03SVladimir Oltean
142147d2ce03SVladimir Oltean dev_put(master);
142247d2ce03SVladimir Oltean
142347d2ce03SVladimir Oltean return dsa_port_parse_cpu(dp, master, NULL);
142447d2ce03SVladimir Oltean }
142547d2ce03SVladimir Oltean
142647d2ce03SVladimir Oltean if (!strcmp(name, "dsa"))
142747d2ce03SVladimir Oltean return dsa_port_parse_dsa(dp);
142847d2ce03SVladimir Oltean
142947d2ce03SVladimir Oltean return dsa_port_parse_user(dp, name);
143047d2ce03SVladimir Oltean }
143147d2ce03SVladimir Oltean
dsa_switch_parse_ports(struct dsa_switch * ds,struct dsa_chip_data * cd)143247d2ce03SVladimir Oltean static int dsa_switch_parse_ports(struct dsa_switch *ds,
143347d2ce03SVladimir Oltean struct dsa_chip_data *cd)
143447d2ce03SVladimir Oltean {
143547d2ce03SVladimir Oltean bool valid_name_found = false;
143647d2ce03SVladimir Oltean struct dsa_port *dp;
143747d2ce03SVladimir Oltean struct device *dev;
143847d2ce03SVladimir Oltean const char *name;
143947d2ce03SVladimir Oltean unsigned int i;
144047d2ce03SVladimir Oltean int err;
144147d2ce03SVladimir Oltean
144247d2ce03SVladimir Oltean for (i = 0; i < DSA_MAX_PORTS; i++) {
144347d2ce03SVladimir Oltean name = cd->port_names[i];
144447d2ce03SVladimir Oltean dev = cd->netdev[i];
144547d2ce03SVladimir Oltean dp = dsa_to_port(ds, i);
144647d2ce03SVladimir Oltean
144747d2ce03SVladimir Oltean if (!name)
144847d2ce03SVladimir Oltean continue;
144947d2ce03SVladimir Oltean
145047d2ce03SVladimir Oltean err = dsa_port_parse(dp, name, dev);
145147d2ce03SVladimir Oltean if (err)
145247d2ce03SVladimir Oltean return err;
145347d2ce03SVladimir Oltean
145447d2ce03SVladimir Oltean valid_name_found = true;
145547d2ce03SVladimir Oltean }
145647d2ce03SVladimir Oltean
145747d2ce03SVladimir Oltean if (!valid_name_found && i == DSA_MAX_PORTS)
145847d2ce03SVladimir Oltean return -EINVAL;
145947d2ce03SVladimir Oltean
146047d2ce03SVladimir Oltean return 0;
146147d2ce03SVladimir Oltean }
146247d2ce03SVladimir Oltean
dsa_switch_parse(struct dsa_switch * ds,struct dsa_chip_data * cd)146347d2ce03SVladimir Oltean static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
146447d2ce03SVladimir Oltean {
146547d2ce03SVladimir Oltean int err;
146647d2ce03SVladimir Oltean
146747d2ce03SVladimir Oltean ds->cd = cd;
146847d2ce03SVladimir Oltean
146947d2ce03SVladimir Oltean /* We don't support interconnected switches nor multiple trees via
147047d2ce03SVladimir Oltean * platform data, so this is the unique switch of the tree.
147147d2ce03SVladimir Oltean */
147247d2ce03SVladimir Oltean ds->index = 0;
147347d2ce03SVladimir Oltean ds->dst = dsa_tree_touch(0);
147447d2ce03SVladimir Oltean if (!ds->dst)
147547d2ce03SVladimir Oltean return -ENOMEM;
147647d2ce03SVladimir Oltean
147747d2ce03SVladimir Oltean err = dsa_switch_touch_ports(ds);
147847d2ce03SVladimir Oltean if (err)
147947d2ce03SVladimir Oltean return err;
148047d2ce03SVladimir Oltean
148147d2ce03SVladimir Oltean return dsa_switch_parse_ports(ds, cd);
148247d2ce03SVladimir Oltean }
148347d2ce03SVladimir Oltean
dsa_switch_release_ports(struct dsa_switch * ds)148447d2ce03SVladimir Oltean static void dsa_switch_release_ports(struct dsa_switch *ds)
148547d2ce03SVladimir Oltean {
148647d2ce03SVladimir Oltean struct dsa_port *dp, *next;
148747d2ce03SVladimir Oltean
148847d2ce03SVladimir Oltean dsa_switch_for_each_port_safe(dp, next, ds) {
148947d2ce03SVladimir Oltean WARN_ON(!list_empty(&dp->fdbs));
149047d2ce03SVladimir Oltean WARN_ON(!list_empty(&dp->mdbs));
149147d2ce03SVladimir Oltean WARN_ON(!list_empty(&dp->vlans));
149247d2ce03SVladimir Oltean list_del(&dp->list);
149347d2ce03SVladimir Oltean kfree(dp);
149447d2ce03SVladimir Oltean }
149547d2ce03SVladimir Oltean }
149647d2ce03SVladimir Oltean
dsa_switch_probe(struct dsa_switch * ds)149747d2ce03SVladimir Oltean static int dsa_switch_probe(struct dsa_switch *ds)
149847d2ce03SVladimir Oltean {
149947d2ce03SVladimir Oltean struct dsa_switch_tree *dst;
150047d2ce03SVladimir Oltean struct dsa_chip_data *pdata;
150147d2ce03SVladimir Oltean struct device_node *np;
150247d2ce03SVladimir Oltean int err;
150347d2ce03SVladimir Oltean
150447d2ce03SVladimir Oltean if (!ds->dev)
150547d2ce03SVladimir Oltean return -ENODEV;
150647d2ce03SVladimir Oltean
150747d2ce03SVladimir Oltean pdata = ds->dev->platform_data;
150847d2ce03SVladimir Oltean np = ds->dev->of_node;
150947d2ce03SVladimir Oltean
151047d2ce03SVladimir Oltean if (!ds->num_ports)
151147d2ce03SVladimir Oltean return -EINVAL;
151247d2ce03SVladimir Oltean
151347d2ce03SVladimir Oltean if (np) {
151447d2ce03SVladimir Oltean err = dsa_switch_parse_of(ds, np);
151547d2ce03SVladimir Oltean if (err)
151647d2ce03SVladimir Oltean dsa_switch_release_ports(ds);
151747d2ce03SVladimir Oltean } else if (pdata) {
151847d2ce03SVladimir Oltean err = dsa_switch_parse(ds, pdata);
151947d2ce03SVladimir Oltean if (err)
152047d2ce03SVladimir Oltean dsa_switch_release_ports(ds);
152147d2ce03SVladimir Oltean } else {
152247d2ce03SVladimir Oltean err = -ENODEV;
152347d2ce03SVladimir Oltean }
152447d2ce03SVladimir Oltean
152547d2ce03SVladimir Oltean if (err)
152647d2ce03SVladimir Oltean return err;
152747d2ce03SVladimir Oltean
152847d2ce03SVladimir Oltean dst = ds->dst;
152947d2ce03SVladimir Oltean dsa_tree_get(dst);
153047d2ce03SVladimir Oltean err = dsa_tree_setup(dst);
153147d2ce03SVladimir Oltean if (err) {
153247d2ce03SVladimir Oltean dsa_switch_release_ports(ds);
153347d2ce03SVladimir Oltean dsa_tree_put(dst);
153447d2ce03SVladimir Oltean }
153547d2ce03SVladimir Oltean
153647d2ce03SVladimir Oltean return err;
153747d2ce03SVladimir Oltean }
153847d2ce03SVladimir Oltean
dsa_register_switch(struct dsa_switch * ds)153947d2ce03SVladimir Oltean int dsa_register_switch(struct dsa_switch *ds)
154047d2ce03SVladimir Oltean {
154147d2ce03SVladimir Oltean int err;
154247d2ce03SVladimir Oltean
154347d2ce03SVladimir Oltean mutex_lock(&dsa2_mutex);
154447d2ce03SVladimir Oltean err = dsa_switch_probe(ds);
154547d2ce03SVladimir Oltean dsa_tree_put(ds->dst);
154647d2ce03SVladimir Oltean mutex_unlock(&dsa2_mutex);
154747d2ce03SVladimir Oltean
154847d2ce03SVladimir Oltean return err;
154947d2ce03SVladimir Oltean }
155047d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_register_switch);
155147d2ce03SVladimir Oltean
dsa_switch_remove(struct dsa_switch * ds)155247d2ce03SVladimir Oltean static void dsa_switch_remove(struct dsa_switch *ds)
155347d2ce03SVladimir Oltean {
155447d2ce03SVladimir Oltean struct dsa_switch_tree *dst = ds->dst;
155547d2ce03SVladimir Oltean
155647d2ce03SVladimir Oltean dsa_tree_teardown(dst);
155747d2ce03SVladimir Oltean dsa_switch_release_ports(ds);
155847d2ce03SVladimir Oltean dsa_tree_put(dst);
155947d2ce03SVladimir Oltean }
156047d2ce03SVladimir Oltean
dsa_unregister_switch(struct dsa_switch * ds)156147d2ce03SVladimir Oltean void dsa_unregister_switch(struct dsa_switch *ds)
156247d2ce03SVladimir Oltean {
156347d2ce03SVladimir Oltean mutex_lock(&dsa2_mutex);
156447d2ce03SVladimir Oltean dsa_switch_remove(ds);
156547d2ce03SVladimir Oltean mutex_unlock(&dsa2_mutex);
156647d2ce03SVladimir Oltean }
156747d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_unregister_switch);
156847d2ce03SVladimir Oltean
156947d2ce03SVladimir Oltean /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
157047d2ce03SVladimir Oltean * blocking that operation from completion, due to the dev_hold taken inside
157147d2ce03SVladimir Oltean * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
157247d2ce03SVladimir Oltean * the DSA master, so that the system can reboot successfully.
157347d2ce03SVladimir Oltean */
dsa_switch_shutdown(struct dsa_switch * ds)157447d2ce03SVladimir Oltean void dsa_switch_shutdown(struct dsa_switch *ds)
157547d2ce03SVladimir Oltean {
157647d2ce03SVladimir Oltean struct net_device *master, *slave_dev;
157747d2ce03SVladimir Oltean struct dsa_port *dp;
157847d2ce03SVladimir Oltean
157947d2ce03SVladimir Oltean mutex_lock(&dsa2_mutex);
158047d2ce03SVladimir Oltean
158147d2ce03SVladimir Oltean if (!ds->setup)
158247d2ce03SVladimir Oltean goto out;
158347d2ce03SVladimir Oltean
158447d2ce03SVladimir Oltean rtnl_lock();
158547d2ce03SVladimir Oltean
158647d2ce03SVladimir Oltean dsa_switch_for_each_user_port(dp, ds) {
158747d2ce03SVladimir Oltean master = dsa_port_to_master(dp);
158847d2ce03SVladimir Oltean slave_dev = dp->slave;
158947d2ce03SVladimir Oltean
159047d2ce03SVladimir Oltean netdev_upper_dev_unlink(master, slave_dev);
159147d2ce03SVladimir Oltean }
159247d2ce03SVladimir Oltean
159347d2ce03SVladimir Oltean /* Disconnect from further netdevice notifiers on the master,
159447d2ce03SVladimir Oltean * since netdev_uses_dsa() will now return false.
159547d2ce03SVladimir Oltean */
159647d2ce03SVladimir Oltean dsa_switch_for_each_cpu_port(dp, ds)
159747d2ce03SVladimir Oltean dp->master->dsa_ptr = NULL;
159847d2ce03SVladimir Oltean
159947d2ce03SVladimir Oltean rtnl_unlock();
160047d2ce03SVladimir Oltean out:
160147d2ce03SVladimir Oltean mutex_unlock(&dsa2_mutex);
160247d2ce03SVladimir Oltean }
160347d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_switch_shutdown);
160447d2ce03SVladimir Oltean
160547d2ce03SVladimir Oltean #ifdef CONFIG_PM_SLEEP
dsa_port_is_initialized(const struct dsa_port * dp)160647d2ce03SVladimir Oltean static bool dsa_port_is_initialized(const struct dsa_port *dp)
160747d2ce03SVladimir Oltean {
160847d2ce03SVladimir Oltean return dp->type == DSA_PORT_TYPE_USER && dp->slave;
160947d2ce03SVladimir Oltean }
161047d2ce03SVladimir Oltean
dsa_switch_suspend(struct dsa_switch * ds)161147d2ce03SVladimir Oltean int dsa_switch_suspend(struct dsa_switch *ds)
161247d2ce03SVladimir Oltean {
161347d2ce03SVladimir Oltean struct dsa_port *dp;
161447d2ce03SVladimir Oltean int ret = 0;
161547d2ce03SVladimir Oltean
161647d2ce03SVladimir Oltean /* Suspend slave network devices */
161747d2ce03SVladimir Oltean dsa_switch_for_each_port(dp, ds) {
161847d2ce03SVladimir Oltean if (!dsa_port_is_initialized(dp))
161947d2ce03SVladimir Oltean continue;
162047d2ce03SVladimir Oltean
162147d2ce03SVladimir Oltean ret = dsa_slave_suspend(dp->slave);
162247d2ce03SVladimir Oltean if (ret)
162347d2ce03SVladimir Oltean return ret;
162447d2ce03SVladimir Oltean }
162547d2ce03SVladimir Oltean
162647d2ce03SVladimir Oltean if (ds->ops->suspend)
162747d2ce03SVladimir Oltean ret = ds->ops->suspend(ds);
162847d2ce03SVladimir Oltean
162947d2ce03SVladimir Oltean return ret;
163047d2ce03SVladimir Oltean }
163147d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_switch_suspend);
163247d2ce03SVladimir Oltean
dsa_switch_resume(struct dsa_switch * ds)163347d2ce03SVladimir Oltean int dsa_switch_resume(struct dsa_switch *ds)
163447d2ce03SVladimir Oltean {
163547d2ce03SVladimir Oltean struct dsa_port *dp;
163647d2ce03SVladimir Oltean int ret = 0;
163747d2ce03SVladimir Oltean
163847d2ce03SVladimir Oltean if (ds->ops->resume)
163947d2ce03SVladimir Oltean ret = ds->ops->resume(ds);
164047d2ce03SVladimir Oltean
164147d2ce03SVladimir Oltean if (ret)
164247d2ce03SVladimir Oltean return ret;
164347d2ce03SVladimir Oltean
164447d2ce03SVladimir Oltean /* Resume slave network devices */
164547d2ce03SVladimir Oltean dsa_switch_for_each_port(dp, ds) {
164647d2ce03SVladimir Oltean if (!dsa_port_is_initialized(dp))
164747d2ce03SVladimir Oltean continue;
164847d2ce03SVladimir Oltean
164947d2ce03SVladimir Oltean ret = dsa_slave_resume(dp->slave);
165047d2ce03SVladimir Oltean if (ret)
165147d2ce03SVladimir Oltean return ret;
165247d2ce03SVladimir Oltean }
165347d2ce03SVladimir Oltean
165447d2ce03SVladimir Oltean return 0;
165547d2ce03SVladimir Oltean }
165647d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_switch_resume);
165747d2ce03SVladimir Oltean #endif
165847d2ce03SVladimir Oltean
dsa_port_from_netdev(struct net_device * netdev)165947d2ce03SVladimir Oltean struct dsa_port *dsa_port_from_netdev(struct net_device *netdev)
166047d2ce03SVladimir Oltean {
166147d2ce03SVladimir Oltean if (!netdev || !dsa_slave_dev_check(netdev))
166247d2ce03SVladimir Oltean return ERR_PTR(-ENODEV);
166347d2ce03SVladimir Oltean
166447d2ce03SVladimir Oltean return dsa_slave_to_port(netdev);
166547d2ce03SVladimir Oltean }
166647d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_port_from_netdev);
166747d2ce03SVladimir Oltean
dsa_db_equal(const struct dsa_db * a,const struct dsa_db * b)166847d2ce03SVladimir Oltean bool dsa_db_equal(const struct dsa_db *a, const struct dsa_db *b)
166947d2ce03SVladimir Oltean {
167047d2ce03SVladimir Oltean if (a->type != b->type)
167147d2ce03SVladimir Oltean return false;
167247d2ce03SVladimir Oltean
167347d2ce03SVladimir Oltean switch (a->type) {
167447d2ce03SVladimir Oltean case DSA_DB_PORT:
167547d2ce03SVladimir Oltean return a->dp == b->dp;
167647d2ce03SVladimir Oltean case DSA_DB_LAG:
167747d2ce03SVladimir Oltean return a->lag.dev == b->lag.dev;
167847d2ce03SVladimir Oltean case DSA_DB_BRIDGE:
167947d2ce03SVladimir Oltean return a->bridge.num == b->bridge.num;
168047d2ce03SVladimir Oltean default:
168147d2ce03SVladimir Oltean WARN_ON(1);
168247d2ce03SVladimir Oltean return false;
168347d2ce03SVladimir Oltean }
168447d2ce03SVladimir Oltean }
168547d2ce03SVladimir Oltean
dsa_fdb_present_in_other_db(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid,struct dsa_db db)168647d2ce03SVladimir Oltean bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
168747d2ce03SVladimir Oltean const unsigned char *addr, u16 vid,
168847d2ce03SVladimir Oltean struct dsa_db db)
168947d2ce03SVladimir Oltean {
169047d2ce03SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port);
169147d2ce03SVladimir Oltean struct dsa_mac_addr *a;
169247d2ce03SVladimir Oltean
169347d2ce03SVladimir Oltean lockdep_assert_held(&dp->addr_lists_lock);
169447d2ce03SVladimir Oltean
169547d2ce03SVladimir Oltean list_for_each_entry(a, &dp->fdbs, list) {
169647d2ce03SVladimir Oltean if (!ether_addr_equal(a->addr, addr) || a->vid != vid)
169747d2ce03SVladimir Oltean continue;
169847d2ce03SVladimir Oltean
169947d2ce03SVladimir Oltean if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
170047d2ce03SVladimir Oltean return true;
170147d2ce03SVladimir Oltean }
170247d2ce03SVladimir Oltean
170347d2ce03SVladimir Oltean return false;
170447d2ce03SVladimir Oltean }
170547d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_fdb_present_in_other_db);
170647d2ce03SVladimir Oltean
dsa_mdb_present_in_other_db(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb,struct dsa_db db)170747d2ce03SVladimir Oltean bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
170847d2ce03SVladimir Oltean const struct switchdev_obj_port_mdb *mdb,
170947d2ce03SVladimir Oltean struct dsa_db db)
171047d2ce03SVladimir Oltean {
171147d2ce03SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port);
171247d2ce03SVladimir Oltean struct dsa_mac_addr *a;
171347d2ce03SVladimir Oltean
171447d2ce03SVladimir Oltean lockdep_assert_held(&dp->addr_lists_lock);
171547d2ce03SVladimir Oltean
171647d2ce03SVladimir Oltean list_for_each_entry(a, &dp->mdbs, list) {
171747d2ce03SVladimir Oltean if (!ether_addr_equal(a->addr, mdb->addr) || a->vid != mdb->vid)
171847d2ce03SVladimir Oltean continue;
171947d2ce03SVladimir Oltean
172047d2ce03SVladimir Oltean if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
172147d2ce03SVladimir Oltean return true;
172247d2ce03SVladimir Oltean }
172347d2ce03SVladimir Oltean
172447d2ce03SVladimir Oltean return false;
172547d2ce03SVladimir Oltean }
172647d2ce03SVladimir Oltean EXPORT_SYMBOL_GPL(dsa_mdb_present_in_other_db);
172747d2ce03SVladimir Oltean
17285a178186SVladimir Oltean static const struct dsa_stubs __dsa_stubs = {
17295a178186SVladimir Oltean .master_hwtstamp_validate = __dsa_master_hwtstamp_validate,
17305a178186SVladimir Oltean };
17315a178186SVladimir Oltean
dsa_register_stubs(void)17325a178186SVladimir Oltean static void dsa_register_stubs(void)
17335a178186SVladimir Oltean {
17345a178186SVladimir Oltean dsa_stubs = &__dsa_stubs;
17355a178186SVladimir Oltean }
17365a178186SVladimir Oltean
dsa_unregister_stubs(void)17375a178186SVladimir Oltean static void dsa_unregister_stubs(void)
17385a178186SVladimir Oltean {
17395a178186SVladimir Oltean dsa_stubs = NULL;
17405a178186SVladimir Oltean }
17415a178186SVladimir Oltean
dsa_init_module(void)174247d2ce03SVladimir Oltean static int __init dsa_init_module(void)
174347d2ce03SVladimir Oltean {
174447d2ce03SVladimir Oltean int rc;
174547d2ce03SVladimir Oltean
174647d2ce03SVladimir Oltean dsa_owq = alloc_ordered_workqueue("dsa_ordered",
174747d2ce03SVladimir Oltean WQ_MEM_RECLAIM);
174847d2ce03SVladimir Oltean if (!dsa_owq)
174947d2ce03SVladimir Oltean return -ENOMEM;
175047d2ce03SVladimir Oltean
175147d2ce03SVladimir Oltean rc = dsa_slave_register_notifier();
175247d2ce03SVladimir Oltean if (rc)
175347d2ce03SVladimir Oltean goto register_notifier_fail;
175447d2ce03SVladimir Oltean
175547d2ce03SVladimir Oltean dev_add_pack(&dsa_pack_type);
175647d2ce03SVladimir Oltean
175747d2ce03SVladimir Oltean rc = rtnl_link_register(&dsa_link_ops);
175847d2ce03SVladimir Oltean if (rc)
175947d2ce03SVladimir Oltean goto netlink_register_fail;
176047d2ce03SVladimir Oltean
17615a178186SVladimir Oltean dsa_register_stubs();
17625a178186SVladimir Oltean
176347d2ce03SVladimir Oltean return 0;
176447d2ce03SVladimir Oltean
176547d2ce03SVladimir Oltean netlink_register_fail:
176647d2ce03SVladimir Oltean dsa_slave_unregister_notifier();
176747d2ce03SVladimir Oltean dev_remove_pack(&dsa_pack_type);
176847d2ce03SVladimir Oltean register_notifier_fail:
176947d2ce03SVladimir Oltean destroy_workqueue(dsa_owq);
177047d2ce03SVladimir Oltean
177147d2ce03SVladimir Oltean return rc;
177247d2ce03SVladimir Oltean }
177347d2ce03SVladimir Oltean module_init(dsa_init_module);
177447d2ce03SVladimir Oltean
dsa_cleanup_module(void)177547d2ce03SVladimir Oltean static void __exit dsa_cleanup_module(void)
177647d2ce03SVladimir Oltean {
17775a178186SVladimir Oltean dsa_unregister_stubs();
17785a178186SVladimir Oltean
177947d2ce03SVladimir Oltean rtnl_link_unregister(&dsa_link_ops);
178047d2ce03SVladimir Oltean
178147d2ce03SVladimir Oltean dsa_slave_unregister_notifier();
178247d2ce03SVladimir Oltean dev_remove_pack(&dsa_pack_type);
178347d2ce03SVladimir Oltean destroy_workqueue(dsa_owq);
178447d2ce03SVladimir Oltean }
178547d2ce03SVladimir Oltean module_exit(dsa_cleanup_module);
178647d2ce03SVladimir Oltean
178747d2ce03SVladimir Oltean MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
178847d2ce03SVladimir Oltean MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
178947d2ce03SVladimir Oltean MODULE_LICENSE("GPL");
179047d2ce03SVladimir Oltean MODULE_ALIAS("platform:dsa");
1791