xref: /kernel/linux/linux-5.10/drivers/net/phy/phy.c (revision 8c2ecf20)
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
4 *
5 * Author: Andy Fleming
6 *
7 * Copyright (c) 2004 Freescale Semiconductor, Inc.
8 * Copyright (c) 2006, 2007  Maciej W. Rozycki
9 */
10
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>
21#include <linux/mm.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>
31#include <linux/io.h>
32#include <linux/uaccess.h>
33#include <linux/atomic.h>
34#include <net/netlink.h>
35#include <net/genetlink.h>
36#include <net/sock.h>
37
38#define PHY_STATE_TIME	HZ
39
40#define PHY_STATE_STR(_state)			\
41	case PHY_##_state:			\
42		return __stringify(_state);	\
43
44static const char *phy_state_to_str(enum phy_state st)
45{
46	switch (st) {
47	PHY_STATE_STR(DOWN)
48	PHY_STATE_STR(READY)
49	PHY_STATE_STR(UP)
50	PHY_STATE_STR(RUNNING)
51	PHY_STATE_STR(NOLINK)
52	PHY_STATE_STR(CABLETEST)
53	PHY_STATE_STR(HALTED)
54	}
55
56	return NULL;
57}
58
59static void phy_process_state_change(struct phy_device *phydev,
60				     enum phy_state old_state)
61{
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);
68	}
69}
70
71static void phy_link_up(struct phy_device *phydev)
72{
73	phydev->phy_link_change(phydev, true);
74	phy_led_trigger_change_speed(phydev);
75}
76
77static void phy_link_down(struct phy_device *phydev)
78{
79	phydev->phy_link_change(phydev, false);
80	phy_led_trigger_change_speed(phydev);
81}
82
83static const char *phy_pause_str(struct phy_device *phydev)
84{
85	bool local_pause, local_asym_pause;
86
87	if (phydev->autoneg == AUTONEG_DISABLE)
88		goto no_pause;
89
90	local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
91					phydev->advertising);
92	local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
93					     phydev->advertising);
94
95	if (local_pause && phydev->pause)
96		return "rx/tx";
97
98	if (local_asym_pause && phydev->asym_pause) {
99		if (local_pause)
100			return "rx";
101		if (phydev->pause)
102			return "tx";
103	}
104
105no_pause:
106	return "off";
107}
108
109/**
110 * phy_print_status - Convenience function to print out the current phy status
111 * @phydev: the phy_device struct
112 */
113void phy_print_status(struct phy_device *phydev)
114{
115	if (phydev->link) {
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));
122	} else	{
123		netdev_info(phydev->attached_dev, "Link is Down\n");
124	}
125}
126EXPORT_SYMBOL(phy_print_status);
127
128/**
129 * phy_clear_interrupt - Ack the phy device's interrupt
130 * @phydev: the phy_device struct
131 *
132 * If the @phydev driver has an ack_interrupt function, call it to
133 * ack and clear the phy device's interrupt.
134 *
135 * Returns 0 on success or < 0 on error.
136 */
137static int phy_clear_interrupt(struct phy_device *phydev)
138{
139	int ret = 0;
140
141	if (phydev->drv->ack_interrupt) {
142		mutex_lock(&phydev->lock);
143		ret = phydev->drv->ack_interrupt(phydev);
144		mutex_unlock(&phydev->lock);
145	}
146
147	return ret;
148}
149
150/**
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
154 *
155 * Returns 0 on success or < 0 on error.
156 */
157static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
158{
159	phydev->interrupts = interrupts ? 1 : 0;
160	if (phydev->drv->config_intr)
161		return phydev->drv->config_intr(phydev);
162
163	return 0;
164}
165
166/**
167 * phy_restart_aneg - restart auto-negotiation
168 * @phydev: target phy_device struct
169 *
170 * Restart the autonegotiation on @phydev.  Returns >= 0 on success or
171 * negative errno on error.
172 */
173int phy_restart_aneg(struct phy_device *phydev)
174{
175	int ret;
176
177	if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
178		ret = genphy_c45_restart_aneg(phydev);
179	else
180		ret = genphy_restart_aneg(phydev);
181
182	return ret;
183}
184EXPORT_SYMBOL_GPL(phy_restart_aneg);
185
186/**
187 * phy_aneg_done - return auto-negotiation status
188 * @phydev: target phy_device struct
189 *
190 * Description: Return the auto-negotiation status from this @phydev
191 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
192 * is still pending.
193 */
194int phy_aneg_done(struct phy_device *phydev)
195{
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);
200	else
201		return genphy_aneg_done(phydev);
202}
203EXPORT_SYMBOL(phy_aneg_done);
204
205/**
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
210 *
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.
217 */
218static const struct phy_setting *
219phy_find_valid(int speed, int duplex, unsigned long *supported)
220{
221	return phy_lookup_setting(speed, duplex, supported, false);
222}
223
224/**
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.
229 *
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.
233 */
234unsigned int phy_supported_speeds(struct phy_device *phy,
235				  unsigned int *speeds,
236				  unsigned int size)
237{
238	return phy_speeds(speeds, size, phy->supported);
239}
240
241/**
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
247 *
248 * Description: Returns true if there is a valid setting, false otherwise.
249 */
250static inline bool phy_check_valid(int speed, int duplex,
251				   unsigned long *features)
252{
253	return !!phy_lookup_setting(speed, duplex, features, true);
254}
255
256/**
257 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
258 * @phydev: the target phy_device struct
259 *
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.
263 */
264static void phy_sanitize_settings(struct phy_device *phydev)
265{
266	const struct phy_setting *setting;
267
268	setting = phy_find_valid(phydev->speed, phydev->duplex,
269				 phydev->supported);
270	if (setting) {
271		phydev->speed = setting->speed;
272		phydev->duplex = setting->duplex;
273	} else {
274		/* We failed to find anything (no supported speeds?) */
275		phydev->speed = SPEED_UNKNOWN;
276		phydev->duplex = DUPLEX_UNKNOWN;
277	}
278}
279
280void phy_ethtool_ksettings_get(struct phy_device *phydev,
281			       struct ethtool_link_ksettings *cmd)
282{
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);
287
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;
294	else
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);
303}
304EXPORT_SYMBOL(phy_ethtool_ksettings_get);
305
306/**
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
311 *
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.
315 */
316int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
317{
318	struct mii_ioctl_data *mii_data = if_mii(ifr);
319	u16 val = mii_data->val_in;
320	bool change_autoneg = false;
321	int prtad, devad;
322
323	switch (cmd) {
324	case SIOCGMIIPHY:
325		mii_data->phy_id = phydev->mdio.addr;
326		fallthrough;
327
328	case SIOCGMIIREG:
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);
333		} else {
334			prtad = mii_data->phy_id;
335			devad = mii_data->reg_num;
336		}
337		mii_data->val_out = mdiobus_read(phydev->mdio.bus, prtad,
338						 devad);
339		return 0;
340
341	case SIOCSMIIREG:
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);
346		} else {
347			prtad = mii_data->phy_id;
348			devad = mii_data->reg_num;
349		}
350		if (prtad == phydev->mdio.addr) {
351			switch (devad) {
352			case MII_BMCR:
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;
359					else
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;
366				}
367				else {
368					if (phydev->autoneg == AUTONEG_DISABLE)
369						change_autoneg = true;
370					phydev->autoneg = AUTONEG_ENABLE;
371				}
372				break;
373			case MII_ADVERTISE:
374				mii_adv_mod_linkmode_adv_t(phydev->advertising,
375							   val);
376				change_autoneg = true;
377				break;
378			case MII_CTRL1000:
379				mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
380							        val);
381				change_autoneg = true;
382				break;
383			default:
384				/* do nothing */
385				break;
386			}
387		}
388
389		mdiobus_write(phydev->mdio.bus, prtad, devad, val);
390
391		if (prtad == phydev->mdio.addr &&
392		    devad == MII_BMCR &&
393		    val & BMCR_RESET)
394			return phy_init_hw(phydev);
395
396		if (change_autoneg)
397			return phy_start_aneg(phydev);
398
399		return 0;
400
401	case SIOCSHWTSTAMP:
402		if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
403			return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
404		fallthrough;
405
406	default:
407		return -EOPNOTSUPP;
408	}
409}
410EXPORT_SYMBOL(phy_mii_ioctl);
411
412/**
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
417 */
418int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
419{
420	if (!dev->phydev)
421		return -ENODEV;
422
423	return phy_mii_ioctl(dev->phydev, ifr, cmd);
424}
425EXPORT_SYMBOL(phy_do_ioctl);
426
427/**
428 * phy_do_ioctl_running - generic ndo_do_ioctl implementation but test first
429 *
430 * @dev: the net_device struct
431 * @ifr: &struct ifreq for socket ioctl's
432 * @cmd: ioctl cmd to execute
433 *
434 * Same as phy_do_ioctl, but ensures that net_device is running before
435 * handling the ioctl.
436 */
437int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
438{
439	if (!netif_running(dev))
440		return -ENODEV;
441
442	return phy_do_ioctl(dev, ifr, cmd);
443}
444EXPORT_SYMBOL(phy_do_ioctl_running);
445
446/**
447 * phy_queue_state_machine - Trigger the state machine to run soon
448 *
449 * @phydev: the phy_device struct
450 * @jiffies: Run the state machine after these jiffies
451 */
452void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
453{
454	mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
455			 jiffies);
456}
457EXPORT_SYMBOL(phy_queue_state_machine);
458
459/**
460 * phy_queue_state_machine - Trigger the state machine to run now
461 *
462 * @phydev: the phy_device struct
463 */
464static void phy_trigger_machine(struct phy_device *phydev)
465{
466	phy_queue_state_machine(phydev, 0);
467}
468
469static void phy_abort_cable_test(struct phy_device *phydev)
470{
471	int err;
472
473	ethnl_cable_test_finished(phydev);
474
475	err = phy_init_hw(phydev);
476	if (err)
477		phydev_err(phydev, "Error while aborting cable test");
478}
479
480/**
481 * phy_ethtool_get_strings - Get the statistic counter names
482 *
483 * @phydev: the phy_device struct
484 * @data: Where to put the strings
485 */
486int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
487{
488	if (!phydev->drv)
489		return -EIO;
490
491	mutex_lock(&phydev->lock);
492	phydev->drv->get_strings(phydev, data);
493	mutex_unlock(&phydev->lock);
494
495	return 0;
496}
497EXPORT_SYMBOL(phy_ethtool_get_strings);
498
499/**
500 * phy_ethtool_get_sset_count - Get the number of statistic counters
501 *
502 * @phydev: the phy_device struct
503 */
504int phy_ethtool_get_sset_count(struct phy_device *phydev)
505{
506	int ret;
507
508	if (!phydev->drv)
509		return -EIO;
510
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);
517
518		return ret;
519	}
520
521	return -EOPNOTSUPP;
522}
523EXPORT_SYMBOL(phy_ethtool_get_sset_count);
524
525/**
526 * phy_ethtool_get_stats - Get the statistic counters
527 *
528 * @phydev: the phy_device struct
529 * @stats: What counters to get
530 * @data: Where to store the counters
531 */
532int phy_ethtool_get_stats(struct phy_device *phydev,
533			  struct ethtool_stats *stats, u64 *data)
534{
535	if (!phydev->drv)
536		return -EIO;
537
538	mutex_lock(&phydev->lock);
539	phydev->drv->get_stats(phydev, stats, data);
540	mutex_unlock(&phydev->lock);
541
542	return 0;
543}
544EXPORT_SYMBOL(phy_ethtool_get_stats);
545
546/**
547 * phy_start_cable_test - Start a cable test
548 *
549 * @phydev: the phy_device struct
550 * @extack: extack for reporting useful error messages
551 */
552int phy_start_cable_test(struct phy_device *phydev,
553			 struct netlink_ext_ack *extack)
554{
555	struct net_device *dev = phydev->attached_dev;
556	int err = -ENOMEM;
557
558	if (!(phydev->drv &&
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");
563		return -EOPNOTSUPP;
564	}
565
566	mutex_lock(&phydev->lock);
567	if (phydev->state == PHY_CABLETEST) {
568		NL_SET_ERR_MSG(extack,
569			       "PHY already performing a test");
570		err = -EBUSY;
571		goto out;
572	}
573
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");
578		err = -EBUSY;
579		goto out;
580	}
581
582	err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
583	if (err)
584		goto out;
585
586	/* Mark the carrier down until the test is complete */
587	phy_link_down(phydev);
588
589	netif_testing_on(dev);
590	err = phydev->drv->cable_test_start(phydev);
591	if (err) {
592		netif_testing_off(dev);
593		phy_link_up(phydev);
594		goto out_free;
595	}
596
597	phydev->state = PHY_CABLETEST;
598
599	if (phy_polling_mode(phydev))
600		phy_trigger_machine(phydev);
601
602	mutex_unlock(&phydev->lock);
603
604	return 0;
605
606out_free:
607	ethnl_cable_test_free(phydev);
608out:
609	mutex_unlock(&phydev->lock);
610
611	return err;
612}
613EXPORT_SYMBOL(phy_start_cable_test);
614
615/**
616 * phy_start_cable_test_tdr - Start a raw TDR cable test
617 *
618 * @phydev: the phy_device struct
619 * @extack: extack for reporting useful error messages
620 * @config: Configuration of the test to run
621 */
622int phy_start_cable_test_tdr(struct phy_device *phydev,
623			     struct netlink_ext_ack *extack,
624			     const struct phy_tdr_config *config)
625{
626	struct net_device *dev = phydev->attached_dev;
627	int err = -ENOMEM;
628
629	if (!(phydev->drv &&
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");
634		return -EOPNOTSUPP;
635	}
636
637	mutex_lock(&phydev->lock);
638	if (phydev->state == PHY_CABLETEST) {
639		NL_SET_ERR_MSG(extack,
640			       "PHY already performing a test");
641		err = -EBUSY;
642		goto out;
643	}
644
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");
649		err = -EBUSY;
650		goto out;
651	}
652
653	err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
654	if (err)
655		goto out;
656
657	/* Mark the carrier down until the test is complete */
658	phy_link_down(phydev);
659
660	netif_testing_on(dev);
661	err = phydev->drv->cable_test_tdr_start(phydev, config);
662	if (err) {
663		netif_testing_off(dev);
664		phy_link_up(phydev);
665		goto out_free;
666	}
667
668	phydev->state = PHY_CABLETEST;
669
670	if (phy_polling_mode(phydev))
671		phy_trigger_machine(phydev);
672
673	mutex_unlock(&phydev->lock);
674
675	return 0;
676
677out_free:
678	ethnl_cable_test_free(phydev);
679out:
680	mutex_unlock(&phydev->lock);
681
682	return err;
683}
684EXPORT_SYMBOL(phy_start_cable_test_tdr);
685
686static int phy_config_aneg(struct phy_device *phydev)
687{
688	if (phydev->drv->config_aneg)
689		return phydev->drv->config_aneg(phydev);
690
691	/* Clause 45 PHYs that don't implement Clause 22 registers are not
692	 * allowed to call genphy_config_aneg()
693	 */
694	if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
695		return genphy_c45_config_aneg(phydev);
696
697	return genphy_config_aneg(phydev);
698}
699
700/**
701 * phy_check_link_status - check link status and set state accordingly
702 * @phydev: the phy_device struct
703 *
704 * Description: Check for link and whether autoneg was triggered / is running
705 * and set state accordingly
706 */
707static int phy_check_link_status(struct phy_device *phydev)
708{
709	int err;
710
711	WARN_ON(!mutex_is_locked(&phydev->lock));
712
713	/* Keep previous state if loopback is enabled because some PHYs
714	 * report that Link is Down when loopback is enabled.
715	 */
716	if (phydev->loopback_enabled)
717		return 0;
718
719	err = phy_read_status(phydev);
720	if (err)
721		return err;
722
723	if (phydev->link && phydev->state != PHY_RUNNING) {
724		phy_check_downshift(phydev);
725		phydev->state = PHY_RUNNING;
726		phy_link_up(phydev);
727	} else if (!phydev->link && phydev->state != PHY_NOLINK) {
728		phydev->state = PHY_NOLINK;
729		phy_link_down(phydev);
730	}
731
732	return 0;
733}
734
735/**
736 * _phy_start_aneg - start auto-negotiation for this PHY device
737 * @phydev: the phy_device struct
738 *
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.
743 */
744static int _phy_start_aneg(struct phy_device *phydev)
745{
746	int err;
747
748	lockdep_assert_held(&phydev->lock);
749
750	if (!phydev->drv)
751		return -EIO;
752
753	if (AUTONEG_DISABLE == phydev->autoneg)
754		phy_sanitize_settings(phydev);
755
756	err = phy_config_aneg(phydev);
757	if (err < 0)
758		return err;
759
760	if (phy_is_started(phydev))
761		err = phy_check_link_status(phydev);
762
763	return err;
764}
765
766/**
767 * phy_start_aneg - start auto-negotiation for this PHY device
768 * @phydev: the phy_device struct
769 *
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.
774 */
775int phy_start_aneg(struct phy_device *phydev)
776{
777	int err;
778
779	mutex_lock(&phydev->lock);
780	err = _phy_start_aneg(phydev);
781	mutex_unlock(&phydev->lock);
782
783	return err;
784}
785EXPORT_SYMBOL(phy_start_aneg);
786
787static int phy_poll_aneg_done(struct phy_device *phydev)
788{
789	unsigned int retries = 100;
790	int ret;
791
792	do {
793		msleep(100);
794		ret = phy_aneg_done(phydev);
795	} while (!ret && --retries);
796
797	if (!ret)
798		return -ETIMEDOUT;
799
800	return ret < 0 ? ret : 0;
801}
802
803int phy_ethtool_ksettings_set(struct phy_device *phydev,
804			      const struct ethtool_link_ksettings *cmd)
805{
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;
810
811	if (cmd->base.phy_address != phydev->mdio.addr)
812		return -EINVAL;
813
814	linkmode_copy(advertising, cmd->link_modes.advertising);
815
816	/* We make sure that we don't pass unsupported values in to the PHY */
817	linkmode_and(advertising, advertising, phydev->supported);
818
819	/* Verify the settings we care about. */
820	if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
821		return -EINVAL;
822
823	if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
824		return -EINVAL;
825
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)))
832		return -EINVAL;
833
834	mutex_lock(&phydev->lock);
835	phydev->autoneg = autoneg;
836
837	if (autoneg == AUTONEG_DISABLE) {
838		phydev->speed = speed;
839		phydev->duplex = duplex;
840	}
841
842	linkmode_copy(phydev->advertising, advertising);
843
844	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
845			 phydev->advertising, autoneg == AUTONEG_ENABLE);
846
847	phydev->master_slave_set = cmd->base.master_slave_cfg;
848	phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
849
850	/* Restart the PHY */
851	if (phy_is_started(phydev)) {
852		phydev->state = PHY_UP;
853		phy_trigger_machine(phydev);
854	} else {
855		_phy_start_aneg(phydev);
856	}
857
858	mutex_unlock(&phydev->lock);
859	return 0;
860}
861EXPORT_SYMBOL(phy_ethtool_ksettings_set);
862
863/**
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
867 *
868 * Description: Typically used to save energy when waiting for a WoL packet
869 *
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
874 * network chip.
875 */
876int phy_speed_down(struct phy_device *phydev, bool sync)
877{
878	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
879	int ret;
880
881	if (phydev->autoneg != AUTONEG_ENABLE)
882		return 0;
883
884	linkmode_copy(adv_tmp, phydev->advertising);
885
886	ret = phy_speed_down_core(phydev);
887	if (ret)
888		return ret;
889
890	linkmode_copy(phydev->adv_old, adv_tmp);
891
892	if (linkmode_equal(phydev->advertising, adv_tmp))
893		return 0;
894
895	ret = phy_config_aneg(phydev);
896	if (ret)
897		return ret;
898
899	return sync ? phy_poll_aneg_done(phydev) : 0;
900}
901EXPORT_SYMBOL_GPL(phy_speed_down);
902
903/**
904 * phy_speed_up - (re)set advertised speeds to all supported speeds
905 * @phydev: the phy_device struct
906 *
907 * Description: Used to revert the effect of phy_speed_down
908 */
909int phy_speed_up(struct phy_device *phydev)
910{
911	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
912
913	if (phydev->autoneg != AUTONEG_ENABLE)
914		return 0;
915
916	if (linkmode_empty(phydev->adv_old))
917		return 0;
918
919	linkmode_copy(adv_tmp, phydev->advertising);
920	linkmode_copy(phydev->advertising, phydev->adv_old);
921	linkmode_zero(phydev->adv_old);
922
923	if (linkmode_equal(phydev->advertising, adv_tmp))
924		return 0;
925
926	return phy_config_aneg(phydev);
927}
928EXPORT_SYMBOL_GPL(phy_speed_up);
929
930/**
931 * phy_start_machine - start PHY state machine tracking
932 * @phydev: the phy_device struct
933 *
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.
939 */
940void phy_start_machine(struct phy_device *phydev)
941{
942	phy_trigger_machine(phydev);
943}
944EXPORT_SYMBOL_GPL(phy_start_machine);
945
946/**
947 * phy_stop_machine - stop the PHY state machine tracking
948 * @phydev: target phy_device struct
949 *
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.
953 */
954void phy_stop_machine(struct phy_device *phydev)
955{
956	cancel_delayed_work_sync(&phydev->state_queue);
957
958	mutex_lock(&phydev->lock);
959	if (phy_is_started(phydev))
960		phydev->state = PHY_UP;
961	mutex_unlock(&phydev->lock);
962}
963
964/**
965 * phy_error - enter HALTED state for this PHY device
966 * @phydev: target phy_device struct
967 *
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.
972 */
973static void phy_error(struct phy_device *phydev)
974{
975	WARN_ON(1);
976
977	mutex_lock(&phydev->lock);
978	phydev->state = PHY_HALTED;
979	mutex_unlock(&phydev->lock);
980
981	phy_trigger_machine(phydev);
982}
983
984/**
985 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
986 * @phydev: target phy_device struct
987 */
988int phy_disable_interrupts(struct phy_device *phydev)
989{
990	int err;
991
992	/* Disable PHY interrupts */
993	err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
994	if (err)
995		return err;
996
997	/* Clear the interrupt */
998	return phy_clear_interrupt(phydev);
999}
1000
1001/**
1002 * phy_did_interrupt - Checks if the PHY generated an interrupt
1003 * @phydev: target phy_device struct
1004 */
1005static int phy_did_interrupt(struct phy_device *phydev)
1006{
1007	int ret;
1008
1009	mutex_lock(&phydev->lock);
1010	ret = phydev->drv->did_interrupt(phydev);
1011	mutex_unlock(&phydev->lock);
1012
1013	return ret;
1014}
1015
1016/**
1017 * phy_handle_interrupt - Handle PHY interrupt
1018 * @phydev: target phy_device struct
1019 */
1020static irqreturn_t phy_handle_interrupt(struct phy_device *phydev)
1021{
1022	irqreturn_t ret;
1023
1024	mutex_lock(&phydev->lock);
1025	ret = phydev->drv->handle_interrupt(phydev);
1026	mutex_unlock(&phydev->lock);
1027
1028	return ret;
1029}
1030
1031/**
1032 * phy_interrupt - PHY interrupt handler
1033 * @irq: interrupt line
1034 * @phy_dat: phy_device pointer
1035 *
1036 * Description: Handle PHY interrupt
1037 */
1038static irqreturn_t phy_interrupt(int irq, void *phy_dat)
1039{
1040	struct phy_device *phydev = phy_dat;
1041	struct phy_driver *drv = phydev->drv;
1042
1043	if (drv->handle_interrupt)
1044		return phy_handle_interrupt(phydev);
1045
1046	if (drv->did_interrupt && !phy_did_interrupt(phydev))
1047		return IRQ_NONE;
1048
1049	/* reschedule state queue work to run as soon as possible */
1050	phy_trigger_machine(phydev);
1051
1052	/* did_interrupt() may have cleared the interrupt already */
1053	if (!drv->did_interrupt && phy_clear_interrupt(phydev)) {
1054		phy_error(phydev);
1055		return IRQ_NONE;
1056	}
1057
1058	return IRQ_HANDLED;
1059}
1060
1061/**
1062 * phy_enable_interrupts - Enable the interrupts from the PHY side
1063 * @phydev: target phy_device struct
1064 */
1065static int phy_enable_interrupts(struct phy_device *phydev)
1066{
1067	int err = phy_clear_interrupt(phydev);
1068
1069	if (err < 0)
1070		return err;
1071
1072	return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1073}
1074
1075/**
1076 * phy_request_interrupt - request and enable interrupt for a PHY device
1077 * @phydev: target phy_device struct
1078 *
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.
1082 */
1083void phy_request_interrupt(struct phy_device *phydev)
1084{
1085	int err;
1086
1087	err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1088				   IRQF_ONESHOT | IRQF_SHARED,
1089				   phydev_name(phydev), phydev);
1090	if (err) {
1091		phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1092			    err, phydev->irq);
1093		phydev->irq = PHY_POLL;
1094	} else {
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;
1099		}
1100	}
1101}
1102EXPORT_SYMBOL(phy_request_interrupt);
1103
1104/**
1105 * phy_free_interrupt - disable and free interrupt for a PHY device
1106 * @phydev: target phy_device struct
1107 *
1108 * Description: Disable and free the interrupt for the given PHY.
1109 *   This should only be called with a valid IRQ number.
1110 */
1111void phy_free_interrupt(struct phy_device *phydev)
1112{
1113	phy_disable_interrupts(phydev);
1114	free_irq(phydev->irq, phydev);
1115}
1116EXPORT_SYMBOL(phy_free_interrupt);
1117
1118/**
1119 * phy_stop - Bring down the PHY link, and stop checking the status
1120 * @phydev: target phy_device struct
1121 */
1122void phy_stop(struct phy_device *phydev)
1123{
1124	struct net_device *dev = phydev->attached_dev;
1125	enum phy_state old_state;
1126
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));
1130		return;
1131	}
1132
1133	mutex_lock(&phydev->lock);
1134	old_state = phydev->state;
1135
1136	if (phydev->state == PHY_CABLETEST) {
1137		phy_abort_cable_test(phydev);
1138		netif_testing_off(dev);
1139	}
1140
1141	if (phydev->sfp_bus)
1142		sfp_upstream_stop(phydev->sfp_bus);
1143
1144	phydev->state = PHY_HALTED;
1145	phy_process_state_change(phydev, old_state);
1146
1147	mutex_unlock(&phydev->lock);
1148
1149	phy_state_machine(&phydev->state_queue.work);
1150	phy_stop_machine(phydev);
1151
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.
1155	 */
1156}
1157EXPORT_SYMBOL(phy_stop);
1158
1159/**
1160 * phy_start - start or restart a PHY device
1161 * @phydev: target phy_device struct
1162 *
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
1167 *   condition.
1168 */
1169void phy_start(struct phy_device *phydev)
1170{
1171	mutex_lock(&phydev->lock);
1172
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));
1176		goto out;
1177	}
1178
1179	if (phydev->sfp_bus)
1180		sfp_upstream_start(phydev->sfp_bus);
1181
1182	/* if phy was suspended, bring the physical link up again */
1183	__phy_resume(phydev);
1184
1185	phydev->state = PHY_UP;
1186
1187	phy_start_machine(phydev);
1188out:
1189	mutex_unlock(&phydev->lock);
1190}
1191EXPORT_SYMBOL(phy_start);
1192
1193/**
1194 * phy_state_machine - Handle the state machine
1195 * @work: work_struct that describes the work to be done
1196 */
1197void phy_state_machine(struct work_struct *work)
1198{
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;
1206	int err = 0;
1207
1208	mutex_lock(&phydev->lock);
1209
1210	old_state = phydev->state;
1211
1212	switch (phydev->state) {
1213	case PHY_DOWN:
1214	case PHY_READY:
1215		break;
1216	case PHY_UP:
1217		needs_aneg = true;
1218
1219		break;
1220	case PHY_NOLINK:
1221	case PHY_RUNNING:
1222		err = phy_check_link_status(phydev);
1223		break;
1224	case PHY_CABLETEST:
1225		err = phydev->drv->cable_test_get_status(phydev, &finished);
1226		if (err) {
1227			phy_abort_cable_test(phydev);
1228			netif_testing_off(dev);
1229			needs_aneg = true;
1230			phydev->state = PHY_UP;
1231			break;
1232		}
1233
1234		if (finished) {
1235			ethnl_cable_test_finished(phydev);
1236			netif_testing_off(dev);
1237			needs_aneg = true;
1238			phydev->state = PHY_UP;
1239		}
1240		break;
1241	case PHY_HALTED:
1242		if (phydev->link) {
1243			phydev->link = 0;
1244			phy_link_down(phydev);
1245		}
1246		do_suspend = true;
1247		break;
1248	}
1249
1250	mutex_unlock(&phydev->lock);
1251
1252	if (needs_aneg)
1253		err = phy_start_aneg(phydev);
1254	else if (do_suspend)
1255		phy_suspend(phydev);
1256
1257	if (err < 0)
1258		phy_error(phydev);
1259
1260	phy_process_state_change(phydev, old_state);
1261
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().
1265	 *
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.
1269	 */
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);
1274}
1275
1276/**
1277 * phy_mac_interrupt - MAC says the link has changed
1278 * @phydev: phy_device struct with changed link
1279 *
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.
1282 */
1283void phy_mac_interrupt(struct phy_device *phydev)
1284{
1285	/* Trigger a state machine change */
1286	phy_trigger_machine(phydev);
1287}
1288EXPORT_SYMBOL(phy_mac_interrupt);
1289
1290static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
1291{
1292	linkmode_zero(advertising);
1293
1294	if (eee_adv & MDIO_EEE_100TX)
1295		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1296				 advertising);
1297	if (eee_adv & MDIO_EEE_1000T)
1298		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1299				 advertising);
1300	if (eee_adv & MDIO_EEE_10GT)
1301		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1302				 advertising);
1303	if (eee_adv & MDIO_EEE_1000KX)
1304		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1305				 advertising);
1306	if (eee_adv & MDIO_EEE_10GKX4)
1307		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1308				 advertising);
1309	if (eee_adv & MDIO_EEE_10GKR)
1310		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1311				 advertising);
1312}
1313
1314/**
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
1318 *
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"
1322 * bit if required.
1323 */
1324int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1325{
1326	if (!phydev->drv)
1327		return -EIO;
1328
1329	/* According to 802.3az,the EEE is supported only in full duplex-mode.
1330	 */
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;
1336		int status;
1337		u32 cap;
1338
1339		/* Read phy status to properly get the right settings */
1340		status = phy_read_status(phydev);
1341		if (status)
1342			return status;
1343
1344		/* First check if the EEE ability is supported */
1345		eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1346		if (eee_cap <= 0)
1347			goto eee_exit_err;
1348
1349		cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1350		if (!cap)
1351			goto eee_exit_err;
1352
1353		/* Check which link settings negotiated and verify it in
1354		 * the EEE advertising registers.
1355		 */
1356		eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1357		if (eee_lp <= 0)
1358			goto eee_exit_err;
1359
1360		eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1361		if (eee_adv <= 0)
1362			goto eee_exit_err;
1363
1364		mmd_eee_adv_to_linkmode(adv, eee_adv);
1365		mmd_eee_adv_to_linkmode(lp, eee_lp);
1366		linkmode_and(common, adv, lp);
1367
1368		if (!phy_check_valid(phydev->speed, phydev->duplex, common))
1369			goto eee_exit_err;
1370
1371		if (clk_stop_enable)
1372			/* Configure the PHY to stop receiving xMII
1373			 * clock while it is signaling LPI.
1374			 */
1375			phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1376					 MDIO_PCS_CTRL1_CLKSTOP_EN);
1377
1378		return 0; /* EEE supported */
1379	}
1380eee_exit_err:
1381	return -EPROTONOSUPPORT;
1382}
1383EXPORT_SYMBOL(phy_init_eee);
1384
1385/**
1386 * phy_get_eee_err - report the EEE wake error count
1387 * @phydev: target phy_device struct
1388 *
1389 * Description: it is to report the number of time where the PHY
1390 * failed to complete its normal wake sequence.
1391 */
1392int phy_get_eee_err(struct phy_device *phydev)
1393{
1394	if (!phydev->drv)
1395		return -EIO;
1396
1397	return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1398}
1399EXPORT_SYMBOL(phy_get_eee_err);
1400
1401/**
1402 * phy_ethtool_get_eee - get EEE supported and status
1403 * @phydev: target phy_device struct
1404 * @data: ethtool_eee data
1405 *
1406 * Description: it reportes the Supported/Advertisement/LP Advertisement
1407 * capabilities.
1408 */
1409int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1410{
1411	int val;
1412
1413	if (!phydev->drv)
1414		return -EIO;
1415
1416	/* Get Supported EEE */
1417	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1418	if (val < 0)
1419		return val;
1420	data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1421
1422	/* Get advertisement EEE */
1423	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1424	if (val < 0)
1425		return val;
1426	data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1427	data->eee_enabled = !!data->advertised;
1428
1429	/* Get LP advertisement EEE */
1430	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1431	if (val < 0)
1432		return val;
1433	data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1434
1435	data->eee_active = !!(data->advertised & data->lp_advertised);
1436
1437	return 0;
1438}
1439EXPORT_SYMBOL(phy_ethtool_get_eee);
1440
1441/**
1442 * phy_ethtool_set_eee - set EEE supported and status
1443 * @phydev: target phy_device struct
1444 * @data: ethtool_eee data
1445 *
1446 * Description: it is to program the Advertisement EEE register.
1447 */
1448int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1449{
1450	int cap, old_adv, adv = 0, ret;
1451
1452	if (!phydev->drv)
1453		return -EIO;
1454
1455	/* Get Supported EEE */
1456	cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1457	if (cap < 0)
1458		return cap;
1459
1460	old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1461	if (old_adv < 0)
1462		return old_adv;
1463
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;
1469	}
1470
1471	if (old_adv != adv) {
1472		ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1473		if (ret < 0)
1474			return ret;
1475
1476		/* Restart autonegotiation so the new modes get sent to the
1477		 * link partner.
1478		 */
1479		if (phydev->autoneg == AUTONEG_ENABLE) {
1480			ret = phy_restart_aneg(phydev);
1481			if (ret < 0)
1482				return ret;
1483		}
1484	}
1485
1486	return 0;
1487}
1488EXPORT_SYMBOL(phy_ethtool_set_eee);
1489
1490/**
1491 * phy_ethtool_set_wol - Configure Wake On LAN
1492 *
1493 * @phydev: target phy_device struct
1494 * @wol: Configuration requested
1495 */
1496int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1497{
1498	if (phydev->drv && phydev->drv->set_wol)
1499		return phydev->drv->set_wol(phydev, wol);
1500
1501	return -EOPNOTSUPP;
1502}
1503EXPORT_SYMBOL(phy_ethtool_set_wol);
1504
1505/**
1506 * phy_ethtool_get_wol - Get the current Wake On LAN configuration
1507 *
1508 * @phydev: target phy_device struct
1509 * @wol: Store the current configuration here
1510 */
1511void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1512{
1513	if (phydev->drv && phydev->drv->get_wol)
1514		phydev->drv->get_wol(phydev, wol);
1515}
1516EXPORT_SYMBOL(phy_ethtool_get_wol);
1517
1518int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1519				   struct ethtool_link_ksettings *cmd)
1520{
1521	struct phy_device *phydev = ndev->phydev;
1522
1523	if (!phydev)
1524		return -ENODEV;
1525
1526	phy_ethtool_ksettings_get(phydev, cmd);
1527
1528	return 0;
1529}
1530EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1531
1532int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1533				   const struct ethtool_link_ksettings *cmd)
1534{
1535	struct phy_device *phydev = ndev->phydev;
1536
1537	if (!phydev)
1538		return -ENODEV;
1539
1540	return phy_ethtool_ksettings_set(phydev, cmd);
1541}
1542EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1543
1544/**
1545 * phy_ethtool_nway_reset - Restart auto negotiation
1546 * @ndev: Network device to restart autoneg for
1547 */
1548int phy_ethtool_nway_reset(struct net_device *ndev)
1549{
1550	struct phy_device *phydev = ndev->phydev;
1551
1552	if (!phydev)
1553		return -ENODEV;
1554
1555	if (!phydev->drv)
1556		return -EIO;
1557
1558	return phy_restart_aneg(phydev);
1559}
1560EXPORT_SYMBOL(phy_ethtool_nway_reset);
1561