xref: /openbmc/linux/drivers/net/phy/phylink.c (revision 904b10fb)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * phylink models the MAC to optional PHY connection, supporting
4  * technologies such as SFP cages where the PHY is hot-pluggable.
5  *
6  * Copyright (C) 2015 Russell King
7  */
8 #include <linux/acpi.h>
9 #include <linux/ethtool.h>
10 #include <linux/export.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/netdevice.h>
13 #include <linux/of.h>
14 #include <linux/of_mdio.h>
15 #include <linux/phy.h>
16 #include <linux/phy_fixed.h>
17 #include <linux/phylink.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/spinlock.h>
20 #include <linux/timer.h>
21 #include <linux/workqueue.h>
22 
23 #include "sfp.h"
24 #include "swphy.h"
25 
26 #define SUPPORTED_INTERFACES \
27 	(SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
28 	 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
29 #define ADVERTISED_INTERFACES \
30 	(ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
31 	 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
32 
33 enum {
34 	PHYLINK_DISABLE_STOPPED,
35 	PHYLINK_DISABLE_LINK,
36 	PHYLINK_DISABLE_MAC_WOL,
37 };
38 
39 /**
40  * struct phylink - internal data type for phylink
41  */
42 struct phylink {
43 	/* private: */
44 	struct net_device *netdev;
45 	const struct phylink_mac_ops *mac_ops;
46 	const struct phylink_pcs_ops *pcs_ops;
47 	struct phylink_config *config;
48 	struct phylink_pcs *pcs;
49 	struct device *dev;
50 	unsigned int old_link_state:1;
51 
52 	unsigned long phylink_disable_state; /* bitmask of disables */
53 	struct phy_device *phydev;
54 	phy_interface_t link_interface;	/* PHY_INTERFACE_xxx */
55 	u8 cfg_link_an_mode;		/* MLO_AN_xxx */
56 	u8 cur_link_an_mode;
57 	u8 link_port;			/* The current non-phy ethtool port */
58 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
59 
60 	/* The link configuration settings */
61 	struct phylink_link_state link_config;
62 
63 	/* The current settings */
64 	phy_interface_t cur_interface;
65 
66 	struct gpio_desc *link_gpio;
67 	unsigned int link_irq;
68 	struct timer_list link_poll;
69 	void (*get_fixed_state)(struct net_device *dev,
70 				struct phylink_link_state *s);
71 
72 	struct mutex state_mutex;
73 	struct phylink_link_state phy_state;
74 	struct work_struct resolve;
75 
76 	bool mac_link_dropped;
77 
78 	struct sfp_bus *sfp_bus;
79 	bool sfp_may_have_phy;
80 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
81 	u8 sfp_port;
82 };
83 
84 #define phylink_printk(level, pl, fmt, ...) \
85 	do { \
86 		if ((pl)->config->type == PHYLINK_NETDEV) \
87 			netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
88 		else if ((pl)->config->type == PHYLINK_DEV) \
89 			dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
90 	} while (0)
91 
92 #define phylink_err(pl, fmt, ...) \
93 	phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
94 #define phylink_warn(pl, fmt, ...) \
95 	phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
96 #define phylink_info(pl, fmt, ...) \
97 	phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
98 #if defined(CONFIG_DYNAMIC_DEBUG)
99 #define phylink_dbg(pl, fmt, ...) \
100 do {									\
101 	if ((pl)->config->type == PHYLINK_NETDEV)			\
102 		netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__);		\
103 	else if ((pl)->config->type == PHYLINK_DEV)			\
104 		dev_dbg((pl)->dev, fmt, ##__VA_ARGS__);			\
105 } while (0)
106 #elif defined(DEBUG)
107 #define phylink_dbg(pl, fmt, ...)					\
108 	phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
109 #else
110 #define phylink_dbg(pl, fmt, ...)					\
111 ({									\
112 	if (0)								\
113 		phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__);	\
114 })
115 #endif
116 
117 /**
118  * phylink_set_port_modes() - set the port type modes in the ethtool mask
119  * @mask: ethtool link mode mask
120  *
121  * Sets all the port type modes in the ethtool mask.  MAC drivers should
122  * use this in their 'validate' callback.
123  */
124 void phylink_set_port_modes(unsigned long *mask)
125 {
126 	phylink_set(mask, TP);
127 	phylink_set(mask, AUI);
128 	phylink_set(mask, MII);
129 	phylink_set(mask, FIBRE);
130 	phylink_set(mask, BNC);
131 	phylink_set(mask, Backplane);
132 }
133 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
134 
135 void phylink_set_10g_modes(unsigned long *mask)
136 {
137 	phylink_set(mask, 10000baseT_Full);
138 	phylink_set(mask, 10000baseCR_Full);
139 	phylink_set(mask, 10000baseSR_Full);
140 	phylink_set(mask, 10000baseLR_Full);
141 	phylink_set(mask, 10000baseLRM_Full);
142 	phylink_set(mask, 10000baseER_Full);
143 }
144 EXPORT_SYMBOL_GPL(phylink_set_10g_modes);
145 
146 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
147 {
148 	__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
149 
150 	phylink_set_port_modes(tmp);
151 	phylink_set(tmp, Autoneg);
152 	phylink_set(tmp, Pause);
153 	phylink_set(tmp, Asym_Pause);
154 
155 	return linkmode_subset(linkmode, tmp);
156 }
157 
158 static const char *phylink_an_mode_str(unsigned int mode)
159 {
160 	static const char *modestr[] = {
161 		[MLO_AN_PHY] = "phy",
162 		[MLO_AN_FIXED] = "fixed",
163 		[MLO_AN_INBAND] = "inband",
164 	};
165 
166 	return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
167 }
168 
169 static void phylink_caps_to_linkmodes(unsigned long *linkmodes,
170 				      unsigned long caps)
171 {
172 	if (caps & MAC_SYM_PAUSE)
173 		__set_bit(ETHTOOL_LINK_MODE_Pause_BIT, linkmodes);
174 
175 	if (caps & MAC_ASYM_PAUSE)
176 		__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, linkmodes);
177 
178 	if (caps & MAC_10HD)
179 		__set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, linkmodes);
180 
181 	if (caps & MAC_10FD)
182 		__set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, linkmodes);
183 
184 	if (caps & MAC_100HD) {
185 		__set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, linkmodes);
186 		__set_bit(ETHTOOL_LINK_MODE_100baseFX_Half_BIT, linkmodes);
187 	}
188 
189 	if (caps & MAC_100FD) {
190 		__set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, linkmodes);
191 		__set_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT, linkmodes);
192 		__set_bit(ETHTOOL_LINK_MODE_100baseFX_Full_BIT, linkmodes);
193 	}
194 
195 	if (caps & MAC_1000HD)
196 		__set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, linkmodes);
197 
198 	if (caps & MAC_1000FD) {
199 		__set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, linkmodes);
200 		__set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, linkmodes);
201 		__set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, linkmodes);
202 		__set_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT, linkmodes);
203 	}
204 
205 	if (caps & MAC_2500FD) {
206 		__set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, linkmodes);
207 		__set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, linkmodes);
208 	}
209 
210 	if (caps & MAC_5000FD)
211 		__set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, linkmodes);
212 
213 	if (caps & MAC_10000FD) {
214 		__set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, linkmodes);
215 		__set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, linkmodes);
216 		__set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, linkmodes);
217 		__set_bit(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT, linkmodes);
218 		__set_bit(ETHTOOL_LINK_MODE_10000baseCR_Full_BIT, linkmodes);
219 		__set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, linkmodes);
220 		__set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, linkmodes);
221 		__set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT, linkmodes);
222 		__set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, linkmodes);
223 	}
224 
225 	if (caps & MAC_25000FD) {
226 		__set_bit(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, linkmodes);
227 		__set_bit(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, linkmodes);
228 		__set_bit(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, linkmodes);
229 	}
230 
231 	if (caps & MAC_40000FD) {
232 		__set_bit(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, linkmodes);
233 		__set_bit(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, linkmodes);
234 		__set_bit(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, linkmodes);
235 		__set_bit(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, linkmodes);
236 	}
237 
238 	if (caps & MAC_50000FD) {
239 		__set_bit(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT, linkmodes);
240 		__set_bit(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT, linkmodes);
241 		__set_bit(ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT, linkmodes);
242 		__set_bit(ETHTOOL_LINK_MODE_50000baseKR_Full_BIT, linkmodes);
243 		__set_bit(ETHTOOL_LINK_MODE_50000baseSR_Full_BIT, linkmodes);
244 		__set_bit(ETHTOOL_LINK_MODE_50000baseCR_Full_BIT, linkmodes);
245 		__set_bit(ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
246 			  linkmodes);
247 		__set_bit(ETHTOOL_LINK_MODE_50000baseDR_Full_BIT, linkmodes);
248 	}
249 
250 	if (caps & MAC_56000FD) {
251 		__set_bit(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT, linkmodes);
252 		__set_bit(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT, linkmodes);
253 		__set_bit(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT, linkmodes);
254 		__set_bit(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT, linkmodes);
255 	}
256 
257 	if (caps & MAC_100000FD) {
258 		__set_bit(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, linkmodes);
259 		__set_bit(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT, linkmodes);
260 		__set_bit(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, linkmodes);
261 		__set_bit(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
262 			  linkmodes);
263 		__set_bit(ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT, linkmodes);
264 		__set_bit(ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT, linkmodes);
265 		__set_bit(ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT, linkmodes);
266 		__set_bit(ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT,
267 			  linkmodes);
268 		__set_bit(ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT, linkmodes);
269 		__set_bit(ETHTOOL_LINK_MODE_100000baseKR_Full_BIT, linkmodes);
270 		__set_bit(ETHTOOL_LINK_MODE_100000baseSR_Full_BIT, linkmodes);
271 		__set_bit(ETHTOOL_LINK_MODE_100000baseLR_ER_FR_Full_BIT,
272 			  linkmodes);
273 		__set_bit(ETHTOOL_LINK_MODE_100000baseCR_Full_BIT, linkmodes);
274 		__set_bit(ETHTOOL_LINK_MODE_100000baseDR_Full_BIT, linkmodes);
275 	}
276 
277 	if (caps & MAC_200000FD) {
278 		__set_bit(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT, linkmodes);
279 		__set_bit(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT, linkmodes);
280 		__set_bit(ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
281 			  linkmodes);
282 		__set_bit(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT, linkmodes);
283 		__set_bit(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT, linkmodes);
284 		__set_bit(ETHTOOL_LINK_MODE_200000baseKR2_Full_BIT, linkmodes);
285 		__set_bit(ETHTOOL_LINK_MODE_200000baseSR2_Full_BIT, linkmodes);
286 		__set_bit(ETHTOOL_LINK_MODE_200000baseLR2_ER2_FR2_Full_BIT,
287 			  linkmodes);
288 		__set_bit(ETHTOOL_LINK_MODE_200000baseDR2_Full_BIT, linkmodes);
289 		__set_bit(ETHTOOL_LINK_MODE_200000baseCR2_Full_BIT, linkmodes);
290 	}
291 
292 	if (caps & MAC_400000FD) {
293 		__set_bit(ETHTOOL_LINK_MODE_400000baseKR8_Full_BIT, linkmodes);
294 		__set_bit(ETHTOOL_LINK_MODE_400000baseSR8_Full_BIT, linkmodes);
295 		__set_bit(ETHTOOL_LINK_MODE_400000baseLR8_ER8_FR8_Full_BIT,
296 			  linkmodes);
297 		__set_bit(ETHTOOL_LINK_MODE_400000baseDR8_Full_BIT, linkmodes);
298 		__set_bit(ETHTOOL_LINK_MODE_400000baseCR8_Full_BIT, linkmodes);
299 		__set_bit(ETHTOOL_LINK_MODE_400000baseKR4_Full_BIT, linkmodes);
300 		__set_bit(ETHTOOL_LINK_MODE_400000baseSR4_Full_BIT, linkmodes);
301 		__set_bit(ETHTOOL_LINK_MODE_400000baseLR4_ER4_FR4_Full_BIT,
302 			  linkmodes);
303 		__set_bit(ETHTOOL_LINK_MODE_400000baseDR4_Full_BIT, linkmodes);
304 		__set_bit(ETHTOOL_LINK_MODE_400000baseCR4_Full_BIT, linkmodes);
305 	}
306 }
307 
308 /**
309  * phylink_get_linkmodes() - get acceptable link modes
310  * @linkmodes: ethtool linkmode mask (must be already initialised)
311  * @interface: phy interface mode defined by &typedef phy_interface_t
312  * @mac_capabilities: bitmask of MAC capabilities
313  *
314  * Set all possible pause, speed and duplex linkmodes in @linkmodes that
315  * are supported by the @interface mode and @mac_capabilities. @linkmodes
316  * must have been initialised previously.
317  */
318 void phylink_get_linkmodes(unsigned long *linkmodes, phy_interface_t interface,
319 			   unsigned long mac_capabilities)
320 {
321 	unsigned long caps = MAC_SYM_PAUSE | MAC_ASYM_PAUSE;
322 
323 	switch (interface) {
324 	case PHY_INTERFACE_MODE_USXGMII:
325 		caps |= MAC_10000FD | MAC_5000FD | MAC_2500FD;
326 		fallthrough;
327 
328 	case PHY_INTERFACE_MODE_RGMII_TXID:
329 	case PHY_INTERFACE_MODE_RGMII_RXID:
330 	case PHY_INTERFACE_MODE_RGMII_ID:
331 	case PHY_INTERFACE_MODE_RGMII:
332 	case PHY_INTERFACE_MODE_QSGMII:
333 	case PHY_INTERFACE_MODE_SGMII:
334 	case PHY_INTERFACE_MODE_GMII:
335 		caps |= MAC_1000HD | MAC_1000FD;
336 		fallthrough;
337 
338 	case PHY_INTERFACE_MODE_REVRMII:
339 	case PHY_INTERFACE_MODE_RMII:
340 	case PHY_INTERFACE_MODE_SMII:
341 	case PHY_INTERFACE_MODE_REVMII:
342 	case PHY_INTERFACE_MODE_MII:
343 		caps |= MAC_10HD | MAC_10FD;
344 		fallthrough;
345 
346 	case PHY_INTERFACE_MODE_100BASEX:
347 		caps |= MAC_100HD | MAC_100FD;
348 		break;
349 
350 	case PHY_INTERFACE_MODE_TBI:
351 	case PHY_INTERFACE_MODE_MOCA:
352 	case PHY_INTERFACE_MODE_RTBI:
353 	case PHY_INTERFACE_MODE_1000BASEX:
354 		caps |= MAC_1000HD;
355 		fallthrough;
356 	case PHY_INTERFACE_MODE_TRGMII:
357 		caps |= MAC_1000FD;
358 		break;
359 
360 	case PHY_INTERFACE_MODE_2500BASEX:
361 		caps |= MAC_2500FD;
362 		break;
363 
364 	case PHY_INTERFACE_MODE_5GBASER:
365 		caps |= MAC_5000FD;
366 		break;
367 
368 	case PHY_INTERFACE_MODE_XGMII:
369 	case PHY_INTERFACE_MODE_RXAUI:
370 	case PHY_INTERFACE_MODE_XAUI:
371 	case PHY_INTERFACE_MODE_10GBASER:
372 	case PHY_INTERFACE_MODE_10GKR:
373 		caps |= MAC_10000FD;
374 		break;
375 
376 	case PHY_INTERFACE_MODE_25GBASER:
377 		caps |= MAC_25000FD;
378 		break;
379 
380 	case PHY_INTERFACE_MODE_XLGMII:
381 		caps |= MAC_40000FD;
382 		break;
383 
384 	case PHY_INTERFACE_MODE_INTERNAL:
385 		caps |= ~0;
386 		break;
387 
388 	case PHY_INTERFACE_MODE_NA:
389 	case PHY_INTERFACE_MODE_MAX:
390 		break;
391 	}
392 
393 	phylink_caps_to_linkmodes(linkmodes, caps & mac_capabilities);
394 }
395 EXPORT_SYMBOL_GPL(phylink_get_linkmodes);
396 
397 /**
398  * phylink_generic_validate() - generic validate() callback implementation
399  * @config: a pointer to a &struct phylink_config.
400  * @supported: ethtool bitmask for supported link modes.
401  * @state: a pointer to a &struct phylink_link_state.
402  *
403  * Generic implementation of the validate() callback that MAC drivers can
404  * use when they pass the range of supported interfaces and MAC capabilities.
405  * This makes use of phylink_get_linkmodes().
406  */
407 void phylink_generic_validate(struct phylink_config *config,
408 			      unsigned long *supported,
409 			      struct phylink_link_state *state)
410 {
411 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
412 
413 	phylink_set_port_modes(mask);
414 	phylink_set(mask, Autoneg);
415 	phylink_get_linkmodes(mask, state->interface, config->mac_capabilities);
416 
417 	linkmode_and(supported, supported, mask);
418 	linkmode_and(state->advertising, state->advertising, mask);
419 }
420 EXPORT_SYMBOL_GPL(phylink_generic_validate);
421 
422 static int phylink_validate_mac_and_pcs(struct phylink *pl,
423 					unsigned long *supported,
424 					struct phylink_link_state *state)
425 {
426 	struct phylink_pcs *pcs;
427 	int ret;
428 
429 	/* Get the PCS for this interface mode */
430 	if (pl->mac_ops->mac_select_pcs) {
431 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
432 		if (IS_ERR(pcs))
433 			return PTR_ERR(pcs);
434 	} else {
435 		pcs = pl->pcs;
436 	}
437 
438 	if (pcs) {
439 		/* The PCS, if present, must be setup before phylink_create()
440 		 * has been called. If the ops is not initialised, print an
441 		 * error and backtrace rather than oopsing the kernel.
442 		 */
443 		if (!pcs->ops) {
444 			phylink_err(pl, "interface %s: uninitialised PCS\n",
445 				    phy_modes(state->interface));
446 			dump_stack();
447 			return -EINVAL;
448 		}
449 
450 		/* Validate the link parameters with the PCS */
451 		if (pcs->ops->pcs_validate) {
452 			ret = pcs->ops->pcs_validate(pcs, supported, state);
453 			if (ret < 0 || phylink_is_empty_linkmode(supported))
454 				return -EINVAL;
455 
456 			/* Ensure the advertising mask is a subset of the
457 			 * supported mask.
458 			 */
459 			linkmode_and(state->advertising, state->advertising,
460 				     supported);
461 		}
462 	}
463 
464 	/* Then validate the link parameters with the MAC */
465 	pl->mac_ops->validate(pl->config, supported, state);
466 
467 	return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
468 }
469 
470 static int phylink_validate_any(struct phylink *pl, unsigned long *supported,
471 				struct phylink_link_state *state)
472 {
473 	__ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
474 	__ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
475 	__ETHTOOL_DECLARE_LINK_MODE_MASK(s);
476 	struct phylink_link_state t;
477 	int intf;
478 
479 	for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) {
480 		if (test_bit(intf, pl->config->supported_interfaces)) {
481 			linkmode_copy(s, supported);
482 
483 			t = *state;
484 			t.interface = intf;
485 			if (!phylink_validate_mac_and_pcs(pl, s, &t)) {
486 				linkmode_or(all_s, all_s, s);
487 				linkmode_or(all_adv, all_adv, t.advertising);
488 			}
489 		}
490 	}
491 
492 	linkmode_copy(supported, all_s);
493 	linkmode_copy(state->advertising, all_adv);
494 
495 	return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
496 }
497 
498 static int phylink_validate(struct phylink *pl, unsigned long *supported,
499 			    struct phylink_link_state *state)
500 {
501 	if (!phy_interface_empty(pl->config->supported_interfaces)) {
502 		if (state->interface == PHY_INTERFACE_MODE_NA)
503 			return phylink_validate_any(pl, supported, state);
504 
505 		if (!test_bit(state->interface,
506 			      pl->config->supported_interfaces))
507 			return -EINVAL;
508 	}
509 
510 	return phylink_validate_mac_and_pcs(pl, supported, state);
511 }
512 
513 static int phylink_parse_fixedlink(struct phylink *pl,
514 				   struct fwnode_handle *fwnode)
515 {
516 	struct fwnode_handle *fixed_node;
517 	const struct phy_setting *s;
518 	struct gpio_desc *desc;
519 	u32 speed;
520 	int ret;
521 
522 	fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
523 	if (fixed_node) {
524 		ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
525 
526 		pl->link_config.speed = speed;
527 		pl->link_config.duplex = DUPLEX_HALF;
528 
529 		if (fwnode_property_read_bool(fixed_node, "full-duplex"))
530 			pl->link_config.duplex = DUPLEX_FULL;
531 
532 		/* We treat the "pause" and "asym-pause" terminology as
533 		 * defining the link partner's ability.
534 		 */
535 		if (fwnode_property_read_bool(fixed_node, "pause"))
536 			__set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
537 				  pl->link_config.lp_advertising);
538 		if (fwnode_property_read_bool(fixed_node, "asym-pause"))
539 			__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
540 				  pl->link_config.lp_advertising);
541 
542 		if (ret == 0) {
543 			desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
544 						      GPIOD_IN, "?");
545 
546 			if (!IS_ERR(desc))
547 				pl->link_gpio = desc;
548 			else if (desc == ERR_PTR(-EPROBE_DEFER))
549 				ret = -EPROBE_DEFER;
550 		}
551 		fwnode_handle_put(fixed_node);
552 
553 		if (ret)
554 			return ret;
555 	} else {
556 		u32 prop[5];
557 
558 		ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
559 						     NULL, 0);
560 		if (ret != ARRAY_SIZE(prop)) {
561 			phylink_err(pl, "broken fixed-link?\n");
562 			return -EINVAL;
563 		}
564 
565 		ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
566 						     prop, ARRAY_SIZE(prop));
567 		if (!ret) {
568 			pl->link_config.duplex = prop[1] ?
569 						DUPLEX_FULL : DUPLEX_HALF;
570 			pl->link_config.speed = prop[2];
571 			if (prop[3])
572 				__set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
573 					  pl->link_config.lp_advertising);
574 			if (prop[4])
575 				__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
576 					  pl->link_config.lp_advertising);
577 		}
578 	}
579 
580 	if (pl->link_config.speed > SPEED_1000 &&
581 	    pl->link_config.duplex != DUPLEX_FULL)
582 		phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
583 			     pl->link_config.speed);
584 
585 	bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
586 	linkmode_copy(pl->link_config.advertising, pl->supported);
587 	phylink_validate(pl, pl->supported, &pl->link_config);
588 
589 	s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
590 			       pl->supported, true);
591 	linkmode_zero(pl->supported);
592 	phylink_set(pl->supported, MII);
593 	phylink_set(pl->supported, Pause);
594 	phylink_set(pl->supported, Asym_Pause);
595 	phylink_set(pl->supported, Autoneg);
596 	if (s) {
597 		__set_bit(s->bit, pl->supported);
598 		__set_bit(s->bit, pl->link_config.lp_advertising);
599 	} else {
600 		phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
601 			     pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
602 			     pl->link_config.speed);
603 	}
604 
605 	linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
606 		     pl->supported);
607 
608 	pl->link_config.link = 1;
609 	pl->link_config.an_complete = 1;
610 
611 	return 0;
612 }
613 
614 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
615 {
616 	struct fwnode_handle *dn;
617 	const char *managed;
618 
619 	dn = fwnode_get_named_child_node(fwnode, "fixed-link");
620 	if (dn || fwnode_property_present(fwnode, "fixed-link"))
621 		pl->cfg_link_an_mode = MLO_AN_FIXED;
622 	fwnode_handle_put(dn);
623 
624 	if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
625 	     strcmp(managed, "in-band-status") == 0) ||
626 	    pl->config->ovr_an_inband) {
627 		if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
628 			phylink_err(pl,
629 				    "can't use both fixed-link and in-band-status\n");
630 			return -EINVAL;
631 		}
632 
633 		linkmode_zero(pl->supported);
634 		phylink_set(pl->supported, MII);
635 		phylink_set(pl->supported, Autoneg);
636 		phylink_set(pl->supported, Asym_Pause);
637 		phylink_set(pl->supported, Pause);
638 		pl->link_config.an_enabled = true;
639 		pl->cfg_link_an_mode = MLO_AN_INBAND;
640 
641 		switch (pl->link_config.interface) {
642 		case PHY_INTERFACE_MODE_SGMII:
643 		case PHY_INTERFACE_MODE_QSGMII:
644 			phylink_set(pl->supported, 10baseT_Half);
645 			phylink_set(pl->supported, 10baseT_Full);
646 			phylink_set(pl->supported, 100baseT_Half);
647 			phylink_set(pl->supported, 100baseT_Full);
648 			phylink_set(pl->supported, 1000baseT_Half);
649 			phylink_set(pl->supported, 1000baseT_Full);
650 			break;
651 
652 		case PHY_INTERFACE_MODE_1000BASEX:
653 			phylink_set(pl->supported, 1000baseX_Full);
654 			break;
655 
656 		case PHY_INTERFACE_MODE_2500BASEX:
657 			phylink_set(pl->supported, 2500baseX_Full);
658 			break;
659 
660 		case PHY_INTERFACE_MODE_5GBASER:
661 			phylink_set(pl->supported, 5000baseT_Full);
662 			break;
663 
664 		case PHY_INTERFACE_MODE_25GBASER:
665 			phylink_set(pl->supported, 25000baseCR_Full);
666 			phylink_set(pl->supported, 25000baseKR_Full);
667 			phylink_set(pl->supported, 25000baseSR_Full);
668 			fallthrough;
669 		case PHY_INTERFACE_MODE_USXGMII:
670 		case PHY_INTERFACE_MODE_10GKR:
671 		case PHY_INTERFACE_MODE_10GBASER:
672 			phylink_set(pl->supported, 10baseT_Half);
673 			phylink_set(pl->supported, 10baseT_Full);
674 			phylink_set(pl->supported, 100baseT_Half);
675 			phylink_set(pl->supported, 100baseT_Full);
676 			phylink_set(pl->supported, 1000baseT_Half);
677 			phylink_set(pl->supported, 1000baseT_Full);
678 			phylink_set(pl->supported, 1000baseX_Full);
679 			phylink_set(pl->supported, 1000baseKX_Full);
680 			phylink_set(pl->supported, 2500baseT_Full);
681 			phylink_set(pl->supported, 2500baseX_Full);
682 			phylink_set(pl->supported, 5000baseT_Full);
683 			phylink_set(pl->supported, 10000baseT_Full);
684 			phylink_set(pl->supported, 10000baseKR_Full);
685 			phylink_set(pl->supported, 10000baseKX4_Full);
686 			phylink_set(pl->supported, 10000baseCR_Full);
687 			phylink_set(pl->supported, 10000baseSR_Full);
688 			phylink_set(pl->supported, 10000baseLR_Full);
689 			phylink_set(pl->supported, 10000baseLRM_Full);
690 			phylink_set(pl->supported, 10000baseER_Full);
691 			break;
692 
693 		case PHY_INTERFACE_MODE_XLGMII:
694 			phylink_set(pl->supported, 25000baseCR_Full);
695 			phylink_set(pl->supported, 25000baseKR_Full);
696 			phylink_set(pl->supported, 25000baseSR_Full);
697 			phylink_set(pl->supported, 40000baseKR4_Full);
698 			phylink_set(pl->supported, 40000baseCR4_Full);
699 			phylink_set(pl->supported, 40000baseSR4_Full);
700 			phylink_set(pl->supported, 40000baseLR4_Full);
701 			phylink_set(pl->supported, 50000baseCR2_Full);
702 			phylink_set(pl->supported, 50000baseKR2_Full);
703 			phylink_set(pl->supported, 50000baseSR2_Full);
704 			phylink_set(pl->supported, 50000baseKR_Full);
705 			phylink_set(pl->supported, 50000baseSR_Full);
706 			phylink_set(pl->supported, 50000baseCR_Full);
707 			phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
708 			phylink_set(pl->supported, 50000baseDR_Full);
709 			phylink_set(pl->supported, 100000baseKR4_Full);
710 			phylink_set(pl->supported, 100000baseSR4_Full);
711 			phylink_set(pl->supported, 100000baseCR4_Full);
712 			phylink_set(pl->supported, 100000baseLR4_ER4_Full);
713 			phylink_set(pl->supported, 100000baseKR2_Full);
714 			phylink_set(pl->supported, 100000baseSR2_Full);
715 			phylink_set(pl->supported, 100000baseCR2_Full);
716 			phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
717 			phylink_set(pl->supported, 100000baseDR2_Full);
718 			break;
719 
720 		default:
721 			phylink_err(pl,
722 				    "incorrect link mode %s for in-band status\n",
723 				    phy_modes(pl->link_config.interface));
724 			return -EINVAL;
725 		}
726 
727 		linkmode_copy(pl->link_config.advertising, pl->supported);
728 
729 		if (phylink_validate(pl, pl->supported, &pl->link_config)) {
730 			phylink_err(pl,
731 				    "failed to validate link configuration for in-band status\n");
732 			return -EINVAL;
733 		}
734 
735 		/* Check if MAC/PCS also supports Autoneg. */
736 		pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
737 	}
738 
739 	return 0;
740 }
741 
742 static void phylink_apply_manual_flow(struct phylink *pl,
743 				      struct phylink_link_state *state)
744 {
745 	/* If autoneg is disabled, pause AN is also disabled */
746 	if (!state->an_enabled)
747 		state->pause &= ~MLO_PAUSE_AN;
748 
749 	/* Manual configuration of pause modes */
750 	if (!(pl->link_config.pause & MLO_PAUSE_AN))
751 		state->pause = pl->link_config.pause;
752 }
753 
754 static void phylink_resolve_flow(struct phylink_link_state *state)
755 {
756 	bool tx_pause, rx_pause;
757 
758 	state->pause = MLO_PAUSE_NONE;
759 	if (state->duplex == DUPLEX_FULL) {
760 		linkmode_resolve_pause(state->advertising,
761 				       state->lp_advertising,
762 				       &tx_pause, &rx_pause);
763 		if (tx_pause)
764 			state->pause |= MLO_PAUSE_TX;
765 		if (rx_pause)
766 			state->pause |= MLO_PAUSE_RX;
767 	}
768 }
769 
770 static void phylink_mac_config(struct phylink *pl,
771 			       const struct phylink_link_state *state)
772 {
773 	phylink_dbg(pl,
774 		    "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
775 		    __func__, phylink_an_mode_str(pl->cur_link_an_mode),
776 		    phy_modes(state->interface),
777 		    phy_speed_to_str(state->speed),
778 		    phy_duplex_to_str(state->duplex),
779 		    __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
780 		    state->pause, state->link, state->an_enabled);
781 
782 	pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
783 }
784 
785 static void phylink_mac_pcs_an_restart(struct phylink *pl)
786 {
787 	if (pl->link_config.an_enabled &&
788 	    phy_interface_mode_is_8023z(pl->link_config.interface) &&
789 	    phylink_autoneg_inband(pl->cur_link_an_mode)) {
790 		if (pl->pcs_ops)
791 			pl->pcs_ops->pcs_an_restart(pl->pcs);
792 		else if (pl->config->legacy_pre_march2020)
793 			pl->mac_ops->mac_an_restart(pl->config);
794 	}
795 }
796 
797 static void phylink_major_config(struct phylink *pl, bool restart,
798 				  const struct phylink_link_state *state)
799 {
800 	struct phylink_pcs *pcs = NULL;
801 	int err;
802 
803 	phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
804 
805 	if (pl->mac_ops->mac_select_pcs) {
806 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
807 		if (IS_ERR(pcs)) {
808 			phylink_err(pl,
809 				    "mac_select_pcs unexpectedly failed: %pe\n",
810 				    pcs);
811 			return;
812 		}
813 	}
814 
815 	if (pl->mac_ops->mac_prepare) {
816 		err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
817 					       state->interface);
818 		if (err < 0) {
819 			phylink_err(pl, "mac_prepare failed: %pe\n",
820 				    ERR_PTR(err));
821 			return;
822 		}
823 	}
824 
825 	/* If we have a new PCS, switch to the new PCS after preparing the MAC
826 	 * for the change.
827 	 */
828 	if (pcs)
829 		phylink_set_pcs(pl, pcs);
830 
831 	phylink_mac_config(pl, state);
832 
833 	if (pl->pcs_ops) {
834 		err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
835 					      state->interface,
836 					      state->advertising,
837 					      !!(pl->link_config.pause &
838 						 MLO_PAUSE_AN));
839 		if (err < 0)
840 			phylink_err(pl, "pcs_config failed: %pe\n",
841 				    ERR_PTR(err));
842 		if (err > 0)
843 			restart = true;
844 	}
845 	if (restart)
846 		phylink_mac_pcs_an_restart(pl);
847 
848 	if (pl->mac_ops->mac_finish) {
849 		err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
850 					      state->interface);
851 		if (err < 0)
852 			phylink_err(pl, "mac_finish failed: %pe\n",
853 				    ERR_PTR(err));
854 	}
855 }
856 
857 /*
858  * Reconfigure for a change of inband advertisement.
859  * If we have a separate PCS, we only need to call its pcs_config() method,
860  * and then restart AN if it indicates something changed. Otherwise, we do
861  * the full MAC reconfiguration.
862  */
863 static int phylink_change_inband_advert(struct phylink *pl)
864 {
865 	int ret;
866 
867 	if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
868 		return 0;
869 
870 	if (!pl->pcs_ops && pl->config->legacy_pre_march2020) {
871 		/* Legacy method */
872 		phylink_mac_config(pl, &pl->link_config);
873 		phylink_mac_pcs_an_restart(pl);
874 		return 0;
875 	}
876 
877 	phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
878 		    phylink_an_mode_str(pl->cur_link_an_mode),
879 		    phy_modes(pl->link_config.interface),
880 		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
881 		    pl->link_config.pause);
882 
883 	/* Modern PCS-based method; update the advert at the PCS, and
884 	 * restart negotiation if the pcs_config() helper indicates that
885 	 * the programmed advertisement has changed.
886 	 */
887 	ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
888 				      pl->link_config.interface,
889 				      pl->link_config.advertising,
890 				      !!(pl->link_config.pause & MLO_PAUSE_AN));
891 	if (ret < 0)
892 		return ret;
893 
894 	if (ret > 0)
895 		phylink_mac_pcs_an_restart(pl);
896 
897 	return 0;
898 }
899 
900 static void phylink_mac_pcs_get_state(struct phylink *pl,
901 				      struct phylink_link_state *state)
902 {
903 	linkmode_copy(state->advertising, pl->link_config.advertising);
904 	linkmode_zero(state->lp_advertising);
905 	state->interface = pl->link_config.interface;
906 	state->an_enabled = pl->link_config.an_enabled;
907 	if  (state->an_enabled) {
908 		state->speed = SPEED_UNKNOWN;
909 		state->duplex = DUPLEX_UNKNOWN;
910 		state->pause = MLO_PAUSE_NONE;
911 	} else {
912 		state->speed =  pl->link_config.speed;
913 		state->duplex = pl->link_config.duplex;
914 		state->pause = pl->link_config.pause;
915 	}
916 	state->an_complete = 0;
917 	state->link = 1;
918 
919 	if (pl->pcs_ops)
920 		pl->pcs_ops->pcs_get_state(pl->pcs, state);
921 	else if (pl->mac_ops->mac_pcs_get_state &&
922 		 pl->config->legacy_pre_march2020)
923 		pl->mac_ops->mac_pcs_get_state(pl->config, state);
924 	else
925 		state->link = 0;
926 }
927 
928 /* The fixed state is... fixed except for the link state,
929  * which may be determined by a GPIO or a callback.
930  */
931 static void phylink_get_fixed_state(struct phylink *pl,
932 				    struct phylink_link_state *state)
933 {
934 	*state = pl->link_config;
935 	if (pl->config->get_fixed_state)
936 		pl->config->get_fixed_state(pl->config, state);
937 	else if (pl->link_gpio)
938 		state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
939 
940 	phylink_resolve_flow(state);
941 }
942 
943 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
944 {
945 	struct phylink_link_state link_state;
946 
947 	switch (pl->cur_link_an_mode) {
948 	case MLO_AN_PHY:
949 		link_state = pl->phy_state;
950 		break;
951 
952 	case MLO_AN_FIXED:
953 		phylink_get_fixed_state(pl, &link_state);
954 		break;
955 
956 	case MLO_AN_INBAND:
957 		link_state = pl->link_config;
958 		if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
959 			link_state.pause = MLO_PAUSE_NONE;
960 		break;
961 
962 	default: /* can't happen */
963 		return;
964 	}
965 
966 	link_state.link = false;
967 
968 	phylink_apply_manual_flow(pl, &link_state);
969 	phylink_major_config(pl, force_restart, &link_state);
970 }
971 
972 static const char *phylink_pause_to_str(int pause)
973 {
974 	switch (pause & MLO_PAUSE_TXRX_MASK) {
975 	case MLO_PAUSE_TX | MLO_PAUSE_RX:
976 		return "rx/tx";
977 	case MLO_PAUSE_TX:
978 		return "tx";
979 	case MLO_PAUSE_RX:
980 		return "rx";
981 	default:
982 		return "off";
983 	}
984 }
985 
986 static void phylink_link_up(struct phylink *pl,
987 			    struct phylink_link_state link_state)
988 {
989 	struct net_device *ndev = pl->netdev;
990 
991 	pl->cur_interface = link_state.interface;
992 
993 	if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
994 		pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
995 					 pl->cur_interface,
996 					 link_state.speed, link_state.duplex);
997 
998 	pl->mac_ops->mac_link_up(pl->config, pl->phydev,
999 				 pl->cur_link_an_mode, pl->cur_interface,
1000 				 link_state.speed, link_state.duplex,
1001 				 !!(link_state.pause & MLO_PAUSE_TX),
1002 				 !!(link_state.pause & MLO_PAUSE_RX));
1003 
1004 	if (ndev)
1005 		netif_carrier_on(ndev);
1006 
1007 	phylink_info(pl,
1008 		     "Link is Up - %s/%s - flow control %s\n",
1009 		     phy_speed_to_str(link_state.speed),
1010 		     phy_duplex_to_str(link_state.duplex),
1011 		     phylink_pause_to_str(link_state.pause));
1012 }
1013 
1014 static void phylink_link_down(struct phylink *pl)
1015 {
1016 	struct net_device *ndev = pl->netdev;
1017 
1018 	if (ndev)
1019 		netif_carrier_off(ndev);
1020 	pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
1021 				   pl->cur_interface);
1022 	phylink_info(pl, "Link is Down\n");
1023 }
1024 
1025 static void phylink_resolve(struct work_struct *w)
1026 {
1027 	struct phylink *pl = container_of(w, struct phylink, resolve);
1028 	struct phylink_link_state link_state;
1029 	struct net_device *ndev = pl->netdev;
1030 	bool mac_config = false;
1031 	bool retrigger = false;
1032 	bool cur_link_state;
1033 
1034 	mutex_lock(&pl->state_mutex);
1035 	if (pl->netdev)
1036 		cur_link_state = netif_carrier_ok(ndev);
1037 	else
1038 		cur_link_state = pl->old_link_state;
1039 
1040 	if (pl->phylink_disable_state) {
1041 		pl->mac_link_dropped = false;
1042 		link_state.link = false;
1043 	} else if (pl->mac_link_dropped) {
1044 		link_state.link = false;
1045 		retrigger = true;
1046 	} else {
1047 		switch (pl->cur_link_an_mode) {
1048 		case MLO_AN_PHY:
1049 			link_state = pl->phy_state;
1050 			phylink_apply_manual_flow(pl, &link_state);
1051 			mac_config = link_state.link;
1052 			break;
1053 
1054 		case MLO_AN_FIXED:
1055 			phylink_get_fixed_state(pl, &link_state);
1056 			mac_config = link_state.link;
1057 			break;
1058 
1059 		case MLO_AN_INBAND:
1060 			phylink_mac_pcs_get_state(pl, &link_state);
1061 
1062 			/* The PCS may have a latching link-fail indicator.
1063 			 * If the link was up, bring the link down and
1064 			 * re-trigger the resolve. Otherwise, re-read the
1065 			 * PCS state to get the current status of the link.
1066 			 */
1067 			if (!link_state.link) {
1068 				if (cur_link_state)
1069 					retrigger = true;
1070 				else
1071 					phylink_mac_pcs_get_state(pl,
1072 								  &link_state);
1073 			}
1074 
1075 			/* If we have a phy, the "up" state is the union of
1076 			 * both the PHY and the MAC
1077 			 */
1078 			if (pl->phydev)
1079 				link_state.link &= pl->phy_state.link;
1080 
1081 			/* Only update if the PHY link is up */
1082 			if (pl->phydev && pl->phy_state.link) {
1083 				/* If the interface has changed, force a
1084 				 * link down event if the link isn't already
1085 				 * down, and re-resolve.
1086 				 */
1087 				if (link_state.interface !=
1088 				    pl->phy_state.interface) {
1089 					retrigger = true;
1090 					link_state.link = false;
1091 				}
1092 				link_state.interface = pl->phy_state.interface;
1093 
1094 				/* If we have a PHY, we need to update with
1095 				 * the PHY flow control bits.
1096 				 */
1097 				link_state.pause = pl->phy_state.pause;
1098 				mac_config = true;
1099 			}
1100 			phylink_apply_manual_flow(pl, &link_state);
1101 			break;
1102 		}
1103 	}
1104 
1105 	if (mac_config) {
1106 		if (link_state.interface != pl->link_config.interface) {
1107 			/* The interface has changed, force the link down and
1108 			 * then reconfigure.
1109 			 */
1110 			if (cur_link_state) {
1111 				phylink_link_down(pl);
1112 				cur_link_state = false;
1113 			}
1114 			phylink_major_config(pl, false, &link_state);
1115 			pl->link_config.interface = link_state.interface;
1116 		} else if (!pl->pcs_ops && pl->config->legacy_pre_march2020) {
1117 			/* The interface remains unchanged, only the speed,
1118 			 * duplex or pause settings have changed. Call the
1119 			 * old mac_config() method to configure the MAC/PCS
1120 			 * only if we do not have a legacy MAC driver.
1121 			 */
1122 			phylink_mac_config(pl, &link_state);
1123 		}
1124 	}
1125 
1126 	if (link_state.link != cur_link_state) {
1127 		pl->old_link_state = link_state.link;
1128 		if (!link_state.link)
1129 			phylink_link_down(pl);
1130 		else
1131 			phylink_link_up(pl, link_state);
1132 	}
1133 	if (!link_state.link && retrigger) {
1134 		pl->mac_link_dropped = false;
1135 		queue_work(system_power_efficient_wq, &pl->resolve);
1136 	}
1137 	mutex_unlock(&pl->state_mutex);
1138 }
1139 
1140 static void phylink_run_resolve(struct phylink *pl)
1141 {
1142 	if (!pl->phylink_disable_state)
1143 		queue_work(system_power_efficient_wq, &pl->resolve);
1144 }
1145 
1146 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
1147 {
1148 	unsigned long state = pl->phylink_disable_state;
1149 
1150 	set_bit(bit, &pl->phylink_disable_state);
1151 	if (state == 0) {
1152 		queue_work(system_power_efficient_wq, &pl->resolve);
1153 		flush_work(&pl->resolve);
1154 	}
1155 }
1156 
1157 static void phylink_enable_and_run_resolve(struct phylink *pl, int bit)
1158 {
1159 	clear_bit(bit, &pl->phylink_disable_state);
1160 	phylink_run_resolve(pl);
1161 }
1162 
1163 static void phylink_fixed_poll(struct timer_list *t)
1164 {
1165 	struct phylink *pl = container_of(t, struct phylink, link_poll);
1166 
1167 	mod_timer(t, jiffies + HZ);
1168 
1169 	phylink_run_resolve(pl);
1170 }
1171 
1172 static const struct sfp_upstream_ops sfp_phylink_ops;
1173 
1174 static int phylink_register_sfp(struct phylink *pl,
1175 				struct fwnode_handle *fwnode)
1176 {
1177 	struct sfp_bus *bus;
1178 	int ret;
1179 
1180 	if (!fwnode)
1181 		return 0;
1182 
1183 	bus = sfp_bus_find_fwnode(fwnode);
1184 	if (IS_ERR(bus)) {
1185 		ret = PTR_ERR(bus);
1186 		phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
1187 		return ret;
1188 	}
1189 
1190 	pl->sfp_bus = bus;
1191 
1192 	ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
1193 	sfp_bus_put(bus);
1194 
1195 	return ret;
1196 }
1197 
1198 /**
1199  * phylink_create() - create a phylink instance
1200  * @config: a pointer to the target &struct phylink_config
1201  * @fwnode: a pointer to a &struct fwnode_handle describing the network
1202  *	interface
1203  * @iface: the desired link mode defined by &typedef phy_interface_t
1204  * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
1205  *
1206  * Create a new phylink instance, and parse the link parameters found in @np.
1207  * This will parse in-band modes, fixed-link or SFP configuration.
1208  *
1209  * Note: the rtnl lock must not be held when calling this function.
1210  *
1211  * Returns a pointer to a &struct phylink, or an error-pointer value. Users
1212  * must use IS_ERR() to check for errors from this function.
1213  */
1214 struct phylink *phylink_create(struct phylink_config *config,
1215 			       struct fwnode_handle *fwnode,
1216 			       phy_interface_t iface,
1217 			       const struct phylink_mac_ops *mac_ops)
1218 {
1219 	struct phylink *pl;
1220 	int ret;
1221 
1222 	/* Validate the supplied configuration */
1223 	if (mac_ops->mac_select_pcs &&
1224 	    phy_interface_empty(config->supported_interfaces)) {
1225 		dev_err(config->dev,
1226 			"phylink: error: empty supported_interfaces but mac_select_pcs() method present\n");
1227 		return ERR_PTR(-EINVAL);
1228 	}
1229 
1230 	pl = kzalloc(sizeof(*pl), GFP_KERNEL);
1231 	if (!pl)
1232 		return ERR_PTR(-ENOMEM);
1233 
1234 	mutex_init(&pl->state_mutex);
1235 	INIT_WORK(&pl->resolve, phylink_resolve);
1236 
1237 	pl->config = config;
1238 	if (config->type == PHYLINK_NETDEV) {
1239 		pl->netdev = to_net_dev(config->dev);
1240 	} else if (config->type == PHYLINK_DEV) {
1241 		pl->dev = config->dev;
1242 	} else {
1243 		kfree(pl);
1244 		return ERR_PTR(-EINVAL);
1245 	}
1246 
1247 	pl->phy_state.interface = iface;
1248 	pl->link_interface = iface;
1249 	if (iface == PHY_INTERFACE_MODE_MOCA)
1250 		pl->link_port = PORT_BNC;
1251 	else
1252 		pl->link_port = PORT_MII;
1253 	pl->link_config.interface = iface;
1254 	pl->link_config.pause = MLO_PAUSE_AN;
1255 	pl->link_config.speed = SPEED_UNKNOWN;
1256 	pl->link_config.duplex = DUPLEX_UNKNOWN;
1257 	pl->link_config.an_enabled = true;
1258 	pl->mac_ops = mac_ops;
1259 	__set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1260 	timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
1261 
1262 	bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1263 	linkmode_copy(pl->link_config.advertising, pl->supported);
1264 	phylink_validate(pl, pl->supported, &pl->link_config);
1265 
1266 	ret = phylink_parse_mode(pl, fwnode);
1267 	if (ret < 0) {
1268 		kfree(pl);
1269 		return ERR_PTR(ret);
1270 	}
1271 
1272 	if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
1273 		ret = phylink_parse_fixedlink(pl, fwnode);
1274 		if (ret < 0) {
1275 			kfree(pl);
1276 			return ERR_PTR(ret);
1277 		}
1278 	}
1279 
1280 	pl->cur_link_an_mode = pl->cfg_link_an_mode;
1281 
1282 	ret = phylink_register_sfp(pl, fwnode);
1283 	if (ret < 0) {
1284 		kfree(pl);
1285 		return ERR_PTR(ret);
1286 	}
1287 
1288 	return pl;
1289 }
1290 EXPORT_SYMBOL_GPL(phylink_create);
1291 
1292 /**
1293  * phylink_set_pcs() - set the current PCS for phylink to use
1294  * @pl: a pointer to a &struct phylink returned from phylink_create()
1295  * @pcs: a pointer to the &struct phylink_pcs
1296  *
1297  * Bind the MAC PCS to phylink.  This may be called after phylink_create().
1298  * If it is desired to dynamically change the PCS, then the preferred method
1299  * is to use mac_select_pcs(), but it may also be called in mac_prepare()
1300  * or mac_config().
1301  *
1302  * Please note that there are behavioural changes with the mac_config()
1303  * callback if a PCS is present (denoting a newer setup) so removing a PCS
1304  * is not supported, and if a PCS is going to be used, it must be registered
1305  * by calling phylink_set_pcs() at the latest in the first mac_config() call.
1306  */
1307 void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs)
1308 {
1309 	pl->pcs = pcs;
1310 	pl->pcs_ops = pcs->ops;
1311 
1312 	if (!pl->phylink_disable_state &&
1313 	    pl->cfg_link_an_mode == MLO_AN_INBAND) {
1314 		if (pl->config->pcs_poll || pcs->poll)
1315 			mod_timer(&pl->link_poll, jiffies + HZ);
1316 		else
1317 			del_timer(&pl->link_poll);
1318 	}
1319 }
1320 EXPORT_SYMBOL_GPL(phylink_set_pcs);
1321 
1322 /**
1323  * phylink_destroy() - cleanup and destroy the phylink instance
1324  * @pl: a pointer to a &struct phylink returned from phylink_create()
1325  *
1326  * Destroy a phylink instance. Any PHY that has been attached must have been
1327  * cleaned up via phylink_disconnect_phy() prior to calling this function.
1328  *
1329  * Note: the rtnl lock must not be held when calling this function.
1330  */
1331 void phylink_destroy(struct phylink *pl)
1332 {
1333 	sfp_bus_del_upstream(pl->sfp_bus);
1334 	if (pl->link_gpio)
1335 		gpiod_put(pl->link_gpio);
1336 
1337 	cancel_work_sync(&pl->resolve);
1338 	kfree(pl);
1339 }
1340 EXPORT_SYMBOL_GPL(phylink_destroy);
1341 
1342 static void phylink_phy_change(struct phy_device *phydev, bool up)
1343 {
1344 	struct phylink *pl = phydev->phylink;
1345 	bool tx_pause, rx_pause;
1346 
1347 	phy_get_pause(phydev, &tx_pause, &rx_pause);
1348 
1349 	mutex_lock(&pl->state_mutex);
1350 	pl->phy_state.speed = phydev->speed;
1351 	pl->phy_state.duplex = phydev->duplex;
1352 	pl->phy_state.pause = MLO_PAUSE_NONE;
1353 	if (tx_pause)
1354 		pl->phy_state.pause |= MLO_PAUSE_TX;
1355 	if (rx_pause)
1356 		pl->phy_state.pause |= MLO_PAUSE_RX;
1357 	pl->phy_state.interface = phydev->interface;
1358 	pl->phy_state.link = up;
1359 	mutex_unlock(&pl->state_mutex);
1360 
1361 	phylink_run_resolve(pl);
1362 
1363 	phylink_dbg(pl, "phy link %s %s/%s/%s/%s\n", up ? "up" : "down",
1364 		    phy_modes(phydev->interface),
1365 		    phy_speed_to_str(phydev->speed),
1366 		    phy_duplex_to_str(phydev->duplex),
1367 		    phylink_pause_to_str(pl->phy_state.pause));
1368 }
1369 
1370 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
1371 			       phy_interface_t interface)
1372 {
1373 	struct phylink_link_state config;
1374 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
1375 	char *irq_str;
1376 	int ret;
1377 
1378 	/*
1379 	 * This is the new way of dealing with flow control for PHYs,
1380 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
1381 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
1382 	 * using our validate call to the MAC, we rely upon the MAC
1383 	 * clearing the bits from both supported and advertising fields.
1384 	 */
1385 	phy_support_asym_pause(phy);
1386 
1387 	memset(&config, 0, sizeof(config));
1388 	linkmode_copy(supported, phy->supported);
1389 	linkmode_copy(config.advertising, phy->advertising);
1390 
1391 	/* Clause 45 PHYs switch their Serdes lane between several different
1392 	 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
1393 	 * speeds. We really need to know which interface modes the PHY and
1394 	 * MAC supports to properly work out which linkmodes can be supported.
1395 	 */
1396 	if (phy->is_c45 &&
1397 	    interface != PHY_INTERFACE_MODE_RXAUI &&
1398 	    interface != PHY_INTERFACE_MODE_XAUI &&
1399 	    interface != PHY_INTERFACE_MODE_USXGMII)
1400 		config.interface = PHY_INTERFACE_MODE_NA;
1401 	else
1402 		config.interface = interface;
1403 
1404 	ret = phylink_validate(pl, supported, &config);
1405 	if (ret) {
1406 		phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
1407 			     phy_modes(config.interface),
1408 			     __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1409 			     __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1410 			     ret);
1411 		return ret;
1412 	}
1413 
1414 	phy->phylink = pl;
1415 	phy->phy_link_change = phylink_phy_change;
1416 
1417 	irq_str = phy_attached_info_irq(phy);
1418 	phylink_info(pl,
1419 		     "PHY [%s] driver [%s] (irq=%s)\n",
1420 		     dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1421 	kfree(irq_str);
1422 
1423 	mutex_lock(&phy->lock);
1424 	mutex_lock(&pl->state_mutex);
1425 	pl->phydev = phy;
1426 	pl->phy_state.interface = interface;
1427 	pl->phy_state.pause = MLO_PAUSE_NONE;
1428 	pl->phy_state.speed = SPEED_UNKNOWN;
1429 	pl->phy_state.duplex = DUPLEX_UNKNOWN;
1430 	linkmode_copy(pl->supported, supported);
1431 	linkmode_copy(pl->link_config.advertising, config.advertising);
1432 
1433 	/* Restrict the phy advertisement according to the MAC support. */
1434 	linkmode_copy(phy->advertising, config.advertising);
1435 	mutex_unlock(&pl->state_mutex);
1436 	mutex_unlock(&phy->lock);
1437 
1438 	phylink_dbg(pl,
1439 		    "phy: %s setting supported %*pb advertising %*pb\n",
1440 		    phy_modes(interface),
1441 		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1442 		    __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1443 
1444 	if (phy_interrupt_is_valid(phy))
1445 		phy_request_interrupt(phy);
1446 
1447 	return 0;
1448 }
1449 
1450 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1451 			      phy_interface_t interface)
1452 {
1453 	if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1454 		    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1455 		     phy_interface_mode_is_8023z(interface))))
1456 		return -EINVAL;
1457 
1458 	if (pl->phydev)
1459 		return -EBUSY;
1460 
1461 	return phy_attach_direct(pl->netdev, phy, 0, interface);
1462 }
1463 
1464 /**
1465  * phylink_connect_phy() - connect a PHY to the phylink instance
1466  * @pl: a pointer to a &struct phylink returned from phylink_create()
1467  * @phy: a pointer to a &struct phy_device.
1468  *
1469  * Connect @phy to the phylink instance specified by @pl by calling
1470  * phy_attach_direct(). Configure the @phy according to the MAC driver's
1471  * capabilities, start the PHYLIB state machine and enable any interrupts
1472  * that the PHY supports.
1473  *
1474  * This updates the phylink's ethtool supported and advertising link mode
1475  * masks.
1476  *
1477  * Returns 0 on success or a negative errno.
1478  */
1479 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1480 {
1481 	int ret;
1482 
1483 	/* Use PHY device/driver interface */
1484 	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1485 		pl->link_interface = phy->interface;
1486 		pl->link_config.interface = pl->link_interface;
1487 	}
1488 
1489 	ret = phylink_attach_phy(pl, phy, pl->link_interface);
1490 	if (ret < 0)
1491 		return ret;
1492 
1493 	ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1494 	if (ret)
1495 		phy_detach(phy);
1496 
1497 	return ret;
1498 }
1499 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1500 
1501 /**
1502  * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1503  * @pl: a pointer to a &struct phylink returned from phylink_create()
1504  * @dn: a pointer to a &struct device_node.
1505  * @flags: PHY-specific flags to communicate to the PHY device driver
1506  *
1507  * Connect the phy specified in the device node @dn to the phylink instance
1508  * specified by @pl. Actions specified in phylink_connect_phy() will be
1509  * performed.
1510  *
1511  * Returns 0 on success or a negative errno.
1512  */
1513 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1514 			   u32 flags)
1515 {
1516 	return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
1517 }
1518 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1519 
1520 /**
1521  * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
1522  * @pl: a pointer to a &struct phylink returned from phylink_create()
1523  * @fwnode: a pointer to a &struct fwnode_handle.
1524  * @flags: PHY-specific flags to communicate to the PHY device driver
1525  *
1526  * Connect the phy specified @fwnode to the phylink instance specified
1527  * by @pl.
1528  *
1529  * Returns 0 on success or a negative errno.
1530  */
1531 int phylink_fwnode_phy_connect(struct phylink *pl,
1532 			       struct fwnode_handle *fwnode,
1533 			       u32 flags)
1534 {
1535 	struct fwnode_handle *phy_fwnode;
1536 	struct phy_device *phy_dev;
1537 	int ret;
1538 
1539 	/* Fixed links and 802.3z are handled without needing a PHY */
1540 	if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1541 	    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1542 	     phy_interface_mode_is_8023z(pl->link_interface)))
1543 		return 0;
1544 
1545 	phy_fwnode = fwnode_get_phy_node(fwnode);
1546 	if (IS_ERR(phy_fwnode)) {
1547 		if (pl->cfg_link_an_mode == MLO_AN_PHY)
1548 			return -ENODEV;
1549 		return 0;
1550 	}
1551 
1552 	phy_dev = fwnode_phy_find_device(phy_fwnode);
1553 	/* We're done with the phy_node handle */
1554 	fwnode_handle_put(phy_fwnode);
1555 	if (!phy_dev)
1556 		return -ENODEV;
1557 
1558 	/* Use PHY device/driver interface */
1559 	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1560 		pl->link_interface = phy_dev->interface;
1561 		pl->link_config.interface = pl->link_interface;
1562 	}
1563 
1564 	ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1565 				pl->link_interface);
1566 	if (ret) {
1567 		phy_device_free(phy_dev);
1568 		return ret;
1569 	}
1570 
1571 	ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1572 	if (ret)
1573 		phy_detach(phy_dev);
1574 
1575 	return ret;
1576 }
1577 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
1578 
1579 /**
1580  * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1581  *   instance.
1582  * @pl: a pointer to a &struct phylink returned from phylink_create()
1583  *
1584  * Disconnect any current PHY from the phylink instance described by @pl.
1585  */
1586 void phylink_disconnect_phy(struct phylink *pl)
1587 {
1588 	struct phy_device *phy;
1589 
1590 	ASSERT_RTNL();
1591 
1592 	phy = pl->phydev;
1593 	if (phy) {
1594 		mutex_lock(&phy->lock);
1595 		mutex_lock(&pl->state_mutex);
1596 		pl->phydev = NULL;
1597 		mutex_unlock(&pl->state_mutex);
1598 		mutex_unlock(&phy->lock);
1599 		flush_work(&pl->resolve);
1600 
1601 		phy_disconnect(phy);
1602 	}
1603 }
1604 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1605 
1606 /**
1607  * phylink_mac_change() - notify phylink of a change in MAC state
1608  * @pl: a pointer to a &struct phylink returned from phylink_create()
1609  * @up: indicates whether the link is currently up.
1610  *
1611  * The MAC driver should call this driver when the state of its link
1612  * changes (eg, link failure, new negotiation results, etc.)
1613  */
1614 void phylink_mac_change(struct phylink *pl, bool up)
1615 {
1616 	if (!up)
1617 		pl->mac_link_dropped = true;
1618 	phylink_run_resolve(pl);
1619 	phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1620 }
1621 EXPORT_SYMBOL_GPL(phylink_mac_change);
1622 
1623 static irqreturn_t phylink_link_handler(int irq, void *data)
1624 {
1625 	struct phylink *pl = data;
1626 
1627 	phylink_run_resolve(pl);
1628 
1629 	return IRQ_HANDLED;
1630 }
1631 
1632 /**
1633  * phylink_start() - start a phylink instance
1634  * @pl: a pointer to a &struct phylink returned from phylink_create()
1635  *
1636  * Start the phylink instance specified by @pl, configuring the MAC for the
1637  * desired link mode(s) and negotiation style. This should be called from the
1638  * network device driver's &struct net_device_ops ndo_open() method.
1639  */
1640 void phylink_start(struct phylink *pl)
1641 {
1642 	bool poll = false;
1643 
1644 	ASSERT_RTNL();
1645 
1646 	phylink_info(pl, "configuring for %s/%s link mode\n",
1647 		     phylink_an_mode_str(pl->cur_link_an_mode),
1648 		     phy_modes(pl->link_config.interface));
1649 
1650 	/* Always set the carrier off */
1651 	if (pl->netdev)
1652 		netif_carrier_off(pl->netdev);
1653 
1654 	/* Apply the link configuration to the MAC when starting. This allows
1655 	 * a fixed-link to start with the correct parameters, and also
1656 	 * ensures that we set the appropriate advertisement for Serdes links.
1657 	 *
1658 	 * Restart autonegotiation if using 802.3z to ensure that the link
1659 	 * parameters are properly negotiated.  This is necessary for DSA
1660 	 * switches using 802.3z negotiation to ensure they see our modes.
1661 	 */
1662 	phylink_mac_initial_config(pl, true);
1663 
1664 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
1665 
1666 	if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1667 		int irq = gpiod_to_irq(pl->link_gpio);
1668 
1669 		if (irq > 0) {
1670 			if (!request_irq(irq, phylink_link_handler,
1671 					 IRQF_TRIGGER_RISING |
1672 					 IRQF_TRIGGER_FALLING,
1673 					 "netdev link", pl))
1674 				pl->link_irq = irq;
1675 			else
1676 				irq = 0;
1677 		}
1678 		if (irq <= 0)
1679 			poll = true;
1680 	}
1681 
1682 	switch (pl->cfg_link_an_mode) {
1683 	case MLO_AN_FIXED:
1684 		poll |= pl->config->poll_fixed_state;
1685 		break;
1686 	case MLO_AN_INBAND:
1687 		poll |= pl->config->pcs_poll;
1688 		if (pl->pcs)
1689 			poll |= pl->pcs->poll;
1690 		break;
1691 	}
1692 	if (poll)
1693 		mod_timer(&pl->link_poll, jiffies + HZ);
1694 	if (pl->phydev)
1695 		phy_start(pl->phydev);
1696 	if (pl->sfp_bus)
1697 		sfp_upstream_start(pl->sfp_bus);
1698 }
1699 EXPORT_SYMBOL_GPL(phylink_start);
1700 
1701 /**
1702  * phylink_stop() - stop a phylink instance
1703  * @pl: a pointer to a &struct phylink returned from phylink_create()
1704  *
1705  * Stop the phylink instance specified by @pl. This should be called from the
1706  * network device driver's &struct net_device_ops ndo_stop() method.  The
1707  * network device's carrier state should not be changed prior to calling this
1708  * function.
1709  *
1710  * This will synchronously bring down the link if the link is not already
1711  * down (in other words, it will trigger a mac_link_down() method call.)
1712  */
1713 void phylink_stop(struct phylink *pl)
1714 {
1715 	ASSERT_RTNL();
1716 
1717 	if (pl->sfp_bus)
1718 		sfp_upstream_stop(pl->sfp_bus);
1719 	if (pl->phydev)
1720 		phy_stop(pl->phydev);
1721 	del_timer_sync(&pl->link_poll);
1722 	if (pl->link_irq) {
1723 		free_irq(pl->link_irq, pl);
1724 		pl->link_irq = 0;
1725 	}
1726 
1727 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1728 }
1729 EXPORT_SYMBOL_GPL(phylink_stop);
1730 
1731 /**
1732  * phylink_suspend() - handle a network device suspend event
1733  * @pl: a pointer to a &struct phylink returned from phylink_create()
1734  * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
1735  *
1736  * Handle a network device suspend event. There are several cases:
1737  *
1738  * - If Wake-on-Lan is not active, we can bring down the link between
1739  *   the MAC and PHY by calling phylink_stop().
1740  * - If Wake-on-Lan is active, and being handled only by the PHY, we
1741  *   can also bring down the link between the MAC and PHY.
1742  * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
1743  *   still needs to receive packets, so we can not bring the link down.
1744  */
1745 void phylink_suspend(struct phylink *pl, bool mac_wol)
1746 {
1747 	ASSERT_RTNL();
1748 
1749 	if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) {
1750 		/* Wake-on-Lan enabled, MAC handling */
1751 		mutex_lock(&pl->state_mutex);
1752 
1753 		/* Stop the resolver bringing the link up */
1754 		__set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1755 
1756 		/* Disable the carrier, to prevent transmit timeouts,
1757 		 * but one would hope all packets have been sent. This
1758 		 * also means phylink_resolve() will do nothing.
1759 		 */
1760 		if (pl->netdev)
1761 			netif_carrier_off(pl->netdev);
1762 		else
1763 			pl->old_link_state = false;
1764 
1765 		/* We do not call mac_link_down() here as we want the
1766 		 * link to remain up to receive the WoL packets.
1767 		 */
1768 		mutex_unlock(&pl->state_mutex);
1769 	} else {
1770 		phylink_stop(pl);
1771 	}
1772 }
1773 EXPORT_SYMBOL_GPL(phylink_suspend);
1774 
1775 /**
1776  * phylink_resume() - handle a network device resume event
1777  * @pl: a pointer to a &struct phylink returned from phylink_create()
1778  *
1779  * Undo the effects of phylink_suspend(), returning the link to an
1780  * operational state.
1781  */
1782 void phylink_resume(struct phylink *pl)
1783 {
1784 	ASSERT_RTNL();
1785 
1786 	if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
1787 		/* Wake-on-Lan enabled, MAC handling */
1788 
1789 		/* Call mac_link_down() so we keep the overall state balanced.
1790 		 * Do this under the state_mutex lock for consistency. This
1791 		 * will cause a "Link Down" message to be printed during
1792 		 * resume, which is harmless - the true link state will be
1793 		 * printed when we run a resolve.
1794 		 */
1795 		mutex_lock(&pl->state_mutex);
1796 		phylink_link_down(pl);
1797 		mutex_unlock(&pl->state_mutex);
1798 
1799 		/* Re-apply the link parameters so that all the settings get
1800 		 * restored to the MAC.
1801 		 */
1802 		phylink_mac_initial_config(pl, true);
1803 
1804 		/* Re-enable and re-resolve the link parameters */
1805 		phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL);
1806 	} else {
1807 		phylink_start(pl);
1808 	}
1809 }
1810 EXPORT_SYMBOL_GPL(phylink_resume);
1811 
1812 /**
1813  * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1814  * @pl: a pointer to a &struct phylink returned from phylink_create()
1815  * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1816  *
1817  * Read the wake on lan parameters from the PHY attached to the phylink
1818  * instance specified by @pl. If no PHY is currently attached, report no
1819  * support for wake on lan.
1820  */
1821 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1822 {
1823 	ASSERT_RTNL();
1824 
1825 	wol->supported = 0;
1826 	wol->wolopts = 0;
1827 
1828 	if (pl->phydev)
1829 		phy_ethtool_get_wol(pl->phydev, wol);
1830 }
1831 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1832 
1833 /**
1834  * phylink_ethtool_set_wol() - set wake on lan parameters
1835  * @pl: a pointer to a &struct phylink returned from phylink_create()
1836  * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1837  *
1838  * Set the wake on lan parameters for the PHY attached to the phylink
1839  * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1840  * error.
1841  *
1842  * Returns zero on success or negative errno code.
1843  */
1844 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1845 {
1846 	int ret = -EOPNOTSUPP;
1847 
1848 	ASSERT_RTNL();
1849 
1850 	if (pl->phydev)
1851 		ret = phy_ethtool_set_wol(pl->phydev, wol);
1852 
1853 	return ret;
1854 }
1855 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1856 
1857 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1858 {
1859 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1860 
1861 	linkmode_zero(mask);
1862 	phylink_set_port_modes(mask);
1863 
1864 	linkmode_and(dst, dst, mask);
1865 	linkmode_or(dst, dst, b);
1866 }
1867 
1868 static void phylink_get_ksettings(const struct phylink_link_state *state,
1869 				  struct ethtool_link_ksettings *kset)
1870 {
1871 	phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1872 	linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1873 	kset->base.speed = state->speed;
1874 	kset->base.duplex = state->duplex;
1875 	kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1876 				AUTONEG_DISABLE;
1877 }
1878 
1879 /**
1880  * phylink_ethtool_ksettings_get() - get the current link settings
1881  * @pl: a pointer to a &struct phylink returned from phylink_create()
1882  * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1883  *
1884  * Read the current link settings for the phylink instance specified by @pl.
1885  * This will be the link settings read from the MAC, PHY or fixed link
1886  * settings depending on the current negotiation mode.
1887  */
1888 int phylink_ethtool_ksettings_get(struct phylink *pl,
1889 				  struct ethtool_link_ksettings *kset)
1890 {
1891 	struct phylink_link_state link_state;
1892 
1893 	ASSERT_RTNL();
1894 
1895 	if (pl->phydev)
1896 		phy_ethtool_ksettings_get(pl->phydev, kset);
1897 	else
1898 		kset->base.port = pl->link_port;
1899 
1900 	linkmode_copy(kset->link_modes.supported, pl->supported);
1901 
1902 	switch (pl->cur_link_an_mode) {
1903 	case MLO_AN_FIXED:
1904 		/* We are using fixed settings. Report these as the
1905 		 * current link settings - and note that these also
1906 		 * represent the supported speeds/duplex/pause modes.
1907 		 */
1908 		phylink_get_fixed_state(pl, &link_state);
1909 		phylink_get_ksettings(&link_state, kset);
1910 		break;
1911 
1912 	case MLO_AN_INBAND:
1913 		/* If there is a phy attached, then use the reported
1914 		 * settings from the phy with no modification.
1915 		 */
1916 		if (pl->phydev)
1917 			break;
1918 
1919 		phylink_mac_pcs_get_state(pl, &link_state);
1920 
1921 		/* The MAC is reporting the link results from its own PCS
1922 		 * layer via in-band status. Report these as the current
1923 		 * link settings.
1924 		 */
1925 		phylink_get_ksettings(&link_state, kset);
1926 		break;
1927 	}
1928 
1929 	return 0;
1930 }
1931 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1932 
1933 /**
1934  * phylink_ethtool_ksettings_set() - set the link settings
1935  * @pl: a pointer to a &struct phylink returned from phylink_create()
1936  * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1937  */
1938 int phylink_ethtool_ksettings_set(struct phylink *pl,
1939 				  const struct ethtool_link_ksettings *kset)
1940 {
1941 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1942 	struct phylink_link_state config;
1943 	const struct phy_setting *s;
1944 
1945 	ASSERT_RTNL();
1946 
1947 	if (pl->phydev) {
1948 		/* We can rely on phylib for this update; we also do not need
1949 		 * to update the pl->link_config settings:
1950 		 * - the configuration returned via ksettings_get() will come
1951 		 *   from phylib whenever a PHY is present.
1952 		 * - link_config.interface will be updated by the PHY calling
1953 		 *   back via phylink_phy_change() and a subsequent resolve.
1954 		 * - initial link configuration for PHY mode comes from the
1955 		 *   last phy state updated via phylink_phy_change().
1956 		 * - other configuration changes (e.g. pause modes) are
1957 		 *   performed directly via phylib.
1958 		 * - if in in-band mode with a PHY, the link configuration
1959 		 *   is passed on the link from the PHY, and all of
1960 		 *   link_config.{speed,duplex,an_enabled,pause} are not used.
1961 		 * - the only possible use would be link_config.advertising
1962 		 *   pause modes when in 1000base-X mode with a PHY, but in
1963 		 *   the presence of a PHY, this should not be changed as that
1964 		 *   should be determined from the media side advertisement.
1965 		 */
1966 		return phy_ethtool_ksettings_set(pl->phydev, kset);
1967 	}
1968 
1969 	config = pl->link_config;
1970 
1971 	/* Mask out unsupported advertisements */
1972 	linkmode_and(config.advertising, kset->link_modes.advertising,
1973 		     pl->supported);
1974 
1975 	/* FIXME: should we reject autoneg if phy/mac does not support it? */
1976 	switch (kset->base.autoneg) {
1977 	case AUTONEG_DISABLE:
1978 		/* Autonegotiation disabled, select a suitable speed and
1979 		 * duplex.
1980 		 */
1981 		s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1982 				       pl->supported, false);
1983 		if (!s)
1984 			return -EINVAL;
1985 
1986 		/* If we have a fixed link, refuse to change link parameters.
1987 		 * If the link parameters match, accept them but do nothing.
1988 		 */
1989 		if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1990 			if (s->speed != pl->link_config.speed ||
1991 			    s->duplex != pl->link_config.duplex)
1992 				return -EINVAL;
1993 			return 0;
1994 		}
1995 
1996 		config.speed = s->speed;
1997 		config.duplex = s->duplex;
1998 		break;
1999 
2000 	case AUTONEG_ENABLE:
2001 		/* If we have a fixed link, allow autonegotiation (since that
2002 		 * is our default case) but do not allow the advertisement to
2003 		 * be changed. If the advertisement matches, simply return.
2004 		 */
2005 		if (pl->cur_link_an_mode == MLO_AN_FIXED) {
2006 			if (!linkmode_equal(config.advertising,
2007 					    pl->link_config.advertising))
2008 				return -EINVAL;
2009 			return 0;
2010 		}
2011 
2012 		config.speed = SPEED_UNKNOWN;
2013 		config.duplex = DUPLEX_UNKNOWN;
2014 		break;
2015 
2016 	default:
2017 		return -EINVAL;
2018 	}
2019 
2020 	/* We have ruled out the case with a PHY attached, and the
2021 	 * fixed-link cases.  All that is left are in-band links.
2022 	 */
2023 	config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
2024 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
2025 			 config.an_enabled);
2026 
2027 	/* If this link is with an SFP, ensure that changes to advertised modes
2028 	 * also cause the associated interface to be selected such that the
2029 	 * link can be configured correctly.
2030 	 */
2031 	if (pl->sfp_bus) {
2032 		config.interface = sfp_select_interface(pl->sfp_bus,
2033 							config.advertising);
2034 		if (config.interface == PHY_INTERFACE_MODE_NA) {
2035 			phylink_err(pl,
2036 				    "selection of interface failed, advertisement %*pb\n",
2037 				    __ETHTOOL_LINK_MODE_MASK_NBITS,
2038 				    config.advertising);
2039 			return -EINVAL;
2040 		}
2041 
2042 		/* Revalidate with the selected interface */
2043 		linkmode_copy(support, pl->supported);
2044 		if (phylink_validate(pl, support, &config)) {
2045 			phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
2046 				    phylink_an_mode_str(pl->cur_link_an_mode),
2047 				    phy_modes(config.interface),
2048 				    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2049 			return -EINVAL;
2050 		}
2051 	} else {
2052 		/* Validate without changing the current supported mask. */
2053 		linkmode_copy(support, pl->supported);
2054 		if (phylink_validate(pl, support, &config))
2055 			return -EINVAL;
2056 	}
2057 
2058 	/* If autonegotiation is enabled, we must have an advertisement */
2059 	if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
2060 		return -EINVAL;
2061 
2062 	mutex_lock(&pl->state_mutex);
2063 	pl->link_config.speed = config.speed;
2064 	pl->link_config.duplex = config.duplex;
2065 	pl->link_config.an_enabled = config.an_enabled;
2066 
2067 	if (pl->link_config.interface != config.interface) {
2068 		/* The interface changed, e.g. 1000base-X <-> 2500base-X */
2069 		/* We need to force the link down, then change the interface */
2070 		if (pl->old_link_state) {
2071 			phylink_link_down(pl);
2072 			pl->old_link_state = false;
2073 		}
2074 		if (!test_bit(PHYLINK_DISABLE_STOPPED,
2075 			      &pl->phylink_disable_state))
2076 			phylink_major_config(pl, false, &config);
2077 		pl->link_config.interface = config.interface;
2078 		linkmode_copy(pl->link_config.advertising, config.advertising);
2079 	} else if (!linkmode_equal(pl->link_config.advertising,
2080 				   config.advertising)) {
2081 		linkmode_copy(pl->link_config.advertising, config.advertising);
2082 		phylink_change_inband_advert(pl);
2083 	}
2084 	mutex_unlock(&pl->state_mutex);
2085 
2086 	return 0;
2087 }
2088 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
2089 
2090 /**
2091  * phylink_ethtool_nway_reset() - restart negotiation
2092  * @pl: a pointer to a &struct phylink returned from phylink_create()
2093  *
2094  * Restart negotiation for the phylink instance specified by @pl. This will
2095  * cause any attached phy to restart negotiation with the link partner, and
2096  * if the MAC is in a BaseX mode, the MAC will also be requested to restart
2097  * negotiation.
2098  *
2099  * Returns zero on success, or negative error code.
2100  */
2101 int phylink_ethtool_nway_reset(struct phylink *pl)
2102 {
2103 	int ret = 0;
2104 
2105 	ASSERT_RTNL();
2106 
2107 	if (pl->phydev)
2108 		ret = phy_restart_aneg(pl->phydev);
2109 	phylink_mac_pcs_an_restart(pl);
2110 
2111 	return ret;
2112 }
2113 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
2114 
2115 /**
2116  * phylink_ethtool_get_pauseparam() - get the current pause parameters
2117  * @pl: a pointer to a &struct phylink returned from phylink_create()
2118  * @pause: a pointer to a &struct ethtool_pauseparam
2119  */
2120 void phylink_ethtool_get_pauseparam(struct phylink *pl,
2121 				    struct ethtool_pauseparam *pause)
2122 {
2123 	ASSERT_RTNL();
2124 
2125 	pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
2126 	pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
2127 	pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
2128 }
2129 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
2130 
2131 /**
2132  * phylink_ethtool_set_pauseparam() - set the current pause parameters
2133  * @pl: a pointer to a &struct phylink returned from phylink_create()
2134  * @pause: a pointer to a &struct ethtool_pauseparam
2135  */
2136 int phylink_ethtool_set_pauseparam(struct phylink *pl,
2137 				   struct ethtool_pauseparam *pause)
2138 {
2139 	struct phylink_link_state *config = &pl->link_config;
2140 	bool manual_changed;
2141 	int pause_state;
2142 
2143 	ASSERT_RTNL();
2144 
2145 	if (pl->cur_link_an_mode == MLO_AN_FIXED)
2146 		return -EOPNOTSUPP;
2147 
2148 	if (!phylink_test(pl->supported, Pause) &&
2149 	    !phylink_test(pl->supported, Asym_Pause))
2150 		return -EOPNOTSUPP;
2151 
2152 	if (!phylink_test(pl->supported, Asym_Pause) &&
2153 	    pause->rx_pause != pause->tx_pause)
2154 		return -EINVAL;
2155 
2156 	pause_state = 0;
2157 	if (pause->autoneg)
2158 		pause_state |= MLO_PAUSE_AN;
2159 	if (pause->rx_pause)
2160 		pause_state |= MLO_PAUSE_RX;
2161 	if (pause->tx_pause)
2162 		pause_state |= MLO_PAUSE_TX;
2163 
2164 	mutex_lock(&pl->state_mutex);
2165 	/*
2166 	 * See the comments for linkmode_set_pause(), wrt the deficiencies
2167 	 * with the current implementation.  A solution to this issue would
2168 	 * be:
2169 	 * ethtool  Local device
2170 	 *  rx  tx  Pause AsymDir
2171 	 *  0   0   0     0
2172 	 *  1   0   1     1
2173 	 *  0   1   0     1
2174 	 *  1   1   1     1
2175 	 * and then use the ethtool rx/tx enablement status to mask the
2176 	 * rx/tx pause resolution.
2177 	 */
2178 	linkmode_set_pause(config->advertising, pause->tx_pause,
2179 			   pause->rx_pause);
2180 
2181 	manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
2182 			 (!(pause_state & MLO_PAUSE_AN) &&
2183 			   (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
2184 
2185 	config->pause = pause_state;
2186 
2187 	/* Update our in-band advertisement, triggering a renegotiation if
2188 	 * the advertisement changed.
2189 	 */
2190 	if (!pl->phydev)
2191 		phylink_change_inband_advert(pl);
2192 
2193 	mutex_unlock(&pl->state_mutex);
2194 
2195 	/* If we have a PHY, a change of the pause frame advertisement will
2196 	 * cause phylib to renegotiate (if AN is enabled) which will in turn
2197 	 * call our phylink_phy_change() and trigger a resolve.  Note that
2198 	 * we can't hold our state mutex while calling phy_set_asym_pause().
2199 	 */
2200 	if (pl->phydev)
2201 		phy_set_asym_pause(pl->phydev, pause->rx_pause,
2202 				   pause->tx_pause);
2203 
2204 	/* If the manual pause settings changed, make sure we trigger a
2205 	 * resolve to update their state; we can not guarantee that the
2206 	 * link will cycle.
2207 	 */
2208 	if (manual_changed) {
2209 		pl->mac_link_dropped = true;
2210 		phylink_run_resolve(pl);
2211 	}
2212 
2213 	return 0;
2214 }
2215 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
2216 
2217 /**
2218  * phylink_get_eee_err() - read the energy efficient ethernet error
2219  *   counter
2220  * @pl: a pointer to a &struct phylink returned from phylink_create().
2221  *
2222  * Read the Energy Efficient Ethernet error counter from the PHY associated
2223  * with the phylink instance specified by @pl.
2224  *
2225  * Returns positive error counter value, or negative error code.
2226  */
2227 int phylink_get_eee_err(struct phylink *pl)
2228 {
2229 	int ret = 0;
2230 
2231 	ASSERT_RTNL();
2232 
2233 	if (pl->phydev)
2234 		ret = phy_get_eee_err(pl->phydev);
2235 
2236 	return ret;
2237 }
2238 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
2239 
2240 /**
2241  * phylink_init_eee() - init and check the EEE features
2242  * @pl: a pointer to a &struct phylink returned from phylink_create()
2243  * @clk_stop_enable: allow PHY to stop receive clock
2244  *
2245  * Must be called either with RTNL held or within mac_link_up()
2246  */
2247 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
2248 {
2249 	int ret = -EOPNOTSUPP;
2250 
2251 	if (pl->phydev)
2252 		ret = phy_init_eee(pl->phydev, clk_stop_enable);
2253 
2254 	return ret;
2255 }
2256 EXPORT_SYMBOL_GPL(phylink_init_eee);
2257 
2258 /**
2259  * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
2260  * @pl: a pointer to a &struct phylink returned from phylink_create()
2261  * @eee: a pointer to a &struct ethtool_eee for the read parameters
2262  */
2263 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
2264 {
2265 	int ret = -EOPNOTSUPP;
2266 
2267 	ASSERT_RTNL();
2268 
2269 	if (pl->phydev)
2270 		ret = phy_ethtool_get_eee(pl->phydev, eee);
2271 
2272 	return ret;
2273 }
2274 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
2275 
2276 /**
2277  * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
2278  * @pl: a pointer to a &struct phylink returned from phylink_create()
2279  * @eee: a pointer to a &struct ethtool_eee for the desired parameters
2280  */
2281 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
2282 {
2283 	int ret = -EOPNOTSUPP;
2284 
2285 	ASSERT_RTNL();
2286 
2287 	if (pl->phydev)
2288 		ret = phy_ethtool_set_eee(pl->phydev, eee);
2289 
2290 	return ret;
2291 }
2292 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
2293 
2294 /* This emulates MII registers for a fixed-mode phy operating as per the
2295  * passed in state. "aneg" defines if we report negotiation is possible.
2296  *
2297  * FIXME: should deal with negotiation state too.
2298  */
2299 static int phylink_mii_emul_read(unsigned int reg,
2300 				 struct phylink_link_state *state)
2301 {
2302 	struct fixed_phy_status fs;
2303 	unsigned long *lpa = state->lp_advertising;
2304 	int val;
2305 
2306 	fs.link = state->link;
2307 	fs.speed = state->speed;
2308 	fs.duplex = state->duplex;
2309 	fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
2310 	fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
2311 
2312 	val = swphy_read_reg(reg, &fs);
2313 	if (reg == MII_BMSR) {
2314 		if (!state->an_complete)
2315 			val &= ~BMSR_ANEGCOMPLETE;
2316 	}
2317 	return val;
2318 }
2319 
2320 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
2321 			    unsigned int reg)
2322 {
2323 	struct phy_device *phydev = pl->phydev;
2324 	int prtad, devad;
2325 
2326 	if (mdio_phy_id_is_c45(phy_id)) {
2327 		prtad = mdio_phy_id_prtad(phy_id);
2328 		devad = mdio_phy_id_devad(phy_id);
2329 		devad = mdiobus_c45_addr(devad, reg);
2330 	} else if (phydev->is_c45) {
2331 		switch (reg) {
2332 		case MII_BMCR:
2333 		case MII_BMSR:
2334 		case MII_PHYSID1:
2335 		case MII_PHYSID2:
2336 			devad = __ffs(phydev->c45_ids.mmds_present);
2337 			break;
2338 		case MII_ADVERTISE:
2339 		case MII_LPA:
2340 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2341 				return -EINVAL;
2342 			devad = MDIO_MMD_AN;
2343 			if (reg == MII_ADVERTISE)
2344 				reg = MDIO_AN_ADVERTISE;
2345 			else
2346 				reg = MDIO_AN_LPA;
2347 			break;
2348 		default:
2349 			return -EINVAL;
2350 		}
2351 		prtad = phy_id;
2352 		devad = mdiobus_c45_addr(devad, reg);
2353 	} else {
2354 		prtad = phy_id;
2355 		devad = reg;
2356 	}
2357 	return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
2358 }
2359 
2360 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
2361 			     unsigned int reg, unsigned int val)
2362 {
2363 	struct phy_device *phydev = pl->phydev;
2364 	int prtad, devad;
2365 
2366 	if (mdio_phy_id_is_c45(phy_id)) {
2367 		prtad = mdio_phy_id_prtad(phy_id);
2368 		devad = mdio_phy_id_devad(phy_id);
2369 		devad = mdiobus_c45_addr(devad, reg);
2370 	} else if (phydev->is_c45) {
2371 		switch (reg) {
2372 		case MII_BMCR:
2373 		case MII_BMSR:
2374 		case MII_PHYSID1:
2375 		case MII_PHYSID2:
2376 			devad = __ffs(phydev->c45_ids.mmds_present);
2377 			break;
2378 		case MII_ADVERTISE:
2379 		case MII_LPA:
2380 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2381 				return -EINVAL;
2382 			devad = MDIO_MMD_AN;
2383 			if (reg == MII_ADVERTISE)
2384 				reg = MDIO_AN_ADVERTISE;
2385 			else
2386 				reg = MDIO_AN_LPA;
2387 			break;
2388 		default:
2389 			return -EINVAL;
2390 		}
2391 		prtad = phy_id;
2392 		devad = mdiobus_c45_addr(devad, reg);
2393 	} else {
2394 		prtad = phy_id;
2395 		devad = reg;
2396 	}
2397 
2398 	return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
2399 }
2400 
2401 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
2402 			    unsigned int reg)
2403 {
2404 	struct phylink_link_state state;
2405 	int val = 0xffff;
2406 
2407 	switch (pl->cur_link_an_mode) {
2408 	case MLO_AN_FIXED:
2409 		if (phy_id == 0) {
2410 			phylink_get_fixed_state(pl, &state);
2411 			val = phylink_mii_emul_read(reg, &state);
2412 		}
2413 		break;
2414 
2415 	case MLO_AN_PHY:
2416 		return -EOPNOTSUPP;
2417 
2418 	case MLO_AN_INBAND:
2419 		if (phy_id == 0) {
2420 			phylink_mac_pcs_get_state(pl, &state);
2421 			val = phylink_mii_emul_read(reg, &state);
2422 		}
2423 		break;
2424 	}
2425 
2426 	return val & 0xffff;
2427 }
2428 
2429 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
2430 			     unsigned int reg, unsigned int val)
2431 {
2432 	switch (pl->cur_link_an_mode) {
2433 	case MLO_AN_FIXED:
2434 		break;
2435 
2436 	case MLO_AN_PHY:
2437 		return -EOPNOTSUPP;
2438 
2439 	case MLO_AN_INBAND:
2440 		break;
2441 	}
2442 
2443 	return 0;
2444 }
2445 
2446 /**
2447  * phylink_mii_ioctl() - generic mii ioctl interface
2448  * @pl: a pointer to a &struct phylink returned from phylink_create()
2449  * @ifr: a pointer to a &struct ifreq for socket ioctls
2450  * @cmd: ioctl cmd to execute
2451  *
2452  * Perform the specified MII ioctl on the PHY attached to the phylink instance
2453  * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
2454  *
2455  * Returns: zero on success or negative error code.
2456  *
2457  * %SIOCGMIIPHY:
2458  *  read register from the current PHY.
2459  * %SIOCGMIIREG:
2460  *  read register from the specified PHY.
2461  * %SIOCSMIIREG:
2462  *  set a register on the specified PHY.
2463  */
2464 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
2465 {
2466 	struct mii_ioctl_data *mii = if_mii(ifr);
2467 	int  ret;
2468 
2469 	ASSERT_RTNL();
2470 
2471 	if (pl->phydev) {
2472 		/* PHYs only exist for MLO_AN_PHY and SGMII */
2473 		switch (cmd) {
2474 		case SIOCGMIIPHY:
2475 			mii->phy_id = pl->phydev->mdio.addr;
2476 			fallthrough;
2477 
2478 		case SIOCGMIIREG:
2479 			ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
2480 			if (ret >= 0) {
2481 				mii->val_out = ret;
2482 				ret = 0;
2483 			}
2484 			break;
2485 
2486 		case SIOCSMIIREG:
2487 			ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
2488 						mii->val_in);
2489 			break;
2490 
2491 		default:
2492 			ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
2493 			break;
2494 		}
2495 	} else {
2496 		switch (cmd) {
2497 		case SIOCGMIIPHY:
2498 			mii->phy_id = 0;
2499 			fallthrough;
2500 
2501 		case SIOCGMIIREG:
2502 			ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
2503 			if (ret >= 0) {
2504 				mii->val_out = ret;
2505 				ret = 0;
2506 			}
2507 			break;
2508 
2509 		case SIOCSMIIREG:
2510 			ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
2511 						mii->val_in);
2512 			break;
2513 
2514 		default:
2515 			ret = -EOPNOTSUPP;
2516 			break;
2517 		}
2518 	}
2519 
2520 	return ret;
2521 }
2522 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2523 
2524 /**
2525  * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2526  *   link partners
2527  * @pl: a pointer to a &struct phylink returned from phylink_create()
2528  * @sync: perform action synchronously
2529  *
2530  * If we have a PHY that is not part of a SFP module, then set the speed
2531  * as described in the phy_speed_down() function. Please see this function
2532  * for a description of the @sync parameter.
2533  *
2534  * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2535  */
2536 int phylink_speed_down(struct phylink *pl, bool sync)
2537 {
2538 	int ret = 0;
2539 
2540 	ASSERT_RTNL();
2541 
2542 	if (!pl->sfp_bus && pl->phydev)
2543 		ret = phy_speed_down(pl->phydev, sync);
2544 
2545 	return ret;
2546 }
2547 EXPORT_SYMBOL_GPL(phylink_speed_down);
2548 
2549 /**
2550  * phylink_speed_up() - restore the advertised speeds prior to the call to
2551  *   phylink_speed_down()
2552  * @pl: a pointer to a &struct phylink returned from phylink_create()
2553  *
2554  * If we have a PHY that is not part of a SFP module, then restore the
2555  * PHY speeds as per phy_speed_up().
2556  *
2557  * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2558  */
2559 int phylink_speed_up(struct phylink *pl)
2560 {
2561 	int ret = 0;
2562 
2563 	ASSERT_RTNL();
2564 
2565 	if (!pl->sfp_bus && pl->phydev)
2566 		ret = phy_speed_up(pl->phydev);
2567 
2568 	return ret;
2569 }
2570 EXPORT_SYMBOL_GPL(phylink_speed_up);
2571 
2572 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2573 {
2574 	struct phylink *pl = upstream;
2575 
2576 	pl->netdev->sfp_bus = bus;
2577 }
2578 
2579 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2580 {
2581 	struct phylink *pl = upstream;
2582 
2583 	pl->netdev->sfp_bus = NULL;
2584 }
2585 
2586 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2587 			      const unsigned long *supported,
2588 			      const unsigned long *advertising)
2589 {
2590 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2591 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2592 	struct phylink_link_state config;
2593 	phy_interface_t iface;
2594 	bool changed;
2595 	int ret;
2596 
2597 	linkmode_copy(support, supported);
2598 
2599 	memset(&config, 0, sizeof(config));
2600 	linkmode_copy(config.advertising, advertising);
2601 	config.interface = PHY_INTERFACE_MODE_NA;
2602 	config.speed = SPEED_UNKNOWN;
2603 	config.duplex = DUPLEX_UNKNOWN;
2604 	config.pause = MLO_PAUSE_AN;
2605 	config.an_enabled = pl->link_config.an_enabled;
2606 
2607 	/* Ignore errors if we're expecting a PHY to attach later */
2608 	ret = phylink_validate(pl, support, &config);
2609 	if (ret) {
2610 		phylink_err(pl, "validation with support %*pb failed: %d\n",
2611 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2612 		return ret;
2613 	}
2614 
2615 	iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2616 	if (iface == PHY_INTERFACE_MODE_NA) {
2617 		phylink_err(pl,
2618 			    "selection of interface failed, advertisement %*pb\n",
2619 			    __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2620 		return -EINVAL;
2621 	}
2622 
2623 	config.interface = iface;
2624 	linkmode_copy(support1, support);
2625 	ret = phylink_validate(pl, support1, &config);
2626 	if (ret) {
2627 		phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
2628 			    phylink_an_mode_str(mode),
2629 			    phy_modes(config.interface),
2630 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2631 		return ret;
2632 	}
2633 
2634 	phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2635 		    phylink_an_mode_str(mode), phy_modes(config.interface),
2636 		    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2637 
2638 	if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2639 		return -EINVAL;
2640 
2641 	changed = !linkmode_equal(pl->supported, support) ||
2642 		  !linkmode_equal(pl->link_config.advertising,
2643 				  config.advertising);
2644 	if (changed) {
2645 		linkmode_copy(pl->supported, support);
2646 		linkmode_copy(pl->link_config.advertising, config.advertising);
2647 	}
2648 
2649 	if (pl->cur_link_an_mode != mode ||
2650 	    pl->link_config.interface != config.interface) {
2651 		pl->link_config.interface = config.interface;
2652 		pl->cur_link_an_mode = mode;
2653 
2654 		changed = true;
2655 
2656 		phylink_info(pl, "switched to %s/%s link mode\n",
2657 			     phylink_an_mode_str(mode),
2658 			     phy_modes(config.interface));
2659 	}
2660 
2661 	pl->link_port = pl->sfp_port;
2662 
2663 	if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2664 				 &pl->phylink_disable_state))
2665 		phylink_mac_initial_config(pl, false);
2666 
2667 	return ret;
2668 }
2669 
2670 static int phylink_sfp_module_insert(void *upstream,
2671 				     const struct sfp_eeprom_id *id)
2672 {
2673 	struct phylink *pl = upstream;
2674 	unsigned long *support = pl->sfp_support;
2675 
2676 	ASSERT_RTNL();
2677 
2678 	linkmode_zero(support);
2679 	sfp_parse_support(pl->sfp_bus, id, support);
2680 	pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2681 
2682 	/* If this module may have a PHY connecting later, defer until later */
2683 	pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2684 	if (pl->sfp_may_have_phy)
2685 		return 0;
2686 
2687 	return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2688 }
2689 
2690 static int phylink_sfp_module_start(void *upstream)
2691 {
2692 	struct phylink *pl = upstream;
2693 
2694 	/* If this SFP module has a PHY, start the PHY now. */
2695 	if (pl->phydev) {
2696 		phy_start(pl->phydev);
2697 		return 0;
2698 	}
2699 
2700 	/* If the module may have a PHY but we didn't detect one we
2701 	 * need to configure the MAC here.
2702 	 */
2703 	if (!pl->sfp_may_have_phy)
2704 		return 0;
2705 
2706 	return phylink_sfp_config(pl, MLO_AN_INBAND,
2707 				  pl->sfp_support, pl->sfp_support);
2708 }
2709 
2710 static void phylink_sfp_module_stop(void *upstream)
2711 {
2712 	struct phylink *pl = upstream;
2713 
2714 	/* If this SFP module has a PHY, stop it. */
2715 	if (pl->phydev)
2716 		phy_stop(pl->phydev);
2717 }
2718 
2719 static void phylink_sfp_link_down(void *upstream)
2720 {
2721 	struct phylink *pl = upstream;
2722 
2723 	ASSERT_RTNL();
2724 
2725 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2726 }
2727 
2728 static void phylink_sfp_link_up(void *upstream)
2729 {
2730 	struct phylink *pl = upstream;
2731 
2732 	ASSERT_RTNL();
2733 
2734 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
2735 }
2736 
2737 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2738  * or 802.3z control word, so inband will not work.
2739  */
2740 static bool phylink_phy_no_inband(struct phy_device *phy)
2741 {
2742 	return phy->is_c45 &&
2743 		(phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2744 }
2745 
2746 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2747 {
2748 	struct phylink *pl = upstream;
2749 	phy_interface_t interface;
2750 	u8 mode;
2751 	int ret;
2752 
2753 	/*
2754 	 * This is the new way of dealing with flow control for PHYs,
2755 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2756 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2757 	 * using our validate call to the MAC, we rely upon the MAC
2758 	 * clearing the bits from both supported and advertising fields.
2759 	 */
2760 	phy_support_asym_pause(phy);
2761 
2762 	if (phylink_phy_no_inband(phy))
2763 		mode = MLO_AN_PHY;
2764 	else
2765 		mode = MLO_AN_INBAND;
2766 
2767 	/* Do the initial configuration */
2768 	ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2769 	if (ret < 0)
2770 		return ret;
2771 
2772 	interface = pl->link_config.interface;
2773 	ret = phylink_attach_phy(pl, phy, interface);
2774 	if (ret < 0)
2775 		return ret;
2776 
2777 	ret = phylink_bringup_phy(pl, phy, interface);
2778 	if (ret)
2779 		phy_detach(phy);
2780 
2781 	return ret;
2782 }
2783 
2784 static void phylink_sfp_disconnect_phy(void *upstream)
2785 {
2786 	phylink_disconnect_phy(upstream);
2787 }
2788 
2789 static const struct sfp_upstream_ops sfp_phylink_ops = {
2790 	.attach = phylink_sfp_attach,
2791 	.detach = phylink_sfp_detach,
2792 	.module_insert = phylink_sfp_module_insert,
2793 	.module_start = phylink_sfp_module_start,
2794 	.module_stop = phylink_sfp_module_stop,
2795 	.link_up = phylink_sfp_link_up,
2796 	.link_down = phylink_sfp_link_down,
2797 	.connect_phy = phylink_sfp_connect_phy,
2798 	.disconnect_phy = phylink_sfp_disconnect_phy,
2799 };
2800 
2801 /* Helpers for MAC drivers */
2802 
2803 /**
2804  * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2805  * @state: a pointer to a &struct phylink_link_state
2806  *
2807  * Inspect the interface mode, advertising mask or forced speed and
2808  * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2809  * the interface mode to suit.  @state->interface is appropriately
2810  * updated, and the advertising mask has the "other" baseX_Full flag
2811  * cleared.
2812  */
2813 void phylink_helper_basex_speed(struct phylink_link_state *state)
2814 {
2815 	if (phy_interface_mode_is_8023z(state->interface)) {
2816 		bool want_2500 = state->an_enabled ?
2817 			phylink_test(state->advertising, 2500baseX_Full) :
2818 			state->speed == SPEED_2500;
2819 
2820 		if (want_2500) {
2821 			phylink_clear(state->advertising, 1000baseX_Full);
2822 			state->interface = PHY_INTERFACE_MODE_2500BASEX;
2823 		} else {
2824 			phylink_clear(state->advertising, 2500baseX_Full);
2825 			state->interface = PHY_INTERFACE_MODE_1000BASEX;
2826 		}
2827 	}
2828 }
2829 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2830 
2831 static void phylink_decode_c37_word(struct phylink_link_state *state,
2832 				    uint16_t config_reg, int speed)
2833 {
2834 	bool tx_pause, rx_pause;
2835 	int fd_bit;
2836 
2837 	if (speed == SPEED_2500)
2838 		fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2839 	else
2840 		fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2841 
2842 	mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2843 
2844 	if (linkmode_test_bit(fd_bit, state->advertising) &&
2845 	    linkmode_test_bit(fd_bit, state->lp_advertising)) {
2846 		state->speed = speed;
2847 		state->duplex = DUPLEX_FULL;
2848 	} else {
2849 		/* negotiation failure */
2850 		state->link = false;
2851 	}
2852 
2853 	linkmode_resolve_pause(state->advertising, state->lp_advertising,
2854 			       &tx_pause, &rx_pause);
2855 
2856 	if (tx_pause)
2857 		state->pause |= MLO_PAUSE_TX;
2858 	if (rx_pause)
2859 		state->pause |= MLO_PAUSE_RX;
2860 }
2861 
2862 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2863 				      uint16_t config_reg)
2864 {
2865 	if (!(config_reg & LPA_SGMII_LINK)) {
2866 		state->link = false;
2867 		return;
2868 	}
2869 
2870 	switch (config_reg & LPA_SGMII_SPD_MASK) {
2871 	case LPA_SGMII_10:
2872 		state->speed = SPEED_10;
2873 		break;
2874 	case LPA_SGMII_100:
2875 		state->speed = SPEED_100;
2876 		break;
2877 	case LPA_SGMII_1000:
2878 		state->speed = SPEED_1000;
2879 		break;
2880 	default:
2881 		state->link = false;
2882 		return;
2883 	}
2884 	if (config_reg & LPA_SGMII_FULL_DUPLEX)
2885 		state->duplex = DUPLEX_FULL;
2886 	else
2887 		state->duplex = DUPLEX_HALF;
2888 }
2889 
2890 /**
2891  * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2892  * @state: a pointer to a struct phylink_link_state.
2893  * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2894  *
2895  * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2896  * code word.  Decode the USXGMII code word and populate the corresponding fields
2897  * (speed, duplex) into the phylink_link_state structure.
2898  */
2899 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2900 				 uint16_t lpa)
2901 {
2902 	switch (lpa & MDIO_USXGMII_SPD_MASK) {
2903 	case MDIO_USXGMII_10:
2904 		state->speed = SPEED_10;
2905 		break;
2906 	case MDIO_USXGMII_100:
2907 		state->speed = SPEED_100;
2908 		break;
2909 	case MDIO_USXGMII_1000:
2910 		state->speed = SPEED_1000;
2911 		break;
2912 	case MDIO_USXGMII_2500:
2913 		state->speed = SPEED_2500;
2914 		break;
2915 	case MDIO_USXGMII_5000:
2916 		state->speed = SPEED_5000;
2917 		break;
2918 	case MDIO_USXGMII_10G:
2919 		state->speed = SPEED_10000;
2920 		break;
2921 	default:
2922 		state->link = false;
2923 		return;
2924 	}
2925 
2926 	if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2927 		state->duplex = DUPLEX_FULL;
2928 	else
2929 		state->duplex = DUPLEX_HALF;
2930 }
2931 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2932 
2933 /**
2934  * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
2935  * @state: a pointer to a &struct phylink_link_state.
2936  * @bmsr: The value of the %MII_BMSR register
2937  * @lpa: The value of the %MII_LPA register
2938  *
2939  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2940  * clause 37 negotiation and/or SGMII control.
2941  *
2942  * Parse the Clause 37 or Cisco SGMII link partner negotiation word into
2943  * the phylink @state structure. This is suitable to be used for implementing
2944  * the mac_pcs_get_state() member of the struct phylink_mac_ops structure if
2945  * accessing @bmsr and @lpa cannot be done with MDIO directly.
2946  */
2947 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
2948 				      u16 bmsr, u16 lpa)
2949 {
2950 	state->link = !!(bmsr & BMSR_LSTATUS);
2951 	state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2952 	/* If there is no link or autonegotiation is disabled, the LP advertisement
2953 	 * data is not meaningful, so don't go any further.
2954 	 */
2955 	if (!state->link || !state->an_enabled)
2956 		return;
2957 
2958 	switch (state->interface) {
2959 	case PHY_INTERFACE_MODE_1000BASEX:
2960 		phylink_decode_c37_word(state, lpa, SPEED_1000);
2961 		break;
2962 
2963 	case PHY_INTERFACE_MODE_2500BASEX:
2964 		phylink_decode_c37_word(state, lpa, SPEED_2500);
2965 		break;
2966 
2967 	case PHY_INTERFACE_MODE_SGMII:
2968 	case PHY_INTERFACE_MODE_QSGMII:
2969 		phylink_decode_sgmii_word(state, lpa);
2970 		break;
2971 
2972 	default:
2973 		state->link = false;
2974 		break;
2975 	}
2976 }
2977 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state);
2978 
2979 /**
2980  * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2981  * @pcs: a pointer to a &struct mdio_device.
2982  * @state: a pointer to a &struct phylink_link_state.
2983  *
2984  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2985  * clause 37 negotiation and/or SGMII control.
2986  *
2987  * Read the MAC PCS state from the MII device configured in @config and
2988  * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2989  * the phylink @state structure. This is suitable to be directly plugged
2990  * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2991  * structure.
2992  */
2993 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2994 				   struct phylink_link_state *state)
2995 {
2996 	int bmsr, lpa;
2997 
2998 	bmsr = mdiodev_read(pcs, MII_BMSR);
2999 	lpa = mdiodev_read(pcs, MII_LPA);
3000 	if (bmsr < 0 || lpa < 0) {
3001 		state->link = false;
3002 		return;
3003 	}
3004 
3005 	phylink_mii_c22_pcs_decode_state(state, bmsr, lpa);
3006 }
3007 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
3008 
3009 /**
3010  * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
3011  *	advertisement
3012  * @interface: the PHY interface mode being configured
3013  * @advertising: the ethtool advertisement mask
3014  *
3015  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3016  * clause 37 negotiation and/or SGMII control.
3017  *
3018  * Encode the clause 37 PCS advertisement as specified by @interface and
3019  * @advertising.
3020  *
3021  * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
3022  */
3023 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
3024 					     const unsigned long *advertising)
3025 {
3026 	u16 adv;
3027 
3028 	switch (interface) {
3029 	case PHY_INTERFACE_MODE_1000BASEX:
3030 	case PHY_INTERFACE_MODE_2500BASEX:
3031 		adv = ADVERTISE_1000XFULL;
3032 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3033 				      advertising))
3034 			adv |= ADVERTISE_1000XPAUSE;
3035 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3036 				      advertising))
3037 			adv |= ADVERTISE_1000XPSE_ASYM;
3038 		return adv;
3039 	case PHY_INTERFACE_MODE_SGMII:
3040 		return 0x0001;
3041 	default:
3042 		/* Nothing to do for other modes */
3043 		return -EINVAL;
3044 	}
3045 }
3046 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement);
3047 
3048 /**
3049  * phylink_mii_c22_pcs_config() - configure clause 22 PCS
3050  * @pcs: a pointer to a &struct mdio_device.
3051  * @mode: link autonegotiation mode
3052  * @interface: the PHY interface mode being configured
3053  * @advertising: the ethtool advertisement mask
3054  *
3055  * Configure a Clause 22 PCS PHY with the appropriate negotiation
3056  * parameters for the @mode, @interface and @advertising parameters.
3057  * Returns negative error number on failure, zero if the advertisement
3058  * has not changed, or positive if there is a change.
3059  */
3060 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
3061 			       phy_interface_t interface,
3062 			       const unsigned long *advertising)
3063 {
3064 	bool changed = 0;
3065 	u16 bmcr;
3066 	int ret, adv;
3067 
3068 	adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
3069 	if (adv >= 0) {
3070 		ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
3071 					     MII_ADVERTISE, 0xffff, adv);
3072 		if (ret < 0)
3073 			return ret;
3074 		changed = ret;
3075 	}
3076 
3077 	/* Ensure ISOLATE bit is disabled */
3078 	if (mode == MLO_AN_INBAND &&
3079 	    linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, advertising))
3080 		bmcr = BMCR_ANENABLE;
3081 	else
3082 		bmcr = 0;
3083 
3084 	ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
3085 	if (ret < 0)
3086 		return ret;
3087 
3088 	return changed;
3089 }
3090 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
3091 
3092 /**
3093  * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
3094  * @pcs: a pointer to a &struct mdio_device.
3095  *
3096  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3097  * clause 37 negotiation.
3098  *
3099  * Restart the clause 37 negotiation with the link partner. This is
3100  * suitable to be directly plugged into the mac_pcs_get_state() member
3101  * of the struct phylink_mac_ops structure.
3102  */
3103 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
3104 {
3105 	int val = mdiodev_read(pcs, MII_BMCR);
3106 
3107 	if (val >= 0) {
3108 		val |= BMCR_ANRESTART;
3109 
3110 		mdiodev_write(pcs, MII_BMCR, val);
3111 	}
3112 }
3113 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
3114 
3115 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
3116 				   struct phylink_link_state *state)
3117 {
3118 	struct mii_bus *bus = pcs->bus;
3119 	int addr = pcs->addr;
3120 	int stat;
3121 
3122 	stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
3123 	if (stat < 0) {
3124 		state->link = false;
3125 		return;
3126 	}
3127 
3128 	state->link = !!(stat & MDIO_STAT1_LSTATUS);
3129 	if (!state->link)
3130 		return;
3131 
3132 	switch (state->interface) {
3133 	case PHY_INTERFACE_MODE_10GBASER:
3134 		state->speed = SPEED_10000;
3135 		state->duplex = DUPLEX_FULL;
3136 		break;
3137 
3138 	default:
3139 		break;
3140 	}
3141 }
3142 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
3143 
3144 MODULE_LICENSE("GPL v2");
3145