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
748
749
750
751 int (*config_intr)(struct phy_device *phydev);
752
753
754 irqreturn_t (*handle_interrupt)(struct phy_device *phydev);
755
756
757 void (*remove)(struct phy_device *phydev);
758
759
760
761
762
763
764 int (*match_phy_device)(struct phy_device *phydev);
765
766
767
768
769
770
771
772 int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
773
774
775
776
777
778 void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
779
780
781
782
783
784
785
786
787
788 void (*link_change_notify)(struct phy_device *dev);
789
790
791
792
793
794
795
796
797
798
799
800 int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
801
802
803
804
805
806
807
808
809
810
811
812 int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
813 u16 val);
814
815
816 int (*read_page)(struct phy_device *dev);
817
818 int (*write_page)(struct phy_device *dev, int page);
819
820
821
822
823
824 int (*module_info)(struct phy_device *dev,
825 struct ethtool_modinfo *modinfo);
826
827
828
829
830
831 int (*module_eeprom)(struct phy_device *dev,
832 struct ethtool_eeprom *ee, u8 *data);
833
834
835 int (*cable_test_start)(struct phy_device *dev);
836
837
838 int (*cable_test_tdr_start)(struct phy_device *dev,
839 const struct phy_tdr_config *config);
840
841
842
843
844
845 int (*cable_test_get_status)(struct phy_device *dev, bool *finished);
846
847
848
849 int (*get_sset_count)(struct phy_device *dev);
850
851 void (*get_strings)(struct phy_device *dev, u8 *data);
852
853 void (*get_stats)(struct phy_device *dev,
854 struct ethtool_stats *stats, u64 *data);
855
856
857
858 int (*get_tunable)(struct phy_device *dev,
859 struct ethtool_tunable *tuna, void *data);
860
861 int (*set_tunable)(struct phy_device *dev,
862 struct ethtool_tunable *tuna,
863 const void *data);
864
865 int (*set_loopback)(struct phy_device *dev, bool enable);
866
867 int (*get_sqi)(struct phy_device *dev);
868
869 int (*get_sqi_max)(struct phy_device *dev);
870};
871#define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
872 struct phy_driver, mdiodrv)
873
874#define PHY_ANY_ID "MATCH ANY PHY"
875#define PHY_ANY_UID 0xffffffff
876
877#define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 0)
878#define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
879#define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
880
881
882struct phy_fixup {
883 struct list_head list;
884 char bus_id[MII_BUS_ID_SIZE + 3];
885 u32 phy_uid;
886 u32 phy_uid_mask;
887 int (*run)(struct phy_device *phydev);
888};
889
890const char *phy_speed_to_str(int speed);
891const char *phy_duplex_to_str(unsigned int duplex);
892
893
894
895
896struct phy_setting {
897 u32 speed;
898 u8 duplex;
899 u8 bit;
900};
901
902const struct phy_setting *
903phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
904 bool exact);
905size_t phy_speeds(unsigned int *speeds, size_t size,
906 unsigned long *mask);
907void of_set_phy_supported(struct phy_device *phydev);
908void of_set_phy_eee_broken(struct phy_device *phydev);
909int phy_speed_down_core(struct phy_device *phydev);
910
911
912
913
914
915static inline bool phy_is_started(struct phy_device *phydev)
916{
917 return phydev->state >= PHY_UP;
918}
919
920void phy_resolve_aneg_pause(struct phy_device *phydev);
921void phy_resolve_aneg_linkmode(struct phy_device *phydev);
922void phy_check_downshift(struct phy_device *phydev);
923
924
925
926
927
928
929
930
931
932
933static inline int phy_read(struct phy_device *phydev, u32 regnum)
934{
935 return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
936}
937
938#define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
939 timeout_us, sleep_before_read) \
940({ \
941 int __ret = read_poll_timeout(phy_read, val, (cond) || val < 0, \
942 sleep_us, timeout_us, sleep_before_read, phydev, regnum); \
943 if (val < 0) \
944 __ret = val; \
945 if (__ret) \
946 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
947 __ret; \
948})
949
950
951
952
953
954
955
956
957
958static inline int __phy_read(struct phy_device *phydev, u32 regnum)
959{
960 return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
961}
962
963
964
965
966
967
968
969
970
971
972
973static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
974{
975 return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
976}
977
978
979
980
981
982
983
984
985
986static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
987{
988 return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum,
989 val);
990}
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
1005 u16 mask, u16 set)
1006{
1007 return __mdiobus_modify_changed(phydev->mdio.bus, phydev->mdio.addr,
1008 regnum, mask, set);
1009}
1010
1011
1012
1013
1014
1015int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035#define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
1036 sleep_us, timeout_us, sleep_before_read) \
1037({ \
1038 int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
1039 sleep_us, timeout_us, sleep_before_read, \
1040 phydev, devaddr, regnum); \
1041 if (val < 0) \
1042 __ret = val; \
1043 if (__ret) \
1044 phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
1045 __ret; \
1046})
1047
1048
1049
1050
1051
1052int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
1053
1054
1055
1056
1057
1058int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1059
1060
1061
1062
1063
1064int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
1065
1066int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1067 u16 set);
1068int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
1069 u16 set);
1070int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1071int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
1072
1073int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1074 u16 mask, u16 set);
1075int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
1076 u16 mask, u16 set);
1077int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1078 u16 mask, u16 set);
1079int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
1080 u16 mask, u16 set);
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1091{
1092 return __phy_modify(phydev, regnum, 0, val);
1093}
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum,
1104 u16 val)
1105{
1106 return __phy_modify(phydev, regnum, val, 0);
1107}
1108
1109
1110
1111
1112
1113
1114
1115static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
1116{
1117 return phy_modify(phydev, regnum, 0, val);
1118}
1119
1120
1121
1122
1123
1124
1125
1126static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val)
1127{
1128 return phy_modify(phydev, regnum, val, 0);
1129}
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
1142 u32 regnum, u16 val)
1143{
1144 return __phy_modify_mmd(phydev, devad, regnum, 0, val);
1145}
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1158 u32 regnum, u16 val)
1159{
1160 return __phy_modify_mmd(phydev, devad, regnum, val, 0);
1161}
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
1172 u32 regnum, u16 val)
1173{
1174 return phy_modify_mmd(phydev, devad, regnum, 0, val);
1175}
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
1186 u32 regnum, u16 val)
1187{
1188 return phy_modify_mmd(phydev, devad, regnum, val, 0);
1189}
1190
1191
1192
1193
1194
1195
1196
1197
1198static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
1199{
1200 return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT;
1201}
1202
1203
1204
1205
1206
1207
1208static inline bool phy_polling_mode(struct phy_device *phydev)
1209{
1210 if (phydev->state == PHY_CABLETEST)
1211 if (phydev->drv->flags & PHY_POLL_CABLE_TEST)
1212 return true;
1213
1214 return phydev->irq == PHY_POLL;
1215}
1216
1217
1218
1219
1220
1221static inline bool phy_has_hwtstamp(struct phy_device *phydev)
1222{
1223 return phydev && phydev->mii_ts && phydev->mii_ts->hwtstamp;
1224}
1225
1226
1227
1228
1229
1230static inline bool phy_has_rxtstamp(struct phy_device *phydev)
1231{
1232 return phydev && phydev->mii_ts && phydev->mii_ts->rxtstamp;
1233}
1234
1235
1236
1237
1238
1239
1240static inline bool phy_has_tsinfo(struct phy_device *phydev)
1241{
1242 return phydev && phydev->mii_ts && phydev->mii_ts->ts_info;
1243}
1244
1245
1246
1247
1248
1249static inline bool phy_has_txtstamp(struct phy_device *phydev)
1250{
1251 return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp;
1252}
1253
1254static inline int phy_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
1255{
1256 return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
1257}
1258
1259static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb,
1260 int type)
1261{
1262 return phydev->mii_ts->rxtstamp(phydev->mii_ts, skb, type);
1263}
1264
1265static inline int phy_ts_info(struct phy_device *phydev,
1266 struct ethtool_ts_info *tsinfo)
1267{
1268 return phydev->mii_ts->ts_info(phydev->mii_ts, tsinfo);
1269}
1270
1271static inline void phy_txtstamp(struct phy_device *phydev, struct sk_buff *skb,
1272 int type)
1273{
1274 phydev->mii_ts->txtstamp(phydev->mii_ts, skb, type);
1275}
1276
1277
1278
1279
1280
1281static inline bool phy_is_internal(struct phy_device *phydev)
1282{
1283 return phydev->is_internal;
1284}
1285
1286
1287
1288
1289
1290
1291static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
1292{
1293 return mode >= PHY_INTERFACE_MODE_RGMII &&
1294 mode <= PHY_INTERFACE_MODE_RGMII_TXID;
1295};
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305static inline bool phy_interface_mode_is_8023z(phy_interface_t mode)
1306{
1307 return mode == PHY_INTERFACE_MODE_1000BASEX ||
1308 mode == PHY_INTERFACE_MODE_2500BASEX;
1309}
1310
1311
1312
1313
1314
1315
1316static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
1317{
1318 return phy_interface_mode_is_rgmii(phydev->interface);
1319};
1320
1321
1322
1323
1324
1325
1326static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
1327{
1328 return phydev->is_pseudo_fixed_link;
1329}
1330
1331int phy_save_page(struct phy_device *phydev);
1332int phy_select_page(struct phy_device *phydev, int page);
1333int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
1334int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
1335int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
1336int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
1337 u16 mask, u16 set);
1338int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
1339 u16 mask, u16 set);
1340
1341struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
1342 bool is_c45,
1343 struct phy_c45_device_ids *c45_ids);
1344#if IS_ENABLED(CONFIG_PHYLIB)
1345struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
1346int phy_device_register(struct phy_device *phy);
1347void phy_device_free(struct phy_device *phydev);
1348#else
1349static inline
1350struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
1351{
1352 return NULL;
1353}
1354
1355static inline int phy_device_register(struct phy_device *phy)
1356{
1357 return 0;
1358}
1359
1360static inline void phy_device_free(struct phy_device *phydev) { }
1361#endif
1362void phy_device_remove(struct phy_device *phydev);
1363int phy_init_hw(struct phy_device *phydev);
1364int phy_suspend(struct phy_device *phydev);
1365int phy_resume(struct phy_device *phydev);
1366int __phy_resume(struct phy_device *phydev);
1367int phy_loopback(struct phy_device *phydev, bool enable);
1368void phy_sfp_attach(void *upstream, struct sfp_bus *bus);
1369void phy_sfp_detach(void *upstream, struct sfp_bus *bus);
1370int phy_sfp_probe(struct phy_device *phydev,
1371 const struct sfp_upstream_ops *ops);
1372struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1373 phy_interface_t interface);
1374struct phy_device *phy_find_first(struct mii_bus *bus);
1375int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1376 u32 flags, phy_interface_t interface);
1377int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1378 void (*handler)(struct net_device *),
1379 phy_interface_t interface);
1380struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1381 void (*handler)(struct net_device *),
1382 phy_interface_t interface);
1383void phy_disconnect(struct phy_device *phydev);
1384void phy_detach(struct phy_device *phydev);
1385void phy_start(struct phy_device *phydev);
1386void phy_stop(struct phy_device *phydev);
1387int phy_start_aneg(struct phy_device *phydev);
1388int phy_aneg_done(struct phy_device *phydev);
1389int phy_speed_down(struct phy_device *phydev, bool sync);
1390int phy_speed_up(struct phy_device *phydev);
1391
1392int phy_restart_aneg(struct phy_device *phydev);
1393int phy_reset_after_clk_enable(struct phy_device *phydev);
1394
1395#if IS_ENABLED(CONFIG_PHYLIB)
1396int phy_start_cable_test(struct phy_device *phydev,
1397 struct netlink_ext_ack *extack);
1398int phy_start_cable_test_tdr(struct phy_device *phydev,
1399 struct netlink_ext_ack *extack,
1400 const struct phy_tdr_config *config);
1401#else
1402static inline
1403int phy_start_cable_test(struct phy_device *phydev,
1404 struct netlink_ext_ack *extack)
1405{
1406 NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1407 return -EOPNOTSUPP;
1408}
1409static inline
1410int phy_start_cable_test_tdr(struct phy_device *phydev,
1411 struct netlink_ext_ack *extack,
1412 const struct phy_tdr_config *config)
1413{
1414 NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
1415 return -EOPNOTSUPP;
1416}
1417#endif
1418
1419int phy_cable_test_result(struct phy_device *phydev, u8 pair, u16 result);
1420int phy_cable_test_fault_length(struct phy_device *phydev, u8 pair,
1421 u16 cm);
1422
1423static inline void phy_device_reset(struct phy_device *phydev, int value)
1424{
1425 mdio_device_reset(&phydev->mdio, value);
1426}
1427
1428#define phydev_err(_phydev, format, args...) \
1429 dev_err(&_phydev->mdio.dev, format, ##args)
1430
1431#define phydev_info(_phydev, format, args...) \
1432 dev_info(&_phydev->mdio.dev, format, ##args)
1433
1434#define phydev_warn(_phydev, format, args...) \
1435 dev_warn(&_phydev->mdio.dev, format, ##args)
1436
1437#define phydev_dbg(_phydev, format, args...) \
1438 dev_dbg(&_phydev->mdio.dev, format, ##args)
1439
1440static inline const char *phydev_name(const struct phy_device *phydev)
1441{
1442 return dev_name(&phydev->mdio.dev);
1443}
1444
1445static inline void phy_lock_mdio_bus(struct phy_device *phydev)
1446{
1447 mutex_lock(&phydev->mdio.bus->mdio_lock);
1448}
1449
1450static inline void phy_unlock_mdio_bus(struct phy_device *phydev)
1451{
1452 mutex_unlock(&phydev->mdio.bus->mdio_lock);
1453}
1454
1455void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1456 __printf(2, 3);
1457char *phy_attached_info_irq(struct phy_device *phydev)
1458 __malloc;
1459void phy_attached_info(struct phy_device *phydev);
1460
1461
1462int genphy_read_abilities(struct phy_device *phydev);
1463int genphy_setup_forced(struct phy_device *phydev);
1464int genphy_restart_aneg(struct phy_device *phydev);
1465int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1466int genphy_config_eee_advert(struct phy_device *phydev);
1467int __genphy_config_aneg(struct phy_device *phydev, bool changed);
1468int genphy_aneg_done(struct phy_device *phydev);
1469int genphy_update_link(struct phy_device *phydev);
1470int genphy_read_lpa(struct phy_device *phydev);
1471int genphy_read_status_fixed(struct phy_device *phydev);
1472int genphy_read_status(struct phy_device *phydev);
1473int genphy_suspend(struct phy_device *phydev);
1474int genphy_resume(struct phy_device *phydev);
1475int genphy_loopback(struct phy_device *phydev, bool enable);
1476int genphy_soft_reset(struct phy_device *phydev);
1477irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev);
1478
1479static inline int genphy_config_aneg(struct phy_device *phydev)
1480{
1481 return __genphy_config_aneg(phydev, false);
1482}
1483
1484static inline int genphy_no_config_intr(struct phy_device *phydev)
1485{
1486 return 0;
1487}
1488int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
1489 u16 regnum);
1490int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1491 u16 regnum, u16 val);
1492
1493
1494int genphy_c37_config_aneg(struct phy_device *phydev);
1495int genphy_c37_read_status(struct phy_device *phydev);
1496
1497
1498int genphy_c45_restart_aneg(struct phy_device *phydev);
1499int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1500int genphy_c45_aneg_done(struct phy_device *phydev);
1501int genphy_c45_read_link(struct phy_device *phydev);
1502int genphy_c45_read_lpa(struct phy_device *phydev);
1503int genphy_c45_read_pma(struct phy_device *phydev);
1504int genphy_c45_pma_setup_forced(struct phy_device *phydev);
1505int genphy_c45_an_config_aneg(struct phy_device *phydev);
1506int genphy_c45_an_disable_aneg(struct phy_device *phydev);
1507int genphy_c45_read_mdix(struct phy_device *phydev);
1508int genphy_c45_pma_read_abilities(struct phy_device *phydev);
1509int genphy_c45_read_status(struct phy_device *phydev);
1510int genphy_c45_config_aneg(struct phy_device *phydev);
1511
1512
1513extern struct phy_driver genphy_c45_driver;
1514
1515
1516int gen10g_config_aneg(struct phy_device *phydev);
1517
1518static inline int phy_read_status(struct phy_device *phydev)
1519{
1520 if (!phydev->drv)
1521 return -EIO;
1522
1523 if (phydev->drv->read_status)
1524 return phydev->drv->read_status(phydev);
1525 else
1526 return genphy_read_status(phydev);
1527}
1528
1529void phy_driver_unregister(struct phy_driver *drv);
1530void phy_drivers_unregister(struct phy_driver *drv, int n);
1531int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
1532int phy_drivers_register(struct phy_driver *new_driver, int n,
1533 struct module *owner);
1534void phy_error(struct phy_device *phydev);
1535void phy_state_machine(struct work_struct *work);
1536void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
1537void phy_trigger_machine(struct phy_device *phydev);
1538void phy_mac_interrupt(struct phy_device *phydev);
1539void phy_start_machine(struct phy_device *phydev);
1540void phy_stop_machine(struct phy_device *phydev);
1541void phy_ethtool_ksettings_get(struct phy_device *phydev,
1542 struct ethtool_link_ksettings *cmd);
1543int phy_ethtool_ksettings_set(struct phy_device *phydev,
1544 const struct ethtool_link_ksettings *cmd);
1545int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
1546int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
1547int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd);
1548int phy_disable_interrupts(struct phy_device *phydev);
1549void phy_request_interrupt(struct phy_device *phydev);
1550void phy_free_interrupt(struct phy_device *phydev);
1551void phy_print_status(struct phy_device *phydev);
1552int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
1553void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
1554void phy_advertise_supported(struct phy_device *phydev);
1555void phy_support_sym_pause(struct phy_device *phydev);
1556void phy_support_asym_pause(struct phy_device *phydev);
1557void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
1558 bool autoneg);
1559void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
1560bool phy_validate_pause(struct phy_device *phydev,
1561 struct ethtool_pauseparam *pp);
1562void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause);
1563
1564s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
1565 const int *delay_values, int size, bool is_rx);
1566
1567void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv,
1568 bool *tx_pause, bool *rx_pause);
1569
1570int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
1571 int (*run)(struct phy_device *));
1572int phy_register_fixup_for_id(const char *bus_id,
1573 int (*run)(struct phy_device *));
1574int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
1575 int (*run)(struct phy_device *));
1576
1577int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
1578int phy_unregister_fixup_for_id(const char *bus_id);
1579int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
1580
1581int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
1582int phy_get_eee_err(struct phy_device *phydev);
1583int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
1584int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
1585int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
1586void phy_ethtool_get_wol(struct phy_device *phydev,
1587 struct ethtool_wolinfo *wol);
1588int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1589 struct ethtool_link_ksettings *cmd);
1590int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1591 const struct ethtool_link_ksettings *cmd);
1592int phy_ethtool_nway_reset(struct net_device *ndev);
1593int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size);
1594void phy_package_leave(struct phy_device *phydev);
1595int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
1596 int addr, size_t priv_size);
1597
1598#if IS_ENABLED(CONFIG_PHYLIB)
1599int __init mdio_bus_init(void);
1600void mdio_bus_exit(void);
1601#endif
1602
1603int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data);
1604int phy_ethtool_get_sset_count(struct phy_device *phydev);
1605int phy_ethtool_get_stats(struct phy_device *phydev,
1606 struct ethtool_stats *stats, u64 *data);
1607
1608static inline int phy_package_read(struct phy_device *phydev, u32 regnum)
1609{
1610 struct phy_package_shared *shared = phydev->shared;
1611
1612 if (!shared)
1613 return -EIO;
1614
1615 return mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
1616}
1617
1618static inline int __phy_package_read(struct phy_device *phydev, u32 regnum)
1619{
1620 struct phy_package_shared *shared = phydev->shared;
1621
1622 if (!shared)
1623 return -EIO;
1624
1625 return __mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
1626}
1627
1628static inline int phy_package_write(struct phy_device *phydev,
1629 u32 regnum, u16 val)
1630{
1631 struct phy_package_shared *shared = phydev->shared;
1632
1633 if (!shared)
1634 return -EIO;
1635
1636 return mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
1637}
1638
1639static inline int __phy_package_write(struct phy_device *phydev,
1640 u32 regnum, u16 val)
1641{
1642 struct phy_package_shared *shared = phydev->shared;
1643
1644 if (!shared)
1645 return -EIO;
1646
1647 return __mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
1648}
1649
1650static inline bool __phy_package_set_once(struct phy_device *phydev,
1651 unsigned int b)
1652{
1653 struct phy_package_shared *shared = phydev->shared;
1654
1655 if (!shared)
1656 return false;
1657
1658 return !test_and_set_bit(b, &shared->flags);
1659}
1660
1661static inline bool phy_package_init_once(struct phy_device *phydev)
1662{
1663 return __phy_package_set_once(phydev, PHY_SHARED_F_INIT_DONE);
1664}
1665
1666static inline bool phy_package_probe_once(struct phy_device *phydev)
1667{
1668 return __phy_package_set_once(phydev, PHY_SHARED_F_PROBE_DONE);
1669}
1670
1671extern struct bus_type mdio_bus_type;
1672
1673struct mdio_board_info {
1674 const char *bus_id;
1675 char modalias[MDIO_NAME_SIZE];
1676 int mdio_addr;
1677 const void *platform_data;
1678};
1679
1680#if IS_ENABLED(CONFIG_MDIO_DEVICE)
1681int mdiobus_register_board_info(const struct mdio_board_info *info,
1682 unsigned int n);
1683#else
1684static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
1685 unsigned int n)
1686{
1687 return 0;
1688}
1689#endif
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701#define phy_module_driver(__phy_drivers, __count) \
1702static int __init phy_module_init(void) \
1703{ \
1704 return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
1705} \
1706module_init(phy_module_init); \
1707static void __exit phy_module_exit(void) \
1708{ \
1709 phy_drivers_unregister(__phy_drivers, __count); \
1710} \
1711module_exit(phy_module_exit)
1712
1713#define module_phy_driver(__phy_drivers) \
1714 phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
1715
1716bool phy_driver_is_genphy(struct phy_device *phydev);
1717bool phy_driver_is_genphy_10g(struct phy_device *phydev);
1718
1719#endif
1720