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