xref: /openbmc/linux/net/dsa/port.c (revision b4e18b29)
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/notifier.h>
11 #include <linux/of_mdio.h>
12 #include <linux/of_net.h>
13 
14 #include "dsa_priv.h"
15 
16 /**
17  * dsa_port_notify - Notify the switching fabric of changes to a port
18  * @dp: port on which change occurred
19  * @e: event, must be of type DSA_NOTIFIER_*
20  * @v: event-specific value.
21  *
22  * Notify all switches in the DSA tree that this port's switch belongs to,
23  * including this switch itself, of an event. Allows the other switches to
24  * reconfigure themselves for cross-chip operations. Can also be used to
25  * reconfigure ports without net_devices (CPU ports, DSA links) whenever
26  * a user port's state changes.
27  */
28 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
29 {
30 	return dsa_tree_notify(dp->ds->dst, e, v);
31 }
32 
33 int dsa_port_set_state(struct dsa_port *dp, u8 state)
34 {
35 	struct dsa_switch *ds = dp->ds;
36 	int port = dp->index;
37 
38 	if (!ds->ops->port_stp_state_set)
39 		return -EOPNOTSUPP;
40 
41 	ds->ops->port_stp_state_set(ds, port, state);
42 
43 	if (ds->ops->port_fast_age) {
44 		/* Fast age FDB entries or flush appropriate forwarding database
45 		 * for the given port, if we are moving it from Learning or
46 		 * Forwarding state, to Disabled or Blocking or Listening state.
47 		 */
48 
49 		if ((dp->stp_state == BR_STATE_LEARNING ||
50 		     dp->stp_state == BR_STATE_FORWARDING) &&
51 		    (state == BR_STATE_DISABLED ||
52 		     state == BR_STATE_BLOCKING ||
53 		     state == BR_STATE_LISTENING))
54 			ds->ops->port_fast_age(ds, port);
55 	}
56 
57 	dp->stp_state = state;
58 
59 	return 0;
60 }
61 
62 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
63 {
64 	int err;
65 
66 	err = dsa_port_set_state(dp, state);
67 	if (err)
68 		pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
69 }
70 
71 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
72 {
73 	struct dsa_switch *ds = dp->ds;
74 	int port = dp->index;
75 	int err;
76 
77 	if (ds->ops->port_enable) {
78 		err = ds->ops->port_enable(ds, port, phy);
79 		if (err)
80 			return err;
81 	}
82 
83 	if (!dp->bridge_dev)
84 		dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
85 
86 	if (dp->pl)
87 		phylink_start(dp->pl);
88 
89 	return 0;
90 }
91 
92 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
93 {
94 	int err;
95 
96 	rtnl_lock();
97 	err = dsa_port_enable_rt(dp, phy);
98 	rtnl_unlock();
99 
100 	return err;
101 }
102 
103 void dsa_port_disable_rt(struct dsa_port *dp)
104 {
105 	struct dsa_switch *ds = dp->ds;
106 	int port = dp->index;
107 
108 	if (dp->pl)
109 		phylink_stop(dp->pl);
110 
111 	if (!dp->bridge_dev)
112 		dsa_port_set_state_now(dp, BR_STATE_DISABLED);
113 
114 	if (ds->ops->port_disable)
115 		ds->ops->port_disable(ds, port);
116 }
117 
118 void dsa_port_disable(struct dsa_port *dp)
119 {
120 	rtnl_lock();
121 	dsa_port_disable_rt(dp);
122 	rtnl_unlock();
123 }
124 
125 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
126 {
127 	struct dsa_notifier_bridge_info info = {
128 		.tree_index = dp->ds->dst->index,
129 		.sw_index = dp->ds->index,
130 		.port = dp->index,
131 		.br = br,
132 	};
133 	int err;
134 
135 	/* Set the flooding mode before joining the port in the switch */
136 	err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD);
137 	if (err)
138 		return err;
139 
140 	/* Here the interface is already bridged. Reflect the current
141 	 * configuration so that drivers can program their chips accordingly.
142 	 */
143 	dp->bridge_dev = br;
144 
145 	err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
146 
147 	/* The bridging is rolled back on error */
148 	if (err) {
149 		dsa_port_bridge_flags(dp, 0);
150 		dp->bridge_dev = NULL;
151 	}
152 
153 	return err;
154 }
155 
156 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
157 {
158 	struct dsa_notifier_bridge_info info = {
159 		.tree_index = dp->ds->dst->index,
160 		.sw_index = dp->ds->index,
161 		.port = dp->index,
162 		.br = br,
163 	};
164 	int err;
165 
166 	/* Here the port is already unbridged. Reflect the current configuration
167 	 * so that drivers can program their chips accordingly.
168 	 */
169 	dp->bridge_dev = NULL;
170 
171 	err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
172 	if (err)
173 		pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
174 
175 	/* Port is leaving the bridge, disable flooding */
176 	dsa_port_bridge_flags(dp, 0);
177 
178 	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
179 	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
180 	 */
181 	dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
182 }
183 
184 int dsa_port_lag_change(struct dsa_port *dp,
185 			struct netdev_lag_lower_state_info *linfo)
186 {
187 	struct dsa_notifier_lag_info info = {
188 		.sw_index = dp->ds->index,
189 		.port = dp->index,
190 	};
191 	bool tx_enabled;
192 
193 	if (!dp->lag_dev)
194 		return 0;
195 
196 	/* On statically configured aggregates (e.g. loadbalance
197 	 * without LACP) ports will always be tx_enabled, even if the
198 	 * link is down. Thus we require both link_up and tx_enabled
199 	 * in order to include it in the tx set.
200 	 */
201 	tx_enabled = linfo->link_up && linfo->tx_enabled;
202 
203 	if (tx_enabled == dp->lag_tx_enabled)
204 		return 0;
205 
206 	dp->lag_tx_enabled = tx_enabled;
207 
208 	return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
209 }
210 
211 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag,
212 		      struct netdev_lag_upper_info *uinfo)
213 {
214 	struct dsa_notifier_lag_info info = {
215 		.sw_index = dp->ds->index,
216 		.port = dp->index,
217 		.lag = lag,
218 		.info = uinfo,
219 	};
220 	int err;
221 
222 	dsa_lag_map(dp->ds->dst, lag);
223 	dp->lag_dev = lag;
224 
225 	err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
226 	if (err) {
227 		dp->lag_dev = NULL;
228 		dsa_lag_unmap(dp->ds->dst, lag);
229 	}
230 
231 	return err;
232 }
233 
234 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag)
235 {
236 	struct dsa_notifier_lag_info info = {
237 		.sw_index = dp->ds->index,
238 		.port = dp->index,
239 		.lag = lag,
240 	};
241 	int err;
242 
243 	if (!dp->lag_dev)
244 		return;
245 
246 	/* Port might have been part of a LAG that in turn was
247 	 * attached to a bridge.
248 	 */
249 	if (dp->bridge_dev)
250 		dsa_port_bridge_leave(dp, dp->bridge_dev);
251 
252 	dp->lag_tx_enabled = false;
253 	dp->lag_dev = NULL;
254 
255 	err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
256 	if (err)
257 		pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n",
258 		       err);
259 
260 	dsa_lag_unmap(dp->ds->dst, lag);
261 }
262 
263 /* Must be called under rcu_read_lock() */
264 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
265 					      bool vlan_filtering)
266 {
267 	struct dsa_switch *ds = dp->ds;
268 	int err, i;
269 
270 	/* VLAN awareness was off, so the question is "can we turn it on".
271 	 * We may have had 8021q uppers, those need to go. Make sure we don't
272 	 * enter an inconsistent state: deny changing the VLAN awareness state
273 	 * as long as we have 8021q uppers.
274 	 */
275 	if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
276 		struct net_device *upper_dev, *slave = dp->slave;
277 		struct net_device *br = dp->bridge_dev;
278 		struct list_head *iter;
279 
280 		netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
281 			struct bridge_vlan_info br_info;
282 			u16 vid;
283 
284 			if (!is_vlan_dev(upper_dev))
285 				continue;
286 
287 			vid = vlan_dev_vlan_id(upper_dev);
288 
289 			/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
290 			 * device, respectively the VID is not found, returning
291 			 * 0 means success, which is a failure for us here.
292 			 */
293 			err = br_vlan_get_info(br, vid, &br_info);
294 			if (err == 0) {
295 				dev_err(ds->dev, "Must remove upper %s first\n",
296 					upper_dev->name);
297 				return false;
298 			}
299 		}
300 	}
301 
302 	if (!ds->vlan_filtering_is_global)
303 		return true;
304 
305 	/* For cases where enabling/disabling VLAN awareness is global to the
306 	 * switch, we need to handle the case where multiple bridges span
307 	 * different ports of the same switch device and one of them has a
308 	 * different setting than what is being requested.
309 	 */
310 	for (i = 0; i < ds->num_ports; i++) {
311 		struct net_device *other_bridge;
312 
313 		other_bridge = dsa_to_port(ds, i)->bridge_dev;
314 		if (!other_bridge)
315 			continue;
316 		/* If it's the same bridge, it also has same
317 		 * vlan_filtering setting => no need to check
318 		 */
319 		if (other_bridge == dp->bridge_dev)
320 			continue;
321 		if (br_vlan_enabled(other_bridge) != vlan_filtering) {
322 			dev_err(ds->dev, "VLAN filtering is a global setting\n");
323 			return false;
324 		}
325 	}
326 	return true;
327 }
328 
329 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering)
330 {
331 	struct dsa_switch *ds = dp->ds;
332 	bool apply;
333 	int err;
334 
335 	if (!ds->ops->port_vlan_filtering)
336 		return -EOPNOTSUPP;
337 
338 	/* We are called from dsa_slave_switchdev_blocking_event(),
339 	 * which is not under rcu_read_lock(), unlike
340 	 * dsa_slave_switchdev_event().
341 	 */
342 	rcu_read_lock();
343 	apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering);
344 	rcu_read_unlock();
345 	if (!apply)
346 		return -EINVAL;
347 
348 	if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
349 		return 0;
350 
351 	err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering);
352 	if (err)
353 		return err;
354 
355 	if (ds->vlan_filtering_is_global)
356 		ds->vlan_filtering = vlan_filtering;
357 	else
358 		dp->vlan_filtering = vlan_filtering;
359 
360 	return 0;
361 }
362 
363 /* This enforces legacy behavior for switch drivers which assume they can't
364  * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
365  */
366 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
367 {
368 	struct dsa_switch *ds = dp->ds;
369 
370 	if (!dp->bridge_dev)
371 		return false;
372 
373 	return (!ds->configure_vlan_while_not_filtering &&
374 		!br_vlan_enabled(dp->bridge_dev));
375 }
376 
377 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
378 {
379 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
380 	unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
381 	struct dsa_notifier_ageing_time_info info;
382 	int err;
383 
384 	info.ageing_time = ageing_time;
385 
386 	err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
387 	if (err)
388 		return err;
389 
390 	dp->ageing_time = ageing_time;
391 
392 	return 0;
393 }
394 
395 int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags)
396 {
397 	struct dsa_switch *ds = dp->ds;
398 
399 	if (!ds->ops->port_egress_floods ||
400 	    (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
401 		return -EINVAL;
402 
403 	return 0;
404 }
405 
406 int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags)
407 {
408 	struct dsa_switch *ds = dp->ds;
409 	int port = dp->index;
410 	int err = 0;
411 
412 	if (ds->ops->port_egress_floods)
413 		err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
414 						  flags & BR_MCAST_FLOOD);
415 
416 	return err;
417 }
418 
419 int dsa_port_mrouter(struct dsa_port *dp, bool mrouter)
420 {
421 	struct dsa_switch *ds = dp->ds;
422 	int port = dp->index;
423 
424 	if (!ds->ops->port_egress_floods)
425 		return -EOPNOTSUPP;
426 
427 	return ds->ops->port_egress_floods(ds, port, true, mrouter);
428 }
429 
430 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
431 			bool propagate_upstream)
432 {
433 	struct dsa_notifier_mtu_info info = {
434 		.sw_index = dp->ds->index,
435 		.propagate_upstream = propagate_upstream,
436 		.port = dp->index,
437 		.mtu = new_mtu,
438 	};
439 
440 	return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
441 }
442 
443 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
444 		     u16 vid)
445 {
446 	struct dsa_notifier_fdb_info info = {
447 		.sw_index = dp->ds->index,
448 		.port = dp->index,
449 		.addr = addr,
450 		.vid = vid,
451 	};
452 
453 	return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
454 }
455 
456 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
457 		     u16 vid)
458 {
459 	struct dsa_notifier_fdb_info info = {
460 		.sw_index = dp->ds->index,
461 		.port = dp->index,
462 		.addr = addr,
463 		.vid = vid,
464 
465 	};
466 
467 	return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
468 }
469 
470 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
471 {
472 	struct dsa_switch *ds = dp->ds;
473 	int port = dp->index;
474 
475 	if (!ds->ops->port_fdb_dump)
476 		return -EOPNOTSUPP;
477 
478 	return ds->ops->port_fdb_dump(ds, port, cb, data);
479 }
480 
481 int dsa_port_mdb_add(const struct dsa_port *dp,
482 		     const struct switchdev_obj_port_mdb *mdb)
483 {
484 	struct dsa_notifier_mdb_info info = {
485 		.sw_index = dp->ds->index,
486 		.port = dp->index,
487 		.mdb = mdb,
488 	};
489 
490 	return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
491 }
492 
493 int dsa_port_mdb_del(const struct dsa_port *dp,
494 		     const struct switchdev_obj_port_mdb *mdb)
495 {
496 	struct dsa_notifier_mdb_info info = {
497 		.sw_index = dp->ds->index,
498 		.port = dp->index,
499 		.mdb = mdb,
500 	};
501 
502 	return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
503 }
504 
505 int dsa_port_vlan_add(struct dsa_port *dp,
506 		      const struct switchdev_obj_port_vlan *vlan)
507 {
508 	struct dsa_notifier_vlan_info info = {
509 		.sw_index = dp->ds->index,
510 		.port = dp->index,
511 		.vlan = vlan,
512 	};
513 
514 	return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
515 }
516 
517 int dsa_port_vlan_del(struct dsa_port *dp,
518 		      const struct switchdev_obj_port_vlan *vlan)
519 {
520 	struct dsa_notifier_vlan_info info = {
521 		.sw_index = dp->ds->index,
522 		.port = dp->index,
523 		.vlan = vlan,
524 	};
525 
526 	return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
527 }
528 
529 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
530 			       const struct dsa_device_ops *tag_ops)
531 {
532 	cpu_dp->filter = tag_ops->filter;
533 	cpu_dp->rcv = tag_ops->rcv;
534 	cpu_dp->tag_ops = tag_ops;
535 }
536 
537 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
538 {
539 	struct device_node *phy_dn;
540 	struct phy_device *phydev;
541 
542 	phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
543 	if (!phy_dn)
544 		return NULL;
545 
546 	phydev = of_phy_find_device(phy_dn);
547 	if (!phydev) {
548 		of_node_put(phy_dn);
549 		return ERR_PTR(-EPROBE_DEFER);
550 	}
551 
552 	of_node_put(phy_dn);
553 	return phydev;
554 }
555 
556 static void dsa_port_phylink_validate(struct phylink_config *config,
557 				      unsigned long *supported,
558 				      struct phylink_link_state *state)
559 {
560 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
561 	struct dsa_switch *ds = dp->ds;
562 
563 	if (!ds->ops->phylink_validate)
564 		return;
565 
566 	ds->ops->phylink_validate(ds, dp->index, supported, state);
567 }
568 
569 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
570 					       struct phylink_link_state *state)
571 {
572 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
573 	struct dsa_switch *ds = dp->ds;
574 	int err;
575 
576 	/* Only called for inband modes */
577 	if (!ds->ops->phylink_mac_link_state) {
578 		state->link = 0;
579 		return;
580 	}
581 
582 	err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
583 	if (err < 0) {
584 		dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
585 			dp->index, err);
586 		state->link = 0;
587 	}
588 }
589 
590 static void dsa_port_phylink_mac_config(struct phylink_config *config,
591 					unsigned int mode,
592 					const struct phylink_link_state *state)
593 {
594 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
595 	struct dsa_switch *ds = dp->ds;
596 
597 	if (!ds->ops->phylink_mac_config)
598 		return;
599 
600 	ds->ops->phylink_mac_config(ds, dp->index, mode, state);
601 }
602 
603 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
604 {
605 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
606 	struct dsa_switch *ds = dp->ds;
607 
608 	if (!ds->ops->phylink_mac_an_restart)
609 		return;
610 
611 	ds->ops->phylink_mac_an_restart(ds, dp->index);
612 }
613 
614 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
615 					   unsigned int mode,
616 					   phy_interface_t interface)
617 {
618 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
619 	struct phy_device *phydev = NULL;
620 	struct dsa_switch *ds = dp->ds;
621 
622 	if (dsa_is_user_port(ds, dp->index))
623 		phydev = dp->slave->phydev;
624 
625 	if (!ds->ops->phylink_mac_link_down) {
626 		if (ds->ops->adjust_link && phydev)
627 			ds->ops->adjust_link(ds, dp->index, phydev);
628 		return;
629 	}
630 
631 	ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
632 }
633 
634 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
635 					 struct phy_device *phydev,
636 					 unsigned int mode,
637 					 phy_interface_t interface,
638 					 int speed, int duplex,
639 					 bool tx_pause, bool rx_pause)
640 {
641 	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
642 	struct dsa_switch *ds = dp->ds;
643 
644 	if (!ds->ops->phylink_mac_link_up) {
645 		if (ds->ops->adjust_link && phydev)
646 			ds->ops->adjust_link(ds, dp->index, phydev);
647 		return;
648 	}
649 
650 	ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
651 				     speed, duplex, tx_pause, rx_pause);
652 }
653 
654 const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
655 	.validate = dsa_port_phylink_validate,
656 	.mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
657 	.mac_config = dsa_port_phylink_mac_config,
658 	.mac_an_restart = dsa_port_phylink_mac_an_restart,
659 	.mac_link_down = dsa_port_phylink_mac_link_down,
660 	.mac_link_up = dsa_port_phylink_mac_link_up,
661 };
662 
663 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
664 {
665 	struct dsa_switch *ds = dp->ds;
666 	struct phy_device *phydev;
667 	int port = dp->index;
668 	int err = 0;
669 
670 	phydev = dsa_port_get_phy_device(dp);
671 	if (!phydev)
672 		return 0;
673 
674 	if (IS_ERR(phydev))
675 		return PTR_ERR(phydev);
676 
677 	if (enable) {
678 		err = genphy_resume(phydev);
679 		if (err < 0)
680 			goto err_put_dev;
681 
682 		err = genphy_read_status(phydev);
683 		if (err < 0)
684 			goto err_put_dev;
685 	} else {
686 		err = genphy_suspend(phydev);
687 		if (err < 0)
688 			goto err_put_dev;
689 	}
690 
691 	if (ds->ops->adjust_link)
692 		ds->ops->adjust_link(ds, port, phydev);
693 
694 	dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
695 
696 err_put_dev:
697 	put_device(&phydev->mdio.dev);
698 	return err;
699 }
700 
701 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
702 {
703 	struct device_node *dn = dp->dn;
704 	struct dsa_switch *ds = dp->ds;
705 	struct phy_device *phydev;
706 	int port = dp->index;
707 	phy_interface_t mode;
708 	int err;
709 
710 	err = of_phy_register_fixed_link(dn);
711 	if (err) {
712 		dev_err(ds->dev,
713 			"failed to register the fixed PHY of port %d\n",
714 			port);
715 		return err;
716 	}
717 
718 	phydev = of_phy_find_device(dn);
719 
720 	err = of_get_phy_mode(dn, &mode);
721 	if (err)
722 		mode = PHY_INTERFACE_MODE_NA;
723 	phydev->interface = mode;
724 
725 	genphy_read_status(phydev);
726 
727 	if (ds->ops->adjust_link)
728 		ds->ops->adjust_link(ds, port, phydev);
729 
730 	put_device(&phydev->mdio.dev);
731 
732 	return 0;
733 }
734 
735 static int dsa_port_phylink_register(struct dsa_port *dp)
736 {
737 	struct dsa_switch *ds = dp->ds;
738 	struct device_node *port_dn = dp->dn;
739 	phy_interface_t mode;
740 	int err;
741 
742 	err = of_get_phy_mode(port_dn, &mode);
743 	if (err)
744 		mode = PHY_INTERFACE_MODE_NA;
745 
746 	dp->pl_config.dev = ds->dev;
747 	dp->pl_config.type = PHYLINK_DEV;
748 	dp->pl_config.pcs_poll = ds->pcs_poll;
749 
750 	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
751 				mode, &dsa_port_phylink_mac_ops);
752 	if (IS_ERR(dp->pl)) {
753 		pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
754 		return PTR_ERR(dp->pl);
755 	}
756 
757 	err = phylink_of_phy_connect(dp->pl, port_dn, 0);
758 	if (err && err != -ENODEV) {
759 		pr_err("could not attach to PHY: %d\n", err);
760 		goto err_phy_connect;
761 	}
762 
763 	return 0;
764 
765 err_phy_connect:
766 	phylink_destroy(dp->pl);
767 	return err;
768 }
769 
770 int dsa_port_link_register_of(struct dsa_port *dp)
771 {
772 	struct dsa_switch *ds = dp->ds;
773 	struct device_node *phy_np;
774 	int port = dp->index;
775 
776 	if (!ds->ops->adjust_link) {
777 		phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
778 		if (of_phy_is_fixed_link(dp->dn) || phy_np) {
779 			if (ds->ops->phylink_mac_link_down)
780 				ds->ops->phylink_mac_link_down(ds, port,
781 					MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
782 			return dsa_port_phylink_register(dp);
783 		}
784 		return 0;
785 	}
786 
787 	dev_warn(ds->dev,
788 		 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
789 
790 	if (of_phy_is_fixed_link(dp->dn))
791 		return dsa_port_fixed_link_register_of(dp);
792 	else
793 		return dsa_port_setup_phy_of(dp, true);
794 }
795 
796 void dsa_port_link_unregister_of(struct dsa_port *dp)
797 {
798 	struct dsa_switch *ds = dp->ds;
799 
800 	if (!ds->ops->adjust_link && dp->pl) {
801 		rtnl_lock();
802 		phylink_disconnect_phy(dp->pl);
803 		rtnl_unlock();
804 		phylink_destroy(dp->pl);
805 		dp->pl = NULL;
806 		return;
807 	}
808 
809 	if (of_phy_is_fixed_link(dp->dn))
810 		of_phy_deregister_fixed_link(dp->dn);
811 	else
812 		dsa_port_setup_phy_of(dp, false);
813 }
814 
815 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
816 {
817 	struct phy_device *phydev;
818 	int ret = -EOPNOTSUPP;
819 
820 	if (of_phy_is_fixed_link(dp->dn))
821 		return ret;
822 
823 	phydev = dsa_port_get_phy_device(dp);
824 	if (IS_ERR_OR_NULL(phydev))
825 		return ret;
826 
827 	ret = phy_ethtool_get_strings(phydev, data);
828 	put_device(&phydev->mdio.dev);
829 
830 	return ret;
831 }
832 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
833 
834 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
835 {
836 	struct phy_device *phydev;
837 	int ret = -EOPNOTSUPP;
838 
839 	if (of_phy_is_fixed_link(dp->dn))
840 		return ret;
841 
842 	phydev = dsa_port_get_phy_device(dp);
843 	if (IS_ERR_OR_NULL(phydev))
844 		return ret;
845 
846 	ret = phy_ethtool_get_stats(phydev, NULL, data);
847 	put_device(&phydev->mdio.dev);
848 
849 	return ret;
850 }
851 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
852 
853 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
854 {
855 	struct phy_device *phydev;
856 	int ret = -EOPNOTSUPP;
857 
858 	if (of_phy_is_fixed_link(dp->dn))
859 		return ret;
860 
861 	phydev = dsa_port_get_phy_device(dp);
862 	if (IS_ERR_OR_NULL(phydev))
863 		return ret;
864 
865 	ret = phy_ethtool_get_sset_count(phydev);
866 	put_device(&phydev->mdio.dev);
867 
868 	return ret;
869 }
870 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);
871 
872 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
873 {
874 	struct dsa_notifier_hsr_info info = {
875 		.sw_index = dp->ds->index,
876 		.port = dp->index,
877 		.hsr = hsr,
878 	};
879 	int err;
880 
881 	dp->hsr_dev = hsr;
882 
883 	err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info);
884 	if (err)
885 		dp->hsr_dev = NULL;
886 
887 	return err;
888 }
889 
890 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
891 {
892 	struct dsa_notifier_hsr_info info = {
893 		.sw_index = dp->ds->index,
894 		.port = dp->index,
895 		.hsr = hsr,
896 	};
897 	int err;
898 
899 	dp->hsr_dev = NULL;
900 
901 	err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info);
902 	if (err)
903 		pr_err("DSA: failed to notify DSA_NOTIFIER_HSR_LEAVE\n");
904 }
905