2 * phylink models the MAC to optional PHY connection, supporting
3 * technologies such as SFP cages where the PHY is hot-pluggable.
5 * Copyright (C) 2015 Russell King
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/ethtool.h>
12 #include <linux/export.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/netdevice.h>
16 #include <linux/of_mdio.h>
17 #include <linux/phy.h>
18 #include <linux/phy_fixed.h>
19 #include <linux/phylink.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/spinlock.h>
22 #include <linux/timer.h>
23 #include <linux/workqueue.h>
28 #define SUPPORTED_INTERFACES \
29 (SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_FIBRE | \
30 SUPPORTED_BNC | SUPPORTED_AUI | SUPPORTED_Backplane)
31 #define ADVERTISED_INTERFACES \
32 (ADVERTISED_TP | ADVERTISED_MII | ADVERTISED_FIBRE | \
33 ADVERTISED_BNC | ADVERTISED_AUI | ADVERTISED_Backplane)
36 PHYLINK_DISABLE_STOPPED
,
41 * struct phylink - internal data type for phylink
45 struct net_device
*netdev
;
46 const struct phylink_mac_ops
*ops
;
48 unsigned long phylink_disable_state
; /* bitmask of disables */
49 struct phy_device
*phydev
;
50 phy_interface_t link_interface
; /* PHY_INTERFACE_xxx */
51 u8 link_an_mode
; /* MLO_AN_xxx */
52 u8 link_port
; /* The current non-phy ethtool port */
53 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
55 /* The link configuration settings */
56 struct phylink_link_state link_config
;
57 struct gpio_desc
*link_gpio
;
58 struct timer_list link_poll
;
59 void (*get_fixed_state
)(struct net_device
*dev
,
60 struct phylink_link_state
*s
);
62 struct mutex state_mutex
;
63 struct phylink_link_state phy_state
;
64 struct work_struct resolve
;
66 bool mac_link_dropped
;
68 struct sfp_bus
*sfp_bus
;
72 * phylink_set_port_modes() - set the port type modes in the ethtool mask
73 * @mask: ethtool link mode mask
75 * Sets all the port type modes in the ethtool mask. MAC drivers should
76 * use this in their 'validate' callback.
78 void phylink_set_port_modes(unsigned long *mask
)
80 phylink_set(mask
, TP
);
81 phylink_set(mask
, AUI
);
82 phylink_set(mask
, MII
);
83 phylink_set(mask
, FIBRE
);
84 phylink_set(mask
, BNC
);
85 phylink_set(mask
, Backplane
);
87 EXPORT_SYMBOL_GPL(phylink_set_port_modes
);
89 static int phylink_is_empty_linkmode(const unsigned long *linkmode
)
91 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp
) = { 0, };
93 phylink_set_port_modes(tmp
);
94 phylink_set(tmp
, Autoneg
);
95 phylink_set(tmp
, Pause
);
96 phylink_set(tmp
, Asym_Pause
);
98 bitmap_andnot(tmp
, linkmode
, tmp
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
100 return linkmode_empty(tmp
);
103 static const char *phylink_an_mode_str(unsigned int mode
)
105 static const char *modestr
[] = {
106 [MLO_AN_PHY
] = "phy",
107 [MLO_AN_FIXED
] = "fixed",
108 [MLO_AN_INBAND
] = "inband",
111 return mode
< ARRAY_SIZE(modestr
) ? modestr
[mode
] : "unknown";
114 static int phylink_validate(struct phylink
*pl
, unsigned long *supported
,
115 struct phylink_link_state
*state
)
117 pl
->ops
->validate(pl
->netdev
, supported
, state
);
119 return phylink_is_empty_linkmode(supported
) ? -EINVAL
: 0;
122 static int phylink_parse_fixedlink(struct phylink
*pl
,
123 struct fwnode_handle
*fwnode
)
125 struct fwnode_handle
*fixed_node
;
126 const struct phy_setting
*s
;
127 struct gpio_desc
*desc
;
131 fixed_node
= fwnode_get_named_child_node(fwnode
, "fixed-link");
133 ret
= fwnode_property_read_u32(fixed_node
, "speed", &speed
);
135 pl
->link_config
.speed
= speed
;
136 pl
->link_config
.duplex
= DUPLEX_HALF
;
138 if (fwnode_property_read_bool(fixed_node
, "full-duplex"))
139 pl
->link_config
.duplex
= DUPLEX_FULL
;
141 /* We treat the "pause" and "asym-pause" terminology as
142 * defining the link partner's ability. */
143 if (fwnode_property_read_bool(fixed_node
, "pause"))
144 pl
->link_config
.pause
|= MLO_PAUSE_SYM
;
145 if (fwnode_property_read_bool(fixed_node
, "asym-pause"))
146 pl
->link_config
.pause
|= MLO_PAUSE_ASYM
;
149 desc
= fwnode_get_named_gpiod(fixed_node
, "link-gpios",
153 pl
->link_gpio
= desc
;
154 else if (desc
== ERR_PTR(-EPROBE_DEFER
))
157 fwnode_handle_put(fixed_node
);
164 ret
= fwnode_property_read_u32_array(fwnode
, "fixed-link",
166 if (ret
!= ARRAY_SIZE(prop
)) {
167 netdev_err(pl
->netdev
, "broken fixed-link?\n");
171 ret
= fwnode_property_read_u32_array(fwnode
, "fixed-link",
172 prop
, ARRAY_SIZE(prop
));
174 pl
->link_config
.duplex
= prop
[1] ?
175 DUPLEX_FULL
: DUPLEX_HALF
;
176 pl
->link_config
.speed
= prop
[2];
178 pl
->link_config
.pause
|= MLO_PAUSE_SYM
;
180 pl
->link_config
.pause
|= MLO_PAUSE_ASYM
;
184 if (pl
->link_config
.speed
> SPEED_1000
&&
185 pl
->link_config
.duplex
!= DUPLEX_FULL
)
186 netdev_warn(pl
->netdev
, "fixed link specifies half duplex for %dMbps link?\n",
187 pl
->link_config
.speed
);
189 bitmap_fill(pl
->supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
190 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
191 phylink_validate(pl
, pl
->supported
, &pl
->link_config
);
193 s
= phy_lookup_setting(pl
->link_config
.speed
, pl
->link_config
.duplex
,
194 pl
->supported
, true);
195 linkmode_zero(pl
->supported
);
196 phylink_set(pl
->supported
, MII
);
198 __set_bit(s
->bit
, pl
->supported
);
200 netdev_warn(pl
->netdev
, "fixed link %s duplex %dMbps not recognised\n",
201 pl
->link_config
.duplex
== DUPLEX_FULL
? "full" : "half",
202 pl
->link_config
.speed
);
205 linkmode_and(pl
->link_config
.advertising
, pl
->link_config
.advertising
,
208 pl
->link_config
.link
= 1;
209 pl
->link_config
.an_complete
= 1;
214 static int phylink_parse_mode(struct phylink
*pl
, struct fwnode_handle
*fwnode
)
216 struct fwnode_handle
*dn
;
219 dn
= fwnode_get_named_child_node(fwnode
, "fixed-link");
220 if (dn
|| fwnode_property_present(fwnode
, "fixed-link"))
221 pl
->link_an_mode
= MLO_AN_FIXED
;
222 fwnode_handle_put(dn
);
224 if (fwnode_property_read_string(fwnode
, "managed", &managed
) == 0 &&
225 strcmp(managed
, "in-band-status") == 0) {
226 if (pl
->link_an_mode
== MLO_AN_FIXED
) {
227 netdev_err(pl
->netdev
,
228 "can't use both fixed-link and in-band-status\n");
232 linkmode_zero(pl
->supported
);
233 phylink_set(pl
->supported
, MII
);
234 phylink_set(pl
->supported
, Autoneg
);
235 phylink_set(pl
->supported
, Asym_Pause
);
236 phylink_set(pl
->supported
, Pause
);
237 pl
->link_config
.an_enabled
= true;
238 pl
->link_an_mode
= MLO_AN_INBAND
;
240 switch (pl
->link_config
.interface
) {
241 case PHY_INTERFACE_MODE_SGMII
:
242 phylink_set(pl
->supported
, 10baseT_Half
);
243 phylink_set(pl
->supported
, 10baseT_Full
);
244 phylink_set(pl
->supported
, 100baseT_Half
);
245 phylink_set(pl
->supported
, 100baseT_Full
);
246 phylink_set(pl
->supported
, 1000baseT_Half
);
247 phylink_set(pl
->supported
, 1000baseT_Full
);
250 case PHY_INTERFACE_MODE_1000BASEX
:
251 phylink_set(pl
->supported
, 1000baseX_Full
);
254 case PHY_INTERFACE_MODE_2500BASEX
:
255 phylink_set(pl
->supported
, 2500baseX_Full
);
258 case PHY_INTERFACE_MODE_10GKR
:
259 phylink_set(pl
->supported
, 10baseT_Half
);
260 phylink_set(pl
->supported
, 10baseT_Full
);
261 phylink_set(pl
->supported
, 100baseT_Half
);
262 phylink_set(pl
->supported
, 100baseT_Full
);
263 phylink_set(pl
->supported
, 1000baseT_Half
);
264 phylink_set(pl
->supported
, 1000baseT_Full
);
265 phylink_set(pl
->supported
, 1000baseX_Full
);
266 phylink_set(pl
->supported
, 10000baseKR_Full
);
267 phylink_set(pl
->supported
, 10000baseCR_Full
);
268 phylink_set(pl
->supported
, 10000baseSR_Full
);
269 phylink_set(pl
->supported
, 10000baseLR_Full
);
270 phylink_set(pl
->supported
, 10000baseLRM_Full
);
271 phylink_set(pl
->supported
, 10000baseER_Full
);
275 netdev_err(pl
->netdev
,
276 "incorrect link mode %s for in-band status\n",
277 phy_modes(pl
->link_config
.interface
));
281 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
283 if (phylink_validate(pl
, pl
->supported
, &pl
->link_config
)) {
284 netdev_err(pl
->netdev
,
285 "failed to validate link configuration for in-band status\n");
293 static void phylink_mac_config(struct phylink
*pl
,
294 const struct phylink_link_state
*state
)
296 netdev_dbg(pl
->netdev
,
297 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
298 __func__
, phylink_an_mode_str(pl
->link_an_mode
),
299 phy_modes(state
->interface
),
300 phy_speed_to_str(state
->speed
),
301 phy_duplex_to_str(state
->duplex
),
302 __ETHTOOL_LINK_MODE_MASK_NBITS
, state
->advertising
,
303 state
->pause
, state
->link
, state
->an_enabled
);
305 pl
->ops
->mac_config(pl
->netdev
, pl
->link_an_mode
, state
);
308 static void phylink_mac_an_restart(struct phylink
*pl
)
310 if (pl
->link_config
.an_enabled
&&
311 phy_interface_mode_is_8023z(pl
->link_config
.interface
))
312 pl
->ops
->mac_an_restart(pl
->netdev
);
315 static int phylink_get_mac_state(struct phylink
*pl
, struct phylink_link_state
*state
)
317 struct net_device
*ndev
= pl
->netdev
;
319 linkmode_copy(state
->advertising
, pl
->link_config
.advertising
);
320 linkmode_zero(state
->lp_advertising
);
321 state
->interface
= pl
->link_config
.interface
;
322 state
->an_enabled
= pl
->link_config
.an_enabled
;
325 return pl
->ops
->mac_link_state(ndev
, state
);
328 /* The fixed state is... fixed except for the link state,
329 * which may be determined by a GPIO or a callback.
331 static void phylink_get_fixed_state(struct phylink
*pl
, struct phylink_link_state
*state
)
333 *state
= pl
->link_config
;
334 if (pl
->get_fixed_state
)
335 pl
->get_fixed_state(pl
->netdev
, state
);
336 else if (pl
->link_gpio
)
337 state
->link
= !!gpiod_get_value_cansleep(pl
->link_gpio
);
340 /* Flow control is resolved according to our and the link partners
341 * advertisements using the following drawn from the 802.3 specs:
342 * Local device Link partner
343 * Pause AsymDir Pause AsymDir Result
348 static void phylink_resolve_flow(struct phylink
*pl
,
349 struct phylink_link_state
*state
)
353 if (pl
->link_config
.pause
& MLO_PAUSE_AN
) {
356 if (phylink_test(pl
->link_config
.advertising
, Pause
))
357 pause
|= MLO_PAUSE_SYM
;
358 if (phylink_test(pl
->link_config
.advertising
, Asym_Pause
))
359 pause
|= MLO_PAUSE_ASYM
;
361 pause
&= state
->pause
;
363 if (pause
& MLO_PAUSE_SYM
)
364 new_pause
= MLO_PAUSE_TX
| MLO_PAUSE_RX
;
365 else if (pause
& MLO_PAUSE_ASYM
)
366 new_pause
= state
->pause
& MLO_PAUSE_SYM
?
367 MLO_PAUSE_RX
: MLO_PAUSE_TX
;
369 new_pause
= pl
->link_config
.pause
& MLO_PAUSE_TXRX_MASK
;
372 state
->pause
&= ~MLO_PAUSE_TXRX_MASK
;
373 state
->pause
|= new_pause
;
376 static const char *phylink_pause_to_str(int pause
)
378 switch (pause
& MLO_PAUSE_TXRX_MASK
) {
379 case MLO_PAUSE_TX
| MLO_PAUSE_RX
:
390 static void phylink_resolve(struct work_struct
*w
)
392 struct phylink
*pl
= container_of(w
, struct phylink
, resolve
);
393 struct phylink_link_state link_state
;
394 struct net_device
*ndev
= pl
->netdev
;
396 mutex_lock(&pl
->state_mutex
);
397 if (pl
->phylink_disable_state
) {
398 pl
->mac_link_dropped
= false;
399 link_state
.link
= false;
400 } else if (pl
->mac_link_dropped
) {
401 link_state
.link
= false;
403 switch (pl
->link_an_mode
) {
405 link_state
= pl
->phy_state
;
406 phylink_resolve_flow(pl
, &link_state
);
407 phylink_mac_config(pl
, &link_state
);
411 phylink_get_fixed_state(pl
, &link_state
);
412 phylink_mac_config(pl
, &link_state
);
416 phylink_get_mac_state(pl
, &link_state
);
418 bool changed
= false;
420 link_state
.link
= link_state
.link
&&
423 if (pl
->phy_state
.interface
!=
424 link_state
.interface
) {
425 link_state
.interface
= pl
->phy_state
.interface
;
429 /* Propagate the flow control from the PHY
430 * to the MAC. Also propagate the interface
433 if (pl
->phy_state
.link
|| changed
) {
434 link_state
.pause
|= pl
->phy_state
.pause
;
435 phylink_resolve_flow(pl
, &link_state
);
437 phylink_mac_config(pl
, &link_state
);
444 if (link_state
.link
!= netif_carrier_ok(ndev
)) {
445 if (!link_state
.link
) {
446 netif_carrier_off(ndev
);
447 pl
->ops
->mac_link_down(ndev
, pl
->link_an_mode
,
448 pl
->phy_state
.interface
);
449 netdev_info(ndev
, "Link is Down\n");
451 pl
->ops
->mac_link_up(ndev
, pl
->link_an_mode
,
452 pl
->phy_state
.interface
,
455 netif_carrier_on(ndev
);
458 "Link is Up - %s/%s - flow control %s\n",
459 phy_speed_to_str(link_state
.speed
),
460 phy_duplex_to_str(link_state
.duplex
),
461 phylink_pause_to_str(link_state
.pause
));
464 if (!link_state
.link
&& pl
->mac_link_dropped
) {
465 pl
->mac_link_dropped
= false;
466 queue_work(system_power_efficient_wq
, &pl
->resolve
);
468 mutex_unlock(&pl
->state_mutex
);
471 static void phylink_run_resolve(struct phylink
*pl
)
473 if (!pl
->phylink_disable_state
)
474 queue_work(system_power_efficient_wq
, &pl
->resolve
);
477 static void phylink_run_resolve_and_disable(struct phylink
*pl
, int bit
)
479 unsigned long state
= pl
->phylink_disable_state
;
481 set_bit(bit
, &pl
->phylink_disable_state
);
483 queue_work(system_power_efficient_wq
, &pl
->resolve
);
484 flush_work(&pl
->resolve
);
488 static void phylink_fixed_poll(struct timer_list
*t
)
490 struct phylink
*pl
= container_of(t
, struct phylink
, link_poll
);
492 mod_timer(t
, jiffies
+ HZ
);
494 phylink_run_resolve(pl
);
497 static const struct sfp_upstream_ops sfp_phylink_ops
;
499 static int phylink_register_sfp(struct phylink
*pl
,
500 struct fwnode_handle
*fwnode
)
502 struct fwnode_reference_args ref
;
508 ret
= fwnode_property_get_reference_args(fwnode
, "sfp", NULL
,
514 netdev_err(pl
->netdev
, "unable to parse \"sfp\" node: %d\n",
519 pl
->sfp_bus
= sfp_register_upstream(ref
.fwnode
, pl
->netdev
, pl
,
528 * phylink_create() - create a phylink instance
529 * @ndev: a pointer to the &struct net_device
530 * @fwnode: a pointer to a &struct fwnode_handle describing the network
532 * @iface: the desired link mode defined by &typedef phy_interface_t
533 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
535 * Create a new phylink instance, and parse the link parameters found in @np.
536 * This will parse in-band modes, fixed-link or SFP configuration.
538 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
539 * must use IS_ERR() to check for errors from this function.
541 struct phylink
*phylink_create(struct net_device
*ndev
,
542 struct fwnode_handle
*fwnode
,
543 phy_interface_t iface
,
544 const struct phylink_mac_ops
*ops
)
549 pl
= kzalloc(sizeof(*pl
), GFP_KERNEL
);
551 return ERR_PTR(-ENOMEM
);
553 mutex_init(&pl
->state_mutex
);
554 INIT_WORK(&pl
->resolve
, phylink_resolve
);
556 pl
->phy_state
.interface
= iface
;
557 pl
->link_interface
= iface
;
558 if (iface
== PHY_INTERFACE_MODE_MOCA
)
559 pl
->link_port
= PORT_BNC
;
561 pl
->link_port
= PORT_MII
;
562 pl
->link_config
.interface
= iface
;
563 pl
->link_config
.pause
= MLO_PAUSE_AN
;
564 pl
->link_config
.speed
= SPEED_UNKNOWN
;
565 pl
->link_config
.duplex
= DUPLEX_UNKNOWN
;
566 pl
->link_config
.an_enabled
= true;
568 __set_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
569 timer_setup(&pl
->link_poll
, phylink_fixed_poll
, 0);
571 bitmap_fill(pl
->supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
572 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
573 phylink_validate(pl
, pl
->supported
, &pl
->link_config
);
575 ret
= phylink_parse_mode(pl
, fwnode
);
581 if (pl
->link_an_mode
== MLO_AN_FIXED
) {
582 ret
= phylink_parse_fixedlink(pl
, fwnode
);
589 ret
= phylink_register_sfp(pl
, fwnode
);
597 EXPORT_SYMBOL_GPL(phylink_create
);
600 * phylink_destroy() - cleanup and destroy the phylink instance
601 * @pl: a pointer to a &struct phylink returned from phylink_create()
603 * Destroy a phylink instance. Any PHY that has been attached must have been
604 * cleaned up via phylink_disconnect_phy() prior to calling this function.
606 void phylink_destroy(struct phylink
*pl
)
609 sfp_unregister_upstream(pl
->sfp_bus
);
610 if (!IS_ERR_OR_NULL(pl
->link_gpio
))
611 gpiod_put(pl
->link_gpio
);
613 cancel_work_sync(&pl
->resolve
);
616 EXPORT_SYMBOL_GPL(phylink_destroy
);
618 static void phylink_phy_change(struct phy_device
*phydev
, bool up
,
621 struct phylink
*pl
= phydev
->phylink
;
623 mutex_lock(&pl
->state_mutex
);
624 pl
->phy_state
.speed
= phydev
->speed
;
625 pl
->phy_state
.duplex
= phydev
->duplex
;
626 pl
->phy_state
.pause
= MLO_PAUSE_NONE
;
628 pl
->phy_state
.pause
|= MLO_PAUSE_SYM
;
629 if (phydev
->asym_pause
)
630 pl
->phy_state
.pause
|= MLO_PAUSE_ASYM
;
631 pl
->phy_state
.interface
= phydev
->interface
;
632 pl
->phy_state
.link
= up
;
633 mutex_unlock(&pl
->state_mutex
);
635 phylink_run_resolve(pl
);
637 netdev_dbg(pl
->netdev
, "phy link %s %s/%s/%s\n", up
? "up" : "down",
638 phy_modes(phydev
->interface
),
639 phy_speed_to_str(phydev
->speed
),
640 phy_duplex_to_str(phydev
->duplex
));
643 static int phylink_bringup_phy(struct phylink
*pl
, struct phy_device
*phy
)
645 struct phylink_link_state config
;
646 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
649 memset(&config
, 0, sizeof(config
));
650 linkmode_copy(supported
, phy
->supported
);
651 linkmode_copy(config
.advertising
, phy
->advertising
);
652 config
.interface
= pl
->link_config
.interface
;
655 * This is the new way of dealing with flow control for PHYs,
656 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
657 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
658 * using our validate call to the MAC, we rely upon the MAC
659 * clearing the bits from both supported and advertising fields.
661 if (phylink_test(supported
, Pause
))
662 phylink_set(config
.advertising
, Pause
);
663 if (phylink_test(supported
, Asym_Pause
))
664 phylink_set(config
.advertising
, Asym_Pause
);
666 ret
= phylink_validate(pl
, supported
, &config
);
671 phy
->phy_link_change
= phylink_phy_change
;
673 netdev_info(pl
->netdev
,
674 "PHY [%s] driver [%s]\n", dev_name(&phy
->mdio
.dev
),
677 mutex_lock(&phy
->lock
);
678 mutex_lock(&pl
->state_mutex
);
680 linkmode_copy(pl
->supported
, supported
);
681 linkmode_copy(pl
->link_config
.advertising
, config
.advertising
);
683 /* Restrict the phy advertisement according to the MAC support. */
684 linkmode_copy(phy
->advertising
, config
.advertising
);
685 mutex_unlock(&pl
->state_mutex
);
686 mutex_unlock(&phy
->lock
);
688 netdev_dbg(pl
->netdev
,
689 "phy: setting supported %*pb advertising %*pb\n",
690 __ETHTOOL_LINK_MODE_MASK_NBITS
, pl
->supported
,
691 __ETHTOOL_LINK_MODE_MASK_NBITS
, phy
->advertising
);
693 phy_start_machine(phy
);
695 phy_start_interrupts(phy
);
700 static int __phylink_connect_phy(struct phylink
*pl
, struct phy_device
*phy
,
701 phy_interface_t interface
)
705 if (WARN_ON(pl
->link_an_mode
== MLO_AN_FIXED
||
706 (pl
->link_an_mode
== MLO_AN_INBAND
&&
707 phy_interface_mode_is_8023z(interface
))))
713 ret
= phy_attach_direct(pl
->netdev
, phy
, 0, interface
);
717 ret
= phylink_bringup_phy(pl
, phy
);
725 * phylink_connect_phy() - connect a PHY to the phylink instance
726 * @pl: a pointer to a &struct phylink returned from phylink_create()
727 * @phy: a pointer to a &struct phy_device.
729 * Connect @phy to the phylink instance specified by @pl by calling
730 * phy_attach_direct(). Configure the @phy according to the MAC driver's
731 * capabilities, start the PHYLIB state machine and enable any interrupts
732 * that the PHY supports.
734 * This updates the phylink's ethtool supported and advertising link mode
737 * Returns 0 on success or a negative errno.
739 int phylink_connect_phy(struct phylink
*pl
, struct phy_device
*phy
)
741 /* Use PHY device/driver interface */
742 if (pl
->link_interface
== PHY_INTERFACE_MODE_NA
) {
743 pl
->link_interface
= phy
->interface
;
744 pl
->link_config
.interface
= pl
->link_interface
;
747 return __phylink_connect_phy(pl
, phy
, pl
->link_interface
);
749 EXPORT_SYMBOL_GPL(phylink_connect_phy
);
752 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
753 * @pl: a pointer to a &struct phylink returned from phylink_create()
754 * @dn: a pointer to a &struct device_node.
755 * @flags: PHY-specific flags to communicate to the PHY device driver
757 * Connect the phy specified in the device node @dn to the phylink instance
758 * specified by @pl. Actions specified in phylink_connect_phy() will be
761 * Returns 0 on success or a negative errno.
763 int phylink_of_phy_connect(struct phylink
*pl
, struct device_node
*dn
,
766 struct device_node
*phy_node
;
767 struct phy_device
*phy_dev
;
770 /* Fixed links and 802.3z are handled without needing a PHY */
771 if (pl
->link_an_mode
== MLO_AN_FIXED
||
772 (pl
->link_an_mode
== MLO_AN_INBAND
&&
773 phy_interface_mode_is_8023z(pl
->link_interface
)))
776 phy_node
= of_parse_phandle(dn
, "phy-handle", 0);
778 phy_node
= of_parse_phandle(dn
, "phy", 0);
780 phy_node
= of_parse_phandle(dn
, "phy-device", 0);
783 if (pl
->link_an_mode
== MLO_AN_PHY
)
788 phy_dev
= of_phy_attach(pl
->netdev
, phy_node
, flags
,
790 /* We're done with the phy_node handle */
791 of_node_put(phy_node
);
796 ret
= phylink_bringup_phy(pl
, phy_dev
);
802 EXPORT_SYMBOL_GPL(phylink_of_phy_connect
);
805 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
807 * @pl: a pointer to a &struct phylink returned from phylink_create()
809 * Disconnect any current PHY from the phylink instance described by @pl.
811 void phylink_disconnect_phy(struct phylink
*pl
)
813 struct phy_device
*phy
;
819 mutex_lock(&phy
->lock
);
820 mutex_lock(&pl
->state_mutex
);
822 mutex_unlock(&pl
->state_mutex
);
823 mutex_unlock(&phy
->lock
);
824 flush_work(&pl
->resolve
);
829 EXPORT_SYMBOL_GPL(phylink_disconnect_phy
);
832 * phylink_fixed_state_cb() - allow setting a fixed link callback
833 * @pl: a pointer to a &struct phylink returned from phylink_create()
834 * @cb: callback to execute to determine the fixed link state.
836 * The MAC driver should call this driver when the state of its link
837 * can be determined through e.g: an out of band MMIO register.
839 int phylink_fixed_state_cb(struct phylink
*pl
,
840 void (*cb
)(struct net_device
*dev
,
841 struct phylink_link_state
*state
))
843 /* It does not make sense to let the link be overriden unless we use
846 if (pl
->link_an_mode
!= MLO_AN_FIXED
)
849 mutex_lock(&pl
->state_mutex
);
850 pl
->get_fixed_state
= cb
;
851 mutex_unlock(&pl
->state_mutex
);
855 EXPORT_SYMBOL_GPL(phylink_fixed_state_cb
);
858 * phylink_mac_change() - notify phylink of a change in MAC state
859 * @pl: a pointer to a &struct phylink returned from phylink_create()
860 * @up: indicates whether the link is currently up.
862 * The MAC driver should call this driver when the state of its link
863 * changes (eg, link failure, new negotiation results, etc.)
865 void phylink_mac_change(struct phylink
*pl
, bool up
)
868 pl
->mac_link_dropped
= true;
869 phylink_run_resolve(pl
);
870 netdev_dbg(pl
->netdev
, "mac link %s\n", up
? "up" : "down");
872 EXPORT_SYMBOL_GPL(phylink_mac_change
);
875 * phylink_start() - start a phylink instance
876 * @pl: a pointer to a &struct phylink returned from phylink_create()
878 * Start the phylink instance specified by @pl, configuring the MAC for the
879 * desired link mode(s) and negotiation style. This should be called from the
880 * network device driver's &struct net_device_ops ndo_open() method.
882 void phylink_start(struct phylink
*pl
)
886 netdev_info(pl
->netdev
, "configuring for %s/%s link mode\n",
887 phylink_an_mode_str(pl
->link_an_mode
),
888 phy_modes(pl
->link_config
.interface
));
890 /* Always set the carrier off */
891 netif_carrier_off(pl
->netdev
);
893 /* Apply the link configuration to the MAC when starting. This allows
894 * a fixed-link to start with the correct parameters, and also
895 * ensures that we set the appropriate advertisement for Serdes links.
897 phylink_resolve_flow(pl
, &pl
->link_config
);
898 phylink_mac_config(pl
, &pl
->link_config
);
900 /* Restart autonegotiation if using 802.3z to ensure that the link
901 * parameters are properly negotiated. This is necessary for DSA
902 * switches using 802.3z negotiation to ensure they see our modes.
904 phylink_mac_an_restart(pl
);
906 clear_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
907 phylink_run_resolve(pl
);
909 if (pl
->link_an_mode
== MLO_AN_FIXED
&& !IS_ERR(pl
->link_gpio
))
910 mod_timer(&pl
->link_poll
, jiffies
+ HZ
);
912 sfp_upstream_start(pl
->sfp_bus
);
914 phy_start(pl
->phydev
);
916 EXPORT_SYMBOL_GPL(phylink_start
);
919 * phylink_stop() - stop a phylink instance
920 * @pl: a pointer to a &struct phylink returned from phylink_create()
922 * Stop the phylink instance specified by @pl. This should be called from the
923 * network device driver's &struct net_device_ops ndo_stop() method. The
924 * network device's carrier state should not be changed prior to calling this
927 void phylink_stop(struct phylink
*pl
)
932 phy_stop(pl
->phydev
);
934 sfp_upstream_stop(pl
->sfp_bus
);
935 if (pl
->link_an_mode
== MLO_AN_FIXED
&& !IS_ERR(pl
->link_gpio
))
936 del_timer_sync(&pl
->link_poll
);
938 phylink_run_resolve_and_disable(pl
, PHYLINK_DISABLE_STOPPED
);
940 EXPORT_SYMBOL_GPL(phylink_stop
);
943 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
944 * @pl: a pointer to a &struct phylink returned from phylink_create()
945 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
947 * Read the wake on lan parameters from the PHY attached to the phylink
948 * instance specified by @pl. If no PHY is currently attached, report no
949 * support for wake on lan.
951 void phylink_ethtool_get_wol(struct phylink
*pl
, struct ethtool_wolinfo
*wol
)
959 phy_ethtool_get_wol(pl
->phydev
, wol
);
961 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol
);
964 * phylink_ethtool_set_wol() - set wake on lan parameters
965 * @pl: a pointer to a &struct phylink returned from phylink_create()
966 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
968 * Set the wake on lan parameters for the PHY attached to the phylink
969 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
972 * Returns zero on success or negative errno code.
974 int phylink_ethtool_set_wol(struct phylink
*pl
, struct ethtool_wolinfo
*wol
)
976 int ret
= -EOPNOTSUPP
;
981 ret
= phy_ethtool_set_wol(pl
->phydev
, wol
);
985 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol
);
987 static void phylink_merge_link_mode(unsigned long *dst
, const unsigned long *b
)
989 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
);
992 phylink_set_port_modes(mask
);
994 linkmode_and(dst
, dst
, mask
);
995 linkmode_or(dst
, dst
, b
);
998 static void phylink_get_ksettings(const struct phylink_link_state
*state
,
999 struct ethtool_link_ksettings
*kset
)
1001 phylink_merge_link_mode(kset
->link_modes
.advertising
, state
->advertising
);
1002 linkmode_copy(kset
->link_modes
.lp_advertising
, state
->lp_advertising
);
1003 kset
->base
.speed
= state
->speed
;
1004 kset
->base
.duplex
= state
->duplex
;
1005 kset
->base
.autoneg
= state
->an_enabled
? AUTONEG_ENABLE
:
1010 * phylink_ethtool_ksettings_get() - get the current link settings
1011 * @pl: a pointer to a &struct phylink returned from phylink_create()
1012 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1014 * Read the current link settings for the phylink instance specified by @pl.
1015 * This will be the link settings read from the MAC, PHY or fixed link
1016 * settings depending on the current negotiation mode.
1018 int phylink_ethtool_ksettings_get(struct phylink
*pl
,
1019 struct ethtool_link_ksettings
*kset
)
1021 struct phylink_link_state link_state
;
1026 phy_ethtool_ksettings_get(pl
->phydev
, kset
);
1028 kset
->base
.port
= pl
->link_port
;
1031 linkmode_copy(kset
->link_modes
.supported
, pl
->supported
);
1033 switch (pl
->link_an_mode
) {
1035 /* We are using fixed settings. Report these as the
1036 * current link settings - and note that these also
1037 * represent the supported speeds/duplex/pause modes.
1039 phylink_get_fixed_state(pl
, &link_state
);
1040 phylink_get_ksettings(&link_state
, kset
);
1044 /* If there is a phy attached, then use the reported
1045 * settings from the phy with no modification.
1050 phylink_get_mac_state(pl
, &link_state
);
1052 /* The MAC is reporting the link results from its own PCS
1053 * layer via in-band status. Report these as the current
1056 phylink_get_ksettings(&link_state
, kset
);
1062 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get
);
1065 * phylink_ethtool_ksettings_set() - set the link settings
1066 * @pl: a pointer to a &struct phylink returned from phylink_create()
1067 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1069 int phylink_ethtool_ksettings_set(struct phylink
*pl
,
1070 const struct ethtool_link_ksettings
*kset
)
1072 struct ethtool_link_ksettings our_kset
;
1073 struct phylink_link_state config
;
1078 if (kset
->base
.autoneg
!= AUTONEG_DISABLE
&&
1079 kset
->base
.autoneg
!= AUTONEG_ENABLE
)
1082 config
= pl
->link_config
;
1084 /* Mask out unsupported advertisements */
1085 linkmode_and(config
.advertising
, kset
->link_modes
.advertising
,
1088 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1089 if (kset
->base
.autoneg
== AUTONEG_DISABLE
) {
1090 const struct phy_setting
*s
;
1092 /* Autonegotiation disabled, select a suitable speed and
1095 s
= phy_lookup_setting(kset
->base
.speed
, kset
->base
.duplex
,
1096 pl
->supported
, false);
1100 /* If we have a fixed link (as specified by firmware), refuse
1101 * to change link parameters.
1103 if (pl
->link_an_mode
== MLO_AN_FIXED
&&
1104 (s
->speed
!= pl
->link_config
.speed
||
1105 s
->duplex
!= pl
->link_config
.duplex
))
1108 config
.speed
= s
->speed
;
1109 config
.duplex
= s
->duplex
;
1110 config
.an_enabled
= false;
1112 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, config
.advertising
);
1114 /* If we have a fixed link, refuse to enable autonegotiation */
1115 if (pl
->link_an_mode
== MLO_AN_FIXED
)
1118 config
.speed
= SPEED_UNKNOWN
;
1119 config
.duplex
= DUPLEX_UNKNOWN
;
1120 config
.an_enabled
= true;
1122 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, config
.advertising
);
1125 if (phylink_validate(pl
, pl
->supported
, &config
))
1128 /* If autonegotiation is enabled, we must have an advertisement */
1129 if (config
.an_enabled
&& phylink_is_empty_linkmode(config
.advertising
))
1133 linkmode_copy(our_kset
.link_modes
.advertising
, config
.advertising
);
1134 our_kset
.base
.speed
= config
.speed
;
1135 our_kset
.base
.duplex
= config
.duplex
;
1137 /* If we have a PHY, configure the phy */
1139 ret
= phy_ethtool_ksettings_set(pl
->phydev
, &our_kset
);
1144 mutex_lock(&pl
->state_mutex
);
1145 /* Configure the MAC to match the new settings */
1146 linkmode_copy(pl
->link_config
.advertising
, our_kset
.link_modes
.advertising
);
1147 pl
->link_config
.interface
= config
.interface
;
1148 pl
->link_config
.speed
= our_kset
.base
.speed
;
1149 pl
->link_config
.duplex
= our_kset
.base
.duplex
;
1150 pl
->link_config
.an_enabled
= our_kset
.base
.autoneg
!= AUTONEG_DISABLE
;
1152 if (!test_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
)) {
1153 phylink_mac_config(pl
, &pl
->link_config
);
1154 phylink_mac_an_restart(pl
);
1156 mutex_unlock(&pl
->state_mutex
);
1160 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set
);
1163 * phylink_ethtool_nway_reset() - restart negotiation
1164 * @pl: a pointer to a &struct phylink returned from phylink_create()
1166 * Restart negotiation for the phylink instance specified by @pl. This will
1167 * cause any attached phy to restart negotiation with the link partner, and
1168 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1171 * Returns zero on success, or negative error code.
1173 int phylink_ethtool_nway_reset(struct phylink
*pl
)
1180 ret
= phy_restart_aneg(pl
->phydev
);
1181 phylink_mac_an_restart(pl
);
1185 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset
);
1188 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1189 * @pl: a pointer to a &struct phylink returned from phylink_create()
1190 * @pause: a pointer to a &struct ethtool_pauseparam
1192 void phylink_ethtool_get_pauseparam(struct phylink
*pl
,
1193 struct ethtool_pauseparam
*pause
)
1197 pause
->autoneg
= !!(pl
->link_config
.pause
& MLO_PAUSE_AN
);
1198 pause
->rx_pause
= !!(pl
->link_config
.pause
& MLO_PAUSE_RX
);
1199 pause
->tx_pause
= !!(pl
->link_config
.pause
& MLO_PAUSE_TX
);
1201 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam
);
1204 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1205 * @pl: a pointer to a &struct phylink returned from phylink_create()
1206 * @pause: a pointer to a &struct ethtool_pauseparam
1208 int phylink_ethtool_set_pauseparam(struct phylink
*pl
,
1209 struct ethtool_pauseparam
*pause
)
1211 struct phylink_link_state
*config
= &pl
->link_config
;
1215 if (!phylink_test(pl
->supported
, Pause
) &&
1216 !phylink_test(pl
->supported
, Asym_Pause
))
1219 if (!phylink_test(pl
->supported
, Asym_Pause
) &&
1220 !pause
->autoneg
&& pause
->rx_pause
!= pause
->tx_pause
)
1223 config
->pause
&= ~(MLO_PAUSE_AN
| MLO_PAUSE_TXRX_MASK
);
1226 config
->pause
|= MLO_PAUSE_AN
;
1227 if (pause
->rx_pause
)
1228 config
->pause
|= MLO_PAUSE_RX
;
1229 if (pause
->tx_pause
)
1230 config
->pause
|= MLO_PAUSE_TX
;
1232 if (!test_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
)) {
1233 switch (pl
->link_an_mode
) {
1235 /* Silently mark the carrier down, and then trigger a resolve */
1236 netif_carrier_off(pl
->netdev
);
1237 phylink_run_resolve(pl
);
1241 /* Should we allow fixed links to change against the config? */
1242 phylink_resolve_flow(pl
, config
);
1243 phylink_mac_config(pl
, config
);
1247 phylink_mac_config(pl
, config
);
1248 phylink_mac_an_restart(pl
);
1255 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam
);
1258 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1260 * @pl: a pointer to a &struct phylink returned from phylink_create().
1262 * Read the Energy Efficient Ethernet error counter from the PHY associated
1263 * with the phylink instance specified by @pl.
1265 * Returns positive error counter value, or negative error code.
1267 int phylink_get_eee_err(struct phylink
*pl
)
1274 ret
= phy_get_eee_err(pl
->phydev
);
1278 EXPORT_SYMBOL_GPL(phylink_get_eee_err
);
1281 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1282 * @pl: a pointer to a &struct phylink returned from phylink_create()
1283 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1285 int phylink_ethtool_get_eee(struct phylink
*pl
, struct ethtool_eee
*eee
)
1287 int ret
= -EOPNOTSUPP
;
1292 ret
= phy_ethtool_get_eee(pl
->phydev
, eee
);
1296 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee
);
1299 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1300 * @pl: a pointer to a &struct phylink returned from phylink_create()
1301 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1303 int phylink_ethtool_set_eee(struct phylink
*pl
, struct ethtool_eee
*eee
)
1305 int ret
= -EOPNOTSUPP
;
1310 ret
= phy_ethtool_set_eee(pl
->phydev
, eee
);
1314 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee
);
1316 /* This emulates MII registers for a fixed-mode phy operating as per the
1317 * passed in state. "aneg" defines if we report negotiation is possible.
1319 * FIXME: should deal with negotiation state too.
1321 static int phylink_mii_emul_read(struct net_device
*ndev
, unsigned int reg
,
1322 struct phylink_link_state
*state
, bool aneg
)
1324 struct fixed_phy_status fs
;
1327 fs
.link
= state
->link
;
1328 fs
.speed
= state
->speed
;
1329 fs
.duplex
= state
->duplex
;
1330 fs
.pause
= state
->pause
& MLO_PAUSE_SYM
;
1331 fs
.asym_pause
= state
->pause
& MLO_PAUSE_ASYM
;
1333 val
= swphy_read_reg(reg
, &fs
);
1334 if (reg
== MII_BMSR
) {
1335 if (!state
->an_complete
)
1336 val
&= ~BMSR_ANEGCOMPLETE
;
1338 val
&= ~BMSR_ANEGCAPABLE
;
1343 static int phylink_phy_read(struct phylink
*pl
, unsigned int phy_id
,
1346 struct phy_device
*phydev
= pl
->phydev
;
1349 if (mdio_phy_id_is_c45(phy_id
)) {
1350 prtad
= mdio_phy_id_prtad(phy_id
);
1351 devad
= mdio_phy_id_devad(phy_id
);
1352 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1353 } else if (phydev
->is_c45
) {
1359 devad
= __ffs(phydev
->c45_ids
.devices_in_package
);
1363 if (!(phydev
->c45_ids
.devices_in_package
& MDIO_DEVS_AN
))
1365 devad
= MDIO_MMD_AN
;
1366 if (reg
== MII_ADVERTISE
)
1367 reg
= MDIO_AN_ADVERTISE
;
1375 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1380 return mdiobus_read(pl
->phydev
->mdio
.bus
, prtad
, devad
);
1383 static int phylink_phy_write(struct phylink
*pl
, unsigned int phy_id
,
1384 unsigned int reg
, unsigned int val
)
1386 struct phy_device
*phydev
= pl
->phydev
;
1389 if (mdio_phy_id_is_c45(phy_id
)) {
1390 prtad
= mdio_phy_id_prtad(phy_id
);
1391 devad
= mdio_phy_id_devad(phy_id
);
1392 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1393 } else if (phydev
->is_c45
) {
1399 devad
= __ffs(phydev
->c45_ids
.devices_in_package
);
1403 if (!(phydev
->c45_ids
.devices_in_package
& MDIO_DEVS_AN
))
1405 devad
= MDIO_MMD_AN
;
1406 if (reg
== MII_ADVERTISE
)
1407 reg
= MDIO_AN_ADVERTISE
;
1415 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1421 return mdiobus_write(phydev
->mdio
.bus
, prtad
, devad
, val
);
1424 static int phylink_mii_read(struct phylink
*pl
, unsigned int phy_id
,
1427 struct phylink_link_state state
;
1430 switch (pl
->link_an_mode
) {
1433 phylink_get_fixed_state(pl
, &state
);
1434 val
= phylink_mii_emul_read(pl
->netdev
, reg
, &state
,
1444 val
= phylink_get_mac_state(pl
, &state
);
1448 val
= phylink_mii_emul_read(pl
->netdev
, reg
, &state
,
1454 return val
& 0xffff;
1457 static int phylink_mii_write(struct phylink
*pl
, unsigned int phy_id
,
1458 unsigned int reg
, unsigned int val
)
1460 switch (pl
->link_an_mode
) {
1475 * phylink_mii_ioctl() - generic mii ioctl interface
1476 * @pl: a pointer to a &struct phylink returned from phylink_create()
1477 * @ifr: a pointer to a &struct ifreq for socket ioctls
1478 * @cmd: ioctl cmd to execute
1480 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1481 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1483 * Returns: zero on success or negative error code.
1486 * read register from the current PHY.
1488 * read register from the specified PHY.
1490 * set a register on the specified PHY.
1492 int phylink_mii_ioctl(struct phylink
*pl
, struct ifreq
*ifr
, int cmd
)
1494 struct mii_ioctl_data
*mii
= if_mii(ifr
);
1500 /* PHYs only exist for MLO_AN_PHY and SGMII */
1503 mii
->phy_id
= pl
->phydev
->mdio
.addr
;
1507 ret
= phylink_phy_read(pl
, mii
->phy_id
, mii
->reg_num
);
1515 ret
= phylink_phy_write(pl
, mii
->phy_id
, mii
->reg_num
,
1520 ret
= phy_mii_ioctl(pl
->phydev
, ifr
, cmd
);
1530 ret
= phylink_mii_read(pl
, mii
->phy_id
, mii
->reg_num
);
1538 ret
= phylink_mii_write(pl
, mii
->phy_id
, mii
->reg_num
,
1550 EXPORT_SYMBOL_GPL(phylink_mii_ioctl
);
1552 static int phylink_sfp_module_insert(void *upstream
,
1553 const struct sfp_eeprom_id
*id
)
1555 struct phylink
*pl
= upstream
;
1556 __ETHTOOL_DECLARE_LINK_MODE_MASK(support
) = { 0, };
1557 struct phylink_link_state config
;
1558 phy_interface_t iface
;
1565 sfp_parse_support(pl
->sfp_bus
, id
, support
);
1566 port
= sfp_parse_port(pl
->sfp_bus
, id
, support
);
1568 memset(&config
, 0, sizeof(config
));
1569 linkmode_copy(config
.advertising
, support
);
1570 config
.interface
= PHY_INTERFACE_MODE_NA
;
1571 config
.speed
= SPEED_UNKNOWN
;
1572 config
.duplex
= DUPLEX_UNKNOWN
;
1573 config
.pause
= MLO_PAUSE_AN
;
1574 config
.an_enabled
= pl
->link_config
.an_enabled
;
1576 /* Ignore errors if we're expecting a PHY to attach later */
1577 ret
= phylink_validate(pl
, support
, &config
);
1579 netdev_err(pl
->netdev
, "validation with support %*pb failed: %d\n",
1580 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
, ret
);
1584 iface
= sfp_select_interface(pl
->sfp_bus
, id
, config
.advertising
);
1585 if (iface
== PHY_INTERFACE_MODE_NA
) {
1586 netdev_err(pl
->netdev
,
1587 "selection of interface failed, advertisement %*pb\n",
1588 __ETHTOOL_LINK_MODE_MASK_NBITS
, config
.advertising
);
1592 config
.interface
= iface
;
1593 ret
= phylink_validate(pl
, support
, &config
);
1595 netdev_err(pl
->netdev
, "validation of %s/%s with support %*pb failed: %d\n",
1596 phylink_an_mode_str(MLO_AN_INBAND
),
1597 phy_modes(config
.interface
),
1598 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
, ret
);
1602 netdev_dbg(pl
->netdev
, "requesting link mode %s/%s with support %*pb\n",
1603 phylink_an_mode_str(MLO_AN_INBAND
),
1604 phy_modes(config
.interface
),
1605 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
);
1607 if (phy_interface_mode_is_8023z(iface
) && pl
->phydev
)
1610 changed
= !bitmap_equal(pl
->supported
, support
,
1611 __ETHTOOL_LINK_MODE_MASK_NBITS
);
1613 linkmode_copy(pl
->supported
, support
);
1614 linkmode_copy(pl
->link_config
.advertising
, config
.advertising
);
1617 if (pl
->link_an_mode
!= MLO_AN_INBAND
||
1618 pl
->link_config
.interface
!= config
.interface
) {
1619 pl
->link_config
.interface
= config
.interface
;
1620 pl
->link_an_mode
= MLO_AN_INBAND
;
1624 netdev_info(pl
->netdev
, "switched to %s/%s link mode\n",
1625 phylink_an_mode_str(MLO_AN_INBAND
),
1626 phy_modes(config
.interface
));
1629 pl
->link_port
= port
;
1631 if (changed
&& !test_bit(PHYLINK_DISABLE_STOPPED
,
1632 &pl
->phylink_disable_state
))
1633 phylink_mac_config(pl
, &pl
->link_config
);
1638 static void phylink_sfp_link_down(void *upstream
)
1640 struct phylink
*pl
= upstream
;
1644 phylink_run_resolve_and_disable(pl
, PHYLINK_DISABLE_LINK
);
1647 static void phylink_sfp_link_up(void *upstream
)
1649 struct phylink
*pl
= upstream
;
1653 clear_bit(PHYLINK_DISABLE_LINK
, &pl
->phylink_disable_state
);
1654 phylink_run_resolve(pl
);
1657 static int phylink_sfp_connect_phy(void *upstream
, struct phy_device
*phy
)
1659 struct phylink
*pl
= upstream
;
1661 return __phylink_connect_phy(upstream
, phy
, pl
->link_config
.interface
);
1664 static void phylink_sfp_disconnect_phy(void *upstream
)
1666 phylink_disconnect_phy(upstream
);
1669 static const struct sfp_upstream_ops sfp_phylink_ops
= {
1670 .module_insert
= phylink_sfp_module_insert
,
1671 .link_up
= phylink_sfp_link_up
,
1672 .link_down
= phylink_sfp_link_down
,
1673 .connect_phy
= phylink_sfp_connect_phy
,
1674 .disconnect_phy
= phylink_sfp_disconnect_phy
,
1677 /* Helpers for MAC drivers */
1680 * phylink_helper_basex_speed() - 1000BaseX/2500BaseX helper
1681 * @state: a pointer to a &struct phylink_link_state
1683 * Inspect the interface mode, advertising mask or forced speed and
1684 * decide whether to run at 2.5Gbit or 1Gbit appropriately, switching
1685 * the interface mode to suit. @state->interface is appropriately
1686 * updated, and the advertising mask has the "other" baseX_Full flag
1689 void phylink_helper_basex_speed(struct phylink_link_state
*state
)
1691 if (phy_interface_mode_is_8023z(state
->interface
)) {
1692 bool want_2500
= state
->an_enabled
?
1693 phylink_test(state
->advertising
, 2500baseX_Full
) :
1694 state
->speed
== SPEED_2500
;
1697 phylink_clear(state
->advertising
, 1000baseX_Full
);
1698 state
->interface
= PHY_INTERFACE_MODE_2500BASEX
;
1700 phylink_clear(state
->advertising
, 2500baseX_Full
);
1701 state
->interface
= PHY_INTERFACE_MODE_1000BASEX
;
1705 EXPORT_SYMBOL_GPL(phylink_helper_basex_speed
);
1707 MODULE_LICENSE("GPL");