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 mutex_lock(&pl->state_mutex);
1611 pl->link_config.speed = config.speed;
1612 pl->link_config.duplex = config.duplex;
1613 pl->link_config.an_enabled = config.an_enabled;
1615 if (pl->link_config.interface != config.interface) {
1616 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
1617 /* We need to force the link down, then change the interface */
1618 if (pl->old_link_state) {
1619 phylink_link_down(pl);
1620 pl->old_link_state = false;
1622 if (!test_bit(PHYLINK_DISABLE_STOPPED,
1623 &pl->phylink_disable_state))
1624 phylink_major_config(pl, false, &config);
1625 pl->link_config.interface = config.interface;
1626 linkmode_copy(pl->link_config.advertising, config.advertising);
1627 } else if (!linkmode_equal(pl->link_config.advertising,
1628 config.advertising)) {
1629 linkmode_copy(pl->link_config.advertising, config.advertising);
1630 phylink_change_inband_advert(pl);
1632 mutex_unlock(&pl->state_mutex);
1636 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1639 * phylink_ethtool_nway_reset() - restart negotiation
1640 * @pl: a pointer to a &struct phylink returned from phylink_create()
1642 * Restart negotiation for the phylink instance specified by @pl. This will
1643 * cause any attached phy to restart negotiation with the link partner, and
1644 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1647 * Returns zero on success, or negative error code.
1649 int phylink_ethtool_nway_reset(struct phylink *pl)
1656 ret = phy_restart_aneg(pl->phydev);
1657 phylink_mac_pcs_an_restart(pl);
1661 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1664 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1665 * @pl: a pointer to a &struct phylink returned from phylink_create()
1666 * @pause: a pointer to a &struct ethtool_pauseparam
1668 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1669 struct ethtool_pauseparam *pause)
1673 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1674 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1675 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1677 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1680 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1681 * @pl: a pointer to a &struct phylink returned from phylink_create()
1682 * @pause: a pointer to a &struct ethtool_pauseparam
1684 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1685 struct ethtool_pauseparam *pause)
1687 struct phylink_link_state *config = &pl->link_config;
1688 bool manual_changed;
1693 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1696 if (!phylink_test(pl->supported, Pause) &&
1697 !phylink_test(pl->supported, Asym_Pause))
1700 if (!phylink_test(pl->supported, Asym_Pause) &&
1701 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1706 pause_state |= MLO_PAUSE_AN;
1707 if (pause->rx_pause)
1708 pause_state |= MLO_PAUSE_RX;
1709 if (pause->tx_pause)
1710 pause_state |= MLO_PAUSE_TX;
1712 mutex_lock(&pl->state_mutex);
1714 * See the comments for linkmode_set_pause(), wrt the deficiencies
1715 * with the current implementation. A solution to this issue would
1717 * ethtool Local device
1718 * rx tx Pause AsymDir
1723 * and then use the ethtool rx/tx enablement status to mask the
1724 * rx/tx pause resolution.
1726 linkmode_set_pause(config->advertising, pause->tx_pause,
1729 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1730 (!(pause_state & MLO_PAUSE_AN) &&
1731 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1733 config->pause = pause_state;
1735 /* Update our in-band advertisement, triggering a renegotiation if
1736 * the advertisement changed.
1739 phylink_change_inband_advert(pl);
1741 mutex_unlock(&pl->state_mutex);
1743 /* If we have a PHY, a change of the pause frame advertisement will
1744 * cause phylib to renegotiate (if AN is enabled) which will in turn
1745 * call our phylink_phy_change() and trigger a resolve. Note that
1746 * we can't hold our state mutex while calling phy_set_asym_pause().
1749 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1752 /* If the manual pause settings changed, make sure we trigger a
1753 * resolve to update their state; we can not guarantee that the
1756 if (manual_changed) {
1757 pl->mac_link_dropped = true;
1758 phylink_run_resolve(pl);
1763 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1766 * phylink_get_eee_err() - read the energy efficient ethernet error
1768 * @pl: a pointer to a &struct phylink returned from phylink_create().
1770 * Read the Energy Efficient Ethernet error counter from the PHY associated
1771 * with the phylink instance specified by @pl.
1773 * Returns positive error counter value, or negative error code.
1775 int phylink_get_eee_err(struct phylink *pl)
1782 ret = phy_get_eee_err(pl->phydev);
1786 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1789 * phylink_init_eee() - init and check the EEE features
1790 * @pl: a pointer to a &struct phylink returned from phylink_create()
1791 * @clk_stop_enable: allow PHY to stop receive clock
1793 * Must be called either with RTNL held or within mac_link_up()
1795 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1797 int ret = -EOPNOTSUPP;
1800 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1804 EXPORT_SYMBOL_GPL(phylink_init_eee);
1807 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1808 * @pl: a pointer to a &struct phylink returned from phylink_create()
1809 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1811 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1813 int ret = -EOPNOTSUPP;
1818 ret = phy_ethtool_get_eee(pl->phydev, eee);
1822 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1825 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1826 * @pl: a pointer to a &struct phylink returned from phylink_create()
1827 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1829 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1831 int ret = -EOPNOTSUPP;
1836 ret = phy_ethtool_set_eee(pl->phydev, eee);
1840 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1842 /* This emulates MII registers for a fixed-mode phy operating as per the
1843 * passed in state. "aneg" defines if we report negotiation is possible.
1845 * FIXME: should deal with negotiation state too.
1847 static int phylink_mii_emul_read(unsigned int reg,
1848 struct phylink_link_state *state)
1850 struct fixed_phy_status fs;
1851 unsigned long *lpa = state->lp_advertising;
1854 fs.link = state->link;
1855 fs.speed = state->speed;
1856 fs.duplex = state->duplex;
1857 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1858 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
1860 val = swphy_read_reg(reg, &fs);
1861 if (reg == MII_BMSR) {
1862 if (!state->an_complete)
1863 val &= ~BMSR_ANEGCOMPLETE;
1868 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1871 struct phy_device *phydev = pl->phydev;
1874 if (mdio_phy_id_is_c45(phy_id)) {
1875 prtad = mdio_phy_id_prtad(phy_id);
1876 devad = mdio_phy_id_devad(phy_id);
1877 devad = mdiobus_c45_addr(devad, reg);
1878 } else if (phydev->is_c45) {
1884 devad = __ffs(phydev->c45_ids.mmds_present);
1888 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1890 devad = MDIO_MMD_AN;
1891 if (reg == MII_ADVERTISE)
1892 reg = MDIO_AN_ADVERTISE;
1900 devad = mdiobus_c45_addr(devad, reg);
1905 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1908 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1909 unsigned int reg, unsigned int val)
1911 struct phy_device *phydev = pl->phydev;
1914 if (mdio_phy_id_is_c45(phy_id)) {
1915 prtad = mdio_phy_id_prtad(phy_id);
1916 devad = mdio_phy_id_devad(phy_id);
1917 devad = mdiobus_c45_addr(devad, reg);
1918 } else if (phydev->is_c45) {
1924 devad = __ffs(phydev->c45_ids.mmds_present);
1928 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1930 devad = MDIO_MMD_AN;
1931 if (reg == MII_ADVERTISE)
1932 reg = MDIO_AN_ADVERTISE;
1940 devad = mdiobus_c45_addr(devad, reg);
1946 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1949 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1952 struct phylink_link_state state;
1955 switch (pl->cur_link_an_mode) {
1958 phylink_get_fixed_state(pl, &state);
1959 val = phylink_mii_emul_read(reg, &state);
1968 phylink_mac_pcs_get_state(pl, &state);
1969 val = phylink_mii_emul_read(reg, &state);
1974 return val & 0xffff;
1977 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1978 unsigned int reg, unsigned int val)
1980 switch (pl->cur_link_an_mode) {
1995 * phylink_mii_ioctl() - generic mii ioctl interface
1996 * @pl: a pointer to a &struct phylink returned from phylink_create()
1997 * @ifr: a pointer to a &struct ifreq for socket ioctls
1998 * @cmd: ioctl cmd to execute
2000 * Perform the specified MII ioctl on the PHY attached to the phylink instance
2001 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
2003 * Returns: zero on success or negative error code.
2006 * read register from the current PHY.
2008 * read register from the specified PHY.
2010 * set a register on the specified PHY.
2012 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
2014 struct mii_ioctl_data *mii = if_mii(ifr);
2020 /* PHYs only exist for MLO_AN_PHY and SGMII */
2023 mii->phy_id = pl->phydev->mdio.addr;
2027 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
2035 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
2040 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
2050 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
2058 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
2070 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
2073 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
2075 * @pl: a pointer to a &struct phylink returned from phylink_create()
2076 * @sync: perform action synchronously
2078 * If we have a PHY that is not part of a SFP module, then set the speed
2079 * as described in the phy_speed_down() function. Please see this function
2080 * for a description of the @sync parameter.
2082 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
2084 int phylink_speed_down(struct phylink *pl, bool sync)
2090 if (!pl->sfp_bus && pl->phydev)
2091 ret = phy_speed_down(pl->phydev, sync);
2095 EXPORT_SYMBOL_GPL(phylink_speed_down);
2098 * phylink_speed_up() - restore the advertised speeds prior to the call to
2099 * phylink_speed_down()
2100 * @pl: a pointer to a &struct phylink returned from phylink_create()
2102 * If we have a PHY that is not part of a SFP module, then restore the
2103 * PHY speeds as per phy_speed_up().
2105 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
2107 int phylink_speed_up(struct phylink *pl)
2113 if (!pl->sfp_bus && pl->phydev)
2114 ret = phy_speed_up(pl->phydev);
2118 EXPORT_SYMBOL_GPL(phylink_speed_up);
2120 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2122 struct phylink *pl = upstream;
2124 pl->netdev->sfp_bus = bus;
2127 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2129 struct phylink *pl = upstream;
2131 pl->netdev->sfp_bus = NULL;
2134 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2135 const unsigned long *supported,
2136 const unsigned long *advertising)
2138 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2139 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2140 struct phylink_link_state config;
2141 phy_interface_t iface;
2145 linkmode_copy(support, supported);
2147 memset(&config, 0, sizeof(config));
2148 linkmode_copy(config.advertising, advertising);
2149 config.interface = PHY_INTERFACE_MODE_NA;
2150 config.speed = SPEED_UNKNOWN;
2151 config.duplex = DUPLEX_UNKNOWN;
2152 config.pause = MLO_PAUSE_AN;
2153 config.an_enabled = pl->link_config.an_enabled;
2155 /* Ignore errors if we're expecting a PHY to attach later */
2156 ret = phylink_validate(pl, support, &config);
2158 phylink_err(pl, "validation with support %*pb failed: %d\n",
2159 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2163 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2164 if (iface == PHY_INTERFACE_MODE_NA) {
2166 "selection of interface failed, advertisement %*pb\n",
2167 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2171 config.interface = iface;
2172 linkmode_copy(support1, support);
2173 ret = phylink_validate(pl, support1, &config);
2175 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
2176 phylink_an_mode_str(mode),
2177 phy_modes(config.interface),
2178 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2182 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2183 phylink_an_mode_str(mode), phy_modes(config.interface),
2184 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2186 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2189 changed = !linkmode_equal(pl->supported, support);
2191 linkmode_copy(pl->supported, support);
2192 linkmode_copy(pl->link_config.advertising, config.advertising);
2195 if (pl->cur_link_an_mode != mode ||
2196 pl->link_config.interface != config.interface) {
2197 pl->link_config.interface = config.interface;
2198 pl->cur_link_an_mode = mode;
2202 phylink_info(pl, "switched to %s/%s link mode\n",
2203 phylink_an_mode_str(mode),
2204 phy_modes(config.interface));
2207 pl->link_port = pl->sfp_port;
2209 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2210 &pl->phylink_disable_state))
2211 phylink_mac_initial_config(pl, false);
2216 static int phylink_sfp_module_insert(void *upstream,
2217 const struct sfp_eeprom_id *id)
2219 struct phylink *pl = upstream;
2220 unsigned long *support = pl->sfp_support;
2224 linkmode_zero(support);
2225 sfp_parse_support(pl->sfp_bus, id, support);
2226 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2228 /* If this module may have a PHY connecting later, defer until later */
2229 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2230 if (pl->sfp_may_have_phy)
2233 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2236 static int phylink_sfp_module_start(void *upstream)
2238 struct phylink *pl = upstream;
2240 /* If this SFP module has a PHY, start the PHY now. */
2242 phy_start(pl->phydev);
2246 /* If the module may have a PHY but we didn't detect one we
2247 * need to configure the MAC here.
2249 if (!pl->sfp_may_have_phy)
2252 return phylink_sfp_config(pl, MLO_AN_INBAND,
2253 pl->sfp_support, pl->sfp_support);
2256 static void phylink_sfp_module_stop(void *upstream)
2258 struct phylink *pl = upstream;
2260 /* If this SFP module has a PHY, stop it. */
2262 phy_stop(pl->phydev);
2265 static void phylink_sfp_link_down(void *upstream)
2267 struct phylink *pl = upstream;
2271 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2274 static void phylink_sfp_link_up(void *upstream)
2276 struct phylink *pl = upstream;
2280 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2281 phylink_run_resolve(pl);
2284 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2285 * or 802.3z control word, so inband will not work.
2287 static bool phylink_phy_no_inband(struct phy_device *phy)
2289 return phy->is_c45 &&
2290 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2293 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2295 struct phylink *pl = upstream;
2296 phy_interface_t interface;
2301 * This is the new way of dealing with flow control for PHYs,
2302 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2303 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2304 * using our validate call to the MAC, we rely upon the MAC
2305 * clearing the bits from both supported and advertising fields.
2307 phy_support_asym_pause(phy);
2309 if (phylink_phy_no_inband(phy))
2312 mode = MLO_AN_INBAND;
2314 /* Do the initial configuration */
2315 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2319 interface = pl->link_config.interface;
2320 ret = phylink_attach_phy(pl, phy, interface);
2324 ret = phylink_bringup_phy(pl, phy, interface);
2331 static void phylink_sfp_disconnect_phy(void *upstream)
2333 phylink_disconnect_phy(upstream);
2336 static const struct sfp_upstream_ops sfp_phylink_ops = {
2337 .attach = phylink_sfp_attach,
2338 .detach = phylink_sfp_detach,
2339 .module_insert = phylink_sfp_module_insert,
2340 .module_start = phylink_sfp_module_start,
2341 .module_stop = phylink_sfp_module_stop,
2342 .link_up = phylink_sfp_link_up,
2343 .link_down = phylink_sfp_link_down,
2344 .connect_phy = phylink_sfp_connect_phy,
2345 .disconnect_phy = phylink_sfp_disconnect_phy,
2348 /* Helpers for MAC drivers */
2351 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2352 * @state: a pointer to a &struct phylink_link_state
2354 * Inspect the interface mode, advertising mask or forced speed and
2355 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2356 * the interface mode to suit. @state->interface is appropriately
2357 * updated, and the advertising mask has the "other" baseX_Full flag
2360 void phylink_helper_basex_speed(struct phylink_link_state *state)
2362 if (phy_interface_mode_is_8023z(state->interface)) {
2363 bool want_2500 = state->an_enabled ?
2364 phylink_test(state->advertising, 2500baseX_Full) :
2365 state->speed == SPEED_2500;
2368 phylink_clear(state->advertising, 1000baseX_Full);
2369 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2371 phylink_clear(state->advertising, 2500baseX_Full);
2372 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2376 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2378 static void phylink_decode_c37_word(struct phylink_link_state *state,
2379 uint16_t config_reg, int speed)
2381 bool tx_pause, rx_pause;
2384 if (speed == SPEED_2500)
2385 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2387 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2389 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2391 if (linkmode_test_bit(fd_bit, state->advertising) &&
2392 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2393 state->speed = speed;
2394 state->duplex = DUPLEX_FULL;
2396 /* negotiation failure */
2397 state->link = false;
2400 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2401 &tx_pause, &rx_pause);
2404 state->pause |= MLO_PAUSE_TX;
2406 state->pause |= MLO_PAUSE_RX;
2409 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2410 uint16_t config_reg)
2412 if (!(config_reg & LPA_SGMII_LINK)) {
2413 state->link = false;
2417 switch (config_reg & LPA_SGMII_SPD_MASK) {
2419 state->speed = SPEED_10;
2422 state->speed = SPEED_100;
2424 case LPA_SGMII_1000:
2425 state->speed = SPEED_1000;
2428 state->link = false;
2431 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2432 state->duplex = DUPLEX_FULL;
2434 state->duplex = DUPLEX_HALF;
2438 * phylink_decode_usxgmii_word() - decode the USXGMII word from a MAC PCS
2439 * @state: a pointer to a struct phylink_link_state.
2440 * @lpa: a 16 bit value which stores the USXGMII auto-negotiation word
2442 * Helper for MAC PCS supporting the USXGMII protocol and the auto-negotiation
2443 * code word. Decode the USXGMII code word and populate the corresponding fields
2444 * (speed, duplex) into the phylink_link_state structure.
2446 void phylink_decode_usxgmii_word(struct phylink_link_state *state,
2449 switch (lpa & MDIO_USXGMII_SPD_MASK) {
2450 case MDIO_USXGMII_10:
2451 state->speed = SPEED_10;
2453 case MDIO_USXGMII_100:
2454 state->speed = SPEED_100;
2456 case MDIO_USXGMII_1000:
2457 state->speed = SPEED_1000;
2459 case MDIO_USXGMII_2500:
2460 state->speed = SPEED_2500;
2462 case MDIO_USXGMII_5000:
2463 state->speed = SPEED_5000;
2465 case MDIO_USXGMII_10G:
2466 state->speed = SPEED_10000;
2469 state->link = false;
2473 if (lpa & MDIO_USXGMII_FULL_DUPLEX)
2474 state->duplex = DUPLEX_FULL;
2476 state->duplex = DUPLEX_HALF;
2478 EXPORT_SYMBOL_GPL(phylink_decode_usxgmii_word);
2481 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2482 * @pcs: a pointer to a &struct mdio_device.
2483 * @state: a pointer to a &struct phylink_link_state.
2485 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2486 * clause 37 negotiation and/or SGMII control.
2488 * Read the MAC PCS state from the MII device configured in @config and
2489 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2490 * the phylink @state structure. This is suitable to be directly plugged
2491 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2494 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2495 struct phylink_link_state *state)
2497 struct mii_bus *bus = pcs->bus;
2498 int addr = pcs->addr;
2501 bmsr = mdiobus_read(bus, addr, MII_BMSR);
2502 lpa = mdiobus_read(bus, addr, MII_LPA);
2503 if (bmsr < 0 || lpa < 0) {
2504 state->link = false;
2508 state->link = !!(bmsr & BMSR_LSTATUS);
2509 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2513 switch (state->interface) {
2514 case PHY_INTERFACE_MODE_1000BASEX:
2515 phylink_decode_c37_word(state, lpa, SPEED_1000);
2518 case PHY_INTERFACE_MODE_2500BASEX:
2519 phylink_decode_c37_word(state, lpa, SPEED_2500);
2522 case PHY_INTERFACE_MODE_SGMII:
2523 case PHY_INTERFACE_MODE_QSGMII:
2524 phylink_decode_sgmii_word(state, lpa);
2528 state->link = false;
2532 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2535 * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2537 * @pcs: a pointer to a &struct mdio_device.
2538 * @interface: the PHY interface mode being configured
2539 * @advertising: the ethtool advertisement mask
2541 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2542 * clause 37 negotiation and/or SGMII control.
2544 * Configure the clause 37 PCS advertisement as specified by @state. This
2545 * does not trigger a renegotiation; phylink will do that via the
2546 * mac_an_restart() method of the struct phylink_mac_ops structure.
2548 * Returns negative error code on failure to configure the advertisement,
2549 * zero if no change has been made, or one if the advertisement has changed.
2551 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
2552 phy_interface_t interface,
2553 const unsigned long *advertising)
2555 struct mii_bus *bus = pcs->bus;
2556 int addr = pcs->addr;
2560 switch (interface) {
2561 case PHY_INTERFACE_MODE_1000BASEX:
2562 case PHY_INTERFACE_MODE_2500BASEX:
2563 adv = ADVERTISE_1000XFULL;
2564 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2566 adv |= ADVERTISE_1000XPAUSE;
2567 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2569 adv |= ADVERTISE_1000XPSE_ASYM;
2571 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2578 ret = mdiobus_write(bus, addr, MII_ADVERTISE, adv);
2584 case PHY_INTERFACE_MODE_SGMII:
2585 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2592 ret = mdiobus_write(bus, addr, MII_ADVERTISE, 0x0001);
2599 /* Nothing to do for other modes */
2603 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2606 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
2607 * @pcs: a pointer to a &struct mdio_device.
2608 * @mode: link autonegotiation mode
2609 * @interface: the PHY interface mode being configured
2610 * @advertising: the ethtool advertisement mask
2612 * Configure a Clause 22 PCS PHY with the appropriate negotiation
2613 * parameters for the @mode, @interface and @advertising parameters.
2614 * Returns negative error number on failure, zero if the advertisement
2615 * has not changed, or positive if there is a change.
2617 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
2618 phy_interface_t interface,
2619 const unsigned long *advertising)
2625 ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface,
2632 /* Ensure ISOLATE bit is disabled */
2633 bmcr = mode == MLO_AN_INBAND ? BMCR_ANENABLE : 0;
2634 ret = mdiobus_modify(pcs->bus, pcs->addr, MII_BMCR,
2635 BMCR_ANENABLE | BMCR_ISOLATE, bmcr);
2639 return changed ? 1 : 0;
2641 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
2644 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2645 * @pcs: a pointer to a &struct mdio_device.
2647 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2648 * clause 37 negotiation.
2650 * Restart the clause 37 negotiation with the link partner. This is
2651 * suitable to be directly plugged into the mac_pcs_get_state() member
2652 * of the struct phylink_mac_ops structure.
2654 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2656 struct mii_bus *bus = pcs->bus;
2657 int val, addr = pcs->addr;
2659 val = mdiobus_read(bus, addr, MII_BMCR);
2661 val |= BMCR_ANRESTART;
2663 mdiobus_write(bus, addr, MII_BMCR, val);
2666 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2668 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2669 struct phylink_link_state *state)
2671 struct mii_bus *bus = pcs->bus;
2672 int addr = pcs->addr;
2675 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
2677 state->link = false;
2681 state->link = !!(stat & MDIO_STAT1_LSTATUS);
2685 switch (state->interface) {
2686 case PHY_INTERFACE_MODE_10GBASER:
2687 state->speed = SPEED_10000;
2688 state->duplex = DUPLEX_FULL;
2695 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2697 MODULE_LICENSE("GPL v2");