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