xref: /openbmc/linux/drivers/net/phy/phy.c (revision 35e6bcd1)
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 static void phy_process_error(struct phy_device *phydev)
1185 {
1186 	mutex_lock(&phydev->lock);
1187 	phydev->state = PHY_HALTED;
1188 	mutex_unlock(&phydev->lock);
1189 
1190 	phy_trigger_machine(phydev);
1191 }
1192 
1193 static void phy_error_precise(struct phy_device *phydev,
1194 			      const void *func, int err)
1195 {
1196 	WARN(1, "%pS: returned: %d\n", func, err);
1197 	phy_process_error(phydev);
1198 }
1199 
1200 /**
1201  * phy_error - enter HALTED state for this PHY device
1202  * @phydev: target phy_device struct
1203  *
1204  * Moves the PHY to the HALTED state in response to a read
1205  * or write error, and tells the controller the link is down.
1206  * Must not be called from interrupt context, or while the
1207  * phydev->lock is held.
1208  */
1209 void phy_error(struct phy_device *phydev)
1210 {
1211 	WARN_ON(1);
1212 	phy_process_error(phydev);
1213 }
1214 EXPORT_SYMBOL(phy_error);
1215 
1216 /**
1217  * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
1218  * @phydev: target phy_device struct
1219  */
1220 int phy_disable_interrupts(struct phy_device *phydev)
1221 {
1222 	/* Disable PHY interrupts */
1223 	return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
1224 }
1225 
1226 /**
1227  * phy_interrupt - PHY interrupt handler
1228  * @irq: interrupt line
1229  * @phy_dat: phy_device pointer
1230  *
1231  * Description: Handle PHY interrupt
1232  */
1233 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
1234 {
1235 	struct phy_device *phydev = phy_dat;
1236 	struct phy_driver *drv = phydev->drv;
1237 	irqreturn_t ret;
1238 
1239 	/* Wakeup interrupts may occur during a system sleep transition.
1240 	 * Postpone handling until the PHY has resumed.
1241 	 */
1242 	if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
1243 		struct net_device *netdev = phydev->attached_dev;
1244 
1245 		if (netdev) {
1246 			struct device *parent = netdev->dev.parent;
1247 
1248 			if (netdev->wol_enabled)
1249 				pm_system_wakeup();
1250 			else if (device_may_wakeup(&netdev->dev))
1251 				pm_wakeup_dev_event(&netdev->dev, 0, true);
1252 			else if (parent && device_may_wakeup(parent))
1253 				pm_wakeup_dev_event(parent, 0, true);
1254 		}
1255 
1256 		phydev->irq_rerun = 1;
1257 		disable_irq_nosync(irq);
1258 		return IRQ_HANDLED;
1259 	}
1260 
1261 	mutex_lock(&phydev->lock);
1262 	ret = drv->handle_interrupt(phydev);
1263 	mutex_unlock(&phydev->lock);
1264 
1265 	return ret;
1266 }
1267 
1268 /**
1269  * phy_enable_interrupts - Enable the interrupts from the PHY side
1270  * @phydev: target phy_device struct
1271  */
1272 static int phy_enable_interrupts(struct phy_device *phydev)
1273 {
1274 	return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1275 }
1276 
1277 /**
1278  * phy_request_interrupt - request and enable interrupt for a PHY device
1279  * @phydev: target phy_device struct
1280  *
1281  * Description: Request and enable the interrupt for the given PHY.
1282  *   If this fails, then we set irq to PHY_POLL.
1283  *   This should only be called with a valid IRQ number.
1284  */
1285 void phy_request_interrupt(struct phy_device *phydev)
1286 {
1287 	int err;
1288 
1289 	err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1290 				   IRQF_ONESHOT | IRQF_SHARED,
1291 				   phydev_name(phydev), phydev);
1292 	if (err) {
1293 		phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1294 			    err, phydev->irq);
1295 		phydev->irq = PHY_POLL;
1296 	} else {
1297 		if (phy_enable_interrupts(phydev)) {
1298 			phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1299 			phy_free_interrupt(phydev);
1300 			phydev->irq = PHY_POLL;
1301 		}
1302 	}
1303 }
1304 EXPORT_SYMBOL(phy_request_interrupt);
1305 
1306 /**
1307  * phy_free_interrupt - disable and free interrupt for a PHY device
1308  * @phydev: target phy_device struct
1309  *
1310  * Description: Disable and free the interrupt for the given PHY.
1311  *   This should only be called with a valid IRQ number.
1312  */
1313 void phy_free_interrupt(struct phy_device *phydev)
1314 {
1315 	phy_disable_interrupts(phydev);
1316 	free_irq(phydev->irq, phydev);
1317 }
1318 EXPORT_SYMBOL(phy_free_interrupt);
1319 
1320 /**
1321  * phy_stop - Bring down the PHY link, and stop checking the status
1322  * @phydev: target phy_device struct
1323  */
1324 void phy_stop(struct phy_device *phydev)
1325 {
1326 	struct net_device *dev = phydev->attached_dev;
1327 	enum phy_state old_state;
1328 
1329 	if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
1330 		WARN(1, "called from state %s\n",
1331 		     phy_state_to_str(phydev->state));
1332 		return;
1333 	}
1334 
1335 	mutex_lock(&phydev->lock);
1336 	old_state = phydev->state;
1337 
1338 	if (phydev->state == PHY_CABLETEST) {
1339 		phy_abort_cable_test(phydev);
1340 		netif_testing_off(dev);
1341 	}
1342 
1343 	if (phydev->sfp_bus)
1344 		sfp_upstream_stop(phydev->sfp_bus);
1345 
1346 	phydev->state = PHY_HALTED;
1347 	phy_process_state_change(phydev, old_state);
1348 
1349 	mutex_unlock(&phydev->lock);
1350 
1351 	phy_state_machine(&phydev->state_queue.work);
1352 	phy_stop_machine(phydev);
1353 
1354 	/* Cannot call flush_scheduled_work() here as desired because
1355 	 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
1356 	 * will not reenable interrupts.
1357 	 */
1358 }
1359 EXPORT_SYMBOL(phy_stop);
1360 
1361 /**
1362  * phy_start - start or restart a PHY device
1363  * @phydev: target phy_device struct
1364  *
1365  * Description: Indicates the attached device's readiness to
1366  *   handle PHY-related work.  Used during startup to start the
1367  *   PHY, and after a call to phy_stop() to resume operation.
1368  *   Also used to indicate the MDIO bus has cleared an error
1369  *   condition.
1370  */
1371 void phy_start(struct phy_device *phydev)
1372 {
1373 	mutex_lock(&phydev->lock);
1374 
1375 	if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1376 		WARN(1, "called from state %s\n",
1377 		     phy_state_to_str(phydev->state));
1378 		goto out;
1379 	}
1380 
1381 	if (phydev->sfp_bus)
1382 		sfp_upstream_start(phydev->sfp_bus);
1383 
1384 	/* if phy was suspended, bring the physical link up again */
1385 	__phy_resume(phydev);
1386 
1387 	phydev->state = PHY_UP;
1388 
1389 	phy_start_machine(phydev);
1390 out:
1391 	mutex_unlock(&phydev->lock);
1392 }
1393 EXPORT_SYMBOL(phy_start);
1394 
1395 /**
1396  * phy_state_machine - Handle the state machine
1397  * @work: work_struct that describes the work to be done
1398  */
1399 void phy_state_machine(struct work_struct *work)
1400 {
1401 	struct delayed_work *dwork = to_delayed_work(work);
1402 	struct phy_device *phydev =
1403 			container_of(dwork, struct phy_device, state_queue);
1404 	struct net_device *dev = phydev->attached_dev;
1405 	bool needs_aneg = false, do_suspend = false;
1406 	enum phy_state old_state;
1407 	const void *func = NULL;
1408 	bool finished = false;
1409 	int err = 0;
1410 
1411 	mutex_lock(&phydev->lock);
1412 
1413 	old_state = phydev->state;
1414 
1415 	switch (phydev->state) {
1416 	case PHY_DOWN:
1417 	case PHY_READY:
1418 		break;
1419 	case PHY_UP:
1420 		needs_aneg = true;
1421 
1422 		break;
1423 	case PHY_NOLINK:
1424 	case PHY_RUNNING:
1425 		err = phy_check_link_status(phydev);
1426 		func = &phy_check_link_status;
1427 		break;
1428 	case PHY_CABLETEST:
1429 		err = phydev->drv->cable_test_get_status(phydev, &finished);
1430 		if (err) {
1431 			phy_abort_cable_test(phydev);
1432 			netif_testing_off(dev);
1433 			needs_aneg = true;
1434 			phydev->state = PHY_UP;
1435 			break;
1436 		}
1437 
1438 		if (finished) {
1439 			ethnl_cable_test_finished(phydev);
1440 			netif_testing_off(dev);
1441 			needs_aneg = true;
1442 			phydev->state = PHY_UP;
1443 		}
1444 		break;
1445 	case PHY_HALTED:
1446 		if (phydev->link) {
1447 			phydev->link = 0;
1448 			phy_link_down(phydev);
1449 		}
1450 		do_suspend = true;
1451 		break;
1452 	}
1453 
1454 	mutex_unlock(&phydev->lock);
1455 
1456 	if (needs_aneg) {
1457 		err = phy_start_aneg(phydev);
1458 		func = &phy_start_aneg;
1459 	} else if (do_suspend) {
1460 		phy_suspend(phydev);
1461 	}
1462 
1463 	if (err == -ENODEV)
1464 		return;
1465 
1466 	if (err < 0)
1467 		phy_error_precise(phydev, func, err);
1468 
1469 	phy_process_state_change(phydev, old_state);
1470 
1471 	/* Only re-schedule a PHY state machine change if we are polling the
1472 	 * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
1473 	 * between states from phy_mac_interrupt().
1474 	 *
1475 	 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1476 	 * state machine would be pointless and possibly error prone when
1477 	 * called from phy_disconnect() synchronously.
1478 	 */
1479 	mutex_lock(&phydev->lock);
1480 	if (phy_polling_mode(phydev) && phy_is_started(phydev))
1481 		phy_queue_state_machine(phydev, PHY_STATE_TIME);
1482 	mutex_unlock(&phydev->lock);
1483 }
1484 
1485 /**
1486  * phy_mac_interrupt - MAC says the link has changed
1487  * @phydev: phy_device struct with changed link
1488  *
1489  * The MAC layer is able to indicate there has been a change in the PHY link
1490  * status. Trigger the state machine and work a work queue.
1491  */
1492 void phy_mac_interrupt(struct phy_device *phydev)
1493 {
1494 	/* Trigger a state machine change */
1495 	phy_trigger_machine(phydev);
1496 }
1497 EXPORT_SYMBOL(phy_mac_interrupt);
1498 
1499 /**
1500  * phy_init_eee - init and check the EEE feature
1501  * @phydev: target phy_device struct
1502  * @clk_stop_enable: PHY may stop the clock during LPI
1503  *
1504  * Description: it checks if the Energy-Efficient Ethernet (EEE)
1505  * is supported by looking at the MMD registers 3.20 and 7.60/61
1506  * and it programs the MMD register 3.0 setting the "Clock stop enable"
1507  * bit if required.
1508  */
1509 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1510 {
1511 	int ret;
1512 
1513 	if (!phydev->drv)
1514 		return -EIO;
1515 
1516 	ret = genphy_c45_eee_is_active(phydev, NULL, NULL, NULL);
1517 	if (ret < 0)
1518 		return ret;
1519 	if (!ret)
1520 		return -EPROTONOSUPPORT;
1521 
1522 	if (clk_stop_enable)
1523 		/* Configure the PHY to stop receiving xMII
1524 		 * clock while it is signaling LPI.
1525 		 */
1526 		ret = phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1527 				       MDIO_PCS_CTRL1_CLKSTOP_EN);
1528 
1529 	return ret < 0 ? ret : 0;
1530 }
1531 EXPORT_SYMBOL(phy_init_eee);
1532 
1533 /**
1534  * phy_get_eee_err - report the EEE wake error count
1535  * @phydev: target phy_device struct
1536  *
1537  * Description: it is to report the number of time where the PHY
1538  * failed to complete its normal wake sequence.
1539  */
1540 int phy_get_eee_err(struct phy_device *phydev)
1541 {
1542 	int ret;
1543 
1544 	if (!phydev->drv)
1545 		return -EIO;
1546 
1547 	mutex_lock(&phydev->lock);
1548 	ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1549 	mutex_unlock(&phydev->lock);
1550 
1551 	return ret;
1552 }
1553 EXPORT_SYMBOL(phy_get_eee_err);
1554 
1555 /**
1556  * phy_ethtool_get_eee - get EEE supported and status
1557  * @phydev: target phy_device struct
1558  * @data: ethtool_eee data
1559  *
1560  * Description: it reportes the Supported/Advertisement/LP Advertisement
1561  * capabilities.
1562  */
1563 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1564 {
1565 	int ret;
1566 
1567 	if (!phydev->drv)
1568 		return -EIO;
1569 
1570 	mutex_lock(&phydev->lock);
1571 	ret = genphy_c45_ethtool_get_eee(phydev, data);
1572 	mutex_unlock(&phydev->lock);
1573 
1574 	return ret;
1575 }
1576 EXPORT_SYMBOL(phy_ethtool_get_eee);
1577 
1578 /**
1579  * phy_ethtool_set_eee - set EEE supported and status
1580  * @phydev: target phy_device struct
1581  * @data: ethtool_eee data
1582  *
1583  * Description: it is to program the Advertisement EEE register.
1584  */
1585 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1586 {
1587 	int ret;
1588 
1589 	if (!phydev->drv)
1590 		return -EIO;
1591 
1592 	mutex_lock(&phydev->lock);
1593 	ret = genphy_c45_ethtool_set_eee(phydev, data);
1594 	mutex_unlock(&phydev->lock);
1595 
1596 	return ret;
1597 }
1598 EXPORT_SYMBOL(phy_ethtool_set_eee);
1599 
1600 /**
1601  * phy_ethtool_set_wol - Configure Wake On LAN
1602  *
1603  * @phydev: target phy_device struct
1604  * @wol: Configuration requested
1605  */
1606 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1607 {
1608 	int ret;
1609 
1610 	if (phydev->drv && phydev->drv->set_wol) {
1611 		mutex_lock(&phydev->lock);
1612 		ret = phydev->drv->set_wol(phydev, wol);
1613 		mutex_unlock(&phydev->lock);
1614 
1615 		return ret;
1616 	}
1617 
1618 	return -EOPNOTSUPP;
1619 }
1620 EXPORT_SYMBOL(phy_ethtool_set_wol);
1621 
1622 /**
1623  * phy_ethtool_get_wol - Get the current Wake On LAN configuration
1624  *
1625  * @phydev: target phy_device struct
1626  * @wol: Store the current configuration here
1627  */
1628 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1629 {
1630 	if (phydev->drv && phydev->drv->get_wol) {
1631 		mutex_lock(&phydev->lock);
1632 		phydev->drv->get_wol(phydev, wol);
1633 		mutex_unlock(&phydev->lock);
1634 	}
1635 }
1636 EXPORT_SYMBOL(phy_ethtool_get_wol);
1637 
1638 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1639 				   struct ethtool_link_ksettings *cmd)
1640 {
1641 	struct phy_device *phydev = ndev->phydev;
1642 
1643 	if (!phydev)
1644 		return -ENODEV;
1645 
1646 	phy_ethtool_ksettings_get(phydev, cmd);
1647 
1648 	return 0;
1649 }
1650 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1651 
1652 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1653 				   const struct ethtool_link_ksettings *cmd)
1654 {
1655 	struct phy_device *phydev = ndev->phydev;
1656 
1657 	if (!phydev)
1658 		return -ENODEV;
1659 
1660 	return phy_ethtool_ksettings_set(phydev, cmd);
1661 }
1662 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1663 
1664 /**
1665  * phy_ethtool_nway_reset - Restart auto negotiation
1666  * @ndev: Network device to restart autoneg for
1667  */
1668 int phy_ethtool_nway_reset(struct net_device *ndev)
1669 {
1670 	struct phy_device *phydev = ndev->phydev;
1671 	int ret;
1672 
1673 	if (!phydev)
1674 		return -ENODEV;
1675 
1676 	if (!phydev->drv)
1677 		return -EIO;
1678 
1679 	mutex_lock(&phydev->lock);
1680 	ret = phy_restart_aneg(phydev);
1681 	mutex_unlock(&phydev->lock);
1682 
1683 	return ret;
1684 }
1685 EXPORT_SYMBOL(phy_ethtool_nway_reset);
1686