1 // SPDX-License-Identifier: GPL-2.0+
2 /* Framework for configuring and reading PHY devices
3 * Based on code in sungem_phy.c and gianfar_phy.c
7 * Copyright (c) 2004 Freescale Semiconductor, Inc.
8 * Copyright (c) 2006, 2007 Maciej W. Rozycki
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/unistd.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/skbuff.h>
21 #include <linux/module.h>
22 #include <linux/mii.h>
23 #include <linux/ethtool.h>
24 #include <linux/phy.h>
25 #include <linux/phy_led_triggers.h>
26 #include <linux/workqueue.h>
27 #include <linux/mdio.h>
29 #include <linux/uaccess.h>
30 #include <linux/atomic.h>
32 #define PHY_STATE_STR(_state) \
34 return __stringify(_state); \
36 static const char *phy_state_to_str(enum phy_state st)
42 PHY_STATE_STR(RUNNING
)
44 PHY_STATE_STR(FORCING
)
46 PHY_STATE_STR(RESUMING
)
52 static void phy_link_up(struct phy_device
*phydev
)
54 phydev
->phy_link_change(phydev
, true, true);
55 phy_led_trigger_change_speed(phydev
);
58 static void phy_link_down(struct phy_device
*phydev
, bool do_carrier
)
60 phydev
->phy_link_change(phydev
, false, do_carrier
);
61 phy_led_trigger_change_speed(phydev
);
65 * phy_print_status - Convenience function to print out the current phy status
66 * @phydev: the phy_device struct
68 void phy_print_status(struct phy_device
*phydev
)
71 netdev_info(phydev
->attached_dev
,
72 "Link is Up - %s/%s - flow control %s\n",
73 phy_speed_to_str(phydev
->speed
),
74 phy_duplex_to_str(phydev
->duplex
),
75 phydev
->pause
? "rx/tx" : "off");
77 netdev_info(phydev
->attached_dev
, "Link is Down\n");
80 EXPORT_SYMBOL(phy_print_status
);
83 * phy_clear_interrupt - Ack the phy device's interrupt
84 * @phydev: the phy_device struct
86 * If the @phydev driver has an ack_interrupt function, call it to
87 * ack and clear the phy device's interrupt.
89 * Returns 0 on success or < 0 on error.
91 static int phy_clear_interrupt(struct phy_device
*phydev
)
93 if (phydev
->drv
->ack_interrupt
)
94 return phydev
->drv
->ack_interrupt(phydev
);
100 * phy_config_interrupt - configure the PHY device for the requested interrupts
101 * @phydev: the phy_device struct
102 * @interrupts: interrupt flags to configure for this @phydev
104 * Returns 0 on success or < 0 on error.
106 static int phy_config_interrupt(struct phy_device
*phydev
, bool interrupts
)
108 phydev
->interrupts
= interrupts
? 1 : 0;
109 if (phydev
->drv
->config_intr
)
110 return phydev
->drv
->config_intr(phydev
);
116 * phy_restart_aneg - restart auto-negotiation
117 * @phydev: target phy_device struct
119 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
120 * negative errno on error.
122 int phy_restart_aneg(struct phy_device
*phydev
)
126 if (phydev
->is_c45
&& !(phydev
->c45_ids
.devices_in_package
& BIT(0)))
127 ret
= genphy_c45_restart_aneg(phydev
);
129 ret
= genphy_restart_aneg(phydev
);
133 EXPORT_SYMBOL_GPL(phy_restart_aneg
);
136 * phy_aneg_done - return auto-negotiation status
137 * @phydev: target phy_device struct
139 * Description: Return the auto-negotiation status from this @phydev
140 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
143 int phy_aneg_done(struct phy_device
*phydev
)
145 if (phydev
->drv
&& phydev
->drv
->aneg_done
)
146 return phydev
->drv
->aneg_done(phydev
);
148 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
149 * implement Clause 22 registers
151 if (phydev
->is_c45
&& !(phydev
->c45_ids
.devices_in_package
& BIT(0)))
154 return genphy_aneg_done(phydev
);
156 EXPORT_SYMBOL(phy_aneg_done
);
159 * phy_find_valid - find a PHY setting that matches the requested parameters
160 * @speed: desired speed
161 * @duplex: desired duplex
162 * @supported: mask of supported link modes
164 * Locate a supported phy setting that is, in priority order:
165 * - an exact match for the specified speed and duplex mode
166 * - a match for the specified speed, or slower speed
167 * - the slowest supported speed
168 * Returns the matched phy_setting entry, or %NULL if no supported phy
169 * settings were found.
171 static const struct phy_setting
*
172 phy_find_valid(int speed
, int duplex
, unsigned long *supported
)
174 return phy_lookup_setting(speed
, duplex
, supported
, false);
178 * phy_supported_speeds - return all speeds currently supported by a phy device
179 * @phy: The phy device to return supported speeds of.
180 * @speeds: buffer to store supported speeds in.
181 * @size: size of speeds buffer.
183 * Description: Returns the number of supported speeds, and fills the speeds
184 * buffer with the supported speeds. If speeds buffer is too small to contain
185 * all currently supported speeds, will return as many speeds as can fit.
187 unsigned int phy_supported_speeds(struct phy_device
*phy
,
188 unsigned int *speeds
,
191 return phy_speeds(speeds
, size
, phy
->supported
);
195 * phy_check_valid - check if there is a valid PHY setting which matches
196 * speed, duplex, and feature mask
197 * @speed: speed to match
198 * @duplex: duplex to match
199 * @features: A mask of the valid settings
201 * Description: Returns true if there is a valid setting, false otherwise.
203 static inline bool phy_check_valid(int speed
, int duplex
,
204 unsigned long *features
)
206 return !!phy_lookup_setting(speed
, duplex
, features
, true);
210 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
211 * @phydev: the target phy_device struct
213 * Description: Make sure the PHY is set to supported speeds and
214 * duplexes. Drop down by one in this order: 1000/FULL,
215 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
217 static void phy_sanitize_settings(struct phy_device
*phydev
)
219 const struct phy_setting
*setting
;
221 /* Sanitize settings based on PHY capabilities */
222 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
, phydev
->supported
))
223 phydev
->autoneg
= AUTONEG_DISABLE
;
225 setting
= phy_find_valid(phydev
->speed
, phydev
->duplex
,
228 phydev
->speed
= setting
->speed
;
229 phydev
->duplex
= setting
->duplex
;
231 /* We failed to find anything (no supported speeds?) */
232 phydev
->speed
= SPEED_UNKNOWN
;
233 phydev
->duplex
= DUPLEX_UNKNOWN
;
238 * phy_ethtool_sset - generic ethtool sset function, handles all the details
239 * @phydev: target phy_device struct
242 * A few notes about parameter checking:
244 * - We don't set port or transceiver, so we don't care what they
246 * - phy_start_aneg() will make sure forced settings are sane, and
247 * choose the next best ones from the ones selected, so we don't
248 * care if ethtool tries to give us bad values.
250 int phy_ethtool_sset(struct phy_device
*phydev
, struct ethtool_cmd
*cmd
)
252 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising
);
253 u32 speed
= ethtool_cmd_speed(cmd
);
255 if (cmd
->phy_address
!= phydev
->mdio
.addr
)
258 /* We make sure that we don't pass unsupported values in to the PHY */
259 ethtool_convert_legacy_u32_to_link_mode(advertising
, cmd
->advertising
);
260 linkmode_and(advertising
, advertising
, phydev
->supported
);
262 /* Verify the settings we care about. */
263 if (cmd
->autoneg
!= AUTONEG_ENABLE
&& cmd
->autoneg
!= AUTONEG_DISABLE
)
266 if (cmd
->autoneg
== AUTONEG_ENABLE
&& cmd
->advertising
== 0)
269 if (cmd
->autoneg
== AUTONEG_DISABLE
&&
270 ((speed
!= SPEED_1000
&&
271 speed
!= SPEED_100
&&
272 speed
!= SPEED_10
) ||
273 (cmd
->duplex
!= DUPLEX_HALF
&&
274 cmd
->duplex
!= DUPLEX_FULL
)))
277 phydev
->autoneg
= cmd
->autoneg
;
279 phydev
->speed
= speed
;
281 linkmode_copy(phydev
->advertising
, advertising
);
283 if (AUTONEG_ENABLE
== cmd
->autoneg
)
284 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
,
285 phydev
->advertising
);
287 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
,
288 phydev
->advertising
);
290 phydev
->duplex
= cmd
->duplex
;
292 phydev
->mdix_ctrl
= cmd
->eth_tp_mdix_ctrl
;
294 /* Restart the PHY */
295 phy_start_aneg(phydev
);
299 EXPORT_SYMBOL(phy_ethtool_sset
);
301 int phy_ethtool_ksettings_set(struct phy_device
*phydev
,
302 const struct ethtool_link_ksettings
*cmd
)
304 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising
);
305 u8 autoneg
= cmd
->base
.autoneg
;
306 u8 duplex
= cmd
->base
.duplex
;
307 u32 speed
= cmd
->base
.speed
;
309 if (cmd
->base
.phy_address
!= phydev
->mdio
.addr
)
312 linkmode_copy(advertising
, cmd
->link_modes
.advertising
);
314 /* We make sure that we don't pass unsupported values in to the PHY */
315 linkmode_and(advertising
, advertising
, phydev
->supported
);
317 /* Verify the settings we care about. */
318 if (autoneg
!= AUTONEG_ENABLE
&& autoneg
!= AUTONEG_DISABLE
)
321 if (autoneg
== AUTONEG_ENABLE
&& linkmode_empty(advertising
))
324 if (autoneg
== AUTONEG_DISABLE
&&
325 ((speed
!= SPEED_1000
&&
326 speed
!= SPEED_100
&&
327 speed
!= SPEED_10
) ||
328 (duplex
!= DUPLEX_HALF
&&
329 duplex
!= DUPLEX_FULL
)))
332 phydev
->autoneg
= autoneg
;
334 phydev
->speed
= speed
;
336 linkmode_copy(phydev
->advertising
, advertising
);
338 if (autoneg
== AUTONEG_ENABLE
)
339 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
,
340 phydev
->advertising
);
342 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT
,
343 phydev
->advertising
);
345 phydev
->duplex
= duplex
;
347 phydev
->mdix_ctrl
= cmd
->base
.eth_tp_mdix_ctrl
;
349 /* Restart the PHY */
350 phy_start_aneg(phydev
);
354 EXPORT_SYMBOL(phy_ethtool_ksettings_set
);
356 void phy_ethtool_ksettings_get(struct phy_device
*phydev
,
357 struct ethtool_link_ksettings
*cmd
)
359 linkmode_copy(cmd
->link_modes
.supported
, phydev
->supported
);
360 linkmode_copy(cmd
->link_modes
.advertising
, phydev
->advertising
);
361 linkmode_copy(cmd
->link_modes
.lp_advertising
, phydev
->lp_advertising
);
363 cmd
->base
.speed
= phydev
->speed
;
364 cmd
->base
.duplex
= phydev
->duplex
;
365 if (phydev
->interface
== PHY_INTERFACE_MODE_MOCA
)
366 cmd
->base
.port
= PORT_BNC
;
368 cmd
->base
.port
= PORT_MII
;
369 cmd
->base
.transceiver
= phy_is_internal(phydev
) ?
370 XCVR_INTERNAL
: XCVR_EXTERNAL
;
371 cmd
->base
.phy_address
= phydev
->mdio
.addr
;
372 cmd
->base
.autoneg
= phydev
->autoneg
;
373 cmd
->base
.eth_tp_mdix_ctrl
= phydev
->mdix_ctrl
;
374 cmd
->base
.eth_tp_mdix
= phydev
->mdix
;
376 EXPORT_SYMBOL(phy_ethtool_ksettings_get
);
379 * phy_mii_ioctl - generic PHY MII ioctl interface
380 * @phydev: the phy_device struct
381 * @ifr: &struct ifreq for socket ioctl's
382 * @cmd: ioctl cmd to execute
384 * Note that this function is currently incompatible with the
385 * PHYCONTROL layer. It changes registers without regard to
386 * current state. Use at own risk.
388 int phy_mii_ioctl(struct phy_device
*phydev
, struct ifreq
*ifr
, int cmd
)
390 struct mii_ioctl_data
*mii_data
= if_mii(ifr
);
391 u16 val
= mii_data
->val_in
;
392 bool change_autoneg
= false;
396 mii_data
->phy_id
= phydev
->mdio
.addr
;
400 mii_data
->val_out
= mdiobus_read(phydev
->mdio
.bus
,
406 if (mii_data
->phy_id
== phydev
->mdio
.addr
) {
407 switch (mii_data
->reg_num
) {
409 if ((val
& (BMCR_RESET
| BMCR_ANENABLE
)) == 0) {
410 if (phydev
->autoneg
== AUTONEG_ENABLE
)
411 change_autoneg
= true;
412 phydev
->autoneg
= AUTONEG_DISABLE
;
413 if (val
& BMCR_FULLDPLX
)
414 phydev
->duplex
= DUPLEX_FULL
;
416 phydev
->duplex
= DUPLEX_HALF
;
417 if (val
& BMCR_SPEED1000
)
418 phydev
->speed
= SPEED_1000
;
419 else if (val
& BMCR_SPEED100
)
420 phydev
->speed
= SPEED_100
;
421 else phydev
->speed
= SPEED_10
;
424 if (phydev
->autoneg
== AUTONEG_DISABLE
)
425 change_autoneg
= true;
426 phydev
->autoneg
= AUTONEG_ENABLE
;
430 mii_adv_mod_linkmode_adv_t(phydev
->advertising
,
432 change_autoneg
= true;
440 mdiobus_write(phydev
->mdio
.bus
, mii_data
->phy_id
,
441 mii_data
->reg_num
, val
);
443 if (mii_data
->phy_id
== phydev
->mdio
.addr
&&
444 mii_data
->reg_num
== MII_BMCR
&&
446 return phy_init_hw(phydev
);
449 return phy_start_aneg(phydev
);
454 if (phydev
->drv
&& phydev
->drv
->hwtstamp
)
455 return phydev
->drv
->hwtstamp(phydev
, ifr
);
462 EXPORT_SYMBOL(phy_mii_ioctl
);
464 static void phy_queue_state_machine(struct phy_device
*phydev
,
467 mod_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
,
471 static void phy_trigger_machine(struct phy_device
*phydev
)
473 phy_queue_state_machine(phydev
, 0);
476 static int phy_config_aneg(struct phy_device
*phydev
)
478 if (phydev
->drv
->config_aneg
)
479 return phydev
->drv
->config_aneg(phydev
);
481 /* Clause 45 PHYs that don't implement Clause 22 registers are not
482 * allowed to call genphy_config_aneg()
484 if (phydev
->is_c45
&& !(phydev
->c45_ids
.devices_in_package
& BIT(0)))
487 return genphy_config_aneg(phydev
);
491 * phy_check_link_status - check link status and set state accordingly
492 * @phydev: the phy_device struct
494 * Description: Check for link and whether autoneg was triggered / is running
495 * and set state accordingly
497 static int phy_check_link_status(struct phy_device
*phydev
)
501 WARN_ON(!mutex_is_locked(&phydev
->lock
));
503 err
= phy_read_status(phydev
);
507 if (phydev
->link
&& phydev
->state
!= PHY_RUNNING
) {
508 phydev
->state
= PHY_RUNNING
;
510 } else if (!phydev
->link
&& phydev
->state
!= PHY_NOLINK
) {
511 phydev
->state
= PHY_NOLINK
;
512 phy_link_down(phydev
, true);
519 * phy_start_aneg - start auto-negotiation for this PHY device
520 * @phydev: the phy_device struct
522 * Description: Sanitizes the settings (if we're not autonegotiating
523 * them), and then calls the driver's config_aneg function.
524 * If the PHYCONTROL Layer is operating, we change the state to
525 * reflect the beginning of Auto-negotiation or forcing.
527 int phy_start_aneg(struct phy_device
*phydev
)
534 mutex_lock(&phydev
->lock
);
536 if (AUTONEG_DISABLE
== phydev
->autoneg
)
537 phy_sanitize_settings(phydev
);
539 /* Invalidate LP advertising flags */
540 linkmode_zero(phydev
->lp_advertising
);
542 err
= phy_config_aneg(phydev
);
546 if (__phy_is_started(phydev
)) {
547 if (phydev
->autoneg
== AUTONEG_ENABLE
) {
548 err
= phy_check_link_status(phydev
);
550 phydev
->state
= PHY_FORCING
;
551 phydev
->link_timeout
= PHY_FORCE_TIMEOUT
;
556 mutex_unlock(&phydev
->lock
);
560 EXPORT_SYMBOL(phy_start_aneg
);
562 static int phy_poll_aneg_done(struct phy_device
*phydev
)
564 unsigned int retries
= 100;
569 ret
= phy_aneg_done(phydev
);
570 } while (!ret
&& --retries
);
575 return ret
< 0 ? ret
: 0;
579 * phy_speed_down - set speed to lowest speed supported by both link partners
580 * @phydev: the phy_device struct
581 * @sync: perform action synchronously
583 * Description: Typically used to save energy when waiting for a WoL packet
585 * WARNING: Setting sync to false may cause the system being unable to suspend
586 * in case the PHY generates an interrupt when finishing the autonegotiation.
587 * This interrupt may wake up the system immediately after suspend.
588 * Therefore use sync = false only if you're sure it's safe with the respective
591 int phy_speed_down(struct phy_device
*phydev
, bool sync
)
593 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old
);
594 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv
);
597 if (phydev
->autoneg
!= AUTONEG_ENABLE
)
600 linkmode_copy(adv_old
, phydev
->advertising
);
601 linkmode_copy(adv
, phydev
->lp_advertising
);
602 linkmode_and(adv
, adv
, phydev
->supported
);
604 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT
, adv
) ||
605 linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT
, adv
)) {
606 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT
,
607 phydev
->advertising
);
608 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT
,
609 phydev
->advertising
);
610 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT
,
611 phydev
->advertising
);
612 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT
,
613 phydev
->advertising
);
614 } else if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT
,
616 linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT
,
618 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT
,
619 phydev
->advertising
);
620 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT
,
621 phydev
->advertising
);
624 if (linkmode_equal(phydev
->advertising
, adv_old
))
627 ret
= phy_config_aneg(phydev
);
631 return sync
? phy_poll_aneg_done(phydev
) : 0;
633 EXPORT_SYMBOL_GPL(phy_speed_down
);
636 * phy_speed_up - (re)set advertised speeds to all supported speeds
637 * @phydev: the phy_device struct
639 * Description: Used to revert the effect of phy_speed_down
641 int phy_speed_up(struct phy_device
*phydev
)
643 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_speeds
) = { 0, };
644 __ETHTOOL_DECLARE_LINK_MODE_MASK(not_speeds
);
645 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported
);
646 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old
);
647 __ETHTOOL_DECLARE_LINK_MODE_MASK(speeds
);
649 linkmode_copy(adv_old
, phydev
->advertising
);
651 if (phydev
->autoneg
!= AUTONEG_ENABLE
)
654 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT
, all_speeds
);
655 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT
, all_speeds
);
656 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT
, all_speeds
);
657 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT
, all_speeds
);
658 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT
, all_speeds
);
659 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT
, all_speeds
);
661 linkmode_andnot(not_speeds
, adv_old
, all_speeds
);
662 linkmode_copy(supported
, phydev
->supported
);
663 linkmode_and(speeds
, supported
, all_speeds
);
664 linkmode_or(phydev
->advertising
, not_speeds
, speeds
);
666 if (linkmode_equal(phydev
->advertising
, adv_old
))
669 return phy_config_aneg(phydev
);
671 EXPORT_SYMBOL_GPL(phy_speed_up
);
674 * phy_start_machine - start PHY state machine tracking
675 * @phydev: the phy_device struct
677 * Description: The PHY infrastructure can run a state machine
678 * which tracks whether the PHY is starting up, negotiating,
679 * etc. This function starts the delayed workqueue which tracks
680 * the state of the PHY. If you want to maintain your own state machine,
681 * do not call this function.
683 void phy_start_machine(struct phy_device
*phydev
)
685 phy_trigger_machine(phydev
);
687 EXPORT_SYMBOL_GPL(phy_start_machine
);
690 * phy_stop_machine - stop the PHY state machine tracking
691 * @phydev: target phy_device struct
693 * Description: Stops the state machine delayed workqueue, sets the
694 * state to UP (unless it wasn't up yet). This function must be
695 * called BEFORE phy_detach.
697 void phy_stop_machine(struct phy_device
*phydev
)
699 cancel_delayed_work_sync(&phydev
->state_queue
);
701 mutex_lock(&phydev
->lock
);
702 if (__phy_is_started(phydev
))
703 phydev
->state
= PHY_UP
;
704 mutex_unlock(&phydev
->lock
);
708 * phy_error - enter HALTED state for this PHY device
709 * @phydev: target phy_device struct
711 * Moves the PHY to the HALTED state in response to a read
712 * or write error, and tells the controller the link is down.
713 * Must not be called from interrupt context, or while the
714 * phydev->lock is held.
716 static void phy_error(struct phy_device
*phydev
)
720 mutex_lock(&phydev
->lock
);
721 phydev
->state
= PHY_HALTED
;
722 mutex_unlock(&phydev
->lock
);
724 phy_trigger_machine(phydev
);
728 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
729 * @phydev: target phy_device struct
731 static int phy_disable_interrupts(struct phy_device
*phydev
)
735 /* Disable PHY interrupts */
736 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
740 /* Clear the interrupt */
741 return phy_clear_interrupt(phydev
);
745 * phy_interrupt - PHY interrupt handler
746 * @irq: interrupt line
747 * @phy_dat: phy_device pointer
749 * Description: Handle PHY interrupt
751 static irqreturn_t
phy_interrupt(int irq
, void *phy_dat
)
753 struct phy_device
*phydev
= phy_dat
;
755 if (!phy_is_started(phydev
))
756 return IRQ_NONE
; /* It can't be ours. */
758 if (phydev
->drv
->did_interrupt
&& !phydev
->drv
->did_interrupt(phydev
))
761 /* reschedule state queue work to run as soon as possible */
762 phy_trigger_machine(phydev
);
764 if (phy_clear_interrupt(phydev
))
774 * phy_enable_interrupts - Enable the interrupts from the PHY side
775 * @phydev: target phy_device struct
777 static int phy_enable_interrupts(struct phy_device
*phydev
)
779 int err
= phy_clear_interrupt(phydev
);
784 return phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
788 * phy_start_interrupts - request and enable interrupts for a PHY device
789 * @phydev: target phy_device struct
791 * Description: Request the interrupt for the given PHY.
792 * If this fails, then we set irq to PHY_POLL.
793 * Otherwise, we enable the interrupts in the PHY.
794 * This should only be called with a valid IRQ number.
795 * Returns 0 on success or < 0 on error.
797 int phy_start_interrupts(struct phy_device
*phydev
)
799 if (request_threaded_irq(phydev
->irq
, NULL
, phy_interrupt
,
800 IRQF_ONESHOT
| IRQF_SHARED
,
801 phydev_name(phydev
), phydev
) < 0) {
802 phydev_warn(phydev
, "Can't get IRQ %d\n", phydev
->irq
);
803 phydev
->irq
= PHY_POLL
;
807 return phy_enable_interrupts(phydev
);
809 EXPORT_SYMBOL(phy_start_interrupts
);
812 * phy_stop - Bring down the PHY link, and stop checking the status
813 * @phydev: target phy_device struct
815 void phy_stop(struct phy_device
*phydev
)
817 mutex_lock(&phydev
->lock
);
819 if (!__phy_is_started(phydev
)) {
820 WARN(1, "called from state %s\n",
821 phy_state_to_str(phydev
->state
));
822 mutex_unlock(&phydev
->lock
);
826 if (phy_interrupt_is_valid(phydev
))
827 phy_disable_interrupts(phydev
);
829 phydev
->state
= PHY_HALTED
;
831 mutex_unlock(&phydev
->lock
);
833 phy_state_machine(&phydev
->state_queue
.work
);
834 phy_stop_machine(phydev
);
836 /* Cannot call flush_scheduled_work() here as desired because
837 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
838 * will not reenable interrupts.
841 EXPORT_SYMBOL(phy_stop
);
844 * phy_start - start or restart a PHY device
845 * @phydev: target phy_device struct
847 * Description: Indicates the attached device's readiness to
848 * handle PHY-related work. Used during startup to start the
849 * PHY, and after a call to phy_stop() to resume operation.
850 * Also used to indicate the MDIO bus has cleared an error
853 void phy_start(struct phy_device
*phydev
)
857 mutex_lock(&phydev
->lock
);
859 switch (phydev
->state
) {
861 phydev
->state
= PHY_UP
;
864 /* if phy was suspended, bring the physical link up again */
865 __phy_resume(phydev
);
867 /* make sure interrupts are re-enabled for the PHY */
868 if (phy_interrupt_is_valid(phydev
)) {
869 err
= phy_enable_interrupts(phydev
);
874 phydev
->state
= PHY_RESUMING
;
879 mutex_unlock(&phydev
->lock
);
881 phy_start_machine(phydev
);
883 EXPORT_SYMBOL(phy_start
);
886 * phy_state_machine - Handle the state machine
887 * @work: work_struct that describes the work to be done
889 void phy_state_machine(struct work_struct
*work
)
891 struct delayed_work
*dwork
= to_delayed_work(work
);
892 struct phy_device
*phydev
=
893 container_of(dwork
, struct phy_device
, state_queue
);
894 bool needs_aneg
= false, do_suspend
= false;
895 enum phy_state old_state
;
898 mutex_lock(&phydev
->lock
);
900 old_state
= phydev
->state
;
902 if (phydev
->drv
&& phydev
->drv
->link_change_notify
)
903 phydev
->drv
->link_change_notify(phydev
);
905 switch (phydev
->state
) {
916 err
= phy_check_link_status(phydev
);
919 err
= genphy_update_link(phydev
);
924 phydev
->state
= PHY_RUNNING
;
927 if (0 == phydev
->link_timeout
--)
929 phy_link_down(phydev
, false);
935 phy_link_down(phydev
, true);
941 mutex_unlock(&phydev
->lock
);
944 err
= phy_start_aneg(phydev
);
951 if (old_state
!= phydev
->state
)
952 phydev_dbg(phydev
, "PHY state change %s -> %s\n",
953 phy_state_to_str(old_state
),
954 phy_state_to_str(phydev
->state
));
956 /* Only re-schedule a PHY state machine change if we are polling the
957 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
958 * between states from phy_mac_interrupt().
960 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
961 * state machine would be pointless and possibly error prone when
962 * called from phy_disconnect() synchronously.
964 if (phy_polling_mode(phydev
) && phy_is_started(phydev
))
965 phy_queue_state_machine(phydev
, PHY_STATE_TIME
);
969 * phy_mac_interrupt - MAC says the link has changed
970 * @phydev: phy_device struct with changed link
972 * The MAC layer is able to indicate there has been a change in the PHY link
973 * status. Trigger the state machine and work a work queue.
975 void phy_mac_interrupt(struct phy_device
*phydev
)
977 /* Trigger a state machine change */
978 phy_trigger_machine(phydev
);
980 EXPORT_SYMBOL(phy_mac_interrupt
);
982 static void mmd_eee_adv_to_linkmode(unsigned long *advertising
, u16 eee_adv
)
984 linkmode_zero(advertising
);
986 if (eee_adv
& MDIO_EEE_100TX
)
987 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT
,
989 if (eee_adv
& MDIO_EEE_1000T
)
990 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT
,
992 if (eee_adv
& MDIO_EEE_10GT
)
993 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT
,
995 if (eee_adv
& MDIO_EEE_1000KX
)
996 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT
,
998 if (eee_adv
& MDIO_EEE_10GKX4
)
999 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT
,
1001 if (eee_adv
& MDIO_EEE_10GKR
)
1002 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT
,
1007 * phy_init_eee - init and check the EEE feature
1008 * @phydev: target phy_device struct
1009 * @clk_stop_enable: PHY may stop the clock during LPI
1011 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1012 * is supported by looking at the MMD registers 3.20 and 7.60/61
1013 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1016 int phy_init_eee(struct phy_device
*phydev
, bool clk_stop_enable
)
1021 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1023 if (phydev
->duplex
== DUPLEX_FULL
) {
1024 __ETHTOOL_DECLARE_LINK_MODE_MASK(common
);
1025 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp
);
1026 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv
);
1027 int eee_lp
, eee_cap
, eee_adv
;
1031 /* Read phy status to properly get the right settings */
1032 status
= phy_read_status(phydev
);
1036 /* First check if the EEE ability is supported */
1037 eee_cap
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_PCS_EEE_ABLE
);
1041 cap
= mmd_eee_cap_to_ethtool_sup_t(eee_cap
);
1045 /* Check which link settings negotiated and verify it in
1046 * the EEE advertising registers.
1048 eee_lp
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_EEE_LPABLE
);
1052 eee_adv
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_EEE_ADV
);
1056 mmd_eee_adv_to_linkmode(adv
, eee_adv
);
1057 mmd_eee_adv_to_linkmode(lp
, eee_lp
);
1058 linkmode_and(common
, adv
, lp
);
1060 if (!phy_check_valid(phydev
->speed
, phydev
->duplex
, common
))
1063 if (clk_stop_enable
) {
1064 /* Configure the PHY to stop receiving xMII
1065 * clock while it is signaling LPI.
1067 int val
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
);
1071 val
|= MDIO_PCS_CTRL1_CLKSTOP_EN
;
1072 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
, val
);
1075 return 0; /* EEE supported */
1078 return -EPROTONOSUPPORT
;
1080 EXPORT_SYMBOL(phy_init_eee
);
1083 * phy_get_eee_err - report the EEE wake error count
1084 * @phydev: target phy_device struct
1086 * Description: it is to report the number of time where the PHY
1087 * failed to complete its normal wake sequence.
1089 int phy_get_eee_err(struct phy_device
*phydev
)
1094 return phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_PCS_EEE_WK_ERR
);
1096 EXPORT_SYMBOL(phy_get_eee_err
);
1099 * phy_ethtool_get_eee - get EEE supported and status
1100 * @phydev: target phy_device struct
1101 * @data: ethtool_eee data
1103 * Description: it reportes the Supported/Advertisement/LP Advertisement
1106 int phy_ethtool_get_eee(struct phy_device
*phydev
, struct ethtool_eee
*data
)
1113 /* Get Supported EEE */
1114 val
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_PCS_EEE_ABLE
);
1117 data
->supported
= mmd_eee_cap_to_ethtool_sup_t(val
);
1119 /* Get advertisement EEE */
1120 val
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_EEE_ADV
);
1123 data
->advertised
= mmd_eee_adv_to_ethtool_adv_t(val
);
1124 data
->eee_enabled
= !!data
->advertised
;
1126 /* Get LP advertisement EEE */
1127 val
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_EEE_LPABLE
);
1130 data
->lp_advertised
= mmd_eee_adv_to_ethtool_adv_t(val
);
1132 data
->eee_active
= !!(data
->advertised
& data
->lp_advertised
);
1136 EXPORT_SYMBOL(phy_ethtool_get_eee
);
1139 * phy_ethtool_set_eee - set EEE supported and status
1140 * @phydev: target phy_device struct
1141 * @data: ethtool_eee data
1143 * Description: it is to program the Advertisement EEE register.
1145 int phy_ethtool_set_eee(struct phy_device
*phydev
, struct ethtool_eee
*data
)
1147 int cap
, old_adv
, adv
= 0, ret
;
1152 /* Get Supported EEE */
1153 cap
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_PCS_EEE_ABLE
);
1157 old_adv
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_EEE_ADV
);
1161 if (data
->eee_enabled
) {
1162 adv
= !data
->advertised
? cap
:
1163 ethtool_adv_to_mmd_eee_adv_t(data
->advertised
) & cap
;
1164 /* Mask prohibited EEE modes */
1165 adv
&= ~phydev
->eee_broken_modes
;
1168 if (old_adv
!= adv
) {
1169 ret
= phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_EEE_ADV
, adv
);
1173 /* Restart autonegotiation so the new modes get sent to the
1176 ret
= phy_restart_aneg(phydev
);
1183 EXPORT_SYMBOL(phy_ethtool_set_eee
);
1185 int phy_ethtool_set_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
1187 if (phydev
->drv
&& phydev
->drv
->set_wol
)
1188 return phydev
->drv
->set_wol(phydev
, wol
);
1192 EXPORT_SYMBOL(phy_ethtool_set_wol
);
1194 void phy_ethtool_get_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
1196 if (phydev
->drv
&& phydev
->drv
->get_wol
)
1197 phydev
->drv
->get_wol(phydev
, wol
);
1199 EXPORT_SYMBOL(phy_ethtool_get_wol
);
1201 int phy_ethtool_get_link_ksettings(struct net_device
*ndev
,
1202 struct ethtool_link_ksettings
*cmd
)
1204 struct phy_device
*phydev
= ndev
->phydev
;
1209 phy_ethtool_ksettings_get(phydev
, cmd
);
1213 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings
);
1215 int phy_ethtool_set_link_ksettings(struct net_device
*ndev
,
1216 const struct ethtool_link_ksettings
*cmd
)
1218 struct phy_device
*phydev
= ndev
->phydev
;
1223 return phy_ethtool_ksettings_set(phydev
, cmd
);
1225 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings
);
1227 int phy_ethtool_nway_reset(struct net_device
*ndev
)
1229 struct phy_device
*phydev
= ndev
->phydev
;
1237 return phy_restart_aneg(phydev
);
1239 EXPORT_SYMBOL(phy_ethtool_nway_reset
);