1 // SPDX-License-Identifier: GPL-2.0
3 * phylink models the MAC to optional PHY connection, supporting
4 * technologies such as SFP cages where the PHY is hot-pluggable.
6 * Copyright (C) 2015 Russell King
8 #include <linux/acpi.h>
9 #include <linux/ethtool.h>
10 #include <linux/export.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/netdevice.h>
14 #include <linux/of_mdio.h>
15 #include <linux/phy.h>
16 #include <linux/phy_fixed.h>
17 #include <linux/phylink.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/spinlock.h>
20 #include <linux/timer.h>
21 #include <linux/workqueue.h>
26 #define SUPPORTED_INTERFACES \
27 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
28 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
29 #define ADVERTISED_INTERFACES \
30 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
31 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
34 PHYLINK_DISABLE_STOPPED,
36 PHYLINK_DISABLE_MAC_WOL,
40 * struct phylink - internal data type for phylink
44 struct net_device *netdev;
45 const struct phylink_mac_ops *mac_ops;
46 const struct phylink_pcs_ops *pcs_ops;
47 struct phylink_config *config;
48 struct phylink_pcs *pcs;
50 unsigned int old_link_state:1;
52 unsigned long phylink_disable_state; /* bitmask of disables */
53 struct phy_device *phydev;
54 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
55 u8 cfg_link_an_mode; /* MLO_AN_xxx */
57 u8 link_port; /* The current non-phy ethtool port */
58 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
60 /* The link configuration settings */
61 struct phylink_link_state link_config;
63 /* The current settings */
64 phy_interface_t cur_interface;
66 struct gpio_desc *link_gpio;
67 unsigned int link_irq;
68 struct timer_list link_poll;
69 void (*get_fixed_state)(struct net_device *dev,
70 struct phylink_link_state *s);
72 struct mutex state_mutex;
73 struct phylink_link_state phy_state;
74 struct work_struct resolve;
76 bool mac_link_dropped;
77 bool using_mac_select_pcs;
79 struct sfp_bus *sfp_bus;
80 bool sfp_may_have_phy;
81 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
85 #define phylink_printk(level, pl, fmt, ...) \
87 if ((pl)->config->type == PHYLINK_NETDEV) \
88 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
89 else if ((pl)->config->type == PHYLINK_DEV) \
90 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
93 #define phylink_err(pl, fmt, ...) \
94 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
95 #define phylink_warn(pl, fmt, ...) \
96 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
97 #define phylink_info(pl, fmt, ...) \
98 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
99 #if defined(CONFIG_DYNAMIC_DEBUG)
100 #define phylink_dbg(pl, fmt, ...) \
102 if ((pl)->config->type == PHYLINK_NETDEV) \
103 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
104 else if ((pl)->config->type == PHYLINK_DEV) \
105 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
108 #define phylink_dbg(pl, fmt, ...) \
109 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
111 #define phylink_dbg(pl, fmt, ...) \
114 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
119 * phylink_set_port_modes() - set the port type modes in the ethtool mask
120 * @mask: ethtool link mode mask
122 * Sets all the port type modes in the ethtool mask. MAC drivers should
123 * use this in their 'validate' callback.
125 void phylink_set_port_modes(unsigned long *mask)
127 phylink_set(mask, TP);
128 phylink_set(mask, AUI);
129 phylink_set(mask, MII);
130 phylink_set(mask, FIBRE);
131 phylink_set(mask, BNC);
132 phylink_set(mask, Backplane);
134 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
136 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
138 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
140 phylink_set_port_modes(tmp);
141 phylink_set(tmp, Autoneg);
142 phylink_set(tmp, Pause);
143 phylink_set(tmp, Asym_Pause);
145 return linkmode_subset(linkmode, tmp);
148 static const char *phylink_an_mode_str(unsigned int mode)
150 static const char *modestr[] = {
151 [MLO_AN_PHY] = "phy",
152 [MLO_AN_FIXED] = "fixed",
153 [MLO_AN_INBAND] = "inband",
156 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
159 static void phylink_caps_to_linkmodes(unsigned long *linkmodes,
162 if (caps & MAC_SYM_PAUSE)
163 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT, linkmodes);
165 if (caps & MAC_ASYM_PAUSE)
166 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, linkmodes);
169 __set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, linkmodes);
172 __set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, linkmodes);
174 if (caps & MAC_100HD) {
175 __set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, linkmodes);
176 __set_bit(ETHTOOL_LINK_MODE_100baseFX_Half_BIT, linkmodes);
179 if (caps & MAC_100FD) {
180 __set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, linkmodes);
181 __set_bit(ETHTOOL_LINK_MODE_100baseT1_Full_BIT, linkmodes);
182 __set_bit(ETHTOOL_LINK_MODE_100baseFX_Full_BIT, linkmodes);
185 if (caps & MAC_1000HD)
186 __set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, linkmodes);
188 if (caps & MAC_1000FD) {
189 __set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, linkmodes);
190 __set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, linkmodes);
191 __set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, linkmodes);
192 __set_bit(ETHTOOL_LINK_MODE_1000baseT1_Full_BIT, linkmodes);
195 if (caps & MAC_2500FD) {
196 __set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, linkmodes);
197 __set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, linkmodes);
200 if (caps & MAC_5000FD)
201 __set_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, linkmodes);
203 if (caps & MAC_10000FD) {
204 __set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, linkmodes);
205 __set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, linkmodes);
206 __set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, linkmodes);
207 __set_bit(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT, linkmodes);
208 __set_bit(ETHTOOL_LINK_MODE_10000baseCR_Full_BIT, linkmodes);
209 __set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, linkmodes);
210 __set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, linkmodes);
211 __set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT, linkmodes);
212 __set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, linkmodes);
215 if (caps & MAC_25000FD) {
216 __set_bit(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, linkmodes);
217 __set_bit(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, linkmodes);
218 __set_bit(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, linkmodes);
221 if (caps & MAC_40000FD) {
222 __set_bit(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, linkmodes);
223 __set_bit(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, linkmodes);
224 __set_bit(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, linkmodes);
225 __set_bit(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, linkmodes);
228 if (caps & MAC_50000FD) {
229 __set_bit(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT, linkmodes);
230 __set_bit(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT, linkmodes);
231 __set_bit(ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT, linkmodes);
232 __set_bit(ETHTOOL_LINK_MODE_50000baseKR_Full_BIT, linkmodes);
233 __set_bit(ETHTOOL_LINK_MODE_50000baseSR_Full_BIT, linkmodes);
234 __set_bit(ETHTOOL_LINK_MODE_50000baseCR_Full_BIT, linkmodes);
235 __set_bit(ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
237 __set_bit(ETHTOOL_LINK_MODE_50000baseDR_Full_BIT, linkmodes);
240 if (caps & MAC_56000FD) {
241 __set_bit(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT, linkmodes);
242 __set_bit(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT, linkmodes);
243 __set_bit(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT, linkmodes);
244 __set_bit(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT, linkmodes);
247 if (caps & MAC_100000FD) {
248 __set_bit(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, linkmodes);
249 __set_bit(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT, linkmodes);
250 __set_bit(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, linkmodes);
251 __set_bit(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
253 __set_bit(ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT, linkmodes);
254 __set_bit(ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT, linkmodes);
255 __set_bit(ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT, linkmodes);
256 __set_bit(ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT,
258 __set_bit(ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT, linkmodes);
259 __set_bit(ETHTOOL_LINK_MODE_100000baseKR_Full_BIT, linkmodes);
260 __set_bit(ETHTOOL_LINK_MODE_100000baseSR_Full_BIT, linkmodes);
261 __set_bit(ETHTOOL_LINK_MODE_100000baseLR_ER_FR_Full_BIT,
263 __set_bit(ETHTOOL_LINK_MODE_100000baseCR_Full_BIT, linkmodes);
264 __set_bit(ETHTOOL_LINK_MODE_100000baseDR_Full_BIT, linkmodes);
267 if (caps & MAC_200000FD) {
268 __set_bit(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT, linkmodes);
269 __set_bit(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT, linkmodes);
270 __set_bit(ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
272 __set_bit(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT, linkmodes);
273 __set_bit(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT, linkmodes);
274 __set_bit(ETHTOOL_LINK_MODE_200000baseKR2_Full_BIT, linkmodes);
275 __set_bit(ETHTOOL_LINK_MODE_200000baseSR2_Full_BIT, linkmodes);
276 __set_bit(ETHTOOL_LINK_MODE_200000baseLR2_ER2_FR2_Full_BIT,
278 __set_bit(ETHTOOL_LINK_MODE_200000baseDR2_Full_BIT, linkmodes);
279 __set_bit(ETHTOOL_LINK_MODE_200000baseCR2_Full_BIT, linkmodes);
282 if (caps & MAC_400000FD) {
283 __set_bit(ETHTOOL_LINK_MODE_400000baseKR8_Full_BIT, linkmodes);
284 __set_bit(ETHTOOL_LINK_MODE_400000baseSR8_Full_BIT, linkmodes);
285 __set_bit(ETHTOOL_LINK_MODE_400000baseLR8_ER8_FR8_Full_BIT,
287 __set_bit(ETHTOOL_LINK_MODE_400000baseDR8_Full_BIT, linkmodes);
288 __set_bit(ETHTOOL_LINK_MODE_400000baseCR8_Full_BIT, linkmodes);
289 __set_bit(ETHTOOL_LINK_MODE_400000baseKR4_Full_BIT, linkmodes);
290 __set_bit(ETHTOOL_LINK_MODE_400000baseSR4_Full_BIT, linkmodes);
291 __set_bit(ETHTOOL_LINK_MODE_400000baseLR4_ER4_FR4_Full_BIT,
293 __set_bit(ETHTOOL_LINK_MODE_400000baseDR4_Full_BIT, linkmodes);
294 __set_bit(ETHTOOL_LINK_MODE_400000baseCR4_Full_BIT, linkmodes);
299 * phylink_get_linkmodes() - get acceptable link modes
300 * @linkmodes: ethtool linkmode mask (must be already initialised)
301 * @interface: phy interface mode defined by &typedef phy_interface_t
302 * @mac_capabilities: bitmask of MAC capabilities
304 * Set all possible pause, speed and duplex linkmodes in @linkmodes that
305 * are supported by the @interface mode and @mac_capabilities. @linkmodes
306 * must have been initialised previously.
308 void phylink_get_linkmodes(unsigned long *linkmodes, phy_interface_t interface,
309 unsigned long mac_capabilities)
311 unsigned long caps = MAC_SYM_PAUSE | MAC_ASYM_PAUSE;
314 case PHY_INTERFACE_MODE_USXGMII:
315 caps |= MAC_10000FD | MAC_5000FD | MAC_2500FD;
318 case PHY_INTERFACE_MODE_RGMII_TXID:
319 case PHY_INTERFACE_MODE_RGMII_RXID:
320 case PHY_INTERFACE_MODE_RGMII_ID:
321 case PHY_INTERFACE_MODE_RGMII:
322 case PHY_INTERFACE_MODE_QSGMII:
323 case PHY_INTERFACE_MODE_SGMII:
324 case PHY_INTERFACE_MODE_GMII:
325 caps |= MAC_1000HD | MAC_1000FD;
328 case PHY_INTERFACE_MODE_REVRMII:
329 case PHY_INTERFACE_MODE_RMII:
330 case PHY_INTERFACE_MODE_SMII:
331 case PHY_INTERFACE_MODE_REVMII:
332 case PHY_INTERFACE_MODE_MII:
333 caps |= MAC_10HD | MAC_10FD;
336 case PHY_INTERFACE_MODE_100BASEX:
337 caps |= MAC_100HD | MAC_100FD;
340 case PHY_INTERFACE_MODE_TBI:
341 case PHY_INTERFACE_MODE_MOCA:
342 case PHY_INTERFACE_MODE_RTBI:
343 case PHY_INTERFACE_MODE_1000BASEX:
346 case PHY_INTERFACE_MODE_TRGMII:
350 case PHY_INTERFACE_MODE_2500BASEX:
354 case PHY_INTERFACE_MODE_5GBASER:
358 case PHY_INTERFACE_MODE_XGMII:
359 case PHY_INTERFACE_MODE_RXAUI:
360 case PHY_INTERFACE_MODE_XAUI:
361 case PHY_INTERFACE_MODE_10GBASER:
362 case PHY_INTERFACE_MODE_10GKR:
366 case PHY_INTERFACE_MODE_25GBASER:
370 case PHY_INTERFACE_MODE_XLGMII:
374 case PHY_INTERFACE_MODE_INTERNAL:
378 case PHY_INTERFACE_MODE_NA:
379 case PHY_INTERFACE_MODE_MAX:
383 phylink_caps_to_linkmodes(linkmodes, caps & mac_capabilities);
385 EXPORT_SYMBOL_GPL(phylink_get_linkmodes);
388 * phylink_generic_validate() - generic validate() callback implementation
389 * @config: a pointer to a &struct phylink_config.
390 * @supported: ethtool bitmask for supported link modes.
391 * @state: a pointer to a &struct phylink_link_state.
393 * Generic implementation of the validate() callback that MAC drivers can
394 * use when they pass the range of supported interfaces and MAC capabilities.
395 * This makes use of phylink_get_linkmodes().
397 void phylink_generic_validate(struct phylink_config *config,
398 unsigned long *supported,
399 struct phylink_link_state *state)
401 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
403 phylink_set_port_modes(mask);
404 phylink_set(mask, Autoneg);
405 phylink_get_linkmodes(mask, state->interface, config->mac_capabilities);
407 linkmode_and(supported, supported, mask);
408 linkmode_and(state->advertising, state->advertising, mask);
410 EXPORT_SYMBOL_GPL(phylink_generic_validate);
412 static int phylink_validate_mac_and_pcs(struct phylink *pl,
413 unsigned long *supported,
414 struct phylink_link_state *state)
416 struct phylink_pcs *pcs;
419 /* Get the PCS for this interface mode */
420 if (pl->using_mac_select_pcs) {
421 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
429 /* The PCS, if present, must be setup before phylink_create()
430 * has been called. If the ops is not initialised, print an
431 * error and backtrace rather than oopsing the kernel.
434 phylink_err(pl, "interface %s: uninitialised PCS\n",
435 phy_modes(state->interface));
440 /* Validate the link parameters with the PCS */
441 if (pcs->ops->pcs_validate) {
442 ret = pcs->ops->pcs_validate(pcs, supported, state);
443 if (ret < 0 || phylink_is_empty_linkmode(supported))
446 /* Ensure the advertising mask is a subset of the
449 linkmode_and(state->advertising, state->advertising,
454 /* Then validate the link parameters with the MAC */
455 pl->mac_ops->validate(pl->config, supported, state);
457 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
460 static int phylink_validate_any(struct phylink *pl, unsigned long *supported,
461 struct phylink_link_state *state)
463 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
464 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
465 __ETHTOOL_DECLARE_LINK_MODE_MASK(s);
466 struct phylink_link_state t;
469 for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) {
470 if (test_bit(intf, pl->config->supported_interfaces)) {
471 linkmode_copy(s, supported);
475 if (!phylink_validate_mac_and_pcs(pl, s, &t)) {
476 linkmode_or(all_s, all_s, s);
477 linkmode_or(all_adv, all_adv, t.advertising);
482 linkmode_copy(supported, all_s);
483 linkmode_copy(state->advertising, all_adv);
485 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
488 static int phylink_validate(struct phylink *pl, unsigned long *supported,
489 struct phylink_link_state *state)
491 if (!phy_interface_empty(pl->config->supported_interfaces)) {
492 if (state->interface == PHY_INTERFACE_MODE_NA)
493 return phylink_validate_any(pl, supported, state);
495 if (!test_bit(state->interface,
496 pl->config->supported_interfaces))
500 return phylink_validate_mac_and_pcs(pl, supported, state);
503 static int phylink_parse_fixedlink(struct phylink *pl,
504 struct fwnode_handle *fwnode)
506 struct fwnode_handle *fixed_node;
507 const struct phy_setting *s;
508 struct gpio_desc *desc;
512 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
514 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
516 pl->link_config.speed = speed;
517 pl->link_config.duplex = DUPLEX_HALF;
519 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
520 pl->link_config.duplex = DUPLEX_FULL;
522 /* We treat the "pause" and "asym-pause" terminology as
523 * defining the link partner's ability.
525 if (fwnode_property_read_bool(fixed_node, "pause"))
526 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
527 pl->link_config.lp_advertising);
528 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
529 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
530 pl->link_config.lp_advertising);
533 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
537 pl->link_gpio = desc;
538 else if (desc == ERR_PTR(-EPROBE_DEFER))
541 fwnode_handle_put(fixed_node);
548 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
550 if (ret != ARRAY_SIZE(prop)) {
551 phylink_err(pl, "broken fixed-link?\n");
555 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
556 prop, ARRAY_SIZE(prop));
558 pl->link_config.duplex = prop[1] ?
559 DUPLEX_FULL : DUPLEX_HALF;
560 pl->link_config.speed = prop[2];
562 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
563 pl->link_config.lp_advertising);
565 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
566 pl->link_config.lp_advertising);
570 if (pl->link_config.speed > SPEED_1000 &&
571 pl->link_config.duplex != DUPLEX_FULL)
572 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
573 pl->link_config.speed);
575 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
576 linkmode_copy(pl->link_config.advertising, pl->supported);
577 phylink_validate(pl, pl->supported, &pl->link_config);
579 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
580 pl->supported, true);
581 linkmode_zero(pl->supported);
582 phylink_set(pl->supported, MII);
583 phylink_set(pl->supported, Pause);
584 phylink_set(pl->supported, Asym_Pause);
585 phylink_set(pl->supported, Autoneg);
587 __set_bit(s->bit, pl->supported);
588 __set_bit(s->bit, pl->link_config.lp_advertising);
590 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
591 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
592 pl->link_config.speed);
595 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
598 pl->link_config.link = 1;
599 pl->link_config.an_complete = 1;
604 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
606 struct fwnode_handle *dn;
609 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
610 if (dn || fwnode_property_present(fwnode, "fixed-link"))
611 pl->cfg_link_an_mode = MLO_AN_FIXED;
612 fwnode_handle_put(dn);
614 if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
615 strcmp(managed, "in-band-status") == 0) ||
616 pl->config->ovr_an_inband) {
617 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
619 "can't use both fixed-link and in-band-status\n");
623 linkmode_zero(pl->supported);
624 phylink_set(pl->supported, MII);
625 phylink_set(pl->supported, Autoneg);
626 phylink_set(pl->supported, Asym_Pause);
627 phylink_set(pl->supported, Pause);
628 pl->link_config.an_enabled = true;
629 pl->cfg_link_an_mode = MLO_AN_INBAND;
631 switch (pl->link_config.interface) {
632 case PHY_INTERFACE_MODE_SGMII:
633 case PHY_INTERFACE_MODE_QSGMII:
634 phylink_set(pl->supported, 10baseT_Half);
635 phylink_set(pl->supported, 10baseT_Full);
636 phylink_set(pl->supported, 100baseT_Half);
637 phylink_set(pl->supported, 100baseT_Full);
638 phylink_set(pl->supported, 1000baseT_Half);
639 phylink_set(pl->supported, 1000baseT_Full);
642 case PHY_INTERFACE_MODE_1000BASEX:
643 phylink_set(pl->supported, 1000baseX_Full);
646 case PHY_INTERFACE_MODE_2500BASEX:
647 phylink_set(pl->supported, 2500baseX_Full);
650 case PHY_INTERFACE_MODE_5GBASER:
651 phylink_set(pl->supported, 5000baseT_Full);
654 case PHY_INTERFACE_MODE_25GBASER:
655 phylink_set(pl->supported, 25000baseCR_Full);
656 phylink_set(pl->supported, 25000baseKR_Full);
657 phylink_set(pl->supported, 25000baseSR_Full);
659 case PHY_INTERFACE_MODE_USXGMII:
660 case PHY_INTERFACE_MODE_10GKR:
661 case PHY_INTERFACE_MODE_10GBASER:
662 phylink_set(pl->supported, 10baseT_Half);
663 phylink_set(pl->supported, 10baseT_Full);
664 phylink_set(pl->supported, 100baseT_Half);
665 phylink_set(pl->supported, 100baseT_Full);
666 phylink_set(pl->supported, 1000baseT_Half);
667 phylink_set(pl->supported, 1000baseT_Full);
668 phylink_set(pl->supported, 1000baseX_Full);
669 phylink_set(pl->supported, 1000baseKX_Full);
670 phylink_set(pl->supported, 2500baseT_Full);
671 phylink_set(pl->supported, 2500baseX_Full);
672 phylink_set(pl->supported, 5000baseT_Full);
673 phylink_set(pl->supported, 10000baseT_Full);
674 phylink_set(pl->supported, 10000baseKR_Full);
675 phylink_set(pl->supported, 10000baseKX4_Full);
676 phylink_set(pl->supported, 10000baseCR_Full);
677 phylink_set(pl->supported, 10000baseSR_Full);
678 phylink_set(pl->supported, 10000baseLR_Full);
679 phylink_set(pl->supported, 10000baseLRM_Full);
680 phylink_set(pl->supported, 10000baseER_Full);
683 case PHY_INTERFACE_MODE_XLGMII:
684 phylink_set(pl->supported, 25000baseCR_Full);
685 phylink_set(pl->supported, 25000baseKR_Full);
686 phylink_set(pl->supported, 25000baseSR_Full);
687 phylink_set(pl->supported, 40000baseKR4_Full);
688 phylink_set(pl->supported, 40000baseCR4_Full);
689 phylink_set(pl->supported, 40000baseSR4_Full);
690 phylink_set(pl->supported, 40000baseLR4_Full);
691 phylink_set(pl->supported, 50000baseCR2_Full);
692 phylink_set(pl->supported, 50000baseKR2_Full);
693 phylink_set(pl->supported, 50000baseSR2_Full);
694 phylink_set(pl->supported, 50000baseKR_Full);
695 phylink_set(pl->supported, 50000baseSR_Full);
696 phylink_set(pl->supported, 50000baseCR_Full);
697 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
698 phylink_set(pl->supported, 50000baseDR_Full);
699 phylink_set(pl->supported, 100000baseKR4_Full);
700 phylink_set(pl->supported, 100000baseSR4_Full);
701 phylink_set(pl->supported, 100000baseCR4_Full);
702 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
703 phylink_set(pl->supported, 100000baseKR2_Full);
704 phylink_set(pl->supported, 100000baseSR2_Full);
705 phylink_set(pl->supported, 100000baseCR2_Full);
706 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
707 phylink_set(pl->supported, 100000baseDR2_Full);
712 "incorrect link mode %s for in-band status\n",
713 phy_modes(pl->link_config.interface));
717 linkmode_copy(pl->link_config.advertising, pl->supported);
719 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
721 "failed to validate link configuration for in-band status\n");
725 /* Check if MAC/PCS also supports Autoneg. */
726 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
732 static void phylink_apply_manual_flow(struct phylink *pl,
733 struct phylink_link_state *state)
735 /* If autoneg is disabled, pause AN is also disabled */
736 if (!state->an_enabled)
737 state->pause &= ~MLO_PAUSE_AN;
739 /* Manual configuration of pause modes */
740 if (!(pl->link_config.pause & MLO_PAUSE_AN))
741 state->pause = pl->link_config.pause;
744 static void phylink_resolve_flow(struct phylink_link_state *state)
746 bool tx_pause, rx_pause;
748 state->pause = MLO_PAUSE_NONE;
749 if (state->duplex == DUPLEX_FULL) {
750 linkmode_resolve_pause(state->advertising,
751 state->lp_advertising,
752 &tx_pause, &rx_pause);
754 state->pause |= MLO_PAUSE_TX;
756 state->pause |= MLO_PAUSE_RX;
760 static void phylink_mac_config(struct phylink *pl,
761 const struct phylink_link_state *state)
764 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
765 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
766 phy_modes(state->interface),
767 phy_speed_to_str(state->speed),
768 phy_duplex_to_str(state->duplex),
769 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
770 state->pause, state->link, state->an_enabled);
772 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
775 static void phylink_mac_pcs_an_restart(struct phylink *pl)
777 if (pl->link_config.an_enabled &&
778 phy_interface_mode_is_8023z(pl->link_config.interface) &&
779 phylink_autoneg_inband(pl->cur_link_an_mode)) {
781 pl->pcs_ops->pcs_an_restart(pl->pcs);
782 else if (pl->config->legacy_pre_march2020)
783 pl->mac_ops->mac_an_restart(pl->config);
787 static void phylink_major_config(struct phylink *pl, bool restart,
788 const struct phylink_link_state *state)
790 struct phylink_pcs *pcs = NULL;
793 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
795 if (pl->using_mac_select_pcs) {
796 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
799 "mac_select_pcs unexpectedly failed: %pe\n",
805 if (pl->mac_ops->mac_prepare) {
806 err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
809 phylink_err(pl, "mac_prepare failed: %pe\n",
815 /* If we have a new PCS, switch to the new PCS after preparing the MAC
820 pl->pcs_ops = pcs->ops;
822 if (!pl->phylink_disable_state &&
823 pl->cfg_link_an_mode == MLO_AN_INBAND) {
825 mod_timer(&pl->link_poll, jiffies + HZ);
827 del_timer(&pl->link_poll);
831 phylink_mac_config(pl, state);
834 err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
837 !!(pl->link_config.pause &
840 phylink_err(pl, "pcs_config failed: %pe\n",
846 phylink_mac_pcs_an_restart(pl);
848 if (pl->mac_ops->mac_finish) {
849 err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
852 phylink_err(pl, "mac_finish failed: %pe\n",
858 * Reconfigure for a change of inband advertisement.
859 * If we have a separate PCS, we only need to call its pcs_config() method,
860 * and then restart AN if it indicates something changed. Otherwise, we do
861 * the full MAC reconfiguration.
863 static int phylink_change_inband_advert(struct phylink *pl)
867 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
870 if (!pl->pcs_ops && pl->config->legacy_pre_march2020) {
872 phylink_mac_config(pl, &pl->link_config);
873 phylink_mac_pcs_an_restart(pl);
877 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
878 phylink_an_mode_str(pl->cur_link_an_mode),
879 phy_modes(pl->link_config.interface),
880 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
881 pl->link_config.pause);
883 /* Modern PCS-based method; update the advert at the PCS, and
884 * restart negotiation if the pcs_config() helper indicates that
885 * the programmed advertisement has changed.
887 ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
888 pl->link_config.interface,
889 pl->link_config.advertising,
890 !!(pl->link_config.pause & MLO_PAUSE_AN));
895 phylink_mac_pcs_an_restart(pl);
900 static void phylink_mac_pcs_get_state(struct phylink *pl,
901 struct phylink_link_state *state)
903 linkmode_copy(state->advertising, pl->link_config.advertising);
904 linkmode_zero(state->lp_advertising);
905 state->interface = pl->link_config.interface;
906 state->an_enabled = pl->link_config.an_enabled;
907 if (state->an_enabled) {
908 state->speed = SPEED_UNKNOWN;
909 state->duplex = DUPLEX_UNKNOWN;
910 state->pause = MLO_PAUSE_NONE;
912 state->speed = pl->link_config.speed;
913 state->duplex = pl->link_config.duplex;
914 state->pause = pl->link_config.pause;
916 state->an_complete = 0;
920 pl->pcs_ops->pcs_get_state(pl->pcs, state);
921 else if (pl->mac_ops->mac_pcs_get_state &&
922 pl->config->legacy_pre_march2020)
923 pl->mac_ops->mac_pcs_get_state(pl->config, state);
928 /* The fixed state is... fixed except for the link state,
929 * which may be determined by a GPIO or a callback.
931 static void phylink_get_fixed_state(struct phylink *pl,
932 struct phylink_link_state *state)
934 *state = pl->link_config;
935 if (pl->config->get_fixed_state)
936 pl->config->get_fixed_state(pl->config, state);
937 else if (pl->link_gpio)
938 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
940 phylink_resolve_flow(state);
943 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
945 struct phylink_link_state link_state;
947 switch (pl->cur_link_an_mode) {
949 link_state = pl->phy_state;
953 phylink_get_fixed_state(pl, &link_state);
957 link_state = pl->link_config;
958 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
959 link_state.pause = MLO_PAUSE_NONE;
962 default: /* can't happen */
966 link_state.link = false;
968 phylink_apply_manual_flow(pl, &link_state);
969 phylink_major_config(pl, force_restart, &link_state);
972 static const char *phylink_pause_to_str(int pause)
974 switch (pause & MLO_PAUSE_TXRX_MASK) {
975 case MLO_PAUSE_TX | MLO_PAUSE_RX:
986 static void phylink_link_up(struct phylink *pl,
987 struct phylink_link_state link_state)
989 struct net_device *ndev = pl->netdev;
991 pl->cur_interface = link_state.interface;
993 if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
994 pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
996 link_state.speed, link_state.duplex);
998 pl->mac_ops->mac_link_up(pl->config, pl->phydev,
999 pl->cur_link_an_mode, pl->cur_interface,
1000 link_state.speed, link_state.duplex,
1001 !!(link_state.pause & MLO_PAUSE_TX),
1002 !!(link_state.pause & MLO_PAUSE_RX));
1005 netif_carrier_on(ndev);
1008 "Link is Up - %s/%s - flow control %s\n",
1009 phy_speed_to_str(link_state.speed),
1010 phy_duplex_to_str(link_state.duplex),
1011 phylink_pause_to_str(link_state.pause));
1014 static void phylink_link_down(struct phylink *pl)
1016 struct net_device *ndev = pl->netdev;
1019 netif_carrier_off(ndev);
1020 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
1022 phylink_info(pl, "Link is Down\n");
1025 static void phylink_resolve(struct work_struct *w)
1027 struct phylink *pl = container_of(w, struct phylink, resolve);
1028 struct phylink_link_state link_state;
1029 struct net_device *ndev = pl->netdev;
1030 bool mac_config = false;
1031 bool retrigger = false;
1032 bool cur_link_state;
1034 mutex_lock(&pl->state_mutex);
1036 cur_link_state = netif_carrier_ok(ndev);
1038 cur_link_state = pl->old_link_state;
1040 if (pl->phylink_disable_state) {
1041 pl->mac_link_dropped = false;
1042 link_state.link = false;
1043 } else if (pl->mac_link_dropped) {
1044 link_state.link = false;
1047 switch (pl->cur_link_an_mode) {
1049 link_state = pl->phy_state;
1050 phylink_apply_manual_flow(pl, &link_state);
1051 mac_config = link_state.link;
1055 phylink_get_fixed_state(pl, &link_state);
1056 mac_config = link_state.link;
1060 phylink_mac_pcs_get_state(pl, &link_state);
1062 /* The PCS may have a latching link-fail indicator.
1063 * If the link was up, bring the link down and
1064 * re-trigger the resolve. Otherwise, re-read the
1065 * PCS state to get the current status of the link.
1067 if (!link_state.link) {
1071 phylink_mac_pcs_get_state(pl,
1075 /* If we have a phy, the "up" state is the union of
1076 * both the PHY and the MAC
1079 link_state.link &= pl->phy_state.link;
1081 /* Only update if the PHY link is up */
1082 if (pl->phydev && pl->phy_state.link) {
1083 /* If the interface has changed, force a
1084 * link down event if the link isn't already
1085 * down, and re-resolve.
1087 if (link_state.interface !=
1088 pl->phy_state.interface) {
1090 link_state.link = false;
1092 link_state.interface = pl->phy_state.interface;
1094 /* If we have a PHY, we need to update with
1095 * the PHY flow control bits.
1097 link_state.pause = pl->phy_state.pause;
1100 phylink_apply_manual_flow(pl, &link_state);
1106 if (link_state.interface != pl->link_config.interface) {
1107 /* The interface has changed, force the link down and
1110 if (cur_link_state) {
1111 phylink_link_down(pl);
1112 cur_link_state = false;
1114 phylink_major_config(pl, false, &link_state);
1115 pl->link_config.interface = link_state.interface;
1116 } else if (!pl->pcs_ops && pl->config->legacy_pre_march2020) {
1117 /* The interface remains unchanged, only the speed,
1118 * duplex or pause settings have changed. Call the
1119 * old mac_config() method to configure the MAC/PCS
1120 * only if we do not have a legacy MAC driver.
1122 phylink_mac_config(pl, &link_state);
1126 if (link_state.link != cur_link_state) {
1127 pl->old_link_state = link_state.link;
1128 if (!link_state.link)
1129 phylink_link_down(pl);
1131 phylink_link_up(pl, link_state);
1133 if (!link_state.link && retrigger) {
1134 pl->mac_link_dropped = false;
1135 queue_work(system_power_efficient_wq, &pl->resolve);
1137 mutex_unlock(&pl->state_mutex);
1140 static void phylink_run_resolve(struct phylink *pl)
1142 if (!pl->phylink_disable_state)
1143 queue_work(system_power_efficient_wq, &pl->resolve);
1146 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
1148 unsigned long state = pl->phylink_disable_state;
1150 set_bit(bit, &pl->phylink_disable_state);
1152 queue_work(system_power_efficient_wq, &pl->resolve);
1153 flush_work(&pl->resolve);
1157 static void phylink_enable_and_run_resolve(struct phylink *pl, int bit)
1159 clear_bit(bit, &pl->phylink_disable_state);
1160 phylink_run_resolve(pl);
1163 static void phylink_fixed_poll(struct timer_list *t)
1165 struct phylink *pl = container_of(t, struct phylink, link_poll);
1167 mod_timer(t, jiffies + HZ);
1169 phylink_run_resolve(pl);
1172 static const struct sfp_upstream_ops sfp_phylink_ops;
1174 static int phylink_register_sfp(struct phylink *pl,
1175 struct fwnode_handle *fwnode)
1177 struct sfp_bus *bus;
1183 bus = sfp_bus_find_fwnode(fwnode);
1185 phylink_err(pl, "unable to attach SFP bus: %pe\n", bus);
1186 return PTR_ERR(bus);
1191 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
1198 * phylink_create() - create a phylink instance
1199 * @config: a pointer to the target &struct phylink_config
1200 * @fwnode: a pointer to a &struct fwnode_handle describing the network
1202 * @iface: the desired link mode defined by &typedef phy_interface_t
1203 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
1205 * Create a new phylink instance, and parse the link parameters found in @np.
1206 * This will parse in-band modes, fixed-link or SFP configuration.
1208 * Note: the rtnl lock must not be held when calling this function.
1210 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
1211 * must use IS_ERR() to check for errors from this function.
1213 struct phylink *phylink_create(struct phylink_config *config,
1214 struct fwnode_handle *fwnode,
1215 phy_interface_t iface,
1216 const struct phylink_mac_ops *mac_ops)
1218 bool using_mac_select_pcs = false;
1222 if (mac_ops->mac_select_pcs &&
1223 mac_ops->mac_select_pcs(config, PHY_INTERFACE_MODE_NA) !=
1224 ERR_PTR(-EOPNOTSUPP))
1225 using_mac_select_pcs = true;
1227 /* Validate the supplied configuration */
1228 if (using_mac_select_pcs &&
1229 phy_interface_empty(config->supported_interfaces)) {
1230 dev_err(config->dev,
1231 "phylink: error: empty supported_interfaces but mac_select_pcs() method present\n");
1232 return ERR_PTR(-EINVAL);
1235 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
1237 return ERR_PTR(-ENOMEM);
1239 mutex_init(&pl->state_mutex);
1240 INIT_WORK(&pl->resolve, phylink_resolve);
1242 pl->config = config;
1243 if (config->type == PHYLINK_NETDEV) {
1244 pl->netdev = to_net_dev(config->dev);
1245 } else if (config->type == PHYLINK_DEV) {
1246 pl->dev = config->dev;
1249 return ERR_PTR(-EINVAL);
1252 pl->using_mac_select_pcs = using_mac_select_pcs;
1253 pl->phy_state.interface = iface;
1254 pl->link_interface = iface;
1255 if (iface == PHY_INTERFACE_MODE_MOCA)
1256 pl->link_port = PORT_BNC;
1258 pl->link_port = PORT_MII;
1259 pl->link_config.interface = iface;
1260 pl->link_config.pause = MLO_PAUSE_AN;
1261 pl->link_config.speed = SPEED_UNKNOWN;
1262 pl->link_config.duplex = DUPLEX_UNKNOWN;
1263 pl->link_config.an_enabled = true;
1264 pl->mac_ops = mac_ops;
1265 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1266 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
1268 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1269 linkmode_copy(pl->link_config.advertising, pl->supported);
1270 phylink_validate(pl, pl->supported, &pl->link_config);
1272 ret = phylink_parse_mode(pl, fwnode);
1275 return ERR_PTR(ret);
1278 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
1279 ret = phylink_parse_fixedlink(pl, fwnode);
1282 return ERR_PTR(ret);
1286 pl->cur_link_an_mode = pl->cfg_link_an_mode;
1288 ret = phylink_register_sfp(pl, fwnode);
1291 return ERR_PTR(ret);
1296 EXPORT_SYMBOL_GPL(phylink_create);
1299 * phylink_destroy() - cleanup and destroy the phylink instance
1300 * @pl: a pointer to a &struct phylink returned from phylink_create()
1302 * Destroy a phylink instance. Any PHY that has been attached must have been
1303 * cleaned up via phylink_disconnect_phy() prior to calling this function.
1305 * Note: the rtnl lock must not be held when calling this function.
1307 void phylink_destroy(struct phylink *pl)
1309 sfp_bus_del_upstream(pl->sfp_bus);
1311 gpiod_put(pl->link_gpio);
1313 cancel_work_sync(&pl->resolve);
1316 EXPORT_SYMBOL_GPL(phylink_destroy);
1318 static void phylink_phy_change(struct phy_device *phydev, bool up)
1320 struct phylink *pl = phydev->phylink;
1321 bool tx_pause, rx_pause;
1323 phy_get_pause(phydev, &tx_pause, &rx_pause);
1325 mutex_lock(&pl->state_mutex);
1326 pl->phy_state.speed = phydev->speed;
1327 pl->phy_state.duplex = phydev->duplex;
1328 pl->phy_state.pause = MLO_PAUSE_NONE;
1330 pl->phy_state.pause |= MLO_PAUSE_TX;
1332 pl->phy_state.pause |= MLO_PAUSE_RX;
1333 pl->phy_state.interface = phydev->interface;
1334 pl->phy_state.link = up;
1335 mutex_unlock(&pl->state_mutex);
1337 phylink_run_resolve(pl);
1339 phylink_dbg(pl, "phy link %s %s/%s/%s/%s\n", up ? "up" : "down",
1340 phy_modes(phydev->interface),
1341 phy_speed_to_str(phydev->speed),
1342 phy_duplex_to_str(phydev->duplex),
1343 phylink_pause_to_str(pl->phy_state.pause));
1346 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
1347 phy_interface_t interface)
1349 struct phylink_link_state config;
1350 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
1355 * This is the new way of dealing with flow control for PHYs,
1356 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
1357 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
1358 * using our validate call to the MAC, we rely upon the MAC
1359 * clearing the bits from both supported and advertising fields.
1361 phy_support_asym_pause(phy);
1363 memset(&config, 0, sizeof(config));
1364 linkmode_copy(supported, phy->supported);
1365 linkmode_copy(config.advertising, phy->advertising);
1367 /* Clause 45 PHYs switch their Serdes lane between several different
1368 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
1369 * speeds. We really need to know which interface modes the PHY and
1370 * MAC supports to properly work out which linkmodes can be supported.
1373 interface != PHY_INTERFACE_MODE_RXAUI &&
1374 interface != PHY_INTERFACE_MODE_XAUI &&
1375 interface != PHY_INTERFACE_MODE_USXGMII)
1376 config.interface = PHY_INTERFACE_MODE_NA;
1378 config.interface = interface;
1380 ret = phylink_validate(pl, supported, &config);
1382 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %pe\n",
1383 phy_modes(config.interface),
1384 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1385 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1391 phy->phy_link_change = phylink_phy_change;
1393 irq_str = phy_attached_info_irq(phy);
1395 "PHY [%s] driver [%s] (irq=%s)\n",
1396 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1399 mutex_lock(&phy->lock);
1400 mutex_lock(&pl->state_mutex);
1402 pl->phy_state.interface = interface;
1403 pl->phy_state.pause = MLO_PAUSE_NONE;
1404 pl->phy_state.speed = SPEED_UNKNOWN;
1405 pl->phy_state.duplex = DUPLEX_UNKNOWN;
1406 linkmode_copy(pl->supported, supported);
1407 linkmode_copy(pl->link_config.advertising, config.advertising);
1409 /* Restrict the phy advertisement according to the MAC support. */
1410 linkmode_copy(phy->advertising, config.advertising);
1411 mutex_unlock(&pl->state_mutex);
1412 mutex_unlock(&phy->lock);
1415 "phy: %s setting supported %*pb advertising %*pb\n",
1416 phy_modes(interface),
1417 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1418 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1420 if (phy_interrupt_is_valid(phy))
1421 phy_request_interrupt(phy);
1426 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1427 phy_interface_t interface)
1429 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1430 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1431 phy_interface_mode_is_8023z(interface))))
1437 return phy_attach_direct(pl->netdev, phy, 0, interface);
1441 * phylink_connect_phy() - connect a PHY to the phylink instance
1442 * @pl: a pointer to a &struct phylink returned from phylink_create()
1443 * @phy: a pointer to a &struct phy_device.
1445 * Connect @phy to the phylink instance specified by @pl by calling
1446 * phy_attach_direct(). Configure the @phy according to the MAC driver's
1447 * capabilities, start the PHYLIB state machine and enable any interrupts
1448 * that the PHY supports.
1450 * This updates the phylink's ethtool supported and advertising link mode
1453 * Returns 0 on success or a negative errno.
1455 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1459 /* Use PHY device/driver interface */
1460 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1461 pl->link_interface = phy->interface;
1462 pl->link_config.interface = pl->link_interface;
1465 ret = phylink_attach_phy(pl, phy, pl->link_interface);
1469 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1475 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1478 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1479 * @pl: a pointer to a &struct phylink returned from phylink_create()
1480 * @dn: a pointer to a &struct device_node.
1481 * @flags: PHY-specific flags to communicate to the PHY device driver
1483 * Connect the phy specified in the device node @dn to the phylink instance
1484 * specified by @pl. Actions specified in phylink_connect_phy() will be
1487 * Returns 0 on success or a negative errno.
1489 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1492 return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
1494 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1497 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
1498 * @pl: a pointer to a &struct phylink returned from phylink_create()
1499 * @fwnode: a pointer to a &struct fwnode_handle.
1500 * @flags: PHY-specific flags to communicate to the PHY device driver
1502 * Connect the phy specified @fwnode to the phylink instance specified
1505 * Returns 0 on success or a negative errno.
1507 int phylink_fwnode_phy_connect(struct phylink *pl,
1508 struct fwnode_handle *fwnode,
1511 struct fwnode_handle *phy_fwnode;
1512 struct phy_device *phy_dev;
1515 /* Fixed links and 802.3z are handled without needing a PHY */
1516 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1517 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1518 phy_interface_mode_is_8023z(pl->link_interface)))
1521 phy_fwnode = fwnode_get_phy_node(fwnode);
1522 if (IS_ERR(phy_fwnode)) {
1523 if (pl->cfg_link_an_mode == MLO_AN_PHY)
1528 phy_dev = fwnode_phy_find_device(phy_fwnode);
1529 /* We're done with the phy_node handle */
1530 fwnode_handle_put(phy_fwnode);
1534 /* Use PHY device/driver interface */
1535 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1536 pl->link_interface = phy_dev->interface;
1537 pl->link_config.interface = pl->link_interface;
1540 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1541 pl->link_interface);
1543 phy_device_free(phy_dev);
1547 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1549 phy_detach(phy_dev);
1553 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
1556 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1558 * @pl: a pointer to a &struct phylink returned from phylink_create()
1560 * Disconnect any current PHY from the phylink instance described by @pl.
1562 void phylink_disconnect_phy(struct phylink *pl)
1564 struct phy_device *phy;
1570 mutex_lock(&phy->lock);
1571 mutex_lock(&pl->state_mutex);
1573 mutex_unlock(&pl->state_mutex);
1574 mutex_unlock(&phy->lock);
1575 flush_work(&pl->resolve);
1577 phy_disconnect(phy);
1580 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1583 * phylink_mac_change() - notify phylink of a change in MAC state
1584 * @pl: a pointer to a &struct phylink returned from phylink_create()
1585 * @up: indicates whether the link is currently up.
1587 * The MAC driver should call this driver when the state of its link
1588 * changes (eg, link failure, new negotiation results, etc.)
1590 void phylink_mac_change(struct phylink *pl, bool up)
1593 pl->mac_link_dropped = true;
1594 phylink_run_resolve(pl);
1595 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1597 EXPORT_SYMBOL_GPL(phylink_mac_change);
1599 static irqreturn_t phylink_link_handler(int irq, void *data)
1601 struct phylink *pl = data;
1603 phylink_run_resolve(pl);
1609 * phylink_start() - start a phylink instance
1610 * @pl: a pointer to a &struct phylink returned from phylink_create()
1612 * Start the phylink instance specified by @pl, configuring the MAC for the
1613 * desired link mode(s) and negotiation style. This should be called from the
1614 * network device driver's &struct net_device_ops ndo_open() method.
1616 void phylink_start(struct phylink *pl)
1622 phylink_info(pl, "configuring for %s/%s link mode\n",
1623 phylink_an_mode_str(pl->cur_link_an_mode),
1624 phy_modes(pl->link_config.interface));
1626 /* Always set the carrier off */
1628 netif_carrier_off(pl->netdev);
1630 /* Apply the link configuration to the MAC when starting. This allows
1631 * a fixed-link to start with the correct parameters, and also
1632 * ensures that we set the appropriate advertisement for Serdes links.
1634 * Restart autonegotiation if using 802.3z to ensure that the link
1635 * parameters are properly negotiated. This is necessary for DSA
1636 * switches using 802.3z negotiation to ensure they see our modes.
1638 phylink_mac_initial_config(pl, true);
1640 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_STOPPED);
1642 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1643 int irq = gpiod_to_irq(pl->link_gpio);
1646 if (!request_irq(irq, phylink_link_handler,
1647 IRQF_TRIGGER_RISING |
1648 IRQF_TRIGGER_FALLING,
1658 switch (pl->cfg_link_an_mode) {
1660 poll |= pl->config->poll_fixed_state;
1664 poll |= pl->pcs->poll;
1668 mod_timer(&pl->link_poll, jiffies + HZ);
1670 phy_start(pl->phydev);
1672 sfp_upstream_start(pl->sfp_bus);
1674 EXPORT_SYMBOL_GPL(phylink_start);
1677 * phylink_stop() - stop a phylink instance
1678 * @pl: a pointer to a &struct phylink returned from phylink_create()
1680 * Stop the phylink instance specified by @pl. This should be called from the
1681 * network device driver's &struct net_device_ops ndo_stop() method. The
1682 * network device's carrier state should not be changed prior to calling this
1685 * This will synchronously bring down the link if the link is not already
1686 * down (in other words, it will trigger a mac_link_down() method call.)
1688 void phylink_stop(struct phylink *pl)
1693 sfp_upstream_stop(pl->sfp_bus);
1695 phy_stop(pl->phydev);
1696 del_timer_sync(&pl->link_poll);
1698 free_irq(pl->link_irq, pl);
1702 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1704 EXPORT_SYMBOL_GPL(phylink_stop);
1707 * phylink_suspend() - handle a network device suspend event
1708 * @pl: a pointer to a &struct phylink returned from phylink_create()
1709 * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
1711 * Handle a network device suspend event. There are several cases:
1713 * - If Wake-on-Lan is not active, we can bring down the link between
1714 * the MAC and PHY by calling phylink_stop().
1715 * - If Wake-on-Lan is active, and being handled only by the PHY, we
1716 * can also bring down the link between the MAC and PHY.
1717 * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
1718 * still needs to receive packets, so we can not bring the link down.
1720 void phylink_suspend(struct phylink *pl, bool mac_wol)
1724 if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) {
1725 /* Wake-on-Lan enabled, MAC handling */
1726 mutex_lock(&pl->state_mutex);
1728 /* Stop the resolver bringing the link up */
1729 __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1731 /* Disable the carrier, to prevent transmit timeouts,
1732 * but one would hope all packets have been sent. This
1733 * also means phylink_resolve() will do nothing.
1736 netif_carrier_off(pl->netdev);
1738 pl->old_link_state = false;
1740 /* We do not call mac_link_down() here as we want the
1741 * link to remain up to receive the WoL packets.
1743 mutex_unlock(&pl->state_mutex);
1748 EXPORT_SYMBOL_GPL(phylink_suspend);
1751 * phylink_resume() - handle a network device resume event
1752 * @pl: a pointer to a &struct phylink returned from phylink_create()
1754 * Undo the effects of phylink_suspend(), returning the link to an
1755 * operational state.
1757 void phylink_resume(struct phylink *pl)
1761 if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
1762 /* Wake-on-Lan enabled, MAC handling */
1764 /* Call mac_link_down() so we keep the overall state balanced.
1765 * Do this under the state_mutex lock for consistency. This
1766 * will cause a "Link Down" message to be printed during
1767 * resume, which is harmless - the true link state will be
1768 * printed when we run a resolve.
1770 mutex_lock(&pl->state_mutex);
1771 phylink_link_down(pl);
1772 mutex_unlock(&pl->state_mutex);
1774 /* Re-apply the link parameters so that all the settings get
1775 * restored to the MAC.
1777 phylink_mac_initial_config(pl, true);
1779 /* Re-enable and re-resolve the link parameters */
1780 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_MAC_WOL);
1785 EXPORT_SYMBOL_GPL(phylink_resume);
1788 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1789 * @pl: a pointer to a &struct phylink returned from phylink_create()
1790 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1792 * Read the wake on lan parameters from the PHY attached to the phylink
1793 * instance specified by @pl. If no PHY is currently attached, report no
1794 * support for wake on lan.
1796 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1804 phy_ethtool_get_wol(pl->phydev, wol);
1806 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1809 * phylink_ethtool_set_wol() - set wake on lan parameters
1810 * @pl: a pointer to a &struct phylink returned from phylink_create()
1811 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1813 * Set the wake on lan parameters for the PHY attached to the phylink
1814 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1817 * Returns zero on success or negative errno code.
1819 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1821 int ret = -EOPNOTSUPP;
1826 ret = phy_ethtool_set_wol(pl->phydev, wol);
1830 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1832 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1834 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1836 linkmode_zero(mask);
1837 phylink_set_port_modes(mask);
1839 linkmode_and(dst, dst, mask);
1840 linkmode_or(dst, dst, b);
1843 static void phylink_get_ksettings(const struct phylink_link_state *state,
1844 struct ethtool_link_ksettings *kset)
1846 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1847 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1848 kset->base.speed = state->speed;
1849 kset->base.duplex = state->duplex;
1850 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1855 * phylink_ethtool_ksettings_get() - get the current link settings
1856 * @pl: a pointer to a &struct phylink returned from phylink_create()
1857 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1859 * Read the current link settings for the phylink instance specified by @pl.
1860 * This will be the link settings read from the MAC, PHY or fixed link
1861 * settings depending on the current negotiation mode.
1863 int phylink_ethtool_ksettings_get(struct phylink *pl,
1864 struct ethtool_link_ksettings *kset)
1866 struct phylink_link_state link_state;
1871 phy_ethtool_ksettings_get(pl->phydev, kset);
1873 kset->base.port = pl->link_port;
1875 linkmode_copy(kset->link_modes.supported, pl->supported);
1877 switch (pl->cur_link_an_mode) {
1879 /* We are using fixed settings. Report these as the
1880 * current link settings - and note that these also
1881 * represent the supported speeds/duplex/pause modes.
1883 phylink_get_fixed_state(pl, &link_state);
1884 phylink_get_ksettings(&link_state, kset);
1888 /* If there is a phy attached, then use the reported
1889 * settings from the phy with no modification.
1894 phylink_mac_pcs_get_state(pl, &link_state);
1896 /* The MAC is reporting the link results from its own PCS
1897 * layer via in-band status. Report these as the current
1900 phylink_get_ksettings(&link_state, kset);
1906 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1909 * phylink_ethtool_ksettings_set() - set the link settings
1910 * @pl: a pointer to a &struct phylink returned from phylink_create()
1911 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1913 int phylink_ethtool_ksettings_set(struct phylink *pl,
1914 const struct ethtool_link_ksettings *kset)
1916 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1917 struct phylink_link_state config;
1918 const struct phy_setting *s;
1923 /* We can rely on phylib for this update; we also do not need
1924 * to update the pl->link_config settings:
1925 * - the configuration returned via ksettings_get() will come
1926 * from phylib whenever a PHY is present.
1927 * - link_config.interface will be updated by the PHY calling
1928 * back via phylink_phy_change() and a subsequent resolve.
1929 * - initial link configuration for PHY mode comes from the
1930 * last phy state updated via phylink_phy_change().
1931 * - other configuration changes (e.g. pause modes) are
1932 * performed directly via phylib.
1933 * - if in in-band mode with a PHY, the link configuration
1934 * is passed on the link from the PHY, and all of
1935 * link_config.{speed,duplex,an_enabled,pause} are not used.
1936 * - the only possible use would be link_config.advertising
1937 * pause modes when in 1000base-X mode with a PHY, but in
1938 * the presence of a PHY, this should not be changed as that
1939 * should be determined from the media side advertisement.
1941 return phy_ethtool_ksettings_set(pl->phydev, kset);
1944 config = pl->link_config;
1946 /* Mask out unsupported advertisements */
1947 linkmode_and(config.advertising, kset->link_modes.advertising,
1950 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1951 switch (kset->base.autoneg) {
1952 case AUTONEG_DISABLE:
1953 /* Autonegotiation disabled, select a suitable speed and
1956 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1957 pl->supported, false);
1961 /* If we have a fixed link, refuse to change link parameters.
1962 * If the link parameters match, accept them but do nothing.
1964 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1965 if (s->speed != pl->link_config.speed ||
1966 s->duplex != pl->link_config.duplex)
1971 config.speed = s->speed;
1972 config.duplex = s->duplex;
1975 case AUTONEG_ENABLE:
1976 /* If we have a fixed link, allow autonegotiation (since that
1977 * is our default case) but do not allow the advertisement to
1978 * be changed. If the advertisement matches, simply return.
1980 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1981 if (!linkmode_equal(config.advertising,
1982 pl->link_config.advertising))
1987 config.speed = SPEED_UNKNOWN;
1988 config.duplex = DUPLEX_UNKNOWN;
1995 /* We have ruled out the case with a PHY attached, and the
1996 * fixed-link cases. All that is left are in-band links.
1998 config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
1999 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
2002 /* If this link is with an SFP, ensure that changes to advertised modes
2003 * also cause the associated interface to be selected such that the
2004 * link can be configured correctly.
2007 config.interface = sfp_select_interface(pl->sfp_bus,
2008 config.advertising);
2009 if (config.interface == PHY_INTERFACE_MODE_NA) {
2011 "selection of interface failed, advertisement %*pb\n",
2012 __ETHTOOL_LINK_MODE_MASK_NBITS,
2013 config.advertising);
2017 /* Revalidate with the selected interface */
2018 linkmode_copy(support, pl->supported);
2019 if (phylink_validate(pl, support, &config)) {
2020 phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
2021 phylink_an_mode_str(pl->cur_link_an_mode),
2022 phy_modes(config.interface),
2023 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2027 /* Validate without changing the current supported mask. */
2028 linkmode_copy(support, pl->supported);
2029 if (phylink_validate(pl, support, &config))
2033 /* If autonegotiation is enabled, we must have an advertisement */
2034 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
2037 mutex_lock(&pl->state_mutex);
2038 pl->link_config.speed = config.speed;
2039 pl->link_config.duplex = config.duplex;
2040 pl->link_config.an_enabled = config.an_enabled;
2042 if (pl->link_config.interface != config.interface) {
2043 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
2044 /* We need to force the link down, then change the interface */
2045 if (pl->old_link_state) {
2046 phylink_link_down(pl);
2047 pl->old_link_state = false;
2049 if (!test_bit(PHYLINK_DISABLE_STOPPED,
2050 &pl->phylink_disable_state))
2051 phylink_major_config(pl, false, &config);
2052 pl->link_config.interface = config.interface;
2053 linkmode_copy(pl->link_config.advertising, config.advertising);
2054 } else if (!linkmode_equal(pl->link_config.advertising,
2055 config.advertising)) {
2056 linkmode_copy(pl->link_config.advertising, config.advertising);
2057 phylink_change_inband_advert(pl);
2059 mutex_unlock(&pl->state_mutex);
2063 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
2066 * phylink_ethtool_nway_reset() - restart negotiation
2067 * @pl: a pointer to a &struct phylink returned from phylink_create()
2069 * Restart negotiation for the phylink instance specified by @pl. This will
2070 * cause any attached phy to restart negotiation with the link partner, and
2071 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
2074 * Returns zero on success, or negative error code.
2076 int phylink_ethtool_nway_reset(struct phylink *pl)
2083 ret = phy_restart_aneg(pl->phydev);
2084 phylink_mac_pcs_an_restart(pl);
2088 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
2091 * phylink_ethtool_get_pauseparam() - get the current pause parameters
2092 * @pl: a pointer to a &struct phylink returned from phylink_create()
2093 * @pause: a pointer to a &struct ethtool_pauseparam
2095 void phylink_ethtool_get_pauseparam(struct phylink *pl,
2096 struct ethtool_pauseparam *pause)
2100 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
2101 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
2102 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
2104 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
2107 * phylink_ethtool_set_pauseparam() - set the current pause parameters
2108 * @pl: a pointer to a &struct phylink returned from phylink_create()
2109 * @pause: a pointer to a &struct ethtool_pauseparam
2111 int phylink_ethtool_set_pauseparam(struct phylink *pl,
2112 struct ethtool_pauseparam *pause)
2114 struct phylink_link_state *config = &pl->link_config;
2115 bool manual_changed;
2120 if (pl->cur_link_an_mode == MLO_AN_FIXED)
2123 if (!phylink_test(pl->supported, Pause) &&
2124 !phylink_test(pl->supported, Asym_Pause))
2127 if (!phylink_test(pl->supported, Asym_Pause) &&
2128 pause->rx_pause != pause->tx_pause)
2133 pause_state |= MLO_PAUSE_AN;
2134 if (pause->rx_pause)
2135 pause_state |= MLO_PAUSE_RX;
2136 if (pause->tx_pause)
2137 pause_state |= MLO_PAUSE_TX;
2139 mutex_lock(&pl->state_mutex);
2141 * See the comments for linkmode_set_pause(), wrt the deficiencies
2142 * with the current implementation. A solution to this issue would
2144 * ethtool Local device
2145 * rx tx Pause AsymDir
2150 * and then use the ethtool rx/tx enablement status to mask the
2151 * rx/tx pause resolution.
2153 linkmode_set_pause(config->advertising, pause->tx_pause,
2156 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
2157 (!(pause_state & MLO_PAUSE_AN) &&
2158 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
2160 config->pause = pause_state;
2162 /* Update our in-band advertisement, triggering a renegotiation if
2163 * the advertisement changed.
2166 phylink_change_inband_advert(pl);
2168 mutex_unlock(&pl->state_mutex);
2170 /* If we have a PHY, a change of the pause frame advertisement will
2171 * cause phylib to renegotiate (if AN is enabled) which will in turn
2172 * call our phylink_phy_change() and trigger a resolve. Note that
2173 * we can't hold our state mutex while calling phy_set_asym_pause().
2176 phy_set_asym_pause(pl->phydev, pause->rx_pause,
2179 /* If the manual pause settings changed, make sure we trigger a
2180 * resolve to update their state; we can not guarantee that the
2183 if (manual_changed) {
2184 pl->mac_link_dropped = true;
2185 phylink_run_resolve(pl);
2190 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
2193 * phylink_get_eee_err() - read the energy efficient ethernet error
2195 * @pl: a pointer to a &struct phylink returned from phylink_create().
2197 * Read the Energy Efficient Ethernet error counter from the PHY associated
2198 * with the phylink instance specified by @pl.
2200 * Returns positive error counter value, or negative error code.
2202 int phylink_get_eee_err(struct phylink *pl)
2209 ret = phy_get_eee_err(pl->phydev);
2213 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
2216 * phylink_init_eee() - init and check the EEE features
2217 * @pl: a pointer to a &struct phylink returned from phylink_create()
2218 * @clk_stop_enable: allow PHY to stop receive clock
2220 * Must be called either with RTNL held or within mac_link_up()
2222 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
2224 int ret = -EOPNOTSUPP;
2227 ret = phy_init_eee(pl->phydev, clk_stop_enable);
2231 EXPORT_SYMBOL_GPL(phylink_init_eee);
2234 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
2235 * @pl: a pointer to a &struct phylink returned from phylink_create()
2236 * @eee: a pointer to a &struct ethtool_eee for the read parameters
2238 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
2240 int ret = -EOPNOTSUPP;
2245 ret = phy_ethtool_get_eee(pl->phydev, eee);
2249 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
2252 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
2253 * @pl: a pointer to a &struct phylink returned from phylink_create()
2254 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
2256 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
2258 int ret = -EOPNOTSUPP;
2263 ret = phy_ethtool_set_eee(pl->phydev, eee);
2267 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
2269 /* This emulates MII registers for a fixed-mode phy operating as per the
2270 * passed in state. "aneg" defines if we report negotiation is possible.
2272 * FIXME: should deal with negotiation state too.
2274 static int phylink_mii_emul_read(unsigned int reg,
2275 struct phylink_link_state *state)
2277 struct fixed_phy_status fs;
2278 unsigned long *lpa = state->lp_advertising;
2281 fs.link = state->link;
2282 fs.speed = state->speed;
2283 fs.duplex = state->duplex;
2284 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
2285 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
2287 val = swphy_read_reg(reg, &fs);
2288 if (reg == MII_BMSR) {
2289 if (!state->an_complete)
2290 val &= ~BMSR_ANEGCOMPLETE;
2295 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
2298 struct phy_device *phydev = pl->phydev;
2301 if (mdio_phy_id_is_c45(phy_id)) {
2302 prtad = mdio_phy_id_prtad(phy_id);
2303 devad = mdio_phy_id_devad(phy_id);
2304 devad = mdiobus_c45_addr(devad, reg);
2305 } else if (phydev->is_c45) {
2311 devad = __ffs(phydev->c45_ids.mmds_present);
2315 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2317 devad = MDIO_MMD_AN;
2318 if (reg == MII_ADVERTISE)
2319 reg = MDIO_AN_ADVERTISE;
2327 devad = mdiobus_c45_addr(devad, reg);
2332 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
2335 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
2336 unsigned int reg, unsigned int val)
2338 struct phy_device *phydev = pl->phydev;
2341 if (mdio_phy_id_is_c45(phy_id)) {
2342 prtad = mdio_phy_id_prtad(phy_id);
2343 devad = mdio_phy_id_devad(phy_id);
2344 devad = mdiobus_c45_addr(devad, reg);
2345 } else if (phydev->is_c45) {
2351 devad = __ffs(phydev->c45_ids.mmds_present);
2355 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2357 devad = MDIO_MMD_AN;
2358 if (reg == MII_ADVERTISE)
2359 reg = MDIO_AN_ADVERTISE;
2367 devad = mdiobus_c45_addr(devad, reg);
2373 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
2376 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
2379 struct phylink_link_state state;
2382 switch (pl->cur_link_an_mode) {
2385 phylink_get_fixed_state(pl, &state);
2386 val = phylink_mii_emul_read(reg, &state);
2395 phylink_mac_pcs_get_state(pl, &state);
2396 val = phylink_mii_emul_read(reg, &state);
2401 return val & 0xffff;
2404 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
2405 unsigned int reg, unsigned int val)
2407 switch (pl->cur_link_an_mode) {
2422 * phylink_mii_ioctl() - generic mii ioctl interface
2423 * @pl: a pointer to a &struct phylink returned from phylink_create()
2424 * @ifr: a pointer to a &struct ifreq for socket ioctls
2425 * @cmd: ioctl cmd to execute
2427 * Perform the specified MII ioctl on the PHY attached to the phylink instance
2428 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
2430 * Returns: zero on success or negative error code.
2433 * read register from the current PHY.
2435 * read register from the specified PHY.
2437 * set a register on the specified PHY.
2439 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
2441 struct mii_ioctl_data *mii = if_mii(ifr);
2447 /* PHYs only exist for MLO_AN_PHY and SGMII */
2450 mii->phy_id = pl->phydev->mdio.addr;
2454 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
2462 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
2467 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
2477 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
2485 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
2497 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2500 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2502 * @pl: a pointer to a &struct phylink returned from phylink_create()
2503 * @sync: perform action synchronously
2505 * If we have a PHY that is not part of a SFP module, then set the speed
2506 * as described in the phy_speed_down() function. Please see this function
2507 * for a description of the @sync parameter.
2509 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2511 int phylink_speed_down(struct phylink *pl, bool sync)
2517 if (!pl->sfp_bus && pl->phydev)
2518 ret = phy_speed_down(pl->phydev, sync);
2522 EXPORT_SYMBOL_GPL(phylink_speed_down);
2525 * phylink_speed_up() - restore the advertised speeds prior to the call to
2526 * phylink_speed_down()
2527 * @pl: a pointer to a &struct phylink returned from phylink_create()
2529 * If we have a PHY that is not part of a SFP module, then restore the
2530 * PHY speeds as per phy_speed_up().
2532 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2534 int phylink_speed_up(struct phylink *pl)
2540 if (!pl->sfp_bus && pl->phydev)
2541 ret = phy_speed_up(pl->phydev);
2545 EXPORT_SYMBOL_GPL(phylink_speed_up);
2547 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2549 struct phylink *pl = upstream;
2551 pl->netdev->sfp_bus = bus;
2554 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2556 struct phylink *pl = upstream;
2558 pl->netdev->sfp_bus = NULL;
2561 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2562 const unsigned long *supported,
2563 const unsigned long *advertising)
2565 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2566 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2567 struct phylink_link_state config;
2568 phy_interface_t iface;
2572 linkmode_copy(support, supported);
2574 memset(&config, 0, sizeof(config));
2575 linkmode_copy(config.advertising, advertising);
2576 config.interface = PHY_INTERFACE_MODE_NA;
2577 config.speed = SPEED_UNKNOWN;
2578 config.duplex = DUPLEX_UNKNOWN;
2579 config.pause = MLO_PAUSE_AN;
2580 config.an_enabled = pl->link_config.an_enabled;
2582 /* Ignore errors if we're expecting a PHY to attach later */
2583 ret = phylink_validate(pl, support, &config);
2585 phylink_err(pl, "validation with support %*pb failed: %pe\n",
2586 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
2591 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2592 if (iface == PHY_INTERFACE_MODE_NA) {
2594 "selection of interface failed, advertisement %*pb\n",
2595 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2599 config.interface = iface;
2600 linkmode_copy(support1, support);
2601 ret = phylink_validate(pl, support1, &config);
2604 "validation of %s/%s with support %*pb failed: %pe\n",
2605 phylink_an_mode_str(mode),
2606 phy_modes(config.interface),
2607 __ETHTOOL_LINK_MODE_MASK_NBITS, support,
2612 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2613 phylink_an_mode_str(mode), phy_modes(config.interface),
2614 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2616 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2619 changed = !linkmode_equal(pl->supported, support) ||
2620 !linkmode_equal(pl->link_config.advertising,
2621 config.advertising);
2623 linkmode_copy(pl->supported, support);
2624 linkmode_copy(pl->link_config.advertising, config.advertising);
2627 if (pl->cur_link_an_mode != mode ||
2628 pl->link_config.interface != config.interface) {
2629 pl->link_config.interface = config.interface;
2630 pl->cur_link_an_mode = mode;
2634 phylink_info(pl, "switched to %s/%s link mode\n",
2635 phylink_an_mode_str(mode),
2636 phy_modes(config.interface));
2639 pl->link_port = pl->sfp_port;
2641 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2642 &pl->phylink_disable_state))
2643 phylink_mac_initial_config(pl, false);
2648 static int phylink_sfp_module_insert(void *upstream,
2649 const struct sfp_eeprom_id *id)
2651 struct phylink *pl = upstream;
2652 unsigned long *support = pl->sfp_support;
2656 linkmode_zero(support);
2657 sfp_parse_support(pl->sfp_bus, id, support);
2658 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2660 /* If this module may have a PHY connecting later, defer until later */
2661 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2662 if (pl->sfp_may_have_phy)
2665 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2668 static int phylink_sfp_module_start(void *upstream)
2670 struct phylink *pl = upstream;
2672 /* If this SFP module has a PHY, start the PHY now. */
2674 phy_start(pl->phydev);
2678 /* If the module may have a PHY but we didn't detect one we
2679 * need to configure the MAC here.
2681 if (!pl->sfp_may_have_phy)
2684 return phylink_sfp_config(pl, MLO_AN_INBAND,
2685 pl->sfp_support, pl->sfp_support);
2688 static void phylink_sfp_module_stop(void *upstream)
2690 struct phylink *pl = upstream;
2692 /* If this SFP module has a PHY, stop it. */
2694 phy_stop(pl->phydev);
2697 static void phylink_sfp_link_down(void *upstream)
2699 struct phylink *pl = upstream;
2703 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2706 static void phylink_sfp_link_up(void *upstream)
2708 struct phylink *pl = upstream;
2712 phylink_enable_and_run_resolve(pl, PHYLINK_DISABLE_LINK);
2715 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2716 * or 802.3z control word, so inband will not work.
2718 static bool phylink_phy_no_inband(struct phy_device *phy)
2720 return phy->is_c45 &&
2721 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2724 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2726 struct phylink *pl = upstream;
2727 phy_interface_t interface;
2732 * This is the new way of dealing with flow control for PHYs,
2733 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2734 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2735 * using our validate call to the MAC, we rely upon the MAC
2736 * clearing the bits from both supported and advertising fields.
2738 phy_support_asym_pause(phy);
2740 if (phylink_phy_no_inband(phy))
2743 mode = MLO_AN_INBAND;
2745 /* Do the initial configuration */
2746 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2750 interface = pl->link_config.interface;
2751 ret = phylink_attach_phy(pl, phy, interface);
2755 ret = phylink_bringup_phy(pl, phy, interface);
2762 static void phylink_sfp_disconnect_phy(void *upstream)
2764 phylink_disconnect_phy(upstream);
2767 static const struct sfp_upstream_ops sfp_phylink_ops = {
2768 .attach = phylink_sfp_attach,
2769 .detach = phylink_sfp_detach,
2770 .module_insert = phylink_sfp_module_insert,
2771 .module_start = phylink_sfp_module_start,
2772 .module_stop = phylink_sfp_module_stop,
2773 .link_up = phylink_sfp_link_up,
2774 .link_down = phylink_sfp_link_down,
2775 .connect_phy = phylink_sfp_connect_phy,
2776 .disconnect_phy = phylink_sfp_disconnect_phy,
2779 /* Helpers for MAC drivers */
2782 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2783 * @state: a pointer to a &struct phylink_link_state
2785 * Inspect the interface mode, advertising mask or forced speed and
2786 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2787 * the interface mode to suit. @state->interface is appropriately
2788 * updated, and the advertising mask has the "other" baseX_Full flag
2791 void phylink_helper_basex_speed(struct phylink_link_state *state)
2793 if (phy_interface_mode_is_8023z(state->interface)) {
2794 bool want_2500 = state->an_enabled ?
2795 phylink_test(state->advertising, 2500baseX_Full) :
2796 state->speed == SPEED_2500;
2799 phylink_clear(state->advertising, 1000baseX_Full);
2800 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2802 phylink_clear(state->advertising, 2500baseX_Full);
2803 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2807 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2809 static void phylink_decode_c37_word(struct phylink_link_state *state,
2810 uint16_t config_reg, int speed)
2812 bool tx_pause, rx_pause;
2815 if (speed == SPEED_2500)
2816 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2818 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2820 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2822 if (linkmode_test_bit(fd_bit, state->advertising) &&
2823 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2824 state->speed = speed;
2825 state->duplex = DUPLEX_FULL;
2827 /* negotiation failure */
2828 state->link = false;
2831 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2832 &tx_pause, &rx_pause);
2835 state->pause |= MLO_PAUSE_TX;
2837 state->pause |= MLO_PAUSE_RX;
2840 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2841 uint16_t config_reg)
2843 if (!(config_reg & LPA_SGMII_LINK)) {
2844 state->link = false;
2848 switch (config_reg & LPA_SGMII_SPD_MASK) {
2850 state->speed = SPEED_10;
2853 state->speed = SPEED_100;
2855 case LPA_SGMII_1000:
2856 state->speed = SPEED_1000;
2859 state->link = false;
2862 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2863 state->duplex = DUPLEX_FULL;
2865 state->duplex = DUPLEX_HALF;
2869 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2870 * @state: a pointer to a struct phylink_link_state.
2871 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2873 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2874 * code word. Decode the USXGMII code word and populate the corresponding fields
2875 * (speed, duplex) into the phylink_link_state structure.
2877 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2880 switch (lpa & MDIO_USXGMII_SPD_MASK) {
2881 case MDIO_USXGMII_10:
2882 state->speed = SPEED_10;
2884 case MDIO_USXGMII_100:
2885 state->speed = SPEED_100;
2887 case MDIO_USXGMII_1000:
2888 state->speed = SPEED_1000;
2890 case MDIO_USXGMII_2500:
2891 state->speed = SPEED_2500;
2893 case MDIO_USXGMII_5000:
2894 state->speed = SPEED_5000;
2896 case MDIO_USXGMII_10G:
2897 state->speed = SPEED_10000;
2900 state->link = false;
2904 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2905 state->duplex = DUPLEX_FULL;
2907 state->duplex = DUPLEX_HALF;
2909 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2912 * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
2913 * @state: a pointer to a &struct phylink_link_state.
2914 * @bmsr: The value of the %MII_BMSR register
2915 * @lpa: The value of the %MII_LPA register
2917 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2918 * clause 37 negotiation and/or SGMII control.
2920 * Parse the Clause 37 or Cisco SGMII link partner negotiation word into
2921 * the phylink @state structure. This is suitable to be used for implementing
2922 * the mac_pcs_get_state() member of the struct phylink_mac_ops structure if
2923 * accessing @bmsr and @lpa cannot be done with MDIO directly.
2925 void phylink_mii_c22_pcs_decode_state(struct phylink_link_state *state,
2928 state->link = !!(bmsr & BMSR_LSTATUS);
2929 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2930 /* If there is no link or autonegotiation is disabled, the LP advertisement
2931 * data is not meaningful, so don't go any further.
2933 if (!state->link || !state->an_enabled)
2936 switch (state->interface) {
2937 case PHY_INTERFACE_MODE_1000BASEX:
2938 phylink_decode_c37_word(state, lpa, SPEED_1000);
2941 case PHY_INTERFACE_MODE_2500BASEX:
2942 phylink_decode_c37_word(state, lpa, SPEED_2500);
2945 case PHY_INTERFACE_MODE_SGMII:
2946 case PHY_INTERFACE_MODE_QSGMII:
2947 phylink_decode_sgmii_word(state, lpa);
2951 state->link = false;
2955 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_decode_state);
2958 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2959 * @pcs: a pointer to a &struct mdio_device.
2960 * @state: a pointer to a &struct phylink_link_state.
2962 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2963 * clause 37 negotiation and/or SGMII control.
2965 * Read the MAC PCS state from the MII device configured in @config and
2966 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2967 * the phylink @state structure. This is suitable to be directly plugged
2968 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2971 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2972 struct phylink_link_state *state)
2976 bmsr = mdiodev_read(pcs, MII_BMSR);
2977 lpa = mdiodev_read(pcs, MII_LPA);
2978 if (bmsr < 0 || lpa < 0) {
2979 state->link = false;
2983 phylink_mii_c22_pcs_decode_state(state, bmsr, lpa);
2985 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2988 * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
2990 * @interface: the PHY interface mode being configured
2991 * @advertising: the ethtool advertisement mask
2993 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2994 * clause 37 negotiation and/or SGMII control.
2996 * Encode the clause 37 PCS advertisement as specified by @interface and
2999 * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
3001 int phylink_mii_c22_pcs_encode_advertisement(phy_interface_t interface,
3002 const unsigned long *advertising)
3006 switch (interface) {
3007 case PHY_INTERFACE_MODE_1000BASEX:
3008 case PHY_INTERFACE_MODE_2500BASEX:
3009 adv = ADVERTISE_1000XFULL;
3010 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3012 adv |= ADVERTISE_1000XPAUSE;
3013 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3015 adv |= ADVERTISE_1000XPSE_ASYM;
3017 case PHY_INTERFACE_MODE_SGMII:
3020 /* Nothing to do for other modes */
3024 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_encode_advertisement);
3027 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
3028 * @pcs: a pointer to a &struct mdio_device.
3029 * @mode: link autonegotiation mode
3030 * @interface: the PHY interface mode being configured
3031 * @advertising: the ethtool advertisement mask
3033 * Configure a Clause 22 PCS PHY with the appropriate negotiation
3034 * parameters for the @mode, @interface and @advertising parameters.
3035 * Returns negative error number on failure, zero if the advertisement
3036 * has not changed, or positive if there is a change.
3038 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
3039 phy_interface_t interface,
3040 const unsigned long *advertising)
3046 adv = phylink_mii_c22_pcs_encode_advertisement(interface, advertising);
3048 ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
3049 MII_ADVERTISE, 0xffff, adv);
3055 /* Ensure ISOLATE bit is disabled */
3056 if (mode == MLO_AN_INBAND &&
3057 linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, advertising))
3058 bmcr = BMCR_ANENABLE;
3062 ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
3068 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
3071 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
3072 * @pcs: a pointer to a &struct mdio_device.
3074 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
3075 * clause 37 negotiation.
3077 * Restart the clause 37 negotiation with the link partner. This is
3078 * suitable to be directly plugged into the mac_pcs_get_state() member
3079 * of the struct phylink_mac_ops structure.
3081 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
3083 int val = mdiodev_read(pcs, MII_BMCR);
3086 val |= BMCR_ANRESTART;
3088 mdiodev_write(pcs, MII_BMCR, val);
3091 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
3093 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
3094 struct phylink_link_state *state)
3096 struct mii_bus *bus = pcs->bus;
3097 int addr = pcs->addr;
3100 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
3102 state->link = false;
3106 state->link = !!(stat & MDIO_STAT1_LSTATUS);
3110 switch (state->interface) {
3111 case PHY_INTERFACE_MODE_10GBASER:
3112 state->speed = SPEED_10000;
3113 state->duplex = DUPLEX_FULL;
3120 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
3122 MODULE_LICENSE("GPL v2");