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 <net/netlink.h>
35 #include <net/genetlink.h>
38 #define PHY_STATE_TIME HZ
40 #define PHY_STATE_STR(_state) \
42 return __stringify(_state); \
44 static const char *phy_state_to_str(enum phy_state st)
50 PHY_STATE_STR(RUNNING)
52 PHY_STATE_STR(CABLETEST)
59 static void phy_process_state_change(struct phy_device *phydev,
60 enum phy_state old_state)
62 if (old_state != phydev->state) {
63 phydev_dbg(phydev, "PHY state change %s -> %s\n",
64 phy_state_to_str(old_state),
65 phy_state_to_str(phydev->state));
66 if (phydev->drv && phydev->drv->link_change_notify)
67 phydev->drv->link_change_notify(phydev);
71 static void phy_link_up(struct phy_device *phydev)
73 phydev->phy_link_change(phydev, true);
74 phy_led_trigger_change_speed(phydev);
77 static void phy_link_down(struct phy_device *phydev)
79 phydev->phy_link_change(phydev, false);
80 phy_led_trigger_change_speed(phydev);
83 static const char *phy_pause_str(struct phy_device *phydev)
85 bool local_pause, local_asym_pause;
87 if (phydev->autoneg == AUTONEG_DISABLE)
90 local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
92 local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
95 if (local_pause && phydev->pause)
98 if (local_asym_pause && phydev->asym_pause) {
110 * phy_print_status - Convenience function to print out the current phy status
111 * @phydev: the phy_device struct
113 void phy_print_status(struct phy_device *phydev)
116 netdev_info(phydev->attached_dev,
117 "Link is Up - %s/%s %s- flow control %s\n",
118 phy_speed_to_str(phydev->speed),
119 phy_duplex_to_str(phydev->duplex),
120 phydev->downshifted_rate ? "(downshifted) " : "",
121 phy_pause_str(phydev));
123 netdev_info(phydev->attached_dev, "Link is Down\n");
126 EXPORT_SYMBOL(phy_print_status);
129 * phy_clear_interrupt - Ack the phy device's interrupt
130 * @phydev: the phy_device struct
132 * If the @phydev driver has an ack_interrupt function, call it to
133 * ack and clear the phy device's interrupt.
135 * Returns 0 on success or < 0 on error.
137 static int phy_clear_interrupt(struct phy_device *phydev)
141 if (phydev->drv->ack_interrupt) {
142 mutex_lock(&phydev->lock);
143 ret = phydev->drv->ack_interrupt(phydev);
144 mutex_unlock(&phydev->lock);
151 * phy_config_interrupt - configure the PHY device for the requested interrupts
152 * @phydev: the phy_device struct
153 * @interrupts: interrupt flags to configure for this @phydev
155 * Returns 0 on success or < 0 on error.
157 static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
159 phydev->interrupts = interrupts ? 1 : 0;
160 if (phydev->drv->config_intr)
161 return phydev->drv->config_intr(phydev);
167 * phy_restart_aneg - restart auto-negotiation
168 * @phydev: target phy_device struct
170 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
171 * negative errno on error.
173 int phy_restart_aneg(struct phy_device *phydev)
177 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
178 ret = genphy_c45_restart_aneg(phydev);
180 ret = genphy_restart_aneg(phydev);
184 EXPORT_SYMBOL_GPL(phy_restart_aneg);
187 * phy_aneg_done - return auto-negotiation status
188 * @phydev: target phy_device struct
190 * Description: Return the auto-negotiation status from this @phydev
191 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
194 int phy_aneg_done(struct phy_device *phydev)
196 if (phydev->drv && phydev->drv->aneg_done)
197 return phydev->drv->aneg_done(phydev);
198 else if (phydev->is_c45)
199 return genphy_c45_aneg_done(phydev);
201 return genphy_aneg_done(phydev);
203 EXPORT_SYMBOL(phy_aneg_done);
206 * phy_find_valid - find a PHY setting that matches the requested parameters
207 * @speed: desired speed
208 * @duplex: desired duplex
209 * @supported: mask of supported link modes
211 * Locate a supported phy setting that is, in priority order:
212 * - an exact match for the specified speed and duplex mode
213 * - a match for the specified speed, or slower speed
214 * - the slowest supported speed
215 * Returns the matched phy_setting entry, or %NULL if no supported phy
216 * settings were found.
218 static const struct phy_setting *
219 phy_find_valid(int speed, int duplex, unsigned long *supported)
221 return phy_lookup_setting(speed, duplex, supported, false);
225 * phy_supported_speeds - return all speeds currently supported by a phy device
226 * @phy: The phy device to return supported speeds of.
227 * @speeds: buffer to store supported speeds in.
228 * @size: size of speeds buffer.
230 * Description: Returns the number of supported speeds, and fills the speeds
231 * buffer with the supported speeds. If speeds buffer is too small to contain
232 * all currently supported speeds, will return as many speeds as can fit.
234 unsigned int phy_supported_speeds(struct phy_device *phy,
235 unsigned int *speeds,
238 return phy_speeds(speeds, size, phy->supported);
242 * phy_check_valid - check if there is a valid PHY setting which matches
243 * speed, duplex, and feature mask
244 * @speed: speed to match
245 * @duplex: duplex to match
246 * @features: A mask of the valid settings
248 * Description: Returns true if there is a valid setting, false otherwise.
250 static inline bool phy_check_valid(int speed, int duplex,
251 unsigned long *features)
253 return !!phy_lookup_setting(speed, duplex, features, true);
257 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
258 * @phydev: the target phy_device struct
260 * Description: Make sure the PHY is set to supported speeds and
261 * duplexes. Drop down by one in this order: 1000/FULL,
262 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
264 static void phy_sanitize_settings(struct phy_device *phydev)
266 const struct phy_setting *setting;
268 setting = phy_find_valid(phydev->speed, phydev->duplex,
271 phydev->speed = setting->speed;
272 phydev->duplex = setting->duplex;
274 /* We failed to find anything (no supported speeds?) */
275 phydev->speed = SPEED_UNKNOWN;
276 phydev->duplex = DUPLEX_UNKNOWN;
280 void phy_ethtool_ksettings_get(struct phy_device *phydev,
281 struct ethtool_link_ksettings *cmd)
283 mutex_lock(&phydev->lock);
284 linkmode_copy(cmd->link_modes.supported, phydev->supported);
285 linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
286 linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
288 cmd->base.speed = phydev->speed;
289 cmd->base.duplex = phydev->duplex;
290 cmd->base.master_slave_cfg = phydev->master_slave_get;
291 cmd->base.master_slave_state = phydev->master_slave_state;
292 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
293 cmd->base.port = PORT_BNC;
295 cmd->base.port = phydev->port;
296 cmd->base.transceiver = phy_is_internal(phydev) ?
297 XCVR_INTERNAL : XCVR_EXTERNAL;
298 cmd->base.phy_address = phydev->mdio.addr;
299 cmd->base.autoneg = phydev->autoneg;
300 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
301 cmd->base.eth_tp_mdix = phydev->mdix;
302 mutex_unlock(&phydev->lock);
304 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
307 * phy_mii_ioctl - generic PHY MII ioctl interface
308 * @phydev: the phy_device struct
309 * @ifr: &struct ifreq for socket ioctl's
310 * @cmd: ioctl cmd to execute
312 * Note that this function is currently incompatible with the
313 * PHYCONTROL layer. It changes registers without regard to
314 * current state. Use at own risk.
316 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
318 struct mii_ioctl_data *mii_data = if_mii(ifr);
319 u16 val = mii_data->val_in;
320 bool change_autoneg = false;
325 mii_data->phy_id = phydev->mdio.addr;
329 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
330 prtad = mdio_phy_id_prtad(mii_data->phy_id);
331 devad = mdio_phy_id_devad(mii_data->phy_id);
332 devad = mdiobus_c45_addr(devad, mii_data->reg_num);
334 prtad = mii_data->phy_id;
335 devad = mii_data->reg_num;
337 mii_data->val_out = mdiobus_read(phydev->mdio.bus, prtad,
342 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
343 prtad = mdio_phy_id_prtad(mii_data->phy_id);
344 devad = mdio_phy_id_devad(mii_data->phy_id);
345 devad = mdiobus_c45_addr(devad, mii_data->reg_num);
347 prtad = mii_data->phy_id;
348 devad = mii_data->reg_num;
350 if (prtad == phydev->mdio.addr) {
353 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
354 if (phydev->autoneg == AUTONEG_ENABLE)
355 change_autoneg = true;
356 phydev->autoneg = AUTONEG_DISABLE;
357 if (val & BMCR_FULLDPLX)
358 phydev->duplex = DUPLEX_FULL;
360 phydev->duplex = DUPLEX_HALF;
361 if (val & BMCR_SPEED1000)
362 phydev->speed = SPEED_1000;
363 else if (val & BMCR_SPEED100)
364 phydev->speed = SPEED_100;
365 else phydev->speed = SPEED_10;
368 if (phydev->autoneg == AUTONEG_DISABLE)
369 change_autoneg = true;
370 phydev->autoneg = AUTONEG_ENABLE;
374 mii_adv_mod_linkmode_adv_t(phydev->advertising,
376 change_autoneg = true;
379 mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
381 change_autoneg = true;
389 mdiobus_write(phydev->mdio.bus, prtad, devad, val);
391 if (prtad == phydev->mdio.addr &&
394 return phy_init_hw(phydev);
397 return phy_start_aneg(phydev);
402 if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
403 return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
410 EXPORT_SYMBOL(phy_mii_ioctl);
413 * phy_do_ioctl - generic ndo_do_ioctl implementation
414 * @dev: the net_device struct
415 * @ifr: &struct ifreq for socket ioctl's
416 * @cmd: ioctl cmd to execute
418 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
423 return phy_mii_ioctl(dev->phydev, ifr, cmd);
425 EXPORT_SYMBOL(phy_do_ioctl);
428 * phy_do_ioctl_running - generic ndo_do_ioctl implementation but test first
430 * @dev: the net_device struct
431 * @ifr: &struct ifreq for socket ioctl's
432 * @cmd: ioctl cmd to execute
434 * Same as phy_do_ioctl, but ensures that net_device is running before
435 * handling the ioctl.
437 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
439 if (!netif_running(dev))
442 return phy_do_ioctl(dev, ifr, cmd);
444 EXPORT_SYMBOL(phy_do_ioctl_running);
447 * phy_queue_state_machine - Trigger the state machine to run soon
449 * @phydev: the phy_device struct
450 * @jiffies: Run the state machine after these jiffies
452 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
454 mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
457 EXPORT_SYMBOL(phy_queue_state_machine);
460 * phy_queue_state_machine - Trigger the state machine to run now
462 * @phydev: the phy_device struct
464 static void phy_trigger_machine(struct phy_device *phydev)
466 phy_queue_state_machine(phydev, 0);
469 static void phy_abort_cable_test(struct phy_device *phydev)
473 ethnl_cable_test_finished(phydev);
475 err = phy_init_hw(phydev);
477 phydev_err(phydev, "Error while aborting cable test");
481 * phy_ethtool_get_strings - Get the statistic counter names
483 * @phydev: the phy_device struct
484 * @data: Where to put the strings
486 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
491 mutex_lock(&phydev->lock);
492 phydev->drv->get_strings(phydev, data);
493 mutex_unlock(&phydev->lock);
497 EXPORT_SYMBOL(phy_ethtool_get_strings);
500 * phy_ethtool_get_sset_count - Get the number of statistic counters
502 * @phydev: the phy_device struct
504 int phy_ethtool_get_sset_count(struct phy_device *phydev)
511 if (phydev->drv->get_sset_count &&
512 phydev->drv->get_strings &&
513 phydev->drv->get_stats) {
514 mutex_lock(&phydev->lock);
515 ret = phydev->drv->get_sset_count(phydev);
516 mutex_unlock(&phydev->lock);
523 EXPORT_SYMBOL(phy_ethtool_get_sset_count);
526 * phy_ethtool_get_stats - Get the statistic counters
528 * @phydev: the phy_device struct
529 * @stats: What counters to get
530 * @data: Where to store the counters
532 int phy_ethtool_get_stats(struct phy_device *phydev,
533 struct ethtool_stats *stats, u64 *data)
538 mutex_lock(&phydev->lock);
539 phydev->drv->get_stats(phydev, stats, data);
540 mutex_unlock(&phydev->lock);
544 EXPORT_SYMBOL(phy_ethtool_get_stats);
547 * phy_start_cable_test - Start a cable test
549 * @phydev: the phy_device struct
550 * @extack: extack for reporting useful error messages
552 int phy_start_cable_test(struct phy_device *phydev,
553 struct netlink_ext_ack *extack)
555 struct net_device *dev = phydev->attached_dev;
559 phydev->drv->cable_test_start &&
560 phydev->drv->cable_test_get_status)) {
561 NL_SET_ERR_MSG(extack,
562 "PHY driver does not support cable testing");
566 mutex_lock(&phydev->lock);
567 if (phydev->state == PHY_CABLETEST) {
568 NL_SET_ERR_MSG(extack,
569 "PHY already performing a test");
574 if (phydev->state < PHY_UP ||
575 phydev->state > PHY_CABLETEST) {
576 NL_SET_ERR_MSG(extack,
577 "PHY not configured. Try setting interface up");
582 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
586 /* Mark the carrier down until the test is complete */
587 phy_link_down(phydev);
589 netif_testing_on(dev);
590 err = phydev->drv->cable_test_start(phydev);
592 netif_testing_off(dev);
597 phydev->state = PHY_CABLETEST;
599 if (phy_polling_mode(phydev))
600 phy_trigger_machine(phydev);
602 mutex_unlock(&phydev->lock);
607 ethnl_cable_test_free(phydev);
609 mutex_unlock(&phydev->lock);
613 EXPORT_SYMBOL(phy_start_cable_test);
616 * phy_start_cable_test_tdr - Start a raw TDR cable test
618 * @phydev: the phy_device struct
619 * @extack: extack for reporting useful error messages
620 * @config: Configuration of the test to run
622 int phy_start_cable_test_tdr(struct phy_device *phydev,
623 struct netlink_ext_ack *extack,
624 const struct phy_tdr_config *config)
626 struct net_device *dev = phydev->attached_dev;
630 phydev->drv->cable_test_tdr_start &&
631 phydev->drv->cable_test_get_status)) {
632 NL_SET_ERR_MSG(extack,
633 "PHY driver does not support cable test TDR");
637 mutex_lock(&phydev->lock);
638 if (phydev->state == PHY_CABLETEST) {
639 NL_SET_ERR_MSG(extack,
640 "PHY already performing a test");
645 if (phydev->state < PHY_UP ||
646 phydev->state > PHY_CABLETEST) {
647 NL_SET_ERR_MSG(extack,
648 "PHY not configured. Try setting interface up");
653 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
657 /* Mark the carrier down until the test is complete */
658 phy_link_down(phydev);
660 netif_testing_on(dev);
661 err = phydev->drv->cable_test_tdr_start(phydev, config);
663 netif_testing_off(dev);
668 phydev->state = PHY_CABLETEST;
670 if (phy_polling_mode(phydev))
671 phy_trigger_machine(phydev);
673 mutex_unlock(&phydev->lock);
678 ethnl_cable_test_free(phydev);
680 mutex_unlock(&phydev->lock);
684 EXPORT_SYMBOL(phy_start_cable_test_tdr);
686 static int phy_config_aneg(struct phy_device *phydev)
688 if (phydev->drv->config_aneg)
689 return phydev->drv->config_aneg(phydev);
691 /* Clause 45 PHYs that don't implement Clause 22 registers are not
692 * allowed to call genphy_config_aneg()
694 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
695 return genphy_c45_config_aneg(phydev);
697 return genphy_config_aneg(phydev);
701 * phy_check_link_status - check link status and set state accordingly
702 * @phydev: the phy_device struct
704 * Description: Check for link and whether autoneg was triggered / is running
705 * and set state accordingly
707 static int phy_check_link_status(struct phy_device *phydev)
711 WARN_ON(!mutex_is_locked(&phydev->lock));
713 /* Keep previous state if loopback is enabled because some PHYs
714 * report that Link is Down when loopback is enabled.
716 if (phydev->loopback_enabled)
719 err = phy_read_status(phydev);
723 if (phydev->link && phydev->state != PHY_RUNNING) {
724 phy_check_downshift(phydev);
725 phydev->state = PHY_RUNNING;
727 } else if (!phydev->link && phydev->state != PHY_NOLINK) {
728 phydev->state = PHY_NOLINK;
729 phy_link_down(phydev);
736 * _phy_start_aneg - start auto-negotiation for this PHY device
737 * @phydev: the phy_device struct
739 * Description: Sanitizes the settings (if we're not autonegotiating
740 * them), and then calls the driver's config_aneg function.
741 * If the PHYCONTROL Layer is operating, we change the state to
742 * reflect the beginning of Auto-negotiation or forcing.
744 static int _phy_start_aneg(struct phy_device *phydev)
748 lockdep_assert_held(&phydev->lock);
753 if (AUTONEG_DISABLE == phydev->autoneg)
754 phy_sanitize_settings(phydev);
756 err = phy_config_aneg(phydev);
760 if (phy_is_started(phydev))
761 err = phy_check_link_status(phydev);
767 * phy_start_aneg - start auto-negotiation for this PHY device
768 * @phydev: the phy_device struct
770 * Description: Sanitizes the settings (if we're not autonegotiating
771 * them), and then calls the driver's config_aneg function.
772 * If the PHYCONTROL Layer is operating, we change the state to
773 * reflect the beginning of Auto-negotiation or forcing.
775 int phy_start_aneg(struct phy_device *phydev)
779 mutex_lock(&phydev->lock);
780 err = _phy_start_aneg(phydev);
781 mutex_unlock(&phydev->lock);
785 EXPORT_SYMBOL(phy_start_aneg);
787 static int phy_poll_aneg_done(struct phy_device *phydev)
789 unsigned int retries = 100;
794 ret = phy_aneg_done(phydev);
795 } while (!ret && --retries);
800 return ret < 0 ? ret : 0;
803 int phy_ethtool_ksettings_set(struct phy_device *phydev,
804 const struct ethtool_link_ksettings *cmd)
806 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
807 u8 autoneg = cmd->base.autoneg;
808 u8 duplex = cmd->base.duplex;
809 u32 speed = cmd->base.speed;
811 if (cmd->base.phy_address != phydev->mdio.addr)
814 linkmode_copy(advertising, cmd->link_modes.advertising);
816 /* We make sure that we don't pass unsupported values in to the PHY */
817 linkmode_and(advertising, advertising, phydev->supported);
819 /* Verify the settings we care about. */
820 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
823 if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
826 if (autoneg == AUTONEG_DISABLE &&
827 ((speed != SPEED_1000 &&
828 speed != SPEED_100 &&
829 speed != SPEED_10) ||
830 (duplex != DUPLEX_HALF &&
831 duplex != DUPLEX_FULL)))
834 mutex_lock(&phydev->lock);
835 phydev->autoneg = autoneg;
837 if (autoneg == AUTONEG_DISABLE) {
838 phydev->speed = speed;
839 phydev->duplex = duplex;
842 linkmode_copy(phydev->advertising, advertising);
844 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
845 phydev->advertising, autoneg == AUTONEG_ENABLE);
847 phydev->master_slave_set = cmd->base.master_slave_cfg;
848 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
850 /* Restart the PHY */
851 if (phy_is_started(phydev)) {
852 phydev->state = PHY_UP;
853 phy_trigger_machine(phydev);
855 _phy_start_aneg(phydev);
858 mutex_unlock(&phydev->lock);
861 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
864 * phy_speed_down - set speed to lowest speed supported by both link partners
865 * @phydev: the phy_device struct
866 * @sync: perform action synchronously
868 * Description: Typically used to save energy when waiting for a WoL packet
870 * WARNING: Setting sync to false may cause the system being unable to suspend
871 * in case the PHY generates an interrupt when finishing the autonegotiation.
872 * This interrupt may wake up the system immediately after suspend.
873 * Therefore use sync = false only if you're sure it's safe with the respective
876 int phy_speed_down(struct phy_device *phydev, bool sync)
878 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
881 if (phydev->autoneg != AUTONEG_ENABLE)
884 linkmode_copy(adv_tmp, phydev->advertising);
886 ret = phy_speed_down_core(phydev);
890 linkmode_copy(phydev->adv_old, adv_tmp);
892 if (linkmode_equal(phydev->advertising, adv_tmp))
895 ret = phy_config_aneg(phydev);
899 return sync ? phy_poll_aneg_done(phydev) : 0;
901 EXPORT_SYMBOL_GPL(phy_speed_down);
904 * phy_speed_up - (re)set advertised speeds to all supported speeds
905 * @phydev: the phy_device struct
907 * Description: Used to revert the effect of phy_speed_down
909 int phy_speed_up(struct phy_device *phydev)
911 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
913 if (phydev->autoneg != AUTONEG_ENABLE)
916 if (linkmode_empty(phydev->adv_old))
919 linkmode_copy(adv_tmp, phydev->advertising);
920 linkmode_copy(phydev->advertising, phydev->adv_old);
921 linkmode_zero(phydev->adv_old);
923 if (linkmode_equal(phydev->advertising, adv_tmp))
926 return phy_config_aneg(phydev);
928 EXPORT_SYMBOL_GPL(phy_speed_up);
931 * phy_start_machine - start PHY state machine tracking
932 * @phydev: the phy_device struct
934 * Description: The PHY infrastructure can run a state machine
935 * which tracks whether the PHY is starting up, negotiating,
936 * etc. This function starts the delayed workqueue which tracks
937 * the state of the PHY. If you want to maintain your own state machine,
938 * do not call this function.
940 void phy_start_machine(struct phy_device *phydev)
942 phy_trigger_machine(phydev);
944 EXPORT_SYMBOL_GPL(phy_start_machine);
947 * phy_stop_machine - stop the PHY state machine tracking
948 * @phydev: target phy_device struct
950 * Description: Stops the state machine delayed workqueue, sets the
951 * state to UP (unless it wasn't up yet). This function must be
952 * called BEFORE phy_detach.
954 void phy_stop_machine(struct phy_device *phydev)
956 cancel_delayed_work_sync(&phydev->state_queue);
958 mutex_lock(&phydev->lock);
959 if (phy_is_started(phydev))
960 phydev->state = PHY_UP;
961 mutex_unlock(&phydev->lock);
965 * phy_error - enter HALTED state for this PHY device
966 * @phydev: target phy_device struct
968 * Moves the PHY to the HALTED state in response to a read
969 * or write error, and tells the controller the link is down.
970 * Must not be called from interrupt context, or while the
971 * phydev->lock is held.
973 static void phy_error(struct phy_device *phydev)
977 mutex_lock(&phydev->lock);
978 phydev->state = PHY_HALTED;
979 mutex_unlock(&phydev->lock);
981 phy_trigger_machine(phydev);
985 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
986 * @phydev: target phy_device struct
988 int phy_disable_interrupts(struct phy_device *phydev)
992 /* Disable PHY interrupts */
993 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
997 /* Clear the interrupt */
998 return phy_clear_interrupt(phydev);
1002 * phy_did_interrupt - Checks if the PHY generated an interrupt
1003 * @phydev: target phy_device struct
1005 static int phy_did_interrupt(struct phy_device *phydev)
1009 mutex_lock(&phydev->lock);
1010 ret = phydev->drv->did_interrupt(phydev);
1011 mutex_unlock(&phydev->lock);
1017 * phy_handle_interrupt - Handle PHY interrupt
1018 * @phydev: target phy_device struct
1020 static irqreturn_t phy_handle_interrupt(struct phy_device *phydev)
1024 mutex_lock(&phydev->lock);
1025 ret = phydev->drv->handle_interrupt(phydev);
1026 mutex_unlock(&phydev->lock);
1032 * phy_interrupt - PHY interrupt handler
1033 * @irq: interrupt line
1034 * @phy_dat: phy_device pointer
1036 * Description: Handle PHY interrupt
1038 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
1040 struct phy_device *phydev = phy_dat;
1041 struct phy_driver *drv = phydev->drv;
1043 if (drv->handle_interrupt)
1044 return phy_handle_interrupt(phydev);
1046 if (drv->did_interrupt && !phy_did_interrupt(phydev))
1049 /* reschedule state queue work to run as soon as possible */
1050 phy_trigger_machine(phydev);
1052 /* did_interrupt() may have cleared the interrupt already */
1053 if (!drv->did_interrupt && phy_clear_interrupt(phydev)) {
1062 * phy_enable_interrupts - Enable the interrupts from the PHY side
1063 * @phydev: target phy_device struct
1065 static int phy_enable_interrupts(struct phy_device *phydev)
1067 int err = phy_clear_interrupt(phydev);
1072 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1076 * phy_request_interrupt - request and enable interrupt for a PHY device
1077 * @phydev: target phy_device struct
1079 * Description: Request and enable the interrupt for the given PHY.
1080 * If this fails, then we set irq to PHY_POLL.
1081 * This should only be called with a valid IRQ number.
1083 void phy_request_interrupt(struct phy_device *phydev)
1087 err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1088 IRQF_ONESHOT | IRQF_SHARED,
1089 phydev_name(phydev), phydev);
1091 phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1093 phydev->irq = PHY_POLL;
1095 if (phy_enable_interrupts(phydev)) {
1096 phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1097 phy_free_interrupt(phydev);
1098 phydev->irq = PHY_POLL;
1102 EXPORT_SYMBOL(phy_request_interrupt);
1105 * phy_free_interrupt - disable and free interrupt for a PHY device
1106 * @phydev: target phy_device struct
1108 * Description: Disable and free the interrupt for the given PHY.
1109 * This should only be called with a valid IRQ number.
1111 void phy_free_interrupt(struct phy_device *phydev)
1113 phy_disable_interrupts(phydev);
1114 free_irq(phydev->irq, phydev);
1116 EXPORT_SYMBOL(phy_free_interrupt);
1119 * phy_stop - Bring down the PHY link, and stop checking the status
1120 * @phydev: target phy_device struct
1122 void phy_stop(struct phy_device *phydev)
1124 struct net_device *dev = phydev->attached_dev;
1125 enum phy_state old_state;
1127 if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
1128 WARN(1, "called from state %s\n",
1129 phy_state_to_str(phydev->state));
1133 mutex_lock(&phydev->lock);
1134 old_state = phydev->state;
1136 if (phydev->state == PHY_CABLETEST) {
1137 phy_abort_cable_test(phydev);
1138 netif_testing_off(dev);
1141 if (phydev->sfp_bus)
1142 sfp_upstream_stop(phydev->sfp_bus);
1144 phydev->state = PHY_HALTED;
1145 phy_process_state_change(phydev, old_state);
1147 mutex_unlock(&phydev->lock);
1149 phy_state_machine(&phydev->state_queue.work);
1150 phy_stop_machine(phydev);
1152 /* Cannot call flush_scheduled_work() here as desired because
1153 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
1154 * will not reenable interrupts.
1157 EXPORT_SYMBOL(phy_stop);
1160 * phy_start - start or restart a PHY device
1161 * @phydev: target phy_device struct
1163 * Description: Indicates the attached device's readiness to
1164 * handle PHY-related work. Used during startup to start the
1165 * PHY, and after a call to phy_stop() to resume operation.
1166 * Also used to indicate the MDIO bus has cleared an error
1169 void phy_start(struct phy_device *phydev)
1171 mutex_lock(&phydev->lock);
1173 if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1174 WARN(1, "called from state %s\n",
1175 phy_state_to_str(phydev->state));
1179 if (phydev->sfp_bus)
1180 sfp_upstream_start(phydev->sfp_bus);
1182 /* if phy was suspended, bring the physical link up again */
1183 __phy_resume(phydev);
1185 phydev->state = PHY_UP;
1187 phy_start_machine(phydev);
1189 mutex_unlock(&phydev->lock);
1191 EXPORT_SYMBOL(phy_start);
1194 * phy_state_machine - Handle the state machine
1195 * @work: work_struct that describes the work to be done
1197 void phy_state_machine(struct work_struct *work)
1199 struct delayed_work *dwork = to_delayed_work(work);
1200 struct phy_device *phydev =
1201 container_of(dwork, struct phy_device, state_queue);
1202 struct net_device *dev = phydev->attached_dev;
1203 bool needs_aneg = false, do_suspend = false;
1204 enum phy_state old_state;
1205 bool finished = false;
1208 mutex_lock(&phydev->lock);
1210 old_state = phydev->state;
1212 switch (phydev->state) {
1222 err = phy_check_link_status(phydev);
1225 err = phydev->drv->cable_test_get_status(phydev, &finished);
1227 phy_abort_cable_test(phydev);
1228 netif_testing_off(dev);
1230 phydev->state = PHY_UP;
1235 ethnl_cable_test_finished(phydev);
1236 netif_testing_off(dev);
1238 phydev->state = PHY_UP;
1244 phy_link_down(phydev);
1250 mutex_unlock(&phydev->lock);
1253 err = phy_start_aneg(phydev);
1254 else if (do_suspend)
1255 phy_suspend(phydev);
1260 phy_process_state_change(phydev, old_state);
1262 /* Only re-schedule a PHY state machine change if we are polling the
1263 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1264 * between states from phy_mac_interrupt().
1266 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1267 * state machine would be pointless and possibly error prone when
1268 * called from phy_disconnect() synchronously.
1270 mutex_lock(&phydev->lock);
1271 if (phy_polling_mode(phydev) && phy_is_started(phydev))
1272 phy_queue_state_machine(phydev, PHY_STATE_TIME);
1273 mutex_unlock(&phydev->lock);
1277 * phy_mac_interrupt - MAC says the link has changed
1278 * @phydev: phy_device struct with changed link
1280 * The MAC layer is able to indicate there has been a change in the PHY link
1281 * status. Trigger the state machine and work a work queue.
1283 void phy_mac_interrupt(struct phy_device *phydev)
1285 /* Trigger a state machine change */
1286 phy_trigger_machine(phydev);
1288 EXPORT_SYMBOL(phy_mac_interrupt);
1290 static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
1292 linkmode_zero(advertising);
1294 if (eee_adv & MDIO_EEE_100TX)
1295 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1297 if (eee_adv & MDIO_EEE_1000T)
1298 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1300 if (eee_adv & MDIO_EEE_10GT)
1301 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1303 if (eee_adv & MDIO_EEE_1000KX)
1304 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1306 if (eee_adv & MDIO_EEE_10GKX4)
1307 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1309 if (eee_adv & MDIO_EEE_10GKR)
1310 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1315 * phy_init_eee - init and check the EEE feature
1316 * @phydev: target phy_device struct
1317 * @clk_stop_enable: PHY may stop the clock during LPI
1319 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1320 * is supported by looking at the MMD registers 3.20 and 7.60/61
1321 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1324 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1329 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1331 if (phydev->duplex == DUPLEX_FULL) {
1332 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
1333 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp);
1334 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
1335 int eee_lp, eee_cap, eee_adv;
1339 /* Read phy status to properly get the right settings */
1340 status = phy_read_status(phydev);
1344 /* First check if the EEE ability is supported */
1345 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1349 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1353 /* Check which link settings negotiated and verify it in
1354 * the EEE advertising registers.
1356 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1360 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1364 mmd_eee_adv_to_linkmode(adv, eee_adv);
1365 mmd_eee_adv_to_linkmode(lp, eee_lp);
1366 linkmode_and(common, adv, lp);
1368 if (!phy_check_valid(phydev->speed, phydev->duplex, common))
1371 if (clk_stop_enable)
1372 /* Configure the PHY to stop receiving xMII
1373 * clock while it is signaling LPI.
1375 phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1376 MDIO_PCS_CTRL1_CLKSTOP_EN);
1378 return 0; /* EEE supported */
1381 return -EPROTONOSUPPORT;
1383 EXPORT_SYMBOL(phy_init_eee);
1386 * phy_get_eee_err - report the EEE wake error count
1387 * @phydev: target phy_device struct
1389 * Description: it is to report the number of time where the PHY
1390 * failed to complete its normal wake sequence.
1392 int phy_get_eee_err(struct phy_device *phydev)
1397 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1399 EXPORT_SYMBOL(phy_get_eee_err);
1402 * phy_ethtool_get_eee - get EEE supported and status
1403 * @phydev: target phy_device struct
1404 * @data: ethtool_eee data
1406 * Description: it reportes the Supported/Advertisement/LP Advertisement
1409 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1416 /* Get Supported EEE */
1417 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1420 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1422 /* Get advertisement EEE */
1423 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1426 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1427 data->eee_enabled = !!data->advertised;
1429 /* Get LP advertisement EEE */
1430 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1433 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1435 data->eee_active = !!(data->advertised & data->lp_advertised);
1439 EXPORT_SYMBOL(phy_ethtool_get_eee);
1442 * phy_ethtool_set_eee - set EEE supported and status
1443 * @phydev: target phy_device struct
1444 * @data: ethtool_eee data
1446 * Description: it is to program the Advertisement EEE register.
1448 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1450 int cap, old_adv, adv = 0, ret;
1455 /* Get Supported EEE */
1456 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1460 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1464 if (data->eee_enabled) {
1465 adv = !data->advertised ? cap :
1466 ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1467 /* Mask prohibited EEE modes */
1468 adv &= ~phydev->eee_broken_modes;
1471 if (old_adv != adv) {
1472 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1476 /* Restart autonegotiation so the new modes get sent to the
1479 if (phydev->autoneg == AUTONEG_ENABLE) {
1480 ret = phy_restart_aneg(phydev);
1488 EXPORT_SYMBOL(phy_ethtool_set_eee);
1491 * phy_ethtool_set_wol - Configure Wake On LAN
1493 * @phydev: target phy_device struct
1494 * @wol: Configuration requested
1496 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1498 if (phydev->drv && phydev->drv->set_wol)
1499 return phydev->drv->set_wol(phydev, wol);
1503 EXPORT_SYMBOL(phy_ethtool_set_wol);
1506 * phy_ethtool_get_wol - Get the current Wake On LAN configuration
1508 * @phydev: target phy_device struct
1509 * @wol: Store the current configuration here
1511 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1513 if (phydev->drv && phydev->drv->get_wol)
1514 phydev->drv->get_wol(phydev, wol);
1516 EXPORT_SYMBOL(phy_ethtool_get_wol);
1518 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1519 struct ethtool_link_ksettings *cmd)
1521 struct phy_device *phydev = ndev->phydev;
1526 phy_ethtool_ksettings_get(phydev, cmd);
1530 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1532 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1533 const struct ethtool_link_ksettings *cmd)
1535 struct phy_device *phydev = ndev->phydev;
1540 return phy_ethtool_ksettings_set(phydev, cmd);
1542 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1545 * phy_ethtool_nway_reset - Restart auto negotiation
1546 * @ndev: Network device to restart autoneg for
1548 int phy_ethtool_nway_reset(struct net_device *ndev)
1550 struct phy_device *phydev = ndev->phydev;
1558 return phy_restart_aneg(phydev);
1560 EXPORT_SYMBOL(phy_ethtool_nway_reset);