1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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#include <linux/mdio.h>
36#include <linux/io.h>
37#include <linux/uaccess.h>
38#include <linux/atomic.h>
39
40#include <asm/irq.h>
41
42
43
44
45
46void phy_print_status(struct phy_device *phydev)
47{
48 if (phydev->link) {
49 pr_info("%s - Link is Up - %d/%s\n",
50 dev_name(&phydev->mdio_dev),
51 phydev->speed,
52 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
53 } else {
54 pr_info("%s - Link is Down\n", dev_name(&phydev->mdio_dev));
55 }
56}
57EXPORT_SYMBOL(phy_print_status);
58
59
60
61
62
63
64
65
66
67
68static int phy_clear_interrupt(struct phy_device *phydev)
69{
70 if (phydev->drv->ack_interrupt)
71 return phydev->drv->ack_interrupt(phydev);
72
73 return 0;
74}
75
76
77
78
79
80
81
82
83static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
84{
85 phydev->interrupts = interrupts;
86 if (phydev->drv->config_intr)
87 return phydev->drv->config_intr(phydev);
88
89 return 0;
90}
91
92
93
94
95
96
97
98
99int phy_restart_aneg(struct phy_device *phydev)
100{
101 int ret;
102
103 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
104 ret = genphy_c45_restart_aneg(phydev);
105 else
106 ret = genphy_restart_aneg(phydev);
107
108 return ret;
109}
110EXPORT_SYMBOL_GPL(phy_restart_aneg);
111
112
113
114
115
116
117
118
119
120int phy_aneg_done(struct phy_device *phydev)
121{
122 if (phydev->drv && phydev->drv->aneg_done)
123 return phydev->drv->aneg_done(phydev);
124
125
126
127
128 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
129 return -EINVAL;
130
131 return genphy_aneg_done(phydev);
132}
133EXPORT_SYMBOL(phy_aneg_done);
134
135
136
137
138struct phy_setting {
139 int speed;
140 int duplex;
141 u32 setting;
142};
143
144
145static const struct phy_setting settings[] = {
146 {
147 .speed = 10000,
148 .duplex = DUPLEX_FULL,
149 .setting = SUPPORTED_10000baseT_Full,
150 },
151 {
152 .speed = SPEED_1000,
153 .duplex = DUPLEX_FULL,
154 .setting = SUPPORTED_1000baseT_Full,
155 },
156 {
157 .speed = SPEED_1000,
158 .duplex = DUPLEX_HALF,
159 .setting = SUPPORTED_1000baseT_Half,
160 },
161 {
162 .speed = SPEED_100,
163 .duplex = DUPLEX_FULL,
164 .setting = SUPPORTED_100baseT_Full,
165 },
166 {
167 .speed = SPEED_100,
168 .duplex = DUPLEX_HALF,
169 .setting = SUPPORTED_100baseT_Half,
170 },
171 {
172 .speed = SPEED_10,
173 .duplex = DUPLEX_FULL,
174 .setting = SUPPORTED_10baseT_Full,
175 },
176 {
177 .speed = SPEED_10,
178 .duplex = DUPLEX_HALF,
179 .setting = SUPPORTED_10baseT_Half,
180 },
181};
182
183#define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
184
185
186
187
188
189
190
191
192
193
194
195static inline int phy_find_setting(int speed, int duplex)
196{
197 int idx = 0;
198
199 while (idx < ARRAY_SIZE(settings) &&
200 (settings[idx].speed != speed || settings[idx].duplex != duplex))
201 idx++;
202
203 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
204}
205
206
207
208
209
210
211
212
213
214
215
216static inline int phy_find_valid(int idx, u32 features)
217{
218 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
219 idx++;
220
221 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
222}
223
224
225
226
227
228
229
230
231
232static void phy_sanitize_settings(struct phy_device *phydev)
233{
234 u32 features = phydev->supported;
235 int idx;
236
237
238 if ((features & SUPPORTED_Autoneg) == 0)
239 phydev->autoneg = AUTONEG_DISABLE;
240
241 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
242 features);
243
244 phydev->speed = settings[idx].speed;
245 phydev->duplex = settings[idx].duplex;
246}
247
248
249
250
251
252
253
254
255
256
257
258
259
260int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
261{
262 u32 speed = ethtool_cmd_speed(cmd);
263
264 if (cmd->phy_address != phydev->mdio_addr)
265 return -EINVAL;
266
267
268 cmd->advertising &= phydev->supported;
269
270
271 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
272 return -EINVAL;
273
274 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
275 return -EINVAL;
276
277 if (cmd->autoneg == AUTONEG_DISABLE &&
278 ((speed != SPEED_1000 &&
279 speed != SPEED_100 &&
280 speed != SPEED_10) ||
281 (cmd->duplex != DUPLEX_HALF &&
282 cmd->duplex != DUPLEX_FULL)))
283 return -EINVAL;
284
285 phydev->autoneg = cmd->autoneg;
286
287 phydev->speed = speed;
288
289 phydev->advertising = cmd->advertising;
290
291 if (AUTONEG_ENABLE == cmd->autoneg)
292 phydev->advertising |= ADVERTISED_Autoneg;
293 else
294 phydev->advertising &= ~ADVERTISED_Autoneg;
295
296 phydev->duplex = cmd->duplex;
297
298 phydev->mdix = cmd->eth_tp_mdix_ctrl;
299
300
301 phy_start_aneg(phydev);
302
303 return 0;
304}
305EXPORT_SYMBOL(phy_ethtool_sset);
306
307int phy_ethtool_ksettings_set(struct phy_device *phydev,
308 const struct ethtool_link_ksettings *cmd)
309{
310 u8 autoneg = cmd->base.autoneg;
311 u8 duplex = cmd->base.duplex;
312 u32 speed = cmd->base.speed;
313 u32 advertising;
314
315 if (cmd->base.phy_address != phydev->mdio_addr)
316 return -EINVAL;
317
318 ethtool_convert_link_mode_to_legacy_u32(&advertising,
319 cmd->link_modes.advertising);
320
321
322 advertising &= phydev->supported;
323
324
325 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
326 return -EINVAL;
327
328 if (autoneg == AUTONEG_ENABLE && advertising == 0)
329 return -EINVAL;
330
331 if (autoneg == AUTONEG_DISABLE &&
332 ((speed != SPEED_1000 &&
333 speed != SPEED_100 &&
334 speed != SPEED_10) ||
335 (duplex != DUPLEX_HALF &&
336 duplex != DUPLEX_FULL)))
337 return -EINVAL;
338
339 phydev->autoneg = autoneg;
340
341 phydev->speed = speed;
342
343 phydev->advertising = advertising;
344
345 if (autoneg == AUTONEG_ENABLE)
346 phydev->advertising |= ADVERTISED_Autoneg;
347 else
348 phydev->advertising &= ~ADVERTISED_Autoneg;
349
350 phydev->duplex = duplex;
351
352 phydev->mdix = cmd->base.eth_tp_mdix_ctrl;
353
354
355 phy_start_aneg(phydev);
356
357 return 0;
358}
359EXPORT_SYMBOL(phy_ethtool_ksettings_set);
360
361int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
362{
363 cmd->supported = phydev->supported;
364
365 cmd->advertising = phydev->advertising;
366 cmd->lp_advertising = phydev->lp_advertising;
367
368 ethtool_cmd_speed_set(cmd, phydev->speed);
369 cmd->duplex = phydev->duplex;
370 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
371 cmd->port = PORT_BNC;
372 else
373 cmd->port = PORT_MII;
374 cmd->phy_address = phydev->mdio_addr;
375 cmd->transceiver = phy_is_internal(phydev) ?
376 XCVR_INTERNAL : XCVR_EXTERNAL;
377 cmd->autoneg = phydev->autoneg;
378 cmd->eth_tp_mdix_ctrl = phydev->mdix;
379
380 return 0;
381}
382EXPORT_SYMBOL(phy_ethtool_gset);
383
384int phy_ethtool_ksettings_get(struct phy_device *phydev,
385 struct ethtool_link_ksettings *cmd)
386{
387 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
388 phydev->supported);
389
390 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
391 phydev->advertising);
392
393 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
394 phydev->lp_advertising);
395
396 cmd->base.speed = phydev->speed;
397 cmd->base.duplex = phydev->duplex;
398 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
399 cmd->base.port = PORT_BNC;
400 else
401 cmd->base.port = PORT_MII;
402 cmd->base.transceiver = phy_is_internal(phydev) ?
403 XCVR_INTERNAL : XCVR_EXTERNAL;
404 cmd->base.phy_address = phydev->mdio_addr;
405 cmd->base.autoneg = phydev->autoneg;
406 cmd->base.eth_tp_mdix_ctrl = phydev->mdix;
407
408 return 0;
409}
410EXPORT_SYMBOL(phy_ethtool_ksettings_get);
411
412
413
414
415
416
417
418
419
420
421
422int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
423{
424 struct mii_ioctl_data *mii_data = if_mii(ifr);
425 u16 val = mii_data->val_in;
426
427 switch (cmd) {
428 case SIOCGMIIPHY:
429 mii_data->phy_id = phydev->mdio_addr;
430
431
432 case SIOCGMIIREG:
433 mii_data->val_out = mdiobus_read(phydev->mdio_bus,
434 mii_data->phy_id,
435 mii_data->reg_num);
436 return 0;
437
438 case SIOCSMIIREG:
439 if (mii_data->phy_id == phydev->mdio_addr) {
440 switch (mii_data->reg_num) {
441 case MII_BMCR:
442 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0)
443 phydev->autoneg = AUTONEG_DISABLE;
444 else
445 phydev->autoneg = AUTONEG_ENABLE;
446 if (!phydev->autoneg && (val & BMCR_FULLDPLX))
447 phydev->duplex = DUPLEX_FULL;
448 else
449 phydev->duplex = DUPLEX_HALF;
450 if (!phydev->autoneg && (val & BMCR_SPEED1000))
451 phydev->speed = SPEED_1000;
452 else if (!phydev->autoneg &&
453 (val & BMCR_SPEED100))
454 phydev->speed = SPEED_100;
455 break;
456 case MII_ADVERTISE:
457 phydev->advertising = val;
458 break;
459 default:
460
461 break;
462 }
463 }
464
465 mdiobus_write(phydev->mdio_bus, mii_data->phy_id,
466 mii_data->reg_num, val);
467
468 if (mii_data->reg_num == MII_BMCR &&
469 val & BMCR_RESET)
470 return phy_init_hw(phydev);
471 return 0;
472
473 case SIOCSHWTSTAMP:
474 if (phydev->drv && phydev->drv->hwtstamp)
475 return phydev->drv->hwtstamp(phydev, ifr);
476
477
478 default:
479 return -EOPNOTSUPP;
480 }
481}
482EXPORT_SYMBOL(phy_mii_ioctl);
483
484static int phy_config_aneg(struct phy_device *phydev)
485{
486 if (phydev->drv->config_aneg)
487 return phydev->drv->config_aneg(phydev);
488 else
489 return genphy_config_aneg(phydev);
490}
491
492
493
494
495
496
497
498
499
500
501int phy_start_aneg(struct phy_device *phydev)
502{
503 int err;
504
505 if (!phydev->drv)
506 return -EIO;
507
508 mutex_lock(&phydev->lock);
509
510 if (AUTONEG_DISABLE == phydev->autoneg)
511 phy_sanitize_settings(phydev);
512
513
514 phydev->lp_advertising = 0;
515
516 err = phy_config_aneg(phydev);
517 if (err < 0)
518 goto out_unlock;
519
520 if (phydev->state != PHY_HALTED) {
521 if (AUTONEG_ENABLE == phydev->autoneg) {
522 phydev->state = PHY_AN;
523 phydev->link_timeout = PHY_AN_TIMEOUT;
524 } else {
525 phydev->state = PHY_FORCING;
526 phydev->link_timeout = PHY_FORCE_TIMEOUT;
527 }
528 }
529
530out_unlock:
531 mutex_unlock(&phydev->lock);
532 return err;
533}
534EXPORT_SYMBOL(phy_start_aneg);
535
536static int phy_poll_aneg_done(struct phy_device *phydev)
537{
538 unsigned int retries = 100;
539 int ret;
540
541 do {
542 msleep(100);
543 ret = phy_aneg_done(phydev);
544 } while (!ret && --retries);
545
546 if (!ret)
547 return -ETIMEDOUT;
548
549 return ret < 0 ? ret : 0;
550}
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565int phy_speed_down(struct phy_device *phydev, bool sync)
566{
567 u32 adv = phydev->lp_advertising & phydev->supported;
568 u32 adv_old = phydev->advertising;
569 int ret;
570
571 if (phydev->autoneg != AUTONEG_ENABLE)
572 return 0;
573
574 if (adv & PHY_10BT_FEATURES)
575 phydev->advertising &= ~(PHY_100BT_FEATURES |
576 PHY_1000BT_FEATURES);
577 else if (adv & PHY_100BT_FEATURES)
578 phydev->advertising &= ~PHY_1000BT_FEATURES;
579
580 if (phydev->advertising == adv_old)
581 return 0;
582
583 ret = phy_config_aneg(phydev);
584 if (ret)
585 return ret;
586
587 return sync ? phy_poll_aneg_done(phydev) : 0;
588}
589EXPORT_SYMBOL_GPL(phy_speed_down);
590
591
592
593
594
595
596
597int phy_speed_up(struct phy_device *phydev)
598{
599 u32 mask = PHY_10BT_FEATURES | PHY_100BT_FEATURES | PHY_1000BT_FEATURES;
600 u32 adv_old = phydev->advertising;
601
602 if (phydev->autoneg != AUTONEG_ENABLE)
603 return 0;
604
605 phydev->advertising = (adv_old & ~mask) | (phydev->supported & mask);
606
607 if (phydev->advertising == adv_old)
608 return 0;
609
610 return phy_config_aneg(phydev);
611}
612EXPORT_SYMBOL_GPL(phy_speed_up);
613
614
615
616
617
618
619
620
621
622
623
624void phy_start_machine(struct phy_device *phydev)
625{
626 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
627}
628
629
630
631
632
633
634
635
636
637
638
639static void phy_trigger_machine(struct phy_device *phydev, bool sync)
640{
641 if (sync)
642 cancel_delayed_work_sync(&phydev->state_queue);
643 else
644 cancel_delayed_work(&phydev->state_queue);
645 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
646}
647
648
649
650
651
652
653
654
655
656void phy_stop_machine(struct phy_device *phydev)
657{
658 cancel_delayed_work_sync(&phydev->state_queue);
659
660 mutex_lock(&phydev->lock);
661 if (phydev->state > PHY_UP)
662 phydev->state = PHY_UP;
663 mutex_unlock(&phydev->lock);
664}
665
666
667
668
669
670
671
672
673
674
675static void phy_error(struct phy_device *phydev)
676{
677 mutex_lock(&phydev->lock);
678 phydev->state = PHY_HALTED;
679 mutex_unlock(&phydev->lock);
680
681 phy_trigger_machine(phydev, false);
682}
683
684
685
686
687
688
689
690
691
692static irqreturn_t phy_interrupt(int irq, void *phy_dat)
693{
694 struct phy_device *phydev = phy_dat;
695
696 if (PHY_HALTED == phydev->state)
697 return IRQ_NONE;
698
699
700
701
702
703
704 disable_irq_nosync(irq);
705 atomic_inc(&phydev->irq_disable);
706
707 queue_work(system_power_efficient_wq, &phydev->phy_queue);
708
709 return IRQ_HANDLED;
710}
711
712
713
714
715
716static int phy_enable_interrupts(struct phy_device *phydev)
717{
718 int err = phy_clear_interrupt(phydev);
719
720 if (err < 0)
721 return err;
722
723 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
724}
725
726
727
728
729
730static int phy_disable_interrupts(struct phy_device *phydev)
731{
732 int err;
733
734
735 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
736 if (err)
737 goto phy_err;
738
739
740 err = phy_clear_interrupt(phydev);
741 if (err)
742 goto phy_err;
743
744 return 0;
745
746phy_err:
747 phy_error(phydev);
748
749 return err;
750}
751
752
753
754
755
756
757
758
759
760
761
762int phy_start_interrupts(struct phy_device *phydev)
763{
764 atomic_set(&phydev->irq_disable, 0);
765 if (request_irq(phydev->irq, phy_interrupt,
766 IRQF_SHARED,
767 "phy_interrupt",
768 phydev) < 0) {
769 pr_warn("%s: Can't get IRQ %d (PHY)\n",
770 phydev->mdio_bus->name, phydev->irq);
771 phydev->irq = PHY_POLL;
772 return 0;
773 }
774
775 return phy_enable_interrupts(phydev);
776}
777EXPORT_SYMBOL(phy_start_interrupts);
778
779
780
781
782
783int phy_stop_interrupts(struct phy_device *phydev)
784{
785 int err = phy_disable_interrupts(phydev);
786
787 if (err)
788 phy_error(phydev);
789
790 free_irq(phydev->irq, phydev);
791
792
793
794
795
796
797 cancel_work_sync(&phydev->phy_queue);
798
799
800
801
802 while (atomic_dec_return(&phydev->irq_disable) >= 0)
803 enable_irq(phydev->irq);
804
805 return err;
806}
807EXPORT_SYMBOL(phy_stop_interrupts);
808
809
810
811
812
813void phy_change(struct work_struct *work)
814{
815 struct phy_device *phydev =
816 container_of(work, struct phy_device, phy_queue);
817
818 if (phy_interrupt_is_valid(phydev)) {
819 if (phydev->drv->did_interrupt &&
820 !phydev->drv->did_interrupt(phydev))
821 goto ignore;
822
823 if (phy_disable_interrupts(phydev))
824 goto phy_err;
825 }
826
827 mutex_lock(&phydev->lock);
828 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
829 phydev->state = PHY_CHANGELINK;
830 mutex_unlock(&phydev->lock);
831
832 if (phy_interrupt_is_valid(phydev)) {
833 atomic_dec(&phydev->irq_disable);
834 enable_irq(phydev->irq);
835
836
837 if (PHY_HALTED != phydev->state &&
838 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
839 goto irq_enable_err;
840 }
841
842
843 phy_trigger_machine(phydev, true);
844 return;
845
846ignore:
847 atomic_dec(&phydev->irq_disable);
848 enable_irq(phydev->irq);
849 return;
850
851irq_enable_err:
852 disable_irq(phydev->irq);
853 atomic_inc(&phydev->irq_disable);
854phy_err:
855 phy_error(phydev);
856}
857
858
859
860
861
862void phy_stop(struct phy_device *phydev)
863{
864 mutex_lock(&phydev->lock);
865
866 if (PHY_HALTED == phydev->state)
867 goto out_unlock;
868
869 if (phy_interrupt_is_valid(phydev)) {
870
871 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
872
873
874 phy_clear_interrupt(phydev);
875 }
876
877 phydev->state = PHY_HALTED;
878
879out_unlock:
880 mutex_unlock(&phydev->lock);
881
882
883
884
885
886}
887EXPORT_SYMBOL(phy_stop);
888
889
890
891
892
893
894
895
896
897
898
899void phy_start(struct phy_device *phydev)
900{
901 int err = 0;
902
903 mutex_lock(&phydev->lock);
904
905 switch (phydev->state) {
906 case PHY_STARTING:
907 phydev->state = PHY_PENDING;
908 break;
909 case PHY_READY:
910 phydev->state = PHY_UP;
911 break;
912 case PHY_HALTED:
913
914 __phy_resume(phydev);
915
916
917 err = phy_enable_interrupts(phydev);
918 if (err < 0)
919 break;
920
921 phydev->state = PHY_RESUMING;
922 break;
923 default:
924 break;
925 }
926 mutex_unlock(&phydev->lock);
927
928 phy_trigger_machine(phydev, true);
929}
930EXPORT_SYMBOL(phy_start);
931
932
933
934
935
936void phy_state_machine(struct work_struct *work)
937{
938 struct delayed_work *dwork = to_delayed_work(work);
939 struct phy_device *phydev =
940 container_of(dwork, struct phy_device, state_queue);
941 bool needs_aneg = false, do_suspend = false;
942 int err = 0;
943 int old_link;
944
945 mutex_lock(&phydev->lock);
946
947 switch (phydev->state) {
948 case PHY_DOWN:
949 case PHY_STARTING:
950 case PHY_READY:
951 case PHY_PENDING:
952 break;
953 case PHY_UP:
954 needs_aneg = true;
955
956 phydev->link_timeout = PHY_AN_TIMEOUT;
957
958 break;
959 case PHY_AN:
960 err = phy_read_status(phydev);
961 if (err < 0)
962 break;
963
964
965 if (!phydev->link) {
966 phydev->state = PHY_NOLINK;
967 netif_carrier_off(phydev->attached_dev);
968 phydev->adjust_link(phydev->attached_dev);
969 break;
970 }
971
972
973 err = phy_aneg_done(phydev);
974 if (err < 0)
975 break;
976
977
978 if (err > 0) {
979 phydev->state = PHY_RUNNING;
980 netif_carrier_on(phydev->attached_dev);
981 phydev->adjust_link(phydev->attached_dev);
982
983 } else if (0 == phydev->link_timeout--)
984 needs_aneg = true;
985 break;
986 case PHY_NOLINK:
987 err = phy_read_status(phydev);
988 if (err)
989 break;
990
991 if (phydev->link) {
992 if (AUTONEG_ENABLE == phydev->autoneg) {
993 err = phy_aneg_done(phydev);
994 if (err < 0)
995 break;
996
997 if (!err) {
998 phydev->state = PHY_AN;
999 phydev->link_timeout = PHY_AN_TIMEOUT;
1000 break;
1001 }
1002 }
1003 phydev->state = PHY_RUNNING;
1004 netif_carrier_on(phydev->attached_dev);
1005 phydev->adjust_link(phydev->attached_dev);
1006 }
1007 break;
1008 case PHY_FORCING:
1009 err = genphy_update_link(phydev);
1010 if (err)
1011 break;
1012
1013 if (phydev->link) {
1014 phydev->state = PHY_RUNNING;
1015 netif_carrier_on(phydev->attached_dev);
1016 } else {
1017 if (0 == phydev->link_timeout--)
1018 needs_aneg = true;
1019 }
1020
1021 phydev->adjust_link(phydev->attached_dev);
1022 break;
1023 case PHY_RUNNING:
1024
1025
1026
1027 if (phydev->irq == PHY_POLL) {
1028 old_link = phydev->link;
1029 err = phy_read_status(phydev);
1030 if (err)
1031 break;
1032
1033 if (old_link != phydev->link)
1034 phydev->state = PHY_CHANGELINK;
1035 }
1036 break;
1037 case PHY_CHANGELINK:
1038 err = phy_read_status(phydev);
1039 if (err)
1040 break;
1041
1042 if (phydev->link) {
1043 phydev->state = PHY_RUNNING;
1044 netif_carrier_on(phydev->attached_dev);
1045 } else {
1046 phydev->state = PHY_NOLINK;
1047 netif_carrier_off(phydev->attached_dev);
1048 }
1049
1050 phydev->adjust_link(phydev->attached_dev);
1051
1052 if (phy_interrupt_is_valid(phydev))
1053 err = phy_config_interrupt(phydev,
1054 PHY_INTERRUPT_ENABLED);
1055 break;
1056 case PHY_HALTED:
1057 if (phydev->link) {
1058 phydev->link = 0;
1059 netif_carrier_off(phydev->attached_dev);
1060 phydev->adjust_link(phydev->attached_dev);
1061 do_suspend = true;
1062 }
1063 break;
1064 case PHY_RESUMING:
1065 if (AUTONEG_ENABLE == phydev->autoneg) {
1066 err = phy_aneg_done(phydev);
1067 if (err < 0)
1068 break;
1069
1070
1071
1072
1073 if (err > 0) {
1074 err = phy_read_status(phydev);
1075 if (err)
1076 break;
1077
1078 if (phydev->link) {
1079 phydev->state = PHY_RUNNING;
1080 netif_carrier_on(phydev->attached_dev);
1081 } else {
1082 phydev->state = PHY_NOLINK;
1083 }
1084 phydev->adjust_link(phydev->attached_dev);
1085 } else {
1086 phydev->state = PHY_AN;
1087 phydev->link_timeout = PHY_AN_TIMEOUT;
1088 }
1089 } else {
1090 err = phy_read_status(phydev);
1091 if (err)
1092 break;
1093
1094 if (phydev->link) {
1095 phydev->state = PHY_RUNNING;
1096 netif_carrier_on(phydev->attached_dev);
1097 } else {
1098 phydev->state = PHY_NOLINK;
1099 }
1100 phydev->adjust_link(phydev->attached_dev);
1101 }
1102 break;
1103 }
1104
1105 mutex_unlock(&phydev->lock);
1106
1107 if (needs_aneg)
1108 err = phy_start_aneg(phydev);
1109 else if (do_suspend)
1110 phy_suspend(phydev);
1111
1112 if (err < 0)
1113 phy_error(phydev);
1114
1115
1116
1117
1118
1119 if (phydev->irq == PHY_POLL)
1120 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1121 PHY_STATE_TIME * HZ);
1122}
1123
1124
1125
1126
1127
1128
1129
1130
1131void phy_mac_interrupt(struct phy_device *phydev)
1132{
1133
1134 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1135}
1136EXPORT_SYMBOL(phy_mac_interrupt);
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1149{
1150 if (!phydev->drv)
1151 return -EIO;
1152
1153
1154
1155
1156
1157
1158 if ((phydev->duplex == DUPLEX_FULL) &&
1159 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
1160 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
1161 (phydev->interface == PHY_INTERFACE_MODE_RGMII) ||
1162 phy_is_internal(phydev))) {
1163 int eee_lp, eee_cap, eee_adv;
1164 u32 lp, cap, adv;
1165 int idx, status;
1166
1167
1168 status = phy_read_status(phydev);
1169 if (status)
1170 return status;
1171
1172
1173 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1174 if (eee_cap <= 0)
1175 goto eee_exit_err;
1176
1177 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1178 if (!cap)
1179 goto eee_exit_err;
1180
1181
1182
1183
1184 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1185 if (eee_lp <= 0)
1186 goto eee_exit_err;
1187
1188 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1189 if (eee_adv <= 0)
1190 goto eee_exit_err;
1191
1192 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1193 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1194 idx = phy_find_setting(phydev->speed, phydev->duplex);
1195 if (!(lp & adv & settings[idx].setting))
1196 goto eee_exit_err;
1197
1198 if (clk_stop_enable)
1199
1200
1201
1202 phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1203 MDIO_PCS_CTRL1_CLKSTOP_EN);
1204
1205 return 0;
1206 }
1207eee_exit_err:
1208 return -EPROTONOSUPPORT;
1209}
1210EXPORT_SYMBOL(phy_init_eee);
1211
1212
1213
1214
1215
1216
1217
1218
1219int phy_get_eee_err(struct phy_device *phydev)
1220{
1221 if (!phydev->drv)
1222 return -EIO;
1223
1224 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1225}
1226EXPORT_SYMBOL(phy_get_eee_err);
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1237{
1238 int val;
1239
1240 if (!phydev->drv)
1241 return -EIO;
1242
1243
1244 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1245 if (val < 0)
1246 return val;
1247 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1248
1249
1250 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1251 if (val < 0)
1252 return val;
1253 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1254
1255
1256 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1257 if (val < 0)
1258 return val;
1259 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1260
1261 return 0;
1262}
1263EXPORT_SYMBOL(phy_ethtool_get_eee);
1264
1265
1266
1267
1268
1269
1270
1271
1272int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1273{
1274 int cap, old_adv, adv, ret;
1275
1276 if (!phydev->drv)
1277 return -EIO;
1278
1279
1280 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1281 if (cap < 0)
1282 return cap;
1283
1284 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1285 if (old_adv < 0)
1286 return old_adv;
1287
1288 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1289
1290
1291 adv &= ~phydev->eee_broken_modes;
1292
1293 if (old_adv != adv) {
1294 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1295 if (ret < 0)
1296 return ret;
1297
1298
1299
1300
1301 ret = phy_restart_aneg(phydev);
1302 if (ret < 0)
1303 return ret;
1304 }
1305
1306 return 0;
1307}
1308EXPORT_SYMBOL(phy_ethtool_set_eee);
1309
1310int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1311{
1312 if (phydev->drv && phydev->drv->set_wol)
1313 return phydev->drv->set_wol(phydev, wol);
1314
1315 return -EOPNOTSUPP;
1316}
1317EXPORT_SYMBOL(phy_ethtool_set_wol);
1318
1319void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1320{
1321 if (phydev->drv && phydev->drv->get_wol)
1322 phydev->drv->get_wol(phydev, wol);
1323}
1324EXPORT_SYMBOL(phy_ethtool_get_wol);
1325
1326int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1327 struct ethtool_link_ksettings *cmd)
1328{
1329 struct phy_device *phydev = ndev->phydev;
1330
1331 if (!phydev)
1332 return -ENODEV;
1333
1334 return phy_ethtool_ksettings_get(phydev, cmd);
1335}
1336EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1337
1338int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1339 const struct ethtool_link_ksettings *cmd)
1340{
1341 struct phy_device *phydev = ndev->phydev;
1342
1343 if (!phydev)
1344 return -ENODEV;
1345
1346 return phy_ethtool_ksettings_set(phydev, cmd);
1347}
1348EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1349
1350int phy_ethtool_nway_reset(struct net_device *ndev)
1351{
1352 struct phy_device *phydev = ndev->phydev;
1353
1354 if (!phydev)
1355 return -ENODEV;
1356
1357 if (!phydev->drv)
1358 return -EIO;
1359
1360 return phy_restart_aneg(phydev);
1361}
1362EXPORT_SYMBOL(phy_ethtool_nway_reset);
1363