xref: /openbmc/linux/net/dsa/port.c (revision bfa87ac8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Handling of a single switch port
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/of_mdio.h>
13 #include <linux/of_net.h>
14 
15 #include "dsa_priv.h"
16 
17 /**
18  * dsa_port_notify - Notify the switching fabric of changes to a port
19  * @dp: port on which change occurred
20  * @e: event, must be of type DSA_NOTIFIER_*
21  * @v: event-specific value.
22  *
23  * Notify all switches in the DSA tree that this port's switch belongs to,
24  * including this switch itself, of an event. Allows the other switches to
25  * reconfigure themselves for cross-chip operations. Can also be used to
26  * reconfigure ports without net_devices (CPU ports, DSA links) whenever
27  * a user port's state changes.
28  */
29 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
30 {
31 	return dsa_tree_notify(dp->ds->dst, e, v);
32 }
33 
34 static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port *dp, u16 vid)
35 {
36 	struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
37 	struct switchdev_notifier_fdb_info info = {
38 		.vid = vid,
39 	};
40 
41 	/* When the port becomes standalone it has already left the bridge.
42 	 * Don't notify the bridge in that case.
43 	 */
44 	if (!brport_dev)
45 		return;
46 
47 	call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
48 				 brport_dev, &info.info, NULL);
49 }
50 
51 static void dsa_port_fast_age(const struct dsa_port *dp)
52 {
53 	struct dsa_switch *ds = dp->ds;
54 
55 	if (!ds->ops->port_fast_age)
56 		return;
57 
58 	ds->ops->port_fast_age(ds, dp->index);
59 
60 	/* flush all VLANs */
61 	dsa_port_notify_bridge_fdb_flush(dp, 0);
62 }
63 
64 static int dsa_port_vlan_fast_age(const struct dsa_port *dp, u16 vid)
65 {
66 	struct dsa_switch *ds = dp->ds;
67 	int err;
68 
69 	if (!ds->ops->port_vlan_fast_age)
70 		return -EOPNOTSUPP;
71 
72 	err = ds->ops->port_vlan_fast_age(ds, dp->index, vid);
73 
74 	if (!err)
75 		dsa_port_notify_bridge_fdb_flush(dp, vid);
76 
77 	return err;
78 }
79 
80 static int dsa_port_msti_fast_age(const struct dsa_port *dp, u16 msti)
81 {
82 	DECLARE_BITMAP(vids, VLAN_N_VID) = { 0 };
83 	int err, vid;
84 
85 	err = br_mst_get_info(dsa_port_bridge_dev_get(dp), msti, vids);
86 	if (err)
87 		return err;
88 
89 	for_each_set_bit(vid, vids, VLAN_N_VID) {
90 		err = dsa_port_vlan_fast_age(dp, vid);
91 		if (err)
92 			return err;
93 	}
94 
95 	return 0;
96 }
97 
98 static bool dsa_port_can_configure_learning(struct dsa_port *dp)
99 {
100 	struct switchdev_brport_flags flags = {
101 		.mask = BR_LEARNING,
102 	};
103 	struct dsa_switch *ds = dp->ds;
104 	int err;
105 
106 	if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags)
107 		return false;
108 
109 	err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL);
110 	return !err;
111 }
112 
113 bool dsa_port_supports_hwtstamp(struct dsa_port *dp, struct ifreq *ifr)
114 {
115 	struct dsa_switch *ds = dp->ds;
116 	int err;
117 
118 	if (!ds->ops->port_hwtstamp_get || !ds->ops->port_hwtstamp_set)
119 		return false;
120 
121 	/* "See through" shim implementations of the "get" method.
122 	 * This will clobber the ifreq structure, but we will either return an
123 	 * error, or the master will overwrite it with proper values.
124 	 */
125 	err = ds->ops->port_hwtstamp_get(ds, dp->index, ifr);
126 	return err != -EOPNOTSUPP;
127 }
128 
129 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
130 {
131 	struct dsa_switch *ds = dp->ds;
132 	int port = dp->index;
133 
134 	if (!ds->ops->port_stp_state_set)
135 		return -EOPNOTSUPP;
136 
137 	ds->ops->port_stp_state_set(ds, port, state);
138 
139 	if (!dsa_port_can_configure_learning(dp) ||
140 	    (do_fast_age && dp->learning)) {
141 		/* Fast age FDB entries or flush appropriate forwarding database
142 		 * for the given port, if we are moving it from Learning or
143 		 * Forwarding state, to Disabled or Blocking or Listening state.
144 		 * Ports that were standalone before the STP state change don't
145 		 * need to fast age the FDB, since address learning is off in
146 		 * standalone mode.
147 		 */
148 
149 		if ((dp->stp_state == BR_STATE_LEARNING ||
150 		     dp->stp_state == BR_STATE_FORWARDING) &&
151 		    (state == BR_STATE_DISABLED ||
152 		     state == BR_STATE_BLOCKING ||
153 		     state == BR_STATE_LISTENING))
154 			dsa_port_fast_age(dp);
155 	}
156 
157 	dp->stp_state = state;
158 
159 	return 0;
160 }
161 
162 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
163 				   bool do_fast_age)
164 {
165 	struct dsa_switch *ds = dp->ds;
166 	int err;
167 
168 	err = dsa_port_set_state(dp, state, do_fast_age);
169 	if (err && err != -EOPNOTSUPP) {
170 		dev_err(ds->dev, "port %d failed to set STP state %u: %pe\n",
171 			dp->index, state, ERR_PTR(err));
172 	}
173 }
174 
175 int dsa_port_set_mst_state(struct dsa_port *dp,
176 			   const struct switchdev_mst_state *state,
177 			   struct netlink_ext_ack *extack)
178 {
179 	struct dsa_switch *ds = dp->ds;
180 	u8 prev_state;
181 	int err;
182 
183 	if (!ds->ops->port_mst_state_set)
184 		return -EOPNOTSUPP;
185 
186 	err = br_mst_get_state(dsa_port_to_bridge_port(dp), state->msti,
187 			       &prev_state);
188 	if (err)
189 		return err;
190 
191 	err = ds->ops->port_mst_state_set(ds, dp->index, state);
192 	if (err)
193 		return err;
194 
195 	if (!(dp->learning &&
196 	      (prev_state == BR_STATE_LEARNING ||
197 	       prev_state == BR_STATE_FORWARDING) &&
198 	      (state->state == BR_STATE_DISABLED ||
199 	       state->state == BR_STATE_BLOCKING ||
200 	       state->state == BR_STATE_LISTENING)))
201 		return 0;
202 
203 	err = dsa_port_msti_fast_age(dp, state->msti);
204 	if (err)
205 		NL_SET_ERR_MSG_MOD(extack,
206 				   "Unable to flush associated VLANs");
207 
208 	return 0;
209 }
210 
211 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
212 {
213 	struct dsa_switch *ds = dp->ds;
214 	int port = dp->index;
215 	int err;
216 
217 	if (ds->ops->port_enable) {
218 		err = ds->ops->port_enable(ds, port, phy);
219 		if (err)
220 			return err;
221 	}
222 
223 	if (!dp->bridge)
224 		dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
225 
226 	if (dp->pl)
227 		phylink_start(dp->pl);
228 
229 	return 0;
230 }
231 
232 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
233 {
234 	int err;
235 
236 	rtnl_lock();
237 	err = dsa_port_enable_rt(dp, phy);
238 	rtnl_unlock();
239 
240 	return err;
241 }
242 
243 void dsa_port_disable_rt(struct dsa_port *dp)
244 {
245 	struct dsa_switch *ds = dp->ds;
246 	int port = dp->index;
247 
248 	if (dp->pl)
249 		phylink_stop(dp->pl);
250 
251 	if (!dp->bridge)
252 		dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
253 
254 	if (ds->ops->port_disable)
255 		ds->ops->port_disable(ds, port);
256 }
257 
258 void dsa_port_disable(struct dsa_port *dp)
259 {
260 	rtnl_lock();
261 	dsa_port_disable_rt(dp);
262 	rtnl_unlock();
263 }
264 
265 static void dsa_port_reset_vlan_filtering(struct dsa_port *dp,
266 					  struct dsa_bridge bridge)
267 {
268 	struct netlink_ext_ack extack = {0};
269 	bool change_vlan_filtering = false;
270 	struct dsa_switch *ds = dp->ds;
271 	struct dsa_port *other_dp;
272 	bool vlan_filtering;
273 	int err;
274 
275 	if (ds->needs_standalone_vlan_filtering &&
276 	    !br_vlan_enabled(bridge.dev)) {
277 		change_vlan_filtering = true;
278 		vlan_filtering = true;
279 	} else if (!ds->needs_standalone_vlan_filtering &&
280 		   br_vlan_enabled(bridge.dev)) {
281 		change_vlan_filtering = true;
282 		vlan_filtering = false;
283 	}
284 
285 	/* If the bridge was vlan_filtering, the bridge core doesn't trigger an
286 	 * event for changing vlan_filtering setting upon slave ports leaving
287 	 * it. That is a good thing, because that lets us handle it and also
288 	 * handle the case where the switch's vlan_filtering setting is global
289 	 * (not per port). When that happens, the correct moment to trigger the
290 	 * vlan_filtering callback is only when the last port leaves the last
291 	 * VLAN-aware bridge.
292 	 */
293 	if (change_vlan_filtering && ds->vlan_filtering_is_global) {
294 		dsa_switch_for_each_port(other_dp, ds) {
295 			struct net_device *br = dsa_port_bridge_dev_get(other_dp);
296 
297 			if (br && br_vlan_enabled(br)) {
298 				change_vlan_filtering = false;
299 				break;
300 			}
301 		}
302 	}
303 
304 	if (!change_vlan_filtering)
305 		return;
306 
307 	err = dsa_port_vlan_filtering(dp, vlan_filtering, &extack);
308 	if (extack._msg) {
309 		dev_err(ds->dev, "port %d: %s\n", dp->index,
310 			extack._msg);
311 	}
312 	if (err && err != -EOPNOTSUPP) {
313 		dev_err(ds->dev,
314 			"port %d failed to reset VLAN filtering to %d: %pe\n",
315 		       dp->index, vlan_filtering, ERR_PTR(err));
316 	}
317 }
318 
319 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
320 					 struct netlink_ext_ack *extack)
321 {
322 	const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
323 				   BR_BCAST_FLOOD | BR_PORT_LOCKED;
324 	struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
325 	int flag, err;
326 
327 	for_each_set_bit(flag, &mask, 32) {
328 		struct switchdev_brport_flags flags = {0};
329 
330 		flags.mask = BIT(flag);
331 
332 		if (br_port_flag_is_set(brport_dev, BIT(flag)))
333 			flags.val = BIT(flag);
334 
335 		err = dsa_port_bridge_flags(dp, flags, extack);
336 		if (err && err != -EOPNOTSUPP)
337 			return err;
338 	}
339 
340 	return 0;
341 }
342 
343 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
344 {
345 	const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
346 	const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
347 				   BR_BCAST_FLOOD | BR_PORT_LOCKED;
348 	int flag, err;
349 
350 	for_each_set_bit(flag, &mask, 32) {
351 		struct switchdev_brport_flags flags = {0};
352 
353 		flags.mask = BIT(flag);
354 		flags.val = val & BIT(flag);
355 
356 		err = dsa_port_bridge_flags(dp, flags, NULL);
357 		if (err && err != -EOPNOTSUPP)
358 			dev_err(dp->ds->dev,
359 				"failed to clear bridge port flag %lu: %pe\n",
360 				flags.val, ERR_PTR(err));
361 	}
362 }
363 
364 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
365 					 struct netlink_ext_ack *extack)
366 {
367 	struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
368 	struct net_device *br = dsa_port_bridge_dev_get(dp);
369 	int err;
370 
371 	err = dsa_port_inherit_brport_flags(dp, extack);
372 	if (err)
373 		return err;
374 
375 	err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
376 	if (err && err != -EOPNOTSUPP)
377 		return err;
378 
379 	err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
380 	if (err && err != -EOPNOTSUPP)
381 		return err;
382 
383 	err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
384 	if (err && err != -EOPNOTSUPP)
385 		return err;
386 
387 	return 0;
388 }
389 
390 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp,
391 					    struct dsa_bridge bridge)
392 {
393 	/* Configure the port for standalone mode (no address learning,
394 	 * flood everything).
395 	 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
396 	 * when the user requests it through netlink or sysfs, but not
397 	 * automatically at port join or leave, so we need to handle resetting
398 	 * the brport flags ourselves. But we even prefer it that way, because
399 	 * otherwise, some setups might never get the notification they need,
400 	 * for example, when a port leaves a LAG that offloads the bridge,
401 	 * it becomes standalone, but as far as the bridge is concerned, no
402 	 * port ever left.
403 	 */
404 	dsa_port_clear_brport_flags(dp);
405 
406 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
407 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
408 	 */
409 	dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
410 
411 	dsa_port_reset_vlan_filtering(dp, bridge);
412 
413 	/* Ageing time may be global to the switch chip, so don't change it
414 	 * here because we have no good reason (or value) to change it to.
415 	 */
416 }
417 
418 static int dsa_port_bridge_create(struct dsa_port *dp,
419 				  struct net_device *br,
420 				  struct netlink_ext_ack *extack)
421 {
422 	struct dsa_switch *ds = dp->ds;
423 	struct dsa_bridge *bridge;
424 
425 	bridge = dsa_tree_bridge_find(ds->dst, br);
426 	if (bridge) {
427 		refcount_inc(&bridge->refcount);
428 		dp->bridge = bridge;
429 		return 0;
430 	}
431 
432 	bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
433 	if (!bridge)
434 		return -ENOMEM;
435 
436 	refcount_set(&bridge->refcount, 1);
437 
438 	bridge->dev = br;
439 
440 	bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges);
441 	if (ds->max_num_bridges && !bridge->num) {
442 		NL_SET_ERR_MSG_MOD(extack,
443 				   "Range of offloadable bridges exceeded");
444 		kfree(bridge);
445 		return -EOPNOTSUPP;
446 	}
447 
448 	dp->bridge = bridge;
449 
450 	return 0;
451 }
452 
453 static void dsa_port_bridge_destroy(struct dsa_port *dp,
454 				    const struct net_device *br)
455 {
456 	struct dsa_bridge *bridge = dp->bridge;
457 
458 	dp->bridge = NULL;
459 
460 	if (!refcount_dec_and_test(&bridge->refcount))
461 		return;
462 
463 	if (bridge->num)
464 		dsa_bridge_num_put(br, bridge->num);
465 
466 	kfree(bridge);
467 }
468 
469 static bool dsa_port_supports_mst(struct dsa_port *dp)
470 {
471 	struct dsa_switch *ds = dp->ds;
472 
473 	return ds->ops->vlan_msti_set &&
474 		ds->ops->port_mst_state_set &&
475 		ds->ops->port_vlan_fast_age &&
476 		dsa_port_can_configure_learning(dp);
477 }
478 
479 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
480 			 struct netlink_ext_ack *extack)
481 {
482 	struct dsa_notifier_bridge_info info = {
483 		.dp = dp,
484 		.extack = extack,
485 	};
486 	struct net_device *dev = dp->slave;
487 	struct net_device *brport_dev;
488 	int err;
489 
490 	if (br_mst_enabled(br) && !dsa_port_supports_mst(dp))
491 		return -EOPNOTSUPP;
492 
493 	/* Here the interface is already bridged. Reflect the current
494 	 * configuration so that drivers can program their chips accordingly.
495 	 */
496 	err = dsa_port_bridge_create(dp, br, extack);
497 	if (err)
498 		return err;
499 
500 	brport_dev = dsa_port_to_bridge_port(dp);
501 
502 	info.bridge = *dp->bridge;
503 	err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
504 	if (err)
505 		goto out_rollback;
506 
507 	/* Drivers which support bridge TX forwarding should set this */
508 	dp->bridge->tx_fwd_offload = info.tx_fwd_offload;
509 
510 	err = switchdev_bridge_port_offload(brport_dev, dev, dp,
511 					    &dsa_slave_switchdev_notifier,
512 					    &dsa_slave_switchdev_blocking_notifier,
513 					    dp->bridge->tx_fwd_offload, extack);
514 	if (err)
515 		goto out_rollback_unbridge;
516 
517 	err = dsa_port_switchdev_sync_attrs(dp, extack);
518 	if (err)
519 		goto out_rollback_unoffload;
520 
521 	return 0;
522 
523 out_rollback_unoffload:
524 	switchdev_bridge_port_unoffload(brport_dev, dp,
525 					&dsa_slave_switchdev_notifier,
526 					&dsa_slave_switchdev_blocking_notifier);
527 	dsa_flush_workqueue();
528 out_rollback_unbridge:
529 	dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
530 out_rollback:
531 	dsa_port_bridge_destroy(dp, br);
532 	return err;
533 }
534 
535 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
536 {
537 	struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
538 
539 	/* Don't try to unoffload something that is not offloaded */
540 	if (!brport_dev)
541 		return;
542 
543 	switchdev_bridge_port_unoffload(brport_dev, dp,
544 					&dsa_slave_switchdev_notifier,
545 					&dsa_slave_switchdev_blocking_notifier);
546 
547 	dsa_flush_workqueue();
548 }
549 
550 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
551 {
552 	struct dsa_notifier_bridge_info info = {
553 		.dp = dp,
554 	};
555 	int err;
556 
557 	/* If the port could not be offloaded to begin with, then
558 	 * there is nothing to do.
559 	 */
560 	if (!dp->bridge)
561 		return;
562 
563 	info.bridge = *dp->bridge;
564 
565 	/* Here the port is already unbridged. Reflect the current configuration
566 	 * so that drivers can program their chips accordingly.
567 	 */
568 	dsa_port_bridge_destroy(dp, br);
569 
570 	err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
571 	if (err)
572 		dev_err(dp->ds->dev,
573 			"port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
574 			dp->index, ERR_PTR(err));
575 
576 	dsa_port_switchdev_unsync_attrs(dp, info.bridge);
577 }
578 
579 int dsa_port_lag_change(struct dsa_port *dp,
580 			struct netdev_lag_lower_state_info *linfo)
581 {
582 	struct dsa_notifier_lag_info info = {
583 		.dp = dp,
584 	};
585 	bool tx_enabled;
586 
587 	if (!dp->lag)
588 		return 0;
589 
590 	/* On statically configured aggregates (e.g. loadbalance
591 	 * without LACP) ports will always be tx_enabled, even if the
592 	 * link is down. Thus we require both link_up and tx_enabled
593 	 * in order to include it in the tx set.
594 	 */
595 	tx_enabled = linfo->link_up && linfo->tx_enabled;
596 
597 	if (tx_enabled == dp->lag_tx_enabled)
598 		return 0;
599 
600 	dp->lag_tx_enabled = tx_enabled;
601 
602 	return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
603 }
604 
605 static int dsa_port_lag_create(struct dsa_port *dp,
606 			       struct net_device *lag_dev)
607 {
608 	struct dsa_switch *ds = dp->ds;
609 	struct dsa_lag *lag;
610 
611 	lag = dsa_tree_lag_find(ds->dst, lag_dev);
612 	if (lag) {
613 		refcount_inc(&lag->refcount);
614 		dp->lag = lag;
615 		return 0;
616 	}
617 
618 	lag = kzalloc(sizeof(*lag), GFP_KERNEL);
619 	if (!lag)
620 		return -ENOMEM;
621 
622 	refcount_set(&lag->refcount, 1);
623 	mutex_init(&lag->fdb_lock);
624 	INIT_LIST_HEAD(&lag->fdbs);
625 	lag->dev = lag_dev;
626 	dsa_lag_map(ds->dst, lag);
627 	dp->lag = lag;
628 
629 	return 0;
630 }
631 
632 static void dsa_port_lag_destroy(struct dsa_port *dp)
633 {
634 	struct dsa_lag *lag = dp->lag;
635 
636 	dp->lag = NULL;
637 	dp->lag_tx_enabled = false;
638 
639 	if (!refcount_dec_and_test(&lag->refcount))
640 		return;
641 
642 	WARN_ON(!list_empty(&lag->fdbs));
643 	dsa_lag_unmap(dp->ds->dst, lag);
644 	kfree(lag);
645 }
646 
647 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
648 		      struct netdev_lag_upper_info *uinfo,
649 		      struct netlink_ext_ack *extack)
650 {
651 	struct dsa_notifier_lag_info info = {
652 		.dp = dp,
653 		.info = uinfo,
654 		.extack = extack,
655 	};
656 	struct net_device *bridge_dev;
657 	int err;
658 
659 	err = dsa_port_lag_create(dp, lag_dev);
660 	if (err)
661 		goto err_lag_create;
662 
663 	info.lag = *dp->lag;
664 	err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
665 	if (err)
666 		goto err_lag_join;
667 
668 	bridge_dev = netdev_master_upper_dev_get(lag_dev);
669 	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
670 		return 0;
671 
672 	err = dsa_port_bridge_join(dp, bridge_dev, extack);
673 	if (err)
674 		goto err_bridge_join;
675 
676 	return 0;
677 
678 err_bridge_join:
679 	dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
680 err_lag_join:
681 	dsa_port_lag_destroy(dp);
682 err_lag_create:
683 	return err;
684 }
685 
686 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
687 {
688 	struct net_device *br = dsa_port_bridge_dev_get(dp);
689 
690 	if (br)
691 		dsa_port_pre_bridge_leave(dp, br);
692 }
693 
694 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
695 {
696 	struct net_device *br = dsa_port_bridge_dev_get(dp);
697 	struct dsa_notifier_lag_info info = {
698 		.dp = dp,
699 	};
700 	int err;
701 
702 	if (!dp->lag)
703 		return;
704 
705 	/* Port might have been part of a LAG that in turn was
706 	 * attached to a bridge.
707 	 */
708 	if (br)
709 		dsa_port_bridge_leave(dp, br);
710 
711 	info.lag = *dp->lag;
712 
713 	dsa_port_lag_destroy(dp);
714 
715 	err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
716 	if (err)
717 		dev_err(dp->ds->dev,
718 			"port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
719 			dp->index, ERR_PTR(err));
720 }
721 
722 /* Must be called under rcu_read_lock() */
723 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
724 					      bool vlan_filtering,
725 					      struct netlink_ext_ack *extack)
726 {
727 	struct dsa_switch *ds = dp->ds;
728 	struct dsa_port *other_dp;
729 	int err;
730 
731 	/* VLAN awareness was off, so the question is "can we turn it on".
732 	 * We may have had 8021q uppers, those need to go. Make sure we don't
733 	 * enter an inconsistent state: deny changing the VLAN awareness state
734 	 * as long as we have 8021q uppers.
735 	 */
736 	if (vlan_filtering && dsa_port_is_user(dp)) {
737 		struct net_device *br = dsa_port_bridge_dev_get(dp);
738 		struct net_device *upper_dev, *slave = dp->slave;
739 		struct list_head *iter;
740 
741 		netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
742 			struct bridge_vlan_info br_info;
743 			u16 vid;
744 
745 			if (!is_vlan_dev(upper_dev))
746 				continue;
747 
748 			vid = vlan_dev_vlan_id(upper_dev);
749 
750 			/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
751 			 * device, respectively the VID is not found, returning
752 			 * 0 means success, which is a failure for us here.
753 			 */
754 			err = br_vlan_get_info(br, vid, &br_info);
755 			if (err == 0) {
756 				NL_SET_ERR_MSG_MOD(extack,
757 						   "Must first remove VLAN uppers having VIDs also present in bridge");
758 				return false;
759 			}
760 		}
761 	}
762 
763 	if (!ds->vlan_filtering_is_global)
764 		return true;
765 
766 	/* For cases where enabling/disabling VLAN awareness is global to the
767 	 * switch, we need to handle the case where multiple bridges span
768 	 * different ports of the same switch device and one of them has a
769 	 * different setting than what is being requested.
770 	 */
771 	dsa_switch_for_each_port(other_dp, ds) {
772 		struct net_device *other_br = dsa_port_bridge_dev_get(other_dp);
773 
774 		/* If it's the same bridge, it also has same
775 		 * vlan_filtering setting => no need to check
776 		 */
777 		if (!other_br || other_br == dsa_port_bridge_dev_get(dp))
778 			continue;
779 
780 		if (br_vlan_enabled(other_br) != vlan_filtering) {
781 			NL_SET_ERR_MSG_MOD(extack,
782 					   "VLAN filtering is a global setting");
783 			return false;
784 		}
785 	}
786 	return true;
787 }
788 
789 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
790 			    struct netlink_ext_ack *extack)
791 {
792 	bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
793 	struct dsa_switch *ds = dp->ds;
794 	bool apply;
795 	int err;
796 
797 	if (!ds->ops->port_vlan_filtering)
798 		return -EOPNOTSUPP;
799 
800 	/* We are called from dsa_slave_switchdev_blocking_event(),
801 	 * which is not under rcu_read_lock(), unlike
802 	 * dsa_slave_switchdev_event().
803 	 */
804 	rcu_read_lock();
805 	apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
806 	rcu_read_unlock();
807 	if (!apply)
808 		return -EINVAL;
809 
810 	if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
811 		return 0;
812 
813 	err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
814 					   extack);
815 	if (err)
816 		return err;
817 
818 	if (ds->vlan_filtering_is_global) {
819 		struct dsa_port *other_dp;
820 
821 		ds->vlan_filtering = vlan_filtering;
822 
823 		dsa_switch_for_each_user_port(other_dp, ds) {
824 			struct net_device *slave = other_dp->slave;
825 
826 			/* We might be called in the unbind path, so not
827 			 * all slave devices might still be registered.
828 			 */
829 			if (!slave)
830 				continue;
831 
832 			err = dsa_slave_manage_vlan_filtering(slave,
833 							      vlan_filtering);
834 			if (err)
835 				goto restore;
836 		}
837 	} else {
838 		dp->vlan_filtering = vlan_filtering;
839 
840 		err = dsa_slave_manage_vlan_filtering(dp->slave,
841 						      vlan_filtering);
842 		if (err)
843 			goto restore;
844 	}
845 
846 	return 0;
847 
848 restore:
849 	ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
850 
851 	if (ds->vlan_filtering_is_global)
852 		ds->vlan_filtering = old_vlan_filtering;
853 	else
854 		dp->vlan_filtering = old_vlan_filtering;
855 
856 	return err;
857 }
858 
859 /* This enforces legacy behavior for switch drivers which assume they can't
860  * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
861  */
862 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
863 {
864 	struct net_device *br = dsa_port_bridge_dev_get(dp);
865 	struct dsa_switch *ds = dp->ds;
866 
867 	if (!br)
868 		return false;
869 
870 	return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br);
871 }
872 
873 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
874 {
875 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
876 	unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
877 	struct dsa_notifier_ageing_time_info info;
878 	int err;
879 
880 	info.ageing_time = ageing_time;
881 
882 	err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
883 	if (err)
884 		return err;
885 
886 	dp->ageing_time = ageing_time;
887 
888 	return 0;
889 }
890 
891 int dsa_port_mst_enable(struct dsa_port *dp, bool on,
892 			struct netlink_ext_ack *extack)
893 {
894 	if (on && !dsa_port_supports_mst(dp)) {
895 		NL_SET_ERR_MSG_MOD(extack, "Hardware does not support MST");
896 		return -EINVAL;
897 	}
898 
899 	return 0;
900 }
901 
902 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
903 			      struct switchdev_brport_flags flags,
904 			      struct netlink_ext_ack *extack)
905 {
906 	struct dsa_switch *ds = dp->ds;
907 
908 	if (!ds->ops->port_pre_bridge_flags)
909 		return -EINVAL;
910 
911 	return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
912 }
913 
914 int dsa_port_bridge_flags(struct dsa_port *dp,
915 			  struct switchdev_brport_flags flags,
916 			  struct netlink_ext_ack *extack)
917 {
918 	struct dsa_switch *ds = dp->ds;
919 	int err;
920 
921 	if (!ds->ops->port_bridge_flags)
922 		return -EOPNOTSUPP;
923 
924 	err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
925 	if (err)
926 		return err;
927 
928 	if (flags.mask & BR_LEARNING) {
929 		bool learning = flags.val & BR_LEARNING;
930 
931 		if (learning == dp->learning)
932 			return 0;
933 
934 		if ((dp->learning && !learning) &&
935 		    (dp->stp_state == BR_STATE_LEARNING ||
936 		     dp->stp_state == BR_STATE_FORWARDING))
937 			dsa_port_fast_age(dp);
938 
939 		dp->learning = learning;
940 	}
941 
942 	return 0;
943 }
944 
945 void dsa_port_set_host_flood(struct dsa_port *dp, bool uc, bool mc)
946 {
947 	struct dsa_switch *ds = dp->ds;
948 
949 	if (ds->ops->port_set_host_flood)
950 		ds->ops->port_set_host_flood(ds, dp->index, uc, mc);
951 }
952 
953 int dsa_port_vlan_msti(struct dsa_port *dp,
954 		       const struct switchdev_vlan_msti *msti)
955 {
956 	struct dsa_switch *ds = dp->ds;
957 
958 	if (!ds->ops->vlan_msti_set)
959 		return -EOPNOTSUPP;
960 
961 	return ds->ops->vlan_msti_set(ds, *dp->bridge, msti);
962 }
963 
964 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu)
965 {
966 	struct dsa_notifier_mtu_info info = {
967 		.dp = dp,
968 		.mtu = new_mtu,
969 	};
970 
971 	return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
972 }
973 
974 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
975 		     u16 vid)
976 {
977 	struct dsa_notifier_fdb_info info = {
978 		.dp = dp,
979 		.addr = addr,
980 		.vid = vid,
981 		.db = {
982 			.type = DSA_DB_BRIDGE,
983 			.bridge = *dp->bridge,
984 		},
985 	};
986 
987 	/* Refcounting takes bridge.num as a key, and should be global for all
988 	 * bridges in the absence of FDB isolation, and per bridge otherwise.
989 	 * Force the bridge.num to zero here in the absence of FDB isolation.
990 	 */
991 	if (!dp->ds->fdb_isolation)
992 		info.db.bridge.num = 0;
993 
994 	return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
995 }
996 
997 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
998 		     u16 vid)
999 {
1000 	struct dsa_notifier_fdb_info info = {
1001 		.dp = dp,
1002 		.addr = addr,
1003 		.vid = vid,
1004 		.db = {
1005 			.type = DSA_DB_BRIDGE,
1006 			.bridge = *dp->bridge,
1007 		},
1008 	};
1009 
1010 	if (!dp->ds->fdb_isolation)
1011 		info.db.bridge.num = 0;
1012 
1013 	return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
1014 }
1015 
1016 static int dsa_port_host_fdb_add(struct dsa_port *dp,
1017 				 const unsigned char *addr, u16 vid,
1018 				 struct dsa_db db)
1019 {
1020 	struct dsa_notifier_fdb_info info = {
1021 		.dp = dp,
1022 		.addr = addr,
1023 		.vid = vid,
1024 		.db = db,
1025 	};
1026 
1027 	if (!dp->ds->fdb_isolation)
1028 		info.db.bridge.num = 0;
1029 
1030 	return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
1031 }
1032 
1033 int dsa_port_standalone_host_fdb_add(struct dsa_port *dp,
1034 				     const unsigned char *addr, u16 vid)
1035 {
1036 	struct dsa_db db = {
1037 		.type = DSA_DB_PORT,
1038 		.dp = dp,
1039 	};
1040 
1041 	return dsa_port_host_fdb_add(dp, addr, vid, db);
1042 }
1043 
1044 int dsa_port_bridge_host_fdb_add(struct dsa_port *dp,
1045 				 const unsigned char *addr, u16 vid)
1046 {
1047 	struct net_device *master = dsa_port_to_master(dp);
1048 	struct dsa_db db = {
1049 		.type = DSA_DB_BRIDGE,
1050 		.bridge = *dp->bridge,
1051 	};
1052 	int err;
1053 
1054 	/* Avoid a call to __dev_set_promiscuity() on the master, which
1055 	 * requires rtnl_lock(), since we can't guarantee that is held here,
1056 	 * and we can't take it either.
1057 	 */
1058 	if (master->priv_flags & IFF_UNICAST_FLT) {
1059 		err = dev_uc_add(master, addr);
1060 		if (err)
1061 			return err;
1062 	}
1063 
1064 	return dsa_port_host_fdb_add(dp, addr, vid, db);
1065 }
1066 
1067 static int dsa_port_host_fdb_del(struct dsa_port *dp,
1068 				 const unsigned char *addr, u16 vid,
1069 				 struct dsa_db db)
1070 {
1071 	struct dsa_notifier_fdb_info info = {
1072 		.dp = dp,
1073 		.addr = addr,
1074 		.vid = vid,
1075 		.db = db,
1076 	};
1077 
1078 	if (!dp->ds->fdb_isolation)
1079 		info.db.bridge.num = 0;
1080 
1081 	return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
1082 }
1083 
1084 int dsa_port_standalone_host_fdb_del(struct dsa_port *dp,
1085 				     const unsigned char *addr, u16 vid)
1086 {
1087 	struct dsa_db db = {
1088 		.type = DSA_DB_PORT,
1089 		.dp = dp,
1090 	};
1091 
1092 	return dsa_port_host_fdb_del(dp, addr, vid, db);
1093 }
1094 
1095 int dsa_port_bridge_host_fdb_del(struct dsa_port *dp,
1096 				 const unsigned char *addr, u16 vid)
1097 {
1098 	struct net_device *master = dsa_port_to_master(dp);
1099 	struct dsa_db db = {
1100 		.type = DSA_DB_BRIDGE,
1101 		.bridge = *dp->bridge,
1102 	};
1103 	int err;
1104 
1105 	if (master->priv_flags & IFF_UNICAST_FLT) {
1106 		err = dev_uc_del(master, addr);
1107 		if (err)
1108 			return err;
1109 	}
1110 
1111 	return dsa_port_host_fdb_del(dp, addr, vid, db);
1112 }
1113 
1114 int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
1115 			 u16 vid)
1116 {
1117 	struct dsa_notifier_lag_fdb_info info = {
1118 		.lag = dp->lag,
1119 		.addr = addr,
1120 		.vid = vid,
1121 		.db = {
1122 			.type = DSA_DB_BRIDGE,
1123 			.bridge = *dp->bridge,
1124 		},
1125 	};
1126 
1127 	if (!dp->ds->fdb_isolation)
1128 		info.db.bridge.num = 0;
1129 
1130 	return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info);
1131 }
1132 
1133 int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
1134 			 u16 vid)
1135 {
1136 	struct dsa_notifier_lag_fdb_info info = {
1137 		.lag = dp->lag,
1138 		.addr = addr,
1139 		.vid = vid,
1140 		.db = {
1141 			.type = DSA_DB_BRIDGE,
1142 			.bridge = *dp->bridge,
1143 		},
1144 	};
1145 
1146 	if (!dp->ds->fdb_isolation)
1147 		info.db.bridge.num = 0;
1148 
1149 	return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_DEL, &info);
1150 }
1151 
1152 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
1153 {
1154 	struct dsa_switch *ds = dp->ds;
1155 	int port = dp->index;
1156 
1157 	if (!ds->ops->port_fdb_dump)
1158 		return -EOPNOTSUPP;
1159 
1160 	return ds->ops->port_fdb_dump(ds, port, cb, data);
1161 }
1162 
1163 int dsa_port_mdb_add(const struct dsa_port *dp,
1164 		     const struct switchdev_obj_port_mdb *mdb)
1165 {
1166 	struct dsa_notifier_mdb_info info = {
1167 		.dp = dp,
1168 		.mdb = mdb,
1169 		.db = {
1170 			.type = DSA_DB_BRIDGE,
1171 			.bridge = *dp->bridge,
1172 		},
1173 	};
1174 
1175 	if (!dp->ds->fdb_isolation)
1176 		info.db.bridge.num = 0;
1177 
1178 	return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
1179 }
1180 
1181 int dsa_port_mdb_del(const struct dsa_port *dp,
1182 		     const struct switchdev_obj_port_mdb *mdb)
1183 {
1184 	struct dsa_notifier_mdb_info info = {
1185 		.dp = dp,
1186 		.mdb = mdb,
1187 		.db = {
1188 			.type = DSA_DB_BRIDGE,
1189 			.bridge = *dp->bridge,
1190 		},
1191 	};
1192 
1193 	if (!dp->ds->fdb_isolation)
1194 		info.db.bridge.num = 0;
1195 
1196 	return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
1197 }
1198 
1199 static int dsa_port_host_mdb_add(const struct dsa_port *dp,
1200 				 const struct switchdev_obj_port_mdb *mdb,
1201 				 struct dsa_db db)
1202 {
1203 	struct dsa_notifier_mdb_info info = {
1204 		.dp = dp,
1205 		.mdb = mdb,
1206 		.db = db,
1207 	};
1208 
1209 	if (!dp->ds->fdb_isolation)
1210 		info.db.bridge.num = 0;
1211 
1212 	return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
1213 }
1214 
1215 int dsa_port_standalone_host_mdb_add(const struct dsa_port *dp,
1216 				     const struct switchdev_obj_port_mdb *mdb)
1217 {
1218 	struct dsa_db db = {
1219 		.type = DSA_DB_PORT,
1220 		.dp = dp,
1221 	};
1222 
1223 	return dsa_port_host_mdb_add(dp, mdb, db);
1224 }
1225 
1226 int dsa_port_bridge_host_mdb_add(const struct dsa_port *dp,
1227 				 const struct switchdev_obj_port_mdb *mdb)
1228 {
1229 	struct net_device *master = dsa_port_to_master(dp);
1230 	struct dsa_db db = {
1231 		.type = DSA_DB_BRIDGE,
1232 		.bridge = *dp->bridge,
1233 	};
1234 	int err;
1235 
1236 	err = dev_mc_add(master, mdb->addr);
1237 	if (err)
1238 		return err;
1239 
1240 	return dsa_port_host_mdb_add(dp, mdb, db);
1241 }
1242 
1243 static int dsa_port_host_mdb_del(const struct dsa_port *dp,
1244 				 const struct switchdev_obj_port_mdb *mdb,
1245 				 struct dsa_db db)
1246 {
1247 	struct dsa_notifier_mdb_info info = {
1248 		.dp = dp,
1249 		.mdb = mdb,
1250 		.db = db,
1251 	};
1252 
1253 	if (!dp->ds->fdb_isolation)
1254 		info.db.bridge.num = 0;
1255 
1256 	return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
1257 }
1258 
1259 int dsa_port_standalone_host_mdb_del(const struct dsa_port *dp,
1260 				     const struct switchdev_obj_port_mdb *mdb)
1261 {
1262 	struct dsa_db db = {
1263 		.type = DSA_DB_PORT,
1264 		.dp = dp,
1265 	};
1266 
1267 	return dsa_port_host_mdb_del(dp, mdb, db);
1268 }
1269 
1270 int dsa_port_bridge_host_mdb_del(const struct dsa_port *dp,
1271 				 const struct switchdev_obj_port_mdb *mdb)
1272 {
1273 	struct net_device *master = dsa_port_to_master(dp);
1274 	struct dsa_db db = {
1275 		.type = DSA_DB_BRIDGE,
1276 		.bridge = *dp->bridge,
1277 	};
1278 	int err;
1279 
1280 	err = dev_mc_del(master, mdb->addr);
1281 	if (err)
1282 		return err;
1283 
1284 	return dsa_port_host_mdb_del(dp, mdb, db);
1285 }
1286 
1287 int dsa_port_vlan_add(struct dsa_port *dp,
1288 		      const struct switchdev_obj_port_vlan *vlan,
1289 		      struct netlink_ext_ack *extack)
1290 {
1291 	struct dsa_notifier_vlan_info info = {
1292 		.dp = dp,
1293 		.vlan = vlan,
1294 		.extack = extack,
1295 	};
1296 
1297 	return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
1298 }
1299 
1300 int dsa_port_vlan_del(struct dsa_port *dp,
1301 		      const struct switchdev_obj_port_vlan *vlan)
1302 {
1303 	struct dsa_notifier_vlan_info info = {
1304 		.dp = dp,
1305 		.vlan = vlan,
1306 	};
1307 
1308 	return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
1309 }
1310 
1311 int dsa_port_host_vlan_add(struct dsa_port *dp,
1312 			   const struct switchdev_obj_port_vlan *vlan,
1313 			   struct netlink_ext_ack *extack)
1314 {
1315 	struct net_device *master = dsa_port_to_master(dp);
1316 	struct dsa_notifier_vlan_info info = {
1317 		.dp = dp,
1318 		.vlan = vlan,
1319 		.extack = extack,
1320 	};
1321 	int err;
1322 
1323 	err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_ADD, &info);
1324 	if (err && err != -EOPNOTSUPP)
1325 		return err;
1326 
1327 	vlan_vid_add(master, htons(ETH_P_8021Q), vlan->vid);
1328 
1329 	return err;
1330 }
1331 
1332 int dsa_port_host_vlan_del(struct dsa_port *dp,
1333 			   const struct switchdev_obj_port_vlan *vlan)
1334 {
1335 	struct net_device *master = dsa_port_to_master(dp);
1336 	struct dsa_notifier_vlan_info info = {
1337 		.dp = dp,
1338 		.vlan = vlan,
1339 	};
1340 	int err;
1341 
1342 	err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_DEL, &info);
1343 	if (err && err != -EOPNOTSUPP)
1344 		return err;
1345 
1346 	vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid);
1347 
1348 	return err;
1349 }
1350 
1351 int dsa_port_mrp_add(const struct dsa_port *dp,
1352 		     const struct switchdev_obj_mrp *mrp)
1353 {
1354 	struct dsa_switch *ds = dp->ds;
1355 
1356 	if (!ds->ops->port_mrp_add)
1357 		return -EOPNOTSUPP;
1358 
1359 	return ds->ops->port_mrp_add(ds, dp->index, mrp);
1360 }
1361 
1362 int dsa_port_mrp_del(const struct dsa_port *dp,
1363 		     const struct switchdev_obj_mrp *mrp)
1364 {
1365 	struct dsa_switch *ds = dp->ds;
1366 
1367 	if (!ds->ops->port_mrp_del)
1368 		return -EOPNOTSUPP;
1369 
1370 	return ds->ops->port_mrp_del(ds, dp->index, mrp);
1371 }
1372 
1373 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
1374 			       const struct switchdev_obj_ring_role_mrp *mrp)
1375 {
1376 	struct dsa_switch *ds = dp->ds;
1377 
1378 	if (!ds->ops->port_mrp_add_ring_role)
1379 		return -EOPNOTSUPP;
1380 
1381 	return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp);
1382 }
1383 
1384 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
1385 			       const struct switchdev_obj_ring_role_mrp *mrp)
1386 {
1387 	struct dsa_switch *ds = dp->ds;
1388 
1389 	if (!ds->ops->port_mrp_del_ring_role)
1390 		return -EOPNOTSUPP;
1391 
1392 	return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp);
1393 }
1394 
1395 static int dsa_port_assign_master(struct dsa_port *dp,
1396 				  struct net_device *master,
1397 				  struct netlink_ext_ack *extack,
1398 				  bool fail_on_err)
1399 {
1400 	struct dsa_switch *ds = dp->ds;
1401 	int port = dp->index, err;
1402 
1403 	err = ds->ops->port_change_master(ds, port, master, extack);
1404 	if (err && !fail_on_err)
1405 		dev_err(ds->dev, "port %d failed to assign master %s: %pe\n",
1406 			port, master->name, ERR_PTR(err));
1407 
1408 	if (err && fail_on_err)
1409 		return err;
1410 
1411 	dp->cpu_dp = master->dsa_ptr;
1412 	dp->cpu_port_in_lag = netif_is_lag_master(master);
1413 
1414 	return 0;
1415 }
1416 
1417 /* Change the dp->cpu_dp affinity for a user port. Note that both cross-chip
1418  * notifiers and drivers have implicit assumptions about user-to-CPU-port
1419  * mappings, so we unfortunately cannot delay the deletion of the objects
1420  * (switchdev, standalone addresses, standalone VLANs) on the old CPU port
1421  * until the new CPU port has been set up. So we need to completely tear down
1422  * the old CPU port before changing it, and restore it on errors during the
1423  * bringup of the new one.
1424  */
1425 int dsa_port_change_master(struct dsa_port *dp, struct net_device *master,
1426 			   struct netlink_ext_ack *extack)
1427 {
1428 	struct net_device *bridge_dev = dsa_port_bridge_dev_get(dp);
1429 	struct net_device *old_master = dsa_port_to_master(dp);
1430 	struct net_device *dev = dp->slave;
1431 	struct dsa_switch *ds = dp->ds;
1432 	bool vlan_filtering;
1433 	int err, tmp;
1434 
1435 	/* Bridges may hold host FDB, MDB and VLAN objects. These need to be
1436 	 * migrated, so dynamically unoffload and later reoffload the bridge
1437 	 * port.
1438 	 */
1439 	if (bridge_dev) {
1440 		dsa_port_pre_bridge_leave(dp, bridge_dev);
1441 		dsa_port_bridge_leave(dp, bridge_dev);
1442 	}
1443 
1444 	/* The port might still be VLAN filtering even if it's no longer
1445 	 * under a bridge, either due to ds->vlan_filtering_is_global or
1446 	 * ds->needs_standalone_vlan_filtering. In turn this means VLANs
1447 	 * on the CPU port.
1448 	 */
1449 	vlan_filtering = dsa_port_is_vlan_filtering(dp);
1450 	if (vlan_filtering) {
1451 		err = dsa_slave_manage_vlan_filtering(dev, false);
1452 		if (err) {
1453 			NL_SET_ERR_MSG_MOD(extack,
1454 					   "Failed to remove standalone VLANs");
1455 			goto rewind_old_bridge;
1456 		}
1457 	}
1458 
1459 	/* Standalone addresses, and addresses of upper interfaces like
1460 	 * VLAN, LAG, HSR need to be migrated.
1461 	 */
1462 	dsa_slave_unsync_ha(dev);
1463 
1464 	err = dsa_port_assign_master(dp, master, extack, true);
1465 	if (err)
1466 		goto rewind_old_addrs;
1467 
1468 	dsa_slave_sync_ha(dev);
1469 
1470 	if (vlan_filtering) {
1471 		err = dsa_slave_manage_vlan_filtering(dev, true);
1472 		if (err) {
1473 			NL_SET_ERR_MSG_MOD(extack,
1474 					   "Failed to restore standalone VLANs");
1475 			goto rewind_new_addrs;
1476 		}
1477 	}
1478 
1479 	if (bridge_dev) {
1480 		err = dsa_port_bridge_join(dp, bridge_dev, extack);
1481 		if (err && err == -EOPNOTSUPP) {
1482 			NL_SET_ERR_MSG_MOD(extack,
1483 					   "Failed to reoffload bridge");
1484 			goto rewind_new_vlan;
1485 		}
1486 	}
1487 
1488 	return 0;
1489 
1490 rewind_new_vlan:
1491 	if (vlan_filtering)
1492 		dsa_slave_manage_vlan_filtering(dev, false);
1493 
1494 rewind_new_addrs:
1495 	dsa_slave_unsync_ha(dev);
1496 
1497 	dsa_port_assign_master(dp, old_master, NULL, false);
1498 
1499 /* Restore the objects on the old CPU port */
1500 rewind_old_addrs:
1501 	dsa_slave_sync_ha(dev);
1502 
1503 	if (vlan_filtering) {
1504 		tmp = dsa_slave_manage_vlan_filtering(dev, true);
1505 		if (tmp) {
1506 			dev_err(ds->dev,
1507 				"port %d failed to restore standalone VLANs: %pe\n",
1508 				dp->index, ERR_PTR(tmp));
1509 		}
1510 	}
1511 
1512 rewind_old_bridge:
1513 	if (bridge_dev) {
1514 		tmp = dsa_port_bridge_join(dp, bridge_dev, extack);
1515 		if (tmp) {
1516 			dev_err(ds->dev,
1517 				"port %d failed to rejoin bridge %s: %pe\n",
1518 				dp->index, bridge_dev->name, ERR_PTR(tmp));
1519 		}
1520 	}
1521 
1522 	return err;
1523 }
1524 
1525 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
1526 			       const struct dsa_device_ops *tag_ops)
1527 {
1528 	cpu_dp->rcv = tag_ops->rcv;
1529 	cpu_dp->tag_ops = tag_ops;
1530 }
1531 
1532 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
1533 {
1534 	struct device_node *phy_dn;
1535 	struct phy_device *phydev;
1536 
1537 	phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
1538 	if (!phy_dn)
1539 		return NULL;
1540 
1541 	phydev = of_phy_find_device(phy_dn);
1542 	if (!phydev) {
1543 		of_node_put(phy_dn);
1544 		return ERR_PTR(-EPROBE_DEFER);
1545 	}
1546 
1547 	of_node_put(phy_dn);
1548 	return phydev;
1549 }
1550 
1551 static void dsa_port_phylink_validate(struct phylink_config *config,
1552 				      unsigned long *supported,
1553 				      struct phylink_link_state *state)
1554 {
1555 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1556 	struct dsa_switch *ds = dp->ds;
1557 
1558 	if (!ds->ops->phylink_validate) {
1559 		if (config->mac_capabilities)
1560 			phylink_generic_validate(config, supported, state);
1561 		return;
1562 	}
1563 
1564 	ds->ops->phylink_validate(ds, dp->index, supported, state);
1565 }
1566 
1567 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
1568 					       struct phylink_link_state *state)
1569 {
1570 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1571 	struct dsa_switch *ds = dp->ds;
1572 	int err;
1573 
1574 	/* Only called for inband modes */
1575 	if (!ds->ops->phylink_mac_link_state) {
1576 		state->link = 0;
1577 		return;
1578 	}
1579 
1580 	err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
1581 	if (err < 0) {
1582 		dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
1583 			dp->index, err);
1584 		state->link = 0;
1585 	}
1586 }
1587 
1588 static struct phylink_pcs *
1589 dsa_port_phylink_mac_select_pcs(struct phylink_config *config,
1590 				phy_interface_t interface)
1591 {
1592 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1593 	struct phylink_pcs *pcs = ERR_PTR(-EOPNOTSUPP);
1594 	struct dsa_switch *ds = dp->ds;
1595 
1596 	if (ds->ops->phylink_mac_select_pcs)
1597 		pcs = ds->ops->phylink_mac_select_pcs(ds, dp->index, interface);
1598 
1599 	return pcs;
1600 }
1601 
1602 static void dsa_port_phylink_mac_config(struct phylink_config *config,
1603 					unsigned int mode,
1604 					const struct phylink_link_state *state)
1605 {
1606 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1607 	struct dsa_switch *ds = dp->ds;
1608 
1609 	if (!ds->ops->phylink_mac_config)
1610 		return;
1611 
1612 	ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1613 }
1614 
1615 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
1616 {
1617 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1618 	struct dsa_switch *ds = dp->ds;
1619 
1620 	if (!ds->ops->phylink_mac_an_restart)
1621 		return;
1622 
1623 	ds->ops->phylink_mac_an_restart(ds, dp->index);
1624 }
1625 
1626 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1627 					   unsigned int mode,
1628 					   phy_interface_t interface)
1629 {
1630 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1631 	struct phy_device *phydev = NULL;
1632 	struct dsa_switch *ds = dp->ds;
1633 
1634 	if (dsa_port_is_user(dp))
1635 		phydev = dp->slave->phydev;
1636 
1637 	if (!ds->ops->phylink_mac_link_down) {
1638 		if (ds->ops->adjust_link && phydev)
1639 			ds->ops->adjust_link(ds, dp->index, phydev);
1640 		return;
1641 	}
1642 
1643 	ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1644 }
1645 
1646 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1647 					 struct phy_device *phydev,
1648 					 unsigned int mode,
1649 					 phy_interface_t interface,
1650 					 int speed, int duplex,
1651 					 bool tx_pause, bool rx_pause)
1652 {
1653 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1654 	struct dsa_switch *ds = dp->ds;
1655 
1656 	if (!ds->ops->phylink_mac_link_up) {
1657 		if (ds->ops->adjust_link && phydev)
1658 			ds->ops->adjust_link(ds, dp->index, phydev);
1659 		return;
1660 	}
1661 
1662 	ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1663 				     speed, duplex, tx_pause, rx_pause);
1664 }
1665 
1666 static const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1667 	.validate = dsa_port_phylink_validate,
1668 	.mac_select_pcs = dsa_port_phylink_mac_select_pcs,
1669 	.mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
1670 	.mac_config = dsa_port_phylink_mac_config,
1671 	.mac_an_restart = dsa_port_phylink_mac_an_restart,
1672 	.mac_link_down = dsa_port_phylink_mac_link_down,
1673 	.mac_link_up = dsa_port_phylink_mac_link_up,
1674 };
1675 
1676 int dsa_port_phylink_create(struct dsa_port *dp)
1677 {
1678 	struct dsa_switch *ds = dp->ds;
1679 	phy_interface_t mode;
1680 	struct phylink *pl;
1681 	int err;
1682 
1683 	err = of_get_phy_mode(dp->dn, &mode);
1684 	if (err)
1685 		mode = PHY_INTERFACE_MODE_NA;
1686 
1687 	/* Presence of phylink_mac_link_state or phylink_mac_an_restart is
1688 	 * an indicator of a legacy phylink driver.
1689 	 */
1690 	if (ds->ops->phylink_mac_link_state ||
1691 	    ds->ops->phylink_mac_an_restart)
1692 		dp->pl_config.legacy_pre_march2020 = true;
1693 
1694 	if (ds->ops->phylink_get_caps)
1695 		ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config);
1696 
1697 	pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn),
1698 			    mode, &dsa_port_phylink_mac_ops);
1699 	if (IS_ERR(pl)) {
1700 		pr_err("error creating PHYLINK: %ld\n", PTR_ERR(pl));
1701 		return PTR_ERR(pl);
1702 	}
1703 
1704 	dp->pl = pl;
1705 
1706 	return 0;
1707 }
1708 
1709 void dsa_port_phylink_destroy(struct dsa_port *dp)
1710 {
1711 	phylink_destroy(dp->pl);
1712 	dp->pl = NULL;
1713 }
1714 
1715 static int dsa_shared_port_setup_phy_of(struct dsa_port *dp, bool enable)
1716 {
1717 	struct dsa_switch *ds = dp->ds;
1718 	struct phy_device *phydev;
1719 	int port = dp->index;
1720 	int err = 0;
1721 
1722 	phydev = dsa_port_get_phy_device(dp);
1723 	if (!phydev)
1724 		return 0;
1725 
1726 	if (IS_ERR(phydev))
1727 		return PTR_ERR(phydev);
1728 
1729 	if (enable) {
1730 		err = genphy_resume(phydev);
1731 		if (err < 0)
1732 			goto err_put_dev;
1733 
1734 		err = genphy_read_status(phydev);
1735 		if (err < 0)
1736 			goto err_put_dev;
1737 	} else {
1738 		err = genphy_suspend(phydev);
1739 		if (err < 0)
1740 			goto err_put_dev;
1741 	}
1742 
1743 	if (ds->ops->adjust_link)
1744 		ds->ops->adjust_link(ds, port, phydev);
1745 
1746 	dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1747 
1748 err_put_dev:
1749 	put_device(&phydev->mdio.dev);
1750 	return err;
1751 }
1752 
1753 static int dsa_shared_port_fixed_link_register_of(struct dsa_port *dp)
1754 {
1755 	struct device_node *dn = dp->dn;
1756 	struct dsa_switch *ds = dp->ds;
1757 	struct phy_device *phydev;
1758 	int port = dp->index;
1759 	phy_interface_t mode;
1760 	int err;
1761 
1762 	err = of_phy_register_fixed_link(dn);
1763 	if (err) {
1764 		dev_err(ds->dev,
1765 			"failed to register the fixed PHY of port %d\n",
1766 			port);
1767 		return err;
1768 	}
1769 
1770 	phydev = of_phy_find_device(dn);
1771 
1772 	err = of_get_phy_mode(dn, &mode);
1773 	if (err)
1774 		mode = PHY_INTERFACE_MODE_NA;
1775 	phydev->interface = mode;
1776 
1777 	genphy_read_status(phydev);
1778 
1779 	if (ds->ops->adjust_link)
1780 		ds->ops->adjust_link(ds, port, phydev);
1781 
1782 	put_device(&phydev->mdio.dev);
1783 
1784 	return 0;
1785 }
1786 
1787 static int dsa_shared_port_phylink_register(struct dsa_port *dp)
1788 {
1789 	struct dsa_switch *ds = dp->ds;
1790 	struct device_node *port_dn = dp->dn;
1791 	int err;
1792 
1793 	dp->pl_config.dev = ds->dev;
1794 	dp->pl_config.type = PHYLINK_DEV;
1795 
1796 	err = dsa_port_phylink_create(dp);
1797 	if (err)
1798 		return err;
1799 
1800 	err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1801 	if (err && err != -ENODEV) {
1802 		pr_err("could not attach to PHY: %d\n", err);
1803 		goto err_phy_connect;
1804 	}
1805 
1806 	return 0;
1807 
1808 err_phy_connect:
1809 	dsa_port_phylink_destroy(dp);
1810 	return err;
1811 }
1812 
1813 /* During the initial DSA driver migration to OF, port nodes were sometimes
1814  * added to device trees with no indication of how they should operate from a
1815  * link management perspective (phy-handle, fixed-link, etc). Additionally, the
1816  * phy-mode may be absent. The interpretation of these port OF nodes depends on
1817  * their type.
1818  *
1819  * User ports with no phy-handle or fixed-link are expected to connect to an
1820  * internal PHY located on the ds->slave_mii_bus at an MDIO address equal to
1821  * the port number. This description is still actively supported.
1822  *
1823  * Shared (CPU and DSA) ports with no phy-handle or fixed-link are expected to
1824  * operate at the maximum speed that their phy-mode is capable of. If the
1825  * phy-mode is absent, they are expected to operate using the phy-mode
1826  * supported by the port that gives the highest link speed. It is unspecified
1827  * if the port should use flow control or not, half duplex or full duplex, or
1828  * if the phy-mode is a SERDES link, whether in-band autoneg is expected to be
1829  * enabled or not.
1830  *
1831  * In the latter case of shared ports, omitting the link management description
1832  * from the firmware node is deprecated and strongly discouraged. DSA uses
1833  * phylink, which rejects the firmware nodes of these ports for lacking
1834  * required properties.
1835  *
1836  * For switches in this table, DSA will skip enforcing validation and will
1837  * later omit registering a phylink instance for the shared ports, if they lack
1838  * a fixed-link, a phy-handle, or a managed = "in-band-status" property.
1839  * It becomes the responsibility of the driver to ensure that these ports
1840  * operate at the maximum speed (whatever this means) and will interoperate
1841  * with the DSA master or other cascade port, since phylink methods will not be
1842  * invoked for them.
1843  *
1844  * If you are considering expanding this table for newly introduced switches,
1845  * think again. It is OK to remove switches from this table if there aren't DT
1846  * blobs in circulation which rely on defaulting the shared ports.
1847  */
1848 static const char * const dsa_switches_apply_workarounds[] = {
1849 #if IS_ENABLED(CONFIG_NET_DSA_XRS700X)
1850 	"arrow,xrs7003e",
1851 	"arrow,xrs7003f",
1852 	"arrow,xrs7004e",
1853 	"arrow,xrs7004f",
1854 #endif
1855 #if IS_ENABLED(CONFIG_B53)
1856 	"brcm,bcm5325",
1857 	"brcm,bcm53115",
1858 	"brcm,bcm53125",
1859 	"brcm,bcm53128",
1860 	"brcm,bcm5365",
1861 	"brcm,bcm5389",
1862 	"brcm,bcm5395",
1863 	"brcm,bcm5397",
1864 	"brcm,bcm5398",
1865 	"brcm,bcm53010-srab",
1866 	"brcm,bcm53011-srab",
1867 	"brcm,bcm53012-srab",
1868 	"brcm,bcm53018-srab",
1869 	"brcm,bcm53019-srab",
1870 	"brcm,bcm5301x-srab",
1871 	"brcm,bcm11360-srab",
1872 	"brcm,bcm58522-srab",
1873 	"brcm,bcm58525-srab",
1874 	"brcm,bcm58535-srab",
1875 	"brcm,bcm58622-srab",
1876 	"brcm,bcm58623-srab",
1877 	"brcm,bcm58625-srab",
1878 	"brcm,bcm88312-srab",
1879 	"brcm,cygnus-srab",
1880 	"brcm,nsp-srab",
1881 	"brcm,omega-srab",
1882 	"brcm,bcm3384-switch",
1883 	"brcm,bcm6328-switch",
1884 	"brcm,bcm6368-switch",
1885 	"brcm,bcm63xx-switch",
1886 #endif
1887 #if IS_ENABLED(CONFIG_NET_DSA_BCM_SF2)
1888 	"brcm,bcm7445-switch-v4.0",
1889 	"brcm,bcm7278-switch-v4.0",
1890 	"brcm,bcm7278-switch-v4.8",
1891 #endif
1892 #if IS_ENABLED(CONFIG_NET_DSA_LANTIQ_GSWIP)
1893 	"lantiq,xrx200-gswip",
1894 	"lantiq,xrx300-gswip",
1895 	"lantiq,xrx330-gswip",
1896 #endif
1897 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6060)
1898 	"marvell,mv88e6060",
1899 #endif
1900 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6XXX)
1901 	"marvell,mv88e6085",
1902 	"marvell,mv88e6190",
1903 	"marvell,mv88e6250",
1904 #endif
1905 #if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON)
1906 	"microchip,ksz8765",
1907 	"microchip,ksz8794",
1908 	"microchip,ksz8795",
1909 	"microchip,ksz8863",
1910 	"microchip,ksz8873",
1911 	"microchip,ksz9477",
1912 	"microchip,ksz9897",
1913 	"microchip,ksz9893",
1914 	"microchip,ksz9563",
1915 	"microchip,ksz8563",
1916 	"microchip,ksz9567",
1917 #endif
1918 #if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_MDIO)
1919 	"smsc,lan9303-mdio",
1920 #endif
1921 #if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_I2C)
1922 	"smsc,lan9303-i2c",
1923 #endif
1924 	NULL,
1925 };
1926 
1927 static void dsa_shared_port_validate_of(struct dsa_port *dp,
1928 					bool *missing_phy_mode,
1929 					bool *missing_link_description)
1930 {
1931 	struct device_node *dn = dp->dn, *phy_np;
1932 	struct dsa_switch *ds = dp->ds;
1933 	phy_interface_t mode;
1934 
1935 	*missing_phy_mode = false;
1936 	*missing_link_description = false;
1937 
1938 	if (of_get_phy_mode(dn, &mode)) {
1939 		*missing_phy_mode = true;
1940 		dev_err(ds->dev,
1941 			"OF node %pOF of %s port %d lacks the required \"phy-mode\" property\n",
1942 			dn, dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
1943 	}
1944 
1945 	/* Note: of_phy_is_fixed_link() also returns true for
1946 	 * managed = "in-band-status"
1947 	 */
1948 	if (of_phy_is_fixed_link(dn))
1949 		return;
1950 
1951 	phy_np = of_parse_phandle(dn, "phy-handle", 0);
1952 	if (phy_np) {
1953 		of_node_put(phy_np);
1954 		return;
1955 	}
1956 
1957 	*missing_link_description = true;
1958 
1959 	dev_err(ds->dev,
1960 		"OF node %pOF of %s port %d lacks the required \"phy-handle\", \"fixed-link\" or \"managed\" properties\n",
1961 		dn, dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
1962 }
1963 
1964 int dsa_shared_port_link_register_of(struct dsa_port *dp)
1965 {
1966 	struct dsa_switch *ds = dp->ds;
1967 	bool missing_link_description;
1968 	bool missing_phy_mode;
1969 	int port = dp->index;
1970 
1971 	dsa_shared_port_validate_of(dp, &missing_phy_mode,
1972 				    &missing_link_description);
1973 
1974 	if ((missing_phy_mode || missing_link_description) &&
1975 	    !of_device_compatible_match(ds->dev->of_node,
1976 					dsa_switches_apply_workarounds))
1977 		return -EINVAL;
1978 
1979 	if (!ds->ops->adjust_link) {
1980 		if (missing_link_description) {
1981 			dev_warn(ds->dev,
1982 				 "Skipping phylink registration for %s port %d\n",
1983 				 dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
1984 		} else {
1985 			if (ds->ops->phylink_mac_link_down)
1986 				ds->ops->phylink_mac_link_down(ds, port,
1987 					MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1988 
1989 			return dsa_shared_port_phylink_register(dp);
1990 		}
1991 		return 0;
1992 	}
1993 
1994 	dev_warn(ds->dev,
1995 		 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1996 
1997 	if (of_phy_is_fixed_link(dp->dn))
1998 		return dsa_shared_port_fixed_link_register_of(dp);
1999 	else
2000 		return dsa_shared_port_setup_phy_of(dp, true);
2001 }
2002 
2003 void dsa_shared_port_link_unregister_of(struct dsa_port *dp)
2004 {
2005 	struct dsa_switch *ds = dp->ds;
2006 
2007 	if (!ds->ops->adjust_link && dp->pl) {
2008 		rtnl_lock();
2009 		phylink_disconnect_phy(dp->pl);
2010 		rtnl_unlock();
2011 		dsa_port_phylink_destroy(dp);
2012 		return;
2013 	}
2014 
2015 	if (of_phy_is_fixed_link(dp->dn))
2016 		of_phy_deregister_fixed_link(dp->dn);
2017 	else
2018 		dsa_shared_port_setup_phy_of(dp, false);
2019 }
2020 
2021 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
2022 {
2023 	struct dsa_switch *ds = dp->ds;
2024 	int err;
2025 
2026 	if (!ds->ops->port_hsr_join)
2027 		return -EOPNOTSUPP;
2028 
2029 	dp->hsr_dev = hsr;
2030 
2031 	err = ds->ops->port_hsr_join(ds, dp->index, hsr);
2032 	if (err)
2033 		dp->hsr_dev = NULL;
2034 
2035 	return err;
2036 }
2037 
2038 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
2039 {
2040 	struct dsa_switch *ds = dp->ds;
2041 	int err;
2042 
2043 	dp->hsr_dev = NULL;
2044 
2045 	if (ds->ops->port_hsr_leave) {
2046 		err = ds->ops->port_hsr_leave(ds, dp->index, hsr);
2047 		if (err)
2048 			dev_err(dp->ds->dev,
2049 				"port %d failed to leave HSR %s: %pe\n",
2050 				dp->index, hsr->name, ERR_PTR(err));
2051 	}
2052 }
2053 
2054 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
2055 {
2056 	struct dsa_notifier_tag_8021q_vlan_info info = {
2057 		.dp = dp,
2058 		.vid = vid,
2059 	};
2060 
2061 	if (broadcast)
2062 		return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
2063 
2064 	return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
2065 }
2066 
2067 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
2068 {
2069 	struct dsa_notifier_tag_8021q_vlan_info info = {
2070 		.dp = dp,
2071 		.vid = vid,
2072 	};
2073 	int err;
2074 
2075 	if (broadcast)
2076 		err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
2077 	else
2078 		err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
2079 	if (err)
2080 		dev_err(dp->ds->dev,
2081 			"port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
2082 			dp->index, vid, ERR_PTR(err));
2083 }
2084