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