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