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