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