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