Lines Matching +full:mac +full:- +full:phy

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.
15 #include <linux/phy.h>
23 #include "phy-caps.h"
38 * struct phylink - internal data type for phylink
55 u8 link_port; /* The current non-phy ethtool port */
70 /* Serialize updates to pl->phydev with phylink_resolve() */
103 if ((pl)->config->type == PHYLINK_NETDEV) \
104 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
105 else if ((pl)->config->type == PHYLINK_DEV) \
106 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
118 if ((pl)->config->type == PHYLINK_NETDEV) \
119 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
120 else if ((pl)->config->type == PHYLINK_DEV) \
121 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
151 * phylink_set_port_modes() - set the port type modes in the ethtool mask
154 * Sets all the port type modes in the ethtool mask. MAC drivers should
183 [MLO_AN_PHY] = "phy",
201 return "inband,an-enabled";
203 return "inband,an-disabled";
227 * phylink_interface_max_speed() - get the maximum speed of a phy interface
228 * @interface: phy interface mode defined by &typedef phy_interface_t
230 * Determine the maximum speed of a phy interface. This is intended to help
231 * determine the correct speed to pass to the MAC when the phy is performing
331 * phylink_caps_to_link_caps() - Convert a set of MAC capabilities LINK caps
332 * @caps: A set of MAC capabilities
334 * Returns: The corresponding set of LINK_CAPA as defined in phy-caps.h
361 * phylink_caps_to_linkmodes() - Convert capabilities to ethtool link modes
363 * @caps: bitmask of MAC capabilities
383 * phylink_limit_mac_speed - limit the phylink_config to a maximum speed
387 * Mask off MAC capabilities for speeds higher than the @max_speed parameter.
396 config->mac_capabilities &= ~phylink_caps_params[i].mask;
401 * phylink_cap_from_speed_duplex - Get mac capability from speed/duplex
405 * Find the mac capability for a given speed and duplex.
407 * Return: A mask with the mac capability patching @speed and @duplex, or 0 if
425 * phylink_get_capabilities() - get capabilities for a given MAC
426 * @interface: phy interface mode defined by &typedef phy_interface_t
427 * @mac_capabilities: bitmask of MAC capabilities
430 * Get the MAC capabilities that are supported by the @interface mode and
452 /* The MAC must support asymmetric pause towards the local
463 /* We can't adapt if the MAC doesn't support the interface's
472 /* The MAC must support half duplex at the interface's max
487 * phylink_validate_mask_caps() - Restrict link modes based on caps
490 * @mac_capabilities: bitmask of MAC capabilities
505 caps = phylink_get_capabilities(state->interface, mac_capabilities,
506 state->rate_matching);
510 linkmode_and(state->advertising, state->advertising, mask);
522 if (pl->mac_ops->mac_select_pcs) {
523 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
533 if (!pcs->ops) {
535 phy_modes(state->interface));
537 return -EINVAL;
540 /* Ensure that this PCS supports the interface which the MAC
541 * returned it for. It is an error for the MAC to return a PCS
544 if (!phy_interface_empty(pcs->supported_interfaces) &&
545 !test_bit(state->interface, pcs->supported_interfaces)) {
546 phylink_err(pl, "MAC returned PCS which does not support %s\n",
547 phy_modes(state->interface));
548 return -EINVAL;
552 if (pcs->ops->pcs_validate) {
553 ret = pcs->ops->pcs_validate(pcs, supported, state);
555 return -EINVAL;
560 linkmode_and(state->advertising, state->advertising,
565 /* Then validate the link parameters with the MAC */
566 if (pl->mac_ops->mac_get_caps)
567 capabilities = pl->mac_ops->mac_get_caps(pl->config,
568 state->interface);
570 capabilities = pl->config->mac_capabilities;
574 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
577 static void phylink_validate_one(struct phylink *pl, struct phy_device *phy,
592 if (phy)
593 tmp_state.rate_matching = phy_get_rate_matching(phy, interface);
607 static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy,
617 phylink_validate_one(pl, phy, supported, state, interface,
621 linkmode_copy(state->advertising, all_adv);
623 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
629 const unsigned long *interfaces = pl->config->supported_interfaces;
631 if (state->interface == PHY_INTERFACE_MODE_NA)
635 if (!test_bit(state->interface, interfaces))
636 return -EINVAL;
668 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
672 pl->link_config.speed = speed;
673 pl->link_config.duplex = DUPLEX_HALF;
675 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
676 pl->link_config.duplex = DUPLEX_FULL;
678 /* We treat the "pause" and "asym-pause" terminology as
683 pl->link_config.lp_advertising);
684 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
686 pl->link_config.lp_advertising);
693 pl->link_gpio = desc;
694 else if (desc == ERR_PTR(-EPROBE_DEFER))
695 ret = -EPROBE_DEFER;
704 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
707 phylink_err(pl, "broken fixed-link?\n");
708 return -EINVAL;
711 phylink_warn(pl, "%pfw uses deprecated array-style fixed-link binding!\n",
714 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
717 pl->link_config.duplex = prop[1] ?
719 pl->link_config.speed = prop[2];
722 pl->link_config.lp_advertising);
725 pl->link_config.lp_advertising);
729 if (pl->link_config.speed > SPEED_1000 &&
730 pl->link_config.duplex != DUPLEX_FULL)
732 pl->link_config.speed);
734 linkmode_zero(pl->supported);
735 phylink_fill_fixedlink_supported(pl->supported);
737 linkmode_copy(pl->link_config.advertising, pl->supported);
738 phylink_validate(pl, pl->supported, &pl->link_config);
740 c = phy_caps_lookup(pl->link_config.speed, pl->link_config.duplex,
741 pl->supported, true);
743 linkmode_and(match, pl->supported, c->linkmodes);
748 linkmode_and(pl->supported, pl->supported, mask);
750 phylink_set(pl->supported, MII);
753 linkmode_or(pl->supported, pl->supported, match);
754 linkmode_or(pl->link_config.lp_advertising,
755 pl->link_config.lp_advertising, match);
758 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
759 pl->link_config.speed);
762 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
763 pl->supported);
765 pl->link_config.link = 1;
766 pl->link_config.an_complete = 1;
778 if (pl->config->default_an_inband)
779 pl->cfg_link_an_mode = MLO_AN_INBAND;
781 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
782 if (dn || fwnode_property_present(fwnode, "fixed-link"))
783 pl->cfg_link_an_mode = MLO_AN_FIXED;
787 strcmp(managed, "in-band-status") == 0)) {
788 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
790 "can't use both fixed-link and in-band-status\n");
791 return -EINVAL;
794 pl->cfg_link_an_mode = MLO_AN_INBAND;
797 if (pl->cfg_link_an_mode == MLO_AN_INBAND) {
798 linkmode_zero(pl->supported);
799 phylink_set(pl->supported, MII);
800 phylink_set(pl->supported, Autoneg);
801 phylink_set(pl->supported, Asym_Pause);
802 phylink_set(pl->supported, Pause);
804 switch (pl->link_config.interface) {
827 caps = phylink_get_capabilities(pl->link_config.interface, caps,
829 phylink_caps_to_linkmodes(pl->supported, caps);
834 "incorrect link mode %s for in-band status\n",
835 phy_modes(pl->link_config.interface));
836 return -EINVAL;
839 linkmode_copy(pl->link_config.advertising, pl->supported);
841 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
843 "failed to validate link configuration for in-band status\n");
844 return -EINVAL;
856 state->advertising))
857 state->pause &= ~MLO_PAUSE_AN;
860 if (!(pl->link_config.pause & MLO_PAUSE_AN))
861 state->pause = pl->link_config.pause;
868 if (state->duplex == DUPLEX_FULL) {
869 linkmode_resolve_pause(state->advertising,
870 state->lp_advertising,
873 state->pause |= MLO_PAUSE_TX;
875 state->pause |= MLO_PAUSE_RX;
882 if (pcs && pcs->ops->pcs_inband_caps)
883 return pcs->ops->pcs_inband_caps(pcs, interface);
891 if (pcs && pcs->ops->pcs_pre_config)
892 pcs->ops->pcs_pre_config(pcs, interface);
900 if (pcs && pcs->ops->pcs_post_config)
901 err = pcs->ops->pcs_post_config(pcs, interface);
908 if (pcs && pcs->ops->pcs_disable)
909 pcs->ops->pcs_disable(pcs);
916 if (pcs && pcs->ops->pcs_enable)
917 err = pcs->ops->pcs_enable(pcs);
929 return pcs->ops->pcs_config(pcs, neg_mode, state->interface,
930 state->advertising, permit_pause_to_mac);
937 if (pcs && pcs->ops->pcs_link_up)
938 pcs->ops->pcs_link_up(pcs, neg_mode, interface, speed, duplex);
943 if (pcs && pcs->ops->pcs_disable_eee)
944 pcs->ops->pcs_disable_eee(pcs);
949 if (pcs && pcs->ops->pcs_enable_eee)
950 pcs->ops->pcs_enable_eee(pcs);
953 /* Query inband for a specific interface mode, asking the MAC for the
961 if (!pl->mac_ops->mac_select_pcs)
964 pcs = pl->mac_ops->mac_select_pcs(pl->config, interface);
973 if (pl->cfg_link_an_mode == MLO_AN_INBAND)
974 timer_delete(&pl->link_poll);
979 if (pl->pcs && pl->pcs->poll && pl->cfg_link_an_mode == MLO_AN_INBAND)
980 mod_timer(&pl->link_poll, jiffies + HZ);
987 /* Signal to PCS driver that MAC requires RX clock for init */
988 if (pl->config->mac_requires_rxc)
989 pcs->rxc_always_on = true;
991 if (pcs->ops->pcs_pre_init)
992 ret = pcs->ops->pcs_pre_init(pcs);
1012 __func__, phylink_an_mode_str(pl->act_link_an_mode),
1018 pl->mac_ops->mac_config(pl->config, pl->act_link_an_mode, &st);
1023 if (pl->pcs && linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1024 pl->link_config.advertising) &&
1025 phy_interface_mode_is_8023z(pl->link_config.interface) &&
1026 phylink_autoneg_inband(pl->act_link_an_mode))
1027 pl->pcs->ops->pcs_an_restart(pl->pcs);
1044 /* These protocols are designed for use with a PHY which
1045 * communicates its negotiation result back to the MAC via
1053 /* 1000base-X is designed for use media-side for Fibre
1055 * taken into account. We also do this for 2500base-X
1067 * phylink_pcs_neg_mode() - helper to determine PCS inband mode
1076 * - %PHYLINK_PCS_NEG_NONE: interface mode does not support inband
1077 * - %PHYLINK_PCS_NEG_OUTBAND: an out of band mode (e.g. reading the PHY)
1079 * - %PHYLINK_PCS_NEG_INBAND_DISABLED: inband mode selected but autoneg
1081 * - %PHYLINK_PCS_NEG_INBAND_ENABLED: inband mode selected and autoneg enabled
1097 pl->pcs_neg_mode = PHYLINK_PCS_NEG_NONE;
1098 pl->act_link_an_mode = pl->req_link_an_mode;
1102 mode = pl->req_link_an_mode;
1104 pl->phy_ib_mode = 0;
1109 if (pl->phydev)
1110 phy_ib_caps = phy_inband_caps(pl->phydev, interface);
1112 phylink_dbg(pl, "interface %s inband modes: pcs=%02x phy=%02x\n",
1120 /* PCS supports reporting in-band capabilities, and
1130 /* PHY supports in-band capabilities, and supports
1134 pl->phy_ib_mode = LINK_INBAND_DISABLE;
1136 pl->phy_ib_mode = LINK_INBAND_BYPASS;
1141 /* If either the PCS or PHY requires inband to be enabled,
1151 phy_ib_only ? "PHY" : "");
1154 } else if (type == INBAND_CISCO_SGMII || pl->phydev) {
1156 * Base-X with a PHY, we try to use in-band mode where-ever
1158 * do not support in-band.
1164 /* PCS PHY
1170 * 0 0 1 0 phy doesn't support outband
1171 * 1 0 1 0 pcs+phy doesn't support outband
1172 * 0 1 1 0 pcs required, phy doesn't support, invalid
1173 * 1 1 1 0 pcs optional, phy doesn't support, outband
1174 * 0 0 0 1 phy required inband enabled
1175 * 1 0 0 1 pcs doesn't support, phy required, invalid
1176 * 0 1 0 1 pcs+phy required inband enabled
1177 * 1 1 0 1 pcs optional, phy required inband enabled
1178 * 0 0 1 1 phy optional inband enabled
1179 * 1 0 1 1 pcs doesn't support, phy optional, outband
1180 * 0 1 1 1 pcs required, phy optional inband enabled
1181 * 1 1 1 1 pcs+phy optional inband enabled
1185 /* In-band supported or unknown at both ends. Enable
1186 * in-band mode with or without bypass at the PHY.
1189 pl->phy_ib_mode = LINK_INBAND_ENABLE;
1191 pl->phy_ib_mode = LINK_INBAND_BYPASS;
1196 /* Either in-band not supported at at least one end.
1197 * In-band bypass at the other end is possible.
1200 pl->phy_ib_mode = LINK_INBAND_DISABLE;
1202 pl->phy_ib_mode = LINK_INBAND_BYPASS;
1205 if (pl->phydev)
1209 phylink_warn(pl, "%s: incompatible in-band capabilities, trying in-band",
1214 /* For Base-X without a PHY */
1232 pl->pcs_neg_mode = neg_mode;
1233 pl->act_link_an_mode = mode;
1245 phylink_an_mode_str(pl->req_link_an_mode),
1246 phy_modes(state->interface));
1248 pl->major_config_failed = false;
1250 if (pl->mac_ops->mac_select_pcs) {
1251 pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface);
1257 pl->major_config_failed = true;
1261 pcs_changed = pl->pcs != pcs;
1264 phylink_pcs_neg_mode(pl, pcs, state->interface, state->advertising);
1267 phylink_an_mode_str(pl->act_link_an_mode),
1268 phylink_pcs_mode_str(pl->pcs_neg_mode),
1269 phy_modes(state->interface));
1273 if (pl->mac_ops->mac_prepare) {
1274 err = pl->mac_ops->mac_prepare(pl->config, pl->act_link_an_mode,
1275 state->interface);
1279 pl->major_config_failed = true;
1284 /* If we have a new PCS, switch to the new PCS after preparing the MAC
1288 phylink_pcs_disable(pl->pcs);
1290 if (pl->pcs)
1291 pl->pcs->phylink = NULL;
1293 pcs->phylink = pl;
1295 pl->pcs = pcs;
1298 if (pl->pcs)
1299 phylink_pcs_pre_config(pl->pcs, state->interface);
1303 if (pl->pcs) {
1304 err = phylink_pcs_post_config(pl->pcs, state->interface);
1309 pl->major_config_failed = true;
1313 if (pl->pcs_state == PCS_STATE_STARTING || pcs_changed)
1314 phylink_pcs_enable(pl->pcs);
1316 err = phylink_pcs_config(pl->pcs, pl->pcs_neg_mode, state,
1317 !!(pl->link_config.pause & MLO_PAUSE_AN));
1320 pl->major_config_failed = true;
1328 if (pl->mac_ops->mac_finish) {
1329 err = pl->mac_ops->mac_finish(pl->config, pl->act_link_an_mode,
1330 state->interface);
1335 pl->major_config_failed = true;
1339 if (pl->phydev && pl->phy_ib_mode) {
1340 err = phy_config_inband(pl->phydev, pl->phy_ib_mode);
1345 pl->major_config_failed = true;
1349 if (pl->sfp_bus) {
1350 rate_kbd = phylink_interface_signal_rate(state->interface);
1352 sfp_upstream_set_signal_rate(pl->sfp_bus, rate_kbd);
1362 * the full MAC reconfiguration.
1368 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
1372 phylink_an_mode_str(pl->req_link_an_mode),
1373 phy_modes(pl->link_config.interface),
1374 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
1375 pl->link_config.pause);
1378 phylink_pcs_neg_mode(pl, pl->pcs, pl->link_config.interface,
1379 pl->link_config.advertising);
1381 /* Modern PCS-based method; update the advert at the PCS, and
1385 ret = phylink_pcs_config(pl->pcs, pl->pcs_neg_mode, &pl->link_config,
1386 !!(pl->link_config.pause & MLO_PAUSE_AN));
1402 linkmode_copy(state->advertising, pl->link_config.advertising);
1403 linkmode_zero(state->lp_advertising);
1404 state->interface = pl->link_config.interface;
1405 state->rate_matching = pl->link_config.rate_matching;
1406 state->an_complete = 0;
1407 state->link = 1;
1409 autoneg = pl->pcs_neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED;
1411 state->speed = SPEED_UNKNOWN;
1412 state->duplex = DUPLEX_UNKNOWN;
1413 state->pause = MLO_PAUSE_NONE;
1415 state->speed = pl->link_config.speed;
1416 state->duplex = pl->link_config.duplex;
1417 state->pause = pl->link_config.pause;
1420 pcs = pl->pcs;
1422 pcs->ops->pcs_get_state(pcs, pl->pcs_neg_mode, state);
1424 state->link = 0;
1433 *state = pl->link_config;
1434 if (pl->config->get_fixed_state)
1435 pl->config->get_fixed_state(pl->config, state);
1436 else if (pl->link_gpio)
1437 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
1439 state->pause = MLO_PAUSE_NONE;
1446 struct phy_device *phy = pl->phydev;
1448 switch (pl->req_link_an_mode) {
1450 link_state = pl->phy_state;
1458 link_state = pl->link_config;
1470 if (phy)
1471 mutex_lock(&phy->lock);
1473 if (phy)
1474 mutex_unlock(&phy->lock);
1493 if (pl->mac_enable_tx_lpi) {
1494 pl->mac_enable_tx_lpi = false;
1498 pl->mac_ops->mac_disable_tx_lpi(pl->config);
1500 phylink_pcs_disable_eee(pl->pcs);
1508 if (!test_bit(pl->cur_interface, pl->config->lpi_interfaces)) {
1509 phylink_dbg(pl, "MAC does not support LPI with %s\n",
1510 phy_modes(pl->cur_interface));
1515 pl->mac_tx_lpi_timer, pl->mac_tx_clk_stop);
1517 phylink_pcs_enable_eee(pl->pcs);
1519 err = pl->mac_ops->mac_enable_tx_lpi(pl->config, pl->mac_tx_lpi_timer,
1520 pl->mac_tx_clk_stop);
1522 phylink_pcs_disable_eee(pl->pcs);
1524 pl->mac_ops->mac_enable_tx_lpi, ERR_PTR(err));
1528 pl->mac_enable_tx_lpi = true;
1534 struct net_device *ndev = pl->netdev;
1544 /* The PHY is doing rate matchion from the media rate (in
1546 * pause frames to the MAC to limit its transmission speed.
1554 /* The PHY is doing rate matchion from the media rate (in
1556 * collisions to the MAC to limit its transmission speed.
1563 pl->cur_interface = link_state.interface;
1565 phylink_pcs_link_up(pl->pcs, pl->pcs_neg_mode, pl->cur_interface, speed,
1568 pl->mac_ops->mac_link_up(pl->config, pl->phydev, pl->act_link_an_mode,
1569 pl->cur_interface, speed, duplex,
1572 if (pl->mac_supports_eee && pl->phy_enable_tx_lpi)
1579 "Link is Up - %s/%s - flow control %s\n",
1587 struct net_device *ndev = pl->netdev;
1594 pl->mac_ops->mac_link_down(pl->config, pl->act_link_an_mode,
1595 pl->cur_interface);
1601 return pl->netdev ? netif_carrier_ok(pl->netdev) : pl->old_link_state;
1610 struct phy_device *phy;
1613 mutex_lock(&pl->phydev_mutex);
1614 phy = pl->phydev;
1615 if (phy)
1616 mutex_lock(&phy->lock);
1617 mutex_lock(&pl->state_mutex);
1620 if (pl->phylink_disable_state) {
1621 pl->link_failed = false;
1623 } else if (pl->link_failed) {
1626 } else if (pl->act_link_an_mode == MLO_AN_FIXED) {
1629 } else if (pl->act_link_an_mode == MLO_AN_PHY) {
1630 link_state = pl->phy_state;
1635 /* The PCS may have a latching link-fail indicator. If the link
1636 * was up, bring the link down and re-trigger the resolve.
1637 * Otherwise, re-read the PCS state to get the current status
1647 /* If we have a phy, the "up" state is the union of both the
1648 * PHY and the MAC
1650 if (phy)
1651 link_state.link &= pl->phy_state.link;
1653 /* Only update if the PHY link is up */
1654 if (phy && pl->phy_state.link) {
1656 * event if the link isn't already down, and re-resolve.
1658 if (link_state.interface != pl->phy_state.interface) {
1663 link_state.interface = pl->phy_state.interface;
1666 * speed/duplex comes from the PHY
1668 if (pl->phy_state.rate_matching) {
1670 pl->phy_state.rate_matching;
1671 link_state.speed = pl->phy_state.speed;
1672 link_state.duplex = pl->phy_state.duplex;
1675 /* If we have a PHY, we need to update with the PHY
1678 link_state.pause = pl->phy_state.pause;
1683 if (pl->act_link_an_mode != MLO_AN_FIXED)
1687 if (link_state.interface != pl->link_config.interface) {
1696 pl->link_config.interface = link_state.interface;
1703 if (pl->major_config_failed)
1707 pl->old_link_state = link_state.link;
1714 pl->link_failed = false;
1715 queue_work(system_power_efficient_wq, &pl->resolve);
1717 mutex_unlock(&pl->state_mutex);
1718 if (phy)
1719 mutex_unlock(&phy->lock);
1720 mutex_unlock(&pl->phydev_mutex);
1725 if (!pl->phylink_disable_state)
1726 queue_work(system_power_efficient_wq, &pl->resolve);
1731 unsigned long state = pl->phylink_disable_state;
1733 set_bit(bit, &pl->phylink_disable_state);
1735 queue_work(system_power_efficient_wq, &pl->resolve);
1736 flush_work(&pl->resolve);
1742 clear_bit(bit, &pl->phylink_disable_state);
1772 pl->sfp_bus = bus;
1781 * phylink_set_fixed_link() - set the fixed link
1796 if (pl->cfg_link_an_mode != MLO_AN_PHY || !state ||
1797 !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
1798 return -EINVAL;
1800 c = phy_caps_lookup(state->speed, state->duplex,
1801 pl->supported, true);
1803 return -EINVAL;
1805 adv = pl->link_config.advertising;
1806 linkmode_and(adv, pl->supported, c->linkmodes);
1809 pl->link_config.speed = state->speed;
1810 pl->link_config.duplex = state->duplex;
1811 pl->link_config.link = 1;
1812 pl->link_config.an_complete = 1;
1814 pl->cfg_link_an_mode = MLO_AN_FIXED;
1815 pl->req_link_an_mode = pl->cfg_link_an_mode;
1822 * phylink_create() - create a phylink instance
1827 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
1830 * This will parse in-band modes, fixed-link or SFP configuration.
1834 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
1846 if (phy_interface_empty(config->supported_interfaces)) {
1847 dev_err(config->dev,
1849 return ERR_PTR(-EINVAL);
1854 return ERR_PTR(-ENOMEM);
1856 mutex_init(&pl->phydev_mutex);
1857 mutex_init(&pl->state_mutex);
1858 INIT_WORK(&pl->resolve, phylink_resolve);
1860 pl->config = config;
1861 if (config->type == PHYLINK_NETDEV) {
1862 pl->netdev = to_net_dev(config->dev);
1863 netif_carrier_off(pl->netdev);
1864 } else if (config->type == PHYLINK_DEV) {
1865 pl->dev = config->dev;
1868 return ERR_PTR(-EINVAL);
1871 pl->mac_supports_eee_ops = phylink_mac_implements_lpi(mac_ops);
1872 pl->mac_supports_eee = pl->mac_supports_eee_ops &&
1873 pl->config->lpi_capabilities &&
1874 !phy_interface_empty(pl->config->lpi_interfaces);
1877 pl->eee_cfg.eee_enabled = pl->config->eee_enabled_default;
1878 pl->eee_cfg.tx_lpi_enabled = pl->eee_cfg.eee_enabled;
1879 pl->eee_cfg.tx_lpi_timer = pl->config->lpi_timer_default;
1881 pl->phy_state.interface = iface;
1882 pl->link_interface = iface;
1884 pl->link_port = PORT_BNC;
1886 pl->link_port = PORT_MII;
1887 pl->link_config.interface = iface;
1888 pl->link_config.pause = MLO_PAUSE_AN;
1889 pl->link_config.speed = SPEED_UNKNOWN;
1890 pl->link_config.duplex = DUPLEX_UNKNOWN;
1891 pl->pcs_state = PCS_STATE_DOWN;
1892 pl->mac_ops = mac_ops;
1893 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1894 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
1896 linkmode_fill(pl->supported);
1897 linkmode_copy(pl->link_config.advertising, pl->supported);
1898 phylink_validate(pl, pl->supported, &pl->link_config);
1906 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
1914 pl->req_link_an_mode = pl->cfg_link_an_mode;
1927 * phylink_destroy() - cleanup and destroy the phylink instance
1930 * Destroy a phylink instance. Any PHY that has been attached must have been
1937 sfp_bus_del_upstream(pl->sfp_bus);
1938 if (pl->link_gpio)
1939 gpiod_put(pl->link_gpio);
1941 cancel_work_sync(&pl->resolve);
1947 * phylink_expects_phy() - Determine if phylink expects a phy to be attached
1950 * When using fixed-link mode, or in-band mode with 1000base-X or 2500base-X,
1951 * no PHY is needed.
1953 * Returns true if phylink will be expecting a PHY.
1957 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1958 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1959 phy_interface_mode_is_8023z(pl->link_interface)))
1967 struct phylink *pl = phydev->phylink;
1972 mutex_lock(&pl->state_mutex);
1973 pl->phy_state.speed = phydev->speed;
1974 pl->phy_state.duplex = phydev->duplex;
1975 pl->phy_state.rate_matching = phydev->rate_matching;
1976 pl->phy_state.pause = MLO_PAUSE_NONE;
1978 pl->phy_state.pause |= MLO_PAUSE_TX;
1980 pl->phy_state.pause |= MLO_PAUSE_RX;
1981 pl->phy_state.interface = phydev->interface;
1982 pl->phy_state.link = up;
1984 pl->link_failed = true;
1987 pl->phy_enable_tx_lpi = phydev->enable_tx_lpi;
1988 pl->mac_tx_lpi_timer = phydev->eee_cfg.tx_lpi_timer;
1989 mutex_unlock(&pl->state_mutex);
1993 phylink_dbg(pl, "phy link %s %s/%s/%s/%s/%s/%slpi\n",
1995 phy_modes(phydev->interface),
1996 phy_speed_to_str(phydev->speed),
1997 phy_duplex_to_str(phydev->duplex),
1998 phy_rate_matching_to_str(phydev->rate_matching),
1999 phylink_pause_to_str(pl->phy_state.pause),
2000 phydev->enable_tx_lpi ? "" : "no");
2003 static int phylink_validate_phy(struct phylink *pl, struct phy_device *phy,
2009 /* If the PHY provides a bitmap of the interfaces it will be using
2013 if (!phy_interface_empty(phy->possible_interfaces)) {
2014 /* We only care about the union of the PHY's interfaces and
2017 phy_interface_and(interfaces, phy->possible_interfaces,
2018 pl->config->supported_interfaces);
2021 phylink_err(pl, "PHY has no common interfaces\n");
2022 return -EINVAL;
2025 if (phy_on_sfp(phy)) {
2026 /* If the PHY is on a SFP, limit the interfaces to
2033 phylink_err(pl, "SFP PHY's possible interfaces becomes empty\n");
2034 return -EINVAL;
2038 phylink_dbg(pl, "PHY %s uses interfaces %*pbl, validating %*pbl\n",
2039 phydev_name(phy),
2041 phy->possible_interfaces,
2044 return phylink_validate_mask(pl, phy, supported, state,
2048 phylink_dbg(pl, "PHY %s doesn't supply possible interfaces\n",
2049 phydev_name(phy));
2054 state->rate_matching = phy_get_rate_matching(phy, state->interface);
2056 /* Clause 45 PHYs may switch their Serdes lane between, e.g. 10GBASE-R,
2057 * 5GBASE-R, 2500BASE-X and SGMII if they are not using rate matching.
2062 * interface modes the PHY supports to properly work out which ethtool
2063 * linkmodes can be supported. For now, as a work-around, we validate
2067 if (phy->is_c45 && state->rate_matching == RATE_MATCH_NONE &&
2068 state->interface != PHY_INTERFACE_MODE_RXAUI &&
2069 state->interface != PHY_INTERFACE_MODE_XAUI &&
2070 state->interface != PHY_INTERFACE_MODE_USXGMII)
2071 state->interface = PHY_INTERFACE_MODE_NA;
2076 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
2086 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2087 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2088 * using our validate call to the MAC, we rely upon the MAC
2091 phy_support_asym_pause(phy);
2094 linkmode_copy(supported, phy->supported);
2095 linkmode_copy(config.advertising, phy->advertising);
2098 ret = phylink_validate_phy(pl, phy, supported, &config);
2102 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
2108 phy->phylink = pl;
2109 phy->phy_link_change = phylink_phy_change;
2111 irq_str = phy_attached_info_irq(phy);
2113 "PHY [%s] driver [%s] (irq=%s)\n",
2114 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
2117 mutex_lock(&pl->phydev_mutex);
2118 mutex_lock(&phy->lock);
2119 mutex_lock(&pl->state_mutex);
2120 pl->phydev = phy;
2121 pl->phy_state.interface = interface;
2122 pl->phy_state.pause = MLO_PAUSE_NONE;
2123 pl->phy_state.speed = SPEED_UNKNOWN;
2124 pl->phy_state.duplex = DUPLEX_UNKNOWN;
2125 pl->phy_state.rate_matching = RATE_MATCH_NONE;
2126 linkmode_copy(pl->supported, supported);
2127 linkmode_copy(pl->link_config.advertising, config.advertising);
2129 /* Restrict the phy advertisement according to the MAC support. */
2130 linkmode_copy(phy->advertising, config.advertising);
2132 /* If the MAC supports phylink managed EEE, restrict the EEE
2133 * advertisement according to the MAC's LPI capabilities.
2135 if (pl->mac_supports_eee) {
2138 * This also enables EEE at the PHY.
2140 if (pl->eee_cfg.eee_enabled)
2141 phy_support_eee(phy);
2143 phy->eee_cfg.tx_lpi_enabled = pl->eee_cfg.tx_lpi_enabled;
2144 phy->eee_cfg.tx_lpi_timer = pl->eee_cfg.tx_lpi_timer;
2146 /* Convert the MAC's LPI capabilities to linkmodes */
2147 linkmode_zero(pl->supported_lpi);
2148 phylink_caps_to_linkmodes(pl->supported_lpi,
2149 pl->config->lpi_capabilities);
2152 * that the MAC supports.
2154 linkmode_and(phy->advertising_eee, phy->advertising_eee,
2155 pl->supported_lpi);
2156 } else if (pl->mac_supports_eee_ops) {
2157 /* MAC supports phylink EEE, but wants EEE always disabled. */
2158 phy_disable_eee(phy);
2161 mutex_unlock(&pl->state_mutex);
2162 mutex_unlock(&phy->lock);
2163 mutex_unlock(&pl->phydev_mutex);
2166 "phy: %s setting supported %*pb advertising %*pb\n",
2168 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
2169 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
2171 if (pl->config->mac_managed_pm)
2172 phy->mac_managed_pm = true;
2174 /* Allow the MAC to stop its clock if the PHY has the capability */
2175 pl->mac_tx_clk_stop = phy_eee_tx_clock_stop_capable(phy) > 0;
2177 if (pl->mac_supports_eee_ops) {
2178 /* Explicitly configure whether the PHY is allowed to stop it's
2181 ret = phy_eee_rx_clock_stop(phy,
2182 pl->config->eee_rx_clk_stop_enable);
2183 if (ret == -EOPNOTSUPP)
2187 if (ret == 0 && phy_interrupt_is_valid(phy))
2188 phy_request_interrupt(phy);
2193 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
2198 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
2199 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
2200 phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
2201 return -EINVAL;
2203 if (pl->phydev)
2204 return -EBUSY;
2206 if (pl->config->mac_requires_rxc)
2209 return phy_attach_direct(pl->netdev, phy, flags, interface);
2213 * phylink_connect_phy() - connect a PHY to the phylink instance
2215 * @phy: a pointer to a &struct phy_device.
2217 * Connect @phy to the phylink instance specified by @pl by calling
2218 * phy_attach_direct(). Configure the @phy according to the MAC driver's
2220 * that the PHY supports.
2227 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
2231 /* Use PHY device/driver interface */
2232 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
2233 pl->link_interface = phy->interface;
2234 pl->link_config.interface = pl->link_interface;
2237 ret = phylink_attach_phy(pl, phy, pl->link_interface);
2241 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
2243 phy_detach(phy);
2250 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
2253 * @flags: PHY-specific flags to communicate to the PHY device driver
2255 * Connect the phy specified in the device node @dn to the phylink instance
2269 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
2272 * @flags: PHY-specific flags to communicate to the PHY device driver
2274 * Connect the phy specified @fwnode to the phylink instance specified
2287 /* Fixed links and 802.3z are handled without needing a PHY */
2288 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
2289 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
2290 phy_interface_mode_is_8023z(pl->link_interface)))
2295 if (pl->cfg_link_an_mode == MLO_AN_PHY)
2296 return -ENODEV;
2304 return -ENODEV;
2306 /* Use PHY device/driver interface */
2307 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
2308 pl->link_interface = phy_dev->interface;
2309 pl->link_config.interface = pl->link_interface;
2312 if (pl->config->mac_requires_rxc)
2315 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
2316 pl->link_interface);
2321 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
2330 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
2334 * Disconnect any current PHY from the phylink instance described by @pl.
2338 struct phy_device *phy;
2342 mutex_lock(&pl->phydev_mutex);
2343 phy = pl->phydev;
2344 if (phy) {
2345 mutex_lock(&phy->lock);
2346 mutex_lock(&pl->state_mutex);
2347 pl->phydev = NULL;
2348 pl->phy_enable_tx_lpi = false;
2349 pl->mac_tx_clk_stop = false;
2350 mutex_unlock(&pl->state_mutex);
2351 mutex_unlock(&phy->lock);
2353 mutex_unlock(&pl->phydev_mutex);
2355 if (phy) {
2356 flush_work(&pl->resolve);
2357 phy_disconnect(phy);
2365 pl->link_failed = true;
2371 * phylink_mac_change() - notify phylink of a change in MAC state
2375 * The MAC driver should call this driver when the state of its link
2380 phylink_link_changed(pl, up, "mac");
2385 * phylink_pcs_change() - notify phylink of a change to PCS link state
2393 * the latched link-down state, otherwise pass false.
2397 struct phylink *pl = pcs->phylink;
2414 * phylink_start() - start a phylink instance
2417 * Start the phylink instance specified by @pl, configuring the MAC for the
2428 phylink_an_mode_str(pl->req_link_an_mode),
2429 phy_modes(pl->link_config.interface));
2432 if (pl->netdev)
2433 netif_carrier_off(pl->netdev);
2435 pl->pcs_state = PCS_STATE_STARTING;
2437 /* Apply the link configuration to the MAC when starting. This allows
2438 * a fixed-link to start with the correct parameters, and also
2447 pl->pcs_state = PCS_STATE_STARTED;
2451 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
2452 int irq = gpiod_to_irq(pl->link_gpio);
2459 pl->link_irq = irq;
2467 if (pl->cfg_link_an_mode == MLO_AN_FIXED)
2468 poll |= pl->config->poll_fixed_state;
2471 mod_timer(&pl->link_poll, jiffies + HZ);
2472 if (pl->phydev)
2473 phy_start(pl->phydev);
2474 if (pl->sfp_bus)
2475 sfp_upstream_start(pl->sfp_bus);
2480 * phylink_stop() - stop a phylink instance
2495 if (pl->sfp_bus)
2496 sfp_upstream_stop(pl->sfp_bus);
2497 if (pl->phydev)
2498 phy_stop(pl->phydev);
2499 timer_delete_sync(&pl->link_poll);
2500 if (pl->link_irq) {
2501 free_irq(pl->link_irq, pl);
2502 pl->link_irq = 0;
2507 pl->pcs_state = PCS_STATE_DOWN;
2509 phylink_pcs_disable(pl->pcs);
2514 * phylink_rx_clk_stop_block() - block PHY ability to stop receive clock in LPI
2517 * Disable the PHY's ability to stop the receive clock while the receive path
2525 if (pl->mac_rx_clk_stop_blocked == U8_MAX) {
2526 phylink_warn(pl, "%s called too many times - ignoring\n",
2532 /* Disable PHY receive clock stop if this is the first time this
2533 * function has been called and clock-stop was previously enabled.
2535 if (pl->mac_rx_clk_stop_blocked++ == 0 &&
2536 pl->mac_supports_eee_ops && pl->phydev &&
2537 pl->config->eee_rx_clk_stop_enable)
2538 phy_eee_rx_clock_stop(pl->phydev, false);
2543 * phylink_rx_clk_stop_unblock() - unblock PHY ability to stop receive clock
2554 if (pl->mac_rx_clk_stop_blocked == 0) {
2555 phylink_warn(pl, "%s called too many times - ignoring\n",
2561 /* Re-enable PHY receive clock stop if the number of unblocks matches
2564 if (--pl->mac_rx_clk_stop_blocked == 0 &&
2565 pl->mac_supports_eee_ops && pl->phydev &&
2566 pl->config->eee_rx_clk_stop_enable)
2567 phy_eee_rx_clock_stop(pl->phydev, true);
2573 return !!pl->mac_ops->mac_wol_set;
2579 return phydev && (pl->config->wol_phy_legacy || phy_can_wakeup(phydev));
2584 return pl->config->wol_phy_speed_ctrl && !pl->wolopts_mac &&
2585 pl->phydev && phy_may_wakeup(pl->phydev);
2589 * phylink_suspend() - handle a network device suspend event
2591 * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
2595 * - If Wake-on-Lan is not active, we can bring down the link between
2596 * the MAC and PHY by calling phylink_stop().
2597 * - If Wake-on-Lan is active, and being handled only by the PHY, we
2598 * can also bring down the link between the MAC and PHY.
2599 * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
2602 * Note: when phylink managed Wake-on-Lan is in use, @mac_wol is ignored.
2610 mac_wol = !!pl->wolopts_mac;
2612 if (mac_wol && (!pl->netdev || pl->netdev->ethtool->wol_enabled)) {
2613 /* Wake-on-Lan enabled, MAC handling */
2614 mutex_lock(&pl->state_mutex);
2617 __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
2619 pl->suspend_link_up = phylink_link_is_up(pl);
2620 if (pl->suspend_link_up) {
2625 if (pl->netdev)
2626 netif_carrier_off(pl->netdev);
2627 pl->old_link_state = false;
2633 mutex_unlock(&pl->state_mutex);
2644 * phylink_prepare_resume() - prepare to resume a network device
2649 * Prepare to resume a network device, preparing the PHY as necessary.
2653 struct phy_device *phydev = pl->phydev;
2659 * in order to resume. If the MAC requires RXC, and we have a PHY,
2660 * then resume the PHY. Note that 802.3 allows PHYs 500ms before
2663 if (pl->config->mac_requires_rxc && phydev && phydev->suspended)
2669 * phylink_resume() - handle a network device resume event
2682 if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
2683 /* Wake-on-Lan enabled, MAC handling */
2685 if (pl->suspend_link_up) {
2689 * to be printed during resume, which is harmless -
2693 mutex_lock(&pl->state_mutex);
2695 mutex_unlock(&pl->state_mutex);
2698 /* Re-apply the link parameters so that all the settings get
2699 * restored to the MAC.
2703 /* Re-enable and re-resolve the link parameters */
2712 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
2716 * Read the wake on lan parameters from the PHY attached to the phylink
2717 * instance specified by @pl. If no PHY is currently attached, report no
2724 wol->supported = 0;
2725 wol->wolopts = 0;
2728 if (phylink_phy_supports_wol(pl, pl->phydev))
2729 phy_ethtool_get_wol(pl->phydev, wol);
2731 /* Where the MAC augments the WoL support, merge its support and
2734 if (~wol->wolopts & pl->wolopts_mac & WAKE_MAGICSECURE)
2735 memcpy(wol->sopass, pl->wol_sopass,
2736 sizeof(wol->sopass));
2738 wol->supported |= pl->config->wol_mac_support;
2739 wol->wolopts |= pl->wolopts_mac;
2742 if (pl->phydev)
2743 phy_ethtool_get_wol(pl->phydev, wol);
2749 * phylink_ethtool_set_wol() - set wake on lan parameters
2753 * Set the wake on lan parameters for the PHY attached to the phylink
2754 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
2762 int ret = -EOPNOTSUPP;
2769 wolopts = wol->wolopts;
2771 if (phylink_phy_supports_wol(pl, pl->phydev)) {
2772 ret = phy_ethtool_set_wol(pl->phydev, wol);
2773 if (ret != 0 && ret != -EOPNOTSUPP)
2776 phy_ethtool_get_wol(pl->phydev, &w);
2778 /* Any Wake-on-Lan modes which the PHY is handling
2779 * should not be passed on to the MAC.
2784 wolopts &= pl->config->wol_mac_support;
2785 changed = pl->wolopts_mac != wolopts;
2787 changed |= !!memcmp(wol->sopass, pl->wol_sopass,
2788 sizeof(wol->sopass));
2789 memcpy(pl->wol_sopass, wol->sopass, sizeof(pl->wol_sopass));
2792 ret = pl->mac_ops->mac_wol_set(pl->config, wolopts,
2793 wol->sopass);
2795 pl->wolopts_mac = wolopts;
2800 if (pl->phydev)
2801 ret = phy_ethtool_set_wol(pl->phydev, wol);
2813 interface = sfp_select_interface(pl->sfp_bus, link_modes);
2822 if (!test_bit(interface, pl->config->supported_interfaces)) {
2824 "selection of interface failed, SFP selected %s (%u) but MAC supports %*pbl\n",
2827 pl->config->supported_interfaces);
2844 if (!test_bit(interface, pl->sfp_interfaces))
2881 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
2882 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
2883 if (kset->base.rate_matching == RATE_MATCH_NONE) {
2884 kset->base.speed = state->speed;
2885 kset->base.duplex = state->duplex;
2887 kset->base.autoneg = linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2888 state->advertising) ?
2893 * phylink_ethtool_ksettings_get() - get the current link settings
2898 * This will be the link settings read from the MAC, PHY or fixed link
2908 if (pl->phydev)
2909 phy_ethtool_ksettings_get(pl->phydev, kset);
2911 kset->base.port = pl->link_port;
2913 linkmode_copy(kset->link_modes.supported, pl->supported);
2915 switch (pl->act_link_an_mode) {
2918 * current link settings - and note that these also
2926 /* If there is a phy attached, then use the reported
2927 * settings from the phy with no modification.
2929 if (pl->phydev)
2934 /* The MAC is reporting the link results from its own PCS
2935 * layer via in-band status. Report these as the current
2967 * phylink_ethtool_ksettings_set() - set the link settings
2980 if (pl->phydev) {
2985 pl->supported);
2988 * to update the pl->link_config settings:
2989 * - the configuration returned via ksettings_get() will come
2990 * from phylib whenever a PHY is present.
2991 * - link_config.interface will be updated by the PHY calling
2993 * - initial link configuration for PHY mode comes from the
2994 * last phy state updated via phylink_phy_change().
2995 * - other configuration changes (e.g. pause modes) are
2997 * - if in in-band mode with a PHY, the link configuration
2998 * is passed on the link from the PHY, and all of
3000 * - the only possible use would be link_config.advertising
3001 * pause modes when in 1000base-X mode with a PHY, but in
3002 * the presence of a PHY, this should not be changed as that
3005 return phy_ethtool_ksettings_set(pl->phydev, &phy_kset);
3008 config = pl->link_config;
3010 linkmode_and(config.advertising, kset->link_modes.advertising,
3011 pl->supported);
3013 /* FIXME: should we reject autoneg if phy/mac does not support it? */
3014 switch (kset->base.autoneg) {
3019 c = phy_caps_lookup(kset->base.speed, kset->base.duplex,
3020 pl->supported, false);
3022 return -EINVAL;
3027 if (pl->req_link_an_mode == MLO_AN_FIXED) {
3028 if (c->speed != pl->link_config.speed ||
3029 c->duplex != pl->link_config.duplex)
3030 return -EINVAL;
3034 config.speed = c->speed;
3035 config.duplex = c->duplex;
3043 if (pl->req_link_an_mode == MLO_AN_FIXED) {
3045 pl->link_config.advertising))
3046 return -EINVAL;
3055 return -EINVAL;
3058 /* We have ruled out the case with a PHY attached, and the
3059 * fixed-link cases. All that is left are in-band links.
3062 kset->base.autoneg == AUTONEG_ENABLE);
3068 if (pl->sfp_bus) {
3069 if (kset->base.autoneg == AUTONEG_ENABLE)
3078 return -EINVAL;
3081 linkmode_copy(support, pl->supported);
3084 phylink_an_mode_str(pl->req_link_an_mode),
3087 return -EINVAL;
3091 linkmode_copy(support, pl->supported);
3093 return -EINVAL;
3100 return -EINVAL;
3102 /* Validate the autonegotiation state. We don't have a PHY in this
3103 * situation, so the PCS is the media-facing entity.
3107 return -EINVAL;
3109 mutex_lock(&pl->state_mutex);
3110 pl->link_config.speed = config.speed;
3111 pl->link_config.duplex = config.duplex;
3113 if (pl->link_config.interface != config.interface) {
3114 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
3116 if (pl->old_link_state) {
3118 pl->old_link_state = false;
3121 &pl->phylink_disable_state))
3123 pl->link_config.interface = config.interface;
3124 linkmode_copy(pl->link_config.advertising, config.advertising);
3125 } else if (!linkmode_equal(pl->link_config.advertising,
3127 linkmode_copy(pl->link_config.advertising, config.advertising);
3130 mutex_unlock(&pl->state_mutex);
3137 * phylink_ethtool_nway_reset() - restart negotiation
3141 * cause any attached phy to restart negotiation with the link partner, and
3142 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
3153 if (pl->phydev)
3154 ret = phy_restart_aneg(pl->phydev);
3162 * phylink_ethtool_get_pauseparam() - get the current pause parameters
3171 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
3172 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
3173 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
3178 * phylink_ethtool_set_pauseparam() - set the current pause parameters
3185 struct phylink_link_state *config = &pl->link_config;
3191 if (pl->req_link_an_mode == MLO_AN_FIXED)
3192 return -EOPNOTSUPP;
3194 if (!phylink_test(pl->supported, Pause) &&
3195 !phylink_test(pl->supported, Asym_Pause))
3196 return -EOPNOTSUPP;
3198 if (!phylink_test(pl->supported, Asym_Pause) &&
3199 pause->rx_pause != pause->tx_pause)
3200 return -EINVAL;
3203 if (pause->autoneg)
3205 if (pause->rx_pause)
3207 if (pause->tx_pause)
3210 mutex_lock(&pl->state_mutex);
3224 linkmode_set_pause(config->advertising, pause->tx_pause,
3225 pause->rx_pause);
3227 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
3229 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
3231 config->pause = pause_state;
3233 /* Update our in-band advertisement, triggering a renegotiation if
3236 if (!pl->phydev)
3239 mutex_unlock(&pl->state_mutex);
3241 /* If we have a PHY, a change of the pause frame advertisement will
3246 if (pl->phydev)
3247 phy_set_asym_pause(pl->phydev, pause->rx_pause,
3248 pause->tx_pause);
3255 pl->link_failed = true;
3264 * phylink_get_eee_err() - read the energy efficient ethernet error
3268 * Read the Energy Efficient Ethernet error counter from the PHY associated
3279 if (pl->phydev)
3280 ret = phy_get_eee_err(pl->phydev);
3287 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
3293 int ret = -EOPNOTSUPP;
3297 if (pl->mac_supports_eee_ops && !pl->mac_supports_eee)
3300 if (pl->phydev) {
3301 ret = phy_ethtool_get_eee(pl->phydev, eee);
3303 if (ret == 0 && pl->mac_supports_eee_ops)
3304 linkmode_and(eee->supported, eee->supported,
3305 pl->supported_lpi);
3313 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
3319 bool mac_eee = pl->mac_supports_eee;
3320 int ret = -EOPNOTSUPP;
3324 phylink_dbg(pl, "mac %s phylink EEE%s, adv %*pbl, LPI%s timer %uus\n",
3326 eee->eee_enabled ? ", enabled" : "",
3327 __ETHTOOL_LINK_MODE_MASK_NBITS, eee->advertised,
3328 eee->tx_lpi_enabled ? " enabled" : "", eee->tx_lpi_timer);
3330 if (pl->mac_supports_eee_ops && !mac_eee)
3333 if (pl->phydev) {
3335 if (pl->mac_supports_eee_ops)
3336 linkmode_and(eee->advertised, eee->advertised,
3337 pl->supported_lpi);
3338 ret = phy_ethtool_set_eee(pl->phydev, eee);
3340 eee_to_eeecfg(&pl->eee_cfg, eee);
3347 /* This emulates MII registers for a fixed-mode phy operating as per the
3356 unsigned long *lpa = state->lp_advertising;
3359 fs.link = state->link;
3360 fs.speed = state->speed;
3361 fs.duplex = state->duplex;
3367 if (!state->an_complete)
3376 struct phy_device *phydev = pl->phydev;
3382 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
3386 if (phydev->is_c45) {
3392 devad = __ffs(phydev->c45_ids.mmds_present);
3396 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
3397 return -EINVAL;
3405 return -EINVAL;
3408 return mdiobus_c45_read(pl->phydev->mdio.bus, prtad, devad,
3412 return mdiobus_read(pl->phydev->mdio.bus, phy_id, reg);
3418 struct phy_device *phydev = pl->phydev;
3424 return mdiobus_c45_write(pl->phydev->mdio.bus, prtad, devad,
3428 if (phydev->is_c45) {
3434 devad = __ffs(phydev->c45_ids.mmds_present);
3438 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
3439 return -EINVAL;
3447 return -EINVAL;
3449 return mdiobus_c45_write(pl->phydev->mdio.bus, phy_id, devad,
3453 return mdiobus_write(phydev->mdio.bus, phy_id, reg, val);
3462 switch (pl->act_link_an_mode) {
3471 return -EOPNOTSUPP;
3487 switch (pl->act_link_an_mode) {
3492 return -EOPNOTSUPP;
3502 * phylink_mii_ioctl() - generic mii ioctl interface
3507 * Perform the specified MII ioctl on the PHY attached to the phylink instance
3508 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
3513 * read register from the current PHY.
3515 * read register from the specified PHY.
3517 * set a register on the specified PHY.
3526 if (pl->phydev) {
3530 mii->phy_id = pl->phydev->mdio.addr;
3534 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
3536 mii->val_out = ret;
3542 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
3543 mii->val_in);
3547 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
3553 mii->phy_id = 0;
3557 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
3559 mii->val_out = ret;
3565 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
3566 mii->val_in);
3570 ret = -EOPNOTSUPP;
3580 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
3585 * If we have a PHY that is not part of a SFP module, then set the speed
3589 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
3597 if (!pl->sfp_bus && pl->phydev)
3598 ret = phy_speed_down(pl->phydev, sync);
3605 * phylink_speed_up() - restore the advertised speeds prior to the call to
3609 * If we have a PHY that is not part of a SFP module, then restore the
3610 * PHY speeds as per phy_speed_up().
3612 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
3620 if (!pl->sfp_bus && pl->phydev)
3621 ret = phy_speed_up(pl->phydev);
3631 pl->netdev->sfp_bus = bus;
3638 pl->netdev->sfp_bus = NULL;
3664 phylink_an_mode_str(mode), phy_modes(state->interface),
3667 if (!linkmode_equal(pl->supported, supported)) {
3668 linkmode_copy(pl->supported, supported);
3672 if (!linkmode_equal(pl->link_config.advertising, state->advertising)) {
3673 linkmode_copy(pl->link_config.advertising, state->advertising);
3677 if (pl->req_link_an_mode != mode ||
3678 pl->link_config.interface != state->interface) {
3679 pl->req_link_an_mode = mode;
3680 pl->link_config.interface = state->interface;
3686 phy_modes(state->interface));
3690 &pl->phylink_disable_state))
3694 static int phylink_sfp_config_phy(struct phylink *pl, struct phy_device *phy)
3700 /* We're not using pl->sfp_interfaces, so clear it. */
3701 phy_interface_zero(pl->sfp_interfaces);
3702 linkmode_copy(support, phy->supported);
3705 linkmode_copy(config.advertising, phy->advertising);
3711 /* Ignore errors if we're expecting a PHY to attach later */
3722 return -EINVAL;
3724 /* Attach the PHY so that the PHY is present when we do the major
3727 ret = phylink_attach_phy(pl, phy, config.interface);
3732 ret = phylink_bringup_phy(pl, phy, config.interface);
3734 phy_detach(phy);
3738 pl->link_port = pl->sfp_port;
3753 phylink_dbg(pl, "optical SFP: interfaces=[mac=%*pbl, sfp=%*pbl]\n",
3755 pl->config->supported_interfaces,
3757 pl->sfp_interfaces);
3759 /* Find the union of the supported interfaces by the PCS/MAC and
3762 phy_interface_and(pl->sfp_interfaces, pl->config->supported_interfaces,
3763 pl->sfp_interfaces);
3764 if (phy_interface_empty(pl->sfp_interfaces)) {
3766 return -EINVAL;
3770 linkmode_copy(support, pl->sfp_support);
3771 linkmode_copy(config.advertising, pl->sfp_support);
3779 ret = phylink_validate_mask(pl, NULL, pl->sfp_support, &config,
3780 pl->sfp_interfaces);
3787 interface = phylink_choose_sfp_interface(pl, pl->sfp_interfaces);
3790 return -EINVAL;
3803 if (phy_interface_weight(pl->sfp_interfaces) == 1) {
3805 pl->sfp_support);
3816 return -EINVAL;
3821 /* Ignore errors if we're expecting a PHY to attach later */
3830 pl->link_port = pl->sfp_port;
3832 phylink_sfp_set_config(pl, pl->sfp_support, &config, false);
3845 caps = sfp_get_module_caps(pl->sfp_bus);
3846 phy_interface_copy(pl->sfp_interfaces, caps->interfaces);
3847 linkmode_copy(pl->sfp_support, caps->link_modes);
3848 pl->sfp_may_have_phy = caps->may_have_phy;
3849 pl->sfp_port = caps->port;
3851 /* If this module may have a PHY connecting later, defer until later */
3852 if (pl->sfp_may_have_phy)
3862 phy_interface_zero(pl->sfp_interfaces);
3869 /* If this SFP module has a PHY, start the PHY now. */
3870 if (pl->phydev) {
3871 phy_start(pl->phydev);
3875 /* If the module may have a PHY but we didn't detect one we
3876 * need to configure the MAC here.
3878 if (!pl->sfp_may_have_phy)
3888 /* If this SFP module has a PHY, stop it. */
3889 if (pl->phydev)
3890 phy_stop(pl->phydev);
3911 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
3915 if (!phy->drv) {
3916 phylink_err(pl, "PHY %s (id 0x%.8lx) has no driver loaded\n",
3917 phydev_name(phy), (unsigned long)phy->phy_id);
3919 return -EINVAL;
3924 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
3925 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
3926 * using our validate call to the MAC, we rely upon the MAC
3929 phy_support_asym_pause(phy);
3931 /* Set the PHY's host supported interfaces */
3932 phy_interface_and(phy->host_interfaces, phylink_sfp_interfaces,
3933 pl->config->supported_interfaces);
3936 return phylink_sfp_config_phy(pl, phy);
3958 /* Helpers for MAC drivers */
3966 /* 100GBASE-KP4 and 100GBASE-CR10 not supported */
3971 /* 5GBASE-KR not supported */
3982 if (linkmode_test_bit(bit, state->advertising) &&
3983 linkmode_test_bit(bit, state->lp_advertising))
3988 state->speed = phylink_c73_priority_resolution[i].speed;
3989 state->duplex = DUPLEX_FULL;
3992 state->link = false;
4009 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
4011 if (linkmode_test_bit(fd_bit, state->advertising) &&
4012 linkmode_test_bit(fd_bit, state->lp_advertising)) {
4013 state->speed = speed;
4014 state->duplex = DUPLEX_FULL;
4017 state->link = false;
4027 state->link = false;
4033 state->speed = SPEED_10;
4036 state->speed = SPEED_100;
4039 state->speed = SPEED_1000;
4042 state->link = false;
4046 state->duplex = DUPLEX_FULL;
4048 state->duplex = DUPLEX_HALF;
4052 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
4054 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
4056 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
4065 state->speed = SPEED_10;
4068 state->speed = SPEED_100;
4071 state->speed = SPEED_1000;
4074 state->speed = SPEED_2500;
4077 state->speed = SPEED_5000;
4080 state->speed = SPEED_10000;
4083 state->link = false;
4088 state->duplex = DUPLEX_FULL;
4090 state->duplex = DUPLEX_HALF;
4095 * phylink_decode_usgmii_word() - decode the USGMII word from a MAC PCS
4097 * @lpa: a 16 bit value which stores the USGMII auto-negotiation word
4099 * Helper for MAC PCS supporting the USGMII protocol and the auto-negotiation
4110 state->speed = SPEED_10;
4113 state->speed = SPEED_100;
4116 state->speed = SPEED_1000;
4119 state->link = false;
4124 state->duplex = DUPLEX_FULL;
4126 state->duplex = DUPLEX_HALF;
4130 * phylink_mii_c22_pcs_decode_state() - Decode MAC PCS state from MII registers
4136 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4147 state->link = !!(bmsr & BMSR_LSTATUS);
4148 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
4151 if (!state->link)
4154 switch (state->interface) {
4159 state->speed = SPEED_1000;
4160 state->duplex = DUPLEX_FULL;
4161 state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX;
4169 state->speed = SPEED_2500;
4170 state->duplex = DUPLEX_FULL;
4171 state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX;
4187 state->link = false;
4194 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
4199 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4202 * Read the MAC PCS state from the MII device configured in @config and
4217 state->link = false;
4226 * phylink_mii_c22_pcs_encode_advertisement() - configure the clause 37 PCS
4228 * @interface: the PHY interface mode being configured
4231 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4237 * Return: The new value for @adv, or ``-EINVAL`` if it should not be changed.
4260 return -EINVAL;
4266 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
4268 * @interface: the PHY interface mode being configured
4272 * Configure a Clause 22 PCS PHY with the appropriate negotiation
4288 ret = mdiobus_modify_changed(pcs->bus, pcs->addr,
4310 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
4313 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
4335 struct mii_bus *bus = pcs->bus;
4336 int addr = pcs->addr;
4341 state->link = false;
4345 state->link = !!(stat & MDIO_STAT1_LSTATUS);
4346 if (!state->link)
4349 switch (state->interface) {
4351 state->speed = SPEED_10000;
4352 state->duplex = DUPLEX_FULL;
4373 MODULE_DESCRIPTION("phylink models the MAC to optional PHY connection");