1 // SPDX-License-Identifier: GPL-2.0
3 * phylink models the MAC to optional PHY connection, supporting
4 * technologies such as SFP cages where the PHY is hot-pluggable.
6 * Copyright (C) 2015 Russell King
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>
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>
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)
34 PHYLINK_DISABLE_STOPPED,
36 PHYLINK_DISABLE_MAC_WOL,
40 * struct phylink - internal data type for phylink
44 struct net_device *netdev;
45 const struct phylink_mac_ops *mac_ops;
46 struct phylink_config *config;
47 struct phylink_pcs *pcs;
49 unsigned int old_link_state:1;
51 unsigned long phylink_disable_state; /* bitmask of disables */
52 struct phy_device *phydev;
53 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
54 u8 cfg_link_an_mode; /* MLO_AN_xxx */
56 u8 link_port; /* The current non-phy ethtool port */
57 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
59 /* The link configuration settings */
60 struct phylink_link_state link_config;
62 /* The current settings */
63 phy_interface_t cur_interface;
65 struct gpio_desc *link_gpio;
66 unsigned int link_irq;
67 struct timer_list link_poll;
68 void (*get_fixed_state)(struct net_device *dev,
69 struct phylink_link_state *s);
71 struct mutex state_mutex;
72 struct phylink_link_state phy_state;
73 struct work_struct resolve;
75 bool mac_link_dropped;
76 bool using_mac_select_pcs;
78 struct sfp_bus *sfp_bus;
79 bool sfp_may_have_phy;
80 DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
81 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
85 #define phylink_printk(level, pl, fmt, ...) \
87 if ((pl)->config->type == PHYLINK_NETDEV) \
88 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
89 else if ((pl)->config->type == PHYLINK_DEV) \
90 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
93 #define phylink_err(pl, fmt, ...) \
94 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
95 #define phylink_warn(pl, fmt, ...) \
96 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
97 #define phylink_info(pl, fmt, ...) \
98 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
99 #if defined(CONFIG_DYNAMIC_DEBUG)
100 #define phylink_dbg(pl, fmt, ...) \
102 if ((pl)->config->type == PHYLINK_NETDEV) \
103 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
104 else if ((pl)->config->type == PHYLINK_DEV) \
105 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
108 #define phylink_dbg(pl, fmt, ...) \
109 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
111 #define phylink_dbg(pl, fmt, ...) \
114 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
119 * phylink_set_port_modes() - set the port type modes in the ethtool mask
120 * @mask: ethtool link mode mask
122 * Sets all the port type modes in the ethtool mask. MAC drivers should
123 * use this in their 'validate' callback.
125 void phylink_set_port_modes(unsigned long *mask)
127 phylink_set(mask, TP);
128 phylink_set(mask, AUI);
129 phylink_set(mask, MII);
130 phylink_set(mask, FIBRE);
131 phylink_set(mask, BNC);
132 phylink_set(mask, Backplane);
134 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
136 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
138 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
140 phylink_set_port_modes(tmp);
141 phylink_set(tmp, Autoneg);
142 phylink_set(tmp, Pause);
143 phylink_set(tmp, Asym_Pause);
145 return linkmode_subset(linkmode, tmp);
148 static const char *phylink_an_mode_str(unsigned int mode)
150 static const char *modestr[] = {
151 [MLO_AN_PHY] = "phy",
152 [MLO_AN_FIXED] = "fixed",
153 [MLO_AN_INBAND] = "inband",
156 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
160 * phylink_interface_max_speed() - get the maximum speed of a phy interface
161 * @interface: phy interface mode defined by &typedef phy_interface_t
163 * Determine the maximum speed of a phy interface. This is intended to help
164 * determine the correct speed to pass to the MAC when the phy is performing
167 * Return: The maximum speed of @interface
169 static int phylink_interface_max_speed(phy_interface_t interface)
172 case PHY_INTERFACE_MODE_100BASEX:
173 case PHY_INTERFACE_MODE_REVRMII:
174 case PHY_INTERFACE_MODE_RMII:
175 case PHY_INTERFACE_MODE_SMII:
176 case PHY_INTERFACE_MODE_REVMII:
177 case PHY_INTERFACE_MODE_MII:
180 case PHY_INTERFACE_MODE_TBI:
181 case PHY_INTERFACE_MODE_MOCA:
182 case PHY_INTERFACE_MODE_RTBI:
183 case PHY_INTERFACE_MODE_1000BASEX:
184 case PHY_INTERFACE_MODE_1000BASEKX:
185 case PHY_INTERFACE_MODE_TRGMII:
186 case PHY_INTERFACE_MODE_RGMII_TXID:
187 case PHY_INTERFACE_MODE_RGMII_RXID:
188 case PHY_INTERFACE_MODE_RGMII_ID:
189 case PHY_INTERFACE_MODE_RGMII:
190 case PHY_INTERFACE_MODE_QSGMII:
191 case PHY_INTERFACE_MODE_SGMII:
192 case PHY_INTERFACE_MODE_GMII:
195 case PHY_INTERFACE_MODE_2500BASEX:
198 case PHY_INTERFACE_MODE_5GBASER:
201 case PHY_INTERFACE_MODE_XGMII:
202 case PHY_INTERFACE_MODE_RXAUI:
203 case PHY_INTERFACE_MODE_XAUI:
204 case PHY_INTERFACE_MODE_10GBASER:
205 case PHY_INTERFACE_MODE_10GKR:
206 case PHY_INTERFACE_MODE_USXGMII:
207 case PHY_INTERFACE_MODE_QUSGMII:
210 case PHY_INTERFACE_MODE_25GBASER:
213 case PHY_INTERFACE_MODE_XLGMII:
216 case PHY_INTERFACE_MODE_INTERNAL:
217 case PHY_INTERFACE_MODE_NA:
218 case PHY_INTERFACE_MODE_MAX:
219 /* No idea! Garbage in, unknown out */
220 return SPEED_UNKNOWN;
223 /* If we get here, someone forgot to add an interface mode above */
225 return SPEED_UNKNOWN;
229 * phylink_caps_to_linkmodes() - Convert capabilities to ethtool link modes
230 * @linkmodes: ethtool linkmode mask (must be already initialised)
231 * @caps: bitmask of MAC capabilities
233 * Set all possible pause, speed and duplex linkmodes in @linkmodes that are
234 * supported by the @caps. @linkmodes must have been initialised previously.
236 void phylink_caps_to_linkmodes(unsigned long *linkmodes, unsigned long caps)
238 if (caps & MAC_SYM_PAUSE)
239 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, linkmodes);
241 if (caps & MAC_ASYM_PAUSE)
242 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, linkmodes);
245 __set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, linkmodes);
247 if (caps & MAC_10FD) {
248 __set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, linkmodes);
249 __set_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, linkmodes);
252 if (caps & MAC_100HD) {
253 __set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, linkmodes);
254 __set_bit(ETHTOOL_LINK_MODE_100baseFX_Half_BIT, linkmodes);
257 if (caps & MAC_100FD) {
258 __set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, linkmodes);
259 __set_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT, linkmodes);
260 __set_bit(ETHTOOL_LINK_MODE_100baseFX_Full_BIT, linkmodes);
263 if (caps & MAC_1000HD)
264 __set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, linkmodes);
266 if (caps & MAC_1000FD) {
267 __set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, linkmodes);
268 __set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, linkmodes);
269 __set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, linkmodes);
270 __set_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT, linkmodes);
273 if (caps & MAC_2500FD) {
274 __set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, linkmodes);
275 __set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, linkmodes);
278 if (caps & MAC_5000FD)
279 __set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, linkmodes);
281 if (caps & MAC_10000FD) {
282 __set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, linkmodes);
283 __set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, linkmodes);
284 __set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, linkmodes);
285 __set_bit(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT, linkmodes);
286 __set_bit(ETHTOOL_LINK_MODE_10000baseCR_Full_BIT, linkmodes);
287 __set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, linkmodes);
288 __set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, linkmodes);
289 __set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT, linkmodes);
290 __set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, linkmodes);
293 if (caps & MAC_25000FD) {
294 __set_bit(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, linkmodes);
295 __set_bit(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, linkmodes);
296 __set_bit(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, linkmodes);
299 if (caps & MAC_40000FD) {
300 __set_bit(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, linkmodes);
301 __set_bit(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, linkmodes);
302 __set_bit(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, linkmodes);
303 __set_bit(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, linkmodes);
306 if (caps & MAC_50000FD) {
307 __set_bit(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT, linkmodes);
308 __set_bit(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT, linkmodes);
309 __set_bit(ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT, linkmodes);
310 __set_bit(ETHTOOL_LINK_MODE_50000baseKR_Full_BIT, linkmodes);
311 __set_bit(ETHTOOL_LINK_MODE_50000baseSR_Full_BIT, linkmodes);
312 __set_bit(ETHTOOL_LINK_MODE_50000baseCR_Full_BIT, linkmodes);
313 __set_bit(ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
315 __set_bit(ETHTOOL_LINK_MODE_50000baseDR_Full_BIT, linkmodes);
318 if (caps & MAC_56000FD) {
319 __set_bit(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT, linkmodes);
320 __set_bit(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT, linkmodes);
321 __set_bit(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT, linkmodes);
322 __set_bit(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT, linkmodes);
325 if (caps & MAC_100000FD) {
326 __set_bit(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, linkmodes);
327 __set_bit(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT, linkmodes);
328 __set_bit(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, linkmodes);
329 __set_bit(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
331 __set_bit(ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT, linkmodes);
332 __set_bit(ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT, linkmodes);
333 __set_bit(ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT, linkmodes);
334 __set_bit(ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT,
336 __set_bit(ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT, linkmodes);
337 __set_bit(ETHTOOL_LINK_MODE_100000baseKR_Full_BIT, linkmodes);
338 __set_bit(ETHTOOL_LINK_MODE_100000baseSR_Full_BIT, linkmodes);
339 __set_bit(ETHTOOL_LINK_MODE_100000baseLR_ER_FR_Full_BIT,
341 __set_bit(ETHTOOL_LINK_MODE_100000baseCR_Full_BIT, linkmodes);
342 __set_bit(ETHTOOL_LINK_MODE_100000baseDR_Full_BIT, linkmodes);
345 if (caps & MAC_200000FD) {
346 __set_bit(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT, linkmodes);
347 __set_bit(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT, linkmodes);
348 __set_bit(ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
350 __set_bit(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT, linkmodes);
351 __set_bit(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT, linkmodes);
352 __set_bit(ETHTOOL_LINK_MODE_200000baseKR2_Full_BIT, linkmodes);
353 __set_bit(ETHTOOL_LINK_MODE_200000baseSR2_Full_BIT, linkmodes);
354 __set_bit(ETHTOOL_LINK_MODE_200000baseLR2_ER2_FR2_Full_BIT,
356 __set_bit(ETHTOOL_LINK_MODE_200000baseDR2_Full_BIT, linkmodes);
357 __set_bit(ETHTOOL_LINK_MODE_200000baseCR2_Full_BIT, linkmodes);
360 if (caps & MAC_400000FD) {
361 __set_bit(ETHTOOL_LINK_MODE_400000baseKR8_Full_BIT, linkmodes);
362 __set_bit(ETHTOOL_LINK_MODE_400000baseSR8_Full_BIT, linkmodes);
363 __set_bit(ETHTOOL_LINK_MODE_400000baseLR8_ER8_FR8_Full_BIT,
365 __set_bit(ETHTOOL_LINK_MODE_400000baseDR8_Full_BIT, linkmodes);
366 __set_bit(ETHTOOL_LINK_MODE_400000baseCR8_Full_BIT, linkmodes);
367 __set_bit(ETHTOOL_LINK_MODE_400000baseKR4_Full_BIT, linkmodes);
368 __set_bit(ETHTOOL_LINK_MODE_400000baseSR4_Full_BIT, linkmodes);
369 __set_bit(ETHTOOL_LINK_MODE_400000baseLR4_ER4_FR4_Full_BIT,
371 __set_bit(ETHTOOL_LINK_MODE_400000baseDR4_Full_BIT, linkmodes);
372 __set_bit(ETHTOOL_LINK_MODE_400000baseCR4_Full_BIT, linkmodes);
375 EXPORT_SYMBOL_GPL(phylink_caps_to_linkmodes);
381 } phylink_caps_params[] = {
382 { MAC_400000FD, SPEED_400000, DUPLEX_FULL },
383 { MAC_200000FD, SPEED_200000, DUPLEX_FULL },
384 { MAC_100000FD, SPEED_100000, DUPLEX_FULL },
385 { MAC_56000FD, SPEED_56000, DUPLEX_FULL },
386 { MAC_50000FD, SPEED_50000, DUPLEX_FULL },
387 { MAC_40000FD, SPEED_40000, DUPLEX_FULL },
388 { MAC_25000FD, SPEED_25000, DUPLEX_FULL },
389 { MAC_20000FD, SPEED_20000, DUPLEX_FULL },
390 { MAC_10000FD, SPEED_10000, DUPLEX_FULL },
391 { MAC_5000FD, SPEED_5000, DUPLEX_FULL },
392 { MAC_2500FD, SPEED_2500, DUPLEX_FULL },
393 { MAC_1000FD, SPEED_1000, DUPLEX_FULL },
394 { MAC_1000HD, SPEED_1000, DUPLEX_HALF },
395 { MAC_100FD, SPEED_100, DUPLEX_FULL },
396 { MAC_100HD, SPEED_100, DUPLEX_HALF },
397 { MAC_10FD, SPEED_10, DUPLEX_FULL },
398 { MAC_10HD, SPEED_10, DUPLEX_HALF },
402 * phylink_cap_from_speed_duplex - Get mac capability from speed/duplex
403 * @speed: the speed to search for
404 * @duplex: the duplex to search for
406 * Find the mac capability for a given speed and duplex.
408 * Return: A mask with the mac capability patching @speed and @duplex, or 0 if
409 * there were no matches.
411 static unsigned long phylink_cap_from_speed_duplex(int speed,
416 for (i = 0; i < ARRAY_SIZE(phylink_caps_params); i++) {
417 if (speed == phylink_caps_params[i].speed &&
418 duplex == phylink_caps_params[i].duplex)
419 return phylink_caps_params[i].mask;
426 * phylink_get_capabilities() - get capabilities for a given MAC
427 * @interface: phy interface mode defined by &typedef phy_interface_t
428 * @mac_capabilities: bitmask of MAC capabilities
429 * @rate_matching: type of rate matching being performed
431 * Get the MAC capabilities that are supported by the @interface mode and
434 unsigned long phylink_get_capabilities(phy_interface_t interface,
435 unsigned long mac_capabilities,
438 int max_speed = phylink_interface_max_speed(interface);
439 unsigned long caps = MAC_SYM_PAUSE | MAC_ASYM_PAUSE;
440 unsigned long matched_caps = 0;
443 case PHY_INTERFACE_MODE_USXGMII:
444 caps |= MAC_10000FD | MAC_5000FD | MAC_2500FD;
447 case PHY_INTERFACE_MODE_RGMII_TXID:
448 case PHY_INTERFACE_MODE_RGMII_RXID:
449 case PHY_INTERFACE_MODE_RGMII_ID:
450 case PHY_INTERFACE_MODE_RGMII:
451 case PHY_INTERFACE_MODE_QSGMII:
452 case PHY_INTERFACE_MODE_QUSGMII:
453 case PHY_INTERFACE_MODE_SGMII:
454 case PHY_INTERFACE_MODE_GMII:
455 caps |= MAC_1000HD | MAC_1000FD;
458 case PHY_INTERFACE_MODE_REVRMII:
459 case PHY_INTERFACE_MODE_RMII:
460 case PHY_INTERFACE_MODE_SMII:
461 case PHY_INTERFACE_MODE_REVMII:
462 case PHY_INTERFACE_MODE_MII:
463 caps |= MAC_10HD | MAC_10FD;
466 case PHY_INTERFACE_MODE_100BASEX:
467 caps |= MAC_100HD | MAC_100FD;
470 case PHY_INTERFACE_MODE_TBI:
471 case PHY_INTERFACE_MODE_MOCA:
472 case PHY_INTERFACE_MODE_RTBI:
473 case PHY_INTERFACE_MODE_1000BASEX:
476 case PHY_INTERFACE_MODE_1000BASEKX:
477 case PHY_INTERFACE_MODE_TRGMII:
481 case PHY_INTERFACE_MODE_2500BASEX:
485 case PHY_INTERFACE_MODE_5GBASER:
489 case PHY_INTERFACE_MODE_XGMII:
490 case PHY_INTERFACE_MODE_RXAUI:
491 case PHY_INTERFACE_MODE_XAUI:
492 case PHY_INTERFACE_MODE_10GBASER:
493 case PHY_INTERFACE_MODE_10GKR:
497 case PHY_INTERFACE_MODE_25GBASER:
501 case PHY_INTERFACE_MODE_XLGMII:
505 case PHY_INTERFACE_MODE_INTERNAL:
509 case PHY_INTERFACE_MODE_NA:
510 case PHY_INTERFACE_MODE_MAX:
514 switch (rate_matching) {
515 case RATE_MATCH_OPEN_LOOP:
518 case RATE_MATCH_NONE:
521 case RATE_MATCH_PAUSE: {
522 /* The MAC must support asymmetric pause towards the local
523 * device for this. We could allow just symmetric pause, but
524 * then we might have to renegotiate if the link partner
525 * doesn't support pause. This is because there's no way to
526 * accept pause frames without transmitting them if we only
527 * support symmetric pause.
529 if (!(mac_capabilities & MAC_SYM_PAUSE) ||
530 !(mac_capabilities & MAC_ASYM_PAUSE))
533 /* We can't adapt if the MAC doesn't support the interface's
534 * max speed at full duplex.
536 if (mac_capabilities &
537 phylink_cap_from_speed_duplex(max_speed, DUPLEX_FULL)) {
538 /* Although a duplex-matching phy might exist, we
539 * conservatively remove these modes because the MAC
540 * will not be aware of the half-duplex nature of the
543 matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
544 matched_caps &= ~(MAC_1000HD | MAC_100HD | MAC_10HD);
549 /* The MAC must support half duplex at the interface's max
552 if (mac_capabilities &
553 phylink_cap_from_speed_duplex(max_speed, DUPLEX_HALF)) {
554 matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
555 matched_caps &= mac_capabilities;
560 return (caps & mac_capabilities) | matched_caps;
562 EXPORT_SYMBOL_GPL(phylink_get_capabilities);
565 * phylink_validate_mask_caps() - Restrict link modes based on caps
566 * @supported: ethtool bitmask for supported link modes.
567 * @state: pointer to a &struct phylink_link_state.
568 * @mac_capabilities: bitmask of MAC capabilities
570 * Calculate the supported link modes based on @mac_capabilities, and restrict
571 * @supported and @state based on that. Use this function if your capabiliies
572 * aren't constant, such as if they vary depending on the interface.
574 void phylink_validate_mask_caps(unsigned long *supported,
575 struct phylink_link_state *state,
576 unsigned long mac_capabilities)
578 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
581 phylink_set_port_modes(mask);
582 phylink_set(mask, Autoneg);
583 caps = phylink_get_capabilities(state->interface, mac_capabilities,
584 state->rate_matching);
585 phylink_caps_to_linkmodes(mask, caps);
587 linkmode_and(supported, supported, mask);
588 linkmode_and(state->advertising, state->advertising, mask);
590 EXPORT_SYMBOL_GPL(phylink_validate_mask_caps);
593 * phylink_generic_validate() - generic validate() callback implementation
594 * @config: a pointer to a &struct phylink_config.
595 * @supported: ethtool bitmask for supported link modes.
596 * @state: a pointer to a &struct phylink_link_state.
598 * Generic implementation of the validate() callback that MAC drivers can
599 * use when they pass the range of supported interfaces and MAC capabilities.
601 void phylink_generic_validate(struct phylink_config *config,
602 unsigned long *supported,
603 struct phylink_link_state *state)
605 phylink_validate_mask_caps(supported, state, config->mac_capabilities);
607 EXPORT_SYMBOL_GPL(phylink_generic_validate);
609 static int phylink_validate_mac_and_pcs(struct phylink *pl,
610 unsigned long *supported,
611 struct phylink_link_state *state)
613 struct phylink_pcs *pcs;
616 /* Get the PCS for this interface mode */
617 if (pl->using_mac_select_pcs) {
618 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
626 /* The PCS, if present, must be setup before phylink_create()
627 * has been called. If the ops is not initialised, print an
628 * error and backtrace rather than oopsing the kernel.
631 phylink_err(pl, "interface %s: uninitialised PCS\n",
632 phy_modes(state->interface));
637 /* Validate the link parameters with the PCS */
638 if (pcs->ops->pcs_validate) {
639 ret = pcs->ops->pcs_validate(pcs, supported, state);
640 if (ret < 0 || phylink_is_empty_linkmode(supported))
643 /* Ensure the advertising mask is a subset of the
646 linkmode_and(state->advertising, state->advertising,
651 /* Then validate the link parameters with the MAC */
652 if (pl->mac_ops->validate)
653 pl->mac_ops->validate(pl->config, supported, state);
655 phylink_generic_validate(pl->config, supported, state);
657 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
660 static int phylink_validate_mask(struct phylink *pl, unsigned long *supported,
661 struct phylink_link_state *state,
662 const unsigned long *interfaces)
664 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
665 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
666 __ETHTOOL_DECLARE_LINK_MODE_MASK(s);
667 struct phylink_link_state t;
670 for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) {
671 if (test_bit(intf, interfaces)) {
672 linkmode_copy(s, supported);
676 if (!phylink_validate_mac_and_pcs(pl, s, &t)) {
677 linkmode_or(all_s, all_s, s);
678 linkmode_or(all_adv, all_adv, t.advertising);
683 linkmode_copy(supported, all_s);
684 linkmode_copy(state->advertising, all_adv);
686 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
689 static int phylink_validate(struct phylink *pl, unsigned long *supported,
690 struct phylink_link_state *state)
692 const unsigned long *interfaces = pl->config->supported_interfaces;
694 if (!phy_interface_empty(interfaces)) {
695 if (state->interface == PHY_INTERFACE_MODE_NA)
696 return phylink_validate_mask(pl, supported, state,
699 if (!test_bit(state->interface, interfaces))
703 return phylink_validate_mac_and_pcs(pl, supported, state);
706 static int phylink_parse_fixedlink(struct phylink *pl,
707 struct fwnode_handle *fwnode)
709 struct fwnode_handle *fixed_node;
710 const struct phy_setting *s;
711 struct gpio_desc *desc;
715 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
717 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
719 pl->link_config.speed = speed;
720 pl->link_config.duplex = DUPLEX_HALF;
722 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
723 pl->link_config.duplex = DUPLEX_FULL;
725 /* We treat the "pause" and "asym-pause" terminology as
726 * defining the link partner's ability.
728 if (fwnode_property_read_bool(fixed_node, "pause"))
729 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
730 pl->link_config.lp_advertising);
731 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
732 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
733 pl->link_config.lp_advertising);
736 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
740 pl->link_gpio = desc;
741 else if (desc == ERR_PTR(-EPROBE_DEFER))
744 fwnode_handle_put(fixed_node);
751 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
753 if (ret != ARRAY_SIZE(prop)) {
754 phylink_err(pl, "broken fixed-link?\n");
758 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
759 prop, ARRAY_SIZE(prop));
761 pl->link_config.duplex = prop[1] ?
762 DUPLEX_FULL : DUPLEX_HALF;
763 pl->link_config.speed = prop[2];
765 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
766 pl->link_config.lp_advertising);
768 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
769 pl->link_config.lp_advertising);
773 if (pl->link_config.speed > SPEED_1000 &&
774 pl->link_config.duplex != DUPLEX_FULL)
775 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
776 pl->link_config.speed);
778 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
779 linkmode_copy(pl->link_config.advertising, pl->supported);
780 phylink_validate(pl, pl->supported, &pl->link_config);
782 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
783 pl->supported, true);
784 linkmode_zero(pl->supported);
785 phylink_set(pl->supported, MII);
786 phylink_set(pl->supported, Pause);
787 phylink_set(pl->supported, Asym_Pause);
788 phylink_set(pl->supported, Autoneg);
790 __set_bit(s->bit, pl->supported);
791 __set_bit(s->bit, pl->link_config.lp_advertising);
793 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
794 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
795 pl->link_config.speed);
798 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
801 pl->link_config.link = 1;
802 pl->link_config.an_complete = 1;
807 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
809 struct fwnode_handle *dn;
812 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
813 if (dn || fwnode_property_present(fwnode, "fixed-link"))
814 pl->cfg_link_an_mode = MLO_AN_FIXED;
815 fwnode_handle_put(dn);
817 if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
818 strcmp(managed, "in-band-status") == 0) ||
819 pl->config->ovr_an_inband) {
820 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
822 "can't use both fixed-link and in-band-status\n");
826 linkmode_zero(pl->supported);
827 phylink_set(pl->supported, MII);
828 phylink_set(pl->supported, Autoneg);
829 phylink_set(pl->supported, Asym_Pause);
830 phylink_set(pl->supported, Pause);
831 pl->link_config.an_enabled = true;
832 pl->cfg_link_an_mode = MLO_AN_INBAND;
834 switch (pl->link_config.interface) {
835 case PHY_INTERFACE_MODE_SGMII:
836 case PHY_INTERFACE_MODE_QSGMII:
837 case PHY_INTERFACE_MODE_QUSGMII:
838 case PHY_INTERFACE_MODE_RGMII:
839 case PHY_INTERFACE_MODE_RGMII_ID:
840 case PHY_INTERFACE_MODE_RGMII_RXID:
841 case PHY_INTERFACE_MODE_RGMII_TXID:
842 case PHY_INTERFACE_MODE_RTBI:
843 phylink_set(pl->supported, 10baseT_Half);
844 phylink_set(pl->supported, 10baseT_Full);
845 phylink_set(pl->supported, 100baseT_Half);
846 phylink_set(pl->supported, 100baseT_Full);
847 phylink_set(pl->supported, 1000baseT_Half);
848 phylink_set(pl->supported, 1000baseT_Full);
851 case PHY_INTERFACE_MODE_1000BASEX:
852 phylink_set(pl->supported, 1000baseX_Full);
855 case PHY_INTERFACE_MODE_2500BASEX:
856 phylink_set(pl->supported, 2500baseX_Full);
859 case PHY_INTERFACE_MODE_5GBASER:
860 phylink_set(pl->supported, 5000baseT_Full);
863 case PHY_INTERFACE_MODE_25GBASER:
864 phylink_set(pl->supported, 25000baseCR_Full);
865 phylink_set(pl->supported, 25000baseKR_Full);
866 phylink_set(pl->supported, 25000baseSR_Full);
868 case PHY_INTERFACE_MODE_USXGMII:
869 case PHY_INTERFACE_MODE_10GKR:
870 case PHY_INTERFACE_MODE_10GBASER:
871 phylink_set(pl->supported, 10baseT_Half);
872 phylink_set(pl->supported, 10baseT_Full);
873 phylink_set(pl->supported, 100baseT_Half);
874 phylink_set(pl->supported, 100baseT_Full);
875 phylink_set(pl->supported, 1000baseT_Half);
876 phylink_set(pl->supported, 1000baseT_Full);
877 phylink_set(pl->supported, 1000baseX_Full);
878 phylink_set(pl->supported, 1000baseKX_Full);
879 phylink_set(pl->supported, 2500baseT_Full);
880 phylink_set(pl->supported, 2500baseX_Full);
881 phylink_set(pl->supported, 5000baseT_Full);
882 phylink_set(pl->supported, 10000baseT_Full);
883 phylink_set(pl->supported, 10000baseKR_Full);
884 phylink_set(pl->supported, 10000baseKX4_Full);
885 phylink_set(pl->supported, 10000baseCR_Full);
886 phylink_set(pl->supported, 10000baseSR_Full);
887 phylink_set(pl->supported, 10000baseLR_Full);
888 phylink_set(pl->supported, 10000baseLRM_Full);
889 phylink_set(pl->supported, 10000baseER_Full);
892 case PHY_INTERFACE_MODE_XLGMII:
893 phylink_set(pl->supported, 25000baseCR_Full);
894 phylink_set(pl->supported, 25000baseKR_Full);
895 phylink_set(pl->supported, 25000baseSR_Full);
896 phylink_set(pl->supported, 40000baseKR4_Full);
897 phylink_set(pl->supported, 40000baseCR4_Full);
898 phylink_set(pl->supported, 40000baseSR4_Full);
899 phylink_set(pl->supported, 40000baseLR4_Full);
900 phylink_set(pl->supported, 50000baseCR2_Full);
901 phylink_set(pl->supported, 50000baseKR2_Full);
902 phylink_set(pl->supported, 50000baseSR2_Full);
903 phylink_set(pl->supported, 50000baseKR_Full);
904 phylink_set(pl->supported, 50000baseSR_Full);
905 phylink_set(pl->supported, 50000baseCR_Full);
906 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
907 phylink_set(pl->supported, 50000baseDR_Full);
908 phylink_set(pl->supported, 100000baseKR4_Full);
909 phylink_set(pl->supported, 100000baseSR4_Full);
910 phylink_set(pl->supported, 100000baseCR4_Full);
911 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
912 phylink_set(pl->supported, 100000baseKR2_Full);
913 phylink_set(pl->supported, 100000baseSR2_Full);
914 phylink_set(pl->supported, 100000baseCR2_Full);
915 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
916 phylink_set(pl->supported, 100000baseDR2_Full);
921 "incorrect link mode %s for in-band status\n",
922 phy_modes(pl->link_config.interface));
926 linkmode_copy(pl->link_config.advertising, pl->supported);
928 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
930 "failed to validate link configuration for in-band status\n");
934 /* Check if MAC/PCS also supports Autoneg. */
935 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
941 static void phylink_apply_manual_flow(struct phylink *pl,
942 struct phylink_link_state *state)
944 /* If autoneg is disabled, pause AN is also disabled */
945 if (!state->an_enabled)
946 state->pause &= ~MLO_PAUSE_AN;
948 /* Manual configuration of pause modes */
949 if (!(pl->link_config.pause & MLO_PAUSE_AN))
950 state->pause = pl->link_config.pause;
953 static void phylink_resolve_flow(struct phylink_link_state *state)
955 bool tx_pause, rx_pause;
957 state->pause = MLO_PAUSE_NONE;
958 if (state->duplex == DUPLEX_FULL) {
959 linkmode_resolve_pause(state->advertising,
960 state->lp_advertising,
961 &tx_pause, &rx_pause);
963 state->pause |= MLO_PAUSE_TX;
965 state->pause |= MLO_PAUSE_RX;
969 static void phylink_pcs_poll_stop(struct phylink *pl)
971 if (pl->cfg_link_an_mode == MLO_AN_INBAND)
972 del_timer(&pl->link_poll);
975 static void phylink_pcs_poll_start(struct phylink *pl)
977 if (pl->pcs && pl->pcs->poll && pl->cfg_link_an_mode == MLO_AN_INBAND)
978 mod_timer(&pl->link_poll, jiffies + HZ);
981 static void phylink_mac_config(struct phylink *pl,
982 const struct phylink_link_state *state)
985 "%s: mode=%s/%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
986 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
987 phy_modes(state->interface),
988 phy_speed_to_str(state->speed),
989 phy_duplex_to_str(state->duplex),
990 phy_rate_matching_to_str(state->rate_matching),
991 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
992 state->pause, state->link, state->an_enabled);
994 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
997 static void phylink_mac_pcs_an_restart(struct phylink *pl)
999 if (pl->link_config.an_enabled &&
1000 phy_interface_mode_is_8023z(pl->link_config.interface) &&
1001 phylink_autoneg_inband(pl->cur_link_an_mode)) {
1003 pl->pcs->ops->pcs_an_restart(pl->pcs);
1004 else if (pl->config->legacy_pre_march2020)
1005 pl->mac_ops->mac_an_restart(pl->config);
1009 static void phylink_major_config(struct phylink *pl, bool restart,
1010 const struct phylink_link_state *state)
1012 struct phylink_pcs *pcs = NULL;
1013 bool pcs_changed = false;
1016 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
1018 if (pl->using_mac_select_pcs) {
1019 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
1022 "mac_select_pcs unexpectedly failed: %pe\n",
1027 pcs_changed = pcs && pl->pcs != pcs;
1030 phylink_pcs_poll_stop(pl);
1032 if (pl->mac_ops->mac_prepare) {
1033 err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
1036 phylink_err(pl, "mac_prepare failed: %pe\n",
1042 /* If we have a new PCS, switch to the new PCS after preparing the MAC
1048 phylink_mac_config(pl, state);
1051 err = pl->pcs->ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
1054 !!(pl->link_config.pause &
1057 phylink_err(pl, "pcs_config failed: %pe\n",
1063 phylink_mac_pcs_an_restart(pl);
1065 if (pl->mac_ops->mac_finish) {
1066 err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
1069 phylink_err(pl, "mac_finish failed: %pe\n",
1073 phylink_pcs_poll_start(pl);
1077 * Reconfigure for a change of inband advertisement.
1078 * If we have a separate PCS, we only need to call its pcs_config() method,
1079 * and then restart AN if it indicates something changed. Otherwise, we do
1080 * the full MAC reconfiguration.
1082 static int phylink_change_inband_advert(struct phylink *pl)
1086 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
1089 if (!pl->pcs && pl->config->legacy_pre_march2020) {
1091 phylink_mac_config(pl, &pl->link_config);
1092 phylink_mac_pcs_an_restart(pl);
1096 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
1097 phylink_an_mode_str(pl->cur_link_an_mode),
1098 phy_modes(pl->link_config.interface),
1099 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
1100 pl->link_config.pause);
1102 /* Modern PCS-based method; update the advert at the PCS, and
1103 * restart negotiation if the pcs_config() helper indicates that
1104 * the programmed advertisement has changed.
1106 ret = pl->pcs->ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
1107 pl->link_config.interface,
1108 pl->link_config.advertising,
1109 !!(pl->link_config.pause &
1115 phylink_mac_pcs_an_restart(pl);
1120 static void phylink_mac_pcs_get_state(struct phylink *pl,
1121 struct phylink_link_state *state)
1123 linkmode_copy(state->advertising, pl->link_config.advertising);
1124 linkmode_zero(state->lp_advertising);
1125 state->interface = pl->link_config.interface;
1126 state->an_enabled = pl->link_config.an_enabled;
1127 state->rate_matching = pl->link_config.rate_matching;
1128 if (state->an_enabled) {
1129 state->speed = SPEED_UNKNOWN;
1130 state->duplex = DUPLEX_UNKNOWN;
1131 state->pause = MLO_PAUSE_NONE;
1133 state->speed = pl->link_config.speed;
1134 state->duplex = pl->link_config.duplex;
1135 state->pause = pl->link_config.pause;
1137 state->an_complete = 0;
1141 pl->pcs->ops->pcs_get_state(pl->pcs, state);
1142 else if (pl->mac_ops->mac_pcs_get_state &&
1143 pl->config->legacy_pre_march2020)
1144 pl->mac_ops->mac_pcs_get_state(pl->config, state);
1149 /* The fixed state is... fixed except for the link state,
1150 * which may be determined by a GPIO or a callback.
1152 static void phylink_get_fixed_state(struct phylink *pl,
1153 struct phylink_link_state *state)
1155 *state = pl->link_config;
1156 if (pl->config->get_fixed_state)
1157 pl->config->get_fixed_state(pl->config, state);
1158 else if (pl->link_gpio)
1159 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
1161 phylink_resolve_flow(state);
1164 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
1166 struct phylink_link_state link_state;
1168 switch (pl->cur_link_an_mode) {
1170 link_state = pl->phy_state;
1174 phylink_get_fixed_state(pl, &link_state);
1178 link_state = pl->link_config;
1179 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
1180 link_state.pause = MLO_PAUSE_NONE;
1183 default: /* can't happen */
1187 link_state.link = false;
1189 phylink_apply_manual_flow(pl, &link_state);
1190 phylink_major_config(pl, force_restart, &link_state);
1193 static const char *phylink_pause_to_str(int pause)
1195 switch (pause & MLO_PAUSE_TXRX_MASK) {
1196 case MLO_PAUSE_TX | MLO_PAUSE_RX:
1207 static void phylink_link_up(struct phylink *pl,
1208 struct phylink_link_state link_state)
1210 struct net_device *ndev = pl->netdev;
1214 speed = link_state.speed;
1215 duplex = link_state.duplex;
1216 rx_pause = !!(link_state.pause & MLO_PAUSE_RX);
1218 switch (link_state.rate_matching) {
1219 case RATE_MATCH_PAUSE:
1220 /* The PHY is doing rate matchion from the media rate (in
1221 * the link_state) to the interface speed, and will send
1222 * pause frames to the MAC to limit its transmission speed.
1224 speed = phylink_interface_max_speed(link_state.interface);
1225 duplex = DUPLEX_FULL;
1229 case RATE_MATCH_CRS:
1230 /* The PHY is doing rate matchion from the media rate (in
1231 * the link_state) to the interface speed, and will cause
1232 * collisions to the MAC to limit its transmission speed.
1234 speed = phylink_interface_max_speed(link_state.interface);
1235 duplex = DUPLEX_HALF;
1239 pl->cur_interface = link_state.interface;
1241 if (pl->pcs && pl->pcs->ops->pcs_link_up)
1242 pl->pcs->ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
1243 pl->cur_interface, speed, duplex);
1245 pl->mac_ops->mac_link_up(pl->config, pl->phydev, pl->cur_link_an_mode,
1246 pl->cur_interface, speed, duplex,
1247 !!(link_state.pause & MLO_PAUSE_TX), rx_pause);
1250 netif_carrier_on(ndev);
1253 "Link is Up - %s/%s - flow control %s\n",
1254 phy_speed_to_str(link_state.speed),
1255 phy_duplex_to_str(link_state.duplex),
1256 phylink_pause_to_str(link_state.pause));
1259 static void phylink_link_down(struct phylink *pl)
1261 struct net_device *ndev = pl->netdev;
1264 netif_carrier_off(ndev);
1265 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
1267 phylink_info(pl, "Link is Down\n");
1270 static void phylink_resolve(struct work_struct *w)
1272 struct phylink *pl = container_of(w, struct phylink, resolve);
1273 struct phylink_link_state link_state;
1274 struct net_device *ndev = pl->netdev;
1275 bool mac_config = false;
1276 bool retrigger = false;
1277 bool cur_link_state;
1279 mutex_lock(&pl->state_mutex);
1281 cur_link_state = netif_carrier_ok(ndev);
1283 cur_link_state = pl->old_link_state;
1285 if (pl->phylink_disable_state) {
1286 pl->mac_link_dropped = false;
1287 link_state.link = false;
1288 } else if (pl->mac_link_dropped) {
1289 link_state.link = false;
1292 switch (pl->cur_link_an_mode) {
1294 link_state = pl->phy_state;
1295 phylink_apply_manual_flow(pl, &link_state);
1296 mac_config = link_state.link;
1300 phylink_get_fixed_state(pl, &link_state);
1301 mac_config = link_state.link;
1305 phylink_mac_pcs_get_state(pl, &link_state);
1307 /* The PCS may have a latching link-fail indicator.
1308 * If the link was up, bring the link down and
1309 * re-trigger the resolve. Otherwise, re-read the
1310 * PCS state to get the current status of the link.
1312 if (!link_state.link) {
1316 phylink_mac_pcs_get_state(pl,
1320 /* If we have a phy, the "up" state is the union of
1321 * both the PHY and the MAC
1324 link_state.link &= pl->phy_state.link;
1326 /* Only update if the PHY link is up */
1327 if (pl->phydev && pl->phy_state.link) {
1328 /* If the interface has changed, force a
1329 * link down event if the link isn't already
1330 * down, and re-resolve.
1332 if (link_state.interface !=
1333 pl->phy_state.interface) {
1335 link_state.link = false;
1337 link_state.interface = pl->phy_state.interface;
1339 /* If we are doing rate matching, then the
1340 * link speed/duplex comes from the PHY
1342 if (pl->phy_state.rate_matching) {
1343 link_state.rate_matching =
1344 pl->phy_state.rate_matching;
1345 link_state.speed = pl->phy_state.speed;
1347 pl->phy_state.duplex;
1350 /* If we have a PHY, we need to update with
1351 * the PHY flow control bits.
1353 link_state.pause = pl->phy_state.pause;
1356 phylink_apply_manual_flow(pl, &link_state);
1362 if (link_state.interface != pl->link_config.interface) {
1363 /* The interface has changed, force the link down and
1366 if (cur_link_state) {
1367 phylink_link_down(pl);
1368 cur_link_state = false;
1370 phylink_major_config(pl, false, &link_state);
1371 pl->link_config.interface = link_state.interface;
1372 } else if (!pl->pcs && pl->config->legacy_pre_march2020) {
1373 /* The interface remains unchanged, only the speed,
1374 * duplex or pause settings have changed. Call the
1375 * old mac_config() method to configure the MAC/PCS
1376 * only if we do not have a legacy MAC driver.
1378 phylink_mac_config(pl, &link_state);
1382 if (link_state.link != cur_link_state) {
1383 pl->old_link_state = link_state.link;
1384 if (!link_state.link)
1385 phylink_link_down(pl);
1387 phylink_link_up(pl, link_state);
1389 if (!link_state.link && retrigger) {
1390 pl->mac_link_dropped = false;
1391 queue_work(system_power_efficient_wq, &pl->resolve);
1393 mutex_unlock(&pl->state_mutex);
1396 static void phylink_run_resolve(struct phylink *pl)
1398 if (!pl->phylink_disable_state)
1399 queue_work(system_power_efficient_wq, &pl->resolve);
1402 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
1404 unsigned long state = pl->phylink_disable_state;
1406 set_bit(bit, &pl->phylink_disable_state);
1408 queue_work(system_power_efficient_wq, &pl->resolve);
1409 flush_work(&pl->resolve);
1413 static void phylink_enable_and_run_resolve(struct phylink *pl, int bit)
1415 clear_bit(bit, &pl->phylink_disable_state);
1416 phylink_run_resolve(pl);
1419 static void phylink_fixed_poll(struct timer_list *t)
1421 struct phylink *pl = container_of(t, struct phylink, link_poll);
1423 mod_timer(t, jiffies + HZ);
1425 phylink_run_resolve(pl);
1428 static const struct sfp_upstream_ops sfp_phylink_ops;
1430 static int phylink_register_sfp(struct phylink *pl,
1431 struct fwnode_handle *fwnode)
1433 struct sfp_bus *bus;
1439 bus = sfp_bus_find_fwnode(fwnode);
1441 phylink_err(pl, "unable to attach SFP bus: %pe\n", bus);
1442 return PTR_ERR(bus);
1447 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
1454 * phylink_create() - create a phylink instance
1455 * @config: a pointer to the target &struct phylink_config
1456 * @fwnode: a pointer to a &struct fwnode_handle describing the network
1458 * @iface: the desired link mode defined by &typedef phy_interface_t
1459 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
1461 * Create a new phylink instance, and parse the link parameters found in @np.
1462 * This will parse in-band modes, fixed-link or SFP configuration.
1464 * Note: the rtnl lock must not be held when calling this function.
1466 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
1467 * must use IS_ERR() to check for errors from this function.
1469 struct phylink *phylink_create(struct phylink_config *config,
1470 struct fwnode_handle *fwnode,
1471 phy_interface_t iface,
1472 const struct phylink_mac_ops *mac_ops)
1474 bool using_mac_select_pcs = false;
1478 if (mac_ops->mac_select_pcs &&
1479 mac_ops->mac_select_pcs(config, PHY_INTERFACE_MODE_NA) !=
1480 ERR_PTR(-EOPNOTSUPP))
1481 using_mac_select_pcs = true;
1483 /* Validate the supplied configuration */
1484 if (using_mac_select_pcs &&
1485 phy_interface_empty(config->supported_interfaces)) {
1486 dev_err(config->dev,
1487 "phylink: error: empty supported_interfaces but mac_select_pcs() method present\n");
1488 return ERR_PTR(-EINVAL);
1491 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
1493 return ERR_PTR(-ENOMEM);
1495 mutex_init(&pl->state_mutex);
1496 INIT_WORK(&pl->resolve, phylink_resolve);
1498 pl->config = config;
1499 if (config->type == PHYLINK_NETDEV) {
1500 pl->netdev = to_net_dev(config->dev);
1501 } else if (config->type == PHYLINK_DEV) {
1502 pl->dev = config->dev;
1505 return ERR_PTR(-EINVAL);
1508 pl->using_mac_select_pcs = using_mac_select_pcs;
1509 pl->phy_state.interface = iface;
1510 pl->link_interface = iface;
1511 if (iface == PHY_INTERFACE_MODE_MOCA)
1512 pl->link_port = PORT_BNC;
1514 pl->link_port = PORT_MII;
1515 pl->link_config.interface = iface;
1516 pl->link_config.pause = MLO_PAUSE_AN;
1517 pl->link_config.speed = SPEED_UNKNOWN;
1518 pl->link_config.duplex = DUPLEX_UNKNOWN;
1519 pl->link_config.an_enabled = true;
1520 pl->mac_ops = mac_ops;
1521 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1522 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
1524 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1525 linkmode_copy(pl->link_config.advertising, pl->supported);
1526 phylink_validate(pl, pl->supported, &pl->link_config);
1528 ret = phylink_parse_mode(pl, fwnode);
1531 return ERR_PTR(ret);
1534 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
1535 ret = phylink_parse_fixedlink(pl, fwnode);
1538 return ERR_PTR(ret);
1542 pl->cur_link_an_mode = pl->cfg_link_an_mode;
1544 ret = phylink_register_sfp(pl, fwnode);
1547 return ERR_PTR(ret);
1552 EXPORT_SYMBOL_GPL(phylink_create);
1555 * phylink_destroy() - cleanup and destroy the phylink instance
1556 * @pl: a pointer to a &struct phylink returned from phylink_create()
1558 * Destroy a phylink instance. Any PHY that has been attached must have been
1559 * cleaned up via phylink_disconnect_phy() prior to calling this function.
1561 * Note: the rtnl lock must not be held when calling this function.
1563 void phylink_destroy(struct phylink *pl)
1565 sfp_bus_del_upstream(pl->sfp_bus);
1567 gpiod_put(pl->link_gpio);
1569 cancel_work_sync(&pl->resolve);
1572 EXPORT_SYMBOL_GPL(phylink_destroy);
1574 static void phylink_phy_change(struct phy_device *phydev, bool up)
1576 struct phylink *pl = phydev->phylink;
1577 bool tx_pause, rx_pause;
1579 phy_get_pause(phydev, &tx_pause, &rx_pause);
1581 mutex_lock(&pl->state_mutex);
1582 pl->phy_state.speed = phydev->speed;
1583 pl->phy_state.duplex = phydev->duplex;
1584 pl->phy_state.rate_matching = phydev->rate_matching;
1585 pl->phy_state.pause = MLO_PAUSE_NONE;
1587 pl->phy_state.pause |= MLO_PAUSE_TX;
1589 pl->phy_state.pause |= MLO_PAUSE_RX;
1590 pl->phy_state.interface = phydev->interface;
1591 pl->phy_state.link = up;
1592 mutex_unlock(&pl->state_mutex);
1594 phylink_run_resolve(pl);
1596 phylink_dbg(pl, "phy link %s %s/%s/%s/%s/%s\n", up ? "up" : "down",
1597 phy_modes(phydev->interface),
1598 phy_speed_to_str(phydev->speed),
1599 phy_duplex_to_str(phydev->duplex),
1600 phy_rate_matching_to_str(phydev->rate_matching),
1601 phylink_pause_to_str(pl->phy_state.pause));
1604 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
1605 phy_interface_t interface)
1607 struct phylink_link_state config;
1608 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
1613 * This is the new way of dealing with flow control for PHYs,
1614 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
1615 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
1616 * using our validate call to the MAC, we rely upon the MAC
1617 * clearing the bits from both supported and advertising fields.
1619 phy_support_asym_pause(phy);
1621 memset(&config, 0, sizeof(config));
1622 linkmode_copy(supported, phy->supported);
1623 linkmode_copy(config.advertising, phy->advertising);
1625 /* Check whether we would use rate matching for the proposed interface
1628 config.rate_matching = phy_get_rate_matching(phy, interface);
1630 /* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
1631 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
1632 * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching
1633 * their Serdes is either unnecessary or not reasonable.
1635 * For these which switch interface modes, we really need to know which
1636 * interface modes the PHY supports to properly work out which ethtool
1637 * linkmodes can be supported. For now, as a work-around, we validate
1638 * against all interface modes, which may lead to more ethtool link
1639 * modes being advertised than are actually supported.
1641 if (phy->is_c45 && config.rate_matching == RATE_MATCH_NONE &&
1642 interface != PHY_INTERFACE_MODE_RXAUI &&
1643 interface != PHY_INTERFACE_MODE_XAUI &&
1644 interface != PHY_INTERFACE_MODE_USXGMII)
1645 config.interface = PHY_INTERFACE_MODE_NA;
1647 config.interface = interface;
1649 ret = phylink_validate(pl, supported, &config);
1651 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
1652 phy_modes(config.interface),
1653 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1654 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1660 phy->phy_link_change = phylink_phy_change;
1662 irq_str = phy_attached_info_irq(phy);
1664 "PHY [%s] driver [%s] (irq=%s)\n",
1665 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1668 mutex_lock(&phy->lock);
1669 mutex_lock(&pl->state_mutex);
1671 pl->phy_state.interface = interface;
1672 pl->phy_state.pause = MLO_PAUSE_NONE;
1673 pl->phy_state.speed = SPEED_UNKNOWN;
1674 pl->phy_state.duplex = DUPLEX_UNKNOWN;
1675 pl->phy_state.rate_matching = RATE_MATCH_NONE;
1676 linkmode_copy(pl->supported, supported);
1677 linkmode_copy(pl->link_config.advertising, config.advertising);
1679 /* Restrict the phy advertisement according to the MAC support. */
1680 linkmode_copy(phy->advertising, config.advertising);
1681 mutex_unlock(&pl->state_mutex);
1682 mutex_unlock(&phy->lock);
1685 "phy: %s setting supported %*pb advertising %*pb\n",
1686 phy_modes(interface),
1687 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1688 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1690 if (phy_interrupt_is_valid(phy))
1691 phy_request_interrupt(phy);
1693 if (pl->config->mac_managed_pm)
1694 phy->mac_managed_pm = true;
1699 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1700 phy_interface_t interface)
1702 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1703 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1704 phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
1710 return phy_attach_direct(pl->netdev, phy, 0, interface);
1714 * phylink_connect_phy() - connect a PHY to the phylink instance
1715 * @pl: a pointer to a &struct phylink returned from phylink_create()
1716 * @phy: a pointer to a &struct phy_device.
1718 * Connect @phy to the phylink instance specified by @pl by calling
1719 * phy_attach_direct(). Configure the @phy according to the MAC driver's
1720 * capabilities, start the PHYLIB state machine and enable any interrupts
1721 * that the PHY supports.
1723 * This updates the phylink's ethtool supported and advertising link mode
1726 * Returns 0 on success or a negative errno.
1728 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1732 /* Use PHY device/driver interface */
1733 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1734 pl->link_interface = phy->interface;
1735 pl->link_config.interface = pl->link_interface;
1738 ret = phylink_attach_phy(pl, phy, pl->link_interface);
1742 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1748 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1751 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1752 * @pl: a pointer to a &struct phylink returned from phylink_create()
1753 * @dn: a pointer to a &struct device_node.
1754 * @flags: PHY-specific flags to communicate to the PHY device driver
1756 * Connect the phy specified in the device node @dn to the phylink instance
1757 * specified by @pl. Actions specified in phylink_connect_phy() will be
1760 * Returns 0 on success or a negative errno.
1762 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1765 return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
1767 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1770 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
1771 * @pl: a pointer to a &struct phylink returned from phylink_create()
1772 * @fwnode: a pointer to a &struct fwnode_handle.
1773 * @flags: PHY-specific flags to communicate to the PHY device driver
1775 * Connect the phy specified @fwnode to the phylink instance specified
1778 * Returns 0 on success or a negative errno.
1780 int phylink_fwnode_phy_connect(struct phylink *pl,
1781 struct fwnode_handle *fwnode,
1784 struct fwnode_handle *phy_fwnode;
1785 struct phy_device *phy_dev;
1788 /* Fixed links and 802.3z are handled without needing a PHY */
1789 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1790 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1791 phy_interface_mode_is_8023z(pl->link_interface)))
1794 phy_fwnode = fwnode_get_phy_node(fwnode);
1795 if (IS_ERR(phy_fwnode)) {
1796 if (pl->cfg_link_an_mode == MLO_AN_PHY)
1801 phy_dev = fwnode_phy_find_device(phy_fwnode);
1802 /* We're done with the phy_node handle */
1803 fwnode_handle_put(phy_fwnode);
1807 /* Use PHY device/driver interface */
1808 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1809 pl->link_interface = phy_dev->interface;
1810 pl->link_config.interface = pl->link_interface;
1813 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1814 pl->link_interface);
1816 phy_device_free(phy_dev);
1820 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1822 phy_detach(phy_dev);
1826 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
1829 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1831 * @pl: a pointer to a &struct phylink returned from phylink_create()
1833 * Disconnect any current PHY from the phylink instance described by @pl.
1835 void phylink_disconnect_phy(struct phylink *pl)
1837 struct phy_device *phy;
1843 mutex_lock(&phy->lock);
1844 mutex_lock(&pl->state_mutex);
1846 mutex_unlock(&pl->state_mutex);
1847 mutex_unlock(&phy->lock);
1848 flush_work(&pl->resolve);
1850 phy_disconnect(phy);
1853 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1856 * phylink_mac_change() - notify phylink of a change in MAC state
1857 * @pl: a pointer to a &struct phylink returned from phylink_create()
1858 * @up: indicates whether the link is currently up.
1860 * The MAC driver should call this driver when the state of its link
1861 * changes (eg, link failure, new negotiation results, etc.)
1863 void phylink_mac_change(struct phylink *pl, bool up)
1866 pl->mac_link_dropped = true;
1867 phylink_run_resolve(pl);
1868 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1870 EXPORT_SYMBOL_GPL(phylink_mac_change);
1872 static irqreturn_t phylink_link_handler(int irq, void *data)
1874 struct phylink *pl = data;
1876 phylink_run_resolve(pl);
1882 * phylink_start() - start a phylink instance
1883 * @pl: a pointer to a &struct phylink returned from phylink_create()
1885 * Start the phylink instance specified by @pl, configuring the MAC for the
1886 * desired link mode(s) and negotiation style. This should be called from the
1887 * network device driver's &struct net_device_ops ndo_open() method.
1889 void phylink_start(struct phylink *pl)
1895 phylink_info(pl, "configuring for %s/%s link mode\n",
1896 phylink_an_mode_str(pl->cur_link_an_mode),
1897 phy_modes(pl->link_config.interface));
1899 /* Always set the carrier off */
1901 netif_carrier_off(pl->netdev);
1903 /* Apply the link configuration to the MAC when starting. This allows
1904 * a fixed-link to start with the correct parameters, and also
1905 * ensures that we set the appropriate advertisement for Serdes links.
1907 * Restart autonegotiation if using 802.3z to ensure that the link
1908 * parameters are properly negotiated. This is necessary for DSA
1909 * switches using 802.3z negotiation to ensure they see our modes.
1911 phylink_mac_initial_config(pl, true);
1913 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
1915 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1916 int irq = gpiod_to_irq(pl->link_gpio);
1919 if (!request_irq(irq, phylink_link_handler,
1920 IRQF_TRIGGER_RISING |
1921 IRQF_TRIGGER_FALLING,
1931 switch (pl->cfg_link_an_mode) {
1933 poll |= pl->config->poll_fixed_state;
1937 poll |= pl->pcs->poll;
1941 mod_timer(&pl->link_poll, jiffies + HZ);
1943 phy_start(pl->phydev);
1945 sfp_upstream_start(pl->sfp_bus);
1947 EXPORT_SYMBOL_GPL(phylink_start);
1950 * phylink_stop() - stop a phylink instance
1951 * @pl: a pointer to a &struct phylink returned from phylink_create()
1953 * Stop the phylink instance specified by @pl. This should be called from the
1954 * network device driver's &struct net_device_ops ndo_stop() method. The
1955 * network device's carrier state should not be changed prior to calling this
1958 * This will synchronously bring down the link if the link is not already
1959 * down (in other words, it will trigger a mac_link_down() method call.)
1961 void phylink_stop(struct phylink *pl)
1966 sfp_upstream_stop(pl->sfp_bus);
1968 phy_stop(pl->phydev);
1969 del_timer_sync(&pl->link_poll);
1971 free_irq(pl->link_irq, pl);
1975 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1977 EXPORT_SYMBOL_GPL(phylink_stop);
1980 * phylink_suspend() - handle a network device suspend event
1981 * @pl: a pointer to a &struct phylink returned from phylink_create()
1982 * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
1984 * Handle a network device suspend event. There are several cases:
1986 * - If Wake-on-Lan is not active, we can bring down the link between
1987 * the MAC and PHY by calling phylink_stop().
1988 * - If Wake-on-Lan is active, and being handled only by the PHY, we
1989 * can also bring down the link between the MAC and PHY.
1990 * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
1991 * still needs to receive packets, so we can not bring the link down.
1993 void phylink_suspend(struct phylink *pl, bool mac_wol)
1997 if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) {
1998 /* Wake-on-Lan enabled, MAC handling */
1999 mutex_lock(&pl->state_mutex);
2001 /* Stop the resolver bringing the link up */
2002 __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
2004 /* Disable the carrier, to prevent transmit timeouts,
2005 * but one would hope all packets have been sent. This
2006 * also means phylink_resolve() will do nothing.
2009 netif_carrier_off(pl->netdev);
2011 pl->old_link_state = false;
2013 /* We do not call mac_link_down() here as we want the
2014 * link to remain up to receive the WoL packets.
2016 mutex_unlock(&pl->state_mutex);
2021 EXPORT_SYMBOL_GPL(phylink_suspend);
2024 * phylink_resume() - handle a network device resume event
2025 * @pl: a pointer to a &struct phylink returned from phylink_create()
2027 * Undo the effects of phylink_suspend(), returning the link to an
2028 * operational state.
2030 void phylink_resume(struct phylink *pl)
2034 if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
2035 /* Wake-on-Lan enabled, MAC handling */
2037 /* Call mac_link_down() so we keep the overall state balanced.
2038 * Do this under the state_mutex lock for consistency. This
2039 * will cause a "Link Down" message to be printed during
2040 * resume, which is harmless - the true link state will be
2041 * printed when we run a resolve.
2043 mutex_lock(&pl->state_mutex);
2044 phylink_link_down(pl);
2045 mutex_unlock(&pl->state_mutex);
2047 /* Re-apply the link parameters so that all the settings get
2048 * restored to the MAC.
2050 phylink_mac_initial_config(pl, true);
2052 /* Re-enable and re-resolve the link parameters */
2053 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL);
2058 EXPORT_SYMBOL_GPL(phylink_resume);
2061 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
2062 * @pl: a pointer to a &struct phylink returned from phylink_create()
2063 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
2065 * Read the wake on lan parameters from the PHY attached to the phylink
2066 * instance specified by @pl. If no PHY is currently attached, report no
2067 * support for wake on lan.
2069 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2077 phy_ethtool_get_wol(pl->phydev, wol);
2079 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
2082 * phylink_ethtool_set_wol() - set wake on lan parameters
2083 * @pl: a pointer to a &struct phylink returned from phylink_create()
2084 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
2086 * Set the wake on lan parameters for the PHY attached to the phylink
2087 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
2090 * Returns zero on success or negative errno code.
2092 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2094 int ret = -EOPNOTSUPP;
2099 ret = phy_ethtool_set_wol(pl->phydev, wol);
2103 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
2105 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
2107 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
2109 linkmode_zero(mask);
2110 phylink_set_port_modes(mask);
2112 linkmode_and(dst, dst, mask);
2113 linkmode_or(dst, dst, b);
2116 static void phylink_get_ksettings(const struct phylink_link_state *state,
2117 struct ethtool_link_ksettings *kset)
2119 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
2120 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
2121 if (kset->base.rate_matching == RATE_MATCH_NONE) {
2122 kset->base.speed = state->speed;
2123 kset->base.duplex = state->duplex;
2125 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
2130 * phylink_ethtool_ksettings_get() - get the current link settings
2131 * @pl: a pointer to a &struct phylink returned from phylink_create()
2132 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
2134 * Read the current link settings for the phylink instance specified by @pl.
2135 * This will be the link settings read from the MAC, PHY or fixed link
2136 * settings depending on the current negotiation mode.
2138 int phylink_ethtool_ksettings_get(struct phylink *pl,
2139 struct ethtool_link_ksettings *kset)
2141 struct phylink_link_state link_state;
2146 phy_ethtool_ksettings_get(pl->phydev, kset);
2148 kset->base.port = pl->link_port;
2150 linkmode_copy(kset->link_modes.supported, pl->supported);
2152 switch (pl->cur_link_an_mode) {
2154 /* We are using fixed settings. Report these as the
2155 * current link settings - and note that these also
2156 * represent the supported speeds/duplex/pause modes.
2158 phylink_get_fixed_state(pl, &link_state);
2159 phylink_get_ksettings(&link_state, kset);
2163 /* If there is a phy attached, then use the reported
2164 * settings from the phy with no modification.
2169 phylink_mac_pcs_get_state(pl, &link_state);
2171 /* The MAC is reporting the link results from its own PCS
2172 * layer via in-band status. Report these as the current
2175 phylink_get_ksettings(&link_state, kset);
2181 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
2184 * phylink_ethtool_ksettings_set() - set the link settings
2185 * @pl: a pointer to a &struct phylink returned from phylink_create()
2186 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
2188 int phylink_ethtool_ksettings_set(struct phylink *pl,
2189 const struct ethtool_link_ksettings *kset)
2191 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2192 struct phylink_link_state config;
2193 const struct phy_setting *s;
2198 /* We can rely on phylib for this update; we also do not need
2199 * to update the pl->link_config settings:
2200 * - the configuration returned via ksettings_get() will come
2201 * from phylib whenever a PHY is present.
2202 * - link_config.interface will be updated by the PHY calling
2203 * back via phylink_phy_change() and a subsequent resolve.
2204 * - initial link configuration for PHY mode comes from the
2205 * last phy state updated via phylink_phy_change().
2206 * - other configuration changes (e.g. pause modes) are
2207 * performed directly via phylib.
2208 * - if in in-band mode with a PHY, the link configuration
2209 * is passed on the link from the PHY, and all of
2210 * link_config.{speed,duplex,an_enabled,pause} are not used.
2211 * - the only possible use would be link_config.advertising
2212 * pause modes when in 1000base-X mode with a PHY, but in
2213 * the presence of a PHY, this should not be changed as that
2214 * should be determined from the media side advertisement.
2216 return phy_ethtool_ksettings_set(pl->phydev, kset);
2219 config = pl->link_config;
2221 /* Mask out unsupported advertisements */
2222 linkmode_and(config.advertising, kset->link_modes.advertising,
2225 /* FIXME: should we reject autoneg if phy/mac does not support it? */
2226 switch (kset->base.autoneg) {
2227 case AUTONEG_DISABLE:
2228 /* Autonegotiation disabled, select a suitable speed and
2231 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
2232 pl->supported, false);
2236 /* If we have a fixed link, refuse to change link parameters.
2237 * If the link parameters match, accept them but do nothing.
2239 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
2240 if (s->speed != pl->link_config.speed ||
2241 s->duplex != pl->link_config.duplex)
2246 config.speed = s->speed;
2247 config.duplex = s->duplex;
2250 case AUTONEG_ENABLE:
2251 /* If we have a fixed link, allow autonegotiation (since that
2252 * is our default case) but do not allow the advertisement to
2253 * be changed. If the advertisement matches, simply return.
2255 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
2256 if (!linkmode_equal(config.advertising,
2257 pl->link_config.advertising))
2262 config.speed = SPEED_UNKNOWN;
2263 config.duplex = DUPLEX_UNKNOWN;
2270 /* We have ruled out the case with a PHY attached, and the
2271 * fixed-link cases. All that is left are in-band links.
2273 config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
2274 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
2277 /* If this link is with an SFP, ensure that changes to advertised modes
2278 * also cause the associated interface to be selected such that the
2279 * link can be configured correctly.
2282 config.interface = sfp_select_interface(pl->sfp_bus,
2283 config.advertising);
2284 if (config.interface == PHY_INTERFACE_MODE_NA) {
2286 "selection of interface failed, advertisement %*pb\n",
2287 __ETHTOOL_LINK_MODE_MASK_NBITS,
2288 config.advertising);
2292 /* Revalidate with the selected interface */
2293 linkmode_copy(support, pl->supported);
2294 if (phylink_validate(pl, support, &config)) {
2295 phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
2296 phylink_an_mode_str(pl->cur_link_an_mode),
2297 phy_modes(config.interface),
2298 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2302 /* Validate without changing the current supported mask. */
2303 linkmode_copy(support, pl->supported);
2304 if (phylink_validate(pl, support, &config))
2308 /* If autonegotiation is enabled, we must have an advertisement */
2309 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
2312 mutex_lock(&pl->state_mutex);
2313 pl->link_config.speed = config.speed;
2314 pl->link_config.duplex = config.duplex;
2315 pl->link_config.an_enabled = config.an_enabled;
2317 if (pl->link_config.interface != config.interface) {
2318 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
2319 /* We need to force the link down, then change the interface */
2320 if (pl->old_link_state) {
2321 phylink_link_down(pl);
2322 pl->old_link_state = false;
2324 if (!test_bit(PHYLINK_DISABLE_STOPPED,
2325 &pl->phylink_disable_state))
2326 phylink_major_config(pl, false, &config);
2327 pl->link_config.interface = config.interface;
2328 linkmode_copy(pl->link_config.advertising, config.advertising);
2329 } else if (!linkmode_equal(pl->link_config.advertising,
2330 config.advertising)) {
2331 linkmode_copy(pl->link_config.advertising, config.advertising);
2332 phylink_change_inband_advert(pl);
2334 mutex_unlock(&pl->state_mutex);
2338 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
2341 * phylink_ethtool_nway_reset() - restart negotiation
2342 * @pl: a pointer to a &struct phylink returned from phylink_create()
2344 * Restart negotiation for the phylink instance specified by @pl. This will
2345 * cause any attached phy to restart negotiation with the link partner, and
2346 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
2349 * Returns zero on success, or negative error code.
2351 int phylink_ethtool_nway_reset(struct phylink *pl)
2358 ret = phy_restart_aneg(pl->phydev);
2359 phylink_mac_pcs_an_restart(pl);
2363 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
2366 * phylink_ethtool_get_pauseparam() - get the current pause parameters
2367 * @pl: a pointer to a &struct phylink returned from phylink_create()
2368 * @pause: a pointer to a &struct ethtool_pauseparam
2370 void phylink_ethtool_get_pauseparam(struct phylink *pl,
2371 struct ethtool_pauseparam *pause)
2375 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
2376 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
2377 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
2379 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
2382 * phylink_ethtool_set_pauseparam() - set the current pause parameters
2383 * @pl: a pointer to a &struct phylink returned from phylink_create()
2384 * @pause: a pointer to a &struct ethtool_pauseparam
2386 int phylink_ethtool_set_pauseparam(struct phylink *pl,
2387 struct ethtool_pauseparam *pause)
2389 struct phylink_link_state *config = &pl->link_config;
2390 bool manual_changed;
2395 if (pl->cur_link_an_mode == MLO_AN_FIXED)
2398 if (!phylink_test(pl->supported, Pause) &&
2399 !phylink_test(pl->supported, Asym_Pause))
2402 if (!phylink_test(pl->supported, Asym_Pause) &&
2403 pause->rx_pause != pause->tx_pause)
2408 pause_state |= MLO_PAUSE_AN;
2409 if (pause->rx_pause)
2410 pause_state |= MLO_PAUSE_RX;
2411 if (pause->tx_pause)
2412 pause_state |= MLO_PAUSE_TX;
2414 mutex_lock(&pl->state_mutex);
2416 * See the comments for linkmode_set_pause(), wrt the deficiencies
2417 * with the current implementation. A solution to this issue would
2419 * ethtool Local device
2420 * rx tx Pause AsymDir
2425 * and then use the ethtool rx/tx enablement status to mask the
2426 * rx/tx pause resolution.
2428 linkmode_set_pause(config->advertising, pause->tx_pause,
2431 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
2432 (!(pause_state & MLO_PAUSE_AN) &&
2433 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
2435 config->pause = pause_state;
2437 /* Update our in-band advertisement, triggering a renegotiation if
2438 * the advertisement changed.
2441 phylink_change_inband_advert(pl);
2443 mutex_unlock(&pl->state_mutex);
2445 /* If we have a PHY, a change of the pause frame advertisement will
2446 * cause phylib to renegotiate (if AN is enabled) which will in turn
2447 * call our phylink_phy_change() and trigger a resolve. Note that
2448 * we can't hold our state mutex while calling phy_set_asym_pause().
2451 phy_set_asym_pause(pl->phydev, pause->rx_pause,
2454 /* If the manual pause settings changed, make sure we trigger a
2455 * resolve to update their state; we can not guarantee that the
2458 if (manual_changed) {
2459 pl->mac_link_dropped = true;
2460 phylink_run_resolve(pl);
2465 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
2468 * phylink_get_eee_err() - read the energy efficient ethernet error
2470 * @pl: a pointer to a &struct phylink returned from phylink_create().
2472 * Read the Energy Efficient Ethernet error counter from the PHY associated
2473 * with the phylink instance specified by @pl.
2475 * Returns positive error counter value, or negative error code.
2477 int phylink_get_eee_err(struct phylink *pl)
2484 ret = phy_get_eee_err(pl->phydev);
2488 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
2491 * phylink_init_eee() - init and check the EEE features
2492 * @pl: a pointer to a &struct phylink returned from phylink_create()
2493 * @clk_stop_enable: allow PHY to stop receive clock
2495 * Must be called either with RTNL held or within mac_link_up()
2497 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
2499 int ret = -EOPNOTSUPP;
2502 ret = phy_init_eee(pl->phydev, clk_stop_enable);
2506 EXPORT_SYMBOL_GPL(phylink_init_eee);
2509 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
2510 * @pl: a pointer to a &struct phylink returned from phylink_create()
2511 * @eee: a pointer to a &struct ethtool_eee for the read parameters
2513 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
2515 int ret = -EOPNOTSUPP;
2520 ret = phy_ethtool_get_eee(pl->phydev, eee);
2524 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
2527 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
2528 * @pl: a pointer to a &struct phylink returned from phylink_create()
2529 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
2531 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
2533 int ret = -EOPNOTSUPP;
2538 ret = phy_ethtool_set_eee(pl->phydev, eee);
2542 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
2544 /* This emulates MII registers for a fixed-mode phy operating as per the
2545 * passed in state. "aneg" defines if we report negotiation is possible.
2547 * FIXME: should deal with negotiation state too.
2549 static int phylink_mii_emul_read(unsigned int reg,
2550 struct phylink_link_state *state)
2552 struct fixed_phy_status fs;
2553 unsigned long *lpa = state->lp_advertising;
2556 fs.link = state->link;
2557 fs.speed = state->speed;
2558 fs.duplex = state->duplex;
2559 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
2560 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
2562 val = swphy_read_reg(reg, &fs);
2563 if (reg == MII_BMSR) {
2564 if (!state->an_complete)
2565 val &= ~BMSR_ANEGCOMPLETE;
2570 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
2573 struct phy_device *phydev = pl->phydev;
2576 if (mdio_phy_id_is_c45(phy_id)) {
2577 prtad = mdio_phy_id_prtad(phy_id);
2578 devad = mdio_phy_id_devad(phy_id);
2579 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
2583 if (phydev->is_c45) {
2589 devad = __ffs(phydev->c45_ids.mmds_present);
2593 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2595 devad = MDIO_MMD_AN;
2596 if (reg == MII_ADVERTISE)
2597 reg = MDIO_AN_ADVERTISE;
2605 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
2609 return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg);
2612 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
2613 unsigned int reg, unsigned int val)
2615 struct phy_device *phydev = pl->phydev;
2618 if (mdio_phy_id_is_c45(phy_id)) {
2619 prtad = mdio_phy_id_prtad(phy_id);
2620 devad = mdio_phy_id_devad(phy_id);
2621 return mdiobus_c45_write(pl->phydev->mdio.bus, prtad, devad,
2625 if (phydev->is_c45) {
2631 devad = __ffs(phydev->c45_ids.mmds_present);
2635 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2637 devad = MDIO_MMD_AN;
2638 if (reg == MII_ADVERTISE)
2639 reg = MDIO_AN_ADVERTISE;
2646 return mdiobus_c45_write(pl->phydev->mdio.bus, phy_id, devad,
2650 return mdiobus_write(phydev->mdio.bus, phy_id, reg, val);
2653 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
2656 struct phylink_link_state state;
2659 switch (pl->cur_link_an_mode) {
2662 phylink_get_fixed_state(pl, &state);
2663 val = phylink_mii_emul_read(reg, &state);
2672 phylink_mac_pcs_get_state(pl, &state);
2673 val = phylink_mii_emul_read(reg, &state);
2678 return val & 0xffff;
2681 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
2682 unsigned int reg, unsigned int val)
2684 switch (pl->cur_link_an_mode) {
2699 * phylink_mii_ioctl() - generic mii ioctl interface
2700 * @pl: a pointer to a &struct phylink returned from phylink_create()
2701 * @ifr: a pointer to a &struct ifreq for socket ioctls
2702 * @cmd: ioctl cmd to execute
2704 * Perform the specified MII ioctl on the PHY attached to the phylink instance
2705 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
2707 * Returns: zero on success or negative error code.
2710 * read register from the current PHY.
2712 * read register from the specified PHY.
2714 * set a register on the specified PHY.
2716 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
2718 struct mii_ioctl_data *mii = if_mii(ifr);
2724 /* PHYs only exist for MLO_AN_PHY and SGMII */
2727 mii->phy_id = pl->phydev->mdio.addr;
2731 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
2739 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
2744 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
2754 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
2762 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
2774 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2777 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2779 * @pl: a pointer to a &struct phylink returned from phylink_create()
2780 * @sync: perform action synchronously
2782 * If we have a PHY that is not part of a SFP module, then set the speed
2783 * as described in the phy_speed_down() function. Please see this function
2784 * for a description of the @sync parameter.
2786 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2788 int phylink_speed_down(struct phylink *pl, bool sync)
2794 if (!pl->sfp_bus && pl->phydev)
2795 ret = phy_speed_down(pl->phydev, sync);
2799 EXPORT_SYMBOL_GPL(phylink_speed_down);
2802 * phylink_speed_up() - restore the advertised speeds prior to the call to
2803 * phylink_speed_down()
2804 * @pl: a pointer to a &struct phylink returned from phylink_create()
2806 * If we have a PHY that is not part of a SFP module, then restore the
2807 * PHY speeds as per phy_speed_up().
2809 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2811 int phylink_speed_up(struct phylink *pl)
2817 if (!pl->sfp_bus && pl->phydev)
2818 ret = phy_speed_up(pl->phydev);
2822 EXPORT_SYMBOL_GPL(phylink_speed_up);
2824 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2826 struct phylink *pl = upstream;
2828 pl->netdev->sfp_bus = bus;
2831 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2833 struct phylink *pl = upstream;
2835 pl->netdev->sfp_bus = NULL;
2838 static const phy_interface_t phylink_sfp_interface_preference[] = {
2839 PHY_INTERFACE_MODE_25GBASER,
2840 PHY_INTERFACE_MODE_USXGMII,
2841 PHY_INTERFACE_MODE_10GBASER,
2842 PHY_INTERFACE_MODE_5GBASER,
2843 PHY_INTERFACE_MODE_2500BASEX,
2844 PHY_INTERFACE_MODE_SGMII,
2845 PHY_INTERFACE_MODE_1000BASEX,
2846 PHY_INTERFACE_MODE_100BASEX,
2849 static DECLARE_PHY_INTERFACE_MASK(phylink_sfp_interfaces);
2851 static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
2852 const unsigned long *intf)
2854 phy_interface_t interface;
2857 interface = PHY_INTERFACE_MODE_NA;
2858 for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++)
2859 if (test_bit(phylink_sfp_interface_preference[i], intf)) {
2860 interface = phylink_sfp_interface_preference[i];
2867 static void phylink_sfp_set_config(struct phylink *pl, u8 mode,
2868 unsigned long *supported,
2869 struct phylink_link_state *state)
2871 bool changed = false;
2873 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2874 phylink_an_mode_str(mode), phy_modes(state->interface),
2875 __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
2877 if (!linkmode_equal(pl->supported, supported)) {
2878 linkmode_copy(pl->supported, supported);
2882 if (!linkmode_equal(pl->link_config.advertising, state->advertising)) {
2883 linkmode_copy(pl->link_config.advertising, state->advertising);
2887 if (pl->cur_link_an_mode != mode ||
2888 pl->link_config.interface != state->interface) {
2889 pl->cur_link_an_mode = mode;
2890 pl->link_config.interface = state->interface;
2894 phylink_info(pl, "switched to %s/%s link mode\n",
2895 phylink_an_mode_str(mode),
2896 phy_modes(state->interface));
2899 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2900 &pl->phylink_disable_state))
2901 phylink_mac_initial_config(pl, false);
2904 static int phylink_sfp_config_phy(struct phylink *pl, u8 mode,
2905 struct phy_device *phy)
2907 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2908 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2909 struct phylink_link_state config;
2910 phy_interface_t iface;
2913 linkmode_copy(support, phy->supported);
2915 memset(&config, 0, sizeof(config));
2916 linkmode_copy(config.advertising, phy->advertising);
2917 config.interface = PHY_INTERFACE_MODE_NA;
2918 config.speed = SPEED_UNKNOWN;
2919 config.duplex = DUPLEX_UNKNOWN;
2920 config.pause = MLO_PAUSE_AN;
2921 config.an_enabled = pl->link_config.an_enabled;
2923 /* Ignore errors if we're expecting a PHY to attach later */
2924 ret = phylink_validate(pl, support, &config);
2926 phylink_err(pl, "validation with support %*pb failed: %pe\n",
2927 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
2932 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2933 if (iface == PHY_INTERFACE_MODE_NA) {
2935 "selection of interface failed, advertisement %*pb\n",
2936 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2940 config.interface = iface;
2941 linkmode_copy(support1, support);
2942 ret = phylink_validate(pl, support1, &config);
2945 "validation of %s/%s with support %*pb failed: %pe\n",
2946 phylink_an_mode_str(mode),
2947 phy_modes(config.interface),
2948 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
2953 pl->link_port = pl->sfp_port;
2955 phylink_sfp_set_config(pl, mode, support, &config);
2960 static int phylink_sfp_config_optical(struct phylink *pl)
2962 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2963 DECLARE_PHY_INTERFACE_MASK(interfaces);
2964 struct phylink_link_state config;
2965 phy_interface_t interface;
2968 phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n",
2969 (int)PHY_INTERFACE_MODE_MAX,
2970 pl->config->supported_interfaces,
2971 (int)PHY_INTERFACE_MODE_MAX,
2972 pl->sfp_interfaces);
2974 /* Find the union of the supported interfaces by the PCS/MAC and
2977 phy_interface_and(interfaces, pl->config->supported_interfaces,
2978 pl->sfp_interfaces);
2979 if (phy_interface_empty(interfaces)) {
2980 phylink_err(pl, "unsupported SFP module: no common interface modes\n");
2984 memset(&config, 0, sizeof(config));
2985 linkmode_copy(support, pl->sfp_support);
2986 linkmode_copy(config.advertising, pl->sfp_support);
2987 config.speed = SPEED_UNKNOWN;
2988 config.duplex = DUPLEX_UNKNOWN;
2989 config.pause = MLO_PAUSE_AN;
2990 config.an_enabled = true;
2992 /* For all the interfaces that are supported, reduce the sfp_support
2993 * mask to only those link modes that can be supported.
2995 ret = phylink_validate_mask(pl, pl->sfp_support, &config, interfaces);
2997 phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
2998 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
3002 interface = phylink_choose_sfp_interface(pl, interfaces);
3003 if (interface == PHY_INTERFACE_MODE_NA) {
3004 phylink_err(pl, "failed to select SFP interface\n");
3008 phylink_dbg(pl, "optical SFP: chosen %s interface\n",
3009 phy_modes(interface));
3011 config.interface = interface;
3013 /* Ignore errors if we're expecting a PHY to attach later */
3014 ret = phylink_validate(pl, support, &config);
3016 phylink_err(pl, "validation with support %*pb failed: %pe\n",
3017 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3022 pl->link_port = pl->sfp_port;
3024 phylink_sfp_set_config(pl, MLO_AN_INBAND, pl->sfp_support, &config);
3029 static int phylink_sfp_module_insert(void *upstream,
3030 const struct sfp_eeprom_id *id)
3032 struct phylink *pl = upstream;
3036 linkmode_zero(pl->sfp_support);
3037 phy_interface_zero(pl->sfp_interfaces);
3038 sfp_parse_support(pl->sfp_bus, id, pl->sfp_support, pl->sfp_interfaces);
3039 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, pl->sfp_support);
3041 /* If this module may have a PHY connecting later, defer until later */
3042 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
3043 if (pl->sfp_may_have_phy)
3046 return phylink_sfp_config_optical(pl);
3049 static int phylink_sfp_module_start(void *upstream)
3051 struct phylink *pl = upstream;
3053 /* If this SFP module has a PHY, start the PHY now. */
3055 phy_start(pl->phydev);
3059 /* If the module may have a PHY but we didn't detect one we
3060 * need to configure the MAC here.
3062 if (!pl->sfp_may_have_phy)
3065 return phylink_sfp_config_optical(pl);
3068 static void phylink_sfp_module_stop(void *upstream)
3070 struct phylink *pl = upstream;
3072 /* If this SFP module has a PHY, stop it. */
3074 phy_stop(pl->phydev);
3077 static void phylink_sfp_link_down(void *upstream)
3079 struct phylink *pl = upstream;
3083 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
3086 static void phylink_sfp_link_up(void *upstream)
3088 struct phylink *pl = upstream;
3092 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
3095 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
3096 * or 802.3z control word, so inband will not work.
3098 static bool phylink_phy_no_inband(struct phy_device *phy)
3100 return phy->is_c45 &&
3101 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
3104 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
3106 struct phylink *pl = upstream;
3107 phy_interface_t interface;
3112 * This is the new way of dealing with flow control for PHYs,
3113 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
3114 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
3115 * using our validate call to the MAC, we rely upon the MAC
3116 * clearing the bits from both supported and advertising fields.
3118 phy_support_asym_pause(phy);
3120 if (phylink_phy_no_inband(phy))
3123 mode = MLO_AN_INBAND;
3125 /* Set the PHY's host supported interfaces */
3126 phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
3127 pl->config->supported_interfaces);
3129 /* Do the initial configuration */
3130 ret = phylink_sfp_config_phy(pl, mode, phy);
3134 interface = pl->link_config.interface;
3135 ret = phylink_attach_phy(pl, phy, interface);
3139 ret = phylink_bringup_phy(pl, phy, interface);
3146 static void phylink_sfp_disconnect_phy(void *upstream)
3148 phylink_disconnect_phy(upstream);
3151 static const struct sfp_upstream_ops sfp_phylink_ops = {
3152 .attach = phylink_sfp_attach,
3153 .detach = phylink_sfp_detach,
3154 .module_insert = phylink_sfp_module_insert,
3155 .module_start = phylink_sfp_module_start,
3156 .module_stop = phylink_sfp_module_stop,
3157 .link_up = phylink_sfp_link_up,
3158 .link_down = phylink_sfp_link_down,
3159 .connect_phy = phylink_sfp_connect_phy,
3160 .disconnect_phy = phylink_sfp_disconnect_phy,
3163 /* Helpers for MAC drivers */
3165 static void phylink_decode_c37_word(struct phylink_link_state *state,
3166 uint16_t config_reg, int speed)
3168 bool tx_pause, rx_pause;
3171 if (speed == SPEED_2500)
3172 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
3174 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
3176 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
3178 if (linkmode_test_bit(fd_bit, state->advertising) &&
3179 linkmode_test_bit(fd_bit, state->lp_advertising)) {
3180 state->speed = speed;
3181 state->duplex = DUPLEX_FULL;
3183 /* negotiation failure */
3184 state->link = false;
3187 linkmode_resolve_pause(state->advertising, state->lp_advertising,
3188 &tx_pause, &rx_pause);
3191 state->pause |= MLO_PAUSE_TX;
3193 state->pause |= MLO_PAUSE_RX;
3196 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
3197 uint16_t config_reg)
3199 if (!(config_reg & LPA_SGMII_LINK)) {
3200 state->link = false;
3204 switch (config_reg & LPA_SGMII_SPD_MASK) {
3206 state->speed = SPEED_10;
3209 state->speed = SPEED_100;
3211 case LPA_SGMII_1000:
3212 state->speed = SPEED_1000;
3215 state->link = false;
3218 if (config_reg & LPA_SGMII_FULL_DUPLEX)
3219 state->duplex = DUPLEX_FULL;
3221 state->duplex = DUPLEX_HALF;
3225 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
3226 * @state: a pointer to a struct phylink_link_state.
3227 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
3229 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
3230 * code word. Decode the USXGMII code word and populate the corresponding fields
3231 * (speed, duplex) into the phylink_link_state structure.
3233 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
3236 switch (lpa & MDIO_USXGMII_SPD_MASK) {
3237 case MDIO_USXGMII_10:
3238 state->speed = SPEED_10;
3240 case MDIO_USXGMII_100:
3241 state->speed = SPEED_100;
3243 case MDIO_USXGMII_1000:
3244 state->speed = SPEED_1000;
3246 case MDIO_USXGMII_2500:
3247 state->speed = SPEED_2500;
3249 case MDIO_USXGMII_5000:
3250 state->speed = SPEED_5000;
3252 case MDIO_USXGMII_10G:
3253 state->speed = SPEED_10000;
3256 state->link = false;
3260 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
3261 state->duplex = DUPLEX_FULL;
3263 state->duplex = DUPLEX_HALF;
3265 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
3268 * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
3269 * @state: a pointer to a &struct phylink_link_state.
3270 * @bmsr: The value of the %MII_BMSR register
3271 * @lpa: The value of the %MII_LPA register
3273 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3274 * clause 37 negotiation and/or SGMII control.
3276 * Parse the Clause 37 or Cisco SGMII link partner negotiation word into
3277 * the phylink @state structure. This is suitable to be used for implementing
3278 * the mac_pcs_get_state() member of the struct phylink_mac_ops structure if
3279 * accessing @bmsr and @lpa cannot be done with MDIO directly.
3281 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
3284 state->link = !!(bmsr & BMSR_LSTATUS);
3285 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
3286 /* If there is no link or autonegotiation is disabled, the LP advertisement
3287 * data is not meaningful, so don't go any further.
3289 if (!state->link || !state->an_enabled)
3292 switch (state->interface) {
3293 case PHY_INTERFACE_MODE_1000BASEX:
3294 phylink_decode_c37_word(state, lpa, SPEED_1000);
3297 case PHY_INTERFACE_MODE_2500BASEX:
3298 phylink_decode_c37_word(state, lpa, SPEED_2500);
3301 case PHY_INTERFACE_MODE_SGMII:
3302 case PHY_INTERFACE_MODE_QSGMII:
3303 case PHY_INTERFACE_MODE_QUSGMII:
3304 phylink_decode_sgmii_word(state, lpa);
3308 state->link = false;
3312 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state);
3315 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
3316 * @pcs: a pointer to a &struct mdio_device.
3317 * @state: a pointer to a &struct phylink_link_state.
3319 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3320 * clause 37 negotiation and/or SGMII control.
3322 * Read the MAC PCS state from the MII device configured in @config and
3323 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
3324 * the phylink @state structure. This is suitable to be directly plugged
3325 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
3328 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
3329 struct phylink_link_state *state)
3333 bmsr = mdiodev_read(pcs, MII_BMSR);
3334 lpa = mdiodev_read(pcs, MII_LPA);
3335 if (bmsr < 0 || lpa < 0) {
3336 state->link = false;
3340 phylink_mii_c22_pcs_decode_state(state, bmsr, lpa);
3342 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
3345 * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
3347 * @interface: the PHY interface mode being configured
3348 * @advertising: the ethtool advertisement mask
3350 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3351 * clause 37 negotiation and/or SGMII control.
3353 * Encode the clause 37 PCS advertisement as specified by @interface and
3356 * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
3358 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
3359 const unsigned long *advertising)
3363 switch (interface) {
3364 case PHY_INTERFACE_MODE_1000BASEX:
3365 case PHY_INTERFACE_MODE_2500BASEX:
3366 adv = ADVERTISE_1000XFULL;
3367 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3369 adv |= ADVERTISE_1000XPAUSE;
3370 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3372 adv |= ADVERTISE_1000XPSE_ASYM;
3374 case PHY_INTERFACE_MODE_SGMII:
3375 case PHY_INTERFACE_MODE_QSGMII:
3378 /* Nothing to do for other modes */
3382 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement);
3385 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
3386 * @pcs: a pointer to a &struct mdio_device.
3387 * @mode: link autonegotiation mode
3388 * @interface: the PHY interface mode being configured
3389 * @advertising: the ethtool advertisement mask
3391 * Configure a Clause 22 PCS PHY with the appropriate negotiation
3392 * parameters for the @mode, @interface and @advertising parameters.
3393 * Returns negative error number on failure, zero if the advertisement
3394 * has not changed, or positive if there is a change.
3396 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
3397 phy_interface_t interface,
3398 const unsigned long *advertising)
3404 adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
3406 ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
3407 MII_ADVERTISE, 0xffff, adv);
3413 /* Ensure ISOLATE bit is disabled */
3414 if (mode == MLO_AN_INBAND &&
3415 (interface == PHY_INTERFACE_MODE_SGMII ||
3416 interface == PHY_INTERFACE_MODE_QSGMII ||
3417 linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, advertising)))
3418 bmcr = BMCR_ANENABLE;
3422 ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
3428 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
3431 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
3432 * @pcs: a pointer to a &struct mdio_device.
3434 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3435 * clause 37 negotiation.
3437 * Restart the clause 37 negotiation with the link partner. This is
3438 * suitable to be directly plugged into the mac_pcs_get_state() member
3439 * of the struct phylink_mac_ops structure.
3441 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
3443 int val = mdiodev_read(pcs, MII_BMCR);
3446 val |= BMCR_ANRESTART;
3448 mdiodev_write(pcs, MII_BMCR, val);
3451 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
3453 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
3454 struct phylink_link_state *state)
3456 struct mii_bus *bus = pcs->bus;
3457 int addr = pcs->addr;
3460 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
3462 state->link = false;
3466 state->link = !!(stat & MDIO_STAT1_LSTATUS);
3470 switch (state->interface) {
3471 case PHY_INTERFACE_MODE_10GBASER:
3472 state->speed = SPEED_10000;
3473 state->duplex = DUPLEX_FULL;
3480 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
3482 static int __init phylink_init(void)
3484 for (int i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); ++i)
3485 __set_bit(phylink_sfp_interface_preference[i],
3486 phylink_sfp_interfaces);
3491 module_init(phylink_init);
3493 MODULE_LICENSE("GPL v2");