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