xref: /linux/drivers/net/phy/phylink.c (revision b3827c91cc9979fe04d99e016fb9c5f6260f29a0)
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 "phy-caps.h"
24 #include "sfp.h"
25 #include "swphy.h"
26 
27 enum {
28 	PHYLINK_DISABLE_STOPPED,
29 	PHYLINK_DISABLE_LINK,
30 	PHYLINK_DISABLE_MAC_WOL,
31 	PHYLINK_DISABLE_REPLAY,
32 
33 	PCS_STATE_DOWN = 0,
34 	PCS_STATE_STARTING,
35 	PCS_STATE_STARTED,
36 };
37 
38 /**
39  * struct phylink - internal data type for phylink
40  */
41 struct phylink {
42 	/* private: */
43 	struct net_device *netdev;
44 	const struct phylink_mac_ops *mac_ops;
45 	struct phylink_config *config;
46 	struct phylink_pcs *pcs;
47 	struct device *dev;
48 	unsigned int old_link_state:1;
49 
50 	unsigned long phylink_disable_state; /* bitmask of disables */
51 	struct phy_device *phydev;
52 	phy_interface_t link_interface;	/* PHY_INTERFACE_xxx */
53 	u8 cfg_link_an_mode;		/* MLO_AN_xxx */
54 	u8 req_link_an_mode;		/* Requested MLO_AN_xxx mode */
55 	u8 act_link_an_mode;		/* Active MLO_AN_xxx mode */
56 	u8 link_port;			/* The current non-phy ethtool port */
57 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
58 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported_lpi);
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 
70 	struct mutex state_mutex;
71 	/* Serialize updates to pl->phydev with phylink_resolve() */
72 	struct mutex phydev_mutex;
73 	struct phylink_link_state phy_state;
74 	unsigned int phy_ib_mode;
75 	struct work_struct resolve;
76 	unsigned int pcs_neg_mode;
77 	unsigned int pcs_state;
78 
79 	bool link_failed;
80 	bool suspend_link_up;
81 	bool force_major_config;
82 	bool major_config_failed;
83 	bool mac_supports_eee_ops;
84 	bool mac_supports_eee;
85 	bool phy_enable_tx_lpi;
86 	bool mac_enable_tx_lpi;
87 	bool mac_tx_clk_stop;
88 	u32 mac_tx_lpi_timer;
89 	u8 mac_rx_clk_stop_blocked;
90 
91 	struct sfp_bus *sfp_bus;
92 	bool sfp_may_have_phy;
93 	DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
94 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
95 	u8 sfp_port;
96 
97 	struct eee_config eee_cfg;
98 
99 	u32 wolopts_mac;
100 	u8 wol_sopass[SOPASS_MAX];
101 };
102 
103 #define phylink_printk(level, pl, fmt, ...) \
104 	do { \
105 		if ((pl)->config->type == PHYLINK_NETDEV) \
106 			netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
107 		else if ((pl)->config->type == PHYLINK_DEV) \
108 			dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
109 	} while (0)
110 
111 #define phylink_err(pl, fmt, ...) \
112 	phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
113 #define phylink_warn(pl, fmt, ...) \
114 	phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
115 #define phylink_info(pl, fmt, ...) \
116 	phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
117 #if defined(CONFIG_DYNAMIC_DEBUG)
118 #define phylink_dbg(pl, fmt, ...) \
119 do {									\
120 	if ((pl)->config->type == PHYLINK_NETDEV)			\
121 		netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__);		\
122 	else if ((pl)->config->type == PHYLINK_DEV)			\
123 		dev_dbg((pl)->dev, fmt, ##__VA_ARGS__);			\
124 } while (0)
125 #elif defined(DEBUG)
126 #define phylink_dbg(pl, fmt, ...)					\
127 	phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
128 #else
129 #define phylink_dbg(pl, fmt, ...)					\
130 ({									\
131 	if (0)								\
132 		phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__);	\
133 })
134 #endif
135 
136 static const phy_interface_t phylink_sfp_interface_preference[] = {
137 	PHY_INTERFACE_MODE_100GBASEP,
138 	PHY_INTERFACE_MODE_50GBASER,
139 	PHY_INTERFACE_MODE_LAUI,
140 	PHY_INTERFACE_MODE_25GBASER,
141 	PHY_INTERFACE_MODE_USXGMII,
142 	PHY_INTERFACE_MODE_10GBASER,
143 	PHY_INTERFACE_MODE_5GBASER,
144 	PHY_INTERFACE_MODE_2500BASEX,
145 	PHY_INTERFACE_MODE_SGMII,
146 	PHY_INTERFACE_MODE_1000BASEX,
147 	PHY_INTERFACE_MODE_100BASEX,
148 };
149 
150 static DECLARE_PHY_INTERFACE_MASK(phylink_sfp_interfaces);
151 
152 /**
153  * phylink_set_port_modes() - set the port type modes in the ethtool mask
154  * @mask: ethtool link mode mask
155  *
156  * Sets all the port type modes in the ethtool mask.  MAC drivers should
157  * use this in their 'validate' callback.
158  */
159 void phylink_set_port_modes(unsigned long *mask)
160 {
161 	phylink_set(mask, TP);
162 	phylink_set(mask, AUI);
163 	phylink_set(mask, MII);
164 	phylink_set(mask, FIBRE);
165 	phylink_set(mask, BNC);
166 	phylink_set(mask, Backplane);
167 }
168 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
169 
170 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
171 {
172 	__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
173 
174 	phylink_set_port_modes(tmp);
175 	phylink_set(tmp, Autoneg);
176 	phylink_set(tmp, Pause);
177 	phylink_set(tmp, Asym_Pause);
178 
179 	return linkmode_subset(linkmode, tmp);
180 }
181 
182 static const char *phylink_an_mode_str(unsigned int mode)
183 {
184 	static const char *modestr[] = {
185 		[MLO_AN_PHY] = "phy",
186 		[MLO_AN_FIXED] = "fixed",
187 		[MLO_AN_INBAND] = "inband",
188 	};
189 
190 	return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
191 }
192 
193 static const char *phylink_pcs_mode_str(unsigned int mode)
194 {
195 	if (!mode)
196 		return "none";
197 
198 	if (mode & PHYLINK_PCS_NEG_OUTBAND)
199 		return "outband";
200 
201 	if (mode & PHYLINK_PCS_NEG_INBAND) {
202 		if (mode & PHYLINK_PCS_NEG_ENABLED)
203 			return "inband,an-enabled";
204 		else
205 			return "inband,an-disabled";
206 	}
207 
208 	return "unknown";
209 }
210 
211 static unsigned int phylink_interface_signal_rate(phy_interface_t interface)
212 {
213 	switch (interface) {
214 	case PHY_INTERFACE_MODE_SGMII:
215 	case PHY_INTERFACE_MODE_1000BASEX: /* 1.25Mbd */
216 		return 1250;
217 	case PHY_INTERFACE_MODE_2500BASEX: /* 3.125Mbd */
218 		return 3125;
219 	case PHY_INTERFACE_MODE_5GBASER: /* 5.15625Mbd */
220 		return 5156;
221 	case PHY_INTERFACE_MODE_10GBASER: /* 10.3125Mbd */
222 		return 10313;
223 	default:
224 		return 0;
225 	}
226 }
227 
228 /**
229  * phylink_interface_max_speed() - get the maximum speed of a phy interface
230  * @interface: phy interface mode defined by &typedef phy_interface_t
231  *
232  * Determine the maximum speed of a phy interface. This is intended to help
233  * determine the correct speed to pass to the MAC when the phy is performing
234  * rate matching.
235  *
236  * Return: The maximum speed of @interface
237  */
238 static int phylink_interface_max_speed(phy_interface_t interface)
239 {
240 	switch (interface) {
241 	case PHY_INTERFACE_MODE_100BASEX:
242 	case PHY_INTERFACE_MODE_REVRMII:
243 	case PHY_INTERFACE_MODE_RMII:
244 	case PHY_INTERFACE_MODE_SMII:
245 	case PHY_INTERFACE_MODE_REVMII:
246 	case PHY_INTERFACE_MODE_MII:
247 	case PHY_INTERFACE_MODE_MIILITE:
248 		return SPEED_100;
249 
250 	case PHY_INTERFACE_MODE_TBI:
251 	case PHY_INTERFACE_MODE_MOCA:
252 	case PHY_INTERFACE_MODE_RTBI:
253 	case PHY_INTERFACE_MODE_1000BASEX:
254 	case PHY_INTERFACE_MODE_1000BASEKX:
255 	case PHY_INTERFACE_MODE_TRGMII:
256 	case PHY_INTERFACE_MODE_RGMII_TXID:
257 	case PHY_INTERFACE_MODE_RGMII_RXID:
258 	case PHY_INTERFACE_MODE_RGMII_ID:
259 	case PHY_INTERFACE_MODE_RGMII:
260 	case PHY_INTERFACE_MODE_PSGMII:
261 	case PHY_INTERFACE_MODE_QSGMII:
262 	case PHY_INTERFACE_MODE_QUSGMII:
263 	case PHY_INTERFACE_MODE_SGMII:
264 	case PHY_INTERFACE_MODE_GMII:
265 		return SPEED_1000;
266 
267 	case PHY_INTERFACE_MODE_2500BASEX:
268 	case PHY_INTERFACE_MODE_10G_QXGMII:
269 		return SPEED_2500;
270 
271 	case PHY_INTERFACE_MODE_5GBASER:
272 		return SPEED_5000;
273 
274 	case PHY_INTERFACE_MODE_XGMII:
275 	case PHY_INTERFACE_MODE_RXAUI:
276 	case PHY_INTERFACE_MODE_XAUI:
277 	case PHY_INTERFACE_MODE_10GBASER:
278 	case PHY_INTERFACE_MODE_10GKR:
279 	case PHY_INTERFACE_MODE_USXGMII:
280 		return SPEED_10000;
281 
282 	case PHY_INTERFACE_MODE_25GBASER:
283 		return SPEED_25000;
284 
285 	case PHY_INTERFACE_MODE_XLGMII:
286 		return SPEED_40000;
287 
288 	case PHY_INTERFACE_MODE_50GBASER:
289 	case PHY_INTERFACE_MODE_LAUI:
290 		return SPEED_50000;
291 
292 	case PHY_INTERFACE_MODE_100GBASEP:
293 		return SPEED_100000;
294 
295 	case PHY_INTERFACE_MODE_INTERNAL:
296 	case PHY_INTERFACE_MODE_NA:
297 	case PHY_INTERFACE_MODE_MAX:
298 		/* No idea! Garbage in, unknown out */
299 		return SPEED_UNKNOWN;
300 	}
301 
302 	/* If we get here, someone forgot to add an interface mode above */
303 	WARN_ON_ONCE(1);
304 	return SPEED_UNKNOWN;
305 }
306 
307 static struct {
308 	unsigned long mask;
309 	int speed;
310 	unsigned int duplex;
311 	unsigned int caps_bit;
312 } phylink_caps_params[] = {
313 	{ MAC_400000FD, SPEED_400000, DUPLEX_FULL, BIT(LINK_CAPA_400000FD) },
314 	{ MAC_200000FD, SPEED_200000, DUPLEX_FULL, BIT(LINK_CAPA_200000FD) },
315 	{ MAC_100000FD, SPEED_100000, DUPLEX_FULL, BIT(LINK_CAPA_100000FD) },
316 	{ MAC_80000FD,  SPEED_80000,  DUPLEX_FULL, BIT(LINK_CAPA_80000FD) },
317 	{ MAC_56000FD,  SPEED_56000,  DUPLEX_FULL, BIT(LINK_CAPA_56000FD) },
318 	{ MAC_50000FD,  SPEED_50000,  DUPLEX_FULL, BIT(LINK_CAPA_50000FD) },
319 	{ MAC_40000FD,  SPEED_40000,  DUPLEX_FULL, BIT(LINK_CAPA_40000FD) },
320 	{ MAC_25000FD,  SPEED_25000,  DUPLEX_FULL, BIT(LINK_CAPA_25000FD) },
321 	{ MAC_20000FD,  SPEED_20000,  DUPLEX_FULL, BIT(LINK_CAPA_20000FD) },
322 	{ MAC_10000FD,  SPEED_10000,  DUPLEX_FULL, BIT(LINK_CAPA_10000FD) },
323 	{ MAC_5000FD,   SPEED_5000,   DUPLEX_FULL, BIT(LINK_CAPA_5000FD) },
324 	{ MAC_2500FD,   SPEED_2500,   DUPLEX_FULL, BIT(LINK_CAPA_2500FD) },
325 	{ MAC_1000FD,   SPEED_1000,   DUPLEX_FULL, BIT(LINK_CAPA_1000FD) },
326 	{ MAC_1000HD,   SPEED_1000,   DUPLEX_HALF, BIT(LINK_CAPA_1000HD) },
327 	{ MAC_100FD,    SPEED_100,    DUPLEX_FULL, BIT(LINK_CAPA_100FD) },
328 	{ MAC_100HD,    SPEED_100,    DUPLEX_HALF, BIT(LINK_CAPA_100HD) },
329 	{ MAC_10FD,     SPEED_10,     DUPLEX_FULL, BIT(LINK_CAPA_10FD) },
330 	{ MAC_10HD,     SPEED_10,     DUPLEX_HALF, BIT(LINK_CAPA_10HD) },
331 };
332 
333 /**
334  * phylink_caps_to_link_caps() - Convert a set of MAC capabilities LINK caps
335  * @caps: A set of MAC capabilities
336  *
337  * Returns: The corresponding set of LINK_CAPA as defined in phy-caps.h
338  */
339 static unsigned long phylink_caps_to_link_caps(unsigned long caps)
340 {
341 	unsigned long link_caps = 0;
342 	int i;
343 
344 	for (i = 0; i <  ARRAY_SIZE(phylink_caps_params); i++)
345 		if (caps & phylink_caps_params[i].mask)
346 			link_caps |= phylink_caps_params[i].caps_bit;
347 
348 	return link_caps;
349 }
350 
351 static unsigned long phylink_link_caps_to_mac_caps(unsigned long link_caps)
352 {
353 	unsigned long caps = 0;
354 	int i;
355 
356 	for (i = 0; i <  ARRAY_SIZE(phylink_caps_params); i++)
357 		if (link_caps & phylink_caps_params[i].caps_bit)
358 			caps |= phylink_caps_params[i].mask;
359 
360 	return caps;
361 }
362 
363 /**
364  * phylink_caps_to_linkmodes() - Convert capabilities to ethtool link modes
365  * @linkmodes: ethtool linkmode mask (must be already initialised)
366  * @caps: bitmask of MAC capabilities
367  *
368  * Set all possible pause, speed and duplex linkmodes in @linkmodes that are
369  * supported by the @caps. @linkmodes must have been initialised previously.
370  */
371 static void phylink_caps_to_linkmodes(unsigned long *linkmodes,
372 				      unsigned long caps)
373 {
374 	unsigned long link_caps = phylink_caps_to_link_caps(caps);
375 
376 	if (caps & MAC_SYM_PAUSE)
377 		__set_bit(ETHTOOL_LINK_MODE_Pause_BIT, linkmodes);
378 
379 	if (caps & MAC_ASYM_PAUSE)
380 		__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, linkmodes);
381 
382 	phy_caps_linkmodes(link_caps, linkmodes);
383 }
384 
385 /**
386  * phylink_limit_mac_speed - limit the phylink_config to a maximum speed
387  * @config: pointer to a &struct phylink_config
388  * @max_speed: maximum speed
389  *
390  * Mask off MAC capabilities for speeds higher than the @max_speed parameter.
391  * Any further motifications of config.mac_capabilities will override this.
392  */
393 void phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed)
394 {
395 	int i;
396 
397 	for (i = 0; i < ARRAY_SIZE(phylink_caps_params) &&
398 		    phylink_caps_params[i].speed > max_speed; i++)
399 		config->mac_capabilities &= ~phylink_caps_params[i].mask;
400 }
401 EXPORT_SYMBOL_GPL(phylink_limit_mac_speed);
402 
403 /**
404  * phylink_cap_from_speed_duplex - Get mac capability from speed/duplex
405  * @speed: the speed to search for
406  * @duplex: the duplex to search for
407  *
408  * Find the mac capability for a given speed and duplex.
409  *
410  * Return: A mask with the mac capability patching @speed and @duplex, or 0 if
411  *         there were no matches.
412  */
413 static unsigned long phylink_cap_from_speed_duplex(int speed,
414 						   unsigned int duplex)
415 {
416 	int i;
417 
418 	for (i = 0; i < ARRAY_SIZE(phylink_caps_params); i++) {
419 		if (speed == phylink_caps_params[i].speed &&
420 		    duplex == phylink_caps_params[i].duplex)
421 			return phylink_caps_params[i].mask;
422 	}
423 
424 	return 0;
425 }
426 
427 /**
428  * phylink_get_capabilities() - get capabilities for a given MAC
429  * @interface: phy interface mode defined by &typedef phy_interface_t
430  * @mac_capabilities: bitmask of MAC capabilities
431  * @rate_matching: type of rate matching being performed
432  *
433  * Get the MAC capabilities that are supported by the @interface mode and
434  * @mac_capabilities.
435  */
436 static unsigned long phylink_get_capabilities(phy_interface_t interface,
437 					      unsigned long mac_capabilities,
438 					      int rate_matching)
439 {
440 	unsigned long link_caps = phy_caps_from_interface(interface);
441 	int max_speed = phylink_interface_max_speed(interface);
442 	unsigned long caps = MAC_SYM_PAUSE | MAC_ASYM_PAUSE;
443 	unsigned long matched_caps = 0;
444 
445 	caps |= phylink_link_caps_to_mac_caps(link_caps);
446 
447 	switch (rate_matching) {
448 	case RATE_MATCH_OPEN_LOOP:
449 		/* TODO */
450 		fallthrough;
451 	case RATE_MATCH_NONE:
452 		matched_caps = 0;
453 		break;
454 	case RATE_MATCH_PAUSE: {
455 		/* The MAC must support asymmetric pause towards the local
456 		 * device for this. We could allow just symmetric pause, but
457 		 * then we might have to renegotiate if the link partner
458 		 * doesn't support pause. This is because there's no way to
459 		 * accept pause frames without transmitting them if we only
460 		 * support symmetric pause.
461 		 */
462 		if (!(mac_capabilities & MAC_SYM_PAUSE) ||
463 		    !(mac_capabilities & MAC_ASYM_PAUSE))
464 			break;
465 
466 		/* We can't adapt if the MAC doesn't support the interface's
467 		 * max speed at full duplex.
468 		 */
469 		if (mac_capabilities &
470 		    phylink_cap_from_speed_duplex(max_speed, DUPLEX_FULL))
471 			matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
472 		break;
473 	}
474 	case RATE_MATCH_CRS:
475 		/* The MAC must support half duplex at the interface's max
476 		 * speed.
477 		 */
478 		if (mac_capabilities &
479 		    phylink_cap_from_speed_duplex(max_speed, DUPLEX_HALF)) {
480 			matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
481 			matched_caps &= mac_capabilities;
482 		}
483 		break;
484 	}
485 
486 	return (caps & mac_capabilities) | matched_caps;
487 }
488 
489 /**
490  * phylink_validate_mask_caps() - Restrict link modes based on caps
491  * @supported: ethtool bitmask for supported link modes.
492  * @state: pointer to a &struct phylink_link_state.
493  * @mac_capabilities: bitmask of MAC capabilities
494  *
495  * Calculate the supported link modes based on @mac_capabilities, and restrict
496  * @supported and @state based on that. Use this function if your capabiliies
497  * aren't constant, such as if they vary depending on the interface.
498  */
499 static void phylink_validate_mask_caps(unsigned long *supported,
500 				       struct phylink_link_state *state,
501 				       unsigned long mac_capabilities)
502 {
503 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
504 	unsigned long caps;
505 
506 	phylink_set_port_modes(mask);
507 	phylink_set(mask, Autoneg);
508 	caps = phylink_get_capabilities(state->interface, mac_capabilities,
509 					state->rate_matching);
510 	phylink_caps_to_linkmodes(mask, caps);
511 
512 	linkmode_and(supported, supported, mask);
513 	linkmode_and(state->advertising, state->advertising, mask);
514 }
515 
516 static int phylink_validate_mac_and_pcs(struct phylink *pl,
517 					unsigned long *supported,
518 					struct phylink_link_state *state)
519 {
520 	struct phylink_pcs *pcs = NULL;
521 	unsigned long capabilities;
522 	int ret;
523 
524 	/* Get the PCS for this interface mode */
525 	if (pl->mac_ops->mac_select_pcs) {
526 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
527 		if (IS_ERR(pcs))
528 			return PTR_ERR(pcs);
529 	}
530 
531 	if (pcs) {
532 		/* The PCS, if present, must be setup before phylink_create()
533 		 * has been called. If the ops is not initialised, print an
534 		 * error and backtrace rather than oopsing the kernel.
535 		 */
536 		if (!pcs->ops) {
537 			phylink_err(pl, "interface %s: uninitialised PCS\n",
538 				    phy_modes(state->interface));
539 			dump_stack();
540 			return -EINVAL;
541 		}
542 
543 		/* Ensure that this PCS supports the interface which the MAC
544 		 * returned it for. It is an error for the MAC to return a PCS
545 		 * that does not support the interface mode.
546 		 */
547 		if (!phy_interface_empty(pcs->supported_interfaces) &&
548 		    !test_bit(state->interface, pcs->supported_interfaces)) {
549 			phylink_err(pl, "MAC returned PCS which does not support %s\n",
550 				    phy_modes(state->interface));
551 			return -EINVAL;
552 		}
553 
554 		/* Validate the link parameters with the PCS */
555 		if (pcs->ops->pcs_validate) {
556 			ret = pcs->ops->pcs_validate(pcs, supported, state);
557 			if (ret < 0 || phylink_is_empty_linkmode(supported))
558 				return -EINVAL;
559 
560 			/* Ensure the advertising mask is a subset of the
561 			 * supported mask.
562 			 */
563 			linkmode_and(state->advertising, state->advertising,
564 				     supported);
565 		}
566 	}
567 
568 	/* Then validate the link parameters with the MAC */
569 	if (pl->mac_ops->mac_get_caps)
570 		capabilities = pl->mac_ops->mac_get_caps(pl->config,
571 							 state->interface);
572 	else
573 		capabilities = pl->config->mac_capabilities;
574 
575 	phylink_validate_mask_caps(supported, state, capabilities);
576 
577 	return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
578 }
579 
580 static void phylink_validate_one(struct phylink *pl, struct phy_device *phy,
581 				 const unsigned long *supported,
582 				 const struct phylink_link_state *state,
583 				 phy_interface_t interface,
584 				 unsigned long *accum_supported,
585 				 unsigned long *accum_advertising)
586 {
587 	__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp_supported);
588 	struct phylink_link_state tmp_state;
589 
590 	linkmode_copy(tmp_supported, supported);
591 
592 	tmp_state = *state;
593 	tmp_state.interface = interface;
594 
595 	if (phy)
596 		tmp_state.rate_matching = phy_get_rate_matching(phy, interface);
597 
598 	if (!phylink_validate_mac_and_pcs(pl, tmp_supported, &tmp_state)) {
599 		phylink_dbg(pl, " interface %u (%s) rate match %s supports %*pbl\n",
600 			    interface, phy_modes(interface),
601 			    phy_rate_matching_to_str(tmp_state.rate_matching),
602 			    __ETHTOOL_LINK_MODE_MASK_NBITS, tmp_supported);
603 
604 		linkmode_or(accum_supported, accum_supported, tmp_supported);
605 		linkmode_or(accum_advertising, accum_advertising,
606 			    tmp_state.advertising);
607 	}
608 }
609 
610 static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy,
611 				 unsigned long *supported,
612 				 struct phylink_link_state *state,
613 				 const unsigned long *interfaces)
614 {
615 	__ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
616 	__ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
617 	int interface;
618 
619 	for_each_set_bit(interface, interfaces, PHY_INTERFACE_MODE_MAX)
620 		phylink_validate_one(pl, phy, supported, state, interface,
621 				     all_s, all_adv);
622 
623 	linkmode_copy(supported, all_s);
624 	linkmode_copy(state->advertising, all_adv);
625 
626 	return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
627 }
628 
629 static int phylink_validate(struct phylink *pl, unsigned long *supported,
630 			    struct phylink_link_state *state)
631 {
632 	const unsigned long *interfaces = pl->config->supported_interfaces;
633 
634 	if (state->interface == PHY_INTERFACE_MODE_NA)
635 		return phylink_validate_mask(pl, NULL, supported, state,
636 					     interfaces);
637 
638 	if (!test_bit(state->interface, interfaces))
639 		return -EINVAL;
640 
641 	return phylink_validate_mac_and_pcs(pl, supported, state);
642 }
643 
644 static void phylink_fill_fixedlink_supported(unsigned long *supported)
645 {
646 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported);
647 	linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, supported);
648 	linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, supported);
649 	linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, supported);
650 	linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, supported);
651 	linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, supported);
652 	linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, supported);
653 	linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, supported);
654 	linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, supported);
655 	linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, supported);
656 	linkmode_set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, supported);
657 	linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, supported);
658 }
659 
660 static int phylink_parse_fixedlink(struct phylink *pl,
661 				   const struct fwnode_handle *fwnode)
662 {
663 	__ETHTOOL_DECLARE_LINK_MODE_MASK(match) = { 0, };
664 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
665 	const struct link_capabilities *c;
666 	struct fwnode_handle *fixed_node;
667 	struct gpio_desc *desc;
668 	u32 speed;
669 	int ret;
670 
671 	fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
672 	if (fixed_node) {
673 		ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
674 
675 		pl->link_config.speed = speed;
676 		pl->link_config.duplex = DUPLEX_HALF;
677 
678 		if (fwnode_property_read_bool(fixed_node, "full-duplex"))
679 			pl->link_config.duplex = DUPLEX_FULL;
680 
681 		/* We treat the "pause" and "asym-pause" terminology as
682 		 * defining the link partner's ability.
683 		 */
684 		if (fwnode_property_read_bool(fixed_node, "pause"))
685 			__set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
686 				  pl->link_config.lp_advertising);
687 		if (fwnode_property_read_bool(fixed_node, "asym-pause"))
688 			__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
689 				  pl->link_config.lp_advertising);
690 
691 		if (ret == 0) {
692 			desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
693 						      GPIOD_IN, "?");
694 
695 			if (!IS_ERR(desc))
696 				pl->link_gpio = desc;
697 			else if (desc == ERR_PTR(-EPROBE_DEFER))
698 				ret = -EPROBE_DEFER;
699 		}
700 		fwnode_handle_put(fixed_node);
701 
702 		if (ret)
703 			return ret;
704 	} else {
705 		u32 prop[5];
706 
707 		ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
708 						     NULL, 0);
709 		if (ret != ARRAY_SIZE(prop)) {
710 			phylink_err(pl, "broken fixed-link?\n");
711 			return -EINVAL;
712 		}
713 
714 		phylink_warn(pl, "%pfw uses deprecated array-style fixed-link binding!\n",
715 			     fwnode);
716 
717 		ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
718 						     prop, ARRAY_SIZE(prop));
719 		if (!ret) {
720 			pl->link_config.duplex = prop[1] ?
721 						DUPLEX_FULL : DUPLEX_HALF;
722 			pl->link_config.speed = prop[2];
723 			if (prop[3])
724 				__set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
725 					  pl->link_config.lp_advertising);
726 			if (prop[4])
727 				__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
728 					  pl->link_config.lp_advertising);
729 		}
730 	}
731 
732 	if (pl->link_config.speed > SPEED_1000 &&
733 	    pl->link_config.duplex != DUPLEX_FULL)
734 		phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
735 			     pl->link_config.speed);
736 
737 	linkmode_zero(pl->supported);
738 	phylink_fill_fixedlink_supported(pl->supported);
739 
740 	linkmode_copy(pl->link_config.advertising, pl->supported);
741 	phylink_validate(pl, pl->supported, &pl->link_config);
742 
743 	c = phy_caps_lookup(pl->link_config.speed, pl->link_config.duplex,
744 			    pl->supported, true);
745 	if (c)
746 		linkmode_and(match, pl->supported, c->linkmodes);
747 
748 	linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, mask);
749 	linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, mask);
750 	linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mask);
751 	linkmode_and(pl->supported, pl->supported, mask);
752 
753 	phylink_set(pl->supported, MII);
754 
755 	if (c) {
756 		linkmode_or(pl->supported, pl->supported, match);
757 		linkmode_or(pl->link_config.lp_advertising,
758 			    pl->link_config.lp_advertising, match);
759 	} else {
760 		phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
761 			     pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
762 			     pl->link_config.speed);
763 	}
764 
765 	linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
766 		     pl->supported);
767 
768 	pl->link_config.link = 1;
769 	pl->link_config.an_complete = 1;
770 
771 	return 0;
772 }
773 
774 static int phylink_parse_mode(struct phylink *pl,
775 			      const struct fwnode_handle *fwnode)
776 {
777 	struct fwnode_handle *dn;
778 	const char *managed;
779 	unsigned long caps;
780 
781 	if (pl->config->default_an_inband)
782 		pl->cfg_link_an_mode = MLO_AN_INBAND;
783 
784 	dn = fwnode_get_named_child_node(fwnode, "fixed-link");
785 	if (dn || fwnode_property_present(fwnode, "fixed-link"))
786 		pl->cfg_link_an_mode = MLO_AN_FIXED;
787 	fwnode_handle_put(dn);
788 
789 	if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
790 	     strcmp(managed, "in-band-status") == 0)) {
791 		if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
792 			phylink_err(pl,
793 				    "can't use both fixed-link and in-band-status\n");
794 			return -EINVAL;
795 		}
796 
797 		pl->cfg_link_an_mode = MLO_AN_INBAND;
798 	}
799 
800 	if (pl->cfg_link_an_mode == MLO_AN_INBAND) {
801 		linkmode_zero(pl->supported);
802 		phylink_set(pl->supported, MII);
803 		phylink_set(pl->supported, Autoneg);
804 		phylink_set(pl->supported, Asym_Pause);
805 		phylink_set(pl->supported, Pause);
806 
807 		switch (pl->link_config.interface) {
808 		case PHY_INTERFACE_MODE_SGMII:
809 		case PHY_INTERFACE_MODE_PSGMII:
810 		case PHY_INTERFACE_MODE_QSGMII:
811 		case PHY_INTERFACE_MODE_QUSGMII:
812 		case PHY_INTERFACE_MODE_RGMII:
813 		case PHY_INTERFACE_MODE_RGMII_ID:
814 		case PHY_INTERFACE_MODE_RGMII_RXID:
815 		case PHY_INTERFACE_MODE_RGMII_TXID:
816 		case PHY_INTERFACE_MODE_RTBI:
817 		case PHY_INTERFACE_MODE_1000BASEX:
818 		case PHY_INTERFACE_MODE_2500BASEX:
819 		case PHY_INTERFACE_MODE_5GBASER:
820 		case PHY_INTERFACE_MODE_25GBASER:
821 		case PHY_INTERFACE_MODE_USXGMII:
822 		case PHY_INTERFACE_MODE_10G_QXGMII:
823 		case PHY_INTERFACE_MODE_10GKR:
824 		case PHY_INTERFACE_MODE_10GBASER:
825 		case PHY_INTERFACE_MODE_XLGMII:
826 		case PHY_INTERFACE_MODE_50GBASER:
827 		case PHY_INTERFACE_MODE_LAUI:
828 		case PHY_INTERFACE_MODE_100GBASEP:
829 			caps = ~(MAC_SYM_PAUSE | MAC_ASYM_PAUSE);
830 			caps = phylink_get_capabilities(pl->link_config.interface, caps,
831 							RATE_MATCH_NONE);
832 			phylink_caps_to_linkmodes(pl->supported, caps);
833 			break;
834 
835 		default:
836 			phylink_err(pl,
837 				    "incorrect link mode %s for in-band status\n",
838 				    phy_modes(pl->link_config.interface));
839 			return -EINVAL;
840 		}
841 
842 		linkmode_copy(pl->link_config.advertising, pl->supported);
843 
844 		if (phylink_validate(pl, pl->supported, &pl->link_config)) {
845 			phylink_err(pl,
846 				    "failed to validate link configuration for in-band status\n");
847 			return -EINVAL;
848 		}
849 	}
850 
851 	return 0;
852 }
853 
854 static void phylink_apply_manual_flow(struct phylink *pl,
855 				      struct phylink_link_state *state)
856 {
857 	/* If autoneg is disabled, pause AN is also disabled */
858 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
859 			       state->advertising))
860 		state->pause &= ~MLO_PAUSE_AN;
861 
862 	/* Manual configuration of pause modes */
863 	if (!(pl->link_config.pause & MLO_PAUSE_AN))
864 		state->pause = pl->link_config.pause;
865 }
866 
867 static void phylink_resolve_an_pause(struct phylink_link_state *state)
868 {
869 	bool tx_pause, rx_pause;
870 
871 	if (state->duplex == DUPLEX_FULL) {
872 		linkmode_resolve_pause(state->advertising,
873 				       state->lp_advertising,
874 				       &tx_pause, &rx_pause);
875 		if (tx_pause)
876 			state->pause |= MLO_PAUSE_TX;
877 		if (rx_pause)
878 			state->pause |= MLO_PAUSE_RX;
879 	}
880 }
881 
882 static unsigned int phylink_pcs_inband_caps(struct phylink_pcs *pcs,
883 				    phy_interface_t interface)
884 {
885 	if (pcs && pcs->ops->pcs_inband_caps)
886 		return pcs->ops->pcs_inband_caps(pcs, interface);
887 
888 	return 0;
889 }
890 
891 static void phylink_pcs_pre_config(struct phylink_pcs *pcs,
892 				   phy_interface_t interface)
893 {
894 	if (pcs && pcs->ops->pcs_pre_config)
895 		pcs->ops->pcs_pre_config(pcs, interface);
896 }
897 
898 static int phylink_pcs_post_config(struct phylink_pcs *pcs,
899 				   phy_interface_t interface)
900 {
901 	int err = 0;
902 
903 	if (pcs && pcs->ops->pcs_post_config)
904 		err = pcs->ops->pcs_post_config(pcs, interface);
905 
906 	return err;
907 }
908 
909 static void phylink_pcs_disable(struct phylink_pcs *pcs)
910 {
911 	if (pcs && pcs->ops->pcs_disable)
912 		pcs->ops->pcs_disable(pcs);
913 }
914 
915 static int phylink_pcs_enable(struct phylink_pcs *pcs)
916 {
917 	int err = 0;
918 
919 	if (pcs && pcs->ops->pcs_enable)
920 		err = pcs->ops->pcs_enable(pcs);
921 
922 	return err;
923 }
924 
925 static int phylink_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
926 			      const struct phylink_link_state *state,
927 			      bool permit_pause_to_mac)
928 {
929 	if (!pcs)
930 		return 0;
931 
932 	return pcs->ops->pcs_config(pcs, neg_mode, state->interface,
933 				    state->advertising, permit_pause_to_mac);
934 }
935 
936 static void phylink_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
937 				phy_interface_t interface, int speed,
938 				int duplex)
939 {
940 	if (pcs && pcs->ops->pcs_link_up)
941 		pcs->ops->pcs_link_up(pcs, neg_mode, interface, speed, duplex);
942 }
943 
944 static void phylink_pcs_disable_eee(struct phylink_pcs *pcs)
945 {
946 	if (pcs && pcs->ops->pcs_disable_eee)
947 		pcs->ops->pcs_disable_eee(pcs);
948 }
949 
950 static void phylink_pcs_enable_eee(struct phylink_pcs *pcs)
951 {
952 	if (pcs && pcs->ops->pcs_enable_eee)
953 		pcs->ops->pcs_enable_eee(pcs);
954 }
955 
956 /* Query inband for a specific interface mode, asking the MAC for the
957  * PCS which will be used to handle the interface mode.
958  */
959 static unsigned int phylink_inband_caps(struct phylink *pl,
960 					 phy_interface_t interface)
961 {
962 	struct phylink_pcs *pcs;
963 
964 	if (!pl->mac_ops->mac_select_pcs)
965 		return 0;
966 
967 	pcs = pl->mac_ops->mac_select_pcs(pl->config, interface);
968 	if (!pcs)
969 		return 0;
970 
971 	return phylink_pcs_inband_caps(pcs, interface);
972 }
973 
974 static void phylink_pcs_poll_stop(struct phylink *pl)
975 {
976 	if (pl->cfg_link_an_mode == MLO_AN_INBAND)
977 		timer_delete(&pl->link_poll);
978 }
979 
980 static void phylink_pcs_poll_start(struct phylink *pl)
981 {
982 	if (pl->pcs && pl->pcs->poll && pl->cfg_link_an_mode == MLO_AN_INBAND)
983 		mod_timer(&pl->link_poll, jiffies + HZ);
984 }
985 
986 int phylink_pcs_pre_init(struct phylink *pl, struct phylink_pcs *pcs)
987 {
988 	int ret = 0;
989 
990 	/* Signal to PCS driver that MAC requires RX clock for init */
991 	if (pl->config->mac_requires_rxc)
992 		pcs->rxc_always_on = true;
993 
994 	if (pcs->ops->pcs_pre_init)
995 		ret = pcs->ops->pcs_pre_init(pcs);
996 
997 	return ret;
998 }
999 EXPORT_SYMBOL_GPL(phylink_pcs_pre_init);
1000 
1001 static void phylink_mac_config(struct phylink *pl,
1002 			       const struct phylink_link_state *state)
1003 {
1004 	struct phylink_link_state st = *state;
1005 
1006 	/* Stop drivers incorrectly using these */
1007 	linkmode_zero(st.lp_advertising);
1008 	st.speed = SPEED_UNKNOWN;
1009 	st.duplex = DUPLEX_UNKNOWN;
1010 	st.an_complete = false;
1011 	st.link = false;
1012 
1013 	phylink_dbg(pl,
1014 		    "%s: mode=%s/%s/%s adv=%*pb pause=%02x\n",
1015 		    __func__, phylink_an_mode_str(pl->act_link_an_mode),
1016 		    phy_modes(st.interface),
1017 		    phy_rate_matching_to_str(st.rate_matching),
1018 		    __ETHTOOL_LINK_MODE_MASK_NBITS, st.advertising,
1019 		    st.pause);
1020 
1021 	pl->mac_ops->mac_config(pl->config, pl->act_link_an_mode, &st);
1022 }
1023 
1024 static void phylink_pcs_an_restart(struct phylink *pl)
1025 {
1026 	if (pl->pcs && linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1027 					 pl->link_config.advertising) &&
1028 	    phy_interface_mode_is_8023z(pl->link_config.interface) &&
1029 	    phylink_autoneg_inband(pl->act_link_an_mode))
1030 		pl->pcs->ops->pcs_an_restart(pl->pcs);
1031 }
1032 
1033 enum inband_type {
1034 	INBAND_NONE,
1035 	INBAND_CISCO_SGMII,
1036 	INBAND_BASEX,
1037 };
1038 
1039 static enum inband_type phylink_get_inband_type(phy_interface_t interface)
1040 {
1041 	switch (interface) {
1042 	case PHY_INTERFACE_MODE_SGMII:
1043 	case PHY_INTERFACE_MODE_QSGMII:
1044 	case PHY_INTERFACE_MODE_QUSGMII:
1045 	case PHY_INTERFACE_MODE_USXGMII:
1046 	case PHY_INTERFACE_MODE_10G_QXGMII:
1047 		/* These protocols are designed for use with a PHY which
1048 		 * communicates its negotiation result back to the MAC via
1049 		 * inband communication. Note: there exist PHYs that run
1050 		 * with SGMII but do not send the inband data.
1051 		 */
1052 		return INBAND_CISCO_SGMII;
1053 
1054 	case PHY_INTERFACE_MODE_1000BASEX:
1055 	case PHY_INTERFACE_MODE_2500BASEX:
1056 		/* 1000base-X is designed for use media-side for Fibre
1057 		 * connections, and thus the Autoneg bit needs to be
1058 		 * taken into account. We also do this for 2500base-X
1059 		 * as well, but drivers may not support this, so may
1060 		 * need to override this.
1061 		 */
1062 		return INBAND_BASEX;
1063 
1064 	default:
1065 		return INBAND_NONE;
1066 	}
1067 }
1068 
1069 /**
1070  * phylink_pcs_neg_mode() - helper to determine PCS inband mode
1071  * @pl: a pointer to a &struct phylink returned from phylink_create()
1072  * @pcs: a pointer to &struct phylink_pcs
1073  * @interface: interface mode to be used
1074  * @advertising: adertisement ethtool link mode mask
1075  *
1076  * Determines the negotiation mode to be used by the PCS, and returns
1077  * one of:
1078  *
1079  * - %PHYLINK_PCS_NEG_NONE: interface mode does not support inband
1080  * - %PHYLINK_PCS_NEG_OUTBAND: an out of band mode (e.g. reading the PHY)
1081  *   will be used.
1082  * - %PHYLINK_PCS_NEG_INBAND_DISABLED: inband mode selected but autoneg
1083  *   disabled
1084  * - %PHYLINK_PCS_NEG_INBAND_ENABLED: inband mode selected and autoneg enabled
1085  *
1086  * Note: this is for cases where the PCS itself is involved in negotiation
1087  * (e.g. Clause 37, SGMII and similar) not Clause 73.
1088  */
1089 static void phylink_pcs_neg_mode(struct phylink *pl, struct phylink_pcs *pcs,
1090 				 phy_interface_t interface,
1091 				 const unsigned long *advertising)
1092 {
1093 	unsigned int pcs_ib_caps = 0;
1094 	unsigned int phy_ib_caps = 0;
1095 	unsigned int neg_mode, mode;
1096 	enum inband_type type;
1097 
1098 	type = phylink_get_inband_type(interface);
1099 	if (type == INBAND_NONE) {
1100 		pl->pcs_neg_mode = PHYLINK_PCS_NEG_NONE;
1101 		pl->act_link_an_mode = pl->req_link_an_mode;
1102 		return;
1103 	}
1104 
1105 	mode = pl->req_link_an_mode;
1106 
1107 	pl->phy_ib_mode = 0;
1108 
1109 	if (pcs)
1110 		pcs_ib_caps = phylink_pcs_inband_caps(pcs, interface);
1111 
1112 	if (pl->phydev)
1113 		phy_ib_caps = phy_inband_caps(pl->phydev, interface);
1114 
1115 	phylink_dbg(pl, "interface %s inband modes: pcs=%02x phy=%02x\n",
1116 		    phy_modes(interface), pcs_ib_caps, phy_ib_caps);
1117 
1118 	if (!phylink_autoneg_inband(mode)) {
1119 		bool pcs_ib_only = false;
1120 		bool phy_ib_only = false;
1121 
1122 		if (pcs_ib_caps && pcs_ib_caps != LINK_INBAND_DISABLE) {
1123 			/* PCS supports reporting in-band capabilities, and
1124 			 * supports more than disable mode.
1125 			 */
1126 			if (pcs_ib_caps & LINK_INBAND_DISABLE)
1127 				neg_mode = PHYLINK_PCS_NEG_OUTBAND;
1128 			else if (pcs_ib_caps & LINK_INBAND_ENABLE)
1129 				pcs_ib_only = true;
1130 		}
1131 
1132 		if (phy_ib_caps && phy_ib_caps != LINK_INBAND_DISABLE) {
1133 			/* PHY supports in-band capabilities, and supports
1134 			 * more than disable mode.
1135 			 */
1136 			if (phy_ib_caps & LINK_INBAND_DISABLE)
1137 				pl->phy_ib_mode = LINK_INBAND_DISABLE;
1138 			else if (phy_ib_caps & LINK_INBAND_BYPASS)
1139 				pl->phy_ib_mode = LINK_INBAND_BYPASS;
1140 			else if (phy_ib_caps & LINK_INBAND_ENABLE)
1141 				phy_ib_only = true;
1142 		}
1143 
1144 		/* If either the PCS or PHY requires inband to be enabled,
1145 		 * this is an invalid configuration. Provide a diagnostic
1146 		 * message for this case, but don't try to force the issue.
1147 		 */
1148 		if (pcs_ib_only || phy_ib_only)
1149 			phylink_warn(pl,
1150 				     "firmware wants %s mode, but %s%s%s requires inband\n",
1151 				     phylink_an_mode_str(mode),
1152 				     pcs_ib_only ? "PCS" : "",
1153 				     pcs_ib_only && phy_ib_only ? " and " : "",
1154 				     phy_ib_only ? "PHY" : "");
1155 
1156 		neg_mode = PHYLINK_PCS_NEG_OUTBAND;
1157 	} else if (type == INBAND_CISCO_SGMII || pl->phydev) {
1158 		/* For SGMII modes which are designed to be used with PHYs, or
1159 		 * Base-X with a PHY, we try to use in-band mode where-ever
1160 		 * possible. However, there are some PHYs e.g. BCM84881 which
1161 		 * do not support in-band.
1162 		 */
1163 		const unsigned int inband_ok = LINK_INBAND_ENABLE |
1164 					       LINK_INBAND_BYPASS;
1165 		const unsigned int outband_ok = LINK_INBAND_DISABLE |
1166 						LINK_INBAND_BYPASS;
1167 		/* PCS	PHY
1168 		 * D E	D E
1169 		 * 0 0  0 0	no information			inband enabled
1170 		 * 1 0  0 0	pcs doesn't support		outband
1171 		 * 0 1  0 0	pcs required			inband enabled
1172 		 * 1 1  0 0	pcs optional			inband enabled
1173 		 * 0 0  1 0	phy doesn't support		outband
1174 		 * 1 0  1 0	pcs+phy doesn't support		outband
1175 		 * 0 1  1 0	pcs required, phy doesn't support, invalid
1176 		 * 1 1  1 0	pcs optional, phy doesn't support, outband
1177 		 * 0 0  0 1	phy required			inband enabled
1178 		 * 1 0  0 1	pcs doesn't support, phy required, invalid
1179 		 * 0 1  0 1	pcs+phy required		inband enabled
1180 		 * 1 1  0 1	pcs optional, phy required	inband enabled
1181 		 * 0 0  1 1	phy optional			inband enabled
1182 		 * 1 0  1 1	pcs doesn't support, phy optional, outband
1183 		 * 0 1  1 1	pcs required, phy optional	inband enabled
1184 		 * 1 1  1 1	pcs+phy optional		inband enabled
1185 		 */
1186 		if ((!pcs_ib_caps || pcs_ib_caps & inband_ok) &&
1187 		    (!phy_ib_caps || phy_ib_caps & inband_ok)) {
1188 			/* In-band supported or unknown at both ends. Enable
1189 			 * in-band mode with or without bypass at the PHY.
1190 			 */
1191 			if (phy_ib_caps & LINK_INBAND_ENABLE)
1192 				pl->phy_ib_mode = LINK_INBAND_ENABLE;
1193 			else if (phy_ib_caps & LINK_INBAND_BYPASS)
1194 				pl->phy_ib_mode = LINK_INBAND_BYPASS;
1195 
1196 			neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
1197 		} else if ((!pcs_ib_caps || pcs_ib_caps & outband_ok) &&
1198 			   (!phy_ib_caps || phy_ib_caps & outband_ok)) {
1199 			/* Either in-band not supported at at least one end.
1200 			 * In-band bypass at the other end is possible.
1201 			 */
1202 			if (phy_ib_caps & LINK_INBAND_DISABLE)
1203 				pl->phy_ib_mode = LINK_INBAND_DISABLE;
1204 			else if (phy_ib_caps & LINK_INBAND_BYPASS)
1205 				pl->phy_ib_mode = LINK_INBAND_BYPASS;
1206 
1207 			neg_mode = PHYLINK_PCS_NEG_OUTBAND;
1208 			if (pl->phydev)
1209 				mode = MLO_AN_PHY;
1210 		} else {
1211 			/* invalid */
1212 			phylink_warn(pl, "%s: incompatible in-band capabilities, trying in-band",
1213 				     phy_modes(interface));
1214 			neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
1215 		}
1216 	} else {
1217 		/* For Base-X without a PHY */
1218 		if (pcs_ib_caps == LINK_INBAND_DISABLE)
1219 			/* If the PCS doesn't support inband, then inband must
1220 			 * be disabled.
1221 			 */
1222 			neg_mode = PHYLINK_PCS_NEG_INBAND_DISABLED;
1223 		else if (pcs_ib_caps == LINK_INBAND_ENABLE)
1224 			/* If the PCS requires inband, then inband must always
1225 			 * be enabled.
1226 			 */
1227 			neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
1228 		else if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1229 					   advertising))
1230 			neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
1231 		else
1232 			neg_mode = PHYLINK_PCS_NEG_INBAND_DISABLED;
1233 	}
1234 
1235 	pl->pcs_neg_mode = neg_mode;
1236 	pl->act_link_an_mode = mode;
1237 }
1238 
1239 static void phylink_major_config(struct phylink *pl, bool restart,
1240 				  const struct phylink_link_state *state)
1241 {
1242 	struct phylink_pcs *pcs = NULL;
1243 	bool pcs_changed = false;
1244 	unsigned int rate_kbd;
1245 	int err;
1246 
1247 	phylink_dbg(pl, "major config, requested %s/%s\n",
1248 		    phylink_an_mode_str(pl->req_link_an_mode),
1249 		    phy_modes(state->interface));
1250 
1251 	pl->major_config_failed = false;
1252 
1253 	if (pl->mac_ops->mac_select_pcs) {
1254 		pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
1255 		if (IS_ERR(pcs)) {
1256 			phylink_err(pl,
1257 				    "mac_select_pcs unexpectedly failed: %pe\n",
1258 				    pcs);
1259 
1260 			pl->major_config_failed = true;
1261 			return;
1262 		}
1263 
1264 		pcs_changed = pl->pcs != pcs;
1265 	}
1266 
1267 	phylink_pcs_neg_mode(pl, pcs, state->interface, state->advertising);
1268 
1269 	phylink_dbg(pl, "major config, active %s/%s/%s\n",
1270 		    phylink_an_mode_str(pl->act_link_an_mode),
1271 		    phylink_pcs_mode_str(pl->pcs_neg_mode),
1272 		    phy_modes(state->interface));
1273 
1274 	phylink_pcs_poll_stop(pl);
1275 
1276 	if (pl->mac_ops->mac_prepare) {
1277 		err = pl->mac_ops->mac_prepare(pl->config, pl->act_link_an_mode,
1278 					       state->interface);
1279 		if (err < 0) {
1280 			phylink_err(pl, "mac_prepare failed: %pe\n",
1281 				    ERR_PTR(err));
1282 			pl->major_config_failed = true;
1283 			return;
1284 		}
1285 	}
1286 
1287 	/* If we have a new PCS, switch to the new PCS after preparing the MAC
1288 	 * for the change.
1289 	 */
1290 	if (pcs_changed) {
1291 		phylink_pcs_disable(pl->pcs);
1292 
1293 		if (pl->pcs)
1294 			pl->pcs->phylink = NULL;
1295 
1296 		pcs->phylink = pl;
1297 
1298 		pl->pcs = pcs;
1299 	}
1300 
1301 	if (pl->pcs)
1302 		phylink_pcs_pre_config(pl->pcs, state->interface);
1303 
1304 	phylink_mac_config(pl, state);
1305 
1306 	if (pl->pcs) {
1307 		err = phylink_pcs_post_config(pl->pcs, state->interface);
1308 		if (err < 0) {
1309 			phylink_err(pl, "pcs_post_config failed: %pe\n",
1310 				    ERR_PTR(err));
1311 
1312 			pl->major_config_failed = true;
1313 		}
1314 	}
1315 
1316 	if (pl->pcs_state == PCS_STATE_STARTING || pcs_changed)
1317 		phylink_pcs_enable(pl->pcs);
1318 
1319 	err = phylink_pcs_config(pl->pcs, pl->pcs_neg_mode, state,
1320 				 !!(pl->link_config.pause & MLO_PAUSE_AN));
1321 	if (err < 0) {
1322 		phylink_err(pl, "pcs_config failed: %pe\n", ERR_PTR(err));
1323 		pl->major_config_failed = true;
1324 	} else if (err > 0) {
1325 		restart = true;
1326 	}
1327 
1328 	if (restart)
1329 		phylink_pcs_an_restart(pl);
1330 
1331 	if (pl->mac_ops->mac_finish) {
1332 		err = pl->mac_ops->mac_finish(pl->config, pl->act_link_an_mode,
1333 					      state->interface);
1334 		if (err < 0) {
1335 			phylink_err(pl, "mac_finish failed: %pe\n",
1336 				    ERR_PTR(err));
1337 
1338 			pl->major_config_failed = true;
1339 		}
1340 	}
1341 
1342 	if (pl->phydev && pl->phy_ib_mode) {
1343 		err = phy_config_inband(pl->phydev, pl->phy_ib_mode);
1344 		if (err < 0) {
1345 			phylink_err(pl, "phy_config_inband: %pe\n",
1346 				    ERR_PTR(err));
1347 
1348 			pl->major_config_failed = true;
1349 		}
1350 	}
1351 
1352 	if (pl->sfp_bus) {
1353 		rate_kbd = phylink_interface_signal_rate(state->interface);
1354 		if (rate_kbd)
1355 			sfp_upstream_set_signal_rate(pl->sfp_bus, rate_kbd);
1356 	}
1357 
1358 	phylink_pcs_poll_start(pl);
1359 }
1360 
1361 /*
1362  * Reconfigure for a change of inband advertisement.
1363  * If we have a separate PCS, we only need to call its pcs_config() method,
1364  * and then restart AN if it indicates something changed. Otherwise, we do
1365  * the full MAC reconfiguration.
1366  */
1367 static int phylink_change_inband_advert(struct phylink *pl)
1368 {
1369 	int ret;
1370 
1371 	if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
1372 		return 0;
1373 
1374 	phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
1375 		    phylink_an_mode_str(pl->req_link_an_mode),
1376 		    phy_modes(pl->link_config.interface),
1377 		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
1378 		    pl->link_config.pause);
1379 
1380 	/* Recompute the PCS neg mode */
1381 	phylink_pcs_neg_mode(pl, pl->pcs, pl->link_config.interface,
1382 			     pl->link_config.advertising);
1383 
1384 	/* Modern PCS-based method; update the advert at the PCS, and
1385 	 * restart negotiation if the pcs_config() helper indicates that
1386 	 * the programmed advertisement has changed.
1387 	 */
1388 	ret = phylink_pcs_config(pl->pcs, pl->pcs_neg_mode, &pl->link_config,
1389 				 !!(pl->link_config.pause & MLO_PAUSE_AN));
1390 	if (ret < 0)
1391 		return ret;
1392 
1393 	if (ret > 0)
1394 		phylink_pcs_an_restart(pl);
1395 
1396 	return 0;
1397 }
1398 
1399 static void phylink_mac_pcs_get_state(struct phylink *pl,
1400 				      struct phylink_link_state *state)
1401 {
1402 	struct phylink_pcs *pcs;
1403 	bool autoneg;
1404 
1405 	linkmode_copy(state->advertising, pl->link_config.advertising);
1406 	linkmode_zero(state->lp_advertising);
1407 	state->interface = pl->link_config.interface;
1408 	state->rate_matching = pl->link_config.rate_matching;
1409 	state->an_complete = 0;
1410 	state->link = 1;
1411 
1412 	autoneg = pl->pcs_neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED;
1413 	if (autoneg) {
1414 		state->speed = SPEED_UNKNOWN;
1415 		state->duplex = DUPLEX_UNKNOWN;
1416 		state->pause = MLO_PAUSE_NONE;
1417 	} else {
1418 		state->speed =  pl->link_config.speed;
1419 		state->duplex = pl->link_config.duplex;
1420 		state->pause = pl->link_config.pause;
1421 	}
1422 
1423 	pcs = pl->pcs;
1424 	if (pcs)
1425 		pcs->ops->pcs_get_state(pcs, pl->pcs_neg_mode, state);
1426 	else
1427 		state->link = 0;
1428 }
1429 
1430 /* The fixed state is... fixed except for the link state,
1431  * which may be determined by a GPIO or a callback.
1432  */
1433 static void phylink_get_fixed_state(struct phylink *pl,
1434 				    struct phylink_link_state *state)
1435 {
1436 	*state = pl->link_config;
1437 	if (pl->config->get_fixed_state)
1438 		pl->config->get_fixed_state(pl->config, state);
1439 	else if (pl->link_gpio)
1440 		state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
1441 
1442 	state->pause = MLO_PAUSE_NONE;
1443 	phylink_resolve_an_pause(state);
1444 }
1445 
1446 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
1447 {
1448 	struct phylink_link_state link_state;
1449 	struct phy_device *phy = pl->phydev;
1450 
1451 	switch (pl->req_link_an_mode) {
1452 	case MLO_AN_PHY:
1453 		link_state = pl->phy_state;
1454 		break;
1455 
1456 	case MLO_AN_FIXED:
1457 		phylink_get_fixed_state(pl, &link_state);
1458 		break;
1459 
1460 	case MLO_AN_INBAND:
1461 		link_state = pl->link_config;
1462 		if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
1463 			link_state.pause = MLO_PAUSE_NONE;
1464 		break;
1465 
1466 	default: /* can't happen */
1467 		return;
1468 	}
1469 
1470 	link_state.link = false;
1471 
1472 	phylink_apply_manual_flow(pl, &link_state);
1473 	if (phy)
1474 		mutex_lock(&phy->lock);
1475 	phylink_major_config(pl, force_restart, &link_state);
1476 	if (phy)
1477 		mutex_unlock(&phy->lock);
1478 }
1479 
1480 static const char *phylink_pause_to_str(int pause)
1481 {
1482 	switch (pause & MLO_PAUSE_TXRX_MASK) {
1483 	case MLO_PAUSE_TX | MLO_PAUSE_RX:
1484 		return "rx/tx";
1485 	case MLO_PAUSE_TX:
1486 		return "tx";
1487 	case MLO_PAUSE_RX:
1488 		return "rx";
1489 	default:
1490 		return "off";
1491 	}
1492 }
1493 
1494 static void phylink_deactivate_lpi(struct phylink *pl)
1495 {
1496 	if (pl->mac_enable_tx_lpi) {
1497 		pl->mac_enable_tx_lpi = false;
1498 
1499 		phylink_dbg(pl, "disabling LPI\n");
1500 
1501 		pl->mac_ops->mac_disable_tx_lpi(pl->config);
1502 
1503 		phylink_pcs_disable_eee(pl->pcs);
1504 	}
1505 }
1506 
1507 static void phylink_activate_lpi(struct phylink *pl)
1508 {
1509 	int err;
1510 
1511 	if (!test_bit(pl->cur_interface, pl->config->lpi_interfaces)) {
1512 		phylink_dbg(pl, "MAC does not support LPI with %s\n",
1513 			    phy_modes(pl->cur_interface));
1514 		return;
1515 	}
1516 
1517 	phylink_dbg(pl, "LPI timer %uus, tx clock stop %u\n",
1518 		    pl->mac_tx_lpi_timer, pl->mac_tx_clk_stop);
1519 
1520 	phylink_pcs_enable_eee(pl->pcs);
1521 
1522 	err = pl->mac_ops->mac_enable_tx_lpi(pl->config, pl->mac_tx_lpi_timer,
1523 					     pl->mac_tx_clk_stop);
1524 	if (err) {
1525 		phylink_pcs_disable_eee(pl->pcs);
1526 		phylink_err(pl, "%ps() failed: %pe\n",
1527 			    pl->mac_ops->mac_enable_tx_lpi, ERR_PTR(err));
1528 		return;
1529 	}
1530 
1531 	pl->mac_enable_tx_lpi = true;
1532 }
1533 
1534 static void phylink_link_up(struct phylink *pl,
1535 			    struct phylink_link_state link_state)
1536 {
1537 	struct net_device *ndev = pl->netdev;
1538 	int speed, duplex;
1539 	bool rx_pause;
1540 
1541 	speed = link_state.speed;
1542 	duplex = link_state.duplex;
1543 	rx_pause = !!(link_state.pause & MLO_PAUSE_RX);
1544 
1545 	switch (link_state.rate_matching) {
1546 	case RATE_MATCH_PAUSE:
1547 		/* The PHY is doing rate matchion from the media rate (in
1548 		 * the link_state) to the interface speed, and will send
1549 		 * pause frames to the MAC to limit its transmission speed.
1550 		 */
1551 		speed = phylink_interface_max_speed(link_state.interface);
1552 		duplex = DUPLEX_FULL;
1553 		rx_pause = true;
1554 		break;
1555 
1556 	case RATE_MATCH_CRS:
1557 		/* The PHY is doing rate matchion from the media rate (in
1558 		 * the link_state) to the interface speed, and will cause
1559 		 * collisions to the MAC to limit its transmission speed.
1560 		 */
1561 		speed = phylink_interface_max_speed(link_state.interface);
1562 		duplex = DUPLEX_HALF;
1563 		break;
1564 	}
1565 
1566 	pl->cur_interface = link_state.interface;
1567 
1568 	phylink_pcs_link_up(pl->pcs, pl->pcs_neg_mode, pl->cur_interface, speed,
1569 			    duplex);
1570 
1571 	pl->mac_ops->mac_link_up(pl->config, pl->phydev, pl->act_link_an_mode,
1572 				 pl->cur_interface, speed, duplex,
1573 				 !!(link_state.pause & MLO_PAUSE_TX), rx_pause);
1574 
1575 	if (pl->mac_supports_eee && pl->phy_enable_tx_lpi)
1576 		phylink_activate_lpi(pl);
1577 
1578 	if (ndev)
1579 		netif_carrier_on(ndev);
1580 
1581 	phylink_info(pl,
1582 		     "Link is Up - %s/%s - flow control %s\n",
1583 		     phy_speed_to_str(link_state.speed),
1584 		     phy_duplex_to_str(link_state.duplex),
1585 		     phylink_pause_to_str(link_state.pause));
1586 }
1587 
1588 static void phylink_link_down(struct phylink *pl)
1589 {
1590 	struct net_device *ndev = pl->netdev;
1591 
1592 	if (ndev)
1593 		netif_carrier_off(ndev);
1594 
1595 	phylink_deactivate_lpi(pl);
1596 
1597 	pl->mac_ops->mac_link_down(pl->config, pl->act_link_an_mode,
1598 				   pl->cur_interface);
1599 	phylink_info(pl, "Link is Down\n");
1600 }
1601 
1602 static bool phylink_link_is_up(struct phylink *pl)
1603 {
1604 	return pl->netdev ? netif_carrier_ok(pl->netdev) : pl->old_link_state;
1605 }
1606 
1607 static void phylink_resolve(struct work_struct *w)
1608 {
1609 	struct phylink *pl = container_of(w, struct phylink, resolve);
1610 	struct phylink_link_state link_state;
1611 	bool mac_config = false;
1612 	bool retrigger = false;
1613 	struct phy_device *phy;
1614 	bool cur_link_state;
1615 
1616 	mutex_lock(&pl->phydev_mutex);
1617 	phy = pl->phydev;
1618 	if (phy)
1619 		mutex_lock(&phy->lock);
1620 	mutex_lock(&pl->state_mutex);
1621 	cur_link_state = phylink_link_is_up(pl);
1622 
1623 	if (pl->phylink_disable_state) {
1624 		pl->link_failed = false;
1625 		link_state.link = false;
1626 	} else if (pl->link_failed) {
1627 		link_state.link = false;
1628 		retrigger = true;
1629 	} else if (pl->act_link_an_mode == MLO_AN_FIXED) {
1630 		phylink_get_fixed_state(pl, &link_state);
1631 		mac_config = link_state.link;
1632 	} else if (pl->act_link_an_mode == MLO_AN_PHY) {
1633 		link_state = pl->phy_state;
1634 		mac_config = link_state.link;
1635 	} else {
1636 		phylink_mac_pcs_get_state(pl, &link_state);
1637 
1638 		/* The PCS may have a latching link-fail indicator. If the link
1639 		 * was up, bring the link down and re-trigger the resolve.
1640 		 * Otherwise, re-read the PCS state to get the current status
1641 		 * of the link.
1642 		 */
1643 		if (!link_state.link) {
1644 			if (cur_link_state)
1645 				retrigger = true;
1646 			else
1647 				phylink_mac_pcs_get_state(pl, &link_state);
1648 		}
1649 
1650 		/* If we have a phy, the "up" state is the union of both the
1651 		 * PHY and the MAC
1652 		 */
1653 		if (phy)
1654 			link_state.link &= pl->phy_state.link;
1655 
1656 		/* Only update if the PHY link is up */
1657 		if (phy && pl->phy_state.link) {
1658 			/* If the interface has changed, force a link down
1659 			 * event if the link isn't already down, and re-resolve.
1660 			 */
1661 			if (link_state.interface != pl->phy_state.interface) {
1662 				retrigger = true;
1663 				link_state.link = false;
1664 			}
1665 
1666 			link_state.interface = pl->phy_state.interface;
1667 
1668 			/* If we are doing rate matching, then the link
1669 			 * speed/duplex comes from the PHY
1670 			 */
1671 			if (pl->phy_state.rate_matching) {
1672 				link_state.rate_matching =
1673 					pl->phy_state.rate_matching;
1674 				link_state.speed = pl->phy_state.speed;
1675 				link_state.duplex = pl->phy_state.duplex;
1676 			}
1677 
1678 			/* If we have a PHY, we need to update with the PHY
1679 			 * flow control bits.
1680 			 */
1681 			link_state.pause = pl->phy_state.pause;
1682 			mac_config = true;
1683 		}
1684 	}
1685 
1686 	if (pl->act_link_an_mode != MLO_AN_FIXED)
1687 		phylink_apply_manual_flow(pl, &link_state);
1688 
1689 	if ((mac_config && link_state.interface != pl->link_config.interface) ||
1690 	    pl->force_major_config) {
1691 		/* The interface has changed or a forced major configuration
1692 		 * was requested, so force the link down and then reconfigure.
1693 		 */
1694 		if (cur_link_state) {
1695 			phylink_link_down(pl);
1696 			cur_link_state = false;
1697 		}
1698 		phylink_major_config(pl, false, &link_state);
1699 		pl->link_config.interface = link_state.interface;
1700 		pl->force_major_config = false;
1701 	}
1702 
1703 	/* If configuration of the interface failed, force the link down
1704 	 * until we get a successful configuration.
1705 	 */
1706 	if (pl->major_config_failed)
1707 		link_state.link = false;
1708 
1709 	if (link_state.link != cur_link_state) {
1710 		pl->old_link_state = link_state.link;
1711 		if (!link_state.link)
1712 			phylink_link_down(pl);
1713 		else
1714 			phylink_link_up(pl, link_state);
1715 	}
1716 	if (!link_state.link && retrigger) {
1717 		pl->link_failed = false;
1718 		queue_work(system_power_efficient_wq, &pl->resolve);
1719 	}
1720 	mutex_unlock(&pl->state_mutex);
1721 	if (phy)
1722 		mutex_unlock(&phy->lock);
1723 	mutex_unlock(&pl->phydev_mutex);
1724 }
1725 
1726 static void phylink_run_resolve(struct phylink *pl)
1727 {
1728 	if (!pl->phylink_disable_state)
1729 		queue_work(system_power_efficient_wq, &pl->resolve);
1730 }
1731 
1732 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
1733 {
1734 	unsigned long state = pl->phylink_disable_state;
1735 
1736 	set_bit(bit, &pl->phylink_disable_state);
1737 	if (state == 0) {
1738 		queue_work(system_power_efficient_wq, &pl->resolve);
1739 		flush_work(&pl->resolve);
1740 	}
1741 }
1742 
1743 static void phylink_enable_and_run_resolve(struct phylink *pl, int bit)
1744 {
1745 	clear_bit(bit, &pl->phylink_disable_state);
1746 	phylink_run_resolve(pl);
1747 }
1748 
1749 static void phylink_fixed_poll(struct timer_list *t)
1750 {
1751 	struct phylink *pl = container_of(t, struct phylink, link_poll);
1752 
1753 	mod_timer(t, jiffies + HZ);
1754 
1755 	phylink_run_resolve(pl);
1756 }
1757 
1758 static const struct sfp_upstream_ops sfp_phylink_ops;
1759 
1760 static int phylink_register_sfp(struct phylink *pl,
1761 				const struct fwnode_handle *fwnode)
1762 {
1763 	struct sfp_bus *bus;
1764 	int ret;
1765 
1766 	if (!fwnode)
1767 		return 0;
1768 
1769 	bus = sfp_bus_find_fwnode(fwnode);
1770 	if (IS_ERR(bus)) {
1771 		phylink_err(pl, "unable to attach SFP bus: %pe\n", bus);
1772 		return PTR_ERR(bus);
1773 	}
1774 
1775 	pl->sfp_bus = bus;
1776 
1777 	ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
1778 	sfp_bus_put(bus);
1779 
1780 	return ret;
1781 }
1782 
1783 /**
1784  * phylink_set_fixed_link() - set the fixed link
1785  * @pl: a pointer to a &struct phylink returned from phylink_create()
1786  * @state: a pointer to a struct phylink_link_state.
1787  *
1788  * This function is used when the link parameters are known and do not change,
1789  * making it suitable for certain types of network connections.
1790  *
1791  * Returns: zero on success or negative error code.
1792  */
1793 int phylink_set_fixed_link(struct phylink *pl,
1794 			   const struct phylink_link_state *state)
1795 {
1796 	const struct link_capabilities *c;
1797 	unsigned long *adv;
1798 
1799 	if (pl->cfg_link_an_mode != MLO_AN_PHY || !state ||
1800 	    !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
1801 		return -EINVAL;
1802 
1803 	c = phy_caps_lookup(state->speed, state->duplex,
1804 			    pl->supported, true);
1805 	if (!c)
1806 		return -EINVAL;
1807 
1808 	adv = pl->link_config.advertising;
1809 	linkmode_and(adv, pl->supported, c->linkmodes);
1810 	linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv);
1811 
1812 	pl->link_config.speed = state->speed;
1813 	pl->link_config.duplex = state->duplex;
1814 	pl->link_config.link = 1;
1815 	pl->link_config.an_complete = 1;
1816 
1817 	pl->cfg_link_an_mode = MLO_AN_FIXED;
1818 	pl->req_link_an_mode = pl->cfg_link_an_mode;
1819 
1820 	return 0;
1821 }
1822 EXPORT_SYMBOL_GPL(phylink_set_fixed_link);
1823 
1824 /**
1825  * phylink_create() - create a phylink instance
1826  * @config: a pointer to the target &struct phylink_config
1827  * @fwnode: a pointer to a &struct fwnode_handle describing the network
1828  *	interface
1829  * @iface: the desired link mode defined by &typedef phy_interface_t
1830  * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
1831  *
1832  * Create a new phylink instance, and parse the link parameters found in @np.
1833  * This will parse in-band modes, fixed-link or SFP configuration.
1834  *
1835  * Note: the rtnl lock must not be held when calling this function.
1836  *
1837  * Returns a pointer to a &struct phylink, or an error-pointer value. Users
1838  * must use IS_ERR() to check for errors from this function.
1839  */
1840 struct phylink *phylink_create(struct phylink_config *config,
1841 			       const struct fwnode_handle *fwnode,
1842 			       phy_interface_t iface,
1843 			       const struct phylink_mac_ops *mac_ops)
1844 {
1845 	struct phylink *pl;
1846 	int ret;
1847 
1848 	/* Validate the supplied configuration */
1849 	if (phy_interface_empty(config->supported_interfaces)) {
1850 		dev_err(config->dev,
1851 			"phylink: error: empty supported_interfaces\n");
1852 		return ERR_PTR(-EINVAL);
1853 	}
1854 
1855 	pl = kzalloc(sizeof(*pl), GFP_KERNEL);
1856 	if (!pl)
1857 		return ERR_PTR(-ENOMEM);
1858 
1859 	mutex_init(&pl->phydev_mutex);
1860 	mutex_init(&pl->state_mutex);
1861 	INIT_WORK(&pl->resolve, phylink_resolve);
1862 
1863 	pl->config = config;
1864 	if (config->type == PHYLINK_NETDEV) {
1865 		pl->netdev = to_net_dev(config->dev);
1866 		netif_carrier_off(pl->netdev);
1867 	} else if (config->type == PHYLINK_DEV) {
1868 		pl->dev = config->dev;
1869 	} else {
1870 		kfree(pl);
1871 		return ERR_PTR(-EINVAL);
1872 	}
1873 
1874 	pl->mac_supports_eee_ops = phylink_mac_implements_lpi(mac_ops);
1875 	pl->mac_supports_eee = pl->mac_supports_eee_ops &&
1876 			       pl->config->lpi_capabilities &&
1877 			       !phy_interface_empty(pl->config->lpi_interfaces);
1878 
1879 	/* Set the default EEE configuration */
1880 	pl->eee_cfg.eee_enabled = pl->config->eee_enabled_default;
1881 	pl->eee_cfg.tx_lpi_enabled = pl->eee_cfg.eee_enabled;
1882 	pl->eee_cfg.tx_lpi_timer = pl->config->lpi_timer_default;
1883 
1884 	pl->phy_state.interface = iface;
1885 	pl->link_interface = iface;
1886 	if (iface == PHY_INTERFACE_MODE_MOCA)
1887 		pl->link_port = PORT_BNC;
1888 	else
1889 		pl->link_port = PORT_MII;
1890 	pl->link_config.interface = iface;
1891 	pl->link_config.pause = MLO_PAUSE_AN;
1892 	pl->link_config.speed = SPEED_UNKNOWN;
1893 	pl->link_config.duplex = DUPLEX_UNKNOWN;
1894 	pl->pcs_state = PCS_STATE_DOWN;
1895 	pl->mac_ops = mac_ops;
1896 	__set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1897 	timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
1898 
1899 	linkmode_fill(pl->supported);
1900 	linkmode_copy(pl->link_config.advertising, pl->supported);
1901 	phylink_validate(pl, pl->supported, &pl->link_config);
1902 
1903 	ret = phylink_parse_mode(pl, fwnode);
1904 	if (ret < 0) {
1905 		kfree(pl);
1906 		return ERR_PTR(ret);
1907 	}
1908 
1909 	if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
1910 		ret = phylink_parse_fixedlink(pl, fwnode);
1911 		if (ret < 0) {
1912 			kfree(pl);
1913 			return ERR_PTR(ret);
1914 		}
1915 	}
1916 
1917 	pl->req_link_an_mode = pl->cfg_link_an_mode;
1918 
1919 	ret = phylink_register_sfp(pl, fwnode);
1920 	if (ret < 0) {
1921 		kfree(pl);
1922 		return ERR_PTR(ret);
1923 	}
1924 
1925 	return pl;
1926 }
1927 EXPORT_SYMBOL_GPL(phylink_create);
1928 
1929 /**
1930  * phylink_destroy() - cleanup and destroy the phylink instance
1931  * @pl: a pointer to a &struct phylink returned from phylink_create()
1932  *
1933  * Destroy a phylink instance. Any PHY that has been attached must have been
1934  * cleaned up via phylink_disconnect_phy() prior to calling this function.
1935  *
1936  * Note: the rtnl lock must not be held when calling this function.
1937  */
1938 void phylink_destroy(struct phylink *pl)
1939 {
1940 	sfp_bus_del_upstream(pl->sfp_bus);
1941 	if (pl->link_gpio)
1942 		gpiod_put(pl->link_gpio);
1943 
1944 	cancel_work_sync(&pl->resolve);
1945 	kfree(pl);
1946 }
1947 EXPORT_SYMBOL_GPL(phylink_destroy);
1948 
1949 /**
1950  * phylink_expects_phy() - Determine if phylink expects a phy to be attached
1951  * @pl: a pointer to a &struct phylink returned from phylink_create()
1952  *
1953  * When using fixed-link mode, or in-band mode with 1000base-X or 2500base-X,
1954  * no PHY is needed.
1955  *
1956  * Returns true if phylink will be expecting a PHY.
1957  */
1958 bool phylink_expects_phy(struct phylink *pl)
1959 {
1960 	if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1961 	    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1962 	     phy_interface_mode_is_8023z(pl->link_interface)))
1963 		return false;
1964 	return true;
1965 }
1966 EXPORT_SYMBOL_GPL(phylink_expects_phy);
1967 
1968 static void phylink_phy_change(struct phy_device *phydev, bool up)
1969 {
1970 	struct phylink *pl = phydev->phylink;
1971 	bool tx_pause, rx_pause;
1972 
1973 	phy_get_pause(phydev, &tx_pause, &rx_pause);
1974 
1975 	mutex_lock(&pl->state_mutex);
1976 	pl->phy_state.speed = phydev->speed;
1977 	pl->phy_state.duplex = phydev->duplex;
1978 	pl->phy_state.rate_matching = phydev->rate_matching;
1979 	pl->phy_state.pause = MLO_PAUSE_NONE;
1980 	if (tx_pause)
1981 		pl->phy_state.pause |= MLO_PAUSE_TX;
1982 	if (rx_pause)
1983 		pl->phy_state.pause |= MLO_PAUSE_RX;
1984 	pl->phy_state.interface = phydev->interface;
1985 	pl->phy_state.link = up;
1986 	if (!up)
1987 		pl->link_failed = true;
1988 
1989 	/* Get the LPI state from phylib */
1990 	pl->phy_enable_tx_lpi = phydev->enable_tx_lpi;
1991 	pl->mac_tx_lpi_timer = phydev->eee_cfg.tx_lpi_timer;
1992 	mutex_unlock(&pl->state_mutex);
1993 
1994 	phylink_run_resolve(pl);
1995 
1996 	phylink_dbg(pl, "phy link %s %s/%s/%s/%s/%s/%slpi\n",
1997 		    up ? "up" : "down",
1998 		    phy_modes(phydev->interface),
1999 		    phy_speed_to_str(phydev->speed),
2000 		    phy_duplex_to_str(phydev->duplex),
2001 		    phy_rate_matching_to_str(phydev->rate_matching),
2002 		    phylink_pause_to_str(pl->phy_state.pause),
2003 		    phydev->enable_tx_lpi ? "" : "no");
2004 }
2005 
2006 static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
2007 				unsigned long *supported,
2008 				struct phylink_link_state *state)
2009 {
2010 	DECLARE_PHY_INTERFACE_MASK(interfaces);
2011 
2012 	/* If the PHY provides a bitmap of the interfaces it will be using
2013 	 * depending on the negotiated media speeds, use this to validate
2014 	 * which ethtool link modes can be used.
2015 	 */
2016 	if (!phy_interface_empty(phy->possible_interfaces)) {
2017 		/* We only care about the union of the PHY's interfaces and
2018 		 * those which the host supports.
2019 		 */
2020 		phy_interface_and(interfaces, phy->possible_interfaces,
2021 				  pl->config->supported_interfaces);
2022 
2023 		if (phy_interface_empty(interfaces)) {
2024 			phylink_err(pl, "PHY has no common interfaces\n");
2025 			return -EINVAL;
2026 		}
2027 
2028 		if (phy_on_sfp(phy)) {
2029 			/* If the PHY is on a SFP, limit the interfaces to
2030 			 * those that can be used with a SFP module.
2031 			 */
2032 			phy_interface_and(interfaces, interfaces,
2033 					  phylink_sfp_interfaces);
2034 
2035 			if (phy_interface_empty(interfaces)) {
2036 				phylink_err(pl, "SFP PHY's possible interfaces becomes empty\n");
2037 				return -EINVAL;
2038 			}
2039 		}
2040 
2041 		phylink_dbg(pl, "PHY %s uses interfaces %*pbl, validating %*pbl\n",
2042 			    phydev_name(phy),
2043 			    (int)PHY_INTERFACE_MODE_MAX,
2044 			    phy->possible_interfaces,
2045 			    (int)PHY_INTERFACE_MODE_MAX, interfaces);
2046 
2047 		return phylink_validate_mask(pl, phy, supported, state,
2048 					     interfaces);
2049 	}
2050 
2051 	phylink_dbg(pl, "PHY %s doesn't supply possible interfaces\n",
2052 		    phydev_name(phy));
2053 
2054 	/* Check whether we would use rate matching for the proposed interface
2055 	 * mode.
2056 	 */
2057 	state->rate_matching = phy_get_rate_matching(phy, state->interface);
2058 
2059 	/* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
2060 	 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
2061 	 * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching
2062 	 * their Serdes is either unnecessary or not reasonable.
2063 	 *
2064 	 * For these which switch interface modes, we really need to know which
2065 	 * interface modes the PHY supports to properly work out which ethtool
2066 	 * linkmodes can be supported. For now, as a work-around, we validate
2067 	 * against all interface modes, which may lead to more ethtool link
2068 	 * modes being advertised than are actually supported.
2069 	 */
2070 	if (phy->is_c45 && state->rate_matching == RATE_MATCH_NONE &&
2071 	    state->interface != PHY_INTERFACE_MODE_RXAUI &&
2072 	    state->interface != PHY_INTERFACE_MODE_XAUI &&
2073 	    state->interface != PHY_INTERFACE_MODE_USXGMII)
2074 		state->interface = PHY_INTERFACE_MODE_NA;
2075 
2076 	return phylink_validate(pl, supported, state);
2077 }
2078 
2079 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
2080 			       phy_interface_t interface)
2081 {
2082 	struct phylink_link_state config;
2083 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
2084 	char *irq_str;
2085 	int ret;
2086 
2087 	/*
2088 	 * This is the new way of dealing with flow control for PHYs,
2089 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2090 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2091 	 * using our validate call to the MAC, we rely upon the MAC
2092 	 * clearing the bits from both supported and advertising fields.
2093 	 */
2094 	phy_support_asym_pause(phy);
2095 
2096 	memset(&config, 0, sizeof(config));
2097 	linkmode_copy(supported, phy->supported);
2098 	linkmode_copy(config.advertising, phy->advertising);
2099 	config.interface = interface;
2100 
2101 	ret = phylink_validate_phy(pl, phy, supported, &config);
2102 	if (ret) {
2103 		phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
2104 			     phy_modes(config.interface),
2105 			     __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
2106 			     __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
2107 			     ERR_PTR(ret));
2108 		return ret;
2109 	}
2110 
2111 	phy->phylink = pl;
2112 	phy->phy_link_change = phylink_phy_change;
2113 
2114 	irq_str = phy_attached_info_irq(phy);
2115 	phylink_info(pl,
2116 		     "PHY [%s] driver [%s] (irq=%s)\n",
2117 		     dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
2118 	kfree(irq_str);
2119 
2120 	mutex_lock(&pl->phydev_mutex);
2121 	mutex_lock(&phy->lock);
2122 	mutex_lock(&pl->state_mutex);
2123 	pl->phydev = phy;
2124 	pl->phy_state.interface = interface;
2125 	pl->phy_state.pause = MLO_PAUSE_NONE;
2126 	pl->phy_state.speed = SPEED_UNKNOWN;
2127 	pl->phy_state.duplex = DUPLEX_UNKNOWN;
2128 	pl->phy_state.rate_matching = RATE_MATCH_NONE;
2129 	linkmode_copy(pl->supported, supported);
2130 	linkmode_copy(pl->link_config.advertising, config.advertising);
2131 
2132 	/* Restrict the phy advertisement according to the MAC support. */
2133 	linkmode_copy(phy->advertising, config.advertising);
2134 
2135 	/* If the MAC supports phylink managed EEE, restrict the EEE
2136 	 * advertisement according to the MAC's LPI capabilities.
2137 	 */
2138 	if (pl->mac_supports_eee) {
2139 		/* If EEE is enabled, then we need to call phy_support_eee()
2140 		 * to ensure that the advertising mask is appropriately set.
2141 		 * This also enables EEE at the PHY.
2142 		 */
2143 		if (pl->eee_cfg.eee_enabled)
2144 			phy_support_eee(phy);
2145 
2146 		phy->eee_cfg.tx_lpi_enabled = pl->eee_cfg.tx_lpi_enabled;
2147 		phy->eee_cfg.tx_lpi_timer = pl->eee_cfg.tx_lpi_timer;
2148 
2149 		/* Convert the MAC's LPI capabilities to linkmodes */
2150 		linkmode_zero(pl->supported_lpi);
2151 		phylink_caps_to_linkmodes(pl->supported_lpi,
2152 					  pl->config->lpi_capabilities);
2153 
2154 		/* Restrict the PHYs EEE support/advertisement to the modes
2155 		 * that the MAC supports.
2156 		 */
2157 		linkmode_and(phy->advertising_eee, phy->advertising_eee,
2158 			     pl->supported_lpi);
2159 	} else if (pl->mac_supports_eee_ops) {
2160 		/* MAC supports phylink EEE, but wants EEE always disabled. */
2161 		phy_disable_eee(phy);
2162 	}
2163 
2164 	mutex_unlock(&pl->state_mutex);
2165 	mutex_unlock(&phy->lock);
2166 	mutex_unlock(&pl->phydev_mutex);
2167 
2168 	phylink_dbg(pl,
2169 		    "phy: %s setting supported %*pb advertising %*pb\n",
2170 		    phy_modes(interface),
2171 		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
2172 		    __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
2173 
2174 	if (pl->config->mac_managed_pm)
2175 		phy->mac_managed_pm = true;
2176 
2177 	/* Allow the MAC to stop its clock if the PHY has the capability */
2178 	pl->mac_tx_clk_stop = phy_eee_tx_clock_stop_capable(phy) > 0;
2179 
2180 	if (pl->mac_supports_eee_ops) {
2181 		/* Explicitly configure whether the PHY is allowed to stop it's
2182 		 * receive clock.
2183 		 */
2184 		ret = phy_eee_rx_clock_stop(phy,
2185 					    pl->config->eee_rx_clk_stop_enable);
2186 		if (ret == -EOPNOTSUPP)
2187 			ret = 0;
2188 	}
2189 
2190 	if (ret == 0 && phy_interrupt_is_valid(phy))
2191 		phy_request_interrupt(phy);
2192 
2193 	return ret;
2194 }
2195 
2196 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
2197 			      phy_interface_t interface)
2198 {
2199 	u32 flags = 0;
2200 
2201 	if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
2202 		    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
2203 		     phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
2204 		return -EINVAL;
2205 
2206 	if (pl->phydev)
2207 		return -EBUSY;
2208 
2209 	if (pl->config->mac_requires_rxc)
2210 		flags |= PHY_F_RXC_ALWAYS_ON;
2211 
2212 	return phy_attach_direct(pl->netdev, phy, flags, interface);
2213 }
2214 
2215 /**
2216  * phylink_connect_phy() - connect a PHY to the phylink instance
2217  * @pl: a pointer to a &struct phylink returned from phylink_create()
2218  * @phy: a pointer to a &struct phy_device.
2219  *
2220  * Connect @phy to the phylink instance specified by @pl by calling
2221  * phy_attach_direct(). Configure the @phy according to the MAC driver's
2222  * capabilities, start the PHYLIB state machine and enable any interrupts
2223  * that the PHY supports.
2224  *
2225  * This updates the phylink's ethtool supported and advertising link mode
2226  * masks.
2227  *
2228  * Returns 0 on success or a negative errno.
2229  */
2230 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
2231 {
2232 	int ret;
2233 
2234 	/* Use PHY device/driver interface */
2235 	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
2236 		pl->link_interface = phy->interface;
2237 		pl->link_config.interface = pl->link_interface;
2238 	}
2239 
2240 	ret = phylink_attach_phy(pl, phy, pl->link_interface);
2241 	if (ret < 0)
2242 		return ret;
2243 
2244 	ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
2245 	if (ret)
2246 		phy_detach(phy);
2247 
2248 	return ret;
2249 }
2250 EXPORT_SYMBOL_GPL(phylink_connect_phy);
2251 
2252 /**
2253  * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
2254  * @pl: a pointer to a &struct phylink returned from phylink_create()
2255  * @dn: a pointer to a &struct device_node.
2256  * @flags: PHY-specific flags to communicate to the PHY device driver
2257  *
2258  * Connect the phy specified in the device node @dn to the phylink instance
2259  * specified by @pl. Actions specified in phylink_connect_phy() will be
2260  * performed.
2261  *
2262  * Returns 0 on success or a negative errno.
2263  */
2264 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
2265 			   u32 flags)
2266 {
2267 	return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
2268 }
2269 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
2270 
2271 /**
2272  * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
2273  * @pl: a pointer to a &struct phylink returned from phylink_create()
2274  * @fwnode: a pointer to a &struct fwnode_handle.
2275  * @flags: PHY-specific flags to communicate to the PHY device driver
2276  *
2277  * Connect the phy specified @fwnode to the phylink instance specified
2278  * by @pl.
2279  *
2280  * Returns 0 on success or a negative errno.
2281  */
2282 int phylink_fwnode_phy_connect(struct phylink *pl,
2283 			       const struct fwnode_handle *fwnode,
2284 			       u32 flags)
2285 {
2286 	struct fwnode_handle *phy_fwnode;
2287 	struct phy_device *phy_dev;
2288 	int ret;
2289 
2290 	/* Fixed links and 802.3z are handled without needing a PHY */
2291 	if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
2292 	    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
2293 	     phy_interface_mode_is_8023z(pl->link_interface)))
2294 		return 0;
2295 
2296 	phy_fwnode = fwnode_get_phy_node(fwnode);
2297 	if (IS_ERR(phy_fwnode)) {
2298 		if (pl->cfg_link_an_mode == MLO_AN_PHY)
2299 			return -ENODEV;
2300 		return 0;
2301 	}
2302 
2303 	phy_dev = fwnode_phy_find_device(phy_fwnode);
2304 	/* We're done with the phy_node handle */
2305 	fwnode_handle_put(phy_fwnode);
2306 	if (!phy_dev)
2307 		return -ENODEV;
2308 
2309 	/* Use PHY device/driver interface */
2310 	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
2311 		pl->link_interface = phy_dev->interface;
2312 		pl->link_config.interface = pl->link_interface;
2313 	}
2314 
2315 	if (pl->config->mac_requires_rxc)
2316 		flags |= PHY_F_RXC_ALWAYS_ON;
2317 
2318 	ret = phy_attach_direct(pl->netdev, phy_dev, flags,
2319 				pl->link_interface);
2320 	phy_device_free(phy_dev);
2321 	if (ret)
2322 		return ret;
2323 
2324 	ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
2325 	if (ret)
2326 		phy_detach(phy_dev);
2327 
2328 	return ret;
2329 }
2330 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
2331 
2332 /**
2333  * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
2334  *   instance.
2335  * @pl: a pointer to a &struct phylink returned from phylink_create()
2336  *
2337  * Disconnect any current PHY from the phylink instance described by @pl.
2338  */
2339 void phylink_disconnect_phy(struct phylink *pl)
2340 {
2341 	struct phy_device *phy;
2342 
2343 	ASSERT_RTNL();
2344 
2345 	mutex_lock(&pl->phydev_mutex);
2346 	phy = pl->phydev;
2347 	if (phy) {
2348 		mutex_lock(&phy->lock);
2349 		mutex_lock(&pl->state_mutex);
2350 		pl->phydev = NULL;
2351 		pl->phy_enable_tx_lpi = false;
2352 		pl->mac_tx_clk_stop = false;
2353 		mutex_unlock(&pl->state_mutex);
2354 		mutex_unlock(&phy->lock);
2355 	}
2356 	mutex_unlock(&pl->phydev_mutex);
2357 
2358 	if (phy) {
2359 		flush_work(&pl->resolve);
2360 		phy_disconnect(phy);
2361 	}
2362 }
2363 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
2364 
2365 static void phylink_link_changed(struct phylink *pl, bool up, const char *what)
2366 {
2367 	if (!up)
2368 		pl->link_failed = true;
2369 	phylink_run_resolve(pl);
2370 	phylink_dbg(pl, "%s link %s\n", what, up ? "up" : "down");
2371 }
2372 
2373 /**
2374  * phylink_mac_change() - notify phylink of a change in MAC state
2375  * @pl: a pointer to a &struct phylink returned from phylink_create()
2376  * @up: indicates whether the link is currently up.
2377  *
2378  * The MAC driver should call this driver when the state of its link
2379  * changes (eg, link failure, new negotiation results, etc.)
2380  */
2381 void phylink_mac_change(struct phylink *pl, bool up)
2382 {
2383 	phylink_link_changed(pl, up, "mac");
2384 }
2385 EXPORT_SYMBOL_GPL(phylink_mac_change);
2386 
2387 /**
2388  * phylink_pcs_change() - notify phylink of a change to PCS link state
2389  * @pcs: pointer to &struct phylink_pcs
2390  * @up: indicates whether the link is currently up.
2391  *
2392  * The PCS driver should call this when the state of its link changes
2393  * (e.g. link failure, new negotiation results, etc.) Note: it should
2394  * not determine "up" by reading the BMSR. If in doubt about the link
2395  * state at interrupt time, then pass true if pcs_get_state() returns
2396  * the latched link-down state, otherwise pass false.
2397  */
2398 void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
2399 {
2400 	struct phylink *pl = pcs->phylink;
2401 
2402 	if (pl)
2403 		phylink_link_changed(pl, up, "pcs");
2404 }
2405 EXPORT_SYMBOL_GPL(phylink_pcs_change);
2406 
2407 static irqreturn_t phylink_link_handler(int irq, void *data)
2408 {
2409 	struct phylink *pl = data;
2410 
2411 	phylink_run_resolve(pl);
2412 
2413 	return IRQ_HANDLED;
2414 }
2415 
2416 /**
2417  * phylink_start() - start a phylink instance
2418  * @pl: a pointer to a &struct phylink returned from phylink_create()
2419  *
2420  * Start the phylink instance specified by @pl, configuring the MAC for the
2421  * desired link mode(s) and negotiation style. This should be called from the
2422  * network device driver's &struct net_device_ops ndo_open() method.
2423  */
2424 void phylink_start(struct phylink *pl)
2425 {
2426 	bool poll = false;
2427 
2428 	ASSERT_RTNL();
2429 
2430 	phylink_info(pl, "configuring for %s/%s link mode\n",
2431 		     phylink_an_mode_str(pl->req_link_an_mode),
2432 		     phy_modes(pl->link_config.interface));
2433 
2434 	/* Always set the carrier off */
2435 	if (pl->netdev)
2436 		netif_carrier_off(pl->netdev);
2437 
2438 	pl->pcs_state = PCS_STATE_STARTING;
2439 
2440 	/* Apply the link configuration to the MAC when starting. This allows
2441 	 * a fixed-link to start with the correct parameters, and also
2442 	 * ensures that we set the appropriate advertisement for Serdes links.
2443 	 *
2444 	 * Restart autonegotiation if using 802.3z to ensure that the link
2445 	 * parameters are properly negotiated.  This is necessary for DSA
2446 	 * switches using 802.3z negotiation to ensure they see our modes.
2447 	 */
2448 	phylink_mac_initial_config(pl, true);
2449 
2450 	pl->pcs_state = PCS_STATE_STARTED;
2451 
2452 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
2453 
2454 	if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
2455 		int irq = gpiod_to_irq(pl->link_gpio);
2456 
2457 		if (irq > 0) {
2458 			if (!request_irq(irq, phylink_link_handler,
2459 					 IRQF_TRIGGER_RISING |
2460 					 IRQF_TRIGGER_FALLING,
2461 					 "netdev link", pl))
2462 				pl->link_irq = irq;
2463 			else
2464 				irq = 0;
2465 		}
2466 		if (irq <= 0)
2467 			poll = true;
2468 	}
2469 
2470 	if (pl->cfg_link_an_mode == MLO_AN_FIXED)
2471 		poll |= pl->config->poll_fixed_state;
2472 
2473 	if (poll)
2474 		mod_timer(&pl->link_poll, jiffies + HZ);
2475 	if (pl->phydev)
2476 		phy_start(pl->phydev);
2477 	if (pl->sfp_bus)
2478 		sfp_upstream_start(pl->sfp_bus);
2479 }
2480 EXPORT_SYMBOL_GPL(phylink_start);
2481 
2482 /**
2483  * phylink_stop() - stop a phylink instance
2484  * @pl: a pointer to a &struct phylink returned from phylink_create()
2485  *
2486  * Stop the phylink instance specified by @pl. This should be called from the
2487  * network device driver's &struct net_device_ops ndo_stop() method.  The
2488  * network device's carrier state should not be changed prior to calling this
2489  * function.
2490  *
2491  * This will synchronously bring down the link if the link is not already
2492  * down (in other words, it will trigger a mac_link_down() method call.)
2493  */
2494 void phylink_stop(struct phylink *pl)
2495 {
2496 	ASSERT_RTNL();
2497 
2498 	if (pl->sfp_bus)
2499 		sfp_upstream_stop(pl->sfp_bus);
2500 	if (pl->phydev)
2501 		phy_stop(pl->phydev);
2502 	timer_delete_sync(&pl->link_poll);
2503 	if (pl->link_irq) {
2504 		free_irq(pl->link_irq, pl);
2505 		pl->link_irq = 0;
2506 	}
2507 
2508 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
2509 
2510 	pl->pcs_state = PCS_STATE_DOWN;
2511 
2512 	phylink_pcs_disable(pl->pcs);
2513 }
2514 EXPORT_SYMBOL_GPL(phylink_stop);
2515 
2516 /**
2517  * phylink_rx_clk_stop_block() - block PHY ability to stop receive clock in LPI
2518  * @pl: a pointer to a &struct phylink returned from phylink_create()
2519  *
2520  * Disable the PHY's ability to stop the receive clock while the receive path
2521  * is in EEE LPI state, until the number of calls to phylink_rx_clk_stop_block()
2522  * are balanced by calls to phylink_rx_clk_stop_unblock().
2523  */
2524 void phylink_rx_clk_stop_block(struct phylink *pl)
2525 {
2526 	ASSERT_RTNL();
2527 
2528 	if (pl->mac_rx_clk_stop_blocked == U8_MAX) {
2529 		phylink_warn(pl, "%s called too many times - ignoring\n",
2530 			     __func__);
2531 		dump_stack();
2532 		return;
2533 	}
2534 
2535 	/* Disable PHY receive clock stop if this is the first time this
2536 	 * function has been called and clock-stop was previously enabled.
2537 	 */
2538 	if (pl->mac_rx_clk_stop_blocked++ == 0 &&
2539 	    pl->mac_supports_eee_ops && pl->phydev &&
2540 	    pl->config->eee_rx_clk_stop_enable)
2541 		phy_eee_rx_clock_stop(pl->phydev, false);
2542 }
2543 EXPORT_SYMBOL_GPL(phylink_rx_clk_stop_block);
2544 
2545 /**
2546  * phylink_rx_clk_stop_unblock() - unblock PHY ability to stop receive clock
2547  * @pl: a pointer to a &struct phylink returned from phylink_create()
2548  *
2549  * All calls to phylink_rx_clk_stop_block() must be balanced with a
2550  * corresponding call to phylink_rx_clk_stop_unblock() to restore the PHYs
2551  * ability to stop the receive clock when the receive path is in EEE LPI mode.
2552  */
2553 void phylink_rx_clk_stop_unblock(struct phylink *pl)
2554 {
2555 	ASSERT_RTNL();
2556 
2557 	if (pl->mac_rx_clk_stop_blocked == 0) {
2558 		phylink_warn(pl, "%s called too many times - ignoring\n",
2559 			     __func__);
2560 		dump_stack();
2561 		return;
2562 	}
2563 
2564 	/* Re-enable PHY receive clock stop if the number of unblocks matches
2565 	 * the number of calls to the block function above.
2566 	 */
2567 	if (--pl->mac_rx_clk_stop_blocked == 0 &&
2568 	    pl->mac_supports_eee_ops && pl->phydev &&
2569 	    pl->config->eee_rx_clk_stop_enable)
2570 		phy_eee_rx_clock_stop(pl->phydev, true);
2571 }
2572 EXPORT_SYMBOL_GPL(phylink_rx_clk_stop_unblock);
2573 
2574 static bool phylink_mac_supports_wol(struct phylink *pl)
2575 {
2576 	return !!pl->mac_ops->mac_wol_set;
2577 }
2578 
2579 static bool phylink_phy_supports_wol(struct phylink *pl,
2580 				     struct phy_device *phydev)
2581 {
2582 	return phydev && (pl->config->wol_phy_legacy || phy_can_wakeup(phydev));
2583 }
2584 
2585 static bool phylink_phy_pm_speed_ctrl(struct phylink *pl)
2586 {
2587 	return pl->config->wol_phy_speed_ctrl && !pl->wolopts_mac &&
2588 	       pl->phydev && phy_may_wakeup(pl->phydev);
2589 }
2590 
2591 /**
2592  * phylink_suspend() - handle a network device suspend event
2593  * @pl: a pointer to a &struct phylink returned from phylink_create()
2594  * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
2595  *
2596  * Handle a network device suspend event. There are several cases:
2597  *
2598  * - If Wake-on-Lan is not active, we can bring down the link between
2599  *   the MAC and PHY by calling phylink_stop().
2600  * - If Wake-on-Lan is active, and being handled only by the PHY, we
2601  *   can also bring down the link between the MAC and PHY.
2602  * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
2603  *   still needs to receive packets, so we can not bring the link down.
2604  *
2605  * Note: when phylink managed Wake-on-Lan is in use, @mac_wol is ignored.
2606  * (struct phylink_mac_ops.mac_set_wol populated.)
2607  */
2608 void phylink_suspend(struct phylink *pl, bool mac_wol)
2609 {
2610 	ASSERT_RTNL();
2611 
2612 	if (phylink_mac_supports_wol(pl))
2613 		mac_wol = !!pl->wolopts_mac;
2614 
2615 	if (mac_wol && (!pl->netdev || pl->netdev->ethtool->wol_enabled)) {
2616 		/* Wake-on-Lan enabled, MAC handling */
2617 		mutex_lock(&pl->state_mutex);
2618 
2619 		/* Stop the resolver bringing the link up */
2620 		__set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
2621 
2622 		pl->suspend_link_up = phylink_link_is_up(pl);
2623 		if (pl->suspend_link_up) {
2624 			/* Disable the carrier, to prevent transmit timeouts,
2625 			 * but one would hope all packets have been sent. This
2626 			 * also means phylink_resolve() will do nothing.
2627 			 */
2628 			if (pl->netdev)
2629 				netif_carrier_off(pl->netdev);
2630 			pl->old_link_state = false;
2631 		}
2632 
2633 		/* We do not call mac_link_down() here as we want the
2634 		 * link to remain up to receive the WoL packets.
2635 		 */
2636 		mutex_unlock(&pl->state_mutex);
2637 	} else {
2638 		phylink_stop(pl);
2639 	}
2640 
2641 	if (phylink_phy_pm_speed_ctrl(pl))
2642 		phylink_speed_down(pl, false);
2643 }
2644 EXPORT_SYMBOL_GPL(phylink_suspend);
2645 
2646 /**
2647  * phylink_prepare_resume() - prepare to resume a network device
2648  * @pl: a pointer to a &struct phylink returned from phylink_create()
2649  *
2650  * Optional, but if called must be called prior to phylink_resume().
2651  *
2652  * Prepare to resume a network device, preparing the PHY as necessary.
2653  */
2654 void phylink_prepare_resume(struct phylink *pl)
2655 {
2656 	struct phy_device *phydev = pl->phydev;
2657 
2658 	ASSERT_RTNL();
2659 
2660 	/* IEEE 802.3 22.2.4.1.5 allows PHYs to stop their receive clock
2661 	 * when PDOWN is set. However, some MACs require RXC to be running
2662 	 * in order to resume. If the MAC requires RXC, and we have a PHY,
2663 	 * then resume the PHY. Note that 802.3 allows PHYs 500ms before
2664 	 * the clock meets requirements. We do not implement this delay.
2665 	 */
2666 	if (pl->config->mac_requires_rxc && phydev && phydev->suspended)
2667 		phy_resume(phydev);
2668 }
2669 EXPORT_SYMBOL_GPL(phylink_prepare_resume);
2670 
2671 /**
2672  * phylink_resume() - handle a network device resume event
2673  * @pl: a pointer to a &struct phylink returned from phylink_create()
2674  *
2675  * Undo the effects of phylink_suspend(), returning the link to an
2676  * operational state.
2677  */
2678 void phylink_resume(struct phylink *pl)
2679 {
2680 	ASSERT_RTNL();
2681 
2682 	if (phylink_phy_pm_speed_ctrl(pl))
2683 		phylink_speed_up(pl);
2684 
2685 	if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
2686 		/* Wake-on-Lan enabled, MAC handling */
2687 
2688 		if (pl->suspend_link_up) {
2689 			/* Call mac_link_down() so we keep the overall state
2690 			 * balanced. Do this under the state_mutex lock for
2691 			 * consistency. This will cause a "Link Down" message
2692 			 * to be printed during resume, which is harmless -
2693 			 * the true link state will be printed when we run a
2694 			 * resolve.
2695 			 */
2696 			mutex_lock(&pl->state_mutex);
2697 			phylink_link_down(pl);
2698 			mutex_unlock(&pl->state_mutex);
2699 		}
2700 
2701 		/* Re-apply the link parameters so that all the settings get
2702 		 * restored to the MAC.
2703 		 */
2704 		phylink_mac_initial_config(pl, true);
2705 
2706 		/* Re-enable and re-resolve the link parameters */
2707 		phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL);
2708 	} else {
2709 		phylink_start(pl);
2710 	}
2711 }
2712 EXPORT_SYMBOL_GPL(phylink_resume);
2713 
2714 /**
2715  * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
2716  * @pl: a pointer to a &struct phylink returned from phylink_create()
2717  * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
2718  *
2719  * Read the wake on lan parameters from the PHY attached to the phylink
2720  * instance specified by @pl. If no PHY is currently attached, report no
2721  * support for wake on lan.
2722  */
2723 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2724 {
2725 	ASSERT_RTNL();
2726 
2727 	wol->supported = 0;
2728 	wol->wolopts = 0;
2729 
2730 	if (phylink_mac_supports_wol(pl)) {
2731 		if (phylink_phy_supports_wol(pl, pl->phydev))
2732 			phy_ethtool_get_wol(pl->phydev, wol);
2733 
2734 		/* Where the MAC augments the WoL support, merge its support and
2735 		 * current configuration.
2736 		 */
2737 		if (~wol->wolopts & pl->wolopts_mac & WAKE_MAGICSECURE)
2738 			memcpy(wol->sopass, pl->wol_sopass,
2739 			       sizeof(wol->sopass));
2740 
2741 		wol->supported |= pl->config->wol_mac_support;
2742 		wol->wolopts |= pl->wolopts_mac;
2743 	} else {
2744 		/* Legacy */
2745 		if (pl->phydev)
2746 			phy_ethtool_get_wol(pl->phydev, wol);
2747 	}
2748 }
2749 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
2750 
2751 /**
2752  * phylink_ethtool_set_wol() - set wake on lan parameters
2753  * @pl: a pointer to a &struct phylink returned from phylink_create()
2754  * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
2755  *
2756  * Set the wake on lan parameters for the PHY attached to the phylink
2757  * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
2758  * error.
2759  *
2760  * Returns zero on success or negative errno code.
2761  */
2762 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2763 {
2764 	struct ethtool_wolinfo w = { .cmd = ETHTOOL_GWOL };
2765 	int ret = -EOPNOTSUPP;
2766 	bool changed;
2767 	u32 wolopts;
2768 
2769 	ASSERT_RTNL();
2770 
2771 	if (phylink_mac_supports_wol(pl)) {
2772 		wolopts = wol->wolopts;
2773 
2774 		if (phylink_phy_supports_wol(pl, pl->phydev)) {
2775 			ret = phy_ethtool_set_wol(pl->phydev, wol);
2776 			if (ret != 0 && ret != -EOPNOTSUPP)
2777 				return ret;
2778 
2779 			phy_ethtool_get_wol(pl->phydev, &w);
2780 
2781 			/* Any Wake-on-Lan modes which the PHY is handling
2782 			 * should not be passed on to the MAC.
2783 			 */
2784 			wolopts &= ~w.wolopts;
2785 		}
2786 
2787 		wolopts &= pl->config->wol_mac_support;
2788 		changed = pl->wolopts_mac != wolopts;
2789 		if (wolopts & WAKE_MAGICSECURE)
2790 			changed |= !!memcmp(wol->sopass, pl->wol_sopass,
2791 					    sizeof(wol->sopass));
2792 		memcpy(pl->wol_sopass, wol->sopass, sizeof(pl->wol_sopass));
2793 
2794 		if (changed) {
2795 			ret = pl->mac_ops->mac_wol_set(pl->config, wolopts,
2796 						       wol->sopass);
2797 			if (!ret)
2798 				pl->wolopts_mac = wolopts;
2799 		} else {
2800 			ret = 0;
2801 		}
2802 	} else {
2803 		if (pl->phydev)
2804 			ret = phy_ethtool_set_wol(pl->phydev, wol);
2805 	}
2806 
2807 	return ret;
2808 }
2809 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
2810 
2811 static phy_interface_t phylink_sfp_select_interface(struct phylink *pl,
2812 						const unsigned long *link_modes)
2813 {
2814 	phy_interface_t interface;
2815 
2816 	interface = sfp_select_interface(pl->sfp_bus, link_modes);
2817 	if (interface == PHY_INTERFACE_MODE_NA) {
2818 		phylink_err(pl,
2819 			    "selection of interface failed, advertisement %*pb\n",
2820 			    __ETHTOOL_LINK_MODE_MASK_NBITS,
2821 			    link_modes);
2822 		return interface;
2823 	}
2824 
2825 	if (!test_bit(interface, pl->config->supported_interfaces)) {
2826 		phylink_err(pl,
2827 			    "selection of interface failed, SFP selected %s (%u) but MAC supports %*pbl\n",
2828 			    phy_modes(interface), interface,
2829 			    (int)PHY_INTERFACE_MODE_MAX,
2830 			    pl->config->supported_interfaces);
2831 		return PHY_INTERFACE_MODE_NA;
2832 	}
2833 
2834 	return interface;
2835 }
2836 
2837 static phy_interface_t phylink_sfp_select_interface_speed(struct phylink *pl,
2838 							  u32 speed)
2839 {
2840 	phy_interface_t best_interface = PHY_INTERFACE_MODE_NA;
2841 	phy_interface_t interface;
2842 	u32 max_speed;
2843 	int i;
2844 
2845 	for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++) {
2846 		interface = phylink_sfp_interface_preference[i];
2847 		if (!test_bit(interface, pl->sfp_interfaces))
2848 			continue;
2849 
2850 		max_speed = phylink_interface_max_speed(interface);
2851 
2852 		/* The logic here is: if speed == max_speed, then we've found
2853 		 * the best interface. Otherwise we find the interface that
2854 		 * can just support the requested speed.
2855 		 */
2856 		if (max_speed >= speed)
2857 			best_interface = interface;
2858 
2859 		if (max_speed <= speed)
2860 			break;
2861 	}
2862 
2863 	if (best_interface == PHY_INTERFACE_MODE_NA)
2864 		phylink_err(pl, "selection of interface failed, speed %u\n",
2865 			    speed);
2866 
2867 	return best_interface;
2868 }
2869 
2870 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
2871 {
2872 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
2873 
2874 	linkmode_zero(mask);
2875 	phylink_set_port_modes(mask);
2876 
2877 	linkmode_and(dst, dst, mask);
2878 	linkmode_or(dst, dst, b);
2879 }
2880 
2881 static void phylink_get_ksettings(const struct phylink_link_state *state,
2882 				  struct ethtool_link_ksettings *kset)
2883 {
2884 	phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
2885 	linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
2886 	if (kset->base.rate_matching == RATE_MATCH_NONE) {
2887 		kset->base.speed = state->speed;
2888 		kset->base.duplex = state->duplex;
2889 	}
2890 	kset->base.autoneg = linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2891 					       state->advertising) ?
2892 				AUTONEG_ENABLE : AUTONEG_DISABLE;
2893 }
2894 
2895 /**
2896  * phylink_ethtool_ksettings_get() - get the current link settings
2897  * @pl: a pointer to a &struct phylink returned from phylink_create()
2898  * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
2899  *
2900  * Read the current link settings for the phylink instance specified by @pl.
2901  * This will be the link settings read from the MAC, PHY or fixed link
2902  * settings depending on the current negotiation mode.
2903  */
2904 int phylink_ethtool_ksettings_get(struct phylink *pl,
2905 				  struct ethtool_link_ksettings *kset)
2906 {
2907 	struct phylink_link_state link_state;
2908 
2909 	ASSERT_RTNL();
2910 
2911 	if (pl->phydev)
2912 		phy_ethtool_ksettings_get(pl->phydev, kset);
2913 	else
2914 		kset->base.port = pl->link_port;
2915 
2916 	linkmode_copy(kset->link_modes.supported, pl->supported);
2917 
2918 	switch (pl->act_link_an_mode) {
2919 	case MLO_AN_FIXED:
2920 		/* We are using fixed settings. Report these as the
2921 		 * current link settings - and note that these also
2922 		 * represent the supported speeds/duplex/pause modes.
2923 		 */
2924 		phylink_get_fixed_state(pl, &link_state);
2925 		phylink_get_ksettings(&link_state, kset);
2926 		break;
2927 
2928 	case MLO_AN_INBAND:
2929 		/* If there is a phy attached, then use the reported
2930 		 * settings from the phy with no modification.
2931 		 */
2932 		if (pl->phydev)
2933 			break;
2934 
2935 		phylink_mac_pcs_get_state(pl, &link_state);
2936 
2937 		/* The MAC is reporting the link results from its own PCS
2938 		 * layer via in-band status. Report these as the current
2939 		 * link settings.
2940 		 */
2941 		phylink_get_ksettings(&link_state, kset);
2942 		break;
2943 	}
2944 
2945 	return 0;
2946 }
2947 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
2948 
2949 static bool phylink_validate_pcs_inband_autoneg(struct phylink *pl,
2950 					        phy_interface_t interface,
2951 						unsigned long *adv)
2952 {
2953 	unsigned int inband = phylink_inband_caps(pl, interface);
2954 	unsigned int mask;
2955 
2956 	/* If the PCS doesn't implement inband support, be permissive. */
2957 	if (!inband)
2958 		return true;
2959 
2960 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv))
2961 		mask = LINK_INBAND_ENABLE;
2962 	else
2963 		mask = LINK_INBAND_DISABLE;
2964 
2965 	/* Check whether the PCS implements the required mode */
2966 	return !!(inband & mask);
2967 }
2968 
2969 /**
2970  * phylink_ethtool_ksettings_set() - set the link settings
2971  * @pl: a pointer to a &struct phylink returned from phylink_create()
2972  * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
2973  */
2974 int phylink_ethtool_ksettings_set(struct phylink *pl,
2975 				  const struct ethtool_link_ksettings *kset)
2976 {
2977 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2978 	const struct link_capabilities *c;
2979 	struct phylink_link_state config;
2980 
2981 	ASSERT_RTNL();
2982 
2983 	if (pl->phydev) {
2984 		struct ethtool_link_ksettings phy_kset = *kset;
2985 
2986 		linkmode_and(phy_kset.link_modes.advertising,
2987 			     phy_kset.link_modes.advertising,
2988 			     pl->supported);
2989 
2990 		/* We can rely on phylib for this update; we also do not need
2991 		 * to update the pl->link_config settings:
2992 		 * - the configuration returned via ksettings_get() will come
2993 		 *   from phylib whenever a PHY is present.
2994 		 * - link_config.interface will be updated by the PHY calling
2995 		 *   back via phylink_phy_change() and a subsequent resolve.
2996 		 * - initial link configuration for PHY mode comes from the
2997 		 *   last phy state updated via phylink_phy_change().
2998 		 * - other configuration changes (e.g. pause modes) are
2999 		 *   performed directly via phylib.
3000 		 * - if in in-band mode with a PHY, the link configuration
3001 		 *   is passed on the link from the PHY, and all of
3002 		 *   link_config.{speed,duplex,an_enabled,pause} are not used.
3003 		 * - the only possible use would be link_config.advertising
3004 		 *   pause modes when in 1000base-X mode with a PHY, but in
3005 		 *   the presence of a PHY, this should not be changed as that
3006 		 *   should be determined from the media side advertisement.
3007 		 */
3008 		return phy_ethtool_ksettings_set(pl->phydev, &phy_kset);
3009 	}
3010 
3011 	config = pl->link_config;
3012 	/* Mask out unsupported advertisements */
3013 	linkmode_and(config.advertising, kset->link_modes.advertising,
3014 		     pl->supported);
3015 
3016 	/* FIXME: should we reject autoneg if phy/mac does not support it? */
3017 	switch (kset->base.autoneg) {
3018 	case AUTONEG_DISABLE:
3019 		/* Autonegotiation disabled, select a suitable speed and
3020 		 * duplex.
3021 		 */
3022 		c = phy_caps_lookup(kset->base.speed, kset->base.duplex,
3023 				    pl->supported, false);
3024 		if (!c)
3025 			return -EINVAL;
3026 
3027 		/* If we have a fixed link, refuse to change link parameters.
3028 		 * If the link parameters match, accept them but do nothing.
3029 		 */
3030 		if (pl->req_link_an_mode == MLO_AN_FIXED) {
3031 			if (c->speed != pl->link_config.speed ||
3032 			    c->duplex != pl->link_config.duplex)
3033 				return -EINVAL;
3034 			return 0;
3035 		}
3036 
3037 		config.speed = c->speed;
3038 		config.duplex = c->duplex;
3039 		break;
3040 
3041 	case AUTONEG_ENABLE:
3042 		/* If we have a fixed link, allow autonegotiation (since that
3043 		 * is our default case) but do not allow the advertisement to
3044 		 * be changed. If the advertisement matches, simply return.
3045 		 */
3046 		if (pl->req_link_an_mode == MLO_AN_FIXED) {
3047 			if (!linkmode_equal(config.advertising,
3048 					    pl->link_config.advertising))
3049 				return -EINVAL;
3050 			return 0;
3051 		}
3052 
3053 		config.speed = SPEED_UNKNOWN;
3054 		config.duplex = DUPLEX_UNKNOWN;
3055 		break;
3056 
3057 	default:
3058 		return -EINVAL;
3059 	}
3060 
3061 	/* We have ruled out the case with a PHY attached, and the
3062 	 * fixed-link cases.  All that is left are in-band links.
3063 	 */
3064 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
3065 			 kset->base.autoneg == AUTONEG_ENABLE);
3066 
3067 	/* If this link is with an SFP, ensure that changes to advertised modes
3068 	 * also cause the associated interface to be selected such that the
3069 	 * link can be configured correctly.
3070 	 */
3071 	if (pl->sfp_bus) {
3072 		if (kset->base.autoneg == AUTONEG_ENABLE)
3073 			config.interface =
3074 				phylink_sfp_select_interface(pl,
3075 							config.advertising);
3076 		else
3077 			config.interface =
3078 				phylink_sfp_select_interface_speed(pl,
3079 							config.speed);
3080 		if (config.interface == PHY_INTERFACE_MODE_NA)
3081 			return -EINVAL;
3082 
3083 		/* Revalidate with the selected interface */
3084 		linkmode_copy(support, pl->supported);
3085 		if (phylink_validate(pl, support, &config)) {
3086 			phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
3087 				    phylink_an_mode_str(pl->req_link_an_mode),
3088 				    phy_modes(config.interface),
3089 				    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
3090 			return -EINVAL;
3091 		}
3092 	} else {
3093 		/* Validate without changing the current supported mask. */
3094 		linkmode_copy(support, pl->supported);
3095 		if (phylink_validate(pl, support, &config))
3096 			return -EINVAL;
3097 	}
3098 
3099 	/* If autonegotiation is enabled, we must have an advertisement */
3100 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3101 			      config.advertising) &&
3102 	    phylink_is_empty_linkmode(config.advertising))
3103 		return -EINVAL;
3104 
3105 	/* Validate the autonegotiation state. We don't have a PHY in this
3106 	 * situation, so the PCS is the media-facing entity.
3107 	 */
3108 	if (!phylink_validate_pcs_inband_autoneg(pl, config.interface,
3109 						 config.advertising))
3110 		return -EINVAL;
3111 
3112 	mutex_lock(&pl->state_mutex);
3113 	pl->link_config.speed = config.speed;
3114 	pl->link_config.duplex = config.duplex;
3115 
3116 	if (pl->link_config.interface != config.interface) {
3117 		/* The interface changed, e.g. 1000base-X <-> 2500base-X */
3118 		/* We need to force the link down, then change the interface */
3119 		if (pl->old_link_state) {
3120 			phylink_link_down(pl);
3121 			pl->old_link_state = false;
3122 		}
3123 		if (!test_bit(PHYLINK_DISABLE_STOPPED,
3124 			      &pl->phylink_disable_state))
3125 			phylink_major_config(pl, false, &config);
3126 		pl->link_config.interface = config.interface;
3127 		linkmode_copy(pl->link_config.advertising, config.advertising);
3128 	} else if (!linkmode_equal(pl->link_config.advertising,
3129 				   config.advertising)) {
3130 		linkmode_copy(pl->link_config.advertising, config.advertising);
3131 		phylink_change_inband_advert(pl);
3132 	}
3133 	mutex_unlock(&pl->state_mutex);
3134 
3135 	return 0;
3136 }
3137 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
3138 
3139 /**
3140  * phylink_ethtool_nway_reset() - restart negotiation
3141  * @pl: a pointer to a &struct phylink returned from phylink_create()
3142  *
3143  * Restart negotiation for the phylink instance specified by @pl. This will
3144  * cause any attached phy to restart negotiation with the link partner, and
3145  * if the MAC is in a BaseX mode, the MAC will also be requested to restart
3146  * negotiation.
3147  *
3148  * Returns zero on success, or negative error code.
3149  */
3150 int phylink_ethtool_nway_reset(struct phylink *pl)
3151 {
3152 	int ret = 0;
3153 
3154 	ASSERT_RTNL();
3155 
3156 	if (pl->phydev)
3157 		ret = phy_restart_aneg(pl->phydev);
3158 	phylink_pcs_an_restart(pl);
3159 
3160 	return ret;
3161 }
3162 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
3163 
3164 /**
3165  * phylink_ethtool_get_pauseparam() - get the current pause parameters
3166  * @pl: a pointer to a &struct phylink returned from phylink_create()
3167  * @pause: a pointer to a &struct ethtool_pauseparam
3168  */
3169 void phylink_ethtool_get_pauseparam(struct phylink *pl,
3170 				    struct ethtool_pauseparam *pause)
3171 {
3172 	ASSERT_RTNL();
3173 
3174 	pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
3175 	pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
3176 	pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
3177 }
3178 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
3179 
3180 /**
3181  * phylink_ethtool_set_pauseparam() - set the current pause parameters
3182  * @pl: a pointer to a &struct phylink returned from phylink_create()
3183  * @pause: a pointer to a &struct ethtool_pauseparam
3184  */
3185 int phylink_ethtool_set_pauseparam(struct phylink *pl,
3186 				   struct ethtool_pauseparam *pause)
3187 {
3188 	struct phylink_link_state *config = &pl->link_config;
3189 	bool manual_changed;
3190 	int pause_state;
3191 
3192 	ASSERT_RTNL();
3193 
3194 	if (pl->req_link_an_mode == MLO_AN_FIXED)
3195 		return -EOPNOTSUPP;
3196 
3197 	if (!phylink_test(pl->supported, Pause) &&
3198 	    !phylink_test(pl->supported, Asym_Pause))
3199 		return -EOPNOTSUPP;
3200 
3201 	if (!phylink_test(pl->supported, Asym_Pause) &&
3202 	    pause->rx_pause != pause->tx_pause)
3203 		return -EINVAL;
3204 
3205 	pause_state = 0;
3206 	if (pause->autoneg)
3207 		pause_state |= MLO_PAUSE_AN;
3208 	if (pause->rx_pause)
3209 		pause_state |= MLO_PAUSE_RX;
3210 	if (pause->tx_pause)
3211 		pause_state |= MLO_PAUSE_TX;
3212 
3213 	mutex_lock(&pl->state_mutex);
3214 	/*
3215 	 * See the comments for linkmode_set_pause(), wrt the deficiencies
3216 	 * with the current implementation.  A solution to this issue would
3217 	 * be:
3218 	 * ethtool  Local device
3219 	 *  rx  tx  Pause AsymDir
3220 	 *  0   0   0     0
3221 	 *  1   0   1     1
3222 	 *  0   1   0     1
3223 	 *  1   1   1     1
3224 	 * and then use the ethtool rx/tx enablement status to mask the
3225 	 * rx/tx pause resolution.
3226 	 */
3227 	linkmode_set_pause(config->advertising, pause->tx_pause,
3228 			   pause->rx_pause);
3229 
3230 	manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
3231 			 (!(pause_state & MLO_PAUSE_AN) &&
3232 			   (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
3233 
3234 	config->pause = pause_state;
3235 
3236 	/* Update our in-band advertisement, triggering a renegotiation if
3237 	 * the advertisement changed.
3238 	 */
3239 	if (!pl->phydev)
3240 		phylink_change_inband_advert(pl);
3241 
3242 	mutex_unlock(&pl->state_mutex);
3243 
3244 	/* If we have a PHY, a change of the pause frame advertisement will
3245 	 * cause phylib to renegotiate (if AN is enabled) which will in turn
3246 	 * call our phylink_phy_change() and trigger a resolve.  Note that
3247 	 * we can't hold our state mutex while calling phy_set_asym_pause().
3248 	 */
3249 	if (pl->phydev)
3250 		phy_set_asym_pause(pl->phydev, pause->rx_pause,
3251 				   pause->tx_pause);
3252 
3253 	/* If the manual pause settings changed, make sure we trigger a
3254 	 * resolve to update their state; we can not guarantee that the
3255 	 * link will cycle.
3256 	 */
3257 	if (manual_changed) {
3258 		pl->link_failed = true;
3259 		phylink_run_resolve(pl);
3260 	}
3261 
3262 	return 0;
3263 }
3264 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
3265 
3266 /**
3267  * phylink_get_eee_err() - read the energy efficient ethernet error
3268  *   counter
3269  * @pl: a pointer to a &struct phylink returned from phylink_create().
3270  *
3271  * Read the Energy Efficient Ethernet error counter from the PHY associated
3272  * with the phylink instance specified by @pl.
3273  *
3274  * Returns positive error counter value, or negative error code.
3275  */
3276 int phylink_get_eee_err(struct phylink *pl)
3277 {
3278 	int ret = 0;
3279 
3280 	ASSERT_RTNL();
3281 
3282 	if (pl->phydev)
3283 		ret = phy_get_eee_err(pl->phydev);
3284 
3285 	return ret;
3286 }
3287 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
3288 
3289 /**
3290  * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
3291  * @pl: a pointer to a &struct phylink returned from phylink_create()
3292  * @eee: a pointer to a &struct ethtool_keee for the read parameters
3293  */
3294 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_keee *eee)
3295 {
3296 	int ret = -EOPNOTSUPP;
3297 
3298 	ASSERT_RTNL();
3299 
3300 	if (pl->mac_supports_eee_ops && !pl->mac_supports_eee)
3301 		return ret;
3302 
3303 	if (pl->phydev) {
3304 		ret = phy_ethtool_get_eee(pl->phydev, eee);
3305 		/* Restrict supported linkmode mask */
3306 		if (ret == 0 && pl->mac_supports_eee_ops)
3307 			linkmode_and(eee->supported, eee->supported,
3308 				     pl->supported_lpi);
3309 	}
3310 
3311 	return ret;
3312 }
3313 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
3314 
3315 /**
3316  * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
3317  * @pl: a pointer to a &struct phylink returned from phylink_create()
3318  * @eee: a pointer to a &struct ethtool_keee for the desired parameters
3319  */
3320 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_keee *eee)
3321 {
3322 	bool mac_eee = pl->mac_supports_eee;
3323 	int ret = -EOPNOTSUPP;
3324 
3325 	ASSERT_RTNL();
3326 
3327 	phylink_dbg(pl, "mac %s phylink EEE%s, adv %*pbl, LPI%s timer %uus\n",
3328 		    mac_eee ? "supports" : "does not support",
3329 		    eee->eee_enabled ? ", enabled" : "",
3330 		    __ETHTOOL_LINK_MODE_MASK_NBITS, eee->advertised,
3331 		    eee->tx_lpi_enabled ? " enabled" : "", eee->tx_lpi_timer);
3332 
3333 	if (pl->mac_supports_eee_ops && !mac_eee)
3334 		return ret;
3335 
3336 	if (pl->phydev) {
3337 		/* Restrict advertisement mask */
3338 		if (pl->mac_supports_eee_ops)
3339 			linkmode_and(eee->advertised, eee->advertised,
3340 				     pl->supported_lpi);
3341 		ret = phy_ethtool_set_eee(pl->phydev, eee);
3342 		if (ret == 0)
3343 			eee_to_eeecfg(&pl->eee_cfg, eee);
3344 	}
3345 
3346 	return ret;
3347 }
3348 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
3349 
3350 /* This emulates MII registers for a fixed-mode phy operating as per the
3351  * passed in state. "aneg" defines if we report negotiation is possible.
3352  *
3353  * FIXME: should deal with negotiation state too.
3354  */
3355 static int phylink_mii_emul_read(unsigned int reg,
3356 				 struct phylink_link_state *state)
3357 {
3358 	struct fixed_phy_status fs;
3359 	unsigned long *lpa = state->lp_advertising;
3360 	int val;
3361 
3362 	fs.link = state->link;
3363 	fs.speed = state->speed;
3364 	fs.duplex = state->duplex;
3365 	fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
3366 	fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
3367 
3368 	val = swphy_read_reg(reg, &fs);
3369 	if (reg == MII_BMSR) {
3370 		if (!state->an_complete)
3371 			val &= ~BMSR_ANEGCOMPLETE;
3372 	}
3373 	return val;
3374 }
3375 
3376 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
3377 			    unsigned int reg)
3378 {
3379 	struct phy_device *phydev = pl->phydev;
3380 	int prtad, devad;
3381 
3382 	if (mdio_phy_id_is_c45(phy_id)) {
3383 		prtad = mdio_phy_id_prtad(phy_id);
3384 		devad = mdio_phy_id_devad(phy_id);
3385 		return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
3386 					reg);
3387 	}
3388 
3389 	if (phydev->is_c45) {
3390 		switch (reg) {
3391 		case MII_BMCR:
3392 		case MII_BMSR:
3393 		case MII_PHYSID1:
3394 		case MII_PHYSID2:
3395 			devad = __ffs(phydev->c45_ids.mmds_present);
3396 			break;
3397 		case MII_ADVERTISE:
3398 		case MII_LPA:
3399 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
3400 				return -EINVAL;
3401 			devad = MDIO_MMD_AN;
3402 			if (reg == MII_ADVERTISE)
3403 				reg = MDIO_AN_ADVERTISE;
3404 			else
3405 				reg = MDIO_AN_LPA;
3406 			break;
3407 		default:
3408 			return -EINVAL;
3409 		}
3410 		prtad = phy_id;
3411 		return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
3412 					reg);
3413 	}
3414 
3415 	return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg);
3416 }
3417 
3418 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
3419 			     unsigned int reg, unsigned int val)
3420 {
3421 	struct phy_device *phydev = pl->phydev;
3422 	int prtad, devad;
3423 
3424 	if (mdio_phy_id_is_c45(phy_id)) {
3425 		prtad = mdio_phy_id_prtad(phy_id);
3426 		devad = mdio_phy_id_devad(phy_id);
3427 		return mdiobus_c45_write(pl->phydev->mdio.bus, prtad, devad,
3428 					 reg, val);
3429 	}
3430 
3431 	if (phydev->is_c45) {
3432 		switch (reg) {
3433 		case MII_BMCR:
3434 		case MII_BMSR:
3435 		case MII_PHYSID1:
3436 		case MII_PHYSID2:
3437 			devad = __ffs(phydev->c45_ids.mmds_present);
3438 			break;
3439 		case MII_ADVERTISE:
3440 		case MII_LPA:
3441 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
3442 				return -EINVAL;
3443 			devad = MDIO_MMD_AN;
3444 			if (reg == MII_ADVERTISE)
3445 				reg = MDIO_AN_ADVERTISE;
3446 			else
3447 				reg = MDIO_AN_LPA;
3448 			break;
3449 		default:
3450 			return -EINVAL;
3451 		}
3452 		return mdiobus_c45_write(pl->phydev->mdio.bus, phy_id, devad,
3453 					 reg, val);
3454 	}
3455 
3456 	return mdiobus_write(phydev->mdio.bus, phy_id, reg, val);
3457 }
3458 
3459 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
3460 			    unsigned int reg)
3461 {
3462 	struct phylink_link_state state;
3463 	int val = 0xffff;
3464 
3465 	switch (pl->act_link_an_mode) {
3466 	case MLO_AN_FIXED:
3467 		if (phy_id == 0) {
3468 			phylink_get_fixed_state(pl, &state);
3469 			val = phylink_mii_emul_read(reg, &state);
3470 		}
3471 		break;
3472 
3473 	case MLO_AN_PHY:
3474 		return -EOPNOTSUPP;
3475 
3476 	case MLO_AN_INBAND:
3477 		if (phy_id == 0) {
3478 			phylink_mac_pcs_get_state(pl, &state);
3479 			val = phylink_mii_emul_read(reg, &state);
3480 		}
3481 		break;
3482 	}
3483 
3484 	return val & 0xffff;
3485 }
3486 
3487 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
3488 			     unsigned int reg, unsigned int val)
3489 {
3490 	switch (pl->act_link_an_mode) {
3491 	case MLO_AN_FIXED:
3492 		break;
3493 
3494 	case MLO_AN_PHY:
3495 		return -EOPNOTSUPP;
3496 
3497 	case MLO_AN_INBAND:
3498 		break;
3499 	}
3500 
3501 	return 0;
3502 }
3503 
3504 /**
3505  * phylink_mii_ioctl() - generic mii ioctl interface
3506  * @pl: a pointer to a &struct phylink returned from phylink_create()
3507  * @ifr: a pointer to a &struct ifreq for socket ioctls
3508  * @cmd: ioctl cmd to execute
3509  *
3510  * Perform the specified MII ioctl on the PHY attached to the phylink instance
3511  * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
3512  *
3513  * Returns: zero on success or negative error code.
3514  *
3515  * %SIOCGMIIPHY:
3516  *  read register from the current PHY.
3517  * %SIOCGMIIREG:
3518  *  read register from the specified PHY.
3519  * %SIOCSMIIREG:
3520  *  set a register on the specified PHY.
3521  */
3522 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
3523 {
3524 	struct mii_ioctl_data *mii = if_mii(ifr);
3525 	int  ret;
3526 
3527 	ASSERT_RTNL();
3528 
3529 	if (pl->phydev) {
3530 		/* PHYs only exist for MLO_AN_PHY and SGMII */
3531 		switch (cmd) {
3532 		case SIOCGMIIPHY:
3533 			mii->phy_id = pl->phydev->mdio.addr;
3534 			fallthrough;
3535 
3536 		case SIOCGMIIREG:
3537 			ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
3538 			if (ret >= 0) {
3539 				mii->val_out = ret;
3540 				ret = 0;
3541 			}
3542 			break;
3543 
3544 		case SIOCSMIIREG:
3545 			ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
3546 						mii->val_in);
3547 			break;
3548 
3549 		default:
3550 			ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
3551 			break;
3552 		}
3553 	} else {
3554 		switch (cmd) {
3555 		case SIOCGMIIPHY:
3556 			mii->phy_id = 0;
3557 			fallthrough;
3558 
3559 		case SIOCGMIIREG:
3560 			ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
3561 			if (ret >= 0) {
3562 				mii->val_out = ret;
3563 				ret = 0;
3564 			}
3565 			break;
3566 
3567 		case SIOCSMIIREG:
3568 			ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
3569 						mii->val_in);
3570 			break;
3571 
3572 		default:
3573 			ret = -EOPNOTSUPP;
3574 			break;
3575 		}
3576 	}
3577 
3578 	return ret;
3579 }
3580 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
3581 
3582 /**
3583  * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
3584  *   link partners
3585  * @pl: a pointer to a &struct phylink returned from phylink_create()
3586  * @sync: perform action synchronously
3587  *
3588  * If we have a PHY that is not part of a SFP module, then set the speed
3589  * as described in the phy_speed_down() function. Please see this function
3590  * for a description of the @sync parameter.
3591  *
3592  * Returns zero if there is no PHY, otherwise as per phy_speed_down().
3593  */
3594 int phylink_speed_down(struct phylink *pl, bool sync)
3595 {
3596 	int ret = 0;
3597 
3598 	ASSERT_RTNL();
3599 
3600 	if (!pl->sfp_bus && pl->phydev)
3601 		ret = phy_speed_down(pl->phydev, sync);
3602 
3603 	return ret;
3604 }
3605 EXPORT_SYMBOL_GPL(phylink_speed_down);
3606 
3607 /**
3608  * phylink_speed_up() - restore the advertised speeds prior to the call to
3609  *   phylink_speed_down()
3610  * @pl: a pointer to a &struct phylink returned from phylink_create()
3611  *
3612  * If we have a PHY that is not part of a SFP module, then restore the
3613  * PHY speeds as per phy_speed_up().
3614  *
3615  * Returns zero if there is no PHY, otherwise as per phy_speed_up().
3616  */
3617 int phylink_speed_up(struct phylink *pl)
3618 {
3619 	int ret = 0;
3620 
3621 	ASSERT_RTNL();
3622 
3623 	if (!pl->sfp_bus && pl->phydev)
3624 		ret = phy_speed_up(pl->phydev);
3625 
3626 	return ret;
3627 }
3628 EXPORT_SYMBOL_GPL(phylink_speed_up);
3629 
3630 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
3631 {
3632 	struct phylink *pl = upstream;
3633 
3634 	pl->netdev->sfp_bus = bus;
3635 }
3636 
3637 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
3638 {
3639 	struct phylink *pl = upstream;
3640 
3641 	pl->netdev->sfp_bus = NULL;
3642 }
3643 
3644 static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
3645 						    const unsigned long *intf)
3646 {
3647 	phy_interface_t interface;
3648 	size_t i;
3649 
3650 	interface = PHY_INTERFACE_MODE_NA;
3651 	for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++)
3652 		if (test_bit(phylink_sfp_interface_preference[i], intf)) {
3653 			interface = phylink_sfp_interface_preference[i];
3654 			break;
3655 		}
3656 
3657 	return interface;
3658 }
3659 
3660 static void phylink_sfp_set_config(struct phylink *pl, unsigned long *supported,
3661 				   struct phylink_link_state *state,
3662 				   bool changed)
3663 {
3664 	u8 mode = MLO_AN_INBAND;
3665 
3666 	phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
3667 		    phylink_an_mode_str(mode), phy_modes(state->interface),
3668 		    __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
3669 
3670 	if (!linkmode_equal(pl->supported, supported)) {
3671 		linkmode_copy(pl->supported, supported);
3672 		changed = true;
3673 	}
3674 
3675 	if (!linkmode_equal(pl->link_config.advertising, state->advertising)) {
3676 		linkmode_copy(pl->link_config.advertising, state->advertising);
3677 		changed = true;
3678 	}
3679 
3680 	if (pl->req_link_an_mode != mode ||
3681 	    pl->link_config.interface != state->interface) {
3682 		pl->req_link_an_mode = mode;
3683 		pl->link_config.interface = state->interface;
3684 
3685 		changed = true;
3686 
3687 		phylink_info(pl, "switched to %s/%s link mode\n",
3688 			     phylink_an_mode_str(mode),
3689 			     phy_modes(state->interface));
3690 	}
3691 
3692 	if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
3693 				 &pl->phylink_disable_state))
3694 		phylink_mac_initial_config(pl, false);
3695 }
3696 
3697 static int phylink_sfp_config_phy(struct phylink *pl, struct phy_device *phy)
3698 {
3699 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
3700 	struct phylink_link_state config;
3701 	int ret;
3702 
3703 	/* We're not using pl->sfp_interfaces, so clear it. */
3704 	phy_interface_zero(pl->sfp_interfaces);
3705 	linkmode_copy(support, phy->supported);
3706 
3707 	memset(&config, 0, sizeof(config));
3708 	linkmode_copy(config.advertising, phy->advertising);
3709 	config.interface = PHY_INTERFACE_MODE_NA;
3710 	config.speed = SPEED_UNKNOWN;
3711 	config.duplex = DUPLEX_UNKNOWN;
3712 	config.pause = MLO_PAUSE_AN;
3713 
3714 	/* Ignore errors if we're expecting a PHY to attach later */
3715 	ret = phylink_validate(pl, support, &config);
3716 	if (ret) {
3717 		phylink_err(pl, "validation with support %*pb failed: %pe\n",
3718 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3719 			    ERR_PTR(ret));
3720 		return ret;
3721 	}
3722 
3723 	config.interface = phylink_sfp_select_interface(pl, config.advertising);
3724 	if (config.interface == PHY_INTERFACE_MODE_NA)
3725 		return -EINVAL;
3726 
3727 	/* Attach the PHY so that the PHY is present when we do the major
3728 	 * configuration step.
3729 	 */
3730 	ret = phylink_attach_phy(pl, phy, config.interface);
3731 	if (ret < 0)
3732 		return ret;
3733 
3734 	/* This will validate the configuration for us. */
3735 	ret = phylink_bringup_phy(pl, phy, config.interface);
3736 	if (ret < 0) {
3737 		phy_detach(phy);
3738 		return ret;
3739 	}
3740 
3741 	pl->link_port = pl->sfp_port;
3742 
3743 	phylink_sfp_set_config(pl, support, &config, true);
3744 
3745 	return 0;
3746 }
3747 
3748 static int phylink_sfp_config_optical(struct phylink *pl)
3749 {
3750 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
3751 	struct phylink_link_state config;
3752 	enum inband_type inband_type;
3753 	phy_interface_t interface;
3754 	int ret;
3755 
3756 	phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n",
3757 		    (int)PHY_INTERFACE_MODE_MAX,
3758 		    pl->config->supported_interfaces,
3759 		    (int)PHY_INTERFACE_MODE_MAX,
3760 		    pl->sfp_interfaces);
3761 
3762 	/* Find the union of the supported interfaces by the PCS/MAC and
3763 	 * the SFP module.
3764 	 */
3765 	phy_interface_and(pl->sfp_interfaces, pl->config->supported_interfaces,
3766 			  pl->sfp_interfaces);
3767 	if (phy_interface_empty(pl->sfp_interfaces)) {
3768 		phylink_err(pl, "unsupported SFP module: no common interface modes\n");
3769 		return -EINVAL;
3770 	}
3771 
3772 	memset(&config, 0, sizeof(config));
3773 	linkmode_copy(support, pl->sfp_support);
3774 	linkmode_copy(config.advertising, pl->sfp_support);
3775 	config.speed = SPEED_UNKNOWN;
3776 	config.duplex = DUPLEX_UNKNOWN;
3777 	config.pause = MLO_PAUSE_AN;
3778 
3779 	/* For all the interfaces that are supported, reduce the sfp_support
3780 	 * mask to only those link modes that can be supported.
3781 	 */
3782 	ret = phylink_validate_mask(pl, NULL, pl->sfp_support, &config,
3783 				    pl->sfp_interfaces);
3784 	if (ret) {
3785 		phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
3786 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
3787 		return ret;
3788 	}
3789 
3790 	interface = phylink_choose_sfp_interface(pl, pl->sfp_interfaces);
3791 	if (interface == PHY_INTERFACE_MODE_NA) {
3792 		phylink_err(pl, "failed to select SFP interface\n");
3793 		return -EINVAL;
3794 	}
3795 
3796 	phylink_dbg(pl, "optical SFP: chosen %s interface\n",
3797 		    phy_modes(interface));
3798 
3799 	inband_type = phylink_get_inband_type(interface);
3800 	if (inband_type == INBAND_NONE) {
3801 		/* If this is the sole interface, and there is no inband
3802 		 * support, clear the advertising mask and Autoneg bit in
3803 		 * the support mask. Otherwise, just clear the Autoneg bit
3804 		 * in the advertising mask.
3805 		 */
3806 		if (phy_interface_weight(pl->sfp_interfaces) == 1) {
3807 			linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3808 					   pl->sfp_support);
3809 			linkmode_zero(config.advertising);
3810 		} else {
3811 			linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3812 					   config.advertising);
3813 		}
3814 	}
3815 
3816 	if (!phylink_validate_pcs_inband_autoneg(pl, interface,
3817 						 config.advertising)) {
3818 		phylink_err(pl, "autoneg setting not compatible with PCS");
3819 		return -EINVAL;
3820 	}
3821 
3822 	config.interface = interface;
3823 
3824 	/* Ignore errors if we're expecting a PHY to attach later */
3825 	ret = phylink_validate(pl, support, &config);
3826 	if (ret) {
3827 		phylink_err(pl, "validation with support %*pb failed: %pe\n",
3828 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3829 			    ERR_PTR(ret));
3830 		return ret;
3831 	}
3832 
3833 	pl->link_port = pl->sfp_port;
3834 
3835 	phylink_sfp_set_config(pl, pl->sfp_support, &config, false);
3836 
3837 	return 0;
3838 }
3839 
3840 static int phylink_sfp_module_insert(void *upstream,
3841 				     const struct sfp_eeprom_id *id)
3842 {
3843 	const struct sfp_module_caps *caps;
3844 	struct phylink *pl = upstream;
3845 
3846 	ASSERT_RTNL();
3847 
3848 	caps = sfp_get_module_caps(pl->sfp_bus);
3849 	phy_interface_copy(pl->sfp_interfaces, caps->interfaces);
3850 	linkmode_copy(pl->sfp_support, caps->link_modes);
3851 	pl->sfp_may_have_phy = caps->may_have_phy;
3852 	pl->sfp_port = caps->port;
3853 
3854 	/* If this module may have a PHY connecting later, defer until later */
3855 	if (pl->sfp_may_have_phy)
3856 		return 0;
3857 
3858 	return phylink_sfp_config_optical(pl);
3859 }
3860 
3861 static void phylink_sfp_module_remove(void *upstream)
3862 {
3863 	struct phylink *pl = upstream;
3864 
3865 	phy_interface_zero(pl->sfp_interfaces);
3866 }
3867 
3868 static int phylink_sfp_module_start(void *upstream)
3869 {
3870 	struct phylink *pl = upstream;
3871 
3872 	/* If this SFP module has a PHY, start the PHY now. */
3873 	if (pl->phydev) {
3874 		phy_start(pl->phydev);
3875 		return 0;
3876 	}
3877 
3878 	/* If the module may have a PHY but we didn't detect one we
3879 	 * need to configure the MAC here.
3880 	 */
3881 	if (!pl->sfp_may_have_phy)
3882 		return 0;
3883 
3884 	return phylink_sfp_config_optical(pl);
3885 }
3886 
3887 static void phylink_sfp_module_stop(void *upstream)
3888 {
3889 	struct phylink *pl = upstream;
3890 
3891 	/* If this SFP module has a PHY, stop it. */
3892 	if (pl->phydev)
3893 		phy_stop(pl->phydev);
3894 }
3895 
3896 static void phylink_sfp_link_down(void *upstream)
3897 {
3898 	struct phylink *pl = upstream;
3899 
3900 	ASSERT_RTNL();
3901 
3902 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
3903 }
3904 
3905 static void phylink_sfp_link_up(void *upstream)
3906 {
3907 	struct phylink *pl = upstream;
3908 
3909 	ASSERT_RTNL();
3910 
3911 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
3912 }
3913 
3914 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
3915 {
3916 	struct phylink *pl = upstream;
3917 
3918 	if (!phy->drv) {
3919 		phylink_err(pl, "PHY %s (id 0x%.8lx) has no driver loaded\n",
3920 			    phydev_name(phy), (unsigned long)phy->phy_id);
3921 		phylink_err(pl, "Drivers which handle known common cases: CONFIG_BCM84881_PHY, CONFIG_MARVELL_PHY\n");
3922 		return -EINVAL;
3923 	}
3924 
3925 	/*
3926 	 * This is the new way of dealing with flow control for PHYs,
3927 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
3928 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
3929 	 * using our validate call to the MAC, we rely upon the MAC
3930 	 * clearing the bits from both supported and advertising fields.
3931 	 */
3932 	phy_support_asym_pause(phy);
3933 
3934 	/* Set the PHY's host supported interfaces */
3935 	phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
3936 			  pl->config->supported_interfaces);
3937 
3938 	/* Do the initial configuration */
3939 	return phylink_sfp_config_phy(pl, phy);
3940 }
3941 
3942 static void phylink_sfp_disconnect_phy(void *upstream,
3943 				       struct phy_device *phydev)
3944 {
3945 	phylink_disconnect_phy(upstream);
3946 }
3947 
3948 static const struct sfp_upstream_ops sfp_phylink_ops = {
3949 	.attach = phylink_sfp_attach,
3950 	.detach = phylink_sfp_detach,
3951 	.module_insert = phylink_sfp_module_insert,
3952 	.module_remove = phylink_sfp_module_remove,
3953 	.module_start = phylink_sfp_module_start,
3954 	.module_stop = phylink_sfp_module_stop,
3955 	.link_up = phylink_sfp_link_up,
3956 	.link_down = phylink_sfp_link_down,
3957 	.connect_phy = phylink_sfp_connect_phy,
3958 	.disconnect_phy = phylink_sfp_disconnect_phy,
3959 };
3960 
3961 /* Helpers for MAC drivers */
3962 
3963 static struct {
3964 	int bit;
3965 	int speed;
3966 } phylink_c73_priority_resolution[] = {
3967 	{ ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, SPEED_100000 },
3968 	{ ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, SPEED_100000 },
3969 	/* 100GBASE-KP4 and 100GBASE-CR10 not supported */
3970 	{ ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, SPEED_40000 },
3971 	{ ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, SPEED_40000 },
3972 	{ ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, SPEED_10000 },
3973 	{ ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, SPEED_10000 },
3974 	/* 5GBASE-KR not supported */
3975 	{ ETHTOOL_LINK_MODE_2500baseX_Full_BIT, SPEED_2500 },
3976 	{ ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, SPEED_1000 },
3977 };
3978 
3979 void phylink_resolve_c73(struct phylink_link_state *state)
3980 {
3981 	int i;
3982 
3983 	for (i = 0; i < ARRAY_SIZE(phylink_c73_priority_resolution); i++) {
3984 		int bit = phylink_c73_priority_resolution[i].bit;
3985 		if (linkmode_test_bit(bit, state->advertising) &&
3986 		    linkmode_test_bit(bit, state->lp_advertising))
3987 			break;
3988 	}
3989 
3990 	if (i < ARRAY_SIZE(phylink_c73_priority_resolution)) {
3991 		state->speed = phylink_c73_priority_resolution[i].speed;
3992 		state->duplex = DUPLEX_FULL;
3993 	} else {
3994 		/* negotiation failure */
3995 		state->link = false;
3996 	}
3997 
3998 	phylink_resolve_an_pause(state);
3999 }
4000 EXPORT_SYMBOL_GPL(phylink_resolve_c73);
4001 
4002 static void phylink_decode_c37_word(struct phylink_link_state *state,
4003 				    uint16_t config_reg, int speed)
4004 {
4005 	int fd_bit;
4006 
4007 	if (speed == SPEED_2500)
4008 		fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
4009 	else
4010 		fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
4011 
4012 	mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
4013 
4014 	if (linkmode_test_bit(fd_bit, state->advertising) &&
4015 	    linkmode_test_bit(fd_bit, state->lp_advertising)) {
4016 		state->speed = speed;
4017 		state->duplex = DUPLEX_FULL;
4018 	} else {
4019 		/* negotiation failure */
4020 		state->link = false;
4021 	}
4022 
4023 	phylink_resolve_an_pause(state);
4024 }
4025 
4026 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
4027 				      uint16_t config_reg)
4028 {
4029 	if (!(config_reg & LPA_SGMII_LINK)) {
4030 		state->link = false;
4031 		return;
4032 	}
4033 
4034 	switch (config_reg & LPA_SGMII_SPD_MASK) {
4035 	case LPA_SGMII_10:
4036 		state->speed = SPEED_10;
4037 		break;
4038 	case LPA_SGMII_100:
4039 		state->speed = SPEED_100;
4040 		break;
4041 	case LPA_SGMII_1000:
4042 		state->speed = SPEED_1000;
4043 		break;
4044 	default:
4045 		state->link = false;
4046 		return;
4047 	}
4048 	if (config_reg & LPA_SGMII_FULL_DUPLEX)
4049 		state->duplex = DUPLEX_FULL;
4050 	else
4051 		state->duplex = DUPLEX_HALF;
4052 }
4053 
4054 /**
4055  * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
4056  * @state: a pointer to a struct phylink_link_state.
4057  * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
4058  *
4059  * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
4060  * code word.  Decode the USXGMII code word and populate the corresponding fields
4061  * (speed, duplex) into the phylink_link_state structure.
4062  */
4063 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
4064 				 uint16_t lpa)
4065 {
4066 	switch (lpa & MDIO_USXGMII_SPD_MASK) {
4067 	case MDIO_USXGMII_10:
4068 		state->speed = SPEED_10;
4069 		break;
4070 	case MDIO_USXGMII_100:
4071 		state->speed = SPEED_100;
4072 		break;
4073 	case MDIO_USXGMII_1000:
4074 		state->speed = SPEED_1000;
4075 		break;
4076 	case MDIO_USXGMII_2500:
4077 		state->speed = SPEED_2500;
4078 		break;
4079 	case MDIO_USXGMII_5000:
4080 		state->speed = SPEED_5000;
4081 		break;
4082 	case MDIO_USXGMII_10G:
4083 		state->speed = SPEED_10000;
4084 		break;
4085 	default:
4086 		state->link = false;
4087 		return;
4088 	}
4089 
4090 	if (lpa & MDIO_USXGMII_FULL_DUPLEX)
4091 		state->duplex = DUPLEX_FULL;
4092 	else
4093 		state->duplex = DUPLEX_HALF;
4094 }
4095 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
4096 
4097 /**
4098  * phylink_decode_usgmii_word() - decode the USGMII word from a MAC PCS
4099  * @state: a pointer to a struct phylink_link_state.
4100  * @lpa: a 16 bit value which stores the USGMII auto-negotiation word
4101  *
4102  * Helper for MAC PCS supporting the USGMII protocol and the auto-negotiation
4103  * code word.  Decode the USGMII code word and populate the corresponding fields
4104  * (speed, duplex) into the phylink_link_state structure. The structure for this
4105  * word is the same as the USXGMII word, except it only supports speeds up to
4106  * 1Gbps.
4107  */
4108 static void phylink_decode_usgmii_word(struct phylink_link_state *state,
4109 				       uint16_t lpa)
4110 {
4111 	switch (lpa & MDIO_USXGMII_SPD_MASK) {
4112 	case MDIO_USXGMII_10:
4113 		state->speed = SPEED_10;
4114 		break;
4115 	case MDIO_USXGMII_100:
4116 		state->speed = SPEED_100;
4117 		break;
4118 	case MDIO_USXGMII_1000:
4119 		state->speed = SPEED_1000;
4120 		break;
4121 	default:
4122 		state->link = false;
4123 		return;
4124 	}
4125 
4126 	if (lpa & MDIO_USXGMII_FULL_DUPLEX)
4127 		state->duplex = DUPLEX_FULL;
4128 	else
4129 		state->duplex = DUPLEX_HALF;
4130 }
4131 
4132 /**
4133  * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
4134  * @state: a pointer to a &struct phylink_link_state.
4135  * @neg_mode: link negotiation mode (PHYLINK_PCS_NEG_xxx)
4136  * @bmsr: The value of the %MII_BMSR register
4137  * @lpa: The value of the %MII_LPA register
4138  *
4139  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4140  * clause 37 negotiation and/or SGMII control.
4141  *
4142  * Parse the Clause 37 or Cisco SGMII link partner negotiation word into
4143  * the phylink @state structure. This is suitable to be used for implementing
4144  * the pcs_get_state() member of the struct phylink_pcs_ops structure if
4145  * accessing @bmsr and @lpa cannot be done with MDIO directly.
4146  */
4147 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
4148 				      unsigned int neg_mode, u16 bmsr, u16 lpa)
4149 {
4150 	state->link = !!(bmsr & BMSR_LSTATUS);
4151 	state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
4152 
4153 	/* If the link is down, the advertisement data is undefined. */
4154 	if (!state->link)
4155 		return;
4156 
4157 	switch (state->interface) {
4158 	case PHY_INTERFACE_MODE_1000BASEX:
4159 		if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
4160 			phylink_decode_c37_word(state, lpa, SPEED_1000);
4161 		} else {
4162 			state->speed = SPEED_1000;
4163 			state->duplex = DUPLEX_FULL;
4164 			state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX;
4165 		}
4166 		break;
4167 
4168 	case PHY_INTERFACE_MODE_2500BASEX:
4169 		if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
4170 			phylink_decode_c37_word(state, lpa, SPEED_2500);
4171 		} else {
4172 			state->speed = SPEED_2500;
4173 			state->duplex = DUPLEX_FULL;
4174 			state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX;
4175 		}
4176 		break;
4177 
4178 	case PHY_INTERFACE_MODE_SGMII:
4179 	case PHY_INTERFACE_MODE_QSGMII:
4180 		if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
4181 			phylink_decode_sgmii_word(state, lpa);
4182 		break;
4183 
4184 	case PHY_INTERFACE_MODE_QUSGMII:
4185 		if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
4186 			phylink_decode_usgmii_word(state, lpa);
4187 		break;
4188 
4189 	default:
4190 		state->link = false;
4191 		break;
4192 	}
4193 }
4194 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state);
4195 
4196 /**
4197  * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
4198  * @pcs: a pointer to a &struct mdio_device.
4199  * @neg_mode: link negotiation mode (PHYLINK_PCS_NEG_xxx)
4200  * @state: a pointer to a &struct phylink_link_state.
4201  *
4202  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4203  * clause 37 negotiation and/or SGMII control.
4204  *
4205  * Read the MAC PCS state from the MII device configured in @config and
4206  * parse the Clause 37 or Cisco SGMII link partner negotiation word into
4207  * the phylink @state structure. This is suitable to be directly plugged
4208  * into the pcs_get_state() member of the struct phylink_pcs_ops
4209  * structure.
4210  */
4211 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
4212 				   unsigned int neg_mode,
4213 				   struct phylink_link_state *state)
4214 {
4215 	int bmsr, lpa;
4216 
4217 	bmsr = mdiodev_read(pcs, MII_BMSR);
4218 	lpa = mdiodev_read(pcs, MII_LPA);
4219 	if (bmsr < 0 || lpa < 0) {
4220 		state->link = false;
4221 		return;
4222 	}
4223 
4224 	phylink_mii_c22_pcs_decode_state(state, neg_mode, bmsr, lpa);
4225 }
4226 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
4227 
4228 /**
4229  * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
4230  *	advertisement
4231  * @interface: the PHY interface mode being configured
4232  * @advertising: the ethtool advertisement mask
4233  *
4234  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4235  * clause 37 negotiation and/or SGMII control.
4236  *
4237  * Encode the clause 37 PCS advertisement as specified by @interface and
4238  * @advertising.
4239  *
4240  * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
4241  */
4242 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
4243 					     const unsigned long *advertising)
4244 {
4245 	u16 adv;
4246 
4247 	switch (interface) {
4248 	case PHY_INTERFACE_MODE_1000BASEX:
4249 	case PHY_INTERFACE_MODE_2500BASEX:
4250 		adv = ADVERTISE_1000XFULL;
4251 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
4252 				      advertising))
4253 			adv |= ADVERTISE_1000XPAUSE;
4254 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
4255 				      advertising))
4256 			adv |= ADVERTISE_1000XPSE_ASYM;
4257 		return adv;
4258 	case PHY_INTERFACE_MODE_SGMII:
4259 	case PHY_INTERFACE_MODE_QSGMII:
4260 		return 0x0001;
4261 	default:
4262 		/* Nothing to do for other modes */
4263 		return -EINVAL;
4264 	}
4265 }
4266 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement);
4267 
4268 /**
4269  * phylink_mii_c22_pcs_config() - configure clause 22 PCS
4270  * @pcs: a pointer to a &struct mdio_device.
4271  * @interface: the PHY interface mode being configured
4272  * @advertising: the ethtool advertisement mask
4273  * @neg_mode: PCS negotiation mode
4274  *
4275  * Configure a Clause 22 PCS PHY with the appropriate negotiation
4276  * parameters for the @mode, @interface and @advertising parameters.
4277  * Returns negative error number on failure, zero if the advertisement
4278  * has not changed, or positive if there is a change.
4279  */
4280 int phylink_mii_c22_pcs_config(struct mdio_device *pcs,
4281 			       phy_interface_t interface,
4282 			       const unsigned long *advertising,
4283 			       unsigned int neg_mode)
4284 {
4285 	bool changed = 0;
4286 	u16 bmcr;
4287 	int ret, adv;
4288 
4289 	adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
4290 	if (adv >= 0) {
4291 		ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
4292 					     MII_ADVERTISE, 0xffff, adv);
4293 		if (ret < 0)
4294 			return ret;
4295 		changed = ret;
4296 	}
4297 
4298 	if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
4299 		bmcr = BMCR_ANENABLE;
4300 	else
4301 		bmcr = 0;
4302 
4303 	/* Configure the inband state. Ensure ISOLATE bit is disabled */
4304 	ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
4305 	if (ret < 0)
4306 		return ret;
4307 
4308 	return changed;
4309 }
4310 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
4311 
4312 /**
4313  * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
4314  * @pcs: a pointer to a &struct mdio_device.
4315  *
4316  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4317  * clause 37 negotiation.
4318  *
4319  * Restart the clause 37 negotiation with the link partner. This is
4320  * suitable to be directly plugged into the pcs_get_state() member
4321  * of the struct phylink_pcs_ops structure.
4322  */
4323 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
4324 {
4325 	int val = mdiodev_read(pcs, MII_BMCR);
4326 
4327 	if (val >= 0) {
4328 		val |= BMCR_ANRESTART;
4329 
4330 		mdiodev_write(pcs, MII_BMCR, val);
4331 	}
4332 }
4333 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
4334 
4335 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
4336 				   struct phylink_link_state *state)
4337 {
4338 	struct mii_bus *bus = pcs->bus;
4339 	int addr = pcs->addr;
4340 	int stat;
4341 
4342 	stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
4343 	if (stat < 0) {
4344 		state->link = false;
4345 		return;
4346 	}
4347 
4348 	state->link = !!(stat & MDIO_STAT1_LSTATUS);
4349 	if (!state->link)
4350 		return;
4351 
4352 	switch (state->interface) {
4353 	case PHY_INTERFACE_MODE_10GBASER:
4354 		state->speed = SPEED_10000;
4355 		state->duplex = DUPLEX_FULL;
4356 		break;
4357 
4358 	default:
4359 		break;
4360 	}
4361 }
4362 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
4363 
4364 /**
4365  * phylink_replay_link_begin() - begin replay of link callbacks for driver
4366  *				 which loses state
4367  * @pl: a pointer to a &struct phylink returned from phylink_create()
4368  *
4369  * Helper for MAC drivers which may perform a destructive reset at runtime.
4370  * Both the own driver's mac_link_down() method is called, as well as the
4371  * pcs_link_down() method of the split PCS (if any).
4372  *
4373  * This is similar to phylink_stop(), except it does not alter the state of
4374  * the phylib PHY (it is assumed that it is not affected by the MAC destructive
4375  * reset).
4376  */
4377 void phylink_replay_link_begin(struct phylink *pl)
4378 {
4379 	ASSERT_RTNL();
4380 
4381 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_REPLAY);
4382 }
4383 EXPORT_SYMBOL_GPL(phylink_replay_link_begin);
4384 
4385 /**
4386  * phylink_replay_link_end() - end replay of link callbacks for driver
4387  *			       which lost state
4388  * @pl: a pointer to a &struct phylink returned from phylink_create()
4389  *
4390  * Helper for MAC drivers which may perform a destructive reset at runtime.
4391  * Both the own driver's mac_config() and mac_link_up() methods, as well as the
4392  * pcs_config() and pcs_link_up() method of the split PCS (if any), are called.
4393  *
4394  * This is similar to phylink_start(), except it does not alter the state of
4395  * the phylib PHY.
4396  *
4397  * One must call this method only within the same rtnl_lock() critical section
4398  * as a previous phylink_replay_link_start().
4399  */
4400 void phylink_replay_link_end(struct phylink *pl)
4401 {
4402 	ASSERT_RTNL();
4403 
4404 	if (WARN(!test_bit(PHYLINK_DISABLE_REPLAY,
4405 			   &pl->phylink_disable_state),
4406 		 "phylink_replay_link_end() called without a prior phylink_replay_link_begin()\n"))
4407 		return;
4408 
4409 	pl->force_major_config = true;
4410 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_REPLAY);
4411 	flush_work(&pl->resolve);
4412 }
4413 EXPORT_SYMBOL_GPL(phylink_replay_link_end);
4414 
4415 static int __init phylink_init(void)
4416 {
4417 	for (int i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); ++i)
4418 		__set_bit(phylink_sfp_interface_preference[i],
4419 			  phylink_sfp_interfaces);
4420 
4421 	return 0;
4422 }
4423 
4424 module_init(phylink_init);
4425 
4426 MODULE_LICENSE("GPL v2");
4427 MODULE_DESCRIPTION("phylink models the MAC to optional PHY connection");
4428