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