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 #define phylink_dbg(pl, fmt, ...) \
91 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
94 * phylink_set_port_modes() - set the port type modes in the ethtool mask
95 * @mask: ethtool link mode mask
97 * Sets all the port type modes in the ethtool mask. MAC drivers should
98 * use this in their 'validate' callback.
100 void phylink_set_port_modes(unsigned long *mask)
102 phylink_set(mask, TP);
103 phylink_set(mask, AUI);
104 phylink_set(mask, MII);
105 phylink_set(mask, FIBRE);
106 phylink_set(mask, BNC);
107 phylink_set(mask, Backplane);
109 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
111 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
113 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
115 phylink_set_port_modes(tmp);
116 phylink_set(tmp, Autoneg);
117 phylink_set(tmp, Pause);
118 phylink_set(tmp, Asym_Pause);
120 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
122 return linkmode_empty(tmp);
125 static const char *phylink_an_mode_str(unsigned int mode)
127 static const char *modestr[] = {
128 [MLO_AN_PHY] = "phy",
129 [MLO_AN_FIXED] = "fixed",
130 [MLO_AN_INBAND] = "inband",
133 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
136 static int phylink_validate(struct phylink *pl, unsigned long *supported,
137 struct phylink_link_state *state)
139 pl->ops->validate(pl->config, supported, state);
141 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
144 static int phylink_parse_fixedlink(struct phylink *pl,
145 struct fwnode_handle *fwnode)
147 struct fwnode_handle *fixed_node;
148 const struct phy_setting *s;
149 struct gpio_desc *desc;
153 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
155 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
157 pl->link_config.speed = speed;
158 pl->link_config.duplex = DUPLEX_HALF;
160 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
161 pl->link_config.duplex = DUPLEX_FULL;
163 /* We treat the "pause" and "asym-pause" terminology as
164 * defining the link partner's ability. */
165 if (fwnode_property_read_bool(fixed_node, "pause"))
166 pl->link_config.pause |= MLO_PAUSE_SYM;
167 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
168 pl->link_config.pause |= MLO_PAUSE_ASYM;
171 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
175 pl->link_gpio = desc;
176 else if (desc == ERR_PTR(-EPROBE_DEFER))
179 fwnode_handle_put(fixed_node);
186 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
188 if (ret != ARRAY_SIZE(prop)) {
189 phylink_err(pl, "broken fixed-link?\n");
193 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
194 prop, ARRAY_SIZE(prop));
196 pl->link_config.duplex = prop[1] ?
197 DUPLEX_FULL : DUPLEX_HALF;
198 pl->link_config.speed = prop[2];
200 pl->link_config.pause |= MLO_PAUSE_SYM;
202 pl->link_config.pause |= MLO_PAUSE_ASYM;
206 if (pl->link_config.speed > SPEED_1000 &&
207 pl->link_config.duplex != DUPLEX_FULL)
208 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
209 pl->link_config.speed);
211 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
212 linkmode_copy(pl->link_config.advertising, pl->supported);
213 phylink_validate(pl, pl->supported, &pl->link_config);
215 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
216 pl->supported, true);
217 linkmode_zero(pl->supported);
218 phylink_set(pl->supported, MII);
220 __set_bit(s->bit, pl->supported);
222 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
223 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
224 pl->link_config.speed);
227 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
230 pl->link_config.link = 1;
231 pl->link_config.an_complete = 1;
236 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
238 struct fwnode_handle *dn;
241 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
242 if (dn || fwnode_property_present(fwnode, "fixed-link"))
243 pl->link_an_mode = MLO_AN_FIXED;
244 fwnode_handle_put(dn);
246 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
247 strcmp(managed, "in-band-status") == 0) {
248 if (pl->link_an_mode == MLO_AN_FIXED) {
250 "can't use both fixed-link and in-band-status\n");
254 linkmode_zero(pl->supported);
255 phylink_set(pl->supported, MII);
256 phylink_set(pl->supported, Autoneg);
257 phylink_set(pl->supported, Asym_Pause);
258 phylink_set(pl->supported, Pause);
259 pl->link_config.an_enabled = true;
260 pl->link_an_mode = MLO_AN_INBAND;
262 switch (pl->link_config.interface) {
263 case PHY_INTERFACE_MODE_SGMII:
264 phylink_set(pl->supported, 10baseT_Half);
265 phylink_set(pl->supported, 10baseT_Full);
266 phylink_set(pl->supported, 100baseT_Half);
267 phylink_set(pl->supported, 100baseT_Full);
268 phylink_set(pl->supported, 1000baseT_Half);
269 phylink_set(pl->supported, 1000baseT_Full);
272 case PHY_INTERFACE_MODE_1000BASEX:
273 phylink_set(pl->supported, 1000baseX_Full);
276 case PHY_INTERFACE_MODE_2500BASEX:
277 phylink_set(pl->supported, 2500baseX_Full);
280 case PHY_INTERFACE_MODE_10GKR:
281 phylink_set(pl->supported, 10baseT_Half);
282 phylink_set(pl->supported, 10baseT_Full);
283 phylink_set(pl->supported, 100baseT_Half);
284 phylink_set(pl->supported, 100baseT_Full);
285 phylink_set(pl->supported, 1000baseT_Half);
286 phylink_set(pl->supported, 1000baseT_Full);
287 phylink_set(pl->supported, 1000baseX_Full);
288 phylink_set(pl->supported, 10000baseKR_Full);
289 phylink_set(pl->supported, 10000baseCR_Full);
290 phylink_set(pl->supported, 10000baseSR_Full);
291 phylink_set(pl->supported, 10000baseLR_Full);
292 phylink_set(pl->supported, 10000baseLRM_Full);
293 phylink_set(pl->supported, 10000baseER_Full);
298 "incorrect link mode %s for in-band status\n",
299 phy_modes(pl->link_config.interface));
303 linkmode_copy(pl->link_config.advertising, pl->supported);
305 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
307 "failed to validate link configuration for in-band status\n");
315 static void phylink_mac_config(struct phylink *pl,
316 const struct phylink_link_state *state)
319 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
320 __func__, phylink_an_mode_str(pl->link_an_mode),
321 phy_modes(state->interface),
322 phy_speed_to_str(state->speed),
323 phy_duplex_to_str(state->duplex),
324 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
325 state->pause, state->link, state->an_enabled);
327 pl->ops->mac_config(pl->config, pl->link_an_mode, state);
330 static void phylink_mac_config_up(struct phylink *pl,
331 const struct phylink_link_state *state)
334 phylink_mac_config(pl, state);
337 static void phylink_mac_an_restart(struct phylink *pl)
339 if (pl->link_config.an_enabled &&
340 phy_interface_mode_is_8023z(pl->link_config.interface))
341 pl->ops->mac_an_restart(pl->config);
344 static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
347 linkmode_copy(state->advertising, pl->link_config.advertising);
348 linkmode_zero(state->lp_advertising);
349 state->interface = pl->link_config.interface;
350 state->an_enabled = pl->link_config.an_enabled;
351 state->speed = SPEED_UNKNOWN;
352 state->duplex = DUPLEX_UNKNOWN;
353 state->pause = MLO_PAUSE_NONE;
354 state->an_complete = 0;
357 return pl->ops->mac_link_state(pl->config, state);
360 /* The fixed state is... fixed except for the link state,
361 * which may be determined by a GPIO or a callback.
363 static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
365 *state = pl->link_config;
366 if (pl->get_fixed_state)
367 pl->get_fixed_state(pl->netdev, state);
368 else if (pl->link_gpio)
369 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
372 /* Flow control is resolved according to our and the link partners
373 * advertisements using the following drawn from the 802.3 specs:
374 * Local device Link partner
375 * Pause AsymDir Pause AsymDir Result
380 static void phylink_resolve_flow(struct phylink *pl,
381 struct phylink_link_state *state)
385 if (pl->link_config.pause & MLO_PAUSE_AN) {
388 if (phylink_test(pl->link_config.advertising, Pause))
389 pause |= MLO_PAUSE_SYM;
390 if (phylink_test(pl->link_config.advertising, Asym_Pause))
391 pause |= MLO_PAUSE_ASYM;
393 pause &= state->pause;
395 if (pause & MLO_PAUSE_SYM)
396 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
397 else if (pause & MLO_PAUSE_ASYM)
398 new_pause = state->pause & MLO_PAUSE_SYM ?
399 MLO_PAUSE_RX : MLO_PAUSE_TX;
401 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
404 state->pause &= ~MLO_PAUSE_TXRX_MASK;
405 state->pause |= new_pause;
408 static const char *phylink_pause_to_str(int pause)
410 switch (pause & MLO_PAUSE_TXRX_MASK) {
411 case MLO_PAUSE_TX | MLO_PAUSE_RX:
422 static void phylink_mac_link_up(struct phylink *pl,
423 struct phylink_link_state link_state)
425 struct net_device *ndev = pl->netdev;
427 pl->cur_interface = link_state.interface;
428 pl->ops->mac_link_up(pl->config, pl->link_an_mode,
429 pl->phy_state.interface,
433 netif_carrier_on(ndev);
436 "Link is Up - %s/%s - flow control %s\n",
437 phy_speed_to_str(link_state.speed),
438 phy_duplex_to_str(link_state.duplex),
439 phylink_pause_to_str(link_state.pause));
442 static void phylink_mac_link_down(struct phylink *pl)
444 struct net_device *ndev = pl->netdev;
447 netif_carrier_off(ndev);
448 pl->ops->mac_link_down(pl->config, pl->link_an_mode,
450 phylink_info(pl, "Link is Down\n");
453 static void phylink_resolve(struct work_struct *w)
455 struct phylink *pl = container_of(w, struct phylink, resolve);
456 struct phylink_link_state link_state;
457 struct net_device *ndev = pl->netdev;
460 mutex_lock(&pl->state_mutex);
461 if (pl->phylink_disable_state) {
462 pl->mac_link_dropped = false;
463 link_state.link = false;
464 } else if (pl->mac_link_dropped) {
465 link_state.link = false;
467 switch (pl->link_an_mode) {
469 link_state = pl->phy_state;
470 phylink_resolve_flow(pl, &link_state);
471 phylink_mac_config_up(pl, &link_state);
475 phylink_get_fixed_state(pl, &link_state);
476 phylink_mac_config_up(pl, &link_state);
480 phylink_get_mac_state(pl, &link_state);
482 /* If we have a phy, the "up" state is the union of
483 * both the PHY and the MAC */
485 link_state.link &= pl->phy_state.link;
487 /* Only update if the PHY link is up */
488 if (pl->phydev && pl->phy_state.link) {
489 link_state.interface = pl->phy_state.interface;
491 /* If we have a PHY, we need to update with
492 * the pause mode bits. */
493 link_state.pause |= pl->phy_state.pause;
494 phylink_resolve_flow(pl, &link_state);
495 phylink_mac_config(pl, &link_state);
502 link_changed = (link_state.link != netif_carrier_ok(ndev));
504 link_changed = (link_state.link != pl->old_link_state);
507 pl->old_link_state = link_state.link;
508 if (!link_state.link)
509 phylink_mac_link_down(pl);
511 phylink_mac_link_up(pl, link_state);
513 if (!link_state.link && pl->mac_link_dropped) {
514 pl->mac_link_dropped = false;
515 queue_work(system_power_efficient_wq, &pl->resolve);
517 mutex_unlock(&pl->state_mutex);
520 static void phylink_run_resolve(struct phylink *pl)
522 if (!pl->phylink_disable_state)
523 queue_work(system_power_efficient_wq, &pl->resolve);
526 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
528 unsigned long state = pl->phylink_disable_state;
530 set_bit(bit, &pl->phylink_disable_state);
532 queue_work(system_power_efficient_wq, &pl->resolve);
533 flush_work(&pl->resolve);
537 static void phylink_fixed_poll(struct timer_list *t)
539 struct phylink *pl = container_of(t, struct phylink, link_poll);
541 mod_timer(t, jiffies + HZ);
543 phylink_run_resolve(pl);
546 static const struct sfp_upstream_ops sfp_phylink_ops;
548 static int phylink_register_sfp(struct phylink *pl,
549 struct fwnode_handle *fwnode)
551 struct fwnode_reference_args ref;
557 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
563 phylink_err(pl, "unable to parse \"sfp\" node: %d\n",
568 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl, &sfp_phylink_ops);
576 * phylink_create() - create a phylink instance
577 * @ndev: a pointer to the &struct net_device
578 * @fwnode: a pointer to a &struct fwnode_handle describing the network
580 * @iface: the desired link mode defined by &typedef phy_interface_t
581 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
583 * Create a new phylink instance, and parse the link parameters found in @np.
584 * This will parse in-band modes, fixed-link or SFP configuration.
586 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
587 * must use IS_ERR() to check for errors from this function.
589 struct phylink *phylink_create(struct phylink_config *config,
590 struct fwnode_handle *fwnode,
591 phy_interface_t iface,
592 const struct phylink_mac_ops *ops)
597 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
599 return ERR_PTR(-ENOMEM);
601 mutex_init(&pl->state_mutex);
602 INIT_WORK(&pl->resolve, phylink_resolve);
605 if (config->type == PHYLINK_NETDEV) {
606 pl->netdev = to_net_dev(config->dev);
607 } else if (config->type == PHYLINK_DEV) {
608 pl->dev = config->dev;
611 return ERR_PTR(-EINVAL);
614 pl->phy_state.interface = iface;
615 pl->link_interface = iface;
616 if (iface == PHY_INTERFACE_MODE_MOCA)
617 pl->link_port = PORT_BNC;
619 pl->link_port = PORT_MII;
620 pl->link_config.interface = iface;
621 pl->link_config.pause = MLO_PAUSE_AN;
622 pl->link_config.speed = SPEED_UNKNOWN;
623 pl->link_config.duplex = DUPLEX_UNKNOWN;
624 pl->link_config.an_enabled = true;
626 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
627 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
629 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
630 linkmode_copy(pl->link_config.advertising, pl->supported);
631 phylink_validate(pl, pl->supported, &pl->link_config);
633 ret = phylink_parse_mode(pl, fwnode);
639 if (pl->link_an_mode == MLO_AN_FIXED) {
640 ret = phylink_parse_fixedlink(pl, fwnode);
647 ret = phylink_register_sfp(pl, fwnode);
655 EXPORT_SYMBOL_GPL(phylink_create);
658 * phylink_destroy() - cleanup and destroy the phylink instance
659 * @pl: a pointer to a &struct phylink returned from phylink_create()
661 * Destroy a phylink instance. Any PHY that has been attached must have been
662 * cleaned up via phylink_disconnect_phy() prior to calling this function.
664 void phylink_destroy(struct phylink *pl)
667 sfp_unregister_upstream(pl->sfp_bus);
669 gpiod_put(pl->link_gpio);
671 cancel_work_sync(&pl->resolve);
674 EXPORT_SYMBOL_GPL(phylink_destroy);
676 static void phylink_phy_change(struct phy_device *phydev, bool up,
679 struct phylink *pl = phydev->phylink;
681 mutex_lock(&pl->state_mutex);
682 pl->phy_state.speed = phydev->speed;
683 pl->phy_state.duplex = phydev->duplex;
684 pl->phy_state.pause = MLO_PAUSE_NONE;
686 pl->phy_state.pause |= MLO_PAUSE_SYM;
687 if (phydev->asym_pause)
688 pl->phy_state.pause |= MLO_PAUSE_ASYM;
689 pl->phy_state.interface = phydev->interface;
690 pl->phy_state.link = up;
691 mutex_unlock(&pl->state_mutex);
693 phylink_run_resolve(pl);
695 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
696 phy_modes(phydev->interface),
697 phy_speed_to_str(phydev->speed),
698 phy_duplex_to_str(phydev->duplex));
701 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
703 struct phylink_link_state config;
704 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
707 memset(&config, 0, sizeof(config));
708 linkmode_copy(supported, phy->supported);
709 linkmode_copy(config.advertising, phy->advertising);
710 config.interface = pl->link_config.interface;
713 * This is the new way of dealing with flow control for PHYs,
714 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
715 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
716 * using our validate call to the MAC, we rely upon the MAC
717 * clearing the bits from both supported and advertising fields.
719 if (phylink_test(supported, Pause))
720 phylink_set(config.advertising, Pause);
721 if (phylink_test(supported, Asym_Pause))
722 phylink_set(config.advertising, Asym_Pause);
724 ret = phylink_validate(pl, supported, &config);
729 phy->phy_link_change = phylink_phy_change;
732 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
735 mutex_lock(&phy->lock);
736 mutex_lock(&pl->state_mutex);
738 linkmode_copy(pl->supported, supported);
739 linkmode_copy(pl->link_config.advertising, config.advertising);
741 /* Restrict the phy advertisement according to the MAC support. */
742 linkmode_copy(phy->advertising, config.advertising);
743 mutex_unlock(&pl->state_mutex);
744 mutex_unlock(&phy->lock);
747 "phy: setting supported %*pb advertising %*pb\n",
748 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
749 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
751 if (phy_interrupt_is_valid(phy))
752 phy_request_interrupt(phy);
757 static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
758 phy_interface_t interface)
762 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
763 (pl->link_an_mode == MLO_AN_INBAND &&
764 phy_interface_mode_is_8023z(interface))))
770 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
774 ret = phylink_bringup_phy(pl, phy);
782 * phylink_connect_phy() - connect a PHY to the phylink instance
783 * @pl: a pointer to a &struct phylink returned from phylink_create()
784 * @phy: a pointer to a &struct phy_device.
786 * Connect @phy to the phylink instance specified by @pl by calling
787 * phy_attach_direct(). Configure the @phy according to the MAC driver's
788 * capabilities, start the PHYLIB state machine and enable any interrupts
789 * that the PHY supports.
791 * This updates the phylink's ethtool supported and advertising link mode
794 * Returns 0 on success or a negative errno.
796 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
798 /* Use PHY device/driver interface */
799 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
800 pl->link_interface = phy->interface;
801 pl->link_config.interface = pl->link_interface;
804 return __phylink_connect_phy(pl, phy, pl->link_interface);
806 EXPORT_SYMBOL_GPL(phylink_connect_phy);
809 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
810 * @pl: a pointer to a &struct phylink returned from phylink_create()
811 * @dn: a pointer to a &struct device_node.
812 * @flags: PHY-specific flags to communicate to the PHY device driver
814 * Connect the phy specified in the device node @dn to the phylink instance
815 * specified by @pl. Actions specified in phylink_connect_phy() will be
818 * Returns 0 on success or a negative errno.
820 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
823 struct device_node *phy_node;
824 struct phy_device *phy_dev;
827 /* Fixed links and 802.3z are handled without needing a PHY */
828 if (pl->link_an_mode == MLO_AN_FIXED ||
829 (pl->link_an_mode == MLO_AN_INBAND &&
830 phy_interface_mode_is_8023z(pl->link_interface)))
833 phy_node = of_parse_phandle(dn, "phy-handle", 0);
835 phy_node = of_parse_phandle(dn, "phy", 0);
837 phy_node = of_parse_phandle(dn, "phy-device", 0);
840 if (pl->link_an_mode == MLO_AN_PHY)
845 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
847 /* We're done with the phy_node handle */
848 of_node_put(phy_node);
853 ret = phylink_bringup_phy(pl, phy_dev);
859 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
862 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
864 * @pl: a pointer to a &struct phylink returned from phylink_create()
866 * Disconnect any current PHY from the phylink instance described by @pl.
868 void phylink_disconnect_phy(struct phylink *pl)
870 struct phy_device *phy;
876 mutex_lock(&phy->lock);
877 mutex_lock(&pl->state_mutex);
879 mutex_unlock(&pl->state_mutex);
880 mutex_unlock(&phy->lock);
881 flush_work(&pl->resolve);
886 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
889 * phylink_fixed_state_cb() - allow setting a fixed link callback
890 * @pl: a pointer to a &struct phylink returned from phylink_create()
891 * @cb: callback to execute to determine the fixed link state.
893 * The MAC driver should call this driver when the state of its link
894 * can be determined through e.g: an out of band MMIO register.
896 int phylink_fixed_state_cb(struct phylink *pl,
897 void (*cb)(struct net_device *dev,
898 struct phylink_link_state *state))
900 /* It does not make sense to let the link be overriden unless we use
903 if (pl->link_an_mode != MLO_AN_FIXED)
906 mutex_lock(&pl->state_mutex);
907 pl->get_fixed_state = cb;
908 mutex_unlock(&pl->state_mutex);
912 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
915 * phylink_mac_change() - notify phylink of a change in MAC state
916 * @pl: a pointer to a &struct phylink returned from phylink_create()
917 * @up: indicates whether the link is currently up.
919 * The MAC driver should call this driver when the state of its link
920 * changes (eg, link failure, new negotiation results, etc.)
922 void phylink_mac_change(struct phylink *pl, bool up)
925 pl->mac_link_dropped = true;
926 phylink_run_resolve(pl);
927 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
929 EXPORT_SYMBOL_GPL(phylink_mac_change);
931 static irqreturn_t phylink_link_handler(int irq, void *data)
933 struct phylink *pl = data;
935 phylink_run_resolve(pl);
941 * phylink_start() - start a phylink instance
942 * @pl: a pointer to a &struct phylink returned from phylink_create()
944 * Start the phylink instance specified by @pl, configuring the MAC for the
945 * desired link mode(s) and negotiation style. This should be called from the
946 * network device driver's &struct net_device_ops ndo_open() method.
948 void phylink_start(struct phylink *pl)
952 phylink_info(pl, "configuring for %s/%s link mode\n",
953 phylink_an_mode_str(pl->link_an_mode),
954 phy_modes(pl->link_config.interface));
956 /* Always set the carrier off */
958 netif_carrier_off(pl->netdev);
960 /* Apply the link configuration to the MAC when starting. This allows
961 * a fixed-link to start with the correct parameters, and also
962 * ensures that we set the appropriate advertisement for Serdes links.
964 phylink_resolve_flow(pl, &pl->link_config);
965 phylink_mac_config(pl, &pl->link_config);
967 /* Restart autonegotiation if using 802.3z to ensure that the link
968 * parameters are properly negotiated. This is necessary for DSA
969 * switches using 802.3z negotiation to ensure they see our modes.
971 phylink_mac_an_restart(pl);
973 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
974 phylink_run_resolve(pl);
976 if (pl->link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
977 int irq = gpiod_to_irq(pl->link_gpio);
980 if (!request_irq(irq, phylink_link_handler,
981 IRQF_TRIGGER_RISING |
982 IRQF_TRIGGER_FALLING,
989 mod_timer(&pl->link_poll, jiffies + HZ);
991 if (pl->link_an_mode == MLO_AN_FIXED && pl->get_fixed_state)
992 mod_timer(&pl->link_poll, jiffies + HZ);
994 sfp_upstream_start(pl->sfp_bus);
996 phy_start(pl->phydev);
998 EXPORT_SYMBOL_GPL(phylink_start);
1001 * phylink_stop() - stop a phylink instance
1002 * @pl: a pointer to a &struct phylink returned from phylink_create()
1004 * Stop the phylink instance specified by @pl. This should be called from the
1005 * network device driver's &struct net_device_ops ndo_stop() method. The
1006 * network device's carrier state should not be changed prior to calling this
1009 void phylink_stop(struct phylink *pl)
1014 phy_stop(pl->phydev);
1016 sfp_upstream_stop(pl->sfp_bus);
1017 del_timer_sync(&pl->link_poll);
1019 free_irq(pl->link_irq, pl);
1023 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1025 EXPORT_SYMBOL_GPL(phylink_stop);
1028 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1029 * @pl: a pointer to a &struct phylink returned from phylink_create()
1030 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1032 * Read the wake on lan parameters from the PHY attached to the phylink
1033 * instance specified by @pl. If no PHY is currently attached, report no
1034 * support for wake on lan.
1036 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1044 phy_ethtool_get_wol(pl->phydev, wol);
1046 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1049 * phylink_ethtool_set_wol() - set wake on lan parameters
1050 * @pl: a pointer to a &struct phylink returned from phylink_create()
1051 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1053 * Set the wake on lan parameters for the PHY attached to the phylink
1054 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1057 * Returns zero on success or negative errno code.
1059 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1061 int ret = -EOPNOTSUPP;
1066 ret = phy_ethtool_set_wol(pl->phydev, wol);
1070 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1072 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1074 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1076 linkmode_zero(mask);
1077 phylink_set_port_modes(mask);
1079 linkmode_and(dst, dst, mask);
1080 linkmode_or(dst, dst, b);
1083 static void phylink_get_ksettings(const struct phylink_link_state *state,
1084 struct ethtool_link_ksettings *kset)
1086 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1087 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1088 kset->base.speed = state->speed;
1089 kset->base.duplex = state->duplex;
1090 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1095 * phylink_ethtool_ksettings_get() - get the current link settings
1096 * @pl: a pointer to a &struct phylink returned from phylink_create()
1097 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1099 * Read the current link settings for the phylink instance specified by @pl.
1100 * This will be the link settings read from the MAC, PHY or fixed link
1101 * settings depending on the current negotiation mode.
1103 int phylink_ethtool_ksettings_get(struct phylink *pl,
1104 struct ethtool_link_ksettings *kset)
1106 struct phylink_link_state link_state;
1111 phy_ethtool_ksettings_get(pl->phydev, kset);
1113 kset->base.port = pl->link_port;
1116 linkmode_copy(kset->link_modes.supported, pl->supported);
1118 switch (pl->link_an_mode) {
1120 /* We are using fixed settings. Report these as the
1121 * current link settings - and note that these also
1122 * represent the supported speeds/duplex/pause modes.
1124 phylink_get_fixed_state(pl, &link_state);
1125 phylink_get_ksettings(&link_state, kset);
1129 /* If there is a phy attached, then use the reported
1130 * settings from the phy with no modification.
1135 phylink_get_mac_state(pl, &link_state);
1137 /* The MAC is reporting the link results from its own PCS
1138 * layer via in-band status. Report these as the current
1141 phylink_get_ksettings(&link_state, kset);
1147 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1150 * phylink_ethtool_ksettings_set() - set the link settings
1151 * @pl: a pointer to a &struct phylink returned from phylink_create()
1152 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1154 int phylink_ethtool_ksettings_set(struct phylink *pl,
1155 const struct ethtool_link_ksettings *kset)
1157 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1158 struct ethtool_link_ksettings our_kset;
1159 struct phylink_link_state config;
1164 if (kset->base.autoneg != AUTONEG_DISABLE &&
1165 kset->base.autoneg != AUTONEG_ENABLE)
1168 linkmode_copy(support, pl->supported);
1169 config = pl->link_config;
1171 /* Mask out unsupported advertisements */
1172 linkmode_and(config.advertising, kset->link_modes.advertising,
1175 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1176 if (kset->base.autoneg == AUTONEG_DISABLE) {
1177 const struct phy_setting *s;
1179 /* Autonegotiation disabled, select a suitable speed and
1182 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1187 /* If we have a fixed link (as specified by firmware), refuse
1188 * to change link parameters.
1190 if (pl->link_an_mode == MLO_AN_FIXED &&
1191 (s->speed != pl->link_config.speed ||
1192 s->duplex != pl->link_config.duplex))
1195 config.speed = s->speed;
1196 config.duplex = s->duplex;
1197 config.an_enabled = false;
1199 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1201 /* If we have a fixed link, refuse to enable autonegotiation */
1202 if (pl->link_an_mode == MLO_AN_FIXED)
1205 config.speed = SPEED_UNKNOWN;
1206 config.duplex = DUPLEX_UNKNOWN;
1207 config.an_enabled = true;
1209 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1212 if (phylink_validate(pl, support, &config))
1215 /* If autonegotiation is enabled, we must have an advertisement */
1216 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1220 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1221 our_kset.base.speed = config.speed;
1222 our_kset.base.duplex = config.duplex;
1224 /* If we have a PHY, configure the phy */
1226 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1231 mutex_lock(&pl->state_mutex);
1232 /* Configure the MAC to match the new settings */
1233 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1234 pl->link_config.interface = config.interface;
1235 pl->link_config.speed = our_kset.base.speed;
1236 pl->link_config.duplex = our_kset.base.duplex;
1237 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1239 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1240 phylink_mac_config(pl, &pl->link_config);
1241 phylink_mac_an_restart(pl);
1243 mutex_unlock(&pl->state_mutex);
1247 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1250 * phylink_ethtool_nway_reset() - restart negotiation
1251 * @pl: a pointer to a &struct phylink returned from phylink_create()
1253 * Restart negotiation for the phylink instance specified by @pl. This will
1254 * cause any attached phy to restart negotiation with the link partner, and
1255 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1258 * Returns zero on success, or negative error code.
1260 int phylink_ethtool_nway_reset(struct phylink *pl)
1267 ret = phy_restart_aneg(pl->phydev);
1268 phylink_mac_an_restart(pl);
1272 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1275 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1276 * @pl: a pointer to a &struct phylink returned from phylink_create()
1277 * @pause: a pointer to a &struct ethtool_pauseparam
1279 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1280 struct ethtool_pauseparam *pause)
1284 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1285 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1286 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1288 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1291 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1292 * @pl: a pointer to a &struct phylink returned from phylink_create()
1293 * @pause: a pointer to a &struct ethtool_pauseparam
1295 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1296 struct ethtool_pauseparam *pause)
1298 struct phylink_link_state *config = &pl->link_config;
1302 if (!phylink_test(pl->supported, Pause) &&
1303 !phylink_test(pl->supported, Asym_Pause))
1306 if (!phylink_test(pl->supported, Asym_Pause) &&
1307 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1310 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1313 config->pause |= MLO_PAUSE_AN;
1314 if (pause->rx_pause)
1315 config->pause |= MLO_PAUSE_RX;
1316 if (pause->tx_pause)
1317 config->pause |= MLO_PAUSE_TX;
1319 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1320 switch (pl->link_an_mode) {
1322 /* Silently mark the carrier down, and then trigger a resolve */
1324 netif_carrier_off(pl->netdev);
1325 phylink_run_resolve(pl);
1329 /* Should we allow fixed links to change against the config? */
1330 phylink_resolve_flow(pl, config);
1331 phylink_mac_config(pl, config);
1335 phylink_mac_config(pl, config);
1336 phylink_mac_an_restart(pl);
1343 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1346 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1348 * @pl: a pointer to a &struct phylink returned from phylink_create().
1350 * Read the Energy Efficient Ethernet error counter from the PHY associated
1351 * with the phylink instance specified by @pl.
1353 * Returns positive error counter value, or negative error code.
1355 int phylink_get_eee_err(struct phylink *pl)
1362 ret = phy_get_eee_err(pl->phydev);
1366 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1369 * phylink_init_eee() - init and check the EEE features
1370 * @pl: a pointer to a &struct phylink returned from phylink_create()
1371 * @clk_stop_enable: allow PHY to stop receive clock
1373 * Must be called either with RTNL held or within mac_link_up()
1375 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1377 int ret = -EOPNOTSUPP;
1380 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1384 EXPORT_SYMBOL_GPL(phylink_init_eee);
1387 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1388 * @pl: a pointer to a &struct phylink returned from phylink_create()
1389 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1391 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1393 int ret = -EOPNOTSUPP;
1398 ret = phy_ethtool_get_eee(pl->phydev, eee);
1402 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1405 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1406 * @pl: a pointer to a &struct phylink returned from phylink_create()
1407 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1409 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1411 int ret = -EOPNOTSUPP;
1416 ret = phy_ethtool_set_eee(pl->phydev, eee);
1420 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1422 /* This emulates MII registers for a fixed-mode phy operating as per the
1423 * passed in state. "aneg" defines if we report negotiation is possible.
1425 * FIXME: should deal with negotiation state too.
1427 static int phylink_mii_emul_read(unsigned int reg,
1428 struct phylink_link_state *state)
1430 struct fixed_phy_status fs;
1433 fs.link = state->link;
1434 fs.speed = state->speed;
1435 fs.duplex = state->duplex;
1436 fs.pause = state->pause & MLO_PAUSE_SYM;
1437 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1439 val = swphy_read_reg(reg, &fs);
1440 if (reg == MII_BMSR) {
1441 if (!state->an_complete)
1442 val &= ~BMSR_ANEGCOMPLETE;
1447 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1450 struct phy_device *phydev = pl->phydev;
1453 if (mdio_phy_id_is_c45(phy_id)) {
1454 prtad = mdio_phy_id_prtad(phy_id);
1455 devad = mdio_phy_id_devad(phy_id);
1456 devad = MII_ADDR_C45 | devad << 16 | reg;
1457 } else if (phydev->is_c45) {
1463 devad = __ffs(phydev->c45_ids.devices_in_package);
1467 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1469 devad = MDIO_MMD_AN;
1470 if (reg == MII_ADVERTISE)
1471 reg = MDIO_AN_ADVERTISE;
1479 devad = MII_ADDR_C45 | devad << 16 | reg;
1484 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1487 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1488 unsigned int reg, unsigned int val)
1490 struct phy_device *phydev = pl->phydev;
1493 if (mdio_phy_id_is_c45(phy_id)) {
1494 prtad = mdio_phy_id_prtad(phy_id);
1495 devad = mdio_phy_id_devad(phy_id);
1496 devad = MII_ADDR_C45 | devad << 16 | reg;
1497 } else if (phydev->is_c45) {
1503 devad = __ffs(phydev->c45_ids.devices_in_package);
1507 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1509 devad = MDIO_MMD_AN;
1510 if (reg == MII_ADVERTISE)
1511 reg = MDIO_AN_ADVERTISE;
1519 devad = MII_ADDR_C45 | devad << 16 | reg;
1525 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1528 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1531 struct phylink_link_state state;
1534 switch (pl->link_an_mode) {
1537 phylink_get_fixed_state(pl, &state);
1538 val = phylink_mii_emul_read(reg, &state);
1547 val = phylink_get_mac_state(pl, &state);
1551 val = phylink_mii_emul_read(reg, &state);
1556 return val & 0xffff;
1559 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1560 unsigned int reg, unsigned int val)
1562 switch (pl->link_an_mode) {
1577 * phylink_mii_ioctl() - generic mii ioctl interface
1578 * @pl: a pointer to a &struct phylink returned from phylink_create()
1579 * @ifr: a pointer to a &struct ifreq for socket ioctls
1580 * @cmd: ioctl cmd to execute
1582 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1583 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1585 * Returns: zero on success or negative error code.
1588 * read register from the current PHY.
1590 * read register from the specified PHY.
1592 * set a register on the specified PHY.
1594 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1596 struct mii_ioctl_data *mii = if_mii(ifr);
1602 /* PHYs only exist for MLO_AN_PHY and SGMII */
1605 mii->phy_id = pl->phydev->mdio.addr;
1609 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1617 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1622 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1632 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1640 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1652 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1654 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
1656 struct phylink *pl = upstream;
1658 pl->netdev->sfp_bus = bus;
1661 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
1663 struct phylink *pl = upstream;
1665 pl->netdev->sfp_bus = NULL;
1668 static int phylink_sfp_module_insert(void *upstream,
1669 const struct sfp_eeprom_id *id)
1671 struct phylink *pl = upstream;
1672 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1673 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
1674 struct phylink_link_state config;
1675 phy_interface_t iface;
1682 sfp_parse_support(pl->sfp_bus, id, support);
1683 port = sfp_parse_port(pl->sfp_bus, id, support);
1685 memset(&config, 0, sizeof(config));
1686 linkmode_copy(config.advertising, support);
1687 config.interface = PHY_INTERFACE_MODE_NA;
1688 config.speed = SPEED_UNKNOWN;
1689 config.duplex = DUPLEX_UNKNOWN;
1690 config.pause = MLO_PAUSE_AN;
1691 config.an_enabled = pl->link_config.an_enabled;
1693 /* Ignore errors if we're expecting a PHY to attach later */
1694 ret = phylink_validate(pl, support, &config);
1696 phylink_err(pl, "validation with support %*pb failed: %d\n",
1697 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1701 linkmode_copy(support1, support);
1703 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1704 if (iface == PHY_INTERFACE_MODE_NA) {
1706 "selection of interface failed, advertisement %*pb\n",
1707 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1711 config.interface = iface;
1712 ret = phylink_validate(pl, support1, &config);
1714 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
1715 phylink_an_mode_str(MLO_AN_INBAND),
1716 phy_modes(config.interface),
1717 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1721 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
1722 phylink_an_mode_str(MLO_AN_INBAND),
1723 phy_modes(config.interface),
1724 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1726 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1729 changed = !bitmap_equal(pl->supported, support,
1730 __ETHTOOL_LINK_MODE_MASK_NBITS);
1732 linkmode_copy(pl->supported, support);
1733 linkmode_copy(pl->link_config.advertising, config.advertising);
1736 if (pl->link_an_mode != MLO_AN_INBAND ||
1737 pl->link_config.interface != config.interface) {
1738 pl->link_config.interface = config.interface;
1739 pl->link_an_mode = MLO_AN_INBAND;
1743 phylink_info(pl, "switched to %s/%s link mode\n",
1744 phylink_an_mode_str(MLO_AN_INBAND),
1745 phy_modes(config.interface));
1748 pl->link_port = port;
1750 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1751 &pl->phylink_disable_state))
1752 phylink_mac_config(pl, &pl->link_config);
1757 static void phylink_sfp_link_down(void *upstream)
1759 struct phylink *pl = upstream;
1763 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
1766 static void phylink_sfp_link_up(void *upstream)
1768 struct phylink *pl = upstream;
1772 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1773 phylink_run_resolve(pl);
1776 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1778 struct phylink *pl = upstream;
1780 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
1783 static void phylink_sfp_disconnect_phy(void *upstream)
1785 phylink_disconnect_phy(upstream);
1788 static const struct sfp_upstream_ops sfp_phylink_ops = {
1789 .attach = phylink_sfp_attach,
1790 .detach = phylink_sfp_detach,
1791 .module_insert = phylink_sfp_module_insert,
1792 .link_up = phylink_sfp_link_up,
1793 .link_down = phylink_sfp_link_down,
1794 .connect_phy = phylink_sfp_connect_phy,
1795 .disconnect_phy = phylink_sfp_disconnect_phy,
1798 /* Helpers for MAC drivers */
1801 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1802 * @state: a pointer to a &struct phylink_link_state
1804 * Inspect the interface mode, advertising mask or forced speed and
1805 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1806 * the interface mode to suit. @state->interface is appropriately
1807 * updated, and the advertising mask has the "other" baseX_Full flag
1810 void phylink_helper_basex_speed(struct phylink_link_state *state)
1812 if (phy_interface_mode_is_8023z(state->interface)) {
1813 bool want_2500 = state->an_enabled ?
1814 phylink_test(state->advertising, 2500baseX_Full) :
1815 state->speed == SPEED_2500;
1818 phylink_clear(state->advertising, 1000baseX_Full);
1819 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1821 phylink_clear(state->advertising, 2500baseX_Full);
1822 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1826 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1828 MODULE_LICENSE("GPL v2");