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