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