xref: /openbmc/linux/net/dsa/switch.c (revision 6cd70754)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Handling of a single switch chip, part of a switch fabric
4  *
5  * Copyright (c) 2017 Savoir-faire Linux Inc.
6  *	Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7  */
8 
9 #include <linux/if_bridge.h>
10 #include <linux/netdevice.h>
11 #include <linux/notifier.h>
12 #include <linux/if_vlan.h>
13 #include <net/switchdev.h>
14 
15 #include "dsa_priv.h"
16 
17 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
18 						   unsigned int ageing_time)
19 {
20 	int i;
21 
22 	for (i = 0; i < ds->num_ports; ++i) {
23 		struct dsa_port *dp = dsa_to_port(ds, i);
24 
25 		if (dp->ageing_time && dp->ageing_time < ageing_time)
26 			ageing_time = dp->ageing_time;
27 	}
28 
29 	return ageing_time;
30 }
31 
32 static int dsa_switch_ageing_time(struct dsa_switch *ds,
33 				  struct dsa_notifier_ageing_time_info *info)
34 {
35 	unsigned int ageing_time = info->ageing_time;
36 
37 	if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
38 		return -ERANGE;
39 
40 	if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
41 		return -ERANGE;
42 
43 	/* Program the fastest ageing time in case of multiple bridges */
44 	ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
45 
46 	if (ds->ops->set_ageing_time)
47 		return ds->ops->set_ageing_time(ds, ageing_time);
48 
49 	return 0;
50 }
51 
52 static bool dsa_switch_mtu_match(struct dsa_switch *ds, int port,
53 				 struct dsa_notifier_mtu_info *info)
54 {
55 	if (ds->index == info->sw_index)
56 		return (port == info->port) || dsa_is_dsa_port(ds, port);
57 
58 	if (!info->propagate_upstream)
59 		return false;
60 
61 	if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
62 		return true;
63 
64 	return false;
65 }
66 
67 static int dsa_switch_mtu(struct dsa_switch *ds,
68 			  struct dsa_notifier_mtu_info *info)
69 {
70 	int port, ret;
71 
72 	if (!ds->ops->port_change_mtu)
73 		return -EOPNOTSUPP;
74 
75 	for (port = 0; port < ds->num_ports; port++) {
76 		if (dsa_switch_mtu_match(ds, port, info)) {
77 			ret = ds->ops->port_change_mtu(ds, port, info->mtu);
78 			if (ret)
79 				return ret;
80 		}
81 	}
82 
83 	return 0;
84 }
85 
86 static int dsa_switch_bridge_join(struct dsa_switch *ds,
87 				  struct dsa_notifier_bridge_info *info)
88 {
89 	struct dsa_switch_tree *dst = ds->dst;
90 
91 	if (dst->index == info->tree_index && ds->index == info->sw_index &&
92 	    ds->ops->port_bridge_join)
93 		return ds->ops->port_bridge_join(ds, info->port, info->br);
94 
95 	if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
96 	    ds->ops->crosschip_bridge_join)
97 		return ds->ops->crosschip_bridge_join(ds, info->tree_index,
98 						      info->sw_index,
99 						      info->port, info->br);
100 
101 	return 0;
102 }
103 
104 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
105 				   struct dsa_notifier_bridge_info *info)
106 {
107 	bool unset_vlan_filtering = br_vlan_enabled(info->br);
108 	struct dsa_switch_tree *dst = ds->dst;
109 	struct netlink_ext_ack extack = {0};
110 	int err, port;
111 
112 	if (dst->index == info->tree_index && ds->index == info->sw_index &&
113 	    ds->ops->port_bridge_join)
114 		ds->ops->port_bridge_leave(ds, info->port, info->br);
115 
116 	if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
117 	    ds->ops->crosschip_bridge_join)
118 		ds->ops->crosschip_bridge_leave(ds, info->tree_index,
119 						info->sw_index, info->port,
120 						info->br);
121 
122 	/* If the bridge was vlan_filtering, the bridge core doesn't trigger an
123 	 * event for changing vlan_filtering setting upon slave ports leaving
124 	 * it. That is a good thing, because that lets us handle it and also
125 	 * handle the case where the switch's vlan_filtering setting is global
126 	 * (not per port). When that happens, the correct moment to trigger the
127 	 * vlan_filtering callback is only when the last port leaves the last
128 	 * VLAN-aware bridge.
129 	 */
130 	if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
131 		for (port = 0; port < ds->num_ports; port++) {
132 			struct net_device *bridge_dev;
133 
134 			bridge_dev = dsa_to_port(ds, port)->bridge_dev;
135 
136 			if (bridge_dev && br_vlan_enabled(bridge_dev)) {
137 				unset_vlan_filtering = false;
138 				break;
139 			}
140 		}
141 	}
142 	if (unset_vlan_filtering) {
143 		err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
144 					      false, &extack);
145 		if (extack._msg)
146 			dev_err(ds->dev, "port %d: %s\n", info->port,
147 				extack._msg);
148 		if (err && err != EOPNOTSUPP)
149 			return err;
150 	}
151 	return 0;
152 }
153 
154 static int dsa_switch_fdb_add(struct dsa_switch *ds,
155 			      struct dsa_notifier_fdb_info *info)
156 {
157 	int port = dsa_towards_port(ds, info->sw_index, info->port);
158 
159 	if (!ds->ops->port_fdb_add)
160 		return -EOPNOTSUPP;
161 
162 	return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
163 }
164 
165 static int dsa_switch_fdb_del(struct dsa_switch *ds,
166 			      struct dsa_notifier_fdb_info *info)
167 {
168 	int port = dsa_towards_port(ds, info->sw_index, info->port);
169 
170 	if (!ds->ops->port_fdb_del)
171 		return -EOPNOTSUPP;
172 
173 	return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
174 }
175 
176 static int dsa_switch_hsr_join(struct dsa_switch *ds,
177 			       struct dsa_notifier_hsr_info *info)
178 {
179 	if (ds->index == info->sw_index && ds->ops->port_hsr_join)
180 		return ds->ops->port_hsr_join(ds, info->port, info->hsr);
181 
182 	return -EOPNOTSUPP;
183 }
184 
185 static int dsa_switch_hsr_leave(struct dsa_switch *ds,
186 				struct dsa_notifier_hsr_info *info)
187 {
188 	if (ds->index == info->sw_index && ds->ops->port_hsr_leave)
189 		return ds->ops->port_hsr_leave(ds, info->port, info->hsr);
190 
191 	return -EOPNOTSUPP;
192 }
193 
194 static int dsa_switch_lag_change(struct dsa_switch *ds,
195 				 struct dsa_notifier_lag_info *info)
196 {
197 	if (ds->index == info->sw_index && ds->ops->port_lag_change)
198 		return ds->ops->port_lag_change(ds, info->port);
199 
200 	if (ds->index != info->sw_index && ds->ops->crosschip_lag_change)
201 		return ds->ops->crosschip_lag_change(ds, info->sw_index,
202 						     info->port);
203 
204 	return 0;
205 }
206 
207 static int dsa_switch_lag_join(struct dsa_switch *ds,
208 			       struct dsa_notifier_lag_info *info)
209 {
210 	if (ds->index == info->sw_index && ds->ops->port_lag_join)
211 		return ds->ops->port_lag_join(ds, info->port, info->lag,
212 					      info->info);
213 
214 	if (ds->index != info->sw_index && ds->ops->crosschip_lag_join)
215 		return ds->ops->crosschip_lag_join(ds, info->sw_index,
216 						   info->port, info->lag,
217 						   info->info);
218 
219 	return 0;
220 }
221 
222 static int dsa_switch_lag_leave(struct dsa_switch *ds,
223 				struct dsa_notifier_lag_info *info)
224 {
225 	if (ds->index == info->sw_index && ds->ops->port_lag_leave)
226 		return ds->ops->port_lag_leave(ds, info->port, info->lag);
227 
228 	if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave)
229 		return ds->ops->crosschip_lag_leave(ds, info->sw_index,
230 						    info->port, info->lag);
231 
232 	return 0;
233 }
234 
235 static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
236 				 struct dsa_notifier_mdb_info *info)
237 {
238 	if (ds->index == info->sw_index && port == info->port)
239 		return true;
240 
241 	if (dsa_is_dsa_port(ds, port))
242 		return true;
243 
244 	return false;
245 }
246 
247 static int dsa_switch_mdb_add(struct dsa_switch *ds,
248 			      struct dsa_notifier_mdb_info *info)
249 {
250 	int err = 0;
251 	int port;
252 
253 	if (!ds->ops->port_mdb_add)
254 		return -EOPNOTSUPP;
255 
256 	for (port = 0; port < ds->num_ports; port++) {
257 		if (dsa_switch_mdb_match(ds, port, info)) {
258 			err = ds->ops->port_mdb_add(ds, port, info->mdb);
259 			if (err)
260 				break;
261 		}
262 	}
263 
264 	return err;
265 }
266 
267 static int dsa_switch_mdb_del(struct dsa_switch *ds,
268 			      struct dsa_notifier_mdb_info *info)
269 {
270 	if (!ds->ops->port_mdb_del)
271 		return -EOPNOTSUPP;
272 
273 	if (ds->index == info->sw_index)
274 		return ds->ops->port_mdb_del(ds, info->port, info->mdb);
275 
276 	return 0;
277 }
278 
279 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
280 				  struct dsa_notifier_vlan_info *info)
281 {
282 	if (ds->index == info->sw_index && port == info->port)
283 		return true;
284 
285 	if (dsa_is_dsa_port(ds, port))
286 		return true;
287 
288 	return false;
289 }
290 
291 static int dsa_switch_vlan_add(struct dsa_switch *ds,
292 			       struct dsa_notifier_vlan_info *info)
293 {
294 	int port, err;
295 
296 	if (!ds->ops->port_vlan_add)
297 		return -EOPNOTSUPP;
298 
299 	for (port = 0; port < ds->num_ports; port++) {
300 		if (dsa_switch_vlan_match(ds, port, info)) {
301 			err = ds->ops->port_vlan_add(ds, port, info->vlan,
302 						     info->extack);
303 			if (err)
304 				return err;
305 		}
306 	}
307 
308 	return 0;
309 }
310 
311 static int dsa_switch_vlan_del(struct dsa_switch *ds,
312 			       struct dsa_notifier_vlan_info *info)
313 {
314 	if (!ds->ops->port_vlan_del)
315 		return -EOPNOTSUPP;
316 
317 	if (ds->index == info->sw_index)
318 		return ds->ops->port_vlan_del(ds, info->port, info->vlan);
319 
320 	/* Do not deprogram the DSA links as they may be used as conduit
321 	 * for other VLAN members in the fabric.
322 	 */
323 	return 0;
324 }
325 
326 static bool dsa_switch_tag_proto_match(struct dsa_switch *ds, int port,
327 				       struct dsa_notifier_tag_proto_info *info)
328 {
329 	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
330 		return true;
331 
332 	return false;
333 }
334 
335 static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
336 				       struct dsa_notifier_tag_proto_info *info)
337 {
338 	const struct dsa_device_ops *tag_ops = info->tag_ops;
339 	int port, err;
340 
341 	if (!ds->ops->change_tag_protocol)
342 		return -EOPNOTSUPP;
343 
344 	ASSERT_RTNL();
345 
346 	for (port = 0; port < ds->num_ports; port++) {
347 		if (dsa_switch_tag_proto_match(ds, port, info)) {
348 			err = ds->ops->change_tag_protocol(ds, port,
349 							   tag_ops->proto);
350 			if (err)
351 				return err;
352 
353 			if (dsa_is_cpu_port(ds, port))
354 				dsa_port_set_tag_protocol(dsa_to_port(ds, port),
355 							  tag_ops);
356 		}
357 	}
358 
359 	/* Now that changing the tag protocol can no longer fail, let's update
360 	 * the remaining bits which are "duplicated for faster access", and the
361 	 * bits that depend on the tagger, such as the MTU.
362 	 */
363 	for (port = 0; port < ds->num_ports; port++) {
364 		if (dsa_is_user_port(ds, port)) {
365 			struct net_device *slave;
366 
367 			slave = dsa_to_port(ds, port)->slave;
368 			dsa_slave_setup_tagger(slave);
369 
370 			/* rtnl_mutex is held in dsa_tree_change_tag_proto */
371 			dsa_slave_change_mtu(slave, slave->mtu);
372 		}
373 	}
374 
375 	return 0;
376 }
377 
378 static bool dsa_switch_mrp_match(struct dsa_switch *ds, int port,
379 				 struct dsa_notifier_mrp_info *info)
380 {
381 	if (ds->index == info->sw_index && port == info->port)
382 		return true;
383 
384 	if (dsa_is_dsa_port(ds, port))
385 		return true;
386 
387 	return false;
388 }
389 
390 static int dsa_switch_mrp_add(struct dsa_switch *ds,
391 			      struct dsa_notifier_mrp_info *info)
392 {
393 	int err = 0;
394 	int port;
395 
396 	if (!ds->ops->port_mrp_add)
397 		return -EOPNOTSUPP;
398 
399 	for (port = 0; port < ds->num_ports; port++) {
400 		if (dsa_switch_mrp_match(ds, port, info)) {
401 			err = ds->ops->port_mrp_add(ds, port, info->mrp);
402 			if (err)
403 				break;
404 		}
405 	}
406 
407 	return err;
408 }
409 
410 static int dsa_switch_mrp_del(struct dsa_switch *ds,
411 			      struct dsa_notifier_mrp_info *info)
412 {
413 	if (!ds->ops->port_mrp_del)
414 		return -EOPNOTSUPP;
415 
416 	if (ds->index == info->sw_index)
417 		return ds->ops->port_mrp_del(ds, info->port, info->mrp);
418 
419 	return 0;
420 }
421 
422 static bool
423 dsa_switch_mrp_ring_role_match(struct dsa_switch *ds, int port,
424 			       struct dsa_notifier_mrp_ring_role_info *info)
425 {
426 	if (ds->index == info->sw_index && port == info->port)
427 		return true;
428 
429 	if (dsa_is_dsa_port(ds, port))
430 		return true;
431 
432 	return false;
433 }
434 
435 static int
436 dsa_switch_mrp_add_ring_role(struct dsa_switch *ds,
437 			     struct dsa_notifier_mrp_ring_role_info *info)
438 {
439 	int err = 0;
440 	int port;
441 
442 	if (!ds->ops->port_mrp_add)
443 		return -EOPNOTSUPP;
444 
445 	for (port = 0; port < ds->num_ports; port++) {
446 		if (dsa_switch_mrp_ring_role_match(ds, port, info)) {
447 			err = ds->ops->port_mrp_add_ring_role(ds, port,
448 							      info->mrp);
449 			if (err)
450 				break;
451 		}
452 	}
453 
454 	return err;
455 }
456 
457 static int
458 dsa_switch_mrp_del_ring_role(struct dsa_switch *ds,
459 			     struct dsa_notifier_mrp_ring_role_info *info)
460 {
461 	if (!ds->ops->port_mrp_del)
462 		return -EOPNOTSUPP;
463 
464 	if (ds->index == info->sw_index)
465 		return ds->ops->port_mrp_del_ring_role(ds, info->port,
466 						       info->mrp);
467 
468 	return 0;
469 }
470 
471 static int dsa_switch_event(struct notifier_block *nb,
472 			    unsigned long event, void *info)
473 {
474 	struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
475 	int err;
476 
477 	switch (event) {
478 	case DSA_NOTIFIER_AGEING_TIME:
479 		err = dsa_switch_ageing_time(ds, info);
480 		break;
481 	case DSA_NOTIFIER_BRIDGE_JOIN:
482 		err = dsa_switch_bridge_join(ds, info);
483 		break;
484 	case DSA_NOTIFIER_BRIDGE_LEAVE:
485 		err = dsa_switch_bridge_leave(ds, info);
486 		break;
487 	case DSA_NOTIFIER_FDB_ADD:
488 		err = dsa_switch_fdb_add(ds, info);
489 		break;
490 	case DSA_NOTIFIER_FDB_DEL:
491 		err = dsa_switch_fdb_del(ds, info);
492 		break;
493 	case DSA_NOTIFIER_HSR_JOIN:
494 		err = dsa_switch_hsr_join(ds, info);
495 		break;
496 	case DSA_NOTIFIER_HSR_LEAVE:
497 		err = dsa_switch_hsr_leave(ds, info);
498 		break;
499 	case DSA_NOTIFIER_LAG_CHANGE:
500 		err = dsa_switch_lag_change(ds, info);
501 		break;
502 	case DSA_NOTIFIER_LAG_JOIN:
503 		err = dsa_switch_lag_join(ds, info);
504 		break;
505 	case DSA_NOTIFIER_LAG_LEAVE:
506 		err = dsa_switch_lag_leave(ds, info);
507 		break;
508 	case DSA_NOTIFIER_MDB_ADD:
509 		err = dsa_switch_mdb_add(ds, info);
510 		break;
511 	case DSA_NOTIFIER_MDB_DEL:
512 		err = dsa_switch_mdb_del(ds, info);
513 		break;
514 	case DSA_NOTIFIER_VLAN_ADD:
515 		err = dsa_switch_vlan_add(ds, info);
516 		break;
517 	case DSA_NOTIFIER_VLAN_DEL:
518 		err = dsa_switch_vlan_del(ds, info);
519 		break;
520 	case DSA_NOTIFIER_MTU:
521 		err = dsa_switch_mtu(ds, info);
522 		break;
523 	case DSA_NOTIFIER_TAG_PROTO:
524 		err = dsa_switch_change_tag_proto(ds, info);
525 		break;
526 	case DSA_NOTIFIER_MRP_ADD:
527 		err = dsa_switch_mrp_add(ds, info);
528 		break;
529 	case DSA_NOTIFIER_MRP_DEL:
530 		err = dsa_switch_mrp_del(ds, info);
531 		break;
532 	case DSA_NOTIFIER_MRP_ADD_RING_ROLE:
533 		err = dsa_switch_mrp_add_ring_role(ds, info);
534 		break;
535 	case DSA_NOTIFIER_MRP_DEL_RING_ROLE:
536 		err = dsa_switch_mrp_del_ring_role(ds, info);
537 		break;
538 	default:
539 		err = -EOPNOTSUPP;
540 		break;
541 	}
542 
543 	if (err)
544 		dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
545 			event, err);
546 
547 	return notifier_from_errno(err);
548 }
549 
550 int dsa_switch_register_notifier(struct dsa_switch *ds)
551 {
552 	ds->nb.notifier_call = dsa_switch_event;
553 
554 	return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
555 }
556 
557 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
558 {
559 	int err;
560 
561 	err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
562 	if (err)
563 		dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
564 }
565