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