GNU Linux-libre 5.15.54-gnu
[releases.git] / drivers / net / phy / phy.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Framework for configuring and reading PHY devices
3  * Based on code in sungem_phy.c and gianfar_phy.c
4  *
5  * Author: Andy Fleming
6  *
7  * Copyright (c) 2004 Freescale Semiconductor, Inc.
8  * Copyright (c) 2006, 2007  Maciej W. Rozycki
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/unistd.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/netdevice.h>
18 #include <linux/netlink.h>
19 #include <linux/etherdevice.h>
20 #include <linux/skbuff.h>
21 #include <linux/mm.h>
22 #include <linux/module.h>
23 #include <linux/mii.h>
24 #include <linux/ethtool.h>
25 #include <linux/ethtool_netlink.h>
26 #include <linux/phy.h>
27 #include <linux/phy_led_triggers.h>
28 #include <linux/sfp.h>
29 #include <linux/workqueue.h>
30 #include <linux/mdio.h>
31 #include <linux/io.h>
32 #include <linux/uaccess.h>
33 #include <linux/atomic.h>
34 #include <linux/suspend.h>
35 #include <net/netlink.h>
36 #include <net/genetlink.h>
37 #include <net/sock.h>
38
39 #define PHY_STATE_TIME  HZ
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(READY)
50         PHY_STATE_STR(UP)
51         PHY_STATE_STR(RUNNING)
52         PHY_STATE_STR(NOLINK)
53         PHY_STATE_STR(CABLETEST)
54         PHY_STATE_STR(HALTED)
55         }
56
57         return NULL;
58 }
59
60 static void phy_link_up(struct phy_device *phydev)
61 {
62         phydev->phy_link_change(phydev, true);
63         phy_led_trigger_change_speed(phydev);
64 }
65
66 static void phy_link_down(struct phy_device *phydev)
67 {
68         phydev->phy_link_change(phydev, false);
69         phy_led_trigger_change_speed(phydev);
70 }
71
72 static const char *phy_pause_str(struct phy_device *phydev)
73 {
74         bool local_pause, local_asym_pause;
75
76         if (phydev->autoneg == AUTONEG_DISABLE)
77                 goto no_pause;
78
79         local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
80                                         phydev->advertising);
81         local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
82                                              phydev->advertising);
83
84         if (local_pause && phydev->pause)
85                 return "rx/tx";
86
87         if (local_asym_pause && phydev->asym_pause) {
88                 if (local_pause)
89                         return "rx";
90                 if (phydev->pause)
91                         return "tx";
92         }
93
94 no_pause:
95         return "off";
96 }
97
98 /**
99  * phy_print_status - Convenience function to print out the current phy status
100  * @phydev: the phy_device struct
101  */
102 void phy_print_status(struct phy_device *phydev)
103 {
104         if (phydev->link) {
105                 netdev_info(phydev->attached_dev,
106                         "Link is Up - %s/%s %s- flow control %s\n",
107                         phy_speed_to_str(phydev->speed),
108                         phy_duplex_to_str(phydev->duplex),
109                         phydev->downshifted_rate ? "(downshifted) " : "",
110                         phy_pause_str(phydev));
111         } else  {
112                 netdev_info(phydev->attached_dev, "Link is Down\n");
113         }
114 }
115 EXPORT_SYMBOL(phy_print_status);
116
117 /**
118  * phy_config_interrupt - configure the PHY device for the requested interrupts
119  * @phydev: the phy_device struct
120  * @interrupts: interrupt flags to configure for this @phydev
121  *
122  * Returns 0 on success or < 0 on error.
123  */
124 static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
125 {
126         phydev->interrupts = interrupts ? 1 : 0;
127         if (phydev->drv->config_intr)
128                 return phydev->drv->config_intr(phydev);
129
130         return 0;
131 }
132
133 /**
134  * phy_restart_aneg - restart auto-negotiation
135  * @phydev: target phy_device struct
136  *
137  * Restart the autonegotiation on @phydev.  Returns >= 0 on success or
138  * negative errno on error.
139  */
140 int phy_restart_aneg(struct phy_device *phydev)
141 {
142         int ret;
143
144         if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
145                 ret = genphy_c45_restart_aneg(phydev);
146         else
147                 ret = genphy_restart_aneg(phydev);
148
149         return ret;
150 }
151 EXPORT_SYMBOL_GPL(phy_restart_aneg);
152
153 /**
154  * phy_aneg_done - return auto-negotiation status
155  * @phydev: target phy_device struct
156  *
157  * Description: Return the auto-negotiation status from this @phydev
158  * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
159  * is still pending.
160  */
161 int phy_aneg_done(struct phy_device *phydev)
162 {
163         if (phydev->drv && phydev->drv->aneg_done)
164                 return phydev->drv->aneg_done(phydev);
165         else if (phydev->is_c45)
166                 return genphy_c45_aneg_done(phydev);
167         else
168                 return genphy_aneg_done(phydev);
169 }
170 EXPORT_SYMBOL(phy_aneg_done);
171
172 /**
173  * phy_find_valid - find a PHY setting that matches the requested parameters
174  * @speed: desired speed
175  * @duplex: desired duplex
176  * @supported: mask of supported link modes
177  *
178  * Locate a supported phy setting that is, in priority order:
179  * - an exact match for the specified speed and duplex mode
180  * - a match for the specified speed, or slower speed
181  * - the slowest supported speed
182  * Returns the matched phy_setting entry, or %NULL if no supported phy
183  * settings were found.
184  */
185 static const struct phy_setting *
186 phy_find_valid(int speed, int duplex, unsigned long *supported)
187 {
188         return phy_lookup_setting(speed, duplex, supported, false);
189 }
190
191 /**
192  * phy_supported_speeds - return all speeds currently supported by a phy device
193  * @phy: The phy device to return supported speeds of.
194  * @speeds: buffer to store supported speeds in.
195  * @size:   size of speeds buffer.
196  *
197  * Description: Returns the number of supported speeds, and fills the speeds
198  * buffer with the supported speeds. If speeds buffer is too small to contain
199  * all currently supported speeds, will return as many speeds as can fit.
200  */
201 unsigned int phy_supported_speeds(struct phy_device *phy,
202                                   unsigned int *speeds,
203                                   unsigned int size)
204 {
205         return phy_speeds(speeds, size, phy->supported);
206 }
207
208 /**
209  * phy_check_valid - check if there is a valid PHY setting which matches
210  *                   speed, duplex, and feature mask
211  * @speed: speed to match
212  * @duplex: duplex to match
213  * @features: A mask of the valid settings
214  *
215  * Description: Returns true if there is a valid setting, false otherwise.
216  */
217 static inline bool phy_check_valid(int speed, int duplex,
218                                    unsigned long *features)
219 {
220         return !!phy_lookup_setting(speed, duplex, features, true);
221 }
222
223 /**
224  * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
225  * @phydev: the target phy_device struct
226  *
227  * Description: Make sure the PHY is set to supported speeds and
228  *   duplexes.  Drop down by one in this order:  1000/FULL,
229  *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
230  */
231 static void phy_sanitize_settings(struct phy_device *phydev)
232 {
233         const struct phy_setting *setting;
234
235         setting = phy_find_valid(phydev->speed, phydev->duplex,
236                                  phydev->supported);
237         if (setting) {
238                 phydev->speed = setting->speed;
239                 phydev->duplex = setting->duplex;
240         } else {
241                 /* We failed to find anything (no supported speeds?) */
242                 phydev->speed = SPEED_UNKNOWN;
243                 phydev->duplex = DUPLEX_UNKNOWN;
244         }
245 }
246
247 void phy_ethtool_ksettings_get(struct phy_device *phydev,
248                                struct ethtool_link_ksettings *cmd)
249 {
250         mutex_lock(&phydev->lock);
251         linkmode_copy(cmd->link_modes.supported, phydev->supported);
252         linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
253         linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
254
255         cmd->base.speed = phydev->speed;
256         cmd->base.duplex = phydev->duplex;
257         cmd->base.master_slave_cfg = phydev->master_slave_get;
258         cmd->base.master_slave_state = phydev->master_slave_state;
259         if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
260                 cmd->base.port = PORT_BNC;
261         else
262                 cmd->base.port = phydev->port;
263         cmd->base.transceiver = phy_is_internal(phydev) ?
264                                 XCVR_INTERNAL : XCVR_EXTERNAL;
265         cmd->base.phy_address = phydev->mdio.addr;
266         cmd->base.autoneg = phydev->autoneg;
267         cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
268         cmd->base.eth_tp_mdix = phydev->mdix;
269         mutex_unlock(&phydev->lock);
270 }
271 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
272
273 /**
274  * phy_mii_ioctl - generic PHY MII ioctl interface
275  * @phydev: the phy_device struct
276  * @ifr: &struct ifreq for socket ioctl's
277  * @cmd: ioctl cmd to execute
278  *
279  * Note that this function is currently incompatible with the
280  * PHYCONTROL layer.  It changes registers without regard to
281  * current state.  Use at own risk.
282  */
283 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
284 {
285         struct mii_ioctl_data *mii_data = if_mii(ifr);
286         u16 val = mii_data->val_in;
287         bool change_autoneg = false;
288         int prtad, devad;
289
290         switch (cmd) {
291         case SIOCGMIIPHY:
292                 mii_data->phy_id = phydev->mdio.addr;
293                 fallthrough;
294
295         case SIOCGMIIREG:
296                 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
297                         prtad = mdio_phy_id_prtad(mii_data->phy_id);
298                         devad = mdio_phy_id_devad(mii_data->phy_id);
299                         devad = mdiobus_c45_addr(devad, mii_data->reg_num);
300                 } else {
301                         prtad = mii_data->phy_id;
302                         devad = mii_data->reg_num;
303                 }
304                 mii_data->val_out = mdiobus_read(phydev->mdio.bus, prtad,
305                                                  devad);
306                 return 0;
307
308         case SIOCSMIIREG:
309                 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
310                         prtad = mdio_phy_id_prtad(mii_data->phy_id);
311                         devad = mdio_phy_id_devad(mii_data->phy_id);
312                         devad = mdiobus_c45_addr(devad, mii_data->reg_num);
313                 } else {
314                         prtad = mii_data->phy_id;
315                         devad = mii_data->reg_num;
316                 }
317                 if (prtad == phydev->mdio.addr) {
318                         switch (devad) {
319                         case MII_BMCR:
320                                 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
321                                         if (phydev->autoneg == AUTONEG_ENABLE)
322                                                 change_autoneg = true;
323                                         phydev->autoneg = AUTONEG_DISABLE;
324                                         if (val & BMCR_FULLDPLX)
325                                                 phydev->duplex = DUPLEX_FULL;
326                                         else
327                                                 phydev->duplex = DUPLEX_HALF;
328                                         if (val & BMCR_SPEED1000)
329                                                 phydev->speed = SPEED_1000;
330                                         else if (val & BMCR_SPEED100)
331                                                 phydev->speed = SPEED_100;
332                                         else phydev->speed = SPEED_10;
333                                 } else {
334                                         if (phydev->autoneg == AUTONEG_DISABLE)
335                                                 change_autoneg = true;
336                                         phydev->autoneg = AUTONEG_ENABLE;
337                                 }
338                                 break;
339                         case MII_ADVERTISE:
340                                 mii_adv_mod_linkmode_adv_t(phydev->advertising,
341                                                            val);
342                                 change_autoneg = true;
343                                 break;
344                         case MII_CTRL1000:
345                                 mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
346                                                                 val);
347                                 change_autoneg = true;
348                                 break;
349                         default:
350                                 /* do nothing */
351                                 break;
352                         }
353                 }
354
355                 mdiobus_write(phydev->mdio.bus, prtad, devad, val);
356
357                 if (prtad == phydev->mdio.addr &&
358                     devad == MII_BMCR &&
359                     val & BMCR_RESET)
360                         return phy_init_hw(phydev);
361
362                 if (change_autoneg)
363                         return phy_start_aneg(phydev);
364
365                 return 0;
366
367         case SIOCSHWTSTAMP:
368                 if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
369                         return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
370                 fallthrough;
371
372         default:
373                 return -EOPNOTSUPP;
374         }
375 }
376 EXPORT_SYMBOL(phy_mii_ioctl);
377
378 /**
379  * phy_do_ioctl - generic ndo_eth_ioctl implementation
380  * @dev: the net_device struct
381  * @ifr: &struct ifreq for socket ioctl's
382  * @cmd: ioctl cmd to execute
383  */
384 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
385 {
386         if (!dev->phydev)
387                 return -ENODEV;
388
389         return phy_mii_ioctl(dev->phydev, ifr, cmd);
390 }
391 EXPORT_SYMBOL(phy_do_ioctl);
392
393 /**
394  * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first
395  *
396  * @dev: the net_device struct
397  * @ifr: &struct ifreq for socket ioctl's
398  * @cmd: ioctl cmd to execute
399  *
400  * Same as phy_do_ioctl, but ensures that net_device is running before
401  * handling the ioctl.
402  */
403 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
404 {
405         if (!netif_running(dev))
406                 return -ENODEV;
407
408         return phy_do_ioctl(dev, ifr, cmd);
409 }
410 EXPORT_SYMBOL(phy_do_ioctl_running);
411
412 /**
413  * phy_queue_state_machine - Trigger the state machine to run soon
414  *
415  * @phydev: the phy_device struct
416  * @jiffies: Run the state machine after these jiffies
417  */
418 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
419 {
420         mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
421                          jiffies);
422 }
423 EXPORT_SYMBOL(phy_queue_state_machine);
424
425 /**
426  * phy_trigger_machine - Trigger the state machine to run now
427  *
428  * @phydev: the phy_device struct
429  */
430 void phy_trigger_machine(struct phy_device *phydev)
431 {
432         phy_queue_state_machine(phydev, 0);
433 }
434 EXPORT_SYMBOL(phy_trigger_machine);
435
436 static void phy_abort_cable_test(struct phy_device *phydev)
437 {
438         int err;
439
440         ethnl_cable_test_finished(phydev);
441
442         err = phy_init_hw(phydev);
443         if (err)
444                 phydev_err(phydev, "Error while aborting cable test");
445 }
446
447 /**
448  * phy_ethtool_get_strings - Get the statistic counter names
449  *
450  * @phydev: the phy_device struct
451  * @data: Where to put the strings
452  */
453 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
454 {
455         if (!phydev->drv)
456                 return -EIO;
457
458         mutex_lock(&phydev->lock);
459         phydev->drv->get_strings(phydev, data);
460         mutex_unlock(&phydev->lock);
461
462         return 0;
463 }
464 EXPORT_SYMBOL(phy_ethtool_get_strings);
465
466 /**
467  * phy_ethtool_get_sset_count - Get the number of statistic counters
468  *
469  * @phydev: the phy_device struct
470  */
471 int phy_ethtool_get_sset_count(struct phy_device *phydev)
472 {
473         int ret;
474
475         if (!phydev->drv)
476                 return -EIO;
477
478         if (phydev->drv->get_sset_count &&
479             phydev->drv->get_strings &&
480             phydev->drv->get_stats) {
481                 mutex_lock(&phydev->lock);
482                 ret = phydev->drv->get_sset_count(phydev);
483                 mutex_unlock(&phydev->lock);
484
485                 return ret;
486         }
487
488         return -EOPNOTSUPP;
489 }
490 EXPORT_SYMBOL(phy_ethtool_get_sset_count);
491
492 /**
493  * phy_ethtool_get_stats - Get the statistic counters
494  *
495  * @phydev: the phy_device struct
496  * @stats: What counters to get
497  * @data: Where to store the counters
498  */
499 int phy_ethtool_get_stats(struct phy_device *phydev,
500                           struct ethtool_stats *stats, u64 *data)
501 {
502         if (!phydev->drv)
503                 return -EIO;
504
505         mutex_lock(&phydev->lock);
506         phydev->drv->get_stats(phydev, stats, data);
507         mutex_unlock(&phydev->lock);
508
509         return 0;
510 }
511 EXPORT_SYMBOL(phy_ethtool_get_stats);
512
513 /**
514  * phy_start_cable_test - Start a cable test
515  *
516  * @phydev: the phy_device struct
517  * @extack: extack for reporting useful error messages
518  */
519 int phy_start_cable_test(struct phy_device *phydev,
520                          struct netlink_ext_ack *extack)
521 {
522         struct net_device *dev = phydev->attached_dev;
523         int err = -ENOMEM;
524
525         if (!(phydev->drv &&
526               phydev->drv->cable_test_start &&
527               phydev->drv->cable_test_get_status)) {
528                 NL_SET_ERR_MSG(extack,
529                                "PHY driver does not support cable testing");
530                 return -EOPNOTSUPP;
531         }
532
533         mutex_lock(&phydev->lock);
534         if (phydev->state == PHY_CABLETEST) {
535                 NL_SET_ERR_MSG(extack,
536                                "PHY already performing a test");
537                 err = -EBUSY;
538                 goto out;
539         }
540
541         if (phydev->state < PHY_UP ||
542             phydev->state > PHY_CABLETEST) {
543                 NL_SET_ERR_MSG(extack,
544                                "PHY not configured. Try setting interface up");
545                 err = -EBUSY;
546                 goto out;
547         }
548
549         err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
550         if (err)
551                 goto out;
552
553         /* Mark the carrier down until the test is complete */
554         phy_link_down(phydev);
555
556         netif_testing_on(dev);
557         err = phydev->drv->cable_test_start(phydev);
558         if (err) {
559                 netif_testing_off(dev);
560                 phy_link_up(phydev);
561                 goto out_free;
562         }
563
564         phydev->state = PHY_CABLETEST;
565
566         if (phy_polling_mode(phydev))
567                 phy_trigger_machine(phydev);
568
569         mutex_unlock(&phydev->lock);
570
571         return 0;
572
573 out_free:
574         ethnl_cable_test_free(phydev);
575 out:
576         mutex_unlock(&phydev->lock);
577
578         return err;
579 }
580 EXPORT_SYMBOL(phy_start_cable_test);
581
582 /**
583  * phy_start_cable_test_tdr - Start a raw TDR cable test
584  *
585  * @phydev: the phy_device struct
586  * @extack: extack for reporting useful error messages
587  * @config: Configuration of the test to run
588  */
589 int phy_start_cable_test_tdr(struct phy_device *phydev,
590                              struct netlink_ext_ack *extack,
591                              const struct phy_tdr_config *config)
592 {
593         struct net_device *dev = phydev->attached_dev;
594         int err = -ENOMEM;
595
596         if (!(phydev->drv &&
597               phydev->drv->cable_test_tdr_start &&
598               phydev->drv->cable_test_get_status)) {
599                 NL_SET_ERR_MSG(extack,
600                                "PHY driver does not support cable test TDR");
601                 return -EOPNOTSUPP;
602         }
603
604         mutex_lock(&phydev->lock);
605         if (phydev->state == PHY_CABLETEST) {
606                 NL_SET_ERR_MSG(extack,
607                                "PHY already performing a test");
608                 err = -EBUSY;
609                 goto out;
610         }
611
612         if (phydev->state < PHY_UP ||
613             phydev->state > PHY_CABLETEST) {
614                 NL_SET_ERR_MSG(extack,
615                                "PHY not configured. Try setting interface up");
616                 err = -EBUSY;
617                 goto out;
618         }
619
620         err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
621         if (err)
622                 goto out;
623
624         /* Mark the carrier down until the test is complete */
625         phy_link_down(phydev);
626
627         netif_testing_on(dev);
628         err = phydev->drv->cable_test_tdr_start(phydev, config);
629         if (err) {
630                 netif_testing_off(dev);
631                 phy_link_up(phydev);
632                 goto out_free;
633         }
634
635         phydev->state = PHY_CABLETEST;
636
637         if (phy_polling_mode(phydev))
638                 phy_trigger_machine(phydev);
639
640         mutex_unlock(&phydev->lock);
641
642         return 0;
643
644 out_free:
645         ethnl_cable_test_free(phydev);
646 out:
647         mutex_unlock(&phydev->lock);
648
649         return err;
650 }
651 EXPORT_SYMBOL(phy_start_cable_test_tdr);
652
653 int phy_config_aneg(struct phy_device *phydev)
654 {
655         if (phydev->drv->config_aneg)
656                 return phydev->drv->config_aneg(phydev);
657
658         /* Clause 45 PHYs that don't implement Clause 22 registers are not
659          * allowed to call genphy_config_aneg()
660          */
661         if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
662                 return genphy_c45_config_aneg(phydev);
663
664         return genphy_config_aneg(phydev);
665 }
666 EXPORT_SYMBOL(phy_config_aneg);
667
668 /**
669  * phy_check_link_status - check link status and set state accordingly
670  * @phydev: the phy_device struct
671  *
672  * Description: Check for link and whether autoneg was triggered / is running
673  * and set state accordingly
674  */
675 static int phy_check_link_status(struct phy_device *phydev)
676 {
677         int err;
678
679         lockdep_assert_held(&phydev->lock);
680
681         /* Keep previous state if loopback is enabled because some PHYs
682          * report that Link is Down when loopback is enabled.
683          */
684         if (phydev->loopback_enabled)
685                 return 0;
686
687         err = phy_read_status(phydev);
688         if (err)
689                 return err;
690
691         if (phydev->link && phydev->state != PHY_RUNNING) {
692                 phy_check_downshift(phydev);
693                 phydev->state = PHY_RUNNING;
694                 phy_link_up(phydev);
695         } else if (!phydev->link && phydev->state != PHY_NOLINK) {
696                 phydev->state = PHY_NOLINK;
697                 phy_link_down(phydev);
698         }
699
700         return 0;
701 }
702
703 /**
704  * _phy_start_aneg - start auto-negotiation for this PHY device
705  * @phydev: the phy_device struct
706  *
707  * Description: Sanitizes the settings (if we're not autonegotiating
708  *   them), and then calls the driver's config_aneg function.
709  *   If the PHYCONTROL Layer is operating, we change the state to
710  *   reflect the beginning of Auto-negotiation or forcing.
711  */
712 static int _phy_start_aneg(struct phy_device *phydev)
713 {
714         int err;
715
716         lockdep_assert_held(&phydev->lock);
717
718         if (!phydev->drv)
719                 return -EIO;
720
721         if (AUTONEG_DISABLE == phydev->autoneg)
722                 phy_sanitize_settings(phydev);
723
724         err = phy_config_aneg(phydev);
725         if (err < 0)
726                 return err;
727
728         if (phy_is_started(phydev))
729                 err = phy_check_link_status(phydev);
730
731         return err;
732 }
733
734 /**
735  * phy_start_aneg - start auto-negotiation for this PHY device
736  * @phydev: the phy_device struct
737  *
738  * Description: Sanitizes the settings (if we're not autonegotiating
739  *   them), and then calls the driver's config_aneg function.
740  *   If the PHYCONTROL Layer is operating, we change the state to
741  *   reflect the beginning of Auto-negotiation or forcing.
742  */
743 int phy_start_aneg(struct phy_device *phydev)
744 {
745         int err;
746
747         mutex_lock(&phydev->lock);
748         err = _phy_start_aneg(phydev);
749         mutex_unlock(&phydev->lock);
750
751         return err;
752 }
753 EXPORT_SYMBOL(phy_start_aneg);
754
755 static int phy_poll_aneg_done(struct phy_device *phydev)
756 {
757         unsigned int retries = 100;
758         int ret;
759
760         do {
761                 msleep(100);
762                 ret = phy_aneg_done(phydev);
763         } while (!ret && --retries);
764
765         if (!ret)
766                 return -ETIMEDOUT;
767
768         return ret < 0 ? ret : 0;
769 }
770
771 int phy_ethtool_ksettings_set(struct phy_device *phydev,
772                               const struct ethtool_link_ksettings *cmd)
773 {
774         __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
775         u8 autoneg = cmd->base.autoneg;
776         u8 duplex = cmd->base.duplex;
777         u32 speed = cmd->base.speed;
778
779         if (cmd->base.phy_address != phydev->mdio.addr)
780                 return -EINVAL;
781
782         linkmode_copy(advertising, cmd->link_modes.advertising);
783
784         /* We make sure that we don't pass unsupported values in to the PHY */
785         linkmode_and(advertising, advertising, phydev->supported);
786
787         /* Verify the settings we care about. */
788         if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
789                 return -EINVAL;
790
791         if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
792                 return -EINVAL;
793
794         if (autoneg == AUTONEG_DISABLE &&
795             ((speed != SPEED_1000 &&
796               speed != SPEED_100 &&
797               speed != SPEED_10) ||
798              (duplex != DUPLEX_HALF &&
799               duplex != DUPLEX_FULL)))
800                 return -EINVAL;
801
802         mutex_lock(&phydev->lock);
803         phydev->autoneg = autoneg;
804
805         if (autoneg == AUTONEG_DISABLE) {
806                 phydev->speed = speed;
807                 phydev->duplex = duplex;
808         }
809
810         linkmode_copy(phydev->advertising, advertising);
811
812         linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
813                          phydev->advertising, autoneg == AUTONEG_ENABLE);
814
815         phydev->master_slave_set = cmd->base.master_slave_cfg;
816         phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
817
818         /* Restart the PHY */
819         if (phy_is_started(phydev)) {
820                 phydev->state = PHY_UP;
821                 phy_trigger_machine(phydev);
822         } else {
823                 _phy_start_aneg(phydev);
824         }
825
826         mutex_unlock(&phydev->lock);
827         return 0;
828 }
829 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
830
831 /**
832  * phy_speed_down - set speed to lowest speed supported by both link partners
833  * @phydev: the phy_device struct
834  * @sync: perform action synchronously
835  *
836  * Description: Typically used to save energy when waiting for a WoL packet
837  *
838  * WARNING: Setting sync to false may cause the system being unable to suspend
839  * in case the PHY generates an interrupt when finishing the autonegotiation.
840  * This interrupt may wake up the system immediately after suspend.
841  * Therefore use sync = false only if you're sure it's safe with the respective
842  * network chip.
843  */
844 int phy_speed_down(struct phy_device *phydev, bool sync)
845 {
846         __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
847         int ret;
848
849         if (phydev->autoneg != AUTONEG_ENABLE)
850                 return 0;
851
852         linkmode_copy(adv_tmp, phydev->advertising);
853
854         ret = phy_speed_down_core(phydev);
855         if (ret)
856                 return ret;
857
858         linkmode_copy(phydev->adv_old, adv_tmp);
859
860         if (linkmode_equal(phydev->advertising, adv_tmp))
861                 return 0;
862
863         ret = phy_config_aneg(phydev);
864         if (ret)
865                 return ret;
866
867         return sync ? phy_poll_aneg_done(phydev) : 0;
868 }
869 EXPORT_SYMBOL_GPL(phy_speed_down);
870
871 /**
872  * phy_speed_up - (re)set advertised speeds to all supported speeds
873  * @phydev: the phy_device struct
874  *
875  * Description: Used to revert the effect of phy_speed_down
876  */
877 int phy_speed_up(struct phy_device *phydev)
878 {
879         __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
880
881         if (phydev->autoneg != AUTONEG_ENABLE)
882                 return 0;
883
884         if (linkmode_empty(phydev->adv_old))
885                 return 0;
886
887         linkmode_copy(adv_tmp, phydev->advertising);
888         linkmode_copy(phydev->advertising, phydev->adv_old);
889         linkmode_zero(phydev->adv_old);
890
891         if (linkmode_equal(phydev->advertising, adv_tmp))
892                 return 0;
893
894         return phy_config_aneg(phydev);
895 }
896 EXPORT_SYMBOL_GPL(phy_speed_up);
897
898 /**
899  * phy_start_machine - start PHY state machine tracking
900  * @phydev: the phy_device struct
901  *
902  * Description: The PHY infrastructure can run a state machine
903  *   which tracks whether the PHY is starting up, negotiating,
904  *   etc.  This function starts the delayed workqueue which tracks
905  *   the state of the PHY. If you want to maintain your own state machine,
906  *   do not call this function.
907  */
908 void phy_start_machine(struct phy_device *phydev)
909 {
910         phy_trigger_machine(phydev);
911 }
912 EXPORT_SYMBOL_GPL(phy_start_machine);
913
914 /**
915  * phy_stop_machine - stop the PHY state machine tracking
916  * @phydev: target phy_device struct
917  *
918  * Description: Stops the state machine delayed workqueue, sets the
919  *   state to UP (unless it wasn't up yet). This function must be
920  *   called BEFORE phy_detach.
921  */
922 void phy_stop_machine(struct phy_device *phydev)
923 {
924         cancel_delayed_work_sync(&phydev->state_queue);
925
926         mutex_lock(&phydev->lock);
927         if (phy_is_started(phydev))
928                 phydev->state = PHY_UP;
929         mutex_unlock(&phydev->lock);
930 }
931
932 /**
933  * phy_error - enter HALTED state for this PHY device
934  * @phydev: target phy_device struct
935  *
936  * Moves the PHY to the HALTED state in response to a read
937  * or write error, and tells the controller the link is down.
938  * Must not be called from interrupt context, or while the
939  * phydev->lock is held.
940  */
941 void phy_error(struct phy_device *phydev)
942 {
943         WARN_ON(1);
944
945         mutex_lock(&phydev->lock);
946         phydev->state = PHY_HALTED;
947         mutex_unlock(&phydev->lock);
948
949         phy_trigger_machine(phydev);
950 }
951 EXPORT_SYMBOL(phy_error);
952
953 /**
954  * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
955  * @phydev: target phy_device struct
956  */
957 int phy_disable_interrupts(struct phy_device *phydev)
958 {
959         /* Disable PHY interrupts */
960         return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
961 }
962
963 /**
964  * phy_interrupt - PHY interrupt handler
965  * @irq: interrupt line
966  * @phy_dat: phy_device pointer
967  *
968  * Description: Handle PHY interrupt
969  */
970 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
971 {
972         struct phy_device *phydev = phy_dat;
973         struct phy_driver *drv = phydev->drv;
974         irqreturn_t ret;
975
976         /* Wakeup interrupts may occur during a system sleep transition.
977          * Postpone handling until the PHY has resumed.
978          */
979         if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
980                 struct net_device *netdev = phydev->attached_dev;
981
982                 if (netdev) {
983                         struct device *parent = netdev->dev.parent;
984
985                         if (netdev->wol_enabled)
986                                 pm_system_wakeup();
987                         else if (device_may_wakeup(&netdev->dev))
988                                 pm_wakeup_dev_event(&netdev->dev, 0, true);
989                         else if (parent && device_may_wakeup(parent))
990                                 pm_wakeup_dev_event(parent, 0, true);
991                 }
992
993                 phydev->irq_rerun = 1;
994                 disable_irq_nosync(irq);
995                 return IRQ_HANDLED;
996         }
997
998         mutex_lock(&phydev->lock);
999         ret = drv->handle_interrupt(phydev);
1000         mutex_unlock(&phydev->lock);
1001
1002         return ret;
1003 }
1004
1005 /**
1006  * phy_enable_interrupts - Enable the interrupts from the PHY side
1007  * @phydev: target phy_device struct
1008  */
1009 static int phy_enable_interrupts(struct phy_device *phydev)
1010 {
1011         return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1012 }
1013
1014 /**
1015  * phy_request_interrupt - request and enable interrupt for a PHY device
1016  * @phydev: target phy_device struct
1017  *
1018  * Description: Request and enable the interrupt for the given PHY.
1019  *   If this fails, then we set irq to PHY_POLL.
1020  *   This should only be called with a valid IRQ number.
1021  */
1022 void phy_request_interrupt(struct phy_device *phydev)
1023 {
1024         int err;
1025
1026         err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1027                                    IRQF_ONESHOT | IRQF_SHARED,
1028                                    phydev_name(phydev), phydev);
1029         if (err) {
1030                 phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1031                             err, phydev->irq);
1032                 phydev->irq = PHY_POLL;
1033         } else {
1034                 if (phy_enable_interrupts(phydev)) {
1035                         phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1036                         phy_free_interrupt(phydev);
1037                         phydev->irq = PHY_POLL;
1038                 }
1039         }
1040 }
1041 EXPORT_SYMBOL(phy_request_interrupt);
1042
1043 /**
1044  * phy_free_interrupt - disable and free interrupt for a PHY device
1045  * @phydev: target phy_device struct
1046  *
1047  * Description: Disable and free the interrupt for the given PHY.
1048  *   This should only be called with a valid IRQ number.
1049  */
1050 void phy_free_interrupt(struct phy_device *phydev)
1051 {
1052         phy_disable_interrupts(phydev);
1053         free_irq(phydev->irq, phydev);
1054 }
1055 EXPORT_SYMBOL(phy_free_interrupt);
1056
1057 /**
1058  * phy_stop - Bring down the PHY link, and stop checking the status
1059  * @phydev: target phy_device struct
1060  */
1061 void phy_stop(struct phy_device *phydev)
1062 {
1063         struct net_device *dev = phydev->attached_dev;
1064
1065         if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
1066                 WARN(1, "called from state %s\n",
1067                      phy_state_to_str(phydev->state));
1068                 return;
1069         }
1070
1071         mutex_lock(&phydev->lock);
1072
1073         if (phydev->state == PHY_CABLETEST) {
1074                 phy_abort_cable_test(phydev);
1075                 netif_testing_off(dev);
1076         }
1077
1078         if (phydev->sfp_bus)
1079                 sfp_upstream_stop(phydev->sfp_bus);
1080
1081         phydev->state = PHY_HALTED;
1082
1083         mutex_unlock(&phydev->lock);
1084
1085         phy_state_machine(&phydev->state_queue.work);
1086         phy_stop_machine(phydev);
1087
1088         /* Cannot call flush_scheduled_work() here as desired because
1089          * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
1090          * will not reenable interrupts.
1091          */
1092 }
1093 EXPORT_SYMBOL(phy_stop);
1094
1095 /**
1096  * phy_start - start or restart a PHY device
1097  * @phydev: target phy_device struct
1098  *
1099  * Description: Indicates the attached device's readiness to
1100  *   handle PHY-related work.  Used during startup to start the
1101  *   PHY, and after a call to phy_stop() to resume operation.
1102  *   Also used to indicate the MDIO bus has cleared an error
1103  *   condition.
1104  */
1105 void phy_start(struct phy_device *phydev)
1106 {
1107         mutex_lock(&phydev->lock);
1108
1109         if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1110                 WARN(1, "called from state %s\n",
1111                      phy_state_to_str(phydev->state));
1112                 goto out;
1113         }
1114
1115         if (phydev->sfp_bus)
1116                 sfp_upstream_start(phydev->sfp_bus);
1117
1118         /* if phy was suspended, bring the physical link up again */
1119         __phy_resume(phydev);
1120
1121         phydev->state = PHY_UP;
1122
1123         phy_start_machine(phydev);
1124 out:
1125         mutex_unlock(&phydev->lock);
1126 }
1127 EXPORT_SYMBOL(phy_start);
1128
1129 /**
1130  * phy_state_machine - Handle the state machine
1131  * @work: work_struct that describes the work to be done
1132  */
1133 void phy_state_machine(struct work_struct *work)
1134 {
1135         struct delayed_work *dwork = to_delayed_work(work);
1136         struct phy_device *phydev =
1137                         container_of(dwork, struct phy_device, state_queue);
1138         struct net_device *dev = phydev->attached_dev;
1139         bool needs_aneg = false, do_suspend = false;
1140         enum phy_state old_state;
1141         bool finished = false;
1142         int err = 0;
1143
1144         mutex_lock(&phydev->lock);
1145
1146         old_state = phydev->state;
1147
1148         switch (phydev->state) {
1149         case PHY_DOWN:
1150         case PHY_READY:
1151                 break;
1152         case PHY_UP:
1153                 needs_aneg = true;
1154
1155                 break;
1156         case PHY_NOLINK:
1157         case PHY_RUNNING:
1158                 err = phy_check_link_status(phydev);
1159                 break;
1160         case PHY_CABLETEST:
1161                 err = phydev->drv->cable_test_get_status(phydev, &finished);
1162                 if (err) {
1163                         phy_abort_cable_test(phydev);
1164                         netif_testing_off(dev);
1165                         needs_aneg = true;
1166                         phydev->state = PHY_UP;
1167                         break;
1168                 }
1169
1170                 if (finished) {
1171                         ethnl_cable_test_finished(phydev);
1172                         netif_testing_off(dev);
1173                         needs_aneg = true;
1174                         phydev->state = PHY_UP;
1175                 }
1176                 break;
1177         case PHY_HALTED:
1178                 if (phydev->link) {
1179                         phydev->link = 0;
1180                         phy_link_down(phydev);
1181                 }
1182                 do_suspend = true;
1183                 break;
1184         }
1185
1186         mutex_unlock(&phydev->lock);
1187
1188         if (needs_aneg)
1189                 err = phy_start_aneg(phydev);
1190         else if (do_suspend)
1191                 phy_suspend(phydev);
1192
1193         if (err == -ENODEV)
1194                 return;
1195
1196         if (err < 0)
1197                 phy_error(phydev);
1198
1199         if (old_state != phydev->state) {
1200                 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1201                            phy_state_to_str(old_state),
1202                            phy_state_to_str(phydev->state));
1203                 if (phydev->drv && phydev->drv->link_change_notify)
1204                         phydev->drv->link_change_notify(phydev);
1205         }
1206
1207         /* Only re-schedule a PHY state machine change if we are polling the
1208          * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
1209          * between states from phy_mac_interrupt().
1210          *
1211          * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1212          * state machine would be pointless and possibly error prone when
1213          * called from phy_disconnect() synchronously.
1214          */
1215         mutex_lock(&phydev->lock);
1216         if (phy_polling_mode(phydev) && phy_is_started(phydev))
1217                 phy_queue_state_machine(phydev, PHY_STATE_TIME);
1218         mutex_unlock(&phydev->lock);
1219 }
1220
1221 /**
1222  * phy_mac_interrupt - MAC says the link has changed
1223  * @phydev: phy_device struct with changed link
1224  *
1225  * The MAC layer is able to indicate there has been a change in the PHY link
1226  * status. Trigger the state machine and work a work queue.
1227  */
1228 void phy_mac_interrupt(struct phy_device *phydev)
1229 {
1230         /* Trigger a state machine change */
1231         phy_trigger_machine(phydev);
1232 }
1233 EXPORT_SYMBOL(phy_mac_interrupt);
1234
1235 static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
1236 {
1237         linkmode_zero(advertising);
1238
1239         if (eee_adv & MDIO_EEE_100TX)
1240                 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1241                                  advertising);
1242         if (eee_adv & MDIO_EEE_1000T)
1243                 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1244                                  advertising);
1245         if (eee_adv & MDIO_EEE_10GT)
1246                 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1247                                  advertising);
1248         if (eee_adv & MDIO_EEE_1000KX)
1249                 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1250                                  advertising);
1251         if (eee_adv & MDIO_EEE_10GKX4)
1252                 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1253                                  advertising);
1254         if (eee_adv & MDIO_EEE_10GKR)
1255                 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1256                                  advertising);
1257 }
1258
1259 /**
1260  * phy_init_eee - init and check the EEE feature
1261  * @phydev: target phy_device struct
1262  * @clk_stop_enable: PHY may stop the clock during LPI
1263  *
1264  * Description: it checks if the Energy-Efficient Ethernet (EEE)
1265  * is supported by looking at the MMD registers 3.20 and 7.60/61
1266  * and it programs the MMD register 3.0 setting the "Clock stop enable"
1267  * bit if required.
1268  */
1269 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1270 {
1271         if (!phydev->drv)
1272                 return -EIO;
1273
1274         /* According to 802.3az,the EEE is supported only in full duplex-mode.
1275          */
1276         if (phydev->duplex == DUPLEX_FULL) {
1277                 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
1278                 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp);
1279                 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
1280                 int eee_lp, eee_cap, eee_adv;
1281                 int status;
1282                 u32 cap;
1283
1284                 /* Read phy status to properly get the right settings */
1285                 status = phy_read_status(phydev);
1286                 if (status)
1287                         return status;
1288
1289                 /* First check if the EEE ability is supported */
1290                 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1291                 if (eee_cap <= 0)
1292                         goto eee_exit_err;
1293
1294                 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1295                 if (!cap)
1296                         goto eee_exit_err;
1297
1298                 /* Check which link settings negotiated and verify it in
1299                  * the EEE advertising registers.
1300                  */
1301                 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1302                 if (eee_lp <= 0)
1303                         goto eee_exit_err;
1304
1305                 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1306                 if (eee_adv <= 0)
1307                         goto eee_exit_err;
1308
1309                 mmd_eee_adv_to_linkmode(adv, eee_adv);
1310                 mmd_eee_adv_to_linkmode(lp, eee_lp);
1311                 linkmode_and(common, adv, lp);
1312
1313                 if (!phy_check_valid(phydev->speed, phydev->duplex, common))
1314                         goto eee_exit_err;
1315
1316                 if (clk_stop_enable)
1317                         /* Configure the PHY to stop receiving xMII
1318                          * clock while it is signaling LPI.
1319                          */
1320                         phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1321                                          MDIO_PCS_CTRL1_CLKSTOP_EN);
1322
1323                 return 0; /* EEE supported */
1324         }
1325 eee_exit_err:
1326         return -EPROTONOSUPPORT;
1327 }
1328 EXPORT_SYMBOL(phy_init_eee);
1329
1330 /**
1331  * phy_get_eee_err - report the EEE wake error count
1332  * @phydev: target phy_device struct
1333  *
1334  * Description: it is to report the number of time where the PHY
1335  * failed to complete its normal wake sequence.
1336  */
1337 int phy_get_eee_err(struct phy_device *phydev)
1338 {
1339         if (!phydev->drv)
1340                 return -EIO;
1341
1342         return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1343 }
1344 EXPORT_SYMBOL(phy_get_eee_err);
1345
1346 /**
1347  * phy_ethtool_get_eee - get EEE supported and status
1348  * @phydev: target phy_device struct
1349  * @data: ethtool_eee data
1350  *
1351  * Description: it reportes the Supported/Advertisement/LP Advertisement
1352  * capabilities.
1353  */
1354 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1355 {
1356         int val;
1357
1358         if (!phydev->drv)
1359                 return -EIO;
1360
1361         /* Get Supported EEE */
1362         val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1363         if (val < 0)
1364                 return val;
1365         data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1366
1367         /* Get advertisement EEE */
1368         val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1369         if (val < 0)
1370                 return val;
1371         data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1372         data->eee_enabled = !!data->advertised;
1373
1374         /* Get LP advertisement EEE */
1375         val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1376         if (val < 0)
1377                 return val;
1378         data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1379
1380         data->eee_active = !!(data->advertised & data->lp_advertised);
1381
1382         return 0;
1383 }
1384 EXPORT_SYMBOL(phy_ethtool_get_eee);
1385
1386 /**
1387  * phy_ethtool_set_eee - set EEE supported and status
1388  * @phydev: target phy_device struct
1389  * @data: ethtool_eee data
1390  *
1391  * Description: it is to program the Advertisement EEE register.
1392  */
1393 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1394 {
1395         int cap, old_adv, adv = 0, ret;
1396
1397         if (!phydev->drv)
1398                 return -EIO;
1399
1400         /* Get Supported EEE */
1401         cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1402         if (cap < 0)
1403                 return cap;
1404
1405         old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1406         if (old_adv < 0)
1407                 return old_adv;
1408
1409         if (data->eee_enabled) {
1410                 adv = !data->advertised ? cap :
1411                       ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1412                 /* Mask prohibited EEE modes */
1413                 adv &= ~phydev->eee_broken_modes;
1414         }
1415
1416         if (old_adv != adv) {
1417                 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1418                 if (ret < 0)
1419                         return ret;
1420
1421                 /* Restart autonegotiation so the new modes get sent to the
1422                  * link partner.
1423                  */
1424                 if (phydev->autoneg == AUTONEG_ENABLE) {
1425                         ret = phy_restart_aneg(phydev);
1426                         if (ret < 0)
1427                                 return ret;
1428                 }
1429         }
1430
1431         return 0;
1432 }
1433 EXPORT_SYMBOL(phy_ethtool_set_eee);
1434
1435 /**
1436  * phy_ethtool_set_wol - Configure Wake On LAN
1437  *
1438  * @phydev: target phy_device struct
1439  * @wol: Configuration requested
1440  */
1441 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1442 {
1443         if (phydev->drv && phydev->drv->set_wol)
1444                 return phydev->drv->set_wol(phydev, wol);
1445
1446         return -EOPNOTSUPP;
1447 }
1448 EXPORT_SYMBOL(phy_ethtool_set_wol);
1449
1450 /**
1451  * phy_ethtool_get_wol - Get the current Wake On LAN configuration
1452  *
1453  * @phydev: target phy_device struct
1454  * @wol: Store the current configuration here
1455  */
1456 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1457 {
1458         if (phydev->drv && phydev->drv->get_wol)
1459                 phydev->drv->get_wol(phydev, wol);
1460 }
1461 EXPORT_SYMBOL(phy_ethtool_get_wol);
1462
1463 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1464                                    struct ethtool_link_ksettings *cmd)
1465 {
1466         struct phy_device *phydev = ndev->phydev;
1467
1468         if (!phydev)
1469                 return -ENODEV;
1470
1471         phy_ethtool_ksettings_get(phydev, cmd);
1472
1473         return 0;
1474 }
1475 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1476
1477 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1478                                    const struct ethtool_link_ksettings *cmd)
1479 {
1480         struct phy_device *phydev = ndev->phydev;
1481
1482         if (!phydev)
1483                 return -ENODEV;
1484
1485         return phy_ethtool_ksettings_set(phydev, cmd);
1486 }
1487 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1488
1489 /**
1490  * phy_ethtool_nway_reset - Restart auto negotiation
1491  * @ndev: Network device to restart autoneg for
1492  */
1493 int phy_ethtool_nway_reset(struct net_device *ndev)
1494 {
1495         struct phy_device *phydev = ndev->phydev;
1496
1497         if (!phydev)
1498                 return -ENODEV;
1499
1500         if (!phydev->drv)
1501                 return -EIO;
1502
1503         return phy_restart_aneg(phydev);
1504 }
1505 EXPORT_SYMBOL(phy_ethtool_nway_reset);