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