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