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