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_create() - create a phylink instance
1640 * @config: a pointer to the target &struct phylink_config
1641 * @fwnode: a pointer to a &struct fwnode_handle describing the network
1643 * @iface: the desired link mode defined by &typedef phy_interface_t
1644 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
1646 * Create a new phylink instance, and parse the link parameters found in @np.
1647 * This will parse in-band modes, fixed-link or SFP configuration.
1649 * Note: the rtnl lock must not be held when calling this function.
1651 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
1652 * must use IS_ERR() to check for errors from this function.
1654 struct phylink *phylink_create(struct phylink_config *config,
1655 const struct fwnode_handle *fwnode,
1656 phy_interface_t iface,
1657 const struct phylink_mac_ops *mac_ops)
1659 bool using_mac_select_pcs = false;
1663 /* Validate the supplied configuration */
1664 if (phy_interface_empty(config->supported_interfaces)) {
1665 dev_err(config->dev,
1666 "phylink: error: empty supported_interfaces\n");
1667 return ERR_PTR(-EINVAL);
1670 if (mac_ops->mac_select_pcs &&
1671 mac_ops->mac_select_pcs(config, PHY_INTERFACE_MODE_NA) !=
1672 ERR_PTR(-EOPNOTSUPP))
1673 using_mac_select_pcs = true;
1675 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
1677 return ERR_PTR(-ENOMEM);
1679 mutex_init(&pl->state_mutex);
1680 INIT_WORK(&pl->resolve, phylink_resolve);
1682 pl->config = config;
1683 if (config->type == PHYLINK_NETDEV) {
1684 pl->netdev = to_net_dev(config->dev);
1685 netif_carrier_off(pl->netdev);
1686 } else if (config->type == PHYLINK_DEV) {
1687 pl->dev = config->dev;
1690 return ERR_PTR(-EINVAL);
1693 pl->using_mac_select_pcs = using_mac_select_pcs;
1694 pl->phy_state.interface = iface;
1695 pl->link_interface = iface;
1696 if (iface == PHY_INTERFACE_MODE_MOCA)
1697 pl->link_port = PORT_BNC;
1699 pl->link_port = PORT_MII;
1700 pl->link_config.interface = iface;
1701 pl->link_config.pause = MLO_PAUSE_AN;
1702 pl->link_config.speed = SPEED_UNKNOWN;
1703 pl->link_config.duplex = DUPLEX_UNKNOWN;
1704 pl->pcs_state = PCS_STATE_DOWN;
1705 pl->mac_ops = mac_ops;
1706 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1707 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
1709 linkmode_fill(pl->supported);
1710 linkmode_copy(pl->link_config.advertising, pl->supported);
1711 phylink_validate(pl, pl->supported, &pl->link_config);
1713 ret = phylink_parse_mode(pl, fwnode);
1716 return ERR_PTR(ret);
1719 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
1720 ret = phylink_parse_fixedlink(pl, fwnode);
1723 return ERR_PTR(ret);
1727 pl->cur_link_an_mode = pl->cfg_link_an_mode;
1729 ret = phylink_register_sfp(pl, fwnode);
1732 return ERR_PTR(ret);
1737 EXPORT_SYMBOL_GPL(phylink_create);
1740 * phylink_destroy() - cleanup and destroy the phylink instance
1741 * @pl: a pointer to a &struct phylink returned from phylink_create()
1743 * Destroy a phylink instance. Any PHY that has been attached must have been
1744 * cleaned up via phylink_disconnect_phy() prior to calling this function.
1746 * Note: the rtnl lock must not be held when calling this function.
1748 void phylink_destroy(struct phylink *pl)
1750 sfp_bus_del_upstream(pl->sfp_bus);
1752 gpiod_put(pl->link_gpio);
1754 cancel_work_sync(&pl->resolve);
1757 EXPORT_SYMBOL_GPL(phylink_destroy);
1760 * phylink_expects_phy() - Determine if phylink expects a phy to be attached
1761 * @pl: a pointer to a &struct phylink returned from phylink_create()
1763 * When using fixed-link mode, or in-band mode with 1000base-X or 2500base-X,
1766 * Returns true if phylink will be expecting a PHY.
1768 bool phylink_expects_phy(struct phylink *pl)
1770 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1771 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1772 phy_interface_mode_is_8023z(pl->link_config.interface)))
1776 EXPORT_SYMBOL_GPL(phylink_expects_phy);
1778 static void phylink_phy_change(struct phy_device *phydev, bool up)
1780 struct phylink *pl = phydev->phylink;
1781 bool tx_pause, rx_pause;
1783 phy_get_pause(phydev, &tx_pause, &rx_pause);
1785 mutex_lock(&pl->state_mutex);
1786 pl->phy_state.speed = phydev->speed;
1787 pl->phy_state.duplex = phydev->duplex;
1788 pl->phy_state.rate_matching = phydev->rate_matching;
1789 pl->phy_state.pause = MLO_PAUSE_NONE;
1791 pl->phy_state.pause |= MLO_PAUSE_TX;
1793 pl->phy_state.pause |= MLO_PAUSE_RX;
1794 pl->phy_state.interface = phydev->interface;
1795 pl->phy_state.link = up;
1796 mutex_unlock(&pl->state_mutex);
1798 phylink_run_resolve(pl);
1800 phylink_dbg(pl, "phy link %s %s/%s/%s/%s/%s\n", up ? "up" : "down",
1801 phy_modes(phydev->interface),
1802 phy_speed_to_str(phydev->speed),
1803 phy_duplex_to_str(phydev->duplex),
1804 phy_rate_matching_to_str(phydev->rate_matching),
1805 phylink_pause_to_str(pl->phy_state.pause));
1808 static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
1809 unsigned long *supported,
1810 struct phylink_link_state *state)
1812 DECLARE_PHY_INTERFACE_MASK(interfaces);
1814 /* If the PHY provides a bitmap of the interfaces it will be using
1815 * depending on the negotiated media speeds, use this to validate
1816 * which ethtool link modes can be used.
1818 if (!phy_interface_empty(phy->possible_interfaces)) {
1819 /* We only care about the union of the PHY's interfaces and
1820 * those which the host supports.
1822 phy_interface_and(interfaces, phy->possible_interfaces,
1823 pl->config->supported_interfaces);
1825 if (phy_interface_empty(interfaces)) {
1826 phylink_err(pl, "PHY has no common interfaces\n");
1830 if (phy_on_sfp(phy)) {
1831 /* If the PHY is on a SFP, limit the interfaces to
1832 * those that can be used with a SFP module.
1834 phy_interface_and(interfaces, interfaces,
1835 phylink_sfp_interfaces);
1837 if (phy_interface_empty(interfaces)) {
1838 phylink_err(pl, "SFP PHY's possible interfaces becomes empty\n");
1843 phylink_dbg(pl, "PHY %s uses interfaces %*pbl, validating %*pbl\n",
1845 (int)PHY_INTERFACE_MODE_MAX,
1846 phy->possible_interfaces,
1847 (int)PHY_INTERFACE_MODE_MAX, interfaces);
1849 return phylink_validate_mask(pl, phy, supported, state,
1853 phylink_dbg(pl, "PHY %s doesn't supply possible interfaces\n",
1856 /* Check whether we would use rate matching for the proposed interface
1859 state->rate_matching = phy_get_rate_matching(phy, state->interface);
1861 /* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
1862 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
1863 * For some interface modes (e.g. RXAUI, XAUI and USXGMII) switching
1864 * their Serdes is either unnecessary or not reasonable.
1866 * For these which switch interface modes, we really need to know which
1867 * interface modes the PHY supports to properly work out which ethtool
1868 * linkmodes can be supported. For now, as a work-around, we validate
1869 * against all interface modes, which may lead to more ethtool link
1870 * modes being advertised than are actually supported.
1872 if (phy->is_c45 && state->rate_matching == RATE_MATCH_NONE &&
1873 state->interface != PHY_INTERFACE_MODE_RXAUI &&
1874 state->interface != PHY_INTERFACE_MODE_XAUI &&
1875 state->interface != PHY_INTERFACE_MODE_USXGMII)
1876 state->interface = PHY_INTERFACE_MODE_NA;
1878 return phylink_validate(pl, supported, state);
1881 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
1882 phy_interface_t interface)
1884 struct phylink_link_state config;
1885 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
1890 * This is the new way of dealing with flow control for PHYs,
1891 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
1892 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
1893 * using our validate call to the MAC, we rely upon the MAC
1894 * clearing the bits from both supported and advertising fields.
1896 phy_support_asym_pause(phy);
1898 memset(&config, 0, sizeof(config));
1899 linkmode_copy(supported, phy->supported);
1900 linkmode_copy(config.advertising, phy->advertising);
1901 config.interface = interface;
1903 ret = phylink_validate_phy(pl, phy, supported, &config);
1905 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
1906 phy_modes(config.interface),
1907 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1908 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1914 phy->phy_link_change = phylink_phy_change;
1916 irq_str = phy_attached_info_irq(phy);
1918 "PHY [%s] driver [%s] (irq=%s)\n",
1919 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1922 mutex_lock(&phy->lock);
1923 mutex_lock(&pl->state_mutex);
1925 pl->phy_state.interface = interface;
1926 pl->phy_state.pause = MLO_PAUSE_NONE;
1927 pl->phy_state.speed = SPEED_UNKNOWN;
1928 pl->phy_state.duplex = DUPLEX_UNKNOWN;
1929 pl->phy_state.rate_matching = RATE_MATCH_NONE;
1930 linkmode_copy(pl->supported, supported);
1931 linkmode_copy(pl->link_config.advertising, config.advertising);
1933 /* Restrict the phy advertisement according to the MAC support. */
1934 linkmode_copy(phy->advertising, config.advertising);
1935 mutex_unlock(&pl->state_mutex);
1936 mutex_unlock(&phy->lock);
1939 "phy: %s setting supported %*pb advertising %*pb\n",
1940 phy_modes(interface),
1941 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1942 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1944 if (phy_interrupt_is_valid(phy))
1945 phy_request_interrupt(phy);
1947 if (pl->config->mac_managed_pm)
1948 phy->mac_managed_pm = true;
1953 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1954 phy_interface_t interface)
1958 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1959 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1960 phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
1966 if (pl->config->mac_requires_rxc)
1967 flags |= PHY_F_RXC_ALWAYS_ON;
1969 return phy_attach_direct(pl->netdev, phy, flags, interface);
1973 * phylink_connect_phy() - connect a PHY to the phylink instance
1974 * @pl: a pointer to a &struct phylink returned from phylink_create()
1975 * @phy: a pointer to a &struct phy_device.
1977 * Connect @phy to the phylink instance specified by @pl by calling
1978 * phy_attach_direct(). Configure the @phy according to the MAC driver's
1979 * capabilities, start the PHYLIB state machine and enable any interrupts
1980 * that the PHY supports.
1982 * This updates the phylink's ethtool supported and advertising link mode
1985 * Returns 0 on success or a negative errno.
1987 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1991 /* Use PHY device/driver interface */
1992 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1993 pl->link_interface = phy->interface;
1994 pl->link_config.interface = pl->link_interface;
1997 ret = phylink_attach_phy(pl, phy, pl->link_interface);
2001 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
2007 EXPORT_SYMBOL_GPL(phylink_connect_phy);
2010 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
2011 * @pl: a pointer to a &struct phylink returned from phylink_create()
2012 * @dn: a pointer to a &struct device_node.
2013 * @flags: PHY-specific flags to communicate to the PHY device driver
2015 * Connect the phy specified in the device node @dn to the phylink instance
2016 * specified by @pl. Actions specified in phylink_connect_phy() will be
2019 * Returns 0 on success or a negative errno.
2021 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
2024 return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
2026 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
2029 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
2030 * @pl: a pointer to a &struct phylink returned from phylink_create()
2031 * @fwnode: a pointer to a &struct fwnode_handle.
2032 * @flags: PHY-specific flags to communicate to the PHY device driver
2034 * Connect the phy specified @fwnode to the phylink instance specified
2037 * Returns 0 on success or a negative errno.
2039 int phylink_fwnode_phy_connect(struct phylink *pl,
2040 const struct fwnode_handle *fwnode,
2043 struct fwnode_handle *phy_fwnode;
2044 struct phy_device *phy_dev;
2047 /* Fixed links and 802.3z are handled without needing a PHY */
2048 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
2049 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
2050 phy_interface_mode_is_8023z(pl->link_interface)))
2053 phy_fwnode = fwnode_get_phy_node(fwnode);
2054 if (IS_ERR(phy_fwnode)) {
2055 if (pl->cfg_link_an_mode == MLO_AN_PHY)
2060 phy_dev = fwnode_phy_find_device(phy_fwnode);
2061 /* We're done with the phy_node handle */
2062 fwnode_handle_put(phy_fwnode);
2066 /* Use PHY device/driver interface */
2067 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
2068 pl->link_interface = phy_dev->interface;
2069 pl->link_config.interface = pl->link_interface;
2072 if (pl->config->mac_requires_rxc)
2073 flags |= PHY_F_RXC_ALWAYS_ON;
2075 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
2076 pl->link_interface);
2077 phy_device_free(phy_dev);
2081 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
2083 phy_detach(phy_dev);
2087 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
2090 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
2092 * @pl: a pointer to a &struct phylink returned from phylink_create()
2094 * Disconnect any current PHY from the phylink instance described by @pl.
2096 void phylink_disconnect_phy(struct phylink *pl)
2098 struct phy_device *phy;
2104 mutex_lock(&phy->lock);
2105 mutex_lock(&pl->state_mutex);
2107 mutex_unlock(&pl->state_mutex);
2108 mutex_unlock(&phy->lock);
2109 flush_work(&pl->resolve);
2111 phy_disconnect(phy);
2114 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
2116 static void phylink_link_changed(struct phylink *pl, bool up, const char *what)
2119 pl->mac_link_dropped = true;
2120 phylink_run_resolve(pl);
2121 phylink_dbg(pl, "%s link %s\n", what, up ? "up" : "down");
2125 * phylink_mac_change() - notify phylink of a change in MAC state
2126 * @pl: a pointer to a &struct phylink returned from phylink_create()
2127 * @up: indicates whether the link is currently up.
2129 * The MAC driver should call this driver when the state of its link
2130 * changes (eg, link failure, new negotiation results, etc.)
2132 void phylink_mac_change(struct phylink *pl, bool up)
2134 phylink_link_changed(pl, up, "mac");
2136 EXPORT_SYMBOL_GPL(phylink_mac_change);
2139 * phylink_pcs_change() - notify phylink of a change to PCS link state
2140 * @pcs: pointer to &struct phylink_pcs
2141 * @up: indicates whether the link is currently up.
2143 * The PCS driver should call this when the state of its link changes
2144 * (e.g. link failure, new negotiation results, etc.) Note: it should
2145 * not determine "up" by reading the BMSR. If in doubt about the link
2146 * state at interrupt time, then pass true if pcs_get_state() returns
2147 * the latched link-down state, otherwise pass false.
2149 void phylink_pcs_change(struct phylink_pcs *pcs, bool up)
2151 struct phylink *pl = pcs->phylink;
2154 phylink_link_changed(pl, up, "pcs");
2156 EXPORT_SYMBOL_GPL(phylink_pcs_change);
2158 static irqreturn_t phylink_link_handler(int irq, void *data)
2160 struct phylink *pl = data;
2162 phylink_run_resolve(pl);
2168 * phylink_start() - start a phylink instance
2169 * @pl: a pointer to a &struct phylink returned from phylink_create()
2171 * Start the phylink instance specified by @pl, configuring the MAC for the
2172 * desired link mode(s) and negotiation style. This should be called from the
2173 * network device driver's &struct net_device_ops ndo_open() method.
2175 void phylink_start(struct phylink *pl)
2181 phylink_info(pl, "configuring for %s/%s link mode\n",
2182 phylink_an_mode_str(pl->cur_link_an_mode),
2183 phy_modes(pl->link_config.interface));
2185 /* Always set the carrier off */
2187 netif_carrier_off(pl->netdev);
2189 pl->pcs_state = PCS_STATE_STARTING;
2191 /* Apply the link configuration to the MAC when starting. This allows
2192 * a fixed-link to start with the correct parameters, and also
2193 * ensures that we set the appropriate advertisement for Serdes links.
2195 * Restart autonegotiation if using 802.3z to ensure that the link
2196 * parameters are properly negotiated. This is necessary for DSA
2197 * switches using 802.3z negotiation to ensure they see our modes.
2199 phylink_mac_initial_config(pl, true);
2201 pl->pcs_state = PCS_STATE_STARTED;
2203 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
2205 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
2206 int irq = gpiod_to_irq(pl->link_gpio);
2209 if (!request_irq(irq, phylink_link_handler,
2210 IRQF_TRIGGER_RISING |
2211 IRQF_TRIGGER_FALLING,
2221 if (pl->cfg_link_an_mode == MLO_AN_FIXED)
2222 poll |= pl->config->poll_fixed_state;
2225 mod_timer(&pl->link_poll, jiffies + HZ);
2227 phy_start(pl->phydev);
2229 sfp_upstream_start(pl->sfp_bus);
2231 EXPORT_SYMBOL_GPL(phylink_start);
2234 * phylink_stop() - stop a phylink instance
2235 * @pl: a pointer to a &struct phylink returned from phylink_create()
2237 * Stop the phylink instance specified by @pl. This should be called from the
2238 * network device driver's &struct net_device_ops ndo_stop() method. The
2239 * network device's carrier state should not be changed prior to calling this
2242 * This will synchronously bring down the link if the link is not already
2243 * down (in other words, it will trigger a mac_link_down() method call.)
2245 void phylink_stop(struct phylink *pl)
2250 sfp_upstream_stop(pl->sfp_bus);
2252 phy_stop(pl->phydev);
2253 del_timer_sync(&pl->link_poll);
2255 free_irq(pl->link_irq, pl);
2259 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
2261 pl->pcs_state = PCS_STATE_DOWN;
2263 phylink_pcs_disable(pl->pcs);
2265 EXPORT_SYMBOL_GPL(phylink_stop);
2268 * phylink_suspend() - handle a network device suspend event
2269 * @pl: a pointer to a &struct phylink returned from phylink_create()
2270 * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
2272 * Handle a network device suspend event. There are several cases:
2274 * - If Wake-on-Lan is not active, we can bring down the link between
2275 * the MAC and PHY by calling phylink_stop().
2276 * - If Wake-on-Lan is active, and being handled only by the PHY, we
2277 * can also bring down the link between the MAC and PHY.
2278 * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
2279 * still needs to receive packets, so we can not bring the link down.
2281 void phylink_suspend(struct phylink *pl, bool mac_wol)
2285 if (mac_wol && (!pl->netdev || pl->netdev->ethtool->wol_enabled)) {
2286 /* Wake-on-Lan enabled, MAC handling */
2287 mutex_lock(&pl->state_mutex);
2289 /* Stop the resolver bringing the link up */
2290 __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
2292 /* Disable the carrier, to prevent transmit timeouts,
2293 * but one would hope all packets have been sent. This
2294 * also means phylink_resolve() will do nothing.
2297 netif_carrier_off(pl->netdev);
2299 pl->old_link_state = false;
2301 /* We do not call mac_link_down() here as we want the
2302 * link to remain up to receive the WoL packets.
2304 mutex_unlock(&pl->state_mutex);
2309 EXPORT_SYMBOL_GPL(phylink_suspend);
2312 * phylink_resume() - handle a network device resume event
2313 * @pl: a pointer to a &struct phylink returned from phylink_create()
2315 * Undo the effects of phylink_suspend(), returning the link to an
2316 * operational state.
2318 void phylink_resume(struct phylink *pl)
2322 if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
2323 /* Wake-on-Lan enabled, MAC handling */
2325 /* Call mac_link_down() so we keep the overall state balanced.
2326 * Do this under the state_mutex lock for consistency. This
2327 * will cause a "Link Down" message to be printed during
2328 * resume, which is harmless - the true link state will be
2329 * printed when we run a resolve.
2331 mutex_lock(&pl->state_mutex);
2332 phylink_link_down(pl);
2333 mutex_unlock(&pl->state_mutex);
2335 /* Re-apply the link parameters so that all the settings get
2336 * restored to the MAC.
2338 phylink_mac_initial_config(pl, true);
2340 /* Re-enable and re-resolve the link parameters */
2341 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL);
2346 EXPORT_SYMBOL_GPL(phylink_resume);
2349 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
2350 * @pl: a pointer to a &struct phylink returned from phylink_create()
2351 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
2353 * Read the wake on lan parameters from the PHY attached to the phylink
2354 * instance specified by @pl. If no PHY is currently attached, report no
2355 * support for wake on lan.
2357 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2365 phy_ethtool_get_wol(pl->phydev, wol);
2367 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
2370 * phylink_ethtool_set_wol() - set wake on lan parameters
2371 * @pl: a pointer to a &struct phylink returned from phylink_create()
2372 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
2374 * Set the wake on lan parameters for the PHY attached to the phylink
2375 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
2378 * Returns zero on success or negative errno code.
2380 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
2382 int ret = -EOPNOTSUPP;
2387 ret = phy_ethtool_set_wol(pl->phydev, wol);
2391 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
2393 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
2395 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
2397 linkmode_zero(mask);
2398 phylink_set_port_modes(mask);
2400 linkmode_and(dst, dst, mask);
2401 linkmode_or(dst, dst, b);
2404 static void phylink_get_ksettings(const struct phylink_link_state *state,
2405 struct ethtool_link_ksettings *kset)
2407 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
2408 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
2409 if (kset->base.rate_matching == RATE_MATCH_NONE) {
2410 kset->base.speed = state->speed;
2411 kset->base.duplex = state->duplex;
2413 kset->base.autoneg = linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2414 state->advertising) ?
2415 AUTONEG_ENABLE : AUTONEG_DISABLE;
2419 * phylink_ethtool_ksettings_get() - get the current link settings
2420 * @pl: a pointer to a &struct phylink returned from phylink_create()
2421 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
2423 * Read the current link settings for the phylink instance specified by @pl.
2424 * This will be the link settings read from the MAC, PHY or fixed link
2425 * settings depending on the current negotiation mode.
2427 int phylink_ethtool_ksettings_get(struct phylink *pl,
2428 struct ethtool_link_ksettings *kset)
2430 struct phylink_link_state link_state;
2435 phy_ethtool_ksettings_get(pl->phydev, kset);
2437 kset->base.port = pl->link_port;
2439 linkmode_copy(kset->link_modes.supported, pl->supported);
2441 switch (pl->cur_link_an_mode) {
2443 /* We are using fixed settings. Report these as the
2444 * current link settings - and note that these also
2445 * represent the supported speeds/duplex/pause modes.
2447 phylink_get_fixed_state(pl, &link_state);
2448 phylink_get_ksettings(&link_state, kset);
2452 /* If there is a phy attached, then use the reported
2453 * settings from the phy with no modification.
2458 phylink_mac_pcs_get_state(pl, &link_state);
2460 /* The MAC is reporting the link results from its own PCS
2461 * layer via in-band status. Report these as the current
2464 phylink_get_ksettings(&link_state, kset);
2470 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
2473 * phylink_ethtool_ksettings_set() - set the link settings
2474 * @pl: a pointer to a &struct phylink returned from phylink_create()
2475 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
2477 int phylink_ethtool_ksettings_set(struct phylink *pl,
2478 const struct ethtool_link_ksettings *kset)
2480 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2481 struct phylink_link_state config;
2482 const struct phy_setting *s;
2487 struct ethtool_link_ksettings phy_kset = *kset;
2489 linkmode_and(phy_kset.link_modes.advertising,
2490 phy_kset.link_modes.advertising,
2493 /* We can rely on phylib for this update; we also do not need
2494 * to update the pl->link_config settings:
2495 * - the configuration returned via ksettings_get() will come
2496 * from phylib whenever a PHY is present.
2497 * - link_config.interface will be updated by the PHY calling
2498 * back via phylink_phy_change() and a subsequent resolve.
2499 * - initial link configuration for PHY mode comes from the
2500 * last phy state updated via phylink_phy_change().
2501 * - other configuration changes (e.g. pause modes) are
2502 * performed directly via phylib.
2503 * - if in in-band mode with a PHY, the link configuration
2504 * is passed on the link from the PHY, and all of
2505 * link_config.{speed,duplex,an_enabled,pause} are not used.
2506 * - the only possible use would be link_config.advertising
2507 * pause modes when in 1000base-X mode with a PHY, but in
2508 * the presence of a PHY, this should not be changed as that
2509 * should be determined from the media side advertisement.
2511 return phy_ethtool_ksettings_set(pl->phydev, &phy_kset);
2514 config = pl->link_config;
2515 /* Mask out unsupported advertisements */
2516 linkmode_and(config.advertising, kset->link_modes.advertising,
2519 /* FIXME: should we reject autoneg if phy/mac does not support it? */
2520 switch (kset->base.autoneg) {
2521 case AUTONEG_DISABLE:
2522 /* Autonegotiation disabled, select a suitable speed and
2525 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
2526 pl->supported, false);
2530 /* If we have a fixed link, refuse to change link parameters.
2531 * If the link parameters match, accept them but do nothing.
2533 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
2534 if (s->speed != pl->link_config.speed ||
2535 s->duplex != pl->link_config.duplex)
2540 config.speed = s->speed;
2541 config.duplex = s->duplex;
2544 case AUTONEG_ENABLE:
2545 /* If we have a fixed link, allow autonegotiation (since that
2546 * is our default case) but do not allow the advertisement to
2547 * be changed. If the advertisement matches, simply return.
2549 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
2550 if (!linkmode_equal(config.advertising,
2551 pl->link_config.advertising))
2556 config.speed = SPEED_UNKNOWN;
2557 config.duplex = DUPLEX_UNKNOWN;
2564 /* We have ruled out the case with a PHY attached, and the
2565 * fixed-link cases. All that is left are in-band links.
2567 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
2568 kset->base.autoneg == AUTONEG_ENABLE);
2570 /* If this link is with an SFP, ensure that changes to advertised modes
2571 * also cause the associated interface to be selected such that the
2572 * link can be configured correctly.
2575 config.interface = sfp_select_interface(pl->sfp_bus,
2576 config.advertising);
2577 if (config.interface == PHY_INTERFACE_MODE_NA) {
2579 "selection of interface failed, advertisement %*pb\n",
2580 __ETHTOOL_LINK_MODE_MASK_NBITS,
2581 config.advertising);
2585 /* Revalidate with the selected interface */
2586 linkmode_copy(support, pl->supported);
2587 if (phylink_validate(pl, support, &config)) {
2588 phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
2589 phylink_an_mode_str(pl->cur_link_an_mode),
2590 phy_modes(config.interface),
2591 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2595 /* Validate without changing the current supported mask. */
2596 linkmode_copy(support, pl->supported);
2597 if (phylink_validate(pl, support, &config))
2601 /* If autonegotiation is enabled, we must have an advertisement */
2602 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2603 config.advertising) &&
2604 phylink_is_empty_linkmode(config.advertising))
2607 mutex_lock(&pl->state_mutex);
2608 pl->link_config.speed = config.speed;
2609 pl->link_config.duplex = config.duplex;
2611 if (pl->link_config.interface != config.interface) {
2612 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
2613 /* We need to force the link down, then change the interface */
2614 if (pl->old_link_state) {
2615 phylink_link_down(pl);
2616 pl->old_link_state = false;
2618 if (!test_bit(PHYLINK_DISABLE_STOPPED,
2619 &pl->phylink_disable_state))
2620 phylink_major_config(pl, false, &config);
2621 pl->link_config.interface = config.interface;
2622 linkmode_copy(pl->link_config.advertising, config.advertising);
2623 } else if (!linkmode_equal(pl->link_config.advertising,
2624 config.advertising)) {
2625 linkmode_copy(pl->link_config.advertising, config.advertising);
2626 phylink_change_inband_advert(pl);
2628 mutex_unlock(&pl->state_mutex);
2632 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
2635 * phylink_ethtool_nway_reset() - restart negotiation
2636 * @pl: a pointer to a &struct phylink returned from phylink_create()
2638 * Restart negotiation for the phylink instance specified by @pl. This will
2639 * cause any attached phy to restart negotiation with the link partner, and
2640 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
2643 * Returns zero on success, or negative error code.
2645 int phylink_ethtool_nway_reset(struct phylink *pl)
2652 ret = phy_restart_aneg(pl->phydev);
2653 phylink_pcs_an_restart(pl);
2657 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
2660 * phylink_ethtool_get_pauseparam() - get the current pause parameters
2661 * @pl: a pointer to a &struct phylink returned from phylink_create()
2662 * @pause: a pointer to a &struct ethtool_pauseparam
2664 void phylink_ethtool_get_pauseparam(struct phylink *pl,
2665 struct ethtool_pauseparam *pause)
2669 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
2670 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
2671 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
2673 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
2676 * phylink_ethtool_set_pauseparam() - set the current pause parameters
2677 * @pl: a pointer to a &struct phylink returned from phylink_create()
2678 * @pause: a pointer to a &struct ethtool_pauseparam
2680 int phylink_ethtool_set_pauseparam(struct phylink *pl,
2681 struct ethtool_pauseparam *pause)
2683 struct phylink_link_state *config = &pl->link_config;
2684 bool manual_changed;
2689 if (pl->cur_link_an_mode == MLO_AN_FIXED)
2692 if (!phylink_test(pl->supported, Pause) &&
2693 !phylink_test(pl->supported, Asym_Pause))
2696 if (!phylink_test(pl->supported, Asym_Pause) &&
2697 pause->rx_pause != pause->tx_pause)
2702 pause_state |= MLO_PAUSE_AN;
2703 if (pause->rx_pause)
2704 pause_state |= MLO_PAUSE_RX;
2705 if (pause->tx_pause)
2706 pause_state |= MLO_PAUSE_TX;
2708 mutex_lock(&pl->state_mutex);
2710 * See the comments for linkmode_set_pause(), wrt the deficiencies
2711 * with the current implementation. A solution to this issue would
2713 * ethtool Local device
2714 * rx tx Pause AsymDir
2719 * and then use the ethtool rx/tx enablement status to mask the
2720 * rx/tx pause resolution.
2722 linkmode_set_pause(config->advertising, pause->tx_pause,
2725 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
2726 (!(pause_state & MLO_PAUSE_AN) &&
2727 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
2729 config->pause = pause_state;
2731 /* Update our in-band advertisement, triggering a renegotiation if
2732 * the advertisement changed.
2735 phylink_change_inband_advert(pl);
2737 mutex_unlock(&pl->state_mutex);
2739 /* If we have a PHY, a change of the pause frame advertisement will
2740 * cause phylib to renegotiate (if AN is enabled) which will in turn
2741 * call our phylink_phy_change() and trigger a resolve. Note that
2742 * we can't hold our state mutex while calling phy_set_asym_pause().
2745 phy_set_asym_pause(pl->phydev, pause->rx_pause,
2748 /* If the manual pause settings changed, make sure we trigger a
2749 * resolve to update their state; we can not guarantee that the
2752 if (manual_changed) {
2753 pl->mac_link_dropped = true;
2754 phylink_run_resolve(pl);
2759 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
2762 * phylink_get_eee_err() - read the energy efficient ethernet error
2764 * @pl: a pointer to a &struct phylink returned from phylink_create().
2766 * Read the Energy Efficient Ethernet error counter from the PHY associated
2767 * with the phylink instance specified by @pl.
2769 * Returns positive error counter value, or negative error code.
2771 int phylink_get_eee_err(struct phylink *pl)
2778 ret = phy_get_eee_err(pl->phydev);
2782 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
2785 * phylink_init_eee() - init and check the EEE features
2786 * @pl: a pointer to a &struct phylink returned from phylink_create()
2787 * @clk_stop_enable: allow PHY to stop receive clock
2789 * Must be called either with RTNL held or within mac_link_up()
2791 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
2793 int ret = -EOPNOTSUPP;
2796 ret = phy_init_eee(pl->phydev, clk_stop_enable);
2800 EXPORT_SYMBOL_GPL(phylink_init_eee);
2803 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
2804 * @pl: a pointer to a &struct phylink returned from phylink_create()
2805 * @eee: a pointer to a &struct ethtool_keee for the read parameters
2807 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_keee *eee)
2809 int ret = -EOPNOTSUPP;
2814 ret = phy_ethtool_get_eee(pl->phydev, eee);
2818 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
2821 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
2822 * @pl: a pointer to a &struct phylink returned from phylink_create()
2823 * @eee: a pointer to a &struct ethtool_keee for the desired parameters
2825 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_keee *eee)
2827 int ret = -EOPNOTSUPP;
2832 ret = phy_ethtool_set_eee(pl->phydev, eee);
2836 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
2838 /* This emulates MII registers for a fixed-mode phy operating as per the
2839 * passed in state. "aneg" defines if we report negotiation is possible.
2841 * FIXME: should deal with negotiation state too.
2843 static int phylink_mii_emul_read(unsigned int reg,
2844 struct phylink_link_state *state)
2846 struct fixed_phy_status fs;
2847 unsigned long *lpa = state->lp_advertising;
2850 fs.link = state->link;
2851 fs.speed = state->speed;
2852 fs.duplex = state->duplex;
2853 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
2854 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
2856 val = swphy_read_reg(reg, &fs);
2857 if (reg == MII_BMSR) {
2858 if (!state->an_complete)
2859 val &= ~BMSR_ANEGCOMPLETE;
2864 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
2867 struct phy_device *phydev = pl->phydev;
2870 if (mdio_phy_id_is_c45(phy_id)) {
2871 prtad = mdio_phy_id_prtad(phy_id);
2872 devad = mdio_phy_id_devad(phy_id);
2873 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
2877 if (phydev->is_c45) {
2883 devad = __ffs(phydev->c45_ids.mmds_present);
2887 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2889 devad = MDIO_MMD_AN;
2890 if (reg == MII_ADVERTISE)
2891 reg = MDIO_AN_ADVERTISE;
2899 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
2903 return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg);
2906 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
2907 unsigned int reg, unsigned int val)
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_write(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;
2940 return mdiobus_c45_write(pl->phydev->mdio.bus, phy_id, devad,
2944 return mdiobus_write(phydev->mdio.bus, phy_id, reg, val);
2947 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
2950 struct phylink_link_state state;
2953 switch (pl->cur_link_an_mode) {
2956 phylink_get_fixed_state(pl, &state);
2957 val = phylink_mii_emul_read(reg, &state);
2966 phylink_mac_pcs_get_state(pl, &state);
2967 val = phylink_mii_emul_read(reg, &state);
2972 return val & 0xffff;
2975 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
2976 unsigned int reg, unsigned int val)
2978 switch (pl->cur_link_an_mode) {
2993 * phylink_mii_ioctl() - generic mii ioctl interface
2994 * @pl: a pointer to a &struct phylink returned from phylink_create()
2995 * @ifr: a pointer to a &struct ifreq for socket ioctls
2996 * @cmd: ioctl cmd to execute
2998 * Perform the specified MII ioctl on the PHY attached to the phylink instance
2999 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
3001 * Returns: zero on success or negative error code.
3004 * read register from the current PHY.
3006 * read register from the specified PHY.
3008 * set a register on the specified PHY.
3010 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
3012 struct mii_ioctl_data *mii = if_mii(ifr);
3018 /* PHYs only exist for MLO_AN_PHY and SGMII */
3021 mii->phy_id = pl->phydev->mdio.addr;
3025 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
3033 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
3038 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
3048 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
3056 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
3068 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
3071 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
3073 * @pl: a pointer to a &struct phylink returned from phylink_create()
3074 * @sync: perform action synchronously
3076 * If we have a PHY that is not part of a SFP module, then set the speed
3077 * as described in the phy_speed_down() function. Please see this function
3078 * for a description of the @sync parameter.
3080 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
3082 int phylink_speed_down(struct phylink *pl, bool sync)
3088 if (!pl->sfp_bus && pl->phydev)
3089 ret = phy_speed_down(pl->phydev, sync);
3093 EXPORT_SYMBOL_GPL(phylink_speed_down);
3096 * phylink_speed_up() - restore the advertised speeds prior to the call to
3097 * phylink_speed_down()
3098 * @pl: a pointer to a &struct phylink returned from phylink_create()
3100 * If we have a PHY that is not part of a SFP module, then restore the
3101 * PHY speeds as per phy_speed_up().
3103 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
3105 int phylink_speed_up(struct phylink *pl)
3111 if (!pl->sfp_bus && pl->phydev)
3112 ret = phy_speed_up(pl->phydev);
3116 EXPORT_SYMBOL_GPL(phylink_speed_up);
3118 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
3120 struct phylink *pl = upstream;
3122 pl->netdev->sfp_bus = bus;
3125 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
3127 struct phylink *pl = upstream;
3129 pl->netdev->sfp_bus = NULL;
3132 static phy_interface_t phylink_choose_sfp_interface(struct phylink *pl,
3133 const unsigned long *intf)
3135 phy_interface_t interface;
3138 interface = PHY_INTERFACE_MODE_NA;
3139 for (i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); i++)
3140 if (test_bit(phylink_sfp_interface_preference[i], intf)) {
3141 interface = phylink_sfp_interface_preference[i];
3148 static void phylink_sfp_set_config(struct phylink *pl, u8 mode,
3149 unsigned long *supported,
3150 struct phylink_link_state *state)
3152 bool changed = false;
3154 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
3155 phylink_an_mode_str(mode), phy_modes(state->interface),
3156 __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
3158 if (!linkmode_equal(pl->supported, supported)) {
3159 linkmode_copy(pl->supported, supported);
3163 if (!linkmode_equal(pl->link_config.advertising, state->advertising)) {
3164 linkmode_copy(pl->link_config.advertising, state->advertising);
3168 if (pl->cur_link_an_mode != mode ||
3169 pl->link_config.interface != state->interface) {
3170 pl->cur_link_an_mode = mode;
3171 pl->link_config.interface = state->interface;
3175 phylink_info(pl, "switched to %s/%s link mode\n",
3176 phylink_an_mode_str(mode),
3177 phy_modes(state->interface));
3180 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
3181 &pl->phylink_disable_state))
3182 phylink_mac_initial_config(pl, false);
3185 static int phylink_sfp_config_phy(struct phylink *pl, u8 mode,
3186 struct phy_device *phy)
3188 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
3189 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
3190 struct phylink_link_state config;
3191 phy_interface_t iface;
3194 linkmode_copy(support, phy->supported);
3196 memset(&config, 0, sizeof(config));
3197 linkmode_copy(config.advertising, phy->advertising);
3198 config.interface = PHY_INTERFACE_MODE_NA;
3199 config.speed = SPEED_UNKNOWN;
3200 config.duplex = DUPLEX_UNKNOWN;
3201 config.pause = MLO_PAUSE_AN;
3203 /* Ignore errors if we're expecting a PHY to attach later */
3204 ret = phylink_validate(pl, support, &config);
3206 phylink_err(pl, "validation with support %*pb failed: %pe\n",
3207 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3212 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
3213 if (iface == PHY_INTERFACE_MODE_NA) {
3215 "selection of interface failed, advertisement %*pb\n",
3216 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
3220 config.interface = iface;
3221 linkmode_copy(support1, support);
3222 ret = phylink_validate(pl, support1, &config);
3225 "validation of %s/%s with support %*pb failed: %pe\n",
3226 phylink_an_mode_str(mode),
3227 phy_modes(config.interface),
3228 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3233 pl->link_port = pl->sfp_port;
3235 phylink_sfp_set_config(pl, mode, support, &config);
3240 static int phylink_sfp_config_optical(struct phylink *pl)
3242 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
3243 DECLARE_PHY_INTERFACE_MASK(interfaces);
3244 struct phylink_link_state config;
3245 phy_interface_t interface;
3248 phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n",
3249 (int)PHY_INTERFACE_MODE_MAX,
3250 pl->config->supported_interfaces,
3251 (int)PHY_INTERFACE_MODE_MAX,
3252 pl->sfp_interfaces);
3254 /* Find the union of the supported interfaces by the PCS/MAC and
3257 phy_interface_and(interfaces, pl->config->supported_interfaces,
3258 pl->sfp_interfaces);
3259 if (phy_interface_empty(interfaces)) {
3260 phylink_err(pl, "unsupported SFP module: no common interface modes\n");
3264 memset(&config, 0, sizeof(config));
3265 linkmode_copy(support, pl->sfp_support);
3266 linkmode_copy(config.advertising, pl->sfp_support);
3267 config.speed = SPEED_UNKNOWN;
3268 config.duplex = DUPLEX_UNKNOWN;
3269 config.pause = MLO_PAUSE_AN;
3271 /* For all the interfaces that are supported, reduce the sfp_support
3272 * mask to only those link modes that can be supported.
3274 ret = phylink_validate_mask(pl, NULL, pl->sfp_support, &config,
3277 phylink_err(pl, "unsupported SFP module: validation with support %*pb failed\n",
3278 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
3282 interface = phylink_choose_sfp_interface(pl, interfaces);
3283 if (interface == PHY_INTERFACE_MODE_NA) {
3284 phylink_err(pl, "failed to select SFP interface\n");
3288 phylink_dbg(pl, "optical SFP: chosen %s interface\n",
3289 phy_modes(interface));
3291 config.interface = interface;
3293 /* Ignore errors if we're expecting a PHY to attach later */
3294 ret = phylink_validate(pl, support, &config);
3296 phylink_err(pl, "validation with support %*pb failed: %pe\n",
3297 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
3302 pl->link_port = pl->sfp_port;
3304 phylink_sfp_set_config(pl, MLO_AN_INBAND, pl->sfp_support, &config);
3309 static int phylink_sfp_module_insert(void *upstream,
3310 const struct sfp_eeprom_id *id)
3312 struct phylink *pl = upstream;
3316 linkmode_zero(pl->sfp_support);
3317 phy_interface_zero(pl->sfp_interfaces);
3318 sfp_parse_support(pl->sfp_bus, id, pl->sfp_support, pl->sfp_interfaces);
3319 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, pl->sfp_support);
3321 /* If this module may have a PHY connecting later, defer until later */
3322 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
3323 if (pl->sfp_may_have_phy)
3326 return phylink_sfp_config_optical(pl);
3329 static int phylink_sfp_module_start(void *upstream)
3331 struct phylink *pl = upstream;
3333 /* If this SFP module has a PHY, start the PHY now. */
3335 phy_start(pl->phydev);
3339 /* If the module may have a PHY but we didn't detect one we
3340 * need to configure the MAC here.
3342 if (!pl->sfp_may_have_phy)
3345 return phylink_sfp_config_optical(pl);
3348 static void phylink_sfp_module_stop(void *upstream)
3350 struct phylink *pl = upstream;
3352 /* If this SFP module has a PHY, stop it. */
3354 phy_stop(pl->phydev);
3357 static void phylink_sfp_link_down(void *upstream)
3359 struct phylink *pl = upstream;
3363 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
3366 static void phylink_sfp_link_up(void *upstream)
3368 struct phylink *pl = upstream;
3372 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
3375 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
3376 * or 802.3z control word, so inband will not work.
3378 static bool phylink_phy_no_inband(struct phy_device *phy)
3380 return phy->is_c45 && phy_id_compare(phy->c45_ids.device_ids[1],
3381 0xae025150, 0xfffffff0);
3384 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
3386 struct phylink *pl = upstream;
3387 phy_interface_t interface;
3392 * This is the new way of dealing with flow control for PHYs,
3393 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
3394 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
3395 * using our validate call to the MAC, we rely upon the MAC
3396 * clearing the bits from both supported and advertising fields.
3398 phy_support_asym_pause(phy);
3400 if (phylink_phy_no_inband(phy))
3403 mode = MLO_AN_INBAND;
3405 /* Set the PHY's host supported interfaces */
3406 phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
3407 pl->config->supported_interfaces);
3409 /* Do the initial configuration */
3410 ret = phylink_sfp_config_phy(pl, mode, phy);
3414 interface = pl->link_config.interface;
3415 ret = phylink_attach_phy(pl, phy, interface);
3419 ret = phylink_bringup_phy(pl, phy, interface);
3426 static void phylink_sfp_disconnect_phy(void *upstream)
3428 phylink_disconnect_phy(upstream);
3431 static const struct sfp_upstream_ops sfp_phylink_ops = {
3432 .attach = phylink_sfp_attach,
3433 .detach = phylink_sfp_detach,
3434 .module_insert = phylink_sfp_module_insert,
3435 .module_start = phylink_sfp_module_start,
3436 .module_stop = phylink_sfp_module_stop,
3437 .link_up = phylink_sfp_link_up,
3438 .link_down = phylink_sfp_link_down,
3439 .connect_phy = phylink_sfp_connect_phy,
3440 .disconnect_phy = phylink_sfp_disconnect_phy,
3443 /* Helpers for MAC drivers */
3448 } phylink_c73_priority_resolution[] = {
3449 { ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, SPEED_100000 },
3450 { ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, SPEED_100000 },
3451 /* 100GBASE-KP4 and 100GBASE-CR10 not supported */
3452 { ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, SPEED_40000 },
3453 { ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, SPEED_40000 },
3454 { ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, SPEED_10000 },
3455 { ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, SPEED_10000 },
3456 /* 5GBASE-KR not supported */
3457 { ETHTOOL_LINK_MODE_2500baseX_Full_BIT, SPEED_2500 },
3458 { ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, SPEED_1000 },
3461 void phylink_resolve_c73(struct phylink_link_state *state)
3465 for (i = 0; i < ARRAY_SIZE(phylink_c73_priority_resolution); i++) {
3466 int bit = phylink_c73_priority_resolution[i].bit;
3467 if (linkmode_test_bit(bit, state->advertising) &&
3468 linkmode_test_bit(bit, state->lp_advertising))
3472 if (i < ARRAY_SIZE(phylink_c73_priority_resolution)) {
3473 state->speed = phylink_c73_priority_resolution[i].speed;
3474 state->duplex = DUPLEX_FULL;
3476 /* negotiation failure */
3477 state->link = false;
3480 phylink_resolve_an_pause(state);
3482 EXPORT_SYMBOL_GPL(phylink_resolve_c73);
3484 static void phylink_decode_c37_word(struct phylink_link_state *state,
3485 uint16_t config_reg, int speed)
3489 if (speed == SPEED_2500)
3490 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
3492 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
3494 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
3496 if (linkmode_test_bit(fd_bit, state->advertising) &&
3497 linkmode_test_bit(fd_bit, state->lp_advertising)) {
3498 state->speed = speed;
3499 state->duplex = DUPLEX_FULL;
3501 /* negotiation failure */
3502 state->link = false;
3505 phylink_resolve_an_pause(state);
3508 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
3509 uint16_t config_reg)
3511 if (!(config_reg & LPA_SGMII_LINK)) {
3512 state->link = false;
3516 switch (config_reg & LPA_SGMII_SPD_MASK) {
3518 state->speed = SPEED_10;
3521 state->speed = SPEED_100;
3523 case LPA_SGMII_1000:
3524 state->speed = SPEED_1000;
3527 state->link = false;
3530 if (config_reg & LPA_SGMII_FULL_DUPLEX)
3531 state->duplex = DUPLEX_FULL;
3533 state->duplex = DUPLEX_HALF;
3537 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
3538 * @state: a pointer to a struct phylink_link_state.
3539 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
3541 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
3542 * code word. Decode the USXGMII code word and populate the corresponding fields
3543 * (speed, duplex) into the phylink_link_state structure.
3545 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
3548 switch (lpa & MDIO_USXGMII_SPD_MASK) {
3549 case MDIO_USXGMII_10:
3550 state->speed = SPEED_10;
3552 case MDIO_USXGMII_100:
3553 state->speed = SPEED_100;
3555 case MDIO_USXGMII_1000:
3556 state->speed = SPEED_1000;
3558 case MDIO_USXGMII_2500:
3559 state->speed = SPEED_2500;
3561 case MDIO_USXGMII_5000:
3562 state->speed = SPEED_5000;
3564 case MDIO_USXGMII_10G:
3565 state->speed = SPEED_10000;
3568 state->link = false;
3572 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
3573 state->duplex = DUPLEX_FULL;
3575 state->duplex = DUPLEX_HALF;
3577 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
3580 * phylink_decode_usgmii_word() - decode the USGMII word from a MAC PCS
3581 * @state: a pointer to a struct phylink_link_state.
3582 * @lpa: a 16 bit value which stores the USGMII auto-negotiation word
3584 * Helper for MAC PCS supporting the USGMII protocol and the auto-negotiation
3585 * code word. Decode the USGMII code word and populate the corresponding fields
3586 * (speed, duplex) into the phylink_link_state structure. The structure for this
3587 * word is the same as the USXGMII word, except it only supports speeds up to
3590 static void phylink_decode_usgmii_word(struct phylink_link_state *state,
3593 switch (lpa & MDIO_USXGMII_SPD_MASK) {
3594 case MDIO_USXGMII_10:
3595 state->speed = SPEED_10;
3597 case MDIO_USXGMII_100:
3598 state->speed = SPEED_100;
3600 case MDIO_USXGMII_1000:
3601 state->speed = SPEED_1000;
3604 state->link = false;
3608 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
3609 state->duplex = DUPLEX_FULL;
3611 state->duplex = DUPLEX_HALF;
3615 * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
3616 * @state: a pointer to a &struct phylink_link_state.
3617 * @bmsr: The value of the %MII_BMSR register
3618 * @lpa: The value of the %MII_LPA register
3620 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3621 * clause 37 negotiation and/or SGMII control.
3623 * Parse the Clause 37 or Cisco SGMII link partner negotiation word into
3624 * the phylink @state structure. This is suitable to be used for implementing
3625 * the pcs_get_state() member of the struct phylink_pcs_ops structure if
3626 * accessing @bmsr and @lpa cannot be done with MDIO directly.
3628 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
3631 state->link = !!(bmsr & BMSR_LSTATUS);
3632 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
3633 /* If there is no link or autonegotiation is disabled, the LP advertisement
3634 * data is not meaningful, so don't go any further.
3636 if (!state->link || !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3637 state->advertising))
3640 switch (state->interface) {
3641 case PHY_INTERFACE_MODE_1000BASEX:
3642 phylink_decode_c37_word(state, lpa, SPEED_1000);
3645 case PHY_INTERFACE_MODE_2500BASEX:
3646 phylink_decode_c37_word(state, lpa, SPEED_2500);
3649 case PHY_INTERFACE_MODE_SGMII:
3650 case PHY_INTERFACE_MODE_QSGMII:
3651 phylink_decode_sgmii_word(state, lpa);
3653 case PHY_INTERFACE_MODE_QUSGMII:
3654 phylink_decode_usgmii_word(state, lpa);
3658 state->link = false;
3662 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state);
3665 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
3666 * @pcs: a pointer to a &struct mdio_device.
3667 * @state: a pointer to a &struct phylink_link_state.
3669 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3670 * clause 37 negotiation and/or SGMII control.
3672 * Read the MAC PCS state from the MII device configured in @config and
3673 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
3674 * the phylink @state structure. This is suitable to be directly plugged
3675 * into the pcs_get_state() member of the struct phylink_pcs_ops
3678 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
3679 struct phylink_link_state *state)
3683 bmsr = mdiodev_read(pcs, MII_BMSR);
3684 lpa = mdiodev_read(pcs, MII_LPA);
3685 if (bmsr < 0 || lpa < 0) {
3686 state->link = false;
3690 phylink_mii_c22_pcs_decode_state(state, bmsr, lpa);
3692 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
3695 * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
3697 * @interface: the PHY interface mode being configured
3698 * @advertising: the ethtool advertisement mask
3700 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3701 * clause 37 negotiation and/or SGMII control.
3703 * Encode the clause 37 PCS advertisement as specified by @interface and
3706 * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
3708 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
3709 const unsigned long *advertising)
3713 switch (interface) {
3714 case PHY_INTERFACE_MODE_1000BASEX:
3715 case PHY_INTERFACE_MODE_2500BASEX:
3716 adv = ADVERTISE_1000XFULL;
3717 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3719 adv |= ADVERTISE_1000XPAUSE;
3720 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3722 adv |= ADVERTISE_1000XPSE_ASYM;
3724 case PHY_INTERFACE_MODE_SGMII:
3725 case PHY_INTERFACE_MODE_QSGMII:
3728 /* Nothing to do for other modes */
3732 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement);
3735 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
3736 * @pcs: a pointer to a &struct mdio_device.
3737 * @interface: the PHY interface mode being configured
3738 * @advertising: the ethtool advertisement mask
3739 * @neg_mode: PCS negotiation mode
3741 * Configure a Clause 22 PCS PHY with the appropriate negotiation
3742 * parameters for the @mode, @interface and @advertising parameters.
3743 * Returns negative error number on failure, zero if the advertisement
3744 * has not changed, or positive if there is a change.
3746 int phylink_mii_c22_pcs_config(struct mdio_device *pcs,
3747 phy_interface_t interface,
3748 const unsigned long *advertising,
3749 unsigned int neg_mode)
3755 adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
3757 ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
3758 MII_ADVERTISE, 0xffff, adv);
3764 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
3765 bmcr = BMCR_ANENABLE;
3769 /* Configure the inband state. Ensure ISOLATE bit is disabled */
3770 ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
3776 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
3779 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
3780 * @pcs: a pointer to a &struct mdio_device.
3782 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3783 * clause 37 negotiation.
3785 * Restart the clause 37 negotiation with the link partner. This is
3786 * suitable to be directly plugged into the pcs_get_state() member
3787 * of the struct phylink_pcs_ops structure.
3789 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
3791 int val = mdiodev_read(pcs, MII_BMCR);
3794 val |= BMCR_ANRESTART;
3796 mdiodev_write(pcs, MII_BMCR, val);
3799 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
3801 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
3802 struct phylink_link_state *state)
3804 struct mii_bus *bus = pcs->bus;
3805 int addr = pcs->addr;
3808 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
3810 state->link = false;
3814 state->link = !!(stat & MDIO_STAT1_LSTATUS);
3818 switch (state->interface) {
3819 case PHY_INTERFACE_MODE_10GBASER:
3820 state->speed = SPEED_10000;
3821 state->duplex = DUPLEX_FULL;
3828 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
3830 static int __init phylink_init(void)
3832 for (int i = 0; i < ARRAY_SIZE(phylink_sfp_interface_preference); ++i)
3833 __set_bit(phylink_sfp_interface_preference[i],
3834 phylink_sfp_interfaces);
3839 module_init(phylink_init);
3841 MODULE_LICENSE("GPL v2");
3842 MODULE_DESCRIPTION("phylink models the MAC to optional PHY connection");