xref: /linux/drivers/net/phy/phy.c (revision 22fc4c4c9fd60427bcda00878cee94e7622cfa7a)
1 /* Framework for configuring and reading PHY devices
2  * Based on code in sungem_phy.c and gianfar_phy.c
3  *
4  * Author: Andy Fleming
5  *
6  * Copyright (c) 2004 Freescale Semiconductor, Inc.
7  * Copyright (c) 2006, 2007  Maciej W. Rozycki
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  *
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/unistd.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/mm.h>
26 #include <linux/module.h>
27 #include <linux/mii.h>
28 #include <linux/ethtool.h>
29 #include <linux/phy.h>
30 #include <linux/phy_led_triggers.h>
31 #include <linux/workqueue.h>
32 #include <linux/mdio.h>
33 #include <linux/io.h>
34 #include <linux/uaccess.h>
35 #include <linux/atomic.h>
36 
37 #define PHY_STATE_STR(_state)			\
38 	case PHY_##_state:			\
39 		return __stringify(_state);	\
40 
41 static const char *phy_state_to_str(enum phy_state st)
42 {
43 	switch (st) {
44 	PHY_STATE_STR(DOWN)
45 	PHY_STATE_STR(READY)
46 	PHY_STATE_STR(UP)
47 	PHY_STATE_STR(RUNNING)
48 	PHY_STATE_STR(NOLINK)
49 	PHY_STATE_STR(FORCING)
50 	PHY_STATE_STR(CHANGELINK)
51 	PHY_STATE_STR(HALTED)
52 	PHY_STATE_STR(RESUMING)
53 	}
54 
55 	return NULL;
56 }
57 
58 static void phy_link_up(struct phy_device *phydev)
59 {
60 	phydev->phy_link_change(phydev, true, true);
61 	phy_led_trigger_change_speed(phydev);
62 }
63 
64 static void phy_link_down(struct phy_device *phydev, bool do_carrier)
65 {
66 	phydev->phy_link_change(phydev, false, do_carrier);
67 	phy_led_trigger_change_speed(phydev);
68 }
69 
70 /**
71  * phy_print_status - Convenience function to print out the current phy status
72  * @phydev: the phy_device struct
73  */
74 void phy_print_status(struct phy_device *phydev)
75 {
76 	if (phydev->link) {
77 		netdev_info(phydev->attached_dev,
78 			"Link is Up - %s/%s - flow control %s\n",
79 			phy_speed_to_str(phydev->speed),
80 			phy_duplex_to_str(phydev->duplex),
81 			phydev->pause ? "rx/tx" : "off");
82 	} else	{
83 		netdev_info(phydev->attached_dev, "Link is Down\n");
84 	}
85 }
86 EXPORT_SYMBOL(phy_print_status);
87 
88 /**
89  * phy_clear_interrupt - Ack the phy device's interrupt
90  * @phydev: the phy_device struct
91  *
92  * If the @phydev driver has an ack_interrupt function, call it to
93  * ack and clear the phy device's interrupt.
94  *
95  * Returns 0 on success or < 0 on error.
96  */
97 static int phy_clear_interrupt(struct phy_device *phydev)
98 {
99 	if (phydev->drv->ack_interrupt)
100 		return phydev->drv->ack_interrupt(phydev);
101 
102 	return 0;
103 }
104 
105 /**
106  * phy_config_interrupt - configure the PHY device for the requested interrupts
107  * @phydev: the phy_device struct
108  * @interrupts: interrupt flags to configure for this @phydev
109  *
110  * Returns 0 on success or < 0 on error.
111  */
112 static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
113 {
114 	phydev->interrupts = interrupts ? 1 : 0;
115 	if (phydev->drv->config_intr)
116 		return phydev->drv->config_intr(phydev);
117 
118 	return 0;
119 }
120 
121 /**
122  * phy_restart_aneg - restart auto-negotiation
123  * @phydev: target phy_device struct
124  *
125  * Restart the autonegotiation on @phydev.  Returns >= 0 on success or
126  * negative errno on error.
127  */
128 int phy_restart_aneg(struct phy_device *phydev)
129 {
130 	int ret;
131 
132 	if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
133 		ret = genphy_c45_restart_aneg(phydev);
134 	else
135 		ret = genphy_restart_aneg(phydev);
136 
137 	return ret;
138 }
139 EXPORT_SYMBOL_GPL(phy_restart_aneg);
140 
141 /**
142  * phy_aneg_done - return auto-negotiation status
143  * @phydev: target phy_device struct
144  *
145  * Description: Return the auto-negotiation status from this @phydev
146  * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
147  * is still pending.
148  */
149 int phy_aneg_done(struct phy_device *phydev)
150 {
151 	if (phydev->drv && phydev->drv->aneg_done)
152 		return phydev->drv->aneg_done(phydev);
153 
154 	/* Avoid genphy_aneg_done() if the Clause 45 PHY does not
155 	 * implement Clause 22 registers
156 	 */
157 	if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
158 		return -EINVAL;
159 
160 	return genphy_aneg_done(phydev);
161 }
162 EXPORT_SYMBOL(phy_aneg_done);
163 
164 /**
165  * phy_find_valid - find a PHY setting that matches the requested parameters
166  * @speed: desired speed
167  * @duplex: desired duplex
168  * @supported: mask of supported link modes
169  *
170  * Locate a supported phy setting that is, in priority order:
171  * - an exact match for the specified speed and duplex mode
172  * - a match for the specified speed, or slower speed
173  * - the slowest supported speed
174  * Returns the matched phy_setting entry, or %NULL if no supported phy
175  * settings were found.
176  */
177 static const struct phy_setting *
178 phy_find_valid(int speed, int duplex, unsigned long *supported)
179 {
180 	return phy_lookup_setting(speed, duplex, supported, false);
181 }
182 
183 /**
184  * phy_supported_speeds - return all speeds currently supported by a phy device
185  * @phy: The phy device to return supported speeds of.
186  * @speeds: buffer to store supported speeds in.
187  * @size:   size of speeds buffer.
188  *
189  * Description: Returns the number of supported speeds, and fills the speeds
190  * buffer with the supported speeds. If speeds buffer is too small to contain
191  * all currently supported speeds, will return as many speeds as can fit.
192  */
193 unsigned int phy_supported_speeds(struct phy_device *phy,
194 				  unsigned int *speeds,
195 				  unsigned int size)
196 {
197 	return phy_speeds(speeds, size, phy->supported);
198 }
199 
200 /**
201  * phy_check_valid - check if there is a valid PHY setting which matches
202  *		     speed, duplex, and feature mask
203  * @speed: speed to match
204  * @duplex: duplex to match
205  * @features: A mask of the valid settings
206  *
207  * Description: Returns true if there is a valid setting, false otherwise.
208  */
209 static inline bool phy_check_valid(int speed, int duplex,
210 				   unsigned long *features)
211 {
212 	return !!phy_lookup_setting(speed, duplex, features, true);
213 }
214 
215 /**
216  * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
217  * @phydev: the target phy_device struct
218  *
219  * Description: Make sure the PHY is set to supported speeds and
220  *   duplexes.  Drop down by one in this order:  1000/FULL,
221  *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
222  */
223 static void phy_sanitize_settings(struct phy_device *phydev)
224 {
225 	const struct phy_setting *setting;
226 
227 	/* Sanitize settings based on PHY capabilities */
228 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported))
229 		phydev->autoneg = AUTONEG_DISABLE;
230 
231 	setting = phy_find_valid(phydev->speed, phydev->duplex,
232 				 phydev->supported);
233 	if (setting) {
234 		phydev->speed = setting->speed;
235 		phydev->duplex = setting->duplex;
236 	} else {
237 		/* We failed to find anything (no supported speeds?) */
238 		phydev->speed = SPEED_UNKNOWN;
239 		phydev->duplex = DUPLEX_UNKNOWN;
240 	}
241 }
242 
243 /**
244  * phy_ethtool_sset - generic ethtool sset function, handles all the details
245  * @phydev: target phy_device struct
246  * @cmd: ethtool_cmd
247  *
248  * A few notes about parameter checking:
249  *
250  * - We don't set port or transceiver, so we don't care what they
251  *   were set to.
252  * - phy_start_aneg() will make sure forced settings are sane, and
253  *   choose the next best ones from the ones selected, so we don't
254  *   care if ethtool tries to give us bad values.
255  */
256 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
257 {
258 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
259 	u32 speed = ethtool_cmd_speed(cmd);
260 
261 	if (cmd->phy_address != phydev->mdio.addr)
262 		return -EINVAL;
263 
264 	/* We make sure that we don't pass unsupported values in to the PHY */
265 	ethtool_convert_legacy_u32_to_link_mode(advertising, cmd->advertising);
266 	linkmode_and(advertising, advertising, phydev->supported);
267 
268 	/* Verify the settings we care about. */
269 	if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
270 		return -EINVAL;
271 
272 	if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
273 		return -EINVAL;
274 
275 	if (cmd->autoneg == AUTONEG_DISABLE &&
276 	    ((speed != SPEED_1000 &&
277 	      speed != SPEED_100 &&
278 	      speed != SPEED_10) ||
279 	     (cmd->duplex != DUPLEX_HALF &&
280 	      cmd->duplex != DUPLEX_FULL)))
281 		return -EINVAL;
282 
283 	phydev->autoneg = cmd->autoneg;
284 
285 	phydev->speed = speed;
286 
287 	linkmode_copy(phydev->advertising, advertising);
288 
289 	if (AUTONEG_ENABLE == cmd->autoneg)
290 		linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
291 				 phydev->advertising);
292 	else
293 		linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
294 				   phydev->advertising);
295 
296 	phydev->duplex = cmd->duplex;
297 
298 	phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
299 
300 	/* Restart the PHY */
301 	phy_start_aneg(phydev);
302 
303 	return 0;
304 }
305 EXPORT_SYMBOL(phy_ethtool_sset);
306 
307 int phy_ethtool_ksettings_set(struct phy_device *phydev,
308 			      const struct ethtool_link_ksettings *cmd)
309 {
310 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
311 	u8 autoneg = cmd->base.autoneg;
312 	u8 duplex = cmd->base.duplex;
313 	u32 speed = cmd->base.speed;
314 
315 	if (cmd->base.phy_address != phydev->mdio.addr)
316 		return -EINVAL;
317 
318 	linkmode_copy(advertising, cmd->link_modes.advertising);
319 
320 	/* We make sure that we don't pass unsupported values in to the PHY */
321 	linkmode_and(advertising, advertising, phydev->supported);
322 
323 	/* Verify the settings we care about. */
324 	if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
325 		return -EINVAL;
326 
327 	if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
328 		return -EINVAL;
329 
330 	if (autoneg == AUTONEG_DISABLE &&
331 	    ((speed != SPEED_1000 &&
332 	      speed != SPEED_100 &&
333 	      speed != SPEED_10) ||
334 	     (duplex != DUPLEX_HALF &&
335 	      duplex != DUPLEX_FULL)))
336 		return -EINVAL;
337 
338 	phydev->autoneg = autoneg;
339 
340 	phydev->speed = speed;
341 
342 	linkmode_copy(phydev->advertising, advertising);
343 
344 	if (autoneg == AUTONEG_ENABLE)
345 		linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
346 				 phydev->advertising);
347 	else
348 		linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
349 				   phydev->advertising);
350 
351 	phydev->duplex = duplex;
352 
353 	phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
354 
355 	/* Restart the PHY */
356 	phy_start_aneg(phydev);
357 
358 	return 0;
359 }
360 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
361 
362 void phy_ethtool_ksettings_get(struct phy_device *phydev,
363 			       struct ethtool_link_ksettings *cmd)
364 {
365 	linkmode_copy(cmd->link_modes.supported, phydev->supported);
366 	linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
367 	linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
368 
369 	cmd->base.speed = phydev->speed;
370 	cmd->base.duplex = phydev->duplex;
371 	if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
372 		cmd->base.port = PORT_BNC;
373 	else
374 		cmd->base.port = PORT_MII;
375 	cmd->base.transceiver = phy_is_internal(phydev) ?
376 				XCVR_INTERNAL : XCVR_EXTERNAL;
377 	cmd->base.phy_address = phydev->mdio.addr;
378 	cmd->base.autoneg = phydev->autoneg;
379 	cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
380 	cmd->base.eth_tp_mdix = phydev->mdix;
381 }
382 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
383 
384 /**
385  * phy_mii_ioctl - generic PHY MII ioctl interface
386  * @phydev: the phy_device struct
387  * @ifr: &struct ifreq for socket ioctl's
388  * @cmd: ioctl cmd to execute
389  *
390  * Note that this function is currently incompatible with the
391  * PHYCONTROL layer.  It changes registers without regard to
392  * current state.  Use at own risk.
393  */
394 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
395 {
396 	struct mii_ioctl_data *mii_data = if_mii(ifr);
397 	u16 val = mii_data->val_in;
398 	bool change_autoneg = false;
399 
400 	switch (cmd) {
401 	case SIOCGMIIPHY:
402 		mii_data->phy_id = phydev->mdio.addr;
403 		/* fall through */
404 
405 	case SIOCGMIIREG:
406 		mii_data->val_out = mdiobus_read(phydev->mdio.bus,
407 						 mii_data->phy_id,
408 						 mii_data->reg_num);
409 		return 0;
410 
411 	case SIOCSMIIREG:
412 		if (mii_data->phy_id == phydev->mdio.addr) {
413 			switch (mii_data->reg_num) {
414 			case MII_BMCR:
415 				if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
416 					if (phydev->autoneg == AUTONEG_ENABLE)
417 						change_autoneg = true;
418 					phydev->autoneg = AUTONEG_DISABLE;
419 					if (val & BMCR_FULLDPLX)
420 						phydev->duplex = DUPLEX_FULL;
421 					else
422 						phydev->duplex = DUPLEX_HALF;
423 					if (val & BMCR_SPEED1000)
424 						phydev->speed = SPEED_1000;
425 					else if (val & BMCR_SPEED100)
426 						phydev->speed = SPEED_100;
427 					else phydev->speed = SPEED_10;
428 				}
429 				else {
430 					if (phydev->autoneg == AUTONEG_DISABLE)
431 						change_autoneg = true;
432 					phydev->autoneg = AUTONEG_ENABLE;
433 				}
434 				break;
435 			case MII_ADVERTISE:
436 				mii_adv_mod_linkmode_adv_t(phydev->advertising,
437 							   val);
438 				change_autoneg = true;
439 				break;
440 			default:
441 				/* do nothing */
442 				break;
443 			}
444 		}
445 
446 		mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
447 			      mii_data->reg_num, val);
448 
449 		if (mii_data->phy_id == phydev->mdio.addr &&
450 		    mii_data->reg_num == MII_BMCR &&
451 		    val & BMCR_RESET)
452 			return phy_init_hw(phydev);
453 
454 		if (change_autoneg)
455 			return phy_start_aneg(phydev);
456 
457 		return 0;
458 
459 	case SIOCSHWTSTAMP:
460 		if (phydev->drv && phydev->drv->hwtstamp)
461 			return phydev->drv->hwtstamp(phydev, ifr);
462 		/* fall through */
463 
464 	default:
465 		return -EOPNOTSUPP;
466 	}
467 }
468 EXPORT_SYMBOL(phy_mii_ioctl);
469 
470 static void phy_queue_state_machine(struct phy_device *phydev,
471 				    unsigned int secs)
472 {
473 	mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
474 			 secs * HZ);
475 }
476 
477 static void phy_trigger_machine(struct phy_device *phydev)
478 {
479 	phy_queue_state_machine(phydev, 0);
480 }
481 
482 static int phy_config_aneg(struct phy_device *phydev)
483 {
484 	if (phydev->drv->config_aneg)
485 		return phydev->drv->config_aneg(phydev);
486 
487 	/* Clause 45 PHYs that don't implement Clause 22 registers are not
488 	 * allowed to call genphy_config_aneg()
489 	 */
490 	if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
491 		return -EOPNOTSUPP;
492 
493 	return genphy_config_aneg(phydev);
494 }
495 
496 /**
497  * phy_check_link_status - check link status and set state accordingly
498  * @phydev: the phy_device struct
499  *
500  * Description: Check for link and whether autoneg was triggered / is running
501  * and set state accordingly
502  */
503 static int phy_check_link_status(struct phy_device *phydev)
504 {
505 	int err;
506 
507 	WARN_ON(!mutex_is_locked(&phydev->lock));
508 
509 	err = phy_read_status(phydev);
510 	if (err)
511 		return err;
512 
513 	if (phydev->link && phydev->state != PHY_RUNNING) {
514 		phydev->state = PHY_RUNNING;
515 		phy_link_up(phydev);
516 	} else if (!phydev->link && phydev->state != PHY_NOLINK) {
517 		phydev->state = PHY_NOLINK;
518 		phy_link_down(phydev, true);
519 	}
520 
521 	return 0;
522 }
523 
524 /**
525  * phy_start_aneg - start auto-negotiation for this PHY device
526  * @phydev: the phy_device struct
527  *
528  * Description: Sanitizes the settings (if we're not autonegotiating
529  *   them), and then calls the driver's config_aneg function.
530  *   If the PHYCONTROL Layer is operating, we change the state to
531  *   reflect the beginning of Auto-negotiation or forcing.
532  */
533 int phy_start_aneg(struct phy_device *phydev)
534 {
535 	int err;
536 
537 	if (!phydev->drv)
538 		return -EIO;
539 
540 	mutex_lock(&phydev->lock);
541 
542 	if (!__phy_is_started(phydev)) {
543 		WARN(1, "called from state %s\n",
544 		     phy_state_to_str(phydev->state));
545 		err = -EBUSY;
546 		goto out_unlock;
547 	}
548 
549 	if (AUTONEG_DISABLE == phydev->autoneg)
550 		phy_sanitize_settings(phydev);
551 
552 	/* Invalidate LP advertising flags */
553 	linkmode_zero(phydev->lp_advertising);
554 
555 	err = phy_config_aneg(phydev);
556 	if (err < 0)
557 		goto out_unlock;
558 
559 	if (phydev->autoneg == AUTONEG_ENABLE) {
560 		err = phy_check_link_status(phydev);
561 	} else {
562 		phydev->state = PHY_FORCING;
563 		phydev->link_timeout = PHY_FORCE_TIMEOUT;
564 	}
565 
566 out_unlock:
567 	mutex_unlock(&phydev->lock);
568 
569 	return err;
570 }
571 EXPORT_SYMBOL(phy_start_aneg);
572 
573 static int phy_poll_aneg_done(struct phy_device *phydev)
574 {
575 	unsigned int retries = 100;
576 	int ret;
577 
578 	do {
579 		msleep(100);
580 		ret = phy_aneg_done(phydev);
581 	} while (!ret && --retries);
582 
583 	if (!ret)
584 		return -ETIMEDOUT;
585 
586 	return ret < 0 ? ret : 0;
587 }
588 
589 /**
590  * phy_speed_down - set speed to lowest speed supported by both link partners
591  * @phydev: the phy_device struct
592  * @sync: perform action synchronously
593  *
594  * Description: Typically used to save energy when waiting for a WoL packet
595  *
596  * WARNING: Setting sync to false may cause the system being unable to suspend
597  * in case the PHY generates an interrupt when finishing the autonegotiation.
598  * This interrupt may wake up the system immediately after suspend.
599  * Therefore use sync = false only if you're sure it's safe with the respective
600  * network chip.
601  */
602 int phy_speed_down(struct phy_device *phydev, bool sync)
603 {
604 	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
605 	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
606 	int ret;
607 
608 	if (phydev->autoneg != AUTONEG_ENABLE)
609 		return 0;
610 
611 	linkmode_copy(adv_old, phydev->advertising);
612 	linkmode_copy(adv, phydev->lp_advertising);
613 	linkmode_and(adv, adv, phydev->supported);
614 
615 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, adv) ||
616 	    linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, adv)) {
617 		linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
618 				   phydev->advertising);
619 		linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
620 				   phydev->advertising);
621 		linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
622 				   phydev->advertising);
623 		linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
624 				   phydev->advertising);
625 	} else if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
626 				     adv) ||
627 		   linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
628 				     adv)) {
629 		linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
630 				   phydev->advertising);
631 		linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
632 				   phydev->advertising);
633 	}
634 
635 	if (linkmode_equal(phydev->advertising, adv_old))
636 		return 0;
637 
638 	ret = phy_config_aneg(phydev);
639 	if (ret)
640 		return ret;
641 
642 	return sync ? phy_poll_aneg_done(phydev) : 0;
643 }
644 EXPORT_SYMBOL_GPL(phy_speed_down);
645 
646 /**
647  * phy_speed_up - (re)set advertised speeds to all supported speeds
648  * @phydev: the phy_device struct
649  *
650  * Description: Used to revert the effect of phy_speed_down
651  */
652 int phy_speed_up(struct phy_device *phydev)
653 {
654 	__ETHTOOL_DECLARE_LINK_MODE_MASK(all_speeds) = { 0, };
655 	__ETHTOOL_DECLARE_LINK_MODE_MASK(not_speeds);
656 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
657 	__ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
658 	__ETHTOOL_DECLARE_LINK_MODE_MASK(speeds);
659 
660 	linkmode_copy(adv_old, phydev->advertising);
661 
662 	if (phydev->autoneg != AUTONEG_ENABLE)
663 		return 0;
664 
665 	linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, all_speeds);
666 	linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, all_speeds);
667 	linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, all_speeds);
668 	linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, all_speeds);
669 	linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, all_speeds);
670 	linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, all_speeds);
671 
672 	linkmode_andnot(not_speeds, adv_old, all_speeds);
673 	linkmode_copy(supported, phydev->supported);
674 	linkmode_and(speeds, supported, all_speeds);
675 	linkmode_or(phydev->advertising, not_speeds, speeds);
676 
677 	if (linkmode_equal(phydev->advertising, adv_old))
678 		return 0;
679 
680 	return phy_config_aneg(phydev);
681 }
682 EXPORT_SYMBOL_GPL(phy_speed_up);
683 
684 /**
685  * phy_start_machine - start PHY state machine tracking
686  * @phydev: the phy_device struct
687  *
688  * Description: The PHY infrastructure can run a state machine
689  *   which tracks whether the PHY is starting up, negotiating,
690  *   etc.  This function starts the delayed workqueue which tracks
691  *   the state of the PHY. If you want to maintain your own state machine,
692  *   do not call this function.
693  */
694 void phy_start_machine(struct phy_device *phydev)
695 {
696 	phy_trigger_machine(phydev);
697 }
698 EXPORT_SYMBOL_GPL(phy_start_machine);
699 
700 /**
701  * phy_stop_machine - stop the PHY state machine tracking
702  * @phydev: target phy_device struct
703  *
704  * Description: Stops the state machine delayed workqueue, sets the
705  *   state to UP (unless it wasn't up yet). This function must be
706  *   called BEFORE phy_detach.
707  */
708 void phy_stop_machine(struct phy_device *phydev)
709 {
710 	cancel_delayed_work_sync(&phydev->state_queue);
711 
712 	mutex_lock(&phydev->lock);
713 	if (__phy_is_started(phydev))
714 		phydev->state = PHY_UP;
715 	mutex_unlock(&phydev->lock);
716 }
717 
718 /**
719  * phy_error - enter HALTED state for this PHY device
720  * @phydev: target phy_device struct
721  *
722  * Moves the PHY to the HALTED state in response to a read
723  * or write error, and tells the controller the link is down.
724  * Must not be called from interrupt context, or while the
725  * phydev->lock is held.
726  */
727 static void phy_error(struct phy_device *phydev)
728 {
729 	WARN_ON(1);
730 
731 	mutex_lock(&phydev->lock);
732 	phydev->state = PHY_HALTED;
733 	mutex_unlock(&phydev->lock);
734 
735 	phy_trigger_machine(phydev);
736 }
737 
738 /**
739  * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
740  * @phydev: target phy_device struct
741  */
742 static int phy_disable_interrupts(struct phy_device *phydev)
743 {
744 	int err;
745 
746 	/* Disable PHY interrupts */
747 	err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
748 	if (err)
749 		return err;
750 
751 	/* Clear the interrupt */
752 	return phy_clear_interrupt(phydev);
753 }
754 
755 /**
756  * phy_interrupt - PHY interrupt handler
757  * @irq: interrupt line
758  * @phy_dat: phy_device pointer
759  *
760  * Description: Handle PHY interrupt
761  */
762 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
763 {
764 	struct phy_device *phydev = phy_dat;
765 
766 	if (!phy_is_started(phydev))
767 		return IRQ_NONE;		/* It can't be ours.  */
768 
769 	if (phydev->drv->did_interrupt && !phydev->drv->did_interrupt(phydev))
770 		return IRQ_NONE;
771 
772 	/* reschedule state queue work to run as soon as possible */
773 	phy_trigger_machine(phydev);
774 
775 	if (phy_clear_interrupt(phydev))
776 		goto phy_err;
777 	return IRQ_HANDLED;
778 
779 phy_err:
780 	phy_error(phydev);
781 	return IRQ_NONE;
782 }
783 
784 /**
785  * phy_enable_interrupts - Enable the interrupts from the PHY side
786  * @phydev: target phy_device struct
787  */
788 static int phy_enable_interrupts(struct phy_device *phydev)
789 {
790 	int err = phy_clear_interrupt(phydev);
791 
792 	if (err < 0)
793 		return err;
794 
795 	return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
796 }
797 
798 /**
799  * phy_start_interrupts - request and enable interrupts for a PHY device
800  * @phydev: target phy_device struct
801  *
802  * Description: Request the interrupt for the given PHY.
803  *   If this fails, then we set irq to PHY_POLL.
804  *   Otherwise, we enable the interrupts in the PHY.
805  *   This should only be called with a valid IRQ number.
806  *   Returns 0 on success or < 0 on error.
807  */
808 int phy_start_interrupts(struct phy_device *phydev)
809 {
810 	if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
811 				 IRQF_ONESHOT | IRQF_SHARED,
812 				 phydev_name(phydev), phydev) < 0) {
813 		phydev_warn(phydev, "Can't get IRQ %d\n", phydev->irq);
814 		phydev->irq = PHY_POLL;
815 		return 0;
816 	}
817 
818 	return phy_enable_interrupts(phydev);
819 }
820 EXPORT_SYMBOL(phy_start_interrupts);
821 
822 /**
823  * phy_stop_interrupts - disable interrupts from a PHY device
824  * @phydev: target phy_device struct
825  */
826 int phy_stop_interrupts(struct phy_device *phydev)
827 {
828 	int err = phy_disable_interrupts(phydev);
829 
830 	if (err)
831 		phy_error(phydev);
832 
833 	free_irq(phydev->irq, phydev);
834 
835 	return err;
836 }
837 EXPORT_SYMBOL(phy_stop_interrupts);
838 
839 /**
840  * phy_stop - Bring down the PHY link, and stop checking the status
841  * @phydev: target phy_device struct
842  */
843 void phy_stop(struct phy_device *phydev)
844 {
845 	mutex_lock(&phydev->lock);
846 
847 	if (!__phy_is_started(phydev)) {
848 		WARN(1, "called from state %s\n",
849 		     phy_state_to_str(phydev->state));
850 		mutex_unlock(&phydev->lock);
851 		return;
852 	}
853 
854 	if (phy_interrupt_is_valid(phydev))
855 		phy_disable_interrupts(phydev);
856 
857 	phydev->state = PHY_HALTED;
858 
859 	mutex_unlock(&phydev->lock);
860 
861 	phy_state_machine(&phydev->state_queue.work);
862 
863 	/* Cannot call flush_scheduled_work() here as desired because
864 	 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
865 	 * will not reenable interrupts.
866 	 */
867 }
868 EXPORT_SYMBOL(phy_stop);
869 
870 /**
871  * phy_start - start or restart a PHY device
872  * @phydev: target phy_device struct
873  *
874  * Description: Indicates the attached device's readiness to
875  *   handle PHY-related work.  Used during startup to start the
876  *   PHY, and after a call to phy_stop() to resume operation.
877  *   Also used to indicate the MDIO bus has cleared an error
878  *   condition.
879  */
880 void phy_start(struct phy_device *phydev)
881 {
882 	int err = 0;
883 
884 	mutex_lock(&phydev->lock);
885 
886 	switch (phydev->state) {
887 	case PHY_READY:
888 		phydev->state = PHY_UP;
889 		break;
890 	case PHY_HALTED:
891 		/* if phy was suspended, bring the physical link up again */
892 		__phy_resume(phydev);
893 
894 		/* make sure interrupts are re-enabled for the PHY */
895 		if (phy_interrupt_is_valid(phydev)) {
896 			err = phy_enable_interrupts(phydev);
897 			if (err < 0)
898 				break;
899 		}
900 
901 		phydev->state = PHY_RESUMING;
902 		break;
903 	default:
904 		break;
905 	}
906 	mutex_unlock(&phydev->lock);
907 
908 	phy_trigger_machine(phydev);
909 }
910 EXPORT_SYMBOL(phy_start);
911 
912 /**
913  * phy_state_machine - Handle the state machine
914  * @work: work_struct that describes the work to be done
915  */
916 void phy_state_machine(struct work_struct *work)
917 {
918 	struct delayed_work *dwork = to_delayed_work(work);
919 	struct phy_device *phydev =
920 			container_of(dwork, struct phy_device, state_queue);
921 	bool needs_aneg = false, do_suspend = false;
922 	enum phy_state old_state;
923 	int err = 0;
924 
925 	mutex_lock(&phydev->lock);
926 
927 	old_state = phydev->state;
928 
929 	if (phydev->drv && phydev->drv->link_change_notify)
930 		phydev->drv->link_change_notify(phydev);
931 
932 	switch (phydev->state) {
933 	case PHY_DOWN:
934 	case PHY_READY:
935 		break;
936 	case PHY_UP:
937 		needs_aneg = true;
938 
939 		break;
940 	case PHY_NOLINK:
941 	case PHY_RUNNING:
942 	case PHY_CHANGELINK:
943 	case PHY_RESUMING:
944 		err = phy_check_link_status(phydev);
945 		break;
946 	case PHY_FORCING:
947 		err = genphy_update_link(phydev);
948 		if (err)
949 			break;
950 
951 		if (phydev->link) {
952 			phydev->state = PHY_RUNNING;
953 			phy_link_up(phydev);
954 		} else {
955 			if (0 == phydev->link_timeout--)
956 				needs_aneg = true;
957 			phy_link_down(phydev, false);
958 		}
959 		break;
960 	case PHY_HALTED:
961 		if (phydev->link) {
962 			phydev->link = 0;
963 			phy_link_down(phydev, true);
964 			do_suspend = true;
965 		}
966 		break;
967 	}
968 
969 	mutex_unlock(&phydev->lock);
970 
971 	if (needs_aneg)
972 		err = phy_start_aneg(phydev);
973 	else if (do_suspend)
974 		phy_suspend(phydev);
975 
976 	if (err < 0)
977 		phy_error(phydev);
978 
979 	if (old_state != phydev->state)
980 		phydev_dbg(phydev, "PHY state change %s -> %s\n",
981 			   phy_state_to_str(old_state),
982 			   phy_state_to_str(phydev->state));
983 
984 	/* Only re-schedule a PHY state machine change if we are polling the
985 	 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
986 	 * between states from phy_mac_interrupt().
987 	 *
988 	 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
989 	 * state machine would be pointless and possibly error prone when
990 	 * called from phy_disconnect() synchronously.
991 	 */
992 	if (phy_polling_mode(phydev) && phy_is_started(phydev))
993 		phy_queue_state_machine(phydev, PHY_STATE_TIME);
994 }
995 
996 /**
997  * phy_mac_interrupt - MAC says the link has changed
998  * @phydev: phy_device struct with changed link
999  *
1000  * The MAC layer is able to indicate there has been a change in the PHY link
1001  * status. Trigger the state machine and work a work queue.
1002  */
1003 void phy_mac_interrupt(struct phy_device *phydev)
1004 {
1005 	/* Trigger a state machine change */
1006 	phy_trigger_machine(phydev);
1007 }
1008 EXPORT_SYMBOL(phy_mac_interrupt);
1009 
1010 static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
1011 {
1012 	linkmode_zero(advertising);
1013 
1014 	if (eee_adv & MDIO_EEE_100TX)
1015 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1016 				 advertising);
1017 	if (eee_adv & MDIO_EEE_1000T)
1018 		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1019 				 advertising);
1020 	if (eee_adv & MDIO_EEE_10GT)
1021 		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1022 				 advertising);
1023 	if (eee_adv & MDIO_EEE_1000KX)
1024 		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1025 				 advertising);
1026 	if (eee_adv & MDIO_EEE_10GKX4)
1027 		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1028 				 advertising);
1029 	if (eee_adv & MDIO_EEE_10GKR)
1030 		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1031 				 advertising);
1032 }
1033 
1034 /**
1035  * phy_init_eee - init and check the EEE feature
1036  * @phydev: target phy_device struct
1037  * @clk_stop_enable: PHY may stop the clock during LPI
1038  *
1039  * Description: it checks if the Energy-Efficient Ethernet (EEE)
1040  * is supported by looking at the MMD registers 3.20 and 7.60/61
1041  * and it programs the MMD register 3.0 setting the "Clock stop enable"
1042  * bit if required.
1043  */
1044 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1045 {
1046 	if (!phydev->drv)
1047 		return -EIO;
1048 
1049 	/* According to 802.3az,the EEE is supported only in full duplex-mode.
1050 	 */
1051 	if (phydev->duplex == DUPLEX_FULL) {
1052 		__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
1053 		__ETHTOOL_DECLARE_LINK_MODE_MASK(lp);
1054 		__ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
1055 		int eee_lp, eee_cap, eee_adv;
1056 		int status;
1057 		u32 cap;
1058 
1059 		/* Read phy status to properly get the right settings */
1060 		status = phy_read_status(phydev);
1061 		if (status)
1062 			return status;
1063 
1064 		/* First check if the EEE ability is supported */
1065 		eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1066 		if (eee_cap <= 0)
1067 			goto eee_exit_err;
1068 
1069 		cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1070 		if (!cap)
1071 			goto eee_exit_err;
1072 
1073 		/* Check which link settings negotiated and verify it in
1074 		 * the EEE advertising registers.
1075 		 */
1076 		eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1077 		if (eee_lp <= 0)
1078 			goto eee_exit_err;
1079 
1080 		eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1081 		if (eee_adv <= 0)
1082 			goto eee_exit_err;
1083 
1084 		mmd_eee_adv_to_linkmode(adv, eee_adv);
1085 		mmd_eee_adv_to_linkmode(lp, eee_lp);
1086 		linkmode_and(common, adv, lp);
1087 
1088 		if (!phy_check_valid(phydev->speed, phydev->duplex, common))
1089 			goto eee_exit_err;
1090 
1091 		if (clk_stop_enable) {
1092 			/* Configure the PHY to stop receiving xMII
1093 			 * clock while it is signaling LPI.
1094 			 */
1095 			int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1096 			if (val < 0)
1097 				return val;
1098 
1099 			val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1100 			phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1101 		}
1102 
1103 		return 0; /* EEE supported */
1104 	}
1105 eee_exit_err:
1106 	return -EPROTONOSUPPORT;
1107 }
1108 EXPORT_SYMBOL(phy_init_eee);
1109 
1110 /**
1111  * phy_get_eee_err - report the EEE wake error count
1112  * @phydev: target phy_device struct
1113  *
1114  * Description: it is to report the number of time where the PHY
1115  * failed to complete its normal wake sequence.
1116  */
1117 int phy_get_eee_err(struct phy_device *phydev)
1118 {
1119 	if (!phydev->drv)
1120 		return -EIO;
1121 
1122 	return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1123 }
1124 EXPORT_SYMBOL(phy_get_eee_err);
1125 
1126 /**
1127  * phy_ethtool_get_eee - get EEE supported and status
1128  * @phydev: target phy_device struct
1129  * @data: ethtool_eee data
1130  *
1131  * Description: it reportes the Supported/Advertisement/LP Advertisement
1132  * capabilities.
1133  */
1134 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1135 {
1136 	int val;
1137 
1138 	if (!phydev->drv)
1139 		return -EIO;
1140 
1141 	/* Get Supported EEE */
1142 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1143 	if (val < 0)
1144 		return val;
1145 	data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1146 
1147 	/* Get advertisement EEE */
1148 	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1149 	if (val < 0)
1150 		return val;
1151 	data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1152 	data->eee_enabled = !!data->advertised;
1153 
1154 	/* Get LP advertisement EEE */
1155 	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1156 	if (val < 0)
1157 		return val;
1158 	data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1159 
1160 	data->eee_active = !!(data->advertised & data->lp_advertised);
1161 
1162 	return 0;
1163 }
1164 EXPORT_SYMBOL(phy_ethtool_get_eee);
1165 
1166 /**
1167  * phy_ethtool_set_eee - set EEE supported and status
1168  * @phydev: target phy_device struct
1169  * @data: ethtool_eee data
1170  *
1171  * Description: it is to program the Advertisement EEE register.
1172  */
1173 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1174 {
1175 	int cap, old_adv, adv = 0, ret;
1176 
1177 	if (!phydev->drv)
1178 		return -EIO;
1179 
1180 	/* Get Supported EEE */
1181 	cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1182 	if (cap < 0)
1183 		return cap;
1184 
1185 	old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1186 	if (old_adv < 0)
1187 		return old_adv;
1188 
1189 	if (data->eee_enabled) {
1190 		adv = !data->advertised ? cap :
1191 		      ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1192 		/* Mask prohibited EEE modes */
1193 		adv &= ~phydev->eee_broken_modes;
1194 	}
1195 
1196 	if (old_adv != adv) {
1197 		ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1198 		if (ret < 0)
1199 			return ret;
1200 
1201 		/* Restart autonegotiation so the new modes get sent to the
1202 		 * link partner.
1203 		 */
1204 		ret = phy_restart_aneg(phydev);
1205 		if (ret < 0)
1206 			return ret;
1207 	}
1208 
1209 	return 0;
1210 }
1211 EXPORT_SYMBOL(phy_ethtool_set_eee);
1212 
1213 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1214 {
1215 	if (phydev->drv && phydev->drv->set_wol)
1216 		return phydev->drv->set_wol(phydev, wol);
1217 
1218 	return -EOPNOTSUPP;
1219 }
1220 EXPORT_SYMBOL(phy_ethtool_set_wol);
1221 
1222 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1223 {
1224 	if (phydev->drv && phydev->drv->get_wol)
1225 		phydev->drv->get_wol(phydev, wol);
1226 }
1227 EXPORT_SYMBOL(phy_ethtool_get_wol);
1228 
1229 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1230 				   struct ethtool_link_ksettings *cmd)
1231 {
1232 	struct phy_device *phydev = ndev->phydev;
1233 
1234 	if (!phydev)
1235 		return -ENODEV;
1236 
1237 	phy_ethtool_ksettings_get(phydev, cmd);
1238 
1239 	return 0;
1240 }
1241 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1242 
1243 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1244 				   const struct ethtool_link_ksettings *cmd)
1245 {
1246 	struct phy_device *phydev = ndev->phydev;
1247 
1248 	if (!phydev)
1249 		return -ENODEV;
1250 
1251 	return phy_ethtool_ksettings_set(phydev, cmd);
1252 }
1253 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1254 
1255 int phy_ethtool_nway_reset(struct net_device *ndev)
1256 {
1257 	struct phy_device *phydev = ndev->phydev;
1258 
1259 	if (!phydev)
1260 		return -ENODEV;
1261 
1262 	if (!phydev->drv)
1263 		return -EIO;
1264 
1265 	return phy_restart_aneg(phydev);
1266 }
1267 EXPORT_SYMBOL(phy_ethtool_nway_reset);
1268