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