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