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