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