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,
39 * struct phylink - internal data type for phylink
43 struct net_device *netdev;
44 const struct phylink_mac_ops *mac_ops;
45 const struct phylink_pcs_ops *pcs_ops;
46 struct phylink_config *config;
47 struct phylink_pcs *pcs;
49 unsigned int old_link_state:1;
51 unsigned long phylink_disable_state; /* bitmask of disables */
52 struct phy_device *phydev;
53 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
54 u8 cfg_link_an_mode; /* MLO_AN_xxx */
56 u8 link_port; /* The current non-phy ethtool port */
57 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
59 /* The link configuration settings */
60 struct phylink_link_state link_config;
62 /* The current settings */
63 phy_interface_t cur_interface;
65 struct gpio_desc *link_gpio;
66 unsigned int link_irq;
67 struct timer_list link_poll;
68 void (*get_fixed_state)(struct net_device *dev,
69 struct phylink_link_state *s);
71 struct mutex state_mutex;
72 struct phylink_link_state phy_state;
73 struct work_struct resolve;
75 bool mac_link_dropped;
77 struct sfp_bus *sfp_bus;
78 bool sfp_may_have_phy;
79 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
83 #define phylink_printk(level, pl, fmt, ...) \
85 if ((pl)->config->type == PHYLINK_NETDEV) \
86 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
87 else if ((pl)->config->type == PHYLINK_DEV) \
88 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
91 #define phylink_err(pl, fmt, ...) \
92 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
93 #define phylink_warn(pl, fmt, ...) \
94 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
95 #define phylink_info(pl, fmt, ...) \
96 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
97 #if defined(CONFIG_DYNAMIC_DEBUG)
98 #define phylink_dbg(pl, fmt, ...) \
100 if ((pl)->config->type == PHYLINK_NETDEV) \
101 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
102 else if ((pl)->config->type == PHYLINK_DEV) \
103 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
106 #define phylink_dbg(pl, fmt, ...) \
107 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
109 #define phylink_dbg(pl, fmt, ...) \
112 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
117 * phylink_set_port_modes() - set the port type modes in the ethtool mask
118 * @mask: ethtool link mode mask
120 * Sets all the port type modes in the ethtool mask. MAC drivers should
121 * use this in their 'validate' callback.
123 void phylink_set_port_modes(unsigned long *mask)
125 phylink_set(mask, TP);
126 phylink_set(mask, AUI);
127 phylink_set(mask, MII);
128 phylink_set(mask, FIBRE);
129 phylink_set(mask, BNC);
130 phylink_set(mask, Backplane);
132 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
134 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
136 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
138 phylink_set_port_modes(tmp);
139 phylink_set(tmp, Autoneg);
140 phylink_set(tmp, Pause);
141 phylink_set(tmp, Asym_Pause);
143 return linkmode_subset(linkmode, tmp);
146 static const char *phylink_an_mode_str(unsigned int mode)
148 static const char *modestr[] = {
149 [MLO_AN_PHY] = "phy",
150 [MLO_AN_FIXED] = "fixed",
151 [MLO_AN_INBAND] = "inband",
154 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
157 static int phylink_validate(struct phylink *pl, unsigned long *supported,
158 struct phylink_link_state *state)
160 pl->mac_ops->validate(pl->config, supported, state);
162 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
165 static int phylink_parse_fixedlink(struct phylink *pl,
166 struct fwnode_handle *fwnode)
168 struct fwnode_handle *fixed_node;
169 const struct phy_setting *s;
170 struct gpio_desc *desc;
174 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
176 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
178 pl->link_config.speed = speed;
179 pl->link_config.duplex = DUPLEX_HALF;
181 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
182 pl->link_config.duplex = DUPLEX_FULL;
184 /* We treat the "pause" and "asym-pause" terminology as
185 * defining the link partner's ability.
187 if (fwnode_property_read_bool(fixed_node, "pause"))
188 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
189 pl->link_config.lp_advertising);
190 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
191 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
192 pl->link_config.lp_advertising);
195 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
199 pl->link_gpio = desc;
200 else if (desc == ERR_PTR(-EPROBE_DEFER))
203 fwnode_handle_put(fixed_node);
210 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
212 if (ret != ARRAY_SIZE(prop)) {
213 phylink_err(pl, "broken fixed-link?\n");
217 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
218 prop, ARRAY_SIZE(prop));
220 pl->link_config.duplex = prop[1] ?
221 DUPLEX_FULL : DUPLEX_HALF;
222 pl->link_config.speed = prop[2];
224 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
225 pl->link_config.lp_advertising);
227 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
228 pl->link_config.lp_advertising);
232 if (pl->link_config.speed > SPEED_1000 &&
233 pl->link_config.duplex != DUPLEX_FULL)
234 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
235 pl->link_config.speed);
237 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
238 linkmode_copy(pl->link_config.advertising, pl->supported);
239 phylink_validate(pl, pl->supported, &pl->link_config);
241 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
242 pl->supported, true);
243 linkmode_zero(pl->supported);
244 phylink_set(pl->supported, MII);
245 phylink_set(pl->supported, Pause);
246 phylink_set(pl->supported, Asym_Pause);
247 phylink_set(pl->supported, Autoneg);
249 __set_bit(s->bit, pl->supported);
250 __set_bit(s->bit, pl->link_config.lp_advertising);
252 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
253 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
254 pl->link_config.speed);
257 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
260 pl->link_config.link = 1;
261 pl->link_config.an_complete = 1;
266 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
268 struct fwnode_handle *dn;
271 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
272 if (dn || fwnode_property_present(fwnode, "fixed-link"))
273 pl->cfg_link_an_mode = MLO_AN_FIXED;
274 fwnode_handle_put(dn);
276 if ((fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
277 strcmp(managed, "in-band-status") == 0) ||
278 pl->config->ovr_an_inband) {
279 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
281 "can't use both fixed-link and in-band-status\n");
285 linkmode_zero(pl->supported);
286 phylink_set(pl->supported, MII);
287 phylink_set(pl->supported, Autoneg);
288 phylink_set(pl->supported, Asym_Pause);
289 phylink_set(pl->supported, Pause);
290 pl->link_config.an_enabled = true;
291 pl->cfg_link_an_mode = MLO_AN_INBAND;
293 switch (pl->link_config.interface) {
294 case PHY_INTERFACE_MODE_SGMII:
295 case PHY_INTERFACE_MODE_QSGMII:
296 phylink_set(pl->supported, 10baseT_Half);
297 phylink_set(pl->supported, 10baseT_Full);
298 phylink_set(pl->supported, 100baseT_Half);
299 phylink_set(pl->supported, 100baseT_Full);
300 phylink_set(pl->supported, 1000baseT_Half);
301 phylink_set(pl->supported, 1000baseT_Full);
304 case PHY_INTERFACE_MODE_1000BASEX:
305 phylink_set(pl->supported, 1000baseX_Full);
308 case PHY_INTERFACE_MODE_2500BASEX:
309 phylink_set(pl->supported, 2500baseX_Full);
312 case PHY_INTERFACE_MODE_5GBASER:
313 phylink_set(pl->supported, 5000baseT_Full);
316 case PHY_INTERFACE_MODE_25GBASER:
317 phylink_set(pl->supported, 25000baseCR_Full);
318 phylink_set(pl->supported, 25000baseKR_Full);
319 phylink_set(pl->supported, 25000baseSR_Full);
321 case PHY_INTERFACE_MODE_USXGMII:
322 case PHY_INTERFACE_MODE_10GKR:
323 case PHY_INTERFACE_MODE_10GBASER:
324 phylink_set(pl->supported, 10baseT_Half);
325 phylink_set(pl->supported, 10baseT_Full);
326 phylink_set(pl->supported, 100baseT_Half);
327 phylink_set(pl->supported, 100baseT_Full);
328 phylink_set(pl->supported, 1000baseT_Half);
329 phylink_set(pl->supported, 1000baseT_Full);
330 phylink_set(pl->supported, 1000baseX_Full);
331 phylink_set(pl->supported, 1000baseKX_Full);
332 phylink_set(pl->supported, 2500baseT_Full);
333 phylink_set(pl->supported, 2500baseX_Full);
334 phylink_set(pl->supported, 5000baseT_Full);
335 phylink_set(pl->supported, 10000baseT_Full);
336 phylink_set(pl->supported, 10000baseKR_Full);
337 phylink_set(pl->supported, 10000baseKX4_Full);
338 phylink_set(pl->supported, 10000baseCR_Full);
339 phylink_set(pl->supported, 10000baseSR_Full);
340 phylink_set(pl->supported, 10000baseLR_Full);
341 phylink_set(pl->supported, 10000baseLRM_Full);
342 phylink_set(pl->supported, 10000baseER_Full);
345 case PHY_INTERFACE_MODE_XLGMII:
346 phylink_set(pl->supported, 25000baseCR_Full);
347 phylink_set(pl->supported, 25000baseKR_Full);
348 phylink_set(pl->supported, 25000baseSR_Full);
349 phylink_set(pl->supported, 40000baseKR4_Full);
350 phylink_set(pl->supported, 40000baseCR4_Full);
351 phylink_set(pl->supported, 40000baseSR4_Full);
352 phylink_set(pl->supported, 40000baseLR4_Full);
353 phylink_set(pl->supported, 50000baseCR2_Full);
354 phylink_set(pl->supported, 50000baseKR2_Full);
355 phylink_set(pl->supported, 50000baseSR2_Full);
356 phylink_set(pl->supported, 50000baseKR_Full);
357 phylink_set(pl->supported, 50000baseSR_Full);
358 phylink_set(pl->supported, 50000baseCR_Full);
359 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
360 phylink_set(pl->supported, 50000baseDR_Full);
361 phylink_set(pl->supported, 100000baseKR4_Full);
362 phylink_set(pl->supported, 100000baseSR4_Full);
363 phylink_set(pl->supported, 100000baseCR4_Full);
364 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
365 phylink_set(pl->supported, 100000baseKR2_Full);
366 phylink_set(pl->supported, 100000baseSR2_Full);
367 phylink_set(pl->supported, 100000baseCR2_Full);
368 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
369 phylink_set(pl->supported, 100000baseDR2_Full);
374 "incorrect link mode %s for in-band status\n",
375 phy_modes(pl->link_config.interface));
379 linkmode_copy(pl->link_config.advertising, pl->supported);
381 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
383 "failed to validate link configuration for in-band status\n");
387 /* Check if MAC/PCS also supports Autoneg. */
388 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
394 static void phylink_apply_manual_flow(struct phylink *pl,
395 struct phylink_link_state *state)
397 /* If autoneg is disabled, pause AN is also disabled */
398 if (!state->an_enabled)
399 state->pause &= ~MLO_PAUSE_AN;
401 /* Manual configuration of pause modes */
402 if (!(pl->link_config.pause & MLO_PAUSE_AN))
403 state->pause = pl->link_config.pause;
406 static void phylink_resolve_flow(struct phylink_link_state *state)
408 bool tx_pause, rx_pause;
410 state->pause = MLO_PAUSE_NONE;
411 if (state->duplex == DUPLEX_FULL) {
412 linkmode_resolve_pause(state->advertising,
413 state->lp_advertising,
414 &tx_pause, &rx_pause);
416 state->pause |= MLO_PAUSE_TX;
418 state->pause |= MLO_PAUSE_RX;
422 static void phylink_mac_config(struct phylink *pl,
423 const struct phylink_link_state *state)
426 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
427 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
428 phy_modes(state->interface),
429 phy_speed_to_str(state->speed),
430 phy_duplex_to_str(state->duplex),
431 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
432 state->pause, state->link, state->an_enabled);
434 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
437 static void phylink_mac_pcs_an_restart(struct phylink *pl)
439 if (pl->link_config.an_enabled &&
440 phy_interface_mode_is_8023z(pl->link_config.interface) &&
441 phylink_autoneg_inband(pl->cur_link_an_mode)) {
443 pl->pcs_ops->pcs_an_restart(pl->pcs);
445 pl->mac_ops->mac_an_restart(pl->config);
449 static void phylink_major_config(struct phylink *pl, bool restart,
450 const struct phylink_link_state *state)
454 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
456 if (pl->mac_ops->mac_prepare) {
457 err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
460 phylink_err(pl, "mac_prepare failed: %pe\n",
466 phylink_mac_config(pl, state);
469 err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
472 !!(pl->link_config.pause &
475 phylink_err(pl, "pcs_config failed: %pe\n",
481 phylink_mac_pcs_an_restart(pl);
483 if (pl->mac_ops->mac_finish) {
484 err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
487 phylink_err(pl, "mac_finish failed: %pe\n",
493 * Reconfigure for a change of inband advertisement.
494 * If we have a separate PCS, we only need to call its pcs_config() method,
495 * and then restart AN if it indicates something changed. Otherwise, we do
496 * the full MAC reconfiguration.
498 static int phylink_change_inband_advert(struct phylink *pl)
502 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
507 phylink_mac_config(pl, &pl->link_config);
508 phylink_mac_pcs_an_restart(pl);
512 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
513 phylink_an_mode_str(pl->cur_link_an_mode),
514 phy_modes(pl->link_config.interface),
515 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
516 pl->link_config.pause);
518 /* Modern PCS-based method; update the advert at the PCS, and
519 * restart negotiation if the pcs_config() helper indicates that
520 * the programmed advertisement has changed.
522 ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
523 pl->link_config.interface,
524 pl->link_config.advertising,
525 !!(pl->link_config.pause & MLO_PAUSE_AN));
530 phylink_mac_pcs_an_restart(pl);
535 static void phylink_mac_pcs_get_state(struct phylink *pl,
536 struct phylink_link_state *state)
538 linkmode_copy(state->advertising, pl->link_config.advertising);
539 linkmode_zero(state->lp_advertising);
540 state->interface = pl->link_config.interface;
541 state->an_enabled = pl->link_config.an_enabled;
542 state->speed = SPEED_UNKNOWN;
543 state->duplex = DUPLEX_UNKNOWN;
544 state->pause = MLO_PAUSE_NONE;
545 state->an_complete = 0;
549 pl->pcs_ops->pcs_get_state(pl->pcs, state);
550 else if (pl->mac_ops->mac_pcs_get_state)
551 pl->mac_ops->mac_pcs_get_state(pl->config, state);
556 /* The fixed state is... fixed except for the link state,
557 * which may be determined by a GPIO or a callback.
559 static void phylink_get_fixed_state(struct phylink *pl,
560 struct phylink_link_state *state)
562 *state = pl->link_config;
563 if (pl->config->get_fixed_state)
564 pl->config->get_fixed_state(pl->config, state);
565 else if (pl->link_gpio)
566 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
568 phylink_resolve_flow(state);
571 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
573 struct phylink_link_state link_state;
575 switch (pl->cur_link_an_mode) {
577 link_state = pl->phy_state;
581 phylink_get_fixed_state(pl, &link_state);
585 link_state = pl->link_config;
586 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
587 link_state.pause = MLO_PAUSE_NONE;
590 default: /* can't happen */
594 link_state.link = false;
596 phylink_apply_manual_flow(pl, &link_state);
597 phylink_major_config(pl, force_restart, &link_state);
600 static const char *phylink_pause_to_str(int pause)
602 switch (pause & MLO_PAUSE_TXRX_MASK) {
603 case MLO_PAUSE_TX | MLO_PAUSE_RX:
614 static void phylink_link_up(struct phylink *pl,
615 struct phylink_link_state link_state)
617 struct net_device *ndev = pl->netdev;
619 pl->cur_interface = link_state.interface;
621 if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
622 pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
624 link_state.speed, link_state.duplex);
626 pl->mac_ops->mac_link_up(pl->config, pl->phydev,
627 pl->cur_link_an_mode, pl->cur_interface,
628 link_state.speed, link_state.duplex,
629 !!(link_state.pause & MLO_PAUSE_TX),
630 !!(link_state.pause & MLO_PAUSE_RX));
633 netif_carrier_on(ndev);
636 "Link is Up - %s/%s - flow control %s\n",
637 phy_speed_to_str(link_state.speed),
638 phy_duplex_to_str(link_state.duplex),
639 phylink_pause_to_str(link_state.pause));
642 static void phylink_link_down(struct phylink *pl)
644 struct net_device *ndev = pl->netdev;
647 netif_carrier_off(ndev);
648 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
650 phylink_info(pl, "Link is Down\n");
653 static void phylink_resolve(struct work_struct *w)
655 struct phylink *pl = container_of(w, struct phylink, resolve);
656 struct phylink_link_state link_state;
657 struct net_device *ndev = pl->netdev;
658 bool mac_config = false;
661 mutex_lock(&pl->state_mutex);
663 cur_link_state = netif_carrier_ok(ndev);
665 cur_link_state = pl->old_link_state;
667 if (pl->phylink_disable_state) {
668 pl->mac_link_dropped = false;
669 link_state.link = false;
670 } else if (pl->mac_link_dropped) {
671 link_state.link = false;
673 switch (pl->cur_link_an_mode) {
675 link_state = pl->phy_state;
676 phylink_apply_manual_flow(pl, &link_state);
677 mac_config = link_state.link;
681 phylink_get_fixed_state(pl, &link_state);
682 mac_config = link_state.link;
686 phylink_mac_pcs_get_state(pl, &link_state);
688 /* If we have a phy, the "up" state is the union of
689 * both the PHY and the MAC
692 link_state.link &= pl->phy_state.link;
694 /* Only update if the PHY link is up */
695 if (pl->phydev && pl->phy_state.link) {
696 link_state.interface = pl->phy_state.interface;
698 /* If we have a PHY, we need to update with
699 * the PHY flow control bits.
701 link_state.pause = pl->phy_state.pause;
704 phylink_apply_manual_flow(pl, &link_state);
710 if (link_state.interface != pl->link_config.interface) {
711 /* The interface has changed, force the link down and
714 if (cur_link_state) {
715 phylink_link_down(pl);
716 cur_link_state = false;
718 phylink_major_config(pl, false, &link_state);
719 pl->link_config.interface = link_state.interface;
720 } else if (!pl->pcs_ops) {
721 /* The interface remains unchanged, only the speed,
722 * duplex or pause settings have changed. Call the
723 * old mac_config() method to configure the MAC/PCS
724 * only if we do not have a PCS installed (an
727 phylink_mac_config(pl, &link_state);
731 if (link_state.link != cur_link_state) {
732 pl->old_link_state = link_state.link;
733 if (!link_state.link)
734 phylink_link_down(pl);
736 phylink_link_up(pl, link_state);
738 if (!link_state.link && pl->mac_link_dropped) {
739 pl->mac_link_dropped = false;
740 queue_work(system_power_efficient_wq, &pl->resolve);
742 mutex_unlock(&pl->state_mutex);
745 static void phylink_run_resolve(struct phylink *pl)
747 if (!pl->phylink_disable_state)
748 queue_work(system_power_efficient_wq, &pl->resolve);
751 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
753 unsigned long state = pl->phylink_disable_state;
755 set_bit(bit, &pl->phylink_disable_state);
757 queue_work(system_power_efficient_wq, &pl->resolve);
758 flush_work(&pl->resolve);
762 static void phylink_fixed_poll(struct timer_list *t)
764 struct phylink *pl = container_of(t, struct phylink, link_poll);
766 mod_timer(t, jiffies + HZ);
768 phylink_run_resolve(pl);
771 static const struct sfp_upstream_ops sfp_phylink_ops;
773 static int phylink_register_sfp(struct phylink *pl,
774 struct fwnode_handle *fwnode)
782 bus = sfp_bus_find_fwnode(fwnode);
785 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
791 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
798 * phylink_create() - create a phylink instance
799 * @config: a pointer to the target &struct phylink_config
800 * @fwnode: a pointer to a &struct fwnode_handle describing the network
802 * @iface: the desired link mode defined by &typedef phy_interface_t
803 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
805 * Create a new phylink instance, and parse the link parameters found in @np.
806 * This will parse in-band modes, fixed-link or SFP configuration.
808 * Note: the rtnl lock must not be held when calling this function.
810 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
811 * must use IS_ERR() to check for errors from this function.
813 struct phylink *phylink_create(struct phylink_config *config,
814 struct fwnode_handle *fwnode,
815 phy_interface_t iface,
816 const struct phylink_mac_ops *mac_ops)
821 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
823 return ERR_PTR(-ENOMEM);
825 mutex_init(&pl->state_mutex);
826 INIT_WORK(&pl->resolve, phylink_resolve);
829 if (config->type == PHYLINK_NETDEV) {
830 pl->netdev = to_net_dev(config->dev);
831 } else if (config->type == PHYLINK_DEV) {
832 pl->dev = config->dev;
835 return ERR_PTR(-EINVAL);
838 pl->phy_state.interface = iface;
839 pl->link_interface = iface;
840 if (iface == PHY_INTERFACE_MODE_MOCA)
841 pl->link_port = PORT_BNC;
843 pl->link_port = PORT_MII;
844 pl->link_config.interface = iface;
845 pl->link_config.pause = MLO_PAUSE_AN;
846 pl->link_config.speed = SPEED_UNKNOWN;
847 pl->link_config.duplex = DUPLEX_UNKNOWN;
848 pl->link_config.an_enabled = true;
849 pl->mac_ops = mac_ops;
850 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
851 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
853 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
854 linkmode_copy(pl->link_config.advertising, pl->supported);
855 phylink_validate(pl, pl->supported, &pl->link_config);
857 ret = phylink_parse_mode(pl, fwnode);
863 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
864 ret = phylink_parse_fixedlink(pl, fwnode);
871 pl->cur_link_an_mode = pl->cfg_link_an_mode;
873 ret = phylink_register_sfp(pl, fwnode);
881 EXPORT_SYMBOL_GPL(phylink_create);
884 * phylink_set_pcs() - set the current PCS for phylink to use
885 * @pl: a pointer to a &struct phylink returned from phylink_create()
886 * @pcs: a pointer to the &struct phylink_pcs
888 * Bind the MAC PCS to phylink. This may be called after phylink_create(),
889 * in mac_prepare() or mac_config() methods if it is desired to dynamically
892 * Please note that there are behavioural changes with the mac_config()
893 * callback if a PCS is present (denoting a newer setup) so removing a PCS
894 * is not supported, and if a PCS is going to be used, it must be registered
895 * by calling phylink_set_pcs() at the latest in the first mac_config() call.
897 void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs)
900 pl->pcs_ops = pcs->ops;
902 EXPORT_SYMBOL_GPL(phylink_set_pcs);
905 * phylink_destroy() - cleanup and destroy the phylink instance
906 * @pl: a pointer to a &struct phylink returned from phylink_create()
908 * Destroy a phylink instance. Any PHY that has been attached must have been
909 * cleaned up via phylink_disconnect_phy() prior to calling this function.
911 * Note: the rtnl lock must not be held when calling this function.
913 void phylink_destroy(struct phylink *pl)
915 sfp_bus_del_upstream(pl->sfp_bus);
917 gpiod_put(pl->link_gpio);
919 cancel_work_sync(&pl->resolve);
922 EXPORT_SYMBOL_GPL(phylink_destroy);
924 static void phylink_phy_change(struct phy_device *phydev, bool up)
926 struct phylink *pl = phydev->phylink;
927 bool tx_pause, rx_pause;
929 phy_get_pause(phydev, &tx_pause, &rx_pause);
931 mutex_lock(&pl->state_mutex);
932 pl->phy_state.speed = phydev->speed;
933 pl->phy_state.duplex = phydev->duplex;
934 pl->phy_state.pause = MLO_PAUSE_NONE;
936 pl->phy_state.pause |= MLO_PAUSE_TX;
938 pl->phy_state.pause |= MLO_PAUSE_RX;
939 pl->phy_state.interface = phydev->interface;
940 pl->phy_state.link = up;
941 mutex_unlock(&pl->state_mutex);
943 phylink_run_resolve(pl);
945 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
946 phy_modes(phydev->interface),
947 phy_speed_to_str(phydev->speed),
948 phy_duplex_to_str(phydev->duplex));
951 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
952 phy_interface_t interface)
954 struct phylink_link_state config;
955 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
960 * This is the new way of dealing with flow control for PHYs,
961 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
962 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
963 * using our validate call to the MAC, we rely upon the MAC
964 * clearing the bits from both supported and advertising fields.
966 phy_support_asym_pause(phy);
968 memset(&config, 0, sizeof(config));
969 linkmode_copy(supported, phy->supported);
970 linkmode_copy(config.advertising, phy->advertising);
972 /* Clause 45 PHYs switch their Serdes lane between several different
973 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
974 * speeds. We really need to know which interface modes the PHY and
975 * MAC supports to properly work out which linkmodes can be supported.
978 interface != PHY_INTERFACE_MODE_RXAUI &&
979 interface != PHY_INTERFACE_MODE_XAUI &&
980 interface != PHY_INTERFACE_MODE_USXGMII)
981 config.interface = PHY_INTERFACE_MODE_NA;
983 config.interface = interface;
985 ret = phylink_validate(pl, supported, &config);
987 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
988 phy_modes(config.interface),
989 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
990 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
996 phy->phy_link_change = phylink_phy_change;
998 irq_str = phy_attached_info_irq(phy);
1000 "PHY [%s] driver [%s] (irq=%s)\n",
1001 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
1004 mutex_lock(&phy->lock);
1005 mutex_lock(&pl->state_mutex);
1007 pl->phy_state.interface = interface;
1008 pl->phy_state.pause = MLO_PAUSE_NONE;
1009 pl->phy_state.speed = SPEED_UNKNOWN;
1010 pl->phy_state.duplex = DUPLEX_UNKNOWN;
1011 linkmode_copy(pl->supported, supported);
1012 linkmode_copy(pl->link_config.advertising, config.advertising);
1014 /* Restrict the phy advertisement according to the MAC support. */
1015 linkmode_copy(phy->advertising, config.advertising);
1016 mutex_unlock(&pl->state_mutex);
1017 mutex_unlock(&phy->lock);
1020 "phy: setting supported %*pb advertising %*pb\n",
1021 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1022 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1024 if (phy_interrupt_is_valid(phy))
1025 phy_request_interrupt(phy);
1030 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1031 phy_interface_t interface)
1033 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1034 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1035 phy_interface_mode_is_8023z(interface))))
1041 return phy_attach_direct(pl->netdev, phy, 0, interface);
1045 * phylink_connect_phy() - connect a PHY to the phylink instance
1046 * @pl: a pointer to a &struct phylink returned from phylink_create()
1047 * @phy: a pointer to a &struct phy_device.
1049 * Connect @phy to the phylink instance specified by @pl by calling
1050 * phy_attach_direct(). Configure the @phy according to the MAC driver's
1051 * capabilities, start the PHYLIB state machine and enable any interrupts
1052 * that the PHY supports.
1054 * This updates the phylink's ethtool supported and advertising link mode
1057 * Returns 0 on success or a negative errno.
1059 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1063 /* Use PHY device/driver interface */
1064 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1065 pl->link_interface = phy->interface;
1066 pl->link_config.interface = pl->link_interface;
1069 ret = phylink_attach_phy(pl, phy, pl->link_interface);
1073 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1079 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1082 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1083 * @pl: a pointer to a &struct phylink returned from phylink_create()
1084 * @dn: a pointer to a &struct device_node.
1085 * @flags: PHY-specific flags to communicate to the PHY device driver
1087 * Connect the phy specified in the device node @dn to the phylink instance
1088 * specified by @pl. Actions specified in phylink_connect_phy() will be
1091 * Returns 0 on success or a negative errno.
1093 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1096 return phylink_fwnode_phy_connect(pl, of_fwnode_handle(dn), flags);
1098 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1101 * phylink_fwnode_phy_connect() - connect the PHY specified in the fwnode.
1102 * @pl: a pointer to a &struct phylink returned from phylink_create()
1103 * @fwnode: a pointer to a &struct fwnode_handle.
1104 * @flags: PHY-specific flags to communicate to the PHY device driver
1106 * Connect the phy specified @fwnode to the phylink instance specified
1109 * Returns 0 on success or a negative errno.
1111 int phylink_fwnode_phy_connect(struct phylink *pl,
1112 struct fwnode_handle *fwnode,
1115 struct fwnode_handle *phy_fwnode;
1116 struct phy_device *phy_dev;
1119 /* Fixed links and 802.3z are handled without needing a PHY */
1120 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1121 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1122 phy_interface_mode_is_8023z(pl->link_interface)))
1125 phy_fwnode = fwnode_get_phy_node(fwnode);
1126 if (IS_ERR(phy_fwnode)) {
1127 if (pl->cfg_link_an_mode == MLO_AN_PHY)
1132 phy_dev = fwnode_phy_find_device(phy_fwnode);
1133 /* We're done with the phy_node handle */
1134 fwnode_handle_put(phy_fwnode);
1138 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1139 pl->link_interface);
1141 phy_device_free(phy_dev);
1145 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1147 phy_detach(phy_dev);
1151 EXPORT_SYMBOL_GPL(phylink_fwnode_phy_connect);
1154 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1156 * @pl: a pointer to a &struct phylink returned from phylink_create()
1158 * Disconnect any current PHY from the phylink instance described by @pl.
1160 void phylink_disconnect_phy(struct phylink *pl)
1162 struct phy_device *phy;
1168 mutex_lock(&phy->lock);
1169 mutex_lock(&pl->state_mutex);
1171 mutex_unlock(&pl->state_mutex);
1172 mutex_unlock(&phy->lock);
1173 flush_work(&pl->resolve);
1175 phy_disconnect(phy);
1178 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1181 * phylink_mac_change() - notify phylink of a change in MAC state
1182 * @pl: a pointer to a &struct phylink returned from phylink_create()
1183 * @up: indicates whether the link is currently up.
1185 * The MAC driver should call this driver when the state of its link
1186 * changes (eg, link failure, new negotiation results, etc.)
1188 void phylink_mac_change(struct phylink *pl, bool up)
1191 pl->mac_link_dropped = true;
1192 phylink_run_resolve(pl);
1193 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1195 EXPORT_SYMBOL_GPL(phylink_mac_change);
1197 static irqreturn_t phylink_link_handler(int irq, void *data)
1199 struct phylink *pl = data;
1201 phylink_run_resolve(pl);
1207 * phylink_start() - start a phylink instance
1208 * @pl: a pointer to a &struct phylink returned from phylink_create()
1210 * Start the phylink instance specified by @pl, configuring the MAC for the
1211 * desired link mode(s) and negotiation style. This should be called from the
1212 * network device driver's &struct net_device_ops ndo_open() method.
1214 void phylink_start(struct phylink *pl)
1220 phylink_info(pl, "configuring for %s/%s link mode\n",
1221 phylink_an_mode_str(pl->cur_link_an_mode),
1222 phy_modes(pl->link_config.interface));
1224 /* Always set the carrier off */
1226 netif_carrier_off(pl->netdev);
1228 /* Apply the link configuration to the MAC when starting. This allows
1229 * a fixed-link to start with the correct parameters, and also
1230 * ensures that we set the appropriate advertisement for Serdes links.
1232 * Restart autonegotiation if using 802.3z to ensure that the link
1233 * parameters are properly negotiated. This is necessary for DSA
1234 * switches using 802.3z negotiation to ensure they see our modes.
1236 phylink_mac_initial_config(pl, true);
1238 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1239 phylink_run_resolve(pl);
1241 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1242 int irq = gpiod_to_irq(pl->link_gpio);
1245 if (!request_irq(irq, phylink_link_handler,
1246 IRQF_TRIGGER_RISING |
1247 IRQF_TRIGGER_FALLING,
1257 switch (pl->cfg_link_an_mode) {
1259 poll |= pl->config->poll_fixed_state;
1262 poll |= pl->config->pcs_poll;
1264 poll |= pl->pcs->poll;
1268 mod_timer(&pl->link_poll, jiffies + HZ);
1270 phy_start(pl->phydev);
1272 sfp_upstream_start(pl->sfp_bus);
1274 EXPORT_SYMBOL_GPL(phylink_start);
1277 * phylink_stop() - stop a phylink instance
1278 * @pl: a pointer to a &struct phylink returned from phylink_create()
1280 * Stop the phylink instance specified by @pl. This should be called from the
1281 * network device driver's &struct net_device_ops ndo_stop() method. The
1282 * network device's carrier state should not be changed prior to calling this
1285 void phylink_stop(struct phylink *pl)
1290 sfp_upstream_stop(pl->sfp_bus);
1292 phy_stop(pl->phydev);
1293 del_timer_sync(&pl->link_poll);
1295 free_irq(pl->link_irq, pl);
1299 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1301 EXPORT_SYMBOL_GPL(phylink_stop);
1304 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1305 * @pl: a pointer to a &struct phylink returned from phylink_create()
1306 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1308 * Read the wake on lan parameters from the PHY attached to the phylink
1309 * instance specified by @pl. If no PHY is currently attached, report no
1310 * support for wake on lan.
1312 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1320 phy_ethtool_get_wol(pl->phydev, wol);
1322 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1325 * phylink_ethtool_set_wol() - set wake on lan parameters
1326 * @pl: a pointer to a &struct phylink returned from phylink_create()
1327 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1329 * Set the wake on lan parameters for the PHY attached to the phylink
1330 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1333 * Returns zero on success or negative errno code.
1335 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1337 int ret = -EOPNOTSUPP;
1342 ret = phy_ethtool_set_wol(pl->phydev, wol);
1346 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1348 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1350 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1352 linkmode_zero(mask);
1353 phylink_set_port_modes(mask);
1355 linkmode_and(dst, dst, mask);
1356 linkmode_or(dst, dst, b);
1359 static void phylink_get_ksettings(const struct phylink_link_state *state,
1360 struct ethtool_link_ksettings *kset)
1362 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1363 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1364 kset->base.speed = state->speed;
1365 kset->base.duplex = state->duplex;
1366 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1371 * phylink_ethtool_ksettings_get() - get the current link settings
1372 * @pl: a pointer to a &struct phylink returned from phylink_create()
1373 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1375 * Read the current link settings for the phylink instance specified by @pl.
1376 * This will be the link settings read from the MAC, PHY or fixed link
1377 * settings depending on the current negotiation mode.
1379 int phylink_ethtool_ksettings_get(struct phylink *pl,
1380 struct ethtool_link_ksettings *kset)
1382 struct phylink_link_state link_state;
1387 phy_ethtool_ksettings_get(pl->phydev, kset);
1389 kset->base.port = pl->link_port;
1391 linkmode_copy(kset->link_modes.supported, pl->supported);
1393 switch (pl->cur_link_an_mode) {
1395 /* We are using fixed settings. Report these as the
1396 * current link settings - and note that these also
1397 * represent the supported speeds/duplex/pause modes.
1399 phylink_get_fixed_state(pl, &link_state);
1400 phylink_get_ksettings(&link_state, kset);
1404 /* If there is a phy attached, then use the reported
1405 * settings from the phy with no modification.
1410 phylink_mac_pcs_get_state(pl, &link_state);
1412 /* The MAC is reporting the link results from its own PCS
1413 * layer via in-band status. Report these as the current
1416 phylink_get_ksettings(&link_state, kset);
1422 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1425 * phylink_ethtool_ksettings_set() - set the link settings
1426 * @pl: a pointer to a &struct phylink returned from phylink_create()
1427 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1429 int phylink_ethtool_ksettings_set(struct phylink *pl,
1430 const struct ethtool_link_ksettings *kset)
1432 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1433 struct phylink_link_state config;
1434 const struct phy_setting *s;
1439 /* We can rely on phylib for this update; we also do not need
1440 * to update the pl->link_config settings:
1441 * - the configuration returned via ksettings_get() will come
1442 * from phylib whenever a PHY is present.
1443 * - link_config.interface will be updated by the PHY calling
1444 * back via phylink_phy_change() and a subsequent resolve.
1445 * - initial link configuration for PHY mode comes from the
1446 * last phy state updated via phylink_phy_change().
1447 * - other configuration changes (e.g. pause modes) are
1448 * performed directly via phylib.
1449 * - if in in-band mode with a PHY, the link configuration
1450 * is passed on the link from the PHY, and all of
1451 * link_config.{speed,duplex,an_enabled,pause} are not used.
1452 * - the only possible use would be link_config.advertising
1453 * pause modes when in 1000base-X mode with a PHY, but in
1454 * the presence of a PHY, this should not be changed as that
1455 * should be determined from the media side advertisement.
1457 return phy_ethtool_ksettings_set(pl->phydev, kset);
1460 linkmode_copy(support, pl->supported);
1461 config = pl->link_config;
1462 config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
1464 /* Mask out unsupported advertisements, and force the autoneg bit */
1465 linkmode_and(config.advertising, kset->link_modes.advertising,
1467 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
1470 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1471 switch (kset->base.autoneg) {
1472 case AUTONEG_DISABLE:
1473 /* Autonegotiation disabled, select a suitable speed and
1476 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1481 /* If we have a fixed link, refuse to change link parameters.
1482 * If the link parameters match, accept them but do nothing.
1484 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1485 if (s->speed != pl->link_config.speed ||
1486 s->duplex != pl->link_config.duplex)
1491 config.speed = s->speed;
1492 config.duplex = s->duplex;
1495 case AUTONEG_ENABLE:
1496 /* If we have a fixed link, allow autonegotiation (since that
1497 * is our default case) but do not allow the advertisement to
1498 * be changed. If the advertisement matches, simply return.
1500 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1501 if (!linkmode_equal(config.advertising,
1502 pl->link_config.advertising))
1507 config.speed = SPEED_UNKNOWN;
1508 config.duplex = DUPLEX_UNKNOWN;
1515 /* We have ruled out the case with a PHY attached, and the
1516 * fixed-link cases. All that is left are in-band links.
1518 if (phylink_validate(pl, support, &config))
1521 /* If autonegotiation is enabled, we must have an advertisement */
1522 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1525 mutex_lock(&pl->state_mutex);
1526 pl->link_config.speed = config.speed;
1527 pl->link_config.duplex = config.duplex;
1528 pl->link_config.an_enabled = config.an_enabled;
1530 if (pl->link_config.interface != config.interface) {
1531 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
1532 /* We need to force the link down, then change the interface */
1533 if (pl->old_link_state) {
1534 phylink_link_down(pl);
1535 pl->old_link_state = false;
1537 if (!test_bit(PHYLINK_DISABLE_STOPPED,
1538 &pl->phylink_disable_state))
1539 phylink_major_config(pl, false, &config);
1540 pl->link_config.interface = config.interface;
1541 linkmode_copy(pl->link_config.advertising, config.advertising);
1542 } else if (!linkmode_equal(pl->link_config.advertising,
1543 config.advertising)) {
1544 linkmode_copy(pl->link_config.advertising, config.advertising);
1545 phylink_change_inband_advert(pl);
1547 mutex_unlock(&pl->state_mutex);
1551 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1554 * phylink_ethtool_nway_reset() - restart negotiation
1555 * @pl: a pointer to a &struct phylink returned from phylink_create()
1557 * Restart negotiation for the phylink instance specified by @pl. This will
1558 * cause any attached phy to restart negotiation with the link partner, and
1559 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1562 * Returns zero on success, or negative error code.
1564 int phylink_ethtool_nway_reset(struct phylink *pl)
1571 ret = phy_restart_aneg(pl->phydev);
1572 phylink_mac_pcs_an_restart(pl);
1576 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1579 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1580 * @pl: a pointer to a &struct phylink returned from phylink_create()
1581 * @pause: a pointer to a &struct ethtool_pauseparam
1583 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1584 struct ethtool_pauseparam *pause)
1588 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1589 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1590 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1592 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1595 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1596 * @pl: a pointer to a &struct phylink returned from phylink_create()
1597 * @pause: a pointer to a &struct ethtool_pauseparam
1599 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1600 struct ethtool_pauseparam *pause)
1602 struct phylink_link_state *config = &pl->link_config;
1603 bool manual_changed;
1608 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1611 if (!phylink_test(pl->supported, Pause) &&
1612 !phylink_test(pl->supported, Asym_Pause))
1615 if (!phylink_test(pl->supported, Asym_Pause) &&
1616 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1621 pause_state |= MLO_PAUSE_AN;
1622 if (pause->rx_pause)
1623 pause_state |= MLO_PAUSE_RX;
1624 if (pause->tx_pause)
1625 pause_state |= MLO_PAUSE_TX;
1627 mutex_lock(&pl->state_mutex);
1629 * See the comments for linkmode_set_pause(), wrt the deficiencies
1630 * with the current implementation. A solution to this issue would
1632 * ethtool Local device
1633 * rx tx Pause AsymDir
1638 * and then use the ethtool rx/tx enablement status to mask the
1639 * rx/tx pause resolution.
1641 linkmode_set_pause(config->advertising, pause->tx_pause,
1644 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1645 (!(pause_state & MLO_PAUSE_AN) &&
1646 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1648 config->pause = pause_state;
1650 /* Update our in-band advertisement, triggering a renegotiation if
1651 * the advertisement changed.
1654 phylink_change_inband_advert(pl);
1656 mutex_unlock(&pl->state_mutex);
1658 /* If we have a PHY, a change of the pause frame advertisement will
1659 * cause phylib to renegotiate (if AN is enabled) which will in turn
1660 * call our phylink_phy_change() and trigger a resolve. Note that
1661 * we can't hold our state mutex while calling phy_set_asym_pause().
1664 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1667 /* If the manual pause settings changed, make sure we trigger a
1668 * resolve to update their state; we can not guarantee that the
1671 if (manual_changed) {
1672 pl->mac_link_dropped = true;
1673 phylink_run_resolve(pl);
1678 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1681 * phylink_get_eee_err() - read the energy efficient ethernet error
1683 * @pl: a pointer to a &struct phylink returned from phylink_create().
1685 * Read the Energy Efficient Ethernet error counter from the PHY associated
1686 * with the phylink instance specified by @pl.
1688 * Returns positive error counter value, or negative error code.
1690 int phylink_get_eee_err(struct phylink *pl)
1697 ret = phy_get_eee_err(pl->phydev);
1701 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1704 * phylink_init_eee() - init and check the EEE features
1705 * @pl: a pointer to a &struct phylink returned from phylink_create()
1706 * @clk_stop_enable: allow PHY to stop receive clock
1708 * Must be called either with RTNL held or within mac_link_up()
1710 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1712 int ret = -EOPNOTSUPP;
1715 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1719 EXPORT_SYMBOL_GPL(phylink_init_eee);
1722 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1723 * @pl: a pointer to a &struct phylink returned from phylink_create()
1724 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1726 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1728 int ret = -EOPNOTSUPP;
1733 ret = phy_ethtool_get_eee(pl->phydev, eee);
1737 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1740 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1741 * @pl: a pointer to a &struct phylink returned from phylink_create()
1742 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1744 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1746 int ret = -EOPNOTSUPP;
1751 ret = phy_ethtool_set_eee(pl->phydev, eee);
1755 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1757 /* This emulates MII registers for a fixed-mode phy operating as per the
1758 * passed in state. "aneg" defines if we report negotiation is possible.
1760 * FIXME: should deal with negotiation state too.
1762 static int phylink_mii_emul_read(unsigned int reg,
1763 struct phylink_link_state *state)
1765 struct fixed_phy_status fs;
1766 unsigned long *lpa = state->lp_advertising;
1769 fs.link = state->link;
1770 fs.speed = state->speed;
1771 fs.duplex = state->duplex;
1772 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1773 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
1775 val = swphy_read_reg(reg, &fs);
1776 if (reg == MII_BMSR) {
1777 if (!state->an_complete)
1778 val &= ~BMSR_ANEGCOMPLETE;
1783 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1786 struct phy_device *phydev = pl->phydev;
1789 if (mdio_phy_id_is_c45(phy_id)) {
1790 prtad = mdio_phy_id_prtad(phy_id);
1791 devad = mdio_phy_id_devad(phy_id);
1792 devad = mdiobus_c45_addr(devad, reg);
1793 } else if (phydev->is_c45) {
1799 devad = __ffs(phydev->c45_ids.mmds_present);
1803 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1805 devad = MDIO_MMD_AN;
1806 if (reg == MII_ADVERTISE)
1807 reg = MDIO_AN_ADVERTISE;
1815 devad = mdiobus_c45_addr(devad, reg);
1820 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1823 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1824 unsigned int reg, unsigned int val)
1826 struct phy_device *phydev = pl->phydev;
1829 if (mdio_phy_id_is_c45(phy_id)) {
1830 prtad = mdio_phy_id_prtad(phy_id);
1831 devad = mdio_phy_id_devad(phy_id);
1832 devad = mdiobus_c45_addr(devad, reg);
1833 } else if (phydev->is_c45) {
1839 devad = __ffs(phydev->c45_ids.mmds_present);
1843 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1845 devad = MDIO_MMD_AN;
1846 if (reg == MII_ADVERTISE)
1847 reg = MDIO_AN_ADVERTISE;
1855 devad = mdiobus_c45_addr(devad, reg);
1861 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1864 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1867 struct phylink_link_state state;
1870 switch (pl->cur_link_an_mode) {
1873 phylink_get_fixed_state(pl, &state);
1874 val = phylink_mii_emul_read(reg, &state);
1883 phylink_mac_pcs_get_state(pl, &state);
1884 val = phylink_mii_emul_read(reg, &state);
1889 return val & 0xffff;
1892 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1893 unsigned int reg, unsigned int val)
1895 switch (pl->cur_link_an_mode) {
1910 * phylink_mii_ioctl() - generic mii ioctl interface
1911 * @pl: a pointer to a &struct phylink returned from phylink_create()
1912 * @ifr: a pointer to a &struct ifreq for socket ioctls
1913 * @cmd: ioctl cmd to execute
1915 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1916 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1918 * Returns: zero on success or negative error code.
1921 * read register from the current PHY.
1923 * read register from the specified PHY.
1925 * set a register on the specified PHY.
1927 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1929 struct mii_ioctl_data *mii = if_mii(ifr);
1935 /* PHYs only exist for MLO_AN_PHY and SGMII */
1938 mii->phy_id = pl->phydev->mdio.addr;
1942 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1950 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1955 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1965 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1973 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1985 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1988 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
1990 * @pl: a pointer to a &struct phylink returned from phylink_create()
1991 * @sync: perform action synchronously
1993 * If we have a PHY that is not part of a SFP module, then set the speed
1994 * as described in the phy_speed_down() function. Please see this function
1995 * for a description of the @sync parameter.
1997 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
1999 int phylink_speed_down(struct phylink *pl, bool sync)
2005 if (!pl->sfp_bus && pl->phydev)
2006 ret = phy_speed_down(pl->phydev, sync);
2010 EXPORT_SYMBOL_GPL(phylink_speed_down);
2013 * phylink_speed_up() - restore the advertised speeds prior to the call to
2014 * phylink_speed_down()
2015 * @pl: a pointer to a &struct phylink returned from phylink_create()
2017 * If we have a PHY that is not part of a SFP module, then restore the
2018 * PHY speeds as per phy_speed_up().
2020 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2022 int phylink_speed_up(struct phylink *pl)
2028 if (!pl->sfp_bus && pl->phydev)
2029 ret = phy_speed_up(pl->phydev);
2033 EXPORT_SYMBOL_GPL(phylink_speed_up);
2035 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2037 struct phylink *pl = upstream;
2039 pl->netdev->sfp_bus = bus;
2042 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2044 struct phylink *pl = upstream;
2046 pl->netdev->sfp_bus = NULL;
2049 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2050 const unsigned long *supported,
2051 const unsigned long *advertising)
2053 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2054 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2055 struct phylink_link_state config;
2056 phy_interface_t iface;
2060 linkmode_copy(support, supported);
2062 memset(&config, 0, sizeof(config));
2063 linkmode_copy(config.advertising, advertising);
2064 config.interface = PHY_INTERFACE_MODE_NA;
2065 config.speed = SPEED_UNKNOWN;
2066 config.duplex = DUPLEX_UNKNOWN;
2067 config.pause = MLO_PAUSE_AN;
2068 config.an_enabled = pl->link_config.an_enabled;
2070 /* Ignore errors if we're expecting a PHY to attach later */
2071 ret = phylink_validate(pl, support, &config);
2073 phylink_err(pl, "validation with support %*pb failed: %d\n",
2074 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2078 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2079 if (iface == PHY_INTERFACE_MODE_NA) {
2081 "selection of interface failed, advertisement %*pb\n",
2082 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2086 config.interface = iface;
2087 linkmode_copy(support1, support);
2088 ret = phylink_validate(pl, support1, &config);
2090 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
2091 phylink_an_mode_str(mode),
2092 phy_modes(config.interface),
2093 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2097 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2098 phylink_an_mode_str(mode), phy_modes(config.interface),
2099 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2101 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2104 changed = !linkmode_equal(pl->supported, support);
2106 linkmode_copy(pl->supported, support);
2107 linkmode_copy(pl->link_config.advertising, config.advertising);
2110 if (pl->cur_link_an_mode != mode ||
2111 pl->link_config.interface != config.interface) {
2112 pl->link_config.interface = config.interface;
2113 pl->cur_link_an_mode = mode;
2117 phylink_info(pl, "switched to %s/%s link mode\n",
2118 phylink_an_mode_str(mode),
2119 phy_modes(config.interface));
2122 pl->link_port = pl->sfp_port;
2124 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2125 &pl->phylink_disable_state))
2126 phylink_mac_initial_config(pl, false);
2131 static int phylink_sfp_module_insert(void *upstream,
2132 const struct sfp_eeprom_id *id)
2134 struct phylink *pl = upstream;
2135 unsigned long *support = pl->sfp_support;
2139 linkmode_zero(support);
2140 sfp_parse_support(pl->sfp_bus, id, support);
2141 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2143 /* If this module may have a PHY connecting later, defer until later */
2144 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2145 if (pl->sfp_may_have_phy)
2148 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2151 static int phylink_sfp_module_start(void *upstream)
2153 struct phylink *pl = upstream;
2155 /* If this SFP module has a PHY, start the PHY now. */
2157 phy_start(pl->phydev);
2161 /* If the module may have a PHY but we didn't detect one we
2162 * need to configure the MAC here.
2164 if (!pl->sfp_may_have_phy)
2167 return phylink_sfp_config(pl, MLO_AN_INBAND,
2168 pl->sfp_support, pl->sfp_support);
2171 static void phylink_sfp_module_stop(void *upstream)
2173 struct phylink *pl = upstream;
2175 /* If this SFP module has a PHY, stop it. */
2177 phy_stop(pl->phydev);
2180 static void phylink_sfp_link_down(void *upstream)
2182 struct phylink *pl = upstream;
2186 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2189 static void phylink_sfp_link_up(void *upstream)
2191 struct phylink *pl = upstream;
2195 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2196 phylink_run_resolve(pl);
2199 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2200 * or 802.3z control word, so inband will not work.
2202 static bool phylink_phy_no_inband(struct phy_device *phy)
2204 return phy->is_c45 &&
2205 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2208 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2210 struct phylink *pl = upstream;
2211 phy_interface_t interface;
2216 * This is the new way of dealing with flow control for PHYs,
2217 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2218 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2219 * using our validate call to the MAC, we rely upon the MAC
2220 * clearing the bits from both supported and advertising fields.
2222 phy_support_asym_pause(phy);
2224 if (phylink_phy_no_inband(phy))
2227 mode = MLO_AN_INBAND;
2229 /* Do the initial configuration */
2230 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2234 interface = pl->link_config.interface;
2235 ret = phylink_attach_phy(pl, phy, interface);
2239 ret = phylink_bringup_phy(pl, phy, interface);
2246 static void phylink_sfp_disconnect_phy(void *upstream)
2248 phylink_disconnect_phy(upstream);
2251 static const struct sfp_upstream_ops sfp_phylink_ops = {
2252 .attach = phylink_sfp_attach,
2253 .detach = phylink_sfp_detach,
2254 .module_insert = phylink_sfp_module_insert,
2255 .module_start = phylink_sfp_module_start,
2256 .module_stop = phylink_sfp_module_stop,
2257 .link_up = phylink_sfp_link_up,
2258 .link_down = phylink_sfp_link_down,
2259 .connect_phy = phylink_sfp_connect_phy,
2260 .disconnect_phy = phylink_sfp_disconnect_phy,
2263 /* Helpers for MAC drivers */
2266 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2267 * @state: a pointer to a &struct phylink_link_state
2269 * Inspect the interface mode, advertising mask or forced speed and
2270 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2271 * the interface mode to suit. @state->interface is appropriately
2272 * updated, and the advertising mask has the "other" baseX_Full flag
2275 void phylink_helper_basex_speed(struct phylink_link_state *state)
2277 if (phy_interface_mode_is_8023z(state->interface)) {
2278 bool want_2500 = state->an_enabled ?
2279 phylink_test(state->advertising, 2500baseX_Full) :
2280 state->speed == SPEED_2500;
2283 phylink_clear(state->advertising, 1000baseX_Full);
2284 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2286 phylink_clear(state->advertising, 2500baseX_Full);
2287 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2291 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2293 static void phylink_decode_c37_word(struct phylink_link_state *state,
2294 uint16_t config_reg, int speed)
2296 bool tx_pause, rx_pause;
2299 if (speed == SPEED_2500)
2300 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2302 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2304 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2306 if (linkmode_test_bit(fd_bit, state->advertising) &&
2307 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2308 state->speed = speed;
2309 state->duplex = DUPLEX_FULL;
2311 /* negotiation failure */
2312 state->link = false;
2315 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2316 &tx_pause, &rx_pause);
2319 state->pause |= MLO_PAUSE_TX;
2321 state->pause |= MLO_PAUSE_RX;
2324 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2325 uint16_t config_reg)
2327 if (!(config_reg & LPA_SGMII_LINK)) {
2328 state->link = false;
2332 switch (config_reg & LPA_SGMII_SPD_MASK) {
2334 state->speed = SPEED_10;
2337 state->speed = SPEED_100;
2339 case LPA_SGMII_1000:
2340 state->speed = SPEED_1000;
2343 state->link = false;
2346 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2347 state->duplex = DUPLEX_FULL;
2349 state->duplex = DUPLEX_HALF;
2353 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2354 * @state: a pointer to a struct phylink_link_state.
2355 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2357 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2358 * code word. Decode the USXGMII code word and populate the corresponding fields
2359 * (speed, duplex) into the phylink_link_state structure.
2361 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2364 switch (lpa & MDIO_USXGMII_SPD_MASK) {
2365 case MDIO_USXGMII_10:
2366 state->speed = SPEED_10;
2368 case MDIO_USXGMII_100:
2369 state->speed = SPEED_100;
2371 case MDIO_USXGMII_1000:
2372 state->speed = SPEED_1000;
2374 case MDIO_USXGMII_2500:
2375 state->speed = SPEED_2500;
2377 case MDIO_USXGMII_5000:
2378 state->speed = SPEED_5000;
2380 case MDIO_USXGMII_10G:
2381 state->speed = SPEED_10000;
2384 state->link = false;
2388 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2389 state->duplex = DUPLEX_FULL;
2391 state->duplex = DUPLEX_HALF;
2393 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2396 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2397 * @pcs: a pointer to a &struct mdio_device.
2398 * @state: a pointer to a &struct phylink_link_state.
2400 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2401 * clause 37 negotiation and/or SGMII control.
2403 * Read the MAC PCS state from the MII device configured in @config and
2404 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2405 * the phylink @state structure. This is suitable to be directly plugged
2406 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2409 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2410 struct phylink_link_state *state)
2412 struct mii_bus *bus = pcs->bus;
2413 int addr = pcs->addr;
2416 bmsr = mdiobus_read(bus, addr, MII_BMSR);
2417 lpa = mdiobus_read(bus, addr, MII_LPA);
2418 if (bmsr < 0 || lpa < 0) {
2419 state->link = false;
2423 state->link = !!(bmsr & BMSR_LSTATUS);
2424 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2428 switch (state->interface) {
2429 case PHY_INTERFACE_MODE_1000BASEX:
2430 phylink_decode_c37_word(state, lpa, SPEED_1000);
2433 case PHY_INTERFACE_MODE_2500BASEX:
2434 phylink_decode_c37_word(state, lpa, SPEED_2500);
2437 case PHY_INTERFACE_MODE_SGMII:
2438 case PHY_INTERFACE_MODE_QSGMII:
2439 phylink_decode_sgmii_word(state, lpa);
2443 state->link = false;
2447 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2450 * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2452 * @pcs: a pointer to a &struct mdio_device.
2453 * @interface: the PHY interface mode being configured
2454 * @advertising: the ethtool advertisement mask
2456 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2457 * clause 37 negotiation and/or SGMII control.
2459 * Configure the clause 37 PCS advertisement as specified by @state. This
2460 * does not trigger a renegotiation; phylink will do that via the
2461 * mac_an_restart() method of the struct phylink_mac_ops structure.
2463 * Returns negative error code on failure to configure the advertisement,
2464 * zero if no change has been made, or one if the advertisement has changed.
2466 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
2467 phy_interface_t interface,
2468 const unsigned long *advertising)
2470 struct mii_bus *bus = pcs->bus;
2471 int addr = pcs->addr;
2475 switch (interface) {
2476 case PHY_INTERFACE_MODE_1000BASEX:
2477 case PHY_INTERFACE_MODE_2500BASEX:
2478 adv = ADVERTISE_1000XFULL;
2479 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2481 adv |= ADVERTISE_1000XPAUSE;
2482 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2484 adv |= ADVERTISE_1000XPSE_ASYM;
2486 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2493 ret = mdiobus_write(bus, addr, MII_ADVERTISE, adv);
2499 case PHY_INTERFACE_MODE_SGMII:
2500 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2507 ret = mdiobus_write(bus, addr, MII_ADVERTISE, 0x0001);
2514 /* Nothing to do for other modes */
2518 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2521 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
2522 * @pcs: a pointer to a &struct mdio_device.
2523 * @mode: link autonegotiation mode
2524 * @interface: the PHY interface mode being configured
2525 * @advertising: the ethtool advertisement mask
2527 * Configure a Clause 22 PCS PHY with the appropriate negotiation
2528 * parameters for the @mode, @interface and @advertising parameters.
2529 * Returns negative error number on failure, zero if the advertisement
2530 * has not changed, or positive if there is a change.
2532 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
2533 phy_interface_t interface,
2534 const unsigned long *advertising)
2540 ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface,
2547 /* Ensure ISOLATE bit is disabled */
2548 bmcr = mode == MLO_AN_INBAND ? BMCR_ANENABLE : 0;
2549 ret = mdiobus_modify(pcs->bus, pcs->addr, MII_BMCR,
2550 BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
2554 return changed ? 1 : 0;
2556 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
2559 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2560 * @pcs: a pointer to a &struct mdio_device.
2562 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2563 * clause 37 negotiation.
2565 * Restart the clause 37 negotiation with the link partner. This is
2566 * suitable to be directly plugged into the mac_pcs_get_state() member
2567 * of the struct phylink_mac_ops structure.
2569 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2571 struct mii_bus *bus = pcs->bus;
2572 int val, addr = pcs->addr;
2574 val = mdiobus_read(bus, addr, MII_BMCR);
2576 val |= BMCR_ANRESTART;
2578 mdiobus_write(bus, addr, MII_BMCR, val);
2581 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2583 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2584 struct phylink_link_state *state)
2586 struct mii_bus *bus = pcs->bus;
2587 int addr = pcs->addr;
2590 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
2592 state->link = false;
2596 state->link = !!(stat & MDIO_STAT1_LSTATUS);
2600 switch (state->interface) {
2601 case PHY_INTERFACE_MODE_10GBASER:
2602 state->speed = SPEED_10000;
2603 state->duplex = DUPLEX_FULL;
2610 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2612 MODULE_LICENSE("GPL v2");