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/workqueue.h>
27 #define SUPPORTED_INTERFACES \
28 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
29 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
30 #define ADVERTISED_INTERFACES \
31 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
32 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
35 PHYLINK_DISABLE_STOPPED
,
40 struct net_device
*netdev
;
41 const struct phylink_mac_ops
*ops
;
43 unsigned long phylink_disable_state
; /* bitmask of disables */
44 struct phy_device
*phydev
;
45 phy_interface_t link_interface
; /* PHY_INTERFACE_xxx */
46 u8 link_an_mode
; /* MLO_AN_xxx */
47 u8 link_port
; /* The current non-phy ethtool port */
48 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
50 /* The link configuration settings */
51 struct phylink_link_state link_config
;
52 struct gpio_desc
*link_gpio
;
54 struct mutex state_mutex
;
55 struct phylink_link_state phy_state
;
56 struct work_struct resolve
;
58 bool mac_link_dropped
;
60 struct sfp_bus
*sfp_bus
;
63 static inline void linkmode_zero(unsigned long *dst
)
65 bitmap_zero(dst
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
68 static inline void linkmode_copy(unsigned long *dst
, const unsigned long *src
)
70 bitmap_copy(dst
, src
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
73 static inline void linkmode_and(unsigned long *dst
, const unsigned long *a
,
74 const unsigned long *b
)
76 bitmap_and(dst
, a
, b
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
79 static inline void linkmode_or(unsigned long *dst
, const unsigned long *a
,
80 const unsigned long *b
)
82 bitmap_or(dst
, a
, b
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
85 static inline bool linkmode_empty(const unsigned long *src
)
87 return bitmap_empty(src
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
90 void phylink_set_port_modes(unsigned long *mask
)
92 phylink_set(mask
, TP
);
93 phylink_set(mask
, AUI
);
94 phylink_set(mask
, MII
);
95 phylink_set(mask
, FIBRE
);
96 phylink_set(mask
, BNC
);
97 phylink_set(mask
, Backplane
);
99 EXPORT_SYMBOL_GPL(phylink_set_port_modes
);
101 static int phylink_is_empty_linkmode(const unsigned long *linkmode
)
103 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp
) = { 0, };
105 phylink_set_port_modes(tmp
);
106 phylink_set(tmp
, Autoneg
);
107 phylink_set(tmp
, Pause
);
108 phylink_set(tmp
, Asym_Pause
);
110 bitmap_andnot(tmp
, linkmode
, tmp
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
112 return linkmode_empty(tmp
);
115 static const char *phylink_an_mode_str(unsigned int mode
)
117 static const char *modestr
[] = {
118 [MLO_AN_PHY
] = "phy",
119 [MLO_AN_FIXED
] = "fixed",
120 [MLO_AN_SGMII
] = "SGMII",
121 [MLO_AN_8023Z
] = "802.3z",
124 return mode
< ARRAY_SIZE(modestr
) ? modestr
[mode
] : "unknown";
127 static int phylink_validate(struct phylink
*pl
, unsigned long *supported
,
128 struct phylink_link_state
*state
)
130 pl
->ops
->validate(pl
->netdev
, supported
, state
);
132 return phylink_is_empty_linkmode(supported
) ? -EINVAL
: 0;
135 static int phylink_parse_fixedlink(struct phylink
*pl
, struct device_node
*np
)
137 struct device_node
*fixed_node
;
138 const struct phy_setting
*s
;
139 struct gpio_desc
*desc
;
140 const __be32
*fixed_prop
;
144 fixed_node
= of_get_child_by_name(np
, "fixed-link");
146 ret
= of_property_read_u32(fixed_node
, "speed", &speed
);
148 pl
->link_config
.speed
= speed
;
149 pl
->link_config
.duplex
= DUPLEX_HALF
;
151 if (of_property_read_bool(fixed_node
, "full-duplex"))
152 pl
->link_config
.duplex
= DUPLEX_FULL
;
154 /* We treat the "pause" and "asym-pause" terminology as
155 * defining the link partner's ability. */
156 if (of_property_read_bool(fixed_node
, "pause"))
157 pl
->link_config
.pause
|= MLO_PAUSE_SYM
;
158 if (of_property_read_bool(fixed_node
, "asym-pause"))
159 pl
->link_config
.pause
|= MLO_PAUSE_ASYM
;
162 desc
= fwnode_get_named_gpiod(&fixed_node
->fwnode
,
167 pl
->link_gpio
= desc
;
168 else if (desc
== ERR_PTR(-EPROBE_DEFER
))
171 of_node_put(fixed_node
);
176 fixed_prop
= of_get_property(np
, "fixed-link", &len
);
178 netdev_err(pl
->netdev
, "broken fixed-link?\n");
181 if (len
== 5 * sizeof(*fixed_prop
)) {
182 pl
->link_config
.duplex
= be32_to_cpu(fixed_prop
[1]) ?
183 DUPLEX_FULL
: DUPLEX_HALF
;
184 pl
->link_config
.speed
= be32_to_cpu(fixed_prop
[2]);
185 if (be32_to_cpu(fixed_prop
[3]))
186 pl
->link_config
.pause
|= MLO_PAUSE_SYM
;
187 if (be32_to_cpu(fixed_prop
[4]))
188 pl
->link_config
.pause
|= MLO_PAUSE_ASYM
;
192 if (pl
->link_config
.speed
> SPEED_1000
&&
193 pl
->link_config
.duplex
!= DUPLEX_FULL
)
194 netdev_warn(pl
->netdev
, "fixed link specifies half duplex for %dMbps link?\n",
195 pl
->link_config
.speed
);
197 bitmap_fill(pl
->supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
198 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
199 phylink_validate(pl
, pl
->supported
, &pl
->link_config
);
201 s
= phy_lookup_setting(pl
->link_config
.speed
, pl
->link_config
.duplex
,
203 __ETHTOOL_LINK_MODE_MASK_NBITS
, true);
204 linkmode_zero(pl
->supported
);
205 phylink_set(pl
->supported
, MII
);
207 __set_bit(s
->bit
, pl
->supported
);
209 netdev_warn(pl
->netdev
, "fixed link %s duplex %dMbps not recognised\n",
210 pl
->link_config
.duplex
== DUPLEX_FULL
? "full" : "half",
211 pl
->link_config
.speed
);
214 linkmode_and(pl
->link_config
.advertising
, pl
->link_config
.advertising
,
217 pl
->link_config
.link
= 1;
218 pl
->link_config
.an_complete
= 1;
223 static int phylink_parse_mode(struct phylink
*pl
, struct device_node
*np
)
225 struct device_node
*dn
;
228 dn
= of_get_child_by_name(np
, "fixed-link");
229 if (dn
|| of_find_property(np
, "fixed-link", NULL
))
230 pl
->link_an_mode
= MLO_AN_FIXED
;
233 if (of_property_read_string(np
, "managed", &managed
) == 0 &&
234 strcmp(managed
, "in-band-status") == 0) {
235 if (pl
->link_an_mode
== MLO_AN_FIXED
) {
236 netdev_err(pl
->netdev
,
237 "can't use both fixed-link and in-band-status\n");
241 linkmode_zero(pl
->supported
);
242 phylink_set(pl
->supported
, MII
);
243 phylink_set(pl
->supported
, Autoneg
);
244 phylink_set(pl
->supported
, Asym_Pause
);
245 phylink_set(pl
->supported
, Pause
);
246 pl
->link_config
.an_enabled
= true;
248 switch (pl
->link_config
.interface
) {
249 case PHY_INTERFACE_MODE_SGMII
:
250 phylink_set(pl
->supported
, 10baseT_Half
);
251 phylink_set(pl
->supported
, 10baseT_Full
);
252 phylink_set(pl
->supported
, 100baseT_Half
);
253 phylink_set(pl
->supported
, 100baseT_Full
);
254 phylink_set(pl
->supported
, 1000baseT_Half
);
255 phylink_set(pl
->supported
, 1000baseT_Full
);
256 pl
->link_an_mode
= MLO_AN_SGMII
;
259 case PHY_INTERFACE_MODE_1000BASEX
:
260 phylink_set(pl
->supported
, 1000baseX_Full
);
261 pl
->link_an_mode
= MLO_AN_8023Z
;
264 case PHY_INTERFACE_MODE_2500BASEX
:
265 phylink_set(pl
->supported
, 2500baseX_Full
);
266 pl
->link_an_mode
= MLO_AN_8023Z
;
269 case PHY_INTERFACE_MODE_10GKR
:
270 phylink_set(pl
->supported
, 10baseT_Half
);
271 phylink_set(pl
->supported
, 10baseT_Full
);
272 phylink_set(pl
->supported
, 100baseT_Half
);
273 phylink_set(pl
->supported
, 100baseT_Full
);
274 phylink_set(pl
->supported
, 1000baseT_Half
);
275 phylink_set(pl
->supported
, 1000baseT_Full
);
276 phylink_set(pl
->supported
, 1000baseX_Full
);
277 phylink_set(pl
->supported
, 10000baseKR_Full
);
278 phylink_set(pl
->supported
, 10000baseCR_Full
);
279 phylink_set(pl
->supported
, 10000baseSR_Full
);
280 phylink_set(pl
->supported
, 10000baseLR_Full
);
281 phylink_set(pl
->supported
, 10000baseLRM_Full
);
282 phylink_set(pl
->supported
, 10000baseER_Full
);
283 pl
->link_an_mode
= MLO_AN_SGMII
;
287 netdev_err(pl
->netdev
,
288 "incorrect link mode %s for in-band status\n",
289 phy_modes(pl
->link_config
.interface
));
293 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
295 if (phylink_validate(pl
, pl
->supported
, &pl
->link_config
)) {
296 netdev_err(pl
->netdev
,
297 "failed to validate link configuration for in-band status\n");
305 static void phylink_mac_config(struct phylink
*pl
,
306 const struct phylink_link_state
*state
)
308 netdev_dbg(pl
->netdev
,
309 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
310 __func__
, phylink_an_mode_str(pl
->link_an_mode
),
311 phy_modes(state
->interface
),
312 phy_speed_to_str(state
->speed
),
313 phy_duplex_to_str(state
->duplex
),
314 __ETHTOOL_LINK_MODE_MASK_NBITS
, state
->advertising
,
315 state
->pause
, state
->link
, state
->an_enabled
);
317 pl
->ops
->mac_config(pl
->netdev
, pl
->link_an_mode
, state
);
320 static void phylink_mac_an_restart(struct phylink
*pl
)
322 if (pl
->link_config
.an_enabled
&&
323 (pl
->link_config
.interface
== PHY_INTERFACE_MODE_1000BASEX
||
324 pl
->link_config
.interface
== PHY_INTERFACE_MODE_2500BASEX
))
325 pl
->ops
->mac_an_restart(pl
->netdev
);
328 static int phylink_get_mac_state(struct phylink
*pl
, struct phylink_link_state
*state
)
330 struct net_device
*ndev
= pl
->netdev
;
332 linkmode_copy(state
->advertising
, pl
->link_config
.advertising
);
333 linkmode_zero(state
->lp_advertising
);
334 state
->interface
= pl
->link_config
.interface
;
335 state
->an_enabled
= pl
->link_config
.an_enabled
;
338 return pl
->ops
->mac_link_state(ndev
, state
);
341 /* The fixed state is... fixed except for the link state,
342 * which may be determined by a GPIO.
344 static void phylink_get_fixed_state(struct phylink
*pl
, struct phylink_link_state
*state
)
346 *state
= pl
->link_config
;
348 state
->link
= !!gpiod_get_value(pl
->link_gpio
);
351 /* Flow control is resolved according to our and the link partners
352 * advertisments using the following drawn from the 802.3 specs:
353 * Local device Link partner
354 * Pause AsymDir Pause AsymDir Result
359 static void phylink_resolve_flow(struct phylink
*pl
,
360 struct phylink_link_state
*state
)
364 if (pl
->link_config
.pause
& MLO_PAUSE_AN
) {
367 if (phylink_test(pl
->link_config
.advertising
, Pause
))
368 pause
|= MLO_PAUSE_SYM
;
369 if (phylink_test(pl
->link_config
.advertising
, Asym_Pause
))
370 pause
|= MLO_PAUSE_ASYM
;
372 pause
&= state
->pause
;
374 if (pause
& MLO_PAUSE_SYM
)
375 new_pause
= MLO_PAUSE_TX
| MLO_PAUSE_RX
;
376 else if (pause
& MLO_PAUSE_ASYM
)
377 new_pause
= state
->pause
& MLO_PAUSE_SYM
?
378 MLO_PAUSE_RX
: MLO_PAUSE_TX
;
380 new_pause
= pl
->link_config
.pause
& MLO_PAUSE_TXRX_MASK
;
383 state
->pause
&= ~MLO_PAUSE_TXRX_MASK
;
384 state
->pause
|= new_pause
;
387 static const char *phylink_pause_to_str(int pause
)
389 switch (pause
& MLO_PAUSE_TXRX_MASK
) {
390 case MLO_PAUSE_TX
| MLO_PAUSE_RX
:
401 static void phylink_resolve(struct work_struct
*w
)
403 struct phylink
*pl
= container_of(w
, struct phylink
, resolve
);
404 struct phylink_link_state link_state
;
405 struct net_device
*ndev
= pl
->netdev
;
407 mutex_lock(&pl
->state_mutex
);
408 if (pl
->phylink_disable_state
) {
409 pl
->mac_link_dropped
= false;
410 link_state
.link
= false;
411 } else if (pl
->mac_link_dropped
) {
412 link_state
.link
= false;
414 switch (pl
->link_an_mode
) {
416 link_state
= pl
->phy_state
;
417 phylink_resolve_flow(pl
, &link_state
);
418 phylink_mac_config(pl
, &link_state
);
422 phylink_get_fixed_state(pl
, &link_state
);
423 phylink_mac_config(pl
, &link_state
);
427 phylink_get_mac_state(pl
, &link_state
);
429 bool changed
= false;
431 link_state
.link
= link_state
.link
&&
434 if (pl
->phy_state
.interface
!=
435 link_state
.interface
) {
436 link_state
.interface
= pl
->phy_state
.interface
;
440 /* Propagate the flow control from the PHY
441 * to the MAC. Also propagate the interface
444 if (pl
->phy_state
.link
|| changed
) {
445 link_state
.pause
|= pl
->phy_state
.pause
;
446 phylink_resolve_flow(pl
, &link_state
);
448 phylink_mac_config(pl
, &link_state
);
454 phylink_get_mac_state(pl
, &link_state
);
459 if (link_state
.link
!= netif_carrier_ok(ndev
)) {
460 if (!link_state
.link
) {
461 netif_carrier_off(ndev
);
462 pl
->ops
->mac_link_down(ndev
, pl
->link_an_mode
);
463 netdev_info(ndev
, "Link is Down\n");
465 pl
->ops
->mac_link_up(ndev
, pl
->link_an_mode
,
468 netif_carrier_on(ndev
);
471 "Link is Up - %s/%s - flow control %s\n",
472 phy_speed_to_str(link_state
.speed
),
473 phy_duplex_to_str(link_state
.duplex
),
474 phylink_pause_to_str(link_state
.pause
));
477 if (!link_state
.link
&& pl
->mac_link_dropped
) {
478 pl
->mac_link_dropped
= false;
479 queue_work(system_power_efficient_wq
, &pl
->resolve
);
481 mutex_unlock(&pl
->state_mutex
);
484 static void phylink_run_resolve(struct phylink
*pl
)
486 if (!pl
->phylink_disable_state
)
487 queue_work(system_power_efficient_wq
, &pl
->resolve
);
490 static const struct sfp_upstream_ops sfp_phylink_ops
;
492 static int phylink_register_sfp(struct phylink
*pl
, struct device_node
*np
)
494 struct device_node
*sfp_np
;
496 sfp_np
= of_parse_phandle(np
, "sfp", 0);
500 pl
->sfp_bus
= sfp_register_upstream(sfp_np
, pl
->netdev
, pl
,
508 struct phylink
*phylink_create(struct net_device
*ndev
, struct device_node
*np
,
509 phy_interface_t iface
, const struct phylink_mac_ops
*ops
)
514 pl
= kzalloc(sizeof(*pl
), GFP_KERNEL
);
516 return ERR_PTR(-ENOMEM
);
518 mutex_init(&pl
->state_mutex
);
519 INIT_WORK(&pl
->resolve
, phylink_resolve
);
521 pl
->phy_state
.interface
= iface
;
522 pl
->link_interface
= iface
;
523 pl
->link_port
= PORT_MII
;
524 pl
->link_config
.interface
= iface
;
525 pl
->link_config
.pause
= MLO_PAUSE_AN
;
526 pl
->link_config
.speed
= SPEED_UNKNOWN
;
527 pl
->link_config
.duplex
= DUPLEX_UNKNOWN
;
529 __set_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
531 bitmap_fill(pl
->supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
532 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
533 phylink_validate(pl
, pl
->supported
, &pl
->link_config
);
535 ret
= phylink_parse_mode(pl
, np
);
541 if (pl
->link_an_mode
== MLO_AN_FIXED
) {
542 ret
= phylink_parse_fixedlink(pl
, np
);
549 ret
= phylink_register_sfp(pl
, np
);
557 EXPORT_SYMBOL_GPL(phylink_create
);
559 void phylink_destroy(struct phylink
*pl
)
562 sfp_unregister_upstream(pl
->sfp_bus
);
564 cancel_work_sync(&pl
->resolve
);
567 EXPORT_SYMBOL_GPL(phylink_destroy
);
569 void phylink_phy_change(struct phy_device
*phydev
, bool up
, bool do_carrier
)
571 struct phylink
*pl
= phydev
->phylink
;
573 mutex_lock(&pl
->state_mutex
);
574 pl
->phy_state
.speed
= phydev
->speed
;
575 pl
->phy_state
.duplex
= phydev
->duplex
;
576 pl
->phy_state
.pause
= MLO_PAUSE_NONE
;
578 pl
->phy_state
.pause
|= MLO_PAUSE_SYM
;
579 if (phydev
->asym_pause
)
580 pl
->phy_state
.pause
|= MLO_PAUSE_ASYM
;
581 pl
->phy_state
.interface
= phydev
->interface
;
582 pl
->phy_state
.link
= up
;
583 mutex_unlock(&pl
->state_mutex
);
585 phylink_run_resolve(pl
);
587 netdev_dbg(pl
->netdev
, "phy link %s %s/%s/%s\n", up
? "up" : "down",
588 phy_modes(phydev
->interface
),
589 phy_speed_to_str(phydev
->speed
),
590 phy_duplex_to_str(phydev
->duplex
));
593 static int phylink_bringup_phy(struct phylink
*pl
, struct phy_device
*phy
)
595 struct phylink_link_state config
;
596 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
600 memset(&config
, 0, sizeof(config
));
601 ethtool_convert_legacy_u32_to_link_mode(supported
, phy
->supported
);
602 ethtool_convert_legacy_u32_to_link_mode(config
.advertising
,
604 config
.interface
= pl
->link_config
.interface
;
607 * This is the new way of dealing with flow control for PHYs,
608 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
609 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
610 * using our validate call to the MAC, we rely upon the MAC
611 * clearing the bits from both supported and advertising fields.
613 if (phylink_test(supported
, Pause
))
614 phylink_set(config
.advertising
, Pause
);
615 if (phylink_test(supported
, Asym_Pause
))
616 phylink_set(config
.advertising
, Asym_Pause
);
618 ret
= phylink_validate(pl
, supported
, &config
);
623 phy
->phy_link_change
= phylink_phy_change
;
625 netdev_info(pl
->netdev
,
626 "PHY [%s] driver [%s]\n", dev_name(&phy
->mdio
.dev
),
629 mutex_lock(&phy
->lock
);
630 mutex_lock(&pl
->state_mutex
);
631 pl
->netdev
->phydev
= phy
;
633 linkmode_copy(pl
->supported
, supported
);
634 linkmode_copy(pl
->link_config
.advertising
, config
.advertising
);
636 /* Restrict the phy advertisment according to the MAC support. */
637 ethtool_convert_link_mode_to_legacy_u32(&advertising
, config
.advertising
);
638 phy
->advertising
= advertising
;
639 mutex_unlock(&pl
->state_mutex
);
640 mutex_unlock(&phy
->lock
);
642 netdev_dbg(pl
->netdev
,
643 "phy: setting supported %*pb advertising 0x%08x\n",
644 __ETHTOOL_LINK_MODE_MASK_NBITS
, pl
->supported
,
647 phy_start_machine(phy
);
649 phy_start_interrupts(phy
);
654 int phylink_connect_phy(struct phylink
*pl
, struct phy_device
*phy
)
658 ret
= phy_attach_direct(pl
->netdev
, phy
, 0, pl
->link_interface
);
662 ret
= phylink_bringup_phy(pl
, phy
);
668 EXPORT_SYMBOL_GPL(phylink_connect_phy
);
670 int phylink_of_phy_connect(struct phylink
*pl
, struct device_node
*dn
)
672 struct device_node
*phy_node
;
673 struct phy_device
*phy_dev
;
676 /* Fixed links are handled without needing a PHY */
677 if (pl
->link_an_mode
== MLO_AN_FIXED
)
680 phy_node
= of_parse_phandle(dn
, "phy-handle", 0);
682 phy_node
= of_parse_phandle(dn
, "phy", 0);
684 phy_node
= of_parse_phandle(dn
, "phy-device", 0);
687 if (pl
->link_an_mode
== MLO_AN_PHY
) {
688 netdev_err(pl
->netdev
, "unable to find PHY node\n");
694 phy_dev
= of_phy_attach(pl
->netdev
, phy_node
, 0, pl
->link_interface
);
695 /* We're done with the phy_node handle */
696 of_node_put(phy_node
);
701 ret
= phylink_bringup_phy(pl
, phy_dev
);
707 EXPORT_SYMBOL_GPL(phylink_of_phy_connect
);
709 void phylink_disconnect_phy(struct phylink
*pl
)
711 struct phy_device
*phy
;
713 WARN_ON(!lockdep_rtnl_is_held());
717 mutex_lock(&phy
->lock
);
718 mutex_lock(&pl
->state_mutex
);
719 pl
->netdev
->phydev
= NULL
;
721 mutex_unlock(&pl
->state_mutex
);
722 mutex_unlock(&phy
->lock
);
723 flush_work(&pl
->resolve
);
728 EXPORT_SYMBOL_GPL(phylink_disconnect_phy
);
730 void phylink_mac_change(struct phylink
*pl
, bool up
)
733 pl
->mac_link_dropped
= true;
734 phylink_run_resolve(pl
);
735 netdev_dbg(pl
->netdev
, "mac link %s\n", up
? "up" : "down");
737 EXPORT_SYMBOL_GPL(phylink_mac_change
);
739 void phylink_start(struct phylink
*pl
)
741 WARN_ON(!lockdep_rtnl_is_held());
743 netdev_info(pl
->netdev
, "configuring for %s/%s link mode\n",
744 phylink_an_mode_str(pl
->link_an_mode
),
745 phy_modes(pl
->link_config
.interface
));
747 /* Apply the link configuration to the MAC when starting. This allows
748 * a fixed-link to start with the correct parameters, and also
749 * ensures that we set the appropriate advertisment for Serdes links.
751 phylink_resolve_flow(pl
, &pl
->link_config
);
752 phylink_mac_config(pl
, &pl
->link_config
);
754 clear_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
755 phylink_run_resolve(pl
);
758 sfp_upstream_start(pl
->sfp_bus
);
760 phy_start(pl
->phydev
);
762 EXPORT_SYMBOL_GPL(phylink_start
);
764 void phylink_stop(struct phylink
*pl
)
766 WARN_ON(!lockdep_rtnl_is_held());
769 phy_stop(pl
->phydev
);
771 sfp_upstream_stop(pl
->sfp_bus
);
773 set_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
774 flush_work(&pl
->resolve
);
776 EXPORT_SYMBOL_GPL(phylink_stop
);
778 void phylink_ethtool_get_wol(struct phylink
*pl
, struct ethtool_wolinfo
*wol
)
780 WARN_ON(!lockdep_rtnl_is_held());
786 phy_ethtool_get_wol(pl
->phydev
, wol
);
788 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol
);
790 int phylink_ethtool_set_wol(struct phylink
*pl
, struct ethtool_wolinfo
*wol
)
792 int ret
= -EOPNOTSUPP
;
794 WARN_ON(!lockdep_rtnl_is_held());
797 ret
= phy_ethtool_set_wol(pl
->phydev
, wol
);
801 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol
);
803 static void phylink_merge_link_mode(unsigned long *dst
, const unsigned long *b
)
805 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
);
808 phylink_set_port_modes(mask
);
810 linkmode_and(dst
, dst
, mask
);
811 linkmode_or(dst
, dst
, b
);
814 static void phylink_get_ksettings(const struct phylink_link_state
*state
,
815 struct ethtool_link_ksettings
*kset
)
817 phylink_merge_link_mode(kset
->link_modes
.advertising
, state
->advertising
);
818 linkmode_copy(kset
->link_modes
.lp_advertising
, state
->lp_advertising
);
819 kset
->base
.speed
= state
->speed
;
820 kset
->base
.duplex
= state
->duplex
;
821 kset
->base
.autoneg
= state
->an_enabled
? AUTONEG_ENABLE
:
825 int phylink_ethtool_ksettings_get(struct phylink
*pl
,
826 struct ethtool_link_ksettings
*kset
)
828 struct phylink_link_state link_state
;
830 WARN_ON(!lockdep_rtnl_is_held());
833 phy_ethtool_ksettings_get(pl
->phydev
, kset
);
835 kset
->base
.port
= pl
->link_port
;
838 linkmode_copy(kset
->link_modes
.supported
, pl
->supported
);
840 switch (pl
->link_an_mode
) {
842 /* We are using fixed settings. Report these as the
843 * current link settings - and note that these also
844 * represent the supported speeds/duplex/pause modes.
846 phylink_get_fixed_state(pl
, &link_state
);
847 phylink_get_ksettings(&link_state
, kset
);
851 /* If there is a phy attached, then use the reported
852 * settings from the phy with no modification.
858 phylink_get_mac_state(pl
, &link_state
);
860 /* The MAC is reporting the link results from its own PCS
861 * layer via in-band status. Report these as the current
864 phylink_get_ksettings(&link_state
, kset
);
870 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get
);
872 int phylink_ethtool_ksettings_set(struct phylink
*pl
,
873 const struct ethtool_link_ksettings
*kset
)
875 struct ethtool_link_ksettings our_kset
;
876 struct phylink_link_state config
;
879 WARN_ON(!lockdep_rtnl_is_held());
881 if (kset
->base
.autoneg
!= AUTONEG_DISABLE
&&
882 kset
->base
.autoneg
!= AUTONEG_ENABLE
)
885 config
= pl
->link_config
;
887 /* Mask out unsupported advertisments */
888 linkmode_and(config
.advertising
, kset
->link_modes
.advertising
,
891 /* FIXME: should we reject autoneg if phy/mac does not support it? */
892 if (kset
->base
.autoneg
== AUTONEG_DISABLE
) {
893 const struct phy_setting
*s
;
895 /* Autonegotiation disabled, select a suitable speed and
898 s
= phy_lookup_setting(kset
->base
.speed
, kset
->base
.duplex
,
900 __ETHTOOL_LINK_MODE_MASK_NBITS
, false);
904 /* If we have a fixed link (as specified by firmware), refuse
905 * to change link parameters.
907 if (pl
->link_an_mode
== MLO_AN_FIXED
&&
908 (s
->speed
!= pl
->link_config
.speed
||
909 s
->duplex
!= pl
->link_config
.duplex
))
912 config
.speed
= s
->speed
;
913 config
.duplex
= s
->duplex
;
914 config
.an_enabled
= false;
916 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, config
.advertising
);
918 /* If we have a fixed link, refuse to enable autonegotiation */
919 if (pl
->link_an_mode
== MLO_AN_FIXED
)
922 config
.speed
= SPEED_UNKNOWN
;
923 config
.duplex
= DUPLEX_UNKNOWN
;
924 config
.an_enabled
= true;
926 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, config
.advertising
);
929 if (phylink_validate(pl
, pl
->supported
, &config
))
932 /* If autonegotiation is enabled, we must have an advertisment */
933 if (config
.an_enabled
&& phylink_is_empty_linkmode(config
.advertising
))
937 linkmode_copy(our_kset
.link_modes
.advertising
, config
.advertising
);
938 our_kset
.base
.speed
= config
.speed
;
939 our_kset
.base
.duplex
= config
.duplex
;
941 /* If we have a PHY, configure the phy */
943 ret
= phy_ethtool_ksettings_set(pl
->phydev
, &our_kset
);
948 mutex_lock(&pl
->state_mutex
);
949 /* Configure the MAC to match the new settings */
950 linkmode_copy(pl
->link_config
.advertising
, our_kset
.link_modes
.advertising
);
951 pl
->link_config
.speed
= our_kset
.base
.speed
;
952 pl
->link_config
.duplex
= our_kset
.base
.duplex
;
953 pl
->link_config
.an_enabled
= our_kset
.base
.autoneg
!= AUTONEG_DISABLE
;
955 if (!test_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
)) {
956 phylink_mac_config(pl
, &pl
->link_config
);
957 phylink_mac_an_restart(pl
);
959 mutex_unlock(&pl
->state_mutex
);
963 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set
);
965 int phylink_ethtool_nway_reset(struct phylink
*pl
)
969 WARN_ON(!lockdep_rtnl_is_held());
972 ret
= phy_restart_aneg(pl
->phydev
);
973 phylink_mac_an_restart(pl
);
977 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset
);
979 void phylink_ethtool_get_pauseparam(struct phylink
*pl
,
980 struct ethtool_pauseparam
*pause
)
982 WARN_ON(!lockdep_rtnl_is_held());
984 pause
->autoneg
= !!(pl
->link_config
.pause
& MLO_PAUSE_AN
);
985 pause
->rx_pause
= !!(pl
->link_config
.pause
& MLO_PAUSE_RX
);
986 pause
->tx_pause
= !!(pl
->link_config
.pause
& MLO_PAUSE_TX
);
988 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam
);
990 int phylink_ethtool_set_pauseparam(struct phylink
*pl
,
991 struct ethtool_pauseparam
*pause
)
993 struct phylink_link_state
*config
= &pl
->link_config
;
995 WARN_ON(!lockdep_rtnl_is_held());
997 if (!phylink_test(pl
->supported
, Pause
) &&
998 !phylink_test(pl
->supported
, Asym_Pause
))
1001 if (!phylink_test(pl
->supported
, Asym_Pause
) &&
1002 !pause
->autoneg
&& pause
->rx_pause
!= pause
->tx_pause
)
1005 config
->pause
&= ~(MLO_PAUSE_AN
| MLO_PAUSE_TXRX_MASK
);
1008 config
->pause
|= MLO_PAUSE_AN
;
1009 if (pause
->rx_pause
)
1010 config
->pause
|= MLO_PAUSE_RX
;
1011 if (pause
->tx_pause
)
1012 config
->pause
|= MLO_PAUSE_TX
;
1014 if (!test_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
)) {
1015 switch (pl
->link_an_mode
) {
1017 /* Silently mark the carrier down, and then trigger a resolve */
1018 netif_carrier_off(pl
->netdev
);
1019 phylink_run_resolve(pl
);
1023 /* Should we allow fixed links to change against the config? */
1024 phylink_resolve_flow(pl
, config
);
1025 phylink_mac_config(pl
, config
);
1030 phylink_mac_config(pl
, config
);
1031 phylink_mac_an_restart(pl
);
1038 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam
);
1040 int phylink_ethtool_get_module_info(struct phylink
*pl
,
1041 struct ethtool_modinfo
*modinfo
)
1043 int ret
= -EOPNOTSUPP
;
1045 WARN_ON(!lockdep_rtnl_is_held());
1048 ret
= sfp_get_module_info(pl
->sfp_bus
, modinfo
);
1052 EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info
);
1054 int phylink_ethtool_get_module_eeprom(struct phylink
*pl
,
1055 struct ethtool_eeprom
*ee
, u8
*buf
)
1057 int ret
= -EOPNOTSUPP
;
1059 WARN_ON(!lockdep_rtnl_is_held());
1062 ret
= sfp_get_module_eeprom(pl
->sfp_bus
, ee
, buf
);
1066 EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom
);
1068 int phylink_init_eee(struct phylink
*pl
, bool clk_stop_enable
)
1070 int ret
= -EPROTONOSUPPORT
;
1072 WARN_ON(!lockdep_rtnl_is_held());
1075 ret
= phy_init_eee(pl
->phydev
, clk_stop_enable
);
1079 EXPORT_SYMBOL_GPL(phylink_init_eee
);
1081 int phylink_get_eee_err(struct phylink
*pl
)
1085 WARN_ON(!lockdep_rtnl_is_held());
1088 ret
= phy_get_eee_err(pl
->phydev
);
1092 EXPORT_SYMBOL_GPL(phylink_get_eee_err
);
1094 int phylink_ethtool_get_eee(struct phylink
*pl
, struct ethtool_eee
*eee
)
1096 int ret
= -EOPNOTSUPP
;
1098 WARN_ON(!lockdep_rtnl_is_held());
1101 ret
= phy_ethtool_get_eee(pl
->phydev
, eee
);
1105 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee
);
1107 int phylink_ethtool_set_eee(struct phylink
*pl
, struct ethtool_eee
*eee
)
1109 int ret
= -EOPNOTSUPP
;
1111 WARN_ON(!lockdep_rtnl_is_held());
1114 ret
= phy_ethtool_set_eee(pl
->phydev
, eee
);
1118 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee
);
1120 /* This emulates MII registers for a fixed-mode phy operating as per the
1121 * passed in state. "aneg" defines if we report negotiation is possible.
1123 * FIXME: should deal with negotiation state too.
1125 static int phylink_mii_emul_read(struct net_device
*ndev
, unsigned int reg
,
1126 struct phylink_link_state
*state
, bool aneg
)
1128 struct fixed_phy_status fs
;
1131 fs
.link
= state
->link
;
1132 fs
.speed
= state
->speed
;
1133 fs
.duplex
= state
->duplex
;
1134 fs
.pause
= state
->pause
& MLO_PAUSE_SYM
;
1135 fs
.asym_pause
= state
->pause
& MLO_PAUSE_ASYM
;
1137 val
= swphy_read_reg(reg
, &fs
);
1138 if (reg
== MII_BMSR
) {
1139 if (!state
->an_complete
)
1140 val
&= ~BMSR_ANEGCOMPLETE
;
1142 val
&= ~BMSR_ANEGCAPABLE
;
1147 static int phylink_phy_read(struct phylink
*pl
, unsigned int phy_id
,
1150 struct phy_device
*phydev
= pl
->phydev
;
1153 if (mdio_phy_id_is_c45(phy_id
)) {
1154 prtad
= mdio_phy_id_prtad(phy_id
);
1155 devad
= mdio_phy_id_devad(phy_id
);
1156 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1157 } else if (phydev
->is_c45
) {
1163 devad
= __ffs(phydev
->c45_ids
.devices_in_package
);
1167 if (!(phydev
->c45_ids
.devices_in_package
& MDIO_DEVS_AN
))
1169 devad
= MDIO_MMD_AN
;
1170 if (reg
== MII_ADVERTISE
)
1171 reg
= MDIO_AN_ADVERTISE
;
1179 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1184 return mdiobus_read(pl
->phydev
->mdio
.bus
, prtad
, devad
);
1187 static int phylink_phy_write(struct phylink
*pl
, unsigned int phy_id
,
1188 unsigned int reg
, unsigned int val
)
1190 struct phy_device
*phydev
= pl
->phydev
;
1193 if (mdio_phy_id_is_c45(phy_id
)) {
1194 prtad
= mdio_phy_id_prtad(phy_id
);
1195 devad
= mdio_phy_id_devad(phy_id
);
1196 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1197 } else if (phydev
->is_c45
) {
1203 devad
= __ffs(phydev
->c45_ids
.devices_in_package
);
1207 if (!(phydev
->c45_ids
.devices_in_package
& MDIO_DEVS_AN
))
1209 devad
= MDIO_MMD_AN
;
1210 if (reg
== MII_ADVERTISE
)
1211 reg
= MDIO_AN_ADVERTISE
;
1219 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1225 return mdiobus_write(phydev
->mdio
.bus
, prtad
, devad
, val
);
1228 static int phylink_mii_read(struct phylink
*pl
, unsigned int phy_id
,
1231 struct phylink_link_state state
;
1234 switch (pl
->link_an_mode
) {
1237 phylink_get_fixed_state(pl
, &state
);
1238 val
= phylink_mii_emul_read(pl
->netdev
, reg
, &state
,
1247 /* No phy, fall through to 8023z method */
1250 val
= phylink_get_mac_state(pl
, &state
);
1254 val
= phylink_mii_emul_read(pl
->netdev
, reg
, &state
,
1260 return val
& 0xffff;
1263 static int phylink_mii_write(struct phylink
*pl
, unsigned int phy_id
,
1264 unsigned int reg
, unsigned int val
)
1266 switch (pl
->link_an_mode
) {
1274 /* No phy, fall through to 8023z method */
1282 int phylink_mii_ioctl(struct phylink
*pl
, struct ifreq
*ifr
, int cmd
)
1284 struct mii_ioctl_data
*mii
= if_mii(ifr
);
1287 WARN_ON(!lockdep_rtnl_is_held());
1290 /* PHYs only exist for MLO_AN_PHY and MLO_AN_SGMII */
1293 mii
->phy_id
= pl
->phydev
->mdio
.addr
;
1296 ret
= phylink_phy_read(pl
, mii
->phy_id
, mii
->reg_num
);
1304 ret
= phylink_phy_write(pl
, mii
->phy_id
, mii
->reg_num
,
1309 ret
= phy_mii_ioctl(pl
->phydev
, ifr
, cmd
);
1318 ret
= phylink_mii_read(pl
, mii
->phy_id
, mii
->reg_num
);
1326 ret
= phylink_mii_write(pl
, mii
->phy_id
, mii
->reg_num
,
1338 EXPORT_SYMBOL_GPL(phylink_mii_ioctl
);
1342 static int phylink_sfp_module_insert(void *upstream
,
1343 const struct sfp_eeprom_id
*id
)
1345 struct phylink
*pl
= upstream
;
1346 __ETHTOOL_DECLARE_LINK_MODE_MASK(support
) = { 0, };
1347 struct phylink_link_state config
;
1348 phy_interface_t iface
;
1353 sfp_parse_support(pl
->sfp_bus
, id
, support
);
1354 port
= sfp_parse_port(pl
->sfp_bus
, id
, support
);
1355 iface
= sfp_parse_interface(pl
->sfp_bus
, id
);
1357 WARN_ON(!lockdep_rtnl_is_held());
1360 case PHY_INTERFACE_MODE_SGMII
:
1361 mode
= MLO_AN_SGMII
;
1363 case PHY_INTERFACE_MODE_1000BASEX
:
1364 mode
= MLO_AN_8023Z
;
1370 memset(&config
, 0, sizeof(config
));
1371 linkmode_copy(config
.advertising
, support
);
1372 config
.interface
= iface
;
1373 config
.speed
= SPEED_UNKNOWN
;
1374 config
.duplex
= DUPLEX_UNKNOWN
;
1375 config
.pause
= MLO_PAUSE_AN
;
1376 config
.an_enabled
= pl
->link_config
.an_enabled
;
1378 /* Ignore errors if we're expecting a PHY to attach later */
1379 ret
= phylink_validate(pl
, support
, &config
);
1381 netdev_err(pl
->netdev
, "validation of %s/%s with support %*pb failed: %d\n",
1382 phylink_an_mode_str(mode
), phy_modes(config
.interface
),
1383 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
, ret
);
1387 netdev_dbg(pl
->netdev
, "requesting link mode %s/%s with support %*pb\n",
1388 phylink_an_mode_str(mode
), phy_modes(config
.interface
),
1389 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
);
1391 if (mode
== MLO_AN_8023Z
&& pl
->phydev
)
1394 changed
= !bitmap_equal(pl
->supported
, support
,
1395 __ETHTOOL_LINK_MODE_MASK_NBITS
);
1397 linkmode_copy(pl
->supported
, support
);
1398 linkmode_copy(pl
->link_config
.advertising
, config
.advertising
);
1401 if (pl
->link_an_mode
!= mode
||
1402 pl
->link_config
.interface
!= config
.interface
) {
1403 pl
->link_config
.interface
= config
.interface
;
1404 pl
->link_an_mode
= mode
;
1408 netdev_info(pl
->netdev
, "switched to %s/%s link mode\n",
1409 phylink_an_mode_str(mode
),
1410 phy_modes(config
.interface
));
1413 pl
->link_port
= port
;
1415 if (changed
&& !test_bit(PHYLINK_DISABLE_STOPPED
,
1416 &pl
->phylink_disable_state
))
1417 phylink_mac_config(pl
, &pl
->link_config
);
1422 static void phylink_sfp_link_down(void *upstream
)
1424 struct phylink
*pl
= upstream
;
1426 WARN_ON(!lockdep_rtnl_is_held());
1428 set_bit(PHYLINK_DISABLE_LINK
, &pl
->phylink_disable_state
);
1429 flush_work(&pl
->resolve
);
1431 netif_carrier_off(pl
->netdev
);
1434 static void phylink_sfp_link_up(void *upstream
)
1436 struct phylink
*pl
= upstream
;
1438 WARN_ON(!lockdep_rtnl_is_held());
1440 clear_bit(PHYLINK_DISABLE_LINK
, &pl
->phylink_disable_state
);
1441 phylink_run_resolve(pl
);
1444 static int phylink_sfp_connect_phy(void *upstream
, struct phy_device
*phy
)
1446 return phylink_connect_phy(upstream
, phy
);
1449 static void phylink_sfp_disconnect_phy(void *upstream
)
1451 phylink_disconnect_phy(upstream
);
1454 static const struct sfp_upstream_ops sfp_phylink_ops
= {
1455 .module_insert
= phylink_sfp_module_insert
,
1456 .link_up
= phylink_sfp_link_up
,
1457 .link_down
= phylink_sfp_link_down
,
1458 .connect_phy
= phylink_sfp_connect_phy
,
1459 .disconnect_phy
= phylink_sfp_disconnect_phy
,
1462 MODULE_LICENSE("GPL");