xref: /linux/drivers/net/phy/phylink.c (revision 91a4855d6c03e770e42f17c798a36a3c46e63de2)
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 		if (pcs)
1297 			pcs->phylink = pl;
1298 
1299 		pl->pcs = pcs;
1300 	}
1301 
1302 	if (pl->pcs)
1303 		phylink_pcs_pre_config(pl->pcs, state->interface);
1304 
1305 	phylink_mac_config(pl, state);
1306 
1307 	if (pl->pcs) {
1308 		err = phylink_pcs_post_config(pl->pcs, state->interface);
1309 		if (err < 0) {
1310 			phylink_err(pl, "pcs_post_config failed: %pe\n",
1311 				    ERR_PTR(err));
1312 
1313 			pl->major_config_failed = true;
1314 		}
1315 	}
1316 
1317 	if (pl->pcs_state == PCS_STATE_STARTING || pcs_changed)
1318 		phylink_pcs_enable(pl->pcs);
1319 
1320 	err = phylink_pcs_config(pl->pcs, pl->pcs_neg_mode, state,
1321 				 !!(pl->link_config.pause & MLO_PAUSE_AN));
1322 	if (err < 0) {
1323 		phylink_err(pl, "pcs_config failed: %pe\n", ERR_PTR(err));
1324 		pl->major_config_failed = true;
1325 	} else if (err > 0) {
1326 		restart = true;
1327 	}
1328 
1329 	if (restart)
1330 		phylink_pcs_an_restart(pl);
1331 
1332 	if (pl->mac_ops->mac_finish) {
1333 		err = pl->mac_ops->mac_finish(pl->config, pl->act_link_an_mode,
1334 					      state->interface);
1335 		if (err < 0) {
1336 			phylink_err(pl, "mac_finish failed: %pe\n",
1337 				    ERR_PTR(err));
1338 
1339 			pl->major_config_failed = true;
1340 		}
1341 	}
1342 
1343 	if (pl->phydev && pl->phy_ib_mode) {
1344 		phylink_dbg(pl, "configuring PHY for inband%s%s%s\n",
1345 			    pl->phy_ib_mode & LINK_INBAND_DISABLE ?
1346 				" disable" : "",
1347 			    pl->phy_ib_mode & LINK_INBAND_ENABLE ?
1348 				" enable" : "",
1349 			    pl->phy_ib_mode & LINK_INBAND_BYPASS ?
1350 				" bypass" : "");
1351 		err = phy_config_inband(pl->phydev, pl->phy_ib_mode);
1352 		if (err < 0) {
1353 			phylink_err(pl, "phy_config_inband: %pe\n",
1354 				    ERR_PTR(err));
1355 
1356 			pl->major_config_failed = true;
1357 		}
1358 	}
1359 
1360 	if (pl->sfp_bus) {
1361 		rate_kbd = phylink_interface_signal_rate(state->interface);
1362 		if (rate_kbd)
1363 			sfp_upstream_set_signal_rate(pl->sfp_bus, rate_kbd);
1364 	}
1365 
1366 	phylink_pcs_poll_start(pl);
1367 }
1368 
1369 /*
1370  * Reconfigure for a change of inband advertisement.
1371  * If we have a separate PCS, we only need to call its pcs_config() method,
1372  * and then restart AN if it indicates something changed. Otherwise, we do
1373  * the full MAC reconfiguration.
1374  */
1375 static int phylink_change_inband_advert(struct phylink *pl)
1376 {
1377 	int ret;
1378 
1379 	if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
1380 		return 0;
1381 
1382 	phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
1383 		    phylink_an_mode_str(pl->req_link_an_mode),
1384 		    phy_modes(pl->link_config.interface),
1385 		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
1386 		    pl->link_config.pause);
1387 
1388 	/* Recompute the PCS neg mode */
1389 	phylink_pcs_neg_mode(pl, pl->pcs, pl->link_config.interface,
1390 			     pl->link_config.advertising);
1391 
1392 	/* Modern PCS-based method; update the advert at the PCS, and
1393 	 * restart negotiation if the pcs_config() helper indicates that
1394 	 * the programmed advertisement has changed.
1395 	 */
1396 	ret = phylink_pcs_config(pl->pcs, pl->pcs_neg_mode, &pl->link_config,
1397 				 !!(pl->link_config.pause & MLO_PAUSE_AN));
1398 	if (ret < 0)
1399 		return ret;
1400 
1401 	if (ret > 0)
1402 		phylink_pcs_an_restart(pl);
1403 
1404 	return 0;
1405 }
1406 
1407 static void phylink_mac_pcs_get_state(struct phylink *pl,
1408 				      struct phylink_link_state *state)
1409 {
1410 	struct phylink_pcs *pcs;
1411 	bool autoneg;
1412 
1413 	linkmode_copy(state->advertising, pl->link_config.advertising);
1414 	linkmode_zero(state->lp_advertising);
1415 	state->interface = pl->link_config.interface;
1416 	state->rate_matching = pl->link_config.rate_matching;
1417 	state->an_complete = 0;
1418 	state->link = 1;
1419 
1420 	autoneg = pl->pcs_neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED;
1421 	if (autoneg) {
1422 		state->speed = SPEED_UNKNOWN;
1423 		state->duplex = DUPLEX_UNKNOWN;
1424 		state->pause = MLO_PAUSE_NONE;
1425 	} else {
1426 		state->speed =  pl->link_config.speed;
1427 		state->duplex = pl->link_config.duplex;
1428 		state->pause = pl->link_config.pause;
1429 	}
1430 
1431 	pcs = pl->pcs;
1432 	if (pcs)
1433 		pcs->ops->pcs_get_state(pcs, pl->pcs_neg_mode, state);
1434 	else
1435 		state->link = 0;
1436 }
1437 
1438 /* The fixed state is... fixed except for the link state,
1439  * which may be determined by a GPIO or a callback.
1440  */
1441 static void phylink_get_fixed_state(struct phylink *pl,
1442 				    struct phylink_link_state *state)
1443 {
1444 	*state = pl->link_config;
1445 	if (pl->config->get_fixed_state)
1446 		pl->config->get_fixed_state(pl->config, state);
1447 	else if (pl->link_gpio)
1448 		state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
1449 
1450 	state->pause = MLO_PAUSE_NONE;
1451 	phylink_resolve_an_pause(state);
1452 }
1453 
1454 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
1455 {
1456 	struct phylink_link_state link_state;
1457 	struct phy_device *phy = pl->phydev;
1458 
1459 	switch (pl->req_link_an_mode) {
1460 	case MLO_AN_PHY:
1461 		link_state = pl->phy_state;
1462 		break;
1463 
1464 	case MLO_AN_FIXED:
1465 		phylink_get_fixed_state(pl, &link_state);
1466 		break;
1467 
1468 	case MLO_AN_INBAND:
1469 		link_state = pl->link_config;
1470 		if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
1471 			link_state.pause = MLO_PAUSE_NONE;
1472 		break;
1473 
1474 	default: /* can't happen */
1475 		return;
1476 	}
1477 
1478 	link_state.link = false;
1479 
1480 	phylink_apply_manual_flow(pl, &link_state);
1481 	if (phy)
1482 		mutex_lock(&phy->lock);
1483 	phylink_major_config(pl, force_restart, &link_state);
1484 	if (phy)
1485 		mutex_unlock(&phy->lock);
1486 }
1487 
1488 static const char *phylink_pause_to_str(int pause)
1489 {
1490 	switch (pause & MLO_PAUSE_TXRX_MASK) {
1491 	case MLO_PAUSE_TX | MLO_PAUSE_RX:
1492 		return "rx/tx";
1493 	case MLO_PAUSE_TX:
1494 		return "tx";
1495 	case MLO_PAUSE_RX:
1496 		return "rx";
1497 	default:
1498 		return "off";
1499 	}
1500 }
1501 
1502 static void phylink_deactivate_lpi(struct phylink *pl)
1503 {
1504 	if (pl->mac_enable_tx_lpi) {
1505 		pl->mac_enable_tx_lpi = false;
1506 
1507 		phylink_dbg(pl, "disabling LPI\n");
1508 
1509 		pl->mac_ops->mac_disable_tx_lpi(pl->config);
1510 
1511 		phylink_pcs_disable_eee(pl->pcs);
1512 	}
1513 }
1514 
1515 static void phylink_activate_lpi(struct phylink *pl)
1516 {
1517 	int err;
1518 
1519 	if (!test_bit(pl->cur_interface, pl->config->lpi_interfaces)) {
1520 		phylink_dbg(pl, "MAC does not support LPI with %s\n",
1521 			    phy_modes(pl->cur_interface));
1522 		return;
1523 	}
1524 
1525 	phylink_dbg(pl, "LPI timer %uus, tx clock stop %u\n",
1526 		    pl->mac_tx_lpi_timer, pl->mac_tx_clk_stop);
1527 
1528 	phylink_pcs_enable_eee(pl->pcs);
1529 
1530 	err = pl->mac_ops->mac_enable_tx_lpi(pl->config, pl->mac_tx_lpi_timer,
1531 					     pl->mac_tx_clk_stop);
1532 	if (err) {
1533 		phylink_pcs_disable_eee(pl->pcs);
1534 		phylink_err(pl, "%ps() failed: %pe\n",
1535 			    pl->mac_ops->mac_enable_tx_lpi, ERR_PTR(err));
1536 		return;
1537 	}
1538 
1539 	pl->mac_enable_tx_lpi = true;
1540 }
1541 
1542 static void phylink_link_up(struct phylink *pl,
1543 			    struct phylink_link_state link_state)
1544 {
1545 	struct net_device *ndev = pl->netdev;
1546 	int speed, duplex;
1547 	bool rx_pause;
1548 
1549 	speed = link_state.speed;
1550 	duplex = link_state.duplex;
1551 	rx_pause = !!(link_state.pause & MLO_PAUSE_RX);
1552 
1553 	switch (link_state.rate_matching) {
1554 	case RATE_MATCH_PAUSE:
1555 		/* The PHY is doing rate matchion from the media rate (in
1556 		 * the link_state) to the interface speed, and will send
1557 		 * pause frames to the MAC to limit its transmission speed.
1558 		 */
1559 		speed = phylink_interface_max_speed(link_state.interface);
1560 		duplex = DUPLEX_FULL;
1561 		rx_pause = true;
1562 		break;
1563 
1564 	case RATE_MATCH_CRS:
1565 		/* The PHY is doing rate matchion from the media rate (in
1566 		 * the link_state) to the interface speed, and will cause
1567 		 * collisions to the MAC to limit its transmission speed.
1568 		 */
1569 		speed = phylink_interface_max_speed(link_state.interface);
1570 		duplex = DUPLEX_HALF;
1571 		break;
1572 	}
1573 
1574 	pl->cur_interface = link_state.interface;
1575 
1576 	phylink_pcs_link_up(pl->pcs, pl->pcs_neg_mode, pl->cur_interface, speed,
1577 			    duplex);
1578 
1579 	pl->mac_ops->mac_link_up(pl->config, pl->phydev, pl->act_link_an_mode,
1580 				 pl->cur_interface, speed, duplex,
1581 				 !!(link_state.pause & MLO_PAUSE_TX), rx_pause);
1582 
1583 	if (pl->mac_supports_eee && pl->phy_enable_tx_lpi)
1584 		phylink_activate_lpi(pl);
1585 
1586 	if (ndev)
1587 		netif_carrier_on(ndev);
1588 
1589 	phylink_info(pl,
1590 		     "Link is Up - %s/%s - flow control %s\n",
1591 		     phy_speed_to_str(link_state.speed),
1592 		     phy_duplex_to_str(link_state.duplex),
1593 		     phylink_pause_to_str(link_state.pause));
1594 }
1595 
1596 static void phylink_link_down(struct phylink *pl)
1597 {
1598 	struct net_device *ndev = pl->netdev;
1599 
1600 	if (ndev)
1601 		netif_carrier_off(ndev);
1602 
1603 	phylink_deactivate_lpi(pl);
1604 
1605 	pl->mac_ops->mac_link_down(pl->config, pl->act_link_an_mode,
1606 				   pl->cur_interface);
1607 	phylink_info(pl, "Link is Down\n");
1608 }
1609 
1610 static bool phylink_link_is_up(struct phylink *pl)
1611 {
1612 	return pl->netdev ? netif_carrier_ok(pl->netdev) : pl->old_link_state;
1613 }
1614 
1615 static void phylink_resolve(struct work_struct *w)
1616 {
1617 	struct phylink *pl = container_of(w, struct phylink, resolve);
1618 	struct phylink_link_state link_state;
1619 	bool mac_config = false;
1620 	bool retrigger = false;
1621 	struct phy_device *phy;
1622 	bool cur_link_state;
1623 
1624 	mutex_lock(&pl->phydev_mutex);
1625 	phy = pl->phydev;
1626 	if (phy)
1627 		mutex_lock(&phy->lock);
1628 	mutex_lock(&pl->state_mutex);
1629 	cur_link_state = phylink_link_is_up(pl);
1630 
1631 	if (pl->phylink_disable_state) {
1632 		pl->link_failed = false;
1633 		link_state.link = false;
1634 	} else if (pl->link_failed) {
1635 		link_state.link = false;
1636 		retrigger = true;
1637 	} else if (pl->act_link_an_mode == MLO_AN_FIXED) {
1638 		phylink_get_fixed_state(pl, &link_state);
1639 		mac_config = link_state.link;
1640 	} else if (pl->act_link_an_mode == MLO_AN_PHY) {
1641 		link_state = pl->phy_state;
1642 		mac_config = link_state.link;
1643 	} else {
1644 		phylink_mac_pcs_get_state(pl, &link_state);
1645 
1646 		/* The PCS may have a latching link-fail indicator. If the link
1647 		 * was up, bring the link down and re-trigger the resolve.
1648 		 * Otherwise, re-read the PCS state to get the current status
1649 		 * of the link.
1650 		 */
1651 		if (!link_state.link) {
1652 			if (cur_link_state)
1653 				retrigger = true;
1654 			else
1655 				phylink_mac_pcs_get_state(pl, &link_state);
1656 		}
1657 
1658 		/* If we have a phy, the "up" state is the union of both the
1659 		 * PHY and the MAC
1660 		 */
1661 		if (phy)
1662 			link_state.link &= pl->phy_state.link;
1663 
1664 		/* Only update if the PHY link is up */
1665 		if (phy && pl->phy_state.link) {
1666 			/* If the interface has changed, force a link down
1667 			 * event if the link isn't already down, and re-resolve.
1668 			 */
1669 			if (link_state.interface != pl->phy_state.interface) {
1670 				retrigger = true;
1671 				link_state.link = false;
1672 			}
1673 
1674 			link_state.interface = pl->phy_state.interface;
1675 
1676 			/* If we are doing rate matching, then the link
1677 			 * speed/duplex comes from the PHY
1678 			 */
1679 			if (pl->phy_state.rate_matching) {
1680 				link_state.rate_matching =
1681 					pl->phy_state.rate_matching;
1682 				link_state.speed = pl->phy_state.speed;
1683 				link_state.duplex = pl->phy_state.duplex;
1684 			}
1685 
1686 			/* If we have a PHY, we need to update with the PHY
1687 			 * flow control bits.
1688 			 */
1689 			link_state.pause = pl->phy_state.pause;
1690 			mac_config = true;
1691 		}
1692 	}
1693 
1694 	if (pl->act_link_an_mode != MLO_AN_FIXED)
1695 		phylink_apply_manual_flow(pl, &link_state);
1696 
1697 	if ((mac_config && link_state.interface != pl->link_config.interface) ||
1698 	    pl->force_major_config) {
1699 		/* The interface has changed or a forced major configuration
1700 		 * was requested, so force the link down and then reconfigure.
1701 		 */
1702 		if (cur_link_state) {
1703 			phylink_link_down(pl);
1704 			cur_link_state = false;
1705 		}
1706 		phylink_major_config(pl, false, &link_state);
1707 		pl->link_config.interface = link_state.interface;
1708 		pl->force_major_config = false;
1709 	}
1710 
1711 	/* If configuration of the interface failed, force the link down
1712 	 * until we get a successful configuration.
1713 	 */
1714 	if (pl->major_config_failed)
1715 		link_state.link = false;
1716 
1717 	if (link_state.link != cur_link_state) {
1718 		pl->old_link_state = link_state.link;
1719 		if (!link_state.link)
1720 			phylink_link_down(pl);
1721 		else
1722 			phylink_link_up(pl, link_state);
1723 	}
1724 	if (!link_state.link && retrigger) {
1725 		pl->link_failed = false;
1726 		queue_work(system_power_efficient_wq, &pl->resolve);
1727 	}
1728 	mutex_unlock(&pl->state_mutex);
1729 	if (phy)
1730 		mutex_unlock(&phy->lock);
1731 	mutex_unlock(&pl->phydev_mutex);
1732 }
1733 
1734 static void phylink_run_resolve(struct phylink *pl)
1735 {
1736 	if (!pl->phylink_disable_state)
1737 		queue_work(system_power_efficient_wq, &pl->resolve);
1738 }
1739 
1740 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
1741 {
1742 	unsigned long state = pl->phylink_disable_state;
1743 
1744 	set_bit(bit, &pl->phylink_disable_state);
1745 	if (state == 0) {
1746 		queue_work(system_power_efficient_wq, &pl->resolve);
1747 		flush_work(&pl->resolve);
1748 	}
1749 }
1750 
1751 static void phylink_enable_and_run_resolve(struct phylink *pl, int bit)
1752 {
1753 	clear_bit(bit, &pl->phylink_disable_state);
1754 	phylink_run_resolve(pl);
1755 }
1756 
1757 static void phylink_fixed_poll(struct timer_list *t)
1758 {
1759 	struct phylink *pl = container_of(t, struct phylink, link_poll);
1760 
1761 	mod_timer(t, jiffies + HZ);
1762 
1763 	phylink_run_resolve(pl);
1764 }
1765 
1766 static const struct sfp_upstream_ops sfp_phylink_ops;
1767 
1768 static int phylink_register_sfp(struct phylink *pl,
1769 				const struct fwnode_handle *fwnode)
1770 {
1771 	struct sfp_bus *bus;
1772 	int ret;
1773 
1774 	if (!fwnode)
1775 		return 0;
1776 
1777 	bus = sfp_bus_find_fwnode(fwnode);
1778 	if (IS_ERR(bus)) {
1779 		phylink_err(pl, "unable to attach SFP bus: %pe\n", bus);
1780 		return PTR_ERR(bus);
1781 	}
1782 
1783 	pl->sfp_bus = bus;
1784 
1785 	ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
1786 	sfp_bus_put(bus);
1787 
1788 	return ret;
1789 }
1790 
1791 /**
1792  * phylink_set_fixed_link() - set the fixed link
1793  * @pl: a pointer to a &struct phylink returned from phylink_create()
1794  * @state: a pointer to a struct phylink_link_state.
1795  *
1796  * This function is used when the link parameters are known and do not change,
1797  * making it suitable for certain types of network connections.
1798  *
1799  * Returns: zero on success or negative error code.
1800  */
1801 int phylink_set_fixed_link(struct phylink *pl,
1802 			   const struct phylink_link_state *state)
1803 {
1804 	const struct link_capabilities *c;
1805 	unsigned long *adv;
1806 
1807 	if (pl->cfg_link_an_mode != MLO_AN_PHY || !state ||
1808 	    !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
1809 		return -EINVAL;
1810 
1811 	c = phy_caps_lookup(state->speed, state->duplex,
1812 			    pl->supported, true);
1813 	if (!c)
1814 		return -EINVAL;
1815 
1816 	adv = pl->link_config.advertising;
1817 	linkmode_and(adv, pl->supported, c->linkmodes);
1818 	linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv);
1819 
1820 	pl->link_config.speed = state->speed;
1821 	pl->link_config.duplex = state->duplex;
1822 	pl->link_config.link = 1;
1823 	pl->link_config.an_complete = 1;
1824 
1825 	pl->cfg_link_an_mode = MLO_AN_FIXED;
1826 	pl->req_link_an_mode = pl->cfg_link_an_mode;
1827 
1828 	return 0;
1829 }
1830 EXPORT_SYMBOL_GPL(phylink_set_fixed_link);
1831 
1832 /**
1833  * phylink_create() - create a phylink instance
1834  * @config: a pointer to the target &struct phylink_config
1835  * @fwnode: a pointer to a &struct fwnode_handle describing the network
1836  *	interface
1837  * @iface: the desired link mode defined by &typedef phy_interface_t
1838  * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
1839  *
1840  * Create a new phylink instance, and parse the link parameters found in @np.
1841  * This will parse in-band modes, fixed-link or SFP configuration.
1842  *
1843  * Note: the rtnl lock must not be held when calling this function.
1844  *
1845  * Returns a pointer to a &struct phylink, or an error-pointer value. Users
1846  * must use IS_ERR() to check for errors from this function.
1847  */
1848 struct phylink *phylink_create(struct phylink_config *config,
1849 			       const struct fwnode_handle *fwnode,
1850 			       phy_interface_t iface,
1851 			       const struct phylink_mac_ops *mac_ops)
1852 {
1853 	struct phylink *pl;
1854 	int ret;
1855 
1856 	/* Validate the supplied configuration */
1857 	if (phy_interface_empty(config->supported_interfaces)) {
1858 		dev_err(config->dev,
1859 			"phylink: error: empty supported_interfaces\n");
1860 		return ERR_PTR(-EINVAL);
1861 	}
1862 
1863 	pl = kzalloc_obj(*pl);
1864 	if (!pl)
1865 		return ERR_PTR(-ENOMEM);
1866 
1867 	mutex_init(&pl->phydev_mutex);
1868 	mutex_init(&pl->state_mutex);
1869 	INIT_WORK(&pl->resolve, phylink_resolve);
1870 
1871 	pl->config = config;
1872 	if (config->type == PHYLINK_NETDEV) {
1873 		pl->netdev = to_net_dev(config->dev);
1874 		netif_carrier_off(pl->netdev);
1875 	} else if (config->type == PHYLINK_DEV) {
1876 		pl->dev = config->dev;
1877 	} else {
1878 		kfree(pl);
1879 		return ERR_PTR(-EINVAL);
1880 	}
1881 
1882 	pl->mac_supports_eee_ops = phylink_mac_implements_lpi(mac_ops);
1883 	pl->mac_supports_eee = pl->mac_supports_eee_ops &&
1884 			       pl->config->lpi_capabilities &&
1885 			       !phy_interface_empty(pl->config->lpi_interfaces);
1886 
1887 	/* Set the default EEE configuration */
1888 	pl->eee_cfg.eee_enabled = pl->config->eee_enabled_default;
1889 	pl->eee_cfg.tx_lpi_enabled = pl->eee_cfg.eee_enabled;
1890 	pl->eee_cfg.tx_lpi_timer = pl->config->lpi_timer_default;
1891 
1892 	pl->phy_state.interface = iface;
1893 	pl->link_interface = iface;
1894 	if (iface == PHY_INTERFACE_MODE_MOCA)
1895 		pl->link_port = PORT_BNC;
1896 	else
1897 		pl->link_port = PORT_MII;
1898 	pl->link_config.interface = iface;
1899 	pl->link_config.pause = MLO_PAUSE_AN;
1900 	pl->link_config.speed = SPEED_UNKNOWN;
1901 	pl->link_config.duplex = DUPLEX_UNKNOWN;
1902 	pl->pcs_state = PCS_STATE_DOWN;
1903 	pl->mac_ops = mac_ops;
1904 	__set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1905 	timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
1906 
1907 	linkmode_fill(pl->supported);
1908 	linkmode_copy(pl->link_config.advertising, pl->supported);
1909 	phylink_validate(pl, pl->supported, &pl->link_config);
1910 
1911 	ret = phylink_parse_mode(pl, fwnode);
1912 	if (ret < 0) {
1913 		kfree(pl);
1914 		return ERR_PTR(ret);
1915 	}
1916 
1917 	if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
1918 		ret = phylink_parse_fixedlink(pl, fwnode);
1919 		if (ret < 0) {
1920 			kfree(pl);
1921 			return ERR_PTR(ret);
1922 		}
1923 	}
1924 
1925 	pl->req_link_an_mode = pl->cfg_link_an_mode;
1926 
1927 	ret = phylink_register_sfp(pl, fwnode);
1928 	if (ret < 0) {
1929 		kfree(pl);
1930 		return ERR_PTR(ret);
1931 	}
1932 
1933 	return pl;
1934 }
1935 EXPORT_SYMBOL_GPL(phylink_create);
1936 
1937 /**
1938  * phylink_destroy() - cleanup and destroy the phylink instance
1939  * @pl: a pointer to a &struct phylink returned from phylink_create()
1940  *
1941  * Destroy a phylink instance. Any PHY that has been attached must have been
1942  * cleaned up via phylink_disconnect_phy() prior to calling this function.
1943  *
1944  * Note: the rtnl lock must not be held when calling this function.
1945  */
1946 void phylink_destroy(struct phylink *pl)
1947 {
1948 	sfp_bus_del_upstream(pl->sfp_bus);
1949 	if (pl->link_gpio)
1950 		gpiod_put(pl->link_gpio);
1951 
1952 	cancel_work_sync(&pl->resolve);
1953 	kfree(pl);
1954 }
1955 EXPORT_SYMBOL_GPL(phylink_destroy);
1956 
1957 /**
1958  * phylink_expects_phy() - Determine if phylink expects a phy to be attached
1959  * @pl: a pointer to a &struct phylink returned from phylink_create()
1960  *
1961  * When using fixed-link mode, or in-band mode with 1000base-X or 2500base-X,
1962  * no PHY is needed.
1963  *
1964  * Returns true if phylink will be expecting a PHY.
1965  */
1966 bool phylink_expects_phy(struct phylink *pl)
1967 {
1968 	if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1969 	    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1970 	     phy_interface_mode_is_8023z(pl->link_interface)))
1971 		return false;
1972 	return true;
1973 }
1974 EXPORT_SYMBOL_GPL(phylink_expects_phy);
1975 
1976 static void phylink_phy_change(struct phy_device *phydev, bool up)
1977 {
1978 	struct phylink *pl = phydev->phylink;
1979 	bool tx_pause, rx_pause;
1980 
1981 	phy_get_pause(phydev, &tx_pause, &rx_pause);
1982 
1983 	mutex_lock(&pl->state_mutex);
1984 	pl->phy_state.speed = phydev->speed;
1985 	pl->phy_state.duplex = phydev->duplex;
1986 	pl->phy_state.rate_matching = phydev->rate_matching;
1987 	pl->phy_state.pause = MLO_PAUSE_NONE;
1988 	if (tx_pause)
1989 		pl->phy_state.pause |= MLO_PAUSE_TX;
1990 	if (rx_pause)
1991 		pl->phy_state.pause |= MLO_PAUSE_RX;
1992 	pl->phy_state.interface = phydev->interface;
1993 	pl->phy_state.link = up;
1994 	if (!up)
1995 		pl->link_failed = true;
1996 
1997 	/* Get the LPI state from phylib */
1998 	pl->phy_enable_tx_lpi = phydev->enable_tx_lpi;
1999 	pl->mac_tx_lpi_timer = phydev->eee_cfg.tx_lpi_timer;
2000 	mutex_unlock(&pl->state_mutex);
2001 
2002 	phylink_run_resolve(pl);
2003 
2004 	phylink_dbg(pl, "phy link %s %s/%s/%s/%s/%s/%slpi\n",
2005 		    up ? "up" : "down",
2006 		    phy_modes(phydev->interface),
2007 		    phy_speed_to_str(phydev->speed),
2008 		    phy_duplex_to_str(phydev->duplex),
2009 		    phy_rate_matching_to_str(phydev->rate_matching),
2010 		    phylink_pause_to_str(pl->phy_state.pause),
2011 		    phydev->enable_tx_lpi ? "" : "no");
2012 }
2013 
2014 static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
2015 				unsigned long *supported,
2016 				struct phylink_link_state *state)
2017 {
2018 	DECLARE_PHY_INTERFACE_MASK(interfaces);
2019 
2020 	/* If the PHY provides a bitmap of the interfaces it will be using
2021 	 * depending on the negotiated media speeds, use this to validate
2022 	 * which ethtool link modes can be used.
2023 	 */
2024 	if (!phy_interface_empty(phy->possible_interfaces)) {
2025 		/* We only care about the union of the PHY's interfaces and
2026 		 * those which the host supports.
2027 		 */
2028 		phy_interface_and(interfaces, phy->possible_interfaces,
2029 				  pl->config->supported_interfaces);
2030 
2031 		if (phy_interface_empty(interfaces)) {
2032 			phylink_err(pl, "PHY has no common interfaces\n");
2033 			return -EINVAL;
2034 		}
2035 
2036 		if (phy_on_sfp(phy)) {
2037 			/* If the PHY is on a SFP, limit the interfaces to
2038 			 * those that can be used with a SFP module.
2039 			 */
2040 			phy_interface_and(interfaces, interfaces,
2041 					  phylink_sfp_interfaces);
2042 
2043 			if (phy_interface_empty(interfaces)) {
2044 				phylink_err(pl, "SFP PHY's possible interfaces becomes empty\n");
2045 				return -EINVAL;
2046 			}
2047 		}
2048 
2049 		phylink_dbg(pl, "PHY %s uses interfaces %*pbl, validating %*pbl\n",
2050 			    phydev_name(phy),
2051 			    (int)PHY_INTERFACE_MODE_MAX,
2052 			    phy->possible_interfaces,
2053 			    (int)PHY_INTERFACE_MODE_MAX, interfaces);
2054 
2055 		return phylink_validate_mask(pl, phy, supported, state,
2056 					     interfaces);
2057 	}
2058 
2059 	phylink_dbg(pl, "PHY %s doesn't supply possible interfaces\n",
2060 		    phydev_name(phy));
2061 
2062 	/* Check whether we would use rate matching for the proposed interface
2063 	 * mode.
2064 	 */
2065 	state->rate_matching = phy_get_rate_matching(phy, state->interface);
2066 
2067 	/* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
2068 	 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
2069 	 * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching
2070 	 * their Serdes is either unnecessary or not reasonable.
2071 	 *
2072 	 * For these which switch interface modes, we really need to know which
2073 	 * interface modes the PHY supports to properly work out which ethtool
2074 	 * linkmodes can be supported. For now, as a work-around, we validate
2075 	 * against all interface modes, which may lead to more ethtool link
2076 	 * modes being advertised than are actually supported.
2077 	 */
2078 	if (phy->is_c45 && state->rate_matching == RATE_MATCH_NONE &&
2079 	    state->interface != PHY_INTERFACE_MODE_RXAUI &&
2080 	    state->interface != PHY_INTERFACE_MODE_XAUI &&
2081 	    state->interface != PHY_INTERFACE_MODE_USXGMII)
2082 		state->interface = PHY_INTERFACE_MODE_NA;
2083 
2084 	return phylink_validate(pl, supported, state);
2085 }
2086 
2087 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
2088 			       phy_interface_t interface)
2089 {
2090 	struct phylink_link_state config;
2091 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
2092 	char *irq_str;
2093 	int ret;
2094 
2095 	/*
2096 	 * This is the new way of dealing with flow control for PHYs,
2097 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2098 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2099 	 * using our validate call to the MAC, we rely upon the MAC
2100 	 * clearing the bits from both supported and advertising fields.
2101 	 */
2102 	phy_support_asym_pause(phy);
2103 
2104 	memset(&config, 0, sizeof(config));
2105 	linkmode_copy(supported, phy->supported);
2106 	linkmode_copy(config.advertising, phy->advertising);
2107 	config.interface = interface;
2108 
2109 	ret = phylink_validate_phy(pl, phy, supported, &config);
2110 	if (ret) {
2111 		phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
2112 			     phy_modes(config.interface),
2113 			     __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
2114 			     __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
2115 			     ERR_PTR(ret));
2116 		return ret;
2117 	}
2118 
2119 	phy->phylink = pl;
2120 	phy->phy_link_change = phylink_phy_change;
2121 
2122 	irq_str = phy_attached_info_irq(phy);
2123 	phylink_info(pl,
2124 		     "PHY [%s] driver [%s] (irq=%s)\n",
2125 		     dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
2126 	kfree(irq_str);
2127 
2128 	mutex_lock(&pl->phydev_mutex);
2129 	mutex_lock(&phy->lock);
2130 	mutex_lock(&pl->state_mutex);
2131 	pl->phydev = phy;
2132 	pl->phy_state.interface = interface;
2133 	pl->phy_state.pause = MLO_PAUSE_NONE;
2134 	pl->phy_state.speed = SPEED_UNKNOWN;
2135 	pl->phy_state.duplex = DUPLEX_UNKNOWN;
2136 	pl->phy_state.rate_matching = RATE_MATCH_NONE;
2137 	linkmode_copy(pl->supported, supported);
2138 	linkmode_copy(pl->link_config.advertising, config.advertising);
2139 
2140 	/* Restrict the phy advertisement according to the MAC support. */
2141 	linkmode_copy(phy->advertising, config.advertising);
2142 
2143 	/* If the MAC supports phylink managed EEE, restrict the EEE
2144 	 * advertisement according to the MAC's LPI capabilities.
2145 	 */
2146 	if (pl->mac_supports_eee) {
2147 		/* If EEE is enabled, then we need to call phy_support_eee()
2148 		 * to ensure that the advertising mask is appropriately set.
2149 		 * This also enables EEE at the PHY.
2150 		 */
2151 		if (pl->eee_cfg.eee_enabled)
2152 			phy_support_eee(phy);
2153 
2154 		phy->eee_cfg.tx_lpi_enabled = pl->eee_cfg.tx_lpi_enabled;
2155 		phy->eee_cfg.tx_lpi_timer = pl->eee_cfg.tx_lpi_timer;
2156 
2157 		/* Convert the MAC's LPI capabilities to linkmodes */
2158 		linkmode_zero(pl->supported_lpi);
2159 		phylink_caps_to_linkmodes(pl->supported_lpi,
2160 					  pl->config->lpi_capabilities);
2161 
2162 		/* Restrict the PHYs EEE support/advertisement to the modes
2163 		 * that the MAC supports.
2164 		 */
2165 		linkmode_and(phy->advertising_eee, phy->advertising_eee,
2166 			     pl->supported_lpi);
2167 	} else if (pl->mac_supports_eee_ops) {
2168 		/* MAC supports phylink EEE, but wants EEE always disabled. */
2169 		phy_disable_eee(phy);
2170 	}
2171 
2172 	mutex_unlock(&pl->state_mutex);
2173 	mutex_unlock(&phy->lock);
2174 	mutex_unlock(&pl->phydev_mutex);
2175 
2176 	phylink_dbg(pl,
2177 		    "phy: %s setting supported %*pb advertising %*pb\n",
2178 		    phy_modes(interface),
2179 		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
2180 		    __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
2181 
2182 	if (pl->config->mac_managed_pm)
2183 		phy->mac_managed_pm = true;
2184 
2185 	/* Allow the MAC to stop its clock if the PHY has the capability */
2186 	pl->mac_tx_clk_stop = phy_eee_tx_clock_stop_capable(phy) > 0;
2187 
2188 	if (pl->mac_supports_eee_ops) {
2189 		/* Explicitly configure whether the PHY is allowed to stop it's
2190 		 * receive clock.
2191 		 */
2192 		ret = phy_eee_rx_clock_stop(phy,
2193 					    pl->config->eee_rx_clk_stop_enable);
2194 		if (ret == -EOPNOTSUPP)
2195 			ret = 0;
2196 	}
2197 
2198 	if (ret == 0 && phy_interrupt_is_valid(phy))
2199 		phy_request_interrupt(phy);
2200 
2201 	return ret;
2202 }
2203 
2204 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
2205 			      phy_interface_t interface)
2206 {
2207 	u32 flags = 0;
2208 
2209 	if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
2210 		    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
2211 		     phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
2212 		return -EINVAL;
2213 
2214 	if (pl->phydev)
2215 		return -EBUSY;
2216 
2217 	if (pl->config->mac_requires_rxc)
2218 		flags |= PHY_F_RXC_ALWAYS_ON;
2219 
2220 	return phy_attach_direct(pl->netdev, phy, flags, interface);
2221 }
2222 
2223 /**
2224  * phylink_connect_phy() - connect a PHY to the phylink instance
2225  * @pl: a pointer to a &struct phylink returned from phylink_create()
2226  * @phy: a pointer to a &struct phy_device.
2227  *
2228  * Connect @phy to the phylink instance specified by @pl by calling
2229  * phy_attach_direct(). Configure the @phy according to the MAC driver's
2230  * capabilities, start the PHYLIB state machine and enable any interrupts
2231  * that the PHY supports.
2232  *
2233  * This updates the phylink's ethtool supported and advertising link mode
2234  * masks.
2235  *
2236  * Returns 0 on success or a negative errno.
2237  */
2238 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
2239 {
2240 	int ret;
2241 
2242 	/* Use PHY device/driver interface */
2243 	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
2244 		pl->link_interface = phy->interface;
2245 		pl->link_config.interface = pl->link_interface;
2246 	}
2247 
2248 	ret = phylink_attach_phy(pl, phy, pl->link_interface);
2249 	if (ret < 0)
2250 		return ret;
2251 
2252 	ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
2253 	if (ret)
2254 		phy_detach(phy);
2255 
2256 	return ret;
2257 }
2258 EXPORT_SYMBOL_GPL(phylink_connect_phy);
2259 
2260 /**
2261  * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
2262  * @pl: a pointer to a &struct phylink returned from phylink_create()
2263  * @dn: a pointer to a &struct device_node.
2264  * @flags: PHY-specific flags to communicate to the PHY device driver
2265  *
2266  * Connect the phy specified in the device node @dn to the phylink instance
2267  * specified by @pl. Actions specified in phylink_connect_phy() will be
2268  * performed.
2269  *
2270  * Returns 0 on success or a negative errno.
2271  */
2272 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
2273 			   u32 flags)
2274 {
2275 	return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
2276 }
2277 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
2278 
2279 /**
2280  * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
2281  * @pl: a pointer to a &struct phylink returned from phylink_create()
2282  * @fwnode: a pointer to a &struct fwnode_handle.
2283  * @flags: PHY-specific flags to communicate to the PHY device driver
2284  *
2285  * Connect the phy specified @fwnode to the phylink instance specified
2286  * by @pl.
2287  *
2288  * Returns 0 on success or a negative errno.
2289  */
2290 int phylink_fwnode_phy_connect(struct phylink *pl,
2291 			       const struct fwnode_handle *fwnode,
2292 			       u32 flags)
2293 {
2294 	struct fwnode_handle *phy_fwnode;
2295 	struct phy_device *phy_dev;
2296 	int ret;
2297 
2298 	if (!phylink_expects_phy(pl))
2299 		return 0;
2300 
2301 	phy_fwnode = fwnode_get_phy_node(fwnode);
2302 	if (IS_ERR(phy_fwnode)) {
2303 		/* PHY mode requires a PHY to be specified. */
2304 		if (pl->cfg_link_an_mode == MLO_AN_PHY)
2305 			return -ENODEV;
2306 		return 0;
2307 	}
2308 
2309 	phy_dev = fwnode_phy_find_device(phy_fwnode);
2310 	/* We're done with the phy_node handle */
2311 	fwnode_handle_put(phy_fwnode);
2312 	if (!phy_dev)
2313 		return -ENODEV;
2314 
2315 	/* Use PHY device/driver interface */
2316 	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
2317 		pl->link_interface = phy_dev->interface;
2318 		pl->link_config.interface = pl->link_interface;
2319 	}
2320 
2321 	if (pl->config->mac_requires_rxc)
2322 		flags |= PHY_F_RXC_ALWAYS_ON;
2323 
2324 	ret = phy_attach_direct(pl->netdev, phy_dev, flags,
2325 				pl->link_interface);
2326 	phy_device_free(phy_dev);
2327 	if (ret)
2328 		return ret;
2329 
2330 	ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
2331 	if (ret)
2332 		phy_detach(phy_dev);
2333 
2334 	return ret;
2335 }
2336 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
2337 
2338 /**
2339  * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
2340  *   instance.
2341  * @pl: a pointer to a &struct phylink returned from phylink_create()
2342  *
2343  * Disconnect any current PHY from the phylink instance described by @pl.
2344  */
2345 void phylink_disconnect_phy(struct phylink *pl)
2346 {
2347 	struct phy_device *phy;
2348 
2349 	ASSERT_RTNL();
2350 
2351 	mutex_lock(&pl->phydev_mutex);
2352 	phy = pl->phydev;
2353 	if (phy) {
2354 		mutex_lock(&phy->lock);
2355 		mutex_lock(&pl->state_mutex);
2356 		pl->phydev = NULL;
2357 		pl->phy_enable_tx_lpi = false;
2358 		pl->mac_tx_clk_stop = false;
2359 		mutex_unlock(&pl->state_mutex);
2360 		mutex_unlock(&phy->lock);
2361 	}
2362 	mutex_unlock(&pl->phydev_mutex);
2363 
2364 	if (phy) {
2365 		flush_work(&pl->resolve);
2366 		phy_disconnect(phy);
2367 	}
2368 }
2369 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
2370 
2371 static void phylink_link_changed(struct phylink *pl, bool up, const char *what)
2372 {
2373 	if (!up)
2374 		pl->link_failed = true;
2375 	phylink_run_resolve(pl);
2376 	phylink_dbg(pl, "%s link %s\n", what, up ? "up" : "down");
2377 }
2378 
2379 /**
2380  * phylink_mac_change() - notify phylink of a change in MAC state
2381  * @pl: a pointer to a &struct phylink returned from phylink_create()
2382  * @up: indicates whether the link is currently up.
2383  *
2384  * The MAC driver should call this driver when the state of its link
2385  * changes (eg, link failure, new negotiation results, etc.)
2386  */
2387 void phylink_mac_change(struct phylink *pl, bool up)
2388 {
2389 	phylink_link_changed(pl, up, "mac");
2390 }
2391 EXPORT_SYMBOL_GPL(phylink_mac_change);
2392 
2393 /**
2394  * phylink_pcs_change() - notify phylink of a change to PCS link state
2395  * @pcs: pointer to &struct phylink_pcs
2396  * @up: indicates whether the link is currently up.
2397  *
2398  * The PCS driver should call this when the state of its link changes
2399  * (e.g. link failure, new negotiation results, etc.) Note: it should
2400  * not determine "up" by reading the BMSR. If in doubt about the link
2401  * state at interrupt time, then pass true if pcs_get_state() returns
2402  * the latched link-down state, otherwise pass false.
2403  */
2404 void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
2405 {
2406 	struct phylink *pl = pcs->phylink;
2407 
2408 	if (pl)
2409 		phylink_link_changed(pl, up, "pcs");
2410 }
2411 EXPORT_SYMBOL_GPL(phylink_pcs_change);
2412 
2413 static irqreturn_t phylink_link_handler(int irq, void *data)
2414 {
2415 	struct phylink *pl = data;
2416 
2417 	phylink_run_resolve(pl);
2418 
2419 	return IRQ_HANDLED;
2420 }
2421 
2422 /**
2423  * phylink_start() - start a phylink instance
2424  * @pl: a pointer to a &struct phylink returned from phylink_create()
2425  *
2426  * Start the phylink instance specified by @pl, configuring the MAC for the
2427  * desired link mode(s) and negotiation style. This should be called from the
2428  * network device driver's &struct net_device_ops ndo_open() method.
2429  */
2430 void phylink_start(struct phylink *pl)
2431 {
2432 	bool poll = false;
2433 
2434 	ASSERT_RTNL();
2435 
2436 	phylink_info(pl, "configuring for %s/%s link mode\n",
2437 		     phylink_an_mode_str(pl->req_link_an_mode),
2438 		     phy_modes(pl->link_config.interface));
2439 
2440 	/* Always set the carrier off */
2441 	if (pl->netdev)
2442 		netif_carrier_off(pl->netdev);
2443 
2444 	pl->pcs_state = PCS_STATE_STARTING;
2445 
2446 	/* Apply the link configuration to the MAC when starting. This allows
2447 	 * a fixed-link to start with the correct parameters, and also
2448 	 * ensures that we set the appropriate advertisement for Serdes links.
2449 	 *
2450 	 * Restart autonegotiation if using 802.3z to ensure that the link
2451 	 * parameters are properly negotiated.  This is necessary for DSA
2452 	 * switches using 802.3z negotiation to ensure they see our modes.
2453 	 */
2454 	phylink_mac_initial_config(pl, true);
2455 
2456 	pl->pcs_state = PCS_STATE_STARTED;
2457 
2458 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
2459 
2460 	if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
2461 		int irq = gpiod_to_irq(pl->link_gpio);
2462 
2463 		if (irq > 0) {
2464 			if (!request_irq(irq, phylink_link_handler,
2465 					 IRQF_TRIGGER_RISING |
2466 					 IRQF_TRIGGER_FALLING,
2467 					 "netdev link", pl))
2468 				pl->link_irq = irq;
2469 			else
2470 				irq = 0;
2471 		}
2472 		if (irq <= 0)
2473 			poll = true;
2474 	}
2475 
2476 	if (pl->cfg_link_an_mode == MLO_AN_FIXED)
2477 		poll |= pl->config->poll_fixed_state;
2478 
2479 	if (poll)
2480 		mod_timer(&pl->link_poll, jiffies + HZ);
2481 	if (pl->phydev)
2482 		phy_start(pl->phydev);
2483 	if (pl->sfp_bus)
2484 		sfp_upstream_start(pl->sfp_bus);
2485 }
2486 EXPORT_SYMBOL_GPL(phylink_start);
2487 
2488 /**
2489  * phylink_stop() - stop a phylink instance
2490  * @pl: a pointer to a &struct phylink returned from phylink_create()
2491  *
2492  * Stop the phylink instance specified by @pl. This should be called from the
2493  * network device driver's &struct net_device_ops ndo_stop() method.  The
2494  * network device's carrier state should not be changed prior to calling this
2495  * function.
2496  *
2497  * This will synchronously bring down the link if the link is not already
2498  * down (in other words, it will trigger a mac_link_down() method call.)
2499  */
2500 void phylink_stop(struct phylink *pl)
2501 {
2502 	ASSERT_RTNL();
2503 
2504 	if (pl->sfp_bus)
2505 		sfp_upstream_stop(pl->sfp_bus);
2506 	if (pl->phydev)
2507 		phy_stop(pl->phydev);
2508 	timer_delete_sync(&pl->link_poll);
2509 	if (pl->link_irq) {
2510 		free_irq(pl->link_irq, pl);
2511 		pl->link_irq = 0;
2512 	}
2513 
2514 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
2515 
2516 	pl->pcs_state = PCS_STATE_DOWN;
2517 
2518 	phylink_pcs_disable(pl->pcs);
2519 }
2520 EXPORT_SYMBOL_GPL(phylink_stop);
2521 
2522 /**
2523  * phylink_rx_clk_stop_block() - block PHY ability to stop receive clock in LPI
2524  * @pl: a pointer to a &struct phylink returned from phylink_create()
2525  *
2526  * Disable the PHY's ability to stop the receive clock while the receive path
2527  * is in EEE LPI state, until the number of calls to phylink_rx_clk_stop_block()
2528  * are balanced by calls to phylink_rx_clk_stop_unblock().
2529  */
2530 void phylink_rx_clk_stop_block(struct phylink *pl)
2531 {
2532 	ASSERT_RTNL();
2533 
2534 	if (pl->mac_rx_clk_stop_blocked == U8_MAX) {
2535 		phylink_warn(pl, "%s called too many times - ignoring\n",
2536 			     __func__);
2537 		dump_stack();
2538 		return;
2539 	}
2540 
2541 	/* Disable PHY receive clock stop if this is the first time this
2542 	 * function has been called and clock-stop was previously enabled.
2543 	 */
2544 	if (pl->mac_rx_clk_stop_blocked++ == 0 &&
2545 	    pl->mac_supports_eee_ops && pl->phydev &&
2546 	    pl->config->eee_rx_clk_stop_enable)
2547 		phy_eee_rx_clock_stop(pl->phydev, false);
2548 }
2549 EXPORT_SYMBOL_GPL(phylink_rx_clk_stop_block);
2550 
2551 /**
2552  * phylink_rx_clk_stop_unblock() - unblock PHY ability to stop receive clock
2553  * @pl: a pointer to a &struct phylink returned from phylink_create()
2554  *
2555  * All calls to phylink_rx_clk_stop_block() must be balanced with a
2556  * corresponding call to phylink_rx_clk_stop_unblock() to restore the PHYs
2557  * ability to stop the receive clock when the receive path is in EEE LPI mode.
2558  */
2559 void phylink_rx_clk_stop_unblock(struct phylink *pl)
2560 {
2561 	ASSERT_RTNL();
2562 
2563 	if (pl->mac_rx_clk_stop_blocked == 0) {
2564 		phylink_warn(pl, "%s called too many times - ignoring\n",
2565 			     __func__);
2566 		dump_stack();
2567 		return;
2568 	}
2569 
2570 	/* Re-enable PHY receive clock stop if the number of unblocks matches
2571 	 * the number of calls to the block function above.
2572 	 */
2573 	if (--pl->mac_rx_clk_stop_blocked == 0 &&
2574 	    pl->mac_supports_eee_ops && pl->phydev &&
2575 	    pl->config->eee_rx_clk_stop_enable)
2576 		phy_eee_rx_clock_stop(pl->phydev, true);
2577 }
2578 EXPORT_SYMBOL_GPL(phylink_rx_clk_stop_unblock);
2579 
2580 static bool phylink_mac_supports_wol(struct phylink *pl)
2581 {
2582 	return !!pl->mac_ops->mac_wol_set;
2583 }
2584 
2585 static bool phylink_phy_supports_wol(struct phylink *pl,
2586 				     struct phy_device *phydev)
2587 {
2588 	return phydev && (pl->config->wol_phy_legacy || phy_can_wakeup(phydev));
2589 }
2590 
2591 static bool phylink_phy_pm_speed_ctrl(struct phylink *pl)
2592 {
2593 	return pl->config->wol_phy_speed_ctrl && !pl->wolopts_mac &&
2594 	       pl->phydev && phy_may_wakeup(pl->phydev);
2595 }
2596 
2597 /**
2598  * phylink_suspend() - handle a network device suspend event
2599  * @pl: a pointer to a &struct phylink returned from phylink_create()
2600  * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
2601  *
2602  * Handle a network device suspend event. There are several cases:
2603  *
2604  * - If Wake-on-Lan is not active, we can bring down the link between
2605  *   the MAC and PHY by calling phylink_stop().
2606  * - If Wake-on-Lan is active, and being handled only by the PHY, we
2607  *   can also bring down the link between the MAC and PHY.
2608  * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
2609  *   still needs to receive packets, so we can not bring the link down.
2610  *
2611  * Note: when phylink managed Wake-on-Lan is in use, @mac_wol is ignored.
2612  * (struct phylink_mac_ops.mac_set_wol populated.)
2613  */
2614 void phylink_suspend(struct phylink *pl, bool mac_wol)
2615 {
2616 	ASSERT_RTNL();
2617 
2618 	if (phylink_mac_supports_wol(pl))
2619 		mac_wol = !!pl->wolopts_mac;
2620 
2621 	if (mac_wol && (!pl->netdev || pl->netdev->ethtool->wol_enabled)) {
2622 		/* Wake-on-Lan enabled, MAC handling */
2623 		mutex_lock(&pl->state_mutex);
2624 
2625 		/* Stop the resolver bringing the link up */
2626 		__set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
2627 
2628 		pl->suspend_link_up = phylink_link_is_up(pl);
2629 		if (pl->suspend_link_up) {
2630 			/* Disable the carrier, to prevent transmit timeouts,
2631 			 * but one would hope all packets have been sent. This
2632 			 * also means phylink_resolve() will do nothing.
2633 			 */
2634 			if (pl->netdev)
2635 				netif_carrier_off(pl->netdev);
2636 			pl->old_link_state = false;
2637 		}
2638 
2639 		/* We do not call mac_link_down() here as we want the
2640 		 * link to remain up to receive the WoL packets.
2641 		 */
2642 		mutex_unlock(&pl->state_mutex);
2643 	} else {
2644 		phylink_stop(pl);
2645 	}
2646 
2647 	if (phylink_phy_pm_speed_ctrl(pl))
2648 		phylink_speed_down(pl, false);
2649 }
2650 EXPORT_SYMBOL_GPL(phylink_suspend);
2651 
2652 /**
2653  * phylink_prepare_resume() - prepare to resume a network device
2654  * @pl: a pointer to a &struct phylink returned from phylink_create()
2655  *
2656  * Optional, but if called must be called prior to phylink_resume().
2657  *
2658  * Prepare to resume a network device, preparing the PHY as necessary.
2659  */
2660 void phylink_prepare_resume(struct phylink *pl)
2661 {
2662 	struct phy_device *phydev = pl->phydev;
2663 
2664 	ASSERT_RTNL();
2665 
2666 	/* IEEE 802.3 22.2.4.1.5 allows PHYs to stop their receive clock
2667 	 * when PDOWN is set. However, some MACs require RXC to be running
2668 	 * in order to resume. If the MAC requires RXC, and we have a PHY,
2669 	 * then resume the PHY. Note that 802.3 allows PHYs 500ms before
2670 	 * the clock meets requirements. We do not implement this delay.
2671 	 */
2672 	if (pl->config->mac_requires_rxc && phydev && phydev->suspended)
2673 		phy_resume(phydev);
2674 }
2675 EXPORT_SYMBOL_GPL(phylink_prepare_resume);
2676 
2677 /**
2678  * phylink_resume() - handle a network device resume event
2679  * @pl: a pointer to a &struct phylink returned from phylink_create()
2680  *
2681  * Undo the effects of phylink_suspend(), returning the link to an
2682  * operational state.
2683  */
2684 void phylink_resume(struct phylink *pl)
2685 {
2686 	ASSERT_RTNL();
2687 
2688 	if (phylink_phy_pm_speed_ctrl(pl))
2689 		phylink_speed_up(pl);
2690 
2691 	if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
2692 		/* Wake-on-Lan enabled, MAC handling */
2693 
2694 		if (pl->suspend_link_up) {
2695 			/* Call mac_link_down() so we keep the overall state
2696 			 * balanced. Do this under the state_mutex lock for
2697 			 * consistency. This will cause a "Link Down" message
2698 			 * to be printed during resume, which is harmless -
2699 			 * the true link state will be printed when we run a
2700 			 * resolve.
2701 			 */
2702 			mutex_lock(&pl->state_mutex);
2703 			phylink_link_down(pl);
2704 			mutex_unlock(&pl->state_mutex);
2705 		}
2706 
2707 		/* Re-apply the link parameters so that all the settings get
2708 		 * restored to the MAC.
2709 		 */
2710 		phylink_mac_initial_config(pl, true);
2711 
2712 		/* Re-enable and re-resolve the link parameters */
2713 		phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL);
2714 	} else {
2715 		phylink_start(pl);
2716 	}
2717 }
2718 EXPORT_SYMBOL_GPL(phylink_resume);
2719 
2720 /**
2721  * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
2722  * @pl: a pointer to a &struct phylink returned from phylink_create()
2723  * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
2724  *
2725  * Read the wake on lan parameters from the PHY attached to the phylink
2726  * instance specified by @pl. If no PHY is currently attached, report no
2727  * support for wake on lan.
2728  */
2729 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2730 {
2731 	ASSERT_RTNL();
2732 
2733 	wol->supported = 0;
2734 	wol->wolopts = 0;
2735 
2736 	if (phylink_mac_supports_wol(pl)) {
2737 		if (phylink_phy_supports_wol(pl, pl->phydev))
2738 			phy_ethtool_get_wol(pl->phydev, wol);
2739 
2740 		/* Where the MAC augments the WoL support, merge its support and
2741 		 * current configuration.
2742 		 */
2743 		if (~wol->wolopts & pl->wolopts_mac & WAKE_MAGICSECURE)
2744 			memcpy(wol->sopass, pl->wol_sopass,
2745 			       sizeof(wol->sopass));
2746 
2747 		wol->supported |= pl->config->wol_mac_support;
2748 		wol->wolopts |= pl->wolopts_mac;
2749 	} else {
2750 		/* Legacy */
2751 		if (pl->phydev)
2752 			phy_ethtool_get_wol(pl->phydev, wol);
2753 	}
2754 }
2755 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
2756 
2757 /**
2758  * phylink_ethtool_set_wol() - set wake on lan parameters
2759  * @pl: a pointer to a &struct phylink returned from phylink_create()
2760  * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
2761  *
2762  * Set the wake on lan parameters for the PHY attached to the phylink
2763  * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
2764  * error.
2765  *
2766  * Returns zero on success or negative errno code.
2767  */
2768 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2769 {
2770 	struct ethtool_wolinfo w = { .cmd = ETHTOOL_GWOL };
2771 	int ret = -EOPNOTSUPP;
2772 	bool changed;
2773 	u32 wolopts;
2774 
2775 	ASSERT_RTNL();
2776 
2777 	if (phylink_mac_supports_wol(pl)) {
2778 		wolopts = wol->wolopts;
2779 
2780 		if (phylink_phy_supports_wol(pl, pl->phydev)) {
2781 			ret = phy_ethtool_set_wol(pl->phydev, wol);
2782 			if (ret != 0 && ret != -EOPNOTSUPP)
2783 				return ret;
2784 
2785 			phy_ethtool_get_wol(pl->phydev, &w);
2786 
2787 			/* Any Wake-on-Lan modes which the PHY is handling
2788 			 * should not be passed on to the MAC.
2789 			 */
2790 			wolopts &= ~w.wolopts;
2791 		}
2792 
2793 		wolopts &= pl->config->wol_mac_support;
2794 		changed = pl->wolopts_mac != wolopts;
2795 		if (wolopts & WAKE_MAGICSECURE)
2796 			changed |= !!memcmp(wol->sopass, pl->wol_sopass,
2797 					    sizeof(wol->sopass));
2798 		memcpy(pl->wol_sopass, wol->sopass, sizeof(pl->wol_sopass));
2799 
2800 		if (changed) {
2801 			ret = pl->mac_ops->mac_wol_set(pl->config, wolopts,
2802 						       wol->sopass);
2803 			if (!ret)
2804 				pl->wolopts_mac = wolopts;
2805 		} else {
2806 			ret = 0;
2807 		}
2808 	} else {
2809 		if (pl->phydev)
2810 			ret = phy_ethtool_set_wol(pl->phydev, wol);
2811 	}
2812 
2813 	return ret;
2814 }
2815 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
2816 
2817 static phy_interface_t phylink_sfp_select_interface(struct phylink *pl,
2818 						const unsigned long *link_modes)
2819 {
2820 	phy_interface_t interface;
2821 
2822 	interface = sfp_select_interface(pl->sfp_bus, link_modes);
2823 	if (interface == PHY_INTERFACE_MODE_NA) {
2824 		phylink_err(pl,
2825 			    "selection of interface failed, advertisement %*pb\n",
2826 			    __ETHTOOL_LINK_MODE_MASK_NBITS,
2827 			    link_modes);
2828 		return interface;
2829 	}
2830 
2831 	if (!test_bit(interface, pl->config->supported_interfaces)) {
2832 		phylink_err(pl,
2833 			    "selection of interface failed, SFP selected %s (%u) but MAC supports %*pbl\n",
2834 			    phy_modes(interface), interface,
2835 			    (int)PHY_INTERFACE_MODE_MAX,
2836 			    pl->config->supported_interfaces);
2837 		return PHY_INTERFACE_MODE_NA;
2838 	}
2839 
2840 	return interface;
2841 }
2842 
2843 static phy_interface_t phylink_sfp_select_interface_speed(struct phylink *pl,
2844 							  u32 speed)
2845 {
2846 	phy_interface_t best_interface = PHY_INTERFACE_MODE_NA;
2847 	phy_interface_t interface;
2848 	u32 max_speed;
2849 	int i;
2850 
2851 	for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++) {
2852 		interface = phylink_sfp_interface_preference[i];
2853 		if (!test_bit(interface, pl->sfp_interfaces))
2854 			continue;
2855 
2856 		max_speed = phylink_interface_max_speed(interface);
2857 
2858 		/* The logic here is: if speed == max_speed, then we've found
2859 		 * the best interface. Otherwise we find the interface that
2860 		 * can just support the requested speed.
2861 		 */
2862 		if (max_speed >= speed)
2863 			best_interface = interface;
2864 
2865 		if (max_speed <= speed)
2866 			break;
2867 	}
2868 
2869 	if (best_interface == PHY_INTERFACE_MODE_NA)
2870 		phylink_err(pl, "selection of interface failed, speed %u\n",
2871 			    speed);
2872 
2873 	return best_interface;
2874 }
2875 
2876 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
2877 {
2878 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
2879 
2880 	linkmode_zero(mask);
2881 	phylink_set_port_modes(mask);
2882 
2883 	linkmode_and(dst, dst, mask);
2884 	linkmode_or(dst, dst, b);
2885 }
2886 
2887 static void phylink_get_ksettings(const struct phylink_link_state *state,
2888 				  struct ethtool_link_ksettings *kset)
2889 {
2890 	phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
2891 	linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
2892 	if (kset->base.rate_matching == RATE_MATCH_NONE) {
2893 		kset->base.speed = state->speed;
2894 		kset->base.duplex = state->duplex;
2895 	}
2896 	kset->base.autoneg = linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2897 					       state->advertising) ?
2898 				AUTONEG_ENABLE : AUTONEG_DISABLE;
2899 }
2900 
2901 /**
2902  * phylink_ethtool_ksettings_get() - get the current link settings
2903  * @pl: a pointer to a &struct phylink returned from phylink_create()
2904  * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
2905  *
2906  * Read the current link settings for the phylink instance specified by @pl.
2907  * This will be the link settings read from the MAC, PHY or fixed link
2908  * settings depending on the current negotiation mode.
2909  */
2910 int phylink_ethtool_ksettings_get(struct phylink *pl,
2911 				  struct ethtool_link_ksettings *kset)
2912 {
2913 	struct phylink_link_state link_state;
2914 
2915 	ASSERT_RTNL();
2916 
2917 	if (pl->phydev)
2918 		phy_ethtool_ksettings_get(pl->phydev, kset);
2919 	else
2920 		kset->base.port = pl->link_port;
2921 
2922 	linkmode_copy(kset->link_modes.supported, pl->supported);
2923 
2924 	switch (pl->act_link_an_mode) {
2925 	case MLO_AN_FIXED:
2926 		/* We are using fixed settings. Report these as the
2927 		 * current link settings - and note that these also
2928 		 * represent the supported speeds/duplex/pause modes.
2929 		 */
2930 		phylink_get_fixed_state(pl, &link_state);
2931 		phylink_get_ksettings(&link_state, kset);
2932 		break;
2933 
2934 	case MLO_AN_INBAND:
2935 		/* If there is a phy attached, then use the reported
2936 		 * settings from the phy with no modification.
2937 		 */
2938 		if (pl->phydev)
2939 			break;
2940 
2941 		phylink_mac_pcs_get_state(pl, &link_state);
2942 
2943 		/* The MAC is reporting the link results from its own PCS
2944 		 * layer via in-band status. Report these as the current
2945 		 * link settings.
2946 		 */
2947 		phylink_get_ksettings(&link_state, kset);
2948 		break;
2949 	}
2950 
2951 	return 0;
2952 }
2953 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
2954 
2955 static bool phylink_validate_pcs_inband_autoneg(struct phylink *pl,
2956 					        phy_interface_t interface,
2957 						unsigned long *adv)
2958 {
2959 	unsigned int inband = phylink_inband_caps(pl, interface);
2960 	unsigned int mask;
2961 
2962 	/* If the PCS doesn't implement inband support, be permissive. */
2963 	if (!inband)
2964 		return true;
2965 
2966 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv))
2967 		mask = LINK_INBAND_ENABLE;
2968 	else
2969 		mask = LINK_INBAND_DISABLE;
2970 
2971 	/* Check whether the PCS implements the required mode */
2972 	return !!(inband & mask);
2973 }
2974 
2975 /**
2976  * phylink_ethtool_ksettings_set() - set the link settings
2977  * @pl: a pointer to a &struct phylink returned from phylink_create()
2978  * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
2979  */
2980 int phylink_ethtool_ksettings_set(struct phylink *pl,
2981 				  const struct ethtool_link_ksettings *kset)
2982 {
2983 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2984 	const struct link_capabilities *c;
2985 	struct phylink_link_state config;
2986 
2987 	ASSERT_RTNL();
2988 
2989 	if (pl->phydev) {
2990 		struct ethtool_link_ksettings phy_kset = *kset;
2991 
2992 		linkmode_and(phy_kset.link_modes.advertising,
2993 			     phy_kset.link_modes.advertising,
2994 			     pl->supported);
2995 
2996 		/* We can rely on phylib for this update; we also do not need
2997 		 * to update the pl->link_config settings:
2998 		 * - the configuration returned via ksettings_get() will come
2999 		 *   from phylib whenever a PHY is present.
3000 		 * - link_config.interface will be updated by the PHY calling
3001 		 *   back via phylink_phy_change() and a subsequent resolve.
3002 		 * - initial link configuration for PHY mode comes from the
3003 		 *   last phy state updated via phylink_phy_change().
3004 		 * - other configuration changes (e.g. pause modes) are
3005 		 *   performed directly via phylib.
3006 		 * - if in in-band mode with a PHY, the link configuration
3007 		 *   is passed on the link from the PHY, and all of
3008 		 *   link_config.{speed,duplex,an_enabled,pause} are not used.
3009 		 * - the only possible use would be link_config.advertising
3010 		 *   pause modes when in 1000base-X mode with a PHY, but in
3011 		 *   the presence of a PHY, this should not be changed as that
3012 		 *   should be determined from the media side advertisement.
3013 		 */
3014 		return phy_ethtool_ksettings_set(pl->phydev, &phy_kset);
3015 	}
3016 
3017 	config = pl->link_config;
3018 	/* Mask out unsupported advertisements */
3019 	linkmode_and(config.advertising, kset->link_modes.advertising,
3020 		     pl->supported);
3021 
3022 	/* FIXME: should we reject autoneg if phy/mac does not support it? */
3023 	switch (kset->base.autoneg) {
3024 	case AUTONEG_DISABLE:
3025 		/* Autonegotiation disabled, select a suitable speed and
3026 		 * duplex.
3027 		 */
3028 		c = phy_caps_lookup(kset->base.speed, kset->base.duplex,
3029 				    pl->supported, false);
3030 		if (!c)
3031 			return -EINVAL;
3032 
3033 		/* If we have a fixed link, refuse to change link parameters.
3034 		 * If the link parameters match, accept them but do nothing.
3035 		 */
3036 		if (pl->req_link_an_mode == MLO_AN_FIXED) {
3037 			if (c->speed != pl->link_config.speed ||
3038 			    c->duplex != pl->link_config.duplex)
3039 				return -EINVAL;
3040 			return 0;
3041 		}
3042 
3043 		config.speed = c->speed;
3044 		config.duplex = c->duplex;
3045 		break;
3046 
3047 	case AUTONEG_ENABLE:
3048 		/* If we have a fixed link, allow autonegotiation (since that
3049 		 * is our default case) but do not allow the advertisement to
3050 		 * be changed. If the advertisement matches, simply return.
3051 		 */
3052 		if (pl->req_link_an_mode == MLO_AN_FIXED) {
3053 			if (!linkmode_equal(config.advertising,
3054 					    pl->link_config.advertising))
3055 				return -EINVAL;
3056 			return 0;
3057 		}
3058 
3059 		config.speed = SPEED_UNKNOWN;
3060 		config.duplex = DUPLEX_UNKNOWN;
3061 		break;
3062 
3063 	default:
3064 		return -EINVAL;
3065 	}
3066 
3067 	/* We have ruled out the case with a PHY attached, and the
3068 	 * fixed-link cases.  All that is left are in-band links.
3069 	 */
3070 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
3071 			 kset->base.autoneg == AUTONEG_ENABLE);
3072 
3073 	/* If this link is with an SFP, ensure that changes to advertised modes
3074 	 * also cause the associated interface to be selected such that the
3075 	 * link can be configured correctly.
3076 	 */
3077 	if (pl->sfp_bus) {
3078 		if (kset->base.autoneg == AUTONEG_ENABLE)
3079 			config.interface =
3080 				phylink_sfp_select_interface(pl,
3081 							config.advertising);
3082 		else
3083 			config.interface =
3084 				phylink_sfp_select_interface_speed(pl,
3085 							config.speed);
3086 		if (config.interface == PHY_INTERFACE_MODE_NA)
3087 			return -EINVAL;
3088 
3089 		/* Revalidate with the selected interface */
3090 		linkmode_copy(support, pl->supported);
3091 		if (phylink_validate(pl, support, &config)) {
3092 			phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
3093 				    phylink_an_mode_str(pl->req_link_an_mode),
3094 				    phy_modes(config.interface),
3095 				    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
3096 			return -EINVAL;
3097 		}
3098 	} else {
3099 		/* Validate without changing the current supported mask. */
3100 		linkmode_copy(support, pl->supported);
3101 		if (phylink_validate(pl, support, &config))
3102 			return -EINVAL;
3103 	}
3104 
3105 	/* If autonegotiation is enabled, we must have an advertisement */
3106 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3107 			      config.advertising) &&
3108 	    phylink_is_empty_linkmode(config.advertising))
3109 		return -EINVAL;
3110 
3111 	/* Validate the autonegotiation state. We don't have a PHY in this
3112 	 * situation, so the PCS is the media-facing entity.
3113 	 */
3114 	if (!phylink_validate_pcs_inband_autoneg(pl, config.interface,
3115 						 config.advertising))
3116 		return -EINVAL;
3117 
3118 	mutex_lock(&pl->state_mutex);
3119 	pl->link_config.speed = config.speed;
3120 	pl->link_config.duplex = config.duplex;
3121 
3122 	if (pl->link_config.interface != config.interface) {
3123 		/* The interface changed, e.g. 1000base-X <-> 2500base-X */
3124 		/* We need to force the link down, then change the interface */
3125 		if (pl->old_link_state) {
3126 			phylink_link_down(pl);
3127 			pl->old_link_state = false;
3128 		}
3129 		if (!test_bit(PHYLINK_DISABLE_STOPPED,
3130 			      &pl->phylink_disable_state))
3131 			phylink_major_config(pl, false, &config);
3132 		pl->link_config.interface = config.interface;
3133 		linkmode_copy(pl->link_config.advertising, config.advertising);
3134 	} else if (!linkmode_equal(pl->link_config.advertising,
3135 				   config.advertising)) {
3136 		linkmode_copy(pl->link_config.advertising, config.advertising);
3137 		phylink_change_inband_advert(pl);
3138 	}
3139 	mutex_unlock(&pl->state_mutex);
3140 
3141 	return 0;
3142 }
3143 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
3144 
3145 /**
3146  * phylink_ethtool_nway_reset() - restart negotiation
3147  * @pl: a pointer to a &struct phylink returned from phylink_create()
3148  *
3149  * Restart negotiation for the phylink instance specified by @pl. This will
3150  * cause any attached phy to restart negotiation with the link partner, and
3151  * if the MAC is in a BaseX mode, the MAC will also be requested to restart
3152  * negotiation.
3153  *
3154  * Returns zero on success, or negative error code.
3155  */
3156 int phylink_ethtool_nway_reset(struct phylink *pl)
3157 {
3158 	int ret = 0;
3159 
3160 	ASSERT_RTNL();
3161 
3162 	if (pl->phydev)
3163 		ret = phy_restart_aneg(pl->phydev);
3164 	phylink_pcs_an_restart(pl);
3165 
3166 	return ret;
3167 }
3168 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
3169 
3170 /**
3171  * phylink_ethtool_get_pauseparam() - get the current pause parameters
3172  * @pl: a pointer to a &struct phylink returned from phylink_create()
3173  * @pause: a pointer to a &struct ethtool_pauseparam
3174  */
3175 void phylink_ethtool_get_pauseparam(struct phylink *pl,
3176 				    struct ethtool_pauseparam *pause)
3177 {
3178 	ASSERT_RTNL();
3179 
3180 	pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
3181 	pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
3182 	pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
3183 }
3184 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
3185 
3186 /**
3187  * phylink_ethtool_set_pauseparam() - set the current pause parameters
3188  * @pl: a pointer to a &struct phylink returned from phylink_create()
3189  * @pause: a pointer to a &struct ethtool_pauseparam
3190  */
3191 int phylink_ethtool_set_pauseparam(struct phylink *pl,
3192 				   struct ethtool_pauseparam *pause)
3193 {
3194 	struct phylink_link_state *config = &pl->link_config;
3195 	bool manual_changed;
3196 	int pause_state;
3197 
3198 	ASSERT_RTNL();
3199 
3200 	if (pl->req_link_an_mode == MLO_AN_FIXED)
3201 		return -EOPNOTSUPP;
3202 
3203 	if (!phylink_test(pl->supported, Pause) &&
3204 	    !phylink_test(pl->supported, Asym_Pause))
3205 		return -EOPNOTSUPP;
3206 
3207 	if (!phylink_test(pl->supported, Asym_Pause) &&
3208 	    pause->rx_pause != pause->tx_pause)
3209 		return -EINVAL;
3210 
3211 	pause_state = 0;
3212 	if (pause->autoneg)
3213 		pause_state |= MLO_PAUSE_AN;
3214 	if (pause->rx_pause)
3215 		pause_state |= MLO_PAUSE_RX;
3216 	if (pause->tx_pause)
3217 		pause_state |= MLO_PAUSE_TX;
3218 
3219 	mutex_lock(&pl->state_mutex);
3220 	/*
3221 	 * See the comments for linkmode_set_pause(), wrt the deficiencies
3222 	 * with the current implementation.  A solution to this issue would
3223 	 * be:
3224 	 * ethtool  Local device
3225 	 *  rx  tx  Pause AsymDir
3226 	 *  0   0   0     0
3227 	 *  1   0   1     1
3228 	 *  0   1   0     1
3229 	 *  1   1   1     1
3230 	 * and then use the ethtool rx/tx enablement status to mask the
3231 	 * rx/tx pause resolution.
3232 	 */
3233 	linkmode_set_pause(config->advertising, pause->tx_pause,
3234 			   pause->rx_pause);
3235 
3236 	manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
3237 			 (!(pause_state & MLO_PAUSE_AN) &&
3238 			   (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
3239 
3240 	config->pause = pause_state;
3241 
3242 	/* Update our in-band advertisement, triggering a renegotiation if
3243 	 * the advertisement changed.
3244 	 */
3245 	if (!pl->phydev)
3246 		phylink_change_inband_advert(pl);
3247 
3248 	mutex_unlock(&pl->state_mutex);
3249 
3250 	/* If we have a PHY, a change of the pause frame advertisement will
3251 	 * cause phylib to renegotiate (if AN is enabled) which will in turn
3252 	 * call our phylink_phy_change() and trigger a resolve.  Note that
3253 	 * we can't hold our state mutex while calling phy_set_asym_pause().
3254 	 */
3255 	if (pl->phydev)
3256 		phy_set_asym_pause(pl->phydev, pause->rx_pause,
3257 				   pause->tx_pause);
3258 
3259 	/* If the manual pause settings changed, make sure we trigger a
3260 	 * resolve to update their state; we can not guarantee that the
3261 	 * link will cycle.
3262 	 */
3263 	if (manual_changed) {
3264 		pl->link_failed = true;
3265 		phylink_run_resolve(pl);
3266 	}
3267 
3268 	return 0;
3269 }
3270 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
3271 
3272 /**
3273  * phylink_get_eee_err() - read the energy efficient ethernet error
3274  *   counter
3275  * @pl: a pointer to a &struct phylink returned from phylink_create().
3276  *
3277  * Read the Energy Efficient Ethernet error counter from the PHY associated
3278  * with the phylink instance specified by @pl.
3279  *
3280  * Returns positive error counter value, or negative error code.
3281  */
3282 int phylink_get_eee_err(struct phylink *pl)
3283 {
3284 	int ret = 0;
3285 
3286 	ASSERT_RTNL();
3287 
3288 	if (pl->phydev)
3289 		ret = phy_get_eee_err(pl->phydev);
3290 
3291 	return ret;
3292 }
3293 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
3294 
3295 /**
3296  * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
3297  * @pl: a pointer to a &struct phylink returned from phylink_create()
3298  * @eee: a pointer to a &struct ethtool_keee for the read parameters
3299  */
3300 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_keee *eee)
3301 {
3302 	int ret = -EOPNOTSUPP;
3303 
3304 	ASSERT_RTNL();
3305 
3306 	if (pl->mac_supports_eee_ops && !pl->mac_supports_eee)
3307 		return ret;
3308 
3309 	if (pl->phydev) {
3310 		ret = phy_ethtool_get_eee(pl->phydev, eee);
3311 		/* Restrict supported linkmode mask */
3312 		if (ret == 0 && pl->mac_supports_eee_ops)
3313 			linkmode_and(eee->supported, eee->supported,
3314 				     pl->supported_lpi);
3315 	}
3316 
3317 	return ret;
3318 }
3319 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
3320 
3321 /**
3322  * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
3323  * @pl: a pointer to a &struct phylink returned from phylink_create()
3324  * @eee: a pointer to a &struct ethtool_keee for the desired parameters
3325  */
3326 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_keee *eee)
3327 {
3328 	bool mac_eee = pl->mac_supports_eee;
3329 	int ret = -EOPNOTSUPP;
3330 
3331 	ASSERT_RTNL();
3332 
3333 	phylink_dbg(pl, "mac %s phylink EEE%s, adv %*pbl, LPI%s timer %uus\n",
3334 		    mac_eee ? "supports" : "does not support",
3335 		    eee->eee_enabled ? ", enabled" : "",
3336 		    __ETHTOOL_LINK_MODE_MASK_NBITS, eee->advertised,
3337 		    eee->tx_lpi_enabled ? " enabled" : "", eee->tx_lpi_timer);
3338 
3339 	if (pl->mac_supports_eee_ops && !mac_eee)
3340 		return ret;
3341 
3342 	if (pl->phydev) {
3343 		/* Restrict advertisement mask */
3344 		if (pl->mac_supports_eee_ops)
3345 			linkmode_and(eee->advertised, eee->advertised,
3346 				     pl->supported_lpi);
3347 		ret = phy_ethtool_set_eee(pl->phydev, eee);
3348 		if (ret == 0)
3349 			eee_to_eeecfg(&pl->eee_cfg, eee);
3350 	}
3351 
3352 	return ret;
3353 }
3354 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
3355 
3356 /* This emulates MII registers for a fixed-mode phy operating as per the
3357  * passed in state. "aneg" defines if we report negotiation is possible.
3358  *
3359  * FIXME: should deal with negotiation state too.
3360  */
3361 static int phylink_mii_emul_read(unsigned int reg,
3362 				 struct phylink_link_state *state)
3363 {
3364 	struct fixed_phy_status fs;
3365 	unsigned long *lpa = state->lp_advertising;
3366 	int val;
3367 
3368 	fs.link = state->link;
3369 	fs.speed = state->speed;
3370 	fs.duplex = state->duplex;
3371 	fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
3372 	fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
3373 
3374 	val = swphy_read_reg(reg, &fs);
3375 	if (reg == MII_BMSR) {
3376 		if (!state->an_complete)
3377 			val &= ~BMSR_ANEGCOMPLETE;
3378 	}
3379 	return val;
3380 }
3381 
3382 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
3383 			    unsigned int reg)
3384 {
3385 	struct phy_device *phydev = pl->phydev;
3386 	int prtad, devad;
3387 
3388 	if (mdio_phy_id_is_c45(phy_id)) {
3389 		prtad = mdio_phy_id_prtad(phy_id);
3390 		devad = mdio_phy_id_devad(phy_id);
3391 		return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
3392 					reg);
3393 	}
3394 
3395 	if (phydev->is_c45) {
3396 		switch (reg) {
3397 		case MII_BMCR:
3398 		case MII_BMSR:
3399 		case MII_PHYSID1:
3400 		case MII_PHYSID2:
3401 			devad = __ffs(phydev->c45_ids.mmds_present);
3402 			break;
3403 		case MII_ADVERTISE:
3404 		case MII_LPA:
3405 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
3406 				return -EINVAL;
3407 			devad = MDIO_MMD_AN;
3408 			if (reg == MII_ADVERTISE)
3409 				reg = MDIO_AN_ADVERTISE;
3410 			else
3411 				reg = MDIO_AN_LPA;
3412 			break;
3413 		default:
3414 			return -EINVAL;
3415 		}
3416 		prtad = phy_id;
3417 		return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
3418 					reg);
3419 	}
3420 
3421 	return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg);
3422 }
3423 
3424 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
3425 			     unsigned int reg, unsigned int val)
3426 {
3427 	struct phy_device *phydev = pl->phydev;
3428 	int prtad, devad;
3429 
3430 	if (mdio_phy_id_is_c45(phy_id)) {
3431 		prtad = mdio_phy_id_prtad(phy_id);
3432 		devad = mdio_phy_id_devad(phy_id);
3433 		return mdiobus_c45_write(pl->phydev->mdio.bus, prtad, devad,
3434 					 reg, val);
3435 	}
3436 
3437 	if (phydev->is_c45) {
3438 		switch (reg) {
3439 		case MII_BMCR:
3440 		case MII_BMSR:
3441 		case MII_PHYSID1:
3442 		case MII_PHYSID2:
3443 			devad = __ffs(phydev->c45_ids.mmds_present);
3444 			break;
3445 		case MII_ADVERTISE:
3446 		case MII_LPA:
3447 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
3448 				return -EINVAL;
3449 			devad = MDIO_MMD_AN;
3450 			if (reg == MII_ADVERTISE)
3451 				reg = MDIO_AN_ADVERTISE;
3452 			else
3453 				reg = MDIO_AN_LPA;
3454 			break;
3455 		default:
3456 			return -EINVAL;
3457 		}
3458 		return mdiobus_c45_write(pl->phydev->mdio.bus, phy_id, devad,
3459 					 reg, val);
3460 	}
3461 
3462 	return mdiobus_write(phydev->mdio.bus, phy_id, reg, val);
3463 }
3464 
3465 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
3466 			    unsigned int reg)
3467 {
3468 	struct phylink_link_state state;
3469 	int val = 0xffff;
3470 
3471 	switch (pl->act_link_an_mode) {
3472 	case MLO_AN_FIXED:
3473 		if (phy_id == 0) {
3474 			phylink_get_fixed_state(pl, &state);
3475 			val = phylink_mii_emul_read(reg, &state);
3476 		}
3477 		break;
3478 
3479 	case MLO_AN_PHY:
3480 		return -EOPNOTSUPP;
3481 
3482 	case MLO_AN_INBAND:
3483 		if (phy_id == 0) {
3484 			phylink_mac_pcs_get_state(pl, &state);
3485 			val = phylink_mii_emul_read(reg, &state);
3486 		}
3487 		break;
3488 	}
3489 
3490 	return val & 0xffff;
3491 }
3492 
3493 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
3494 			     unsigned int reg, unsigned int val)
3495 {
3496 	switch (pl->act_link_an_mode) {
3497 	case MLO_AN_FIXED:
3498 		break;
3499 
3500 	case MLO_AN_PHY:
3501 		return -EOPNOTSUPP;
3502 
3503 	case MLO_AN_INBAND:
3504 		break;
3505 	}
3506 
3507 	return 0;
3508 }
3509 
3510 /**
3511  * phylink_mii_ioctl() - generic mii ioctl interface
3512  * @pl: a pointer to a &struct phylink returned from phylink_create()
3513  * @ifr: a pointer to a &struct ifreq for socket ioctls
3514  * @cmd: ioctl cmd to execute
3515  *
3516  * Perform the specified MII ioctl on the PHY attached to the phylink instance
3517  * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
3518  *
3519  * Returns: zero on success or negative error code.
3520  *
3521  * %SIOCGMIIPHY:
3522  *  read register from the current PHY.
3523  * %SIOCGMIIREG:
3524  *  read register from the specified PHY.
3525  * %SIOCSMIIREG:
3526  *  set a register on the specified PHY.
3527  */
3528 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
3529 {
3530 	struct mii_ioctl_data *mii = if_mii(ifr);
3531 	int  ret;
3532 
3533 	ASSERT_RTNL();
3534 
3535 	if (pl->phydev) {
3536 		/* PHYs only exist for MLO_AN_PHY and SGMII */
3537 		switch (cmd) {
3538 		case SIOCGMIIPHY:
3539 			mii->phy_id = pl->phydev->mdio.addr;
3540 			fallthrough;
3541 
3542 		case SIOCGMIIREG:
3543 			ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
3544 			if (ret >= 0) {
3545 				mii->val_out = ret;
3546 				ret = 0;
3547 			}
3548 			break;
3549 
3550 		case SIOCSMIIREG:
3551 			ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
3552 						mii->val_in);
3553 			break;
3554 
3555 		default:
3556 			ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
3557 			break;
3558 		}
3559 	} else {
3560 		switch (cmd) {
3561 		case SIOCGMIIPHY:
3562 			mii->phy_id = 0;
3563 			fallthrough;
3564 
3565 		case SIOCGMIIREG:
3566 			ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
3567 			if (ret >= 0) {
3568 				mii->val_out = ret;
3569 				ret = 0;
3570 			}
3571 			break;
3572 
3573 		case SIOCSMIIREG:
3574 			ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
3575 						mii->val_in);
3576 			break;
3577 
3578 		default:
3579 			ret = -EOPNOTSUPP;
3580 			break;
3581 		}
3582 	}
3583 
3584 	return ret;
3585 }
3586 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
3587 
3588 /**
3589  * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
3590  *   link partners
3591  * @pl: a pointer to a &struct phylink returned from phylink_create()
3592  * @sync: perform action synchronously
3593  *
3594  * If we have a PHY that is not part of a SFP module, then set the speed
3595  * as described in the phy_speed_down() function. Please see this function
3596  * for a description of the @sync parameter.
3597  *
3598  * Returns zero if there is no PHY, otherwise as per phy_speed_down().
3599  */
3600 int phylink_speed_down(struct phylink *pl, bool sync)
3601 {
3602 	int ret = 0;
3603 
3604 	ASSERT_RTNL();
3605 
3606 	if (!pl->sfp_bus && pl->phydev)
3607 		ret = phy_speed_down(pl->phydev, sync);
3608 
3609 	return ret;
3610 }
3611 EXPORT_SYMBOL_GPL(phylink_speed_down);
3612 
3613 /**
3614  * phylink_speed_up() - restore the advertised speeds prior to the call to
3615  *   phylink_speed_down()
3616  * @pl: a pointer to a &struct phylink returned from phylink_create()
3617  *
3618  * If we have a PHY that is not part of a SFP module, then restore the
3619  * PHY speeds as per phy_speed_up().
3620  *
3621  * Returns zero if there is no PHY, otherwise as per phy_speed_up().
3622  */
3623 int phylink_speed_up(struct phylink *pl)
3624 {
3625 	int ret = 0;
3626 
3627 	ASSERT_RTNL();
3628 
3629 	if (!pl->sfp_bus && pl->phydev)
3630 		ret = phy_speed_up(pl->phydev);
3631 
3632 	return ret;
3633 }
3634 EXPORT_SYMBOL_GPL(phylink_speed_up);
3635 
3636 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
3637 {
3638 	struct phylink *pl = upstream;
3639 
3640 	pl->netdev->sfp_bus = bus;
3641 }
3642 
3643 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
3644 {
3645 	struct phylink *pl = upstream;
3646 
3647 	pl->netdev->sfp_bus = NULL;
3648 }
3649 
3650 static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
3651 						    const unsigned long *intf)
3652 {
3653 	phy_interface_t interface;
3654 	size_t i;
3655 
3656 	interface = PHY_INTERFACE_MODE_NA;
3657 	for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++)
3658 		if (test_bit(phylink_sfp_interface_preference[i], intf)) {
3659 			interface = phylink_sfp_interface_preference[i];
3660 			break;
3661 		}
3662 
3663 	return interface;
3664 }
3665 
3666 static void phylink_sfp_set_config(struct phylink *pl, unsigned long *supported,
3667 				   struct phylink_link_state *state,
3668 				   bool changed)
3669 {
3670 	u8 mode = MLO_AN_INBAND;
3671 
3672 	phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
3673 		    phylink_an_mode_str(mode), phy_modes(state->interface),
3674 		    __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
3675 
3676 	if (!linkmode_equal(pl->supported, supported)) {
3677 		linkmode_copy(pl->supported, supported);
3678 		changed = true;
3679 	}
3680 
3681 	if (!linkmode_equal(pl->link_config.advertising, state->advertising)) {
3682 		linkmode_copy(pl->link_config.advertising, state->advertising);
3683 		changed = true;
3684 	}
3685 
3686 	if (pl->req_link_an_mode != mode ||
3687 	    pl->link_config.interface != state->interface) {
3688 		pl->req_link_an_mode = mode;
3689 		pl->link_config.interface = state->interface;
3690 
3691 		changed = true;
3692 
3693 		phylink_info(pl, "switched to %s/%s link mode\n",
3694 			     phylink_an_mode_str(mode),
3695 			     phy_modes(state->interface));
3696 	}
3697 
3698 	if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
3699 				 &pl->phylink_disable_state))
3700 		phylink_mac_initial_config(pl, false);
3701 }
3702 
3703 static int phylink_sfp_config_phy(struct phylink *pl, struct phy_device *phy)
3704 {
3705 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
3706 	struct phylink_link_state config;
3707 	int ret;
3708 
3709 	/* We're not using pl->sfp_interfaces, so clear it. */
3710 	phy_interface_zero(pl->sfp_interfaces);
3711 	linkmode_copy(support, phy->supported);
3712 
3713 	memset(&config, 0, sizeof(config));
3714 	linkmode_copy(config.advertising, phy->advertising);
3715 	config.interface = PHY_INTERFACE_MODE_NA;
3716 	config.speed = SPEED_UNKNOWN;
3717 	config.duplex = DUPLEX_UNKNOWN;
3718 	config.pause = MLO_PAUSE_AN;
3719 
3720 	/* Ignore errors if we're expecting a PHY to attach later */
3721 	ret = phylink_validate(pl, support, &config);
3722 	if (ret) {
3723 		phylink_err(pl, "validation with support %*pb failed: %pe\n",
3724 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3725 			    ERR_PTR(ret));
3726 		return ret;
3727 	}
3728 
3729 	config.interface = phylink_sfp_select_interface(pl, config.advertising);
3730 	if (config.interface == PHY_INTERFACE_MODE_NA)
3731 		return -EINVAL;
3732 
3733 	/* Attach the PHY so that the PHY is present when we do the major
3734 	 * configuration step.
3735 	 */
3736 	ret = phylink_attach_phy(pl, phy, config.interface);
3737 	if (ret < 0)
3738 		return ret;
3739 
3740 	/* This will validate the configuration for us. */
3741 	ret = phylink_bringup_phy(pl, phy, config.interface);
3742 	if (ret < 0) {
3743 		phy_detach(phy);
3744 		return ret;
3745 	}
3746 
3747 	pl->link_port = pl->sfp_port;
3748 
3749 	phylink_sfp_set_config(pl, support, &config, true);
3750 
3751 	return 0;
3752 }
3753 
3754 static int phylink_sfp_config_optical(struct phylink *pl)
3755 {
3756 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
3757 	struct phylink_link_state config;
3758 	enum inband_type inband_type;
3759 	phy_interface_t interface;
3760 	int ret;
3761 
3762 	phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n",
3763 		    (int)PHY_INTERFACE_MODE_MAX,
3764 		    pl->config->supported_interfaces,
3765 		    (int)PHY_INTERFACE_MODE_MAX,
3766 		    pl->sfp_interfaces);
3767 
3768 	/* Find the union of the supported interfaces by the PCS/MAC and
3769 	 * the SFP module.
3770 	 */
3771 	phy_interface_and(pl->sfp_interfaces, pl->config->supported_interfaces,
3772 			  pl->sfp_interfaces);
3773 	if (phy_interface_empty(pl->sfp_interfaces)) {
3774 		phylink_err(pl, "unsupported SFP module: no common interface modes\n");
3775 		return -EINVAL;
3776 	}
3777 
3778 	memset(&config, 0, sizeof(config));
3779 	linkmode_copy(support, pl->sfp_support);
3780 	linkmode_copy(config.advertising, pl->sfp_support);
3781 	config.speed = SPEED_UNKNOWN;
3782 	config.duplex = DUPLEX_UNKNOWN;
3783 	config.pause = MLO_PAUSE_AN;
3784 
3785 	/* For all the interfaces that are supported, reduce the sfp_support
3786 	 * mask to only those link modes that can be supported.
3787 	 */
3788 	ret = phylink_validate_mask(pl, NULL, pl->sfp_support, &config,
3789 				    pl->sfp_interfaces);
3790 	if (ret) {
3791 		phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
3792 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
3793 		return ret;
3794 	}
3795 
3796 	interface = phylink_choose_sfp_interface(pl, pl->sfp_interfaces);
3797 	if (interface == PHY_INTERFACE_MODE_NA) {
3798 		phylink_err(pl, "failed to select SFP interface\n");
3799 		return -EINVAL;
3800 	}
3801 
3802 	phylink_dbg(pl, "optical SFP: chosen %s interface\n",
3803 		    phy_modes(interface));
3804 
3805 	inband_type = phylink_get_inband_type(interface);
3806 	if (inband_type == INBAND_NONE) {
3807 		/* If this is the sole interface, and there is no inband
3808 		 * support, clear the advertising mask and Autoneg bit in
3809 		 * the support mask. Otherwise, just clear the Autoneg bit
3810 		 * in the advertising mask.
3811 		 */
3812 		if (phy_interface_weight(pl->sfp_interfaces) == 1) {
3813 			linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3814 					   pl->sfp_support);
3815 			linkmode_zero(config.advertising);
3816 		} else {
3817 			linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3818 					   config.advertising);
3819 		}
3820 	}
3821 
3822 	if (!phylink_validate_pcs_inband_autoneg(pl, interface,
3823 						 config.advertising)) {
3824 		phylink_err(pl, "autoneg setting not compatible with PCS");
3825 		return -EINVAL;
3826 	}
3827 
3828 	config.interface = interface;
3829 
3830 	/* Ignore errors if we're expecting a PHY to attach later */
3831 	ret = phylink_validate(pl, support, &config);
3832 	if (ret) {
3833 		phylink_err(pl, "validation with support %*pb failed: %pe\n",
3834 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3835 			    ERR_PTR(ret));
3836 		return ret;
3837 	}
3838 
3839 	pl->link_port = pl->sfp_port;
3840 
3841 	phylink_sfp_set_config(pl, pl->sfp_support, &config, false);
3842 
3843 	return 0;
3844 }
3845 
3846 static int phylink_sfp_module_insert(void *upstream,
3847 				     const struct sfp_eeprom_id *id)
3848 {
3849 	const struct sfp_module_caps *caps;
3850 	struct phylink *pl = upstream;
3851 
3852 	ASSERT_RTNL();
3853 
3854 	caps = sfp_get_module_caps(pl->sfp_bus);
3855 	phy_interface_copy(pl->sfp_interfaces, caps->interfaces);
3856 	linkmode_copy(pl->sfp_support, caps->link_modes);
3857 	pl->sfp_may_have_phy = caps->may_have_phy;
3858 	pl->sfp_port = caps->port;
3859 
3860 	/* If this module may have a PHY connecting later, defer until later */
3861 	if (pl->sfp_may_have_phy)
3862 		return 0;
3863 
3864 	return phylink_sfp_config_optical(pl);
3865 }
3866 
3867 static void phylink_sfp_module_remove(void *upstream)
3868 {
3869 	struct phylink *pl = upstream;
3870 
3871 	phy_interface_zero(pl->sfp_interfaces);
3872 }
3873 
3874 static int phylink_sfp_module_start(void *upstream)
3875 {
3876 	struct phylink *pl = upstream;
3877 
3878 	/* If this SFP module has a PHY, start the PHY now. */
3879 	if (pl->phydev) {
3880 		phy_start(pl->phydev);
3881 		return 0;
3882 	}
3883 
3884 	/* If the module may have a PHY but we didn't detect one we
3885 	 * need to configure the MAC here.
3886 	 */
3887 	if (!pl->sfp_may_have_phy)
3888 		return 0;
3889 
3890 	return phylink_sfp_config_optical(pl);
3891 }
3892 
3893 static void phylink_sfp_module_stop(void *upstream)
3894 {
3895 	struct phylink *pl = upstream;
3896 
3897 	/* If this SFP module has a PHY, stop it. */
3898 	if (pl->phydev)
3899 		phy_stop(pl->phydev);
3900 }
3901 
3902 static void phylink_sfp_link_down(void *upstream)
3903 {
3904 	struct phylink *pl = upstream;
3905 
3906 	ASSERT_RTNL();
3907 
3908 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
3909 }
3910 
3911 static void phylink_sfp_link_up(void *upstream)
3912 {
3913 	struct phylink *pl = upstream;
3914 
3915 	ASSERT_RTNL();
3916 
3917 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
3918 }
3919 
3920 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
3921 {
3922 	struct phylink *pl = upstream;
3923 
3924 	if (!phy->drv) {
3925 		phylink_err(pl, "PHY %s (id 0x%.8lx) has no driver loaded\n",
3926 			    phydev_name(phy), (unsigned long)phy->phy_id);
3927 		phylink_err(pl, "Drivers which handle known common cases: CONFIG_BCM84881_PHY, CONFIG_MARVELL_PHY\n");
3928 		return -EINVAL;
3929 	}
3930 
3931 	/*
3932 	 * This is the new way of dealing with flow control for PHYs,
3933 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
3934 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
3935 	 * using our validate call to the MAC, we rely upon the MAC
3936 	 * clearing the bits from both supported and advertising fields.
3937 	 */
3938 	phy_support_asym_pause(phy);
3939 
3940 	/* Set the PHY's host supported interfaces */
3941 	phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
3942 			  pl->config->supported_interfaces);
3943 
3944 	/* Do the initial configuration */
3945 	return phylink_sfp_config_phy(pl, phy);
3946 }
3947 
3948 static void phylink_sfp_disconnect_phy(void *upstream,
3949 				       struct phy_device *phydev)
3950 {
3951 	phylink_disconnect_phy(upstream);
3952 }
3953 
3954 static const struct sfp_upstream_ops sfp_phylink_ops = {
3955 	.attach = phylink_sfp_attach,
3956 	.detach = phylink_sfp_detach,
3957 	.module_insert = phylink_sfp_module_insert,
3958 	.module_remove = phylink_sfp_module_remove,
3959 	.module_start = phylink_sfp_module_start,
3960 	.module_stop = phylink_sfp_module_stop,
3961 	.link_up = phylink_sfp_link_up,
3962 	.link_down = phylink_sfp_link_down,
3963 	.connect_phy = phylink_sfp_connect_phy,
3964 	.disconnect_phy = phylink_sfp_disconnect_phy,
3965 };
3966 
3967 /* Helpers for MAC drivers */
3968 
3969 static struct {
3970 	int bit;
3971 	int speed;
3972 } phylink_c73_priority_resolution[] = {
3973 	{ ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, SPEED_100000 },
3974 	{ ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, SPEED_100000 },
3975 	/* 100GBASE-KP4 and 100GBASE-CR10 not supported */
3976 	{ ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, SPEED_40000 },
3977 	{ ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, SPEED_40000 },
3978 	{ ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, SPEED_10000 },
3979 	{ ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, SPEED_10000 },
3980 	/* 5GBASE-KR not supported */
3981 	{ ETHTOOL_LINK_MODE_2500baseX_Full_BIT, SPEED_2500 },
3982 	{ ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, SPEED_1000 },
3983 };
3984 
3985 void phylink_resolve_c73(struct phylink_link_state *state)
3986 {
3987 	int i;
3988 
3989 	for (i = 0; i < ARRAY_SIZE(phylink_c73_priority_resolution); i++) {
3990 		int bit = phylink_c73_priority_resolution[i].bit;
3991 		if (linkmode_test_bit(bit, state->advertising) &&
3992 		    linkmode_test_bit(bit, state->lp_advertising))
3993 			break;
3994 	}
3995 
3996 	if (i < ARRAY_SIZE(phylink_c73_priority_resolution)) {
3997 		state->speed = phylink_c73_priority_resolution[i].speed;
3998 		state->duplex = DUPLEX_FULL;
3999 	} else {
4000 		/* negotiation failure */
4001 		state->link = false;
4002 	}
4003 
4004 	phylink_resolve_an_pause(state);
4005 }
4006 EXPORT_SYMBOL_GPL(phylink_resolve_c73);
4007 
4008 static void phylink_decode_c37_word(struct phylink_link_state *state,
4009 				    uint16_t config_reg, int speed)
4010 {
4011 	int fd_bit;
4012 
4013 	if (speed == SPEED_2500)
4014 		fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
4015 	else
4016 		fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
4017 
4018 	mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
4019 
4020 	if (linkmode_test_bit(fd_bit, state->advertising) &&
4021 	    linkmode_test_bit(fd_bit, state->lp_advertising)) {
4022 		state->speed = speed;
4023 		state->duplex = DUPLEX_FULL;
4024 	} else {
4025 		/* negotiation failure */
4026 		state->link = false;
4027 	}
4028 
4029 	phylink_resolve_an_pause(state);
4030 }
4031 
4032 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
4033 				      uint16_t config_reg)
4034 {
4035 	if (!(config_reg & LPA_SGMII_LINK)) {
4036 		state->link = false;
4037 		return;
4038 	}
4039 
4040 	switch (config_reg & LPA_SGMII_SPD_MASK) {
4041 	case LPA_SGMII_10:
4042 		state->speed = SPEED_10;
4043 		break;
4044 	case LPA_SGMII_100:
4045 		state->speed = SPEED_100;
4046 		break;
4047 	case LPA_SGMII_1000:
4048 		state->speed = SPEED_1000;
4049 		break;
4050 	default:
4051 		state->link = false;
4052 		return;
4053 	}
4054 	if (config_reg & LPA_SGMII_FULL_DUPLEX)
4055 		state->duplex = DUPLEX_FULL;
4056 	else
4057 		state->duplex = DUPLEX_HALF;
4058 }
4059 
4060 /**
4061  * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
4062  * @state: a pointer to a struct phylink_link_state.
4063  * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
4064  *
4065  * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
4066  * code word.  Decode the USXGMII code word and populate the corresponding fields
4067  * (speed, duplex) into the phylink_link_state structure.
4068  */
4069 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
4070 				 uint16_t lpa)
4071 {
4072 	switch (lpa & MDIO_USXGMII_SPD_MASK) {
4073 	case MDIO_USXGMII_10:
4074 		state->speed = SPEED_10;
4075 		break;
4076 	case MDIO_USXGMII_100:
4077 		state->speed = SPEED_100;
4078 		break;
4079 	case MDIO_USXGMII_1000:
4080 		state->speed = SPEED_1000;
4081 		break;
4082 	case MDIO_USXGMII_2500:
4083 		state->speed = SPEED_2500;
4084 		break;
4085 	case MDIO_USXGMII_5000:
4086 		state->speed = SPEED_5000;
4087 		break;
4088 	case MDIO_USXGMII_10G:
4089 		state->speed = SPEED_10000;
4090 		break;
4091 	default:
4092 		state->link = false;
4093 		return;
4094 	}
4095 
4096 	if (lpa & MDIO_USXGMII_FULL_DUPLEX)
4097 		state->duplex = DUPLEX_FULL;
4098 	else
4099 		state->duplex = DUPLEX_HALF;
4100 }
4101 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
4102 
4103 /**
4104  * phylink_decode_usgmii_word() - decode the USGMII word from a MAC PCS
4105  * @state: a pointer to a struct phylink_link_state.
4106  * @lpa: a 16 bit value which stores the USGMII auto-negotiation word
4107  *
4108  * Helper for MAC PCS supporting the USGMII protocol and the auto-negotiation
4109  * code word.  Decode the USGMII code word and populate the corresponding fields
4110  * (speed, duplex) into the phylink_link_state structure. The structure for this
4111  * word is the same as the USXGMII word, except it only supports speeds up to
4112  * 1Gbps.
4113  */
4114 static void phylink_decode_usgmii_word(struct phylink_link_state *state,
4115 				       uint16_t lpa)
4116 {
4117 	switch (lpa & MDIO_USXGMII_SPD_MASK) {
4118 	case MDIO_USXGMII_10:
4119 		state->speed = SPEED_10;
4120 		break;
4121 	case MDIO_USXGMII_100:
4122 		state->speed = SPEED_100;
4123 		break;
4124 	case MDIO_USXGMII_1000:
4125 		state->speed = SPEED_1000;
4126 		break;
4127 	default:
4128 		state->link = false;
4129 		return;
4130 	}
4131 
4132 	if (lpa & MDIO_USXGMII_FULL_DUPLEX)
4133 		state->duplex = DUPLEX_FULL;
4134 	else
4135 		state->duplex = DUPLEX_HALF;
4136 }
4137 
4138 /**
4139  * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
4140  * @state: a pointer to a &struct phylink_link_state.
4141  * @neg_mode: link negotiation mode (PHYLINK_PCS_NEG_xxx)
4142  * @bmsr: The value of the %MII_BMSR register
4143  * @lpa: The value of the %MII_LPA register
4144  *
4145  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4146  * clause 37 negotiation and/or SGMII control.
4147  *
4148  * Parse the Clause 37 or Cisco SGMII link partner negotiation word into
4149  * the phylink @state structure. This is suitable to be used for implementing
4150  * the pcs_get_state() member of the struct phylink_pcs_ops structure if
4151  * accessing @bmsr and @lpa cannot be done with MDIO directly.
4152  */
4153 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
4154 				      unsigned int neg_mode, u16 bmsr, u16 lpa)
4155 {
4156 	state->link = !!(bmsr & BMSR_LSTATUS);
4157 	state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
4158 
4159 	/* If the link is down, the advertisement data is undefined. */
4160 	if (!state->link)
4161 		return;
4162 
4163 	switch (state->interface) {
4164 	case PHY_INTERFACE_MODE_1000BASEX:
4165 		if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
4166 			phylink_decode_c37_word(state, lpa, SPEED_1000);
4167 		} else {
4168 			state->speed = SPEED_1000;
4169 			state->duplex = DUPLEX_FULL;
4170 			state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX;
4171 		}
4172 		break;
4173 
4174 	case PHY_INTERFACE_MODE_2500BASEX:
4175 		if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
4176 			phylink_decode_c37_word(state, lpa, SPEED_2500);
4177 		} else {
4178 			state->speed = SPEED_2500;
4179 			state->duplex = DUPLEX_FULL;
4180 			state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX;
4181 		}
4182 		break;
4183 
4184 	case PHY_INTERFACE_MODE_SGMII:
4185 	case PHY_INTERFACE_MODE_QSGMII:
4186 		if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
4187 			phylink_decode_sgmii_word(state, lpa);
4188 		break;
4189 
4190 	case PHY_INTERFACE_MODE_QUSGMII:
4191 		if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
4192 			phylink_decode_usgmii_word(state, lpa);
4193 		break;
4194 
4195 	default:
4196 		state->link = false;
4197 		break;
4198 	}
4199 }
4200 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state);
4201 
4202 /**
4203  * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
4204  * @pcs: a pointer to a &struct mdio_device.
4205  * @neg_mode: link negotiation mode (PHYLINK_PCS_NEG_xxx)
4206  * @state: a pointer to a &struct phylink_link_state.
4207  *
4208  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4209  * clause 37 negotiation and/or SGMII control.
4210  *
4211  * Read the MAC PCS state from the MII device configured in @config and
4212  * parse the Clause 37 or Cisco SGMII link partner negotiation word into
4213  * the phylink @state structure. This is suitable to be directly plugged
4214  * into the pcs_get_state() member of the struct phylink_pcs_ops
4215  * structure.
4216  */
4217 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
4218 				   unsigned int neg_mode,
4219 				   struct phylink_link_state *state)
4220 {
4221 	int bmsr, lpa;
4222 
4223 	bmsr = mdiodev_read(pcs, MII_BMSR);
4224 	lpa = mdiodev_read(pcs, MII_LPA);
4225 	if (bmsr < 0 || lpa < 0) {
4226 		state->link = false;
4227 		return;
4228 	}
4229 
4230 	phylink_mii_c22_pcs_decode_state(state, neg_mode, bmsr, lpa);
4231 }
4232 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
4233 
4234 /**
4235  * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
4236  *	advertisement
4237  * @interface: the PHY interface mode being configured
4238  * @advertising: the ethtool advertisement mask
4239  *
4240  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4241  * clause 37 negotiation and/or SGMII control.
4242  *
4243  * Encode the clause 37 PCS advertisement as specified by @interface and
4244  * @advertising.
4245  *
4246  * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
4247  */
4248 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
4249 					     const unsigned long *advertising)
4250 {
4251 	u16 adv;
4252 
4253 	switch (interface) {
4254 	case PHY_INTERFACE_MODE_1000BASEX:
4255 	case PHY_INTERFACE_MODE_2500BASEX:
4256 		adv = ADVERTISE_1000XFULL;
4257 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
4258 				      advertising))
4259 			adv |= ADVERTISE_1000XPAUSE;
4260 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
4261 				      advertising))
4262 			adv |= ADVERTISE_1000XPSE_ASYM;
4263 		return adv;
4264 	case PHY_INTERFACE_MODE_SGMII:
4265 	case PHY_INTERFACE_MODE_QSGMII:
4266 		return 0x0001;
4267 	default:
4268 		/* Nothing to do for other modes */
4269 		return -EINVAL;
4270 	}
4271 }
4272 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement);
4273 
4274 /**
4275  * phylink_mii_c22_pcs_config() - configure clause 22 PCS
4276  * @pcs: a pointer to a &struct mdio_device.
4277  * @interface: the PHY interface mode being configured
4278  * @advertising: the ethtool advertisement mask
4279  * @neg_mode: PCS negotiation mode
4280  *
4281  * Configure a Clause 22 PCS PHY with the appropriate negotiation
4282  * parameters for the @mode, @interface and @advertising parameters.
4283  * Returns negative error number on failure, zero if the advertisement
4284  * has not changed, or positive if there is a change.
4285  */
4286 int phylink_mii_c22_pcs_config(struct mdio_device *pcs,
4287 			       phy_interface_t interface,
4288 			       const unsigned long *advertising,
4289 			       unsigned int neg_mode)
4290 {
4291 	bool changed = 0;
4292 	u16 bmcr;
4293 	int ret, adv;
4294 
4295 	adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
4296 	if (adv >= 0) {
4297 		ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
4298 					     MII_ADVERTISE, 0xffff, adv);
4299 		if (ret < 0)
4300 			return ret;
4301 		changed = ret;
4302 	}
4303 
4304 	if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
4305 		bmcr = BMCR_ANENABLE;
4306 	else
4307 		bmcr = 0;
4308 
4309 	/* Configure the inband state. Ensure ISOLATE bit is disabled */
4310 	ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
4311 	if (ret < 0)
4312 		return ret;
4313 
4314 	return changed;
4315 }
4316 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
4317 
4318 /**
4319  * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
4320  * @pcs: a pointer to a &struct mdio_device.
4321  *
4322  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4323  * clause 37 negotiation.
4324  *
4325  * Restart the clause 37 negotiation with the link partner. This is
4326  * suitable to be directly plugged into the pcs_get_state() member
4327  * of the struct phylink_pcs_ops structure.
4328  */
4329 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
4330 {
4331 	int val = mdiodev_read(pcs, MII_BMCR);
4332 
4333 	if (val >= 0) {
4334 		val |= BMCR_ANRESTART;
4335 
4336 		mdiodev_write(pcs, MII_BMCR, val);
4337 	}
4338 }
4339 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
4340 
4341 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
4342 				   struct phylink_link_state *state)
4343 {
4344 	struct mii_bus *bus = pcs->bus;
4345 	int addr = pcs->addr;
4346 	int stat;
4347 
4348 	stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
4349 	if (stat < 0) {
4350 		state->link = false;
4351 		return;
4352 	}
4353 
4354 	state->link = !!(stat & MDIO_STAT1_LSTATUS);
4355 	if (!state->link)
4356 		return;
4357 
4358 	switch (state->interface) {
4359 	case PHY_INTERFACE_MODE_10GBASER:
4360 		state->speed = SPEED_10000;
4361 		state->duplex = DUPLEX_FULL;
4362 		break;
4363 
4364 	default:
4365 		break;
4366 	}
4367 }
4368 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
4369 
4370 /**
4371  * phylink_replay_link_begin() - begin replay of link callbacks for driver
4372  *				 which loses state
4373  * @pl: a pointer to a &struct phylink returned from phylink_create()
4374  *
4375  * Helper for MAC drivers which may perform a destructive reset at runtime.
4376  * Both the own driver's mac_link_down() method is called, as well as the
4377  * pcs_link_down() method of the split PCS (if any).
4378  *
4379  * This is similar to phylink_stop(), except it does not alter the state of
4380  * the phylib PHY (it is assumed that it is not affected by the MAC destructive
4381  * reset).
4382  */
4383 void phylink_replay_link_begin(struct phylink *pl)
4384 {
4385 	ASSERT_RTNL();
4386 
4387 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_REPLAY);
4388 }
4389 EXPORT_SYMBOL_GPL(phylink_replay_link_begin);
4390 
4391 /**
4392  * phylink_replay_link_end() - end replay of link callbacks for driver
4393  *			       which lost state
4394  * @pl: a pointer to a &struct phylink returned from phylink_create()
4395  *
4396  * Helper for MAC drivers which may perform a destructive reset at runtime.
4397  * Both the own driver's mac_config() and mac_link_up() methods, as well as the
4398  * pcs_config() and pcs_link_up() method of the split PCS (if any), are called.
4399  *
4400  * This is similar to phylink_start(), except it does not alter the state of
4401  * the phylib PHY.
4402  *
4403  * One must call this method only within the same rtnl_lock() critical section
4404  * as a previous phylink_replay_link_start().
4405  */
4406 void phylink_replay_link_end(struct phylink *pl)
4407 {
4408 	ASSERT_RTNL();
4409 
4410 	if (WARN(!test_bit(PHYLINK_DISABLE_REPLAY,
4411 			   &pl->phylink_disable_state),
4412 		 "phylink_replay_link_end() called without a prior phylink_replay_link_begin()\n"))
4413 		return;
4414 
4415 	pl->force_major_config = true;
4416 	phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_REPLAY);
4417 	flush_work(&pl->resolve);
4418 }
4419 EXPORT_SYMBOL_GPL(phylink_replay_link_end);
4420 
4421 static int __init phylink_init(void)
4422 {
4423 	for (int i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); ++i)
4424 		__set_bit(phylink_sfp_interface_preference[i],
4425 			  phylink_sfp_interfaces);
4426 
4427 	return 0;
4428 }
4429 
4430 module_init(phylink_init);
4431 
4432 MODULE_LICENSE("GPL v2");
4433 MODULE_DESCRIPTION("phylink models the MAC to optional PHY connection");
4434