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