xref: /openbmc/linux/net/dsa/switch.c (revision 5d0e4d78)
1 /*
2  * Handling of a single switch chip, part of a switch fabric
3  *
4  * Copyright (c) 2017 Savoir-faire Linux Inc.
5  *	Vivien Didelot <vivien.didelot@savoirfairelinux.com>
6  *
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.
11  */
12 
13 #include <linux/netdevice.h>
14 #include <linux/notifier.h>
15 #include <net/switchdev.h>
16 
17 #include "dsa_priv.h"
18 
19 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
20 						   unsigned int ageing_time)
21 {
22 	int i;
23 
24 	for (i = 0; i < ds->num_ports; ++i) {
25 		struct dsa_port *dp = &ds->ports[i];
26 
27 		if (dp->ageing_time && dp->ageing_time < ageing_time)
28 			ageing_time = dp->ageing_time;
29 	}
30 
31 	return ageing_time;
32 }
33 
34 static int dsa_switch_ageing_time(struct dsa_switch *ds,
35 				  struct dsa_notifier_ageing_time_info *info)
36 {
37 	unsigned int ageing_time = info->ageing_time;
38 	struct switchdev_trans *trans = info->trans;
39 
40 	if (switchdev_trans_ph_prepare(trans)) {
41 		if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
42 			return -ERANGE;
43 		if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
44 			return -ERANGE;
45 		return 0;
46 	}
47 
48 	/* Program the fastest ageing time in case of multiple bridges */
49 	ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
50 
51 	if (ds->ops->set_ageing_time)
52 		return ds->ops->set_ageing_time(ds, ageing_time);
53 
54 	return 0;
55 }
56 
57 static int dsa_switch_bridge_join(struct dsa_switch *ds,
58 				  struct dsa_notifier_bridge_info *info)
59 {
60 	if (ds->index == info->sw_index && ds->ops->port_bridge_join)
61 		return ds->ops->port_bridge_join(ds, info->port, info->br);
62 
63 	if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
64 		return ds->ops->crosschip_bridge_join(ds, info->sw_index,
65 						      info->port, info->br);
66 
67 	return 0;
68 }
69 
70 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
71 				   struct dsa_notifier_bridge_info *info)
72 {
73 	if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
74 		ds->ops->port_bridge_leave(ds, info->port, info->br);
75 
76 	if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
77 		ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
78 						info->br);
79 
80 	return 0;
81 }
82 
83 static int dsa_switch_fdb_add(struct dsa_switch *ds,
84 			      struct dsa_notifier_fdb_info *info)
85 {
86 	const struct switchdev_obj_port_fdb *fdb = info->fdb;
87 	struct switchdev_trans *trans = info->trans;
88 
89 	/* Do not care yet about other switch chips of the fabric */
90 	if (ds->index != info->sw_index)
91 		return 0;
92 
93 	if (switchdev_trans_ph_prepare(trans)) {
94 		if (!ds->ops->port_fdb_prepare || !ds->ops->port_fdb_add)
95 			return -EOPNOTSUPP;
96 
97 		return ds->ops->port_fdb_prepare(ds, info->port, fdb, trans);
98 	}
99 
100 	ds->ops->port_fdb_add(ds, info->port, fdb, trans);
101 
102 	return 0;
103 }
104 
105 static int dsa_switch_fdb_del(struct dsa_switch *ds,
106 			      struct dsa_notifier_fdb_info *info)
107 {
108 	const struct switchdev_obj_port_fdb *fdb = info->fdb;
109 
110 	/* Do not care yet about other switch chips of the fabric */
111 	if (ds->index != info->sw_index)
112 		return 0;
113 
114 	if (!ds->ops->port_fdb_del)
115 		return -EOPNOTSUPP;
116 
117 	return ds->ops->port_fdb_del(ds, info->port, fdb);
118 }
119 
120 static int dsa_switch_mdb_add(struct dsa_switch *ds,
121 			      struct dsa_notifier_mdb_info *info)
122 {
123 	const struct switchdev_obj_port_mdb *mdb = info->mdb;
124 	struct switchdev_trans *trans = info->trans;
125 	DECLARE_BITMAP(group, ds->num_ports);
126 	int port, err;
127 
128 	/* Build a mask of Multicast group members */
129 	bitmap_zero(group, ds->num_ports);
130 	if (ds->index == info->sw_index)
131 		set_bit(info->port, group);
132 	for (port = 0; port < ds->num_ports; port++)
133 		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
134 			set_bit(port, group);
135 
136 	if (switchdev_trans_ph_prepare(trans)) {
137 		if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
138 			return -EOPNOTSUPP;
139 
140 		for_each_set_bit(port, group, ds->num_ports) {
141 			err = ds->ops->port_mdb_prepare(ds, port, mdb, trans);
142 			if (err)
143 				return err;
144 		}
145 	}
146 
147 	for_each_set_bit(port, group, ds->num_ports)
148 		ds->ops->port_mdb_add(ds, port, mdb, trans);
149 
150 	return 0;
151 }
152 
153 static int dsa_switch_mdb_del(struct dsa_switch *ds,
154 			      struct dsa_notifier_mdb_info *info)
155 {
156 	const struct switchdev_obj_port_mdb *mdb = info->mdb;
157 
158 	if (!ds->ops->port_mdb_del)
159 		return -EOPNOTSUPP;
160 
161 	if (ds->index == info->sw_index)
162 		return ds->ops->port_mdb_del(ds, info->port, mdb);
163 
164 	return 0;
165 }
166 
167 static int dsa_switch_vlan_add(struct dsa_switch *ds,
168 			       struct dsa_notifier_vlan_info *info)
169 {
170 	const struct switchdev_obj_port_vlan *vlan = info->vlan;
171 	struct switchdev_trans *trans = info->trans;
172 	DECLARE_BITMAP(members, ds->num_ports);
173 	int port, err;
174 
175 	/* Build a mask of VLAN members */
176 	bitmap_zero(members, ds->num_ports);
177 	if (ds->index == info->sw_index)
178 		set_bit(info->port, members);
179 	for (port = 0; port < ds->num_ports; port++)
180 		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
181 			set_bit(port, members);
182 
183 	if (switchdev_trans_ph_prepare(trans)) {
184 		if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
185 			return -EOPNOTSUPP;
186 
187 		for_each_set_bit(port, members, ds->num_ports) {
188 			err = ds->ops->port_vlan_prepare(ds, port, vlan, trans);
189 			if (err)
190 				return err;
191 		}
192 	}
193 
194 	for_each_set_bit(port, members, ds->num_ports)
195 		ds->ops->port_vlan_add(ds, port, vlan, trans);
196 
197 	return 0;
198 }
199 
200 static int dsa_switch_vlan_del(struct dsa_switch *ds,
201 			       struct dsa_notifier_vlan_info *info)
202 {
203 	const struct switchdev_obj_port_vlan *vlan = info->vlan;
204 
205 	if (!ds->ops->port_vlan_del)
206 		return -EOPNOTSUPP;
207 
208 	if (ds->index == info->sw_index)
209 		return ds->ops->port_vlan_del(ds, info->port, vlan);
210 
211 	return 0;
212 }
213 
214 static int dsa_switch_event(struct notifier_block *nb,
215 			    unsigned long event, void *info)
216 {
217 	struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
218 	int err;
219 
220 	switch (event) {
221 	case DSA_NOTIFIER_AGEING_TIME:
222 		err = dsa_switch_ageing_time(ds, info);
223 		break;
224 	case DSA_NOTIFIER_BRIDGE_JOIN:
225 		err = dsa_switch_bridge_join(ds, info);
226 		break;
227 	case DSA_NOTIFIER_BRIDGE_LEAVE:
228 		err = dsa_switch_bridge_leave(ds, info);
229 		break;
230 	case DSA_NOTIFIER_FDB_ADD:
231 		err = dsa_switch_fdb_add(ds, info);
232 		break;
233 	case DSA_NOTIFIER_FDB_DEL:
234 		err = dsa_switch_fdb_del(ds, info);
235 		break;
236 	case DSA_NOTIFIER_MDB_ADD:
237 		err = dsa_switch_mdb_add(ds, info);
238 		break;
239 	case DSA_NOTIFIER_MDB_DEL:
240 		err = dsa_switch_mdb_del(ds, info);
241 		break;
242 	case DSA_NOTIFIER_VLAN_ADD:
243 		err = dsa_switch_vlan_add(ds, info);
244 		break;
245 	case DSA_NOTIFIER_VLAN_DEL:
246 		err = dsa_switch_vlan_del(ds, info);
247 		break;
248 	default:
249 		err = -EOPNOTSUPP;
250 		break;
251 	}
252 
253 	/* Non-switchdev operations cannot be rolled back. If a DSA driver
254 	 * returns an error during the chained call, switch chips may be in an
255 	 * inconsistent state.
256 	 */
257 	if (err)
258 		dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
259 			event, err);
260 
261 	return notifier_from_errno(err);
262 }
263 
264 int dsa_switch_register_notifier(struct dsa_switch *ds)
265 {
266 	ds->nb.notifier_call = dsa_switch_event;
267 
268 	return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
269 }
270 
271 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
272 {
273 	int err;
274 
275 	err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
276 	if (err)
277 		dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
278 }
279