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