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