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,
44 * struct phylink - internal data type for phylink
48 struct net_device *netdev;
49 const struct phylink_mac_ops *mac_ops;
50 struct phylink_config *config;
51 struct phylink_pcs *pcs;
53 unsigned int old_link_state:1;
55 unsigned long phylink_disable_state; /* bitmask of disables */
56 struct phy_device *phydev;
57 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
58 u8 cfg_link_an_mode; /* MLO_AN_xxx */
60 u8 link_port; /* The current non-phy ethtool port */
61 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
63 /* The link configuration settings */
64 struct phylink_link_state link_config;
66 /* The current settings */
67 phy_interface_t cur_interface;
69 struct gpio_desc *link_gpio;
70 unsigned int link_irq;
71 struct timer_list link_poll;
72 void (*get_fixed_state)(struct net_device *dev,
73 struct phylink_link_state *s);
75 struct mutex state_mutex;
76 struct phylink_link_state phy_state;
77 struct work_struct resolve;
78 unsigned int pcs_neg_mode;
79 unsigned int pcs_state;
81 bool mac_link_dropped;
82 bool using_mac_select_pcs;
84 struct sfp_bus *sfp_bus;
85 bool sfp_may_have_phy;
86 DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
87 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
91 #define phylink_printk(level, pl, fmt, ...) \
93 if ((pl)->config->type == PHYLINK_NETDEV) \
94 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
95 else if ((pl)->config->type == PHYLINK_DEV) \
96 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
99 #define phylink_err(pl, fmt, ...) \
100 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
101 #define phylink_warn(pl, fmt, ...) \
102 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
103 #define phylink_info(pl, fmt, ...) \
104 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
105 #if defined(CONFIG_DYNAMIC_DEBUG)
106 #define phylink_dbg(pl, fmt, ...) \
108 if ((pl)->config->type == PHYLINK_NETDEV) \
109 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
110 else if ((pl)->config->type == PHYLINK_DEV) \
111 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
114 #define phylink_dbg(pl, fmt, ...) \
115 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
117 #define phylink_dbg(pl, fmt, ...) \
120 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
124 static const phy_interface_t phylink_sfp_interface_preference[] = {
125 PHY_INTERFACE_MODE_25GBASER,
126 PHY_INTERFACE_MODE_USXGMII,
127 PHY_INTERFACE_MODE_10GBASER,
128 PHY_INTERFACE_MODE_5GBASER,
129 PHY_INTERFACE_MODE_2500BASEX,
130 PHY_INTERFACE_MODE_SGMII,
131 PHY_INTERFACE_MODE_1000BASEX,
132 PHY_INTERFACE_MODE_100BASEX,
135 static DECLARE_PHY_INTERFACE_MASK(phylink_sfp_interfaces);
138 * phylink_set_port_modes() - set the port type modes in the ethtool mask
139 * @mask: ethtool link mode mask
141 * Sets all the port type modes in the ethtool mask. MAC drivers should
142 * use this in their 'validate' callback.
144 void phylink_set_port_modes(unsigned long *mask)
146 phylink_set(mask, TP);
147 phylink_set(mask, AUI);
148 phylink_set(mask, MII);
149 phylink_set(mask, FIBRE);
150 phylink_set(mask, BNC);
151 phylink_set(mask, Backplane);
153 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
155 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
157 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
159 phylink_set_port_modes(tmp);
160 phylink_set(tmp, Autoneg);
161 phylink_set(tmp, Pause);
162 phylink_set(tmp, Asym_Pause);
164 return linkmode_subset(linkmode, tmp);
167 static const char *phylink_an_mode_str(unsigned int mode)
169 static const char *modestr[] = {
170 [MLO_AN_PHY] = "phy",
171 [MLO_AN_FIXED] = "fixed",
172 [MLO_AN_INBAND] = "inband",
175 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
178 static unsigned int phylink_interface_signal_rate(phy_interface_t interface)
181 case PHY_INTERFACE_MODE_SGMII:
182 case PHY_INTERFACE_MODE_1000BASEX: /* 1.25Mbd */
184 case PHY_INTERFACE_MODE_2500BASEX: /* 3.125Mbd */
186 case PHY_INTERFACE_MODE_5GBASER: /* 5.15625Mbd */
188 case PHY_INTERFACE_MODE_10GBASER: /* 10.3125Mbd */
196 * phylink_interface_max_speed() - get the maximum speed of a phy interface
197 * @interface: phy interface mode defined by &typedef phy_interface_t
199 * Determine the maximum speed of a phy interface. This is intended to help
200 * determine the correct speed to pass to the MAC when the phy is performing
203 * Return: The maximum speed of @interface
205 static int phylink_interface_max_speed(phy_interface_t interface)
208 case PHY_INTERFACE_MODE_100BASEX:
209 case PHY_INTERFACE_MODE_REVRMII:
210 case PHY_INTERFACE_MODE_RMII:
211 case PHY_INTERFACE_MODE_SMII:
212 case PHY_INTERFACE_MODE_REVMII:
213 case PHY_INTERFACE_MODE_MII:
216 case PHY_INTERFACE_MODE_TBI:
217 case PHY_INTERFACE_MODE_MOCA:
218 case PHY_INTERFACE_MODE_RTBI:
219 case PHY_INTERFACE_MODE_1000BASEX:
220 case PHY_INTERFACE_MODE_1000BASEKX:
221 case PHY_INTERFACE_MODE_TRGMII:
222 case PHY_INTERFACE_MODE_RGMII_TXID:
223 case PHY_INTERFACE_MODE_RGMII_RXID:
224 case PHY_INTERFACE_MODE_RGMII_ID:
225 case PHY_INTERFACE_MODE_RGMII:
226 case PHY_INTERFACE_MODE_PSGMII:
227 case PHY_INTERFACE_MODE_QSGMII:
228 case PHY_INTERFACE_MODE_QUSGMII:
229 case PHY_INTERFACE_MODE_SGMII:
230 case PHY_INTERFACE_MODE_GMII:
233 case PHY_INTERFACE_MODE_2500BASEX:
234 case PHY_INTERFACE_MODE_10G_QXGMII:
237 case PHY_INTERFACE_MODE_5GBASER:
240 case PHY_INTERFACE_MODE_XGMII:
241 case PHY_INTERFACE_MODE_RXAUI:
242 case PHY_INTERFACE_MODE_XAUI:
243 case PHY_INTERFACE_MODE_10GBASER:
244 case PHY_INTERFACE_MODE_10GKR:
245 case PHY_INTERFACE_MODE_USXGMII:
248 case PHY_INTERFACE_MODE_25GBASER:
251 case PHY_INTERFACE_MODE_XLGMII:
254 case PHY_INTERFACE_MODE_INTERNAL:
255 case PHY_INTERFACE_MODE_NA:
256 case PHY_INTERFACE_MODE_MAX:
257 /* No idea! Garbage in, unknown out */
258 return SPEED_UNKNOWN;
261 /* If we get here, someone forgot to add an interface mode above */
263 return SPEED_UNKNOWN;
267 * phylink_caps_to_linkmodes() - Convert capabilities to ethtool link modes
268 * @linkmodes: ethtool linkmode mask (must be already initialised)
269 * @caps: bitmask of MAC capabilities
271 * Set all possible pause, speed and duplex linkmodes in @linkmodes that are
272 * supported by the @caps. @linkmodes must have been initialised previously.
274 static void phylink_caps_to_linkmodes(unsigned long *linkmodes,
277 if (caps & MAC_SYM_PAUSE)
278 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, linkmodes);
280 if (caps & MAC_ASYM_PAUSE)
281 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, linkmodes);
283 if (caps & MAC_10HD) {
284 __set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, linkmodes);
285 __set_bit(ETHTOOL_LINK_MODE_10baseT1S_Half_BIT, linkmodes);
286 __set_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT, linkmodes);
289 if (caps & MAC_10FD) {
290 __set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, linkmodes);
291 __set_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, linkmodes);
292 __set_bit(ETHTOOL_LINK_MODE_10baseT1S_Full_BIT, linkmodes);
295 if (caps & MAC_100HD) {
296 __set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, linkmodes);
297 __set_bit(ETHTOOL_LINK_MODE_100baseFX_Half_BIT, linkmodes);
300 if (caps & MAC_100FD) {
301 __set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, linkmodes);
302 __set_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT, linkmodes);
303 __set_bit(ETHTOOL_LINK_MODE_100baseFX_Full_BIT, linkmodes);
306 if (caps & MAC_1000HD)
307 __set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, linkmodes);
309 if (caps & MAC_1000FD) {
310 __set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, linkmodes);
311 __set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, linkmodes);
312 __set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, linkmodes);
313 __set_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT, linkmodes);
316 if (caps & MAC_2500FD) {
317 __set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, linkmodes);
318 __set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, linkmodes);
321 if (caps & MAC_5000FD)
322 __set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, linkmodes);
324 if (caps & MAC_10000FD) {
325 __set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, linkmodes);
326 __set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, linkmodes);
327 __set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, linkmodes);
328 __set_bit(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT, linkmodes);
329 __set_bit(ETHTOOL_LINK_MODE_10000baseCR_Full_BIT, linkmodes);
330 __set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, linkmodes);
331 __set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, linkmodes);
332 __set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT, linkmodes);
333 __set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, linkmodes);
336 if (caps & MAC_25000FD) {
337 __set_bit(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, linkmodes);
338 __set_bit(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, linkmodes);
339 __set_bit(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, linkmodes);
342 if (caps & MAC_40000FD) {
343 __set_bit(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, linkmodes);
344 __set_bit(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, linkmodes);
345 __set_bit(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, linkmodes);
346 __set_bit(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, linkmodes);
349 if (caps & MAC_50000FD) {
350 __set_bit(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT, linkmodes);
351 __set_bit(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT, linkmodes);
352 __set_bit(ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT, linkmodes);
353 __set_bit(ETHTOOL_LINK_MODE_50000baseKR_Full_BIT, linkmodes);
354 __set_bit(ETHTOOL_LINK_MODE_50000baseSR_Full_BIT, linkmodes);
355 __set_bit(ETHTOOL_LINK_MODE_50000baseCR_Full_BIT, linkmodes);
356 __set_bit(ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
358 __set_bit(ETHTOOL_LINK_MODE_50000baseDR_Full_BIT, linkmodes);
361 if (caps & MAC_56000FD) {
362 __set_bit(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT, linkmodes);
363 __set_bit(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT, linkmodes);
364 __set_bit(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT, linkmodes);
365 __set_bit(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT, linkmodes);
368 if (caps & MAC_100000FD) {
369 __set_bit(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, linkmodes);
370 __set_bit(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT, linkmodes);
371 __set_bit(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, linkmodes);
372 __set_bit(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
374 __set_bit(ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT, linkmodes);
375 __set_bit(ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT, linkmodes);
376 __set_bit(ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT, linkmodes);
377 __set_bit(ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT,
379 __set_bit(ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT, linkmodes);
380 __set_bit(ETHTOOL_LINK_MODE_100000baseKR_Full_BIT, linkmodes);
381 __set_bit(ETHTOOL_LINK_MODE_100000baseSR_Full_BIT, linkmodes);
382 __set_bit(ETHTOOL_LINK_MODE_100000baseLR_ER_FR_Full_BIT,
384 __set_bit(ETHTOOL_LINK_MODE_100000baseCR_Full_BIT, linkmodes);
385 __set_bit(ETHTOOL_LINK_MODE_100000baseDR_Full_BIT, linkmodes);
388 if (caps & MAC_200000FD) {
389 __set_bit(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT, linkmodes);
390 __set_bit(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT, linkmodes);
391 __set_bit(ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
393 __set_bit(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT, linkmodes);
394 __set_bit(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT, linkmodes);
395 __set_bit(ETHTOOL_LINK_MODE_200000baseKR2_Full_BIT, linkmodes);
396 __set_bit(ETHTOOL_LINK_MODE_200000baseSR2_Full_BIT, linkmodes);
397 __set_bit(ETHTOOL_LINK_MODE_200000baseLR2_ER2_FR2_Full_BIT,
399 __set_bit(ETHTOOL_LINK_MODE_200000baseDR2_Full_BIT, linkmodes);
400 __set_bit(ETHTOOL_LINK_MODE_200000baseCR2_Full_BIT, linkmodes);
403 if (caps & MAC_400000FD) {
404 __set_bit(ETHTOOL_LINK_MODE_400000baseKR8_Full_BIT, linkmodes);
405 __set_bit(ETHTOOL_LINK_MODE_400000baseSR8_Full_BIT, linkmodes);
406 __set_bit(ETHTOOL_LINK_MODE_400000baseLR8_ER8_FR8_Full_BIT,
408 __set_bit(ETHTOOL_LINK_MODE_400000baseDR8_Full_BIT, linkmodes);
409 __set_bit(ETHTOOL_LINK_MODE_400000baseCR8_Full_BIT, linkmodes);
410 __set_bit(ETHTOOL_LINK_MODE_400000baseKR4_Full_BIT, linkmodes);
411 __set_bit(ETHTOOL_LINK_MODE_400000baseSR4_Full_BIT, linkmodes);
412 __set_bit(ETHTOOL_LINK_MODE_400000baseLR4_ER4_FR4_Full_BIT,
414 __set_bit(ETHTOOL_LINK_MODE_400000baseDR4_Full_BIT, linkmodes);
415 __set_bit(ETHTOOL_LINK_MODE_400000baseCR4_Full_BIT, linkmodes);
423 } phylink_caps_params[] = {
424 { MAC_400000FD, SPEED_400000, DUPLEX_FULL },
425 { MAC_200000FD, SPEED_200000, DUPLEX_FULL },
426 { MAC_100000FD, SPEED_100000, DUPLEX_FULL },
427 { MAC_56000FD, SPEED_56000, DUPLEX_FULL },
428 { MAC_50000FD, SPEED_50000, DUPLEX_FULL },
429 { MAC_40000FD, SPEED_40000, DUPLEX_FULL },
430 { MAC_25000FD, SPEED_25000, DUPLEX_FULL },
431 { MAC_20000FD, SPEED_20000, DUPLEX_FULL },
432 { MAC_10000FD, SPEED_10000, DUPLEX_FULL },
433 { MAC_5000FD, SPEED_5000, DUPLEX_FULL },
434 { MAC_2500FD, SPEED_2500, DUPLEX_FULL },
435 { MAC_1000FD, SPEED_1000, DUPLEX_FULL },
436 { MAC_1000HD, SPEED_1000, DUPLEX_HALF },
437 { MAC_100FD, SPEED_100, DUPLEX_FULL },
438 { MAC_100HD, SPEED_100, DUPLEX_HALF },
439 { MAC_10FD, SPEED_10, DUPLEX_FULL },
440 { MAC_10HD, SPEED_10, DUPLEX_HALF },
444 * phylink_limit_mac_speed - limit the phylink_config to a maximum speed
445 * @config: pointer to a &struct phylink_config
446 * @max_speed: maximum speed
448 * Mask off MAC capabilities for speeds higher than the @max_speed parameter.
449 * Any further motifications of config.mac_capabilities will override this.
451 void phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed)
455 for (i = 0; i < ARRAY_SIZE(phylink_caps_params) &&
456 phylink_caps_params[i].speed > max_speed; i++)
457 config->mac_capabilities &= ~phylink_caps_params[i].mask;
459 EXPORT_SYMBOL_GPL(phylink_limit_mac_speed);
462 * phylink_cap_from_speed_duplex - Get mac capability from speed/duplex
463 * @speed: the speed to search for
464 * @duplex: the duplex to search for
466 * Find the mac capability for a given speed and duplex.
468 * Return: A mask with the mac capability patching @speed and @duplex, or 0 if
469 * there were no matches.
471 static unsigned long phylink_cap_from_speed_duplex(int speed,
476 for (i = 0; i < ARRAY_SIZE(phylink_caps_params); i++) {
477 if (speed == phylink_caps_params[i].speed &&
478 duplex == phylink_caps_params[i].duplex)
479 return phylink_caps_params[i].mask;
486 * phylink_get_capabilities() - get capabilities for a given MAC
487 * @interface: phy interface mode defined by &typedef phy_interface_t
488 * @mac_capabilities: bitmask of MAC capabilities
489 * @rate_matching: type of rate matching being performed
491 * Get the MAC capabilities that are supported by the @interface mode and
494 static unsigned long phylink_get_capabilities(phy_interface_t interface,
495 unsigned long mac_capabilities,
498 int max_speed = phylink_interface_max_speed(interface);
499 unsigned long caps = MAC_SYM_PAUSE | MAC_ASYM_PAUSE;
500 unsigned long matched_caps = 0;
503 case PHY_INTERFACE_MODE_USXGMII:
504 caps |= MAC_10000FD | MAC_5000FD;
507 case PHY_INTERFACE_MODE_10G_QXGMII:
511 case PHY_INTERFACE_MODE_RGMII_TXID:
512 case PHY_INTERFACE_MODE_RGMII_RXID:
513 case PHY_INTERFACE_MODE_RGMII_ID:
514 case PHY_INTERFACE_MODE_RGMII:
515 case PHY_INTERFACE_MODE_PSGMII:
516 case PHY_INTERFACE_MODE_QSGMII:
517 case PHY_INTERFACE_MODE_QUSGMII:
518 case PHY_INTERFACE_MODE_SGMII:
519 case PHY_INTERFACE_MODE_GMII:
520 caps |= MAC_1000HD | MAC_1000FD;
523 case PHY_INTERFACE_MODE_REVRMII:
524 case PHY_INTERFACE_MODE_RMII:
525 case PHY_INTERFACE_MODE_SMII:
526 case PHY_INTERFACE_MODE_REVMII:
527 case PHY_INTERFACE_MODE_MII:
528 caps |= MAC_10HD | MAC_10FD;
531 case PHY_INTERFACE_MODE_100BASEX:
532 caps |= MAC_100HD | MAC_100FD;
535 case PHY_INTERFACE_MODE_TBI:
536 case PHY_INTERFACE_MODE_MOCA:
537 case PHY_INTERFACE_MODE_RTBI:
538 case PHY_INTERFACE_MODE_1000BASEX:
541 case PHY_INTERFACE_MODE_1000BASEKX:
542 case PHY_INTERFACE_MODE_TRGMII:
546 case PHY_INTERFACE_MODE_2500BASEX:
550 case PHY_INTERFACE_MODE_5GBASER:
554 case PHY_INTERFACE_MODE_XGMII:
555 case PHY_INTERFACE_MODE_RXAUI:
556 case PHY_INTERFACE_MODE_XAUI:
557 case PHY_INTERFACE_MODE_10GBASER:
558 case PHY_INTERFACE_MODE_10GKR:
562 case PHY_INTERFACE_MODE_25GBASER:
566 case PHY_INTERFACE_MODE_XLGMII:
570 case PHY_INTERFACE_MODE_INTERNAL:
574 case PHY_INTERFACE_MODE_NA:
575 case PHY_INTERFACE_MODE_MAX:
579 switch (rate_matching) {
580 case RATE_MATCH_OPEN_LOOP:
583 case RATE_MATCH_NONE:
586 case RATE_MATCH_PAUSE: {
587 /* The MAC must support asymmetric pause towards the local
588 * device for this. We could allow just symmetric pause, but
589 * then we might have to renegotiate if the link partner
590 * doesn't support pause. This is because there's no way to
591 * accept pause frames without transmitting them if we only
592 * support symmetric pause.
594 if (!(mac_capabilities & MAC_SYM_PAUSE) ||
595 !(mac_capabilities & MAC_ASYM_PAUSE))
598 /* We can't adapt if the MAC doesn't support the interface's
599 * max speed at full duplex.
601 if (mac_capabilities &
602 phylink_cap_from_speed_duplex(max_speed, DUPLEX_FULL)) {
603 /* Although a duplex-matching phy might exist, we
604 * conservatively remove these modes because the MAC
605 * will not be aware of the half-duplex nature of the
608 matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
609 matched_caps &= ~(MAC_1000HD | MAC_100HD | MAC_10HD);
614 /* The MAC must support half duplex at the interface's max
617 if (mac_capabilities &
618 phylink_cap_from_speed_duplex(max_speed, DUPLEX_HALF)) {
619 matched_caps = GENMASK(__fls(caps), __fls(MAC_10HD));
620 matched_caps &= mac_capabilities;
625 return (caps & mac_capabilities) | matched_caps;
629 * phylink_validate_mask_caps() - Restrict link modes based on caps
630 * @supported: ethtool bitmask for supported link modes.
631 * @state: pointer to a &struct phylink_link_state.
632 * @mac_capabilities: bitmask of MAC capabilities
634 * Calculate the supported link modes based on @mac_capabilities, and restrict
635 * @supported and @state based on that. Use this function if your capabiliies
636 * aren't constant, such as if they vary depending on the interface.
638 static void phylink_validate_mask_caps(unsigned long *supported,
639 struct phylink_link_state *state,
640 unsigned long mac_capabilities)
642 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
645 phylink_set_port_modes(mask);
646 phylink_set(mask, Autoneg);
647 caps = phylink_get_capabilities(state->interface, mac_capabilities,
648 state->rate_matching);
649 phylink_caps_to_linkmodes(mask, caps);
651 linkmode_and(supported, supported, mask);
652 linkmode_and(state->advertising, state->advertising, mask);
655 static int phylink_validate_mac_and_pcs(struct phylink *pl,
656 unsigned long *supported,
657 struct phylink_link_state *state)
659 unsigned long capabilities;
660 struct phylink_pcs *pcs;
663 /* Get the PCS for this interface mode */
664 if (pl->using_mac_select_pcs) {
665 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
673 /* The PCS, if present, must be setup before phylink_create()
674 * has been called. If the ops is not initialised, print an
675 * error and backtrace rather than oopsing the kernel.
678 phylink_err(pl, "interface %s: uninitialised PCS\n",
679 phy_modes(state->interface));
684 /* Validate the link parameters with the PCS */
685 if (pcs->ops->pcs_validate) {
686 ret = pcs->ops->pcs_validate(pcs, supported, state);
687 if (ret < 0 || phylink_is_empty_linkmode(supported))
690 /* Ensure the advertising mask is a subset of the
693 linkmode_and(state->advertising, state->advertising,
698 /* Then validate the link parameters with the MAC */
699 if (pl->mac_ops->mac_get_caps)
700 capabilities = pl->mac_ops->mac_get_caps(pl->config,
703 capabilities = pl->config->mac_capabilities;
705 phylink_validate_mask_caps(supported, state, capabilities);
707 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
710 static void phylink_validate_one(struct phylink *pl, struct phy_device *phy,
711 const unsigned long *supported,
712 const struct phylink_link_state *state,
713 phy_interface_t interface,
714 unsigned long *accum_supported,
715 unsigned long *accum_advertising)
717 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp_supported);
718 struct phylink_link_state tmp_state;
720 linkmode_copy(tmp_supported, supported);
723 tmp_state.interface = interface;
726 tmp_state.rate_matching = phy_get_rate_matching(phy, interface);
728 if (!phylink_validate_mac_and_pcs(pl, tmp_supported, &tmp_state)) {
729 phylink_dbg(pl, " interface %u (%s) rate match %s supports %*pbl\n",
730 interface, phy_modes(interface),
731 phy_rate_matching_to_str(tmp_state.rate_matching),
732 __ETHTOOL_LINK_MODE_MASK_NBITS, tmp_supported);
734 linkmode_or(accum_supported, accum_supported, tmp_supported);
735 linkmode_or(accum_advertising, accum_advertising,
736 tmp_state.advertising);
740 static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy,
741 unsigned long *supported,
742 struct phylink_link_state *state,
743 const unsigned long *interfaces)
745 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
746 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
749 for_each_set_bit(interface, interfaces, PHY_INTERFACE_MODE_MAX)
750 phylink_validate_one(pl, phy, supported, state, interface,
753 linkmode_copy(supported, all_s);
754 linkmode_copy(state->advertising, all_adv);
756 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
759 static int phylink_validate(struct phylink *pl, unsigned long *supported,
760 struct phylink_link_state *state)
762 const unsigned long *interfaces = pl->config->supported_interfaces;
764 if (state->interface == PHY_INTERFACE_MODE_NA)
765 return phylink_validate_mask(pl, NULL, supported, state,
768 if (!test_bit(state->interface, interfaces))
771 return phylink_validate_mac_and_pcs(pl, supported, state);
774 static int phylink_parse_fixedlink(struct phylink *pl,
775 const struct fwnode_handle *fwnode)
777 struct fwnode_handle *fixed_node;
778 bool pause, asym_pause, autoneg;
779 const struct phy_setting *s;
780 struct gpio_desc *desc;
784 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
786 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
788 pl->link_config.speed = speed;
789 pl->link_config.duplex = DUPLEX_HALF;
791 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
792 pl->link_config.duplex = DUPLEX_FULL;
794 /* We treat the "pause" and "asym-pause" terminology as
795 * defining the link partner's ability.
797 if (fwnode_property_read_bool(fixed_node, "pause"))
798 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
799 pl->link_config.lp_advertising);
800 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
801 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
802 pl->link_config.lp_advertising);
805 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
809 pl->link_gpio = desc;
810 else if (desc == ERR_PTR(-EPROBE_DEFER))
813 fwnode_handle_put(fixed_node);
820 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
822 if (ret != ARRAY_SIZE(prop)) {
823 phylink_err(pl, "broken fixed-link?\n");
827 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
828 prop, ARRAY_SIZE(prop));
830 pl->link_config.duplex = prop[1] ?
831 DUPLEX_FULL : DUPLEX_HALF;
832 pl->link_config.speed = prop[2];
834 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
835 pl->link_config.lp_advertising);
837 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
838 pl->link_config.lp_advertising);
842 if (pl->link_config.speed > SPEED_1000 &&
843 pl->link_config.duplex != DUPLEX_FULL)
844 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
845 pl->link_config.speed);
847 linkmode_fill(pl->supported);
848 linkmode_copy(pl->link_config.advertising, pl->supported);
849 phylink_validate(pl, pl->supported, &pl->link_config);
851 pause = phylink_test(pl->supported, Pause);
852 asym_pause = phylink_test(pl->supported, Asym_Pause);
853 autoneg = phylink_test(pl->supported, Autoneg);
854 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
855 pl->supported, true);
856 linkmode_zero(pl->supported);
857 phylink_set(pl->supported, MII);
860 phylink_set(pl->supported, Pause);
863 phylink_set(pl->supported, Asym_Pause);
866 phylink_set(pl->supported, Autoneg);
869 __set_bit(s->bit, pl->supported);
870 __set_bit(s->bit, pl->link_config.lp_advertising);
872 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
873 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
874 pl->link_config.speed);
877 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
880 pl->link_config.link = 1;
881 pl->link_config.an_complete = 1;
886 static int phylink_parse_mode(struct phylink *pl,
887 const struct fwnode_handle *fwnode)
889 struct fwnode_handle *dn;
893 if (pl->config->default_an_inband)
894 pl->cfg_link_an_mode = MLO_AN_INBAND;
896 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
897 if (dn || fwnode_property_present(fwnode, "fixed-link"))
898 pl->cfg_link_an_mode = MLO_AN_FIXED;
899 fwnode_handle_put(dn);
901 if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
902 strcmp(managed, "in-band-status") == 0)) {
903 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
905 "can't use both fixed-link and in-band-status\n");
909 pl->cfg_link_an_mode = MLO_AN_INBAND;
912 if (pl->cfg_link_an_mode == MLO_AN_INBAND) {
913 linkmode_zero(pl->supported);
914 phylink_set(pl->supported, MII);
915 phylink_set(pl->supported, Autoneg);
916 phylink_set(pl->supported, Asym_Pause);
917 phylink_set(pl->supported, Pause);
919 switch (pl->link_config.interface) {
920 case PHY_INTERFACE_MODE_SGMII:
921 case PHY_INTERFACE_MODE_PSGMII:
922 case PHY_INTERFACE_MODE_QSGMII:
923 case PHY_INTERFACE_MODE_QUSGMII:
924 case PHY_INTERFACE_MODE_RGMII:
925 case PHY_INTERFACE_MODE_RGMII_ID:
926 case PHY_INTERFACE_MODE_RGMII_RXID:
927 case PHY_INTERFACE_MODE_RGMII_TXID:
928 case PHY_INTERFACE_MODE_RTBI:
929 case PHY_INTERFACE_MODE_1000BASEX:
930 case PHY_INTERFACE_MODE_2500BASEX:
931 case PHY_INTERFACE_MODE_5GBASER:
932 case PHY_INTERFACE_MODE_25GBASER:
933 case PHY_INTERFACE_MODE_USXGMII:
934 case PHY_INTERFACE_MODE_10G_QXGMII:
935 case PHY_INTERFACE_MODE_10GKR:
936 case PHY_INTERFACE_MODE_10GBASER:
937 case PHY_INTERFACE_MODE_XLGMII:
938 caps = ~(MAC_SYM_PAUSE | MAC_ASYM_PAUSE);
939 caps = phylink_get_capabilities(pl->link_config.interface, caps,
941 phylink_caps_to_linkmodes(pl->supported, caps);
946 "incorrect link mode %s for in-band status\n",
947 phy_modes(pl->link_config.interface));
951 linkmode_copy(pl->link_config.advertising, pl->supported);
953 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
955 "failed to validate link configuration for in-band status\n");
963 static void phylink_apply_manual_flow(struct phylink *pl,
964 struct phylink_link_state *state)
966 /* If autoneg is disabled, pause AN is also disabled */
967 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
969 state->pause &= ~MLO_PAUSE_AN;
971 /* Manual configuration of pause modes */
972 if (!(pl->link_config.pause & MLO_PAUSE_AN))
973 state->pause = pl->link_config.pause;
976 static void phylink_resolve_an_pause(struct phylink_link_state *state)
978 bool tx_pause, rx_pause;
980 if (state->duplex == DUPLEX_FULL) {
981 linkmode_resolve_pause(state->advertising,
982 state->lp_advertising,
983 &tx_pause, &rx_pause);
985 state->pause |= MLO_PAUSE_TX;
987 state->pause |= MLO_PAUSE_RX;
991 static void phylink_pcs_pre_config(struct phylink_pcs *pcs,
992 phy_interface_t interface)
994 if (pcs && pcs->ops->pcs_pre_config)
995 pcs->ops->pcs_pre_config(pcs, interface);
998 static int phylink_pcs_post_config(struct phylink_pcs *pcs,
999 phy_interface_t interface)
1003 if (pcs && pcs->ops->pcs_post_config)
1004 err = pcs->ops->pcs_post_config(pcs, interface);
1009 static void phylink_pcs_disable(struct phylink_pcs *pcs)
1011 if (pcs && pcs->ops->pcs_disable)
1012 pcs->ops->pcs_disable(pcs);
1015 static int phylink_pcs_enable(struct phylink_pcs *pcs)
1019 if (pcs && pcs->ops->pcs_enable)
1020 err = pcs->ops->pcs_enable(pcs);
1025 static int phylink_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
1026 const struct phylink_link_state *state,
1027 bool permit_pause_to_mac)
1032 return pcs->ops->pcs_config(pcs, neg_mode, state->interface,
1033 state->advertising, permit_pause_to_mac);
1036 static void phylink_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
1037 phy_interface_t interface, int speed,
1040 if (pcs && pcs->ops->pcs_link_up)
1041 pcs->ops->pcs_link_up(pcs, neg_mode, interface, speed, duplex);
1044 static void phylink_pcs_poll_stop(struct phylink *pl)
1046 if (pl->cfg_link_an_mode == MLO_AN_INBAND)
1047 del_timer(&pl->link_poll);
1050 static void phylink_pcs_poll_start(struct phylink *pl)
1052 if (pl->pcs && pl->pcs->poll && pl->cfg_link_an_mode == MLO_AN_INBAND)
1053 mod_timer(&pl->link_poll, jiffies + HZ);
1056 int phylink_pcs_pre_init(struct phylink *pl, struct phylink_pcs *pcs)
1060 /* Signal to PCS driver that MAC requires RX clock for init */
1061 if (pl->config->mac_requires_rxc)
1062 pcs->rxc_always_on = true;
1064 if (pcs->ops->pcs_pre_init)
1065 ret = pcs->ops->pcs_pre_init(pcs);
1069 EXPORT_SYMBOL_GPL(phylink_pcs_pre_init);
1071 static void phylink_mac_config(struct phylink *pl,
1072 const struct phylink_link_state *state)
1074 struct phylink_link_state st = *state;
1076 /* Stop drivers incorrectly using these */
1077 linkmode_zero(st.lp_advertising);
1078 st.speed = SPEED_UNKNOWN;
1079 st.duplex = DUPLEX_UNKNOWN;
1080 st.an_complete = false;
1084 "%s: mode=%s/%s/%s adv=%*pb pause=%02x\n",
1085 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
1086 phy_modes(st.interface),
1087 phy_rate_matching_to_str(st.rate_matching),
1088 __ETHTOOL_LINK_MODE_MASK_NBITS, st.advertising,
1091 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, &st);
1094 static void phylink_pcs_an_restart(struct phylink *pl)
1096 if (pl->pcs && linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1097 pl->link_config.advertising) &&
1098 phy_interface_mode_is_8023z(pl->link_config.interface) &&
1099 phylink_autoneg_inband(pl->cur_link_an_mode))
1100 pl->pcs->ops->pcs_an_restart(pl->pcs);
1104 * phylink_pcs_neg_mode() - helper to determine PCS inband mode
1105 * @mode: one of %MLO_AN_FIXED, %MLO_AN_PHY, %MLO_AN_INBAND.
1106 * @interface: interface mode to be used
1107 * @advertising: adertisement ethtool link mode mask
1109 * Determines the negotiation mode to be used by the PCS, and returns
1112 * - %PHYLINK_PCS_NEG_NONE: interface mode does not support inband
1113 * - %PHYLINK_PCS_NEG_OUTBAND: an out of band mode (e.g. reading the PHY)
1115 * - %PHYLINK_PCS_NEG_INBAND_DISABLED: inband mode selected but autoneg
1117 * - %PHYLINK_PCS_NEG_INBAND_ENABLED: inband mode selected and autoneg enabled
1119 * Note: this is for cases where the PCS itself is involved in negotiation
1120 * (e.g. Clause 37, SGMII and similar) not Clause 73.
1122 static unsigned int phylink_pcs_neg_mode(unsigned int mode,
1123 phy_interface_t interface,
1124 const unsigned long *advertising)
1126 unsigned int neg_mode;
1128 switch (interface) {
1129 case PHY_INTERFACE_MODE_SGMII:
1130 case PHY_INTERFACE_MODE_QSGMII:
1131 case PHY_INTERFACE_MODE_QUSGMII:
1132 case PHY_INTERFACE_MODE_USXGMII:
1133 case PHY_INTERFACE_MODE_10G_QXGMII:
1134 /* These protocols are designed for use with a PHY which
1135 * communicates its negotiation result back to the MAC via
1136 * inband communication. Note: there exist PHYs that run
1137 * with SGMII but do not send the inband data.
1139 if (!phylink_autoneg_inband(mode))
1140 neg_mode = PHYLINK_PCS_NEG_OUTBAND;
1142 neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
1145 case PHY_INTERFACE_MODE_1000BASEX:
1146 case PHY_INTERFACE_MODE_2500BASEX:
1147 /* 1000base-X is designed for use media-side for Fibre
1148 * connections, and thus the Autoneg bit needs to be
1149 * taken into account. We also do this for 2500base-X
1150 * as well, but drivers may not support this, so may
1151 * need to override this.
1153 if (!phylink_autoneg_inband(mode))
1154 neg_mode = PHYLINK_PCS_NEG_OUTBAND;
1155 else if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1157 neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
1159 neg_mode = PHYLINK_PCS_NEG_INBAND_DISABLED;
1163 neg_mode = PHYLINK_PCS_NEG_NONE;
1170 static void phylink_major_config(struct phylink *pl, bool restart,
1171 const struct phylink_link_state *state)
1173 struct phylink_pcs *pcs = NULL;
1174 bool pcs_changed = false;
1175 unsigned int rate_kbd;
1176 unsigned int neg_mode;
1179 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
1181 pl->pcs_neg_mode = phylink_pcs_neg_mode(pl->cur_link_an_mode,
1183 state->advertising);
1185 if (pl->using_mac_select_pcs) {
1186 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
1189 "mac_select_pcs unexpectedly failed: %pe\n",
1194 pcs_changed = pcs && pl->pcs != pcs;
1197 phylink_pcs_poll_stop(pl);
1199 if (pl->mac_ops->mac_prepare) {
1200 err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
1203 phylink_err(pl, "mac_prepare failed: %pe\n",
1209 /* If we have a new PCS, switch to the new PCS after preparing the MAC
1213 phylink_pcs_disable(pl->pcs);
1216 pl->pcs->phylink = NULL;
1224 phylink_pcs_pre_config(pl->pcs, state->interface);
1226 phylink_mac_config(pl, state);
1229 phylink_pcs_post_config(pl->pcs, state->interface);
1231 if (pl->pcs_state == PCS_STATE_STARTING || pcs_changed)
1232 phylink_pcs_enable(pl->pcs);
1234 neg_mode = pl->cur_link_an_mode;
1235 if (pl->pcs && pl->pcs->neg_mode)
1236 neg_mode = pl->pcs_neg_mode;
1238 err = phylink_pcs_config(pl->pcs, neg_mode, state,
1239 !!(pl->link_config.pause & MLO_PAUSE_AN));
1241 phylink_err(pl, "pcs_config failed: %pe\n",
1247 phylink_pcs_an_restart(pl);
1249 if (pl->mac_ops->mac_finish) {
1250 err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
1253 phylink_err(pl, "mac_finish failed: %pe\n",
1258 rate_kbd = phylink_interface_signal_rate(state->interface);
1260 sfp_upstream_set_signal_rate(pl->sfp_bus, rate_kbd);
1263 phylink_pcs_poll_start(pl);
1267 * Reconfigure for a change of inband advertisement.
1268 * If we have a separate PCS, we only need to call its pcs_config() method,
1269 * and then restart AN if it indicates something changed. Otherwise, we do
1270 * the full MAC reconfiguration.
1272 static int phylink_change_inband_advert(struct phylink *pl)
1274 unsigned int neg_mode;
1277 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
1280 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
1281 phylink_an_mode_str(pl->cur_link_an_mode),
1282 phy_modes(pl->link_config.interface),
1283 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
1284 pl->link_config.pause);
1286 /* Recompute the PCS neg mode */
1287 pl->pcs_neg_mode = phylink_pcs_neg_mode(pl->cur_link_an_mode,
1288 pl->link_config.interface,
1289 pl->link_config.advertising);
1291 neg_mode = pl->cur_link_an_mode;
1292 if (pl->pcs->neg_mode)
1293 neg_mode = pl->pcs_neg_mode;
1295 /* Modern PCS-based method; update the advert at the PCS, and
1296 * restart negotiation if the pcs_config() helper indicates that
1297 * the programmed advertisement has changed.
1299 ret = phylink_pcs_config(pl->pcs, neg_mode, &pl->link_config,
1300 !!(pl->link_config.pause & MLO_PAUSE_AN));
1305 phylink_pcs_an_restart(pl);
1310 static void phylink_mac_pcs_get_state(struct phylink *pl,
1311 struct phylink_link_state *state)
1313 linkmode_copy(state->advertising, pl->link_config.advertising);
1314 linkmode_zero(state->lp_advertising);
1315 state->interface = pl->link_config.interface;
1316 state->rate_matching = pl->link_config.rate_matching;
1317 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1318 state->advertising)) {
1319 state->speed = SPEED_UNKNOWN;
1320 state->duplex = DUPLEX_UNKNOWN;
1321 state->pause = MLO_PAUSE_NONE;
1323 state->speed = pl->link_config.speed;
1324 state->duplex = pl->link_config.duplex;
1325 state->pause = pl->link_config.pause;
1327 state->an_complete = 0;
1331 pl->pcs->ops->pcs_get_state(pl->pcs, state);
1336 /* The fixed state is... fixed except for the link state,
1337 * which may be determined by a GPIO or a callback.
1339 static void phylink_get_fixed_state(struct phylink *pl,
1340 struct phylink_link_state *state)
1342 *state = pl->link_config;
1343 if (pl->config->get_fixed_state)
1344 pl->config->get_fixed_state(pl->config, state);
1345 else if (pl->link_gpio)
1346 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
1348 state->pause = MLO_PAUSE_NONE;
1349 phylink_resolve_an_pause(state);
1352 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
1354 struct phylink_link_state link_state;
1356 switch (pl->cur_link_an_mode) {
1358 link_state = pl->phy_state;
1362 phylink_get_fixed_state(pl, &link_state);
1366 link_state = pl->link_config;
1367 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
1368 link_state.pause = MLO_PAUSE_NONE;
1371 default: /* can't happen */
1375 link_state.link = false;
1377 phylink_apply_manual_flow(pl, &link_state);
1378 phylink_major_config(pl, force_restart, &link_state);
1381 static const char *phylink_pause_to_str(int pause)
1383 switch (pause & MLO_PAUSE_TXRX_MASK) {
1384 case MLO_PAUSE_TX | MLO_PAUSE_RX:
1395 static void phylink_link_up(struct phylink *pl,
1396 struct phylink_link_state link_state)
1398 struct net_device *ndev = pl->netdev;
1399 unsigned int neg_mode;
1403 speed = link_state.speed;
1404 duplex = link_state.duplex;
1405 rx_pause = !!(link_state.pause & MLO_PAUSE_RX);
1407 switch (link_state.rate_matching) {
1408 case RATE_MATCH_PAUSE:
1409 /* The PHY is doing rate matchion from the media rate (in
1410 * the link_state) to the interface speed, and will send
1411 * pause frames to the MAC to limit its transmission speed.
1413 speed = phylink_interface_max_speed(link_state.interface);
1414 duplex = DUPLEX_FULL;
1418 case RATE_MATCH_CRS:
1419 /* The PHY is doing rate matchion from the media rate (in
1420 * the link_state) to the interface speed, and will cause
1421 * collisions to the MAC to limit its transmission speed.
1423 speed = phylink_interface_max_speed(link_state.interface);
1424 duplex = DUPLEX_HALF;
1428 pl->cur_interface = link_state.interface;
1430 neg_mode = pl->cur_link_an_mode;
1431 if (pl->pcs && pl->pcs->neg_mode)
1432 neg_mode = pl->pcs_neg_mode;
1434 phylink_pcs_link_up(pl->pcs, neg_mode, pl->cur_interface, speed,
1437 pl->mac_ops->mac_link_up(pl->config, pl->phydev, pl->cur_link_an_mode,
1438 pl->cur_interface, speed, duplex,
1439 !!(link_state.pause & MLO_PAUSE_TX), rx_pause);
1442 netif_carrier_on(ndev);
1445 "Link is Up - %s/%s - flow control %s\n",
1446 phy_speed_to_str(link_state.speed),
1447 phy_duplex_to_str(link_state.duplex),
1448 phylink_pause_to_str(link_state.pause));
1451 static void phylink_link_down(struct phylink *pl)
1453 struct net_device *ndev = pl->netdev;
1456 netif_carrier_off(ndev);
1457 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
1459 phylink_info(pl, "Link is Down\n");
1462 static void phylink_resolve(struct work_struct *w)
1464 struct phylink *pl = container_of(w, struct phylink, resolve);
1465 struct phylink_link_state link_state;
1466 struct net_device *ndev = pl->netdev;
1467 bool mac_config = false;
1468 bool retrigger = false;
1469 bool cur_link_state;
1471 mutex_lock(&pl->state_mutex);
1473 cur_link_state = netif_carrier_ok(ndev);
1475 cur_link_state = pl->old_link_state;
1477 if (pl->phylink_disable_state) {
1478 pl->mac_link_dropped = false;
1479 link_state.link = false;
1480 } else if (pl->mac_link_dropped) {
1481 link_state.link = false;
1484 switch (pl->cur_link_an_mode) {
1486 link_state = pl->phy_state;
1487 phylink_apply_manual_flow(pl, &link_state);
1488 mac_config = link_state.link;
1492 phylink_get_fixed_state(pl, &link_state);
1493 mac_config = link_state.link;
1497 phylink_mac_pcs_get_state(pl, &link_state);
1499 /* The PCS may have a latching link-fail indicator.
1500 * If the link was up, bring the link down and
1501 * re-trigger the resolve. Otherwise, re-read the
1502 * PCS state to get the current status of the link.
1504 if (!link_state.link) {
1508 phylink_mac_pcs_get_state(pl,
1512 /* If we have a phy, the "up" state is the union of
1513 * both the PHY and the MAC
1516 link_state.link &= pl->phy_state.link;
1518 /* Only update if the PHY link is up */
1519 if (pl->phydev && pl->phy_state.link) {
1520 /* If the interface has changed, force a
1521 * link down event if the link isn't already
1522 * down, and re-resolve.
1524 if (link_state.interface !=
1525 pl->phy_state.interface) {
1527 link_state.link = false;
1529 link_state.interface = pl->phy_state.interface;
1531 /* If we are doing rate matching, then the
1532 * link speed/duplex comes from the PHY
1534 if (pl->phy_state.rate_matching) {
1535 link_state.rate_matching =
1536 pl->phy_state.rate_matching;
1537 link_state.speed = pl->phy_state.speed;
1539 pl->phy_state.duplex;
1542 /* If we have a PHY, we need to update with
1543 * the PHY flow control bits.
1545 link_state.pause = pl->phy_state.pause;
1548 phylink_apply_manual_flow(pl, &link_state);
1554 if (link_state.interface != pl->link_config.interface) {
1555 /* The interface has changed, force the link down and
1558 if (cur_link_state) {
1559 phylink_link_down(pl);
1560 cur_link_state = false;
1562 phylink_major_config(pl, false, &link_state);
1563 pl->link_config.interface = link_state.interface;
1567 if (link_state.link != cur_link_state) {
1568 pl->old_link_state = link_state.link;
1569 if (!link_state.link)
1570 phylink_link_down(pl);
1572 phylink_link_up(pl, link_state);
1574 if (!link_state.link && retrigger) {
1575 pl->mac_link_dropped = false;
1576 queue_work(system_power_efficient_wq, &pl->resolve);
1578 mutex_unlock(&pl->state_mutex);
1581 static void phylink_run_resolve(struct phylink *pl)
1583 if (!pl->phylink_disable_state)
1584 queue_work(system_power_efficient_wq, &pl->resolve);
1587 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
1589 unsigned long state = pl->phylink_disable_state;
1591 set_bit(bit, &pl->phylink_disable_state);
1593 queue_work(system_power_efficient_wq, &pl->resolve);
1594 flush_work(&pl->resolve);
1598 static void phylink_enable_and_run_resolve(struct phylink *pl, int bit)
1600 clear_bit(bit, &pl->phylink_disable_state);
1601 phylink_run_resolve(pl);
1604 static void phylink_fixed_poll(struct timer_list *t)
1606 struct phylink *pl = container_of(t, struct phylink, link_poll);
1608 mod_timer(t, jiffies + HZ);
1610 phylink_run_resolve(pl);
1613 static const struct sfp_upstream_ops sfp_phylink_ops;
1615 static int phylink_register_sfp(struct phylink *pl,
1616 const struct fwnode_handle *fwnode)
1618 struct sfp_bus *bus;
1624 bus = sfp_bus_find_fwnode(fwnode);
1626 phylink_err(pl, "unable to attach SFP bus: %pe\n", bus);
1627 return PTR_ERR(bus);
1632 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
1639 * phylink_set_fixed_link() - set the fixed link
1640 * @pl: a pointer to a &struct phylink returned from phylink_create()
1641 * @state: a pointer to a struct phylink_link_state.
1643 * This function is used when the link parameters are known and do not change,
1644 * making it suitable for certain types of network connections.
1646 * Returns: zero on success or negative error code.
1648 int phylink_set_fixed_link(struct phylink *pl,
1649 const struct phylink_link_state *state)
1651 const struct phy_setting *s;
1654 if (pl->cfg_link_an_mode != MLO_AN_PHY || !state ||
1655 !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
1658 s = phy_lookup_setting(state->speed, state->duplex,
1659 pl->supported, true);
1663 adv = pl->link_config.advertising;
1665 linkmode_set_bit(s->bit, adv);
1666 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, adv);
1668 pl->link_config.speed = state->speed;
1669 pl->link_config.duplex = state->duplex;
1670 pl->link_config.link = 1;
1671 pl->link_config.an_complete = 1;
1673 pl->cfg_link_an_mode = MLO_AN_FIXED;
1674 pl->cur_link_an_mode = pl->cfg_link_an_mode;
1678 EXPORT_SYMBOL_GPL(phylink_set_fixed_link);
1681 * phylink_create() - create a phylink instance
1682 * @config: a pointer to the target &struct phylink_config
1683 * @fwnode: a pointer to a &struct fwnode_handle describing the network
1685 * @iface: the desired link mode defined by &typedef phy_interface_t
1686 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
1688 * Create a new phylink instance, and parse the link parameters found in @np.
1689 * This will parse in-band modes, fixed-link or SFP configuration.
1691 * Note: the rtnl lock must not be held when calling this function.
1693 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
1694 * must use IS_ERR() to check for errors from this function.
1696 struct phylink *phylink_create(struct phylink_config *config,
1697 const struct fwnode_handle *fwnode,
1698 phy_interface_t iface,
1699 const struct phylink_mac_ops *mac_ops)
1701 bool using_mac_select_pcs = false;
1705 /* Validate the supplied configuration */
1706 if (phy_interface_empty(config->supported_interfaces)) {
1707 dev_err(config->dev,
1708 "phylink: error: empty supported_interfaces\n");
1709 return ERR_PTR(-EINVAL);
1712 if (mac_ops->mac_select_pcs &&
1713 mac_ops->mac_select_pcs(config, PHY_INTERFACE_MODE_NA) !=
1714 ERR_PTR(-EOPNOTSUPP))
1715 using_mac_select_pcs = true;
1717 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
1719 return ERR_PTR(-ENOMEM);
1721 mutex_init(&pl->state_mutex);
1722 INIT_WORK(&pl->resolve, phylink_resolve);
1724 pl->config = config;
1725 if (config->type == PHYLINK_NETDEV) {
1726 pl->netdev = to_net_dev(config->dev);
1727 netif_carrier_off(pl->netdev);
1728 } else if (config->type == PHYLINK_DEV) {
1729 pl->dev = config->dev;
1732 return ERR_PTR(-EINVAL);
1735 pl->using_mac_select_pcs = using_mac_select_pcs;
1736 pl->phy_state.interface = iface;
1737 pl->link_interface = iface;
1738 if (iface == PHY_INTERFACE_MODE_MOCA)
1739 pl->link_port = PORT_BNC;
1741 pl->link_port = PORT_MII;
1742 pl->link_config.interface = iface;
1743 pl->link_config.pause = MLO_PAUSE_AN;
1744 pl->link_config.speed = SPEED_UNKNOWN;
1745 pl->link_config.duplex = DUPLEX_UNKNOWN;
1746 pl->pcs_state = PCS_STATE_DOWN;
1747 pl->mac_ops = mac_ops;
1748 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1749 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
1751 linkmode_fill(pl->supported);
1752 linkmode_copy(pl->link_config.advertising, pl->supported);
1753 phylink_validate(pl, pl->supported, &pl->link_config);
1755 ret = phylink_parse_mode(pl, fwnode);
1758 return ERR_PTR(ret);
1761 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
1762 ret = phylink_parse_fixedlink(pl, fwnode);
1765 return ERR_PTR(ret);
1769 pl->cur_link_an_mode = pl->cfg_link_an_mode;
1771 ret = phylink_register_sfp(pl, fwnode);
1774 return ERR_PTR(ret);
1779 EXPORT_SYMBOL_GPL(phylink_create);
1782 * phylink_destroy() - cleanup and destroy the phylink instance
1783 * @pl: a pointer to a &struct phylink returned from phylink_create()
1785 * Destroy a phylink instance. Any PHY that has been attached must have been
1786 * cleaned up via phylink_disconnect_phy() prior to calling this function.
1788 * Note: the rtnl lock must not be held when calling this function.
1790 void phylink_destroy(struct phylink *pl)
1792 sfp_bus_del_upstream(pl->sfp_bus);
1794 gpiod_put(pl->link_gpio);
1796 cancel_work_sync(&pl->resolve);
1799 EXPORT_SYMBOL_GPL(phylink_destroy);
1802 * phylink_expects_phy() - Determine if phylink expects a phy to be attached
1803 * @pl: a pointer to a &struct phylink returned from phylink_create()
1805 * When using fixed-link mode, or in-band mode with 1000base-X or 2500base-X,
1808 * Returns true if phylink will be expecting a PHY.
1810 bool phylink_expects_phy(struct phylink *pl)
1812 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1813 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1814 phy_interface_mode_is_8023z(pl->link_config.interface)))
1818 EXPORT_SYMBOL_GPL(phylink_expects_phy);
1820 static void phylink_phy_change(struct phy_device *phydev, bool up)
1822 struct phylink *pl = phydev->phylink;
1823 bool tx_pause, rx_pause;
1825 phy_get_pause(phydev, &tx_pause, &rx_pause);
1827 mutex_lock(&pl->state_mutex);
1828 pl->phy_state.speed = phydev->speed;
1829 pl->phy_state.duplex = phydev->duplex;
1830 pl->phy_state.rate_matching = phydev->rate_matching;
1831 pl->phy_state.pause = MLO_PAUSE_NONE;
1833 pl->phy_state.pause |= MLO_PAUSE_TX;
1835 pl->phy_state.pause |= MLO_PAUSE_RX;
1836 pl->phy_state.interface = phydev->interface;
1837 pl->phy_state.link = up;
1838 mutex_unlock(&pl->state_mutex);
1840 phylink_run_resolve(pl);
1842 phylink_dbg(pl, "phy link %s %s/%s/%s/%s/%s\n", up ? "up" : "down",
1843 phy_modes(phydev->interface),
1844 phy_speed_to_str(phydev->speed),
1845 phy_duplex_to_str(phydev->duplex),
1846 phy_rate_matching_to_str(phydev->rate_matching),
1847 phylink_pause_to_str(pl->phy_state.pause));
1850 static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
1851 unsigned long *supported,
1852 struct phylink_link_state *state)
1854 DECLARE_PHY_INTERFACE_MASK(interfaces);
1856 /* If the PHY provides a bitmap of the interfaces it will be using
1857 * depending on the negotiated media speeds, use this to validate
1858 * which ethtool link modes can be used.
1860 if (!phy_interface_empty(phy->possible_interfaces)) {
1861 /* We only care about the union of the PHY's interfaces and
1862 * those which the host supports.
1864 phy_interface_and(interfaces, phy->possible_interfaces,
1865 pl->config->supported_interfaces);
1867 if (phy_interface_empty(interfaces)) {
1868 phylink_err(pl, "PHY has no common interfaces\n");
1872 if (phy_on_sfp(phy)) {
1873 /* If the PHY is on a SFP, limit the interfaces to
1874 * those that can be used with a SFP module.
1876 phy_interface_and(interfaces, interfaces,
1877 phylink_sfp_interfaces);
1879 if (phy_interface_empty(interfaces)) {
1880 phylink_err(pl, "SFP PHY's possible interfaces becomes empty\n");
1885 phylink_dbg(pl, "PHY %s uses interfaces %*pbl, validating %*pbl\n",
1887 (int)PHY_INTERFACE_MODE_MAX,
1888 phy->possible_interfaces,
1889 (int)PHY_INTERFACE_MODE_MAX, interfaces);
1891 return phylink_validate_mask(pl, phy, supported, state,
1895 phylink_dbg(pl, "PHY %s doesn't supply possible interfaces\n",
1898 /* Check whether we would use rate matching for the proposed interface
1901 state->rate_matching = phy_get_rate_matching(phy, state->interface);
1903 /* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
1904 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
1905 * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching
1906 * their Serdes is either unnecessary or not reasonable.
1908 * For these which switch interface modes, we really need to know which
1909 * interface modes the PHY supports to properly work out which ethtool
1910 * linkmodes can be supported. For now, as a work-around, we validate
1911 * against all interface modes, which may lead to more ethtool link
1912 * modes being advertised than are actually supported.
1914 if (phy->is_c45 && state->rate_matching == RATE_MATCH_NONE &&
1915 state->interface != PHY_INTERFACE_MODE_RXAUI &&
1916 state->interface != PHY_INTERFACE_MODE_XAUI &&
1917 state->interface != PHY_INTERFACE_MODE_USXGMII)
1918 state->interface = PHY_INTERFACE_MODE_NA;
1920 return phylink_validate(pl, supported, state);
1923 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
1924 phy_interface_t interface)
1926 struct phylink_link_state config;
1927 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
1932 * This is the new way of dealing with flow control for PHYs,
1933 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
1934 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
1935 * using our validate call to the MAC, we rely upon the MAC
1936 * clearing the bits from both supported and advertising fields.
1938 phy_support_asym_pause(phy);
1940 memset(&config, 0, sizeof(config));
1941 linkmode_copy(supported, phy->supported);
1942 linkmode_copy(config.advertising, phy->advertising);
1943 config.interface = interface;
1945 ret = phylink_validate_phy(pl, phy, supported, &config);
1947 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
1948 phy_modes(config.interface),
1949 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1950 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1956 phy->phy_link_change = phylink_phy_change;
1958 irq_str = phy_attached_info_irq(phy);
1960 "PHY [%s] driver [%s] (irq=%s)\n",
1961 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1964 mutex_lock(&phy->lock);
1965 mutex_lock(&pl->state_mutex);
1967 pl->phy_state.interface = interface;
1968 pl->phy_state.pause = MLO_PAUSE_NONE;
1969 pl->phy_state.speed = SPEED_UNKNOWN;
1970 pl->phy_state.duplex = DUPLEX_UNKNOWN;
1971 pl->phy_state.rate_matching = RATE_MATCH_NONE;
1972 linkmode_copy(pl->supported, supported);
1973 linkmode_copy(pl->link_config.advertising, config.advertising);
1975 /* Restrict the phy advertisement according to the MAC support. */
1976 linkmode_copy(phy->advertising, config.advertising);
1977 mutex_unlock(&pl->state_mutex);
1978 mutex_unlock(&phy->lock);
1981 "phy: %s setting supported %*pb advertising %*pb\n",
1982 phy_modes(interface),
1983 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1984 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1986 if (phy_interrupt_is_valid(phy))
1987 phy_request_interrupt(phy);
1989 if (pl->config->mac_managed_pm)
1990 phy->mac_managed_pm = true;
1995 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1996 phy_interface_t interface)
2000 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
2001 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
2002 phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
2008 if (pl->config->mac_requires_rxc)
2009 flags |= PHY_F_RXC_ALWAYS_ON;
2011 return phy_attach_direct(pl->netdev, phy, flags, interface);
2015 * phylink_connect_phy() - connect a PHY to the phylink instance
2016 * @pl: a pointer to a &struct phylink returned from phylink_create()
2017 * @phy: a pointer to a &struct phy_device.
2019 * Connect @phy to the phylink instance specified by @pl by calling
2020 * phy_attach_direct(). Configure the @phy according to the MAC driver's
2021 * capabilities, start the PHYLIB state machine and enable any interrupts
2022 * that the PHY supports.
2024 * This updates the phylink's ethtool supported and advertising link mode
2027 * Returns 0 on success or a negative errno.
2029 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
2033 /* Use PHY device/driver interface */
2034 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
2035 pl->link_interface = phy->interface;
2036 pl->link_config.interface = pl->link_interface;
2039 ret = phylink_attach_phy(pl, phy, pl->link_interface);
2043 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
2049 EXPORT_SYMBOL_GPL(phylink_connect_phy);
2052 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
2053 * @pl: a pointer to a &struct phylink returned from phylink_create()
2054 * @dn: a pointer to a &struct device_node.
2055 * @flags: PHY-specific flags to communicate to the PHY device driver
2057 * Connect the phy specified in the device node @dn to the phylink instance
2058 * specified by @pl. Actions specified in phylink_connect_phy() will be
2061 * Returns 0 on success or a negative errno.
2063 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
2066 return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
2068 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
2071 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
2072 * @pl: a pointer to a &struct phylink returned from phylink_create()
2073 * @fwnode: a pointer to a &struct fwnode_handle.
2074 * @flags: PHY-specific flags to communicate to the PHY device driver
2076 * Connect the phy specified @fwnode to the phylink instance specified
2079 * Returns 0 on success or a negative errno.
2081 int phylink_fwnode_phy_connect(struct phylink *pl,
2082 const struct fwnode_handle *fwnode,
2085 struct fwnode_handle *phy_fwnode;
2086 struct phy_device *phy_dev;
2089 /* Fixed links and 802.3z are handled without needing a PHY */
2090 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
2091 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
2092 phy_interface_mode_is_8023z(pl->link_interface)))
2095 phy_fwnode = fwnode_get_phy_node(fwnode);
2096 if (IS_ERR(phy_fwnode)) {
2097 if (pl->cfg_link_an_mode == MLO_AN_PHY)
2102 phy_dev = fwnode_phy_find_device(phy_fwnode);
2103 /* We're done with the phy_node handle */
2104 fwnode_handle_put(phy_fwnode);
2108 /* Use PHY device/driver interface */
2109 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
2110 pl->link_interface = phy_dev->interface;
2111 pl->link_config.interface = pl->link_interface;
2114 if (pl->config->mac_requires_rxc)
2115 flags |= PHY_F_RXC_ALWAYS_ON;
2117 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
2118 pl->link_interface);
2119 phy_device_free(phy_dev);
2123 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
2125 phy_detach(phy_dev);
2129 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
2132 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
2134 * @pl: a pointer to a &struct phylink returned from phylink_create()
2136 * Disconnect any current PHY from the phylink instance described by @pl.
2138 void phylink_disconnect_phy(struct phylink *pl)
2140 struct phy_device *phy;
2146 mutex_lock(&phy->lock);
2147 mutex_lock(&pl->state_mutex);
2149 mutex_unlock(&pl->state_mutex);
2150 mutex_unlock(&phy->lock);
2151 flush_work(&pl->resolve);
2153 phy_disconnect(phy);
2156 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
2158 static void phylink_link_changed(struct phylink *pl, bool up, const char *what)
2161 pl->mac_link_dropped = true;
2162 phylink_run_resolve(pl);
2163 phylink_dbg(pl, "%s link %s\n", what, up ? "up" : "down");
2167 * phylink_mac_change() - notify phylink of a change in MAC state
2168 * @pl: a pointer to a &struct phylink returned from phylink_create()
2169 * @up: indicates whether the link is currently up.
2171 * The MAC driver should call this driver when the state of its link
2172 * changes (eg, link failure, new negotiation results, etc.)
2174 void phylink_mac_change(struct phylink *pl, bool up)
2176 phylink_link_changed(pl, up, "mac");
2178 EXPORT_SYMBOL_GPL(phylink_mac_change);
2181 * phylink_pcs_change() - notify phylink of a change to PCS link state
2182 * @pcs: pointer to &struct phylink_pcs
2183 * @up: indicates whether the link is currently up.
2185 * The PCS driver should call this when the state of its link changes
2186 * (e.g. link failure, new negotiation results, etc.) Note: it should
2187 * not determine "up" by reading the BMSR. If in doubt about the link
2188 * state at interrupt time, then pass true if pcs_get_state() returns
2189 * the latched link-down state, otherwise pass false.
2191 void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
2193 struct phylink *pl = pcs->phylink;
2196 phylink_link_changed(pl, up, "pcs");
2198 EXPORT_SYMBOL_GPL(phylink_pcs_change);
2200 static irqreturn_t phylink_link_handler(int irq, void *data)
2202 struct phylink *pl = data;
2204 phylink_run_resolve(pl);
2210 * phylink_start() - start a phylink instance
2211 * @pl: a pointer to a &struct phylink returned from phylink_create()
2213 * Start the phylink instance specified by @pl, configuring the MAC for the
2214 * desired link mode(s) and negotiation style. This should be called from the
2215 * network device driver's &struct net_device_ops ndo_open() method.
2217 void phylink_start(struct phylink *pl)
2223 phylink_info(pl, "configuring for %s/%s link mode\n",
2224 phylink_an_mode_str(pl->cur_link_an_mode),
2225 phy_modes(pl->link_config.interface));
2227 /* Always set the carrier off */
2229 netif_carrier_off(pl->netdev);
2231 pl->pcs_state = PCS_STATE_STARTING;
2233 /* Apply the link configuration to the MAC when starting. This allows
2234 * a fixed-link to start with the correct parameters, and also
2235 * ensures that we set the appropriate advertisement for Serdes links.
2237 * Restart autonegotiation if using 802.3z to ensure that the link
2238 * parameters are properly negotiated. This is necessary for DSA
2239 * switches using 802.3z negotiation to ensure they see our modes.
2241 phylink_mac_initial_config(pl, true);
2243 pl->pcs_state = PCS_STATE_STARTED;
2245 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
2247 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
2248 int irq = gpiod_to_irq(pl->link_gpio);
2251 if (!request_irq(irq, phylink_link_handler,
2252 IRQF_TRIGGER_RISING |
2253 IRQF_TRIGGER_FALLING,
2263 if (pl->cfg_link_an_mode == MLO_AN_FIXED)
2264 poll |= pl->config->poll_fixed_state;
2267 mod_timer(&pl->link_poll, jiffies + HZ);
2269 phy_start(pl->phydev);
2271 sfp_upstream_start(pl->sfp_bus);
2273 EXPORT_SYMBOL_GPL(phylink_start);
2276 * phylink_stop() - stop a phylink instance
2277 * @pl: a pointer to a &struct phylink returned from phylink_create()
2279 * Stop the phylink instance specified by @pl. This should be called from the
2280 * network device driver's &struct net_device_ops ndo_stop() method. The
2281 * network device's carrier state should not be changed prior to calling this
2284 * This will synchronously bring down the link if the link is not already
2285 * down (in other words, it will trigger a mac_link_down() method call.)
2287 void phylink_stop(struct phylink *pl)
2292 sfp_upstream_stop(pl->sfp_bus);
2294 phy_stop(pl->phydev);
2295 del_timer_sync(&pl->link_poll);
2297 free_irq(pl->link_irq, pl);
2301 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
2303 pl->pcs_state = PCS_STATE_DOWN;
2305 phylink_pcs_disable(pl->pcs);
2307 EXPORT_SYMBOL_GPL(phylink_stop);
2310 * phylink_suspend() - handle a network device suspend event
2311 * @pl: a pointer to a &struct phylink returned from phylink_create()
2312 * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
2314 * Handle a network device suspend event. There are several cases:
2316 * - If Wake-on-Lan is not active, we can bring down the link between
2317 * the MAC and PHY by calling phylink_stop().
2318 * - If Wake-on-Lan is active, and being handled only by the PHY, we
2319 * can also bring down the link between the MAC and PHY.
2320 * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
2321 * still needs to receive packets, so we can not bring the link down.
2323 void phylink_suspend(struct phylink *pl, bool mac_wol)
2327 if (mac_wol && (!pl->netdev || pl->netdev->ethtool->wol_enabled)) {
2328 /* Wake-on-Lan enabled, MAC handling */
2329 mutex_lock(&pl->state_mutex);
2331 /* Stop the resolver bringing the link up */
2332 __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
2334 /* Disable the carrier, to prevent transmit timeouts,
2335 * but one would hope all packets have been sent. This
2336 * also means phylink_resolve() will do nothing.
2339 netif_carrier_off(pl->netdev);
2341 pl->old_link_state = false;
2343 /* We do not call mac_link_down() here as we want the
2344 * link to remain up to receive the WoL packets.
2346 mutex_unlock(&pl->state_mutex);
2351 EXPORT_SYMBOL_GPL(phylink_suspend);
2354 * phylink_resume() - handle a network device resume event
2355 * @pl: a pointer to a &struct phylink returned from phylink_create()
2357 * Undo the effects of phylink_suspend(), returning the link to an
2358 * operational state.
2360 void phylink_resume(struct phylink *pl)
2364 if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
2365 /* Wake-on-Lan enabled, MAC handling */
2367 /* Call mac_link_down() so we keep the overall state balanced.
2368 * Do this under the state_mutex lock for consistency. This
2369 * will cause a "Link Down" message to be printed during
2370 * resume, which is harmless - the true link state will be
2371 * printed when we run a resolve.
2373 mutex_lock(&pl->state_mutex);
2374 phylink_link_down(pl);
2375 mutex_unlock(&pl->state_mutex);
2377 /* Re-apply the link parameters so that all the settings get
2378 * restored to the MAC.
2380 phylink_mac_initial_config(pl, true);
2382 /* Re-enable and re-resolve the link parameters */
2383 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL);
2388 EXPORT_SYMBOL_GPL(phylink_resume);
2391 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
2392 * @pl: a pointer to a &struct phylink returned from phylink_create()
2393 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
2395 * Read the wake on lan parameters from the PHY attached to the phylink
2396 * instance specified by @pl. If no PHY is currently attached, report no
2397 * support for wake on lan.
2399 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2407 phy_ethtool_get_wol(pl->phydev, wol);
2409 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
2412 * phylink_ethtool_set_wol() - set wake on lan parameters
2413 * @pl: a pointer to a &struct phylink returned from phylink_create()
2414 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
2416 * Set the wake on lan parameters for the PHY attached to the phylink
2417 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
2420 * Returns zero on success or negative errno code.
2422 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2424 int ret = -EOPNOTSUPP;
2429 ret = phy_ethtool_set_wol(pl->phydev, wol);
2433 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
2435 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
2437 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
2439 linkmode_zero(mask);
2440 phylink_set_port_modes(mask);
2442 linkmode_and(dst, dst, mask);
2443 linkmode_or(dst, dst, b);
2446 static void phylink_get_ksettings(const struct phylink_link_state *state,
2447 struct ethtool_link_ksettings *kset)
2449 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
2450 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
2451 if (kset->base.rate_matching == RATE_MATCH_NONE) {
2452 kset->base.speed = state->speed;
2453 kset->base.duplex = state->duplex;
2455 kset->base.autoneg = linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2456 state->advertising) ?
2457 AUTONEG_ENABLE : AUTONEG_DISABLE;
2461 * phylink_ethtool_ksettings_get() - get the current link settings
2462 * @pl: a pointer to a &struct phylink returned from phylink_create()
2463 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
2465 * Read the current link settings for the phylink instance specified by @pl.
2466 * This will be the link settings read from the MAC, PHY or fixed link
2467 * settings depending on the current negotiation mode.
2469 int phylink_ethtool_ksettings_get(struct phylink *pl,
2470 struct ethtool_link_ksettings *kset)
2472 struct phylink_link_state link_state;
2477 phy_ethtool_ksettings_get(pl->phydev, kset);
2479 kset->base.port = pl->link_port;
2481 linkmode_copy(kset->link_modes.supported, pl->supported);
2483 switch (pl->cur_link_an_mode) {
2485 /* We are using fixed settings. Report these as the
2486 * current link settings - and note that these also
2487 * represent the supported speeds/duplex/pause modes.
2489 phylink_get_fixed_state(pl, &link_state);
2490 phylink_get_ksettings(&link_state, kset);
2494 /* If there is a phy attached, then use the reported
2495 * settings from the phy with no modification.
2500 phylink_mac_pcs_get_state(pl, &link_state);
2502 /* The MAC is reporting the link results from its own PCS
2503 * layer via in-band status. Report these as the current
2506 phylink_get_ksettings(&link_state, kset);
2512 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
2515 * phylink_ethtool_ksettings_set() - set the link settings
2516 * @pl: a pointer to a &struct phylink returned from phylink_create()
2517 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
2519 int phylink_ethtool_ksettings_set(struct phylink *pl,
2520 const struct ethtool_link_ksettings *kset)
2522 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2523 struct phylink_link_state config;
2524 const struct phy_setting *s;
2529 struct ethtool_link_ksettings phy_kset = *kset;
2531 linkmode_and(phy_kset.link_modes.advertising,
2532 phy_kset.link_modes.advertising,
2535 /* We can rely on phylib for this update; we also do not need
2536 * to update the pl->link_config settings:
2537 * - the configuration returned via ksettings_get() will come
2538 * from phylib whenever a PHY is present.
2539 * - link_config.interface will be updated by the PHY calling
2540 * back via phylink_phy_change() and a subsequent resolve.
2541 * - initial link configuration for PHY mode comes from the
2542 * last phy state updated via phylink_phy_change().
2543 * - other configuration changes (e.g. pause modes) are
2544 * performed directly via phylib.
2545 * - if in in-band mode with a PHY, the link configuration
2546 * is passed on the link from the PHY, and all of
2547 * link_config.{speed,duplex,an_enabled,pause} are not used.
2548 * - the only possible use would be link_config.advertising
2549 * pause modes when in 1000base-X mode with a PHY, but in
2550 * the presence of a PHY, this should not be changed as that
2551 * should be determined from the media side advertisement.
2553 return phy_ethtool_ksettings_set(pl->phydev, &phy_kset);
2556 config = pl->link_config;
2557 /* Mask out unsupported advertisements */
2558 linkmode_and(config.advertising, kset->link_modes.advertising,
2561 /* FIXME: should we reject autoneg if phy/mac does not support it? */
2562 switch (kset->base.autoneg) {
2563 case AUTONEG_DISABLE:
2564 /* Autonegotiation disabled, select a suitable speed and
2567 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
2568 pl->supported, false);
2572 /* If we have a fixed link, refuse to change link parameters.
2573 * If the link parameters match, accept them but do nothing.
2575 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
2576 if (s->speed != pl->link_config.speed ||
2577 s->duplex != pl->link_config.duplex)
2582 config.speed = s->speed;
2583 config.duplex = s->duplex;
2586 case AUTONEG_ENABLE:
2587 /* If we have a fixed link, allow autonegotiation (since that
2588 * is our default case) but do not allow the advertisement to
2589 * be changed. If the advertisement matches, simply return.
2591 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
2592 if (!linkmode_equal(config.advertising,
2593 pl->link_config.advertising))
2598 config.speed = SPEED_UNKNOWN;
2599 config.duplex = DUPLEX_UNKNOWN;
2606 /* We have ruled out the case with a PHY attached, and the
2607 * fixed-link cases. All that is left are in-band links.
2609 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
2610 kset->base.autoneg == AUTONEG_ENABLE);
2612 /* If this link is with an SFP, ensure that changes to advertised modes
2613 * also cause the associated interface to be selected such that the
2614 * link can be configured correctly.
2617 config.interface = sfp_select_interface(pl->sfp_bus,
2618 config.advertising);
2619 if (config.interface == PHY_INTERFACE_MODE_NA) {
2621 "selection of interface failed, advertisement %*pb\n",
2622 __ETHTOOL_LINK_MODE_MASK_NBITS,
2623 config.advertising);
2627 /* Revalidate with the selected interface */
2628 linkmode_copy(support, pl->supported);
2629 if (phylink_validate(pl, support, &config)) {
2630 phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
2631 phylink_an_mode_str(pl->cur_link_an_mode),
2632 phy_modes(config.interface),
2633 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2637 /* Validate without changing the current supported mask. */
2638 linkmode_copy(support, pl->supported);
2639 if (phylink_validate(pl, support, &config))
2643 /* If autonegotiation is enabled, we must have an advertisement */
2644 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2645 config.advertising) &&
2646 phylink_is_empty_linkmode(config.advertising))
2649 mutex_lock(&pl->state_mutex);
2650 pl->link_config.speed = config.speed;
2651 pl->link_config.duplex = config.duplex;
2653 if (pl->link_config.interface != config.interface) {
2654 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
2655 /* We need to force the link down, then change the interface */
2656 if (pl->old_link_state) {
2657 phylink_link_down(pl);
2658 pl->old_link_state = false;
2660 if (!test_bit(PHYLINK_DISABLE_STOPPED,
2661 &pl->phylink_disable_state))
2662 phylink_major_config(pl, false, &config);
2663 pl->link_config.interface = config.interface;
2664 linkmode_copy(pl->link_config.advertising, config.advertising);
2665 } else if (!linkmode_equal(pl->link_config.advertising,
2666 config.advertising)) {
2667 linkmode_copy(pl->link_config.advertising, config.advertising);
2668 phylink_change_inband_advert(pl);
2670 mutex_unlock(&pl->state_mutex);
2674 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
2677 * phylink_ethtool_nway_reset() - restart negotiation
2678 * @pl: a pointer to a &struct phylink returned from phylink_create()
2680 * Restart negotiation for the phylink instance specified by @pl. This will
2681 * cause any attached phy to restart negotiation with the link partner, and
2682 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
2685 * Returns zero on success, or negative error code.
2687 int phylink_ethtool_nway_reset(struct phylink *pl)
2694 ret = phy_restart_aneg(pl->phydev);
2695 phylink_pcs_an_restart(pl);
2699 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
2702 * phylink_ethtool_get_pauseparam() - get the current pause parameters
2703 * @pl: a pointer to a &struct phylink returned from phylink_create()
2704 * @pause: a pointer to a &struct ethtool_pauseparam
2706 void phylink_ethtool_get_pauseparam(struct phylink *pl,
2707 struct ethtool_pauseparam *pause)
2711 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
2712 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
2713 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
2715 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
2718 * phylink_ethtool_set_pauseparam() - set the current pause parameters
2719 * @pl: a pointer to a &struct phylink returned from phylink_create()
2720 * @pause: a pointer to a &struct ethtool_pauseparam
2722 int phylink_ethtool_set_pauseparam(struct phylink *pl,
2723 struct ethtool_pauseparam *pause)
2725 struct phylink_link_state *config = &pl->link_config;
2726 bool manual_changed;
2731 if (pl->cur_link_an_mode == MLO_AN_FIXED)
2734 if (!phylink_test(pl->supported, Pause) &&
2735 !phylink_test(pl->supported, Asym_Pause))
2738 if (!phylink_test(pl->supported, Asym_Pause) &&
2739 pause->rx_pause != pause->tx_pause)
2744 pause_state |= MLO_PAUSE_AN;
2745 if (pause->rx_pause)
2746 pause_state |= MLO_PAUSE_RX;
2747 if (pause->tx_pause)
2748 pause_state |= MLO_PAUSE_TX;
2750 mutex_lock(&pl->state_mutex);
2752 * See the comments for linkmode_set_pause(), wrt the deficiencies
2753 * with the current implementation. A solution to this issue would
2755 * ethtool Local device
2756 * rx tx Pause AsymDir
2761 * and then use the ethtool rx/tx enablement status to mask the
2762 * rx/tx pause resolution.
2764 linkmode_set_pause(config->advertising, pause->tx_pause,
2767 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
2768 (!(pause_state & MLO_PAUSE_AN) &&
2769 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
2771 config->pause = pause_state;
2773 /* Update our in-band advertisement, triggering a renegotiation if
2774 * the advertisement changed.
2777 phylink_change_inband_advert(pl);
2779 mutex_unlock(&pl->state_mutex);
2781 /* If we have a PHY, a change of the pause frame advertisement will
2782 * cause phylib to renegotiate (if AN is enabled) which will in turn
2783 * call our phylink_phy_change() and trigger a resolve. Note that
2784 * we can't hold our state mutex while calling phy_set_asym_pause().
2787 phy_set_asym_pause(pl->phydev, pause->rx_pause,
2790 /* If the manual pause settings changed, make sure we trigger a
2791 * resolve to update their state; we can not guarantee that the
2794 if (manual_changed) {
2795 pl->mac_link_dropped = true;
2796 phylink_run_resolve(pl);
2801 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
2804 * phylink_get_eee_err() - read the energy efficient ethernet error
2806 * @pl: a pointer to a &struct phylink returned from phylink_create().
2808 * Read the Energy Efficient Ethernet error counter from the PHY associated
2809 * with the phylink instance specified by @pl.
2811 * Returns positive error counter value, or negative error code.
2813 int phylink_get_eee_err(struct phylink *pl)
2820 ret = phy_get_eee_err(pl->phydev);
2824 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
2827 * phylink_init_eee() - init and check the EEE features
2828 * @pl: a pointer to a &struct phylink returned from phylink_create()
2829 * @clk_stop_enable: allow PHY to stop receive clock
2831 * Must be called either with RTNL held or within mac_link_up()
2833 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
2835 int ret = -EOPNOTSUPP;
2838 ret = phy_init_eee(pl->phydev, clk_stop_enable);
2842 EXPORT_SYMBOL_GPL(phylink_init_eee);
2845 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
2846 * @pl: a pointer to a &struct phylink returned from phylink_create()
2847 * @eee: a pointer to a &struct ethtool_keee for the read parameters
2849 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_keee *eee)
2851 int ret = -EOPNOTSUPP;
2856 ret = phy_ethtool_get_eee(pl->phydev, eee);
2860 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
2863 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
2864 * @pl: a pointer to a &struct phylink returned from phylink_create()
2865 * @eee: a pointer to a &struct ethtool_keee for the desired parameters
2867 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_keee *eee)
2869 int ret = -EOPNOTSUPP;
2874 ret = phy_ethtool_set_eee(pl->phydev, eee);
2878 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
2880 /* This emulates MII registers for a fixed-mode phy operating as per the
2881 * passed in state. "aneg" defines if we report negotiation is possible.
2883 * FIXME: should deal with negotiation state too.
2885 static int phylink_mii_emul_read(unsigned int reg,
2886 struct phylink_link_state *state)
2888 struct fixed_phy_status fs;
2889 unsigned long *lpa = state->lp_advertising;
2892 fs.link = state->link;
2893 fs.speed = state->speed;
2894 fs.duplex = state->duplex;
2895 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
2896 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
2898 val = swphy_read_reg(reg, &fs);
2899 if (reg == MII_BMSR) {
2900 if (!state->an_complete)
2901 val &= ~BMSR_ANEGCOMPLETE;
2906 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
2909 struct phy_device *phydev = pl->phydev;
2912 if (mdio_phy_id_is_c45(phy_id)) {
2913 prtad = mdio_phy_id_prtad(phy_id);
2914 devad = mdio_phy_id_devad(phy_id);
2915 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
2919 if (phydev->is_c45) {
2925 devad = __ffs(phydev->c45_ids.mmds_present);
2929 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2931 devad = MDIO_MMD_AN;
2932 if (reg == MII_ADVERTISE)
2933 reg = MDIO_AN_ADVERTISE;
2941 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
2945 return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg);
2948 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
2949 unsigned int reg, unsigned int val)
2951 struct phy_device *phydev = pl->phydev;
2954 if (mdio_phy_id_is_c45(phy_id)) {
2955 prtad = mdio_phy_id_prtad(phy_id);
2956 devad = mdio_phy_id_devad(phy_id);
2957 return mdiobus_c45_write(pl->phydev->mdio.bus, prtad, devad,
2961 if (phydev->is_c45) {
2967 devad = __ffs(phydev->c45_ids.mmds_present);
2971 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2973 devad = MDIO_MMD_AN;
2974 if (reg == MII_ADVERTISE)
2975 reg = MDIO_AN_ADVERTISE;
2982 return mdiobus_c45_write(pl->phydev->mdio.bus, phy_id, devad,
2986 return mdiobus_write(phydev->mdio.bus, phy_id, reg, val);
2989 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
2992 struct phylink_link_state state;
2995 switch (pl->cur_link_an_mode) {
2998 phylink_get_fixed_state(pl, &state);
2999 val = phylink_mii_emul_read(reg, &state);
3008 phylink_mac_pcs_get_state(pl, &state);
3009 val = phylink_mii_emul_read(reg, &state);
3014 return val & 0xffff;
3017 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
3018 unsigned int reg, unsigned int val)
3020 switch (pl->cur_link_an_mode) {
3035 * phylink_mii_ioctl() - generic mii ioctl interface
3036 * @pl: a pointer to a &struct phylink returned from phylink_create()
3037 * @ifr: a pointer to a &struct ifreq for socket ioctls
3038 * @cmd: ioctl cmd to execute
3040 * Perform the specified MII ioctl on the PHY attached to the phylink instance
3041 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
3043 * Returns: zero on success or negative error code.
3046 * read register from the current PHY.
3048 * read register from the specified PHY.
3050 * set a register on the specified PHY.
3052 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
3054 struct mii_ioctl_data *mii = if_mii(ifr);
3060 /* PHYs only exist for MLO_AN_PHY and SGMII */
3063 mii->phy_id = pl->phydev->mdio.addr;
3067 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
3075 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
3080 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
3090 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
3098 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
3110 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
3113 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
3115 * @pl: a pointer to a &struct phylink returned from phylink_create()
3116 * @sync: perform action synchronously
3118 * If we have a PHY that is not part of a SFP module, then set the speed
3119 * as described in the phy_speed_down() function. Please see this function
3120 * for a description of the @sync parameter.
3122 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
3124 int phylink_speed_down(struct phylink *pl, bool sync)
3130 if (!pl->sfp_bus && pl->phydev)
3131 ret = phy_speed_down(pl->phydev, sync);
3135 EXPORT_SYMBOL_GPL(phylink_speed_down);
3138 * phylink_speed_up() - restore the advertised speeds prior to the call to
3139 * phylink_speed_down()
3140 * @pl: a pointer to a &struct phylink returned from phylink_create()
3142 * If we have a PHY that is not part of a SFP module, then restore the
3143 * PHY speeds as per phy_speed_up().
3145 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
3147 int phylink_speed_up(struct phylink *pl)
3153 if (!pl->sfp_bus && pl->phydev)
3154 ret = phy_speed_up(pl->phydev);
3158 EXPORT_SYMBOL_GPL(phylink_speed_up);
3160 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
3162 struct phylink *pl = upstream;
3164 pl->netdev->sfp_bus = bus;
3167 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
3169 struct phylink *pl = upstream;
3171 pl->netdev->sfp_bus = NULL;
3174 static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
3175 const unsigned long *intf)
3177 phy_interface_t interface;
3180 interface = PHY_INTERFACE_MODE_NA;
3181 for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++)
3182 if (test_bit(phylink_sfp_interface_preference[i], intf)) {
3183 interface = phylink_sfp_interface_preference[i];
3190 static void phylink_sfp_set_config(struct phylink *pl, u8 mode,
3191 unsigned long *supported,
3192 struct phylink_link_state *state)
3194 bool changed = false;
3196 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
3197 phylink_an_mode_str(mode), phy_modes(state->interface),
3198 __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
3200 if (!linkmode_equal(pl->supported, supported)) {
3201 linkmode_copy(pl->supported, supported);
3205 if (!linkmode_equal(pl->link_config.advertising, state->advertising)) {
3206 linkmode_copy(pl->link_config.advertising, state->advertising);
3210 if (pl->cur_link_an_mode != mode ||
3211 pl->link_config.interface != state->interface) {
3212 pl->cur_link_an_mode = mode;
3213 pl->link_config.interface = state->interface;
3217 phylink_info(pl, "switched to %s/%s link mode\n",
3218 phylink_an_mode_str(mode),
3219 phy_modes(state->interface));
3222 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
3223 &pl->phylink_disable_state))
3224 phylink_mac_initial_config(pl, false);
3227 static int phylink_sfp_config_phy(struct phylink *pl, u8 mode,
3228 struct phy_device *phy)
3230 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
3231 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
3232 struct phylink_link_state config;
3233 phy_interface_t iface;
3236 linkmode_copy(support, phy->supported);
3238 memset(&config, 0, sizeof(config));
3239 linkmode_copy(config.advertising, phy->advertising);
3240 config.interface = PHY_INTERFACE_MODE_NA;
3241 config.speed = SPEED_UNKNOWN;
3242 config.duplex = DUPLEX_UNKNOWN;
3243 config.pause = MLO_PAUSE_AN;
3245 /* Ignore errors if we're expecting a PHY to attach later */
3246 ret = phylink_validate(pl, support, &config);
3248 phylink_err(pl, "validation with support %*pb failed: %pe\n",
3249 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3254 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
3255 if (iface == PHY_INTERFACE_MODE_NA) {
3257 "selection of interface failed, advertisement %*pb\n",
3258 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
3262 config.interface = iface;
3263 linkmode_copy(support1, support);
3264 ret = phylink_validate(pl, support1, &config);
3267 "validation of %s/%s with support %*pb failed: %pe\n",
3268 phylink_an_mode_str(mode),
3269 phy_modes(config.interface),
3270 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3275 pl->link_port = pl->sfp_port;
3277 phylink_sfp_set_config(pl, mode, support, &config);
3282 static int phylink_sfp_config_optical(struct phylink *pl)
3284 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
3285 DECLARE_PHY_INTERFACE_MASK(interfaces);
3286 struct phylink_link_state config;
3287 phy_interface_t interface;
3290 phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n",
3291 (int)PHY_INTERFACE_MODE_MAX,
3292 pl->config->supported_interfaces,
3293 (int)PHY_INTERFACE_MODE_MAX,
3294 pl->sfp_interfaces);
3296 /* Find the union of the supported interfaces by the PCS/MAC and
3299 phy_interface_and(interfaces, pl->config->supported_interfaces,
3300 pl->sfp_interfaces);
3301 if (phy_interface_empty(interfaces)) {
3302 phylink_err(pl, "unsupported SFP module: no common interface modes\n");
3306 memset(&config, 0, sizeof(config));
3307 linkmode_copy(support, pl->sfp_support);
3308 linkmode_copy(config.advertising, pl->sfp_support);
3309 config.speed = SPEED_UNKNOWN;
3310 config.duplex = DUPLEX_UNKNOWN;
3311 config.pause = MLO_PAUSE_AN;
3313 /* For all the interfaces that are supported, reduce the sfp_support
3314 * mask to only those link modes that can be supported.
3316 ret = phylink_validate_mask(pl, NULL, pl->sfp_support, &config,
3319 phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
3320 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
3324 interface = phylink_choose_sfp_interface(pl, interfaces);
3325 if (interface == PHY_INTERFACE_MODE_NA) {
3326 phylink_err(pl, "failed to select SFP interface\n");
3330 phylink_dbg(pl, "optical SFP: chosen %s interface\n",
3331 phy_modes(interface));
3333 config.interface = interface;
3335 /* Ignore errors if we're expecting a PHY to attach later */
3336 ret = phylink_validate(pl, support, &config);
3338 phylink_err(pl, "validation with support %*pb failed: %pe\n",
3339 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3344 pl->link_port = pl->sfp_port;
3346 phylink_sfp_set_config(pl, MLO_AN_INBAND, pl->sfp_support, &config);
3351 static int phylink_sfp_module_insert(void *upstream,
3352 const struct sfp_eeprom_id *id)
3354 struct phylink *pl = upstream;
3358 linkmode_zero(pl->sfp_support);
3359 phy_interface_zero(pl->sfp_interfaces);
3360 sfp_parse_support(pl->sfp_bus, id, pl->sfp_support, pl->sfp_interfaces);
3361 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, pl->sfp_support);
3363 /* If this module may have a PHY connecting later, defer until later */
3364 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
3365 if (pl->sfp_may_have_phy)
3368 return phylink_sfp_config_optical(pl);
3371 static int phylink_sfp_module_start(void *upstream)
3373 struct phylink *pl = upstream;
3375 /* If this SFP module has a PHY, start the PHY now. */
3377 phy_start(pl->phydev);
3381 /* If the module may have a PHY but we didn't detect one we
3382 * need to configure the MAC here.
3384 if (!pl->sfp_may_have_phy)
3387 return phylink_sfp_config_optical(pl);
3390 static void phylink_sfp_module_stop(void *upstream)
3392 struct phylink *pl = upstream;
3394 /* If this SFP module has a PHY, stop it. */
3396 phy_stop(pl->phydev);
3399 static void phylink_sfp_link_down(void *upstream)
3401 struct phylink *pl = upstream;
3405 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
3408 static void phylink_sfp_link_up(void *upstream)
3410 struct phylink *pl = upstream;
3414 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
3417 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
3418 * or 802.3z control word, so inband will not work.
3420 static bool phylink_phy_no_inband(struct phy_device *phy)
3422 return phy->is_c45 && phy_id_compare(phy->c45_ids.device_ids[1],
3423 0xae025150, 0xfffffff0);
3426 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
3428 struct phylink *pl = upstream;
3429 phy_interface_t interface;
3434 * This is the new way of dealing with flow control for PHYs,
3435 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
3436 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
3437 * using our validate call to the MAC, we rely upon the MAC
3438 * clearing the bits from both supported and advertising fields.
3440 phy_support_asym_pause(phy);
3442 if (phylink_phy_no_inband(phy))
3445 mode = MLO_AN_INBAND;
3447 /* Set the PHY's host supported interfaces */
3448 phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
3449 pl->config->supported_interfaces);
3451 /* Do the initial configuration */
3452 ret = phylink_sfp_config_phy(pl, mode, phy);
3456 interface = pl->link_config.interface;
3457 ret = phylink_attach_phy(pl, phy, interface);
3461 ret = phylink_bringup_phy(pl, phy, interface);
3468 static void phylink_sfp_disconnect_phy(void *upstream,
3469 struct phy_device *phydev)
3471 phylink_disconnect_phy(upstream);
3474 static const struct sfp_upstream_ops sfp_phylink_ops = {
3475 .attach = phylink_sfp_attach,
3476 .detach = phylink_sfp_detach,
3477 .module_insert = phylink_sfp_module_insert,
3478 .module_start = phylink_sfp_module_start,
3479 .module_stop = phylink_sfp_module_stop,
3480 .link_up = phylink_sfp_link_up,
3481 .link_down = phylink_sfp_link_down,
3482 .connect_phy = phylink_sfp_connect_phy,
3483 .disconnect_phy = phylink_sfp_disconnect_phy,
3486 /* Helpers for MAC drivers */
3491 } phylink_c73_priority_resolution[] = {
3492 { ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, SPEED_100000 },
3493 { ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, SPEED_100000 },
3494 /* 100GBASE-KP4 and 100GBASE-CR10 not supported */
3495 { ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, SPEED_40000 },
3496 { ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, SPEED_40000 },
3497 { ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, SPEED_10000 },
3498 { ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, SPEED_10000 },
3499 /* 5GBASE-KR not supported */
3500 { ETHTOOL_LINK_MODE_2500baseX_Full_BIT, SPEED_2500 },
3501 { ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, SPEED_1000 },
3504 void phylink_resolve_c73(struct phylink_link_state *state)
3508 for (i = 0; i < ARRAY_SIZE(phylink_c73_priority_resolution); i++) {
3509 int bit = phylink_c73_priority_resolution[i].bit;
3510 if (linkmode_test_bit(bit, state->advertising) &&
3511 linkmode_test_bit(bit, state->lp_advertising))
3515 if (i < ARRAY_SIZE(phylink_c73_priority_resolution)) {
3516 state->speed = phylink_c73_priority_resolution[i].speed;
3517 state->duplex = DUPLEX_FULL;
3519 /* negotiation failure */
3520 state->link = false;
3523 phylink_resolve_an_pause(state);
3525 EXPORT_SYMBOL_GPL(phylink_resolve_c73);
3527 static void phylink_decode_c37_word(struct phylink_link_state *state,
3528 uint16_t config_reg, int speed)
3532 if (speed == SPEED_2500)
3533 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
3535 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
3537 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
3539 if (linkmode_test_bit(fd_bit, state->advertising) &&
3540 linkmode_test_bit(fd_bit, state->lp_advertising)) {
3541 state->speed = speed;
3542 state->duplex = DUPLEX_FULL;
3544 /* negotiation failure */
3545 state->link = false;
3548 phylink_resolve_an_pause(state);
3551 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
3552 uint16_t config_reg)
3554 if (!(config_reg & LPA_SGMII_LINK)) {
3555 state->link = false;
3559 switch (config_reg & LPA_SGMII_SPD_MASK) {
3561 state->speed = SPEED_10;
3564 state->speed = SPEED_100;
3566 case LPA_SGMII_1000:
3567 state->speed = SPEED_1000;
3570 state->link = false;
3573 if (config_reg & LPA_SGMII_FULL_DUPLEX)
3574 state->duplex = DUPLEX_FULL;
3576 state->duplex = DUPLEX_HALF;
3580 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
3581 * @state: a pointer to a struct phylink_link_state.
3582 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
3584 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
3585 * code word. Decode the USXGMII code word and populate the corresponding fields
3586 * (speed, duplex) into the phylink_link_state structure.
3588 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
3591 switch (lpa & MDIO_USXGMII_SPD_MASK) {
3592 case MDIO_USXGMII_10:
3593 state->speed = SPEED_10;
3595 case MDIO_USXGMII_100:
3596 state->speed = SPEED_100;
3598 case MDIO_USXGMII_1000:
3599 state->speed = SPEED_1000;
3601 case MDIO_USXGMII_2500:
3602 state->speed = SPEED_2500;
3604 case MDIO_USXGMII_5000:
3605 state->speed = SPEED_5000;
3607 case MDIO_USXGMII_10G:
3608 state->speed = SPEED_10000;
3611 state->link = false;
3615 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
3616 state->duplex = DUPLEX_FULL;
3618 state->duplex = DUPLEX_HALF;
3620 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
3623 * phylink_decode_usgmii_word() - decode the USGMII word from a MAC PCS
3624 * @state: a pointer to a struct phylink_link_state.
3625 * @lpa: a 16 bit value which stores the USGMII auto-negotiation word
3627 * Helper for MAC PCS supporting the USGMII protocol and the auto-negotiation
3628 * code word. Decode the USGMII code word and populate the corresponding fields
3629 * (speed, duplex) into the phylink_link_state structure. The structure for this
3630 * word is the same as the USXGMII word, except it only supports speeds up to
3633 static void phylink_decode_usgmii_word(struct phylink_link_state *state,
3636 switch (lpa & MDIO_USXGMII_SPD_MASK) {
3637 case MDIO_USXGMII_10:
3638 state->speed = SPEED_10;
3640 case MDIO_USXGMII_100:
3641 state->speed = SPEED_100;
3643 case MDIO_USXGMII_1000:
3644 state->speed = SPEED_1000;
3647 state->link = false;
3651 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
3652 state->duplex = DUPLEX_FULL;
3654 state->duplex = DUPLEX_HALF;
3658 * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
3659 * @state: a pointer to a &struct phylink_link_state.
3660 * @bmsr: The value of the %MII_BMSR register
3661 * @lpa: The value of the %MII_LPA register
3663 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3664 * clause 37 negotiation and/or SGMII control.
3666 * Parse the Clause 37 or Cisco SGMII link partner negotiation word into
3667 * the phylink @state structure. This is suitable to be used for implementing
3668 * the pcs_get_state() member of the struct phylink_pcs_ops structure if
3669 * accessing @bmsr and @lpa cannot be done with MDIO directly.
3671 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
3674 state->link = !!(bmsr & BMSR_LSTATUS);
3675 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
3676 /* If there is no link or autonegotiation is disabled, the LP advertisement
3677 * data is not meaningful, so don't go any further.
3679 if (!state->link || !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3680 state->advertising))
3683 switch (state->interface) {
3684 case PHY_INTERFACE_MODE_1000BASEX:
3685 phylink_decode_c37_word(state, lpa, SPEED_1000);
3688 case PHY_INTERFACE_MODE_2500BASEX:
3689 phylink_decode_c37_word(state, lpa, SPEED_2500);
3692 case PHY_INTERFACE_MODE_SGMII:
3693 case PHY_INTERFACE_MODE_QSGMII:
3694 phylink_decode_sgmii_word(state, lpa);
3696 case PHY_INTERFACE_MODE_QUSGMII:
3697 phylink_decode_usgmii_word(state, lpa);
3701 state->link = false;
3705 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state);
3708 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
3709 * @pcs: a pointer to a &struct mdio_device.
3710 * @state: a pointer to a &struct phylink_link_state.
3712 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3713 * clause 37 negotiation and/or SGMII control.
3715 * Read the MAC PCS state from the MII device configured in @config and
3716 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
3717 * the phylink @state structure. This is suitable to be directly plugged
3718 * into the pcs_get_state() member of the struct phylink_pcs_ops
3721 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
3722 struct phylink_link_state *state)
3726 bmsr = mdiodev_read(pcs, MII_BMSR);
3727 lpa = mdiodev_read(pcs, MII_LPA);
3728 if (bmsr < 0 || lpa < 0) {
3729 state->link = false;
3733 phylink_mii_c22_pcs_decode_state(state, bmsr, lpa);
3735 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
3738 * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
3740 * @interface: the PHY interface mode being configured
3741 * @advertising: the ethtool advertisement mask
3743 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3744 * clause 37 negotiation and/or SGMII control.
3746 * Encode the clause 37 PCS advertisement as specified by @interface and
3749 * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
3751 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
3752 const unsigned long *advertising)
3756 switch (interface) {
3757 case PHY_INTERFACE_MODE_1000BASEX:
3758 case PHY_INTERFACE_MODE_2500BASEX:
3759 adv = ADVERTISE_1000XFULL;
3760 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3762 adv |= ADVERTISE_1000XPAUSE;
3763 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3765 adv |= ADVERTISE_1000XPSE_ASYM;
3767 case PHY_INTERFACE_MODE_SGMII:
3768 case PHY_INTERFACE_MODE_QSGMII:
3771 /* Nothing to do for other modes */
3775 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement);
3778 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
3779 * @pcs: a pointer to a &struct mdio_device.
3780 * @interface: the PHY interface mode being configured
3781 * @advertising: the ethtool advertisement mask
3782 * @neg_mode: PCS negotiation mode
3784 * Configure a Clause 22 PCS PHY with the appropriate negotiation
3785 * parameters for the @mode, @interface and @advertising parameters.
3786 * Returns negative error number on failure, zero if the advertisement
3787 * has not changed, or positive if there is a change.
3789 int phylink_mii_c22_pcs_config(struct mdio_device *pcs,
3790 phy_interface_t interface,
3791 const unsigned long *advertising,
3792 unsigned int neg_mode)
3798 adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
3800 ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
3801 MII_ADVERTISE, 0xffff, adv);
3807 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
3808 bmcr = BMCR_ANENABLE;
3812 /* Configure the inband state. Ensure ISOLATE bit is disabled */
3813 ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
3819 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
3822 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
3823 * @pcs: a pointer to a &struct mdio_device.
3825 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3826 * clause 37 negotiation.
3828 * Restart the clause 37 negotiation with the link partner. This is
3829 * suitable to be directly plugged into the pcs_get_state() member
3830 * of the struct phylink_pcs_ops structure.
3832 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
3834 int val = mdiodev_read(pcs, MII_BMCR);
3837 val |= BMCR_ANRESTART;
3839 mdiodev_write(pcs, MII_BMCR, val);
3842 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
3844 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
3845 struct phylink_link_state *state)
3847 struct mii_bus *bus = pcs->bus;
3848 int addr = pcs->addr;
3851 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
3853 state->link = false;
3857 state->link = !!(stat & MDIO_STAT1_LSTATUS);
3861 switch (state->interface) {
3862 case PHY_INTERFACE_MODE_10GBASER:
3863 state->speed = SPEED_10000;
3864 state->duplex = DUPLEX_FULL;
3871 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
3873 static int __init phylink_init(void)
3875 for (int i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); ++i)
3876 __set_bit(phylink_sfp_interface_preference[i],
3877 phylink_sfp_interfaces);
3882 module_init(phylink_init);
3884 MODULE_LICENSE("GPL v2");
3885 MODULE_DESCRIPTION("phylink models the MAC to optional PHY connection");