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