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 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
138 return linkmode_empty(tmp);
141 static const char *phylink_an_mode_str(unsigned int mode)
143 static const char *modestr[] = {
144 [MLO_AN_PHY] = "phy",
145 [MLO_AN_FIXED] = "fixed",
146 [MLO_AN_INBAND] = "inband",
149 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
152 static int phylink_validate(struct phylink *pl, unsigned long *supported,
153 struct phylink_link_state *state)
155 pl->ops->validate(pl->config, supported, state);
157 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
160 static int phylink_parse_fixedlink(struct phylink *pl,
161 struct fwnode_handle *fwnode)
163 struct fwnode_handle *fixed_node;
164 const struct phy_setting *s;
165 struct gpio_desc *desc;
169 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
171 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
173 pl->link_config.speed = speed;
174 pl->link_config.duplex = DUPLEX_HALF;
176 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
177 pl->link_config.duplex = DUPLEX_FULL;
179 /* We treat the "pause" and "asym-pause" terminology as
180 * defining the link partner's ability. */
181 if (fwnode_property_read_bool(fixed_node, "pause"))
182 pl->link_config.pause |= MLO_PAUSE_SYM;
183 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
184 pl->link_config.pause |= MLO_PAUSE_ASYM;
187 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
191 pl->link_gpio = desc;
192 else if (desc == ERR_PTR(-EPROBE_DEFER))
195 fwnode_handle_put(fixed_node);
202 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
204 if (ret != ARRAY_SIZE(prop)) {
205 phylink_err(pl, "broken fixed-link?\n");
209 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
210 prop, ARRAY_SIZE(prop));
212 pl->link_config.duplex = prop[1] ?
213 DUPLEX_FULL : DUPLEX_HALF;
214 pl->link_config.speed = prop[2];
216 pl->link_config.pause |= MLO_PAUSE_SYM;
218 pl->link_config.pause |= MLO_PAUSE_ASYM;
222 if (pl->link_config.speed > SPEED_1000 &&
223 pl->link_config.duplex != DUPLEX_FULL)
224 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
225 pl->link_config.speed);
227 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
228 linkmode_copy(pl->link_config.advertising, pl->supported);
229 phylink_validate(pl, pl->supported, &pl->link_config);
231 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
232 pl->supported, true);
233 linkmode_zero(pl->supported);
234 phylink_set(pl->supported, MII);
235 phylink_set(pl->supported, Pause);
236 phylink_set(pl->supported, Asym_Pause);
238 __set_bit(s->bit, pl->supported);
240 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
241 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
242 pl->link_config.speed);
245 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
248 pl->link_config.link = 1;
249 pl->link_config.an_complete = 1;
254 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
256 struct fwnode_handle *dn;
259 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
260 if (dn || fwnode_property_present(fwnode, "fixed-link"))
261 pl->link_an_mode = MLO_AN_FIXED;
262 fwnode_handle_put(dn);
264 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
265 strcmp(managed, "in-band-status") == 0) {
266 if (pl->link_an_mode == MLO_AN_FIXED) {
268 "can't use both fixed-link and in-band-status\n");
272 linkmode_zero(pl->supported);
273 phylink_set(pl->supported, MII);
274 phylink_set(pl->supported, Autoneg);
275 phylink_set(pl->supported, Asym_Pause);
276 phylink_set(pl->supported, Pause);
277 pl->link_config.an_enabled = true;
278 pl->link_an_mode = MLO_AN_INBAND;
280 switch (pl->link_config.interface) {
281 case PHY_INTERFACE_MODE_SGMII:
282 phylink_set(pl->supported, 10baseT_Half);
283 phylink_set(pl->supported, 10baseT_Full);
284 phylink_set(pl->supported, 100baseT_Half);
285 phylink_set(pl->supported, 100baseT_Full);
286 phylink_set(pl->supported, 1000baseT_Half);
287 phylink_set(pl->supported, 1000baseT_Full);
290 case PHY_INTERFACE_MODE_1000BASEX:
291 phylink_set(pl->supported, 1000baseX_Full);
294 case PHY_INTERFACE_MODE_2500BASEX:
295 phylink_set(pl->supported, 2500baseX_Full);
298 case PHY_INTERFACE_MODE_10GKR:
299 phylink_set(pl->supported, 10baseT_Half);
300 phylink_set(pl->supported, 10baseT_Full);
301 phylink_set(pl->supported, 100baseT_Half);
302 phylink_set(pl->supported, 100baseT_Full);
303 phylink_set(pl->supported, 1000baseT_Half);
304 phylink_set(pl->supported, 1000baseT_Full);
305 phylink_set(pl->supported, 1000baseX_Full);
306 phylink_set(pl->supported, 10000baseKR_Full);
307 phylink_set(pl->supported, 10000baseCR_Full);
308 phylink_set(pl->supported, 10000baseSR_Full);
309 phylink_set(pl->supported, 10000baseLR_Full);
310 phylink_set(pl->supported, 10000baseLRM_Full);
311 phylink_set(pl->supported, 10000baseER_Full);
316 "incorrect link mode %s for in-band status\n",
317 phy_modes(pl->link_config.interface));
321 linkmode_copy(pl->link_config.advertising, pl->supported);
323 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
325 "failed to validate link configuration for in-band status\n");
333 static void phylink_mac_config(struct phylink *pl,
334 const struct phylink_link_state *state)
337 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
338 __func__, phylink_an_mode_str(pl->link_an_mode),
339 phy_modes(state->interface),
340 phy_speed_to_str(state->speed),
341 phy_duplex_to_str(state->duplex),
342 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
343 state->pause, state->link, state->an_enabled);
345 pl->ops->mac_config(pl->config, pl->link_an_mode, state);
348 static void phylink_mac_config_up(struct phylink *pl,
349 const struct phylink_link_state *state)
352 phylink_mac_config(pl, state);
355 static void phylink_mac_an_restart(struct phylink *pl)
357 if (pl->link_config.an_enabled &&
358 phy_interface_mode_is_8023z(pl->link_config.interface))
359 pl->ops->mac_an_restart(pl->config);
362 static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
365 linkmode_copy(state->advertising, pl->link_config.advertising);
366 linkmode_zero(state->lp_advertising);
367 state->interface = pl->link_config.interface;
368 state->an_enabled = pl->link_config.an_enabled;
369 state->speed = SPEED_UNKNOWN;
370 state->duplex = DUPLEX_UNKNOWN;
371 state->pause = MLO_PAUSE_NONE;
372 state->an_complete = 0;
375 return pl->ops->mac_link_state(pl->config, state);
378 /* The fixed state is... fixed except for the link state,
379 * which may be determined by a GPIO or a callback.
381 static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
383 *state = pl->link_config;
384 if (pl->get_fixed_state)
385 pl->get_fixed_state(pl->netdev, state);
386 else if (pl->link_gpio)
387 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
390 /* Flow control is resolved according to our and the link partners
391 * advertisements using the following drawn from the 802.3 specs:
392 * Local device Link partner
393 * Pause AsymDir Pause AsymDir Result
398 static void phylink_resolve_flow(struct phylink *pl,
399 struct phylink_link_state *state)
403 if (pl->link_config.pause & MLO_PAUSE_AN) {
406 if (phylink_test(pl->link_config.advertising, Pause))
407 pause |= MLO_PAUSE_SYM;
408 if (phylink_test(pl->link_config.advertising, Asym_Pause))
409 pause |= MLO_PAUSE_ASYM;
411 pause &= state->pause;
413 if (pause & MLO_PAUSE_SYM)
414 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
415 else if (pause & MLO_PAUSE_ASYM)
416 new_pause = state->pause & MLO_PAUSE_SYM ?
417 MLO_PAUSE_TX : MLO_PAUSE_RX;
419 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
422 state->pause &= ~MLO_PAUSE_TXRX_MASK;
423 state->pause |= new_pause;
426 static const char *phylink_pause_to_str(int pause)
428 switch (pause & MLO_PAUSE_TXRX_MASK) {
429 case MLO_PAUSE_TX | MLO_PAUSE_RX:
440 static void phylink_mac_link_up(struct phylink *pl,
441 struct phylink_link_state link_state)
443 struct net_device *ndev = pl->netdev;
445 pl->cur_interface = link_state.interface;
446 pl->ops->mac_link_up(pl->config, pl->link_an_mode,
447 pl->phy_state.interface,
451 netif_carrier_on(ndev);
454 "Link is Up - %s/%s - flow control %s\n",
455 phy_speed_to_str(link_state.speed),
456 phy_duplex_to_str(link_state.duplex),
457 phylink_pause_to_str(link_state.pause));
460 static void phylink_mac_link_down(struct phylink *pl)
462 struct net_device *ndev = pl->netdev;
465 netif_carrier_off(ndev);
466 pl->ops->mac_link_down(pl->config, pl->link_an_mode,
468 phylink_info(pl, "Link is Down\n");
471 static void phylink_resolve(struct work_struct *w)
473 struct phylink *pl = container_of(w, struct phylink, resolve);
474 struct phylink_link_state link_state;
475 struct net_device *ndev = pl->netdev;
478 mutex_lock(&pl->state_mutex);
479 if (pl->phylink_disable_state) {
480 pl->mac_link_dropped = false;
481 link_state.link = false;
482 } else if (pl->mac_link_dropped) {
483 link_state.link = false;
485 switch (pl->link_an_mode) {
487 link_state = pl->phy_state;
488 phylink_resolve_flow(pl, &link_state);
489 phylink_mac_config_up(pl, &link_state);
493 phylink_get_fixed_state(pl, &link_state);
494 phylink_mac_config_up(pl, &link_state);
498 phylink_get_mac_state(pl, &link_state);
500 /* If we have a phy, the "up" state is the union of
501 * both the PHY and the MAC */
503 link_state.link &= pl->phy_state.link;
505 /* Only update if the PHY link is up */
506 if (pl->phydev && pl->phy_state.link) {
507 link_state.interface = pl->phy_state.interface;
509 /* If we have a PHY, we need to update with
510 * the pause mode bits. */
511 link_state.pause |= pl->phy_state.pause;
512 phylink_resolve_flow(pl, &link_state);
513 phylink_mac_config(pl, &link_state);
520 link_changed = (link_state.link != netif_carrier_ok(ndev));
522 link_changed = (link_state.link != pl->old_link_state);
525 pl->old_link_state = link_state.link;
526 if (!link_state.link)
527 phylink_mac_link_down(pl);
529 phylink_mac_link_up(pl, link_state);
531 if (!link_state.link && pl->mac_link_dropped) {
532 pl->mac_link_dropped = false;
533 queue_work(system_power_efficient_wq, &pl->resolve);
535 mutex_unlock(&pl->state_mutex);
538 static void phylink_run_resolve(struct phylink *pl)
540 if (!pl->phylink_disable_state)
541 queue_work(system_power_efficient_wq, &pl->resolve);
544 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
546 unsigned long state = pl->phylink_disable_state;
548 set_bit(bit, &pl->phylink_disable_state);
550 queue_work(system_power_efficient_wq, &pl->resolve);
551 flush_work(&pl->resolve);
555 static void phylink_fixed_poll(struct timer_list *t)
557 struct phylink *pl = container_of(t, struct phylink, link_poll);
559 mod_timer(t, jiffies + HZ);
561 phylink_run_resolve(pl);
564 static const struct sfp_upstream_ops sfp_phylink_ops;
566 static int phylink_register_sfp(struct phylink *pl,
567 struct fwnode_handle *fwnode)
569 struct fwnode_reference_args ref;
575 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
581 phylink_err(pl, "unable to parse \"sfp\" node: %d\n",
586 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl, &sfp_phylink_ops);
594 * phylink_create() - create a phylink instance
595 * @config: a pointer to the target &struct phylink_config
596 * @fwnode: a pointer to a &struct fwnode_handle describing the network
598 * @iface: the desired link mode defined by &typedef phy_interface_t
599 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
601 * Create a new phylink instance, and parse the link parameters found in @np.
602 * This will parse in-band modes, fixed-link or SFP configuration.
604 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
605 * must use IS_ERR() to check for errors from this function.
607 struct phylink *phylink_create(struct phylink_config *config,
608 struct fwnode_handle *fwnode,
609 phy_interface_t iface,
610 const struct phylink_mac_ops *ops)
615 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
617 return ERR_PTR(-ENOMEM);
619 mutex_init(&pl->state_mutex);
620 INIT_WORK(&pl->resolve, phylink_resolve);
623 if (config->type == PHYLINK_NETDEV) {
624 pl->netdev = to_net_dev(config->dev);
625 } else if (config->type == PHYLINK_DEV) {
626 pl->dev = config->dev;
629 return ERR_PTR(-EINVAL);
632 pl->phy_state.interface = iface;
633 pl->link_interface = iface;
634 if (iface == PHY_INTERFACE_MODE_MOCA)
635 pl->link_port = PORT_BNC;
637 pl->link_port = PORT_MII;
638 pl->link_config.interface = iface;
639 pl->link_config.pause = MLO_PAUSE_AN;
640 pl->link_config.speed = SPEED_UNKNOWN;
641 pl->link_config.duplex = DUPLEX_UNKNOWN;
642 pl->link_config.an_enabled = true;
644 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
645 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
647 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
648 linkmode_copy(pl->link_config.advertising, pl->supported);
649 phylink_validate(pl, pl->supported, &pl->link_config);
651 ret = phylink_parse_mode(pl, fwnode);
657 if (pl->link_an_mode == MLO_AN_FIXED) {
658 ret = phylink_parse_fixedlink(pl, fwnode);
665 ret = phylink_register_sfp(pl, fwnode);
673 EXPORT_SYMBOL_GPL(phylink_create);
676 * phylink_destroy() - cleanup and destroy the phylink instance
677 * @pl: a pointer to a &struct phylink returned from phylink_create()
679 * Destroy a phylink instance. Any PHY that has been attached must have been
680 * cleaned up via phylink_disconnect_phy() prior to calling this function.
682 void phylink_destroy(struct phylink *pl)
685 sfp_unregister_upstream(pl->sfp_bus);
687 gpiod_put(pl->link_gpio);
689 cancel_work_sync(&pl->resolve);
692 EXPORT_SYMBOL_GPL(phylink_destroy);
694 static void phylink_phy_change(struct phy_device *phydev, bool up,
697 struct phylink *pl = phydev->phylink;
699 mutex_lock(&pl->state_mutex);
700 pl->phy_state.speed = phydev->speed;
701 pl->phy_state.duplex = phydev->duplex;
702 pl->phy_state.pause = MLO_PAUSE_NONE;
704 pl->phy_state.pause |= MLO_PAUSE_SYM;
705 if (phydev->asym_pause)
706 pl->phy_state.pause |= MLO_PAUSE_ASYM;
707 pl->phy_state.interface = phydev->interface;
708 pl->phy_state.link = up;
709 mutex_unlock(&pl->state_mutex);
711 phylink_run_resolve(pl);
713 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
714 phy_modes(phydev->interface),
715 phy_speed_to_str(phydev->speed),
716 phy_duplex_to_str(phydev->duplex));
719 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
721 struct phylink_link_state config;
722 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
725 memset(&config, 0, sizeof(config));
726 linkmode_copy(supported, phy->supported);
727 linkmode_copy(config.advertising, phy->advertising);
728 config.interface = pl->link_config.interface;
731 * This is the new way of dealing with flow control for PHYs,
732 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
733 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
734 * using our validate call to the MAC, we rely upon the MAC
735 * clearing the bits from both supported and advertising fields.
737 if (phylink_test(supported, Pause))
738 phylink_set(config.advertising, Pause);
739 if (phylink_test(supported, Asym_Pause))
740 phylink_set(config.advertising, Asym_Pause);
742 ret = phylink_validate(pl, supported, &config);
747 phy->phy_link_change = phylink_phy_change;
750 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
753 mutex_lock(&phy->lock);
754 mutex_lock(&pl->state_mutex);
756 linkmode_copy(pl->supported, supported);
757 linkmode_copy(pl->link_config.advertising, config.advertising);
759 /* Restrict the phy advertisement according to the MAC support. */
760 linkmode_copy(phy->advertising, config.advertising);
761 mutex_unlock(&pl->state_mutex);
762 mutex_unlock(&phy->lock);
765 "phy: setting supported %*pb advertising %*pb\n",
766 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
767 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
769 if (phy_interrupt_is_valid(phy))
770 phy_request_interrupt(phy);
775 static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
776 phy_interface_t interface)
780 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
781 (pl->link_an_mode == MLO_AN_INBAND &&
782 phy_interface_mode_is_8023z(interface))))
788 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
792 ret = phylink_bringup_phy(pl, phy);
800 * phylink_connect_phy() - connect a PHY to the phylink instance
801 * @pl: a pointer to a &struct phylink returned from phylink_create()
802 * @phy: a pointer to a &struct phy_device.
804 * Connect @phy to the phylink instance specified by @pl by calling
805 * phy_attach_direct(). Configure the @phy according to the MAC driver's
806 * capabilities, start the PHYLIB state machine and enable any interrupts
807 * that the PHY supports.
809 * This updates the phylink's ethtool supported and advertising link mode
812 * Returns 0 on success or a negative errno.
814 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
816 /* Use PHY device/driver interface */
817 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
818 pl->link_interface = phy->interface;
819 pl->link_config.interface = pl->link_interface;
822 return __phylink_connect_phy(pl, phy, pl->link_interface);
824 EXPORT_SYMBOL_GPL(phylink_connect_phy);
827 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
828 * @pl: a pointer to a &struct phylink returned from phylink_create()
829 * @dn: a pointer to a &struct device_node.
830 * @flags: PHY-specific flags to communicate to the PHY device driver
832 * Connect the phy specified in the device node @dn to the phylink instance
833 * specified by @pl. Actions specified in phylink_connect_phy() will be
836 * Returns 0 on success or a negative errno.
838 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
841 struct device_node *phy_node;
842 struct phy_device *phy_dev;
845 /* Fixed links and 802.3z are handled without needing a PHY */
846 if (pl->link_an_mode == MLO_AN_FIXED ||
847 (pl->link_an_mode == MLO_AN_INBAND &&
848 phy_interface_mode_is_8023z(pl->link_interface)))
851 phy_node = of_parse_phandle(dn, "phy-handle", 0);
853 phy_node = of_parse_phandle(dn, "phy", 0);
855 phy_node = of_parse_phandle(dn, "phy-device", 0);
858 if (pl->link_an_mode == MLO_AN_PHY)
863 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
865 /* We're done with the phy_node handle */
866 of_node_put(phy_node);
871 ret = phylink_bringup_phy(pl, phy_dev);
877 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
880 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
882 * @pl: a pointer to a &struct phylink returned from phylink_create()
884 * Disconnect any current PHY from the phylink instance described by @pl.
886 void phylink_disconnect_phy(struct phylink *pl)
888 struct phy_device *phy;
894 mutex_lock(&phy->lock);
895 mutex_lock(&pl->state_mutex);
897 mutex_unlock(&pl->state_mutex);
898 mutex_unlock(&phy->lock);
899 flush_work(&pl->resolve);
904 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
907 * phylink_fixed_state_cb() - allow setting a fixed link callback
908 * @pl: a pointer to a &struct phylink returned from phylink_create()
909 * @cb: callback to execute to determine the fixed link state.
911 * The MAC driver should call this driver when the state of its link
912 * can be determined through e.g: an out of band MMIO register.
914 int phylink_fixed_state_cb(struct phylink *pl,
915 void (*cb)(struct net_device *dev,
916 struct phylink_link_state *state))
918 /* It does not make sense to let the link be overriden unless we use
921 if (pl->link_an_mode != MLO_AN_FIXED)
924 mutex_lock(&pl->state_mutex);
925 pl->get_fixed_state = cb;
926 mutex_unlock(&pl->state_mutex);
930 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
933 * phylink_mac_change() - notify phylink of a change in MAC state
934 * @pl: a pointer to a &struct phylink returned from phylink_create()
935 * @up: indicates whether the link is currently up.
937 * The MAC driver should call this driver when the state of its link
938 * changes (eg, link failure, new negotiation results, etc.)
940 void phylink_mac_change(struct phylink *pl, bool up)
943 pl->mac_link_dropped = true;
944 phylink_run_resolve(pl);
945 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
947 EXPORT_SYMBOL_GPL(phylink_mac_change);
949 static irqreturn_t phylink_link_handler(int irq, void *data)
951 struct phylink *pl = data;
953 phylink_run_resolve(pl);
959 * phylink_start() - start a phylink instance
960 * @pl: a pointer to a &struct phylink returned from phylink_create()
962 * Start the phylink instance specified by @pl, configuring the MAC for the
963 * desired link mode(s) and negotiation style. This should be called from the
964 * network device driver's &struct net_device_ops ndo_open() method.
966 void phylink_start(struct phylink *pl)
970 phylink_info(pl, "configuring for %s/%s link mode\n",
971 phylink_an_mode_str(pl->link_an_mode),
972 phy_modes(pl->link_config.interface));
974 /* Always set the carrier off */
976 netif_carrier_off(pl->netdev);
978 /* Apply the link configuration to the MAC when starting. This allows
979 * a fixed-link to start with the correct parameters, and also
980 * ensures that we set the appropriate advertisement for Serdes links.
982 phylink_resolve_flow(pl, &pl->link_config);
983 phylink_mac_config(pl, &pl->link_config);
985 /* Restart autonegotiation if using 802.3z to ensure that the link
986 * parameters are properly negotiated. This is necessary for DSA
987 * switches using 802.3z negotiation to ensure they see our modes.
989 phylink_mac_an_restart(pl);
991 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
992 phylink_run_resolve(pl);
994 if (pl->link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
995 int irq = gpiod_to_irq(pl->link_gpio);
998 if (!request_irq(irq, phylink_link_handler,
999 IRQF_TRIGGER_RISING |
1000 IRQF_TRIGGER_FALLING,
1007 mod_timer(&pl->link_poll, jiffies + HZ);
1009 if (pl->link_an_mode == MLO_AN_FIXED && pl->get_fixed_state)
1010 mod_timer(&pl->link_poll, jiffies + HZ);
1012 phy_start(pl->phydev);
1014 sfp_upstream_start(pl->sfp_bus);
1016 EXPORT_SYMBOL_GPL(phylink_start);
1019 * phylink_stop() - stop a phylink instance
1020 * @pl: a pointer to a &struct phylink returned from phylink_create()
1022 * Stop the phylink instance specified by @pl. This should be called from the
1023 * network device driver's &struct net_device_ops ndo_stop() method. The
1024 * network device's carrier state should not be changed prior to calling this
1027 void phylink_stop(struct phylink *pl)
1032 sfp_upstream_stop(pl->sfp_bus);
1034 phy_stop(pl->phydev);
1035 del_timer_sync(&pl->link_poll);
1037 free_irq(pl->link_irq, pl);
1041 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1043 EXPORT_SYMBOL_GPL(phylink_stop);
1046 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1047 * @pl: a pointer to a &struct phylink returned from phylink_create()
1048 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1050 * Read the wake on lan parameters from the PHY attached to the phylink
1051 * instance specified by @pl. If no PHY is currently attached, report no
1052 * support for wake on lan.
1054 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1062 phy_ethtool_get_wol(pl->phydev, wol);
1064 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1067 * phylink_ethtool_set_wol() - set wake on lan parameters
1068 * @pl: a pointer to a &struct phylink returned from phylink_create()
1069 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1071 * Set the wake on lan parameters for the PHY attached to the phylink
1072 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1075 * Returns zero on success or negative errno code.
1077 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1079 int ret = -EOPNOTSUPP;
1084 ret = phy_ethtool_set_wol(pl->phydev, wol);
1088 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1090 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1092 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1094 linkmode_zero(mask);
1095 phylink_set_port_modes(mask);
1097 linkmode_and(dst, dst, mask);
1098 linkmode_or(dst, dst, b);
1101 static void phylink_get_ksettings(const struct phylink_link_state *state,
1102 struct ethtool_link_ksettings *kset)
1104 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1105 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1106 kset->base.speed = state->speed;
1107 kset->base.duplex = state->duplex;
1108 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1113 * phylink_ethtool_ksettings_get() - get the current link settings
1114 * @pl: a pointer to a &struct phylink returned from phylink_create()
1115 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1117 * Read the current link settings for the phylink instance specified by @pl.
1118 * This will be the link settings read from the MAC, PHY or fixed link
1119 * settings depending on the current negotiation mode.
1121 int phylink_ethtool_ksettings_get(struct phylink *pl,
1122 struct ethtool_link_ksettings *kset)
1124 struct phylink_link_state link_state;
1129 phy_ethtool_ksettings_get(pl->phydev, kset);
1131 kset->base.port = pl->link_port;
1134 linkmode_copy(kset->link_modes.supported, pl->supported);
1136 switch (pl->link_an_mode) {
1138 /* We are using fixed settings. Report these as the
1139 * current link settings - and note that these also
1140 * represent the supported speeds/duplex/pause modes.
1142 phylink_get_fixed_state(pl, &link_state);
1143 phylink_get_ksettings(&link_state, kset);
1147 /* If there is a phy attached, then use the reported
1148 * settings from the phy with no modification.
1153 phylink_get_mac_state(pl, &link_state);
1155 /* The MAC is reporting the link results from its own PCS
1156 * layer via in-band status. Report these as the current
1159 phylink_get_ksettings(&link_state, kset);
1165 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1168 * phylink_ethtool_ksettings_set() - set the link settings
1169 * @pl: a pointer to a &struct phylink returned from phylink_create()
1170 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1172 int phylink_ethtool_ksettings_set(struct phylink *pl,
1173 const struct ethtool_link_ksettings *kset)
1175 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1176 struct ethtool_link_ksettings our_kset;
1177 struct phylink_link_state config;
1182 if (kset->base.autoneg != AUTONEG_DISABLE &&
1183 kset->base.autoneg != AUTONEG_ENABLE)
1186 linkmode_copy(support, pl->supported);
1187 config = pl->link_config;
1189 /* Mask out unsupported advertisements */
1190 linkmode_and(config.advertising, kset->link_modes.advertising,
1193 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1194 if (kset->base.autoneg == AUTONEG_DISABLE) {
1195 const struct phy_setting *s;
1197 /* Autonegotiation disabled, select a suitable speed and
1200 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1205 /* If we have a fixed link (as specified by firmware), refuse
1206 * to change link parameters.
1208 if (pl->link_an_mode == MLO_AN_FIXED &&
1209 (s->speed != pl->link_config.speed ||
1210 s->duplex != pl->link_config.duplex))
1213 config.speed = s->speed;
1214 config.duplex = s->duplex;
1215 config.an_enabled = false;
1217 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1219 /* If we have a fixed link, refuse to enable autonegotiation */
1220 if (pl->link_an_mode == MLO_AN_FIXED)
1223 config.speed = SPEED_UNKNOWN;
1224 config.duplex = DUPLEX_UNKNOWN;
1225 config.an_enabled = true;
1227 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1230 if (phylink_validate(pl, support, &config))
1233 /* If autonegotiation is enabled, we must have an advertisement */
1234 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1238 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1239 our_kset.base.speed = config.speed;
1240 our_kset.base.duplex = config.duplex;
1242 /* If we have a PHY, configure the phy */
1244 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1249 mutex_lock(&pl->state_mutex);
1250 /* Configure the MAC to match the new settings */
1251 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1252 pl->link_config.interface = config.interface;
1253 pl->link_config.speed = our_kset.base.speed;
1254 pl->link_config.duplex = our_kset.base.duplex;
1255 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1257 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1258 phylink_mac_config(pl, &pl->link_config);
1259 phylink_mac_an_restart(pl);
1261 mutex_unlock(&pl->state_mutex);
1265 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1268 * phylink_ethtool_nway_reset() - restart negotiation
1269 * @pl: a pointer to a &struct phylink returned from phylink_create()
1271 * Restart negotiation for the phylink instance specified by @pl. This will
1272 * cause any attached phy to restart negotiation with the link partner, and
1273 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1276 * Returns zero on success, or negative error code.
1278 int phylink_ethtool_nway_reset(struct phylink *pl)
1285 ret = phy_restart_aneg(pl->phydev);
1286 phylink_mac_an_restart(pl);
1290 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1293 * phylink_ethtool_get_pauseparam() - get 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 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1298 struct ethtool_pauseparam *pause)
1302 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1303 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1304 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1306 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1309 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1310 * @pl: a pointer to a &struct phylink returned from phylink_create()
1311 * @pause: a pointer to a &struct ethtool_pauseparam
1313 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1314 struct ethtool_pauseparam *pause)
1316 struct phylink_link_state *config = &pl->link_config;
1320 if (!phylink_test(pl->supported, Pause) &&
1321 !phylink_test(pl->supported, Asym_Pause))
1324 if (!phylink_test(pl->supported, Asym_Pause) &&
1325 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1328 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1331 config->pause |= MLO_PAUSE_AN;
1332 if (pause->rx_pause)
1333 config->pause |= MLO_PAUSE_RX;
1334 if (pause->tx_pause)
1335 config->pause |= MLO_PAUSE_TX;
1337 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1338 switch (pl->link_an_mode) {
1340 /* Silently mark the carrier down, and then trigger a resolve */
1342 netif_carrier_off(pl->netdev);
1343 phylink_run_resolve(pl);
1347 /* Should we allow fixed links to change against the config? */
1348 phylink_resolve_flow(pl, config);
1349 phylink_mac_config(pl, config);
1353 phylink_mac_config(pl, config);
1354 phylink_mac_an_restart(pl);
1361 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1364 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1366 * @pl: a pointer to a &struct phylink returned from phylink_create().
1368 * Read the Energy Efficient Ethernet error counter from the PHY associated
1369 * with the phylink instance specified by @pl.
1371 * Returns positive error counter value, or negative error code.
1373 int phylink_get_eee_err(struct phylink *pl)
1380 ret = phy_get_eee_err(pl->phydev);
1384 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1387 * phylink_init_eee() - init and check the EEE features
1388 * @pl: a pointer to a &struct phylink returned from phylink_create()
1389 * @clk_stop_enable: allow PHY to stop receive clock
1391 * Must be called either with RTNL held or within mac_link_up()
1393 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1395 int ret = -EOPNOTSUPP;
1398 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1402 EXPORT_SYMBOL_GPL(phylink_init_eee);
1405 * phylink_ethtool_get_eee() - read 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 read parameters
1409 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1411 int ret = -EOPNOTSUPP;
1416 ret = phy_ethtool_get_eee(pl->phydev, eee);
1420 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1423 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1424 * @pl: a pointer to a &struct phylink returned from phylink_create()
1425 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1427 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1429 int ret = -EOPNOTSUPP;
1434 ret = phy_ethtool_set_eee(pl->phydev, eee);
1438 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1440 /* This emulates MII registers for a fixed-mode phy operating as per the
1441 * passed in state. "aneg" defines if we report negotiation is possible.
1443 * FIXME: should deal with negotiation state too.
1445 static int phylink_mii_emul_read(unsigned int reg,
1446 struct phylink_link_state *state)
1448 struct fixed_phy_status fs;
1451 fs.link = state->link;
1452 fs.speed = state->speed;
1453 fs.duplex = state->duplex;
1454 fs.pause = state->pause & MLO_PAUSE_SYM;
1455 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1457 val = swphy_read_reg(reg, &fs);
1458 if (reg == MII_BMSR) {
1459 if (!state->an_complete)
1460 val &= ~BMSR_ANEGCOMPLETE;
1465 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1468 struct phy_device *phydev = pl->phydev;
1471 if (mdio_phy_id_is_c45(phy_id)) {
1472 prtad = mdio_phy_id_prtad(phy_id);
1473 devad = mdio_phy_id_devad(phy_id);
1474 devad = MII_ADDR_C45 | devad << 16 | reg;
1475 } else if (phydev->is_c45) {
1481 devad = __ffs(phydev->c45_ids.devices_in_package);
1485 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1487 devad = MDIO_MMD_AN;
1488 if (reg == MII_ADVERTISE)
1489 reg = MDIO_AN_ADVERTISE;
1497 devad = MII_ADDR_C45 | devad << 16 | reg;
1502 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1505 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1506 unsigned int reg, unsigned int val)
1508 struct phy_device *phydev = pl->phydev;
1511 if (mdio_phy_id_is_c45(phy_id)) {
1512 prtad = mdio_phy_id_prtad(phy_id);
1513 devad = mdio_phy_id_devad(phy_id);
1514 devad = MII_ADDR_C45 | devad << 16 | reg;
1515 } else if (phydev->is_c45) {
1521 devad = __ffs(phydev->c45_ids.devices_in_package);
1525 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1527 devad = MDIO_MMD_AN;
1528 if (reg == MII_ADVERTISE)
1529 reg = MDIO_AN_ADVERTISE;
1537 devad = MII_ADDR_C45 | devad << 16 | reg;
1543 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1546 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1549 struct phylink_link_state state;
1552 switch (pl->link_an_mode) {
1555 phylink_get_fixed_state(pl, &state);
1556 val = phylink_mii_emul_read(reg, &state);
1565 val = phylink_get_mac_state(pl, &state);
1569 val = phylink_mii_emul_read(reg, &state);
1574 return val & 0xffff;
1577 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1578 unsigned int reg, unsigned int val)
1580 switch (pl->link_an_mode) {
1595 * phylink_mii_ioctl() - generic mii ioctl interface
1596 * @pl: a pointer to a &struct phylink returned from phylink_create()
1597 * @ifr: a pointer to a &struct ifreq for socket ioctls
1598 * @cmd: ioctl cmd to execute
1600 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1601 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1603 * Returns: zero on success or negative error code.
1606 * read register from the current PHY.
1608 * read register from the specified PHY.
1610 * set a register on the specified PHY.
1612 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1614 struct mii_ioctl_data *mii = if_mii(ifr);
1620 /* PHYs only exist for MLO_AN_PHY and SGMII */
1623 mii->phy_id = pl->phydev->mdio.addr;
1627 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1635 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1640 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1650 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1658 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1670 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1672 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
1674 struct phylink *pl = upstream;
1676 pl->netdev->sfp_bus = bus;
1679 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
1681 struct phylink *pl = upstream;
1683 pl->netdev->sfp_bus = NULL;
1686 static int phylink_sfp_module_insert(void *upstream,
1687 const struct sfp_eeprom_id *id)
1689 struct phylink *pl = upstream;
1690 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1691 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
1692 struct phylink_link_state config;
1693 phy_interface_t iface;
1700 sfp_parse_support(pl->sfp_bus, id, support);
1701 port = sfp_parse_port(pl->sfp_bus, id, support);
1703 memset(&config, 0, sizeof(config));
1704 linkmode_copy(config.advertising, support);
1705 config.interface = PHY_INTERFACE_MODE_NA;
1706 config.speed = SPEED_UNKNOWN;
1707 config.duplex = DUPLEX_UNKNOWN;
1708 config.pause = MLO_PAUSE_AN;
1709 config.an_enabled = pl->link_config.an_enabled;
1711 /* Ignore errors if we're expecting a PHY to attach later */
1712 ret = phylink_validate(pl, support, &config);
1714 phylink_err(pl, "validation with support %*pb failed: %d\n",
1715 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1719 linkmode_copy(support1, support);
1721 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1722 if (iface == PHY_INTERFACE_MODE_NA) {
1724 "selection of interface failed, advertisement %*pb\n",
1725 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1729 config.interface = iface;
1730 ret = phylink_validate(pl, support1, &config);
1732 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
1733 phylink_an_mode_str(MLO_AN_INBAND),
1734 phy_modes(config.interface),
1735 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1739 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
1740 phylink_an_mode_str(MLO_AN_INBAND),
1741 phy_modes(config.interface),
1742 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1744 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1747 changed = !bitmap_equal(pl->supported, support,
1748 __ETHTOOL_LINK_MODE_MASK_NBITS);
1750 linkmode_copy(pl->supported, support);
1751 linkmode_copy(pl->link_config.advertising, config.advertising);
1754 if (pl->link_an_mode != MLO_AN_INBAND ||
1755 pl->link_config.interface != config.interface) {
1756 pl->link_config.interface = config.interface;
1757 pl->link_an_mode = MLO_AN_INBAND;
1761 phylink_info(pl, "switched to %s/%s link mode\n",
1762 phylink_an_mode_str(MLO_AN_INBAND),
1763 phy_modes(config.interface));
1766 pl->link_port = port;
1768 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1769 &pl->phylink_disable_state))
1770 phylink_mac_config(pl, &pl->link_config);
1775 static void phylink_sfp_link_down(void *upstream)
1777 struct phylink *pl = upstream;
1781 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
1784 static void phylink_sfp_link_up(void *upstream)
1786 struct phylink *pl = upstream;
1790 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1791 phylink_run_resolve(pl);
1794 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1796 struct phylink *pl = upstream;
1798 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
1801 static void phylink_sfp_disconnect_phy(void *upstream)
1803 phylink_disconnect_phy(upstream);
1806 static const struct sfp_upstream_ops sfp_phylink_ops = {
1807 .attach = phylink_sfp_attach,
1808 .detach = phylink_sfp_detach,
1809 .module_insert = phylink_sfp_module_insert,
1810 .link_up = phylink_sfp_link_up,
1811 .link_down = phylink_sfp_link_down,
1812 .connect_phy = phylink_sfp_connect_phy,
1813 .disconnect_phy = phylink_sfp_disconnect_phy,
1816 /* Helpers for MAC drivers */
1819 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1820 * @state: a pointer to a &struct phylink_link_state
1822 * Inspect the interface mode, advertising mask or forced speed and
1823 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1824 * the interface mode to suit. @state->interface is appropriately
1825 * updated, and the advertising mask has the "other" baseX_Full flag
1828 void phylink_helper_basex_speed(struct phylink_link_state *state)
1830 if (phy_interface_mode_is_8023z(state->interface)) {
1831 bool want_2500 = state->an_enabled ?
1832 phylink_test(state->advertising, 2500baseX_Full) :
1833 state->speed == SPEED_2500;
1836 phylink_clear(state->advertising, 1000baseX_Full);
1837 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1839 phylink_clear(state->advertising, 2500baseX_Full);
1840 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1844 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1846 MODULE_LICENSE("GPL v2");