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