xref: /openbmc/linux/drivers/net/phy/phylink.c (revision 67559900)
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 cur_link_state;
714 
715 	mutex_lock(&pl->state_mutex);
716 	if (pl->netdev)
717 		cur_link_state = netif_carrier_ok(ndev);
718 	else
719 		cur_link_state = pl->old_link_state;
720 
721 	if (pl->phylink_disable_state) {
722 		pl->mac_link_dropped = false;
723 		link_state.link = false;
724 	} else if (pl->mac_link_dropped) {
725 		link_state.link = false;
726 	} else {
727 		switch (pl->cur_link_an_mode) {
728 		case MLO_AN_PHY:
729 			link_state = pl->phy_state;
730 			phylink_apply_manual_flow(pl, &link_state);
731 			mac_config = link_state.link;
732 			break;
733 
734 		case MLO_AN_FIXED:
735 			phylink_get_fixed_state(pl, &link_state);
736 			mac_config = link_state.link;
737 			break;
738 
739 		case MLO_AN_INBAND:
740 			phylink_mac_pcs_get_state(pl, &link_state);
741 
742 			/* If we have a phy, the "up" state is the union of
743 			 * both the PHY and the MAC
744 			 */
745 			if (pl->phydev)
746 				link_state.link &= pl->phy_state.link;
747 
748 			/* Only update if the PHY link is up */
749 			if (pl->phydev && pl->phy_state.link) {
750 				link_state.interface = pl->phy_state.interface;
751 
752 				/* If we have a PHY, we need to update with
753 				 * the PHY flow control bits.
754 				 */
755 				link_state.pause = pl->phy_state.pause;
756 				mac_config = true;
757 			}
758 			phylink_apply_manual_flow(pl, &link_state);
759 			break;
760 		}
761 	}
762 
763 	if (mac_config) {
764 		if (link_state.interface != pl->link_config.interface) {
765 			/* The interface has changed, force the link down and
766 			 * then reconfigure.
767 			 */
768 			if (cur_link_state) {
769 				phylink_link_down(pl);
770 				cur_link_state = false;
771 			}
772 			phylink_major_config(pl, false, &link_state);
773 			pl->link_config.interface = link_state.interface;
774 		} else if (!pl->pcs_ops) {
775 			/* The interface remains unchanged, only the speed,
776 			 * duplex or pause settings have changed. Call the
777 			 * old mac_config() method to configure the MAC/PCS
778 			 * only if we do not have a PCS installed (an
779 			 * unconverted user.)
780 			 */
781 			phylink_mac_config(pl, &link_state);
782 		}
783 	}
784 
785 	if (link_state.link != cur_link_state) {
786 		pl->old_link_state = link_state.link;
787 		if (!link_state.link)
788 			phylink_link_down(pl);
789 		else
790 			phylink_link_up(pl, link_state);
791 	}
792 	if (!link_state.link && pl->mac_link_dropped) {
793 		pl->mac_link_dropped = false;
794 		queue_work(system_power_efficient_wq, &pl->resolve);
795 	}
796 	mutex_unlock(&pl->state_mutex);
797 }
798 
799 static void phylink_run_resolve(struct phylink *pl)
800 {
801 	if (!pl->phylink_disable_state)
802 		queue_work(system_power_efficient_wq, &pl->resolve);
803 }
804 
805 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
806 {
807 	unsigned long state = pl->phylink_disable_state;
808 
809 	set_bit(bit, &pl->phylink_disable_state);
810 	if (state == 0) {
811 		queue_work(system_power_efficient_wq, &pl->resolve);
812 		flush_work(&pl->resolve);
813 	}
814 }
815 
816 static void phylink_fixed_poll(struct timer_list *t)
817 {
818 	struct phylink *pl = container_of(t, struct phylink, link_poll);
819 
820 	mod_timer(t, jiffies + HZ);
821 
822 	phylink_run_resolve(pl);
823 }
824 
825 static const struct sfp_upstream_ops sfp_phylink_ops;
826 
827 static int phylink_register_sfp(struct phylink *pl,
828 				struct fwnode_handle *fwnode)
829 {
830 	struct sfp_bus *bus;
831 	int ret;
832 
833 	if (!fwnode)
834 		return 0;
835 
836 	bus = sfp_bus_find_fwnode(fwnode);
837 	if (IS_ERR(bus)) {
838 		ret = PTR_ERR(bus);
839 		phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
840 		return ret;
841 	}
842 
843 	pl->sfp_bus = bus;
844 
845 	ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
846 	sfp_bus_put(bus);
847 
848 	return ret;
849 }
850 
851 /**
852  * phylink_create() - create a phylink instance
853  * @config: a pointer to the target &struct phylink_config
854  * @fwnode: a pointer to a &struct fwnode_handle describing the network
855  *	interface
856  * @iface: the desired link mode defined by &typedef phy_interface_t
857  * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
858  *
859  * Create a new phylink instance, and parse the link parameters found in @np.
860  * This will parse in-band modes, fixed-link or SFP configuration.
861  *
862  * Note: the rtnl lock must not be held when calling this function.
863  *
864  * Returns a pointer to a &struct phylink, or an error-pointer value. Users
865  * must use IS_ERR() to check for errors from this function.
866  */
867 struct phylink *phylink_create(struct phylink_config *config,
868 			       struct fwnode_handle *fwnode,
869 			       phy_interface_t iface,
870 			       const struct phylink_mac_ops *mac_ops)
871 {
872 	struct phylink *pl;
873 	int ret;
874 
875 	pl = kzalloc(sizeof(*pl), GFP_KERNEL);
876 	if (!pl)
877 		return ERR_PTR(-ENOMEM);
878 
879 	mutex_init(&pl->state_mutex);
880 	INIT_WORK(&pl->resolve, phylink_resolve);
881 
882 	pl->config = config;
883 	if (config->type == PHYLINK_NETDEV) {
884 		pl->netdev = to_net_dev(config->dev);
885 	} else if (config->type == PHYLINK_DEV) {
886 		pl->dev = config->dev;
887 	} else {
888 		kfree(pl);
889 		return ERR_PTR(-EINVAL);
890 	}
891 
892 	pl->phy_state.interface = iface;
893 	pl->link_interface = iface;
894 	if (iface == PHY_INTERFACE_MODE_MOCA)
895 		pl->link_port = PORT_BNC;
896 	else
897 		pl->link_port = PORT_MII;
898 	pl->link_config.interface = iface;
899 	pl->link_config.pause = MLO_PAUSE_AN;
900 	pl->link_config.speed = SPEED_UNKNOWN;
901 	pl->link_config.duplex = DUPLEX_UNKNOWN;
902 	pl->link_config.an_enabled = true;
903 	pl->mac_ops = mac_ops;
904 	__set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
905 	timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
906 
907 	bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
908 	linkmode_copy(pl->link_config.advertising, pl->supported);
909 	phylink_validate(pl, pl->supported, &pl->link_config);
910 
911 	ret = phylink_parse_mode(pl, fwnode);
912 	if (ret < 0) {
913 		kfree(pl);
914 		return ERR_PTR(ret);
915 	}
916 
917 	if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
918 		ret = phylink_parse_fixedlink(pl, fwnode);
919 		if (ret < 0) {
920 			kfree(pl);
921 			return ERR_PTR(ret);
922 		}
923 	}
924 
925 	pl->cur_link_an_mode = pl->cfg_link_an_mode;
926 
927 	ret = phylink_register_sfp(pl, fwnode);
928 	if (ret < 0) {
929 		kfree(pl);
930 		return ERR_PTR(ret);
931 	}
932 
933 	return pl;
934 }
935 EXPORT_SYMBOL_GPL(phylink_create);
936 
937 /**
938  * phylink_set_pcs() - set the current PCS for phylink to use
939  * @pl: a pointer to a &struct phylink returned from phylink_create()
940  * @pcs: a pointer to the &struct phylink_pcs
941  *
942  * Bind the MAC PCS to phylink.  This may be called after phylink_create(),
943  * in mac_prepare() or mac_config() methods if it is desired to dynamically
944  * change the PCS.
945  *
946  * Please note that there are behavioural changes with the mac_config()
947  * callback if a PCS is present (denoting a newer setup) so removing a PCS
948  * is not supported, and if a PCS is going to be used, it must be registered
949  * by calling phylink_set_pcs() at the latest in the first mac_config() call.
950  */
951 void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs)
952 {
953 	pl->pcs = pcs;
954 	pl->pcs_ops = pcs->ops;
955 }
956 EXPORT_SYMBOL_GPL(phylink_set_pcs);
957 
958 /**
959  * phylink_destroy() - cleanup and destroy the phylink instance
960  * @pl: a pointer to a &struct phylink returned from phylink_create()
961  *
962  * Destroy a phylink instance. Any PHY that has been attached must have been
963  * cleaned up via phylink_disconnect_phy() prior to calling this function.
964  *
965  * Note: the rtnl lock must not be held when calling this function.
966  */
967 void phylink_destroy(struct phylink *pl)
968 {
969 	sfp_bus_del_upstream(pl->sfp_bus);
970 	if (pl->link_gpio)
971 		gpiod_put(pl->link_gpio);
972 
973 	cancel_work_sync(&pl->resolve);
974 	kfree(pl);
975 }
976 EXPORT_SYMBOL_GPL(phylink_destroy);
977 
978 static void phylink_phy_change(struct phy_device *phydev, bool up)
979 {
980 	struct phylink *pl = phydev->phylink;
981 	bool tx_pause, rx_pause;
982 
983 	phy_get_pause(phydev, &tx_pause, &rx_pause);
984 
985 	mutex_lock(&pl->state_mutex);
986 	pl->phy_state.speed = phydev->speed;
987 	pl->phy_state.duplex = phydev->duplex;
988 	pl->phy_state.pause = MLO_PAUSE_NONE;
989 	if (tx_pause)
990 		pl->phy_state.pause |= MLO_PAUSE_TX;
991 	if (rx_pause)
992 		pl->phy_state.pause |= MLO_PAUSE_RX;
993 	pl->phy_state.interface = phydev->interface;
994 	pl->phy_state.link = up;
995 	mutex_unlock(&pl->state_mutex);
996 
997 	phylink_run_resolve(pl);
998 
999 	phylink_dbg(pl, "phy link %s %s/%s/%s/%s\n", up ? "up" : "down",
1000 		    phy_modes(phydev->interface),
1001 		    phy_speed_to_str(phydev->speed),
1002 		    phy_duplex_to_str(phydev->duplex),
1003 		    phylink_pause_to_str(pl->phy_state.pause));
1004 }
1005 
1006 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
1007 			       phy_interface_t interface)
1008 {
1009 	struct phylink_link_state config;
1010 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
1011 	char *irq_str;
1012 	int ret;
1013 
1014 	/*
1015 	 * This is the new way of dealing with flow control for PHYs,
1016 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
1017 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
1018 	 * using our validate call to the MAC, we rely upon the MAC
1019 	 * clearing the bits from both supported and advertising fields.
1020 	 */
1021 	phy_support_asym_pause(phy);
1022 
1023 	memset(&config, 0, sizeof(config));
1024 	linkmode_copy(supported, phy->supported);
1025 	linkmode_copy(config.advertising, phy->advertising);
1026 
1027 	/* Clause 45 PHYs switch their Serdes lane between several different
1028 	 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
1029 	 * speeds. We really need to know which interface modes the PHY and
1030 	 * MAC supports to properly work out which linkmodes can be supported.
1031 	 */
1032 	if (phy->is_c45 &&
1033 	    interface != PHY_INTERFACE_MODE_RXAUI &&
1034 	    interface != PHY_INTERFACE_MODE_XAUI &&
1035 	    interface != PHY_INTERFACE_MODE_USXGMII)
1036 		config.interface = PHY_INTERFACE_MODE_NA;
1037 	else
1038 		config.interface = interface;
1039 
1040 	ret = phylink_validate(pl, supported, &config);
1041 	if (ret) {
1042 		phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
1043 			     phy_modes(config.interface),
1044 			     __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1045 			     __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1046 			     ret);
1047 		return ret;
1048 	}
1049 
1050 	phy->phylink = pl;
1051 	phy->phy_link_change = phylink_phy_change;
1052 
1053 	irq_str = phy_attached_info_irq(phy);
1054 	phylink_info(pl,
1055 		     "PHY [%s] driver [%s] (irq=%s)\n",
1056 		     dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1057 	kfree(irq_str);
1058 
1059 	mutex_lock(&phy->lock);
1060 	mutex_lock(&pl->state_mutex);
1061 	pl->phydev = phy;
1062 	pl->phy_state.interface = interface;
1063 	pl->phy_state.pause = MLO_PAUSE_NONE;
1064 	pl->phy_state.speed = SPEED_UNKNOWN;
1065 	pl->phy_state.duplex = DUPLEX_UNKNOWN;
1066 	linkmode_copy(pl->supported, supported);
1067 	linkmode_copy(pl->link_config.advertising, config.advertising);
1068 
1069 	/* Restrict the phy advertisement according to the MAC support. */
1070 	linkmode_copy(phy->advertising, config.advertising);
1071 	mutex_unlock(&pl->state_mutex);
1072 	mutex_unlock(&phy->lock);
1073 
1074 	phylink_dbg(pl,
1075 		    "phy: setting supported %*pb advertising %*pb\n",
1076 		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1077 		    __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1078 
1079 	if (phy_interrupt_is_valid(phy))
1080 		phy_request_interrupt(phy);
1081 
1082 	return 0;
1083 }
1084 
1085 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1086 			      phy_interface_t interface)
1087 {
1088 	if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1089 		    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1090 		     phy_interface_mode_is_8023z(interface))))
1091 		return -EINVAL;
1092 
1093 	if (pl->phydev)
1094 		return -EBUSY;
1095 
1096 	return phy_attach_direct(pl->netdev, phy, 0, interface);
1097 }
1098 
1099 /**
1100  * phylink_connect_phy() - connect a PHY to the phylink instance
1101  * @pl: a pointer to a &struct phylink returned from phylink_create()
1102  * @phy: a pointer to a &struct phy_device.
1103  *
1104  * Connect @phy to the phylink instance specified by @pl by calling
1105  * phy_attach_direct(). Configure the @phy according to the MAC driver's
1106  * capabilities, start the PHYLIB state machine and enable any interrupts
1107  * that the PHY supports.
1108  *
1109  * This updates the phylink's ethtool supported and advertising link mode
1110  * masks.
1111  *
1112  * Returns 0 on success or a negative errno.
1113  */
1114 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1115 {
1116 	int ret;
1117 
1118 	/* Use PHY device/driver interface */
1119 	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1120 		pl->link_interface = phy->interface;
1121 		pl->link_config.interface = pl->link_interface;
1122 	}
1123 
1124 	ret = phylink_attach_phy(pl, phy, pl->link_interface);
1125 	if (ret < 0)
1126 		return ret;
1127 
1128 	ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1129 	if (ret)
1130 		phy_detach(phy);
1131 
1132 	return ret;
1133 }
1134 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1135 
1136 /**
1137  * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1138  * @pl: a pointer to a &struct phylink returned from phylink_create()
1139  * @dn: a pointer to a &struct device_node.
1140  * @flags: PHY-specific flags to communicate to the PHY device driver
1141  *
1142  * Connect the phy specified in the device node @dn to the phylink instance
1143  * specified by @pl. Actions specified in phylink_connect_phy() will be
1144  * performed.
1145  *
1146  * Returns 0 on success or a negative errno.
1147  */
1148 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1149 			   u32 flags)
1150 {
1151 	return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
1152 }
1153 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1154 
1155 /**
1156  * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
1157  * @pl: a pointer to a &struct phylink returned from phylink_create()
1158  * @fwnode: a pointer to a &struct fwnode_handle.
1159  * @flags: PHY-specific flags to communicate to the PHY device driver
1160  *
1161  * Connect the phy specified @fwnode to the phylink instance specified
1162  * by @pl.
1163  *
1164  * Returns 0 on success or a negative errno.
1165  */
1166 int phylink_fwnode_phy_connect(struct phylink *pl,
1167 			       struct fwnode_handle *fwnode,
1168 			       u32 flags)
1169 {
1170 	struct fwnode_handle *phy_fwnode;
1171 	struct phy_device *phy_dev;
1172 	int ret;
1173 
1174 	/* Fixed links and 802.3z are handled without needing a PHY */
1175 	if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1176 	    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1177 	     phy_interface_mode_is_8023z(pl->link_interface)))
1178 		return 0;
1179 
1180 	phy_fwnode = fwnode_get_phy_node(fwnode);
1181 	if (IS_ERR(phy_fwnode)) {
1182 		if (pl->cfg_link_an_mode == MLO_AN_PHY)
1183 			return -ENODEV;
1184 		return 0;
1185 	}
1186 
1187 	phy_dev = fwnode_phy_find_device(phy_fwnode);
1188 	/* We're done with the phy_node handle */
1189 	fwnode_handle_put(phy_fwnode);
1190 	if (!phy_dev)
1191 		return -ENODEV;
1192 
1193 	ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1194 				pl->link_interface);
1195 	if (ret) {
1196 		phy_device_free(phy_dev);
1197 		return ret;
1198 	}
1199 
1200 	ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1201 	if (ret)
1202 		phy_detach(phy_dev);
1203 
1204 	return ret;
1205 }
1206 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
1207 
1208 /**
1209  * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1210  *   instance.
1211  * @pl: a pointer to a &struct phylink returned from phylink_create()
1212  *
1213  * Disconnect any current PHY from the phylink instance described by @pl.
1214  */
1215 void phylink_disconnect_phy(struct phylink *pl)
1216 {
1217 	struct phy_device *phy;
1218 
1219 	ASSERT_RTNL();
1220 
1221 	phy = pl->phydev;
1222 	if (phy) {
1223 		mutex_lock(&phy->lock);
1224 		mutex_lock(&pl->state_mutex);
1225 		pl->phydev = NULL;
1226 		mutex_unlock(&pl->state_mutex);
1227 		mutex_unlock(&phy->lock);
1228 		flush_work(&pl->resolve);
1229 
1230 		phy_disconnect(phy);
1231 	}
1232 }
1233 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1234 
1235 /**
1236  * phylink_mac_change() - notify phylink of a change in MAC state
1237  * @pl: a pointer to a &struct phylink returned from phylink_create()
1238  * @up: indicates whether the link is currently up.
1239  *
1240  * The MAC driver should call this driver when the state of its link
1241  * changes (eg, link failure, new negotiation results, etc.)
1242  */
1243 void phylink_mac_change(struct phylink *pl, bool up)
1244 {
1245 	if (!up)
1246 		pl->mac_link_dropped = true;
1247 	phylink_run_resolve(pl);
1248 	phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1249 }
1250 EXPORT_SYMBOL_GPL(phylink_mac_change);
1251 
1252 static irqreturn_t phylink_link_handler(int irq, void *data)
1253 {
1254 	struct phylink *pl = data;
1255 
1256 	phylink_run_resolve(pl);
1257 
1258 	return IRQ_HANDLED;
1259 }
1260 
1261 /**
1262  * phylink_start() - start a phylink instance
1263  * @pl: a pointer to a &struct phylink returned from phylink_create()
1264  *
1265  * Start the phylink instance specified by @pl, configuring the MAC for the
1266  * desired link mode(s) and negotiation style. This should be called from the
1267  * network device driver's &struct net_device_ops ndo_open() method.
1268  */
1269 void phylink_start(struct phylink *pl)
1270 {
1271 	bool poll = false;
1272 
1273 	ASSERT_RTNL();
1274 
1275 	phylink_info(pl, "configuring for %s/%s link mode\n",
1276 		     phylink_an_mode_str(pl->cur_link_an_mode),
1277 		     phy_modes(pl->link_config.interface));
1278 
1279 	/* Always set the carrier off */
1280 	if (pl->netdev)
1281 		netif_carrier_off(pl->netdev);
1282 
1283 	/* Apply the link configuration to the MAC when starting. This allows
1284 	 * a fixed-link to start with the correct parameters, and also
1285 	 * ensures that we set the appropriate advertisement for Serdes links.
1286 	 *
1287 	 * Restart autonegotiation if using 802.3z to ensure that the link
1288 	 * parameters are properly negotiated.  This is necessary for DSA
1289 	 * switches using 802.3z negotiation to ensure they see our modes.
1290 	 */
1291 	phylink_mac_initial_config(pl, true);
1292 
1293 	clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1294 	phylink_run_resolve(pl);
1295 
1296 	if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1297 		int irq = gpiod_to_irq(pl->link_gpio);
1298 
1299 		if (irq > 0) {
1300 			if (!request_irq(irq, phylink_link_handler,
1301 					 IRQF_TRIGGER_RISING |
1302 					 IRQF_TRIGGER_FALLING,
1303 					 "netdev link", pl))
1304 				pl->link_irq = irq;
1305 			else
1306 				irq = 0;
1307 		}
1308 		if (irq <= 0)
1309 			poll = true;
1310 	}
1311 
1312 	switch (pl->cfg_link_an_mode) {
1313 	case MLO_AN_FIXED:
1314 		poll |= pl->config->poll_fixed_state;
1315 		break;
1316 	case MLO_AN_INBAND:
1317 		poll |= pl->config->pcs_poll;
1318 		if (pl->pcs)
1319 			poll |= pl->pcs->poll;
1320 		break;
1321 	}
1322 	if (poll)
1323 		mod_timer(&pl->link_poll, jiffies + HZ);
1324 	if (pl->phydev)
1325 		phy_start(pl->phydev);
1326 	if (pl->sfp_bus)
1327 		sfp_upstream_start(pl->sfp_bus);
1328 }
1329 EXPORT_SYMBOL_GPL(phylink_start);
1330 
1331 /**
1332  * phylink_stop() - stop a phylink instance
1333  * @pl: a pointer to a &struct phylink returned from phylink_create()
1334  *
1335  * Stop the phylink instance specified by @pl. This should be called from the
1336  * network device driver's &struct net_device_ops ndo_stop() method.  The
1337  * network device's carrier state should not be changed prior to calling this
1338  * function.
1339  *
1340  * This will synchronously bring down the link if the link is not already
1341  * down (in other words, it will trigger a mac_link_down() method call.)
1342  */
1343 void phylink_stop(struct phylink *pl)
1344 {
1345 	ASSERT_RTNL();
1346 
1347 	if (pl->sfp_bus)
1348 		sfp_upstream_stop(pl->sfp_bus);
1349 	if (pl->phydev)
1350 		phy_stop(pl->phydev);
1351 	del_timer_sync(&pl->link_poll);
1352 	if (pl->link_irq) {
1353 		free_irq(pl->link_irq, pl);
1354 		pl->link_irq = 0;
1355 	}
1356 
1357 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1358 }
1359 EXPORT_SYMBOL_GPL(phylink_stop);
1360 
1361 /**
1362  * phylink_suspend() - handle a network device suspend event
1363  * @pl: a pointer to a &struct phylink returned from phylink_create()
1364  * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
1365  *
1366  * Handle a network device suspend event. There are several cases:
1367  * - If Wake-on-Lan is not active, we can bring down the link between
1368  *   the MAC and PHY by calling phylink_stop().
1369  * - If Wake-on-Lan is active, and being handled only by the PHY, we
1370  *   can also bring down the link between the MAC and PHY.
1371  * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
1372  *   still needs to receive packets, so we can not bring the link down.
1373  */
1374 void phylink_suspend(struct phylink *pl, bool mac_wol)
1375 {
1376 	ASSERT_RTNL();
1377 
1378 	if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) {
1379 		/* Wake-on-Lan enabled, MAC handling */
1380 		mutex_lock(&pl->state_mutex);
1381 
1382 		/* Stop the resolver bringing the link up */
1383 		__set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1384 
1385 		/* Disable the carrier, to prevent transmit timeouts,
1386 		 * but one would hope all packets have been sent. This
1387 		 * also means phylink_resolve() will do nothing.
1388 		 */
1389 		if (pl->netdev)
1390 			netif_carrier_off(pl->netdev);
1391 		else
1392 			pl->old_link_state = false;
1393 
1394 		/* We do not call mac_link_down() here as we want the
1395 		 * link to remain up to receive the WoL packets.
1396 		 */
1397 		mutex_unlock(&pl->state_mutex);
1398 	} else {
1399 		phylink_stop(pl);
1400 	}
1401 }
1402 EXPORT_SYMBOL_GPL(phylink_suspend);
1403 
1404 /**
1405  * phylink_resume() - handle a network device resume event
1406  * @pl: a pointer to a &struct phylink returned from phylink_create()
1407  *
1408  * Undo the effects of phylink_suspend(), returning the link to an
1409  * operational state.
1410  */
1411 void phylink_resume(struct phylink *pl)
1412 {
1413 	ASSERT_RTNL();
1414 
1415 	if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
1416 		/* Wake-on-Lan enabled, MAC handling */
1417 
1418 		/* Call mac_link_down() so we keep the overall state balanced.
1419 		 * Do this under the state_mutex lock for consistency. This
1420 		 * will cause a "Link Down" message to be printed during
1421 		 * resume, which is harmless - the true link state will be
1422 		 * printed when we run a resolve.
1423 		 */
1424 		mutex_lock(&pl->state_mutex);
1425 		phylink_link_down(pl);
1426 		mutex_unlock(&pl->state_mutex);
1427 
1428 		/* Re-apply the link parameters so that all the settings get
1429 		 * restored to the MAC.
1430 		 */
1431 		phylink_mac_initial_config(pl, true);
1432 
1433 		/* Re-enable and re-resolve the link parameters */
1434 		clear_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1435 		phylink_run_resolve(pl);
1436 	} else {
1437 		phylink_start(pl);
1438 	}
1439 }
1440 EXPORT_SYMBOL_GPL(phylink_resume);
1441 
1442 /**
1443  * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1444  * @pl: a pointer to a &struct phylink returned from phylink_create()
1445  * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1446  *
1447  * Read the wake on lan parameters from the PHY attached to the phylink
1448  * instance specified by @pl. If no PHY is currently attached, report no
1449  * support for wake on lan.
1450  */
1451 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1452 {
1453 	ASSERT_RTNL();
1454 
1455 	wol->supported = 0;
1456 	wol->wolopts = 0;
1457 
1458 	if (pl->phydev)
1459 		phy_ethtool_get_wol(pl->phydev, wol);
1460 }
1461 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1462 
1463 /**
1464  * phylink_ethtool_set_wol() - set wake on lan parameters
1465  * @pl: a pointer to a &struct phylink returned from phylink_create()
1466  * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1467  *
1468  * Set the wake on lan parameters for the PHY attached to the phylink
1469  * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1470  * error.
1471  *
1472  * Returns zero on success or negative errno code.
1473  */
1474 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1475 {
1476 	int ret = -EOPNOTSUPP;
1477 
1478 	ASSERT_RTNL();
1479 
1480 	if (pl->phydev)
1481 		ret = phy_ethtool_set_wol(pl->phydev, wol);
1482 
1483 	return ret;
1484 }
1485 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1486 
1487 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1488 {
1489 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1490 
1491 	linkmode_zero(mask);
1492 	phylink_set_port_modes(mask);
1493 
1494 	linkmode_and(dst, dst, mask);
1495 	linkmode_or(dst, dst, b);
1496 }
1497 
1498 static void phylink_get_ksettings(const struct phylink_link_state *state,
1499 				  struct ethtool_link_ksettings *kset)
1500 {
1501 	phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1502 	linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1503 	kset->base.speed = state->speed;
1504 	kset->base.duplex = state->duplex;
1505 	kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1506 				AUTONEG_DISABLE;
1507 }
1508 
1509 /**
1510  * phylink_ethtool_ksettings_get() - get the current link settings
1511  * @pl: a pointer to a &struct phylink returned from phylink_create()
1512  * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1513  *
1514  * Read the current link settings for the phylink instance specified by @pl.
1515  * This will be the link settings read from the MAC, PHY or fixed link
1516  * settings depending on the current negotiation mode.
1517  */
1518 int phylink_ethtool_ksettings_get(struct phylink *pl,
1519 				  struct ethtool_link_ksettings *kset)
1520 {
1521 	struct phylink_link_state link_state;
1522 
1523 	ASSERT_RTNL();
1524 
1525 	if (pl->phydev)
1526 		phy_ethtool_ksettings_get(pl->phydev, kset);
1527 	else
1528 		kset->base.port = pl->link_port;
1529 
1530 	linkmode_copy(kset->link_modes.supported, pl->supported);
1531 
1532 	switch (pl->cur_link_an_mode) {
1533 	case MLO_AN_FIXED:
1534 		/* We are using fixed settings. Report these as the
1535 		 * current link settings - and note that these also
1536 		 * represent the supported speeds/duplex/pause modes.
1537 		 */
1538 		phylink_get_fixed_state(pl, &link_state);
1539 		phylink_get_ksettings(&link_state, kset);
1540 		break;
1541 
1542 	case MLO_AN_INBAND:
1543 		/* If there is a phy attached, then use the reported
1544 		 * settings from the phy with no modification.
1545 		 */
1546 		if (pl->phydev)
1547 			break;
1548 
1549 		phylink_mac_pcs_get_state(pl, &link_state);
1550 
1551 		/* The MAC is reporting the link results from its own PCS
1552 		 * layer via in-band status. Report these as the current
1553 		 * link settings.
1554 		 */
1555 		phylink_get_ksettings(&link_state, kset);
1556 		break;
1557 	}
1558 
1559 	return 0;
1560 }
1561 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1562 
1563 /**
1564  * phylink_ethtool_ksettings_set() - set the link settings
1565  * @pl: a pointer to a &struct phylink returned from phylink_create()
1566  * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1567  */
1568 int phylink_ethtool_ksettings_set(struct phylink *pl,
1569 				  const struct ethtool_link_ksettings *kset)
1570 {
1571 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1572 	struct phylink_link_state config;
1573 	const struct phy_setting *s;
1574 
1575 	ASSERT_RTNL();
1576 
1577 	if (pl->phydev) {
1578 		/* We can rely on phylib for this update; we also do not need
1579 		 * to update the pl->link_config settings:
1580 		 * - the configuration returned via ksettings_get() will come
1581 		 *   from phylib whenever a PHY is present.
1582 		 * - link_config.interface will be updated by the PHY calling
1583 		 *   back via phylink_phy_change() and a subsequent resolve.
1584 		 * - initial link configuration for PHY mode comes from the
1585 		 *   last phy state updated via phylink_phy_change().
1586 		 * - other configuration changes (e.g. pause modes) are
1587 		 *   performed directly via phylib.
1588 		 * - if in in-band mode with a PHY, the link configuration
1589 		 *   is passed on the link from the PHY, and all of
1590 		 *   link_config.{speed,duplex,an_enabled,pause} are not used.
1591 		 * - the only possible use would be link_config.advertising
1592 		 *   pause modes when in 1000base-X mode with a PHY, but in
1593 		 *   the presence of a PHY, this should not be changed as that
1594 		 *   should be determined from the media side advertisement.
1595 		 */
1596 		return phy_ethtool_ksettings_set(pl->phydev, kset);
1597 	}
1598 
1599 	config = pl->link_config;
1600 
1601 	/* Mask out unsupported advertisements */
1602 	linkmode_and(config.advertising, kset->link_modes.advertising,
1603 		     pl->supported);
1604 
1605 	/* FIXME: should we reject autoneg if phy/mac does not support it? */
1606 	switch (kset->base.autoneg) {
1607 	case AUTONEG_DISABLE:
1608 		/* Autonegotiation disabled, select a suitable speed and
1609 		 * duplex.
1610 		 */
1611 		s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1612 				       pl->supported, false);
1613 		if (!s)
1614 			return -EINVAL;
1615 
1616 		/* If we have a fixed link, refuse to change link parameters.
1617 		 * If the link parameters match, accept them but do nothing.
1618 		 */
1619 		if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1620 			if (s->speed != pl->link_config.speed ||
1621 			    s->duplex != pl->link_config.duplex)
1622 				return -EINVAL;
1623 			return 0;
1624 		}
1625 
1626 		config.speed = s->speed;
1627 		config.duplex = s->duplex;
1628 		break;
1629 
1630 	case AUTONEG_ENABLE:
1631 		/* If we have a fixed link, allow autonegotiation (since that
1632 		 * is our default case) but do not allow the advertisement to
1633 		 * be changed. If the advertisement matches, simply return.
1634 		 */
1635 		if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1636 			if (!linkmode_equal(config.advertising,
1637 					    pl->link_config.advertising))
1638 				return -EINVAL;
1639 			return 0;
1640 		}
1641 
1642 		config.speed = SPEED_UNKNOWN;
1643 		config.duplex = DUPLEX_UNKNOWN;
1644 		break;
1645 
1646 	default:
1647 		return -EINVAL;
1648 	}
1649 
1650 	/* We have ruled out the case with a PHY attached, and the
1651 	 * fixed-link cases.  All that is left are in-band links.
1652 	 */
1653 	config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
1654 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
1655 			 config.an_enabled);
1656 
1657 	/* If this link is with an SFP, ensure that changes to advertised modes
1658 	 * also cause the associated interface to be selected such that the
1659 	 * link can be configured correctly.
1660 	 */
1661 	if (pl->sfp_bus) {
1662 		config.interface = sfp_select_interface(pl->sfp_bus,
1663 							config.advertising);
1664 		if (config.interface == PHY_INTERFACE_MODE_NA) {
1665 			phylink_err(pl,
1666 				    "selection of interface failed, advertisement %*pb\n",
1667 				    __ETHTOOL_LINK_MODE_MASK_NBITS,
1668 				    config.advertising);
1669 			return -EINVAL;
1670 		}
1671 
1672 		/* Revalidate with the selected interface */
1673 		linkmode_copy(support, pl->supported);
1674 		if (phylink_validate(pl, support, &config)) {
1675 			phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
1676 				    phylink_an_mode_str(pl->cur_link_an_mode),
1677 				    phy_modes(config.interface),
1678 				    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1679 			return -EINVAL;
1680 		}
1681 	} else {
1682 		/* Validate without changing the current supported mask. */
1683 		linkmode_copy(support, pl->supported);
1684 		if (phylink_validate(pl, support, &config))
1685 			return -EINVAL;
1686 	}
1687 
1688 	/* If autonegotiation is enabled, we must have an advertisement */
1689 	if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1690 		return -EINVAL;
1691 
1692 	mutex_lock(&pl->state_mutex);
1693 	pl->link_config.speed = config.speed;
1694 	pl->link_config.duplex = config.duplex;
1695 	pl->link_config.an_enabled = config.an_enabled;
1696 
1697 	if (pl->link_config.interface != config.interface) {
1698 		/* The interface changed, e.g. 1000base-X <-> 2500base-X */
1699 		/* We need to force the link down, then change the interface */
1700 		if (pl->old_link_state) {
1701 			phylink_link_down(pl);
1702 			pl->old_link_state = false;
1703 		}
1704 		if (!test_bit(PHYLINK_DISABLE_STOPPED,
1705 			      &pl->phylink_disable_state))
1706 			phylink_major_config(pl, false, &config);
1707 		pl->link_config.interface = config.interface;
1708 		linkmode_copy(pl->link_config.advertising, config.advertising);
1709 	} else if (!linkmode_equal(pl->link_config.advertising,
1710 				   config.advertising)) {
1711 		linkmode_copy(pl->link_config.advertising, config.advertising);
1712 		phylink_change_inband_advert(pl);
1713 	}
1714 	mutex_unlock(&pl->state_mutex);
1715 
1716 	return 0;
1717 }
1718 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1719 
1720 /**
1721  * phylink_ethtool_nway_reset() - restart negotiation
1722  * @pl: a pointer to a &struct phylink returned from phylink_create()
1723  *
1724  * Restart negotiation for the phylink instance specified by @pl. This will
1725  * cause any attached phy to restart negotiation with the link partner, and
1726  * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1727  * negotiation.
1728  *
1729  * Returns zero on success, or negative error code.
1730  */
1731 int phylink_ethtool_nway_reset(struct phylink *pl)
1732 {
1733 	int ret = 0;
1734 
1735 	ASSERT_RTNL();
1736 
1737 	if (pl->phydev)
1738 		ret = phy_restart_aneg(pl->phydev);
1739 	phylink_mac_pcs_an_restart(pl);
1740 
1741 	return ret;
1742 }
1743 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1744 
1745 /**
1746  * phylink_ethtool_get_pauseparam() - get the current pause parameters
1747  * @pl: a pointer to a &struct phylink returned from phylink_create()
1748  * @pause: a pointer to a &struct ethtool_pauseparam
1749  */
1750 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1751 				    struct ethtool_pauseparam *pause)
1752 {
1753 	ASSERT_RTNL();
1754 
1755 	pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1756 	pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1757 	pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1758 }
1759 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1760 
1761 /**
1762  * phylink_ethtool_set_pauseparam() - set the current pause parameters
1763  * @pl: a pointer to a &struct phylink returned from phylink_create()
1764  * @pause: a pointer to a &struct ethtool_pauseparam
1765  */
1766 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1767 				   struct ethtool_pauseparam *pause)
1768 {
1769 	struct phylink_link_state *config = &pl->link_config;
1770 	bool manual_changed;
1771 	int pause_state;
1772 
1773 	ASSERT_RTNL();
1774 
1775 	if (pl->cur_link_an_mode == MLO_AN_FIXED)
1776 		return -EOPNOTSUPP;
1777 
1778 	if (!phylink_test(pl->supported, Pause) &&
1779 	    !phylink_test(pl->supported, Asym_Pause))
1780 		return -EOPNOTSUPP;
1781 
1782 	if (!phylink_test(pl->supported, Asym_Pause) &&
1783 	    pause->rx_pause != pause->tx_pause)
1784 		return -EINVAL;
1785 
1786 	pause_state = 0;
1787 	if (pause->autoneg)
1788 		pause_state |= MLO_PAUSE_AN;
1789 	if (pause->rx_pause)
1790 		pause_state |= MLO_PAUSE_RX;
1791 	if (pause->tx_pause)
1792 		pause_state |= MLO_PAUSE_TX;
1793 
1794 	mutex_lock(&pl->state_mutex);
1795 	/*
1796 	 * See the comments for linkmode_set_pause(), wrt the deficiencies
1797 	 * with the current implementation.  A solution to this issue would
1798 	 * be:
1799 	 * ethtool  Local device
1800 	 *  rx  tx  Pause AsymDir
1801 	 *  0   0   0     0
1802 	 *  1   0   1     1
1803 	 *  0   1   0     1
1804 	 *  1   1   1     1
1805 	 * and then use the ethtool rx/tx enablement status to mask the
1806 	 * rx/tx pause resolution.
1807 	 */
1808 	linkmode_set_pause(config->advertising, pause->tx_pause,
1809 			   pause->rx_pause);
1810 
1811 	manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1812 			 (!(pause_state & MLO_PAUSE_AN) &&
1813 			   (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1814 
1815 	config->pause = pause_state;
1816 
1817 	/* Update our in-band advertisement, triggering a renegotiation if
1818 	 * the advertisement changed.
1819 	 */
1820 	if (!pl->phydev)
1821 		phylink_change_inband_advert(pl);
1822 
1823 	mutex_unlock(&pl->state_mutex);
1824 
1825 	/* If we have a PHY, a change of the pause frame advertisement will
1826 	 * cause phylib to renegotiate (if AN is enabled) which will in turn
1827 	 * call our phylink_phy_change() and trigger a resolve.  Note that
1828 	 * we can't hold our state mutex while calling phy_set_asym_pause().
1829 	 */
1830 	if (pl->phydev)
1831 		phy_set_asym_pause(pl->phydev, pause->rx_pause,
1832 				   pause->tx_pause);
1833 
1834 	/* If the manual pause settings changed, make sure we trigger a
1835 	 * resolve to update their state; we can not guarantee that the
1836 	 * link will cycle.
1837 	 */
1838 	if (manual_changed) {
1839 		pl->mac_link_dropped = true;
1840 		phylink_run_resolve(pl);
1841 	}
1842 
1843 	return 0;
1844 }
1845 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1846 
1847 /**
1848  * phylink_get_eee_err() - read the energy efficient ethernet error
1849  *   counter
1850  * @pl: a pointer to a &struct phylink returned from phylink_create().
1851  *
1852  * Read the Energy Efficient Ethernet error counter from the PHY associated
1853  * with the phylink instance specified by @pl.
1854  *
1855  * Returns positive error counter value, or negative error code.
1856  */
1857 int phylink_get_eee_err(struct phylink *pl)
1858 {
1859 	int ret = 0;
1860 
1861 	ASSERT_RTNL();
1862 
1863 	if (pl->phydev)
1864 		ret = phy_get_eee_err(pl->phydev);
1865 
1866 	return ret;
1867 }
1868 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1869 
1870 /**
1871  * phylink_init_eee() - init and check the EEE features
1872  * @pl: a pointer to a &struct phylink returned from phylink_create()
1873  * @clk_stop_enable: allow PHY to stop receive clock
1874  *
1875  * Must be called either with RTNL held or within mac_link_up()
1876  */
1877 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1878 {
1879 	int ret = -EOPNOTSUPP;
1880 
1881 	if (pl->phydev)
1882 		ret = phy_init_eee(pl->phydev, clk_stop_enable);
1883 
1884 	return ret;
1885 }
1886 EXPORT_SYMBOL_GPL(phylink_init_eee);
1887 
1888 /**
1889  * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1890  * @pl: a pointer to a &struct phylink returned from phylink_create()
1891  * @eee: a pointer to a &struct ethtool_eee for the read parameters
1892  */
1893 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1894 {
1895 	int ret = -EOPNOTSUPP;
1896 
1897 	ASSERT_RTNL();
1898 
1899 	if (pl->phydev)
1900 		ret = phy_ethtool_get_eee(pl->phydev, eee);
1901 
1902 	return ret;
1903 }
1904 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1905 
1906 /**
1907  * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1908  * @pl: a pointer to a &struct phylink returned from phylink_create()
1909  * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1910  */
1911 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1912 {
1913 	int ret = -EOPNOTSUPP;
1914 
1915 	ASSERT_RTNL();
1916 
1917 	if (pl->phydev)
1918 		ret = phy_ethtool_set_eee(pl->phydev, eee);
1919 
1920 	return ret;
1921 }
1922 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1923 
1924 /* This emulates MII registers for a fixed-mode phy operating as per the
1925  * passed in state. "aneg" defines if we report negotiation is possible.
1926  *
1927  * FIXME: should deal with negotiation state too.
1928  */
1929 static int phylink_mii_emul_read(unsigned int reg,
1930 				 struct phylink_link_state *state)
1931 {
1932 	struct fixed_phy_status fs;
1933 	unsigned long *lpa = state->lp_advertising;
1934 	int val;
1935 
1936 	fs.link = state->link;
1937 	fs.speed = state->speed;
1938 	fs.duplex = state->duplex;
1939 	fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1940 	fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
1941 
1942 	val = swphy_read_reg(reg, &fs);
1943 	if (reg == MII_BMSR) {
1944 		if (!state->an_complete)
1945 			val &= ~BMSR_ANEGCOMPLETE;
1946 	}
1947 	return val;
1948 }
1949 
1950 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1951 			    unsigned int reg)
1952 {
1953 	struct phy_device *phydev = pl->phydev;
1954 	int prtad, devad;
1955 
1956 	if (mdio_phy_id_is_c45(phy_id)) {
1957 		prtad = mdio_phy_id_prtad(phy_id);
1958 		devad = mdio_phy_id_devad(phy_id);
1959 		devad = mdiobus_c45_addr(devad, reg);
1960 	} else if (phydev->is_c45) {
1961 		switch (reg) {
1962 		case MII_BMCR:
1963 		case MII_BMSR:
1964 		case MII_PHYSID1:
1965 		case MII_PHYSID2:
1966 			devad = __ffs(phydev->c45_ids.mmds_present);
1967 			break;
1968 		case MII_ADVERTISE:
1969 		case MII_LPA:
1970 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1971 				return -EINVAL;
1972 			devad = MDIO_MMD_AN;
1973 			if (reg == MII_ADVERTISE)
1974 				reg = MDIO_AN_ADVERTISE;
1975 			else
1976 				reg = MDIO_AN_LPA;
1977 			break;
1978 		default:
1979 			return -EINVAL;
1980 		}
1981 		prtad = phy_id;
1982 		devad = mdiobus_c45_addr(devad, reg);
1983 	} else {
1984 		prtad = phy_id;
1985 		devad = reg;
1986 	}
1987 	return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1988 }
1989 
1990 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1991 			     unsigned int reg, unsigned int val)
1992 {
1993 	struct phy_device *phydev = pl->phydev;
1994 	int prtad, devad;
1995 
1996 	if (mdio_phy_id_is_c45(phy_id)) {
1997 		prtad = mdio_phy_id_prtad(phy_id);
1998 		devad = mdio_phy_id_devad(phy_id);
1999 		devad = mdiobus_c45_addr(devad, reg);
2000 	} else if (phydev->is_c45) {
2001 		switch (reg) {
2002 		case MII_BMCR:
2003 		case MII_BMSR:
2004 		case MII_PHYSID1:
2005 		case MII_PHYSID2:
2006 			devad = __ffs(phydev->c45_ids.mmds_present);
2007 			break;
2008 		case MII_ADVERTISE:
2009 		case MII_LPA:
2010 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2011 				return -EINVAL;
2012 			devad = MDIO_MMD_AN;
2013 			if (reg == MII_ADVERTISE)
2014 				reg = MDIO_AN_ADVERTISE;
2015 			else
2016 				reg = MDIO_AN_LPA;
2017 			break;
2018 		default:
2019 			return -EINVAL;
2020 		}
2021 		prtad = phy_id;
2022 		devad = mdiobus_c45_addr(devad, reg);
2023 	} else {
2024 		prtad = phy_id;
2025 		devad = reg;
2026 	}
2027 
2028 	return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
2029 }
2030 
2031 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
2032 			    unsigned int reg)
2033 {
2034 	struct phylink_link_state state;
2035 	int val = 0xffff;
2036 
2037 	switch (pl->cur_link_an_mode) {
2038 	case MLO_AN_FIXED:
2039 		if (phy_id == 0) {
2040 			phylink_get_fixed_state(pl, &state);
2041 			val = phylink_mii_emul_read(reg, &state);
2042 		}
2043 		break;
2044 
2045 	case MLO_AN_PHY:
2046 		return -EOPNOTSUPP;
2047 
2048 	case MLO_AN_INBAND:
2049 		if (phy_id == 0) {
2050 			phylink_mac_pcs_get_state(pl, &state);
2051 			val = phylink_mii_emul_read(reg, &state);
2052 		}
2053 		break;
2054 	}
2055 
2056 	return val & 0xffff;
2057 }
2058 
2059 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
2060 			     unsigned int reg, unsigned int val)
2061 {
2062 	switch (pl->cur_link_an_mode) {
2063 	case MLO_AN_FIXED:
2064 		break;
2065 
2066 	case MLO_AN_PHY:
2067 		return -EOPNOTSUPP;
2068 
2069 	case MLO_AN_INBAND:
2070 		break;
2071 	}
2072 
2073 	return 0;
2074 }
2075 
2076 /**
2077  * phylink_mii_ioctl() - generic mii ioctl interface
2078  * @pl: a pointer to a &struct phylink returned from phylink_create()
2079  * @ifr: a pointer to a &struct ifreq for socket ioctls
2080  * @cmd: ioctl cmd to execute
2081  *
2082  * Perform the specified MII ioctl on the PHY attached to the phylink instance
2083  * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
2084  *
2085  * Returns: zero on success or negative error code.
2086  *
2087  * %SIOCGMIIPHY:
2088  *  read register from the current PHY.
2089  * %SIOCGMIIREG:
2090  *  read register from the specified PHY.
2091  * %SIOCSMIIREG:
2092  *  set a register on the specified PHY.
2093  */
2094 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
2095 {
2096 	struct mii_ioctl_data *mii = if_mii(ifr);
2097 	int  ret;
2098 
2099 	ASSERT_RTNL();
2100 
2101 	if (pl->phydev) {
2102 		/* PHYs only exist for MLO_AN_PHY and SGMII */
2103 		switch (cmd) {
2104 		case SIOCGMIIPHY:
2105 			mii->phy_id = pl->phydev->mdio.addr;
2106 			fallthrough;
2107 
2108 		case SIOCGMIIREG:
2109 			ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
2110 			if (ret >= 0) {
2111 				mii->val_out = ret;
2112 				ret = 0;
2113 			}
2114 			break;
2115 
2116 		case SIOCSMIIREG:
2117 			ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
2118 						mii->val_in);
2119 			break;
2120 
2121 		default:
2122 			ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
2123 			break;
2124 		}
2125 	} else {
2126 		switch (cmd) {
2127 		case SIOCGMIIPHY:
2128 			mii->phy_id = 0;
2129 			fallthrough;
2130 
2131 		case SIOCGMIIREG:
2132 			ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
2133 			if (ret >= 0) {
2134 				mii->val_out = ret;
2135 				ret = 0;
2136 			}
2137 			break;
2138 
2139 		case SIOCSMIIREG:
2140 			ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
2141 						mii->val_in);
2142 			break;
2143 
2144 		default:
2145 			ret = -EOPNOTSUPP;
2146 			break;
2147 		}
2148 	}
2149 
2150 	return ret;
2151 }
2152 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2153 
2154 /**
2155  * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2156  *   link partners
2157  * @pl: a pointer to a &struct phylink returned from phylink_create()
2158  * @sync: perform action synchronously
2159  *
2160  * If we have a PHY that is not part of a SFP module, then set the speed
2161  * as described in the phy_speed_down() function. Please see this function
2162  * for a description of the @sync parameter.
2163  *
2164  * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2165  */
2166 int phylink_speed_down(struct phylink *pl, bool sync)
2167 {
2168 	int ret = 0;
2169 
2170 	ASSERT_RTNL();
2171 
2172 	if (!pl->sfp_bus && pl->phydev)
2173 		ret = phy_speed_down(pl->phydev, sync);
2174 
2175 	return ret;
2176 }
2177 EXPORT_SYMBOL_GPL(phylink_speed_down);
2178 
2179 /**
2180  * phylink_speed_up() - restore the advertised speeds prior to the call to
2181  *   phylink_speed_down()
2182  * @pl: a pointer to a &struct phylink returned from phylink_create()
2183  *
2184  * If we have a PHY that is not part of a SFP module, then restore the
2185  * PHY speeds as per phy_speed_up().
2186  *
2187  * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2188  */
2189 int phylink_speed_up(struct phylink *pl)
2190 {
2191 	int ret = 0;
2192 
2193 	ASSERT_RTNL();
2194 
2195 	if (!pl->sfp_bus && pl->phydev)
2196 		ret = phy_speed_up(pl->phydev);
2197 
2198 	return ret;
2199 }
2200 EXPORT_SYMBOL_GPL(phylink_speed_up);
2201 
2202 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2203 {
2204 	struct phylink *pl = upstream;
2205 
2206 	pl->netdev->sfp_bus = bus;
2207 }
2208 
2209 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2210 {
2211 	struct phylink *pl = upstream;
2212 
2213 	pl->netdev->sfp_bus = NULL;
2214 }
2215 
2216 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2217 			      const unsigned long *supported,
2218 			      const unsigned long *advertising)
2219 {
2220 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2221 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2222 	struct phylink_link_state config;
2223 	phy_interface_t iface;
2224 	bool changed;
2225 	int ret;
2226 
2227 	linkmode_copy(support, supported);
2228 
2229 	memset(&config, 0, sizeof(config));
2230 	linkmode_copy(config.advertising, advertising);
2231 	config.interface = PHY_INTERFACE_MODE_NA;
2232 	config.speed = SPEED_UNKNOWN;
2233 	config.duplex = DUPLEX_UNKNOWN;
2234 	config.pause = MLO_PAUSE_AN;
2235 	config.an_enabled = pl->link_config.an_enabled;
2236 
2237 	/* Ignore errors if we're expecting a PHY to attach later */
2238 	ret = phylink_validate(pl, support, &config);
2239 	if (ret) {
2240 		phylink_err(pl, "validation with support %*pb failed: %d\n",
2241 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2242 		return ret;
2243 	}
2244 
2245 	iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2246 	if (iface == PHY_INTERFACE_MODE_NA) {
2247 		phylink_err(pl,
2248 			    "selection of interface failed, advertisement %*pb\n",
2249 			    __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2250 		return -EINVAL;
2251 	}
2252 
2253 	config.interface = iface;
2254 	linkmode_copy(support1, support);
2255 	ret = phylink_validate(pl, support1, &config);
2256 	if (ret) {
2257 		phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
2258 			    phylink_an_mode_str(mode),
2259 			    phy_modes(config.interface),
2260 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2261 		return ret;
2262 	}
2263 
2264 	phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2265 		    phylink_an_mode_str(mode), phy_modes(config.interface),
2266 		    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2267 
2268 	if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2269 		return -EINVAL;
2270 
2271 	changed = !linkmode_equal(pl->supported, support) ||
2272 		  !linkmode_equal(pl->link_config.advertising,
2273 				  config.advertising);
2274 	if (changed) {
2275 		linkmode_copy(pl->supported, support);
2276 		linkmode_copy(pl->link_config.advertising, config.advertising);
2277 	}
2278 
2279 	if (pl->cur_link_an_mode != mode ||
2280 	    pl->link_config.interface != config.interface) {
2281 		pl->link_config.interface = config.interface;
2282 		pl->cur_link_an_mode = mode;
2283 
2284 		changed = true;
2285 
2286 		phylink_info(pl, "switched to %s/%s link mode\n",
2287 			     phylink_an_mode_str(mode),
2288 			     phy_modes(config.interface));
2289 	}
2290 
2291 	pl->link_port = pl->sfp_port;
2292 
2293 	if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2294 				 &pl->phylink_disable_state))
2295 		phylink_mac_initial_config(pl, false);
2296 
2297 	return ret;
2298 }
2299 
2300 static int phylink_sfp_module_insert(void *upstream,
2301 				     const struct sfp_eeprom_id *id)
2302 {
2303 	struct phylink *pl = upstream;
2304 	unsigned long *support = pl->sfp_support;
2305 
2306 	ASSERT_RTNL();
2307 
2308 	linkmode_zero(support);
2309 	sfp_parse_support(pl->sfp_bus, id, support);
2310 	pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2311 
2312 	/* If this module may have a PHY connecting later, defer until later */
2313 	pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2314 	if (pl->sfp_may_have_phy)
2315 		return 0;
2316 
2317 	return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2318 }
2319 
2320 static int phylink_sfp_module_start(void *upstream)
2321 {
2322 	struct phylink *pl = upstream;
2323 
2324 	/* If this SFP module has a PHY, start the PHY now. */
2325 	if (pl->phydev) {
2326 		phy_start(pl->phydev);
2327 		return 0;
2328 	}
2329 
2330 	/* If the module may have a PHY but we didn't detect one we
2331 	 * need to configure the MAC here.
2332 	 */
2333 	if (!pl->sfp_may_have_phy)
2334 		return 0;
2335 
2336 	return phylink_sfp_config(pl, MLO_AN_INBAND,
2337 				  pl->sfp_support, pl->sfp_support);
2338 }
2339 
2340 static void phylink_sfp_module_stop(void *upstream)
2341 {
2342 	struct phylink *pl = upstream;
2343 
2344 	/* If this SFP module has a PHY, stop it. */
2345 	if (pl->phydev)
2346 		phy_stop(pl->phydev);
2347 }
2348 
2349 static void phylink_sfp_link_down(void *upstream)
2350 {
2351 	struct phylink *pl = upstream;
2352 
2353 	ASSERT_RTNL();
2354 
2355 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2356 }
2357 
2358 static void phylink_sfp_link_up(void *upstream)
2359 {
2360 	struct phylink *pl = upstream;
2361 
2362 	ASSERT_RTNL();
2363 
2364 	clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2365 	phylink_run_resolve(pl);
2366 }
2367 
2368 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2369  * or 802.3z control word, so inband will not work.
2370  */
2371 static bool phylink_phy_no_inband(struct phy_device *phy)
2372 {
2373 	return phy->is_c45 &&
2374 		(phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2375 }
2376 
2377 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2378 {
2379 	struct phylink *pl = upstream;
2380 	phy_interface_t interface;
2381 	u8 mode;
2382 	int ret;
2383 
2384 	/*
2385 	 * This is the new way of dealing with flow control for PHYs,
2386 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2387 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2388 	 * using our validate call to the MAC, we rely upon the MAC
2389 	 * clearing the bits from both supported and advertising fields.
2390 	 */
2391 	phy_support_asym_pause(phy);
2392 
2393 	if (phylink_phy_no_inband(phy))
2394 		mode = MLO_AN_PHY;
2395 	else
2396 		mode = MLO_AN_INBAND;
2397 
2398 	/* Do the initial configuration */
2399 	ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2400 	if (ret < 0)
2401 		return ret;
2402 
2403 	interface = pl->link_config.interface;
2404 	ret = phylink_attach_phy(pl, phy, interface);
2405 	if (ret < 0)
2406 		return ret;
2407 
2408 	ret = phylink_bringup_phy(pl, phy, interface);
2409 	if (ret)
2410 		phy_detach(phy);
2411 
2412 	return ret;
2413 }
2414 
2415 static void phylink_sfp_disconnect_phy(void *upstream)
2416 {
2417 	phylink_disconnect_phy(upstream);
2418 }
2419 
2420 static const struct sfp_upstream_ops sfp_phylink_ops = {
2421 	.attach = phylink_sfp_attach,
2422 	.detach = phylink_sfp_detach,
2423 	.module_insert = phylink_sfp_module_insert,
2424 	.module_start = phylink_sfp_module_start,
2425 	.module_stop = phylink_sfp_module_stop,
2426 	.link_up = phylink_sfp_link_up,
2427 	.link_down = phylink_sfp_link_down,
2428 	.connect_phy = phylink_sfp_connect_phy,
2429 	.disconnect_phy = phylink_sfp_disconnect_phy,
2430 };
2431 
2432 /* Helpers for MAC drivers */
2433 
2434 /**
2435  * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2436  * @state: a pointer to a &struct phylink_link_state
2437  *
2438  * Inspect the interface mode, advertising mask or forced speed and
2439  * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2440  * the interface mode to suit.  @state->interface is appropriately
2441  * updated, and the advertising mask has the "other" baseX_Full flag
2442  * cleared.
2443  */
2444 void phylink_helper_basex_speed(struct phylink_link_state *state)
2445 {
2446 	if (phy_interface_mode_is_8023z(state->interface)) {
2447 		bool want_2500 = state->an_enabled ?
2448 			phylink_test(state->advertising, 2500baseX_Full) :
2449 			state->speed == SPEED_2500;
2450 
2451 		if (want_2500) {
2452 			phylink_clear(state->advertising, 1000baseX_Full);
2453 			state->interface = PHY_INTERFACE_MODE_2500BASEX;
2454 		} else {
2455 			phylink_clear(state->advertising, 2500baseX_Full);
2456 			state->interface = PHY_INTERFACE_MODE_1000BASEX;
2457 		}
2458 	}
2459 }
2460 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2461 
2462 static void phylink_decode_c37_word(struct phylink_link_state *state,
2463 				    uint16_t config_reg, int speed)
2464 {
2465 	bool tx_pause, rx_pause;
2466 	int fd_bit;
2467 
2468 	if (speed == SPEED_2500)
2469 		fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2470 	else
2471 		fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2472 
2473 	mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2474 
2475 	if (linkmode_test_bit(fd_bit, state->advertising) &&
2476 	    linkmode_test_bit(fd_bit, state->lp_advertising)) {
2477 		state->speed = speed;
2478 		state->duplex = DUPLEX_FULL;
2479 	} else {
2480 		/* negotiation failure */
2481 		state->link = false;
2482 	}
2483 
2484 	linkmode_resolve_pause(state->advertising, state->lp_advertising,
2485 			       &tx_pause, &rx_pause);
2486 
2487 	if (tx_pause)
2488 		state->pause |= MLO_PAUSE_TX;
2489 	if (rx_pause)
2490 		state->pause |= MLO_PAUSE_RX;
2491 }
2492 
2493 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2494 				      uint16_t config_reg)
2495 {
2496 	if (!(config_reg & LPA_SGMII_LINK)) {
2497 		state->link = false;
2498 		return;
2499 	}
2500 
2501 	switch (config_reg & LPA_SGMII_SPD_MASK) {
2502 	case LPA_SGMII_10:
2503 		state->speed = SPEED_10;
2504 		break;
2505 	case LPA_SGMII_100:
2506 		state->speed = SPEED_100;
2507 		break;
2508 	case LPA_SGMII_1000:
2509 		state->speed = SPEED_1000;
2510 		break;
2511 	default:
2512 		state->link = false;
2513 		return;
2514 	}
2515 	if (config_reg & LPA_SGMII_FULL_DUPLEX)
2516 		state->duplex = DUPLEX_FULL;
2517 	else
2518 		state->duplex = DUPLEX_HALF;
2519 }
2520 
2521 /**
2522  * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2523  * @state: a pointer to a struct phylink_link_state.
2524  * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2525  *
2526  * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2527  * code word.  Decode the USXGMII code word and populate the corresponding fields
2528  * (speed, duplex) into the phylink_link_state structure.
2529  */
2530 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2531 				 uint16_t lpa)
2532 {
2533 	switch (lpa & MDIO_USXGMII_SPD_MASK) {
2534 	case MDIO_USXGMII_10:
2535 		state->speed = SPEED_10;
2536 		break;
2537 	case MDIO_USXGMII_100:
2538 		state->speed = SPEED_100;
2539 		break;
2540 	case MDIO_USXGMII_1000:
2541 		state->speed = SPEED_1000;
2542 		break;
2543 	case MDIO_USXGMII_2500:
2544 		state->speed = SPEED_2500;
2545 		break;
2546 	case MDIO_USXGMII_5000:
2547 		state->speed = SPEED_5000;
2548 		break;
2549 	case MDIO_USXGMII_10G:
2550 		state->speed = SPEED_10000;
2551 		break;
2552 	default:
2553 		state->link = false;
2554 		return;
2555 	}
2556 
2557 	if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2558 		state->duplex = DUPLEX_FULL;
2559 	else
2560 		state->duplex = DUPLEX_HALF;
2561 }
2562 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2563 
2564 /**
2565  * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2566  * @pcs: a pointer to a &struct mdio_device.
2567  * @state: a pointer to a &struct phylink_link_state.
2568  *
2569  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2570  * clause 37 negotiation and/or SGMII control.
2571  *
2572  * Read the MAC PCS state from the MII device configured in @config and
2573  * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2574  * the phylink @state structure. This is suitable to be directly plugged
2575  * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2576  * structure.
2577  */
2578 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2579 				   struct phylink_link_state *state)
2580 {
2581 	int bmsr, lpa;
2582 
2583 	bmsr = mdiodev_read(pcs, MII_BMSR);
2584 	lpa = mdiodev_read(pcs, MII_LPA);
2585 	if (bmsr < 0 || lpa < 0) {
2586 		state->link = false;
2587 		return;
2588 	}
2589 
2590 	state->link = !!(bmsr & BMSR_LSTATUS);
2591 	state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2592 	/* If there is no link or autonegotiation is disabled, the LP advertisement
2593 	 * data is not meaningful, so don't go any further.
2594 	 */
2595 	if (!state->link || !state->an_enabled)
2596 		return;
2597 
2598 	switch (state->interface) {
2599 	case PHY_INTERFACE_MODE_1000BASEX:
2600 		phylink_decode_c37_word(state, lpa, SPEED_1000);
2601 		break;
2602 
2603 	case PHY_INTERFACE_MODE_2500BASEX:
2604 		phylink_decode_c37_word(state, lpa, SPEED_2500);
2605 		break;
2606 
2607 	case PHY_INTERFACE_MODE_SGMII:
2608 	case PHY_INTERFACE_MODE_QSGMII:
2609 		phylink_decode_sgmii_word(state, lpa);
2610 		break;
2611 
2612 	default:
2613 		state->link = false;
2614 		break;
2615 	}
2616 }
2617 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2618 
2619 /**
2620  * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2621  *	advertisement
2622  * @pcs: a pointer to a &struct mdio_device.
2623  * @interface: the PHY interface mode being configured
2624  * @advertising: the ethtool advertisement mask
2625  *
2626  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2627  * clause 37 negotiation and/or SGMII control.
2628  *
2629  * Configure the clause 37 PCS advertisement as specified by @state. This
2630  * does not trigger a renegotiation; phylink will do that via the
2631  * mac_an_restart() method of the struct phylink_mac_ops structure.
2632  *
2633  * Returns negative error code on failure to configure the advertisement,
2634  * zero if no change has been made, or one if the advertisement has changed.
2635  */
2636 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
2637 					  phy_interface_t interface,
2638 					  const unsigned long *advertising)
2639 {
2640 	u16 adv;
2641 
2642 	switch (interface) {
2643 	case PHY_INTERFACE_MODE_1000BASEX:
2644 	case PHY_INTERFACE_MODE_2500BASEX:
2645 		adv = ADVERTISE_1000XFULL;
2646 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2647 				      advertising))
2648 			adv |= ADVERTISE_1000XPAUSE;
2649 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2650 				      advertising))
2651 			adv |= ADVERTISE_1000XPSE_ASYM;
2652 
2653 		return mdiodev_modify_changed(pcs, MII_ADVERTISE, 0xffff, adv);
2654 
2655 	case PHY_INTERFACE_MODE_SGMII:
2656 		return mdiodev_modify_changed(pcs, MII_ADVERTISE, 0xffff, 0x0001);
2657 
2658 	default:
2659 		/* Nothing to do for other modes */
2660 		return 0;
2661 	}
2662 }
2663 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2664 
2665 /**
2666  * phylink_mii_c22_pcs_config() - configure clause 22 PCS
2667  * @pcs: a pointer to a &struct mdio_device.
2668  * @mode: link autonegotiation mode
2669  * @interface: the PHY interface mode being configured
2670  * @advertising: the ethtool advertisement mask
2671  *
2672  * Configure a Clause 22 PCS PHY with the appropriate negotiation
2673  * parameters for the @mode, @interface and @advertising parameters.
2674  * Returns negative error number on failure, zero if the advertisement
2675  * has not changed, or positive if there is a change.
2676  */
2677 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
2678 			       phy_interface_t interface,
2679 			       const unsigned long *advertising)
2680 {
2681 	bool changed;
2682 	u16 bmcr;
2683 	int ret;
2684 
2685 	ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface,
2686 						    advertising);
2687 	if (ret < 0)
2688 		return ret;
2689 
2690 	changed = ret > 0;
2691 
2692 	/* Ensure ISOLATE bit is disabled */
2693 	if (mode == MLO_AN_INBAND &&
2694 	    linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, advertising))
2695 		bmcr = BMCR_ANENABLE;
2696 	else
2697 		bmcr = 0;
2698 
2699 	ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
2700 	if (ret < 0)
2701 		return ret;
2702 
2703 	return changed ? 1 : 0;
2704 }
2705 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
2706 
2707 /**
2708  * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2709  * @pcs: a pointer to a &struct mdio_device.
2710  *
2711  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2712  * clause 37 negotiation.
2713  *
2714  * Restart the clause 37 negotiation with the link partner. This is
2715  * suitable to be directly plugged into the mac_pcs_get_state() member
2716  * of the struct phylink_mac_ops structure.
2717  */
2718 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2719 {
2720 	int val = mdiodev_read(pcs, MII_BMCR);
2721 
2722 	if (val >= 0) {
2723 		val |= BMCR_ANRESTART;
2724 
2725 		mdiodev_write(pcs, MII_BMCR, val);
2726 	}
2727 }
2728 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2729 
2730 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2731 				   struct phylink_link_state *state)
2732 {
2733 	struct mii_bus *bus = pcs->bus;
2734 	int addr = pcs->addr;
2735 	int stat;
2736 
2737 	stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
2738 	if (stat < 0) {
2739 		state->link = false;
2740 		return;
2741 	}
2742 
2743 	state->link = !!(stat & MDIO_STAT1_LSTATUS);
2744 	if (!state->link)
2745 		return;
2746 
2747 	switch (state->interface) {
2748 	case PHY_INTERFACE_MODE_10GBASER:
2749 		state->speed = SPEED_10000;
2750 		state->duplex = DUPLEX_FULL;
2751 		break;
2752 
2753 	default:
2754 		break;
2755 	}
2756 }
2757 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2758 
2759 MODULE_LICENSE("GPL v2");
2760