xref: /openbmc/linux/net/dsa/switch.c (revision 134ef238)
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 	struct dsa_port *dp;
21 
22 	dsa_switch_for_each_port(dp, ds)
23 		if (dp->ageing_time && dp->ageing_time < ageing_time)
24 			ageing_time = dp->ageing_time;
25 
26 	return ageing_time;
27 }
28 
29 static int dsa_switch_ageing_time(struct dsa_switch *ds,
30 				  struct dsa_notifier_ageing_time_info *info)
31 {
32 	unsigned int ageing_time = info->ageing_time;
33 
34 	if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
35 		return -ERANGE;
36 
37 	if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
38 		return -ERANGE;
39 
40 	/* Program the fastest ageing time in case of multiple bridges */
41 	ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
42 
43 	if (ds->ops->set_ageing_time)
44 		return ds->ops->set_ageing_time(ds, ageing_time);
45 
46 	return 0;
47 }
48 
49 static bool dsa_port_mtu_match(struct dsa_port *dp,
50 			       struct dsa_notifier_mtu_info *info)
51 {
52 	if (dp->ds->index == info->sw_index && dp->index == info->port)
53 		return true;
54 
55 	/* Do not propagate to other switches in the tree if the notifier was
56 	 * targeted for a single switch.
57 	 */
58 	if (info->targeted_match)
59 		return false;
60 
61 	if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
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 	struct dsa_port *dp;
71 	int ret;
72 
73 	if (!ds->ops->port_change_mtu)
74 		return -EOPNOTSUPP;
75 
76 	dsa_switch_for_each_port(dp, ds) {
77 		if (dsa_port_mtu_match(dp, info)) {
78 			ret = ds->ops->port_change_mtu(ds, dp->index,
79 						       info->mtu);
80 			if (ret)
81 				return ret;
82 		}
83 	}
84 
85 	return 0;
86 }
87 
88 static int dsa_switch_bridge_join(struct dsa_switch *ds,
89 				  struct dsa_notifier_bridge_info *info)
90 {
91 	struct dsa_switch_tree *dst = ds->dst;
92 	int err;
93 
94 	if (dst->index == info->tree_index && ds->index == info->sw_index) {
95 		if (!ds->ops->port_bridge_join)
96 			return -EOPNOTSUPP;
97 
98 		err = ds->ops->port_bridge_join(ds, info->port, info->bridge,
99 						&info->tx_fwd_offload);
100 		if (err)
101 			return err;
102 	}
103 
104 	if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
105 	    ds->ops->crosschip_bridge_join) {
106 		err = ds->ops->crosschip_bridge_join(ds, info->tree_index,
107 						     info->sw_index,
108 						     info->port, info->bridge);
109 		if (err)
110 			return err;
111 	}
112 
113 	return dsa_tag_8021q_bridge_join(ds, info);
114 }
115 
116 static int dsa_switch_sync_vlan_filtering(struct dsa_switch *ds,
117 					  struct dsa_notifier_bridge_info *info)
118 {
119 	struct netlink_ext_ack extack = {0};
120 	bool change_vlan_filtering = false;
121 	bool vlan_filtering;
122 	struct dsa_port *dp;
123 	int err;
124 
125 	if (ds->needs_standalone_vlan_filtering &&
126 	    !br_vlan_enabled(info->bridge.dev)) {
127 		change_vlan_filtering = true;
128 		vlan_filtering = true;
129 	} else if (!ds->needs_standalone_vlan_filtering &&
130 		   br_vlan_enabled(info->bridge.dev)) {
131 		change_vlan_filtering = true;
132 		vlan_filtering = false;
133 	}
134 
135 	/* If the bridge was vlan_filtering, the bridge core doesn't trigger an
136 	 * event for changing vlan_filtering setting upon slave ports leaving
137 	 * it. That is a good thing, because that lets us handle it and also
138 	 * handle the case where the switch's vlan_filtering setting is global
139 	 * (not per port). When that happens, the correct moment to trigger the
140 	 * vlan_filtering callback is only when the last port leaves the last
141 	 * VLAN-aware bridge.
142 	 */
143 	if (change_vlan_filtering && ds->vlan_filtering_is_global) {
144 		dsa_switch_for_each_port(dp, ds) {
145 			struct net_device *br = dsa_port_bridge_dev_get(dp);
146 
147 			if (br && br_vlan_enabled(br)) {
148 				change_vlan_filtering = false;
149 				break;
150 			}
151 		}
152 	}
153 
154 	if (change_vlan_filtering) {
155 		err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
156 					      vlan_filtering, &extack);
157 		if (extack._msg)
158 			dev_err(ds->dev, "port %d: %s\n", info->port,
159 				extack._msg);
160 		if (err && err != -EOPNOTSUPP)
161 			return err;
162 	}
163 
164 	return 0;
165 }
166 
167 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
168 				   struct dsa_notifier_bridge_info *info)
169 {
170 	struct dsa_switch_tree *dst = ds->dst;
171 	int err;
172 
173 	if (dst->index == info->tree_index && ds->index == info->sw_index &&
174 	    ds->ops->port_bridge_leave)
175 		ds->ops->port_bridge_leave(ds, info->port, info->bridge);
176 
177 	if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
178 	    ds->ops->crosschip_bridge_leave)
179 		ds->ops->crosschip_bridge_leave(ds, info->tree_index,
180 						info->sw_index, info->port,
181 						info->bridge);
182 
183 	if (ds->dst->index == info->tree_index && ds->index == info->sw_index) {
184 		err = dsa_switch_sync_vlan_filtering(ds, info);
185 		if (err)
186 			return err;
187 	}
188 
189 	return dsa_tag_8021q_bridge_leave(ds, info);
190 }
191 
192 /* Matches for all upstream-facing ports (the CPU port and all upstream-facing
193  * DSA links) that sit between the targeted port on which the notifier was
194  * emitted and its dedicated CPU port.
195  */
196 static bool dsa_port_host_address_match(struct dsa_port *dp,
197 					int info_sw_index, int info_port)
198 {
199 	struct dsa_port *targeted_dp, *cpu_dp;
200 	struct dsa_switch *targeted_ds;
201 
202 	targeted_ds = dsa_switch_find(dp->ds->dst->index, info_sw_index);
203 	targeted_dp = dsa_to_port(targeted_ds, info_port);
204 	cpu_dp = targeted_dp->cpu_dp;
205 
206 	if (dsa_switch_is_upstream_of(dp->ds, targeted_ds))
207 		return dp->index == dsa_towards_port(dp->ds, cpu_dp->ds->index,
208 						     cpu_dp->index);
209 
210 	return false;
211 }
212 
213 static struct dsa_mac_addr *dsa_mac_addr_find(struct list_head *addr_list,
214 					      const unsigned char *addr,
215 					      u16 vid)
216 {
217 	struct dsa_mac_addr *a;
218 
219 	list_for_each_entry(a, addr_list, list)
220 		if (ether_addr_equal(a->addr, addr) && a->vid == vid)
221 			return a;
222 
223 	return NULL;
224 }
225 
226 static int dsa_port_do_mdb_add(struct dsa_port *dp,
227 			       const struct switchdev_obj_port_mdb *mdb)
228 {
229 	struct dsa_switch *ds = dp->ds;
230 	struct dsa_mac_addr *a;
231 	int port = dp->index;
232 	int err = 0;
233 
234 	/* No need to bother with refcounting for user ports */
235 	if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
236 		return ds->ops->port_mdb_add(ds, port, mdb);
237 
238 	mutex_lock(&dp->addr_lists_lock);
239 
240 	a = dsa_mac_addr_find(&dp->mdbs, mdb->addr, mdb->vid);
241 	if (a) {
242 		refcount_inc(&a->refcount);
243 		goto out;
244 	}
245 
246 	a = kzalloc(sizeof(*a), GFP_KERNEL);
247 	if (!a) {
248 		err = -ENOMEM;
249 		goto out;
250 	}
251 
252 	err = ds->ops->port_mdb_add(ds, port, mdb);
253 	if (err) {
254 		kfree(a);
255 		goto out;
256 	}
257 
258 	ether_addr_copy(a->addr, mdb->addr);
259 	a->vid = mdb->vid;
260 	refcount_set(&a->refcount, 1);
261 	list_add_tail(&a->list, &dp->mdbs);
262 
263 out:
264 	mutex_unlock(&dp->addr_lists_lock);
265 
266 	return err;
267 }
268 
269 static int dsa_port_do_mdb_del(struct dsa_port *dp,
270 			       const struct switchdev_obj_port_mdb *mdb)
271 {
272 	struct dsa_switch *ds = dp->ds;
273 	struct dsa_mac_addr *a;
274 	int port = dp->index;
275 	int err = 0;
276 
277 	/* No need to bother with refcounting for user ports */
278 	if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
279 		return ds->ops->port_mdb_del(ds, port, mdb);
280 
281 	mutex_lock(&dp->addr_lists_lock);
282 
283 	a = dsa_mac_addr_find(&dp->mdbs, mdb->addr, mdb->vid);
284 	if (!a) {
285 		err = -ENOENT;
286 		goto out;
287 	}
288 
289 	if (!refcount_dec_and_test(&a->refcount))
290 		goto out;
291 
292 	err = ds->ops->port_mdb_del(ds, port, mdb);
293 	if (err) {
294 		refcount_set(&a->refcount, 1);
295 		goto out;
296 	}
297 
298 	list_del(&a->list);
299 	kfree(a);
300 
301 out:
302 	mutex_unlock(&dp->addr_lists_lock);
303 
304 	return err;
305 }
306 
307 static int dsa_port_do_fdb_add(struct dsa_port *dp, const unsigned char *addr,
308 			       u16 vid)
309 {
310 	struct dsa_switch *ds = dp->ds;
311 	struct dsa_mac_addr *a;
312 	int port = dp->index;
313 	int err = 0;
314 
315 	/* No need to bother with refcounting for user ports */
316 	if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
317 		return ds->ops->port_fdb_add(ds, port, addr, vid);
318 
319 	mutex_lock(&dp->addr_lists_lock);
320 
321 	a = dsa_mac_addr_find(&dp->fdbs, addr, vid);
322 	if (a) {
323 		refcount_inc(&a->refcount);
324 		goto out;
325 	}
326 
327 	a = kzalloc(sizeof(*a), GFP_KERNEL);
328 	if (!a) {
329 		err = -ENOMEM;
330 		goto out;
331 	}
332 
333 	err = ds->ops->port_fdb_add(ds, port, addr, vid);
334 	if (err) {
335 		kfree(a);
336 		goto out;
337 	}
338 
339 	ether_addr_copy(a->addr, addr);
340 	a->vid = vid;
341 	refcount_set(&a->refcount, 1);
342 	list_add_tail(&a->list, &dp->fdbs);
343 
344 out:
345 	mutex_unlock(&dp->addr_lists_lock);
346 
347 	return err;
348 }
349 
350 static int dsa_port_do_fdb_del(struct dsa_port *dp, const unsigned char *addr,
351 			       u16 vid)
352 {
353 	struct dsa_switch *ds = dp->ds;
354 	struct dsa_mac_addr *a;
355 	int port = dp->index;
356 	int err = 0;
357 
358 	/* No need to bother with refcounting for user ports */
359 	if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
360 		return ds->ops->port_fdb_del(ds, port, addr, vid);
361 
362 	mutex_lock(&dp->addr_lists_lock);
363 
364 	a = dsa_mac_addr_find(&dp->fdbs, addr, vid);
365 	if (!a) {
366 		err = -ENOENT;
367 		goto out;
368 	}
369 
370 	if (!refcount_dec_and_test(&a->refcount))
371 		goto out;
372 
373 	err = ds->ops->port_fdb_del(ds, port, addr, vid);
374 	if (err) {
375 		refcount_set(&a->refcount, 1);
376 		goto out;
377 	}
378 
379 	list_del(&a->list);
380 	kfree(a);
381 
382 out:
383 	mutex_unlock(&dp->addr_lists_lock);
384 
385 	return err;
386 }
387 
388 static int dsa_switch_host_fdb_add(struct dsa_switch *ds,
389 				   struct dsa_notifier_fdb_info *info)
390 {
391 	struct dsa_port *dp;
392 	int err = 0;
393 
394 	if (!ds->ops->port_fdb_add)
395 		return -EOPNOTSUPP;
396 
397 	dsa_switch_for_each_port(dp, ds) {
398 		if (dsa_port_host_address_match(dp, info->sw_index,
399 						info->port)) {
400 			err = dsa_port_do_fdb_add(dp, info->addr, info->vid);
401 			if (err)
402 				break;
403 		}
404 	}
405 
406 	return err;
407 }
408 
409 static int dsa_switch_host_fdb_del(struct dsa_switch *ds,
410 				   struct dsa_notifier_fdb_info *info)
411 {
412 	struct dsa_port *dp;
413 	int err = 0;
414 
415 	if (!ds->ops->port_fdb_del)
416 		return -EOPNOTSUPP;
417 
418 	dsa_switch_for_each_port(dp, ds) {
419 		if (dsa_port_host_address_match(dp, info->sw_index,
420 						info->port)) {
421 			err = dsa_port_do_fdb_del(dp, info->addr, info->vid);
422 			if (err)
423 				break;
424 		}
425 	}
426 
427 	return err;
428 }
429 
430 static int dsa_switch_fdb_add(struct dsa_switch *ds,
431 			      struct dsa_notifier_fdb_info *info)
432 {
433 	int port = dsa_towards_port(ds, info->sw_index, info->port);
434 	struct dsa_port *dp = dsa_to_port(ds, port);
435 
436 	if (!ds->ops->port_fdb_add)
437 		return -EOPNOTSUPP;
438 
439 	return dsa_port_do_fdb_add(dp, info->addr, info->vid);
440 }
441 
442 static int dsa_switch_fdb_del(struct dsa_switch *ds,
443 			      struct dsa_notifier_fdb_info *info)
444 {
445 	int port = dsa_towards_port(ds, info->sw_index, info->port);
446 	struct dsa_port *dp = dsa_to_port(ds, port);
447 
448 	if (!ds->ops->port_fdb_del)
449 		return -EOPNOTSUPP;
450 
451 	return dsa_port_do_fdb_del(dp, info->addr, info->vid);
452 }
453 
454 static int dsa_switch_lag_change(struct dsa_switch *ds,
455 				 struct dsa_notifier_lag_info *info)
456 {
457 	if (ds->index == info->sw_index && ds->ops->port_lag_change)
458 		return ds->ops->port_lag_change(ds, info->port);
459 
460 	if (ds->index != info->sw_index && ds->ops->crosschip_lag_change)
461 		return ds->ops->crosschip_lag_change(ds, info->sw_index,
462 						     info->port);
463 
464 	return 0;
465 }
466 
467 static int dsa_switch_lag_join(struct dsa_switch *ds,
468 			       struct dsa_notifier_lag_info *info)
469 {
470 	if (ds->index == info->sw_index && ds->ops->port_lag_join)
471 		return ds->ops->port_lag_join(ds, info->port, info->lag,
472 					      info->info);
473 
474 	if (ds->index != info->sw_index && ds->ops->crosschip_lag_join)
475 		return ds->ops->crosschip_lag_join(ds, info->sw_index,
476 						   info->port, info->lag,
477 						   info->info);
478 
479 	return -EOPNOTSUPP;
480 }
481 
482 static int dsa_switch_lag_leave(struct dsa_switch *ds,
483 				struct dsa_notifier_lag_info *info)
484 {
485 	if (ds->index == info->sw_index && ds->ops->port_lag_leave)
486 		return ds->ops->port_lag_leave(ds, info->port, info->lag);
487 
488 	if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave)
489 		return ds->ops->crosschip_lag_leave(ds, info->sw_index,
490 						    info->port, info->lag);
491 
492 	return -EOPNOTSUPP;
493 }
494 
495 static int dsa_switch_mdb_add(struct dsa_switch *ds,
496 			      struct dsa_notifier_mdb_info *info)
497 {
498 	int port = dsa_towards_port(ds, info->sw_index, info->port);
499 	struct dsa_port *dp = dsa_to_port(ds, port);
500 
501 	if (!ds->ops->port_mdb_add)
502 		return -EOPNOTSUPP;
503 
504 	return dsa_port_do_mdb_add(dp, info->mdb);
505 }
506 
507 static int dsa_switch_mdb_del(struct dsa_switch *ds,
508 			      struct dsa_notifier_mdb_info *info)
509 {
510 	int port = dsa_towards_port(ds, info->sw_index, info->port);
511 	struct dsa_port *dp = dsa_to_port(ds, port);
512 
513 	if (!ds->ops->port_mdb_del)
514 		return -EOPNOTSUPP;
515 
516 	return dsa_port_do_mdb_del(dp, info->mdb);
517 }
518 
519 static int dsa_switch_host_mdb_add(struct dsa_switch *ds,
520 				   struct dsa_notifier_mdb_info *info)
521 {
522 	struct dsa_port *dp;
523 	int err = 0;
524 
525 	if (!ds->ops->port_mdb_add)
526 		return -EOPNOTSUPP;
527 
528 	dsa_switch_for_each_port(dp, ds) {
529 		if (dsa_port_host_address_match(dp, info->sw_index,
530 						info->port)) {
531 			err = dsa_port_do_mdb_add(dp, info->mdb);
532 			if (err)
533 				break;
534 		}
535 	}
536 
537 	return err;
538 }
539 
540 static int dsa_switch_host_mdb_del(struct dsa_switch *ds,
541 				   struct dsa_notifier_mdb_info *info)
542 {
543 	struct dsa_port *dp;
544 	int err = 0;
545 
546 	if (!ds->ops->port_mdb_del)
547 		return -EOPNOTSUPP;
548 
549 	dsa_switch_for_each_port(dp, ds) {
550 		if (dsa_port_host_address_match(dp, info->sw_index,
551 						info->port)) {
552 			err = dsa_port_do_mdb_del(dp, info->mdb);
553 			if (err)
554 				break;
555 		}
556 	}
557 
558 	return err;
559 }
560 
561 /* Port VLANs match on the targeted port and on all DSA ports */
562 static bool dsa_port_vlan_match(struct dsa_port *dp,
563 				struct dsa_notifier_vlan_info *info)
564 {
565 	if (dp->ds->index == info->sw_index && dp->index == info->port)
566 		return true;
567 
568 	if (dsa_port_is_dsa(dp))
569 		return true;
570 
571 	return false;
572 }
573 
574 /* Host VLANs match on the targeted port's CPU port, and on all DSA ports
575  * (upstream and downstream) of that switch and its upstream switches.
576  */
577 static bool dsa_port_host_vlan_match(struct dsa_port *dp,
578 				     struct dsa_notifier_vlan_info *info)
579 {
580 	struct dsa_port *targeted_dp, *cpu_dp;
581 	struct dsa_switch *targeted_ds;
582 
583 	targeted_ds = dsa_switch_find(dp->ds->dst->index, info->sw_index);
584 	targeted_dp = dsa_to_port(targeted_ds, info->port);
585 	cpu_dp = targeted_dp->cpu_dp;
586 
587 	if (dsa_switch_is_upstream_of(dp->ds, targeted_ds))
588 		return dsa_port_is_dsa(dp) || dp == cpu_dp;
589 
590 	return false;
591 }
592 
593 static struct dsa_vlan *dsa_vlan_find(struct list_head *vlan_list,
594 				      const struct switchdev_obj_port_vlan *vlan)
595 {
596 	struct dsa_vlan *v;
597 
598 	list_for_each_entry(v, vlan_list, list)
599 		if (v->vid == vlan->vid)
600 			return v;
601 
602 	return NULL;
603 }
604 
605 static int dsa_port_do_vlan_add(struct dsa_port *dp,
606 				const struct switchdev_obj_port_vlan *vlan,
607 				struct netlink_ext_ack *extack)
608 {
609 	struct dsa_switch *ds = dp->ds;
610 	int port = dp->index;
611 	struct dsa_vlan *v;
612 	int err = 0;
613 
614 	/* No need to bother with refcounting for user ports. */
615 	if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
616 		return ds->ops->port_vlan_add(ds, port, vlan, extack);
617 
618 	/* No need to propagate on shared ports the existing VLANs that were
619 	 * re-notified after just the flags have changed. This would cause a
620 	 * refcount bump which we need to avoid, since it unbalances the
621 	 * additions with the deletions.
622 	 */
623 	if (vlan->changed)
624 		return 0;
625 
626 	mutex_lock(&dp->vlans_lock);
627 
628 	v = dsa_vlan_find(&dp->vlans, vlan);
629 	if (v) {
630 		refcount_inc(&v->refcount);
631 		goto out;
632 	}
633 
634 	v = kzalloc(sizeof(*v), GFP_KERNEL);
635 	if (!v) {
636 		err = -ENOMEM;
637 		goto out;
638 	}
639 
640 	err = ds->ops->port_vlan_add(ds, port, vlan, extack);
641 	if (err) {
642 		kfree(v);
643 		goto out;
644 	}
645 
646 	v->vid = vlan->vid;
647 	refcount_set(&v->refcount, 1);
648 	list_add_tail(&v->list, &dp->vlans);
649 
650 out:
651 	mutex_unlock(&dp->vlans_lock);
652 
653 	return err;
654 }
655 
656 static int dsa_port_do_vlan_del(struct dsa_port *dp,
657 				const struct switchdev_obj_port_vlan *vlan)
658 {
659 	struct dsa_switch *ds = dp->ds;
660 	int port = dp->index;
661 	struct dsa_vlan *v;
662 	int err = 0;
663 
664 	/* No need to bother with refcounting for user ports */
665 	if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
666 		return ds->ops->port_vlan_del(ds, port, vlan);
667 
668 	mutex_lock(&dp->vlans_lock);
669 
670 	v = dsa_vlan_find(&dp->vlans, vlan);
671 	if (!v) {
672 		err = -ENOENT;
673 		goto out;
674 	}
675 
676 	if (!refcount_dec_and_test(&v->refcount))
677 		goto out;
678 
679 	err = ds->ops->port_vlan_del(ds, port, vlan);
680 	if (err) {
681 		refcount_set(&v->refcount, 1);
682 		goto out;
683 	}
684 
685 	list_del(&v->list);
686 	kfree(v);
687 
688 out:
689 	mutex_unlock(&dp->vlans_lock);
690 
691 	return err;
692 }
693 
694 static int dsa_switch_vlan_add(struct dsa_switch *ds,
695 			       struct dsa_notifier_vlan_info *info)
696 {
697 	struct dsa_port *dp;
698 	int err;
699 
700 	if (!ds->ops->port_vlan_add)
701 		return -EOPNOTSUPP;
702 
703 	dsa_switch_for_each_port(dp, ds) {
704 		if (dsa_port_vlan_match(dp, info)) {
705 			err = dsa_port_do_vlan_add(dp, info->vlan,
706 						   info->extack);
707 			if (err)
708 				return err;
709 		}
710 	}
711 
712 	return 0;
713 }
714 
715 static int dsa_switch_vlan_del(struct dsa_switch *ds,
716 			       struct dsa_notifier_vlan_info *info)
717 {
718 	struct dsa_port *dp;
719 	int err;
720 
721 	if (!ds->ops->port_vlan_del)
722 		return -EOPNOTSUPP;
723 
724 	dsa_switch_for_each_port(dp, ds) {
725 		if (dsa_port_vlan_match(dp, info)) {
726 			err = dsa_port_do_vlan_del(dp, info->vlan);
727 			if (err)
728 				return err;
729 		}
730 	}
731 
732 	return 0;
733 }
734 
735 static int dsa_switch_host_vlan_add(struct dsa_switch *ds,
736 				    struct dsa_notifier_vlan_info *info)
737 {
738 	struct dsa_port *dp;
739 	int err;
740 
741 	if (!ds->ops->port_vlan_add)
742 		return -EOPNOTSUPP;
743 
744 	dsa_switch_for_each_port(dp, ds) {
745 		if (dsa_port_host_vlan_match(dp, info)) {
746 			err = dsa_port_do_vlan_add(dp, info->vlan,
747 						   info->extack);
748 			if (err)
749 				return err;
750 		}
751 	}
752 
753 	return 0;
754 }
755 
756 static int dsa_switch_host_vlan_del(struct dsa_switch *ds,
757 				    struct dsa_notifier_vlan_info *info)
758 {
759 	struct dsa_port *dp;
760 	int err;
761 
762 	if (!ds->ops->port_vlan_del)
763 		return -EOPNOTSUPP;
764 
765 	dsa_switch_for_each_port(dp, ds) {
766 		if (dsa_port_host_vlan_match(dp, info)) {
767 			err = dsa_port_do_vlan_del(dp, info->vlan);
768 			if (err)
769 				return err;
770 		}
771 	}
772 
773 	return 0;
774 }
775 
776 static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
777 				       struct dsa_notifier_tag_proto_info *info)
778 {
779 	const struct dsa_device_ops *tag_ops = info->tag_ops;
780 	struct dsa_port *dp, *cpu_dp;
781 	int err;
782 
783 	if (!ds->ops->change_tag_protocol)
784 		return -EOPNOTSUPP;
785 
786 	ASSERT_RTNL();
787 
788 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
789 		err = ds->ops->change_tag_protocol(ds, cpu_dp->index,
790 						   tag_ops->proto);
791 		if (err)
792 			return err;
793 
794 		dsa_port_set_tag_protocol(cpu_dp, tag_ops);
795 	}
796 
797 	/* Now that changing the tag protocol can no longer fail, let's update
798 	 * the remaining bits which are "duplicated for faster access", and the
799 	 * bits that depend on the tagger, such as the MTU.
800 	 */
801 	dsa_switch_for_each_user_port(dp, ds) {
802 		struct net_device *slave = dp->slave;
803 
804 		dsa_slave_setup_tagger(slave);
805 
806 		/* rtnl_mutex is held in dsa_tree_change_tag_proto */
807 		dsa_slave_change_mtu(slave, slave->mtu);
808 	}
809 
810 	return 0;
811 }
812 
813 /* We use the same cross-chip notifiers to inform both the tagger side, as well
814  * as the switch side, of connection and disconnection events.
815  * Since ds->tagger_data is owned by the tagger, it isn't a hard error if the
816  * switch side doesn't support connecting to this tagger, and therefore, the
817  * fact that we don't disconnect the tagger side doesn't constitute a memory
818  * leak: the tagger will still operate with persistent per-switch memory, just
819  * with the switch side unconnected to it. What does constitute a hard error is
820  * when the switch side supports connecting but fails.
821  */
822 static int
823 dsa_switch_connect_tag_proto(struct dsa_switch *ds,
824 			     struct dsa_notifier_tag_proto_info *info)
825 {
826 	const struct dsa_device_ops *tag_ops = info->tag_ops;
827 	int err;
828 
829 	/* Notify the new tagger about the connection to this switch */
830 	if (tag_ops->connect) {
831 		err = tag_ops->connect(ds);
832 		if (err)
833 			return err;
834 	}
835 
836 	if (!ds->ops->connect_tag_protocol)
837 		return -EOPNOTSUPP;
838 
839 	/* Notify the switch about the connection to the new tagger */
840 	err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
841 	if (err) {
842 		/* Revert the new tagger's connection to this tree */
843 		if (tag_ops->disconnect)
844 			tag_ops->disconnect(ds);
845 		return err;
846 	}
847 
848 	return 0;
849 }
850 
851 static int
852 dsa_switch_disconnect_tag_proto(struct dsa_switch *ds,
853 				struct dsa_notifier_tag_proto_info *info)
854 {
855 	const struct dsa_device_ops *tag_ops = info->tag_ops;
856 
857 	/* Notify the tagger about the disconnection from this switch */
858 	if (tag_ops->disconnect && ds->tagger_data)
859 		tag_ops->disconnect(ds);
860 
861 	/* No need to notify the switch, since it shouldn't have any
862 	 * resources to tear down
863 	 */
864 	return 0;
865 }
866 
867 static int
868 dsa_switch_master_state_change(struct dsa_switch *ds,
869 			       struct dsa_notifier_master_state_info *info)
870 {
871 	if (!ds->ops->master_state_change)
872 		return 0;
873 
874 	ds->ops->master_state_change(ds, info->master, info->operational);
875 
876 	return 0;
877 }
878 
879 static int dsa_switch_event(struct notifier_block *nb,
880 			    unsigned long event, void *info)
881 {
882 	struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
883 	int err;
884 
885 	switch (event) {
886 	case DSA_NOTIFIER_AGEING_TIME:
887 		err = dsa_switch_ageing_time(ds, info);
888 		break;
889 	case DSA_NOTIFIER_BRIDGE_JOIN:
890 		err = dsa_switch_bridge_join(ds, info);
891 		break;
892 	case DSA_NOTIFIER_BRIDGE_LEAVE:
893 		err = dsa_switch_bridge_leave(ds, info);
894 		break;
895 	case DSA_NOTIFIER_FDB_ADD:
896 		err = dsa_switch_fdb_add(ds, info);
897 		break;
898 	case DSA_NOTIFIER_FDB_DEL:
899 		err = dsa_switch_fdb_del(ds, info);
900 		break;
901 	case DSA_NOTIFIER_HOST_FDB_ADD:
902 		err = dsa_switch_host_fdb_add(ds, info);
903 		break;
904 	case DSA_NOTIFIER_HOST_FDB_DEL:
905 		err = dsa_switch_host_fdb_del(ds, info);
906 		break;
907 	case DSA_NOTIFIER_LAG_CHANGE:
908 		err = dsa_switch_lag_change(ds, info);
909 		break;
910 	case DSA_NOTIFIER_LAG_JOIN:
911 		err = dsa_switch_lag_join(ds, info);
912 		break;
913 	case DSA_NOTIFIER_LAG_LEAVE:
914 		err = dsa_switch_lag_leave(ds, info);
915 		break;
916 	case DSA_NOTIFIER_MDB_ADD:
917 		err = dsa_switch_mdb_add(ds, info);
918 		break;
919 	case DSA_NOTIFIER_MDB_DEL:
920 		err = dsa_switch_mdb_del(ds, info);
921 		break;
922 	case DSA_NOTIFIER_HOST_MDB_ADD:
923 		err = dsa_switch_host_mdb_add(ds, info);
924 		break;
925 	case DSA_NOTIFIER_HOST_MDB_DEL:
926 		err = dsa_switch_host_mdb_del(ds, info);
927 		break;
928 	case DSA_NOTIFIER_VLAN_ADD:
929 		err = dsa_switch_vlan_add(ds, info);
930 		break;
931 	case DSA_NOTIFIER_VLAN_DEL:
932 		err = dsa_switch_vlan_del(ds, info);
933 		break;
934 	case DSA_NOTIFIER_HOST_VLAN_ADD:
935 		err = dsa_switch_host_vlan_add(ds, info);
936 		break;
937 	case DSA_NOTIFIER_HOST_VLAN_DEL:
938 		err = dsa_switch_host_vlan_del(ds, info);
939 		break;
940 	case DSA_NOTIFIER_MTU:
941 		err = dsa_switch_mtu(ds, info);
942 		break;
943 	case DSA_NOTIFIER_TAG_PROTO:
944 		err = dsa_switch_change_tag_proto(ds, info);
945 		break;
946 	case DSA_NOTIFIER_TAG_PROTO_CONNECT:
947 		err = dsa_switch_connect_tag_proto(ds, info);
948 		break;
949 	case DSA_NOTIFIER_TAG_PROTO_DISCONNECT:
950 		err = dsa_switch_disconnect_tag_proto(ds, info);
951 		break;
952 	case DSA_NOTIFIER_TAG_8021Q_VLAN_ADD:
953 		err = dsa_switch_tag_8021q_vlan_add(ds, info);
954 		break;
955 	case DSA_NOTIFIER_TAG_8021Q_VLAN_DEL:
956 		err = dsa_switch_tag_8021q_vlan_del(ds, info);
957 		break;
958 	case DSA_NOTIFIER_MASTER_STATE_CHANGE:
959 		err = dsa_switch_master_state_change(ds, info);
960 		break;
961 	default:
962 		err = -EOPNOTSUPP;
963 		break;
964 	}
965 
966 	if (err)
967 		dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
968 			event, err);
969 
970 	return notifier_from_errno(err);
971 }
972 
973 int dsa_switch_register_notifier(struct dsa_switch *ds)
974 {
975 	ds->nb.notifier_call = dsa_switch_event;
976 
977 	return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
978 }
979 
980 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
981 {
982 	int err;
983 
984 	err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
985 	if (err)
986 		dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
987 }
988