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);
219 phylink_set(pl->supported, Pause);
220 phylink_set(pl->supported, Asym_Pause);
222 __set_bit(s->bit, pl->supported);
224 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
225 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
226 pl->link_config.speed);
229 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
232 pl->link_config.link = 1;
233 pl->link_config.an_complete = 1;
238 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
240 struct fwnode_handle *dn;
243 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
244 if (dn || fwnode_property_present(fwnode, "fixed-link"))
245 pl->link_an_mode = MLO_AN_FIXED;
246 fwnode_handle_put(dn);
248 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
249 strcmp(managed, "in-band-status") == 0) {
250 if (pl->link_an_mode == MLO_AN_FIXED) {
252 "can't use both fixed-link and in-band-status\n");
256 linkmode_zero(pl->supported);
257 phylink_set(pl->supported, MII);
258 phylink_set(pl->supported, Autoneg);
259 phylink_set(pl->supported, Asym_Pause);
260 phylink_set(pl->supported, Pause);
261 pl->link_config.an_enabled = true;
262 pl->link_an_mode = MLO_AN_INBAND;
264 switch (pl->link_config.interface) {
265 case PHY_INTERFACE_MODE_SGMII:
266 phylink_set(pl->supported, 10baseT_Half);
267 phylink_set(pl->supported, 10baseT_Full);
268 phylink_set(pl->supported, 100baseT_Half);
269 phylink_set(pl->supported, 100baseT_Full);
270 phylink_set(pl->supported, 1000baseT_Half);
271 phylink_set(pl->supported, 1000baseT_Full);
274 case PHY_INTERFACE_MODE_1000BASEX:
275 phylink_set(pl->supported, 1000baseX_Full);
278 case PHY_INTERFACE_MODE_2500BASEX:
279 phylink_set(pl->supported, 2500baseX_Full);
282 case PHY_INTERFACE_MODE_10GKR:
283 phylink_set(pl->supported, 10baseT_Half);
284 phylink_set(pl->supported, 10baseT_Full);
285 phylink_set(pl->supported, 100baseT_Half);
286 phylink_set(pl->supported, 100baseT_Full);
287 phylink_set(pl->supported, 1000baseT_Half);
288 phylink_set(pl->supported, 1000baseT_Full);
289 phylink_set(pl->supported, 1000baseX_Full);
290 phylink_set(pl->supported, 10000baseKR_Full);
291 phylink_set(pl->supported, 10000baseCR_Full);
292 phylink_set(pl->supported, 10000baseSR_Full);
293 phylink_set(pl->supported, 10000baseLR_Full);
294 phylink_set(pl->supported, 10000baseLRM_Full);
295 phylink_set(pl->supported, 10000baseER_Full);
300 "incorrect link mode %s for in-band status\n",
301 phy_modes(pl->link_config.interface));
305 linkmode_copy(pl->link_config.advertising, pl->supported);
307 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
309 "failed to validate link configuration for in-band status\n");
317 static void phylink_mac_config(struct phylink *pl,
318 const struct phylink_link_state *state)
321 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
322 __func__, phylink_an_mode_str(pl->link_an_mode),
323 phy_modes(state->interface),
324 phy_speed_to_str(state->speed),
325 phy_duplex_to_str(state->duplex),
326 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
327 state->pause, state->link, state->an_enabled);
329 pl->ops->mac_config(pl->config, pl->link_an_mode, state);
332 static void phylink_mac_config_up(struct phylink *pl,
333 const struct phylink_link_state *state)
336 phylink_mac_config(pl, state);
339 static void phylink_mac_an_restart(struct phylink *pl)
341 if (pl->link_config.an_enabled &&
342 phy_interface_mode_is_8023z(pl->link_config.interface))
343 pl->ops->mac_an_restart(pl->config);
346 static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
349 linkmode_copy(state->advertising, pl->link_config.advertising);
350 linkmode_zero(state->lp_advertising);
351 state->interface = pl->link_config.interface;
352 state->an_enabled = pl->link_config.an_enabled;
353 state->speed = SPEED_UNKNOWN;
354 state->duplex = DUPLEX_UNKNOWN;
355 state->pause = MLO_PAUSE_NONE;
356 state->an_complete = 0;
359 return pl->ops->mac_link_state(pl->config, state);
362 /* The fixed state is... fixed except for the link state,
363 * which may be determined by a GPIO or a callback.
365 static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
367 *state = pl->link_config;
368 if (pl->get_fixed_state)
369 pl->get_fixed_state(pl->netdev, state);
370 else if (pl->link_gpio)
371 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
374 /* Flow control is resolved according to our and the link partners
375 * advertisements using the following drawn from the 802.3 specs:
376 * Local device Link partner
377 * Pause AsymDir Pause AsymDir Result
382 static void phylink_resolve_flow(struct phylink *pl,
383 struct phylink_link_state *state)
387 if (pl->link_config.pause & MLO_PAUSE_AN) {
390 if (phylink_test(pl->link_config.advertising, Pause))
391 pause |= MLO_PAUSE_SYM;
392 if (phylink_test(pl->link_config.advertising, Asym_Pause))
393 pause |= MLO_PAUSE_ASYM;
395 pause &= state->pause;
397 if (pause & MLO_PAUSE_SYM)
398 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
399 else if (pause & MLO_PAUSE_ASYM)
400 new_pause = state->pause & MLO_PAUSE_SYM ?
401 MLO_PAUSE_TX : MLO_PAUSE_RX;
403 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
406 state->pause &= ~MLO_PAUSE_TXRX_MASK;
407 state->pause |= new_pause;
410 static const char *phylink_pause_to_str(int pause)
412 switch (pause & MLO_PAUSE_TXRX_MASK) {
413 case MLO_PAUSE_TX | MLO_PAUSE_RX:
424 static void phylink_mac_link_up(struct phylink *pl,
425 struct phylink_link_state link_state)
427 struct net_device *ndev = pl->netdev;
429 pl->cur_interface = link_state.interface;
430 pl->ops->mac_link_up(pl->config, pl->link_an_mode,
431 pl->phy_state.interface,
435 netif_carrier_on(ndev);
438 "Link is Up - %s/%s - flow control %s\n",
439 phy_speed_to_str(link_state.speed),
440 phy_duplex_to_str(link_state.duplex),
441 phylink_pause_to_str(link_state.pause));
444 static void phylink_mac_link_down(struct phylink *pl)
446 struct net_device *ndev = pl->netdev;
449 netif_carrier_off(ndev);
450 pl->ops->mac_link_down(pl->config, pl->link_an_mode,
452 phylink_info(pl, "Link is Down\n");
455 static void phylink_resolve(struct work_struct *w)
457 struct phylink *pl = container_of(w, struct phylink, resolve);
458 struct phylink_link_state link_state;
459 struct net_device *ndev = pl->netdev;
462 mutex_lock(&pl->state_mutex);
463 if (pl->phylink_disable_state) {
464 pl->mac_link_dropped = false;
465 link_state.link = false;
466 } else if (pl->mac_link_dropped) {
467 link_state.link = false;
469 switch (pl->link_an_mode) {
471 link_state = pl->phy_state;
472 phylink_resolve_flow(pl, &link_state);
473 phylink_mac_config_up(pl, &link_state);
477 phylink_get_fixed_state(pl, &link_state);
478 phylink_mac_config_up(pl, &link_state);
482 phylink_get_mac_state(pl, &link_state);
484 /* If we have a phy, the "up" state is the union of
485 * both the PHY and the MAC */
487 link_state.link &= pl->phy_state.link;
489 /* Only update if the PHY link is up */
490 if (pl->phydev && pl->phy_state.link) {
491 link_state.interface = pl->phy_state.interface;
493 /* If we have a PHY, we need to update with
494 * the pause mode bits. */
495 link_state.pause |= pl->phy_state.pause;
496 phylink_resolve_flow(pl, &link_state);
497 phylink_mac_config(pl, &link_state);
504 link_changed = (link_state.link != netif_carrier_ok(ndev));
506 link_changed = (link_state.link != pl->old_link_state);
509 pl->old_link_state = link_state.link;
510 if (!link_state.link)
511 phylink_mac_link_down(pl);
513 phylink_mac_link_up(pl, link_state);
515 if (!link_state.link && pl->mac_link_dropped) {
516 pl->mac_link_dropped = false;
517 queue_work(system_power_efficient_wq, &pl->resolve);
519 mutex_unlock(&pl->state_mutex);
522 static void phylink_run_resolve(struct phylink *pl)
524 if (!pl->phylink_disable_state)
525 queue_work(system_power_efficient_wq, &pl->resolve);
528 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
530 unsigned long state = pl->phylink_disable_state;
532 set_bit(bit, &pl->phylink_disable_state);
534 queue_work(system_power_efficient_wq, &pl->resolve);
535 flush_work(&pl->resolve);
539 static void phylink_fixed_poll(struct timer_list *t)
541 struct phylink *pl = container_of(t, struct phylink, link_poll);
543 mod_timer(t, jiffies + HZ);
545 phylink_run_resolve(pl);
548 static const struct sfp_upstream_ops sfp_phylink_ops;
550 static int phylink_register_sfp(struct phylink *pl,
551 struct fwnode_handle *fwnode)
553 struct fwnode_reference_args ref;
559 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
565 phylink_err(pl, "unable to parse \"sfp\" node: %d\n",
570 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl, &sfp_phylink_ops);
578 * phylink_create() - create a phylink instance
579 * @config: a pointer to the target &struct phylink_config
580 * @fwnode: a pointer to a &struct fwnode_handle describing the network
582 * @iface: the desired link mode defined by &typedef phy_interface_t
583 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
585 * Create a new phylink instance, and parse the link parameters found in @np.
586 * This will parse in-band modes, fixed-link or SFP configuration.
588 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
589 * must use IS_ERR() to check for errors from this function.
591 struct phylink *phylink_create(struct phylink_config *config,
592 struct fwnode_handle *fwnode,
593 phy_interface_t iface,
594 const struct phylink_mac_ops *ops)
599 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
601 return ERR_PTR(-ENOMEM);
603 mutex_init(&pl->state_mutex);
604 INIT_WORK(&pl->resolve, phylink_resolve);
607 if (config->type == PHYLINK_NETDEV) {
608 pl->netdev = to_net_dev(config->dev);
609 } else if (config->type == PHYLINK_DEV) {
610 pl->dev = config->dev;
613 return ERR_PTR(-EINVAL);
616 pl->phy_state.interface = iface;
617 pl->link_interface = iface;
618 if (iface == PHY_INTERFACE_MODE_MOCA)
619 pl->link_port = PORT_BNC;
621 pl->link_port = PORT_MII;
622 pl->link_config.interface = iface;
623 pl->link_config.pause = MLO_PAUSE_AN;
624 pl->link_config.speed = SPEED_UNKNOWN;
625 pl->link_config.duplex = DUPLEX_UNKNOWN;
626 pl->link_config.an_enabled = true;
628 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
629 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
631 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
632 linkmode_copy(pl->link_config.advertising, pl->supported);
633 phylink_validate(pl, pl->supported, &pl->link_config);
635 ret = phylink_parse_mode(pl, fwnode);
641 if (pl->link_an_mode == MLO_AN_FIXED) {
642 ret = phylink_parse_fixedlink(pl, fwnode);
649 ret = phylink_register_sfp(pl, fwnode);
657 EXPORT_SYMBOL_GPL(phylink_create);
660 * phylink_destroy() - cleanup and destroy the phylink instance
661 * @pl: a pointer to a &struct phylink returned from phylink_create()
663 * Destroy a phylink instance. Any PHY that has been attached must have been
664 * cleaned up via phylink_disconnect_phy() prior to calling this function.
666 void phylink_destroy(struct phylink *pl)
669 sfp_unregister_upstream(pl->sfp_bus);
671 gpiod_put(pl->link_gpio);
673 cancel_work_sync(&pl->resolve);
676 EXPORT_SYMBOL_GPL(phylink_destroy);
678 static void phylink_phy_change(struct phy_device *phydev, bool up,
681 struct phylink *pl = phydev->phylink;
683 mutex_lock(&pl->state_mutex);
684 pl->phy_state.speed = phydev->speed;
685 pl->phy_state.duplex = phydev->duplex;
686 pl->phy_state.pause = MLO_PAUSE_NONE;
688 pl->phy_state.pause |= MLO_PAUSE_SYM;
689 if (phydev->asym_pause)
690 pl->phy_state.pause |= MLO_PAUSE_ASYM;
691 pl->phy_state.interface = phydev->interface;
692 pl->phy_state.link = up;
693 mutex_unlock(&pl->state_mutex);
695 phylink_run_resolve(pl);
697 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
698 phy_modes(phydev->interface),
699 phy_speed_to_str(phydev->speed),
700 phy_duplex_to_str(phydev->duplex));
703 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
705 struct phylink_link_state config;
706 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
709 memset(&config, 0, sizeof(config));
710 linkmode_copy(supported, phy->supported);
711 linkmode_copy(config.advertising, phy->advertising);
712 config.interface = pl->link_config.interface;
715 * This is the new way of dealing with flow control for PHYs,
716 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
717 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
718 * using our validate call to the MAC, we rely upon the MAC
719 * clearing the bits from both supported and advertising fields.
721 if (phylink_test(supported, Pause))
722 phylink_set(config.advertising, Pause);
723 if (phylink_test(supported, Asym_Pause))
724 phylink_set(config.advertising, Asym_Pause);
726 ret = phylink_validate(pl, supported, &config);
731 phy->phy_link_change = phylink_phy_change;
734 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
737 mutex_lock(&phy->lock);
738 mutex_lock(&pl->state_mutex);
740 linkmode_copy(pl->supported, supported);
741 linkmode_copy(pl->link_config.advertising, config.advertising);
743 /* Restrict the phy advertisement according to the MAC support. */
744 linkmode_copy(phy->advertising, config.advertising);
745 mutex_unlock(&pl->state_mutex);
746 mutex_unlock(&phy->lock);
749 "phy: setting supported %*pb advertising %*pb\n",
750 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
751 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
753 if (phy_interrupt_is_valid(phy))
754 phy_request_interrupt(phy);
759 static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
760 phy_interface_t interface)
764 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
765 (pl->link_an_mode == MLO_AN_INBAND &&
766 phy_interface_mode_is_8023z(interface))))
772 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
776 ret = phylink_bringup_phy(pl, phy);
784 * phylink_connect_phy() - connect a PHY to the phylink instance
785 * @pl: a pointer to a &struct phylink returned from phylink_create()
786 * @phy: a pointer to a &struct phy_device.
788 * Connect @phy to the phylink instance specified by @pl by calling
789 * phy_attach_direct(). Configure the @phy according to the MAC driver's
790 * capabilities, start the PHYLIB state machine and enable any interrupts
791 * that the PHY supports.
793 * This updates the phylink's ethtool supported and advertising link mode
796 * Returns 0 on success or a negative errno.
798 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
800 /* Use PHY device/driver interface */
801 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
802 pl->link_interface = phy->interface;
803 pl->link_config.interface = pl->link_interface;
806 return __phylink_connect_phy(pl, phy, pl->link_interface);
808 EXPORT_SYMBOL_GPL(phylink_connect_phy);
811 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
812 * @pl: a pointer to a &struct phylink returned from phylink_create()
813 * @dn: a pointer to a &struct device_node.
814 * @flags: PHY-specific flags to communicate to the PHY device driver
816 * Connect the phy specified in the device node @dn to the phylink instance
817 * specified by @pl. Actions specified in phylink_connect_phy() will be
820 * Returns 0 on success or a negative errno.
822 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
825 struct device_node *phy_node;
826 struct phy_device *phy_dev;
829 /* Fixed links and 802.3z are handled without needing a PHY */
830 if (pl->link_an_mode == MLO_AN_FIXED ||
831 (pl->link_an_mode == MLO_AN_INBAND &&
832 phy_interface_mode_is_8023z(pl->link_interface)))
835 phy_node = of_parse_phandle(dn, "phy-handle", 0);
837 phy_node = of_parse_phandle(dn, "phy", 0);
839 phy_node = of_parse_phandle(dn, "phy-device", 0);
842 if (pl->link_an_mode == MLO_AN_PHY)
847 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
849 /* We're done with the phy_node handle */
850 of_node_put(phy_node);
855 ret = phylink_bringup_phy(pl, phy_dev);
861 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
864 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
866 * @pl: a pointer to a &struct phylink returned from phylink_create()
868 * Disconnect any current PHY from the phylink instance described by @pl.
870 void phylink_disconnect_phy(struct phylink *pl)
872 struct phy_device *phy;
878 mutex_lock(&phy->lock);
879 mutex_lock(&pl->state_mutex);
881 mutex_unlock(&pl->state_mutex);
882 mutex_unlock(&phy->lock);
883 flush_work(&pl->resolve);
888 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
891 * phylink_fixed_state_cb() - allow setting a fixed link callback
892 * @pl: a pointer to a &struct phylink returned from phylink_create()
893 * @cb: callback to execute to determine the fixed link state.
895 * The MAC driver should call this driver when the state of its link
896 * can be determined through e.g: an out of band MMIO register.
898 int phylink_fixed_state_cb(struct phylink *pl,
899 void (*cb)(struct net_device *dev,
900 struct phylink_link_state *state))
902 /* It does not make sense to let the link be overriden unless we use
905 if (pl->link_an_mode != MLO_AN_FIXED)
908 mutex_lock(&pl->state_mutex);
909 pl->get_fixed_state = cb;
910 mutex_unlock(&pl->state_mutex);
914 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
917 * phylink_mac_change() - notify phylink of a change in MAC state
918 * @pl: a pointer to a &struct phylink returned from phylink_create()
919 * @up: indicates whether the link is currently up.
921 * The MAC driver should call this driver when the state of its link
922 * changes (eg, link failure, new negotiation results, etc.)
924 void phylink_mac_change(struct phylink *pl, bool up)
927 pl->mac_link_dropped = true;
928 phylink_run_resolve(pl);
929 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
931 EXPORT_SYMBOL_GPL(phylink_mac_change);
933 static irqreturn_t phylink_link_handler(int irq, void *data)
935 struct phylink *pl = data;
937 phylink_run_resolve(pl);
943 * phylink_start() - start a phylink instance
944 * @pl: a pointer to a &struct phylink returned from phylink_create()
946 * Start the phylink instance specified by @pl, configuring the MAC for the
947 * desired link mode(s) and negotiation style. This should be called from the
948 * network device driver's &struct net_device_ops ndo_open() method.
950 void phylink_start(struct phylink *pl)
954 phylink_info(pl, "configuring for %s/%s link mode\n",
955 phylink_an_mode_str(pl->link_an_mode),
956 phy_modes(pl->link_config.interface));
958 /* Always set the carrier off */
960 netif_carrier_off(pl->netdev);
962 /* Apply the link configuration to the MAC when starting. This allows
963 * a fixed-link to start with the correct parameters, and also
964 * ensures that we set the appropriate advertisement for Serdes links.
966 phylink_resolve_flow(pl, &pl->link_config);
967 phylink_mac_config(pl, &pl->link_config);
969 /* Restart autonegotiation if using 802.3z to ensure that the link
970 * parameters are properly negotiated. This is necessary for DSA
971 * switches using 802.3z negotiation to ensure they see our modes.
973 phylink_mac_an_restart(pl);
975 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
976 phylink_run_resolve(pl);
978 if (pl->link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
979 int irq = gpiod_to_irq(pl->link_gpio);
982 if (!request_irq(irq, phylink_link_handler,
983 IRQF_TRIGGER_RISING |
984 IRQF_TRIGGER_FALLING,
991 mod_timer(&pl->link_poll, jiffies + HZ);
993 if (pl->link_an_mode == MLO_AN_FIXED && pl->get_fixed_state)
994 mod_timer(&pl->link_poll, jiffies + HZ);
996 phy_start(pl->phydev);
998 sfp_upstream_start(pl->sfp_bus);
1000 EXPORT_SYMBOL_GPL(phylink_start);
1003 * phylink_stop() - stop a phylink instance
1004 * @pl: a pointer to a &struct phylink returned from phylink_create()
1006 * Stop the phylink instance specified by @pl. This should be called from the
1007 * network device driver's &struct net_device_ops ndo_stop() method. The
1008 * network device's carrier state should not be changed prior to calling this
1011 void phylink_stop(struct phylink *pl)
1016 sfp_upstream_stop(pl->sfp_bus);
1018 phy_stop(pl->phydev);
1019 del_timer_sync(&pl->link_poll);
1021 free_irq(pl->link_irq, pl);
1025 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1027 EXPORT_SYMBOL_GPL(phylink_stop);
1030 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1031 * @pl: a pointer to a &struct phylink returned from phylink_create()
1032 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1034 * Read the wake on lan parameters from the PHY attached to the phylink
1035 * instance specified by @pl. If no PHY is currently attached, report no
1036 * support for wake on lan.
1038 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1046 phy_ethtool_get_wol(pl->phydev, wol);
1048 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1051 * phylink_ethtool_set_wol() - set wake on lan parameters
1052 * @pl: a pointer to a &struct phylink returned from phylink_create()
1053 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1055 * Set the wake on lan parameters for the PHY attached to the phylink
1056 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1059 * Returns zero on success or negative errno code.
1061 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1063 int ret = -EOPNOTSUPP;
1068 ret = phy_ethtool_set_wol(pl->phydev, wol);
1072 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1074 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1076 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1078 linkmode_zero(mask);
1079 phylink_set_port_modes(mask);
1081 linkmode_and(dst, dst, mask);
1082 linkmode_or(dst, dst, b);
1085 static void phylink_get_ksettings(const struct phylink_link_state *state,
1086 struct ethtool_link_ksettings *kset)
1088 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1089 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1090 kset->base.speed = state->speed;
1091 kset->base.duplex = state->duplex;
1092 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1097 * phylink_ethtool_ksettings_get() - get the current link settings
1098 * @pl: a pointer to a &struct phylink returned from phylink_create()
1099 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1101 * Read the current link settings for the phylink instance specified by @pl.
1102 * This will be the link settings read from the MAC, PHY or fixed link
1103 * settings depending on the current negotiation mode.
1105 int phylink_ethtool_ksettings_get(struct phylink *pl,
1106 struct ethtool_link_ksettings *kset)
1108 struct phylink_link_state link_state;
1113 phy_ethtool_ksettings_get(pl->phydev, kset);
1115 kset->base.port = pl->link_port;
1118 linkmode_copy(kset->link_modes.supported, pl->supported);
1120 switch (pl->link_an_mode) {
1122 /* We are using fixed settings. Report these as the
1123 * current link settings - and note that these also
1124 * represent the supported speeds/duplex/pause modes.
1126 phylink_get_fixed_state(pl, &link_state);
1127 phylink_get_ksettings(&link_state, kset);
1131 /* If there is a phy attached, then use the reported
1132 * settings from the phy with no modification.
1137 phylink_get_mac_state(pl, &link_state);
1139 /* The MAC is reporting the link results from its own PCS
1140 * layer via in-band status. Report these as the current
1143 phylink_get_ksettings(&link_state, kset);
1149 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1152 * phylink_ethtool_ksettings_set() - set the link settings
1153 * @pl: a pointer to a &struct phylink returned from phylink_create()
1154 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1156 int phylink_ethtool_ksettings_set(struct phylink *pl,
1157 const struct ethtool_link_ksettings *kset)
1159 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1160 struct ethtool_link_ksettings our_kset;
1161 struct phylink_link_state config;
1166 if (kset->base.autoneg != AUTONEG_DISABLE &&
1167 kset->base.autoneg != AUTONEG_ENABLE)
1170 linkmode_copy(support, pl->supported);
1171 config = pl->link_config;
1173 /* Mask out unsupported advertisements */
1174 linkmode_and(config.advertising, kset->link_modes.advertising,
1177 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1178 if (kset->base.autoneg == AUTONEG_DISABLE) {
1179 const struct phy_setting *s;
1181 /* Autonegotiation disabled, select a suitable speed and
1184 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1189 /* If we have a fixed link (as specified by firmware), refuse
1190 * to change link parameters.
1192 if (pl->link_an_mode == MLO_AN_FIXED &&
1193 (s->speed != pl->link_config.speed ||
1194 s->duplex != pl->link_config.duplex))
1197 config.speed = s->speed;
1198 config.duplex = s->duplex;
1199 config.an_enabled = false;
1201 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1203 /* If we have a fixed link, refuse to enable autonegotiation */
1204 if (pl->link_an_mode == MLO_AN_FIXED)
1207 config.speed = SPEED_UNKNOWN;
1208 config.duplex = DUPLEX_UNKNOWN;
1209 config.an_enabled = true;
1211 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1214 if (phylink_validate(pl, support, &config))
1217 /* If autonegotiation is enabled, we must have an advertisement */
1218 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1222 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1223 our_kset.base.speed = config.speed;
1224 our_kset.base.duplex = config.duplex;
1226 /* If we have a PHY, configure the phy */
1228 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1233 mutex_lock(&pl->state_mutex);
1234 /* Configure the MAC to match the new settings */
1235 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1236 pl->link_config.interface = config.interface;
1237 pl->link_config.speed = our_kset.base.speed;
1238 pl->link_config.duplex = our_kset.base.duplex;
1239 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1241 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1242 phylink_mac_config(pl, &pl->link_config);
1243 phylink_mac_an_restart(pl);
1245 mutex_unlock(&pl->state_mutex);
1249 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1252 * phylink_ethtool_nway_reset() - restart negotiation
1253 * @pl: a pointer to a &struct phylink returned from phylink_create()
1255 * Restart negotiation for the phylink instance specified by @pl. This will
1256 * cause any attached phy to restart negotiation with the link partner, and
1257 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1260 * Returns zero on success, or negative error code.
1262 int phylink_ethtool_nway_reset(struct phylink *pl)
1269 ret = phy_restart_aneg(pl->phydev);
1270 phylink_mac_an_restart(pl);
1274 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1277 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1278 * @pl: a pointer to a &struct phylink returned from phylink_create()
1279 * @pause: a pointer to a &struct ethtool_pauseparam
1281 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1282 struct ethtool_pauseparam *pause)
1286 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1287 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1288 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1290 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1293 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1294 * @pl: a pointer to a &struct phylink returned from phylink_create()
1295 * @pause: a pointer to a &struct ethtool_pauseparam
1297 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1298 struct ethtool_pauseparam *pause)
1300 struct phylink_link_state *config = &pl->link_config;
1304 if (!phylink_test(pl->supported, Pause) &&
1305 !phylink_test(pl->supported, Asym_Pause))
1308 if (!phylink_test(pl->supported, Asym_Pause) &&
1309 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1312 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1315 config->pause |= MLO_PAUSE_AN;
1316 if (pause->rx_pause)
1317 config->pause |= MLO_PAUSE_RX;
1318 if (pause->tx_pause)
1319 config->pause |= MLO_PAUSE_TX;
1321 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1322 switch (pl->link_an_mode) {
1324 /* Silently mark the carrier down, and then trigger a resolve */
1326 netif_carrier_off(pl->netdev);
1327 phylink_run_resolve(pl);
1331 /* Should we allow fixed links to change against the config? */
1332 phylink_resolve_flow(pl, config);
1333 phylink_mac_config(pl, config);
1337 phylink_mac_config(pl, config);
1338 phylink_mac_an_restart(pl);
1345 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1348 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1350 * @pl: a pointer to a &struct phylink returned from phylink_create().
1352 * Read the Energy Efficient Ethernet error counter from the PHY associated
1353 * with the phylink instance specified by @pl.
1355 * Returns positive error counter value, or negative error code.
1357 int phylink_get_eee_err(struct phylink *pl)
1364 ret = phy_get_eee_err(pl->phydev);
1368 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1371 * phylink_init_eee() - init and check the EEE features
1372 * @pl: a pointer to a &struct phylink returned from phylink_create()
1373 * @clk_stop_enable: allow PHY to stop receive clock
1375 * Must be called either with RTNL held or within mac_link_up()
1377 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1379 int ret = -EOPNOTSUPP;
1382 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1386 EXPORT_SYMBOL_GPL(phylink_init_eee);
1389 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1390 * @pl: a pointer to a &struct phylink returned from phylink_create()
1391 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1393 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1395 int ret = -EOPNOTSUPP;
1400 ret = phy_ethtool_get_eee(pl->phydev, eee);
1404 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1407 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1408 * @pl: a pointer to a &struct phylink returned from phylink_create()
1409 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1411 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1413 int ret = -EOPNOTSUPP;
1418 ret = phy_ethtool_set_eee(pl->phydev, eee);
1422 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1424 /* This emulates MII registers for a fixed-mode phy operating as per the
1425 * passed in state. "aneg" defines if we report negotiation is possible.
1427 * FIXME: should deal with negotiation state too.
1429 static int phylink_mii_emul_read(unsigned int reg,
1430 struct phylink_link_state *state)
1432 struct fixed_phy_status fs;
1435 fs.link = state->link;
1436 fs.speed = state->speed;
1437 fs.duplex = state->duplex;
1438 fs.pause = state->pause & MLO_PAUSE_SYM;
1439 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1441 val = swphy_read_reg(reg, &fs);
1442 if (reg == MII_BMSR) {
1443 if (!state->an_complete)
1444 val &= ~BMSR_ANEGCOMPLETE;
1449 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1452 struct phy_device *phydev = pl->phydev;
1455 if (mdio_phy_id_is_c45(phy_id)) {
1456 prtad = mdio_phy_id_prtad(phy_id);
1457 devad = mdio_phy_id_devad(phy_id);
1458 devad = MII_ADDR_C45 | devad << 16 | reg;
1459 } else if (phydev->is_c45) {
1465 devad = __ffs(phydev->c45_ids.devices_in_package);
1469 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1471 devad = MDIO_MMD_AN;
1472 if (reg == MII_ADVERTISE)
1473 reg = MDIO_AN_ADVERTISE;
1481 devad = MII_ADDR_C45 | devad << 16 | reg;
1486 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1489 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1490 unsigned int reg, unsigned int val)
1492 struct phy_device *phydev = pl->phydev;
1495 if (mdio_phy_id_is_c45(phy_id)) {
1496 prtad = mdio_phy_id_prtad(phy_id);
1497 devad = mdio_phy_id_devad(phy_id);
1498 devad = MII_ADDR_C45 | devad << 16 | reg;
1499 } else if (phydev->is_c45) {
1505 devad = __ffs(phydev->c45_ids.devices_in_package);
1509 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1511 devad = MDIO_MMD_AN;
1512 if (reg == MII_ADVERTISE)
1513 reg = MDIO_AN_ADVERTISE;
1521 devad = MII_ADDR_C45 | devad << 16 | reg;
1527 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1530 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1533 struct phylink_link_state state;
1536 switch (pl->link_an_mode) {
1539 phylink_get_fixed_state(pl, &state);
1540 val = phylink_mii_emul_read(reg, &state);
1549 val = phylink_get_mac_state(pl, &state);
1553 val = phylink_mii_emul_read(reg, &state);
1558 return val & 0xffff;
1561 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1562 unsigned int reg, unsigned int val)
1564 switch (pl->link_an_mode) {
1579 * phylink_mii_ioctl() - generic mii ioctl interface
1580 * @pl: a pointer to a &struct phylink returned from phylink_create()
1581 * @ifr: a pointer to a &struct ifreq for socket ioctls
1582 * @cmd: ioctl cmd to execute
1584 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1585 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1587 * Returns: zero on success or negative error code.
1590 * read register from the current PHY.
1592 * read register from the specified PHY.
1594 * set a register on the specified PHY.
1596 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1598 struct mii_ioctl_data *mii = if_mii(ifr);
1604 /* PHYs only exist for MLO_AN_PHY and SGMII */
1607 mii->phy_id = pl->phydev->mdio.addr;
1611 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1619 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1624 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1634 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1642 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1654 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1656 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
1658 struct phylink *pl = upstream;
1660 pl->netdev->sfp_bus = bus;
1663 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
1665 struct phylink *pl = upstream;
1667 pl->netdev->sfp_bus = NULL;
1670 static int phylink_sfp_module_insert(void *upstream,
1671 const struct sfp_eeprom_id *id)
1673 struct phylink *pl = upstream;
1674 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1675 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
1676 struct phylink_link_state config;
1677 phy_interface_t iface;
1684 sfp_parse_support(pl->sfp_bus, id, support);
1685 port = sfp_parse_port(pl->sfp_bus, id, support);
1687 memset(&config, 0, sizeof(config));
1688 linkmode_copy(config.advertising, support);
1689 config.interface = PHY_INTERFACE_MODE_NA;
1690 config.speed = SPEED_UNKNOWN;
1691 config.duplex = DUPLEX_UNKNOWN;
1692 config.pause = MLO_PAUSE_AN;
1693 config.an_enabled = pl->link_config.an_enabled;
1695 /* Ignore errors if we're expecting a PHY to attach later */
1696 ret = phylink_validate(pl, support, &config);
1698 phylink_err(pl, "validation with support %*pb failed: %d\n",
1699 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1703 linkmode_copy(support1, support);
1705 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1706 if (iface == PHY_INTERFACE_MODE_NA) {
1708 "selection of interface failed, advertisement %*pb\n",
1709 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1713 config.interface = iface;
1714 ret = phylink_validate(pl, support1, &config);
1716 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
1717 phylink_an_mode_str(MLO_AN_INBAND),
1718 phy_modes(config.interface),
1719 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1723 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
1724 phylink_an_mode_str(MLO_AN_INBAND),
1725 phy_modes(config.interface),
1726 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1728 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1731 changed = !bitmap_equal(pl->supported, support,
1732 __ETHTOOL_LINK_MODE_MASK_NBITS);
1734 linkmode_copy(pl->supported, support);
1735 linkmode_copy(pl->link_config.advertising, config.advertising);
1738 if (pl->link_an_mode != MLO_AN_INBAND ||
1739 pl->link_config.interface != config.interface) {
1740 pl->link_config.interface = config.interface;
1741 pl->link_an_mode = MLO_AN_INBAND;
1745 phylink_info(pl, "switched to %s/%s link mode\n",
1746 phylink_an_mode_str(MLO_AN_INBAND),
1747 phy_modes(config.interface));
1750 pl->link_port = port;
1752 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1753 &pl->phylink_disable_state))
1754 phylink_mac_config(pl, &pl->link_config);
1759 static void phylink_sfp_link_down(void *upstream)
1761 struct phylink *pl = upstream;
1765 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
1768 static void phylink_sfp_link_up(void *upstream)
1770 struct phylink *pl = upstream;
1774 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1775 phylink_run_resolve(pl);
1778 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1780 struct phylink *pl = upstream;
1782 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
1785 static void phylink_sfp_disconnect_phy(void *upstream)
1787 phylink_disconnect_phy(upstream);
1790 static const struct sfp_upstream_ops sfp_phylink_ops = {
1791 .attach = phylink_sfp_attach,
1792 .detach = phylink_sfp_detach,
1793 .module_insert = phylink_sfp_module_insert,
1794 .link_up = phylink_sfp_link_up,
1795 .link_down = phylink_sfp_link_down,
1796 .connect_phy = phylink_sfp_connect_phy,
1797 .disconnect_phy = phylink_sfp_disconnect_phy,
1800 /* Helpers for MAC drivers */
1803 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1804 * @state: a pointer to a &struct phylink_link_state
1806 * Inspect the interface mode, advertising mask or forced speed and
1807 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1808 * the interface mode to suit. @state->interface is appropriately
1809 * updated, and the advertising mask has the "other" baseX_Full flag
1812 void phylink_helper_basex_speed(struct phylink_link_state *state)
1814 if (phy_interface_mode_is_8023z(state->interface)) {
1815 bool want_2500 = state->an_enabled ?
1816 phylink_test(state->advertising, 2500baseX_Full) :
1817 state->speed == SPEED_2500;
1820 phylink_clear(state->advertising, 1000baseX_Full);
1821 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1823 phylink_clear(state->advertising, 2500baseX_Full);
1824 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1828 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1830 MODULE_LICENSE("GPL v2");