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