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