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 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
137 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
139 phylink_set_port_modes(tmp);
140 phylink_set(tmp, Autoneg);
141 phylink_set(tmp, Pause);
142 phylink_set(tmp, Asym_Pause);
144 return linkmode_subset(linkmode, tmp);
147 static const char *phylink_an_mode_str(unsigned int mode)
149 static const char *modestr[] = {
150 [MLO_AN_PHY] = "phy",
151 [MLO_AN_FIXED] = "fixed",
152 [MLO_AN_INBAND] = "inband",
155 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
158 static int phylink_validate(struct phylink *pl, unsigned long *supported,
159 struct phylink_link_state *state)
161 pl->mac_ops->validate(pl->config, supported, state);
163 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
166 static int phylink_parse_fixedlink(struct phylink *pl,
167 struct fwnode_handle *fwnode)
169 struct fwnode_handle *fixed_node;
170 const struct phy_setting *s;
171 struct gpio_desc *desc;
175 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
177 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
179 pl->link_config.speed = speed;
180 pl->link_config.duplex = DUPLEX_HALF;
182 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
183 pl->link_config.duplex = DUPLEX_FULL;
185 /* We treat the "pause" and "asym-pause" terminology as
186 * defining the link partner's ability.
188 if (fwnode_property_read_bool(fixed_node, "pause"))
189 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
190 pl->link_config.lp_advertising);
191 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
192 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
193 pl->link_config.lp_advertising);
196 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
200 pl->link_gpio = desc;
201 else if (desc == ERR_PTR(-EPROBE_DEFER))
204 fwnode_handle_put(fixed_node);
211 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
213 if (ret != ARRAY_SIZE(prop)) {
214 phylink_err(pl, "broken fixed-link?\n");
218 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
219 prop, ARRAY_SIZE(prop));
221 pl->link_config.duplex = prop[1] ?
222 DUPLEX_FULL : DUPLEX_HALF;
223 pl->link_config.speed = prop[2];
225 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
226 pl->link_config.lp_advertising);
228 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
229 pl->link_config.lp_advertising);
233 if (pl->link_config.speed > SPEED_1000 &&
234 pl->link_config.duplex != DUPLEX_FULL)
235 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
236 pl->link_config.speed);
238 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
239 linkmode_copy(pl->link_config.advertising, pl->supported);
240 phylink_validate(pl, pl->supported, &pl->link_config);
242 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
243 pl->supported, true);
244 linkmode_zero(pl->supported);
245 phylink_set(pl->supported, MII);
246 phylink_set(pl->supported, Pause);
247 phylink_set(pl->supported, Asym_Pause);
248 phylink_set(pl->supported, Autoneg);
250 __set_bit(s->bit, pl->supported);
251 __set_bit(s->bit, pl->link_config.lp_advertising);
253 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
254 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
255 pl->link_config.speed);
258 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
261 pl->link_config.link = 1;
262 pl->link_config.an_complete = 1;
267 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
269 struct fwnode_handle *dn;
272 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
273 if (dn || fwnode_property_present(fwnode, "fixed-link"))
274 pl->cfg_link_an_mode = MLO_AN_FIXED;
275 fwnode_handle_put(dn);
277 if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
278 strcmp(managed, "in-band-status") == 0) ||
279 pl->config->ovr_an_inband) {
280 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
282 "can't use both fixed-link and in-band-status\n");
286 linkmode_zero(pl->supported);
287 phylink_set(pl->supported, MII);
288 phylink_set(pl->supported, Autoneg);
289 phylink_set(pl->supported, Asym_Pause);
290 phylink_set(pl->supported, Pause);
291 pl->link_config.an_enabled = true;
292 pl->cfg_link_an_mode = MLO_AN_INBAND;
294 switch (pl->link_config.interface) {
295 case PHY_INTERFACE_MODE_SGMII:
296 case PHY_INTERFACE_MODE_QSGMII:
297 phylink_set(pl->supported, 10baseT_Half);
298 phylink_set(pl->supported, 10baseT_Full);
299 phylink_set(pl->supported, 100baseT_Half);
300 phylink_set(pl->supported, 100baseT_Full);
301 phylink_set(pl->supported, 1000baseT_Half);
302 phylink_set(pl->supported, 1000baseT_Full);
305 case PHY_INTERFACE_MODE_1000BASEX:
306 phylink_set(pl->supported, 1000baseX_Full);
309 case PHY_INTERFACE_MODE_2500BASEX:
310 phylink_set(pl->supported, 2500baseX_Full);
313 case PHY_INTERFACE_MODE_5GBASER:
314 phylink_set(pl->supported, 5000baseT_Full);
317 case PHY_INTERFACE_MODE_25GBASER:
318 phylink_set(pl->supported, 25000baseCR_Full);
319 phylink_set(pl->supported, 25000baseKR_Full);
320 phylink_set(pl->supported, 25000baseSR_Full);
322 case PHY_INTERFACE_MODE_USXGMII:
323 case PHY_INTERFACE_MODE_10GKR:
324 case PHY_INTERFACE_MODE_10GBASER:
325 phylink_set(pl->supported, 10baseT_Half);
326 phylink_set(pl->supported, 10baseT_Full);
327 phylink_set(pl->supported, 100baseT_Half);
328 phylink_set(pl->supported, 100baseT_Full);
329 phylink_set(pl->supported, 1000baseT_Half);
330 phylink_set(pl->supported, 1000baseT_Full);
331 phylink_set(pl->supported, 1000baseX_Full);
332 phylink_set(pl->supported, 1000baseKX_Full);
333 phylink_set(pl->supported, 2500baseT_Full);
334 phylink_set(pl->supported, 2500baseX_Full);
335 phylink_set(pl->supported, 5000baseT_Full);
336 phylink_set(pl->supported, 10000baseT_Full);
337 phylink_set(pl->supported, 10000baseKR_Full);
338 phylink_set(pl->supported, 10000baseKX4_Full);
339 phylink_set(pl->supported, 10000baseCR_Full);
340 phylink_set(pl->supported, 10000baseSR_Full);
341 phylink_set(pl->supported, 10000baseLR_Full);
342 phylink_set(pl->supported, 10000baseLRM_Full);
343 phylink_set(pl->supported, 10000baseER_Full);
346 case PHY_INTERFACE_MODE_XLGMII:
347 phylink_set(pl->supported, 25000baseCR_Full);
348 phylink_set(pl->supported, 25000baseKR_Full);
349 phylink_set(pl->supported, 25000baseSR_Full);
350 phylink_set(pl->supported, 40000baseKR4_Full);
351 phylink_set(pl->supported, 40000baseCR4_Full);
352 phylink_set(pl->supported, 40000baseSR4_Full);
353 phylink_set(pl->supported, 40000baseLR4_Full);
354 phylink_set(pl->supported, 50000baseCR2_Full);
355 phylink_set(pl->supported, 50000baseKR2_Full);
356 phylink_set(pl->supported, 50000baseSR2_Full);
357 phylink_set(pl->supported, 50000baseKR_Full);
358 phylink_set(pl->supported, 50000baseSR_Full);
359 phylink_set(pl->supported, 50000baseCR_Full);
360 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
361 phylink_set(pl->supported, 50000baseDR_Full);
362 phylink_set(pl->supported, 100000baseKR4_Full);
363 phylink_set(pl->supported, 100000baseSR4_Full);
364 phylink_set(pl->supported, 100000baseCR4_Full);
365 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
366 phylink_set(pl->supported, 100000baseKR2_Full);
367 phylink_set(pl->supported, 100000baseSR2_Full);
368 phylink_set(pl->supported, 100000baseCR2_Full);
369 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
370 phylink_set(pl->supported, 100000baseDR2_Full);
375 "incorrect link mode %s for in-band status\n",
376 phy_modes(pl->link_config.interface));
380 linkmode_copy(pl->link_config.advertising, pl->supported);
382 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
384 "failed to validate link configuration for in-band status\n");
388 /* Check if MAC/PCS also supports Autoneg. */
389 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
395 static void phylink_apply_manual_flow(struct phylink *pl,
396 struct phylink_link_state *state)
398 /* If autoneg is disabled, pause AN is also disabled */
399 if (!state->an_enabled)
400 state->pause &= ~MLO_PAUSE_AN;
402 /* Manual configuration of pause modes */
403 if (!(pl->link_config.pause & MLO_PAUSE_AN))
404 state->pause = pl->link_config.pause;
407 static void phylink_resolve_flow(struct phylink_link_state *state)
409 bool tx_pause, rx_pause;
411 state->pause = MLO_PAUSE_NONE;
412 if (state->duplex == DUPLEX_FULL) {
413 linkmode_resolve_pause(state->advertising,
414 state->lp_advertising,
415 &tx_pause, &rx_pause);
417 state->pause |= MLO_PAUSE_TX;
419 state->pause |= MLO_PAUSE_RX;
423 static void phylink_mac_config(struct phylink *pl,
424 const struct phylink_link_state *state)
427 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
428 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
429 phy_modes(state->interface),
430 phy_speed_to_str(state->speed),
431 phy_duplex_to_str(state->duplex),
432 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
433 state->pause, state->link, state->an_enabled);
435 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
438 static void phylink_mac_pcs_an_restart(struct phylink *pl)
440 if (pl->link_config.an_enabled &&
441 phy_interface_mode_is_8023z(pl->link_config.interface) &&
442 phylink_autoneg_inband(pl->cur_link_an_mode)) {
444 pl->pcs_ops->pcs_an_restart(pl->pcs);
446 pl->mac_ops->mac_an_restart(pl->config);
450 static void phylink_major_config(struct phylink *pl, bool restart,
451 const struct phylink_link_state *state)
455 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
457 if (pl->mac_ops->mac_prepare) {
458 err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
461 phylink_err(pl, "mac_prepare failed: %pe\n",
467 phylink_mac_config(pl, state);
470 err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
473 !!(pl->link_config.pause &
476 phylink_err(pl, "pcs_config failed: %pe\n",
482 phylink_mac_pcs_an_restart(pl);
484 if (pl->mac_ops->mac_finish) {
485 err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
488 phylink_err(pl, "mac_finish failed: %pe\n",
494 * Reconfigure for a change of inband advertisement.
495 * If we have a separate PCS, we only need to call its pcs_config() method,
496 * and then restart AN if it indicates something changed. Otherwise, we do
497 * the full MAC reconfiguration.
499 static int phylink_change_inband_advert(struct phylink *pl)
503 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
508 phylink_mac_config(pl, &pl->link_config);
509 phylink_mac_pcs_an_restart(pl);
513 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
514 phylink_an_mode_str(pl->cur_link_an_mode),
515 phy_modes(pl->link_config.interface),
516 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
517 pl->link_config.pause);
519 /* Modern PCS-based method; update the advert at the PCS, and
520 * restart negotiation if the pcs_config() helper indicates that
521 * the programmed advertisement has changed.
523 ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
524 pl->link_config.interface,
525 pl->link_config.advertising,
526 !!(pl->link_config.pause & MLO_PAUSE_AN));
531 phylink_mac_pcs_an_restart(pl);
536 static void phylink_mac_pcs_get_state(struct phylink *pl,
537 struct phylink_link_state *state)
539 linkmode_copy(state->advertising, pl->link_config.advertising);
540 linkmode_zero(state->lp_advertising);
541 state->interface = pl->link_config.interface;
542 state->an_enabled = pl->link_config.an_enabled;
543 state->speed = SPEED_UNKNOWN;
544 state->duplex = DUPLEX_UNKNOWN;
545 state->pause = MLO_PAUSE_NONE;
546 state->an_complete = 0;
550 pl->pcs_ops->pcs_get_state(pl->pcs, state);
551 else if (pl->mac_ops->mac_pcs_get_state)
552 pl->mac_ops->mac_pcs_get_state(pl->config, state);
557 /* The fixed state is... fixed except for the link state,
558 * which may be determined by a GPIO or a callback.
560 static void phylink_get_fixed_state(struct phylink *pl,
561 struct phylink_link_state *state)
563 *state = pl->link_config;
564 if (pl->config->get_fixed_state)
565 pl->config->get_fixed_state(pl->config, state);
566 else if (pl->link_gpio)
567 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
569 phylink_resolve_flow(state);
572 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
574 struct phylink_link_state link_state;
576 switch (pl->cur_link_an_mode) {
578 link_state = pl->phy_state;
582 phylink_get_fixed_state(pl, &link_state);
586 link_state = pl->link_config;
587 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
588 link_state.pause = MLO_PAUSE_NONE;
591 default: /* can't happen */
595 link_state.link = false;
597 phylink_apply_manual_flow(pl, &link_state);
598 phylink_major_config(pl, force_restart, &link_state);
601 static const char *phylink_pause_to_str(int pause)
603 switch (pause & MLO_PAUSE_TXRX_MASK) {
604 case MLO_PAUSE_TX | MLO_PAUSE_RX:
615 static void phylink_link_up(struct phylink *pl,
616 struct phylink_link_state link_state)
618 struct net_device *ndev = pl->netdev;
620 pl->cur_interface = link_state.interface;
622 if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
623 pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
625 link_state.speed, link_state.duplex);
627 pl->mac_ops->mac_link_up(pl->config, pl->phydev,
628 pl->cur_link_an_mode, pl->cur_interface,
629 link_state.speed, link_state.duplex,
630 !!(link_state.pause & MLO_PAUSE_TX),
631 !!(link_state.pause & MLO_PAUSE_RX));
634 netif_carrier_on(ndev);
637 "Link is Up - %s/%s - flow control %s\n",
638 phy_speed_to_str(link_state.speed),
639 phy_duplex_to_str(link_state.duplex),
640 phylink_pause_to_str(link_state.pause));
643 static void phylink_link_down(struct phylink *pl)
645 struct net_device *ndev = pl->netdev;
648 netif_carrier_off(ndev);
649 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
651 phylink_info(pl, "Link is Down\n");
654 static void phylink_resolve(struct work_struct *w)
656 struct phylink *pl = container_of(w, struct phylink, resolve);
657 struct phylink_link_state link_state;
658 struct net_device *ndev = pl->netdev;
659 bool mac_config = false;
662 mutex_lock(&pl->state_mutex);
664 cur_link_state = netif_carrier_ok(ndev);
666 cur_link_state = pl->old_link_state;
668 if (pl->phylink_disable_state) {
669 pl->mac_link_dropped = false;
670 link_state.link = false;
671 } else if (pl->mac_link_dropped) {
672 link_state.link = false;
674 switch (pl->cur_link_an_mode) {
676 link_state = pl->phy_state;
677 phylink_apply_manual_flow(pl, &link_state);
678 mac_config = link_state.link;
682 phylink_get_fixed_state(pl, &link_state);
683 mac_config = link_state.link;
687 phylink_mac_pcs_get_state(pl, &link_state);
689 /* If we have a phy, the "up" state is the union of
690 * both the PHY and the MAC
693 link_state.link &= pl->phy_state.link;
695 /* Only update if the PHY link is up */
696 if (pl->phydev && pl->phy_state.link) {
697 link_state.interface = pl->phy_state.interface;
699 /* If we have a PHY, we need to update with
700 * the PHY flow control bits.
702 link_state.pause = pl->phy_state.pause;
705 phylink_apply_manual_flow(pl, &link_state);
711 if (link_state.interface != pl->link_config.interface) {
712 /* The interface has changed, force the link down and
715 if (cur_link_state) {
716 phylink_link_down(pl);
717 cur_link_state = false;
719 phylink_major_config(pl, false, &link_state);
720 pl->link_config.interface = link_state.interface;
721 } else if (!pl->pcs_ops) {
722 /* The interface remains unchanged, only the speed,
723 * duplex or pause settings have changed. Call the
724 * old mac_config() method to configure the MAC/PCS
725 * only if we do not have a PCS installed (an
728 phylink_mac_config(pl, &link_state);
732 if (link_state.link != cur_link_state) {
733 pl->old_link_state = link_state.link;
734 if (!link_state.link)
735 phylink_link_down(pl);
737 phylink_link_up(pl, link_state);
739 if (!link_state.link && pl->mac_link_dropped) {
740 pl->mac_link_dropped = false;
741 queue_work(system_power_efficient_wq, &pl->resolve);
743 mutex_unlock(&pl->state_mutex);
746 static void phylink_run_resolve(struct phylink *pl)
748 if (!pl->phylink_disable_state)
749 queue_work(system_power_efficient_wq, &pl->resolve);
752 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
754 unsigned long state = pl->phylink_disable_state;
756 set_bit(bit, &pl->phylink_disable_state);
758 queue_work(system_power_efficient_wq, &pl->resolve);
759 flush_work(&pl->resolve);
763 static void phylink_fixed_poll(struct timer_list *t)
765 struct phylink *pl = container_of(t, struct phylink, link_poll);
767 mod_timer(t, jiffies + HZ);
769 phylink_run_resolve(pl);
772 static const struct sfp_upstream_ops sfp_phylink_ops;
774 static int phylink_register_sfp(struct phylink *pl,
775 struct fwnode_handle *fwnode)
783 bus = sfp_bus_find_fwnode(fwnode);
786 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
792 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
799 * phylink_create() - create a phylink instance
800 * @config: a pointer to the target &struct phylink_config
801 * @fwnode: a pointer to a &struct fwnode_handle describing the network
803 * @iface: the desired link mode defined by &typedef phy_interface_t
804 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
806 * Create a new phylink instance, and parse the link parameters found in @np.
807 * This will parse in-band modes, fixed-link or SFP configuration.
809 * Note: the rtnl lock must not be held when calling this function.
811 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
812 * must use IS_ERR() to check for errors from this function.
814 struct phylink *phylink_create(struct phylink_config *config,
815 struct fwnode_handle *fwnode,
816 phy_interface_t iface,
817 const struct phylink_mac_ops *mac_ops)
822 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
824 return ERR_PTR(-ENOMEM);
826 mutex_init(&pl->state_mutex);
827 INIT_WORK(&pl->resolve, phylink_resolve);
830 if (config->type == PHYLINK_NETDEV) {
831 pl->netdev = to_net_dev(config->dev);
832 } else if (config->type == PHYLINK_DEV) {
833 pl->dev = config->dev;
836 return ERR_PTR(-EINVAL);
839 pl->phy_state.interface = iface;
840 pl->link_interface = iface;
841 if (iface == PHY_INTERFACE_MODE_MOCA)
842 pl->link_port = PORT_BNC;
844 pl->link_port = PORT_MII;
845 pl->link_config.interface = iface;
846 pl->link_config.pause = MLO_PAUSE_AN;
847 pl->link_config.speed = SPEED_UNKNOWN;
848 pl->link_config.duplex = DUPLEX_UNKNOWN;
849 pl->link_config.an_enabled = true;
850 pl->mac_ops = mac_ops;
851 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
852 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
854 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
855 linkmode_copy(pl->link_config.advertising, pl->supported);
856 phylink_validate(pl, pl->supported, &pl->link_config);
858 ret = phylink_parse_mode(pl, fwnode);
864 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
865 ret = phylink_parse_fixedlink(pl, fwnode);
872 pl->cur_link_an_mode = pl->cfg_link_an_mode;
874 ret = phylink_register_sfp(pl, fwnode);
882 EXPORT_SYMBOL_GPL(phylink_create);
885 * phylink_set_pcs() - set the current PCS for phylink to use
886 * @pl: a pointer to a &struct phylink returned from phylink_create()
887 * @pcs: a pointer to the &struct phylink_pcs
889 * Bind the MAC PCS to phylink. This may be called after phylink_create(),
890 * in mac_prepare() or mac_config() methods if it is desired to dynamically
893 * Please note that there are behavioural changes with the mac_config()
894 * callback if a PCS is present (denoting a newer setup) so removing a PCS
895 * is not supported, and if a PCS is going to be used, it must be registered
896 * by calling phylink_set_pcs() at the latest in the first mac_config() call.
898 void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs)
901 pl->pcs_ops = pcs->ops;
903 EXPORT_SYMBOL_GPL(phylink_set_pcs);
906 * phylink_destroy() - cleanup and destroy the phylink instance
907 * @pl: a pointer to a &struct phylink returned from phylink_create()
909 * Destroy a phylink instance. Any PHY that has been attached must have been
910 * cleaned up via phylink_disconnect_phy() prior to calling this function.
912 * Note: the rtnl lock must not be held when calling this function.
914 void phylink_destroy(struct phylink *pl)
916 sfp_bus_del_upstream(pl->sfp_bus);
918 gpiod_put(pl->link_gpio);
920 cancel_work_sync(&pl->resolve);
923 EXPORT_SYMBOL_GPL(phylink_destroy);
925 static void phylink_phy_change(struct phy_device *phydev, bool up)
927 struct phylink *pl = phydev->phylink;
928 bool tx_pause, rx_pause;
930 phy_get_pause(phydev, &tx_pause, &rx_pause);
932 mutex_lock(&pl->state_mutex);
933 pl->phy_state.speed = phydev->speed;
934 pl->phy_state.duplex = phydev->duplex;
935 pl->phy_state.pause = MLO_PAUSE_NONE;
937 pl->phy_state.pause |= MLO_PAUSE_TX;
939 pl->phy_state.pause |= MLO_PAUSE_RX;
940 pl->phy_state.interface = phydev->interface;
941 pl->phy_state.link = up;
942 mutex_unlock(&pl->state_mutex);
944 phylink_run_resolve(pl);
946 phylink_dbg(pl, "phy link %s %s/%s/%s/%s\n", up ? "up" : "down",
947 phy_modes(phydev->interface),
948 phy_speed_to_str(phydev->speed),
949 phy_duplex_to_str(phydev->duplex),
950 phylink_pause_to_str(pl->phy_state.pause));
953 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
954 phy_interface_t interface)
956 struct phylink_link_state config;
957 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
962 * This is the new way of dealing with flow control for PHYs,
963 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
964 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
965 * using our validate call to the MAC, we rely upon the MAC
966 * clearing the bits from both supported and advertising fields.
968 phy_support_asym_pause(phy);
970 memset(&config, 0, sizeof(config));
971 linkmode_copy(supported, phy->supported);
972 linkmode_copy(config.advertising, phy->advertising);
974 /* Clause 45 PHYs switch their Serdes lane between several different
975 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
976 * speeds. We really need to know which interface modes the PHY and
977 * MAC supports to properly work out which linkmodes can be supported.
980 interface != PHY_INTERFACE_MODE_RXAUI &&
981 interface != PHY_INTERFACE_MODE_XAUI &&
982 interface != PHY_INTERFACE_MODE_USXGMII)
983 config.interface = PHY_INTERFACE_MODE_NA;
985 config.interface = interface;
987 ret = phylink_validate(pl, supported, &config);
989 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
990 phy_modes(config.interface),
991 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
992 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
998 phy->phy_link_change = phylink_phy_change;
1000 irq_str = phy_attached_info_irq(phy);
1002 "PHY [%s] driver [%s] (irq=%s)\n",
1003 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1006 mutex_lock(&phy->lock);
1007 mutex_lock(&pl->state_mutex);
1009 pl->phy_state.interface = interface;
1010 pl->phy_state.pause = MLO_PAUSE_NONE;
1011 pl->phy_state.speed = SPEED_UNKNOWN;
1012 pl->phy_state.duplex = DUPLEX_UNKNOWN;
1013 linkmode_copy(pl->supported, supported);
1014 linkmode_copy(pl->link_config.advertising, config.advertising);
1016 /* Restrict the phy advertisement according to the MAC support. */
1017 linkmode_copy(phy->advertising, config.advertising);
1018 mutex_unlock(&pl->state_mutex);
1019 mutex_unlock(&phy->lock);
1022 "phy: setting supported %*pb advertising %*pb\n",
1023 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1024 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1026 if (phy_interrupt_is_valid(phy))
1027 phy_request_interrupt(phy);
1032 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1033 phy_interface_t interface)
1035 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1036 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1037 phy_interface_mode_is_8023z(interface))))
1043 return phy_attach_direct(pl->netdev, phy, 0, interface);
1047 * phylink_connect_phy() - connect a PHY to the phylink instance
1048 * @pl: a pointer to a &struct phylink returned from phylink_create()
1049 * @phy: a pointer to a &struct phy_device.
1051 * Connect @phy to the phylink instance specified by @pl by calling
1052 * phy_attach_direct(). Configure the @phy according to the MAC driver's
1053 * capabilities, start the PHYLIB state machine and enable any interrupts
1054 * that the PHY supports.
1056 * This updates the phylink's ethtool supported and advertising link mode
1059 * Returns 0 on success or a negative errno.
1061 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1065 /* Use PHY device/driver interface */
1066 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1067 pl->link_interface = phy->interface;
1068 pl->link_config.interface = pl->link_interface;
1071 ret = phylink_attach_phy(pl, phy, pl->link_interface);
1075 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1081 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1084 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1085 * @pl: a pointer to a &struct phylink returned from phylink_create()
1086 * @dn: a pointer to a &struct device_node.
1087 * @flags: PHY-specific flags to communicate to the PHY device driver
1089 * Connect the phy specified in the device node @dn to the phylink instance
1090 * specified by @pl. Actions specified in phylink_connect_phy() will be
1093 * Returns 0 on success or a negative errno.
1095 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1098 return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
1100 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1103 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
1104 * @pl: a pointer to a &struct phylink returned from phylink_create()
1105 * @fwnode: a pointer to a &struct fwnode_handle.
1106 * @flags: PHY-specific flags to communicate to the PHY device driver
1108 * Connect the phy specified @fwnode to the phylink instance specified
1111 * Returns 0 on success or a negative errno.
1113 int phylink_fwnode_phy_connect(struct phylink *pl,
1114 struct fwnode_handle *fwnode,
1117 struct fwnode_handle *phy_fwnode;
1118 struct phy_device *phy_dev;
1121 /* Fixed links and 802.3z are handled without needing a PHY */
1122 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1123 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1124 phy_interface_mode_is_8023z(pl->link_interface)))
1127 phy_fwnode = fwnode_get_phy_node(fwnode);
1128 if (IS_ERR(phy_fwnode)) {
1129 if (pl->cfg_link_an_mode == MLO_AN_PHY)
1134 phy_dev = fwnode_phy_find_device(phy_fwnode);
1135 /* We're done with the phy_node handle */
1136 fwnode_handle_put(phy_fwnode);
1140 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1141 pl->link_interface);
1143 phy_device_free(phy_dev);
1147 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1149 phy_detach(phy_dev);
1153 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
1156 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1158 * @pl: a pointer to a &struct phylink returned from phylink_create()
1160 * Disconnect any current PHY from the phylink instance described by @pl.
1162 void phylink_disconnect_phy(struct phylink *pl)
1164 struct phy_device *phy;
1170 mutex_lock(&phy->lock);
1171 mutex_lock(&pl->state_mutex);
1173 mutex_unlock(&pl->state_mutex);
1174 mutex_unlock(&phy->lock);
1175 flush_work(&pl->resolve);
1177 phy_disconnect(phy);
1180 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1183 * phylink_mac_change() - notify phylink of a change in MAC state
1184 * @pl: a pointer to a &struct phylink returned from phylink_create()
1185 * @up: indicates whether the link is currently up.
1187 * The MAC driver should call this driver when the state of its link
1188 * changes (eg, link failure, new negotiation results, etc.)
1190 void phylink_mac_change(struct phylink *pl, bool up)
1193 pl->mac_link_dropped = true;
1194 phylink_run_resolve(pl);
1195 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1197 EXPORT_SYMBOL_GPL(phylink_mac_change);
1199 static irqreturn_t phylink_link_handler(int irq, void *data)
1201 struct phylink *pl = data;
1203 phylink_run_resolve(pl);
1209 * phylink_start() - start a phylink instance
1210 * @pl: a pointer to a &struct phylink returned from phylink_create()
1212 * Start the phylink instance specified by @pl, configuring the MAC for the
1213 * desired link mode(s) and negotiation style. This should be called from the
1214 * network device driver's &struct net_device_ops ndo_open() method.
1216 void phylink_start(struct phylink *pl)
1222 phylink_info(pl, "configuring for %s/%s link mode\n",
1223 phylink_an_mode_str(pl->cur_link_an_mode),
1224 phy_modes(pl->link_config.interface));
1226 /* Always set the carrier off */
1228 netif_carrier_off(pl->netdev);
1230 /* Apply the link configuration to the MAC when starting. This allows
1231 * a fixed-link to start with the correct parameters, and also
1232 * ensures that we set the appropriate advertisement for Serdes links.
1234 * Restart autonegotiation if using 802.3z to ensure that the link
1235 * parameters are properly negotiated. This is necessary for DSA
1236 * switches using 802.3z negotiation to ensure they see our modes.
1238 phylink_mac_initial_config(pl, true);
1240 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1241 phylink_run_resolve(pl);
1243 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1244 int irq = gpiod_to_irq(pl->link_gpio);
1247 if (!request_irq(irq, phylink_link_handler,
1248 IRQF_TRIGGER_RISING |
1249 IRQF_TRIGGER_FALLING,
1259 switch (pl->cfg_link_an_mode) {
1261 poll |= pl->config->poll_fixed_state;
1264 poll |= pl->config->pcs_poll;
1266 poll |= pl->pcs->poll;
1270 mod_timer(&pl->link_poll, jiffies + HZ);
1272 phy_start(pl->phydev);
1274 sfp_upstream_start(pl->sfp_bus);
1276 EXPORT_SYMBOL_GPL(phylink_start);
1279 * phylink_stop() - stop a phylink instance
1280 * @pl: a pointer to a &struct phylink returned from phylink_create()
1282 * Stop the phylink instance specified by @pl. This should be called from the
1283 * network device driver's &struct net_device_ops ndo_stop() method. The
1284 * network device's carrier state should not be changed prior to calling this
1287 * This will synchronously bring down the link if the link is not already
1288 * down (in other words, it will trigger a mac_link_down() method call.)
1290 void phylink_stop(struct phylink *pl)
1295 sfp_upstream_stop(pl->sfp_bus);
1297 phy_stop(pl->phydev);
1298 del_timer_sync(&pl->link_poll);
1300 free_irq(pl->link_irq, pl);
1304 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1306 EXPORT_SYMBOL_GPL(phylink_stop);
1309 * phylink_suspend() - handle a network device suspend event
1310 * @pl: a pointer to a &struct phylink returned from phylink_create()
1311 * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan
1313 * Handle a network device suspend event. There are several cases:
1314 * - If Wake-on-Lan is not active, we can bring down the link between
1315 * the MAC and PHY by calling phylink_stop().
1316 * - If Wake-on-Lan is active, and being handled only by the PHY, we
1317 * can also bring down the link between the MAC and PHY.
1318 * - If Wake-on-Lan is active, but being handled by the MAC, the MAC
1319 * still needs to receive packets, so we can not bring the link down.
1321 void phylink_suspend(struct phylink *pl, bool mac_wol)
1325 if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) {
1326 /* Wake-on-Lan enabled, MAC handling */
1327 mutex_lock(&pl->state_mutex);
1329 /* Stop the resolver bringing the link up */
1330 __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1332 /* Disable the carrier, to prevent transmit timeouts,
1333 * but one would hope all packets have been sent. This
1334 * also means phylink_resolve() will do nothing.
1336 netif_carrier_off(pl->netdev);
1338 /* We do not call mac_link_down() here as we want the
1339 * link to remain up to receive the WoL packets.
1341 mutex_unlock(&pl->state_mutex);
1346 EXPORT_SYMBOL_GPL(phylink_suspend);
1349 * phylink_resume() - handle a network device resume event
1350 * @pl: a pointer to a &struct phylink returned from phylink_create()
1352 * Undo the effects of phylink_suspend(), returning the link to an
1353 * operational state.
1355 void phylink_resume(struct phylink *pl)
1359 if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) {
1360 /* Wake-on-Lan enabled, MAC handling */
1362 /* Call mac_link_down() so we keep the overall state balanced.
1363 * Do this under the state_mutex lock for consistency. This
1364 * will cause a "Link Down" message to be printed during
1365 * resume, which is harmless - the true link state will be
1366 * printed when we run a resolve.
1368 mutex_lock(&pl->state_mutex);
1369 phylink_link_down(pl);
1370 mutex_unlock(&pl->state_mutex);
1372 /* Re-apply the link parameters so that all the settings get
1373 * restored to the MAC.
1375 phylink_mac_initial_config(pl, true);
1377 /* Re-enable and re-resolve the link parameters */
1378 clear_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state);
1379 phylink_run_resolve(pl);
1384 EXPORT_SYMBOL_GPL(phylink_resume);
1387 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1388 * @pl: a pointer to a &struct phylink returned from phylink_create()
1389 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1391 * Read the wake on lan parameters from the PHY attached to the phylink
1392 * instance specified by @pl. If no PHY is currently attached, report no
1393 * support for wake on lan.
1395 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1403 phy_ethtool_get_wol(pl->phydev, wol);
1405 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1408 * phylink_ethtool_set_wol() - set wake on lan parameters
1409 * @pl: a pointer to a &struct phylink returned from phylink_create()
1410 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1412 * Set the wake on lan parameters for the PHY attached to the phylink
1413 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1416 * Returns zero on success or negative errno code.
1418 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1420 int ret = -EOPNOTSUPP;
1425 ret = phy_ethtool_set_wol(pl->phydev, wol);
1429 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1431 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1433 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1435 linkmode_zero(mask);
1436 phylink_set_port_modes(mask);
1438 linkmode_and(dst, dst, mask);
1439 linkmode_or(dst, dst, b);
1442 static void phylink_get_ksettings(const struct phylink_link_state *state,
1443 struct ethtool_link_ksettings *kset)
1445 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1446 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1447 kset->base.speed = state->speed;
1448 kset->base.duplex = state->duplex;
1449 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1454 * phylink_ethtool_ksettings_get() - get the current link settings
1455 * @pl: a pointer to a &struct phylink returned from phylink_create()
1456 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1458 * Read the current link settings for the phylink instance specified by @pl.
1459 * This will be the link settings read from the MAC, PHY or fixed link
1460 * settings depending on the current negotiation mode.
1462 int phylink_ethtool_ksettings_get(struct phylink *pl,
1463 struct ethtool_link_ksettings *kset)
1465 struct phylink_link_state link_state;
1470 phy_ethtool_ksettings_get(pl->phydev, kset);
1472 kset->base.port = pl->link_port;
1474 linkmode_copy(kset->link_modes.supported, pl->supported);
1476 switch (pl->cur_link_an_mode) {
1478 /* We are using fixed settings. Report these as the
1479 * current link settings - and note that these also
1480 * represent the supported speeds/duplex/pause modes.
1482 phylink_get_fixed_state(pl, &link_state);
1483 phylink_get_ksettings(&link_state, kset);
1487 /* If there is a phy attached, then use the reported
1488 * settings from the phy with no modification.
1493 phylink_mac_pcs_get_state(pl, &link_state);
1495 /* The MAC is reporting the link results from its own PCS
1496 * layer via in-band status. Report these as the current
1499 phylink_get_ksettings(&link_state, kset);
1505 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1508 * phylink_ethtool_ksettings_set() - set the link settings
1509 * @pl: a pointer to a &struct phylink returned from phylink_create()
1510 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1512 int phylink_ethtool_ksettings_set(struct phylink *pl,
1513 const struct ethtool_link_ksettings *kset)
1515 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1516 struct phylink_link_state config;
1517 const struct phy_setting *s;
1522 /* We can rely on phylib for this update; we also do not need
1523 * to update the pl->link_config settings:
1524 * - the configuration returned via ksettings_get() will come
1525 * from phylib whenever a PHY is present.
1526 * - link_config.interface will be updated by the PHY calling
1527 * back via phylink_phy_change() and a subsequent resolve.
1528 * - initial link configuration for PHY mode comes from the
1529 * last phy state updated via phylink_phy_change().
1530 * - other configuration changes (e.g. pause modes) are
1531 * performed directly via phylib.
1532 * - if in in-band mode with a PHY, the link configuration
1533 * is passed on the link from the PHY, and all of
1534 * link_config.{speed,duplex,an_enabled,pause} are not used.
1535 * - the only possible use would be link_config.advertising
1536 * pause modes when in 1000base-X mode with a PHY, but in
1537 * the presence of a PHY, this should not be changed as that
1538 * should be determined from the media side advertisement.
1540 return phy_ethtool_ksettings_set(pl->phydev, kset);
1543 config = pl->link_config;
1545 /* Mask out unsupported advertisements */
1546 linkmode_and(config.advertising, kset->link_modes.advertising,
1549 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1550 switch (kset->base.autoneg) {
1551 case AUTONEG_DISABLE:
1552 /* Autonegotiation disabled, select a suitable speed and
1555 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1556 pl->supported, false);
1560 /* If we have a fixed link, refuse to change link parameters.
1561 * If the link parameters match, accept them but do nothing.
1563 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1564 if (s->speed != pl->link_config.speed ||
1565 s->duplex != pl->link_config.duplex)
1570 config.speed = s->speed;
1571 config.duplex = s->duplex;
1574 case AUTONEG_ENABLE:
1575 /* If we have a fixed link, allow autonegotiation (since that
1576 * is our default case) but do not allow the advertisement to
1577 * be changed. If the advertisement matches, simply return.
1579 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1580 if (!linkmode_equal(config.advertising,
1581 pl->link_config.advertising))
1586 config.speed = SPEED_UNKNOWN;
1587 config.duplex = DUPLEX_UNKNOWN;
1594 /* We have ruled out the case with a PHY attached, and the
1595 * fixed-link cases. All that is left are in-band links.
1597 config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
1598 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
1601 /* Validate without changing the current supported mask. */
1602 linkmode_copy(support, pl->supported);
1603 if (phylink_validate(pl, support, &config))
1606 /* If autonegotiation is enabled, we must have an advertisement */
1607 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1610 /* If this link is with an SFP, ensure that changes to advertised modes
1611 * also cause the associated interface to be selected such that the
1612 * link can be configured correctly.
1614 if (pl->sfp_port && pl->sfp_bus) {
1615 config.interface = sfp_select_interface(pl->sfp_bus,
1616 config.advertising);
1617 if (config.interface == PHY_INTERFACE_MODE_NA) {
1619 "selection of interface failed, advertisement %*pb\n",
1620 __ETHTOOL_LINK_MODE_MASK_NBITS,
1621 config.advertising);
1625 /* Revalidate with the selected interface */
1626 linkmode_copy(support, pl->supported);
1627 if (phylink_validate(pl, support, &config)) {
1628 phylink_err(pl, "validation of %s/%s with support %*pb failed\n",
1629 phylink_an_mode_str(pl->cur_link_an_mode),
1630 phy_modes(config.interface),
1631 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1636 mutex_lock(&pl->state_mutex);
1637 pl->link_config.speed = config.speed;
1638 pl->link_config.duplex = config.duplex;
1639 pl->link_config.an_enabled = config.an_enabled;
1641 if (pl->link_config.interface != config.interface) {
1642 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
1643 /* We need to force the link down, then change the interface */
1644 if (pl->old_link_state) {
1645 phylink_link_down(pl);
1646 pl->old_link_state = false;
1648 if (!test_bit(PHYLINK_DISABLE_STOPPED,
1649 &pl->phylink_disable_state))
1650 phylink_major_config(pl, false, &config);
1651 pl->link_config.interface = config.interface;
1652 linkmode_copy(pl->link_config.advertising, config.advertising);
1653 } else if (!linkmode_equal(pl->link_config.advertising,
1654 config.advertising)) {
1655 linkmode_copy(pl->link_config.advertising, config.advertising);
1656 phylink_change_inband_advert(pl);
1658 mutex_unlock(&pl->state_mutex);
1662 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1665 * phylink_ethtool_nway_reset() - restart negotiation
1666 * @pl: a pointer to a &struct phylink returned from phylink_create()
1668 * Restart negotiation for the phylink instance specified by @pl. This will
1669 * cause any attached phy to restart negotiation with the link partner, and
1670 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1673 * Returns zero on success, or negative error code.
1675 int phylink_ethtool_nway_reset(struct phylink *pl)
1682 ret = phy_restart_aneg(pl->phydev);
1683 phylink_mac_pcs_an_restart(pl);
1687 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1690 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1691 * @pl: a pointer to a &struct phylink returned from phylink_create()
1692 * @pause: a pointer to a &struct ethtool_pauseparam
1694 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1695 struct ethtool_pauseparam *pause)
1699 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1700 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1701 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1703 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1706 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1707 * @pl: a pointer to a &struct phylink returned from phylink_create()
1708 * @pause: a pointer to a &struct ethtool_pauseparam
1710 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1711 struct ethtool_pauseparam *pause)
1713 struct phylink_link_state *config = &pl->link_config;
1714 bool manual_changed;
1719 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1722 if (!phylink_test(pl->supported, Pause) &&
1723 !phylink_test(pl->supported, Asym_Pause))
1726 if (!phylink_test(pl->supported, Asym_Pause) &&
1727 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1732 pause_state |= MLO_PAUSE_AN;
1733 if (pause->rx_pause)
1734 pause_state |= MLO_PAUSE_RX;
1735 if (pause->tx_pause)
1736 pause_state |= MLO_PAUSE_TX;
1738 mutex_lock(&pl->state_mutex);
1740 * See the comments for linkmode_set_pause(), wrt the deficiencies
1741 * with the current implementation. A solution to this issue would
1743 * ethtool Local device
1744 * rx tx Pause AsymDir
1749 * and then use the ethtool rx/tx enablement status to mask the
1750 * rx/tx pause resolution.
1752 linkmode_set_pause(config->advertising, pause->tx_pause,
1755 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1756 (!(pause_state & MLO_PAUSE_AN) &&
1757 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1759 config->pause = pause_state;
1761 /* Update our in-band advertisement, triggering a renegotiation if
1762 * the advertisement changed.
1765 phylink_change_inband_advert(pl);
1767 mutex_unlock(&pl->state_mutex);
1769 /* If we have a PHY, a change of the pause frame advertisement will
1770 * cause phylib to renegotiate (if AN is enabled) which will in turn
1771 * call our phylink_phy_change() and trigger a resolve. Note that
1772 * we can't hold our state mutex while calling phy_set_asym_pause().
1775 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1778 /* If the manual pause settings changed, make sure we trigger a
1779 * resolve to update their state; we can not guarantee that the
1782 if (manual_changed) {
1783 pl->mac_link_dropped = true;
1784 phylink_run_resolve(pl);
1789 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1792 * phylink_get_eee_err() - read the energy efficient ethernet error
1794 * @pl: a pointer to a &struct phylink returned from phylink_create().
1796 * Read the Energy Efficient Ethernet error counter from the PHY associated
1797 * with the phylink instance specified by @pl.
1799 * Returns positive error counter value, or negative error code.
1801 int phylink_get_eee_err(struct phylink *pl)
1808 ret = phy_get_eee_err(pl->phydev);
1812 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1815 * phylink_init_eee() - init and check the EEE features
1816 * @pl: a pointer to a &struct phylink returned from phylink_create()
1817 * @clk_stop_enable: allow PHY to stop receive clock
1819 * Must be called either with RTNL held or within mac_link_up()
1821 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1823 int ret = -EOPNOTSUPP;
1826 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1830 EXPORT_SYMBOL_GPL(phylink_init_eee);
1833 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1834 * @pl: a pointer to a &struct phylink returned from phylink_create()
1835 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1837 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1839 int ret = -EOPNOTSUPP;
1844 ret = phy_ethtool_get_eee(pl->phydev, eee);
1848 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1851 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1852 * @pl: a pointer to a &struct phylink returned from phylink_create()
1853 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1855 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1857 int ret = -EOPNOTSUPP;
1862 ret = phy_ethtool_set_eee(pl->phydev, eee);
1866 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1868 /* This emulates MII registers for a fixed-mode phy operating as per the
1869 * passed in state. "aneg" defines if we report negotiation is possible.
1871 * FIXME: should deal with negotiation state too.
1873 static int phylink_mii_emul_read(unsigned int reg,
1874 struct phylink_link_state *state)
1876 struct fixed_phy_status fs;
1877 unsigned long *lpa = state->lp_advertising;
1880 fs.link = state->link;
1881 fs.speed = state->speed;
1882 fs.duplex = state->duplex;
1883 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1884 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
1886 val = swphy_read_reg(reg, &fs);
1887 if (reg == MII_BMSR) {
1888 if (!state->an_complete)
1889 val &= ~BMSR_ANEGCOMPLETE;
1894 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1897 struct phy_device *phydev = pl->phydev;
1900 if (mdio_phy_id_is_c45(phy_id)) {
1901 prtad = mdio_phy_id_prtad(phy_id);
1902 devad = mdio_phy_id_devad(phy_id);
1903 devad = mdiobus_c45_addr(devad, reg);
1904 } else if (phydev->is_c45) {
1910 devad = __ffs(phydev->c45_ids.mmds_present);
1914 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1916 devad = MDIO_MMD_AN;
1917 if (reg == MII_ADVERTISE)
1918 reg = MDIO_AN_ADVERTISE;
1926 devad = mdiobus_c45_addr(devad, reg);
1931 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1934 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1935 unsigned int reg, unsigned int val)
1937 struct phy_device *phydev = pl->phydev;
1940 if (mdio_phy_id_is_c45(phy_id)) {
1941 prtad = mdio_phy_id_prtad(phy_id);
1942 devad = mdio_phy_id_devad(phy_id);
1943 devad = mdiobus_c45_addr(devad, reg);
1944 } else if (phydev->is_c45) {
1950 devad = __ffs(phydev->c45_ids.mmds_present);
1954 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1956 devad = MDIO_MMD_AN;
1957 if (reg == MII_ADVERTISE)
1958 reg = MDIO_AN_ADVERTISE;
1966 devad = mdiobus_c45_addr(devad, reg);
1972 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1975 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1978 struct phylink_link_state state;
1981 switch (pl->cur_link_an_mode) {
1984 phylink_get_fixed_state(pl, &state);
1985 val = phylink_mii_emul_read(reg, &state);
1994 phylink_mac_pcs_get_state(pl, &state);
1995 val = phylink_mii_emul_read(reg, &state);
2000 return val & 0xffff;
2003 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
2004 unsigned int reg, unsigned int val)
2006 switch (pl->cur_link_an_mode) {
2021 * phylink_mii_ioctl() - generic mii ioctl interface
2022 * @pl: a pointer to a &struct phylink returned from phylink_create()
2023 * @ifr: a pointer to a &struct ifreq for socket ioctls
2024 * @cmd: ioctl cmd to execute
2026 * Perform the specified MII ioctl on the PHY attached to the phylink instance
2027 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
2029 * Returns: zero on success or negative error code.
2032 * read register from the current PHY.
2034 * read register from the specified PHY.
2036 * set a register on the specified PHY.
2038 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
2040 struct mii_ioctl_data *mii = if_mii(ifr);
2046 /* PHYs only exist for MLO_AN_PHY and SGMII */
2049 mii->phy_id = pl->phydev->mdio.addr;
2053 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
2061 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
2066 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
2076 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
2084 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
2096 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2099 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2101 * @pl: a pointer to a &struct phylink returned from phylink_create()
2102 * @sync: perform action synchronously
2104 * If we have a PHY that is not part of a SFP module, then set the speed
2105 * as described in the phy_speed_down() function. Please see this function
2106 * for a description of the @sync parameter.
2108 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2110 int phylink_speed_down(struct phylink *pl, bool sync)
2116 if (!pl->sfp_bus && pl->phydev)
2117 ret = phy_speed_down(pl->phydev, sync);
2121 EXPORT_SYMBOL_GPL(phylink_speed_down);
2124 * phylink_speed_up() - restore the advertised speeds prior to the call to
2125 * phylink_speed_down()
2126 * @pl: a pointer to a &struct phylink returned from phylink_create()
2128 * If we have a PHY that is not part of a SFP module, then restore the
2129 * PHY speeds as per phy_speed_up().
2131 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2133 int phylink_speed_up(struct phylink *pl)
2139 if (!pl->sfp_bus && pl->phydev)
2140 ret = phy_speed_up(pl->phydev);
2144 EXPORT_SYMBOL_GPL(phylink_speed_up);
2146 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2148 struct phylink *pl = upstream;
2150 pl->netdev->sfp_bus = bus;
2153 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2155 struct phylink *pl = upstream;
2157 pl->netdev->sfp_bus = NULL;
2160 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2161 const unsigned long *supported,
2162 const unsigned long *advertising)
2164 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2165 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2166 struct phylink_link_state config;
2167 phy_interface_t iface;
2171 linkmode_copy(support, supported);
2173 memset(&config, 0, sizeof(config));
2174 linkmode_copy(config.advertising, advertising);
2175 config.interface = PHY_INTERFACE_MODE_NA;
2176 config.speed = SPEED_UNKNOWN;
2177 config.duplex = DUPLEX_UNKNOWN;
2178 config.pause = MLO_PAUSE_AN;
2179 config.an_enabled = pl->link_config.an_enabled;
2181 /* Ignore errors if we're expecting a PHY to attach later */
2182 ret = phylink_validate(pl, support, &config);
2184 phylink_err(pl, "validation with support %*pb failed: %d\n",
2185 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2189 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2190 if (iface == PHY_INTERFACE_MODE_NA) {
2192 "selection of interface failed, advertisement %*pb\n",
2193 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2197 config.interface = iface;
2198 linkmode_copy(support1, support);
2199 ret = phylink_validate(pl, support1, &config);
2201 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
2202 phylink_an_mode_str(mode),
2203 phy_modes(config.interface),
2204 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2208 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2209 phylink_an_mode_str(mode), phy_modes(config.interface),
2210 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2212 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2215 changed = !linkmode_equal(pl->supported, support) ||
2216 !linkmode_equal(pl->link_config.advertising,
2217 config.advertising);
2219 linkmode_copy(pl->supported, support);
2220 linkmode_copy(pl->link_config.advertising, config.advertising);
2223 if (pl->cur_link_an_mode != mode ||
2224 pl->link_config.interface != config.interface) {
2225 pl->link_config.interface = config.interface;
2226 pl->cur_link_an_mode = mode;
2230 phylink_info(pl, "switched to %s/%s link mode\n",
2231 phylink_an_mode_str(mode),
2232 phy_modes(config.interface));
2235 pl->link_port = pl->sfp_port;
2237 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2238 &pl->phylink_disable_state))
2239 phylink_mac_initial_config(pl, false);
2244 static int phylink_sfp_module_insert(void *upstream,
2245 const struct sfp_eeprom_id *id)
2247 struct phylink *pl = upstream;
2248 unsigned long *support = pl->sfp_support;
2252 linkmode_zero(support);
2253 sfp_parse_support(pl->sfp_bus, id, support);
2254 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2256 /* If this module may have a PHY connecting later, defer until later */
2257 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2258 if (pl->sfp_may_have_phy)
2261 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2264 static int phylink_sfp_module_start(void *upstream)
2266 struct phylink *pl = upstream;
2268 /* If this SFP module has a PHY, start the PHY now. */
2270 phy_start(pl->phydev);
2274 /* If the module may have a PHY but we didn't detect one we
2275 * need to configure the MAC here.
2277 if (!pl->sfp_may_have_phy)
2280 return phylink_sfp_config(pl, MLO_AN_INBAND,
2281 pl->sfp_support, pl->sfp_support);
2284 static void phylink_sfp_module_stop(void *upstream)
2286 struct phylink *pl = upstream;
2288 /* If this SFP module has a PHY, stop it. */
2290 phy_stop(pl->phydev);
2293 static void phylink_sfp_link_down(void *upstream)
2295 struct phylink *pl = upstream;
2299 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2302 static void phylink_sfp_link_up(void *upstream)
2304 struct phylink *pl = upstream;
2308 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2309 phylink_run_resolve(pl);
2312 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2313 * or 802.3z control word, so inband will not work.
2315 static bool phylink_phy_no_inband(struct phy_device *phy)
2317 return phy->is_c45 &&
2318 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2321 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2323 struct phylink *pl = upstream;
2324 phy_interface_t interface;
2329 * This is the new way of dealing with flow control for PHYs,
2330 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2331 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2332 * using our validate call to the MAC, we rely upon the MAC
2333 * clearing the bits from both supported and advertising fields.
2335 phy_support_asym_pause(phy);
2337 if (phylink_phy_no_inband(phy))
2340 mode = MLO_AN_INBAND;
2342 /* Do the initial configuration */
2343 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2347 interface = pl->link_config.interface;
2348 ret = phylink_attach_phy(pl, phy, interface);
2352 ret = phylink_bringup_phy(pl, phy, interface);
2359 static void phylink_sfp_disconnect_phy(void *upstream)
2361 phylink_disconnect_phy(upstream);
2364 static const struct sfp_upstream_ops sfp_phylink_ops = {
2365 .attach = phylink_sfp_attach,
2366 .detach = phylink_sfp_detach,
2367 .module_insert = phylink_sfp_module_insert,
2368 .module_start = phylink_sfp_module_start,
2369 .module_stop = phylink_sfp_module_stop,
2370 .link_up = phylink_sfp_link_up,
2371 .link_down = phylink_sfp_link_down,
2372 .connect_phy = phylink_sfp_connect_phy,
2373 .disconnect_phy = phylink_sfp_disconnect_phy,
2376 /* Helpers for MAC drivers */
2379 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2380 * @state: a pointer to a &struct phylink_link_state
2382 * Inspect the interface mode, advertising mask or forced speed and
2383 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2384 * the interface mode to suit. @state->interface is appropriately
2385 * updated, and the advertising mask has the "other" baseX_Full flag
2388 void phylink_helper_basex_speed(struct phylink_link_state *state)
2390 if (phy_interface_mode_is_8023z(state->interface)) {
2391 bool want_2500 = state->an_enabled ?
2392 phylink_test(state->advertising, 2500baseX_Full) :
2393 state->speed == SPEED_2500;
2396 phylink_clear(state->advertising, 1000baseX_Full);
2397 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2399 phylink_clear(state->advertising, 2500baseX_Full);
2400 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2404 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2406 static void phylink_decode_c37_word(struct phylink_link_state *state,
2407 uint16_t config_reg, int speed)
2409 bool tx_pause, rx_pause;
2412 if (speed == SPEED_2500)
2413 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2415 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2417 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2419 if (linkmode_test_bit(fd_bit, state->advertising) &&
2420 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2421 state->speed = speed;
2422 state->duplex = DUPLEX_FULL;
2424 /* negotiation failure */
2425 state->link = false;
2428 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2429 &tx_pause, &rx_pause);
2432 state->pause |= MLO_PAUSE_TX;
2434 state->pause |= MLO_PAUSE_RX;
2437 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2438 uint16_t config_reg)
2440 if (!(config_reg & LPA_SGMII_LINK)) {
2441 state->link = false;
2445 switch (config_reg & LPA_SGMII_SPD_MASK) {
2447 state->speed = SPEED_10;
2450 state->speed = SPEED_100;
2452 case LPA_SGMII_1000:
2453 state->speed = SPEED_1000;
2456 state->link = false;
2459 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2460 state->duplex = DUPLEX_FULL;
2462 state->duplex = DUPLEX_HALF;
2466 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2467 * @state: a pointer to a struct phylink_link_state.
2468 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2470 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2471 * code word. Decode the USXGMII code word and populate the corresponding fields
2472 * (speed, duplex) into the phylink_link_state structure.
2474 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2477 switch (lpa & MDIO_USXGMII_SPD_MASK) {
2478 case MDIO_USXGMII_10:
2479 state->speed = SPEED_10;
2481 case MDIO_USXGMII_100:
2482 state->speed = SPEED_100;
2484 case MDIO_USXGMII_1000:
2485 state->speed = SPEED_1000;
2487 case MDIO_USXGMII_2500:
2488 state->speed = SPEED_2500;
2490 case MDIO_USXGMII_5000:
2491 state->speed = SPEED_5000;
2493 case MDIO_USXGMII_10G:
2494 state->speed = SPEED_10000;
2497 state->link = false;
2501 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2502 state->duplex = DUPLEX_FULL;
2504 state->duplex = DUPLEX_HALF;
2506 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2509 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2510 * @pcs: a pointer to a &struct mdio_device.
2511 * @state: a pointer to a &struct phylink_link_state.
2513 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2514 * clause 37 negotiation and/or SGMII control.
2516 * Read the MAC PCS state from the MII device configured in @config and
2517 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2518 * the phylink @state structure. This is suitable to be directly plugged
2519 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2522 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2523 struct phylink_link_state *state)
2525 struct mii_bus *bus = pcs->bus;
2526 int addr = pcs->addr;
2529 bmsr = mdiobus_read(bus, addr, MII_BMSR);
2530 lpa = mdiobus_read(bus, addr, MII_LPA);
2531 if (bmsr < 0 || lpa < 0) {
2532 state->link = false;
2536 state->link = !!(bmsr & BMSR_LSTATUS);
2537 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2541 switch (state->interface) {
2542 case PHY_INTERFACE_MODE_1000BASEX:
2543 phylink_decode_c37_word(state, lpa, SPEED_1000);
2546 case PHY_INTERFACE_MODE_2500BASEX:
2547 phylink_decode_c37_word(state, lpa, SPEED_2500);
2550 case PHY_INTERFACE_MODE_SGMII:
2551 case PHY_INTERFACE_MODE_QSGMII:
2552 phylink_decode_sgmii_word(state, lpa);
2556 state->link = false;
2560 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2563 * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2565 * @pcs: a pointer to a &struct mdio_device.
2566 * @interface: the PHY interface mode being configured
2567 * @advertising: the ethtool advertisement mask
2569 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2570 * clause 37 negotiation and/or SGMII control.
2572 * Configure the clause 37 PCS advertisement as specified by @state. This
2573 * does not trigger a renegotiation; phylink will do that via the
2574 * mac_an_restart() method of the struct phylink_mac_ops structure.
2576 * Returns negative error code on failure to configure the advertisement,
2577 * zero if no change has been made, or one if the advertisement has changed.
2579 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
2580 phy_interface_t interface,
2581 const unsigned long *advertising)
2583 struct mii_bus *bus = pcs->bus;
2584 int addr = pcs->addr;
2588 switch (interface) {
2589 case PHY_INTERFACE_MODE_1000BASEX:
2590 case PHY_INTERFACE_MODE_2500BASEX:
2591 adv = ADVERTISE_1000XFULL;
2592 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2594 adv |= ADVERTISE_1000XPAUSE;
2595 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2597 adv |= ADVERTISE_1000XPSE_ASYM;
2599 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2606 ret = mdiobus_write(bus, addr, MII_ADVERTISE, adv);
2612 case PHY_INTERFACE_MODE_SGMII:
2613 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2620 ret = mdiobus_write(bus, addr, MII_ADVERTISE, 0x0001);
2627 /* Nothing to do for other modes */
2631 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2634 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
2635 * @pcs: a pointer to a &struct mdio_device.
2636 * @mode: link autonegotiation mode
2637 * @interface: the PHY interface mode being configured
2638 * @advertising: the ethtool advertisement mask
2640 * Configure a Clause 22 PCS PHY with the appropriate negotiation
2641 * parameters for the @mode, @interface and @advertising parameters.
2642 * Returns negative error number on failure, zero if the advertisement
2643 * has not changed, or positive if there is a change.
2645 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
2646 phy_interface_t interface,
2647 const unsigned long *advertising)
2653 ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface,
2660 /* Ensure ISOLATE bit is disabled */
2661 bmcr = mode == MLO_AN_INBAND ? BMCR_ANENABLE : 0;
2662 ret = mdiobus_modify(pcs->bus, pcs->addr, MII_BMCR,
2663 BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
2667 return changed ? 1 : 0;
2669 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
2672 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2673 * @pcs: a pointer to a &struct mdio_device.
2675 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2676 * clause 37 negotiation.
2678 * Restart the clause 37 negotiation with the link partner. This is
2679 * suitable to be directly plugged into the mac_pcs_get_state() member
2680 * of the struct phylink_mac_ops structure.
2682 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2684 struct mii_bus *bus = pcs->bus;
2685 int val, addr = pcs->addr;
2687 val = mdiobus_read(bus, addr, MII_BMCR);
2689 val |= BMCR_ANRESTART;
2691 mdiobus_write(bus, addr, MII_BMCR, val);
2694 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2696 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2697 struct phylink_link_state *state)
2699 struct mii_bus *bus = pcs->bus;
2700 int addr = pcs->addr;
2703 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
2705 state->link = false;
2709 state->link = !!(stat & MDIO_STAT1_LSTATUS);
2713 switch (state->interface) {
2714 case PHY_INTERFACE_MODE_10GBASER:
2715 state->speed = SPEED_10000;
2716 state->duplex = DUPLEX_FULL;
2723 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2725 MODULE_LICENSE("GPL v2");