2 * phylink models the MAC to optional PHY connection, supporting
3 * technologies such as SFP cages where the PHY is hot-pluggable.
5 * Copyright (C) 2015 Russell King
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/ethtool.h>
12 #include <linux/export.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/netdevice.h>
16 #include <linux/of_mdio.h>
17 #include <linux/phy.h>
18 #include <linux/phy_fixed.h>
19 #include <linux/phylink.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/spinlock.h>
22 #include <linux/timer.h>
23 #include <linux/workqueue.h>
28 #define SUPPORTED_INTERFACES \
29 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
30 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
31 #define ADVERTISED_INTERFACES \
32 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
33 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
36 PHYLINK_DISABLE_STOPPED,
41 * struct phylink - internal data type for phylink
45 struct net_device *netdev;
46 const struct phylink_mac_ops *ops;
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 struct timer_list link_poll;
63 void (*get_fixed_state)(struct net_device *dev,
64 struct phylink_link_state *s);
66 struct mutex state_mutex;
67 struct phylink_link_state phy_state;
68 struct work_struct resolve;
70 bool mac_link_dropped;
72 struct sfp_bus *sfp_bus;
75 static inline void linkmode_zero(unsigned long *dst)
77 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
80 static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
82 bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
85 static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
86 const unsigned long *b)
88 bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
91 static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
92 const unsigned long *b)
94 bitmap_or(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
97 static inline bool linkmode_empty(const unsigned long *src)
99 return bitmap_empty(src, __ETHTOOL_LINK_MODE_MASK_NBITS);
103 * phylink_set_port_modes() - set the port type modes in the ethtool mask
104 * @mask: ethtool link mode mask
106 * Sets all the port type modes in the ethtool mask. MAC drivers should
107 * use this in their 'validate' callback.
109 void phylink_set_port_modes(unsigned long *mask)
111 phylink_set(mask, TP);
112 phylink_set(mask, AUI);
113 phylink_set(mask, MII);
114 phylink_set(mask, FIBRE);
115 phylink_set(mask, BNC);
116 phylink_set(mask, Backplane);
118 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
120 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
122 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
124 phylink_set_port_modes(tmp);
125 phylink_set(tmp, Autoneg);
126 phylink_set(tmp, Pause);
127 phylink_set(tmp, Asym_Pause);
129 bitmap_andnot(tmp, linkmode, tmp, __ETHTOOL_LINK_MODE_MASK_NBITS);
131 return linkmode_empty(tmp);
134 static const char *phylink_an_mode_str(unsigned int mode)
136 static const char *modestr[] = {
137 [MLO_AN_PHY] = "phy",
138 [MLO_AN_FIXED] = "fixed",
139 [MLO_AN_INBAND] = "inband",
142 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
145 static int phylink_validate(struct phylink *pl, unsigned long *supported,
146 struct phylink_link_state *state)
148 pl->ops->validate(pl->netdev, supported, state);
150 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
153 static int phylink_parse_fixedlink(struct phylink *pl,
154 struct fwnode_handle *fwnode)
156 struct fwnode_handle *fixed_node;
157 const struct phy_setting *s;
158 struct gpio_desc *desc;
162 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
164 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
166 pl->link_config.speed = speed;
167 pl->link_config.duplex = DUPLEX_HALF;
169 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
170 pl->link_config.duplex = DUPLEX_FULL;
172 /* We treat the "pause" and "asym-pause" terminology as
173 * defining the link partner's ability. */
174 if (fwnode_property_read_bool(fixed_node, "pause"))
175 pl->link_config.pause |= MLO_PAUSE_SYM;
176 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
177 pl->link_config.pause |= MLO_PAUSE_ASYM;
180 desc = fwnode_get_named_gpiod(fixed_node, "link-gpios",
184 pl->link_gpio = desc;
185 else if (desc == ERR_PTR(-EPROBE_DEFER))
188 fwnode_handle_put(fixed_node);
195 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
197 if (ret != ARRAY_SIZE(prop)) {
198 netdev_err(pl->netdev, "broken fixed-link?\n");
202 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
203 prop, ARRAY_SIZE(prop));
205 pl->link_config.duplex = prop[1] ?
206 DUPLEX_FULL : DUPLEX_HALF;
207 pl->link_config.speed = prop[2];
209 pl->link_config.pause |= MLO_PAUSE_SYM;
211 pl->link_config.pause |= MLO_PAUSE_ASYM;
215 if (pl->link_config.speed > SPEED_1000 &&
216 pl->link_config.duplex != DUPLEX_FULL)
217 netdev_warn(pl->netdev, "fixed link specifies half duplex for %dMbps link?\n",
218 pl->link_config.speed);
220 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
221 linkmode_copy(pl->link_config.advertising, pl->supported);
222 phylink_validate(pl, pl->supported, &pl->link_config);
224 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
226 __ETHTOOL_LINK_MODE_MASK_NBITS, true);
227 linkmode_zero(pl->supported);
228 phylink_set(pl->supported, MII);
229 phylink_set(pl->supported, Pause);
230 phylink_set(pl->supported, Asym_Pause);
232 __set_bit(s->bit, pl->supported);
234 netdev_warn(pl->netdev, "fixed link %s duplex %dMbps not recognised\n",
235 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
236 pl->link_config.speed);
239 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
242 pl->link_config.link = 1;
243 pl->link_config.an_complete = 1;
248 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
250 struct fwnode_handle *dn;
253 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
254 if (dn || fwnode_property_present(fwnode, "fixed-link"))
255 pl->link_an_mode = MLO_AN_FIXED;
256 fwnode_handle_put(dn);
258 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
259 strcmp(managed, "in-band-status") == 0) {
260 if (pl->link_an_mode == MLO_AN_FIXED) {
261 netdev_err(pl->netdev,
262 "can't use both fixed-link and in-band-status\n");
266 linkmode_zero(pl->supported);
267 phylink_set(pl->supported, MII);
268 phylink_set(pl->supported, Autoneg);
269 phylink_set(pl->supported, Asym_Pause);
270 phylink_set(pl->supported, Pause);
271 pl->link_config.an_enabled = true;
272 pl->link_an_mode = MLO_AN_INBAND;
274 switch (pl->link_config.interface) {
275 case PHY_INTERFACE_MODE_SGMII:
276 phylink_set(pl->supported, 10baseT_Half);
277 phylink_set(pl->supported, 10baseT_Full);
278 phylink_set(pl->supported, 100baseT_Half);
279 phylink_set(pl->supported, 100baseT_Full);
280 phylink_set(pl->supported, 1000baseT_Half);
281 phylink_set(pl->supported, 1000baseT_Full);
284 case PHY_INTERFACE_MODE_1000BASEX:
285 phylink_set(pl->supported, 1000baseX_Full);
288 case PHY_INTERFACE_MODE_2500BASEX:
289 phylink_set(pl->supported, 2500baseX_Full);
292 case PHY_INTERFACE_MODE_10GKR:
293 phylink_set(pl->supported, 10baseT_Half);
294 phylink_set(pl->supported, 10baseT_Full);
295 phylink_set(pl->supported, 100baseT_Half);
296 phylink_set(pl->supported, 100baseT_Full);
297 phylink_set(pl->supported, 1000baseT_Half);
298 phylink_set(pl->supported, 1000baseT_Full);
299 phylink_set(pl->supported, 1000baseX_Full);
300 phylink_set(pl->supported, 10000baseKR_Full);
301 phylink_set(pl->supported, 10000baseCR_Full);
302 phylink_set(pl->supported, 10000baseSR_Full);
303 phylink_set(pl->supported, 10000baseLR_Full);
304 phylink_set(pl->supported, 10000baseLRM_Full);
305 phylink_set(pl->supported, 10000baseER_Full);
309 netdev_err(pl->netdev,
310 "incorrect link mode %s for in-band status\n",
311 phy_modes(pl->link_config.interface));
315 linkmode_copy(pl->link_config.advertising, pl->supported);
317 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
318 netdev_err(pl->netdev,
319 "failed to validate link configuration for in-band status\n");
327 static void phylink_mac_config(struct phylink *pl,
328 const struct phylink_link_state *state)
330 netdev_dbg(pl->netdev,
331 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
332 __func__, phylink_an_mode_str(pl->link_an_mode),
333 phy_modes(state->interface),
334 phy_speed_to_str(state->speed),
335 phy_duplex_to_str(state->duplex),
336 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
337 state->pause, state->link, state->an_enabled);
339 pl->ops->mac_config(pl->netdev, pl->link_an_mode, state);
342 static void phylink_mac_an_restart(struct phylink *pl)
344 if (pl->link_config.an_enabled &&
345 phy_interface_mode_is_8023z(pl->link_config.interface))
346 pl->ops->mac_an_restart(pl->netdev);
349 static int phylink_get_mac_state(struct phylink *pl, struct phylink_link_state *state)
351 struct net_device *ndev = pl->netdev;
353 linkmode_copy(state->advertising, pl->link_config.advertising);
354 linkmode_zero(state->lp_advertising);
355 state->interface = pl->link_config.interface;
356 state->an_enabled = pl->link_config.an_enabled;
357 state->speed = SPEED_UNKNOWN;
358 state->duplex = DUPLEX_UNKNOWN;
359 state->pause = MLO_PAUSE_NONE;
360 state->an_complete = 0;
363 return pl->ops->mac_link_state(ndev, state);
366 /* The fixed state is... fixed except for the link state,
367 * which may be determined by a GPIO or a callback.
369 static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_state *state)
371 *state = pl->link_config;
372 if (pl->get_fixed_state)
373 pl->get_fixed_state(pl->netdev, state);
374 else if (pl->link_gpio)
375 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
378 /* Flow control is resolved according to our and the link partners
379 * advertisements using the following drawn from the 802.3 specs:
380 * Local device Link partner
381 * Pause AsymDir Pause AsymDir Result
386 static void phylink_resolve_flow(struct phylink *pl,
387 struct phylink_link_state *state)
391 if (pl->link_config.pause & MLO_PAUSE_AN) {
394 if (phylink_test(pl->link_config.advertising, Pause))
395 pause |= MLO_PAUSE_SYM;
396 if (phylink_test(pl->link_config.advertising, Asym_Pause))
397 pause |= MLO_PAUSE_ASYM;
399 pause &= state->pause;
401 if (pause & MLO_PAUSE_SYM)
402 new_pause = MLO_PAUSE_TX | MLO_PAUSE_RX;
403 else if (pause & MLO_PAUSE_ASYM)
404 new_pause = state->pause & MLO_PAUSE_SYM ?
405 MLO_PAUSE_TX : MLO_PAUSE_RX;
407 new_pause = pl->link_config.pause & MLO_PAUSE_TXRX_MASK;
410 state->pause &= ~MLO_PAUSE_TXRX_MASK;
411 state->pause |= new_pause;
414 static const char *phylink_pause_to_str(int pause)
416 switch (pause & MLO_PAUSE_TXRX_MASK) {
417 case MLO_PAUSE_TX | MLO_PAUSE_RX:
428 static void phylink_resolve(struct work_struct *w)
430 struct phylink *pl = container_of(w, struct phylink, resolve);
431 struct phylink_link_state link_state;
432 struct net_device *ndev = pl->netdev;
434 mutex_lock(&pl->state_mutex);
435 if (pl->phylink_disable_state) {
436 pl->mac_link_dropped = false;
437 link_state.link = false;
438 } else if (pl->mac_link_dropped) {
439 link_state.link = false;
441 switch (pl->link_an_mode) {
443 link_state = pl->phy_state;
444 phylink_resolve_flow(pl, &link_state);
445 phylink_mac_config(pl, &link_state);
449 phylink_get_fixed_state(pl, &link_state);
450 phylink_mac_config(pl, &link_state);
454 phylink_get_mac_state(pl, &link_state);
456 bool changed = false;
458 link_state.link = link_state.link &&
461 if (pl->phy_state.interface !=
462 link_state.interface) {
463 link_state.interface = pl->phy_state.interface;
467 /* Propagate the flow control from the PHY
468 * to the MAC. Also propagate the interface
471 if (pl->phy_state.link || changed) {
472 link_state.pause |= pl->phy_state.pause;
473 phylink_resolve_flow(pl, &link_state);
475 phylink_mac_config(pl, &link_state);
482 if (link_state.link != netif_carrier_ok(ndev)) {
483 if (!link_state.link) {
484 netif_carrier_off(ndev);
485 pl->ops->mac_link_down(ndev, pl->link_an_mode,
487 netdev_info(ndev, "Link is Down\n");
489 pl->cur_interface = link_state.interface;
490 pl->ops->mac_link_up(ndev, pl->link_an_mode,
491 pl->cur_interface, pl->phydev);
493 netif_carrier_on(ndev);
496 "Link is Up - %s/%s - flow control %s\n",
497 phy_speed_to_str(link_state.speed),
498 phy_duplex_to_str(link_state.duplex),
499 phylink_pause_to_str(link_state.pause));
502 if (!link_state.link && pl->mac_link_dropped) {
503 pl->mac_link_dropped = false;
504 queue_work(system_power_efficient_wq, &pl->resolve);
506 mutex_unlock(&pl->state_mutex);
509 static void phylink_run_resolve(struct phylink *pl)
511 if (!pl->phylink_disable_state)
512 queue_work(system_power_efficient_wq, &pl->resolve);
515 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
517 unsigned long state = pl->phylink_disable_state;
519 set_bit(bit, &pl->phylink_disable_state);
521 queue_work(system_power_efficient_wq, &pl->resolve);
522 flush_work(&pl->resolve);
526 static void phylink_fixed_poll(struct timer_list *t)
528 struct phylink *pl = container_of(t, struct phylink, link_poll);
530 mod_timer(t, jiffies + HZ);
532 phylink_run_resolve(pl);
535 static const struct sfp_upstream_ops sfp_phylink_ops;
537 static int phylink_register_sfp(struct phylink *pl,
538 struct fwnode_handle *fwnode)
540 struct fwnode_reference_args ref;
546 ret = fwnode_property_get_reference_args(fwnode, "sfp", NULL,
552 netdev_err(pl->netdev, "unable to parse \"sfp\" node: %d\n",
557 if (!fwnode_device_is_available(ref.fwnode)) {
558 fwnode_handle_put(ref.fwnode);
562 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
571 * phylink_create() - create a phylink instance
572 * @ndev: a pointer to the &struct net_device
573 * @fwnode: a pointer to a &struct fwnode_handle describing the network
575 * @iface: the desired link mode defined by &typedef phy_interface_t
576 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
578 * Create a new phylink instance, and parse the link parameters found in @np.
579 * This will parse in-band modes, fixed-link or SFP configuration.
581 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
582 * must use IS_ERR() to check for errors from this function.
584 struct phylink *phylink_create(struct net_device *ndev,
585 struct fwnode_handle *fwnode,
586 phy_interface_t iface,
587 const struct phylink_mac_ops *ops)
592 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
594 return ERR_PTR(-ENOMEM);
596 mutex_init(&pl->state_mutex);
597 INIT_WORK(&pl->resolve, phylink_resolve);
599 pl->phy_state.interface = iface;
600 pl->link_interface = iface;
601 if (iface == PHY_INTERFACE_MODE_MOCA)
602 pl->link_port = PORT_BNC;
604 pl->link_port = PORT_MII;
605 pl->link_config.interface = iface;
606 pl->link_config.pause = MLO_PAUSE_AN;
607 pl->link_config.speed = SPEED_UNKNOWN;
608 pl->link_config.duplex = DUPLEX_UNKNOWN;
609 pl->link_config.an_enabled = true;
611 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
612 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
614 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
615 linkmode_copy(pl->link_config.advertising, pl->supported);
616 phylink_validate(pl, pl->supported, &pl->link_config);
618 ret = phylink_parse_mode(pl, fwnode);
624 if (pl->link_an_mode == MLO_AN_FIXED) {
625 ret = phylink_parse_fixedlink(pl, fwnode);
632 ret = phylink_register_sfp(pl, fwnode);
640 EXPORT_SYMBOL_GPL(phylink_create);
643 * phylink_destroy() - cleanup and destroy the phylink instance
644 * @pl: a pointer to a &struct phylink returned from phylink_create()
646 * Destroy a phylink instance. Any PHY that has been attached must have been
647 * cleaned up via phylink_disconnect_phy() prior to calling this function.
649 void phylink_destroy(struct phylink *pl)
652 sfp_unregister_upstream(pl->sfp_bus);
653 if (!IS_ERR_OR_NULL(pl->link_gpio))
654 gpiod_put(pl->link_gpio);
656 cancel_work_sync(&pl->resolve);
659 EXPORT_SYMBOL_GPL(phylink_destroy);
661 static void phylink_phy_change(struct phy_device *phydev, bool up,
664 struct phylink *pl = phydev->phylink;
666 mutex_lock(&pl->state_mutex);
667 pl->phy_state.speed = phydev->speed;
668 pl->phy_state.duplex = phydev->duplex;
669 pl->phy_state.pause = MLO_PAUSE_NONE;
671 pl->phy_state.pause |= MLO_PAUSE_SYM;
672 if (phydev->asym_pause)
673 pl->phy_state.pause |= MLO_PAUSE_ASYM;
674 pl->phy_state.interface = phydev->interface;
675 pl->phy_state.link = up;
676 mutex_unlock(&pl->state_mutex);
678 phylink_run_resolve(pl);
680 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
681 phy_modes(phydev->interface),
682 phy_speed_to_str(phydev->speed),
683 phy_duplex_to_str(phydev->duplex));
686 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
688 struct phylink_link_state config;
689 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
693 memset(&config, 0, sizeof(config));
694 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
695 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
697 config.interface = pl->link_config.interface;
700 * This is the new way of dealing with flow control for PHYs,
701 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
702 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
703 * using our validate call to the MAC, we rely upon the MAC
704 * clearing the bits from both supported and advertising fields.
706 if (phylink_test(supported, Pause))
707 phylink_set(config.advertising, Pause);
708 if (phylink_test(supported, Asym_Pause))
709 phylink_set(config.advertising, Asym_Pause);
711 ret = phylink_validate(pl, supported, &config);
716 phy->phy_link_change = phylink_phy_change;
718 netdev_info(pl->netdev,
719 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
722 mutex_lock(&phy->lock);
723 mutex_lock(&pl->state_mutex);
725 linkmode_copy(pl->supported, supported);
726 linkmode_copy(pl->link_config.advertising, config.advertising);
728 /* Restrict the phy advertisement according to the MAC support. */
729 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
730 phy->advertising = advertising;
731 mutex_unlock(&pl->state_mutex);
732 mutex_unlock(&phy->lock);
734 netdev_dbg(pl->netdev,
735 "phy: setting supported %*pb advertising 0x%08x\n",
736 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
739 phy_start_machine(phy);
741 phy_start_interrupts(phy);
746 static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
747 phy_interface_t interface)
751 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
752 (pl->link_an_mode == MLO_AN_INBAND &&
753 phy_interface_mode_is_8023z(interface))))
759 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
763 ret = phylink_bringup_phy(pl, phy);
771 * phylink_connect_phy() - connect a PHY to the phylink instance
772 * @pl: a pointer to a &struct phylink returned from phylink_create()
773 * @phy: a pointer to a &struct phy_device.
775 * Connect @phy to the phylink instance specified by @pl by calling
776 * phy_attach_direct(). Configure the @phy according to the MAC driver's
777 * capabilities, start the PHYLIB state machine and enable any interrupts
778 * that the PHY supports.
780 * This updates the phylink's ethtool supported and advertising link mode
783 * Returns 0 on success or a negative errno.
785 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
787 /* Use PHY device/driver interface */
788 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
789 pl->link_interface = phy->interface;
790 pl->link_config.interface = pl->link_interface;
793 return __phylink_connect_phy(pl, phy, pl->link_interface);
795 EXPORT_SYMBOL_GPL(phylink_connect_phy);
798 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
799 * @pl: a pointer to a &struct phylink returned from phylink_create()
800 * @dn: a pointer to a &struct device_node.
801 * @flags: PHY-specific flags to communicate to the PHY device driver
803 * Connect the phy specified in the device node @dn to the phylink instance
804 * specified by @pl. Actions specified in phylink_connect_phy() will be
807 * Returns 0 on success or a negative errno.
809 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
812 struct device_node *phy_node;
813 struct phy_device *phy_dev;
816 /* Fixed links and 802.3z are handled without needing a PHY */
817 if (pl->link_an_mode == MLO_AN_FIXED ||
818 (pl->link_an_mode == MLO_AN_INBAND &&
819 phy_interface_mode_is_8023z(pl->link_interface)))
822 phy_node = of_parse_phandle(dn, "phy-handle", 0);
824 phy_node = of_parse_phandle(dn, "phy", 0);
826 phy_node = of_parse_phandle(dn, "phy-device", 0);
829 if (pl->link_an_mode == MLO_AN_PHY)
834 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
836 /* We're done with the phy_node handle */
837 of_node_put(phy_node);
842 ret = phylink_bringup_phy(pl, phy_dev);
848 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
851 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
853 * @pl: a pointer to a &struct phylink returned from phylink_create()
855 * Disconnect any current PHY from the phylink instance described by @pl.
857 void phylink_disconnect_phy(struct phylink *pl)
859 struct phy_device *phy;
865 mutex_lock(&phy->lock);
866 mutex_lock(&pl->state_mutex);
868 mutex_unlock(&pl->state_mutex);
869 mutex_unlock(&phy->lock);
870 flush_work(&pl->resolve);
875 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
878 * phylink_fixed_state_cb() - allow setting a fixed link callback
879 * @pl: a pointer to a &struct phylink returned from phylink_create()
880 * @cb: callback to execute to determine the fixed link state.
882 * The MAC driver should call this driver when the state of its link
883 * can be determined through e.g: an out of band MMIO register.
885 int phylink_fixed_state_cb(struct phylink *pl,
886 void (*cb)(struct net_device *dev,
887 struct phylink_link_state *state))
889 /* It does not make sense to let the link be overriden unless we use
892 if (pl->link_an_mode != MLO_AN_FIXED)
895 mutex_lock(&pl->state_mutex);
896 pl->get_fixed_state = cb;
897 mutex_unlock(&pl->state_mutex);
901 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
904 * phylink_mac_change() - notify phylink of a change in MAC state
905 * @pl: a pointer to a &struct phylink returned from phylink_create()
906 * @up: indicates whether the link is currently up.
908 * The MAC driver should call this driver when the state of its link
909 * changes (eg, link failure, new negotiation results, etc.)
911 void phylink_mac_change(struct phylink *pl, bool up)
914 pl->mac_link_dropped = true;
915 phylink_run_resolve(pl);
916 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
918 EXPORT_SYMBOL_GPL(phylink_mac_change);
921 * phylink_start() - start a phylink instance
922 * @pl: a pointer to a &struct phylink returned from phylink_create()
924 * Start the phylink instance specified by @pl, configuring the MAC for the
925 * desired link mode(s) and negotiation style. This should be called from the
926 * network device driver's &struct net_device_ops ndo_open() method.
928 void phylink_start(struct phylink *pl)
932 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
933 phylink_an_mode_str(pl->link_an_mode),
934 phy_modes(pl->link_config.interface));
936 /* Always set the carrier off */
937 netif_carrier_off(pl->netdev);
939 /* Apply the link configuration to the MAC when starting. This allows
940 * a fixed-link to start with the correct parameters, and also
941 * ensures that we set the appropriate advertisement for Serdes links.
943 phylink_resolve_flow(pl, &pl->link_config);
944 phylink_mac_config(pl, &pl->link_config);
946 /* Restart autonegotiation if using 802.3z to ensure that the link
947 * parameters are properly negotiated. This is necessary for DSA
948 * switches using 802.3z negotiation to ensure they see our modes.
950 phylink_mac_an_restart(pl);
952 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
953 phylink_run_resolve(pl);
955 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
956 mod_timer(&pl->link_poll, jiffies + HZ);
958 sfp_upstream_start(pl->sfp_bus);
960 phy_start(pl->phydev);
962 EXPORT_SYMBOL_GPL(phylink_start);
965 * phylink_stop() - stop a phylink instance
966 * @pl: a pointer to a &struct phylink returned from phylink_create()
968 * Stop the phylink instance specified by @pl. This should be called from the
969 * network device driver's &struct net_device_ops ndo_stop() method. The
970 * network device's carrier state should not be changed prior to calling this
973 void phylink_stop(struct phylink *pl)
978 phy_stop(pl->phydev);
980 sfp_upstream_stop(pl->sfp_bus);
981 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
982 del_timer_sync(&pl->link_poll);
984 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
986 EXPORT_SYMBOL_GPL(phylink_stop);
989 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
990 * @pl: a pointer to a &struct phylink returned from phylink_create()
991 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
993 * Read the wake on lan parameters from the PHY attached to the phylink
994 * instance specified by @pl. If no PHY is currently attached, report no
995 * support for wake on lan.
997 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1005 phy_ethtool_get_wol(pl->phydev, wol);
1007 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1010 * phylink_ethtool_set_wol() - set wake on lan parameters
1011 * @pl: a pointer to a &struct phylink returned from phylink_create()
1012 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1014 * Set the wake on lan parameters for the PHY attached to the phylink
1015 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1018 * Returns zero on success or negative errno code.
1020 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1022 int ret = -EOPNOTSUPP;
1027 ret = phy_ethtool_set_wol(pl->phydev, wol);
1031 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1033 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1035 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1037 linkmode_zero(mask);
1038 phylink_set_port_modes(mask);
1040 linkmode_and(dst, dst, mask);
1041 linkmode_or(dst, dst, b);
1044 static void phylink_get_ksettings(const struct phylink_link_state *state,
1045 struct ethtool_link_ksettings *kset)
1047 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1048 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1049 kset->base.speed = state->speed;
1050 kset->base.duplex = state->duplex;
1051 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1056 * phylink_ethtool_ksettings_get() - get the current link settings
1057 * @pl: a pointer to a &struct phylink returned from phylink_create()
1058 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1060 * Read the current link settings for the phylink instance specified by @pl.
1061 * This will be the link settings read from the MAC, PHY or fixed link
1062 * settings depending on the current negotiation mode.
1064 int phylink_ethtool_ksettings_get(struct phylink *pl,
1065 struct ethtool_link_ksettings *kset)
1067 struct phylink_link_state link_state;
1072 phy_ethtool_ksettings_get(pl->phydev, kset);
1074 kset->base.port = pl->link_port;
1077 linkmode_copy(kset->link_modes.supported, pl->supported);
1079 switch (pl->link_an_mode) {
1081 /* We are using fixed settings. Report these as the
1082 * current link settings - and note that these also
1083 * represent the supported speeds/duplex/pause modes.
1085 phylink_get_fixed_state(pl, &link_state);
1086 phylink_get_ksettings(&link_state, kset);
1090 /* If there is a phy attached, then use the reported
1091 * settings from the phy with no modification.
1096 phylink_get_mac_state(pl, &link_state);
1098 /* The MAC is reporting the link results from its own PCS
1099 * layer via in-band status. Report these as the current
1102 phylink_get_ksettings(&link_state, kset);
1108 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1111 * phylink_ethtool_ksettings_set() - set the link settings
1112 * @pl: a pointer to a &struct phylink returned from phylink_create()
1113 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1115 int phylink_ethtool_ksettings_set(struct phylink *pl,
1116 const struct ethtool_link_ksettings *kset)
1118 struct ethtool_link_ksettings our_kset;
1119 struct phylink_link_state config;
1124 if (kset->base.autoneg != AUTONEG_DISABLE &&
1125 kset->base.autoneg != AUTONEG_ENABLE)
1128 config = pl->link_config;
1130 /* Mask out unsupported advertisements */
1131 linkmode_and(config.advertising, kset->link_modes.advertising,
1134 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1135 if (kset->base.autoneg == AUTONEG_DISABLE) {
1136 const struct phy_setting *s;
1138 /* Autonegotiation disabled, select a suitable speed and
1141 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1143 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1147 /* If we have a fixed link (as specified by firmware), refuse
1148 * to change link parameters.
1150 if (pl->link_an_mode == MLO_AN_FIXED &&
1151 (s->speed != pl->link_config.speed ||
1152 s->duplex != pl->link_config.duplex))
1155 config.speed = s->speed;
1156 config.duplex = s->duplex;
1157 config.an_enabled = false;
1159 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1161 /* If we have a fixed link, refuse to enable autonegotiation */
1162 if (pl->link_an_mode == MLO_AN_FIXED)
1165 config.speed = SPEED_UNKNOWN;
1166 config.duplex = DUPLEX_UNKNOWN;
1167 config.an_enabled = true;
1169 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1172 if (phylink_validate(pl, pl->supported, &config))
1175 /* If autonegotiation is enabled, we must have an advertisement */
1176 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1180 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1181 our_kset.base.speed = config.speed;
1182 our_kset.base.duplex = config.duplex;
1184 /* If we have a PHY, configure the phy */
1186 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1191 mutex_lock(&pl->state_mutex);
1192 /* Configure the MAC to match the new settings */
1193 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1194 pl->link_config.interface = config.interface;
1195 pl->link_config.speed = our_kset.base.speed;
1196 pl->link_config.duplex = our_kset.base.duplex;
1197 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1199 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1200 phylink_mac_config(pl, &pl->link_config);
1201 phylink_mac_an_restart(pl);
1203 mutex_unlock(&pl->state_mutex);
1207 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1210 * phylink_ethtool_nway_reset() - restart negotiation
1211 * @pl: a pointer to a &struct phylink returned from phylink_create()
1213 * Restart negotiation for the phylink instance specified by @pl. This will
1214 * cause any attached phy to restart negotiation with the link partner, and
1215 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1218 * Returns zero on success, or negative error code.
1220 int phylink_ethtool_nway_reset(struct phylink *pl)
1227 ret = phy_restart_aneg(pl->phydev);
1228 phylink_mac_an_restart(pl);
1232 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1235 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1236 * @pl: a pointer to a &struct phylink returned from phylink_create()
1237 * @pause: a pointer to a &struct ethtool_pauseparam
1239 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1240 struct ethtool_pauseparam *pause)
1244 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1245 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1246 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1248 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1251 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1252 * @pl: a pointer to a &struct phylink returned from phylink_create()
1253 * @pause: a pointer to a &struct ethtool_pauseparam
1255 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1256 struct ethtool_pauseparam *pause)
1258 struct phylink_link_state *config = &pl->link_config;
1262 if (!phylink_test(pl->supported, Pause) &&
1263 !phylink_test(pl->supported, Asym_Pause))
1266 if (!phylink_test(pl->supported, Asym_Pause) &&
1267 pause->rx_pause != pause->tx_pause)
1270 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1273 config->pause |= MLO_PAUSE_AN;
1274 if (pause->rx_pause)
1275 config->pause |= MLO_PAUSE_RX;
1276 if (pause->tx_pause)
1277 config->pause |= MLO_PAUSE_TX;
1279 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1280 switch (pl->link_an_mode) {
1282 /* Silently mark the carrier down, and then trigger a resolve */
1283 netif_carrier_off(pl->netdev);
1284 phylink_run_resolve(pl);
1288 /* Should we allow fixed links to change against the config? */
1289 phylink_resolve_flow(pl, config);
1290 phylink_mac_config(pl, config);
1294 phylink_mac_config(pl, config);
1295 phylink_mac_an_restart(pl);
1302 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1305 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1307 * @pl: a pointer to a &struct phylink returned from phylink_create().
1309 * Read the Energy Efficient Ethernet error counter from the PHY associated
1310 * with the phylink instance specified by @pl.
1312 * Returns positive error counter value, or negative error code.
1314 int phylink_get_eee_err(struct phylink *pl)
1321 ret = phy_get_eee_err(pl->phydev);
1325 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1328 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1329 * @pl: a pointer to a &struct phylink returned from phylink_create()
1330 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1332 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1334 int ret = -EOPNOTSUPP;
1339 ret = phy_ethtool_get_eee(pl->phydev, eee);
1343 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1346 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1347 * @pl: a pointer to a &struct phylink returned from phylink_create()
1348 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1350 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1352 int ret = -EOPNOTSUPP;
1357 ret = phy_ethtool_set_eee(pl->phydev, eee);
1361 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1363 /* This emulates MII registers for a fixed-mode phy operating as per the
1364 * passed in state. "aneg" defines if we report negotiation is possible.
1366 * FIXME: should deal with negotiation state too.
1368 static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1369 struct phylink_link_state *state, bool aneg)
1371 struct fixed_phy_status fs;
1374 fs.link = state->link;
1375 fs.speed = state->speed;
1376 fs.duplex = state->duplex;
1377 fs.pause = state->pause & MLO_PAUSE_SYM;
1378 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1380 val = swphy_read_reg(reg, &fs);
1381 if (reg == MII_BMSR) {
1382 if (!state->an_complete)
1383 val &= ~BMSR_ANEGCOMPLETE;
1385 val &= ~BMSR_ANEGCAPABLE;
1390 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1393 struct phy_device *phydev = pl->phydev;
1396 if (mdio_phy_id_is_c45(phy_id)) {
1397 prtad = mdio_phy_id_prtad(phy_id);
1398 devad = mdio_phy_id_devad(phy_id);
1399 devad = MII_ADDR_C45 | devad << 16 | reg;
1400 } else if (phydev->is_c45) {
1406 devad = __ffs(phydev->c45_ids.devices_in_package);
1410 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1412 devad = MDIO_MMD_AN;
1413 if (reg == MII_ADVERTISE)
1414 reg = MDIO_AN_ADVERTISE;
1422 devad = MII_ADDR_C45 | devad << 16 | reg;
1427 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1430 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1431 unsigned int reg, unsigned int val)
1433 struct phy_device *phydev = pl->phydev;
1436 if (mdio_phy_id_is_c45(phy_id)) {
1437 prtad = mdio_phy_id_prtad(phy_id);
1438 devad = mdio_phy_id_devad(phy_id);
1439 devad = MII_ADDR_C45 | devad << 16 | reg;
1440 } else if (phydev->is_c45) {
1446 devad = __ffs(phydev->c45_ids.devices_in_package);
1450 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1452 devad = MDIO_MMD_AN;
1453 if (reg == MII_ADVERTISE)
1454 reg = MDIO_AN_ADVERTISE;
1462 devad = MII_ADDR_C45 | devad << 16 | reg;
1468 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1471 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1474 struct phylink_link_state state;
1477 switch (pl->link_an_mode) {
1480 phylink_get_fixed_state(pl, &state);
1481 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1491 val = phylink_get_mac_state(pl, &state);
1495 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1501 return val & 0xffff;
1504 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1505 unsigned int reg, unsigned int val)
1507 switch (pl->link_an_mode) {
1522 * phylink_mii_ioctl() - generic mii ioctl interface
1523 * @pl: a pointer to a &struct phylink returned from phylink_create()
1524 * @ifr: a pointer to a &struct ifreq for socket ioctls
1525 * @cmd: ioctl cmd to execute
1527 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1528 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1530 * Returns: zero on success or negative error code.
1533 * read register from the current PHY.
1535 * read register from the specified PHY.
1537 * set a register on the specified PHY.
1539 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1541 struct mii_ioctl_data *mii = if_mii(ifr);
1547 /* PHYs only exist for MLO_AN_PHY and SGMII */
1550 mii->phy_id = pl->phydev->mdio.addr;
1554 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1562 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1567 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1577 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1585 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1597 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1599 static int phylink_sfp_module_insert(void *upstream,
1600 const struct sfp_eeprom_id *id)
1602 struct phylink *pl = upstream;
1603 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1604 struct phylink_link_state config;
1605 phy_interface_t iface;
1612 sfp_parse_support(pl->sfp_bus, id, support);
1613 port = sfp_parse_port(pl->sfp_bus, id, support);
1615 memset(&config, 0, sizeof(config));
1616 linkmode_copy(config.advertising, support);
1617 config.interface = PHY_INTERFACE_MODE_NA;
1618 config.speed = SPEED_UNKNOWN;
1619 config.duplex = DUPLEX_UNKNOWN;
1620 config.pause = MLO_PAUSE_AN;
1621 config.an_enabled = pl->link_config.an_enabled;
1623 /* Ignore errors if we're expecting a PHY to attach later */
1624 ret = phylink_validate(pl, support, &config);
1626 netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
1627 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1631 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1632 if (iface == PHY_INTERFACE_MODE_NA) {
1633 netdev_err(pl->netdev,
1634 "selection of interface failed, advertisement %*pb\n",
1635 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1639 config.interface = iface;
1640 ret = phylink_validate(pl, support, &config);
1642 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1643 phylink_an_mode_str(MLO_AN_INBAND),
1644 phy_modes(config.interface),
1645 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1649 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1650 phylink_an_mode_str(MLO_AN_INBAND),
1651 phy_modes(config.interface),
1652 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1654 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1657 changed = !bitmap_equal(pl->supported, support,
1658 __ETHTOOL_LINK_MODE_MASK_NBITS);
1660 linkmode_copy(pl->supported, support);
1661 linkmode_copy(pl->link_config.advertising, config.advertising);
1664 if (pl->link_an_mode != MLO_AN_INBAND ||
1665 pl->link_config.interface != config.interface) {
1666 pl->link_config.interface = config.interface;
1667 pl->link_an_mode = MLO_AN_INBAND;
1671 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1672 phylink_an_mode_str(MLO_AN_INBAND),
1673 phy_modes(config.interface));
1676 pl->link_port = port;
1678 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1679 &pl->phylink_disable_state))
1680 phylink_mac_config(pl, &pl->link_config);
1685 static void phylink_sfp_link_down(void *upstream)
1687 struct phylink *pl = upstream;
1691 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
1694 static void phylink_sfp_link_up(void *upstream)
1696 struct phylink *pl = upstream;
1700 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1701 phylink_run_resolve(pl);
1704 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1706 struct phylink *pl = upstream;
1708 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
1711 static void phylink_sfp_disconnect_phy(void *upstream)
1713 phylink_disconnect_phy(upstream);
1716 static const struct sfp_upstream_ops sfp_phylink_ops = {
1717 .module_insert = phylink_sfp_module_insert,
1718 .link_up = phylink_sfp_link_up,
1719 .link_down = phylink_sfp_link_down,
1720 .connect_phy = phylink_sfp_connect_phy,
1721 .disconnect_phy = phylink_sfp_disconnect_phy,
1724 /* Helpers for MAC drivers */
1727 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1728 * @state: a pointer to a &struct phylink_link_state
1730 * Inspect the interface mode, advertising mask or forced speed and
1731 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1732 * the interface mode to suit. @state->interface is appropriately
1733 * updated, and the advertising mask has the "other" baseX_Full flag
1736 void phylink_helper_basex_speed(struct phylink_link_state *state)
1738 if (phy_interface_mode_is_8023z(state->interface)) {
1739 bool want_2500 = state->an_enabled ?
1740 phylink_test(state->advertising, 2500baseX_Full) :
1741 state->speed == SPEED_2500;
1744 phylink_clear(state->advertising, 1000baseX_Full);
1745 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1747 phylink_clear(state->advertising, 2500baseX_Full);
1748 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1752 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1754 MODULE_LICENSE("GPL");