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
;
45 unsigned long phylink_disable_state
; /* bitmask of disables */
46 struct phy_device
*phydev
;
47 phy_interface_t link_interface
; /* PHY_INTERFACE_xxx */
48 u8 link_an_mode
; /* MLO_AN_xxx */
49 u8 link_port
; /* The current non-phy ethtool port */
50 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
52 /* The link configuration settings */
53 struct phylink_link_state link_config
;
55 /* The current settings */
56 phy_interface_t cur_interface
;
58 struct gpio_desc
*link_gpio
;
59 struct timer_list link_poll
;
60 void (*get_fixed_state
)(struct net_device
*dev
,
61 struct phylink_link_state
*s
);
63 struct mutex state_mutex
;
64 struct phylink_link_state phy_state
;
65 struct work_struct resolve
;
67 bool mac_link_dropped
;
69 struct sfp_bus
*sfp_bus
;
73 * phylink_set_port_modes() - set the port type modes in the ethtool mask
74 * @mask: ethtool link mode mask
76 * Sets all the port type modes in the ethtool mask. MAC drivers should
77 * use this in their 'validate' callback.
79 void phylink_set_port_modes(unsigned long *mask
)
81 phylink_set(mask
, TP
);
82 phylink_set(mask
, AUI
);
83 phylink_set(mask
, MII
);
84 phylink_set(mask
, FIBRE
);
85 phylink_set(mask
, BNC
);
86 phylink_set(mask
, Backplane
);
88 EXPORT_SYMBOL_GPL(phylink_set_port_modes
);
90 static int phylink_is_empty_linkmode(const unsigned long *linkmode
)
92 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp
) = { 0, };
94 phylink_set_port_modes(tmp
);
95 phylink_set(tmp
, Autoneg
);
96 phylink_set(tmp
, Pause
);
97 phylink_set(tmp
, Asym_Pause
);
99 bitmap_andnot(tmp
, linkmode
, tmp
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
101 return linkmode_empty(tmp
);
104 static const char *phylink_an_mode_str(unsigned int mode
)
106 static const char *modestr
[] = {
107 [MLO_AN_PHY
] = "phy",
108 [MLO_AN_FIXED
] = "fixed",
109 [MLO_AN_INBAND
] = "inband",
112 return mode
< ARRAY_SIZE(modestr
) ? modestr
[mode
] : "unknown";
115 static int phylink_validate(struct phylink
*pl
, unsigned long *supported
,
116 struct phylink_link_state
*state
)
118 pl
->ops
->validate(pl
->netdev
, supported
, state
);
120 return phylink_is_empty_linkmode(supported
) ? -EINVAL
: 0;
123 static int phylink_parse_fixedlink(struct phylink
*pl
,
124 struct fwnode_handle
*fwnode
)
126 struct fwnode_handle
*fixed_node
;
127 const struct phy_setting
*s
;
128 struct gpio_desc
*desc
;
132 fixed_node
= fwnode_get_named_child_node(fwnode
, "fixed-link");
134 ret
= fwnode_property_read_u32(fixed_node
, "speed", &speed
);
136 pl
->link_config
.speed
= speed
;
137 pl
->link_config
.duplex
= DUPLEX_HALF
;
139 if (fwnode_property_read_bool(fixed_node
, "full-duplex"))
140 pl
->link_config
.duplex
= DUPLEX_FULL
;
142 /* We treat the "pause" and "asym-pause" terminology as
143 * defining the link partner's ability. */
144 if (fwnode_property_read_bool(fixed_node
, "pause"))
145 pl
->link_config
.pause
|= MLO_PAUSE_SYM
;
146 if (fwnode_property_read_bool(fixed_node
, "asym-pause"))
147 pl
->link_config
.pause
|= MLO_PAUSE_ASYM
;
150 desc
= fwnode_get_named_gpiod(fixed_node
, "link-gpios",
154 pl
->link_gpio
= desc
;
155 else if (desc
== ERR_PTR(-EPROBE_DEFER
))
158 fwnode_handle_put(fixed_node
);
165 ret
= fwnode_property_read_u32_array(fwnode
, "fixed-link",
167 if (ret
!= ARRAY_SIZE(prop
)) {
168 netdev_err(pl
->netdev
, "broken fixed-link?\n");
172 ret
= fwnode_property_read_u32_array(fwnode
, "fixed-link",
173 prop
, ARRAY_SIZE(prop
));
175 pl
->link_config
.duplex
= prop
[1] ?
176 DUPLEX_FULL
: DUPLEX_HALF
;
177 pl
->link_config
.speed
= prop
[2];
179 pl
->link_config
.pause
|= MLO_PAUSE_SYM
;
181 pl
->link_config
.pause
|= MLO_PAUSE_ASYM
;
185 if (pl
->link_config
.speed
> SPEED_1000
&&
186 pl
->link_config
.duplex
!= DUPLEX_FULL
)
187 netdev_warn(pl
->netdev
, "fixed link specifies half duplex for %dMbps link?\n",
188 pl
->link_config
.speed
);
190 bitmap_fill(pl
->supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
191 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
192 phylink_validate(pl
, pl
->supported
, &pl
->link_config
);
194 s
= phy_lookup_setting(pl
->link_config
.speed
, pl
->link_config
.duplex
,
195 pl
->supported
, true);
196 linkmode_zero(pl
->supported
);
197 phylink_set(pl
->supported
, MII
);
199 __set_bit(s
->bit
, pl
->supported
);
201 netdev_warn(pl
->netdev
, "fixed link %s duplex %dMbps not recognised\n",
202 pl
->link_config
.duplex
== DUPLEX_FULL
? "full" : "half",
203 pl
->link_config
.speed
);
206 linkmode_and(pl
->link_config
.advertising
, pl
->link_config
.advertising
,
209 pl
->link_config
.link
= 1;
210 pl
->link_config
.an_complete
= 1;
215 static int phylink_parse_mode(struct phylink
*pl
, struct fwnode_handle
*fwnode
)
217 struct fwnode_handle
*dn
;
220 dn
= fwnode_get_named_child_node(fwnode
, "fixed-link");
221 if (dn
|| fwnode_property_present(fwnode
, "fixed-link"))
222 pl
->link_an_mode
= MLO_AN_FIXED
;
223 fwnode_handle_put(dn
);
225 if (fwnode_property_read_string(fwnode
, "managed", &managed
) == 0 &&
226 strcmp(managed
, "in-band-status") == 0) {
227 if (pl
->link_an_mode
== MLO_AN_FIXED
) {
228 netdev_err(pl
->netdev
,
229 "can't use both fixed-link and in-band-status\n");
233 linkmode_zero(pl
->supported
);
234 phylink_set(pl
->supported
, MII
);
235 phylink_set(pl
->supported
, Autoneg
);
236 phylink_set(pl
->supported
, Asym_Pause
);
237 phylink_set(pl
->supported
, Pause
);
238 pl
->link_config
.an_enabled
= true;
239 pl
->link_an_mode
= MLO_AN_INBAND
;
241 switch (pl
->link_config
.interface
) {
242 case PHY_INTERFACE_MODE_SGMII
:
243 phylink_set(pl
->supported
, 10baseT_Half
);
244 phylink_set(pl
->supported
, 10baseT_Full
);
245 phylink_set(pl
->supported
, 100baseT_Half
);
246 phylink_set(pl
->supported
, 100baseT_Full
);
247 phylink_set(pl
->supported
, 1000baseT_Half
);
248 phylink_set(pl
->supported
, 1000baseT_Full
);
251 case PHY_INTERFACE_MODE_1000BASEX
:
252 phylink_set(pl
->supported
, 1000baseX_Full
);
255 case PHY_INTERFACE_MODE_2500BASEX
:
256 phylink_set(pl
->supported
, 2500baseX_Full
);
259 case PHY_INTERFACE_MODE_10GKR
:
260 phylink_set(pl
->supported
, 10baseT_Half
);
261 phylink_set(pl
->supported
, 10baseT_Full
);
262 phylink_set(pl
->supported
, 100baseT_Half
);
263 phylink_set(pl
->supported
, 100baseT_Full
);
264 phylink_set(pl
->supported
, 1000baseT_Half
);
265 phylink_set(pl
->supported
, 1000baseT_Full
);
266 phylink_set(pl
->supported
, 1000baseX_Full
);
267 phylink_set(pl
->supported
, 10000baseKR_Full
);
268 phylink_set(pl
->supported
, 10000baseCR_Full
);
269 phylink_set(pl
->supported
, 10000baseSR_Full
);
270 phylink_set(pl
->supported
, 10000baseLR_Full
);
271 phylink_set(pl
->supported
, 10000baseLRM_Full
);
272 phylink_set(pl
->supported
, 10000baseER_Full
);
276 netdev_err(pl
->netdev
,
277 "incorrect link mode %s for in-band status\n",
278 phy_modes(pl
->link_config
.interface
));
282 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
284 if (phylink_validate(pl
, pl
->supported
, &pl
->link_config
)) {
285 netdev_err(pl
->netdev
,
286 "failed to validate link configuration for in-band status\n");
294 static void phylink_mac_config(struct phylink
*pl
,
295 const struct phylink_link_state
*state
)
297 netdev_dbg(pl
->netdev
,
298 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
299 __func__
, phylink_an_mode_str(pl
->link_an_mode
),
300 phy_modes(state
->interface
),
301 phy_speed_to_str(state
->speed
),
302 phy_duplex_to_str(state
->duplex
),
303 __ETHTOOL_LINK_MODE_MASK_NBITS
, state
->advertising
,
304 state
->pause
, state
->link
, state
->an_enabled
);
306 pl
->ops
->mac_config(pl
->netdev
, pl
->link_an_mode
, state
);
309 static void phylink_mac_config_up(struct phylink
*pl
,
310 const struct phylink_link_state
*state
)
313 phylink_mac_config(pl
, state
);
316 static void phylink_mac_an_restart(struct phylink
*pl
)
318 if (pl
->link_config
.an_enabled
&&
319 phy_interface_mode_is_8023z(pl
->link_config
.interface
))
320 pl
->ops
->mac_an_restart(pl
->netdev
);
323 static int phylink_get_mac_state(struct phylink
*pl
, struct phylink_link_state
*state
)
325 struct net_device
*ndev
= pl
->netdev
;
327 linkmode_copy(state
->advertising
, pl
->link_config
.advertising
);
328 linkmode_zero(state
->lp_advertising
);
329 state
->interface
= pl
->link_config
.interface
;
330 state
->an_enabled
= pl
->link_config
.an_enabled
;
331 state
->speed
= SPEED_UNKNOWN
;
332 state
->duplex
= DUPLEX_UNKNOWN
;
333 state
->pause
= MLO_PAUSE_NONE
;
334 state
->an_complete
= 0;
337 return pl
->ops
->mac_link_state(ndev
, state
);
340 /* The fixed state is... fixed except for the link state,
341 * which may be determined by a GPIO or a callback.
343 static void phylink_get_fixed_state(struct phylink
*pl
, struct phylink_link_state
*state
)
345 *state
= pl
->link_config
;
346 if (pl
->get_fixed_state
)
347 pl
->get_fixed_state(pl
->netdev
, state
);
348 else if (pl
->link_gpio
)
349 state
->link
= !!gpiod_get_value_cansleep(pl
->link_gpio
);
352 /* Flow control is resolved according to our and the link partners
353 * advertisements using the following drawn from the 802.3 specs:
354 * Local device Link partner
355 * Pause AsymDir Pause AsymDir Result
360 static void phylink_resolve_flow(struct phylink
*pl
,
361 struct phylink_link_state
*state
)
365 if (pl
->link_config
.pause
& MLO_PAUSE_AN
) {
368 if (phylink_test(pl
->link_config
.advertising
, Pause
))
369 pause
|= MLO_PAUSE_SYM
;
370 if (phylink_test(pl
->link_config
.advertising
, Asym_Pause
))
371 pause
|= MLO_PAUSE_ASYM
;
373 pause
&= state
->pause
;
375 if (pause
& MLO_PAUSE_SYM
)
376 new_pause
= MLO_PAUSE_TX
| MLO_PAUSE_RX
;
377 else if (pause
& MLO_PAUSE_ASYM
)
378 new_pause
= state
->pause
& MLO_PAUSE_SYM
?
379 MLO_PAUSE_RX
: MLO_PAUSE_TX
;
381 new_pause
= pl
->link_config
.pause
& MLO_PAUSE_TXRX_MASK
;
384 state
->pause
&= ~MLO_PAUSE_TXRX_MASK
;
385 state
->pause
|= new_pause
;
388 static const char *phylink_pause_to_str(int pause
)
390 switch (pause
& MLO_PAUSE_TXRX_MASK
) {
391 case MLO_PAUSE_TX
| MLO_PAUSE_RX
:
402 static void phylink_resolve(struct work_struct
*w
)
404 struct phylink
*pl
= container_of(w
, struct phylink
, resolve
);
405 struct phylink_link_state link_state
;
406 struct net_device
*ndev
= pl
->netdev
;
408 mutex_lock(&pl
->state_mutex
);
409 if (pl
->phylink_disable_state
) {
410 pl
->mac_link_dropped
= false;
411 link_state
.link
= false;
412 } else if (pl
->mac_link_dropped
) {
413 link_state
.link
= false;
415 switch (pl
->link_an_mode
) {
417 link_state
= pl
->phy_state
;
418 phylink_resolve_flow(pl
, &link_state
);
419 phylink_mac_config_up(pl
, &link_state
);
423 phylink_get_fixed_state(pl
, &link_state
);
424 phylink_mac_config_up(pl
, &link_state
);
428 phylink_get_mac_state(pl
, &link_state
);
430 /* If we have a phy, the "up" state is the union of
431 * both the PHY and the MAC */
433 link_state
.link
&= pl
->phy_state
.link
;
435 /* Only update if the PHY link is up */
436 if (pl
->phydev
&& pl
->phy_state
.link
) {
437 link_state
.interface
= pl
->phy_state
.interface
;
439 /* If we have a PHY, we need to update with
440 * the pause mode bits. */
441 link_state
.pause
|= pl
->phy_state
.pause
;
442 phylink_resolve_flow(pl
, &link_state
);
443 phylink_mac_config(pl
, &link_state
);
449 if (link_state
.link
!= netif_carrier_ok(ndev
)) {
450 if (!link_state
.link
) {
451 netif_carrier_off(ndev
);
452 pl
->ops
->mac_link_down(ndev
, pl
->link_an_mode
,
454 netdev_info(ndev
, "Link is Down\n");
456 pl
->cur_interface
= link_state
.interface
;
457 pl
->ops
->mac_link_up(ndev
, pl
->link_an_mode
,
458 pl
->cur_interface
, pl
->phydev
);
460 netif_carrier_on(ndev
);
463 "Link is Up - %s/%s - flow control %s\n",
464 phy_speed_to_str(link_state
.speed
),
465 phy_duplex_to_str(link_state
.duplex
),
466 phylink_pause_to_str(link_state
.pause
));
469 if (!link_state
.link
&& pl
->mac_link_dropped
) {
470 pl
->mac_link_dropped
= false;
471 queue_work(system_power_efficient_wq
, &pl
->resolve
);
473 mutex_unlock(&pl
->state_mutex
);
476 static void phylink_run_resolve(struct phylink
*pl
)
478 if (!pl
->phylink_disable_state
)
479 queue_work(system_power_efficient_wq
, &pl
->resolve
);
482 static void phylink_run_resolve_and_disable(struct phylink
*pl
, int bit
)
484 unsigned long state
= pl
->phylink_disable_state
;
486 set_bit(bit
, &pl
->phylink_disable_state
);
488 queue_work(system_power_efficient_wq
, &pl
->resolve
);
489 flush_work(&pl
->resolve
);
493 static void phylink_fixed_poll(struct timer_list
*t
)
495 struct phylink
*pl
= container_of(t
, struct phylink
, link_poll
);
497 mod_timer(t
, jiffies
+ HZ
);
499 phylink_run_resolve(pl
);
502 static const struct sfp_upstream_ops sfp_phylink_ops
;
504 static int phylink_register_sfp(struct phylink
*pl
,
505 struct fwnode_handle
*fwnode
)
507 struct fwnode_reference_args ref
;
513 ret
= fwnode_property_get_reference_args(fwnode
, "sfp", NULL
,
519 netdev_err(pl
->netdev
, "unable to parse \"sfp\" node: %d\n",
524 pl
->sfp_bus
= sfp_register_upstream(ref
.fwnode
, pl
->netdev
, pl
,
533 * phylink_create() - create a phylink instance
534 * @ndev: a pointer to the &struct net_device
535 * @fwnode: a pointer to a &struct fwnode_handle describing the network
537 * @iface: the desired link mode defined by &typedef phy_interface_t
538 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
540 * Create a new phylink instance, and parse the link parameters found in @np.
541 * This will parse in-band modes, fixed-link or SFP configuration.
543 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
544 * must use IS_ERR() to check for errors from this function.
546 struct phylink
*phylink_create(struct net_device
*ndev
,
547 struct fwnode_handle
*fwnode
,
548 phy_interface_t iface
,
549 const struct phylink_mac_ops
*ops
)
554 pl
= kzalloc(sizeof(*pl
), GFP_KERNEL
);
556 return ERR_PTR(-ENOMEM
);
558 mutex_init(&pl
->state_mutex
);
559 INIT_WORK(&pl
->resolve
, phylink_resolve
);
561 pl
->phy_state
.interface
= iface
;
562 pl
->link_interface
= iface
;
563 if (iface
== PHY_INTERFACE_MODE_MOCA
)
564 pl
->link_port
= PORT_BNC
;
566 pl
->link_port
= PORT_MII
;
567 pl
->link_config
.interface
= iface
;
568 pl
->link_config
.pause
= MLO_PAUSE_AN
;
569 pl
->link_config
.speed
= SPEED_UNKNOWN
;
570 pl
->link_config
.duplex
= DUPLEX_UNKNOWN
;
571 pl
->link_config
.an_enabled
= true;
573 __set_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
574 timer_setup(&pl
->link_poll
, phylink_fixed_poll
, 0);
576 bitmap_fill(pl
->supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
577 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
578 phylink_validate(pl
, pl
->supported
, &pl
->link_config
);
580 ret
= phylink_parse_mode(pl
, fwnode
);
586 if (pl
->link_an_mode
== MLO_AN_FIXED
) {
587 ret
= phylink_parse_fixedlink(pl
, fwnode
);
594 ret
= phylink_register_sfp(pl
, fwnode
);
602 EXPORT_SYMBOL_GPL(phylink_create
);
605 * phylink_destroy() - cleanup and destroy the phylink instance
606 * @pl: a pointer to a &struct phylink returned from phylink_create()
608 * Destroy a phylink instance. Any PHY that has been attached must have been
609 * cleaned up via phylink_disconnect_phy() prior to calling this function.
611 void phylink_destroy(struct phylink
*pl
)
614 sfp_unregister_upstream(pl
->sfp_bus
);
615 if (!IS_ERR_OR_NULL(pl
->link_gpio
))
616 gpiod_put(pl
->link_gpio
);
618 cancel_work_sync(&pl
->resolve
);
621 EXPORT_SYMBOL_GPL(phylink_destroy
);
623 static void phylink_phy_change(struct phy_device
*phydev
, bool up
,
626 struct phylink
*pl
= phydev
->phylink
;
628 mutex_lock(&pl
->state_mutex
);
629 pl
->phy_state
.speed
= phydev
->speed
;
630 pl
->phy_state
.duplex
= phydev
->duplex
;
631 pl
->phy_state
.pause
= MLO_PAUSE_NONE
;
633 pl
->phy_state
.pause
|= MLO_PAUSE_SYM
;
634 if (phydev
->asym_pause
)
635 pl
->phy_state
.pause
|= MLO_PAUSE_ASYM
;
636 pl
->phy_state
.interface
= phydev
->interface
;
637 pl
->phy_state
.link
= up
;
638 pl
->phy_state
.an_enabled
= phydev
->autoneg
;
639 mutex_unlock(&pl
->state_mutex
);
641 phylink_run_resolve(pl
);
643 netdev_dbg(pl
->netdev
, "phy link %s %s/%s/%s\n", up
? "up" : "down",
644 phy_modes(phydev
->interface
),
645 phy_speed_to_str(phydev
->speed
),
646 phy_duplex_to_str(phydev
->duplex
));
649 static int phylink_bringup_phy(struct phylink
*pl
, struct phy_device
*phy
)
651 struct phylink_link_state config
;
652 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
655 memset(&config
, 0, sizeof(config
));
656 linkmode_copy(supported
, phy
->supported
);
657 linkmode_copy(config
.advertising
, phy
->advertising
);
658 config
.interface
= pl
->link_config
.interface
;
661 * This is the new way of dealing with flow control for PHYs,
662 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
663 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
664 * using our validate call to the MAC, we rely upon the MAC
665 * clearing the bits from both supported and advertising fields.
667 if (phylink_test(supported
, Pause
))
668 phylink_set(config
.advertising
, Pause
);
669 if (phylink_test(supported
, Asym_Pause
))
670 phylink_set(config
.advertising
, Asym_Pause
);
672 ret
= phylink_validate(pl
, supported
, &config
);
677 phy
->phy_link_change
= phylink_phy_change
;
679 netdev_info(pl
->netdev
,
680 "PHY [%s] driver [%s]\n", dev_name(&phy
->mdio
.dev
),
683 mutex_lock(&phy
->lock
);
684 mutex_lock(&pl
->state_mutex
);
686 linkmode_copy(pl
->supported
, supported
);
687 linkmode_copy(pl
->link_config
.advertising
, config
.advertising
);
689 /* Restrict the phy advertisement according to the MAC support. */
690 linkmode_copy(phy
->advertising
, config
.advertising
);
691 mutex_unlock(&pl
->state_mutex
);
692 mutex_unlock(&phy
->lock
);
694 netdev_dbg(pl
->netdev
,
695 "phy: setting supported %*pb advertising %*pb\n",
696 __ETHTOOL_LINK_MODE_MASK_NBITS
, pl
->supported
,
697 __ETHTOOL_LINK_MODE_MASK_NBITS
, phy
->advertising
);
699 if (phy_interrupt_is_valid(phy
))
700 phy_request_interrupt(phy
);
705 static int __phylink_connect_phy(struct phylink
*pl
, struct phy_device
*phy
,
706 phy_interface_t interface
)
710 if (WARN_ON(pl
->link_an_mode
== MLO_AN_FIXED
||
711 (pl
->link_an_mode
== MLO_AN_INBAND
&&
712 phy_interface_mode_is_8023z(interface
))))
718 ret
= phy_attach_direct(pl
->netdev
, phy
, 0, interface
);
722 ret
= phylink_bringup_phy(pl
, phy
);
730 * phylink_connect_phy() - connect a PHY to the phylink instance
731 * @pl: a pointer to a &struct phylink returned from phylink_create()
732 * @phy: a pointer to a &struct phy_device.
734 * Connect @phy to the phylink instance specified by @pl by calling
735 * phy_attach_direct(). Configure the @phy according to the MAC driver's
736 * capabilities, start the PHYLIB state machine and enable any interrupts
737 * that the PHY supports.
739 * This updates the phylink's ethtool supported and advertising link mode
742 * Returns 0 on success or a negative errno.
744 int phylink_connect_phy(struct phylink
*pl
, struct phy_device
*phy
)
746 /* Use PHY device/driver interface */
747 if (pl
->link_interface
== PHY_INTERFACE_MODE_NA
) {
748 pl
->link_interface
= phy
->interface
;
749 pl
->link_config
.interface
= pl
->link_interface
;
752 return __phylink_connect_phy(pl
, phy
, pl
->link_interface
);
754 EXPORT_SYMBOL_GPL(phylink_connect_phy
);
757 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
758 * @pl: a pointer to a &struct phylink returned from phylink_create()
759 * @dn: a pointer to a &struct device_node.
760 * @flags: PHY-specific flags to communicate to the PHY device driver
762 * Connect the phy specified in the device node @dn to the phylink instance
763 * specified by @pl. Actions specified in phylink_connect_phy() will be
766 * Returns 0 on success or a negative errno.
768 int phylink_of_phy_connect(struct phylink
*pl
, struct device_node
*dn
,
771 struct device_node
*phy_node
;
772 struct phy_device
*phy_dev
;
775 /* Fixed links and 802.3z are handled without needing a PHY */
776 if (pl
->link_an_mode
== MLO_AN_FIXED
||
777 (pl
->link_an_mode
== MLO_AN_INBAND
&&
778 phy_interface_mode_is_8023z(pl
->link_interface
)))
781 phy_node
= of_parse_phandle(dn
, "phy-handle", 0);
783 phy_node
= of_parse_phandle(dn
, "phy", 0);
785 phy_node
= of_parse_phandle(dn
, "phy-device", 0);
788 if (pl
->link_an_mode
== MLO_AN_PHY
)
793 phy_dev
= of_phy_attach(pl
->netdev
, phy_node
, flags
,
795 /* We're done with the phy_node handle */
796 of_node_put(phy_node
);
801 ret
= phylink_bringup_phy(pl
, phy_dev
);
807 EXPORT_SYMBOL_GPL(phylink_of_phy_connect
);
810 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
812 * @pl: a pointer to a &struct phylink returned from phylink_create()
814 * Disconnect any current PHY from the phylink instance described by @pl.
816 void phylink_disconnect_phy(struct phylink
*pl
)
818 struct phy_device
*phy
;
824 mutex_lock(&phy
->lock
);
825 mutex_lock(&pl
->state_mutex
);
827 mutex_unlock(&pl
->state_mutex
);
828 mutex_unlock(&phy
->lock
);
829 flush_work(&pl
->resolve
);
834 EXPORT_SYMBOL_GPL(phylink_disconnect_phy
);
837 * phylink_fixed_state_cb() - allow setting a fixed link callback
838 * @pl: a pointer to a &struct phylink returned from phylink_create()
839 * @cb: callback to execute to determine the fixed link state.
841 * The MAC driver should call this driver when the state of its link
842 * can be determined through e.g: an out of band MMIO register.
844 int phylink_fixed_state_cb(struct phylink
*pl
,
845 void (*cb
)(struct net_device
*dev
,
846 struct phylink_link_state
*state
))
848 /* It does not make sense to let the link be overriden unless we use
851 if (pl
->link_an_mode
!= MLO_AN_FIXED
)
854 mutex_lock(&pl
->state_mutex
);
855 pl
->get_fixed_state
= cb
;
856 mutex_unlock(&pl
->state_mutex
);
860 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb
);
863 * phylink_mac_change() - notify phylink of a change in MAC state
864 * @pl: a pointer to a &struct phylink returned from phylink_create()
865 * @up: indicates whether the link is currently up.
867 * The MAC driver should call this driver when the state of its link
868 * changes (eg, link failure, new negotiation results, etc.)
870 void phylink_mac_change(struct phylink
*pl
, bool up
)
873 pl
->mac_link_dropped
= true;
874 phylink_run_resolve(pl
);
875 netdev_dbg(pl
->netdev
, "mac link %s\n", up
? "up" : "down");
877 EXPORT_SYMBOL_GPL(phylink_mac_change
);
880 * phylink_start() - start a phylink instance
881 * @pl: a pointer to a &struct phylink returned from phylink_create()
883 * Start the phylink instance specified by @pl, configuring the MAC for the
884 * desired link mode(s) and negotiation style. This should be called from the
885 * network device driver's &struct net_device_ops ndo_open() method.
887 void phylink_start(struct phylink
*pl
)
891 netdev_info(pl
->netdev
, "configuring for %s/%s link mode\n",
892 phylink_an_mode_str(pl
->link_an_mode
),
893 phy_modes(pl
->link_config
.interface
));
895 /* Always set the carrier off */
896 netif_carrier_off(pl
->netdev
);
898 /* Apply the link configuration to the MAC when starting. This allows
899 * a fixed-link to start with the correct parameters, and also
900 * ensures that we set the appropriate advertisement for Serdes links.
902 phylink_resolve_flow(pl
, &pl
->link_config
);
903 phylink_mac_config(pl
, &pl
->link_config
);
905 /* Restart autonegotiation if using 802.3z to ensure that the link
906 * parameters are properly negotiated. This is necessary for DSA
907 * switches using 802.3z negotiation to ensure they see our modes.
909 phylink_mac_an_restart(pl
);
911 clear_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
912 phylink_run_resolve(pl
);
914 if (pl
->link_an_mode
== MLO_AN_FIXED
&& !IS_ERR(pl
->link_gpio
))
915 mod_timer(&pl
->link_poll
, jiffies
+ HZ
);
917 sfp_upstream_start(pl
->sfp_bus
);
919 phy_start(pl
->phydev
);
921 EXPORT_SYMBOL_GPL(phylink_start
);
924 * phylink_stop() - stop a phylink instance
925 * @pl: a pointer to a &struct phylink returned from phylink_create()
927 * Stop the phylink instance specified by @pl. This should be called from the
928 * network device driver's &struct net_device_ops ndo_stop() method. The
929 * network device's carrier state should not be changed prior to calling this
932 void phylink_stop(struct phylink
*pl
)
937 phy_stop(pl
->phydev
);
939 sfp_upstream_stop(pl
->sfp_bus
);
940 if (pl
->link_an_mode
== MLO_AN_FIXED
&& !IS_ERR(pl
->link_gpio
))
941 del_timer_sync(&pl
->link_poll
);
943 phylink_run_resolve_and_disable(pl
, PHYLINK_DISABLE_STOPPED
);
945 EXPORT_SYMBOL_GPL(phylink_stop
);
948 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
949 * @pl: a pointer to a &struct phylink returned from phylink_create()
950 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
952 * Read the wake on lan parameters from the PHY attached to the phylink
953 * instance specified by @pl. If no PHY is currently attached, report no
954 * support for wake on lan.
956 void phylink_ethtool_get_wol(struct phylink
*pl
, struct ethtool_wolinfo
*wol
)
964 phy_ethtool_get_wol(pl
->phydev
, wol
);
966 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol
);
969 * phylink_ethtool_set_wol() - set wake on lan parameters
970 * @pl: a pointer to a &struct phylink returned from phylink_create()
971 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
973 * Set the wake on lan parameters for the PHY attached to the phylink
974 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
977 * Returns zero on success or negative errno code.
979 int phylink_ethtool_set_wol(struct phylink
*pl
, struct ethtool_wolinfo
*wol
)
981 int ret
= -EOPNOTSUPP
;
986 ret
= phy_ethtool_set_wol(pl
->phydev
, wol
);
990 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol
);
992 static void phylink_merge_link_mode(unsigned long *dst
, const unsigned long *b
)
994 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
);
997 phylink_set_port_modes(mask
);
999 linkmode_and(dst
, dst
, mask
);
1000 linkmode_or(dst
, dst
, b
);
1003 static void phylink_get_ksettings(const struct phylink_link_state
*state
,
1004 struct ethtool_link_ksettings
*kset
)
1006 phylink_merge_link_mode(kset
->link_modes
.advertising
, state
->advertising
);
1007 linkmode_copy(kset
->link_modes
.lp_advertising
, state
->lp_advertising
);
1008 kset
->base
.speed
= state
->speed
;
1009 kset
->base
.duplex
= state
->duplex
;
1010 kset
->base
.autoneg
= state
->an_enabled
? AUTONEG_ENABLE
:
1015 * phylink_ethtool_ksettings_get() - get the current link settings
1016 * @pl: a pointer to a &struct phylink returned from phylink_create()
1017 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1019 * Read the current link settings for the phylink instance specified by @pl.
1020 * This will be the link settings read from the MAC, PHY or fixed link
1021 * settings depending on the current negotiation mode.
1023 int phylink_ethtool_ksettings_get(struct phylink
*pl
,
1024 struct ethtool_link_ksettings
*kset
)
1026 struct phylink_link_state link_state
;
1031 phy_ethtool_ksettings_get(pl
->phydev
, kset
);
1033 kset
->base
.port
= pl
->link_port
;
1036 linkmode_copy(kset
->link_modes
.supported
, pl
->supported
);
1038 switch (pl
->link_an_mode
) {
1040 /* We are using fixed settings. Report these as the
1041 * current link settings - and note that these also
1042 * represent the supported speeds/duplex/pause modes.
1044 phylink_get_fixed_state(pl
, &link_state
);
1045 phylink_get_ksettings(&link_state
, kset
);
1049 /* If there is a phy attached, then use the reported
1050 * settings from the phy with no modification.
1055 phylink_get_mac_state(pl
, &link_state
);
1057 /* The MAC is reporting the link results from its own PCS
1058 * layer via in-band status. Report these as the current
1061 phylink_get_ksettings(&link_state
, kset
);
1067 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get
);
1070 * phylink_ethtool_ksettings_set() - set the link settings
1071 * @pl: a pointer to a &struct phylink returned from phylink_create()
1072 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1074 int phylink_ethtool_ksettings_set(struct phylink
*pl
,
1075 const struct ethtool_link_ksettings
*kset
)
1077 __ETHTOOL_DECLARE_LINK_MODE_MASK(support
);
1078 struct ethtool_link_ksettings our_kset
;
1079 struct phylink_link_state config
;
1084 if (kset
->base
.autoneg
!= AUTONEG_DISABLE
&&
1085 kset
->base
.autoneg
!= AUTONEG_ENABLE
)
1088 linkmode_copy(support
, pl
->supported
);
1089 config
= pl
->link_config
;
1091 /* Mask out unsupported advertisements */
1092 linkmode_and(config
.advertising
, kset
->link_modes
.advertising
,
1095 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1096 if (kset
->base
.autoneg
== AUTONEG_DISABLE
) {
1097 const struct phy_setting
*s
;
1099 /* Autonegotiation disabled, select a suitable speed and
1102 s
= phy_lookup_setting(kset
->base
.speed
, kset
->base
.duplex
,
1107 /* If we have a fixed link (as specified by firmware), refuse
1108 * to change link parameters.
1110 if (pl
->link_an_mode
== MLO_AN_FIXED
&&
1111 (s
->speed
!= pl
->link_config
.speed
||
1112 s
->duplex
!= pl
->link_config
.duplex
))
1115 config
.speed
= s
->speed
;
1116 config
.duplex
= s
->duplex
;
1117 config
.an_enabled
= false;
1119 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, config
.advertising
);
1121 /* If we have a fixed link, refuse to enable autonegotiation */
1122 if (pl
->link_an_mode
== MLO_AN_FIXED
)
1125 config
.speed
= SPEED_UNKNOWN
;
1126 config
.duplex
= DUPLEX_UNKNOWN
;
1127 config
.an_enabled
= true;
1129 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, config
.advertising
);
1132 if (phylink_validate(pl
, support
, &config
))
1135 /* If autonegotiation is enabled, we must have an advertisement */
1136 if (config
.an_enabled
&& phylink_is_empty_linkmode(config
.advertising
))
1140 linkmode_copy(our_kset
.link_modes
.advertising
, config
.advertising
);
1141 our_kset
.base
.speed
= config
.speed
;
1142 our_kset
.base
.duplex
= config
.duplex
;
1144 /* If we have a PHY, configure the phy */
1146 ret
= phy_ethtool_ksettings_set(pl
->phydev
, &our_kset
);
1151 mutex_lock(&pl
->state_mutex
);
1152 /* Configure the MAC to match the new settings */
1153 linkmode_copy(pl
->link_config
.advertising
, our_kset
.link_modes
.advertising
);
1154 pl
->link_config
.interface
= config
.interface
;
1155 pl
->link_config
.speed
= our_kset
.base
.speed
;
1156 pl
->link_config
.duplex
= our_kset
.base
.duplex
;
1157 pl
->link_config
.an_enabled
= our_kset
.base
.autoneg
!= AUTONEG_DISABLE
;
1159 if (!test_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
)) {
1160 phylink_mac_config(pl
, &pl
->link_config
);
1161 phylink_mac_an_restart(pl
);
1163 mutex_unlock(&pl
->state_mutex
);
1167 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set
);
1170 * phylink_ethtool_nway_reset() - restart negotiation
1171 * @pl: a pointer to a &struct phylink returned from phylink_create()
1173 * Restart negotiation for the phylink instance specified by @pl. This will
1174 * cause any attached phy to restart negotiation with the link partner, and
1175 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1178 * Returns zero on success, or negative error code.
1180 int phylink_ethtool_nway_reset(struct phylink
*pl
)
1187 ret
= phy_restart_aneg(pl
->phydev
);
1188 phylink_mac_an_restart(pl
);
1192 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset
);
1195 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1196 * @pl: a pointer to a &struct phylink returned from phylink_create()
1197 * @pause: a pointer to a &struct ethtool_pauseparam
1199 void phylink_ethtool_get_pauseparam(struct phylink
*pl
,
1200 struct ethtool_pauseparam
*pause
)
1204 pause
->autoneg
= !!(pl
->link_config
.pause
& MLO_PAUSE_AN
);
1205 pause
->rx_pause
= !!(pl
->link_config
.pause
& MLO_PAUSE_RX
);
1206 pause
->tx_pause
= !!(pl
->link_config
.pause
& MLO_PAUSE_TX
);
1208 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam
);
1211 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1212 * @pl: a pointer to a &struct phylink returned from phylink_create()
1213 * @pause: a pointer to a &struct ethtool_pauseparam
1215 int phylink_ethtool_set_pauseparam(struct phylink
*pl
,
1216 struct ethtool_pauseparam
*pause
)
1218 struct phylink_link_state
*config
= &pl
->link_config
;
1222 if (!phylink_test(pl
->supported
, Pause
) &&
1223 !phylink_test(pl
->supported
, Asym_Pause
))
1226 if (!phylink_test(pl
->supported
, Asym_Pause
) &&
1227 !pause
->autoneg
&& pause
->rx_pause
!= pause
->tx_pause
)
1230 config
->pause
&= ~(MLO_PAUSE_AN
| MLO_PAUSE_TXRX_MASK
);
1233 config
->pause
|= MLO_PAUSE_AN
;
1234 if (pause
->rx_pause
)
1235 config
->pause
|= MLO_PAUSE_RX
;
1236 if (pause
->tx_pause
)
1237 config
->pause
|= MLO_PAUSE_TX
;
1239 if (!test_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
)) {
1240 switch (pl
->link_an_mode
) {
1242 /* Silently mark the carrier down, and then trigger a resolve */
1243 netif_carrier_off(pl
->netdev
);
1244 phylink_run_resolve(pl
);
1248 /* Should we allow fixed links to change against the config? */
1249 phylink_resolve_flow(pl
, config
);
1250 phylink_mac_config(pl
, config
);
1254 phylink_mac_config(pl
, config
);
1255 phylink_mac_an_restart(pl
);
1262 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam
);
1265 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1267 * @pl: a pointer to a &struct phylink returned from phylink_create().
1269 * Read the Energy Efficient Ethernet error counter from the PHY associated
1270 * with the phylink instance specified by @pl.
1272 * Returns positive error counter value, or negative error code.
1274 int phylink_get_eee_err(struct phylink
*pl
)
1281 ret
= phy_get_eee_err(pl
->phydev
);
1285 EXPORT_SYMBOL_GPL(phylink_get_eee_err
);
1288 * phylink_init_eee() - init and check the EEE features
1289 * @pl: a pointer to a &struct phylink returned from phylink_create()
1290 * @clk_stop_enable: allow PHY to stop receive clock
1292 * Must be called either with RTNL held or within mac_link_up()
1294 int phylink_init_eee(struct phylink
*pl
, bool clk_stop_enable
)
1296 int ret
= -EOPNOTSUPP
;
1299 ret
= phy_init_eee(pl
->phydev
, clk_stop_enable
);
1303 EXPORT_SYMBOL_GPL(phylink_init_eee
);
1306 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1307 * @pl: a pointer to a &struct phylink returned from phylink_create()
1308 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1310 int phylink_ethtool_get_eee(struct phylink
*pl
, struct ethtool_eee
*eee
)
1312 int ret
= -EOPNOTSUPP
;
1317 ret
= phy_ethtool_get_eee(pl
->phydev
, eee
);
1321 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee
);
1324 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1325 * @pl: a pointer to a &struct phylink returned from phylink_create()
1326 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1328 int phylink_ethtool_set_eee(struct phylink
*pl
, struct ethtool_eee
*eee
)
1330 int ret
= -EOPNOTSUPP
;
1335 ret
= phy_ethtool_set_eee(pl
->phydev
, eee
);
1339 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee
);
1341 /* This emulates MII registers for a fixed-mode phy operating as per the
1342 * passed in state. "aneg" defines if we report negotiation is possible.
1344 * FIXME: should deal with negotiation state too.
1346 static int phylink_mii_emul_read(struct net_device
*ndev
, unsigned int reg
,
1347 struct phylink_link_state
*state
, bool aneg
)
1349 struct fixed_phy_status fs
;
1352 fs
.link
= state
->link
;
1353 fs
.speed
= state
->speed
;
1354 fs
.duplex
= state
->duplex
;
1355 fs
.pause
= state
->pause
& MLO_PAUSE_SYM
;
1356 fs
.asym_pause
= state
->pause
& MLO_PAUSE_ASYM
;
1358 val
= swphy_read_reg(reg
, &fs
);
1359 if (reg
== MII_BMSR
) {
1360 if (!state
->an_complete
)
1361 val
&= ~BMSR_ANEGCOMPLETE
;
1363 val
&= ~BMSR_ANEGCAPABLE
;
1368 static int phylink_phy_read(struct phylink
*pl
, unsigned int phy_id
,
1371 struct phy_device
*phydev
= pl
->phydev
;
1374 if (mdio_phy_id_is_c45(phy_id
)) {
1375 prtad
= mdio_phy_id_prtad(phy_id
);
1376 devad
= mdio_phy_id_devad(phy_id
);
1377 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1378 } else if (phydev
->is_c45
) {
1384 devad
= __ffs(phydev
->c45_ids
.devices_in_package
);
1388 if (!(phydev
->c45_ids
.devices_in_package
& MDIO_DEVS_AN
))
1390 devad
= MDIO_MMD_AN
;
1391 if (reg
== MII_ADVERTISE
)
1392 reg
= MDIO_AN_ADVERTISE
;
1400 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1405 return mdiobus_read(pl
->phydev
->mdio
.bus
, prtad
, devad
);
1408 static int phylink_phy_write(struct phylink
*pl
, unsigned int phy_id
,
1409 unsigned int reg
, unsigned int val
)
1411 struct phy_device
*phydev
= pl
->phydev
;
1414 if (mdio_phy_id_is_c45(phy_id
)) {
1415 prtad
= mdio_phy_id_prtad(phy_id
);
1416 devad
= mdio_phy_id_devad(phy_id
);
1417 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1418 } else if (phydev
->is_c45
) {
1424 devad
= __ffs(phydev
->c45_ids
.devices_in_package
);
1428 if (!(phydev
->c45_ids
.devices_in_package
& MDIO_DEVS_AN
))
1430 devad
= MDIO_MMD_AN
;
1431 if (reg
== MII_ADVERTISE
)
1432 reg
= MDIO_AN_ADVERTISE
;
1440 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1446 return mdiobus_write(phydev
->mdio
.bus
, prtad
, devad
, val
);
1449 static int phylink_mii_read(struct phylink
*pl
, unsigned int phy_id
,
1452 struct phylink_link_state state
;
1455 switch (pl
->link_an_mode
) {
1458 phylink_get_fixed_state(pl
, &state
);
1459 val
= phylink_mii_emul_read(pl
->netdev
, reg
, &state
,
1469 val
= phylink_get_mac_state(pl
, &state
);
1473 val
= phylink_mii_emul_read(pl
->netdev
, reg
, &state
,
1479 return val
& 0xffff;
1482 static int phylink_mii_write(struct phylink
*pl
, unsigned int phy_id
,
1483 unsigned int reg
, unsigned int val
)
1485 switch (pl
->link_an_mode
) {
1500 * phylink_mii_ioctl() - generic mii ioctl interface
1501 * @pl: a pointer to a &struct phylink returned from phylink_create()
1502 * @ifr: a pointer to a &struct ifreq for socket ioctls
1503 * @cmd: ioctl cmd to execute
1505 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1506 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1508 * Returns: zero on success or negative error code.
1511 * read register from the current PHY.
1513 * read register from the specified PHY.
1515 * set a register on the specified PHY.
1517 int phylink_mii_ioctl(struct phylink
*pl
, struct ifreq
*ifr
, int cmd
)
1519 struct mii_ioctl_data
*mii
= if_mii(ifr
);
1525 /* PHYs only exist for MLO_AN_PHY and SGMII */
1528 mii
->phy_id
= pl
->phydev
->mdio
.addr
;
1532 ret
= phylink_phy_read(pl
, mii
->phy_id
, mii
->reg_num
);
1540 ret
= phylink_phy_write(pl
, mii
->phy_id
, mii
->reg_num
,
1545 ret
= phy_mii_ioctl(pl
->phydev
, ifr
, cmd
);
1555 ret
= phylink_mii_read(pl
, mii
->phy_id
, mii
->reg_num
);
1563 ret
= phylink_mii_write(pl
, mii
->phy_id
, mii
->reg_num
,
1575 EXPORT_SYMBOL_GPL(phylink_mii_ioctl
);
1577 static int phylink_sfp_module_insert(void *upstream
,
1578 const struct sfp_eeprom_id
*id
)
1580 struct phylink
*pl
= upstream
;
1581 __ETHTOOL_DECLARE_LINK_MODE_MASK(support
) = { 0, };
1582 __ETHTOOL_DECLARE_LINK_MODE_MASK(support1
);
1583 struct phylink_link_state config
;
1584 phy_interface_t iface
;
1591 sfp_parse_support(pl
->sfp_bus
, id
, support
);
1592 port
= sfp_parse_port(pl
->sfp_bus
, id
, support
);
1594 memset(&config
, 0, sizeof(config
));
1595 linkmode_copy(config
.advertising
, support
);
1596 config
.interface
= PHY_INTERFACE_MODE_NA
;
1597 config
.speed
= SPEED_UNKNOWN
;
1598 config
.duplex
= DUPLEX_UNKNOWN
;
1599 config
.pause
= MLO_PAUSE_AN
;
1600 config
.an_enabled
= pl
->link_config
.an_enabled
;
1602 /* Ignore errors if we're expecting a PHY to attach later */
1603 ret
= phylink_validate(pl
, support
, &config
);
1605 netdev_err(pl
->netdev
, "validation with support %*pb failed: %d\n",
1606 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
, ret
);
1610 linkmode_copy(support1
, support
);
1612 iface
= sfp_select_interface(pl
->sfp_bus
, id
, config
.advertising
);
1613 if (iface
== PHY_INTERFACE_MODE_NA
) {
1614 netdev_err(pl
->netdev
,
1615 "selection of interface failed, advertisement %*pb\n",
1616 __ETHTOOL_LINK_MODE_MASK_NBITS
, config
.advertising
);
1620 config
.interface
= iface
;
1621 ret
= phylink_validate(pl
, support1
, &config
);
1623 netdev_err(pl
->netdev
, "validation of %s/%s with support %*pb failed: %d\n",
1624 phylink_an_mode_str(MLO_AN_INBAND
),
1625 phy_modes(config
.interface
),
1626 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
, ret
);
1630 netdev_dbg(pl
->netdev
, "requesting link mode %s/%s with support %*pb\n",
1631 phylink_an_mode_str(MLO_AN_INBAND
),
1632 phy_modes(config
.interface
),
1633 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
);
1635 if (phy_interface_mode_is_8023z(iface
) && pl
->phydev
)
1638 changed
= !bitmap_equal(pl
->supported
, support
,
1639 __ETHTOOL_LINK_MODE_MASK_NBITS
);
1641 linkmode_copy(pl
->supported
, support
);
1642 linkmode_copy(pl
->link_config
.advertising
, config
.advertising
);
1645 if (pl
->link_an_mode
!= MLO_AN_INBAND
||
1646 pl
->link_config
.interface
!= config
.interface
) {
1647 pl
->link_config
.interface
= config
.interface
;
1648 pl
->link_an_mode
= MLO_AN_INBAND
;
1652 netdev_info(pl
->netdev
, "switched to %s/%s link mode\n",
1653 phylink_an_mode_str(MLO_AN_INBAND
),
1654 phy_modes(config
.interface
));
1657 pl
->link_port
= port
;
1659 if (changed
&& !test_bit(PHYLINK_DISABLE_STOPPED
,
1660 &pl
->phylink_disable_state
))
1661 phylink_mac_config(pl
, &pl
->link_config
);
1666 static void phylink_sfp_link_down(void *upstream
)
1668 struct phylink
*pl
= upstream
;
1672 phylink_run_resolve_and_disable(pl
, PHYLINK_DISABLE_LINK
);
1675 static void phylink_sfp_link_up(void *upstream
)
1677 struct phylink
*pl
= upstream
;
1681 clear_bit(PHYLINK_DISABLE_LINK
, &pl
->phylink_disable_state
);
1682 phylink_run_resolve(pl
);
1685 static int phylink_sfp_connect_phy(void *upstream
, struct phy_device
*phy
)
1687 struct phylink
*pl
= upstream
;
1689 return __phylink_connect_phy(upstream
, phy
, pl
->link_config
.interface
);
1692 static void phylink_sfp_disconnect_phy(void *upstream
)
1694 phylink_disconnect_phy(upstream
);
1697 static const struct sfp_upstream_ops sfp_phylink_ops
= {
1698 .module_insert
= phylink_sfp_module_insert
,
1699 .link_up
= phylink_sfp_link_up
,
1700 .link_down
= phylink_sfp_link_down
,
1701 .connect_phy
= phylink_sfp_connect_phy
,
1702 .disconnect_phy
= phylink_sfp_disconnect_phy
,
1705 /* Helpers for MAC drivers */
1708 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1709 * @state: a pointer to a &struct phylink_link_state
1711 * Inspect the interface mode, advertising mask or forced speed and
1712 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1713 * the interface mode to suit. @state->interface is appropriately
1714 * updated, and the advertising mask has the "other" baseX_Full flag
1717 void phylink_helper_basex_speed(struct phylink_link_state
*state
)
1719 if (phy_interface_mode_is_8023z(state
->interface
)) {
1720 bool want_2500
= state
->an_enabled
?
1721 phylink_test(state
->advertising
, 2500baseX_Full
) :
1722 state
->speed
== SPEED_2500
;
1725 phylink_clear(state
->advertising
, 1000baseX_Full
);
1726 state
->interface
= PHY_INTERFACE_MODE_2500BASEX
;
1728 phylink_clear(state
->advertising
, 2500baseX_Full
);
1729 state
->interface
= PHY_INTERFACE_MODE_1000BASEX
;
1733 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed
);
1735 MODULE_LICENSE("GPL v2");