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