net: phy: allow settings table to support more than 32 link modes
[platform/kernel/linux-rpi.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/workqueue.h>
34 #include <linux/mdio.h>
35 #include <linux/io.h>
36 #include <linux/uaccess.h>
37 #include <linux/atomic.h>
38
39 #include <asm/irq.h>
40
41 static 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";
52         case SPEED_5000:
53                 return "5Gbps";
54         case SPEED_10000:
55                 return "10Gbps";
56         case SPEED_14000:
57                 return "14Gbps";
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";
70         case SPEED_UNKNOWN:
71                 return "Unknown";
72         default:
73                 return "Unsupported (update phy.c)";
74         }
75 }
76
77 #define PHY_STATE_STR(_state)                   \
78         case PHY_##_state:                      \
79                 return __stringify(_state);     \
80
81 static 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
102 /**
103  * phy_print_status - Convenience function to print out the current phy status
104  * @phydev: the phy_device struct
105  */
106 void phy_print_status(struct phy_device *phydev)
107 {
108         if (phydev->link) {
109                 netdev_info(phydev->attached_dev,
110                         "Link is Up - %s/%s - flow control %s\n",
111                         phy_speed_to_str(phydev->speed),
112                         DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
113                         phydev->pause ? "rx/tx" : "off");
114         } else  {
115                 netdev_info(phydev->attached_dev, "Link is Down\n");
116         }
117 }
118 EXPORT_SYMBOL(phy_print_status);
119
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  *
127  * Returns 0 on success or < 0 on error.
128  */
129 static int phy_clear_interrupt(struct phy_device *phydev)
130 {
131         if (phydev->drv->ack_interrupt)
132                 return phydev->drv->ack_interrupt(phydev);
133
134         return 0;
135 }
136
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  *
142  * Returns 0 on success or < 0 on error.
143  */
144 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
145 {
146         phydev->interrupts = interrupts;
147         if (phydev->drv->config_intr)
148                 return phydev->drv->config_intr(phydev);
149
150         return 0;
151 }
152
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  */
160 int 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 }
171 EXPORT_SYMBOL_GPL(phy_restart_aneg);
172
173 /**
174  * phy_aneg_done - return auto-negotiation status
175  * @phydev: target phy_device struct
176  *
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.
180  */
181 int phy_aneg_done(struct phy_device *phydev)
182 {
183         if (phydev->drv && phydev->drv->aneg_done)
184                 return phydev->drv->aneg_done(phydev);
185
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
192         return genphy_aneg_done(phydev);
193 }
194 EXPORT_SYMBOL(phy_aneg_done);
195
196 /* A structure for mapping a particular speed and duplex
197  * combination to a particular SUPPORTED and ADVERTISED value
198  */
199 struct phy_setting {
200         int speed;
201         int duplex;
202         int bit;
203 };
204
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. */
208 static const struct phy_setting settings[] = {
209         {
210                 .speed = SPEED_10000,
211                 .duplex = DUPLEX_FULL,
212                 .bit = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
213         },
214         {
215                 .speed = SPEED_10000,
216                 .duplex = DUPLEX_FULL,
217                 .bit = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
218         },
219         {
220                 .speed = SPEED_10000,
221                 .duplex = DUPLEX_FULL,
222                 .bit = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
223         },
224         {
225                 .speed = SPEED_2500,
226                 .duplex = DUPLEX_FULL,
227                 .bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
228         },
229         {
230                 .speed = SPEED_1000,
231                 .duplex = DUPLEX_FULL,
232                 .bit = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
233         },
234         {
235                 .speed = SPEED_1000,
236                 .duplex = DUPLEX_FULL,
237                 .bit = ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
238         },
239         {
240                 .speed = SPEED_1000,
241                 .duplex = DUPLEX_HALF,
242                 .bit = ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
243         },
244         {
245                 .speed = SPEED_100,
246                 .duplex = DUPLEX_FULL,
247                 .bit = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
248         },
249         {
250                 .speed = SPEED_100,
251                 .duplex = DUPLEX_HALF,
252                 .bit = ETHTOOL_LINK_MODE_100baseT_Half_BIT,
253         },
254         {
255                 .speed = SPEED_10,
256                 .duplex = DUPLEX_FULL,
257                 .bit = ETHTOOL_LINK_MODE_10baseT_Full_BIT,
258         },
259         {
260                 .speed = SPEED_10,
261                 .duplex = DUPLEX_HALF,
262                 .bit = ETHTOOL_LINK_MODE_10baseT_Half_BIT,
263         },
264 };
265
266 /**
267  * phy_lookup_setting - lookup a PHY setting
268  * @speed: speed to match
269  * @duplex: duplex to match
270  * @mask: allowed link modes
271  * @maxbit: bit size of link modes
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.
276  *
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.
283  */
284 static const struct phy_setting *
285 phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
286                    size_t maxbit, bool exact)
287 {
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++) {
292                 if (p->bit < maxbit && test_bit(p->bit, mask)) {
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         }
308
309         if (!match && !exact)
310                 match = last;
311
312         return match;
313 }
314
315 /**
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
320  *
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.
327  */
328 static const struct phy_setting *
329 phy_find_valid(int speed, int duplex, u32 supported)
330 {
331         unsigned long mask = supported;
332
333         return phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, false);
334 }
335
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  */
346 unsigned int phy_supported_speeds(struct phy_device *phy,
347                                   unsigned int *speeds,
348                                   unsigned int size)
349 {
350         unsigned long supported = phy->supported;
351         unsigned int count = 0;
352         unsigned int idx = 0;
353
354         for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++)
355                 /* Assumes settings are grouped by speed */
356                 if (settings[idx].bit < BITS_PER_LONG &&
357                     !test_bit(settings[idx].bit, &supported) &&
358                     (count == 0 || speeds[count - 1] != settings[idx].speed))
359                         speeds[count++] = settings[idx].speed;
360
361         return count;
362 }
363
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  */
373 static inline bool phy_check_valid(int speed, int duplex, u32 features)
374 {
375         unsigned long mask = features;
376
377         return !!phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, true);
378 }
379
380 /**
381  * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
382  * @phydev: the target phy_device struct
383  *
384  * Description: Make sure the PHY is set to supported speeds and
385  *   duplexes.  Drop down by one in this order:  1000/FULL,
386  *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
387  */
388 static void phy_sanitize_settings(struct phy_device *phydev)
389 {
390         const struct phy_setting *setting;
391         u32 features = phydev->supported;
392
393         /* Sanitize settings based on PHY capabilities */
394         if ((features & SUPPORTED_Autoneg) == 0)
395                 phydev->autoneg = AUTONEG_DISABLE;
396
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         }
406 }
407
408 /**
409  * phy_ethtool_sset - generic ethtool sset function, handles all the details
410  * @phydev: target phy_device struct
411  * @cmd: ethtool_cmd
412  *
413  * A few notes about parameter checking:
414  *
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
419  *   care if ethtool tries to give us bad values.
420  */
421 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
422 {
423         u32 speed = ethtool_cmd_speed(cmd);
424
425         if (cmd->phy_address != phydev->mdio.addr)
426                 return -EINVAL;
427
428         /* We make sure that we don't pass unsupported values in to the PHY */
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
438         if (cmd->autoneg == AUTONEG_DISABLE &&
439             ((speed != SPEED_1000 &&
440               speed != SPEED_100 &&
441               speed != SPEED_10) ||
442              (cmd->duplex != DUPLEX_HALF &&
443               cmd->duplex != DUPLEX_FULL)))
444                 return -EINVAL;
445
446         phydev->autoneg = cmd->autoneg;
447
448         phydev->speed = speed;
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
459         phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
460
461         /* Restart the PHY */
462         phy_start_aneg(phydev);
463
464         return 0;
465 }
466 EXPORT_SYMBOL(phy_ethtool_sset);
467
468 int 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
513         phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
514
515         /* Restart the PHY */
516         phy_start_aneg(phydev);
517
518         return 0;
519 }
520 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
521
522 void phy_ethtool_ksettings_get(struct phy_device *phydev,
523                                struct ethtool_link_ksettings *cmd)
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;
543         cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
544         cmd->base.eth_tp_mdix = phydev->mdix;
545 }
546 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
547
548 /**
549  * phy_mii_ioctl - generic PHY MII ioctl interface
550  * @phydev: the phy_device struct
551  * @ifr: &struct ifreq for socket ioctl's
552  * @cmd: ioctl cmd to execute
553  *
554  * Note that this function is currently incompatible with the
555  * PHYCONTROL layer.  It changes registers without regard to
556  * current state.  Use at own risk.
557  */
558 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
559 {
560         struct mii_ioctl_data *mii_data = if_mii(ifr);
561         u16 val = mii_data->val_in;
562         bool change_autoneg = false;
563
564         switch (cmd) {
565         case SIOCGMIIPHY:
566                 mii_data->phy_id = phydev->mdio.addr;
567                 /* fall through */
568
569         case SIOCGMIIREG:
570                 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
571                                                  mii_data->phy_id,
572                                                  mii_data->reg_num);
573                 return 0;
574
575         case SIOCSMIIREG:
576                 if (mii_data->phy_id == phydev->mdio.addr) {
577                         switch (mii_data->reg_num) {
578                         case MII_BMCR:
579                                 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
580                                         if (phydev->autoneg == AUTONEG_ENABLE)
581                                                 change_autoneg = true;
582                                         phydev->autoneg = AUTONEG_DISABLE;
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;
596                                         phydev->autoneg = AUTONEG_ENABLE;
597                                 }
598                                 break;
599                         case MII_ADVERTISE:
600                                 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
601                                 change_autoneg = true;
602                                 break;
603                         default:
604                                 /* do nothing */
605                                 break;
606                         }
607                 }
608
609                 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
610                               mii_data->reg_num, val);
611
612                 if (mii_data->phy_id == phydev->mdio.addr &&
613                     mii_data->reg_num == MII_BMCR &&
614                     val & BMCR_RESET)
615                         return phy_init_hw(phydev);
616
617                 if (change_autoneg)
618                         return phy_start_aneg(phydev);
619
620                 return 0;
621
622         case SIOCSHWTSTAMP:
623                 if (phydev->drv && phydev->drv->hwtstamp)
624                         return phydev->drv->hwtstamp(phydev, ifr);
625                 /* fall through */
626
627         default:
628                 return -EOPNOTSUPP;
629         }
630 }
631 EXPORT_SYMBOL(phy_mii_ioctl);
632
633 /**
634  * phy_start_aneg_priv - start auto-negotiation for this PHY device
635  * @phydev: the phy_device struct
636  * @sync: indicate whether we should wait for the workqueue cancelation
637  *
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.
642  */
643 static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
644 {
645         bool trigger = 0;
646         int err;
647
648         if (!phydev->drv)
649                 return -EIO;
650
651         mutex_lock(&phydev->lock);
652
653         if (AUTONEG_DISABLE == phydev->autoneg)
654                 phy_sanitize_settings(phydev);
655
656         /* Invalidate LP advertising flags */
657         phydev->lp_advertising = 0;
658
659         err = phydev->drv->config_aneg(phydev);
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
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
685 out_unlock:
686         mutex_unlock(&phydev->lock);
687
688         if (trigger)
689                 phy_trigger_machine(phydev, sync);
690
691         return err;
692 }
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  */
703 int phy_start_aneg(struct phy_device *phydev)
704 {
705         return phy_start_aneg_priv(phydev, true);
706 }
707 EXPORT_SYMBOL(phy_start_aneg);
708
709 /**
710  * phy_start_machine - start PHY state machine tracking
711  * @phydev: the phy_device struct
712  *
713  * Description: The PHY infrastructure can run a state machine
714  *   which tracks whether the PHY is starting up, negotiating,
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,
717  *   do not call this function.
718  */
719 void phy_start_machine(struct phy_device *phydev)
720 {
721         queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
722 }
723
724 /**
725  * phy_trigger_machine - trigger the state machine to run
726  *
727  * @phydev: the phy_device struct
728  * @sync: indicate whether we should wait for the workqueue cancelation
729  *
730  * Description: There has been a change in state which requires that the
731  *   state machine runs.
732  */
733
734 void phy_trigger_machine(struct phy_device *phydev, bool sync)
735 {
736         if (sync)
737                 cancel_delayed_work_sync(&phydev->state_queue);
738         else
739                 cancel_delayed_work(&phydev->state_queue);
740         queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
741 }
742
743 /**
744  * phy_stop_machine - stop the PHY state machine tracking
745  * @phydev: target phy_device struct
746  *
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.
750  */
751 void phy_stop_machine(struct phy_device *phydev)
752 {
753         cancel_delayed_work_sync(&phydev->state_queue);
754
755         mutex_lock(&phydev->lock);
756         if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
757                 phydev->state = PHY_UP;
758         mutex_unlock(&phydev->lock);
759
760         /* Now we can run the state machine synchronously */
761         phy_state_machine(&phydev->state_queue.work);
762 }
763
764 /**
765  * phy_error - enter HALTED state for this PHY device
766  * @phydev: target phy_device struct
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  */
773 static void phy_error(struct phy_device *phydev)
774 {
775         mutex_lock(&phydev->lock);
776         phydev->state = PHY_HALTED;
777         mutex_unlock(&phydev->lock);
778
779         phy_trigger_machine(phydev, false);
780 }
781
782 /**
783  * phy_interrupt - PHY interrupt handler
784  * @irq: interrupt line
785  * @phy_dat: phy_device pointer
786  *
787  * Description: When a PHY interrupt occurs, the handler disables
788  * interrupts, and uses phy_change to handle the interrupt.
789  */
790 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
791 {
792         struct phy_device *phydev = phy_dat;
793
794         if (PHY_HALTED == phydev->state)
795                 return IRQ_NONE;                /* It can't be ours.  */
796
797         disable_irq_nosync(irq);
798         atomic_inc(&phydev->irq_disable);
799
800         phy_change(phydev);
801
802         return IRQ_HANDLED;
803 }
804
805 /**
806  * phy_enable_interrupts - Enable the interrupts from the PHY side
807  * @phydev: target phy_device struct
808  */
809 static int phy_enable_interrupts(struct phy_device *phydev)
810 {
811         int err = phy_clear_interrupt(phydev);
812
813         if (err < 0)
814                 return err;
815
816         return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
817 }
818
819 /**
820  * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
821  * @phydev: target phy_device struct
822  */
823 static int phy_disable_interrupts(struct phy_device *phydev)
824 {
825         int err;
826
827         /* Disable PHY interrupts */
828         err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
829         if (err)
830                 goto phy_err;
831
832         /* Clear the interrupt */
833         err = phy_clear_interrupt(phydev);
834         if (err)
835                 goto phy_err;
836
837         return 0;
838
839 phy_err:
840         phy_error(phydev);
841
842         return err;
843 }
844
845 /**
846  * phy_start_interrupts - request and enable interrupts for a PHY device
847  * @phydev: target phy_device struct
848  *
849  * Description: Request the interrupt for the given PHY.
850  *   If this fails, then we set irq to PHY_POLL.
851  *   Otherwise, we enable the interrupts in the PHY.
852  *   This should only be called with a valid IRQ number.
853  *   Returns 0 on success or < 0 on error.
854  */
855 int phy_start_interrupts(struct phy_device *phydev)
856 {
857         atomic_set(&phydev->irq_disable, 0);
858         if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
859                                  IRQF_ONESHOT | IRQF_SHARED,
860                                  phydev_name(phydev), phydev) < 0) {
861                 pr_warn("%s: Can't get IRQ %d (PHY)\n",
862                         phydev->mdio.bus->name, phydev->irq);
863                 phydev->irq = PHY_POLL;
864                 return 0;
865         }
866
867         return phy_enable_interrupts(phydev);
868 }
869 EXPORT_SYMBOL(phy_start_interrupts);
870
871 /**
872  * phy_stop_interrupts - disable interrupts from a PHY device
873  * @phydev: target phy_device struct
874  */
875 int phy_stop_interrupts(struct phy_device *phydev)
876 {
877         int err = phy_disable_interrupts(phydev);
878
879         if (err)
880                 phy_error(phydev);
881
882         free_irq(phydev->irq, phydev);
883
884         /* If work indeed has been cancelled, disable_irq() will have
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);
890
891         return err;
892 }
893 EXPORT_SYMBOL(phy_stop_interrupts);
894
895 /**
896  * phy_change - Called by the phy_interrupt to handle PHY changes
897  * @phydev: phy_device struct that interrupted
898  */
899 void phy_change(struct phy_device *phydev)
900 {
901         if (phy_interrupt_is_valid(phydev)) {
902                 if (phydev->drv->did_interrupt &&
903                     !phydev->drv->did_interrupt(phydev))
904                         goto ignore;
905
906                 if (phy_disable_interrupts(phydev))
907                         goto phy_err;
908         }
909
910         mutex_lock(&phydev->lock);
911         if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
912                 phydev->state = PHY_CHANGELINK;
913         mutex_unlock(&phydev->lock);
914
915         if (phy_interrupt_is_valid(phydev)) {
916                 atomic_dec(&phydev->irq_disable);
917                 enable_irq(phydev->irq);
918
919                 /* Reenable interrupts */
920                 if (PHY_HALTED != phydev->state &&
921                     phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
922                         goto irq_enable_err;
923         }
924
925         /* reschedule state queue work to run as soon as possible */
926         phy_trigger_machine(phydev, true);
927         return;
928
929 ignore:
930         atomic_dec(&phydev->irq_disable);
931         enable_irq(phydev->irq);
932         return;
933
934 irq_enable_err:
935         disable_irq(phydev->irq);
936         atomic_inc(&phydev->irq_disable);
937 phy_err:
938         phy_error(phydev);
939 }
940
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  */
945 void 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
953 /**
954  * phy_stop - Bring down the PHY link, and stop checking the status
955  * @phydev: target phy_device struct
956  */
957 void phy_stop(struct phy_device *phydev)
958 {
959         mutex_lock(&phydev->lock);
960
961         if (PHY_HALTED == phydev->state)
962                 goto out_unlock;
963
964         if (phy_interrupt_is_valid(phydev)) {
965                 /* Disable PHY Interrupts */
966                 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
967
968                 /* Clear any pending interrupts */
969                 phy_clear_interrupt(phydev);
970         }
971
972         phydev->state = PHY_HALTED;
973
974 out_unlock:
975         mutex_unlock(&phydev->lock);
976
977         /* Cannot call flush_scheduled_work() here as desired because
978          * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
979          * will not reenable interrupts.
980          */
981 }
982 EXPORT_SYMBOL(phy_stop);
983
984 /**
985  * phy_start - start or restart a PHY device
986  * @phydev: target phy_device struct
987  *
988  * Description: Indicates the attached device's readiness to
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  */
994 void phy_start(struct phy_device *phydev)
995 {
996         bool do_resume = false;
997         int err = 0;
998
999         mutex_lock(&phydev->lock);
1000
1001         switch (phydev->state) {
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:
1009                 /* make sure interrupts are re-enabled for the PHY */
1010                 if (phydev->irq != PHY_POLL) {
1011                         err = phy_enable_interrupts(phydev);
1012                         if (err < 0)
1013                                 break;
1014                 }
1015
1016                 phydev->state = PHY_RESUMING;
1017                 do_resume = true;
1018                 break;
1019         default:
1020                 break;
1021         }
1022         mutex_unlock(&phydev->lock);
1023
1024         /* if phy was suspended, bring the physical link up again */
1025         if (do_resume)
1026                 phy_resume(phydev);
1027
1028         phy_trigger_machine(phydev, true);
1029 }
1030 EXPORT_SYMBOL(phy_start);
1031
1032 static void phy_adjust_link(struct phy_device *phydev)
1033 {
1034         phydev->adjust_link(phydev->attached_dev);
1035         phy_led_trigger_change_speed(phydev);
1036 }
1037
1038 /**
1039  * phy_state_machine - Handle the state machine
1040  * @work: work_struct that describes the work to be done
1041  */
1042 void phy_state_machine(struct work_struct *work)
1043 {
1044         struct delayed_work *dwork = to_delayed_work(work);
1045         struct phy_device *phydev =
1046                         container_of(dwork, struct phy_device, state_queue);
1047         bool needs_aneg = false, do_suspend = false;
1048         enum phy_state old_state;
1049         int err = 0;
1050         int old_link;
1051
1052         mutex_lock(&phydev->lock);
1053
1054         old_state = phydev->state;
1055
1056         if (phydev->drv && phydev->drv->link_change_notify)
1057                 phydev->drv->link_change_notify(phydev);
1058
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:
1066                 needs_aneg = true;
1067
1068                 phydev->link_timeout = PHY_AN_TIMEOUT;
1069
1070                 break;
1071         case PHY_AN:
1072                 err = phy_read_status(phydev);
1073                 if (err < 0)
1074                         break;
1075
1076                 /* If the link is down, give up on negotiation for now */
1077                 if (!phydev->link) {
1078                         phydev->state = PHY_NOLINK;
1079                         netif_carrier_off(phydev->attached_dev);
1080                         phy_adjust_link(phydev);
1081                         break;
1082                 }
1083
1084                 /* Check if negotiation is done.  Break if there's an error */
1085                 err = phy_aneg_done(phydev);
1086                 if (err < 0)
1087                         break;
1088
1089                 /* If AN is done, we're running */
1090                 if (err > 0) {
1091                         phydev->state = PHY_RUNNING;
1092                         netif_carrier_on(phydev->attached_dev);
1093                         phy_adjust_link(phydev);
1094
1095                 } else if (0 == phydev->link_timeout--)
1096                         needs_aneg = true;
1097                 break;
1098         case PHY_NOLINK:
1099                 if (phy_interrupt_is_valid(phydev))
1100                         break;
1101
1102                 err = phy_read_status(phydev);
1103                 if (err)
1104                         break;
1105
1106                 if (phydev->link) {
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                         }
1118                         phydev->state = PHY_RUNNING;
1119                         netif_carrier_on(phydev->attached_dev);
1120                         phy_adjust_link(phydev);
1121                 }
1122                 break;
1123         case PHY_FORCING:
1124                 err = genphy_update_link(phydev);
1125                 if (err)
1126                         break;
1127
1128                 if (phydev->link) {
1129                         phydev->state = PHY_RUNNING;
1130                         netif_carrier_on(phydev->attached_dev);
1131                 } else {
1132                         if (0 == phydev->link_timeout--)
1133                                 needs_aneg = true;
1134                 }
1135
1136                 phy_adjust_link(phydev);
1137                 break;
1138         case PHY_RUNNING:
1139                 /* Only register a CHANGE if we are polling and link changed
1140                  * since latest checking.
1141                  */
1142                 if (phydev->irq == PHY_POLL) {
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                 }
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                 }
1160                 break;
1161         case PHY_CHANGELINK:
1162                 err = phy_read_status(phydev);
1163                 if (err)
1164                         break;
1165
1166                 if (phydev->link) {
1167                         phydev->state = PHY_RUNNING;
1168                         netif_carrier_on(phydev->attached_dev);
1169                 } else {
1170                         phydev->state = PHY_NOLINK;
1171                         netif_carrier_off(phydev->attached_dev);
1172                 }
1173
1174                 phy_adjust_link(phydev);
1175
1176                 if (phy_interrupt_is_valid(phydev))
1177                         err = phy_config_interrupt(phydev,
1178                                                    PHY_INTERRUPT_ENABLED);
1179                 break;
1180         case PHY_HALTED:
1181                 if (phydev->link) {
1182                         phydev->link = 0;
1183                         netif_carrier_off(phydev->attached_dev);
1184                         phy_adjust_link(phydev);
1185                         do_suspend = true;
1186                 }
1187                 break;
1188         case PHY_RESUMING:
1189                 if (AUTONEG_ENABLE == phydev->autoneg) {
1190                         err = phy_aneg_done(phydev);
1191                         if (err < 0)
1192                                 break;
1193
1194                         /* err > 0 if AN is done.
1195                          * Otherwise, it's 0, and we're  still waiting for AN
1196                          */
1197                         if (err > 0) {
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);
1205                                 } else  {
1206                                         phydev->state = PHY_NOLINK;
1207                                 }
1208                                 phy_adjust_link(phydev);
1209                         } else {
1210                                 phydev->state = PHY_AN;
1211                                 phydev->link_timeout = PHY_AN_TIMEOUT;
1212                         }
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);
1221                         } else  {
1222                                 phydev->state = PHY_NOLINK;
1223                         }
1224                         phy_adjust_link(phydev);
1225                 }
1226                 break;
1227         }
1228
1229         mutex_unlock(&phydev->lock);
1230
1231         if (needs_aneg)
1232                 err = phy_start_aneg_priv(phydev, false);
1233         else if (do_suspend)
1234                 phy_suspend(phydev);
1235
1236         if (err < 0)
1237                 phy_error(phydev);
1238
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));
1243
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);
1251 }
1252
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  */
1262 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1263 {
1264         phydev->link = new_link;
1265
1266         /* Trigger a state machine change */
1267         queue_work(system_power_efficient_wq, &phydev->phy_queue);
1268 }
1269 EXPORT_SYMBOL(phy_mac_interrupt);
1270
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  */
1281 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1282 {
1283         if (!phydev->drv)
1284                 return -EIO;
1285
1286         /* According to 802.3az,the EEE is supported only in full duplex-mode.
1287          */
1288         if (phydev->duplex == DUPLEX_FULL) {
1289                 int eee_lp, eee_cap, eee_adv;
1290                 u32 lp, cap, adv;
1291                 int status;
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 */
1299                 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1300                 if (eee_cap <= 0)
1301                         goto eee_exit_err;
1302
1303                 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1304                 if (!cap)
1305                         goto eee_exit_err;
1306
1307                 /* Check which link settings negotiated and verify it in
1308                  * the EEE advertising registers.
1309                  */
1310                 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1311                 if (eee_lp <= 0)
1312                         goto eee_exit_err;
1313
1314                 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1315                 if (eee_adv <= 0)
1316                         goto eee_exit_err;
1317
1318                 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1319                 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1320                 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1321                         goto eee_exit_err;
1322
1323                 if (clk_stop_enable) {
1324                         /* Configure the PHY to stop receiving xMII
1325                          * clock while it is signaling LPI.
1326                          */
1327                         int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1328                         if (val < 0)
1329                                 return val;
1330
1331                         val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1332                         phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1333                 }
1334
1335                 return 0; /* EEE supported */
1336         }
1337 eee_exit_err:
1338         return -EPROTONOSUPPORT;
1339 }
1340 EXPORT_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  */
1349 int phy_get_eee_err(struct phy_device *phydev)
1350 {
1351         if (!phydev->drv)
1352                 return -EIO;
1353
1354         return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1355 }
1356 EXPORT_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  */
1366 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1367 {
1368         int val;
1369
1370         if (!phydev->drv)
1371                 return -EIO;
1372
1373         /* Get Supported EEE */
1374         val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1375         if (val < 0)
1376                 return val;
1377         data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1378
1379         /* Get advertisement EEE */
1380         val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1381         if (val < 0)
1382                 return val;
1383         data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1384
1385         /* Get LP advertisement EEE */
1386         val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1387         if (val < 0)
1388                 return val;
1389         data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1390
1391         return 0;
1392 }
1393 EXPORT_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  */
1402 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1403 {
1404         int cap, old_adv, adv, ret;
1405
1406         if (!phydev->drv)
1407                 return -EIO;
1408
1409         /* Get Supported EEE */
1410         cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1411         if (cap < 0)
1412                 return cap;
1413
1414         old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1415         if (old_adv < 0)
1416                 return old_adv;
1417
1418         adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1419
1420         /* Mask prohibited EEE modes */
1421         adv &= ~phydev->eee_broken_modes;
1422
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                  */
1431                 ret = phy_restart_aneg(phydev);
1432                 if (ret < 0)
1433                         return ret;
1434         }
1435
1436         return 0;
1437 }
1438 EXPORT_SYMBOL(phy_ethtool_set_eee);
1439
1440 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1441 {
1442         if (phydev->drv && phydev->drv->set_wol)
1443                 return phydev->drv->set_wol(phydev, wol);
1444
1445         return -EOPNOTSUPP;
1446 }
1447 EXPORT_SYMBOL(phy_ethtool_set_wol);
1448
1449 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1450 {
1451         if (phydev->drv && phydev->drv->get_wol)
1452                 phydev->drv->get_wol(phydev, wol);
1453 }
1454 EXPORT_SYMBOL(phy_ethtool_get_wol);
1455
1456 int 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
1464         phy_ethtool_ksettings_get(phydev, cmd);
1465
1466         return 0;
1467 }
1468 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1469
1470 int 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 }
1480 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1481
1482 int phy_ethtool_nway_reset(struct net_device *ndev)
1483 {
1484         struct phy_device *phydev = ndev->phydev;
1485
1486         if (!phydev)
1487                 return -ENODEV;
1488
1489         if (!phydev->drv)
1490                 return -EIO;
1491
1492         return phy_restart_aneg(phydev);
1493 }
1494 EXPORT_SYMBOL(phy_ethtool_nway_reset);