Fix numerous minor problems with new phy subsystem.
[safe/jmp/linux-2.6] / drivers / net / phy / phy.c
1 /*
2  * drivers/net/phy/phy.c
3  *
4  * Framework for configuring and reading PHY devices
5  * Based on code in sungem_phy.c and gianfar_phy.c
6  *
7  * Author: Andy Fleming
8  *
9  * Copyright (c) 2004 Freescale Semiconductor, Inc.
10  *
11  * This program is free software; you can redistribute  it and/or modify it
12  * under  the terms of  the GNU General  Public License as published by the
13  * Free Software Foundation;  either version 2 of the  License, or (at your
14  * option) any later version.
15  *
16  */
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/phy.h>
37
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 #include <asm/uaccess.h>
41
42 static void phy_timer(unsigned long data);
43
44 /* Convenience function to print out the current phy status
45  */
46 void phy_print_status(struct phy_device *phydev)
47 {
48         pr_info("%s: Link is %s", phydev->dev.bus_id,
49                         phydev->link ? "Up" : "Down");
50         if (phydev->link)
51                 printk(" - %d/%s", phydev->speed,
52                                 DUPLEX_FULL == phydev->duplex ?
53                                 "Full" : "Half");
54
55         printk("\n");
56 }
57 EXPORT_SYMBOL(phy_print_status);
58
59
60 /* Convenience functions for reading/writing a given PHY
61  * register. They MUST NOT be called from interrupt context,
62  * because the bus read/write functions may wait for an interrupt
63  * to conclude the operation. */
64 int phy_read(struct phy_device *phydev, u16 regnum)
65 {
66         int retval;
67         struct mii_bus *bus = phydev->bus;
68
69         spin_lock_bh(&bus->mdio_lock);
70         retval = bus->read(bus, phydev->addr, regnum);
71         spin_unlock_bh(&bus->mdio_lock);
72
73         return retval;
74 }
75 EXPORT_SYMBOL(phy_read);
76
77 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
78 {
79         int err;
80         struct mii_bus *bus = phydev->bus;
81
82         spin_lock_bh(&bus->mdio_lock);
83         err = bus->write(bus, phydev->addr, regnum, val);
84         spin_unlock_bh(&bus->mdio_lock);
85
86         return err;
87 }
88 EXPORT_SYMBOL(phy_write);
89
90
91 int phy_clear_interrupt(struct phy_device *phydev)
92 {
93         int err = 0;
94
95         if (phydev->drv->ack_interrupt)
96                 err = phydev->drv->ack_interrupt(phydev);
97
98         return err;
99 }
100
101
102 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
103 {
104         int err = 0;
105
106         phydev->interrupts = interrupts;
107         if (phydev->drv->config_intr)
108                 err = phydev->drv->config_intr(phydev);
109
110         return err;
111 }
112
113
114 /* phy_aneg_done
115  *
116  * description: Reads the status register and returns 0 either if
117  *   auto-negotiation is incomplete, or if there was an error.
118  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
119  */
120 static inline int phy_aneg_done(struct phy_device *phydev)
121 {
122         int retval;
123
124         retval = phy_read(phydev, MII_BMSR);
125
126         return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
127 }
128
129 /* phy_start_aneg
130  *
131  * description: Calls the PHY driver's config_aneg, and then
132  *   sets the PHY state to PHY_AN if auto-negotiation is enabled,
133  *   and to PHY_FORCING if auto-negotiation is disabled. Unless
134  *   the PHY is currently HALTED.
135  */
136 int phy_start_aneg(struct phy_device *phydev)
137 {
138         int err;
139
140         spin_lock(&phydev->lock);
141
142         if (AUTONEG_DISABLE == phydev->autoneg)
143                 phy_sanitize_settings(phydev);
144
145         err = phydev->drv->config_aneg(phydev);
146
147         if (err < 0)
148                 goto out_unlock;
149
150         if (phydev->state != PHY_HALTED) {
151                 if (AUTONEG_ENABLE == phydev->autoneg) {
152                         phydev->state = PHY_AN;
153                         phydev->link_timeout = PHY_AN_TIMEOUT;
154                 } else {
155                         phydev->state = PHY_FORCING;
156                         phydev->link_timeout = PHY_FORCE_TIMEOUT;
157                 }
158         }
159
160 out_unlock:
161         spin_unlock(&phydev->lock);
162         return err;
163 }
164 EXPORT_SYMBOL(phy_start_aneg);
165
166
167 /* A structure for mapping a particular speed and duplex
168  * combination to a particular SUPPORTED and ADVERTISED value */
169 struct phy_setting {
170         int speed;
171         int duplex;
172         u32 setting;
173 };
174
175 /* A mapping of all SUPPORTED settings to speed/duplex */
176 static struct phy_setting settings[] = {
177         {
178                 .speed = 10000,
179                 .duplex = DUPLEX_FULL,
180                 .setting = SUPPORTED_10000baseT_Full,
181         },
182         {
183                 .speed = SPEED_1000,
184                 .duplex = DUPLEX_FULL,
185                 .setting = SUPPORTED_1000baseT_Full,
186         },
187         {
188                 .speed = SPEED_1000,
189                 .duplex = DUPLEX_HALF,
190                 .setting = SUPPORTED_1000baseT_Half,
191         },
192         {
193                 .speed = SPEED_100,
194                 .duplex = DUPLEX_FULL,
195                 .setting = SUPPORTED_100baseT_Full,
196         },
197         {
198                 .speed = SPEED_100,
199                 .duplex = DUPLEX_HALF,
200                 .setting = SUPPORTED_100baseT_Half,
201         },
202         {
203                 .speed = SPEED_10,
204                 .duplex = DUPLEX_FULL,
205                 .setting = SUPPORTED_10baseT_Full,
206         },
207         {
208                 .speed = SPEED_10,
209                 .duplex = DUPLEX_HALF,
210                 .setting = SUPPORTED_10baseT_Half,
211         },
212 };
213
214 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
215
216 /* phy_find_setting
217  *
218  * description: Searches the settings array for the setting which
219  *   matches the desired speed and duplex, and returns the index
220  *   of that setting.  Returns the index of the last setting if
221  *   none of the others match.
222  */
223 static inline int phy_find_setting(int speed, int duplex)
224 {
225         int idx = 0;
226
227         while (idx < ARRAY_SIZE(settings) &&
228                         (settings[idx].speed != speed ||
229                         settings[idx].duplex != duplex))
230                 idx++;
231
232         return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
233 }
234
235 /* phy_find_valid
236  * idx: The first index in settings[] to search
237  * features: A mask of the valid settings
238  *
239  * description: Returns the index of the first valid setting less
240  *   than or equal to the one pointed to by idx, as determined by
241  *   the mask in features.  Returns the index of the last setting
242  *   if nothing else matches.
243  */
244 static inline int phy_find_valid(int idx, u32 features)
245 {
246         while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
247                 idx++;
248
249         return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
250 }
251
252 /* phy_sanitize_settings
253  *
254  * description: Make sure the PHY is set to supported speeds and
255  *   duplexes.  Drop down by one in this order:  1000/FULL,
256  *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
257  */
258 void phy_sanitize_settings(struct phy_device *phydev)
259 {
260         u32 features = phydev->supported;
261         int idx;
262
263         /* Sanitize settings based on PHY capabilities */
264         if ((features & SUPPORTED_Autoneg) == 0)
265                 phydev->autoneg = 0;
266
267         idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
268                         features);
269
270         phydev->speed = settings[idx].speed;
271         phydev->duplex = settings[idx].duplex;
272 }
273 EXPORT_SYMBOL(phy_sanitize_settings);
274
275 /* phy_force_reduction
276  *
277  * description: Reduces the speed/duplex settings by
278  *   one notch.  The order is so:
279  *   1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
280  *   10/FULL, 10/HALF.  The function bottoms out at 10/HALF.
281  */
282 static void phy_force_reduction(struct phy_device *phydev)
283 {
284         int idx;
285
286         idx = phy_find_setting(phydev->speed, phydev->duplex);
287         
288         idx++;
289
290         idx = phy_find_valid(idx, phydev->supported);
291
292         phydev->speed = settings[idx].speed;
293         phydev->duplex = settings[idx].duplex;
294
295         pr_info("Trying %d/%s\n", phydev->speed,
296                         DUPLEX_FULL == phydev->duplex ?
297                         "FULL" : "HALF");
298 }
299
300 /* phy_ethtool_sset:
301  * A generic ethtool sset function.  Handles all the details
302  *
303  * A few notes about parameter checking:
304  * - We don't set port or transceiver, so we don't care what they
305  *   were set to.
306  * - phy_start_aneg() will make sure forced settings are sane, and
307  *   choose the next best ones from the ones selected, so we don't
308  *   care if ethtool tries to give us bad values
309  */
310 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
311 {
312         if (cmd->phy_address != phydev->addr)
313                 return -EINVAL;
314
315         /* We make sure that we don't pass unsupported
316          * values in to the PHY */
317         cmd->advertising &= phydev->supported;
318
319         /* Verify the settings we care about. */
320         if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
321                 return -EINVAL;
322
323         if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
324                 return -EINVAL;
325
326         if (cmd->autoneg == AUTONEG_DISABLE
327                         && ((cmd->speed != SPEED_1000
328                                         && cmd->speed != SPEED_100
329                                         && cmd->speed != SPEED_10)
330                                 || (cmd->duplex != DUPLEX_HALF
331                                         && cmd->duplex != DUPLEX_FULL)))
332                 return -EINVAL;
333
334         phydev->autoneg = cmd->autoneg;
335
336         phydev->speed = cmd->speed;
337
338         phydev->advertising = cmd->advertising;
339
340         if (AUTONEG_ENABLE == cmd->autoneg)
341                 phydev->advertising |= ADVERTISED_Autoneg;
342         else
343                 phydev->advertising &= ~ADVERTISED_Autoneg;
344
345         phydev->duplex = cmd->duplex;
346
347         /* Restart the PHY */
348         phy_start_aneg(phydev);
349
350         return 0;
351 }
352
353 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
354 {
355         cmd->supported = phydev->supported;
356
357         cmd->advertising = phydev->advertising;
358
359         cmd->speed = phydev->speed;
360         cmd->duplex = phydev->duplex;
361         cmd->port = PORT_MII;
362         cmd->phy_address = phydev->addr;
363         cmd->transceiver = XCVR_EXTERNAL;
364         cmd->autoneg = phydev->autoneg;
365
366         return 0;
367 }
368
369
370 /* Note that this function is currently incompatible with the
371  * PHYCONTROL layer.  It changes registers without regard to
372  * current state.  Use at own risk
373  */
374 int phy_mii_ioctl(struct phy_device *phydev,
375                 struct mii_ioctl_data *mii_data, int cmd)
376 {
377         u16 val = mii_data->val_in;
378
379         switch (cmd) {
380         case SIOCGMIIPHY:
381                 mii_data->phy_id = phydev->addr;
382                 break;
383         case SIOCGMIIREG:
384                 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
385                 break;
386
387         case SIOCSMIIREG:
388                 if (!capable(CAP_NET_ADMIN))
389                         return -EPERM;
390
391                 if (mii_data->phy_id == phydev->addr) {
392                         switch(mii_data->reg_num) {
393                         case MII_BMCR:
394                                 if (val & (BMCR_RESET|BMCR_ANENABLE))
395                                         phydev->autoneg = AUTONEG_DISABLE;
396                                 else
397                                         phydev->autoneg = AUTONEG_ENABLE;
398                                 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
399                                         phydev->duplex = DUPLEX_FULL;
400                                 else
401                                         phydev->duplex = DUPLEX_HALF;
402                                 break;
403                         case MII_ADVERTISE:
404                                 phydev->advertising = val;
405                                 break;
406                         default:
407                                 /* do nothing */
408                                 break;
409                         }
410                 }
411
412                 phy_write(phydev, mii_data->reg_num, val);
413                 
414                 if (mii_data->reg_num == MII_BMCR 
415                                 && val & BMCR_RESET
416                                 && phydev->drv->config_init)
417                         phydev->drv->config_init(phydev);
418                 break;
419         }
420
421         return 0;
422 }
423
424 /* phy_start_machine:
425  *
426  * description: The PHY infrastructure can run a state machine
427  *   which tracks whether the PHY is starting up, negotiating,
428  *   etc.  This function starts the timer which tracks the state
429  *   of the PHY.  If you want to be notified when the state
430  *   changes, pass in the callback, otherwise, pass NULL.  If you
431  *   want to maintain your own state machine, do not call this
432  *   function. */
433 void phy_start_machine(struct phy_device *phydev,
434                 void (*handler)(struct net_device *))
435 {
436         phydev->adjust_state = handler;
437
438         init_timer(&phydev->phy_timer);
439         phydev->phy_timer.function = &phy_timer;
440         phydev->phy_timer.data = (unsigned long) phydev;
441         mod_timer(&phydev->phy_timer, jiffies + HZ);
442 }
443
444 /* phy_stop_machine
445  *
446  * description: Stops the state machine timer, sets the state to
447  *   UP (unless it wasn't up yet), and then frees the interrupt,
448  *   if it is in use. This function must be called BEFORE
449  *   phy_detach.
450  */
451 void phy_stop_machine(struct phy_device *phydev)
452 {
453         del_timer_sync(&phydev->phy_timer);
454
455         spin_lock(&phydev->lock);
456         if (phydev->state > PHY_UP)
457                 phydev->state = PHY_UP;
458         spin_unlock(&phydev->lock);
459
460         if (phydev->irq != PHY_POLL)
461                 phy_stop_interrupts(phydev);
462
463         phydev->adjust_state = NULL;
464 }
465
466 /* phy_error:
467  *
468  * Moves the PHY to the HALTED state in response to a read
469  * or write error, and tells the controller the link is down.
470  * Must not be called from interrupt context, or while the
471  * phydev->lock is held.
472  */
473 void phy_error(struct phy_device *phydev)
474 {
475         spin_lock(&phydev->lock);
476         phydev->state = PHY_HALTED;
477         spin_unlock(&phydev->lock);
478 }
479
480 #ifdef CONFIG_PHYCONTROL
481
482 static void phy_change(void *data);
483
484 /* phy_interrupt
485  *
486  * description: When a PHY interrupt occurs, the handler disables
487  * interrupts, and schedules a work task to clear the interrupt.
488  */
489 static irqreturn_t phy_interrupt(int irq, void *phy_dat, struct pt_regs *regs)
490 {
491         struct phy_device *phydev = phy_dat;
492
493         /* The MDIO bus is not allowed to be written in interrupt
494          * context, so we need to disable the irq here.  A work
495          * queue will write the PHY to disable and clear the
496          * interrupt, and then reenable the irq line. */
497         disable_irq_nosync(irq);
498
499         schedule_work(&phydev->phy_queue);
500
501         return IRQ_HANDLED;
502 }
503
504 /* Enable the interrupts from the PHY side */
505 int phy_enable_interrupts(struct phy_device *phydev)
506 {
507         int err;
508
509         err = phy_clear_interrupt(phydev);
510
511         if (err < 0)
512                 return err;
513
514         err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
515
516         return err;
517 }
518 EXPORT_SYMBOL(phy_enable_interrupts);
519
520 /* Disable the PHY interrupts from the PHY side */
521 int phy_disable_interrupts(struct phy_device *phydev)
522 {
523         int err;
524
525         /* Disable PHY interrupts */
526         err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
527
528         if (err)
529                 goto phy_err;
530
531         /* Clear the interrupt */
532         err = phy_clear_interrupt(phydev);
533
534         if (err)
535                 goto phy_err;
536
537         return 0;
538
539 phy_err:
540         phy_error(phydev);
541
542         return err;
543 }
544 EXPORT_SYMBOL(phy_disable_interrupts);
545
546 /* phy_start_interrupts
547  *
548  * description: Request the interrupt for the given PHY.  If
549  *   this fails, then we set irq to PHY_POLL.
550  *   Otherwise, we enable the interrupts in the PHY.
551  *   Returns 0 on success.
552  *   This should only be called with a valid IRQ number.
553  */
554 int phy_start_interrupts(struct phy_device *phydev)
555 {
556         int err = 0;
557
558         INIT_WORK(&phydev->phy_queue, phy_change, phydev);
559
560         if (request_irq(phydev->irq, phy_interrupt,
561                                 SA_SHIRQ,
562                                 "phy_interrupt",
563                                 phydev) < 0) {
564                 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
565                                 phydev->bus->name,
566                                 phydev->irq);
567                 phydev->irq = PHY_POLL;
568                 return 0;
569         }
570
571         err = phy_enable_interrupts(phydev);
572
573         return err;
574 }
575 EXPORT_SYMBOL(phy_start_interrupts);
576
577 int phy_stop_interrupts(struct phy_device *phydev)
578 {
579         int err;
580
581         err = phy_disable_interrupts(phydev);
582
583         if (err)
584                 phy_error(phydev);
585
586         free_irq(phydev->irq, phydev);
587
588         return err;
589 }
590 EXPORT_SYMBOL(phy_stop_interrupts);
591
592
593 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
594 static void phy_change(void *data)
595 {
596         int err;
597         struct phy_device *phydev = data;
598
599         err = phy_disable_interrupts(phydev);
600
601         if (err)
602                 goto phy_err;
603
604         spin_lock(&phydev->lock);
605         if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
606                 phydev->state = PHY_CHANGELINK;
607         spin_unlock(&phydev->lock);
608
609         enable_irq(phydev->irq);
610
611         /* Reenable interrupts */
612         err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
613
614         if (err)
615                 goto irq_enable_err;
616
617         return;
618
619 irq_enable_err:
620         disable_irq(phydev->irq);
621 phy_err:
622         phy_error(phydev);
623 }
624
625 /* Bring down the PHY link, and stop checking the status. */
626 void phy_stop(struct phy_device *phydev)
627 {
628         spin_lock(&phydev->lock);
629
630         if (PHY_HALTED == phydev->state)
631                 goto out_unlock;
632
633         if (phydev->irq != PHY_POLL) {
634                 /* Clear any pending interrupts */
635                 phy_clear_interrupt(phydev);
636
637                 /* Disable PHY Interrupts */
638                 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
639         }
640
641         phydev->state = PHY_HALTED;
642
643 out_unlock:
644         spin_unlock(&phydev->lock);
645 }
646
647
648 /* phy_start
649  *
650  * description: Indicates the attached device's readiness to
651  *   handle PHY-related work.  Used during startup to start the
652  *   PHY, and after a call to phy_stop() to resume operation.
653  *   Also used to indicate the MDIO bus has cleared an error
654  *   condition.
655  */
656 void phy_start(struct phy_device *phydev)
657 {
658         spin_lock(&phydev->lock);
659
660         switch (phydev->state) {
661                 case PHY_STARTING:
662                         phydev->state = PHY_PENDING;
663                         break;
664                 case PHY_READY:
665                         phydev->state = PHY_UP;
666                         break;
667                 case PHY_HALTED:
668                         phydev->state = PHY_RESUMING;
669                 default:
670                         break;
671         }
672         spin_unlock(&phydev->lock);
673 }
674 EXPORT_SYMBOL(phy_stop);
675 EXPORT_SYMBOL(phy_start);
676
677 #endif /* CONFIG_PHYCONTROL */
678
679 /* PHY timer which handles the state machine */
680 static void phy_timer(unsigned long data)
681 {
682         struct phy_device *phydev = (struct phy_device *)data;
683         int needs_aneg = 0;
684         int err = 0;
685
686         spin_lock(&phydev->lock);
687
688         if (phydev->adjust_state)
689                 phydev->adjust_state(phydev->attached_dev);
690
691         switch(phydev->state) {
692                 case PHY_DOWN:
693                 case PHY_STARTING:
694                 case PHY_READY:
695                 case PHY_PENDING:
696                         break;
697                 case PHY_UP:
698                         needs_aneg = 1;
699
700                         phydev->link_timeout = PHY_AN_TIMEOUT;
701
702                         break;
703                 case PHY_AN:
704                         /* Check if negotiation is done.  Break
705                          * if there's an error */
706                         err = phy_aneg_done(phydev);
707                         if (err < 0)
708                                 break;
709
710                         /* If auto-negotiation is done, we change to
711                          * either RUNNING, or NOLINK */
712                         if (err > 0) {
713                                 err = phy_read_status(phydev);
714
715                                 if (err)
716                                         break;
717
718                                 if (phydev->link) {
719                                         phydev->state = PHY_RUNNING;
720                                         netif_carrier_on(phydev->attached_dev);
721                                 } else {
722                                         phydev->state = PHY_NOLINK;
723                                         netif_carrier_off(phydev->attached_dev);
724                                 }
725
726                                 phydev->adjust_link(phydev->attached_dev);
727
728                         } else if (0 == phydev->link_timeout--) {
729                                 /* The counter expired, so either we
730                                  * switch to forced mode, or the
731                                  * magic_aneg bit exists, and we try aneg
732                                  * again */
733                                 if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
734                                         int idx;
735
736                                         /* We'll start from the
737                                          * fastest speed, and work
738                                          * our way down */
739                                         idx = phy_find_valid(0,
740                                                         phydev->supported);
741
742                                         phydev->speed = settings[idx].speed;
743                                         phydev->duplex = settings[idx].duplex;
744                                         
745                                         phydev->autoneg = AUTONEG_DISABLE;
746                                         phydev->state = PHY_FORCING;
747                                         phydev->link_timeout =
748                                                 PHY_FORCE_TIMEOUT;
749
750                                         pr_info("Trying %d/%s\n",
751                                                         phydev->speed,
752                                                         DUPLEX_FULL ==
753                                                         phydev->duplex ?
754                                                         "FULL" : "HALF");
755                                 }
756
757                                 needs_aneg = 1;
758                         }
759                         break;
760                 case PHY_NOLINK:
761                         err = phy_read_status(phydev);
762
763                         if (err)
764                                 break;
765
766                         if (phydev->link) {
767                                 phydev->state = PHY_RUNNING;
768                                 netif_carrier_on(phydev->attached_dev);
769                                 phydev->adjust_link(phydev->attached_dev);
770                         }
771                         break;
772                 case PHY_FORCING:
773                         err = phy_read_status(phydev);
774
775                         if (err)
776                                 break;
777
778                         if (phydev->link) {
779                                 phydev->state = PHY_RUNNING;
780                                 netif_carrier_on(phydev->attached_dev);
781                         } else {
782                                 if (0 == phydev->link_timeout--) {
783                                         phy_force_reduction(phydev);
784                                         needs_aneg = 1;
785                                 }
786                         }
787
788                         phydev->adjust_link(phydev->attached_dev);
789                         break;
790                 case PHY_RUNNING:
791                         /* Only register a CHANGE if we are
792                          * polling */
793                         if (PHY_POLL == phydev->irq)
794                                 phydev->state = PHY_CHANGELINK;
795                         break;
796                 case PHY_CHANGELINK:
797                         err = phy_read_status(phydev);
798
799                         if (err)
800                                 break;
801
802                         if (phydev->link) {
803                                 phydev->state = PHY_RUNNING;
804                                 netif_carrier_on(phydev->attached_dev);
805                         } else {
806                                 phydev->state = PHY_NOLINK;
807                                 netif_carrier_off(phydev->attached_dev);
808                         }
809
810                         phydev->adjust_link(phydev->attached_dev);
811
812                         if (PHY_POLL != phydev->irq)
813                                 err = phy_config_interrupt(phydev,
814                                                 PHY_INTERRUPT_ENABLED);
815                         break;
816                 case PHY_HALTED:
817                         if (phydev->link) {
818                                 phydev->link = 0;
819                                 netif_carrier_off(phydev->attached_dev);
820                                 phydev->adjust_link(phydev->attached_dev);
821                         }
822                         break;
823                 case PHY_RESUMING:
824
825                         err = phy_clear_interrupt(phydev);
826
827                         if (err)
828                                 break;
829
830                         err = phy_config_interrupt(phydev,
831                                         PHY_INTERRUPT_ENABLED);
832
833                         if (err)
834                                 break;
835
836                         if (AUTONEG_ENABLE == phydev->autoneg) {
837                                 err = phy_aneg_done(phydev);
838                                 if (err < 0)
839                                         break;
840
841                                 /* err > 0 if AN is done.
842                                  * Otherwise, it's 0, and we're
843                                  * still waiting for AN */
844                                 if (err > 0) {
845                                         phydev->state = PHY_RUNNING;
846                                 } else {
847                                         phydev->state = PHY_AN;
848                                         phydev->link_timeout = PHY_AN_TIMEOUT;
849                                 }
850                         } else
851                                 phydev->state = PHY_RUNNING;
852                         break;
853         }
854
855         spin_unlock(&phydev->lock);
856
857         if (needs_aneg)
858                 err = phy_start_aneg(phydev);
859
860         if (err < 0)
861                 phy_error(phydev);
862
863         mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
864 }
865