xref: /linux/drivers/net/phy/phy.c (revision 6aac2aa2dfae38b60f22c3dfe4103ceefbe2d761)
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 <linux/suspend.h>
35 #include <net/netlink.h>
36 #include <net/genetlink.h>
37 #include <net/sock.h>
38 
39 #include "phylib-internal.h"
40 #include "phy-caps.h"
41 
42 #define PHY_STATE_TIME	HZ
43 
44 #define PHY_STATE_STR(_state)			\
45 	case PHY_##_state:			\
46 		return __stringify(_state);	\
47 
48 static const char *phy_state_to_str(enum phy_state st)
49 {
50 	switch (st) {
51 	PHY_STATE_STR(DOWN)
52 	PHY_STATE_STR(READY)
53 	PHY_STATE_STR(UP)
54 	PHY_STATE_STR(RUNNING)
55 	PHY_STATE_STR(NOLINK)
56 	PHY_STATE_STR(CABLETEST)
57 	PHY_STATE_STR(HALTED)
58 	PHY_STATE_STR(ERROR)
59 	}
60 
61 	return NULL;
62 }
63 
64 static void phy_process_state_change(struct phy_device *phydev,
65 				     enum phy_state old_state)
66 {
67 	if (old_state != phydev->state) {
68 		phydev_dbg(phydev, "PHY state change %s -> %s\n",
69 			   phy_state_to_str(old_state),
70 			   phy_state_to_str(phydev->state));
71 		if (phydev->drv && phydev->drv->link_change_notify)
72 			phydev->drv->link_change_notify(phydev);
73 	}
74 }
75 
76 static void phy_link_up(struct phy_device *phydev)
77 {
78 	phydev->phy_link_change(phydev, true);
79 	phy_led_trigger_change_speed(phydev);
80 }
81 
82 static void phy_link_down(struct phy_device *phydev)
83 {
84 	phydev->phy_link_change(phydev, false);
85 	phy_led_trigger_change_speed(phydev);
86 	WRITE_ONCE(phydev->link_down_events, phydev->link_down_events + 1);
87 }
88 
89 static const char *phy_pause_str(struct phy_device *phydev)
90 {
91 	bool local_pause, local_asym_pause;
92 
93 	if (phydev->autoneg == AUTONEG_DISABLE)
94 		goto no_pause;
95 
96 	local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
97 					phydev->advertising);
98 	local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
99 					     phydev->advertising);
100 
101 	if (local_pause && phydev->pause)
102 		return "rx/tx";
103 
104 	if (local_asym_pause && phydev->asym_pause) {
105 		if (local_pause)
106 			return "rx";
107 		if (phydev->pause)
108 			return "tx";
109 	}
110 
111 no_pause:
112 	return "off";
113 }
114 
115 /**
116  * phy_print_status - Convenience function to print out the current phy status
117  * @phydev: the phy_device struct
118  */
119 void phy_print_status(struct phy_device *phydev)
120 {
121 	if (phydev->link) {
122 		netdev_info(phydev->attached_dev,
123 			"Link is Up - %s/%s %s- flow control %s\n",
124 			phy_speed_to_str(phydev->speed),
125 			phy_duplex_to_str(phydev->duplex),
126 			phydev->downshifted_rate ? "(downshifted) " : "",
127 			phy_pause_str(phydev));
128 	} else	{
129 		netdev_info(phydev->attached_dev, "Link is Down\n");
130 	}
131 }
132 EXPORT_SYMBOL(phy_print_status);
133 
134 /**
135  * phy_get_rate_matching - determine if rate matching is supported
136  * @phydev: The phy device to return rate matching for
137  * @iface: The interface mode to use
138  *
139  * This determines the type of rate matching (if any) that @phy supports
140  * using @iface. @iface may be %PHY_INTERFACE_MODE_NA to determine if any
141  * interface supports rate matching.
142  *
143  * Return: The type of rate matching @phy supports for @iface, or
144  *         %RATE_MATCH_NONE.
145  */
146 int phy_get_rate_matching(struct phy_device *phydev,
147 			  phy_interface_t iface)
148 {
149 	int ret = RATE_MATCH_NONE;
150 
151 	if (phydev->drv->get_rate_matching) {
152 		mutex_lock(&phydev->lock);
153 		ret = phydev->drv->get_rate_matching(phydev, iface);
154 		mutex_unlock(&phydev->lock);
155 	}
156 
157 	return ret;
158 }
159 EXPORT_SYMBOL_GPL(phy_get_rate_matching);
160 
161 /**
162  * phy_config_interrupt - configure the PHY device for the requested interrupts
163  * @phydev: the phy_device struct
164  * @interrupts: interrupt flags to configure for this @phydev
165  *
166  * Returns 0 on success or < 0 on error.
167  */
168 static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
169 {
170 	phydev->interrupts = interrupts ? 1 : 0;
171 	if (phydev->drv->config_intr)
172 		return phydev->drv->config_intr(phydev);
173 
174 	return 0;
175 }
176 
177 /**
178  * phy_restart_aneg - restart auto-negotiation
179  * @phydev: target phy_device struct
180  *
181  * Restart the autonegotiation on @phydev.  Returns >= 0 on success or
182  * negative errno on error.
183  */
184 int phy_restart_aneg(struct phy_device *phydev)
185 {
186 	int ret;
187 
188 	if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
189 		ret = genphy_c45_restart_aneg(phydev);
190 	else
191 		ret = genphy_restart_aneg(phydev);
192 
193 	return ret;
194 }
195 EXPORT_SYMBOL_GPL(phy_restart_aneg);
196 
197 /**
198  * phy_aneg_done - return auto-negotiation status
199  * @phydev: target phy_device struct
200  *
201  * Description: Return the auto-negotiation status from this @phydev
202  * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
203  * is still pending.
204  */
205 int phy_aneg_done(struct phy_device *phydev)
206 {
207 	if (phydev->drv && phydev->drv->aneg_done)
208 		return phydev->drv->aneg_done(phydev);
209 	else if (phydev->is_c45)
210 		return genphy_c45_aneg_done(phydev);
211 	else
212 		return genphy_aneg_done(phydev);
213 }
214 EXPORT_SYMBOL(phy_aneg_done);
215 
216 /**
217  * phy_supported_speeds - return all speeds currently supported by a phy device
218  * @phy: The phy device to return supported speeds of.
219  * @speeds: buffer to store supported speeds in.
220  * @size:   size of speeds buffer.
221  *
222  * Description: Returns the number of supported speeds, and fills the speeds
223  * buffer with the supported speeds. If speeds buffer is too small to contain
224  * all currently supported speeds, will return as many speeds as can fit.
225  */
226 unsigned int phy_supported_speeds(struct phy_device *phy,
227 				  unsigned int *speeds,
228 				  unsigned int size)
229 {
230 	return phy_caps_speeds(speeds, size, phy->supported);
231 }
232 
233 /**
234  * phy_check_valid - check if there is a valid PHY setting which matches
235  *		     speed, duplex, and feature mask
236  * @speed: speed to match
237  * @duplex: duplex to match
238  * @features: A mask of the valid settings
239  *
240  * Description: Returns true if there is a valid setting, false otherwise.
241  */
242 bool phy_check_valid(int speed, int duplex, unsigned long *features)
243 {
244 	return phy_caps_valid(speed, duplex, features);
245 }
246 EXPORT_SYMBOL(phy_check_valid);
247 
248 /**
249  * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
250  * @phydev: the target phy_device struct
251  *
252  * Description: Make sure the PHY is set to supported speeds and
253  *   duplexes.  Drop down by one in this order:  1000/FULL,
254  *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
255  */
256 static void phy_sanitize_settings(struct phy_device *phydev)
257 {
258 	const struct link_capabilities *c;
259 
260 	c = phy_caps_lookup(phydev->speed, phydev->duplex, phydev->supported,
261 			    false);
262 
263 	if (c) {
264 		phydev->speed = c->speed;
265 		phydev->duplex = c->duplex;
266 	} else {
267 		/* We failed to find anything (no supported speeds?) */
268 		phydev->speed = SPEED_UNKNOWN;
269 		phydev->duplex = DUPLEX_UNKNOWN;
270 	}
271 }
272 
273 void phy_ethtool_ksettings_get(struct phy_device *phydev,
274 			       struct ethtool_link_ksettings *cmd)
275 {
276 	mutex_lock(&phydev->lock);
277 	linkmode_copy(cmd->link_modes.supported, phydev->supported);
278 	linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
279 	linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
280 
281 	cmd->base.speed = phydev->speed;
282 	cmd->base.duplex = phydev->duplex;
283 	cmd->base.master_slave_cfg = phydev->master_slave_get;
284 	cmd->base.master_slave_state = phydev->master_slave_state;
285 	cmd->base.rate_matching = phydev->rate_matching;
286 	if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
287 		cmd->base.port = PORT_BNC;
288 	else
289 		cmd->base.port = phydev->port;
290 	cmd->base.transceiver = phydev->is_internal ?
291 				XCVR_INTERNAL : XCVR_EXTERNAL;
292 	cmd->base.phy_address = phydev->mdio.addr;
293 	cmd->base.autoneg = phydev->autoneg;
294 	cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
295 	cmd->base.eth_tp_mdix = phydev->mdix;
296 	mutex_unlock(&phydev->lock);
297 }
298 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
299 
300 /**
301  * phy_mii_ioctl - generic PHY MII ioctl interface
302  * @phydev: the phy_device struct
303  * @ifr: &struct ifreq for socket ioctl's
304  * @cmd: ioctl cmd to execute
305  *
306  * Note that this function is currently incompatible with the
307  * PHYCONTROL layer.  It changes registers without regard to
308  * current state.  Use at own risk.
309  */
310 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
311 {
312 	struct mii_ioctl_data *mii_data = if_mii(ifr);
313 	struct kernel_hwtstamp_config kernel_cfg;
314 	struct netlink_ext_ack extack = {};
315 	u16 val = mii_data->val_in;
316 	bool change_autoneg = false;
317 	struct hwtstamp_config cfg;
318 	int prtad, devad;
319 	int ret;
320 
321 	switch (cmd) {
322 	case SIOCGMIIPHY:
323 		mii_data->phy_id = phydev->mdio.addr;
324 		fallthrough;
325 
326 	case SIOCGMIIREG:
327 		if (mdio_phy_id_is_c45(mii_data->phy_id)) {
328 			prtad = mdio_phy_id_prtad(mii_data->phy_id);
329 			devad = mdio_phy_id_devad(mii_data->phy_id);
330 			ret = mdiobus_c45_read(phydev->mdio.bus, prtad, devad,
331 					       mii_data->reg_num);
332 
333 		} else {
334 			ret = mdiobus_read(phydev->mdio.bus, mii_data->phy_id,
335 					   mii_data->reg_num);
336 		}
337 
338 		if (ret < 0)
339 			return ret;
340 
341 		mii_data->val_out = ret;
342 
343 		return 0;
344 
345 	case SIOCSMIIREG:
346 		if (mdio_phy_id_is_c45(mii_data->phy_id)) {
347 			prtad = mdio_phy_id_prtad(mii_data->phy_id);
348 			devad = mdio_phy_id_devad(mii_data->phy_id);
349 		} else {
350 			prtad = mii_data->phy_id;
351 			devad = mii_data->reg_num;
352 		}
353 		if (prtad == phydev->mdio.addr) {
354 			switch (devad) {
355 			case MII_BMCR:
356 				if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
357 					if (phydev->autoneg == AUTONEG_ENABLE)
358 						change_autoneg = true;
359 					phydev->autoneg = AUTONEG_DISABLE;
360 					if (val & BMCR_FULLDPLX)
361 						phydev->duplex = DUPLEX_FULL;
362 					else
363 						phydev->duplex = DUPLEX_HALF;
364 					if (val & BMCR_SPEED1000)
365 						phydev->speed = SPEED_1000;
366 					else if (val & BMCR_SPEED100)
367 						phydev->speed = SPEED_100;
368 					else phydev->speed = SPEED_10;
369 				} else {
370 					if (phydev->autoneg == AUTONEG_DISABLE)
371 						change_autoneg = true;
372 					phydev->autoneg = AUTONEG_ENABLE;
373 				}
374 				break;
375 			case MII_ADVERTISE:
376 				mii_adv_mod_linkmode_adv_t(phydev->advertising,
377 							   val);
378 				change_autoneg = true;
379 				break;
380 			case MII_CTRL1000:
381 				mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
382 							        val);
383 				change_autoneg = true;
384 				break;
385 			default:
386 				/* do nothing */
387 				break;
388 			}
389 		}
390 
391 		if (mdio_phy_id_is_c45(mii_data->phy_id))
392 			mdiobus_c45_write(phydev->mdio.bus, prtad, devad,
393 					  mii_data->reg_num, val);
394 		else
395 			mdiobus_write(phydev->mdio.bus, prtad, devad, val);
396 
397 		if (prtad == phydev->mdio.addr &&
398 		    devad == MII_BMCR &&
399 		    val & BMCR_RESET)
400 			return phy_init_hw(phydev);
401 
402 		if (change_autoneg)
403 			return phy_start_aneg(phydev);
404 
405 		return 0;
406 
407 	case SIOCSHWTSTAMP:
408 		if (phydev->mii_ts && phydev->mii_ts->hwtstamp_set) {
409 			if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
410 				return -EFAULT;
411 
412 			hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
413 			ret = phydev->mii_ts->hwtstamp_set(phydev->mii_ts,
414 							   &kernel_cfg,
415 							   &extack);
416 			if (ret)
417 				return ret;
418 
419 			hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
420 			if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
421 				return -EFAULT;
422 
423 			return 0;
424 		}
425 		fallthrough;
426 
427 	default:
428 		return -EOPNOTSUPP;
429 	}
430 }
431 EXPORT_SYMBOL(phy_mii_ioctl);
432 
433 /**
434  * phy_do_ioctl - generic ndo_eth_ioctl implementation
435  * @dev: the net_device struct
436  * @ifr: &struct ifreq for socket ioctl's
437  * @cmd: ioctl cmd to execute
438  */
439 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
440 {
441 	if (!dev->phydev)
442 		return -ENODEV;
443 
444 	return phy_mii_ioctl(dev->phydev, ifr, cmd);
445 }
446 EXPORT_SYMBOL(phy_do_ioctl);
447 
448 /**
449  * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first
450  *
451  * @dev: the net_device struct
452  * @ifr: &struct ifreq for socket ioctl's
453  * @cmd: ioctl cmd to execute
454  *
455  * Same as phy_do_ioctl, but ensures that net_device is running before
456  * handling the ioctl.
457  */
458 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
459 {
460 	if (!netif_running(dev))
461 		return -ENODEV;
462 
463 	return phy_do_ioctl(dev, ifr, cmd);
464 }
465 EXPORT_SYMBOL(phy_do_ioctl_running);
466 
467 /**
468  * __phy_hwtstamp_get - Get hardware timestamping configuration from PHY
469  *
470  * @phydev: the PHY device structure
471  * @config: structure holding the timestamping configuration
472  *
473  * Query the PHY device for its current hardware timestamping configuration.
474  */
475 int __phy_hwtstamp_get(struct phy_device *phydev,
476 		       struct kernel_hwtstamp_config *config)
477 {
478 	if (!phydev)
479 		return -ENODEV;
480 
481 	return -EOPNOTSUPP;
482 }
483 
484 /**
485  * __phy_hwtstamp_set - Modify PHY hardware timestamping configuration
486  *
487  * @phydev: the PHY device structure
488  * @config: structure holding the timestamping configuration
489  * @extack: netlink extended ack structure, for error reporting
490  */
491 int __phy_hwtstamp_set(struct phy_device *phydev,
492 		       struct kernel_hwtstamp_config *config,
493 		       struct netlink_ext_ack *extack)
494 {
495 	if (!phydev)
496 		return -ENODEV;
497 
498 	if (phydev->mii_ts && phydev->mii_ts->hwtstamp_set)
499 		return phydev->mii_ts->hwtstamp_set(phydev->mii_ts, config,
500 						    extack);
501 
502 	return -EOPNOTSUPP;
503 }
504 
505 /**
506  * phy_queue_state_machine - Trigger the state machine to run soon
507  *
508  * @phydev: the phy_device struct
509  * @jiffies: Run the state machine after these jiffies
510  */
511 static void phy_queue_state_machine(struct phy_device *phydev,
512 				    unsigned long jiffies)
513 {
514 	mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
515 			 jiffies);
516 }
517 
518 /**
519  * phy_trigger_machine - Trigger the state machine to run now
520  *
521  * @phydev: the phy_device struct
522  */
523 void phy_trigger_machine(struct phy_device *phydev)
524 {
525 	phy_queue_state_machine(phydev, 0);
526 }
527 EXPORT_SYMBOL(phy_trigger_machine);
528 
529 static void phy_abort_cable_test(struct phy_device *phydev)
530 {
531 	int err;
532 
533 	ethnl_cable_test_finished(phydev);
534 
535 	err = phy_init_hw(phydev);
536 	if (err)
537 		phydev_err(phydev, "Error while aborting cable test");
538 }
539 
540 /**
541  * phy_ethtool_get_strings - Get the statistic counter names
542  *
543  * @phydev: the phy_device struct
544  * @data: Where to put the strings
545  */
546 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
547 {
548 	if (!phydev->drv)
549 		return -EIO;
550 
551 	mutex_lock(&phydev->lock);
552 	phydev->drv->get_strings(phydev, data);
553 	mutex_unlock(&phydev->lock);
554 
555 	return 0;
556 }
557 EXPORT_SYMBOL(phy_ethtool_get_strings);
558 
559 /**
560  * phy_ethtool_get_sset_count - Get the number of statistic counters
561  *
562  * @phydev: the phy_device struct
563  */
564 int phy_ethtool_get_sset_count(struct phy_device *phydev)
565 {
566 	int ret;
567 
568 	if (!phydev->drv)
569 		return -EIO;
570 
571 	if (phydev->drv->get_sset_count &&
572 	    phydev->drv->get_strings &&
573 	    phydev->drv->get_stats) {
574 		mutex_lock(&phydev->lock);
575 		ret = phydev->drv->get_sset_count(phydev);
576 		mutex_unlock(&phydev->lock);
577 
578 		return ret;
579 	}
580 
581 	return -EOPNOTSUPP;
582 }
583 EXPORT_SYMBOL(phy_ethtool_get_sset_count);
584 
585 /**
586  * phy_ethtool_get_stats - Get the statistic counters
587  *
588  * @phydev: the phy_device struct
589  * @stats: What counters to get
590  * @data: Where to store the counters
591  */
592 int phy_ethtool_get_stats(struct phy_device *phydev,
593 			  struct ethtool_stats *stats, u64 *data)
594 {
595 	if (!phydev->drv)
596 		return -EIO;
597 
598 	mutex_lock(&phydev->lock);
599 	phydev->drv->get_stats(phydev, stats, data);
600 	mutex_unlock(&phydev->lock);
601 
602 	return 0;
603 }
604 EXPORT_SYMBOL(phy_ethtool_get_stats);
605 
606 /**
607  * __phy_ethtool_get_phy_stats - Retrieve standardized PHY statistics
608  * @phydev: Pointer to the PHY device
609  * @phy_stats: Pointer to ethtool_eth_phy_stats structure
610  * @phydev_stats: Pointer to ethtool_phy_stats structure
611  *
612  * Fetches PHY statistics using a kernel-defined interface for consistent
613  * diagnostics. Unlike phy_ethtool_get_stats(), which allows custom stats,
614  * this function enforces a standardized format for better interoperability.
615  */
616 void __phy_ethtool_get_phy_stats(struct phy_device *phydev,
617 				 struct ethtool_eth_phy_stats *phy_stats,
618 				 struct ethtool_phy_stats *phydev_stats)
619 {
620 	if (!phydev->drv || !phydev->drv->get_phy_stats)
621 		return;
622 
623 	mutex_lock(&phydev->lock);
624 	phydev->drv->get_phy_stats(phydev, phy_stats, phydev_stats);
625 	mutex_unlock(&phydev->lock);
626 }
627 
628 /**
629  * __phy_ethtool_get_link_ext_stats - Retrieve extended link statistics for a PHY
630  * @phydev: Pointer to the PHY device
631  * @link_stats: Pointer to the structure to store extended link statistics
632  *
633  * Populates the ethtool_link_ext_stats structure with link down event counts
634  * and additional driver-specific link statistics, if available.
635  */
636 void __phy_ethtool_get_link_ext_stats(struct phy_device *phydev,
637 				      struct ethtool_link_ext_stats *link_stats)
638 {
639 	link_stats->link_down_events = READ_ONCE(phydev->link_down_events);
640 
641 	if (!phydev->drv || !phydev->drv->get_link_stats)
642 		return;
643 
644 	mutex_lock(&phydev->lock);
645 	phydev->drv->get_link_stats(phydev, link_stats);
646 	mutex_unlock(&phydev->lock);
647 }
648 
649 /**
650  * phy_ethtool_get_plca_cfg - Get PLCA RS configuration
651  * @phydev: the phy_device struct
652  * @plca_cfg: where to store the retrieved configuration
653  *
654  * Retrieve the PLCA configuration from the PHY. Return 0 on success or a
655  * negative value if an error occurred.
656  */
657 int phy_ethtool_get_plca_cfg(struct phy_device *phydev,
658 			     struct phy_plca_cfg *plca_cfg)
659 {
660 	int ret;
661 
662 	if (!phydev->drv) {
663 		ret = -EIO;
664 		goto out;
665 	}
666 
667 	if (!phydev->drv->get_plca_cfg) {
668 		ret = -EOPNOTSUPP;
669 		goto out;
670 	}
671 
672 	mutex_lock(&phydev->lock);
673 	ret = phydev->drv->get_plca_cfg(phydev, plca_cfg);
674 
675 	mutex_unlock(&phydev->lock);
676 out:
677 	return ret;
678 }
679 
680 /**
681  * plca_check_valid - Check PLCA configuration before enabling
682  * @phydev: the phy_device struct
683  * @plca_cfg: current PLCA configuration
684  * @extack: extack for reporting useful error messages
685  *
686  * Checks whether the PLCA and PHY configuration are consistent and it is safe
687  * to enable PLCA. Returns 0 on success or a negative value if the PLCA or PHY
688  * configuration is not consistent.
689  */
690 static int plca_check_valid(struct phy_device *phydev,
691 			    const struct phy_plca_cfg *plca_cfg,
692 			    struct netlink_ext_ack *extack)
693 {
694 	int ret = 0;
695 
696 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT,
697 			       phydev->advertising)) {
698 		ret = -EOPNOTSUPP;
699 		NL_SET_ERR_MSG(extack,
700 			       "Point to Multi-Point mode is not enabled");
701 	} else if (plca_cfg->node_id >= 255) {
702 		NL_SET_ERR_MSG(extack, "PLCA node ID is not set");
703 		ret = -EINVAL;
704 	}
705 
706 	return ret;
707 }
708 
709 /**
710  * phy_ethtool_set_plca_cfg - Set PLCA RS configuration
711  * @phydev: the phy_device struct
712  * @plca_cfg: new PLCA configuration to apply
713  * @extack: extack for reporting useful error messages
714  *
715  * Sets the PLCA configuration in the PHY. Return 0 on success or a
716  * negative value if an error occurred.
717  */
718 int phy_ethtool_set_plca_cfg(struct phy_device *phydev,
719 			     const struct phy_plca_cfg *plca_cfg,
720 			     struct netlink_ext_ack *extack)
721 {
722 	struct phy_plca_cfg *curr_plca_cfg;
723 	int ret;
724 
725 	if (!phydev->drv) {
726 		ret = -EIO;
727 		goto out;
728 	}
729 
730 	if (!phydev->drv->set_plca_cfg ||
731 	    !phydev->drv->get_plca_cfg) {
732 		ret = -EOPNOTSUPP;
733 		goto out;
734 	}
735 
736 	curr_plca_cfg = kmalloc(sizeof(*curr_plca_cfg), GFP_KERNEL);
737 	if (!curr_plca_cfg) {
738 		ret = -ENOMEM;
739 		goto out;
740 	}
741 
742 	mutex_lock(&phydev->lock);
743 
744 	ret = phydev->drv->get_plca_cfg(phydev, curr_plca_cfg);
745 	if (ret)
746 		goto out_drv;
747 
748 	if (curr_plca_cfg->enabled < 0 && plca_cfg->enabled >= 0) {
749 		NL_SET_ERR_MSG(extack,
750 			       "PHY does not support changing the PLCA 'enable' attribute");
751 		ret = -EINVAL;
752 		goto out_drv;
753 	}
754 
755 	if (curr_plca_cfg->node_id < 0 && plca_cfg->node_id >= 0) {
756 		NL_SET_ERR_MSG(extack,
757 			       "PHY does not support changing the PLCA 'local node ID' attribute");
758 		ret = -EINVAL;
759 		goto out_drv;
760 	}
761 
762 	if (curr_plca_cfg->node_cnt < 0 && plca_cfg->node_cnt >= 0) {
763 		NL_SET_ERR_MSG(extack,
764 			       "PHY does not support changing the PLCA 'node count' attribute");
765 		ret = -EINVAL;
766 		goto out_drv;
767 	}
768 
769 	if (curr_plca_cfg->to_tmr < 0 && plca_cfg->to_tmr >= 0) {
770 		NL_SET_ERR_MSG(extack,
771 			       "PHY does not support changing the PLCA 'TO timer' attribute");
772 		ret = -EINVAL;
773 		goto out_drv;
774 	}
775 
776 	if (curr_plca_cfg->burst_cnt < 0 && plca_cfg->burst_cnt >= 0) {
777 		NL_SET_ERR_MSG(extack,
778 			       "PHY does not support changing the PLCA 'burst count' attribute");
779 		ret = -EINVAL;
780 		goto out_drv;
781 	}
782 
783 	if (curr_plca_cfg->burst_tmr < 0 && plca_cfg->burst_tmr >= 0) {
784 		NL_SET_ERR_MSG(extack,
785 			       "PHY does not support changing the PLCA 'burst timer' attribute");
786 		ret = -EINVAL;
787 		goto out_drv;
788 	}
789 
790 	// if enabling PLCA, perform a few sanity checks
791 	if (plca_cfg->enabled > 0) {
792 		// allow setting node_id concurrently with enabled
793 		if (plca_cfg->node_id >= 0)
794 			curr_plca_cfg->node_id = plca_cfg->node_id;
795 
796 		ret = plca_check_valid(phydev, curr_plca_cfg, extack);
797 		if (ret)
798 			goto out_drv;
799 	}
800 
801 	ret = phydev->drv->set_plca_cfg(phydev, plca_cfg);
802 
803 out_drv:
804 	kfree(curr_plca_cfg);
805 	mutex_unlock(&phydev->lock);
806 out:
807 	return ret;
808 }
809 
810 /**
811  * phy_ethtool_get_plca_status - Get PLCA RS status information
812  * @phydev: the phy_device struct
813  * @plca_st: where to store the retrieved status information
814  *
815  * Retrieve the PLCA status information from the PHY. Return 0 on success or a
816  * negative value if an error occurred.
817  */
818 int phy_ethtool_get_plca_status(struct phy_device *phydev,
819 				struct phy_plca_status *plca_st)
820 {
821 	int ret;
822 
823 	if (!phydev->drv) {
824 		ret = -EIO;
825 		goto out;
826 	}
827 
828 	if (!phydev->drv->get_plca_status) {
829 		ret = -EOPNOTSUPP;
830 		goto out;
831 	}
832 
833 	mutex_lock(&phydev->lock);
834 	ret = phydev->drv->get_plca_status(phydev, plca_st);
835 
836 	mutex_unlock(&phydev->lock);
837 out:
838 	return ret;
839 }
840 
841 /**
842  * phy_start_cable_test - Start a cable test
843  *
844  * @phydev: the phy_device struct
845  * @extack: extack for reporting useful error messages
846  */
847 int phy_start_cable_test(struct phy_device *phydev,
848 			 struct netlink_ext_ack *extack)
849 {
850 	struct net_device *dev = phydev->attached_dev;
851 	int err = -ENOMEM;
852 
853 	if (!(phydev->drv &&
854 	      phydev->drv->cable_test_start &&
855 	      phydev->drv->cable_test_get_status)) {
856 		NL_SET_ERR_MSG(extack,
857 			       "PHY driver does not support cable testing");
858 		return -EOPNOTSUPP;
859 	}
860 
861 	mutex_lock(&phydev->lock);
862 	if (phydev->state == PHY_CABLETEST) {
863 		NL_SET_ERR_MSG(extack,
864 			       "PHY already performing a test");
865 		err = -EBUSY;
866 		goto out;
867 	}
868 
869 	if (phydev->state < PHY_UP ||
870 	    phydev->state > PHY_CABLETEST) {
871 		NL_SET_ERR_MSG(extack,
872 			       "PHY not configured. Try setting interface up");
873 		err = -EBUSY;
874 		goto out;
875 	}
876 
877 	err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
878 	if (err)
879 		goto out;
880 
881 	/* Mark the carrier down until the test is complete */
882 	phy_link_down(phydev);
883 
884 	netif_testing_on(dev);
885 	err = phydev->drv->cable_test_start(phydev);
886 	if (err) {
887 		netif_testing_off(dev);
888 		phy_link_up(phydev);
889 		goto out_free;
890 	}
891 
892 	phydev->state = PHY_CABLETEST;
893 
894 	if (phy_polling_mode(phydev))
895 		phy_trigger_machine(phydev);
896 
897 	mutex_unlock(&phydev->lock);
898 
899 	return 0;
900 
901 out_free:
902 	ethnl_cable_test_free(phydev);
903 out:
904 	mutex_unlock(&phydev->lock);
905 
906 	return err;
907 }
908 EXPORT_SYMBOL(phy_start_cable_test);
909 
910 /**
911  * phy_start_cable_test_tdr - Start a raw TDR cable test
912  *
913  * @phydev: the phy_device struct
914  * @extack: extack for reporting useful error messages
915  * @config: Configuration of the test to run
916  */
917 int phy_start_cable_test_tdr(struct phy_device *phydev,
918 			     struct netlink_ext_ack *extack,
919 			     const struct phy_tdr_config *config)
920 {
921 	struct net_device *dev = phydev->attached_dev;
922 	int err = -ENOMEM;
923 
924 	if (!(phydev->drv &&
925 	      phydev->drv->cable_test_tdr_start &&
926 	      phydev->drv->cable_test_get_status)) {
927 		NL_SET_ERR_MSG(extack,
928 			       "PHY driver does not support cable test TDR");
929 		return -EOPNOTSUPP;
930 	}
931 
932 	mutex_lock(&phydev->lock);
933 	if (phydev->state == PHY_CABLETEST) {
934 		NL_SET_ERR_MSG(extack,
935 			       "PHY already performing a test");
936 		err = -EBUSY;
937 		goto out;
938 	}
939 
940 	if (phydev->state < PHY_UP ||
941 	    phydev->state > PHY_CABLETEST) {
942 		NL_SET_ERR_MSG(extack,
943 			       "PHY not configured. Try setting interface up");
944 		err = -EBUSY;
945 		goto out;
946 	}
947 
948 	err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
949 	if (err)
950 		goto out;
951 
952 	/* Mark the carrier down until the test is complete */
953 	phy_link_down(phydev);
954 
955 	netif_testing_on(dev);
956 	err = phydev->drv->cable_test_tdr_start(phydev, config);
957 	if (err) {
958 		netif_testing_off(dev);
959 		phy_link_up(phydev);
960 		goto out_free;
961 	}
962 
963 	phydev->state = PHY_CABLETEST;
964 
965 	if (phy_polling_mode(phydev))
966 		phy_trigger_machine(phydev);
967 
968 	mutex_unlock(&phydev->lock);
969 
970 	return 0;
971 
972 out_free:
973 	ethnl_cable_test_free(phydev);
974 out:
975 	mutex_unlock(&phydev->lock);
976 
977 	return err;
978 }
979 EXPORT_SYMBOL(phy_start_cable_test_tdr);
980 
981 int phy_config_aneg(struct phy_device *phydev)
982 {
983 	if (phydev->drv->config_aneg)
984 		return phydev->drv->config_aneg(phydev);
985 
986 	/* Clause 45 PHYs that don't implement Clause 22 registers are not
987 	 * allowed to call genphy_config_aneg()
988 	 */
989 	if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
990 		return genphy_c45_config_aneg(phydev);
991 
992 	return genphy_config_aneg(phydev);
993 }
994 EXPORT_SYMBOL(phy_config_aneg);
995 
996 /**
997  * phy_check_link_status - check link status and set state accordingly
998  * @phydev: the phy_device struct
999  *
1000  * Description: Check for link and whether autoneg was triggered / is running
1001  * and set state accordingly
1002  */
1003 static int phy_check_link_status(struct phy_device *phydev)
1004 {
1005 	int err;
1006 
1007 	lockdep_assert_held(&phydev->lock);
1008 
1009 	/* Keep previous state if loopback is enabled because some PHYs
1010 	 * report that Link is Down when loopback is enabled.
1011 	 */
1012 	if (phydev->loopback_enabled)
1013 		return 0;
1014 
1015 	err = phy_read_status(phydev);
1016 	if (err)
1017 		return err;
1018 
1019 	if (phydev->link && phydev->state != PHY_RUNNING) {
1020 		phy_check_downshift(phydev);
1021 		phydev->state = PHY_RUNNING;
1022 		err = genphy_c45_eee_is_active(phydev, NULL);
1023 		phydev->eee_active = err > 0;
1024 		phydev->enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled &&
1025 					phydev->eee_active;
1026 
1027 		phy_link_up(phydev);
1028 	} else if (!phydev->link && phydev->state != PHY_NOLINK) {
1029 		phydev->state = PHY_NOLINK;
1030 		phydev->eee_active = false;
1031 		phydev->enable_tx_lpi = false;
1032 		phy_link_down(phydev);
1033 	}
1034 
1035 	return 0;
1036 }
1037 
1038 /**
1039  * phy_inband_caps - query which in-band signalling modes are supported
1040  * @phydev: a pointer to a &struct phy_device
1041  * @interface: the interface mode for the PHY
1042  *
1043  * Returns zero if it is unknown what in-band signalling is supported by the
1044  * PHY (e.g. because the PHY driver doesn't implement the method.) Otherwise,
1045  * returns a bit mask of the LINK_INBAND_* values from
1046  * &enum link_inband_signalling to describe which inband modes are supported
1047  * by the PHY for this interface mode.
1048  */
1049 unsigned int phy_inband_caps(struct phy_device *phydev,
1050 			     phy_interface_t interface)
1051 {
1052 	if (phydev->drv && phydev->drv->inband_caps)
1053 		return phydev->drv->inband_caps(phydev, interface);
1054 
1055 	return 0;
1056 }
1057 EXPORT_SYMBOL_GPL(phy_inband_caps);
1058 
1059 /**
1060  * phy_config_inband - configure the desired PHY in-band mode
1061  * @phydev: the phy_device struct
1062  * @modes: in-band modes to configure
1063  *
1064  * Description: disables, enables or enables-with-bypass in-band signalling
1065  *   between the PHY and host system.
1066  *
1067  * Returns: zero on success, or negative errno value.
1068  */
1069 int phy_config_inband(struct phy_device *phydev, unsigned int modes)
1070 {
1071 	lockdep_assert_held(&phydev->lock);
1072 
1073 	if (!!(modes & LINK_INBAND_DISABLE) +
1074 	    !!(modes & LINK_INBAND_ENABLE) +
1075 	    !!(modes & LINK_INBAND_BYPASS) != 1)
1076 		return -EINVAL;
1077 
1078 	if (!phydev->drv)
1079 		return -EIO;
1080 	else if (!phydev->drv->config_inband)
1081 		return -EOPNOTSUPP;
1082 
1083 	return phydev->drv->config_inband(phydev, modes);
1084 }
1085 EXPORT_SYMBOL(phy_config_inband);
1086 
1087 /**
1088  * _phy_start_aneg - start auto-negotiation for this PHY device
1089  * @phydev: the phy_device struct
1090  *
1091  * Description: Sanitizes the settings (if we're not autonegotiating
1092  *   them), and then calls the driver's config_aneg function.
1093  *   If the PHYCONTROL Layer is operating, we change the state to
1094  *   reflect the beginning of Auto-negotiation or forcing.
1095  */
1096 int _phy_start_aneg(struct phy_device *phydev)
1097 {
1098 	int err;
1099 
1100 	lockdep_assert_held(&phydev->lock);
1101 
1102 	if (!phydev->drv)
1103 		return -EIO;
1104 
1105 	if (AUTONEG_DISABLE == phydev->autoneg)
1106 		phy_sanitize_settings(phydev);
1107 
1108 	err = phy_config_aneg(phydev);
1109 	if (err < 0)
1110 		return err;
1111 
1112 	if (phy_is_started(phydev))
1113 		err = phy_check_link_status(phydev);
1114 
1115 	return err;
1116 }
1117 EXPORT_SYMBOL(_phy_start_aneg);
1118 
1119 /**
1120  * phy_start_aneg - start auto-negotiation for this PHY device
1121  * @phydev: the phy_device struct
1122  *
1123  * Description: Sanitizes the settings (if we're not autonegotiating
1124  *   them), and then calls the driver's config_aneg function.
1125  *   If the PHYCONTROL Layer is operating, we change the state to
1126  *   reflect the beginning of Auto-negotiation or forcing.
1127  */
1128 int phy_start_aneg(struct phy_device *phydev)
1129 {
1130 	int err;
1131 
1132 	mutex_lock(&phydev->lock);
1133 	err = _phy_start_aneg(phydev);
1134 	mutex_unlock(&phydev->lock);
1135 
1136 	return err;
1137 }
1138 EXPORT_SYMBOL(phy_start_aneg);
1139 
1140 static int phy_poll_aneg_done(struct phy_device *phydev)
1141 {
1142 	unsigned int retries = 100;
1143 	int ret;
1144 
1145 	do {
1146 		msleep(100);
1147 		ret = phy_aneg_done(phydev);
1148 	} while (!ret && --retries);
1149 
1150 	if (!ret)
1151 		return -ETIMEDOUT;
1152 
1153 	return ret < 0 ? ret : 0;
1154 }
1155 
1156 int phy_ethtool_ksettings_set(struct phy_device *phydev,
1157 			      const struct ethtool_link_ksettings *cmd)
1158 {
1159 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
1160 	u8 autoneg = cmd->base.autoneg;
1161 	u8 duplex = cmd->base.duplex;
1162 	u32 speed = cmd->base.speed;
1163 
1164 	if (cmd->base.phy_address != phydev->mdio.addr)
1165 		return -EINVAL;
1166 
1167 	linkmode_copy(advertising, cmd->link_modes.advertising);
1168 
1169 	/* We make sure that we don't pass unsupported values in to the PHY */
1170 	linkmode_and(advertising, advertising, phydev->supported);
1171 
1172 	/* Verify the settings we care about. */
1173 	if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
1174 		return -EINVAL;
1175 
1176 	if (autoneg == AUTONEG_ENABLE &&
1177 	    (linkmode_empty(advertising) ||
1178 	     !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1179 				phydev->supported)))
1180 		return -EINVAL;
1181 
1182 	if (autoneg == AUTONEG_DISABLE &&
1183 	    ((speed != SPEED_1000 &&
1184 	      speed != SPEED_100 &&
1185 	      speed != SPEED_10) ||
1186 	     (duplex != DUPLEX_HALF &&
1187 	      duplex != DUPLEX_FULL)))
1188 		return -EINVAL;
1189 
1190 	mutex_lock(&phydev->lock);
1191 	phydev->autoneg = autoneg;
1192 
1193 	if (autoneg == AUTONEG_DISABLE) {
1194 		phydev->speed = speed;
1195 		phydev->duplex = duplex;
1196 	}
1197 
1198 	linkmode_copy(phydev->advertising, advertising);
1199 
1200 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1201 			 phydev->advertising, autoneg == AUTONEG_ENABLE);
1202 
1203 	phydev->master_slave_set = cmd->base.master_slave_cfg;
1204 	phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
1205 
1206 	/* Restart the PHY */
1207 	if (phy_is_started(phydev)) {
1208 		phydev->state = PHY_UP;
1209 		phy_trigger_machine(phydev);
1210 	} else {
1211 		_phy_start_aneg(phydev);
1212 	}
1213 
1214 	mutex_unlock(&phydev->lock);
1215 	return 0;
1216 }
1217 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
1218 
1219 /**
1220  * phy_speed_down - set speed to lowest speed supported by both link partners
1221  * @phydev: the phy_device struct
1222  * @sync: perform action synchronously
1223  *
1224  * Description: Typically used to save energy when waiting for a WoL packet
1225  *
1226  * WARNING: Setting sync to false may cause the system being unable to suspend
1227  * in case the PHY generates an interrupt when finishing the autonegotiation.
1228  * This interrupt may wake up the system immediately after suspend.
1229  * Therefore use sync = false only if you're sure it's safe with the respective
1230  * network chip.
1231  */
1232 int phy_speed_down(struct phy_device *phydev, bool sync)
1233 {
1234 	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
1235 	int ret = 0;
1236 
1237 	mutex_lock(&phydev->lock);
1238 
1239 	if (phydev->autoneg != AUTONEG_ENABLE)
1240 		goto out;
1241 
1242 	linkmode_copy(adv_tmp, phydev->advertising);
1243 
1244 	ret = phy_speed_down_core(phydev);
1245 	if (ret)
1246 		goto out;
1247 
1248 	linkmode_copy(phydev->adv_old, adv_tmp);
1249 
1250 	if (linkmode_equal(phydev->advertising, adv_tmp)) {
1251 		ret = 0;
1252 		goto out;
1253 	}
1254 
1255 	ret = phy_config_aneg(phydev);
1256 	if (ret)
1257 		goto out;
1258 
1259 	ret = sync ? phy_poll_aneg_done(phydev) : 0;
1260 out:
1261 	mutex_unlock(&phydev->lock);
1262 
1263 	return ret;
1264 }
1265 EXPORT_SYMBOL_GPL(phy_speed_down);
1266 
1267 /**
1268  * phy_speed_up - (re)set advertised speeds to all supported speeds
1269  * @phydev: the phy_device struct
1270  *
1271  * Description: Used to revert the effect of phy_speed_down
1272  */
1273 int phy_speed_up(struct phy_device *phydev)
1274 {
1275 	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
1276 	int ret = 0;
1277 
1278 	mutex_lock(&phydev->lock);
1279 
1280 	if (phydev->autoneg != AUTONEG_ENABLE)
1281 		goto out;
1282 
1283 	if (linkmode_empty(phydev->adv_old))
1284 		goto out;
1285 
1286 	linkmode_copy(adv_tmp, phydev->advertising);
1287 	linkmode_copy(phydev->advertising, phydev->adv_old);
1288 	linkmode_zero(phydev->adv_old);
1289 
1290 	if (linkmode_equal(phydev->advertising, adv_tmp))
1291 		goto out;
1292 
1293 	ret = phy_config_aneg(phydev);
1294 out:
1295 	mutex_unlock(&phydev->lock);
1296 
1297 	return ret;
1298 }
1299 EXPORT_SYMBOL_GPL(phy_speed_up);
1300 
1301 /**
1302  * phy_start_machine - start PHY state machine tracking
1303  * @phydev: the phy_device struct
1304  *
1305  * Description: The PHY infrastructure can run a state machine
1306  *   which tracks whether the PHY is starting up, negotiating,
1307  *   etc.  This function starts the delayed workqueue which tracks
1308  *   the state of the PHY. If you want to maintain your own state machine,
1309  *   do not call this function.
1310  */
1311 void phy_start_machine(struct phy_device *phydev)
1312 {
1313 	phy_trigger_machine(phydev);
1314 }
1315 EXPORT_SYMBOL_GPL(phy_start_machine);
1316 
1317 /**
1318  * phy_stop_machine - stop the PHY state machine tracking
1319  * @phydev: target phy_device struct
1320  *
1321  * Description: Stops the state machine delayed workqueue, sets the
1322  *   state to UP (unless it wasn't up yet). This function must be
1323  *   called BEFORE phy_detach.
1324  */
1325 void phy_stop_machine(struct phy_device *phydev)
1326 {
1327 	cancel_delayed_work_sync(&phydev->state_queue);
1328 
1329 	mutex_lock(&phydev->lock);
1330 	if (phy_is_started(phydev))
1331 		phydev->state = PHY_UP;
1332 	mutex_unlock(&phydev->lock);
1333 }
1334 
1335 static void phy_process_error(struct phy_device *phydev)
1336 {
1337 	/* phydev->lock must be held for the state change to be safe */
1338 	if (!mutex_is_locked(&phydev->lock))
1339 		phydev_err(phydev, "PHY-device data unsafe context\n");
1340 
1341 	phydev->state = PHY_ERROR;
1342 
1343 	phy_trigger_machine(phydev);
1344 }
1345 
1346 static void phy_error_precise(struct phy_device *phydev,
1347 			      const void *func, int err)
1348 {
1349 	WARN(1, "%pS: returned: %d\n", func, err);
1350 	phy_process_error(phydev);
1351 }
1352 
1353 /**
1354  * phy_error - enter ERROR state for this PHY device
1355  * @phydev: target phy_device struct
1356  *
1357  * Moves the PHY to the ERROR state in response to a read
1358  * or write error, and tells the controller the link is down.
1359  * Must be called with phydev->lock held.
1360  */
1361 void phy_error(struct phy_device *phydev)
1362 {
1363 	WARN_ON(1);
1364 	phy_process_error(phydev);
1365 }
1366 EXPORT_SYMBOL(phy_error);
1367 
1368 /**
1369  * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
1370  * @phydev: target phy_device struct
1371  */
1372 int phy_disable_interrupts(struct phy_device *phydev)
1373 {
1374 	/* Disable PHY interrupts */
1375 	return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
1376 }
1377 
1378 /**
1379  * phy_interrupt - PHY interrupt handler
1380  * @irq: interrupt line
1381  * @phy_dat: phy_device pointer
1382  *
1383  * Description: Handle PHY interrupt
1384  */
1385 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
1386 {
1387 	struct phy_device *phydev = phy_dat;
1388 	irqreturn_t ret;
1389 
1390 	/* Wakeup interrupts may occur during a system sleep transition.
1391 	 * Postpone handling until the PHY has resumed.
1392 	 */
1393 	if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
1394 		struct net_device *netdev = phydev->attached_dev;
1395 
1396 		if (netdev) {
1397 			struct device *parent = netdev->dev.parent;
1398 
1399 			if (netdev->ethtool->wol_enabled)
1400 				pm_system_wakeup();
1401 			else if (device_may_wakeup(&netdev->dev))
1402 				pm_wakeup_dev_event(&netdev->dev, 0, true);
1403 			else if (parent && device_may_wakeup(parent))
1404 				pm_wakeup_dev_event(parent, 0, true);
1405 		}
1406 
1407 		phydev->irq_rerun = 1;
1408 		disable_irq_nosync(irq);
1409 		return IRQ_HANDLED;
1410 	}
1411 
1412 	mutex_lock(&phydev->lock);
1413 	ret = phydev->drv->handle_interrupt(phydev);
1414 	mutex_unlock(&phydev->lock);
1415 
1416 	return ret;
1417 }
1418 
1419 /**
1420  * phy_enable_interrupts - Enable the interrupts from the PHY side
1421  * @phydev: target phy_device struct
1422  */
1423 static int phy_enable_interrupts(struct phy_device *phydev)
1424 {
1425 	return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1426 }
1427 
1428 /**
1429  * phy_update_stats - Update PHY device statistics if supported.
1430  * @phydev: Pointer to the PHY device structure.
1431  *
1432  * If the PHY driver provides an update_stats callback, this function
1433  * invokes it to update the PHY statistics. If not, it returns 0.
1434  *
1435  * Return: 0 on success, or a negative error code if the callback fails.
1436  */
1437 static int phy_update_stats(struct phy_device *phydev)
1438 {
1439 	if (!phydev->drv->update_stats)
1440 		return 0;
1441 
1442 	return phydev->drv->update_stats(phydev);
1443 }
1444 
1445 /**
1446  * phy_request_interrupt - request and enable interrupt for a PHY device
1447  * @phydev: target phy_device struct
1448  *
1449  * Description: Request and enable the interrupt for the given PHY.
1450  *   If this fails, then we set irq to PHY_POLL.
1451  *   This should only be called with a valid IRQ number.
1452  */
1453 void phy_request_interrupt(struct phy_device *phydev)
1454 {
1455 	int err;
1456 
1457 	err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1458 				   IRQF_ONESHOT | IRQF_SHARED,
1459 				   phydev_name(phydev), phydev);
1460 	if (err) {
1461 		phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1462 			    err, phydev->irq);
1463 		phydev->irq = PHY_POLL;
1464 	} else {
1465 		if (phy_enable_interrupts(phydev)) {
1466 			phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1467 			phy_free_interrupt(phydev);
1468 			phydev->irq = PHY_POLL;
1469 		}
1470 	}
1471 }
1472 EXPORT_SYMBOL(phy_request_interrupt);
1473 
1474 /**
1475  * phy_free_interrupt - disable and free interrupt for a PHY device
1476  * @phydev: target phy_device struct
1477  *
1478  * Description: Disable and free the interrupt for the given PHY.
1479  *   This should only be called with a valid IRQ number.
1480  */
1481 void phy_free_interrupt(struct phy_device *phydev)
1482 {
1483 	phy_disable_interrupts(phydev);
1484 	free_irq(phydev->irq, phydev);
1485 }
1486 EXPORT_SYMBOL(phy_free_interrupt);
1487 
1488 /**
1489  * phy_get_next_update_time - Determine the next PHY update time
1490  * @phydev: Pointer to the phy_device structure
1491  *
1492  * This function queries the PHY driver to get the time for the next polling
1493  * event. If the driver does not implement the callback, a default value is
1494  * used.
1495  *
1496  * Return: The time for the next polling event in jiffies
1497  */
1498 static unsigned int phy_get_next_update_time(struct phy_device *phydev)
1499 {
1500 	if (phydev->drv && phydev->drv->get_next_update_time)
1501 		return phydev->drv->get_next_update_time(phydev);
1502 
1503 	return PHY_STATE_TIME;
1504 }
1505 
1506 enum phy_state_work {
1507 	PHY_STATE_WORK_NONE,
1508 	PHY_STATE_WORK_ANEG,
1509 	PHY_STATE_WORK_SUSPEND,
1510 };
1511 
1512 static enum phy_state_work _phy_state_machine(struct phy_device *phydev)
1513 {
1514 	enum phy_state_work state_work = PHY_STATE_WORK_NONE;
1515 	struct net_device *dev = phydev->attached_dev;
1516 	enum phy_state old_state = phydev->state;
1517 	const void *func = NULL;
1518 	bool finished = false;
1519 	int err = 0;
1520 
1521 	switch (phydev->state) {
1522 	case PHY_DOWN:
1523 	case PHY_READY:
1524 		break;
1525 	case PHY_UP:
1526 		state_work = PHY_STATE_WORK_ANEG;
1527 		break;
1528 	case PHY_NOLINK:
1529 	case PHY_RUNNING:
1530 		err = phy_check_link_status(phydev);
1531 		func = &phy_check_link_status;
1532 
1533 		if (!err)
1534 			err = phy_update_stats(phydev);
1535 		break;
1536 	case PHY_CABLETEST:
1537 		err = phydev->drv->cable_test_get_status(phydev, &finished);
1538 		if (err) {
1539 			phy_abort_cable_test(phydev);
1540 			netif_testing_off(dev);
1541 			state_work = PHY_STATE_WORK_ANEG;
1542 			phydev->state = PHY_UP;
1543 			break;
1544 		}
1545 
1546 		if (finished) {
1547 			ethnl_cable_test_finished(phydev);
1548 			netif_testing_off(dev);
1549 			state_work = PHY_STATE_WORK_ANEG;
1550 			phydev->state = PHY_UP;
1551 		}
1552 		break;
1553 	case PHY_HALTED:
1554 		if (phydev->link) {
1555 			if (phydev->autoneg == AUTONEG_ENABLE) {
1556 				phydev->speed = SPEED_UNKNOWN;
1557 				phydev->duplex = DUPLEX_UNKNOWN;
1558 			}
1559 			if (phydev->master_slave_state !=
1560 						MASTER_SLAVE_STATE_UNSUPPORTED)
1561 				phydev->master_slave_state =
1562 						MASTER_SLAVE_STATE_UNKNOWN;
1563 			phydev->mdix = ETH_TP_MDI_INVALID;
1564 			linkmode_zero(phydev->lp_advertising);
1565 		}
1566 		fallthrough;
1567 	case PHY_ERROR:
1568 		if (phydev->link) {
1569 			phydev->link = 0;
1570 			phydev->eee_active = false;
1571 			phydev->enable_tx_lpi = false;
1572 			phy_link_down(phydev);
1573 		}
1574 		state_work = PHY_STATE_WORK_SUSPEND;
1575 		break;
1576 	}
1577 
1578 	if (state_work == PHY_STATE_WORK_ANEG) {
1579 		err = _phy_start_aneg(phydev);
1580 		func = &_phy_start_aneg;
1581 	}
1582 
1583 	if (err == -ENODEV)
1584 		return state_work;
1585 
1586 	if (err < 0)
1587 		phy_error_precise(phydev, func, err);
1588 
1589 	phy_process_state_change(phydev, old_state);
1590 
1591 	/* Only re-schedule a PHY state machine change if we are polling the
1592 	 * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
1593 	 * between states from phy_mac_interrupt().
1594 	 *
1595 	 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1596 	 * state machine would be pointless and possibly error prone when
1597 	 * called from phy_disconnect() synchronously.
1598 	 */
1599 	if (phy_polling_mode(phydev) && phy_is_started(phydev))
1600 		phy_queue_state_machine(phydev,
1601 					phy_get_next_update_time(phydev));
1602 
1603 	return state_work;
1604 }
1605 
1606 /* unlocked part of the PHY state machine */
1607 static void _phy_state_machine_post_work(struct phy_device *phydev,
1608 					 enum phy_state_work state_work)
1609 {
1610 	if (state_work == PHY_STATE_WORK_SUSPEND)
1611 		phy_suspend(phydev);
1612 }
1613 
1614 /**
1615  * phy_state_machine - Handle the state machine
1616  * @work: work_struct that describes the work to be done
1617  */
1618 void phy_state_machine(struct work_struct *work)
1619 {
1620 	struct delayed_work *dwork = to_delayed_work(work);
1621 	struct phy_device *phydev =
1622 			container_of(dwork, struct phy_device, state_queue);
1623 	enum phy_state_work state_work;
1624 
1625 	mutex_lock(&phydev->lock);
1626 	state_work = _phy_state_machine(phydev);
1627 	mutex_unlock(&phydev->lock);
1628 
1629 	_phy_state_machine_post_work(phydev, state_work);
1630 }
1631 
1632 /**
1633  * phy_stop - Bring down the PHY link, and stop checking the status
1634  * @phydev: target phy_device struct
1635  */
1636 void phy_stop(struct phy_device *phydev)
1637 {
1638 	struct net_device *dev = phydev->attached_dev;
1639 	enum phy_state_work state_work;
1640 	enum phy_state old_state;
1641 
1642 	if (!phy_is_started(phydev) && phydev->state != PHY_DOWN &&
1643 	    phydev->state != PHY_ERROR) {
1644 		WARN(1, "called from state %s\n",
1645 		     phy_state_to_str(phydev->state));
1646 		return;
1647 	}
1648 
1649 	mutex_lock(&phydev->lock);
1650 	old_state = phydev->state;
1651 
1652 	if (phydev->state == PHY_CABLETEST) {
1653 		phy_abort_cable_test(phydev);
1654 		netif_testing_off(dev);
1655 	}
1656 
1657 	if (phydev->sfp_bus)
1658 		sfp_upstream_stop(phydev->sfp_bus);
1659 
1660 	phydev->state = PHY_HALTED;
1661 	phy_process_state_change(phydev, old_state);
1662 
1663 	state_work = _phy_state_machine(phydev);
1664 	mutex_unlock(&phydev->lock);
1665 
1666 	_phy_state_machine_post_work(phydev, state_work);
1667 	phy_stop_machine(phydev);
1668 
1669 	/* Cannot call flush_scheduled_work() here as desired because
1670 	 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
1671 	 * will not reenable interrupts.
1672 	 */
1673 }
1674 EXPORT_SYMBOL(phy_stop);
1675 
1676 /**
1677  * phy_start - start or restart a PHY device
1678  * @phydev: target phy_device struct
1679  *
1680  * Description: Indicates the attached device's readiness to
1681  *   handle PHY-related work.  Used during startup to start the
1682  *   PHY, and after a call to phy_stop() to resume operation.
1683  *   Also used to indicate the MDIO bus has cleared an error
1684  *   condition.
1685  */
1686 void phy_start(struct phy_device *phydev)
1687 {
1688 	mutex_lock(&phydev->lock);
1689 
1690 	if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1691 		WARN(1, "called from state %s\n",
1692 		     phy_state_to_str(phydev->state));
1693 		goto out;
1694 	}
1695 
1696 	if (phydev->sfp_bus)
1697 		sfp_upstream_start(phydev->sfp_bus);
1698 
1699 	/* if phy was suspended, bring the physical link up again */
1700 	__phy_resume(phydev);
1701 
1702 	phydev->state = PHY_UP;
1703 
1704 	phy_start_machine(phydev);
1705 out:
1706 	mutex_unlock(&phydev->lock);
1707 }
1708 EXPORT_SYMBOL(phy_start);
1709 
1710 /**
1711  * phy_mac_interrupt - MAC says the link has changed
1712  * @phydev: phy_device struct with changed link
1713  *
1714  * The MAC layer is able to indicate there has been a change in the PHY link
1715  * status. Trigger the state machine and work a work queue.
1716  */
1717 void phy_mac_interrupt(struct phy_device *phydev)
1718 {
1719 	/* Trigger a state machine change */
1720 	phy_trigger_machine(phydev);
1721 }
1722 EXPORT_SYMBOL(phy_mac_interrupt);
1723 
1724 /**
1725  * phy_loopback - Configure loopback mode of PHY
1726  * @phydev: target phy_device struct
1727  * @enable: enable or disable loopback mode
1728  * @speed: enable loopback mode with speed
1729  *
1730  * Configure loopback mode of PHY and signal link down and link up if speed is
1731  * changing.
1732  *
1733  * Return: 0 on success, negative error code on failure.
1734  */
1735 int phy_loopback(struct phy_device *phydev, bool enable, int speed)
1736 {
1737 	bool link_up = false;
1738 	int ret = 0;
1739 
1740 	if (!phydev->drv)
1741 		return -EIO;
1742 
1743 	mutex_lock(&phydev->lock);
1744 
1745 	if (enable && phydev->loopback_enabled) {
1746 		ret = -EBUSY;
1747 		goto out;
1748 	}
1749 
1750 	if (!enable && !phydev->loopback_enabled) {
1751 		ret = -EINVAL;
1752 		goto out;
1753 	}
1754 
1755 	if (enable) {
1756 		/*
1757 		 * Link up is signaled with a defined speed. If speed changes,
1758 		 * then first link down and after that link up needs to be
1759 		 * signaled.
1760 		 */
1761 		if (phydev->link && phydev->state == PHY_RUNNING) {
1762 			/* link is up and signaled */
1763 			if (speed && phydev->speed != speed) {
1764 				/* signal link down and up for new speed */
1765 				phydev->link = false;
1766 				phydev->state = PHY_NOLINK;
1767 				phy_link_down(phydev);
1768 
1769 				link_up = true;
1770 			}
1771 		} else {
1772 			/* link is not signaled */
1773 			if (speed) {
1774 				/* signal link up for new speed */
1775 				link_up = true;
1776 			}
1777 		}
1778 	}
1779 
1780 	if (phydev->drv->set_loopback)
1781 		ret = phydev->drv->set_loopback(phydev, enable, speed);
1782 	else
1783 		ret = genphy_loopback(phydev, enable, speed);
1784 
1785 	if (ret) {
1786 		if (enable) {
1787 			/* try to restore link if enabling loopback fails */
1788 			if (phydev->drv->set_loopback)
1789 				phydev->drv->set_loopback(phydev, false, 0);
1790 			else
1791 				genphy_loopback(phydev, false, 0);
1792 		}
1793 
1794 		goto out;
1795 	}
1796 
1797 	if (link_up) {
1798 		phydev->link = true;
1799 		phydev->state = PHY_RUNNING;
1800 		phy_link_up(phydev);
1801 	}
1802 
1803 	phydev->loopback_enabled = enable;
1804 
1805 out:
1806 	mutex_unlock(&phydev->lock);
1807 	return ret;
1808 }
1809 EXPORT_SYMBOL(phy_loopback);
1810 
1811 /**
1812  * phy_eee_tx_clock_stop_capable() - indicate whether the MAC can stop tx clock
1813  * @phydev: target phy_device struct
1814  *
1815  * Indicate whether the MAC can disable the transmit xMII clock while in LPI
1816  * state. Returns 1 if the MAC may stop the transmit clock, 0 if the MAC must
1817  * not stop the transmit clock, or negative error.
1818  */
1819 int phy_eee_tx_clock_stop_capable(struct phy_device *phydev)
1820 {
1821 	int stat1;
1822 
1823 	stat1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1824 	if (stat1 < 0)
1825 		return stat1;
1826 
1827 	return !!(stat1 & MDIO_PCS_STAT1_CLKSTOP_CAP);
1828 }
1829 EXPORT_SYMBOL_GPL(phy_eee_tx_clock_stop_capable);
1830 
1831 /**
1832  * phy_eee_rx_clock_stop() - configure PHY receive clock in LPI
1833  * @phydev: target phy_device struct
1834  * @clk_stop_enable: flag to indicate whether the clock can be stopped
1835  *
1836  * Configure whether the PHY can disable its receive clock during LPI mode,
1837  * See IEEE 802.3 sections 22.2.2.2, 35.2.2.10, and 45.2.3.1.4.
1838  *
1839  * Returns: 0 or negative error.
1840  */
1841 int phy_eee_rx_clock_stop(struct phy_device *phydev, bool clk_stop_enable)
1842 {
1843 	/* Configure the PHY to stop receiving xMII
1844 	 * clock while it is signaling LPI.
1845 	 */
1846 	return phy_modify_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1847 			      MDIO_PCS_CTRL1_CLKSTOP_EN,
1848 			      clk_stop_enable ? MDIO_PCS_CTRL1_CLKSTOP_EN : 0);
1849 }
1850 EXPORT_SYMBOL_GPL(phy_eee_rx_clock_stop);
1851 
1852 /**
1853  * phy_init_eee - init and check the EEE feature
1854  * @phydev: target phy_device struct
1855  * @clk_stop_enable: PHY may stop the clock during LPI
1856  *
1857  * Description: it checks if the Energy-Efficient Ethernet (EEE)
1858  * is supported by looking at the MMD registers 3.20 and 7.60/61
1859  * and it programs the MMD register 3.0 setting the "Clock stop enable"
1860  * bit if required.
1861  */
1862 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1863 {
1864 	int ret;
1865 
1866 	if (!phydev->drv)
1867 		return -EIO;
1868 
1869 	ret = genphy_c45_eee_is_active(phydev, NULL);
1870 	if (ret < 0)
1871 		return ret;
1872 	if (!ret)
1873 		return -EPROTONOSUPPORT;
1874 
1875 	if (clk_stop_enable)
1876 		ret = phy_eee_rx_clock_stop(phydev, true);
1877 
1878 	return ret < 0 ? ret : 0;
1879 }
1880 EXPORT_SYMBOL(phy_init_eee);
1881 
1882 /**
1883  * phy_get_eee_err - report the EEE wake error count
1884  * @phydev: target phy_device struct
1885  *
1886  * Description: it is to report the number of time where the PHY
1887  * failed to complete its normal wake sequence.
1888  */
1889 int phy_get_eee_err(struct phy_device *phydev)
1890 {
1891 	int ret;
1892 
1893 	if (!phydev->drv)
1894 		return -EIO;
1895 
1896 	mutex_lock(&phydev->lock);
1897 	ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1898 	mutex_unlock(&phydev->lock);
1899 
1900 	return ret;
1901 }
1902 EXPORT_SYMBOL(phy_get_eee_err);
1903 
1904 /**
1905  * phy_ethtool_get_eee - get EEE supported and status
1906  * @phydev: target phy_device struct
1907  * @data: ethtool_keee data
1908  *
1909  * Description: get the current EEE settings, filling in all members of
1910  * @data.
1911  */
1912 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_keee *data)
1913 {
1914 	int ret;
1915 
1916 	if (!phydev->drv)
1917 		return -EIO;
1918 
1919 	mutex_lock(&phydev->lock);
1920 	ret = genphy_c45_ethtool_get_eee(phydev, data);
1921 	eeecfg_to_eee(data, &phydev->eee_cfg);
1922 	mutex_unlock(&phydev->lock);
1923 
1924 	return ret;
1925 }
1926 EXPORT_SYMBOL(phy_ethtool_get_eee);
1927 
1928 /**
1929  * phy_ethtool_set_eee_noneg - Adjusts MAC LPI configuration without PHY
1930  *			       renegotiation
1931  * @phydev: pointer to the target PHY device structure
1932  * @old_cfg: pointer to the eee_config structure containing the old EEE settings
1933  *
1934  * This function updates the Energy Efficient Ethernet (EEE) configuration
1935  * for cases where only the MAC's Low Power Idle (LPI) configuration changes,
1936  * without triggering PHY renegotiation. It ensures that the MAC is properly
1937  * informed of the new LPI settings by cycling the link down and up, which
1938  * is necessary for the MAC to adopt the new configuration. This adjustment
1939  * is done only if there is a change in the tx_lpi_enabled or tx_lpi_timer
1940  * configuration.
1941  */
1942 static void phy_ethtool_set_eee_noneg(struct phy_device *phydev,
1943 				      const struct eee_config *old_cfg)
1944 {
1945 	bool enable_tx_lpi;
1946 
1947 	if (!phydev->link)
1948 		return;
1949 
1950 	enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled && phydev->eee_active;
1951 
1952 	if (phydev->enable_tx_lpi != enable_tx_lpi ||
1953 	    phydev->eee_cfg.tx_lpi_timer != old_cfg->tx_lpi_timer) {
1954 		phydev->enable_tx_lpi = false;
1955 		phydev->link = false;
1956 		phy_link_down(phydev);
1957 		phydev->enable_tx_lpi = enable_tx_lpi;
1958 		phydev->link = true;
1959 		phy_link_up(phydev);
1960 	}
1961 }
1962 
1963 /**
1964  * phy_ethtool_set_eee - set EEE supported and status
1965  * @phydev: target phy_device struct
1966  * @data: ethtool_keee data
1967  *
1968  * Description: it is to program the Advertisement EEE register.
1969  */
1970 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_keee *data)
1971 {
1972 	struct eee_config old_cfg;
1973 	int ret;
1974 
1975 	if (!phydev->drv)
1976 		return -EIO;
1977 
1978 	mutex_lock(&phydev->lock);
1979 
1980 	old_cfg = phydev->eee_cfg;
1981 	eee_to_eeecfg(&phydev->eee_cfg, data);
1982 
1983 	ret = genphy_c45_ethtool_set_eee(phydev, data);
1984 	if (ret == 0)
1985 		phy_ethtool_set_eee_noneg(phydev, &old_cfg);
1986 	else if (ret < 0)
1987 		phydev->eee_cfg = old_cfg;
1988 
1989 	mutex_unlock(&phydev->lock);
1990 
1991 	return ret < 0 ? ret : 0;
1992 }
1993 EXPORT_SYMBOL(phy_ethtool_set_eee);
1994 
1995 /**
1996  * phy_ethtool_set_wol - Configure Wake On LAN
1997  *
1998  * @phydev: target phy_device struct
1999  * @wol: Configuration requested
2000  */
2001 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
2002 {
2003 	int ret;
2004 
2005 	if (phydev->drv && phydev->drv->set_wol) {
2006 		mutex_lock(&phydev->lock);
2007 		ret = phydev->drv->set_wol(phydev, wol);
2008 		mutex_unlock(&phydev->lock);
2009 
2010 		return ret;
2011 	}
2012 
2013 	return -EOPNOTSUPP;
2014 }
2015 EXPORT_SYMBOL(phy_ethtool_set_wol);
2016 
2017 /**
2018  * phy_ethtool_get_wol - Get the current Wake On LAN configuration
2019  *
2020  * @phydev: target phy_device struct
2021  * @wol: Store the current configuration here
2022  */
2023 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
2024 {
2025 	if (phydev->drv && phydev->drv->get_wol) {
2026 		mutex_lock(&phydev->lock);
2027 		phydev->drv->get_wol(phydev, wol);
2028 		mutex_unlock(&phydev->lock);
2029 	}
2030 }
2031 EXPORT_SYMBOL(phy_ethtool_get_wol);
2032 
2033 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
2034 				   struct ethtool_link_ksettings *cmd)
2035 {
2036 	struct phy_device *phydev = ndev->phydev;
2037 
2038 	if (!phydev)
2039 		return -ENODEV;
2040 
2041 	phy_ethtool_ksettings_get(phydev, cmd);
2042 
2043 	return 0;
2044 }
2045 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
2046 
2047 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
2048 				   const struct ethtool_link_ksettings *cmd)
2049 {
2050 	struct phy_device *phydev = ndev->phydev;
2051 
2052 	if (!phydev)
2053 		return -ENODEV;
2054 
2055 	return phy_ethtool_ksettings_set(phydev, cmd);
2056 }
2057 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
2058 
2059 /**
2060  * phy_ethtool_nway_reset - Restart auto negotiation
2061  * @ndev: Network device to restart autoneg for
2062  */
2063 int phy_ethtool_nway_reset(struct net_device *ndev)
2064 {
2065 	struct phy_device *phydev = ndev->phydev;
2066 	int ret;
2067 
2068 	if (!phydev)
2069 		return -ENODEV;
2070 
2071 	if (!phydev->drv)
2072 		return -EIO;
2073 
2074 	mutex_lock(&phydev->lock);
2075 	ret = phy_restart_aneg(phydev);
2076 	mutex_unlock(&phydev->lock);
2077 
2078 	return ret;
2079 }
2080 EXPORT_SYMBOL(phy_ethtool_nway_reset);
2081