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/netlink.h>
19 #include <linux/etherdevice.h>
20 #include <linux/skbuff.h>
22 #include <linux/module.h>
23 #include <linux/mii.h>
24 #include <linux/ethtool.h>
25 #include <linux/ethtool_netlink.h>
26 #include <linux/phy.h>
27 #include <linux/phy_led_triggers.h>
28 #include <linux/sfp.h>
29 #include <linux/workqueue.h>
30 #include <linux/mdio.h>
32 #include <linux/uaccess.h>
33 #include <linux/atomic.h>
34 #include <linux/suspend.h>
35 #include <net/netlink.h>
36 #include <net/genetlink.h>
39 #define PHY_STATE_TIME HZ
41 #define PHY_STATE_STR(_state) \
43 return __stringify(_state); \
45 static const char *phy_state_to_str(enum phy_state st)
51 PHY_STATE_STR(RUNNING)
53 PHY_STATE_STR(CABLETEST)
60 static void phy_link_up(struct phy_device *phydev)
62 phydev->phy_link_change(phydev, true);
63 phy_led_trigger_change_speed(phydev);
66 static void phy_link_down(struct phy_device *phydev)
68 phydev->phy_link_change(phydev, false);
69 phy_led_trigger_change_speed(phydev);
72 static const char *phy_pause_str(struct phy_device *phydev)
74 bool local_pause, local_asym_pause;
76 if (phydev->autoneg == AUTONEG_DISABLE)
79 local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
81 local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
84 if (local_pause && phydev->pause)
87 if (local_asym_pause && phydev->asym_pause) {
99 * phy_print_status - Convenience function to print out the current phy status
100 * @phydev: the phy_device struct
102 void phy_print_status(struct phy_device *phydev)
105 netdev_info(phydev->attached_dev,
106 "Link is Up - %s/%s %s- flow control %s\n",
107 phy_speed_to_str(phydev->speed),
108 phy_duplex_to_str(phydev->duplex),
109 phydev->downshifted_rate ? "(downshifted) " : "",
110 phy_pause_str(phydev));
112 netdev_info(phydev->attached_dev, "Link is Down\n");
115 EXPORT_SYMBOL(phy_print_status);
118 * phy_config_interrupt - configure the PHY device for the requested interrupts
119 * @phydev: the phy_device struct
120 * @interrupts: interrupt flags to configure for this @phydev
122 * Returns 0 on success or < 0 on error.
124 static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
126 phydev->interrupts = interrupts ? 1 : 0;
127 if (phydev->drv->config_intr)
128 return phydev->drv->config_intr(phydev);
134 * phy_restart_aneg - restart auto-negotiation
135 * @phydev: target phy_device struct
137 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
138 * negative errno on error.
140 int phy_restart_aneg(struct phy_device *phydev)
144 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
145 ret = genphy_c45_restart_aneg(phydev);
147 ret = genphy_restart_aneg(phydev);
151 EXPORT_SYMBOL_GPL(phy_restart_aneg);
154 * phy_aneg_done - return auto-negotiation status
155 * @phydev: target phy_device struct
157 * Description: Return the auto-negotiation status from this @phydev
158 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
161 int phy_aneg_done(struct phy_device *phydev)
163 if (phydev->drv && phydev->drv->aneg_done)
164 return phydev->drv->aneg_done(phydev);
165 else if (phydev->is_c45)
166 return genphy_c45_aneg_done(phydev);
168 return genphy_aneg_done(phydev);
170 EXPORT_SYMBOL(phy_aneg_done);
173 * phy_find_valid - find a PHY setting that matches the requested parameters
174 * @speed: desired speed
175 * @duplex: desired duplex
176 * @supported: mask of supported link modes
178 * Locate a supported phy setting that is, in priority order:
179 * - an exact match for the specified speed and duplex mode
180 * - a match for the specified speed, or slower speed
181 * - the slowest supported speed
182 * Returns the matched phy_setting entry, or %NULL if no supported phy
183 * settings were found.
185 static const struct phy_setting *
186 phy_find_valid(int speed, int duplex, unsigned long *supported)
188 return phy_lookup_setting(speed, duplex, supported, false);
192 * phy_supported_speeds - return all speeds currently supported by a phy device
193 * @phy: The phy device to return supported speeds of.
194 * @speeds: buffer to store supported speeds in.
195 * @size: size of speeds buffer.
197 * Description: Returns the number of supported speeds, and fills the speeds
198 * buffer with the supported speeds. If speeds buffer is too small to contain
199 * all currently supported speeds, will return as many speeds as can fit.
201 unsigned int phy_supported_speeds(struct phy_device *phy,
202 unsigned int *speeds,
205 return phy_speeds(speeds, size, phy->supported);
209 * phy_check_valid - check if there is a valid PHY setting which matches
210 * speed, duplex, and feature mask
211 * @speed: speed to match
212 * @duplex: duplex to match
213 * @features: A mask of the valid settings
215 * Description: Returns true if there is a valid setting, false otherwise.
217 static inline bool phy_check_valid(int speed, int duplex,
218 unsigned long *features)
220 return !!phy_lookup_setting(speed, duplex, features, true);
224 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
225 * @phydev: the target phy_device struct
227 * Description: Make sure the PHY is set to supported speeds and
228 * duplexes. Drop down by one in this order: 1000/FULL,
229 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
231 static void phy_sanitize_settings(struct phy_device *phydev)
233 const struct phy_setting *setting;
235 setting = phy_find_valid(phydev->speed, phydev->duplex,
238 phydev->speed = setting->speed;
239 phydev->duplex = setting->duplex;
241 /* We failed to find anything (no supported speeds?) */
242 phydev->speed = SPEED_UNKNOWN;
243 phydev->duplex = DUPLEX_UNKNOWN;
247 void phy_ethtool_ksettings_get(struct phy_device *phydev,
248 struct ethtool_link_ksettings *cmd)
250 mutex_lock(&phydev->lock);
251 linkmode_copy(cmd->link_modes.supported, phydev->supported);
252 linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
253 linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
255 cmd->base.speed = phydev->speed;
256 cmd->base.duplex = phydev->duplex;
257 cmd->base.master_slave_cfg = phydev->master_slave_get;
258 cmd->base.master_slave_state = phydev->master_slave_state;
259 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
260 cmd->base.port = PORT_BNC;
262 cmd->base.port = phydev->port;
263 cmd->base.transceiver = phy_is_internal(phydev) ?
264 XCVR_INTERNAL : XCVR_EXTERNAL;
265 cmd->base.phy_address = phydev->mdio.addr;
266 cmd->base.autoneg = phydev->autoneg;
267 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
268 cmd->base.eth_tp_mdix = phydev->mdix;
269 mutex_unlock(&phydev->lock);
271 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
274 * phy_mii_ioctl - generic PHY MII ioctl interface
275 * @phydev: the phy_device struct
276 * @ifr: &struct ifreq for socket ioctl's
277 * @cmd: ioctl cmd to execute
279 * Note that this function is currently incompatible with the
280 * PHYCONTROL layer. It changes registers without regard to
281 * current state. Use at own risk.
283 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
285 struct mii_ioctl_data *mii_data = if_mii(ifr);
286 u16 val = mii_data->val_in;
287 bool change_autoneg = false;
292 mii_data->phy_id = phydev->mdio.addr;
296 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
297 prtad = mdio_phy_id_prtad(mii_data->phy_id);
298 devad = mdio_phy_id_devad(mii_data->phy_id);
299 devad = mdiobus_c45_addr(devad, mii_data->reg_num);
301 prtad = mii_data->phy_id;
302 devad = mii_data->reg_num;
304 mii_data->val_out = mdiobus_read(phydev->mdio.bus, prtad,
309 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
310 prtad = mdio_phy_id_prtad(mii_data->phy_id);
311 devad = mdio_phy_id_devad(mii_data->phy_id);
312 devad = mdiobus_c45_addr(devad, mii_data->reg_num);
314 prtad = mii_data->phy_id;
315 devad = mii_data->reg_num;
317 if (prtad == phydev->mdio.addr) {
320 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
321 if (phydev->autoneg == AUTONEG_ENABLE)
322 change_autoneg = true;
323 phydev->autoneg = AUTONEG_DISABLE;
324 if (val & BMCR_FULLDPLX)
325 phydev->duplex = DUPLEX_FULL;
327 phydev->duplex = DUPLEX_HALF;
328 if (val & BMCR_SPEED1000)
329 phydev->speed = SPEED_1000;
330 else if (val & BMCR_SPEED100)
331 phydev->speed = SPEED_100;
332 else phydev->speed = SPEED_10;
334 if (phydev->autoneg == AUTONEG_DISABLE)
335 change_autoneg = true;
336 phydev->autoneg = AUTONEG_ENABLE;
340 mii_adv_mod_linkmode_adv_t(phydev->advertising,
342 change_autoneg = true;
345 mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
347 change_autoneg = true;
355 mdiobus_write(phydev->mdio.bus, prtad, devad, val);
357 if (prtad == phydev->mdio.addr &&
360 return phy_init_hw(phydev);
363 return phy_start_aneg(phydev);
368 if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
369 return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
376 EXPORT_SYMBOL(phy_mii_ioctl);
379 * phy_do_ioctl - generic ndo_eth_ioctl implementation
380 * @dev: the net_device struct
381 * @ifr: &struct ifreq for socket ioctl's
382 * @cmd: ioctl cmd to execute
384 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
389 return phy_mii_ioctl(dev->phydev, ifr, cmd);
391 EXPORT_SYMBOL(phy_do_ioctl);
394 * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first
396 * @dev: the net_device struct
397 * @ifr: &struct ifreq for socket ioctl's
398 * @cmd: ioctl cmd to execute
400 * Same as phy_do_ioctl, but ensures that net_device is running before
401 * handling the ioctl.
403 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
405 if (!netif_running(dev))
408 return phy_do_ioctl(dev, ifr, cmd);
410 EXPORT_SYMBOL(phy_do_ioctl_running);
413 * phy_queue_state_machine - Trigger the state machine to run soon
415 * @phydev: the phy_device struct
416 * @jiffies: Run the state machine after these jiffies
418 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
420 mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
423 EXPORT_SYMBOL(phy_queue_state_machine);
426 * phy_trigger_machine - Trigger the state machine to run now
428 * @phydev: the phy_device struct
430 void phy_trigger_machine(struct phy_device *phydev)
432 phy_queue_state_machine(phydev, 0);
434 EXPORT_SYMBOL(phy_trigger_machine);
436 static void phy_abort_cable_test(struct phy_device *phydev)
440 ethnl_cable_test_finished(phydev);
442 err = phy_init_hw(phydev);
444 phydev_err(phydev, "Error while aborting cable test");
448 * phy_ethtool_get_strings - Get the statistic counter names
450 * @phydev: the phy_device struct
451 * @data: Where to put the strings
453 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
458 mutex_lock(&phydev->lock);
459 phydev->drv->get_strings(phydev, data);
460 mutex_unlock(&phydev->lock);
464 EXPORT_SYMBOL(phy_ethtool_get_strings);
467 * phy_ethtool_get_sset_count - Get the number of statistic counters
469 * @phydev: the phy_device struct
471 int phy_ethtool_get_sset_count(struct phy_device *phydev)
478 if (phydev->drv->get_sset_count &&
479 phydev->drv->get_strings &&
480 phydev->drv->get_stats) {
481 mutex_lock(&phydev->lock);
482 ret = phydev->drv->get_sset_count(phydev);
483 mutex_unlock(&phydev->lock);
490 EXPORT_SYMBOL(phy_ethtool_get_sset_count);
493 * phy_ethtool_get_stats - Get the statistic counters
495 * @phydev: the phy_device struct
496 * @stats: What counters to get
497 * @data: Where to store the counters
499 int phy_ethtool_get_stats(struct phy_device *phydev,
500 struct ethtool_stats *stats, u64 *data)
505 mutex_lock(&phydev->lock);
506 phydev->drv->get_stats(phydev, stats, data);
507 mutex_unlock(&phydev->lock);
511 EXPORT_SYMBOL(phy_ethtool_get_stats);
514 * phy_start_cable_test - Start a cable test
516 * @phydev: the phy_device struct
517 * @extack: extack for reporting useful error messages
519 int phy_start_cable_test(struct phy_device *phydev,
520 struct netlink_ext_ack *extack)
522 struct net_device *dev = phydev->attached_dev;
526 phydev->drv->cable_test_start &&
527 phydev->drv->cable_test_get_status)) {
528 NL_SET_ERR_MSG(extack,
529 "PHY driver does not support cable testing");
533 mutex_lock(&phydev->lock);
534 if (phydev->state == PHY_CABLETEST) {
535 NL_SET_ERR_MSG(extack,
536 "PHY already performing a test");
541 if (phydev->state < PHY_UP ||
542 phydev->state > PHY_CABLETEST) {
543 NL_SET_ERR_MSG(extack,
544 "PHY not configured. Try setting interface up");
549 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
553 /* Mark the carrier down until the test is complete */
554 phy_link_down(phydev);
556 netif_testing_on(dev);
557 err = phydev->drv->cable_test_start(phydev);
559 netif_testing_off(dev);
564 phydev->state = PHY_CABLETEST;
566 if (phy_polling_mode(phydev))
567 phy_trigger_machine(phydev);
569 mutex_unlock(&phydev->lock);
574 ethnl_cable_test_free(phydev);
576 mutex_unlock(&phydev->lock);
580 EXPORT_SYMBOL(phy_start_cable_test);
583 * phy_start_cable_test_tdr - Start a raw TDR cable test
585 * @phydev: the phy_device struct
586 * @extack: extack for reporting useful error messages
587 * @config: Configuration of the test to run
589 int phy_start_cable_test_tdr(struct phy_device *phydev,
590 struct netlink_ext_ack *extack,
591 const struct phy_tdr_config *config)
593 struct net_device *dev = phydev->attached_dev;
597 phydev->drv->cable_test_tdr_start &&
598 phydev->drv->cable_test_get_status)) {
599 NL_SET_ERR_MSG(extack,
600 "PHY driver does not support cable test TDR");
604 mutex_lock(&phydev->lock);
605 if (phydev->state == PHY_CABLETEST) {
606 NL_SET_ERR_MSG(extack,
607 "PHY already performing a test");
612 if (phydev->state < PHY_UP ||
613 phydev->state > PHY_CABLETEST) {
614 NL_SET_ERR_MSG(extack,
615 "PHY not configured. Try setting interface up");
620 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
624 /* Mark the carrier down until the test is complete */
625 phy_link_down(phydev);
627 netif_testing_on(dev);
628 err = phydev->drv->cable_test_tdr_start(phydev, config);
630 netif_testing_off(dev);
635 phydev->state = PHY_CABLETEST;
637 if (phy_polling_mode(phydev))
638 phy_trigger_machine(phydev);
640 mutex_unlock(&phydev->lock);
645 ethnl_cable_test_free(phydev);
647 mutex_unlock(&phydev->lock);
651 EXPORT_SYMBOL(phy_start_cable_test_tdr);
653 int phy_config_aneg(struct phy_device *phydev)
655 if (phydev->drv->config_aneg)
656 return phydev->drv->config_aneg(phydev);
658 /* Clause 45 PHYs that don't implement Clause 22 registers are not
659 * allowed to call genphy_config_aneg()
661 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
662 return genphy_c45_config_aneg(phydev);
664 return genphy_config_aneg(phydev);
666 EXPORT_SYMBOL(phy_config_aneg);
669 * phy_check_link_status - check link status and set state accordingly
670 * @phydev: the phy_device struct
672 * Description: Check for link and whether autoneg was triggered / is running
673 * and set state accordingly
675 static int phy_check_link_status(struct phy_device *phydev)
679 lockdep_assert_held(&phydev->lock);
681 /* Keep previous state if loopback is enabled because some PHYs
682 * report that Link is Down when loopback is enabled.
684 if (phydev->loopback_enabled)
687 err = phy_read_status(phydev);
691 if (phydev->link && phydev->state != PHY_RUNNING) {
692 phy_check_downshift(phydev);
693 phydev->state = PHY_RUNNING;
695 } else if (!phydev->link && phydev->state != PHY_NOLINK) {
696 phydev->state = PHY_NOLINK;
697 phy_link_down(phydev);
704 * _phy_start_aneg - start auto-negotiation for this PHY device
705 * @phydev: the phy_device struct
707 * Description: Sanitizes the settings (if we're not autonegotiating
708 * them), and then calls the driver's config_aneg function.
709 * If the PHYCONTROL Layer is operating, we change the state to
710 * reflect the beginning of Auto-negotiation or forcing.
712 static int _phy_start_aneg(struct phy_device *phydev)
716 lockdep_assert_held(&phydev->lock);
721 if (AUTONEG_DISABLE == phydev->autoneg)
722 phy_sanitize_settings(phydev);
724 err = phy_config_aneg(phydev);
728 if (phy_is_started(phydev))
729 err = phy_check_link_status(phydev);
735 * phy_start_aneg - start auto-negotiation for this PHY device
736 * @phydev: the phy_device struct
738 * Description: Sanitizes the settings (if we're not autonegotiating
739 * them), and then calls the driver's config_aneg function.
740 * If the PHYCONTROL Layer is operating, we change the state to
741 * reflect the beginning of Auto-negotiation or forcing.
743 int phy_start_aneg(struct phy_device *phydev)
747 mutex_lock(&phydev->lock);
748 err = _phy_start_aneg(phydev);
749 mutex_unlock(&phydev->lock);
753 EXPORT_SYMBOL(phy_start_aneg);
755 static int phy_poll_aneg_done(struct phy_device *phydev)
757 unsigned int retries = 100;
762 ret = phy_aneg_done(phydev);
763 } while (!ret && --retries);
768 return ret < 0 ? ret : 0;
771 int phy_ethtool_ksettings_set(struct phy_device *phydev,
772 const struct ethtool_link_ksettings *cmd)
774 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
775 u8 autoneg = cmd->base.autoneg;
776 u8 duplex = cmd->base.duplex;
777 u32 speed = cmd->base.speed;
779 if (cmd->base.phy_address != phydev->mdio.addr)
782 linkmode_copy(advertising, cmd->link_modes.advertising);
784 /* We make sure that we don't pass unsupported values in to the PHY */
785 linkmode_and(advertising, advertising, phydev->supported);
787 /* Verify the settings we care about. */
788 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
791 if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
794 if (autoneg == AUTONEG_DISABLE &&
795 ((speed != SPEED_1000 &&
796 speed != SPEED_100 &&
797 speed != SPEED_10) ||
798 (duplex != DUPLEX_HALF &&
799 duplex != DUPLEX_FULL)))
802 mutex_lock(&phydev->lock);
803 phydev->autoneg = autoneg;
805 if (autoneg == AUTONEG_DISABLE) {
806 phydev->speed = speed;
807 phydev->duplex = duplex;
810 linkmode_copy(phydev->advertising, advertising);
812 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
813 phydev->advertising, autoneg == AUTONEG_ENABLE);
815 phydev->master_slave_set = cmd->base.master_slave_cfg;
816 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
818 /* Restart the PHY */
819 if (phy_is_started(phydev)) {
820 phydev->state = PHY_UP;
821 phy_trigger_machine(phydev);
823 _phy_start_aneg(phydev);
826 mutex_unlock(&phydev->lock);
829 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
832 * phy_speed_down - set speed to lowest speed supported by both link partners
833 * @phydev: the phy_device struct
834 * @sync: perform action synchronously
836 * Description: Typically used to save energy when waiting for a WoL packet
838 * WARNING: Setting sync to false may cause the system being unable to suspend
839 * in case the PHY generates an interrupt when finishing the autonegotiation.
840 * This interrupt may wake up the system immediately after suspend.
841 * Therefore use sync = false only if you're sure it's safe with the respective
844 int phy_speed_down(struct phy_device *phydev, bool sync)
846 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
849 if (phydev->autoneg != AUTONEG_ENABLE)
852 linkmode_copy(adv_tmp, phydev->advertising);
854 ret = phy_speed_down_core(phydev);
858 linkmode_copy(phydev->adv_old, adv_tmp);
860 if (linkmode_equal(phydev->advertising, adv_tmp))
863 ret = phy_config_aneg(phydev);
867 return sync ? phy_poll_aneg_done(phydev) : 0;
869 EXPORT_SYMBOL_GPL(phy_speed_down);
872 * phy_speed_up - (re)set advertised speeds to all supported speeds
873 * @phydev: the phy_device struct
875 * Description: Used to revert the effect of phy_speed_down
877 int phy_speed_up(struct phy_device *phydev)
879 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
881 if (phydev->autoneg != AUTONEG_ENABLE)
884 if (linkmode_empty(phydev->adv_old))
887 linkmode_copy(adv_tmp, phydev->advertising);
888 linkmode_copy(phydev->advertising, phydev->adv_old);
889 linkmode_zero(phydev->adv_old);
891 if (linkmode_equal(phydev->advertising, adv_tmp))
894 return phy_config_aneg(phydev);
896 EXPORT_SYMBOL_GPL(phy_speed_up);
899 * phy_start_machine - start PHY state machine tracking
900 * @phydev: the phy_device struct
902 * Description: The PHY infrastructure can run a state machine
903 * which tracks whether the PHY is starting up, negotiating,
904 * etc. This function starts the delayed workqueue which tracks
905 * the state of the PHY. If you want to maintain your own state machine,
906 * do not call this function.
908 void phy_start_machine(struct phy_device *phydev)
910 phy_trigger_machine(phydev);
912 EXPORT_SYMBOL_GPL(phy_start_machine);
915 * phy_stop_machine - stop the PHY state machine tracking
916 * @phydev: target phy_device struct
918 * Description: Stops the state machine delayed workqueue, sets the
919 * state to UP (unless it wasn't up yet). This function must be
920 * called BEFORE phy_detach.
922 void phy_stop_machine(struct phy_device *phydev)
924 cancel_delayed_work_sync(&phydev->state_queue);
926 mutex_lock(&phydev->lock);
927 if (phy_is_started(phydev))
928 phydev->state = PHY_UP;
929 mutex_unlock(&phydev->lock);
933 * phy_error - enter HALTED state for this PHY device
934 * @phydev: target phy_device struct
936 * Moves the PHY to the HALTED state in response to a read
937 * or write error, and tells the controller the link is down.
938 * Must not be called from interrupt context, or while the
939 * phydev->lock is held.
941 void phy_error(struct phy_device *phydev)
945 mutex_lock(&phydev->lock);
946 phydev->state = PHY_HALTED;
947 mutex_unlock(&phydev->lock);
949 phy_trigger_machine(phydev);
951 EXPORT_SYMBOL(phy_error);
954 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
955 * @phydev: target phy_device struct
957 int phy_disable_interrupts(struct phy_device *phydev)
959 /* Disable PHY interrupts */
960 return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
964 * phy_interrupt - PHY interrupt handler
965 * @irq: interrupt line
966 * @phy_dat: phy_device pointer
968 * Description: Handle PHY interrupt
970 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
972 struct phy_device *phydev = phy_dat;
973 struct phy_driver *drv = phydev->drv;
976 /* Wakeup interrupts may occur during a system sleep transition.
977 * Postpone handling until the PHY has resumed.
979 if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
980 struct net_device *netdev = phydev->attached_dev;
983 struct device *parent = netdev->dev.parent;
985 if (netdev->wol_enabled)
987 else if (device_may_wakeup(&netdev->dev))
988 pm_wakeup_dev_event(&netdev->dev, 0, true);
989 else if (parent && device_may_wakeup(parent))
990 pm_wakeup_dev_event(parent, 0, true);
993 phydev->irq_rerun = 1;
994 disable_irq_nosync(irq);
998 mutex_lock(&phydev->lock);
999 ret = drv->handle_interrupt(phydev);
1000 mutex_unlock(&phydev->lock);
1006 * phy_enable_interrupts - Enable the interrupts from the PHY side
1007 * @phydev: target phy_device struct
1009 static int phy_enable_interrupts(struct phy_device *phydev)
1011 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1015 * phy_request_interrupt - request and enable interrupt for a PHY device
1016 * @phydev: target phy_device struct
1018 * Description: Request and enable the interrupt for the given PHY.
1019 * If this fails, then we set irq to PHY_POLL.
1020 * This should only be called with a valid IRQ number.
1022 void phy_request_interrupt(struct phy_device *phydev)
1026 err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1027 IRQF_ONESHOT | IRQF_SHARED,
1028 phydev_name(phydev), phydev);
1030 phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1032 phydev->irq = PHY_POLL;
1034 if (phy_enable_interrupts(phydev)) {
1035 phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1036 phy_free_interrupt(phydev);
1037 phydev->irq = PHY_POLL;
1041 EXPORT_SYMBOL(phy_request_interrupt);
1044 * phy_free_interrupt - disable and free interrupt for a PHY device
1045 * @phydev: target phy_device struct
1047 * Description: Disable and free the interrupt for the given PHY.
1048 * This should only be called with a valid IRQ number.
1050 void phy_free_interrupt(struct phy_device *phydev)
1052 phy_disable_interrupts(phydev);
1053 free_irq(phydev->irq, phydev);
1055 EXPORT_SYMBOL(phy_free_interrupt);
1058 * phy_stop - Bring down the PHY link, and stop checking the status
1059 * @phydev: target phy_device struct
1061 void phy_stop(struct phy_device *phydev)
1063 struct net_device *dev = phydev->attached_dev;
1065 if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
1066 WARN(1, "called from state %s\n",
1067 phy_state_to_str(phydev->state));
1071 mutex_lock(&phydev->lock);
1073 if (phydev->state == PHY_CABLETEST) {
1074 phy_abort_cable_test(phydev);
1075 netif_testing_off(dev);
1078 if (phydev->sfp_bus)
1079 sfp_upstream_stop(phydev->sfp_bus);
1081 phydev->state = PHY_HALTED;
1083 mutex_unlock(&phydev->lock);
1085 phy_state_machine(&phydev->state_queue.work);
1086 phy_stop_machine(phydev);
1088 /* Cannot call flush_scheduled_work() here as desired because
1089 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
1090 * will not reenable interrupts.
1093 EXPORT_SYMBOL(phy_stop);
1096 * phy_start - start or restart a PHY device
1097 * @phydev: target phy_device struct
1099 * Description: Indicates the attached device's readiness to
1100 * handle PHY-related work. Used during startup to start the
1101 * PHY, and after a call to phy_stop() to resume operation.
1102 * Also used to indicate the MDIO bus has cleared an error
1105 void phy_start(struct phy_device *phydev)
1107 mutex_lock(&phydev->lock);
1109 if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1110 WARN(1, "called from state %s\n",
1111 phy_state_to_str(phydev->state));
1115 if (phydev->sfp_bus)
1116 sfp_upstream_start(phydev->sfp_bus);
1118 /* if phy was suspended, bring the physical link up again */
1119 __phy_resume(phydev);
1121 phydev->state = PHY_UP;
1123 phy_start_machine(phydev);
1125 mutex_unlock(&phydev->lock);
1127 EXPORT_SYMBOL(phy_start);
1130 * phy_state_machine - Handle the state machine
1131 * @work: work_struct that describes the work to be done
1133 void phy_state_machine(struct work_struct *work)
1135 struct delayed_work *dwork = to_delayed_work(work);
1136 struct phy_device *phydev =
1137 container_of(dwork, struct phy_device, state_queue);
1138 struct net_device *dev = phydev->attached_dev;
1139 bool needs_aneg = false, do_suspend = false;
1140 enum phy_state old_state;
1141 bool finished = false;
1144 mutex_lock(&phydev->lock);
1146 old_state = phydev->state;
1148 switch (phydev->state) {
1158 err = phy_check_link_status(phydev);
1161 err = phydev->drv->cable_test_get_status(phydev, &finished);
1163 phy_abort_cable_test(phydev);
1164 netif_testing_off(dev);
1166 phydev->state = PHY_UP;
1171 ethnl_cable_test_finished(phydev);
1172 netif_testing_off(dev);
1174 phydev->state = PHY_UP;
1180 phy_link_down(phydev);
1186 mutex_unlock(&phydev->lock);
1189 err = phy_start_aneg(phydev);
1190 else if (do_suspend)
1191 phy_suspend(phydev);
1199 if (old_state != phydev->state) {
1200 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1201 phy_state_to_str(old_state),
1202 phy_state_to_str(phydev->state));
1203 if (phydev->drv && phydev->drv->link_change_notify)
1204 phydev->drv->link_change_notify(phydev);
1207 /* Only re-schedule a PHY state machine change if we are polling the
1208 * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
1209 * between states from phy_mac_interrupt().
1211 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1212 * state machine would be pointless and possibly error prone when
1213 * called from phy_disconnect() synchronously.
1215 mutex_lock(&phydev->lock);
1216 if (phy_polling_mode(phydev) && phy_is_started(phydev))
1217 phy_queue_state_machine(phydev, PHY_STATE_TIME);
1218 mutex_unlock(&phydev->lock);
1222 * phy_mac_interrupt - MAC says the link has changed
1223 * @phydev: phy_device struct with changed link
1225 * The MAC layer is able to indicate there has been a change in the PHY link
1226 * status. Trigger the state machine and work a work queue.
1228 void phy_mac_interrupt(struct phy_device *phydev)
1230 /* Trigger a state machine change */
1231 phy_trigger_machine(phydev);
1233 EXPORT_SYMBOL(phy_mac_interrupt);
1235 static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
1237 linkmode_zero(advertising);
1239 if (eee_adv & MDIO_EEE_100TX)
1240 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1242 if (eee_adv & MDIO_EEE_1000T)
1243 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1245 if (eee_adv & MDIO_EEE_10GT)
1246 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1248 if (eee_adv & MDIO_EEE_1000KX)
1249 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1251 if (eee_adv & MDIO_EEE_10GKX4)
1252 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1254 if (eee_adv & MDIO_EEE_10GKR)
1255 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1260 * phy_init_eee - init and check the EEE feature
1261 * @phydev: target phy_device struct
1262 * @clk_stop_enable: PHY may stop the clock during LPI
1264 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1265 * is supported by looking at the MMD registers 3.20 and 7.60/61
1266 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1269 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1274 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1276 if (phydev->duplex == DUPLEX_FULL) {
1277 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
1278 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp);
1279 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
1280 int eee_lp, eee_cap, eee_adv;
1284 /* Read phy status to properly get the right settings */
1285 status = phy_read_status(phydev);
1289 /* First check if the EEE ability is supported */
1290 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1294 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1298 /* Check which link settings negotiated and verify it in
1299 * the EEE advertising registers.
1301 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1305 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1309 mmd_eee_adv_to_linkmode(adv, eee_adv);
1310 mmd_eee_adv_to_linkmode(lp, eee_lp);
1311 linkmode_and(common, adv, lp);
1313 if (!phy_check_valid(phydev->speed, phydev->duplex, common))
1316 if (clk_stop_enable)
1317 /* Configure the PHY to stop receiving xMII
1318 * clock while it is signaling LPI.
1320 phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1321 MDIO_PCS_CTRL1_CLKSTOP_EN);
1323 return 0; /* EEE supported */
1326 return -EPROTONOSUPPORT;
1328 EXPORT_SYMBOL(phy_init_eee);
1331 * phy_get_eee_err - report the EEE wake error count
1332 * @phydev: target phy_device struct
1334 * Description: it is to report the number of time where the PHY
1335 * failed to complete its normal wake sequence.
1337 int phy_get_eee_err(struct phy_device *phydev)
1342 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1344 EXPORT_SYMBOL(phy_get_eee_err);
1347 * phy_ethtool_get_eee - get EEE supported and status
1348 * @phydev: target phy_device struct
1349 * @data: ethtool_eee data
1351 * Description: it reportes the Supported/Advertisement/LP Advertisement
1354 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1361 /* Get Supported EEE */
1362 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1365 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1367 /* Get advertisement EEE */
1368 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1371 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1372 data->eee_enabled = !!data->advertised;
1374 /* Get LP advertisement EEE */
1375 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1378 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1380 data->eee_active = !!(data->advertised & data->lp_advertised);
1384 EXPORT_SYMBOL(phy_ethtool_get_eee);
1387 * phy_ethtool_set_eee - set EEE supported and status
1388 * @phydev: target phy_device struct
1389 * @data: ethtool_eee data
1391 * Description: it is to program the Advertisement EEE register.
1393 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1395 int cap, old_adv, adv = 0, ret;
1400 /* Get Supported EEE */
1401 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1405 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1409 if (data->eee_enabled) {
1410 adv = !data->advertised ? cap :
1411 ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1412 /* Mask prohibited EEE modes */
1413 adv &= ~phydev->eee_broken_modes;
1416 if (old_adv != adv) {
1417 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1421 /* Restart autonegotiation so the new modes get sent to the
1424 if (phydev->autoneg == AUTONEG_ENABLE) {
1425 ret = phy_restart_aneg(phydev);
1433 EXPORT_SYMBOL(phy_ethtool_set_eee);
1436 * phy_ethtool_set_wol - Configure Wake On LAN
1438 * @phydev: target phy_device struct
1439 * @wol: Configuration requested
1441 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1443 if (phydev->drv && phydev->drv->set_wol)
1444 return phydev->drv->set_wol(phydev, wol);
1448 EXPORT_SYMBOL(phy_ethtool_set_wol);
1451 * phy_ethtool_get_wol - Get the current Wake On LAN configuration
1453 * @phydev: target phy_device struct
1454 * @wol: Store the current configuration here
1456 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1458 if (phydev->drv && phydev->drv->get_wol)
1459 phydev->drv->get_wol(phydev, wol);
1461 EXPORT_SYMBOL(phy_ethtool_get_wol);
1463 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1464 struct ethtool_link_ksettings *cmd)
1466 struct phy_device *phydev = ndev->phydev;
1471 phy_ethtool_ksettings_get(phydev, cmd);
1475 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1477 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1478 const struct ethtool_link_ksettings *cmd)
1480 struct phy_device *phydev = ndev->phydev;
1485 return phy_ethtool_ksettings_set(phydev, cmd);
1487 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1490 * phy_ethtool_nway_reset - Restart auto negotiation
1491 * @ndev: Network device to restart autoneg for
1493 int phy_ethtool_nway_reset(struct net_device *ndev)
1495 struct phy_device *phydev = ndev->phydev;
1503 return phy_restart_aneg(phydev);
1505 EXPORT_SYMBOL(phy_ethtool_nway_reset);