• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * phylink models the MAC to optional PHY connection, supporting
4  * technologies such as SFP cages where the PHY is hot-pluggable.
5  *
6  * Copyright (C) 2015 Russell King
7  */
8 #include <linux/acpi.h>
9 #include <linux/ethtool.h>
10 #include <linux/export.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/netdevice.h>
13 #include <linux/of.h>
14 #include <linux/of_mdio.h>
15 #include <linux/phy.h>
16 #include <linux/phy_fixed.h>
17 #include <linux/phylink.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/spinlock.h>
20 #include <linux/timer.h>
21 #include <linux/workqueue.h>
22 
23 #include "sfp.h"
24 #include "swphy.h"
25 
26 #define SUPPORTED_INTERFACES \
27 	(SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
28 	 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
29 #define ADVERTISED_INTERFACES \
30 	(ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
31 	 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
32 
33 enum {
34 	PHYLINK_DISABLE_STOPPED,
35 	PHYLINK_DISABLE_LINK,
36 	PHYLINK_DISABLE_MAC_WOL,
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 *mac_ops;
46 	const struct phylink_pcs_ops *pcs_ops;
47 	struct phylink_config *config;
48 	struct phylink_pcs *pcs;
49 	struct device *dev;
50 	unsigned int old_link_state:1;
51 
52 	unsigned long phylink_disable_state; /* bitmask of disables */
53 	struct phy_device *phydev;
54 	phy_interface_t link_interface;	/* PHY_INTERFACE_xxx */
55 	u8 cfg_link_an_mode;		/* MLO_AN_xxx */
56 	u8 cur_link_an_mode;
57 	u8 link_port;			/* The current non-phy ethtool port */
58 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
59 
60 	/* The link configuration settings */
61 	struct phylink_link_state link_config;
62 
63 	/* The current settings */
64 	phy_interface_t cur_interface;
65 
66 	struct gpio_desc *link_gpio;
67 	unsigned int link_irq;
68 	struct timer_list link_poll;
69 	void (*get_fixed_state)(struct net_device *dev,
70 				struct phylink_link_state *s);
71 
72 	struct mutex state_mutex;
73 	struct phylink_link_state phy_state;
74 	struct work_struct resolve;
75 
76 	bool mac_link_dropped;
77 
78 	struct sfp_bus *sfp_bus;
79 	bool sfp_may_have_phy;
80 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
81 	u8 sfp_port;
82 };
83 
84 #define phylink_printk(level, pl, fmt, ...) \
85 	do { \
86 		if ((pl)->config->type == PHYLINK_NETDEV) \
87 			netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
88 		else if ((pl)->config->type == PHYLINK_DEV) \
89 			dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
90 	} while (0)
91 
92 #define phylink_err(pl, fmt, ...) \
93 	phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
94 #define phylink_warn(pl, fmt, ...) \
95 	phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
96 #define phylink_info(pl, fmt, ...) \
97 	phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
98 #if defined(CONFIG_DYNAMIC_DEBUG)
99 #define phylink_dbg(pl, fmt, ...) \
100 do {									\
101 	if ((pl)->config->type == PHYLINK_NETDEV)			\
102 		netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__);		\
103 	else if ((pl)->config->type == PHYLINK_DEV)			\
104 		dev_dbg((pl)->dev, fmt, ##__VA_ARGS__);			\
105 } while (0)
106 #elif defined(DEBUG)
107 #define phylink_dbg(pl, fmt, ...)					\
108 	phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
109 #else
110 #define phylink_dbg(pl, fmt, ...)					\
111 ({									\
112 	if (0)								\
113 		phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__);	\
114 })
115 #endif
116 
117 /**
118  * phylink_set_port_modes() - set the port type modes in the ethtool mask
119  * @mask: ethtool link mode mask
120  *
121  * Sets all the port type modes in the ethtool mask.  MAC drivers should
122  * use this in their 'validate' callback.
123  */
phylink_set_port_modes(unsigned long * mask)124 void phylink_set_port_modes(unsigned long *mask)
125 {
126 	phylink_set(mask, TP);
127 	phylink_set(mask, AUI);
128 	phylink_set(mask, MII);
129 	phylink_set(mask, FIBRE);
130 	phylink_set(mask, BNC);
131 	phylink_set(mask, Backplane);
132 }
133 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
134 
phylink_is_empty_linkmode(const unsigned long * linkmode)135 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
136 {
137 	__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
138 
139 	phylink_set_port_modes(tmp);
140 	phylink_set(tmp, Autoneg);
141 	phylink_set(tmp, Pause);
142 	phylink_set(tmp, Asym_Pause);
143 
144 	return linkmode_subset(linkmode, tmp);
145 }
146 
phylink_an_mode_str(unsigned int mode)147 static const char *phylink_an_mode_str(unsigned int mode)
148 {
149 	static const char *modestr[] = {
150 		[MLO_AN_PHY] = "phy",
151 		[MLO_AN_FIXED] = "fixed",
152 		[MLO_AN_INBAND] = "inband",
153 	};
154 
155 	return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
156 }
157 
phylink_validate(struct phylink * pl,unsigned long * supported,struct phylink_link_state * state)158 static int phylink_validate(struct phylink *pl, unsigned long *supported,
159 			    struct phylink_link_state *state)
160 {
161 	pl->mac_ops->validate(pl->config, supported, state);
162 
163 	return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
164 }
165 
phylink_parse_fixedlink(struct phylink * pl,struct fwnode_handle * fwnode)166 static int phylink_parse_fixedlink(struct phylink *pl,
167 				   struct fwnode_handle *fwnode)
168 {
169 	struct fwnode_handle *fixed_node;
170 	const struct phy_setting *s;
171 	struct gpio_desc *desc;
172 	u32 speed;
173 	int ret;
174 
175 	fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
176 	if (fixed_node) {
177 		ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
178 
179 		pl->link_config.speed = speed;
180 		pl->link_config.duplex = DUPLEX_HALF;
181 
182 		if (fwnode_property_read_bool(fixed_node, "full-duplex"))
183 			pl->link_config.duplex = DUPLEX_FULL;
184 
185 		/* We treat the "pause" and "asym-pause" terminology as
186 		 * defining the link partner's ability.
187 		 */
188 		if (fwnode_property_read_bool(fixed_node, "pause"))
189 			__set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
190 				  pl->link_config.lp_advertising);
191 		if (fwnode_property_read_bool(fixed_node, "asym-pause"))
192 			__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
193 				  pl->link_config.lp_advertising);
194 
195 		if (ret == 0) {
196 			desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
197 						      GPIOD_IN, "?");
198 
199 			if (!IS_ERR(desc))
200 				pl->link_gpio = desc;
201 			else if (desc == ERR_PTR(-EPROBE_DEFER))
202 				ret = -EPROBE_DEFER;
203 		}
204 		fwnode_handle_put(fixed_node);
205 
206 		if (ret)
207 			return ret;
208 	} else {
209 		u32 prop[5];
210 
211 		ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
212 						     NULL, 0);
213 		if (ret != ARRAY_SIZE(prop)) {
214 			phylink_err(pl, "broken fixed-link?\n");
215 			return -EINVAL;
216 		}
217 
218 		ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
219 						     prop, ARRAY_SIZE(prop));
220 		if (!ret) {
221 			pl->link_config.duplex = prop[1] ?
222 						DUPLEX_FULL : DUPLEX_HALF;
223 			pl->link_config.speed = prop[2];
224 			if (prop[3])
225 				__set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
226 					  pl->link_config.lp_advertising);
227 			if (prop[4])
228 				__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
229 					  pl->link_config.lp_advertising);
230 		}
231 	}
232 
233 	if (pl->link_config.speed > SPEED_1000 &&
234 	    pl->link_config.duplex != DUPLEX_FULL)
235 		phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
236 			     pl->link_config.speed);
237 
238 	bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
239 	linkmode_copy(pl->link_config.advertising, pl->supported);
240 	phylink_validate(pl, pl->supported, &pl->link_config);
241 
242 	s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
243 			       pl->supported, true);
244 	linkmode_zero(pl->supported);
245 	phylink_set(pl->supported, MII);
246 	phylink_set(pl->supported, Pause);
247 	phylink_set(pl->supported, Asym_Pause);
248 	phylink_set(pl->supported, Autoneg);
249 	if (s) {
250 		__set_bit(s->bit, pl->supported);
251 		__set_bit(s->bit, pl->link_config.lp_advertising);
252 	} else {
253 		phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
254 			     pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
255 			     pl->link_config.speed);
256 	}
257 
258 	linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
259 		     pl->supported);
260 
261 	pl->link_config.link = 1;
262 	pl->link_config.an_complete = 1;
263 
264 	return 0;
265 }
266 
phylink_parse_mode(struct phylink * pl,struct fwnode_handle * fwnode)267 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
268 {
269 	struct fwnode_handle *dn;
270 	const char *managed;
271 
272 	dn = fwnode_get_named_child_node(fwnode, "fixed-link");
273 	if (dn || fwnode_property_present(fwnode, "fixed-link"))
274 		pl->cfg_link_an_mode = MLO_AN_FIXED;
275 	fwnode_handle_put(dn);
276 
277 	if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
278 	     strcmp(managed, "in-band-status") == 0) ||
279 	    pl->config->ovr_an_inband) {
280 		if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
281 			phylink_err(pl,
282 				    "can't use both fixed-link and in-band-status\n");
283 			return -EINVAL;
284 		}
285 
286 		linkmode_zero(pl->supported);
287 		phylink_set(pl->supported, MII);
288 		phylink_set(pl->supported, Autoneg);
289 		phylink_set(pl->supported, Asym_Pause);
290 		phylink_set(pl->supported, Pause);
291 		pl->link_config.an_enabled = true;
292 		pl->cfg_link_an_mode = MLO_AN_INBAND;
293 
294 		switch (pl->link_config.interface) {
295 		case PHY_INTERFACE_MODE_SGMII:
296 		case PHY_INTERFACE_MODE_QSGMII:
297 			phylink_set(pl->supported, 10baseT_Half);
298 			phylink_set(pl->supported, 10baseT_Full);
299 			phylink_set(pl->supported, 100baseT_Half);
300 			phylink_set(pl->supported, 100baseT_Full);
301 			phylink_set(pl->supported, 1000baseT_Half);
302 			phylink_set(pl->supported, 1000baseT_Full);
303 			break;
304 
305 		case PHY_INTERFACE_MODE_1000BASEX:
306 			phylink_set(pl->supported, 1000baseX_Full);
307 			break;
308 
309 		case PHY_INTERFACE_MODE_2500BASEX:
310 			phylink_set(pl->supported, 2500baseX_Full);
311 			break;
312 
313 		case PHY_INTERFACE_MODE_5GBASER:
314 			phylink_set(pl->supported, 5000baseT_Full);
315 			break;
316 
317 		case PHY_INTERFACE_MODE_25GBASER:
318 			phylink_set(pl->supported, 25000baseCR_Full);
319 			phylink_set(pl->supported, 25000baseKR_Full);
320 			phylink_set(pl->supported, 25000baseSR_Full);
321 			fallthrough;
322 		case PHY_INTERFACE_MODE_USXGMII:
323 		case PHY_INTERFACE_MODE_10GKR:
324 		case PHY_INTERFACE_MODE_10GBASER:
325 			phylink_set(pl->supported, 10baseT_Half);
326 			phylink_set(pl->supported, 10baseT_Full);
327 			phylink_set(pl->supported, 100baseT_Half);
328 			phylink_set(pl->supported, 100baseT_Full);
329 			phylink_set(pl->supported, 1000baseT_Half);
330 			phylink_set(pl->supported, 1000baseT_Full);
331 			phylink_set(pl->supported, 1000baseX_Full);
332 			phylink_set(pl->supported, 1000baseKX_Full);
333 			phylink_set(pl->supported, 2500baseT_Full);
334 			phylink_set(pl->supported, 2500baseX_Full);
335 			phylink_set(pl->supported, 5000baseT_Full);
336 			phylink_set(pl->supported, 10000baseT_Full);
337 			phylink_set(pl->supported, 10000baseKR_Full);
338 			phylink_set(pl->supported, 10000baseKX4_Full);
339 			phylink_set(pl->supported, 10000baseCR_Full);
340 			phylink_set(pl->supported, 10000baseSR_Full);
341 			phylink_set(pl->supported, 10000baseLR_Full);
342 			phylink_set(pl->supported, 10000baseLRM_Full);
343 			phylink_set(pl->supported, 10000baseER_Full);
344 			break;
345 
346 		case PHY_INTERFACE_MODE_XLGMII:
347 			phylink_set(pl->supported, 25000baseCR_Full);
348 			phylink_set(pl->supported, 25000baseKR_Full);
349 			phylink_set(pl->supported, 25000baseSR_Full);
350 			phylink_set(pl->supported, 40000baseKR4_Full);
351 			phylink_set(pl->supported, 40000baseCR4_Full);
352 			phylink_set(pl->supported, 40000baseSR4_Full);
353 			phylink_set(pl->supported, 40000baseLR4_Full);
354 			phylink_set(pl->supported, 50000baseCR2_Full);
355 			phylink_set(pl->supported, 50000baseKR2_Full);
356 			phylink_set(pl->supported, 50000baseSR2_Full);
357 			phylink_set(pl->supported, 50000baseKR_Full);
358 			phylink_set(pl->supported, 50000baseSR_Full);
359 			phylink_set(pl->supported, 50000baseCR_Full);
360 			phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
361 			phylink_set(pl->supported, 50000baseDR_Full);
362 			phylink_set(pl->supported, 100000baseKR4_Full);
363 			phylink_set(pl->supported, 100000baseSR4_Full);
364 			phylink_set(pl->supported, 100000baseCR4_Full);
365 			phylink_set(pl->supported, 100000baseLR4_ER4_Full);
366 			phylink_set(pl->supported, 100000baseKR2_Full);
367 			phylink_set(pl->supported, 100000baseSR2_Full);
368 			phylink_set(pl->supported, 100000baseCR2_Full);
369 			phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
370 			phylink_set(pl->supported, 100000baseDR2_Full);
371 			break;
372 
373 		default:
374 			phylink_err(pl,
375 				    "incorrect link mode %s for in-band status\n",
376 				    phy_modes(pl->link_config.interface));
377 			return -EINVAL;
378 		}
379 
380 		linkmode_copy(pl->link_config.advertising, pl->supported);
381 
382 		if (phylink_validate(pl, pl->supported, &pl->link_config)) {
383 			phylink_err(pl,
384 				    "failed to validate link configuration for in-band status\n");
385 			return -EINVAL;
386 		}
387 
388 		/* Check if MAC/PCS also supports Autoneg. */
389 		pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
390 	}
391 
392 	return 0;
393 }
394 
phylink_apply_manual_flow(struct phylink * pl,struct phylink_link_state * state)395 static void phylink_apply_manual_flow(struct phylink *pl,
396 				      struct phylink_link_state *state)
397 {
398 	/* If autoneg is disabled, pause AN is also disabled */
399 	if (!state->an_enabled)
400 		state->pause &= ~MLO_PAUSE_AN;
401 
402 	/* Manual configuration of pause modes */
403 	if (!(pl->link_config.pause & MLO_PAUSE_AN))
404 		state->pause = pl->link_config.pause;
405 }
406 
phylink_resolve_flow(struct phylink_link_state * state)407 static void phylink_resolve_flow(struct phylink_link_state *state)
408 {
409 	bool tx_pause, rx_pause;
410 
411 	state->pause = MLO_PAUSE_NONE;
412 	if (state->duplex == DUPLEX_FULL) {
413 		linkmode_resolve_pause(state->advertising,
414 				       state->lp_advertising,
415 				       &tx_pause, &rx_pause);
416 		if (tx_pause)
417 			state->pause |= MLO_PAUSE_TX;
418 		if (rx_pause)
419 			state->pause |= MLO_PAUSE_RX;
420 	}
421 }
422 
phylink_mac_config(struct phylink * pl,const struct phylink_link_state * state)423 static void phylink_mac_config(struct phylink *pl,
424 			       const struct phylink_link_state *state)
425 {
426 	phylink_dbg(pl,
427 		    "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
428 		    __func__, phylink_an_mode_str(pl->cur_link_an_mode),
429 		    phy_modes(state->interface),
430 		    phy_speed_to_str(state->speed),
431 		    phy_duplex_to_str(state->duplex),
432 		    __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
433 		    state->pause, state->link, state->an_enabled);
434 
435 	pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
436 }
437 
phylink_mac_pcs_an_restart(struct phylink * pl)438 static void phylink_mac_pcs_an_restart(struct phylink *pl)
439 {
440 	if (pl->link_config.an_enabled &&
441 	    phy_interface_mode_is_8023z(pl->link_config.interface) &&
442 	    phylink_autoneg_inband(pl->cur_link_an_mode)) {
443 		if (pl->pcs_ops)
444 			pl->pcs_ops->pcs_an_restart(pl->pcs);
445 		else
446 			pl->mac_ops->mac_an_restart(pl->config);
447 	}
448 }
449 
phylink_major_config(struct phylink * pl,bool restart,const struct phylink_link_state * state)450 static void phylink_major_config(struct phylink *pl, bool restart,
451 				  const struct phylink_link_state *state)
452 {
453 	int err;
454 
455 	phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
456 
457 	if (pl->mac_ops->mac_prepare) {
458 		err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
459 					       state->interface);
460 		if (err < 0) {
461 			phylink_err(pl, "mac_prepare failed: %pe\n",
462 				    ERR_PTR(err));
463 			return;
464 		}
465 	}
466 
467 	phylink_mac_config(pl, state);
468 
469 	if (pl->pcs_ops) {
470 		err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
471 					      state->interface,
472 					      state->advertising,
473 					      !!(pl->link_config.pause &
474 						 MLO_PAUSE_AN));
475 		if (err < 0)
476 			phylink_err(pl, "pcs_config failed: %pe\n",
477 				    ERR_PTR(err));
478 		if (err > 0)
479 			restart = true;
480 	}
481 	if (restart)
482 		phylink_mac_pcs_an_restart(pl);
483 
484 	if (pl->mac_ops->mac_finish) {
485 		err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
486 					      state->interface);
487 		if (err < 0)
488 			phylink_err(pl, "mac_finish failed: %pe\n",
489 				    ERR_PTR(err));
490 	}
491 }
492 
493 /*
494  * Reconfigure for a change of inband advertisement.
495  * If we have a separate PCS, we only need to call its pcs_config() method,
496  * and then restart AN if it indicates something changed. Otherwise, we do
497  * the full MAC reconfiguration.
498  */
phylink_change_inband_advert(struct phylink * pl)499 static int phylink_change_inband_advert(struct phylink *pl)
500 {
501 	int ret;
502 
503 	if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
504 		return 0;
505 
506 	if (!pl->pcs_ops) {
507 		/* Legacy method */
508 		phylink_mac_config(pl, &pl->link_config);
509 		phylink_mac_pcs_an_restart(pl);
510 		return 0;
511 	}
512 
513 	phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
514 		    phylink_an_mode_str(pl->cur_link_an_mode),
515 		    phy_modes(pl->link_config.interface),
516 		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
517 		    pl->link_config.pause);
518 
519 	/* Modern PCS-based method; update the advert at the PCS, and
520 	 * restart negotiation if the pcs_config() helper indicates that
521 	 * the programmed advertisement has changed.
522 	 */
523 	ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
524 				      pl->link_config.interface,
525 				      pl->link_config.advertising,
526 				      !!(pl->link_config.pause & MLO_PAUSE_AN));
527 	if (ret < 0)
528 		return ret;
529 
530 	if (ret > 0)
531 		phylink_mac_pcs_an_restart(pl);
532 
533 	return 0;
534 }
535 
phylink_mac_pcs_get_state(struct phylink * pl,struct phylink_link_state * state)536 static void phylink_mac_pcs_get_state(struct phylink *pl,
537 				      struct phylink_link_state *state)
538 {
539 	linkmode_copy(state->advertising, pl->link_config.advertising);
540 	linkmode_zero(state->lp_advertising);
541 	state->interface = pl->link_config.interface;
542 	state->an_enabled = pl->link_config.an_enabled;
543 	state->speed = SPEED_UNKNOWN;
544 	state->duplex = DUPLEX_UNKNOWN;
545 	state->pause = MLO_PAUSE_NONE;
546 	state->an_complete = 0;
547 	state->link = 1;
548 
549 	if (pl->pcs_ops)
550 		pl->pcs_ops->pcs_get_state(pl->pcs, state);
551 	else if (pl->mac_ops->mac_pcs_get_state)
552 		pl->mac_ops->mac_pcs_get_state(pl->config, state);
553 	else
554 		state->link = 0;
555 }
556 
557 /* The fixed state is... fixed except for the link state,
558  * which may be determined by a GPIO or a callback.
559  */
phylink_get_fixed_state(struct phylink * pl,struct phylink_link_state * state)560 static void phylink_get_fixed_state(struct phylink *pl,
561 				    struct phylink_link_state *state)
562 {
563 	*state = pl->link_config;
564 	if (pl->config->get_fixed_state)
565 		pl->config->get_fixed_state(pl->config, state);
566 	else if (pl->link_gpio)
567 		state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
568 
569 	phylink_resolve_flow(state);
570 }
571 
phylink_mac_initial_config(struct phylink * pl,bool force_restart)572 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
573 {
574 	struct phylink_link_state link_state;
575 
576 	switch (pl->cur_link_an_mode) {
577 	case MLO_AN_PHY:
578 		link_state = pl->phy_state;
579 		break;
580 
581 	case MLO_AN_FIXED:
582 		phylink_get_fixed_state(pl, &link_state);
583 		break;
584 
585 	case MLO_AN_INBAND:
586 		link_state = pl->link_config;
587 		if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
588 			link_state.pause = MLO_PAUSE_NONE;
589 		break;
590 
591 	default: /* can't happen */
592 		return;
593 	}
594 
595 	link_state.link = false;
596 
597 	phylink_apply_manual_flow(pl, &link_state);
598 	phylink_major_config(pl, force_restart, &link_state);
599 }
600 
phylink_pause_to_str(int pause)601 static const char *phylink_pause_to_str(int pause)
602 {
603 	switch (pause & MLO_PAUSE_TXRX_MASK) {
604 	case MLO_PAUSE_TX | MLO_PAUSE_RX:
605 		return "rx/tx";
606 	case MLO_PAUSE_TX:
607 		return "tx";
608 	case MLO_PAUSE_RX:
609 		return "rx";
610 	default:
611 		return "off";
612 	}
613 }
614 
phylink_link_up(struct phylink * pl,struct phylink_link_state link_state)615 static void phylink_link_up(struct phylink *pl,
616 			    struct phylink_link_state link_state)
617 {
618 	struct net_device *ndev = pl->netdev;
619 
620 	pl->cur_interface = link_state.interface;
621 
622 	if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
623 		pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
624 					 pl->cur_interface,
625 					 link_state.speed, link_state.duplex);
626 
627 	pl->mac_ops->mac_link_up(pl->config, pl->phydev,
628 				 pl->cur_link_an_mode, pl->cur_interface,
629 				 link_state.speed, link_state.duplex,
630 				 !!(link_state.pause & MLO_PAUSE_TX),
631 				 !!(link_state.pause & MLO_PAUSE_RX));
632 
633 	if (ndev)
634 		netif_carrier_on(ndev);
635 
636 	phylink_info(pl,
637 		     "Link is Up - %s/%s - flow control %s\n",
638 		     phy_speed_to_str(link_state.speed),
639 		     phy_duplex_to_str(link_state.duplex),
640 		     phylink_pause_to_str(link_state.pause));
641 }
642 
phylink_link_down(struct phylink * pl)643 static void phylink_link_down(struct phylink *pl)
644 {
645 	struct net_device *ndev = pl->netdev;
646 
647 	if (ndev)
648 		netif_carrier_off(ndev);
649 	pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
650 				   pl->cur_interface);
651 	phylink_info(pl, "Link is Down\n");
652 }
653 
phylink_resolve(struct work_struct * w)654 static void phylink_resolve(struct work_struct *w)
655 {
656 	struct phylink *pl = container_of(w, struct phylink, resolve);
657 	struct phylink_link_state link_state;
658 	struct net_device *ndev = pl->netdev;
659 	bool mac_config = false;
660 	bool retrigger = false;
661 	bool cur_link_state;
662 
663 	mutex_lock(&pl->state_mutex);
664 	if (pl->netdev)
665 		cur_link_state = netif_carrier_ok(ndev);
666 	else
667 		cur_link_state = pl->old_link_state;
668 
669 	if (pl->phylink_disable_state) {
670 		pl->mac_link_dropped = false;
671 		link_state.link = false;
672 	} else if (pl->mac_link_dropped) {
673 		link_state.link = false;
674 		retrigger = true;
675 	} else {
676 		switch (pl->cur_link_an_mode) {
677 		case MLO_AN_PHY:
678 			link_state = pl->phy_state;
679 			phylink_apply_manual_flow(pl, &link_state);
680 			mac_config = link_state.link;
681 			break;
682 
683 		case MLO_AN_FIXED:
684 			phylink_get_fixed_state(pl, &link_state);
685 			mac_config = link_state.link;
686 			break;
687 
688 		case MLO_AN_INBAND:
689 			phylink_mac_pcs_get_state(pl, &link_state);
690 
691 			/* The PCS may have a latching link-fail indicator.
692 			 * If the link was up, bring the link down and
693 			 * re-trigger the resolve. Otherwise, re-read the
694 			 * PCS state to get the current status of the link.
695 			 */
696 			if (!link_state.link) {
697 				if (cur_link_state)
698 					retrigger = true;
699 				else
700 					phylink_mac_pcs_get_state(pl,
701 								  &link_state);
702 			}
703 
704 			/* If we have a phy, the "up" state is the union of
705 			 * both the PHY and the MAC
706 			 */
707 			if (pl->phydev)
708 				link_state.link &= pl->phy_state.link;
709 
710 			/* Only update if the PHY link is up */
711 			if (pl->phydev && pl->phy_state.link) {
712 				/* If the interface has changed, force a
713 				 * link down event if the link isn't already
714 				 * down, and re-resolve.
715 				 */
716 				if (link_state.interface !=
717 				    pl->phy_state.interface) {
718 					retrigger = true;
719 					link_state.link = false;
720 				}
721 				link_state.interface = pl->phy_state.interface;
722 
723 				/* If we have a PHY, we need to update with
724 				 * the PHY flow control bits.
725 				 */
726 				link_state.pause = pl->phy_state.pause;
727 				mac_config = true;
728 			}
729 			phylink_apply_manual_flow(pl, &link_state);
730 			break;
731 		}
732 	}
733 
734 	if (mac_config) {
735 		if (link_state.interface != pl->link_config.interface) {
736 			/* The interface has changed, force the link down and
737 			 * then reconfigure.
738 			 */
739 			if (cur_link_state) {
740 				phylink_link_down(pl);
741 				cur_link_state = false;
742 			}
743 			phylink_major_config(pl, false, &link_state);
744 			pl->link_config.interface = link_state.interface;
745 		} else if (!pl->pcs_ops) {
746 			/* The interface remains unchanged, only the speed,
747 			 * duplex or pause settings have changed. Call the
748 			 * old mac_config() method to configure the MAC/PCS
749 			 * only if we do not have a PCS installed (an
750 			 * unconverted user.)
751 			 */
752 			phylink_mac_config(pl, &link_state);
753 		}
754 	}
755 
756 	if (link_state.link != cur_link_state) {
757 		pl->old_link_state = link_state.link;
758 		if (!link_state.link)
759 			phylink_link_down(pl);
760 		else
761 			phylink_link_up(pl, link_state);
762 	}
763 	if (!link_state.link && retrigger) {
764 		pl->mac_link_dropped = false;
765 		queue_work(system_power_efficient_wq, &pl->resolve);
766 	}
767 	mutex_unlock(&pl->state_mutex);
768 }
769 
phylink_run_resolve(struct phylink * pl)770 static void phylink_run_resolve(struct phylink *pl)
771 {
772 	if (!pl->phylink_disable_state)
773 		queue_work(system_power_efficient_wq, &pl->resolve);
774 }
775 
phylink_run_resolve_and_disable(struct phylink * pl,int bit)776 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
777 {
778 	unsigned long state = pl->phylink_disable_state;
779 
780 	set_bit(bit, &pl->phylink_disable_state);
781 	if (state == 0) {
782 		queue_work(system_power_efficient_wq, &pl->resolve);
783 		flush_work(&pl->resolve);
784 	}
785 }
786 
phylink_fixed_poll(struct timer_list * t)787 static void phylink_fixed_poll(struct timer_list *t)
788 {
789 	struct phylink *pl = container_of(t, struct phylink, link_poll);
790 
791 	mod_timer(t, jiffies + HZ);
792 
793 	phylink_run_resolve(pl);
794 }
795 
796 static const struct sfp_upstream_ops sfp_phylink_ops;
797 
phylink_register_sfp(struct phylink * pl,struct fwnode_handle * fwnode)798 static int phylink_register_sfp(struct phylink *pl,
799 				struct fwnode_handle *fwnode)
800 {
801 	struct sfp_bus *bus;
802 	int ret;
803 
804 	if (!fwnode)
805 		return 0;
806 
807 	bus = sfp_bus_find_fwnode(fwnode);
808 	if (IS_ERR(bus)) {
809 		ret = PTR_ERR(bus);
810 		phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
811 		return ret;
812 	}
813 
814 	pl->sfp_bus = bus;
815 
816 	ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
817 	sfp_bus_put(bus);
818 
819 	return ret;
820 }
821 
822 /**
823  * phylink_create() - create a phylink instance
824  * @config: a pointer to the target &struct phylink_config
825  * @fwnode: a pointer to a &struct fwnode_handle describing the network
826  *	interface
827  * @iface: the desired link mode defined by &typedef phy_interface_t
828  * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
829  *
830  * Create a new phylink instance, and parse the link parameters found in @np.
831  * This will parse in-band modes, fixed-link or SFP configuration.
832  *
833  * Note: the rtnl lock must not be held when calling this function.
834  *
835  * Returns a pointer to a &struct phylink, or an error-pointer value. Users
836  * must use IS_ERR() to check for errors from this function.
837  */
phylink_create(struct phylink_config * config,struct fwnode_handle * fwnode,phy_interface_t iface,const struct phylink_mac_ops * mac_ops)838 struct phylink *phylink_create(struct phylink_config *config,
839 			       struct fwnode_handle *fwnode,
840 			       phy_interface_t iface,
841 			       const struct phylink_mac_ops *mac_ops)
842 {
843 	struct phylink *pl;
844 	int ret;
845 
846 	pl = kzalloc(sizeof(*pl), GFP_KERNEL);
847 	if (!pl)
848 		return ERR_PTR(-ENOMEM);
849 
850 	mutex_init(&pl->state_mutex);
851 	INIT_WORK(&pl->resolve, phylink_resolve);
852 
853 	pl->config = config;
854 	if (config->type == PHYLINK_NETDEV) {
855 		pl->netdev = to_net_dev(config->dev);
856 		netif_carrier_off(pl->netdev);
857 	} else if (config->type == PHYLINK_DEV) {
858 		pl->dev = config->dev;
859 	} else {
860 		kfree(pl);
861 		return ERR_PTR(-EINVAL);
862 	}
863 
864 	pl->phy_state.interface = iface;
865 	pl->link_interface = iface;
866 	if (iface == PHY_INTERFACE_MODE_MOCA)
867 		pl->link_port = PORT_BNC;
868 	else
869 		pl->link_port = PORT_MII;
870 	pl->link_config.interface = iface;
871 	pl->link_config.pause = MLO_PAUSE_AN;
872 	pl->link_config.speed = SPEED_UNKNOWN;
873 	pl->link_config.duplex = DUPLEX_UNKNOWN;
874 	pl->link_config.an_enabled = true;
875 	pl->mac_ops = mac_ops;
876 	__set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
877 	timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
878 
879 	bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
880 	linkmode_copy(pl->link_config.advertising, pl->supported);
881 	phylink_validate(pl, pl->supported, &pl->link_config);
882 
883 	ret = phylink_parse_mode(pl, fwnode);
884 	if (ret < 0) {
885 		kfree(pl);
886 		return ERR_PTR(ret);
887 	}
888 
889 	if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
890 		ret = phylink_parse_fixedlink(pl, fwnode);
891 		if (ret < 0) {
892 			kfree(pl);
893 			return ERR_PTR(ret);
894 		}
895 	}
896 
897 	pl->cur_link_an_mode = pl->cfg_link_an_mode;
898 
899 	ret = phylink_register_sfp(pl, fwnode);
900 	if (ret < 0) {
901 		kfree(pl);
902 		return ERR_PTR(ret);
903 	}
904 
905 	return pl;
906 }
907 EXPORT_SYMBOL_GPL(phylink_create);
908 
909 /**
910  * phylink_set_pcs() - set the current PCS for phylink to use
911  * @pl: a pointer to a &struct phylink returned from phylink_create()
912  * @pcs: a pointer to the &struct phylink_pcs
913  *
914  * Bind the MAC PCS to phylink.  This may be called after phylink_create(),
915  * in mac_prepare() or mac_config() methods if it is desired to dynamically
916  * change the PCS.
917  *
918  * Please note that there are behavioural changes with the mac_config()
919  * callback if a PCS is present (denoting a newer setup) so removing a PCS
920  * is not supported, and if a PCS is going to be used, it must be registered
921  * by calling phylink_set_pcs() at the latest in the first mac_config() call.
922  */
phylink_set_pcs(struct phylink * pl,struct phylink_pcs * pcs)923 void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs)
924 {
925 	pl->pcs = pcs;
926 	pl->pcs_ops = pcs->ops;
927 }
928 EXPORT_SYMBOL_GPL(phylink_set_pcs);
929 
930 /**
931  * phylink_destroy() - cleanup and destroy the phylink instance
932  * @pl: a pointer to a &struct phylink returned from phylink_create()
933  *
934  * Destroy a phylink instance. Any PHY that has been attached must have been
935  * cleaned up via phylink_disconnect_phy() prior to calling this function.
936  *
937  * Note: the rtnl lock must not be held when calling this function.
938  */
phylink_destroy(struct phylink * pl)939 void phylink_destroy(struct phylink *pl)
940 {
941 	sfp_bus_del_upstream(pl->sfp_bus);
942 	if (pl->link_gpio)
943 		gpiod_put(pl->link_gpio);
944 
945 	cancel_work_sync(&pl->resolve);
946 	kfree(pl);
947 }
948 EXPORT_SYMBOL_GPL(phylink_destroy);
949 
phylink_phy_change(struct phy_device * phydev,bool up)950 static void phylink_phy_change(struct phy_device *phydev, bool up)
951 {
952 	struct phylink *pl = phydev->phylink;
953 	bool tx_pause, rx_pause;
954 
955 	phy_get_pause(phydev, &tx_pause, &rx_pause);
956 
957 	mutex_lock(&pl->state_mutex);
958 	pl->phy_state.speed = phydev->speed;
959 	pl->phy_state.duplex = phydev->duplex;
960 	pl->phy_state.pause = MLO_PAUSE_NONE;
961 	if (tx_pause)
962 		pl->phy_state.pause |= MLO_PAUSE_TX;
963 	if (rx_pause)
964 		pl->phy_state.pause |= MLO_PAUSE_RX;
965 	pl->phy_state.interface = phydev->interface;
966 	pl->phy_state.link = up;
967 	mutex_unlock(&pl->state_mutex);
968 
969 	phylink_run_resolve(pl);
970 
971 	phylink_dbg(pl, "phy link %s %s/%s/%s/%s\n", up ? "up" : "down",
972 		    phy_modes(phydev->interface),
973 		    phy_speed_to_str(phydev->speed),
974 		    phy_duplex_to_str(phydev->duplex),
975 		    phylink_pause_to_str(pl->phy_state.pause));
976 }
977 
phylink_bringup_phy(struct phylink * pl,struct phy_device * phy,phy_interface_t interface)978 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
979 			       phy_interface_t interface)
980 {
981 	struct phylink_link_state config;
982 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
983 	char *irq_str;
984 	int ret;
985 
986 	/*
987 	 * This is the new way of dealing with flow control for PHYs,
988 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
989 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
990 	 * using our validate call to the MAC, we rely upon the MAC
991 	 * clearing the bits from both supported and advertising fields.
992 	 */
993 	phy_support_asym_pause(phy);
994 
995 	memset(&config, 0, sizeof(config));
996 	linkmode_copy(supported, phy->supported);
997 	linkmode_copy(config.advertising, phy->advertising);
998 
999 	/* Clause 45 PHYs switch their Serdes lane between several different
1000 	 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
1001 	 * speeds. We really need to know which interface modes the PHY and
1002 	 * MAC supports to properly work out which linkmodes can be supported.
1003 	 */
1004 	if (phy->is_c45 &&
1005 	    interface != PHY_INTERFACE_MODE_RXAUI &&
1006 	    interface != PHY_INTERFACE_MODE_XAUI &&
1007 	    interface != PHY_INTERFACE_MODE_USXGMII)
1008 		config.interface = PHY_INTERFACE_MODE_NA;
1009 	else
1010 		config.interface = interface;
1011 
1012 	ret = phylink_validate(pl, supported, &config);
1013 	if (ret) {
1014 		phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
1015 			     phy_modes(config.interface),
1016 			     __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1017 			     __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1018 			     ret);
1019 		return ret;
1020 	}
1021 
1022 	phy->phylink = pl;
1023 	phy->phy_link_change = phylink_phy_change;
1024 
1025 	irq_str = phy_attached_info_irq(phy);
1026 	phylink_info(pl,
1027 		     "PHY [%s] driver [%s] (irq=%s)\n",
1028 		     dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1029 	kfree(irq_str);
1030 
1031 	mutex_lock(&phy->lock);
1032 	mutex_lock(&pl->state_mutex);
1033 	pl->phydev = phy;
1034 	pl->phy_state.interface = interface;
1035 	pl->phy_state.pause = MLO_PAUSE_NONE;
1036 	pl->phy_state.speed = SPEED_UNKNOWN;
1037 	pl->phy_state.duplex = DUPLEX_UNKNOWN;
1038 	linkmode_copy(pl->supported, supported);
1039 	linkmode_copy(pl->link_config.advertising, config.advertising);
1040 
1041 	/* Restrict the phy advertisement according to the MAC support. */
1042 	linkmode_copy(phy->advertising, config.advertising);
1043 	mutex_unlock(&pl->state_mutex);
1044 	mutex_unlock(&phy->lock);
1045 
1046 	phylink_dbg(pl,
1047 		    "phy: setting supported %*pb advertising %*pb\n",
1048 		    __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1049 		    __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1050 
1051 	if (phy_interrupt_is_valid(phy))
1052 		phy_request_interrupt(phy);
1053 
1054 	if (pl->config->mac_managed_pm)
1055 		phy->mac_managed_pm = true;
1056 
1057 	return 0;
1058 }
1059 
phylink_attach_phy(struct phylink * pl,struct phy_device * phy,phy_interface_t interface)1060 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1061 			      phy_interface_t interface)
1062 {
1063 	if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1064 		    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1065 		     phy_interface_mode_is_8023z(interface))))
1066 		return -EINVAL;
1067 
1068 	if (pl->phydev)
1069 		return -EBUSY;
1070 
1071 	return phy_attach_direct(pl->netdev, phy, 0, interface);
1072 }
1073 
1074 /**
1075  * phylink_connect_phy() - connect a PHY to the phylink instance
1076  * @pl: a pointer to a &struct phylink returned from phylink_create()
1077  * @phy: a pointer to a &struct phy_device.
1078  *
1079  * Connect @phy to the phylink instance specified by @pl by calling
1080  * phy_attach_direct(). Configure the @phy according to the MAC driver's
1081  * capabilities, start the PHYLIB state machine and enable any interrupts
1082  * that the PHY supports.
1083  *
1084  * This updates the phylink's ethtool supported and advertising link mode
1085  * masks.
1086  *
1087  * Returns 0 on success or a negative errno.
1088  */
phylink_connect_phy(struct phylink * pl,struct phy_device * phy)1089 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1090 {
1091 	int ret;
1092 
1093 	/* Use PHY device/driver interface */
1094 	if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1095 		pl->link_interface = phy->interface;
1096 		pl->link_config.interface = pl->link_interface;
1097 	}
1098 
1099 	ret = phylink_attach_phy(pl, phy, pl->link_interface);
1100 	if (ret < 0)
1101 		return ret;
1102 
1103 	ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1104 	if (ret)
1105 		phy_detach(phy);
1106 
1107 	return ret;
1108 }
1109 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1110 
1111 /**
1112  * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1113  * @pl: a pointer to a &struct phylink returned from phylink_create()
1114  * @dn: a pointer to a &struct device_node.
1115  * @flags: PHY-specific flags to communicate to the PHY device driver
1116  *
1117  * Connect the phy specified in the device node @dn to the phylink instance
1118  * specified by @pl. Actions specified in phylink_connect_phy() will be
1119  * performed.
1120  *
1121  * Returns 0 on success or a negative errno.
1122  */
phylink_of_phy_connect(struct phylink * pl,struct device_node * dn,u32 flags)1123 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1124 			   u32 flags)
1125 {
1126 	return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
1127 }
1128 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1129 
1130 /**
1131  * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
1132  * @pl: a pointer to a &struct phylink returned from phylink_create()
1133  * @fwnode: a pointer to a &struct fwnode_handle.
1134  * @flags: PHY-specific flags to communicate to the PHY device driver
1135  *
1136  * Connect the phy specified @fwnode to the phylink instance specified
1137  * by @pl.
1138  *
1139  * Returns 0 on success or a negative errno.
1140  */
phylink_fwnode_phy_connect(struct phylink * pl,struct fwnode_handle * fwnode,u32 flags)1141 int phylink_fwnode_phy_connect(struct phylink *pl,
1142 			       struct fwnode_handle *fwnode,
1143 			       u32 flags)
1144 {
1145 	struct fwnode_handle *phy_fwnode;
1146 	struct phy_device *phy_dev;
1147 	int ret;
1148 
1149 	/* Fixed links and 802.3z are handled without needing a PHY */
1150 	if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1151 	    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1152 	     phy_interface_mode_is_8023z(pl->link_interface)))
1153 		return 0;
1154 
1155 	phy_fwnode = fwnode_get_phy_node(fwnode);
1156 	if (IS_ERR(phy_fwnode)) {
1157 		if (pl->cfg_link_an_mode == MLO_AN_PHY)
1158 			return -ENODEV;
1159 		return 0;
1160 	}
1161 
1162 	phy_dev = fwnode_phy_find_device(phy_fwnode);
1163 	/* We're done with the phy_node handle */
1164 	fwnode_handle_put(phy_fwnode);
1165 	if (!phy_dev)
1166 		return -ENODEV;
1167 
1168 	ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1169 				pl->link_interface);
1170 	phy_device_free(phy_dev);
1171 	if (ret)
1172 		return ret;
1173 
1174 	ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1175 	if (ret)
1176 		phy_detach(phy_dev);
1177 
1178 	return ret;
1179 }
1180 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
1181 
1182 /**
1183  * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1184  *   instance.
1185  * @pl: a pointer to a &struct phylink returned from phylink_create()
1186  *
1187  * Disconnect any current PHY from the phylink instance described by @pl.
1188  */
phylink_disconnect_phy(struct phylink * pl)1189 void phylink_disconnect_phy(struct phylink *pl)
1190 {
1191 	struct phy_device *phy;
1192 
1193 	ASSERT_RTNL();
1194 
1195 	phy = pl->phydev;
1196 	if (phy) {
1197 		mutex_lock(&phy->lock);
1198 		mutex_lock(&pl->state_mutex);
1199 		pl->phydev = NULL;
1200 		mutex_unlock(&pl->state_mutex);
1201 		mutex_unlock(&phy->lock);
1202 		flush_work(&pl->resolve);
1203 
1204 		phy_disconnect(phy);
1205 	}
1206 }
1207 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1208 
1209 /**
1210  * phylink_mac_change() - notify phylink of a change in MAC state
1211  * @pl: a pointer to a &struct phylink returned from phylink_create()
1212  * @up: indicates whether the link is currently up.
1213  *
1214  * The MAC driver should call this driver when the state of its link
1215  * changes (eg, link failure, new negotiation results, etc.)
1216  */
phylink_mac_change(struct phylink * pl,bool up)1217 void phylink_mac_change(struct phylink *pl, bool up)
1218 {
1219 	if (!up)
1220 		pl->mac_link_dropped = true;
1221 	phylink_run_resolve(pl);
1222 	phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1223 }
1224 EXPORT_SYMBOL_GPL(phylink_mac_change);
1225 
phylink_link_handler(int irq,void * data)1226 static irqreturn_t phylink_link_handler(int irq, void *data)
1227 {
1228 	struct phylink *pl = data;
1229 
1230 	phylink_run_resolve(pl);
1231 
1232 	return IRQ_HANDLED;
1233 }
1234 
1235 /**
1236  * phylink_start() - start a phylink instance
1237  * @pl: a pointer to a &struct phylink returned from phylink_create()
1238  *
1239  * Start the phylink instance specified by @pl, configuring the MAC for the
1240  * desired link mode(s) and negotiation style. This should be called from the
1241  * network device driver's &struct net_device_ops ndo_open() method.
1242  */
phylink_start(struct phylink * pl)1243 void phylink_start(struct phylink *pl)
1244 {
1245 	bool poll = false;
1246 
1247 	ASSERT_RTNL();
1248 
1249 	phylink_info(pl, "configuring for %s/%s link mode\n",
1250 		     phylink_an_mode_str(pl->cur_link_an_mode),
1251 		     phy_modes(pl->link_config.interface));
1252 
1253 	/* Always set the carrier off */
1254 	if (pl->netdev)
1255 		netif_carrier_off(pl->netdev);
1256 
1257 	/* Apply the link configuration to the MAC when starting. This allows
1258 	 * a fixed-link to start with the correct parameters, and also
1259 	 * ensures that we set the appropriate advertisement for Serdes links.
1260 	 *
1261 	 * Restart autonegotiation if using 802.3z to ensure that the link
1262 	 * parameters are properly negotiated.  This is necessary for DSA
1263 	 * switches using 802.3z negotiation to ensure they see our modes.
1264 	 */
1265 	phylink_mac_initial_config(pl, true);
1266 
1267 	clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1268 	phylink_run_resolve(pl);
1269 
1270 	if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1271 		int irq = gpiod_to_irq(pl->link_gpio);
1272 
1273 		if (irq > 0) {
1274 			if (!request_irq(irq, phylink_link_handler,
1275 					 IRQF_TRIGGER_RISING |
1276 					 IRQF_TRIGGER_FALLING,
1277 					 "netdev link", pl))
1278 				pl->link_irq = irq;
1279 			else
1280 				irq = 0;
1281 		}
1282 		if (irq <= 0)
1283 			poll = true;
1284 	}
1285 
1286 	switch (pl->cfg_link_an_mode) {
1287 	case MLO_AN_FIXED:
1288 		poll |= pl->config->poll_fixed_state;
1289 		break;
1290 	case MLO_AN_INBAND:
1291 		poll |= pl->config->pcs_poll;
1292 		if (pl->pcs)
1293 			poll |= pl->pcs->poll;
1294 		break;
1295 	}
1296 	if (poll)
1297 		mod_timer(&pl->link_poll, jiffies + HZ);
1298 	if (pl->phydev)
1299 		phy_start(pl->phydev);
1300 	if (pl->sfp_bus)
1301 		sfp_upstream_start(pl->sfp_bus);
1302 }
1303 EXPORT_SYMBOL_GPL(phylink_start);
1304 
1305 /**
1306  * phylink_stop() - stop a phylink instance
1307  * @pl: a pointer to a &struct phylink returned from phylink_create()
1308  *
1309  * Stop the phylink instance specified by @pl. This should be called from the
1310  * network device driver's &struct net_device_ops ndo_stop() method.  The
1311  * network device's carrier state should not be changed prior to calling this
1312  * function.
1313  *
1314  * This will synchronously bring down the link if the link is not already
1315  * down (in other words, it will trigger a mac_link_down() method call.)
1316  */
phylink_stop(struct phylink * pl)1317 void phylink_stop(struct phylink *pl)
1318 {
1319 	ASSERT_RTNL();
1320 
1321 	if (pl->sfp_bus)
1322 		sfp_upstream_stop(pl->sfp_bus);
1323 	if (pl->phydev)
1324 		phy_stop(pl->phydev);
1325 	del_timer_sync(&pl->link_poll);
1326 	if (pl->link_irq) {
1327 		free_irq(pl->link_irq, pl);
1328 		pl->link_irq = 0;
1329 	}
1330 
1331 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1332 }
1333 EXPORT_SYMBOL_GPL(phylink_stop);
1334 
1335 /**
1336  * phylink_suspend() - handle a network device suspend event
1337  * @pl: a pointer to a &struct phylink returned from phylink_create()
1338  * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
1339  *
1340  * Handle a network device suspend event. There are several cases:
1341  * - If Wake-on-Lan is not active, we can bring down the link between
1342  *   the MAC and PHY by calling phylink_stop().
1343  * - If Wake-on-Lan is active, and being handled only by the PHY, we
1344  *   can also bring down the link between the MAC and PHY.
1345  * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
1346  *   still needs to receive packets, so we can not bring the link down.
1347  */
phylink_suspend(struct phylink * pl,bool mac_wol)1348 void phylink_suspend(struct phylink *pl, bool mac_wol)
1349 {
1350 	ASSERT_RTNL();
1351 
1352 	if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) {
1353 		/* Wake-on-Lan enabled, MAC handling */
1354 		mutex_lock(&pl->state_mutex);
1355 
1356 		/* Stop the resolver bringing the link up */
1357 		__set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1358 
1359 		/* Disable the carrier, to prevent transmit timeouts,
1360 		 * but one would hope all packets have been sent. This
1361 		 * also means phylink_resolve() will do nothing.
1362 		 */
1363 		if (pl->netdev)
1364 			netif_carrier_off(pl->netdev);
1365 		else
1366 			pl->old_link_state = false;
1367 
1368 		/* We do not call mac_link_down() here as we want the
1369 		 * link to remain up to receive the WoL packets.
1370 		 */
1371 		mutex_unlock(&pl->state_mutex);
1372 	} else {
1373 		phylink_stop(pl);
1374 	}
1375 }
1376 EXPORT_SYMBOL_GPL(phylink_suspend);
1377 
1378 /**
1379  * phylink_resume() - handle a network device resume event
1380  * @pl: a pointer to a &struct phylink returned from phylink_create()
1381  *
1382  * Undo the effects of phylink_suspend(), returning the link to an
1383  * operational state.
1384  */
phylink_resume(struct phylink * pl)1385 void phylink_resume(struct phylink *pl)
1386 {
1387 	ASSERT_RTNL();
1388 
1389 	if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
1390 		/* Wake-on-Lan enabled, MAC handling */
1391 
1392 		/* Call mac_link_down() so we keep the overall state balanced.
1393 		 * Do this under the state_mutex lock for consistency. This
1394 		 * will cause a "Link Down" message to be printed during
1395 		 * resume, which is harmless - the true link state will be
1396 		 * printed when we run a resolve.
1397 		 */
1398 		mutex_lock(&pl->state_mutex);
1399 		phylink_link_down(pl);
1400 		mutex_unlock(&pl->state_mutex);
1401 
1402 		/* Re-apply the link parameters so that all the settings get
1403 		 * restored to the MAC.
1404 		 */
1405 		phylink_mac_initial_config(pl, true);
1406 
1407 		/* Re-enable and re-resolve the link parameters */
1408 		clear_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1409 		phylink_run_resolve(pl);
1410 	} else {
1411 		phylink_start(pl);
1412 	}
1413 }
1414 EXPORT_SYMBOL_GPL(phylink_resume);
1415 
1416 /**
1417  * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1418  * @pl: a pointer to a &struct phylink returned from phylink_create()
1419  * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1420  *
1421  * Read the wake on lan parameters from the PHY attached to the phylink
1422  * instance specified by @pl. If no PHY is currently attached, report no
1423  * support for wake on lan.
1424  */
phylink_ethtool_get_wol(struct phylink * pl,struct ethtool_wolinfo * wol)1425 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1426 {
1427 	ASSERT_RTNL();
1428 
1429 	wol->supported = 0;
1430 	wol->wolopts = 0;
1431 
1432 	if (pl->phydev)
1433 		phy_ethtool_get_wol(pl->phydev, wol);
1434 }
1435 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1436 
1437 /**
1438  * phylink_ethtool_set_wol() - set wake on lan parameters
1439  * @pl: a pointer to a &struct phylink returned from phylink_create()
1440  * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1441  *
1442  * Set the wake on lan parameters for the PHY attached to the phylink
1443  * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1444  * error.
1445  *
1446  * Returns zero on success or negative errno code.
1447  */
phylink_ethtool_set_wol(struct phylink * pl,struct ethtool_wolinfo * wol)1448 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1449 {
1450 	int ret = -EOPNOTSUPP;
1451 
1452 	ASSERT_RTNL();
1453 
1454 	if (pl->phydev)
1455 		ret = phy_ethtool_set_wol(pl->phydev, wol);
1456 
1457 	return ret;
1458 }
1459 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1460 
phylink_merge_link_mode(unsigned long * dst,const unsigned long * b)1461 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1462 {
1463 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1464 
1465 	linkmode_zero(mask);
1466 	phylink_set_port_modes(mask);
1467 
1468 	linkmode_and(dst, dst, mask);
1469 	linkmode_or(dst, dst, b);
1470 }
1471 
phylink_get_ksettings(const struct phylink_link_state * state,struct ethtool_link_ksettings * kset)1472 static void phylink_get_ksettings(const struct phylink_link_state *state,
1473 				  struct ethtool_link_ksettings *kset)
1474 {
1475 	phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1476 	linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1477 	kset->base.speed = state->speed;
1478 	kset->base.duplex = state->duplex;
1479 	kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1480 				AUTONEG_DISABLE;
1481 }
1482 
1483 /**
1484  * phylink_ethtool_ksettings_get() - get the current link settings
1485  * @pl: a pointer to a &struct phylink returned from phylink_create()
1486  * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1487  *
1488  * Read the current link settings for the phylink instance specified by @pl.
1489  * This will be the link settings read from the MAC, PHY or fixed link
1490  * settings depending on the current negotiation mode.
1491  */
phylink_ethtool_ksettings_get(struct phylink * pl,struct ethtool_link_ksettings * kset)1492 int phylink_ethtool_ksettings_get(struct phylink *pl,
1493 				  struct ethtool_link_ksettings *kset)
1494 {
1495 	struct phylink_link_state link_state;
1496 
1497 	ASSERT_RTNL();
1498 
1499 	if (pl->phydev)
1500 		phy_ethtool_ksettings_get(pl->phydev, kset);
1501 	else
1502 		kset->base.port = pl->link_port;
1503 
1504 	linkmode_copy(kset->link_modes.supported, pl->supported);
1505 
1506 	switch (pl->cur_link_an_mode) {
1507 	case MLO_AN_FIXED:
1508 		/* We are using fixed settings. Report these as the
1509 		 * current link settings - and note that these also
1510 		 * represent the supported speeds/duplex/pause modes.
1511 		 */
1512 		phylink_get_fixed_state(pl, &link_state);
1513 		phylink_get_ksettings(&link_state, kset);
1514 		break;
1515 
1516 	case MLO_AN_INBAND:
1517 		/* If there is a phy attached, then use the reported
1518 		 * settings from the phy with no modification.
1519 		 */
1520 		if (pl->phydev)
1521 			break;
1522 
1523 		phylink_mac_pcs_get_state(pl, &link_state);
1524 
1525 		/* The MAC is reporting the link results from its own PCS
1526 		 * layer via in-band status. Report these as the current
1527 		 * link settings.
1528 		 */
1529 		phylink_get_ksettings(&link_state, kset);
1530 		break;
1531 	}
1532 
1533 	return 0;
1534 }
1535 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1536 
1537 /**
1538  * phylink_ethtool_ksettings_set() - set the link settings
1539  * @pl: a pointer to a &struct phylink returned from phylink_create()
1540  * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1541  */
phylink_ethtool_ksettings_set(struct phylink * pl,const struct ethtool_link_ksettings * kset)1542 int phylink_ethtool_ksettings_set(struct phylink *pl,
1543 				  const struct ethtool_link_ksettings *kset)
1544 {
1545 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1546 	struct phylink_link_state config;
1547 	const struct phy_setting *s;
1548 
1549 	ASSERT_RTNL();
1550 
1551 	if (pl->phydev) {
1552 		/* We can rely on phylib for this update; we also do not need
1553 		 * to update the pl->link_config settings:
1554 		 * - the configuration returned via ksettings_get() will come
1555 		 *   from phylib whenever a PHY is present.
1556 		 * - link_config.interface will be updated by the PHY calling
1557 		 *   back via phylink_phy_change() and a subsequent resolve.
1558 		 * - initial link configuration for PHY mode comes from the
1559 		 *   last phy state updated via phylink_phy_change().
1560 		 * - other configuration changes (e.g. pause modes) are
1561 		 *   performed directly via phylib.
1562 		 * - if in in-band mode with a PHY, the link configuration
1563 		 *   is passed on the link from the PHY, and all of
1564 		 *   link_config.{speed,duplex,an_enabled,pause} are not used.
1565 		 * - the only possible use would be link_config.advertising
1566 		 *   pause modes when in 1000base-X mode with a PHY, but in
1567 		 *   the presence of a PHY, this should not be changed as that
1568 		 *   should be determined from the media side advertisement.
1569 		 */
1570 		return phy_ethtool_ksettings_set(pl->phydev, kset);
1571 	}
1572 
1573 	config = pl->link_config;
1574 
1575 	/* Mask out unsupported advertisements */
1576 	linkmode_and(config.advertising, kset->link_modes.advertising,
1577 		     pl->supported);
1578 
1579 	/* FIXME: should we reject autoneg if phy/mac does not support it? */
1580 	switch (kset->base.autoneg) {
1581 	case AUTONEG_DISABLE:
1582 		/* Autonegotiation disabled, select a suitable speed and
1583 		 * duplex.
1584 		 */
1585 		s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1586 				       pl->supported, false);
1587 		if (!s)
1588 			return -EINVAL;
1589 
1590 		/* If we have a fixed link, refuse to change link parameters.
1591 		 * If the link parameters match, accept them but do nothing.
1592 		 */
1593 		if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1594 			if (s->speed != pl->link_config.speed ||
1595 			    s->duplex != pl->link_config.duplex)
1596 				return -EINVAL;
1597 			return 0;
1598 		}
1599 
1600 		config.speed = s->speed;
1601 		config.duplex = s->duplex;
1602 		break;
1603 
1604 	case AUTONEG_ENABLE:
1605 		/* If we have a fixed link, allow autonegotiation (since that
1606 		 * is our default case) but do not allow the advertisement to
1607 		 * be changed. If the advertisement matches, simply return.
1608 		 */
1609 		if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1610 			if (!linkmode_equal(config.advertising,
1611 					    pl->link_config.advertising))
1612 				return -EINVAL;
1613 			return 0;
1614 		}
1615 
1616 		config.speed = SPEED_UNKNOWN;
1617 		config.duplex = DUPLEX_UNKNOWN;
1618 		break;
1619 
1620 	default:
1621 		return -EINVAL;
1622 	}
1623 
1624 	/* We have ruled out the case with a PHY attached, and the
1625 	 * fixed-link cases.  All that is left are in-band links.
1626 	 */
1627 	config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
1628 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
1629 			 config.an_enabled);
1630 
1631 	/* Validate without changing the current supported mask. */
1632 	linkmode_copy(support, pl->supported);
1633 	if (phylink_validate(pl, support, &config))
1634 		return -EINVAL;
1635 
1636 	/* If autonegotiation is enabled, we must have an advertisement */
1637 	if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1638 		return -EINVAL;
1639 
1640 	/* If this link is with an SFP, ensure that changes to advertised modes
1641 	 * also cause the associated interface to be selected such that the
1642 	 * link can be configured correctly.
1643 	 */
1644 	if (pl->sfp_port && pl->sfp_bus) {
1645 		config.interface = sfp_select_interface(pl->sfp_bus,
1646 							config.advertising);
1647 		if (config.interface == PHY_INTERFACE_MODE_NA) {
1648 			phylink_err(pl,
1649 				    "selection of interface failed, advertisement %*pb\n",
1650 				    __ETHTOOL_LINK_MODE_MASK_NBITS,
1651 				    config.advertising);
1652 			return -EINVAL;
1653 		}
1654 
1655 		/* Revalidate with the selected interface */
1656 		linkmode_copy(support, pl->supported);
1657 		if (phylink_validate(pl, support, &config)) {
1658 			phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
1659 				    phylink_an_mode_str(pl->cur_link_an_mode),
1660 				    phy_modes(config.interface),
1661 				    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1662 			return -EINVAL;
1663 		}
1664 	}
1665 
1666 	mutex_lock(&pl->state_mutex);
1667 	pl->link_config.speed = config.speed;
1668 	pl->link_config.duplex = config.duplex;
1669 	pl->link_config.an_enabled = config.an_enabled;
1670 
1671 	if (pl->link_config.interface != config.interface) {
1672 		/* The interface changed, e.g. 1000base-X <-> 2500base-X */
1673 		/* We need to force the link down, then change the interface */
1674 		if (pl->old_link_state) {
1675 			phylink_link_down(pl);
1676 			pl->old_link_state = false;
1677 		}
1678 		if (!test_bit(PHYLINK_DISABLE_STOPPED,
1679 			      &pl->phylink_disable_state))
1680 			phylink_major_config(pl, false, &config);
1681 		pl->link_config.interface = config.interface;
1682 		linkmode_copy(pl->link_config.advertising, config.advertising);
1683 	} else if (!linkmode_equal(pl->link_config.advertising,
1684 				   config.advertising)) {
1685 		linkmode_copy(pl->link_config.advertising, config.advertising);
1686 		phylink_change_inband_advert(pl);
1687 	}
1688 	mutex_unlock(&pl->state_mutex);
1689 
1690 	return 0;
1691 }
1692 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1693 
1694 /**
1695  * phylink_ethtool_nway_reset() - restart negotiation
1696  * @pl: a pointer to a &struct phylink returned from phylink_create()
1697  *
1698  * Restart negotiation for the phylink instance specified by @pl. This will
1699  * cause any attached phy to restart negotiation with the link partner, and
1700  * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1701  * negotiation.
1702  *
1703  * Returns zero on success, or negative error code.
1704  */
phylink_ethtool_nway_reset(struct phylink * pl)1705 int phylink_ethtool_nway_reset(struct phylink *pl)
1706 {
1707 	int ret = 0;
1708 
1709 	ASSERT_RTNL();
1710 
1711 	if (pl->phydev)
1712 		ret = phy_restart_aneg(pl->phydev);
1713 	phylink_mac_pcs_an_restart(pl);
1714 
1715 	return ret;
1716 }
1717 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1718 
1719 /**
1720  * phylink_ethtool_get_pauseparam() - get the current pause parameters
1721  * @pl: a pointer to a &struct phylink returned from phylink_create()
1722  * @pause: a pointer to a &struct ethtool_pauseparam
1723  */
phylink_ethtool_get_pauseparam(struct phylink * pl,struct ethtool_pauseparam * pause)1724 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1725 				    struct ethtool_pauseparam *pause)
1726 {
1727 	ASSERT_RTNL();
1728 
1729 	pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1730 	pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1731 	pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1732 }
1733 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1734 
1735 /**
1736  * phylink_ethtool_set_pauseparam() - set the current pause parameters
1737  * @pl: a pointer to a &struct phylink returned from phylink_create()
1738  * @pause: a pointer to a &struct ethtool_pauseparam
1739  */
phylink_ethtool_set_pauseparam(struct phylink * pl,struct ethtool_pauseparam * pause)1740 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1741 				   struct ethtool_pauseparam *pause)
1742 {
1743 	struct phylink_link_state *config = &pl->link_config;
1744 	bool manual_changed;
1745 	int pause_state;
1746 
1747 	ASSERT_RTNL();
1748 
1749 	if (pl->cur_link_an_mode == MLO_AN_FIXED)
1750 		return -EOPNOTSUPP;
1751 
1752 	if (!phylink_test(pl->supported, Pause) &&
1753 	    !phylink_test(pl->supported, Asym_Pause))
1754 		return -EOPNOTSUPP;
1755 
1756 	if (!phylink_test(pl->supported, Asym_Pause) &&
1757 	    pause->rx_pause != pause->tx_pause)
1758 		return -EINVAL;
1759 
1760 	pause_state = 0;
1761 	if (pause->autoneg)
1762 		pause_state |= MLO_PAUSE_AN;
1763 	if (pause->rx_pause)
1764 		pause_state |= MLO_PAUSE_RX;
1765 	if (pause->tx_pause)
1766 		pause_state |= MLO_PAUSE_TX;
1767 
1768 	mutex_lock(&pl->state_mutex);
1769 	/*
1770 	 * See the comments for linkmode_set_pause(), wrt the deficiencies
1771 	 * with the current implementation.  A solution to this issue would
1772 	 * be:
1773 	 * ethtool  Local device
1774 	 *  rx  tx  Pause AsymDir
1775 	 *  0   0   0     0
1776 	 *  1   0   1     1
1777 	 *  0   1   0     1
1778 	 *  1   1   1     1
1779 	 * and then use the ethtool rx/tx enablement status to mask the
1780 	 * rx/tx pause resolution.
1781 	 */
1782 	linkmode_set_pause(config->advertising, pause->tx_pause,
1783 			   pause->rx_pause);
1784 
1785 	manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1786 			 (!(pause_state & MLO_PAUSE_AN) &&
1787 			   (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1788 
1789 	config->pause = pause_state;
1790 
1791 	/* Update our in-band advertisement, triggering a renegotiation if
1792 	 * the advertisement changed.
1793 	 */
1794 	if (!pl->phydev)
1795 		phylink_change_inband_advert(pl);
1796 
1797 	mutex_unlock(&pl->state_mutex);
1798 
1799 	/* If we have a PHY, a change of the pause frame advertisement will
1800 	 * cause phylib to renegotiate (if AN is enabled) which will in turn
1801 	 * call our phylink_phy_change() and trigger a resolve.  Note that
1802 	 * we can't hold our state mutex while calling phy_set_asym_pause().
1803 	 */
1804 	if (pl->phydev)
1805 		phy_set_asym_pause(pl->phydev, pause->rx_pause,
1806 				   pause->tx_pause);
1807 
1808 	/* If the manual pause settings changed, make sure we trigger a
1809 	 * resolve to update their state; we can not guarantee that the
1810 	 * link will cycle.
1811 	 */
1812 	if (manual_changed) {
1813 		pl->mac_link_dropped = true;
1814 		phylink_run_resolve(pl);
1815 	}
1816 
1817 	return 0;
1818 }
1819 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1820 
1821 /**
1822  * phylink_get_eee_err() - read the energy efficient ethernet error
1823  *   counter
1824  * @pl: a pointer to a &struct phylink returned from phylink_create().
1825  *
1826  * Read the Energy Efficient Ethernet error counter from the PHY associated
1827  * with the phylink instance specified by @pl.
1828  *
1829  * Returns positive error counter value, or negative error code.
1830  */
phylink_get_eee_err(struct phylink * pl)1831 int phylink_get_eee_err(struct phylink *pl)
1832 {
1833 	int ret = 0;
1834 
1835 	ASSERT_RTNL();
1836 
1837 	if (pl->phydev)
1838 		ret = phy_get_eee_err(pl->phydev);
1839 
1840 	return ret;
1841 }
1842 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1843 
1844 /**
1845  * phylink_init_eee() - init and check the EEE features
1846  * @pl: a pointer to a &struct phylink returned from phylink_create()
1847  * @clk_stop_enable: allow PHY to stop receive clock
1848  *
1849  * Must be called either with RTNL held or within mac_link_up()
1850  */
phylink_init_eee(struct phylink * pl,bool clk_stop_enable)1851 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1852 {
1853 	int ret = -EOPNOTSUPP;
1854 
1855 	if (pl->phydev)
1856 		ret = phy_init_eee(pl->phydev, clk_stop_enable);
1857 
1858 	return ret;
1859 }
1860 EXPORT_SYMBOL_GPL(phylink_init_eee);
1861 
1862 /**
1863  * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1864  * @pl: a pointer to a &struct phylink returned from phylink_create()
1865  * @eee: a pointer to a &struct ethtool_eee for the read parameters
1866  */
phylink_ethtool_get_eee(struct phylink * pl,struct ethtool_eee * eee)1867 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1868 {
1869 	int ret = -EOPNOTSUPP;
1870 
1871 	ASSERT_RTNL();
1872 
1873 	if (pl->phydev)
1874 		ret = phy_ethtool_get_eee(pl->phydev, eee);
1875 
1876 	return ret;
1877 }
1878 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1879 
1880 /**
1881  * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1882  * @pl: a pointer to a &struct phylink returned from phylink_create()
1883  * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1884  */
phylink_ethtool_set_eee(struct phylink * pl,struct ethtool_eee * eee)1885 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1886 {
1887 	int ret = -EOPNOTSUPP;
1888 
1889 	ASSERT_RTNL();
1890 
1891 	if (pl->phydev)
1892 		ret = phy_ethtool_set_eee(pl->phydev, eee);
1893 
1894 	return ret;
1895 }
1896 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1897 
1898 /* This emulates MII registers for a fixed-mode phy operating as per the
1899  * passed in state. "aneg" defines if we report negotiation is possible.
1900  *
1901  * FIXME: should deal with negotiation state too.
1902  */
phylink_mii_emul_read(unsigned int reg,struct phylink_link_state * state)1903 static int phylink_mii_emul_read(unsigned int reg,
1904 				 struct phylink_link_state *state)
1905 {
1906 	struct fixed_phy_status fs;
1907 	unsigned long *lpa = state->lp_advertising;
1908 	int val;
1909 
1910 	fs.link = state->link;
1911 	fs.speed = state->speed;
1912 	fs.duplex = state->duplex;
1913 	fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1914 	fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
1915 
1916 	val = swphy_read_reg(reg, &fs);
1917 	if (reg == MII_BMSR) {
1918 		if (!state->an_complete)
1919 			val &= ~BMSR_ANEGCOMPLETE;
1920 	}
1921 	return val;
1922 }
1923 
phylink_phy_read(struct phylink * pl,unsigned int phy_id,unsigned int reg)1924 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1925 			    unsigned int reg)
1926 {
1927 	struct phy_device *phydev = pl->phydev;
1928 	int prtad, devad;
1929 
1930 	if (mdio_phy_id_is_c45(phy_id)) {
1931 		prtad = mdio_phy_id_prtad(phy_id);
1932 		devad = mdio_phy_id_devad(phy_id);
1933 		devad = mdiobus_c45_addr(devad, reg);
1934 	} else if (phydev->is_c45) {
1935 		switch (reg) {
1936 		case MII_BMCR:
1937 		case MII_BMSR:
1938 		case MII_PHYSID1:
1939 		case MII_PHYSID2:
1940 			devad = __ffs(phydev->c45_ids.mmds_present);
1941 			break;
1942 		case MII_ADVERTISE:
1943 		case MII_LPA:
1944 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1945 				return -EINVAL;
1946 			devad = MDIO_MMD_AN;
1947 			if (reg == MII_ADVERTISE)
1948 				reg = MDIO_AN_ADVERTISE;
1949 			else
1950 				reg = MDIO_AN_LPA;
1951 			break;
1952 		default:
1953 			return -EINVAL;
1954 		}
1955 		prtad = phy_id;
1956 		devad = mdiobus_c45_addr(devad, reg);
1957 	} else {
1958 		prtad = phy_id;
1959 		devad = reg;
1960 	}
1961 	return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1962 }
1963 
phylink_phy_write(struct phylink * pl,unsigned int phy_id,unsigned int reg,unsigned int val)1964 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1965 			     unsigned int reg, unsigned int val)
1966 {
1967 	struct phy_device *phydev = pl->phydev;
1968 	int prtad, devad;
1969 
1970 	if (mdio_phy_id_is_c45(phy_id)) {
1971 		prtad = mdio_phy_id_prtad(phy_id);
1972 		devad = mdio_phy_id_devad(phy_id);
1973 		devad = mdiobus_c45_addr(devad, reg);
1974 	} else if (phydev->is_c45) {
1975 		switch (reg) {
1976 		case MII_BMCR:
1977 		case MII_BMSR:
1978 		case MII_PHYSID1:
1979 		case MII_PHYSID2:
1980 			devad = __ffs(phydev->c45_ids.mmds_present);
1981 			break;
1982 		case MII_ADVERTISE:
1983 		case MII_LPA:
1984 			if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1985 				return -EINVAL;
1986 			devad = MDIO_MMD_AN;
1987 			if (reg == MII_ADVERTISE)
1988 				reg = MDIO_AN_ADVERTISE;
1989 			else
1990 				reg = MDIO_AN_LPA;
1991 			break;
1992 		default:
1993 			return -EINVAL;
1994 		}
1995 		prtad = phy_id;
1996 		devad = mdiobus_c45_addr(devad, reg);
1997 	} else {
1998 		prtad = phy_id;
1999 		devad = reg;
2000 	}
2001 
2002 	return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
2003 }
2004 
phylink_mii_read(struct phylink * pl,unsigned int phy_id,unsigned int reg)2005 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
2006 			    unsigned int reg)
2007 {
2008 	struct phylink_link_state state;
2009 	int val = 0xffff;
2010 
2011 	switch (pl->cur_link_an_mode) {
2012 	case MLO_AN_FIXED:
2013 		if (phy_id == 0) {
2014 			phylink_get_fixed_state(pl, &state);
2015 			val = phylink_mii_emul_read(reg, &state);
2016 		}
2017 		break;
2018 
2019 	case MLO_AN_PHY:
2020 		return -EOPNOTSUPP;
2021 
2022 	case MLO_AN_INBAND:
2023 		if (phy_id == 0) {
2024 			phylink_mac_pcs_get_state(pl, &state);
2025 			val = phylink_mii_emul_read(reg, &state);
2026 		}
2027 		break;
2028 	}
2029 
2030 	return val & 0xffff;
2031 }
2032 
phylink_mii_write(struct phylink * pl,unsigned int phy_id,unsigned int reg,unsigned int val)2033 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
2034 			     unsigned int reg, unsigned int val)
2035 {
2036 	switch (pl->cur_link_an_mode) {
2037 	case MLO_AN_FIXED:
2038 		break;
2039 
2040 	case MLO_AN_PHY:
2041 		return -EOPNOTSUPP;
2042 
2043 	case MLO_AN_INBAND:
2044 		break;
2045 	}
2046 
2047 	return 0;
2048 }
2049 
2050 /**
2051  * phylink_mii_ioctl() - generic mii ioctl interface
2052  * @pl: a pointer to a &struct phylink returned from phylink_create()
2053  * @ifr: a pointer to a &struct ifreq for socket ioctls
2054  * @cmd: ioctl cmd to execute
2055  *
2056  * Perform the specified MII ioctl on the PHY attached to the phylink instance
2057  * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
2058  *
2059  * Returns: zero on success or negative error code.
2060  *
2061  * %SIOCGMIIPHY:
2062  *  read register from the current PHY.
2063  * %SIOCGMIIREG:
2064  *  read register from the specified PHY.
2065  * %SIOCSMIIREG:
2066  *  set a register on the specified PHY.
2067  */
phylink_mii_ioctl(struct phylink * pl,struct ifreq * ifr,int cmd)2068 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
2069 {
2070 	struct mii_ioctl_data *mii = if_mii(ifr);
2071 	int  ret;
2072 
2073 	ASSERT_RTNL();
2074 
2075 	if (pl->phydev) {
2076 		/* PHYs only exist for MLO_AN_PHY and SGMII */
2077 		switch (cmd) {
2078 		case SIOCGMIIPHY:
2079 			mii->phy_id = pl->phydev->mdio.addr;
2080 			fallthrough;
2081 
2082 		case SIOCGMIIREG:
2083 			ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
2084 			if (ret >= 0) {
2085 				mii->val_out = ret;
2086 				ret = 0;
2087 			}
2088 			break;
2089 
2090 		case SIOCSMIIREG:
2091 			ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
2092 						mii->val_in);
2093 			break;
2094 
2095 		default:
2096 			ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
2097 			break;
2098 		}
2099 	} else {
2100 		switch (cmd) {
2101 		case SIOCGMIIPHY:
2102 			mii->phy_id = 0;
2103 			fallthrough;
2104 
2105 		case SIOCGMIIREG:
2106 			ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
2107 			if (ret >= 0) {
2108 				mii->val_out = ret;
2109 				ret = 0;
2110 			}
2111 			break;
2112 
2113 		case SIOCSMIIREG:
2114 			ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
2115 						mii->val_in);
2116 			break;
2117 
2118 		default:
2119 			ret = -EOPNOTSUPP;
2120 			break;
2121 		}
2122 	}
2123 
2124 	return ret;
2125 }
2126 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2127 
2128 /**
2129  * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2130  *   link partners
2131  * @pl: a pointer to a &struct phylink returned from phylink_create()
2132  * @sync: perform action synchronously
2133  *
2134  * If we have a PHY that is not part of a SFP module, then set the speed
2135  * as described in the phy_speed_down() function. Please see this function
2136  * for a description of the @sync parameter.
2137  *
2138  * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2139  */
phylink_speed_down(struct phylink * pl,bool sync)2140 int phylink_speed_down(struct phylink *pl, bool sync)
2141 {
2142 	int ret = 0;
2143 
2144 	ASSERT_RTNL();
2145 
2146 	if (!pl->sfp_bus && pl->phydev)
2147 		ret = phy_speed_down(pl->phydev, sync);
2148 
2149 	return ret;
2150 }
2151 EXPORT_SYMBOL_GPL(phylink_speed_down);
2152 
2153 /**
2154  * phylink_speed_up() - restore the advertised speeds prior to the call to
2155  *   phylink_speed_down()
2156  * @pl: a pointer to a &struct phylink returned from phylink_create()
2157  *
2158  * If we have a PHY that is not part of a SFP module, then restore the
2159  * PHY speeds as per phy_speed_up().
2160  *
2161  * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2162  */
phylink_speed_up(struct phylink * pl)2163 int phylink_speed_up(struct phylink *pl)
2164 {
2165 	int ret = 0;
2166 
2167 	ASSERT_RTNL();
2168 
2169 	if (!pl->sfp_bus && pl->phydev)
2170 		ret = phy_speed_up(pl->phydev);
2171 
2172 	return ret;
2173 }
2174 EXPORT_SYMBOL_GPL(phylink_speed_up);
2175 
phylink_sfp_attach(void * upstream,struct sfp_bus * bus)2176 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2177 {
2178 	struct phylink *pl = upstream;
2179 
2180 	pl->netdev->sfp_bus = bus;
2181 }
2182 
phylink_sfp_detach(void * upstream,struct sfp_bus * bus)2183 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2184 {
2185 	struct phylink *pl = upstream;
2186 
2187 	pl->netdev->sfp_bus = NULL;
2188 }
2189 
phylink_sfp_config(struct phylink * pl,u8 mode,const unsigned long * supported,const unsigned long * advertising)2190 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2191 			      const unsigned long *supported,
2192 			      const unsigned long *advertising)
2193 {
2194 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2195 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2196 	struct phylink_link_state config;
2197 	phy_interface_t iface;
2198 	bool changed;
2199 	int ret;
2200 
2201 	linkmode_copy(support, supported);
2202 
2203 	memset(&config, 0, sizeof(config));
2204 	linkmode_copy(config.advertising, advertising);
2205 	config.interface = PHY_INTERFACE_MODE_NA;
2206 	config.speed = SPEED_UNKNOWN;
2207 	config.duplex = DUPLEX_UNKNOWN;
2208 	config.pause = MLO_PAUSE_AN;
2209 	config.an_enabled = pl->link_config.an_enabled;
2210 
2211 	/* Ignore errors if we're expecting a PHY to attach later */
2212 	ret = phylink_validate(pl, support, &config);
2213 	if (ret) {
2214 		phylink_err(pl, "validation with support %*pb failed: %d\n",
2215 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2216 		return ret;
2217 	}
2218 
2219 	iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2220 	if (iface == PHY_INTERFACE_MODE_NA) {
2221 		phylink_err(pl,
2222 			    "selection of interface failed, advertisement %*pb\n",
2223 			    __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2224 		return -EINVAL;
2225 	}
2226 
2227 	config.interface = iface;
2228 	linkmode_copy(support1, support);
2229 	ret = phylink_validate(pl, support1, &config);
2230 	if (ret) {
2231 		phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
2232 			    phylink_an_mode_str(mode),
2233 			    phy_modes(config.interface),
2234 			    __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2235 		return ret;
2236 	}
2237 
2238 	phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2239 		    phylink_an_mode_str(mode), phy_modes(config.interface),
2240 		    __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2241 
2242 	if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2243 		return -EINVAL;
2244 
2245 	changed = !linkmode_equal(pl->supported, support) ||
2246 		  !linkmode_equal(pl->link_config.advertising,
2247 				  config.advertising);
2248 	if (changed) {
2249 		linkmode_copy(pl->supported, support);
2250 		linkmode_copy(pl->link_config.advertising, config.advertising);
2251 	}
2252 
2253 	if (pl->cur_link_an_mode != mode ||
2254 	    pl->link_config.interface != config.interface) {
2255 		pl->link_config.interface = config.interface;
2256 		pl->cur_link_an_mode = mode;
2257 
2258 		changed = true;
2259 
2260 		phylink_info(pl, "switched to %s/%s link mode\n",
2261 			     phylink_an_mode_str(mode),
2262 			     phy_modes(config.interface));
2263 	}
2264 
2265 	pl->link_port = pl->sfp_port;
2266 
2267 	if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2268 				 &pl->phylink_disable_state))
2269 		phylink_mac_initial_config(pl, false);
2270 
2271 	return ret;
2272 }
2273 
phylink_sfp_module_insert(void * upstream,const struct sfp_eeprom_id * id)2274 static int phylink_sfp_module_insert(void *upstream,
2275 				     const struct sfp_eeprom_id *id)
2276 {
2277 	struct phylink *pl = upstream;
2278 	unsigned long *support = pl->sfp_support;
2279 
2280 	ASSERT_RTNL();
2281 
2282 	linkmode_zero(support);
2283 	sfp_parse_support(pl->sfp_bus, id, support);
2284 	pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2285 
2286 	/* If this module may have a PHY connecting later, defer until later */
2287 	pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2288 	if (pl->sfp_may_have_phy)
2289 		return 0;
2290 
2291 	return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2292 }
2293 
phylink_sfp_module_start(void * upstream)2294 static int phylink_sfp_module_start(void *upstream)
2295 {
2296 	struct phylink *pl = upstream;
2297 
2298 	/* If this SFP module has a PHY, start the PHY now. */
2299 	if (pl->phydev) {
2300 		phy_start(pl->phydev);
2301 		return 0;
2302 	}
2303 
2304 	/* If the module may have a PHY but we didn't detect one we
2305 	 * need to configure the MAC here.
2306 	 */
2307 	if (!pl->sfp_may_have_phy)
2308 		return 0;
2309 
2310 	return phylink_sfp_config(pl, MLO_AN_INBAND,
2311 				  pl->sfp_support, pl->sfp_support);
2312 }
2313 
phylink_sfp_module_stop(void * upstream)2314 static void phylink_sfp_module_stop(void *upstream)
2315 {
2316 	struct phylink *pl = upstream;
2317 
2318 	/* If this SFP module has a PHY, stop it. */
2319 	if (pl->phydev)
2320 		phy_stop(pl->phydev);
2321 }
2322 
phylink_sfp_link_down(void * upstream)2323 static void phylink_sfp_link_down(void *upstream)
2324 {
2325 	struct phylink *pl = upstream;
2326 
2327 	ASSERT_RTNL();
2328 
2329 	phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2330 }
2331 
phylink_sfp_link_up(void * upstream)2332 static void phylink_sfp_link_up(void *upstream)
2333 {
2334 	struct phylink *pl = upstream;
2335 
2336 	ASSERT_RTNL();
2337 
2338 	clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2339 	phylink_run_resolve(pl);
2340 }
2341 
2342 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2343  * or 802.3z control word, so inband will not work.
2344  */
phylink_phy_no_inband(struct phy_device * phy)2345 static bool phylink_phy_no_inband(struct phy_device *phy)
2346 {
2347 	return phy->is_c45 &&
2348 		(phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2349 }
2350 
phylink_sfp_connect_phy(void * upstream,struct phy_device * phy)2351 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2352 {
2353 	struct phylink *pl = upstream;
2354 	phy_interface_t interface;
2355 	u8 mode;
2356 	int ret;
2357 
2358 	/*
2359 	 * This is the new way of dealing with flow control for PHYs,
2360 	 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2361 	 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2362 	 * using our validate call to the MAC, we rely upon the MAC
2363 	 * clearing the bits from both supported and advertising fields.
2364 	 */
2365 	phy_support_asym_pause(phy);
2366 
2367 	if (phylink_phy_no_inband(phy))
2368 		mode = MLO_AN_PHY;
2369 	else
2370 		mode = MLO_AN_INBAND;
2371 
2372 	/* Do the initial configuration */
2373 	ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2374 	if (ret < 0)
2375 		return ret;
2376 
2377 	interface = pl->link_config.interface;
2378 	ret = phylink_attach_phy(pl, phy, interface);
2379 	if (ret < 0)
2380 		return ret;
2381 
2382 	ret = phylink_bringup_phy(pl, phy, interface);
2383 	if (ret)
2384 		phy_detach(phy);
2385 
2386 	return ret;
2387 }
2388 
phylink_sfp_disconnect_phy(void * upstream)2389 static void phylink_sfp_disconnect_phy(void *upstream)
2390 {
2391 	phylink_disconnect_phy(upstream);
2392 }
2393 
2394 static const struct sfp_upstream_ops sfp_phylink_ops = {
2395 	.attach = phylink_sfp_attach,
2396 	.detach = phylink_sfp_detach,
2397 	.module_insert = phylink_sfp_module_insert,
2398 	.module_start = phylink_sfp_module_start,
2399 	.module_stop = phylink_sfp_module_stop,
2400 	.link_up = phylink_sfp_link_up,
2401 	.link_down = phylink_sfp_link_down,
2402 	.connect_phy = phylink_sfp_connect_phy,
2403 	.disconnect_phy = phylink_sfp_disconnect_phy,
2404 };
2405 
2406 /* Helpers for MAC drivers */
2407 
2408 /**
2409  * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2410  * @state: a pointer to a &struct phylink_link_state
2411  *
2412  * Inspect the interface mode, advertising mask or forced speed and
2413  * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2414  * the interface mode to suit.  @state->interface is appropriately
2415  * updated, and the advertising mask has the "other" baseX_Full flag
2416  * cleared.
2417  */
phylink_helper_basex_speed(struct phylink_link_state * state)2418 void phylink_helper_basex_speed(struct phylink_link_state *state)
2419 {
2420 	if (phy_interface_mode_is_8023z(state->interface)) {
2421 		bool want_2500 = state->an_enabled ?
2422 			phylink_test(state->advertising, 2500baseX_Full) :
2423 			state->speed == SPEED_2500;
2424 
2425 		if (want_2500) {
2426 			phylink_clear(state->advertising, 1000baseX_Full);
2427 			state->interface = PHY_INTERFACE_MODE_2500BASEX;
2428 		} else {
2429 			phylink_clear(state->advertising, 2500baseX_Full);
2430 			state->interface = PHY_INTERFACE_MODE_1000BASEX;
2431 		}
2432 	}
2433 }
2434 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2435 
phylink_decode_c37_word(struct phylink_link_state * state,uint16_t config_reg,int speed)2436 static void phylink_decode_c37_word(struct phylink_link_state *state,
2437 				    uint16_t config_reg, int speed)
2438 {
2439 	bool tx_pause, rx_pause;
2440 	int fd_bit;
2441 
2442 	if (speed == SPEED_2500)
2443 		fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2444 	else
2445 		fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2446 
2447 	mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2448 
2449 	if (linkmode_test_bit(fd_bit, state->advertising) &&
2450 	    linkmode_test_bit(fd_bit, state->lp_advertising)) {
2451 		state->speed = speed;
2452 		state->duplex = DUPLEX_FULL;
2453 	} else {
2454 		/* negotiation failure */
2455 		state->link = false;
2456 	}
2457 
2458 	linkmode_resolve_pause(state->advertising, state->lp_advertising,
2459 			       &tx_pause, &rx_pause);
2460 
2461 	if (tx_pause)
2462 		state->pause |= MLO_PAUSE_TX;
2463 	if (rx_pause)
2464 		state->pause |= MLO_PAUSE_RX;
2465 }
2466 
phylink_decode_sgmii_word(struct phylink_link_state * state,uint16_t config_reg)2467 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2468 				      uint16_t config_reg)
2469 {
2470 	if (!(config_reg & LPA_SGMII_LINK)) {
2471 		state->link = false;
2472 		return;
2473 	}
2474 
2475 	switch (config_reg & LPA_SGMII_SPD_MASK) {
2476 	case LPA_SGMII_10:
2477 		state->speed = SPEED_10;
2478 		break;
2479 	case LPA_SGMII_100:
2480 		state->speed = SPEED_100;
2481 		break;
2482 	case LPA_SGMII_1000:
2483 		state->speed = SPEED_1000;
2484 		break;
2485 	default:
2486 		state->link = false;
2487 		return;
2488 	}
2489 	if (config_reg & LPA_SGMII_FULL_DUPLEX)
2490 		state->duplex = DUPLEX_FULL;
2491 	else
2492 		state->duplex = DUPLEX_HALF;
2493 }
2494 
2495 /**
2496  * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2497  * @state: a pointer to a struct phylink_link_state.
2498  * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2499  *
2500  * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2501  * code word.  Decode the USXGMII code word and populate the corresponding fields
2502  * (speed, duplex) into the phylink_link_state structure.
2503  */
phylink_decode_usxgmii_word(struct phylink_link_state * state,uint16_t lpa)2504 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2505 				 uint16_t lpa)
2506 {
2507 	switch (lpa & MDIO_USXGMII_SPD_MASK) {
2508 	case MDIO_USXGMII_10:
2509 		state->speed = SPEED_10;
2510 		break;
2511 	case MDIO_USXGMII_100:
2512 		state->speed = SPEED_100;
2513 		break;
2514 	case MDIO_USXGMII_1000:
2515 		state->speed = SPEED_1000;
2516 		break;
2517 	case MDIO_USXGMII_2500:
2518 		state->speed = SPEED_2500;
2519 		break;
2520 	case MDIO_USXGMII_5000:
2521 		state->speed = SPEED_5000;
2522 		break;
2523 	case MDIO_USXGMII_10G:
2524 		state->speed = SPEED_10000;
2525 		break;
2526 	default:
2527 		state->link = false;
2528 		return;
2529 	}
2530 
2531 	if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2532 		state->duplex = DUPLEX_FULL;
2533 	else
2534 		state->duplex = DUPLEX_HALF;
2535 }
2536 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2537 
2538 /**
2539  * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2540  * @pcs: a pointer to a &struct mdio_device.
2541  * @state: a pointer to a &struct phylink_link_state.
2542  *
2543  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2544  * clause 37 negotiation and/or SGMII control.
2545  *
2546  * Read the MAC PCS state from the MII device configured in @config and
2547  * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2548  * the phylink @state structure. This is suitable to be directly plugged
2549  * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2550  * structure.
2551  */
phylink_mii_c22_pcs_get_state(struct mdio_device * pcs,struct phylink_link_state * state)2552 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2553 				   struct phylink_link_state *state)
2554 {
2555 	struct mii_bus *bus = pcs->bus;
2556 	int addr = pcs->addr;
2557 	int bmsr, lpa;
2558 
2559 	bmsr = mdiobus_read(bus, addr, MII_BMSR);
2560 	lpa = mdiobus_read(bus, addr, MII_LPA);
2561 	if (bmsr < 0 || lpa < 0) {
2562 		state->link = false;
2563 		return;
2564 	}
2565 
2566 	state->link = !!(bmsr & BMSR_LSTATUS);
2567 	state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2568 	if (!state->link)
2569 		return;
2570 
2571 	switch (state->interface) {
2572 	case PHY_INTERFACE_MODE_1000BASEX:
2573 		phylink_decode_c37_word(state, lpa, SPEED_1000);
2574 		break;
2575 
2576 	case PHY_INTERFACE_MODE_2500BASEX:
2577 		phylink_decode_c37_word(state, lpa, SPEED_2500);
2578 		break;
2579 
2580 	case PHY_INTERFACE_MODE_SGMII:
2581 	case PHY_INTERFACE_MODE_QSGMII:
2582 		phylink_decode_sgmii_word(state, lpa);
2583 		break;
2584 
2585 	default:
2586 		state->link = false;
2587 		break;
2588 	}
2589 }
2590 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2591 
2592 /**
2593  * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2594  *	advertisement
2595  * @pcs: a pointer to a &struct mdio_device.
2596  * @interface: the PHY interface mode being configured
2597  * @advertising: the ethtool advertisement mask
2598  *
2599  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2600  * clause 37 negotiation and/or SGMII control.
2601  *
2602  * Configure the clause 37 PCS advertisement as specified by @state. This
2603  * does not trigger a renegotiation; phylink will do that via the
2604  * mac_an_restart() method of the struct phylink_mac_ops structure.
2605  *
2606  * Returns negative error code on failure to configure the advertisement,
2607  * zero if no change has been made, or one if the advertisement has changed.
2608  */
phylink_mii_c22_pcs_set_advertisement(struct mdio_device * pcs,phy_interface_t interface,const unsigned long * advertising)2609 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
2610 					  phy_interface_t interface,
2611 					  const unsigned long *advertising)
2612 {
2613 	struct mii_bus *bus = pcs->bus;
2614 	int addr = pcs->addr;
2615 	int val, ret;
2616 	u16 adv;
2617 
2618 	switch (interface) {
2619 	case PHY_INTERFACE_MODE_1000BASEX:
2620 	case PHY_INTERFACE_MODE_2500BASEX:
2621 		adv = ADVERTISE_1000XFULL;
2622 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2623 				      advertising))
2624 			adv |= ADVERTISE_1000XPAUSE;
2625 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2626 				      advertising))
2627 			adv |= ADVERTISE_1000XPSE_ASYM;
2628 
2629 		val = mdiobus_read(bus, addr, MII_ADVERTISE);
2630 		if (val < 0)
2631 			return val;
2632 
2633 		if (val == adv)
2634 			return 0;
2635 
2636 		ret = mdiobus_write(bus, addr, MII_ADVERTISE, adv);
2637 		if (ret < 0)
2638 			return ret;
2639 
2640 		return 1;
2641 
2642 	case PHY_INTERFACE_MODE_SGMII:
2643 		val = mdiobus_read(bus, addr, MII_ADVERTISE);
2644 		if (val < 0)
2645 			return val;
2646 
2647 		if (val == 0x0001)
2648 			return 0;
2649 
2650 		ret = mdiobus_write(bus, addr, MII_ADVERTISE, 0x0001);
2651 		if (ret < 0)
2652 			return ret;
2653 
2654 		return 1;
2655 
2656 	default:
2657 		/* Nothing to do for other modes */
2658 		return 0;
2659 	}
2660 }
2661 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2662 
2663 /**
2664  * phylink_mii_c22_pcs_config() - configure clause 22 PCS
2665  * @pcs: a pointer to a &struct mdio_device.
2666  * @mode: link autonegotiation mode
2667  * @interface: the PHY interface mode being configured
2668  * @advertising: the ethtool advertisement mask
2669  *
2670  * Configure a Clause 22 PCS PHY with the appropriate negotiation
2671  * parameters for the @mode, @interface and @advertising parameters.
2672  * Returns negative error number on failure, zero if the advertisement
2673  * has not changed, or positive if there is a change.
2674  */
phylink_mii_c22_pcs_config(struct mdio_device * pcs,unsigned int mode,phy_interface_t interface,const unsigned long * advertising)2675 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
2676 			       phy_interface_t interface,
2677 			       const unsigned long *advertising)
2678 {
2679 	bool changed;
2680 	u16 bmcr;
2681 	int ret;
2682 
2683 	ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface,
2684 						    advertising);
2685 	if (ret < 0)
2686 		return ret;
2687 
2688 	changed = ret > 0;
2689 
2690 	/* Ensure ISOLATE bit is disabled */
2691 	bmcr = mode == MLO_AN_INBAND ? BMCR_ANENABLE : 0;
2692 	ret = mdiobus_modify(pcs->bus, pcs->addr, MII_BMCR,
2693 			     BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
2694 	if (ret < 0)
2695 		return ret;
2696 
2697 	return changed ? 1 : 0;
2698 }
2699 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
2700 
2701 /**
2702  * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2703  * @pcs: a pointer to a &struct mdio_device.
2704  *
2705  * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2706  * clause 37 negotiation.
2707  *
2708  * Restart the clause 37 negotiation with the link partner. This is
2709  * suitable to be directly plugged into the mac_pcs_get_state() member
2710  * of the struct phylink_mac_ops structure.
2711  */
phylink_mii_c22_pcs_an_restart(struct mdio_device * pcs)2712 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2713 {
2714 	struct mii_bus *bus = pcs->bus;
2715 	int val, addr = pcs->addr;
2716 
2717 	val = mdiobus_read(bus, addr, MII_BMCR);
2718 	if (val >= 0) {
2719 		val |= BMCR_ANRESTART;
2720 
2721 		mdiobus_write(bus, addr, MII_BMCR, val);
2722 	}
2723 }
2724 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2725 
phylink_mii_c45_pcs_get_state(struct mdio_device * pcs,struct phylink_link_state * state)2726 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2727 				   struct phylink_link_state *state)
2728 {
2729 	struct mii_bus *bus = pcs->bus;
2730 	int addr = pcs->addr;
2731 	int stat;
2732 
2733 	stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
2734 	if (stat < 0) {
2735 		state->link = false;
2736 		return;
2737 	}
2738 
2739 	state->link = !!(stat & MDIO_STAT1_LSTATUS);
2740 	if (!state->link)
2741 		return;
2742 
2743 	switch (state->interface) {
2744 	case PHY_INTERFACE_MODE_10GBASER:
2745 		state->speed = SPEED_10000;
2746 		state->duplex = DUPLEX_FULL;
2747 		break;
2748 
2749 	default:
2750 		break;
2751 	}
2752 }
2753 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2754 
2755 MODULE_LICENSE("GPL v2");
2756