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/ethtool.h>
9 #include <linux/export.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/netdevice.h>
13 #include <linux/of_mdio.h>
14 #include <linux/phy.h>
15 #include <linux/phy_fixed.h>
16 #include <linux/phylink.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/spinlock.h>
19 #include <linux/timer.h>
20 #include <linux/workqueue.h>
25 #define SUPPORTED_INTERFACES \
26 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
27 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
28 #define ADVERTISED_INTERFACES \
29 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
30 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
33 PHYLINK_DISABLE_STOPPED,
38 * struct phylink - internal data type for phylink
42 struct net_device *netdev;
43 const struct phylink_mac_ops *mac_ops;
44 const struct phylink_pcs_ops *pcs_ops;
45 struct phylink_config *config;
46 struct phylink_pcs *pcs;
48 unsigned int old_link_state:1;
50 unsigned long phylink_disable_state; /* bitmask of disables */
51 struct phy_device *phydev;
52 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
53 u8 cfg_link_an_mode; /* MLO_AN_xxx */
55 u8 link_port; /* The current non-phy ethtool port */
56 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
58 /* The link configuration settings */
59 struct phylink_link_state link_config;
61 /* The current settings */
62 phy_interface_t cur_interface;
64 struct gpio_desc *link_gpio;
65 unsigned int link_irq;
66 struct timer_list link_poll;
67 void (*get_fixed_state)(struct net_device *dev,
68 struct phylink_link_state *s);
70 struct mutex state_mutex;
71 struct phylink_link_state phy_state;
72 struct work_struct resolve;
74 bool mac_link_dropped;
76 struct sfp_bus *sfp_bus;
77 bool sfp_may_have_phy;
78 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
82 #define phylink_printk(level, pl, fmt, ...) \
84 if ((pl)->config->type == PHYLINK_NETDEV) \
85 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
86 else if ((pl)->config->type == PHYLINK_DEV) \
87 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
90 #define phylink_err(pl, fmt, ...) \
91 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
92 #define phylink_warn(pl, fmt, ...) \
93 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
94 #define phylink_info(pl, fmt, ...) \
95 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
96 #if defined(CONFIG_DYNAMIC_DEBUG)
97 #define phylink_dbg(pl, fmt, ...) \
99 if ((pl)->config->type == PHYLINK_NETDEV) \
100 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
101 else if ((pl)->config->type == PHYLINK_DEV) \
102 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
105 #define phylink_dbg(pl, fmt, ...) \
106 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
108 #define phylink_dbg(pl, fmt, ...) \
111 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
116 * phylink_set_port_modes() - set the port type modes in the ethtool mask
117 * @mask: ethtool link mode mask
119 * Sets all the port type modes in the ethtool mask. MAC drivers should
120 * use this in their 'validate' callback.
122 void phylink_set_port_modes(unsigned long *mask)
124 phylink_set(mask, TP);
125 phylink_set(mask, AUI);
126 phylink_set(mask, MII);
127 phylink_set(mask, FIBRE);
128 phylink_set(mask, BNC);
129 phylink_set(mask, Backplane);
131 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
133 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
135 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
137 phylink_set_port_modes(tmp);
138 phylink_set(tmp, Autoneg);
139 phylink_set(tmp, Pause);
140 phylink_set(tmp, Asym_Pause);
142 return linkmode_subset(linkmode, tmp);
145 static const char *phylink_an_mode_str(unsigned int mode)
147 static const char *modestr[] = {
148 [MLO_AN_PHY] = "phy",
149 [MLO_AN_FIXED] = "fixed",
150 [MLO_AN_INBAND] = "inband",
153 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
156 static int phylink_validate(struct phylink *pl, unsigned long *supported,
157 struct phylink_link_state *state)
159 pl->mac_ops->validate(pl->config, supported, state);
161 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
164 static int phylink_parse_fixedlink(struct phylink *pl,
165 struct fwnode_handle *fwnode)
167 struct fwnode_handle *fixed_node;
168 const struct phy_setting *s;
169 struct gpio_desc *desc;
173 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
175 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
177 pl->link_config.speed = speed;
178 pl->link_config.duplex = DUPLEX_HALF;
180 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
181 pl->link_config.duplex = DUPLEX_FULL;
183 /* We treat the "pause" and "asym-pause" terminology as
184 * defining the link partner's ability. */
185 if (fwnode_property_read_bool(fixed_node, "pause"))
186 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
187 pl->link_config.lp_advertising);
188 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
189 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
190 pl->link_config.lp_advertising);
193 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
197 pl->link_gpio = desc;
198 else if (desc == ERR_PTR(-EPROBE_DEFER))
201 fwnode_handle_put(fixed_node);
208 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
210 if (ret != ARRAY_SIZE(prop)) {
211 phylink_err(pl, "broken fixed-link?\n");
215 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
216 prop, ARRAY_SIZE(prop));
218 pl->link_config.duplex = prop[1] ?
219 DUPLEX_FULL : DUPLEX_HALF;
220 pl->link_config.speed = prop[2];
222 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
223 pl->link_config.lp_advertising);
225 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
226 pl->link_config.lp_advertising);
230 if (pl->link_config.speed > SPEED_1000 &&
231 pl->link_config.duplex != DUPLEX_FULL)
232 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
233 pl->link_config.speed);
235 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
236 linkmode_copy(pl->link_config.advertising, pl->supported);
237 phylink_validate(pl, pl->supported, &pl->link_config);
239 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
240 pl->supported, true);
241 linkmode_zero(pl->supported);
242 phylink_set(pl->supported, MII);
243 phylink_set(pl->supported, Pause);
244 phylink_set(pl->supported, Asym_Pause);
245 phylink_set(pl->supported, Autoneg);
247 __set_bit(s->bit, pl->supported);
248 __set_bit(s->bit, pl->link_config.lp_advertising);
250 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
251 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
252 pl->link_config.speed);
255 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
258 pl->link_config.link = 1;
259 pl->link_config.an_complete = 1;
264 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
266 struct fwnode_handle *dn;
269 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
270 if (dn || fwnode_property_present(fwnode, "fixed-link"))
271 pl->cfg_link_an_mode = MLO_AN_FIXED;
272 fwnode_handle_put(dn);
274 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
275 strcmp(managed, "in-band-status") == 0) {
276 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
278 "can't use both fixed-link and in-band-status\n");
282 linkmode_zero(pl->supported);
283 phylink_set(pl->supported, MII);
284 phylink_set(pl->supported, Autoneg);
285 phylink_set(pl->supported, Asym_Pause);
286 phylink_set(pl->supported, Pause);
287 pl->link_config.an_enabled = true;
288 pl->cfg_link_an_mode = MLO_AN_INBAND;
290 switch (pl->link_config.interface) {
291 case PHY_INTERFACE_MODE_SGMII:
292 case PHY_INTERFACE_MODE_QSGMII:
293 phylink_set(pl->supported, 10baseT_Half);
294 phylink_set(pl->supported, 10baseT_Full);
295 phylink_set(pl->supported, 100baseT_Half);
296 phylink_set(pl->supported, 100baseT_Full);
297 phylink_set(pl->supported, 1000baseT_Half);
298 phylink_set(pl->supported, 1000baseT_Full);
301 case PHY_INTERFACE_MODE_1000BASEX:
302 phylink_set(pl->supported, 1000baseX_Full);
305 case PHY_INTERFACE_MODE_2500BASEX:
306 phylink_set(pl->supported, 2500baseX_Full);
309 case PHY_INTERFACE_MODE_USXGMII:
310 case PHY_INTERFACE_MODE_10GKR:
311 case PHY_INTERFACE_MODE_10GBASER:
312 phylink_set(pl->supported, 10baseT_Half);
313 phylink_set(pl->supported, 10baseT_Full);
314 phylink_set(pl->supported, 100baseT_Half);
315 phylink_set(pl->supported, 100baseT_Full);
316 phylink_set(pl->supported, 1000baseT_Half);
317 phylink_set(pl->supported, 1000baseT_Full);
318 phylink_set(pl->supported, 1000baseX_Full);
319 phylink_set(pl->supported, 1000baseKX_Full);
320 phylink_set(pl->supported, 2500baseT_Full);
321 phylink_set(pl->supported, 2500baseX_Full);
322 phylink_set(pl->supported, 5000baseT_Full);
323 phylink_set(pl->supported, 10000baseT_Full);
324 phylink_set(pl->supported, 10000baseKR_Full);
325 phylink_set(pl->supported, 10000baseKX4_Full);
326 phylink_set(pl->supported, 10000baseCR_Full);
327 phylink_set(pl->supported, 10000baseSR_Full);
328 phylink_set(pl->supported, 10000baseLR_Full);
329 phylink_set(pl->supported, 10000baseLRM_Full);
330 phylink_set(pl->supported, 10000baseER_Full);
333 case PHY_INTERFACE_MODE_XLGMII:
334 phylink_set(pl->supported, 25000baseCR_Full);
335 phylink_set(pl->supported, 25000baseKR_Full);
336 phylink_set(pl->supported, 25000baseSR_Full);
337 phylink_set(pl->supported, 40000baseKR4_Full);
338 phylink_set(pl->supported, 40000baseCR4_Full);
339 phylink_set(pl->supported, 40000baseSR4_Full);
340 phylink_set(pl->supported, 40000baseLR4_Full);
341 phylink_set(pl->supported, 50000baseCR2_Full);
342 phylink_set(pl->supported, 50000baseKR2_Full);
343 phylink_set(pl->supported, 50000baseSR2_Full);
344 phylink_set(pl->supported, 50000baseKR_Full);
345 phylink_set(pl->supported, 50000baseSR_Full);
346 phylink_set(pl->supported, 50000baseCR_Full);
347 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
348 phylink_set(pl->supported, 50000baseDR_Full);
349 phylink_set(pl->supported, 100000baseKR4_Full);
350 phylink_set(pl->supported, 100000baseSR4_Full);
351 phylink_set(pl->supported, 100000baseCR4_Full);
352 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
353 phylink_set(pl->supported, 100000baseKR2_Full);
354 phylink_set(pl->supported, 100000baseSR2_Full);
355 phylink_set(pl->supported, 100000baseCR2_Full);
356 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
357 phylink_set(pl->supported, 100000baseDR2_Full);
362 "incorrect link mode %s for in-band status\n",
363 phy_modes(pl->link_config.interface));
367 linkmode_copy(pl->link_config.advertising, pl->supported);
369 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
371 "failed to validate link configuration for in-band status\n");
375 /* Check if MAC/PCS also supports Autoneg. */
376 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
382 static void phylink_apply_manual_flow(struct phylink *pl,
383 struct phylink_link_state *state)
385 /* If autoneg is disabled, pause AN is also disabled */
386 if (!state->an_enabled)
387 state->pause &= ~MLO_PAUSE_AN;
389 /* Manual configuration of pause modes */
390 if (!(pl->link_config.pause & MLO_PAUSE_AN))
391 state->pause = pl->link_config.pause;
394 static void phylink_resolve_flow(struct phylink_link_state *state)
396 bool tx_pause, rx_pause;
398 state->pause = MLO_PAUSE_NONE;
399 if (state->duplex == DUPLEX_FULL) {
400 linkmode_resolve_pause(state->advertising,
401 state->lp_advertising,
402 &tx_pause, &rx_pause);
404 state->pause |= MLO_PAUSE_TX;
406 state->pause |= MLO_PAUSE_RX;
410 static void phylink_mac_config(struct phylink *pl,
411 const struct phylink_link_state *state)
414 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
415 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
416 phy_modes(state->interface),
417 phy_speed_to_str(state->speed),
418 phy_duplex_to_str(state->duplex),
419 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
420 state->pause, state->link, state->an_enabled);
422 pl->mac_ops->mac_config(pl->config, pl->cur_link_an_mode, state);
425 static void phylink_mac_pcs_an_restart(struct phylink *pl)
427 if (pl->link_config.an_enabled &&
428 phy_interface_mode_is_8023z(pl->link_config.interface) &&
429 phylink_autoneg_inband(pl->cur_link_an_mode)) {
431 pl->pcs_ops->pcs_an_restart(pl->pcs);
433 pl->mac_ops->mac_an_restart(pl->config);
437 static void phylink_major_config(struct phylink *pl, bool restart,
438 const struct phylink_link_state *state)
442 phylink_dbg(pl, "major config %s\n", phy_modes(state->interface));
444 if (pl->mac_ops->mac_prepare) {
445 err = pl->mac_ops->mac_prepare(pl->config, pl->cur_link_an_mode,
448 phylink_err(pl, "mac_prepare failed: %pe\n",
454 phylink_mac_config(pl, state);
457 err = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
460 !!(pl->link_config.pause &
463 phylink_err(pl, "pcs_config failed: %pe\n",
469 phylink_mac_pcs_an_restart(pl);
471 if (pl->mac_ops->mac_finish) {
472 err = pl->mac_ops->mac_finish(pl->config, pl->cur_link_an_mode,
475 phylink_err(pl, "mac_prepare failed: %pe\n",
481 * Reconfigure for a change of inband advertisement.
482 * If we have a separate PCS, we only need to call its pcs_config() method,
483 * and then restart AN if it indicates something changed. Otherwise, we do
484 * the full MAC reconfiguration.
486 static int phylink_change_inband_advert(struct phylink *pl)
490 if (test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state))
495 phylink_mac_config(pl, &pl->link_config);
496 phylink_mac_pcs_an_restart(pl);
500 phylink_dbg(pl, "%s: mode=%s/%s adv=%*pb pause=%02x\n", __func__,
501 phylink_an_mode_str(pl->cur_link_an_mode),
502 phy_modes(pl->link_config.interface),
503 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->link_config.advertising,
504 pl->link_config.pause);
506 /* Modern PCS-based method; update the advert at the PCS, and
507 * restart negotiation if the pcs_config() helper indicates that
508 * the programmed advertisement has changed.
510 ret = pl->pcs_ops->pcs_config(pl->pcs, pl->cur_link_an_mode,
511 pl->link_config.interface,
512 pl->link_config.advertising,
513 !!(pl->link_config.pause & MLO_PAUSE_AN));
518 phylink_mac_pcs_an_restart(pl);
523 static void phylink_mac_pcs_get_state(struct phylink *pl,
524 struct phylink_link_state *state)
526 linkmode_copy(state->advertising, pl->link_config.advertising);
527 linkmode_zero(state->lp_advertising);
528 state->interface = pl->link_config.interface;
529 state->an_enabled = pl->link_config.an_enabled;
530 state->speed = SPEED_UNKNOWN;
531 state->duplex = DUPLEX_UNKNOWN;
532 state->pause = MLO_PAUSE_NONE;
533 state->an_complete = 0;
537 pl->pcs_ops->pcs_get_state(pl->pcs, state);
539 pl->mac_ops->mac_pcs_get_state(pl->config, state);
542 /* The fixed state is... fixed except for the link state,
543 * which may be determined by a GPIO or a callback.
545 static void phylink_get_fixed_state(struct phylink *pl,
546 struct phylink_link_state *state)
548 *state = pl->link_config;
549 if (pl->config->get_fixed_state)
550 pl->config->get_fixed_state(pl->config, state);
551 else if (pl->link_gpio)
552 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
554 phylink_resolve_flow(state);
557 static void phylink_mac_initial_config(struct phylink *pl, bool force_restart)
559 struct phylink_link_state link_state;
561 switch (pl->cur_link_an_mode) {
563 link_state = pl->phy_state;
567 phylink_get_fixed_state(pl, &link_state);
571 link_state = pl->link_config;
572 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
573 link_state.pause = MLO_PAUSE_NONE;
576 default: /* can't happen */
580 link_state.link = false;
582 phylink_apply_manual_flow(pl, &link_state);
583 phylink_major_config(pl, force_restart, &link_state);
586 static const char *phylink_pause_to_str(int pause)
588 switch (pause & MLO_PAUSE_TXRX_MASK) {
589 case MLO_PAUSE_TX | MLO_PAUSE_RX:
600 static void phylink_link_up(struct phylink *pl,
601 struct phylink_link_state link_state)
603 struct net_device *ndev = pl->netdev;
605 pl->cur_interface = link_state.interface;
607 if (pl->pcs_ops && pl->pcs_ops->pcs_link_up)
608 pl->pcs_ops->pcs_link_up(pl->pcs, pl->cur_link_an_mode,
610 link_state.speed, link_state.duplex);
612 pl->mac_ops->mac_link_up(pl->config, pl->phydev,
613 pl->cur_link_an_mode, pl->cur_interface,
614 link_state.speed, link_state.duplex,
615 !!(link_state.pause & MLO_PAUSE_TX),
616 !!(link_state.pause & MLO_PAUSE_RX));
619 netif_carrier_on(ndev);
622 "Link is Up - %s/%s - flow control %s\n",
623 phy_speed_to_str(link_state.speed),
624 phy_duplex_to_str(link_state.duplex),
625 phylink_pause_to_str(link_state.pause));
628 static void phylink_link_down(struct phylink *pl)
630 struct net_device *ndev = pl->netdev;
633 netif_carrier_off(ndev);
634 pl->mac_ops->mac_link_down(pl->config, pl->cur_link_an_mode,
636 phylink_info(pl, "Link is Down\n");
639 static void phylink_resolve(struct work_struct *w)
641 struct phylink *pl = container_of(w, struct phylink, resolve);
642 struct phylink_link_state link_state;
643 struct net_device *ndev = pl->netdev;
644 bool mac_config = false;
647 mutex_lock(&pl->state_mutex);
649 cur_link_state = netif_carrier_ok(ndev);
651 cur_link_state = pl->old_link_state;
653 if (pl->phylink_disable_state) {
654 pl->mac_link_dropped = false;
655 link_state.link = false;
656 } else if (pl->mac_link_dropped) {
657 link_state.link = false;
659 switch (pl->cur_link_an_mode) {
661 link_state = pl->phy_state;
662 phylink_apply_manual_flow(pl, &link_state);
663 mac_config = link_state.link;
667 phylink_get_fixed_state(pl, &link_state);
668 mac_config = link_state.link;
672 phylink_mac_pcs_get_state(pl, &link_state);
674 /* If we have a phy, the "up" state is the union of
675 * both the PHY and the MAC */
677 link_state.link &= pl->phy_state.link;
679 /* Only update if the PHY link is up */
680 if (pl->phydev && pl->phy_state.link) {
681 link_state.interface = pl->phy_state.interface;
683 /* If we have a PHY, we need to update with
684 * the PHY flow control bits. */
685 link_state.pause = pl->phy_state.pause;
688 phylink_apply_manual_flow(pl, &link_state);
694 if (link_state.interface != pl->link_config.interface) {
695 /* The interface has changed, force the link down and
698 if (cur_link_state) {
699 phylink_link_down(pl);
700 cur_link_state = false;
702 phylink_major_config(pl, false, &link_state);
703 pl->link_config.interface = link_state.interface;
704 } else if (!pl->pcs_ops) {
705 /* The interface remains unchanged, only the speed,
706 * duplex or pause settings have changed. Call the
707 * old mac_config() method to configure the MAC/PCS
708 * only if we do not have a PCS installed (an
711 phylink_mac_config(pl, &link_state);
715 if (link_state.link != cur_link_state) {
716 pl->old_link_state = link_state.link;
717 if (!link_state.link)
718 phylink_link_down(pl);
720 phylink_link_up(pl, link_state);
722 if (!link_state.link && pl->mac_link_dropped) {
723 pl->mac_link_dropped = false;
724 queue_work(system_power_efficient_wq, &pl->resolve);
726 mutex_unlock(&pl->state_mutex);
729 static void phylink_run_resolve(struct phylink *pl)
731 if (!pl->phylink_disable_state)
732 queue_work(system_power_efficient_wq, &pl->resolve);
735 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
737 unsigned long state = pl->phylink_disable_state;
739 set_bit(bit, &pl->phylink_disable_state);
741 queue_work(system_power_efficient_wq, &pl->resolve);
742 flush_work(&pl->resolve);
746 static void phylink_fixed_poll(struct timer_list *t)
748 struct phylink *pl = container_of(t, struct phylink, link_poll);
750 mod_timer(t, jiffies + HZ);
752 phylink_run_resolve(pl);
755 static const struct sfp_upstream_ops sfp_phylink_ops;
757 static int phylink_register_sfp(struct phylink *pl,
758 struct fwnode_handle *fwnode)
766 bus = sfp_bus_find_fwnode(fwnode);
769 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
775 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
782 * phylink_create() - create a phylink instance
783 * @config: a pointer to the target &struct phylink_config
784 * @fwnode: a pointer to a &struct fwnode_handle describing the network
786 * @iface: the desired link mode defined by &typedef phy_interface_t
787 * @mac_ops: a pointer to a &struct phylink_mac_ops for the MAC.
789 * Create a new phylink instance, and parse the link parameters found in @np.
790 * This will parse in-band modes, fixed-link or SFP configuration.
792 * Note: the rtnl lock must not be held when calling this function.
794 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
795 * must use IS_ERR() to check for errors from this function.
797 struct phylink *phylink_create(struct phylink_config *config,
798 struct fwnode_handle *fwnode,
799 phy_interface_t iface,
800 const struct phylink_mac_ops *mac_ops)
805 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
807 return ERR_PTR(-ENOMEM);
809 mutex_init(&pl->state_mutex);
810 INIT_WORK(&pl->resolve, phylink_resolve);
813 if (config->type == PHYLINK_NETDEV) {
814 pl->netdev = to_net_dev(config->dev);
815 } else if (config->type == PHYLINK_DEV) {
816 pl->dev = config->dev;
819 return ERR_PTR(-EINVAL);
822 pl->phy_state.interface = iface;
823 pl->link_interface = iface;
824 if (iface == PHY_INTERFACE_MODE_MOCA)
825 pl->link_port = PORT_BNC;
827 pl->link_port = PORT_MII;
828 pl->link_config.interface = iface;
829 pl->link_config.pause = MLO_PAUSE_AN;
830 pl->link_config.speed = SPEED_UNKNOWN;
831 pl->link_config.duplex = DUPLEX_UNKNOWN;
832 pl->link_config.an_enabled = true;
833 pl->mac_ops = mac_ops;
834 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
835 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
837 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
838 linkmode_copy(pl->link_config.advertising, pl->supported);
839 phylink_validate(pl, pl->supported, &pl->link_config);
841 ret = phylink_parse_mode(pl, fwnode);
847 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
848 ret = phylink_parse_fixedlink(pl, fwnode);
855 pl->cur_link_an_mode = pl->cfg_link_an_mode;
857 ret = phylink_register_sfp(pl, fwnode);
865 EXPORT_SYMBOL_GPL(phylink_create);
868 * phylink_set_pcs() - set the current PCS for phylink to use
869 * @pl: a pointer to a &struct phylink returned from phylink_create()
870 * @pcs: a pointer to the &struct phylink_pcs
872 * Bind the MAC PCS to phylink. This may be called after phylink_create(),
873 * in mac_prepare() or mac_config() methods if it is desired to dynamically
876 * Please note that there are behavioural changes with the mac_config()
877 * callback if a PCS is present (denoting a newer setup) so removing a PCS
878 * is not supported, and if a PCS is going to be used, it must be registered
879 * by calling phylink_set_pcs() at the latest in the first mac_config() call.
881 void phylink_set_pcs(struct phylink *pl, struct phylink_pcs *pcs)
884 pl->pcs_ops = pcs->ops;
886 EXPORT_SYMBOL_GPL(phylink_set_pcs);
889 * phylink_destroy() - cleanup and destroy the phylink instance
890 * @pl: a pointer to a &struct phylink returned from phylink_create()
892 * Destroy a phylink instance. Any PHY that has been attached must have been
893 * cleaned up via phylink_disconnect_phy() prior to calling this function.
895 * Note: the rtnl lock must not be held when calling this function.
897 void phylink_destroy(struct phylink *pl)
899 sfp_bus_del_upstream(pl->sfp_bus);
901 gpiod_put(pl->link_gpio);
903 cancel_work_sync(&pl->resolve);
906 EXPORT_SYMBOL_GPL(phylink_destroy);
908 static void phylink_phy_change(struct phy_device *phydev, bool up)
910 struct phylink *pl = phydev->phylink;
911 bool tx_pause, rx_pause;
913 phy_get_pause(phydev, &tx_pause, &rx_pause);
915 mutex_lock(&pl->state_mutex);
916 pl->phy_state.speed = phydev->speed;
917 pl->phy_state.duplex = phydev->duplex;
918 pl->phy_state.pause = MLO_PAUSE_NONE;
920 pl->phy_state.pause |= MLO_PAUSE_TX;
922 pl->phy_state.pause |= MLO_PAUSE_RX;
923 pl->phy_state.interface = phydev->interface;
924 pl->phy_state.link = up;
925 mutex_unlock(&pl->state_mutex);
927 phylink_run_resolve(pl);
929 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
930 phy_modes(phydev->interface),
931 phy_speed_to_str(phydev->speed),
932 phy_duplex_to_str(phydev->duplex));
935 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
936 phy_interface_t interface)
938 struct phylink_link_state config;
939 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
944 * This is the new way of dealing with flow control for PHYs,
945 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
946 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
947 * using our validate call to the MAC, we rely upon the MAC
948 * clearing the bits from both supported and advertising fields.
950 phy_support_asym_pause(phy);
952 memset(&config, 0, sizeof(config));
953 linkmode_copy(supported, phy->supported);
954 linkmode_copy(config.advertising, phy->advertising);
956 /* Clause 45 PHYs switch their Serdes lane between several different
957 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
958 * speeds. We really need to know which interface modes the PHY and
959 * MAC supports to properly work out which linkmodes can be supported.
962 interface != PHY_INTERFACE_MODE_RXAUI &&
963 interface != PHY_INTERFACE_MODE_XAUI &&
964 interface != PHY_INTERFACE_MODE_USXGMII)
965 config.interface = PHY_INTERFACE_MODE_NA;
967 config.interface = interface;
969 ret = phylink_validate(pl, supported, &config);
971 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
972 phy_modes(config.interface),
973 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
974 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
980 phy->phy_link_change = phylink_phy_change;
982 irq_str = phy_attached_info_irq(phy);
984 "PHY [%s] driver [%s] (irq=%s)\n",
985 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
988 mutex_lock(&phy->lock);
989 mutex_lock(&pl->state_mutex);
991 pl->phy_state.interface = interface;
992 pl->phy_state.pause = MLO_PAUSE_NONE;
993 pl->phy_state.speed = SPEED_UNKNOWN;
994 pl->phy_state.duplex = DUPLEX_UNKNOWN;
995 linkmode_copy(pl->supported, supported);
996 linkmode_copy(pl->link_config.advertising, config.advertising);
998 /* Restrict the phy advertisement according to the MAC support. */
999 linkmode_copy(phy->advertising, config.advertising);
1000 mutex_unlock(&pl->state_mutex);
1001 mutex_unlock(&phy->lock);
1004 "phy: setting supported %*pb advertising %*pb\n",
1005 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
1006 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
1008 if (phy_interrupt_is_valid(phy))
1009 phy_request_interrupt(phy);
1014 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
1015 phy_interface_t interface)
1017 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
1018 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1019 phy_interface_mode_is_8023z(interface))))
1025 return phy_attach_direct(pl->netdev, phy, 0, interface);
1029 * phylink_connect_phy() - connect a PHY to the phylink instance
1030 * @pl: a pointer to a &struct phylink returned from phylink_create()
1031 * @phy: a pointer to a &struct phy_device.
1033 * Connect @phy to the phylink instance specified by @pl by calling
1034 * phy_attach_direct(). Configure the @phy according to the MAC driver's
1035 * capabilities, start the PHYLIB state machine and enable any interrupts
1036 * that the PHY supports.
1038 * This updates the phylink's ethtool supported and advertising link mode
1041 * Returns 0 on success or a negative errno.
1043 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
1047 /* Use PHY device/driver interface */
1048 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
1049 pl->link_interface = phy->interface;
1050 pl->link_config.interface = pl->link_interface;
1053 ret = phylink_attach_phy(pl, phy, pl->link_interface);
1057 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
1063 EXPORT_SYMBOL_GPL(phylink_connect_phy);
1066 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
1067 * @pl: a pointer to a &struct phylink returned from phylink_create()
1068 * @dn: a pointer to a &struct device_node.
1069 * @flags: PHY-specific flags to communicate to the PHY device driver
1071 * Connect the phy specified in the device node @dn to the phylink instance
1072 * specified by @pl. Actions specified in phylink_connect_phy() will be
1075 * Returns 0 on success or a negative errno.
1077 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
1080 struct device_node *phy_node;
1081 struct phy_device *phy_dev;
1084 /* Fixed links and 802.3z are handled without needing a PHY */
1085 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
1086 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
1087 phy_interface_mode_is_8023z(pl->link_interface)))
1090 phy_node = of_parse_phandle(dn, "phy-handle", 0);
1092 phy_node = of_parse_phandle(dn, "phy", 0);
1094 phy_node = of_parse_phandle(dn, "phy-device", 0);
1097 if (pl->cfg_link_an_mode == MLO_AN_PHY)
1102 phy_dev = of_phy_find_device(phy_node);
1103 /* We're done with the phy_node handle */
1104 of_node_put(phy_node);
1108 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
1109 pl->link_interface);
1113 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
1115 phy_detach(phy_dev);
1119 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
1122 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
1124 * @pl: a pointer to a &struct phylink returned from phylink_create()
1126 * Disconnect any current PHY from the phylink instance described by @pl.
1128 void phylink_disconnect_phy(struct phylink *pl)
1130 struct phy_device *phy;
1136 mutex_lock(&phy->lock);
1137 mutex_lock(&pl->state_mutex);
1139 mutex_unlock(&pl->state_mutex);
1140 mutex_unlock(&phy->lock);
1141 flush_work(&pl->resolve);
1143 phy_disconnect(phy);
1146 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1149 * phylink_mac_change() - notify phylink of a change in MAC state
1150 * @pl: a pointer to a &struct phylink returned from phylink_create()
1151 * @up: indicates whether the link is currently up.
1153 * The MAC driver should call this driver when the state of its link
1154 * changes (eg, link failure, new negotiation results, etc.)
1156 void phylink_mac_change(struct phylink *pl, bool up)
1159 pl->mac_link_dropped = true;
1160 phylink_run_resolve(pl);
1161 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1163 EXPORT_SYMBOL_GPL(phylink_mac_change);
1165 static irqreturn_t phylink_link_handler(int irq, void *data)
1167 struct phylink *pl = data;
1169 phylink_run_resolve(pl);
1175 * phylink_start() - start a phylink instance
1176 * @pl: a pointer to a &struct phylink returned from phylink_create()
1178 * Start the phylink instance specified by @pl, configuring the MAC for the
1179 * desired link mode(s) and negotiation style. This should be called from the
1180 * network device driver's &struct net_device_ops ndo_open() method.
1182 void phylink_start(struct phylink *pl)
1188 phylink_info(pl, "configuring for %s/%s link mode\n",
1189 phylink_an_mode_str(pl->cur_link_an_mode),
1190 phy_modes(pl->link_config.interface));
1192 /* Always set the carrier off */
1194 netif_carrier_off(pl->netdev);
1196 /* Apply the link configuration to the MAC when starting. This allows
1197 * a fixed-link to start with the correct parameters, and also
1198 * ensures that we set the appropriate advertisement for Serdes links.
1200 * Restart autonegotiation if using 802.3z to ensure that the link
1201 * parameters are properly negotiated. This is necessary for DSA
1202 * switches using 802.3z negotiation to ensure they see our modes.
1204 phylink_mac_initial_config(pl, true);
1206 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1207 phylink_run_resolve(pl);
1209 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1210 int irq = gpiod_to_irq(pl->link_gpio);
1213 if (!request_irq(irq, phylink_link_handler,
1214 IRQF_TRIGGER_RISING |
1215 IRQF_TRIGGER_FALLING,
1225 switch (pl->cfg_link_an_mode) {
1227 poll |= pl->config->poll_fixed_state;
1230 poll |= pl->config->pcs_poll;
1232 poll |= pl->pcs->poll;
1236 mod_timer(&pl->link_poll, jiffies + HZ);
1238 phy_start(pl->phydev);
1240 sfp_upstream_start(pl->sfp_bus);
1242 EXPORT_SYMBOL_GPL(phylink_start);
1245 * phylink_stop() - stop a phylink instance
1246 * @pl: a pointer to a &struct phylink returned from phylink_create()
1248 * Stop the phylink instance specified by @pl. This should be called from the
1249 * network device driver's &struct net_device_ops ndo_stop() method. The
1250 * network device's carrier state should not be changed prior to calling this
1253 void phylink_stop(struct phylink *pl)
1258 sfp_upstream_stop(pl->sfp_bus);
1260 phy_stop(pl->phydev);
1261 del_timer_sync(&pl->link_poll);
1263 free_irq(pl->link_irq, pl);
1267 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1269 EXPORT_SYMBOL_GPL(phylink_stop);
1272 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1273 * @pl: a pointer to a &struct phylink returned from phylink_create()
1274 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1276 * Read the wake on lan parameters from the PHY attached to the phylink
1277 * instance specified by @pl. If no PHY is currently attached, report no
1278 * support for wake on lan.
1280 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1288 phy_ethtool_get_wol(pl->phydev, wol);
1290 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1293 * phylink_ethtool_set_wol() - set wake on lan parameters
1294 * @pl: a pointer to a &struct phylink returned from phylink_create()
1295 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1297 * Set the wake on lan parameters for the PHY attached to the phylink
1298 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1301 * Returns zero on success or negative errno code.
1303 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1305 int ret = -EOPNOTSUPP;
1310 ret = phy_ethtool_set_wol(pl->phydev, wol);
1314 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1316 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1318 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1320 linkmode_zero(mask);
1321 phylink_set_port_modes(mask);
1323 linkmode_and(dst, dst, mask);
1324 linkmode_or(dst, dst, b);
1327 static void phylink_get_ksettings(const struct phylink_link_state *state,
1328 struct ethtool_link_ksettings *kset)
1330 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1331 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1332 kset->base.speed = state->speed;
1333 kset->base.duplex = state->duplex;
1334 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1339 * phylink_ethtool_ksettings_get() - get the current link settings
1340 * @pl: a pointer to a &struct phylink returned from phylink_create()
1341 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1343 * Read the current link settings for the phylink instance specified by @pl.
1344 * This will be the link settings read from the MAC, PHY or fixed link
1345 * settings depending on the current negotiation mode.
1347 int phylink_ethtool_ksettings_get(struct phylink *pl,
1348 struct ethtool_link_ksettings *kset)
1350 struct phylink_link_state link_state;
1355 phy_ethtool_ksettings_get(pl->phydev, kset);
1357 kset->base.port = pl->link_port;
1360 linkmode_copy(kset->link_modes.supported, pl->supported);
1362 switch (pl->cur_link_an_mode) {
1364 /* We are using fixed settings. Report these as the
1365 * current link settings - and note that these also
1366 * represent the supported speeds/duplex/pause modes.
1368 phylink_get_fixed_state(pl, &link_state);
1369 phylink_get_ksettings(&link_state, kset);
1373 /* If there is a phy attached, then use the reported
1374 * settings from the phy with no modification.
1379 phylink_mac_pcs_get_state(pl, &link_state);
1381 /* The MAC is reporting the link results from its own PCS
1382 * layer via in-band status. Report these as the current
1385 phylink_get_ksettings(&link_state, kset);
1391 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1394 * phylink_ethtool_ksettings_set() - set the link settings
1395 * @pl: a pointer to a &struct phylink returned from phylink_create()
1396 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1398 int phylink_ethtool_ksettings_set(struct phylink *pl,
1399 const struct ethtool_link_ksettings *kset)
1401 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1402 struct phylink_link_state config;
1403 const struct phy_setting *s;
1408 /* We can rely on phylib for this update; we also do not need
1409 * to update the pl->link_config settings:
1410 * - the configuration returned via ksettings_get() will come
1411 * from phylib whenever a PHY is present.
1412 * - link_config.interface will be updated by the PHY calling
1413 * back via phylink_phy_change() and a subsequent resolve.
1414 * - initial link configuration for PHY mode comes from the
1415 * last phy state updated via phylink_phy_change().
1416 * - other configuration changes (e.g. pause modes) are
1417 * performed directly via phylib.
1418 * - if in in-band mode with a PHY, the link configuration
1419 * is passed on the link from the PHY, and all of
1420 * link_config.{speed,duplex,an_enabled,pause} are not used.
1421 * - the only possible use would be link_config.advertising
1422 * pause modes when in 1000base-X mode with a PHY, but in
1423 * the presence of a PHY, this should not be changed as that
1424 * should be determined from the media side advertisement.
1426 return phy_ethtool_ksettings_set(pl->phydev, kset);
1429 linkmode_copy(support, pl->supported);
1430 config = pl->link_config;
1431 config.an_enabled = kset->base.autoneg == AUTONEG_ENABLE;
1433 /* Mask out unsupported advertisements, and force the autoneg bit */
1434 linkmode_and(config.advertising, kset->link_modes.advertising,
1436 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising,
1439 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1440 switch (kset->base.autoneg) {
1441 case AUTONEG_DISABLE:
1442 /* Autonegotiation disabled, select a suitable speed and
1445 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1450 /* If we have a fixed link, refuse to change link parameters.
1451 * If the link parameters match, accept them but do nothing.
1453 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1454 if (s->speed != pl->link_config.speed ||
1455 s->duplex != pl->link_config.duplex)
1460 config.speed = s->speed;
1461 config.duplex = s->duplex;
1464 case AUTONEG_ENABLE:
1465 /* If we have a fixed link, allow autonegotiation (since that
1466 * is our default case) but do not allow the advertisement to
1467 * be changed. If the advertisement matches, simply return.
1469 if (pl->cur_link_an_mode == MLO_AN_FIXED) {
1470 if (!linkmode_equal(config.advertising,
1471 pl->link_config.advertising))
1476 config.speed = SPEED_UNKNOWN;
1477 config.duplex = DUPLEX_UNKNOWN;
1484 /* We have ruled out the case with a PHY attached, and the
1485 * fixed-link cases. All that is left are in-band links.
1487 if (phylink_validate(pl, support, &config))
1490 /* If autonegotiation is enabled, we must have an advertisement */
1491 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1494 mutex_lock(&pl->state_mutex);
1495 pl->link_config.speed = config.speed;
1496 pl->link_config.duplex = config.duplex;
1497 pl->link_config.an_enabled = config.an_enabled;
1499 if (pl->link_config.interface != config.interface) {
1500 /* The interface changed, e.g. 1000base-X <-> 2500base-X */
1501 /* We need to force the link down, then change the interface */
1502 if (pl->old_link_state) {
1503 phylink_link_down(pl);
1504 pl->old_link_state = false;
1506 if (!test_bit(PHYLINK_DISABLE_STOPPED,
1507 &pl->phylink_disable_state))
1508 phylink_major_config(pl, false, &config);
1509 pl->link_config.interface = config.interface;
1510 linkmode_copy(pl->link_config.advertising, config.advertising);
1511 } else if (!linkmode_equal(pl->link_config.advertising,
1512 config.advertising)) {
1513 linkmode_copy(pl->link_config.advertising, config.advertising);
1514 phylink_change_inband_advert(pl);
1516 mutex_unlock(&pl->state_mutex);
1520 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1523 * phylink_ethtool_nway_reset() - restart negotiation
1524 * @pl: a pointer to a &struct phylink returned from phylink_create()
1526 * Restart negotiation for the phylink instance specified by @pl. This will
1527 * cause any attached phy to restart negotiation with the link partner, and
1528 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1531 * Returns zero on success, or negative error code.
1533 int phylink_ethtool_nway_reset(struct phylink *pl)
1540 ret = phy_restart_aneg(pl->phydev);
1541 phylink_mac_pcs_an_restart(pl);
1545 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1548 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1549 * @pl: a pointer to a &struct phylink returned from phylink_create()
1550 * @pause: a pointer to a &struct ethtool_pauseparam
1552 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1553 struct ethtool_pauseparam *pause)
1557 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1558 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1559 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1561 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1564 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1565 * @pl: a pointer to a &struct phylink returned from phylink_create()
1566 * @pause: a pointer to a &struct ethtool_pauseparam
1568 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1569 struct ethtool_pauseparam *pause)
1571 struct phylink_link_state *config = &pl->link_config;
1572 bool manual_changed;
1577 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1580 if (!phylink_test(pl->supported, Pause) &&
1581 !phylink_test(pl->supported, Asym_Pause))
1584 if (!phylink_test(pl->supported, Asym_Pause) &&
1585 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1590 pause_state |= MLO_PAUSE_AN;
1591 if (pause->rx_pause)
1592 pause_state |= MLO_PAUSE_RX;
1593 if (pause->tx_pause)
1594 pause_state |= MLO_PAUSE_TX;
1596 mutex_lock(&pl->state_mutex);
1598 * See the comments for linkmode_set_pause(), wrt the deficiencies
1599 * with the current implementation. A solution to this issue would
1601 * ethtool Local device
1602 * rx tx Pause AsymDir
1607 * and then use the ethtool rx/tx enablement status to mask the
1608 * rx/tx pause resolution.
1610 linkmode_set_pause(config->advertising, pause->tx_pause,
1613 manual_changed = (config->pause ^ pause_state) & MLO_PAUSE_AN ||
1614 (!(pause_state & MLO_PAUSE_AN) &&
1615 (config->pause ^ pause_state) & MLO_PAUSE_TXRX_MASK);
1617 config->pause = pause_state;
1619 /* Update our in-band advertisement, triggering a renegotiation if
1620 * the advertisement changed.
1623 phylink_change_inband_advert(pl);
1625 mutex_unlock(&pl->state_mutex);
1627 /* If we have a PHY, a change of the pause frame advertisement will
1628 * cause phylib to renegotiate (if AN is enabled) which will in turn
1629 * call our phylink_phy_change() and trigger a resolve. Note that
1630 * we can't hold our state mutex while calling phy_set_asym_pause().
1633 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1636 /* If the manual pause settings changed, make sure we trigger a
1637 * resolve to update their state; we can not guarantee that the
1640 if (manual_changed) {
1641 pl->mac_link_dropped = true;
1642 phylink_run_resolve(pl);
1647 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1650 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1652 * @pl: a pointer to a &struct phylink returned from phylink_create().
1654 * Read the Energy Efficient Ethernet error counter from the PHY associated
1655 * with the phylink instance specified by @pl.
1657 * Returns positive error counter value, or negative error code.
1659 int phylink_get_eee_err(struct phylink *pl)
1666 ret = phy_get_eee_err(pl->phydev);
1670 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1673 * phylink_init_eee() - init and check the EEE features
1674 * @pl: a pointer to a &struct phylink returned from phylink_create()
1675 * @clk_stop_enable: allow PHY to stop receive clock
1677 * Must be called either with RTNL held or within mac_link_up()
1679 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1681 int ret = -EOPNOTSUPP;
1684 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1688 EXPORT_SYMBOL_GPL(phylink_init_eee);
1691 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1692 * @pl: a pointer to a &struct phylink returned from phylink_create()
1693 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1695 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1697 int ret = -EOPNOTSUPP;
1702 ret = phy_ethtool_get_eee(pl->phydev, eee);
1706 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1709 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1710 * @pl: a pointer to a &struct phylink returned from phylink_create()
1711 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1713 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1715 int ret = -EOPNOTSUPP;
1720 ret = phy_ethtool_set_eee(pl->phydev, eee);
1724 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1726 /* This emulates MII registers for a fixed-mode phy operating as per the
1727 * passed in state. "aneg" defines if we report negotiation is possible.
1729 * FIXME: should deal with negotiation state too.
1731 static int phylink_mii_emul_read(unsigned int reg,
1732 struct phylink_link_state *state)
1734 struct fixed_phy_status fs;
1735 unsigned long *lpa = state->lp_advertising;
1738 fs.link = state->link;
1739 fs.speed = state->speed;
1740 fs.duplex = state->duplex;
1741 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1742 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
1744 val = swphy_read_reg(reg, &fs);
1745 if (reg == MII_BMSR) {
1746 if (!state->an_complete)
1747 val &= ~BMSR_ANEGCOMPLETE;
1752 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1755 struct phy_device *phydev = pl->phydev;
1758 if (mdio_phy_id_is_c45(phy_id)) {
1759 prtad = mdio_phy_id_prtad(phy_id);
1760 devad = mdio_phy_id_devad(phy_id);
1761 devad = mdiobus_c45_addr(devad, reg);
1762 } else if (phydev->is_c45) {
1768 devad = __ffs(phydev->c45_ids.mmds_present);
1772 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1774 devad = MDIO_MMD_AN;
1775 if (reg == MII_ADVERTISE)
1776 reg = MDIO_AN_ADVERTISE;
1784 devad = mdiobus_c45_addr(devad, reg);
1789 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1792 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1793 unsigned int reg, unsigned int val)
1795 struct phy_device *phydev = pl->phydev;
1798 if (mdio_phy_id_is_c45(phy_id)) {
1799 prtad = mdio_phy_id_prtad(phy_id);
1800 devad = mdio_phy_id_devad(phy_id);
1801 devad = mdiobus_c45_addr(devad, reg);
1802 } else if (phydev->is_c45) {
1808 devad = __ffs(phydev->c45_ids.mmds_present);
1812 if (!(phydev->c45_ids.mmds_present & MDIO_DEVS_AN))
1814 devad = MDIO_MMD_AN;
1815 if (reg == MII_ADVERTISE)
1816 reg = MDIO_AN_ADVERTISE;
1824 devad = mdiobus_c45_addr(devad, reg);
1830 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1833 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1836 struct phylink_link_state state;
1839 switch (pl->cur_link_an_mode) {
1842 phylink_get_fixed_state(pl, &state);
1843 val = phylink_mii_emul_read(reg, &state);
1852 phylink_mac_pcs_get_state(pl, &state);
1853 val = phylink_mii_emul_read(reg, &state);
1858 return val & 0xffff;
1861 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1862 unsigned int reg, unsigned int val)
1864 switch (pl->cur_link_an_mode) {
1879 * phylink_mii_ioctl() - generic mii ioctl interface
1880 * @pl: a pointer to a &struct phylink returned from phylink_create()
1881 * @ifr: a pointer to a &struct ifreq for socket ioctls
1882 * @cmd: ioctl cmd to execute
1884 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1885 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1887 * Returns: zero on success or negative error code.
1890 * read register from the current PHY.
1892 * read register from the specified PHY.
1894 * set a register on the specified PHY.
1896 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1898 struct mii_ioctl_data *mii = if_mii(ifr);
1904 /* PHYs only exist for MLO_AN_PHY and SGMII */
1907 mii->phy_id = pl->phydev->mdio.addr;
1911 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1919 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1924 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1934 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1942 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1954 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1957 * phylink_speed_down() - set the non-SFP PHY to lowest speed supported by both
1959 * @pl: a pointer to a &struct phylink returned from phylink_create()
1960 * @sync: perform action synchronously
1962 * If we have a PHY that is not part of a SFP module, then set the speed
1963 * as described in the phy_speed_down() function. Please see this function
1964 * for a description of the @sync parameter.
1966 * Returns zero if there is no PHY, otherwise as per phy_speed_down().
1968 int phylink_speed_down(struct phylink *pl, bool sync)
1974 if (!pl->sfp_bus && pl->phydev)
1975 ret = phy_speed_down(pl->phydev, sync);
1979 EXPORT_SYMBOL_GPL(phylink_speed_down);
1982 * phylink_speed_up() - restore the advertised speeds prior to the call to
1983 * phylink_speed_down()
1984 * @pl: a pointer to a &struct phylink returned from phylink_create()
1986 * If we have a PHY that is not part of a SFP module, then restore the
1987 * PHY speeds as per phy_speed_up().
1989 * Returns zero if there is no PHY, otherwise as per phy_speed_up().
1991 int phylink_speed_up(struct phylink *pl)
1997 if (!pl->sfp_bus && pl->phydev)
1998 ret = phy_speed_up(pl->phydev);
2002 EXPORT_SYMBOL_GPL(phylink_speed_up);
2004 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
2006 struct phylink *pl = upstream;
2008 pl->netdev->sfp_bus = bus;
2011 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
2013 struct phylink *pl = upstream;
2015 pl->netdev->sfp_bus = NULL;
2018 static int phylink_sfp_config(struct phylink *pl, u8 mode,
2019 const unsigned long *supported,
2020 const unsigned long *advertising)
2022 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
2023 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
2024 struct phylink_link_state config;
2025 phy_interface_t iface;
2029 linkmode_copy(support, supported);
2031 memset(&config, 0, sizeof(config));
2032 linkmode_copy(config.advertising, advertising);
2033 config.interface = PHY_INTERFACE_MODE_NA;
2034 config.speed = SPEED_UNKNOWN;
2035 config.duplex = DUPLEX_UNKNOWN;
2036 config.pause = MLO_PAUSE_AN;
2037 config.an_enabled = pl->link_config.an_enabled;
2039 /* Ignore errors if we're expecting a PHY to attach later */
2040 ret = phylink_validate(pl, support, &config);
2042 phylink_err(pl, "validation with support %*pb failed: %d\n",
2043 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2047 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
2048 if (iface == PHY_INTERFACE_MODE_NA) {
2050 "selection of interface failed, advertisement %*pb\n",
2051 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
2055 config.interface = iface;
2056 linkmode_copy(support1, support);
2057 ret = phylink_validate(pl, support1, &config);
2059 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
2060 phylink_an_mode_str(mode),
2061 phy_modes(config.interface),
2062 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
2066 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
2067 phylink_an_mode_str(mode), phy_modes(config.interface),
2068 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
2070 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
2073 changed = !linkmode_equal(pl->supported, support);
2075 linkmode_copy(pl->supported, support);
2076 linkmode_copy(pl->link_config.advertising, config.advertising);
2079 if (pl->cur_link_an_mode != mode ||
2080 pl->link_config.interface != config.interface) {
2081 pl->link_config.interface = config.interface;
2082 pl->cur_link_an_mode = mode;
2086 phylink_info(pl, "switched to %s/%s link mode\n",
2087 phylink_an_mode_str(mode),
2088 phy_modes(config.interface));
2091 pl->link_port = pl->sfp_port;
2093 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
2094 &pl->phylink_disable_state))
2095 phylink_mac_initial_config(pl, false);
2100 static int phylink_sfp_module_insert(void *upstream,
2101 const struct sfp_eeprom_id *id)
2103 struct phylink *pl = upstream;
2104 unsigned long *support = pl->sfp_support;
2108 linkmode_zero(support);
2109 sfp_parse_support(pl->sfp_bus, id, support);
2110 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
2112 /* If this module may have a PHY connecting later, defer until later */
2113 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
2114 if (pl->sfp_may_have_phy)
2117 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
2120 static int phylink_sfp_module_start(void *upstream)
2122 struct phylink *pl = upstream;
2124 /* If this SFP module has a PHY, start the PHY now. */
2126 phy_start(pl->phydev);
2130 /* If the module may have a PHY but we didn't detect one we
2131 * need to configure the MAC here.
2133 if (!pl->sfp_may_have_phy)
2136 return phylink_sfp_config(pl, MLO_AN_INBAND,
2137 pl->sfp_support, pl->sfp_support);
2140 static void phylink_sfp_module_stop(void *upstream)
2142 struct phylink *pl = upstream;
2144 /* If this SFP module has a PHY, stop it. */
2146 phy_stop(pl->phydev);
2149 static void phylink_sfp_link_down(void *upstream)
2151 struct phylink *pl = upstream;
2155 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
2158 static void phylink_sfp_link_up(void *upstream)
2160 struct phylink *pl = upstream;
2164 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
2165 phylink_run_resolve(pl);
2168 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
2169 * or 802.3z control word, so inband will not work.
2171 static bool phylink_phy_no_inband(struct phy_device *phy)
2173 return phy->is_c45 &&
2174 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
2177 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
2179 struct phylink *pl = upstream;
2180 phy_interface_t interface;
2185 * This is the new way of dealing with flow control for PHYs,
2186 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
2187 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
2188 * using our validate call to the MAC, we rely upon the MAC
2189 * clearing the bits from both supported and advertising fields.
2191 phy_support_asym_pause(phy);
2193 if (phylink_phy_no_inband(phy))
2196 mode = MLO_AN_INBAND;
2198 /* Do the initial configuration */
2199 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2203 interface = pl->link_config.interface;
2204 ret = phylink_attach_phy(pl, phy, interface);
2208 ret = phylink_bringup_phy(pl, phy, interface);
2215 static void phylink_sfp_disconnect_phy(void *upstream)
2217 phylink_disconnect_phy(upstream);
2220 static const struct sfp_upstream_ops sfp_phylink_ops = {
2221 .attach = phylink_sfp_attach,
2222 .detach = phylink_sfp_detach,
2223 .module_insert = phylink_sfp_module_insert,
2224 .module_start = phylink_sfp_module_start,
2225 .module_stop = phylink_sfp_module_stop,
2226 .link_up = phylink_sfp_link_up,
2227 .link_down = phylink_sfp_link_down,
2228 .connect_phy = phylink_sfp_connect_phy,
2229 .disconnect_phy = phylink_sfp_disconnect_phy,
2232 /* Helpers for MAC drivers */
2235 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2236 * @state: a pointer to a &struct phylink_link_state
2238 * Inspect the interface mode, advertising mask or forced speed and
2239 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2240 * the interface mode to suit. @state->interface is appropriately
2241 * updated, and the advertising mask has the "other" baseX_Full flag
2244 void phylink_helper_basex_speed(struct phylink_link_state *state)
2246 if (phy_interface_mode_is_8023z(state->interface)) {
2247 bool want_2500 = state->an_enabled ?
2248 phylink_test(state->advertising, 2500baseX_Full) :
2249 state->speed == SPEED_2500;
2252 phylink_clear(state->advertising, 1000baseX_Full);
2253 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2255 phylink_clear(state->advertising, 2500baseX_Full);
2256 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2260 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2262 static void phylink_decode_c37_word(struct phylink_link_state *state,
2263 uint16_t config_reg, int speed)
2265 bool tx_pause, rx_pause;
2268 if (speed == SPEED_2500)
2269 fd_bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT;
2271 fd_bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT;
2273 mii_lpa_mod_linkmode_x(state->lp_advertising, config_reg, fd_bit);
2275 if (linkmode_test_bit(fd_bit, state->advertising) &&
2276 linkmode_test_bit(fd_bit, state->lp_advertising)) {
2277 state->speed = speed;
2278 state->duplex = DUPLEX_FULL;
2280 /* negotiation failure */
2281 state->link = false;
2284 linkmode_resolve_pause(state->advertising, state->lp_advertising,
2285 &tx_pause, &rx_pause);
2288 state->pause |= MLO_PAUSE_TX;
2290 state->pause |= MLO_PAUSE_RX;
2293 static void phylink_decode_sgmii_word(struct phylink_link_state *state,
2294 uint16_t config_reg)
2296 if (!(config_reg & LPA_SGMII_LINK)) {
2297 state->link = false;
2301 switch (config_reg & LPA_SGMII_SPD_MASK) {
2303 state->speed = SPEED_10;
2306 state->speed = SPEED_100;
2308 case LPA_SGMII_1000:
2309 state->speed = SPEED_1000;
2312 state->link = false;
2315 if (config_reg & LPA_SGMII_FULL_DUPLEX)
2316 state->duplex = DUPLEX_FULL;
2318 state->duplex = DUPLEX_HALF;
2322 * phylink_mii_c22_pcs_get_state() - read the MAC PCS state
2323 * @pcs: a pointer to a &struct mdio_device.
2324 * @state: a pointer to a &struct phylink_link_state.
2326 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2327 * clause 37 negotiation and/or SGMII control.
2329 * Read the MAC PCS state from the MII device configured in @config and
2330 * parse the Clause 37 or Cisco SGMII link partner negotiation word into
2331 * the phylink @state structure. This is suitable to be directly plugged
2332 * into the mac_pcs_get_state() member of the struct phylink_mac_ops
2335 void phylink_mii_c22_pcs_get_state(struct mdio_device *pcs,
2336 struct phylink_link_state *state)
2338 struct mii_bus *bus = pcs->bus;
2339 int addr = pcs->addr;
2342 bmsr = mdiobus_read(bus, addr, MII_BMSR);
2343 lpa = mdiobus_read(bus, addr, MII_LPA);
2344 if (bmsr < 0 || lpa < 0) {
2345 state->link = false;
2349 state->link = !!(bmsr & BMSR_LSTATUS);
2350 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
2354 switch (state->interface) {
2355 case PHY_INTERFACE_MODE_1000BASEX:
2356 phylink_decode_c37_word(state, lpa, SPEED_1000);
2359 case PHY_INTERFACE_MODE_2500BASEX:
2360 phylink_decode_c37_word(state, lpa, SPEED_2500);
2363 case PHY_INTERFACE_MODE_SGMII:
2364 phylink_decode_sgmii_word(state, lpa);
2368 state->link = false;
2372 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_get_state);
2375 * phylink_mii_c22_pcs_set_advertisement() - configure the clause 37 PCS
2377 * @pcs: a pointer to a &struct mdio_device.
2378 * @interface: the PHY interface mode being configured
2379 * @advertising: the ethtool advertisement mask
2381 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2382 * clause 37 negotiation and/or SGMII control.
2384 * Configure the clause 37 PCS advertisement as specified by @state. This
2385 * does not trigger a renegotiation; phylink will do that via the
2386 * mac_an_restart() method of the struct phylink_mac_ops structure.
2388 * Returns negative error code on failure to configure the advertisement,
2389 * zero if no change has been made, or one if the advertisement has changed.
2391 int phylink_mii_c22_pcs_set_advertisement(struct mdio_device *pcs,
2392 phy_interface_t interface,
2393 const unsigned long *advertising)
2395 struct mii_bus *bus = pcs->bus;
2396 int addr = pcs->addr;
2400 switch (interface) {
2401 case PHY_INTERFACE_MODE_1000BASEX:
2402 case PHY_INTERFACE_MODE_2500BASEX:
2403 adv = ADVERTISE_1000XFULL;
2404 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2406 adv |= ADVERTISE_1000XPAUSE;
2407 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2409 adv |= ADVERTISE_1000XPSE_ASYM;
2411 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2418 ret = mdiobus_write(bus, addr, MII_ADVERTISE, adv);
2424 case PHY_INTERFACE_MODE_SGMII:
2425 val = mdiobus_read(bus, addr, MII_ADVERTISE);
2432 ret = mdiobus_write(bus, addr, MII_ADVERTISE, 0x0001);
2439 /* Nothing to do for other modes */
2443 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_set_advertisement);
2446 * phylink_mii_c22_pcs_config() - configure clause 22 PCS
2447 * @pcs: a pointer to a &struct mdio_device.
2448 * @mode: link autonegotiation mode
2449 * @interface: the PHY interface mode being configured
2450 * @advertising: the ethtool advertisement mask
2452 * Configure a Clause 22 PCS PHY with the appropriate negotiation
2453 * parameters for the @mode, @interface and @advertising parameters.
2454 * Returns negative error number on failure, zero if the advertisement
2455 * has not changed, or positive if there is a change.
2457 int phylink_mii_c22_pcs_config(struct mdio_device *pcs, unsigned int mode,
2458 phy_interface_t interface,
2459 const unsigned long *advertising)
2465 ret = phylink_mii_c22_pcs_set_advertisement(pcs, interface,
2472 bmcr = mode == MLO_AN_INBAND ? BMCR_ANENABLE : 0;
2473 ret = mdiobus_modify(pcs->bus, pcs->addr, MII_BMCR,
2474 BMCR_ANENABLE, bmcr);
2478 return changed ? 1 : 0;
2480 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_config);
2483 * phylink_mii_c22_pcs_an_restart() - restart 802.3z autonegotiation
2484 * @pcs: a pointer to a &struct mdio_device.
2486 * Helper for MAC PCS supporting the 802.3 clause 22 register set for
2487 * clause 37 negotiation.
2489 * Restart the clause 37 negotiation with the link partner. This is
2490 * suitable to be directly plugged into the mac_pcs_get_state() member
2491 * of the struct phylink_mac_ops structure.
2493 void phylink_mii_c22_pcs_an_restart(struct mdio_device *pcs)
2495 struct mii_bus *bus = pcs->bus;
2496 int val, addr = pcs->addr;
2498 val = mdiobus_read(bus, addr, MII_BMCR);
2500 val |= BMCR_ANRESTART;
2502 mdiobus_write(bus, addr, MII_BMCR, val);
2505 EXPORT_SYMBOL_GPL(phylink_mii_c22_pcs_an_restart);
2507 void phylink_mii_c45_pcs_get_state(struct mdio_device *pcs,
2508 struct phylink_link_state *state)
2510 struct mii_bus *bus = pcs->bus;
2511 int addr = pcs->addr;
2514 stat = mdiobus_c45_read(bus, addr, MDIO_MMD_PCS, MDIO_STAT1);
2516 state->link = false;
2520 state->link = !!(stat & MDIO_STAT1_LSTATUS);
2524 switch (state->interface) {
2525 case PHY_INTERFACE_MODE_10GBASER:
2526 state->speed = SPEED_10000;
2527 state->duplex = DUPLEX_FULL;
2534 EXPORT_SYMBOL_GPL(phylink_mii_c45_pcs_get_state);
2536 MODULE_LICENSE("GPL v2");