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