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