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;
78 struct sfp_bus *sfp_bus;
79 bool sfp_may_have_phy;
80 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
84 #define phylink_printk(level, pl, fmt, ...) \
86 if ((pl)->config->type == PHYLINK_NETDEV) \
87 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
88 else if ((pl)->config->type == PHYLINK_DEV) \
89 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
92 #define phylink_err(pl, fmt, ...) \
93 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
94 #define phylink_warn(pl, fmt, ...) \
95 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
96 #define phylink_info(pl, fmt, ...) \
97 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
98 #if defined(CONFIG_DYNAMIC_DEBUG)
99 #define phylink_dbg(pl, fmt, ...) \
101 if ((pl)->config->type == PHYLINK_NETDEV) \
102 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
103 else if ((pl)->config->type == PHYLINK_DEV) \
104 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
107 #define phylink_dbg(pl, fmt, ...) \
108 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
110 #define phylink_dbg(pl, fmt, ...) \
113 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
118 * phylink_set_port_modes() - set the port type modes in the ethtool mask
119 * @mask: ethtool link mode mask
121 * Sets all the port type modes in the ethtool mask. MAC drivers should
122 * use this in their 'validate' callback.
124 void phylink_set_port_modes(unsigned long *mask)
126 phylink_set(mask, TP);
127 phylink_set(mask, AUI);
128 phylink_set(mask, MII);
129 phylink_set(mask, FIBRE);
130 phylink_set(mask, BNC);
131 phylink_set(mask, Backplane);
133 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
135 void phylink_set_10g_modes(unsigned long *mask)
137 phylink_set(mask, 10000baseT_Full);
138 phylink_set(mask, 10000baseCR_Full);
139 phylink_set(mask, 10000baseSR_Full);
140 phylink_set(mask, 10000baseLR_Full);
141 phylink_set(mask, 10000baseLRM_Full);
142 phylink_set(mask, 10000baseER_Full);
144 EXPORT_SYMBOL_GPL(phylink_set_10g_modes);
146 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
148 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
150 phylink_set_port_modes(tmp);
151 phylink_set(tmp, Autoneg);
152 phylink_set(tmp, Pause);
153 phylink_set(tmp, Asym_Pause);
155 return linkmode_subset(linkmode, tmp);
158 static const char *phylink_an_mode_str(unsigned int mode)
160 static const char *modestr[] = {
161 [MLO_AN_PHY] = "phy",
162 [MLO_AN_FIXED] = "fixed",
163 [MLO_AN_INBAND] = "inband",
166 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
169 static int phylink_validate_any(struct phylink *pl, unsigned long *supported,
170 struct phylink_link_state *state)
172 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, };
173 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, };
174 __ETHTOOL_DECLARE_LINK_MODE_MASK(s);
175 struct phylink_link_state t;
178 for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) {
179 if (test_bit(intf, pl->config->supported_interfaces)) {
180 linkmode_copy(s, supported);
184 pl->mac_ops->validate(pl->config, s, &t);
185 linkmode_or(all_s, all_s, s);
186 linkmode_or(all_adv, all_adv, t.advertising);
190 linkmode_copy(supported, all_s);
191 linkmode_copy(state->advertising, all_adv);
193 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
196 static int phylink_validate(struct phylink *pl, unsigned long *supported,
197 struct phylink_link_state *state)
199 if (!phy_interface_empty(pl->config->supported_interfaces)) {
200 if (state->interface == PHY_INTERFACE_MODE_NA)
201 return phylink_validate_any(pl, supported, state);
203 if (!test_bit(state->interface,
204 pl->config->supported_interfaces))
208 pl->mac_ops->validate(pl->config, supported, state);
210 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
213 static int phylink_parse_fixedlink(struct phylink *pl,
214 struct fwnode_handle *fwnode)
216 struct fwnode_handle *fixed_node;
217 const struct phy_setting *s;
218 struct gpio_desc *desc;
222 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
224 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
226 pl->link_config.speed = speed;
227 pl->link_config.duplex = DUPLEX_HALF;
229 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
230 pl->link_config.duplex = DUPLEX_FULL;
232 /* We treat the "pause" and "asym-pause" terminology as
233 * defining the link partner's ability.
235 if (fwnode_property_read_bool(fixed_node, "pause"))
236 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
237 pl->link_config.lp_advertising);
238 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
239 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
240 pl->link_config.lp_advertising);
243 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
247 pl->link_gpio = desc;
248 else if (desc == ERR_PTR(-EPROBE_DEFER))
251 fwnode_handle_put(fixed_node);
258 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
260 if (ret != ARRAY_SIZE(prop)) {
261 phylink_err(pl, "broken fixed-link?\n");
265 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
266 prop, ARRAY_SIZE(prop));
268 pl->link_config.duplex = prop[1] ?
269 DUPLEX_FULL : DUPLEX_HALF;
270 pl->link_config.speed = prop[2];
272 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
273 pl->link_config.lp_advertising);
275 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
276 pl->link_config.lp_advertising);
280 if (pl->link_config.speed > SPEED_1000 &&
281 pl->link_config.duplex != DUPLEX_FULL)
282 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
283 pl->link_config.speed);
285 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
286 linkmode_copy(pl->link_config.advertising, pl->supported);
287 phylink_validate(pl, pl->supported, &pl->link_config);
289 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
290 pl->supported, true);
291 linkmode_zero(pl->supported);
292 phylink_set(pl->supported, MII);
293 phylink_set(pl->supported, Pause);
294 phylink_set(pl->supported, Asym_Pause);
295 phylink_set(pl->supported, Autoneg);
297 __set_bit(s->bit, pl->supported);
298 __set_bit(s->bit, pl->link_config.lp_advertising);
300 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
301 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
302 pl->link_config.speed);
305 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
308 pl->link_config.link = 1;
309 pl->link_config.an_complete = 1;
314 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
316 struct fwnode_handle *dn;
319 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
320 if (dn || fwnode_property_present(fwnode, "fixed-link"))
321 pl->cfg_link_an_mode = MLO_AN_FIXED;
322 fwnode_handle_put(dn);
324 if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
325 strcmp(managed, "in-band-status") == 0) ||
326 pl->config->ovr_an_inband) {
327 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
329 "can't use both fixed-link and in-band-status\n");
333 linkmode_zero(pl->supported);
334 phylink_set(pl->supported, MII);
335 phylink_set(pl->supported, Autoneg);
336 phylink_set(pl->supported, Asym_Pause);
337 phylink_set(pl->supported, Pause);
338 pl->link_config.an_enabled = true;
339 pl->cfg_link_an_mode = MLO_AN_INBAND;
341 switch (pl->link_config.interface) {
342 case PHY_INTERFACE_MODE_SGMII:
343 case PHY_INTERFACE_MODE_QSGMII:
344 phylink_set(pl->supported, 10baseT_Half);
345 phylink_set(pl->supported, 10baseT_Full);
346 phylink_set(pl->supported, 100baseT_Half);
347 phylink_set(pl->supported, 100baseT_Full);
348 phylink_set(pl->supported, 1000baseT_Half);
349 phylink_set(pl->supported, 1000baseT_Full);
352 case PHY_INTERFACE_MODE_1000BASEX:
353 phylink_set(pl->supported, 1000baseX_Full);
356 case PHY_INTERFACE_MODE_2500BASEX:
357 phylink_set(pl->supported, 2500baseX_Full);
360 case PHY_INTERFACE_MODE_5GBASER:
361 phylink_set(pl->supported, 5000baseT_Full);
364 case PHY_INTERFACE_MODE_25GBASER:
365 phylink_set(pl->supported, 25000baseCR_Full);
366 phylink_set(pl->supported, 25000baseKR_Full);
367 phylink_set(pl->supported, 25000baseSR_Full);
369 case PHY_INTERFACE_MODE_USXGMII:
370 case PHY_INTERFACE_MODE_10GKR:
371 case PHY_INTERFACE_MODE_10GBASER:
372 phylink_set(pl->supported, 10baseT_Half);
373 phylink_set(pl->supported, 10baseT_Full);
374 phylink_set(pl->supported, 100baseT_Half);
375 phylink_set(pl->supported, 100baseT_Full);
376 phylink_set(pl->supported, 1000baseT_Half);
377 phylink_set(pl->supported, 1000baseT_Full);
378 phylink_set(pl->supported, 1000baseX_Full);
379 phylink_set(pl->supported, 1000baseKX_Full);
380 phylink_set(pl->supported, 2500baseT_Full);
381 phylink_set(pl->supported, 2500baseX_Full);
382 phylink_set(pl->supported, 5000baseT_Full);
383 phylink_set(pl->supported, 10000baseT_Full);
384 phylink_set(pl->supported, 10000baseKR_Full);
385 phylink_set(pl->supported, 10000baseKX4_Full);
386 phylink_set(pl->supported, 10000baseCR_Full);
387 phylink_set(pl->supported, 10000baseSR_Full);
388 phylink_set(pl->supported, 10000baseLR_Full);
389 phylink_set(pl->supported, 10000baseLRM_Full);
390 phylink_set(pl->supported, 10000baseER_Full);
393 case PHY_INTERFACE_MODE_XLGMII:
394 phylink_set(pl->supported, 25000baseCR_Full);
395 phylink_set(pl->supported, 25000baseKR_Full);
396 phylink_set(pl->supported, 25000baseSR_Full);
397 phylink_set(pl->supported, 40000baseKR4_Full);
398 phylink_set(pl->supported, 40000baseCR4_Full);
399 phylink_set(pl->supported, 40000baseSR4_Full);
400 phylink_set(pl->supported, 40000baseLR4_Full);
401 phylink_set(pl->supported, 50000baseCR2_Full);
402 phylink_set(pl->supported, 50000baseKR2_Full);
403 phylink_set(pl->supported, 50000baseSR2_Full);
404 phylink_set(pl->supported, 50000baseKR_Full);
405 phylink_set(pl->supported, 50000baseSR_Full);
406 phylink_set(pl->supported, 50000baseCR_Full);
407 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
408 phylink_set(pl->supported, 50000baseDR_Full);
409 phylink_set(pl->supported, 100000baseKR4_Full);
410 phylink_set(pl->supported, 100000baseSR4_Full);
411 phylink_set(pl->supported, 100000baseCR4_Full);
412 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
413 phylink_set(pl->supported, 100000baseKR2_Full);
414 phylink_set(pl->supported, 100000baseSR2_Full);
415 phylink_set(pl->supported, 100000baseCR2_Full);
416 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
417 phylink_set(pl->supported, 100000baseDR2_Full);
422 "incorrect link mode %s for in-band status\n",
423 phy_modes(pl->link_config.interface));
427 linkmode_copy(pl->link_config.advertising, pl->supported);
429 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
431 "failed to validate link configuration for in-band status\n");
435 /* Check if MAC/PCS also supports Autoneg. */
436 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
442 static void phylink_apply_manual_flow(struct phylink *pl,
443 struct phylink_link_state *state)
445 /* If autoneg is disabled, pause AN is also disabled */
446 if (!state->an_enabled)
447 state->pause &= ~MLO_PAUSE_AN;
449 /* Manual configuration of pause modes */
450 if (!(pl->link_config.pause & MLO_PAUSE_AN))
451 state->pause = pl->link_config.pause;
454 static void phylink_resolve_flow(struct phylink_link_state *state)
456 bool tx_pause, rx_pause;
458 state->pause = MLO_PAUSE_NONE;
459 if (state->duplex == DUPLEX_FULL) {
460 linkmode_resolve_pause(state->advertising,
461 state->lp_advertising,
462 &tx_pause, &rx_pause);
464 state->pause |= MLO_PAUSE_TX;
466 state->pause |= MLO_PAUSE_RX;
470 static void phylink_mac_config(struct phylink *pl,
471 const struct phylink_link_state *state)
474 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
475 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
476 phy_modes(state->interface),
477 phy_speed_to_str(state->speed),
478 phy_duplex_to_str(state->duplex),
479 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
480 state->pause, state->link, state->an_enabled);
482 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
485 static void phylink_mac_pcs_an_restart(struct phylink *pl)
487 if (pl->link_config.an_enabled &&
488 phy_interface_mode_is_8023z(pl->link_config.interface) &&
489 phylink_autoneg_inband(pl->cur_link_an_mode)) {
491 pl->pcs_ops->pcs_an_restart(pl->pcs);
493 pl->mac_ops->mac_an_restart(pl->config);
497 static void phylink_major_config(struct phylink *pl, bool restart,
498 const struct phylink_link_state *state)
502 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
504 if (pl->mac_ops->mac_prepare) {
505 err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
508 phylink_err(pl, "mac_prepare failed: %pe\n",
514 phylink_mac_config(pl, state);
517 err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
520 !!(pl->link_config.pause &
523 phylink_err(pl, "pcs_config failed: %pe\n",
529 phylink_mac_pcs_an_restart(pl);
531 if (pl->mac_ops->mac_finish) {
532 err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
535 phylink_err(pl, "mac_finish failed: %pe\n",
541 * Reconfigure for a change of inband advertisement.
542 * If we have a separate PCS, we only need to call its pcs_config() method,
543 * and then restart AN if it indicates something changed. Otherwise, we do
544 * the full MAC reconfiguration.
546 static int phylink_change_inband_advert(struct phylink *pl)
550 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
555 phylink_mac_config(pl, &pl->link_config);
556 phylink_mac_pcs_an_restart(pl);
560 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
561 phylink_an_mode_str(pl->cur_link_an_mode),
562 phy_modes(pl->link_config.interface),
563 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
564 pl->link_config.pause);
566 /* Modern PCS-based method; update the advert at the PCS, and
567 * restart negotiation if the pcs_config() helper indicates that
568 * the programmed advertisement has changed.
570 ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
571 pl->link_config.interface,
572 pl->link_config.advertising,
573 !!(pl->link_config.pause & MLO_PAUSE_AN));
578 phylink_mac_pcs_an_restart(pl);
583 static void phylink_mac_pcs_get_state(struct phylink *pl,
584 struct phylink_link_state *state)
586 linkmode_copy(state->advertising, pl->link_config.advertising);
587 linkmode_zero(state->lp_advertising);
588 state->interface = pl->link_config.interface;
589 state->an_enabled = pl->link_config.an_enabled;
590 if (state->an_enabled) {
591 state->speed = SPEED_UNKNOWN;
592 state->duplex = DUPLEX_UNKNOWN;
593 state->pause = MLO_PAUSE_NONE;
595 state->speed = pl->link_config.speed;
596 state->duplex = pl->link_config.duplex;
597 state->pause = pl->link_config.pause;
599 state->an_complete = 0;
603 pl->pcs_ops->pcs_get_state(pl->pcs, state);
604 else if (pl->mac_ops->mac_pcs_get_state)
605 pl->mac_ops->mac_pcs_get_state(pl->config, state);
610 /* The fixed state is... fixed except for the link state,
611 * which may be determined by a GPIO or a callback.
613 static void phylink_get_fixed_state(struct phylink *pl,
614 struct phylink_link_state *state)
616 *state = pl->link_config;
617 if (pl->config->get_fixed_state)
618 pl->config->get_fixed_state(pl->config, state);
619 else if (pl->link_gpio)
620 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
622 phylink_resolve_flow(state);
625 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
627 struct phylink_link_state link_state;
629 switch (pl->cur_link_an_mode) {
631 link_state = pl->phy_state;
635 phylink_get_fixed_state(pl, &link_state);
639 link_state = pl->link_config;
640 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
641 link_state.pause = MLO_PAUSE_NONE;
644 default: /* can't happen */
648 link_state.link = false;
650 phylink_apply_manual_flow(pl, &link_state);
651 phylink_major_config(pl, force_restart, &link_state);
654 static const char *phylink_pause_to_str(int pause)
656 switch (pause & MLO_PAUSE_TXRX_MASK) {
657 case MLO_PAUSE_TX | MLO_PAUSE_RX:
668 static void phylink_link_up(struct phylink *pl,
669 struct phylink_link_state link_state)
671 struct net_device *ndev = pl->netdev;
673 pl->cur_interface = link_state.interface;
675 if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
676 pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
678 link_state.speed, link_state.duplex);
680 pl->mac_ops->mac_link_up(pl->config, pl->phydev,
681 pl->cur_link_an_mode, pl->cur_interface,
682 link_state.speed, link_state.duplex,
683 !!(link_state.pause & MLO_PAUSE_TX),
684 !!(link_state.pause & MLO_PAUSE_RX));
687 netif_carrier_on(ndev);
690 "Link is Up - %s/%s - flow control %s\n",
691 phy_speed_to_str(link_state.speed),
692 phy_duplex_to_str(link_state.duplex),
693 phylink_pause_to_str(link_state.pause));
696 static void phylink_link_down(struct phylink *pl)
698 struct net_device *ndev = pl->netdev;
701 netif_carrier_off(ndev);
702 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
704 phylink_info(pl, "Link is Down\n");
707 static void phylink_resolve(struct work_struct *w)
709 struct phylink *pl = container_of(w, struct phylink, resolve);
710 struct phylink_link_state link_state;
711 struct net_device *ndev = pl->netdev;
712 bool mac_config = false;
715 mutex_lock(&pl->state_mutex);
717 cur_link_state = netif_carrier_ok(ndev);
719 cur_link_state = pl->old_link_state;
721 if (pl->phylink_disable_state) {
722 pl->mac_link_dropped = false;
723 link_state.link = false;
724 } else if (pl->mac_link_dropped) {
725 link_state.link = false;
727 switch (pl->cur_link_an_mode) {
729 link_state = pl->phy_state;
730 phylink_apply_manual_flow(pl, &link_state);
731 mac_config = link_state.link;
735 phylink_get_fixed_state(pl, &link_state);
736 mac_config = link_state.link;
740 phylink_mac_pcs_get_state(pl, &link_state);
742 /* If we have a phy, the "up" state is the union of
743 * both the PHY and the MAC
746 link_state.link &= pl->phy_state.link;
748 /* Only update if the PHY link is up */
749 if (pl->phydev && pl->phy_state.link) {
750 link_state.interface = pl->phy_state.interface;
752 /* If we have a PHY, we need to update with
753 * the PHY flow control bits.
755 link_state.pause = pl->phy_state.pause;
758 phylink_apply_manual_flow(pl, &link_state);
764 if (link_state.interface != pl->link_config.interface) {
765 /* The interface has changed, force the link down and
768 if (cur_link_state) {
769 phylink_link_down(pl);
770 cur_link_state = false;
772 phylink_major_config(pl, false, &link_state);
773 pl->link_config.interface = link_state.interface;
774 } else if (!pl->pcs_ops) {
775 /* The interface remains unchanged, only the speed,
776 * duplex or pause settings have changed. Call the
777 * old mac_config() method to configure the MAC/PCS
778 * only if we do not have a PCS installed (an
781 phylink_mac_config(pl, &link_state);
785 if (link_state.link != cur_link_state) {
786 pl->old_link_state = link_state.link;
787 if (!link_state.link)
788 phylink_link_down(pl);
790 phylink_link_up(pl, link_state);
792 if (!link_state.link && pl->mac_link_dropped) {
793 pl->mac_link_dropped = false;
794 queue_work(system_power_efficient_wq, &pl->resolve);
796 mutex_unlock(&pl->state_mutex);
799 static void phylink_run_resolve(struct phylink *pl)
801 if (!pl->phylink_disable_state)
802 queue_work(system_power_efficient_wq, &pl->resolve);
805 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
807 unsigned long state = pl->phylink_disable_state;
809 set_bit(bit, &pl->phylink_disable_state);
811 queue_work(system_power_efficient_wq, &pl->resolve);
812 flush_work(&pl->resolve);
816 static void phylink_fixed_poll(struct timer_list *t)
818 struct phylink *pl = container_of(t, struct phylink, link_poll);
820 mod_timer(t, jiffies + HZ);
822 phylink_run_resolve(pl);
825 static const struct sfp_upstream_ops sfp_phylink_ops;
827 static int phylink_register_sfp(struct phylink *pl,
828 struct fwnode_handle *fwnode)
836 bus = sfp_bus_find_fwnode(fwnode);
839 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
845 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
852 * phylink_create() - create a phylink instance
853 * @config: a pointer to the target &struct phylink_config
854 * @fwnode: a pointer to a &struct fwnode_handle describing the network
856 * @iface: the desired link mode defined by &typedef phy_interface_t
857 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
859 * Create a new phylink instance, and parse the link parameters found in @np.
860 * This will parse in-band modes, fixed-link or SFP configuration.
862 * Note: the rtnl lock must not be held when calling this function.
864 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
865 * must use IS_ERR() to check for errors from this function.
867 struct phylink *phylink_create(struct phylink_config *config,
868 struct fwnode_handle *fwnode,
869 phy_interface_t iface,
870 const struct phylink_mac_ops *mac_ops)
875 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
877 return ERR_PTR(-ENOMEM);
879 mutex_init(&pl->state_mutex);
880 INIT_WORK(&pl->resolve, phylink_resolve);
883 if (config->type == PHYLINK_NETDEV) {
884 pl->netdev = to_net_dev(config->dev);
885 } else if (config->type == PHYLINK_DEV) {
886 pl->dev = config->dev;
889 return ERR_PTR(-EINVAL);
892 pl->phy_state.interface = iface;
893 pl->link_interface = iface;
894 if (iface == PHY_INTERFACE_MODE_MOCA)
895 pl->link_port = PORT_BNC;
897 pl->link_port = PORT_MII;
898 pl->link_config.interface = iface;
899 pl->link_config.pause = MLO_PAUSE_AN;
900 pl->link_config.speed = SPEED_UNKNOWN;
901 pl->link_config.duplex = DUPLEX_UNKNOWN;
902 pl->link_config.an_enabled = true;
903 pl->mac_ops = mac_ops;
904 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
905 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
907 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
908 linkmode_copy(pl->link_config.advertising, pl->supported);
909 phylink_validate(pl, pl->supported, &pl->link_config);
911 ret = phylink_parse_mode(pl, fwnode);
917 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
918 ret = phylink_parse_fixedlink(pl, fwnode);
925 pl->cur_link_an_mode = pl->cfg_link_an_mode;
927 ret = phylink_register_sfp(pl, fwnode);
935 EXPORT_SYMBOL_GPL(phylink_create);
938 * phylink_set_pcs() - set the current PCS for phylink to use
939 * @pl: a pointer to a &struct phylink returned from phylink_create()
940 * @pcs: a pointer to the &struct phylink_pcs
942 * Bind the MAC PCS to phylink. This may be called after phylink_create(),
943 * in mac_prepare() or mac_config() methods if it is desired to dynamically
946 * Please note that there are behavioural changes with the mac_config()
947 * callback if a PCS is present (denoting a newer setup) so removing a PCS
948 * is not supported, and if a PCS is going to be used, it must be registered
949 * by calling phylink_set_pcs() at the latest in the first mac_config() call.
951 void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs)
954 pl->pcs_ops = pcs->ops;
956 EXPORT_SYMBOL_GPL(phylink_set_pcs);
959 * phylink_destroy() - cleanup and destroy the phylink instance
960 * @pl: a pointer to a &struct phylink returned from phylink_create()
962 * Destroy a phylink instance. Any PHY that has been attached must have been
963 * cleaned up via phylink_disconnect_phy() prior to calling this function.
965 * Note: the rtnl lock must not be held when calling this function.
967 void phylink_destroy(struct phylink *pl)
969 sfp_bus_del_upstream(pl->sfp_bus);
971 gpiod_put(pl->link_gpio);
973 cancel_work_sync(&pl->resolve);
976 EXPORT_SYMBOL_GPL(phylink_destroy);
978 static void phylink_phy_change(struct phy_device *phydev, bool up)
980 struct phylink *pl = phydev->phylink;
981 bool tx_pause, rx_pause;
983 phy_get_pause(phydev, &tx_pause, &rx_pause);
985 mutex_lock(&pl->state_mutex);
986 pl->phy_state.speed = phydev->speed;
987 pl->phy_state.duplex = phydev->duplex;
988 pl->phy_state.pause = MLO_PAUSE_NONE;
990 pl->phy_state.pause |= MLO_PAUSE_TX;
992 pl->phy_state.pause |= MLO_PAUSE_RX;
993 pl->phy_state.interface = phydev->interface;
994 pl->phy_state.link = up;
995 mutex_unlock(&pl->state_mutex);
997 phylink_run_resolve(pl);
999 phylink_dbg(pl, "phy link %s %s/%s/%s/%s\n", up ? "up" : "down",
1000 phy_modes(phydev->interface),
1001 phy_speed_to_str(phydev->speed),
1002 phy_duplex_to_str(phydev->duplex),
1003 phylink_pause_to_str(pl->phy_state.pause));
1006 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
1007 phy_interface_t interface)
1009 struct phylink_link_state config;
1010 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
1015 * This is the new way of dealing with flow control for PHYs,
1016 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
1017 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
1018 * using our validate call to the MAC, we rely upon the MAC
1019 * clearing the bits from both supported and advertising fields.
1021 phy_support_asym_pause(phy);
1023 memset(&config, 0, sizeof(config));
1024 linkmode_copy(supported, phy->supported);
1025 linkmode_copy(config.advertising, phy->advertising);
1027 /* Clause 45 PHYs switch their Serdes lane between several different
1028 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
1029 * speeds. We really need to know which interface modes the PHY and
1030 * MAC supports to properly work out which linkmodes can be supported.
1033 interface != PHY_INTERFACE_MODE_RXAUI &&
1034 interface != PHY_INTERFACE_MODE_XAUI &&
1035 interface != PHY_INTERFACE_MODE_USXGMII)
1036 config.interface = PHY_INTERFACE_MODE_NA;
1038 config.interface = interface;
1040 ret = phylink_validate(pl, supported, &config);
1042 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
1043 phy_modes(config.interface),
1044 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
1045 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
1051 phy->phy_link_change = phylink_phy_change;
1053 irq_str = phy_attached_info_irq(phy);
1055 "PHY [%s] driver [%s] (irq=%s)\n",
1056 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1059 mutex_lock(&phy->lock);
1060 mutex_lock(&pl->state_mutex);
1062 pl->phy_state.interface = interface;
1063 pl->phy_state.pause = MLO_PAUSE_NONE;
1064 pl->phy_state.speed = SPEED_UNKNOWN;
1065 pl->phy_state.duplex = DUPLEX_UNKNOWN;
1066 linkmode_copy(pl->supported, supported);
1067 linkmode_copy(pl->link_config.advertising, config.advertising);
1069 /* Restrict the phy advertisement according to the MAC support. */
1070 linkmode_copy(phy->advertising, config.advertising);
1071 mutex_unlock(&pl->state_mutex);
1072 mutex_unlock(&phy->lock);
1075 "phy: setting supported %*pb advertising %*pb\n",
1076 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1077 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1079 if (phy_interrupt_is_valid(phy))
1080 phy_request_interrupt(phy);
1085 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1086 phy_interface_t interface)
1088 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1089 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1090 phy_interface_mode_is_8023z(interface))))
1096 return phy_attach_direct(pl->netdev, phy, 0, interface);
1100 * phylink_connect_phy() - connect a PHY to the phylink instance
1101 * @pl: a pointer to a &struct phylink returned from phylink_create()
1102 * @phy: a pointer to a &struct phy_device.
1104 * Connect @phy to the phylink instance specified by @pl by calling
1105 * phy_attach_direct(). Configure the @phy according to the MAC driver's
1106 * capabilities, start the PHYLIB state machine and enable any interrupts
1107 * that the PHY supports.
1109 * This updates the phylink's ethtool supported and advertising link mode
1112 * Returns 0 on success or a negative errno.
1114 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1118 /* Use PHY device/driver interface */
1119 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1120 pl->link_interface = phy->interface;
1121 pl->link_config.interface = pl->link_interface;
1124 ret = phylink_attach_phy(pl, phy, pl->link_interface);
1128 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1134 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1137 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1138 * @pl: a pointer to a &struct phylink returned from phylink_create()
1139 * @dn: a pointer to a &struct device_node.
1140 * @flags: PHY-specific flags to communicate to the PHY device driver
1142 * Connect the phy specified in the device node @dn to the phylink instance
1143 * specified by @pl. Actions specified in phylink_connect_phy() will be
1146 * Returns 0 on success or a negative errno.
1148 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1151 return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
1153 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1156 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
1157 * @pl: a pointer to a &struct phylink returned from phylink_create()
1158 * @fwnode: a pointer to a &struct fwnode_handle.
1159 * @flags: PHY-specific flags to communicate to the PHY device driver
1161 * Connect the phy specified @fwnode to the phylink instance specified
1164 * Returns 0 on success or a negative errno.
1166 int phylink_fwnode_phy_connect(struct phylink *pl,
1167 struct fwnode_handle *fwnode,
1170 struct fwnode_handle *phy_fwnode;
1171 struct phy_device *phy_dev;
1174 /* Fixed links and 802.3z are handled without needing a PHY */
1175 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1176 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1177 phy_interface_mode_is_8023z(pl->link_interface)))
1180 phy_fwnode = fwnode_get_phy_node(fwnode);
1181 if (IS_ERR(phy_fwnode)) {
1182 if (pl->cfg_link_an_mode == MLO_AN_PHY)
1187 phy_dev = fwnode_phy_find_device(phy_fwnode);
1188 /* We're done with the phy_node handle */
1189 fwnode_handle_put(phy_fwnode);
1193 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1194 pl->link_interface);
1196 phy_device_free(phy_dev);
1200 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1202 phy_detach(phy_dev);
1206 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
1209 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1211 * @pl: a pointer to a &struct phylink returned from phylink_create()
1213 * Disconnect any current PHY from the phylink instance described by @pl.
1215 void phylink_disconnect_phy(struct phylink *pl)
1217 struct phy_device *phy;
1223 mutex_lock(&phy->lock);
1224 mutex_lock(&pl->state_mutex);
1226 mutex_unlock(&pl->state_mutex);
1227 mutex_unlock(&phy->lock);
1228 flush_work(&pl->resolve);
1230 phy_disconnect(phy);
1233 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1236 * phylink_mac_change() - notify phylink of a change in MAC state
1237 * @pl: a pointer to a &struct phylink returned from phylink_create()
1238 * @up: indicates whether the link is currently up.
1240 * The MAC driver should call this driver when the state of its link
1241 * changes (eg, link failure, new negotiation results, etc.)
1243 void phylink_mac_change(struct phylink *pl, bool up)
1246 pl->mac_link_dropped = true;
1247 phylink_run_resolve(pl);
1248 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1250 EXPORT_SYMBOL_GPL(phylink_mac_change);
1252 static irqreturn_t phylink_link_handler(int irq, void *data)
1254 struct phylink *pl = data;
1256 phylink_run_resolve(pl);
1262 * phylink_start() - start a phylink instance
1263 * @pl: a pointer to a &struct phylink returned from phylink_create()
1265 * Start the phylink instance specified by @pl, configuring the MAC for the
1266 * desired link mode(s) and negotiation style. This should be called from the
1267 * network device driver's &struct net_device_ops ndo_open() method.
1269 void phylink_start(struct phylink *pl)
1275 phylink_info(pl, "configuring for %s/%s link mode\n",
1276 phylink_an_mode_str(pl->cur_link_an_mode),
1277 phy_modes(pl->link_config.interface));
1279 /* Always set the carrier off */
1281 netif_carrier_off(pl->netdev);
1283 /* Apply the link configuration to the MAC when starting. This allows
1284 * a fixed-link to start with the correct parameters, and also
1285 * ensures that we set the appropriate advertisement for Serdes links.
1287 * Restart autonegotiation if using 802.3z to ensure that the link
1288 * parameters are properly negotiated. This is necessary for DSA
1289 * switches using 802.3z negotiation to ensure they see our modes.
1291 phylink_mac_initial_config(pl, true);
1293 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1294 phylink_run_resolve(pl);
1296 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1297 int irq = gpiod_to_irq(pl->link_gpio);
1300 if (!request_irq(irq, phylink_link_handler,
1301 IRQF_TRIGGER_RISING |
1302 IRQF_TRIGGER_FALLING,
1312 switch (pl->cfg_link_an_mode) {
1314 poll |= pl->config->poll_fixed_state;
1317 poll |= pl->config->pcs_poll;
1319 poll |= pl->pcs->poll;
1323 mod_timer(&pl->link_poll, jiffies + HZ);
1325 phy_start(pl->phydev);
1327 sfp_upstream_start(pl->sfp_bus);
1329 EXPORT_SYMBOL_GPL(phylink_start);
1332 * phylink_stop() - stop a phylink instance
1333 * @pl: a pointer to a &struct phylink returned from phylink_create()
1335 * Stop the phylink instance specified by @pl. This should be called from the
1336 * network device driver's &struct net_device_ops ndo_stop() method. The
1337 * network device's carrier state should not be changed prior to calling this
1340 * This will synchronously bring down the link if the link is not already
1341 * down (in other words, it will trigger a mac_link_down() method call.)
1343 void phylink_stop(struct phylink *pl)
1348 sfp_upstream_stop(pl->sfp_bus);
1350 phy_stop(pl->phydev);
1351 del_timer_sync(&pl->link_poll);
1353 free_irq(pl->link_irq, pl);
1357 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1359 EXPORT_SYMBOL_GPL(phylink_stop);
1362 * phylink_suspend() - handle a network device suspend event
1363 * @pl: a pointer to a &struct phylink returned from phylink_create()
1364 * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
1366 * Handle a network device suspend event. There are several cases:
1367 * - If Wake-on-Lan is not active, we can bring down the link between
1368 * the MAC and PHY by calling phylink_stop().
1369 * - If Wake-on-Lan is active, and being handled only by the PHY, we
1370 * can also bring down the link between the MAC and PHY.
1371 * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
1372 * still needs to receive packets, so we can not bring the link down.
1374 void phylink_suspend(struct phylink *pl, bool mac_wol)
1378 if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) {
1379 /* Wake-on-Lan enabled, MAC handling */
1380 mutex_lock(&pl->state_mutex);
1382 /* Stop the resolver bringing the link up */
1383 __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1385 /* Disable the carrier, to prevent transmit timeouts,
1386 * but one would hope all packets have been sent. This
1387 * also means phylink_resolve() will do nothing.
1390 netif_carrier_off(pl->netdev);
1392 pl->old_link_state = false;
1394 /* We do not call mac_link_down() here as we want the
1395 * link to remain up to receive the WoL packets.
1397 mutex_unlock(&pl->state_mutex);
1402 EXPORT_SYMBOL_GPL(phylink_suspend);
1405 * phylink_resume() - handle a network device resume event
1406 * @pl: a pointer to a &struct phylink returned from phylink_create()
1408 * Undo the effects of phylink_suspend(), returning the link to an
1409 * operational state.
1411 void phylink_resume(struct phylink *pl)
1415 if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
1416 /* Wake-on-Lan enabled, MAC handling */
1418 /* Call mac_link_down() so we keep the overall state balanced.
1419 * Do this under the state_mutex lock for consistency. This
1420 * will cause a "Link Down" message to be printed during
1421 * resume, which is harmless - the true link state will be
1422 * printed when we run a resolve.
1424 mutex_lock(&pl->state_mutex);
1425 phylink_link_down(pl);
1426 mutex_unlock(&pl->state_mutex);
1428 /* Re-apply the link parameters so that all the settings get
1429 * restored to the MAC.
1431 phylink_mac_initial_config(pl, true);
1433 /* Re-enable and re-resolve the link parameters */
1434 clear_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1435 phylink_run_resolve(pl);
1440 EXPORT_SYMBOL_GPL(phylink_resume);
1443 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1444 * @pl: a pointer to a &struct phylink returned from phylink_create()
1445 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1447 * Read the wake on lan parameters from the PHY attached to the phylink
1448 * instance specified by @pl. If no PHY is currently attached, report no
1449 * support for wake on lan.
1451 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1459 phy_ethtool_get_wol(pl->phydev, wol);
1461 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1464 * phylink_ethtool_set_wol() - set wake on lan parameters
1465 * @pl: a pointer to a &struct phylink returned from phylink_create()
1466 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1468 * Set the wake on lan parameters for the PHY attached to the phylink
1469 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1472 * Returns zero on success or negative errno code.
1474 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1476 int ret = -EOPNOTSUPP;
1481 ret = phy_ethtool_set_wol(pl->phydev, wol);
1485 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1487 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1489 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1491 linkmode_zero(mask);
1492 phylink_set_port_modes(mask);
1494 linkmode_and(dst, dst, mask);
1495 linkmode_or(dst, dst, b);
1498 static void phylink_get_ksettings(const struct phylink_link_state *state,
1499 struct ethtool_link_ksettings *kset)
1501 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1502 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1503 kset->base.speed = state->speed;
1504 kset->base.duplex = state->duplex;
1505 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1510 * phylink_ethtool_ksettings_get() - get the current link settings
1511 * @pl: a pointer to a &struct phylink returned from phylink_create()
1512 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1514 * Read the current link settings for the phylink instance specified by @pl.
1515 * This will be the link settings read from the MAC, PHY or fixed link
1516 * settings depending on the current negotiation mode.
1518 int phylink_ethtool_ksettings_get(struct phylink *pl,
1519 struct ethtool_link_ksettings *kset)
1521 struct phylink_link_state link_state;
1526 phy_ethtool_ksettings_get(pl->phydev, kset);
1528 kset->base.port = pl->link_port;
1530 linkmode_copy(kset->link_modes.supported, pl->supported);
1532 switch (pl->cur_link_an_mode) {
1534 /* We are using fixed settings. Report these as the
1535 * current link settings - and note that these also
1536 * represent the supported speeds/duplex/pause modes.
1538 phylink_get_fixed_state(pl, &link_state);
1539 phylink_get_ksettings(&link_state, kset);
1543 /* If there is a phy attached, then use the reported
1544 * settings from the phy with no modification.
1549 phylink_mac_pcs_get_state(pl, &link_state);
1551 /* The MAC is reporting the link results from its own PCS
1552 * layer via in-band status. Report these as the current
1555 phylink_get_ksettings(&link_state, kset);
1561 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1564 * phylink_ethtool_ksettings_set() - set the link settings
1565 * @pl: a pointer to a &struct phylink returned from phylink_create()
1566 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1568 int phylink_ethtool_ksettings_set(struct phylink *pl,
1569 const struct ethtool_link_ksettings *kset)
1571 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1572 struct phylink_link_state config;
1573 const struct phy_setting *s;
1578 /* We can rely on phylib for this update; we also do not need
1579 * to update the pl->link_config settings:
1580 * - the configuration returned via ksettings_get() will come
1581 * from phylib whenever a PHY is present.
1582 * - link_config.interface will be updated by the PHY calling
1583 * back via phylink_phy_change() and a subsequent resolve.
1584 * - initial link configuration for PHY mode comes from the
1585 * last phy state updated via phylink_phy_change().
1586 * - other configuration changes (e.g. pause modes) are
1587 * performed directly via phylib.
1588 * - if in in-band mode with a PHY, the link configuration
1589 * is passed on the link from the PHY, and all of
1590 * link_config.{speed,duplex,an_enabled,pause} are not used.
1591 * - the only possible use would be link_config.advertising
1592 * pause modes when in 1000base-X mode with a PHY, but in
1593 * the presence of a PHY, this should not be changed as that
1594 * should be determined from the media side advertisement.
1596 return phy_ethtool_ksettings_set(pl->phydev, kset);
1599 config = pl->link_config;
1601 /* Mask out unsupported advertisements */
1602 linkmode_and(config.advertising, kset->link_modes.advertising,
1605 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1606 switch (kset->base.autoneg) {
1607 case AUTONEG_DISABLE:
1608 /* Autonegotiation disabled, select a suitable speed and
1611 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1612 pl->supported, false);
1616 /* If we have a fixed link, refuse to change link parameters.
1617 * If the link parameters match, accept them but do nothing.
1619 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1620 if (s->speed != pl->link_config.speed ||
1621 s->duplex != pl->link_config.duplex)
1626 config.speed = s->speed;
1627 config.duplex = s->duplex;
1630 case AUTONEG_ENABLE:
1631 /* If we have a fixed link, allow autonegotiation (since that
1632 * is our default case) but do not allow the advertisement to
1633 * be changed. If the advertisement matches, simply return.
1635 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1636 if (!linkmode_equal(config.advertising,
1637 pl->link_config.advertising))
1642 config.speed = SPEED_UNKNOWN;
1643 config.duplex = DUPLEX_UNKNOWN;
1650 /* We have ruled out the case with a PHY attached, and the
1651 * fixed-link cases. All that is left are in-band links.
1653 config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
1654 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
1657 /* If this link is with an SFP, ensure that changes to advertised modes
1658 * also cause the associated interface to be selected such that the
1659 * link can be configured correctly.
1662 config.interface = sfp_select_interface(pl->sfp_bus,
1663 config.advertising);
1664 if (config.interface == PHY_INTERFACE_MODE_NA) {
1666 "selection of interface failed, advertisement %*pb\n",
1667 __ETHTOOL_LINK_MODE_MASK_NBITS,
1668 config.advertising);
1672 /* Revalidate with the selected interface */
1673 linkmode_copy(support, pl->supported);
1674 if (phylink_validate(pl, support, &config)) {
1675 phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
1676 phylink_an_mode_str(pl->cur_link_an_mode),
1677 phy_modes(config.interface),
1678 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1682 /* Validate without changing the current supported mask. */
1683 linkmode_copy(support, pl->supported);
1684 if (phylink_validate(pl, support, &config))
1688 /* If autonegotiation is enabled, we must have an advertisement */
1689 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1692 mutex_lock(&pl->state_mutex);
1693 pl->link_config.speed = config.speed;
1694 pl->link_config.duplex = config.duplex;
1695 pl->link_config.an_enabled = config.an_enabled;
1697 if (pl->link_config.interface != config.interface) {
1698 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
1699 /* We need to force the link down, then change the interface */
1700 if (pl->old_link_state) {
1701 phylink_link_down(pl);
1702 pl->old_link_state = false;
1704 if (!test_bit(PHYLINK_DISABLE_STOPPED,
1705 &pl->phylink_disable_state))
1706 phylink_major_config(pl, false, &config);
1707 pl->link_config.interface = config.interface;
1708 linkmode_copy(pl->link_config.advertising, config.advertising);
1709 } else if (!linkmode_equal(pl->link_config.advertising,
1710 config.advertising)) {
1711 linkmode_copy(pl->link_config.advertising, config.advertising);
1712 phylink_change_inband_advert(pl);
1714 mutex_unlock(&pl->state_mutex);
1718 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1721 * phylink_ethtool_nway_reset() - restart negotiation
1722 * @pl: a pointer to a &struct phylink returned from phylink_create()
1724 * Restart negotiation for the phylink instance specified by @pl. This will
1725 * cause any attached phy to restart negotiation with the link partner, and
1726 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1729 * Returns zero on success, or negative error code.
1731 int phylink_ethtool_nway_reset(struct phylink *pl)
1738 ret = phy_restart_aneg(pl->phydev);
1739 phylink_mac_pcs_an_restart(pl);
1743 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1746 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1747 * @pl: a pointer to a &struct phylink returned from phylink_create()
1748 * @pause: a pointer to a &struct ethtool_pauseparam
1750 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1751 struct ethtool_pauseparam *pause)
1755 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1756 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1757 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1759 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1762 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1763 * @pl: a pointer to a &struct phylink returned from phylink_create()
1764 * @pause: a pointer to a &struct ethtool_pauseparam
1766 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1767 struct ethtool_pauseparam *pause)
1769 struct phylink_link_state *config = &pl->link_config;
1770 bool manual_changed;
1775 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1778 if (!phylink_test(pl->supported, Pause) &&
1779 !phylink_test(pl->supported, Asym_Pause))
1782 if (!phylink_test(pl->supported, Asym_Pause) &&
1783 pause->rx_pause != pause->tx_pause)
1788 pause_state |= MLO_PAUSE_AN;
1789 if (pause->rx_pause)
1790 pause_state |= MLO_PAUSE_RX;
1791 if (pause->tx_pause)
1792 pause_state |= MLO_PAUSE_TX;
1794 mutex_lock(&pl->state_mutex);
1796 * See the comments for linkmode_set_pause(), wrt the deficiencies
1797 * with the current implementation. A solution to this issue would
1799 * ethtool Local device
1800 * rx tx Pause AsymDir
1805 * and then use the ethtool rx/tx enablement status to mask the
1806 * rx/tx pause resolution.
1808 linkmode_set_pause(config->advertising, pause->tx_pause,
1811 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1812 (!(pause_state & MLO_PAUSE_AN) &&
1813 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1815 config->pause = pause_state;
1817 /* Update our in-band advertisement, triggering a renegotiation if
1818 * the advertisement changed.
1821 phylink_change_inband_advert(pl);
1823 mutex_unlock(&pl->state_mutex);
1825 /* If we have a PHY, a change of the pause frame advertisement will
1826 * cause phylib to renegotiate (if AN is enabled) which will in turn
1827 * call our phylink_phy_change() and trigger a resolve. Note that
1828 * we can't hold our state mutex while calling phy_set_asym_pause().
1831 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1834 /* If the manual pause settings changed, make sure we trigger a
1835 * resolve to update their state; we can not guarantee that the
1838 if (manual_changed) {
1839 pl->mac_link_dropped = true;
1840 phylink_run_resolve(pl);
1845 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1848 * phylink_get_eee_err() - read the energy efficient ethernet error
1850 * @pl: a pointer to a &struct phylink returned from phylink_create().
1852 * Read the Energy Efficient Ethernet error counter from the PHY associated
1853 * with the phylink instance specified by @pl.
1855 * Returns positive error counter value, or negative error code.
1857 int phylink_get_eee_err(struct phylink *pl)
1864 ret = phy_get_eee_err(pl->phydev);
1868 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1871 * phylink_init_eee() - init and check the EEE features
1872 * @pl: a pointer to a &struct phylink returned from phylink_create()
1873 * @clk_stop_enable: allow PHY to stop receive clock
1875 * Must be called either with RTNL held or within mac_link_up()
1877 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1879 int ret = -EOPNOTSUPP;
1882 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1886 EXPORT_SYMBOL_GPL(phylink_init_eee);
1889 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1890 * @pl: a pointer to a &struct phylink returned from phylink_create()
1891 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1893 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1895 int ret = -EOPNOTSUPP;
1900 ret = phy_ethtool_get_eee(pl->phydev, eee);
1904 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1907 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1908 * @pl: a pointer to a &struct phylink returned from phylink_create()
1909 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1911 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1913 int ret = -EOPNOTSUPP;
1918 ret = phy_ethtool_set_eee(pl->phydev, eee);
1922 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1924 /* This emulates MII registers for a fixed-mode phy operating as per the
1925 * passed in state. "aneg" defines if we report negotiation is possible.
1927 * FIXME: should deal with negotiation state too.
1929 static int phylink_mii_emul_read(unsigned int reg,
1930 struct phylink_link_state *state)
1932 struct fixed_phy_status fs;
1933 unsigned long *lpa = state->lp_advertising;
1936 fs.link = state->link;
1937 fs.speed = state->speed;
1938 fs.duplex = state->duplex;
1939 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1940 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
1942 val = swphy_read_reg(reg, &fs);
1943 if (reg == MII_BMSR) {
1944 if (!state->an_complete)
1945 val &= ~BMSR_ANEGCOMPLETE;
1950 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1953 struct phy_device *phydev = pl->phydev;
1956 if (mdio_phy_id_is_c45(phy_id)) {
1957 prtad = mdio_phy_id_prtad(phy_id);
1958 devad = mdio_phy_id_devad(phy_id);
1959 devad = mdiobus_c45_addr(devad, reg);
1960 } else if (phydev->is_c45) {
1966 devad = __ffs(phydev->c45_ids.mmds_present);
1970 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1972 devad = MDIO_MMD_AN;
1973 if (reg == MII_ADVERTISE)
1974 reg = MDIO_AN_ADVERTISE;
1982 devad = mdiobus_c45_addr(devad, reg);
1987 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1990 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1991 unsigned int reg, unsigned int val)
1993 struct phy_device *phydev = pl->phydev;
1996 if (mdio_phy_id_is_c45(phy_id)) {
1997 prtad = mdio_phy_id_prtad(phy_id);
1998 devad = mdio_phy_id_devad(phy_id);
1999 devad = mdiobus_c45_addr(devad, reg);
2000 } else if (phydev->is_c45) {
2006 devad = __ffs(phydev->c45_ids.mmds_present);
2010 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
2012 devad = MDIO_MMD_AN;
2013 if (reg == MII_ADVERTISE)
2014 reg = MDIO_AN_ADVERTISE;
2022 devad = mdiobus_c45_addr(devad, reg);
2028 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
2031 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
2034 struct phylink_link_state state;
2037 switch (pl->cur_link_an_mode) {
2040 phylink_get_fixed_state(pl, &state);
2041 val = phylink_mii_emul_read(reg, &state);
2050 phylink_mac_pcs_get_state(pl, &state);
2051 val = phylink_mii_emul_read(reg, &state);
2056 return val & 0xffff;
2059 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
2060 unsigned int reg, unsigned int val)
2062 switch (pl->cur_link_an_mode) {
2077 * phylink_mii_ioctl() - generic mii ioctl interface
2078 * @pl: a pointer to a &struct phylink returned from phylink_create()
2079 * @ifr: a pointer to a &struct ifreq for socket ioctls
2080 * @cmd: ioctl cmd to execute
2082 * Perform the specified MII ioctl on the PHY attached to the phylink instance
2083 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
2085 * Returns: zero on success or negative error code.
2088 * read register from the current PHY.
2090 * read register from the specified PHY.
2092 * set a register on the specified PHY.
2094 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
2096 struct mii_ioctl_data *mii = if_mii(ifr);
2102 /* PHYs only exist for MLO_AN_PHY and SGMII */
2105 mii->phy_id = pl->phydev->mdio.addr;
2109 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
2117 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
2122 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
2132 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
2140 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
2152 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2155 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2157 * @pl: a pointer to a &struct phylink returned from phylink_create()
2158 * @sync: perform action synchronously
2160 * If we have a PHY that is not part of a SFP module, then set the speed
2161 * as described in the phy_speed_down() function. Please see this function
2162 * for a description of the @sync parameter.
2164 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2166 int phylink_speed_down(struct phylink *pl, bool sync)
2172 if (!pl->sfp_bus && pl->phydev)
2173 ret = phy_speed_down(pl->phydev, sync);
2177 EXPORT_SYMBOL_GPL(phylink_speed_down);
2180 * phylink_speed_up() - restore the advertised speeds prior to the call to
2181 * phylink_speed_down()
2182 * @pl: a pointer to a &struct phylink returned from phylink_create()
2184 * If we have a PHY that is not part of a SFP module, then restore the
2185 * PHY speeds as per phy_speed_up().
2187 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2189 int phylink_speed_up(struct phylink *pl)
2195 if (!pl->sfp_bus && pl->phydev)
2196 ret = phy_speed_up(pl->phydev);
2200 EXPORT_SYMBOL_GPL(phylink_speed_up);
2202 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2204 struct phylink *pl = upstream;
2206 pl->netdev->sfp_bus = bus;
2209 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2211 struct phylink *pl = upstream;
2213 pl->netdev->sfp_bus = NULL;
2216 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2217 const unsigned long *supported,
2218 const unsigned long *advertising)
2220 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2221 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2222 struct phylink_link_state config;
2223 phy_interface_t iface;
2227 linkmode_copy(support, supported);
2229 memset(&config, 0, sizeof(config));
2230 linkmode_copy(config.advertising, advertising);
2231 config.interface = PHY_INTERFACE_MODE_NA;
2232 config.speed = SPEED_UNKNOWN;
2233 config.duplex = DUPLEX_UNKNOWN;
2234 config.pause = MLO_PAUSE_AN;
2235 config.an_enabled = pl->link_config.an_enabled;
2237 /* Ignore errors if we're expecting a PHY to attach later */
2238 ret = phylink_validate(pl, support, &config);
2240 phylink_err(pl, "validation with support %*pb failed: %d\n",
2241 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2245 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2246 if (iface == PHY_INTERFACE_MODE_NA) {
2248 "selection of interface failed, advertisement %*pb\n",
2249 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2253 config.interface = iface;
2254 linkmode_copy(support1, support);
2255 ret = phylink_validate(pl, support1, &config);
2257 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
2258 phylink_an_mode_str(mode),
2259 phy_modes(config.interface),
2260 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2264 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2265 phylink_an_mode_str(mode), phy_modes(config.interface),
2266 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2268 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2271 changed = !linkmode_equal(pl->supported, support) ||
2272 !linkmode_equal(pl->link_config.advertising,
2273 config.advertising);
2275 linkmode_copy(pl->supported, support);
2276 linkmode_copy(pl->link_config.advertising, config.advertising);
2279 if (pl->cur_link_an_mode != mode ||
2280 pl->link_config.interface != config.interface) {
2281 pl->link_config.interface = config.interface;
2282 pl->cur_link_an_mode = mode;
2286 phylink_info(pl, "switched to %s/%s link mode\n",
2287 phylink_an_mode_str(mode),
2288 phy_modes(config.interface));
2291 pl->link_port = pl->sfp_port;
2293 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2294 &pl->phylink_disable_state))
2295 phylink_mac_initial_config(pl, false);
2300 static int phylink_sfp_module_insert(void *upstream,
2301 const struct sfp_eeprom_id *id)
2303 struct phylink *pl = upstream;
2304 unsigned long *support = pl->sfp_support;
2308 linkmode_zero(support);
2309 sfp_parse_support(pl->sfp_bus, id, support);
2310 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2312 /* If this module may have a PHY connecting later, defer until later */
2313 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2314 if (pl->sfp_may_have_phy)
2317 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2320 static int phylink_sfp_module_start(void *upstream)
2322 struct phylink *pl = upstream;
2324 /* If this SFP module has a PHY, start the PHY now. */
2326 phy_start(pl->phydev);
2330 /* If the module may have a PHY but we didn't detect one we
2331 * need to configure the MAC here.
2333 if (!pl->sfp_may_have_phy)
2336 return phylink_sfp_config(pl, MLO_AN_INBAND,
2337 pl->sfp_support, pl->sfp_support);
2340 static void phylink_sfp_module_stop(void *upstream)
2342 struct phylink *pl = upstream;
2344 /* If this SFP module has a PHY, stop it. */
2346 phy_stop(pl->phydev);
2349 static void phylink_sfp_link_down(void *upstream)
2351 struct phylink *pl = upstream;
2355 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2358 static void phylink_sfp_link_up(void *upstream)
2360 struct phylink *pl = upstream;
2364 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2365 phylink_run_resolve(pl);
2368 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2369 * or 802.3z control word, so inband will not work.
2371 static bool phylink_phy_no_inband(struct phy_device *phy)
2373 return phy->is_c45 &&
2374 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2377 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2379 struct phylink *pl = upstream;
2380 phy_interface_t interface;
2385 * This is the new way of dealing with flow control for PHYs,
2386 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2387 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2388 * using our validate call to the MAC, we rely upon the MAC
2389 * clearing the bits from both supported and advertising fields.
2391 phy_support_asym_pause(phy);
2393 if (phylink_phy_no_inband(phy))
2396 mode = MLO_AN_INBAND;
2398 /* Do the initial configuration */
2399 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2403 interface = pl->link_config.interface;
2404 ret = phylink_attach_phy(pl, phy, interface);
2408 ret = phylink_bringup_phy(pl, phy, interface);
2415 static void phylink_sfp_disconnect_phy(void *upstream)
2417 phylink_disconnect_phy(upstream);
2420 static const struct sfp_upstream_ops sfp_phylink_ops = {
2421 .attach = phylink_sfp_attach,
2422 .detach = phylink_sfp_detach,
2423 .module_insert = phylink_sfp_module_insert,
2424 .module_start = phylink_sfp_module_start,
2425 .module_stop = phylink_sfp_module_stop,
2426 .link_up = phylink_sfp_link_up,
2427 .link_down = phylink_sfp_link_down,
2428 .connect_phy = phylink_sfp_connect_phy,
2429 .disconnect_phy = phylink_sfp_disconnect_phy,
2432 /* Helpers for MAC drivers */
2435 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2436 * @state: a pointer to a &struct phylink_link_state
2438 * Inspect the interface mode, advertising mask or forced speed and
2439 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2440 * the interface mode to suit. @state->interface is appropriately
2441 * updated, and the advertising mask has the "other" baseX_Full flag
2444 void phylink_helper_basex_speed(struct phylink_link_state *state)
2446 if (phy_interface_mode_is_8023z(state->interface)) {
2447 bool want_2500 = state->an_enabled ?
2448 phylink_test(state->advertising, 2500baseX_Full) :
2449 state->speed == SPEED_2500;
2452 phylink_clear(state->advertising, 1000baseX_Full);
2453 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2455 phylink_clear(state->advertising, 2500baseX_Full);
2456 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2460 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2462 static void phylink_decode_c37_word(struct phylink_link_state *state,
2463 uint16_t config_reg, int speed)
2465 bool tx_pause, rx_pause;
2468 if (speed == SPEED_2500)
2469 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2471 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2473 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2475 if (linkmode_test_bit(fd_bit, state->advertising) &&
2476 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2477 state->speed = speed;
2478 state->duplex = DUPLEX_FULL;
2480 /* negotiation failure */
2481 state->link = false;
2484 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2485 &tx_pause, &rx_pause);
2488 state->pause |= MLO_PAUSE_TX;
2490 state->pause |= MLO_PAUSE_RX;
2493 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2494 uint16_t config_reg)
2496 if (!(config_reg & LPA_SGMII_LINK)) {
2497 state->link = false;
2501 switch (config_reg & LPA_SGMII_SPD_MASK) {
2503 state->speed = SPEED_10;
2506 state->speed = SPEED_100;
2508 case LPA_SGMII_1000:
2509 state->speed = SPEED_1000;
2512 state->link = false;
2515 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2516 state->duplex = DUPLEX_FULL;
2518 state->duplex = DUPLEX_HALF;
2522 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2523 * @state: a pointer to a struct phylink_link_state.
2524 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2526 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2527 * code word. Decode the USXGMII code word and populate the corresponding fields
2528 * (speed, duplex) into the phylink_link_state structure.
2530 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2533 switch (lpa & MDIO_USXGMII_SPD_MASK) {
2534 case MDIO_USXGMII_10:
2535 state->speed = SPEED_10;
2537 case MDIO_USXGMII_100:
2538 state->speed = SPEED_100;
2540 case MDIO_USXGMII_1000:
2541 state->speed = SPEED_1000;
2543 case MDIO_USXGMII_2500:
2544 state->speed = SPEED_2500;
2546 case MDIO_USXGMII_5000:
2547 state->speed = SPEED_5000;
2549 case MDIO_USXGMII_10G:
2550 state->speed = SPEED_10000;
2553 state->link = false;
2557 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2558 state->duplex = DUPLEX_FULL;
2560 state->duplex = DUPLEX_HALF;
2562 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2565 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2566 * @pcs: a pointer to a &struct mdio_device.
2567 * @state: a pointer to a &struct phylink_link_state.
2569 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2570 * clause 37 negotiation and/or SGMII control.
2572 * Read the MAC PCS state from the MII device configured in @config and
2573 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2574 * the phylink @state structure. This is suitable to be directly plugged
2575 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2578 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2579 struct phylink_link_state *state)
2583 bmsr = mdiodev_read(pcs, MII_BMSR);
2584 lpa = mdiodev_read(pcs, MII_LPA);
2585 if (bmsr < 0 || lpa < 0) {
2586 state->link = false;
2590 state->link = !!(bmsr & BMSR_LSTATUS);
2591 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2592 /* If there is no link or autonegotiation is disabled, the LP advertisement
2593 * data is not meaningful, so don't go any further.
2595 if (!state->link || !state->an_enabled)
2598 switch (state->interface) {
2599 case PHY_INTERFACE_MODE_1000BASEX:
2600 phylink_decode_c37_word(state, lpa, SPEED_1000);
2603 case PHY_INTERFACE_MODE_2500BASEX:
2604 phylink_decode_c37_word(state, lpa, SPEED_2500);
2607 case PHY_INTERFACE_MODE_SGMII:
2608 case PHY_INTERFACE_MODE_QSGMII:
2609 phylink_decode_sgmii_word(state, lpa);
2613 state->link = false;
2617 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2620 * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2622 * @pcs: a pointer to a &struct mdio_device.
2623 * @interface: the PHY interface mode being configured
2624 * @advertising: the ethtool advertisement mask
2626 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2627 * clause 37 negotiation and/or SGMII control.
2629 * Configure the clause 37 PCS advertisement as specified by @state. This
2630 * does not trigger a renegotiation; phylink will do that via the
2631 * mac_an_restart() method of the struct phylink_mac_ops structure.
2633 * Returns negative error code on failure to configure the advertisement,
2634 * zero if no change has been made, or one if the advertisement has changed.
2636 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
2637 phy_interface_t interface,
2638 const unsigned long *advertising)
2642 switch (interface) {
2643 case PHY_INTERFACE_MODE_1000BASEX:
2644 case PHY_INTERFACE_MODE_2500BASEX:
2645 adv = ADVERTISE_1000XFULL;
2646 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2648 adv |= ADVERTISE_1000XPAUSE;
2649 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2651 adv |= ADVERTISE_1000XPSE_ASYM;
2653 return mdiodev_modify_changed(pcs, MII_ADVERTISE, 0xffff, adv);
2655 case PHY_INTERFACE_MODE_SGMII:
2656 return mdiodev_modify_changed(pcs, MII_ADVERTISE, 0xffff, 0x0001);
2659 /* Nothing to do for other modes */
2663 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2666 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
2667 * @pcs: a pointer to a &struct mdio_device.
2668 * @mode: link autonegotiation mode
2669 * @interface: the PHY interface mode being configured
2670 * @advertising: the ethtool advertisement mask
2672 * Configure a Clause 22 PCS PHY with the appropriate negotiation
2673 * parameters for the @mode, @interface and @advertising parameters.
2674 * Returns negative error number on failure, zero if the advertisement
2675 * has not changed, or positive if there is a change.
2677 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
2678 phy_interface_t interface,
2679 const unsigned long *advertising)
2685 ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface,
2692 /* Ensure ISOLATE bit is disabled */
2693 if (mode == MLO_AN_INBAND &&
2694 linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, advertising))
2695 bmcr = BMCR_ANENABLE;
2699 ret = mdiodev_modify(pcs, MII_BMCR, BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
2703 return changed ? 1 : 0;
2705 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
2708 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2709 * @pcs: a pointer to a &struct mdio_device.
2711 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2712 * clause 37 negotiation.
2714 * Restart the clause 37 negotiation with the link partner. This is
2715 * suitable to be directly plugged into the mac_pcs_get_state() member
2716 * of the struct phylink_mac_ops structure.
2718 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2720 int val = mdiodev_read(pcs, MII_BMCR);
2723 val |= BMCR_ANRESTART;
2725 mdiodev_write(pcs, MII_BMCR, val);
2728 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2730 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2731 struct phylink_link_state *state)
2733 struct mii_bus *bus = pcs->bus;
2734 int addr = pcs->addr;
2737 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
2739 state->link = false;
2743 state->link = !!(stat & MDIO_STAT1_LSTATUS);
2747 switch (state->interface) {
2748 case PHY_INTERFACE_MODE_10GBASER:
2749 state->speed = SPEED_10000;
2750 state->duplex = DUPLEX_FULL;
2757 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2759 MODULE_LICENSE("GPL v2");