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 phylink - internal data type for phylink
44 struct net_device
*netdev
;
45 const struct phylink_mac_ops
*ops
;
47 unsigned long phylink_disable_state
; /* bitmask of disables */
48 struct phy_device
*phydev
;
49 phy_interface_t link_interface
; /* PHY_INTERFACE_xxx */
50 u8 link_an_mode
; /* MLO_AN_xxx */
51 u8 link_port
; /* The current non-phy ethtool port */
52 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
54 /* The link configuration settings */
55 struct phylink_link_state link_config
;
56 struct gpio_desc
*link_gpio
;
57 void (*get_fixed_state
)(struct net_device
*dev
,
58 struct phylink_link_state
*s
);
60 struct mutex state_mutex
;
61 struct phylink_link_state phy_state
;
62 struct work_struct resolve
;
64 bool mac_link_dropped
;
66 struct sfp_bus
*sfp_bus
;
69 static inline void linkmode_zero(unsigned long *dst
)
71 bitmap_zero(dst
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
74 static inline void linkmode_copy(unsigned long *dst
, const unsigned long *src
)
76 bitmap_copy(dst
, src
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
79 static inline void linkmode_and(unsigned long *dst
, const unsigned long *a
,
80 const unsigned long *b
)
82 bitmap_and(dst
, a
, b
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
85 static inline void linkmode_or(unsigned long *dst
, const unsigned long *a
,
86 const unsigned long *b
)
88 bitmap_or(dst
, a
, b
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
91 static inline bool linkmode_empty(const unsigned long *src
)
93 return bitmap_empty(src
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
97 * phylink_set_port_modes() - set the port type modes in the ethtool mask
98 * @mask: ethtool link mode mask
100 * Sets all the port type modes in the ethtool mask. MAC drivers should
101 * use this in their 'validate' callback.
103 void phylink_set_port_modes(unsigned long *mask
)
105 phylink_set(mask
, TP
);
106 phylink_set(mask
, AUI
);
107 phylink_set(mask
, MII
);
108 phylink_set(mask
, FIBRE
);
109 phylink_set(mask
, BNC
);
110 phylink_set(mask
, Backplane
);
112 EXPORT_SYMBOL_GPL(phylink_set_port_modes
);
114 static int phylink_is_empty_linkmode(const unsigned long *linkmode
)
116 __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp
) = { 0, };
118 phylink_set_port_modes(tmp
);
119 phylink_set(tmp
, Autoneg
);
120 phylink_set(tmp
, Pause
);
121 phylink_set(tmp
, Asym_Pause
);
123 bitmap_andnot(tmp
, linkmode
, tmp
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
125 return linkmode_empty(tmp
);
128 static const char *phylink_an_mode_str(unsigned int mode
)
130 static const char *modestr
[] = {
131 [MLO_AN_PHY
] = "phy",
132 [MLO_AN_FIXED
] = "fixed",
133 [MLO_AN_INBAND
] = "inband",
136 return mode
< ARRAY_SIZE(modestr
) ? modestr
[mode
] : "unknown";
139 static int phylink_validate(struct phylink
*pl
, unsigned long *supported
,
140 struct phylink_link_state
*state
)
142 pl
->ops
->validate(pl
->netdev
, supported
, state
);
144 return phylink_is_empty_linkmode(supported
) ? -EINVAL
: 0;
147 static int phylink_parse_fixedlink(struct phylink
*pl
,
148 struct fwnode_handle
*fwnode
)
150 struct fwnode_handle
*fixed_node
;
151 const struct phy_setting
*s
;
152 struct gpio_desc
*desc
;
156 fixed_node
= fwnode_get_named_child_node(fwnode
, "fixed-link");
158 ret
= fwnode_property_read_u32(fixed_node
, "speed", &speed
);
160 pl
->link_config
.speed
= speed
;
161 pl
->link_config
.duplex
= DUPLEX_HALF
;
163 if (fwnode_property_read_bool(fixed_node
, "full-duplex"))
164 pl
->link_config
.duplex
= DUPLEX_FULL
;
166 /* We treat the "pause" and "asym-pause" terminology as
167 * defining the link partner's ability. */
168 if (fwnode_property_read_bool(fixed_node
, "pause"))
169 pl
->link_config
.pause
|= MLO_PAUSE_SYM
;
170 if (fwnode_property_read_bool(fixed_node
, "asym-pause"))
171 pl
->link_config
.pause
|= MLO_PAUSE_ASYM
;
174 desc
= fwnode_get_named_gpiod(fixed_node
, "link-gpios",
178 pl
->link_gpio
= desc
;
179 else if (desc
== ERR_PTR(-EPROBE_DEFER
))
182 fwnode_handle_put(fixed_node
);
189 ret
= fwnode_property_read_u32_array(fwnode
, "fixed-link",
191 if (ret
!= ARRAY_SIZE(prop
)) {
192 netdev_err(pl
->netdev
, "broken fixed-link?\n");
196 ret
= fwnode_property_read_u32_array(fwnode
, "fixed-link",
197 prop
, ARRAY_SIZE(prop
));
199 pl
->link_config
.duplex
= prop
[1] ?
200 DUPLEX_FULL
: DUPLEX_HALF
;
201 pl
->link_config
.speed
= prop
[2];
203 pl
->link_config
.pause
|= MLO_PAUSE_SYM
;
205 pl
->link_config
.pause
|= MLO_PAUSE_ASYM
;
209 if (pl
->link_config
.speed
> SPEED_1000
&&
210 pl
->link_config
.duplex
!= DUPLEX_FULL
)
211 netdev_warn(pl
->netdev
, "fixed link specifies half duplex for %dMbps link?\n",
212 pl
->link_config
.speed
);
214 bitmap_fill(pl
->supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
215 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
216 phylink_validate(pl
, pl
->supported
, &pl
->link_config
);
218 s
= phy_lookup_setting(pl
->link_config
.speed
, pl
->link_config
.duplex
,
220 __ETHTOOL_LINK_MODE_MASK_NBITS
, true);
221 linkmode_zero(pl
->supported
);
222 phylink_set(pl
->supported
, MII
);
224 __set_bit(s
->bit
, pl
->supported
);
226 netdev_warn(pl
->netdev
, "fixed link %s duplex %dMbps not recognised\n",
227 pl
->link_config
.duplex
== DUPLEX_FULL
? "full" : "half",
228 pl
->link_config
.speed
);
231 linkmode_and(pl
->link_config
.advertising
, pl
->link_config
.advertising
,
234 pl
->link_config
.link
= 1;
235 pl
->link_config
.an_complete
= 1;
240 static int phylink_parse_mode(struct phylink
*pl
, struct fwnode_handle
*fwnode
)
242 struct fwnode_handle
*dn
;
245 dn
= fwnode_get_named_child_node(fwnode
, "fixed-link");
246 if (dn
|| fwnode_property_present(fwnode
, "fixed-link"))
247 pl
->link_an_mode
= MLO_AN_FIXED
;
248 fwnode_handle_put(dn
);
250 if (fwnode_property_read_string(fwnode
, "managed", &managed
) == 0 &&
251 strcmp(managed
, "in-band-status") == 0) {
252 if (pl
->link_an_mode
== MLO_AN_FIXED
) {
253 netdev_err(pl
->netdev
,
254 "can't use both fixed-link and in-band-status\n");
258 linkmode_zero(pl
->supported
);
259 phylink_set(pl
->supported
, MII
);
260 phylink_set(pl
->supported
, Autoneg
);
261 phylink_set(pl
->supported
, Asym_Pause
);
262 phylink_set(pl
->supported
, Pause
);
263 pl
->link_config
.an_enabled
= true;
264 pl
->link_an_mode
= MLO_AN_INBAND
;
266 switch (pl
->link_config
.interface
) {
267 case PHY_INTERFACE_MODE_SGMII
:
268 phylink_set(pl
->supported
, 10baseT_Half
);
269 phylink_set(pl
->supported
, 10baseT_Full
);
270 phylink_set(pl
->supported
, 100baseT_Half
);
271 phylink_set(pl
->supported
, 100baseT_Full
);
272 phylink_set(pl
->supported
, 1000baseT_Half
);
273 phylink_set(pl
->supported
, 1000baseT_Full
);
276 case PHY_INTERFACE_MODE_1000BASEX
:
277 phylink_set(pl
->supported
, 1000baseX_Full
);
280 case PHY_INTERFACE_MODE_2500BASEX
:
281 phylink_set(pl
->supported
, 2500baseX_Full
);
284 case PHY_INTERFACE_MODE_10GKR
:
285 phylink_set(pl
->supported
, 10baseT_Half
);
286 phylink_set(pl
->supported
, 10baseT_Full
);
287 phylink_set(pl
->supported
, 100baseT_Half
);
288 phylink_set(pl
->supported
, 100baseT_Full
);
289 phylink_set(pl
->supported
, 1000baseT_Half
);
290 phylink_set(pl
->supported
, 1000baseT_Full
);
291 phylink_set(pl
->supported
, 1000baseX_Full
);
292 phylink_set(pl
->supported
, 10000baseKR_Full
);
293 phylink_set(pl
->supported
, 10000baseCR_Full
);
294 phylink_set(pl
->supported
, 10000baseSR_Full
);
295 phylink_set(pl
->supported
, 10000baseLR_Full
);
296 phylink_set(pl
->supported
, 10000baseLRM_Full
);
297 phylink_set(pl
->supported
, 10000baseER_Full
);
301 netdev_err(pl
->netdev
,
302 "incorrect link mode %s for in-band status\n",
303 phy_modes(pl
->link_config
.interface
));
307 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
309 if (phylink_validate(pl
, pl
->supported
, &pl
->link_config
)) {
310 netdev_err(pl
->netdev
,
311 "failed to validate link configuration for in-band status\n");
319 static void phylink_mac_config(struct phylink
*pl
,
320 const struct phylink_link_state
*state
)
322 netdev_dbg(pl
->netdev
,
323 "%s: mode=%s/%s/%s/%s adv=%*pb pause=%02x link=%u an=%u\n",
324 __func__
, phylink_an_mode_str(pl
->link_an_mode
),
325 phy_modes(state
->interface
),
326 phy_speed_to_str(state
->speed
),
327 phy_duplex_to_str(state
->duplex
),
328 __ETHTOOL_LINK_MODE_MASK_NBITS
, state
->advertising
,
329 state
->pause
, state
->link
, state
->an_enabled
);
331 pl
->ops
->mac_config(pl
->netdev
, pl
->link_an_mode
, state
);
334 static void phylink_mac_an_restart(struct phylink
*pl
)
336 if (pl
->link_config
.an_enabled
&&
337 phy_interface_mode_is_8023z(pl
->link_config
.interface
))
338 pl
->ops
->mac_an_restart(pl
->netdev
);
341 static int phylink_get_mac_state(struct phylink
*pl
, struct phylink_link_state
*state
)
343 struct net_device
*ndev
= pl
->netdev
;
345 linkmode_copy(state
->advertising
, pl
->link_config
.advertising
);
346 linkmode_zero(state
->lp_advertising
);
347 state
->interface
= pl
->link_config
.interface
;
348 state
->an_enabled
= pl
->link_config
.an_enabled
;
351 return pl
->ops
->mac_link_state(ndev
, state
);
354 /* The fixed state is... fixed except for the link state,
355 * which may be determined by a GPIO or a callback.
357 static void phylink_get_fixed_state(struct phylink
*pl
, struct phylink_link_state
*state
)
359 *state
= pl
->link_config
;
360 if (pl
->get_fixed_state
)
361 pl
->get_fixed_state(pl
->netdev
, state
);
362 else if (pl
->link_gpio
)
363 state
->link
= !!gpiod_get_value(pl
->link_gpio
);
366 /* Flow control is resolved according to our and the link partners
367 * advertisments using the following drawn from the 802.3 specs:
368 * Local device Link partner
369 * Pause AsymDir Pause AsymDir Result
374 static void phylink_resolve_flow(struct phylink
*pl
,
375 struct phylink_link_state
*state
)
379 if (pl
->link_config
.pause
& MLO_PAUSE_AN
) {
382 if (phylink_test(pl
->link_config
.advertising
, Pause
))
383 pause
|= MLO_PAUSE_SYM
;
384 if (phylink_test(pl
->link_config
.advertising
, Asym_Pause
))
385 pause
|= MLO_PAUSE_ASYM
;
387 pause
&= state
->pause
;
389 if (pause
& MLO_PAUSE_SYM
)
390 new_pause
= MLO_PAUSE_TX
| MLO_PAUSE_RX
;
391 else if (pause
& MLO_PAUSE_ASYM
)
392 new_pause
= state
->pause
& MLO_PAUSE_SYM
?
393 MLO_PAUSE_RX
: MLO_PAUSE_TX
;
395 new_pause
= pl
->link_config
.pause
& MLO_PAUSE_TXRX_MASK
;
398 state
->pause
&= ~MLO_PAUSE_TXRX_MASK
;
399 state
->pause
|= new_pause
;
402 static const char *phylink_pause_to_str(int pause
)
404 switch (pause
& MLO_PAUSE_TXRX_MASK
) {
405 case MLO_PAUSE_TX
| MLO_PAUSE_RX
:
416 static void phylink_resolve(struct work_struct
*w
)
418 struct phylink
*pl
= container_of(w
, struct phylink
, resolve
);
419 struct phylink_link_state link_state
;
420 struct net_device
*ndev
= pl
->netdev
;
422 mutex_lock(&pl
->state_mutex
);
423 if (pl
->phylink_disable_state
) {
424 pl
->mac_link_dropped
= false;
425 link_state
.link
= false;
426 } else if (pl
->mac_link_dropped
) {
427 link_state
.link
= false;
429 switch (pl
->link_an_mode
) {
431 link_state
= pl
->phy_state
;
432 phylink_resolve_flow(pl
, &link_state
);
433 phylink_mac_config(pl
, &link_state
);
437 phylink_get_fixed_state(pl
, &link_state
);
438 phylink_mac_config(pl
, &link_state
);
442 phylink_get_mac_state(pl
, &link_state
);
444 bool changed
= false;
446 link_state
.link
= link_state
.link
&&
449 if (pl
->phy_state
.interface
!=
450 link_state
.interface
) {
451 link_state
.interface
= pl
->phy_state
.interface
;
455 /* Propagate the flow control from the PHY
456 * to the MAC. Also propagate the interface
459 if (pl
->phy_state
.link
|| changed
) {
460 link_state
.pause
|= pl
->phy_state
.pause
;
461 phylink_resolve_flow(pl
, &link_state
);
463 phylink_mac_config(pl
, &link_state
);
470 if (link_state
.link
!= netif_carrier_ok(ndev
)) {
471 if (!link_state
.link
) {
472 netif_carrier_off(ndev
);
473 pl
->ops
->mac_link_down(ndev
, pl
->link_an_mode
);
474 netdev_info(ndev
, "Link is Down\n");
476 pl
->ops
->mac_link_up(ndev
, pl
->link_an_mode
,
479 netif_carrier_on(ndev
);
482 "Link is Up - %s/%s - flow control %s\n",
483 phy_speed_to_str(link_state
.speed
),
484 phy_duplex_to_str(link_state
.duplex
),
485 phylink_pause_to_str(link_state
.pause
));
488 if (!link_state
.link
&& pl
->mac_link_dropped
) {
489 pl
->mac_link_dropped
= false;
490 queue_work(system_power_efficient_wq
, &pl
->resolve
);
492 mutex_unlock(&pl
->state_mutex
);
495 static void phylink_run_resolve(struct phylink
*pl
)
497 if (!pl
->phylink_disable_state
)
498 queue_work(system_power_efficient_wq
, &pl
->resolve
);
501 static const struct sfp_upstream_ops sfp_phylink_ops
;
503 static int phylink_register_sfp(struct phylink
*pl
,
504 struct fwnode_handle
*fwnode
)
506 struct fwnode_reference_args ref
;
512 ret
= fwnode_property_get_reference_args(fwnode
, "sfp", NULL
,
518 netdev_err(pl
->netdev
, "unable to parse \"sfp\" node: %d\n",
523 pl
->sfp_bus
= sfp_register_upstream(ref
.fwnode
, pl
->netdev
, pl
,
532 * phylink_create() - create a phylink instance
533 * @ndev: a pointer to the &struct net_device
534 * @fwnode: a pointer to a &struct fwnode_handle describing the network
536 * @iface: the desired link mode defined by &typedef phy_interface_t
537 * @ops: a pointer to a &struct phylink_mac_ops for the MAC.
539 * Create a new phylink instance, and parse the link parameters found in @np.
540 * This will parse in-band modes, fixed-link or SFP configuration.
542 * Returns a pointer to a &struct phylink, or an error-pointer value. Users
543 * must use IS_ERR() to check for errors from this function.
545 struct phylink
*phylink_create(struct net_device
*ndev
,
546 struct fwnode_handle
*fwnode
,
547 phy_interface_t iface
,
548 const struct phylink_mac_ops
*ops
)
553 pl
= kzalloc(sizeof(*pl
), GFP_KERNEL
);
555 return ERR_PTR(-ENOMEM
);
557 mutex_init(&pl
->state_mutex
);
558 INIT_WORK(&pl
->resolve
, phylink_resolve
);
560 pl
->phy_state
.interface
= iface
;
561 pl
->link_interface
= iface
;
562 if (iface
== PHY_INTERFACE_MODE_MOCA
)
563 pl
->link_port
= PORT_BNC
;
565 pl
->link_port
= PORT_MII
;
566 pl
->link_config
.interface
= iface
;
567 pl
->link_config
.pause
= MLO_PAUSE_AN
;
568 pl
->link_config
.speed
= SPEED_UNKNOWN
;
569 pl
->link_config
.duplex
= DUPLEX_UNKNOWN
;
570 pl
->link_config
.an_enabled
= true;
572 __set_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
574 bitmap_fill(pl
->supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
575 linkmode_copy(pl
->link_config
.advertising
, pl
->supported
);
576 phylink_validate(pl
, pl
->supported
, &pl
->link_config
);
578 ret
= phylink_parse_mode(pl
, fwnode
);
584 if (pl
->link_an_mode
== MLO_AN_FIXED
) {
585 ret
= phylink_parse_fixedlink(pl
, fwnode
);
592 ret
= phylink_register_sfp(pl
, fwnode
);
600 EXPORT_SYMBOL_GPL(phylink_create
);
603 * phylink_destroy() - cleanup and destroy the phylink instance
604 * @pl: a pointer to a &struct phylink returned from phylink_create()
606 * Destroy a phylink instance. Any PHY that has been attached must have been
607 * cleaned up via phylink_disconnect_phy() prior to calling this function.
609 void phylink_destroy(struct phylink
*pl
)
612 sfp_unregister_upstream(pl
->sfp_bus
);
614 cancel_work_sync(&pl
->resolve
);
617 EXPORT_SYMBOL_GPL(phylink_destroy
);
619 static void phylink_phy_change(struct phy_device
*phydev
, bool up
,
622 struct phylink
*pl
= phydev
->phylink
;
624 mutex_lock(&pl
->state_mutex
);
625 pl
->phy_state
.speed
= phydev
->speed
;
626 pl
->phy_state
.duplex
= phydev
->duplex
;
627 pl
->phy_state
.pause
= MLO_PAUSE_NONE
;
629 pl
->phy_state
.pause
|= MLO_PAUSE_SYM
;
630 if (phydev
->asym_pause
)
631 pl
->phy_state
.pause
|= MLO_PAUSE_ASYM
;
632 pl
->phy_state
.interface
= phydev
->interface
;
633 pl
->phy_state
.link
= up
;
634 mutex_unlock(&pl
->state_mutex
);
636 phylink_run_resolve(pl
);
638 netdev_dbg(pl
->netdev
, "phy link %s %s/%s/%s\n", up
? "up" : "down",
639 phy_modes(phydev
->interface
),
640 phy_speed_to_str(phydev
->speed
),
641 phy_duplex_to_str(phydev
->duplex
));
644 static int phylink_bringup_phy(struct phylink
*pl
, struct phy_device
*phy
)
646 struct phylink_link_state config
;
647 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
651 memset(&config
, 0, sizeof(config
));
652 ethtool_convert_legacy_u32_to_link_mode(supported
, phy
->supported
);
653 ethtool_convert_legacy_u32_to_link_mode(config
.advertising
,
655 config
.interface
= pl
->link_config
.interface
;
658 * This is the new way of dealing with flow control for PHYs,
659 * as described by Timur Tabi in commit 529ed1275263 ("net: phy:
660 * phy drivers should not set SUPPORTED_[Asym_]Pause") except
661 * using our validate call to the MAC, we rely upon the MAC
662 * clearing the bits from both supported and advertising fields.
664 if (phylink_test(supported
, Pause
))
665 phylink_set(config
.advertising
, Pause
);
666 if (phylink_test(supported
, Asym_Pause
))
667 phylink_set(config
.advertising
, Asym_Pause
);
669 ret
= phylink_validate(pl
, supported
, &config
);
674 phy
->phy_link_change
= phylink_phy_change
;
676 netdev_info(pl
->netdev
,
677 "PHY [%s] driver [%s]\n", dev_name(&phy
->mdio
.dev
),
680 mutex_lock(&phy
->lock
);
681 mutex_lock(&pl
->state_mutex
);
682 pl
->netdev
->phydev
= phy
;
684 linkmode_copy(pl
->supported
, supported
);
685 linkmode_copy(pl
->link_config
.advertising
, config
.advertising
);
687 /* Restrict the phy advertisment according to the MAC support. */
688 ethtool_convert_link_mode_to_legacy_u32(&advertising
, config
.advertising
);
689 phy
->advertising
= advertising
;
690 mutex_unlock(&pl
->state_mutex
);
691 mutex_unlock(&phy
->lock
);
693 netdev_dbg(pl
->netdev
,
694 "phy: setting supported %*pb advertising 0x%08x\n",
695 __ETHTOOL_LINK_MODE_MASK_NBITS
, pl
->supported
,
698 phy_start_machine(phy
);
700 phy_start_interrupts(phy
);
706 * phylink_connect_phy() - connect a PHY to the phylink instance
707 * @pl: a pointer to a &struct phylink returned from phylink_create()
708 * @phy: a pointer to a &struct phy_device.
710 * Connect @phy to the phylink instance specified by @pl by calling
711 * phy_attach_direct(). Configure the @phy according to the MAC driver's
712 * capabilities, start the PHYLIB state machine and enable any interrupts
713 * that the PHY supports.
715 * This updates the phylink's ethtool supported and advertising link mode
718 * Returns 0 on success or a negative errno.
720 int phylink_connect_phy(struct phylink
*pl
, struct phy_device
*phy
)
724 if (WARN_ON(pl
->link_an_mode
== MLO_AN_FIXED
||
725 (pl
->link_an_mode
== MLO_AN_INBAND
&&
726 phy_interface_mode_is_8023z(pl
->link_interface
))))
732 /* Use PHY device/driver interface */
733 if (pl
->link_interface
== PHY_INTERFACE_MODE_NA
) {
734 pl
->link_interface
= phy
->interface
;
735 pl
->link_config
.interface
= pl
->link_interface
;
738 ret
= phy_attach_direct(pl
->netdev
, phy
, 0, pl
->link_interface
);
742 ret
= phylink_bringup_phy(pl
, phy
);
748 EXPORT_SYMBOL_GPL(phylink_connect_phy
);
751 * phylink_of_phy_connect() - connect the PHY specified in the DT mode.
752 * @pl: a pointer to a &struct phylink returned from phylink_create()
753 * @dn: a pointer to a &struct device_node.
754 * @flags: PHY-specific flags to communicate to the PHY device driver
756 * Connect the phy specified in the device node @dn to the phylink instance
757 * specified by @pl. Actions specified in phylink_connect_phy() will be
760 * Returns 0 on success or a negative errno.
762 int phylink_of_phy_connect(struct phylink
*pl
, struct device_node
*dn
,
765 struct device_node
*phy_node
;
766 struct phy_device
*phy_dev
;
769 /* Fixed links and 802.3z are handled without needing a PHY */
770 if (pl
->link_an_mode
== MLO_AN_FIXED
||
771 (pl
->link_an_mode
== MLO_AN_INBAND
&&
772 phy_interface_mode_is_8023z(pl
->link_interface
)))
775 phy_node
= of_parse_phandle(dn
, "phy-handle", 0);
777 phy_node
= of_parse_phandle(dn
, "phy", 0);
779 phy_node
= of_parse_phandle(dn
, "phy-device", 0);
782 if (pl
->link_an_mode
== MLO_AN_PHY
)
787 phy_dev
= of_phy_attach(pl
->netdev
, phy_node
, flags
,
789 /* We're done with the phy_node handle */
790 of_node_put(phy_node
);
795 ret
= phylink_bringup_phy(pl
, phy_dev
);
801 EXPORT_SYMBOL_GPL(phylink_of_phy_connect
);
804 * phylink_disconnect_phy() - disconnect any PHY attached to the phylink
806 * @pl: a pointer to a &struct phylink returned from phylink_create()
808 * Disconnect any current PHY from the phylink instance described by @pl.
810 void phylink_disconnect_phy(struct phylink
*pl
)
812 struct phy_device
*phy
;
818 mutex_lock(&phy
->lock
);
819 mutex_lock(&pl
->state_mutex
);
820 pl
->netdev
->phydev
= NULL
;
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 /* Apply the link configuration to the MAC when starting. This allows
891 * a fixed-link to start with the correct parameters, and also
892 * ensures that we set the appropriate advertisment for Serdes links.
894 phylink_resolve_flow(pl
, &pl
->link_config
);
895 phylink_mac_config(pl
, &pl
->link_config
);
897 /* Restart autonegotiation if using 802.3z to ensure that the link
898 * parameters are properly negotiated. This is necessary for DSA
899 * switches using 802.3z negotiation to ensure they see our modes.
901 phylink_mac_an_restart(pl
);
903 clear_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
904 phylink_run_resolve(pl
);
907 sfp_upstream_start(pl
->sfp_bus
);
909 phy_start(pl
->phydev
);
911 EXPORT_SYMBOL_GPL(phylink_start
);
914 * phylink_stop() - stop a phylink instance
915 * @pl: a pointer to a &struct phylink returned from phylink_create()
917 * Stop the phylink instance specified by @pl. This should be called from the
918 * network device driver's &struct net_device_ops ndo_stop() method. The
919 * network device's carrier state should not be changed prior to calling this
922 void phylink_stop(struct phylink
*pl
)
927 phy_stop(pl
->phydev
);
929 sfp_upstream_stop(pl
->sfp_bus
);
931 set_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
);
932 queue_work(system_power_efficient_wq
, &pl
->resolve
);
933 flush_work(&pl
->resolve
);
935 EXPORT_SYMBOL_GPL(phylink_stop
);
938 * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY
939 * @pl: a pointer to a &struct phylink returned from phylink_create()
940 * @wol: a pointer to &struct ethtool_wolinfo to hold the read parameters
942 * Read the wake on lan parameters from the PHY attached to the phylink
943 * instance specified by @pl. If no PHY is currently attached, report no
944 * support for wake on lan.
946 void phylink_ethtool_get_wol(struct phylink
*pl
, struct ethtool_wolinfo
*wol
)
954 phy_ethtool_get_wol(pl
->phydev
, wol
);
956 EXPORT_SYMBOL_GPL(phylink_ethtool_get_wol
);
959 * phylink_ethtool_set_wol() - set wake on lan parameters
960 * @pl: a pointer to a &struct phylink returned from phylink_create()
961 * @wol: a pointer to &struct ethtool_wolinfo for the desired parameters
963 * Set the wake on lan parameters for the PHY attached to the phylink
964 * instance specified by @pl. If no PHY is attached, returns %EOPNOTSUPP
967 * Returns zero on success or negative errno code.
969 int phylink_ethtool_set_wol(struct phylink
*pl
, struct ethtool_wolinfo
*wol
)
971 int ret
= -EOPNOTSUPP
;
976 ret
= phy_ethtool_set_wol(pl
->phydev
, wol
);
980 EXPORT_SYMBOL_GPL(phylink_ethtool_set_wol
);
982 static void phylink_merge_link_mode(unsigned long *dst
, const unsigned long *b
)
984 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
);
987 phylink_set_port_modes(mask
);
989 linkmode_and(dst
, dst
, mask
);
990 linkmode_or(dst
, dst
, b
);
993 static void phylink_get_ksettings(const struct phylink_link_state
*state
,
994 struct ethtool_link_ksettings
*kset
)
996 phylink_merge_link_mode(kset
->link_modes
.advertising
, state
->advertising
);
997 linkmode_copy(kset
->link_modes
.lp_advertising
, state
->lp_advertising
);
998 kset
->base
.speed
= state
->speed
;
999 kset
->base
.duplex
= state
->duplex
;
1000 kset
->base
.autoneg
= state
->an_enabled
? AUTONEG_ENABLE
:
1005 * phylink_ethtool_ksettings_get() - get the current link settings
1006 * @pl: a pointer to a &struct phylink returned from phylink_create()
1007 * @kset: a pointer to a &struct ethtool_link_ksettings to hold link settings
1009 * Read the current link settings for the phylink instance specified by @pl.
1010 * This will be the link settings read from the MAC, PHY or fixed link
1011 * settings depending on the current negotiation mode.
1013 int phylink_ethtool_ksettings_get(struct phylink
*pl
,
1014 struct ethtool_link_ksettings
*kset
)
1016 struct phylink_link_state link_state
;
1021 phy_ethtool_ksettings_get(pl
->phydev
, kset
);
1023 kset
->base
.port
= pl
->link_port
;
1026 linkmode_copy(kset
->link_modes
.supported
, pl
->supported
);
1028 switch (pl
->link_an_mode
) {
1030 /* We are using fixed settings. Report these as the
1031 * current link settings - and note that these also
1032 * represent the supported speeds/duplex/pause modes.
1034 phylink_get_fixed_state(pl
, &link_state
);
1035 phylink_get_ksettings(&link_state
, kset
);
1039 /* If there is a phy attached, then use the reported
1040 * settings from the phy with no modification.
1045 phylink_get_mac_state(pl
, &link_state
);
1047 /* The MAC is reporting the link results from its own PCS
1048 * layer via in-band status. Report these as the current
1051 phylink_get_ksettings(&link_state
, kset
);
1057 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_get
);
1060 * phylink_ethtool_ksettings_set() - set the link settings
1061 * @pl: a pointer to a &struct phylink returned from phylink_create()
1062 * @kset: a pointer to a &struct ethtool_link_ksettings for the desired modes
1064 int phylink_ethtool_ksettings_set(struct phylink
*pl
,
1065 const struct ethtool_link_ksettings
*kset
)
1067 struct ethtool_link_ksettings our_kset
;
1068 struct phylink_link_state config
;
1073 if (kset
->base
.autoneg
!= AUTONEG_DISABLE
&&
1074 kset
->base
.autoneg
!= AUTONEG_ENABLE
)
1077 config
= pl
->link_config
;
1079 /* Mask out unsupported advertisments */
1080 linkmode_and(config
.advertising
, kset
->link_modes
.advertising
,
1083 /* FIXME: should we reject autoneg if phy/mac does not support it? */
1084 if (kset
->base
.autoneg
== AUTONEG_DISABLE
) {
1085 const struct phy_setting
*s
;
1087 /* Autonegotiation disabled, select a suitable speed and
1090 s
= phy_lookup_setting(kset
->base
.speed
, kset
->base
.duplex
,
1092 __ETHTOOL_LINK_MODE_MASK_NBITS
, false);
1096 /* If we have a fixed link (as specified by firmware), refuse
1097 * to change link parameters.
1099 if (pl
->link_an_mode
== MLO_AN_FIXED
&&
1100 (s
->speed
!= pl
->link_config
.speed
||
1101 s
->duplex
!= pl
->link_config
.duplex
))
1104 config
.speed
= s
->speed
;
1105 config
.duplex
= s
->duplex
;
1106 config
.an_enabled
= false;
1108 __clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, config
.advertising
);
1110 /* If we have a fixed link, refuse to enable autonegotiation */
1111 if (pl
->link_an_mode
== MLO_AN_FIXED
)
1114 config
.speed
= SPEED_UNKNOWN
;
1115 config
.duplex
= DUPLEX_UNKNOWN
;
1116 config
.an_enabled
= true;
1118 __set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, config
.advertising
);
1121 if (phylink_validate(pl
, pl
->supported
, &config
))
1124 /* If autonegotiation is enabled, we must have an advertisment */
1125 if (config
.an_enabled
&& phylink_is_empty_linkmode(config
.advertising
))
1129 linkmode_copy(our_kset
.link_modes
.advertising
, config
.advertising
);
1130 our_kset
.base
.speed
= config
.speed
;
1131 our_kset
.base
.duplex
= config
.duplex
;
1133 /* If we have a PHY, configure the phy */
1135 ret
= phy_ethtool_ksettings_set(pl
->phydev
, &our_kset
);
1140 mutex_lock(&pl
->state_mutex
);
1141 /* Configure the MAC to match the new settings */
1142 linkmode_copy(pl
->link_config
.advertising
, our_kset
.link_modes
.advertising
);
1143 pl
->link_config
.interface
= config
.interface
;
1144 pl
->link_config
.speed
= our_kset
.base
.speed
;
1145 pl
->link_config
.duplex
= our_kset
.base
.duplex
;
1146 pl
->link_config
.an_enabled
= our_kset
.base
.autoneg
!= AUTONEG_DISABLE
;
1148 if (!test_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
)) {
1149 phylink_mac_config(pl
, &pl
->link_config
);
1150 phylink_mac_an_restart(pl
);
1152 mutex_unlock(&pl
->state_mutex
);
1156 EXPORT_SYMBOL_GPL(phylink_ethtool_ksettings_set
);
1159 * phylink_ethtool_nway_reset() - restart negotiation
1160 * @pl: a pointer to a &struct phylink returned from phylink_create()
1162 * Restart negotiation for the phylink instance specified by @pl. This will
1163 * cause any attached phy to restart negotiation with the link partner, and
1164 * if the MAC is in a BaseX mode, the MAC will also be requested to restart
1167 * Returns zero on success, or negative error code.
1169 int phylink_ethtool_nway_reset(struct phylink
*pl
)
1176 ret
= phy_restart_aneg(pl
->phydev
);
1177 phylink_mac_an_restart(pl
);
1181 EXPORT_SYMBOL_GPL(phylink_ethtool_nway_reset
);
1184 * phylink_ethtool_get_pauseparam() - get the current pause parameters
1185 * @pl: a pointer to a &struct phylink returned from phylink_create()
1186 * @pause: a pointer to a &struct ethtool_pauseparam
1188 void phylink_ethtool_get_pauseparam(struct phylink
*pl
,
1189 struct ethtool_pauseparam
*pause
)
1193 pause
->autoneg
= !!(pl
->link_config
.pause
& MLO_PAUSE_AN
);
1194 pause
->rx_pause
= !!(pl
->link_config
.pause
& MLO_PAUSE_RX
);
1195 pause
->tx_pause
= !!(pl
->link_config
.pause
& MLO_PAUSE_TX
);
1197 EXPORT_SYMBOL_GPL(phylink_ethtool_get_pauseparam
);
1200 * phylink_ethtool_set_pauseparam() - set the current pause parameters
1201 * @pl: a pointer to a &struct phylink returned from phylink_create()
1202 * @pause: a pointer to a &struct ethtool_pauseparam
1204 int phylink_ethtool_set_pauseparam(struct phylink
*pl
,
1205 struct ethtool_pauseparam
*pause
)
1207 struct phylink_link_state
*config
= &pl
->link_config
;
1211 if (!phylink_test(pl
->supported
, Pause
) &&
1212 !phylink_test(pl
->supported
, Asym_Pause
))
1215 if (!phylink_test(pl
->supported
, Asym_Pause
) &&
1216 !pause
->autoneg
&& pause
->rx_pause
!= pause
->tx_pause
)
1219 config
->pause
&= ~(MLO_PAUSE_AN
| MLO_PAUSE_TXRX_MASK
);
1222 config
->pause
|= MLO_PAUSE_AN
;
1223 if (pause
->rx_pause
)
1224 config
->pause
|= MLO_PAUSE_RX
;
1225 if (pause
->tx_pause
)
1226 config
->pause
|= MLO_PAUSE_TX
;
1228 if (!test_bit(PHYLINK_DISABLE_STOPPED
, &pl
->phylink_disable_state
)) {
1229 switch (pl
->link_an_mode
) {
1231 /* Silently mark the carrier down, and then trigger a resolve */
1232 netif_carrier_off(pl
->netdev
);
1233 phylink_run_resolve(pl
);
1237 /* Should we allow fixed links to change against the config? */
1238 phylink_resolve_flow(pl
, config
);
1239 phylink_mac_config(pl
, config
);
1243 phylink_mac_config(pl
, config
);
1244 phylink_mac_an_restart(pl
);
1251 EXPORT_SYMBOL_GPL(phylink_ethtool_set_pauseparam
);
1253 int phylink_ethtool_get_module_info(struct phylink
*pl
,
1254 struct ethtool_modinfo
*modinfo
)
1256 int ret
= -EOPNOTSUPP
;
1258 WARN_ON(!lockdep_rtnl_is_held());
1261 ret
= sfp_get_module_info(pl
->sfp_bus
, modinfo
);
1265 EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_info
);
1267 int phylink_ethtool_get_module_eeprom(struct phylink
*pl
,
1268 struct ethtool_eeprom
*ee
, u8
*buf
)
1270 int ret
= -EOPNOTSUPP
;
1272 WARN_ON(!lockdep_rtnl_is_held());
1275 ret
= sfp_get_module_eeprom(pl
->sfp_bus
, ee
, buf
);
1279 EXPORT_SYMBOL_GPL(phylink_ethtool_get_module_eeprom
);
1282 * phylink_ethtool_get_eee_err() - read the energy efficient ethernet error
1284 * @pl: a pointer to a &struct phylink returned from phylink_create().
1286 * Read the Energy Efficient Ethernet error counter from the PHY associated
1287 * with the phylink instance specified by @pl.
1289 * Returns positive error counter value, or negative error code.
1291 int phylink_get_eee_err(struct phylink
*pl
)
1298 ret
= phy_get_eee_err(pl
->phydev
);
1302 EXPORT_SYMBOL_GPL(phylink_get_eee_err
);
1305 * phylink_ethtool_get_eee() - read the energy efficient ethernet parameters
1306 * @pl: a pointer to a &struct phylink returned from phylink_create()
1307 * @eee: a pointer to a &struct ethtool_eee for the read parameters
1309 int phylink_ethtool_get_eee(struct phylink
*pl
, struct ethtool_eee
*eee
)
1311 int ret
= -EOPNOTSUPP
;
1316 ret
= phy_ethtool_get_eee(pl
->phydev
, eee
);
1320 EXPORT_SYMBOL_GPL(phylink_ethtool_get_eee
);
1323 * phylink_ethtool_set_eee() - set the energy efficient ethernet parameters
1324 * @pl: a pointer to a &struct phylink returned from phylink_create()
1325 * @eee: a pointer to a &struct ethtool_eee for the desired parameters
1327 int phylink_ethtool_set_eee(struct phylink
*pl
, struct ethtool_eee
*eee
)
1329 int ret
= -EOPNOTSUPP
;
1334 ret
= phy_ethtool_set_eee(pl
->phydev
, eee
);
1338 EXPORT_SYMBOL_GPL(phylink_ethtool_set_eee
);
1340 /* This emulates MII registers for a fixed-mode phy operating as per the
1341 * passed in state. "aneg" defines if we report negotiation is possible.
1343 * FIXME: should deal with negotiation state too.
1345 static int phylink_mii_emul_read(struct net_device
*ndev
, unsigned int reg
,
1346 struct phylink_link_state
*state
, bool aneg
)
1348 struct fixed_phy_status fs
;
1351 fs
.link
= state
->link
;
1352 fs
.speed
= state
->speed
;
1353 fs
.duplex
= state
->duplex
;
1354 fs
.pause
= state
->pause
& MLO_PAUSE_SYM
;
1355 fs
.asym_pause
= state
->pause
& MLO_PAUSE_ASYM
;
1357 val
= swphy_read_reg(reg
, &fs
);
1358 if (reg
== MII_BMSR
) {
1359 if (!state
->an_complete
)
1360 val
&= ~BMSR_ANEGCOMPLETE
;
1362 val
&= ~BMSR_ANEGCAPABLE
;
1367 static int phylink_phy_read(struct phylink
*pl
, unsigned int phy_id
,
1370 struct phy_device
*phydev
= pl
->phydev
;
1373 if (mdio_phy_id_is_c45(phy_id
)) {
1374 prtad
= mdio_phy_id_prtad(phy_id
);
1375 devad
= mdio_phy_id_devad(phy_id
);
1376 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1377 } else if (phydev
->is_c45
) {
1383 devad
= __ffs(phydev
->c45_ids
.devices_in_package
);
1387 if (!(phydev
->c45_ids
.devices_in_package
& MDIO_DEVS_AN
))
1389 devad
= MDIO_MMD_AN
;
1390 if (reg
== MII_ADVERTISE
)
1391 reg
= MDIO_AN_ADVERTISE
;
1399 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1404 return mdiobus_read(pl
->phydev
->mdio
.bus
, prtad
, devad
);
1407 static int phylink_phy_write(struct phylink
*pl
, unsigned int phy_id
,
1408 unsigned int reg
, unsigned int val
)
1410 struct phy_device
*phydev
= pl
->phydev
;
1413 if (mdio_phy_id_is_c45(phy_id
)) {
1414 prtad
= mdio_phy_id_prtad(phy_id
);
1415 devad
= mdio_phy_id_devad(phy_id
);
1416 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1417 } else if (phydev
->is_c45
) {
1423 devad
= __ffs(phydev
->c45_ids
.devices_in_package
);
1427 if (!(phydev
->c45_ids
.devices_in_package
& MDIO_DEVS_AN
))
1429 devad
= MDIO_MMD_AN
;
1430 if (reg
== MII_ADVERTISE
)
1431 reg
= MDIO_AN_ADVERTISE
;
1439 devad
= MII_ADDR_C45
| devad
<< 16 | reg
;
1445 return mdiobus_write(phydev
->mdio
.bus
, prtad
, devad
, val
);
1448 static int phylink_mii_read(struct phylink
*pl
, unsigned int phy_id
,
1451 struct phylink_link_state state
;
1454 switch (pl
->link_an_mode
) {
1457 phylink_get_fixed_state(pl
, &state
);
1458 val
= phylink_mii_emul_read(pl
->netdev
, reg
, &state
,
1468 val
= phylink_get_mac_state(pl
, &state
);
1472 val
= phylink_mii_emul_read(pl
->netdev
, reg
, &state
,
1478 return val
& 0xffff;
1481 static int phylink_mii_write(struct phylink
*pl
, unsigned int phy_id
,
1482 unsigned int reg
, unsigned int val
)
1484 switch (pl
->link_an_mode
) {
1499 * phylink_mii_ioctl() - generic mii ioctl interface
1500 * @pl: a pointer to a &struct phylink returned from phylink_create()
1501 * @ifr: a pointer to a &struct ifreq for socket ioctls
1502 * @cmd: ioctl cmd to execute
1504 * Perform the specified MII ioctl on the PHY attached to the phylink instance
1505 * specified by @pl. If no PHY is attached, emulate the presence of the PHY.
1507 * Returns: zero on success or negative error code.
1510 * read register from the current PHY.
1512 * read register from the specified PHY.
1514 * set a register on the specified PHY.
1516 int phylink_mii_ioctl(struct phylink
*pl
, struct ifreq
*ifr
, int cmd
)
1518 struct mii_ioctl_data
*mii
= if_mii(ifr
);
1524 /* PHYs only exist for MLO_AN_PHY and SGMII */
1527 mii
->phy_id
= pl
->phydev
->mdio
.addr
;
1531 ret
= phylink_phy_read(pl
, mii
->phy_id
, mii
->reg_num
);
1539 ret
= phylink_phy_write(pl
, mii
->phy_id
, mii
->reg_num
,
1544 ret
= phy_mii_ioctl(pl
->phydev
, ifr
, cmd
);
1554 ret
= phylink_mii_read(pl
, mii
->phy_id
, mii
->reg_num
);
1562 ret
= phylink_mii_write(pl
, mii
->phy_id
, mii
->reg_num
,
1574 EXPORT_SYMBOL_GPL(phylink_mii_ioctl
);
1576 static int phylink_sfp_module_insert(void *upstream
,
1577 const struct sfp_eeprom_id
*id
)
1579 struct phylink
*pl
= upstream
;
1580 __ETHTOOL_DECLARE_LINK_MODE_MASK(support
) = { 0, };
1581 struct phylink_link_state config
;
1582 phy_interface_t iface
;
1587 sfp_parse_support(pl
->sfp_bus
, id
, support
);
1588 port
= sfp_parse_port(pl
->sfp_bus
, id
, support
);
1589 iface
= sfp_parse_interface(pl
->sfp_bus
, id
);
1594 case PHY_INTERFACE_MODE_SGMII
:
1595 case PHY_INTERFACE_MODE_1000BASEX
:
1596 case PHY_INTERFACE_MODE_2500BASEX
:
1597 case PHY_INTERFACE_MODE_10GKR
:
1603 memset(&config
, 0, sizeof(config
));
1604 linkmode_copy(config
.advertising
, support
);
1605 config
.interface
= iface
;
1606 config
.speed
= SPEED_UNKNOWN
;
1607 config
.duplex
= DUPLEX_UNKNOWN
;
1608 config
.pause
= MLO_PAUSE_AN
;
1609 config
.an_enabled
= pl
->link_config
.an_enabled
;
1611 /* Ignore errors if we're expecting a PHY to attach later */
1612 ret
= phylink_validate(pl
, support
, &config
);
1614 netdev_err(pl
->netdev
, "validation of %s/%s with support %*pb failed: %d\n",
1615 phylink_an_mode_str(MLO_AN_INBAND
),
1616 phy_modes(config
.interface
),
1617 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
, ret
);
1621 netdev_dbg(pl
->netdev
, "requesting link mode %s/%s with support %*pb\n",
1622 phylink_an_mode_str(MLO_AN_INBAND
),
1623 phy_modes(config
.interface
),
1624 __ETHTOOL_LINK_MODE_MASK_NBITS
, support
);
1626 if (phy_interface_mode_is_8023z(iface
) && pl
->phydev
)
1629 changed
= !bitmap_equal(pl
->supported
, support
,
1630 __ETHTOOL_LINK_MODE_MASK_NBITS
);
1632 linkmode_copy(pl
->supported
, support
);
1633 linkmode_copy(pl
->link_config
.advertising
, config
.advertising
);
1636 if (pl
->link_an_mode
!= MLO_AN_INBAND
||
1637 pl
->link_config
.interface
!= config
.interface
) {
1638 pl
->link_config
.interface
= config
.interface
;
1639 pl
->link_an_mode
= MLO_AN_INBAND
;
1643 netdev_info(pl
->netdev
, "switched to %s/%s link mode\n",
1644 phylink_an_mode_str(MLO_AN_INBAND
),
1645 phy_modes(config
.interface
));
1648 pl
->link_port
= port
;
1650 if (changed
&& !test_bit(PHYLINK_DISABLE_STOPPED
,
1651 &pl
->phylink_disable_state
))
1652 phylink_mac_config(pl
, &pl
->link_config
);
1657 static void phylink_sfp_link_down(void *upstream
)
1659 struct phylink
*pl
= upstream
;
1663 set_bit(PHYLINK_DISABLE_LINK
, &pl
->phylink_disable_state
);
1664 queue_work(system_power_efficient_wq
, &pl
->resolve
);
1665 flush_work(&pl
->resolve
);
1668 static void phylink_sfp_link_up(void *upstream
)
1670 struct phylink
*pl
= upstream
;
1674 clear_bit(PHYLINK_DISABLE_LINK
, &pl
->phylink_disable_state
);
1675 phylink_run_resolve(pl
);
1678 static int phylink_sfp_connect_phy(void *upstream
, struct phy_device
*phy
)
1680 return phylink_connect_phy(upstream
, phy
);
1683 static void phylink_sfp_disconnect_phy(void *upstream
)
1685 phylink_disconnect_phy(upstream
);
1688 static const struct sfp_upstream_ops sfp_phylink_ops
= {
1689 .module_insert
= phylink_sfp_module_insert
,
1690 .link_up
= phylink_sfp_link_up
,
1691 .link_down
= phylink_sfp_link_down
,
1692 .connect_phy
= phylink_sfp_connect_phy
,
1693 .disconnect_phy
= phylink_sfp_disconnect_phy
,
1696 MODULE_LICENSE("GPL");