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