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