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