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 pl->sfp_bus = sfp_register_upstream(ref.fwnode, pl->netdev, pl,
566 * phylink_create() - create a phylink instance
567 * @ndev: a pointer to the &struct net_device
568 * @fwnode: a pointer to a &struct fwnode_handle describing the network
570 * @iface: the desired link mode defined by &typedef phy_interface_t
571 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
573 * Create a new phylink instance, and parse the link parameters found in @np.
574 * This will parse in-band modes, fixed-link or SFP configuration.
576 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
577 * must use IS_ERR() to check for errors from this function.
579 struct phylink *phylink_create(struct net_device *ndev,
580 struct fwnode_handle *fwnode,
581 phy_interface_t iface,
582 const struct phylink_mac_ops *ops)
587 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
589 return ERR_PTR(-ENOMEM);
591 mutex_init(&pl->state_mutex);
592 INIT_WORK(&pl->resolve, phylink_resolve);
594 pl->phy_state.interface = iface;
595 pl->link_interface = iface;
596 if (iface == PHY_INTERFACE_MODE_MOCA)
597 pl->link_port = PORT_BNC;
599 pl->link_port = PORT_MII;
600 pl->link_config.interface = iface;
601 pl->link_config.pause = MLO_PAUSE_AN;
602 pl->link_config.speed = SPEED_UNKNOWN;
603 pl->link_config.duplex = DUPLEX_UNKNOWN;
604 pl->link_config.an_enabled = true;
606 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
607 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
609 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
610 linkmode_copy(pl->link_config.advertising, pl->supported);
611 phylink_validate(pl, pl->supported, &pl->link_config);
613 ret = phylink_parse_mode(pl, fwnode);
619 if (pl->link_an_mode == MLO_AN_FIXED) {
620 ret = phylink_parse_fixedlink(pl, fwnode);
627 ret = phylink_register_sfp(pl, fwnode);
635 EXPORT_SYMBOL_GPL(phylink_create);
638 * phylink_destroy() - cleanup and destroy the phylink instance
639 * @pl: a pointer to a &struct phylink returned from phylink_create()
641 * Destroy a phylink instance. Any PHY that has been attached must have been
642 * cleaned up via phylink_disconnect_phy() prior to calling this function.
644 void phylink_destroy(struct phylink *pl)
647 sfp_unregister_upstream(pl->sfp_bus);
648 if (!IS_ERR_OR_NULL(pl->link_gpio))
649 gpiod_put(pl->link_gpio);
651 cancel_work_sync(&pl->resolve);
654 EXPORT_SYMBOL_GPL(phylink_destroy);
656 static void phylink_phy_change(struct phy_device *phydev, bool up,
659 struct phylink *pl = phydev->phylink;
661 mutex_lock(&pl->state_mutex);
662 pl->phy_state.speed = phydev->speed;
663 pl->phy_state.duplex = phydev->duplex;
664 pl->phy_state.pause = MLO_PAUSE_NONE;
666 pl->phy_state.pause |= MLO_PAUSE_SYM;
667 if (phydev->asym_pause)
668 pl->phy_state.pause |= MLO_PAUSE_ASYM;
669 pl->phy_state.interface = phydev->interface;
670 pl->phy_state.link = up;
671 mutex_unlock(&pl->state_mutex);
673 phylink_run_resolve(pl);
675 netdev_dbg(pl->netdev, "phy link %s %s/%s/%s\n", up ? "up" : "down",
676 phy_modes(phydev->interface),
677 phy_speed_to_str(phydev->speed),
678 phy_duplex_to_str(phydev->duplex));
681 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
683 struct phylink_link_state config;
684 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
688 memset(&config, 0, sizeof(config));
689 ethtool_convert_legacy_u32_to_link_mode(supported, phy->supported);
690 ethtool_convert_legacy_u32_to_link_mode(config.advertising,
692 config.interface = pl->link_config.interface;
695 * This is the new way of dealing with flow control for PHYs,
696 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
697 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
698 * using our validate call to the MAC, we rely upon the MAC
699 * clearing the bits from both supported and advertising fields.
701 if (phylink_test(supported, Pause))
702 phylink_set(config.advertising, Pause);
703 if (phylink_test(supported, Asym_Pause))
704 phylink_set(config.advertising, Asym_Pause);
706 ret = phylink_validate(pl, supported, &config);
711 phy->phy_link_change = phylink_phy_change;
713 netdev_info(pl->netdev,
714 "PHY [%s] driver [%s]\n", dev_name(&phy->mdio.dev),
717 mutex_lock(&phy->lock);
718 mutex_lock(&pl->state_mutex);
720 linkmode_copy(pl->supported, supported);
721 linkmode_copy(pl->link_config.advertising, config.advertising);
723 /* Restrict the phy advertisement according to the MAC support. */
724 ethtool_convert_link_mode_to_legacy_u32(&advertising, config.advertising);
725 phy->advertising = advertising;
726 mutex_unlock(&pl->state_mutex);
727 mutex_unlock(&phy->lock);
729 netdev_dbg(pl->netdev,
730 "phy: setting supported %*pb advertising 0x%08x\n",
731 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
734 phy_start_machine(phy);
736 phy_start_interrupts(phy);
741 static int __phylink_connect_phy(struct phylink *pl, struct phy_device *phy,
742 phy_interface_t interface)
746 if (WARN_ON(pl->link_an_mode == MLO_AN_FIXED ||
747 (pl->link_an_mode == MLO_AN_INBAND &&
748 phy_interface_mode_is_8023z(interface))))
754 ret = phy_attach_direct(pl->netdev, phy, 0, interface);
758 ret = phylink_bringup_phy(pl, phy);
766 * phylink_connect_phy() - connect a PHY to the phylink instance
767 * @pl: a pointer to a &struct phylink returned from phylink_create()
768 * @phy: a pointer to a &struct phy_device.
770 * Connect @phy to the phylink instance specified by @pl by calling
771 * phy_attach_direct(). Configure the @phy according to the MAC driver's
772 * capabilities, start the PHYLIB state machine and enable any interrupts
773 * that the PHY supports.
775 * This updates the phylink's ethtool supported and advertising link mode
778 * Returns 0 on success or a negative errno.
780 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
782 /* Use PHY device/driver interface */
783 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
784 pl->link_interface = phy->interface;
785 pl->link_config.interface = pl->link_interface;
788 return __phylink_connect_phy(pl, phy, pl->link_interface);
790 EXPORT_SYMBOL_GPL(phylink_connect_phy);
793 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
794 * @pl: a pointer to a &struct phylink returned from phylink_create()
795 * @dn: a pointer to a &struct device_node.
796 * @flags: PHY-specific flags to communicate to the PHY device driver
798 * Connect the phy specified in the device node @dn to the phylink instance
799 * specified by @pl. Actions specified in phylink_connect_phy() will be
802 * Returns 0 on success or a negative errno.
804 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
807 struct device_node *phy_node;
808 struct phy_device *phy_dev;
811 /* Fixed links and 802.3z are handled without needing a PHY */
812 if (pl->link_an_mode == MLO_AN_FIXED ||
813 (pl->link_an_mode == MLO_AN_INBAND &&
814 phy_interface_mode_is_8023z(pl->link_interface)))
817 phy_node = of_parse_phandle(dn, "phy-handle", 0);
819 phy_node = of_parse_phandle(dn, "phy", 0);
821 phy_node = of_parse_phandle(dn, "phy-device", 0);
824 if (pl->link_an_mode == MLO_AN_PHY)
829 phy_dev = of_phy_attach(pl->netdev, phy_node, flags,
831 /* We're done with the phy_node handle */
832 of_node_put(phy_node);
837 ret = phylink_bringup_phy(pl, phy_dev);
843 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
846 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
848 * @pl: a pointer to a &struct phylink returned from phylink_create()
850 * Disconnect any current PHY from the phylink instance described by @pl.
852 void phylink_disconnect_phy(struct phylink *pl)
854 struct phy_device *phy;
860 mutex_lock(&phy->lock);
861 mutex_lock(&pl->state_mutex);
863 mutex_unlock(&pl->state_mutex);
864 mutex_unlock(&phy->lock);
865 flush_work(&pl->resolve);
870 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
873 * phylink_fixed_state_cb() - allow setting a fixed link callback
874 * @pl: a pointer to a &struct phylink returned from phylink_create()
875 * @cb: callback to execute to determine the fixed link state.
877 * The MAC driver should call this driver when the state of its link
878 * can be determined through e.g: an out of band MMIO register.
880 int phylink_fixed_state_cb(struct phylink *pl,
881 void (*cb)(struct net_device *dev,
882 struct phylink_link_state *state))
884 /* It does not make sense to let the link be overriden unless we use
887 if (pl->link_an_mode != MLO_AN_FIXED)
890 mutex_lock(&pl->state_mutex);
891 pl->get_fixed_state = cb;
892 mutex_unlock(&pl->state_mutex);
896 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
899 * phylink_mac_change() - notify phylink of a change in MAC state
900 * @pl: a pointer to a &struct phylink returned from phylink_create()
901 * @up: indicates whether the link is currently up.
903 * The MAC driver should call this driver when the state of its link
904 * changes (eg, link failure, new negotiation results, etc.)
906 void phylink_mac_change(struct phylink *pl, bool up)
909 pl->mac_link_dropped = true;
910 phylink_run_resolve(pl);
911 netdev_dbg(pl->netdev, "mac link %s\n", up ? "up" : "down");
913 EXPORT_SYMBOL_GPL(phylink_mac_change);
916 * phylink_start() - start a phylink instance
917 * @pl: a pointer to a &struct phylink returned from phylink_create()
919 * Start the phylink instance specified by @pl, configuring the MAC for the
920 * desired link mode(s) and negotiation style. This should be called from the
921 * network device driver's &struct net_device_ops ndo_open() method.
923 void phylink_start(struct phylink *pl)
927 netdev_info(pl->netdev, "configuring for %s/%s link mode\n",
928 phylink_an_mode_str(pl->link_an_mode),
929 phy_modes(pl->link_config.interface));
931 /* Always set the carrier off */
932 netif_carrier_off(pl->netdev);
934 /* Apply the link configuration to the MAC when starting. This allows
935 * a fixed-link to start with the correct parameters, and also
936 * ensures that we set the appropriate advertisement for Serdes links.
938 phylink_resolve_flow(pl, &pl->link_config);
939 phylink_mac_config(pl, &pl->link_config);
941 /* Restart autonegotiation if using 802.3z to ensure that the link
942 * parameters are properly negotiated. This is necessary for DSA
943 * switches using 802.3z negotiation to ensure they see our modes.
945 phylink_mac_an_restart(pl);
947 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
948 phylink_run_resolve(pl);
950 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
951 mod_timer(&pl->link_poll, jiffies + HZ);
953 sfp_upstream_start(pl->sfp_bus);
955 phy_start(pl->phydev);
957 EXPORT_SYMBOL_GPL(phylink_start);
960 * phylink_stop() - stop a phylink instance
961 * @pl: a pointer to a &struct phylink returned from phylink_create()
963 * Stop the phylink instance specified by @pl. This should be called from the
964 * network device driver's &struct net_device_ops ndo_stop() method. The
965 * network device's carrier state should not be changed prior to calling this
968 void phylink_stop(struct phylink *pl)
973 phy_stop(pl->phydev);
975 sfp_upstream_stop(pl->sfp_bus);
976 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
977 del_timer_sync(&pl->link_poll);
979 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
981 EXPORT_SYMBOL_GPL(phylink_stop);
984 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
985 * @pl: a pointer to a &struct phylink returned from phylink_create()
986 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
988 * Read the wake on lan parameters from the PHY attached to the phylink
989 * instance specified by @pl. If no PHY is currently attached, report no
990 * support for wake on lan.
992 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1000 phy_ethtool_get_wol(pl->phydev, wol);
1002 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1005 * phylink_ethtool_set_wol() - set wake on lan parameters
1006 * @pl: a pointer to a &struct phylink returned from phylink_create()
1007 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1009 * Set the wake on lan parameters for the PHY attached to the phylink
1010 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1013 * Returns zero on success or negative errno code.
1015 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1017 int ret = -EOPNOTSUPP;
1022 ret = phy_ethtool_set_wol(pl->phydev, wol);
1026 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1028 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1030 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1032 linkmode_zero(mask);
1033 phylink_set_port_modes(mask);
1035 linkmode_and(dst, dst, mask);
1036 linkmode_or(dst, dst, b);
1039 static void phylink_get_ksettings(const struct phylink_link_state *state,
1040 struct ethtool_link_ksettings *kset)
1042 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1043 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1044 kset->base.speed = state->speed;
1045 kset->base.duplex = state->duplex;
1046 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1051 * phylink_ethtool_ksettings_get() - get the current link settings
1052 * @pl: a pointer to a &struct phylink returned from phylink_create()
1053 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1055 * Read the current link settings for the phylink instance specified by @pl.
1056 * This will be the link settings read from the MAC, PHY or fixed link
1057 * settings depending on the current negotiation mode.
1059 int phylink_ethtool_ksettings_get(struct phylink *pl,
1060 struct ethtool_link_ksettings *kset)
1062 struct phylink_link_state link_state;
1067 phy_ethtool_ksettings_get(pl->phydev, kset);
1069 kset->base.port = pl->link_port;
1072 linkmode_copy(kset->link_modes.supported, pl->supported);
1074 switch (pl->link_an_mode) {
1076 /* We are using fixed settings. Report these as the
1077 * current link settings - and note that these also
1078 * represent the supported speeds/duplex/pause modes.
1080 phylink_get_fixed_state(pl, &link_state);
1081 phylink_get_ksettings(&link_state, kset);
1085 /* If there is a phy attached, then use the reported
1086 * settings from the phy with no modification.
1091 phylink_get_mac_state(pl, &link_state);
1093 /* The MAC is reporting the link results from its own PCS
1094 * layer via in-band status. Report these as the current
1097 phylink_get_ksettings(&link_state, kset);
1103 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1106 * phylink_ethtool_ksettings_set() - set the link settings
1107 * @pl: a pointer to a &struct phylink returned from phylink_create()
1108 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1110 int phylink_ethtool_ksettings_set(struct phylink *pl,
1111 const struct ethtool_link_ksettings *kset)
1113 struct ethtool_link_ksettings our_kset;
1114 struct phylink_link_state config;
1119 if (kset->base.autoneg != AUTONEG_DISABLE &&
1120 kset->base.autoneg != AUTONEG_ENABLE)
1123 config = pl->link_config;
1125 /* Mask out unsupported advertisements */
1126 linkmode_and(config.advertising, kset->link_modes.advertising,
1129 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1130 if (kset->base.autoneg == AUTONEG_DISABLE) {
1131 const struct phy_setting *s;
1133 /* Autonegotiation disabled, select a suitable speed and
1136 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1138 __ETHTOOL_LINK_MODE_MASK_NBITS, false);
1142 /* If we have a fixed link (as specified by firmware), refuse
1143 * to change link parameters.
1145 if (pl->link_an_mode == MLO_AN_FIXED &&
1146 (s->speed != pl->link_config.speed ||
1147 s->duplex != pl->link_config.duplex))
1150 config.speed = s->speed;
1151 config.duplex = s->duplex;
1152 config.an_enabled = false;
1154 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1156 /* If we have a fixed link, refuse to enable autonegotiation */
1157 if (pl->link_an_mode == MLO_AN_FIXED)
1160 config.speed = SPEED_UNKNOWN;
1161 config.duplex = DUPLEX_UNKNOWN;
1162 config.an_enabled = true;
1164 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1167 if (phylink_validate(pl, pl->supported, &config))
1170 /* If autonegotiation is enabled, we must have an advertisement */
1171 if (config.an_enabled && phylink_is_empty_linkmode(config.advertising))
1175 linkmode_copy(our_kset.link_modes.advertising, config.advertising);
1176 our_kset.base.speed = config.speed;
1177 our_kset.base.duplex = config.duplex;
1179 /* If we have a PHY, configure the phy */
1181 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1186 mutex_lock(&pl->state_mutex);
1187 /* Configure the MAC to match the new settings */
1188 linkmode_copy(pl->link_config.advertising, our_kset.link_modes.advertising);
1189 pl->link_config.interface = config.interface;
1190 pl->link_config.speed = our_kset.base.speed;
1191 pl->link_config.duplex = our_kset.base.duplex;
1192 pl->link_config.an_enabled = our_kset.base.autoneg != AUTONEG_DISABLE;
1194 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1195 phylink_mac_config(pl, &pl->link_config);
1196 phylink_mac_an_restart(pl);
1198 mutex_unlock(&pl->state_mutex);
1202 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1205 * phylink_ethtool_nway_reset() - restart negotiation
1206 * @pl: a pointer to a &struct phylink returned from phylink_create()
1208 * Restart negotiation for the phylink instance specified by @pl. This will
1209 * cause any attached phy to restart negotiation with the link partner, and
1210 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1213 * Returns zero on success, or negative error code.
1215 int phylink_ethtool_nway_reset(struct phylink *pl)
1222 ret = phy_restart_aneg(pl->phydev);
1223 phylink_mac_an_restart(pl);
1227 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1230 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1231 * @pl: a pointer to a &struct phylink returned from phylink_create()
1232 * @pause: a pointer to a &struct ethtool_pauseparam
1234 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1235 struct ethtool_pauseparam *pause)
1239 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1240 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1241 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1243 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1246 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1247 * @pl: a pointer to a &struct phylink returned from phylink_create()
1248 * @pause: a pointer to a &struct ethtool_pauseparam
1250 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1251 struct ethtool_pauseparam *pause)
1253 struct phylink_link_state *config = &pl->link_config;
1257 if (!phylink_test(pl->supported, Pause) &&
1258 !phylink_test(pl->supported, Asym_Pause))
1261 if (!phylink_test(pl->supported, Asym_Pause) &&
1262 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1265 config->pause &= ~(MLO_PAUSE_AN | MLO_PAUSE_TXRX_MASK);
1268 config->pause |= MLO_PAUSE_AN;
1269 if (pause->rx_pause)
1270 config->pause |= MLO_PAUSE_RX;
1271 if (pause->tx_pause)
1272 config->pause |= MLO_PAUSE_TX;
1274 if (!test_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state)) {
1275 switch (pl->link_an_mode) {
1277 /* Silently mark the carrier down, and then trigger a resolve */
1278 netif_carrier_off(pl->netdev);
1279 phylink_run_resolve(pl);
1283 /* Should we allow fixed links to change against the config? */
1284 phylink_resolve_flow(pl, config);
1285 phylink_mac_config(pl, config);
1289 phylink_mac_config(pl, config);
1290 phylink_mac_an_restart(pl);
1297 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1300 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1302 * @pl: a pointer to a &struct phylink returned from phylink_create().
1304 * Read the Energy Efficient Ethernet error counter from the PHY associated
1305 * with the phylink instance specified by @pl.
1307 * Returns positive error counter value, or negative error code.
1309 int phylink_get_eee_err(struct phylink *pl)
1316 ret = phy_get_eee_err(pl->phydev);
1320 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1323 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1324 * @pl: a pointer to a &struct phylink returned from phylink_create()
1325 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1327 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1329 int ret = -EOPNOTSUPP;
1334 ret = phy_ethtool_get_eee(pl->phydev, eee);
1338 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1341 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1342 * @pl: a pointer to a &struct phylink returned from phylink_create()
1343 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1345 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1347 int ret = -EOPNOTSUPP;
1352 ret = phy_ethtool_set_eee(pl->phydev, eee);
1356 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1358 /* This emulates MII registers for a fixed-mode phy operating as per the
1359 * passed in state. "aneg" defines if we report negotiation is possible.
1361 * FIXME: should deal with negotiation state too.
1363 static int phylink_mii_emul_read(struct net_device *ndev, unsigned int reg,
1364 struct phylink_link_state *state, bool aneg)
1366 struct fixed_phy_status fs;
1369 fs.link = state->link;
1370 fs.speed = state->speed;
1371 fs.duplex = state->duplex;
1372 fs.pause = state->pause & MLO_PAUSE_SYM;
1373 fs.asym_pause = state->pause & MLO_PAUSE_ASYM;
1375 val = swphy_read_reg(reg, &fs);
1376 if (reg == MII_BMSR) {
1377 if (!state->an_complete)
1378 val &= ~BMSR_ANEGCOMPLETE;
1380 val &= ~BMSR_ANEGCAPABLE;
1385 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1388 struct phy_device *phydev = pl->phydev;
1391 if (mdio_phy_id_is_c45(phy_id)) {
1392 prtad = mdio_phy_id_prtad(phy_id);
1393 devad = mdio_phy_id_devad(phy_id);
1394 devad = MII_ADDR_C45 | devad << 16 | reg;
1395 } else if (phydev->is_c45) {
1401 devad = __ffs(phydev->c45_ids.devices_in_package);
1405 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1407 devad = MDIO_MMD_AN;
1408 if (reg == MII_ADVERTISE)
1409 reg = MDIO_AN_ADVERTISE;
1417 devad = MII_ADDR_C45 | devad << 16 | reg;
1422 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1425 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1426 unsigned int reg, unsigned int val)
1428 struct phy_device *phydev = pl->phydev;
1431 if (mdio_phy_id_is_c45(phy_id)) {
1432 prtad = mdio_phy_id_prtad(phy_id);
1433 devad = mdio_phy_id_devad(phy_id);
1434 devad = MII_ADDR_C45 | devad << 16 | reg;
1435 } else if (phydev->is_c45) {
1441 devad = __ffs(phydev->c45_ids.devices_in_package);
1445 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1447 devad = MDIO_MMD_AN;
1448 if (reg == MII_ADVERTISE)
1449 reg = MDIO_AN_ADVERTISE;
1457 devad = MII_ADDR_C45 | devad << 16 | reg;
1463 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1466 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1469 struct phylink_link_state state;
1472 switch (pl->link_an_mode) {
1475 phylink_get_fixed_state(pl, &state);
1476 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1486 val = phylink_get_mac_state(pl, &state);
1490 val = phylink_mii_emul_read(pl->netdev, reg, &state,
1496 return val & 0xffff;
1499 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1500 unsigned int reg, unsigned int val)
1502 switch (pl->link_an_mode) {
1517 * phylink_mii_ioctl() - generic mii ioctl interface
1518 * @pl: a pointer to a &struct phylink returned from phylink_create()
1519 * @ifr: a pointer to a &struct ifreq for socket ioctls
1520 * @cmd: ioctl cmd to execute
1522 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1523 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1525 * Returns: zero on success or negative error code.
1528 * read register from the current PHY.
1530 * read register from the specified PHY.
1532 * set a register on the specified PHY.
1534 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1536 struct mii_ioctl_data *mii = if_mii(ifr);
1542 /* PHYs only exist for MLO_AN_PHY and SGMII */
1545 mii->phy_id = pl->phydev->mdio.addr;
1549 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1557 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1562 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1572 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1580 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1592 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1594 static int phylink_sfp_module_insert(void *upstream,
1595 const struct sfp_eeprom_id *id)
1597 struct phylink *pl = upstream;
1598 __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
1599 struct phylink_link_state config;
1600 phy_interface_t iface;
1607 sfp_parse_support(pl->sfp_bus, id, support);
1608 port = sfp_parse_port(pl->sfp_bus, id, support);
1610 memset(&config, 0, sizeof(config));
1611 linkmode_copy(config.advertising, support);
1612 config.interface = PHY_INTERFACE_MODE_NA;
1613 config.speed = SPEED_UNKNOWN;
1614 config.duplex = DUPLEX_UNKNOWN;
1615 config.pause = MLO_PAUSE_AN;
1616 config.an_enabled = pl->link_config.an_enabled;
1618 /* Ignore errors if we're expecting a PHY to attach later */
1619 ret = phylink_validate(pl, support, &config);
1621 netdev_err(pl->netdev, "validation with support %*pb failed: %d\n",
1622 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1626 iface = sfp_select_interface(pl->sfp_bus, id, config.advertising);
1627 if (iface == PHY_INTERFACE_MODE_NA) {
1628 netdev_err(pl->netdev,
1629 "selection of interface failed, advertisement %*pb\n",
1630 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1634 config.interface = iface;
1635 ret = phylink_validate(pl, support, &config);
1637 netdev_err(pl->netdev, "validation of %s/%s with support %*pb failed: %d\n",
1638 phylink_an_mode_str(MLO_AN_INBAND),
1639 phy_modes(config.interface),
1640 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1644 netdev_dbg(pl->netdev, "requesting link mode %s/%s with support %*pb\n",
1645 phylink_an_mode_str(MLO_AN_INBAND),
1646 phy_modes(config.interface),
1647 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1649 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1652 changed = !bitmap_equal(pl->supported, support,
1653 __ETHTOOL_LINK_MODE_MASK_NBITS);
1655 linkmode_copy(pl->supported, support);
1656 linkmode_copy(pl->link_config.advertising, config.advertising);
1659 if (pl->link_an_mode != MLO_AN_INBAND ||
1660 pl->link_config.interface != config.interface) {
1661 pl->link_config.interface = config.interface;
1662 pl->link_an_mode = MLO_AN_INBAND;
1666 netdev_info(pl->netdev, "switched to %s/%s link mode\n",
1667 phylink_an_mode_str(MLO_AN_INBAND),
1668 phy_modes(config.interface));
1671 pl->link_port = port;
1673 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1674 &pl->phylink_disable_state))
1675 phylink_mac_config(pl, &pl->link_config);
1680 static void phylink_sfp_link_down(void *upstream)
1682 struct phylink *pl = upstream;
1686 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
1689 static void phylink_sfp_link_up(void *upstream)
1691 struct phylink *pl = upstream;
1695 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1696 phylink_run_resolve(pl);
1699 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1701 struct phylink *pl = upstream;
1703 return __phylink_connect_phy(upstream, phy, pl->link_config.interface);
1706 static void phylink_sfp_disconnect_phy(void *upstream)
1708 phylink_disconnect_phy(upstream);
1711 static const struct sfp_upstream_ops sfp_phylink_ops = {
1712 .module_insert = phylink_sfp_module_insert,
1713 .link_up = phylink_sfp_link_up,
1714 .link_down = phylink_sfp_link_down,
1715 .connect_phy = phylink_sfp_connect_phy,
1716 .disconnect_phy = phylink_sfp_disconnect_phy,
1719 /* Helpers for MAC drivers */
1722 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1723 * @state: a pointer to a &struct phylink_link_state
1725 * Inspect the interface mode, advertising mask or forced speed and
1726 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1727 * the interface mode to suit. @state->interface is appropriately
1728 * updated, and the advertising mask has the "other" baseX_Full flag
1731 void phylink_helper_basex_speed(struct phylink_link_state *state)
1733 if (phy_interface_mode_is_8023z(state->interface)) {
1734 bool want_2500 = state->an_enabled ?
1735 phylink_test(state->advertising, 2500baseX_Full) :
1736 state->speed == SPEED_2500;
1739 phylink_clear(state->advertising, 1000baseX_Full);
1740 state->interface = PHY_INTERFACE_MODE_2500BASEX;
1742 phylink_clear(state->advertising, 2500baseX_Full);
1743 state->interface = PHY_INTERFACE_MODE_1000BASEX;
1747 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
1749 MODULE_LICENSE("GPL");