1
2
3
4
5
6
7
8
9
10
11#ifndef __PHY_H
12#define __PHY_H
13
14#include <linux/compiler.h>
15#include <linux/spinlock.h>
16#include <linux/ethtool.h>
17#include <linux/linkmode.h>
18#include <linux/netlink.h>
19#include <linux/mdio.h>
20#include <linux/mii.h>
21#include <linux/mii_timestamper.h>
22#include <linux/module.h>
23#include <linux/timer.h>
24#include <linux/workqueue.h>
25#include <linux/mod_devicetable.h>
26#include <linux/u64_stats_sync.h>
27#include <linux/irqreturn.h>
28#include <linux/iopoll.h>
29#include <linux/refcount.h>
30
31#include <linux/atomic.h>
32
33#define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \
34 SUPPORTED_TP | \
35 SUPPORTED_MII)
36
37#define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \
38 SUPPORTED_10baseT_Full)
39
40#define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \
41 SUPPORTED_100baseT_Full)
42
43#define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \
44 SUPPORTED_1000baseT_Full)
45
46extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
47extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
48extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
49extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
50extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
51extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
52extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
53extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
54
55#define PHY_BASIC_FEATURES ((unsigned long *)&phy_basic_features)
56#define PHY_BASIC_T1_FEATURES ((unsigned long *)&phy_basic_t1_features)
57#define PHY_GBIT_FEATURES ((unsigned long *)&phy_gbit_features)
58#define PHY_GBIT_FIBRE_FEATURES ((unsigned long *)&phy_gbit_fibre_features)
59#define PHY_GBIT_ALL_PORTS_FEATURES ((unsigned long *)&phy_gbit_all_ports_features)
60#define PHY_10GBIT_FEATURES ((unsigned long *)&phy_10gbit_features)
61#define PHY_10GBIT_FEC_FEATURES ((unsigned long *)&phy_10gbit_fec_features)
62#define PHY_10GBIT_FULL_FEATURES ((unsigned long *)&phy_10gbit_full_features)
63
64extern const int phy_basic_ports_array[3];
65extern const int phy_fibre_port_array[1];
66extern const int phy_all_ports_features_array[7];
67extern const int phy_10_100_features_array[4];
68extern const int phy_basic_t1_features_array[2];
69extern const int phy_gbit_features_array[2];
70extern const int phy_10gbit_features_array[1];
71
72
73
74
75
76
77#define PHY_POLL -1
78#define PHY_IGNORE_INTERRUPT -2
79
80#define PHY_IS_INTERNAL 0x00000001
81#define PHY_RST_AFTER_CLK_EN 0x00000002
82#define PHY_POLL_CABLE_TEST 0x00000004
83#define MDIO_DEVICE_IS_PHY 0x80000000
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118typedef enum {
119 PHY_INTERFACE_MODE_NA,
120 PHY_INTERFACE_MODE_INTERNAL,
121 PHY_INTERFACE_MODE_MII,
122 PHY_INTERFACE_MODE_GMII,
123 PHY_INTERFACE_MODE_SGMII,
124 PHY_INTERFACE_MODE_TBI,
125 PHY_INTERFACE_MODE_REVMII,
126 PHY_INTERFACE_MODE_RMII,
127 PHY_INTERFACE_MODE_RGMII,
128 PHY_INTERFACE_MODE_RGMII_ID,
129 PHY_INTERFACE_MODE_RGMII_RXID,
130 PHY_INTERFACE_MODE_RGMII_TXID,
131 PHY_INTERFACE_MODE_RTBI,
132 PHY_INTERFACE_MODE_SMII,
133 PHY_INTERFACE_MODE_XGMII,
134 PHY_INTERFACE_MODE_XLGMII,
135 PHY_INTERFACE_MODE_MOCA,
136 PHY_INTERFACE_MODE_QSGMII,
137 PHY_INTERFACE_MODE_TRGMII,
138 PHY_INTERFACE_MODE_1000BASEX,
139 PHY_INTERFACE_MODE_2500BASEX,
140 PHY_INTERFACE_MODE_RXAUI,
141 PHY_INTERFACE_MODE_XAUI,
142
143 PHY_INTERFACE_MODE_10GBASER,
144 PHY_INTERFACE_MODE_USXGMII,
145
146 PHY_INTERFACE_MODE_10GKR,
147 PHY_INTERFACE_MODE_MAX,
148} phy_interface_t;
149
150
151
152
153unsigned int phy_supported_speeds(struct phy_device *phy,
154 unsigned int *speeds,
155 unsigned int size);
156
157
158
159
160
161
162
163
164
165static inline const char *phy_modes(phy_interface_t interface)
166{
167 switch (interface) {
168 case PHY_INTERFACE_MODE_NA:
169 return "";
170 case PHY_INTERFACE_MODE_INTERNAL:
171 return "internal";
172 case PHY_INTERFACE_MODE_MII:
173 return "mii";
174 case PHY_INTERFACE_MODE_GMII:
175 return "gmii";
176 case PHY_INTERFACE_MODE_SGMII:
177 return "sgmii";
178 case PHY_INTERFACE_MODE_TBI:
179 return "tbi";
180 case PHY_INTERFACE_MODE_REVMII:
181 return "rev-mii";
182 case PHY_INTERFACE_MODE_RMII:
183 return "rmii";
184 case PHY_INTERFACE_MODE_RGMII:
185 return "rgmii";
186 case PHY_INTERFACE_MODE_RGMII_ID:
187 return "rgmii-id";
188 case PHY_INTERFACE_MODE_RGMII_RXID:
189 return "rgmii-rxid";
190 case PHY_INTERFACE_MODE_RGMII_TXID:
191 return "rgmii-txid";
192 case PHY_INTERFACE_MODE_RTBI:
193 return "rtbi";
194 case PHY_INTERFACE_MODE_SMII:
195 return "smii";
196 case PHY_INTERFACE_MODE_XGMII:
197 return "xgmii";
198 case PHY_INTERFACE_MODE_XLGMII:
199 return "xlgmii";
200 case PHY_INTERFACE_MODE_MOCA:
201 return "moca";
202 case PHY_INTERFACE_MODE_QSGMII:
203 return "qsgmii";
204 case PHY_INTERFACE_MODE_TRGMII:
205 return "trgmii";
206 case PHY_INTERFACE_MODE_1000BASEX:
207 return "1000base-x";
208 case PHY_INTERFACE_MODE_2500BASEX:
209 return "2500base-x";
210 case PHY_INTERFACE_MODE_RXAUI:
211 return "rxaui";
212 case PHY_INTERFACE_MODE_XAUI:
213 return "xaui";
214 case PHY_INTERFACE_MODE_10GBASER:
215 return "10gbase-r";
216 case PHY_INTERFACE_MODE_USXGMII:
217 return "usxgmii";
218 case PHY_INTERFACE_MODE_10GKR:
219 return "10gbase-kr";
220 default:
221 return "unknown";
222 }
223}
224
225
226#define PHY_INIT_TIMEOUT 100000
227#define PHY_FORCE_TIMEOUT 10
228
229#define PHY_MAX_ADDR 32
230
231
232#define PHY_ID_FMT "%s:%02x"
233
234#define MII_BUS_ID_SIZE 61
235
236struct device;
237struct phylink;
238struct sfp_bus;
239struct sfp_upstream_ops;
240struct sk_buff;
241
242
243
244
245
246
247
248
249
250struct mdio_bus_stats {
251 u64_stats_t transfers;
252 u64_stats_t errors;
253 u64_stats_t writes;
254 u64_stats_t reads;
255
256 struct u64_stats_sync syncp;
257};
258
259
260
261
262
263
264
265
266
267
268
269
270
271struct phy_package_shared {
272 int addr;
273 refcount_t refcnt;
274 unsigned long flags;
275 size_t priv_size;
276
277
278
279
280
281
282
283 void *priv;
284};
285
286
287#define PHY_SHARED_F_INIT_DONE 0
288#define PHY_SHARED_F_PROBE_DONE 1
289
290
291
292
293
294
295
296
297
298
299
300
301struct mii_bus {
302 struct module *owner;
303 const char *name;
304 char id[MII_BUS_ID_SIZE];
305 void *priv;
306
307 int (*read)(struct mii_bus *bus, int addr, int regnum);
308
309 int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val);
310
311 int (*reset)(struct mii_bus *bus);
312
313
314 struct mdio_bus_stats stats[PHY_MAX_ADDR];
315
316
317
318
319
320 struct mutex mdio_lock;
321
322
323 struct device *parent;
324
325 enum {
326 MDIOBUS_ALLOCATED = 1,
327 MDIOBUS_REGISTERED,
328 MDIOBUS_UNREGISTERED,
329 MDIOBUS_RELEASED,
330 } state;
331
332
333 struct device dev;
334
335
336 struct mdio_device *mdio_map[PHY_MAX_ADDR];
337
338
339 u32 phy_mask;
340
341
342 u32 phy_ignore_ta_mask;
343
344
345
346
347
348 int irq[PHY_MAX_ADDR];
349
350
351 int reset_delay_us;
352
353 int reset_post_delay_us;
354
355 struct gpio_desc *reset_gpiod;
356
357
358 enum {
359 MDIOBUS_NO_CAP = 0,
360 MDIOBUS_C22,
361 MDIOBUS_C45,
362 MDIOBUS_C22_C45,
363 } probe_capabilities;
364
365
366 struct mutex shared_lock;
367
368
369 struct phy_package_shared *shared[PHY_MAX_ADDR];
370};
371#define to_mii_bus(d) container_of(d, struct mii_bus, dev)
372
373struct mii_bus *mdiobus_alloc_size(size_t size);
374
375
376
377
378
379
380
381static inline struct mii_bus *mdiobus_alloc(void)
382{
383 return mdiobus_alloc_size(0);
384}
385
386int __mdiobus_register(struct mii_bus *bus, struct module *owner);
387int __devm_mdiobus_register(struct device *dev, struct mii_bus *bus,
388 struct module *owner);
389#define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
390#define devm_mdiobus_register(dev, bus) \
391 __devm_mdiobus_register(dev, bus, THIS_MODULE)
392
393void mdiobus_unregister(struct mii_bus *bus);
394void mdiobus_free(struct mii_bus *bus);
395struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
396static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
397{
398 return devm_mdiobus_alloc_size(dev, 0);
399}
400
401struct mii_bus *mdio_find_bus(const char *mdio_name);
402struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
403
404#define PHY_INTERRUPT_DISABLED false
405#define PHY_INTERRUPT_ENABLED true
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443enum phy_state {
444 PHY_DOWN = 0,
445 PHY_READY,
446 PHY_HALTED,
447 PHY_UP,
448 PHY_RUNNING,
449 PHY_NOLINK,
450 PHY_CABLETEST,
451};
452
453#define MDIO_MMD_NUM 32
454
455
456
457
458
459
460
461struct phy_c45_device_ids {
462 u32 devices_in_package;
463 u32 mmds_present;
464 u32 device_ids[MDIO_MMD_NUM];
465};
466
467struct macsec_context;
468struct macsec_ops;
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539struct phy_device {
540 struct mdio_device mdio;
541
542
543
544 struct phy_driver *drv;
545
546 u32 phy_id;
547
548 struct phy_c45_device_ids c45_ids;
549 unsigned is_c45:1;
550 unsigned is_internal:1;
551 unsigned is_pseudo_fixed_link:1;
552 unsigned is_gigabit_capable:1;
553 unsigned has_fixups:1;
554 unsigned suspended:1;
555 unsigned suspended_by_mdio_bus:1;
556 unsigned sysfs_links:1;
557 unsigned loopback_enabled:1;
558 unsigned downshifted_rate:1;
559
560 unsigned autoneg:1;
561
562 unsigned link:1;
563 unsigned autoneg_complete:1;
564
565
566 unsigned interrupts:1;
567
568 enum phy_state state;
569
570 u32 dev_flags;
571
572 phy_interface_t interface;
573
574
575
576
577
578 int speed;
579 int duplex;
580 int pause;
581 int asym_pause;
582 u8 master_slave_get;
583 u8 master_slave_set;
584 u8 master_slave_state;
585
586
587
588 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
589 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
590 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
591
592 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
593
594
595 u32 eee_broken_modes;
596
597#ifdef CONFIG_LED_TRIGGER_PHY
598 struct phy_led_trigger *phy_led_triggers;
599 unsigned int phy_num_led_triggers;
600 struct phy_led_trigger *last_triggered;
601
602 struct phy_led_trigger *led_link_trigger;
603#endif
604
605
606
607
608
609 int irq;
610
611
612
613 void *priv;
614
615
616
617 struct phy_package_shared *shared;
618
619
620 struct sk_buff *skb;
621 void *ehdr;
622 struct nlattr *nest;
623
624
625 struct delayed_work state_queue;
626
627 struct mutex lock;
628
629
630 bool sfp_bus_attached;
631 struct sfp_bus *sfp_bus;
632 struct phylink *phylink;
633 struct net_device *attached_dev;
634 struct mii_timestamper *mii_ts;
635
636 u8 mdix;
637 u8 mdix_ctrl;
638
639 void (*phy_link_change)(struct phy_device *phydev, bool up);
640 void (*adjust_link)(struct net_device *dev);
641
642#if IS_ENABLED(CONFIG_MACSEC)
643
644 const struct macsec_ops *macsec_ops;
645#endif
646};
647#define to_phy_device(d) container_of(to_mdio_device(d), \
648 struct phy_device, mdio)
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663struct phy_tdr_config {
664 u32 first;
665 u32 last;
666 u32 step;
667 s8 pair;
668};
669#define PHY_PAIR_ALL -1
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694struct phy_driver {
695 struct mdio_driver_common mdiodrv;
696 u32 phy_id;
697 char *name;
698 u32 phy_id_mask;
699 const unsigned long * const features;
700 u32 flags;
701 const void *driver_data;
702
703
704
705
706 int (*soft_reset)(struct phy_device *phydev);
707
708
709
710
711
712 int (*config_init)(struct phy_device *phydev);
713
714
715
716
717
718 int (*probe)(struct phy_device *phydev);
719
720
721
722
723
724 int (*get_features)(struct phy_device *phydev);
725
726
727
728 int (*suspend)(struct phy_device *phydev);
729
730 int (*resume)(struct phy_device *phydev);
731
732
733
734
735
736
737
738 int (*config_aneg)(struct phy_device *phydev);
739
740
741 int (*aneg_done)(struct phy_device *phydev);
742
743
744 int (*read_status)(struct phy_device *phydev);
745
746
747 int (*ack_interrupt)(struct phy_device *phydev);
748
749
750 int (*config_intr)(struct phy_device *phydev);
751
752
753
754
755
756
757 int (*did_interrupt)(struct phy_device *phydev);
758
759
760 irqreturn_t (*handle_interrupt)(struct phy_device *phydev);
761
762
763 void (*remove)(struct phy_device *phydev);
764
765
766
767
768
769
770 int (*match_phy_device)(struct phy_device *phydev);
771
772
773
774
775
776
777
778 int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
779
780
781
782
783
784 void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
785
786
787
788
789
790
791
792
793
794 void (*link_change_notify)(struct phy_device *dev);
795
796
797
798
799
800
801
802
803
804
805
806 int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
807
808
809
810
811
812
813
814
815
816
817
818 int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
819 u16 val);
820
821
822 int (*read_page)(struct phy_device *dev);
823
824 int (*write_page)(struct phy_device *dev, int page);
825
826
827
828
829
830 int (*module_info)(struct phy_device *dev,
831 struct ethtool_modinfo *modinfo);
832
833
834
835
836
837 int (*module_eeprom)(struct phy_device *dev,
838 struct ethtool_eeprom *ee, u8 *data);
839
840
841 int (*cable_test_start)(struct phy_device *dev);
842
843
844 int (*cable_test_tdr_start)(struct phy_device *dev,
845 const struct phy_tdr_config *config);
846
847
848
849
850
851 int (*cable_test_get_status)(struct phy_device *dev, bool *finished);
852
853
854
855 int (*get_sset_count)(struct phy_device *dev);
856
857 void (*get_strings)(struct phy_device *dev, u8 *data);
858
859 void (*get_stats)(struct phy_device *dev,
860 struct ethtool_stats *stats, u64 *data);
861
862
863
864 int (*get_tunable)(struct phy_device *dev,
865 struct ethtool_tunable *tuna, void *data);
866
867 int (*set_tunable)(struct phy_device *dev,
868 struct ethtool_tunable *tuna,
869 const void *data);
870
871 int (*set_loopback)(struct phy_device *dev, bool enable);
872
873 int (*get_sqi)(struct phy_device *dev);
874
875 int (*get_sqi_max)(struct phy_device *dev);
876};
877#define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
878 struct phy_driver, mdiodrv)
879
880#define PHY_ANY_ID "MATCH ANY PHY"
881#define PHY_ANY_UID 0xffffffff
882
883#define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 0)
884#define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
885#define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
886
887
888struct phy_fixup {
889 struct list_head list;
890 char bus_id[MII_BUS_ID_SIZE + 3];
891 u32 phy_uid;
892 u32 phy_uid_mask;
893 int (*run)(struct phy_device *phydev);
894};
895
896const char *phy_speed_to_str(int speed);
897const char *phy_duplex_to_str(unsigned int duplex);
898
899
900
901
902struct phy_setting {
903 u32 speed;
904 u8 duplex;
905 u8 bit;
906};
907
908const struct phy_setting *
909phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
910 bool exact);
911size_t phy_speeds(unsigned int *speeds, size_t size,
912 unsigned long *mask);
913void of_set_phy_supported(struct phy_device *phydev);
914void of_set_phy_eee_broken(struct phy_device *phydev);
915int phy_speed_down_core(struct phy_device *phydev);
916
917
918
919
920
921static inline bool phy_is_started(struct phy_device *phydev)
922{
923 return phydev->state >= PHY_UP;
924}
925
926void phy_resolve_aneg_pause(struct phy_device *phydev);
927void phy_resolve_aneg_linkmode(struct phy_device *phydev);
928void phy_check_downshift(struct phy_device *phydev);
929
930
931
932
933
934
935
936
937
938
939static inline int phy_read(struct phy_device *phydev, u32 regnum)
940{
941 return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
942}
943
944#define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
945 timeout_us, sleep_before_read) \
946({ \
947 int __ret = read_poll_timeout(phy_read, val, (cond) || val < 0, \
948 sleep_us, timeout_us, sleep_before_read, phydev, regnum); \
949 if (val < 0) \
950 __ret = val; \
951 if (__ret) \
952 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
953 __ret; \
954})
955
956
957
958
959
960
961
962
963
964static inline int __phy_read(struct phy_device *phydev, u32 regnum)
965{
966 return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
967}
968
969
970
971
972
973
974
975
976
977
978
979static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
980{
981 return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
982}
983
984
985
986
987
988
989
990
991
992static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
993{
994 return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum,
995 val);
996}
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
1011 u16 mask, u16 set)
1012{
1013 return __mdiobus_modify_changed(phydev->mdio.bus, phydev->mdio.addr,
1014 regnum, mask, set);
1015}
1016
1017
1018
1019
1020
1021int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041#define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
1042 sleep_us, timeout_us, sleep_before_read) \
1043({ \
1044 int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
1045 sleep_us, timeout_us, sleep_before_read, \
1046 phydev, devaddr, regnum); \
1047 if (val < 0) \
1048 __ret = val; \
1049 if (__ret) \
1050 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
1051 __ret; \
1052})
1053
1054
1055
1056
1057
1058int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1059
1060
1061
1062
1063
1064int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1065
1066
1067
1068
1069
1070int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1071
1072int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1073 u16 set);
1074int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1075 u16 set);
1076int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1077int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1078
1079int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1080 u16 mask, u16 set);
1081int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1082 u16 mask, u16 set);
1083int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1084 u16 mask, u16 set);
1085int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1086 u16 mask, u16 set);
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1097{
1098 return __phy_modify(phydev, regnum, 0, val);
1099}
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum,
1110 u16 val)
1111{
1112 return __phy_modify(phydev, regnum, val, 0);
1113}
1114
1115
1116
1117
1118
1119
1120
1121static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1122{
1123 return phy_modify(phydev, regnum, 0, val);
1124}
1125
1126
1127
1128
1129
1130
1131
1132static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val)
1133{
1134 return phy_modify(phydev, regnum, val, 0);
1135}
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
1148 u32 regnum, u16 val)
1149{
1150 return __phy_modify_mmd(phydev, devad, regnum, 0, val);
1151}
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1164 u32 regnum, u16 val)
1165{
1166 return __phy_modify_mmd(phydev, devad, regnum, val, 0);
1167}
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
1178 u32 regnum, u16 val)
1179{
1180 return phy_modify_mmd(phydev, devad, regnum, 0, val);
1181}
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1192 u32 regnum, u16 val)
1193{
1194 return phy_modify_mmd(phydev, devad, regnum, val, 0);
1195}
1196
1197
1198
1199
1200
1201
1202
1203
1204static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
1205{
1206 return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT;
1207}
1208
1209
1210
1211
1212
1213
1214static inline bool phy_polling_mode(struct phy_device *phydev)
1215{
1216 if (phydev->state == PHY_CABLETEST)
1217 if (phydev->drv->flags & PHY_POLL_CABLE_TEST)
1218 return true;
1219
1220 return phydev->irq == PHY_POLL;
1221}
1222
1223
1224
1225
1226
1227static inline bool phy_has_hwtstamp(struct phy_device *phydev)
1228{
1229 return phydev && phydev->mii_ts && phydev->mii_ts->hwtstamp;
1230}
1231
1232
1233
1234
1235
1236static inline bool phy_has_rxtstamp(struct phy_device *phydev)
1237{
1238 return phydev && phydev->mii_ts && phydev->mii_ts->rxtstamp;
1239}
1240
1241
1242
1243
1244
1245
1246static inline bool phy_has_tsinfo(struct phy_device *phydev)
1247{
1248 return phydev && phydev->mii_ts && phydev->mii_ts->ts_info;
1249}
1250
1251
1252
1253
1254
1255static inline bool phy_has_txtstamp(struct phy_device *phydev)
1256{
1257 return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp;
1258}
1259
1260static inline int phy_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1261{
1262 return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
1263}
1264
1265static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb,
1266 int type)
1267{
1268 return phydev->mii_ts->rxtstamp(phydev->mii_ts, skb, type);
1269}
1270
1271static inline int phy_ts_info(struct phy_device *phydev,
1272 struct ethtool_ts_info *tsinfo)
1273{
1274 return phydev->mii_ts->ts_info(phydev->mii_ts, tsinfo);
1275}
1276
1277static inline void phy_txtstamp(struct phy_device *phydev, struct sk_buff *skb,
1278 int type)
1279{
1280 phydev->mii_ts->txtstamp(phydev->mii_ts, skb, type);
1281}
1282
1283
1284
1285
1286
1287static inline bool phy_is_internal(struct phy_device *phydev)
1288{
1289 return phydev->is_internal;
1290}
1291
1292
1293
1294
1295
1296
1297static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
1298{
1299 return mode >= PHY_INTERFACE_MODE_RGMII &&
1300 mode <= PHY_INTERFACE_MODE_RGMII_TXID;
1301};
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311static inline bool phy_interface_mode_is_8023z(phy_interface_t mode)
1312{
1313 return mode == PHY_INTERFACE_MODE_1000BASEX ||
1314 mode == PHY_INTERFACE_MODE_2500BASEX;
1315}
1316
1317
1318
1319
1320
1321
1322static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
1323{
1324 return phy_interface_mode_is_rgmii(phydev->interface);
1325};
1326
1327
1328
1329
1330
1331
1332static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
1333{
1334 return phydev->is_pseudo_fixed_link;
1335}
1336
1337int phy_save_page(struct phy_device *phydev);
1338int phy_select_page(struct phy_device *phydev, int page);
1339int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
1340int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
1341int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
1342int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
1343 u16 mask, u16 set);
1344int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
1345 u16 mask, u16 set);
1346
1347struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
1348 bool is_c45,
1349 struct phy_c45_device_ids *c45_ids);
1350#if IS_ENABLED(CONFIG_PHYLIB)
1351struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
1352int phy_device_register(struct phy_device *phy);
1353void phy_device_free(struct phy_device *phydev);
1354#else
1355static inline
1356struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
1357{
1358 return NULL;
1359}
1360
1361static inline int phy_device_register(struct phy_device *phy)
1362{
1363 return 0;
1364}
1365
1366static inline void phy_device_free(struct phy_device *phydev) { }
1367#endif
1368void phy_device_remove(struct phy_device *phydev);
1369int phy_init_hw(struct phy_device *phydev);
1370int phy_suspend(struct phy_device *phydev);
1371int phy_resume(struct phy_device *phydev);
1372int __phy_resume(struct phy_device *phydev);
1373int phy_loopback(struct phy_device *phydev, bool enable);
1374void phy_sfp_attach(void *upstream, struct sfp_bus *bus);
1375void phy_sfp_detach(void *upstream, struct sfp_bus *bus);
1376int phy_sfp_probe(struct phy_device *phydev,
1377 const struct sfp_upstream_ops *ops);
1378struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1379 phy_interface_t interface);
1380struct phy_device *phy_find_first(struct mii_bus *bus);
1381int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1382 u32 flags, phy_interface_t interface);
1383int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1384 void (*handler)(struct net_device *),
1385 phy_interface_t interface);
1386struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1387 void (*handler)(struct net_device *),
1388 phy_interface_t interface);
1389void phy_disconnect(struct phy_device *phydev);
1390void phy_detach(struct phy_device *phydev);
1391void phy_start(struct phy_device *phydev);
1392void phy_stop(struct phy_device *phydev);
1393int phy_start_aneg(struct phy_device *phydev);
1394int phy_aneg_done(struct phy_device *phydev);
1395int phy_speed_down(struct phy_device *phydev, bool sync);
1396int phy_speed_up(struct phy_device *phydev);
1397
1398int phy_restart_aneg(struct phy_device *phydev);
1399int phy_reset_after_clk_enable(struct phy_device *phydev);
1400
1401#if IS_ENABLED(CONFIG_PHYLIB)
1402int phy_start_cable_test(struct phy_device *phydev,
1403 struct netlink_ext_ack *extack);
1404int phy_start_cable_test_tdr(struct phy_device *phydev,
1405 struct netlink_ext_ack *extack,
1406 const struct phy_tdr_config *config);
1407#else
1408static inline
1409int phy_start_cable_test(struct phy_device *phydev,
1410 struct netlink_ext_ack *extack)
1411{
1412 NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1413 return -EOPNOTSUPP;
1414}
1415static inline
1416int phy_start_cable_test_tdr(struct phy_device *phydev,
1417 struct netlink_ext_ack *extack,
1418 const struct phy_tdr_config *config)
1419{
1420 NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1421 return -EOPNOTSUPP;
1422}
1423#endif
1424
1425int phy_cable_test_result(struct phy_device *phydev, u8 pair, u16 result);
1426int phy_cable_test_fault_length(struct phy_device *phydev, u8 pair,
1427 u16 cm);
1428
1429static inline void phy_device_reset(struct phy_device *phydev, int value)
1430{
1431 mdio_device_reset(&phydev->mdio, value);
1432}
1433
1434#define phydev_err(_phydev, format, args...) \
1435 dev_err(&_phydev->mdio.dev, format, ##args)
1436
1437#define phydev_info(_phydev, format, args...) \
1438 dev_info(&_phydev->mdio.dev, format, ##args)
1439
1440#define phydev_warn(_phydev, format, args...) \
1441 dev_warn(&_phydev->mdio.dev, format, ##args)
1442
1443#define phydev_dbg(_phydev, format, args...) \
1444 dev_dbg(&_phydev->mdio.dev, format, ##args)
1445
1446static inline const char *phydev_name(const struct phy_device *phydev)
1447{
1448 return dev_name(&phydev->mdio.dev);
1449}
1450
1451static inline void phy_lock_mdio_bus(struct phy_device *phydev)
1452{
1453 mutex_lock(&phydev->mdio.bus->mdio_lock);
1454}
1455
1456static inline void phy_unlock_mdio_bus(struct phy_device *phydev)
1457{
1458 mutex_unlock(&phydev->mdio.bus->mdio_lock);
1459}
1460
1461void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1462 __printf(2, 3);
1463char *phy_attached_info_irq(struct phy_device *phydev)
1464 __malloc;
1465void phy_attached_info(struct phy_device *phydev);
1466
1467
1468int genphy_read_abilities(struct phy_device *phydev);
1469int genphy_setup_forced(struct phy_device *phydev);
1470int genphy_restart_aneg(struct phy_device *phydev);
1471int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1472int genphy_config_eee_advert(struct phy_device *phydev);
1473int __genphy_config_aneg(struct phy_device *phydev, bool changed);
1474int genphy_aneg_done(struct phy_device *phydev);
1475int genphy_update_link(struct phy_device *phydev);
1476int genphy_read_lpa(struct phy_device *phydev);
1477int genphy_read_status_fixed(struct phy_device *phydev);
1478int genphy_read_status(struct phy_device *phydev);
1479int genphy_suspend(struct phy_device *phydev);
1480int genphy_resume(struct phy_device *phydev);
1481int genphy_loopback(struct phy_device *phydev, bool enable);
1482int genphy_soft_reset(struct phy_device *phydev);
1483
1484static inline int genphy_config_aneg(struct phy_device *phydev)
1485{
1486 return __genphy_config_aneg(phydev, false);
1487}
1488
1489static inline int genphy_no_ack_interrupt(struct phy_device *phydev)
1490{
1491 return 0;
1492}
1493static inline int genphy_no_config_intr(struct phy_device *phydev)
1494{
1495 return 0;
1496}
1497int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
1498 u16 regnum);
1499int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1500 u16 regnum, u16 val);
1501
1502
1503int genphy_c37_config_aneg(struct phy_device *phydev);
1504int genphy_c37_read_status(struct phy_device *phydev);
1505
1506
1507int genphy_c45_restart_aneg(struct phy_device *phydev);
1508int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1509int genphy_c45_aneg_done(struct phy_device *phydev);
1510int genphy_c45_read_link(struct phy_device *phydev);
1511int genphy_c45_read_lpa(struct phy_device *phydev);
1512int genphy_c45_read_pma(struct phy_device *phydev);
1513int genphy_c45_pma_setup_forced(struct phy_device *phydev);
1514int genphy_c45_an_config_aneg(struct phy_device *phydev);
1515int genphy_c45_an_disable_aneg(struct phy_device *phydev);
1516int genphy_c45_read_mdix(struct phy_device *phydev);
1517int genphy_c45_pma_read_abilities(struct phy_device *phydev);
1518int genphy_c45_read_status(struct phy_device *phydev);
1519int genphy_c45_config_aneg(struct phy_device *phydev);
1520
1521
1522extern struct phy_driver genphy_c45_driver;
1523
1524
1525int gen10g_config_aneg(struct phy_device *phydev);
1526
1527static inline int phy_read_status(struct phy_device *phydev)
1528{
1529 if (!phydev->drv)
1530 return -EIO;
1531
1532 if (phydev->drv->read_status)
1533 return phydev->drv->read_status(phydev);
1534 else
1535 return genphy_read_status(phydev);
1536}
1537
1538void phy_driver_unregister(struct phy_driver *drv);
1539void phy_drivers_unregister(struct phy_driver *drv, int n);
1540int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
1541int phy_drivers_register(struct phy_driver *new_driver, int n,
1542 struct module *owner);
1543void phy_state_machine(struct work_struct *work);
1544void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
1545void phy_mac_interrupt(struct phy_device *phydev);
1546void phy_start_machine(struct phy_device *phydev);
1547void phy_stop_machine(struct phy_device *phydev);
1548void phy_ethtool_ksettings_get(struct phy_device *phydev,
1549 struct ethtool_link_ksettings *cmd);
1550int phy_ethtool_ksettings_set(struct phy_device *phydev,
1551 const struct ethtool_link_ksettings *cmd);
1552int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
1553int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
1554int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd);
1555int phy_disable_interrupts(struct phy_device *phydev);
1556void phy_request_interrupt(struct phy_device *phydev);
1557void phy_free_interrupt(struct phy_device *phydev);
1558void phy_print_status(struct phy_device *phydev);
1559int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
1560void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
1561void phy_advertise_supported(struct phy_device *phydev);
1562void phy_support_sym_pause(struct phy_device *phydev);
1563void phy_support_asym_pause(struct phy_device *phydev);
1564void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
1565 bool autoneg);
1566void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
1567bool phy_validate_pause(struct phy_device *phydev,
1568 struct ethtool_pauseparam *pp);
1569void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause);
1570
1571s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
1572 const int *delay_values, int size, bool is_rx);
1573
1574void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv,
1575 bool *tx_pause, bool *rx_pause);
1576
1577int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
1578 int (*run)(struct phy_device *));
1579int phy_register_fixup_for_id(const char *bus_id,
1580 int (*run)(struct phy_device *));
1581int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
1582 int (*run)(struct phy_device *));
1583
1584int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
1585int phy_unregister_fixup_for_id(const char *bus_id);
1586int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
1587
1588int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
1589int phy_get_eee_err(struct phy_device *phydev);
1590int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
1591int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
1592int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
1593void phy_ethtool_get_wol(struct phy_device *phydev,
1594 struct ethtool_wolinfo *wol);
1595int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1596 struct ethtool_link_ksettings *cmd);
1597int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1598 const struct ethtool_link_ksettings *cmd);
1599int phy_ethtool_nway_reset(struct net_device *ndev);
1600int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size);
1601void phy_package_leave(struct phy_device *phydev);
1602int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
1603 int addr, size_t priv_size);
1604
1605#if IS_ENABLED(CONFIG_PHYLIB)
1606int __init mdio_bus_init(void);
1607void mdio_bus_exit(void);
1608#endif
1609
1610int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data);
1611int phy_ethtool_get_sset_count(struct phy_device *phydev);
1612int phy_ethtool_get_stats(struct phy_device *phydev,
1613 struct ethtool_stats *stats, u64 *data);
1614
1615static inline int phy_package_read(struct phy_device *phydev, u32 regnum)
1616{
1617 struct phy_package_shared *shared = phydev->shared;
1618
1619 if (!shared)
1620 return -EIO;
1621
1622 return mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
1623}
1624
1625static inline int __phy_package_read(struct phy_device *phydev, u32 regnum)
1626{
1627 struct phy_package_shared *shared = phydev->shared;
1628
1629 if (!shared)
1630 return -EIO;
1631
1632 return __mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
1633}
1634
1635static inline int phy_package_write(struct phy_device *phydev,
1636 u32 regnum, u16 val)
1637{
1638 struct phy_package_shared *shared = phydev->shared;
1639
1640 if (!shared)
1641 return -EIO;
1642
1643 return mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
1644}
1645
1646static inline int __phy_package_write(struct phy_device *phydev,
1647 u32 regnum, u16 val)
1648{
1649 struct phy_package_shared *shared = phydev->shared;
1650
1651 if (!shared)
1652 return -EIO;
1653
1654 return __mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
1655}
1656
1657static inline bool __phy_package_set_once(struct phy_device *phydev,
1658 unsigned int b)
1659{
1660 struct phy_package_shared *shared = phydev->shared;
1661
1662 if (!shared)
1663 return false;
1664
1665 return !test_and_set_bit(b, &shared->flags);
1666}
1667
1668static inline bool phy_package_init_once(struct phy_device *phydev)
1669{
1670 return __phy_package_set_once(phydev, PHY_SHARED_F_INIT_DONE);
1671}
1672
1673static inline bool phy_package_probe_once(struct phy_device *phydev)
1674{
1675 return __phy_package_set_once(phydev, PHY_SHARED_F_PROBE_DONE);
1676}
1677
1678extern struct bus_type mdio_bus_type;
1679
1680struct mdio_board_info {
1681 const char *bus_id;
1682 char modalias[MDIO_NAME_SIZE];
1683 int mdio_addr;
1684 const void *platform_data;
1685};
1686
1687#if IS_ENABLED(CONFIG_MDIO_DEVICE)
1688int mdiobus_register_board_info(const struct mdio_board_info *info,
1689 unsigned int n);
1690#else
1691static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
1692 unsigned int n)
1693{
1694 return 0;
1695}
1696#endif
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708#define phy_module_driver(__phy_drivers, __count) \
1709static int __init phy_module_init(void) \
1710{ \
1711 return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
1712} \
1713module_init(phy_module_init); \
1714static void __exit phy_module_exit(void) \
1715{ \
1716 phy_drivers_unregister(__phy_drivers, __count); \
1717} \
1718module_exit(phy_module_exit)
1719
1720#define module_phy_driver(__phy_drivers) \
1721 phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
1722
1723bool phy_driver_is_genphy(struct phy_device *phydev);
1724bool phy_driver_is_genphy_10g(struct phy_device *phydev);
1725
1726#endif
1727