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