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 *ops;
44 struct phylink_config *config;
46 unsigned int old_link_state:1;
48 unsigned long phylink_disable_state; /* bitmask of disables */
49 struct phy_device *phydev;
50 phy_interface_t link_interface; /* PHY_INTERFACE_xxx */
51 u8 link_an_mode; /* MLO_AN_xxx */
52 u8 link_port; /* The current non-phy ethtool port */
53 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
55 /* The link configuration settings */
56 struct phylink_link_state link_config;
58 /* The current settings */
59 phy_interface_t cur_interface;
61 struct gpio_desc *link_gpio;
62 unsigned int link_irq;
63 struct timer_list link_poll;
64 void (*get_fixed_state)(struct net_device *dev,
65 struct phylink_link_state *s);
67 struct mutex state_mutex;
68 struct phylink_link_state phy_state;
69 struct work_struct resolve;
71 bool mac_link_dropped;
73 struct sfp_bus *sfp_bus;
76 #define phylink_printk(level, pl, fmt, ...) \
78 if ((pl)->config->type == PHYLINK_NETDEV) \
79 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
80 else if ((pl)->config->type == PHYLINK_DEV) \
81 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
84 #define phylink_err(pl, fmt, ...) \
85 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
86 #define phylink_warn(pl, fmt, ...) \
87 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
88 #define phylink_info(pl, fmt, ...) \
89 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
90 #if defined(CONFIG_DYNAMIC_DEBUG)
91 #define phylink_dbg(pl, fmt, ...) \
93 if ((pl)->config->type == PHYLINK_NETDEV) \
94 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
95 else if ((pl)->config->type == PHYLINK_DEV) \
96 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
99 #define phylink_dbg(pl, fmt, ...) \
100 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
102 #define phylink_dbg(pl, fmt, ...) \
105 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
110 * phylink_set_port_modes() - set the port type modes in the ethtool mask
111 * @mask: ethtool link mode mask
113 * Sets all the port type modes in the ethtool mask. MAC drivers should
114 * use this in their 'validate' callback.
116 void phylink_set_port_modes(unsigned long *mask)
118 phylink_set(mask, TP);
119 phylink_set(mask, AUI);
120 phylink_set(mask, MII);
121 phylink_set(mask, FIBRE);
122 phylink_set(mask, BNC);
123 phylink_set(mask, Backplane);
125 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
127 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
129 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
131 phylink_set_port_modes(tmp);
132 phylink_set(tmp, Autoneg);
133 phylink_set(tmp, Pause);
134 phylink_set(tmp, Asym_Pause);
136 return linkmode_subset(linkmode, tmp);
139 static const char *phylink_an_mode_str(unsigned int mode)
141 static const char *modestr[] = {
142 [MLO_AN_PHY] = "phy",
143 [MLO_AN_FIXED] = "fixed",
144 [MLO_AN_INBAND] = "inband",
147 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
150 static int phylink_validate(struct phylink *pl, unsigned long *supported,
151 struct phylink_link_state *state)
153 pl->ops->validate(pl->config, supported, state);
155 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
158 static int phylink_parse_fixedlink(struct phylink *pl,
159 struct fwnode_handle *fwnode)
161 struct fwnode_handle *fixed_node;
162 const struct phy_setting *s;
163 struct gpio_desc *desc;
167 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
169 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
171 pl->link_config.speed = speed;
172 pl->link_config.duplex = DUPLEX_HALF;
174 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
175 pl->link_config.duplex = DUPLEX_FULL;
177 /* We treat the "pause" and "asym-pause" terminology as
178 * defining the link partner's ability. */
179 if (fwnode_property_read_bool(fixed_node, "pause"))
180 pl->link_config.pause |= MLO_PAUSE_SYM;
181 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
182 pl->link_config.pause |= MLO_PAUSE_ASYM;
185 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
189 pl->link_gpio = desc;
190 else if (desc == ERR_PTR(-EPROBE_DEFER))
193 fwnode_handle_put(fixed_node);
200 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
202 if (ret != ARRAY_SIZE(prop)) {
203 phylink_err(pl, "broken fixed-link?\n");
207 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
208 prop, ARRAY_SIZE(prop));
210 pl->link_config.duplex = prop[1] ?
211 DUPLEX_FULL : DUPLEX_HALF;
212 pl->link_config.speed = prop[2];
214 pl->link_config.pause |= MLO_PAUSE_SYM;
216 pl->link_config.pause |= MLO_PAUSE_ASYM;
220 if (pl->link_config.speed > SPEED_1000 &&
221 pl->link_config.duplex != DUPLEX_FULL)
222 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
223 pl->link_config.speed);
225 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
226 linkmode_copy(pl->link_config.advertising, pl->supported);
227 phylink_validate(pl, pl->supported, &pl->link_config);
229 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
230 pl->supported, true);
231 linkmode_zero(pl->supported);
232 phylink_set(pl->supported, MII);
233 phylink_set(pl->supported, Pause);
234 phylink_set(pl->supported, Asym_Pause);
236 __set_bit(s->bit, pl->supported);
238 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
239 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
240 pl->link_config.speed);
243 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
246 pl->link_config.link = 1;
247 pl->link_config.an_complete = 1;
252 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
254 struct fwnode_handle *dn;
257 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
258 if (dn || fwnode_property_present(fwnode, "fixed-link"))
259 pl->link_an_mode = MLO_AN_FIXED;
260 fwnode_handle_put(dn);
262 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
263 strcmp(managed, "in-band-status") == 0) {
264 if (pl->link_an_mode == MLO_AN_FIXED) {
266 "can't use both fixed-link and in-band-status\n");
270 linkmode_zero(pl->supported);
271 phylink_set(pl->supported, MII);
272 phylink_set(pl->supported, Autoneg);
273 phylink_set(pl->supported, Asym_Pause);
274 phylink_set(pl->supported, Pause);
275 pl->link_config.an_enabled = true;
276 pl->link_an_mode = MLO_AN_INBAND;
278 switch (pl->link_config.interface) {
279 case PHY_INTERFACE_MODE_SGMII:
280 phylink_set(pl->supported, 10baseT_Half);
281 phylink_set(pl->supported, 10baseT_Full);
282 phylink_set(pl->supported, 100baseT_Half);
283 phylink_set(pl->supported, 100baseT_Full);
284 phylink_set(pl->supported, 1000baseT_Half);
285 phylink_set(pl->supported, 1000baseT_Full);
288 case PHY_INTERFACE_MODE_1000BASEX:
289 phylink_set(pl->supported, 1000baseX_Full);
292 case PHY_INTERFACE_MODE_2500BASEX:
293 phylink_set(pl->supported, 2500baseX_Full);
296 case PHY_INTERFACE_MODE_10GKR:
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);
303 phylink_set(pl->supported, 1000baseX_Full);
304 phylink_set(pl->supported, 10000baseKR_Full);
305 phylink_set(pl->supported, 10000baseCR_Full);
306 phylink_set(pl->supported, 10000baseSR_Full);
307 phylink_set(pl->supported, 10000baseLR_Full);
308 phylink_set(pl->supported, 10000baseLRM_Full);
309 phylink_set(pl->supported, 10000baseER_Full);
314 "incorrect link mode %s for in-band status\n",
315 phy_modes(pl->link_config.interface));
319 linkmode_copy(pl->link_config.advertising, pl->supported);
321 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
323 "failed to validate link configuration for in-band status\n");
331 static void phylink_mac_config(struct phylink *pl,
332 const struct phylink_link_state *state)
335 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
336 __func__, phylink_an_mode_str(pl->link_an_mode),
337 phy_modes(state->interface),
338 phy_speed_to_str(state->speed),
339 phy_duplex_to_str(state->duplex),
340 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
341 state->pause, state->link, state->an_enabled);
343 pl->ops->mac_config(pl->config, pl->link_an_mode, state);
346 static void phylink_mac_config_up(struct phylink *pl,
347 const struct phylink_link_state *state)
350 phylink_mac_config(pl, state);
353 static void phylink_mac_an_restart(struct phylink *pl)
355 if (pl->link_config.an_enabled &&
356 phy_interface_mode_is_8023z(pl->link_config.interface))
357 pl->ops->mac_an_restart(pl->config);
360 static void phylink_mac_pcs_get_state(struct phylink *pl,
361 struct phylink_link_state *state)
363 linkmode_copy(state->advertising, pl->link_config.advertising);
364 linkmode_zero(state->lp_advertising);
365 state->interface = pl->link_config.interface;
366 state->an_enabled = pl->link_config.an_enabled;
367 state->speed = SPEED_UNKNOWN;
368 state->duplex = DUPLEX_UNKNOWN;
369 state->pause = MLO_PAUSE_NONE;
370 state->an_complete = 0;
373 pl->ops->mac_pcs_get_state(pl->config, state);
376 /* The fixed state is... fixed except for the link state,
377 * which may be determined by a GPIO or a callback.
379 static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
381 *state = pl->link_config;
382 if (pl->get_fixed_state)
383 pl->get_fixed_state(pl->netdev, state);
384 else if (pl->link_gpio)
385 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
388 /* Flow control is resolved according to our and the link partners
389 * advertisements using the following drawn from the 802.3 specs:
390 * Local device Link partner
391 * Pause AsymDir Pause AsymDir Result
396 static void phylink_resolve_flow(struct phylink *pl,
397 struct phylink_link_state *state)
401 if (pl->link_config.pause & MLO_PAUSE_AN) {
404 if (phylink_test(pl->link_config.advertising, Pause))
405 pause |= MLO_PAUSE_SYM;
406 if (phylink_test(pl->link_config.advertising, Asym_Pause))
407 pause |= MLO_PAUSE_ASYM;
409 pause &= state->pause;
411 if (pause & MLO_PAUSE_SYM)
412 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
413 else if (pause & MLO_PAUSE_ASYM)
414 new_pause = state->pause & MLO_PAUSE_SYM ?
415 MLO_PAUSE_TX : MLO_PAUSE_RX;
417 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
420 state->pause &= ~MLO_PAUSE_TXRX_MASK;
421 state->pause |= new_pause;
424 static const char *phylink_pause_to_str(int pause)
426 switch (pause & MLO_PAUSE_TXRX_MASK) {
427 case MLO_PAUSE_TX | MLO_PAUSE_RX:
438 static void phylink_mac_link_up(struct phylink *pl,
439 struct phylink_link_state link_state)
441 struct net_device *ndev = pl->netdev;
443 pl->cur_interface = link_state.interface;
444 pl->ops->mac_link_up(pl->config, pl->link_an_mode,
445 pl->cur_interface, pl->phydev);
448 netif_carrier_on(ndev);
451 "Link is Up - %s/%s - flow control %s\n",
452 phy_speed_to_str(link_state.speed),
453 phy_duplex_to_str(link_state.duplex),
454 phylink_pause_to_str(link_state.pause));
457 static void phylink_mac_link_down(struct phylink *pl)
459 struct net_device *ndev = pl->netdev;
462 netif_carrier_off(ndev);
463 pl->ops->mac_link_down(pl->config, pl->link_an_mode,
465 phylink_info(pl, "Link is Down\n");
468 static void phylink_resolve(struct work_struct *w)
470 struct phylink *pl = container_of(w, struct phylink, resolve);
471 struct phylink_link_state link_state;
472 struct net_device *ndev = pl->netdev;
475 mutex_lock(&pl->state_mutex);
476 if (pl->phylink_disable_state) {
477 pl->mac_link_dropped = false;
478 link_state.link = false;
479 } else if (pl->mac_link_dropped) {
480 link_state.link = false;
482 switch (pl->link_an_mode) {
484 link_state = pl->phy_state;
485 phylink_resolve_flow(pl, &link_state);
486 phylink_mac_config_up(pl, &link_state);
490 phylink_get_fixed_state(pl, &link_state);
491 phylink_mac_config_up(pl, &link_state);
495 phylink_mac_pcs_get_state(pl, &link_state);
497 /* If we have a phy, the "up" state is the union of
498 * both the PHY and the MAC */
500 link_state.link &= pl->phy_state.link;
502 /* Only update if the PHY link is up */
503 if (pl->phydev && pl->phy_state.link) {
504 link_state.interface = pl->phy_state.interface;
506 /* If we have a PHY, we need to update with
507 * the pause mode bits. */
508 link_state.pause |= pl->phy_state.pause;
509 phylink_resolve_flow(pl, &link_state);
510 phylink_mac_config(pl, &link_state);
517 link_changed = (link_state.link != netif_carrier_ok(ndev));
519 link_changed = (link_state.link != pl->old_link_state);
522 pl->old_link_state = link_state.link;
523 if (!link_state.link)
524 phylink_mac_link_down(pl);
526 phylink_mac_link_up(pl, link_state);
528 if (!link_state.link && pl->mac_link_dropped) {
529 pl->mac_link_dropped = false;
530 queue_work(system_power_efficient_wq, &pl->resolve);
532 mutex_unlock(&pl->state_mutex);
535 static void phylink_run_resolve(struct phylink *pl)
537 if (!pl->phylink_disable_state)
538 queue_work(system_power_efficient_wq, &pl->resolve);
541 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
543 unsigned long state = pl->phylink_disable_state;
545 set_bit(bit, &pl->phylink_disable_state);
547 queue_work(system_power_efficient_wq, &pl->resolve);
548 flush_work(&pl->resolve);
552 static void phylink_fixed_poll(struct timer_list *t)
554 struct phylink *pl = container_of(t, struct phylink, link_poll);
556 mod_timer(t, jiffies + HZ);
558 phylink_run_resolve(pl);
561 static const struct sfp_upstream_ops sfp_phylink_ops;
563 static int phylink_register_sfp(struct phylink *pl,
564 struct fwnode_handle *fwnode)
569 bus = sfp_bus_find_fwnode(fwnode);
572 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
578 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
585 * phylink_create() - create a phylink instance
586 * @config: a pointer to the target &struct phylink_config
587 * @fwnode: a pointer to a &struct fwnode_handle describing the network
589 * @iface: the desired link mode defined by &typedef phy_interface_t
590 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
592 * Create a new phylink instance, and parse the link parameters found in @np.
593 * This will parse in-band modes, fixed-link or SFP configuration.
595 * Note: the rtnl lock must not be held when calling this function.
597 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
598 * must use IS_ERR() to check for errors from this function.
600 struct phylink *phylink_create(struct phylink_config *config,
601 struct fwnode_handle *fwnode,
602 phy_interface_t iface,
603 const struct phylink_mac_ops *ops)
608 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
610 return ERR_PTR(-ENOMEM);
612 mutex_init(&pl->state_mutex);
613 INIT_WORK(&pl->resolve, phylink_resolve);
616 if (config->type == PHYLINK_NETDEV) {
617 pl->netdev = to_net_dev(config->dev);
618 } else if (config->type == PHYLINK_DEV) {
619 pl->dev = config->dev;
622 return ERR_PTR(-EINVAL);
625 pl->phy_state.interface = iface;
626 pl->link_interface = iface;
627 if (iface == PHY_INTERFACE_MODE_MOCA)
628 pl->link_port = PORT_BNC;
630 pl->link_port = PORT_MII;
631 pl->link_config.interface = iface;
632 pl->link_config.pause = MLO_PAUSE_AN;
633 pl->link_config.speed = SPEED_UNKNOWN;
634 pl->link_config.duplex = DUPLEX_UNKNOWN;
635 pl->link_config.an_enabled = true;
637 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
638 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
640 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
641 linkmode_copy(pl->link_config.advertising, pl->supported);
642 phylink_validate(pl, pl->supported, &pl->link_config);
644 ret = phylink_parse_mode(pl, fwnode);
650 if (pl->link_an_mode == MLO_AN_FIXED) {
651 ret = phylink_parse_fixedlink(pl, fwnode);
658 ret = phylink_register_sfp(pl, fwnode);
666 EXPORT_SYMBOL_GPL(phylink_create);
669 * phylink_destroy() - cleanup and destroy the phylink instance
670 * @pl: a pointer to a &struct phylink returned from phylink_create()
672 * Destroy a phylink instance. Any PHY that has been attached must have been
673 * cleaned up via phylink_disconnect_phy() prior to calling this function.
675 * Note: the rtnl lock must not be held when calling this function.
677 void phylink_destroy(struct phylink *pl)
679 sfp_bus_del_upstream(pl->sfp_bus);
681 gpiod_put(pl->link_gpio);
683 cancel_work_sync(&pl->resolve);
686 EXPORT_SYMBOL_GPL(phylink_destroy);
688 static void phylink_phy_change(struct phy_device *phydev, bool up,
691 struct phylink *pl = phydev->phylink;
693 mutex_lock(&pl->state_mutex);
694 pl->phy_state.speed = phydev->speed;
695 pl->phy_state.duplex = phydev->duplex;
696 pl->phy_state.pause = MLO_PAUSE_NONE;
698 pl->phy_state.pause |= MLO_PAUSE_SYM;
699 if (phydev->asym_pause)
700 pl->phy_state.pause |= MLO_PAUSE_ASYM;
701 pl->phy_state.interface = phydev->interface;
702 pl->phy_state.link = up;
703 mutex_unlock(&pl->state_mutex);
705 phylink_run_resolve(pl);
707 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
708 phy_modes(phydev->interface),
709 phy_speed_to_str(phydev->speed),
710 phy_duplex_to_str(phydev->duplex));
713 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
715 struct phylink_link_state config;
716 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
720 * This is the new way of dealing with flow control for PHYs,
721 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
722 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
723 * using our validate call to the MAC, we rely upon the MAC
724 * clearing the bits from both supported and advertising fields.
726 phy_support_asym_pause(phy);
728 memset(&config, 0, sizeof(config));
729 linkmode_copy(supported, phy->supported);
730 linkmode_copy(config.advertising, phy->advertising);
731 config.interface = pl->link_config.interface;
733 ret = phylink_validate(pl, supported, &config);
738 phy->phy_link_change = phylink_phy_change;
741 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
744 mutex_lock(&phy->lock);
745 mutex_lock(&pl->state_mutex);
747 linkmode_copy(pl->supported, supported);
748 linkmode_copy(pl->link_config.advertising, config.advertising);
750 /* Restrict the phy advertisement according to the MAC support. */
751 linkmode_copy(phy->advertising, config.advertising);
752 mutex_unlock(&pl->state_mutex);
753 mutex_unlock(&phy->lock);
756 "phy: setting supported %*pb advertising %*pb\n",
757 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
758 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
760 if (phy_interrupt_is_valid(phy))
761 phy_request_interrupt(phy);
766 static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
767 phy_interface_t interface)
771 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
772 (pl->link_an_mode == MLO_AN_INBAND &&
773 phy_interface_mode_is_8023z(interface))))
779 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
783 ret = phylink_bringup_phy(pl, phy);
791 * phylink_connect_phy() - connect a PHY to the phylink instance
792 * @pl: a pointer to a &struct phylink returned from phylink_create()
793 * @phy: a pointer to a &struct phy_device.
795 * Connect @phy to the phylink instance specified by @pl by calling
796 * phy_attach_direct(). Configure the @phy according to the MAC driver's
797 * capabilities, start the PHYLIB state machine and enable any interrupts
798 * that the PHY supports.
800 * This updates the phylink's ethtool supported and advertising link mode
803 * Returns 0 on success or a negative errno.
805 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
807 /* Use PHY device/driver interface */
808 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
809 pl->link_interface = phy->interface;
810 pl->link_config.interface = pl->link_interface;
813 return __phylink_connect_phy(pl, phy, pl->link_interface);
815 EXPORT_SYMBOL_GPL(phylink_connect_phy);
818 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
819 * @pl: a pointer to a &struct phylink returned from phylink_create()
820 * @dn: a pointer to a &struct device_node.
821 * @flags: PHY-specific flags to communicate to the PHY device driver
823 * Connect the phy specified in the device node @dn to the phylink instance
824 * specified by @pl. Actions specified in phylink_connect_phy() will be
827 * Returns 0 on success or a negative errno.
829 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
832 struct device_node *phy_node;
833 struct phy_device *phy_dev;
836 /* Fixed links and 802.3z are handled without needing a PHY */
837 if (pl->link_an_mode == MLO_AN_FIXED ||
838 (pl->link_an_mode == MLO_AN_INBAND &&
839 phy_interface_mode_is_8023z(pl->link_interface)))
842 phy_node = of_parse_phandle(dn, "phy-handle", 0);
844 phy_node = of_parse_phandle(dn, "phy", 0);
846 phy_node = of_parse_phandle(dn, "phy-device", 0);
849 if (pl->link_an_mode == MLO_AN_PHY)
854 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
856 /* We're done with the phy_node handle */
857 of_node_put(phy_node);
862 ret = phylink_bringup_phy(pl, phy_dev);
868 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
871 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
873 * @pl: a pointer to a &struct phylink returned from phylink_create()
875 * Disconnect any current PHY from the phylink instance described by @pl.
877 void phylink_disconnect_phy(struct phylink *pl)
879 struct phy_device *phy;
885 mutex_lock(&phy->lock);
886 mutex_lock(&pl->state_mutex);
888 mutex_unlock(&pl->state_mutex);
889 mutex_unlock(&phy->lock);
890 flush_work(&pl->resolve);
895 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
898 * phylink_fixed_state_cb() - allow setting a fixed link callback
899 * @pl: a pointer to a &struct phylink returned from phylink_create()
900 * @cb: callback to execute to determine the fixed link state.
902 * The MAC driver should call this driver when the state of its link
903 * can be determined through e.g: an out of band MMIO register.
905 int phylink_fixed_state_cb(struct phylink *pl,
906 void (*cb)(struct net_device *dev,
907 struct phylink_link_state *state))
909 /* It does not make sense to let the link be overriden unless we use
912 if (pl->link_an_mode != MLO_AN_FIXED)
915 mutex_lock(&pl->state_mutex);
916 pl->get_fixed_state = cb;
917 mutex_unlock(&pl->state_mutex);
921 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
924 * phylink_mac_change() - notify phylink of a change in MAC state
925 * @pl: a pointer to a &struct phylink returned from phylink_create()
926 * @up: indicates whether the link is currently up.
928 * The MAC driver should call this driver when the state of its link
929 * changes (eg, link failure, new negotiation results, etc.)
931 void phylink_mac_change(struct phylink *pl, bool up)
934 pl->mac_link_dropped = true;
935 phylink_run_resolve(pl);
936 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
938 EXPORT_SYMBOL_GPL(phylink_mac_change);
940 static irqreturn_t phylink_link_handler(int irq, void *data)
942 struct phylink *pl = data;
944 phylink_run_resolve(pl);
950 * phylink_start() - start a phylink instance
951 * @pl: a pointer to a &struct phylink returned from phylink_create()
953 * Start the phylink instance specified by @pl, configuring the MAC for the
954 * desired link mode(s) and negotiation style. This should be called from the
955 * network device driver's &struct net_device_ops ndo_open() method.
957 void phylink_start(struct phylink *pl)
961 phylink_info(pl, "configuring for %s/%s link mode\n",
962 phylink_an_mode_str(pl->link_an_mode),
963 phy_modes(pl->link_config.interface));
965 /* Always set the carrier off */
967 netif_carrier_off(pl->netdev);
969 /* Apply the link configuration to the MAC when starting. This allows
970 * a fixed-link to start with the correct parameters, and also
971 * ensures that we set the appropriate advertisement for Serdes links.
973 phylink_resolve_flow(pl, &pl->link_config);
974 phylink_mac_config(pl, &pl->link_config);
976 /* Restart autonegotiation if using 802.3z to ensure that the link
977 * parameters are properly negotiated. This is necessary for DSA
978 * switches using 802.3z negotiation to ensure they see our modes.
980 phylink_mac_an_restart(pl);
982 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
983 phylink_run_resolve(pl);
985 if (pl->link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
986 int irq = gpiod_to_irq(pl->link_gpio);
989 if (!request_irq(irq, phylink_link_handler,
990 IRQF_TRIGGER_RISING |
991 IRQF_TRIGGER_FALLING,
998 mod_timer(&pl->link_poll, jiffies + HZ);
1000 if (pl->link_an_mode == MLO_AN_FIXED && pl->get_fixed_state)
1001 mod_timer(&pl->link_poll, jiffies + HZ);
1003 phy_start(pl->phydev);
1005 sfp_upstream_start(pl->sfp_bus);
1007 EXPORT_SYMBOL_GPL(phylink_start);
1010 * phylink_stop() - stop a phylink instance
1011 * @pl: a pointer to a &struct phylink returned from phylink_create()
1013 * Stop the phylink instance specified by @pl. This should be called from the
1014 * network device driver's &struct net_device_ops ndo_stop() method. The
1015 * network device's carrier state should not be changed prior to calling this
1018 void phylink_stop(struct phylink *pl)
1023 sfp_upstream_stop(pl->sfp_bus);
1025 phy_stop(pl->phydev);
1026 del_timer_sync(&pl->link_poll);
1028 free_irq(pl->link_irq, pl);
1032 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1034 EXPORT_SYMBOL_GPL(phylink_stop);
1037 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1038 * @pl: a pointer to a &struct phylink returned from phylink_create()
1039 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1041 * Read the wake on lan parameters from the PHY attached to the phylink
1042 * instance specified by @pl. If no PHY is currently attached, report no
1043 * support for wake on lan.
1045 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1053 phy_ethtool_get_wol(pl->phydev, wol);
1055 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1058 * phylink_ethtool_set_wol() - set wake on lan parameters
1059 * @pl: a pointer to a &struct phylink returned from phylink_create()
1060 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1062 * Set the wake on lan parameters for the PHY attached to the phylink
1063 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1066 * Returns zero on success or negative errno code.
1068 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1070 int ret = -EOPNOTSUPP;
1075 ret = phy_ethtool_set_wol(pl->phydev, wol);
1079 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1081 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1083 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1085 linkmode_zero(mask);
1086 phylink_set_port_modes(mask);
1088 linkmode_and(dst, dst, mask);
1089 linkmode_or(dst, dst, b);
1092 static void phylink_get_ksettings(const struct phylink_link_state *state,
1093 struct ethtool_link_ksettings *kset)
1095 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1096 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1097 kset->base.speed = state->speed;
1098 kset->base.duplex = state->duplex;
1099 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1104 * phylink_ethtool_ksettings_get() - get the current link settings
1105 * @pl: a pointer to a &struct phylink returned from phylink_create()
1106 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1108 * Read the current link settings for the phylink instance specified by @pl.
1109 * This will be the link settings read from the MAC, PHY or fixed link
1110 * settings depending on the current negotiation mode.
1112 int phylink_ethtool_ksettings_get(struct phylink *pl,
1113 struct ethtool_link_ksettings *kset)
1115 struct phylink_link_state link_state;
1120 phy_ethtool_ksettings_get(pl->phydev, kset);
1122 kset->base.port = pl->link_port;
1125 linkmode_copy(kset->link_modes.supported, pl->supported);
1127 switch (pl->link_an_mode) {
1129 /* We are using fixed settings. Report these as the
1130 * current link settings - and note that these also
1131 * represent the supported speeds/duplex/pause modes.
1133 phylink_get_fixed_state(pl, &link_state);
1134 phylink_get_ksettings(&link_state, kset);
1138 /* If there is a phy attached, then use the reported
1139 * settings from the phy with no modification.
1144 phylink_mac_pcs_get_state(pl, &link_state);
1146 /* The MAC is reporting the link results from its own PCS
1147 * layer via in-band status. Report these as the current
1150 phylink_get_ksettings(&link_state, kset);
1156 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1159 * phylink_ethtool_ksettings_set() - set the link settings
1160 * @pl: a pointer to a &struct phylink returned from phylink_create()
1161 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1163 int phylink_ethtool_ksettings_set(struct phylink *pl,
1164 const struct ethtool_link_ksettings *kset)
1166 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1167 struct ethtool_link_ksettings our_kset;
1168 struct phylink_link_state config;
1173 if (kset->base.autoneg != AUTONEG_DISABLE &&
1174 kset->base.autoneg != AUTONEG_ENABLE)
1177 linkmode_copy(support, pl->supported);
1178 config = pl->link_config;
1180 /* Mask out unsupported advertisements */
1181 linkmode_and(config.advertising, kset->link_modes.advertising,
1184 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1185 if (kset->base.autoneg == AUTONEG_DISABLE) {
1186 const struct phy_setting *s;
1188 /* Autonegotiation disabled, select a suitable speed and
1191 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1196 /* If we have a fixed link (as specified by firmware), refuse
1197 * to change link parameters.
1199 if (pl->link_an_mode == MLO_AN_FIXED &&
1200 (s->speed != pl->link_config.speed ||
1201 s->duplex != pl->link_config.duplex))
1204 config.speed = s->speed;
1205 config.duplex = s->duplex;
1206 config.an_enabled = false;
1208 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1210 /* If we have a fixed link, refuse to enable autonegotiation */
1211 if (pl->link_an_mode == MLO_AN_FIXED)
1214 config.speed = SPEED_UNKNOWN;
1215 config.duplex = DUPLEX_UNKNOWN;
1216 config.an_enabled = true;
1218 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1221 if (phylink_validate(pl, support, &config))
1224 /* If autonegotiation is enabled, we must have an advertisement */
1225 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1229 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1230 our_kset.base.speed = config.speed;
1231 our_kset.base.duplex = config.duplex;
1233 /* If we have a PHY, configure the phy */
1235 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1240 mutex_lock(&pl->state_mutex);
1241 /* Configure the MAC to match the new settings */
1242 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1243 pl->link_config.interface = config.interface;
1244 pl->link_config.speed = our_kset.base.speed;
1245 pl->link_config.duplex = our_kset.base.duplex;
1246 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1248 /* If we have a PHY, phylib will call our link state function if the
1249 * mode has changed, which will trigger a resolve and update the MAC
1250 * configuration. For a fixed link, this isn't able to change any
1251 * parameters, which just leaves inband mode.
1253 if (pl->link_an_mode == MLO_AN_INBAND &&
1254 !test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1255 phylink_mac_config(pl, &pl->link_config);
1256 phylink_mac_an_restart(pl);
1258 mutex_unlock(&pl->state_mutex);
1262 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1265 * phylink_ethtool_nway_reset() - restart negotiation
1266 * @pl: a pointer to a &struct phylink returned from phylink_create()
1268 * Restart negotiation for the phylink instance specified by @pl. This will
1269 * cause any attached phy to restart negotiation with the link partner, and
1270 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1273 * Returns zero on success, or negative error code.
1275 int phylink_ethtool_nway_reset(struct phylink *pl)
1282 ret = phy_restart_aneg(pl->phydev);
1283 phylink_mac_an_restart(pl);
1287 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1290 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1291 * @pl: a pointer to a &struct phylink returned from phylink_create()
1292 * @pause: a pointer to a &struct ethtool_pauseparam
1294 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1295 struct ethtool_pauseparam *pause)
1299 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1300 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1301 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1303 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1306 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1307 * @pl: a pointer to a &struct phylink returned from phylink_create()
1308 * @pause: a pointer to a &struct ethtool_pauseparam
1310 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1311 struct ethtool_pauseparam *pause)
1313 struct phylink_link_state *config = &pl->link_config;
1317 if (!phylink_test(pl->supported, Pause) &&
1318 !phylink_test(pl->supported, Asym_Pause))
1321 if (!phylink_test(pl->supported, Asym_Pause) &&
1322 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1325 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1328 config->pause |= MLO_PAUSE_AN;
1329 if (pause->rx_pause)
1330 config->pause |= MLO_PAUSE_RX;
1331 if (pause->tx_pause)
1332 config->pause |= MLO_PAUSE_TX;
1334 /* If we have a PHY, phylib will call our link state function if the
1335 * mode has changed, which will trigger a resolve and update the MAC
1339 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1341 } else if (!test_bit(PHYLINK_DISABLE_STOPPED,
1342 &pl->phylink_disable_state)) {
1343 switch (pl->link_an_mode) {
1345 /* Should we allow fixed links to change against the config? */
1346 phylink_resolve_flow(pl, config);
1347 phylink_mac_config(pl, config);
1351 phylink_mac_config(pl, config);
1352 phylink_mac_an_restart(pl);
1359 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1362 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1364 * @pl: a pointer to a &struct phylink returned from phylink_create().
1366 * Read the Energy Efficient Ethernet error counter from the PHY associated
1367 * with the phylink instance specified by @pl.
1369 * Returns positive error counter value, or negative error code.
1371 int phylink_get_eee_err(struct phylink *pl)
1378 ret = phy_get_eee_err(pl->phydev);
1382 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1385 * phylink_init_eee() - init and check the EEE features
1386 * @pl: a pointer to a &struct phylink returned from phylink_create()
1387 * @clk_stop_enable: allow PHY to stop receive clock
1389 * Must be called either with RTNL held or within mac_link_up()
1391 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1393 int ret = -EOPNOTSUPP;
1396 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1400 EXPORT_SYMBOL_GPL(phylink_init_eee);
1403 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1404 * @pl: a pointer to a &struct phylink returned from phylink_create()
1405 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1407 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1409 int ret = -EOPNOTSUPP;
1414 ret = phy_ethtool_get_eee(pl->phydev, eee);
1418 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1421 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1422 * @pl: a pointer to a &struct phylink returned from phylink_create()
1423 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1425 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1427 int ret = -EOPNOTSUPP;
1432 ret = phy_ethtool_set_eee(pl->phydev, eee);
1436 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1438 /* This emulates MII registers for a fixed-mode phy operating as per the
1439 * passed in state. "aneg" defines if we report negotiation is possible.
1441 * FIXME: should deal with negotiation state too.
1443 static int phylink_mii_emul_read(unsigned int reg,
1444 struct phylink_link_state *state)
1446 struct fixed_phy_status fs;
1449 fs.link = state->link;
1450 fs.speed = state->speed;
1451 fs.duplex = state->duplex;
1452 fs.pause = state->pause & MLO_PAUSE_SYM;
1453 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1455 val = swphy_read_reg(reg, &fs);
1456 if (reg == MII_BMSR) {
1457 if (!state->an_complete)
1458 val &= ~BMSR_ANEGCOMPLETE;
1463 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1466 struct phy_device *phydev = pl->phydev;
1469 if (mdio_phy_id_is_c45(phy_id)) {
1470 prtad = mdio_phy_id_prtad(phy_id);
1471 devad = mdio_phy_id_devad(phy_id);
1472 devad = MII_ADDR_C45 | devad << 16 | reg;
1473 } else if (phydev->is_c45) {
1479 devad = __ffs(phydev->c45_ids.devices_in_package);
1483 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1485 devad = MDIO_MMD_AN;
1486 if (reg == MII_ADVERTISE)
1487 reg = MDIO_AN_ADVERTISE;
1495 devad = MII_ADDR_C45 | devad << 16 | reg;
1500 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1503 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1504 unsigned int reg, unsigned int val)
1506 struct phy_device *phydev = pl->phydev;
1509 if (mdio_phy_id_is_c45(phy_id)) {
1510 prtad = mdio_phy_id_prtad(phy_id);
1511 devad = mdio_phy_id_devad(phy_id);
1512 devad = MII_ADDR_C45 | devad << 16 | reg;
1513 } else if (phydev->is_c45) {
1519 devad = __ffs(phydev->c45_ids.devices_in_package);
1523 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1525 devad = MDIO_MMD_AN;
1526 if (reg == MII_ADVERTISE)
1527 reg = MDIO_AN_ADVERTISE;
1535 devad = MII_ADDR_C45 | devad << 16 | reg;
1541 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1544 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1547 struct phylink_link_state state;
1550 switch (pl->link_an_mode) {
1553 phylink_get_fixed_state(pl, &state);
1554 val = phylink_mii_emul_read(reg, &state);
1563 phylink_mac_pcs_get_state(pl, &state);
1564 val = phylink_mii_emul_read(reg, &state);
1569 return val & 0xffff;
1572 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1573 unsigned int reg, unsigned int val)
1575 switch (pl->link_an_mode) {
1590 * phylink_mii_ioctl() - generic mii ioctl interface
1591 * @pl: a pointer to a &struct phylink returned from phylink_create()
1592 * @ifr: a pointer to a &struct ifreq for socket ioctls
1593 * @cmd: ioctl cmd to execute
1595 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1596 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1598 * Returns: zero on success or negative error code.
1601 * read register from the current PHY.
1603 * read register from the specified PHY.
1605 * set a register on the specified PHY.
1607 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1609 struct mii_ioctl_data *mii = if_mii(ifr);
1615 /* PHYs only exist for MLO_AN_PHY and SGMII */
1618 mii->phy_id = pl->phydev->mdio.addr;
1622 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1630 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1635 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1645 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1653 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1665 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1667 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
1669 struct phylink *pl = upstream;
1671 pl->netdev->sfp_bus = bus;
1674 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
1676 struct phylink *pl = upstream;
1678 pl->netdev->sfp_bus = NULL;
1681 static int phylink_sfp_module_insert(void *upstream,
1682 const struct sfp_eeprom_id *id)
1684 struct phylink *pl = upstream;
1685 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1686 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
1687 struct phylink_link_state config;
1688 phy_interface_t iface;
1695 sfp_parse_support(pl->sfp_bus, id, support);
1696 port = sfp_parse_port(pl->sfp_bus, id, support);
1698 memset(&config, 0, sizeof(config));
1699 linkmode_copy(config.advertising, support);
1700 config.interface = PHY_INTERFACE_MODE_NA;
1701 config.speed = SPEED_UNKNOWN;
1702 config.duplex = DUPLEX_UNKNOWN;
1703 config.pause = MLO_PAUSE_AN;
1704 config.an_enabled = pl->link_config.an_enabled;
1706 /* Ignore errors if we're expecting a PHY to attach later */
1707 ret = phylink_validate(pl, support, &config);
1709 phylink_err(pl, "validation with support %*pb failed: %d\n",
1710 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1714 linkmode_copy(support1, support);
1716 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1717 if (iface == PHY_INTERFACE_MODE_NA) {
1719 "selection of interface failed, advertisement %*pb\n",
1720 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1724 config.interface = iface;
1725 ret = phylink_validate(pl, support1, &config);
1727 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
1728 phylink_an_mode_str(MLO_AN_INBAND),
1729 phy_modes(config.interface),
1730 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1734 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
1735 phylink_an_mode_str(MLO_AN_INBAND),
1736 phy_modes(config.interface),
1737 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1739 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1742 changed = !linkmode_equal(pl->supported, support);
1744 linkmode_copy(pl->supported, support);
1745 linkmode_copy(pl->link_config.advertising, config.advertising);
1748 if (pl->link_an_mode != MLO_AN_INBAND ||
1749 pl->link_config.interface != config.interface) {
1750 pl->link_config.interface = config.interface;
1751 pl->link_an_mode = MLO_AN_INBAND;
1755 phylink_info(pl, "switched to %s/%s link mode\n",
1756 phylink_an_mode_str(MLO_AN_INBAND),
1757 phy_modes(config.interface));
1760 pl->link_port = port;
1762 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1763 &pl->phylink_disable_state))
1764 phylink_mac_config(pl, &pl->link_config);
1769 static void phylink_sfp_link_down(void *upstream)
1771 struct phylink *pl = upstream;
1775 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
1778 static void phylink_sfp_link_up(void *upstream)
1780 struct phylink *pl = upstream;
1784 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1785 phylink_run_resolve(pl);
1788 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1790 struct phylink *pl = upstream;
1792 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
1795 static void phylink_sfp_disconnect_phy(void *upstream)
1797 phylink_disconnect_phy(upstream);
1800 static const struct sfp_upstream_ops sfp_phylink_ops = {
1801 .attach = phylink_sfp_attach,
1802 .detach = phylink_sfp_detach,
1803 .module_insert = phylink_sfp_module_insert,
1804 .link_up = phylink_sfp_link_up,
1805 .link_down = phylink_sfp_link_down,
1806 .connect_phy = phylink_sfp_connect_phy,
1807 .disconnect_phy = phylink_sfp_disconnect_phy,
1810 /* Helpers for MAC drivers */
1813 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1814 * @state: a pointer to a &struct phylink_link_state
1816 * Inspect the interface mode, advertising mask or forced speed and
1817 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1818 * the interface mode to suit. @state->interface is appropriately
1819 * updated, and the advertising mask has the "other" baseX_Full flag
1822 void phylink_helper_basex_speed(struct phylink_link_state *state)
1824 if (phy_interface_mode_is_8023z(state->interface)) {
1825 bool want_2500 = state->an_enabled ?
1826 phylink_test(state->advertising, 2500baseX_Full) :
1827 state->speed == SPEED_2500;
1830 phylink_clear(state->advertising, 1000baseX_Full);
1831 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1833 phylink_clear(state->advertising, 2500baseX_Full);
1834 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1838 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1840 MODULE_LICENSE("GPL v2");