xref: /openbmc/linux/drivers/net/phy/phy.c (revision e847c767)
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 #define PHY_STATE_TIME	HZ
40 
41 #define PHY_STATE_STR(_state)			\
42 	case PHY_##_state:			\
43 		return __stringify(_state);	\
44 
45 static const char *phy_state_to_str(enum phy_state st)
46 {
47 	switch (st) {
48 	PHY_STATE_STR(DOWN)
49 	PHY_STATE_STR(READY)
50 	PHY_STATE_STR(UP)
51 	PHY_STATE_STR(RUNNING)
52 	PHY_STATE_STR(NOLINK)
53 	PHY_STATE_STR(CABLETEST)
54 	PHY_STATE_STR(HALTED)
55 	}
56 
57 	return NULL;
58 }
59 
60 static void phy_process_state_change(struct phy_device *phydev,
61 				     enum phy_state old_state)
62 {
63 	if (old_state != phydev->state) {
64 		phydev_dbg(phydev, "PHY state change %s -> %s\n",
65 			   phy_state_to_str(old_state),
66 			   phy_state_to_str(phydev->state));
67 		if (phydev->drv && phydev->drv->link_change_notify)
68 			phydev->drv->link_change_notify(phydev);
69 	}
70 }
71 
72 static void phy_link_up(struct phy_device *phydev)
73 {
74 	phydev->phy_link_change(phydev, true);
75 	phy_led_trigger_change_speed(phydev);
76 }
77 
78 static void phy_link_down(struct phy_device *phydev)
79 {
80 	phydev->phy_link_change(phydev, false);
81 	phy_led_trigger_change_speed(phydev);
82 	WRITE_ONCE(phydev->link_down_events, phydev->link_down_events + 1);
83 }
84 
85 static const char *phy_pause_str(struct phy_device *phydev)
86 {
87 	bool local_pause, local_asym_pause;
88 
89 	if (phydev->autoneg == AUTONEG_DISABLE)
90 		goto no_pause;
91 
92 	local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
93 					phydev->advertising);
94 	local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
95 					     phydev->advertising);
96 
97 	if (local_pause && phydev->pause)
98 		return "rx/tx";
99 
100 	if (local_asym_pause && phydev->asym_pause) {
101 		if (local_pause)
102 			return "rx";
103 		if (phydev->pause)
104 			return "tx";
105 	}
106 
107 no_pause:
108 	return "off";
109 }
110 
111 /**
112  * phy_print_status - Convenience function to print out the current phy status
113  * @phydev: the phy_device struct
114  */
115 void phy_print_status(struct phy_device *phydev)
116 {
117 	if (phydev->link) {
118 		netdev_info(phydev->attached_dev,
119 			"Link is Up - %s/%s %s- flow control %s\n",
120 			phy_speed_to_str(phydev->speed),
121 			phy_duplex_to_str(phydev->duplex),
122 			phydev->downshifted_rate ? "(downshifted) " : "",
123 			phy_pause_str(phydev));
124 	} else	{
125 		netdev_info(phydev->attached_dev, "Link is Down\n");
126 	}
127 }
128 EXPORT_SYMBOL(phy_print_status);
129 
130 /**
131  * phy_get_rate_matching - determine if rate matching is supported
132  * @phydev: The phy device to return rate matching for
133  * @iface: The interface mode to use
134  *
135  * This determines the type of rate matching (if any) that @phy supports
136  * using @iface. @iface may be %PHY_INTERFACE_MODE_NA to determine if any
137  * interface supports rate matching.
138  *
139  * Return: The type of rate matching @phy supports for @iface, or
140  *         %RATE_MATCH_NONE.
141  */
142 int phy_get_rate_matching(struct phy_device *phydev,
143 			  phy_interface_t iface)
144 {
145 	int ret = RATE_MATCH_NONE;
146 
147 	if (phydev->drv->get_rate_matching) {
148 		mutex_lock(&phydev->lock);
149 		ret = phydev->drv->get_rate_matching(phydev, iface);
150 		mutex_unlock(&phydev->lock);
151 	}
152 
153 	return ret;
154 }
155 EXPORT_SYMBOL_GPL(phy_get_rate_matching);
156 
157 /**
158  * phy_config_interrupt - configure the PHY device for the requested interrupts
159  * @phydev: the phy_device struct
160  * @interrupts: interrupt flags to configure for this @phydev
161  *
162  * Returns 0 on success or < 0 on error.
163  */
164 static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
165 {
166 	phydev->interrupts = interrupts ? 1 : 0;
167 	if (phydev->drv->config_intr)
168 		return phydev->drv->config_intr(phydev);
169 
170 	return 0;
171 }
172 
173 /**
174  * phy_restart_aneg - restart auto-negotiation
175  * @phydev: target phy_device struct
176  *
177  * Restart the autonegotiation on @phydev.  Returns >= 0 on success or
178  * negative errno on error.
179  */
180 int phy_restart_aneg(struct phy_device *phydev)
181 {
182 	int ret;
183 
184 	if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
185 		ret = genphy_c45_restart_aneg(phydev);
186 	else
187 		ret = genphy_restart_aneg(phydev);
188 
189 	return ret;
190 }
191 EXPORT_SYMBOL_GPL(phy_restart_aneg);
192 
193 /**
194  * phy_aneg_done - return auto-negotiation status
195  * @phydev: target phy_device struct
196  *
197  * Description: Return the auto-negotiation status from this @phydev
198  * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
199  * is still pending.
200  */
201 int phy_aneg_done(struct phy_device *phydev)
202 {
203 	if (phydev->drv && phydev->drv->aneg_done)
204 		return phydev->drv->aneg_done(phydev);
205 	else if (phydev->is_c45)
206 		return genphy_c45_aneg_done(phydev);
207 	else
208 		return genphy_aneg_done(phydev);
209 }
210 EXPORT_SYMBOL(phy_aneg_done);
211 
212 /**
213  * phy_find_valid - find a PHY setting that matches the requested parameters
214  * @speed: desired speed
215  * @duplex: desired duplex
216  * @supported: mask of supported link modes
217  *
218  * Locate a supported phy setting that is, in priority order:
219  * - an exact match for the specified speed and duplex mode
220  * - a match for the specified speed, or slower speed
221  * - the slowest supported speed
222  * Returns the matched phy_setting entry, or %NULL if no supported phy
223  * settings were found.
224  */
225 static const struct phy_setting *
226 phy_find_valid(int speed, int duplex, unsigned long *supported)
227 {
228 	return phy_lookup_setting(speed, duplex, supported, false);
229 }
230 
231 /**
232  * phy_supported_speeds - return all speeds currently supported by a phy device
233  * @phy: The phy device to return supported speeds of.
234  * @speeds: buffer to store supported speeds in.
235  * @size:   size of speeds buffer.
236  *
237  * Description: Returns the number of supported speeds, and fills the speeds
238  * buffer with the supported speeds. If speeds buffer is too small to contain
239  * all currently supported speeds, will return as many speeds as can fit.
240  */
241 unsigned int phy_supported_speeds(struct phy_device *phy,
242 				  unsigned int *speeds,
243 				  unsigned int size)
244 {
245 	return phy_speeds(speeds, size, phy->supported);
246 }
247 
248 /**
249  * phy_check_valid - check if there is a valid PHY setting which matches
250  *		     speed, duplex, and feature mask
251  * @speed: speed to match
252  * @duplex: duplex to match
253  * @features: A mask of the valid settings
254  *
255  * Description: Returns true if there is a valid setting, false otherwise.
256  */
257 bool phy_check_valid(int speed, int duplex, unsigned long *features)
258 {
259 	return !!phy_lookup_setting(speed, duplex, features, true);
260 }
261 EXPORT_SYMBOL(phy_check_valid);
262 
263 /**
264  * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
265  * @phydev: the target phy_device struct
266  *
267  * Description: Make sure the PHY is set to supported speeds and
268  *   duplexes.  Drop down by one in this order:  1000/FULL,
269  *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
270  */
271 static void phy_sanitize_settings(struct phy_device *phydev)
272 {
273 	const struct phy_setting *setting;
274 
275 	setting = phy_find_valid(phydev->speed, phydev->duplex,
276 				 phydev->supported);
277 	if (setting) {
278 		phydev->speed = setting->speed;
279 		phydev->duplex = setting->duplex;
280 	} else {
281 		/* We failed to find anything (no supported speeds?) */
282 		phydev->speed = SPEED_UNKNOWN;
283 		phydev->duplex = DUPLEX_UNKNOWN;
284 	}
285 }
286 
287 void phy_ethtool_ksettings_get(struct phy_device *phydev,
288 			       struct ethtool_link_ksettings *cmd)
289 {
290 	mutex_lock(&phydev->lock);
291 	linkmode_copy(cmd->link_modes.supported, phydev->supported);
292 	linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
293 	linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
294 
295 	cmd->base.speed = phydev->speed;
296 	cmd->base.duplex = phydev->duplex;
297 	cmd->base.master_slave_cfg = phydev->master_slave_get;
298 	cmd->base.master_slave_state = phydev->master_slave_state;
299 	cmd->base.rate_matching = phydev->rate_matching;
300 	if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
301 		cmd->base.port = PORT_BNC;
302 	else
303 		cmd->base.port = phydev->port;
304 	cmd->base.transceiver = phy_is_internal(phydev) ?
305 				XCVR_INTERNAL : XCVR_EXTERNAL;
306 	cmd->base.phy_address = phydev->mdio.addr;
307 	cmd->base.autoneg = phydev->autoneg;
308 	cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
309 	cmd->base.eth_tp_mdix = phydev->mdix;
310 	mutex_unlock(&phydev->lock);
311 }
312 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
313 
314 /**
315  * phy_mii_ioctl - generic PHY MII ioctl interface
316  * @phydev: the phy_device struct
317  * @ifr: &struct ifreq for socket ioctl's
318  * @cmd: ioctl cmd to execute
319  *
320  * Note that this function is currently incompatible with the
321  * PHYCONTROL layer.  It changes registers without regard to
322  * current state.  Use at own risk.
323  */
324 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
325 {
326 	struct mii_ioctl_data *mii_data = if_mii(ifr);
327 	u16 val = mii_data->val_in;
328 	bool change_autoneg = false;
329 	int prtad, devad;
330 
331 	switch (cmd) {
332 	case SIOCGMIIPHY:
333 		mii_data->phy_id = phydev->mdio.addr;
334 		fallthrough;
335 
336 	case SIOCGMIIREG:
337 		if (mdio_phy_id_is_c45(mii_data->phy_id)) {
338 			prtad = mdio_phy_id_prtad(mii_data->phy_id);
339 			devad = mdio_phy_id_devad(mii_data->phy_id);
340 			mii_data->val_out = mdiobus_c45_read(
341 				phydev->mdio.bus, prtad, devad,
342 				mii_data->reg_num);
343 		} else {
344 			mii_data->val_out = mdiobus_read(
345 				phydev->mdio.bus, mii_data->phy_id,
346 				mii_data->reg_num);
347 		}
348 		return 0;
349 
350 	case SIOCSMIIREG:
351 		if (mdio_phy_id_is_c45(mii_data->phy_id)) {
352 			prtad = mdio_phy_id_prtad(mii_data->phy_id);
353 			devad = mdio_phy_id_devad(mii_data->phy_id);
354 		} else {
355 			prtad = mii_data->phy_id;
356 			devad = mii_data->reg_num;
357 		}
358 		if (prtad == phydev->mdio.addr) {
359 			switch (devad) {
360 			case MII_BMCR:
361 				if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
362 					if (phydev->autoneg == AUTONEG_ENABLE)
363 						change_autoneg = true;
364 					phydev->autoneg = AUTONEG_DISABLE;
365 					if (val & BMCR_FULLDPLX)
366 						phydev->duplex = DUPLEX_FULL;
367 					else
368 						phydev->duplex = DUPLEX_HALF;
369 					if (val & BMCR_SPEED1000)
370 						phydev->speed = SPEED_1000;
371 					else if (val & BMCR_SPEED100)
372 						phydev->speed = SPEED_100;
373 					else phydev->speed = SPEED_10;
374 				} else {
375 					if (phydev->autoneg == AUTONEG_DISABLE)
376 						change_autoneg = true;
377 					phydev->autoneg = AUTONEG_ENABLE;
378 				}
379 				break;
380 			case MII_ADVERTISE:
381 				mii_adv_mod_linkmode_adv_t(phydev->advertising,
382 							   val);
383 				change_autoneg = true;
384 				break;
385 			case MII_CTRL1000:
386 				mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
387 							        val);
388 				change_autoneg = true;
389 				break;
390 			default:
391 				/* do nothing */
392 				break;
393 			}
394 		}
395 
396 		if (mdio_phy_id_is_c45(mii_data->phy_id))
397 			mdiobus_c45_write(phydev->mdio.bus, prtad, devad,
398 					  mii_data->reg_num, val);
399 		else
400 			mdiobus_write(phydev->mdio.bus, prtad, devad, val);
401 
402 		if (prtad == phydev->mdio.addr &&
403 		    devad == MII_BMCR &&
404 		    val & BMCR_RESET)
405 			return phy_init_hw(phydev);
406 
407 		if (change_autoneg)
408 			return phy_start_aneg(phydev);
409 
410 		return 0;
411 
412 	case SIOCSHWTSTAMP:
413 		if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
414 			return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
415 		fallthrough;
416 
417 	default:
418 		return -EOPNOTSUPP;
419 	}
420 }
421 EXPORT_SYMBOL(phy_mii_ioctl);
422 
423 /**
424  * phy_do_ioctl - generic ndo_eth_ioctl implementation
425  * @dev: the net_device struct
426  * @ifr: &struct ifreq for socket ioctl's
427  * @cmd: ioctl cmd to execute
428  */
429 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
430 {
431 	if (!dev->phydev)
432 		return -ENODEV;
433 
434 	return phy_mii_ioctl(dev->phydev, ifr, cmd);
435 }
436 EXPORT_SYMBOL(phy_do_ioctl);
437 
438 /**
439  * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first
440  *
441  * @dev: the net_device struct
442  * @ifr: &struct ifreq for socket ioctl's
443  * @cmd: ioctl cmd to execute
444  *
445  * Same as phy_do_ioctl, but ensures that net_device is running before
446  * handling the ioctl.
447  */
448 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
449 {
450 	if (!netif_running(dev))
451 		return -ENODEV;
452 
453 	return phy_do_ioctl(dev, ifr, cmd);
454 }
455 EXPORT_SYMBOL(phy_do_ioctl_running);
456 
457 /**
458  * phy_queue_state_machine - Trigger the state machine to run soon
459  *
460  * @phydev: the phy_device struct
461  * @jiffies: Run the state machine after these jiffies
462  */
463 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
464 {
465 	mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
466 			 jiffies);
467 }
468 EXPORT_SYMBOL(phy_queue_state_machine);
469 
470 /**
471  * phy_trigger_machine - Trigger the state machine to run now
472  *
473  * @phydev: the phy_device struct
474  */
475 void phy_trigger_machine(struct phy_device *phydev)
476 {
477 	phy_queue_state_machine(phydev, 0);
478 }
479 EXPORT_SYMBOL(phy_trigger_machine);
480 
481 static void phy_abort_cable_test(struct phy_device *phydev)
482 {
483 	int err;
484 
485 	ethnl_cable_test_finished(phydev);
486 
487 	err = phy_init_hw(phydev);
488 	if (err)
489 		phydev_err(phydev, "Error while aborting cable test");
490 }
491 
492 /**
493  * phy_ethtool_get_strings - Get the statistic counter names
494  *
495  * @phydev: the phy_device struct
496  * @data: Where to put the strings
497  */
498 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
499 {
500 	if (!phydev->drv)
501 		return -EIO;
502 
503 	mutex_lock(&phydev->lock);
504 	phydev->drv->get_strings(phydev, data);
505 	mutex_unlock(&phydev->lock);
506 
507 	return 0;
508 }
509 EXPORT_SYMBOL(phy_ethtool_get_strings);
510 
511 /**
512  * phy_ethtool_get_sset_count - Get the number of statistic counters
513  *
514  * @phydev: the phy_device struct
515  */
516 int phy_ethtool_get_sset_count(struct phy_device *phydev)
517 {
518 	int ret;
519 
520 	if (!phydev->drv)
521 		return -EIO;
522 
523 	if (phydev->drv->get_sset_count &&
524 	    phydev->drv->get_strings &&
525 	    phydev->drv->get_stats) {
526 		mutex_lock(&phydev->lock);
527 		ret = phydev->drv->get_sset_count(phydev);
528 		mutex_unlock(&phydev->lock);
529 
530 		return ret;
531 	}
532 
533 	return -EOPNOTSUPP;
534 }
535 EXPORT_SYMBOL(phy_ethtool_get_sset_count);
536 
537 /**
538  * phy_ethtool_get_stats - Get the statistic counters
539  *
540  * @phydev: the phy_device struct
541  * @stats: What counters to get
542  * @data: Where to store the counters
543  */
544 int phy_ethtool_get_stats(struct phy_device *phydev,
545 			  struct ethtool_stats *stats, u64 *data)
546 {
547 	if (!phydev->drv)
548 		return -EIO;
549 
550 	mutex_lock(&phydev->lock);
551 	phydev->drv->get_stats(phydev, stats, data);
552 	mutex_unlock(&phydev->lock);
553 
554 	return 0;
555 }
556 EXPORT_SYMBOL(phy_ethtool_get_stats);
557 
558 /**
559  * phy_ethtool_get_plca_cfg - Get PLCA RS configuration
560  * @phydev: the phy_device struct
561  * @plca_cfg: where to store the retrieved configuration
562  *
563  * Retrieve the PLCA configuration from the PHY. Return 0 on success or a
564  * negative value if an error occurred.
565  */
566 int phy_ethtool_get_plca_cfg(struct phy_device *phydev,
567 			     struct phy_plca_cfg *plca_cfg)
568 {
569 	int ret;
570 
571 	if (!phydev->drv) {
572 		ret = -EIO;
573 		goto out;
574 	}
575 
576 	if (!phydev->drv->get_plca_cfg) {
577 		ret = -EOPNOTSUPP;
578 		goto out;
579 	}
580 
581 	mutex_lock(&phydev->lock);
582 	ret = phydev->drv->get_plca_cfg(phydev, plca_cfg);
583 
584 	mutex_unlock(&phydev->lock);
585 out:
586 	return ret;
587 }
588 
589 /**
590  * plca_check_valid - Check PLCA configuration before enabling
591  * @phydev: the phy_device struct
592  * @plca_cfg: current PLCA configuration
593  * @extack: extack for reporting useful error messages
594  *
595  * Checks whether the PLCA and PHY configuration are consistent and it is safe
596  * to enable PLCA. Returns 0 on success or a negative value if the PLCA or PHY
597  * configuration is not consistent.
598  */
599 static int plca_check_valid(struct phy_device *phydev,
600 			    const struct phy_plca_cfg *plca_cfg,
601 			    struct netlink_ext_ack *extack)
602 {
603 	int ret = 0;
604 
605 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT,
606 			       phydev->advertising)) {
607 		ret = -EOPNOTSUPP;
608 		NL_SET_ERR_MSG(extack,
609 			       "Point to Multi-Point mode is not enabled");
610 	} else if (plca_cfg->node_id >= 255) {
611 		NL_SET_ERR_MSG(extack, "PLCA node ID is not set");
612 		ret = -EINVAL;
613 	}
614 
615 	return ret;
616 }
617 
618 /**
619  * phy_ethtool_set_plca_cfg - Set PLCA RS configuration
620  * @phydev: the phy_device struct
621  * @plca_cfg: new PLCA configuration to apply
622  * @extack: extack for reporting useful error messages
623  *
624  * Sets the PLCA configuration in the PHY. Return 0 on success or a
625  * negative value if an error occurred.
626  */
627 int phy_ethtool_set_plca_cfg(struct phy_device *phydev,
628 			     const struct phy_plca_cfg *plca_cfg,
629 			     struct netlink_ext_ack *extack)
630 {
631 	struct phy_plca_cfg *curr_plca_cfg;
632 	int ret;
633 
634 	if (!phydev->drv) {
635 		ret = -EIO;
636 		goto out;
637 	}
638 
639 	if (!phydev->drv->set_plca_cfg ||
640 	    !phydev->drv->get_plca_cfg) {
641 		ret = -EOPNOTSUPP;
642 		goto out;
643 	}
644 
645 	curr_plca_cfg = kmalloc(sizeof(*curr_plca_cfg), GFP_KERNEL);
646 	if (!curr_plca_cfg) {
647 		ret = -ENOMEM;
648 		goto out;
649 	}
650 
651 	mutex_lock(&phydev->lock);
652 
653 	ret = phydev->drv->get_plca_cfg(phydev, curr_plca_cfg);
654 	if (ret)
655 		goto out_drv;
656 
657 	if (curr_plca_cfg->enabled < 0 && plca_cfg->enabled >= 0) {
658 		NL_SET_ERR_MSG(extack,
659 			       "PHY does not support changing the PLCA 'enable' attribute");
660 		ret = -EINVAL;
661 		goto out_drv;
662 	}
663 
664 	if (curr_plca_cfg->node_id < 0 && plca_cfg->node_id >= 0) {
665 		NL_SET_ERR_MSG(extack,
666 			       "PHY does not support changing the PLCA 'local node ID' attribute");
667 		ret = -EINVAL;
668 		goto out_drv;
669 	}
670 
671 	if (curr_plca_cfg->node_cnt < 0 && plca_cfg->node_cnt >= 0) {
672 		NL_SET_ERR_MSG(extack,
673 			       "PHY does not support changing the PLCA 'node count' attribute");
674 		ret = -EINVAL;
675 		goto out_drv;
676 	}
677 
678 	if (curr_plca_cfg->to_tmr < 0 && plca_cfg->to_tmr >= 0) {
679 		NL_SET_ERR_MSG(extack,
680 			       "PHY does not support changing the PLCA 'TO timer' attribute");
681 		ret = -EINVAL;
682 		goto out_drv;
683 	}
684 
685 	if (curr_plca_cfg->burst_cnt < 0 && plca_cfg->burst_cnt >= 0) {
686 		NL_SET_ERR_MSG(extack,
687 			       "PHY does not support changing the PLCA 'burst count' attribute");
688 		ret = -EINVAL;
689 		goto out_drv;
690 	}
691 
692 	if (curr_plca_cfg->burst_tmr < 0 && plca_cfg->burst_tmr >= 0) {
693 		NL_SET_ERR_MSG(extack,
694 			       "PHY does not support changing the PLCA 'burst timer' attribute");
695 		ret = -EINVAL;
696 		goto out_drv;
697 	}
698 
699 	// if enabling PLCA, perform a few sanity checks
700 	if (plca_cfg->enabled > 0) {
701 		// allow setting node_id concurrently with enabled
702 		if (plca_cfg->node_id >= 0)
703 			curr_plca_cfg->node_id = plca_cfg->node_id;
704 
705 		ret = plca_check_valid(phydev, curr_plca_cfg, extack);
706 		if (ret)
707 			goto out_drv;
708 	}
709 
710 	ret = phydev->drv->set_plca_cfg(phydev, plca_cfg);
711 
712 out_drv:
713 	kfree(curr_plca_cfg);
714 	mutex_unlock(&phydev->lock);
715 out:
716 	return ret;
717 }
718 
719 /**
720  * phy_ethtool_get_plca_status - Get PLCA RS status information
721  * @phydev: the phy_device struct
722  * @plca_st: where to store the retrieved status information
723  *
724  * Retrieve the PLCA status information from the PHY. Return 0 on success or a
725  * negative value if an error occurred.
726  */
727 int phy_ethtool_get_plca_status(struct phy_device *phydev,
728 				struct phy_plca_status *plca_st)
729 {
730 	int ret;
731 
732 	if (!phydev->drv) {
733 		ret = -EIO;
734 		goto out;
735 	}
736 
737 	if (!phydev->drv->get_plca_status) {
738 		ret = -EOPNOTSUPP;
739 		goto out;
740 	}
741 
742 	mutex_lock(&phydev->lock);
743 	ret = phydev->drv->get_plca_status(phydev, plca_st);
744 
745 	mutex_unlock(&phydev->lock);
746 out:
747 	return ret;
748 }
749 
750 /**
751  * phy_start_cable_test - Start a cable test
752  *
753  * @phydev: the phy_device struct
754  * @extack: extack for reporting useful error messages
755  */
756 int phy_start_cable_test(struct phy_device *phydev,
757 			 struct netlink_ext_ack *extack)
758 {
759 	struct net_device *dev = phydev->attached_dev;
760 	int err = -ENOMEM;
761 
762 	if (!(phydev->drv &&
763 	      phydev->drv->cable_test_start &&
764 	      phydev->drv->cable_test_get_status)) {
765 		NL_SET_ERR_MSG(extack,
766 			       "PHY driver does not support cable testing");
767 		return -EOPNOTSUPP;
768 	}
769 
770 	mutex_lock(&phydev->lock);
771 	if (phydev->state == PHY_CABLETEST) {
772 		NL_SET_ERR_MSG(extack,
773 			       "PHY already performing a test");
774 		err = -EBUSY;
775 		goto out;
776 	}
777 
778 	if (phydev->state < PHY_UP ||
779 	    phydev->state > PHY_CABLETEST) {
780 		NL_SET_ERR_MSG(extack,
781 			       "PHY not configured. Try setting interface up");
782 		err = -EBUSY;
783 		goto out;
784 	}
785 
786 	err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
787 	if (err)
788 		goto out;
789 
790 	/* Mark the carrier down until the test is complete */
791 	phy_link_down(phydev);
792 
793 	netif_testing_on(dev);
794 	err = phydev->drv->cable_test_start(phydev);
795 	if (err) {
796 		netif_testing_off(dev);
797 		phy_link_up(phydev);
798 		goto out_free;
799 	}
800 
801 	phydev->state = PHY_CABLETEST;
802 
803 	if (phy_polling_mode(phydev))
804 		phy_trigger_machine(phydev);
805 
806 	mutex_unlock(&phydev->lock);
807 
808 	return 0;
809 
810 out_free:
811 	ethnl_cable_test_free(phydev);
812 out:
813 	mutex_unlock(&phydev->lock);
814 
815 	return err;
816 }
817 EXPORT_SYMBOL(phy_start_cable_test);
818 
819 /**
820  * phy_start_cable_test_tdr - Start a raw TDR cable test
821  *
822  * @phydev: the phy_device struct
823  * @extack: extack for reporting useful error messages
824  * @config: Configuration of the test to run
825  */
826 int phy_start_cable_test_tdr(struct phy_device *phydev,
827 			     struct netlink_ext_ack *extack,
828 			     const struct phy_tdr_config *config)
829 {
830 	struct net_device *dev = phydev->attached_dev;
831 	int err = -ENOMEM;
832 
833 	if (!(phydev->drv &&
834 	      phydev->drv->cable_test_tdr_start &&
835 	      phydev->drv->cable_test_get_status)) {
836 		NL_SET_ERR_MSG(extack,
837 			       "PHY driver does not support cable test TDR");
838 		return -EOPNOTSUPP;
839 	}
840 
841 	mutex_lock(&phydev->lock);
842 	if (phydev->state == PHY_CABLETEST) {
843 		NL_SET_ERR_MSG(extack,
844 			       "PHY already performing a test");
845 		err = -EBUSY;
846 		goto out;
847 	}
848 
849 	if (phydev->state < PHY_UP ||
850 	    phydev->state > PHY_CABLETEST) {
851 		NL_SET_ERR_MSG(extack,
852 			       "PHY not configured. Try setting interface up");
853 		err = -EBUSY;
854 		goto out;
855 	}
856 
857 	err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
858 	if (err)
859 		goto out;
860 
861 	/* Mark the carrier down until the test is complete */
862 	phy_link_down(phydev);
863 
864 	netif_testing_on(dev);
865 	err = phydev->drv->cable_test_tdr_start(phydev, config);
866 	if (err) {
867 		netif_testing_off(dev);
868 		phy_link_up(phydev);
869 		goto out_free;
870 	}
871 
872 	phydev->state = PHY_CABLETEST;
873 
874 	if (phy_polling_mode(phydev))
875 		phy_trigger_machine(phydev);
876 
877 	mutex_unlock(&phydev->lock);
878 
879 	return 0;
880 
881 out_free:
882 	ethnl_cable_test_free(phydev);
883 out:
884 	mutex_unlock(&phydev->lock);
885 
886 	return err;
887 }
888 EXPORT_SYMBOL(phy_start_cable_test_tdr);
889 
890 int phy_config_aneg(struct phy_device *phydev)
891 {
892 	if (phydev->drv->config_aneg)
893 		return phydev->drv->config_aneg(phydev);
894 
895 	/* Clause 45 PHYs that don't implement Clause 22 registers are not
896 	 * allowed to call genphy_config_aneg()
897 	 */
898 	if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
899 		return genphy_c45_config_aneg(phydev);
900 
901 	return genphy_config_aneg(phydev);
902 }
903 EXPORT_SYMBOL(phy_config_aneg);
904 
905 /**
906  * phy_check_link_status - check link status and set state accordingly
907  * @phydev: the phy_device struct
908  *
909  * Description: Check for link and whether autoneg was triggered / is running
910  * and set state accordingly
911  */
912 static int phy_check_link_status(struct phy_device *phydev)
913 {
914 	int err;
915 
916 	lockdep_assert_held(&phydev->lock);
917 
918 	/* Keep previous state if loopback is enabled because some PHYs
919 	 * report that Link is Down when loopback is enabled.
920 	 */
921 	if (phydev->loopback_enabled)
922 		return 0;
923 
924 	err = phy_read_status(phydev);
925 	if (err)
926 		return err;
927 
928 	if (phydev->link && phydev->state != PHY_RUNNING) {
929 		phy_check_downshift(phydev);
930 		phydev->state = PHY_RUNNING;
931 		phy_link_up(phydev);
932 	} else if (!phydev->link && phydev->state != PHY_NOLINK) {
933 		phydev->state = PHY_NOLINK;
934 		phy_link_down(phydev);
935 	}
936 
937 	return 0;
938 }
939 
940 /**
941  * _phy_start_aneg - start auto-negotiation for this PHY device
942  * @phydev: the phy_device struct
943  *
944  * Description: Sanitizes the settings (if we're not autonegotiating
945  *   them), and then calls the driver's config_aneg function.
946  *   If the PHYCONTROL Layer is operating, we change the state to
947  *   reflect the beginning of Auto-negotiation or forcing.
948  */
949 static int _phy_start_aneg(struct phy_device *phydev)
950 {
951 	int err;
952 
953 	lockdep_assert_held(&phydev->lock);
954 
955 	if (!phydev->drv)
956 		return -EIO;
957 
958 	if (AUTONEG_DISABLE == phydev->autoneg)
959 		phy_sanitize_settings(phydev);
960 
961 	err = phy_config_aneg(phydev);
962 	if (err < 0)
963 		return err;
964 
965 	if (phy_is_started(phydev))
966 		err = phy_check_link_status(phydev);
967 
968 	return err;
969 }
970 
971 /**
972  * phy_start_aneg - start auto-negotiation for this PHY device
973  * @phydev: the phy_device struct
974  *
975  * Description: Sanitizes the settings (if we're not autonegotiating
976  *   them), and then calls the driver's config_aneg function.
977  *   If the PHYCONTROL Layer is operating, we change the state to
978  *   reflect the beginning of Auto-negotiation or forcing.
979  */
980 int phy_start_aneg(struct phy_device *phydev)
981 {
982 	int err;
983 
984 	mutex_lock(&phydev->lock);
985 	err = _phy_start_aneg(phydev);
986 	mutex_unlock(&phydev->lock);
987 
988 	return err;
989 }
990 EXPORT_SYMBOL(phy_start_aneg);
991 
992 static int phy_poll_aneg_done(struct phy_device *phydev)
993 {
994 	unsigned int retries = 100;
995 	int ret;
996 
997 	do {
998 		msleep(100);
999 		ret = phy_aneg_done(phydev);
1000 	} while (!ret && --retries);
1001 
1002 	if (!ret)
1003 		return -ETIMEDOUT;
1004 
1005 	return ret < 0 ? ret : 0;
1006 }
1007 
1008 int phy_ethtool_ksettings_set(struct phy_device *phydev,
1009 			      const struct ethtool_link_ksettings *cmd)
1010 {
1011 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
1012 	u8 autoneg = cmd->base.autoneg;
1013 	u8 duplex = cmd->base.duplex;
1014 	u32 speed = cmd->base.speed;
1015 
1016 	if (cmd->base.phy_address != phydev->mdio.addr)
1017 		return -EINVAL;
1018 
1019 	linkmode_copy(advertising, cmd->link_modes.advertising);
1020 
1021 	/* We make sure that we don't pass unsupported values in to the PHY */
1022 	linkmode_and(advertising, advertising, phydev->supported);
1023 
1024 	/* Verify the settings we care about. */
1025 	if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
1026 		return -EINVAL;
1027 
1028 	if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
1029 		return -EINVAL;
1030 
1031 	if (autoneg == AUTONEG_DISABLE &&
1032 	    ((speed != SPEED_1000 &&
1033 	      speed != SPEED_100 &&
1034 	      speed != SPEED_10) ||
1035 	     (duplex != DUPLEX_HALF &&
1036 	      duplex != DUPLEX_FULL)))
1037 		return -EINVAL;
1038 
1039 	mutex_lock(&phydev->lock);
1040 	phydev->autoneg = autoneg;
1041 
1042 	if (autoneg == AUTONEG_DISABLE) {
1043 		phydev->speed = speed;
1044 		phydev->duplex = duplex;
1045 	}
1046 
1047 	linkmode_copy(phydev->advertising, advertising);
1048 
1049 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1050 			 phydev->advertising, autoneg == AUTONEG_ENABLE);
1051 
1052 	phydev->master_slave_set = cmd->base.master_slave_cfg;
1053 	phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
1054 
1055 	/* Restart the PHY */
1056 	if (phy_is_started(phydev)) {
1057 		phydev->state = PHY_UP;
1058 		phy_trigger_machine(phydev);
1059 	} else {
1060 		_phy_start_aneg(phydev);
1061 	}
1062 
1063 	mutex_unlock(&phydev->lock);
1064 	return 0;
1065 }
1066 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
1067 
1068 /**
1069  * phy_speed_down - set speed to lowest speed supported by both link partners
1070  * @phydev: the phy_device struct
1071  * @sync: perform action synchronously
1072  *
1073  * Description: Typically used to save energy when waiting for a WoL packet
1074  *
1075  * WARNING: Setting sync to false may cause the system being unable to suspend
1076  * in case the PHY generates an interrupt when finishing the autonegotiation.
1077  * This interrupt may wake up the system immediately after suspend.
1078  * Therefore use sync = false only if you're sure it's safe with the respective
1079  * network chip.
1080  */
1081 int phy_speed_down(struct phy_device *phydev, bool sync)
1082 {
1083 	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
1084 	int ret = 0;
1085 
1086 	mutex_lock(&phydev->lock);
1087 
1088 	if (phydev->autoneg != AUTONEG_ENABLE)
1089 		goto out;
1090 
1091 	linkmode_copy(adv_tmp, phydev->advertising);
1092 
1093 	ret = phy_speed_down_core(phydev);
1094 	if (ret)
1095 		goto out;
1096 
1097 	linkmode_copy(phydev->adv_old, adv_tmp);
1098 
1099 	if (linkmode_equal(phydev->advertising, adv_tmp)) {
1100 		ret = 0;
1101 		goto out;
1102 	}
1103 
1104 	ret = phy_config_aneg(phydev);
1105 	if (ret)
1106 		goto out;
1107 
1108 	ret = sync ? phy_poll_aneg_done(phydev) : 0;
1109 out:
1110 	mutex_unlock(&phydev->lock);
1111 
1112 	return ret;
1113 }
1114 EXPORT_SYMBOL_GPL(phy_speed_down);
1115 
1116 /**
1117  * phy_speed_up - (re)set advertised speeds to all supported speeds
1118  * @phydev: the phy_device struct
1119  *
1120  * Description: Used to revert the effect of phy_speed_down
1121  */
1122 int phy_speed_up(struct phy_device *phydev)
1123 {
1124 	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
1125 	int ret = 0;
1126 
1127 	mutex_lock(&phydev->lock);
1128 
1129 	if (phydev->autoneg != AUTONEG_ENABLE)
1130 		goto out;
1131 
1132 	if (linkmode_empty(phydev->adv_old))
1133 		goto out;
1134 
1135 	linkmode_copy(adv_tmp, phydev->advertising);
1136 	linkmode_copy(phydev->advertising, phydev->adv_old);
1137 	linkmode_zero(phydev->adv_old);
1138 
1139 	if (linkmode_equal(phydev->advertising, adv_tmp))
1140 		goto out;
1141 
1142 	ret = phy_config_aneg(phydev);
1143 out:
1144 	mutex_unlock(&phydev->lock);
1145 
1146 	return ret;
1147 }
1148 EXPORT_SYMBOL_GPL(phy_speed_up);
1149 
1150 /**
1151  * phy_start_machine - start PHY state machine tracking
1152  * @phydev: the phy_device struct
1153  *
1154  * Description: The PHY infrastructure can run a state machine
1155  *   which tracks whether the PHY is starting up, negotiating,
1156  *   etc.  This function starts the delayed workqueue which tracks
1157  *   the state of the PHY. If you want to maintain your own state machine,
1158  *   do not call this function.
1159  */
1160 void phy_start_machine(struct phy_device *phydev)
1161 {
1162 	phy_trigger_machine(phydev);
1163 }
1164 EXPORT_SYMBOL_GPL(phy_start_machine);
1165 
1166 /**
1167  * phy_stop_machine - stop the PHY state machine tracking
1168  * @phydev: target phy_device struct
1169  *
1170  * Description: Stops the state machine delayed workqueue, sets the
1171  *   state to UP (unless it wasn't up yet). This function must be
1172  *   called BEFORE phy_detach.
1173  */
1174 void phy_stop_machine(struct phy_device *phydev)
1175 {
1176 	cancel_delayed_work_sync(&phydev->state_queue);
1177 
1178 	mutex_lock(&phydev->lock);
1179 	if (phy_is_started(phydev))
1180 		phydev->state = PHY_UP;
1181 	mutex_unlock(&phydev->lock);
1182 }
1183 
1184 /**
1185  * phy_error - enter HALTED state for this PHY device
1186  * @phydev: target phy_device struct
1187  *
1188  * Moves the PHY to the HALTED state in response to a read
1189  * or write error, and tells the controller the link is down.
1190  * Must not be called from interrupt context, or while the
1191  * phydev->lock is held.
1192  */
1193 void phy_error(struct phy_device *phydev)
1194 {
1195 	WARN_ON(1);
1196 
1197 	mutex_lock(&phydev->lock);
1198 	phydev->state = PHY_HALTED;
1199 	mutex_unlock(&phydev->lock);
1200 
1201 	phy_trigger_machine(phydev);
1202 }
1203 EXPORT_SYMBOL(phy_error);
1204 
1205 /**
1206  * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
1207  * @phydev: target phy_device struct
1208  */
1209 int phy_disable_interrupts(struct phy_device *phydev)
1210 {
1211 	/* Disable PHY interrupts */
1212 	return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
1213 }
1214 
1215 /**
1216  * phy_interrupt - PHY interrupt handler
1217  * @irq: interrupt line
1218  * @phy_dat: phy_device pointer
1219  *
1220  * Description: Handle PHY interrupt
1221  */
1222 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
1223 {
1224 	struct phy_device *phydev = phy_dat;
1225 	struct phy_driver *drv = phydev->drv;
1226 	irqreturn_t ret;
1227 
1228 	/* Wakeup interrupts may occur during a system sleep transition.
1229 	 * Postpone handling until the PHY has resumed.
1230 	 */
1231 	if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
1232 		struct net_device *netdev = phydev->attached_dev;
1233 
1234 		if (netdev) {
1235 			struct device *parent = netdev->dev.parent;
1236 
1237 			if (netdev->wol_enabled)
1238 				pm_system_wakeup();
1239 			else if (device_may_wakeup(&netdev->dev))
1240 				pm_wakeup_dev_event(&netdev->dev, 0, true);
1241 			else if (parent && device_may_wakeup(parent))
1242 				pm_wakeup_dev_event(parent, 0, true);
1243 		}
1244 
1245 		phydev->irq_rerun = 1;
1246 		disable_irq_nosync(irq);
1247 		return IRQ_HANDLED;
1248 	}
1249 
1250 	mutex_lock(&phydev->lock);
1251 	ret = drv->handle_interrupt(phydev);
1252 	mutex_unlock(&phydev->lock);
1253 
1254 	return ret;
1255 }
1256 
1257 /**
1258  * phy_enable_interrupts - Enable the interrupts from the PHY side
1259  * @phydev: target phy_device struct
1260  */
1261 static int phy_enable_interrupts(struct phy_device *phydev)
1262 {
1263 	return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1264 }
1265 
1266 /**
1267  * phy_request_interrupt - request and enable interrupt for a PHY device
1268  * @phydev: target phy_device struct
1269  *
1270  * Description: Request and enable the interrupt for the given PHY.
1271  *   If this fails, then we set irq to PHY_POLL.
1272  *   This should only be called with a valid IRQ number.
1273  */
1274 void phy_request_interrupt(struct phy_device *phydev)
1275 {
1276 	int err;
1277 
1278 	err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1279 				   IRQF_ONESHOT | IRQF_SHARED,
1280 				   phydev_name(phydev), phydev);
1281 	if (err) {
1282 		phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1283 			    err, phydev->irq);
1284 		phydev->irq = PHY_POLL;
1285 	} else {
1286 		if (phy_enable_interrupts(phydev)) {
1287 			phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1288 			phy_free_interrupt(phydev);
1289 			phydev->irq = PHY_POLL;
1290 		}
1291 	}
1292 }
1293 EXPORT_SYMBOL(phy_request_interrupt);
1294 
1295 /**
1296  * phy_free_interrupt - disable and free interrupt for a PHY device
1297  * @phydev: target phy_device struct
1298  *
1299  * Description: Disable and free the interrupt for the given PHY.
1300  *   This should only be called with a valid IRQ number.
1301  */
1302 void phy_free_interrupt(struct phy_device *phydev)
1303 {
1304 	phy_disable_interrupts(phydev);
1305 	free_irq(phydev->irq, phydev);
1306 }
1307 EXPORT_SYMBOL(phy_free_interrupt);
1308 
1309 /**
1310  * phy_stop - Bring down the PHY link, and stop checking the status
1311  * @phydev: target phy_device struct
1312  */
1313 void phy_stop(struct phy_device *phydev)
1314 {
1315 	struct net_device *dev = phydev->attached_dev;
1316 	enum phy_state old_state;
1317 
1318 	if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
1319 		WARN(1, "called from state %s\n",
1320 		     phy_state_to_str(phydev->state));
1321 		return;
1322 	}
1323 
1324 	mutex_lock(&phydev->lock);
1325 	old_state = phydev->state;
1326 
1327 	if (phydev->state == PHY_CABLETEST) {
1328 		phy_abort_cable_test(phydev);
1329 		netif_testing_off(dev);
1330 	}
1331 
1332 	if (phydev->sfp_bus)
1333 		sfp_upstream_stop(phydev->sfp_bus);
1334 
1335 	phydev->state = PHY_HALTED;
1336 	phy_process_state_change(phydev, old_state);
1337 
1338 	mutex_unlock(&phydev->lock);
1339 
1340 	phy_state_machine(&phydev->state_queue.work);
1341 	phy_stop_machine(phydev);
1342 
1343 	/* Cannot call flush_scheduled_work() here as desired because
1344 	 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
1345 	 * will not reenable interrupts.
1346 	 */
1347 }
1348 EXPORT_SYMBOL(phy_stop);
1349 
1350 /**
1351  * phy_start - start or restart a PHY device
1352  * @phydev: target phy_device struct
1353  *
1354  * Description: Indicates the attached device's readiness to
1355  *   handle PHY-related work.  Used during startup to start the
1356  *   PHY, and after a call to phy_stop() to resume operation.
1357  *   Also used to indicate the MDIO bus has cleared an error
1358  *   condition.
1359  */
1360 void phy_start(struct phy_device *phydev)
1361 {
1362 	mutex_lock(&phydev->lock);
1363 
1364 	if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1365 		WARN(1, "called from state %s\n",
1366 		     phy_state_to_str(phydev->state));
1367 		goto out;
1368 	}
1369 
1370 	if (phydev->sfp_bus)
1371 		sfp_upstream_start(phydev->sfp_bus);
1372 
1373 	/* if phy was suspended, bring the physical link up again */
1374 	__phy_resume(phydev);
1375 
1376 	phydev->state = PHY_UP;
1377 
1378 	phy_start_machine(phydev);
1379 out:
1380 	mutex_unlock(&phydev->lock);
1381 }
1382 EXPORT_SYMBOL(phy_start);
1383 
1384 /**
1385  * phy_state_machine - Handle the state machine
1386  * @work: work_struct that describes the work to be done
1387  */
1388 void phy_state_machine(struct work_struct *work)
1389 {
1390 	struct delayed_work *dwork = to_delayed_work(work);
1391 	struct phy_device *phydev =
1392 			container_of(dwork, struct phy_device, state_queue);
1393 	struct net_device *dev = phydev->attached_dev;
1394 	bool needs_aneg = false, do_suspend = false;
1395 	enum phy_state old_state;
1396 	bool finished = false;
1397 	int err = 0;
1398 
1399 	mutex_lock(&phydev->lock);
1400 
1401 	old_state = phydev->state;
1402 
1403 	switch (phydev->state) {
1404 	case PHY_DOWN:
1405 	case PHY_READY:
1406 		break;
1407 	case PHY_UP:
1408 		needs_aneg = true;
1409 
1410 		break;
1411 	case PHY_NOLINK:
1412 	case PHY_RUNNING:
1413 		err = phy_check_link_status(phydev);
1414 		break;
1415 	case PHY_CABLETEST:
1416 		err = phydev->drv->cable_test_get_status(phydev, &finished);
1417 		if (err) {
1418 			phy_abort_cable_test(phydev);
1419 			netif_testing_off(dev);
1420 			needs_aneg = true;
1421 			phydev->state = PHY_UP;
1422 			break;
1423 		}
1424 
1425 		if (finished) {
1426 			ethnl_cable_test_finished(phydev);
1427 			netif_testing_off(dev);
1428 			needs_aneg = true;
1429 			phydev->state = PHY_UP;
1430 		}
1431 		break;
1432 	case PHY_HALTED:
1433 		if (phydev->link) {
1434 			phydev->link = 0;
1435 			phy_link_down(phydev);
1436 		}
1437 		do_suspend = true;
1438 		break;
1439 	}
1440 
1441 	mutex_unlock(&phydev->lock);
1442 
1443 	if (needs_aneg)
1444 		err = phy_start_aneg(phydev);
1445 	else if (do_suspend)
1446 		phy_suspend(phydev);
1447 
1448 	if (err == -ENODEV)
1449 		return;
1450 
1451 	if (err < 0)
1452 		phy_error(phydev);
1453 
1454 	phy_process_state_change(phydev, old_state);
1455 
1456 	/* Only re-schedule a PHY state machine change if we are polling the
1457 	 * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
1458 	 * between states from phy_mac_interrupt().
1459 	 *
1460 	 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1461 	 * state machine would be pointless and possibly error prone when
1462 	 * called from phy_disconnect() synchronously.
1463 	 */
1464 	mutex_lock(&phydev->lock);
1465 	if (phy_polling_mode(phydev) && phy_is_started(phydev))
1466 		phy_queue_state_machine(phydev, PHY_STATE_TIME);
1467 	mutex_unlock(&phydev->lock);
1468 }
1469 
1470 /**
1471  * phy_mac_interrupt - MAC says the link has changed
1472  * @phydev: phy_device struct with changed link
1473  *
1474  * The MAC layer is able to indicate there has been a change in the PHY link
1475  * status. Trigger the state machine and work a work queue.
1476  */
1477 void phy_mac_interrupt(struct phy_device *phydev)
1478 {
1479 	/* Trigger a state machine change */
1480 	phy_trigger_machine(phydev);
1481 }
1482 EXPORT_SYMBOL(phy_mac_interrupt);
1483 
1484 /**
1485  * phy_init_eee - init and check the EEE feature
1486  * @phydev: target phy_device struct
1487  * @clk_stop_enable: PHY may stop the clock during LPI
1488  *
1489  * Description: it checks if the Energy-Efficient Ethernet (EEE)
1490  * is supported by looking at the MMD registers 3.20 and 7.60/61
1491  * and it programs the MMD register 3.0 setting the "Clock stop enable"
1492  * bit if required.
1493  */
1494 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1495 {
1496 	int ret;
1497 
1498 	if (!phydev->drv)
1499 		return -EIO;
1500 
1501 	ret = genphy_c45_eee_is_active(phydev, NULL, NULL, NULL);
1502 	if (ret < 0)
1503 		return ret;
1504 	if (!ret)
1505 		return -EPROTONOSUPPORT;
1506 
1507 	if (clk_stop_enable)
1508 		/* Configure the PHY to stop receiving xMII
1509 		 * clock while it is signaling LPI.
1510 		 */
1511 		ret = phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1512 				       MDIO_PCS_CTRL1_CLKSTOP_EN);
1513 
1514 	return ret < 0 ? ret : 0;
1515 }
1516 EXPORT_SYMBOL(phy_init_eee);
1517 
1518 /**
1519  * phy_get_eee_err - report the EEE wake error count
1520  * @phydev: target phy_device struct
1521  *
1522  * Description: it is to report the number of time where the PHY
1523  * failed to complete its normal wake sequence.
1524  */
1525 int phy_get_eee_err(struct phy_device *phydev)
1526 {
1527 	int ret;
1528 
1529 	if (!phydev->drv)
1530 		return -EIO;
1531 
1532 	mutex_lock(&phydev->lock);
1533 	ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1534 	mutex_unlock(&phydev->lock);
1535 
1536 	return ret;
1537 }
1538 EXPORT_SYMBOL(phy_get_eee_err);
1539 
1540 /**
1541  * phy_ethtool_get_eee - get EEE supported and status
1542  * @phydev: target phy_device struct
1543  * @data: ethtool_eee data
1544  *
1545  * Description: it reportes the Supported/Advertisement/LP Advertisement
1546  * capabilities.
1547  */
1548 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1549 {
1550 	int ret;
1551 
1552 	if (!phydev->drv)
1553 		return -EIO;
1554 
1555 	mutex_lock(&phydev->lock);
1556 	ret = genphy_c45_ethtool_get_eee(phydev, data);
1557 	mutex_unlock(&phydev->lock);
1558 
1559 	return ret;
1560 }
1561 EXPORT_SYMBOL(phy_ethtool_get_eee);
1562 
1563 /**
1564  * phy_ethtool_set_eee - set EEE supported and status
1565  * @phydev: target phy_device struct
1566  * @data: ethtool_eee data
1567  *
1568  * Description: it is to program the Advertisement EEE register.
1569  */
1570 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1571 {
1572 	int ret;
1573 
1574 	if (!phydev->drv)
1575 		return -EIO;
1576 
1577 	mutex_lock(&phydev->lock);
1578 	ret = genphy_c45_ethtool_set_eee(phydev, data);
1579 	mutex_unlock(&phydev->lock);
1580 
1581 	return ret;
1582 }
1583 EXPORT_SYMBOL(phy_ethtool_set_eee);
1584 
1585 /**
1586  * phy_ethtool_set_wol - Configure Wake On LAN
1587  *
1588  * @phydev: target phy_device struct
1589  * @wol: Configuration requested
1590  */
1591 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1592 {
1593 	int ret;
1594 
1595 	if (phydev->drv && phydev->drv->set_wol) {
1596 		mutex_lock(&phydev->lock);
1597 		ret = phydev->drv->set_wol(phydev, wol);
1598 		mutex_unlock(&phydev->lock);
1599 
1600 		return ret;
1601 	}
1602 
1603 	return -EOPNOTSUPP;
1604 }
1605 EXPORT_SYMBOL(phy_ethtool_set_wol);
1606 
1607 /**
1608  * phy_ethtool_get_wol - Get the current Wake On LAN configuration
1609  *
1610  * @phydev: target phy_device struct
1611  * @wol: Store the current configuration here
1612  */
1613 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1614 {
1615 	if (phydev->drv && phydev->drv->get_wol) {
1616 		mutex_lock(&phydev->lock);
1617 		phydev->drv->get_wol(phydev, wol);
1618 		mutex_unlock(&phydev->lock);
1619 	}
1620 }
1621 EXPORT_SYMBOL(phy_ethtool_get_wol);
1622 
1623 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1624 				   struct ethtool_link_ksettings *cmd)
1625 {
1626 	struct phy_device *phydev = ndev->phydev;
1627 
1628 	if (!phydev)
1629 		return -ENODEV;
1630 
1631 	phy_ethtool_ksettings_get(phydev, cmd);
1632 
1633 	return 0;
1634 }
1635 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1636 
1637 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1638 				   const struct ethtool_link_ksettings *cmd)
1639 {
1640 	struct phy_device *phydev = ndev->phydev;
1641 
1642 	if (!phydev)
1643 		return -ENODEV;
1644 
1645 	return phy_ethtool_ksettings_set(phydev, cmd);
1646 }
1647 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1648 
1649 /**
1650  * phy_ethtool_nway_reset - Restart auto negotiation
1651  * @ndev: Network device to restart autoneg for
1652  */
1653 int phy_ethtool_nway_reset(struct net_device *ndev)
1654 {
1655 	struct phy_device *phydev = ndev->phydev;
1656 	int ret;
1657 
1658 	if (!phydev)
1659 		return -ENODEV;
1660 
1661 	if (!phydev->drv)
1662 		return -EIO;
1663 
1664 	mutex_lock(&phydev->lock);
1665 	ret = phy_restart_aneg(phydev);
1666 	mutex_unlock(&phydev->lock);
1667 
1668 	return ret;
1669 }
1670 EXPORT_SYMBOL(phy_ethtool_nway_reset);
1671