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