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