]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/net/phy/phy.c
net: phy: add 802.3 clause 45 support to phylib
[mirror_ubuntu-focal-kernel.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
MR
33#include <linux/timer.h>
34#include <linux/workqueue.h>
a59a4d19 35#include <linux/mdio.h>
2f53e904
SS
36#include <linux/io.h>
37#include <linux/uaccess.h>
60063497 38#include <linux/atomic.h>
2f53e904 39
00db8189 40#include <asm/irq.h>
00db8189 41
766d1d38
FF
42static const char *phy_speed_to_str(int speed)
43{
44 switch (speed) {
45 case SPEED_10:
46 return "10Mbps";
47 case SPEED_100:
48 return "100Mbps";
49 case SPEED_1000:
50 return "1Gbps";
51 case SPEED_2500:
52 return "2.5Gbps";
1f37b177
JP
53 case SPEED_5000:
54 return "5Gbps";
766d1d38
FF
55 case SPEED_10000:
56 return "10Gbps";
1f37b177
JP
57 case SPEED_20000:
58 return "20Gbps";
59 case SPEED_25000:
60 return "25Gbps";
61 case SPEED_40000:
62 return "40Gbps";
63 case SPEED_50000:
64 return "50Gbps";
65 case SPEED_56000:
66 return "56Gbps";
67 case SPEED_100000:
68 return "100Gbps";
766d1d38
FF
69 case SPEED_UNKNOWN:
70 return "Unknown";
71 default:
72 return "Unsupported (update phy.c)";
73 }
74}
75
3e2186e0
FF
76#define PHY_STATE_STR(_state) \
77 case PHY_##_state: \
78 return __stringify(_state); \
79
80static const char *phy_state_to_str(enum phy_state st)
81{
82 switch (st) {
83 PHY_STATE_STR(DOWN)
84 PHY_STATE_STR(STARTING)
85 PHY_STATE_STR(READY)
86 PHY_STATE_STR(PENDING)
87 PHY_STATE_STR(UP)
88 PHY_STATE_STR(AN)
89 PHY_STATE_STR(RUNNING)
90 PHY_STATE_STR(NOLINK)
91 PHY_STATE_STR(FORCING)
92 PHY_STATE_STR(CHANGELINK)
93 PHY_STATE_STR(HALTED)
94 PHY_STATE_STR(RESUMING)
95 }
96
97 return NULL;
98}
99
100
b3df0da8
RD
101/**
102 * phy_print_status - Convenience function to print out the current phy status
103 * @phydev: the phy_device struct
e1393456
AF
104 */
105void phy_print_status(struct phy_device *phydev)
106{
2f53e904 107 if (phydev->link) {
df40cc88 108 netdev_info(phydev->attached_dev,
766d1d38
FF
109 "Link is Up - %s/%s - flow control %s\n",
110 phy_speed_to_str(phydev->speed),
df40cc88
FF
111 DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
112 phydev->pause ? "rx/tx" : "off");
2f53e904 113 } else {
43b6329f 114 netdev_info(phydev->attached_dev, "Link is Down\n");
2f53e904 115 }
e1393456
AF
116}
117EXPORT_SYMBOL(phy_print_status);
00db8189 118
b3df0da8
RD
119/**
120 * phy_clear_interrupt - Ack the phy device's interrupt
121 * @phydev: the phy_device struct
122 *
123 * If the @phydev driver has an ack_interrupt function, call it to
124 * ack and clear the phy device's interrupt.
125 *
ad033506 126 * Returns 0 on success or < 0 on error.
b3df0da8 127 */
89ff05ec 128static int phy_clear_interrupt(struct phy_device *phydev)
00db8189 129{
00db8189 130 if (phydev->drv->ack_interrupt)
e62a768f 131 return phydev->drv->ack_interrupt(phydev);
00db8189 132
e62a768f 133 return 0;
00db8189
AF
134}
135
b3df0da8
RD
136/**
137 * phy_config_interrupt - configure the PHY device for the requested interrupts
138 * @phydev: the phy_device struct
139 * @interrupts: interrupt flags to configure for this @phydev
140 *
ad033506 141 * Returns 0 on success or < 0 on error.
b3df0da8 142 */
89ff05ec 143static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
00db8189 144{
00db8189
AF
145 phydev->interrupts = interrupts;
146 if (phydev->drv->config_intr)
e62a768f 147 return phydev->drv->config_intr(phydev);
00db8189 148
e62a768f 149 return 0;
00db8189
AF
150}
151
152
b3df0da8
RD
153/**
154 * phy_aneg_done - return auto-negotiation status
155 * @phydev: target phy_device struct
00db8189 156 *
76a423a3
FF
157 * Description: Return the auto-negotiation status from this @phydev
158 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
159 * is still pending.
00db8189 160 */
372788f9 161int phy_aneg_done(struct phy_device *phydev)
00db8189 162{
65f2767a 163 if (phydev->drv && phydev->drv->aneg_done)
76a423a3
FF
164 return phydev->drv->aneg_done(phydev);
165
a9fa6e6a 166 return genphy_aneg_done(phydev);
00db8189 167}
372788f9 168EXPORT_SYMBOL(phy_aneg_done);
00db8189 169
00db8189 170/* A structure for mapping a particular speed and duplex
2f53e904
SS
171 * combination to a particular SUPPORTED and ADVERTISED value
172 */
00db8189
AF
173struct phy_setting {
174 int speed;
175 int duplex;
176 u32 setting;
177};
178
d0613037
RK
179/* A mapping of all SUPPORTED settings to speed/duplex. This table
180 * must be grouped by speed and sorted in descending match priority
181 * - iow, descending speed. */
f71e1309 182static const struct phy_setting settings[] = {
00db8189 183 {
3e707706
LT
184 .speed = SPEED_10000,
185 .duplex = DUPLEX_FULL,
186 .setting = SUPPORTED_10000baseKR_Full,
187 },
188 {
189 .speed = SPEED_10000,
190 .duplex = DUPLEX_FULL,
191 .setting = SUPPORTED_10000baseKX4_Full,
192 },
193 {
194 .speed = SPEED_10000,
00db8189
AF
195 .duplex = DUPLEX_FULL,
196 .setting = SUPPORTED_10000baseT_Full,
197 },
3e707706
LT
198 {
199 .speed = SPEED_2500,
200 .duplex = DUPLEX_FULL,
201 .setting = SUPPORTED_2500baseX_Full,
202 },
203 {
204 .speed = SPEED_1000,
205 .duplex = DUPLEX_FULL,
206 .setting = SUPPORTED_1000baseKX_Full,
207 },
00db8189
AF
208 {
209 .speed = SPEED_1000,
210 .duplex = DUPLEX_FULL,
211 .setting = SUPPORTED_1000baseT_Full,
212 },
213 {
214 .speed = SPEED_1000,
215 .duplex = DUPLEX_HALF,
216 .setting = SUPPORTED_1000baseT_Half,
217 },
218 {
219 .speed = SPEED_100,
220 .duplex = DUPLEX_FULL,
221 .setting = SUPPORTED_100baseT_Full,
222 },
223 {
224 .speed = SPEED_100,
225 .duplex = DUPLEX_HALF,
226 .setting = SUPPORTED_100baseT_Half,
227 },
228 {
229 .speed = SPEED_10,
230 .duplex = DUPLEX_FULL,
231 .setting = SUPPORTED_10baseT_Full,
232 },
233 {
234 .speed = SPEED_10,
235 .duplex = DUPLEX_HALF,
236 .setting = SUPPORTED_10baseT_Half,
237 },
238};
239
b3df0da8 240/**
d0613037 241 * phy_lookup_setting - lookup a PHY setting
b3df0da8
RD
242 * @speed: speed to match
243 * @duplex: duplex to match
d0613037
RK
244 * @feature: allowed link modes
245 * @exact: an exact match is required
246 *
247 * Search the settings array for a setting that matches the speed and
248 * duplex, and which is supported.
00db8189 249 *
d0613037
RK
250 * If @exact is unset, either an exact match or %NULL for no match will
251 * be returned.
252 *
253 * If @exact is set, an exact match, the fastest supported setting at
254 * or below the specified speed, the slowest supported setting, or if
255 * they all fail, %NULL will be returned.
00db8189 256 */
d0613037
RK
257static const struct phy_setting *
258phy_lookup_setting(int speed, int duplex, u32 features, bool exact)
00db8189 259{
d0613037
RK
260 const struct phy_setting *p, *match = NULL, *last = NULL;
261 int i;
262
263 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
264 if (p->setting & features) {
265 last = p;
266 if (p->speed == speed && p->duplex == duplex) {
267 /* Exact match for speed and duplex */
268 match = p;
269 break;
270 } else if (!exact) {
271 if (!match && p->speed <= speed)
272 /* Candidate */
273 match = p;
274
275 if (p->speed < speed)
276 break;
277 }
278 }
279 }
00db8189 280
d0613037
RK
281 if (!match && !exact)
282 match = last;
00db8189 283
d0613037 284 return match;
00db8189
AF
285}
286
b3df0da8 287/**
d0613037
RK
288 * phy_find_valid - find a PHY setting that matches the requested parameters
289 * @speed: desired speed
290 * @duplex: desired duplex
291 * @supported: mask of supported link modes
00db8189 292 *
d0613037
RK
293 * Locate a supported phy setting that is, in priority order:
294 * - an exact match for the specified speed and duplex mode
295 * - a match for the specified speed, or slower speed
296 * - the slowest supported speed
297 * Returns the matched phy_setting entry, or %NULL if no supported phy
298 * settings were found.
00db8189 299 */
d0613037
RK
300static const struct phy_setting *
301phy_find_valid(int speed, int duplex, u32 supported)
00db8189 302{
d0613037 303 return phy_lookup_setting(speed, duplex, supported, false);
00db8189
AF
304}
305
1f9127ca
ZB
306/**
307 * phy_supported_speeds - return all speeds currently supported by a phy device
308 * @phy: The phy device to return supported speeds of.
309 * @speeds: buffer to store supported speeds in.
310 * @size: size of speeds buffer.
311 *
312 * Description: Returns the number of supported speeds, and fills the speeds
313 * buffer with the supported speeds. If speeds buffer is too small to contain
314 * all currently supported speeds, will return as many speeds as can fit.
315 */
316unsigned int phy_supported_speeds(struct phy_device *phy,
317 unsigned int *speeds,
318 unsigned int size)
319{
320 unsigned int count = 0;
321 unsigned int idx = 0;
322
786df9c2 323 for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++)
1f9127ca 324 /* Assumes settings are grouped by speed */
786df9c2
RK
325 if ((settings[idx].setting & phy->supported) &&
326 (count == 0 || speeds[count - 1] != settings[idx].speed))
327 speeds[count++] = settings[idx].speed;
1f9127ca
ZB
328
329 return count;
330}
331
54da5a8b
GR
332/**
333 * phy_check_valid - check if there is a valid PHY setting which matches
334 * speed, duplex, and feature mask
335 * @speed: speed to match
336 * @duplex: duplex to match
337 * @features: A mask of the valid settings
338 *
339 * Description: Returns true if there is a valid setting, false otherwise.
340 */
341static inline bool phy_check_valid(int speed, int duplex, u32 features)
342{
d0613037 343 return !!phy_lookup_setting(speed, duplex, features, true);
54da5a8b
GR
344}
345
b3df0da8
RD
346/**
347 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
348 * @phydev: the target phy_device struct
00db8189 349 *
b3df0da8 350 * Description: Make sure the PHY is set to supported speeds and
00db8189 351 * duplexes. Drop down by one in this order: 1000/FULL,
b3df0da8 352 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
00db8189 353 */
89ff05ec 354static void phy_sanitize_settings(struct phy_device *phydev)
00db8189 355{
d0613037 356 const struct phy_setting *setting;
00db8189 357 u32 features = phydev->supported;
00db8189
AF
358
359 /* Sanitize settings based on PHY capabilities */
360 if ((features & SUPPORTED_Autoneg) == 0)
163642a2 361 phydev->autoneg = AUTONEG_DISABLE;
00db8189 362
d0613037
RK
363 setting = phy_find_valid(phydev->speed, phydev->duplex, features);
364 if (setting) {
365 phydev->speed = setting->speed;
366 phydev->duplex = setting->duplex;
367 } else {
368 /* We failed to find anything (no supported speeds?) */
369 phydev->speed = SPEED_UNKNOWN;
370 phydev->duplex = DUPLEX_UNKNOWN;
371 }
00db8189 372}
00db8189 373
b3df0da8
RD
374/**
375 * phy_ethtool_sset - generic ethtool sset function, handles all the details
376 * @phydev: target phy_device struct
377 * @cmd: ethtool_cmd
00db8189
AF
378 *
379 * A few notes about parameter checking:
380 * - We don't set port or transceiver, so we don't care what they
381 * were set to.
382 * - phy_start_aneg() will make sure forced settings are sane, and
383 * choose the next best ones from the ones selected, so we don't
b3df0da8 384 * care if ethtool tries to give us bad values.
00db8189
AF
385 */
386int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
387{
25db0338
DD
388 u32 speed = ethtool_cmd_speed(cmd);
389
e5a03bfd 390 if (cmd->phy_address != phydev->mdio.addr)
00db8189
AF
391 return -EINVAL;
392
2f53e904 393 /* We make sure that we don't pass unsupported values in to the PHY */
00db8189
AF
394 cmd->advertising &= phydev->supported;
395
396 /* Verify the settings we care about. */
397 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
398 return -EINVAL;
399
400 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
401 return -EINVAL;
402
8e95a202 403 if (cmd->autoneg == AUTONEG_DISABLE &&
25db0338
DD
404 ((speed != SPEED_1000 &&
405 speed != SPEED_100 &&
406 speed != SPEED_10) ||
8e95a202
JP
407 (cmd->duplex != DUPLEX_HALF &&
408 cmd->duplex != DUPLEX_FULL)))
00db8189
AF
409 return -EINVAL;
410
411 phydev->autoneg = cmd->autoneg;
412
25db0338 413 phydev->speed = speed;
00db8189
AF
414
415 phydev->advertising = cmd->advertising;
416
417 if (AUTONEG_ENABLE == cmd->autoneg)
418 phydev->advertising |= ADVERTISED_Autoneg;
419 else
420 phydev->advertising &= ~ADVERTISED_Autoneg;
421
422 phydev->duplex = cmd->duplex;
423
1004ee61 424 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
634ec36c 425
00db8189
AF
426 /* Restart the PHY */
427 phy_start_aneg(phydev);
428
429 return 0;
430}
9f6d55d0 431EXPORT_SYMBOL(phy_ethtool_sset);
00db8189 432
2d55173e
PR
433int phy_ethtool_ksettings_set(struct phy_device *phydev,
434 const struct ethtool_link_ksettings *cmd)
435{
436 u8 autoneg = cmd->base.autoneg;
437 u8 duplex = cmd->base.duplex;
438 u32 speed = cmd->base.speed;
439 u32 advertising;
440
441 if (cmd->base.phy_address != phydev->mdio.addr)
442 return -EINVAL;
443
444 ethtool_convert_link_mode_to_legacy_u32(&advertising,
445 cmd->link_modes.advertising);
446
447 /* We make sure that we don't pass unsupported values in to the PHY */
448 advertising &= phydev->supported;
449
450 /* Verify the settings we care about. */
451 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
452 return -EINVAL;
453
454 if (autoneg == AUTONEG_ENABLE && advertising == 0)
455 return -EINVAL;
456
457 if (autoneg == AUTONEG_DISABLE &&
458 ((speed != SPEED_1000 &&
459 speed != SPEED_100 &&
460 speed != SPEED_10) ||
461 (duplex != DUPLEX_HALF &&
462 duplex != DUPLEX_FULL)))
463 return -EINVAL;
464
465 phydev->autoneg = autoneg;
466
467 phydev->speed = speed;
468
469 phydev->advertising = advertising;
470
471 if (autoneg == AUTONEG_ENABLE)
472 phydev->advertising |= ADVERTISED_Autoneg;
473 else
474 phydev->advertising &= ~ADVERTISED_Autoneg;
475
476 phydev->duplex = duplex;
477
1004ee61 478 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
2d55173e
PR
479
480 /* Restart the PHY */
481 phy_start_aneg(phydev);
482
483 return 0;
484}
485EXPORT_SYMBOL(phy_ethtool_ksettings_set);
486
2d55173e
PR
487int phy_ethtool_ksettings_get(struct phy_device *phydev,
488 struct ethtool_link_ksettings *cmd)
489{
490 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
491 phydev->supported);
492
493 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
494 phydev->advertising);
495
496 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
497 phydev->lp_advertising);
498
499 cmd->base.speed = phydev->speed;
500 cmd->base.duplex = phydev->duplex;
501 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
502 cmd->base.port = PORT_BNC;
503 else
504 cmd->base.port = PORT_MII;
505
506 cmd->base.phy_address = phydev->mdio.addr;
507 cmd->base.autoneg = phydev->autoneg;
1004ee61
RL
508 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
509 cmd->base.eth_tp_mdix = phydev->mdix;
2d55173e
PR
510
511 return 0;
512}
513EXPORT_SYMBOL(phy_ethtool_ksettings_get);
00db8189 514
b3df0da8
RD
515/**
516 * phy_mii_ioctl - generic PHY MII ioctl interface
517 * @phydev: the phy_device struct
00c7d920 518 * @ifr: &struct ifreq for socket ioctl's
b3df0da8
RD
519 * @cmd: ioctl cmd to execute
520 *
521 * Note that this function is currently incompatible with the
00db8189 522 * PHYCONTROL layer. It changes registers without regard to
b3df0da8 523 * current state. Use at own risk.
00db8189 524 */
2f53e904 525int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
00db8189 526{
28b04113 527 struct mii_ioctl_data *mii_data = if_mii(ifr);
00db8189 528 u16 val = mii_data->val_in;
79ce0477 529 bool change_autoneg = false;
00db8189
AF
530
531 switch (cmd) {
532 case SIOCGMIIPHY:
e5a03bfd 533 mii_data->phy_id = phydev->mdio.addr;
c6d6a511
LB
534 /* fall through */
535
00db8189 536 case SIOCGMIIREG:
e5a03bfd
AL
537 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
538 mii_data->phy_id,
af1dc13e 539 mii_data->reg_num);
e62a768f 540 return 0;
00db8189
AF
541
542 case SIOCSMIIREG:
e5a03bfd 543 if (mii_data->phy_id == phydev->mdio.addr) {
e109374f 544 switch (mii_data->reg_num) {
00db8189 545 case MII_BMCR:
79ce0477
BH
546 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
547 if (phydev->autoneg == AUTONEG_ENABLE)
548 change_autoneg = true;
00db8189 549 phydev->autoneg = AUTONEG_DISABLE;
79ce0477
BH
550 if (val & BMCR_FULLDPLX)
551 phydev->duplex = DUPLEX_FULL;
552 else
553 phydev->duplex = DUPLEX_HALF;
554 if (val & BMCR_SPEED1000)
555 phydev->speed = SPEED_1000;
556 else if (val & BMCR_SPEED100)
557 phydev->speed = SPEED_100;
558 else phydev->speed = SPEED_10;
559 }
560 else {
561 if (phydev->autoneg == AUTONEG_DISABLE)
562 change_autoneg = true;
00db8189 563 phydev->autoneg = AUTONEG_ENABLE;
79ce0477 564 }
00db8189
AF
565 break;
566 case MII_ADVERTISE:
79ce0477
BH
567 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
568 change_autoneg = true;
00db8189
AF
569 break;
570 default:
571 /* do nothing */
572 break;
573 }
574 }
575
e5a03bfd 576 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
af1dc13e
PK
577 mii_data->reg_num, val);
578
e5a03bfd 579 if (mii_data->phy_id == phydev->mdio.addr &&
cf18b778 580 mii_data->reg_num == MII_BMCR &&
2613f95f 581 val & BMCR_RESET)
e62a768f 582 return phy_init_hw(phydev);
79ce0477
BH
583
584 if (change_autoneg)
585 return phy_start_aneg(phydev);
586
e62a768f 587 return 0;
dda93b48 588
c1f19b51 589 case SIOCSHWTSTAMP:
25149ef9 590 if (phydev->drv && phydev->drv->hwtstamp)
c1f19b51
RC
591 return phydev->drv->hwtstamp(phydev, ifr);
592 /* fall through */
593
dda93b48 594 default:
c6d6a511 595 return -EOPNOTSUPP;
00db8189 596 }
00db8189 597}
680e9fe9 598EXPORT_SYMBOL(phy_mii_ioctl);
00db8189 599
b3df0da8 600/**
f555f34f 601 * phy_start_aneg_priv - start auto-negotiation for this PHY device
b3df0da8 602 * @phydev: the phy_device struct
f555f34f 603 * @sync: indicate whether we should wait for the workqueue cancelation
e1393456 604 *
b3df0da8
RD
605 * Description: Sanitizes the settings (if we're not autonegotiating
606 * them), and then calls the driver's config_aneg function.
607 * If the PHYCONTROL Layer is operating, we change the state to
608 * reflect the beginning of Auto-negotiation or forcing.
e1393456 609 */
f555f34f 610static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
e1393456 611{
f555f34f 612 bool trigger = 0;
e1393456
AF
613 int err;
614
25149ef9
FF
615 if (!phydev->drv)
616 return -EIO;
617
35b5f6b1 618 mutex_lock(&phydev->lock);
e1393456
AF
619
620 if (AUTONEG_DISABLE == phydev->autoneg)
621 phy_sanitize_settings(phydev);
622
9b3320ef
BH
623 /* Invalidate LP advertising flags */
624 phydev->lp_advertising = 0;
625
e1393456 626 err = phydev->drv->config_aneg(phydev);
e1393456
AF
627 if (err < 0)
628 goto out_unlock;
629
630 if (phydev->state != PHY_HALTED) {
631 if (AUTONEG_ENABLE == phydev->autoneg) {
632 phydev->state = PHY_AN;
633 phydev->link_timeout = PHY_AN_TIMEOUT;
634 } else {
635 phydev->state = PHY_FORCING;
636 phydev->link_timeout = PHY_FORCE_TIMEOUT;
637 }
638 }
639
f555f34f
AK
640 /* Re-schedule a PHY state machine to check PHY status because
641 * negotiation may already be done and aneg interrupt may not be
642 * generated.
643 */
644 if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) {
645 err = phy_aneg_done(phydev);
646 if (err > 0) {
647 trigger = true;
648 err = 0;
649 }
650 }
651
e1393456 652out_unlock:
35b5f6b1 653 mutex_unlock(&phydev->lock);
f555f34f
AK
654
655 if (trigger)
656 phy_trigger_machine(phydev, sync);
657
e1393456
AF
658 return err;
659}
f555f34f
AK
660
661/**
662 * phy_start_aneg - start auto-negotiation for this PHY device
663 * @phydev: the phy_device struct
664 *
665 * Description: Sanitizes the settings (if we're not autonegotiating
666 * them), and then calls the driver's config_aneg function.
667 * If the PHYCONTROL Layer is operating, we change the state to
668 * reflect the beginning of Auto-negotiation or forcing.
669 */
670int phy_start_aneg(struct phy_device *phydev)
671{
672 return phy_start_aneg_priv(phydev, true);
673}
e1393456
AF
674EXPORT_SYMBOL(phy_start_aneg);
675
b3df0da8
RD
676/**
677 * phy_start_machine - start PHY state machine tracking
678 * @phydev: the phy_device struct
00db8189 679 *
b3df0da8 680 * Description: The PHY infrastructure can run a state machine
00db8189
AF
681 * which tracks whether the PHY is starting up, negotiating,
682 * etc. This function starts the timer which tracks the state
29935aeb
SS
683 * of the PHY. If you want to maintain your own state machine,
684 * do not call this function.
b3df0da8 685 */
29935aeb 686void phy_start_machine(struct phy_device *phydev)
00db8189 687{
bbb47bde 688 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
00db8189
AF
689}
690
3c293f4e
AL
691/**
692 * phy_trigger_machine - trigger the state machine to run
693 *
694 * @phydev: the phy_device struct
eab12771 695 * @sync: indicate whether we should wait for the workqueue cancelation
3c293f4e
AL
696 *
697 * Description: There has been a change in state which requires that the
698 * state machine runs.
699 */
700
f555f34f 701void phy_trigger_machine(struct phy_device *phydev, bool sync)
3c293f4e 702{
eab12771
FF
703 if (sync)
704 cancel_delayed_work_sync(&phydev->state_queue);
705 else
706 cancel_delayed_work(&phydev->state_queue);
3c293f4e
AL
707 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
708}
709
b3df0da8
RD
710/**
711 * phy_stop_machine - stop the PHY state machine tracking
712 * @phydev: target phy_device struct
00db8189 713 *
b3df0da8 714 * Description: Stops the state machine timer, sets the state to UP
817acf5e 715 * (unless it wasn't up yet). This function must be called BEFORE
00db8189
AF
716 * phy_detach.
717 */
718void phy_stop_machine(struct phy_device *phydev)
719{
a390d1f3 720 cancel_delayed_work_sync(&phydev->state_queue);
00db8189 721
35b5f6b1 722 mutex_lock(&phydev->lock);
49d52e81 723 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
00db8189 724 phydev->state = PHY_UP;
35b5f6b1 725 mutex_unlock(&phydev->lock);
00db8189
AF
726}
727
b3df0da8
RD
728/**
729 * phy_error - enter HALTED state for this PHY device
730 * @phydev: target phy_device struct
00db8189
AF
731 *
732 * Moves the PHY to the HALTED state in response to a read
733 * or write error, and tells the controller the link is down.
734 * Must not be called from interrupt context, or while the
735 * phydev->lock is held.
736 */
9b9a8bfc 737static void phy_error(struct phy_device *phydev)
00db8189 738{
35b5f6b1 739 mutex_lock(&phydev->lock);
00db8189 740 phydev->state = PHY_HALTED;
35b5f6b1 741 mutex_unlock(&phydev->lock);
3c293f4e 742
eab12771 743 phy_trigger_machine(phydev, false);
00db8189
AF
744}
745
b3df0da8
RD
746/**
747 * phy_interrupt - PHY interrupt handler
748 * @irq: interrupt line
749 * @phy_dat: phy_device pointer
e1393456 750 *
b3df0da8 751 * Description: When a PHY interrupt occurs, the handler disables
664fcf12 752 * interrupts, and uses phy_change to handle the interrupt.
e1393456 753 */
7d12e780 754static irqreturn_t phy_interrupt(int irq, void *phy_dat)
e1393456
AF
755{
756 struct phy_device *phydev = phy_dat;
757
3c3070d7
MR
758 if (PHY_HALTED == phydev->state)
759 return IRQ_NONE; /* It can't be ours. */
760
e1393456 761 disable_irq_nosync(irq);
0ac49527 762 atomic_inc(&phydev->irq_disable);
e1393456 763
664fcf12 764 phy_change(phydev);
e1393456
AF
765
766 return IRQ_HANDLED;
767}
768
b3df0da8
RD
769/**
770 * phy_enable_interrupts - Enable the interrupts from the PHY side
771 * @phydev: target phy_device struct
772 */
89ff05ec 773static int phy_enable_interrupts(struct phy_device *phydev)
00db8189 774{
553fe92b 775 int err = phy_clear_interrupt(phydev);
00db8189 776
e1393456
AF
777 if (err < 0)
778 return err;
00db8189 779
553fe92b 780 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
00db8189 781}
00db8189 782
b3df0da8
RD
783/**
784 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
785 * @phydev: target phy_device struct
786 */
89ff05ec 787static int phy_disable_interrupts(struct phy_device *phydev)
00db8189
AF
788{
789 int err;
790
791 /* Disable PHY interrupts */
792 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
00db8189
AF
793 if (err)
794 goto phy_err;
795
796 /* Clear the interrupt */
797 err = phy_clear_interrupt(phydev);
00db8189
AF
798 if (err)
799 goto phy_err;
800
801 return 0;
802
803phy_err:
804 phy_error(phydev);
805
806 return err;
807}
e1393456 808
b3df0da8
RD
809/**
810 * phy_start_interrupts - request and enable interrupts for a PHY device
811 * @phydev: target phy_device struct
e1393456 812 *
b3df0da8
RD
813 * Description: Request the interrupt for the given PHY.
814 * If this fails, then we set irq to PHY_POLL.
e1393456 815 * Otherwise, we enable the interrupts in the PHY.
e1393456 816 * This should only be called with a valid IRQ number.
b3df0da8 817 * Returns 0 on success or < 0 on error.
e1393456
AF
818 */
819int phy_start_interrupts(struct phy_device *phydev)
820{
0ac49527 821 atomic_set(&phydev->irq_disable, 0);
c974bdbc
AL
822 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
823 IRQF_ONESHOT | IRQF_SHARED,
ae0219cb 824 phydev_name(phydev), phydev) < 0) {
8d242488 825 pr_warn("%s: Can't get IRQ %d (PHY)\n",
e5a03bfd 826 phydev->mdio.bus->name, phydev->irq);
e1393456
AF
827 phydev->irq = PHY_POLL;
828 return 0;
829 }
830
e62a768f 831 return phy_enable_interrupts(phydev);
e1393456
AF
832}
833EXPORT_SYMBOL(phy_start_interrupts);
834
b3df0da8
RD
835/**
836 * phy_stop_interrupts - disable interrupts from a PHY device
837 * @phydev: target phy_device struct
838 */
e1393456
AF
839int phy_stop_interrupts(struct phy_device *phydev)
840{
553fe92b 841 int err = phy_disable_interrupts(phydev);
e1393456
AF
842
843 if (err)
844 phy_error(phydev);
845
0ac49527
MR
846 free_irq(phydev->irq, phydev);
847
2f53e904 848 /* If work indeed has been cancelled, disable_irq() will have
0ac49527
MR
849 * been left unbalanced from phy_interrupt() and enable_irq()
850 * has to be called so that other devices on the line work.
851 */
852 while (atomic_dec_return(&phydev->irq_disable) >= 0)
853 enable_irq(phydev->irq);
e1393456
AF
854
855 return err;
856}
857EXPORT_SYMBOL(phy_stop_interrupts);
858
b3df0da8 859/**
664fcf12
AL
860 * phy_change - Called by the phy_interrupt to handle PHY changes
861 * @phydev: phy_device struct that interrupted
b3df0da8 862 */
664fcf12 863void phy_change(struct phy_device *phydev)
e1393456 864{
deccd16f
FF
865 if (phy_interrupt_is_valid(phydev)) {
866 if (phydev->drv->did_interrupt &&
867 !phydev->drv->did_interrupt(phydev))
868 goto ignore;
a8729eb3 869
deccd16f
FF
870 if (phy_disable_interrupts(phydev))
871 goto phy_err;
872 }
e1393456 873
35b5f6b1 874 mutex_lock(&phydev->lock);
e1393456
AF
875 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
876 phydev->state = PHY_CHANGELINK;
35b5f6b1 877 mutex_unlock(&phydev->lock);
e1393456 878
deccd16f
FF
879 if (phy_interrupt_is_valid(phydev)) {
880 atomic_dec(&phydev->irq_disable);
881 enable_irq(phydev->irq);
e1393456 882
deccd16f
FF
883 /* Reenable interrupts */
884 if (PHY_HALTED != phydev->state &&
885 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
886 goto irq_enable_err;
887 }
e1393456 888
a390d1f3 889 /* reschedule state queue work to run as soon as possible */
eab12771 890 phy_trigger_machine(phydev, true);
e1393456
AF
891 return;
892
a8729eb3
AG
893ignore:
894 atomic_dec(&phydev->irq_disable);
895 enable_irq(phydev->irq);
896 return;
897
e1393456
AF
898irq_enable_err:
899 disable_irq(phydev->irq);
0ac49527 900 atomic_inc(&phydev->irq_disable);
e1393456
AF
901phy_err:
902 phy_error(phydev);
903}
904
664fcf12
AL
905/**
906 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
907 * @work: work_struct that describes the work to be done
908 */
909void phy_change_work(struct work_struct *work)
910{
911 struct phy_device *phydev =
912 container_of(work, struct phy_device, phy_queue);
913
914 phy_change(phydev);
915}
916
b3df0da8
RD
917/**
918 * phy_stop - Bring down the PHY link, and stop checking the status
919 * @phydev: target phy_device struct
920 */
e1393456
AF
921void phy_stop(struct phy_device *phydev)
922{
35b5f6b1 923 mutex_lock(&phydev->lock);
e1393456
AF
924
925 if (PHY_HALTED == phydev->state)
926 goto out_unlock;
927
2c7b4921 928 if (phy_interrupt_is_valid(phydev)) {
e1393456
AF
929 /* Disable PHY Interrupts */
930 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
e1393456 931
3c3070d7
MR
932 /* Clear any pending interrupts */
933 phy_clear_interrupt(phydev);
934 }
e1393456 935
6daf6531
MR
936 phydev->state = PHY_HALTED;
937
e1393456 938out_unlock:
35b5f6b1 939 mutex_unlock(&phydev->lock);
3c3070d7 940
2f53e904 941 /* Cannot call flush_scheduled_work() here as desired because
3c3070d7
MR
942 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
943 * will not reenable interrupts.
944 */
e1393456 945}
2f53e904 946EXPORT_SYMBOL(phy_stop);
e1393456 947
b3df0da8
RD
948/**
949 * phy_start - start or restart a PHY device
950 * @phydev: target phy_device struct
e1393456 951 *
b3df0da8 952 * Description: Indicates the attached device's readiness to
e1393456
AF
953 * handle PHY-related work. Used during startup to start the
954 * PHY, and after a call to phy_stop() to resume operation.
955 * Also used to indicate the MDIO bus has cleared an error
956 * condition.
957 */
958void phy_start(struct phy_device *phydev)
959{
c15e10e7
TB
960 bool do_resume = false;
961 int err = 0;
962
35b5f6b1 963 mutex_lock(&phydev->lock);
e1393456
AF
964
965 switch (phydev->state) {
e109374f
FF
966 case PHY_STARTING:
967 phydev->state = PHY_PENDING;
968 break;
969 case PHY_READY:
970 phydev->state = PHY_UP;
971 break;
972 case PHY_HALTED:
c15e10e7 973 /* make sure interrupts are re-enabled for the PHY */
84a527a4
SX
974 if (phydev->irq != PHY_POLL) {
975 err = phy_enable_interrupts(phydev);
976 if (err < 0)
977 break;
978 }
c15e10e7 979
e109374f 980 phydev->state = PHY_RESUMING;
c15e10e7
TB
981 do_resume = true;
982 break;
e109374f
FF
983 default:
984 break;
e1393456 985 }
35b5f6b1 986 mutex_unlock(&phydev->lock);
c15e10e7
TB
987
988 /* if phy was suspended, bring the physical link up again */
989 if (do_resume)
990 phy_resume(phydev);
3c293f4e 991
eab12771 992 phy_trigger_machine(phydev, true);
e1393456 993}
e1393456 994EXPORT_SYMBOL(phy_start);
67c4f3fa 995
61a17965
ZB
996static void phy_adjust_link(struct phy_device *phydev)
997{
998 phydev->adjust_link(phydev->attached_dev);
2e0bc452 999 phy_led_trigger_change_speed(phydev);
61a17965
ZB
1000}
1001
35b5f6b1
NC
1002/**
1003 * phy_state_machine - Handle the state machine
1004 * @work: work_struct that describes the work to be done
35b5f6b1 1005 */
4f9c85a1 1006void phy_state_machine(struct work_struct *work)
00db8189 1007{
bf6aede7 1008 struct delayed_work *dwork = to_delayed_work(work);
35b5f6b1 1009 struct phy_device *phydev =
a390d1f3 1010 container_of(dwork, struct phy_device, state_queue);
c15e10e7 1011 bool needs_aneg = false, do_suspend = false;
3e2186e0 1012 enum phy_state old_state;
00db8189 1013 int err = 0;
11e122cb 1014 int old_link;
00db8189 1015
35b5f6b1 1016 mutex_lock(&phydev->lock);
00db8189 1017
3e2186e0
FF
1018 old_state = phydev->state;
1019
25149ef9 1020 if (phydev->drv && phydev->drv->link_change_notify)
2b8f2a28
DM
1021 phydev->drv->link_change_notify(phydev);
1022
e109374f
FF
1023 switch (phydev->state) {
1024 case PHY_DOWN:
1025 case PHY_STARTING:
1026 case PHY_READY:
1027 case PHY_PENDING:
1028 break;
1029 case PHY_UP:
6e14a5ee 1030 needs_aneg = true;
00db8189 1031
e109374f
FF
1032 phydev->link_timeout = PHY_AN_TIMEOUT;
1033
1034 break;
1035 case PHY_AN:
1036 err = phy_read_status(phydev);
e109374f 1037 if (err < 0)
00db8189 1038 break;
6b655529 1039
2f53e904 1040 /* If the link is down, give up on negotiation for now */
e109374f
FF
1041 if (!phydev->link) {
1042 phydev->state = PHY_NOLINK;
1043 netif_carrier_off(phydev->attached_dev);
61a17965 1044 phy_adjust_link(phydev);
e109374f
FF
1045 break;
1046 }
6b655529 1047
2f53e904 1048 /* Check if negotiation is done. Break if there's an error */
e109374f
FF
1049 err = phy_aneg_done(phydev);
1050 if (err < 0)
1051 break;
6b655529 1052
e109374f
FF
1053 /* If AN is done, we're running */
1054 if (err > 0) {
1055 phydev->state = PHY_RUNNING;
1056 netif_carrier_on(phydev->attached_dev);
61a17965 1057 phy_adjust_link(phydev);
00db8189 1058
fa8cddaf 1059 } else if (0 == phydev->link_timeout--)
6e14a5ee 1060 needs_aneg = true;
e109374f
FF
1061 break;
1062 case PHY_NOLINK:
321beec5
AL
1063 if (phy_interrupt_is_valid(phydev))
1064 break;
1065
e109374f 1066 err = phy_read_status(phydev);
e109374f 1067 if (err)
00db8189 1068 break;
00db8189 1069
e109374f 1070 if (phydev->link) {
e46e08b8
BK
1071 if (AUTONEG_ENABLE == phydev->autoneg) {
1072 err = phy_aneg_done(phydev);
1073 if (err < 0)
1074 break;
1075
1076 if (!err) {
1077 phydev->state = PHY_AN;
1078 phydev->link_timeout = PHY_AN_TIMEOUT;
1079 break;
1080 }
1081 }
e109374f
FF
1082 phydev->state = PHY_RUNNING;
1083 netif_carrier_on(phydev->attached_dev);
61a17965 1084 phy_adjust_link(phydev);
e109374f
FF
1085 }
1086 break;
1087 case PHY_FORCING:
1088 err = genphy_update_link(phydev);
e109374f 1089 if (err)
00db8189 1090 break;
00db8189 1091
e109374f
FF
1092 if (phydev->link) {
1093 phydev->state = PHY_RUNNING;
1094 netif_carrier_on(phydev->attached_dev);
1095 } else {
1096 if (0 == phydev->link_timeout--)
6e14a5ee 1097 needs_aneg = true;
e109374f 1098 }
00db8189 1099
61a17965 1100 phy_adjust_link(phydev);
e109374f
FF
1101 break;
1102 case PHY_RUNNING:
d5c3d846
FF
1103 /* Only register a CHANGE if we are polling and link changed
1104 * since latest checking.
e109374f 1105 */
d5c3d846 1106 if (phydev->irq == PHY_POLL) {
11e122cb
SX
1107 old_link = phydev->link;
1108 err = phy_read_status(phydev);
1109 if (err)
1110 break;
1111
1112 if (old_link != phydev->link)
1113 phydev->state = PHY_CHANGELINK;
1114 }
811a9191
ZK
1115 /*
1116 * Failsafe: check that nobody set phydev->link=0 between two
1117 * poll cycles, otherwise we won't leave RUNNING state as long
1118 * as link remains down.
1119 */
1120 if (!phydev->link && phydev->state == PHY_RUNNING) {
1121 phydev->state = PHY_CHANGELINK;
1122 phydev_err(phydev, "no link in PHY_RUNNING\n");
1123 }
e109374f
FF
1124 break;
1125 case PHY_CHANGELINK:
1126 err = phy_read_status(phydev);
e109374f 1127 if (err)
00db8189 1128 break;
00db8189 1129
e109374f
FF
1130 if (phydev->link) {
1131 phydev->state = PHY_RUNNING;
1132 netif_carrier_on(phydev->attached_dev);
1133 } else {
1134 phydev->state = PHY_NOLINK;
1135 netif_carrier_off(phydev->attached_dev);
1136 }
00db8189 1137
61a17965 1138 phy_adjust_link(phydev);
00db8189 1139
e109374f
FF
1140 if (phy_interrupt_is_valid(phydev))
1141 err = phy_config_interrupt(phydev,
2f53e904 1142 PHY_INTERRUPT_ENABLED);
e109374f
FF
1143 break;
1144 case PHY_HALTED:
1145 if (phydev->link) {
1146 phydev->link = 0;
1147 netif_carrier_off(phydev->attached_dev);
61a17965 1148 phy_adjust_link(phydev);
6e14a5ee 1149 do_suspend = true;
e109374f
FF
1150 }
1151 break;
1152 case PHY_RESUMING:
e109374f
FF
1153 if (AUTONEG_ENABLE == phydev->autoneg) {
1154 err = phy_aneg_done(phydev);
1155 if (err < 0)
00db8189
AF
1156 break;
1157
e109374f 1158 /* err > 0 if AN is done.
2f53e904
SS
1159 * Otherwise, it's 0, and we're still waiting for AN
1160 */
e109374f 1161 if (err > 0) {
42caa074
WF
1162 err = phy_read_status(phydev);
1163 if (err)
1164 break;
1165
1166 if (phydev->link) {
1167 phydev->state = PHY_RUNNING;
1168 netif_carrier_on(phydev->attached_dev);
2f53e904 1169 } else {
42caa074 1170 phydev->state = PHY_NOLINK;
2f53e904 1171 }
61a17965 1172 phy_adjust_link(phydev);
e109374f
FF
1173 } else {
1174 phydev->state = PHY_AN;
1175 phydev->link_timeout = PHY_AN_TIMEOUT;
42caa074 1176 }
e109374f
FF
1177 } else {
1178 err = phy_read_status(phydev);
1179 if (err)
1180 break;
1181
1182 if (phydev->link) {
1183 phydev->state = PHY_RUNNING;
1184 netif_carrier_on(phydev->attached_dev);
2f53e904 1185 } else {
e109374f 1186 phydev->state = PHY_NOLINK;
2f53e904 1187 }
61a17965 1188 phy_adjust_link(phydev);
e109374f
FF
1189 }
1190 break;
00db8189
AF
1191 }
1192
35b5f6b1 1193 mutex_unlock(&phydev->lock);
00db8189
AF
1194
1195 if (needs_aneg)
f555f34f 1196 err = phy_start_aneg_priv(phydev, false);
6e14a5ee 1197 else if (do_suspend)
be9dad1f
SH
1198 phy_suspend(phydev);
1199
00db8189
AF
1200 if (err < 0)
1201 phy_error(phydev);
1202
72ba48be
AL
1203 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1204 phy_state_to_str(old_state),
1205 phy_state_to_str(phydev->state));
3e2186e0 1206
d5c3d846
FF
1207 /* Only re-schedule a PHY state machine change if we are polling the
1208 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1209 * between states from phy_mac_interrupt()
1210 */
1211 if (phydev->irq == PHY_POLL)
1212 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1213 PHY_STATE_TIME * HZ);
35b5f6b1 1214}
a59a4d19 1215
664fcf12
AL
1216/**
1217 * phy_mac_interrupt - MAC says the link has changed
1218 * @phydev: phy_device struct with changed link
1219 * @new_link: Link is Up/Down.
1220 *
1221 * Description: The MAC layer is able indicate there has been a change
1222 * in the PHY link status. Set the new link status, and trigger the
1223 * state machine, work a work queue.
1224 */
5ea94e76
FF
1225void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1226{
5ea94e76 1227 phydev->link = new_link;
deccd16f
FF
1228
1229 /* Trigger a state machine change */
1230 queue_work(system_power_efficient_wq, &phydev->phy_queue);
5ea94e76
FF
1231}
1232EXPORT_SYMBOL(phy_mac_interrupt);
1233
a59a4d19
GC
1234/**
1235 * phy_init_eee - init and check the EEE feature
1236 * @phydev: target phy_device struct
1237 * @clk_stop_enable: PHY may stop the clock during LPI
1238 *
1239 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1240 * is supported by looking at the MMD registers 3.20 and 7.60/61
1241 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1242 * bit if required.
1243 */
1244int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1245{
25149ef9
FF
1246 if (!phydev->drv)
1247 return -EIO;
1248
a59a4d19 1249 /* According to 802.3az,the EEE is supported only in full duplex-mode.
a59a4d19 1250 */
32d75141 1251 if (phydev->duplex == DUPLEX_FULL) {
a59a4d19
GC
1252 int eee_lp, eee_cap, eee_adv;
1253 u32 lp, cap, adv;
4ae6e50c 1254 int status;
a59a4d19
GC
1255
1256 /* Read phy status to properly get the right settings */
1257 status = phy_read_status(phydev);
1258 if (status)
1259 return status;
1260
1261 /* First check if the EEE ability is supported */
a6d99fcd 1262 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
7a4cecf7
GC
1263 if (eee_cap <= 0)
1264 goto eee_exit_err;
a59a4d19 1265
b32607dd 1266 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
a59a4d19 1267 if (!cap)
7a4cecf7 1268 goto eee_exit_err;
a59a4d19
GC
1269
1270 /* Check which link settings negotiated and verify it in
1271 * the EEE advertising registers.
1272 */
a6d99fcd 1273 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
7a4cecf7
GC
1274 if (eee_lp <= 0)
1275 goto eee_exit_err;
a59a4d19 1276
a6d99fcd 1277 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
7a4cecf7
GC
1278 if (eee_adv <= 0)
1279 goto eee_exit_err;
a59a4d19 1280
b32607dd
AB
1281 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1282 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
54da5a8b 1283 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
7a4cecf7 1284 goto eee_exit_err;
a59a4d19
GC
1285
1286 if (clk_stop_enable) {
1287 /* Configure the PHY to stop receiving xMII
1288 * clock while it is signaling LPI.
1289 */
a6d99fcd 1290 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
a59a4d19
GC
1291 if (val < 0)
1292 return val;
1293
1294 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
a6d99fcd 1295 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
a59a4d19
GC
1296 }
1297
e62a768f 1298 return 0; /* EEE supported */
a59a4d19 1299 }
7a4cecf7 1300eee_exit_err:
e62a768f 1301 return -EPROTONOSUPPORT;
a59a4d19
GC
1302}
1303EXPORT_SYMBOL(phy_init_eee);
1304
1305/**
1306 * phy_get_eee_err - report the EEE wake error count
1307 * @phydev: target phy_device struct
1308 *
1309 * Description: it is to report the number of time where the PHY
1310 * failed to complete its normal wake sequence.
1311 */
1312int phy_get_eee_err(struct phy_device *phydev)
1313{
25149ef9
FF
1314 if (!phydev->drv)
1315 return -EIO;
1316
a6d99fcd 1317 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
a59a4d19
GC
1318}
1319EXPORT_SYMBOL(phy_get_eee_err);
1320
1321/**
1322 * phy_ethtool_get_eee - get EEE supported and status
1323 * @phydev: target phy_device struct
1324 * @data: ethtool_eee data
1325 *
1326 * Description: it reportes the Supported/Advertisement/LP Advertisement
1327 * capabilities.
1328 */
1329int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1330{
1331 int val;
1332
25149ef9
FF
1333 if (!phydev->drv)
1334 return -EIO;
1335
a59a4d19 1336 /* Get Supported EEE */
a6d99fcd 1337 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
a59a4d19
GC
1338 if (val < 0)
1339 return val;
b32607dd 1340 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
a59a4d19
GC
1341
1342 /* Get advertisement EEE */
a6d99fcd 1343 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
a59a4d19
GC
1344 if (val < 0)
1345 return val;
b32607dd 1346 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
a59a4d19
GC
1347
1348 /* Get LP advertisement EEE */
a6d99fcd 1349 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
a59a4d19
GC
1350 if (val < 0)
1351 return val;
b32607dd 1352 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
a59a4d19
GC
1353
1354 return 0;
1355}
1356EXPORT_SYMBOL(phy_ethtool_get_eee);
1357
1358/**
1359 * phy_ethtool_set_eee - set EEE supported and status
1360 * @phydev: target phy_device struct
1361 * @data: ethtool_eee data
1362 *
1363 * Description: it is to program the Advertisement EEE register.
1364 */
1365int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1366{
f75abeb8 1367 int cap, old_adv, adv, ret;
a59a4d19 1368
25149ef9
FF
1369 if (!phydev->drv)
1370 return -EIO;
1371
83ea067f
RK
1372 /* Get Supported EEE */
1373 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1374 if (cap < 0)
1375 return cap;
d853d145 1376
f75abeb8
RK
1377 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1378 if (old_adv < 0)
1379 return old_adv;
1380
83ea067f 1381 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
a59a4d19 1382
83ea067f
RK
1383 /* Mask prohibited EEE modes */
1384 adv &= ~phydev->eee_broken_modes;
1385
f75abeb8
RK
1386 if (old_adv != adv) {
1387 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1388 if (ret < 0)
1389 return ret;
1390
1391 /* Restart autonegotiation so the new modes get sent to the
1392 * link partner.
1393 */
1394 ret = genphy_restart_aneg(phydev);
1395 if (ret < 0)
1396 return ret;
1397 }
1398
1399 return 0;
a59a4d19
GC
1400}
1401EXPORT_SYMBOL(phy_ethtool_set_eee);
42e836eb
MS
1402
1403int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1404{
25149ef9 1405 if (phydev->drv && phydev->drv->set_wol)
42e836eb
MS
1406 return phydev->drv->set_wol(phydev, wol);
1407
1408 return -EOPNOTSUPP;
1409}
1410EXPORT_SYMBOL(phy_ethtool_set_wol);
1411
1412void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1413{
25149ef9 1414 if (phydev->drv && phydev->drv->get_wol)
42e836eb
MS
1415 phydev->drv->get_wol(phydev, wol);
1416}
1417EXPORT_SYMBOL(phy_ethtool_get_wol);
9d9a77ce
PR
1418
1419int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1420 struct ethtool_link_ksettings *cmd)
1421{
1422 struct phy_device *phydev = ndev->phydev;
1423
1424 if (!phydev)
1425 return -ENODEV;
1426
1427 return phy_ethtool_ksettings_get(phydev, cmd);
1428}
1429EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1430
1431int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1432 const struct ethtool_link_ksettings *cmd)
1433{
1434 struct phy_device *phydev = ndev->phydev;
1435
1436 if (!phydev)
1437 return -ENODEV;
1438
1439 return phy_ethtool_ksettings_set(phydev, cmd);
1440}
1441EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
e86a8987
FF
1442
1443int phy_ethtool_nway_reset(struct net_device *ndev)
1444{
1445 struct phy_device *phydev = ndev->phydev;
1446
1447 if (!phydev)
1448 return -ENODEV;
1449
25149ef9
FF
1450 if (!phydev->drv)
1451 return -EIO;
1452
e86a8987
FF
1453 return genphy_restart_aneg(phydev);
1454}
1455EXPORT_SYMBOL(phy_ethtool_nway_reset);