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 cfg_link_an_mode; /* MLO_AN_xxx */
53 u8 link_port; /* The current non-phy ethtool port */
54 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
56 /* The link configuration settings */
57 struct phylink_link_state link_config;
59 /* The current settings */
60 phy_interface_t cur_interface;
62 struct gpio_desc *link_gpio;
63 unsigned int link_irq;
64 struct timer_list link_poll;
65 void (*get_fixed_state)(struct net_device *dev,
66 struct phylink_link_state *s);
68 struct mutex state_mutex;
69 struct phylink_link_state phy_state;
70 struct work_struct resolve;
72 bool mac_link_dropped;
74 struct sfp_bus *sfp_bus;
75 bool sfp_may_have_phy;
76 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
80 #define phylink_printk(level, pl, fmt, ...) \
82 if ((pl)->config->type == PHYLINK_NETDEV) \
83 netdev_printk(level, (pl)->netdev, fmt, ##__VA_ARGS__); \
84 else if ((pl)->config->type == PHYLINK_DEV) \
85 dev_printk(level, (pl)->dev, fmt, ##__VA_ARGS__); \
88 #define phylink_err(pl, fmt, ...) \
89 phylink_printk(KERN_ERR, pl, fmt, ##__VA_ARGS__)
90 #define phylink_warn(pl, fmt, ...) \
91 phylink_printk(KERN_WARNING, pl, fmt, ##__VA_ARGS__)
92 #define phylink_info(pl, fmt, ...) \
93 phylink_printk(KERN_INFO, pl, fmt, ##__VA_ARGS__)
94 #if defined(CONFIG_DYNAMIC_DEBUG)
95 #define phylink_dbg(pl, fmt, ...) \
97 if ((pl)->config->type == PHYLINK_NETDEV) \
98 netdev_dbg((pl)->netdev, fmt, ##__VA_ARGS__); \
99 else if ((pl)->config->type == PHYLINK_DEV) \
100 dev_dbg((pl)->dev, fmt, ##__VA_ARGS__); \
103 #define phylink_dbg(pl, fmt, ...) \
104 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__)
106 #define phylink_dbg(pl, fmt, ...) \
109 phylink_printk(KERN_DEBUG, pl, fmt, ##__VA_ARGS__); \
114 * phylink_set_port_modes() - set the port type modes in the ethtool mask
115 * @mask: ethtool link mode mask
117 * Sets all the port type modes in the ethtool mask. MAC drivers should
118 * use this in their 'validate' callback.
120 void phylink_set_port_modes(unsigned long *mask)
122 phylink_set(mask, TP);
123 phylink_set(mask, AUI);
124 phylink_set(mask, MII);
125 phylink_set(mask, FIBRE);
126 phylink_set(mask, BNC);
127 phylink_set(mask, Backplane);
129 EXPORT_SYMBOL_GPL(phylink_set_port_modes);
131 static int phylink_is_empty_linkmode(const unsigned long *linkmode)
133 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp) = { 0, };
135 phylink_set_port_modes(tmp);
136 phylink_set(tmp, Autoneg);
137 phylink_set(tmp, Pause);
138 phylink_set(tmp, Asym_Pause);
140 return linkmode_subset(linkmode, tmp);
143 static const char *phylink_an_mode_str(unsigned int mode)
145 static const char *modestr[] = {
146 [MLO_AN_PHY] = "phy",
147 [MLO_AN_FIXED] = "fixed",
148 [MLO_AN_INBAND] = "inband",
151 return mode < ARRAY_SIZE(modestr) ? modestr[mode] : "unknown";
154 static int phylink_validate(struct phylink *pl, unsigned long *supported,
155 struct phylink_link_state *state)
157 pl->ops->validate(pl->config, supported, state);
159 return phylink_is_empty_linkmode(supported) ? -EINVAL : 0;
162 static int phylink_parse_fixedlink(struct phylink *pl,
163 struct fwnode_handle *fwnode)
165 struct fwnode_handle *fixed_node;
166 const struct phy_setting *s;
167 struct gpio_desc *desc;
171 fixed_node = fwnode_get_named_child_node(fwnode, "fixed-link");
173 ret = fwnode_property_read_u32(fixed_node, "speed", &speed);
175 pl->link_config.speed = speed;
176 pl->link_config.duplex = DUPLEX_HALF;
178 if (fwnode_property_read_bool(fixed_node, "full-duplex"))
179 pl->link_config.duplex = DUPLEX_FULL;
181 /* We treat the "pause" and "asym-pause" terminology as
182 * defining the link partner's ability. */
183 if (fwnode_property_read_bool(fixed_node, "pause"))
184 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
185 pl->link_config.lp_advertising);
186 if (fwnode_property_read_bool(fixed_node, "asym-pause"))
187 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
188 pl->link_config.lp_advertising);
191 desc = fwnode_gpiod_get_index(fixed_node, "link", 0,
195 pl->link_gpio = desc;
196 else if (desc == ERR_PTR(-EPROBE_DEFER))
199 fwnode_handle_put(fixed_node);
206 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
208 if (ret != ARRAY_SIZE(prop)) {
209 phylink_err(pl, "broken fixed-link?\n");
213 ret = fwnode_property_read_u32_array(fwnode, "fixed-link",
214 prop, ARRAY_SIZE(prop));
216 pl->link_config.duplex = prop[1] ?
217 DUPLEX_FULL : DUPLEX_HALF;
218 pl->link_config.speed = prop[2];
220 __set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
221 pl->link_config.lp_advertising);
223 __set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
224 pl->link_config.lp_advertising);
228 if (pl->link_config.speed > SPEED_1000 &&
229 pl->link_config.duplex != DUPLEX_FULL)
230 phylink_warn(pl, "fixed link specifies half duplex for %dMbps link?\n",
231 pl->link_config.speed);
233 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
234 linkmode_copy(pl->link_config.advertising, pl->supported);
235 phylink_validate(pl, pl->supported, &pl->link_config);
237 s = phy_lookup_setting(pl->link_config.speed, pl->link_config.duplex,
238 pl->supported, true);
239 linkmode_zero(pl->supported);
240 phylink_set(pl->supported, MII);
241 phylink_set(pl->supported, Pause);
242 phylink_set(pl->supported, Asym_Pause);
244 __set_bit(s->bit, pl->supported);
246 phylink_warn(pl, "fixed link %s duplex %dMbps not recognised\n",
247 pl->link_config.duplex == DUPLEX_FULL ? "full" : "half",
248 pl->link_config.speed);
251 linkmode_and(pl->link_config.advertising, pl->link_config.advertising,
254 pl->link_config.link = 1;
255 pl->link_config.an_complete = 1;
260 static int phylink_parse_mode(struct phylink *pl, struct fwnode_handle *fwnode)
262 struct fwnode_handle *dn;
265 dn = fwnode_get_named_child_node(fwnode, "fixed-link");
266 if (dn || fwnode_property_present(fwnode, "fixed-link"))
267 pl->cfg_link_an_mode = MLO_AN_FIXED;
268 fwnode_handle_put(dn);
270 if (fwnode_property_read_string(fwnode, "managed", &managed) == 0 &&
271 strcmp(managed, "in-band-status") == 0) {
272 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
274 "can't use both fixed-link and in-band-status\n");
278 linkmode_zero(pl->supported);
279 phylink_set(pl->supported, MII);
280 phylink_set(pl->supported, Autoneg);
281 phylink_set(pl->supported, Asym_Pause);
282 phylink_set(pl->supported, Pause);
283 pl->link_config.an_enabled = true;
284 pl->cfg_link_an_mode = MLO_AN_INBAND;
286 switch (pl->link_config.interface) {
287 case PHY_INTERFACE_MODE_SGMII:
288 case PHY_INTERFACE_MODE_QSGMII:
289 phylink_set(pl->supported, 10baseT_Half);
290 phylink_set(pl->supported, 10baseT_Full);
291 phylink_set(pl->supported, 100baseT_Half);
292 phylink_set(pl->supported, 100baseT_Full);
293 phylink_set(pl->supported, 1000baseT_Half);
294 phylink_set(pl->supported, 1000baseT_Full);
297 case PHY_INTERFACE_MODE_1000BASEX:
298 phylink_set(pl->supported, 1000baseX_Full);
301 case PHY_INTERFACE_MODE_2500BASEX:
302 phylink_set(pl->supported, 2500baseX_Full);
305 case PHY_INTERFACE_MODE_USXGMII:
306 case PHY_INTERFACE_MODE_10GKR:
307 case PHY_INTERFACE_MODE_10GBASER:
308 phylink_set(pl->supported, 10baseT_Half);
309 phylink_set(pl->supported, 10baseT_Full);
310 phylink_set(pl->supported, 100baseT_Half);
311 phylink_set(pl->supported, 100baseT_Full);
312 phylink_set(pl->supported, 1000baseT_Half);
313 phylink_set(pl->supported, 1000baseT_Full);
314 phylink_set(pl->supported, 1000baseX_Full);
315 phylink_set(pl->supported, 1000baseKX_Full);
316 phylink_set(pl->supported, 2500baseT_Full);
317 phylink_set(pl->supported, 2500baseX_Full);
318 phylink_set(pl->supported, 5000baseT_Full);
319 phylink_set(pl->supported, 10000baseT_Full);
320 phylink_set(pl->supported, 10000baseKR_Full);
321 phylink_set(pl->supported, 10000baseKX4_Full);
322 phylink_set(pl->supported, 10000baseCR_Full);
323 phylink_set(pl->supported, 10000baseSR_Full);
324 phylink_set(pl->supported, 10000baseLR_Full);
325 phylink_set(pl->supported, 10000baseLRM_Full);
326 phylink_set(pl->supported, 10000baseER_Full);
329 case PHY_INTERFACE_MODE_XLGMII:
330 phylink_set(pl->supported, 25000baseCR_Full);
331 phylink_set(pl->supported, 25000baseKR_Full);
332 phylink_set(pl->supported, 25000baseSR_Full);
333 phylink_set(pl->supported, 40000baseKR4_Full);
334 phylink_set(pl->supported, 40000baseCR4_Full);
335 phylink_set(pl->supported, 40000baseSR4_Full);
336 phylink_set(pl->supported, 40000baseLR4_Full);
337 phylink_set(pl->supported, 50000baseCR2_Full);
338 phylink_set(pl->supported, 50000baseKR2_Full);
339 phylink_set(pl->supported, 50000baseSR2_Full);
340 phylink_set(pl->supported, 50000baseKR_Full);
341 phylink_set(pl->supported, 50000baseSR_Full);
342 phylink_set(pl->supported, 50000baseCR_Full);
343 phylink_set(pl->supported, 50000baseLR_ER_FR_Full);
344 phylink_set(pl->supported, 50000baseDR_Full);
345 phylink_set(pl->supported, 100000baseKR4_Full);
346 phylink_set(pl->supported, 100000baseSR4_Full);
347 phylink_set(pl->supported, 100000baseCR4_Full);
348 phylink_set(pl->supported, 100000baseLR4_ER4_Full);
349 phylink_set(pl->supported, 100000baseKR2_Full);
350 phylink_set(pl->supported, 100000baseSR2_Full);
351 phylink_set(pl->supported, 100000baseCR2_Full);
352 phylink_set(pl->supported, 100000baseLR2_ER2_FR2_Full);
353 phylink_set(pl->supported, 100000baseDR2_Full);
358 "incorrect link mode %s for in-band status\n",
359 phy_modes(pl->link_config.interface));
363 linkmode_copy(pl->link_config.advertising, pl->supported);
365 if (phylink_validate(pl, pl->supported, &pl->link_config)) {
367 "failed to validate link configuration for in-band status\n");
371 /* Check if MAC/PCS also supports Autoneg. */
372 pl->link_config.an_enabled = phylink_test(pl->supported, Autoneg);
378 static void phylink_apply_manual_flow(struct phylink *pl,
379 struct phylink_link_state *state)
381 /* If autoneg is disabled, pause AN is also disabled */
382 if (!state->an_enabled)
383 state->pause &= ~MLO_PAUSE_AN;
385 /* Manual configuration of pause modes */
386 if (!(pl->link_config.pause & MLO_PAUSE_AN))
387 state->pause = pl->link_config.pause;
390 static void phylink_resolve_flow(struct phylink_link_state *state)
392 bool tx_pause, rx_pause;
394 state->pause = MLO_PAUSE_NONE;
395 if (state->duplex == DUPLEX_FULL) {
396 linkmode_resolve_pause(state->advertising,
397 state->lp_advertising,
398 &tx_pause, &rx_pause);
400 state->pause |= MLO_PAUSE_TX;
402 state->pause |= MLO_PAUSE_RX;
406 static void phylink_mac_config(struct phylink *pl,
407 const struct phylink_link_state *state)
410 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
411 __func__, phylink_an_mode_str(pl->cur_link_an_mode),
412 phy_modes(state->interface),
413 phy_speed_to_str(state->speed),
414 phy_duplex_to_str(state->duplex),
415 __ETHTOOL_LINK_MODE_MASK_NBITS, state->advertising,
416 state->pause, state->link, state->an_enabled);
418 pl->ops->mac_config(pl->config, pl->cur_link_an_mode, state);
421 static void phylink_mac_config_up(struct phylink *pl,
422 const struct phylink_link_state *state)
425 phylink_mac_config(pl, state);
428 static void phylink_mac_an_restart(struct phylink *pl)
430 if (pl->link_config.an_enabled &&
431 phy_interface_mode_is_8023z(pl->link_config.interface))
432 pl->ops->mac_an_restart(pl->config);
435 static void phylink_mac_pcs_get_state(struct phylink *pl,
436 struct phylink_link_state *state)
438 linkmode_copy(state->advertising, pl->link_config.advertising);
439 linkmode_zero(state->lp_advertising);
440 state->interface = pl->link_config.interface;
441 state->an_enabled = pl->link_config.an_enabled;
442 state->speed = SPEED_UNKNOWN;
443 state->duplex = DUPLEX_UNKNOWN;
444 state->pause = MLO_PAUSE_NONE;
445 state->an_complete = 0;
448 pl->ops->mac_pcs_get_state(pl->config, state);
451 /* The fixed state is... fixed except for the link state,
452 * which may be determined by a GPIO or a callback.
454 static void phylink_get_fixed_state(struct phylink *pl,
455 struct phylink_link_state *state)
457 *state = pl->link_config;
458 if (pl->get_fixed_state)
459 pl->get_fixed_state(pl->netdev, state);
460 else if (pl->link_gpio)
461 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
463 phylink_resolve_flow(state);
466 static void phylink_mac_initial_config(struct phylink *pl)
468 struct phylink_link_state link_state;
470 switch (pl->cur_link_an_mode) {
472 link_state = pl->phy_state;
476 phylink_get_fixed_state(pl, &link_state);
480 link_state = pl->link_config;
481 if (link_state.interface == PHY_INTERFACE_MODE_SGMII)
482 link_state.pause = MLO_PAUSE_NONE;
485 default: /* can't happen */
489 link_state.link = false;
491 phylink_apply_manual_flow(pl, &link_state);
492 phylink_mac_config(pl, &link_state);
495 static const char *phylink_pause_to_str(int pause)
497 switch (pause & MLO_PAUSE_TXRX_MASK) {
498 case MLO_PAUSE_TX | MLO_PAUSE_RX:
509 static void phylink_mac_link_up(struct phylink *pl,
510 struct phylink_link_state link_state)
512 struct net_device *ndev = pl->netdev;
514 pl->cur_interface = link_state.interface;
515 pl->ops->mac_link_up(pl->config, pl->phydev,
516 pl->cur_link_an_mode, pl->cur_interface,
517 link_state.speed, link_state.duplex,
518 !!(link_state.pause & MLO_PAUSE_TX),
519 !!(link_state.pause & MLO_PAUSE_RX));
522 netif_carrier_on(ndev);
525 "Link is Up - %s/%s - flow control %s\n",
526 phy_speed_to_str(link_state.speed),
527 phy_duplex_to_str(link_state.duplex),
528 phylink_pause_to_str(link_state.pause));
531 static void phylink_mac_link_down(struct phylink *pl)
533 struct net_device *ndev = pl->netdev;
536 netif_carrier_off(ndev);
537 pl->ops->mac_link_down(pl->config, pl->cur_link_an_mode,
539 phylink_info(pl, "Link is Down\n");
542 static void phylink_resolve(struct work_struct *w)
544 struct phylink *pl = container_of(w, struct phylink, resolve);
545 struct phylink_link_state link_state;
546 struct net_device *ndev = pl->netdev;
549 mutex_lock(&pl->state_mutex);
550 if (pl->phylink_disable_state) {
551 pl->mac_link_dropped = false;
552 link_state.link = false;
553 } else if (pl->mac_link_dropped) {
554 link_state.link = false;
556 switch (pl->cur_link_an_mode) {
558 link_state = pl->phy_state;
559 phylink_apply_manual_flow(pl, &link_state);
560 phylink_mac_config_up(pl, &link_state);
564 phylink_get_fixed_state(pl, &link_state);
565 phylink_mac_config_up(pl, &link_state);
569 phylink_mac_pcs_get_state(pl, &link_state);
571 /* If we have a phy, the "up" state is the union of
572 * both the PHY and the MAC */
574 link_state.link &= pl->phy_state.link;
576 /* Only update if the PHY link is up */
577 if (pl->phydev && pl->phy_state.link) {
578 link_state.interface = pl->phy_state.interface;
580 /* If we have a PHY, we need to update with
581 * the PHY flow control bits. */
582 link_state.pause = pl->phy_state.pause;
583 phylink_apply_manual_flow(pl, &link_state);
584 phylink_mac_config(pl, &link_state);
586 phylink_apply_manual_flow(pl, &link_state);
593 link_changed = (link_state.link != netif_carrier_ok(ndev));
595 link_changed = (link_state.link != pl->old_link_state);
598 pl->old_link_state = link_state.link;
599 if (!link_state.link)
600 phylink_mac_link_down(pl);
602 phylink_mac_link_up(pl, link_state);
604 if (!link_state.link && pl->mac_link_dropped) {
605 pl->mac_link_dropped = false;
606 queue_work(system_power_efficient_wq, &pl->resolve);
608 mutex_unlock(&pl->state_mutex);
611 static void phylink_run_resolve(struct phylink *pl)
613 if (!pl->phylink_disable_state)
614 queue_work(system_power_efficient_wq, &pl->resolve);
617 static void phylink_run_resolve_and_disable(struct phylink *pl, int bit)
619 unsigned long state = pl->phylink_disable_state;
621 set_bit(bit, &pl->phylink_disable_state);
623 queue_work(system_power_efficient_wq, &pl->resolve);
624 flush_work(&pl->resolve);
628 static void phylink_fixed_poll(struct timer_list *t)
630 struct phylink *pl = container_of(t, struct phylink, link_poll);
632 mod_timer(t, jiffies + HZ);
634 phylink_run_resolve(pl);
637 static const struct sfp_upstream_ops sfp_phylink_ops;
639 static int phylink_register_sfp(struct phylink *pl,
640 struct fwnode_handle *fwnode)
648 bus = sfp_bus_find_fwnode(fwnode);
651 phylink_err(pl, "unable to attach SFP bus: %d\n", ret);
657 ret = sfp_bus_add_upstream(bus, pl, &sfp_phylink_ops);
664 * phylink_create() - create a phylink instance
665 * @config: a pointer to the target &struct phylink_config
666 * @fwnode: a pointer to a &struct fwnode_handle describing the network
668 * @iface: the desired link mode defined by &typedef phy_interface_t
669 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
671 * Create a new phylink instance, and parse the link parameters found in @np.
672 * This will parse in-band modes, fixed-link or SFP configuration.
674 * Note: the rtnl lock must not be held when calling this function.
676 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
677 * must use IS_ERR() to check for errors from this function.
679 struct phylink *phylink_create(struct phylink_config *config,
680 struct fwnode_handle *fwnode,
681 phy_interface_t iface,
682 const struct phylink_mac_ops *ops)
687 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
689 return ERR_PTR(-ENOMEM);
691 mutex_init(&pl->state_mutex);
692 INIT_WORK(&pl->resolve, phylink_resolve);
695 if (config->type == PHYLINK_NETDEV) {
696 pl->netdev = to_net_dev(config->dev);
697 } else if (config->type == PHYLINK_DEV) {
698 pl->dev = config->dev;
701 return ERR_PTR(-EINVAL);
704 pl->phy_state.interface = iface;
705 pl->link_interface = iface;
706 if (iface == PHY_INTERFACE_MODE_MOCA)
707 pl->link_port = PORT_BNC;
709 pl->link_port = PORT_MII;
710 pl->link_config.interface = iface;
711 pl->link_config.pause = MLO_PAUSE_AN;
712 pl->link_config.speed = SPEED_UNKNOWN;
713 pl->link_config.duplex = DUPLEX_UNKNOWN;
714 pl->link_config.an_enabled = true;
716 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
717 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
719 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
720 linkmode_copy(pl->link_config.advertising, pl->supported);
721 phylink_validate(pl, pl->supported, &pl->link_config);
723 ret = phylink_parse_mode(pl, fwnode);
729 if (pl->cfg_link_an_mode == MLO_AN_FIXED) {
730 ret = phylink_parse_fixedlink(pl, fwnode);
737 pl->cur_link_an_mode = pl->cfg_link_an_mode;
739 ret = phylink_register_sfp(pl, fwnode);
747 EXPORT_SYMBOL_GPL(phylink_create);
750 * phylink_destroy() - cleanup and destroy the phylink instance
751 * @pl: a pointer to a &struct phylink returned from phylink_create()
753 * Destroy a phylink instance. Any PHY that has been attached must have been
754 * cleaned up via phylink_disconnect_phy() prior to calling this function.
756 * Note: the rtnl lock must not be held when calling this function.
758 void phylink_destroy(struct phylink *pl)
760 sfp_bus_del_upstream(pl->sfp_bus);
762 gpiod_put(pl->link_gpio);
764 cancel_work_sync(&pl->resolve);
767 EXPORT_SYMBOL_GPL(phylink_destroy);
769 static void phylink_phy_change(struct phy_device *phydev, bool up,
772 struct phylink *pl = phydev->phylink;
773 bool tx_pause, rx_pause;
775 phy_get_pause(phydev, &tx_pause, &rx_pause);
777 mutex_lock(&pl->state_mutex);
778 pl->phy_state.speed = phydev->speed;
779 pl->phy_state.duplex = phydev->duplex;
780 pl->phy_state.pause = MLO_PAUSE_NONE;
782 pl->phy_state.pause |= MLO_PAUSE_TX;
784 pl->phy_state.pause |= MLO_PAUSE_RX;
785 pl->phy_state.interface = phydev->interface;
786 pl->phy_state.link = up;
787 mutex_unlock(&pl->state_mutex);
789 phylink_run_resolve(pl);
791 phylink_dbg(pl, "phy link %s %s/%s/%s\n", up ? "up" : "down",
792 phy_modes(phydev->interface),
793 phy_speed_to_str(phydev->speed),
794 phy_duplex_to_str(phydev->duplex));
797 static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
798 phy_interface_t interface)
800 struct phylink_link_state config;
801 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
806 * This is the new way of dealing with flow control for PHYs,
807 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
808 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
809 * using our validate call to the MAC, we rely upon the MAC
810 * clearing the bits from both supported and advertising fields.
812 phy_support_asym_pause(phy);
814 memset(&config, 0, sizeof(config));
815 linkmode_copy(supported, phy->supported);
816 linkmode_copy(config.advertising, phy->advertising);
818 /* Clause 45 PHYs switch their Serdes lane between several different
819 * modes, normally 10GBASE-R, SGMII. Some use 2500BASE-X for 2.5G
820 * speeds. We really need to know which interface modes the PHY and
821 * MAC supports to properly work out which linkmodes can be supported.
824 interface != PHY_INTERFACE_MODE_RXAUI &&
825 interface != PHY_INTERFACE_MODE_XAUI &&
826 interface != PHY_INTERFACE_MODE_USXGMII)
827 config.interface = PHY_INTERFACE_MODE_NA;
829 config.interface = interface;
831 ret = phylink_validate(pl, supported, &config);
833 phylink_warn(pl, "validation of %s with support %*pb and advertisement %*pb failed: %d\n",
834 phy_modes(config.interface),
835 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->supported,
836 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising,
842 phy->phy_link_change = phylink_phy_change;
844 irq_str = phy_attached_info_irq(phy);
846 "PHY [%s] driver [%s] (irq=%s)\n",
847 dev_name(&phy->mdio.dev), phy->drv->name, irq_str);
850 mutex_lock(&phy->lock);
851 mutex_lock(&pl->state_mutex);
853 pl->phy_state.interface = interface;
854 pl->phy_state.pause = MLO_PAUSE_NONE;
855 pl->phy_state.speed = SPEED_UNKNOWN;
856 pl->phy_state.duplex = DUPLEX_UNKNOWN;
857 linkmode_copy(pl->supported, supported);
858 linkmode_copy(pl->link_config.advertising, config.advertising);
860 /* Restrict the phy advertisement according to the MAC support. */
861 linkmode_copy(phy->advertising, config.advertising);
862 mutex_unlock(&pl->state_mutex);
863 mutex_unlock(&phy->lock);
866 "phy: setting supported %*pb advertising %*pb\n",
867 __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
868 __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
870 if (phy_interrupt_is_valid(phy))
871 phy_request_interrupt(phy);
876 static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
877 phy_interface_t interface)
879 if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
880 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
881 phy_interface_mode_is_8023z(interface))))
887 return phy_attach_direct(pl->netdev, phy, 0, interface);
891 * phylink_connect_phy() - connect a PHY to the phylink instance
892 * @pl: a pointer to a &struct phylink returned from phylink_create()
893 * @phy: a pointer to a &struct phy_device.
895 * Connect @phy to the phylink instance specified by @pl by calling
896 * phy_attach_direct(). Configure the @phy according to the MAC driver's
897 * capabilities, start the PHYLIB state machine and enable any interrupts
898 * that the PHY supports.
900 * This updates the phylink's ethtool supported and advertising link mode
903 * Returns 0 on success or a negative errno.
905 int phylink_connect_phy(struct phylink *pl, struct phy_device *phy)
909 /* Use PHY device/driver interface */
910 if (pl->link_interface == PHY_INTERFACE_MODE_NA) {
911 pl->link_interface = phy->interface;
912 pl->link_config.interface = pl->link_interface;
915 ret = phylink_attach_phy(pl, phy, pl->link_interface);
919 ret = phylink_bringup_phy(pl, phy, pl->link_config.interface);
925 EXPORT_SYMBOL_GPL(phylink_connect_phy);
928 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
929 * @pl: a pointer to a &struct phylink returned from phylink_create()
930 * @dn: a pointer to a &struct device_node.
931 * @flags: PHY-specific flags to communicate to the PHY device driver
933 * Connect the phy specified in the device node @dn to the phylink instance
934 * specified by @pl. Actions specified in phylink_connect_phy() will be
937 * Returns 0 on success or a negative errno.
939 int phylink_of_phy_connect(struct phylink *pl, struct device_node *dn,
942 struct device_node *phy_node;
943 struct phy_device *phy_dev;
946 /* Fixed links and 802.3z are handled without needing a PHY */
947 if (pl->cfg_link_an_mode == MLO_AN_FIXED ||
948 (pl->cfg_link_an_mode == MLO_AN_INBAND &&
949 phy_interface_mode_is_8023z(pl->link_interface)))
952 phy_node = of_parse_phandle(dn, "phy-handle", 0);
954 phy_node = of_parse_phandle(dn, "phy", 0);
956 phy_node = of_parse_phandle(dn, "phy-device", 0);
959 if (pl->cfg_link_an_mode == MLO_AN_PHY)
964 phy_dev = of_phy_find_device(phy_node);
965 /* We're done with the phy_node handle */
966 of_node_put(phy_node);
970 ret = phy_attach_direct(pl->netdev, phy_dev, flags,
975 ret = phylink_bringup_phy(pl, phy_dev, pl->link_config.interface);
981 EXPORT_SYMBOL_GPL(phylink_of_phy_connect);
984 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
986 * @pl: a pointer to a &struct phylink returned from phylink_create()
988 * Disconnect any current PHY from the phylink instance described by @pl.
990 void phylink_disconnect_phy(struct phylink *pl)
992 struct phy_device *phy;
998 mutex_lock(&phy->lock);
999 mutex_lock(&pl->state_mutex);
1001 mutex_unlock(&pl->state_mutex);
1002 mutex_unlock(&phy->lock);
1003 flush_work(&pl->resolve);
1005 phy_disconnect(phy);
1008 EXPORT_SYMBOL_GPL(phylink_disconnect_phy);
1011 * phylink_fixed_state_cb() - allow setting a fixed link callback
1012 * @pl: a pointer to a &struct phylink returned from phylink_create()
1013 * @cb: callback to execute to determine the fixed link state.
1015 * The MAC driver should call this driver when the state of its link
1016 * can be determined through e.g: an out of band MMIO register.
1018 int phylink_fixed_state_cb(struct phylink *pl,
1019 void (*cb)(struct net_device *dev,
1020 struct phylink_link_state *state))
1022 /* It does not make sense to let the link be overriden unless we use
1025 if (pl->cfg_link_an_mode != MLO_AN_FIXED)
1028 mutex_lock(&pl->state_mutex);
1029 pl->get_fixed_state = cb;
1030 mutex_unlock(&pl->state_mutex);
1034 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb);
1037 * phylink_mac_change() - notify phylink of a change in MAC state
1038 * @pl: a pointer to a &struct phylink returned from phylink_create()
1039 * @up: indicates whether the link is currently up.
1041 * The MAC driver should call this driver when the state of its link
1042 * changes (eg, link failure, new negotiation results, etc.)
1044 void phylink_mac_change(struct phylink *pl, bool up)
1047 pl->mac_link_dropped = true;
1048 phylink_run_resolve(pl);
1049 phylink_dbg(pl, "mac link %s\n", up ? "up" : "down");
1051 EXPORT_SYMBOL_GPL(phylink_mac_change);
1053 static irqreturn_t phylink_link_handler(int irq, void *data)
1055 struct phylink *pl = data;
1057 phylink_run_resolve(pl);
1063 * phylink_start() - start a phylink instance
1064 * @pl: a pointer to a &struct phylink returned from phylink_create()
1066 * Start the phylink instance specified by @pl, configuring the MAC for the
1067 * desired link mode(s) and negotiation style. This should be called from the
1068 * network device driver's &struct net_device_ops ndo_open() method.
1070 void phylink_start(struct phylink *pl)
1074 phylink_info(pl, "configuring for %s/%s link mode\n",
1075 phylink_an_mode_str(pl->cur_link_an_mode),
1076 phy_modes(pl->link_config.interface));
1078 /* Always set the carrier off */
1080 netif_carrier_off(pl->netdev);
1082 /* Apply the link configuration to the MAC when starting. This allows
1083 * a fixed-link to start with the correct parameters, and also
1084 * ensures that we set the appropriate advertisement for Serdes links.
1086 phylink_mac_initial_config(pl);
1088 /* Restart autonegotiation if using 802.3z to ensure that the link
1089 * parameters are properly negotiated. This is necessary for DSA
1090 * switches using 802.3z negotiation to ensure they see our modes.
1092 phylink_mac_an_restart(pl);
1094 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
1095 phylink_run_resolve(pl);
1097 if (pl->cfg_link_an_mode == MLO_AN_FIXED && pl->link_gpio) {
1098 int irq = gpiod_to_irq(pl->link_gpio);
1101 if (!request_irq(irq, phylink_link_handler,
1102 IRQF_TRIGGER_RISING |
1103 IRQF_TRIGGER_FALLING,
1110 mod_timer(&pl->link_poll, jiffies + HZ);
1112 if ((pl->cfg_link_an_mode == MLO_AN_FIXED && pl->get_fixed_state) ||
1113 pl->config->pcs_poll)
1114 mod_timer(&pl->link_poll, jiffies + HZ);
1116 phy_start(pl->phydev);
1118 sfp_upstream_start(pl->sfp_bus);
1120 EXPORT_SYMBOL_GPL(phylink_start);
1123 * phylink_stop() - stop a phylink instance
1124 * @pl: a pointer to a &struct phylink returned from phylink_create()
1126 * Stop the phylink instance specified by @pl. This should be called from the
1127 * network device driver's &struct net_device_ops ndo_stop() method. The
1128 * network device's carrier state should not be changed prior to calling this
1131 void phylink_stop(struct phylink *pl)
1136 sfp_upstream_stop(pl->sfp_bus);
1138 phy_stop(pl->phydev);
1139 del_timer_sync(&pl->link_poll);
1141 free_irq(pl->link_irq, pl);
1145 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_STOPPED);
1147 EXPORT_SYMBOL_GPL(phylink_stop);
1150 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
1151 * @pl: a pointer to a &struct phylink returned from phylink_create()
1152 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
1154 * Read the wake on lan parameters from the PHY attached to the phylink
1155 * instance specified by @pl. If no PHY is currently attached, report no
1156 * support for wake on lan.
1158 void phylink_ethtool_get_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1166 phy_ethtool_get_wol(pl->phydev, wol);
1168 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol);
1171 * phylink_ethtool_set_wol() - set wake on lan parameters
1172 * @pl: a pointer to a &struct phylink returned from phylink_create()
1173 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
1175 * Set the wake on lan parameters for the PHY attached to the phylink
1176 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
1179 * Returns zero on success or negative errno code.
1181 int phylink_ethtool_set_wol(struct phylink *pl, struct ethtool_wolinfo *wol)
1183 int ret = -EOPNOTSUPP;
1188 ret = phy_ethtool_set_wol(pl->phydev, wol);
1192 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol);
1194 static void phylink_merge_link_mode(unsigned long *dst, const unsigned long *b)
1196 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask);
1198 linkmode_zero(mask);
1199 phylink_set_port_modes(mask);
1201 linkmode_and(dst, dst, mask);
1202 linkmode_or(dst, dst, b);
1205 static void phylink_get_ksettings(const struct phylink_link_state *state,
1206 struct ethtool_link_ksettings *kset)
1208 phylink_merge_link_mode(kset->link_modes.advertising, state->advertising);
1209 linkmode_copy(kset->link_modes.lp_advertising, state->lp_advertising);
1210 kset->base.speed = state->speed;
1211 kset->base.duplex = state->duplex;
1212 kset->base.autoneg = state->an_enabled ? AUTONEG_ENABLE :
1217 * phylink_ethtool_ksettings_get() - get the current link settings
1218 * @pl: a pointer to a &struct phylink returned from phylink_create()
1219 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1221 * Read the current link settings for the phylink instance specified by @pl.
1222 * This will be the link settings read from the MAC, PHY or fixed link
1223 * settings depending on the current negotiation mode.
1225 int phylink_ethtool_ksettings_get(struct phylink *pl,
1226 struct ethtool_link_ksettings *kset)
1228 struct phylink_link_state link_state;
1233 phy_ethtool_ksettings_get(pl->phydev, kset);
1235 kset->base.port = pl->link_port;
1238 linkmode_copy(kset->link_modes.supported, pl->supported);
1240 switch (pl->cur_link_an_mode) {
1242 /* We are using fixed settings. Report these as the
1243 * current link settings - and note that these also
1244 * represent the supported speeds/duplex/pause modes.
1246 phylink_get_fixed_state(pl, &link_state);
1247 phylink_get_ksettings(&link_state, kset);
1251 /* If there is a phy attached, then use the reported
1252 * settings from the phy with no modification.
1257 phylink_mac_pcs_get_state(pl, &link_state);
1259 /* The MAC is reporting the link results from its own PCS
1260 * layer via in-band status. Report these as the current
1263 phylink_get_ksettings(&link_state, kset);
1269 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get);
1272 * phylink_ethtool_ksettings_set() - set the link settings
1273 * @pl: a pointer to a &struct phylink returned from phylink_create()
1274 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1276 int phylink_ethtool_ksettings_set(struct phylink *pl,
1277 const struct ethtool_link_ksettings *kset)
1279 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1280 struct ethtool_link_ksettings our_kset;
1281 struct phylink_link_state config;
1286 if (kset->base.autoneg != AUTONEG_DISABLE &&
1287 kset->base.autoneg != AUTONEG_ENABLE)
1290 linkmode_copy(support, pl->supported);
1291 config = pl->link_config;
1293 /* Mask out unsupported advertisements */
1294 linkmode_and(config.advertising, kset->link_modes.advertising,
1297 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1298 if (kset->base.autoneg == AUTONEG_DISABLE) {
1299 const struct phy_setting *s;
1301 /* Autonegotiation disabled, select a suitable speed and
1304 s = phy_lookup_setting(kset->base.speed, kset->base.duplex,
1309 /* If we have a fixed link (as specified by firmware), refuse
1310 * to change link parameters.
1312 if (pl->cur_link_an_mode == MLO_AN_FIXED &&
1313 (s->speed != pl->link_config.speed ||
1314 s->duplex != pl->link_config.duplex))
1317 config.speed = s->speed;
1318 config.duplex = s->duplex;
1319 config.an_enabled = false;
1321 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1323 /* If we have a fixed link, refuse to enable autonegotiation */
1324 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1327 config.speed = SPEED_UNKNOWN;
1328 config.duplex = DUPLEX_UNKNOWN;
1329 config.an_enabled = true;
1331 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, config.advertising);
1335 /* If we have a PHY, we process the kset change via phylib.
1336 * phylib will call our link state function if the PHY
1337 * parameters have changed, which will trigger a resolve
1338 * and update the MAC configuration.
1341 linkmode_copy(our_kset.link_modes.advertising,
1342 config.advertising);
1343 our_kset.base.speed = config.speed;
1344 our_kset.base.duplex = config.duplex;
1346 ret = phy_ethtool_ksettings_set(pl->phydev, &our_kset);
1350 mutex_lock(&pl->state_mutex);
1351 /* Save the new configuration */
1352 linkmode_copy(pl->link_config.advertising,
1353 our_kset.link_modes.advertising);
1354 pl->link_config.interface = config.interface;
1355 pl->link_config.speed = our_kset.base.speed;
1356 pl->link_config.duplex = our_kset.base.duplex;
1357 pl->link_config.an_enabled = our_kset.base.autoneg !=
1359 mutex_unlock(&pl->state_mutex);
1361 /* For a fixed link, this isn't able to change any parameters,
1362 * which just leaves inband mode.
1364 if (phylink_validate(pl, support, &config))
1367 /* If autonegotiation is enabled, we must have an advertisement */
1368 if (config.an_enabled &&
1369 phylink_is_empty_linkmode(config.advertising))
1372 mutex_lock(&pl->state_mutex);
1373 linkmode_copy(pl->link_config.advertising, config.advertising);
1374 pl->link_config.interface = config.interface;
1375 pl->link_config.speed = config.speed;
1376 pl->link_config.duplex = config.duplex;
1377 pl->link_config.an_enabled = kset->base.autoneg !=
1380 if (pl->cur_link_an_mode == MLO_AN_INBAND &&
1381 !test_bit(PHYLINK_DISABLE_STOPPED,
1382 &pl->phylink_disable_state)) {
1383 /* If in 802.3z mode, this updates the advertisement.
1385 * If we are in SGMII mode without a PHY, there is no
1386 * advertisement; the only thing we have is the pause
1387 * modes which can only come from a PHY.
1389 phylink_mac_config(pl, &pl->link_config);
1390 phylink_mac_an_restart(pl);
1392 mutex_unlock(&pl->state_mutex);
1397 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set);
1400 * phylink_ethtool_nway_reset() - restart negotiation
1401 * @pl: a pointer to a &struct phylink returned from phylink_create()
1403 * Restart negotiation for the phylink instance specified by @pl. This will
1404 * cause any attached phy to restart negotiation with the link partner, and
1405 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1408 * Returns zero on success, or negative error code.
1410 int phylink_ethtool_nway_reset(struct phylink *pl)
1417 ret = phy_restart_aneg(pl->phydev);
1418 phylink_mac_an_restart(pl);
1422 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset);
1425 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1426 * @pl: a pointer to a &struct phylink returned from phylink_create()
1427 * @pause: a pointer to a &struct ethtool_pauseparam
1429 void phylink_ethtool_get_pauseparam(struct phylink *pl,
1430 struct ethtool_pauseparam *pause)
1434 pause->autoneg = !!(pl->link_config.pause & MLO_PAUSE_AN);
1435 pause->rx_pause = !!(pl->link_config.pause & MLO_PAUSE_RX);
1436 pause->tx_pause = !!(pl->link_config.pause & MLO_PAUSE_TX);
1438 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam);
1441 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1442 * @pl: a pointer to a &struct phylink returned from phylink_create()
1443 * @pause: a pointer to a &struct ethtool_pauseparam
1445 int phylink_ethtool_set_pauseparam(struct phylink *pl,
1446 struct ethtool_pauseparam *pause)
1448 struct phylink_link_state *config = &pl->link_config;
1452 if (pl->cur_link_an_mode == MLO_AN_FIXED)
1455 if (!phylink_test(pl->supported, Pause) &&
1456 !phylink_test(pl->supported, Asym_Pause))
1459 if (!phylink_test(pl->supported, Asym_Pause) &&
1460 !pause->autoneg && pause->rx_pause != pause->tx_pause)
1463 mutex_lock(&pl->state_mutex);
1466 config->pause |= MLO_PAUSE_AN;
1467 if (pause->rx_pause)
1468 config->pause |= MLO_PAUSE_RX;
1469 if (pause->tx_pause)
1470 config->pause |= MLO_PAUSE_TX;
1473 * See the comments for linkmode_set_pause(), wrt the deficiencies
1474 * with the current implementation. A solution to this issue would
1476 * ethtool Local device
1477 * rx tx Pause AsymDir
1482 * and then use the ethtool rx/tx enablement status to mask the
1483 * rx/tx pause resolution.
1485 linkmode_set_pause(config->advertising, pause->tx_pause,
1488 /* If we have a PHY, phylib will call our link state function if the
1489 * mode has changed, which will trigger a resolve and update the MAC
1493 phy_set_asym_pause(pl->phydev, pause->rx_pause,
1495 } else if (!test_bit(PHYLINK_DISABLE_STOPPED,
1496 &pl->phylink_disable_state)) {
1497 phylink_mac_config(pl, &pl->link_config);
1498 phylink_mac_an_restart(pl);
1500 mutex_unlock(&pl->state_mutex);
1504 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam);
1507 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1509 * @pl: a pointer to a &struct phylink returned from phylink_create().
1511 * Read the Energy Efficient Ethernet error counter from the PHY associated
1512 * with the phylink instance specified by @pl.
1514 * Returns positive error counter value, or negative error code.
1516 int phylink_get_eee_err(struct phylink *pl)
1523 ret = phy_get_eee_err(pl->phydev);
1527 EXPORT_SYMBOL_GPL(phylink_get_eee_err);
1530 * phylink_init_eee() - init and check the EEE features
1531 * @pl: a pointer to a &struct phylink returned from phylink_create()
1532 * @clk_stop_enable: allow PHY to stop receive clock
1534 * Must be called either with RTNL held or within mac_link_up()
1536 int phylink_init_eee(struct phylink *pl, bool clk_stop_enable)
1538 int ret = -EOPNOTSUPP;
1541 ret = phy_init_eee(pl->phydev, clk_stop_enable);
1545 EXPORT_SYMBOL_GPL(phylink_init_eee);
1548 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1549 * @pl: a pointer to a &struct phylink returned from phylink_create()
1550 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1552 int phylink_ethtool_get_eee(struct phylink *pl, struct ethtool_eee *eee)
1554 int ret = -EOPNOTSUPP;
1559 ret = phy_ethtool_get_eee(pl->phydev, eee);
1563 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee);
1566 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1567 * @pl: a pointer to a &struct phylink returned from phylink_create()
1568 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1570 int phylink_ethtool_set_eee(struct phylink *pl, struct ethtool_eee *eee)
1572 int ret = -EOPNOTSUPP;
1577 ret = phy_ethtool_set_eee(pl->phydev, eee);
1581 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee);
1583 /* This emulates MII registers for a fixed-mode phy operating as per the
1584 * passed in state. "aneg" defines if we report negotiation is possible.
1586 * FIXME: should deal with negotiation state too.
1588 static int phylink_mii_emul_read(unsigned int reg,
1589 struct phylink_link_state *state)
1591 struct fixed_phy_status fs;
1592 unsigned long *lpa = state->lp_advertising;
1595 fs.link = state->link;
1596 fs.speed = state->speed;
1597 fs.duplex = state->duplex;
1598 fs.pause = test_bit(ETHTOOL_LINK_MODE_Pause_BIT, lpa);
1599 fs.asym_pause = test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, lpa);
1601 val = swphy_read_reg(reg, &fs);
1602 if (reg == MII_BMSR) {
1603 if (!state->an_complete)
1604 val &= ~BMSR_ANEGCOMPLETE;
1609 static int phylink_phy_read(struct phylink *pl, unsigned int phy_id,
1612 struct phy_device *phydev = pl->phydev;
1615 if (mdio_phy_id_is_c45(phy_id)) {
1616 prtad = mdio_phy_id_prtad(phy_id);
1617 devad = mdio_phy_id_devad(phy_id);
1618 devad = MII_ADDR_C45 | devad << 16 | reg;
1619 } else if (phydev->is_c45) {
1625 devad = __ffs(phydev->c45_ids.devices_in_package);
1629 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1631 devad = MDIO_MMD_AN;
1632 if (reg == MII_ADVERTISE)
1633 reg = MDIO_AN_ADVERTISE;
1641 devad = MII_ADDR_C45 | devad << 16 | reg;
1646 return mdiobus_read(pl->phydev->mdio.bus, prtad, devad);
1649 static int phylink_phy_write(struct phylink *pl, unsigned int phy_id,
1650 unsigned int reg, unsigned int val)
1652 struct phy_device *phydev = pl->phydev;
1655 if (mdio_phy_id_is_c45(phy_id)) {
1656 prtad = mdio_phy_id_prtad(phy_id);
1657 devad = mdio_phy_id_devad(phy_id);
1658 devad = MII_ADDR_C45 | devad << 16 | reg;
1659 } else if (phydev->is_c45) {
1665 devad = __ffs(phydev->c45_ids.devices_in_package);
1669 if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_AN))
1671 devad = MDIO_MMD_AN;
1672 if (reg == MII_ADVERTISE)
1673 reg = MDIO_AN_ADVERTISE;
1681 devad = MII_ADDR_C45 | devad << 16 | reg;
1687 return mdiobus_write(phydev->mdio.bus, prtad, devad, val);
1690 static int phylink_mii_read(struct phylink *pl, unsigned int phy_id,
1693 struct phylink_link_state state;
1696 switch (pl->cur_link_an_mode) {
1699 phylink_get_fixed_state(pl, &state);
1700 val = phylink_mii_emul_read(reg, &state);
1709 phylink_mac_pcs_get_state(pl, &state);
1710 val = phylink_mii_emul_read(reg, &state);
1715 return val & 0xffff;
1718 static int phylink_mii_write(struct phylink *pl, unsigned int phy_id,
1719 unsigned int reg, unsigned int val)
1721 switch (pl->cur_link_an_mode) {
1736 * phylink_mii_ioctl() - generic mii ioctl interface
1737 * @pl: a pointer to a &struct phylink returned from phylink_create()
1738 * @ifr: a pointer to a &struct ifreq for socket ioctls
1739 * @cmd: ioctl cmd to execute
1741 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1742 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1744 * Returns: zero on success or negative error code.
1747 * read register from the current PHY.
1749 * read register from the specified PHY.
1751 * set a register on the specified PHY.
1753 int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
1755 struct mii_ioctl_data *mii = if_mii(ifr);
1761 /* PHYs only exist for MLO_AN_PHY and SGMII */
1764 mii->phy_id = pl->phydev->mdio.addr;
1768 ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
1776 ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
1781 ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
1791 ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
1799 ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
1811 EXPORT_SYMBOL_GPL(phylink_mii_ioctl);
1813 static void phylink_sfp_attach(void *upstream, struct sfp_bus *bus)
1815 struct phylink *pl = upstream;
1817 pl->netdev->sfp_bus = bus;
1820 static void phylink_sfp_detach(void *upstream, struct sfp_bus *bus)
1822 struct phylink *pl = upstream;
1824 pl->netdev->sfp_bus = NULL;
1827 static int phylink_sfp_config(struct phylink *pl, u8 mode,
1828 const unsigned long *supported,
1829 const unsigned long *advertising)
1831 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1);
1832 __ETHTOOL_DECLARE_LINK_MODE_MASK(support);
1833 struct phylink_link_state config;
1834 phy_interface_t iface;
1838 linkmode_copy(support, supported);
1840 memset(&config, 0, sizeof(config));
1841 linkmode_copy(config.advertising, advertising);
1842 config.interface = PHY_INTERFACE_MODE_NA;
1843 config.speed = SPEED_UNKNOWN;
1844 config.duplex = DUPLEX_UNKNOWN;
1845 config.pause = MLO_PAUSE_AN;
1846 config.an_enabled = pl->link_config.an_enabled;
1848 /* Ignore errors if we're expecting a PHY to attach later */
1849 ret = phylink_validate(pl, support, &config);
1851 phylink_err(pl, "validation with support %*pb failed: %d\n",
1852 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1856 iface = sfp_select_interface(pl->sfp_bus, config.advertising);
1857 if (iface == PHY_INTERFACE_MODE_NA) {
1859 "selection of interface failed, advertisement %*pb\n",
1860 __ETHTOOL_LINK_MODE_MASK_NBITS, config.advertising);
1864 config.interface = iface;
1865 linkmode_copy(support1, support);
1866 ret = phylink_validate(pl, support1, &config);
1868 phylink_err(pl, "validation of %s/%s with support %*pb failed: %d\n",
1869 phylink_an_mode_str(mode),
1870 phy_modes(config.interface),
1871 __ETHTOOL_LINK_MODE_MASK_NBITS, support, ret);
1875 phylink_dbg(pl, "requesting link mode %s/%s with support %*pb\n",
1876 phylink_an_mode_str(mode), phy_modes(config.interface),
1877 __ETHTOOL_LINK_MODE_MASK_NBITS, support);
1879 if (phy_interface_mode_is_8023z(iface) && pl->phydev)
1882 changed = !linkmode_equal(pl->supported, support);
1884 linkmode_copy(pl->supported, support);
1885 linkmode_copy(pl->link_config.advertising, config.advertising);
1888 if (pl->cur_link_an_mode != mode ||
1889 pl->link_config.interface != config.interface) {
1890 pl->link_config.interface = config.interface;
1891 pl->cur_link_an_mode = mode;
1895 phylink_info(pl, "switched to %s/%s link mode\n",
1896 phylink_an_mode_str(mode),
1897 phy_modes(config.interface));
1900 pl->link_port = pl->sfp_port;
1902 if (changed && !test_bit(PHYLINK_DISABLE_STOPPED,
1903 &pl->phylink_disable_state))
1904 phylink_mac_initial_config(pl);
1909 static int phylink_sfp_module_insert(void *upstream,
1910 const struct sfp_eeprom_id *id)
1912 struct phylink *pl = upstream;
1913 unsigned long *support = pl->sfp_support;
1917 linkmode_zero(support);
1918 sfp_parse_support(pl->sfp_bus, id, support);
1919 pl->sfp_port = sfp_parse_port(pl->sfp_bus, id, support);
1921 /* If this module may have a PHY connecting later, defer until later */
1922 pl->sfp_may_have_phy = sfp_may_have_phy(pl->sfp_bus, id);
1923 if (pl->sfp_may_have_phy)
1926 return phylink_sfp_config(pl, MLO_AN_INBAND, support, support);
1929 static int phylink_sfp_module_start(void *upstream)
1931 struct phylink *pl = upstream;
1933 /* If this SFP module has a PHY, start the PHY now. */
1935 phy_start(pl->phydev);
1939 /* If the module may have a PHY but we didn't detect one we
1940 * need to configure the MAC here.
1942 if (!pl->sfp_may_have_phy)
1945 return phylink_sfp_config(pl, MLO_AN_INBAND,
1946 pl->sfp_support, pl->sfp_support);
1949 static void phylink_sfp_module_stop(void *upstream)
1951 struct phylink *pl = upstream;
1953 /* If this SFP module has a PHY, stop it. */
1955 phy_stop(pl->phydev);
1958 static void phylink_sfp_link_down(void *upstream)
1960 struct phylink *pl = upstream;
1964 phylink_run_resolve_and_disable(pl, PHYLINK_DISABLE_LINK);
1967 static void phylink_sfp_link_up(void *upstream)
1969 struct phylink *pl = upstream;
1973 clear_bit(PHYLINK_DISABLE_LINK, &pl->phylink_disable_state);
1974 phylink_run_resolve(pl);
1977 /* The Broadcom BCM84881 in the Methode DM7052 is unable to provide a SGMII
1978 * or 802.3z control word, so inband will not work.
1980 static bool phylink_phy_no_inband(struct phy_device *phy)
1982 return phy->is_c45 &&
1983 (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
1986 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
1988 struct phylink *pl = upstream;
1989 phy_interface_t interface;
1994 * This is the new way of dealing with flow control for PHYs,
1995 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
1996 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
1997 * using our validate call to the MAC, we rely upon the MAC
1998 * clearing the bits from both supported and advertising fields.
2000 phy_support_asym_pause(phy);
2002 if (phylink_phy_no_inband(phy))
2005 mode = MLO_AN_INBAND;
2007 /* Do the initial configuration */
2008 ret = phylink_sfp_config(pl, mode, phy->supported, phy->advertising);
2012 interface = pl->link_config.interface;
2013 ret = phylink_attach_phy(pl, phy, interface);
2017 ret = phylink_bringup_phy(pl, phy, interface);
2024 static void phylink_sfp_disconnect_phy(void *upstream)
2026 phylink_disconnect_phy(upstream);
2029 static const struct sfp_upstream_ops sfp_phylink_ops = {
2030 .attach = phylink_sfp_attach,
2031 .detach = phylink_sfp_detach,
2032 .module_insert = phylink_sfp_module_insert,
2033 .module_start = phylink_sfp_module_start,
2034 .module_stop = phylink_sfp_module_stop,
2035 .link_up = phylink_sfp_link_up,
2036 .link_down = phylink_sfp_link_down,
2037 .connect_phy = phylink_sfp_connect_phy,
2038 .disconnect_phy = phylink_sfp_disconnect_phy,
2041 /* Helpers for MAC drivers */
2044 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
2045 * @state: a pointer to a &struct phylink_link_state
2047 * Inspect the interface mode, advertising mask or forced speed and
2048 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
2049 * the interface mode to suit. @state->interface is appropriately
2050 * updated, and the advertising mask has the "other" baseX_Full flag
2053 void phylink_helper_basex_speed(struct phylink_link_state *state)
2055 if (phy_interface_mode_is_8023z(state->interface)) {
2056 bool want_2500 = state->an_enabled ?
2057 phylink_test(state->advertising, 2500baseX_Full) :
2058 state->speed == SPEED_2500;
2061 phylink_clear(state->advertising, 1000baseX_Full);
2062 state->interface = PHY_INTERFACE_MODE_2500BASEX;
2064 phylink_clear(state->advertising, 2500baseX_Full);
2065 state->interface = PHY_INTERFACE_MODE_1000BASEX;
2069 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed);
2071 MODULE_LICENSE("GPL v2");