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