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