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