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